197 end; |
197 end; |
198 |
198 |
199 val mk_ctor = mk_ctor_or_dtor range_type; |
199 val mk_ctor = mk_ctor_or_dtor range_type; |
200 val mk_dtor = mk_ctor_or_dtor domain_type; |
200 val mk_dtor = mk_ctor_or_dtor domain_type; |
201 |
201 |
202 fun mk_xxiter lfp Ts Us t = |
202 fun mk_co_iter lfp Ts Us t = |
203 let |
203 let |
204 val (bindings, body) = strip_type (fastype_of t); |
204 val (bindings, body) = strip_type (fastype_of t); |
205 val (f_Us, prebody) = split_last bindings; |
205 val (f_Us, prebody) = split_last bindings; |
206 val Type (_, Ts0) = if lfp then prebody else body; |
206 val Type (_, Ts0) = if lfp then prebody else body; |
207 val Us0 = distinct (op =) (map (if lfp then body_type else domain_type) f_Us); |
207 val Us0 = distinct (op =) (map (if lfp then body_type else domain_type) f_Us); |
210 end; |
210 end; |
211 |
211 |
212 val mk_fp_iter_fun_types = fst o split_last o binder_types o fastype_of; |
212 val mk_fp_iter_fun_types = fst o split_last o binder_types o fastype_of; |
213 |
213 |
214 fun mk_fp_iter lfp As Cs fp_iters0 = |
214 fun mk_fp_iter lfp As Cs fp_iters0 = |
215 map (mk_xxiter lfp As Cs) fp_iters0 |
215 map (mk_co_iter lfp As Cs) fp_iters0 |
216 |> (fn ts => (ts, mk_fp_iter_fun_types (hd ts))); |
216 |> (fn ts => (ts, mk_fp_iter_fun_types (hd ts))); |
217 |
217 |
218 fun unzip_recT fpTs T = |
218 fun unzip_recT fpTs T = |
219 let |
219 let |
220 fun project_recT proj = |
220 fun project_recT proj = |
1214 |
1214 |
1215 val phi = Proof_Context.export_morphism lthy lthy'; |
1215 val phi = Proof_Context.export_morphism lthy lthy'; |
1216 |
1216 |
1217 val [fold_def, rec_def] = map (Morphism.thm phi) defs; |
1217 val [fold_def, rec_def] = map (Morphism.thm phi) defs; |
1218 |
1218 |
1219 val [foldx, recx] = map (mk_xxiter lfp As Cs o Morphism.term phi) csts; |
1219 val [foldx, recx] = map (mk_co_iter lfp As Cs o Morphism.term phi) csts; |
1220 in |
1220 in |
1221 ((foldx, recx, fold_def, rec_def), lthy') |
1221 ((foldx, recx, fold_def, rec_def), lthy') |
1222 end; |
1222 end; |
1223 |
1223 |
1224 fun define_unfold_corec no_defs_lthy = |
1224 fun define_unfold_corec no_defs_lthy = |
1264 |
1264 |
1265 val phi = Proof_Context.export_morphism lthy lthy'; |
1265 val phi = Proof_Context.export_morphism lthy lthy'; |
1266 |
1266 |
1267 val [unfold_def, corec_def] = map (Morphism.thm phi) defs; |
1267 val [unfold_def, corec_def] = map (Morphism.thm phi) defs; |
1268 |
1268 |
1269 val [unfold, corec] = map (mk_xxiter lfp As Cs o Morphism.term phi) csts; |
1269 val [unfold, corec] = map (mk_co_iter lfp As Cs o Morphism.term phi) csts; |
1270 in |
1270 in |
1271 ((unfold, corec, unfold_def, corec_def), lthy') |
1271 ((unfold, corec, unfold_def, corec_def), lthy') |
1272 end; |
1272 end; |
1273 |
1273 |
1274 fun massage_res (((maps_sets_rels, ctr_sugar), xxiter_res), lthy) = |
1274 fun massage_res (((maps_sets_rels, ctr_sugar), co_iter_res), lthy) = |
1275 (((maps_sets_rels, (ctrs, xss, ctr_defs, ctr_sugar)), xxiter_res), lthy); |
1275 (((maps_sets_rels, (ctrs, xss, ctr_defs, ctr_sugar)), co_iter_res), lthy); |
1276 in |
1276 in |
1277 (wrap |
1277 (wrap |
1278 #> derive_maps_sets_rels |
1278 #> derive_maps_sets_rels |
1279 ##>> (if lfp then define_fold_rec else define_unfold_corec) |
1279 ##>> (if lfp then define_fold_rec else define_unfold_corec) |
1280 #> massage_res, lthy') |
1280 #> massage_res, lthy') |