Last active
November 4, 2026 23:27
-
-
Save bubba2k/c4c584da1e4c051373e614a3aeac1b90 to your computer and use it in GitHub Desktop.
The Zebra puzzle encoded as a SAT problem with Haskell ersatz library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Ersatz | |
| import Ersatz.Solver.Kissat (kissat) | |
| import Ersatz.Counting | |
| import Control.Monad | |
| import Prelude hiding ( (||), (&&), xor, not, and, or ) | |
| import Control.Monad (replicateM) | |
| import Data.List(transpose, intercalate) | |
| import GHC.Num (naturalIsOne) | |
| import Text.Printf (printf) | |
| {-- | |
| This file contains my solution to the famous Zebra puzzle (also known as the Einstein riddle), | |
| encoded/implemented with the Ersatz library, leveraging the kissat solver. | |
| The puzzle: | |
| Five houses stand in a row. Each has a different color; the nationalities of the residents are distinct; | |
| each resident prefers a specific drink, smokes a specific cigarette brand, and keeps a specific pet. | |
| None of the 5 residents drinks the same drink, smokes the same cigarette brand, | |
| or keeps the same pet as any of their neighbors. | |
| The following hints are given: | |
| 1. The british lives in the red house. | |
| 2. The swedish keeps a dog. | |
| 3. The danish likes to drink tea. | |
| 4. The green house stands to the left of the white house. | |
| 5. The resident of the green house drinks coffee. | |
| 6. The person who smokes pallmall keeps a bird. | |
| 7. The man who lives in the middle house drinks milk. | |
| 8. The resident of the yellow house smokes dunhill. | |
| 9. The norwegian lives in the leftmost house. | |
| 10. The marlboro smoker lives next to the one who keeps a cat. | |
| 11. The man who keeps a horse lives next to the one who smokes dunhill. | |
| 12. The winfield smoker likes to drink beer. | |
| 13. The norwegian lives next to the blue house. | |
| 14. The german smokes rothmanns. | |
| 15. The marlboro smoker has a neighbor who drinks water. | |
| --} | |
| -- For propositions of form "the brit lives in the red house" or "the inhabitant of the green house drinks coffe", and so on; | |
| -- eg: `interdependent [brit1, brit2, brit3, brit4, brit5] [red1, red2, red3, red4, red5]` | |
| -- checks whether the red brit lives in the red house. | |
| interdependent :: [Bit] -> [Bit] -> Bit | |
| interdependent as bs = and $ zipWith (===) as bs | |
| -- Check whether the first property applies to the left neighbour of the holder of the second property | |
| -- eg: "The green house is to the left of the right house" corresponds to `leftNeighbour green white` | |
| leftNeighbourOf :: [Bit] -> [Bit] -> Bit | |
| leftNeighbourOf as bs = let len = length as | |
| in (not $ as !! (len - 1)) && ( and $ zipWith (===) as (drop 1 bs) ) | |
| -- Check whether the two properties are neighbours of eachother (in arbitrary order) | |
| neighbourOf :: [Bit] -> [Bit] -> Bit | |
| neighbourOf as bs = (as `leftNeighbourOf` bs) || (bs `leftNeighbourOf` as) | |
| -- SAT encoding of the problem | |
| problem :: (MonadSAT s m, MonadFail m) | |
| => m ([[Bit]], [[Bit]], [[Bit]], [[Bit]], [[Bit]]) | |
| problem = do | |
| colors@[red, blue, green, yellow, white] <- replicateM 5 (replicateM 5 (exists @Bit)); | |
| nationalities@[british, german, norwegian, danish, swedish] <- replicateM 5 (replicateM 5 (exists @Bit)); | |
| cigarettes@[pallmall, dunhill, marlboro, winfield, rothmanns] <- replicateM 5 (replicateM 5 (exists @Bit)); | |
| pets@[dog, bird, cat, horse, fish] <- replicateM 5 (replicateM 5 (exists @Bit)); | |
| drinks@[tea, coffee, milk, beer, water] <- replicateM 5 (replicateM 5 (exists @Bit)); | |
| -- EXOs - forbid duplicates, | |
| -- for each domain... | |
| mapM (assert . exactly 1) colors | |
| mapM (assert . exactly 1) nationalities | |
| mapM (assert . exactly 1) cigarettes | |
| mapM (assert . exactly 1) pets | |
| mapM (assert . exactly 1) drinks | |
| -- ... and for each house | |
| mapM (assert . exactly 1) ( transpose colors ) | |
| mapM (assert . exactly 1) ( transpose nationalities ) | |
| mapM (assert . exactly 1) ( transpose cigarettes ) | |
| mapM (assert . exactly 1) ( transpose pets ) | |
| mapM (assert . exactly 1) ( transpose drinks ) | |
| assert $ interdependent british red -- 1 | |
| assert $ interdependent swedish dog -- 2 | |
| assert $ interdependent danish tea -- 3 | |
| assert $ green `leftNeighbourOf` white -- 4 | |
| assert $ interdependent green coffee -- 5 | |
| assert $ interdependent pallmall bird -- 6 | |
| assert $ (milk !! 2) -- 7 | |
| assert $ interdependent yellow dunhill -- 8 | |
| assert $ (norwegian !! 0) -- 9 | |
| assert $ marlboro `neighbourOf` cat -- 10 | |
| assert $ horse `neighbourOf` dunhill -- 11 | |
| assert $ interdependent winfield beer -- 12 | |
| assert $ norwegian `neighbourOf` blue -- 13 | |
| assert $ interdependent german rothmanns -- 14 | |
| assert $ marlboro `neighbourOf` water -- 15 | |
| return (colors, nationalities, cigarettes, pets, drinks) | |
| -- Solve the problem with kissat and pretty-print the solution | |
| main = do | |
| -- Expects kissat to be on path - you can also change this path, | |
| -- or use a different solver supported by Ersatz (e.g. minisat) | |
| let myKissatPath = "kissat" | |
| (_, ma) <- solveWith (kissatPath myKissatPath) problem | |
| putStr (maybe "" displayAssignment ma) | |
| --------- generated code for convenient pretty printing of the solution --------- | |
| -- Names of the properties, in the same order as in `problem`. | |
| colorNames, nationalityNames, cigaretteNames, petNames, drinkNames :: [String] | |
| colorNames = ["red", "blue", "green", "yellow", "white"] | |
| nationalityNames = ["british", "german", "norwegian", "danish", "swedish"] | |
| cigaretteNames = ["pallmall", "dunhill", "marlboro", "winfield", "rothmanns"] | |
| petNames = ["dog", "bird", "cat", "horse", "fish"] | |
| drinkNames = ["tea", "coffee", "milk", "beer", "water"] | |
| -- Return the name of the property that holds for house i. | |
| propertyAt :: [String] -> [[Bool]] -> Int -> String | |
| propertyAt names rows i = | |
| case [name | (name, row) <- zip names rows, row !! i] of | |
| [name] -> name | |
| _ -> "?" | |
| -- Format a given assignment (/solution) of this problem to a table | |
| displayAssignment :: ([[Bool]], [[Bool]], [[Bool]], [[Bool]], [[Bool]]) -> String | |
| displayAssignment (colors, nationalities, cigarettes, pets, drinks) = | |
| unlines (header : separator : map row [0 .. 4]) | |
| where | |
| header = printf "%-5s | %-7s | %-11s | %-9s | %-6s | %s" | |
| "House" "Color" "Nationality" "Cigarette" "Pet" "Drink" | |
| separator = intercalate "-+-" | |
| [ replicate 5 '-' | |
| , replicate 7 '-' | |
| , replicate 11 '-' | |
| , replicate 9 '-' | |
| , replicate 6 '-' | |
| , replicate 5 '-' | |
| ] | |
| row i = printf "%-5d | %-7s | %-11s | %-9s | %-6s | %s" | |
| (i + 1) | |
| (propertyAt colorNames colors i) | |
| (propertyAt nationalityNames nationalities i) | |
| (propertyAt cigaretteNames cigarettes i) | |
| (propertyAt petNames pets i) | |
| (propertyAt drinkNames drinks i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment