1 (* Title: Tools/solve_direct.ML
2 Author: Timothy Bourke and Gerwin Klein, NICTA
4 Check whether a newly stated theorem can be solved directly by an
5 existing theorem. Duplicate lemmas can be detected in this way.
7 The implementation relies critically on the Find_Theorems solves
11 signature SOLVE_DIRECT =
13 val auto : bool Unsynchronized.ref
14 val max_solutions : int Unsynchronized.ref
15 val setup : theory -> theory
18 structure Solve_Direct : SOLVE_DIRECT =
21 val solve_directN = "solve_direct";
26 val auto = Unsynchronized.ref false;
27 val max_solutions = Unsynchronized.ref 5;
30 ProofGeneralPgip.add_preference Preferences.category_tracing
31 (Unsynchronized.setmp auto true (fn () =>
32 Preferences.bool_pref auto
33 ("Auto " ^ solve_directN)
34 ("Run " ^ quote solve_directN ^ " automatically.")) ());
37 (* solve_direct command *)
39 fun solve_direct auto state =
41 val ctxt = Proof.context_of state;
43 val crits = [(true, Find_Theorems.Solves)];
45 snd (Find_Theorems.find_theorems ctxt (SOME g) (SOME (! max_solutions)) false crits);
47 fun prt_result (goal, results) =
50 (if auto then "Auto " else "") ^ solve_directN ^ ": " ^
52 NONE => "The current goal"
53 | SOME sg => Syntax.string_of_term ctxt (Thm.term_of sg));
56 (msg ^ " can be solved directly with")
57 (map (Find_Theorems.pretty_thm ctxt) results)
60 fun seek_against_goal () =
61 (case try Proof.simple_goal state of
63 | SOME {goal = st, ...} =>
65 val subgoals = Drule.strip_imp_prems (Thm.cprop_of st);
67 if length subgoals = 1
68 then [(NONE, find st)]
69 else map (fn sg => (SOME sg, find (Goal.init sg))) subgoals;
70 val results = filter_out (null o snd) rs;
71 in if null results then NONE else SOME results end);
72 fun message results = separate (Pretty.brk 0) (map prt_result results);
74 (case try seek_against_goal () of
75 SOME (SOME results) =>
83 Pretty.markup Markup.hilite (message results)])
86 priority (Pretty.string_of (Pretty.chunks (message results))))))
87 | _ => (false, state))
90 val invoke_solve_direct = fst o solve_direct false;
93 Outer_Syntax.improper_command solve_directN
94 "try to solve conjectures directly with existing theorems" Keyword.diag
95 (Scan.succeed (Toplevel.keep (K () o invoke_solve_direct o Toplevel.proof_of)));
100 fun auto_solve_direct state =
101 if not (! auto) then (false, state) else solve_direct true state;
103 val setup = Auto_Tools.register_tool (solve_directN, auto_solve_direct);