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