author | desharna |
Thu, 27 Mar 2025 11:20:59 +0100 | |
changeset 82359 | d2960b321468 |
parent 82358 | cc85ccb1a6b2 |
child 82360 | 6a09257afd06 |
permissions | -rw-r--r-- |
46641 | 1 |
(* Title: HOL/Tools/try0.ML |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
2 |
Author: Jasmin Blanchette, TU Muenchen |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
3 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
4 |
Try a combination of proof methods. |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
5 |
*) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
6 |
|
46641 | 7 |
signature TRY0 = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
8 |
sig |
81369
0677712016b5
try0: add 'use' modifier for thms to insert;
Fabian Huch <huch@in.tum.de>
parents:
81368
diff
changeset
|
9 |
datatype modifier = Use | Simp | Intro | Elim | Dest |
82355 | 10 |
type xref = Facts.ref * Token.src list |
11 |
type tagged_xref = xref * modifier list |
|
82213 | 12 |
type result = {name: string, command: string, time: Time.time, state: Proof.state} |
82358 | 13 |
type proof_method = Time.time option -> tagged_xref list -> Proof.state -> result option |
14 |
type proof_method_options = {run_if_auto_try: bool} |
|
15 |
||
16 |
val register_proof_method : string -> proof_method_options -> proof_method -> unit |
|
17 |
val get_proof_method : string -> proof_method option |
|
18 |
||
82355 | 19 |
val try0 : Time.time option -> tagged_xref list -> Proof.state -> result list |
82359
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
20 |
|
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
21 |
val silence_methods : bool -> Proof.context -> Proof.context |
81365 | 22 |
end |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
23 |
|
46641 | 24 |
structure Try0 : TRY0 = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
25 |
struct |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
26 |
|
81365 | 27 |
val noneN = "none" |
43026
0f15575a6465
handle non-auto try cases gracefully in Try Methods
blanchet
parents:
43024
diff
changeset
|
28 |
|
81365 | 29 |
datatype mode = Auto_Try | Try | Normal |
43020
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
blanchet
parents:
43018
diff
changeset
|
30 |
|
81365 | 31 |
val default_timeout = seconds 5.0 |
38944 | 32 |
|
81369
0677712016b5
try0: add 'use' modifier for thms to insert;
Fabian Huch <huch@in.tum.de>
parents:
81368
diff
changeset
|
33 |
datatype modifier = Use | Simp | Intro | Elim | Dest |
82355 | 34 |
type xref = Facts.ref * Token.src list |
35 |
type tagged_xref = xref * modifier list |
|
81366 | 36 |
|
82213 | 37 |
type result = {name: string, command: string, time: Time.time, state: Proof.state} |
82356 | 38 |
type proof_method = Time.time option -> tagged_xref list -> Proof.state -> result option |
39 |
type proof_method_options = {run_if_auto_try: bool} |
|
40 |
||
41 |
val noop_proof_method : proof_method = fn _ => fn _ => fn _ => NONE |
|
42 |
||
43 |
local |
|
44 |
val proof_methods = |
|
45 |
Synchronized.var "Try0.proof_methods" (Symtab.empty : proof_method Symtab.table); |
|
46 |
val auto_try_proof_methods_names = |
|
47 |
Synchronized.var "Try0.auto_try_proof_methods" (Symset.empty : Symset.T); |
|
48 |
in |
|
49 |
||
50 |
fun register_proof_method name ({run_if_auto_try} : proof_method_options) proof_method = |
|
51 |
let |
|
52 |
val () = if name = "" then error "Registering unnamed proof method" else () |
|
53 |
val () = Synchronized.change proof_methods (Symtab.update_new (name, proof_method)) |
|
54 |
val () = |
|
55 |
if run_if_auto_try then |
|
56 |
Synchronized.change auto_try_proof_methods_names (Symset.insert name) |
|
57 |
else |
|
58 |
() |
|
59 |
in () end |
|
60 |
||
82358 | 61 |
fun get_proof_method (name : string) : proof_method option = |
62 |
Symtab.lookup (Synchronized.value proof_methods) name; |
|
82356 | 63 |
|
82358 | 64 |
fun get_all_proof_methods () : (string * proof_method) list = |
82356 | 65 |
Symtab.fold (fn x => fn xs => x :: xs) (Synchronized.value proof_methods) [] |
66 |
||
82358 | 67 |
fun get_all_proof_method_names () : string list = |
82356 | 68 |
Symtab.fold (fn (name, _) => fn names => name :: names) (Synchronized.value proof_methods) [] |
69 |
||
70 |
fun get_all_auto_try_proof_method_names () : string list = |
|
71 |
Symset.dest (Synchronized.value auto_try_proof_methods_names) |
|
72 |
||
73 |
fun should_auto_try_proof_method (name : string) : bool = |
|
74 |
Symset.member (Synchronized.value auto_try_proof_methods_names) name |
|
75 |
||
76 |
end |
|
77 |
||
78 |
fun get_proof_method_or_noop name = |
|
79 |
(case get_proof_method name of |
|
80 |
NONE => noop_proof_method |
|
81 |
| SOME proof_method => proof_method) |
|
82 |
||
83 |
val apply_proof_method = get_proof_method_or_noop |
|
84 |
||
85 |
fun maybe_apply_proof_method name mode : proof_method = |
|
86 |
if mode <> Auto_Try orelse should_auto_try_proof_method name then |
|
87 |
get_proof_method_or_noop name |
|
88 |
else |
|
89 |
noop_proof_method |
|
90 |
||
91 |
fun time_string time = string_of_int (Time.toMilliseconds time) ^ " ms" |
|
92 |
fun tool_time_string (s, time) = s ^ ": " ^ time_string time |
|
93 |
||
94 |
fun generic_try0 mode timeout_opt (tagged : tagged_xref list) st = |
|
95 |
let |
|
96 |
fun try_method method = method mode timeout_opt tagged st |
|
97 |
fun get_message {command, time, ...} = "Found proof: " ^ Active.sendback_markup_command |
|
98 |
command ^ " (" ^ time_string time ^ ")" |
|
99 |
val print_step = Option.map (tap (writeln o get_message)) |
|
100 |
fun get_results methods : result list = |
|
101 |
if mode = Normal then |
|
102 |
methods |
|
103 |
|> Par_List.map (try_method #> print_step) |
|
104 |
|> map_filter I |
|
105 |
|> sort (Time.compare o apply2 #time) |
|
106 |
else |
|
107 |
methods |
|
108 |
|> Par_List.get_some try_method |
|
109 |
|> the_list |
|
110 |
val proof_method_names = get_all_proof_method_names () |
|
111 |
val maybe_apply_methods = map maybe_apply_proof_method proof_method_names |
|
112 |
in |
|
113 |
if mode = Normal then |
|
114 |
let val names = map quote proof_method_names in |
|
115 |
writeln ("Trying " ^ implode_space (Try.serial_commas "and" names) ^ "...") |
|
116 |
end |
|
117 |
else |
|
118 |
(); |
|
119 |
(case get_results maybe_apply_methods of |
|
120 |
[] => (if mode = Normal then writeln "No proof found" else (); ((false, (noneN, [])), [])) |
|
121 |
| results as {name, command, ...} :: _ => |
|
122 |
let |
|
123 |
val method_times = |
|
124 |
results |
|
125 |
|> map (fn {name, time, ...} => (time, name)) |
|
126 |
|> AList.coalesce (op =) |
|
127 |
|> map (swap o apsnd commas) |
|
128 |
val message = |
|
129 |
(case mode of |
|
130 |
Auto_Try => "Auto Try0 found a proof" |
|
131 |
| Try => "Try0 found a proof" |
|
132 |
| Normal => "Try this") ^ ": " ^ |
|
133 |
Active.sendback_markup_command command ^ |
|
134 |
(case method_times of |
|
135 |
[(_, ms)] => " (" ^ time_string ms ^ ")" |
|
136 |
| method_times => "\n(" ^ space_implode "; " (map tool_time_string method_times) ^ ")") |
|
137 |
in |
|
138 |
((true, (name, if mode = Auto_Try then [message] else (writeln message; []))), results) |
|
139 |
end) |
|
140 |
end |
|
141 |
||
142 |
fun try0 timeout_opt = snd oo generic_try0 Normal timeout_opt |
|
143 |
||
144 |
fun try0_trans tagged = |
|
145 |
Toplevel.keep_proof |
|
146 |
(ignore o generic_try0 Normal (SOME default_timeout) tagged o Toplevel.proof_of) |
|
147 |
||
148 |
val parse_fact_refs = Scan.repeat1 (Scan.unless (Parse.name -- Args.colon) Parse.thm) |
|
149 |
||
150 |
val parse_attr = |
|
151 |
Args.$$$ "simp" |-- Args.colon |-- parse_fact_refs >> (map (rpair [Simp])) |
|
152 |
|| Args.$$$ "intro" |-- Args.colon |-- parse_fact_refs >> (map (rpair [Intro])) |
|
153 |
|| Args.$$$ "elim" |-- Args.colon |-- parse_fact_refs >> (map (rpair [Elim])) |
|
154 |
|| Args.$$$ "dest" |-- Args.colon |-- parse_fact_refs >> (map (rpair [Dest])) |
|
155 |
||
156 |
fun parse_attrs x = |
|
157 |
(Args.parens parse_attrs |
|
158 |
|| Scan.repeat parse_attr >> (fn tagged => fold (curry (op @)) tagged [])) x |
|
159 |
||
160 |
val _ = |
|
161 |
Outer_Syntax.command \<^command_keyword>\<open>try0\<close> "try a combination of proof methods" |
|
162 |
(Scan.optional parse_attrs [] #>> try0_trans) |
|
163 |
||
164 |
val _ = |
|
165 |
Try.tool_setup |
|
166 |
{name = "try0", weight = 30, auto_option = \<^system_option>\<open>auto_methods\<close>, |
|
167 |
body = fn auto => fst o generic_try0 (if auto then Auto_Try else Try) NONE []} |
|
168 |
||
82359
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
169 |
(* Makes reconstructor tools as silent as possible. The "set_visible" calls suppresses "Unification |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
170 |
bound exceeded" warnings and the like. *) |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
171 |
fun silence_methods debug = |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
172 |
Config.put Metis_Tactic.verbose debug |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
173 |
#> not debug ? (fn ctxt => |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
174 |
ctxt |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
175 |
|> Simplifier_Trace.disable |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
176 |
|> Context_Position.set_visible false |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
177 |
|> Config.put Unify.unify_trace false |
d2960b321468
moved HOL-specific code out of ML file for generic try0
desharna
parents:
82358
diff
changeset
|
178 |
|> Config.put Argo_Tactic.trace "none") |
82216 | 179 |
|
180 |
end |