src/HOL/Tools/BNF/bnf_fp_n2m.ML
author desharna
Thu Sep 25 16:35:53 2014 +0200 (2014-09-25)
changeset 58446 e89f57d1e46c
parent 58375 7b92932ffea5
child 58448 a1d4e7473c98
permissions -rw-r--r--
generate 'rec_transfer' for datatypes
     1 (*  Title:      HOL/Tools/BNF/bnf_fp_n2m.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Copyright   2013
     4 
     5 Flattening of nested to mutual (co)recursion.
     6 *)
     7 
     8 signature BNF_FP_N2M =
     9 sig
    10   val construct_mutualized_fp: BNF_Util.fp_kind -> int list -> typ list ->
    11     BNF_FP_Def_Sugar.fp_sugar list -> binding list -> (string * sort) list ->
    12     typ list * typ list list -> BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory ->
    13     BNF_FP_Util.fp_result * local_theory
    14 end;
    15 
    16 structure BNF_FP_N2M : BNF_FP_N2M =
    17 struct
    18 
    19 open BNF_Def
    20 open BNF_Util
    21 open BNF_Comp
    22 open BNF_FP_Util
    23 open BNF_FP_Def_Sugar
    24 open BNF_Tactics
    25 open BNF_FP_N2M_Tactics
    26 
    27 fun force_typ ctxt T =
    28   Term.map_types Type_Infer.paramify_vars
    29   #> Type.constraint T
    30   #> Syntax.check_term ctxt
    31   #> singleton (Variable.polymorphic ctxt);
    32 
    33 fun mk_prod_map f g =
    34   let
    35     val ((fAT, fBT), fT) = `dest_funT (fastype_of f);
    36     val ((gAT, gBT), gT) = `dest_funT (fastype_of g);
    37   in
    38     Const (@{const_name map_prod},
    39       fT --> gT --> HOLogic.mk_prodT (fAT, gAT) --> HOLogic.mk_prodT (fBT, gBT)) $ f $ g
    40   end;
    41 
    42 fun mk_map_sum f g =
    43   let
    44     val ((fAT, fBT), fT) = `dest_funT (fastype_of f);
    45     val ((gAT, gBT), gT) = `dest_funT (fastype_of g);
    46   in
    47     Const (@{const_name map_sum}, fT --> gT --> mk_sumT (fAT, gAT) --> mk_sumT (fBT, gBT)) $ f $ g
    48   end;
    49 
    50 fun construct_mutualized_fp fp mutual_cliques fpTs (fp_sugars : fp_sugar list) bs resBs (resDs, Dss)
    51     bnfs (absT_infos : absT_info list) lthy =
    52   let
    53     fun of_fp_res get =
    54       map (fn {fp_res, fp_res_index, ...} => nth (get fp_res) fp_res_index) fp_sugars;
    55 
    56     fun mk_co_algT T U = case_fp fp (T --> U) (U --> T);
    57     fun co_swap pair = case_fp fp I swap pair;
    58     val mk_co_comp = HOLogic.mk_comp o co_swap;
    59     val co_productC = BNF_FP_Rec_Sugar_Util.case_fp fp @{type_name prod} @{type_name sum};
    60 
    61     val dest_co_algT = co_swap o dest_funT;
    62     val co_alg_argT = case_fp fp range_type domain_type;
    63     val co_alg_funT = case_fp fp domain_type range_type;
    64     val mk_co_product = curry (case_fp fp mk_convol mk_case_sum);
    65     val mk_map_co_product = case_fp fp mk_prod_map mk_map_sum;
    66     val co_proj1_const = case_fp fp (fst_const o fst) (uncurry Inl_const o dest_sumT o snd);
    67     val mk_co_productT = curry (case_fp fp HOLogic.mk_prodT mk_sumT);
    68     val dest_co_productT = case_fp fp HOLogic.dest_prodT dest_sumT;
    69     val rewrite_comp_comp = case_fp fp @{thm rewriteL_comp_comp} @{thm rewriteR_comp_comp};
    70 
    71     val fp_absT_infos = map #absT_info fp_sugars;
    72     val fp_bnfs = of_fp_res #bnfs;
    73     val pre_bnfs = map #pre_bnf fp_sugars;
    74     val nesting_bnfss =
    75       map (fn sugar => #fp_nesting_bnfs sugar @ #live_nesting_bnfs sugar) fp_sugars;
    76     val fp_or_nesting_bnfss = fp_bnfs :: nesting_bnfss;
    77     val fp_or_nesting_bnfs = distinct (op = o pairself T_of_bnf) (flat fp_or_nesting_bnfss);
    78 
    79     val fp_absTs = map #absT fp_absT_infos;
    80     val fp_repTs = map #repT fp_absT_infos;
    81     val fp_abss = map #abs fp_absT_infos;
    82     val fp_reps = map #rep fp_absT_infos;
    83     val fp_type_definitions = map #type_definition fp_absT_infos;
    84 
    85     val absTs = map #absT absT_infos;
    86     val repTs = map #repT absT_infos;
    87     val absTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) absTs;
    88     val repTs' = map (Logic.type_map (singleton (Variable.polymorphic lthy))) repTs;
    89     val abss = map #abs absT_infos;
    90     val reps = map #rep absT_infos;
    91     val abs_inverses = map #abs_inverse absT_infos;
    92     val type_definitions = map #type_definition absT_infos;
    93 
    94     val n = length bnfs;
    95     val deads = fold (union (op =)) Dss resDs;
    96     val As = subtract (op =) deads (map TFree resBs);
    97     val names_lthy = fold Variable.declare_typ (As @ deads) lthy;
    98     val m = length As;
    99     val live = m + n;
   100 
   101     val ((Xs, Bs), names_lthy) = names_lthy
   102       |> mk_TFrees n
   103       ||>> mk_TFrees m;
   104 
   105     val allAs = As @ Xs;
   106     val allBs = Bs @ Xs;
   107     val phiTs = map2 mk_pred2T As Bs;
   108     val thetaBs = As ~~ Bs;
   109     val fpTs' = map (Term.typ_subst_atomic thetaBs) fpTs;
   110     val fold_thetaAs = Xs ~~ fpTs;
   111     val fold_thetaBs = Xs ~~ fpTs';
   112     val rec_theta = Xs ~~ map2 mk_co_productT fpTs Xs;
   113     val pre_phiTs = map2 mk_pred2T fpTs fpTs';
   114 
   115     val ((ctors, dtors), (xtor's, xtors)) =
   116       let
   117         val ctors = map2 (force_typ names_lthy o (fn T => dummyT --> T)) fpTs (of_fp_res #ctors);
   118         val dtors = map2 (force_typ names_lthy o (fn T => T --> dummyT)) fpTs (of_fp_res #dtors);
   119       in
   120         ((ctors, dtors), `(map (Term.subst_atomic_types thetaBs)) (case_fp fp ctors dtors))
   121       end;
   122 
   123     val absATs = map (domain_type o fastype_of) ctors;
   124     val absBTs = map (Term.typ_subst_atomic thetaBs) absATs;
   125     val xTs = map (domain_type o fastype_of) xtors;
   126     val yTs = map (domain_type o fastype_of) xtor's;
   127 
   128     val absAs = map3 (fn Ds => mk_abs o mk_T_of_bnf Ds allAs) Dss bnfs abss;
   129     val absBs = map3 (fn Ds => mk_abs o mk_T_of_bnf Ds allBs) Dss bnfs abss;
   130     val fp_repAs = map2 mk_rep absATs fp_reps;
   131     val fp_repBs = map2 mk_rep absBTs fp_reps;
   132 
   133     val (((((phis, phis'), pre_phis), xs), ys), names_lthy) = names_lthy
   134       |> mk_Frees' "R" phiTs
   135       ||>> mk_Frees "S" pre_phiTs
   136       ||>> mk_Frees "x" xTs
   137       ||>> mk_Frees "y" yTs;
   138 
   139     val rels =
   140       let
   141         fun find_rel T As Bs = fp_or_nesting_bnfss
   142           |> map (filter_out (curry (op = o pairself name_of_bnf) BNF_Comp.DEADID_bnf))
   143           |> get_first (find_first (fn bnf => Type.could_unify (T_of_bnf bnf, T)))
   144           |> Option.map (fn bnf =>
   145             let val live = live_of_bnf bnf;
   146             in (mk_rel live As Bs (rel_of_bnf bnf), live) end)
   147           |> the_default (HOLogic.eq_const T, 0);
   148 
   149         fun mk_rel (T as Type (_, Ts)) (Type (_, Us)) =
   150               let
   151                 val (rel, live) = find_rel T Ts Us;
   152                 val (Ts', Us') = fastype_of rel |> strip_typeN live |> fst |> map_split dest_pred2T;
   153                 val rels = map2 mk_rel Ts' Us';
   154               in
   155                 Term.list_comb (rel, rels)
   156               end
   157           | mk_rel (T as TFree _) _ = (nth phis (find_index (curry op = T) As)
   158               handle General.Subscript => HOLogic.eq_const T)
   159           | mk_rel _ _ = raise Fail "fpTs contains schematic type variables";
   160       in
   161         map2 (fold_rev Term.absfree phis' oo mk_rel) fpTs fpTs'
   162       end;
   163 
   164     val pre_rels = map2 (fn Ds => mk_rel_of_bnf Ds (As @ fpTs) (Bs @ fpTs')) Dss bnfs;
   165 
   166     val rel_unfolds = maps (no_refl o single o rel_def_of_bnf) pre_bnfs;
   167     val rel_xtor_co_inducts = of_fp_res (split_conj_thm o #rel_xtor_co_induct_thm)
   168       |> map (unfold_thms lthy (id_apply :: rel_unfolds));
   169 
   170     val rel_defs = map rel_def_of_bnf bnfs;
   171     val rel_monos = map rel_mono_of_bnf bnfs;
   172 
   173     fun cast castA castB pre_rel =
   174       let
   175         val castAB = mk_vimage2p (Term.subst_atomic_types fold_thetaAs castA)
   176           (Term.subst_atomic_types fold_thetaBs castB);
   177       in
   178         fold_rev (fold_rev Term.absdummy) [phiTs, pre_phiTs]
   179           (castAB $ Term.list_comb (pre_rel, map Bound (live - 1 downto 0)))
   180       end;
   181 
   182     val castAs = map2 (curry HOLogic.mk_comp) absAs fp_repAs;
   183     val castBs = map2 (curry HOLogic.mk_comp) absBs fp_repBs;
   184 
   185     val fp_or_nesting_rel_eqs = no_refl (map rel_eq_of_bnf fp_or_nesting_bnfs);
   186     val fp_or_nesting_rel_monos = map rel_mono_of_bnf fp_or_nesting_bnfs;
   187 
   188     val rel_xtor_co_induct_thm =
   189       mk_rel_xtor_co_induct_thm fp (map3 cast castAs castBs pre_rels) pre_phis rels phis xs ys
   190         xtors xtor's (mk_rel_xtor_co_induct_tactic fp abs_inverses rel_xtor_co_inducts rel_defs
   191           rel_monos fp_or_nesting_rel_eqs fp_or_nesting_rel_monos)
   192         lthy;
   193 
   194     val map_id0s = no_refl (map map_id0_of_bnf bnfs);
   195 
   196     val xtor_co_induct_thm =
   197       (case fp of
   198         Least_FP =>
   199           let
   200             val (Ps, names_lthy) = names_lthy
   201               |> mk_Frees "P" (map (fn T => T --> HOLogic.boolT) fpTs);
   202             fun mk_Grp_id P =
   203               let val T = domain_type (fastype_of P);
   204               in mk_Grp (HOLogic.Collect_const T $ P) (HOLogic.id_const T) end;
   205             val cts = map (SOME o certify lthy) (map HOLogic.eq_const As @ map mk_Grp_id Ps);
   206             fun mk_fp_type_copy_thms thm = map (curry op RS thm)
   207               @{thms type_copy_Abs_o_Rep type_copy_vimage2p_Grp_Rep};
   208             fun mk_type_copy_thms thm = map (curry op RS thm)
   209               @{thms type_copy_Rep_o_Abs type_copy_vimage2p_Grp_Abs};
   210           in
   211             cterm_instantiate_pos cts rel_xtor_co_induct_thm
   212             |> singleton (Proof_Context.export names_lthy lthy)
   213             |> unfold_thms lthy (@{thms eq_le_Grp_id_iff all_simps(1,2)[symmetric]} @
   214                 fp_or_nesting_rel_eqs)
   215             |> funpow n (fn thm => thm RS spec)
   216             |> unfold_thms lthy (@{thm eq_alt} :: map rel_Grp_of_bnf bnfs @ map_id0s)
   217             |> unfold_thms lthy (@{thms vimage2p_id vimage2p_comp comp_apply comp_id
   218                Grp_id_mono_subst eqTrueI[OF subset_UNIV] simp_thms(22)} @
   219                maps mk_fp_type_copy_thms fp_type_definitions @
   220                maps mk_type_copy_thms type_definitions)
   221             |> unfold_thms lthy @{thms subset_iff mem_Collect_eq
   222                atomize_conjL[symmetric] atomize_all[symmetric] atomize_imp[symmetric]}
   223           end
   224       | Greatest_FP =>
   225           let
   226             val cts = NONE :: map (SOME o certify lthy) (map HOLogic.eq_const As);
   227           in
   228             cterm_instantiate_pos cts rel_xtor_co_induct_thm
   229             |> unfold_thms lthy (@{thms le_fun_def le_bool_def all_simps(1,2)[symmetric]} @
   230                 fp_or_nesting_rel_eqs)
   231             |> funpow (2 * n) (fn thm => thm RS spec)
   232             |> Conv.fconv_rule (Object_Logic.atomize lthy)
   233             |> funpow n (fn thm => thm RS mp)
   234           end);
   235 
   236     val fold_preTs = map2 (fn Ds => mk_T_of_bnf Ds allAs) Dss bnfs;
   237     val rec_preTs = map (Term.typ_subst_atomic rec_theta) fold_preTs;
   238 
   239     val rec_strTs = map2 mk_co_algT rec_preTs Xs;
   240     val resTs = map2 mk_co_algT fpTs Xs;
   241 
   242     val ((rec_strs, rec_strs'), names_lthy) = names_lthy
   243       |> mk_Frees' "s" rec_strTs;
   244 
   245     val co_recs = of_fp_res #xtor_co_recs;
   246     val ns = map (length o #Ts o #fp_res) fp_sugars;
   247 
   248     val typ_subst_nonatomic_sorted = fold_rev (typ_subst_nonatomic o single);
   249 
   250     fun foldT_of_recT recT =
   251       let
   252         val ((FTXs, Ys), TX) = strip_fun_type recT |>> map_split dest_co_algT;
   253         val Zs = union op = Xs Ys;
   254         fun subst (Type (C, Ts as [_, X])) =
   255             if C = co_productC andalso member op = Zs X then X else Type (C, map subst Ts)
   256           | subst (Type (C, Ts)) = Type (C, map subst Ts)
   257           | subst T = T;
   258       in
   259         map2 (mk_co_algT o subst) FTXs Ys ---> TX
   260       end;
   261 
   262     fun force_rec i TU raw_rec =
   263       let
   264         val thy = Proof_Context.theory_of lthy;
   265 
   266         val approx_rec = raw_rec
   267           |> force_typ names_lthy (replicate (nth ns i) dummyT ---> TU);
   268         val subst = Term.typ_subst_atomic fold_thetaAs;
   269 
   270         fun mk_fp_absT_repT fp_repT fp_absT = mk_absT thy fp_repT fp_absT ooo mk_repT;
   271         val mk_fp_absT_repTs = map5 mk_fp_absT_repT fp_repTs fp_absTs absTs repTs;
   272 
   273         val fold_preTs' = mk_fp_absT_repTs (map subst fold_preTs);
   274 
   275         val fold_pre_deads_only_Ts =
   276           map (typ_subst_nonatomic_sorted (map (rpair dummyT)
   277             (As @ sort (int_ord o pairself Term.size_of_typ) fpTs))) fold_preTs';
   278 
   279         val (mutual_clique, TUs) =
   280           map_split dest_co_algT (binder_fun_types (foldT_of_recT (fastype_of approx_rec)))
   281           |>> map subst
   282           |> `(fn (_, Ys) =>
   283             nth mutual_cliques (find_index (fn X => X = the (find_first (can dest_TFree) Ys)) Xs))
   284           ||> uncurry (map2 mk_co_algT);
   285         val cands = mutual_cliques ~~ map2 mk_co_algT fold_preTs' Xs;
   286         val js = find_indices (fn ((cl, cand), TU) =>
   287           cl = mutual_clique andalso Type.could_unify (TU, cand)) TUs cands;
   288         val Tpats = map (fn j => mk_co_algT (nth fold_pre_deads_only_Ts j) (nth Xs j)) js;
   289       in
   290         force_typ names_lthy (Tpats ---> TU) raw_rec
   291       end;
   292 
   293     fun mk_co_comp_abs_rep fp_absT absT fp_abs fp_rep abs rep t =
   294       case_fp fp (HOLogic.mk_comp (HOLogic.mk_comp (t, mk_abs absT abs), mk_rep fp_absT fp_rep))
   295         (HOLogic.mk_comp (mk_abs fp_absT fp_abs, HOLogic.mk_comp (mk_rep absT rep, t)));
   296 
   297     fun mk_rec b_opt recs lthy TU =
   298       let
   299         val thy = Proof_Context.theory_of lthy;
   300 
   301         val x = co_alg_argT TU;
   302         val i = find_index (fn T => x = T) Xs;
   303         val TUrec =
   304           (case find_first (fn f => body_fun_type (fastype_of f) = TU) recs of
   305             NONE => force_rec i TU (nth co_recs i)
   306           | SOME f => f);
   307 
   308         val TUs = binder_fun_types (fastype_of TUrec);
   309 
   310         fun mk_s TU' =
   311           let
   312             fun mk_absT_fp_repT repT absT = mk_absT thy repT absT ooo mk_repT;
   313 
   314             val i = find_index (fn T => co_alg_argT TU' = T) Xs;
   315             val fp_abs = nth fp_abss i;
   316             val fp_rep = nth fp_reps i;
   317             val abs = nth abss i;
   318             val rep = nth reps i;
   319             val sF = co_alg_funT TU';
   320             val sF' =
   321               mk_absT_fp_repT (nth repTs' i) (nth absTs' i) (nth fp_absTs i) (nth fp_repTs i) sF
   322                 handle Term.TYPE _ => sF;
   323             val F = nth rec_preTs i;
   324             val s = nth rec_strs i;
   325           in
   326             if sF = F then s
   327             else if sF' = F then mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep s
   328             else
   329               let
   330                 val smapT = replicate live dummyT ---> mk_co_algT sF' F;
   331                 fun hidden_to_unit t =
   332                   Term.subst_TVars (map (rpair HOLogic.unitT) (Term.add_tvar_names t [])) t;
   333                 val smap = map_of_bnf (nth bnfs i)
   334                   |> force_typ names_lthy smapT
   335                   |> hidden_to_unit;
   336                 val smap_argTs = strip_typeN live (fastype_of smap) |> fst;
   337                 fun mk_smap_arg T_to_U =
   338                   (if domain_type T_to_U = range_type T_to_U then
   339                     HOLogic.id_const (domain_type T_to_U)
   340                   else
   341                     let
   342                       val (TY, (U, X)) = T_to_U |> dest_co_algT ||> dest_co_productT;
   343                       val T = mk_co_algT TY U;
   344                       fun mk_co_proj TU =
   345                         build_map lthy [] (fn TU =>
   346                           let val ((T1, T2), U) = TU |> co_swap |>> dest_co_productT in
   347                             if T1 = U then co_proj1_const TU
   348                             else mk_co_comp (mk_co_proj (co_swap (T1, U)),
   349                               co_proj1_const (co_swap (mk_co_productT T1 T2, T1)))
   350                           end)
   351                           TU;
   352                       fun default () =
   353                         mk_co_product (mk_co_proj (dest_funT T))
   354                           (fst (fst (mk_rec NONE recs lthy (mk_co_algT TY X))));
   355                     in
   356                       if can dest_co_productT TY then
   357                         mk_map_co_product (mk_co_proj (co_swap (dest_co_productT TY |> fst, U)))
   358                           (HOLogic.id_const X)
   359                         handle TYPE _ => default () (*N2M involving "prod" type*)
   360                       else
   361                         default ()
   362                     end)
   363                 val smap_args = map mk_smap_arg smap_argTs;
   364               in
   365                 mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep
   366                   (mk_co_comp (s, Term.list_comb (smap, smap_args)))
   367               end
   368           end;
   369         val t = Term.list_comb (TUrec, map mk_s TUs);
   370       in
   371         (case b_opt of
   372           NONE => ((t, Drule.dummy_thm), lthy)
   373         | SOME b => Local_Theory.define ((b, NoSyn), ((Binding.conceal (Thm.def_binding b), []),
   374             fold_rev Term.absfree rec_strs' t)) lthy |>> apsnd snd)
   375       end;
   376 
   377     val recN = case_fp fp ctor_recN dtor_corecN;
   378     fun mk_recs lthy =
   379       fold2 (fn TU => fn b => fn ((recs, defs), lthy) =>
   380         mk_rec (SOME b) recs lthy TU |>> (fn (f, d) => (f :: recs, d :: defs)))
   381       resTs (map (Binding.suffix_name ("_" ^ recN)) bs) (([], []), lthy)
   382       |>> map_prod rev rev;
   383     val ((raw_co_recs, raw_co_rec_defs), (lthy, raw_lthy)) = lthy
   384       |> mk_recs
   385       ||> `Local_Theory.restore;
   386 
   387     val phi = Proof_Context.export_morphism raw_lthy lthy;
   388 
   389     val co_recs = map (Morphism.term phi) raw_co_recs;
   390 
   391     val fp_rec_o_maps = of_fp_res #xtor_co_rec_o_map_thms
   392       |> maps (fn thm => [thm, thm RS rewrite_comp_comp]);
   393 
   394     val xtor_co_rec_thms =
   395       let
   396         val recs = map (fn r => Term.list_comb (r, rec_strs)) raw_co_recs;
   397         val rec_mapTs = co_swap (As @ fpTs, As @ map2 mk_co_productT fpTs Xs);
   398         val pre_rec_maps =
   399           map2 (fn Ds => fn bnf =>
   400             Term.list_comb (uncurry (mk_map_of_bnf Ds) rec_mapTs bnf,
   401               map HOLogic.id_const As @ map2 (mk_co_product o HOLogic.id_const) fpTs recs))
   402           Dss bnfs;
   403 
   404         fun mk_goals f xtor s smap fp_abs fp_rep abs rep =
   405           let
   406             val lhs = mk_co_comp (f, xtor);
   407             val rhs = mk_co_comp (s, smap);
   408           in
   409             HOLogic.mk_eq (lhs,
   410               mk_co_comp_abs_rep (co_alg_funT (fastype_of lhs)) (co_alg_funT (fastype_of rhs))
   411                 fp_abs fp_rep abs rep rhs)
   412           end;
   413 
   414         val goals = map8 mk_goals recs xtors rec_strs pre_rec_maps fp_abss fp_reps abss reps;
   415 
   416         val pre_map_defs = no_refl (map map_def_of_bnf bnfs);
   417         val fp_pre_map_defs = no_refl (map map_def_of_bnf pre_bnfs);
   418 
   419         val unfold_map = map (unfold_thms lthy (id_apply :: pre_map_defs));
   420 
   421         val fp_xtor_co_recs = map (mk_pointfree lthy) (of_fp_res #xtor_co_rec_thms);
   422 
   423         val fold_thms = case_fp fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} ::
   424           map (fn thm => thm RS rewrite_comp_comp) @{thms map_prod.comp map_sum.comp} @
   425           @{thms id_apply comp_id id_comp map_prod.comp map_prod.id map_sum.comp map_sum.id};
   426         val rec_thms = fold_thms @ case_fp fp
   427           @{thms fst_convol map_prod_o_convol convol_o fst_comp_map_prod}
   428           @{thms case_sum_o_inj(1) case_sum_o_map_sum o_case_sum map_sum_o_inj(1)};
   429 
   430         val eq_thm_prop_untyped = Term.aconv_untyped o pairself Thm.full_prop_of;
   431 
   432         val map_thms = no_refl (maps (fn bnf =>
   433            let val map_comp0 = map_comp0_of_bnf bnf RS sym
   434            in [map_comp0, map_comp0 RS rewrite_comp_comp, map_id0_of_bnf bnf] end)
   435           fp_or_nesting_bnfs) @
   436           remove eq_thm_prop_untyped (case_fp fp @{thm comp_assoc[symmetric]} @{thm comp_assoc})
   437           (map2 (fn thm => fn bnf =>
   438             @{thm type_copy_map_comp0_undo} OF
   439               (replicate 3 thm @ unfold_map [map_comp0_of_bnf bnf]) RS
   440               rewrite_comp_comp)
   441           type_definitions bnfs);
   442 
   443         fun mk_Rep_o_Abs thm = (thm RS @{thm type_copy_Rep_o_Abs})
   444           |> (fn thm => [thm, thm RS rewrite_comp_comp]);
   445 
   446         val fp_Rep_o_Abss = maps mk_Rep_o_Abs fp_type_definitions;
   447         val Rep_o_Abss = maps mk_Rep_o_Abs type_definitions;
   448 
   449         fun tac {context = ctxt, prems = _} =
   450           unfold_thms_tac ctxt (flat [rec_thms, raw_co_rec_defs, pre_map_defs,
   451             fp_pre_map_defs, fp_xtor_co_recs, fp_rec_o_maps, map_thms, fp_Rep_o_Abss,
   452             Rep_o_Abss]) THEN
   453           CONJ_WRAP (K (HEADGOAL (rtac refl))) bnfs;
   454       in
   455         Library.foldr1 HOLogic.mk_conj goals
   456         |> HOLogic.mk_Trueprop
   457         |> fold_rev Logic.all rec_strs
   458         |> (fn goal => Goal.prove_sorry raw_lthy [] [] goal tac)
   459         |> Thm.close_derivation
   460         |> Morphism.thm phi
   461         |> split_conj_thm
   462         |> map (fn thm => thm RS @{thm comp_eq_dest})
   463       end;
   464 
   465     (* These results are half broken. This is deliberate. We care only about those fields that are
   466        used by "primrec", "primcorecursive", and "datatype_compat". *)
   467     val fp_res =
   468       ({Ts = fpTs, bnfs = of_fp_res #bnfs, dtors = dtors, ctors = ctors,
   469         xtor_co_recs = co_recs, xtor_co_induct = xtor_co_induct_thm,
   470         dtor_ctors = of_fp_res #dtor_ctors (*too general types*),
   471         ctor_dtors = of_fp_res #ctor_dtors (*too general types*),
   472         ctor_injects = of_fp_res #ctor_injects (*too general types*),
   473         dtor_injects = of_fp_res #dtor_injects (*too general types*),
   474         xtor_map_thms = of_fp_res #xtor_map_thms (*too general types and terms*),
   475         xtor_set_thmss = of_fp_res #xtor_set_thmss (*too general types and terms*),
   476         xtor_rel_thms = of_fp_res #xtor_rel_thms (*too general types and terms*),
   477         xtor_co_rec_thms = xtor_co_rec_thms,
   478         xtor_co_rec_o_map_thms = fp_rec_o_maps (*theorems about old constants*),
   479         rel_xtor_co_induct_thm = rel_xtor_co_induct_thm,
   480         dtor_set_induct_thms = [],
   481         ctor_rec_transfer_thms = []}
   482        |> morph_fp_result (Morphism.term_morphism "BNF" (singleton (Variable.polymorphic lthy))));
   483   in
   484     (fp_res, lthy)
   485   end;
   486 
   487 end;