author | blanchet |
Sun, 02 Feb 2014 20:53:51 +0100 | |
changeset 55260 | ada3ae6458d4 |
parent 55257 | abfd7b90bba2 |
child 55278 | 73372494fd80 |
permissions | -rw-r--r-- |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_isar_proof.ML |
50263 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
Author: Steffen Juilf Smolka, TU Muenchen |
|
4 |
||
5 |
Basic data structures for representing and basic methods |
|
6 |
for dealing with Isar proof texts. |
|
7 |
*) |
|
8 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
9 |
signature SLEDGEHAMMER_ISAR_PROOF = |
50259 | 10 |
sig |
51239 | 11 |
type label = string * int |
54816
10d48c2a3e32
made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents:
54767
diff
changeset
|
12 |
type facts = label list * string list (* local and global facts *) |
50268 | 13 |
|
51178 | 14 |
datatype isar_qualifier = Show | Then |
50268 | 15 |
|
55218 | 16 |
datatype proof_method = |
55257 | 17 |
Metis_Method of string option * string option | |
18 |
Simp_Method | |
|
19 |
Simp_Size_Method | |
|
20 |
Auto_Method | |
|
21 |
Fastforce_Method | |
|
22 |
Force_Method | |
|
23 |
Arith_Method | |
|
24 |
Blast_Method | |
|
25 |
Meson_Method | |
|
26 |
Algebra_Method |
|
55218 | 27 |
|
52454 | 28 |
datatype isar_proof = |
54700 | 29 |
Proof of (string * typ) list * (label * term) list * isar_step list |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
30 |
and isar_step = |
50268 | 31 |
Let of term * term | |
54700 | 32 |
Prove of isar_qualifier list * (string * typ) list * label * term * isar_proof list * |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
33 |
(facts * proof_method list) |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
34 |
|
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
35 |
val no_label : label |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
36 |
val no_facts : facts |
50268 | 37 |
|
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
38 |
val label_ord : label * label -> order |
51998
f732a674db1b
renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents:
51239
diff
changeset
|
39 |
val string_of_label : label -> string |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
40 |
|
55222 | 41 |
val string_of_proof_method : proof_method -> string |
42 |
||
55260 | 43 |
val steps_of_isar_proof : isar_proof -> isar_step list |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
44 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
45 |
val label_of_isar_step : isar_step -> label option |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
46 |
val byline_of_isar_step : isar_step -> (facts * proof_method list) option |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
47 |
|
54765 | 48 |
val fold_isar_steps : (isar_step -> 'a -> 'a) -> isar_step list -> 'a -> 'a |
55212 | 49 |
val map_isar_steps : (isar_step -> isar_step) -> isar_proof -> isar_proof |
50 |
val add_isar_steps : isar_step list -> int -> int |
|
52454 | 51 |
|
55212 | 52 |
structure Canonical_Label_Tab : TABLE |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
53 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
54 |
val canonical_label_ord : (label * label) -> order |
55220 | 55 |
|
56 |
val chain_isar_proof : isar_proof -> isar_proof |
|
57 |
val kill_useless_labels_in_isar_proof : isar_proof -> isar_proof |
|
55213 | 58 |
val relabel_isar_proof_canonically : isar_proof -> isar_proof |
55220 | 59 |
val relabel_isar_proof_finally : isar_proof -> isar_proof |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
60 |
|
55257 | 61 |
val string_of_isar_proof : Proof.context -> int -> int -> |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
62 |
(label -> proof_method list -> string) -> isar_proof -> string |
54504 | 63 |
end; |
50259 | 64 |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
65 |
structure Sledgehammer_Isar_Proof : SLEDGEHAMMER_ISAR_PROOF = |
50259 | 66 |
struct |
67 |
||
55211 | 68 |
open ATP_Util |
69 |
open ATP_Proof |
|
70 |
open ATP_Problem_Generate |
|
71 |
open ATP_Proof_Reconstruct |
|
72 |
open Sledgehammer_Util |
|
73 |
open Sledgehammer_Reconstructor |
|
74 |
open Sledgehammer_Isar_Annotate |
|
75 |
||
50259 | 76 |
type label = string * int |
54534 | 77 |
type facts = label list * string list (* local and global facts *) |
50259 | 78 |
|
51178 | 79 |
datatype isar_qualifier = Show | Then |
50259 | 80 |
|
55218 | 81 |
datatype proof_method = |
55257 | 82 |
Metis_Method of string option * string option | |
83 |
Simp_Method | |
|
84 |
Simp_Size_Method | |
|
85 |
Auto_Method | |
|
86 |
Fastforce_Method | |
|
87 |
Force_Method | |
|
88 |
Arith_Method | |
|
89 |
Blast_Method | |
|
90 |
Meson_Method | |
|
91 |
Algebra_Method |
|
55218 | 92 |
|
52454 | 93 |
datatype isar_proof = |
54700 | 94 |
Proof of (string * typ) list * (label * term) list * isar_step list |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
95 |
and isar_step = |
50259 | 96 |
Let of term * term | |
54700 | 97 |
Prove of isar_qualifier list * (string * typ) list * label * term * isar_proof list * |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
98 |
(facts * proof_method list) |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
99 |
|
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
100 |
val no_label = ("", ~1) |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
101 |
val no_facts = ([],[]) |
50259 | 102 |
|
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
103 |
val label_ord = pairself swap #> prod_ord int_ord fast_string_ord |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
104 |
|
51998
f732a674db1b
renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents:
51239
diff
changeset
|
105 |
fun string_of_label (s, num) = s ^ string_of_int num |
50259 | 106 |
|
55222 | 107 |
fun string_of_proof_method meth = |
55218 | 108 |
(case meth of |
55257 | 109 |
Metis_Method (NONE, NONE) => "metis" |
110 |
| Metis_Method (type_enc_opt, lam_trans_opt) => |
|
111 |
"metis (" ^ commas (map_filter I [type_enc_opt, lam_trans_opt]) ^ ")" |
|
55218 | 112 |
| Simp_Method => "simp" |
55257 | 113 |
| Simp_Size_Method => "simp add: size_ne_size_imp_ne" |
55218 | 114 |
| Auto_Method => "auto" |
115 |
| Fastforce_Method => "fastforce" |
|
116 |
| Force_Method => "force" |
|
117 |
| Arith_Method => "arith" |
|
118 |
| Blast_Method => "blast" |
|
55219 | 119 |
| Meson_Method => "meson" |
120 |
| Algebra_Method => "algebra") |
|
55218 | 121 |
|
55260 | 122 |
fun steps_of_isar_proof (Proof (_, _, steps)) = steps |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
123 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
124 |
fun label_of_isar_step (Prove (_, _, l, _, _, _)) = SOME l |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
125 |
| label_of_isar_step _ = NONE |
51178 | 126 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
127 |
fun subproofs_of_isar_step (Prove (_, _, _, _, subproofs, _)) = SOME subproofs |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
128 |
| subproofs_of_isar_step _ = NONE |
52454 | 129 |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
130 |
fun byline_of_isar_step (Prove (_, _, _, _, _, byline)) = SOME byline |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
131 |
| byline_of_isar_step _ = NONE |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
132 |
|
55212 | 133 |
fun fold_isar_step f step = |
55260 | 134 |
fold (steps_of_isar_proof #> fold_isar_steps f) (these (subproofs_of_isar_step step)) #> f step |
55212 | 135 |
and fold_isar_steps f = fold (fold_isar_step f) |
52454 | 136 |
|
54765 | 137 |
fun map_isar_steps f = |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
138 |
let |
55212 | 139 |
fun map_proof (Proof (fix, assms, steps)) = Proof (fix, assms, map map_step steps) |
140 |
and map_step (step as Let _) = f step |
|
141 |
| map_step (Prove (qs, xs, l, t, subproofs, by)) = |
|
142 |
f (Prove (qs, xs, l, t, map map_proof subproofs, by)) |
|
143 |
in map_proof end |
|
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52590
diff
changeset
|
144 |
|
55212 | 145 |
val add_isar_steps = fold_isar_steps (fn Prove _ => Integer.add 1 | _ => I) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
146 |
|
55211 | 147 |
(* canonical proof labels: 1, 2, 3, ... in post traversal order *) |
52557 | 148 |
fun canonical_label_ord (((_, i1), (_, i2)) : label * label) = int_ord (i1, i2) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
149 |
|
55212 | 150 |
structure Canonical_Label_Tab = Table( |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
151 |
type key = label |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
152 |
val ord = canonical_label_ord) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
153 |
|
55220 | 154 |
fun chain_qs_lfs NONE lfs = ([], lfs) |
155 |
| chain_qs_lfs (SOME l0) lfs = |
|
156 |
if member (op =) lfs l0 then ([Then], lfs |> remove (op =) l0) else ([], lfs) |
|
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
157 |
fun chain_isar_step lbl (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), meths))) = |
55220 | 158 |
let val (qs', lfs) = chain_qs_lfs lbl lfs in |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
159 |
Prove (qs' @ qs, xs, l, t, map chain_isar_proof subproofs, ((lfs, gfs), meths)) |
55220 | 160 |
end |
161 |
| chain_isar_step _ step = step |
|
162 |
and chain_isar_steps _ [] = [] |
|
163 |
| chain_isar_steps (prev as SOME _) (i :: is) = |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
164 |
chain_isar_step prev i :: chain_isar_steps (label_of_isar_step i) is |
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
165 |
| chain_isar_steps _ (i :: is) = i :: chain_isar_steps (label_of_isar_step i) is |
55220 | 166 |
and chain_isar_proof (Proof (fix, assms, steps)) = |
167 |
Proof (fix, assms, chain_isar_steps (try (List.last #> fst) assms) steps) |
|
168 |
||
169 |
fun kill_useless_labels_in_isar_proof proof = |
|
170 |
let |
|
171 |
val used_ls = |
|
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55222
diff
changeset
|
172 |
fold_isar_steps (byline_of_isar_step #> (fn SOME ((ls, _), _) => union (op =) ls | _ => I)) |
55260 | 173 |
(steps_of_isar_proof proof) [] |
55220 | 174 |
|
175 |
fun kill_label l = if member (op =) used_ls l then l else no_label |
|
176 |
||
177 |
fun kill_step (Prove (qs, xs, l, t, subproofs, by)) = |
|
178 |
Prove (qs, xs, kill_label l, t, map kill_proof subproofs, by) |
|
179 |
| kill_step step = step |
|
180 |
and kill_proof (Proof (fix, assms, steps)) = |
|
181 |
Proof (fix, map (apfst kill_label) assms, map kill_step steps) |
|
182 |
in |
|
183 |
kill_proof proof |
|
184 |
end |
|
185 |
||
55213 | 186 |
fun relabel_isar_proof_canonically proof = |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
187 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
188 |
fun next_label l (next, subst) = |
54534 | 189 |
let val l' = ("", next) in (l', (next + 1, (l, l') :: subst)) end |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
190 |
|
54700 | 191 |
fun do_byline by (_, subst) = apfst (apfst (map (AList.lookup (op =) subst #> the))) by |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
192 |
handle Option.Option => |
55213 | 193 |
raise Fail "Sledgehammer_Isar_Proof: relabel_isar_proof_canonically" |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
194 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
195 |
fun do_assm (l, t) state = |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
196 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
197 |
val (l, state) = next_label l state |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
198 |
in ((l, t), state) end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
199 |
|
54700 | 200 |
fun do_proof (Proof (fix, assms, steps)) state = |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
201 |
let |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
202 |
val (assms, state) = fold_map do_assm assms state |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
203 |
val (steps, state) = fold_map do_step steps state |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
204 |
in |
54700 | 205 |
(Proof (fix, assms, steps), state) |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
206 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
207 |
|
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
208 |
and do_step (step as Let _) state = (step, state) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
209 |
| do_step (Prove (qs, fix, l, t, subproofs, by)) state= |
54534 | 210 |
let |
211 |
val by = do_byline by state |
|
212 |
val (subproofs, state) = fold_map do_proof subproofs state |
|
213 |
val (l, state) = next_label l state |
|
214 |
in |
|
215 |
(Prove (qs, fix, l, t, subproofs, by), state) |
|
216 |
end |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
217 |
in |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
218 |
fst (do_proof proof (0, [])) |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
219 |
end |
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
220 |
|
55220 | 221 |
val assume_prefix = "a" |
222 |
val have_prefix = "f" |
|
223 |
||
224 |
val relabel_isar_proof_finally = |
|
225 |
let |
|
226 |
fun fresh_label depth prefix (accum as (l, subst, next)) = |
|
227 |
if l = no_label then |
|
228 |
accum |
|
229 |
else |
|
230 |
let val l' = (replicate_string (depth + 1) prefix, next) in |
|
231 |
(l', (l, l') :: subst, next + 1) |
|
232 |
end |
|
233 |
||
234 |
fun relabel_facts subst = apfst (maps (the_list o AList.lookup (op =) subst)) |
|
235 |
||
236 |
fun relabel_assm depth (l, t) (subst, next) = |
|
237 |
let val (l, subst, next) = (l, subst, next) |> fresh_label depth assume_prefix in |
|
238 |
((l, t), (subst, next)) |
|
239 |
end |
|
240 |
||
241 |
fun relabel_assms subst depth assms = fold_map (relabel_assm depth) assms (subst, 1) ||> fst |
|
242 |
||
243 |
fun relabel_steps _ _ _ [] = [] |
|
244 |
| relabel_steps subst depth next (Prove (qs, xs, l, t, sub, by) :: steps) = |
|
245 |
let |
|
246 |
val (l, subst, next) = (l, subst, next) |> fresh_label depth have_prefix |
|
247 |
val sub = relabel_proofs subst depth sub |
|
248 |
val by = apfst (relabel_facts subst) by |
|
249 |
in |
|
250 |
Prove (qs, xs, l, t, sub, by) :: relabel_steps subst depth next steps |
|
251 |
end |
|
252 |
| relabel_steps subst depth next (step :: steps) = |
|
253 |
step :: relabel_steps subst depth next steps |
|
254 |
and relabel_proof subst depth (Proof (fix, assms, steps)) = |
|
255 |
let val (assms, subst) = relabel_assms subst depth assms in |
|
256 |
Proof (fix, assms, relabel_steps subst depth 1 steps) |
|
257 |
end |
|
258 |
and relabel_proofs subst depth = map (relabel_proof subst (depth + 1)) |
|
259 |
in |
|
260 |
relabel_proof [] 0 |
|
261 |
end |
|
262 |
||
55211 | 263 |
val indent_size = 2 |
264 |
||
55257 | 265 |
fun string_of_isar_proof ctxt i n comment_of proof = |
55211 | 266 |
let |
267 |
(* Make sure only type constraints inserted by the type annotation code are printed. *) |
|
268 |
val ctxt = |
|
269 |
ctxt |> Config.put show_markup false |
|
270 |
|> Config.put Printer.show_type_emphasis false |
|
271 |
|> Config.put show_types false |
|
272 |
|> Config.put show_sorts false |
|
273 |
|> Config.put show_consts false |
|
274 |
||
275 |
val register_fixes = map Free #> fold Variable.auto_fixes |
|
276 |
||
55216 | 277 |
fun add_str s' = apfst (suffix s') |
55211 | 278 |
|
279 |
fun of_indent ind = replicate_string (ind * indent_size) " " |
|
280 |
fun of_moreover ind = of_indent ind ^ "moreover\n" |
|
281 |
fun of_label l = if l = no_label then "" else string_of_label l ^ ": " |
|
282 |
||
283 |
fun of_obtain qs nr = |
|
284 |
(if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then "ultimately " |
|
285 |
else if nr = 1 orelse member (op =) qs Then then "then " |
|
286 |
else "") ^ "obtain" |
|
287 |
||
288 |
fun of_show_have qs = if member (op =) qs Show then "show" else "have" |
|
289 |
fun of_thus_hence qs = if member (op =) qs Show then "thus" else "hence" |
|
290 |
||
291 |
fun of_have qs nr = |
|
292 |
if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then "ultimately " ^ of_show_have qs |
|
293 |
else if nr = 1 orelse member (op =) qs Then then of_thus_hence qs |
|
294 |
else of_show_have qs |
|
295 |
||
296 |
fun add_term term (s, ctxt) = |
|
297 |
(s ^ (term |
|
298 |
|> singleton (Syntax.uncheck_terms ctxt) |
|
55213 | 299 |
|> annotate_types_in_term ctxt |
55211 | 300 |
|> with_vanilla_print_mode (Syntax.unparse_term ctxt #> Pretty.string_of) |
301 |
|> simplify_spaces |
|
302 |
|> maybe_quote), |
|
303 |
ctxt |> Variable.auto_fixes term) |
|
304 |
||
305 |
fun with_facts none _ [] [] = none |
|
306 |
| with_facts _ some ls ss = some (space_implode " " (map string_of_label ls @ ss)) |
|
307 |
||
308 |
val using_facts = with_facts "" (enclose "using " " ") |
|
55257 | 309 |
|
310 |
fun maybe_paren s = s |> String.isSubstring " " s ? enclose "(" ")" |
|
311 |
fun by_facts meth ls ss = |
|
312 |
"by " ^ with_facts (maybe_paren meth) (enclose ("(" ^ meth ^ " ") ")") ls ss |
|
313 |
||
314 |
fun is_direct_method (Metis_Method _) = true |
|
315 |
| is_direct_method Meson_Method = true |
|
316 |
| is_direct_method _ = false |
|
55211 | 317 |
|
318 |
(* Local facts are always passed via "using", which affects "meson" and "metis". This is |
|
319 |
arguably stylistically superior, because it emphasises the structure of the proof. It is also |
|
320 |
more robust w.r.t. preplay: Preplay is performed before chaining of local facts with "hence" |
|
321 |
and "thus" is introduced. See also "tac_of_method" in "Sledgehammer_Isar_Preplay". *) |
|
55257 | 322 |
fun of_method ls ss meth = |
323 |
let val direct = is_direct_method meth in |
|
324 |
using_facts ls (if direct then [] else ss) ^ |
|
325 |
by_facts (string_of_proof_method meth) [] (if direct then ss else []) |
|
326 |
end |
|
55211 | 327 |
|
328 |
fun of_free (s, T) = |
|
329 |
maybe_quote s ^ " :: " ^ |
|
330 |
maybe_quote (simplify_spaces (with_vanilla_print_mode (Syntax.string_of_typ ctxt) T)) |
|
331 |
||
332 |
fun add_frees xs (s, ctxt) = |
|
333 |
(s ^ space_implode " and " (map of_free xs), ctxt |> register_fixes xs) |
|
334 |
||
335 |
fun add_fix _ [] = I |
|
336 |
| add_fix ind xs = |
|
55216 | 337 |
add_str (of_indent ind ^ "fix ") |
55211 | 338 |
#> add_frees xs |
55216 | 339 |
#> add_str "\n" |
55211 | 340 |
|
341 |
fun add_assm ind (l, t) = |
|
55216 | 342 |
add_str (of_indent ind ^ "assume " ^ of_label l) |
55211 | 343 |
#> add_term t |
55216 | 344 |
#> add_str "\n" |
55211 | 345 |
|
346 |
fun add_assms ind assms = fold (add_assm ind) assms |
|
347 |
||
348 |
fun of_subproof ind ctxt proof = |
|
349 |
let |
|
350 |
val ind = ind + 1 |
|
351 |
val s = of_proof ind ctxt proof |
|
352 |
val prefix = "{ " |
|
353 |
val suffix = " }" |
|
354 |
in |
|
355 |
replicate_string (ind * indent_size - size prefix) " " ^ prefix ^ |
|
356 |
String.extract (s, ind * indent_size, SOME (size s - ind * indent_size - 1)) ^ |
|
357 |
suffix ^ "\n" |
|
358 |
end |
|
359 |
and of_subproofs _ _ _ [] = "" |
|
360 |
| of_subproofs ind ctxt qs subproofs = |
|
361 |
(if member (op =) qs Then then of_moreover ind else "") ^ |
|
362 |
space_implode (of_moreover ind) (map (of_subproof ind ctxt) subproofs) |
|
363 |
and add_step_pre ind qs subproofs (s, ctxt) = |
|
364 |
(s ^ of_subproofs ind ctxt qs subproofs ^ of_indent ind, ctxt) |
|
365 |
and add_step ind (Let (t1, t2)) = |
|
55216 | 366 |
add_str (of_indent ind ^ "let ") |
367 |
#> add_term t1 #> add_str " = " #> add_term t2 |
|
368 |
#> add_str "\n" |
|
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
369 |
| add_step ind (Prove (qs, xs, l, t, subproofs, ((ls, ss), meths as meth :: _))) = |
55211 | 370 |
add_step_pre ind qs subproofs |
371 |
#> (case xs of |
|
55216 | 372 |
[] => add_str (of_have qs (length subproofs) ^ " ") |
373 |
| _ => |
|
374 |
add_str (of_obtain qs (length subproofs) ^ " ") #> add_frees xs #> add_str " where ") |
|
55217 | 375 |
#> add_str (of_label l) |
376 |
#> add_term t |
|
377 |
#> add_str (" " ^ |
|
55218 | 378 |
of_method (sort_distinct label_ord ls) (sort_distinct string_ord ss) meth ^ |
55244
12e1a5d8ee48
simplified data structure -- eliminated distinction between 'first-class' and 'second-class' proof methods
blanchet
parents:
55223
diff
changeset
|
379 |
(case comment_of l meths of |
55222 | 380 |
"" => "" |
381 |
| comment => " (* " ^ comment ^ " *)") ^ "\n") |
|
55211 | 382 |
and add_steps ind = fold (add_step ind) |
383 |
and of_proof ind ctxt (Proof (xs, assms, steps)) = |
|
384 |
("", ctxt) |> add_fix ind xs |> add_assms ind assms |> add_steps ind steps |> fst |
|
385 |
in |
|
386 |
(* One-step Metis proofs are pointless; better use the one-liner directly. *) |
|
387 |
(case proof of |
|
388 |
Proof ([], [], []) => "" (* degenerate case: the conjecture is "True" with Z3 *) |
|
55257 | 389 |
| Proof ([], [], [Prove (_, [], _, _, [], (_, Metis_Method _ :: _))]) => "" |
55211 | 390 |
| _ => |
391 |
(if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^ |
|
392 |
of_indent 0 ^ "proof -\n" ^ of_proof 1 ctxt proof ^ |
|
393 |
of_indent 0 ^ (if n <> 1 then "next" else "qed")) |
|
394 |
end |
|
395 |
||
54504 | 396 |
end; |