17618
|
1 |
(* Title: HOL/Tools/sat_funcs.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Stephan Merz and Alwen Tiu, QSL Team, LORIA (http://qsl.loria.fr)
|
|
4 |
Copyright 2005
|
|
5 |
|
|
6 |
|
|
7 |
Proof reconstruction from SAT solvers.
|
|
8 |
|
|
9 |
Description:
|
|
10 |
This file defines several tactics to invoke a proof-producing
|
|
11 |
SAT solver on a propositional goal in clausal form.
|
|
12 |
|
|
13 |
We use a sequent presentation of clauses to speed up resolution
|
|
14 |
proof reconstruction.
|
|
15 |
We call such clauses as "raw clauses", which are of the form
|
|
16 |
[| c1; c2; ...; ck |] ==> False
|
|
17 |
where each clause ci is of the form
|
|
18 |
[|l1, l2, ..., lm |] ==> False,
|
|
19 |
where li is a literal (see also comments in cnf_funcs.ML).
|
|
20 |
|
|
21 |
-- observe that this is the "dualized" version of the standard
|
|
22 |
clausal form
|
|
23 |
l1' \/ l2' \/ ... \/ lm', where li is the negation normal
|
|
24 |
form of ~li'.
|
|
25 |
The tactic produces a clause representation of the given goal
|
|
26 |
in DIMACS format and invokes a SAT solver, which should return
|
|
27 |
a proof consisting of a sequence of resolution steps, indicating
|
|
28 |
the two input clauses and the variable resolved upon, and resulting
|
|
29 |
in new clauses, leading to the empty clause (i.e., False).
|
|
30 |
The tactic replays this proof in Isabelle and thus solves the
|
|
31 |
overall goal.
|
|
32 |
|
|
33 |
There are two SAT tactics available. They differ in the CNF transformation
|
|
34 |
used. The "sat_tac" uses naive CNF transformation to transform the theorem
|
|
35 |
to be proved before giving it to SAT solver. The naive transformation in
|
|
36 |
some worst case can lead to explonential blow up in formula size.
|
|
37 |
The other tactic, the "satx_tac" uses the "definitional CNF transformation"
|
|
38 |
which produces formula of linear size increase compared to the input formula.
|
|
39 |
This transformation introduces new variables. See also cnf_funcs.ML for
|
|
40 |
more comments.
|
|
41 |
|
|
42 |
|
|
43 |
Notes for the current revision:
|
|
44 |
- The current version supports only zChaff prover.
|
|
45 |
- The environment variable ZCHAFF_HOME must be set to point to
|
|
46 |
the directory where zChaff executable resides
|
|
47 |
- The environment variable ZCHAFF_VERSION must be set according to
|
|
48 |
the version of zChaff used. Current supported version of zChaff:
|
|
49 |
zChaff version 2004.11.15
|
|
50 |
|
|
51 |
*)
|
|
52 |
|
|
53 |
(***************************************************************************)
|
|
54 |
(** Array of clauses **)
|
|
55 |
|
|
56 |
signature CLAUSEARR =
|
|
57 |
sig
|
|
58 |
val init : int -> unit
|
|
59 |
val register_at : int -> Thm.thm -> unit
|
|
60 |
val register_list : Thm.thm list -> unit
|
|
61 |
val getClause : int -> Thm.thm option
|
|
62 |
end
|
|
63 |
|
|
64 |
structure ClauseArr : CLAUSEARR =
|
|
65 |
struct
|
|
66 |
|
|
67 |
val clauses : ((Thm.thm option) array) ref = ref (Array.array(1, NONE));
|
|
68 |
|
|
69 |
fun init size = (clauses := Array.array(size, NONE))
|
|
70 |
|
|
71 |
fun register_at i c = Array.update (!clauses, i, (SOME c))
|
|
72 |
|
|
73 |
fun reg_list n nil = ()
|
|
74 |
| reg_list n (x::l) =
|
|
75 |
(register_at n x; reg_list (n+1) l)
|
|
76 |
|
|
77 |
fun register_list l = reg_list 0 l
|
|
78 |
|
|
79 |
fun getClause i = Array.sub (!clauses, i)
|
|
80 |
|
|
81 |
end
|
|
82 |
|
|
83 |
|
|
84 |
(***************************************************************************)
|
|
85 |
|
|
86 |
signature SAT =
|
|
87 |
sig
|
|
88 |
val trace_sat : bool ref (* trace tactic *)
|
|
89 |
val sat_tac : Tactical.tactic
|
|
90 |
val satx_tac : Tactical.tactic
|
|
91 |
end
|
|
92 |
|
|
93 |
functor SATFunc (structure cnf_struct : CNF) : SAT =
|
|
94 |
struct
|
|
95 |
|
|
96 |
structure cnf = cnf_struct
|
|
97 |
|
|
98 |
val trace_sat = ref false; (* debugging flag *)
|
|
99 |
|
|
100 |
|
|
101 |
(* Look up the Isabelle atom corresponding to a DIMACS index in the
|
|
102 |
reverse dictionary. This entry should exist, otherwise an error
|
|
103 |
is raised.
|
|
104 |
*)
|
|
105 |
fun rev_lookup idx dictionary =
|
|
106 |
let
|
|
107 |
fun rev_assoc [] = NONE
|
|
108 |
| rev_assoc ((key, entry)::list) =
|
|
109 |
if entry = idx then SOME key else rev_assoc list
|
|
110 |
in
|
|
111 |
the (rev_assoc dictionary)
|
|
112 |
end;
|
|
113 |
|
|
114 |
|
|
115 |
(* Convert rules of the form
|
|
116 |
l1 ==> l2 ==> .. ==> li ==> .. ==> False
|
|
117 |
to
|
|
118 |
l1 ==> l2 ==> .... ==> ~li
|
|
119 |
*)
|
|
120 |
fun swap_prem rslv c =
|
|
121 |
let
|
|
122 |
val thm1 = rule_by_tactic (metacut_tac c 1 THEN
|
|
123 |
(atac 1) THEN (REPEAT_SOME atac)) rslv
|
|
124 |
in
|
|
125 |
rule_by_tactic (ALLGOALS (cnf.weakening_tac)) thm1
|
|
126 |
end
|
|
127 |
|
|
128 |
(*** Proof reconstruction: given two clauses
|
|
129 |
[| x1 ; .. ; a ; .. ; xn |] ==> False
|
|
130 |
and
|
|
131 |
[| y1 ; .. ; ~a ; .. ; ym |] ==> False ,
|
|
132 |
|
|
133 |
we firt convert the first clause into
|
|
134 |
|
|
135 |
[| x1 ; ... ; xn |] ==> ~a (using swap_prem)
|
|
136 |
|
|
137 |
and do a resolution with the second clause to produce
|
|
138 |
[| y1 ; ... ; x1 ; ... ; xn ; ... ; yn |] ==> False
|
|
139 |
|
|
140 |
***)
|
|
141 |
|
|
142 |
fun dual (Const("Trueprop",_) $ x) (Const("Trueprop",_) $ y) = dual x y
|
|
143 |
| dual (Const("Not",_) $ x) y = (x = y)
|
|
144 |
| dual x (Const("Not",_) $ y) = (x = y)
|
|
145 |
| dual x y = false
|
|
146 |
|
|
147 |
(* Check if an atom has a dual in a list of atoms *)
|
|
148 |
|
|
149 |
fun dual_mem x nil = false
|
|
150 |
| dual_mem x (y::l) = if (dual x y) then true else dual_mem x l
|
|
151 |
|
|
152 |
|
|
153 |
fun replay_chain sg idx (c::cs) =
|
|
154 |
let
|
|
155 |
val (SOME fc) = ClauseArr.getClause c;
|
|
156 |
|
|
157 |
fun strip_neg (Const("Trueprop", _) $ x) = strip_neg x
|
|
158 |
| strip_neg (Const("Not",_) $ x) = x
|
|
159 |
| strip_neg x = x
|
|
160 |
|
|
161 |
(* Find out which atom (literal) is used in the resolution *)
|
|
162 |
fun res_atom nil l = raise THM ("Proof reconstruction failed!", 0, [])
|
|
163 |
| res_atom (x::l1) l2 =
|
|
164 |
if (dual_mem x l2) then strip_neg x
|
|
165 |
else res_atom l1 l2
|
|
166 |
|
|
167 |
fun replay old [] = old
|
|
168 |
| replay old (cls :: clss) =
|
|
169 |
let
|
|
170 |
val (SOME icls) = ClauseArr.getClause cls;
|
|
171 |
val var = res_atom (prems_of old) (prems_of icls);
|
|
172 |
val atom = cterm_of sg var;
|
|
173 |
val rslv = instantiate' [] [SOME atom] notI;
|
|
174 |
|
|
175 |
val _ = if (!trace_sat) then
|
|
176 |
(
|
|
177 |
writeln "-- resolving clause:";
|
|
178 |
print_thm old;
|
|
179 |
writeln "-- with clause: ";
|
|
180 |
print_thm icls;
|
|
181 |
writeln "-- using ";
|
|
182 |
writeln (string_of_cterm atom)
|
|
183 |
) else ();
|
|
184 |
|
|
185 |
val thm1 = (
|
|
186 |
rule_by_tactic (REPEAT_SOME (rtac (swap_prem rslv old))) icls
|
|
187 |
handle THM _ =>
|
|
188 |
rule_by_tactic (REPEAT_SOME (rtac (swap_prem rslv icls))) old );
|
|
189 |
|
|
190 |
val new = rule_by_tactic distinct_subgoals_tac thm1;
|
|
191 |
val _ = if (!trace_sat) then (writeln "-- resulting clause:"; print_thm new)
|
|
192 |
else ()
|
|
193 |
in
|
|
194 |
replay new clss
|
|
195 |
end
|
|
196 |
in
|
|
197 |
ClauseArr.register_at idx (replay fc cs);
|
|
198 |
if (!trace_sat) then (
|
|
199 |
writeln ("-- Replay chain successful. " ^
|
|
200 |
"The resulting clause stored at #" ^ (Int.toString idx))
|
|
201 |
) else ()
|
|
202 |
end
|
|
203 |
|
|
204 |
|
|
205 |
(* Replay the resolution proof from file prf_file with hypotheses hyps.
|
|
206 |
Returns the theorem established by the proof (which is just False).
|
|
207 |
*)
|
|
208 |
fun replay_prf sg tab size =
|
|
209 |
let
|
|
210 |
val prf = Inttab.dest tab;
|
|
211 |
|
|
212 |
fun complete nil = true
|
|
213 |
| complete (x::l) = (
|
|
214 |
case ClauseArr.getClause x of
|
|
215 |
NONE => false
|
|
216 |
| (SOME _) => complete l)
|
|
217 |
|
|
218 |
fun do_chains [] = ()
|
|
219 |
| do_chains (pr :: rs) =
|
|
220 |
let val (idx, cls) = pr
|
|
221 |
in
|
|
222 |
if (complete cls) then
|
|
223 |
(replay_chain sg idx cls; do_chains rs)
|
|
224 |
else do_chains (rs @ [pr])
|
|
225 |
end
|
|
226 |
in
|
|
227 |
if (!trace_sat) then (
|
|
228 |
writeln "Proof trace from SAT solver: ";
|
|
229 |
print prf ; ()
|
|
230 |
) else () ;
|
|
231 |
do_chains prf;
|
|
232 |
ClauseArr.getClause size
|
|
233 |
end;
|
|
234 |
|
|
235 |
|
|
236 |
(***************************************************************************)
|
|
237 |
(*** Functions to build the sat tactic ***)
|
|
238 |
|
|
239 |
(* A trivial tactic, used in preprocessing before calling the main
|
|
240 |
tactic *)
|
|
241 |
|
|
242 |
val pre_sat_tac = (REPEAT (etac conjE 1)) THEN
|
|
243 |
(REPEAT ((atac 1) ORELSE (etac FalseE 1)))
|
|
244 |
|
|
245 |
fun collect_atoms (Const("Trueprop",_) $ x) l = collect_atoms x l
|
|
246 |
| collect_atoms (Const("op |", _) $ x $ y) l =
|
|
247 |
collect_atoms x (collect_atoms y l)
|
|
248 |
| collect_atoms x l = if (x mem l) then l else (x::l)
|
|
249 |
|
|
250 |
fun has_duals nil = false
|
|
251 |
| has_duals (x::l) = if (dual_mem x l) then true else has_duals l
|
|
252 |
|
|
253 |
fun trivial_clause (Const("True",_)) = true
|
|
254 |
| trivial_clause c = has_duals (collect_atoms c nil)
|
|
255 |
|
|
256 |
(* Remove trivial clauses *)
|
|
257 |
fun filter_clauses nil = nil
|
|
258 |
| filter_clauses (x::l) =
|
|
259 |
if (trivial_clause (term_of (cprop_of x))) then filter_clauses l
|
|
260 |
else (x:: filter_clauses l)
|
|
261 |
|
|
262 |
fun is_true assignment x =
|
|
263 |
case (assignment x) of
|
|
264 |
NONE => false
|
|
265 |
| SOME b => b
|
|
266 |
|
|
267 |
fun get_model dict assignment =
|
|
268 |
map (fn (x,y) => x) (List.filter (fn (x,y) => is_true assignment y) dict)
|
|
269 |
|
|
270 |
fun string_of_model sg nil = ""
|
|
271 |
| string_of_model sg [x] = Sign.string_of_term sg x
|
|
272 |
| string_of_model sg (x::l) = (Sign.string_of_term sg x) ^ ", " ^ (string_of_model sg l)
|
|
273 |
|
|
274 |
(* Run external SAT solver with the given clauses. Reconstruct a proof from
|
|
275 |
the resulting proof trace of the SAT solver.
|
|
276 |
*)
|
|
277 |
|
|
278 |
fun rawsat_thm sg prems =
|
|
279 |
let val thms = filter_clauses prems
|
|
280 |
val (fm, dict) = cnf.cnf2prop thms
|
|
281 |
val raw_thms = cnf.cnf2raw_thms thms
|
|
282 |
in
|
|
283 |
let
|
|
284 |
val result = SatSolver.invoke_solver "zchaff_with_proofs" fm;
|
|
285 |
in
|
|
286 |
case result of
|
|
287 |
(SatSolver.UNSATISFIABLE (SOME (table, size))) =>
|
|
288 |
let val _ = ClauseArr.init (size + 1);
|
|
289 |
val _ = ClauseArr.register_list
|
|
290 |
(map (fn x => (rule_by_tactic distinct_subgoals_tac x)) raw_thms);
|
|
291 |
val (SOME thm1) = replay_prf sg table size
|
|
292 |
in
|
|
293 |
thm1
|
|
294 |
end
|
|
295 |
| (SatSolver.SATISFIABLE model) =>
|
|
296 |
let val msg = "\nSAT solver found a countermodel:\n{ " ^
|
|
297 |
(string_of_model sg (get_model dict model)) ^ " }\n"
|
|
298 |
in
|
|
299 |
raise THM (msg, 0, [])
|
|
300 |
end
|
|
301 |
| _ => raise THM ("SAT solver failed!\n", 0, [])
|
|
302 |
|
|
303 |
end
|
|
304 |
end
|
|
305 |
|
|
306 |
fun cnfsat_basic_tac state =
|
|
307 |
let val sg = Thm.sign_of_thm state
|
|
308 |
in
|
|
309 |
METAHYPS (fn prems => rtac (rawsat_thm sg prems) 1) 1 state
|
|
310 |
end
|
|
311 |
|
|
312 |
(* Tactic for calling external SAT solver, taking as input CNF clauses *)
|
|
313 |
val cnfsat_tac = pre_sat_tac THEN (IF_UNSOLVED cnfsat_basic_tac)
|
|
314 |
|
|
315 |
|
|
316 |
(*
|
|
317 |
Tactic for calling external SAT solver, taking as input arbitrary formula.
|
|
318 |
*)
|
|
319 |
val sat_tac = cnf.cnf_thin_tac THEN cnfsat_tac;
|
|
320 |
|
|
321 |
(*
|
|
322 |
Tactic for calling external SAT solver, taking as input arbitratry formula.
|
|
323 |
The input is translated to CNF (in primitive form), possibly introducing
|
|
324 |
new literals.
|
|
325 |
*)
|
|
326 |
val satx_tac = cnf.cnfx_thin_tac THEN cnfsat_tac;
|
|
327 |
|
|
328 |
end (*of structure*)
|