Skip to content

Commit 3883210

Browse files
committed
Make most of the sugar also work for codatatypes
1 parent 191ed7c commit 3883210

File tree

1 file changed

+74
-60
lines changed

1 file changed

+74
-60
lines changed

Tools/mrbnf_sugar.ML

Lines changed: 74 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,22 @@ fun build_set_for _ aT x (TFree (s, _)) = if s = fst (dest_TFree aT) then SOME (
248248
end
249249
) (mr_bnf_of lthy false C);
250250

251-
fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
251+
fun create_binder_datatype co (spec : spec) lthy =
252252
let
253-
val (recursor_result, lthy) = MRBNF_Recursor.create_binding_recursor I res lthy;
253+
val fp_kind = if co then MRBNF_Util.Greatest_FP else MRBNF_Util.Least_FP;
254+
val ((res, fp_pre_T, mrbnf, absinfo), lthy) = create_binder_type fp_kind spec lthy;
254255

255-
val ([(rec_mrbnf, vvsubst_res)], lthy) = MRBNF_VVSubst.mrbnf_of_quotient_fixpoint [#map_b spec] I res (#QREC_fixed recursor_result) lthy;
256-
val lthy = MRBNF_Def.register_mrbnf_raw (fst (dest_Type (#T (hd (#quotient_fps res))))) rec_mrbnf lthy;
256+
val (vvsubst_res_opt, lthy) = if co then (NONE, lthy) else (* TODO: Automate renaming for codatatypes *)
257+
let
258+
val (recursor_result, lthy) = MRBNF_Recursor.create_binding_recursor I res lthy;
259+
260+
val ([(rec_mrbnf, vvsubst_res)], lthy) = MRBNF_VVSubst.mrbnf_of_quotient_fixpoint [#map_b spec] I res (#QREC_fixed recursor_result) lthy;
261+
val lthy = MRBNF_Def.register_mrbnf_raw (fst (dest_Type (#T (hd (#quotient_fps res))))) rec_mrbnf lthy;
262+
in (SOME {
263+
recursor_result = recursor_result,
264+
rec_mrbnf = rec_mrbnf,
265+
vvsubst_res = vvsubst_res
266+
}, lthy) end
257267

258268
val nrecs = fold (curry (op+)) (#rec_vars res) 0;
259269
val quotient_ctor = #ctor (hd (#quotient_fps res));
@@ -293,15 +303,15 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
293303
val (vars, passives) = chop (length (#binding_rel spec)) vars;
294304

295305
val ((pfrees, plives), pbounds) = passives
296-
|> chop (MRBNF_Def.free_of_mrbnf rec_mrbnf - length vars)
297-
||>> chop (MRBNF_Def.live_of_mrbnf rec_mrbnf);
306+
|> chop (MRBNF_Def.free_of_mrbnf mrbnf - length vars)
307+
||>> chop (MRBNF_Def.live_of_mrbnf mrbnf - nrecs);
298308

299309
val (plives', _) = lthy
300310
|> fold_rev Variable.declare_typ (vars @ passives)
301311
|> mk_TFrees (length plives);
302312
val args' = fst (@{fold_map 2} (fn MRBNF_Def.Live_Var => K (fn xs => (hd xs, tl xs))
303313
| _ => fn x => fn xs => (x, xs)
304-
) (MRBNF_Def.var_types_of_mrbnf rec_mrbnf) (vars @ passives) plives');
314+
) (take (length (vars @ passives)) (MRBNF_Def.var_types_of_mrbnf mrbnf)) (vars @ passives) plives');
305315

306316
val fp_nesting_mrbnfs = nesting_mrbnfs lthy [map snd (#ctors spec)] (*(take (#rec_vars spec) (
307317
map_filter (fn (x, Live_Var) => SOME (TFree x) | _ => NONE) (#vars spec)
@@ -550,7 +560,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
550560
in ((bset_opt, sets), Local_Defs.unfold0 lthy @{thms insert_is_Un[symmetric]} (Goal.prove_sorry lthy (names xs) [] goal (fn {context=ctxt, ...} => EVERY1 [
551561
K (Local_Defs.unfold0_tac ctxt (
552562
set_simp_thms @ map snd ctors @ #FVars_ctors quotient @ MRBNF_Def.set_defs_of_mrbnf pre_mrbnf
553-
@ [#Abs_inverse (snd info) OF @{thms UNIV_I}] @ #pset_ctors vvsubst_res
563+
@ [#Abs_inverse (snd info) OF @{thms UNIV_I}] @ the_default [] (Option.map (#pset_ctors o #vvsubst_res) vvsubst_res_opt)
554564
)),
555565
REPEAT_DETERM o EVERY' [
556566
EqSubst.eqsubst_tac ctxt [0] (maps MRBNF_Def.set_map_of_mrbnf fp_nesting_mrbnfs),
@@ -571,7 +581,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
571581
]
572582
]))) end
573583
) ctors (#ctors spec))) end
574-
) (#FVarss quotient @ map fst (#psets vvsubst_res))));
584+
) (#FVarss quotient @ map fst (the_default [] (Option.map (#psets o #vvsubst_res) vvsubst_res_opt)))));
575585

576586
val ctors_tys = ctors ~~ map snd (#ctors spec);
577587
val distinct = flat (flat (map_index (fn (i, ((ctor, ctor_def), tys1)) => map_index (fn (j, ((ctor2, ctor2_def), tys2)) =>
@@ -594,12 +604,16 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
594604

595605
val injects = @{map_filter 2} (fn ((ctor, ctor_def), tys) => fn setss => if null tys then NONE else
596606
let
607+
val class = if co then (* TODO *)
608+
[the (Var_Classes.get_class_for_bound (mk_card_suc (MRBNF_Def.bd_of_mrbnf mrbnf)) lthy)]
609+
else MRBNF_Def.class_of_mrbnf mrbnf;
610+
597611
val bound_tys = inter (op=) bounds (rev (map TFree (fold Term.add_tfreesT tys [])));
598612
val bfree_tys = inter (op=) bfrees (rev (map TFree (fold Term.add_tfreesT tys [])));
599613
val ((bound_tys', bfree_tys'), _) = lthy
600614
|> fold Variable.declare_typ (map fst replace @ map snd replace)
601-
|> mk_TFrees' (replicate (length bound_tys) (MRBNF_Def.class_of_mrbnf mrbnf))
602-
||>> mk_TFrees' (replicate (length bfree_tys) (MRBNF_Def.class_of_mrbnf mrbnf));
615+
|> mk_TFrees' (replicate (length bound_tys) class)
616+
||>> mk_TFrees' (replicate (length bfree_tys) class);
603617

604618
val tys' = map (Term.typ_subst_atomic replace) tys;
605619
val (((xs, ys), fs'), _) = names_lthy
@@ -608,7 +622,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
608622
||>> mk_Frees "f" (map (fn a => a --> a) bound_tys');
609623

610624
val replace = map (fn (a, b) => if member (op=) bfrees a then
611-
(a, resort_tfree_or_tvar (MRBNF_Def.class_of_mrbnf mrbnf) (
625+
(a, resort_tfree_or_tvar class (
612626
Term.typ_subst_atomic (bound_tys ~~ bound_tys') (
613627
Term.typ_subst_atomic (map swap (filter (member (op=) bounds o fst) replace)) b
614628
)
@@ -786,7 +800,6 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
786800
val goal = mk_Trueprop_eq (
787801
HOLogic.mk_eq (Term.list_comb (ctor, xs), Term.list_comb (ctor, ys)), rhs
788802
);
789-
val infinite_UNIV = @{thm cinfinite_imp_infinite} OF [MRBNF_Def.UNIV_cinfinite_of_mrbnf rec_mrbnf];
790803
in SOME (Goal.prove_sorry lthy (names (xs @ ys)) [] goal (fn {context=ctxt, ...} => EVERY1 [
791804
rtac ctxt trans,
792805
rtac ctxt inject,
@@ -806,7 +819,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
806819
REPEAT_DETERM o EVERY' [
807820
TRY o rtac ctxt conjI,
808821
REPEAT_DETERM1 o FIRST' [
809-
resolve_tac ctxt (@{thms refl supp_id_bound bij_id bij_swap supp_swap_bound} @ [infinite_UNIV]),
822+
resolve_tac ctxt (@{thms refl supp_id_bound bij_id bij_swap supp_swap_bound infinite_class.infinite_UNIV}),
810823
assume_tac ctxt,
811824
resolve_tac ctxt (map map_cong0_of_mr_bnf (maps (map snd) mr_bnfs)),
812825
EVERY' [
@@ -825,7 +838,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
825838
REPEAT_DETERM o rtac ctxt @{thm conjI[rotated]},
826839
REPEAT_DETERM o FIRST' [
827840
assume_tac ctxt,
828-
resolve_tac ctxt (@{thms swap_simps supp_swap_bound bij_swap} @ [infinite_UNIV]),
841+
resolve_tac ctxt (@{thms swap_simps supp_swap_bound bij_swap infinite_class.infinite_UNIV}),
829842
etac ctxt @{thm id_on_swap}
830843
]
831844
])) end
@@ -921,7 +934,7 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
921934
| SOME t => SOME (f, map HOLogic.mk_Trueprop (if var_type = MRBNF_Def.Bound_Var then
922935
[mk_bij f, t] else [t]
923936
))
924-
) (take (length fs) (MRBNF_Def.var_types_of_mrbnf rec_mrbnf)) fs);
937+
) (take (length fs) (MRBNF_Def.var_types_of_mrbnf mrbnf)) fs);
925938

926939
fun mk_map (T as Type (n, Ts)) = (case MRBNF_Def.mrbnf_of lthy n of
927940
SOME mrbnf =>
@@ -1047,8 +1060,11 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
10471060
) end
10481061
) ctors (#ctors spec) end;
10491062

1050-
val map_simps =
1063+
val map_simps = if co then [] else (* TODO *)
10511064
let
1065+
val rec_mrbnf = #rec_mrbnf (the vvsubst_res_opt)
1066+
val vvsubst_res = #vvsubst_res (the vvsubst_res_opt)
1067+
10521068
val (fs, _) = lthy
10531069
|> mk_Frees "f" (map2 (curry (op-->)) (vars @ passives) args');
10541070

@@ -1115,8 +1131,9 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
11151131
REPEAT_DETERM o resolve_tac ctxt (MRBNF_Def.UNIV_cinfinite_of_mrbnf mrbnf :: @{thms cmin_Cinfinite conjI card_of_Card_order})
11161132
]);
11171133

1118-
val (lthy, tvsubst_opt) = if not (null (map_filter I (#etas tvsubst_model))) then
1134+
val (lthy, tvsubst_opt) = if not (null (map_filter I (#etas tvsubst_model))) andalso not co then
11191135
let
1136+
val recursor_result = #recursor_result (the vvsubst_res_opt);
11201137
val (tvsubst_ress, lthy) = MRBNF_TVSubst.create_tvsubst_of_mrbnf (Binding.prefix_name "tv") res [tvsubst_model] (#QREC_cmin_fixed recursor_result) lthy;
11211138
val tvsubst_res = hd tvsubst_ress;
11221139
val tvsubst_simps =
@@ -1181,47 +1198,6 @@ fun create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy =
11811198
val induct_attrib = Attrib.internal Position.none (K (Induct.induct_type (fst (dest_Type qT))))
11821199
val equiv = @{attributes [simp, equiv]}
11831200

1184-
val sugar = {
1185-
map_simps = map_simps,
1186-
set_simpss = set_simpss,
1187-
permute_simps = permute_simps,
1188-
map_permute = SOME (#vvsubst_permute vvsubst_res),
1189-
strong_induct = strong_induct_opt,
1190-
subst_simps = Option.map snd tvsubst_opt,
1191-
bsetss = bset_optss,
1192-
bset_bounds = [],
1193-
mrbnf = mrbnf,
1194-
distinct = distinct,
1195-
inject = injects,
1196-
ctors = ctors
1197-
} : binder_sugar;
1198-
1199-
val lthy = register_binder_sugar (fst (dest_Type qT)) sugar lthy;
1200-
1201-
val note_inducts = Option.map (fn strong_induct => [
1202-
("strong_induct", [strong_induct], []),
1203-
("fresh_induct", [the fresh_induct_opt], []),
1204-
("induct", [the induct_opt], [induct_attrib])
1205-
]) strong_induct_opt;
1206-
val notes =
1207-
(the_default [] note_inducts
1208-
@ [("set", flat set_simpss, simp),
1209-
("map", map_simps, simp),
1210-
("permute", permute_simps, equiv),
1211-
("distinct", distinct, simp),
1212-
("inject", injects, simp)
1213-
] @ the_default [] (Option.map (fn (_, tvsubst_simps) => [("subst", tvsubst_simps, simp)]) tvsubst_opt)
1214-
) |> (map (fn (thmN, thms, attrs) =>
1215-
((Binding.qualify true (Binding.name_of (#fp_b spec)) (Binding.name thmN), attrs), [(thms, [])])
1216-
))
1217-
val (_, lthy) = Local_Theory.notes notes lthy
1218-
in (sugar, lthy) end;
1219-
1220-
fun create_binder_datatype co (spec : spec) lthy =
1221-
let
1222-
val fp_kind = if co then MRBNF_Util.Greatest_FP else MRBNF_Util.Least_FP;
1223-
val ((res, fp_pre_T, mrbnf, absinfo), lthy) = create_binder_type fp_kind spec lthy;
1224-
12251201
val (sugar, lthy) = if co then
12261202
let
12271203
val (locale_name, lthy) = MRBNF_Corecursor.create_binding_corecursor I res lthy;
@@ -1240,7 +1216,45 @@ fun create_binder_datatype co (spec : spec) lthy =
12401216
ctors = []
12411217
} : binder_sugar;
12421218
in (sugar, lthy) end
1243-
else create_binder_datatype_sugar spec res fp_pre_T mrbnf absinfo lthy
1219+
else
1220+
let
1221+
val sugar = {
1222+
map_simps = map_simps,
1223+
set_simpss = set_simpss,
1224+
permute_simps = permute_simps,
1225+
map_permute = SOME (#vvsubst_permute (#vvsubst_res (the vvsubst_res_opt))),
1226+
strong_induct = strong_induct_opt,
1227+
subst_simps = Option.map snd tvsubst_opt,
1228+
bsetss = bset_optss,
1229+
bset_bounds = [],
1230+
mrbnf = mrbnf,
1231+
distinct = distinct,
1232+
inject = injects,
1233+
ctors = ctors
1234+
} : binder_sugar;
1235+
in (sugar, lthy) end
1236+
1237+
val lthy = register_binder_sugar (fst (dest_Type qT)) sugar lthy;
1238+
1239+
val note_inducts = Option.map (fn strong_induct => [
1240+
("strong_induct", [strong_induct], []),
1241+
("fresh_induct", [the fresh_induct_opt], []),
1242+
("induct", [the induct_opt], [induct_attrib])
1243+
]) strong_induct_opt;
1244+
val notes =
1245+
(the_default [] note_inducts
1246+
@ [("set", flat set_simpss, simp),
1247+
("map", map_simps, simp),
1248+
("permute", permute_simps, equiv),
1249+
("distinct", distinct, simp),
1250+
("inject", injects, simp)
1251+
] @ the_default [] (Option.map (fn (_, tvsubst_simps) => [("subst", tvsubst_simps, simp)]) tvsubst_opt)
1252+
)
1253+
|> filter_out (null o #2)
1254+
|> (map (fn (thmN, thms, attrs) =>
1255+
((Binding.qualify true (Binding.name_of (#fp_b spec)) (Binding.name thmN), attrs), [(thms, [])])
1256+
))
1257+
val (_, lthy) = Local_Theory.notes notes lthy;;
12441258
in (sugar, lthy) end;
12451259

12461260
end

0 commit comments

Comments
 (0)