| author | wenzelm | 
| Sun, 05 Sep 2010 19:47:40 +0200 | |
| changeset 39133 | 70d3915c92f0 | 
| parent 35416 | d8d7d1b785af | 
| child 41589 | bbd861837ebc | 
| permissions | -rw-r--r-- | 
| 18408 | 1 | (* Title: Sudoku.thy | 
| 2 | ID: $Id$ | |
| 3 | Author: Tjark Weber | |
| 28949 | 4 | Copyright 2005-2008 | 
| 18408 | 5 | *) | 
| 6 | ||
| 7 | header {* A SAT-based Sudoku Solver *}
 | |
| 8 | ||
| 9 | theory Sudoku | |
| 10 | imports Main | |
| 11 | begin | |
| 12 | ||
| 13 | text {*
 | |
| 28949 | 14 | Please make sure you are using an efficient SAT solver (e.g., zChaff) | 
| 18408 | 15 | when loading this theory. See Isabelle's settings file for details. | 
| 16 | ||
| 28949 | 17 | Using zChaff, each Sudoku in this theory is solved in less than a | 
| 18 | second. | |
| 19 | ||
| 18408 | 20 | See the paper "A SAT-based Sudoku Solver" (Tjark Weber, published at | 
| 21 | LPAR'05) for further explanations. | |
| 22 | *} | |
| 23 | ||
| 24 | datatype digit = A ("1") | B ("2") | C ("3") | D ("4") | E ("5") | F ("6") | G ("7") | H ("8") | I ("9")
 | |
| 25 | ||
| 35416 
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
 haftmann parents: 
28949diff
changeset | 26 | definition valid :: "digit => digit => digit => digit => digit => digit => digit => digit => digit => bool" where | 
| 18408 | 27 | |
| 28 | "valid x1 x2 x3 x4 x5 x6 x7 x8 x9 == | |
| 29 | (x1 \<noteq> x2) \<and> (x1 \<noteq> x3) \<and> (x1 \<noteq> x4) \<and> (x1 \<noteq> x5) \<and> (x1 \<noteq> x6) \<and> (x1 \<noteq> x7) \<and> (x1 \<noteq> x8) \<and> (x1 \<noteq> x9) | |
| 30 | \<and> (x2 \<noteq> x3) \<and> (x2 \<noteq> x4) \<and> (x2 \<noteq> x5) \<and> (x2 \<noteq> x6) \<and> (x2 \<noteq> x7) \<and> (x2 \<noteq> x8) \<and> (x2 \<noteq> x9) | |
| 31 | \<and> (x3 \<noteq> x4) \<and> (x3 \<noteq> x5) \<and> (x3 \<noteq> x6) \<and> (x3 \<noteq> x7) \<and> (x3 \<noteq> x8) \<and> (x3 \<noteq> x9) | |
| 32 | \<and> (x4 \<noteq> x5) \<and> (x4 \<noteq> x6) \<and> (x4 \<noteq> x7) \<and> (x4 \<noteq> x8) \<and> (x4 \<noteq> x9) | |
| 33 | \<and> (x5 \<noteq> x6) \<and> (x5 \<noteq> x7) \<and> (x5 \<noteq> x8) \<and> (x5 \<noteq> x9) | |
| 34 | \<and> (x6 \<noteq> x7) \<and> (x6 \<noteq> x8) \<and> (x6 \<noteq> x9) | |
| 35 | \<and> (x7 \<noteq> x8) \<and> (x7 \<noteq> x9) | |
| 36 | \<and> (x8 \<noteq> x9)" | |
| 37 | ||
| 35416 
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
 haftmann parents: 
28949diff
changeset | 38 | definition sudoku :: "digit => digit => digit => digit => digit => digit => digit => digit => digit => | 
| 18408 | 39 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | 
| 40 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 41 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 42 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 43 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 44 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 45 | digit => digit => digit => digit => digit => digit => digit => digit => digit => | |
| 35416 
d8d7d1b785af
replaced a couple of constsdefs by definitions (also some old primrecs by modern ones)
 haftmann parents: 
28949diff
changeset | 46 | digit => digit => digit => digit => digit => digit => digit => digit => digit => bool" where | 
| 18408 | 47 | |
| 48 | "sudoku x11 x12 x13 x14 x15 x16 x17 x18 x19 | |
| 49 | x21 x22 x23 x24 x25 x26 x27 x28 x29 | |
| 50 | x31 x32 x33 x34 x35 x36 x37 x38 x39 | |
| 51 | x41 x42 x43 x44 x45 x46 x47 x48 x49 | |
| 52 | x51 x52 x53 x54 x55 x56 x57 x58 x59 | |
| 53 | x61 x62 x63 x64 x65 x66 x67 x68 x69 | |
| 54 | x71 x72 x73 x74 x75 x76 x77 x78 x79 | |
| 55 | x81 x82 x83 x84 x85 x86 x87 x88 x89 | |
| 56 | x91 x92 x93 x94 x95 x96 x97 x98 x99 == | |
| 57 | ||
| 58 | valid x11 x12 x13 x14 x15 x16 x17 x18 x19 | |
| 59 | \<and> valid x21 x22 x23 x24 x25 x26 x27 x28 x29 | |
| 60 | \<and> valid x31 x32 x33 x34 x35 x36 x37 x38 x39 | |
| 61 | \<and> valid x41 x42 x43 x44 x45 x46 x47 x48 x49 | |
| 62 | \<and> valid x51 x52 x53 x54 x55 x56 x57 x58 x59 | |
| 63 | \<and> valid x61 x62 x63 x64 x65 x66 x67 x68 x69 | |
| 64 | \<and> valid x71 x72 x73 x74 x75 x76 x77 x78 x79 | |
| 65 | \<and> valid x81 x82 x83 x84 x85 x86 x87 x88 x89 | |
| 66 | \<and> valid x91 x92 x93 x94 x95 x96 x97 x98 x99 | |
| 67 | ||
| 68 | \<and> valid x11 x21 x31 x41 x51 x61 x71 x81 x91 | |
| 69 | \<and> valid x12 x22 x32 x42 x52 x62 x72 x82 x92 | |
| 70 | \<and> valid x13 x23 x33 x43 x53 x63 x73 x83 x93 | |
| 71 | \<and> valid x14 x24 x34 x44 x54 x64 x74 x84 x94 | |
| 72 | \<and> valid x15 x25 x35 x45 x55 x65 x75 x85 x95 | |
| 73 | \<and> valid x16 x26 x36 x46 x56 x66 x76 x86 x96 | |
| 74 | \<and> valid x17 x27 x37 x47 x57 x67 x77 x87 x97 | |
| 75 | \<and> valid x18 x28 x38 x48 x58 x68 x78 x88 x98 | |
| 76 | \<and> valid x19 x29 x39 x49 x59 x69 x79 x89 x99 | |
| 77 | ||
| 78 | \<and> valid x11 x12 x13 x21 x22 x23 x31 x32 x33 | |
| 79 | \<and> valid x14 x15 x16 x24 x25 x26 x34 x35 x36 | |
| 80 | \<and> valid x17 x18 x19 x27 x28 x29 x37 x38 x39 | |
| 81 | \<and> valid x41 x42 x43 x51 x52 x53 x61 x62 x63 | |
| 82 | \<and> valid x44 x45 x46 x54 x55 x56 x64 x65 x66 | |
| 83 | \<and> valid x47 x48 x49 x57 x58 x59 x67 x68 x69 | |
| 84 | \<and> valid x71 x72 x73 x81 x82 x83 x91 x92 x93 | |
| 85 | \<and> valid x74 x75 x76 x84 x85 x86 x94 x95 x96 | |
| 86 | \<and> valid x77 x78 x79 x87 x88 x89 x97 x98 x99" | |
| 87 | ||
| 88 | text {*
 | |
| 89 | Just an arbitrary Sudoku grid: | |
| 90 | *} | |
| 91 | ||
| 92 | theorem "\<not> sudoku | |
| 93 | x11 x12 x13 x14 x15 x16 x17 x18 x19 | |
| 94 | x21 x22 x23 x24 x25 x26 x27 x28 x29 | |
| 95 | x31 x32 x33 x34 x35 x36 x37 x38 x39 | |
| 96 | x41 x42 x43 x44 x45 x46 x47 x48 x49 | |
| 97 | x51 x52 x53 x54 x55 x56 x57 x58 x59 | |
| 98 | x61 x62 x63 x64 x65 x66 x67 x68 x69 | |
| 99 | x71 x72 x73 x74 x75 x76 x77 x78 x79 | |
| 100 | x81 x82 x83 x84 x85 x86 x87 x88 x89 | |
| 101 | x91 x92 x93 x94 x95 x96 x97 x98 x99" | |
| 102 | refute | |
| 103 | oops | |
| 104 | ||
| 105 | text {*
 | |
| 106 | An "easy" Sudoku: | |
| 107 | *} | |
| 108 | ||
| 109 | theorem "\<not> sudoku | |
| 110 | 5 3 x13 x14 7 x16 x17 x18 x19 | |
| 111 | 6 x22 x23 1 9 5 x27 x28 x29 | |
| 112 | x31 9 8 x34 x35 x36 x37 6 x39 | |
| 113 | 8 x42 x43 x44 6 x46 x47 x48 3 | |
| 114 | 4 x52 x53 8 x55 3 x57 x58 1 | |
| 115 | 7 x62 x63 x64 2 x66 x67 x68 6 | |
| 116 | x71 6 x73 x74 x75 x76 2 8 x79 | |
| 117 | x81 x82 x83 4 1 9 x87 x88 5 | |
| 118 | x91 x92 x93 x94 8 x96 x97 7 9 " | |
| 119 | refute | |
| 120 | oops | |
| 121 | ||
| 122 | text {*
 | |
| 123 | A "hard" Sudoku: | |
| 124 | *} | |
| 125 | ||
| 126 | theorem "\<not> sudoku | |
| 127 | x11 2 x13 x14 x15 x16 x17 x18 x19 | |
| 128 | x21 x22 x23 6 x25 x26 x27 x28 3 | |
| 129 | x31 7 4 x34 8 x36 x37 x38 x39 | |
| 130 | x41 x42 x43 x44 x45 3 x47 x48 2 | |
| 131 | x51 8 x53 x54 4 x56 x57 1 x59 | |
| 132 | 6 x62 x63 5 x65 x66 x67 x68 x69 | |
| 133 | x71 x72 x73 x74 1 x76 7 8 x79 | |
| 134 | 5 x82 x83 x84 x85 9 x87 x88 x89 | |
| 135 | x91 x92 x93 x94 x95 x96 x97 4 x99" | |
| 136 | refute | |
| 137 | oops | |
| 138 | ||
| 28949 | 139 | text {*
 | 
| 140 | Some "exceptionally difficult" Sudokus, taken from | |
| 141 |   \url{http://en.wikipedia.org/w/index.php?title=Algorithmics_of_sudoku&oldid=254685903}
 | |
| 142 | (accessed December 2, 2008). | |
| 143 | *} | |
| 144 | ||
| 145 | text {*
 | |
| 146 | \begin{verbatim}
 | |
| 147 | Rating Program: gsf's sudoku q1 (rating) | |
| 148 | Rating: 99408 | |
| 149 | Poster: JPF | |
| 150 | Label: Easter Monster | |
| 151 | 1.......2.9.4...5...6...7...5.9.3.......7.......85..4.7.....6...3...9.8...2.....1 | |
| 152 | 1 . . | . . . | . . 2 | |
| 153 | . 9 . | 4 . . | . 5 . | |
| 154 | . . 6 | . . . | 7 . . | |
| 155 | ------+-------+------ | |
| 156 | . 5 . | 9 . 3 | . . . | |
| 157 | . . . | . 7 . | . . . | |
| 158 | . . . | 8 5 . | . 4 . | |
| 159 | ------+-------+------ | |
| 160 | 7 . . | . . . | 6 . . | |
| 161 | . 3 . | . . 9 | . 8 . | |
| 162 | . . 2 | . . . | . . 1 | |
| 163 | \end{verbatim}
 | |
| 164 | *} | |
| 165 | ||
| 166 | theorem "\<not> sudoku | |
| 167 | 1 x12 x13 x14 x15 x16 x17 x18 2 | |
| 168 | x21 9 x23 4 x25 x26 x27 5 x29 | |
| 169 | x31 x32 6 x34 x35 x36 7 x38 x39 | |
| 170 | x41 5 x43 9 x45 3 x47 x48 x49 | |
| 171 | x51 x52 x53 x54 7 x56 x57 x58 x59 | |
| 172 | x61 x62 x63 8 5 x66 x67 4 x69 | |
| 173 | 7 x72 x73 x74 x75 x76 6 x78 x79 | |
| 174 | x81 3 x83 x84 x85 9 x87 8 x89 | |
| 175 | x91 x92 2 x94 x95 x96 x97 x98 1 " | |
| 176 | refute | |
| 177 | oops | |
| 178 | ||
| 179 | text {*
 | |
| 180 | \begin{verbatim}
 | |
| 181 | Rating Program: gsf's sudoku q1 (Processing time) | |
| 182 | Rating: 4m19s@2 GHz | |
| 183 | Poster: tarek | |
| 184 | Label: tarek071223170000-052 | |
| 185 | ..1..4.......6.3.5...9.....8.....7.3.......285...7.6..3...8...6..92......4...1... | |
| 186 | . . 1 | . . 4 | . . . | |
| 187 | . . . | . 6 . | 3 . 5 | |
| 188 | . . . | 9 . . | . . . | |
| 189 | ------+-------+------ | |
| 190 | 8 . . | . . . | 7 . 3 | |
| 191 | . . . | . . . | . 2 8 | |
| 192 | 5 . . | . 7 . | 6 . . | |
| 193 | ------+-------+------ | |
| 194 | 3 . . | . 8 . | . . 6 | |
| 195 | . . 9 | 2 . . | . . . | |
| 196 | . 4 . | . . 1 | . . . | |
| 197 | \end{verbatim}
 | |
| 198 | *} | |
| 199 | ||
| 200 | theorem "\<not> sudoku | |
| 201 | x11 x12 1 x14 x15 4 x17 x18 x19 | |
| 202 | x21 x22 x23 x24 6 x26 3 x28 5 | |
| 203 | x31 x32 x33 9 x35 x36 x37 x38 x39 | |
| 204 | 8 x42 x43 x44 x45 x46 7 x48 3 | |
| 205 | x51 x52 x53 x54 x55 x56 x57 2 8 | |
| 206 | 5 x62 x63 x64 7 x66 6 x68 x69 | |
| 207 | 3 x72 x73 x74 8 x76 x77 x78 6 | |
| 208 | x81 x82 9 2 x85 x86 x87 x88 x89 | |
| 209 | x91 4 x93 x94 x95 1 x97 x98 x99" | |
| 210 | refute | |
| 211 | oops | |
| 212 | ||
| 213 | text {*
 | |
| 214 | \begin{verbatim}
 | |
| 215 | Rating Program: Nicolas Juillerat's Sudoku explainer 1.2.1 | |
| 216 | Rating: 11.9 | |
| 217 | Poster: tarek | |
| 218 | Label: golden nugget | |
| 219 | .......39.....1..5..3.5.8....8.9...6.7...2...1..4.......9.8..5..2....6..4..7..... | |
| 220 | . . . | . . . | . 3 9 | |
| 221 | . . . | . . 1 | . . 5 | |
| 222 | . . 3 | . 5 . | 8 . . | |
| 223 | ------+-------+------ | |
| 224 | . . 8 | . 9 . | . . 6 | |
| 225 | . 7 . | . . 2 | . . . | |
| 226 | 1 . . | 4 . . | . . . | |
| 227 | ------+-------+------ | |
| 228 | . . 9 | . 8 . | . 5 . | |
| 229 | . 2 . | . . . | 6 . . | |
| 230 | 4 . . | 7 . . | . . . | |
| 231 | \end{verbatim}
 | |
| 232 | *} | |
| 233 | ||
| 234 | theorem "\<not> sudoku | |
| 235 | x11 x12 x13 x14 x15 x16 x17 3 9 | |
| 236 | x21 x22 x23 x24 x25 1 x27 x28 5 | |
| 237 | x31 x32 3 x34 5 x36 8 x38 x39 | |
| 238 | x41 x42 8 x44 9 x46 x47 x48 6 | |
| 239 | x51 7 x53 x54 x55 2 x57 x58 x59 | |
| 240 | 1 x62 x63 4 x65 x66 x67 x68 x69 | |
| 241 | x71 x72 9 x74 8 x76 x77 5 x79 | |
| 242 | x81 2 x83 x84 x85 x86 6 x88 x89 | |
| 243 | 4 x92 x93 7 x95 x96 x97 x98 x99" | |
| 244 | refute | |
| 245 | oops | |
| 246 | ||
| 247 | text {*
 | |
| 248 | \begin{verbatim}
 | |
| 249 | Rating Program: dukuso's suexrat9 | |
| 250 | Rating: 4483 | |
| 251 | Poster: coloin | |
| 252 | Label: col-02-08-071 | |
| 253 | .2.4.37.........32........4.4.2...7.8...5.........1...5.....9...3.9....7..1..86.. | |
| 254 | . 2 . | 4 . 3 | 7 . . | |
| 255 | . . . | . . . | . 3 2 | |
| 256 | . . . | . . . | . . 4 | |
| 257 | ------+-------+------ | |
| 258 | . 4 . | 2 . . | . 7 . | |
| 259 | 8 . . | . 5 . | . . . | |
| 260 | . . . | . . 1 | . . . | |
| 261 | ------+-------+------ | |
| 262 | 5 . . | . . . | 9 . . | |
| 263 | . 3 . | 9 . . | . . 7 | |
| 264 | . . 1 | . . 8 | 6 . . | |
| 265 | \end{verbatim}
 | |
| 266 | *} | |
| 267 | ||
| 268 | theorem "\<not> sudoku | |
| 269 | x11 2 x13 4 x15 3 7 x18 x19 | |
| 270 | x21 x22 x23 x24 x25 x26 x27 3 2 | |
| 271 | x31 x32 x33 x34 x35 x36 x37 x38 4 | |
| 272 | x41 4 x43 2 x45 x46 x47 7 x49 | |
| 273 | 8 x52 x53 x54 5 x56 x57 x58 x59 | |
| 274 | x61 x62 x63 x64 x65 1 x67 x68 x69 | |
| 275 | 5 x72 x73 x74 x75 x76 9 x78 x79 | |
| 276 | x81 3 x83 9 x85 x86 x87 x88 7 | |
| 277 | x91 x92 1 x94 x95 8 6 x98 x99" | |
| 278 | refute | |
| 279 | oops | |
| 280 | ||
| 281 | text {*
 | |
| 282 | \begin{verbatim}
 | |
| 283 | Rating Program: dukuso's suexratt (10000 2 option) | |
| 284 | Rating: 2141 | |
| 285 | Poster: tarek | |
| 286 | Label: golden nugget | |
| 287 | .......39.....1..5..3.5.8....8.9...6.7...2...1..4.......9.8..5..2....6..4..7..... | |
| 288 | . . . | . . . | . 3 9 | |
| 289 | . . . | . . 1 | . . 5 | |
| 290 | . . 3 | . 5 . | 8 . . | |
| 291 | ------+-------+------ | |
| 292 | . . 8 | . 9 . | . . 6 | |
| 293 | . 7 . | . . 2 | . . . | |
| 294 | 1 . . | 4 . . | . . . | |
| 295 | ------+-------+------ | |
| 296 | . . 9 | . 8 . | . 5 . | |
| 297 | . 2 . | . . . | 6 . . | |
| 298 | 4 . . | 7 . . | . . . | |
| 299 | \end{verbatim}
 | |
| 300 | *} | |
| 301 | ||
| 302 | theorem "\<not> sudoku | |
| 303 | x11 x12 x13 x14 x15 x16 x17 3 9 | |
| 304 | x21 x22 x23 x24 x25 1 x27 x28 5 | |
| 305 | x31 x32 3 x34 5 x36 8 x38 x39 | |
| 306 | x41 x42 8 x44 9 x46 x47 x48 6 | |
| 307 | x51 7 x53 x54 x55 2 x57 x58 x59 | |
| 308 | 1 x62 x63 4 x65 x66 x67 x68 x69 | |
| 309 | x71 x72 9 x74 8 x76 x77 5 x79 | |
| 310 | x81 2 x83 x84 x85 x86 6 x88 x89 | |
| 311 | 4 x92 x93 7 x95 x96 x97 x98 x99" | |
| 312 | refute | |
| 313 | oops | |
| 314 | ||
| 18408 | 315 | end |