author  blanchet 
Sat, 08 Sep 2012 21:04:26 +0200  
changeset 49212  ca59649170b0 
parent 49211  239a4fa29ddf 
child 49213  975ccb0130cb 
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 

49205  10 
(* TODO: programmatic interface *) 
49112  11 
end; 
12 

13 
structure BNF_FP_Sugar : BNF_FP_SUGAR = 

14 
struct 

15 

49119  16 
open BNF_Util 
17 
open BNF_Wrap 

18 
open BNF_FP_Util 

19 
open BNF_LFP 

20 
open BNF_GFP 

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

21 
open BNF_FP_Sugar_Tactics 
49119  22 

49129  23 
val caseN = "case"; 
49212  24 
val coitersN = "iters"; 
25 
val corecsN = "recs"; 

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

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

27 
val recsN = "recs"; 
49129  28 

49205  29 
fun split_list7 xs = (map #1 xs, map #2 xs, map #3 xs, map #4 xs, map #5 xs, map #6 xs, map #7 xs); 
30 

49199  31 
fun retype_free (Free (s, _)) T = Free (s, T); 
32 

49212  33 
val lists_bmoc = fold (fn xs => fn t => Term.list_comb (t, xs)) 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

34 

49200  35 
fun mk_tupled_fun x f xs = HOLogic.tupled_lambda x (Term.list_comb (f, xs)); 
36 
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

37 
fun mk_uncurried2_fun f xss = 
49200  38 
mk_tupled_fun (HOLogic.mk_tuple (map HOLogic.mk_tuple xss)) f (flat xss); 
39 

49212  40 
fun popescu_zip [] [fs] = fs 
41 
 popescu_zip (p :: ps) (fs :: fss) = p :: fs @ popescu_zip ps fss; 

42 

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

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

46 
if T = T' then 

47 
(case (c, c') of 

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

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

50 
 _ => 

51 
if c = c' then 

52 
(T, c) 

53 
else 

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

55 
quote (Syntax.string_of_typ ctxt T))) 

56 
else 

57 
cannot_merge_types (); 

58 

59 
fun merge_type_args_constrained ctxt (cAs, cAs') = 

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

61 
else cannot_merge_types (); 

62 

49121  63 
fun type_args_constrained_of (((cAs, _), _), _) = cAs; 
64 
val type_args_of = map fst o type_args_constrained_of; 

49129  65 
fun type_binder_of (((_, b), _), _) = b; 
49181  66 
fun mixfix_of ((_, mx), _) = mx; 
49121  67 
fun ctr_specs_of (_, ctr_specs) = ctr_specs; 
49119  68 

49121  69 
fun disc_of (((disc, _), _), _) = disc; 
70 
fun ctr_of (((_, ctr), _), _) = ctr; 

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

49181  72 
fun ctr_mixfix_of (_, mx) = mx; 
49119  73 

49208  74 
fun prepare_datatype prepare_typ lfp specs fake_lthy no_defs_lthy = 
49112  75 
let 
49121  76 
val constrained_As = 
77 
map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs 

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

80 
val As' = map dest_TFree As; 
49119  81 

49121  82 
val _ = (case duplicates (op =) As of [] => () 
49204  83 
 A :: _ => error ("Duplicate type parameter " ^ 
84 
quote (Syntax.string_of_typ no_defs_lthy A))); 

49119  85 

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

87 

88 
val N = length specs; 

89 

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

90 
fun mk_fake_T b = 
49121  91 
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), 
92 
As); 

93 

49129  94 
val bs = map type_binder_of specs; 
49204  95 
val fakeTs = map mk_fake_T bs; 
49121  96 

49181  97 
val mixfixes = map mixfix_of specs; 
49119  98 

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

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

101 

49121  102 
val ctr_specss = map ctr_specs_of specs; 
49119  103 

49129  104 
val disc_binderss = map (map disc_of) ctr_specss; 
105 
val ctr_binderss = map (map ctr_of) ctr_specss; 

