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