61
|
1 |
theory ExternalSolver
|
|
2 |
imports "../Base"
|
|
3 |
begin
|
|
4 |
|
81
|
5 |
section {* Using an External Solver\label{rec:external} *}
|
61
|
6 |
|
|
7 |
text {*
|
|
8 |
{\bf Problem:}
|
79
|
9 |
You want to use an external solver, because the solver might be more efficient
|
|
10 |
for deciding a certain class of formulae than Isabelle tactics.
|
61
|
11 |
\smallskip
|
|
12 |
|
79
|
13 |
{\bf Solution:} The easiest way to do this is by implementing an oracle.
|
|
14 |
We will also construct proofs inside Isabelle by using the results produced
|
|
15 |
by the oracle.
|
61
|
16 |
\smallskip
|
|
17 |
|
|
18 |
\begin{readmore}
|
|
19 |
A short introduction to oracles can be found in [isar-ref: no suitable label
|
|
20 |
for section 3.11]. A simple example is given in
|
|
21 |
@{ML_file "FOL/ex/IffOracle"}.
|
|
22 |
(TODO: add more references to the code)
|
|
23 |
\end{readmore}
|
|
24 |
|
79
|
25 |
For our explanation here, we will use the @{text metis} prover for proving
|
|
26 |
propositional formulae. The general method will be roughly as follows:
|
|
27 |
Given a goal @{text G}, we transform it into the syntactical respresentation
|
|
28 |
of @{text "metis"}, build the CNF and then let metis search for a refutation.
|
|
29 |
@{text Metis} will either return the proved goal or raise an exception meaning
|
|
30 |
that it was unable to prove the goal (FIXME: is this so?).
|
61
|
31 |
|
79
|
32 |
The translation function from Isabelle propositions into formulae of
|
|
33 |
@{text metis} is as follows:
|
61
|
34 |
*}
|
|
35 |
|
79
|
36 |
ML{*fun trans t =
|
61
|
37 |
(case t of
|
|
38 |
@{term Trueprop} $ t => trans t
|
|
39 |
| @{term True} => Metis.Formula.True
|
|
40 |
| @{term False} => Metis.Formula.False
|
|
41 |
| @{term Not} $ t => Metis.Formula.Not (trans t)
|
|
42 |
| @{term "op &"} $ t1 $ t2 => Metis.Formula.And (trans t1, trans t2)
|
|
43 |
| @{term "op |"} $ t1 $ t2 => Metis.Formula.Or (trans t1, trans t2)
|
|
44 |
| @{term "op -->"} $ t1 $ t2 => Metis.Formula.Imp (trans t1, trans t2)
|
|
45 |
| @{term "op = :: bool => bool => bool"} $ t1 $ t2 =>
|
79
|
46 |
Metis.Formula.Iff (trans t1, trans t2)
|
61
|
47 |
| Free (n, @{typ bool}) => Metis.Formula.Atom (n, [])
|
79
|
48 |
| _ => error "inacceptable term")*}
|
|
49 |
|
|
50 |
|
|
51 |
text {*
|
|
52 |
An example is as follows:
|
|
53 |
|
|
54 |
@{ML_response [display,gray] "trans @{prop \"A \<and> B\"}"
|
|
55 |
"Metis.Formula.And
|
|
56 |
(Metis.Formula.Atom (\"A\", []), Metis.Formula.Atom (\"B\", []))"}
|
|
57 |
|
|
58 |
|
|
59 |
The next function computes the conjunctive-normal-form.
|
61
|
60 |
*}
|
|
61 |
|
|
62 |
|
79
|
63 |
ML %linenumbers{*fun make_cnfs fm =
|
|
64 |
fm |> Metis.Formula.Not
|
|
65 |
|> Metis.Normalize.cnf
|
|
66 |
|> map Metis.Formula.stripConj
|
|
67 |
|> map (map Metis.Formula.stripDisj)
|
|
68 |
|> map (map (map Metis.Literal.fromFormula))
|
|
69 |
|> map (map Metis.LiteralSet.fromList)
|
|
70 |
|> map (map Metis.Thm.axiom)*}
|
61
|
71 |
|
79
|
72 |
text {*
|
|
73 |
(FIXME: Is there a deep reason why Metis.Normalize.cnf returns a list?)
|
|
74 |
|
|
75 |
(FIXME: What does Line 8 do?)
|
|
76 |
|
|
77 |
(FIXME: Can this code improved?)
|
|
78 |
|
|
79 |
|
|
80 |
Setting up the resolution.
|
61
|
81 |
*}
|
|
82 |
|
79
|
83 |
ML{*fun refute cls =
|
|
84 |
let val result =
|
|
85 |
Metis.Resolution.loop
|
|
86 |
(Metis.Resolution.new Metis.Resolution.default cls)
|
|
87 |
in
|
|
88 |
(case result of
|
|
89 |
Metis.Resolution.Contradiction _ => true
|
|
90 |
| Metis.Resolution.Satisfiable _ => false)
|
|
91 |
end*}
|
61
|
92 |
|
79
|
93 |
text {*
|
|
94 |
Stringing the functions together.
|
61
|
95 |
*}
|
|
96 |
|
79
|
97 |
ML{*fun solve f = List.all refute (make_cnfs f)*}
|
|
98 |
|
|
99 |
text {* Setting up the oracle*}
|
|
100 |
|
|
101 |
ML{*fun prop_dp (thy, t) =
|
|
102 |
if solve (trans t) then (Thm.cterm_of thy t)
|
|
103 |
else error "Proof failed."*}
|
|
104 |
|
61
|
105 |
oracle prop_oracle = prop_dp
|
|
106 |
|
79
|
107 |
text {* (FIXME: What does oracle this do?) *}
|
|
108 |
|
|
109 |
|
|
110 |
ML{*fun prop_oracle_tac ctxt =
|
61
|
111 |
SUBGOAL (fn (goal, i) =>
|
79
|
112 |
(case (try prop_oracle (ProofContext.theory_of ctxt, goal)) of
|
61
|
113 |
SOME thm => rtac thm i
|
79
|
114 |
| NONE => no_tac))*}
|
|
115 |
|
|
116 |
text {*
|
|
117 |
(FIXME: The oracle returns a @{text cterm}. How is it possible
|
|
118 |
that I can apply this cterm with @{ML rtac}?)
|
61
|
119 |
*}
|
|
120 |
|
|
121 |
method_setup prop_oracle = {*
|
|
122 |
Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' (prop_oracle_tac ctxt))
|
|
123 |
*} "Oracle-based decision procedure for propositional logic"
|
|
124 |
|
79
|
125 |
text {* (FIXME What does @{ML Method.SIMPLE_METHOD'} do?) *}
|
|
126 |
|
|
127 |
lemma test: "p \<or> \<not>p"
|
61
|
128 |
by prop_oracle
|
|
129 |
|
79
|
130 |
text {* (FIXME: say something about what the proof of the oracle is) *}
|
|
131 |
|
|
132 |
ML {* Thm.proof_of @{thm test} *}
|
|
133 |
|
61
|
134 |
lemma "((p \<longrightarrow> q) \<longrightarrow> p) \<longrightarrow> p"
|
|
135 |
by prop_oracle
|
|
136 |
|
|
137 |
lemma "\<forall>x::nat. x \<ge> 0"
|
|
138 |
sorry
|
|
139 |
|
79
|
140 |
text {*
|
|
141 |
(FIXME: proof reconstruction)
|
|
142 |
*}
|
61
|
143 |
|
79
|
144 |
|
|
145 |
|
|
146 |
text {*
|
|
147 |
For communication with external programs, there are the primitives
|
|
148 |
@{ML_text system} and @{ML_text system_out}, the latter of which captures
|
|
149 |
the invoked program's output. For simplicity, here, we will use metis, an
|
|
150 |
external solver included in the Isabelle destribution. Since it is written
|
|
151 |
in ML, we can call it directly without the detour of invoking an external
|
|
152 |
program.
|
|
153 |
*}
|
61
|
154 |
|
|
155 |
|
|
156 |
end |