49121  106 
val ctr_argsss = map (map args_of) ctr_specss; 
49181  107 
val ctr_mixfixess = map (map ctr_mixfix_of) ctr_specss; 
49119  108 

49129  109 
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

110 
val fake_ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; 
49121  111 

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

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

49204  116 
quote (Syntax.string_of_typ no_defs_lthy (TFree A')))); 
49165  117 

49176  118 
val ((Cs, Xs), _) = 
49204  119 
no_defs_lthy 
49121  120 
> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs 
121 
> mk_TFrees N 

49176  122 
>> mk_TFrees N; 
49119  123 

49204  124 
fun eq_fpT (T as Type (s, Us)) (Type (s', Us')) = 
49146  125 
s = s' andalso (Us = Us' orelse error ("Illegal occurrence of recursive type " ^ 
126 
quote (Syntax.string_of_typ fake_lthy T))) 

49204  127 
 eq_fpT _ _ = false; 
49146  128 

49204  129 
fun freeze_fp (T as Type (s, Us)) = 
130 
(case find_index (eq_fpT T) fakeTs of ~1 => Type (s, map freeze_fp Us)  j => nth Xs j) 

131 
 freeze_fp T = T; 

49121  132 

49204  133 
val ctr_TsssXs = map (map (map freeze_fp)) fake_ctr_Tsss; 
49176  134 
val sum_prod_TsXs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssXs; 
49119  135 

49176  136 
val eqs = map dest_TFree Xs ~~ sum_prod_TsXs; 
49121  137 

49207  138 
val (pre_map_defs, ((unfs0, flds0, fp_iters0, fp_recs0, unf_flds, fld_unfs, fld_injects, 
139 
fp_iter_thms, fp_rec_thms), lthy)) = 

49208  140 
fp_bnf (if lfp then bnf_lfp else bnf_gfp) bs mixfixes As' eqs no_defs_lthy; 
49121  141 

49167  142 
val timer = time (Timer.startRealTimer ()); 
143 

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

49124  146 
Term.subst_atomic_types (Ts0 ~~ Ts) t 
49121  147 
end; 
49119  148 

49126  149 
val mk_unf = mk_unf_or_fld domain_type; 
150 
val mk_fld = mk_unf_or_fld range_type; 

49121  151 

49203  152 
val unfs = map (mk_unf As) unfs0; 
153 
val flds = map (mk_fld As) flds0; 

49124  154 

49201  155 
val fpTs = map (domain_type o fastype_of) unfs; 
49204  156 

49201  157 
val ctr_Tsss = map (map (map (Term.typ_subst_atomic (Xs ~~ fpTs)))) ctr_TsssXs; 
49203  158 
val ns = map length ctr_Tsss; 
49212  159 
val kss = map (fn n => 1 upto n) ns; 
49203  160 
val mss = map (map length) ctr_Tsss; 
161 
val Css = map2 replicate ns Cs; 

162 

49210  163 
fun mk_iter_like Ts Us c = 
49121  164 
let 
49201  165 
val (binders, body) = strip_type (fastype_of c); 
49210  166 
val (f_Us, prebody) = split_last binders; 
167 
val Type (_, Ts0) = if lfp then prebody else body; 

168 
val Us0 = distinct (op =) (map (if lfp then body_type else domain_type) f_Us); 

49176  169 
in 
49201  170 
Term.subst_atomic_types (Ts0 @ Us0 ~~ Ts @ Us) c 
49176  171 
end; 
172 

49210  173 
val fp_iters as fp_iter1 :: _ = map (mk_iter_like As Cs) fp_iters0; 
174 
val fp_recs as fp_rec1 :: _ = map (mk_iter_like As Cs) fp_recs0; 

175 

49212  176 
val fp_iter_fun_Ts = fst (split_last (binder_types (fastype_of fp_iter1))); 
177 
val fp_rec_fun_Ts = fst (split_last (binder_types (fastype_of fp_rec1))); 

49204  178 

179 
fun dest_rec_pair (T as Type (@{type_name prod}, Us as [_, U])) = 

180 
if member (op =) Cs U then Us else [T] 

181 
 dest_rec_pair T = [T]; 

182 

49212  183 
val (((gss, g_Tss, ysss), (hss, h_Tss, zssss)), 
184 
(cs, cpss, p_Tss, coiter_extra as ((pgss, cgsss), g_sum_prod_Ts, g_prod_Tss, g_Tsss), 

185 
corec_extra as ((phss, chsss), h_sum_prod_Ts, h_prod_Tss, h_Tsss))) = 

49208  186 
if lfp then 
187 
let 

188 
val y_Tsss = 

49210  189 
map3 (fn n => fn ms => map2 dest_tupleT ms o dest_sumTN n o domain_type) 
49212  190 
ns mss fp_iter_fun_Ts; 
49208  191 
val g_Tss = map2 (map2 (curry (op >))) y_Tsss Css; 
192 

193 
val ((gss, ysss), _) = 

194 
lthy 

195 
> mk_Freess "f" g_Tss 

196 
>> mk_Freesss "x" y_Tsss; 

49204  197 

49208  198 
val z_Tssss = 
49210  199 
map3 (fn n => fn ms => map2 (map dest_rec_pair oo dest_tupleT) ms o dest_sumTN n 
49212  200 
o domain_type) ns mss fp_rec_fun_Ts; 
49208  201 
val h_Tss = map2 (map2 (fold_rev (curry (op >)))) z_Tssss Css; 
49204  202 

49208  203 
val hss = map2 (map2 retype_free) gss h_Tss; 
204 
val (zssss, _) = 

205 
lthy 

206 
> mk_Freessss "x" z_Tssss; 

49210  207 
in 
49212  208 
(((gss, g_Tss, ysss), (hss, h_Tss, zssss)), 
209 
([], [], [], (([], []), [], [], []), (([], []), [], [], []))) 

49210  210 
end 
49208  211 
else 
49210  212 
let 
49211  213 
val p_Tss = 
49210  214 
map2 (fn C => fn n => replicate (Int.max (0, n  1)) (C > HOLogic.boolT)) Cs ns; 
49211  215 

49212  216 
fun mk_types fun_Ts = 
217 
let 

218 
val f_sum_prod_Ts = map range_type fun_Ts; 

219 
val f_prod_Tss = map2 dest_sumTN ns f_sum_prod_Ts; 

220 
val f_Tsss = 

221 
map3 (fn C => map2 (map (curry (op >) C) oo dest_tupleT)) Cs mss f_prod_Tss; 

222 
val pf_Tss = map2 popescu_zip p_Tss f_Tsss 

223 
in (f_sum_prod_Ts, f_prod_Tss, f_Tsss, pf_Tss) end; 

49176  224 

49212  225 
val (g_sum_prod_Ts, g_prod_Tss, g_Tsss, pg_Tss) = mk_types fp_iter_fun_Ts; 
226 
val (h_sum_prod_Ts, h_prod_Tss, h_Tsss, ph_Tss) = mk_types fp_rec_fun_Ts; 

49211  227 

228 
val (((c, pss), gsss), _) = 

49210  229 
lthy 
230 
> yield_singleton (mk_Frees "c") dummyT 

49211  231 
>> mk_Freess "p" p_Tss 
49210  232 
>> mk_Freesss "g" g_Tsss; 
233 

49211  234 
val hsss = map2 (map2 (map2 retype_free)) gsss h_Tsss; 
235 

49210  236 
val cs = map (retype_free c) Cs; 
49212  237 
val cpss = map2 (fn c => map (fn p => p $ c)) cs pss; 
238 

239 
fun mk_terms fsss = 

240 
let 

241 
val pfss = map2 popescu_zip pss fsss; 

242 
val cfsss = map2 (fn c => map (map (fn f => f $ c))) cs fsss 

243 
in (pfss, cfsss) end; 

49210  244 
in 
49212  245 
((([], [], []), ([], [], [])), 
246 
(cs, cpss, p_Tss, (mk_terms gsss, g_sum_prod_Ts, g_prod_Tss, pg_Tss), 

247 
(mk_terms hsss, h_sum_prod_Ts, h_prod_Tss, ph_Tss))) 

49210  248 
end; 
249 

49212  250 
fun pour_some_sugar_on_type (((((((((((((((((b, fpT), C), fld), unf), fp_iter), fp_rec), 
251 
fld_unf), unf_fld), fld_inject), n), ks), ms), ctr_binders), ctr_mixfixes), ctr_Tss), 

252 
disc_binders), sel_binderss) no_defs_lthy = 

49176  253 
let 
49201  254 
val unfT = domain_type (fastype_of fld); 
49210  255 
val ctr_prod_Ts = map HOLogic.mk_tupleT ctr_Tss; 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

256 
val case_Ts = map (fn Ts => Ts > C) ctr_Tss; 
49119  257 

49176  258 
val ((((u, v), fs), xss), _) = 
49204  259 
no_defs_lthy 
49201  260 
> yield_singleton (mk_Frees "u") unfT 
261 
>> yield_singleton (mk_Frees "v") fpT 

49176  262 
>> mk_Frees "f" case_Ts 
49124  263 
>> mk_Freess "x" ctr_Tss; 
49121  264 

49129  265 
val ctr_rhss = 
49121  266 
map2 (fn k => fn xs => 
49210  267 
fold_rev Term.lambda xs (fld $ mk_InN ctr_prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; 
49121  268 

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

269 
val case_binder = Binding.suffix_name ("_" ^ caseN) b; 
49129  270 

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

271 
val case_rhs = 
49176  272 
fold_rev Term.lambda (fs @ [v]) (mk_sum_caseN (map2 mk_uncurried_fun fs xss) $ (unf $ v)); 
49129  273 

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

49201  277 
(case_binder :: ctr_binders) (NoSyn :: ctr_mixfixes) (case_rhs :: ctr_rhss) 
49121  278 
> `Local_Theory.restore; 
279 

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

281 
val phi = Proof_Context.export_morphism lthy lthy'; 

282 

283 
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

284 
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

285 

49203  286 
val ctrs0 = map (Morphism.term phi) raw_ctrs; 
287 
val casex0 = Morphism.term phi raw_case; 

288 

289 
val ctrs = map (mk_ctr As) ctrs0; 

49121  290 

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

292 
let 
49135  293 
val fld_iff_unf_thm = 
294 
let 

295 
val goal = 

296 
fold_rev Logic.all [u, v] 

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

298 
in 

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

49201  300 
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [unfT, fpT]) 
49176  301 
(certify lthy fld) (certify lthy unf) fld_unf unf_fld) 
49135  302 
> Thm.close_derivation 
303 
> Morphism.thm phi 

304 
end; 

305 

306 
val sumEN_thm' = 

307 
Local_Defs.unfold lthy @{thms all_unit_eq} 

49210  308 
(Drule.instantiate' (map (SOME o certifyT lthy) ctr_prod_Ts) [] (mk_sumEN n)) 
49135  309 
> Morphism.thm phi; 
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset

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

311 
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

312 
end; 
49121  313 

49126  314 
val inject_tacss = 
49205  315 
map2 (fn 0 => K []  _ => fn ctr_def => [fn {context = ctxt, ...} => 
316 
mk_inject_tac ctxt ctr_def fld_inject]) ms ctr_defs; 

49126  317 

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

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

321 

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

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

323 
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

324 
mk_case_tac ctxt n k m case_def ctr_def unf_fld) ks ms ctr_defs; 
49121  325 

326 
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

327 

49210  328 
fun some_lfp_sugar no_defs_lthy = 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

329 
let 
49208  330 
val fpT_to_C = fpT > C; 
331 
val iter_T = fold_rev (curry (op >)) g_Tss fpT_to_C; 

332 
val rec_T = fold_rev (curry (op >)) h_Tss fpT_to_C; 

49199  333 

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

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

336 

337 
val iter_spec = 

49212  338 
mk_Trueprop_eq (lists_bmoc gss (Free (Binding.name_of iter_binder, iter_T)), 
49199  339 
Term.list_comb (fp_iter, map2 (mk_sum_caseN oo map2 mk_uncurried_fun) gss ysss)); 
340 
val rec_spec = 

49212  341 
mk_Trueprop_eq (lists_bmoc hss (Free (Binding.name_of rec_binder, rec_T)), 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

342 
Term.list_comb (fp_rec, map2 (mk_sum_caseN oo map2 mk_uncurried2_fun) hss zssss)); 
49199  343 

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

49201  345 
> apfst split_list o fold_map2 (fn b => fn spec => 
49199  346 
Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec)) 
49201  347 
#>> apsnd snd) [iter_binder, rec_binder] [iter_spec, rec_spec] 
49199  348 
> `Local_Theory.restore; 
49201  349 

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

351 
val phi = Proof_Context.export_morphism lthy lthy'; 

352 

353 
val iter_def = Morphism.thm phi raw_iter_def; 

354 
val rec_def = Morphism.thm phi raw_rec_def; 

355 

49203  356 
val iter0 = Morphism.term phi raw_iter; 
357 
val rec0 = Morphism.term phi raw_rec; 

358 

49210  359 
val iter = mk_iter_like As Cs iter0; 
360 
val recx = mk_iter_like As Cs rec0; 

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

361 
in 
49205  362 
((ctrs, iter, recx, xss, ctr_defs, iter_def, rec_def), lthy) 
49134
846264f80f16
optionally provide extra dead variables to the FP constructions
blanchet
parents:
49130
diff
changeset

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

364 

49210  365 
fun some_gfp_sugar no_defs_lthy = 
366 
let 

367 
val B_to_fpT = C > fpT; 

49211  368 

49212  369 
fun generate_coiter_like (suf, fp_iter_like, ((pfss, cfsss), f_sum_prod_Ts, f_prod_Tss, 
370 
pf_Tss)) = 

49211  371 
let 
372 
val res_T = fold_rev (curry (op >)) pf_Tss B_to_fpT; 

49210  373 

49211  374 
val binder = Binding.suffix_name ("_" ^ suf) b; 
49210  375 

49211  376 
fun mk_join c n cps sum_prod_T prod_Ts cfss = 
377 
Term.lambda c (mk_IfN sum_prod_T cps 

378 
(map2 (mk_InN prod_Ts) (map HOLogic.mk_tuple cfss) (1 upto n))); 

49210  379 

49211  380 
val spec = 
49212  381 
mk_Trueprop_eq (lists_bmoc pfss (Free (Binding.name_of binder, res_T)), 
49211  382 
Term.list_comb (fp_iter_like, 
383 
map6 mk_join cs ns cpss f_sum_prod_Ts f_prod_Tss cfsss)); 

384 
in (binder, spec) end; 

49210  385 

49212  386 
val coiter_likes = 
387 
[(coiterN, fp_iter, coiter_extra), 

388 
(corecN, fp_rec, corec_extra)]; 

389 

49211  390 
val (binders, specs) = map generate_coiter_like coiter_likes > split_list; 
391 

392 
val ((csts, defs), (lthy', lthy)) = no_defs_lthy 

49210  393 
> apfst split_list o fold_map2 (fn b => fn spec => 
394 
Specification.definition (SOME (b, NONE, NoSyn), ((Thm.def_binding b, []), spec)) 

49211  395 
#>> apsnd snd) binders specs 
49210  396 
> `Local_Theory.restore; 
397 

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

399 
val phi = Proof_Context.export_morphism lthy lthy'; 

400 

49211  401 
val [coiter_def, corec_def] = map (Morphism.thm phi) defs; 
49210  402 

49211  403 
val [coiter, corec] = map (mk_iter_like As Cs o Morphism.term phi) csts; 
49210  404 
in 
405 
((ctrs, coiter, corec, xss, ctr_defs, coiter_def, corec_def), lthy) 

406 
end; 

49119  407 
in 
49203  408 
wrap_datatype tacss ((ctrs0, casex0), (disc_binders, sel_binderss)) lthy' 
49210  409 
> (if lfp then some_lfp_sugar else some_gfp_sugar) 
49119  410 
end; 
49167  411 

49212  412 
fun pour_more_sugar_on_lfps ((ctrss, iters, recs, xsss, ctr_defss, iter_defs, rec_defs), 
49205  413 
lthy) = 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

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

415 
val xctrss = map2 (map2 (curry Term.list_comb)) ctrss xsss; 
49212  416 
val giters = map (lists_bmoc gss) iters; 
417 
val hrecs = map (lists_bmoc hss) recs; 

49201  418 

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

419 
val (iter_thmss, rec_thmss) = 
49207  420 
let 
49212  421 
fun mk_goal_iter_like fss fiter_like xctr f xs fxs = 
49207  422 
fold_rev (fold_rev Logic.all) (xs :: fss) 
49212  423 
(mk_Trueprop_eq (fiter_like $ xctr, Term.list_comb (f, fxs))); 
49204  424 

49212  425 
fun repair_iter_call (x as Free (_, T)) = 
426 
(case find_index (curry (op =) T) fpTs of ~1 => x  j => nth giters j $ x); 

427 
fun repair_rec_call (x as Free (_, T)) = 

428 
(case find_index (curry (op =) T) fpTs of ~1 => [x]  j => [x, nth hrecs j $ x]); 

49201  429 

49212  430 
val gxsss = map (map (map repair_iter_call)) xsss; 
431 
val hxsss = map (map (maps repair_rec_call)) xsss; 

49204  432 

49212  433 
val goal_iterss = map5 (map4 o mk_goal_iter_like gss) giters xctrss gss xsss gxsss; 
434 
val goal_recss = map5 (map4 o mk_goal_iter_like hss) hrecs xctrss hss xsss hxsss; 

49204  435 

49203  436 
val iter_tacss = 
49211  437 
map2 (map o mk_iter_like_tac pre_map_defs iter_defs) fp_iter_thms ctr_defss; 
49203  438 
val rec_tacss = 
49211  439 
map2 (map o mk_iter_like_tac pre_map_defs rec_defs) fp_rec_thms ctr_defss; 
49202
f493cd25737f
some work towards iterator and recursor properties
blanchet
parents:
49201
diff
changeset

440 
in 
49205  441 
(map2 (map2 (fn goal => fn tac => Skip_Proof.prove lthy [] [] goal (tac o #context))) 
442 
goal_iterss iter_tacss, 

443 
map2 (map2 (fn goal => fn tac => Skip_Proof.prove lthy [] [] goal (tac o #context))) 

444 
goal_recss rec_tacss) 

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

445 
end; 
49201  446 

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

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

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

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

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

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

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

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

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

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

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

457 

49212  458 
fun pour_more_sugar_on_gfps ((ctrss, coiters, corecs, xsss, ctr_defss, coiter_defs, corec_defs), 
459 
lthy) = 

460 
let 

461 
val gcoiters = map (lists_bmoc pgss) coiters; 

462 
val hcorecs = map (lists_bmoc phss) corecs; 

463 

464 
val (coiter_thmss, corec_thmss) = 

465 
let 

466 
fun mk_cond pos = HOLogic.mk_Trueprop o (not pos ? HOLogic.mk_not); 

467 

468 
fun mk_goal_coiter_like pfss c cps fcoiter_like n k ctr cfs' = 

469 
fold_rev (fold_rev Logic.all) ([c] :: pfss) 

470 
(Logic.list_implies (seq_conds mk_cond n k cps, 

471 
mk_Trueprop_eq (fcoiter_like $ c, Term.list_comb (ctr, cfs')))); 

472 

473 
fun repair_coiter_like_call fcoiter_likes (cf as Free (_, Type (_, [_, T])) $ _) = 

474 
(case find_index (curry (op =) T) Cs of ~1 => cf  j => nth fcoiter_likes j $ cf); 

475 

476 
val cgsss = map (map (map (repair_coiter_like_call gcoiters))) cgsss; 

477 
val chsss = map (map (map (repair_coiter_like_call hcorecs))) chsss; 

478 

479 
val goal_coiterss = 

480 
map7 (map3 oooo mk_goal_coiter_like pgss) cs cpss gcoiters ns kss ctrss cgsss; 

481 
val goal_corecss = 

482 
map7 (map3 oooo mk_goal_coiter_like phss) cs cpss hcorecs ns kss ctrss chsss; 

483 
in 

484 
(map (map (Skip_Proof.make_thm (Proof_Context.theory_of lthy))) goal_coiterss, 

485 
map (map (Skip_Proof.make_thm (Proof_Context.theory_of lthy))) goal_coiterss (*### goal_corecss*)) 

486 
end; 

487 

488 
val notes = 

489 
[(coitersN, coiter_thmss), 

490 
(corecsN, corec_thmss)] 

491 
> maps (fn (thmN, thmss) => 

492 
map2 (fn b => fn thms => 

493 
((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])])) 

494 
bs thmss); 

495 
in 

496 
lthy > Local_Theory.notes notes > snd 

497 
end; 

498 

49204  499 
val lthy' = lthy 
49210  500 
> fold_map pour_some_sugar_on_type (bs ~~ fpTs ~~ Cs ~~ flds ~~ unfs ~~ fp_iters ~~ 
49212  501 
fp_recs ~~ fld_unfs ~~ unf_flds ~~ fld_injects ~~ ns ~~ kss ~~ mss ~~ ctr_binderss ~~ 
502 
ctr_mixfixess ~~ ctr_Tsss ~~ disc_binderss ~~ sel_bindersss) 

49205  503 
>> split_list7 
49212  504 
> (if lfp then pour_more_sugar_on_lfps else pour_more_sugar_on_gfps); 
49167  505 

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

49208  507 
(if lfp then "" else "co") ^ "datatype")); 
49112  508 
in 
49204  509 
(timer; lthy') 
49112  510 
end; 
511 

49199  512 
fun datatype_cmd info specs lthy = 
49121  513 
let 
49209  514 
(* TODO: cleaner handling of fake contexts, without "background_theory" *) 
49184
83fdea0c4779
gracefully handle shadowing case (fourth step of sugar localization)
blanchet
parents:
49183
diff
changeset

515 
(*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

516 
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

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

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

519 
(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

520 
val fake_lthy = Proof_Context.background_theory fake_thy lthy; 
49121  521 
in 
49199  522 
prepare_datatype Syntax.read_typ info specs fake_lthy lthy 
49121  523 
end; 
49119  524 

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

49112  527 
val parse_ctr_arg = 
49119  528 
Parse.$$$ "("  parse_opt_binding_colon  Parse.typ  Parse.$$$ ")"  
49129  529 
(Parse.typ >> pair no_binder); 
49112  530 

531 
val parse_single_spec = 

532 
Parse.type_args_constrained  Parse.binding  Parse.opt_mixfix  

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

49112  535 

536 
val _ = 

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

49208  538 
(Parse.and_list1 parse_single_spec >> datatype_cmd true); 
49112  539 

540 
val _ = 

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

49208  542 
(Parse.and_list1 parse_single_spec >> datatype_cmd false); 
49112  543 

544 
end; 