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