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