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