author  blanchet 
Sat, 08 Sep 2012 21:04:26 +0200  
changeset 49203  262ab1ac38b9 
parent 49202  f493cd25737f 
child 49204  0b735fb2602e 
permissions  rwrr 
49112  1 
(* Title: HOL/Codatatype/Tools/bnf_fp_sugar.ML 
2 
Author: Jasmin Blanchette, TU Muenchen 

3 
Copyright 2012 

4 

5 
Sugar for constructing LFPs and GFPs. 

6 
*) 

7 

8 
signature BNF_FP_SUGAR = 

9 
sig 

10 
end; 

11 

12 
structure BNF_FP_Sugar : BNF_FP_SUGAR = 

13 
struct 

14 

49119  15 
open BNF_Util 
16 
open BNF_Wrap 

17 
open BNF_FP_Util 

18 
open BNF_LFP 

19 
open BNF_GFP 

49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset

20 
open BNF_FP_Sugar_Tactics 
49119  21 

49129  22 
val caseN = "case"; 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

23 
val itersN = "iters"; 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

24 
val recsN = "recs"; 
49129  25 

49199  26 
fun retype_free (Free (s, _)) T = Free (s, T); 
27 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

28 
fun flat_list_comb (f, xss) = fold (fn xs => fn t => Term.list_comb (t, xs)) xss f 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

29 

49200  30 
fun mk_tupled_fun x f xs = HOLogic.tupled_lambda x (Term.list_comb (f, xs)); 
31 
fun mk_uncurried_fun f xs = mk_tupled_fun (HOLogic.mk_tuple xs) f xs; 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

32 
fun mk_uncurried2_fun f xss = 
49200  33 
mk_tupled_fun (HOLogic.mk_tuple (map HOLogic.mk_tuple xss)) f (flat xss); 
34 

49124  35 
fun cannot_merge_types () = error "Mutually recursive types must have the same type parameters"; 
49119  36 

37 
fun merge_type_arg_constrained ctxt (T, c) (T', c') = 

38 
if T = T' then 

39 
(case (c, c') of 

40 
(_, NONE) => (T, c) 

41 
 (NONE, _) => (T, c') 

42 
 _ => 

43 
if c = c' then 

44 
(T, c) 

45 
else 

46 
error ("Inconsistent sort constraints for type variable " ^ 

47 
quote (Syntax.string_of_typ ctxt T))) 

48 
else 

49 
cannot_merge_types (); 

50 

51 
fun merge_type_args_constrained ctxt (cAs, cAs') = 

52 
if length cAs = length cAs' then map2 (merge_type_arg_constrained ctxt) cAs cAs' 

53 
else cannot_merge_types (); 

54 

49121  55 
fun type_args_constrained_of (((cAs, _), _), _) = cAs; 
56 
val type_args_of = map fst o type_args_constrained_of; 

49129  57 
fun type_binder_of (((_, b), _), _) = b; 
49181  58 
fun mixfix_of ((_, mx), _) = mx; 
49121  59 
fun ctr_specs_of (_, ctr_specs) = ctr_specs; 
49119  60 

49121  61 
fun disc_of (((disc, _), _), _) = disc; 
62 
fun ctr_of (((_, ctr), _), _) = ctr; 

63 
fun args_of ((_, args), _) = args; 

49181  64 
fun ctr_mixfix_of (_, mx) = mx; 
49119  65 

49199  66 
fun prepare_datatype prepare_typ gfp specs fake_lthy lthy = 
49112  67 
let 
49121  68 
val constrained_As = 
69 
map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs 

49119  70 
> Library.foldr1 (merge_type_args_constrained lthy); 
49121  71 
val As = map fst constrained_As; 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

72 
val As' = map dest_TFree As; 
49119  73 

49121  74 
val _ = (case duplicates (op =) As of [] => () 
49165  75 
 A :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy A))); 
49119  76 

77 
(* TODO: use sort constraints on type args *) 

78 

79 
val N = length specs; 

80 

49182
b8517107ffc5
read the real types off the constant types, rather than using the fake parser types (second step of sugar localization)
blanchet
parents:
49181
diff
changeset

81 
fun mk_fake_T b = 
49121  82 
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), 
83 
As); 

