wenzelm@12191
|
1 |
(* Title: ZF/Tools/inductive_package.ML
|
paulson@6051
|
2 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
paulson@6051
|
3 |
|
paulson@6051
|
4 |
Fixedpoint definition module -- for Inductive/Coinductive Definitions
|
paulson@6051
|
5 |
|
paulson@6051
|
6 |
The functor will be instantiated for normal sums/products (inductive defs)
|
paulson@6051
|
7 |
and non-standard sums/products (coinductive defs)
|
paulson@6051
|
8 |
|
paulson@6051
|
9 |
Sums are used only for mutual recursion;
|
paulson@6051
|
10 |
Products are used only to derive "streamlined" induction rules for relations
|
paulson@6051
|
11 |
*)
|
paulson@6051
|
12 |
|
paulson@6051
|
13 |
type inductive_result =
|
paulson@6051
|
14 |
{defs : thm list, (*definitions made in thy*)
|
paulson@6051
|
15 |
bnd_mono : thm, (*monotonicity for the lfp definition*)
|
paulson@6051
|
16 |
dom_subset : thm, (*inclusion of recursive set in dom*)
|
paulson@6051
|
17 |
intrs : thm list, (*introduction rules*)
|
paulson@6051
|
18 |
elim : thm, (*case analysis theorem*)
|
paulson@6141
|
19 |
mk_cases : string -> thm, (*generates case theorems*)
|
paulson@6051
|
20 |
induct : thm, (*main induction rule*)
|
paulson@6051
|
21 |
mutual_induct : thm}; (*mutual induction rule*)
|
paulson@6051
|
22 |
|
paulson@6051
|
23 |
|
paulson@6051
|
24 |
(*Functor's result signature*)
|
paulson@6051
|
25 |
signature INDUCTIVE_PACKAGE =
|
wenzelm@12132
|
26 |
sig
|
paulson@6051
|
27 |
(*Insert definitions for the recursive sets, which
|
paulson@6051
|
28 |
must *already* be declared as constants in parent theory!*)
|
wenzelm@12132
|
29 |
val add_inductive_i: bool -> term list * term ->
|
haftmann@28965
|
30 |
((Binding.T * term) * attribute list) list ->
|
wenzelm@12132
|
31 |
thm list * thm list * thm list * thm list -> theory -> theory * inductive_result
|
wenzelm@12132
|
32 |
val add_inductive: string list * string ->
|
haftmann@28965
|
33 |
((Binding.T * string) * Attrib.src list) list ->
|
wenzelm@26336
|
34 |
(Facts.ref * Attrib.src list) list * (Facts.ref * Attrib.src list) list *
|
wenzelm@26336
|
35 |
(Facts.ref * Attrib.src list) list * (Facts.ref * Attrib.src list) list ->
|
wenzelm@12132
|
36 |
theory -> theory * inductive_result
|
wenzelm@12132
|
37 |
end;
|
paulson@6051
|
38 |
|
paulson@6051
|
39 |
|
paulson@6051
|
40 |
(*Declares functions to add fixedpoint/constructor defs to a theory.
|
paulson@6051
|
41 |
Recursive sets must *already* be declared as constants.*)
|
wenzelm@12132
|
42 |
functor Add_inductive_def_Fun
|
wenzelm@12132
|
43 |
(structure Fp: FP and Pr : PR and CP: CARTPROD and Su : SU val coind: bool)
|
paulson@6051
|
44 |
: INDUCTIVE_PACKAGE =
|
paulson@6051
|
45 |
struct
|
wenzelm@12183
|
46 |
|
wenzelm@16855
|
47 |
open Ind_Syntax;
|
paulson@6051
|
48 |
|
wenzelm@12227
|
49 |
val co_prefix = if coind then "co" else "";
|
wenzelm@12227
|
50 |
|
wenzelm@7695
|
51 |
|
wenzelm@7695
|
52 |
(* utils *)
|
wenzelm@7695
|
53 |
|
wenzelm@7695
|
54 |
(*make distinct individual variables a1, a2, a3, ..., an. *)
|
wenzelm@7695
|
55 |
fun mk_frees a [] = []
|
wenzelm@12902
|
56 |
| mk_frees a (T::Ts) = Free(a,T) :: mk_frees (Symbol.bump_string a) Ts;
|
wenzelm@7695
|
57 |
|
wenzelm@7695
|
58 |
|
wenzelm@7695
|
59 |
(* add_inductive(_i) *)
|
wenzelm@7695
|
60 |
|
paulson@6051
|
61 |
(*internal version, accepting terms*)
|
wenzelm@12132
|
62 |
fun add_inductive_i verbose (rec_tms, dom_sum)
|
wenzelm@28083
|
63 |
raw_intr_specs (monos, con_defs, type_intrs, type_elims) thy =
|
wenzelm@12132
|
64 |
let
|
krauss@26056
|
65 |
val _ = Theory.requires thy "Inductive_ZF" "(co)inductive definitions";
|
wenzelm@26189
|
66 |
val ctxt = ProofContext.init thy;
|
paulson@6051
|
67 |
|
haftmann@28999
|
68 |
val intr_specs = map (apfst (apfst Binding.base_name)) raw_intr_specs;
|
wenzelm@12191
|
69 |
val (intr_names, intr_tms) = split_list (map fst intr_specs);
|
wenzelm@12191
|
70 |
val case_names = RuleCases.case_names intr_names;
|
paulson@6051
|
71 |
|
paulson@6051
|
72 |
(*recT and rec_params should agree for all mutually recursive components*)
|
paulson@6051
|
73 |
val rec_hds = map head_of rec_tms;
|
paulson@6051
|
74 |
|
paulson@6051
|
75 |
val dummy = assert_all is_Const rec_hds
|
wenzelm@12132
|
76 |
(fn t => "Recursive set not previously declared as constant: " ^
|
wenzelm@26189
|
77 |
Syntax.string_of_term ctxt t);
|
paulson@6051
|
78 |
|
paulson@6051
|
79 |
(*Now we know they are all Consts, so get their names, type and params*)
|
paulson@6051
|
80 |
val rec_names = map (#1 o dest_Const) rec_hds
|
paulson@6051
|
81 |
and (Const(_,recT),rec_params) = strip_comb (hd rec_tms);
|
paulson@6051
|
82 |
|
paulson@6051
|
83 |
val rec_base_names = map Sign.base_name rec_names;
|
paulson@6051
|
84 |
val dummy = assert_all Syntax.is_identifier rec_base_names
|
paulson@6051
|
85 |
(fn a => "Base name of recursive set not an identifier: " ^ a);
|
paulson@6051
|
86 |
|
paulson@6051
|
87 |
local (*Checking the introduction rules*)
|
wenzelm@20342
|
88 |
val intr_sets = map (#2 o rule_concl_msg thy) intr_tms;
|
paulson@6051
|
89 |
fun intr_ok set =
|
wenzelm@12132
|
90 |
case head_of set of Const(a,recT) => a mem rec_names | _ => false;
|
paulson@6051
|
91 |
in
|
paulson@6051
|
92 |
val dummy = assert_all intr_ok intr_sets
|
wenzelm@12132
|
93 |
(fn t => "Conclusion of rule does not name a recursive set: " ^
|
wenzelm@26189
|
94 |
Syntax.string_of_term ctxt t);
|
paulson@6051
|
95 |
end;
|
paulson@6051
|
96 |
|
paulson@6051
|
97 |
val dummy = assert_all is_Free rec_params
|
paulson@6051
|
98 |
(fn t => "Param in recursion term not a free variable: " ^
|
wenzelm@26189
|
99 |
Syntax.string_of_term ctxt t);
|
paulson@6051
|
100 |
|
paulson@6051
|
101 |
(*** Construct the fixedpoint definition ***)
|
wenzelm@29270
|
102 |
val mk_variant = Name.variant (foldr OldTerm.add_term_names [] intr_tms);
|
paulson@6051
|
103 |
|
paulson@6051
|
104 |
val z' = mk_variant"z" and X' = mk_variant"X" and w' = mk_variant"w";
|
paulson@6051
|
105 |
|
paulson@6051
|
106 |
fun dest_tprop (Const("Trueprop",_) $ P) = P
|
wenzelm@12132
|
107 |
| dest_tprop Q = error ("Ill-formed premise of introduction rule: " ^
|
wenzelm@26189
|
108 |
Syntax.string_of_term ctxt Q);
|
paulson@6051
|
109 |
|
paulson@6051
|
110 |
(*Makes a disjunct from an introduction rule*)
|
paulson@6051
|
111 |
fun fp_part intr = (*quantify over rule's free vars except parameters*)
|
wenzelm@16855
|
112 |
let val prems = map dest_tprop (Logic.strip_imp_prems intr)
|
skalberg@15570
|
113 |
val dummy = List.app (fn rec_hd => List.app (chk_prem rec_hd) prems) rec_hds
|
wenzelm@29265
|
114 |
val exfrees = OldTerm.term_frees intr \\ rec_params
|
wenzelm@12132
|
115 |
val zeq = FOLogic.mk_eq (Free(z',iT), #1 (rule_concl intr))
|
skalberg@15574
|
116 |
in foldr FOLogic.mk_exists
|
wenzelm@23419
|
117 |
(BalancedTree.make FOLogic.mk_conj (zeq::prems)) exfrees
|
paulson@6051
|
118 |
end;
|
paulson@6051
|
119 |
|
paulson@6051
|
120 |
(*The Part(A,h) terms -- compose injections to make h*)
|
paulson@6051
|
121 |
fun mk_Part (Bound 0) = Free(X',iT) (*no mutual rec, no Part needed*)
|
wenzelm@26189
|
122 |
| mk_Part h = @{const Part} $ Free(X',iT) $ Abs(w',iT,h);
|
paulson@6051
|
123 |
|
paulson@6051
|
124 |
(*Access to balanced disjoint sums via injections*)
|
wenzelm@23419
|
125 |
val parts = map mk_Part
|
wenzelm@23419
|
126 |
(BalancedTree.accesses {left = fn t => Su.inl $ t, right = fn t => Su.inr $ t, init = Bound 0}
|
wenzelm@23419
|
127 |
(length rec_tms));
|
paulson@6051
|
128 |
|
paulson@6051
|
129 |
(*replace each set by the corresponding Part(A,h)*)
|
paulson@6051
|
130 |
val part_intrs = map (subst_free (rec_tms ~~ parts) o fp_part) intr_tms;
|
paulson@6051
|
131 |
|
wenzelm@12132
|
132 |
val fp_abs = absfree(X', iT,
|
wenzelm@12132
|
133 |
mk_Collect(z', dom_sum,
|
wenzelm@23419
|
134 |
BalancedTree.make FOLogic.mk_disj part_intrs));
|
paulson@6051
|
135 |
|
paulson@6051
|
136 |
val fp_rhs = Fp.oper $ dom_sum $ fp_abs
|
paulson@6051
|
137 |
|
wenzelm@22567
|
138 |
val dummy = List.app (fn rec_hd => (Logic.occs (rec_hd, fp_rhs) andalso
|
wenzelm@22567
|
139 |
error "Illegal occurrence of recursion operator"; ()))
|
wenzelm@12132
|
140 |
rec_hds;
|
paulson@6051
|
141 |
|
paulson@6051
|
142 |
(*** Make the new theory ***)
|
paulson@6051
|
143 |
|
paulson@6051
|
144 |
(*A key definition:
|
paulson@6051
|
145 |
If no mutual recursion then it equals the one recursive set.
|
paulson@6051
|
146 |
If mutual recursion then it differs from all the recursive sets. *)
|
paulson@6051
|
147 |
val big_rec_base_name = space_implode "_" rec_base_names;
|
wenzelm@20342
|
148 |
val big_rec_name = Sign.intern_const thy big_rec_base_name;
|
paulson@6051
|
149 |
|
wenzelm@12132
|
150 |
|
wenzelm@21962
|
151 |
val _ =
|
wenzelm@21962
|
152 |
if verbose then
|
wenzelm@21962
|
153 |
writeln ((if coind then "Coind" else "Ind") ^ "uctive definition " ^ quote big_rec_name)
|
wenzelm@21962
|
154 |
else ();
|
paulson@6051
|
155 |
|
paulson@6051
|
156 |
(*Big_rec... is the union of the mutually recursive sets*)
|
paulson@6051
|
157 |
val big_rec_tm = list_comb(Const(big_rec_name,recT), rec_params);
|
paulson@6051
|
158 |
|
paulson@6051
|
159 |
(*The individual sets must already be declared*)
|
wenzelm@24255
|
160 |
val axpairs = map PrimitiveDefs.mk_defpair
|
wenzelm@12132
|
161 |
((big_rec_tm, fp_rhs) ::
|
wenzelm@12132
|
162 |
(case parts of
|
wenzelm@12132
|
163 |
[_] => [] (*no mutual recursion*)
|
wenzelm@12132
|
164 |
| _ => rec_tms ~~ (*define the sets as Parts*)
|
wenzelm@12132
|
165 |
map (subst_atomic [(Free(X',iT),big_rec_tm)]) parts));
|
paulson@6051
|
166 |
|
paulson@6051
|
167 |
(*tracing: print the fixedpoint definition*)
|
paulson@6051
|
168 |
val dummy = if !Ind_Syntax.trace then
|
wenzelm@26189
|
169 |
writeln (cat_lines (map (Syntax.string_of_term ctxt o #2) axpairs))
|
wenzelm@12132
|
170 |
else ()
|
paulson@6051
|
171 |
|
paulson@6051
|
172 |
(*add definitions of the inductive sets*)
|
haftmann@18377
|
173 |
val (_, thy1) =
|
haftmann@18377
|
174 |
thy
|
wenzelm@24712
|
175 |
|> Sign.add_path big_rec_base_name
|
haftmann@27691
|
176 |
|> PureThy.add_defs false (map Thm.no_attributes axpairs);
|
wenzelm@26189
|
177 |
|
wenzelm@26189
|
178 |
val ctxt1 = ProofContext.init thy1;
|
paulson@6051
|
179 |
|
paulson@6051
|
180 |
|
paulson@6051
|
181 |
(*fetch fp definitions from the theory*)
|
wenzelm@12132
|
182 |
val big_rec_def::part_rec_defs =
|
wenzelm@28678
|
183 |
map (Thm.get_def thy1)
|
wenzelm@12132
|
184 |
(case rec_names of [_] => rec_names
|
wenzelm@12132
|
185 |
| _ => big_rec_base_name::rec_names);
|
paulson@6051
|
186 |
|
paulson@6051
|
187 |
|
paulson@6051
|
188 |
(********)
|
paulson@6051
|
189 |
val dummy = writeln " Proving monotonicity...";
|
paulson@6051
|
190 |
|
wenzelm@12132
|
191 |
val bnd_mono =
|
wenzelm@20342
|
192 |
Goal.prove_global thy1 [] [] (FOLogic.mk_Trueprop (Fp.bnd_mono $ dom_sum $ fp_abs))
|
wenzelm@17985
|
193 |
(fn _ => EVERY
|
wenzelm@24893
|
194 |
[rtac (@{thm Collect_subset} RS @{thm bnd_monoI}) 1,
|
wenzelm@24893
|
195 |
REPEAT (ares_tac (@{thms basic_monos} @ monos) 1)]);
|
paulson@6051
|
196 |
|
paulson@6051
|
197 |
val dom_subset = standard (big_rec_def RS Fp.subs);
|
paulson@6051
|
198 |
|
paulson@6051
|
199 |
val unfold = standard ([big_rec_def, bnd_mono] MRS Fp.Tarski);
|
paulson@6051
|
200 |
|
paulson@6051
|
201 |
(********)
|
paulson@6051
|
202 |
val dummy = writeln " Proving the introduction rules...";
|
paulson@6051
|
203 |
|
wenzelm@12132
|
204 |
(*Mutual recursion? Helps to derive subset rules for the
|
paulson@6051
|
205 |
individual sets.*)
|
paulson@6051
|
206 |
val Part_trans =
|
paulson@6051
|
207 |
case rec_names of
|
wenzelm@12132
|
208 |
[_] => asm_rl
|
wenzelm@24893
|
209 |
| _ => standard (@{thm Part_subset} RS @{thm subset_trans});
|
paulson@6051
|
210 |
|
paulson@6051
|
211 |
(*To type-check recursive occurrences of the inductive sets, possibly
|
paulson@6051
|
212 |
enclosed in some monotonic operator M.*)
|
wenzelm@12132
|
213 |
val rec_typechecks =
|
wenzelm@12132
|
214 |
[dom_subset] RL (asm_rl :: ([Part_trans] RL monos))
|
wenzelm@24893
|
215 |
RL [@{thm subsetD}];
|
paulson@6051
|
216 |
|
paulson@6051
|
217 |
(*Type-checking is hardest aspect of proof;
|
paulson@6051
|
218 |
disjIn selects the correct disjunct after unfolding*)
|
wenzelm@17985
|
219 |
fun intro_tacsf disjIn =
|
wenzelm@17985
|
220 |
[DETERM (stac unfold 1),
|
wenzelm@24893
|
221 |
REPEAT (resolve_tac [@{thm Part_eqI}, @{thm CollectI}] 1),
|
paulson@6051
|
222 |
(*Now 2-3 subgoals: typechecking, the disjunction, perhaps equality.*)
|
paulson@6051
|
223 |
rtac disjIn 2,
|
paulson@6051
|
224 |
(*Not ares_tac, since refl must be tried before equality assumptions;
|
paulson@6051
|
225 |
backtracking may occur if the premises have extra variables!*)
|
paulson@6051
|
226 |
DEPTH_SOLVE_1 (resolve_tac [refl,exI,conjI] 2 APPEND assume_tac 2),
|
paulson@6051
|
227 |
(*Now solve the equations like Tcons(a,f) = Inl(?b4)*)
|
paulson@6051
|
228 |
rewrite_goals_tac con_defs,
|
wenzelm@26189
|
229 |
REPEAT (rtac @{thm refl} 2),
|
paulson@6051
|
230 |
(*Typechecking; this can fail*)
|
paulson@6172
|
231 |
if !Ind_Syntax.trace then print_tac "The type-checking subgoal:"
|
paulson@6051
|
232 |
else all_tac,
|
paulson@6051
|
233 |
REPEAT (FIRSTGOAL ( dresolve_tac rec_typechecks
|
wenzelm@24893
|
234 |
ORELSE' eresolve_tac (asm_rl::@{thm PartE}::@{thm SigmaE2}::
|
wenzelm@12132
|
235 |
type_elims)
|
wenzelm@12132
|
236 |
ORELSE' hyp_subst_tac)),
|
paulson@6051
|
237 |
if !Ind_Syntax.trace then print_tac "The subgoal after monos, type_elims:"
|
paulson@6051
|
238 |
else all_tac,
|
wenzelm@24893
|
239 |
DEPTH_SOLVE (swap_res_tac (@{thm SigmaI}::@{thm subsetI}::type_intrs) 1)];
|
paulson@6051
|
240 |
|
paulson@6051
|
241 |
(*combines disjI1 and disjI2 to get the corresponding nested disjunct...*)
|
wenzelm@23419
|
242 |
val mk_disj_rls = BalancedTree.accesses
|
wenzelm@26189
|
243 |
{left = fn rl => rl RS @{thm disjI1},
|
wenzelm@26189
|
244 |
right = fn rl => rl RS @{thm disjI2},
|
wenzelm@26189
|
245 |
init = @{thm asm_rl}};
|
paulson@6051
|
246 |
|
wenzelm@17985
|
247 |
val intrs =
|
wenzelm@17985
|
248 |
(intr_tms, map intro_tacsf (mk_disj_rls (length intr_tms)))
|
wenzelm@17985
|
249 |
|> ListPair.map (fn (t, tacs) =>
|
wenzelm@20342
|
250 |
Goal.prove_global thy1 [] [] t
|
wenzelm@20046
|
251 |
(fn _ => EVERY (rewrite_goals_tac part_rec_defs :: tacs)))
|
wenzelm@29302
|
252 |
handle Simplifier.SIMPLIFIER (msg, thm) => (Display.print_thm thm; error msg);
|
paulson@6051
|
253 |
|
paulson@6051
|
254 |
(********)
|
paulson@6051
|
255 |
val dummy = writeln " Proving the elimination rule...";
|
paulson@6051
|
256 |
|
paulson@6051
|
257 |
(*Breaks down logical connectives in the monotonic function*)
|
paulson@6051
|
258 |
val basic_elim_tac =
|
paulson@6051
|
259 |
REPEAT (SOMEGOAL (eresolve_tac (Ind_Syntax.elim_rls @ Su.free_SEs)
|
wenzelm@12132
|
260 |
ORELSE' bound_hyp_subst_tac))
|
paulson@6051
|
261 |
THEN prune_params_tac
|
wenzelm@12132
|
262 |
(*Mutual recursion: collapse references to Part(D,h)*)
|
wenzelm@28839
|
263 |
THEN (PRIMITIVE (fold_rule part_rec_defs));
|
paulson@6051
|
264 |
|
paulson@6051
|
265 |
(*Elimination*)
|
wenzelm@12132
|
266 |
val elim = rule_by_tactic basic_elim_tac
|
wenzelm@12132
|
267 |
(unfold RS Ind_Syntax.equals_CollectD)
|
paulson@6051
|
268 |
|
paulson@6051
|
269 |
(*Applies freeness of the given constructors, which *must* be unfolded by
|
wenzelm@12132
|
270 |
the given defs. Cannot simply use the local con_defs because
|
wenzelm@12132
|
271 |
con_defs=[] for inference systems.
|
wenzelm@12175
|
272 |
Proposition A should have the form t:Si where Si is an inductive set*)
|
wenzelm@12175
|
273 |
fun make_cases ss A =
|
wenzelm@12175
|
274 |
rule_by_tactic
|
wenzelm@12175
|
275 |
(basic_elim_tac THEN ALLGOALS (asm_full_simp_tac ss) THEN basic_elim_tac)
|
wenzelm@12175
|
276 |
(Thm.assume A RS elim)
|
wenzelm@12175
|
277 |
|> Drule.standard';
|
wenzelm@12175
|
278 |
fun mk_cases a = make_cases (*delayed evaluation of body!*)
|
wenzelm@27261
|
279 |
(simpset ())
|
wenzelm@27261
|
280 |
let val thy = Thm.theory_of_thm elim in cterm_of thy (Syntax.read_prop_global thy a) end;
|
paulson@6051
|
281 |
|
paulson@6051
|
282 |
fun induction_rules raw_induct thy =
|
paulson@6051
|
283 |
let
|
paulson@6051
|
284 |
val dummy = writeln " Proving the induction rule...";
|
paulson@6051
|
285 |
|
paulson@6051
|
286 |
(*** Prove the main induction rule ***)
|
paulson@6051
|
287 |
|
paulson@6051
|
288 |
val pred_name = "P"; (*name for predicate variables*)
|
paulson@6051
|
289 |
|
paulson@6051
|
290 |
(*Used to make induction rules;
|
wenzelm@12132
|
291 |
ind_alist = [(rec_tm1,pred1),...] associates predicates with rec ops
|
wenzelm@12132
|
292 |
prem is a premise of an intr rule*)
|
wenzelm@26189
|
293 |
fun add_induct_prem ind_alist (prem as Const (@{const_name Trueprop}, _) $
|
wenzelm@26189
|
294 |
(Const (@{const_name mem}, _) $ t $ X), iprems) =
|
haftmann@17314
|
295 |
(case AList.lookup (op aconv) ind_alist X of
|
skalberg@15531
|
296 |
SOME pred => prem :: FOLogic.mk_Trueprop (pred $ t) :: iprems
|
skalberg@15531
|
297 |
| NONE => (*possibly membership in M(rec_tm), for M monotone*)
|
wenzelm@12132
|
298 |
let fun mk_sb (rec_tm,pred) =
|
wenzelm@26189
|
299 |
(rec_tm, @{const Collect} $ rec_tm $ pred)
|
wenzelm@12132
|
300 |
in subst_free (map mk_sb ind_alist) prem :: iprems end)
|
paulson@6051
|
301 |
| add_induct_prem ind_alist (prem,iprems) = prem :: iprems;
|
paulson@6051
|
302 |
|
paulson@6051
|
303 |
(*Make a premise of the induction rule.*)
|
paulson@6051
|
304 |
fun induct_prem ind_alist intr =
|
wenzelm@29265
|
305 |
let val quantfrees = map dest_Free (OldTerm.term_frees intr \\ rec_params)
|
skalberg@15574
|
306 |
val iprems = foldr (add_induct_prem ind_alist) []
|
skalberg@15574
|
307 |
(Logic.strip_imp_prems intr)
|
wenzelm@12132
|
308 |
val (t,X) = Ind_Syntax.rule_concl intr
|
haftmann@17314
|
309 |
val (SOME pred) = AList.lookup (op aconv) ind_alist X
|
wenzelm@12132
|
310 |
val concl = FOLogic.mk_Trueprop (pred $ t)
|
paulson@6051
|
311 |
in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end
|
paulson@6051
|
312 |
handle Bind => error"Recursion term not found in conclusion";
|
paulson@6051
|
313 |
|
paulson@6051
|
314 |
(*Minimizes backtracking by delivering the correct premise to each goal.
|
paulson@6051
|
315 |
Intro rules with extra Vars in premises still cause some backtracking *)
|
paulson@6051
|
316 |
fun ind_tac [] 0 = all_tac
|
wenzelm@12132
|
317 |
| ind_tac(prem::prems) i =
|
paulson@13747
|
318 |
DEPTH_SOLVE_1 (ares_tac [prem, refl] i) THEN ind_tac prems (i-1);
|
paulson@6051
|
319 |
|
paulson@6051
|
320 |
val pred = Free(pred_name, Ind_Syntax.iT --> FOLogic.oT);
|
paulson@6051
|
321 |
|
wenzelm@12132
|
322 |
val ind_prems = map (induct_prem (map (rpair pred) rec_tms))
|
wenzelm@12132
|
323 |
intr_tms;
|
paulson@6051
|
324 |
|
wenzelm@12132
|
325 |
val dummy = if !Ind_Syntax.trace then
|
wenzelm@12132
|
326 |
(writeln "ind_prems = ";
|
wenzelm@26189
|
327 |
List.app (writeln o Syntax.string_of_term ctxt1) ind_prems;
|
wenzelm@26928
|
328 |
writeln "raw_induct = "; Display.print_thm raw_induct)
|
wenzelm@12132
|
329 |
else ();
|
paulson@6051
|
330 |
|
paulson@6051
|
331 |
|
wenzelm@12132
|
332 |
(*We use a MINIMAL simpset. Even FOL_ss contains too many simpules.
|
paulson@6051
|
333 |
If the premises get simplified, then the proofs could fail.*)
|
wenzelm@17892
|
334 |
val min_ss = Simplifier.theory_context thy empty_ss
|
wenzelm@12725
|
335 |
setmksimps (map mk_eq o ZF_atomize o gen_all)
|
wenzelm@12132
|
336 |
setSolver (mk_solver "minimal"
|
wenzelm@12132
|
337 |
(fn prems => resolve_tac (triv_rls@prems)
|
wenzelm@12132
|
338 |
ORELSE' assume_tac
|
wenzelm@12132
|
339 |
ORELSE' etac FalseE));
|
paulson@6051
|
340 |
|
wenzelm@12132
|
341 |
val quant_induct =
|
wenzelm@20342
|
342 |
Goal.prove_global thy1 [] ind_prems
|
wenzelm@17985
|
343 |
(FOLogic.mk_Trueprop (Ind_Syntax.mk_all_imp (big_rec_tm, pred)))
|
wenzelm@26712
|
344 |
(fn {prems, ...} => EVERY
|
wenzelm@17985
|
345 |
[rewrite_goals_tac part_rec_defs,
|
wenzelm@26189
|
346 |
rtac (@{thm impI} RS @{thm allI}) 1,
|
wenzelm@17985
|
347 |
DETERM (etac raw_induct 1),
|
wenzelm@17985
|
348 |
(*Push Part inside Collect*)
|
wenzelm@24893
|
349 |
full_simp_tac (min_ss addsimps [@{thm Part_Collect}]) 1,
|
wenzelm@17985
|
350 |
(*This CollectE and disjE separates out the introduction rules*)
|
wenzelm@26189
|
351 |
REPEAT (FIRSTGOAL (eresolve_tac [@{thm CollectE}, @{thm disjE}])),
|
wenzelm@17985
|
352 |
(*Now break down the individual cases. No disjE here in case
|
wenzelm@17985
|
353 |
some premise involves disjunction.*)
|
wenzelm@26189
|
354 |
REPEAT (FIRSTGOAL (eresolve_tac [@{thm CollectE}, @{thm exE}, @{thm conjE}]
|
wenzelm@17985
|
355 |
ORELSE' bound_hyp_subst_tac)),
|
wenzelm@20046
|
356 |
ind_tac (rev (map (rewrite_rule part_rec_defs) prems)) (length prems)]);
|
paulson@6051
|
357 |
|
wenzelm@12132
|
358 |
val dummy = if !Ind_Syntax.trace then
|
wenzelm@26928
|
359 |
(writeln "quant_induct = "; Display.print_thm quant_induct)
|
wenzelm@12132
|
360 |
else ();
|
paulson@6051
|
361 |
|
paulson@6051
|
362 |
|
paulson@6051
|
363 |
(*** Prove the simultaneous induction rule ***)
|
paulson@6051
|
364 |
|
paulson@6051
|
365 |
(*Make distinct predicates for each inductive set*)
|
paulson@6051
|
366 |
|
paulson@6051
|
367 |
(*The components of the element type, several if it is a product*)
|
paulson@6051
|
368 |
val elem_type = CP.pseudo_type dom_sum;
|
paulson@6051
|
369 |
val elem_factors = CP.factors elem_type;
|
paulson@6051
|
370 |
val elem_frees = mk_frees "za" elem_factors;
|
paulson@6051
|
371 |
val elem_tuple = CP.mk_tuple Pr.pair elem_type elem_frees;
|
paulson@6051
|
372 |
|
paulson@6051
|
373 |
(*Given a recursive set and its domain, return the "fsplit" predicate
|
paulson@6051
|
374 |
and a conclusion for the simultaneous induction rule.
|
paulson@6051
|
375 |
NOTE. This will not work for mutually recursive predicates. Previously
|
paulson@6051
|
376 |
a summand 'domt' was also an argument, but this required the domain of
|
paulson@6051
|
377 |
mutual recursion to invariably be a disjoint sum.*)
|
wenzelm@12132
|
378 |
fun mk_predpair rec_tm =
|
paulson@6051
|
379 |
let val rec_name = (#1 o dest_Const o head_of) rec_tm
|
wenzelm@12132
|
380 |
val pfree = Free(pred_name ^ "_" ^ Sign.base_name rec_name,
|
wenzelm@12132
|
381 |
elem_factors ---> FOLogic.oT)
|
wenzelm@12132
|
382 |
val qconcl =
|
skalberg@15574
|
383 |
foldr FOLogic.mk_all
|
skalberg@15574
|
384 |
(FOLogic.imp $
|
wenzelm@26189
|
385 |
(@{const mem} $ elem_tuple $ rec_tm)
|
skalberg@15574
|
386 |
$ (list_comb (pfree, elem_frees))) elem_frees
|
wenzelm@12132
|
387 |
in (CP.ap_split elem_type FOLogic.oT pfree,
|
wenzelm@12132
|
388 |
qconcl)
|
paulson@6051
|
389 |
end;
|
paulson@6051
|
390 |
|
paulson@6051
|
391 |
val (preds,qconcls) = split_list (map mk_predpair rec_tms);
|
paulson@6051
|
392 |
|
paulson@6051
|
393 |
(*Used to form simultaneous induction lemma*)
|
wenzelm@12132
|
394 |
fun mk_rec_imp (rec_tm,pred) =
|
wenzelm@26189
|
395 |
FOLogic.imp $ (@{const mem} $ Bound 0 $ rec_tm) $
|
wenzelm@12132
|
396 |
(pred $ Bound 0);
|
paulson@6051
|
397 |
|
paulson@6051
|
398 |
(*To instantiate the main induction rule*)
|
wenzelm@12132
|
399 |
val induct_concl =
|
wenzelm@12132
|
400 |
FOLogic.mk_Trueprop
|
wenzelm@12132
|
401 |
(Ind_Syntax.mk_all_imp
|
wenzelm@12132
|
402 |
(big_rec_tm,
|
wenzelm@12132
|
403 |
Abs("z", Ind_Syntax.iT,
|
wenzelm@23419
|
404 |
BalancedTree.make FOLogic.mk_conj
|
wenzelm@12132
|
405 |
(ListPair.map mk_rec_imp (rec_tms, preds)))))
|
paulson@6051
|
406 |
and mutual_induct_concl =
|
wenzelm@23419
|
407 |
FOLogic.mk_Trueprop(BalancedTree.make FOLogic.mk_conj qconcls);
|
paulson@6051
|
408 |
|
wenzelm@12132
|
409 |
val dummy = if !Ind_Syntax.trace then
|
wenzelm@12132
|
410 |
(writeln ("induct_concl = " ^
|
wenzelm@26189
|
411 |
Syntax.string_of_term ctxt1 induct_concl);
|
wenzelm@12132
|
412 |
writeln ("mutual_induct_concl = " ^
|
wenzelm@26189
|
413 |
Syntax.string_of_term ctxt1 mutual_induct_concl))
|
wenzelm@12132
|
414 |
else ();
|
paulson@6051
|
415 |
|
paulson@6051
|
416 |
|
wenzelm@26189
|
417 |
val lemma_tac = FIRST' [eresolve_tac [@{thm asm_rl}, @{thm conjE}, @{thm PartE}, @{thm mp}],
|
wenzelm@26189
|
418 |
resolve_tac [@{thm allI}, @{thm impI}, @{thm conjI}, @{thm Part_eqI}],
|
wenzelm@26189
|
419 |
dresolve_tac [@{thm spec}, @{thm mp}, Pr.fsplitD]];
|
paulson@6051
|
420 |
|
paulson@6051
|
421 |
val need_mutual = length rec_names > 1;
|
paulson@6051
|
422 |
|
paulson@6051
|
423 |
val lemma = (*makes the link between the two induction rules*)
|
paulson@6051
|
424 |
if need_mutual then
|
wenzelm@12132
|
425 |
(writeln " Proving the mutual induction rule...";
|
wenzelm@20342
|
426 |
Goal.prove_global thy1 [] []
|
wenzelm@17985
|
427 |
(Logic.mk_implies (induct_concl, mutual_induct_concl))
|
wenzelm@17985
|
428 |
(fn _ => EVERY
|
wenzelm@17985
|
429 |
[rewrite_goals_tac part_rec_defs,
|
wenzelm@20046
|
430 |
REPEAT (rewrite_goals_tac [Pr.split_eq] THEN lemma_tac 1)]))
|
wenzelm@26189
|
431 |
else (writeln " [ No mutual induction rule needed ]"; @{thm TrueI});
|
paulson@6051
|
432 |
|
wenzelm@12132
|
433 |
val dummy = if !Ind_Syntax.trace then
|
wenzelm@26928
|
434 |
(writeln "lemma = "; Display.print_thm lemma)
|
wenzelm@12132
|
435 |
else ();
|
paulson@6051
|
436 |
|
paulson@6051
|
437 |
|
paulson@6051
|
438 |
(*Mutual induction follows by freeness of Inl/Inr.*)
|
paulson@6051
|
439 |
|
wenzelm@12132
|
440 |
(*Simplification largely reduces the mutual induction rule to the
|
paulson@6051
|
441 |
standard rule*)
|
wenzelm@12132
|
442 |
val mut_ss =
|
wenzelm@12132
|
443 |
min_ss addsimps [Su.distinct, Su.distinct', Su.inl_iff, Su.inr_iff];
|
paulson@6051
|
444 |
|
paulson@6051
|
445 |
val all_defs = con_defs @ part_rec_defs;
|
paulson@6051
|
446 |
|
paulson@6051
|
447 |
(*Removes Collects caused by M-operators in the intro rules. It is very
|
paulson@6051
|
448 |
hard to simplify
|
wenzelm@12132
|
449 |
list({v: tf. (v : t --> P_t(v)) & (v : f --> P_f(v))})
|
paulson@6051
|
450 |
where t==Part(tf,Inl) and f==Part(tf,Inr) to list({v: tf. P_t(v)}).
|
paulson@6051
|
451 |
Instead the following rules extract the relevant conjunct.
|
paulson@6051
|
452 |
*)
|
wenzelm@24893
|
453 |
val cmonos = [@{thm subset_refl} RS @{thm Collect_mono}] RL monos
|
wenzelm@24893
|
454 |
RLN (2,[@{thm rev_subsetD}]);
|
paulson@6051
|
455 |
|
paulson@6051
|
456 |
(*Minimizes backtracking by delivering the correct premise to each goal*)
|
paulson@6051
|
457 |
fun mutual_ind_tac [] 0 = all_tac
|
wenzelm@12132
|
458 |
| mutual_ind_tac(prem::prems) i =
|
wenzelm@12132
|
459 |
DETERM
|
wenzelm@12132
|
460 |
(SELECT_GOAL
|
wenzelm@12132
|
461 |
(
|
wenzelm@12132
|
462 |
(*Simplify the assumptions and goal by unfolding Part and
|
wenzelm@12132
|
463 |
using freeness of the Sum constructors; proves all but one
|
wenzelm@12132
|
464 |
conjunct by contradiction*)
|
wenzelm@12132
|
465 |
rewrite_goals_tac all_defs THEN
|
wenzelm@24893
|
466 |
simp_tac (mut_ss addsimps [@{thm Part_iff}]) 1 THEN
|
wenzelm@12132
|
467 |
IF_UNSOLVED (*simp_tac may have finished it off!*)
|
wenzelm@12132
|
468 |
((*simplify assumptions*)
|
wenzelm@12132
|
469 |
(*some risk of excessive simplification here -- might have
|
wenzelm@12132
|
470 |
to identify the bare minimum set of rewrites*)
|
wenzelm@12132
|
471 |
full_simp_tac
|
wenzelm@26287
|
472 |
(mut_ss addsimps @{thms conj_simps} @ @{thms imp_simps} @ @{thms quant_simps}) 1
|
wenzelm@12132
|
473 |
THEN
|
wenzelm@12132
|
474 |
(*unpackage and use "prem" in the corresponding place*)
|
wenzelm@12132
|
475 |
REPEAT (rtac impI 1) THEN
|
wenzelm@12132
|
476 |
rtac (rewrite_rule all_defs prem) 1 THEN
|
wenzelm@12132
|
477 |
(*prem must not be REPEATed below: could loop!*)
|
wenzelm@12132
|
478 |
DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE'
|
wenzelm@12132
|
479 |
eresolve_tac (conjE::mp::cmonos))))
|
wenzelm@12132
|
480 |
) i)
|
wenzelm@12132
|
481 |
THEN mutual_ind_tac prems (i-1);
|
paulson@6051
|
482 |
|
wenzelm@12132
|
483 |
val mutual_induct_fsplit =
|
paulson@6051
|
484 |
if need_mutual then
|
wenzelm@20342
|
485 |
Goal.prove_global thy1 [] (map (induct_prem (rec_tms~~preds)) intr_tms)
|
wenzelm@17985
|
486 |
mutual_induct_concl
|
wenzelm@26712
|
487 |
(fn {prems, ...} => EVERY
|
wenzelm@17985
|
488 |
[rtac (quant_induct RS lemma) 1,
|
wenzelm@20046
|
489 |
mutual_ind_tac (rev prems) (length prems)])
|
paulson@6051
|
490 |
else TrueI;
|
paulson@6051
|
491 |
|
paulson@6051
|
492 |
(** Uncurrying the predicate in the ordinary induction rule **)
|
paulson@6051
|
493 |
|
paulson@6051
|
494 |
(*instantiate the variable to a tuple, if it is non-trivial, in order to
|
paulson@6051
|
495 |
allow the predicate to be "opened up".
|
paulson@6051
|
496 |
The name "x.1" comes from the "RS spec" !*)
|
wenzelm@12132
|
497 |
val inst =
|
wenzelm@12132
|
498 |
case elem_frees of [_] => I
|
wenzelm@20342
|
499 |
| _ => instantiate ([], [(cterm_of thy1 (Var(("x",1), Ind_Syntax.iT)),
|
wenzelm@20342
|
500 |
cterm_of thy1 elem_tuple)]);
|
paulson@6051
|
501 |
|
paulson@6051
|
502 |
(*strip quantifier and the implication*)
|
wenzelm@26189
|
503 |
val induct0 = inst (quant_induct RS spec RSN (2, @{thm rev_mp}));
|
paulson@6051
|
504 |
|
wenzelm@26189
|
505 |
val Const (@{const_name Trueprop}, _) $ (pred_var $ _) = concl_of induct0
|
paulson@6051
|
506 |
|
wenzelm@12132
|
507 |
val induct = CP.split_rule_var(pred_var, elem_type-->FOLogic.oT, induct0)
|
wenzelm@12132
|
508 |
|> standard
|
paulson@6051
|
509 |
and mutual_induct = CP.remove_split mutual_induct_fsplit
|
wenzelm@8438
|
510 |
|
haftmann@18377
|
511 |
val ([induct', mutual_induct'], thy') =
|
haftmann@18377
|
512 |
thy
|
wenzelm@18643
|
513 |
|> PureThy.add_thms [((co_prefix ^ "induct", induct),
|
wenzelm@24861
|
514 |
[case_names, Induct.induct_pred big_rec_name]),
|
wenzelm@18643
|
515 |
(("mutual_induct", mutual_induct), [case_names])];
|
wenzelm@12227
|
516 |
in ((thy', induct'), mutual_induct')
|
paulson@6051
|
517 |
end; (*of induction_rules*)
|
paulson@6051
|
518 |
|
paulson@6051
|
519 |
val raw_induct = standard ([big_rec_def, bnd_mono] MRS Fp.induct)
|
paulson@6051
|
520 |
|
wenzelm@12227
|
521 |
val ((thy2, induct), mutual_induct) =
|
wenzelm@12227
|
522 |
if not coind then induction_rules raw_induct thy1
|
haftmann@18377
|
523 |
else
|
haftmann@18377
|
524 |
(thy1
|
haftmann@18377
|
525 |
|> PureThy.add_thms [((co_prefix ^ "induct", raw_induct), [])]
|
haftmann@18377
|
526 |
|> apfst hd |> Library.swap, TrueI)
|
paulson@6051
|
527 |
and defs = big_rec_def :: part_rec_defs
|
paulson@6051
|
528 |
|
paulson@6051
|
529 |
|
haftmann@18377
|
530 |
val (([bnd_mono', dom_subset', elim'], [defs', intrs']), thy3) =
|
wenzelm@8438
|
531 |
thy2
|
wenzelm@12183
|
532 |
|> IndCases.declare big_rec_name make_cases
|
wenzelm@12132
|
533 |
|> PureThy.add_thms
|
wenzelm@12132
|
534 |
[(("bnd_mono", bnd_mono), []),
|
wenzelm@12132
|
535 |
(("dom_subset", dom_subset), []),
|
wenzelm@24861
|
536 |
(("cases", elim), [case_names, Induct.cases_pred big_rec_name])]
|
haftmann@18377
|
537 |
||>> (PureThy.add_thmss o map Thm.no_attributes)
|
wenzelm@8438
|
538 |
[("defs", defs),
|
wenzelm@12175
|
539 |
("intros", intrs)];
|
haftmann@18377
|
540 |
val (intrs'', thy4) =
|
haftmann@18377
|
541 |
thy3
|
haftmann@18377
|
542 |
|> PureThy.add_thms ((intr_names ~~ intrs') ~~ map #2 intr_specs)
|
wenzelm@24712
|
543 |
||> Sign.parent_path;
|
wenzelm@8438
|
544 |
in
|
wenzelm@12132
|
545 |
(thy4,
|
wenzelm@8438
|
546 |
{defs = defs',
|
wenzelm@8438
|
547 |
bnd_mono = bnd_mono',
|
wenzelm@8438
|
548 |
dom_subset = dom_subset',
|
wenzelm@12132
|
549 |
intrs = intrs'',
|
wenzelm@8438
|
550 |
elim = elim',
|
wenzelm@8438
|
551 |
mk_cases = mk_cases,
|
wenzelm@8438
|
552 |
induct = induct,
|
wenzelm@8438
|
553 |
mutual_induct = mutual_induct})
|
wenzelm@8438
|
554 |
end;
|
paulson@6051
|
555 |
|
wenzelm@12132
|
556 |
(*source version*)
|
wenzelm@12132
|
557 |
fun add_inductive (srec_tms, sdom_sum) intr_srcs
|
wenzelm@12132
|
558 |
(raw_monos, raw_con_defs, raw_type_intrs, raw_type_elims) thy =
|
wenzelm@12132
|
559 |
let
|
wenzelm@24726
|
560 |
val ctxt = ProofContext.init thy;
|
wenzelm@24726
|
561 |
val read_terms = map (Syntax.parse_term ctxt #> TypeInfer.constrain Ind_Syntax.iT)
|
wenzelm@24726
|
562 |
#> Syntax.check_terms ctxt;
|
wenzelm@24726
|
563 |
|
wenzelm@18728
|
564 |
val intr_atts = map (map (Attrib.attribute thy) o snd) intr_srcs;
|
wenzelm@17937
|
565 |
val sintrs = map fst intr_srcs ~~ intr_atts;
|
wenzelm@24726
|
566 |
val rec_tms = read_terms srec_tms;
|
wenzelm@24726
|
567 |
val dom_sum = singleton read_terms sdom_sum;
|
wenzelm@24726
|
568 |
val intr_tms = Syntax.read_props ctxt (map (snd o fst) sintrs);
|
wenzelm@17937
|
569 |
val intr_specs = (map (fst o fst) sintrs ~~ intr_tms) ~~ map snd sintrs;
|
wenzelm@24726
|
570 |
val monos = Attrib.eval_thms ctxt raw_monos;
|
wenzelm@24726
|
571 |
val con_defs = Attrib.eval_thms ctxt raw_con_defs;
|
wenzelm@24726
|
572 |
val type_intrs = Attrib.eval_thms ctxt raw_type_intrs;
|
wenzelm@24726
|
573 |
val type_elims = Attrib.eval_thms ctxt raw_type_elims;
|
wenzelm@12132
|
574 |
in
|
haftmann@18418
|
575 |
thy
|
wenzelm@24726
|
576 |
|> add_inductive_i true (rec_tms, dom_sum) intr_specs (monos, con_defs, type_intrs, type_elims)
|
haftmann@18418
|
577 |
end;
|
wenzelm@12132
|
578 |
|
wenzelm@12132
|
579 |
|
wenzelm@12132
|
580 |
(* outer syntax *)
|
wenzelm@12132
|
581 |
|
wenzelm@17057
|
582 |
local structure P = OuterParse and K = OuterKeyword in
|
wenzelm@12132
|
583 |
|
wenzelm@27354
|
584 |
val _ = List.app OuterKeyword.keyword
|
wenzelm@24867
|
585 |
["domains", "intros", "monos", "con_defs", "type_intros", "type_elims"];
|
wenzelm@24867
|
586 |
|
wenzelm@12132
|
587 |
fun mk_ind (((((doms, intrs), monos), con_defs), type_intrs), type_elims) =
|
wenzelm@12132
|
588 |
#1 o add_inductive doms (map P.triple_swap intrs) (monos, con_defs, type_intrs, type_elims);
|
wenzelm@12132
|
589 |
|
wenzelm@12132
|
590 |
val ind_decl =
|
wenzelm@12132
|
591 |
(P.$$$ "domains" |-- P.!!! (P.enum1 "+" P.term --
|
wenzelm@25985
|
592 |
((P.$$$ "\<subseteq>" || P.$$$ "<=") |-- P.term))) --
|
wenzelm@12132
|
593 |
(P.$$$ "intros" |--
|
wenzelm@22101
|
594 |
P.!!! (Scan.repeat1 (SpecParse.opt_thm_name ":" -- P.prop))) --
|
wenzelm@22101
|
595 |
Scan.optional (P.$$$ "monos" |-- P.!!! SpecParse.xthms1) [] --
|
wenzelm@22101
|
596 |
Scan.optional (P.$$$ "con_defs" |-- P.!!! SpecParse.xthms1) [] --
|
wenzelm@22101
|
597 |
Scan.optional (P.$$$ "type_intros" |-- P.!!! SpecParse.xthms1) [] --
|
wenzelm@22101
|
598 |
Scan.optional (P.$$$ "type_elims" |-- P.!!! SpecParse.xthms1) []
|
wenzelm@12132
|
599 |
>> (Toplevel.theory o mk_ind);
|
wenzelm@12132
|
600 |
|
wenzelm@24867
|
601 |
val _ = OuterSyntax.command (co_prefix ^ "inductive")
|
wenzelm@12227
|
602 |
("define " ^ co_prefix ^ "inductive sets") K.thy_decl ind_decl;
|
wenzelm@12132
|
603 |
|
paulson@6051
|
604 |
end;
|
wenzelm@12132
|
605 |
|
wenzelm@12132
|
606 |
end;
|
wenzelm@15705
|
607 |
|