210 ML {* add_primrec_wrapper *} |
210 ML {* add_primrec_wrapper *} |
211 |
211 |
212 ML {* |
212 ML {* |
213 fun nominal_datatype2 dts bn_funs bn_eqs binds lthy = |
213 fun nominal_datatype2 dts bn_funs bn_eqs binds lthy = |
214 let |
214 let |
215 val (((raw_dt_names, (raw_bn_funs, raw_bn_eqs)), raw_binds), lthy2) = |
215 val (((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_binds), lthy2) = |
216 raw_nominal_decls dts bn_funs bn_eqs binds lthy |
216 raw_nominal_decls dts bn_funs bn_eqs binds lthy |
|
217 val morphism_2_1 = ProofContext.export_morphism lthy2 lthy |
|
218 val raw_bn_funs = map (Morphism.term morphism_2_1) raw_bn_funs_loc |
|
219 val raw_bn_eqs = ProofContext.export lthy2 lthy raw_bn_eqs_loc |
217 val dtinfo = Datatype.the_info (ProofContext.theory_of lthy2) (hd raw_dt_names); |
220 val dtinfo = Datatype.the_info (ProofContext.theory_of lthy2) (hd raw_dt_names); |
218 val descr = #descr dtinfo; |
221 val descr = #descr dtinfo; |
|
222 val sorts = #sorts dtinfo; |
|
223 val all_typs = map (fn i => typ_of_dtyp descr sorts (DtRec i)) (map fst descr) |
219 val all_full_tnames = map (fn (_, (n, _, _)) => n) descr; |
224 val all_full_tnames = map (fn (_, (n, _, _)) => n) descr; |
220 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy2)) all_full_tnames; |
225 val dtinfos = map (Datatype.the_info (ProofContext.theory_of lthy2)) all_full_tnames; |
221 val inject = flat (map #inject dtinfos); |
226 val inject = flat (map #inject dtinfos); |
222 val distinct = flat (map #distinct dtinfos); |
227 val distinct = flat (map #distinct dtinfos); |
|
228 val induct = #induct dtinfo; |
223 val inducts = #inducts dtinfo; |
229 val inducts = #inducts dtinfo; |
224 val ((raw_perm_def, raw_perm_simps, perms), lthy3) = |
230 val ((raw_perm_def, raw_perm_simps, perms), lthy3) = |
225 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2; |
231 Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2; |
226 val raw_binds_flat = map (map flat) raw_binds; |
232 val raw_binds_flat = map (map flat) raw_binds; |
227 val (((fv_ts_loc, fv_def_loc), alpha), lthy4) = define_fv_alpha dtinfo raw_binds_flat lthy3; |
233 val (((fv_ts_loc, fv_def_loc), alpha), lthy4) = define_fv_alpha dtinfo raw_binds_flat lthy3; |
|
234 val alpha_ts_loc = #preds alpha |
|
235 val morphism_4_3 = ProofContext.export_morphism lthy4 lthy3; |
|
236 val alpha_ts = map (Morphism.term morphism_4_3) alpha_ts_loc; |
|
237 val alpha_induct_loc = #induct alpha |
228 val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo); |
238 val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo); |
229 val _ = tracing (PolyML.makestring dts_names); |
|
230 val bn_tys = map (domain_type o fastype_of) raw_bn_funs; |
239 val bn_tys = map (domain_type o fastype_of) raw_bn_funs; |
231 val _ = tracing (PolyML.makestring bn_tys); |
|
232 val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys; |
240 val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys; |
233 val _ = tracing (PolyML.makestring bn_nos); |
|
234 val bns = raw_bn_funs ~~ bn_nos; |
241 val bns = raw_bn_funs ~~ bn_nos; |
235 val alpha_intros = #intrs alpha; |
242 val alpha_intros = #intrs alpha; |
236 val alpha_cases = #elims alpha |
243 val alpha_cases = #elims alpha |
237 val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases lthy4 |
244 val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases lthy4 |
238 val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4; |
245 val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4; |
239 |
246 val (fv_eqvts, lthy6) = build_eqvts Binding.empty fv_ts_loc perms |
240 in |
247 ((flat (map snd bv_eqvts)) @ fv_def_loc @ raw_perm_def) induct lthy5; |
241 ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy5) |
248 val alpha_eqvt_loc = build_alpha_eqvts alpha_ts_loc perms |
|
249 (raw_perm_def @ alpha_inj_loc) alpha_induct_loc lthy6; |
|
250 val alpha_eqvt = ProofContext.export lthy6 lthy2 alpha_eqvt_loc; |
|
251 val alpha_equivp_loc = build_equivps alpha_ts_loc induct alpha_induct_loc |
|
252 inject alpha_inj_loc distinct alpha_cases alpha_eqvt_loc lthy6; |
|
253 val alpha_equivp = ProofContext.export lthy6 lthy2 alpha_equivp_loc; |
|
254 val qty_names = map (fn (_, b, _, _) => b) dts; |
|
255 val lthy7 = define_quotient_type |
|
256 (map (fn ((b, t), alpha) => (([], b, NoSyn), (t, alpha))) ((qty_names ~~ all_typs) ~~ alpha_ts)) |
|
257 (ALLGOALS (resolve_tac alpha_equivp)) lthy6; |
|
258 in |
|
259 ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy7) |
242 end |
260 end |
243 *} |
261 *} |
244 |
262 |
245 ML {* |
263 ML {* |
246 (* parsing the datatypes and declaring *) |
264 (* parsing the datatypes and declaring *) |