author  wenzelm 
Fri, 09 Nov 2001 22:53:41 +0100  
changeset 12132  1ef58b332ca9 
parent 11680  b5b96188e94c 
child 12175  5cf58a1799a7 
permissions  rwrr 
6051  1 
(* Title: ZF/inductive_package.ML 
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 nonstandard 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 newstyle 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 newstyle theories;
wenzelm
parents:
11680
diff
changeset

31 
val add_inductive_i: bool > term list * term > 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

32 
((bstring * term) * theory attribute list) list > 
1ef58b332ca9
support co/inductive definitions in newstyle 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 newstyle theories;
wenzelm
parents:
11680
diff
changeset

34 
val add_inductive_x: string list * string > ((bstring * string) * theory attribute list) list 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

35 
> thm list * thm list * thm list * thm list > theory > theory * inductive_result 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

36 
val add_inductive: string list * string > 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

37 
((bstring * string) * Args.src list) list > 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

38 
(xstring * Args.src list) list * (xstring * Args.src list) list * 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

39 
(xstring * Args.src list) list * (xstring * Args.src list) list > 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

40 
theory > theory * inductive_result 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

41 
end; 
6051  42 

43 

44 
(*Declares functions to add fixedpoint/constructor defs to a theory. 

45 
Recursive sets must *already* be declared as constants.*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

46 
functor Add_inductive_def_Fun 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

47 
(structure Fp: FP and Pr : PR and CP: CARTPROD and Su : SU val coind: bool) 
6051  48 
: INDUCTIVE_PACKAGE = 
49 
struct 

50 
open Logic Ind_Syntax; 

51 

7695  52 

53 
(* utils *) 

54 

55 
(*make distinct individual variables a1, a2, a3, ..., an. *) 

56 
fun mk_frees a [] = [] 

57 
 mk_frees a (T::Ts) = Free(a,T) :: mk_frees (bump_string a) Ts; 

58 

59 
(*read an assumption in the given theory*) 

60 
fun assume_read thy a = Thm.assume (read_cterm (Theory.sign_of thy) (a,propT)); 

61 

62 

63 
(* add_inductive(_i) *) 

64 

6051  65 
(*internal version, accepting terms*) 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

66 
fun add_inductive_i verbose (rec_tms, dom_sum) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

67 
intr_specs (monos, con_defs, type_intrs, type_elims) thy = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

68 
let 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

69 
val _ = Theory.requires thy "Inductive" "(co)inductive definitions"; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

70 
val sign = sign_of thy; 
6051  71 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

72 
val intr_tms = map (#2 o #1) intr_specs; 
6051  73 

74 
(*recT and rec_params should agree for all mutually recursive components*) 

75 
val rec_hds = map head_of rec_tms; 

76 

77 
val dummy = assert_all is_Const rec_hds 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

78 
(fn t => "Recursive set not previously declared as constant: " ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

79 
Sign.string_of_term sign t); 
6051  80 

81 
(*Now we know they are all Consts, so get their names, type and params*) 

82 
val rec_names = map (#1 o dest_Const) rec_hds 

83 
and (Const(_,recT),rec_params) = strip_comb (hd rec_tms); 

84 

85 
val rec_base_names = map Sign.base_name rec_names; 

86 
val dummy = assert_all Syntax.is_identifier rec_base_names 

87 
(fn a => "Base name of recursive set not an identifier: " ^ a); 

88 

89 
local (*Checking the introduction rules*) 

90 
val intr_sets = map (#2 o rule_concl_msg sign) intr_tms; 

91 
fun intr_ok set = 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

92 
case head_of set of Const(a,recT) => a mem rec_names  _ => false; 
6051  93 
in 
94 
val dummy = assert_all intr_ok intr_sets 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

95 
(fn t => "Conclusion of rule does not name a recursive set: " ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

96 
Sign.string_of_term sign t); 
6051  97 
end; 
98 

99 
val dummy = assert_all is_Free rec_params 

100 
(fn t => "Param in recursion term not a free variable: " ^ 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

101 
Sign.string_of_term sign t); 
6051  102 

103 
(*** Construct the fixedpoint definition ***) 

104 
val mk_variant = variant (foldr add_term_names (intr_tms,[])); 

105 

106 
val z' = mk_variant"z" and X' = mk_variant"X" and w' = mk_variant"w"; 

107 

108 
fun dest_tprop (Const("Trueprop",_) $ P) = P 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

109 
 dest_tprop Q = error ("Illformed premise of introduction rule: " ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

110 
Sign.string_of_term sign Q); 
6051  111 

112 
(*Makes a disjunct from an introduction rule*) 

113 
fun fp_part intr = (*quantify over rule's free vars except parameters*) 

114 
let val prems = map dest_tprop (strip_imp_prems intr) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

115 
val dummy = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

116 
val exfrees = term_frees intr \\ rec_params 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

117 
val zeq = FOLogic.mk_eq (Free(z',iT), #1 (rule_concl intr)) 
6051  118 
in foldr FOLogic.mk_exists 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

119 
(exfrees, fold_bal FOLogic.mk_conj (zeq::prems)) 
6051  120 
end; 
121 

122 
(*The Part(A,h) terms  compose injections to make h*) 

123 
fun mk_Part (Bound 0) = Free(X',iT) (*no mutual rec, no Part needed*) 

124 
 mk_Part h = Part_const $ Free(X',iT) $ Abs(w',iT,h); 

125 

126 
(*Access to balanced disjoint sums via injections*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

127 
val parts = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

128 
map mk_Part (accesses_bal (fn t => Su.inl $ t, fn t => Su.inr $ t, Bound 0) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

129 
(length rec_tms)); 
6051  130 

131 
(*replace each set by the corresponding Part(A,h)*) 

132 
val part_intrs = map (subst_free (rec_tms ~~ parts) o fp_part) intr_tms; 

133 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

134 
val fp_abs = absfree(X', iT, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

135 
mk_Collect(z', dom_sum, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

136 
fold_bal FOLogic.mk_disj part_intrs)); 
6051  137 

138 
val fp_rhs = Fp.oper $ dom_sum $ fp_abs 

139 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

140 
val dummy = seq (fn rec_hd => deny (rec_hd occs fp_rhs) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

141 
"Illegal occurrence of recursion operator") 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

142 
rec_hds; 
6051  143 

144 
(*** Make the new theory ***) 

145 

146 
(*A key definition: 

147 
If no mutual recursion then it equals the one recursive set. 

148 
If mutual recursion then it differs from all the recursive sets. *) 

149 
val big_rec_base_name = space_implode "_" rec_base_names; 

150 
val big_rec_name = Sign.intern_const sign big_rec_base_name; 

151 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

152 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

153 
val dummy = conditional verbose (fn () => 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

154 
writeln ((if coind then "Coind" else "Ind") ^ "uctive definition " ^ big_rec_name)); 
6051  155 

156 
(*Forbid the inductive definition structure from clashing with a theory 

157 
name. This restriction may become obsolete as ML is deemphasized.*) 

158 
val dummy = deny (big_rec_base_name mem (Sign.stamp_names_of sign)) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

159 
("Definition " ^ big_rec_base_name ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
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 newstyle theories;
wenzelm
parents:
11680
diff
changeset

166 
val axpairs = map Logic.mk_defpair 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

167 
((big_rec_tm, fp_rhs) :: 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

168 
(case parts of 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

169 
[_] => [] (*no mutual recursion*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

170 
 _ => rec_tms ~~ (*define the sets as Parts*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
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 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

175 
seq (writeln o Sign.string_of_term sign o #2) axpairs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

176 
else () 
6051  177 

178 
(*add definitions of the inductive sets*) 

179 
val thy1 = thy > Theory.add_path big_rec_base_name 

9329  180 
> (#1 o PureThy.add_defs_i false (map Thm.no_attributes axpairs)) 
6051  181 

182 

183 
(*fetch fp definitions from the theory*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

184 
val big_rec_def::part_rec_defs = 
6051  185 
map (get_def thy1) 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

186 
(case rec_names of [_] => rec_names 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

187 
 _ => big_rec_base_name::rec_names); 
6051  188 

189 

190 
val sign1 = sign_of thy1; 

191 

192 
(********) 

193 
val dummy = writeln " Proving monotonicity..."; 

194 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

195 
val bnd_mono = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

196 
prove_goalw_cterm [] 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

197 
(cterm_of sign1 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

198 
(FOLogic.mk_Trueprop (Fp.bnd_mono $ dom_sum $ fp_abs))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

199 
(fn _ => 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

200 
[rtac (Collect_subset RS bnd_monoI) 1, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

201 
REPEAT (ares_tac (basic_monos @ monos) 1)]); 
6051  202 

203 
val dom_subset = standard (big_rec_def RS Fp.subs); 

204 

205 
val unfold = standard ([big_rec_def, bnd_mono] MRS Fp.Tarski); 

206 

207 
(********) 

208 
val dummy = writeln " Proving the introduction rules..."; 

209 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

210 
(*Mutual recursion? Helps to derive subset rules for the 
6051  211 
individual sets.*) 
212 
val Part_trans = 

213 
case rec_names of 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

214 
[_] => asm_rl 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

215 
 _ => standard (Part_subset RS subset_trans); 
6051  216 

217 
(*To typecheck recursive occurrences of the inductive sets, possibly 

218 
enclosed in some monotonic operator M.*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

219 
val rec_typechecks = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

220 
[dom_subset] RL (asm_rl :: ([Part_trans] RL monos)) 
6051  221 
RL [subsetD]; 
222 

223 
(*Typechecking is hardest aspect of proof; 

224 
disjIn selects the correct disjunct after unfolding*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

225 
fun intro_tacsf disjIn prems = 
6051  226 
[(*insert prems and underlying sets*) 
227 
cut_facts_tac prems 1, 

228 
DETERM (stac unfold 1), 

229 
REPEAT (resolve_tac [Part_eqI,CollectI] 1), 

230 
(*Now 23 subgoals: typechecking, the disjunction, perhaps equality.*) 

231 
rtac disjIn 2, 

232 
(*Not ares_tac, since refl must be tried before equality assumptions; 

233 
backtracking may occur if the premises have extra variables!*) 

234 
DEPTH_SOLVE_1 (resolve_tac [refl,exI,conjI] 2 APPEND assume_tac 2), 

235 
(*Now solve the equations like Tcons(a,f) = Inl(?b4)*) 

236 
rewrite_goals_tac con_defs, 

237 
REPEAT (rtac refl 2), 

238 
(*Typechecking; this can fail*) 

6172  239 
if !Ind_Syntax.trace then print_tac "The typechecking subgoal:" 
6051  240 
else all_tac, 
241 
REPEAT (FIRSTGOAL ( dresolve_tac rec_typechecks 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

242 
ORELSE' eresolve_tac (asm_rl::PartE::SigmaE2:: 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

243 
type_elims) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

244 
ORELSE' hyp_subst_tac)), 
6051  245 
if !Ind_Syntax.trace then print_tac "The subgoal after monos, type_elims:" 
246 
else all_tac, 

247 
DEPTH_SOLVE (swap_res_tac (SigmaI::subsetI::type_intrs) 1)]; 

248 

249 
(*combines disjI1 and disjI2 to get the corresponding nested disjunct...*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

250 
val mk_disj_rls = 
6051  251 
let fun f rl = rl RS disjI1 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

252 
and g rl = rl RS disjI2 
6051  253 
in accesses_bal(f, g, asm_rl) end; 
254 

255 
fun prove_intr (ct, tacsf) = prove_goalw_cterm part_rec_defs ct tacsf; 

256 

257 
val intrs = ListPair.map prove_intr 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

258 
(map (cterm_of sign1) intr_tms, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

259 
map intro_tacsf (mk_disj_rls(length intr_tms))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

260 
handle MetaSimplifier.SIMPLIFIER (msg,thm) => (print_thm thm; error msg); 
6051  261 

262 
(********) 

263 
val dummy = writeln " Proving the elimination rule..."; 

264 

265 
(*Breaks down logical connectives in the monotonic function*) 

266 
val basic_elim_tac = 

267 
REPEAT (SOMEGOAL (eresolve_tac (Ind_Syntax.elim_rls @ Su.free_SEs) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

268 
ORELSE' bound_hyp_subst_tac)) 
6051  269 
THEN prune_params_tac 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

270 
(*Mutual recursion: collapse references to Part(D,h)*) 
6051  271 
THEN fold_tac part_rec_defs; 
272 

273 
(*Elimination*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

274 
val elim = rule_by_tactic basic_elim_tac 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

275 
(unfold RS Ind_Syntax.equals_CollectD) 
6051  276 

277 
(*Applies freeness of the given constructors, which *must* be unfolded by 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

278 
the given defs. Cannot simply use the local con_defs because 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

279 
con_defs=[] for inference systems. 
6051  280 
String s should have the form t:Si where Si is an inductive set*) 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

281 
fun mk_cases s = 
6141  282 
rule_by_tactic (basic_elim_tac THEN 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

283 
ALLGOALS Asm_full_simp_tac THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

284 
basic_elim_tac) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

285 
(assume_read (theory_of_thm elim) s 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

286 
(*Don't use thy1: it will be stale*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

287 
RS elim) 
6051  288 
> standard; 
289 

290 

291 
fun induction_rules raw_induct thy = 

292 
let 

293 
val dummy = writeln " Proving the induction rule..."; 

294 

295 
(*** Prove the main induction rule ***) 

296 

297 
val pred_name = "P"; (*name for predicate variables*) 

298 

299 
(*Used to make induction rules; 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

300 
ind_alist = [(rec_tm1,pred1),...] associates predicates with rec ops 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

301 
prem is a premise of an intr rule*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

302 
fun add_induct_prem ind_alist (prem as Const("Trueprop",_) $ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

303 
(Const("op :",_)$t$X), iprems) = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

304 
(case gen_assoc (op aconv) (ind_alist, X) of 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

305 
Some pred => prem :: FOLogic.mk_Trueprop (pred $ t) :: iprems 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

306 
 None => (*possibly membership in M(rec_tm), for M monotone*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

307 
let fun mk_sb (rec_tm,pred) = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

308 
(rec_tm, Ind_Syntax.Collect_const$rec_tm$pred) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

309 
in subst_free (map mk_sb ind_alist) prem :: iprems end) 
6051  310 
 add_induct_prem ind_alist (prem,iprems) = prem :: iprems; 
311 

312 
(*Make a premise of the induction rule.*) 

313 
fun induct_prem ind_alist intr = 

314 
let val quantfrees = map dest_Free (term_frees intr \\ rec_params) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

315 
val iprems = foldr (add_induct_prem ind_alist) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

316 
(Logic.strip_imp_prems intr,[]) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

317 
val (t,X) = Ind_Syntax.rule_concl intr 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

318 
val (Some pred) = gen_assoc (op aconv) (ind_alist, X) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

319 
val concl = FOLogic.mk_Trueprop (pred $ t) 
6051  320 
in list_all_free (quantfrees, Logic.list_implies (iprems,concl)) end 
321 
handle Bind => error"Recursion term not found in conclusion"; 

322 

323 
(*Minimizes backtracking by delivering the correct premise to each goal. 

324 
Intro rules with extra Vars in premises still cause some backtracking *) 

325 
fun ind_tac [] 0 = all_tac 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

326 
 ind_tac(prem::prems) i = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

327 
DEPTH_SOLVE_1 (ares_tac [prem, refl] i) THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

328 
ind_tac prems (i1); 
6051  329 

330 
val pred = Free(pred_name, Ind_Syntax.iT > FOLogic.oT); 

331 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

332 
val ind_prems = map (induct_prem (map (rpair pred) rec_tms)) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

333 
intr_tms; 
6051  334 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

335 
val dummy = if !Ind_Syntax.trace then 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

336 
(writeln "ind_prems = "; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

337 
seq (writeln o Sign.string_of_term sign1) ind_prems; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

338 
writeln "raw_induct = "; print_thm raw_induct) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

339 
else (); 
6051  340 

341 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

342 
(*We use a MINIMAL simpset. Even FOL_ss contains too many simpules. 
6051  343 
If the premises get simplified, then the proofs could fail.*) 
344 
val min_ss = empty_ss 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

345 
setmksimps (map mk_eq o ZF_atomize o gen_all) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

346 
setSolver (mk_solver "minimal" 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

347 
(fn prems => resolve_tac (triv_rls@prems) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

348 
ORELSE' assume_tac 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

349 
ORELSE' etac FalseE)); 
6051  350 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

351 
val quant_induct = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

352 
prove_goalw_cterm part_rec_defs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

353 
(cterm_of sign1 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

354 
(Logic.list_implies 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

355 
(ind_prems, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

356 
FOLogic.mk_Trueprop (Ind_Syntax.mk_all_imp(big_rec_tm,pred))))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

357 
(fn prems => 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

358 
[rtac (impI RS allI) 1, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

359 
DETERM (etac raw_induct 1), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

360 
(*Push Part inside Collect*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

361 
full_simp_tac (min_ss addsimps [Part_Collect]) 1, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

362 
(*This CollectE and disjE separates out the introduction rules*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

363 
REPEAT (FIRSTGOAL (eresolve_tac [CollectE, disjE])), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

364 
(*Now break down the individual cases. No disjE here in case 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

365 
some premise involves disjunction.*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

366 
REPEAT (FIRSTGOAL (eresolve_tac [CollectE, exE, conjE] 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

367 
ORELSE' hyp_subst_tac)), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

368 
ind_tac (rev prems) (length prems) ]); 
6051  369 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

370 
val dummy = if !Ind_Syntax.trace then 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

371 
(writeln "quant_induct = "; print_thm quant_induct) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

372 
else (); 
6051  373 

374 

375 
(*** Prove the simultaneous induction rule ***) 

376 

377 
(*Make distinct predicates for each inductive set*) 

378 

379 
(*The components of the element type, several if it is a product*) 

380 
val elem_type = CP.pseudo_type dom_sum; 

381 
val elem_factors = CP.factors elem_type; 

382 
val elem_frees = mk_frees "za" elem_factors; 

383 
val elem_tuple = CP.mk_tuple Pr.pair elem_type elem_frees; 

384 

385 
(*Given a recursive set and its domain, return the "fsplit" predicate 

386 
and a conclusion for the simultaneous induction rule. 

387 
NOTE. This will not work for mutually recursive predicates. Previously 

388 
a summand 'domt' was also an argument, but this required the domain of 

389 
mutual recursion to invariably be a disjoint sum.*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

390 
fun mk_predpair rec_tm = 
6051  391 
let val rec_name = (#1 o dest_Const o head_of) rec_tm 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

392 
val pfree = Free(pred_name ^ "_" ^ Sign.base_name rec_name, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

393 
elem_factors > FOLogic.oT) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

394 
val qconcl = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

395 
foldr FOLogic.mk_all 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

396 
(elem_frees, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

397 
FOLogic.imp $ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

398 
(Ind_Syntax.mem_const $ elem_tuple $ rec_tm) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

399 
$ (list_comb (pfree, elem_frees))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

400 
in (CP.ap_split elem_type FOLogic.oT pfree, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

401 
qconcl) 
6051  402 
end; 
403 

404 
val (preds,qconcls) = split_list (map mk_predpair rec_tms); 

405 

406 
(*Used to form simultaneous induction lemma*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

407 
fun mk_rec_imp (rec_tm,pred) = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

408 
FOLogic.imp $ (Ind_Syntax.mem_const $ Bound 0 $ rec_tm) $ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

409 
(pred $ Bound 0); 
6051  410 

411 
(*To instantiate the main induction rule*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

412 
val induct_concl = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

413 
FOLogic.mk_Trueprop 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

414 
(Ind_Syntax.mk_all_imp 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

415 
(big_rec_tm, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

416 
Abs("z", Ind_Syntax.iT, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

417 
fold_bal FOLogic.mk_conj 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

418 
(ListPair.map mk_rec_imp (rec_tms, preds))))) 
6051  419 
and mutual_induct_concl = 
7695  420 
FOLogic.mk_Trueprop(fold_bal FOLogic.mk_conj qconcls); 
6051  421 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

422 
val dummy = if !Ind_Syntax.trace then 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

423 
(writeln ("induct_concl = " ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

424 
Sign.string_of_term sign1 induct_concl); 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

425 
writeln ("mutual_induct_concl = " ^ 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

426 
Sign.string_of_term sign1 mutual_induct_concl)) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

427 
else (); 
6051  428 

429 

430 
val lemma_tac = FIRST' [eresolve_tac [asm_rl, conjE, PartE, mp], 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

431 
resolve_tac [allI, impI, conjI, Part_eqI], 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

432 
dresolve_tac [spec, mp, Pr.fsplitD]]; 
6051  433 

434 
val need_mutual = length rec_names > 1; 

435 

436 
val lemma = (*makes the link between the two induction rules*) 

437 
if need_mutual then 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

438 
(writeln " Proving the mutual induction rule..."; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

439 
prove_goalw_cterm part_rec_defs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

440 
(cterm_of sign1 (Logic.mk_implies (induct_concl, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

441 
mutual_induct_concl))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

442 
(fn prems => 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

443 
[cut_facts_tac prems 1, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

444 
REPEAT (rewrite_goals_tac [Pr.split_eq] THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

445 
lemma_tac 1)])) 
6051  446 
else (writeln " [ No mutual induction rule needed ]"; 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

447 
TrueI); 
6051  448 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

449 
val dummy = if !Ind_Syntax.trace then 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

450 
(writeln "lemma = "; print_thm lemma) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

451 
else (); 
6051  452 

453 

454 
(*Mutual induction follows by freeness of Inl/Inr.*) 

455 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

456 
(*Simplification largely reduces the mutual induction rule to the 
6051  457 
standard rule*) 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

458 
val mut_ss = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

459 
min_ss addsimps [Su.distinct, Su.distinct', Su.inl_iff, Su.inr_iff]; 
6051  460 

461 
val all_defs = con_defs @ part_rec_defs; 

462 

463 
(*Removes Collects caused by Moperators in the intro rules. It is very 

464 
hard to simplify 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

465 
list({v: tf. (v : t > P_t(v)) & (v : f > P_f(v))}) 
6051  466 
where t==Part(tf,Inl) and f==Part(tf,Inr) to list({v: tf. P_t(v)}). 
467 
Instead the following rules extract the relevant conjunct. 

468 
*) 

469 
val cmonos = [subset_refl RS Collect_mono] RL monos 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

470 
RLN (2,[rev_subsetD]); 
6051  471 

472 
(*Minimizes backtracking by delivering the correct premise to each goal*) 

473 
fun mutual_ind_tac [] 0 = all_tac 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

474 
 mutual_ind_tac(prem::prems) i = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

475 
DETERM 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

476 
(SELECT_GOAL 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

477 
( 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

478 
(*Simplify the assumptions and goal by unfolding Part and 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

479 
using freeness of the Sum constructors; proves all but one 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

480 
conjunct by contradiction*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

481 
rewrite_goals_tac all_defs THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

482 
simp_tac (mut_ss addsimps [Part_iff]) 1 THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

483 
IF_UNSOLVED (*simp_tac may have finished it off!*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

484 
((*simplify assumptions*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

485 
(*some risk of excessive simplification here  might have 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

486 
to identify the bare minimum set of rewrites*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

487 
full_simp_tac 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

488 
(mut_ss addsimps conj_simps @ imp_simps @ quant_simps) 1 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

489 
THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

490 
(*unpackage and use "prem" in the corresponding place*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

491 
REPEAT (rtac impI 1) THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

492 
rtac (rewrite_rule all_defs prem) 1 THEN 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

493 
(*prem must not be REPEATed below: could loop!*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

494 
DEPTH_SOLVE (FIRSTGOAL (ares_tac [impI] ORELSE' 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

495 
eresolve_tac (conjE::mp::cmonos)))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

496 
) i) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

497 
THEN mutual_ind_tac prems (i1); 
6051  498 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

499 
val mutual_induct_fsplit = 
6051  500 
if need_mutual then 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

501 
prove_goalw_cterm [] 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

502 
(cterm_of sign1 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

503 
(Logic.list_implies 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

504 
(map (induct_prem (rec_tms~~preds)) intr_tms, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

505 
mutual_induct_concl))) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

506 
(fn prems => 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

507 
[rtac (quant_induct RS lemma) 1, 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

508 
mutual_ind_tac (rev prems) (length prems)]) 
6051  509 
else TrueI; 
510 

511 
(** Uncurrying the predicate in the ordinary induction rule **) 

512 

513 
(*instantiate the variable to a tuple, if it is nontrivial, in order to 

514 
allow the predicate to be "opened up". 

515 
The name "x.1" comes from the "RS spec" !*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

516 
val inst = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

517 
case elem_frees of [_] => I 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

518 
 _ => instantiate ([], [(cterm_of sign1 (Var(("x",1), Ind_Syntax.iT)), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

519 
cterm_of sign1 elem_tuple)]); 
6051  520 

521 
(*strip quantifier and the implication*) 

522 
val induct0 = inst (quant_induct RS spec RSN (2,rev_mp)); 

523 

524 
val Const ("Trueprop", _) $ (pred_var $ _) = concl_of induct0 

525 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

526 
val induct = CP.split_rule_var(pred_var, elem_type>FOLogic.oT, induct0) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

527 
> standard 
6051  528 
and mutual_induct = CP.remove_split mutual_induct_fsplit 
8438  529 

530 
val (thy', [induct', mutual_induct']) = 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

531 
thy > PureThy.add_thms [(("induct", induct), [InductAttrib.induct_set_global ""]), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

532 
(("mutual_induct", mutual_induct), [])]; 
8438  533 
in (thy', induct', mutual_induct') 
6051  534 
end; (*of induction_rules*) 
535 

536 
val raw_induct = standard ([big_rec_def, bnd_mono] MRS Fp.induct) 

537 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

538 
val (thy2, induct, mutual_induct) = 
6093  539 
if #1 (dest_Const Fp.oper) = "Fixedpt.lfp" then induction_rules raw_induct thy1 
6051  540 
else (thy1, raw_induct, TrueI) 
541 
and defs = big_rec_def :: part_rec_defs 

542 

543 

8438  544 
val (thy3, ([bnd_mono', dom_subset', elim'], [defs', intrs'])) = 
545 
thy2 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

546 
> PureThy.add_thms 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

547 
[(("bnd_mono", bnd_mono), []), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

548 
(("dom_subset", dom_subset), []), 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

549 
(("cases", elim), [InductAttrib.cases_set_global ""])] 
8438  550 
>>> (PureThy.add_thmss o map Thm.no_attributes) 
551 
[("defs", defs), 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

552 
("intros", intrs)] 
8438  553 
>> Theory.parent_path; 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

554 
val (thy4, intrs'') = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

555 
thy3 > PureThy.add_thms 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

556 
(map2 (fn (((bname, _), atts), th) => ((bname, th), atts)) (intr_specs, intrs')); 
8438  557 
in 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

558 
(thy4, 
8438  559 
{defs = defs', 
560 
bnd_mono = bnd_mono', 

561 
dom_subset = dom_subset', 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

562 
intrs = intrs'', 
8438  563 
elim = elim', 
564 
mk_cases = mk_cases, 

565 
induct = induct, 

566 
mutual_induct = mutual_induct}) 

567 
end; 

6051  568 

569 

570 
(*external version, accepting strings*) 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

571 
fun add_inductive_x (srec_tms, sdom_sum) sintrs (monos, con_defs, type_intrs, type_elims) thy = 
8819  572 
let 
573 
val read = Sign.simple_read_term (Theory.sign_of thy); 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

574 
val rec_tms = map (read Ind_Syntax.iT) srec_tms; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

575 
val dom_sum = read Ind_Syntax.iT sdom_sum; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

576 
val intr_tms = map (read propT o snd o fst) sintrs; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

577 
val intr_specs = (map (fst o fst) sintrs ~~ intr_tms) ~~ map snd sintrs; 
8819  578 
in 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

579 
add_inductive_i true (rec_tms, dom_sum) intr_specs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

580 
(monos, con_defs, type_intrs, type_elims) thy 
8819  581 
end 
6051  582 

12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

583 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

584 
(*source version*) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

585 
fun add_inductive (srec_tms, sdom_sum) intr_srcs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

586 
(raw_monos, raw_con_defs, raw_type_intrs, raw_type_elims) thy = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

587 
let 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

588 
val intr_atts = map (map (Attrib.global_attribute thy) o snd) intr_srcs; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

589 
val (thy', (((monos, con_defs), type_intrs), type_elims)) = thy 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

590 
> IsarThy.apply_theorems raw_monos 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

591 
>>> IsarThy.apply_theorems raw_con_defs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

592 
>>> IsarThy.apply_theorems raw_type_intrs 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

593 
>>> IsarThy.apply_theorems raw_type_elims; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

594 
in 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

595 
add_inductive_x (srec_tms, sdom_sum) (map fst intr_srcs ~~ intr_atts) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

596 
(monos, con_defs, type_intrs, type_elims) thy' 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

597 
end; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

598 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

599 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

600 
(* outer syntax *) 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

601 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

602 
local structure P = OuterParse and K = OuterSyntax.Keyword in 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

603 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

604 
fun mk_ind (((((doms, intrs), monos), con_defs), type_intrs), type_elims) = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

605 
#1 o add_inductive doms (map P.triple_swap intrs) (monos, con_defs, type_intrs, type_elims); 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

606 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

607 
val ind_decl = 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

608 
(P.$$$ "domains"  P.!!! (P.enum1 "+" P.term  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

609 
((P.$$$ "\\<subseteq>"  P.$$$ "<=")  P.term))  P.marg_comment)  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

610 
(P.$$$ "intros"  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

611 
P.!!! (Scan.repeat1 (P.opt_thm_name ":"  P.prop  P.marg_comment)))  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

612 
Scan.optional (P.$$$ "monos"  P.!!! P.xthms1  P.marg_comment) []  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

613 
Scan.optional (P.$$$ "con_defs"  P.!!! P.xthms1  P.marg_comment) []  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

614 
Scan.optional (P.$$$ "type_intros"  P.!!! P.xthms1  P.marg_comment) []  
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

615 
Scan.optional (P.$$$ "type_elims"  P.!!! P.xthms1  P.marg_comment) [] 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

616 
>> (Toplevel.theory o mk_ind); 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

617 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

618 
val coind_prefix = if coind then "co" else ""; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

619 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

620 
val inductiveP = OuterSyntax.command (coind_prefix ^ "inductive") 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

621 
("define " ^ coind_prefix ^ "inductive sets") K.thy_decl ind_decl; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

622 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

623 
val _ = OuterSyntax.add_keywords 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

624 
["domains", "intros", "monos", "con_defs", "type_intros", "type_elims"]; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

625 
val _ = OuterSyntax.add_parsers [inductiveP]; 
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

626 

6051  627 
end; 
12132
1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

628 

1ef58b332ca9
support co/inductive definitions in newstyle theories;
wenzelm
parents:
11680
diff
changeset

629 
end; 