author | wenzelm |
Mon, 14 Jul 2008 17:51:42 +0200 | |
changeset 27575 | e540ad3fb50a |
parent 27218 | 4548c83cd508 |
child 28340 | e8597242f649 |
permissions | -rw-r--r-- |
17980 | 1 |
(* Title: Pure/goal.ML |
2 |
ID: $Id$ |
|
3 |
Author: Makarius and Lawrence C Paulson |
|
4 |
||
18139 | 5 |
Goals in tactical theorem proving. |
17980 | 6 |
*) |
7 |
||
8 |
signature BASIC_GOAL = |
|
9 |
sig |
|
10 |
val SELECT_GOAL: tactic -> int -> tactic |
|
23418 | 11 |
val CONJUNCTS: tactic -> int -> tactic |
23414
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
12 |
val PRECISE_CONJUNCTS: int -> tactic -> int -> tactic |
17980 | 13 |
end; |
14 |
||
15 |
signature GOAL = |
|
16 |
sig |
|
17 |
include BASIC_GOAL |
|
18 |
val init: cterm -> thm |
|
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
19 |
val protect: thm -> thm |
17980 | 20 |
val conclude: thm -> thm |
21 |
val finish: thm -> thm |
|
21604
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
22 |
val norm_result: thm -> thm |
23356 | 23 |
val prove_internal: cterm list -> cterm -> (thm list -> tactic) -> thm |
20290 | 24 |
val prove_multi: Proof.context -> string list -> term list -> term list -> |
25 |
({prems: thm list, context: Proof.context} -> tactic) -> thm list |
|
26 |
val prove: Proof.context -> string list -> term list -> term -> |
|
27 |
({prems: thm list, context: Proof.context} -> tactic) -> thm |
|
26713
1c6181def1d0
prove_global: Variable.set_body true, pass context;
wenzelm
parents:
26628
diff
changeset
|
28 |
val prove_global: theory -> string list -> term list -> term -> |
1c6181def1d0
prove_global: Variable.set_body true, pass context;
wenzelm
parents:
26628
diff
changeset
|
29 |
({prems: thm list, context: Proof.context} -> tactic) -> thm |
19184 | 30 |
val extract: int -> int -> thm -> thm Seq.seq |
31 |
val retrofit: int -> int -> thm -> thm -> thm Seq.seq |
|
23418 | 32 |
val conjunction_tac: int -> tactic |
23414
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
33 |
val precise_conjunction_tac: int -> int -> tactic |
23418 | 34 |
val recover_conjunction_tac: tactic |
21687 | 35 |
val norm_hhf_tac: int -> tactic |
36 |
val compose_hhf_tac: thm -> int -> tactic |
|
23237 | 37 |
val assume_rule_tac: Proof.context -> int -> tactic |
17980 | 38 |
end; |
39 |
||
40 |
structure Goal: GOAL = |
|
41 |
struct |
|
42 |
||
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
43 |
(** goals **) |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
44 |
|
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
45 |
(* |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
46 |
-------- (init) |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
47 |
C ==> #C |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
48 |
*) |
20290 | 49 |
val init = |
22902
ac833b4bb7ee
moved some Drule operations to Thm (see more_thm.ML);
wenzelm
parents:
21687
diff
changeset
|
50 |
let val A = #1 (Thm.dest_implies (Thm.cprop_of Drule.protectI)) |
20290 | 51 |
in fn C => Thm.instantiate ([], [(A, C)]) Drule.protectI end; |
17980 | 52 |
|
53 |
(* |
|
18119 | 54 |
C |
55 |
--- (protect) |
|
56 |
#C |
|
17980 | 57 |
*) |
21579 | 58 |
fun protect th = th COMP_INCR Drule.protectI; |
17980 | 59 |
|
60 |
(* |
|
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
61 |
A ==> ... ==> #C |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
62 |
---------------- (conclude) |
17980 | 63 |
A ==> ... ==> C |
64 |
*) |
|
65 |
fun conclude th = |
|
18497 | 66 |
(case SINGLE (Thm.compose_no_flatten false (th, Thm.nprems_of th) 1) |
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
67 |
(Drule.incr_indexes th Drule.protectD) of |
17980 | 68 |
SOME th' => th' |
69 |
| NONE => raise THM ("Failed to conclude goal", 0, [th])); |
|
70 |
||
71 |
(* |
|
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
72 |
#C |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
73 |
--- (finish) |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
74 |
C |
17983 | 75 |
*) |
17980 | 76 |
fun finish th = |
77 |
(case Thm.nprems_of th of |
|
78 |
0 => conclude th |
|
79 |
| n => raise THM ("Proof failed.\n" ^ |
|
80 |
Pretty.string_of (Pretty.chunks (Display.pretty_goals n th)) ^ |
|
81 |
("\n" ^ string_of_int n ^ " unsolved goal(s)!"), 0, [th])); |
|
82 |
||
83 |
||
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
84 |
|
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
85 |
(** results **) |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
86 |
|
21604
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
87 |
val norm_result = |
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
88 |
Drule.flexflex_unique |
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
89 |
#> MetaSimplifier.norm_hhf_protect |
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
90 |
#> Thm.strip_shyps |
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
91 |
#> Drule.zero_var_indexes; |
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
92 |
|
1af327306c8e
added norm/close_result (supercede local_standard etc.);
wenzelm
parents:
21579
diff
changeset
|
93 |
|
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
94 |
|
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
95 |
(** tactical theorem proving **) |
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
96 |
|
23356 | 97 |
(* prove_internal -- minimal checks, no normalization of result! *) |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
98 |
|
23356 | 99 |
fun prove_internal casms cprop tac = |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
100 |
(case SINGLE (tac (map Assumption.assume casms)) (init cprop) of |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
101 |
SOME th => Drule.implies_intr_list casms (finish th) |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
102 |
| NONE => error "Tactic failed."); |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
103 |
|
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
104 |
|
18119 | 105 |
(* prove_multi *) |
17986 | 106 |
|
20056 | 107 |
fun prove_multi ctxt xs asms props tac = |
17980 | 108 |
let |
21516 | 109 |
val thy = ProofContext.theory_of ctxt; |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26713
diff
changeset
|
110 |
val string_of_term = Syntax.string_of_term ctxt; |
20056 | 111 |
|
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
112 |
fun err msg = cat_error msg |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
113 |
("The error(s) above occurred for the goal statement:\n" ^ |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
114 |
string_of_term (Logic.list_implies (asms, Logic.mk_conjunction_list props))); |
17980 | 115 |
|
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
116 |
fun cert_safe t = Thm.cterm_of thy (Envir.beta_norm (Term.no_dummy_patterns t)) |
17980 | 117 |
handle TERM (msg, _) => err msg | TYPE (msg, _, _) => err msg; |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
118 |
val casms = map cert_safe asms; |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
119 |
val cprops = map cert_safe props; |
17980 | 120 |
|
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
121 |
val (prems, ctxt') = ctxt |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
122 |
|> Variable.add_fixes_direct xs |
27218
4548c83cd508
prove: full Variable.declare_term, including constraints;
wenzelm
parents:
26939
diff
changeset
|
123 |
|> fold Variable.declare_term (asms @ props) |
26713
1c6181def1d0
prove_global: Variable.set_body true, pass context;
wenzelm
parents:
26628
diff
changeset
|
124 |
|> Assumption.add_assumes casms |
1c6181def1d0
prove_global: Variable.set_body true, pass context;
wenzelm
parents:
26628
diff
changeset
|
125 |
||> Variable.set_body true; |
17980 | 126 |
|
23418 | 127 |
val goal = init (Conjunction.mk_conjunction_balanced cprops); |
19774
5fe7731d0836
allow non-trivial schematic goals (via embedded term vars);
wenzelm
parents:
19619
diff
changeset
|
128 |
val res = |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
129 |
(case SINGLE (tac {prems = prems, context = ctxt'}) goal of |
19774
5fe7731d0836
allow non-trivial schematic goals (via embedded term vars);
wenzelm
parents:
19619
diff
changeset
|
130 |
NONE => err "Tactic failed." |
5fe7731d0836
allow non-trivial schematic goals (via embedded term vars);
wenzelm
parents:
19619
diff
changeset
|
131 |
| SOME res => res); |
23418 | 132 |
val results = Conjunction.elim_balanced (length props) (finish res) |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
133 |
handle THM (msg, _, _) => err msg; |
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
134 |
val _ = Unify.matches_list thy (map Thm.term_of cprops) (map Thm.prop_of results) |
20056 | 135 |
orelse err ("Proved a different theorem: " ^ string_of_term (Thm.prop_of res)); |
17980 | 136 |
in |
20056 | 137 |
results |
20290 | 138 |
|> map (Assumption.export false ctxt' ctxt) |
20056 | 139 |
|> Variable.export ctxt' ctxt |
20250
c3f209752749
prove: proper assumption context, more tactic arguments;
wenzelm
parents:
20228
diff
changeset
|
140 |
|> map Drule.zero_var_indexes |
17980 | 141 |
end; |
142 |
||
143 |
||
18119 | 144 |
(* prove *) |
17980 | 145 |
|
20056 | 146 |
fun prove ctxt xs asms prop tac = hd (prove_multi ctxt xs asms [prop] tac); |
147 |
||
148 |
fun prove_global thy xs asms prop tac = |
|
26713
1c6181def1d0
prove_global: Variable.set_body true, pass context;
wenzelm
parents:
26628
diff
changeset
|
149 |
Drule.standard (prove (ProofContext.init thy) xs asms prop tac); |
18027
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
150 |
|
09ab79d4e8e1
renamed Goal constant to prop, more general protect/unprotect interfaces;
wenzelm
parents:
17986
diff
changeset
|
151 |
|
17980 | 152 |
|
21687 | 153 |
(** goal structure **) |
154 |
||
155 |
(* nested goals *) |
|
18207 | 156 |
|
19184 | 157 |
fun extract i n st = |
158 |
(if i < 1 orelse n < 1 orelse i + n - 1 > Thm.nprems_of st then Seq.empty |
|
159 |
else if n = 1 then Seq.single (Thm.cprem_of st i) |
|
23418 | 160 |
else |
161 |
Seq.single (Conjunction.mk_conjunction_balanced (map (Thm.cprem_of st) (i upto i + n - 1)))) |
|
20260 | 162 |
|> Seq.map (Thm.adjust_maxidx_cterm ~1 #> init); |
17980 | 163 |
|
19221 | 164 |
fun retrofit i n st' st = |
165 |
(if n = 1 then st |
|
23418 | 166 |
else st |> Drule.with_subgoal i (Conjunction.uncurry_balanced n)) |
19221 | 167 |
|> Thm.compose_no_flatten false (conclude st', Thm.nprems_of st') i; |
18207 | 168 |
|
17980 | 169 |
fun SELECT_GOAL tac i st = |
19191 | 170 |
if Thm.nprems_of st = 1 andalso i = 1 then tac st |
19184 | 171 |
else Seq.lifts (retrofit i 1) (Seq.maps tac (extract i 1 st)) st; |
17980 | 172 |
|
21687 | 173 |
|
174 |
(* multiple goals *) |
|
175 |
||
23418 | 176 |
fun precise_conjunction_tac 0 i = eq_assume_tac i |
177 |
| precise_conjunction_tac 1 i = SUBGOAL (K all_tac) i |
|
178 |
| precise_conjunction_tac n i = PRIMITIVE (Drule.with_subgoal i (Conjunction.curry_balanced n)); |
|
23414
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
179 |
|
23418 | 180 |
val adhoc_conjunction_tac = REPEAT_ALL_NEW |
181 |
(SUBGOAL (fn (goal, i) => |
|
182 |
if can Logic.dest_conjunction goal then rtac Conjunction.conjunctionI i |
|
183 |
else no_tac)); |
|
21687 | 184 |
|
23418 | 185 |
val conjunction_tac = SUBGOAL (fn (goal, i) => |
186 |
precise_conjunction_tac (length (Logic.dest_conjunctions goal)) i ORELSE |
|
187 |
TRY (adhoc_conjunction_tac i)); |
|
21687 | 188 |
|
23418 | 189 |
val recover_conjunction_tac = PRIMITIVE (fn th => |
190 |
Conjunction.uncurry_balanced (Thm.nprems_of th) th); |
|
23414
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
191 |
|
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
192 |
fun PRECISE_CONJUNCTS n tac = |
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
193 |
SELECT_GOAL (precise_conjunction_tac n 1 |
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
194 |
THEN tac |
23418 | 195 |
THEN recover_conjunction_tac); |
23414
927203ad4b3a
tuned conjunction tactics: slightly smaller proof terms;
wenzelm
parents:
23356
diff
changeset
|
196 |
|
21687 | 197 |
fun CONJUNCTS tac = |
198 |
SELECT_GOAL (conjunction_tac 1 |
|
199 |
THEN tac |
|
23418 | 200 |
THEN recover_conjunction_tac); |
21687 | 201 |
|
202 |
||
203 |
(* hhf normal form *) |
|
204 |
||
205 |
val norm_hhf_tac = |
|
206 |
rtac Drule.asm_rl (*cheap approximation -- thanks to builtin Logic.flatten_params*) |
|
207 |
THEN' SUBGOAL (fn (t, i) => |
|
208 |
if Drule.is_norm_hhf t then all_tac |
|
23536
60a1672e298e
moved (asm_)rewrite_goal_tac from goal.ML to meta_simplifier.ML (no longer depends on SELECT_GOAL);
wenzelm
parents:
23418
diff
changeset
|
209 |
else MetaSimplifier.rewrite_goal_tac [Drule.norm_hhf_eq] i); |
21687 | 210 |
|
25301 | 211 |
fun compose_hhf_tac th i st = |
212 |
PRIMSEQ (Thm.bicompose false (false, Drule.lift_all (Thm.cprem_of st i) th, 0) i) st; |
|
21687 | 213 |
|
23237 | 214 |
|
215 |
(* non-atomic goal assumptions *) |
|
216 |
||
23356 | 217 |
fun non_atomic (Const ("==>", _) $ _ $ _) = true |
218 |
| non_atomic (Const ("all", _) $ _) = true |
|
219 |
| non_atomic _ = false; |
|
220 |
||
23237 | 221 |
fun assume_rule_tac ctxt = norm_hhf_tac THEN' CSUBGOAL (fn (goal, i) => |
222 |
let |
|
223 |
val ((_, goal'), ctxt') = Variable.focus goal ctxt; |
|
224 |
val goal'' = Drule.cterm_rule (singleton (Variable.export ctxt' ctxt)) goal'; |
|
23356 | 225 |
val Rs = filter (non_atomic o Thm.term_of) (Drule.strip_imp_prems goal''); |
23237 | 226 |
val tacs = Rs |> map (fn R => |
227 |
Tactic.etac (MetaSimplifier.norm_hhf (Thm.trivial R)) THEN_ALL_NEW assume_tac); |
|
228 |
in fold_rev (curry op APPEND') tacs (K no_tac) i end); |
|
229 |
||
18207 | 230 |
end; |
231 |
||
17980 | 232 |
structure BasicGoal: BASIC_GOAL = Goal; |
233 |
open BasicGoal; |