84 

49129  85 
val bs = map type_binder_of specs; 
49182
b8517107ffc5
read the real types off the constant types, rather than using the fake parser types (second step of sugar localization)
blanchet
parents:
49181
diff
changeset

86 
val fake_Ts = map mk_fake_T bs; 
49121  87 

49181  88 
val mixfixes = map mixfix_of specs; 
49119  89 

90 
val _ = (case duplicates Binding.eq_name bs of [] => () 

91 
 b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b))); 

92 

49121  93 
val ctr_specss = map ctr_specs_of specs; 
49119  94 

49129  95 
val disc_binderss = map (map disc_of) ctr_specss; 
96 
val ctr_binderss = map (map ctr_of) ctr_specss; 

49121  97 
val ctr_argsss = map (map args_of) ctr_specss; 
49181  98 
val ctr_mixfixess = map (map ctr_mixfix_of) ctr_specss; 
49119  99 

49129  100 
val sel_bindersss = map (map (map fst)) ctr_argsss; 
49183
0cc46e2dee7e
careful about constructor types w.r.t. fake context (third step of localization)
blanchet
parents:
49182
diff
changeset

101 
val fake_ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; 
49121  102 

49183
0cc46e2dee7e
careful about constructor types w.r.t. fake context (third step of localization)
blanchet
parents:
49182
diff
changeset

