author | blanchet |
Mon, 27 Sep 2010 09:17:24 +0200 | |
changeset 39719 | b876d7525e72 |
parent 39616 | 8052101883c3 |
child 40113 | 1f61f0826e8a |
permissions | -rw-r--r-- |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
1 |
(* Title: HOL/Tools/try.ML |
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. |
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
5 |
|
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
6 |
FIXME: reintroduce or remove commented code (see also HOL.thy) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
7 |
*) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
8 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
9 |
signature TRY = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
10 |
sig |
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
11 |
(* |
39331 | 12 |
val auto : bool Unsynchronized.ref |
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
13 |
*) |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
14 |
val invoke_try : Time.time option -> Proof.state -> bool |
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
15 |
(* |
39331 | 16 |
val setup : theory -> theory |
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
17 |
*) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
18 |
end; |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
19 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
20 |
structure Try : TRY = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
21 |
struct |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
22 |
|
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
23 |
(* |
39331 | 24 |
val auto = Unsynchronized.ref false |
25 |
||
39333 | 26 |
val _ = |
27 |
ProofGeneralPgip.add_preference Preferences.category_tracing |
|
39616
8052101883c3
renamed setmp_noncritical to Unsynchronized.setmp to emphasize its meaning;
wenzelm
parents:
39547
diff
changeset
|
28 |
(Unsynchronized.setmp auto true (fn () => |
39333 | 29 |
Preferences.bool_pref auto |
30 |
"auto-try" "Try standard proof methods.") ()); |
|
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
31 |
*) |
39333 | 32 |
|
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
33 |
val default_timeout = Time.fromSeconds 5 |
38944 | 34 |
|
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
35 |
fun can_apply timeout_opt pre post tac st = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
36 |
let val {goal, ...} = Proof.goal st in |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
37 |
case (case timeout_opt of |
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
38 |
SOME timeout => TimeLimit.timeLimit timeout |
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
39 |
| NONE => fn f => fn x => f x) (Seq.pull o tac) (pre st) of |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
40 |
SOME (x, _) => nprems_of (post x) < nprems_of goal |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
41 |
| NONE => false |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
42 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
43 |
|
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
44 |
fun do_generic timeout_opt command pre post apply st = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
45 |
let val timer = Timer.startRealTimer () in |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
46 |
if can_apply timeout_opt pre post apply st then |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
47 |
SOME (command, Time.toMilliseconds (Timer.checkRealTimer timer)) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
48 |
else |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
49 |
NONE |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
50 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
51 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
52 |
fun named_method thy name = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
53 |
Method.method thy (Args.src ((name, []), Position.none)) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
54 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
55 |
fun apply_named_method name ctxt = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
56 |
let val thy = ProofContext.theory_of ctxt in |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
57 |
Method.apply (named_method thy name) ctxt [] |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
58 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
59 |
|
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
60 |
fun do_named_method name timeout_opt st = |
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
61 |
do_generic timeout_opt name (#goal o Proof.goal) snd |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
62 |
(apply_named_method name (Proof.context_of st)) st |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
63 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
64 |
fun apply_named_method_on_first_goal name ctxt = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
65 |
let val thy = ProofContext.theory_of ctxt in |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
66 |
Proof.refine (Method.SelectGoals (1, Method.Basic (named_method thy name))) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
67 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
68 |
|
39547 | 69 |
fun do_named_method (name, all_goals) timeout_opt st = |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
70 |
do_generic timeout_opt |
39547 | 71 |
(name ^ (if all_goals andalso |
72 |
nprems_of (#goal (Proof.goal st)) > 1 then |
|
73 |
"[1]" |
|
74 |
else |
|
75 |
"")) I (#goal o Proof.goal) |
|
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
76 |
(apply_named_method_on_first_goal name (Proof.context_of st)) st |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
77 |
|
39547 | 78 |
val named_methods = |
79 |
[("simp", false), ("auto", true), ("fast", false), ("fastsimp", false), |
|
80 |
("force", false), ("blast", false), ("arith", false)] |
|
81 |
val do_methods = map do_named_method named_methods |
|
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
82 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
83 |
fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms" |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
84 |
|
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
85 |
fun do_try auto timeout_opt st = |
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
86 |
case do_methods |> Par_List.map (fn f => f timeout_opt st) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
87 |
|> map_filter I |> sort (int_ord o pairself snd) of |
39331 | 88 |
[] => (if auto then () else writeln "No proof found."; (false, st)) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
89 |
| xs as (s, _) :: _ => |
39331 | 90 |
let |
39332 | 91 |
val xs = xs |> map swap |> AList.coalesce (op =) |
92 |
|> map (swap o apsnd commas) |
|
39331 | 93 |
val message = |
94 |
(if auto then "Auto Try found a proof" else "Try this command") ^ ": " ^ |
|
38946 | 95 |
Markup.markup Markup.sendback |
96 |
((if nprems_of (#goal (Proof.goal st)) = 1 then "by" else "apply") ^ |
|
97 |
" " ^ s) ^ |
|
39331 | 98 |
".\n(" ^ space_implode "; " (map time_string xs) ^ ")\n" |
99 |
in |
|
100 |
(true, st |> (if auto then |
|
101 |
Proof.goal_message |
|
102 |
(fn () => Pretty.chunks [Pretty.str "", |
|
103 |
Pretty.markup Markup.hilite |
|
104 |
[Pretty.str message]]) |
|
105 |
else |
|
106 |
tap (fn _ => priority message))) |
|
107 |
end |
|
108 |
||
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
109 |
val invoke_try = fst oo do_try false |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
110 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
111 |
val tryN = "try" |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
112 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
113 |
val _ = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
114 |
Outer_Syntax.improper_command tryN |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
115 |
"try a combination of proof methods" Keyword.diag |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
116 |
(Scan.succeed (Toplevel.keep (K () o do_try false (SOME default_timeout) |
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
117 |
o Toplevel.proof_of))) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
118 |
|
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
119 |
(* |
39336
1899349a5026
change signature of "Try.invoke_try" to make it more flexible
blanchet
parents:
39334
diff
changeset
|
120 |
fun auto_try st = if not (!auto) then (false, st) else do_try true NONE st |
39331 | 121 |
|
122 |
val setup = Auto_Tools.register_tool (tryN, auto_try) |
|
39719
b876d7525e72
comment out Auto Try until issues are resolved (automatically on by default even though the code says off; thread that continues in the background)
blanchet
parents:
39616
diff
changeset
|
123 |
*) |
39331 | 124 |
|
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
125 |
end; |