Solving Logic Grid Puzzles in Haskell
The Zebra Puzzle is a decades-old exercise in deductive logic. Unfortunately, I lack the patience to sit down and solve this kind of puzzle. So in this post, we're going to cheat by teaching Haskell to solve it for us.
We will take our cue from this solution written in SWI Prolog, and encode our puzzle as a set of constraints that our solving code must satisfy.
An 'entry' is a single entity in the solution. In the Zebra puzzle, these are going to be the individual houses. For maximum generality, we're just going to represent all the properties of the entries with strings.
An answer to the puzzle is a collection of several entities which together satisfy all of the rules set forth in the puzzle description.
I was going to explain the rule types at this point, but the explanation ended up being a lot harder to understand than just looking at the rules for this puzzle, so we'll do that instead.
The first four rules simply teach our solver about numbers. After these rules are satisfied, the entries in the solution will be numbered one through five.
> [ Follows [ "1", "", "", "", "", "" ]
> [ "2", "", "", "", "", "" ]
> , Follows [ "2", "", "", "", "", "" ]
> [ "3", "", "", "", "", "" ]
> , Follows [ "3", "", "", "", "", "" ]
> [ "4", "", "", "", "", "" ]
> , Follows [ "4", "", "", "", "", "" ]
> [ "5", "", "", "", "", "" ]
Then we specify all of the clues that make up the puzzle itself. These are given in order, so it should be easy to see the correspondence with the clues listed in the wikipedia article
> , Literal [ "", "Spain", "Dog", "", "", "" ]
> , Literal [ "", "", "", "Coffee", "Green", "" ]
> , Literal [ "", "Ukraine", "", "Tea", "", "" ]
> , Follows [ "", "", "", "", "Green", "" ]
> [ "", "", "", "", "Ivory", "" ]
> , Literal [ "", "", "Snails", "", "", "Old Gold" ]
> , Literal [ "", "", "", "", "Yellow", "Kools" ]
> , Literal [ "3", "", "", "Milk", "", "" ]
> , Literal [ "1", "Norway", "", "", "", "" ]
> , Adjacent [ "", "", "", "", "", "Chesterfields" ]
> [ "", "", "Fox", "", "", "" ]
> , Adjacent [ "", "", "", "", "", "Kools" ]
> [ "", "", "Horse", "", "", "" ]
> , Literal [ "", "", "", "Juice", "", "Lucky Strike" ]
> , Literal [ "", "Japan", "", "", "", "Parliaments" ]
> , Adjacent [ "", "Norway", "", "", "", "" ]
> [ "", "", "", "", "Blue", "" ]
Unfortunately, one drink and one animal are missing from the rules as stated, so here we just inform the solver "someone drinks water" and "someone owns a zebra"
> , Literal [ "", "", "Zebra", "", "", "" ]
> ]
So we have three kinds of rules, for which we'll need a data definition. By now it should be self-evident how each of these work
At this point, we have a nice, declarative specification of what a solution will look like, and we need to write the code to solve for it. The key to solving the puzzle efficiently is to realize that each rule is effectively describing some small portion of a possible answer, with empty strings representing unknown values. What we need next is a way to expand a rule into a list of all those answers that it represents
> expandRule n (Literal a ) = [ expand n [ a ] x | x <- [0 .. n - 1] ]
> expandRule n (Follows a b) = [ expand n [a,b] x | x <- [0 .. n - 2] ]
> expandRule n (Adjacent a b) = concat [e $ Follows a b, e $ Follows b a]
> where e = expandRule n
>
> expand :: Int -> [Entry] -> Int -> [Entry]
> expand n rs@(r:_) x = replicate x blank ++ rs ++ replicate (n - x - 1) blank
> where blank = replicate (length r) ""
As we go about solving the puzzle, we will at all times have a collection of answers the solver currently knows to be possible, and a set of answer fragments resulting from expanding the rule we're currently trying to satisfy. What we need is a way to test every possible combination of the old answers with the new answer fragments.
Our solution will make use of the nondeterminism monad, called the "list" monad by the unenlightened. As we iterate over the rules, we will expand each one in turn, test every possible combination with the old answers, and then filter out the impossible ones.
At first, this will cause a combinatorial explosion of possible answers, but as new rules are added, we will eventually reach a point where each additional rule manages only to decrease the number of possible solutions, until there is only one remaining.
> applyRules answer rules = foldM applyRule answer rules
> where applyRule a r = catMaybes [overlay a x | x <- expandRule (length a) r]
From the definition of applyRules, it is clear that our overlay operation needs to have type Answer -> Answer -> Maybe Answer. If any two answers are both defined and different from each other, we return Nothing, and otherwise we return the most defined of the two fields.
> overlay old new = sequence $ zipWith overlay' old new
> where overlay' old new = sequence $ zipWith overlay'' old new
> overlay'' "" "" = Just ""
> overlay'' "" n = Just n
> overlay'' o "" = Just o
> overlay'' o n
> | o == n = Just o
> | otherwise = Nothing
Before any constraints are applied, the answer is entirely undefined, so the process of solving consists simply of applying all the rules to an initial empty answer, and seeing what results. In this case, there is only one answer, but removing one or more rules from the list can make other solutions equally valid.
To see how each additional rule changes the set of possible answers, you can try something like "mapM_ print . applyRules emptyAnswer . take ## $ rules" in GHCi, for all the numbers between 1 and 20.
(This post is Literate Haskell, meaning that it can be copied and pasted in its entirety into a *.lhs file, and then run with runhaskell)
Brian’s (Purely) Functional Brain
So about a week ago I came across an interesting post in which the author implemented the Brian's Brain cellular automaton in 67 lines of Clojure. Not about to let my favorite language be outdone, I thought I'd see how well Haskell would do with the same task.
Then I was kept horribly busy for a week by schoolwork, so a couple of days ago I started playing around with the problem. The results? Not too shabby!
So first of all, we'll be needing some imports. Since (for some odd reason) Haskell requires all imports up front, and since this blog post is supposed to be Literate Haskell, you'll have to just trust me that we'll need these for now:
> import System.Random -- Used to generate the initial random world
> import Control.Monad -- Used for some fancy looping constructs
> import Control.Concurrent -- Used to fork the quit event handler
> import Graphics.UI.SDL as SDL -- Used to draw the pretty pictures
> import Control.Parallel.Strategies
Cells can be either On, Dying, or Off:
For convenience, let's define some constants:
> worldY = 90 -- The vertical size of the world
> cellSize = 8 -- The overall size of a cell
> border = 1 -- The border width between cells
> screenX = worldX * cellSize -- The horizontal size of the world, in pixels
> screenY = worldY * cellSize -- The vertical size of the world, in pixels
> fillSize = cellSize - border -- The size of the filled area in each cell
Cells progress from On to Dying to Off, and they turn on only when they have exactly two live neighbors.
> stepCell (Dying, _) = Off -- Dying cells always turn off
> stepCell (Off, 2) = On -- If a dead cell has 2 live neighbors, turn on
> stepCell (Off, _) = Off -- Otherwise, just stay turned off
Since we know from the rules that we'll need the ability to count a cell's live neighbors, let's get that out of the way next.
> where neighbors = [getCell x y | x <- [x-1 .. x+1], y <- [y-1 .. y+1]]
> getCell x y = world ! (clip worldX x, clip worldY y)
> clip max val | val < 1 = clip max $ val + max - 1
> | val > max = clip max $ val - max + 1
> | otherwise = val
So now we have all the pieces to progress from one world state to the next. For each position in the array, we need to look up all its neighbors, count the live ones, and then pass that data to the stepCell function. The helper function indexArray creates an array of cell indices. We map over this array to generate new values for each cell.
The `using` parArr rwhnf is some Haskell magic which causes the array to be evaluated in parallel:
> stepWorld w = newWorld `using` parArr rwhnf
> where newWorld = fmap (stepCell . getPeers w) $ indexArray worldX worldY
Now we have all we need to run a simulation, but it's not quite enough if you insist on getting some pretty pictures. For my fancy display purposes, I happen to like SDL.
The main function initializes SDL, generates a random initial state, produces an infinite list of future world states from that, and then draws each of the states in turn:
> SDL.init [SDL.InitVideo]
> SDL.setCaption "Brian's Purely Functional Brain" "Brian's Brain"
> surface <- SDL.setVideoMode screenX screenY 24 [SDL.DoubleBuf]
> forkIO . forever $ waitEvent >>= \e -> when (e == Quit) quit
> mapM (drawWorld surface) (iterate stepWorld $ world rng)
> where world = listArray ((1,1),(worldX,worldY)) . map toEnum . randomRs (0,2)
And our world drawing function is positively boring. We map over each combination of X and Y values, draw each one, and then flip the resulting image on-screen:
> SDL.flip s
> where draw x y = SDL.fillRect s (Just rect) . color $ w ! (x,y)
> where rect = SDL.Rect (scale x) (scale y) fillSize fillSize
> scale n = (n - 1) * cellSize
> color On = SDL.Pixel 0x00FFFFFF
> color Dying = SDL.Pixel 0x00888888
> color Off = SDL.Pixel 0x00000000
To take full advantage of the parallelism in this program, you'll need to compile with the threaded runtime and run it on multiple OS threads.
./BriansBrain +RTS -N2
And then just sit back and watch the mesmerising patterns.
Haskell While Loop
In the past week, while working on three completely unrelated Haskell projects, I have found myself in need of a while loop. So I came up with this one:
Hoogle shows no functions with that type signature, and I couldn't find anything in the documentation that seemed to fit the bill. This seems like a rather fundamental function for getting stuff done, is there something I'm missing?
EDIT: The most code-golfed version I have yet come up with is while p a = p >>= flip when (a >> while p a) can anyone improve on that?
The Magic Dot
So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions,
is equivalent to the composition operation. I think
is possibly the coolest function in all of Haskell, so it annoys me when it requires six characters just to use it as an infix operator.
So I present what is, character-for-character, the coolest Haskell trick I have seen so far:
import Prelude hiding ((.)) import Control.Monad.Instances a . b = a `fmap` b infixr 9 .
And now, all sorts of fun little tricks are possible, like replacing
with
, and replacing
with
And of course, the same function composition magic still works like always. Maybe it's just me, but something this simple, elegant, and fun to use just makes me happy inside.
But finally, I have to ask: is there any way of specifically declaring that a module *replaces* the standard Prelude (or even better, individual elements of it)? It would be really nice to just type
and not have to also bother with adding
Haskell Exceptions
Haskell's absolute worst feature, in my opinion, is its system of exceptions. I have yet to see a single use of exceptions that wouldn't be better served by the use of a "Maybe" or an "Either" type. Luckily, in a small nod to sanity, Haskell provides 'Control.Exception.Base.try', which returns an Either type, with the exception left, and the value right.
Once you have that, it becomes easy to implement some sane functionality for exception handling, such as my current favorite functions, defaultOnError and errorToMaybe:
Useful Haskell List Function
I keep finding myself with a long list of elements, which I need to split into a list of lists of those elements. For example, I need to go from [0,1,2,3,4,5,6,7,8] to [[0,1,2],[3,4,5],[6,7,8]]
So far, the best function I have found to do this is
But that seems a little bit overcomplicated for such a simple function. Am I missing a better, more obvious solution?













