src/HOL/Tools/BNF/bnf_fp_n2m.ML
author traytel
Wed Aug 06 10:20:50 2014 +0200 (2014-08-06)
changeset 57801 4adfa833072b
parent 57700 a2c4adb839a9
child 57802 9c065009cd8a
permissions -rw-r--r--
made tactic more robust
     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_Util.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 cliques fpTs (fp_sugars : fp_sugar list) bs resBs (resDs, Dss) bnfs
    51     (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 rel_xtor_co_induct_thm =
   186       mk_rel_xtor_co_induct_thm fp (map3 cast castAs castBs pre_rels) pre_phis rels phis xs ys xtors
   187         xtor's (mk_rel_xtor_co_induct_tactic fp abs_inverses rel_xtor_co_inducts rel_defs rel_monos)
   188         lthy;
   189 
   190     val rel_eqs = no_refl (map rel_eq_of_bnf fp_or_nesting_bnfs);
   191     val map_id0s = no_refl (map map_id0_of_bnf bnfs);
   192 
   193     val xtor_co_induct_thm =
   194       (case fp of
   195         Least_FP =>
   196           let
   197             val (Ps, names_lthy) = names_lthy
   198               |> mk_Frees "P" (map (fn T => T --> HOLogic.boolT) fpTs);
   199             fun mk_Grp_id P =
   200               let val T = domain_type (fastype_of P);
   201               in mk_Grp (HOLogic.Collect_const T $ P) (HOLogic.id_const T) end;
   202             val cts = map (SOME o certify lthy) (map HOLogic.eq_const As @ map mk_Grp_id Ps);
   203             fun mk_fp_type_copy_thms thm = map (curry op RS thm)
   204               @{thms type_copy_Abs_o_Rep type_copy_vimage2p_Grp_Rep};
   205             fun mk_type_copy_thms thm = map (curry op RS thm)
   206               @{thms type_copy_Rep_o_Abs type_copy_vimage2p_Grp_Abs};
   207           in
   208             cterm_instantiate_pos cts rel_xtor_co_induct_thm
   209             |> singleton (Proof_Context.export names_lthy lthy)
   210             |> unfold_thms lthy (@{thms eq_le_Grp_id_iff all_simps(1,2)[symmetric]} @ rel_eqs)
   211             |> funpow n (fn thm => thm RS spec)
   212             |> unfold_thms lthy (@{thm eq_alt} :: map rel_Grp_of_bnf bnfs @ map_id0s)
   213             |> unfold_thms lthy (@{thms vimage2p_id vimage2p_comp comp_apply comp_id
   214                Grp_id_mono_subst eqTrueI[OF subset_UNIV] simp_thms(22)} @
   215                maps mk_fp_type_copy_thms fp_type_definitions @
   216                maps mk_type_copy_thms type_definitions)
   217             |> unfold_thms lthy @{thms subset_iff mem_Collect_eq
   218                atomize_conjL[symmetric] atomize_all[symmetric] atomize_imp[symmetric]}
   219           end
   220       | Greatest_FP =>
   221           let
   222             val cts = NONE :: map (SOME o certify lthy) (map HOLogic.eq_const As);
   223           in
   224             cterm_instantiate_pos cts rel_xtor_co_induct_thm
   225             |> unfold_thms lthy (@{thms le_fun_def le_bool_def all_simps(1,2)[symmetric]} @ rel_eqs)
   226             |> funpow (2 * n) (fn thm => thm RS spec)
   227             |> Conv.fconv_rule (Object_Logic.atomize lthy)
   228             |> funpow n (fn thm => thm RS mp)
   229           end);
   230 
   231     val fold_preTs = map2 (fn Ds => mk_T_of_bnf Ds allAs) Dss bnfs;
   232     val rec_preTs = map (Term.typ_subst_atomic rec_theta) fold_preTs;
   233 
   234     val rec_strTs = map2 mk_co_algT rec_preTs Xs;
   235     val resTs = map2 mk_co_algT fpTs Xs;
   236 
   237     val ((rec_strs, rec_strs'), names_lthy) = names_lthy
   238       |> mk_Frees' "s" rec_strTs;
   239 
   240     val co_recs = of_fp_res #xtor_co_recs;
   241     val ns = map (length o #Ts o #fp_res) fp_sugars;
   242 
   243     fun substT rho (Type (@{type_name fun}, [T, U])) = substT rho T --> substT rho U
   244       | substT rho (Type (s, Ts)) = Type (s, map (typ_subst_nonatomic rho) Ts)
   245       | substT _ T = T;
   246 
   247     val typ_subst_nonatomic_sorted = fold_rev (typ_subst_nonatomic o single);
   248 
   249     fun foldT_of_recT recT =
   250       let
   251         val ((FTXs, Xs), TX) = strip_fun_type recT |>> map_split dest_co_algT;
   252         fun subst (Type (C, Ts as [_, X])) =
   253             if C = co_productC andalso member op = Xs X then X else Type (C, map subst Ts)
   254           | subst (Type (C, Ts)) = Type (C, map subst Ts)
   255           | subst T = T;
   256       in
   257         map2 (mk_co_algT o subst) FTXs Xs ---> TX
   258       end;
   259 
   260     fun force_rec i TU TU_rec raw_rec =
   261       let
   262         val thy = Proof_Context.theory_of lthy;
   263 
   264         val approx_rec = raw_rec
   265           |> force_typ names_lthy (replicate (nth ns i) dummyT ---> TU_rec);
   266         val subst = Term.typ_subst_atomic fold_thetaAs;
   267 
   268         fun mk_fp_absT_repT fp_repT fp_absT = mk_absT thy fp_repT fp_absT ooo mk_repT;
   269         val mk_fp_absT_repTs = map5 mk_fp_absT_repT fp_repTs fp_absTs absTs repTs;
   270 
   271         val fold_preTs' = mk_fp_absT_repTs (map subst fold_preTs);
   272 
   273         val fold_pre_deads_only_Ts =
   274           map (typ_subst_nonatomic_sorted (map (rpair dummyT) (As @ fpTs))) fold_preTs';
   275 
   276         val (clique, TUs) =
   277           map_split dest_co_algT (binder_fun_types (foldT_of_recT (fastype_of approx_rec)))
   278           |>> map subst
   279           |> `(fn (_, Ys) =>
   280             nth cliques (find_index (fn X => X = the (find_first (can dest_TFree) Ys)) Xs))
   281           ||> uncurry (map2 mk_co_algT);
   282         val cands = cliques ~~ map2 mk_co_algT fold_preTs' Xs;
   283         val js = find_indices (fn ((cl, cand), TU) =>
   284           cl = clique andalso Type.could_unify (TU, cand)) TUs cands;
   285         val Tpats = map (fn j => mk_co_algT (nth fold_pre_deads_only_Ts j) (nth Xs j)) js;
   286       in
   287         force_typ names_lthy (Tpats ---> TU) raw_rec
   288       end;
   289 
   290     fun mk_co_comp_abs_rep fp_absT absT fp_abs fp_rep abs rep t =
   291       case_fp fp (HOLogic.mk_comp (HOLogic.mk_comp (t, mk_abs absT abs), mk_rep fp_absT fp_rep))
   292         (HOLogic.mk_comp (mk_abs fp_absT fp_abs, HOLogic.mk_comp (mk_rep absT rep, t)));
   293 
   294     fun mk_rec b_opt recs lthy TU =
   295       let
   296         val thy = Proof_Context.theory_of lthy;
   297 
   298         val x = co_alg_argT TU;
   299         val i = find_index (fn T => x = T) Xs;
   300         val TUrec =
   301           (case find_first (fn f => body_fun_type (fastype_of f) = TU) recs of
   302             NONE =>
   303               force_rec i TU
   304                 (TU |> is_none b_opt ? substT (map2 mk_co_productT fpTs Xs ~~ Xs)) (nth co_recs i)
   305           | SOME f => f);
   306 
   307         val TUs = binder_fun_types (fastype_of TUrec);
   308 
   309         fun mk_s TU' =
   310           let
   311             fun mk_absT_fp_repT repT absT = mk_absT thy repT absT ooo mk_repT;
   312 
   313             val i = find_index (fn T => co_alg_argT TU' = T) Xs;
   314             val fp_abs = nth fp_abss i;
   315             val fp_rep = nth fp_reps i;
   316             val abs = nth abss i;
   317             val rep = nth reps i;
   318             val sF = co_alg_funT TU';
   319             val sF' =
   320               mk_absT_fp_repT (nth repTs' i) (nth absTs' i) (nth fp_absTs i) (nth fp_repTs i) sF
   321                 handle Term.TYPE _ => sF;
   322             val F = nth rec_preTs i;
   323             val s = nth rec_strs i;
   324           in
   325             if sF = F then s
   326             else if sF' = F then mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep s
   327             else
   328               let
   329                 val smapT = replicate live dummyT ---> mk_co_algT sF' F;
   330                 fun hidden_to_unit t =
   331                   Term.subst_TVars (map (rpair HOLogic.unitT) (Term.add_tvar_names t [])) t;
   332                 val smap = map_of_bnf (nth bnfs i)
   333                   |> force_typ names_lthy smapT
   334                   |> hidden_to_unit;
   335                 val smap_argTs = strip_typeN live (fastype_of smap) |> fst;
   336                 fun mk_smap_arg TU =
   337                   (if domain_type TU = range_type TU then
   338                     HOLogic.id_const (domain_type TU)
   339                   else
   340                     let
   341                       val (TY, (U, X)) = TU |> dest_co_algT ||> dest_co_productT;
   342                       val T = mk_co_algT TY U;
   343                     in
   344                       (case try (force_typ names_lthy T o build_map lthy [] co_proj1_const o dest_funT) T of
   345                         SOME f => mk_co_product f
   346                           (fst (fst (mk_rec NONE recs lthy (mk_co_algT TY X))))
   347                       | NONE => mk_map_co_product
   348                           (build_map lthy [] co_proj1_const
   349                             (dest_funT (mk_co_algT (dest_co_productT TY |> fst) U)))
   350                           (HOLogic.id_const X))
   351                     end)
   352                 val smap_args = map mk_smap_arg smap_argTs;
   353               in
   354                 mk_co_comp_abs_rep sF sF' fp_abs fp_rep abs rep
   355                   (mk_co_comp (s, Term.list_comb (smap, smap_args)))
   356               end
   357           end;
   358         val t = Term.list_comb (TUrec, map mk_s TUs);
   359       in
   360         (case b_opt of
   361           NONE => ((t, Drule.dummy_thm), lthy)
   362         | SOME b => Local_Theory.define ((b, NoSyn), ((Binding.conceal (Thm.def_binding b), []),
   363             fold_rev Term.absfree rec_strs' t)) lthy |>> apsnd snd)
   364       end;
   365 
   366     val recN = case_fp fp ctor_recN dtor_corecN;
   367     fun mk_recs lthy =
   368       fold2 (fn TU => fn b => fn ((recs, defs), lthy) =>
   369         mk_rec (SOME b) recs lthy TU |>> (fn (f, d) => (f :: recs, d :: defs)))
   370       resTs (map (Binding.suffix_name ("_" ^ recN)) bs) (([], []), lthy)
   371       |>> map_prod rev rev;
   372     val ((raw_co_recs, raw_co_rec_defs), (lthy, raw_lthy)) = lthy
   373       |> mk_recs
   374       ||> `Local_Theory.restore;
   375 
   376     val phi = Proof_Context.export_morphism raw_lthy lthy;
   377 
   378     val co_recs = map (Morphism.term phi) raw_co_recs;
   379 
   380     val fp_rec_o_maps = of_fp_res #xtor_co_rec_o_map_thms
   381       |> maps (fn thm => [thm, thm RS rewrite_comp_comp]);
   382 
   383     val xtor_co_rec_thms =
   384       let
   385         val recs = map (fn r => Term.list_comb (r, rec_strs)) raw_co_recs;
   386         val rec_mapTs = co_swap (As @ fpTs, As @ map2 mk_co_productT fpTs Xs);
   387         val pre_rec_maps =
   388           map2 (fn Ds => fn bnf =>
   389             Term.list_comb (uncurry (mk_map_of_bnf Ds) rec_mapTs bnf,
   390               map HOLogic.id_const As @ map2 (mk_co_product o HOLogic.id_const) fpTs recs))
   391           Dss bnfs;
   392 
   393         fun mk_goals f xtor s smap fp_abs fp_rep abs rep =
   394           let
   395             val lhs = mk_co_comp (f, xtor);
   396             val rhs = mk_co_comp (s, smap);
   397           in
   398             HOLogic.mk_eq (lhs,
   399               mk_co_comp_abs_rep (co_alg_funT (fastype_of lhs)) (co_alg_funT (fastype_of rhs))
   400                 fp_abs fp_rep abs rep rhs)
   401           end;
   402 
   403         val goals = map8 mk_goals recs xtors rec_strs pre_rec_maps fp_abss fp_reps abss reps;
   404 
   405         val pre_map_defs = no_refl (map map_def_of_bnf bnfs);
   406         val fp_pre_map_defs = no_refl (map map_def_of_bnf pre_bnfs);
   407 
   408         val unfold_map = map (unfold_thms lthy (id_apply :: pre_map_defs));
   409 
   410         val fp_xtor_co_recs = map (mk_pointfree lthy) (of_fp_res #xtor_co_rec_thms);
   411 
   412         val fold_thms = case_fp fp @{thm comp_assoc} @{thm comp_assoc[symmetric]} ::
   413           map (fn thm => thm RS rewrite_comp_comp) @{thms map_prod.comp map_sum.comp} @
   414           @{thms id_apply comp_id id_comp map_prod.comp map_prod.id map_sum.comp map_sum.id};
   415         val rec_thms = fold_thms @ case_fp fp
   416           @{thms fst_convol map_prod_o_convol convol_o}
   417           @{thms case_sum_o_inj(1) case_sum_o_map_sum o_case_sum};
   418 
   419         val eq_thm_prop_untyped = Term.aconv_untyped o pairself Thm.full_prop_of;
   420 
   421         val map_thms = no_refl (maps (fn bnf =>
   422            let val map_comp0 = map_comp0_of_bnf bnf RS sym
   423            in [map_comp0, map_comp0 RS rewrite_comp_comp, map_id0_of_bnf bnf] end)
   424           fp_or_nesting_bnfs) @
   425           remove eq_thm_prop_untyped (case_fp fp @{thm comp_assoc[symmetric]} @{thm comp_assoc})
   426           (map2 (fn thm => fn bnf =>
   427             @{thm type_copy_map_comp0_undo} OF
   428               (replicate 3 thm @ unfold_map [map_comp0_of_bnf bnf]) RS
   429               rewrite_comp_comp)
   430           type_definitions bnfs);
   431 
   432         fun mk_Rep_o_Abs thm = (thm RS @{thm type_copy_Rep_o_Abs})
   433           |> (fn thm => [thm, thm RS rewrite_comp_comp]);
   434 
   435         val fp_Rep_o_Abss = maps mk_Rep_o_Abs fp_type_definitions;
   436         val Rep_o_Abss = maps mk_Rep_o_Abs type_definitions;
   437 
   438         fun tac {context = ctxt, prems = _} =
   439           unfold_thms_tac ctxt (flat [rec_thms, raw_co_rec_defs, pre_map_defs, fp_pre_map_defs,
   440             fp_xtor_co_recs, fp_rec_o_maps, map_thms, fp_Rep_o_Abss, Rep_o_Abss]) THEN
   441           CONJ_WRAP (K (HEADGOAL (rtac refl))) bnfs;
   442       in
   443         Library.foldr1 HOLogic.mk_conj goals
   444         |> HOLogic.mk_Trueprop
   445         |> fold_rev Logic.all rec_strs
   446         |> (fn goal => Goal.prove_sorry raw_lthy [] [] goal tac)
   447         |> Thm.close_derivation
   448         |> Morphism.thm phi
   449         |> split_conj_thm
   450         |> map (fn thm => thm RS @{thm comp_eq_dest})
   451       end;
   452 
   453     (* These results are half broken. This is deliberate. We care only about those fields that are
   454        used by "primrec", "primcorecursive", and "datatype_compat". *)
   455     val fp_res =
   456       ({Ts = fpTs, bnfs = of_fp_res #bnfs, dtors = dtors, ctors = ctors,
   457         xtor_co_recs = co_recs, xtor_co_induct = xtor_co_induct_thm,
   458         dtor_ctors = of_fp_res #dtor_ctors (*too general types*),
   459         ctor_dtors = of_fp_res #ctor_dtors (*too general types*),
   460         ctor_injects = of_fp_res #ctor_injects (*too general types*),
   461         dtor_injects = of_fp_res #dtor_injects (*too general types*),
   462         xtor_map_thms = of_fp_res #xtor_map_thms (*too general types and terms*),
   463         xtor_set_thmss = of_fp_res #xtor_set_thmss (*too general types and terms*),
   464         xtor_rel_thms = of_fp_res #xtor_rel_thms (*too general types and terms*),
   465         xtor_co_rec_thms = xtor_co_rec_thms,
   466         xtor_co_rec_o_map_thms = fp_rec_o_maps (*theorems about old constants*),
   467         rel_xtor_co_induct_thm = rel_xtor_co_induct_thm,
   468         dtor_set_induct_thms = []}
   469        |> morph_fp_result (Morphism.term_morphism "BNF" (singleton (Variable.polymorphic lthy))));
   470   in
   471     (fp_res, lthy)
   472   end;
   473 
   474 end;