author | Norbert Schirmer <norbert.schirmer@web.de> |
Tue, 21 May 2019 14:37:39 +0200 | |
changeset 572 | 438703674711 |
parent 569 | f875a25aa72d |
child 574 | 034150db9d91 |
permissions | -rw-r--r-- |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
1 |
|
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
2 |
theory Sat |
441 | 3 |
imports "../Appendix" "../First_Steps" |
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
4 |
begin |
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
5 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
6 |
section \<open>SAT Solvers\label{rec:sat}\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
7 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
8 |
text \<open> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
9 |
{\bf Problem:} |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
10 |
You like to use a SAT solver to find out whether |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
11 |
an Isabelle formula is satisfiable or not.\smallskip |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
12 |
|
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
13 |
{\bf Solution:} Isabelle contains a general interface for |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
14 |
a number of external SAT solvers (including ZChaff and Minisat) |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
15 |
and also contains a simple internal SAT solver that |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
16 |
is based on the DPLL algorithm.\smallskip |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
17 |
|
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
18 |
The SAT solvers expect a propositional formula as input and produce |
184 | 19 |
a result indicating that the formula is either satisfiable, unsatisfiable or |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
20 |
unknown. The type of the propositional formula is |
458
242e81f4d461
updated to post-2011 Isabelle
Christian Urban <urbanc@in.tum.de>
parents:
441
diff
changeset
|
21 |
@{ML_type "Prop_Logic.prop_formula"} with the usual constructors such |
242e81f4d461
updated to post-2011 Isabelle
Christian Urban <urbanc@in.tum.de>
parents:
441
diff
changeset
|
22 |
as @{ML And in Prop_Logic}, @{ML Or in Prop_Logic} and so on. |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
23 |
|
458
242e81f4d461
updated to post-2011 Isabelle
Christian Urban <urbanc@in.tum.de>
parents:
441
diff
changeset
|
24 |
The function @{ML Prop_Logic.prop_formula_of_term} translates an Isabelle |
184 | 25 |
term into a propositional formula. Let |
26 |
us illustrate this function by translating @{term "A \<and> \<not>A \<or> B"}. |
|
27 |
The function will return a propositional formula and a table. Suppose |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
28 |
\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
29 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
30 |
ML %grayML\<open>val (pform, table) = |
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
31 |
Prop_Logic.prop_formula_of_term @{term "A \<and> \<not>A \<or> B"} Termtab.empty\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
32 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
33 |
text \<open> |
184 | 34 |
then the resulting propositional formula @{ML pform} is |
35 |
||
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
36 |
@{ML [display] \<open>Or (And (BoolVar 1, Not (BoolVar 1)), BoolVar 2)\<close> in Prop_Logic} |
184 | 37 |
|
38 |
||
39 |
where indices are assigned for the variables |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
40 |
\<open>A\<close> and \<open>B\<close>, respectively. This assignment is recorded |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
41 |
in the table that is given to the translation function and also returned |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
42 |
(appropriately updated) in the result. In the case above the |
184 | 43 |
input table is empty (i.e.~@{ML Termtab.empty}) and the output table is |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
44 |
|
572
438703674711
prefer more result checking in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
569
diff
changeset
|
45 |
@{ML_response [display,gray] |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
46 |
\<open>Termtab.dest table\<close> |
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
47 |
\<open>[(Free ("A", "bool"), 1), (Free ("B", "bool"), 2)]\<close>} |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
48 |
|
184 | 49 |
An index is also produced whenever the translation |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
50 |
function cannot find an appropriate propositional formula for a term. |
184 | 51 |
Attempting to translate @{term "\<forall>x::nat. P x"} |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
52 |
\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
53 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
54 |
ML %grayML\<open>val (pform', table') = |
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
55 |
Prop_Logic.prop_formula_of_term @{term "\<forall>x::nat. P x"} Termtab.empty\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
56 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
57 |
text \<open> |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
58 |
returns @{ML \<open>BoolVar 1\<close> in Prop_Logic} for @{ML pform'} and the table |
184 | 59 |
@{ML table'} is: |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
60 |
|
572
438703674711
prefer more result checking in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
569
diff
changeset
|
61 |
@{ML_response [display,gray] |
438703674711
prefer more result checking in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
569
diff
changeset
|
62 |
\<open>map (apfst (YXML.content_of o Syntax.string_of_term @{context})) (Termtab.dest table')\<close> |
438703674711
prefer more result checking in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
569
diff
changeset
|
63 |
\<open>("\<forall>x. P x", 1)\<close>} |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
64 |
|
191 | 65 |
In the print out of the tabel, we used some pretty printing scaffolding |
66 |
to see better which assignment the table contains. |
|
184 | 67 |
|
68 |
Having produced a propositional formula, you can now call the SAT solvers |
|
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
69 |
with the function @{ML \<open>SAT_Solver.invoke_solver\<close>}. For example |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
70 |
|
567
f7c97e64cc2a
tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents:
565
diff
changeset
|
71 |
@{ML_matchresult_fake [display,gray] |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
72 |
\<open>SAT_Solver.invoke_solver "minisat" pform\<close> |
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
73 |
\<open>SAT_Solver.SATISFIABLE assg\<close>} |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
74 |
\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
75 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
76 |
text \<open> |
184 | 77 |
determines that the formula @{ML pform} is satisfiable. If we inspect |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
78 |
the returned function \<open>assg\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
79 |
|
567
f7c97e64cc2a
tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents:
565
diff
changeset
|
80 |
@{ML_matchresult [display,gray] |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
81 |
\<open>let |
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
82 |
val SAT_Solver.SATISFIABLE assg = SAT_Solver.invoke_solver "auto" pform |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
83 |
in |
184 | 84 |
(assg 1, assg 2, assg 3) |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
85 |
end\<close> |
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
86 |
\<open>(NONE, SOME true, NONE)\<close>} |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
87 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
88 |
we obtain a possible assignment for the variables \<open>A\<close> an \<open>B\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
89 |
that makes the formula satisfiable. |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
90 |
|
556
3c214b215f7e
some small updates for Isabelle and corrections in the Parsing chapter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
553
diff
changeset
|
91 |
Note that we invoked the SAT solver with the string @{text [quotes] auto}. |
3c214b215f7e
some small updates for Isabelle and corrections in the Parsing chapter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
553
diff
changeset
|
92 |
This string specifies which specific SAT solver is invoked. If instead |
3c214b215f7e
some small updates for Isabelle and corrections in the Parsing chapter
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
553
diff
changeset
|
93 |
called with @{text [quotes] "auto"} |
184 | 94 |
several external SAT solvers will be tried (assuming they are installed). |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
95 |
|
184 | 96 |
There are also two tactics that make use of SAT solvers. One |
553
c53d74b34123
updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
517
diff
changeset
|
97 |
is the tactic @{ML sat_tac in SAT}. For example |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
98 |
\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
99 |
|
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
100 |
lemma "True" |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
101 |
apply(tactic \<open>SAT.sat_tac @{context} 1\<close>) |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
102 |
done |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
103 |
|
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
104 |
text \<open> |
569
f875a25aa72d
prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents:
567
diff
changeset
|
105 |
However, for proving anything more exciting using @{ML \<open>sat_tac\<close> in SAT} you |
191 | 106 |
have to use a SAT solver that can produce a proof. The internal |
107 |
one is not usuable for this. |
|
184 | 108 |
|
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
109 |
\begin{readmore} |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
110 |
The interface for the external SAT solvers is implemented |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
111 |
in @{ML_file "HOL/Tools/sat_solver.ML"}. This file contains also a simple |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
112 |
SAT solver based on the DPLL algorithm. The tactics for SAT solvers are |
553
c53d74b34123
updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
517
diff
changeset
|
113 |
implemented in @{ML_file "HOL/Tools/sat.ML"}. Functions concerning |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
114 |
propositional formulas are implemented in @{ML_file |
181
5baaabe1ab92
updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents:
180
diff
changeset
|
115 |
"HOL/Tools/prop_logic.ML"}. The tables used in the translation function are |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
116 |
implemented in @{ML_file "Pure/General/table.ML"}. |
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
117 |
\end{readmore} |
565
cecd7a941885
isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents:
562
diff
changeset
|
118 |
\<close> |
180
9c25418db6f0
added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents:
127
diff
changeset
|
119 |
|
127
74846cb0fff9
updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff
changeset
|
120 |
|
517
d8c376662bb4
removed special ML-setup and replaced it by explicit markups (i.e., %grayML)
Christian Urban <urbanc@in.tum.de>
parents:
458
diff
changeset
|
121 |
end |