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