author | blanchet |
Mon, 03 Feb 2014 10:14:18 +0100 | |
changeset 55266 | d9d31354834e |
parent 55264 | 43473497fb65 |
child 55268 | a46458d368d5 |
permissions | -rw-r--r-- |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
1 |
(* Title: HOL/Tools/Sledgehammer/sledgehammer_isar_preplay.ML |
54712 | 2 |
Author: Steffen Juilf Smolka, TU Muenchen |
50923 | 3 |
Author: Jasmin Blanchette, TU Muenchen |
4 |
||
54763 | 5 |
Preplaying of Isar proofs. |
50923 | 6 |
*) |
7 |
||
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
8 |
signature SLEDGEHAMMER_ISAR_PREPLAY = |
50923 | 9 |
sig |
54828 | 10 |
type play_outcome = Sledgehammer_Reconstructor.play_outcome |
55223
3c593bad6b31
generalized preplaying infrastructure to store various results for various methods
blanchet
parents:
55221
diff
changeset
|
11 |
type proof_method = Sledgehammer_Isar_Proof.proof_method |
55212 | 12 |
type isar_step = Sledgehammer_Isar_Proof.isar_step |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
13 |
type isar_proof = Sledgehammer_Isar_Proof.isar_proof |
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
14 |
type label = Sledgehammer_Isar_Proof.label |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
15 |
|
55212 | 16 |
val trace : bool Config.T |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
17 |
|
55260 | 18 |
type isar_preplay_data |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
19 |
|
55256 | 20 |
val enrich_context_with_local_facts : isar_proof -> Proof.context -> Proof.context |
55258 | 21 |
val preplay_isar_step : Proof.context -> Time.time -> proof_method -> isar_step -> play_outcome |
55264 | 22 |
val set_preplay_outcomes_of_isar_step : Proof.context -> Time.time -> |
23 |
isar_preplay_data Unsynchronized.ref -> isar_step -> |
|
55260 | 24 |
(proof_method * play_outcome Lazy.lazy) list -> unit |
55266 | 25 |
val preplay_outcome_of_isar_step_for_method : isar_preplay_data -> label -> proof_method -> |
55260 | 26 |
play_outcome Lazy.lazy |
55266 | 27 |
val fastest_method_of_isar_step : isar_preplay_data -> label -> proof_method |
55260 | 28 |
val preplay_outcome_of_isar_proof : isar_preplay_data -> isar_proof -> play_outcome |
54504 | 29 |
end; |
50923 | 30 |
|
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
31 |
structure Sledgehammer_Isar_Preplay : SLEDGEHAMMER_ISAR_PREPLAY = |
50923 | 32 |
struct |
33 |
||
55257 | 34 |
open ATP_Proof_Reconstruct |
50923 | 35 |
open Sledgehammer_Util |
54828 | 36 |
open Sledgehammer_Reconstructor |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
37 |
open Sledgehammer_Isar_Proof |
50923 | 38 |
|
54763 | 39 |
val trace = Attrib.setup_config_bool @{binding sledgehammer_preplay_trace} (K false) |
50924 | 40 |
|
55256 | 41 |
fun enrich_context_with_local_facts proof ctxt = |
42 |
let |
|
43 |
val thy = Proof_Context.theory_of ctxt |
|
44 |
||
45 |
fun enrich_with_fact l t = |
|
46 |
Proof_Context.put_thms false (string_of_label l, SOME [Skip_Proof.make_thm thy t]) |
|
47 |
||
48 |
val enrich_with_assms = fold (uncurry enrich_with_fact) |
|
49 |
||
50 |
fun enrich_with_proof (Proof (_, assms, isar_steps)) = |
|
51 |
enrich_with_assms assms #> fold enrich_with_step isar_steps |
|
52 |
and enrich_with_step (Let _) = I |
|
53 |
| enrich_with_step (Prove (_, _, l, t, subproofs, _)) = |
|
54 |
enrich_with_fact l t #> fold enrich_with_proof subproofs |
|
55 |
in |
|
56 |
enrich_with_proof proof ctxt |
|
57 |
end |
|
58 |
||
55260 | 59 |
fun preplay_trace ctxt assmsp concl outcome = |
51879 | 60 |
let |
61 |
val ctxt = ctxt |> Config.put show_markup true |
|
55194 | 62 |
val assms = op @ assmsp |
55260 | 63 |
val time = Pretty.str ("[" ^ string_of_play_outcome outcome ^ "]") |
55251 | 64 |
val assms = Pretty.enum " and " "using " " shows " (map (Display.pretty_thm ctxt) assms) |
65 |
val concl = Syntax.pretty_term ctxt concl |
|
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
66 |
in |
55251 | 67 |
tracing (Pretty.string_of (Pretty.blk (2, Pretty.breaks [time, assms, concl]))) |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
68 |
end |
51879 | 69 |
|
50923 | 70 |
fun take_time timeout tac arg = |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
71 |
let val timing = Timing.start () in |
54828 | 72 |
(TimeLimit.timeLimit timeout tac arg; Played (#cpu (Timing.result timing))) |
73 |
handle TimeLimit.TimeOut => Play_Timed_Out timeout |
|
50923 | 74 |
end |
75 |
||
76 |
fun resolve_fact_names ctxt names = |
|
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
|
77 |
(names |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
78 |
|>> map string_of_label |
55194 | 79 |
|> pairself (maps (thms_of_name ctxt))) |
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
|
80 |
handle ERROR msg => error ("preplay error: " ^ msg) |
50923 | 81 |
|
54700 | 82 |
fun thm_of_proof ctxt (Proof (fixed_frees, assms, steps)) = |
50923 | 83 |
let |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
84 |
val thy = Proof_Context.theory_of ctxt |
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
85 |
|
54700 | 86 |
val concl = |
87 |
(case try List.last steps of |
|
88 |
SOME (Prove (_, [], _, t, _, _)) => t |
|
89 |
| _ => raise Fail "preplay error: malformed subproof") |
|
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
90 |
|
51178 | 91 |
val var_idx = maxidx_of_term concl + 1 |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
92 |
fun var_of_free (x, T) = Var ((x, var_idx), T) |
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
93 |
val subst = map (`var_of_free #> swap #> apfst Free) fixed_frees |
51178 | 94 |
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
|
95 |
Logic.list_implies (assms |> map snd, concl) |
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
96 |
|> subst_free subst |
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
97 |
|> Skip_Proof.make_thm thy |
51178 | 98 |
end |
99 |
||
55257 | 100 |
fun tac_of_method ctxt (local_facts, global_facts) meth = |
55194 | 101 |
Method.insert_tac local_facts THEN' |
54766
6ac273f176cd
store alternative proof methods in Isar data structure
blanchet
parents:
54765
diff
changeset
|
102 |
(case meth of |
55194 | 103 |
Meson_Method => Meson.meson_tac ctxt global_facts |
55257 | 104 |
| Metis_Method (type_enc_opt, lam_trans_opt) => |
105 |
Metis_Tactic.metis_tac [type_enc_opt |> the_default partial_type_enc] |
|
106 |
(the_default default_metis_lam_trans lam_trans_opt) ctxt global_facts |
|
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52575
diff
changeset
|
107 |
| _ => |
55194 | 108 |
Method.insert_tac global_facts THEN' |
54766
6ac273f176cd
store alternative proof methods in Isar data structure
blanchet
parents:
54765
diff
changeset
|
109 |
(case meth of |
55194 | 110 |
Simp_Method => Simplifier.asm_full_simp_tac ctxt |
54838
16511f84913c
reconstruct SPASS-Pirate steps of the form 'x ~= C x' (or more complicated)
blanchet
parents:
54831
diff
changeset
|
111 |
| Simp_Size_Method => |
16511f84913c
reconstruct SPASS-Pirate steps of the form 'x ~= C x' (or more complicated)
blanchet
parents:
54831
diff
changeset
|
112 |
Simplifier.asm_full_simp_tac (Simplifier.add_simp @{thm size_ne_size_imp_ne} ctxt) |
54765 | 113 |
| Auto_Method => K (Clasimp.auto_tac ctxt) |
114 |
| Fastforce_Method => Clasimp.fast_force_tac ctxt |
|
115 |
| Force_Method => Clasimp.force_tac ctxt |
|
116 |
| Arith_Method => Arith_Data.arith_tac ctxt |
|
117 |
| Blast_Method => blast_tac ctxt |
|
55219 | 118 |
| Algebra_Method => Groebner.algebra_tac [] [] ctxt |
55202
824c48a539c9
renamed many Sledgehammer ML files to clarify structure
blanchet
parents:
55194
diff
changeset
|
119 |
| _ => raise Fail "Sledgehammer_Isar_Preplay: tac_of_method")) |
52592
8a25b17e3d79
optimize isar-proofs by trying different proof methods
smolkas
parents:
52575
diff
changeset
|
120 |
|
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
121 |
(* main function for preplaying Isar steps; may throw exceptions *) |
55258 | 122 |
fun raw_preplay_step ctxt timeout meth (Prove (_, xs, _, t, subproofs, (fact_names, _))) = |
123 |
let |
|
124 |
val goal = |
|
125 |
(case xs of |
|
126 |
[] => t |
|
127 |
| _ => |
|
128 |
(* proof obligation: !!thesis. (!!x. A x ==> thesis) ==> thesis |
|
129 |
(cf. "~~/src/Pure/Isar/obtain.ML") *) |
|
130 |
let |
|
131 |
(* FIXME: generate fresh name *) |
|
132 |
val thesis = Free ("thesis_preplay", HOLogic.boolT) |
|
133 |
val thesis_prop = HOLogic.mk_Trueprop thesis |
|
134 |
val frees = map Free xs |
|
50923 | 135 |
|
55258 | 136 |
(* !!x1..xn. t ==> thesis (xs = [x1, .., xn]) *) |
137 |
val inner_prop = fold_rev Logic.all frees (Logic.mk_implies (t, thesis_prop)) |
|
138 |
in |
|
139 |
(* !!thesis. (!!x1..xn. t ==> thesis) ==> thesis *) |
|
140 |
Logic.all thesis (Logic.mk_implies (inner_prop, thesis_prop)) |
|
141 |
end) |
|
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
142 |
|
55258 | 143 |
val facts = |
144 |
resolve_fact_names ctxt fact_names |
|
145 |
|>> append (map (thm_of_proof ctxt) subproofs) |
|
55194 | 146 |
|
55258 | 147 |
fun prove () = |
148 |
Goal.prove ctxt [] [] goal (fn {context = ctxt, ...} => |
|
149 |
HEADGOAL (tac_of_method ctxt facts meth)) |
|
150 |
handle ERROR msg => error ("Preplay error: " ^ msg) |
|
54761
0ef52f40d419
use consistent condition for setting 'metis_new_skolem' (in preplaying and in output printing) + tuning
blanchet
parents:
54712
diff
changeset
|
151 |
|
55258 | 152 |
val play_outcome = take_time timeout prove () |
153 |
in |
|
154 |
(if Config.get ctxt trace then preplay_trace ctxt facts goal play_outcome else (); |
|
155 |
play_outcome) |
|
156 |
end |
|
157 |
||
158 |
fun preplay_isar_step ctxt timeout meth = |
|
55260 | 159 |
try (raw_preplay_step ctxt timeout meth) #> the_default Play_Failed |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
160 |
|
55260 | 161 |
type isar_preplay_data = (proof_method * play_outcome Lazy.lazy) list Canonical_Label_Tab.table |
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
162 |
|
55256 | 163 |
fun time_of_play (Played time) = time |
164 |
| time_of_play (Play_Timed_Out time) = time |
|
50923 | 165 |
|
55260 | 166 |
fun add_preplay_outcomes Play_Failed _ = Play_Failed |
167 |
| add_preplay_outcomes _ Play_Failed = Play_Failed |
|
168 |
| add_preplay_outcomes (Played time1) (Played time2) = Played (Time.+ (time1, time2)) |
|
169 |
| add_preplay_outcomes play1 play2 = |
|
55256 | 170 |
Play_Timed_Out (Time.+ (pairself time_of_play (play1, play2))) |
54827 | 171 |
|
55264 | 172 |
fun set_preplay_outcomes_of_isar_step ctxt timeout preplay_data |
173 |
(step as Prove (_, _, l, _, _, (_, meths))) meths_outcomes0 = |
|
174 |
let |
|
175 |
fun preplay meth = Lazy.lazy (fn () => preplay_isar_step ctxt timeout meth step) |
|
176 |
val meths_outcomes = |
|
177 |
fold (fn meth => AList.default (op =) (meth, preplay meth)) meths meths_outcomes0 |
|
178 |
in |
|
179 |
preplay_data := Canonical_Label_Tab.map_default (l, []) |
|
180 |
(fold (AList.update (op =)) meths_outcomes) (!preplay_data) |
|
181 |
end |
|
182 |
| set_preplay_outcomes_of_isar_step _ _ _ _ _ = () |
|
52556
c8357085217c
completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents:
52454
diff
changeset
|
183 |
|
55266 | 184 |
fun preplay_outcome_of_isar_step_for_method preplay_data l meth = |
185 |
let val SOME meths_outcomes = Canonical_Label_Tab.lookup preplay_data l in |
|
186 |
the (AList.lookup (op =) meths_outcomes meth) |
|
187 |
end |
|
55252 | 188 |
|
55266 | 189 |
fun fastest_method_of_isar_step preplay_data l = |
190 |
the (Canonical_Label_Tab.lookup preplay_data l) |
|
191 |
|> map (fn (meth, outcome) => |
|
192 |
(meth, Time.toMilliseconds (case Lazy.force outcome of Played time => time | _ => one_year))) |
|
193 |
|> sort (int_ord o pairself snd) |
|
194 |
|> hd |> fst |
|
195 |
||
196 |
fun forced_outcome_of_step preplay_data (Prove (_, _, l, _, _, (_, meths))) = |
|
197 |
Lazy.force (preplay_outcome_of_isar_step_for_method preplay_data l (the_single meths)) |
|
55260 | 198 |
| forced_outcome_of_step _ _ = Played Time.zeroTime |
55252 | 199 |
|
55260 | 200 |
fun preplay_outcome_of_isar_proof preplay_data (Proof (_, _, steps)) = |
201 |
fold_isar_steps (add_preplay_outcomes o forced_outcome_of_step preplay_data) steps |
|
202 |
(Played Time.zeroTime) |
|
50923 | 203 |
|
54504 | 204 |
end; |