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