|
1 (* Title: HOL/Tools/try_methods.ML |
|
2 Author: Jasmin Blanchette, TU Muenchen |
|
3 |
|
4 Try a combination of proof methods. |
|
5 *) |
|
6 |
|
7 signature TRY_METHODS = |
|
8 sig |
|
9 val auto : bool Unsynchronized.ref |
|
10 val try_methods : |
|
11 Time.time option -> string list * string list * string list * string list |
|
12 -> Proof.state -> bool |
|
13 val setup : theory -> theory |
|
14 end; |
|
15 |
|
16 structure Try_Methods : TRY_METHODS = |
|
17 struct |
|
18 |
|
19 val auto = Unsynchronized.ref false |
|
20 |
|
21 val _ = |
|
22 ProofGeneralPgip.add_preference Preferences.category_tracing |
|
23 (Preferences.bool_pref auto "auto-try-methods" |
|
24 "Try standard proof methods.") |
|
25 |
|
26 val default_timeout = seconds 5.0 |
|
27 |
|
28 fun can_apply timeout_opt pre post tac st = |
|
29 let val {goal, ...} = Proof.goal st in |
|
30 case (case timeout_opt of |
|
31 SOME timeout => TimeLimit.timeLimit timeout |
|
32 | NONE => fn f => fn x => f x) (Seq.pull o tac) (pre st) of |
|
33 SOME (x, _) => nprems_of (post x) < nprems_of goal |
|
34 | NONE => false |
|
35 end |
|
36 handle TimeLimit.TimeOut => false |
|
37 |
|
38 fun do_generic timeout_opt command pre post apply st = |
|
39 let val timer = Timer.startRealTimer () in |
|
40 if can_apply timeout_opt pre post apply st then |
|
41 SOME (command, Time.toMilliseconds (Timer.checkRealTimer timer)) |
|
42 else |
|
43 NONE |
|
44 end |
|
45 |
|
46 val parse_method = |
|
47 enclose "(" ")" |
|
48 #> Outer_Syntax.scan Position.start |
|
49 #> filter Token.is_proper |
|
50 #> Scan.read Token.stopper Method.parse |
|
51 #> (fn SOME (Method.Source src) => src | _ => raise Fail "expected Source") |
|
52 |
|
53 fun apply_named_method_on_first_goal method thy = |
|
54 method |> parse_method |
|
55 |> Method.method thy |
|
56 |> Method.Basic |
|
57 |> curry Method.SelectGoals 1 |
|
58 |> Proof.refine |
|
59 handle ERROR _ => K Seq.empty (* e.g., the method isn't available yet *) |
|
60 |
|
61 fun add_attr_text (NONE, _) s = s |
|
62 | add_attr_text (_, []) s = s |
|
63 | add_attr_text (SOME x, fs) s = |
|
64 s ^ " " ^ (if x = "" then "" else x ^ ": ") ^ space_implode " " fs |
|
65 fun attrs_text (sx, ix, ex, dx) (ss, is, es, ds) = |
|
66 "" |> fold add_attr_text [(sx, ss), (ix, is), (ex, es), (dx, ds)] |
|
67 |
|
68 fun do_named_method (name, ((all_goals, run_if_auto), attrs)) auto timeout_opt |
|
69 quad st = |
|
70 if not auto orelse run_if_auto then |
|
71 let val attrs = attrs_text attrs quad in |
|
72 do_generic timeout_opt |
|
73 (name ^ (if all_goals andalso |
|
74 nprems_of (#goal (Proof.goal st)) > 1 then |
|
75 "[1]" |
|
76 else |
|
77 "") ^ |
|
78 attrs) I (#goal o Proof.goal) |
|
79 (apply_named_method_on_first_goal (name ^ attrs) |
|
80 (Proof.theory_of st)) st |
|
81 end |
|
82 else |
|
83 NONE |
|
84 |
|
85 val full_attrs = (SOME "simp", SOME "intro", SOME "elim", SOME "dest") |
|
86 val clas_attrs = (NONE, SOME "intro", SOME "elim", SOME "dest") |
|
87 val simp_attrs = (SOME "add", NONE, NONE, NONE) |
|
88 val metis_attrs = (SOME "", SOME "", SOME "", SOME "") |
|
89 val no_attrs = (NONE, NONE, NONE, NONE) |
|
90 |
|
91 (* name * ((all_goals, run_if_auto), (simp, intro, elim, dest) *) |
|
92 val named_methods = |
|
93 [("simp", ((false, true), simp_attrs)), |
|
94 ("auto", ((true, true), full_attrs)), |
|
95 ("fast", ((false, false), clas_attrs)), |
|
96 ("fastsimp", ((false, false), full_attrs)), |
|
97 ("force", ((false, false), full_attrs)), |
|
98 ("blast", ((false, true), clas_attrs)), |
|
99 ("metis", ((false, true), metis_attrs)), |
|
100 ("linarith", ((false, true), no_attrs)), |
|
101 ("presburger", ((false, true), no_attrs))] |
|
102 val do_methods = map do_named_method named_methods |
|
103 |
|
104 fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms" |
|
105 |
|
106 fun do_try_methods auto timeout_opt quad st = |
|
107 let |
|
108 val st = st |> Proof.map_context (Config.put Metis_Tactics.verbose false) |
|
109 in |
|
110 case do_methods |> Par_List.map (fn f => f auto timeout_opt quad st) |
|
111 |> map_filter I |> sort (int_ord o pairself snd) of |
|
112 [] => (if auto then () else writeln "No proof found."; (false, st)) |
|
113 | xs as (s, _) :: _ => |
|
114 let |
|
115 val xs = xs |> map (fn (s, n) => (n, hd (space_explode " " s))) |
|
116 |> AList.coalesce (op =) |
|
117 |> map (swap o apsnd commas) |
|
118 val need_parens = exists_string (curry (op =) " ") s |
|
119 val message = |
|
120 (if auto then "Auto Try Methods found a proof" |
|
121 else "Try this command") ^ ": " ^ |
|
122 Markup.markup Markup.sendback |
|
123 ((if nprems_of (#goal (Proof.goal st)) = 1 then "by" |
|
124 else "apply") ^ " " ^ (s |> need_parens ? enclose "(" ")")) ^ |
|
125 "\n(" ^ space_implode "; " (map time_string xs) ^ ").\n" |
|
126 in |
|
127 (true, st |> (if auto then |
|
128 Proof.goal_message |
|
129 (fn () => Pretty.chunks [Pretty.str "", |
|
130 Pretty.markup Markup.hilite |
|
131 [Pretty.str message]]) |
|
132 else |
|
133 tap (fn _ => Output.urgent_message message))) |
|
134 end |
|
135 end |
|
136 |
|
137 fun try_methods timeout_opt = fst oo do_try_methods false timeout_opt |
|
138 |
|
139 val try_methodsN = "try_methods" |
|
140 |
|
141 fun try_methods_trans quad = |
|
142 Toplevel.keep (K () o do_try_methods false (SOME default_timeout) quad |
|
143 o Toplevel.proof_of) |
|
144 |
|
145 fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) = |
|
146 (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2) |
|
147 |
|
148 fun string_of_xthm (xref, args) = |
|
149 Facts.string_of_ref xref ^ |
|
150 implode (map (enclose "[" "]" o Pretty.str_of |
|
151 o Args.pretty_src @{context}) args) |
|
152 |
|
153 val parse_fact_refs = |
|
154 Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) |
|
155 (Parse_Spec.xthm >> string_of_xthm)) |
|
156 val parse_attr = |
|
157 Args.$$$ "simp" |-- Args.colon |-- parse_fact_refs |
|
158 >> (fn ss => (ss, [], [], [])) |
|
159 || Args.$$$ "intro" |-- Args.colon |-- parse_fact_refs |
|
160 >> (fn is => ([], is, [], [])) |
|
161 || Args.$$$ "elim" |-- Args.colon |-- parse_fact_refs |
|
162 >> (fn es => ([], [], es, [])) |
|
163 || Args.$$$ "dest" |-- Args.colon |-- parse_fact_refs |
|
164 >> (fn ds => ([], [], [], ds)) |
|
165 fun parse_attrs x = |
|
166 (Args.parens parse_attrs |
|
167 || Scan.repeat parse_attr |
|
168 >> (fn quad => fold merge_attrs quad ([], [], [], []))) x |
|
169 |
|
170 val parse_try_methods_command = |
|
171 Scan.optional parse_attrs ([], [], [], []) #>> try_methods_trans |
|
172 |
|
173 val _ = |
|
174 Outer_Syntax.improper_command try_methodsN |
|
175 "try a combination of proof methods" Keyword.diag |
|
176 parse_try_methods_command |
|
177 |
|
178 val auto_try_methods = do_try_methods true NONE ([], [], [], []) |
|
179 |
|
180 val setup = Auto_Tools.register_tool (auto, auto_try_methods) |
|
181 |
|
182 end; |