
I was writing some haskell code for fun to solve some "knights and knaves" problems, and I have troubles with http://en.wikipedia.org/wiki/Knights_and_knaves#Question_3 So knights always tell the truth and knaves always lie. John and Bill are two persons, but you don't know what they are, and you have to find out. Question 3 You: Are you both knights? John: answers either Yes or No, but you don't have enough information to solve the problem. You: Are you both knaves? John: answers either Yes or No, and you can now solve the problem. My haskell code gave the following result: --(what is john, what is bill, first answer from john, second answer from john) ("John is a knight","Bill is a knight","Yes","No ") ("John is a knight","Bill is a knave ","No ","No ") ("John is a knave ","Bill is a knight","Yes","Yes") ("John is a knave ","Bill is a knave ","Yes","No ") Anyone has an idea what I missed here? Thanks, Peter PS: The (quick and dirty) haskell code I wrote for this is: infixl 1 <=> infixl 1 ==> -- Equivalence x <=> y = (x == y) -- Implication True ==> False = False _ ==> _ = True -- Knights tell the truth name `isa` True = name ++ " is a knight" -- Knaves always lie name `isa` False = name ++ " is a knave " answer True = "Yes" answer False = "No " --Logician: Are you both knights? --John answers either Yes or No, but the Logician does not have enough information to solve the problem. --Logician: Are you both knaves? --John answers either Yes or No, and the Logician can now solve the problem. condition j b answer1 answer2 = (j <=> ((b && j) == answer1)) && (j <=> ((not b && not j) == answer2)) solution = [("John" `isa` j, "Bill" `isa` b, answer answer1, answer answer2) | j <- [True,False], b <- [True, False], answer1 <- [True, False], answer2 <- [True, False], condition j b answer1 answer2 ] main = mapM_ putStrLn (map show solution)

On Thu, 9 Aug 2007 20:07:02 +0200, you wrote:
("John is a knight","Bill is a knight","Yes","No ") ("John is a knight","Bill is a knave ","No ","No ") ("John is a knave ","Bill is a knight","Yes","Yes") ("John is a knave ","Bill is a knave ","Yes","No ")
Anyone has an idea what I missed here?
You're missing a key element of the problem: After John answers the first question, the Logician doesn't have enough information to solve the problem. Think about that for a second, and you will see the light. Steve Schafer Fenestra Technologies Corp. http://www.fenestra.com/

Indeed, I missed that. This rules out the first answer is "no" But I still keep the 3 other solutions then :(
("John is a knight","Bill is a knight","Yes","No ") ("John is a knave ","Bill is a knight","Yes","Yes") ("John is a knave ","Bill is a knave ","Yes","No ")
Any more help (or just the solution, I give up) is very welcome to help this poor man in logic hell ;-) Oh well, it seems I'm getting too old for this stuff ;) -----Original Message----- From: haskell-cafe-bounces@haskell.org [mailto:haskell-cafe-bounces@haskell.org] On Behalf Of Steve Schafer Sent: Thursday, August 09, 2007 8:22 PM To: haskell-cafe@haskell.org Subject: Re: [Haskell-cafe] Problem with question 3 about knights and knaves onw ikipedia On Thu, 9 Aug 2007 20:07:02 +0200, you wrote:
("John is a knight","Bill is a knight","Yes","No ") ("John is a knave ","Bill is a knight","Yes","Yes") ("John is a knave ","Bill is a knave ","Yes","No ")
Anyone has an idea what I missed here?
You're missing a key element of the problem: After John answers the first question, the Logician doesn't have enough information to solve the problem. Think about that for a second, and you will see the light. Steve Schafer Fenestra Technologies Corp. http://www.fenestra.com/ _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 8/9/07, Peter Verswyvelen
I was writing some haskell code for fun to solve some "knights and knaves" . . . John: answers either Yes or No, and you can now solve the problem.
We can write a Haskell *program* to solve this problem. But is there a nice way to make the Haskell *compiler* itself solve this problem? In particular, can we write equations for the values of john and bill and somehow have the type inferencer figure out whether the Haskell types of john and bill are Knight or Knave? -- Dan
participants (3)
-
Dan Piponi
-
Peter Verswyvelen
-
Steve Schafer