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