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