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 try_methodsN : string
11 val auto : bool Unsynchronized.ref
13 Time.time option -> string list * string list * string list * string list
14 -> Proof.state -> bool
15 val setup : theory -> theory
18 structure Try_Methods : TRY_METHODS =
21 datatype mode = Auto_Try | Try | Normal
23 val try_methodsN = "try_methods"
27 val auto = Unsynchronized.ref false
30 ProofGeneralPgip.add_preference Preferences.category_tracing
31 (Preferences.bool_pref auto "auto-try-methods"
32 "Try standard proof methods.")
34 val default_timeout = seconds 5.0
36 fun can_apply timeout_opt pre post tac st =
37 let val {goal, ...} = Proof.goal st in
38 case (case timeout_opt of
39 SOME timeout => TimeLimit.timeLimit timeout
40 | NONE => fn f => fn x => f x) (Seq.pull o tac) (pre st) of
41 SOME (x, _) => nprems_of (post x) < nprems_of goal
44 handle TimeLimit.TimeOut => false
46 fun do_generic timeout_opt command pre post apply st =
47 let val timer = Timer.startRealTimer () in
48 if can_apply timeout_opt pre post apply st then
49 SOME (command, Time.toMilliseconds (Timer.checkRealTimer timer))
56 #> Outer_Syntax.scan Position.start
57 #> filter Token.is_proper
58 #> Scan.read Token.stopper Method.parse
59 #> (fn SOME (Method.Source src) => src | _ => raise Fail "expected Source")
61 fun apply_named_method_on_first_goal method thy =
62 method |> parse_method
65 |> curry Method.SelectGoals 1
67 handle ERROR _ => K Seq.empty (* e.g., the method isn't available yet *)
69 fun add_attr_text (NONE, _) s = s
70 | add_attr_text (_, []) s = s
71 | add_attr_text (SOME x, fs) s =
72 s ^ " " ^ (if x = "" then "" else x ^ ": ") ^ space_implode " " fs
73 fun attrs_text (sx, ix, ex, dx) (ss, is, es, ds) =
74 "" |> fold add_attr_text [(sx, ss), (ix, is), (ex, es), (dx, ds)]
76 fun do_named_method (name, ((all_goals, run_if_auto_try), attrs)) mode
78 if mode <> Auto_Try orelse run_if_auto_try then
79 let val attrs = attrs_text attrs quad in
80 do_generic timeout_opt
81 (name ^ (if all_goals andalso
82 nprems_of (#goal (Proof.goal st)) > 1 then
86 attrs) I (#goal o Proof.goal)
87 (apply_named_method_on_first_goal (name ^ attrs)
88 (Proof.theory_of st)) st
93 val full_attrs = (SOME "simp", SOME "intro", SOME "elim", SOME "dest")
94 val clas_attrs = (NONE, SOME "intro", SOME "elim", SOME "dest")
95 val simp_attrs = (SOME "add", NONE, NONE, NONE)
96 val metis_attrs = (SOME "", SOME "", SOME "", SOME "")
97 val no_attrs = (NONE, NONE, NONE, NONE)
99 (* name * ((all_goals, run_if_auto_try), (simp, intro, elim, dest) *)
101 [("simp", ((false, true), simp_attrs)),
102 ("auto", ((true, true), full_attrs)),
103 ("fast", ((false, false), clas_attrs)),
104 ("fastsimp", ((false, false), full_attrs)),
105 ("force", ((false, false), full_attrs)),
106 ("blast", ((false, true), clas_attrs)),
107 ("metis", ((false, true), metis_attrs)),
108 ("linarith", ((false, true), no_attrs)),
109 ("presburger", ((false, true), no_attrs))]
110 val do_methods = map do_named_method named_methods
112 fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms"
114 fun do_try_methods mode timeout_opt quad st =
116 val st = st |> Proof.map_context (Config.put Metis_Tactics.verbose false)
118 if mode = Normal then
119 "Trying " ^ space_implode " " (Try.serial_commas "and"
120 (map (quote o fst) named_methods)) ^ "..."
121 |> Output.urgent_message
124 case do_methods |> Par_List.map (fn f => f mode timeout_opt quad st)
125 |> map_filter I |> sort (int_ord o pairself snd) of
127 (if mode = Normal then Output.urgent_message "No proof found." else ();
128 (false, (noneN, st)))
129 | xs as (s, _) :: _ =>
131 val xs = xs |> map (fn (s, n) => (n, hd (space_explode " " s)))
132 |> AList.coalesce (op =)
133 |> map (swap o apsnd commas)
134 val need_parens = exists_string (curry (op =) " ") s
137 Auto_Try => "Auto Try Methods found a proof"
138 | Try => "Try Methods found a proof"
139 | Normal => "Try this command") ^ ": " ^
140 Markup.markup Markup.sendback
141 ((if nprems_of (#goal (Proof.goal st)) = 1 then "by"
142 else "apply") ^ " " ^ (s |> need_parens ? enclose "(" ")")) ^
143 "\n(" ^ space_implode "; " (map time_string xs) ^ ").\n"
145 (true, (s, st |> (if mode = Auto_Try then
147 (fn () => Pretty.chunks [Pretty.str "",
148 Pretty.markup Markup.hilite
149 [Pretty.str message]])
151 tap (fn _ => Output.urgent_message message))))
155 fun try_methods timeout_opt = fst oo do_try_methods Normal timeout_opt
157 fun try_methods_trans quad =
158 Toplevel.keep (K () o do_try_methods Normal (SOME default_timeout) quad
161 fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) =
162 (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2)
164 fun string_of_xthm (xref, args) =
165 Facts.string_of_ref xref ^
166 implode (map (enclose "[" "]" o Pretty.str_of
167 o Args.pretty_src @{context}) args)
169 val parse_fact_refs =
170 Scan.repeat1 (Scan.unless (Parse.name -- Args.colon)
171 (Parse_Spec.xthm >> string_of_xthm))
173 Args.$$$ "simp" |-- Args.colon |-- parse_fact_refs
174 >> (fn ss => (ss, [], [], []))
175 || Args.$$$ "intro" |-- Args.colon |-- parse_fact_refs
176 >> (fn is => ([], is, [], []))
177 || Args.$$$ "elim" |-- Args.colon |-- parse_fact_refs
178 >> (fn es => ([], [], es, []))
179 || Args.$$$ "dest" |-- Args.colon |-- parse_fact_refs
180 >> (fn ds => ([], [], [], ds))
182 (Args.parens parse_attrs
183 || Scan.repeat parse_attr
184 >> (fn quad => fold merge_attrs quad ([], [], [], []))) x
186 val parse_try_methods_command =
187 Scan.optional parse_attrs ([], [], [], []) #>> try_methods_trans
190 Outer_Syntax.improper_command try_methodsN
191 "try a combination of proof methods" Keyword.diag
192 parse_try_methods_command
194 fun try_try_methods auto =
195 do_try_methods (if auto then Auto_Try else Try) NONE ([], [], [], [])
197 val setup = Try.register_tool (try_methodsN, (30, auto, try_try_methods))