author | skalberg |
Sun, 13 Feb 2005 17:15:14 +0100 | |
changeset 15531 | 08c8dad8e399 |
parent 15332 | 0dc05858a862 |
child 15570 | 8d8c70b41bab |
permissions | -rw-r--r-- |
14453 | 1 |
(* Title: HOL/Tools/sat_solver.ML |
2 |
ID: $Id$ |
|
3 |
Author: Tjark Weber |
|
4 |
Copyright 2004 |
|
5 |
||
6 |
Interface to external SAT solvers, and (simple) built-in SAT solvers. |
|
7 |
*) |
|
8 |
||
9 |
signature SAT_SOLVER = |
|
10 |
sig |
|
14965 | 11 |
exception NOT_CONFIGURED |
14453 | 12 |
|
14965 | 13 |
type assignment = int -> bool option |
14 |
datatype result = SATISFIABLE of assignment |
|
15 |
| UNSATISFIABLE |
|
16 |
| UNKNOWN |
|
17 |
type solver = PropLogic.prop_formula -> result |
|
18 |
||
19 |
(* auxiliary functions to create external SAT solvers *) |
|
14453 | 20 |
val write_dimacs_cnf_file : Path.T -> PropLogic.prop_formula -> unit |
21 |
val write_dimacs_sat_file : Path.T -> PropLogic.prop_formula -> unit |
|
14965 | 22 |
val parse_std_result_file : Path.T -> string * string * string -> result |
23 |
val make_external_solver : string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver |
|
14453 | 24 |
|
15040 | 25 |
val read_dimacs_cnf_file : Path.T -> PropLogic.prop_formula |
26 |
||
14453 | 27 |
(* generic interface *) |
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
28 |
val solvers : (string * solver) list ref |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
29 |
val add_solver : string * solver -> unit |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
30 |
val invoke_solver : string -> solver (* exception OPTION *) |
14453 | 31 |
end; |
32 |
||
33 |
structure SatSolver : SAT_SOLVER = |
|
34 |
struct |
|
35 |
||
36 |
open PropLogic; |
|
37 |
||
38 |
(* ------------------------------------------------------------------------- *) |
|
14965 | 39 |
(* should be raised by an external SAT solver to indicate that the solver is *) |
40 |
(* not configured properly *) |
|
41 |
(* ------------------------------------------------------------------------- *) |
|
42 |
||
43 |
exception NOT_CONFIGURED; |
|
44 |
||
45 |
(* ------------------------------------------------------------------------- *) |
|
15531 | 46 |
(* type of partial (satisfying) assignments: 'a i = NONE' means that 'a' is *) |
14965 | 47 |
(* a satisfying assigment regardless of the value of variable 'i' *) |
14453 | 48 |
(* ------------------------------------------------------------------------- *) |
49 |
||
14965 | 50 |
type assignment = int -> bool option; |
51 |
||
52 |
(* ------------------------------------------------------------------------- *) |
|
53 |
(* return type of SAT solvers: if the result is 'SATISFIABLE', a satisfying *) |
|
54 |
(* assignment must be returned as well *) |
|
55 |
(* ------------------------------------------------------------------------- *) |
|
56 |
||
57 |
datatype result = SATISFIABLE of assignment |
|
58 |
| UNSATISFIABLE |
|
59 |
| UNKNOWN; |
|
60 |
||
61 |
(* ------------------------------------------------------------------------- *) |
|
62 |
(* type of SAT solvers: given a propositional formula, a satisfying *) |
|
63 |
(* assignment may be returned *) |
|
64 |
(* ------------------------------------------------------------------------- *) |
|
65 |
||
66 |
type solver = prop_formula -> result; |
|
14453 | 67 |
|
68 |
(* ------------------------------------------------------------------------- *) |
|
69 |
(* write_dimacs_cnf_file: serializes a formula 'fm' of propositional logic *) |
|
70 |
(* to a file in DIMACS CNF format (see "Satisfiability Suggested *) |
|
71 |
(* Format", May 8 1993, Section 2.1) *) |
|
72 |
(* Note: 'fm' must not contain a variable index less than 1. *) |
|
14965 | 73 |
(* Note: 'fm' must be given in CNF. *) |
14453 | 74 |
(* ------------------------------------------------------------------------- *) |
75 |
||
76 |
(* Path.T -> prop_formula -> unit *) |
|
77 |
||
78 |
fun write_dimacs_cnf_file path fm = |
|
79 |
let |
|
80 |
(* prop_formula -> prop_formula *) |
|
81 |
fun cnf_True_False_elim True = |
|
82 |
Or (BoolVar 1, Not (BoolVar 1)) |
|
83 |
| cnf_True_False_elim False = |
|
84 |
And (BoolVar 1, Not (BoolVar 1)) |
|
85 |
| cnf_True_False_elim fm = |
|
86 |
fm (* since 'fm' is in CNF, either 'fm'='True'/'False', or 'fm' does not contain 'True'/'False' at all *) |
|
87 |
(* prop_formula -> int *) |
|
88 |
fun cnf_number_of_clauses (And (fm1,fm2)) = |
|
89 |
(cnf_number_of_clauses fm1) + (cnf_number_of_clauses fm2) |
|
90 |
| cnf_number_of_clauses _ = |
|
91 |
1 |
|
92 |
(* prop_formula -> string *) |
|
14999
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
93 |
fun cnf_string fm = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
94 |
let |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
95 |
(* prop_formula -> string list -> string list *) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
96 |
fun cnf_string_acc True acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
97 |
error "formula is not in CNF" |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
98 |
| cnf_string_acc False acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
99 |
error "formula is not in CNF" |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
100 |
| cnf_string_acc (BoolVar i) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
101 |
(assert (i>=1) "formula contains a variable index less than 1"; |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
102 |
string_of_int i :: acc) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
103 |
| cnf_string_acc (Not (BoolVar i)) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
104 |
"-" :: cnf_string_acc (BoolVar i) acc |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
105 |
| cnf_string_acc (Not _) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
106 |
error "formula is not in CNF" |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
107 |
| cnf_string_acc (Or (fm1,fm2)) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
108 |
cnf_string_acc fm1 (" " :: cnf_string_acc fm2 acc) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
109 |
| cnf_string_acc (And (fm1,fm2)) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
110 |
cnf_string_acc fm1 (" 0\n" :: cnf_string_acc fm2 acc) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
111 |
in |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
112 |
concat (cnf_string_acc fm []) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
113 |
end |
14453 | 114 |
in |
115 |
File.write path |
|
116 |
(let |
|
14965 | 117 |
val fm' = cnf_True_False_elim fm |
118 |
val number_of_vars = maxidx fm' |
|
119 |
val number_of_clauses = cnf_number_of_clauses fm' |
|
14453 | 120 |
in |
121 |
"c This file was generated by SatSolver.write_dimacs_cnf_file\n" ^ |
|
122 |
"c (c) Tjark Weber\n" ^ |
|
14965 | 123 |
"p cnf " ^ string_of_int number_of_vars ^ " " ^ string_of_int number_of_clauses ^ "\n" ^ |
14999
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
124 |
cnf_string fm' ^ " 0\n" |
14453 | 125 |
end) |
126 |
end; |
|
127 |
||
128 |
(* ------------------------------------------------------------------------- *) |
|
129 |
(* write_dimacs_sat_file: serializes a formula 'fm' of propositional logic *) |
|
130 |
(* to a file in DIMACS SAT format (see "Satisfiability Suggested *) |
|
131 |
(* Format", May 8 1993, Section 2.2) *) |
|
132 |
(* Note: 'fm' must not contain a variable index less than 1. *) |
|
133 |
(* ------------------------------------------------------------------------- *) |
|
134 |
||
135 |
(* Path.T -> prop_formula -> unit *) |
|
136 |
||
137 |
fun write_dimacs_sat_file path fm = |
|
138 |
let |
|
139 |
(* prop_formula -> string *) |
|
14999
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
140 |
fun sat_string fm = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
141 |
let |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
142 |
(* prop_formula -> string list -> string list *) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
143 |
fun sat_string_acc True acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
144 |
"*()" :: acc |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
145 |
| sat_string_acc False acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
146 |
"+()" :: acc |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
147 |
| sat_string_acc (BoolVar i) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
148 |
(assert (i>=1) "formula contains a variable index less than 1"; |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
149 |
string_of_int i :: acc) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
150 |
| sat_string_acc (Not fm) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
151 |
"-(" :: sat_string_acc fm (")" :: acc) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
152 |
| sat_string_acc (Or (fm1,fm2)) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
153 |
"+(" :: sat_string_acc fm1 (" " :: sat_string_acc fm2 (")" :: acc)) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
154 |
| sat_string_acc (And (fm1,fm2)) acc = |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
155 |
"*(" :: sat_string_acc fm1 (" " :: sat_string_acc fm2 (")" :: acc)) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
156 |
in |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
157 |
concat (sat_string_acc fm []) |
2c39efba8f67
faster conversion into DIMACS CNF and DIMACS SAT format
webertj
parents:
14965
diff
changeset
|
158 |
end |
14453 | 159 |
in |
160 |
File.write path |
|
161 |
(let |
|
162 |
val number_of_vars = Int.max (maxidx fm, 1) |
|
163 |
in |
|
164 |
"c This file was generated by SatSolver.write_dimacs_sat_file\n" ^ |
|
165 |
"c (c) Tjark Weber\n" ^ |
|
14965 | 166 |
"p sat " ^ string_of_int number_of_vars ^ "\n" ^ |
167 |
"(" ^ sat_string fm ^ ")\n" |
|
14453 | 168 |
end) |
169 |
end; |
|
170 |
||
171 |
(* ------------------------------------------------------------------------- *) |
|
172 |
(* parse_std_result_file: scans a SAT solver's output file for a satisfying *) |
|
14965 | 173 |
(* variable assignment. Returns the assignment, or 'UNSATISFIABLE' if *) |
174 |
(* the file contains 'unsatisfiable', or 'UNKNOWN' if the file contains *) |
|
175 |
(* neither 'satisfiable' nor 'unsatisfiable'. Empty lines are ignored. *) |
|
176 |
(* The assignment must be given in one or more lines immediately after *) |
|
177 |
(* the line that contains 'satisfiable'. These lines must begin with *) |
|
178 |
(* 'assignment_prefix'. Variables must be separated by " ". Non- *) |
|
179 |
(* integer strings are ignored. If variable i is contained in the *) |
|
180 |
(* assignment, then i is interpreted as 'true'. If ~i is contained in *) |
|
181 |
(* the assignment, then i is interpreted as 'false'. Otherwise the *) |
|
182 |
(* value of i is taken to be unspecified. *) |
|
14453 | 183 |
(* ------------------------------------------------------------------------- *) |
184 |
||
14965 | 185 |
(* Path.T -> string * string * string -> result *) |
14453 | 186 |
|
14965 | 187 |
fun parse_std_result_file path (satisfiable, assignment_prefix, unsatisfiable) = |
14453 | 188 |
let |
15531 | 189 |
(* 'a option -> 'a option *) |
14453 | 190 |
fun option (SOME a) = |
15531 | 191 |
SOME a |
14453 | 192 |
| option NONE = |
15531 | 193 |
NONE |
14453 | 194 |
(* string -> int list *) |
195 |
fun int_list_from_string s = |
|
196 |
mapfilter (option o Int.fromString) (space_explode " " s) |
|
14965 | 197 |
(* int list -> assignment *) |
14453 | 198 |
fun assignment_from_list [] i = |
15531 | 199 |
NONE (* the SAT solver didn't provide a value for this variable *) |
14453 | 200 |
| assignment_from_list (x::xs) i = |
15531 | 201 |
if x=i then (SOME true) |
202 |
else if x=(~i) then (SOME false) |
|
14453 | 203 |
else assignment_from_list xs i |
14965 | 204 |
(* int list -> string list -> assignment *) |
205 |
fun parse_assignment xs [] = |
|
206 |
assignment_from_list xs |
|
207 |
| parse_assignment xs (line::lines) = |
|
208 |
if String.isPrefix assignment_prefix line then |
|
209 |
parse_assignment (xs @ int_list_from_string line) lines |
|
210 |
else |
|
211 |
assignment_from_list xs |
|
14453 | 212 |
(* string -> string -> bool *) |
213 |
fun is_substring needle haystack = |
|
214 |
let |
|
215 |
val length1 = String.size needle |
|
216 |
val length2 = String.size haystack |
|
217 |
in |
|
218 |
if length2 < length1 then |
|
219 |
false |
|
220 |
else if needle = String.substring (haystack, 0, length1) then |
|
221 |
true |
|
222 |
else is_substring needle (String.substring (haystack, 1, length2-1)) |
|
223 |
end |
|
14965 | 224 |
(* string list -> result *) |
14453 | 225 |
fun parse_lines [] = |
14965 | 226 |
UNKNOWN |
14453 | 227 |
| parse_lines (line::lines) = |
14965 | 228 |
if is_substring satisfiable line then |
229 |
SATISFIABLE (parse_assignment [] lines) |
|
230 |
else if is_substring unsatisfiable line then |
|
231 |
UNSATISFIABLE |
|
14453 | 232 |
else |
233 |
parse_lines lines |
|
234 |
in |
|
14965 | 235 |
(parse_lines o (filter (fn l => l <> "")) o split_lines o File.read) path |
14453 | 236 |
end; |
237 |
||
238 |
(* ------------------------------------------------------------------------- *) |
|
239 |
(* make_external_solver: call 'writefn', execute 'cmd', call 'readfn' *) |
|
240 |
(* ------------------------------------------------------------------------- *) |
|
241 |
||
14965 | 242 |
(* string -> (PropLogic.prop_formula -> unit) -> (unit -> result) -> solver *) |
14453 | 243 |
|
244 |
fun make_external_solver cmd writefn readfn fm = |
|
14965 | 245 |
(writefn fm; system cmd; readfn ()); |
14453 | 246 |
|
247 |
(* ------------------------------------------------------------------------- *) |
|
15040 | 248 |
(* read_dimacs_cnf_file: returns a propositional formula that corresponds to *) |
249 |
(* a SAT problem given in DIMACS CNF format *) |
|
250 |
(* ------------------------------------------------------------------------- *) |
|
251 |
||
252 |
(* Path.T -> PropLogic.prop_formula *) |
|
253 |
||
254 |
fun read_dimacs_cnf_file path = |
|
255 |
let |
|
256 |
(* string list -> string list *) |
|
257 |
fun filter_preamble [] = |
|
258 |
error "problem line not found in DIMACS CNF file" |
|
259 |
| filter_preamble (line::lines) = |
|
260 |
if String.isPrefix "c " line then |
|
261 |
(* ignore comments *) |
|
262 |
filter_preamble lines |
|
263 |
else if String.isPrefix "p " line then |
|
264 |
(* ignore the problem line (which must be the last line of the preamble) *) |
|
265 |
(* Ignoring the problem line implies that if the file contains more clauses *) |
|
266 |
(* or variables than specified in its preamble, we will accept it anyway. *) |
|
267 |
lines |
|
268 |
else |
|
269 |
error "preamble in DIMACS CNF file contains a line that does not begin with \"c \" or \"p \"" |
|
270 |
(* string -> int *) |
|
271 |
fun int_from_string s = |
|
272 |
case Int.fromString s of |
|
273 |
SOME i => i |
|
274 |
| NONE => error ("token " ^ quote s ^ "in DIMACS CNF file is not a number") |
|
275 |
(* int list -> int list list *) |
|
276 |
fun clauses xs = |
|
277 |
let |
|
278 |
val (xs1, xs2) = take_prefix (fn i => i <> 0) xs |
|
279 |
in |
|
15128 | 280 |
case xs2 of |
281 |
[] => [xs1] |
|
282 |
| (0::[]) => [xs1] |
|
283 |
| (0::tl) => xs1 :: clauses tl |
|
15329 | 284 |
| _ => raise ERROR (* this cannot happen *) |
15040 | 285 |
end |
286 |
(* int -> PropLogic.prop_formula *) |
|
287 |
fun literal_from_int i = |
|
288 |
(assert (i<>0) "variable index in DIMACS CNF file is 0"; |
|
289 |
if i>0 then |
|
290 |
PropLogic.BoolVar i |
|
291 |
else |
|
292 |
PropLogic.Not (PropLogic.BoolVar (~i))) |
|
293 |
(* PropLogic.prop_formula list -> PropLogic.prop_formula *) |
|
294 |
fun disjunction [] = |
|
295 |
error "empty clause in DIMACS CNF file" |
|
296 |
| disjunction (x::xs) = |
|
297 |
(case xs of |
|
298 |
[] => x |
|
299 |
| _ => PropLogic.Or (x, disjunction xs)) |
|
300 |
(* PropLogic.prop_formula list -> PropLogic.prop_formula *) |
|
301 |
fun conjunction [] = |
|
302 |
error "no clause in DIMACS CNF file" |
|
303 |
| conjunction (x::xs) = |
|
304 |
(case xs of |
|
305 |
[] => x |
|
306 |
| _ => PropLogic.And (x, conjunction xs)) |
|
307 |
in |
|
308 |
(conjunction |
|
309 |
o (map disjunction) |
|
310 |
o (map (map literal_from_int)) |
|
311 |
o clauses |
|
312 |
o (map int_from_string) |
|
313 |
o flat |
|
314 |
o (map (String.fields (fn c => c mem [#" ", #"\t", #"\n"]))) |
|
315 |
o filter_preamble |
|
316 |
o (filter (fn l => l <> "")) |
|
317 |
o split_lines |
|
318 |
o File.read) |
|
319 |
path |
|
320 |
end; |
|
321 |
||
322 |
(* ------------------------------------------------------------------------- *) |
|
14453 | 323 |
(* solvers: a (reference to a) table of all registered SAT solvers *) |
324 |
(* ------------------------------------------------------------------------- *) |
|
325 |
||
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
326 |
val solvers = ref ([] : (string * solver) list); |
14453 | 327 |
|
328 |
(* ------------------------------------------------------------------------- *) |
|
329 |
(* add_solver: updates 'solvers' by adding a new solver *) |
|
330 |
(* ------------------------------------------------------------------------- *) |
|
331 |
||
332 |
(* string * solver -> unit *) |
|
333 |
||
334 |
fun add_solver (name, new_solver) = |
|
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
335 |
(solvers := overwrite_warn (!solvers, (name, new_solver)) ("SAT solver " ^ quote name ^ " was defined before")); |
14453 | 336 |
|
337 |
(* ------------------------------------------------------------------------- *) |
|
338 |
(* invoke_solver: returns the solver associated with the given 'name' *) |
|
339 |
(* Note: If no solver is associated with 'name', exception 'OPTION' will be *) |
|
340 |
(* raised. *) |
|
341 |
(* ------------------------------------------------------------------------- *) |
|
342 |
||
343 |
(* string -> solver *) |
|
344 |
||
345 |
fun invoke_solver name = |
|
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
346 |
(the o assoc) (!solvers, name); |
14453 | 347 |
|
348 |
end; (* SatSolver *) |
|
349 |
||
350 |
||
351 |
(* ------------------------------------------------------------------------- *) |
|
352 |
(* Predefined SAT solvers *) |
|
353 |
(* ------------------------------------------------------------------------- *) |
|
354 |
||
355 |
(* ------------------------------------------------------------------------- *) |
|
14753 | 356 |
(* Internal SAT solver, available as 'SatSolver.invoke_solver "enumerate"' *) |
357 |
(* -- simply enumerates assignments until a satisfying assignment is found *) |
|
14453 | 358 |
(* ------------------------------------------------------------------------- *) |
359 |
||
360 |
let |
|
361 |
fun enum_solver fm = |
|
362 |
let |
|
363 |
(* int list *) |
|
364 |
val indices = PropLogic.indices fm |
|
365 |
(* int list -> int list -> int list option *) |
|
366 |
(* binary increment: list 'xs' of current bits, list 'ys' of all bits (lower bits first) *) |
|
367 |
fun next_list _ ([]:int list) = |
|
15531 | 368 |
NONE |
14453 | 369 |
| next_list [] (y::ys) = |
15531 | 370 |
SOME [y] |
14453 | 371 |
| next_list (x::xs) (y::ys) = |
372 |
if x=y then |
|
373 |
(* reset the bit, continue *) |
|
374 |
next_list xs ys |
|
375 |
else |
|
376 |
(* set the lowest bit that wasn't set before, keep the higher bits *) |
|
15531 | 377 |
SOME (y::x::xs) |
14453 | 378 |
(* int list -> int -> bool *) |
379 |
fun assignment_from_list xs i = |
|
380 |
i mem xs |
|
14965 | 381 |
(* int list -> SatSolver.result *) |
14453 | 382 |
fun solver_loop xs = |
383 |
if PropLogic.eval (assignment_from_list xs) fm then |
|
15531 | 384 |
SatSolver.SATISFIABLE (SOME o (assignment_from_list xs)) |
14453 | 385 |
else |
386 |
(case next_list xs indices of |
|
15531 | 387 |
SOME xs' => solver_loop xs' |
388 |
| NONE => SatSolver.UNSATISFIABLE) |
|
14453 | 389 |
in |
14965 | 390 |
(* start with the "first" assignment (all variables are interpreted as 'false') *) |
14453 | 391 |
solver_loop [] |
392 |
end |
|
393 |
in |
|
14965 | 394 |
SatSolver.add_solver ("enumerate", enum_solver) |
14453 | 395 |
end; |
396 |
||
397 |
(* ------------------------------------------------------------------------- *) |
|
14753 | 398 |
(* Internal SAT solver, available as 'SatSolver.invoke_solver "dpll"' -- a *) |
399 |
(* simple implementation of the DPLL algorithm (cf. L. Zhang, S. Malik: "The *) |
|
400 |
(* Quest for Efficient Boolean Satisfiability Solvers", July 2002, Fig. 1). *) |
|
14453 | 401 |
(* ------------------------------------------------------------------------- *) |
402 |
||
403 |
let |
|
404 |
local |
|
405 |
open PropLogic |
|
406 |
in |
|
407 |
fun dpll_solver fm = |
|
408 |
let |
|
14965 | 409 |
(* We could use 'PropLogic.defcnf fm' instead of 'PropLogic.nnf fm' *) |
410 |
(* but that sometimes introduces more boolean variables than we can *) |
|
411 |
(* handle efficiently. *) |
|
14514 | 412 |
val fm' = PropLogic.nnf fm |
14453 | 413 |
(* int list *) |
14514 | 414 |
val indices = PropLogic.indices fm' |
14453 | 415 |
(* int list -> int -> prop_formula *) |
416 |
fun partial_var_eval [] i = BoolVar i |
|
417 |
| partial_var_eval (x::xs) i = if x=i then True else if x=(~i) then False else partial_var_eval xs i |
|
418 |
(* int list -> prop_formula -> prop_formula *) |
|
419 |
fun partial_eval xs True = True |
|
420 |
| partial_eval xs False = False |
|
421 |
| partial_eval xs (BoolVar i) = partial_var_eval xs i |
|
422 |
| partial_eval xs (Not fm) = SNot (partial_eval xs fm) |
|
423 |
| partial_eval xs (Or (fm1, fm2)) = SOr (partial_eval xs fm1, partial_eval xs fm2) |
|
424 |
| partial_eval xs (And (fm1, fm2)) = SAnd (partial_eval xs fm1, partial_eval xs fm2) |
|
425 |
(* prop_formula -> int list *) |
|
426 |
fun forced_vars True = [] |
|
427 |
| forced_vars False = [] |
|
428 |
| forced_vars (BoolVar i) = [i] |
|
429 |
| forced_vars (Not (BoolVar i)) = [~i] |
|
430 |
| forced_vars (Or (fm1, fm2)) = (forced_vars fm1) inter_int (forced_vars fm2) |
|
431 |
| forced_vars (And (fm1, fm2)) = (forced_vars fm1) union_int (forced_vars fm2) |
|
432 |
(* Above, i *and* ~i may be forced. In this case the first occurrence takes *) |
|
433 |
(* precedence, and the next partial evaluation of the formula returns 'False'. *) |
|
434 |
| forced_vars _ = raise ERROR (* formula is not in negation normal form *) |
|
435 |
(* int list -> prop_formula -> (int list * prop_formula) *) |
|
436 |
fun eval_and_force xs fm = |
|
437 |
let |
|
438 |
val fm' = partial_eval xs fm |
|
439 |
val xs' = forced_vars fm' |
|
440 |
in |
|
441 |
if null xs' then |
|
442 |
(xs, fm') |
|
443 |
else |
|
444 |
eval_and_force (xs@xs') fm' (* xs and xs' should be distinct, so '@' here should have *) |
|
445 |
(* the same effect as 'union_int' *) |
|
446 |
end |
|
447 |
(* int list -> int option *) |
|
448 |
fun fresh_var xs = |
|
449 |
Library.find_first (fn i => not (i mem_int xs) andalso not ((~i) mem_int xs)) indices |
|
450 |
(* int list -> prop_formula -> int list option *) |
|
451 |
(* partial assignment 'xs' *) |
|
452 |
fun dpll xs fm = |
|
453 |
let |
|
454 |
val (xs', fm') = eval_and_force xs fm |
|
455 |
in |
|
456 |
case fm' of |
|
15531 | 457 |
True => SOME xs' |
458 |
| False => NONE |
|
14453 | 459 |
| _ => |
460 |
let |
|
461 |
val x = the (fresh_var xs') (* a fresh variable must exist since 'fm' did not evaluate to 'True'/'False' *) |
|
462 |
in |
|
463 |
case dpll (x::xs') fm' of (* passing fm' rather than fm should save some simplification work *) |
|
15531 | 464 |
SOME xs'' => SOME xs'' |
465 |
| NONE => dpll ((~x)::xs') fm' (* now try interpreting 'x' as 'False' *) |
|
14453 | 466 |
end |
467 |
end |
|
14965 | 468 |
(* int list -> assignment *) |
14453 | 469 |
fun assignment_from_list [] i = |
15531 | 470 |
NONE (* the DPLL procedure didn't provide a value for this variable *) |
14453 | 471 |
| assignment_from_list (x::xs) i = |
15531 | 472 |
if x=i then (SOME true) |
473 |
else if x=(~i) then (SOME false) |
|
14453 | 474 |
else assignment_from_list xs i |
475 |
in |
|
476 |
(* initially, no variable is interpreted yet *) |
|
14965 | 477 |
case dpll [] fm' of |
15531 | 478 |
SOME assignment => SatSolver.SATISFIABLE (assignment_from_list assignment) |
479 |
| NONE => SatSolver.UNSATISFIABLE |
|
14453 | 480 |
end |
481 |
end (* local *) |
|
482 |
in |
|
14965 | 483 |
SatSolver.add_solver ("dpll", dpll_solver) |
14453 | 484 |
end; |
485 |
||
486 |
(* ------------------------------------------------------------------------- *) |
|
14753 | 487 |
(* Internal SAT solver, available as 'SatSolver.invoke_solver "auto"': uses *) |
15299
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents:
15128
diff
changeset
|
488 |
(* the first installed solver (other than "auto" itself) that does not raise *) |
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents:
15128
diff
changeset
|
489 |
(* 'NOT_CONFIGURED'. (However, the solver may return 'UNKNOWN'.) *) |
14453 | 490 |
(* ------------------------------------------------------------------------- *) |
491 |
||
492 |
let |
|
493 |
fun auto_solver fm = |
|
14965 | 494 |
let |
495 |
fun loop [] = |
|
496 |
SatSolver.UNKNOWN |
|
497 |
| loop ((name, solver)::solvers) = |
|
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
498 |
if name="auto" then |
14965 | 499 |
(* do not call solver "auto" from within "auto" *) |
500 |
loop solvers |
|
15299
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents:
15128
diff
changeset
|
501 |
else ( |
14965 | 502 |
(if name="dpll" orelse name="enumerate" then |
14805
eff7b9df27e9
solver "auto" now issues a warning when it uses solver "enumerate"
webertj
parents:
14753
diff
changeset
|
503 |
warning ("Using SAT solver " ^ quote name ^ "; for better performance, consider using an external solver.") |
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
504 |
else |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
505 |
()); |
14965 | 506 |
(* apply 'solver' to 'fm' *) |
15299
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents:
15128
diff
changeset
|
507 |
solver fm |
576fd0b65ed8
solver auto now returns the result of the first solver that does not raise NOT_CONFIGURED (which may be UNKNOWN)
webertj
parents:
15128
diff
changeset
|
508 |
handle SatSolver.NOT_CONFIGURED => loop solvers |
14965 | 509 |
) |
510 |
in |
|
511 |
loop (rev (!SatSolver.solvers)) |
|
512 |
end |
|
14453 | 513 |
in |
514 |
SatSolver.add_solver ("auto", auto_solver) |
|
515 |
end; |
|
516 |
||
517 |
(* ------------------------------------------------------------------------- *) |
|
15332 | 518 |
(* zChaff (http://www.princeton.edu/~chaff/zchaff.html) *) |
14453 | 519 |
(* ------------------------------------------------------------------------- *) |
520 |
||
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
521 |
let |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
522 |
fun zchaff fm = |
14453 | 523 |
let |
14965 | 524 |
val _ = if (getenv "ZCHAFF_HOME") = "" then raise SatSolver.NOT_CONFIGURED else () |
15332 | 525 |
val _ = if (getenv "ZCHAFF_VERSION") <> "2004.5.13" andalso |
526 |
(getenv "ZCHAFF_VERSION") <> "2004.11.15" then raise SatSolver.NOT_CONFIGURED else () |
|
527 |
(* both versions of zChaff appear to have the same interface, so we do *) |
|
528 |
(* not actually need to distinguish between them in the following code *) |
|
14965 | 529 |
val inpath = File.tmp_path (Path.unpack "isabelle.cnf") |
530 |
val outpath = File.tmp_path (Path.unpack "result") |
|
14453 | 531 |
val cmd = (getenv "ZCHAFF_HOME") ^ "/zchaff " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath) |
14965 | 532 |
fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm) |
15040 | 533 |
fun readfn () = SatSolver.parse_std_result_file outpath ("Instance Satisfiable", "", "Instance Unsatisfiable") |
15313
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
534 |
val _ = if File.exists inpath then warning ("overwriting existing file " ^ quote (File.sysify_path inpath)) else () |
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
535 |
val _ = if File.exists outpath then warning ("overwriting existing file " ^ quote (File.sysify_path outpath)) else () |
14965 | 536 |
val result = SatSolver.make_external_solver cmd writefn readfn fm |
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
537 |
val _ = (File.rm inpath handle _ => ()) |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
538 |
val _ = (File.rm outpath handle _ => ()) |
14453 | 539 |
in |
14965 | 540 |
result |
14453 | 541 |
end |
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
542 |
in |
14965 | 543 |
SatSolver.add_solver ("zchaff", zchaff) |
544 |
end; |
|
545 |
||
546 |
(* ------------------------------------------------------------------------- *) |
|
547 |
(* BerkMin 561 (http://eigold.tripod.com/BerkMin.html) *) |
|
548 |
(* ------------------------------------------------------------------------- *) |
|
549 |
||
550 |
let |
|
551 |
fun berkmin fm = |
|
552 |
let |
|
553 |
val _ = if (getenv "BERKMIN_HOME") = "" orelse (getenv "BERKMIN_EXE") = "" then raise SatSolver.NOT_CONFIGURED else () |
|
554 |
val inpath = File.tmp_path (Path.unpack "isabelle.cnf") |
|
555 |
val outpath = File.tmp_path (Path.unpack "result") |
|
556 |
val cmd = (getenv "BERKMIN_HOME") ^ "/" ^ (getenv "BERKMIN_EXE") ^ " " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath) |
|
557 |
fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm) |
|
558 |
fun readfn () = SatSolver.parse_std_result_file outpath ("Satisfiable !!", "solution =", "UNSATISFIABLE !!") |
|
15313
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
559 |
val _ = if File.exists inpath then warning ("overwriting existing file " ^ quote (File.sysify_path inpath)) else () |
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
560 |
val _ = if File.exists outpath then warning ("overwriting existing file " ^ quote (File.sysify_path outpath)) else () |
14965 | 561 |
val result = SatSolver.make_external_solver cmd writefn readfn fm |
562 |
val _ = (File.rm inpath handle _ => ()) |
|
563 |
val _ = (File.rm outpath handle _ => ()) |
|
564 |
in |
|
565 |
result |
|
566 |
end |
|
567 |
in |
|
568 |
SatSolver.add_solver ("berkmin", berkmin) |
|
569 |
end; |
|
570 |
||
571 |
(* ------------------------------------------------------------------------- *) |
|
572 |
(* Jerusat 1.3 (http://www.cs.tau.ac.il/~ale1/) *) |
|
573 |
(* ------------------------------------------------------------------------- *) |
|
574 |
||
575 |
let |
|
576 |
fun jerusat fm = |
|
577 |
let |
|
578 |
val _ = if (getenv "JERUSAT_HOME") = "" then raise SatSolver.NOT_CONFIGURED else () |
|
579 |
val inpath = File.tmp_path (Path.unpack "isabelle.cnf") |
|
580 |
val outpath = File.tmp_path (Path.unpack "result") |
|
581 |
val cmd = (getenv "JERUSAT_HOME") ^ "/Jerusat1.3 " ^ (Path.pack inpath) ^ " > " ^ (Path.pack outpath) |
|
582 |
fun writefn fm = SatSolver.write_dimacs_cnf_file inpath (PropLogic.defcnf fm) |
|
583 |
fun readfn () = SatSolver.parse_std_result_file outpath ("s SATISFIABLE", "v ", "s UNSATISFIABLE") |
|
15313
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
584 |
val _ = if File.exists inpath then warning ("overwriting existing file " ^ quote (File.sysify_path inpath)) else () |
24aee76539df
external solvers may now overwrite existing temporary files
webertj
parents:
15299
diff
changeset
|
585 |
val _ = if File.exists outpath then warning ("overwriting existing file " ^ quote (File.sysify_path outpath)) else () |
14965 | 586 |
val result = SatSolver.make_external_solver cmd writefn readfn fm |
587 |
val _ = (File.rm inpath handle _ => ()) |
|
588 |
val _ = (File.rm outpath handle _ => ()) |
|
589 |
in |
|
590 |
result |
|
591 |
end |
|
592 |
in |
|
593 |
SatSolver.add_solver ("jerusat", jerusat) |
|
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
594 |
end; |
14453 | 595 |
|
596 |
(* ------------------------------------------------------------------------- *) |
|
597 |
(* Add code for other SAT solvers below. *) |
|
598 |
(* ------------------------------------------------------------------------- *) |
|
599 |
||
600 |
(* |
|
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
601 |
let |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
602 |
fun mysolver fm = ... |
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
603 |
in |
14965 | 604 |
SatSolver.add_solver ("myname", (fn fm => if mysolver_is_configured then mysolver fm else raise SatSolver.NOT_CONFIGURED)); -- register the solver |
14487
157d0ea7b2da
Installed solvers now determined at call time (as opposed to compile time)
webertj
parents:
14460
diff
changeset
|
605 |
end; |
14453 | 606 |
|
607 |
-- the solver is now available as SatSolver.invoke_solver "myname" |
|
608 |
*) |