| author | haftmann |
| Sun, 06 Apr 2025 14:20:41 +0200 | |
| changeset 82447 | 741f6f6df144 |
| parent 82368 | ef3ec45ded4d |
| child 82577 | f3b3d49d84d7 |
| permissions | -rw-r--r-- |
| 82360 | 1 |
(* Title: HOL/Tools/try0_hol.ML |
2 |
Author: Jasmin Blanchette, LMU Muenchen |
|
3 |
Author: Martin Desharnais, LMU Muenchen |
|
4 |
Author: Fabian Huch, TU Muenchen |
|
5 |
||
6 |
General-purpose functions used by Try0. |
|
7 |
*) |
|
8 |
||
9 |
signature TRY0_UTIL = |
|
10 |
sig |
|
11 |
val string_of_xref : Try0.xref -> string |
|
| 82364 | 12 |
|
13 |
type facts_prefixes = |
|
| 82368 | 14 |
{simps : string option,
|
15 |
intros : string option, |
|
16 |
elims : string option, |
|
17 |
dests : string option} |
|
| 82364 | 18 |
val full_attrs : facts_prefixes |
19 |
val clas_attrs : facts_prefixes |
|
20 |
val simp_attrs : facts_prefixes |
|
21 |
val metis_attrs : facts_prefixes |
|
22 |
val no_attrs : facts_prefixes |
|
23 |
val apply_raw_named_method : string -> bool -> facts_prefixes -> |
|
24 |
(Proof.context -> Proof.context) -> Time.time option -> Try0.facts -> Proof.state -> |
|
|
82363
3a7fc54b50ca
tuned and moved configuration of auto_try0 to theory HOL
desharna
parents:
82361
diff
changeset
|
25 |
Try0.result option |
| 82360 | 26 |
end |
27 |
||
28 |
structure Try0_Util : TRY0_UTIL = struct |
|
29 |
||
30 |
fun string_of_xref ((xref, args) : Try0.xref) = |
|
31 |
(case xref of |
|
32 |
Facts.Fact literal => literal |> Symbol_Pos.explode0 |> Symbol_Pos.implode |> cartouche |
|
33 |
| _ => |
|
34 |
Facts.string_of_ref xref) ^ implode |
|
35 |
(map (enclose "[" "]" o Pretty.unformatted_string_of o Token.pretty_src \<^context>) args) |
|
36 |
||
|
82363
3a7fc54b50ca
tuned and moved configuration of auto_try0 to theory HOL
desharna
parents:
82361
diff
changeset
|
37 |
|
| 82364 | 38 |
type facts_prefixes = |
| 82368 | 39 |
{simps : string option,
|
40 |
intros : string option, |
|
41 |
elims : string option, |
|
42 |
dests : string option} |
|
| 82364 | 43 |
|
44 |
val no_attrs : facts_prefixes = |
|
| 82368 | 45 |
{simps = NONE, intros = NONE, elims = NONE, dests = NONE}
|
| 82364 | 46 |
val full_attrs : facts_prefixes = |
| 82368 | 47 |
{simps = SOME "simp: ", intros = SOME "intro: ", elims = SOME "elim: ", dests = SOME "dest: "}
|
| 82364 | 48 |
val clas_attrs : facts_prefixes = |
| 82368 | 49 |
{simps = NONE, intros = SOME "intro: ", elims = SOME "elim: ", dests = SOME "dest: "}
|
| 82364 | 50 |
val simp_attrs : facts_prefixes = |
| 82368 | 51 |
{simps = SOME "add: ", intros = NONE, elims = NONE, dests = NONE}
|
52 |
val metis_attrs : facts_prefixes = |
|
53 |
{simps = SOME "", intros = SOME "", elims = SOME "", dests = SOME ""}
|
|
|
82363
3a7fc54b50ca
tuned and moved configuration of auto_try0 to theory HOL
desharna
parents:
82361
diff
changeset
|
54 |
|
| 82361 | 55 |
local |
56 |
||
57 |
fun parse_method ctxt s = |
|
58 |
enclose "(" ")" s
|
|
59 |
|> Token.explode (Thy_Header.get_keywords' ctxt) Position.start |
|
60 |
|> filter Token.is_proper |
|
61 |
|> Scan.read Token.stopper Method.parse |
|
62 |
|> (fn SOME (Method.Source src, _) => src | _ => raise Fail "expected Source") |
|
63 |
||
64 |
fun run_tac timeout_opt tac st = |
|
65 |
let val with_timeout = |
|
66 |
(case timeout_opt of SOME timeout => Timeout.apply_physical timeout | NONE => I) |
|
67 |
in with_timeout (Seq.pull o tac) st |> Option.map fst end |
|
68 |
||
69 |
val num_goals = Thm.nprems_of o #goal o Proof.goal |
|
70 |
||
71 |
fun apply_recursive recurse elapsed0 timeout_opt apply st = |
|
72 |
(case Timing.timing (Option.join o try (run_tac timeout_opt apply)) st of |
|
73 |
({elapsed, ...}, SOME st') =>
|
|
74 |
if recurse andalso num_goals st' > 0 andalso num_goals st' < num_goals st then |
|
75 |
let val timeout_opt1 = (Option.map (fn timeout => timeout - elapsed) timeout_opt) |
|
76 |
in apply_recursive recurse (elapsed0 + elapsed) timeout_opt1 apply st' end |
|
77 |
else (elapsed0 + elapsed, st') |
|
78 |
|_ => (elapsed0, st)) |
|
79 |
||
80 |
in |
|
81 |
||
| 82364 | 82 |
fun apply_raw_named_method (name : string) all_goals (prefixes: facts_prefixes) |
83 |
(silence_methods : Proof.context -> Proof.context) timeout_opt (facts : Try0.facts) |
|
|
82363
3a7fc54b50ca
tuned and moved configuration of auto_try0 to theory HOL
desharna
parents:
82361
diff
changeset
|
84 |
(st : Proof.state) : Try0.result option = |
| 82361 | 85 |
let |
|
82363
3a7fc54b50ca
tuned and moved configuration of auto_try0 to theory HOL
desharna
parents:
82361
diff
changeset
|
86 |
val st = Proof.map_contexts silence_methods st |
| 82361 | 87 |
val ctxt = Proof.context_of st |
88 |
||
| 82364 | 89 |
val (unused_simps, simps_attrs) = |
| 82368 | 90 |
if null (#simps facts) then |
91 |
([], "") |
|
| 82364 | 92 |
else |
| 82368 | 93 |
(case #simps prefixes of |
94 |
NONE => (#simps facts, "") |
|
95 |
| SOME prefix => ([], " " ^ prefix ^ space_implode "" (map string_of_xref (#simps facts)))) |
|
| 82364 | 96 |
|
97 |
val (unused_intros, intros_attrs) = |
|
| 82368 | 98 |
if null (#intros facts) then |
99 |
([], "") |
|
| 82364 | 100 |
else |
| 82368 | 101 |
(case #intros prefixes of |
102 |
NONE => (#intros facts, "") |
|
103 |
| SOME prefix => ([], " " ^ prefix ^ space_implode "" (map string_of_xref (#intros facts)))) |
|
| 82364 | 104 |
|
105 |
val (unused_elims, elims_attrs) = |
|
| 82368 | 106 |
if null (#elims facts) then |
107 |
([], "") |
|
| 82364 | 108 |
else |
| 82368 | 109 |
(case #elims prefixes of |
110 |
NONE => (#elims facts, "") |
|
111 |
| SOME prefix => ([], " " ^ prefix ^ space_implode "" (map string_of_xref (#elims facts)))) |
|
| 82364 | 112 |
|
113 |
val (unused_dests, dests_attrs) = |
|
| 82368 | 114 |
if null (#dests facts) then |
115 |
([], "") |
|
| 82364 | 116 |
else |
| 82368 | 117 |
(case #dests prefixes of |
118 |
NONE => (#dests facts, "") |
|
119 |
| SOME prefix => ([], " " ^ prefix ^ space_implode "" (map string_of_xref (#dests facts)))) |
|
| 82364 | 120 |
|
121 |
val unused = #usings facts @ unused_simps @ unused_intros @ unused_elims @ unused_dests |
|
122 |
||
123 |
val attrs = simps_attrs ^ intros_attrs ^ elims_attrs ^ dests_attrs |
|
| 82361 | 124 |
val text = |
125 |
name ^ attrs |
|
126 |
|> parse_method ctxt |
|
127 |
|> Method.method_cmd ctxt |
|
128 |
|> Method.Basic |
|
129 |
|> (fn m => Method.Combinator (Method.no_combinator_info, Method.Select_Goals 1, [m])) |
|
130 |
||
131 |
val apply = |
|
| 82364 | 132 |
Proof.using [Attrib.eval_thms ctxt unused |> map (rpair [] o single)] |
133 |
#> Proof.refine text #> Seq.filter_results |
|
| 82361 | 134 |
val num_before = num_goals st |
135 |
val multiple_goals = all_goals andalso num_before > 1 |
|
136 |
val (time, st') = apply_recursive multiple_goals Time.zeroTime timeout_opt apply st |
|
137 |
val num_after = num_goals st' |
|
138 |
val select = "[" ^ string_of_int (num_before - num_after) ^ "]" |
|
139 |
val unused = implode_space (unused |> map string_of_xref) |
|
140 |
val command = |
|
141 |
(if unused <> "" then "using " ^ unused ^ " " else "") ^ |
|
142 |
(if num_after = 0 then "by " else "apply ") ^ |
|
143 |
(name ^ attrs |> attrs <> "" ? enclose "(" ")") ^
|
|
144 |
(if multiple_goals andalso num_after > 0 then select else "") |
|
145 |
in |
|
146 |
if num_before > num_after then |
|
147 |
SOME {name = name, command = command, time = time, state = st'}
|
|
148 |
else NONE |
|
149 |
end |
|
150 |
||
151 |
end |
|
152 |
||
| 82360 | 153 |
end |