Nominal/Parser.thy
changeset 1688 0b2535a72fd0
parent 1685 721d92623c9d
child 1744 00680cea0dde
equal deleted inserted replaced
1687:51bc795b81fd 1688:0b2535a72fd0
   155 fun find [] _ = error ("cannot find element")
   155 fun find [] _ = error ("cannot find element")
   156   | find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y
   156   | find ((x, z)::xs) y = if (Long_Name.base_name x) = y then z else find xs y
   157 *}
   157 *}
   158 
   158 
   159 ML {*
   159 ML {*
   160 fun strip_union t =
   160 fun strip_bn_fun t =
   161   case t of
   161   case t of
   162     Const (@{const_name sup}, _) $ l $ r => strip_union l @ strip_union r
   162     Const (@{const_name sup}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   163   | (h as (Const (@{const_name insert}, T))) $ x $ y =>
   163   | Const (@{const_name append}, _) $ l $ r => strip_bn_fun l @ strip_bn_fun r
   164      (h $ x $ Const (@{const_name bot}, range_type (range_type T))) :: strip_union y
   164   | Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
       
   165       (i, NONE) :: strip_bn_fun y
       
   166   | Const (@{const_name Cons}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ y =>
       
   167       (i, NONE) :: strip_bn_fun y
   165   | Const (@{const_name bot}, _) => []
   168   | Const (@{const_name bot}, _) => []
   166   | _ => [t]
   169   | Const (@{const_name Nil}, _) => []
   167 *}
   170   | (f as Free _) $ Bound i => [(i, SOME f)]
   168 
   171   | _ => error ("Unsupported binding function: " ^ (PolyML.makestring t))
   169 ML {*
       
   170 fun bn_or_atom t =
       
   171   case t of
       
   172     Const (@{const_name insert}, _) $ (Const (@{const_name atom}, _) $ Bound i) $ 
       
   173       Const (@{const_name bot}, _) => (i, NONE)
       
   174   | f $ Bound i => (i, SOME f)
       
   175   | _ => error "Unsupported binding function"
       
   176 *}
   172 *}
   177 
   173 
   178 ML {*
   174 ML {*
   179 fun prep_bn dt_names dts eqs = 
   175 fun prep_bn dt_names dts eqs = 
   180 let
   176 let
   187     val (bn_fun, [cnstr]) = strip_comb lhs
   183     val (bn_fun, [cnstr]) = strip_comb lhs
   188     val (_, ty) = dest_Free bn_fun
   184     val (_, ty) = dest_Free bn_fun
   189     val (ty_name, _) = dest_Type (domain_type ty)
   185     val (ty_name, _) = dest_Type (domain_type ty)
   190     val dt_index = find_index (fn x => x = ty_name) dt_names
   186     val dt_index = find_index (fn x => x = ty_name) dt_names
   191     val (cnstr_head, cnstr_args) = strip_comb cnstr
   187     val (cnstr_head, cnstr_args) = strip_comb cnstr
   192     val rhs_elements = map bn_or_atom (strip_union rhs)
   188     val rhs_elements = strip_bn_fun rhs
   193     val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements
   189     val included = map (apfst (fn i => length (cnstr_args) - i - 1)) rhs_elements
   194   in
   190   in
   195     (dt_index, (bn_fun, (cnstr_head, included)))
   191     (dt_index, (bn_fun, (cnstr_head, included)))
   196   end 
   192   end 
   197   fun order dts i ts = 
   193   fun order dts i ts = 
   355   val rel_dtinfos = List.take (dtinfos, (length dts));
   351   val rel_dtinfos = List.take (dtinfos, (length dts));
   356   val inject = flat (map #inject dtinfos);
   352   val inject = flat (map #inject dtinfos);
   357   val distincts = flat (map #distinct dtinfos);
   353   val distincts = flat (map #distinct dtinfos);
   358   val rel_distinct = map #distinct rel_dtinfos;
   354   val rel_distinct = map #distinct rel_dtinfos;
   359   val induct = #induct dtinfo;
   355   val induct = #induct dtinfo;
   360   val inducts = #inducts dtinfo;
       
   361   val exhausts = map #exhaust dtinfos;
   356   val exhausts = map #exhaust dtinfos;
   362   val _ = tracing "Defining permutations, fv and alpha";
   357   val _ = tracing "Defining permutations, fv and alpha";
   363   val ((raw_perm_def, raw_perm_simps, perms), lthy3) =
   358   val ((raw_perm_def, raw_perm_simps, perms), lthy3) =
   364     Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2;
   359     Local_Theory.theory_result (define_raw_perms dtinfo (length dts)) lthy2;
   365   val raw_binds_flat = map (map flat) raw_binds;
   360   val raw_binds_flat = map (map flat) raw_binds;
   393     else build_equivps alpha_ts reflps alpha_induct
   388     else build_equivps alpha_ts reflps alpha_induct
   394       inject alpha_eq_iff distincts alpha_cases alpha_eqvt lthy6;
   389       inject alpha_eq_iff distincts alpha_cases alpha_eqvt lthy6;
   395   val qty_binds = map (fn (_, b, _, _) => b) dts;
   390   val qty_binds = map (fn (_, b, _, _) => b) dts;
   396   val qty_names = map Name.of_binding qty_binds;
   391   val qty_names = map Name.of_binding qty_binds;
   397   val qty_full_names = map (Long_Name.qualify thy_name) qty_names
   392   val qty_full_names = map (Long_Name.qualify thy_name) qty_names
   398   val lthy7 = define_quotient_type
   393   val (qtys, lthy7) = define_quotient_types qty_binds all_typs alpha_ts_nobn alpha_equivp lthy6;
   399     (map (fn ((b, t), alpha) => (([], b, NoSyn), (t, alpha))) ((qty_binds ~~ all_typs) ~~ alpha_ts_nobn))
       
   400     (ALLGOALS (resolve_tac alpha_equivp)) lthy6;
       
   401   val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
   394   val const_names = map Name.of_binding (flat (map (fn (_, _, _, t) => map (fn (b, _, _) => b) t) dts));
   402   val raw_consts =
   395   val raw_consts =
   403     flat (map (fn (i, (_, _, l)) =>
   396     flat (map (fn (i, (_, _, l)) =>
   404       map (fn (cname, dts) =>
   397       map (fn (cname, dts) =>
   405         Const (cname, map (typ_of_dtyp descr sorts) dts --->
   398         Const (cname, map (typ_of_dtyp descr sorts) dts --->
   406           typ_of_dtyp descr sorts (DtRec i))) l) descr);
   399           typ_of_dtyp descr sorts (DtRec i))) l) descr);
   407   val (consts, const_defs, lthy8) = quotient_lift_consts_export (const_names ~~ raw_consts) lthy7;
   400   val (consts, const_defs, lthy8) = quotient_lift_consts_export qtys (const_names ~~ raw_consts) lthy7;
   408   (* Could be done nicer *)
       
   409   val q_tys = distinct (op =) (map (snd o strip_type o fastype_of) consts);
       
   410   val _ = tracing "Proving respects";
   401   val _ = tracing "Proving respects";
   411   val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8;
   402   val bns_rsp_pre' = build_fvbv_rsps alpha_ts alpha_induct raw_bn_eqs (map fst bns) lthy8;
       
   403   val _ = map tracing (map PolyML.makestring bns_rsp_pre')
   412   val (bns_rsp_pre, lthy9) = fold_map (
   404   val (bns_rsp_pre, lthy9) = fold_map (
   413     fn (bn_t, i) => prove_const_rsp Binding.empty [bn_t] (fn _ =>
   405     fn (bn_t, i) => prove_const_rsp qtys Binding.empty [bn_t] (fn _ =>
   414        resolve_tac bns_rsp_pre' 1)) bns lthy8;
   406        resolve_tac bns_rsp_pre' 1)) bns lthy8;
   415   val bns_rsp = flat (map snd bns_rsp_pre);
   407   val bns_rsp = flat (map snd bns_rsp_pre);
   416   fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
   408   fun fv_rsp_tac _ = if !cheat_fv_rsp then Skip_Proof.cheat_tac thy
   417     else fvbv_rsp_tac alpha_induct fv_def lthy8 1;
   409     else fvbv_rsp_tac alpha_induct fv_def lthy8 1;
   418   val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
   410   val fv_rsps = prove_fv_rsp fv_alpha_all alpha_ts fv_rsp_tac lthy9;
   419   val (fv_rsp_pre, lthy10) = fold_map
   411   val (fv_rsp_pre, lthy10) = fold_map
   420     (fn fv => fn ctxt => prove_const_rsp Binding.empty [fv]
   412     (fn fv => fn ctxt => prove_const_rsp qtys Binding.empty [fv]
   421     (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) fv_ts lthy9;
   413     (fn _ => asm_simp_tac (HOL_ss addsimps fv_rsps) 1) ctxt) fv_ts lthy9;
   422   val fv_rsp = flat (map snd fv_rsp_pre);
   414   val fv_rsp = flat (map snd fv_rsp_pre);
   423   val (perms_rsp, lthy11) = prove_const_rsp Binding.empty perms
   415   val (perms_rsp, lthy11) = prove_const_rsp qtys Binding.empty perms
   424     (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
   416     (fn _ => asm_simp_tac (HOL_ss addsimps alpha_eqvt) 1) lthy10;
   425   val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_ts alpha_induct (alpha_eq_iff @ rel_dists @ rel_dists_bn) alpha_equivp exhausts alpha_ts_bn lthy11;
   417   val alpha_bn_rsp_pre = prove_alpha_bn_rsp alpha_ts alpha_induct (alpha_eq_iff @ rel_dists @ rel_dists_bn) alpha_equivp exhausts alpha_ts_bn lthy11;
   426   val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp Binding.empty [cnst]
   418   val (alpha_bn_rsps, lthy11a) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   427         (fn _ => asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1)) alpha_ts_bn lthy11
   419         (fn _ => asm_simp_tac (HOL_ss addsimps alpha_bn_rsp_pre) 1)) alpha_ts_bn lthy11
   428 (*  val _ = map tracing (map PolyML.makestring alpha_bn_rsps);*)
   420 (*  val _ = map tracing (map PolyML.makestring alpha_bn_rsps);*)
   429   fun const_rsp_tac _ =
   421   fun const_rsp_tac _ =
   430     if !cheat_const_rsp then Skip_Proof.cheat_tac thy
   422     if !cheat_const_rsp then Skip_Proof.cheat_tac thy
   431     else let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff alpha_ts_bn lthy11a
   423     else let val alpha_alphabn = prove_alpha_alphabn alpha_ts alpha_induct alpha_eq_iff alpha_ts_bn lthy11a
   432       in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end
   424       in constr_rsp_tac alpha_eq_iff (fv_rsp @ bns_rsp @ reflps @ alpha_alphabn) 1 end
   433   val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp Binding.empty [cnst]
   425   val (const_rsps, lthy12) = fold_map (fn cnst => prove_const_rsp qtys Binding.empty [cnst]
   434     const_rsp_tac) raw_consts lthy11a
   426     const_rsp_tac) raw_consts lthy11a
   435   val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) ordered_fv_ts
   427   val qfv_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) ordered_fv_ts
   436   val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export (qfv_names ~~ ordered_fv_ts) lthy12;
   428   val (qfv_ts, qfv_defs, lthy12a) = quotient_lift_consts_export qtys (qfv_names ~~ ordered_fv_ts) lthy12;
   437   val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts;
   429   val (qfv_ts_nobn, qfv_ts_bn) = chop (length perms) qfv_ts;
   438   val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
   430   val qbn_names = map (fn (b, _ , _) => Name.of_binding b) bn_funs
   439   val (qbn_defs, lthy12b) = fold_map Quotient_Def.quotient_lift_const (qbn_names ~~ raw_bn_funs) lthy12a;
   431   val (qbn_ts, qbn_defs, lthy12b) = quotient_lift_consts_export qtys (qbn_names ~~ raw_bn_funs) lthy12a;
   440   val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_ts_bn
   432   val qalpha_bn_names = map (unsuffix "_raw" o Long_Name.base_name o fst o dest_Const) alpha_ts_bn
   441   val (qalpha_ts_bn, qbn_defs, lthy12c) = quotient_lift_consts_export (qalpha_bn_names ~~ alpha_ts_bn) lthy12b;
   433   val (qalpha_ts_bn, qalphabn_defs, lthy12c) = quotient_lift_consts_export qtys (qalpha_bn_names ~~ alpha_ts_bn) lthy12b;
   442   val _ = tracing "Lifting permutations";
   434   val _ = tracing "Lifting permutations";
   443   val thy = Local_Theory.exit_global lthy12c;
   435   val thy = Local_Theory.exit_global lthy12c;
   444   val perm_names = map (fn x => "permute_" ^ x) qty_names
   436   val perm_names = map (fn x => "permute_" ^ x) qty_names
   445   val thy' = define_lifted_perms qty_full_names (perm_names ~~ perms) raw_perm_simps thy;
   437   val thy' = define_lifted_perms qtys qty_full_names (perm_names ~~ perms) raw_perm_simps thy;
   446   val lthy13 = Theory_Target.init NONE thy';
   438   val lthy13 = Theory_Target.init NONE thy';
   447   val q_name = space_implode "_" qty_names;
   439   val q_name = space_implode "_" qty_names;
   448   fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
   440   fun suffix_bind s = Binding.qualify true q_name (Binding.name s);
   449   val _ = tracing "Lifting induction";
   441   val _ = tracing "Lifting induction";
   450   val constr_names = map (Long_Name.base_name o fst o dest_Const) consts;
   442   val constr_names = map (Long_Name.base_name o fst o dest_Const) consts;
   451   val q_induct = Rule_Cases.name constr_names (lift_thm lthy13 induct);
   443   val q_induct = Rule_Cases.name constr_names (lift_thm qtys lthy13 induct);
   452   fun note_suffix s th ctxt =
   444   fun note_suffix s th ctxt =
   453     snd (Local_Theory.note ((suffix_bind s, []), th) ctxt);
   445     snd (Local_Theory.note ((suffix_bind s, []), th) ctxt);
   454   fun note_simp_suffix s th ctxt =
   446   fun note_simp_suffix s th ctxt =
   455     snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt);
   447     snd (Local_Theory.note ((suffix_bind s, [Attrib.internal (K Simplifier.simp_add)]), th) ctxt);
   456   val (_, lthy14) = Local_Theory.note ((suffix_bind "induct",
   448   val (_, lthy14) = Local_Theory.note ((suffix_bind "induct",
   457     [Attrib.internal (K (Rule_Cases.case_names constr_names))]), [Rule_Cases.name constr_names q_induct]) lthy13;
   449     [Attrib.internal (K (Rule_Cases.case_names constr_names))]), [Rule_Cases.name constr_names q_induct]) lthy13;
   458   val q_inducts = Project_Rule.projects lthy13 (1 upto (length alpha_inducts)) q_induct
   450   val q_inducts = Project_Rule.projects lthy13 (1 upto (length alpha_inducts)) q_induct
   459   val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
   451   val (_, lthy14a) = Local_Theory.note ((suffix_bind "inducts", []), q_inducts) lthy14;
   460   val q_perm = map (lift_thm lthy14) raw_perm_def;
   452   val q_perm = map (lift_thm qtys lthy14) raw_perm_def;
   461   val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
   453   val lthy15 = note_simp_suffix "perm" q_perm lthy14a;
   462   val q_fv = map (lift_thm lthy15) fv_def;
   454   val q_fv = map (lift_thm qtys lthy15) fv_def;
   463   val lthy16 = note_simp_suffix "fv" q_fv lthy15;
   455   val lthy16 = note_simp_suffix "fv" q_fv lthy15;
   464   val q_bn = map (lift_thm lthy16) raw_bn_eqs;
   456   val q_bn = map (lift_thm qtys lthy16) raw_bn_eqs;
   465   val lthy17 = note_simp_suffix "bn" q_bn lthy16;
   457   val lthy17 = note_simp_suffix "bn" q_bn lthy16;
   466   val _ = tracing "Lifting eq-iff";
   458   val _ = tracing "Lifting eq-iff";
   467   val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) alpha_eq_iff
   459   val eq_iff_unfolded1 = map (Local_Defs.unfold lthy17 @{thms alphas2}) alpha_eq_iff
   468   val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas}) eq_iff_unfolded1
   460   val eq_iff_unfolded2 = map (Local_Defs.unfold lthy17 @{thms alphas}) eq_iff_unfolded1
   469   val q_eq_iff_pre1 = map (lift_thm lthy17) eq_iff_unfolded2;
   461   val q_eq_iff_pre1 = map (lift_thm qtys lthy17) eq_iff_unfolded2;
   470   val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1
   462   val q_eq_iff_pre2 = map (Local_Defs.fold lthy17 @{thms alphas2}) q_eq_iff_pre1
   471   val q_eq_iff = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre2
   463   val q_eq_iff = map (Local_Defs.fold lthy17 @{thms alphas}) q_eq_iff_pre2
   472   val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
   464   val (_, lthy18) = Local_Theory.note ((suffix_bind "eq_iff", []), q_eq_iff) lthy17;
   473   val q_dis = map (lift_thm lthy18) rel_dists;
   465   val q_dis = map (lift_thm qtys lthy18) rel_dists;
   474   val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
   466   val lthy19 = note_simp_suffix "distinct" q_dis lthy18;
   475   val q_eqvt = map (lift_thm lthy19) (bv_eqvt @ fv_eqvt);
   467   val q_eqvt = map (lift_thm qtys lthy19) (bv_eqvt @ fv_eqvt);
   476   val (_, lthy20) = Local_Theory.note ((Binding.empty,
   468   val (_, lthy20) = Local_Theory.note ((Binding.empty,
   477     [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19;
   469     [Attrib.internal (fn _ => Nominal_ThmDecls.eqvt_add)]), q_eqvt) lthy19;
   478   val _ = tracing "Finite Support";
   470   val _ = tracing "Finite Support";
   479   val supports = map (prove_supports lthy20 q_perm) consts;
   471   val supports = map (prove_supports lthy20 q_perm) consts;
   480   val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports q_tys);
   472   val fin_supp = HOLogic.conj_elims (prove_fs lthy20 q_induct supports qtys);
   481   val thy3 = Local_Theory.exit_global lthy20;
   473   val thy3 = Local_Theory.exit_global lthy20;
   482   val lthy21 = Theory_Target.instantiation (qty_full_names, [], @{sort fs}) thy3;
   474   val lthy21 = Theory_Target.instantiation (qty_full_names, [], @{sort fs}) thy3;
   483   fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
   475   fun tac _ = Class.intro_classes_tac [] THEN (ALLGOALS (resolve_tac fin_supp))
   484   val lthy22 = Class.prove_instantiation_instance tac lthy21
   476   val lthy22 = Class.prove_instantiation_instance tac lthy21
   485   val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
   477   val fv_alpha_all = combine_fv_alpha_bns (qfv_ts_nobn, qfv_ts_bn) (alpha_ts_nobn, qalpha_ts_bn) bn_nos;
   600   in
   592   in
   601     map_index (prep_typ binds) annos
   593     map_index (prep_typ binds) annos
   602   end
   594   end
   603 
   595 
   604 in
   596 in
   605   map (map (map (map (fn (a, b, c) => (a, b, c, !alpha_type)))))
   597   map (map (map (map (fn (a, b, c) => (a, b, c, if !alpha_type=AlphaLst andalso a = NONE then AlphaGen else !alpha_type)))))
   606   (map (map prep_binds) (extract_annos_binds (get_cnstrs dt_strs)))
   598   (map (map prep_binds) (extract_annos_binds (get_cnstrs dt_strs)))
   607 end
   599 end
   608 *}
   600 *}
   609 
   601 
   610 ML {*
   602 ML {*