src/HOL/Tools/BNF/bnf_lfp.ML
author traytel
Tue Mar 04 12:32:33 2014 +0100 (2014-03-04)
changeset 55899 8c0a13e84963
parent 55868 37b99986d435
child 55901 8c6d49dd8ae1
permissions -rw-r--r--
N2M does not use the low-level 'fold'; removed the latter from the fp_result interface;
     1 (*  Title:      HOL/Tools/BNF/bnf_lfp.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Andrei Popescu, TU Muenchen
     4     Copyright   2012
     5 
     6 Datatype construction.
     7 *)
     8 
     9 signature BNF_LFP =
    10 sig
    11   val construct_lfp: mixfix list -> binding list -> binding list -> binding list list ->
    12     binding list -> (string * sort) list -> typ list * typ list list -> BNF_Def.bnf list ->
    13     BNF_Comp.absT_info list -> local_theory -> BNF_FP_Util.fp_result * local_theory
    14 end;
    15 
    16 structure BNF_LFP : BNF_LFP =
    17 struct
    18 
    19 open BNF_Def
    20 open BNF_Util
    21 open BNF_Tactics
    22 open BNF_Comp
    23 open BNF_FP_Util
    24 open BNF_FP_Def_Sugar
    25 open BNF_LFP_Util
    26 open BNF_LFP_Tactics
    27 
    28 (*all BNFs have the same lives*)
    29 fun construct_lfp mixfixes map_bs rel_bs set_bss0 bs resBs (resDs, Dss) bnfs _ lthy =
    30   let
    31     val time = time lthy;
    32     val timer = time (Timer.startRealTimer ());
    33 
    34     val live = live_of_bnf (hd bnfs);
    35     val n = length bnfs; (*active*)
    36     val ks = 1 upto n;
    37     val m = live - n; (*passive, if 0 don't generate a new BNF*)
    38 
    39     val note_all = Config.get lthy bnf_note_all;
    40     val b_names = map Binding.name_of bs;
    41     val b_name = mk_common_name b_names;
    42     val b = Binding.name b_name;
    43     val mk_internal_b = Binding.name #> Binding.prefix true b_name #> Binding.conceal;
    44     fun mk_internal_bs name =
    45       map (fn b =>
    46         Binding.prefix true b_name (Binding.prefix_name (name ^ "_") b) |> Binding.conceal) bs;
    47     val external_bs = map2 (Binding.prefix false) b_names bs
    48       |> note_all = false ? map Binding.conceal;
    49 
    50     (* TODO: check if m, n, etc., are sane *)
    51 
    52     val deads = fold (union (op =)) Dss resDs;
    53     val names_lthy = fold Variable.declare_typ deads lthy;
    54     val passives = map fst (subtract (op = o apsnd TFree) deads resBs);
    55 
    56     (* tvars *)
    57     val (((((passiveAs, activeAs), passiveBs), activeBs), passiveCs), activeCs) =
    58       names_lthy
    59       |> variant_tfrees passives
    60       ||>> mk_TFrees n
    61       ||>> variant_tfrees passives
    62       ||>> mk_TFrees n
    63       ||>> variant_tfrees passives
    64       ||>> mk_TFrees n
    65       |> fst;
    66 
    67     val allAs = passiveAs @ activeAs;
    68     val allBs' = passiveBs @ activeBs;
    69     val Ass = replicate n allAs;
    70     val allBs = passiveAs @ activeBs;
    71     val Bss = replicate n allBs;
    72     val allCs = passiveAs @ activeCs;
    73     val allCs' = passiveBs @ activeCs;
    74     val Css' = replicate n allCs';
    75 
    76     (* types *)
    77     val dead_poss =
    78       map (fn x => if member (op =) deads (TFree x) then SOME (TFree x) else NONE) resBs;
    79     fun mk_param NONE passive = (hd passive, tl passive)
    80       | mk_param (SOME a) passive = (a, passive);
    81     val mk_params = fold_map mk_param dead_poss #> fst;
    82 
    83     fun mk_FTs Ts = map2 (fn Ds => mk_T_of_bnf Ds Ts) Dss bnfs;
    84     val (params, params') = `(map Term.dest_TFree) (mk_params passiveAs);
    85     val (dead_params, dead_params') = `(map Term.dest_TFree) (subtract (op =) passiveAs params');
    86     val FTsAs = mk_FTs allAs;
    87     val FTsBs = mk_FTs allBs;
    88     val FTsCs = mk_FTs allCs;
    89     val BTs = map HOLogic.mk_setT activeAs;
    90     val B'Ts = map HOLogic.mk_setT activeBs;
    91     val B''Ts = map HOLogic.mk_setT activeCs;
    92     val sTs = map2 (curry op -->) FTsAs activeAs;
    93     val s'Ts = map2 (curry op -->) FTsBs activeBs;
    94     val s''Ts = map2 (curry op -->) FTsCs activeCs;
    95     val fTs = map2 (curry op -->) activeAs activeBs;
    96     val inv_fTs = map2 (curry op -->) activeBs activeAs;
    97     val self_fTs = map2 (curry op -->) activeAs activeAs;
    98     val gTs = map2 (curry op -->) activeBs activeCs;
    99     val all_gTs = map2 (curry op -->) allBs allCs';
   100     val prodBsAs = map2 (curry HOLogic.mk_prodT) activeBs activeAs;
   101     val prodFTs = mk_FTs (passiveAs @ prodBsAs);
   102     val prod_sTs = map2 (curry op -->) prodFTs activeAs;
   103 
   104     (* terms *)
   105     val mapsAsAs = map4 mk_map_of_bnf Dss Ass Ass bnfs;
   106     val mapsAsBs = map4 mk_map_of_bnf Dss Ass Bss bnfs;
   107     val mapsBsAs = map4 mk_map_of_bnf Dss Bss Ass bnfs;
   108     val mapsBsCs' = map4 mk_map_of_bnf Dss Bss Css' bnfs;
   109     val mapsAsCs' = map4 mk_map_of_bnf Dss Ass Css' bnfs;
   110     val map_fsts = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ prodBsAs)) Bss bnfs;
   111     val map_fsts_rev = map4 mk_map_of_bnf Dss Bss (replicate n (passiveAs @ prodBsAs)) bnfs;
   112     fun mk_setss Ts = map3 mk_sets_of_bnf (map (replicate live) Dss)
   113       (map (replicate live) (replicate n Ts)) bnfs;
   114     val setssAs = mk_setss allAs;
   115     val bd0s = map3 mk_bd_of_bnf Dss Ass bnfs;
   116     val bds =
   117       map3 (fn bd0 => fn Ds => fn bnf => mk_csum bd0
   118         (mk_card_of (HOLogic.mk_UNIV
   119           (mk_T_of_bnf Ds (replicate live (fst (dest_relT (fastype_of bd0)))) bnf))))
   120       bd0s Dss bnfs;
   121     val witss = map wits_of_bnf bnfs;
   122 
   123     val (((((((((((((((((zs, zs'), Bs), Bs_copy), B's), B''s), ss), prod_ss), s's), s''s),
   124       fs), fs_copy), inv_fs), self_fs), gs), all_gs), (xFs, xFs')),
   125       names_lthy) = lthy
   126       |> mk_Frees' "z" activeAs
   127       ||>> mk_Frees "B" BTs
   128       ||>> mk_Frees "B" BTs
   129       ||>> mk_Frees "B'" B'Ts
   130       ||>> mk_Frees "B''" B''Ts
   131       ||>> mk_Frees "s" sTs
   132       ||>> mk_Frees "prods" prod_sTs
   133       ||>> mk_Frees "s'" s'Ts
   134       ||>> mk_Frees "s''" s''Ts
   135       ||>> mk_Frees "f" fTs
   136       ||>> mk_Frees "f" fTs
   137       ||>> mk_Frees "f" inv_fTs
   138       ||>> mk_Frees "f" self_fTs
   139       ||>> mk_Frees "g" gTs
   140       ||>> mk_Frees "g" all_gTs
   141       ||>> mk_Frees' "x" FTsAs;
   142 
   143     val passive_UNIVs = map HOLogic.mk_UNIV passiveAs;
   144     val active_UNIVs = map HOLogic.mk_UNIV activeAs;
   145     val prod_UNIVs = map HOLogic.mk_UNIV prodBsAs;
   146     val passive_ids = map HOLogic.id_const passiveAs;
   147     val active_ids = map HOLogic.id_const activeAs;
   148     val fsts = map fst_const prodBsAs;
   149 
   150     (* thms *)
   151     val bd0_card_orders = map bd_card_order_of_bnf bnfs;
   152     val bd0_Card_orders = map bd_Card_order_of_bnf bnfs;
   153     val bd0_Cinfinites = map bd_Cinfinite_of_bnf bnfs;
   154     val set_bd0ss = map set_bd_of_bnf bnfs;
   155 
   156     val bd_card_orders =
   157       map (fn thm => @{thm card_order_csum} OF [thm, @{thm card_of_card_order_on}]) bd0_card_orders;
   158     val bd_Card_order = @{thm Card_order_csum};
   159     val bd_Card_orders = replicate n bd_Card_order;
   160     val bd_Cinfinites = map (fn thm => thm RS @{thm Cinfinite_csum1}) bd0_Cinfinites;
   161     val bd_Cnotzeros = map (fn thm => thm RS @{thm Cinfinite_Cnotzero}) bd_Cinfinites;
   162     val bd_Cinfinite = hd bd_Cinfinites;
   163     val set_bdss =
   164       map2 (fn set_bd0s => fn bd0_Card_order =>
   165         map (fn thm => ctrans OF [thm, bd0_Card_order RS @{thm ordLeq_csum1}]) set_bd0s)
   166       set_bd0ss bd0_Card_orders;
   167     val in_bds = map in_bd_of_bnf bnfs;
   168     val sym_map_comps = map (fn bnf => map_comp0_of_bnf bnf RS sym) bnfs;
   169     val map_comps = map map_comp_of_bnf bnfs;
   170     val map_cong0s = map map_cong0_of_bnf bnfs;
   171     val map_id0s = map map_id0_of_bnf bnfs;
   172     val map_ids = map map_id_of_bnf bnfs;
   173     val set_mapss = map set_map_of_bnf bnfs;
   174     val rel_mono_strongs = map rel_mono_strong_of_bnf bnfs;
   175     val rel_OOs = map rel_OO_of_bnf bnfs;
   176 
   177     val timer = time (timer "Extracted terms & thms");
   178 
   179     (* nonemptiness check *)
   180     fun new_wit X (wit: nonemptiness_witness) = subset (op =) (#I wit, (0 upto m - 1) @ map snd X);
   181 
   182     val all = m upto m + n - 1;
   183 
   184     fun enrich X = map_filter (fn i =>
   185       (case find_first (fn (_, i') => i = i') X of
   186         NONE =>
   187           (case find_index (new_wit X) (nth witss (i - m)) of
   188             ~1 => NONE
   189           | j => SOME (j, i))
   190       | SOME ji => SOME ji)) all;
   191     val reachable = fixpoint (op =) enrich [];
   192     val _ = (case subtract (op =) (map snd reachable) all of
   193         [] => ()
   194       | i :: _ => error ("Cannot define empty datatype " ^ quote (Binding.name_of (nth bs (i - m)))));
   195 
   196     val wit_thms = flat (map2 (fn bnf => fn (j, _) => nth (wit_thmss_of_bnf bnf) j) bnfs reachable);
   197 
   198     val timer = time (timer "Checked nonemptiness");
   199 
   200     (* derived thms *)
   201 
   202     (*map g1 ... gm g(m+1) ... g(m+n) (map id ... id f(m+1) ... f(m+n) x) =
   203       map g1 ... gm (g(m+1) o f(m+1)) ... (g(m+n) o f(m+n)) x*)
   204     fun mk_map_comp_id x mapAsBs mapBsCs mapAsCs map_comp0 =
   205       let
   206         val lhs = Term.list_comb (mapBsCs, all_gs) $
   207           (Term.list_comb (mapAsBs, passive_ids @ fs) $ x);
   208         val rhs = Term.list_comb (mapAsCs,
   209           take m all_gs @ map HOLogic.mk_comp (drop m all_gs ~~ fs)) $ x;
   210       in
   211         Goal.prove_sorry lthy [] []
   212           (fold_rev Logic.all (x :: fs @ all_gs) (mk_Trueprop_eq (lhs, rhs)))
   213           (fn {context = ctxt, prems = _} => mk_map_comp_id_tac ctxt map_comp0)
   214         |> Thm.close_derivation
   215       end;
   216 
   217     val map_comp_id_thms = map5 mk_map_comp_id xFs mapsAsBs mapsBsCs' mapsAsCs' map_comps;
   218 
   219     (*forall a : set(m+1) x. f(m+1) a = a; ...; forall a : set(m+n) x. f(m+n) a = a ==>
   220       map id ... id f(m+1) ... f(m+n) x = x*)
   221     fun mk_map_cong0L x mapAsAs sets map_cong0 map_id =
   222       let
   223         fun mk_prem set f z z' = HOLogic.mk_Trueprop
   224           (mk_Ball (set $ x) (Term.absfree z' (HOLogic.mk_eq (f $ z, z))));
   225         val prems = map4 mk_prem (drop m sets) self_fs zs zs';
   226         val goal = mk_Trueprop_eq (Term.list_comb (mapAsAs, passive_ids @ self_fs) $ x, x);
   227       in
   228         Goal.prove_sorry lthy [] []
   229           (fold_rev Logic.all (x :: self_fs) (Logic.list_implies (prems, goal)))
   230           (K (mk_map_cong0L_tac m map_cong0 map_id))
   231         |> Thm.close_derivation
   232       end;
   233 
   234     val map_cong0L_thms = map5 mk_map_cong0L xFs mapsAsAs setssAs map_cong0s map_ids;
   235     val in_mono'_thms = map (fn bnf => in_mono_of_bnf bnf OF (replicate m subset_refl)) bnfs;
   236     val in_cong'_thms = map (fn bnf => in_cong_of_bnf bnf OF (replicate m refl)) bnfs;
   237 
   238     val timer = time (timer "Derived simple theorems");
   239 
   240     (* algebra *)
   241 
   242     val alg_bind = mk_internal_b algN;
   243     val alg_def_bind = (Thm.def_binding alg_bind, []);
   244 
   245     (*forall i = 1 ... n: (\<forall>x \<in> Fi_in UNIV .. UNIV B1 ... Bn. si x \<in> Bi)*)
   246     val alg_spec =
   247       let
   248         val ins = map3 mk_in (replicate n (passive_UNIVs @ Bs)) setssAs FTsAs;
   249         fun mk_alg_conjunct B s X x x' =
   250           mk_Ball X (Term.absfree x' (HOLogic.mk_mem (s $ x, B)));
   251 
   252         val rhs = Library.foldr1 HOLogic.mk_conj (map5 mk_alg_conjunct Bs ss ins xFs xFs')
   253       in
   254         fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss) rhs
   255       end;
   256 
   257     val ((alg_free, (_, alg_def_free)), (lthy, lthy_old)) =
   258         lthy
   259         |> Local_Theory.define ((alg_bind, NoSyn), (alg_def_bind, alg_spec))
   260         ||> `Local_Theory.restore;
   261 
   262     val phi = Proof_Context.export_morphism lthy_old lthy;
   263     val alg = fst (Term.dest_Const (Morphism.term phi alg_free));
   264     val alg_def = mk_unabs_def (2 * n) (Morphism.thm phi alg_def_free RS meta_eq_to_obj_eq);
   265 
   266     fun mk_alg Bs ss =
   267       let
   268         val args = Bs @ ss;
   269         val Ts = map fastype_of args;
   270         val algT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   271       in
   272         Term.list_comb (Const (alg, algT), args)
   273       end;
   274 
   275     val alg_set_thms =
   276       let
   277         val alg_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   278         fun mk_prem x set B = HOLogic.mk_Trueprop (mk_leq (set $ x) B);
   279         fun mk_concl s x B = HOLogic.mk_Trueprop (HOLogic.mk_mem (s $ x, B));
   280         val premss = map2 ((fn x => fn sets => map2 (mk_prem x) (drop m sets) Bs)) xFs setssAs;
   281         val concls = map3 mk_concl ss xFs Bs;
   282         val goals = map3 (fn x => fn prems => fn concl =>
   283           fold_rev Logic.all (x :: Bs @ ss)
   284             (Logic.list_implies (alg_prem :: prems, concl))) xFs premss concls;
   285       in
   286         map (fn goal =>
   287           Goal.prove_sorry lthy [] [] goal (K (mk_alg_set_tac alg_def)) |> Thm.close_derivation)
   288         goals
   289       end;
   290 
   291     fun mk_talg BTs = mk_alg (map HOLogic.mk_UNIV BTs);
   292 
   293     val talg_thm =
   294       let
   295         val goal = fold_rev Logic.all ss
   296           (HOLogic.mk_Trueprop (mk_talg activeAs ss))
   297       in
   298         Goal.prove_sorry lthy [] [] goal
   299           (K (stac alg_def 1 THEN CONJ_WRAP (K (EVERY' [rtac ballI, rtac UNIV_I] 1)) ss))
   300         |> Thm.close_derivation
   301       end;
   302 
   303     val timer = time (timer "Algebra definition & thms");
   304 
   305     val alg_not_empty_thms =
   306       let
   307         val alg_prem =
   308           HOLogic.mk_Trueprop (mk_alg Bs ss);
   309         val concls = map (HOLogic.mk_Trueprop o mk_not_empty) Bs;
   310         val goals =
   311           map (fn concl =>
   312             fold_rev Logic.all (Bs @ ss) (Logic.mk_implies (alg_prem, concl))) concls;
   313       in
   314         map2 (fn goal => fn alg_set =>
   315           Goal.prove_sorry lthy [] []
   316             goal (K (mk_alg_not_empty_tac lthy alg_set alg_set_thms wit_thms))
   317           |> Thm.close_derivation)
   318         goals alg_set_thms
   319       end;
   320 
   321     val timer = time (timer "Proved nonemptiness");
   322 
   323     (* morphism *)
   324 
   325     val mor_bind = mk_internal_b morN;
   326     val mor_def_bind = (Thm.def_binding mor_bind, []);
   327 
   328     (*fbetw) forall i = 1 ... n: (\<forall>x \<in> Bi. f x \<in> B'i)*)
   329     (*mor) forall i = 1 ... n: (\<forall>x \<in> Fi_in UNIV ... UNIV B1 ... Bn.
   330        f (s1 x) = s1' (Fi_map id ... id f1 ... fn x))*)
   331     val mor_spec =
   332       let
   333         fun mk_fbetw f B1 B2 z z' =
   334           mk_Ball B1 (Term.absfree z' (HOLogic.mk_mem (f $ z, B2)));
   335         fun mk_mor sets mapAsBs f s s' T x x' =
   336           mk_Ball (mk_in (passive_UNIVs @ Bs) sets T)
   337             (Term.absfree x' (HOLogic.mk_eq (f $ (s $ x), s' $
   338               (Term.list_comb (mapAsBs, passive_ids @ fs) $ x))));
   339         val rhs = HOLogic.mk_conj
   340           (Library.foldr1 HOLogic.mk_conj (map5 mk_fbetw fs Bs B's zs zs'),
   341           Library.foldr1 HOLogic.mk_conj
   342             (map8 mk_mor setssAs mapsAsBs fs ss s's FTsAs xFs xFs'))
   343       in
   344         fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss @ B's @ s's @ fs) rhs
   345       end;
   346 
   347     val ((mor_free, (_, mor_def_free)), (lthy, lthy_old)) =
   348         lthy
   349         |> Local_Theory.define ((mor_bind, NoSyn), (mor_def_bind, mor_spec))
   350         ||> `Local_Theory.restore;
   351 
   352     val phi = Proof_Context.export_morphism lthy_old lthy;
   353     val mor = fst (Term.dest_Const (Morphism.term phi mor_free));
   354     val mor_def = mk_unabs_def (5 * n) (Morphism.thm phi mor_def_free RS meta_eq_to_obj_eq);
   355 
   356     fun mk_mor Bs1 ss1 Bs2 ss2 fs =
   357       let
   358         val args = Bs1 @ ss1 @ Bs2 @ ss2 @ fs;
   359         val Ts = map fastype_of (Bs1 @ ss1 @ Bs2 @ ss2 @ fs);
   360         val morT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   361       in
   362         Term.list_comb (Const (mor, morT), args)
   363       end;
   364 
   365     val (mor_image_thms, morE_thms) =
   366       let
   367         val prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   368         fun mk_image_goal f B1 B2 = fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs)
   369           (Logic.mk_implies (prem, HOLogic.mk_Trueprop (mk_leq (mk_image f $ B1) B2)));
   370         val image_goals = map3 mk_image_goal fs Bs B's;
   371         fun mk_elim_prem sets x T = HOLogic.mk_Trueprop
   372           (HOLogic.mk_mem (x, mk_in (passive_UNIVs @ Bs) sets T));
   373         fun mk_elim_goal sets mapAsBs f s s' x T =
   374           fold_rev Logic.all (x :: Bs @ ss @ B's @ s's @ fs)
   375             (Logic.list_implies ([prem, mk_elim_prem sets x T],
   376               mk_Trueprop_eq (f $ (s $ x), s' $ Term.list_comb (mapAsBs, passive_ids @ fs @ [x]))));
   377         val elim_goals = map7 mk_elim_goal setssAs mapsAsBs fs ss s's xFs FTsAs;
   378         fun prove goal =
   379           Goal.prove_sorry lthy [] [] goal (K (mk_mor_elim_tac mor_def)) |> Thm.close_derivation;
   380       in
   381         (map prove image_goals, map prove elim_goals)
   382       end;
   383 
   384     val mor_incl_thm =
   385       let
   386         val prems = map2 (HOLogic.mk_Trueprop oo mk_leq) Bs Bs_copy;
   387         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss Bs_copy ss active_ids);
   388       in
   389         Goal.prove_sorry lthy [] []
   390           (fold_rev Logic.all (Bs @ ss @ Bs_copy) (Logic.list_implies (prems, concl)))
   391           (K (mk_mor_incl_tac mor_def map_ids))
   392         |> Thm.close_derivation
   393       end;
   394 
   395     val mor_comp_thm =
   396       let
   397         val prems =
   398           [HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs),
   399            HOLogic.mk_Trueprop (mk_mor B's s's B''s s''s gs)];
   400         val concl =
   401           HOLogic.mk_Trueprop (mk_mor Bs ss B''s s''s (map2 (curry HOLogic.mk_comp) gs fs));
   402       in
   403         Goal.prove_sorry lthy [] []
   404           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ B''s @ s''s @ fs @ gs)
   405              (Logic.list_implies (prems, concl)))
   406           (K (mk_mor_comp_tac mor_def set_mapss map_comp_id_thms))
   407         |> Thm.close_derivation
   408       end;
   409 
   410     val mor_inv_thm =
   411       let
   412         fun mk_inv_prem f inv_f B B' = HOLogic.mk_conj (mk_leq (mk_image inv_f $ B') B,
   413           HOLogic.mk_conj (mk_inver inv_f f B, mk_inver f inv_f B'));
   414         val prems = map HOLogic.mk_Trueprop
   415           ([mk_mor Bs ss B's s's fs, mk_alg Bs ss, mk_alg B's s's] @
   416           map4 mk_inv_prem fs inv_fs Bs B's);
   417         val concl = HOLogic.mk_Trueprop (mk_mor B's s's Bs ss inv_fs);
   418       in
   419         Goal.prove_sorry lthy [] []
   420           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs @ inv_fs)
   421             (Logic.list_implies (prems, concl)))
   422           (K (mk_mor_inv_tac alg_def mor_def set_mapss morE_thms map_comp_id_thms map_cong0L_thms))
   423         |> Thm.close_derivation
   424       end;
   425 
   426     val mor_cong_thm =
   427       let
   428         val prems = map HOLogic.mk_Trueprop
   429          (map2 (curry HOLogic.mk_eq) fs_copy fs @ [mk_mor Bs ss B's s's fs])
   430         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs_copy);
   431       in
   432         Goal.prove_sorry lthy [] []
   433           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs @ fs_copy)
   434              (Logic.list_implies (prems, concl)))
   435           (K ((hyp_subst_tac lthy THEN' atac) 1))
   436         |> Thm.close_derivation
   437       end;
   438 
   439     val mor_str_thm =
   440       let
   441         val maps = map2 (fn Ds => fn bnf => Term.list_comb
   442           (mk_map_of_bnf Ds (passiveAs @ FTsAs) allAs bnf, passive_ids @ ss)) Dss bnfs;
   443       in
   444         Goal.prove_sorry lthy [] []
   445           (fold_rev Logic.all ss (HOLogic.mk_Trueprop
   446             (mk_mor (map HOLogic.mk_UNIV FTsAs) maps active_UNIVs ss ss)))
   447           (K (mk_mor_str_tac ks mor_def))
   448         |> Thm.close_derivation
   449       end;
   450 
   451     val mor_convol_thm =
   452       let
   453         val maps = map3 (fn s => fn prod_s => fn mapx =>
   454           mk_convol (HOLogic.mk_comp (s, Term.list_comb (mapx, passive_ids @ fsts)), prod_s))
   455           s's prod_ss map_fsts;
   456       in
   457         Goal.prove_sorry lthy [] []
   458           (fold_rev Logic.all (s's @ prod_ss) (HOLogic.mk_Trueprop
   459             (mk_mor prod_UNIVs maps (map HOLogic.mk_UNIV activeBs) s's fsts)))
   460           (K (mk_mor_convol_tac ks mor_def))
   461         |> Thm.close_derivation
   462       end;
   463 
   464     val mor_UNIV_thm =
   465       let
   466         fun mk_conjunct mapAsBs f s s' = HOLogic.mk_eq
   467             (HOLogic.mk_comp (f, s),
   468             HOLogic.mk_comp (s', Term.list_comb (mapAsBs, passive_ids @ fs)));
   469         val lhs = mk_mor active_UNIVs ss (map HOLogic.mk_UNIV activeBs) s's fs;
   470         val rhs = Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct mapsAsBs fs ss s's);
   471       in
   472         Goal.prove_sorry lthy [] [] (fold_rev Logic.all (ss @ s's @ fs) (mk_Trueprop_eq (lhs, rhs)))
   473           (K (mk_mor_UNIV_tac m morE_thms mor_def))
   474         |> Thm.close_derivation
   475       end;
   476 
   477     val timer = time (timer "Morphism definition & thms");
   478 
   479     (* isomorphism *)
   480 
   481     (*mor Bs1 ss1 Bs2 ss2 fs \<and> (\<exists>gs. mor Bs2 ss2 Bs1 ss1 fs \<and>
   482        forall i = 1 ... n. (inver gs[i] fs[i] Bs1[i] \<and> inver fs[i] gs[i] Bs2[i]))*)
   483     fun mk_iso Bs1 ss1 Bs2 ss2 fs gs =
   484       let
   485         val ex_inv_mor = list_exists_free gs
   486           (HOLogic.mk_conj (mk_mor Bs2 ss2 Bs1 ss1 gs,
   487             Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_conj)
   488               (map3 mk_inver gs fs Bs1) (map3 mk_inver fs gs Bs2))));
   489       in
   490         HOLogic.mk_conj (mk_mor Bs1 ss1 Bs2 ss2 fs, ex_inv_mor)
   491       end;
   492 
   493     val iso_alt_thm =
   494       let
   495         val prems = map HOLogic.mk_Trueprop [mk_alg Bs ss, mk_alg B's s's]
   496         val concl = mk_Trueprop_eq (mk_iso Bs ss B's s's fs inv_fs,
   497           HOLogic.mk_conj (mk_mor Bs ss B's s's fs,
   498             Library.foldr1 HOLogic.mk_conj (map3 mk_bij_betw fs Bs B's)));
   499       in
   500         Goal.prove_sorry lthy [] []
   501           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs) (Logic.list_implies (prems, concl)))
   502           (K (mk_iso_alt_tac mor_image_thms mor_inv_thm))
   503         |> Thm.close_derivation
   504       end;
   505 
   506     val timer = time (timer "Isomorphism definition & thms");
   507 
   508     (* algebra copies *)
   509 
   510     val (copy_alg_thm, ex_copy_alg_thm) =
   511       let
   512         val prems = map HOLogic.mk_Trueprop
   513          (mk_alg Bs ss :: map3 mk_bij_betw inv_fs B's Bs);
   514         val inver_prems = map HOLogic.mk_Trueprop
   515           (map3 mk_inver inv_fs fs Bs @ map3 mk_inver fs inv_fs B's);
   516         val all_prems = prems @ inver_prems;
   517         fun mk_s f s mapT = Library.foldl1 HOLogic.mk_comp [f, s,
   518           Term.list_comb (mapT, passive_ids @ inv_fs)];
   519 
   520         val alg = HOLogic.mk_Trueprop
   521           (mk_alg B's (map3 mk_s fs ss mapsBsAs));
   522         val copy_str_thm = Goal.prove_sorry lthy [] []
   523           (fold_rev Logic.all (Bs @ ss @ B's @ inv_fs @ fs)
   524             (Logic.list_implies (all_prems, alg)))
   525           (K (mk_copy_str_tac set_mapss alg_def alg_set_thms))
   526           |> Thm.close_derivation;
   527 
   528         val iso = HOLogic.mk_Trueprop
   529           (mk_iso B's (map3 mk_s fs ss mapsBsAs) Bs ss inv_fs fs_copy);
   530         val copy_alg_thm = Goal.prove_sorry lthy [] []
   531           (fold_rev Logic.all (Bs @ ss @ B's @ inv_fs @ fs)
   532             (Logic.list_implies (all_prems, iso)))
   533           (fn {context = ctxt, prems = _} =>
   534             mk_copy_alg_tac ctxt set_mapss alg_set_thms mor_def iso_alt_thm copy_str_thm)
   535           |> Thm.close_derivation;
   536 
   537         val ex = HOLogic.mk_Trueprop
   538           (list_exists_free s's
   539             (HOLogic.mk_conj (mk_alg B's s's,
   540               mk_iso B's s's Bs ss inv_fs fs_copy)));
   541         val ex_copy_alg_thm = Goal.prove_sorry lthy [] []
   542           (fold_rev Logic.all (Bs @ ss @ B's @ inv_fs @ fs)
   543              (Logic.list_implies (prems, ex)))
   544           (K (mk_ex_copy_alg_tac n copy_str_thm copy_alg_thm))
   545           |> Thm.close_derivation;
   546       in
   547         (copy_alg_thm, ex_copy_alg_thm)
   548       end;
   549 
   550     val timer = time (timer "Copy thms");
   551 
   552 
   553     (* bounds *)
   554 
   555     val sum_bd = Library.foldr1 (uncurry mk_csum) bds;
   556     val sum_bdT = fst (dest_relT (fastype_of sum_bd));
   557 
   558     val (lthy, sbd, sbd_card_order, sbd_Cinfinite, sbd_Card_order, set_sbdss, in_sbds) =
   559       if n = 1
   560       then (lthy, sum_bd, hd bd_card_orders, bd_Cinfinite, bd_Card_order, set_bdss, in_bds)
   561       else
   562         let
   563           val sbdT_bind = mk_internal_b sum_bdTN;
   564 
   565           val ((sbdT_name, (sbdT_glob_info, sbdT_loc_info)), lthy) =
   566             typedef (sbdT_bind, dead_params, NoSyn)
   567               (HOLogic.mk_UNIV sum_bdT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
   568 
   569           val sbdT = Type (sbdT_name, dead_params');
   570           val Abs_sbdT = Const (#Abs_name sbdT_glob_info, sum_bdT --> sbdT);
   571 
   572           val sbd_bind = mk_internal_b sum_bdN;
   573           val sbd_def_bind = (Thm.def_binding sbd_bind, []);
   574 
   575           val sbd_spec = mk_dir_image sum_bd Abs_sbdT;
   576 
   577           val ((sbd_free, (_, sbd_def_free)), (lthy, lthy_old)) =
   578             lthy
   579             |> Local_Theory.define ((sbd_bind, NoSyn), (sbd_def_bind, sbd_spec))
   580             ||> `Local_Theory.restore;
   581 
   582           val phi = Proof_Context.export_morphism lthy_old lthy;
   583 
   584           val sbd_def = Morphism.thm phi sbd_def_free RS meta_eq_to_obj_eq;
   585           val sbd = Const (fst (Term.dest_Const (Morphism.term phi sbd_free)), mk_relT (`I sbdT));
   586 
   587           val Abs_sbdT_inj = mk_Abs_inj_thm (#Abs_inject sbdT_loc_info);
   588           val Abs_sbdT_bij = mk_Abs_bij_thm lthy Abs_sbdT_inj (#Abs_cases sbdT_loc_info);
   589 
   590           val sum_Cinfinite = mk_sum_Cinfinite bd_Cinfinites;
   591           val sum_Card_order = sum_Cinfinite RS conjunct2;
   592           val sum_card_order = mk_sum_card_order bd_card_orders;
   593 
   594           val sbd_ordIso = @{thm ssubst_Pair_rhs} OF
   595             [@{thm dir_image} OF [Abs_sbdT_inj, sum_Card_order], sbd_def];
   596           val sbd_Cinfinite = @{thm Cinfinite_cong} OF [sbd_ordIso, sum_Cinfinite];
   597           val sbd_Card_order = sbd_Cinfinite RS conjunct2;
   598 
   599           val sbd_card_order = @{thm iffD2[OF arg_cong[of _ _ card_order]]} OF
   600             [sbd_def, @{thm card_order_dir_image} OF [Abs_sbdT_bij, sum_card_order]];
   601 
   602           fun mk_set_sbd i bd_Card_order bds =
   603             map (fn thm => @{thm ordLeq_ordIso_trans} OF
   604               [bd_Card_order RS mk_ordLeq_csum n i thm, sbd_ordIso]) bds;
   605           val set_sbdss = map3 mk_set_sbd ks bd_Card_orders set_bdss;
   606 
   607           fun mk_in_bd_sum i Co Cnz bd =
   608             Cnz RS ((@{thm ordLeq_ordIso_trans} OF
   609               [Co RS mk_ordLeq_csum n i (Co RS @{thm ordLeq_refl}), sbd_ordIso]) RS
   610               (bd RS @{thm ordLeq_transitive[OF _ cexp_mono2_Cnotzero[OF _ Card_order_csum]]}));
   611           val in_sbds = map4 mk_in_bd_sum ks bd_Card_orders bd_Cnotzeros in_bds;
   612        in
   613          (lthy, sbd, sbd_card_order, sbd_Cinfinite, sbd_Card_order, set_sbdss, in_sbds)
   614        end;
   615 
   616     val sbd_Cnotzero = sbd_Cinfinite RS @{thm Cinfinite_Cnotzero};
   617     val suc_bd = mk_cardSuc sbd;
   618 
   619     val field_suc_bd = mk_Field suc_bd;
   620     val suc_bdT = fst (dest_relT (fastype_of suc_bd));
   621     fun mk_Asuc_bd [] = mk_cexp ctwo suc_bd
   622       | mk_Asuc_bd As =
   623         mk_cexp (mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo) suc_bd;
   624 
   625     val suc_bd_Card_order =  sbd_Card_order RS @{thm cardSuc_Card_order};
   626     val suc_bd_Cinfinite = sbd_Cinfinite RS @{thm Cinfinite_cardSuc};
   627     val suc_bd_Cnotzero = suc_bd_Cinfinite RS @{thm Cinfinite_Cnotzero};
   628     val suc_bd_worel = suc_bd_Card_order RS @{thm Card_order_wo_rel}
   629     val basis_Asuc = if m = 0 then @{thm ordLeq_refl[OF Card_order_ctwo]}
   630         else @{thm ordLeq_csum2[OF Card_order_ctwo]};
   631     val Asuc_bd_Cinfinite = suc_bd_Cinfinite RS (basis_Asuc RS @{thm Cinfinite_cexp});
   632 
   633     val suc_bd_Asuc_bd = @{thm ordLess_ordLeq_trans[OF ordLess_ctwo_cexp cexp_mono1]} OF
   634       [suc_bd_Card_order, basis_Asuc, suc_bd_Card_order];
   635 
   636 
   637     val Asuc_bd = mk_Asuc_bd passive_UNIVs;
   638     val Asuc_bdT = fst (dest_relT (fastype_of Asuc_bd));
   639     val II_BTs = replicate n (HOLogic.mk_setT Asuc_bdT);
   640     val II_sTs = map2 (fn Ds => fn bnf =>
   641       mk_T_of_bnf Ds (passiveAs @ replicate n Asuc_bdT) bnf --> Asuc_bdT) Dss bnfs;
   642 
   643     val (((((((idxs, Asi_name), (idx, idx')), (jdx, jdx')), II_Bs), II_ss), Asuc_fs),
   644       names_lthy) = names_lthy
   645       |> mk_Frees "i" (replicate n suc_bdT)
   646       ||>> (fn ctxt => apfst the_single (mk_fresh_names ctxt 1 "Asi"))
   647       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") suc_bdT
   648       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "j") suc_bdT
   649       ||>> mk_Frees "IIB" II_BTs
   650       ||>> mk_Frees "IIs" II_sTs
   651       ||>> mk_Frees "f" (map (fn T => Asuc_bdT --> T) activeAs);
   652 
   653     val suc_bd_limit_thm =
   654       let
   655         val prem = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
   656           (map (fn idx => HOLogic.mk_mem (idx, field_suc_bd)) idxs));
   657         fun mk_conjunct idx = HOLogic.mk_conj (mk_not_eq idx jdx,
   658           HOLogic.mk_mem (HOLogic.mk_prod (idx, jdx), suc_bd));
   659         val concl = HOLogic.mk_Trueprop (mk_Bex field_suc_bd
   660           (Term.absfree jdx' (Library.foldr1 HOLogic.mk_conj (map mk_conjunct idxs))));
   661       in
   662         Goal.prove_sorry lthy [] []
   663           (fold_rev Logic.all idxs (Logic.list_implies ([prem], concl)))
   664           (K (mk_bd_limit_tac n suc_bd_Cinfinite))
   665         |> Thm.close_derivation
   666       end;
   667 
   668     val timer = time (timer "Bounds");
   669 
   670 
   671     (* minimal algebra *)
   672 
   673     fun mk_minG Asi i k = mk_UNION (mk_underS suc_bd $ i)
   674       (Term.absfree jdx' (mk_nthN n (Asi $ jdx) k));
   675 
   676     fun mk_minH_component Asi i sets Ts s k =
   677       HOLogic.mk_binop @{const_name "sup"}
   678       (mk_minG Asi i k, mk_image s $ mk_in (passive_UNIVs @ map (mk_minG Asi i) ks) sets Ts);
   679 
   680     fun mk_min_algs ss =
   681       let
   682         val BTs = map (range_type o fastype_of) ss;
   683         val Ts = passiveAs @ BTs;
   684         val (Asi, Asi') = `Free (Asi_name, suc_bdT -->
   685           Library.foldr1 HOLogic.mk_prodT (map HOLogic.mk_setT BTs));
   686       in
   687          mk_worec suc_bd (Term.absfree Asi' (Term.absfree idx' (HOLogic.mk_tuple
   688            (map4 (mk_minH_component Asi idx) (mk_setss Ts) (mk_FTs Ts) ss ks))))
   689       end;
   690 
   691     val (min_algs_thms, min_algs_mono_thms, card_of_min_algs_thm, least_min_algs_thm) =
   692       let
   693         val i_field = HOLogic.mk_mem (idx, field_suc_bd);
   694         val min_algs = mk_min_algs ss;
   695 
   696         val min_algss = map (fn k => mk_nthN n (min_algs $ idx) k) ks;
   697 
   698         val concl = HOLogic.mk_Trueprop
   699           (HOLogic.mk_eq (min_algs $ idx, HOLogic.mk_tuple
   700             (map4 (mk_minH_component min_algs idx) setssAs FTsAs ss ks)));
   701         val goal = fold_rev Logic.all (idx :: ss)
   702           (Logic.mk_implies (HOLogic.mk_Trueprop i_field, concl));
   703 
   704         val min_algs_thm = Goal.prove_sorry lthy [] [] goal
   705           (K (mk_min_algs_tac suc_bd_worel in_cong'_thms))
   706           |> Thm.close_derivation;
   707 
   708         val min_algs_thms = map (fn k => min_algs_thm RS mk_nthI n k) ks;
   709 
   710         fun mk_mono_goal min_alg =
   711           fold_rev Logic.all ss (HOLogic.mk_Trueprop (mk_relChain suc_bd
   712             (Term.absfree idx' min_alg)));
   713 
   714         val monos =
   715           map2 (fn goal => fn min_algs =>
   716             Goal.prove_sorry lthy [] [] goal (K (mk_min_algs_mono_tac lthy min_algs))
   717             |> Thm.close_derivation)
   718           (map mk_mono_goal min_algss) min_algs_thms;
   719 
   720         fun mk_card_conjunct min_alg = mk_ordLeq (mk_card_of min_alg) Asuc_bd;
   721         val card_conjunction = Library.foldr1 HOLogic.mk_conj (map mk_card_conjunct min_algss);
   722         val card_cT = certifyT lthy suc_bdT;
   723         val card_ct = certify lthy (Term.absfree idx' card_conjunction);
   724 
   725         val card_of = singleton (Proof_Context.export names_lthy lthy)
   726           (Goal.prove_sorry lthy [] []
   727             (HOLogic.mk_Trueprop (HOLogic.mk_imp (i_field, card_conjunction)))
   728             (K (mk_min_algs_card_of_tac card_cT card_ct
   729               m suc_bd_worel min_algs_thms in_sbds
   730               sbd_Card_order sbd_Cnotzero suc_bd_Card_order suc_bd_Cinfinite suc_bd_Cnotzero
   731               suc_bd_Asuc_bd Asuc_bd_Cinfinite)))
   732           |> Thm.close_derivation;
   733 
   734         val least_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   735         val least_conjunction = Library.foldr1 HOLogic.mk_conj (map2 mk_leq min_algss Bs);
   736         val least_cT = certifyT lthy suc_bdT;
   737         val least_ct = certify lthy (Term.absfree idx' least_conjunction);
   738 
   739         val least = singleton (Proof_Context.export names_lthy lthy)
   740           (Goal.prove_sorry lthy [] []
   741             (Logic.mk_implies (least_prem,
   742               HOLogic.mk_Trueprop (HOLogic.mk_imp (i_field, least_conjunction))))
   743             (K (mk_min_algs_least_tac least_cT least_ct
   744               suc_bd_worel min_algs_thms alg_set_thms)))
   745           |> Thm.close_derivation;
   746       in
   747         (min_algs_thms, monos, card_of, least)
   748       end;
   749 
   750     val timer = time (timer "min_algs definition & thms");
   751 
   752     val min_alg_binds = mk_internal_bs min_algN;
   753     fun min_alg_bind i = nth min_alg_binds (i - 1);
   754     val min_alg_def_bind = rpair [] o Thm.def_binding o min_alg_bind;
   755 
   756     fun min_alg_spec i =
   757       let
   758         val rhs = mk_UNION (field_suc_bd)
   759           (Term.absfree idx' (mk_nthN n (mk_min_algs ss $ idx) i));
   760       in
   761         fold_rev (Term.absfree o Term.dest_Free) ss rhs
   762       end;
   763 
   764     val ((min_alg_frees, (_, min_alg_def_frees)), (lthy, lthy_old)) =
   765         lthy
   766         |> fold_map (fn i => Local_Theory.define
   767           ((min_alg_bind i, NoSyn), (min_alg_def_bind i, min_alg_spec i))) ks
   768         |>> apsnd split_list o split_list
   769         ||> `Local_Theory.restore;
   770 
   771     val phi = Proof_Context.export_morphism lthy_old lthy;
   772     val min_algs = map (fst o Term.dest_Const o Morphism.term phi) min_alg_frees;
   773     val min_alg_defs = map (fn def =>
   774       mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) min_alg_def_frees;
   775 
   776     fun mk_min_alg ss i =
   777       let
   778         val T = HOLogic.mk_setT (range_type (fastype_of (nth ss (i - 1))))
   779         val Ts = map fastype_of ss;
   780         val min_algT = Library.foldr (op -->) (Ts, T);
   781       in
   782         Term.list_comb (Const (nth min_algs (i - 1), min_algT), ss)
   783       end;
   784 
   785     val (alg_min_alg_thm, card_of_min_alg_thms, least_min_alg_thms, mor_incl_min_alg_thm) =
   786       let
   787         val min_algs = map (mk_min_alg ss) ks;
   788 
   789         val goal = fold_rev Logic.all ss (HOLogic.mk_Trueprop (mk_alg min_algs ss));
   790         val alg_min_alg = Goal.prove_sorry lthy [] [] goal
   791           (K (mk_alg_min_alg_tac m alg_def min_alg_defs suc_bd_limit_thm sbd_Cinfinite
   792             set_sbdss min_algs_thms min_algs_mono_thms))
   793           |> Thm.close_derivation;
   794 
   795         fun mk_card_of_thm min_alg def = Goal.prove_sorry lthy [] []
   796           (fold_rev Logic.all ss
   797             (HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of min_alg) Asuc_bd)))
   798           (K (mk_card_of_min_alg_tac def card_of_min_algs_thm
   799             suc_bd_Card_order suc_bd_Asuc_bd Asuc_bd_Cinfinite))
   800           |> Thm.close_derivation;
   801 
   802         val least_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   803         fun mk_least_thm min_alg B def = Goal.prove_sorry lthy [] []
   804           (fold_rev Logic.all (Bs @ ss)
   805             (Logic.mk_implies (least_prem, HOLogic.mk_Trueprop (mk_leq min_alg B))))
   806           (K (mk_least_min_alg_tac def least_min_algs_thm))
   807           |> Thm.close_derivation;
   808 
   809         val leasts = map3 mk_least_thm min_algs Bs min_alg_defs;
   810 
   811         val incl_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   812         val incl_min_algs = map (mk_min_alg ss) ks;
   813         val incl = Goal.prove_sorry lthy [] []
   814           (fold_rev Logic.all (Bs @ ss)
   815             (Logic.mk_implies (incl_prem,
   816               HOLogic.mk_Trueprop (mk_mor incl_min_algs ss Bs ss active_ids))))
   817           (K (EVERY' (rtac mor_incl_thm :: map etac leasts) 1))
   818           |> Thm.close_derivation;
   819       in
   820         (alg_min_alg, map2 mk_card_of_thm min_algs min_alg_defs, leasts, incl)
   821       end;
   822 
   823     val timer = time (timer "Minimal algebra definition & thms");
   824 
   825     val II_repT = HOLogic.mk_prodT (HOLogic.mk_tupleT II_BTs, HOLogic.mk_tupleT II_sTs);
   826     val IIT_bind = mk_internal_b IITN;
   827 
   828     val ((IIT_name, (IIT_glob_info, IIT_loc_info)), lthy) =
   829       typedef (IIT_bind, params, NoSyn)
   830         (HOLogic.mk_UNIV II_repT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
   831 
   832     val IIT = Type (IIT_name, params');
   833     val Abs_IIT = Const (#Abs_name IIT_glob_info, II_repT --> IIT);
   834     val Rep_IIT = Const (#Rep_name IIT_glob_info, IIT --> II_repT);
   835     val Abs_IIT_inverse_thm = UNIV_I RS #Abs_inverse IIT_loc_info;
   836 
   837     val initT = IIT --> Asuc_bdT;
   838     val active_initTs = replicate n initT;
   839     val init_FTs = map2 (fn Ds => mk_T_of_bnf Ds (passiveAs @ active_initTs)) Dss bnfs;
   840     val init_fTs = map (fn T => initT --> T) activeAs;
   841 
   842     val (((((((iidx, iidx'), init_xs), (init_xFs, init_xFs')),
   843       init_fs), init_fs_copy), init_phis), names_lthy) = names_lthy
   844       |> yield_singleton (apfst (op ~~) oo mk_Frees' "i") IIT
   845       ||>> mk_Frees "ix" active_initTs
   846       ||>> mk_Frees' "x" init_FTs
   847       ||>> mk_Frees "f" init_fTs
   848       ||>> mk_Frees "f" init_fTs
   849       ||>> mk_Frees "P" (replicate n (mk_pred1T initT));
   850 
   851     val II = HOLogic.mk_Collect (fst iidx', IIT, list_exists_free (II_Bs @ II_ss)
   852       (HOLogic.mk_conj (HOLogic.mk_eq (iidx,
   853         Abs_IIT $ (HOLogic.mk_prod (HOLogic.mk_tuple II_Bs, HOLogic.mk_tuple II_ss))),
   854         mk_alg II_Bs II_ss)));
   855 
   856     val select_Bs = map (mk_nthN n (HOLogic.mk_fst (Rep_IIT $ iidx))) ks;
   857     val select_ss = map (mk_nthN n (HOLogic.mk_snd (Rep_IIT $ iidx))) ks;
   858 
   859     val str_init_binds = mk_internal_bs str_initN;
   860     fun str_init_bind i = nth str_init_binds (i - 1);
   861     val str_init_def_bind = rpair [] o Thm.def_binding o str_init_bind;
   862 
   863     fun str_init_spec i =
   864       let
   865         val init_xF = nth init_xFs (i - 1)
   866         val select_s = nth select_ss (i - 1);
   867         val map = mk_map_of_bnf (nth Dss (i - 1))
   868           (passiveAs @ active_initTs) (passiveAs @ replicate n Asuc_bdT)
   869           (nth bnfs (i - 1));
   870         val map_args = passive_ids @ replicate n (mk_rapp iidx Asuc_bdT);
   871         val rhs = select_s $ (Term.list_comb (map, map_args) $ init_xF);
   872       in
   873         fold_rev (Term.absfree o Term.dest_Free) [init_xF, iidx] rhs
   874       end;
   875 
   876     val ((str_init_frees, (_, str_init_def_frees)), (lthy, lthy_old)) =
   877       lthy
   878       |> fold_map (fn i => Local_Theory.define
   879         ((str_init_bind i, NoSyn), (str_init_def_bind i, str_init_spec i))) ks
   880       |>> apsnd split_list o split_list
   881       ||> `Local_Theory.restore;
   882 
   883     val phi = Proof_Context.export_morphism lthy_old lthy;
   884     val str_inits =
   885       map (Term.subst_atomic_types (map (`(Morphism.typ phi)) params') o Morphism.term phi)
   886         str_init_frees;
   887 
   888     val str_init_defs = map (fn def =>
   889       mk_unabs_def 2 (Morphism.thm phi def RS meta_eq_to_obj_eq)) str_init_def_frees;
   890 
   891     val car_inits = map (mk_min_alg str_inits) ks;
   892 
   893     (*TODO: replace with instantiate? (problem: figure out right type instantiation)*)
   894     val alg_init_thm = Goal.prove_sorry lthy [] []
   895       (HOLogic.mk_Trueprop (mk_alg car_inits str_inits))
   896       (K (rtac alg_min_alg_thm 1))
   897       |> Thm.close_derivation;
   898 
   899     val alg_select_thm = Goal.prove_sorry lthy [] []
   900       (HOLogic.mk_Trueprop (mk_Ball II
   901         (Term.absfree iidx' (mk_alg select_Bs select_ss))))
   902       (fn {context = ctxt, prems = _} => mk_alg_select_tac ctxt Abs_IIT_inverse_thm)
   903       |> Thm.close_derivation;
   904 
   905     val mor_select_thm =
   906       let
   907         val alg_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   908         val i_prem = HOLogic.mk_Trueprop (HOLogic.mk_mem (iidx, II));
   909         val mor_prem = HOLogic.mk_Trueprop (mk_mor select_Bs select_ss Bs ss Asuc_fs);
   910         val prems = [alg_prem, i_prem, mor_prem];
   911         val concl = HOLogic.mk_Trueprop
   912           (mk_mor car_inits str_inits Bs ss
   913             (map (fn f => HOLogic.mk_comp (f, mk_rapp iidx Asuc_bdT)) Asuc_fs));
   914       in
   915         Goal.prove_sorry lthy [] []
   916           (fold_rev Logic.all (iidx :: Bs @ ss @ Asuc_fs) (Logic.list_implies (prems, concl)))
   917           (K (mk_mor_select_tac mor_def mor_cong_thm mor_comp_thm mor_incl_min_alg_thm alg_def
   918             alg_select_thm alg_set_thms set_mapss str_init_defs))
   919         |> Thm.close_derivation
   920       end;
   921 
   922     val (init_ex_mor_thm, init_unique_mor_thms) =
   923       let
   924         val prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
   925         val concl = HOLogic.mk_Trueprop
   926           (list_exists_free init_fs (mk_mor car_inits str_inits Bs ss init_fs));
   927         val ex_mor = Goal.prove_sorry lthy [] []
   928           (fold_rev Logic.all (Bs @ ss) (Logic.mk_implies (prem, concl)))
   929           (fn {context = ctxt, prems = _} => mk_init_ex_mor_tac ctxt Abs_IIT_inverse_thm
   930             ex_copy_alg_thm alg_min_alg_thm card_of_min_alg_thms mor_comp_thm mor_select_thm
   931             mor_incl_min_alg_thm)
   932           |> Thm.close_derivation;
   933 
   934         val prems = map2 (HOLogic.mk_Trueprop oo curry HOLogic.mk_mem) init_xs car_inits
   935         val mor_prems = map HOLogic.mk_Trueprop
   936           [mk_mor car_inits str_inits Bs ss init_fs,
   937           mk_mor car_inits str_inits Bs ss init_fs_copy];
   938         fun mk_fun_eq f g x = HOLogic.mk_eq (f $ x, g $ x);
   939         val unique = HOLogic.mk_Trueprop
   940           (Library.foldr1 HOLogic.mk_conj (map3 mk_fun_eq init_fs init_fs_copy init_xs));
   941         val unique_mor = Goal.prove_sorry lthy [] []
   942           (fold_rev Logic.all (init_xs @ Bs @ ss @ init_fs @ init_fs_copy)
   943             (Logic.list_implies (prems @ mor_prems, unique)))
   944           (K (mk_init_unique_mor_tac m alg_def alg_init_thm least_min_alg_thms
   945             in_mono'_thms alg_set_thms morE_thms map_cong0s))
   946           |> Thm.close_derivation;
   947       in
   948         (ex_mor, split_conj_thm unique_mor)
   949       end;
   950 
   951     val init_setss = mk_setss (passiveAs @ active_initTs);
   952     val active_init_setss = map (drop m) init_setss;
   953     val init_ins = map2 (fn sets => mk_in (passive_UNIVs @ car_inits) sets) init_setss init_FTs;
   954 
   955     fun mk_closed phis =
   956       let
   957         fun mk_conjunct phi str_init init_sets init_in x x' =
   958           let
   959             val prem = Library.foldr1 HOLogic.mk_conj
   960               (map2 (fn set => mk_Ball (set $ x)) init_sets phis);
   961             val concl = phi $ (str_init $ x);
   962           in
   963             mk_Ball init_in (Term.absfree x' (HOLogic.mk_imp (prem, concl)))
   964           end;
   965       in
   966         Library.foldr1 HOLogic.mk_conj
   967           (map6 mk_conjunct phis str_inits active_init_setss init_ins init_xFs init_xFs')
   968       end;
   969 
   970     val init_induct_thm =
   971       let
   972         val prem = HOLogic.mk_Trueprop (mk_closed init_phis);
   973         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
   974           (map2 mk_Ball car_inits init_phis));
   975       in
   976         Goal.prove_sorry lthy [] []
   977           (fold_rev Logic.all init_phis (Logic.mk_implies (prem, concl)))
   978           (K (mk_init_induct_tac m alg_def alg_init_thm least_min_alg_thms alg_set_thms))
   979         |> Thm.close_derivation
   980       end;
   981 
   982     val timer = time (timer "Initiality definition & thms");
   983 
   984     val ((T_names, (T_glob_infos, T_loc_infos)), lthy) =
   985       lthy
   986       |> fold_map3 (fn b => fn mx => fn car_init =>
   987         typedef (Binding.conceal b, params, mx) car_init NONE
   988           (EVERY' [rtac ssubst, rtac @{thm ex_in_conv}, resolve_tac alg_not_empty_thms,
   989             rtac alg_init_thm] 1)) bs mixfixes car_inits
   990       |>> apsnd split_list o split_list;
   991 
   992     val Ts = map (fn name => Type (name, params')) T_names;
   993     fun mk_Ts passive = map (Term.typ_subst_atomic (passiveAs ~~ passive)) Ts;
   994     val Ts' = mk_Ts passiveBs;
   995     val Rep_Ts = map2 (fn info => fn T => Const (#Rep_name info, T --> initT)) T_glob_infos Ts;
   996     val Abs_Ts = map2 (fn info => fn T => Const (#Abs_name info, initT --> T)) T_glob_infos Ts;
   997 
   998     val type_defs = map #type_definition T_loc_infos;
   999     val Reps = map #Rep T_loc_infos;
  1000     val Rep_casess = map #Rep_cases T_loc_infos;
  1001     val Rep_injects = map #Rep_inject T_loc_infos;
  1002     val Rep_inverses = map #Rep_inverse T_loc_infos;
  1003     val Abs_inverses = map #Abs_inverse T_loc_infos;
  1004 
  1005     fun mk_inver_thm mk_tac rep abs X thm =
  1006       Goal.prove_sorry lthy [] []
  1007         (HOLogic.mk_Trueprop (mk_inver rep abs X))
  1008         (K (EVERY' [rtac ssubst, rtac @{thm inver_def}, rtac ballI, mk_tac thm] 1))
  1009       |> Thm.close_derivation;
  1010 
  1011     val inver_Reps = map4 (mk_inver_thm rtac) Abs_Ts Rep_Ts (map HOLogic.mk_UNIV Ts) Rep_inverses;
  1012     val inver_Abss = map4 (mk_inver_thm etac) Rep_Ts Abs_Ts car_inits Abs_inverses;
  1013 
  1014     val timer = time (timer "THE TYPEDEFs & Rep/Abs thms");
  1015 
  1016     val UNIVs = map HOLogic.mk_UNIV Ts;
  1017     val FTs = mk_FTs (passiveAs @ Ts);
  1018     val FTs' = mk_FTs (passiveBs @ Ts');
  1019     fun mk_set_Ts T = passiveAs @ replicate n (HOLogic.mk_setT T);
  1020     val setFTss = map (mk_FTs o mk_set_Ts) passiveAs;
  1021     val FTs_setss = mk_setss (passiveAs @ Ts);
  1022     val FTs'_setss = mk_setss (passiveBs @ Ts');
  1023     val map_FT_inits = map2 (fn Ds =>
  1024       mk_map_of_bnf Ds (passiveAs @ Ts) (passiveAs @ active_initTs)) Dss bnfs;
  1025     val fTs = map2 (curry op -->) Ts activeAs;
  1026     val foldT = Library.foldr1 HOLogic.mk_prodT (map2 (curry op -->) Ts activeAs);
  1027     val rec_sTs = map (Term.typ_subst_atomic (activeBs ~~ Ts)) prod_sTs;
  1028     val rec_maps = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_fsts;
  1029     val rec_maps_rev = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_fsts_rev;
  1030     val rec_fsts = map (Term.subst_atomic_types (activeBs ~~ Ts)) fsts;
  1031     val rec_UNIVs = map2 (HOLogic.mk_UNIV oo curry HOLogic.mk_prodT) Ts activeAs;
  1032 
  1033     val (((((((((Izs1, Izs1'), (Izs2, Izs2')), xFs), yFs), (AFss, AFss')),
  1034       (fold_f, fold_f')), fs), rec_ss), names_lthy) = names_lthy
  1035       |> mk_Frees' "z1" Ts
  1036       ||>> mk_Frees' "z2" Ts'
  1037       ||>> mk_Frees "x" FTs
  1038       ||>> mk_Frees "y" FTs'
  1039       ||>> mk_Freess' "z" setFTss
  1040       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "f") foldT
  1041       ||>> mk_Frees "f" fTs
  1042       ||>> mk_Frees "s" rec_sTs;
  1043 
  1044     val Izs = map2 retype_free Ts zs;
  1045     val phis = map2 retype_free (map mk_pred1T Ts) init_phis;
  1046     val phi2s = map2 retype_free (map2 mk_pred2T Ts Ts') init_phis;
  1047 
  1048     fun ctor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctorN ^ "_");
  1049     val ctor_def_bind = rpair [] o Binding.conceal o Thm.def_binding o ctor_bind;
  1050 
  1051     fun ctor_spec abs str map_FT_init =
  1052       Library.foldl1 HOLogic.mk_comp [abs, str,
  1053         Term.list_comb (map_FT_init, map HOLogic.id_const passiveAs @ Rep_Ts)];
  1054 
  1055     val ((ctor_frees, (_, ctor_def_frees)), (lthy, lthy_old)) =
  1056       lthy
  1057       |> fold_map4 (fn i => fn abs => fn str => fn mapx =>
  1058         Local_Theory.define
  1059           ((ctor_bind i, NoSyn), (ctor_def_bind i, ctor_spec abs str mapx)))
  1060           ks Abs_Ts str_inits map_FT_inits
  1061       |>> apsnd split_list o split_list
  1062       ||> `Local_Theory.restore;
  1063 
  1064     val phi = Proof_Context.export_morphism lthy_old lthy;
  1065     fun mk_ctors passive =
  1066       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ (mk_params passive)) o
  1067         Morphism.term phi) ctor_frees;
  1068     val ctors = mk_ctors passiveAs;
  1069     val ctor's = mk_ctors passiveBs;
  1070     val ctor_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) ctor_def_frees;
  1071 
  1072     val (mor_Rep_thm, mor_Abs_thm) =
  1073       let
  1074         val copy = alg_init_thm RS copy_alg_thm;
  1075         fun mk_bij inj Rep cases = @{thm bij_betwI'} OF [inj, Rep, cases];
  1076         val bijs = map3 mk_bij Rep_injects Reps Rep_casess;
  1077         val mor_Rep =
  1078           Goal.prove_sorry lthy [] []
  1079             (HOLogic.mk_Trueprop (mk_mor UNIVs ctors car_inits str_inits Rep_Ts))
  1080             (fn {context = ctxt, prems = _} => mk_mor_Rep_tac ctxt ctor_defs copy bijs inver_Abss
  1081               inver_Reps)
  1082           |> Thm.close_derivation;
  1083 
  1084         val inv = mor_inv_thm OF [mor_Rep, talg_thm, alg_init_thm];
  1085         val mor_Abs =
  1086           Goal.prove_sorry lthy [] []
  1087             (HOLogic.mk_Trueprop (mk_mor car_inits str_inits UNIVs ctors Abs_Ts))
  1088             (K (mk_mor_Abs_tac inv inver_Abss inver_Reps))
  1089           |> Thm.close_derivation;
  1090       in
  1091         (mor_Rep, mor_Abs)
  1092       end;
  1093 
  1094     val timer = time (timer "ctor definitions & thms");
  1095 
  1096     val fold_fun = Term.absfree fold_f'
  1097       (mk_mor UNIVs ctors active_UNIVs ss (map (mk_nthN n fold_f) ks));
  1098     val foldx = HOLogic.choice_const foldT $ fold_fun;
  1099 
  1100     fun fold_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctor_foldN ^ "_");
  1101     val fold_def_bind = rpair [] o Binding.conceal o Thm.def_binding o fold_bind;
  1102 
  1103     fun fold_spec i = fold_rev (Term.absfree o Term.dest_Free) ss (mk_nthN n foldx i);
  1104 
  1105     val ((fold_frees, (_, fold_def_frees)), (lthy, lthy_old)) =
  1106       lthy
  1107       |> fold_map (fn i =>
  1108         Local_Theory.define ((fold_bind i, NoSyn), (fold_def_bind i, fold_spec i))) ks
  1109       |>> apsnd split_list o split_list
  1110       ||> `Local_Theory.restore;
  1111 
  1112     val phi = Proof_Context.export_morphism lthy_old lthy;
  1113     val folds = map (Morphism.term phi) fold_frees;
  1114     val fold_names = map (fst o dest_Const) folds;
  1115     fun mk_folds passives actives =
  1116       map3 (fn name => fn T => fn active =>
  1117         Const (name, Library.foldr (op -->)
  1118           (map2 (curry op -->) (mk_FTs (passives @ actives)) actives, T --> active)))
  1119       fold_names (mk_Ts passives) actives;
  1120     fun mk_fold Ts ss i = Term.list_comb (Const (nth fold_names (i - 1), Library.foldr (op -->)
  1121       (map fastype_of ss, nth Ts (i - 1) --> range_type (fastype_of (nth ss (i - 1))))), ss);
  1122     val fold_defs = map (fn def =>
  1123       mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) fold_def_frees;
  1124 
  1125     val mor_fold_thm =
  1126       let
  1127         val ex_mor = talg_thm RS init_ex_mor_thm;
  1128         val mor_cong = mor_cong_thm OF (map (mk_nth_conv n) ks);
  1129         val mor_comp = mor_Rep_thm RS mor_comp_thm;
  1130         val cT = certifyT lthy foldT;
  1131         val ct = certify lthy fold_fun
  1132       in
  1133         singleton (Proof_Context.export names_lthy lthy)
  1134           (Goal.prove_sorry lthy [] []
  1135             (HOLogic.mk_Trueprop (mk_mor UNIVs ctors active_UNIVs ss (map (mk_fold Ts ss) ks)))
  1136             (K (mk_mor_fold_tac cT ct fold_defs ex_mor (mor_comp RS mor_cong))))
  1137         |> Thm.close_derivation
  1138       end;
  1139 
  1140     val ctor_fold_thms = map (fn morE => rule_by_tactic lthy
  1141       ((rtac CollectI THEN' CONJ_WRAP' (K (rtac @{thm subset_UNIV})) (1 upto m + n)) 1)
  1142       (mor_fold_thm RS morE)) morE_thms;
  1143 
  1144     val (fold_unique_mor_thms, fold_unique_mor_thm) =
  1145       let
  1146         val prem = HOLogic.mk_Trueprop (mk_mor UNIVs ctors active_UNIVs ss fs);
  1147         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_fold Ts ss i);
  1148         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj (map2 mk_fun_eq fs ks));
  1149         val unique_mor = Goal.prove_sorry lthy [] []
  1150           (fold_rev Logic.all (ss @ fs) (Logic.mk_implies (prem, unique)))
  1151           (K (mk_fold_unique_mor_tac type_defs init_unique_mor_thms Reps
  1152             mor_comp_thm mor_Abs_thm mor_fold_thm))
  1153           |> Thm.close_derivation;
  1154       in
  1155         `split_conj_thm unique_mor
  1156       end;
  1157 
  1158     val (ctor_fold_unique_thms, ctor_fold_unique_thm) =
  1159       `split_conj_thm (mk_conjIN n RS
  1160         (mor_UNIV_thm RS iffD2 RS fold_unique_mor_thm))
  1161 
  1162     val fold_ctor_thms =
  1163       map (fn thm => (mor_incl_thm OF replicate n @{thm subset_UNIV}) RS thm RS sym)
  1164         fold_unique_mor_thms;
  1165 
  1166     val ctor_o_fold_thms =
  1167       let
  1168         val mor = mor_comp_thm OF [mor_fold_thm, mor_str_thm];
  1169       in
  1170         map2 (fn unique => fn fold_ctor =>
  1171           trans OF [mor RS unique, fold_ctor]) fold_unique_mor_thms fold_ctor_thms
  1172       end;
  1173 
  1174     val timer = time (timer "fold definitions & thms");
  1175 
  1176     val map_ctors = map2 (fn Ds => fn bnf =>
  1177       Term.list_comb (mk_map_of_bnf Ds (passiveAs @ FTs) (passiveAs @ Ts) bnf,
  1178         map HOLogic.id_const passiveAs @ ctors)) Dss bnfs;
  1179 
  1180     fun dtor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (dtorN ^ "_");
  1181     val dtor_def_bind = rpair [] o Binding.conceal o Thm.def_binding o dtor_bind;
  1182 
  1183     fun dtor_spec i = mk_fold Ts map_ctors i;
  1184 
  1185     val ((dtor_frees, (_, dtor_def_frees)), (lthy, lthy_old)) =
  1186       lthy
  1187       |> fold_map (fn i =>
  1188         Local_Theory.define ((dtor_bind i, NoSyn), (dtor_def_bind i, dtor_spec i))) ks
  1189       |>> apsnd split_list o split_list
  1190       ||> `Local_Theory.restore;
  1191 
  1192     val phi = Proof_Context.export_morphism lthy_old lthy;
  1193     fun mk_dtors params =
  1194       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ params) o Morphism.term phi)
  1195         dtor_frees;
  1196     val dtors = mk_dtors params';
  1197     val dtor_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) dtor_def_frees;
  1198 
  1199     val ctor_o_dtor_thms = map2 (fold_thms lthy o single) dtor_defs ctor_o_fold_thms;
  1200 
  1201     val dtor_o_ctor_thms =
  1202       let
  1203         fun mk_goal dtor ctor FT =
  1204           mk_Trueprop_eq (HOLogic.mk_comp (dtor, ctor), HOLogic.id_const FT);
  1205         val goals = map3 mk_goal dtors ctors FTs;
  1206       in
  1207         map5 (fn goal => fn dtor_def => fn foldx => fn map_comp_id => fn map_cong0L =>
  1208           Goal.prove_sorry lthy [] [] goal
  1209             (K (mk_dtor_o_ctor_tac dtor_def foldx map_comp_id map_cong0L ctor_o_fold_thms))
  1210           |> Thm.close_derivation)
  1211         goals dtor_defs ctor_fold_thms map_comp_id_thms map_cong0L_thms
  1212       end;
  1213 
  1214     val dtor_ctor_thms = map (fn thm => thm RS @{thm pointfree_idE}) dtor_o_ctor_thms;
  1215     val ctor_dtor_thms = map (fn thm => thm RS @{thm pointfree_idE}) ctor_o_dtor_thms;
  1216 
  1217     val bij_dtor_thms =
  1218       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) ctor_o_dtor_thms dtor_o_ctor_thms;
  1219     val inj_dtor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_dtor_thms;
  1220     val surj_dtor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_dtor_thms;
  1221     val dtor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_dtor_thms;
  1222     val dtor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_dtor_thms;
  1223     val dtor_exhaust_thms = map (fn thm => thm RS exE) dtor_nchotomy_thms;
  1224 
  1225     val bij_ctor_thms =
  1226       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) dtor_o_ctor_thms ctor_o_dtor_thms;
  1227     val inj_ctor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_ctor_thms;
  1228     val surj_ctor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_ctor_thms;
  1229     val ctor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_ctor_thms;
  1230     val ctor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_ctor_thms;
  1231     val ctor_exhaust_thms = map (fn thm => thm RS exE) ctor_nchotomy_thms;
  1232 
  1233     val timer = time (timer "dtor definitions & thms");
  1234 
  1235     val fst_rec_pair_thms =
  1236       let
  1237         val mor = mor_comp_thm OF [mor_fold_thm, mor_convol_thm];
  1238       in
  1239         map2 (fn unique => fn fold_ctor =>
  1240           trans OF [mor RS unique, fold_ctor]) fold_unique_mor_thms fold_ctor_thms
  1241       end;
  1242 
  1243     fun rec_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctor_recN ^ "_");
  1244     val rec_def_bind = rpair [] o Binding.conceal o Thm.def_binding o rec_bind;
  1245 
  1246     val rec_strs =
  1247       map3 (fn ctor => fn prod_s => fn mapx =>
  1248         mk_convol (HOLogic.mk_comp (ctor, Term.list_comb (mapx, passive_ids @ rec_fsts)), prod_s))
  1249       ctors rec_ss rec_maps;
  1250 
  1251     fun rec_spec i T AT =
  1252       fold_rev (Term.absfree o Term.dest_Free) rec_ss
  1253         (HOLogic.mk_comp (snd_const (HOLogic.mk_prodT (T, AT)), mk_fold Ts rec_strs i));
  1254 
  1255     val ((rec_frees, (_, rec_def_frees)), (lthy, lthy_old)) =
  1256       lthy
  1257       |> fold_map3 (fn i => fn T => fn AT =>
  1258         Local_Theory.define ((rec_bind i, NoSyn), (rec_def_bind i, rec_spec i T AT))) ks Ts activeAs
  1259       |>> apsnd split_list o split_list
  1260       ||> `Local_Theory.restore;
  1261 
  1262     val phi = Proof_Context.export_morphism lthy_old lthy;
  1263     val recs = map (Morphism.term phi) rec_frees;
  1264     val rec_names = map (fst o dest_Const) recs;
  1265     fun mk_rec ss i = Term.list_comb (Const (nth rec_names (i - 1), Library.foldr (op -->)
  1266       (map fastype_of ss, nth Ts (i - 1) --> range_type (fastype_of (nth ss (i - 1))))), ss);
  1267     val rec_defs = map (fn def =>
  1268       mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) rec_def_frees;
  1269 
  1270     val convols = map2 (fn T => fn i => mk_convol (HOLogic.id_const T, mk_rec rec_ss i)) Ts ks;
  1271     val ctor_rec_thms =
  1272       let
  1273         fun mk_goal i rec_s rec_map ctor x =
  1274           let
  1275             val lhs = mk_rec rec_ss i $ (ctor $ x);
  1276             val rhs = rec_s $ (Term.list_comb (rec_map, passive_ids @ convols) $ x);
  1277           in
  1278             fold_rev Logic.all (x :: rec_ss) (mk_Trueprop_eq (lhs, rhs))
  1279           end;
  1280         val goals = map5 mk_goal ks rec_ss rec_maps_rev ctors xFs;
  1281       in
  1282         map2 (fn goal => fn foldx =>
  1283           Goal.prove_sorry lthy [] [] goal
  1284             (fn {context = ctxt, prems = _} => mk_rec_tac ctxt rec_defs foldx fst_rec_pair_thms)
  1285           |> Thm.close_derivation)
  1286         goals ctor_fold_thms
  1287       end;
  1288 
  1289     val rec_unique_mor_thm =
  1290       let
  1291         val id_fs = map2 (fn T => fn f => mk_convol (HOLogic.id_const T, f)) Ts fs;
  1292         val prem = HOLogic.mk_Trueprop (mk_mor UNIVs ctors rec_UNIVs rec_strs id_fs);
  1293         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_rec rec_ss i);
  1294         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj (map2 mk_fun_eq fs ks));
  1295       in
  1296         Goal.prove_sorry lthy [] []
  1297           (fold_rev Logic.all (rec_ss @ fs) (Logic.mk_implies (prem, unique)))
  1298           (fn {context = ctxt, prems = _} => mk_rec_unique_mor_tac ctxt rec_defs fst_rec_pair_thms
  1299             fold_unique_mor_thm)
  1300           |> Thm.close_derivation
  1301       end;
  1302 
  1303     val (ctor_rec_unique_thms, ctor_rec_unique_thm) =
  1304       `split_conj_thm (split_conj_prems n
  1305         (mor_UNIV_thm RS iffD2 RS rec_unique_mor_thm)
  1306         |> Local_Defs.unfold lthy (@{thms convol_o comp_id id_comp comp_assoc fst_convol} @
  1307            map_id0s @ sym_map_comps) OF replicate n @{thm arg_cong2[of _ _ _ _ convol, OF refl]});
  1308 
  1309     val timer = time (timer "rec definitions & thms");
  1310 
  1311     val (ctor_induct_thm, induct_params) =
  1312       let
  1313         fun mk_prem phi ctor sets x =
  1314           let
  1315             fun mk_IH phi set z =
  1316               let
  1317                 val prem = HOLogic.mk_Trueprop (HOLogic.mk_mem (z, set $ x));
  1318                 val concl = HOLogic.mk_Trueprop (phi $ z);
  1319               in
  1320                 Logic.all z (Logic.mk_implies (prem, concl))
  1321               end;
  1322 
  1323             val IHs = map3 mk_IH phis (drop m sets) Izs;
  1324             val concl = HOLogic.mk_Trueprop (phi $ (ctor $ x));
  1325           in
  1326             Logic.all x (Logic.list_implies (IHs, concl))
  1327           end;
  1328 
  1329         val prems = map4 mk_prem phis ctors FTs_setss xFs;
  1330 
  1331         fun mk_concl phi z = phi $ z;
  1332         val concl =
  1333           HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj (map2 mk_concl phis Izs));
  1334 
  1335         val goal = Logic.list_implies (prems, concl);
  1336       in
  1337         (Goal.prove_sorry lthy [] []
  1338           (fold_rev Logic.all (phis @ Izs) goal)
  1339           (K (mk_ctor_induct_tac lthy m set_mapss init_induct_thm morE_thms mor_Abs_thm
  1340             Rep_inverses Abs_inverses Reps))
  1341         |> Thm.close_derivation,
  1342         rev (Term.add_tfrees goal []))
  1343       end;
  1344 
  1345     val cTs = map (SOME o certifyT lthy o TFree) induct_params;
  1346 
  1347     val weak_ctor_induct_thms =
  1348       let fun insts i = (replicate (i - 1) TrueI) @ (asm_rl :: replicate (n - i) TrueI);
  1349       in map (fn i => (ctor_induct_thm OF insts i) RS mk_conjunctN n i) ks end;
  1350 
  1351     val (ctor_induct2_thm, induct2_params) =
  1352       let
  1353         fun mk_prem phi ctor ctor' sets sets' x y =
  1354           let
  1355             fun mk_IH phi set set' z1 z2 =
  1356               let
  1357                 val prem1 = HOLogic.mk_Trueprop (HOLogic.mk_mem (z1, (set $ x)));
  1358                 val prem2 = HOLogic.mk_Trueprop (HOLogic.mk_mem (z2, (set' $ y)));
  1359                 val concl = HOLogic.mk_Trueprop (phi $ z1 $ z2);
  1360               in
  1361                 fold_rev Logic.all [z1, z2] (Logic.list_implies ([prem1, prem2], concl))
  1362               end;
  1363 
  1364             val IHs = map5 mk_IH phi2s (drop m sets) (drop m sets') Izs1 Izs2;
  1365             val concl = HOLogic.mk_Trueprop (phi $ (ctor $ x) $ (ctor' $ y));
  1366           in
  1367             fold_rev Logic.all [x, y] (Logic.list_implies (IHs, concl))
  1368           end;
  1369 
  1370         val prems = map7 mk_prem phi2s ctors ctor's FTs_setss FTs'_setss xFs yFs;
  1371 
  1372         fun mk_concl phi z1 z2 = phi $ z1 $ z2;
  1373         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1374           (map3 mk_concl phi2s Izs1 Izs2));
  1375         fun mk_t phi (z1, z1') (z2, z2') =
  1376           Term.absfree z1' (HOLogic.mk_all (fst z2', snd z2', phi $ z1 $ z2));
  1377         val cts = map3 (SOME o certify lthy ooo mk_t) phi2s (Izs1 ~~ Izs1') (Izs2 ~~ Izs2');
  1378         val goal = Logic.list_implies (prems, concl);
  1379       in
  1380         (singleton (Proof_Context.export names_lthy lthy)
  1381           (Goal.prove_sorry lthy [] [] goal
  1382             (fn {context = ctxt, prems = _} => mk_ctor_induct2_tac ctxt cTs cts ctor_induct_thm
  1383               weak_ctor_induct_thms))
  1384           |> Thm.close_derivation,
  1385         rev (Term.add_tfrees goal []))
  1386       end;
  1387 
  1388     val timer = time (timer "induction");
  1389 
  1390     fun mk_ctor_map_DEADID_thm ctor_inject map_id0 =
  1391       trans OF [id_apply, iffD2 OF [ctor_inject, map_id0 RS sym]];
  1392 
  1393     fun mk_ctor_Irel_DEADID_thm ctor_inject bnf =
  1394       trans OF [ctor_inject, rel_eq_of_bnf bnf RS @{thm predicate2_eqD} RS sym];
  1395 
  1396     val IphiTs = map2 mk_pred2T passiveAs passiveBs;
  1397     val Ipsi1Ts = map2 mk_pred2T passiveAs passiveCs;
  1398     val Ipsi2Ts = map2 mk_pred2T passiveCs passiveBs;
  1399     val activephiTs = map2 mk_pred2T activeAs activeBs;
  1400     val activeIphiTs = map2 mk_pred2T Ts Ts';
  1401     val (((((Iphis, Ipsi1s), Ipsi2s), activephis), activeIphis), names_lthy) = names_lthy
  1402       |> mk_Frees "R" IphiTs
  1403       ||>> mk_Frees "R" Ipsi1Ts
  1404       ||>> mk_Frees "Q" Ipsi2Ts
  1405       ||>> mk_Frees "S" activephiTs
  1406       ||>> mk_Frees "IR" activeIphiTs;
  1407     val rels = map2 (fn Ds => mk_rel_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  1408 
  1409     (*register new datatypes as BNFs*)
  1410     val (timer, Ibnfs, (ctor_Imap_o_thms, ctor_Imap_thms), ctor_Iset_thmss',
  1411         ctor_Irel_thms, Ibnf_notes, lthy) =
  1412       if m = 0 then
  1413         (timer, replicate n DEADID_bnf,
  1414         map_split (`(mk_pointfree lthy)) (map2 mk_ctor_map_DEADID_thm ctor_inject_thms map_ids),
  1415         replicate n [], map2 mk_ctor_Irel_DEADID_thm ctor_inject_thms bnfs, [], lthy)
  1416       else let
  1417         val fTs = map2 (curry op -->) passiveAs passiveBs;
  1418         val uTs = map2 (curry op -->) Ts Ts';
  1419 
  1420         val (((((fs, fs'), fs_copy), us), (ys, ys')),
  1421           names_lthy) = names_lthy
  1422           |> mk_Frees' "f" fTs
  1423           ||>> mk_Frees "f" fTs
  1424           ||>> mk_Frees "u" uTs
  1425           ||>> mk_Frees' "y" passiveAs;
  1426 
  1427         val map_FTFT's = map2 (fn Ds =>
  1428           mk_map_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  1429         fun mk_passive_maps ATs BTs Ts =
  1430           map2 (fn Ds => mk_map_of_bnf Ds (ATs @ Ts) (BTs @ Ts)) Dss bnfs;
  1431         fun mk_map_fold_arg fs Ts ctor fmap =
  1432           HOLogic.mk_comp (ctor, Term.list_comb (fmap, fs @ map HOLogic.id_const Ts));
  1433         fun mk_map Ts fs Ts' ctors mk_maps =
  1434           mk_fold Ts (map2 (mk_map_fold_arg fs Ts') ctors (mk_maps Ts'));
  1435         val pmapsABT' = mk_passive_maps passiveAs passiveBs;
  1436         val fs_maps = map (mk_map Ts fs Ts' ctor's pmapsABT') ks;
  1437 
  1438         val ls = 1 upto m;
  1439         val setsss = map (mk_setss o mk_set_Ts) passiveAs;
  1440 
  1441         fun mk_col l T z z' sets =
  1442           let
  1443             fun mk_UN set = mk_Union T $ (set $ z);
  1444           in
  1445             Term.absfree z'
  1446               (mk_union (nth sets (l - 1) $ z,
  1447                 Library.foldl1 mk_union (map mk_UN (drop m sets))))
  1448           end;
  1449 
  1450         val colss = map5 (fn l => fn T => map3 (mk_col l T)) ls passiveAs AFss AFss' setsss;
  1451         val setss_by_range = map (fn cols => map (mk_fold Ts cols) ks) colss;
  1452         val setss_by_bnf = transpose setss_by_range;
  1453 
  1454         val set_bss =
  1455           map (flat o map2 (fn B => fn b =>
  1456             if member (op =) deads (TFree B) then [] else [b]) resBs) set_bss0;
  1457 
  1458         val ctor_witss =
  1459           let
  1460             val witss = map2 (fn Ds => fn bnf => mk_wits_of_bnf
  1461               (replicate (nwits_of_bnf bnf) Ds)
  1462               (replicate (nwits_of_bnf bnf) (passiveAs @ Ts)) bnf) Dss bnfs;
  1463             fun close_wit (I, wit) = fold_rev Term.absfree (map (nth ys') I) wit;
  1464             fun wit_apply (arg_I, arg_wit) (fun_I, fun_wit) =
  1465               (union (op =) arg_I fun_I, fun_wit $ arg_wit);
  1466 
  1467             fun gen_arg support i =
  1468               if i < m then [([i], nth ys i)]
  1469               else maps (mk_wit support (nth ctors (i - m)) (i - m)) (nth support (i - m))
  1470             and mk_wit support ctor i (I, wit) =
  1471               let val args = map (gen_arg (nth_map i (remove (op =) (I, wit)) support)) I;
  1472               in
  1473                 (args, [([], wit)])
  1474                 |-> fold (map_product wit_apply)
  1475                 |> map (apsnd (fn t => ctor $ t))
  1476                 |> minimize_wits
  1477               end;
  1478           in
  1479             map3 (fn ctor => fn i => map close_wit o minimize_wits o maps (mk_wit witss ctor i))
  1480               ctors (0 upto n - 1) witss
  1481           end;
  1482 
  1483         val (Ibnf_consts, lthy) =
  1484           fold_map8 (fn b => fn map_b => fn rel_b => fn set_bs => fn mapx => fn sets => fn wits =>
  1485               fn T => fn lthy =>
  1486             define_bnf_consts Dont_Inline (user_policy Note_Some lthy) (SOME deads)
  1487               map_b rel_b set_bs
  1488               ((((((b, T), fold_rev Term.absfree fs' mapx), sets), sbd), wits), NONE) lthy)
  1489           bs map_bs rel_bs set_bss fs_maps setss_by_bnf ctor_witss Ts lthy;
  1490 
  1491         val (_, Iconsts, Iconst_defs, mk_Iconsts) = split_list4 Ibnf_consts;
  1492         val (_, Isetss, Ibds_Ds, Iwitss_Ds, _) = split_list5 Iconsts;
  1493         val (Imap_defs, Iset_defss, Ibd_defs, Iwit_defss, Irel_defs) = split_list5 Iconst_defs;
  1494         val (mk_Imaps_Ds, mk_It_Ds, _, mk_Irels_Ds, _) = split_list5 mk_Iconsts;
  1495 
  1496         val Irel_unabs_defs = map (fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) Irel_defs;
  1497         val Iset_defs = flat Iset_defss;
  1498 
  1499         fun mk_Imaps As Bs = map (fn mk => mk deads As Bs) mk_Imaps_Ds;
  1500         fun mk_Isetss As = map2 (fn mk => fn Isets => map (mk deads As) Isets) mk_It_Ds Isetss;
  1501         val Ibds = map2 (fn mk => mk deads passiveAs) mk_It_Ds Ibds_Ds;
  1502         val Iwitss =
  1503           map2 (fn mk => fn Iwits => map (mk deads passiveAs o snd) Iwits) mk_It_Ds Iwitss_Ds;
  1504         fun mk_Irels As Bs = map (fn mk => mk deads As Bs) mk_Irels_Ds;
  1505 
  1506         val Imaps = mk_Imaps passiveAs passiveBs;
  1507         val fs_Imaps = map (fn m => Term.list_comb (m, fs)) Imaps;
  1508         val fs_copy_Imaps = map (fn m => Term.list_comb (m, fs_copy)) Imaps;
  1509         val (Isetss_by_range, Isetss_by_bnf) = `transpose (mk_Isetss passiveAs);
  1510 
  1511         val map_setss = map (fn T => map2 (fn Ds =>
  1512           mk_map_of_bnf Ds (passiveAs @ Ts) (mk_set_Ts T)) Dss bnfs) passiveAs;
  1513 
  1514         val timer = time (timer "bnf constants for the new datatypes");
  1515 
  1516         val (ctor_Imap_thms, ctor_Imap_o_thms) =
  1517           let
  1518             fun mk_goal fs_map map ctor ctor' = fold_rev Logic.all fs
  1519               (mk_Trueprop_eq (HOLogic.mk_comp (fs_map, ctor),
  1520                 HOLogic.mk_comp (ctor', Term.list_comb (map, fs @ fs_Imaps))));
  1521             val goals = map4 mk_goal fs_Imaps map_FTFT's ctors ctor's;
  1522             val maps =
  1523               map4 (fn goal => fn foldx => fn map_comp_id => fn map_cong0 =>
  1524                 Goal.prove_sorry lthy [] [] goal
  1525                   (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Imap_defs THEN
  1526                     mk_map_tac m n foldx map_comp_id map_cong0)
  1527                 |> Thm.close_derivation)
  1528               goals ctor_fold_thms map_comp_id_thms map_cong0s;
  1529           in
  1530             `(map (fn thm => thm RS @{thm comp_eq_dest})) maps
  1531           end;
  1532 
  1533         val (ctor_Imap_unique_thms, ctor_Imap_unique_thm) =
  1534           let
  1535             fun mk_prem u map ctor ctor' =
  1536               mk_Trueprop_eq (HOLogic.mk_comp (u, ctor),
  1537                 HOLogic.mk_comp (ctor', Term.list_comb (map, fs @ us)));
  1538             val prems = map4 mk_prem us map_FTFT's ctors ctor's;
  1539             val goal =
  1540               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1541                 (map2 (curry HOLogic.mk_eq) us fs_Imaps));
  1542             val unique = Goal.prove_sorry lthy [] []
  1543               (fold_rev Logic.all (us @ fs) (Logic.list_implies (prems, goal)))
  1544               (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Imap_defs THEN
  1545                 mk_ctor_map_unique_tac ctxt ctor_fold_unique_thm sym_map_comps)
  1546               |> Thm.close_derivation;
  1547           in
  1548             `split_conj_thm unique
  1549           end;
  1550 
  1551         val timer = time (timer "map functions for the new datatypes");
  1552 
  1553         val ctor_Iset_thmss =
  1554           let
  1555             fun mk_goal sets ctor set col map =
  1556               mk_Trueprop_eq (HOLogic.mk_comp (set, ctor),
  1557                 HOLogic.mk_comp (col, Term.list_comb (map, passive_ids @ sets)));
  1558             val goalss =
  1559               map3 (fn sets => map4 (mk_goal sets) ctors sets) Isetss_by_range colss map_setss;
  1560             val setss = map (map2 (fn foldx => fn goal =>
  1561                 Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, prems = _} =>
  1562                   unfold_thms_tac ctxt Iset_defs THEN mk_set_tac foldx)
  1563                 |> Thm.close_derivation)
  1564               ctor_fold_thms) goalss;
  1565 
  1566             fun mk_simp_goal pas_set act_sets sets ctor z set =
  1567               Logic.all z (mk_Trueprop_eq (set $ (ctor $ z),
  1568                 mk_union (pas_set $ z,
  1569                   Library.foldl1 mk_union (map2 (fn X => mk_UNION (X $ z)) act_sets sets))));
  1570             val simp_goalss =
  1571               map2 (fn i => fn sets =>
  1572                 map4 (fn Fsets => mk_simp_goal (nth Fsets (i - 1)) (drop m Fsets) sets)
  1573                   FTs_setss ctors xFs sets)
  1574                 ls Isetss_by_range;
  1575 
  1576             val ctor_setss = map3 (fn i => map3 (fn set_nats => fn goal => fn set =>
  1577                 Goal.prove_sorry lthy [] [] goal
  1578                   (K (mk_ctor_set_tac set (nth set_nats (i - 1)) (drop m set_nats)))
  1579                 |> Thm.close_derivation)
  1580               set_mapss) ls simp_goalss setss;
  1581           in
  1582             ctor_setss
  1583           end;
  1584 
  1585         fun mk_set_thms ctor_set = (@{thm xt1(3)} OF [ctor_set, @{thm Un_upper1}]) ::
  1586           map (fn i => (@{thm xt1(3)} OF [ctor_set, @{thm Un_upper2}]) RS
  1587             (mk_Un_upper n i RS subset_trans) RSN
  1588             (2, @{thm UN_upper} RS subset_trans))
  1589             (1 upto n);
  1590         val set_Iset_thmsss = transpose (map (map mk_set_thms) ctor_Iset_thmss);
  1591 
  1592         val timer = time (timer "set functions for the new datatypes");
  1593 
  1594         val cxs = map (SOME o certify lthy) Izs;
  1595         val Isetss_by_range' =
  1596           map (map (Term.subst_atomic_types (passiveAs ~~ passiveBs))) Isetss_by_range;
  1597 
  1598         val Iset_Imap0_thmss =
  1599           let
  1600             fun mk_set_map0 f map z set set' =
  1601               HOLogic.mk_eq (mk_image f $ (set $ z), set' $ (map $ z));
  1602 
  1603             fun mk_cphi f map z set set' = certify lthy
  1604               (Term.absfree (dest_Free z) (mk_set_map0 f map z set set'));
  1605 
  1606             val csetss = map (map (certify lthy)) Isetss_by_range';
  1607 
  1608             val cphiss = map3 (fn f => fn sets => fn sets' =>
  1609               (map4 (mk_cphi f) fs_Imaps Izs sets sets')) fs Isetss_by_range Isetss_by_range';
  1610 
  1611             val inducts = map (fn cphis =>
  1612               Drule.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm) cphiss;
  1613 
  1614             val goals =
  1615               map3 (fn f => fn sets => fn sets' =>
  1616                 HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1617                   (map4 (mk_set_map0 f) fs_Imaps Izs sets sets')))
  1618                   fs Isetss_by_range Isetss_by_range';
  1619 
  1620             fun mk_tac ctxt induct = mk_set_nat_tac ctxt m (rtac induct) set_mapss ctor_Imap_thms;
  1621             val thms =
  1622               map5 (fn goal => fn csets => fn ctor_sets => fn induct => fn i =>
  1623                 singleton (Proof_Context.export names_lthy lthy)
  1624                   (Goal.prove_sorry lthy [] [] goal
  1625                     (fn {context = ctxt, prems = _} => mk_tac ctxt induct csets ctor_sets i))
  1626                 |> Thm.close_derivation)
  1627               goals csetss ctor_Iset_thmss inducts ls;
  1628           in
  1629             map split_conj_thm thms
  1630           end;
  1631 
  1632         val Iset_bd_thmss =
  1633           let
  1634             fun mk_set_bd z bd set = mk_ordLeq (mk_card_of (set $ z)) bd;
  1635 
  1636             fun mk_cphi z set = certify lthy (Term.absfree (dest_Free z) (mk_set_bd z sbd set));
  1637 
  1638             val cphiss = map (map2 mk_cphi Izs) Isetss_by_range;
  1639 
  1640             val inducts = map (fn cphis =>
  1641               Drule.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm) cphiss;
  1642 
  1643             val goals =
  1644               map (fn sets =>
  1645                 HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1646                   (map3 mk_set_bd Izs Ibds sets))) Isetss_by_range;
  1647 
  1648             fun mk_tac ctxt induct = mk_set_bd_tac ctxt m (rtac induct) sbd_Cinfinite set_sbdss;
  1649             val thms =
  1650               map4 (fn goal => fn ctor_sets => fn induct => fn i =>
  1651                 singleton (Proof_Context.export names_lthy lthy)
  1652                   (Goal.prove_sorry lthy [] [] goal
  1653                     (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Ibd_defs THEN
  1654                       mk_tac ctxt induct ctor_sets i))
  1655                 |> Thm.close_derivation)
  1656               goals ctor_Iset_thmss inducts ls;
  1657           in
  1658             map split_conj_thm thms
  1659           end;
  1660 
  1661         val Imap_cong0_thms =
  1662           let
  1663             fun mk_prem z set f g y y' =
  1664               mk_Ball (set $ z) (Term.absfree y' (HOLogic.mk_eq (f $ y, g $ y)));
  1665 
  1666             fun mk_map_cong0 sets z fmap gmap =
  1667               HOLogic.mk_imp
  1668                 (Library.foldr1 HOLogic.mk_conj (map5 (mk_prem z) sets fs fs_copy ys ys'),
  1669                 HOLogic.mk_eq (fmap $ z, gmap $ z));
  1670 
  1671             fun mk_cphi sets z fmap gmap =
  1672               certify lthy (Term.absfree (dest_Free z) (mk_map_cong0 sets z fmap gmap));
  1673 
  1674             val cphis = map4 mk_cphi Isetss_by_bnf Izs fs_Imaps fs_copy_Imaps;
  1675 
  1676             val induct = Drule.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm;
  1677 
  1678             val goal =
  1679               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1680                 (map4 mk_map_cong0 Isetss_by_bnf Izs fs_Imaps fs_copy_Imaps));
  1681 
  1682             val thm = singleton (Proof_Context.export names_lthy lthy)
  1683               (Goal.prove_sorry lthy [] [] goal
  1684               (fn {context = ctxt, prems = _} => mk_mcong_tac ctxt (rtac induct) set_Iset_thmsss
  1685                 map_cong0s ctor_Imap_thms))
  1686               |> Thm.close_derivation;
  1687           in
  1688             split_conj_thm thm
  1689           end;
  1690 
  1691         val in_rels = map in_rel_of_bnf bnfs;
  1692         val in_Irels = map (fn def => trans OF [def, @{thm OO_Grp_alt}] RS @{thm predicate2_eqD})
  1693             Irel_unabs_defs;
  1694 
  1695         val ctor_Iset_incl_thmss = map (map hd) set_Iset_thmsss;
  1696         val ctor_set_Iset_incl_thmsss = map (transpose o map tl) set_Iset_thmsss;
  1697         val ctor_Iset_thmss' = transpose ctor_Iset_thmss;
  1698 
  1699         val Irels = mk_Irels passiveAs passiveBs;
  1700         val Irelphis = map (fn rel => Term.list_comb (rel, Iphis)) Irels;
  1701         val relphis = map (fn rel => Term.list_comb (rel, Iphis @ Irelphis)) rels;
  1702         val Irelpsi1s = map (fn rel => Term.list_comb (rel, Ipsi1s)) (mk_Irels passiveAs passiveCs);
  1703         val Irelpsi2s = map (fn rel => Term.list_comb (rel, Ipsi2s)) (mk_Irels passiveCs passiveBs);
  1704         val Irelpsi12s = map (fn rel =>
  1705             Term.list_comb (rel, map2 (curry mk_rel_compp) Ipsi1s Ipsi2s)) Irels;
  1706 
  1707         val ctor_Irel_thms =
  1708           let
  1709             fun mk_goal xF yF ctor ctor' Irelphi relphi = fold_rev Logic.all (xF :: yF :: Iphis)
  1710               (mk_Trueprop_eq (Irelphi $ (ctor $ xF) $ (ctor' $ yF), relphi $ xF $ yF));
  1711             val goals = map6 mk_goal xFs yFs ctors ctor's Irelphis relphis;
  1712           in
  1713             map12 (fn i => fn goal => fn in_rel => fn map_comp0 => fn map_cong0 =>
  1714               fn ctor_map => fn ctor_sets => fn ctor_inject => fn ctor_dtor =>
  1715               fn set_map0s => fn ctor_set_incls => fn ctor_set_set_inclss =>
  1716               Goal.prove_sorry lthy [] [] goal
  1717                (K (mk_ctor_rel_tac lthy in_Irels i in_rel map_comp0 map_cong0 ctor_map ctor_sets
  1718                  ctor_inject ctor_dtor set_map0s ctor_set_incls ctor_set_set_inclss))
  1719               |> Thm.close_derivation)
  1720             ks goals in_rels map_comps map_cong0s ctor_Imap_thms ctor_Iset_thmss'
  1721               ctor_inject_thms ctor_dtor_thms set_mapss ctor_Iset_incl_thmss
  1722               ctor_set_Iset_incl_thmsss
  1723           end;
  1724 
  1725         val le_Irel_OO_thm =
  1726           let
  1727             fun mk_le_Irel_OO Irelpsi1 Irelpsi2 Irelpsi12 Iz1 Iz2 =
  1728               HOLogic.mk_imp (mk_rel_compp (Irelpsi1, Irelpsi2) $ Iz1 $ Iz2,
  1729                 Irelpsi12 $ Iz1 $ Iz2);
  1730             val goals = map5 mk_le_Irel_OO Irelpsi1s Irelpsi2s Irelpsi12s Izs1 Izs2;
  1731 
  1732             val cTs = map (SOME o certifyT lthy o TFree) induct2_params;
  1733             val cxs = map (SOME o certify lthy) (splice Izs1 Izs2);
  1734             fun mk_cphi z1 z2 goal = SOME (certify lthy (Term.absfree z1 (Term.absfree z2 goal)));
  1735             val cphis = map3 mk_cphi Izs1' Izs2' goals;
  1736             val induct = Drule.instantiate' cTs (cphis @ cxs) ctor_induct2_thm;
  1737 
  1738             val goal = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj goals);
  1739           in
  1740             singleton (Proof_Context.export names_lthy lthy)
  1741               (Goal.prove_sorry lthy [] [] goal
  1742                 (fn {context = ctxt, prems = _} => mk_le_rel_OO_tac ctxt m induct ctor_nchotomy_thms
  1743                   ctor_Irel_thms rel_mono_strongs rel_OOs))
  1744               |> Thm.close_derivation
  1745           end;
  1746 
  1747         val timer = time (timer "helpers for BNF properties");
  1748 
  1749         val map_id0_tacs = map (K o mk_map_id0_tac map_id0s) ctor_Imap_unique_thms;
  1750         val map_comp0_tacs =
  1751           map2 (K oo mk_map_comp0_tac map_comps ctor_Imap_thms) ctor_Imap_unique_thms ks;
  1752         val map_cong0_tacs = map (fn thm => fn ctxt => mk_map_cong0_tac ctxt m thm) Imap_cong0_thms;
  1753         val set_map0_tacss = map (map (K o mk_set_map0_tac)) (transpose Iset_Imap0_thmss);
  1754         val bd_co_tacs = replicate n (fn ctxt =>
  1755           unfold_thms_tac ctxt Ibd_defs THEN rtac sbd_card_order 1);
  1756         val bd_cinf_tacs = replicate n (fn ctxt =>
  1757           unfold_thms_tac ctxt Ibd_defs THEN rtac (sbd_Cinfinite RS conjunct1) 1);
  1758         val set_bd_tacss = map (map (fn thm => K (rtac thm 1))) (transpose Iset_bd_thmss);
  1759         val le_rel_OO_tacs = map (fn i =>
  1760           K ((rtac @{thm predicate2I} THEN' etac (le_Irel_OO_thm RS mk_conjunctN n i RS mp)) 1)) ks;
  1761 
  1762         val rel_OO_Grp_tacs = map (fn def => K (rtac def 1)) Irel_unabs_defs;
  1763 
  1764         val tacss = map9 zip_axioms map_id0_tacs map_comp0_tacs map_cong0_tacs set_map0_tacss
  1765           bd_co_tacs bd_cinf_tacs set_bd_tacss le_rel_OO_tacs rel_OO_Grp_tacs;
  1766 
  1767         fun wit_tac ctxt = unfold_thms_tac ctxt (flat Iwit_defss) THEN
  1768           mk_wit_tac ctxt n (flat ctor_Iset_thmss) (maps wit_thms_of_bnf bnfs);
  1769 
  1770         val (Ibnfs, lthy) =
  1771           fold_map6 (fn b => fn tacs => fn map_b => fn rel_b => fn set_bs => fn consts => fn lthy =>
  1772             bnf_def Do_Inline (user_policy Note_Some) I tacs wit_tac (SOME deads)
  1773               map_b rel_b set_bs consts lthy
  1774             |> register_bnf (Local_Theory.full_name lthy b))
  1775           bs tacss map_bs rel_bs set_bss
  1776             ((((((bs ~~ Ts) ~~ Imaps) ~~ Isetss_by_bnf) ~~ Ibds) ~~ Iwitss) ~~ map SOME Irels)
  1777             lthy;
  1778 
  1779         val timer = time (timer "registered new datatypes as BNFs");
  1780 
  1781         val ls' = if m = 1 then [0] else ls
  1782 
  1783         val Ibnf_common_notes =
  1784           [(ctor_map_uniqueN, [ctor_Imap_unique_thm])]
  1785           |> map (fn (thmN, thms) =>
  1786             ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  1787 
  1788         val Ibnf_notes =
  1789           [(ctor_mapN, map single ctor_Imap_thms),
  1790           (ctor_relN, map single ctor_Irel_thms),
  1791           (ctor_set_inclN, ctor_Iset_incl_thmss),
  1792           (ctor_set_set_inclN, map flat ctor_set_Iset_incl_thmsss)] @
  1793           map2 (fn i => fn thms => (mk_ctor_setN i, map single thms)) ls' ctor_Iset_thmss
  1794           |> maps (fn (thmN, thmss) =>
  1795             map2 (fn b => fn thms =>
  1796               ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  1797             bs thmss)
  1798       in
  1799         (timer, Ibnfs, (ctor_Imap_o_thms, ctor_Imap_thms), ctor_Iset_thmss',
  1800           ctor_Irel_thms, Ibnf_common_notes @ Ibnf_notes, lthy)
  1801       end;
  1802 
  1803       val ctor_fold_o_Imap_thms = mk_xtor_un_fold_o_map_thms Least_FP false m ctor_fold_unique_thm
  1804         ctor_Imap_o_thms (map (mk_pointfree lthy) ctor_fold_thms) sym_map_comps map_cong0s;
  1805       val ctor_rec_o_Imap_thms = mk_xtor_un_fold_o_map_thms Least_FP true m ctor_rec_unique_thm
  1806         ctor_Imap_o_thms (map (mk_pointfree lthy) ctor_rec_thms) sym_map_comps map_cong0s;
  1807 
  1808       val Irels = if m = 0 then map HOLogic.eq_const Ts
  1809         else map (mk_rel_of_bnf deads passiveAs passiveBs) Ibnfs;
  1810       val Irel_induct_thm =
  1811         mk_rel_xtor_co_induct_thm Least_FP rels activeIphis Irels Iphis xFs yFs ctors ctor's
  1812           (fn {context = ctxt, prems = IHs} => mk_rel_induct_tac ctxt IHs m ctor_induct2_thm ks
  1813              ctor_Irel_thms rel_mono_strongs) lthy;
  1814 
  1815       val rels = map2 (fn Ds => mk_rel_of_bnf Ds allAs allBs') Dss bnfs;
  1816       val ctor_fold_transfer_thms =
  1817         mk_un_fold_transfer_thms Least_FP rels activephis Irels Iphis
  1818           (mk_folds passiveAs activeAs) (mk_folds passiveBs activeBs)
  1819           (fn {context = ctxt, prems = _} => mk_fold_transfer_tac ctxt m Irel_induct_thm
  1820             (map map_transfer_of_bnf bnfs) ctor_fold_thms)
  1821           lthy;
  1822 
  1823       val timer = time (timer "relator induction");
  1824 
  1825       val common_notes =
  1826         [(ctor_inductN, [ctor_induct_thm]),
  1827         (ctor_induct2N, [ctor_induct2_thm]),
  1828         (rel_inductN, [Irel_induct_thm]),
  1829         (ctor_fold_transferN, ctor_fold_transfer_thms)]
  1830         |> map (fn (thmN, thms) =>
  1831           ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  1832 
  1833       val notes =
  1834         [(ctor_dtorN, ctor_dtor_thms),
  1835         (ctor_exhaustN, ctor_exhaust_thms),
  1836         (ctor_foldN, ctor_fold_thms),
  1837         (ctor_fold_uniqueN, ctor_fold_unique_thms),
  1838         (ctor_rec_uniqueN, ctor_rec_unique_thms),
  1839         (ctor_fold_o_mapN, ctor_fold_o_Imap_thms),
  1840         (ctor_rec_o_mapN, ctor_rec_o_Imap_thms),
  1841         (ctor_injectN, ctor_inject_thms),
  1842         (ctor_recN, ctor_rec_thms),
  1843         (dtor_ctorN, dtor_ctor_thms),
  1844         (dtor_exhaustN, dtor_exhaust_thms),
  1845         (dtor_injectN, dtor_inject_thms)]
  1846         |> map (apsnd (map single))
  1847         |> maps (fn (thmN, thmss) =>
  1848           map2 (fn b => fn thms =>
  1849             ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  1850           bs thmss);
  1851 
  1852     (*FIXME: once the package exports all the necessary high-level characteristic theorems,
  1853        those should not only be concealed but rather not noted at all*)
  1854     val maybe_conceal_notes = note_all = false ? map (apfst (apfst Binding.conceal));
  1855   in
  1856     timer;
  1857     ({Ts = Ts, bnfs = Ibnfs, ctors = ctors, dtors = dtors, xtor_co_recs = recs,
  1858       xtor_co_induct = ctor_induct_thm, dtor_ctors = dtor_ctor_thms,
  1859       ctor_dtors = ctor_dtor_thms, ctor_injects = ctor_inject_thms, dtor_injects = dtor_inject_thms,
  1860       xtor_map_thms = ctor_Imap_thms, xtor_set_thmss = ctor_Iset_thmss',
  1861       xtor_rel_thms = ctor_Irel_thms, xtor_co_rec_thms = ctor_rec_thms,
  1862       xtor_co_rec_o_map_thms = ctor_rec_o_Imap_thms, rel_xtor_co_induct_thm = Irel_induct_thm},
  1863      lthy |> Local_Theory.notes (maybe_conceal_notes (common_notes @ notes @ Ibnf_notes)) |> snd)
  1864   end;
  1865 
  1866 val _ =
  1867   Outer_Syntax.local_theory @{command_spec "datatype_new"} "define new-style inductive datatypes"
  1868     (parse_co_datatype_cmd Least_FP construct_lfp);
  1869 
  1870 end;