author | wenzelm |
Tue, 31 Mar 2009 20:40:25 +0200 | |
changeset 30822 | 8aac4b974280 |
parent 30785 | 15f64e05e703 |
child 30825 | 14d24e1fe594 |
permissions | -rw-r--r-- |
30147 | 1 |
(* Title: Pure/Tools/auto_solve.ML |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
2 |
Author: Timothy Bourke and Gerwin Klein, NICTA |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
3 |
|
30147 | 4 |
Check whether a newly stated theorem can be solved directly by an |
5 |
existing theorem. Duplicate lemmas can be detected in this way. |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
6 |
|
30147 | 7 |
The implemenation is based in part on Berghofer and Haftmann's |
8 |
Pure/codegen.ML. It relies critically on the FindTheorems solves |
|
9 |
feature. |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
10 |
*) |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
11 |
|
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
12 |
signature AUTO_SOLVE = |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
13 |
sig |
30147 | 14 |
val auto : bool ref |
15 |
val auto_time_limit : int ref |
|
30785
15f64e05e703
Limit the number of results returned by auto_solves.
Timothy Bourke
parents:
30242
diff
changeset
|
16 |
val limit : int ref |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
17 |
|
30147 | 18 |
val seek_solution : bool -> Proof.state -> Proof.state |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
19 |
end; |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
20 |
|
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
21 |
structure AutoSolve : AUTO_SOLVE = |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
22 |
struct |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
23 |
|
30147 | 24 |
val auto = ref false; |
25 |
val auto_time_limit = ref 2500; |
|
30785
15f64e05e703
Limit the number of results returned by auto_solves.
Timothy Bourke
parents:
30242
diff
changeset
|
26 |
val limit = ref 5; |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
27 |
|
30147 | 28 |
fun seek_solution int state = |
29 |
let |
|
30 |
val ctxt = Proof.context_of state; |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
31 |
|
30147 | 32 |
fun conj_to_list [] = [] |
33 |
| conj_to_list (t::ts) = |
|
34 |
(Conjunction.dest_conjunction t |
|
35 |
|> (fn (t1, t2) => conj_to_list (t1::t2::ts))) |
|
36 |
handle TERM _ => t::conj_to_list ts; |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
37 |
|
30147 | 38 |
val crits = [(true, FindTheorems.Solves)]; |
30822 | 39 |
fun find g = (NONE, snd (FindTheorems.find_theorems ctxt g (SOME (! limit)) false crits)); |
40 |
fun find_cterm g = |
|
41 |
(SOME g, snd (FindTheorems.find_theorems ctxt |
|
42 |
(SOME (Goal.init g)) (SOME (! limit)) false crits)); |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
43 |
|
30147 | 44 |
fun prt_result (goal, results) = |
45 |
let |
|
30822 | 46 |
val msg = |
47 |
(case goal of |
|
48 |
NONE => "The current goal" |
|
49 |
| SOME g => Syntax.string_of_term ctxt (Thm.term_of g)); |
|
30147 | 50 |
in |
30822 | 51 |
Pretty.big_list |
52 |
(msg ^ " could be solved directly with:") |
|
53 |
(map (FindTheorems.pretty_thm ctxt) results) |
|
30147 | 54 |
end; |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
55 |
|
30147 | 56 |
fun seek_against_goal () = |
57 |
let |
|
58 |
val goal = try Proof.get_goal state |
|
30822 | 59 |
|> Option.map (#2 o #2); |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
60 |
|
30147 | 61 |
val goals = goal |
30822 | 62 |
|> Option.map (fn g => cprem_of g 1) |
63 |
|> the_list |
|
64 |
|> conj_to_list; |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
65 |
|
30822 | 66 |
val rs = |
67 |
if length goals = 1 |
|
68 |
then [find goal] |
|
69 |
else map find_cterm goals; |
|
70 |
val results = filter_out (null o snd) rs; |
|
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
71 |
|
30822 | 72 |
in if null results then NONE else SOME results end; |
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
73 |
|
30147 | 74 |
fun go () = |
75 |
let |
|
30822 | 76 |
val res = |
77 |
TimeLimit.timeLimit (Time.fromMilliseconds (! auto_time_limit)) |
|
78 |
(try seek_against_goal) (); |
|
30147 | 79 |
in |
30822 | 80 |
(case res of |
81 |
SOME (SOME results) => |
|
82 |
state |> Proof.goal_message |
|
83 |
(fn () => Pretty.chunks |
|
84 |
[Pretty.str "", |
|
85 |
Pretty.markup Markup.hilite |
|
86 |
(separate (Pretty.brk 0) (map prt_result results))]) |
|
87 |
| NONE => state) |
|
30147 | 88 |
end handle TimeLimit.TimeOut => (warning "AutoSolve: timeout."; state); |
89 |
in |
|
90 |
if int andalso ! auto andalso not (! Toplevel.quiet) |
|
91 |
then go () |
|
92 |
else state |
|
93 |
end; |
|
94 |
||
29858
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
95 |
end; |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
96 |
|
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
97 |
val _ = Context.>> (Specification.add_theorem_hook AutoSolve.seek_solution); |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
98 |
|
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
99 |
val auto_solve = AutoSolve.auto; |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
100 |
val auto_solve_time_limit = AutoSolve.auto_time_limit; |
c8cee17d7e50
Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff
changeset
|
101 |