Solving Logic Grid Puzzles in Haskell
November 6, 2009 at 2:48 pm | In coding, haskell | 2 CommentsTags: coding, 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.
> import Data.Maybe
> import Control.Monad
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.
> type Entry = [String]
> type Answer = [Entry]
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.
> rules =
> [ 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 [ "", "England", "", "", "Red", "" ]
> , 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 [ "", "", "", "Water", "", "" ]
> , 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
> data Rule = Literal Entry
> | Adjacent Entry Entry
> | Follows Entry Entry
> deriving (Show)
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 :: Int -> Rule -> [Answer]
> 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 -> [Rule] -> [Answer]
> 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 :: Answer -> Answer -> Maybe Answer
> 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.
> main = showAnswers . applyRules emptyAnswer $ rules
> where emptyAnswer = replicate 5 . replicate 6 $ ""
> showAnswers = mapM_ $ mapM_ print
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
October 14, 2009 at 9:16 pm | In haskell | 15 CommentsTags: anythingyoucandohaskellcandobetter, brians brain, haskell
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 Data.Array -- Used to store the world state for processing > 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:
> data Cell = Off | Dying | On deriving (Eq, Enum)
For convenience, let’s define some constants:
> worldX = 90 -- The horizontal size of the world > 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 (On, _) = Dying -- Live cells always start to die > 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.
> getPeers world (x,y) = (world ! (x,y), length . filter (== On) $ neighbors) > 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:
> indexArray x y = listArray ((1,1),(x,y)) [(a,b) | a <- [1..x], b <- [1..y]] > 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:
> main = do rng <- newStdGen > 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:
> drawWorld s w = do sequence [draw x y | x <- [1..worldX], y <- [1..worldY]] > 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.
ghc -O3 -threaded --make BriansBrain.hs ./BriansBrain +RTS -N2
And then just sit back and watch the mesmerising patterns.
Haskell While Loop
September 15, 2009 at 9:48 pm | In haskell | 2 CommentsIn 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:
-- | For some reason Control.Monad doesn't provide a 'while'
-- function, even though it has a 'forever' function.
while :: Monad m => m Bool -> m a -> m ()
while predicate action = do
b <- predicate
if b then action >> while predicate action
else return ()
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
August 24, 2009 at 11:45 am | In haskell | 6 CommentsTags: fmap, haskell, prelude
So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions, fmap is equivalent to the composition operation. I think fmap 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 "map succ $ [0..9]" with "succ . [0..9]", and replacing "getLine >>= return . (+1) . read" with "(+1) . read . getLine"
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 import Prelude.MagicDot and not have to also bother with adding import Prelude hiding ((.))
Useful Shell Trick
August 6, 2009 at 10:40 pm | In Uncategorized | Leave a CommentSometimes, when doing a bunch of data munging in the shell, I want to pass the contents of a file through a shell pipeline, and then directly out into the file again. Unfortunately, doing this the naive way will obliterate the file’s contents.
So a while back, I saw a neat little utility called “sponge” which simply buffered data until it ended, and then output it. While useful, this wasn’t a standard Unix tool, so I couldn’t very well rely on having it available.
Well today, after literally minutes of searching, I have found a command that seems to work just as well:
tail -n +0
It outputs all lines in the input, beginning with line zero. Due, I suppose, to the way tail is implemented, it buffers all input before outputting anything.
Not exactly groundbreaking or anything, but a useful little trick to know when playing around in the shell.
Git Short URLs
July 20, 2009 at 2:27 pm | In Note To Self | Leave a CommentI knew for a fact this feature existed, but even so it took me a half hour to track down the syntax for it. To prevent myself ever needing to do that again, I’m putting it here.
To create a nice, short URL abbreviation for git, such as “myserver:<project>.git”, put the following in ‘~/.gitconfig’
[url "ssh://MY-SERVER/~/"]
insteadOf = myserver:
That’s all there is to it.
Haskell Exceptions
July 18, 2009 at 6:36 pm | In haskell | Leave a CommentTags: error handling, haskell
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’:
defaultOnError :: a -> IO a -> IO a
defaultOnError d a = do tryValue <- try a
case tryValue of
Left _ -> return d
Right v -> return v
errorToMaybe :: IO a -> IO (Maybe a)
errorToMaybe a = do tryValue <- try a
case tryValue of
Left _ -> return $ Nothing
Right v -> return $ Just v
What the Hell, XDG?
July 18, 2009 at 4:36 pm | In Uncategorized | Leave a CommentTake a look at the XDG Base Directory Specification. It looks good, doesn’t it? It really makes sense to have some “official” directories to store this kind of stuff in. Now look at the default locations listed:
$XDG_CONFIG_HOME – Looks like they nailed that pretty well. I might rather see some user-editable config files placed directly in ‘~/.app/’, but ‘~/.config/app’ is alright as a second choice.
$XDG_CACHE_HOME – Dead on. It is incredibly nice to have a single home for all those nasty little cache files that are a little too persistent to place in ‘/tmp’.
$XDG_DATA_HOME – Okay, it’s a little bit ugly to store extra data in ‘~/.local/share’, why couldn’t they just put it in ‘~/.local’? But on the other hand, most applications don’t even need extra data of that sort anyway, and it will rarely, if ever, be edited by a human, so it’s at least a little bit acceptable.
$XDG_DATA_DIRS – Yeah, they’re back on form for this one. Those two default directories cover basically every app in existence, so not a single fault can be found there.
$XDG_CONFIG_DIRS – WHAT THE FUCK! Why the hell do they decide every application needs to store their system-wide configuration in a special ‘/etc/xdg’ directory? On my system, I count a grand total of three applications in there, of which I use one and have configured zero. For comparison, I count 157 files and directories in ‘/etc’, other than ‘/etc/xdg’. Obviously, people will kind of expect to see their system-wide config files placed directly in ‘/etc’
What reason can there be for that kind of annoyance? When I have to go in and manually edit a system-wide config file, I will look for it in ‘/etc’. It would never even have occurred to me to look in ‘/etc/xdg’. At the very least, couldn’t the default search path have been ‘/etc/xdg’, followed by ‘/etc’?
This kind of trivial little issue weakens the entire specification, because such a seemingly inconsequential little rough edge can completely sour people’s opinions of the whole document. And it would be a shame if that happened, because I would rather see the majority of these things become common convention.
Useful Haskell List Function
July 12, 2009 at 3:09 pm | In haskell | Leave a CommentI 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
split :: Int -> [a] -> [[a]]
split n = unfoldr (\r -> case r of [] -> Nothing; x -> Just $ splitAt n x)
But that seems a little bit overcomplicated for such a simple function. Am I missing a better, more obvious solution?
Escaping Strings in a Shell Pipeline
June 25, 2009 at 5:15 pm | In Uncategorized | Leave a CommentTags: bash, sed, shell
A useful little sed script I wrote, which turned out to be much more useful than I expected. It simply takes a list of files, one per line, and outputs that same list, but with all the spaces and special characters escaped. It’s mainly intended for use right between find and xargs, as in “find . -name ‘asd’ -print | shell-escape | xargs -I % echo %’
The list of characters is taken from IEEE Std 1003.1
#!/bin/sed -f # # Shell-Escape # A very basic script to escape characters in the shell. # Mainly intended for use in shell pipelines, right between # 'find' and 'xargs' s@\\@\\\\@g s@ @\\ @g s@'@\\'@g s@"@\\"@g s@(@\\(@g s@)@\\)@g s@|@\\|@g s@&@\\&@g s@;@\\;@g s@<@\\<@g s@>@\\>@g s@`@\\`@g
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.
