author | smolkas |
Wed, 28 Nov 2012 12:22:05 +0100 | |
changeset 50258 | 1c708d7728c7 |
parent 50257 | bafbc4a3d976 |
child 50259 | 9c64a52ae499 |
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 = |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
26 |
bool * bool * Time.time * real * string Symtab.table |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
27 |
* (string * stature) list vector * int Symtab.table * string proof * thm |
49914 | 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 |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
55 |
open Sledgehammer_Util |
50258 | 56 |
open Sledgehammer_Annotate |
49914 | 57 |
|
58 |
structure String_Redirect = ATP_Proof_Redirect( |
|
59 |
type key = step_name |
|
60 |
val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s') |
|
61 |
val string_of = fst) |
|
62 |
||
49883 | 63 |
open String_Redirect |
64 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
65 |
|
49914 | 66 |
(** reconstructors **) |
67 |
||
68 |
datatype reconstructor = |
|
69 |
Metis of string * string | |
|
70 |
SMT |
|
71 |
||
72 |
datatype play = |
|
73 |
Played of reconstructor * Time.time | |
|
74 |
Trust_Playable of reconstructor * Time.time option | |
|
75 |
Failed_to_Play of reconstructor |
|
76 |
||
77 |
val smtN = "smt" |
|
78 |
||
79 |
fun string_for_reconstructor (Metis (type_enc, lam_trans)) = |
|
80 |
metis_call type_enc lam_trans |
|
81 |
| string_for_reconstructor SMT = smtN |
|
82 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
83 |
fun thms_of_name ctxt name = |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
84 |
let |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
85 |
val lex = Keyword.get_lexicons |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
86 |
val get = maps (Proof_Context.get_fact ctxt o fst) |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
87 |
in |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
88 |
Source.of_string name |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
89 |
|> Symbol.source |
49981 | 90 |
|> Token.source {do_recover = SOME false} lex Position.start |
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
91 |
|> Token.source_proper |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
92 |
|> Source.source Token.stopper (Parse_Spec.xthms1 >> get) NONE |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
93 |
|> Source.exhaust |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
94 |
end |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
95 |
|
49914 | 96 |
|
97 |
(** fact extraction from ATP proofs **) |
|
98 |
||
99 |
fun find_first_in_list_vector vec key = |
|
100 |
Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key |
|
101 |
| (_, value) => value) NONE vec |
|
102 |
||
103 |
val unprefix_fact_number = space_implode "_" o tl o space_explode "_" |
|
104 |
||
105 |
fun resolve_one_named_fact fact_names s = |
|
106 |
case try (unprefix fact_prefix) s of |
|
107 |
SOME s' => |
|
108 |
let val s' = s' |> unprefix_fact_number |> unascii_of in |
|
109 |
s' |> find_first_in_list_vector fact_names |> Option.map (pair s') |
|
110 |
end |
|
111 |
| NONE => NONE |
|
112 |
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names) |
|
113 |
fun is_fact fact_names = not o null o resolve_fact fact_names |
|
114 |
||
115 |
fun resolve_one_named_conjecture s = |
|
116 |
case try (unprefix conjecture_prefix) s of |
|
117 |
SOME s' => Int.fromString s' |
|
118 |
| NONE => NONE |
|
119 |
||
120 |
val resolve_conjecture = map_filter resolve_one_named_conjecture |
|
121 |
val is_conjecture = not o null o resolve_conjecture |
|
122 |
||
123 |
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix |
|
124 |
||
125 |
(* overapproximation (good enough) *) |
|
126 |
fun is_lam_lifted s = |
|
127 |
String.isPrefix fact_prefix s andalso |
|
128 |
String.isSubstring ascii_of_lam_fact_prefix s |
|
129 |
||
130 |
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix) |
|
131 |
||
132 |
fun is_axiom_used_in_proof pred = |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
133 |
exists (fn Inference_Step ((_, ss), _, _, _, []) => exists pred ss |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
134 |
| _ => false) |
49914 | 135 |
|
136 |
fun lam_trans_from_atp_proof atp_proof default = |
|
137 |
case (is_axiom_used_in_proof is_combinator_def atp_proof, |
|
138 |
is_axiom_used_in_proof is_lam_lifted atp_proof) of |
|
139 |
(false, false) => default |
|
140 |
| (false, true) => liftingN |
|
141 |
(* | (true, true) => combs_and_liftingN -- not supported by "metis" *) |
|
142 |
| (true, _) => combsN |
|
143 |
||
144 |
val is_typed_helper_name = |
|
145 |
String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix |
|
146 |
fun is_typed_helper_used_in_atp_proof atp_proof = |
|
147 |
is_axiom_used_in_proof is_typed_helper_name atp_proof |
|
148 |
||
149 |
fun add_non_rec_defs fact_names accum = |
|
150 |
Vector.foldl (fn (facts, facts') => |
|
151 |
union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts) |
|
152 |
facts') |
|
153 |
accum fact_names |
|
154 |
||
155 |
val isa_ext = Thm.get_name_hint @{thm ext} |
|
156 |
val isa_short_ext = Long_Name.base_name isa_ext |
|
157 |
||
158 |
fun ext_name ctxt = |
|
159 |
if Thm.eq_thm_prop (@{thm ext}, |
|
160 |
singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then |
|
161 |
isa_short_ext |
|
162 |
else |
|
163 |
isa_ext |
|
164 |
||
165 |
val leo2_ext = "extcnf_equal_neg" |
|
166 |
val leo2_unfold_def = "unfold_def" |
|
167 |
||
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
168 |
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, _, rule, deps)) = |
49914 | 169 |
(if rule = leo2_ext then |
170 |
insert (op =) (ext_name ctxt, (Global, General)) |
|
171 |
else if rule = leo2_unfold_def then |
|
172 |
(* LEO 1.3.3 does not record definitions properly, leading to missing |
|
173 |
dependencies in the TSTP proof. Remove the next line once this is |
|
174 |
fixed. *) |
|
175 |
add_non_rec_defs fact_names |
|
176 |
else if rule = satallax_coreN then |
|
177 |
(fn [] => |
|
178 |
(* Satallax doesn't include definitions in its unsatisfiable cores, |
|
179 |
so we assume the worst and include them all here. *) |
|
180 |
[(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names |
|
181 |
| facts => facts) |
|
182 |
else |
|
183 |
I) |
|
184 |
#> (if null deps then union (op =) (resolve_fact fact_names ss) |
|
185 |
else I) |
|
186 |
| add_fact _ _ _ = I |
|
187 |
||
188 |
fun used_facts_in_atp_proof ctxt fact_names atp_proof = |
|
189 |
if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names |
|
190 |
else fold (add_fact ctxt fact_names) atp_proof [] |
|
191 |
||
192 |
fun used_facts_in_unsound_atp_proof _ _ [] = NONE |
|
193 |
| used_facts_in_unsound_atp_proof ctxt fact_names atp_proof = |
|
194 |
let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in |
|
195 |
if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso |
|
196 |
not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then |
|
197 |
SOME (map fst used_facts) |
|
198 |
else |
|
199 |
NONE |
|
200 |
end |
|
201 |
||
202 |
||
203 |
(** one-liner reconstructor proofs **) |
|
204 |
||
205 |
fun string_for_label (s, num) = s ^ string_of_int num |
|
206 |
||
207 |
fun show_time NONE = "" |
|
208 |
| show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")" |
|
209 |
||
49986
90e7be285b49
took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents:
49983
diff
changeset
|
210 |
(* FIXME: Various bugs, esp. with "unfolding" |
49914 | 211 |
fun unusing_chained_facts _ 0 = "" |
212 |
| unusing_chained_facts used_chaineds num_chained = |
|
213 |
if length used_chaineds = num_chained then "" |
|
214 |
else if null used_chaineds then "(* using no facts *) " |
|
215 |
else "(* using only " ^ space_implode " " used_chaineds ^ " *) " |
|
49986
90e7be285b49
took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents:
49983
diff
changeset
|
216 |
*) |
49914 | 217 |
|
218 |
fun apply_on_subgoal _ 1 = "by " |
|
219 |
| apply_on_subgoal 1 _ = "apply " |
|
220 |
| apply_on_subgoal i n = |
|
221 |
"prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n |
|
222 |
||
223 |
fun using_labels [] = "" |
|
224 |
| using_labels ls = |
|
225 |
"using " ^ space_implode " " (map string_for_label ls) ^ " " |
|
226 |
||
227 |
fun command_call name [] = |
|
50239 | 228 |
name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")" |
49914 | 229 |
| command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")" |
230 |
||
231 |
fun reconstructor_command reconstr i n used_chaineds num_chained (ls, ss) = |
|
49986
90e7be285b49
took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents:
49983
diff
changeset
|
232 |
(* unusing_chained_facts used_chaineds num_chained ^ *) |
49914 | 233 |
using_labels ls ^ apply_on_subgoal i n ^ |
234 |
command_call (string_for_reconstructor reconstr) ss |
|
235 |
||
236 |
fun try_command_line banner time command = |
|
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
237 |
banner ^ ": " ^ Sendback.markup command ^ show_time time ^ "." |
49914 | 238 |
|
239 |
fun minimize_line _ [] = "" |
|
240 |
| minimize_line minimize_command ss = |
|
241 |
case minimize_command ss of |
|
242 |
"" => "" |
|
243 |
| command => |
|
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
244 |
"\nTo minimize: " ^ Sendback.markup command ^ "." |
49914 | 245 |
|
246 |
fun split_used_facts facts = |
|
247 |
facts |> List.partition (fn (_, (sc, _)) => sc = Chained) |
|
248 |
|> pairself (sort_distinct (string_ord o pairself fst)) |
|
249 |
||
250 |
type minimize_command = string list -> string |
|
251 |
type one_line_params = |
|
252 |
play * string * (string * stature) list * minimize_command * int * int |
|
253 |
||
254 |
fun one_line_proof_text num_chained |
|
255 |
(preplay, banner, used_facts, minimize_command, subgoal, |
|
256 |
subgoal_count) = |
|
257 |
let |
|
258 |
val (chained, extra) = split_used_facts used_facts |
|
259 |
val (failed, reconstr, ext_time) = |
|
260 |
case preplay of |
|
261 |
Played (reconstr, time) => (false, reconstr, (SOME (false, time))) |
|
262 |
| Trust_Playable (reconstr, time) => |
|
263 |
(false, reconstr, |
|
264 |
case time of |
|
265 |
NONE => NONE |
|
266 |
| SOME time => |
|
267 |
if time = Time.zeroTime then NONE else SOME (true, time)) |
|
268 |
| Failed_to_Play reconstr => (true, reconstr, NONE) |
|
269 |
val try_line = |
|
270 |
([], map fst extra) |
|
271 |
|> reconstructor_command reconstr subgoal subgoal_count (map fst chained) |
|
272 |
num_chained |
|
273 |
|> (if failed then |
|
274 |
enclose "One-line proof reconstruction failed: " |
|
275 |
".\n(Invoking \"sledgehammer\" with \"[strict]\" might \ |
|
276 |
\solve this.)" |
|
277 |
else |
|
278 |
try_command_line banner ext_time) |
|
279 |
in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end |
|
280 |
||
281 |
||
282 |
(** Isar proof construction and manipulation **) |
|
283 |
||
284 |
type label = string * int |
|
285 |
type facts = label list * string list |
|
286 |
||
287 |
datatype isar_qualifier = Show | Then | Moreover | Ultimately |
|
288 |
||
289 |
datatype isar_step = |
|
290 |
Fix of (string * typ) list | |
|
291 |
Let of term * term | |
|
292 |
Assume of label * term | |
|
293 |
Prove of isar_qualifier list * label * term * byline |
|
294 |
and byline = |
|
295 |
By_Metis of facts | |
|
296 |
Case_Split of isar_step list list * facts |
|
297 |
||
50017 | 298 |
val assume_prefix = "a" |
49914 | 299 |
val have_prefix = "f" |
300 |
val raw_prefix = "x" |
|
301 |
||
302 |
fun raw_label_for_name (num, ss) = |
|
303 |
case resolve_conjecture ss of |
|
304 |
[j] => (conjecture_prefix, j) |
|
305 |
| _ => (raw_prefix ^ ascii_of num, 0) |
|
306 |
||
50005 | 307 |
fun label_of_clause [name] = raw_label_for_name name |
308 |
| label_of_clause c = (space_implode "___" (map fst c), 0) |
|
309 |
||
310 |
fun add_fact_from_dependencies fact_names (names as [(_, ss)]) = |
|
311 |
if is_fact fact_names ss then |
|
312 |
apsnd (union (op =) (map fst (resolve_fact fact_names ss))) |
|
313 |
else |
|
314 |
apfst (insert (op =) (label_of_clause names)) |
|
315 |
| add_fact_from_dependencies fact_names names = |
|
316 |
apfst (insert (op =) (label_of_clause names)) |
|
49914 | 317 |
|
318 |
fun repair_name "$true" = "c_True" |
|
319 |
| repair_name "$false" = "c_False" |
|
320 |
| repair_name "$$e" = tptp_equal (* seen in Vampire proofs *) |
|
321 |
| repair_name s = |
|
322 |
if is_tptp_equal s orelse |
|
323 |
(* seen in Vampire proofs *) |
|
324 |
(String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then |
|
325 |
tptp_equal |
|
326 |
else |
|
327 |
s |
|
328 |
||
329 |
fun unvarify_term (Var ((s, 0), T)) = Free (s, T) |
|
330 |
| unvarify_term t = raise TERM ("unvarify_term: non-Var", [t]) |
|
331 |
||
332 |
fun infer_formula_types ctxt = |
|
333 |
Type.constraint HOLogic.boolT |
|
334 |
#> Syntax.check_term |
|
335 |
(Proof_Context.set_mode Proof_Context.mode_schematic ctxt) |
|
336 |
||
337 |
val combinator_table = |
|
338 |
[(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}), |
|
339 |
(@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}), |
|
340 |
(@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}), |
|
341 |
(@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}), |
|
342 |
(@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})] |
|
343 |
||
344 |
fun uncombine_term thy = |
|
345 |
let |
|
346 |
fun aux (t1 $ t2) = betapply (pairself aux (t1, t2)) |
|
347 |
| aux (Abs (s, T, t')) = Abs (s, T, aux t') |
|
348 |
| aux (t as Const (x as (s, _))) = |
|
349 |
(case AList.lookup (op =) combinator_table s of |
|
350 |
SOME thm => thm |> prop_of |> specialize_type thy x |
|
351 |
|> Logic.dest_equals |> snd |
|
352 |
| NONE => t) |
|
353 |
| aux t = t |
|
354 |
in aux end |
|
355 |
||
356 |
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt = |
|
357 |
let |
|
358 |
val thy = Proof_Context.theory_of ctxt |
|
359 |
val t1 = prop_from_atp ctxt true sym_tab phi1 |
|
360 |
val vars = snd (strip_comb t1) |
|
361 |
val frees = map unvarify_term vars |
|
362 |
val unvarify_args = subst_atomic (vars ~~ frees) |
|
363 |
val t2 = prop_from_atp ctxt true sym_tab phi2 |
|
364 |
val (t1, t2) = |
|
365 |
HOLogic.eq_const HOLogic.typeT $ t1 $ t2 |
|
366 |
|> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt |
|
367 |
|> HOLogic.dest_eq |
|
368 |
in |
|
369 |
(Definition_Step (name, t1, t2), |
|
370 |
fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt) |
|
371 |
end |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
372 |
| decode_line sym_tab (Inference_Step (name, role, u, rule, deps)) ctxt = |
49914 | 373 |
let |
374 |
val thy = Proof_Context.theory_of ctxt |
|
375 |
val t = u |> prop_from_atp ctxt true sym_tab |
|
376 |
|> uncombine_term thy |> infer_formula_types ctxt |
|
377 |
in |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
378 |
(Inference_Step (name, role, t, rule, deps), |
49914 | 379 |
fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt) |
380 |
end |
|
381 |
fun decode_lines ctxt sym_tab lines = |
|
382 |
fst (fold_map (decode_line sym_tab) lines ctxt) |
|
383 |
||
384 |
fun replace_one_dependency (old, new) dep = |
|
385 |
if is_same_atp_step dep old then new else [dep] |
|
386 |
fun replace_dependencies_in_line _ (line as Definition_Step _) = line |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
387 |
| replace_dependencies_in_line p |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
388 |
(Inference_Step (name, role, t, rule, deps)) = |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
389 |
Inference_Step (name, role, t, rule, |
49914 | 390 |
fold (union (op =) o replace_one_dependency p) deps []) |
391 |
||
392 |
(* No "real" literals means only type information (tfree_tcs, clsrel, or |
|
393 |
clsarity). *) |
|
394 |
fun is_only_type_information t = t aconv @{term True} |
|
395 |
||
396 |
fun is_same_inference _ (Definition_Step _) = false |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
397 |
| is_same_inference t (Inference_Step (_, _, t', _, _)) = t aconv t' |
49914 | 398 |
|
399 |
(* Discard facts; consolidate adjacent lines that prove the same formula, since |
|
400 |
they differ only in type information.*) |
|
401 |
fun add_line _ (line as Definition_Step _) lines = line :: lines |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
402 |
| add_line fact_names (Inference_Step (name as (_, ss), role, t, rule, [])) |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
403 |
lines = |
49914 | 404 |
(* No dependencies: fact, conjecture, or (for Vampire) internal facts or |
405 |
definitions. *) |
|
406 |
if is_fact fact_names ss then |
|
407 |
(* Facts are not proof lines. *) |
|
408 |
if is_only_type_information t then |
|
409 |
map (replace_dependencies_in_line (name, [])) lines |
|
410 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
|
411 |
else case take_prefix (not o is_same_inference t) lines of |
|
412 |
(_, []) => lines (* no repetition of proof line *) |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
413 |
| (pre, Inference_Step (name', _, _, _, _) :: post) => |
49914 | 414 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
415 |
| _ => raise Fail "unexpected inference" |
|
416 |
else if is_conjecture ss then |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
417 |
Inference_Step (name, role, t, rule, []) :: lines |
49914 | 418 |
else |
419 |
map (replace_dependencies_in_line (name, [])) lines |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
420 |
| add_line _ (Inference_Step (name, role, t, rule, deps)) lines = |
49914 | 421 |
(* Type information will be deleted later; skip repetition test. *) |
422 |
if is_only_type_information t then |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
423 |
Inference_Step (name, role, t, rule, deps) :: lines |
49914 | 424 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
425 |
else case take_prefix (not o is_same_inference t) lines of |
|
426 |
(* FIXME: Doesn't this code risk conflating proofs involving different |
|
427 |
types? *) |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
428 |
(_, []) => Inference_Step (name, role, t, rule, deps) :: lines |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
429 |
| (pre, Inference_Step (name', role, t', rule, _) :: post) => |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
430 |
Inference_Step (name, role, t', rule, deps) :: |
49914 | 431 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
432 |
| _ => raise Fail "unexpected inference" |
|
433 |
||
434 |
val waldmeister_conjecture_num = "1.0.0.0" |
|
435 |
||
436 |
val repair_waldmeister_endgame = |
|
437 |
let |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
438 |
fun do_tail (Inference_Step (name, role, t, rule, deps)) = |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
439 |
Inference_Step (name, role, s_not t, rule, deps) |
49914 | 440 |
| do_tail line = line |
441 |
fun do_body [] = [] |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
442 |
| do_body ((line as Inference_Step ((num, _), _, _, _, _)) :: lines) = |
49914 | 443 |
if num = waldmeister_conjecture_num then map do_tail (line :: lines) |
444 |
else line :: do_body lines |
|
445 |
| do_body (line :: lines) = line :: do_body lines |
|
446 |
in do_body end |
|
447 |
||
448 |
(* Recursively delete empty lines (type information) from the proof. *) |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
449 |
fun add_nontrivial_line (line as Inference_Step (name, _, t, _, [])) lines = |
49914 | 450 |
if is_only_type_information t then delete_dependency name lines |
451 |
else line :: lines |
|
452 |
| add_nontrivial_line line lines = line :: lines |
|
453 |
and delete_dependency name lines = |
|
454 |
fold_rev add_nontrivial_line |
|
455 |
(map (replace_dependencies_in_line (name, [])) lines) [] |
|
456 |
||
457 |
(* ATPs sometimes reuse free variable names in the strangest ways. Removing |
|
458 |
offending lines often does the trick. *) |
|
459 |
fun is_bad_free frees (Free x) = not (member (op =) frees x) |
|
460 |
| is_bad_free _ _ = false |
|
461 |
||
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
462 |
fun add_desired_line _ _ (line as Definition_Step (name, _, _)) (j, lines) = |
49914 | 463 |
(j, line :: map (replace_dependencies_in_line (name, [])) lines) |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
464 |
| add_desired_line fact_names frees |
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
465 |
(Inference_Step (name as (_, ss), role, t, rule, deps)) (j, lines) = |
49914 | 466 |
(j + 1, |
467 |
if is_fact fact_names ss orelse |
|
468 |
is_conjecture ss orelse |
|
469 |
(* the last line must be kept *) |
|
470 |
j = 0 orelse |
|
471 |
(not (is_only_type_information t) andalso |
|
472 |
null (Term.add_tvars t []) andalso |
|
473 |
not (exists_subterm (is_bad_free frees) t) andalso |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
474 |
length deps >= 2 andalso |
49914 | 475 |
(* kill next to last line, which usually results in a trivial step *) |
476 |
j <> 1) then |
|
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
477 |
Inference_Step (name, role, t, rule, deps) :: lines (* keep line *) |
49914 | 478 |
else |
479 |
map (replace_dependencies_in_line (name, deps)) lines) (* drop line *) |
|
480 |
||
481 |
val indent_size = 2 |
|
482 |
val no_label = ("", ~1) |
|
483 |
||
49883 | 484 |
fun string_for_proof ctxt type_enc lam_trans i n = |
485 |
let |
|
486 |
fun do_indent ind = replicate_string (ind * indent_size) " " |
|
487 |
fun do_free (s, T) = |
|
488 |
maybe_quote s ^ " :: " ^ |
|
50049
dd6a4655cd72
avoid messing too much with output of "string_of_term", so that it doesn't break the yxml encoding for jEdit
blanchet
parents:
50048
diff
changeset
|
489 |
maybe_quote (simplify_spaces (with_vanilla_print_mode |
50048 | 490 |
(Syntax.string_of_typ ctxt) T)) |
49883 | 491 |
fun do_label l = if l = no_label then "" else string_for_label l ^ ": " |
492 |
fun do_have qs = |
|
493 |
(if member (op =) qs Moreover then "moreover " else "") ^ |
|
494 |
(if member (op =) qs Ultimately then "ultimately " else "") ^ |
|
495 |
(if member (op =) qs Then then |
|
496 |
if member (op =) qs Show then "thus" else "hence" |
|
497 |
else |
|
498 |
if member (op =) qs Show then "show" else "have") |
|
499 |
val do_term = |
|
50048 | 500 |
annotate_types ctxt |
501 |
#> with_vanilla_print_mode (Syntax.string_of_term ctxt) |
|
50049
dd6a4655cd72
avoid messing too much with output of "string_of_term", so that it doesn't break the yxml encoding for jEdit
blanchet
parents:
50048
diff
changeset
|
502 |
#> simplify_spaces |
50048 | 503 |
#> maybe_quote |
49883 | 504 |
val reconstr = Metis (type_enc, lam_trans) |
49921 | 505 |
fun do_facts ind (ls, ss) = |
506 |
"\n" ^ do_indent (ind + 1) ^ |
|
49883 | 507 |
reconstructor_command reconstr 1 1 [] 0 |
508 |
(ls |> sort_distinct (prod_ord string_ord int_ord), |
|
509 |
ss |> sort_distinct string_ord) |
|
510 |
and do_step ind (Fix xs) = |
|
511 |
do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n" |
|
512 |
| do_step ind (Let (t1, t2)) = |
|
513 |
do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n" |
|
514 |
| do_step ind (Assume (l, t)) = |
|
515 |
do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n" |
|
516 |
| do_step ind (Prove (qs, l, t, By_Metis facts)) = |
|
517 |
do_indent ind ^ do_have qs ^ " " ^ |
|
49921 | 518 |
do_label l ^ do_term t ^ do_facts ind facts ^ "\n" |
49883 | 519 |
| do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) = |
520 |
implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind) |
|
521 |
proofs) ^ |
|
49921 | 522 |
do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ |
523 |
do_facts ind facts ^ "\n" |
|
49883 | 524 |
and do_steps prefix suffix ind steps = |
525 |
let val s = implode (map (do_step ind) steps) in |
|
526 |
replicate_string (ind * indent_size - size prefix) " " ^ prefix ^ |
|
527 |
String.extract (s, ind * indent_size, |
|
528 |
SOME (size s - ind * indent_size - 1)) ^ |
|
529 |
suffix ^ "\n" |
|
530 |
end |
|
531 |
and do_block ind proof = do_steps "{ " " }" (ind + 1) proof |
|
532 |
(* One-step proofs are pointless; better use the Metis one-liner |
|
533 |
directly. *) |
|
534 |
and do_proof [Prove (_, _, _, By_Metis _)] = "" |
|
535 |
| do_proof proof = |
|
536 |
(if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^ |
|
537 |
do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^ |
|
538 |
(if n <> 1 then "next" else "qed") |
|
539 |
in do_proof end |
|
540 |
||
49914 | 541 |
fun used_labels_of_step (Prove (_, _, _, by)) = |
542 |
(case by of |
|
543 |
By_Metis (ls, _) => ls |
|
544 |
| Case_Split (proofs, (ls, _)) => |
|
545 |
fold (union (op =) o used_labels_of) proofs ls) |
|
546 |
| used_labels_of_step _ = [] |
|
547 |
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof [] |
|
548 |
||
549 |
fun kill_useless_labels_in_proof proof = |
|
550 |
let |
|
551 |
val used_ls = used_labels_of proof |
|
552 |
fun do_label l = if member (op =) used_ls l then l else no_label |
|
553 |
fun do_step (Assume (l, t)) = Assume (do_label l, t) |
|
554 |
| do_step (Prove (qs, l, t, by)) = |
|
555 |
Prove (qs, do_label l, t, |
|
556 |
case by of |
|
557 |
Case_Split (proofs, facts) => |
|
558 |
Case_Split (map (map do_step) proofs, facts) |
|
559 |
| _ => by) |
|
560 |
| do_step step = step |
|
561 |
in map do_step proof end |
|
562 |
||
563 |
fun prefix_for_depth n = replicate_string (n + 1) |
|
564 |
||
565 |
val relabel_proof = |
|
566 |
let |
|
567 |
fun aux _ _ _ [] = [] |
|
50017 | 568 |
| aux subst depth (next_assum, next_have) (Assume (l, t) :: proof) = |
49914 | 569 |
if l = no_label then |
50017 | 570 |
Assume (l, t) :: aux subst depth (next_assum, next_have) proof |
49914 | 571 |
else |
50017 | 572 |
let val l' = (prefix_for_depth depth assume_prefix, next_assum) in |
49914 | 573 |
Assume (l', t) :: |
50017 | 574 |
aux ((l, l') :: subst) depth (next_assum + 1, next_have) proof |
49914 | 575 |
end |
50017 | 576 |
| aux subst depth (next_assum, next_have) |
49914 | 577 |
(Prove (qs, l, t, by) :: proof) = |
578 |
let |
|
50017 | 579 |
val (l', subst, next_have) = |
49914 | 580 |
if l = no_label then |
50017 | 581 |
(l, subst, next_have) |
49914 | 582 |
else |
50017 | 583 |
let val l' = (prefix_for_depth depth have_prefix, next_have) in |
584 |
(l', (l, l') :: subst, next_have + 1) |
|
585 |
end |
|
49914 | 586 |
val relabel_facts = |
587 |
apfst (maps (the_list o AList.lookup (op =) subst)) |
|
588 |
val by = |
|
589 |
case by of |
|
590 |
By_Metis facts => By_Metis (relabel_facts facts) |
|
591 |
| Case_Split (proofs, facts) => |
|
592 |
Case_Split (map (aux subst (depth + 1) (1, 1)) proofs, |
|
593 |
relabel_facts facts) |
|
594 |
in |
|
50017 | 595 |
Prove (qs, l', t, by) :: aux subst depth (next_assum, next_have) proof |
49914 | 596 |
end |
597 |
| aux subst depth nextp (step :: proof) = |
|
598 |
step :: aux subst depth nextp proof |
|
599 |
in aux [] 0 (1, 1) end |
|
600 |
||
49917 | 601 |
val merge_timeout_slack = 1.2 |
602 |
||
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
603 |
val label_ord = prod_ord int_ord fast_string_ord o pairself swap |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
604 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
605 |
structure Label_Table = Table( |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
606 |
type key = label |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
607 |
val ord = label_ord) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
608 |
|
50020 | 609 |
fun shrink_proof debug ctxt type_enc lam_trans preplay preplay_timeout |
610 |
isar_shrink proof = |
|
49883 | 611 |
let |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
612 |
(* clean vector interface *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
613 |
fun get i v = Vector.sub (v, i) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
614 |
fun replace x i v = Vector.update (v, i, x) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
615 |
fun update f i v = replace (get i v |> f) i v |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
616 |
fun v_fold_index f v s = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
617 |
Vector.foldl (fn (x, (i, s)) => (i+1, f (i, x) s)) (0, s) v |> snd |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
618 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
619 |
(* Queue interface to table *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
620 |
fun pop tab key = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
621 |
let val v = hd (Inttab.lookup_list tab key) in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
622 |
(v, Inttab.remove_list (op =) (key, v) tab) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
623 |
end |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
624 |
fun pop_max tab = pop tab (the (Inttab.max_key tab)) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
625 |
val is_empty = Inttab.is_empty |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
626 |
fun add_list tab xs = fold (Inttab.insert_list (op =)) xs tab |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
627 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
628 |
(* proof vector *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
629 |
val proof_vect = proof |> map SOME |> Vector.fromList |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
630 |
val n = Vector.length proof_vect |
50020 | 631 |
val n_target = Real.fromInt n / isar_shrink |> Real.round |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
632 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
633 |
(* table for mapping from label to proof position *) |
50257 | 634 |
fun update_table (i, Assume (label, _)) = |
635 |
Label_Table.update_new (label, i) |
|
636 |
| update_table (i, Prove (_, label, _, _)) = |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
637 |
Label_Table.update_new (label, i) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
638 |
| update_table _ = I |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
639 |
val label_index_table = fold_index update_table proof Label_Table.empty |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
640 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
641 |
(* proof references *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
642 |
fun refs (Prove (_, _, _, By_Metis (refs, _))) = |
50048 | 643 |
map (the o Label_Table.lookup label_index_table) refs |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
644 |
| refs _ = [] |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
645 |
val refed_by_vect = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
646 |
Vector.tabulate (n, (fn _ => [])) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
647 |
|> fold_index (fn (i, step) => fold (update (cons i)) (refs step)) proof |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
648 |
|> Vector.map rev (* after rev, indices are sorted in ascending order *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
649 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
650 |
(* candidates for elimination, use table as priority queue (greedy |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
651 |
algorithm) *) |
50257 | 652 |
fun add_if_cand proof_vect (i, [j]) = |
653 |
(case (the (get i proof_vect), the (get j proof_vect)) of |
|
654 |
(Prove (_, _, t, By_Metis _), Prove (_, _, _, By_Metis _)) => |
|
655 |
cons (Term.size_of_term t, i) |
|
656 |
| _ => I) |
|
657 |
| add_if_cand _ _ = I |
|
658 |
val cand_tab = |
|
659 |
v_fold_index (add_if_cand proof_vect) refed_by_vect [] |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
660 |
|> Inttab.make_list |
49883 | 661 |
|
49917 | 662 |
(* Enrich context with local facts *) |
49883 | 663 |
val thy = Proof_Context.theory_of ctxt |
50257 | 664 |
fun enrich_ctxt (Assume (label, t)) ctxt = |
665 |
Proof_Context.put_thms false |
|
666 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
667 |
| enrich_ctxt (Prove (_, label, t, _)) ctxt = |
|
50014 | 668 |
Proof_Context.put_thms false |
669 |
(string_for_label label, SOME [Skip_Proof.make_thm thy t]) ctxt |
|
50256 | 670 |
| enrich_ctxt _ ctxt = ctxt |
671 |
val rich_ctxt = fold enrich_ctxt proof ctxt |
|
49883 | 672 |
|
673 |
(* Timing *) |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
674 |
fun take_time timeout tac arg = |
49917 | 675 |
let val timing = Timing.start () in |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
676 |
(TimeLimit.timeLimit timeout tac arg; |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
677 |
Timing.result timing |> #cpu |> SOME) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
678 |
handle _ => NONE |
49883 | 679 |
end |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
680 |
val sum_up_time = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
681 |
Vector.foldl |
50102 | 682 |
((fn (SOME t, (b, s)) => (b, Time.+ (t, s)) |
683 |
| (NONE, (_, s)) => (true, Time.+ (preplay_timeout, s))) o apfst Lazy.force) |
|
50015 | 684 |
(false, seconds 0.0) |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
685 |
|
50218
d50119e69453
distinguish declated tfrees from other tfrees -- only the later can be optimized away
blanchet
parents:
50163
diff
changeset
|
686 |
(* Metis preplaying *) |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
687 |
fun try_metis timeout (Prove (_, _, t, By_Metis fact_names)) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
688 |
if not preplay then (fn () => SOME (seconds 0.0)) else |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
689 |
let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
690 |
val facts = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
691 |
fact_names |
50257 | 692 |
|>> map string_for_label |
693 |
|> op @ |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
694 |
|> map (the_single o thms_of_name rich_ctxt) |
50014 | 695 |
val goal = |
696 |
Goal.prove (Config.put Metis_Tactic.verbose debug ctxt) [] [] t |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
697 |
fun tac {context = ctxt, prems = _} = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
698 |
Metis_Tactic.metis_tac [type_enc] lam_trans ctxt facts 1 |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
699 |
in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
700 |
take_time timeout (fn () => goal tac) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
701 |
end |
50257 | 702 |
(*| try_metis timeout (Prove (_, _, t, Case_Split _)) = *) (*FIXME: Yet to be implemented *) |
703 |
| try_metis _ _ = (fn () => SOME (seconds 0.0) ) |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
704 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
705 |
(* Lazy metis time vector, cache *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
706 |
val metis_time = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
707 |
Vector.map (Lazy.lazy o try_metis preplay_timeout o the) proof_vect |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
708 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
709 |
(* Merging *) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
710 |
fun merge (Prove (qs1, label1, _, By_Metis (lfs1, gfs1))) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
711 |
(Prove (qs2, label2 , t, By_Metis (lfs2, gfs2))) = |
49883 | 712 |
let |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
713 |
val qs = inter (op =) qs1 qs2 (* FIXME: Is this correct? *) |
49913 | 714 |
|> member (op =) (union (op =) qs1 qs2) Ultimately ? cons Ultimately |
715 |
|> member (op =) qs2 Show ? cons Show |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
716 |
val ls = remove (op =) label1 lfs2 |> union (op =) lfs1 |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
717 |
val ss = union (op =) gfs1 gfs2 |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
718 |
in Prove (qs, label2, t, By_Metis (ls, ss)) end |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
719 |
fun try_merge metis_time (s1, i) (s2, j) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
720 |
(case get i metis_time |> Lazy.force of |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
721 |
NONE => (NONE, metis_time) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
722 |
| SOME t1 => |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
723 |
(case get j metis_time |> Lazy.force of |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
724 |
NONE => (NONE, metis_time) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
725 |
| SOME t2 => |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
726 |
let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
727 |
val s12 = merge s1 s2 |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
728 |
val timeout = |
50102 | 729 |
Time.+ (t1, t2) |> Time.toReal |> curry Real.* merge_timeout_slack |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
730 |
|> Time.fromReal |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
731 |
in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
732 |
case try_metis timeout s12 () of |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
733 |
NONE => (NONE, metis_time) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
734 |
| some_t12 => |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
735 |
(SOME s12, metis_time |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
736 |
|> replace (seconds 0.0 |> SOME |> Lazy.value) i |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
737 |
|> replace (Lazy.value some_t12) j) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
738 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
739 |
end)) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
740 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
741 |
fun merge_steps metis_time proof_vect refed_by cand_tab n' = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
742 |
if is_empty cand_tab orelse n' <= n_target orelse n'<3 then |
50257 | 743 |
(Vector.foldr |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
744 |
(fn (NONE, proof) => proof | (SOME s, proof) => s :: proof) |
50257 | 745 |
[] proof_vect, |
746 |
sum_up_time metis_time) |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
747 |
else |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
748 |
let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
749 |
val (i, cand_tab) = pop_max cand_tab |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
750 |
val j = get i refed_by |> the_single |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
751 |
val s1 = get i proof_vect |> the |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
752 |
val s2 = get j proof_vect |> the |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
753 |
in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
754 |
case try_merge metis_time (s1, i) (s2, j) of |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
755 |
(NONE, metis_time) => |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
756 |
merge_steps metis_time proof_vect refed_by cand_tab n' |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
757 |
| (s, metis_time) => let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
758 |
val refs = refs s1 |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
759 |
val refed_by = refed_by |> fold |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
760 |
(update (Ord_List.remove int_ord i #> Ord_List.insert int_ord j)) refs |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
761 |
val new_candidates = |
50257 | 762 |
fold (add_if_cand proof_vect) |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
763 |
(map (fn i => (i, get i refed_by)) refs) [] |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
764 |
val cand_tab = add_list cand_tab new_candidates |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
765 |
val proof_vect = proof_vect |> replace NONE i |> replace s j |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
766 |
in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
767 |
merge_steps metis_time proof_vect refed_by cand_tab (n' - 1) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
768 |
end |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
769 |
end |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
770 |
in |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
771 |
merge_steps metis_time proof_vect refed_by_vect cand_tab n |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
772 |
end |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
773 |
|
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
774 |
val chain_direct_proof = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
775 |
let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
776 |
fun succedent_of_step (Prove (_, label, _, _)) = SOME label |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
777 |
| succedent_of_step (Assume (label, _)) = SOME label |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
778 |
| succedent_of_step _ = NONE |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
779 |
fun chain_inf (SOME label0) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
780 |
(step as Prove (qs, label, t, By_Metis (lfs, gfs))) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
781 |
if member (op =) lfs label0 then |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
782 |
Prove (Then :: qs, label, t, |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
783 |
By_Metis (filter_out (curry (op =) label0) lfs, gfs)) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
784 |
else |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
785 |
step |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
786 |
| chain_inf _ (Prove (qs, label, t, Case_Split (proofs, facts))) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
787 |
Prove (qs, label, t, Case_Split ((map (chain_proof NONE) proofs), facts)) |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
788 |
| chain_inf _ step = step |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
789 |
and chain_proof _ [] = [] |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
790 |
| chain_proof (prev as SOME _) (i :: is) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
791 |
chain_inf prev i :: chain_proof (succedent_of_step i) is |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
792 |
| chain_proof _ (i :: is) = |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
793 |
i :: chain_proof (succedent_of_step i) is |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
794 |
in chain_proof NONE end |
49883 | 795 |
|
49914 | 796 |
type isar_params = |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
797 |
bool * bool * Time.time * real * string Symtab.table |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
798 |
* (string * stature) list vector * int Symtab.table * string proof * thm |
49914 | 799 |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
800 |
fun isar_proof_text ctxt isar_proofs |
50020 | 801 |
(debug, verbose, preplay_timeout, isar_shrink, pool, fact_names, sym_tab, |
802 |
atp_proof, goal) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
803 |
(one_line_params as (_, _, _, _, subgoal, subgoal_count)) = |
49883 | 804 |
let |
805 |
val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal |
|
806 |
val frees = fold Term.add_frees (concl_t :: hyp_ts) [] |
|
807 |
val one_line_proof = one_line_proof_text 0 one_line_params |
|
808 |
val type_enc = |
|
809 |
if is_typed_helper_used_in_atp_proof atp_proof then full_typesN |
|
810 |
else partial_typesN |
|
811 |
val lam_trans = lam_trans_from_atp_proof atp_proof metis_default_lam_trans |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
812 |
val preplay = preplay_timeout <> seconds 0.0 |
49883 | 813 |
|
814 |
fun isar_proof_of () = |
|
815 |
let |
|
816 |
val atp_proof = |
|
817 |
atp_proof |
|
818 |
|> clean_up_atp_proof_dependencies |
|
819 |
|> nasty_atp_proof pool |
|
820 |
|> map_term_names_in_atp_proof repair_name |
|
821 |
|> decode_lines ctxt sym_tab |
|
822 |
|> rpair [] |-> fold_rev (add_line fact_names) |
|
823 |
|> repair_waldmeister_endgame |
|
824 |
|> rpair [] |-> fold_rev add_nontrivial_line |
|
825 |
|> rpair (0, []) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
826 |
|-> fold_rev (add_desired_line fact_names frees) |
49883 | 827 |
|> snd |
828 |
val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts) |
|
829 |
val conjs = |
|
50010
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
830 |
atp_proof |> map_filter |
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
831 |
(fn Inference_Step (name as (_, ss), _, _, _, []) => |
50010
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
832 |
if member (op =) ss conj_name then SOME name else NONE |
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
833 |
| _ => NONE) |
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
834 |
val assms = |
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
835 |
atp_proof |> map_filter |
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
836 |
(fn Inference_Step (name as (_, ss), _, _, _, []) => |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
837 |
(case resolve_conjecture ss of |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
838 |
[j] => |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
839 |
if j = length hyp_ts then NONE |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
840 |
else SOME (Assume (raw_label_for_name name, nth hyp_ts j)) |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
841 |
| _ => NONE) |
50010
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
842 |
| _ => NONE) |
49883 | 843 |
fun dep_of_step (Definition_Step _) = NONE |
50012
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
844 |
| dep_of_step (Inference_Step (name, _, _, _, from)) = |
01cb92151a53
track formula roles in proofs and use that to determine whether the conjecture should be negated or not
blanchet
parents:
50010
diff
changeset
|
845 |
SOME (from, name) |
49883 | 846 |
val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph |
847 |
val axioms = axioms_of_ref_graph ref_graph conjs |
|
848 |
val tainted = tainted_atoms_of_ref_graph ref_graph conjs |
|
849 |
val props = |
|
850 |
Symtab.empty |
|
851 |
|> fold (fn Definition_Step _ => I (* FIXME *) |
|
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
852 |
| Inference_Step (name as (s, ss), role, t, _, _) => |
49883 | 853 |
Symtab.update_new (s, |
50016 | 854 |
if member (op = o apsnd fst) tainted s then |
855 |
t |> role <> Conjecture ? s_not |
|
856 |
|> fold exists_of (map Var (Term.add_vars t [])) |
|
857 |
else |
|
858 |
t)) |
|
49883 | 859 |
atp_proof |
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
860 |
(* The assumptions and conjecture are props; the rest are bools. *) |
50016 | 861 |
fun prop_of_clause [name as (s, ss)] = |
862 |
(case resolve_conjecture ss of |
|
863 |
[j] => if j = length hyp_ts then concl_t else nth hyp_ts j |
|
864 |
| _ => the_default @{term False} (Symtab.lookup props s) |
|
865 |
|> HOLogic.mk_Trueprop |> close_form) |
|
866 |
| prop_of_clause names = |
|
50018
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
867 |
let val lits = map_filter (Symtab.lookup props o fst) names in |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
868 |
case List.partition (can HOLogic.dest_not) lits of |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
869 |
(negs as _ :: _, pos as _ :: _) => |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
870 |
HOLogic.mk_imp |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
871 |
(Library.foldr1 s_conj (map HOLogic.dest_not negs), |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
872 |
Library.foldr1 s_disj pos) |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
873 |
| _ => fold (curry s_disj) lits @{term False} |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
874 |
end |
50016 | 875 |
|> HOLogic.mk_Trueprop |> close_form |
49883 | 876 |
fun maybe_show outer c = |
877 |
(outer andalso length c = 1 andalso subset (op =) (c, conjs)) |
|
878 |
? cons Show |
|
879 |
fun do_have outer qs (gamma, c) = |
|
880 |
Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c, |
|
50005 | 881 |
By_Metis (fold (add_fact_from_dependencies fact_names) gamma |
882 |
([], []))) |
|
49883 | 883 |
fun do_inf outer (Have z) = do_have outer [] z |
884 |
| do_inf outer (Cases cases) = |
|
885 |
let val c = succedent_of_cases cases in |
|
886 |
Prove (maybe_show outer c [Ultimately], label_of_clause c, |
|
887 |
prop_of_clause c, |
|
888 |
Case_Split (map (do_case false) cases, ([], []))) |
|
889 |
end |
|
890 |
and do_case outer (c, infs) = |
|
891 |
Assume (label_of_clause c, prop_of_clause c) :: |
|
892 |
map (do_inf outer) infs |
|
50257 | 893 |
val (isar_proof, ext_time) = |
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
894 |
ref_graph |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
895 |
|> redirect_graph axioms tainted |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
896 |
|> map (do_inf true) |
50010
17488e45eb5a
proper handling of assumptions arising from the goal's being expressed in rule format, for Isar proof construction
blanchet
parents:
50005
diff
changeset
|
897 |
|> append assms |
50257 | 898 |
|> relabel_proof (* FIXME: Is there a better way? *) |
899 |
|> (if not preplay andalso isar_shrink <= 1.0 |
|
900 |
then pair (true, seconds 0.0) #> swap |
|
901 |
else shrink_proof debug ctxt type_enc lam_trans preplay |
|
902 |
preplay_timeout (if isar_proofs then isar_shrink else 1000.0)) |
|
903 |
(* |>> reorder_proof_to_minimize_jumps (* ? *) *) |
|
904 |
|>> chain_direct_proof |
|
905 |
|>> kill_useless_labels_in_proof |
|
906 |
|>> relabel_proof |
|
907 |
|>> not (null params) ? cons (Fix params) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
908 |
val num_steps = length isar_proof |
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
909 |
val isar_text = |
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
910 |
string_for_proof ctxt type_enc lam_trans subgoal subgoal_count |
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
911 |
isar_proof |
49883 | 912 |
in |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
913 |
case isar_text of |
49883 | 914 |
"" => |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
915 |
if isar_proofs then |
49883 | 916 |
"\nNo structured proof available (proof too short)." |
917 |
else |
|
918 |
"" |
|
919 |
| _ => |
|
50019 | 920 |
"\n\nStructured proof" ^ |
921 |
(if verbose then |
|
922 |
" (" ^ string_of_int num_steps ^ " step" ^ plural_s num_steps ^ |
|
923 |
(if preplay then ", " ^ string_from_ext_time ext_time |
|
924 |
else "") ^ ")" |
|
925 |
else if preplay then |
|
926 |
" (" ^ string_from_ext_time ext_time ^ ")" |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
927 |
else |
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
928 |
"") ^ ":\n" ^ Sendback.markup isar_text |
49883 | 929 |
end |
930 |
val isar_proof = |
|
931 |
if debug then |
|
932 |
isar_proof_of () |
|
933 |
else case try isar_proof_of () of |
|
934 |
SOME s => s |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
935 |
| NONE => if isar_proofs then |
49883 | 936 |
"\nWarning: The Isar proof construction failed." |
937 |
else |
|
938 |
"" |
|
939 |
in one_line_proof ^ isar_proof end |
|
940 |
||
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
941 |
fun proof_text ctxt isar_proofs isar_params num_chained |
49883 | 942 |
(one_line_params as (preplay, _, _, _, _, _)) = |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
943 |
(if case preplay of Failed_to_Play _ => true | _ => isar_proofs then |
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
944 |
isar_proof_text ctxt isar_proofs isar_params |
49883 | 945 |
else |
946 |
one_line_proof_text num_chained) one_line_params |
|
947 |
||
948 |
end; |