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