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