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