|
1 (* Title: Tools/auto_solve.ML |
|
2 Author: Timothy Bourke and Gerwin Klein, NICTA |
|
3 |
|
4 Check whether a newly stated theorem can be solved directly by an |
|
5 existing theorem. Duplicate lemmas can be detected in this way. |
|
6 |
|
7 The implementation is based in part on Berghofer and Haftmann's |
|
8 quickcheck.ML. It relies critically on the FindTheorems solves |
|
9 feature. |
|
10 *) |
|
11 |
|
12 signature AUTO_SOLVE = |
|
13 sig |
|
14 val auto : bool ref |
|
15 val auto_time_limit : int ref |
|
16 val limit : int ref |
|
17 |
|
18 val seek_solution : bool -> Proof.state -> Proof.state |
|
19 end; |
|
20 |
|
21 structure AutoSolve : AUTO_SOLVE = |
|
22 struct |
|
23 |
|
24 val auto = ref false; |
|
25 val auto_time_limit = ref 2500; |
|
26 val limit = ref 5; |
|
27 |
|
28 fun seek_solution int state = |
|
29 let |
|
30 val ctxt = Proof.context_of state; |
|
31 |
|
32 val crits = [(true, FindTheorems.Solves)]; |
|
33 fun find g = snd (FindTheorems.find_theorems ctxt (SOME g) (SOME (! limit)) false crits); |
|
34 |
|
35 fun prt_result (goal, results) = |
|
36 let |
|
37 val msg = |
|
38 (case goal of |
|
39 NONE => "The current goal" |
|
40 | SOME sg => Syntax.string_of_term ctxt (Thm.term_of sg)); |
|
41 in |
|
42 Pretty.big_list |
|
43 (msg ^ " could be solved directly with:") |
|
44 (map (FindTheorems.pretty_thm ctxt) results) |
|
45 end; |
|
46 |
|
47 fun seek_against_goal () = |
|
48 (case try Proof.get_goal state of |
|
49 NONE => NONE |
|
50 | SOME (_, (_, goal)) => |
|
51 let |
|
52 val subgoals = Conjunction.dest_conjunctions (Thm.cprem_of goal 1); |
|
53 val rs = |
|
54 if length subgoals = 1 |
|
55 then [(NONE, find goal)] |
|
56 else map (fn sg => (SOME sg, find (Goal.init sg))) subgoals; |
|
57 val results = filter_out (null o snd) rs; |
|
58 in if null results then NONE else SOME results end); |
|
59 |
|
60 fun go () = |
|
61 let |
|
62 val res = |
|
63 TimeLimit.timeLimit (Time.fromMilliseconds (! auto_time_limit)) |
|
64 (try seek_against_goal) (); |
|
65 in |
|
66 (case res of |
|
67 SOME (SOME results) => |
|
68 state |> Proof.goal_message |
|
69 (fn () => Pretty.chunks |
|
70 [Pretty.str "", |
|
71 Pretty.markup Markup.hilite |
|
72 (separate (Pretty.brk 0) (map prt_result results))]) |
|
73 | _ => state) |
|
74 end handle TimeLimit.TimeOut => (warning "AutoSolve: timeout."; state); |
|
75 in |
|
76 if int andalso ! auto andalso not (! Toplevel.quiet) |
|
77 then go () |
|
78 else state |
|
79 end; |
|
80 |
|
81 end; |
|
82 |
|
83 val _ = Context.>> (Specification.add_theorem_hook AutoSolve.seek_solution); |
|
84 |
|
85 val auto_solve = AutoSolve.auto; |
|
86 val auto_solve_time_limit = AutoSolve.auto_time_limit; |
|
87 |