A Method to Solve the Puzzles of Knights and Knaves ∗ L. Aszalós † Abstract In his book ”What is the name of this book?” Raymond M. Smullyan used a lot of puzzles to illustrate the background of the Gödel incompleteness theorem. These puzzles became popular and nowadays are being published in amusement magazines, too. In each section of the book different conditions are met. In the best known type of puzzles we have only two types of people, knights and knaves. Knights always tell the truth and knaves always lie. We describe a method to formulate this kind of puzzles, then we prove the soundness and the completeness of this logic. At the end we give Prolog programs for solving the puzzles. 1 The logic of puzzles — Introduction In the puzzles the inhabitants make statements about themselves and about the others, for example ‘Mr. A is a knave.’ or ‘Mrs. B said that she was not a knave.’ Usually, to solve a puzzle we must determine the type of persons. If we want to formulate the puzzles, we need to express statements of the following kinds: the person x is a knight, the person x is a knave and the person x can say the statement B. In these cases we shall write Tx , Fx , and Sx B, respectively. In his puzzles Smullyan uses say which sounds better than can say, but we shall use the latter. If somebody said something, then he is able to say it, so he can say it, hence we do not loose information when we use can say instead of say. If somebody didn’t say a sentence B, then we have no information whether he cannot say B or he can but just for some reason he didn’t. In [2] there is no puzzle formulated such that it contains the negation of say, but why should conform to this restriction? Of course, we could have a problem in the formalization of puzzles with say, because if x said that y has not said the statement B earlier (which is true), and later y says B, then we must use tense logic to handle this situation or we must restrict our tools and definitions in some way. But if we use can say, then the former situation cannot occur so our life will be easier, we can formulate puzzles easily and we can use the negation of can say in the puzzles, too. ∗ Research was partly supported by the grant OTKA no. 354-19341 of Debrecen, 4010 Debrecen, PO Box 12, Hungary, e-mail: aszalos@math.klte.hu † University 1 2 L. Aszalós We could demonstrate our method better if we complicate the puzzles and if we introduce a third type of inhabitants: normals. Normals sometimes tell the truth and are sometimes lying. If the person x is a normal, then we shall write Nx . The set of islanders is denoted by P. Of course we have only finitely many islanders, so the set P is finite. Our connectives are the usual ones: ¬ (negation), ∧ (conjunction), ∨ (disjunction) and ⊃ (implication) and we are using the > (constant true) and the ⊥ (constant false). In the propositional logic the atomic statements are denoted by propositional letters (for example by Q, R, ...). The set of atomic statements (propositional constants) is denoted by S. The definition of the set of formulae is the following: Let P and S be finite sets. The set of formulae is the smallest set F such that • > ∈ F and ⊥ ∈ F. • S ⊂ F, • If A ∈ F, then ¬A ∈ F. • If A ∈ F and B ∈ F, then (A ⊃ B) ∈ F, (A ∨ B) ∈ F and (A ∧ B) ∈ F. • If x ∈ P then Tx ∈ F, Fx ∈ F and Nx ∈ F. • If x ∈ P and B ∈ F, then Sx B ∈ F. The valuation ϑ assigns a truth value to every formula. If the formula A is true in a valuation ϑ, this is denoted by ϑ |= A. The properties of the islanders can be coded into the valuation: Let ϑS ⊂ S (ϑS is the set of true atomic statements), ϑT : P → {t, f, n} (ϑT is the classification of islanders). ϑ = hϑS , ϑT i is a valuation over F if: • ϑ|= >, ϑ|6 = ⊥. • If Q ∈ S then ϑ|= Q iff Q ∈ ϑS . • ϑ|= ¬A iff ϑ|6 = A. • ϑ|= A ⊃ B iff ϑ|6 = A or ϑ|= B. • ϑ|= A ∨ B iff ϑ|= A or ϑ|= B. • ϑ|= A ∧ B iff ϑ|= A and ϑ|= B. • If x ∈ P then ϑ|= Tx iff ϑT (x) = t • If x ∈ P then ϑ|= Fx iff ϑT (x) = f • If x ∈ P then ϑ|= Nx iff ϑT (x) = n • ϑ|= Sx B iff ϑ|= Tx and ϑ|= B or ϑ|= Fx and ϑ|6 = B or ϑ|= Nx . A Method to Solve the Puzzles of Knights and Knaves 3 A formula A is valid if for every valuation ϑ ϑ|= A and it is satisfiable if there exists a valuation ϑ such that ϑ|= A. We shall use analytic tableaux [1, pp. 20-21] to check the provability of formulae. We shall use the notation and definitions of this book, but 1) in the nodes (except the root) we allow not only a formula but the set of formulae, 2) we need rules about can say: ¬Sx B Sx B Tx ¬Tx ¬Tx Tx ¬Tx and Fx ¬Fx Fx ¬Fx ¬Fx ¬Nx ¬Nx Nx ¬Nx ¬Nx ¬B B B ¬B 2 Soundness First we prove that our proving tool, the tableaux method is correct, that is, if we can prove a formula using it, then the formula is valid. Theorem 1 If the tableaux for a formula F is closed, then F is unsatisfiable. Proof. Let ϑ be a valuation that makes F true. We prove by mathematical induction (on the size of the tree) that the tableaux for F is open. If the tableaux has only one node (root), then the claim is true. Let us assume that Υ is a tableaux for F , which is true in ϑ. Hence, Υ is open. Let Υ0 be an extension of Υ. Υ has an open path, say p. If Υ0 is obtained by an application of a rule for a formula from another path, then the path p is an open path of Υ0 , so Υ0 is an open tableaux. Otherwise, we get Υ0 by applying a rule for the formula A in the path p of Υ. • If A is an α formula then the new node contains formulae α1 and α2 so the new path contains them, too. The formula α is on the true path, so it is true. By definition of the valuation, formulae α1 and α2 are true, too. Hence the new path is open. • If A is a β formula then the new nodes contain formulae β1 and β2 , respectively. So the new path contains exactly one of them. β is true, so by definition, one of β1 and β2 is true, hence one of the new paths is open. • If A is a Sx B type formula, it is true in ϑ and by definition of ϑ |= Sx B we know that (ϑ |= Tx and ϑ |= B) or (ϑ |= Fx and ϑ |= ¬B) or ϑ |= Nx is true. In the first case ϑ|= ¬Fx and ϑ|= ¬Nx , in the second case ϑ| = ¬Tx and ϑ|= ¬Nx , and in the third case ϑ| = ¬Tx and ϑ|= ¬Fx by definition of ϑT , so one of the new paths is open. • If A is a ¬Sx B type formula, then by ϑ6|= Sx B we know that ϑ|= Fx and ϑ|= B or ϑ |= Tx and ϑ |= ¬B. In the first case ϑ |= ¬Tx and ϑ |= ¬Nx , in the second case ϑ|= ¬Fx and ϑ|= ¬Nx by definition of ϑT , so one of the new paths is open, too. 4 L. Aszalós Now a closed tableaux Υ cannot be true under any interpolation, so the origin of any closed tableaux must be unsatisfiable. From this follows that every formula provable by the tableaux method must be valid. 2 3 Completeness In this section we want to show that any valid formula is provable, so the method is complete. For this purpose we shall introduce a function that assigns a natural number to every formula. This number represents a measure of complexity of the formula. In the inductive proof below we use these numbers. The degree of a formula is defined as follows • The degree of >, ⊥, of all elements of S, of Tx , Fx and Nx is 0. • The degree of ¬A and the degree of A are the same. • If the degree of A is n and the degree of B is m, then the degrees of (A ∧ B), (A ∨ B) and (A ⊃ B) are max(m, n) + 1. • If the degree of B is n, then the degree of Sx B is n + 1. The Hintikka set is a usual tool to show the completeness of a logic. Our logic is more complicated than the propositional logic, so the following definition contains four new items. The set of formulae U is a Hintikka set if the following holds: H0 U does not contain a predicate letter and its negation simultaneously. H1 If α ∈ U, then α1 ∈ U and α2 ∈ U. H2 If β ∈ U, then β1 ∈ U or β2 ∈ U. H3 If x ∈ P, then only one of Tx , Fx and Nx is in U. H4 If x ∈ P then not all of ¬Tx , ¬Fx and ¬Nx are the members of U H5 If Sx B ∈ U, then {Tx , ¬Fx , ¬Nx , A} or {¬Tx , Fx , ¬Nx , ¬A} or {¬Tx , ¬Fx , Nx , } is the subset of the set of formulae U. H6 If ¬Sx B ∈ U, then {¬Tx , Fx , ¬Nx , A} or {Tx , ¬Fx , ¬Nx , ¬A} is the subset of the set of formulae U. Lemma 2 Every Hintikka set is satisfiable. Proof. Let U is a Hintikka set. We make a suitable ϑ, in which all the members of U are true. Let ϑS be the set of all predicate letters which are in U. • If Tx ∈ U or {¬Fx , ¬Nx } ⊂ U, then ϑT (x) is t. • If Fx ∈ U, or {¬Tx , ¬Nx } ⊂ U, then ϑT (x) is f . A Method to Solve the Puzzles of Knights and Knaves 5 • If Nx ∈ U, or {¬Tx , ¬Fx } ⊂ U, then ϑT (x) is n. If none of the former three points hold, then • If ¬Tx ∈ / U, then ϑT (x) is t. • If ¬Fx ∈ / U, then ϑT (x) is f . • If ¬Nx ∈ / U, then ϑT (x) is n. By H3 and H4, by this rules we can assign a value to every x. Now all of the formulae of degree 0 in U are true in hϑS , ϑT i. Let A be a formula with a degree greater than 0. • If A is an α formula, then by H1 α1 ∈ U and α2 ∈ U and their degrees are smaller than the degree of α; by induction hypotheses α1 and α2 are true in ϑ and so α. • If A is a β formula, then the proof is similar. • If A is a Sx B formula, then by H5 {Tx , ¬Fx , ¬Nx , B} or {¬Tx , Fx , ¬Nx , ¬B} or {¬Tx , ¬Fx , Nx } is the subset of the set of formulae U. All this formulae are of degree smaller than the degree of Sx B, so all of them are true in ϑ and by definition ϑ|= Sx B. • The case of ¬Sx B is similar. We have constructed a valuation ϑ such that every formula of U is true in ϑ, so U is satisfiable. 2 Theorem 3 Let be F is a formula. If F is unsatisfiable then the tableaux for F is closed. Proof. Let F be unsatisfiable. Construct the full tableaux for F and assume that this tableaux has an open path. By the construction, the set of formulae of this path is a Hintikka set. By the previous lemma there exists a valuation ϑ such that all formulae of this path are true in it, hence F is true, too. This contradicts our hypothesis. The origin of this contradiction was our assumption, so the tableaux has no open path. 2 The theorem above claims that our logic is complete. If we have a valid formula A, then we can construct a tableaux for ¬A. This formula is unsatisfiable, so its tableaux is closed. 4 4.1 Other type of puzzles The Island of Knights and Knaves Let us return to the original puzzles! If we have only two type of inhabitants then the definitions are simpler than before: in the definition of the formulae we 6 L. Aszalós do not need Nx , we have only two types of inhabitants, hence we need a different ϑT : P → {t, f } and one item of the definition of the valuation is changed: ϑ|= Sx B iff (ϑ|= Tx and ϑ|= B) or (ϑ|= Fx and ϑ6|= B). The rules for can say are the following: ¬Sx B Sx B Tx ¬Tx Tx ¬Tx and Fx Fx ¬Fx ¬Fx B ¬B ¬B B The lemmas and theorems are the same as before, but the definition of the Hintikka set is changed due the changes in preconditions. We leave to the reader to rewrite and check the definitions and proofs. 4.2 Island of Knights, Knaves, Normals and Mutes To get a symmetric type of puzzles, we shall introduce a fourth type of inhabitants. This new type is the opposite to the normals. Any inhabitant of this kind can say nothing, so we call them mutes, and we shall write Mx if the person x is a mute. We need to revise the definition of formulae according to this new type of persons. Of course we need a different ϑT : P → {t, f, n, m}, but the definition of ϑ |= Sx B will be remain the same. The other definitions and proofs can be extend according to the rules: ¬Sx B Sx B Tx ¬Tx ¬Tx Tx ¬Tx ¬Tx Fx ¬Fx Fx ¬Fx ¬Fx ¬Fx and ¬Nx ¬Nx Nx ¬Nx ¬Nx ¬Nx ¬Mx ¬Mx Mx ¬Mx ¬Mx ¬Mx ¬B B B ¬B but we left it to the reader. 4.3 Island of Bahava On the Island of Bahava there are exactly tree types of islanders. Each knight’s spouse is a knave and conversely. Let us denote by x0 the spouse of x. Of course, we shall identify x00 and x, so we shall write x instead of x00 without notifying it. Our rules are extended to Sx B ¬Sx B Tx ¬Tx ¬Tx Tx ¬Tx ¬Fx Fx ¬Fx ¬Fx Fx ¬Nx Nx ¬Nx ¬Nx ¬Nx and ¬Tx0 Tx0 ¬Tx0 ¬Tx0 Tx0 Fx0 ¬Fx0 ¬Fx0 Fx0 ¬Fx0 ¬Nx0 ¬Nx0 N x0 ¬Nx0 ¬Nx0 B ¬B ¬B B A Method to Solve the Puzzles of Knights and Knaves 5 7 Proof of the solution of two puzzles The puzzle 40 of [2] is the following: On the Island of Knights, Knaves and Normals A said that B is a knight. B said that A is a knave. Let us prove that one of them told the truth but is not a knight or that one of them told a lie but is not a knave. To prove the statement above we need to construct the tableau-proof of the formula Sa Tb ∧ Sb Fa ⊃ (Tb ∧ ¬Ta ) ∨ (Fa ∧ ¬Tb ) ∨ (¬Tb ∧ ¬Fa ) ∨ (¬Fa ∧ ¬Fb ). After a few of uninteresting steps we get the proof-tree on Fig. 1. On this picture we have boxed the subtrees instead of writing them vertically. The formulae closing the paths are denoted by a star. Ta , ¬Fa , ¬Na , Tb Tb , ¬Fb , ¬Nb , Fa ∗ ¬Ta , Fa , ¬Na , ¬Tb Tb ∗ , ¬Fb , ¬Nb , Fa ¬Tb ∗ , Fb , ¬Nb , ¬Fa ¬Tb , Fb , ¬Nb , ¬Fa ∗ ¬Tb ∗ , ¬Fb , Nb ¬Tb , ¬Fb , Nb Tb ∗ ¬Fa ∗ ¬Ta , ¬Fa , Na Tb , ¬Fb , ¬Nb , Fa ∗ ¬Tb , Fb , ¬Nb , ¬Fa Tb ∗ Fa ∗ ¬Tb , ¬Fb , Nb Tb ∗ Fa ∗ Figure 1. The puzzle 44 of [2] states that on Island of Bahava Mr. A said that his wife is not a normal. Mrs. A said that her husband is not a normal. We are proving that both of them are normals on Fig. 2. ¬(Sa ¬Na0 ∧ Sa0 ¬Na ⊃ Na ∧ Na0 ) Sa ¬Na0 , Sa0 ¬Na , ¬Na ∨ ¬Na0 Ta , ¬Fa , ¬Na , ¬Ta0 , Fa0 , ¬Na0 , ¬Na0 Ta0 ∗ , ¬Fa0 , ¬Na0 , ¬Ta , Fa , ¬Na , ¬Na ¬Ta0 , Fa0 , ¬Na0 , Ta , ¬Fa , ¬Na , Na ∗ ¬Ta0 , ¬Fa0 ∗ , Na0 , ¬Ta , ¬Fa , ¬Na ¬Ta , Fa , ¬Na , Ta0 , ¬Fa0 , ¬Na0 , Na0 ∗ ¬Ta , ¬Fa , Na , ¬Ta0 , ¬Fa0 , Na0 ¬Na ∗ ¬Na0 ∗ Figure 2. 8 6 L. Aszalós Methods of Automated Puzzle Solving There are several methods for solving the puzzles of the Island of Knight and Knaves. In 1998 Piotr Rudnicki used the formalism of his proof-checker Mizar-MSE when he formalized the puzzles in his syllabus for CMPUT 272 at the University of Alberta. In the newsgroup comp.lang.prolog Ralph Becket showed a method in which (in our notation) the person x says that A is written as Tx ≡ A and our puzzles are translated to formulae of predicate logic that we can solve easily. David Gries used a similar method, but he tests the validity of predicate logic formulae with rewriting. In the late eighties Larry Wos formulated puzzles as a clause-cluster form of first-order sentences [4]. He applied the first-order theorem prover Otter to solve the puzzles. Lee Naish wrote a NU-Prolog program to test the puzzles. He published his program in newsgroup comp.lang.prolog at 11 Feb 2000. Independently of Lee Naish on the basis of [3] we wrote several programs to solve the puzzles. We shall use the programming language Prolog to construct the program to solve puzzles of the Island of Knights, Knaves and Normals. (The programs for the other type of puzzles are similar.) For the sake of simplicity we shall define some operators ::::::- op(930, op(920, op(910, op(909, op(900, op(901, xfx, xfx, xfy, xfy, fx, xfy, === ). -> ). \/ ). /\ ). ~ ). s ). /* /* /* /* /* /* equivalence implication disjunction conjunction not can say */ */ */ */ */ */ The canBeTrue and the canBeFalse are two predicates to test whether there is a valuation in which the formula in the argument is true/false. For the predicate logic the following code is well-known [3, Program 3.31]: canBeTrue(true) canBeTrue(~ X) canBeTrue(X -> Y) canBeTrue(X \/ Y) canBeTrue(X /\ Y) canBeTrue(X === Y) :- !. :- canBeFalse(X). :- canBeFalse(X); canBeTrue(Y). :- canBeTrue(X); canBeTrue(Y). :- canBeTrue(X), canBeTrue(Y). :-(canBeTrue(X), canBeTrue(Y)); (canBeFalse(X), canBeFalse(Y)). canBeFalse(false) :- !. canBeFalse(~ X) :- canBeTrue(X). canBeFalse(X -> Y) :- canBeTrue(X), canBeFalse(Y). canBeFalse(X \/ Y) :- canBeFalse(X), canBeFalse(Y). canBeFalse(X /\ Y) :- canBeFalse(X); canBeFalse(Y). canBeFalse(X === Y) :-(canBeTrue(X), canBeFalse(Y)); (canBeFalse(X), canBeTrue(Y)). We shall write knight(X), knave(X) and normal(X) instead of Tx , Fx and Nx , respectively. The definition ϑT is coded as below: A Method to Solve the Puzzles of Knights and Knaves 9 canBeTrue(knight(knight)). canBeTrue(knave( knave) ). canBeTrue(normal(normal)). canBeFalse( canBeFalse( canBeFalse( canBeFalse( canBeFalse( canBeFalse( knight(knave) ). knave(knight) ). knight(normal) ). knave(normal) ). normal(knight)). normal(knave)). The definition of ϑ|= Sx B is coded as below: canBeTrue(knight s X) :- canBeTrue(X). canBeTrue(knave s X) :- canBeFalse(X). canBeTrue(normal s _). canBeFalse(knight s X) :- canBeFalse(X). canBeFalse(knave s X) :- canBeTrue(X). We can write down the preconditions of puzzle 40 of [2] puzzle(w40, A s knight(B) /\ B s ~knight(A)). and we can test models of the puzzle by the question: ?- puzzle(w40, Formula), canBeTrue(Formula) If we want to check the validity of the formula of the puzzle then we can ask the following: canBeFalse(A s knight(B) /\ B s knave(A) -> ( knight(B) /\ ~ knight(A) ) \/ ( knave(A) /\ ~ knight(B) ) \/ ( ~ knight(B) /\ ~ knave(A)) \/ (~ knave(A) /\ ~ knave(B) ) ). In the program above we cheated. For the sake of simplicity we have used the first-order predicates of Prolog. We could substitute the Horn-fact KnightA for the predicate knight(A), and consistently in the others cases to obtain the same program as our tableaux method, but this has no effect on the results. 7 Conclusion We have shown a method to formulate Smullyan’s puzzles. We have proved soundness and completeness of several logics of puzzles, in meantime we introduced some new types of puzzles that do not occur in [2]. We have shown a Prolog program that automatizes the solution of puzzles. This program can be easily improved to be suitable for other types of puzzles, too. 10 8 L. Aszalós Acknowledgements I would like to express my thanks to K. Pásztor-Varga for her many helpful criticisms during the preparation of this paper. I would like to thank late A. Kron for his help, and for trying valiantly to make this a more readable paper. References [1] Raymond M. Smullyan. First-order logic. Springer-Verlag New York, Inc., New York, 1968. Ergebnisse der Mathematik und ihrer Grenzgebiete, Band 43. [2] Raymond M. Smullyan. What is the Name of This Book? (The Riddle of Dracula and Other Logical Puzzles). Prentice-Hall Inc., 1978. [3] L. Sterling and E. Shapiro. The Art of Prolog. MIT Press., 1986. [4] L. Wos, R. Overbeek, E. Lusk and J. Boyle. Automated Reasoning: Introduction and Applications. McGraw-Hill., 1992.