Nominal/Parser.thy
changeset 1340 f201eb6acafc
parent 1339 5256f256edd8
child 1341 c25f797c7e6e
equal deleted inserted replaced
1339:5256f256edd8 1340:f201eb6acafc
   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 thy = ProofContext.theory_of lthy
       
   216   val thy_name = Context.theory_name thy
   215   val (((raw_dt_names, (raw_bn_funs_loc, raw_bn_eqs_loc)), raw_binds), lthy2) =
   217   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
   218     raw_nominal_decls dts bn_funs bn_eqs binds lthy
   217   val morphism_2_1 = ProofContext.export_morphism lthy2 lthy
   219   val morphism_2_1 = ProofContext.export_morphism lthy2 lthy
   218   val raw_bn_funs = map (Morphism.term morphism_2_1) raw_bn_funs_loc
   220   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
   221   val raw_bn_eqs = ProofContext.export lthy2 lthy raw_bn_eqs_loc
   231     Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2;
   233     Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2;
   232   val raw_binds_flat = map (map flat) raw_binds;
   234   val raw_binds_flat = map (map flat) raw_binds;
   233   val (((fv_ts_loc, fv_def_loc), alpha), lthy4) = define_fv_alpha dtinfo raw_binds_flat lthy3;
   235   val (((fv_ts_loc, fv_def_loc), alpha), lthy4) = define_fv_alpha dtinfo raw_binds_flat lthy3;
   234   val alpha_ts_loc = #preds alpha
   236   val alpha_ts_loc = #preds alpha
   235   val morphism_4_3 = ProofContext.export_morphism lthy4 lthy3;
   237   val morphism_4_3 = ProofContext.export_morphism lthy4 lthy3;
       
   238   val fv_ts = map (Morphism.term morphism_4_3) fv_ts_loc;
   236   val alpha_ts = map (Morphism.term morphism_4_3) alpha_ts_loc;
   239   val alpha_ts = map (Morphism.term morphism_4_3) alpha_ts_loc;
   237   val alpha_induct_loc = #induct alpha
   240   val alpha_induct_loc = #induct alpha
       
   241   val [alpha_induct] = ProofContext.export lthy4 lthy3 [alpha_induct_loc];
       
   242   val alpha_inducts = Project_Rule.projects lthy4 (1 upto (length dts)) alpha_induct
       
   243   val fv_def = ProofContext.export lthy4 lthy3 fv_def_loc;
   238   val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo);
   244   val dts_names = map (fn (i, (s, _, _)) => (s, i)) (#descr dtinfo);
   239   val bn_tys = map (domain_type o fastype_of) raw_bn_funs;
   245   val bn_tys = map (domain_type o fastype_of) raw_bn_funs;
   240   val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys;
   246   val bn_nos = map (dtyp_no_of_typ dts_names) bn_tys;
   241   val bns = raw_bn_funs ~~ bn_nos;
   247   val bns = raw_bn_funs ~~ bn_nos;
   242   val alpha_intros = #intrs alpha;
   248   val alpha_intros = #intrs alpha;
   243   val alpha_cases = #elims alpha
   249   val alpha_cases = #elims alpha
   244   val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases lthy4
   250   val alpha_inj_loc = build_alpha_inj alpha_intros (inject @ distinct) alpha_cases lthy4
       
   251   val alpha_inj = ProofContext.export lthy4 lthy3 alpha_inj_loc
   245   val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4;
   252   val (bv_eqvts, lthy5) = fold_map (build_bv_eqvt perms (raw_bn_eqs @ raw_perm_def) inducts) bns lthy4;
   246   val (fv_eqvts, lthy6) = build_eqvts Binding.empty fv_ts_loc perms
   253   val (fv_eqvts, lthy6) = build_eqvts Binding.empty fv_ts_loc perms
   247     ((flat (map snd bv_eqvts)) @ fv_def_loc @ raw_perm_def) induct lthy5;
   254     ((flat (map snd bv_eqvts)) @ fv_def_loc @ raw_perm_def) induct lthy5;
   248   val alpha_eqvt_loc = build_alpha_eqvts alpha_ts_loc perms
   255   val alpha_eqvt_loc = build_alpha_eqvts alpha_ts_loc perms
   249     (raw_perm_def @ alpha_inj_loc) alpha_induct_loc lthy6;
   256     (raw_perm_def @ alpha_inj_loc) alpha_induct_loc lthy6;
   250   val alpha_eqvt = ProofContext.export lthy6 lthy2 alpha_eqvt_loc;
   257   val alpha_eqvt = ProofContext.export lthy6 lthy2 alpha_eqvt_loc;
   251   val alpha_equivp_loc = build_equivps alpha_ts_loc induct alpha_induct_loc
   258   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;
   259     inject alpha_inj_loc distinct alpha_cases alpha_eqvt_loc lthy6;
   253   val alpha_equivp = ProofContext.export lthy6 lthy2 alpha_equivp_loc;
   260   val alpha_equivp = ProofContext.export lthy6 lthy2 alpha_equivp_loc;
   254   val qty_names = map (fn (_, b, _, _) => b) dts;
   261   val qty_binds = map (fn (_, b, _, _) => b) dts;
       
   262   val qty_names = map Name.of_binding qty_binds;
       
   263   val qty_full_names = map (Long_Name.qualify thy_name) qty_names
   255   val lthy7 = define_quotient_type
   264   val lthy7 = define_quotient_type
   256     (map (fn ((b, t), alpha) => (([], b, NoSyn), (t, alpha))) ((qty_names ~~ all_typs) ~~ alpha_ts))
   265     (map (fn ((b, t), alpha) => (([], b, NoSyn), (t, alpha))) ((qty_binds ~~ all_typs) ~~ alpha_ts))
   257     (ALLGOALS (resolve_tac alpha_equivp)) lthy6;
   266     (ALLGOALS (resolve_tac alpha_equivp)) lthy6;
   258 in
   267   val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
   259   ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy7)
   268   val raw_consts =
   260 end
   269     flat (map (fn (i, (_, _, l)) =>
   261 *}
   270       map (fn (cname, dts) =>
       
   271         Const (cname, map (typ_of_dtyp descr sorts) dts --->
       
   272           typ_of_dtyp descr sorts (DtRec i))) l) descr);
       
   273   val (consts_defs, lthy8) = fold_map Quotient_Def.quotient_lift_const (const_names ~~ raw_consts) lthy7;
       
   274   val (consts, const_defs) = split_list consts_defs;
       
   275   val (bns_rsp_pre, lthy9) = fold_map (
       
   276     fn (bn_t, i) => prove_const_rsp Binding.empty [bn_t]
       
   277       (fn _ => fvbv_rsp_tac (nth alpha_inducts i) raw_bn_eqs 1)) bns lthy8;
       
   278   val bns_rsp = flat (map snd bns_rsp_pre);
       
   279   val ((_, fv_rsp), lthy10) = prove_const_rsp Binding.empty fv_ts
       
   280     (fn _ => fvbv_rsp_tac alpha_induct fv_def 1) lthy9;
       
   281   val (const_rsps, lthy11) = fold_map (fn cnst => prove_const_rsp Binding.empty [cnst]
       
   282     (fn _ => constr_rsp_tac alpha_inj (fv_rsp @ bns_rsp) alpha_equivp 1)) raw_consts lthy10
       
   283   val (perms_rsp, lthy12) = prove_const_rsp Binding.empty perms
       
   284     (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy11;
       
   285   val qfv_names = map (fn x => "fv_" ^ x) qty_names
       
   286   val (qfv_defs, lthy12a) = fold_map Quotient_Def.quotient_lift_const (qfv_names ~~ fv_ts) lthy12;
       
   287   val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
       
   288   val (qbn_defs, lthy12b) = fold_map Quotient_Def.quotient_lift_const (qbn_names ~~ raw_bn_funs) lthy12a;
       
   289   val thy = Local_Theory.exit_global lthy12b;
       
   290   val perm_names = map (fn x => "permute_" ^ x) qty_names
       
   291   val thy' = define_lifted_perms qty_full_names (perm_names ~~ perms) raw_perm_simps thy;
       
   292   val lthy13 = Theory_Target.init NONE thy';
       
   293   val q_name = space_implode "_" qty_names;
       
   294   val q_induct = snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy13, induct));
       
   295   val (_, lthy14) = Local_Theory.note ((Binding.name (q_name ^ "_induct"), []), [q_induct]) lthy13;
       
   296   val q_perm = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy14, th))) raw_perm_def;
       
   297   val (_, lthy15) = Local_Theory.note ((Binding.name (q_name ^ "_perm"), []), q_perm) lthy14;
       
   298   val q_fv = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy15, th))) fv_def;
       
   299   val (_, lthy16) = Local_Theory.note ((Binding.name (q_name ^ "_fv"), []), q_fv) lthy15;
       
   300   val q_bn = map (fn th => snd (Quotient_Tacs.lifted_attrib (Context.Proof lthy16, th))) raw_bn_eqs;
       
   301   val (_, lthy17) = Local_Theory.note ((Binding.name (q_name ^ "_bn"), []), q_bn) lthy16;
       
   302 in
       
   303   ((raw_dt_names, raw_bn_funs, raw_bn_eqs, raw_binds), lthy17)
       
   304 end
       
   305 *}
       
   306 
       
   307 ML name_of_typ
   262 
   308 
   263 ML {* 
   309 ML {* 
   264 (* parsing the datatypes and declaring *)
   310 (* parsing the datatypes and declaring *)
   265 (* constructors in the local theory    *)
   311 (* constructors in the local theory    *)
   266 fun prepare_dts dt_strs lthy = 
   312 fun prepare_dts dt_strs lthy =