src/HOL/Codatatype/Tools/bnf_gfp.ML
author blanchet
Tue Sep 04 16:17:22 2012 +0200 (2012-09-04)
changeset 49126 1bbd7a37fc29
parent 49125 5fc5211cf104
child 49128 1a86ef0a0210
permissions -rw-r--r--
implemented "mk_inject_tac"
     1 (*  Title:      HOL/Codatatype/Tools/bnf_gfp.ML
     2     Author:     Dmitriy Traytel, TU Muenchen
     3     Author:     Andrei Popescu, TU Muenchen
     4     Author:     Jasmin Blanchette, TU Muenchen
     5     Copyright   2012
     6 
     7 Codatatype construction.
     8 *)
     9 
    10 signature BNF_GFP =
    11 sig
    12   val bnf_gfp: binding list -> typ list list -> BNF_Def.BNF list -> local_theory ->
    13     (term list * term list * thm list * thm list * thm list) * local_theory
    14 end;
    15 
    16 structure BNF_GFP : BNF_GFP =
    17 struct
    18 
    19 open BNF_Def
    20 open BNF_Util
    21 open BNF_Tactics
    22 open BNF_FP_Util
    23 open BNF_GFP_Util
    24 open BNF_GFP_Tactics
    25 
    26 datatype wit_tree = Leaf of int | Node of (int * int * int list) * wit_tree list;
    27 
    28 fun mk_tree_args (I, T) (I', Ts) = (sort_distinct int_ord (I @ I'), T :: Ts);
    29 
    30 fun finish Iss m seen i (nwit, I) =
    31   let
    32     val treess = map (fn j =>
    33         if j < m orelse member (op =) seen j then [([j], Leaf j)]
    34         else
    35           map_index (finish Iss m (insert (op =) j seen) j) (nth Iss (j - m))
    36           |> flat
    37           |> minimize_wits)
    38       I;
    39   in
    40     map (fn (I, t) => (I, Node ((i - m, nwit, filter (fn i => i < m) I), t)))
    41       (fold_rev (map_product mk_tree_args) treess [([], [])])
    42     |> minimize_wits
    43   end;
    44 
    45 fun tree_to_fld_wit vars _ _ (Leaf j) = ([j], nth vars j)
    46   | tree_to_fld_wit vars flds witss (Node ((i, nwit, I), subtrees)) =
    47      (I, nth flds i $ (Term.list_comb (snd (nth (nth witss i) nwit),
    48        map (snd o tree_to_fld_wit vars flds witss) subtrees)));
    49 
    50 fun tree_to_coind_wits _ (Leaf j) = []
    51   | tree_to_coind_wits lwitss (Node ((i, nwit, I), subtrees)) =
    52      ((i, I), nth (nth lwitss i) nwit) :: maps (tree_to_coind_wits lwitss) subtrees;
    53 
    54 (*all bnfs have the same lives*)
    55 fun bnf_gfp bs Dss_insts bnfs lthy =
    56   let
    57     val timer = time (Timer.startRealTimer ());
    58 
    59     val live = live_of_bnf (hd bnfs);
    60     val n = length bnfs; (*active*)
    61     val ks = 1 upto n;
    62     val m = live - n (*passive, if 0 don't generate a new bnf*);
    63     val ls = 1 upto m;
    64     val b = Binding.name (fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "");
    65 
    66     (* TODO: check if m, n etc are sane *)
    67 
    68     val Dss = map (fn Ds => map TFree (fold Term.add_tfreesT Ds [])) Dss_insts;
    69     val deads = distinct (op =) (flat Dss);
    70     val names_lthy = fold Variable.declare_typ deads lthy;
    71 
    72     (* tvars *)
    73     val ((((((((passiveAs, activeAs), allAs)), (passiveBs, activeBs)),
    74       (passiveCs, activeCs)), passiveXs), passiveYs), idxT) = names_lthy
    75       |> mk_TFrees live
    76       |> apfst (`(chop m))
    77       ||> mk_TFrees live
    78       ||>> apfst (chop m)
    79       ||> mk_TFrees live
    80       ||>> apfst (chop m)
    81       ||>> mk_TFrees m
    82       ||>> mk_TFrees m
    83       ||> fst o mk_TFrees 1
    84       ||> the_single;
    85 
    86     val Ass = replicate n allAs;
    87     val allBs = passiveAs @ activeBs;
    88     val Bss = replicate n allBs;
    89     val allCs = passiveAs @ activeCs;
    90     val allCs' = passiveBs @ activeCs;
    91     val Css' = replicate n allCs';
    92 
    93     (* typs *)
    94     fun mk_FTs Ts = map2 (fn Ds => mk_T_of_bnf Ds Ts) Dss bnfs;
    95     val (params, params') = `(map Term.dest_TFree) (deads @ passiveAs);
    96     val FTsAs = mk_FTs allAs;
    97     val FTsBs = mk_FTs allBs;
    98     val FTsCs = mk_FTs allCs;
    99     val ATs = map HOLogic.mk_setT passiveAs;
   100     val BTs = map HOLogic.mk_setT activeAs;
   101     val B'Ts = map HOLogic.mk_setT activeBs;
   102     val B''Ts = map HOLogic.mk_setT activeCs;
   103     val sTs = map2 (fn T => fn U => T --> U) activeAs FTsAs;
   104     val s'Ts = map2 (fn T => fn U => T --> U) activeBs FTsBs;
   105     val s''Ts = map2 (fn T => fn U => T --> U) activeCs FTsCs;
   106     val fTs = map2 (fn T => fn U => T --> U) activeAs activeBs;
   107     val all_fTs = map2 (fn T => fn U => T --> U) allAs allBs;
   108     val self_fTs = map (fn T => T --> T) activeAs;
   109     val gTs = map2 (fn T => fn U => T --> U) activeBs activeCs;
   110     val all_gTs = map2 (fn T => fn U => T --> U) allBs allCs';
   111     val RTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeBs;
   112     val sRTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeAs;
   113     val R'Ts = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeBs activeCs;
   114     val setsRTs = map HOLogic.mk_setT sRTs;
   115     val setRTs = map HOLogic.mk_setT RTs;
   116     val all_sbisT = HOLogic.mk_tupleT setsRTs;
   117     val setR'Ts = map HOLogic.mk_setT R'Ts;
   118     val FRTs = mk_FTs (passiveAs @ RTs);
   119     val sumBsAs = map2 (curry mk_sumT) activeBs activeAs;
   120     val sumFTs = mk_FTs (passiveAs @ sumBsAs);
   121     val sum_sTs = map2 (fn T => fn U => T --> U) activeAs sumFTs;
   122 
   123     (* terms *)
   124     val mapsAsAs = map4 mk_map_of_bnf Dss Ass Ass bnfs;
   125     val mapsAsBs = map4 mk_map_of_bnf Dss Ass Bss bnfs;
   126     val mapsBsCs' = map4 mk_map_of_bnf Dss Bss Css' bnfs;
   127     val mapsAsCs' = map4 mk_map_of_bnf Dss Ass Css' bnfs;
   128     val map_Inls = map4 mk_map_of_bnf Dss Bss (replicate n (passiveAs @ sumBsAs)) bnfs;
   129     val map_Inls_rev = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ sumBsAs)) Bss bnfs;
   130     val map_fsts = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Ass bnfs;
   131     val map_snds = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Bss bnfs;
   132     fun mk_setss Ts = map3 mk_sets_of_bnf (map (replicate live) Dss)
   133       (map (replicate live) (replicate n Ts)) bnfs;
   134     val setssAs = mk_setss allAs;
   135     val setssAs' = transpose setssAs;
   136     val bis_setss = mk_setss (passiveAs @ RTs);
   137     val relsAsBs = map4 mk_rel_of_bnf Dss Ass Bss bnfs;
   138     val bds = map3 mk_bd_of_bnf Dss Ass bnfs;
   139     val sum_bd = Library.foldr1 (uncurry mk_csum) bds;
   140     val sum_bdT = fst (dest_relT (fastype_of sum_bd));
   141     val witss = map wits_of_bnf bnfs;
   142 
   143     val emptys = map (fn T => HOLogic.mk_set T []) passiveAs;
   144     val Zeros = map (fn empty =>
   145      HOLogic.mk_tuple (map (fn U => absdummy U empty) activeAs)) emptys;
   146     val hrecTs = map fastype_of Zeros;
   147     val hsetTs = map (fn hrecT => Library.foldr (op -->) (sTs, HOLogic.natT --> hrecT)) hrecTs;
   148 
   149     val (((((((((((((((((((((((((((((((((((zs, zs'), zs_copy), zs_copy2),
   150       z's), As), As_copy), Bs), Bs_copy), B's), B''s), ss), sum_ss), s's), s''s), fs), fs_copy),
   151       self_fs), all_fs), gs), all_gs), xFs), xFs_copy), RFs), (Rtuple, Rtuple')), (hrecs, hrecs')),
   152       (nat, nat')), Rs), Rs_copy), R's), sRs), (idx, idx')), Idx), Ris), Kss),
   153       names_lthy) = lthy
   154       |> mk_Frees' "b" activeAs
   155       ||>> mk_Frees "b" activeAs
   156       ||>> mk_Frees "b" activeAs
   157       ||>> mk_Frees "b" activeBs
   158       ||>> mk_Frees "A" ATs
   159       ||>> mk_Frees "A" ATs
   160       ||>> mk_Frees "B" BTs
   161       ||>> mk_Frees "B" BTs
   162       ||>> mk_Frees "B'" B'Ts
   163       ||>> mk_Frees "B''" B''Ts
   164       ||>> mk_Frees "s" sTs
   165       ||>> mk_Frees "sums" sum_sTs
   166       ||>> mk_Frees "s'" s'Ts
   167       ||>> mk_Frees "s''" s''Ts
   168       ||>> mk_Frees "f" fTs
   169       ||>> mk_Frees "f" fTs
   170       ||>> mk_Frees "f" self_fTs
   171       ||>> mk_Frees "f" all_fTs
   172       ||>> mk_Frees "g" gTs
   173       ||>> mk_Frees "g" all_gTs
   174       ||>> mk_Frees "x" FTsAs
   175       ||>> mk_Frees "x" FTsAs
   176       ||>> mk_Frees "x" FRTs
   177       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Rtuple") all_sbisT
   178       ||>> mk_Frees' "rec" hrecTs
   179       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "n") HOLogic.natT
   180       ||>> mk_Frees "R" setRTs
   181       ||>> mk_Frees "R" setRTs
   182       ||>> mk_Frees "R'" setR'Ts
   183       ||>> mk_Frees "R" setsRTs
   184       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") idxT
   185       ||>> yield_singleton (mk_Frees "I") (HOLogic.mk_setT idxT)
   186       ||>> mk_Frees "Ri" (map (fn T => idxT --> T) setRTs)
   187       ||>> mk_Freess "K" (map (fn AT => map (fn T => T --> AT) activeAs) ATs);
   188 
   189     val passive_UNIVs = map HOLogic.mk_UNIV passiveAs;
   190     val passive_diags = map mk_diag As;
   191     val active_UNIVs = map HOLogic.mk_UNIV activeAs;
   192     val sum_UNIVs = map HOLogic.mk_UNIV sumBsAs;
   193     val passive_ids = map HOLogic.id_const passiveAs;
   194     val active_ids = map HOLogic.id_const activeAs;
   195     val Inls = map2 Inl_const activeBs activeAs;
   196     val fsts = map fst_const RTs;
   197     val snds = map snd_const RTs;
   198 
   199     (* thms *)
   200     val bd_card_orders = map bd_card_order_of_bnf bnfs;
   201     val bd_card_order = hd bd_card_orders
   202     val bd_Card_orders = map bd_Card_order_of_bnf bnfs;
   203     val bd_Card_order = hd bd_Card_orders;
   204     val bd_Cinfinites = map bd_Cinfinite_of_bnf bnfs;
   205     val bd_Cinfinite = hd bd_Cinfinites;
   206     val bd_Cnotzeros = map bd_Cnotzero_of_bnf bnfs;
   207     val bd_Cnotzero = hd bd_Cnotzeros;
   208     val in_bds = map in_bd_of_bnf bnfs;
   209     val in_monos = map in_mono_of_bnf bnfs;
   210     val map_comps = map map_comp_of_bnf bnfs;
   211     val map_comp's = map map_comp'_of_bnf bnfs;
   212     val map_congs = map map_cong_of_bnf bnfs;
   213     val map_id's = map map_id'_of_bnf bnfs;
   214     val pred_defs = map pred_def_of_bnf bnfs;
   215     val rel_congs = map rel_cong_of_bnf bnfs;
   216     val rel_converses = map rel_converse_of_bnf bnfs;
   217     val rel_defs = map rel_def_of_bnf bnfs;
   218     val rel_Grs = map rel_Gr_of_bnf bnfs;
   219     val rel_Ids = map rel_Id_of_bnf bnfs;
   220     val rel_monos = map rel_mono_of_bnf bnfs;
   221     val rel_Os = map rel_O_of_bnf bnfs;
   222     val map_wpulls = map map_wpull_of_bnf bnfs;
   223     val set_bdss = map set_bd_of_bnf bnfs;
   224     val set_natural'ss = map set_natural'_of_bnf bnfs;
   225 
   226     val timer = time (timer "Extracted terms & thms");
   227 
   228     (* derived thms *)
   229 
   230     (*map g1 ... gm g(m+1) ... g(m+n) (map id ... id f(m+1) ... f(m+n) x)=
   231       map g1 ... gm (g(m+1) o f(m+1)) ... (g(m+n) o f(m+n)) x*)
   232     fun mk_map_comp_id x mapAsBs mapBsCs mapAsCs map_comp =
   233       let
   234         val lhs = Term.list_comb (mapBsCs, all_gs) $
   235           (Term.list_comb (mapAsBs, passive_ids @ fs) $ x);
   236         val rhs =
   237           Term.list_comb (mapAsCs, take m all_gs @ map HOLogic.mk_comp (drop m all_gs ~~ fs)) $ x;
   238       in
   239         Skip_Proof.prove lthy [] []
   240           (fold_rev Logic.all (x :: fs @ all_gs) (mk_Trueprop_eq (lhs, rhs)))
   241           (K (mk_map_comp_id_tac map_comp))
   242         |> Thm.close_derivation
   243       end;
   244 
   245     val map_comp_id_thms = map5 mk_map_comp_id xFs mapsAsBs mapsBsCs' mapsAsCs' map_comp's;
   246 
   247     (*forall a : set(m+1) x. f(m+1) a = a; ...; forall a : set(m+n) x. f(m+n) a = a ==>
   248       map id ... id f(m+1) ... f(m+n) x = x*)
   249     fun mk_map_congL x mapAsAs sets map_cong map_id' =
   250       let
   251         fun mk_prem set f z z' =
   252           HOLogic.mk_Trueprop
   253             (mk_Ball (set $ x) (Term.absfree z' (HOLogic.mk_eq (f $ z, z))));
   254         val prems = map4 mk_prem (drop m sets) self_fs zs zs';
   255         val goal = mk_Trueprop_eq (Term.list_comb (mapAsAs, passive_ids @ self_fs) $ x, x);
   256       in
   257         Skip_Proof.prove lthy [] []
   258           (fold_rev Logic.all (x :: self_fs) (Logic.list_implies (prems, goal)))
   259           (K (mk_map_congL_tac m map_cong map_id'))
   260         |> Thm.close_derivation
   261       end;
   262 
   263     val map_congL_thms = map5 mk_map_congL xFs mapsAsAs setssAs map_congs map_id's;
   264     val in_mono'_thms = map (fn thm =>
   265       (thm OF (replicate m subset_refl)) RS @{thm set_mp}) in_monos;
   266     val in_cong'_thms = map (fn bnf => in_cong_of_bnf bnf OF (replicate m refl)) bnfs;
   267 
   268     val map_arg_cong_thms =
   269       let
   270         val prems = map2 (curry mk_Trueprop_eq) xFs xFs_copy;
   271         val maps = map (fn map => Term.list_comb (map, all_fs)) mapsAsBs;
   272         val concls =
   273           map3 (fn x => fn y => fn map => mk_Trueprop_eq (map $ x, map $ y)) xFs xFs_copy maps;
   274         val goals =
   275           map4 (fn prem => fn concl => fn x => fn y =>
   276             fold_rev Logic.all (x :: y :: all_fs) (Logic.mk_implies (prem, concl)))
   277           prems concls xFs xFs_copy;
   278       in
   279         map (fn goal => Skip_Proof.prove lthy [] [] goal
   280           (K ((hyp_subst_tac THEN' rtac refl) 1)) |> Thm.close_derivation) goals
   281       end;
   282 
   283     val timer = time (timer "Derived simple theorems");
   284 
   285     (* coalgebra *)
   286 
   287     val coalg_bind = Binding.suffix_name ("_" ^ coN ^ algN) b;
   288     val coalg_name = Binding.name_of coalg_bind;
   289     val coalg_def_bind = (Thm.def_binding coalg_bind, []);
   290 
   291     (*forall i = 1 ... n: (\<forall>x \<in> Bi. si \<in> Fi_in A1 .. Am B1 ... Bn)*)
   292     val coalg_spec =
   293       let
   294         val coalgT = Library.foldr (op -->) (ATs @ BTs @ sTs, HOLogic.boolT);
   295 
   296         val ins = map3 mk_in (replicate n (As @ Bs)) setssAs FTsAs;
   297         fun mk_coalg_conjunct B s X z z' =
   298           mk_Ball B (Term.absfree z' (HOLogic.mk_mem (s $ z, X)));
   299 
   300         val lhs = Term.list_comb (Free (coalg_name, coalgT), As @ Bs @ ss);
   301         val rhs = Library.foldr1 HOLogic.mk_conj (map5 mk_coalg_conjunct Bs ss ins zs zs')
   302       in
   303         mk_Trueprop_eq (lhs, rhs)
   304       end;
   305 
   306     val ((coalg_free, (_, coalg_def_free)), (lthy, lthy_old)) =
   307         lthy
   308         |> Specification.definition (SOME (coalg_bind, NONE, NoSyn), (coalg_def_bind, coalg_spec))
   309         ||> `Local_Theory.restore;
   310 
   311     (*transforms defined frees into consts*)
   312     val phi = Proof_Context.export_morphism lthy_old lthy;
   313     val coalg = fst (Term.dest_Const (Morphism.term phi coalg_free));
   314     val coalg_def = Morphism.thm phi coalg_def_free;
   315 
   316     fun mk_coalg As Bs ss =
   317       let
   318         val args = As @ Bs @ ss;
   319         val Ts = map fastype_of args;
   320         val coalgT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   321       in
   322         Term.list_comb (Const (coalg, coalgT), args)
   323       end;
   324 
   325     val coalg_prem = HOLogic.mk_Trueprop (mk_coalg As Bs ss);
   326 
   327     val coalg_in_thms = map (fn i =>
   328       coalg_def RS @{thm subst[of _ _ "%x. x"]} RS mk_conjunctN n i RS bspec) ks
   329 
   330     val coalg_set_thmss =
   331       let
   332         val coalg_prem = HOLogic.mk_Trueprop (mk_coalg As Bs ss);
   333         fun mk_prem x B = HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B));
   334         fun mk_concl s x B set = HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) B);
   335         val prems = map2 mk_prem zs Bs;
   336         val conclss = map3 (fn s => fn x => fn sets => map2 (mk_concl s x) (As @ Bs) sets)
   337           ss zs setssAs;
   338         val goalss = map3 (fn x => fn prem => fn concls => map (fn concl =>
   339           fold_rev Logic.all (x :: As @ Bs @ ss)
   340             (Logic.list_implies (coalg_prem :: [prem], concl))) concls) zs prems conclss;
   341       in
   342         map (fn goals => map (fn goal => Skip_Proof.prove lthy [] [] goal
   343           (K (mk_coalg_set_tac coalg_def)) |> Thm.close_derivation) goals) goalss
   344       end;
   345 
   346     val coalg_set_thmss' = transpose coalg_set_thmss;
   347 
   348     fun mk_tcoalg ATs BTs = mk_coalg (map HOLogic.mk_UNIV ATs) (map HOLogic.mk_UNIV BTs);
   349 
   350     val tcoalg_thm =
   351       let
   352         val goal = fold_rev Logic.all ss
   353           (HOLogic.mk_Trueprop (mk_tcoalg passiveAs activeAs ss))
   354       in
   355         Skip_Proof.prove lthy [] [] goal
   356           (K (stac coalg_def 1 THEN CONJ_WRAP
   357             (K (EVERY' [rtac ballI, rtac CollectI,
   358               CONJ_WRAP' (K (EVERY' [rtac @{thm subset_UNIV}])) allAs] 1)) ss))
   359         |> Thm.close_derivation
   360       end;
   361 
   362     val timer = time (timer "Coalgebra definition & thms");
   363 
   364     (* morphism *)
   365 
   366     val mor_bind = Binding.suffix_name ("_" ^ morN) b;
   367     val mor_name = Binding.name_of mor_bind;
   368     val mor_def_bind = (Thm.def_binding mor_bind, []);
   369 
   370     (*fbetw) forall i = 1 ... n: (\<forall>x \<in> Bi. fi x \<in> B'i)*)
   371     (*mor) forall i = 1 ... n: (\<forall>x \<in> Bi.
   372        Fi_map id ... id f1 ... fn (si x) = si' (fi x)*)
   373     val mor_spec =
   374       let
   375         val morT = Library.foldr (op -->) (BTs @ sTs @ B'Ts @ s'Ts @ fTs, HOLogic.boolT);
   376 
   377         fun mk_fbetw f B1 B2 z z' =
   378           mk_Ball B1 (Term.absfree z' (HOLogic.mk_mem (f $ z, B2)));
   379         fun mk_mor B mapAsBs f s s' z z' =
   380           mk_Ball B (Term.absfree z' (HOLogic.mk_eq
   381             (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ z]), s' $ (f $ z))));
   382         val lhs = Term.list_comb (Free (mor_name, morT), Bs @ ss @ B's @ s's @ fs);
   383         val rhs = HOLogic.mk_conj
   384           (Library.foldr1 HOLogic.mk_conj (map5 mk_fbetw fs Bs B's zs zs'),
   385            Library.foldr1 HOLogic.mk_conj (map7 mk_mor Bs mapsAsBs fs ss s's zs zs'))
   386       in
   387         mk_Trueprop_eq (lhs, rhs)
   388       end;
   389 
   390     val ((mor_free, (_, mor_def_free)), (lthy, lthy_old)) =
   391         lthy
   392         |> Specification.definition (SOME (mor_bind, NONE, NoSyn), (mor_def_bind, mor_spec))
   393         ||> `Local_Theory.restore;
   394 
   395     (*transforms defined frees into consts*)
   396     val phi = Proof_Context.export_morphism lthy_old lthy;
   397     val mor = fst (Term.dest_Const (Morphism.term phi mor_free));
   398     val mor_def = Morphism.thm phi mor_def_free;
   399 
   400     fun mk_mor Bs1 ss1 Bs2 ss2 fs =
   401       let
   402         val args = Bs1 @ ss1 @ Bs2 @ ss2 @ fs;
   403         val Ts = map fastype_of (Bs1 @ ss1 @ Bs2 @ ss2 @ fs);
   404         val morT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   405       in
   406         Term.list_comb (Const (mor, morT), args)
   407       end;
   408 
   409     val mor_prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   410 
   411     val (mor_image_thms, morE_thms) =
   412       let
   413         val prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   414         fun mk_image_goal f B1 B2 = fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs)
   415           (Logic.mk_implies (prem, HOLogic.mk_Trueprop (mk_subset (mk_image f $ B1) B2)));
   416         val image_goals = map3 mk_image_goal fs Bs B's;
   417         fun mk_elim_goal B mapAsBs f s s' x =
   418           fold_rev Logic.all (x :: Bs @ ss @ B's @ s's @ fs)
   419             (Logic.list_implies ([prem, HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B))],
   420               mk_Trueprop_eq (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ x]), s' $ (f $ x))));
   421         val elim_goals = map6 mk_elim_goal Bs mapsAsBs fs ss s's zs;
   422         fun prove goal =
   423           Skip_Proof.prove lthy [] [] goal (K (mk_mor_elim_tac mor_def))
   424           |> Thm.close_derivation;
   425       in
   426         (map prove image_goals, map prove elim_goals)
   427       end;
   428 
   429     val mor_image'_thms = map (fn thm => @{thm set_mp} OF [thm, imageI]) mor_image_thms;
   430 
   431     val mor_incl_thm =
   432       let
   433         val prems = map2 (HOLogic.mk_Trueprop oo mk_subset) Bs Bs_copy;
   434         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss Bs_copy ss active_ids);
   435       in
   436         Skip_Proof.prove lthy [] []
   437           (fold_rev Logic.all (Bs @ ss @ Bs_copy) (Logic.list_implies (prems, concl)))
   438           (K (mk_mor_incl_tac mor_def map_id's))
   439         |> Thm.close_derivation
   440       end;
   441 
   442     val mor_id_thm = mor_incl_thm OF (replicate n subset_refl);
   443 
   444     val mor_comp_thm =
   445       let
   446         val prems =
   447           [HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs),
   448            HOLogic.mk_Trueprop (mk_mor B's s's B''s s''s gs)];
   449         val concl =
   450           HOLogic.mk_Trueprop (mk_mor Bs ss B''s s''s (map2 (curry HOLogic.mk_comp) gs fs));
   451       in
   452         Skip_Proof.prove lthy [] []
   453           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ B''s @ s''s @ fs @ gs)
   454             (Logic.list_implies (prems, concl)))
   455           (K (mk_mor_comp_tac mor_def mor_image'_thms morE_thms map_comp_id_thms))
   456         |> Thm.close_derivation
   457       end;
   458 
   459     val mor_cong_thm =
   460       let
   461         val prems = map HOLogic.mk_Trueprop
   462          (map2 (curry HOLogic.mk_eq) fs_copy fs @ [mk_mor Bs ss B's s's fs])
   463         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs_copy);
   464       in
   465         Skip_Proof.prove lthy [] []
   466           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs @ fs_copy)
   467             (Logic.list_implies (prems, concl)))
   468           (K ((hyp_subst_tac THEN' atac) 1))
   469         |> Thm.close_derivation
   470       end;
   471 
   472     val mor_UNIV_thm =
   473       let
   474         fun mk_conjunct mapAsBs f s s' = HOLogic.mk_eq
   475             (HOLogic.mk_comp (Term.list_comb (mapAsBs, passive_ids @ fs), s),
   476             HOLogic.mk_comp (s', f));
   477         val lhs = mk_mor active_UNIVs ss (map HOLogic.mk_UNIV activeBs) s's fs;
   478         val rhs = Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct mapsAsBs fs ss s's);
   479       in
   480         Skip_Proof.prove lthy [] [] (fold_rev Logic.all (ss @ s's @ fs) (mk_Trueprop_eq (lhs, rhs)))
   481           (K (mk_mor_UNIV_tac morE_thms mor_def))
   482         |> Thm.close_derivation
   483       end;
   484 
   485     val mor_str_thm =
   486       let
   487         val maps = map2 (fn Ds => fn bnf => Term.list_comb
   488           (mk_map_of_bnf Ds allAs (passiveAs @ FTsAs) bnf, passive_ids @ ss)) Dss bnfs;
   489       in
   490         Skip_Proof.prove lthy [] []
   491           (fold_rev Logic.all ss (HOLogic.mk_Trueprop
   492             (mk_mor active_UNIVs ss (map HOLogic.mk_UNIV FTsAs) maps ss)))
   493           (K (mk_mor_str_tac ks mor_UNIV_thm))
   494         |> Thm.close_derivation
   495       end;
   496 
   497     val mor_sum_case_thm =
   498       let
   499         val maps = map3 (fn s => fn sum_s => fn map =>
   500           mk_sum_case (HOLogic.mk_comp (Term.list_comb (map, passive_ids @ Inls), s)) sum_s)
   501           s's sum_ss map_Inls;
   502       in
   503         Skip_Proof.prove lthy [] []
   504           (fold_rev Logic.all (s's @ sum_ss) (HOLogic.mk_Trueprop
   505             (mk_mor (map HOLogic.mk_UNIV activeBs) s's sum_UNIVs maps Inls)))
   506           (K (mk_mor_sum_case_tac ks mor_UNIV_thm))
   507         |> Thm.close_derivation
   508       end;
   509 
   510     val timer = time (timer "Morphism definition & thms");
   511 
   512     fun hset_rec_bind j = Binding.suffix_name ("_" ^ hset_recN ^ (if m = 1 then "" else
   513       string_of_int j)) b;
   514     val hset_rec_name = Binding.name_of o hset_rec_bind;
   515     val hset_rec_def_bind = rpair [] o Thm.def_binding o hset_rec_bind;
   516 
   517     fun hset_rec_spec j Zero hsetT hrec hrec' =
   518       let
   519         fun mk_Suc s setsAs z z' =
   520           let
   521             val (set, sets) = apfst (fn xs => nth xs (j - 1)) (chop m setsAs);
   522             fun mk_UN set k = mk_UNION (set $ (s $ z)) (mk_nthN n hrec k);
   523           in
   524             Term.absfree z'
   525               (mk_union (set $ (s $ z), Library.foldl1 mk_union (map2 mk_UN sets ks)))
   526           end;
   527 
   528         val Suc = Term.absdummy HOLogic.natT (Term.absfree hrec'
   529           (HOLogic.mk_tuple (map4 mk_Suc ss setssAs zs zs')));
   530 
   531         val lhs = Term.list_comb (Free (hset_rec_name j, hsetT), ss);
   532         val rhs = mk_nat_rec Zero Suc;
   533       in
   534         mk_Trueprop_eq (lhs, rhs)
   535       end;
   536 
   537     val ((hset_rec_frees, (_, hset_rec_def_frees)), (lthy, lthy_old)) =
   538       lthy
   539       |> fold_map5 (fn j => fn Zero => fn hsetT => fn hrec => fn hrec' => Specification.definition
   540         (SOME (hset_rec_bind j, NONE, NoSyn),
   541           (hset_rec_def_bind j, hset_rec_spec j Zero hsetT hrec hrec')))
   542         ls Zeros hsetTs hrecs hrecs'
   543       |>> apsnd split_list o split_list
   544       ||> `Local_Theory.restore;
   545 
   546     (*transforms defined frees into consts*)
   547     val phi = Proof_Context.export_morphism lthy_old lthy;
   548 
   549     val hset_rec_defs = map (Morphism.thm phi) hset_rec_def_frees;
   550     val hset_recs = map (fst o Term.dest_Const o Morphism.term phi) hset_rec_frees;
   551 
   552     fun mk_hset_rec ss nat i j T =
   553       let
   554         val args = ss @ [nat];
   555         val Ts = map fastype_of ss;
   556         val bTs = map domain_type Ts;
   557         val hrecT = HOLogic.mk_tupleT (map (fn U => U --> HOLogic.mk_setT T) bTs)
   558         val hset_recT = Library.foldr (op -->) (Ts, HOLogic.natT --> hrecT);
   559       in
   560         mk_nthN n (Term.list_comb (Const (nth hset_recs (j - 1), hset_recT), args)) i
   561       end;
   562 
   563     val hset_rec_0ss = mk_rec_simps n @{thm nat_rec_0} hset_rec_defs;
   564     val hset_rec_Sucss = mk_rec_simps n @{thm nat_rec_Suc} hset_rec_defs;
   565     val hset_rec_0ss' = transpose hset_rec_0ss;
   566     val hset_rec_Sucss' = transpose hset_rec_Sucss;
   567 
   568     fun hset_bind i j = Binding.suffix_name ("_" ^ hsetN ^
   569       (if m = 1 then "" else string_of_int j)) (nth bs (i - 1));
   570     val hset_name = Binding.name_of oo hset_bind;
   571     val hset_def_bind = rpair [] o Thm.def_binding oo hset_bind;
   572 
   573     fun hset_spec i j =
   574       let
   575         val U = nth activeAs (i - 1);
   576         val z = nth zs (i - 1);
   577         val T = nth passiveAs (j - 1);
   578         val setT = HOLogic.mk_setT T;
   579         val hsetT = Library.foldr (op -->) (sTs, U --> setT);
   580 
   581         val lhs = Term.list_comb (Free (hset_name i j, hsetT), ss @ [z]);
   582         val rhs = mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
   583           (Term.absfree nat' (mk_hset_rec ss nat i j T $ z));
   584       in
   585         mk_Trueprop_eq (lhs, rhs)
   586       end;
   587 
   588     val ((hset_frees, (_, hset_def_frees)), (lthy, lthy_old)) =
   589       lthy
   590       |> fold_map (fn i => fold_map (fn j => Specification.definition
   591         (SOME (hset_bind i j, NONE, NoSyn), (hset_def_bind i j, hset_spec i j))) ls) ks
   592       |>> map (apsnd split_list o split_list)
   593       |>> apsnd split_list o split_list
   594       ||> `Local_Theory.restore;
   595 
   596     (*transforms defined frees into consts*)
   597     val phi = Proof_Context.export_morphism lthy_old lthy;
   598 
   599     val hset_defss = map (map (Morphism.thm phi)) hset_def_frees;
   600     val hset_defss' = transpose hset_defss;
   601     val hset_namess = map (map (fst o Term.dest_Const o Morphism.term phi)) hset_frees;
   602 
   603     fun mk_hset ss i j T =
   604       let
   605         val Ts = map fastype_of ss;
   606         val bTs = map domain_type Ts;
   607         val hsetT = Library.foldr (op -->) (Ts, nth bTs (i - 1) --> HOLogic.mk_setT T);
   608       in
   609         Term.list_comb (Const (nth (nth hset_namess (i - 1)) (j - 1), hsetT), ss)
   610       end;
   611 
   612     val hsetssAs = map (fn i => map2 (mk_hset ss i) ls passiveAs) ks;
   613 
   614     val (set_incl_hset_thmss, set_hset_incl_hset_thmsss) =
   615       let
   616         fun mk_set_incl_hset s x set hset = fold_rev Logic.all (x :: ss)
   617           (HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (hset $ x)));
   618 
   619         fun mk_set_hset_incl_hset s x y set hset1 hset2 =
   620           fold_rev Logic.all (x :: y :: ss)
   621             (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (x, set $ (s $ y))),
   622             HOLogic.mk_Trueprop (mk_subset (hset1 $ x) (hset2 $ y))));
   623 
   624         val set_incl_hset_goalss =
   625           map4 (fn s => fn x => fn sets => fn hsets =>
   626             map2 (mk_set_incl_hset s x) (take m sets) hsets)
   627           ss zs setssAs hsetssAs;
   628 
   629         (*xk : F(i)set(m+k) (si yi) ==> F(k)_hset(j) s1 ... sn xk <= F(i)_hset(j) s1 ... sn yi*)
   630         val set_hset_incl_hset_goalsss =
   631           map4 (fn si => fn yi => fn sets => fn hsetsi =>
   632             map3 (fn xk => fn set => fn hsetsk =>
   633               map2 (mk_set_hset_incl_hset si xk yi set) hsetsk hsetsi)
   634             zs_copy (drop m sets) hsetssAs)
   635           ss zs setssAs hsetssAs;
   636       in
   637         (map3 (fn goals => fn defs => fn rec_Sucs =>
   638           map3 (fn goal => fn def => fn rec_Suc =>
   639             Skip_Proof.prove lthy [] [] goal (K (mk_set_incl_hset_tac def rec_Suc))
   640             |> Thm.close_derivation)
   641           goals defs rec_Sucs)
   642         set_incl_hset_goalss hset_defss hset_rec_Sucss,
   643         map3 (fn goalss => fn defsi => fn rec_Sucs =>
   644           map3 (fn k => fn goals => fn defsk =>
   645             map4 (fn goal => fn defk => fn defi => fn rec_Suc =>
   646               Skip_Proof.prove lthy [] [] goal
   647                 (K (mk_set_hset_incl_hset_tac n [defk, defi] rec_Suc k))
   648               |> Thm.close_derivation)
   649             goals defsk defsi rec_Sucs)
   650           ks goalss hset_defss)
   651         set_hset_incl_hset_goalsss hset_defss hset_rec_Sucss)
   652       end;
   653 
   654     val set_incl_hset_thmss' = transpose set_incl_hset_thmss;
   655     val set_hset_incl_hset_thmsss' = transpose (map transpose set_hset_incl_hset_thmsss);
   656     val set_hset_incl_hset_thmsss'' = map transpose set_hset_incl_hset_thmsss';
   657     val set_hset_thmss = map (map (fn thm => thm RS @{thm set_mp})) set_incl_hset_thmss;
   658     val set_hset_hset_thmsss = map (map (map (fn thm => thm RS @{thm set_mp})))
   659       set_hset_incl_hset_thmsss;
   660     val set_hset_thmss' = transpose set_hset_thmss;
   661     val set_hset_hset_thmsss' = transpose (map transpose set_hset_hset_thmsss);
   662 
   663     val set_incl_hin_thmss =
   664       let
   665         fun mk_set_incl_hin s x hsets1 set hsets2 T =
   666           fold_rev Logic.all (x :: ss @ As)
   667             (Logic.list_implies
   668               (map2 (fn hset => fn A => HOLogic.mk_Trueprop (mk_subset (hset $ x) A)) hsets1 As,
   669               HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (mk_in As hsets2 T))));
   670 
   671         val set_incl_hin_goalss =
   672           map4 (fn s => fn x => fn sets => fn hsets =>
   673             map3 (mk_set_incl_hin s x hsets) (drop m sets) hsetssAs activeAs)
   674           ss zs setssAs hsetssAs;
   675       in
   676         map2 (map2 (fn goal => fn thms =>
   677           Skip_Proof.prove lthy [] [] goal (K (mk_set_incl_hin_tac thms))
   678           |> Thm.close_derivation))
   679         set_incl_hin_goalss set_hset_incl_hset_thmsss
   680       end;
   681 
   682     val hset_minimal_thms =
   683       let
   684         fun mk_passive_prem set s x K =
   685           Logic.all x (HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (K $ x)));
   686 
   687         fun mk_active_prem s x1 K1 set x2 K2 =
   688           fold_rev Logic.all [x1, x2]
   689             (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (x2, set $ (s $ x1))),
   690               HOLogic.mk_Trueprop (mk_subset (K2 $ x2) (K1 $ x1))));
   691 
   692         val premss = map2 (fn j => fn Ks =>
   693           map4 mk_passive_prem (map (fn xs => nth xs (j - 1)) setssAs) ss zs Ks @
   694             flat (map4 (fn sets => fn s => fn x1 => fn K1 =>
   695               map3 (mk_active_prem s x1 K1) (drop m sets) zs_copy Ks) setssAs ss zs Ks))
   696           ls Kss;
   697 
   698         val hset_rec_minimal_thms =
   699           let
   700             fun mk_conjunct j T i K x = mk_subset (mk_hset_rec ss nat i j T $ x) (K $ x);
   701             fun mk_concl j T Ks = list_all_free zs
   702               (Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T) ks Ks zs));
   703             val concls = map3 mk_concl ls passiveAs Kss;
   704 
   705             val goals = map2 (fn prems => fn concl =>
   706               Logic.list_implies (prems, HOLogic.mk_Trueprop concl)) premss concls
   707 
   708             val ctss =
   709               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
   710           in
   711             map4 (fn goal => fn cts => fn hset_rec_0s => fn hset_rec_Sucs =>
   712               singleton (Proof_Context.export names_lthy lthy)
   713                 (Skip_Proof.prove lthy [] [] goal
   714                   (mk_hset_rec_minimal_tac m cts hset_rec_0s hset_rec_Sucs))
   715               |> Thm.close_derivation)
   716             goals ctss hset_rec_0ss' hset_rec_Sucss'
   717           end;
   718 
   719         fun mk_conjunct j T i K x = mk_subset (mk_hset ss i j T $ x) (K $ x);
   720         fun mk_concl j T Ks = Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T) ks Ks zs);
   721         val concls = map3 mk_concl ls passiveAs Kss;
   722 
   723         val goals = map3 (fn Ks => fn prems => fn concl =>
   724           fold_rev Logic.all (Ks @ ss @ zs)
   725             (Logic.list_implies (prems, HOLogic.mk_Trueprop concl))) Kss premss concls;
   726       in
   727         map3 (fn goal => fn hset_defs => fn hset_rec_minimal =>
   728           Skip_Proof.prove lthy [] [] goal
   729             (mk_hset_minimal_tac n hset_defs hset_rec_minimal)
   730           |> Thm.close_derivation)
   731         goals hset_defss' hset_rec_minimal_thms
   732       end;
   733 
   734     val mor_hset_thmss =
   735       let
   736         val mor_hset_rec_thms =
   737           let
   738             fun mk_conjunct j T i f x B =
   739               HOLogic.mk_imp (HOLogic.mk_mem (x, B), HOLogic.mk_eq
   740                (mk_hset_rec s's nat i j T $ (f $ x), mk_hset_rec ss nat i j T $ x));
   741 
   742             fun mk_concl j T = list_all_free zs
   743               (Library.foldr1 HOLogic.mk_conj (map4 (mk_conjunct j T) ks fs zs Bs));
   744             val concls = map2 mk_concl ls passiveAs;
   745 
   746             val ctss =
   747               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
   748 
   749             val goals = map (fn concl =>
   750               Logic.list_implies ([coalg_prem, mor_prem], HOLogic.mk_Trueprop concl)) concls;
   751           in
   752             map5 (fn j => fn goal => fn cts => fn hset_rec_0s => fn hset_rec_Sucs =>
   753               singleton (Proof_Context.export names_lthy lthy)
   754                 (Skip_Proof.prove lthy [] [] goal
   755                   (K (mk_mor_hset_rec_tac m n cts j hset_rec_0s hset_rec_Sucs
   756                     morE_thms set_natural'ss coalg_set_thmss)))
   757               |> Thm.close_derivation)
   758             ls goals ctss hset_rec_0ss' hset_rec_Sucss'
   759           end;
   760 
   761         val mor_hset_rec_thmss = map (fn thm => map (fn i =>
   762           mk_specN n thm RS mk_conjunctN n i RS mp) ks) mor_hset_rec_thms;
   763 
   764         fun mk_prem x B = HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B));
   765 
   766         fun mk_concl j T i f x =
   767           mk_Trueprop_eq (mk_hset s's i j T $ (f $ x), mk_hset ss i j T $ x);
   768 
   769         val goalss = map2 (fn j => fn T => map4 (fn i => fn f => fn x => fn B =>
   770           fold_rev Logic.all (x :: As @ Bs @ ss @ B's @ s's @ fs)
   771             (Logic.list_implies ([coalg_prem, mor_prem,
   772               mk_prem x B], mk_concl j T i f x))) ks fs zs Bs) ls passiveAs;
   773       in
   774         map3 (map3 (fn goal => fn hset_def => fn mor_hset_rec =>
   775           Skip_Proof.prove lthy [] [] goal
   776             (K (mk_mor_hset_tac hset_def mor_hset_rec))
   777           |> Thm.close_derivation))
   778         goalss hset_defss' mor_hset_rec_thmss
   779       end;
   780 
   781     val timer = time (timer "Hereditary sets");
   782 
   783     (* bisimulation *)
   784 
   785     val bis_bind = Binding.suffix_name ("_" ^ bisN) b;
   786     val bis_name = Binding.name_of bis_bind;
   787     val bis_def_bind = (Thm.def_binding bis_bind, []);
   788 
   789     fun mk_bis_le_conjunct R B1 B2 = mk_subset R (mk_Times (B1, B2));
   790     val bis_le = Library.foldr1 HOLogic.mk_conj (map3 mk_bis_le_conjunct Rs Bs B's)
   791 
   792     val bis_spec =
   793       let
   794         val bisT = Library.foldr (op -->) (ATs @ BTs @ sTs @ B'Ts @ s'Ts @ setRTs, HOLogic.boolT);
   795 
   796         val fst_args = passive_ids @ fsts;
   797         val snd_args = passive_ids @ snds;
   798         fun mk_bis R s s' b1 b2 RF map1 map2 sets =
   799           list_all_free [b1, b2] (HOLogic.mk_imp
   800             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   801             mk_Bex (mk_in (As @ Rs) sets (snd (dest_Free RF))) (Term.absfree (dest_Free RF)
   802               (HOLogic.mk_conj
   803                 (HOLogic.mk_eq (Term.list_comb (map1, fst_args) $ RF, s $ b1),
   804                 HOLogic.mk_eq (Term.list_comb (map2, snd_args) $ RF, s' $ b2))))));
   805 
   806         val lhs = Term.list_comb (Free (bis_name, bisT), As @ Bs @ ss @ B's @ s's @ Rs);
   807         val rhs = HOLogic.mk_conj
   808           (bis_le, Library.foldr1 HOLogic.mk_conj
   809             (map9 mk_bis Rs ss s's zs z's RFs map_fsts map_snds bis_setss))
   810       in
   811         mk_Trueprop_eq (lhs, rhs)
   812       end;
   813 
   814     val ((bis_free, (_, bis_def_free)), (lthy, lthy_old)) =
   815         lthy
   816         |> Specification.definition (SOME (bis_bind, NONE, NoSyn), (bis_def_bind, bis_spec))
   817         ||> `Local_Theory.restore;
   818 
   819     (*transforms defined frees into consts*)
   820     val phi = Proof_Context.export_morphism lthy_old lthy;
   821     val bis = fst (Term.dest_Const (Morphism.term phi bis_free));
   822     val bis_def = Morphism.thm phi bis_def_free;
   823 
   824     fun mk_bis As Bs1 ss1 Bs2 ss2 Rs =
   825       let
   826         val args = As @ Bs1 @ ss1 @ Bs2 @ ss2 @ Rs;
   827         val Ts = map fastype_of args;
   828         val bisT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   829       in
   830         Term.list_comb (Const (bis, bisT), args)
   831       end;
   832 
   833     val bis_cong_thm =
   834       let
   835         val prems = map HOLogic.mk_Trueprop
   836          (mk_bis As Bs ss B's s's Rs :: map2 (curry HOLogic.mk_eq) Rs_copy Rs)
   837         val concl = HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs_copy);
   838       in
   839         Skip_Proof.prove lthy [] []
   840           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs @ Rs_copy)
   841             (Logic.list_implies (prems, concl)))
   842           (K ((hyp_subst_tac THEN' atac) 1))
   843         |> Thm.close_derivation
   844       end;
   845 
   846     val bis_rel_thm =
   847       let
   848         fun mk_conjunct R s s' b1 b2 rel =
   849           list_all_free [b1, b2] (HOLogic.mk_imp
   850             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   851             HOLogic.mk_mem (HOLogic.mk_prod (s $ b1, s' $ b2),
   852               Term.list_comb (rel, passive_diags @ Rs))));
   853 
   854         val rhs = HOLogic.mk_conj
   855           (bis_le, Library.foldr1 HOLogic.mk_conj
   856             (map6 mk_conjunct Rs ss s's zs z's relsAsBs))
   857       in
   858         Skip_Proof.prove lthy [] []
   859           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs)
   860             (mk_Trueprop_eq (mk_bis As Bs ss B's s's Rs, rhs)))
   861           (K (mk_bis_rel_tac m bis_def rel_defs map_comp's map_congs set_natural'ss))
   862         |> Thm.close_derivation
   863       end;
   864 
   865     val bis_converse_thm =
   866       Skip_Proof.prove lthy [] []
   867         (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs)
   868           (Logic.mk_implies
   869             (HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs),
   870             HOLogic.mk_Trueprop (mk_bis As B's s's Bs ss (map mk_converse Rs)))))
   871         (K (mk_bis_converse_tac m bis_rel_thm rel_congs rel_converses))
   872       |> Thm.close_derivation;
   873 
   874     val bis_O_thm =
   875       let
   876         val prems =
   877           [HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs),
   878            HOLogic.mk_Trueprop (mk_bis As B's s's B''s s''s R's)];
   879         val concl =
   880           HOLogic.mk_Trueprop (mk_bis As Bs ss B''s s''s (map2 (curry mk_rel_comp) Rs R's));
   881       in
   882         Skip_Proof.prove lthy [] []
   883           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ B''s @ s''s @ Rs @ R's)
   884             (Logic.list_implies (prems, concl)))
   885           (K (mk_bis_O_tac m bis_rel_thm rel_congs rel_Os))
   886         |> Thm.close_derivation
   887       end;
   888 
   889     val bis_Gr_thm =
   890       let
   891         val concl =
   892           HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's (map2 mk_Gr Bs fs));
   893       in
   894         Skip_Proof.prove lthy [] []
   895           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ fs)
   896             (Logic.list_implies ([coalg_prem, mor_prem], concl)))
   897           (mk_bis_Gr_tac bis_rel_thm rel_Grs mor_image_thms morE_thms coalg_in_thms)
   898         |> Thm.close_derivation
   899       end;
   900 
   901     val bis_image2_thm = bis_cong_thm OF
   902       ((bis_O_thm OF [bis_Gr_thm RS bis_converse_thm, bis_Gr_thm]) ::
   903       replicate n @{thm image2_Gr});
   904 
   905     val bis_diag_thm = bis_cong_thm OF ((mor_id_thm RSN (2, bis_Gr_thm)) ::
   906       replicate n @{thm diag_Gr});
   907 
   908     val bis_Union_thm =
   909       let
   910         val prem =
   911           HOLogic.mk_Trueprop (mk_Ball Idx
   912             (Term.absfree idx' (mk_bis As Bs ss B's s's (map (fn R => R $ idx) Ris))));
   913         val concl =
   914           HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's (map (mk_UNION Idx) Ris));
   915       in
   916         Skip_Proof.prove lthy [] []
   917           (fold_rev Logic.all (Idx :: As @ Bs @ ss @ B's @ s's @ Ris)
   918             (Logic.mk_implies (prem, concl)))
   919           (mk_bis_Union_tac bis_def in_mono'_thms)
   920         |> Thm.close_derivation
   921       end;
   922 
   923     (* self-bisimulation *)
   924 
   925     fun mk_sbis As Bs ss Rs = mk_bis As Bs ss Bs ss Rs;
   926 
   927     val sbis_prem = HOLogic.mk_Trueprop (mk_sbis As Bs ss sRs);
   928 
   929     (* largest self-bisimulation *)
   930 
   931     fun lsbis_bind i = Binding.suffix_name ("_" ^ lsbisN ^ (if n = 1 then "" else
   932       string_of_int i)) b;
   933     val lsbis_name = Binding.name_of o lsbis_bind;
   934     val lsbis_def_bind = rpair [] o Thm.def_binding o lsbis_bind;
   935 
   936     val all_sbis = HOLogic.mk_Collect (fst Rtuple', snd Rtuple', list_exists_free sRs
   937       (HOLogic.mk_conj (HOLogic.mk_eq (Rtuple, HOLogic.mk_tuple sRs), mk_sbis As Bs ss sRs)));
   938 
   939     fun lsbis_spec i RT =
   940       let
   941         fun mk_lsbisT RT =
   942           Library.foldr (op -->) (map fastype_of (As @ Bs @ ss), RT);
   943         val lhs = Term.list_comb (Free (lsbis_name i, mk_lsbisT RT), As @ Bs @ ss);
   944         val rhs = mk_UNION all_sbis (Term.absfree Rtuple' (mk_nthN n Rtuple i));
   945       in
   946         mk_Trueprop_eq (lhs, rhs)
   947       end;
   948 
   949     val ((lsbis_frees, (_, lsbis_def_frees)), (lthy, lthy_old)) =
   950       lthy
   951       |> fold_map2 (fn i => fn RT => Specification.definition
   952         (SOME (lsbis_bind i, NONE, NoSyn), (lsbis_def_bind i, lsbis_spec i RT))) ks setsRTs
   953       |>> apsnd split_list o split_list
   954       ||> `Local_Theory.restore;
   955 
   956     (*transforms defined frees into consts*)
   957     val phi = Proof_Context.export_morphism lthy_old lthy;
   958 
   959     val lsbis_defs = map (Morphism.thm phi) lsbis_def_frees;
   960     val lsbiss = map (fst o Term.dest_Const o Morphism.term phi) lsbis_frees;
   961 
   962     fun mk_lsbis As Bs ss i =
   963       let
   964         val args = As @ Bs @ ss;
   965         val Ts = map fastype_of args;
   966         val RT = mk_relT (`I (HOLogic.dest_setT (fastype_of (nth Bs (i - 1)))));
   967         val lsbisT = Library.foldr (op -->) (Ts, RT);
   968       in
   969         Term.list_comb (Const (nth lsbiss (i - 1), lsbisT), args)
   970       end;
   971 
   972     val sbis_lsbis_thm =
   973       Skip_Proof.prove lthy [] []
   974         (fold_rev Logic.all (As @ Bs @ ss)
   975           (HOLogic.mk_Trueprop (mk_sbis As Bs ss (map (mk_lsbis As Bs ss) ks))))
   976         (K (mk_sbis_lsbis_tac lsbis_defs bis_Union_thm bis_cong_thm))
   977       |> Thm.close_derivation;
   978 
   979     val lsbis_incl_thms = map (fn i => sbis_lsbis_thm RS
   980       (bis_def RS @{thm subst[of _ _ "%x. x"]} RS conjunct1 RS mk_conjunctN n i)) ks;
   981     val lsbisE_thms = map (fn i => (mk_specN 2 (sbis_lsbis_thm RS
   982       (bis_def RS @{thm subst[of _ _ "%x. x"]} RS conjunct2 RS mk_conjunctN n i))) RS mp) ks;
   983 
   984     val incl_lsbis_thms =
   985       let
   986         fun mk_concl i R = HOLogic.mk_Trueprop (mk_subset R (mk_lsbis As Bs ss i));
   987         val goals = map2 (fn i => fn R => fold_rev Logic.all (As @ Bs @ ss @ sRs)
   988           (Logic.mk_implies (sbis_prem, mk_concl i R))) ks sRs;
   989       in
   990         map3 (fn goal => fn i => fn def => Skip_Proof.prove lthy [] [] goal
   991           (K (mk_incl_lsbis_tac n i def)) |> Thm.close_derivation) goals ks lsbis_defs
   992       end;
   993 
   994     val equiv_lsbis_thms =
   995       let
   996         fun mk_concl i B = HOLogic.mk_Trueprop (mk_equiv B (mk_lsbis As Bs ss i));
   997         val goals = map2 (fn i => fn B => fold_rev Logic.all (As @ Bs @ ss)
   998           (Logic.mk_implies (coalg_prem, mk_concl i B))) ks Bs;
   999       in
  1000         map3 (fn goal => fn l_incl => fn incl_l =>
  1001           Skip_Proof.prove lthy [] [] goal
  1002             (K (mk_equiv_lsbis_tac sbis_lsbis_thm l_incl incl_l
  1003               bis_diag_thm bis_converse_thm bis_O_thm))
  1004           |> Thm.close_derivation)
  1005         goals lsbis_incl_thms incl_lsbis_thms
  1006       end;
  1007 
  1008     val timer = time (timer "Bisimulations");
  1009 
  1010     (* bounds *)
  1011 
  1012     val (lthy, sbd, sbdT,
  1013       sbd_card_order, sbd_Cinfinite, sbd_Cnotzero, sbd_Card_order, set_sbdss, in_sbds) =
  1014       if n = 1
  1015       then (lthy, sum_bd, sum_bdT,
  1016         bd_card_order, bd_Cinfinite, bd_Cnotzero, bd_Card_order, set_bdss, in_bds)
  1017       else
  1018         let
  1019           val sbdT_bind = Binding.suffix_name ("_" ^ sum_bdTN) b;
  1020 
  1021           val ((sbdT_name, (sbdT_glob_info, sbdT_loc_info)), lthy) =
  1022             typedef true NONE (sbdT_bind, params, NoSyn)
  1023               (HOLogic.mk_UNIV sum_bdT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
  1024 
  1025           val sbdT = Type (sbdT_name, params');
  1026           val Abs_sbdT = Const (#Abs_name sbdT_glob_info, sum_bdT --> sbdT);
  1027 
  1028           val sbd_bind = Binding.suffix_name ("_" ^ sum_bdN) b;
  1029           val sbd_name = Binding.name_of sbd_bind;
  1030           val sbd_def_bind = (Thm.def_binding sbd_bind, []);
  1031 
  1032           val sbd_spec = HOLogic.mk_Trueprop
  1033             (HOLogic.mk_eq (Free (sbd_name, mk_relT (`I sbdT)), mk_dir_image sum_bd Abs_sbdT));
  1034 
  1035           val ((sbd_free, (_, sbd_def_free)), (lthy, lthy_old)) =
  1036             lthy
  1037             |> Specification.definition (SOME (sbd_bind, NONE, NoSyn), (sbd_def_bind, sbd_spec))
  1038             ||> `Local_Theory.restore;
  1039 
  1040           (*transforms defined frees into consts*)
  1041           val phi = Proof_Context.export_morphism lthy_old lthy;
  1042 
  1043           val sbd_def = Morphism.thm phi sbd_def_free;
  1044           val sbd = Const (fst (Term.dest_Const (Morphism.term phi sbd_free)), mk_relT (`I sbdT));
  1045 
  1046           val sbdT_set_def = the (#set_def sbdT_loc_info);
  1047           val sbdT_Abs_inject = #Abs_inject sbdT_loc_info;
  1048           val sbdT_Abs_cases = #Abs_cases sbdT_loc_info;
  1049 
  1050           val Abs_sbdT_inj = mk_Abs_inj_thm sbdT_set_def sbdT_Abs_inject;
  1051           val Abs_sbdT_bij = mk_Abs_bij_thm lthy sbdT_set_def sbdT_Abs_inject sbdT_Abs_cases;
  1052 
  1053           fun mk_sum_Cinfinite [thm] = thm
  1054             | mk_sum_Cinfinite (thm :: thms) =
  1055               @{thm Cinfinite_csum_strong} OF [thm, mk_sum_Cinfinite thms];
  1056 
  1057           val sum_Cinfinite = mk_sum_Cinfinite bd_Cinfinites;
  1058           val sum_Card_order = sum_Cinfinite RS conjunct2;
  1059 
  1060           fun mk_sum_card_order [thm] = thm
  1061             | mk_sum_card_order (thm :: thms) =
  1062               @{thm card_order_csum} OF [thm, mk_sum_card_order thms];
  1063 
  1064           val sum_card_order = mk_sum_card_order bd_card_orders;
  1065 
  1066           val sbd_ordIso = Local_Defs.fold lthy [sbd_def]
  1067             (@{thm dir_image} OF [Abs_sbdT_inj, sum_Card_order]);
  1068           val sbd_card_order =  Local_Defs.fold lthy [sbd_def]
  1069             (@{thm card_order_dir_image} OF [Abs_sbdT_bij, sum_card_order]);
  1070           val sbd_Cinfinite = @{thm Cinfinite_cong} OF [sbd_ordIso, sum_Cinfinite];
  1071           val sbd_Cnotzero = sbd_Cinfinite RS @{thm Cinfinite_Cnotzero};
  1072           val sbd_Card_order = sbd_Cinfinite RS conjunct2;
  1073 
  1074           fun mk_set_sbd i bd_Card_order bds =
  1075             map (fn thm => @{thm ordLeq_ordIso_trans} OF
  1076               [bd_Card_order RS mk_ordLeq_csum n i thm, sbd_ordIso]) bds;
  1077           val set_sbdss = map3 mk_set_sbd ks bd_Card_orders set_bdss;
  1078 
  1079           fun mk_in_sbd i Co Cnz bd =
  1080             Cnz RS ((@{thm ordLeq_ordIso_trans} OF
  1081               [(Co RS mk_ordLeq_csum n i (Co RS @{thm ordLeq_refl})), sbd_ordIso]) RS
  1082               (bd RS @{thm ordLeq_transitive[OF _
  1083                 cexp_mono2_Cnotzero[OF _ csum_Cnotzero2[OF ctwo_Cnotzero]]]}));
  1084           val in_sbds = map4 mk_in_sbd ks bd_Card_orders bd_Cnotzeros in_bds;
  1085        in
  1086          (lthy, sbd, sbdT,
  1087            sbd_card_order, sbd_Cinfinite, sbd_Cnotzero, sbd_Card_order, set_sbdss, in_sbds)
  1088        end;
  1089 
  1090     fun mk_sbd_sbd 1 = sbd_Card_order RS @{thm ordIso_refl}
  1091       | mk_sbd_sbd n = @{thm csum_absorb1} OF
  1092           [sbd_Cinfinite, mk_sbd_sbd (n - 1) RS @{thm ordIso_imp_ordLeq}];
  1093 
  1094     val sbd_sbd_thm = mk_sbd_sbd n;
  1095 
  1096     val sbdTs = replicate n sbdT;
  1097     val sum_sbd = Library.foldr1 (uncurry mk_csum) (replicate n sbd);
  1098     val sum_sbdT = mk_sumTN sbdTs;
  1099     val sum_sbd_listT = HOLogic.listT sum_sbdT;
  1100     val sum_sbd_list_setT = HOLogic.mk_setT sum_sbd_listT;
  1101     val bdTs = passiveAs @ replicate n sbdT;
  1102     val to_sbd_maps = map4 mk_map_of_bnf Dss Ass (replicate n bdTs) bnfs;
  1103     val bdFTs = mk_FTs bdTs;
  1104     val sbdFT = mk_sumTN bdFTs;
  1105     val treeT = HOLogic.mk_prodT (sum_sbd_list_setT, sum_sbd_listT --> sbdFT);
  1106     val treeQT = HOLogic.mk_setT treeT;
  1107     val treeTs = passiveAs @ replicate n treeT;
  1108     val treeQTs = passiveAs @ replicate n treeQT;
  1109     val treeFTs = mk_FTs treeTs;
  1110     val tree_maps = map4 mk_map_of_bnf Dss (replicate n bdTs) (replicate n treeTs) bnfs;
  1111     val final_maps = map4 mk_map_of_bnf Dss (replicate n treeTs) (replicate n treeQTs) bnfs;
  1112     val tree_setss = mk_setss treeTs;
  1113     val isNode_setss = mk_setss (passiveAs @ replicate n sbdT);
  1114 
  1115     val root = HOLogic.mk_set sum_sbd_listT [HOLogic.mk_list sum_sbdT []];
  1116     val Zero = HOLogic.mk_tuple (map (fn U => absdummy U root) activeAs);
  1117     val Lev_recT = fastype_of Zero;
  1118     val LevT = Library.foldr (op -->) (sTs, HOLogic.natT --> Lev_recT);
  1119 
  1120     val Nil = HOLogic.mk_tuple (map3 (fn i => fn z => fn z'=>
  1121       Term.absfree z' (mk_InN activeAs z i)) ks zs zs');
  1122     val rv_recT = fastype_of Nil;
  1123     val rvT = Library.foldr (op -->) (sTs, sum_sbd_listT --> rv_recT);
  1124 
  1125     val (((((((((((sumx, sumx'), (kks, kks')), (kl, kl')), (kl_copy, kl'_copy)), (Kl, Kl')),
  1126       (lab, lab')), (Kl_lab, Kl_lab')), xs), (Lev_rec, Lev_rec')), (rv_rec, rv_rec')),
  1127       names_lthy) = names_lthy
  1128       |> yield_singleton (apfst (op ~~) oo mk_Frees' "sumx") sum_sbdT
  1129       ||>> mk_Frees' "k" sbdTs
  1130       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
  1131       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
  1132       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl") sum_sbd_list_setT
  1133       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "lab") (sum_sbd_listT --> sbdFT)
  1134       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl_lab") treeT
  1135       ||>> mk_Frees "x" bdFTs
  1136       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") Lev_recT
  1137       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") rv_recT;
  1138 
  1139     val (k, k') = (hd kks, hd kks')
  1140 
  1141     val timer = time (timer "Bounds");
  1142 
  1143     (* tree coalgebra *)
  1144 
  1145     fun isNode_bind i = Binding.suffix_name ("_" ^ isNodeN ^ (if n = 1 then "" else
  1146       string_of_int i)) b;
  1147     val isNode_name = Binding.name_of o isNode_bind;
  1148     val isNode_def_bind = rpair [] o Thm.def_binding o isNode_bind;
  1149 
  1150     val isNodeT =
  1151       Library.foldr (op -->) (map fastype_of (As @ [Kl, lab, kl]), HOLogic.boolT);
  1152 
  1153     val Succs = map3 (fn i => fn k => fn k' =>
  1154       HOLogic.mk_Collect (fst k', snd k', HOLogic.mk_mem (mk_InN sbdTs k i, mk_Succ Kl kl)))
  1155       ks kks kks';
  1156 
  1157     fun isNode_spec sets x i =
  1158       let
  1159         val (passive_sets, active_sets) = chop m (map (fn set => set $ x) sets);
  1160         val lhs = Term.list_comb (Free (isNode_name i, isNodeT), As @ [Kl, lab, kl]);
  1161         val rhs = list_exists_free [x]
  1162           (Library.foldr1 HOLogic.mk_conj (HOLogic.mk_eq (lab $ kl, mk_InN bdFTs x i) ::
  1163           map2 mk_subset passive_sets As @ map2 (curry HOLogic.mk_eq) active_sets Succs));
  1164       in
  1165         mk_Trueprop_eq (lhs, rhs)
  1166       end;
  1167 
  1168     val ((isNode_frees, (_, isNode_def_frees)), (lthy, lthy_old)) =
  1169       lthy
  1170       |> fold_map3 (fn i => fn x => fn sets => Specification.definition
  1171         (SOME (isNode_bind i, NONE, NoSyn), (isNode_def_bind i, isNode_spec sets x i)))
  1172         ks xs isNode_setss
  1173       |>> apsnd split_list o split_list
  1174       ||> `Local_Theory.restore;
  1175 
  1176     (*transforms defined frees into consts*)
  1177     val phi = Proof_Context.export_morphism lthy_old lthy;
  1178 
  1179     val isNode_defs = map (Morphism.thm phi) isNode_def_frees;
  1180     val isNodes = map (fst o Term.dest_Const o Morphism.term phi) isNode_frees;
  1181 
  1182     fun mk_isNode As kl i =
  1183       Term.list_comb (Const (nth isNodes (i - 1), isNodeT), As @ [Kl, lab, kl]);
  1184 
  1185     val isTree =
  1186       let
  1187         val empty = HOLogic.mk_mem (HOLogic.mk_list sum_sbdT [], Kl);
  1188         val Field = mk_subset Kl (mk_Field (mk_clists sum_sbd));
  1189         val prefCl = mk_prefCl Kl;
  1190 
  1191         val tree = mk_Ball Kl (Term.absfree kl'
  1192           (HOLogic.mk_conj
  1193             (Library.foldr1 HOLogic.mk_disj (map (mk_isNode As kl) ks),
  1194             Library.foldr1 HOLogic.mk_conj (map4 (fn Succ => fn i => fn k => fn k' =>
  1195               mk_Ball Succ (Term.absfree k' (mk_isNode As
  1196                 (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i])) i)))
  1197             Succs ks kks kks'))));
  1198 
  1199         val undef = list_all_free [kl] (HOLogic.mk_imp
  1200           (HOLogic.mk_not (HOLogic.mk_mem (kl, Kl)),
  1201           HOLogic.mk_eq (lab $ kl, mk_undefined sbdFT)));
  1202       in
  1203         Library.foldr1 HOLogic.mk_conj [empty, Field, prefCl, tree, undef]
  1204       end;
  1205 
  1206     fun carT_bind i = Binding.suffix_name ("_" ^ carTN ^ (if n = 1 then "" else
  1207       string_of_int i)) b;
  1208     val carT_name = Binding.name_of o carT_bind;
  1209     val carT_def_bind = rpair [] o Thm.def_binding o carT_bind;
  1210 
  1211     fun carT_spec i =
  1212       let
  1213         val carTT = Library.foldr (op -->) (ATs, HOLogic.mk_setT treeT);
  1214 
  1215         val lhs = Term.list_comb (Free (carT_name i, carTT), As);
  1216         val rhs = HOLogic.mk_Collect (fst Kl_lab', snd Kl_lab', list_exists_free [Kl, lab]
  1217           (HOLogic.mk_conj (HOLogic.mk_eq (Kl_lab, HOLogic.mk_prod (Kl, lab)),
  1218             HOLogic.mk_conj (isTree, mk_isNode As (HOLogic.mk_list sum_sbdT []) i))));
  1219       in
  1220         mk_Trueprop_eq (lhs, rhs)
  1221       end;
  1222 
  1223     val ((carT_frees, (_, carT_def_frees)), (lthy, lthy_old)) =
  1224       lthy
  1225       |> fold_map (fn i => Specification.definition
  1226         (SOME (carT_bind i, NONE, NoSyn), (carT_def_bind i, carT_spec i))) ks
  1227       |>> apsnd split_list o split_list
  1228       ||> `Local_Theory.restore;
  1229 
  1230     (*transforms defined frees into consts*)
  1231     val phi = Proof_Context.export_morphism lthy_old lthy;
  1232 
  1233     val carT_defs = map (Morphism.thm phi) carT_def_frees;
  1234     val carTs = map (fst o Term.dest_Const o Morphism.term phi) carT_frees;
  1235 
  1236     fun mk_carT As i = Term.list_comb
  1237       (Const (nth carTs (i - 1),
  1238          Library.foldr (op -->) (map fastype_of As, HOLogic.mk_setT treeT)), As);
  1239 
  1240     fun strT_bind i = Binding.suffix_name ("_" ^ strTN ^ (if n = 1 then "" else
  1241       string_of_int i)) b;
  1242     val strT_name = Binding.name_of o strT_bind;
  1243     val strT_def_bind = rpair [] o Thm.def_binding o strT_bind;
  1244 
  1245     fun strT_spec mapFT FT i =
  1246       let
  1247         val strTT = treeT --> FT;
  1248 
  1249         fun mk_f i k k' =
  1250           let val in_k = mk_InN sbdTs k i;
  1251           in Term.absfree k' (HOLogic.mk_prod (mk_Shift Kl in_k, mk_shift lab in_k)) end;
  1252 
  1253         val f = Term.list_comb (mapFT, passive_ids @ map3 mk_f ks kks kks');
  1254         val (fTs1, fTs2) = apsnd tl (chop (i - 1) (map (fn T => T --> FT) bdFTs));
  1255         val fs = map mk_undefined fTs1 @ (f :: map mk_undefined fTs2);
  1256         val lhs = Free (strT_name i, strTT);
  1257         val rhs = HOLogic.mk_split (Term.absfree Kl' (Term.absfree lab'
  1258           (mk_sum_caseN fs $ (lab $ HOLogic.mk_list sum_sbdT []))));
  1259       in
  1260         mk_Trueprop_eq (lhs, rhs)
  1261       end;
  1262 
  1263     val ((strT_frees, (_, strT_def_frees)), (lthy, lthy_old)) =
  1264       lthy
  1265       |> fold_map3 (fn i => fn mapFT => fn FT => Specification.definition
  1266         (SOME (strT_bind i, NONE, NoSyn), (strT_def_bind i, strT_spec mapFT FT i)))
  1267         ks tree_maps treeFTs
  1268       |>> apsnd split_list o split_list
  1269       ||> `Local_Theory.restore;
  1270 
  1271     (*transforms defined frees into consts*)
  1272     val phi = Proof_Context.export_morphism lthy_old lthy;
  1273 
  1274     val strT_defs = map ((fn def => trans OF [def RS fun_cong, @{thm prod.cases}]) o
  1275       Morphism.thm phi) strT_def_frees;
  1276     val strTs = map (fst o Term.dest_Const o Morphism.term phi) strT_frees;
  1277 
  1278     fun mk_strT FT i = Const (nth strTs (i - 1), treeT --> FT);
  1279 
  1280     val carTAs = map (mk_carT As) ks;
  1281     val carTAs_copy = map (mk_carT As_copy) ks;
  1282     val strTAs = map2 mk_strT treeFTs ks;
  1283     val hset_strTss = map (fn i => map2 (mk_hset strTAs i) ls passiveAs) ks;
  1284 
  1285     val coalgT_thm =
  1286       Skip_Proof.prove lthy [] []
  1287         (fold_rev Logic.all As (HOLogic.mk_Trueprop (mk_coalg As carTAs strTAs)))
  1288         (mk_coalgT_tac m (coalg_def :: isNode_defs @ carT_defs) strT_defs set_natural'ss)
  1289       |> Thm.close_derivation;
  1290 
  1291     val card_of_carT_thms =
  1292       let
  1293         val lhs = mk_card_of
  1294           (HOLogic.mk_Collect (fst Kl_lab', snd Kl_lab', list_exists_free [Kl, lab]
  1295             (HOLogic.mk_conj (HOLogic.mk_eq (Kl_lab, HOLogic.mk_prod (Kl, lab)), isTree))));
  1296         val rhs = mk_cexp
  1297           (if m = 0 then ctwo else
  1298             (mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo))
  1299             (mk_cexp sbd sbd);
  1300         val card_of_carT =
  1301           Skip_Proof.prove lthy [] []
  1302             (fold_rev Logic.all As (HOLogic.mk_Trueprop (mk_ordLeq lhs rhs)))
  1303             (K (mk_card_of_carT_tac m isNode_defs sbd_sbd_thm
  1304               sbd_card_order sbd_Card_order sbd_Cinfinite sbd_Cnotzero in_sbds))
  1305           |> Thm.close_derivation
  1306       in
  1307         map (fn def => @{thm ordLeq_transitive[OF
  1308           card_of_mono1[OF ord_eq_le_trans[OF _ Collect_restrict']]]} OF [def, card_of_carT])
  1309         carT_defs
  1310       end;
  1311 
  1312     val carT_set_thmss =
  1313       let
  1314         val Kl_lab = HOLogic.mk_prod (Kl, lab);
  1315         fun mk_goal carT strT set k i =
  1316           fold_rev Logic.all (sumx :: Kl :: lab :: k :: kl :: As)
  1317             (Logic.list_implies (map HOLogic.mk_Trueprop
  1318               [HOLogic.mk_mem (Kl_lab, carT), HOLogic.mk_mem (mk_Cons sumx kl, Kl),
  1319               HOLogic.mk_eq (sumx, mk_InN sbdTs k i)],
  1320             HOLogic.mk_Trueprop (HOLogic.mk_mem
  1321               (HOLogic.mk_prod (mk_Shift Kl sumx, mk_shift lab sumx),
  1322               set $ (strT $ Kl_lab)))));
  1323 
  1324         val goalss = map3 (fn carT => fn strT => fn sets =>
  1325           map3 (mk_goal carT strT) (drop m sets) kks ks) carTAs strTAs tree_setss;
  1326       in
  1327         map6 (fn i => fn goals =>
  1328             fn carT_def => fn strT_def => fn isNode_def => fn set_naturals =>
  1329           map2 (fn goal => fn set_natural =>
  1330             Skip_Proof.prove lthy [] [] goal
  1331               (mk_carT_set_tac n i carT_def strT_def isNode_def set_natural)
  1332             |> Thm.close_derivation)
  1333           goals (drop m set_naturals))
  1334         ks goalss carT_defs strT_defs isNode_defs set_natural'ss
  1335       end;
  1336 
  1337     val carT_set_thmss' = transpose carT_set_thmss;
  1338 
  1339     val isNode_hset_thmss =
  1340       let
  1341         val Kl_lab = HOLogic.mk_prod (Kl, lab);
  1342         fun mk_Kl_lab carT = HOLogic.mk_mem (Kl_lab, carT);
  1343 
  1344         val strT_hset_thmsss =
  1345           let
  1346             val strT_hset_thms =
  1347               let
  1348                 fun mk_lab_kl i x = HOLogic.mk_eq (lab $ kl, mk_InN bdFTs x i);
  1349 
  1350                 fun mk_inner_conjunct j T i x set i' carT =
  1351                   HOLogic.mk_imp (HOLogic.mk_conj (mk_Kl_lab carT, mk_lab_kl i x),
  1352                     mk_subset (set $ x) (mk_hset strTAs i' j T $ Kl_lab));
  1353 
  1354                 fun mk_conjunct j T i x set =
  1355                   Library.foldr1 HOLogic.mk_conj (map2 (mk_inner_conjunct j T i x set) ks carTAs);
  1356 
  1357                 fun mk_concl j T = list_all_free (Kl :: lab :: xs @ As)
  1358                   (HOLogic.mk_imp (HOLogic.mk_mem (kl, Kl),
  1359                     Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T)
  1360                       ks xs (map (fn xs => nth xs (j - 1)) isNode_setss))));
  1361                 val concls = map2 mk_concl ls passiveAs;
  1362 
  1363                 val cTs = [SOME (certifyT lthy sum_sbdT)];
  1364                 val arg_cong_cTs = map (SOME o certifyT lthy) treeFTs;
  1365                 val ctss =
  1366                   map (fn phi => map (SOME o certify lthy) [Term.absfree kl' phi, kl]) concls;
  1367 
  1368                 val goals = map HOLogic.mk_Trueprop concls;
  1369               in
  1370                 map5 (fn j => fn goal => fn cts => fn set_incl_hsets => fn set_hset_incl_hsetss =>
  1371                   singleton (Proof_Context.export names_lthy lthy)
  1372                     (Skip_Proof.prove lthy [] [] goal
  1373                       (K (mk_strT_hset_tac n m j arg_cong_cTs cTs cts
  1374                         carT_defs strT_defs isNode_defs
  1375                         set_incl_hsets set_hset_incl_hsetss coalg_set_thmss' carT_set_thmss'
  1376                         coalgT_thm set_natural'ss)))
  1377                   |> Thm.close_derivation)
  1378                 ls goals ctss set_incl_hset_thmss' set_hset_incl_hset_thmsss''
  1379               end;
  1380 
  1381             val strT_hset'_thms = map (fn thm => mk_specN (2 + n + m) thm RS mp) strT_hset_thms;
  1382           in
  1383             map (fn thm => map (fn i => map (fn i' =>
  1384               thm RS mk_conjunctN n i RS mk_conjunctN n i' RS mp) ks) ks) strT_hset'_thms
  1385           end;
  1386 
  1387         val carT_prems = map (fn carT =>
  1388           HOLogic.mk_Trueprop (HOLogic.mk_mem (Kl_lab, carT))) carTAs_copy;
  1389         val prem = HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, Kl));
  1390         val in_prems = map (fn hsets =>
  1391           HOLogic.mk_Trueprop (HOLogic.mk_mem (Kl_lab, mk_in As hsets treeT))) hset_strTss;
  1392         val isNode_premss = replicate n (map (HOLogic.mk_Trueprop o mk_isNode As_copy kl) ks);
  1393         val conclss = replicate n (map (HOLogic.mk_Trueprop o mk_isNode As kl) ks);
  1394       in
  1395         map5 (fn carT_prem => fn isNode_prems => fn in_prem => fn concls => fn strT_hset_thmss =>
  1396           map4 (fn isNode_prem => fn concl => fn isNode_def => fn strT_hset_thms =>
  1397             Skip_Proof.prove lthy [] []
  1398               (fold_rev Logic.all (Kl :: lab :: kl :: As @ As_copy)
  1399                 (Logic.list_implies ([carT_prem, prem, isNode_prem, in_prem], concl)))
  1400               (mk_isNode_hset_tac n isNode_def strT_hset_thms)
  1401             |> Thm.close_derivation)
  1402           isNode_prems concls isNode_defs
  1403           (if m = 0 then replicate n [] else transpose strT_hset_thmss))
  1404         carT_prems isNode_premss in_prems conclss
  1405         (if m = 0 then replicate n [] else transpose (map transpose strT_hset_thmsss))
  1406       end;
  1407 
  1408     val timer = time (timer "Tree coalgebra");
  1409 
  1410     fun mk_to_sbd s x i i' =
  1411       mk_toCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
  1412     fun mk_from_sbd s x i i' =
  1413       mk_fromCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
  1414 
  1415     fun mk_to_sbd_thmss thm = map (map (fn set_sbd =>
  1416       thm OF [set_sbd, sbd_Card_order]) o drop m) set_sbdss;
  1417 
  1418     val to_sbd_inj_thmss = mk_to_sbd_thmss @{thm toCard_inj};
  1419     val to_sbd_thmss = mk_to_sbd_thmss @{thm toCard};
  1420     val from_to_sbd_thmss = mk_to_sbd_thmss @{thm fromCard_toCard};
  1421 
  1422     val Lev_bind = Binding.suffix_name ("_" ^ LevN) b;
  1423     val Lev_name = Binding.name_of Lev_bind;
  1424     val Lev_def_bind = rpair [] (Thm.def_binding Lev_bind);
  1425 
  1426     val Lev_spec =
  1427       let
  1428         fun mk_Suc i s setsAs a a' =
  1429           let
  1430             val sets = drop m setsAs;
  1431             fun mk_set i' set b =
  1432               let
  1433                 val Cons = HOLogic.mk_eq (kl_copy,
  1434                   mk_Cons (mk_InN sbdTs (mk_to_sbd s a i i' $ b) i') kl)
  1435                 val b_set = HOLogic.mk_mem (b, set $ (s $ a));
  1436                 val kl_rec = HOLogic.mk_mem (kl, mk_nthN n Lev_rec i' $ b);
  1437               in
  1438                 HOLogic.mk_Collect (fst kl'_copy, snd kl'_copy, list_exists_free [b, kl]
  1439                   (HOLogic.mk_conj (Cons, HOLogic.mk_conj (b_set, kl_rec))))
  1440               end;
  1441           in
  1442             Term.absfree a' (Library.foldl1 mk_union (map3 mk_set ks sets zs_copy))
  1443           end;
  1444 
  1445         val Suc = Term.absdummy HOLogic.natT (Term.absfree Lev_rec'
  1446           (HOLogic.mk_tuple (map5 mk_Suc ks ss setssAs zs zs')));
  1447 
  1448         val lhs = Term.list_comb (Free (Lev_name, LevT), ss);
  1449         val rhs = mk_nat_rec Zero Suc;
  1450       in
  1451         mk_Trueprop_eq (lhs, rhs)
  1452       end;
  1453 
  1454     val ((Lev_free, (_, Lev_def_free)), (lthy, lthy_old)) =
  1455       lthy
  1456       |> Specification.definition (SOME (Lev_bind, NONE, NoSyn), (Lev_def_bind, Lev_spec))
  1457       ||> `Local_Theory.restore;
  1458 
  1459     (*transforms defined frees into consts*)
  1460     val phi = Proof_Context.export_morphism lthy_old lthy;
  1461 
  1462     val Lev_def = Morphism.thm phi Lev_def_free;
  1463     val Lev = fst (Term.dest_Const (Morphism.term phi Lev_free));
  1464 
  1465     fun mk_Lev ss nat i =
  1466       let
  1467         val Ts = map fastype_of ss;
  1468         val LevT = Library.foldr (op -->) (Ts, HOLogic.natT -->
  1469           HOLogic.mk_tupleT (map (fn U => domain_type U --> sum_sbd_list_setT) Ts));
  1470       in
  1471         mk_nthN n (Term.list_comb (Const (Lev, LevT), ss) $ nat) i
  1472       end;
  1473 
  1474     val Lev_0s = flat (mk_rec_simps n @{thm nat_rec_0} [Lev_def]);
  1475     val Lev_Sucs = flat (mk_rec_simps n @{thm nat_rec_Suc} [Lev_def]);
  1476 
  1477     val rv_bind = Binding.suffix_name ("_" ^ rvN) b;
  1478     val rv_name = Binding.name_of rv_bind;
  1479     val rv_def_bind = rpair [] (Thm.def_binding rv_bind);
  1480 
  1481     val rv_spec =
  1482       let
  1483         fun mk_Cons i s b b' =
  1484           let
  1485             fun mk_case i' =
  1486               Term.absfree k' (mk_nthN n rv_rec i' $ (mk_from_sbd s b i i' $ k));
  1487           in
  1488             Term.absfree b' (mk_sum_caseN (map mk_case ks) $ sumx)
  1489           end;
  1490 
  1491         val Cons = Term.absfree sumx' (Term.absdummy sum_sbd_listT (Term.absfree rv_rec'
  1492           (HOLogic.mk_tuple (map4 mk_Cons ks ss zs zs'))));
  1493 
  1494         val lhs = Term.list_comb (Free (rv_name, rvT), ss);
  1495         val rhs = mk_list_rec Nil Cons;
  1496       in
  1497         mk_Trueprop_eq (lhs, rhs)
  1498       end;
  1499 
  1500     val ((rv_free, (_, rv_def_free)), (lthy, lthy_old)) =
  1501       lthy
  1502       |> Specification.definition (SOME (rv_bind, NONE, NoSyn), (rv_def_bind, rv_spec))
  1503       ||> `Local_Theory.restore;
  1504 
  1505     (*transforms defined frees into consts*)
  1506     val phi = Proof_Context.export_morphism lthy_old lthy;
  1507 
  1508     val rv_def = Morphism.thm phi rv_def_free;
  1509     val rv = fst (Term.dest_Const (Morphism.term phi rv_free));
  1510 
  1511     fun mk_rv ss kl i =
  1512       let
  1513         val Ts = map fastype_of ss;
  1514         val As = map domain_type Ts;
  1515         val rvT = Library.foldr (op -->) (Ts, fastype_of kl -->
  1516           HOLogic.mk_tupleT (map (fn U => U --> mk_sumTN As) As));
  1517       in
  1518         mk_nthN n (Term.list_comb (Const (rv, rvT), ss) $ kl) i
  1519       end;
  1520 
  1521     val rv_Nils = flat (mk_rec_simps n @{thm list_rec_Nil} [rv_def]);
  1522     val rv_Conss = flat (mk_rec_simps n @{thm list_rec_Cons} [rv_def]);
  1523 
  1524     fun beh_bind i = Binding.suffix_name ("_" ^ behN ^ (if n = 1 then "" else
  1525       string_of_int i)) b;
  1526     val beh_name = Binding.name_of o beh_bind;
  1527     val beh_def_bind = rpair [] o Thm.def_binding o beh_bind;
  1528 
  1529     fun beh_spec i z =
  1530       let
  1531         val mk_behT = Library.foldr (op -->) (map fastype_of (ss @ [z]), treeT);
  1532 
  1533         fun mk_case i to_sbd_map s k k' =
  1534           Term.absfree k' (mk_InN bdFTs
  1535             (Term.list_comb (to_sbd_map, passive_ids @ map (mk_to_sbd s k i) ks) $ (s $ k)) i);
  1536 
  1537         val Lab = Term.absfree kl' (mk_If
  1538           (HOLogic.mk_mem (kl, mk_Lev ss (mk_size kl) i $ z))
  1539           (mk_sum_caseN (map5 mk_case ks to_sbd_maps ss zs zs') $ (mk_rv ss kl i $ z))
  1540           (mk_undefined sbdFT));
  1541 
  1542         val lhs = Term.list_comb (Free (beh_name i, mk_behT), ss) $ z;
  1543         val rhs = HOLogic.mk_prod (mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
  1544           (Term.absfree nat' (mk_Lev ss nat i $ z)), Lab);
  1545       in
  1546         mk_Trueprop_eq (lhs, rhs)
  1547       end;
  1548 
  1549     val ((beh_frees, (_, beh_def_frees)), (lthy, lthy_old)) =
  1550       lthy
  1551       |> fold_map2 (fn i => fn z => Specification.definition
  1552         (SOME (beh_bind i, NONE, NoSyn), (beh_def_bind i, beh_spec i z))) ks zs
  1553       |>> apsnd split_list o split_list
  1554       ||> `Local_Theory.restore;
  1555 
  1556     (*transforms defined frees into consts*)
  1557     val phi = Proof_Context.export_morphism lthy_old lthy;
  1558 
  1559     val beh_defs = map (Morphism.thm phi) beh_def_frees;
  1560     val behs = map (fst o Term.dest_Const o Morphism.term phi) beh_frees;
  1561 
  1562     fun mk_beh ss i =
  1563       let
  1564         val Ts = map fastype_of ss;
  1565         val behT = Library.foldr (op -->) (Ts, nth activeAs (i - 1) --> treeT);
  1566       in
  1567         Term.list_comb (Const (nth behs (i - 1), behT), ss)
  1568       end;
  1569 
  1570     val Lev_sbd_thms =
  1571       let
  1572         fun mk_conjunct i z = mk_subset (mk_Lev ss nat i $ z) (mk_Field (mk_clists sum_sbd));
  1573         val goal = list_all_free zs
  1574           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1575 
  1576         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1577 
  1578         val Lev_sbd = singleton (Proof_Context.export names_lthy lthy)
  1579           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1580             (K (mk_Lev_sbd_tac cts Lev_0s Lev_Sucs to_sbd_thmss))
  1581           |> Thm.close_derivation);
  1582 
  1583         val Lev_sbd' = mk_specN n Lev_sbd;
  1584       in
  1585         map (fn i => Lev_sbd' RS mk_conjunctN n i) ks
  1586       end;
  1587 
  1588     val (length_Lev_thms, length_Lev'_thms) =
  1589       let
  1590         fun mk_conjunct i z = HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1591           HOLogic.mk_eq (mk_size kl, nat));
  1592         val goal = list_all_free (kl :: zs)
  1593           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1594 
  1595         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1596 
  1597         val length_Lev = singleton (Proof_Context.export names_lthy lthy)
  1598           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1599             (K (mk_length_Lev_tac cts Lev_0s Lev_Sucs))
  1600           |> Thm.close_derivation);
  1601 
  1602         val length_Lev' = mk_specN (n + 1) length_Lev;
  1603         val length_Levs = map (fn i => length_Lev' RS mk_conjunctN n i RS mp) ks;
  1604 
  1605         fun mk_goal i z = fold_rev Logic.all (z :: kl :: nat :: ss) (Logic.mk_implies
  1606             (HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z)),
  1607             HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, mk_Lev ss (mk_size kl) i $ z))));
  1608         val goals = map2 mk_goal ks zs;
  1609 
  1610         val length_Levs' = map2 (fn goal => fn length_Lev =>
  1611           Skip_Proof.prove lthy [] [] goal (K (mk_length_Lev'_tac length_Lev))
  1612           |> Thm.close_derivation) goals length_Levs;
  1613       in
  1614         (length_Levs, length_Levs')
  1615       end;
  1616 
  1617     val prefCl_Lev_thms =
  1618       let
  1619         fun mk_conjunct i z = HOLogic.mk_imp
  1620           (HOLogic.mk_conj (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z), mk_subset kl_copy kl),
  1621           HOLogic.mk_mem (kl_copy, mk_Lev ss (mk_size kl_copy) i $ z));
  1622         val goal = list_all_free (kl :: kl_copy :: zs)
  1623           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1624 
  1625         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1626 
  1627         val prefCl_Lev = singleton (Proof_Context.export names_lthy lthy)
  1628           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1629             (K (mk_prefCl_Lev_tac cts Lev_0s Lev_Sucs)))
  1630           |> Thm.close_derivation;
  1631 
  1632         val prefCl_Lev' = mk_specN (n + 2) prefCl_Lev;
  1633       in
  1634         map (fn i => prefCl_Lev' RS mk_conjunctN n i RS mp) ks
  1635       end;
  1636 
  1637     val rv_last_thmss =
  1638       let
  1639         fun mk_conjunct i z i' z_copy = list_exists_free [z_copy]
  1640           (HOLogic.mk_eq
  1641             (mk_rv ss (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i'])) i $ z,
  1642             mk_InN activeAs z_copy i'));
  1643         val goal = list_all_free (k :: zs)
  1644           (Library.foldr1 HOLogic.mk_conj (map2 (fn i => fn z =>
  1645             Library.foldr1 HOLogic.mk_conj
  1646               (map2 (mk_conjunct i z) ks zs_copy)) ks zs));
  1647 
  1648         val cTs = [SOME (certifyT lthy sum_sbdT)];
  1649         val cts = map (SOME o certify lthy) [Term.absfree kl' goal, kl];
  1650 
  1651         val rv_last = singleton (Proof_Context.export names_lthy lthy)
  1652           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1653             (K (mk_rv_last_tac cTs cts rv_Nils rv_Conss)))
  1654           |> Thm.close_derivation;
  1655 
  1656         val rv_last' = mk_specN (n + 1) rv_last;
  1657       in
  1658         map (fn i => map (fn i' => rv_last' RS mk_conjunctN n i RS mk_conjunctN n i') ks) ks
  1659       end;
  1660 
  1661     val set_rv_Lev_thmsss = if m = 0 then replicate n (replicate n []) else
  1662       let
  1663         fun mk_case s sets z z_free = Term.absfree z_free (Library.foldr1 HOLogic.mk_conj
  1664           (map2 (fn set => fn A => mk_subset (set $ (s $ z)) A) (take m sets) As));
  1665 
  1666         fun mk_conjunct i z B = HOLogic.mk_imp
  1667           (HOLogic.mk_conj (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z), HOLogic.mk_mem (z, B)),
  1668           mk_sum_caseN (map4 mk_case ss setssAs zs zs') $ (mk_rv ss kl i $ z));
  1669 
  1670         val goal = list_all_free (kl :: zs)
  1671           (Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct ks zs Bs));
  1672 
  1673         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1674 
  1675         val set_rv_Lev = singleton (Proof_Context.export names_lthy lthy)
  1676           (Skip_Proof.prove lthy [] []
  1677             (Logic.mk_implies (coalg_prem, HOLogic.mk_Trueprop goal))
  1678             (K (mk_set_rv_Lev_tac m cts Lev_0s Lev_Sucs rv_Nils rv_Conss
  1679               coalg_set_thmss from_to_sbd_thmss)))
  1680           |> Thm.close_derivation;
  1681 
  1682         val set_rv_Lev' = mk_specN (n + 1) set_rv_Lev;
  1683       in
  1684         map (fn i => map (fn i' =>
  1685           split_conj_thm (if n = 1 then set_rv_Lev' RS mk_conjunctN n i RS mp
  1686             else set_rv_Lev' RS mk_conjunctN n i RS mp RSN
  1687               (2, @{thm sum_case_cong} RS @{thm subst[of _ _ "%x. x"]}) RS
  1688               (mk_sum_casesN n i' RS @{thm subst[of _ _ "%x. x"]}))) ks) ks
  1689       end;
  1690 
  1691     val set_Lev_thmsss =
  1692       let
  1693         fun mk_conjunct i z =
  1694           let
  1695             fun mk_conjunct' i' sets s z' =
  1696               let
  1697                 fun mk_conjunct'' i'' set z'' = HOLogic.mk_imp
  1698                   (HOLogic.mk_mem (z'', set $ (s $ z')),
  1699                     HOLogic.mk_mem (mk_append (kl,
  1700                       HOLogic.mk_list sum_sbdT [mk_InN sbdTs (mk_to_sbd s z' i' i'' $ z'') i'']),
  1701                       mk_Lev ss (HOLogic.mk_Suc nat) i $ z));
  1702               in
  1703                 HOLogic.mk_imp (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z' i'),
  1704                   (Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct'' ks (drop m sets) zs_copy2)))
  1705               end;
  1706           in
  1707             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1708               Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct' ks setssAs ss zs_copy))
  1709           end;
  1710 
  1711         val goal = list_all_free (kl :: zs @ zs_copy @ zs_copy2)
  1712           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1713 
  1714         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1715 
  1716         val set_Lev = singleton (Proof_Context.export names_lthy lthy)
  1717           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1718             (K (mk_set_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss from_to_sbd_thmss)))
  1719           |> Thm.close_derivation;
  1720 
  1721         val set_Lev' = mk_specN (3 * n + 1) set_Lev;
  1722       in
  1723         map (fn i => map (fn i' => map (fn i'' => set_Lev' RS
  1724           mk_conjunctN n i RS mp RS
  1725           mk_conjunctN n i' RS mp RS
  1726           mk_conjunctN n i'' RS mp) ks) ks) ks
  1727       end;
  1728 
  1729     val set_image_Lev_thmsss =
  1730       let
  1731         fun mk_conjunct i z =
  1732           let
  1733             fun mk_conjunct' i' sets =
  1734               let
  1735                 fun mk_conjunct'' i'' set s z'' = HOLogic.mk_imp
  1736                   (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z'' i''),
  1737                   HOLogic.mk_mem (k, mk_image (mk_to_sbd s z'' i'' i') $ (set $ (s $ z''))));
  1738               in
  1739                 HOLogic.mk_imp (HOLogic.mk_mem
  1740                   (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i']),
  1741                     mk_Lev ss (HOLogic.mk_Suc nat) i $ z),
  1742                   (Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct'' ks sets ss zs_copy)))
  1743               end;
  1744           in
  1745             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1746               Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct' ks (drop m setssAs')))
  1747           end;
  1748 
  1749         val goal = list_all_free (kl :: k :: zs @ zs_copy)
  1750           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1751 
  1752         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1753 
  1754         val set_image_Lev = singleton (Proof_Context.export names_lthy lthy)
  1755           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1756             (K (mk_set_image_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss
  1757               from_to_sbd_thmss to_sbd_inj_thmss)))
  1758           |> Thm.close_derivation;
  1759 
  1760         val set_image_Lev' = mk_specN (2 * n + 2) set_image_Lev;
  1761       in
  1762         map (fn i => map (fn i' => map (fn i'' => set_image_Lev' RS
  1763           mk_conjunctN n i RS mp RS
  1764           mk_conjunctN n i'' RS mp RS
  1765           mk_conjunctN n i' RS mp) ks) ks) ks
  1766       end;
  1767 
  1768     val mor_beh_thm =
  1769       Skip_Proof.prove lthy [] []
  1770         (fold_rev Logic.all (As @ Bs @ ss) (Logic.mk_implies (coalg_prem,
  1771           HOLogic.mk_Trueprop (mk_mor Bs ss carTAs strTAs (map (mk_beh ss) ks)))))
  1772         (mk_mor_beh_tac m mor_def mor_cong_thm
  1773           beh_defs carT_defs strT_defs isNode_defs
  1774           to_sbd_inj_thmss from_to_sbd_thmss Lev_0s Lev_Sucs rv_Nils rv_Conss Lev_sbd_thms
  1775           length_Lev_thms length_Lev'_thms prefCl_Lev_thms rv_last_thmss
  1776           set_rv_Lev_thmsss set_Lev_thmsss set_image_Lev_thmsss
  1777           set_natural'ss coalg_set_thmss map_comp_id_thms map_congs map_arg_cong_thms)
  1778       |> Thm.close_derivation;
  1779 
  1780     val timer = time (timer "Behavioral morphism");
  1781 
  1782     fun mk_LSBIS As i = mk_lsbis As (map (mk_carT As) ks) strTAs i;
  1783     fun mk_car_final As i =
  1784       mk_quotient (mk_carT As i) (mk_LSBIS As i);
  1785     fun mk_str_final As i =
  1786       mk_univ (HOLogic.mk_comp (Term.list_comb (nth final_maps (i - 1),
  1787         passive_ids @ map (mk_proj o mk_LSBIS As) ks), nth strTAs (i - 1)));
  1788 
  1789     val car_finalAs = map (mk_car_final As) ks;
  1790     val str_finalAs = map (mk_str_final As) ks;
  1791     val car_finals = map (mk_car_final passive_UNIVs) ks;
  1792     val str_finals = map (mk_str_final passive_UNIVs) ks;
  1793 
  1794     val coalgT_set_thmss = map (map (fn thm => coalgT_thm RS thm)) coalg_set_thmss;
  1795     val equiv_LSBIS_thms = map (fn thm => coalgT_thm RS thm) equiv_lsbis_thms;
  1796 
  1797     val congruent_str_final_thms =
  1798       let
  1799         fun mk_goal R final_map strT =
  1800           fold_rev Logic.all As (HOLogic.mk_Trueprop
  1801             (mk_congruent R (HOLogic.mk_comp
  1802               (Term.list_comb (final_map, passive_ids @ map (mk_proj o mk_LSBIS As) ks), strT))));
  1803 
  1804         val goals = map3 mk_goal (map (mk_LSBIS As) ks) final_maps strTAs;
  1805       in
  1806         map4 (fn goal => fn lsbisE => fn map_comp_id => fn map_cong =>
  1807           Skip_Proof.prove lthy [] [] goal
  1808             (K (mk_congruent_str_final_tac m lsbisE map_comp_id map_cong equiv_LSBIS_thms))
  1809           |> Thm.close_derivation)
  1810         goals lsbisE_thms map_comp_id_thms map_congs
  1811       end;
  1812 
  1813     val coalg_final_thm = Skip_Proof.prove lthy [] [] (fold_rev Logic.all As
  1814       (HOLogic.mk_Trueprop (mk_coalg As car_finalAs str_finalAs)))
  1815       (K (mk_coalg_final_tac m coalg_def congruent_str_final_thms equiv_LSBIS_thms
  1816         set_natural'ss coalgT_set_thmss))
  1817       |> Thm.close_derivation;
  1818 
  1819     val mor_T_final_thm = Skip_Proof.prove lthy [] [] (fold_rev Logic.all As
  1820       (HOLogic.mk_Trueprop (mk_mor carTAs strTAs car_finalAs str_finalAs
  1821         (map (mk_proj o mk_LSBIS As) ks))))
  1822       (K (mk_mor_T_final_tac mor_def congruent_str_final_thms equiv_LSBIS_thms))
  1823       |> Thm.close_derivation;
  1824 
  1825     val mor_final_thm = mor_comp_thm OF [mor_beh_thm, mor_T_final_thm];
  1826     val in_car_final_thms = map (fn mor_image' => mor_image' OF
  1827       [tcoalg_thm RS mor_final_thm, UNIV_I]) mor_image'_thms;
  1828 
  1829     val timer = time (timer "Final coalgebra");
  1830 
  1831     val ((T_names, (T_glob_infos, T_loc_infos)), lthy) =
  1832       lthy
  1833       |> fold_map3 (fn b => fn car_final => fn in_car_final =>
  1834         typedef false NONE (b, params, NoSyn) car_final NONE
  1835           (EVERY' [rtac exI, rtac in_car_final] 1)) bs car_finals in_car_final_thms
  1836       |>> apsnd split_list o split_list;
  1837 
  1838     val Ts = map (fn name => Type (name, params')) T_names;
  1839     fun mk_Ts passive = map (Term.typ_subst_atomic (passiveAs ~~ passive)) Ts;
  1840     val Ts' = mk_Ts passiveBs;
  1841     val Ts'' = mk_Ts passiveCs;
  1842     val Rep_Ts = map2 (fn info => fn T => Const (#Rep_name info, T --> treeQT)) T_glob_infos Ts;
  1843     val Abs_Ts = map2 (fn info => fn T => Const (#Abs_name info, treeQT --> T)) T_glob_infos Ts;
  1844 
  1845     val Reps = map #Rep T_loc_infos;
  1846     val Rep_injects = map #Rep_inject T_loc_infos;
  1847     val Rep_inverses = map #Rep_inverse T_loc_infos;
  1848     val Abs_inverses = map #Abs_inverse T_loc_infos;
  1849 
  1850     val timer = time (timer "THE TYPEDEFs & Rep/Abs thms");
  1851 
  1852     val UNIVs = map HOLogic.mk_UNIV Ts;
  1853     val FTs = mk_FTs (passiveAs @ Ts);
  1854     val FTs' = mk_FTs (passiveBs @ Ts);
  1855     val prodTs = map (HOLogic.mk_prodT o `I) Ts;
  1856     val prodFTs = mk_FTs (passiveAs @ prodTs);
  1857     val FTs_setss = mk_setss (passiveAs @ Ts);
  1858     val FTs'_setss = mk_setss (passiveBs @ Ts);
  1859     val prodFT_setss = mk_setss (passiveAs @ prodTs);
  1860     val map_FTs = map2 (fn Ds => mk_map_of_bnf Ds treeQTs (passiveAs @ Ts)) Dss bnfs;
  1861     val map_FT_nths = map2 (fn Ds =>
  1862       mk_map_of_bnf Ds (passiveAs @ prodTs) (passiveAs @ Ts)) Dss bnfs;
  1863     val fstsTs = map fst_const prodTs;
  1864     val sndsTs = map snd_const prodTs;
  1865     val unfTs = map2 (curry (op -->)) Ts FTs;
  1866     val fldTs = map2 (curry (op -->)) FTs Ts;
  1867     val coiter_fTs = map2 (curry op -->) activeAs Ts;
  1868     val corec_sTs = map (Term.typ_subst_atomic (activeBs ~~ Ts)) sum_sTs;
  1869     val corec_maps = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls;
  1870     val corec_maps_rev = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls_rev;
  1871     val corec_Inls = map (Term.subst_atomic_types (activeBs ~~ Ts)) Inls;
  1872 
  1873     val (((((((((((((Jzs, Jzs'), (Jz's, Jz's')), Jzs_copy), Jzs1), Jzs2), Jpairs),
  1874       FJzs), TRs), coiter_fs), coiter_fs_copy), corec_ss), phis), names_lthy) = names_lthy
  1875       |> mk_Frees' "z" Ts
  1876       ||>> mk_Frees' "z" Ts'
  1877       ||>> mk_Frees "z" Ts
  1878       ||>> mk_Frees "z1" Ts
  1879       ||>> mk_Frees "z2" Ts
  1880       ||>> mk_Frees "j" (map2 (curry HOLogic.mk_prodT) Ts Ts')
  1881       ||>> mk_Frees "x" prodFTs
  1882       ||>> mk_Frees "R" (map (mk_relT o `I) Ts)
  1883       ||>> mk_Frees "f" coiter_fTs
  1884       ||>> mk_Frees "g" coiter_fTs
  1885       ||>> mk_Frees "s" corec_sTs
  1886       ||>> mk_Frees "phi" (map (fn T => T --> T --> HOLogic.boolT) Ts);
  1887 
  1888     fun unf_bind i = Binding.suffix_name ("_" ^ unfN) (nth bs (i - 1));
  1889     val unf_name = Binding.name_of o unf_bind;
  1890     val unf_def_bind = rpair [] o Thm.def_binding o unf_bind;
  1891 
  1892     fun unf_spec i rep str map_FT unfT Jz Jz' =
  1893       let
  1894         val lhs = Free (unf_name i, unfT);
  1895         val rhs = Term.absfree Jz'
  1896           (Term.list_comb (map_FT, map HOLogic.id_const passiveAs @ Abs_Ts) $
  1897             (str $ (rep $ Jz)));
  1898       in
  1899         mk_Trueprop_eq (lhs, rhs)
  1900       end;
  1901 
  1902     val ((unf_frees, (_, unf_def_frees)), (lthy, lthy_old)) =
  1903       lthy
  1904       |> fold_map7 (fn i => fn rep => fn str => fn map => fn unfT => fn Jz => fn Jz' =>
  1905         Specification.definition
  1906           (SOME (unf_bind i, NONE, NoSyn), (unf_def_bind i, unf_spec i rep str map unfT Jz Jz')))
  1907           ks Rep_Ts str_finals map_FTs unfTs Jzs Jzs'
  1908       |>> apsnd split_list o split_list
  1909       ||> `Local_Theory.restore;
  1910 
  1911     (*transforms defined frees into consts*)
  1912     val phi = Proof_Context.export_morphism lthy_old lthy;
  1913     fun mk_unfs passive =
  1914       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ (deads @ passive)) o
  1915         Morphism.term phi) unf_frees;
  1916     val unfs = mk_unfs passiveAs;
  1917     val unf's = mk_unfs passiveBs;
  1918     val unf_defs = map ((fn thm => thm RS fun_cong) o Morphism.thm phi) unf_def_frees;
  1919 
  1920     val coalg_final_set_thmss = map (map (fn thm => coalg_final_thm RS thm)) coalg_set_thmss;
  1921     val (mor_Rep_thm, mor_Abs_thm) =
  1922       let
  1923         val mor_Rep =
  1924           Skip_Proof.prove lthy [] []
  1925             (HOLogic.mk_Trueprop (mk_mor UNIVs unfs car_finals str_finals Rep_Ts))
  1926             (mk_mor_Rep_tac m (mor_def :: unf_defs) Reps Abs_inverses coalg_final_set_thmss
  1927               map_comp_id_thms map_congL_thms)
  1928           |> Thm.close_derivation;
  1929 
  1930         val mor_Abs =
  1931           Skip_Proof.prove lthy [] []
  1932             (HOLogic.mk_Trueprop (mk_mor car_finals str_finals UNIVs unfs Abs_Ts))
  1933             (mk_mor_Abs_tac (mor_def :: unf_defs) Abs_inverses)
  1934           |> Thm.close_derivation;
  1935       in
  1936         (mor_Rep, mor_Abs)
  1937       end;
  1938 
  1939     val timer = time (timer "unf definitions & thms");
  1940 
  1941     fun coiter_bind i = Binding.suffix_name ("_" ^ coN ^ iterN) (nth bs (i - 1));
  1942     val coiter_name = Binding.name_of o coiter_bind;
  1943     val coiter_def_bind = rpair [] o Thm.def_binding o coiter_bind;
  1944 
  1945     fun coiter_spec i T AT abs f z z' =
  1946       let
  1947         val coiterT = Library.foldr (op -->) (sTs, AT --> T);
  1948 
  1949         val lhs = Term.list_comb (Free (coiter_name i, coiterT), ss);
  1950         val rhs = Term.absfree z' (abs $ (f $ z));
  1951       in
  1952         mk_Trueprop_eq (lhs, rhs)
  1953       end;
  1954 
  1955     val ((coiter_frees, (_, coiter_def_frees)), (lthy, lthy_old)) =
  1956       lthy
  1957       |> fold_map7 (fn i => fn T => fn AT => fn abs => fn f => fn z => fn z' =>
  1958         Specification.definition
  1959           (SOME (coiter_bind i, NONE, NoSyn), (coiter_def_bind i, coiter_spec i T AT abs f z z')))
  1960           ks Ts activeAs Abs_Ts (map (fn i => HOLogic.mk_comp
  1961             (mk_proj (mk_LSBIS passive_UNIVs i), mk_beh ss i)) ks) zs zs'
  1962       |>> apsnd split_list o split_list
  1963       ||> `Local_Theory.restore;
  1964 
  1965     (*transforms defined frees into consts*)
  1966     val phi = Proof_Context.export_morphism lthy_old lthy;
  1967     val coiters = map (fst o dest_Const o Morphism.term phi) coiter_frees;
  1968     fun mk_coiter Ts ss i = Term.list_comb (Const (nth coiters (i - 1), Library.foldr (op -->)
  1969       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  1970     val coiter_defs = map ((fn thm => thm RS fun_cong) o Morphism.thm phi) coiter_def_frees;
  1971 
  1972     val mor_coiter_thm =
  1973       let
  1974         val Abs_inverses' = map2 (curry op RS) in_car_final_thms Abs_inverses;
  1975         val morEs' = map (fn thm =>
  1976           (thm OF [tcoalg_thm RS mor_final_thm, UNIV_I]) RS sym) morE_thms;
  1977       in
  1978         Skip_Proof.prove lthy [] []
  1979           (fold_rev Logic.all ss
  1980             (HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs unfs (map (mk_coiter Ts ss) ks))))
  1981           (K (mk_mor_coiter_tac m mor_UNIV_thm unf_defs coiter_defs Abs_inverses' morEs'
  1982             map_comp_id_thms map_congs))
  1983         |> Thm.close_derivation
  1984       end;
  1985     val coiter_thms = map (fn thm => (thm OF [mor_coiter_thm, UNIV_I]) RS sym) morE_thms;
  1986 
  1987     val (raw_coind_thms, raw_coind_thm) =
  1988       let
  1989         val prem = HOLogic.mk_Trueprop (mk_sbis passive_UNIVs UNIVs unfs TRs);
  1990         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1991           (map2 (fn R => fn T => mk_subset R (Id_const T)) TRs Ts));
  1992         val goal = fold_rev Logic.all TRs (Logic.mk_implies (prem, concl));
  1993       in
  1994         `split_conj_thm (Skip_Proof.prove lthy [] [] goal
  1995           (K (mk_raw_coind_tac bis_def bis_cong_thm bis_O_thm bis_converse_thm bis_Gr_thm
  1996             tcoalg_thm coalgT_thm mor_T_final_thm sbis_lsbis_thm
  1997             lsbis_incl_thms incl_lsbis_thms equiv_LSBIS_thms mor_Rep_thm Rep_injects))
  1998           |> Thm.close_derivation)
  1999       end;
  2000 
  2001     val unique_mor_thms =
  2002       let
  2003         val prems = [HOLogic.mk_Trueprop (mk_coalg passive_UNIVs Bs ss), HOLogic.mk_Trueprop
  2004           (HOLogic.mk_conj (mk_mor Bs ss UNIVs unfs coiter_fs,
  2005             mk_mor Bs ss UNIVs unfs coiter_fs_copy))];
  2006         fun mk_fun_eq B f g z = HOLogic.mk_imp
  2007           (HOLogic.mk_mem (z, B), HOLogic.mk_eq (f $ z, g $ z));
  2008         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2009           (map4 mk_fun_eq Bs coiter_fs coiter_fs_copy zs));
  2010 
  2011         val unique_mor = Skip_Proof.prove lthy [] []
  2012           (fold_rev Logic.all (Bs @ ss @ coiter_fs @ coiter_fs_copy @ zs)
  2013             (Logic.list_implies (prems, unique)))
  2014           (K (mk_unique_mor_tac raw_coind_thms bis_image2_thm))
  2015           |> Thm.close_derivation;
  2016       in
  2017         map (fn thm => conjI RSN (2, thm RS mp)) (split_conj_thm unique_mor)
  2018       end;
  2019 
  2020     val (coiter_unique_mor_thms, coiter_unique_mor_thm) =
  2021       let
  2022         val prem = HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs unfs coiter_fs);
  2023         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_coiter Ts ss i);
  2024         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2025           (map2 mk_fun_eq coiter_fs ks));
  2026 
  2027         val bis_thm = tcoalg_thm RSN (2, tcoalg_thm RS bis_image2_thm);
  2028         val mor_thm = mor_comp_thm OF [tcoalg_thm RS mor_final_thm, mor_Abs_thm];
  2029 
  2030         val unique_mor = Skip_Proof.prove lthy [] []
  2031           (fold_rev Logic.all (ss @ coiter_fs) (Logic.mk_implies (prem, unique)))
  2032           (K (mk_coiter_unique_mor_tac raw_coind_thms bis_thm mor_thm coiter_defs))
  2033           |> Thm.close_derivation;
  2034       in
  2035         `split_conj_thm unique_mor
  2036       end;
  2037 
  2038     val (coiter_unique_thms, coiter_unique_thm) = `split_conj_thm (split_conj_prems n
  2039       (mor_UNIV_thm RS @{thm ssubst[of _ _ "%x. x"]} RS coiter_unique_mor_thm));
  2040 
  2041     val coiter_unf_thms = map (fn thm => mor_id_thm RS thm RS sym) coiter_unique_mor_thms;
  2042 
  2043     val coiter_o_unf_thms =
  2044       let
  2045         val mor = mor_comp_thm OF [mor_str_thm, mor_coiter_thm];
  2046       in
  2047         map2 (fn unique => fn coiter_fld =>
  2048           trans OF [mor RS unique, coiter_fld]) coiter_unique_mor_thms coiter_unf_thms
  2049       end;
  2050 
  2051     val timer = time (timer "coiter definitions & thms");
  2052 
  2053     val map_unfs = map2 (fn Ds => fn bnf =>
  2054       Term.list_comb (mk_map_of_bnf Ds (passiveAs @ Ts) (passiveAs @ FTs) bnf,
  2055         map HOLogic.id_const passiveAs @ unfs)) Dss bnfs;
  2056 
  2057     fun fld_bind i = Binding.suffix_name ("_" ^ fldN) (nth bs (i - 1));
  2058     val fld_name = Binding.name_of o fld_bind;
  2059     val fld_def_bind = rpair [] o Thm.def_binding o fld_bind;
  2060 
  2061     fun fld_spec i fldT =
  2062       let
  2063         val lhs = Free (fld_name i, fldT);
  2064         val rhs = mk_coiter Ts map_unfs i;
  2065       in
  2066         mk_Trueprop_eq (lhs, rhs)
  2067       end;
  2068 
  2069     val ((fld_frees, (_, fld_def_frees)), (lthy, lthy_old)) =
  2070         lthy
  2071         |> fold_map2 (fn i => fn fldT =>
  2072           Specification.definition
  2073             (SOME (fld_bind i, NONE, NoSyn), (fld_def_bind i, fld_spec i fldT))) ks fldTs
  2074         |>> apsnd split_list o split_list
  2075         ||> `Local_Theory.restore;
  2076 
  2077     (*transforms defined frees into consts*)
  2078     val phi = Proof_Context.export_morphism lthy_old lthy;
  2079     fun mk_flds params =
  2080       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ params) o Morphism.term phi)
  2081         fld_frees;
  2082     val flds = mk_flds params';
  2083     val fld_defs = map (Morphism.thm phi) fld_def_frees;
  2084 
  2085     val fld_o_unf_thms = map2 (Local_Defs.fold lthy o single) fld_defs coiter_o_unf_thms;
  2086 
  2087     val unf_o_fld_thms =
  2088       let
  2089         fun mk_goal unf fld FT = mk_Trueprop_eq (HOLogic.mk_comp (unf, fld), HOLogic.id_const FT);
  2090         val goals = map3 mk_goal unfs flds FTs;
  2091       in
  2092         map5 (fn goal => fn fld_def => fn coiter => fn map_comp_id => fn map_congL =>
  2093           Skip_Proof.prove lthy [] [] goal
  2094             (mk_unf_o_fld_tac fld_def coiter map_comp_id map_congL coiter_o_unf_thms)
  2095           |> Thm.close_derivation)
  2096           goals fld_defs coiter_thms map_comp_id_thms map_congL_thms
  2097       end;
  2098 
  2099     val unf_fld_thms = map (fn thm => thm RS @{thm pointfree_idE}) unf_o_fld_thms;
  2100     val fld_unf_thms = map (fn thm => thm RS @{thm pointfree_idE}) fld_o_unf_thms;
  2101 
  2102     val bij_unf_thms =
  2103       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) fld_o_unf_thms unf_o_fld_thms;
  2104     val inj_unf_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_unf_thms;
  2105     val surj_unf_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_unf_thms;
  2106     val unf_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_unf_thms;
  2107     val unf_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_unf_thms;
  2108     val unf_exhaust_thms = map (fn thm => thm RS exE) unf_nchotomy_thms;
  2109 
  2110     val bij_fld_thms =
  2111       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) unf_o_fld_thms fld_o_unf_thms;
  2112     val inj_fld_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_fld_thms;
  2113     val surj_fld_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_fld_thms;
  2114     val fld_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_fld_thms;
  2115     val fld_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_fld_thms;
  2116     val fld_exhaust_thms = map (fn thm => thm RS exE) fld_nchotomy_thms;
  2117 
  2118     val fld_coiter_thms = map3 (fn unf_inject => fn coiter => fn unf_fld =>
  2119       iffD1 OF [unf_inject, trans  OF [coiter, unf_fld RS sym]])
  2120       unf_inject_thms coiter_thms unf_fld_thms;
  2121 
  2122     val timer = time (timer "fld definitions & thms");
  2123 
  2124     val corec_Inl_sum_thms =
  2125       let
  2126         val mor = mor_comp_thm OF [mor_sum_case_thm, mor_coiter_thm];
  2127       in
  2128         map2 (fn unique => fn coiter_unf =>
  2129           trans OF [mor RS unique, coiter_unf]) coiter_unique_mor_thms coiter_unf_thms
  2130       end;
  2131 
  2132     fun corec_bind i = Binding.suffix_name ("_" ^ coN ^ recN) (nth bs (i - 1));
  2133     val corec_name = Binding.name_of o corec_bind;
  2134     val corec_def_bind = rpair [] o Thm.def_binding o corec_bind;
  2135 
  2136     fun corec_spec i T AT =
  2137       let
  2138         val corecT = Library.foldr (op -->) (corec_sTs, AT --> T);
  2139         val maps = map3 (fn unf => fn sum_s => fn map => mk_sum_case
  2140             (HOLogic.mk_comp (Term.list_comb (map, passive_ids @ corec_Inls), unf)) sum_s)
  2141           unfs corec_ss corec_maps;
  2142 
  2143         val lhs = Term.list_comb (Free (corec_name i, corecT), corec_ss);
  2144         val rhs = HOLogic.mk_comp (mk_coiter Ts maps i, Inr_const T AT);
  2145       in
  2146         mk_Trueprop_eq (lhs, rhs)
  2147       end;
  2148 
  2149     val ((corec_frees, (_, corec_def_frees)), (lthy, lthy_old)) =
  2150         lthy
  2151         |> fold_map3 (fn i => fn T => fn AT =>
  2152           Specification.definition
  2153             (SOME (corec_bind i, NONE, NoSyn), (corec_def_bind i, corec_spec i T AT)))
  2154             ks Ts activeAs
  2155         |>> apsnd split_list o split_list
  2156         ||> `Local_Theory.restore;
  2157 
  2158     (*transforms defined frees into consts*)
  2159     val phi = Proof_Context.export_morphism lthy_old lthy;
  2160     val corecs = map (fst o dest_Const o Morphism.term phi) corec_frees;
  2161     fun mk_corec ss i = Term.list_comb (Const (nth corecs (i - 1), Library.foldr (op -->)
  2162       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  2163     val corec_defs = map (Morphism.thm phi) corec_def_frees;
  2164 
  2165     val sum_cases =
  2166       map2 (fn T => fn i => mk_sum_case (HOLogic.id_const T) (mk_corec corec_ss i)) Ts ks;
  2167     val corec_thms =
  2168       let
  2169         fun mk_goal i corec_s corec_map unf z =
  2170           let
  2171             val lhs = unf $ (mk_corec corec_ss i $ z);
  2172             val rhs = Term.list_comb (corec_map, passive_ids @ sum_cases) $ (corec_s $ z);
  2173           in
  2174             fold_rev Logic.all (z :: corec_ss) (mk_Trueprop_eq (lhs, rhs))
  2175           end;
  2176         val goals = map5 mk_goal ks corec_ss corec_maps_rev unfs zs;
  2177       in
  2178         map3 (fn goal => fn coiter => fn map_cong =>
  2179           Skip_Proof.prove lthy [] [] goal
  2180             (mk_corec_tac m corec_defs coiter map_cong corec_Inl_sum_thms)
  2181           |> Thm.close_derivation)
  2182         goals coiter_thms map_congs
  2183       end;
  2184 
  2185     val timer = time (timer "corec definitions & thms");
  2186 
  2187     val (unf_coinduct_thm, coinduct_params, rel_coinduct_thm, pred_coinduct_thm,
  2188          unf_coinduct_upto_thm, rel_coinduct_upto_thm, pred_coinduct_upto_thm) =
  2189       let
  2190         val zs = Jzs1 @ Jzs2;
  2191         val frees = phis @ zs;
  2192 
  2193         fun mk_Ids Id = if Id then map Id_const passiveAs else map mk_diag passive_UNIVs;
  2194 
  2195         fun mk_phi upto_eq phi z1 z2 = if upto_eq
  2196           then Term.absfree (dest_Free z1) (Term.absfree (dest_Free z2)
  2197             (HOLogic.mk_disj (phi $ z1 $ z2, HOLogic.mk_eq (z1, z2))))
  2198           else phi;
  2199 
  2200         fun phi_rels upto_eq = map4 (fn phi => fn T => fn z1 => fn z2 =>
  2201           HOLogic.Collect_const (HOLogic.mk_prodT (T, T)) $
  2202             HOLogic.mk_split (mk_phi upto_eq phi z1 z2)) phis Ts Jzs1 Jzs2;
  2203 
  2204         val rels = map (Term.subst_atomic_types ((activeAs ~~ Ts) @ (activeBs ~~ Ts))) relsAsBs;
  2205 
  2206         fun mk_concl phi z1 z2 = HOLogic.mk_imp (phi $ z1 $ z2, HOLogic.mk_eq (z1, z2));
  2207         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2208           (map3 mk_concl phis Jzs1 Jzs2));
  2209 
  2210         fun mk_rel_prem upto_eq phi unf rel Jz Jz_copy =
  2211           let
  2212             val concl = HOLogic.mk_mem (HOLogic.mk_tuple [unf $ Jz, unf $ Jz_copy],
  2213               Term.list_comb (rel, mk_Ids upto_eq @ phi_rels upto_eq));
  2214           in
  2215             HOLogic.mk_Trueprop
  2216               (list_all_free [Jz, Jz_copy] (HOLogic.mk_imp (phi $ Jz $ Jz_copy, concl)))
  2217           end;
  2218 
  2219         val rel_prems = map5 (mk_rel_prem false) phis unfs rels Jzs Jzs_copy;
  2220         val rel_upto_prems = map5 (mk_rel_prem true) phis unfs rels Jzs Jzs_copy;
  2221 
  2222         val rel_coinduct_goal = fold_rev Logic.all frees (Logic.list_implies (rel_prems, concl));
  2223         val coinduct_params = rev (Term.add_tfrees rel_coinduct_goal []);
  2224 
  2225         val rel_coinduct = Local_Defs.unfold lthy @{thms diag_UNIV}
  2226           (Skip_Proof.prove lthy [] [] rel_coinduct_goal
  2227             (K (mk_rel_coinduct_tac ks raw_coind_thm bis_rel_thm))
  2228           |> Thm.close_derivation);
  2229 
  2230         fun mk_unf_prem upto_eq phi unf map_nth sets Jz Jz_copy FJz =
  2231           let
  2232             val xs = [Jz, Jz_copy];
  2233 
  2234             fun mk_map_conjunct nths x =
  2235               HOLogic.mk_eq (Term.list_comb (map_nth, passive_ids @ nths) $ FJz, unf $ x);
  2236 
  2237             fun mk_set_conjunct set phi z1 z2 =
  2238               list_all_free [z1, z2]
  2239                 (HOLogic.mk_imp (HOLogic.mk_mem (HOLogic.mk_prod (z1, z2), set $ FJz),
  2240                   mk_phi upto_eq phi z1 z2 $ z1 $ z2));
  2241 
  2242             val concl = list_exists_free [FJz] (HOLogic.mk_conj
  2243               (Library.foldr1 HOLogic.mk_conj (map2 mk_map_conjunct [fstsTs, sndsTs] xs),
  2244               Library.foldr1 HOLogic.mk_conj
  2245                 (map4 mk_set_conjunct (drop m sets) phis Jzs1 Jzs2)));
  2246           in
  2247             fold_rev Logic.all xs (Logic.mk_implies
  2248               (HOLogic.mk_Trueprop (Term.list_comb (phi, xs)), HOLogic.mk_Trueprop concl))
  2249           end;
  2250 
  2251         fun mk_unf_prems upto_eq =
  2252           map7 (mk_unf_prem upto_eq) phis unfs map_FT_nths prodFT_setss Jzs Jzs_copy FJzs
  2253 
  2254         val unf_prems = mk_unf_prems false;
  2255         val unf_upto_prems = mk_unf_prems true;
  2256 
  2257         val unf_coinduct_goal = fold_rev Logic.all frees (Logic.list_implies (unf_prems, concl));
  2258         val unf_coinduct = Skip_Proof.prove lthy [] [] unf_coinduct_goal
  2259           (K (mk_unf_coinduct_tac m ks raw_coind_thm bis_def))
  2260           |> Thm.close_derivation;
  2261 
  2262         val cTs = map (SOME o certifyT lthy o TFree) coinduct_params;
  2263         val cts = map3 (SOME o certify lthy ooo mk_phi true) phis Jzs1 Jzs2;
  2264 
  2265         val rel_coinduct_upto = singleton (Proof_Context.export names_lthy lthy)
  2266           (Skip_Proof.prove lthy [] []
  2267             (fold_rev Logic.all zs (Logic.list_implies (rel_upto_prems, concl)))
  2268             (K (mk_rel_coinduct_upto_tac m cTs cts rel_coinduct rel_monos rel_Ids)))
  2269           |> Thm.close_derivation;
  2270 
  2271         val unf_coinduct_upto = singleton (Proof_Context.export names_lthy lthy)
  2272           (Skip_Proof.prove lthy [] []
  2273             (fold_rev Logic.all zs (Logic.list_implies (unf_upto_prems, concl)))
  2274             (K (mk_unf_coinduct_upto_tac ks cTs cts unf_coinduct bis_def
  2275               (tcoalg_thm RS bis_diag_thm))))
  2276           |> Thm.close_derivation;
  2277 
  2278         val pred_coinduct = rel_coinduct
  2279           |> Local_Defs.unfold lthy @{thms Id_def'}
  2280           |> Local_Defs.fold lthy pred_defs;
  2281         val pred_coinduct_upto = rel_coinduct_upto
  2282           |> Local_Defs.unfold lthy @{thms Id_def'}
  2283           |> Local_Defs.fold lthy pred_defs;
  2284       in
  2285         (unf_coinduct, rev (Term.add_tfrees unf_coinduct_goal []), rel_coinduct, pred_coinduct,
  2286          unf_coinduct_upto, rel_coinduct_upto, pred_coinduct_upto)
  2287       end;
  2288 
  2289     val timer = time (timer "coinduction");
  2290 
  2291     (*register new codatatypes as BNFs*)
  2292     val lthy = if m = 0 then lthy else
  2293       let
  2294         val fTs = map2 (curry op -->) passiveAs passiveBs;
  2295         val gTs = map2 (curry op -->) passiveBs passiveCs;
  2296         val f1Ts = map2 (curry op -->) passiveAs passiveYs;
  2297         val f2Ts = map2 (curry op -->) passiveBs passiveYs;
  2298         val p1Ts = map2 (curry op -->) passiveXs passiveAs;
  2299         val p2Ts = map2 (curry op -->) passiveXs passiveBs;
  2300         val pTs = map2 (curry op -->) passiveXs passiveCs;
  2301         val uTs = map2 (curry op -->) Ts Ts';
  2302         val JRTs = map2 (curry mk_relT) passiveAs passiveBs;
  2303         val JphiTs = map2 (fn T => fn U => T --> U --> HOLogic.boolT) passiveAs passiveBs;
  2304         val prodTs = map2 (curry HOLogic.mk_prodT) Ts Ts';
  2305         val B1Ts = map HOLogic.mk_setT passiveAs;
  2306         val B2Ts = map HOLogic.mk_setT passiveBs;
  2307         val AXTs = map HOLogic.mk_setT passiveXs;
  2308         val XTs = mk_Ts passiveXs;
  2309         val YTs = mk_Ts passiveYs;
  2310 
  2311         val (((((((((((((((((((((fs, fs'), (fs_copy, fs'_copy)), (gs, gs')), us),
  2312           (Jys, Jys')), (Jys_copy, Jys'_copy)), set_induct_phiss), JRs), Jphis),
  2313           B1s), B2s), AXs), Xs), f1s), f2s), p1s), p2s), ps), (ys, ys')), (ys_copy, ys'_copy)),
  2314           names_lthy) = names_lthy
  2315           |> mk_Frees' "f" fTs
  2316           ||>> mk_Frees' "f" fTs
  2317           ||>> mk_Frees' "g" gTs
  2318           ||>> mk_Frees "u" uTs
  2319           ||>> mk_Frees' "b" Ts'
  2320           ||>> mk_Frees' "b" Ts'
  2321           ||>> mk_Freess "phi" (map (fn T => map (fn U => T --> U --> HOLogic.boolT) Ts) passiveAs)
  2322           ||>> mk_Frees "R" JRTs
  2323           ||>> mk_Frees "phi" JphiTs
  2324           ||>> mk_Frees "B1" B1Ts
  2325           ||>> mk_Frees "B2" B2Ts
  2326           ||>> mk_Frees "A" AXTs
  2327           ||>> mk_Frees "x" XTs
  2328           ||>> mk_Frees "f1" f1Ts
  2329           ||>> mk_Frees "f2" f2Ts
  2330           ||>> mk_Frees "p1" p1Ts
  2331           ||>> mk_Frees "p2" p2Ts
  2332           ||>> mk_Frees "p" pTs
  2333           ||>> mk_Frees' "y" passiveAs
  2334           ||>> mk_Frees' "y" passiveAs;
  2335 
  2336         val map_FTFT's = map2 (fn Ds =>
  2337           mk_map_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2338 
  2339         fun mk_maps ATs BTs Ts mk_T =
  2340           map2 (fn Ds => mk_map_of_bnf Ds (ATs @ Ts) (BTs @ map mk_T Ts)) Dss bnfs;
  2341         fun mk_Fmap mk_const fs Ts Fmap = Term.list_comb (Fmap, fs @ map mk_const Ts);
  2342         fun mk_map mk_const mk_T Ts fs Ts' unfs mk_maps =
  2343           mk_coiter Ts' (map2 (fn unf => fn Fmap =>
  2344             HOLogic.mk_comp (mk_Fmap mk_const fs Ts Fmap, unf)) unfs (mk_maps Ts mk_T));
  2345         val mk_map_id = mk_map HOLogic.id_const I;
  2346         val mk_mapsAB = mk_maps passiveAs passiveBs;
  2347         val mk_mapsBC = mk_maps passiveBs passiveCs;
  2348         val mk_mapsAC = mk_maps passiveAs passiveCs;
  2349         val mk_mapsAY = mk_maps passiveAs passiveYs;
  2350         val mk_mapsBY = mk_maps passiveBs passiveYs;
  2351         val mk_mapsXA = mk_maps passiveXs passiveAs;
  2352         val mk_mapsXB = mk_maps passiveXs passiveBs;
  2353         val mk_mapsXC = mk_maps passiveXs passiveCs;
  2354         val fs_maps = map (mk_map_id Ts fs Ts' unfs mk_mapsAB) ks;
  2355         val fs_copy_maps = map (mk_map_id Ts fs_copy Ts' unfs mk_mapsAB) ks;
  2356         val gs_maps = map (mk_map_id Ts' gs Ts'' unf's mk_mapsBC) ks;
  2357         val fgs_maps =
  2358           map (mk_map_id Ts (map2 (curry HOLogic.mk_comp) gs fs) Ts'' unfs mk_mapsAC) ks;
  2359         val Xunfs = mk_unfs passiveXs;
  2360         val UNIV's = map HOLogic.mk_UNIV Ts';
  2361         val CUNIVs = map HOLogic.mk_UNIV passiveCs;
  2362         val UNIV''s = map HOLogic.mk_UNIV Ts'';
  2363         val fstsTsTs' = map fst_const prodTs;
  2364         val sndsTsTs' = map snd_const prodTs;
  2365         val unf''s = mk_unfs passiveCs;
  2366         val f1s_maps = map (mk_map_id Ts f1s YTs unfs mk_mapsAY) ks;
  2367         val f2s_maps = map (mk_map_id Ts' f2s YTs unf's mk_mapsBY) ks;
  2368         val pid_maps = map (mk_map_id XTs ps Ts'' Xunfs mk_mapsXC) ks;
  2369         val pfst_Fmaps =
  2370           map (mk_Fmap fst_const p1s prodTs) (mk_mapsXA prodTs (fst o HOLogic.dest_prodT));
  2371         val psnd_Fmaps =
  2372           map (mk_Fmap snd_const p2s prodTs) (mk_mapsXB prodTs (snd o HOLogic.dest_prodT));
  2373         val p1id_Fmaps = map (mk_Fmap HOLogic.id_const p1s prodTs) (mk_mapsXA prodTs I);
  2374         val p2id_Fmaps = map (mk_Fmap HOLogic.id_const p2s prodTs) (mk_mapsXB prodTs I);
  2375         val pid_Fmaps = map (mk_Fmap HOLogic.id_const ps prodTs) (mk_mapsXC prodTs I);
  2376 
  2377         val (map_simp_thms, map_thms) =
  2378           let
  2379             fun mk_goal fs_map map unf unf' = fold_rev Logic.all fs
  2380               (mk_Trueprop_eq (HOLogic.mk_comp (unf', fs_map),
  2381                 HOLogic.mk_comp (Term.list_comb (map, fs @ fs_maps), unf)));
  2382             val goals = map4 mk_goal fs_maps map_FTFT's unfs unf's;
  2383             val cTs = map (SOME o certifyT lthy) FTs';
  2384             val maps =
  2385               map5 (fn goal => fn cT => fn coiter => fn map_comp' => fn map_cong =>
  2386                 Skip_Proof.prove lthy [] [] goal
  2387                   (K (mk_map_tac m n cT coiter map_comp' map_cong))
  2388                 |> Thm.close_derivation)
  2389               goals cTs coiter_thms map_comp's map_congs;
  2390           in
  2391             map_split (fn thm => (thm RS @{thm pointfreeE}, thm)) maps
  2392           end;
  2393 
  2394         val map_comp_thms =
  2395           let
  2396             val goal = fold_rev Logic.all (fs @ gs)
  2397               (HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2398                 (map3 (fn fmap => fn gmap => fn fgmap =>
  2399                    HOLogic.mk_eq (HOLogic.mk_comp (gmap, fmap), fgmap))
  2400                 fs_maps gs_maps fgs_maps)))
  2401           in
  2402             split_conj_thm (Skip_Proof.prove lthy [] [] goal
  2403               (K (mk_map_comp_tac m n map_thms map_comps map_congs coiter_unique_thm))
  2404               |> Thm.close_derivation)
  2405           end;
  2406 
  2407         val (map_unique_thms, map_unique_thm) =
  2408           let
  2409             fun mk_prem u map unf unf' =
  2410               mk_Trueprop_eq (HOLogic.mk_comp (unf', u),
  2411                 HOLogic.mk_comp (Term.list_comb (map, fs @ us), unf));
  2412             val prems = map4 mk_prem us map_FTFT's unfs unf's;
  2413             val goal =
  2414               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2415                 (map2 (curry HOLogic.mk_eq) us fs_maps));
  2416             val unique = Skip_Proof.prove lthy [] []
  2417               (fold_rev Logic.all (us @ fs) (Logic.list_implies (prems, goal)))
  2418               (mk_map_unique_tac coiter_unique_thm map_comps)
  2419               |> Thm.close_derivation;
  2420           in
  2421             `split_conj_thm unique
  2422           end;
  2423 
  2424         val timer = time (timer "map functions for the new codatatypes");
  2425 
  2426         val bd = mk_ccexp sbd sbd;
  2427 
  2428         val timer = time (timer "bounds for the new codatatypes");
  2429 
  2430         fun mk_set_Ts T = passiveAs @ replicate n (HOLogic.mk_setT T);
  2431         val setsss = map (mk_setss o mk_set_Ts) passiveAs;
  2432         val map_setss = map (fn T => map2 (fn Ds =>
  2433           mk_map_of_bnf Ds (passiveAs @ Ts) (mk_set_Ts T)) Dss bnfs) passiveAs;
  2434 
  2435         val setss_by_bnf = map (fn i => map2 (mk_hset unfs i) ls passiveAs) ks;
  2436         val setss_by_bnf' = map (fn i => map2 (mk_hset unf's i) ls passiveBs) ks;
  2437         val setss_by_range = transpose setss_by_bnf;
  2438 
  2439         val set_simp_thmss =
  2440           let
  2441             fun mk_simp_goal relate pas_set act_sets sets unf z set =
  2442               relate (set $ z, mk_union (pas_set $ (unf $ z),
  2443                  Library.foldl1 mk_union
  2444                    (map2 (fn X => mk_UNION (X $ (unf $ z))) act_sets sets)));
  2445             fun mk_goals eq =
  2446               map2 (fn i => fn sets =>
  2447                 map4 (fn Fsets =>
  2448                   mk_simp_goal eq (nth Fsets (i - 1)) (drop m Fsets) sets)
  2449                 FTs_setss unfs Jzs sets)
  2450               ls setss_by_range;
  2451 
  2452             val le_goals = map
  2453               (fold_rev Logic.all Jzs o HOLogic.mk_Trueprop o Library.foldr1 HOLogic.mk_conj)
  2454               (mk_goals (uncurry mk_subset));
  2455             val set_le_thmss = map split_conj_thm
  2456               (map4 (fn goal => fn hset_minimal => fn set_hsets => fn set_hset_hsetss =>
  2457                 Skip_Proof.prove lthy [] [] goal
  2458                   (K (mk_set_le_tac n hset_minimal set_hsets set_hset_hsetss))
  2459                 |> Thm.close_derivation)
  2460               le_goals hset_minimal_thms set_hset_thmss' set_hset_hset_thmsss');
  2461 
  2462             val simp_goalss = map (map2 (fn z => fn goal =>
  2463                 Logic.all z (HOLogic.mk_Trueprop goal)) Jzs)
  2464               (mk_goals HOLogic.mk_eq);
  2465           in
  2466             map4 (map4 (fn goal => fn set_le => fn set_incl_hset => fn set_hset_incl_hsets =>
  2467               Skip_Proof.prove lthy [] [] goal
  2468                 (K (mk_set_simp_tac n set_le set_incl_hset set_hset_incl_hsets))
  2469               |> Thm.close_derivation))
  2470             simp_goalss set_le_thmss set_incl_hset_thmss' set_hset_incl_hset_thmsss'
  2471           end;
  2472 
  2473         val timer = time (timer "set functions for the new codatatypes");
  2474 
  2475         val colss = map2 (fn j => fn T =>
  2476           map (fn i => mk_hset_rec unfs nat i j T) ks) ls passiveAs;
  2477         val colss' = map2 (fn j => fn T =>
  2478           map (fn i => mk_hset_rec unf's nat i j T) ks) ls passiveBs;
  2479         val Xcolss = map2 (fn j => fn T =>
  2480           map (fn i => mk_hset_rec Xunfs nat i j T) ks) ls passiveXs;
  2481 
  2482         val col_natural_thmss =
  2483           let
  2484             fun mk_col_natural f map z col col' =
  2485               HOLogic.mk_eq (mk_image f $ (col $ z), col' $ (map $ z));
  2486 
  2487             fun mk_goal f cols cols' = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2488               (map4 (mk_col_natural f) fs_maps Jzs cols cols'));
  2489 
  2490             val goals = map3 mk_goal fs colss colss';
  2491 
  2492             val ctss =
  2493               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) goals;
  2494 
  2495             val thms =
  2496               map4 (fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2497                 singleton (Proof_Context.export names_lthy lthy)
  2498                   (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  2499                     (mk_col_natural_tac cts rec_0s rec_Sucs map_simp_thms set_natural'ss))
  2500                 |> Thm.close_derivation)
  2501               goals ctss hset_rec_0ss' hset_rec_Sucss';
  2502           in
  2503             map (split_conj_thm o mk_specN n) thms
  2504           end;
  2505 
  2506         val col_bd_thmss =
  2507           let
  2508             fun mk_col_bd z col = mk_ordLeq (mk_card_of (col $ z)) sbd;
  2509 
  2510             fun mk_goal cols = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2511               (map2 mk_col_bd Jzs cols));
  2512 
  2513             val goals = map mk_goal colss;
  2514 
  2515             val ctss =
  2516               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) goals;
  2517 
  2518             val thms =
  2519               map5 (fn j => fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2520                 singleton (Proof_Context.export names_lthy lthy)
  2521                   (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  2522                     (K (mk_col_bd_tac m j cts rec_0s rec_Sucs
  2523                       sbd_Card_order sbd_Cinfinite set_sbdss)))
  2524                 |> Thm.close_derivation)
  2525               ls goals ctss hset_rec_0ss' hset_rec_Sucss';
  2526           in
  2527             map (split_conj_thm o mk_specN n) thms
  2528           end;
  2529 
  2530         val map_cong_thms =
  2531           let
  2532             val cTs = map (SOME o certifyT lthy o
  2533               Term.typ_subst_atomic (passiveAs ~~ passiveBs) o TFree) coinduct_params;
  2534 
  2535             fun mk_prem z set f g y y' =
  2536               mk_Ball (set $ z) (Term.absfree y' (HOLogic.mk_eq (f $ y, g $ y)));
  2537 
  2538             fun mk_prems sets z =
  2539               Library.foldr1 HOLogic.mk_conj (map5 (mk_prem z) sets fs fs_copy ys ys')
  2540 
  2541             fun mk_map_cong sets z fmap gmap =
  2542               HOLogic.mk_imp (mk_prems sets z, HOLogic.mk_eq (fmap $ z, gmap $ z));
  2543 
  2544             fun mk_coind_body sets (x, T) z fmap gmap y y_copy =
  2545               HOLogic.mk_conj
  2546                 (HOLogic.mk_mem (z, HOLogic.mk_Collect (x, T, mk_prems sets z)),
  2547                   HOLogic.mk_conj (HOLogic.mk_eq (y, fmap $ z),
  2548                     HOLogic.mk_eq (y_copy, gmap $ z)))
  2549 
  2550             fun mk_cphi sets (z' as (x, T)) z fmap gmap y' y y'_copy y_copy =
  2551               HOLogic.mk_exists (x, T, mk_coind_body sets z' z fmap gmap y y_copy)
  2552               |> Term.absfree y'_copy
  2553               |> Term.absfree y'
  2554               |> certify lthy;
  2555 
  2556             val cphis =
  2557               map9 mk_cphi setss_by_bnf Jzs' Jzs fs_maps fs_copy_maps Jys' Jys Jys'_copy Jys_copy;
  2558 
  2559             val coinduct = Drule.instantiate' cTs (map SOME cphis) unf_coinduct_thm;
  2560 
  2561             val goal =
  2562               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2563                 (map4 mk_map_cong setss_by_bnf Jzs fs_maps fs_copy_maps));
  2564 
  2565             val thm = singleton (Proof_Context.export names_lthy lthy)
  2566               (Skip_Proof.prove lthy [] [] goal
  2567               (K (mk_mcong_tac m (rtac coinduct) map_comp's map_simp_thms map_congs set_natural'ss
  2568               set_hset_thmss set_hset_hset_thmsss)))
  2569               |> Thm.close_derivation
  2570           in
  2571             split_conj_thm thm
  2572           end;
  2573 
  2574         val B1_ins = map2 (mk_in B1s) setss_by_bnf Ts;
  2575         val B2_ins = map2 (mk_in B2s) setss_by_bnf' Ts';
  2576         val thePulls = map4 mk_thePull B1_ins B2_ins f1s_maps f2s_maps;
  2577         val thePullTs = passiveXs @ map2 (curry HOLogic.mk_prodT) Ts Ts';
  2578         val thePull_ins = map2 (mk_in (AXs @ thePulls)) (mk_setss thePullTs) (mk_FTs thePullTs);
  2579         val pickFs = map5 mk_pickWP thePull_ins pfst_Fmaps psnd_Fmaps
  2580           (map2 (curry (op $)) unfs Jzs) (map2 (curry (op $)) unf's Jz's);
  2581         val pickF_ss = map3 (fn pickF => fn z => fn z' =>
  2582           HOLogic.mk_split (Term.absfree z (Term.absfree z' pickF))) pickFs Jzs' Jz's';
  2583         val picks = map (mk_coiter XTs pickF_ss) ks;
  2584 
  2585         val wpull_prem = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2586           (map8 mk_wpull AXs B1s B2s f1s f2s (replicate m NONE) p1s p2s));
  2587 
  2588         val map_eq_thms = map2 (fn simp => fn diff => box_equals OF [diff RS iffD2, simp, simp])
  2589           map_simp_thms unf_inject_thms;
  2590         val map_wpull_thms = map (fn thm => thm OF
  2591           (replicate m asm_rl @ replicate n @{thm wpull_thePull})) map_wpulls;
  2592         val pickWP_assms_tacs =
  2593           map3 mk_pickWP_assms_tac set_incl_hset_thmss set_incl_hin_thmss map_eq_thms;
  2594 
  2595         val coalg_thePull_thm =
  2596           let
  2597             val coalg = HOLogic.mk_Trueprop
  2598               (mk_coalg CUNIVs thePulls (map2 (curry HOLogic.mk_comp) pid_Fmaps pickF_ss));
  2599             val goal = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s @ ps)
  2600               (Logic.mk_implies (wpull_prem, coalg));
  2601           in
  2602             Skip_Proof.prove lthy [] [] goal (mk_coalg_thePull_tac m coalg_def map_wpull_thms
  2603               set_natural'ss pickWP_assms_tacs)
  2604             |> Thm.close_derivation
  2605           end;
  2606 
  2607         val (mor_thePull_fst_thm, mor_thePull_snd_thm, mor_thePull_pick_thm) =
  2608           let
  2609             val mor_fst = HOLogic.mk_Trueprop
  2610               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) p1id_Fmaps pickF_ss)
  2611                 UNIVs unfs fstsTsTs');
  2612             val mor_snd = HOLogic.mk_Trueprop
  2613               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) p2id_Fmaps pickF_ss)
  2614                 UNIV's unf's sndsTsTs');
  2615             val mor_pick = HOLogic.mk_Trueprop
  2616               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) pid_Fmaps pickF_ss)
  2617                 UNIV''s unf''s (map2 (curry HOLogic.mk_comp) pid_maps picks));
  2618 
  2619             val goal_fst = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
  2620               (Logic.mk_implies (wpull_prem, mor_fst));
  2621             val goal_snd = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
  2622               (Logic.mk_implies (wpull_prem, mor_snd));
  2623             val goal_pick = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s @ ps)
  2624               (Logic.mk_implies (wpull_prem, mor_pick));
  2625           in
  2626             (Skip_Proof.prove lthy [] [] goal_fst (mk_mor_thePull_fst_tac m mor_def map_wpull_thms
  2627               map_comp's pickWP_assms_tacs) |> Thm.close_derivation,
  2628             Skip_Proof.prove lthy [] [] goal_snd (mk_mor_thePull_snd_tac m mor_def map_wpull_thms
  2629               map_comp's pickWP_assms_tacs) |> Thm.close_derivation,
  2630             Skip_Proof.prove lthy [] [] goal_pick (mk_mor_thePull_pick_tac mor_def coiter_thms
  2631               map_comp's) |> Thm.close_derivation)
  2632           end;
  2633 
  2634         val pick_col_thmss =
  2635           let
  2636             fun mk_conjunct AX Jpair pick thePull col =
  2637               HOLogic.mk_imp (HOLogic.mk_mem (Jpair, thePull), mk_subset (col $ (pick $ Jpair)) AX);
  2638 
  2639             fun mk_concl AX cols =
  2640               list_all_free Jpairs (Library.foldr1 HOLogic.mk_conj
  2641                 (map4 (mk_conjunct AX) Jpairs picks thePulls cols));
  2642 
  2643             val concls = map2 mk_concl AXs Xcolss;
  2644 
  2645             val ctss =
  2646               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
  2647 
  2648             val goals =
  2649               map (fn concl => Logic.mk_implies (wpull_prem, HOLogic.mk_Trueprop concl)) concls;
  2650 
  2651             val thms =
  2652               map5 (fn j => fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2653                 singleton (Proof_Context.export names_lthy lthy) (Skip_Proof.prove lthy [] [] goal
  2654                   (mk_pick_col_tac m j cts rec_0s rec_Sucs coiter_thms set_natural'ss map_wpull_thms
  2655                     pickWP_assms_tacs))
  2656                 |> Thm.close_derivation)
  2657               ls goals ctss hset_rec_0ss' hset_rec_Sucss';
  2658           in
  2659             map (map (fn thm => thm RS mp) o split_conj_thm o mk_specN n) thms
  2660           end;
  2661 
  2662         val timer = time (timer "helpers for BNF properties");
  2663 
  2664         val map_id_tacs = map2 (K oo mk_map_id_tac map_thms) coiter_unique_thms coiter_unf_thms;
  2665         val map_comp_tacs = map (fn thm => K (rtac (thm RS sym) 1)) map_comp_thms;
  2666         val map_cong_tacs = map (mk_map_cong_tac m) map_cong_thms;
  2667         val set_nat_tacss =
  2668           map2 (map2 (K oo mk_set_natural_tac)) hset_defss (transpose col_natural_thmss);
  2669 
  2670         val bd_co_tacs = replicate n (K (mk_bd_card_order_tac sbd_card_order));
  2671         val bd_cinf_tacs = replicate n (K (mk_bd_cinfinite_tac sbd_Cinfinite));
  2672 
  2673         val set_bd_tacss =
  2674           map2 (map2 (K oo mk_set_bd_tac sbd_Cinfinite)) hset_defss (transpose col_bd_thmss);
  2675 
  2676         val in_bd_tacs = map7 (fn i => fn isNode_hsets => fn carT_def =>
  2677             fn card_of_carT => fn mor_image => fn Rep_inverse => fn mor_hsets =>
  2678           K (mk_in_bd_tac (nth isNode_hsets (i - 1)) isNode_hsets carT_def
  2679             card_of_carT mor_image Rep_inverse mor_hsets
  2680             sbd_Cnotzero sbd_Card_order mor_Rep_thm coalgT_thm mor_T_final_thm tcoalg_thm))
  2681           ks isNode_hset_thmss carT_defs card_of_carT_thms
  2682           mor_image'_thms Rep_inverses (transpose mor_hset_thmss);
  2683 
  2684         val map_wpull_tacs =
  2685           map3 (K ooo mk_wpull_tac m coalg_thePull_thm mor_thePull_fst_thm mor_thePull_snd_thm
  2686             mor_thePull_pick_thm) unique_mor_thms (transpose pick_col_thmss) hset_defss;
  2687 
  2688         val tacss = map9 mk_tactics map_id_tacs map_comp_tacs map_cong_tacs set_nat_tacss bd_co_tacs
  2689           bd_cinf_tacs set_bd_tacss in_bd_tacs map_wpull_tacs;
  2690 
  2691         val (hset_unf_incl_thmss, hset_hset_unf_incl_thmsss, hset_induct_thms) =
  2692           let
  2693             fun tinst_of unf =
  2694               map (SOME o certify lthy) (unf :: remove (op =) unf unfs);
  2695             fun tinst_of' unf = case tinst_of unf of t :: ts => t :: NONE :: ts;
  2696             val Tinst = map (pairself (certifyT lthy))
  2697               (map Logic.varifyT_global (deads @ allAs) ~~ (deads @ passiveAs @ Ts));
  2698             val set_incl_thmss =
  2699               map2 (fn unf => map (singleton (Proof_Context.export names_lthy lthy) o
  2700                 Drule.instantiate' [] (tinst_of' unf) o
  2701                 Thm.instantiate (Tinst, []) o Drule.zero_var_indexes))
  2702               unfs set_incl_hset_thmss;
  2703 
  2704             val tinst = interleave (map (SOME o certify lthy) unfs) (replicate n NONE)
  2705             val set_minimal_thms =
  2706               map (Drule.instantiate' [] tinst o Thm.instantiate (Tinst, []) o
  2707                 Drule.zero_var_indexes)
  2708               hset_minimal_thms;
  2709 
  2710             val set_set_incl_thmsss =
  2711               map2 (fn unf => map (map (singleton (Proof_Context.export names_lthy lthy) o
  2712                 Drule.instantiate' [] (NONE :: tinst_of' unf) o
  2713                 Thm.instantiate (Tinst, []) o Drule.zero_var_indexes)))
  2714               unfs set_hset_incl_hset_thmsss;
  2715 
  2716             val set_set_incl_thmsss' = transpose (map transpose set_set_incl_thmsss);
  2717 
  2718             val incls =
  2719               maps (map (fn thm => thm RS @{thm subset_Collect_iff})) set_incl_thmss @
  2720                 @{thms subset_Collect_iff[OF subset_refl]};
  2721 
  2722             fun mk_induct_tinst phis jsets y y' =
  2723               map4 (fn phi => fn jset => fn Jz => fn Jz' =>
  2724                 SOME (certify lthy (Term.absfree Jz' (HOLogic.mk_Collect (fst y', snd y',
  2725                   HOLogic.mk_conj (HOLogic.mk_mem (y, jset $ Jz), phi $ y $ Jz))))))
  2726               phis jsets Jzs Jzs';
  2727             val set_induct_thms =
  2728               map6 (fn set_minimal => fn set_set_inclss => fn jsets => fn y => fn y' => fn phis =>
  2729                 ((set_minimal
  2730                   |> Drule.instantiate' [] (mk_induct_tinst phis jsets y y')
  2731                   |> Local_Defs.unfold lthy incls) OF
  2732                   (replicate n ballI @
  2733                     maps (map (fn thm => thm RS @{thm subset_CollectI})) set_set_inclss))
  2734                 |> singleton (Proof_Context.export names_lthy lthy)
  2735                 |> rule_by_tactic lthy (ALLGOALS (TRY o etac asm_rl)))
  2736               set_minimal_thms set_set_incl_thmsss' setss_by_range ys ys' set_induct_phiss
  2737           in
  2738             (set_incl_thmss, set_set_incl_thmsss, set_induct_thms)
  2739           end;
  2740 
  2741         fun close_wit I wit = (I, fold_rev Term.absfree (map (nth ys') I) wit);
  2742 
  2743         val all_unitTs = replicate live HOLogic.unitT;
  2744         val unitTs = replicate n HOLogic.unitT;
  2745         val unit_funs = replicate n (Term.absdummy HOLogic.unitT HOLogic.unit);
  2746         fun mk_map_args I =
  2747           map (fn i =>
  2748             if member (op =) I i then Term.absdummy HOLogic.unitT (nth ys i)
  2749             else mk_undefined (HOLogic.unitT --> nth passiveAs i))
  2750           (0 upto (m - 1));
  2751 
  2752         fun mk_nat_wit Ds bnf (I, wit) () =
  2753           let
  2754             val passiveI = filter (fn i => i < m) I;
  2755             val map_args = mk_map_args passiveI;
  2756           in
  2757             Term.absdummy HOLogic.unitT (Term.list_comb
  2758               (mk_map_of_bnf Ds all_unitTs (passiveAs @ unitTs) bnf, map_args @ unit_funs) $ wit)
  2759           end;
  2760 
  2761         fun mk_dummy_wit Ds bnf I =
  2762           let
  2763             val map_args = mk_map_args I;
  2764           in
  2765             Term.absdummy HOLogic.unitT (Term.list_comb
  2766               (mk_map_of_bnf Ds all_unitTs (passiveAs @ unitTs) bnf, map_args @ unit_funs) $
  2767               mk_undefined (mk_T_of_bnf Ds all_unitTs bnf))
  2768           end;
  2769 
  2770         val nat_witss =
  2771           map3 (fn i => fn Ds => fn bnf => mk_wits_of_bnf (replicate (nwits_of_bnf bnf) Ds)
  2772             (replicate (nwits_of_bnf bnf) (replicate live HOLogic.unitT)) bnf
  2773             |> map (fn (I, wit) =>
  2774               (I, Lazy.lazy (mk_nat_wit Ds bnf (I, Term.list_comb (wit, map (K HOLogic.unit) I))))))
  2775           ks Dss bnfs;
  2776 
  2777         val nat_wit_thmss = map2 (curry op ~~) nat_witss (map wit_thmss_of_bnf bnfs)
  2778 
  2779         val Iss = map (map fst) nat_witss;
  2780 
  2781         fun filter_wits (I, wit) =
  2782           let val J = filter (fn i => i < m) I;
  2783           in (J, (length J < length I, wit)) end;
  2784 
  2785         val wit_treess = map_index (fn (i, Is) =>
  2786           map_index (finish Iss m [i+m] (i+m)) Is) Iss
  2787           |> map (minimize_wits o map filter_wits o minimize_wits o flat);
  2788 
  2789         val coind_wit_argsss =
  2790           map (map (tree_to_coind_wits nat_wit_thmss o snd o snd) o filter (fst o snd)) wit_treess;
  2791 
  2792         val nonredundant_coind_wit_argsss =
  2793           fold (fn i => fn argsss =>
  2794             nth_map (i - 1) (filter_out (fn xs =>
  2795               exists (fn ys =>
  2796                 let
  2797                   val xs' = (map (fst o fst) xs, snd (fst (hd xs)));
  2798                   val ys' = (map (fst o fst) ys, snd (fst (hd ys)));
  2799                 in
  2800                   eq_pair (subset (op =)) (eq_set (op =)) (xs', ys') andalso not (fst xs' = fst ys')
  2801                 end)
  2802               (flat argsss)))
  2803             argsss)
  2804           ks coind_wit_argsss;
  2805 
  2806         fun prepare_args args =
  2807           let
  2808             val I = snd (fst (hd args));
  2809             val (dummys, args') =
  2810               map_split (fn i =>
  2811                 (case find_first (fn arg => fst (fst arg) = i - 1) args of
  2812                   SOME (_, ((_, wit), thms)) => (NONE, (Lazy.force wit, thms))
  2813                 | NONE =>
  2814                   (SOME (i - 1), (mk_dummy_wit (nth Dss (i - 1)) (nth bnfs (i - 1)) I, []))))
  2815               ks;
  2816           in
  2817             ((I, dummys), apsnd flat (split_list args'))
  2818           end;
  2819 
  2820         fun mk_coind_wits ((I, dummys), (args, thms)) =
  2821           ((I, dummys), (map (fn i => mk_coiter Ts args i $ HOLogic.unit) ks, thms));
  2822 
  2823         val coind_witss =
  2824           maps (map (mk_coind_wits o prepare_args)) nonredundant_coind_wit_argsss;
  2825 
  2826         fun mk_coind_wit_thms ((I, dummys), (wits, wit_thms)) =
  2827           let
  2828             fun mk_goal sets y y_copy y'_copy j =
  2829               let
  2830                 fun mk_conjunct set z dummy wit =
  2831                   mk_Ball (set $ z) (Term.absfree y'_copy
  2832                     (if dummy = NONE orelse member (op =) I (j - 1) then
  2833                       HOLogic.mk_imp (HOLogic.mk_eq (z, wit),
  2834                         if member (op =) I (j - 1) then HOLogic.mk_eq (y_copy, y)
  2835                         else @{term False})
  2836                     else @{term True}));
  2837               in
  2838                 fold_rev Logic.all (map (nth ys) I @ Jzs) (HOLogic.mk_Trueprop
  2839                   (Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct sets Jzs dummys wits)))
  2840               end;
  2841             val goals = map5 mk_goal setss_by_range ys ys_copy ys'_copy ls;
  2842           in
  2843             map2 (fn goal => fn induct =>
  2844               Skip_Proof.prove lthy [] [] goal
  2845                 (mk_coind_wit_tac induct coiter_thms (flat set_natural'ss) wit_thms)
  2846               |> Thm.close_derivation)
  2847             goals hset_induct_thms
  2848             |> map split_conj_thm
  2849             |> transpose
  2850             |> map (map_filter (try (fn thm => thm RS bspec RS mp)))
  2851             |> curry op ~~ (map_index Library.I (map (close_wit I) wits))
  2852             |> filter (fn (_, thms) => length thms = m)
  2853           end;
  2854 
  2855         val coind_wit_thms = maps mk_coind_wit_thms coind_witss;
  2856 
  2857         val witss = map2 (fn Ds => fn bnf => mk_wits_of_bnf
  2858           (replicate (nwits_of_bnf bnf) Ds)
  2859           (replicate (nwits_of_bnf bnf) (passiveAs @ Ts)) bnf) Dss bnfs;
  2860 
  2861         val fld_witss =
  2862           map (map (uncurry close_wit o tree_to_fld_wit ys flds witss o snd o snd) o
  2863             filter_out (fst o snd)) wit_treess;
  2864 
  2865         val all_witss =
  2866           fold (fn ((i, wit), thms) => fn witss =>
  2867             nth_map i (fn (thms', wits) => (thms @ thms', wit :: wits)) witss)
  2868           coind_wit_thms (map (pair []) fld_witss)
  2869           |> map (apsnd (map snd o minimize_wits));
  2870 
  2871         val wit_tac = mk_wit_tac n unf_fld_thms (flat set_simp_thmss) (maps wit_thms_of_bnf bnfs);
  2872 
  2873         val (Jbnfs, lthy) =
  2874           fold_map6 (fn tacs => fn b => fn map => fn sets => fn T => fn (thms, wits) =>
  2875             bnf_def Dont_Inline user_policy I tacs (wit_tac thms) (SOME deads)
  2876               ((((b, fold_rev Term.absfree fs' map), sets), absdummy T bd), wits))
  2877           tacss bs fs_maps setss_by_bnf Ts all_witss lthy;
  2878 
  2879         val fold_maps = Local_Defs.fold lthy (map (fn bnf =>
  2880           mk_unabs_def m (map_def_of_bnf bnf RS @{thm meta_eq_to_obj_eq})) Jbnfs);
  2881 
  2882         val fold_sets = Local_Defs.fold lthy (maps (fn bnf =>
  2883          map (fn thm => thm RS @{thm meta_eq_to_obj_eq}) (set_defs_of_bnf bnf)) Jbnfs);
  2884 
  2885         val timer = time (timer "registered new codatatypes as BNFs");
  2886 
  2887         val set_incl_thmss = map (map fold_sets) hset_unf_incl_thmss;
  2888         val set_set_incl_thmsss = map (map (map fold_sets)) hset_hset_unf_incl_thmsss;
  2889         val set_induct_thms = map fold_sets hset_induct_thms;
  2890 
  2891         val rels = map2 (fn Ds => mk_rel_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2892         val Jrels = map (mk_rel_of_bnf deads passiveAs passiveBs) Jbnfs;
  2893         val preds = map2 (fn Ds => mk_pred_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2894         val Jpreds = map (mk_pred_of_bnf deads passiveAs passiveBs) Jbnfs;
  2895 
  2896         val JrelRs = map (fn Jrel => Term.list_comb (Jrel, JRs)) Jrels;
  2897         val relRs = map (fn rel => Term.list_comb (rel, JRs @ JrelRs)) rels;
  2898         val Jpredphis = map (fn Jrel => Term.list_comb (Jrel, Jphis)) Jpreds;
  2899         val predphis = map (fn rel => Term.list_comb (rel, Jphis @ Jpredphis)) preds;
  2900 
  2901         val in_rels = map in_rel_of_bnf bnfs;
  2902         val in_Jrels = map in_rel_of_bnf Jbnfs;
  2903         val Jpred_defs =
  2904           map (Drule.abs_def o (fn thm => thm RS @{thm eq_reflection}) o pred_def_of_bnf) Jbnfs;
  2905 
  2906         val folded_map_simp_thms = map fold_maps map_simp_thms;
  2907         val folded_set_simp_thmss = map (map fold_sets) set_simp_thmss;
  2908         val folded_set_simp_thmss' = transpose folded_set_simp_thmss;
  2909 
  2910         val Jrel_unfold_thms =
  2911           let
  2912             fun mk_goal Jz Jz' unf unf' JrelR relR = fold_rev Logic.all (Jz :: Jz' :: JRs)
  2913               (mk_Trueprop_eq (HOLogic.mk_mem (HOLogic.mk_prod (Jz, Jz'), JrelR),
  2914                   HOLogic.mk_mem (HOLogic.mk_prod (unf $ Jz, unf' $ Jz'), relR)));
  2915             val goals = map6 mk_goal Jzs Jz's unfs unf's JrelRs relRs;
  2916           in
  2917             map12 (fn i => fn goal => fn in_rel => fn map_comp => fn map_cong =>
  2918               fn map_simp => fn set_simps => fn unf_inject => fn unf_fld =>
  2919               fn set_naturals => fn set_incls => fn set_set_inclss =>
  2920               Skip_Proof.prove lthy [] [] goal
  2921                 (K (mk_rel_unfold_tac in_Jrels i in_rel map_comp map_cong map_simp set_simps
  2922                   unf_inject unf_fld set_naturals set_incls set_set_inclss))
  2923               |> Thm.close_derivation)
  2924             ks goals in_rels map_comp's map_congs folded_map_simp_thms folded_set_simp_thmss'
  2925               unf_inject_thms unf_fld_thms set_natural'ss set_incl_thmss set_set_incl_thmsss
  2926           end;
  2927 
  2928         val Jpred_unfold_thms =
  2929           let
  2930             fun mk_goal Jz Jz' unf unf' Jpredphi predphi = fold_rev Logic.all (Jz :: Jz' :: Jphis)
  2931               (mk_Trueprop_eq (Jpredphi $ Jz $ Jz', predphi $ (unf $ Jz) $ (unf' $ Jz')));
  2932             val goals = map6 mk_goal Jzs Jz's unfs unf's Jpredphis predphis;
  2933           in
  2934             map3 (fn goal => fn pred_def => fn Jrel_unfold =>
  2935               Skip_Proof.prove lthy [] [] goal (mk_pred_unfold_tac pred_def Jpred_defs Jrel_unfold)
  2936               |> Thm.close_derivation)
  2937             goals pred_defs Jrel_unfold_thms
  2938           end;
  2939 
  2940         val timer = time (timer "additional properties");
  2941 
  2942         val ls' = if m = 1 then [0] else ls;
  2943 
  2944         val Jbnf_common_notes =
  2945           [(map_uniqueN, [fold_maps map_unique_thm])] @
  2946           map2 (fn i => fn thm => (mk_set_inductN i, [thm])) ls' set_induct_thms
  2947           |> map (fn (thmN, thms) =>
  2948             ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  2949 
  2950         val Jbnf_notes =
  2951           [(map_simpsN, map single folded_map_simp_thms),
  2952           (set_inclN, set_incl_thmss),
  2953           (set_set_inclN, map flat set_set_incl_thmsss), (* nicer names? *)
  2954           (rel_unfoldN, map single Jrel_unfold_thms),
  2955           (pred_unfoldN, map single Jpred_unfold_thms)] @
  2956           map2 (fn i => fn thms => (mk_set_simpsN i, map single thms)) ls' folded_set_simp_thmss
  2957           |> maps (fn (thmN, thmss) =>
  2958             map2 (fn b => fn thms =>
  2959               ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  2960             bs thmss)
  2961       in
  2962         lthy |> Local_Theory.notes (Jbnf_common_notes @ Jbnf_notes) |> snd
  2963       end;
  2964 
  2965       val common_notes =
  2966         [(unf_coinductN, [unf_coinduct_thm]),
  2967         (rel_coinductN, [rel_coinduct_thm]),
  2968         (pred_coinductN, [pred_coinduct_thm]),
  2969         (unf_coinduct_uptoN, [unf_coinduct_upto_thm]),
  2970         (rel_coinduct_uptoN, [rel_coinduct_upto_thm]),
  2971         (pred_coinduct_uptoN, [pred_coinduct_upto_thm])]
  2972         |> map (fn (thmN, thms) =>
  2973           ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  2974 
  2975       val notes =
  2976         [(coiterN, coiter_thms),
  2977         (coiter_uniqueN, coiter_unique_thms),
  2978         (corecN, corec_thms),
  2979         (unf_fldN, unf_fld_thms),
  2980         (fld_unfN, fld_unf_thms),
  2981         (unf_injectN, unf_inject_thms),
  2982         (unf_exhaustN, unf_exhaust_thms),
  2983         (fld_injectN, fld_inject_thms),
  2984         (fld_exhaustN, fld_exhaust_thms),
  2985         (fld_coiterN, fld_coiter_thms)]
  2986         |> map (apsnd (map single))
  2987         |> maps (fn (thmN, thmss) =>
  2988           map2 (fn b => fn thms =>
  2989             ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  2990           bs thmss)
  2991   in
  2992     ((unfs, flds, unf_fld_thms, fld_unf_thms, fld_inject_thms),
  2993       lthy |> Local_Theory.notes (common_notes @ notes) |> snd)
  2994   end;
  2995 
  2996 val _ =
  2997   Outer_Syntax.local_theory @{command_spec "codata_raw"} "greatest fixed points for BNF equations"
  2998     (Parse.and_list1
  2999       ((Parse.binding --| Parse.$$$ ":") -- (Parse.typ --| Parse.$$$ "=" -- Parse.typ)) >>
  3000       (snd oo fp_bnf_cmd bnf_gfp o apsnd split_list o split_list));
  3001 
  3002 end;