author | blanchet |
Thu, 18 Oct 2012 13:46:24 +0200 | |
changeset 49916 | 412346127bfa |
parent 49915 | e88a864fa35c |
child 49917 | 4e17a6a0ef4f |
permissions | -rw-r--r-- |
49883 | 1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_reconstruct.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Author: Steffen Juilf Smolka, TU Muenchen |
|
4 |
||
49914 | 5 |
Isar proof reconstruction from ATP proofs. |
49883 | 6 |
*) |
7 |
||
8 |
signature SLEDGEHAMMER_PROOF_RECONSTRUCT = |
|
9 |
sig |
|
49914 | 10 |
type 'a proof = 'a ATP_Proof.proof |
11 |
type stature = ATP_Problem_Generate.stature |
|
12 |
||
13 |
datatype reconstructor = |
|
14 |
Metis of string * string | |
|
15 |
SMT |
|
16 |
||
17 |
datatype play = |
|
18 |
Played of reconstructor * Time.time | |
|
19 |
Trust_Playable of reconstructor * Time.time option | |
|
20 |
Failed_to_Play of reconstructor |
|
21 |
||
22 |
type minimize_command = string list -> string |
|
23 |
type one_line_params = |
|
24 |
play * string * (string * stature) list * minimize_command * int * int |
|
25 |
type isar_params = |
|
26 |
bool * int * string Symtab.table * (string * stature) list vector |
|
27 |
* int Symtab.table * string proof * thm |
|
28 |
||
29 |
val smtN : string |
|
30 |
val string_for_reconstructor : reconstructor -> string |
|
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
31 |
val thms_of_name : Proof.context -> string -> thm list |
49914 | 32 |
val lam_trans_from_atp_proof : string proof -> string -> string |
33 |
val is_typed_helper_used_in_atp_proof : string proof -> bool |
|
34 |
val used_facts_in_atp_proof : |
|
35 |
Proof.context -> (string * stature) list vector -> string proof -> |
|
36 |
(string * stature) list |
|
37 |
val used_facts_in_unsound_atp_proof : |
|
38 |
Proof.context -> (string * stature) list vector -> 'a proof -> |
|
39 |
string list option |
|
40 |
val one_line_proof_text : int -> one_line_params -> string |
|
49883 | 41 |
val isar_proof_text : |
49913 | 42 |
Proof.context -> bool -> isar_params -> one_line_params -> string |
49883 | 43 |
val proof_text : |
49913 | 44 |
Proof.context -> bool -> isar_params -> int -> one_line_params -> string |
49883 | 45 |
end; |
46 |
||
47 |
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_PROOF_RECONSTRUCT = |
|
48 |
struct |
|
49 |
||
50 |
open ATP_Util |
|
49914 | 51 |
open ATP_Problem |
49883 | 52 |
open ATP_Proof |
53 |
open ATP_Problem_Generate |
|
54 |
open ATP_Proof_Reconstruct |
|
49914 | 55 |
|
56 |
structure String_Redirect = ATP_Proof_Redirect( |
|
57 |
type key = step_name |
|
58 |
val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s') |
|
59 |
val string_of = fst) |
|
60 |
||
49883 | 61 |
open String_Redirect |
62 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
63 |
|
49914 | 64 |
(** reconstructors **) |
65 |
||
66 |
datatype reconstructor = |
|
67 |
Metis of string * string | |
|
68 |
SMT |
|
69 |
||
70 |
datatype play = |
|
71 |
Played of reconstructor * Time.time | |
|
72 |
Trust_Playable of reconstructor * Time.time option | |
|
73 |
Failed_to_Play of reconstructor |
|
74 |
||
75 |
val smtN = "smt" |
|
76 |
||
77 |
fun string_for_reconstructor (Metis (type_enc, lam_trans)) = |
|
78 |
metis_call type_enc lam_trans |
|
79 |
| string_for_reconstructor SMT = smtN |
|
80 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
81 |
fun thms_of_name ctxt name = |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
82 |
let |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
83 |
val lex = Keyword.get_lexicons |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
84 |
val get = maps (Proof_Context.get_fact ctxt o fst) |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
85 |
in |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
86 |
Source.of_string name |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
87 |
|> Symbol.source |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
88 |
|> Token.source {do_recover=SOME false} lex Position.start |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
89 |
|> Token.source_proper |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
90 |
|> Source.source Token.stopper (Parse_Spec.xthms1 >> get) NONE |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
91 |
|> Source.exhaust |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
92 |
end |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
93 |
|
49914 | 94 |
|
95 |
(** fact extraction from ATP proofs **) |
|
96 |
||
97 |
fun find_first_in_list_vector vec key = |
|
98 |
Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key |
|
99 |
| (_, value) => value) NONE vec |
|
100 |
||
101 |
val unprefix_fact_number = space_implode "_" o tl o space_explode "_" |
|
102 |
||
103 |
fun resolve_one_named_fact fact_names s = |
|
104 |
case try (unprefix fact_prefix) s of |
|
105 |
SOME s' => |
|
106 |
let val s' = s' |> unprefix_fact_number |> unascii_of in |
|
107 |
s' |> find_first_in_list_vector fact_names |> Option.map (pair s') |
|
108 |
end |
|
109 |
| NONE => NONE |
|
110 |
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names) |
|
111 |
fun is_fact fact_names = not o null o resolve_fact fact_names |
|
112 |
||
113 |
fun resolve_one_named_conjecture s = |
|
114 |
case try (unprefix conjecture_prefix) s of |
|
115 |
SOME s' => Int.fromString s' |
|
116 |
| NONE => NONE |
|
117 |
||
118 |
val resolve_conjecture = map_filter resolve_one_named_conjecture |
|
119 |
val is_conjecture = not o null o resolve_conjecture |
|
120 |
||
121 |
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix |
|
122 |
||
123 |
(* overapproximation (good enough) *) |
|
124 |
fun is_lam_lifted s = |
|
125 |
String.isPrefix fact_prefix s andalso |
|
126 |
String.isSubstring ascii_of_lam_fact_prefix s |
|
127 |
||
128 |
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix) |
|
129 |
||
130 |
fun is_axiom_used_in_proof pred = |
|
131 |
exists (fn Inference_Step ((_, ss), _, _, []) => exists pred ss | _ => false) |
|
132 |
||
133 |
fun lam_trans_from_atp_proof atp_proof default = |
|
134 |
case (is_axiom_used_in_proof is_combinator_def atp_proof, |
|
135 |
is_axiom_used_in_proof is_lam_lifted atp_proof) of |
|
136 |
(false, false) => default |
|
137 |
| (false, true) => liftingN |
|
138 |
(* | (true, true) => combs_and_liftingN -- not supported by "metis" *) |
|
139 |
| (true, _) => combsN |
|
140 |
||
141 |
val is_typed_helper_name = |
|
142 |
String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix |
|
143 |
fun is_typed_helper_used_in_atp_proof atp_proof = |
|
144 |
is_axiom_used_in_proof is_typed_helper_name atp_proof |
|
145 |
||
146 |
fun add_non_rec_defs fact_names accum = |
|
147 |
Vector.foldl (fn (facts, facts') => |
|
148 |
union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts) |
|
149 |
facts') |
|
150 |
accum fact_names |
|
151 |
||
152 |
val isa_ext = Thm.get_name_hint @{thm ext} |
|
153 |
val isa_short_ext = Long_Name.base_name isa_ext |
|
154 |
||
155 |
fun ext_name ctxt = |
|
156 |
if Thm.eq_thm_prop (@{thm ext}, |
|
157 |
singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then |
|
158 |
isa_short_ext |
|
159 |
else |
|
160 |
isa_ext |
|
161 |
||
162 |
val leo2_ext = "extcnf_equal_neg" |
|
163 |
val leo2_unfold_def = "unfold_def" |
|
164 |
||
165 |
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, rule, deps)) = |
|
166 |
(if rule = leo2_ext then |
|
167 |
insert (op =) (ext_name ctxt, (Global, General)) |
|
168 |
else if rule = leo2_unfold_def then |
|
169 |
(* LEO 1.3.3 does not record definitions properly, leading to missing |
|
170 |
dependencies in the TSTP proof. Remove the next line once this is |
|
171 |
fixed. *) |
|
172 |
add_non_rec_defs fact_names |
|
173 |
else if rule = satallax_coreN then |
|
174 |
(fn [] => |
|
175 |
(* Satallax doesn't include definitions in its unsatisfiable cores, |
|
176 |
so we assume the worst and include them all here. *) |
|
177 |
[(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names |
|
178 |
| facts => facts) |
|
179 |
else |
|
180 |
I) |
|
181 |
#> (if null deps then union (op =) (resolve_fact fact_names ss) |
|
182 |
else I) |
|
183 |
| add_fact _ _ _ = I |
|
184 |
||
185 |
fun used_facts_in_atp_proof ctxt fact_names atp_proof = |
|
186 |
if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names |
|
187 |
else fold (add_fact ctxt fact_names) atp_proof [] |
|
188 |
||
189 |
fun used_facts_in_unsound_atp_proof _ _ [] = NONE |
|
190 |
| used_facts_in_unsound_atp_proof ctxt fact_names atp_proof = |
|
191 |
let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in |
|
192 |
if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso |
|
193 |
not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then |
|
194 |
SOME (map fst used_facts) |
|
195 |
else |
|
196 |
NONE |
|
197 |
end |
|
198 |
||
199 |
||
200 |
(** one-liner reconstructor proofs **) |
|
201 |
||
202 |
fun string_for_label (s, num) = s ^ string_of_int num |
|
203 |
||
204 |
fun show_time NONE = "" |
|
205 |
| show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")" |
|
206 |
||
207 |
fun unusing_chained_facts _ 0 = "" |
|
208 |
| unusing_chained_facts used_chaineds num_chained = |
|
209 |
if length used_chaineds = num_chained then "" |
|
210 |
else if null used_chaineds then "(* using no facts *) " |
|
211 |
else "(* using only " ^ space_implode " " used_chaineds ^ " *) " |
|
212 |
||
213 |
fun apply_on_subgoal _ 1 = "by " |
|
214 |
| apply_on_subgoal 1 _ = "apply " |
|
215 |
| apply_on_subgoal i n = |
|
216 |
"prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n |
|
217 |
||
218 |
fun using_labels [] = "" |
|
219 |
| using_labels ls = |
|
220 |
"using " ^ space_implode " " (map string_for_label ls) ^ " " |
|
221 |
||
222 |
fun command_call name [] = |
|
223 |
name |> not (Lexicon.is_identifier name) ? enclose "(" ")" |
|
224 |
| command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")" |
|
225 |
||
226 |
fun reconstructor_command reconstr i n used_chaineds num_chained (ls, ss) = |
|
227 |
unusing_chained_facts used_chaineds num_chained ^ |
|
228 |
using_labels ls ^ apply_on_subgoal i n ^ |
|
229 |
command_call (string_for_reconstructor reconstr) ss |
|
230 |
||
231 |
fun try_command_line banner time command = |
|
232 |
banner ^ ": " ^ Markup.markup Isabelle_Markup.sendback command ^ |
|
233 |
show_time time ^ "." |
|
234 |
||
235 |
fun minimize_line _ [] = "" |
|
236 |
| minimize_line minimize_command ss = |
|
237 |
case minimize_command ss of |
|
238 |
"" => "" |
|
239 |
| command => |
|
240 |
"\nTo minimize: " ^ Markup.markup Isabelle_Markup.sendback command ^ "." |
|
241 |
||
242 |
fun split_used_facts facts = |
|
243 |
facts |> List.partition (fn (_, (sc, _)) => sc = Chained) |
|
244 |
|> pairself (sort_distinct (string_ord o pairself fst)) |
|
245 |
||
246 |
type minimize_command = string list -> string |
|
247 |
type one_line_params = |
|
248 |
play * string * (string * stature) list * minimize_command * int * int |
|
249 |
||
250 |
fun one_line_proof_text num_chained |
|
251 |
(preplay, banner, used_facts, minimize_command, subgoal, |
|
252 |
subgoal_count) = |
|
253 |
let |
|
254 |
val (chained, extra) = split_used_facts used_facts |
|
255 |
val (failed, reconstr, ext_time) = |
|
256 |
case preplay of |
|
257 |
Played (reconstr, time) => (false, reconstr, (SOME (false, time))) |
|
258 |
| Trust_Playable (reconstr, time) => |
|
259 |
(false, reconstr, |
|
260 |
case time of |
|
261 |
NONE => NONE |
|
262 |
| SOME time => |
|
263 |
if time = Time.zeroTime then NONE else SOME (true, time)) |
|
264 |
| Failed_to_Play reconstr => (true, reconstr, NONE) |
|
265 |
val try_line = |
|
266 |
([], map fst extra) |
|
267 |
|> reconstructor_command reconstr subgoal subgoal_count (map fst chained) |
|
268 |
num_chained |
|
269 |
|> (if failed then |
|
270 |
enclose "One-line proof reconstruction failed: " |
|
271 |
".\n(Invoking \"sledgehammer\" with \"[strict]\" might \ |
|
272 |
\solve this.)" |
|
273 |
else |
|
274 |
try_command_line banner ext_time) |
|
275 |
in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end |
|
276 |
||
277 |
||
278 |
(** Isar proof construction and manipulation **) |
|
279 |
||
280 |
type label = string * int |
|
281 |
type facts = label list * string list |
|
282 |
||
283 |
datatype isar_qualifier = Show | Then | Moreover | Ultimately |
|
284 |
||
285 |
datatype isar_step = |
|
286 |
Fix of (string * typ) list | |
|
287 |
Let of term * term | |
|
288 |
Assume of label * term | |
|
289 |
Prove of isar_qualifier list * label * term * byline |
|
290 |
and byline = |
|
291 |
By_Metis of facts | |
|
292 |
Case_Split of isar_step list list * facts |
|
293 |
||
294 |
val assum_prefix = "a" |
|
295 |
val have_prefix = "f" |
|
296 |
val raw_prefix = "x" |
|
297 |
||
298 |
fun raw_label_for_name (num, ss) = |
|
299 |
case resolve_conjecture ss of |
|
300 |
[j] => (conjecture_prefix, j) |
|
301 |
| _ => (raw_prefix ^ ascii_of num, 0) |
|
302 |
||
303 |
fun add_fact_from_dependency fact_names (name as (_, ss)) = |
|
304 |
if is_fact fact_names ss then |
|
305 |
apsnd (union (op =) (map fst (resolve_fact fact_names ss))) |
|
306 |
else |
|
307 |
apfst (insert (op =) (raw_label_for_name name)) |
|
308 |
||
309 |
fun repair_name "$true" = "c_True" |
|
310 |
| repair_name "$false" = "c_False" |
|
311 |
| repair_name "$$e" = tptp_equal (* seen in Vampire proofs *) |
|
312 |
| repair_name s = |
|
313 |
if is_tptp_equal s orelse |
|
314 |
(* seen in Vampire proofs *) |
|
315 |
(String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then |
|
316 |
tptp_equal |
|
317 |
else |
|
318 |
s |
|
319 |
||
320 |
fun unvarify_term (Var ((s, 0), T)) = Free (s, T) |
|
321 |
| unvarify_term t = raise TERM ("unvarify_term: non-Var", [t]) |
|
322 |
||
323 |
fun infer_formula_types ctxt = |
|
324 |
Type.constraint HOLogic.boolT |
|
325 |
#> Syntax.check_term |
|
326 |
(Proof_Context.set_mode Proof_Context.mode_schematic ctxt) |
|
327 |
||
328 |
val combinator_table = |
|
329 |
[(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}), |
|
330 |
(@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}), |
|
331 |
(@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}), |
|
332 |
(@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}), |
|
333 |
(@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})] |
|
334 |
||
335 |
fun uncombine_term thy = |
|
336 |
let |
|
337 |
fun aux (t1 $ t2) = betapply (pairself aux (t1, t2)) |
|
338 |
| aux (Abs (s, T, t')) = Abs (s, T, aux t') |
|
339 |
| aux (t as Const (x as (s, _))) = |
|
340 |
(case AList.lookup (op =) combinator_table s of |
|
341 |
SOME thm => thm |> prop_of |> specialize_type thy x |
|
342 |
|> Logic.dest_equals |> snd |
|
343 |
| NONE => t) |
|
344 |
| aux t = t |
|
345 |
in aux end |
|
346 |
||
347 |
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt = |
|
348 |
let |
|
349 |
val thy = Proof_Context.theory_of ctxt |
|
350 |
val t1 = prop_from_atp ctxt true sym_tab phi1 |
|
351 |
val vars = snd (strip_comb t1) |
|
352 |
val frees = map unvarify_term vars |
|
353 |
val unvarify_args = subst_atomic (vars ~~ frees) |
|
354 |
val t2 = prop_from_atp ctxt true sym_tab phi2 |
|
355 |
val (t1, t2) = |
|
356 |
HOLogic.eq_const HOLogic.typeT $ t1 $ t2 |
|
357 |
|> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt |
|
358 |
|> HOLogic.dest_eq |
|
359 |
in |
|
360 |
(Definition_Step (name, t1, t2), |
|
361 |
fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt) |
|
362 |
end |
|
363 |
| decode_line sym_tab (Inference_Step (name, u, rule, deps)) ctxt = |
|
364 |
let |
|
365 |
val thy = Proof_Context.theory_of ctxt |
|
366 |
val t = u |> prop_from_atp ctxt true sym_tab |
|
367 |
|> uncombine_term thy |> infer_formula_types ctxt |
|
368 |
in |
|
369 |
(Inference_Step (name, t, rule, deps), |
|
370 |
fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt) |
|
371 |
end |
|
372 |
fun decode_lines ctxt sym_tab lines = |
|
373 |
fst (fold_map (decode_line sym_tab) lines ctxt) |
|
374 |
||
375 |
fun replace_one_dependency (old, new) dep = |
|
376 |
if is_same_atp_step dep old then new else [dep] |
|
377 |
fun replace_dependencies_in_line _ (line as Definition_Step _) = line |
|
378 |
| replace_dependencies_in_line p (Inference_Step (name, t, rule, deps)) = |
|
379 |
Inference_Step (name, t, rule, |
|
380 |
fold (union (op =) o replace_one_dependency p) deps []) |
|
381 |
||
382 |
(* No "real" literals means only type information (tfree_tcs, clsrel, or |
|
383 |
clsarity). *) |
|
384 |
fun is_only_type_information t = t aconv @{term True} |
|
385 |
||
386 |
fun is_same_inference _ (Definition_Step _) = false |
|
387 |
| is_same_inference t (Inference_Step (_, t', _, _)) = t aconv t' |
|
388 |
||
389 |
(* Discard facts; consolidate adjacent lines that prove the same formula, since |
|
390 |
they differ only in type information.*) |
|
391 |
fun add_line _ (line as Definition_Step _) lines = line :: lines |
|
392 |
| add_line fact_names (Inference_Step (name as (_, ss), t, rule, [])) lines = |
|
393 |
(* No dependencies: fact, conjecture, or (for Vampire) internal facts or |
|
394 |
definitions. *) |
|
395 |
if is_fact fact_names ss then |
|
396 |
(* Facts are not proof lines. *) |
|
397 |
if is_only_type_information t then |
|
398 |
map (replace_dependencies_in_line (name, [])) lines |
|
399 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
|
400 |
else case take_prefix (not o is_same_inference t) lines of |
|
401 |
(_, []) => lines (* no repetition of proof line *) |
|
402 |
| (pre, Inference_Step (name', _, _, _) :: post) => |
|
403 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
|
404 |
| _ => raise Fail "unexpected inference" |
|
405 |
else if is_conjecture ss then |
|
406 |
Inference_Step (name, t, rule, []) :: lines |
|
407 |
else |
|
408 |
map (replace_dependencies_in_line (name, [])) lines |
|
409 |
| add_line _ (Inference_Step (name, t, rule, deps)) lines = |
|
410 |
(* Type information will be deleted later; skip repetition test. *) |
|
411 |
if is_only_type_information t then |
|
412 |
Inference_Step (name, t, rule, deps) :: lines |
|
413 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
|
414 |
else case take_prefix (not o is_same_inference t) lines of |
|
415 |
(* FIXME: Doesn't this code risk conflating proofs involving different |
|
416 |
types? *) |
|
417 |
(_, []) => Inference_Step (name, t, rule, deps) :: lines |
|
418 |
| (pre, Inference_Step (name', t', rule, _) :: post) => |
|
419 |
Inference_Step (name, t', rule, deps) :: |
|
420 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
|
421 |
| _ => raise Fail "unexpected inference" |
|
422 |
||
423 |
val waldmeister_conjecture_num = "1.0.0.0" |
|
424 |
||
425 |
val repair_waldmeister_endgame = |
|
426 |
let |
|
427 |
fun do_tail (Inference_Step (name, t, rule, deps)) = |
|
428 |
Inference_Step (name, s_not t, rule, deps) |
|
429 |
| do_tail line = line |
|
430 |
fun do_body [] = [] |
|
431 |
| do_body ((line as Inference_Step ((num, _), _, _, _)) :: lines) = |
|
432 |
if num = waldmeister_conjecture_num then map do_tail (line :: lines) |
|
433 |
else line :: do_body lines |
|
434 |
| do_body (line :: lines) = line :: do_body lines |
|
435 |
in do_body end |
|
436 |
||
437 |
(* Recursively delete empty lines (type information) from the proof. *) |
|
438 |
fun add_nontrivial_line (line as Inference_Step (name, t, _, [])) lines = |
|
439 |
if is_only_type_information t then delete_dependency name lines |
|
440 |
else line :: lines |
|
441 |
| add_nontrivial_line line lines = line :: lines |
|
442 |
and delete_dependency name lines = |
|
443 |
fold_rev add_nontrivial_line |
|
444 |
(map (replace_dependencies_in_line (name, [])) lines) [] |
|
445 |
||
446 |
(* ATPs sometimes reuse free variable names in the strangest ways. Removing |
|
447 |
offending lines often does the trick. *) |
|
448 |
fun is_bad_free frees (Free x) = not (member (op =) frees x) |
|
449 |
| is_bad_free _ _ = false |
|
450 |
||
451 |
fun add_desired_line _ _ _ (line as Definition_Step (name, _, _)) (j, lines) = |
|
452 |
(j, line :: map (replace_dependencies_in_line (name, [])) lines) |
|
453 |
| add_desired_line isar_shrink_factor fact_names frees |
|
454 |
(Inference_Step (name as (_, ss), t, rule, deps)) (j, lines) = |
|
455 |
(j + 1, |
|
456 |
if is_fact fact_names ss orelse |
|
457 |
is_conjecture ss orelse |
|
458 |
(* the last line must be kept *) |
|
459 |
j = 0 orelse |
|
460 |
(not (is_only_type_information t) andalso |
|
461 |
null (Term.add_tvars t []) andalso |
|
462 |
not (exists_subterm (is_bad_free frees) t) andalso |
|
463 |
length deps >= 2 andalso j mod isar_shrink_factor = 0 andalso |
|
464 |
(* kill next to last line, which usually results in a trivial step *) |
|
465 |
j <> 1) then |
|
466 |
Inference_Step (name, t, rule, deps) :: lines (* keep line *) |
|
467 |
else |
|
468 |
map (replace_dependencies_in_line (name, deps)) lines) (* drop line *) |
|
469 |
||
49883 | 470 |
(** Type annotations **) |
471 |
||
472 |
fun post_traverse_term_type' f _ (t as Const (_, T)) s = f t T s |
|
473 |
| post_traverse_term_type' f _ (t as Free (_, T)) s = f t T s |
|
474 |
| post_traverse_term_type' f _ (t as Var (_, T)) s = f t T s |
|
475 |
| post_traverse_term_type' f env (t as Bound i) s = f t (nth env i) s |
|
476 |
| post_traverse_term_type' f env (Abs (x, T1, b)) s = |
|
477 |
let |
|
478 |
val ((b', s'), T2) = post_traverse_term_type' f (T1 :: env) b s |
|
479 |
in f (Abs (x, T1, b')) (T1 --> T2) s' end |
|
480 |
| post_traverse_term_type' f env (u $ v) s = |
|
481 |
let |
|
482 |
val ((u', s'), Type (_, [_, T])) = post_traverse_term_type' f env u s |
|
483 |
val ((v', s''), _) = post_traverse_term_type' f env v s' |
|
484 |
in f (u' $ v') T s'' end |
|
485 |
||
486 |
fun post_traverse_term_type f s t = |
|
487 |
post_traverse_term_type' (fn t => fn T => fn s => (f t T s, T)) [] t s |> fst |
|
488 |
fun post_fold_term_type f s t = |
|
489 |
post_traverse_term_type (fn t => fn T => fn s => (t, f t T s)) s t |> snd |
|
490 |
||
491 |
(* Data structures, orders *) |
|
492 |
val cost_ord = prod_ord int_ord (prod_ord int_ord int_ord) |
|
493 |
||
494 |
structure Var_Set_Tab = Table( |
|
495 |
type key = indexname list |
|
496 |
val ord = list_ord Term_Ord.fast_indexname_ord) |
|
497 |
||
498 |
(* (1) Generalize Types *) |
|
499 |
fun generalize_types ctxt t = |
|
500 |
t |> map_types (fn _ => dummyT) |
|
501 |
|> Syntax.check_term |
|
502 |
(Proof_Context.set_mode Proof_Context.mode_pattern ctxt) |
|
503 |
||
504 |
(* (2) Typing-spot Table *) |
|
505 |
local |
|
506 |
fun key_of_atype (TVar (idxn, _)) = |
|
507 |
Ord_List.insert Term_Ord.fast_indexname_ord idxn |
|
508 |
| key_of_atype _ = I |
|
509 |
fun key_of_type T = fold_atyps key_of_atype T [] |
|
510 |
fun update_tab t T (tab, pos) = |
|
511 |
(case key_of_type T of |
|
512 |
[] => tab |
|
513 |
| key => |
|
514 |
let val cost = (size_of_typ T, (size_of_term t, pos)) in |
|
515 |
case Var_Set_Tab.lookup tab key of |
|
516 |
NONE => Var_Set_Tab.update_new (key, cost) tab |
|
517 |
| SOME old_cost => |
|
518 |
(case cost_ord (cost, old_cost) of |
|
519 |
LESS => Var_Set_Tab.update (key, cost) tab |
|
520 |
| _ => tab) |
|
521 |
end, |
|
522 |
pos + 1) |
|
523 |
in |
|
524 |
val typing_spot_table = |
|
525 |
post_fold_term_type update_tab (Var_Set_Tab.empty, 0) #> fst |
|
526 |
end |
|
527 |
||
528 |
(* (3) Reverse-Greedy *) |
|
529 |
fun reverse_greedy typing_spot_tab = |
|
530 |
let |
|
531 |
fun update_count z = |
|
532 |
fold (fn tvar => fn tab => |
|
533 |
let val c = Vartab.lookup tab tvar |> the_default 0 in |
|
534 |
Vartab.update (tvar, c + z) tab |
|
535 |
end) |
|
536 |
fun superfluous tcount = |
|
537 |
forall (fn tvar => the (Vartab.lookup tcount tvar) > 1) |
|
538 |
fun drop_superfluous (tvars, (_, (_, spot))) (spots, tcount) = |
|
539 |
if superfluous tcount tvars then (spots, update_count ~1 tvars tcount) |
|
540 |
else (spot :: spots, tcount) |
|
541 |
val (typing_spots, tvar_count_tab) = |
|
542 |
Var_Set_Tab.fold |
|
543 |
(fn kv as (k, _) => apfst (cons kv) #> apsnd (update_count 1 k)) |
|
544 |
typing_spot_tab ([], Vartab.empty) |
|
545 |
|>> sort_distinct (rev_order o cost_ord o pairself snd) |
|
546 |
in fold drop_superfluous typing_spots ([], tvar_count_tab) |> fst end |
|
547 |
||
548 |
(* (4) Introduce Annotations *) |
|
549 |
fun introduce_annotations thy spots t t' = |
|
550 |
let |
|
551 |
val get_types = post_fold_term_type (K cons) [] |
|
552 |
fun match_types tp = |
|
553 |
fold (Sign.typ_match thy) (op ~~ (pairself get_types tp)) Vartab.empty |
|
554 |
fun unica' b x [] = if b then [x] else [] |
|
555 |
| unica' b x (y :: ys) = |
|
556 |
if x = y then unica' false x ys |
|
557 |
else unica' true y ys |> b ? cons x |
|
558 |
fun unica ord xs = |
|
559 |
case sort ord xs of x :: ys => unica' true x ys | [] => [] |
|
560 |
val add_all_tfree_namesT = fold_atyps (fn TFree (x, _) => cons x | _ => I) |
|
561 |
fun erase_unica_tfrees env = |
|
562 |
let |
|
563 |
val unica = |
|
564 |
Vartab.fold (add_all_tfree_namesT o snd o snd) env [] |
|
565 |
|> unica fast_string_ord |
|
566 |
val erase_unica = map_atyps |
|
567 |
(fn T as TFree (s, _) => |
|
568 |
if Ord_List.member fast_string_ord unica s then dummyT else T |
|
569 |
| T => T) |
|
570 |
in Vartab.map (K (apsnd erase_unica)) env end |
|
571 |
val env = match_types (t', t) |> erase_unica_tfrees |
|
572 |
fun get_annot env (TFree _) = (false, (env, dummyT)) |
|
573 |
| get_annot env (T as TVar (v, S)) = |
|
574 |
let val T' = Envir.subst_type env T in |
|
575 |
if T' = dummyT then (false, (env, dummyT)) |
|
576 |
else (true, (Vartab.update (v, (S, dummyT)) env, T')) |
|
577 |
end |
|
578 |
| get_annot env (Type (S, Ts)) = |
|
579 |
(case fold_rev (fn T => fn (b, (env, Ts)) => |
|
580 |
let |
|
581 |
val (b', (env', T)) = get_annot env T |
|
582 |
in (b orelse b', (env', T :: Ts)) end) |
|
583 |
Ts (false, (env, [])) of |
|
584 |
(true, (env', Ts)) => (true, (env', Type (S, Ts))) |
|
585 |
| (false, (env', _)) => (false, (env', dummyT))) |
|
586 |
fun post1 _ T (env, cp, ps as p :: ps', annots) = |
|
587 |
if p <> cp then |
|
588 |
(env, cp + 1, ps, annots) |
|
589 |
else |
|
590 |
let val (_, (env', T')) = get_annot env T in |
|
591 |
(env', cp + 1, ps', (p, T') :: annots) |
|
592 |
end |
|
593 |
| post1 _ _ accum = accum |
|
594 |
val (_, _, _, annots) = post_fold_term_type post1 (env, 0, spots, []) t' |
|
595 |
fun post2 t _ (cp, annots as (p, T) :: annots') = |
|
596 |
if p <> cp then (t, (cp + 1, annots)) |
|
597 |
else (Type.constraint T t, (cp + 1, annots')) |
|
598 |
| post2 t _ x = (t, x) |
|
599 |
in post_traverse_term_type post2 (0, rev annots) t |> fst end |
|
600 |
||
601 |
(* (5) Annotate *) |
|
602 |
fun annotate_types ctxt t = |
|
603 |
let |
|
604 |
val thy = Proof_Context.theory_of ctxt |
|
605 |
val t' = generalize_types ctxt t |
|
606 |
val typing_spots = |
|
607 |
t' |> typing_spot_table |
|
608 |
|> reverse_greedy |
|
609 |
|> sort int_ord |
|
610 |
in introduce_annotations thy typing_spots t t' end |
|
611 |
||
49914 | 612 |
val indent_size = 2 |
613 |
val no_label = ("", ~1) |
|
614 |
||
49883 | 615 |
fun string_for_proof ctxt type_enc lam_trans i n = |
616 |
let |
|
617 |
fun fix_print_mode f x = |
|
618 |
Print_Mode.setmp (filter (curry (op =) Symbol.xsymbolsN) |
|
619 |
(print_mode_value ())) f x |
|
620 |
fun do_indent ind = replicate_string (ind * indent_size) " " |
|
621 |
fun do_free (s, T) = |
|
622 |
maybe_quote s ^ " :: " ^ |
|
623 |
maybe_quote (fix_print_mode (Syntax.string_of_typ ctxt) T) |
|
624 |
fun do_label l = if l = no_label then "" else string_for_label l ^ ": " |
|
625 |
fun do_have qs = |
|
626 |
(if member (op =) qs Moreover then "moreover " else "") ^ |
|
627 |
(if member (op =) qs Ultimately then "ultimately " else "") ^ |
|
628 |
(if member (op =) qs Then then |
|
629 |
if member (op =) qs Show then "thus" else "hence" |
|
630 |
else |
|
631 |
if member (op =) qs Show then "show" else "have") |
|
632 |
val do_term = |
|
633 |
maybe_quote o fix_print_mode (Syntax.string_of_term ctxt) |
|
634 |
o annotate_types ctxt |
|
635 |
val reconstr = Metis (type_enc, lam_trans) |
|
636 |
fun do_facts (ls, ss) = |
|
637 |
reconstructor_command reconstr 1 1 [] 0 |
|
638 |
(ls |> sort_distinct (prod_ord string_ord int_ord), |
|
639 |
ss |> sort_distinct string_ord) |
|
640 |
and do_step ind (Fix xs) = |
|
641 |
do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n" |
|
642 |
| do_step ind (Let (t1, t2)) = |
|
643 |
do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n" |
|
644 |
| do_step ind (Assume (l, t)) = |
|
645 |
do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n" |
|
646 |
| do_step ind (Prove (qs, l, t, By_Metis facts)) = |
|
647 |
do_indent ind ^ do_have qs ^ " " ^ |
|
648 |
do_label l ^ do_term t ^ " " ^ do_facts facts ^ "\n" |
|
649 |
| do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) = |
|
650 |
implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind) |
|
651 |
proofs) ^ |
|
652 |
do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ " " ^ |
|
653 |
do_facts facts ^ "\n" |
|
654 |
and do_steps prefix suffix ind steps = |
|
655 |
let val s = implode (map (do_step ind) steps) in |
|
656 |
replicate_string (ind * indent_size - size prefix) " " ^ prefix ^ |
|
657 |
String.extract (s, ind * indent_size, |
|
658 |
SOME (size s - ind * indent_size - 1)) ^ |
|
659 |
suffix ^ "\n" |
|
660 |
end |
|
661 |
and do_block ind proof = do_steps "{ " " }" (ind + 1) proof |
|
662 |
(* One-step proofs are pointless; better use the Metis one-liner |
|
663 |
directly. *) |
|
664 |
and do_proof [Prove (_, _, _, By_Metis _)] = "" |
|
665 |
| do_proof proof = |
|
666 |
(if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^ |
|
667 |
do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^ |
|
668 |
(if n <> 1 then "next" else "qed") |
|
669 |
in do_proof end |
|
670 |
||
49914 | 671 |
(* FIXME: Still needed? Try with SPASS proofs perhaps. *) |
672 |
val kill_duplicate_assumptions_in_proof = |
|
673 |
let |
|
674 |
fun relabel_facts subst = |
|
675 |
apfst (map (fn l => AList.lookup (op =) subst l |> the_default l)) |
|
676 |
fun do_step (step as Assume (l, t)) (proof, subst, assums) = |
|
677 |
(case AList.lookup (op aconv) assums t of |
|
678 |
SOME l' => (proof, (l, l') :: subst, assums) |
|
679 |
| NONE => (step :: proof, subst, (t, l) :: assums)) |
|
680 |
| do_step (Prove (qs, l, t, by)) (proof, subst, assums) = |
|
681 |
(Prove (qs, l, t, |
|
682 |
case by of |
|
683 |
By_Metis facts => By_Metis (relabel_facts subst facts) |
|
684 |
| Case_Split (proofs, facts) => |
|
685 |
Case_Split (map do_proof proofs, |
|
686 |
relabel_facts subst facts)) :: |
|
687 |
proof, subst, assums) |
|
688 |
| do_step step (proof, subst, assums) = (step :: proof, subst, assums) |
|
689 |
and do_proof proof = fold do_step proof ([], [], []) |> #1 |> rev |
|
690 |
in do_proof end |
|
691 |
||
692 |
fun used_labels_of_step (Prove (_, _, _, by)) = |
|
693 |
(case by of |
|
694 |
By_Metis (ls, _) => ls |
|
695 |
| Case_Split (proofs, (ls, _)) => |
|
696 |
fold (union (op =) o used_labels_of) proofs ls) |
|
697 |
| used_labels_of_step _ = [] |
|
698 |
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof [] |
|
699 |
||
700 |
fun kill_useless_labels_in_proof proof = |
|
701 |
let |
|
702 |
val used_ls = used_labels_of proof |
|
703 |
fun do_label l = if member (op =) used_ls l then l else no_label |
|
704 |
fun do_step (Assume (l, t)) = Assume (do_label l, t) |
|
705 |
| do_step (Prove (qs, l, t, by)) = |
|
706 |
Prove (qs, do_label l, t, |
|
707 |
case by of |
|
708 |
Case_Split (proofs, facts) => |
|
709 |
Case_Split (map (map do_step) proofs, facts) |
|
710 |
| _ => by) |
|
711 |
| do_step step = step |
|
712 |
in map do_step proof end |
|
713 |
||
714 |
fun prefix_for_depth n = replicate_string (n + 1) |
|
715 |
||
716 |
val relabel_proof = |
|
717 |
let |
|
718 |
fun aux _ _ _ [] = [] |
|
719 |
| aux subst depth (next_assum, next_fact) (Assume (l, t) :: proof) = |
|
720 |
if l = no_label then |
|
721 |
Assume (l, t) :: aux subst depth (next_assum, next_fact) proof |
|
722 |
else |
|
723 |
let val l' = (prefix_for_depth depth assum_prefix, next_assum) in |
|
724 |
Assume (l', t) :: |
|
725 |
aux ((l, l') :: subst) depth (next_assum + 1, next_fact) proof |
|
726 |
end |
|
727 |
| aux subst depth (next_assum, next_fact) |
|
728 |
(Prove (qs, l, t, by) :: proof) = |
|
729 |
let |
|
730 |
val (l', subst, next_fact) = |
|
731 |
if l = no_label then |
|
732 |
(l, subst, next_fact) |
|
733 |
else |
|
734 |
let |
|
735 |
val l' = (prefix_for_depth depth have_prefix, next_fact) |
|
736 |
in (l', (l, l') :: subst, next_fact + 1) end |
|
737 |
val relabel_facts = |
|
738 |
apfst (maps (the_list o AList.lookup (op =) subst)) |
|
739 |
val by = |
|
740 |
case by of |
|
741 |
By_Metis facts => By_Metis (relabel_facts facts) |
|
742 |
| Case_Split (proofs, facts) => |
|
743 |
Case_Split (map (aux subst (depth + 1) (1, 1)) proofs, |
|
744 |
relabel_facts facts) |
|
745 |
in |
|
746 |
Prove (qs, l', t, by) :: aux subst depth (next_assum, next_fact) proof |
|
747 |
end |
|
748 |
| aux subst depth nextp (step :: proof) = |
|
749 |
step :: aux subst depth nextp proof |
|
750 |
in aux [] 0 (1, 1) end |
|
751 |
||
49913 | 752 |
fun minimize_locally ctxt type_enc lam_trans proof = |
49883 | 753 |
let |
754 |
(* Merging spots, greedy algorithm *) |
|
755 |
fun cost (Prove (_, _ , t, _)) = Term.size_of_term t |
|
756 |
| cost _ = ~1 |
|
49913 | 757 |
fun can_merge (Prove (_, lbl, _, By_Metis _)) |
758 |
(Prove (_, _, _, By_Metis _)) = |
|
759 |
(lbl = no_label) |
|
49883 | 760 |
| can_merge _ _ = false |
761 |
val merge_spots = |
|
49913 | 762 |
fold_index (fn (i, s2) => fn (s1, pile) => |
763 |
(s2, pile |> can_merge s1 s2 ? cons (i, cost s1))) |
|
49883 | 764 |
(tl proof) (hd proof, []) |
765 |
|> snd |> sort (rev_order o int_ord o pairself snd) |> map fst |
|
766 |
||
767 |
(* Enrich context with facts *) |
|
768 |
val thy = Proof_Context.theory_of ctxt |
|
49915 | 769 |
fun sorry t = Skip_Proof.make_thm thy (HOLogic.mk_Trueprop t) |
49883 | 770 |
fun enrich_ctxt' (Prove (_, lbl, t, _)) ctxt = |
49913 | 771 |
ctxt |> lbl <> no_label |
772 |
? Proof_Context.put_thms false (string_for_label lbl, SOME [sorry t]) |
|
49883 | 773 |
| enrich_ctxt' _ ctxt = ctxt |
774 |
val rich_ctxt = fold enrich_ctxt' proof ctxt |
|
775 |
||
776 |
(* Timing *) |
|
777 |
fun take_time tac arg = |
|
49915 | 778 |
let val t_start = Timing.start () in |
779 |
(tac arg; Timing.result t_start |> #cpu) |
|
49883 | 780 |
end |
781 |
fun try_metis (Prove (qs, _, t, By_Metis fact_names)) s0 = |
|
782 |
let |
|
783 |
fun thmify (Prove (_, _, t, _)) = sorry t |
|
49913 | 784 |
val facts = |
785 |
fact_names |
|
49915 | 786 |
|>> map string_for_label |> op @ |
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
787 |
|> map (the_single o thms_of_name rich_ctxt) |
49913 | 788 |
|> (if member (op =) qs Then then cons (the s0 |> thmify) else I) |
49915 | 789 |
val goal = Goal.prove ctxt [] [] (HOLogic.mk_Trueprop t) |
49883 | 790 |
fun tac {context = ctxt, prems = _} = |
791 |
Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1 |
|
792 |
in |
|
793 |
take_time (fn () => goal tac) |
|
794 |
end |
|
795 |
||
796 |
(* Merging *) |
|
797 |
fun merge (Prove (qs1, _, _, By_Metis (ls1, ss1))) |
|
798 |
(Prove (qs2, lbl , t, By_Metis (ls2, ss2))) = |
|
799 |
let |
|
49913 | 800 |
val qs = |
801 |
inter (op =) qs1 qs2 (* FIXME: Is this correct? *) |
|
802 |
|> member (op =) (union (op =) qs1 qs2) Ultimately ? cons Ultimately |
|
803 |
|> member (op =) qs2 Show ? cons Show |
|
804 |
in Prove (qs, lbl, t, By_Metis (ls1 @ ls2, ss1 @ ss2)) end |
|
49883 | 805 |
fun try_merge proof i = |
806 |
let |
|
807 |
val (front, s0, s1, s2, tail) = |
|
808 |
case (proof, i) of |
|
49913 | 809 |
((s1 :: s2 :: proof), 0) => ([], NONE, s1, s2, proof) |
810 |
| _ => |
|
811 |
let val (front, s0 :: s1 :: s2 :: tail) = chop (i - 1) proof in |
|
812 |
(front, SOME s0, s1, s2, tail) |
|
813 |
end |
|
49883 | 814 |
val s12 = merge s1 s2 |
815 |
val t1 = try_metis s1 s0 () |
|
816 |
val t2 = try_metis s2 (SOME s1) () |
|
817 |
val tlimit = t1 + t2 |> Time.toReal |> curry Real.* 1.2 |> Time.fromReal |
|
818 |
in |
|
819 |
(TimeLimit.timeLimit tlimit (try_metis s12 s0) (); |
|
49915 | 820 |
SOME (front @ (the_list s0 @ s12 :: tail))) |
49883 | 821 |
handle _ => NONE |
822 |
end |
|
823 |
fun merge_steps proof [] = proof |
|
49913 | 824 |
| merge_steps proof (i :: is) = |
49883 | 825 |
case try_merge proof i of |
826 |
NONE => merge_steps proof is |
|
49913 | 827 |
| SOME proof' => |
828 |
merge_steps proof' (map (fn j => if j > i then j - 1 else j) is) |
|
49883 | 829 |
in merge_steps proof merge_spots end |
830 |
||
49914 | 831 |
type isar_params = |
832 |
bool * int * string Symtab.table * (string * stature) list vector |
|
833 |
* int Symtab.table * string proof * thm |
|
834 |
||
49883 | 835 |
fun isar_proof_text ctxt isar_proof_requested |
836 |
(debug, isar_shrink_factor, pool, fact_names, sym_tab, atp_proof, goal) |
|
837 |
(one_line_params as (_, _, _, _, subgoal, subgoal_count)) = |
|
838 |
let |
|
839 |
val isar_shrink_factor = |
|
840 |
(if isar_proof_requested then 1 else 2) * isar_shrink_factor |
|
841 |
val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal |
|
842 |
val frees = fold Term.add_frees (concl_t :: hyp_ts) [] |
|
843 |
val one_line_proof = one_line_proof_text 0 one_line_params |
|
844 |
val type_enc = |
|
845 |
if is_typed_helper_used_in_atp_proof atp_proof then full_typesN |
|
846 |
else partial_typesN |
|
847 |
val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans |
|
848 |
||
849 |
fun isar_proof_of () = |
|
850 |
let |
|
851 |
val atp_proof = |
|
852 |
atp_proof |
|
853 |
|> clean_up_atp_proof_dependencies |
|
854 |
|> nasty_atp_proof pool |
|
855 |
|> map_term_names_in_atp_proof repair_name |
|
856 |
|> decode_lines ctxt sym_tab |
|
857 |
|> rpair [] |-> fold_rev (add_line fact_names) |
|
858 |
|> repair_waldmeister_endgame |
|
859 |
|> rpair [] |-> fold_rev add_nontrivial_line |
|
860 |
|> rpair (0, []) |
|
861 |
|-> fold_rev (add_desired_line isar_shrink_factor fact_names frees) |
|
862 |
|> snd |
|
863 |
val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts) |
|
864 |
val conjs = |
|
865 |
atp_proof |
|
866 |
|> map_filter (fn Inference_Step (name as (_, ss), _, _, []) => |
|
867 |
if member (op =) ss conj_name then SOME name else NONE |
|
868 |
| _ => NONE) |
|
869 |
fun dep_of_step (Definition_Step _) = NONE |
|
870 |
| dep_of_step (Inference_Step (name, _, _, from)) = SOME (from, name) |
|
871 |
val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph |
|
872 |
val axioms = axioms_of_ref_graph ref_graph conjs |
|
873 |
val tainted = tainted_atoms_of_ref_graph ref_graph conjs |
|
874 |
val props = |
|
875 |
Symtab.empty |
|
876 |
|> fold (fn Definition_Step _ => I (* FIXME *) |
|
877 |
| Inference_Step ((s, _), t, _, _) => |
|
878 |
Symtab.update_new (s, |
|
879 |
t |> fold forall_of (map Var (Term.add_vars t [])) |
|
880 |
|> member (op = o apsnd fst) tainted s ? s_not)) |
|
881 |
atp_proof |
|
882 |
fun prop_of_clause c = |
|
883 |
fold (curry s_disj) (map_filter (Symtab.lookup props o fst) c) |
|
884 |
@{term False} |
|
885 |
fun label_of_clause [name] = raw_label_for_name name |
|
886 |
| label_of_clause c = (space_implode "___" (map fst c), 0) |
|
887 |
fun maybe_show outer c = |
|
888 |
(outer andalso length c = 1 andalso subset (op =) (c, conjs)) |
|
889 |
? cons Show |
|
890 |
fun do_have outer qs (gamma, c) = |
|
891 |
Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c, |
|
892 |
By_Metis (fold (add_fact_from_dependency fact_names |
|
893 |
o the_single) gamma ([], []))) |
|
894 |
fun do_inf outer (Have z) = do_have outer [] z |
|
895 |
| do_inf outer (Hence z) = do_have outer [Then] z |
|
896 |
| do_inf outer (Cases cases) = |
|
897 |
let val c = succedent_of_cases cases in |
|
898 |
Prove (maybe_show outer c [Ultimately], label_of_clause c, |
|
899 |
prop_of_clause c, |
|
900 |
Case_Split (map (do_case false) cases, ([], []))) |
|
901 |
end |
|
902 |
and do_case outer (c, infs) = |
|
903 |
Assume (label_of_clause c, prop_of_clause c) :: |
|
904 |
map (do_inf outer) infs |
|
905 |
val isar_proof = |
|
906 |
(if null params then [] else [Fix params]) @ |
|
907 |
(ref_graph |
|
908 |
|> redirect_graph axioms tainted |
|
909 |
|> chain_direct_proof |
|
910 |
|> map (do_inf true) |
|
911 |
|> kill_duplicate_assumptions_in_proof |
|
912 |
|> kill_useless_labels_in_proof |
|
913 |
|> relabel_proof |
|
49913 | 914 |
|> minimize_locally ctxt type_enc lam_trans) |
49883 | 915 |
|> string_for_proof ctxt type_enc lam_trans subgoal subgoal_count |
916 |
in |
|
917 |
case isar_proof of |
|
918 |
"" => |
|
919 |
if isar_proof_requested then |
|
920 |
"\nNo structured proof available (proof too short)." |
|
921 |
else |
|
922 |
"" |
|
923 |
| _ => |
|
924 |
"\n\n" ^ (if isar_proof_requested then "Structured proof" |
|
925 |
else "Perhaps this will work") ^ |
|
926 |
":\n" ^ Markup.markup Isabelle_Markup.sendback isar_proof |
|
927 |
end |
|
928 |
val isar_proof = |
|
929 |
if debug then |
|
930 |
isar_proof_of () |
|
931 |
else case try isar_proof_of () of |
|
932 |
SOME s => s |
|
933 |
| NONE => if isar_proof_requested then |
|
934 |
"\nWarning: The Isar proof construction failed." |
|
935 |
else |
|
936 |
"" |
|
937 |
in one_line_proof ^ isar_proof end |
|
938 |
||
939 |
fun proof_text ctxt isar_proof isar_params num_chained |
|
940 |
(one_line_params as (preplay, _, _, _, _, _)) = |
|
941 |
(if case preplay of Failed_to_Play _ => true | _ => isar_proof then |
|
942 |
isar_proof_text ctxt isar_proof isar_params |
|
943 |
else |
|
944 |
one_line_proof_text num_chained) one_line_params |
|
945 |
||
946 |
end; |