author | blanchet |
Mon, 13 Sep 2010 09:36:34 +0200 | |
changeset 39334 | c0bb925ae912 |
parent 39333 | c277c79fb9db |
child 39336 | 1899349a5026 |
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. |
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 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
7 |
signature TRY = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
8 |
sig |
39331 | 9 |
val auto : bool Unsynchronized.ref |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
10 |
val invoke_try : Proof.state -> unit |
39331 | 11 |
val setup : theory -> theory |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
12 |
end; |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
13 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
14 |
structure Try : TRY = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
15 |
struct |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
16 |
|
39331 | 17 |
val auto = Unsynchronized.ref false |
18 |
||
39333 | 19 |
val _ = |
20 |
ProofGeneralPgip.add_preference Preferences.category_tracing |
|
21 |
(setmp_noncritical auto true (fn () => |
|
22 |
Preferences.bool_pref auto |
|
23 |
"auto-try" "Try standard proof methods.") ()); |
|
24 |
||
38944 | 25 |
val timeout = Time.fromSeconds 5 |
26 |
||
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
27 |
fun can_apply auto pre post tac st = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
28 |
let val {goal, ...} = Proof.goal st in |
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
29 |
case (if auto then fn f => fn x => f x |
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
30 |
else TimeLimit.timeLimit timeout) (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
|
31 |
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
|
32 |
| NONE => false |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
33 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
34 |
|
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
35 |
fun do_generic auto command pre post apply st = |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
36 |
let val timer = Timer.startRealTimer () in |
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
37 |
if can_apply auto pre post apply st then |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
38 |
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
|
39 |
else |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
40 |
NONE |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
41 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
42 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
43 |
fun named_method thy name = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
44 |
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
|
45 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
46 |
fun apply_named_method name ctxt = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
47 |
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
|
48 |
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
|
49 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
50 |
|
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
51 |
fun do_named_method name auto st = |
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
52 |
do_generic auto 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
|
53 |
(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
|
54 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
55 |
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
|
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 |
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
|
58 |
end |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
59 |
|
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
60 |
fun do_named_method_on_first_goal name auto st = |
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
61 |
do_generic auto (name ^ (if nprems_of (#goal (Proof.goal st)) > 1 then "[1]" |
38944 | 62 |
else "")) I (#goal o Proof.goal) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
63 |
(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
|
64 |
|
39222
decf607a5a67
remove "safe" (as suggested by Tobias) and added "arith" to "try"
blanchet
parents:
38946
diff
changeset
|
65 |
val all_goals_named_methods = ["auto"] |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
66 |
val first_goal_named_methods = |
39222
decf607a5a67
remove "safe" (as suggested by Tobias) and added "arith" to "try"
blanchet
parents:
38946
diff
changeset
|
67 |
["simp", "fast", "fastsimp", "force", "best", "blast", "arith"] |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
68 |
val do_methods = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
69 |
map do_named_method_on_first_goal all_goals_named_methods @ |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
70 |
map do_named_method first_goal_named_methods |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
71 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
72 |
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
|
73 |
|
39331 | 74 |
fun do_try auto st = |
39334
c0bb925ae912
no timeout for Auto Try, since the Auto Tools framework takes care of timeouts
blanchet
parents:
39333
diff
changeset
|
75 |
case do_methods |> Par_List.map (fn f => f auto st) |
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
76 |
|> map_filter I |> sort (int_ord o pairself snd) of |
39331 | 77 |
[] => (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
|
78 |
| xs as (s, _) :: _ => |
39331 | 79 |
let |
39332 | 80 |
val xs = xs |> map swap |> AList.coalesce (op =) |
81 |
|> map (swap o apsnd commas) |
|
39331 | 82 |
val message = |
83 |
(if auto then "Auto Try found a proof" else "Try this command") ^ ": " ^ |
|
38946 | 84 |
Markup.markup Markup.sendback |
85 |
((if nprems_of (#goal (Proof.goal st)) = 1 then "by" else "apply") ^ |
|
86 |
" " ^ s) ^ |
|
39331 | 87 |
".\n(" ^ space_implode "; " (map time_string xs) ^ ")\n" |
88 |
in |
|
89 |
(true, st |> (if auto then |
|
90 |
Proof.goal_message |
|
91 |
(fn () => Pretty.chunks [Pretty.str "", |
|
92 |
Pretty.markup Markup.hilite |
|
93 |
[Pretty.str message]]) |
|
94 |
else |
|
95 |
tap (fn _ => priority message))) |
|
96 |
end |
|
97 |
||
98 |
val invoke_try = do_try false #> K () |
|
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
99 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
100 |
val tryN = "try" |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
101 |
|
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
102 |
val _ = |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
103 |
Outer_Syntax.improper_command tryN |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
104 |
"try a combination of proof methods" Keyword.diag |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
105 |
(Scan.succeed (Toplevel.keep (invoke_try o Toplevel.proof_of))) |
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
106 |
|
39331 | 107 |
fun auto_try st = if not (!auto) then (false, st) else do_try true st |
108 |
||
109 |
val setup = Auto_Tools.register_tool (tryN, auto_try) |
|
110 |
||
38942
e10c11971fa7
"try" -- a new diagnosis tool that tries to apply several methods in parallel
blanchet
parents:
diff
changeset
|
111 |
end; |