1 (* Title: HOL/Tools/try_methods.ML
2 Author: Jasmin Blanchette, TU Muenchen
4 Try a combination of proof methods.
7 signature TRY_METHODS =
9 val auto : bool Unsynchronized.ref
11 Time.time option -> string list * string list * string list * string list
12 -> Proof.state -> bool
13 val setup : theory -> theory
16 structure Try_Methods : TRY_METHODS =
19 val auto = Unsynchronized.ref false
22 ProofGeneralPgip.add_preference Preferences.category_tracing
23 (Preferences.bool_pref auto "auto-try-methods"
24 "Try standard proof methods.")
26 val default_timeout = seconds 5.0
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
36 handle TimeLimit.TimeOut => false
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))
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")
53 fun apply_named_method_on_first_goal method thy =
54 method |> parse_method
57 |> curry Method.SelectGoals 1
59 handle ERROR _ => K Seq.empty (* e.g., the method isn't available yet *)
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)]
68 fun do_named_method (name, ((all_goals, run_if_auto), attrs)) auto timeout_opt
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
78 attrs) I (#goal o Proof.goal)
79 (apply_named_method_on_first_goal (name ^ attrs)
80 (Proof.theory_of st)) st
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)
91 (* name * ((all_goals, run_if_auto), (simp, intro, elim, dest) *)
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
104 fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms"
106 fun do_try_methods auto timeout_opt quad st =
108 val st = st |> Proof.map_context (Config.put Metis_Tactics.verbose false)
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, _) :: _ =>
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
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"
127 (true, st |> (if auto then
129 (fn () => Pretty.chunks [Pretty.str "",
130 Pretty.markup Markup.hilite
131 [Pretty.str message]])
133 tap (fn _ => Output.urgent_message message)))
137 fun try_methods timeout_opt = fst oo do_try_methods false timeout_opt
139 val try_methodsN = "try_methods"
141 fun try_methods_trans quad =
142 Toplevel.keep (K () o do_try_methods false (SOME default_timeout) quad
145 fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) =
146 (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2)
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)
153 val parse_fact_refs =
154 Scan.repeat1 (Scan.unless (Parse.name -- Args.colon)
155 (Parse_Spec.xthm >> string_of_xthm))
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))
166 (Args.parens parse_attrs
167 || Scan.repeat parse_attr
168 >> (fn quad => fold merge_attrs quad ([], [], [], []))) x
170 val parse_try_methods_command =
171 Scan.optional parse_attrs ([], [], [], []) #>> try_methods_trans
174 Outer_Syntax.improper_command try_methodsN
175 "try a combination of proof methods" Keyword.diag
176 parse_try_methods_command
178 val auto_try_methods = do_try_methods true NONE ([], [], [], [])
180 val setup = Auto_Tools.register_tool (auto, auto_try_methods)