103 
val rhs_As' = fold (fold (fold Term.add_tfreesT)) fake_ctr_Tsss []; 
49167  104 
val _ = (case subtract (op =) As' rhs_As' of 
49165  105 
[] => () 
106 
 A' :: _ => error ("Extra type variables on rhs: " ^ 

107 
quote (Syntax.string_of_typ lthy (TFree A')))); 

108 

49176  109 
val ((Cs, Xs), _) = 
49121  110 
lthy 
111 
> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs 

112 
> mk_TFrees N 

49176  113 
>> mk_TFrees N; 
49119  114 

49201  115 
fun is_same_fpT (T as Type (s, Us)) (Type (s', Us')) = 
49146  116 
s = s' andalso (Us = Us' orelse error ("Illegal occurrence of recursive type " ^ 
117 
quote (Syntax.string_of_typ fake_lthy T))) 

49201  118 
 is_same_fpT _ _ = false; 
49146  119 

49201  120 
fun freeze_fpXs (T as Type (s, Us)) = 
121 
(case find_index (is_same_fpT T) fake_Ts of 

122 
~1 => Type (s, map freeze_fpXs Us) 

49176  123 
 i => nth Xs i) 
49201  124 
 freeze_fpXs T = T; 
49121  125 

49201  126 
val ctr_TsssXs = map (map (map freeze_fpXs)) fake_ctr_Tsss; 
49176  127 
val sum_prod_TsXs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssXs; 
49119  128 

49176  129 
val eqs = map dest_TFree Xs ~~ sum_prod_TsXs; 
49121  130 

49203  131 
val ((unfs0, flds0, fp_iters0, fp_recs0, unf_flds, fld_unfs, fld_injects), lthy') = 
49169  132 
fp_bnf (if gfp then bnf_gfp else bnf_lfp) bs mixfixes As' eqs lthy; 
49121  133 

49167  134 
val timer = time (Timer.startRealTimer ()); 
135 

49176  136 
fun mk_unf_or_fld get_T Ts t = 
137 
let val Type (_, Ts0) = get_T (fastype_of t) in 

49124  138 
Term.subst_atomic_types (Ts0 ~~ Ts) t 
49121  139 
end; 
49119  140 

49126  141 
val mk_unf = mk_unf_or_fld domain_type; 
142 
val mk_fld = mk_unf_or_fld range_type; 

49121  143 

49203  144 
val unfs = map (mk_unf As) unfs0; 
145 
val flds = map (mk_fld As) flds0; 

49124  146 

49201  147 
val fpTs = map (domain_type o fastype_of) unfs; 
148 
val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctr_TsssXs; 

49182
b8517107ffc5
read the real types off the constant types, rather than using the fake parser types (second step of sugar localization)
blanchet
parents:
49181
diff
changeset

149 

49203  150 
val ns = map length ctr_Tsss; 
151 
val mss = map (map length) ctr_Tsss; 

152 
val Css = map2 replicate ns Cs; 

153 
val Cs' = flat Css; 

154 

155 
fun mk_iter_or_rec Ts Us c = 

49121  156 
let 
49201  157 
val (binders, body) = strip_type (fastype_of c); 
49203  158 
val (fst_binders, last_binder) = split_last binders; 
159 
val Type (_, Ts0) = if gfp then body else last_binder; 

160 
val Us0 = map (if gfp then domain_type else body_type) fst_binders; 

49176  161 
in 
49201  162 
Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) c 
49176  163 
end; 
164 

49203  165 
val fp_iters = map (mk_iter_or_rec As Cs) fp_iters0; 
166 
val fp_recs = map (mk_iter_or_rec As Cs) fp_recs0; 

49176  167 

49201  168 
fun pour_sugar_on_type ((((((((((((((b, fpT), C), fld), unf), fp_iter), fp_rec), fld_unf), 
49176  169 
unf_fld), fld_inject), ctr_binders), ctr_mixfixes), ctr_Tss), disc_binders), sel_binderss) 
170 
no_defs_lthy = 

171 
let 

172 
val n = length ctr_Tss; 

49121  173 
val ks = 1 upto n; 
174 
val ms = map length ctr_Tss; 

175 

49201  176 
val unfT = domain_type (fastype_of fld); 
49121  177 
val prod_Ts = map HOLogic.mk_tupleT ctr_Tss; 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

178 
val case_Ts = map (fn Ts => Ts > C) ctr_Tss; 
49119  179 

49176  180 
val ((((u, v), fs), xss), _) = 
49124  181 
lthy 
49201  182 
> yield_singleton (mk_Frees "u") unfT 
183 
>> yield_singleton (mk_Frees "v") fpT 

49176  184 
>> mk_Frees "f" case_Ts 
49124  185 
>> mk_Freess "x" ctr_Tss; 
49121  186 

49129  187 
val ctr_rhss = 
49121  188 
map2 (fn k => fn xs => 
189 
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; 

190 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

191 
val case_binder = Binding.suffix_name ("_" ^ caseN) b; 
49129  192 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

193 
val case_rhs = 
49176  194 
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (map2 mk_uncurried_fun fs xss) $ (unf $ v)); 
49129  195 

49201  196 
val ((raw_case :: raw_ctrs, raw_case_def :: raw_ctr_defs), (lthy', lthy)) = no_defs_lthy 
49169  197 
> apfst split_list o fold_map3 (fn b => fn mx => fn rhs => 
198 
Local_Theory.define ((b, mx), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) 

49201  199 
(case_binder :: ctr_binders) (NoSyn :: ctr_mixfixes) (case_rhs :: ctr_rhss) 
49121  200 
> `Local_Theory.restore; 
201 

202 
(*transforms defined frees into consts (and more)*) 

203 
val phi = Proof_Context.export_morphism lthy lthy'; 

204 

205 
val ctr_defs = map (Morphism.thm phi) raw_ctr_defs; 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

206 
val case_def = Morphism.thm phi raw_case_def; 
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

207 

49203  208 
val ctrs0 = map (Morphism.term phi) raw_ctrs; 
209 
val casex0 = Morphism.term phi raw_case; 

210 

211 
val ctrs = map (mk_ctr As) ctrs0; 

49121  212 

49135  213 
fun exhaust_tac {context = ctxt, ...} = 
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset

214 
let 
49135  215 
val fld_iff_unf_thm = 
216 
let 

217 
val goal = 

218 
fold_rev Logic.all [u, v] 

219 
(mk_Trueprop_eq (HOLogic.mk_eq (v, fld $ u), HOLogic.mk_eq (unf $ v, u))); 

220 
in 

221 
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => 

49201  222 
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unfT, fpT]) 
49176  223 
(certify lthy fld) (certify lthy unf) fld_unf unf_fld) 
49135  224 
> Thm.close_derivation 
225 
> Morphism.thm phi 

226 
end; 

227 

228 
val sumEN_thm' = 

229 
Local_Defs.unfold lthy @{thms all_unit_eq} 

230 
(Drule.instantiate' (map (SOME o certifyT lthy) prod_Ts) [] (mk_sumEN n)) 

231 
> Morphism.thm phi; 

49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset

232 
in 
49161
a8e74375d971
fixed (n + 1)st bug in "mk_exhaust_tac"  arose with uncurried constructors
blanchet
parents:
49157
diff
changeset

233 
mk_exhaust_tac ctxt n ctr_defs fld_iff_unf_thm sumEN_thm' 
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset

234 
end; 
49121  235 

49126  236 
val inject_tacss = 
237 
map2 (fn 0 => K [] 

238 
 _ => fn ctr_def => [fn {context = ctxt, ...} => 

239 
mk_inject_tac ctxt ctr_def fld_inject]) 

240 
ms ctr_defs; 

241 

49127  242 
val half_distinct_tacss = 
243 
map (map (fn (def, def') => fn {context = ctxt, ...} => 

244 
mk_half_distinct_tac ctxt fld_inject [def, def'])) (mk_half_pairss ctr_defs); 

245 

49130
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

246 
val case_tacs = 
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

247 
map3 (fn k => fn m => fn ctr_def => fn {context = ctxt, ...} => 
3c26e17b2849
implemented "mk_case_tac"  and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset

248 
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs; 
49121  249 

250 
val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs]; 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

251 

49176  252 
(* (co)iterators, (co)recursors, (co)induction *) 
253 

49201  254 
val is_fpT = member (op =) fpTs; 
49176  255 

49200  256 
fun dest_rec_pair (T as Type (@{type_name prod}, Us as [_, U])) = 
257 
if member (op =) Cs U then Us else [T] 

258 
 dest_rec_pair T = [T]; 

259 

49199  260 
fun sugar_datatype no_defs_lthy = 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

261 
let 
49176  262 
val fp_y_Ts = map domain_type (fst (split_last (binder_types (fastype_of fp_iter)))); 
263 
val y_prod_Tss = map2 dest_sumTN ns fp_y_Ts; 

264 
val y_Tsss = map2 (map2 dest_tupleT) mss y_prod_Tss; 

265 
val g_Tss = map2 (map2 (curry (op >))) y_Tsss Css; 

49201  266 
val iter_T = flat g_Tss > fpT > C; 
49146  267 

49199  268 
val fp_z_Ts = map domain_type (fst (split_last (binder_types (fastype_of fp_rec)))); 
269 
val z_prod_Tss = map2 dest_sumTN ns fp_z_Ts; 

270 
val z_Tsss = map2 (map2 dest_tupleT) mss z_prod_Tss; 

49200  271 
val z_Tssss = map (map (map dest_rec_pair)) z_Tsss; 
272 
val h_Tss = map2 (map2 (fold_rev (curry (op >)))) z_Tssss Css; 

49201  273 
val rec_T = flat h_Tss > fpT > C; 
49199  274 

49176  275 
val ((gss, ysss), _) = 
49199  276 
no_defs_lthy 
49176  277 
> mk_Freess "f" g_Tss 
49177
db8ce685073f
introduced and used "mk_Freesss", and simplified "mk_Freess(')"
blanchet
parents:
49176
diff
changeset

278 
>> mk_Freesss "x" y_Tsss; 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

279 

49199  280 
val hss = map2 (map2 retype_free) gss h_Tss; 
49200  281 
val (zssss, _) = 
49199  282 
no_defs_lthy 
49200  283 
> mk_Freessss "x" z_Tssss; 
49199  284 

285 
val iter_binder = Binding.suffix_name ("_" ^ iterN) b; 

286 
val rec_binder = Binding.suffix_name ("_" ^ recN) b; 

287 

288 
val iter_free = Free (Binding.name_of iter_binder, iter_T); 

289 
val rec_free = Free (Binding.name_of rec_binder, rec_T); 

290 

291 
val iter_spec = 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

292 
mk_Trueprop_eq (flat_list_comb (iter_free, gss), 
49199  293 
Term.list_comb (fp_iter, map2 (mk_sum_caseN oo map2 mk_uncurried_fun) gss ysss)); 
294 
val rec_spec = 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

295 
mk_Trueprop_eq (flat_list_comb (rec_free, hss), 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

296 
Term.list_comb (fp_rec, map2 (mk_sum_caseN oo map2 mk_uncurried2_fun) hss zssss)); 
49199  297 

298 
val (([raw_iter, raw_rec], [raw_iter_def, raw_rec_def]), (lthy', lthy)) = no_defs_lthy 

49201  299 
> apfst split_list o fold_map2 (fn b => fn spec => 
49199  300 
Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec)) 
49201  301 
#>> apsnd snd) [iter_binder, rec_binder] [iter_spec, rec_spec] 
49199  302 
> `Local_Theory.restore; 
49201  303 

304 
(*transforms defined frees into consts (and more)*) 

305 
val phi = Proof_Context.export_morphism lthy lthy'; 

306 

307 
val iter_def = Morphism.thm phi raw_iter_def; 

308 
val rec_def = Morphism.thm phi raw_rec_def; 

309 

49203  310 
val iter0 = Morphism.term phi raw_iter; 
311 
val rec0 = Morphism.term phi raw_rec; 

312 

313 
val iter = mk_iter_or_rec As Cs' iter0; 

314 
val recx = mk_iter_or_rec As Cs' rec0; 

49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

315 
in 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

316 
([[ctrs], [[iter]], [[recx]], xss, gss, hss], lthy) 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

317 
end; 
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

318 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

319 
fun sugar_codatatype no_defs_lthy = ([], no_defs_lthy); 
49119  320 
in 
49203  321 
wrap_datatype tacss ((ctrs0, casex0), (disc_binders, sel_binderss)) lthy' 
49199  322 
> (if gfp then sugar_codatatype else sugar_datatype) 
49119  323 
end; 
49167  324 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

325 
fun pour_more_sugar_on_datatypes ([[ctrss], [[iters]], [[recs]], xsss, gsss, hsss], lthy) = 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

326 
let 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

327 
val xctrss = map2 (map2 (curry Term.list_comb)) ctrss xsss; 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

328 
val giters = map2 (curry flat_list_comb) iters gsss; 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

329 
val hrecs = map2 (curry flat_list_comb) recs hsss; 
49201  330 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

331 
val (iter_thmss, rec_thmss) = 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

332 
let 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

333 
fun mk_goal_iter_or_rec fc xctr = 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

334 
mk_Trueprop_eq (fc $ xctr, fc $ xctr); 
49201  335 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

336 
val goal_iterss = map2 (fn giter => map (mk_goal_iter_or_rec giter)) giters xctrss; 
49203  337 
val goal_recss = map2 (fn hrec => map (mk_goal_iter_or_rec hrec)) hrecs xctrss; 
338 
val iter_tacss = 

339 
map (map (K (fn _ => Skip_Proof.cheat_tac (Proof_Context.theory_of lthy)))) goal_iterss; 

340 
(* ### map (map mk_iter_or_rec_tac); (* needs ctr_def, iter_def, fld_iter *) *) 

341 
val rec_tacss = 

342 
map (map (K (fn _ => Skip_Proof.cheat_tac (Proof_Context.theory_of lthy)))) goal_recss; 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

343 
in 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

344 
(map2 (map2 (Skip_Proof.prove lthy [] [])) goal_iterss iter_tacss, 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

345 
map2 (map2 (Skip_Proof.prove lthy [] [])) goal_recss rec_tacss) 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

346 
end; 
49201  347 

49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

348 
val notes = 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

349 
[(itersN, iter_thmss), 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

350 
(recsN, rec_thmss)] 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

351 
> maps (fn (thmN, thmss) => 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

352 
map2 (fn b => fn thms => 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

353 
((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])])) 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

354 
bs thmss); 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

355 
in 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

356 
lthy > Local_Theory.notes notes > snd 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

357 
end; 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

358 

f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

359 
val lthy'' = lthy' 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

360 
> fold_map pour_sugar_on_type (bs ~~ fpTs ~~ Cs ~~ flds ~~ unfs ~~ fp_iters ~~ fp_recs ~~ 
49176  361 
fld_unfs ~~ unf_flds ~~ fld_injects ~~ ctr_binderss ~~ ctr_mixfixess ~~ ctr_Tsss ~~ 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

362 
disc_binderss ~~ sel_bindersss) 
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

363 
> (if gfp then snd else pour_more_sugar_on_datatypes o apfst transpose); 
49167  364 

365 
val timer = time (timer ("Constructors, discriminators, selectors, etc., for the new " ^ 

366 
(if gfp then "co" else "") ^ "datatype")); 

49112  367 
in 
49167  368 
(timer; lthy'') 
49112  369 
end; 
370 

49199  371 
fun datatype_cmd info specs lthy = 
49121  372 
let 
49184
83fdea0c4779
gracefully handle shadowing case (fourth step of sugar localization)
blanchet
parents:
49183
diff
changeset

373 
(*the "perhaps o try" below helps gracefully handles the case where the new type is defined in a 
83fdea0c4779
gracefully handle shadowing case (fourth step of sugar localization)
blanchet
parents:
49183
diff
changeset

374 
locale and shadows an existing global type*) 
49179
f9d48d479c84
don't throw away the context when hacking the theory (first step to localize the sugar code)
blanchet
parents:
49177
diff
changeset

375 
val fake_thy = Theory.copy 
49184
83fdea0c4779
gracefully handle shadowing case (fourth step of sugar localization)
blanchet
parents:
49183
diff
changeset

376 
#> fold (fn spec => perhaps (try (Sign.add_type lthy 
83fdea0c4779
gracefully handle shadowing case (fourth step of sugar localization)
blanchet
parents:
49183
diff
changeset

377 
(type_binder_of spec, length (type_args_constrained_of spec), mixfix_of spec)))) specs; 
49179
f9d48d479c84
don't throw away the context when hacking the theory (first step to localize the sugar code)
blanchet
parents:
49177
diff
changeset

378 
val fake_lthy = Proof_Context.background_theory fake_thy lthy; 
49121  379 
in 
49199  380 
prepare_datatype Syntax.read_typ info specs fake_lthy lthy 
49121  381 
end; 
49119  382 

49129  383 
val parse_opt_binding_colon = Scan.optional (Parse.binding  Parse.$$$ ":") no_binder 
49119  384 

49112  385 
val parse_ctr_arg = 
49119  386 
Parse.$$$ "("  parse_opt_binding_colon  Parse.typ  Parse.$$$ ")"  
49129  387 
(Parse.typ >> pair no_binder); 
49112  388 

389 
val parse_single_spec = 

390 
Parse.type_args_constrained  Parse.binding  Parse.opt_mixfix  

49119  391 
(@{keyword "="}  Parse.enum1 "" (parse_opt_binding_colon  Parse.binding  
392 
Scan.repeat parse_ctr_arg  Parse.opt_mixfix)); 

49112  393 

394 
val _ = 

395 
Outer_Syntax.local_theory @{command_spec "data"} "define BNFbased inductive datatypes" 

49199  396 
(Parse.and_list1 parse_single_spec >> datatype_cmd false); 
49112  397 

398 
val _ = 

399 
Outer_Syntax.local_theory @{command_spec "codata"} "define BNFbased coinductive datatypes" 

49199  400 
(Parse.and_list1 parse_single_spec >> datatype_cmd true); 
49112  401 

402 
end; 