author | blanchet |
Sun, 15 Dec 2013 20:31:25 +0100 | |
changeset 54759 | b632390b5966 |
parent 54758 | ba488d89614a |
child 54760 | a1ac3eaa0d11 |
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 |
||
52374 | 8 |
signature SLEDGEHAMMER_RECONSTRUCT = |
49883 | 9 |
sig |
54495 | 10 |
type ('a, 'b) atp_step = ('a, 'b) ATP_Proof.atp_step |
53586
bd5fa6425993
prefixed types and some functions with "atp_" for disambiguation
blanchet
parents:
53052
diff
changeset
|
11 |
type 'a atp_proof = 'a ATP_Proof.atp_proof |
49914 | 12 |
type stature = ATP_Problem_Generate.stature |
54495 | 13 |
type one_line_params = Sledgehammer_Reconstructor.one_line_params |
49914 | 14 |
|
15 |
type isar_params = |
|
54505 | 16 |
bool * bool * string * string * Time.time option * real * bool * (term, string) atp_step list * |
17 |
thm |
|
49914 | 18 |
|
49883 | 19 |
val isar_proof_text : |
53052
a0db255af8c5
sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents:
53047
diff
changeset
|
20 |
Proof.context -> bool option -> isar_params -> one_line_params -> string |
49883 | 21 |
val proof_text : |
54500 | 22 |
Proof.context -> bool option -> (unit -> isar_params) -> int -> one_line_params -> string |
49883 | 23 |
end; |
24 |
||
54751 | 25 |
structure Sledgehammer_Reconstruct : SLEDGEHAMMER_RECONSTRUCT = |
49883 | 26 |
struct |
27 |
||
28 |
open ATP_Util |
|
49914 | 29 |
open ATP_Problem |
49883 | 30 |
open ATP_Proof |
31 |
open ATP_Problem_Generate |
|
32 |
open ATP_Proof_Reconstruct |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
33 |
open Sledgehammer_Util |
52555 | 34 |
open Sledgehammer_Reconstructor |
50264
a9ec48b98734
renamed sledgehammer_isar_reconstruct to sledgehammer_proof
smolkas
parents:
50262
diff
changeset
|
35 |
open Sledgehammer_Proof |
50258 | 36 |
open Sledgehammer_Annotate |
52555 | 37 |
open Sledgehammer_Print |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
38 |
open Sledgehammer_Preplay |
51130
76d68444cd59
renamed sledgehammer_shrink to sledgehammer_compress
smolkas
parents:
51129
diff
changeset
|
39 |
open Sledgehammer_Compress |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52556
diff
changeset
|
40 |
open Sledgehammer_Try0 |
52611
831f7479c74f
minimize dependencies (used facts) of Isar proof steps; remove unreferenced steps
smolkas
parents:
52592
diff
changeset
|
41 |
open Sledgehammer_Minimize_Isar |
49914 | 42 |
|
43 |
structure String_Redirect = ATP_Proof_Redirect( |
|
53586
bd5fa6425993
prefixed types and some functions with "atp_" for disambiguation
blanchet
parents:
53052
diff
changeset
|
44 |
type key = atp_step_name |
49914 | 45 |
val ord = fn ((s, _ : string list), (s', _)) => fast_string_ord (s, s') |
46 |
val string_of = fst) |
|
47 |
||
49883 | 48 |
open String_Redirect |
49 |
||
54746 | 50 |
val e_skolemize_rule = "skolemize" |
51 |
val vampire_skolemisation_rule = "skolemisation" |
|
52 |
(* TODO: Use "Z3_Proof.string_of_rule" once it is moved to Isabelle *) |
|
54753 | 53 |
val z3_apply_def_rule = "apply-def" |
54746 | 54 |
val z3_hypothesis_rule = "hypothesis" |
54753 | 55 |
val z3_intro_def_rule = "intro-def" |
54746 | 56 |
val z3_lemma_rule = "lemma" |
54753 | 57 |
val z3_skolemize_rule = "sk" |
58 |
val z3_th_lemma_rule = "th-lemma" |
|
54746 | 59 |
|
60 |
val is_skolemize_rule = |
|
61 |
member (op =) [e_skolemize_rule, vampire_skolemisation_rule, z3_skolemize_rule] |
|
62 |
||
54755 | 63 |
val is_arith_rule = String.isPrefix z3_th_lemma_rule |
64 |
||
54501 | 65 |
fun raw_label_of_num num = (num, 0) |
49914 | 66 |
|
54501 | 67 |
fun label_of_clause [(num, _)] = raw_label_of_num num |
68 |
| label_of_clause c = (space_implode "___" (map (fst o raw_label_of_num o fst) c), 0) |
|
50005 | 69 |
|
54505 | 70 |
fun add_fact_of_dependencies [(_, ss as _ :: _)] = apsnd (union (op =) ss) |
71 |
| add_fact_of_dependencies names = apfst (insert (op =) (label_of_clause names)) |
|
49914 | 72 |
|
73 |
fun replace_one_dependency (old, new) dep = |
|
74 |
if is_same_atp_step dep old then new else [dep] |
|
51201 | 75 |
fun replace_dependencies_in_line p (name, role, t, rule, deps) = |
76 |
(name, role, t, rule, fold (union (op =) o replace_one_dependency p) deps []) |
|
49914 | 77 |
|
54746 | 78 |
fun inline_z3_defs _ [] = [] |
79 |
| inline_z3_defs defs ((line as (name, role, t, rule, deps)) :: lines) = |
|
80 |
if rule = z3_intro_def_rule then |
|
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54756
diff
changeset
|
81 |
let val def = t |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> swap in |
54746 | 82 |
inline_z3_defs (insert (op =) def defs) |
83 |
(map (replace_dependencies_in_line (name, [])) lines) |
|
84 |
end |
|
85 |
else if rule = z3_apply_def_rule then |
|
86 |
inline_z3_defs defs (map (replace_dependencies_in_line (name, [])) lines) |
|
87 |
else |
|
88 |
(name, role, Term.subst_atomic defs t, rule, deps) :: inline_z3_defs defs lines |
|
89 |
||
54750 | 90 |
fun alist_cons_list eq (k, v) = AList.map_default eq (k, []) (cons v) |
91 |
||
92 |
fun add_z3_hypotheses [] = I |
|
93 |
| add_z3_hypotheses hyps = |
|
94 |
HOLogic.dest_Trueprop |
|
54758 | 95 |
#> curry s_imp (Library.foldr1 s_conj (map HOLogic.dest_Trueprop hyps)) |
54750 | 96 |
#> HOLogic.mk_Trueprop |
97 |
||
98 |
fun inline_z3_hypotheses _ _ [] = [] |
|
99 |
| inline_z3_hypotheses hyp_names hyps ((name, role, t, rule, deps) :: lines) = |
|
100 |
if rule = z3_hypothesis_rule then |
|
101 |
inline_z3_hypotheses (name :: hyp_names) (alist_cons_list (op =) (t, name) hyps) lines |
|
102 |
else |
|
103 |
let val deps' = subtract (op =) hyp_names deps in |
|
104 |
if rule = z3_lemma_rule then |
|
105 |
(name, role, t, rule, deps') :: inline_z3_hypotheses hyp_names hyps lines |
|
106 |
else |
|
107 |
let |
|
108 |
val add_hyps = filter_out (null o inter (op =) deps o snd) hyps |
|
109 |
val t' = add_z3_hypotheses (map fst add_hyps) t |
|
110 |
val deps' = subtract (op =) hyp_names deps |
|
111 |
val hyps' = fold (AList.update (op =) o apsnd (insert (op =) name)) add_hyps hyps |
|
112 |
in |
|
113 |
(name, role, t', rule, deps') :: inline_z3_hypotheses hyp_names hyps' lines |
|
114 |
end |
|
115 |
end |
|
54746 | 116 |
|
54758 | 117 |
fun simplify_prop (@{const Not} $ t) = s_not (simplify_prop t) |
118 |
| simplify_prop (@{const conj} $ t $ u) = s_conj (simplify_prop t, simplify_prop u) |
|
119 |
| simplify_prop (@{const disj} $ t $ u) = s_disj (simplify_prop t, simplify_prop u) |
|
120 |
| simplify_prop (@{const implies} $ t $ u) = s_imp (simplify_prop t, simplify_prop u) |
|
121 |
| simplify_prop (@{const HOL.eq (bool)} $ t $ u) = s_iff (simplify_prop t, simplify_prop u) |
|
122 |
| simplify_prop (t $ u) = simplify_prop t $ simplify_prop u |
|
123 |
| simplify_prop (Abs (s, T, t)) = Abs (s, T, simplify_prop t) |
|
124 |
| simplify_prop t = t |
|
49914 | 125 |
|
54758 | 126 |
fun simplify_line (name, role, t, rule, deps) = (name, role, simplify_prop t, rule, deps) |
127 |
||
128 |
(* No "real" literals means only type information (tfree_tcs, clsrel, or clsarity). *) |
|
129 |
fun is_only_type_information t = t aconv @{prop True} |
|
130 |
||
131 |
(* Discard facts; consolidate adjacent lines that prove the same formula, since they differ only in |
|
54759 | 132 |
type information. *) |
54755 | 133 |
fun add_line_pass1 (line as (name as (_, ss), role, t, rule, [])) lines = |
54700 | 134 |
(* No dependencies: lemma (for Z3), fact, conjecture, or (for Vampire) |
135 |
internal facts or definitions. *) |
|
136 |
if role = Lemma orelse role = Conjecture orelse role = Negated_Conjecture orelse |
|
54755 | 137 |
role = Hypothesis orelse is_arith_rule rule then |
54746 | 138 |
line :: lines |
54505 | 139 |
else if role = Axiom then |
49914 | 140 |
(* Facts are not proof lines. *) |
54507 | 141 |
lines |> is_only_type_information t ? map (replace_dependencies_in_line (name, [])) |
49914 | 142 |
else |
143 |
map (replace_dependencies_in_line (name, [])) lines |
|
54755 | 144 |
| add_line_pass1 line lines = line :: lines |
49914 | 145 |
|
54758 | 146 |
(* Recursively delete empty lines (type information) from the proof. |
147 |
(FIXME: needed? And why is "delete_dependency" so complicated?) *) |
|
54755 | 148 |
fun add_line_pass2 (line as (name, _, t, _, [])) lines = |
54507 | 149 |
if is_only_type_information t then delete_dependency name lines else line :: lines |
54755 | 150 |
| add_line_pass2 line lines = line :: lines |
49914 | 151 |
and delete_dependency name lines = |
54755 | 152 |
fold_rev add_line_pass2 (map (replace_dependencies_in_line (name, [])) lines) [] |
49914 | 153 |
|
54755 | 154 |
fun add_line_pass3 res [] = rev res |
54758 | 155 |
| add_line_pass3 res ((name as (_, ss), role, t, rule, deps) :: lines) = |
54755 | 156 |
if role <> Plain orelse is_skolemize_rule rule orelse is_arith_rule rule orelse |
54716
55ed20a29a8c
reverse order in which lines are selected, to ensure that the number of dependencies is accurate
blanchet
parents:
54715
diff
changeset
|
157 |
(* the last line must be kept *) |
55ed20a29a8c
reverse order in which lines are selected, to ensure that the number of dependencies is accurate
blanchet
parents:
54715
diff
changeset
|
158 |
null lines orelse |
54746 | 159 |
(not (is_only_type_information t) andalso null (Term.add_tvars t []) |
160 |
andalso length deps >= 2 andalso |
|
54716
55ed20a29a8c
reverse order in which lines are selected, to ensure that the number of dependencies is accurate
blanchet
parents:
54715
diff
changeset
|
161 |
(* don't keep next to last line, which usually results in a trivial step *) |
55ed20a29a8c
reverse order in which lines are selected, to ensure that the number of dependencies is accurate
blanchet
parents:
54715
diff
changeset
|
162 |
not (can the_single lines)) then |
54758 | 163 |
add_line_pass3 ((name, role, simplify_prop t, rule, deps) :: res) lines |
54716
55ed20a29a8c
reverse order in which lines are selected, to ensure that the number of dependencies is accurate
blanchet
parents:
54715
diff
changeset
|
164 |
else |
54755 | 165 |
add_line_pass3 res (map (replace_dependencies_in_line (name, deps)) lines) |
49914 | 166 |
|
52454 | 167 |
val add_labels_of_proof = |
54700 | 168 |
steps_of_proof |
169 |
#> fold_isar_steps (byline_of_step #> (fn SOME ((ls, _), _) => union (op =) ls | _ => I)) |
|
49914 | 170 |
|
171 |
fun kill_useless_labels_in_proof proof = |
|
172 |
let |
|
51178 | 173 |
val used_ls = add_labels_of_proof proof [] |
54758 | 174 |
|
54700 | 175 |
fun kill_label l = if member (op =) used_ls l then l else no_label |
176 |
fun kill_assms assms = map (apfst kill_label) assms |
|
54758 | 177 |
|
54700 | 178 |
fun kill_step (Prove (qs, xs, l, t, subproofs, by)) = |
179 |
Prove (qs, xs, kill_label l, t, map kill_proof subproofs, by) |
|
180 |
| kill_step step = step |
|
181 |
and kill_proof (Proof (fix, assms, steps)) = |
|
182 |
Proof (fix, kill_assms assms, map kill_step steps) |
|
183 |
in |
|
184 |
kill_proof proof |
|
185 |
end |
|
49914 | 186 |
|
54501 | 187 |
val assume_prefix = "a" |
188 |
val have_prefix = "f" |
|
189 |
||
49914 | 190 |
val relabel_proof = |
191 |
let |
|
54758 | 192 |
fun fresh_label depth prefix (accum as (l, subst, next)) = |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50671
diff
changeset
|
193 |
if l = no_label then |
54758 | 194 |
accum |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50671
diff
changeset
|
195 |
else |
54758 | 196 |
let val l' = (replicate_string (depth + 1) prefix, next) in |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
197 |
(l', (l, l') :: subst, next + 1) |
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50671
diff
changeset
|
198 |
end |
54700 | 199 |
|
200 |
fun relabel_facts subst = apfst (maps (the_list o AList.lookup (op =) subst)) |
|
201 |
||
202 |
fun relabel_assm depth (l, t) (subst, next) = |
|
54507 | 203 |
let val (l, subst, next) = (l, subst, next) |> fresh_label depth assume_prefix in |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
204 |
((l, t), (subst, next)) |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
205 |
end |
54700 | 206 |
|
207 |
fun relabel_assms subst depth assms = fold_map (relabel_assm depth) assms (subst, 1) ||> fst |
|
208 |
||
209 |
fun relabel_steps _ _ _ [] = [] |
|
210 |
| relabel_steps subst depth next (Prove (qs, xs, l, t, sub, by) :: steps) = |
|
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50671
diff
changeset
|
211 |
let |
54507 | 212 |
val (l, subst, next) = (l, subst, next) |> fresh_label depth have_prefix |
54700 | 213 |
val sub = relabel_proofs subst depth sub |
54759 | 214 |
val by = apfst (relabel_facts subst) by |
54700 | 215 |
in |
216 |
Prove (qs, xs, l, t, sub, by) :: relabel_steps subst depth next steps |
|
217 |
end |
|
218 |
| relabel_steps subst depth next (step :: steps) = |
|
219 |
step :: relabel_steps subst depth next steps |
|
220 |
and relabel_proof subst depth (Proof (fix, assms, steps)) = |
|
221 |
let val (assms, subst) = relabel_assms subst depth assms in |
|
222 |
Proof (fix, assms, relabel_steps subst depth 1 steps) |
|
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
223 |
end |
54700 | 224 |
and relabel_proofs subst depth = map (relabel_proof subst (depth + 1)) |
225 |
in |
|
226 |
relabel_proof [] 0 |
|
227 |
end |
|
49914 | 228 |
|
50004
c96e8e40d789
several improvements to Isar proof reconstruction, by Steffen Smolka (step merging in case splits, time measurements, etc.)
blanchet
parents:
49994
diff
changeset
|
229 |
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
|
230 |
let |
54700 | 231 |
fun chain_qs_lfs NONE lfs = ([], lfs) |
232 |
| chain_qs_lfs (SOME l0) lfs = |
|
233 |
if member (op =) lfs l0 then ([Then], lfs |> remove (op =) l0) else ([], lfs) |
|
234 |
fun chain_step lbl (Prove (qs, xs, l, t, subproofs, ((lfs, gfs), method))) = |
|
235 |
let val (qs', lfs) = chain_qs_lfs lbl lfs in |
|
236 |
Prove (qs' @ qs, xs, l, t, chain_proofs subproofs, ((lfs, gfs), method)) |
|
237 |
end |
|
50672
ab5b8b5c9cbe
added "obtain" to Isar proof construction data structure
blanchet
parents:
50671
diff
changeset
|
238 |
| chain_step _ step = step |
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
239 |
and chain_steps _ [] = [] |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
240 |
| chain_steps (prev as SOME _) (i :: is) = |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
241 |
chain_step prev i :: chain_steps (label_of_step i) is |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
242 |
| chain_steps _ (i :: is) = i :: chain_steps (label_of_step i) is |
54700 | 243 |
and chain_proof (Proof (fix, assms, steps)) = |
244 |
Proof (fix, assms, chain_steps (try (List.last #> fst) assms) steps) |
|
51179
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
245 |
and chain_proofs proofs = map (chain_proof) proofs |
0d5f8812856f
split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents:
51178
diff
changeset
|
246 |
in chain_proof end |
49883 | 247 |
|
49914 | 248 |
type isar_params = |
54505 | 249 |
bool * bool * string * string * Time.time option * real * bool * (term, string) atp_step list * |
250 |
thm |
|
49914 | 251 |
|
53052
a0db255af8c5
sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents:
53047
diff
changeset
|
252 |
fun isar_proof_text ctxt isar_proofs |
54756 | 253 |
(debug, verbose, metis_type_enc, metis_lam_trans, preplay_timeout, isar_compress, isar_try0, |
254 |
atp_proof, goal) |
|
49918
cf441f4a358b
renamed Isar-proof related options + changed semantics of Isar shrinking
blanchet
parents:
49917
diff
changeset
|
255 |
(one_line_params as (_, _, _, _, subgoal, subgoal_count)) = |
49883 | 256 |
let |
52196
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52150
diff
changeset
|
257 |
val (params, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt |
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52150
diff
changeset
|
258 |
val (_, ctxt) = |
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52150
diff
changeset
|
259 |
params |
2281f33e8da6
redid rac7830871177 to avoid duplicate fixed variable (e.g. lemma "P (a::nat)" proof - have "!!a::int. Q a" sledgehammer [e])
blanchet
parents:
52150
diff
changeset
|
260 |
|> map (fn (s, T) => (Binding.name s, SOME T, NoSyn)) |
54507 | 261 |
|> (fn fixes => ctxt |> Variable.set_body false |> Proof_Context.add_fixes fixes) |
53052
a0db255af8c5
sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents:
53047
diff
changeset
|
262 |
val one_line_proof = one_line_proof_text 0 one_line_params |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
263 |
val do_preplay = preplay_timeout <> SOME Time.zeroTime |
49883 | 264 |
|
54751 | 265 |
val is_fixed = Variable.is_declared ctxt orf can Name.dest_skolem |
266 |
fun skolems_of t = Term.add_frees t [] |> filter_out (is_fixed o fst) |> rev |
|
267 |
||
268 |
fun get_role keep_role ((num, _), role, t, rule, _) = |
|
269 |
if keep_role role then SOME ((raw_label_of_num num, t), rule) else NONE |
|
54700 | 270 |
|
49883 | 271 |
fun isar_proof_of () = |
272 |
let |
|
273 |
val atp_proof = |
|
274 |
atp_proof |
|
54746 | 275 |
|> inline_z3_defs [] |
54758 | 276 |
|> map simplify_line |
54750 | 277 |
|> inline_z3_hypotheses [] [] |
54755 | 278 |
|> rpair [] |-> fold_rev add_line_pass1 |
279 |
|> rpair [] |-> fold_rev add_line_pass2 |
|
280 |
|> add_line_pass3 [] |
|
54700 | 281 |
|
54535
59737a43e044
support Negated_Conjecture as a TPTP role as well (e.g. for SMT proofs)
blanchet
parents:
54507
diff
changeset
|
282 |
val conjs = |
54700 | 283 |
map_filter (fn (name, role, _, _, _) => |
284 |
if member (op =) [Conjecture, Negated_Conjecture] role then SOME name else NONE) |
|
285 |
atp_proof |
|
54751 | 286 |
val assms = map_filter (Option.map fst o get_role (curry (op =) Hypothesis)) atp_proof |
54700 | 287 |
val lems = |
288 |
map_filter (get_role (curry (op =) Lemma)) atp_proof |
|
54751 | 289 |
|> map (fn ((l, t), rule) => |
54753 | 290 |
let |
291 |
val (skos, meth) = |
|
292 |
if is_skolemize_rule rule then (skolems_of t, MetisM) |
|
54755 | 293 |
else if is_arith_rule rule then ([], ArithM) |
294 |
else ([], AutoM) |
|
54753 | 295 |
in |
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54756
diff
changeset
|
296 |
Prove ([], skos, l, t, [], (([], []), meth)) |
54753 | 297 |
end) |
54700 | 298 |
|
51212
2bbcc9cc12b4
ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents:
51208
diff
changeset
|
299 |
val bot = atp_proof |> List.last |> #1 |
54700 | 300 |
|
51145 | 301 |
val refute_graph = |
51212
2bbcc9cc12b4
ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents:
51208
diff
changeset
|
302 |
atp_proof |
2bbcc9cc12b4
ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents:
51208
diff
changeset
|
303 |
|> map (fn (name, _, _, _, from) => (from, name)) |
2bbcc9cc12b4
ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents:
51208
diff
changeset
|
304 |
|> make_refute_graph bot |
2bbcc9cc12b4
ensure all conjecture clauses are in the graph -- to prevent exceptions later
blanchet
parents:
51208
diff
changeset
|
305 |
|> fold (Atom_Graph.default_node o rpair ()) conjs |
54700 | 306 |
|
51145 | 307 |
val axioms = axioms_of_refute_graph refute_graph conjs |
54700 | 308 |
|
51145 | 309 |
val tainted = tainted_atoms_of_refute_graph refute_graph conjs |
51156 | 310 |
val is_clause_tainted = exists (member (op =) tainted) |
50676
83b8a5a39709
generate "obtain" steps corresponding to skolemization inferences
blanchet
parents:
50675
diff
changeset
|
311 |
val steps = |
49883 | 312 |
Symtab.empty |
51201 | 313 |
|> fold (fn (name as (s, _), role, t, rule, _) => |
54758 | 314 |
Symtab.update_new (s, (rule, t |
315 |
|> (if is_clause_tainted [name] then |
|
316 |
role <> Conjecture ? s_not_prop |
|
317 |
#> fold exists_of (map Var (Term.add_vars t [])) |
|
318 |
else |
|
319 |
I)))) |
|
320 |
atp_proof |
|
54700 | 321 |
|
54755 | 322 |
val rule_of_clause_id = fst o the o Symtab.lookup steps o fst |
54700 | 323 |
|
54757
4960647932ec
use 'prop' rather than 'bool' systematically in Isar reconstruction code
blanchet
parents:
54756
diff
changeset
|
324 |
fun prop_of_clause [(num, _)] = Symtab.lookup steps num |> the |> snd |> close_form |
50016 | 325 |
| prop_of_clause names = |
50676
83b8a5a39709
generate "obtain" steps corresponding to skolemization inferences
blanchet
parents:
50675
diff
changeset
|
326 |
let |
54758 | 327 |
val lits = map (HOLogic.dest_Trueprop o snd) |
328 |
(map_filter (Symtab.lookup steps o fst) names) |
|
50676
83b8a5a39709
generate "obtain" steps corresponding to skolemization inferences
blanchet
parents:
50675
diff
changeset
|
329 |
in |
54754 | 330 |
(case List.partition (can HOLogic.dest_not) lits of |
50018
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
331 |
(negs as _ :: _, pos as _ :: _) => |
54507 | 332 |
s_imp (Library.foldr1 s_conj (map HOLogic.dest_not negs), Library.foldr1 s_disj pos) |
54754 | 333 |
| _ => fold (curry s_disj) lits @{term False}) |
50018
4ea26c74d7ea
use implications rather than disjunctions to improve readability
blanchet
parents:
50017
diff
changeset
|
334 |
end |
50016 | 335 |
|> HOLogic.mk_Trueprop |> close_form |
54700 | 336 |
|
337 |
fun maybe_show outer c = |
|
338 |
(outer andalso length c = 1 andalso subset (op =) (c, conjs)) ? cons Show |
|
339 |
||
340 |
fun isar_steps outer predecessor accum [] = |
|
341 |
accum |
|
342 |
|> (if tainted = [] then |
|
343 |
cons (Prove (if outer then [Show] else [], [], no_label, concl_t, [], |
|
54755 | 344 |
((the_list predecessor, []), MetisM))) |
54700 | 345 |
else |
346 |
I) |
|
347 |
|> rev |
|
54755 | 348 |
| isar_steps outer _ accum (Have (id, (gamma, c)) :: infs) = |
54700 | 349 |
let |
350 |
val l = label_of_clause c |
|
351 |
val t = prop_of_clause c |
|
54755 | 352 |
val rule = rule_of_clause_id id |
353 |
val skolem = is_skolemize_rule rule |
|
354 |
||
54700 | 355 |
fun prove sub by = Prove (maybe_show outer c [], [], l, t, sub, by) |
356 |
fun do_rest l step = isar_steps outer (SOME l) (step :: accum) infs |
|
54755 | 357 |
|
358 |
val deps = fold add_fact_of_dependencies gamma no_facts |
|
359 |
val meth = if is_arith_rule rule then ArithM else MetisM |
|
360 |
val by = (deps, meth) |
|
54700 | 361 |
in |
362 |
if is_clause_tainted c then |
|
54712 | 363 |
(case gamma of |
54700 | 364 |
[g] => |
54755 | 365 |
if skolem andalso is_clause_tainted g then |
54751 | 366 |
let val subproof = Proof (skolems_of (prop_of_clause g), [], rev accum) in |
54700 | 367 |
isar_steps outer (SOME l) [prove [subproof] (no_facts, MetisM)] [] |
368 |
end |
|
51148
2246a2e17f92
tuning -- refactoring in preparation for handling skolemization of conjecture
blanchet
parents:
51147
diff
changeset
|
369 |
else |
54700 | 370 |
do_rest l (prove [] by) |
54712 | 371 |
| _ => do_rest l (prove [] by)) |
54700 | 372 |
else |
54755 | 373 |
(if skolem then Prove ([], skolems_of t, l, t, [], by) |
54751 | 374 |
else prove [] by) |
375 |
|> do_rest l |
|
54700 | 376 |
end |
377 |
| isar_steps outer predecessor accum (Cases cases :: infs) = |
|
378 |
let |
|
379 |
fun isar_case (c, infs) = |
|
380 |
isar_proof false [] [(label_of_clause c, prop_of_clause c)] [] infs |
|
381 |
val c = succedent_of_cases cases |
|
382 |
val l = label_of_clause c |
|
383 |
val t = prop_of_clause c |
|
384 |
val step = |
|
385 |
Prove (maybe_show outer c [], [], l, t, map isar_case cases, |
|
386 |
((the_list predecessor, []), MetisM)) |
|
387 |
in |
|
388 |
isar_steps outer (SOME l) (step :: accum) infs |
|
389 |
end |
|
390 |
and isar_proof outer fix assms lems infs = |
|
391 |
Proof (fix, assms, lems @ isar_steps outer NONE [] infs) |
|
392 |
||
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52556
diff
changeset
|
393 |
(* 60 seconds seems like a good interpreation of "no timeout" *) |
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52556
diff
changeset
|
394 |
val preplay_timeout = preplay_timeout |> the_default (seconds 60.0) |
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52556
diff
changeset
|
395 |
|
53761
4d34e267fba9
use configuration mechanism for low-level tracing
blanchet
parents:
53586
diff
changeset
|
396 |
val (preplay_interface as {overall_preplay_stats, ...}, isar_proof) = |
51145 | 397 |
refute_graph |
54699 | 398 |
(* |
54751 | 399 |
|> tap (tracing o prefix "Refute graph: " o string_of_refute_graph) |
54699 | 400 |
*) |
51031
63d71b247323
more robustness in Isar proof reconstruction (cf. bug report by Ondrej)
blanchet
parents:
51026
diff
changeset
|
401 |
|> redirect_graph axioms tainted bot |
54699 | 402 |
(* |
54751 | 403 |
|> tap (tracing o prefix "Direct proof: " o string_of_direct_proof) |
54699 | 404 |
*) |
54754 | 405 |
|> isar_proof true params assms lems |
54712 | 406 |
|> postprocess_remove_unreferenced_steps I |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
407 |
|> relabel_proof_canonically |
54500 | 408 |
|> `(proof_preplay_interface debug ctxt metis_type_enc metis_lam_trans do_preplay |
53761
4d34e267fba9
use configuration mechanism for low-level tracing
blanchet
parents:
53586
diff
changeset
|
409 |
preplay_timeout) |
54754 | 410 |
|
52626 | 411 |
val ((preplay_time, preplay_fail), isar_proof) = |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
412 |
isar_proof |
54501 | 413 |
|> compress_proof (if isar_proofs = SOME true then isar_compress else 1000.0) |
53762 | 414 |
preplay_interface |
52632
23393c31c7fe
added sledgehammer parameters isar_try0 and isar_minimize and their negative aliases
smolkas
parents:
52626
diff
changeset
|
415 |
|> isar_try0 ? try0 preplay_timeout preplay_interface |
54712 | 416 |
|> postprocess_remove_unreferenced_steps (isar_try0 ? min_deps_of_step preplay_interface) |
52626 | 417 |
|> `overall_preplay_stats |
54758 | 418 |
||> (chain_direct_proof #> kill_useless_labels_in_proof #> relabel_proof) |
54754 | 419 |
|
54500 | 420 |
val isar_text = |
421 |
string_of_proof ctxt metis_type_enc metis_lam_trans subgoal subgoal_count isar_proof |
|
49883 | 422 |
in |
54754 | 423 |
(case isar_text of |
49883 | 424 |
"" => |
51190
2654b3965c8d
made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents:
51187
diff
changeset
|
425 |
if isar_proofs = SOME true then |
50671 | 426 |
"\nNo structured proof available (proof too simple)." |
49883 | 427 |
else |
428 |
"" |
|
429 |
| _ => |
|
50670
eaa540986291
properly take the existential closure of skolems
blanchet
parents:
50557
diff
changeset
|
430 |
let |
eaa540986291
properly take the existential closure of skolems
blanchet
parents:
50557
diff
changeset
|
431 |
val msg = |
51203 | 432 |
(if verbose then |
433 |
let |
|
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52556
diff
changeset
|
434 |
val num_steps = add_proof_steps (steps_of_proof isar_proof) 0 |
51203 | 435 |
in [string_of_int num_steps ^ " step" ^ plural_s num_steps] end |
436 |
else |
|
437 |
[]) @ |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
438 |
(if do_preplay then |
50924 | 439 |
[(if preplay_fail then "may fail, " else "") ^ |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52555
diff
changeset
|
440 |
string_of_preplay_time preplay_time] |
50670
eaa540986291
properly take the existential closure of skolems
blanchet
parents:
50557
diff
changeset
|
441 |
else |
eaa540986291
properly take the existential closure of skolems
blanchet
parents:
50557
diff
changeset
|
442 |
[]) |
50277 | 443 |
in |
54507 | 444 |
"\n\nStructured proof" ^ (commas msg |> not (null msg) ? enclose " (" ")") ^ ":\n" ^ |
445 |
Active.sendback_markup [Markup.padding_command] isar_text |
|
54754 | 446 |
end) |
49883 | 447 |
end |
448 |
val isar_proof = |
|
449 |
if debug then |
|
450 |
isar_proof_of () |
|
54754 | 451 |
else |
452 |
(case try isar_proof_of () of |
|
453 |
SOME s => s |
|
454 |
| NONE => |
|
455 |
if isar_proofs = SOME true then "\nWarning: The Isar proof construction failed." else "") |
|
49883 | 456 |
in one_line_proof ^ isar_proof end |
457 |
||
51187
c344cf148e8f
avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents:
51179
diff
changeset
|
458 |
fun isar_proof_would_be_a_good_idea preplay = |
54754 | 459 |
(case preplay of |
51187
c344cf148e8f
avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents:
51179
diff
changeset
|
460 |
Played (reconstr, _) => reconstr = SMT |
54093 | 461 |
| Trust_Playable _ => false |
54754 | 462 |
| Failed_to_Play _ => true) |
51187
c344cf148e8f
avoid using "smt" for minimization -- better use the prover itself, since then Sledgehammer gets to try metis again and gives the opportunity to output an Isar proof -- and show Isar proof as fallback for SMT proofs
blanchet
parents:
51179
diff
changeset
|
463 |
|
53052
a0db255af8c5
sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents:
53047
diff
changeset
|
464 |
fun proof_text ctxt isar_proofs isar_params num_chained |
49883 | 465 |
(one_line_params as (preplay, _, _, _, _, _)) = |
51190
2654b3965c8d
made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents:
51187
diff
changeset
|
466 |
(if isar_proofs = SOME true orelse |
2654b3965c8d
made "isar_proofs" a 3-way option, to provide a way to totally disable isar_proofs if desired
blanchet
parents:
51187
diff
changeset
|
467 |
(isar_proofs = NONE andalso isar_proof_would_be_a_good_idea preplay) then |
54500 | 468 |
isar_proof_text ctxt isar_proofs (isar_params ()) |
49883 | 469 |
else |
53052
a0db255af8c5
sledgehammer sendback always uses Markup.padding_command: sensible default for most practical applications -- old-style in-line replacement is superseded by auto mode or panel;
wenzelm
parents:
53047
diff
changeset
|
470 |
one_line_proof_text num_chained) one_line_params |
49883 | 471 |
|
472 |
end; |