author | smolkas |
Wed, 28 Nov 2012 12:25:43 +0100 | |
changeset 50262 | 6dc80eead659 |
parent 50261 | a1a1685b4ee8 |
child 50264 | a9ec48b98734 |
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 |
50259 | 56 |
open Sledgehammer_Isar_Reconstruct |
50258 | 57 |
open Sledgehammer_Annotate |
50259 | 58 |
open Sledgehammer_Shrink |
49914 | 59 |
|
60 |
structure String_Redirect = ATP_Proof_Redirect( |
|
61 |
type key = step_name |
|
62 |
val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s') |
|
63 |
val string_of = fst) |
|
64 |
||
49883 | 65 |
open String_Redirect |
66 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
67 |
|
49914 | 68 |
(** reconstructors **) |
69 |
||
70 |
datatype reconstructor = |
|
71 |
Metis of string * string | |
|
72 |
SMT |
|
73 |
||
74 |
datatype play = |
|
75 |
Played of reconstructor * Time.time | |
|
76 |
Trust_Playable of reconstructor * Time.time option | |
|
77 |
Failed_to_Play of reconstructor |
|
78 |
||
79 |
val smtN = "smt" |
|
80 |
||
81 |
fun string_for_reconstructor (Metis (type_enc, lam_trans)) = |
|
82 |
metis_call type_enc lam_trans |
|
83 |
| string_for_reconstructor SMT = smtN |
|
84 |
||
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
85 |
fun thms_of_name ctxt name = |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
86 |
let |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
87 |
val lex = Keyword.get_lexicons |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
88 |
val get = maps (Proof_Context.get_fact ctxt o fst) |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
89 |
in |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
90 |
Source.of_string name |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
91 |
|> Symbol.source |
49981 | 92 |
|> Token.source {do_recover = SOME false} lex Position.start |
49916
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
93 |
|> Token.source_proper |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
94 |
|> Source.source Token.stopper (Parse_Spec.xthms1 >> get) NONE |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
95 |
|> Source.exhaust |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
96 |
end |
412346127bfa
fixed theorem lookup code in Isar proof reconstruction
blanchet
parents:
49915
diff
changeset
|
97 |
|
49914 | 98 |
|
99 |
(** fact extraction from ATP proofs **) |
|
100 |
||
101 |
fun find_first_in_list_vector vec key = |
|
102 |
Vector.foldl (fn (ps, NONE) => AList.lookup (op =) ps key |
|
103 |
| (_, value) => value) NONE vec |
|
104 |
||
105 |
val unprefix_fact_number = space_implode "_" o tl o space_explode "_" |
|
106 |
||
107 |
fun resolve_one_named_fact fact_names s = |
|
108 |
case try (unprefix fact_prefix) s of |
|
109 |
SOME s' => |
|
110 |
let val s' = s' |> unprefix_fact_number |> unascii_of in |
|
111 |
s' |> find_first_in_list_vector fact_names |> Option.map (pair s') |
|
112 |
end |
|
113 |
| NONE => NONE |
|
114 |
fun resolve_fact fact_names = map_filter (resolve_one_named_fact fact_names) |
|
115 |
fun is_fact fact_names = not o null o resolve_fact fact_names |
|
116 |
||
117 |
fun resolve_one_named_conjecture s = |
|
118 |
case try (unprefix conjecture_prefix) s of |
|
119 |
SOME s' => Int.fromString s' |
|
120 |
| NONE => NONE |
|
121 |
||
122 |
val resolve_conjecture = map_filter resolve_one_named_conjecture |
|
123 |
val is_conjecture = not o null o resolve_conjecture |
|
124 |
||
125 |
val ascii_of_lam_fact_prefix = ascii_of lam_fact_prefix |
|
126 |
||
127 |
(* overapproximation (good enough) *) |
|
128 |
fun is_lam_lifted s = |
|
129 |
String.isPrefix fact_prefix s andalso |
|
130 |
String.isSubstring ascii_of_lam_fact_prefix s |
|
131 |
||
132 |
val is_combinator_def = String.isPrefix (helper_prefix ^ combinator_prefix) |
|
133 |
||
134 |
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
|
135 |
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
|
136 |
| _ => false) |
49914 | 137 |
|
138 |
fun lam_trans_from_atp_proof atp_proof default = |
|
139 |
case (is_axiom_used_in_proof is_combinator_def atp_proof, |
|
140 |
is_axiom_used_in_proof is_lam_lifted atp_proof) of |
|
141 |
(false, false) => default |
|
142 |
| (false, true) => liftingN |
|
143 |
(* | (true, true) => combs_and_liftingN -- not supported by "metis" *) |
|
144 |
| (true, _) => combsN |
|
145 |
||
146 |
val is_typed_helper_name = |
|
147 |
String.isPrefix helper_prefix andf String.isSuffix typed_helper_suffix |
|
148 |
fun is_typed_helper_used_in_atp_proof atp_proof = |
|
149 |
is_axiom_used_in_proof is_typed_helper_name atp_proof |
|
150 |
||
151 |
fun add_non_rec_defs fact_names accum = |
|
152 |
Vector.foldl (fn (facts, facts') => |
|
153 |
union (op =) (filter (fn (_, (_, status)) => status = Non_Rec_Def) facts) |
|
154 |
facts') |
|
155 |
accum fact_names |
|
156 |
||
157 |
val isa_ext = Thm.get_name_hint @{thm ext} |
|
158 |
val isa_short_ext = Long_Name.base_name isa_ext |
|
159 |
||
160 |
fun ext_name ctxt = |
|
161 |
if Thm.eq_thm_prop (@{thm ext}, |
|
162 |
singleton (Attrib.eval_thms ctxt) (Facts.named isa_short_ext, [])) then |
|
163 |
isa_short_ext |
|
164 |
else |
|
165 |
isa_ext |
|
166 |
||
167 |
val leo2_ext = "extcnf_equal_neg" |
|
168 |
val leo2_unfold_def = "unfold_def" |
|
169 |
||
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
|
170 |
fun add_fact ctxt fact_names (Inference_Step ((_, ss), _, _, rule, deps)) = |
49914 | 171 |
(if rule = leo2_ext then |
172 |
insert (op =) (ext_name ctxt, (Global, General)) |
|
173 |
else if rule = leo2_unfold_def then |
|
174 |
(* LEO 1.3.3 does not record definitions properly, leading to missing |
|
175 |
dependencies in the TSTP proof. Remove the next line once this is |
|
176 |
fixed. *) |
|
177 |
add_non_rec_defs fact_names |
|
178 |
else if rule = satallax_coreN then |
|
179 |
(fn [] => |
|
180 |
(* Satallax doesn't include definitions in its unsatisfiable cores, |
|
181 |
so we assume the worst and include them all here. *) |
|
182 |
[(ext_name ctxt, (Global, General))] |> add_non_rec_defs fact_names |
|
183 |
| facts => facts) |
|
184 |
else |
|
185 |
I) |
|
186 |
#> (if null deps then union (op =) (resolve_fact fact_names ss) |
|
187 |
else I) |
|
188 |
| add_fact _ _ _ = I |
|
189 |
||
190 |
fun used_facts_in_atp_proof ctxt fact_names atp_proof = |
|
191 |
if null atp_proof then Vector.foldl (uncurry (union (op =))) [] fact_names |
|
192 |
else fold (add_fact ctxt fact_names) atp_proof [] |
|
193 |
||
194 |
fun used_facts_in_unsound_atp_proof _ _ [] = NONE |
|
195 |
| used_facts_in_unsound_atp_proof ctxt fact_names atp_proof = |
|
196 |
let val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof in |
|
197 |
if forall (fn (_, (sc, _)) => sc = Global) used_facts andalso |
|
198 |
not (is_axiom_used_in_proof (is_conjecture o single) atp_proof) then |
|
199 |
SOME (map fst used_facts) |
|
200 |
else |
|
201 |
NONE |
|
202 |
end |
|
203 |
||
204 |
||
205 |
(** one-liner reconstructor proofs **) |
|
206 |
||
207 |
fun string_for_label (s, num) = s ^ string_of_int num |
|
208 |
||
209 |
fun show_time NONE = "" |
|
210 |
| show_time (SOME ext_time) = " (" ^ string_from_ext_time ext_time ^ ")" |
|
211 |
||
49986
90e7be285b49
took out "using only ..." comments in Sledgehammer generated metis/smt calls, until these can be generated soundly
blanchet
parents:
49983
diff
changeset
|
212 |
(* FIXME: Various bugs, esp. with "unfolding" |
49914 | 213 |
fun unusing_chained_facts _ 0 = "" |
214 |
| unusing_chained_facts used_chaineds num_chained = |
|
215 |
if length used_chaineds = num_chained then "" |
|
216 |
else if null used_chaineds then "(* using no facts *) " |
|
217 |
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
|
218 |
*) |
49914 | 219 |
|
220 |
fun apply_on_subgoal _ 1 = "by " |
|
221 |
| apply_on_subgoal 1 _ = "apply " |
|
222 |
| apply_on_subgoal i n = |
|
223 |
"prefer " ^ string_of_int i ^ " " ^ apply_on_subgoal 1 n |
|
224 |
||
225 |
fun using_labels [] = "" |
|
226 |
| using_labels ls = |
|
227 |
"using " ^ space_implode " " (map string_for_label ls) ^ " " |
|
228 |
||
229 |
fun command_call name [] = |
|
50239 | 230 |
name |> not (Symbol_Pos.is_identifier name) ? enclose "(" ")" |
49914 | 231 |
| command_call name args = "(" ^ name ^ " " ^ space_implode " " args ^ ")" |
232 |
||
233 |
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
|
234 |
(* unusing_chained_facts used_chaineds num_chained ^ *) |
49914 | 235 |
using_labels ls ^ apply_on_subgoal i n ^ |
236 |
command_call (string_for_reconstructor reconstr) ss |
|
237 |
||
238 |
fun try_command_line banner time command = |
|
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
239 |
banner ^ ": " ^ Sendback.markup command ^ show_time time ^ "." |
49914 | 240 |
|
241 |
fun minimize_line _ [] = "" |
|
242 |
| minimize_line minimize_command ss = |
|
243 |
case minimize_command ss of |
|
244 |
"" => "" |
|
245 |
| command => |
|
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
246 |
"\nTo minimize: " ^ Sendback.markup command ^ "." |
49914 | 247 |
|
248 |
fun split_used_facts facts = |
|
249 |
facts |> List.partition (fn (_, (sc, _)) => sc = Chained) |
|
250 |
|> pairself (sort_distinct (string_ord o pairself fst)) |
|
251 |
||
252 |
type minimize_command = string list -> string |
|
253 |
type one_line_params = |
|
254 |
play * string * (string * stature) list * minimize_command * int * int |
|
255 |
||
256 |
fun one_line_proof_text num_chained |
|
257 |
(preplay, banner, used_facts, minimize_command, subgoal, |
|
258 |
subgoal_count) = |
|
259 |
let |
|
260 |
val (chained, extra) = split_used_facts used_facts |
|
261 |
val (failed, reconstr, ext_time) = |
|
262 |
case preplay of |
|
263 |
Played (reconstr, time) => (false, reconstr, (SOME (false, time))) |
|
264 |
| Trust_Playable (reconstr, time) => |
|
265 |
(false, reconstr, |
|
266 |
case time of |
|
267 |
NONE => NONE |
|
268 |
| SOME time => |
|
269 |
if time = Time.zeroTime then NONE else SOME (true, time)) |
|
270 |
| Failed_to_Play reconstr => (true, reconstr, NONE) |
|
271 |
val try_line = |
|
272 |
([], map fst extra) |
|
273 |
|> reconstructor_command reconstr subgoal subgoal_count (map fst chained) |
|
274 |
num_chained |
|
275 |
|> (if failed then |
|
276 |
enclose "One-line proof reconstruction failed: " |
|
277 |
".\n(Invoking \"sledgehammer\" with \"[strict]\" might \ |
|
278 |
\solve this.)" |
|
279 |
else |
|
280 |
try_command_line banner ext_time) |
|
281 |
in try_line ^ minimize_line minimize_command (map fst (extra @ chained)) end |
|
282 |
||
283 |
||
284 |
(** Isar proof construction and manipulation **) |
|
285 |
||
50017 | 286 |
val assume_prefix = "a" |
49914 | 287 |
val have_prefix = "f" |
288 |
val raw_prefix = "x" |
|
289 |
||
290 |
fun raw_label_for_name (num, ss) = |
|
291 |
case resolve_conjecture ss of |
|
292 |
[j] => (conjecture_prefix, j) |
|
293 |
| _ => (raw_prefix ^ ascii_of num, 0) |
|
294 |
||
50005 | 295 |
fun label_of_clause [name] = raw_label_for_name name |
50262 | 296 |
| label_of_clause c = (space_implode "___" (map (fst o raw_label_for_name) c), 0) |
50005 | 297 |
|
298 |
fun add_fact_from_dependencies fact_names (names as [(_, ss)]) = |
|
299 |
if is_fact fact_names ss then |
|
300 |
apsnd (union (op =) (map fst (resolve_fact fact_names ss))) |
|
301 |
else |
|
302 |
apfst (insert (op =) (label_of_clause names)) |
|
303 |
| add_fact_from_dependencies fact_names names = |
|
304 |
apfst (insert (op =) (label_of_clause names)) |
|
49914 | 305 |
|
306 |
fun repair_name "$true" = "c_True" |
|
307 |
| repair_name "$false" = "c_False" |
|
308 |
| repair_name "$$e" = tptp_equal (* seen in Vampire proofs *) |
|
309 |
| repair_name s = |
|
310 |
if is_tptp_equal s orelse |
|
311 |
(* seen in Vampire proofs *) |
|
312 |
(String.isPrefix "sQ" s andalso String.isSuffix "_eqProxy" s) then |
|
313 |
tptp_equal |
|
314 |
else |
|
315 |
s |
|
316 |
||
317 |
fun unvarify_term (Var ((s, 0), T)) = Free (s, T) |
|
318 |
| unvarify_term t = raise TERM ("unvarify_term: non-Var", [t]) |
|
319 |
||
320 |
fun infer_formula_types ctxt = |
|
321 |
Type.constraint HOLogic.boolT |
|
322 |
#> Syntax.check_term |
|
323 |
(Proof_Context.set_mode Proof_Context.mode_schematic ctxt) |
|
324 |
||
325 |
val combinator_table = |
|
326 |
[(@{const_name Meson.COMBI}, @{thm Meson.COMBI_def [abs_def]}), |
|
327 |
(@{const_name Meson.COMBK}, @{thm Meson.COMBK_def [abs_def]}), |
|
328 |
(@{const_name Meson.COMBB}, @{thm Meson.COMBB_def [abs_def]}), |
|
329 |
(@{const_name Meson.COMBC}, @{thm Meson.COMBC_def [abs_def]}), |
|
330 |
(@{const_name Meson.COMBS}, @{thm Meson.COMBS_def [abs_def]})] |
|
331 |
||
332 |
fun uncombine_term thy = |
|
333 |
let |
|
334 |
fun aux (t1 $ t2) = betapply (pairself aux (t1, t2)) |
|
335 |
| aux (Abs (s, T, t')) = Abs (s, T, aux t') |
|
336 |
| aux (t as Const (x as (s, _))) = |
|
337 |
(case AList.lookup (op =) combinator_table s of |
|
338 |
SOME thm => thm |> prop_of |> specialize_type thy x |
|
339 |
|> Logic.dest_equals |> snd |
|
340 |
| NONE => t) |
|
341 |
| aux t = t |
|
342 |
in aux end |
|
343 |
||
344 |
fun decode_line sym_tab (Definition_Step (name, phi1, phi2)) ctxt = |
|
345 |
let |
|
346 |
val thy = Proof_Context.theory_of ctxt |
|
347 |
val t1 = prop_from_atp ctxt true sym_tab phi1 |
|
348 |
val vars = snd (strip_comb t1) |
|
349 |
val frees = map unvarify_term vars |
|
350 |
val unvarify_args = subst_atomic (vars ~~ frees) |
|
351 |
val t2 = prop_from_atp ctxt true sym_tab phi2 |
|
352 |
val (t1, t2) = |
|
353 |
HOLogic.eq_const HOLogic.typeT $ t1 $ t2 |
|
354 |
|> unvarify_args |> uncombine_term thy |> infer_formula_types ctxt |
|
355 |
|> HOLogic.dest_eq |
|
356 |
in |
|
357 |
(Definition_Step (name, t1, t2), |
|
358 |
fold Variable.declare_term (maps Misc_Legacy.term_frees [t1, t2]) ctxt) |
|
359 |
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
|
360 |
| decode_line sym_tab (Inference_Step (name, role, u, rule, deps)) ctxt = |
49914 | 361 |
let |
362 |
val thy = Proof_Context.theory_of ctxt |
|
363 |
val t = u |> prop_from_atp ctxt true sym_tab |
|
364 |
|> uncombine_term thy |> infer_formula_types ctxt |
|
365 |
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
|
366 |
(Inference_Step (name, role, t, rule, deps), |
49914 | 367 |
fold Variable.declare_term (Misc_Legacy.term_frees t) ctxt) |
368 |
end |
|
369 |
fun decode_lines ctxt sym_tab lines = |
|
370 |
fst (fold_map (decode_line sym_tab) lines ctxt) |
|
371 |
||
372 |
fun replace_one_dependency (old, new) dep = |
|
373 |
if is_same_atp_step dep old then new else [dep] |
|
374 |
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
|
375 |
| 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
|
376 |
(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
|
377 |
Inference_Step (name, role, t, rule, |
49914 | 378 |
fold (union (op =) o replace_one_dependency p) deps []) |
379 |
||
380 |
(* No "real" literals means only type information (tfree_tcs, clsrel, or |
|
381 |
clsarity). *) |
|
382 |
fun is_only_type_information t = t aconv @{term True} |
|
383 |
||
384 |
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
|
385 |
| is_same_inference t (Inference_Step (_, _, t', _, _)) = t aconv t' |
49914 | 386 |
|
387 |
(* Discard facts; consolidate adjacent lines that prove the same formula, since |
|
388 |
they differ only in type information.*) |
|
389 |
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
|
390 |
| 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
|
391 |
lines = |
49914 | 392 |
(* No dependencies: fact, conjecture, or (for Vampire) internal facts or |
393 |
definitions. *) |
|
394 |
if is_fact fact_names ss then |
|
395 |
(* Facts are not proof lines. *) |
|
396 |
if is_only_type_information t then |
|
397 |
map (replace_dependencies_in_line (name, [])) lines |
|
398 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
|
399 |
else case take_prefix (not o is_same_inference t) lines of |
|
400 |
(_, []) => 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
|
401 |
| (pre, Inference_Step (name', _, _, _, _) :: post) => |
49914 | 402 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
403 |
| _ => raise Fail "unexpected inference" |
|
404 |
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
|
405 |
Inference_Step (name, role, t, rule, []) :: lines |
49914 | 406 |
else |
407 |
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
|
408 |
| add_line _ (Inference_Step (name, role, t, rule, deps)) lines = |
49914 | 409 |
(* Type information will be deleted later; skip repetition test. *) |
410 |
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
|
411 |
Inference_Step (name, role, t, rule, deps) :: lines |
49914 | 412 |
(* Is there a repetition? If so, replace later line by earlier one. *) |
413 |
else case take_prefix (not o is_same_inference t) lines of |
|
414 |
(* FIXME: Doesn't this code risk conflating proofs involving different |
|
415 |
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
|
416 |
(_, []) => 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
|
417 |
| (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
|
418 |
Inference_Step (name, role, t', rule, deps) :: |
49914 | 419 |
pre @ map (replace_dependencies_in_line (name', [name])) post |
420 |
| _ => raise Fail "unexpected inference" |
|
421 |
||
422 |
val waldmeister_conjecture_num = "1.0.0.0" |
|
423 |
||
424 |
val repair_waldmeister_endgame = |
|
425 |
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
|
426 |
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
|
427 |
Inference_Step (name, role, s_not t, rule, deps) |
49914 | 428 |
| do_tail line = line |
429 |
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
|
430 |
| do_body ((line as Inference_Step ((num, _), _, _, _, _)) :: lines) = |
49914 | 431 |
if num = waldmeister_conjecture_num then map do_tail (line :: lines) |
432 |
else line :: do_body lines |
|
433 |
| do_body (line :: lines) = line :: do_body lines |
|
434 |
in do_body end |
|
435 |
||
436 |
(* 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
|
437 |
fun add_nontrivial_line (line as Inference_Step (name, _, t, _, [])) lines = |
49914 | 438 |
if is_only_type_information t then delete_dependency name lines |
439 |
else line :: lines |
|
440 |
| add_nontrivial_line line lines = line :: lines |
|
441 |
and delete_dependency name lines = |
|
442 |
fold_rev add_nontrivial_line |
|
443 |
(map (replace_dependencies_in_line (name, [])) lines) [] |
|
444 |
||
445 |
(* ATPs sometimes reuse free variable names in the strangest ways. Removing |
|
446 |
offending lines often does the trick. *) |
|
447 |
fun is_bad_free frees (Free x) = not (member (op =) frees x) |
|
448 |
| is_bad_free _ _ = false |
|
449 |
||
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
450 |
fun add_desired_line _ _ (line as Definition_Step (name, _, _)) (j, lines) = |
49914 | 451 |
(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
|
452 |
| 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
|
453 |
(Inference_Step (name as (_, ss), role, t, rule, deps)) (j, lines) = |
49914 | 454 |
(j + 1, |
455 |
if is_fact fact_names ss orelse |
|
456 |
is_conjecture ss orelse |
|
457 |
(* the last line must be kept *) |
|
458 |
j = 0 orelse |
|
459 |
(not (is_only_type_information t) andalso |
|
460 |
null (Term.add_tvars t []) andalso |
|
461 |
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
|
462 |
length deps >= 2 andalso |
49914 | 463 |
(* kill next to last line, which usually results in a trivial step *) |
464 |
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
|
465 |
Inference_Step (name, role, t, rule, deps) :: lines (* keep line *) |
49914 | 466 |
else |
467 |
map (replace_dependencies_in_line (name, deps)) lines) (* drop line *) |
|
468 |
||
469 |
val indent_size = 2 |
|
470 |
val no_label = ("", ~1) |
|
471 |
||
49883 | 472 |
fun string_for_proof ctxt type_enc lam_trans i n = |
473 |
let |
|
474 |
fun do_indent ind = replicate_string (ind * indent_size) " " |
|
475 |
fun do_free (s, T) = |
|
476 |
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
|
477 |
maybe_quote (simplify_spaces (with_vanilla_print_mode |
50048 | 478 |
(Syntax.string_of_typ ctxt) T)) |
49883 | 479 |
fun do_label l = if l = no_label then "" else string_for_label l ^ ": " |
480 |
fun do_have qs = |
|
481 |
(if member (op =) qs Ultimately then "ultimately " else "") ^ |
|
482 |
(if member (op =) qs Then then |
|
483 |
if member (op =) qs Show then "thus" else "hence" |
|
484 |
else |
|
485 |
if member (op =) qs Show then "show" else "have") |
|
486 |
val do_term = |
|
50048 | 487 |
annotate_types ctxt |
488 |
#> 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
|
489 |
#> simplify_spaces |
50048 | 490 |
#> maybe_quote |
49883 | 491 |
val reconstr = Metis (type_enc, lam_trans) |
49921 | 492 |
fun do_facts ind (ls, ss) = |
493 |
"\n" ^ do_indent (ind + 1) ^ |
|
49883 | 494 |
reconstructor_command reconstr 1 1 [] 0 |
495 |
(ls |> sort_distinct (prod_ord string_ord int_ord), |
|
496 |
ss |> sort_distinct string_ord) |
|
497 |
and do_step ind (Fix xs) = |
|
498 |
do_indent ind ^ "fix " ^ space_implode " and " (map do_free xs) ^ "\n" |
|
499 |
| do_step ind (Let (t1, t2)) = |
|
500 |
do_indent ind ^ "let " ^ do_term t1 ^ " = " ^ do_term t2 ^ "\n" |
|
501 |
| do_step ind (Assume (l, t)) = |
|
502 |
do_indent ind ^ "assume " ^ do_label l ^ do_term t ^ "\n" |
|
503 |
| do_step ind (Prove (qs, l, t, By_Metis facts)) = |
|
504 |
do_indent ind ^ do_have qs ^ " " ^ |
|
49921 | 505 |
do_label l ^ do_term t ^ do_facts ind facts ^ "\n" |
49883 | 506 |
| do_step ind (Prove (qs, l, t, Case_Split (proofs, facts))) = |
507 |
implode (map (prefix (do_indent ind ^ "moreover\n") o do_block ind) |
|
508 |
proofs) ^ |
|
49921 | 509 |
do_indent ind ^ do_have qs ^ " " ^ do_label l ^ do_term t ^ |
510 |
do_facts ind facts ^ "\n" |
|
49883 | 511 |
and do_steps prefix suffix ind steps = |
512 |
let val s = implode (map (do_step ind) steps) in |
|
513 |
replicate_string (ind * indent_size - size prefix) " " ^ prefix ^ |
|
514 |
String.extract (s, ind * indent_size, |
|
515 |
SOME (size s - ind * indent_size - 1)) ^ |
|
516 |
suffix ^ "\n" |
|
517 |
end |
|
518 |
and do_block ind proof = do_steps "{ " " }" (ind + 1) proof |
|
519 |
(* One-step proofs are pointless; better use the Metis one-liner |
|
520 |
directly. *) |
|
521 |
and do_proof [Prove (_, _, _, By_Metis _)] = "" |
|
522 |
| do_proof proof = |
|
523 |
(if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^ |
|
524 |
do_indent 0 ^ "proof -\n" ^ do_steps "" "" 1 proof ^ do_indent 0 ^ |
|
525 |
(if n <> 1 then "next" else "qed") |
|
526 |
in do_proof end |
|
527 |
||
49914 | 528 |
fun used_labels_of_step (Prove (_, _, _, by)) = |
529 |
(case by of |
|
530 |
By_Metis (ls, _) => ls |
|
531 |
| Case_Split (proofs, (ls, _)) => |
|
532 |
fold (union (op =) o used_labels_of) proofs ls) |
|
533 |
| used_labels_of_step _ = [] |
|
534 |
and used_labels_of proof = fold (union (op =) o used_labels_of_step) proof [] |
|
535 |
||
536 |
fun kill_useless_labels_in_proof proof = |
|
537 |
let |
|
538 |
val used_ls = used_labels_of proof |
|
539 |
fun do_label l = if member (op =) used_ls l then l else no_label |
|
540 |
fun do_step (Assume (l, t)) = Assume (do_label l, t) |
|
541 |
| do_step (Prove (qs, l, t, by)) = |
|
542 |
Prove (qs, do_label l, t, |
|
543 |
case by of |
|
544 |
Case_Split (proofs, facts) => |
|
545 |
Case_Split (map (map do_step) proofs, facts) |
|
546 |
| _ => by) |
|
547 |
| do_step step = step |
|
548 |
in map do_step proof end |
|
549 |
||
550 |
fun prefix_for_depth n = replicate_string (n + 1) |
|
551 |
||
552 |
val relabel_proof = |
|
553 |
let |
|
554 |
fun aux _ _ _ [] = [] |
|
50017 | 555 |
| aux subst depth (next_assum, next_have) (Assume (l, t) :: proof) = |
49914 | 556 |
if l = no_label then |
50017 | 557 |
Assume (l, t) :: aux subst depth (next_assum, next_have) proof |
49914 | 558 |
else |
50017 | 559 |
let val l' = (prefix_for_depth depth assume_prefix, next_assum) in |
49914 | 560 |
Assume (l', t) :: |
50017 | 561 |
aux ((l, l') :: subst) depth (next_assum + 1, next_have) proof |
49914 | 562 |
end |
50017 | 563 |
| aux subst depth (next_assum, next_have) |
49914 | 564 |
(Prove (qs, l, t, by) :: proof) = |
565 |
let |
|
50017 | 566 |
val (l', subst, next_have) = |
49914 | 567 |
if l = no_label then |
50017 | 568 |
(l, subst, next_have) |
49914 | 569 |
else |
50017 | 570 |
let val l' = (prefix_for_depth depth have_prefix, next_have) in |
571 |
(l', (l, l') :: subst, next_have + 1) |
|
572 |
end |
|
49914 | 573 |
val relabel_facts = |
574 |
apfst (maps (the_list o AList.lookup (op =) subst)) |
|
575 |
val by = |
|
576 |
case by of |
|
577 |
By_Metis facts => By_Metis (relabel_facts facts) |
|
578 |
| Case_Split (proofs, facts) => |
|
579 |
Case_Split (map (aux subst (depth + 1) (1, 1)) proofs, |
|
580 |
relabel_facts facts) |
|
581 |
in |
|
50017 | 582 |
Prove (qs, l', t, by) :: aux subst depth (next_assum, next_have) proof |
49914 | 583 |
end |
584 |
| aux subst depth nextp (step :: proof) = |
|
585 |
step :: aux subst depth nextp proof |
|
586 |
in aux [] 0 (1, 1) end |
|
587 |
||
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
588 |
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
|
589 |
let |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
590 |
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
|
591 |
| 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
|
592 |
| 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
|
593 |
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
|
594 |
(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
|
595 |
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
|
596 |
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
|
597 |
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
|
598 |
else |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
599 |
step |
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
600 |
| 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
|
601 |
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
|
602 |
| 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
|
603 |
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
|
604 |
| 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
|
605 |
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
|
606 |
| 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
|
607 |
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
|
608 |
in chain_proof NONE end |
49883 | 609 |
|
49914 | 610 |
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
|
611 |
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
|
612 |
* (string * stature) list vector * int Symtab.table * string proof * thm |
49914 | 613 |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
614 |
fun isar_proof_text ctxt isar_proofs |
50020 | 615 |
(debug, verbose, preplay_timeout, isar_shrink, pool, fact_names, sym_tab, |
616 |
atp_proof, goal) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
617 |
(one_line_params as (_, _, _, _, subgoal, subgoal_count)) = |
49883 | 618 |
let |
619 |
val (params, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal |
|
620 |
val frees = fold Term.add_frees (concl_t :: hyp_ts) [] |
|
621 |
val one_line_proof = one_line_proof_text 0 one_line_params |
|
622 |
val type_enc = |
|
623 |
if is_typed_helper_used_in_atp_proof atp_proof then full_typesN |
|
624 |
else partial_typesN |
|
625 |
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
|
626 |
val preplay = preplay_timeout <> seconds 0.0 |
49883 | 627 |
|
628 |
fun isar_proof_of () = |
|
629 |
let |
|
630 |
val atp_proof = |
|
631 |
atp_proof |
|
632 |
|> clean_up_atp_proof_dependencies |
|
633 |
|> nasty_atp_proof pool |
|
634 |
|> map_term_names_in_atp_proof repair_name |
|
635 |
|> decode_lines ctxt sym_tab |
|
636 |
|> rpair [] |-> fold_rev (add_line fact_names) |
|
637 |
|> repair_waldmeister_endgame |
|
638 |
|> rpair [] |-> fold_rev add_nontrivial_line |
|
639 |
|> rpair (0, []) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
640 |
|-> fold_rev (add_desired_line fact_names frees) |
49883 | 641 |
|> snd |
642 |
val conj_name = conjecture_prefix ^ string_of_int (length hyp_ts) |
|
643 |
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
|
644 |
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
|
645 |
(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
|
646 |
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
|
647 |
| _ => 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
|
648 |
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
|
649 |
atp_proof |> map_filter |
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
650 |
(fn Inference_Step (name as (_, ss), _, _, _, []) => |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
651 |
(case resolve_conjecture ss of |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
652 |
[j] => |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
653 |
if j = length hyp_ts then NONE |
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
654 |
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
|
655 |
| _ => 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
|
656 |
| _ => NONE) |
49883 | 657 |
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
|
658 |
| 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
|
659 |
SOME (from, name) |
49883 | 660 |
val ref_graph = atp_proof |> map_filter dep_of_step |> make_ref_graph |
661 |
val axioms = axioms_of_ref_graph ref_graph conjs |
|
662 |
val tainted = tainted_atoms_of_ref_graph ref_graph conjs |
|
663 |
val props = |
|
664 |
Symtab.empty |
|
665 |
|> fold (fn Definition_Step _ => I (* FIXME *) |
|
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
666 |
| Inference_Step (name as (s, ss), role, t, _, _) => |
49883 | 667 |
Symtab.update_new (s, |
50016 | 668 |
if member (op = o apsnd fst) tainted s then |
669 |
t |> role <> Conjecture ? s_not |
|
670 |
|> fold exists_of (map Var (Term.add_vars t [])) |
|
671 |
else |
|
672 |
t)) |
|
49883 | 673 |
atp_proof |
50013
cceec179bdca
use original formulas for hypotheses and conclusion to avoid mismatches
blanchet
parents:
50012
diff
changeset
|
674 |
(* The assumptions and conjecture are props; the rest are bools. *) |
50016 | 675 |
fun prop_of_clause [name as (s, ss)] = |
676 |
(case resolve_conjecture ss of |
|
677 |
[j] => if j = length hyp_ts then concl_t else nth hyp_ts j |
|
678 |
| _ => the_default @{term False} (Symtab.lookup props s) |
|
679 |
|> HOLogic.mk_Trueprop |> close_form) |
|
680 |
| prop_of_clause names = |
|
50018
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
681 |
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
|
682 |
case List.partition (can HOLogic.dest_not) lits of |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
683 |
(negs as _ :: _, pos as _ :: _) => |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
684 |
HOLogic.mk_imp |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
685 |
(Library.foldr1 s_conj (map HOLogic.dest_not negs), |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
686 |
Library.foldr1 s_disj pos) |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
687 |
| _ => fold (curry s_disj) lits @{term False} |
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
688 |
end |
50016 | 689 |
|> HOLogic.mk_Trueprop |> close_form |
49883 | 690 |
fun maybe_show outer c = |
691 |
(outer andalso length c = 1 andalso subset (op =) (c, conjs)) |
|
692 |
? cons Show |
|
693 |
fun do_have outer qs (gamma, c) = |
|
694 |
Prove (maybe_show outer c qs, label_of_clause c, prop_of_clause c, |
|
50005 | 695 |
By_Metis (fold (add_fact_from_dependencies fact_names) gamma |
696 |
([], []))) |
|
49883 | 697 |
fun do_inf outer (Have z) = do_have outer [] z |
698 |
| do_inf outer (Cases cases) = |
|
699 |
let val c = succedent_of_cases cases in |
|
700 |
Prove (maybe_show outer c [Ultimately], label_of_clause c, |
|
701 |
prop_of_clause c, |
|
702 |
Case_Split (map (do_case false) cases, ([], []))) |
|
703 |
end |
|
704 |
and do_case outer (c, infs) = |
|
705 |
Assume (label_of_clause c, prop_of_clause c) :: |
|
706 |
map (do_inf outer) infs |
|
50257 | 707 |
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
|
708 |
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
|
709 |
|> 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
|
710 |
|> 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
|
711 |
|> append assms |
50257 | 712 |
|> (if not preplay andalso isar_shrink <= 1.0 |
713 |
then pair (true, seconds 0.0) #> swap |
|
714 |
else shrink_proof debug ctxt type_enc lam_trans preplay |
|
715 |
preplay_timeout (if isar_proofs then isar_shrink else 1000.0)) |
|
716 |
(* |>> reorder_proof_to_minimize_jumps (* ? *) *) |
|
717 |
|>> chain_direct_proof |
|
718 |
|>> kill_useless_labels_in_proof |
|
719 |
|>> relabel_proof |
|
720 |
|>> not (null params) ? cons (Fix params) |
|
50259 | 721 |
val num_steps = metis_steps_recursive isar_proof |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
722 |
val isar_text = |
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
723 |
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
|
724 |
isar_proof |
49883 | 725 |
in |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
726 |
case isar_text of |
49883 | 727 |
"" => |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
728 |
if isar_proofs then |
49883 | 729 |
"\nNo structured proof available (proof too short)." |
730 |
else |
|
731 |
"" |
|
732 |
| _ => |
|
50019 | 733 |
"\n\nStructured proof" ^ |
734 |
(if verbose then |
|
50259 | 735 |
" (" ^ string_of_int num_steps ^ " metis step" ^ plural_s num_steps ^ |
50019 | 736 |
(if preplay then ", " ^ string_from_ext_time ext_time |
737 |
else "") ^ ")" |
|
738 |
else if preplay then |
|
739 |
" (" ^ string_from_ext_time ext_time ^ ")" |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
740 |
else |
50163
c62ce309dc26
more abstract Sendback operations, with explicit id/exec_id properties;
wenzelm
parents:
50102
diff
changeset
|
741 |
"") ^ ":\n" ^ Sendback.markup isar_text |
49883 | 742 |
end |
743 |
val isar_proof = |
|
744 |
if debug then |
|
745 |
isar_proof_of () |
|
746 |
else case try isar_proof_of () of |
|
747 |
SOME s => s |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
748 |
| NONE => if isar_proofs then |
49883 | 749 |
"\nWarning: The Isar proof construction failed." |
750 |
else |
|
751 |
"" |
|
752 |
in one_line_proof ^ isar_proof end |
|
753 |
||
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
754 |
fun proof_text ctxt isar_proofs isar_params num_chained |
49883 | 755 |
(one_line_params as (preplay, _, _, _, _, _)) = |
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
756 |
(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
|
757 |
isar_proof_text ctxt isar_proofs isar_params |
49883 | 758 |
else |
759 |
one_line_proof_text num_chained) one_line_params |
|
760 |
||
761 |
end; |