Introduction to Functional Programming – Lab 3: "Sudoku" | TDA555 / DIT440, LP1 2014 |
Home | Schedule | Labs | Exercises | Exam | About | FAQ | Fire | Forum | TimeEdit | Links |
Introduction to Functional Programming – Lab 3: "Sudoku" | TDA555 / DIT440, LP1 2014 |
Home | Schedule | Labs | Exercises | Exam | About | FAQ | Fire | Forum | TimeEdit | Links |
In this Lab Assignment, you will design a Haskell program that will be able to solve Sudokus, a popular logical puzzle originating from Japan.
Assignments A, B, and C form Part I of the lab, and need to be submitted before Monday October 6 at 9.00 in the morning.
Assignments D, E and F form Part II of the Lab, and need to be submitted before Monday October 13 at 9.00 in the morning.
There are also extra assignments X, Y, Z, P, and Q. You can choose freely whether to do one of these.
The final deadline for this lab is Wednesday October 22 at 9.00 in the morning.
A Sudoku puzzle consists of a 9x9 grid. Some of the cells in the grid have digits (from 1 to 9), others are blank. The objective of the puzzle is to fill in the blank cells with digits from 1 to 9, in such a way that every row, every column and every 3x3 block has exactly one occurrence of each digit 1 to 9.
Here is an example of a Sudoku puzzle:
And here is the solution:
In this lab assignment, you will write a Haskell program that can read in a Sudoku puzzle and solve it.
If you want to read more about Sudokus, here are a few links:
Summing up, a natural way to represent Sudokus is using the following Haskell datatype:
data Sudoku = Sudoku [[Maybe Int]]Since it is convenient to have a function that extracts the actual rows from the Sudoku, we say:
rows :: Sudoku -> [[Maybe Int]] rows (Sudoku rs) = rsFor example, the above Sudoku puzzle has the following representation in Haskell:
example :: Sudoku example = Sudoku [ [Just 3, Just 6, Nothing,Nothing,Just 7, Just 1, Just 2, Nothing,Nothing] , [Nothing,Just 5, Nothing,Nothing,Nothing,Nothing,Just 1, Just 8, Nothing] , [Nothing,Nothing,Just 9, Just 2, Nothing,Just 4, Just 7, Nothing,Nothing] , [Nothing,Nothing,Nothing,Nothing,Just 1, Just 3, Nothing,Just 2, Just 8] , [Just 4, Nothing,Nothing,Just 5, Nothing,Just 2, Nothing,Nothing,Just 9] , [Just 2, Just 7, Nothing,Just 4, Just 6, Nothing,Nothing,Nothing,Nothing] , [Nothing,Nothing,Just 5, Just 3, Nothing,Just 8, Just 9, Nothing,Nothing] , [Nothing,Just 8, Just 3, Nothing,Nothing,Nothing,Nothing,Just 6, Nothing] , [Nothing,Nothing,Just 7, Just 6, Just 9, Nothing,Nothing,Just 4, Just 3] ]Now, a number of assignments follows, which will lead you step-by-step towards an implementation of a Sudoku-solver.
A1. Implement a function
allBlankSudoku :: Sudokuthat represents a Sudoku that only contains blank cells (this means that no digits are present). Do not use copy-and-paste programming here! Your definition does not need to be longer than a few short lines. |
A2. The Sudoku type we have defined allows for more things than Sudokus.
For example, there is nothing in the type definition that says that a
Sudoku has 9 rows and 9 columns,
or that digits need to lie between 1 and 9. Implement a function
isSudoku :: Sudoku -> Boolthat checks if all such extra conditions are met by the given Sudoku. Examples: Sudoku> isSudoku (Sudoku []) False Sudoku> isSudoku allBlankSudoku True Sudoku> isSudoku example True Sudoku> isSudoku (Sudoku (tail (rows example))) False |
A3. Our job is to solve Sudokus. So, it would be handy to know
when a Sudoku is solved or not. We say that a Sudoku is solved if there
are no blank cells left to be filled in anymore. Implement the following
function:
isSolved :: Sudoku -> BoolNote that we do not check here if the Sudoku is a valid solution; we will do this later. This means that any Sudoku without blanks (even Sudokus with the same digit appearing twice in a row) is considered solved by this function! |
To implement the above, use list comprehensions! Also, the following standard Haskell functions might come in handy:
replicate :: Int -> a -> [a] length :: [a] -> Int and :: [Bool] -> BoolTo help you get started, ahere is a file that you can use:
The following is an example text-representation that we will use in this assignment. It actually represents the example above.
36..712.. .5....18. ..92.47.. ....13.28 4..5.2..9 27.46.... ..53.89.. .83....6. ..769..43There are 9 lines of text in this representation, each corresponding to a row. Each line contains 9 characters. A digit 1 -- 9 represents a filled cell, and a period (.) represents a blank cell.
B1. Implement a function:
printSudoku :: Sudoku -> IO ()that, given a Sudoku, creates instructions to print the Sudoku on the screen, using the format shown above. Example: Sudoku> printSudoku allBlankSudoku ......... ......... ......... ......... ......... ......... ......... ......... ......... Sudoku> printSudoku example 36..712.. .5....18. ..92.47.. ....13.28 4..5.2..9 27.46.... ..53.89.. .83....6. ..769..43 |
B2. Implement a function:
readSudoku :: FilePath -> IO Sudokuthat, given a filename, creates instructions that read the Sudoku from the file, and deliver it as the result of the instructions. You may decide yourself what to do when the file does not contain a representation of a Sudoku. Examples: Sudoku> sud <- readSudoku "example.sud" Sudoku> printSudoku sud 36..712.. .5....18. ..92.47.. ....13.28 4..5.2..9 27.46.... ..53.89.. .83....6. ..769..43 Sudoku> readSudoku "Sudoku.hs" Exception: Not a Sudoku!(Note: In the above example, we make use of the fact that commands in GHCi are part of an implicit do block. This allows us to bind results of IO instructions, just like in do notation: sud <- readSudoku ... )
|
To implement the above, you will need to be able to convert between characters (type Char) and digits/integers (type Int). The standard functions chr and ord (import the module Data.Char) will come in handy here. Think about the following problems:
The constant value ord '0' will play a central role in all this. You can also use the function digitToInt.Here are some functions that might come in handy:
chr :: Int -> Char ord :: Char -> Int digitToInt :: Char -> Int putStr :: String -> IO () putStrLn :: String -> IO () sequence_ :: [IO a] -> IO () readFile :: FilePath -> IO String lines :: String -> [String]Here are some example Sudoku-files that you can download and use:
Let us split this problem into a number of smaller problems. First, we need to know how to generate arbitrary cell values (of type Maybe Int). Then, we need to know how to generate 81 such cells, and compose them all into a Sudoku.
C1. Implement a function:
cell :: Gen (Maybe Int)that, contains instructions for generating a Sudoku cell. You have to think about the following: Example: Sudoku> sample cell Just 3 Nothing Nothing Just 7 Nothing |
C2. Make Sudokus an instance of the class Arbitrary.
instance Arbitrary Sudoku where ...We have already done this for you in the file Sudoku.hs. |
C3. Define a property
prop_Sudoku :: Sudoku -> Boolthat expresses that each generated Sudoku actually is a Sudoku according to Assignment A2. Also use QuickCheck to check that the property actually holds for all Sudokus that are generated. |
sample :: Gen a -> IO () choose :: Random a => (a,a) -> Gen a frequency :: [(Int,Gen a)] -> Gen a sequence :: [Gen a] -> Gen [a]You might want to take a look at the lecture notes and example code on test data generation.
type Block = [Maybe Int]We are going to define a function that checks if a Sudoku is not violating any of the above constraints, by checking that none of the blocks violate those constraints.
D1. Implement a function:
isOkayBlock :: Block -> Boolthat, given a block, checks if that block does not contain the same digit twice. Examples: Sudoku> isOkayBlock [Just 1, Just 7, Nothing, Nothing, Just 3, Nothing, Nothing, Nothing, Just 2] True Sudoku> isOkayBlock [Just 1, Just 7, Nothing, Just 7, Just 3, Nothing, Nothing, Nothing, Just 2] False |
D2. Implement a function:
blocks :: Sudoku -> [Block]that, given a Sudoku, creates a list of all blocks of that Sudoku. This means: Also add a property that states that, for each Sudoku, there are 3*9 blocks, and each block has exactly 9 cells. |
D3. Now, implement a function:
isOkay :: Sudoku -> Boolthat, given a Soduko, checks that all rows, colums and 3x3 blocks do not contain the same digit twice. Examples: Sudoku> isOkay allBlankSudoku True Sudoku> do sud <- readSudoku "example.sud"; print (isOkay sud) True |
nub :: Eq a => [a] -> [a] transpose :: [[a]] -> [[a]] take :: Int -> [a] -> [a] drop :: Int -> [a] -> [a]Note that some of the above functions only appear when you import Data.List.
You might want to take a look at the exercises and answers on lists and list comprehensions.
Given a Sudoku, if there are no blanks left in the Sudoku, we are done. Otherwise, there is at least one blank cell that needs to be filled in somehow. We are going to write functions to find and manipulate such a blank cell.
It is quite natural to start to talk about positions. A position is a coordinate that identifies a cell in the Sudoku. Here is a way of modelling coordinates:
type Pos = (Int,Int)We use positions as indicating first the row and then the column. It is common in programming languages to start counting at 0! Therefore, the position that indicates the upper left corner is (0,0), and the position indicating the lower right corner is (8,8). And, for example, the position (3,5) denotes the 6th cell in the 4th row.
E1. Implement a function:
blank :: Sudoku -> Posthat, given a Sudoku that has not yet been solved, returns a position in the Sudoku that is still blank. If there are more than one blank position, you may decide yourself which one to return. Examples: Sudoku> blank allBlankSudoku (0,0) Sudoku> blank example (0,2)Also write a property that states that the cell at the blank position is actually blank. |
E2. Implement a function:
(!!=) :: [a] -> (Int,a) -> [a]that, given a list, and a tuple containing an index in the list and a new value, updates the given list with the new value at the given index. Examples: Sudoku> ["a","b","c","d"] !!= (1,"apa") ["a","apa","c","d"] Sudoku> ["p","qq","rrr"] !!= (0,"bepa") ["bepa","qq","rrr"]Also write (a) propert(y/ies) that state(s) the expected properties of this function. Think about what can go wrong! |
E3. Implement a function:
update :: Sudoku -> Pos -> Maybe Int -> Sudokuthat, given a Sudoku, a position, and a new cell value, updates the given Sudoku at the given position with the new value. Example: Sudoku> printSudoku (update allBlankSudoku (1,3) (Just 5)) ......... ...5..... ......... ......... ......... ......... ......... ......... .........Also write a property that checks that the updated position really has gotten the new value. |
There is a standard function (!!) in Haskell for getting a specific element from a list. It starts indexing at 0, so for example to get the first element from a list xs, you can use xs !! 0.
We usually use the standard function zip to pair up elements in a list with their corresponding index. Example:
*Main> ["apa","bepa","cepa"] `zip` [1..3] [("apa",1),("bepa",2),("cepa",3)]This, in combination with list comprehensions, should be very useful for this assignment!
When testing a property that is polymorphic (meaning that it has type variables in its type), you need to add a type signature that picks an arbitrary type. For example, when testing properties for the function (!!=), which works for lists of any type, you have to fix the type when testing, for example lists of Integers. Do this by adding a type signature to your properties.
Here are some more useful functions:
head :: [a] -> a (!!) :: [a] -> Int -> a zip :: [a] -> [b] -> [(a,b)]You might want to take a look at the exercises and answers on lists and list comprehensions.
Our objective is to define a Haskell function
solve :: Sudoku -> Maybe SudokuThe idea is as follows. If we have a Sudoku sud that we would like to solve, we give it to the function solve. This will produce one of two results: How should solve be implemented. Here is one idea. When we try to solve a given Sudoku sud, there exist three cases:
F1. Implement a function:
solve :: Sudoku -> Maybe Sudokuusing the above idea. Examples: Sudoku> printSudoku (fromJust (solve allBlankSudoku)) 123456789 456789123 789123456 214365897 365897214 897214365 531642978 642978531 978531642 Sudoku> do sud <- readSudoku "example.sud"; printSudoku (fromJust (solve sud)) 364871295 752936184 819254736 596713428 431582679 278469351 645328917 983147562 127695843 Sudoku> do sud <- readSudoku "impossible.sud"; print (solve sud) Nothing(In the above examples, we use the standard function fromJust from the library Data.Maybe.) |
F2. For your own convenience, define a function:
readAndSolve :: FilePath -> IO ()that produces instructions for reading the Sudoku from the given file, solving it, and printing the answer. Examples: Sudoku> readAndSolve "example.sud" 364871295 752936184 819254736 596713428 431582679 278469351 645328917 983147562 127695843 Sudoku> readAndSolve "impossible.sud" (no solution) |
F3. Implement a function:
isSolutionOf :: Sudoku -> Sudoku -> Boolthat checks, given two Sudokus, whether the first one is a solution (i.e. all blocks are okay, there are no blanks), and also whether the first one is a solution of the second one (i.e. all digits in the second sudoku are maintained in the first one). Examples: Sudoku> fromJust (solve allBlankSudoku) `isSolutionOf` allBlankSudoku True Sudoku> allBlankSudoku `isSolutionOf` allBlankSudoku False Sudoku> fromJust (solve allBlankSudoku) `isSolutionOf` example False |
F4. Define a property:
prop_SolveSound :: Sudoku -> Propertythat says that the function solve is sound. Soundness means that every supposed solution produced by solve actually is a valid solution of the original problem. |
All the work we did in the assignments A -- E should be used in order to implement the function solve.
To implement the third, recursive, case of solve, you can use a list comprehension that enumerates all possible values for the blank cell. You can then use the standard function listToMaybe to turn the result into something of type Maybe.
QuickChecking the property prop_SolveSound will probably take a long time. Be patient! Alternatively, there are a number of things you can do about this.
It is okay if you do not find a completely satisfactory solution to this.Here are some useful functions:
fromJust :: Maybe a -> a listToMaybe :: [a] -> Maybe aYou might want to take a look at the function majorities defined in the lecture and exercises.
Here is an example of an impossible Sudoku:
There are no perfect, pre-defined answers here, but try to show your thoughts and understanding in your answers.
X. The solving method we have used in this lab assignment is very basic, and in some sense
naive. One way to boost performance is to look closer at the function
blank. Perhaps if we picked the blank in a smarter way, the solve
function would go faster?
One idea is to always pick the blank spot where there are as few possibilities left. For example, if we have a row with one or two blank spots, it is probably a good idea to pick one of those blank spots, since it will limit the consecutive search most, and it will lead to search to a state with more digits filled in. (Such a way of changing a solving method is called a heuristic -- there is no absoluate guarantee that the search will go faster, but often it actually will.) Change the implementation of the blank function, so that it always picks the blank spot that is in a row, column, or 3x3 block where there are as few blank spots left. For example, in the Sudoku below: 36..712.. .5....18. ..92.47.. ...x13.28 4..5.2..9 27.46.... ..53.89.. .83....6. ..769..43we have marked one blank spot with an x. The row in which this x is has 5 blank spots (including the x itself); the column in which this x is has 4 blank spots, and the 3x3 block in which this x is has 3 blank spots. It turns out that this is the best we can do; it is good to pick x as the next blank spot, since there will only be 2 blank spots left in the middle 3x3 block. Does your solve function work faster now? Experiment with different heuristics (for example: only look at rows and columns, and not at 3x3 blocks), and see which one performs best. Can you solve some of the hard Sudokus now? Do not forget to add appropriate properties that test your functions. |
Y. The solving method we have used in this lab assignment is very basic, and in some sense
naive. The best known methods to solve problems like Sudoku is to also include the notion of
propagation. This is the way most humans actually solve a Sudoku.
A simple variant of propagation is the following. Suppose we have Sudoku with a row with precisely one blank, such as the 3rd row in the example below: 36..712.. .5....18. ..92.47.. 596.13428 4..5.2..9 27.46.... ..53.89.. .83....6. ..769..43Our current solution would go and pick blanks, and start searching recursively, without making use of the fact that we already know the value of that blank (namely 7 in this case); all the other values have been used by the other cells in the row. Implement a function propagate :: Sudoku -> Sudokuthat, given a Sudoku, finds out which rows, columns, and 3x3 blocks only have one blank in them, and then fills those blanks with the only possibly remaining value. It repeats doing this until all rows, columns and 3x3 blocks are either completely filled up, or contain two holes or more. Now, add this function at the appropriate place in your solve function. Does it work faster now? For other, more powerful propagation, you can for example read the following webpage: Or come up with your own propagation rules!Do not forget to add appropriate properties that test your functions. |
Z. Write a function that produces interesting Sudoku puzzles. For example, one could have a function
createSudoku :: IO ()that every time we run it, would print a new, interesting Sudoku puzzle on the screen. One can discuss what an interesting Sudoku puzzle is. Here are three properties that an interesting Sudoku puzzle must have: Can you think of a way to define a function for generating an infinite supply of new Sudokus satisfying the above two properties? You should of course make use of the functions you already have.Do not forget to add appropriate properties that test your functions. |
P. Generalize the Sudokus that are dealt with in this assignment to
other dimensions, for example 4x4 Sudokus, or 4x3 Sudokus. Do all dimensions
make sense? Make sure your solution works in general, for all possible dimensions that make sense.
For inspiration, look here: Monster Sudokus. Or here: xkcd comics :-) Do not forget to add appropriate properties that test your functions. |
Q. We have stated the soundness of the solve function as a property;
every produced solution should be a real solution.
The dual of soundness is completeness. Completeness says that whenever there is
a solution, the solve function should also produce a solution. (Equivalently, if the
solve function says that there is no solution, then there really is no solution.)
If we define the following helper datatype: data SolvableSudoku = Solvable SudokuThen, implement a property prop_SolveComplete :: SolvableSudoku -> Bool prop_SolveComplete (Solvable sud) = ...that states that, for any solvale Sudoku, solve produces an answer. Now, make the type SolvableSudoku an instance of Arbitrary: instance Arbitrary SolvableSudoku where arbitrary = ...Here, you need to think about how to generate arbitrary Sudokus that are guaranteed to be solvable! One idea is to start with an arbitrary solved Sudoku, and randomly blank out some of the digits. Implement this! |
Your submission should consist of the following files:
Before you submit your code, Clean It Up! Remember, submitting clean code is Really Important, and simply the polite thing to do. After you feel you are done, spend some time on cleaning your code; make it simpler, remove unneccessary things, etc. We will reject your solution if it is not clean. Clean code:
Good Luck!