src/HOL/Tools/BNF/bnf_gfp.ML
author desharna
Wed Jul 30 10:50:28 2014 +0200 (2014-07-30)
changeset 57700 a2c4adb839a9
parent 57631 959caab43a3d
child 57726 18b8a8f10313
permissions -rw-r--r--
generate 'set_induct' theorem for codatatypes
     1 (*  Title:      HOL/Tools/BNF/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     BNF_Comp.absT_info list -> 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_bss0 bs resBs (resDs, Dss) bnfs _ lthy =
    59   let
    60     val time = time lthy;
    61     val timer = time (Timer.startRealTimer ());
    62 
    63     val live = live_of_bnf (hd bnfs);
    64     val n = length bnfs; (*active*)
    65     val ks = 1 upto n;
    66     val m = live - n; (*passive, if 0 don't generate a new BNF*)
    67     val ls = 1 upto m;
    68 
    69     val note_all = Config.get lthy bnf_note_all;
    70     val b_names = map Binding.name_of bs;
    71     val b_name = mk_common_name b_names;
    72     val b = Binding.name b_name;
    73     val mk_internal_b = Binding.name #> Binding.prefix true b_name #> Binding.conceal;
    74     fun mk_internal_bs name =
    75       map (fn b =>
    76         Binding.prefix true b_name (Binding.prefix_name (name ^ "_") b) |> Binding.conceal) bs;
    77     val external_bs = map2 (Binding.prefix false) b_names bs
    78       |> note_all = false ? map Binding.conceal;
    79 
    80     (* TODO: check if m, n, etc., are sane *)
    81 
    82     val deads = fold (union (op =)) Dss resDs;
    83     val names_lthy = fold Variable.declare_typ deads lthy;
    84     val passives = map fst (subtract (op = o apsnd TFree) deads resBs);
    85 
    86     (* tvars *)
    87     val ((((((passiveAs, activeAs), passiveBs), activeBs), passiveCs), activeCs), idxT) = names_lthy
    88       |> variant_tfrees passives
    89       ||>> mk_TFrees n
    90       ||>> variant_tfrees passives
    91       ||>> mk_TFrees n
    92       ||>> mk_TFrees m
    93       ||>> mk_TFrees n
    94       ||> fst o mk_TFrees 1
    95       ||> the_single;
    96 
    97     val allAs = passiveAs @ activeAs;
    98     val allBs' = passiveBs @ activeBs;
    99     val Ass = replicate n allAs;
   100     val allBs = passiveAs @ activeBs;
   101     val Bss = replicate n allBs;
   102     val allCs = passiveAs @ activeCs;
   103     val allCs' = passiveBs @ activeCs;
   104     val Css' = replicate n allCs';
   105 
   106     (* types *)
   107     val dead_poss =
   108       map (fn x => if member (op =) deads (TFree x) then SOME (TFree x) else NONE) resBs;
   109     fun mk_param NONE passive = (hd passive, tl passive)
   110       | mk_param (SOME a) passive = (a, passive);
   111     val mk_params = fold_map mk_param dead_poss #> fst;
   112 
   113     fun mk_FTs Ts = map2 (fn Ds => mk_T_of_bnf Ds Ts) Dss bnfs;
   114     val (params, params') = `(map Term.dest_TFree) (mk_params passiveAs);
   115     val FTsAs = mk_FTs allAs;
   116     val FTsBs = mk_FTs allBs;
   117     val FTsCs = mk_FTs allCs;
   118     val ATs = map HOLogic.mk_setT passiveAs;
   119     val BTs = map HOLogic.mk_setT activeAs;
   120     val B'Ts = map HOLogic.mk_setT activeBs;
   121     val B''Ts = map HOLogic.mk_setT activeCs;
   122     val sTs = map2 (fn T => fn U => T --> U) activeAs FTsAs;
   123     val s'Ts = map2 (fn T => fn U => T --> U) activeBs FTsBs;
   124     val s''Ts = map2 (fn T => fn U => T --> U) activeCs FTsCs;
   125     val fTs = map2 (fn T => fn U => T --> U) activeAs activeBs;
   126     val self_fTs = map (fn T => T --> T) activeAs;
   127     val gTs = map2 (fn T => fn U => T --> U) activeBs activeCs;
   128     val all_gTs = map2 (fn T => fn U => T --> U) allBs allCs';
   129     val RTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeBs;
   130     val sRTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeAs;
   131     val R'Ts = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeBs activeCs;
   132     val setsRTs = map HOLogic.mk_setT sRTs;
   133     val setRTs = map HOLogic.mk_setT RTs;
   134     val all_sbisT = HOLogic.mk_tupleT setsRTs;
   135     val setR'Ts = map HOLogic.mk_setT R'Ts;
   136     val FRTs = mk_FTs (passiveAs @ RTs);
   137     val sumBsAs = map2 (curry mk_sumT) activeBs activeAs;
   138     val sumFTs = mk_FTs (passiveAs @ sumBsAs);
   139     val sum_sTs = map2 (fn T => fn U => T --> U) activeAs sumFTs;
   140 
   141     (* terms *)
   142     val mapsAsAs = map4 mk_map_of_bnf Dss Ass Ass bnfs;
   143     val mapsAsBs = map4 mk_map_of_bnf Dss Ass Bss bnfs;
   144     val mapsBsCs' = map4 mk_map_of_bnf Dss Bss Css' bnfs;
   145     val mapsAsCs' = map4 mk_map_of_bnf Dss Ass Css' bnfs;
   146     val map_Inls = map4 mk_map_of_bnf Dss Bss (replicate n (passiveAs @ sumBsAs)) bnfs;
   147     val map_Inls_rev = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ sumBsAs)) Bss bnfs;
   148     val map_fsts = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Ass bnfs;
   149     val map_snds = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Bss bnfs;
   150     fun mk_setss Ts = map3 mk_sets_of_bnf (map (replicate live) Dss)
   151       (map (replicate live) (replicate n Ts)) bnfs;
   152     val setssAs = mk_setss allAs;
   153     val setssAs' = transpose setssAs;
   154     val bis_setss = mk_setss (passiveAs @ RTs);
   155     val relsAsBs = map4 mk_rel_of_bnf Dss Ass Bss bnfs;
   156     val bds = map3 mk_bd_of_bnf Dss Ass bnfs;
   157     val sum_bd = Library.foldr1 (uncurry mk_csum) bds;
   158     val sum_bdT = fst (dest_relT (fastype_of sum_bd));
   159     val (sum_bdT_params, sum_bdT_params') = `(map TFree) (Term.add_tfreesT sum_bdT []);
   160 
   161     val ((((((((((((((((((((((((((((((((zs, zs'), zs_copy), zs_copy2), z's), (ys, ys')),
   162       Bs), Bs_copy), B's), B''s), ss), sum_ss), s's), s''s), fs), fs_copy),
   163       self_fs), gs), all_gs), xFs), yFs), yFs_copy), RFs), (Rtuple, Rtuple')),
   164       (nat, nat')), Rs), Rs_copy), R's), sRs), (idx, idx')), Idx), Ris), names_lthy) = lthy
   165       |> mk_Frees' "b" activeAs
   166       ||>> mk_Frees "b" activeAs
   167       ||>> mk_Frees "b" activeAs
   168       ||>> mk_Frees "b" activeBs
   169       ||>> mk_Frees' "y" passiveAs
   170       ||>> mk_Frees "B" BTs
   171       ||>> mk_Frees "B" BTs
   172       ||>> mk_Frees "B'" B'Ts
   173       ||>> mk_Frees "B''" B''Ts
   174       ||>> mk_Frees "s" sTs
   175       ||>> mk_Frees "sums" sum_sTs
   176       ||>> mk_Frees "s'" s'Ts
   177       ||>> mk_Frees "s''" s''Ts
   178       ||>> mk_Frees "f" fTs
   179       ||>> mk_Frees "f" fTs
   180       ||>> mk_Frees "f" self_fTs
   181       ||>> mk_Frees "g" gTs
   182       ||>> mk_Frees "g" all_gTs
   183       ||>> mk_Frees "x" FTsAs
   184       ||>> mk_Frees "y" FTsBs
   185       ||>> mk_Frees "y" FTsBs
   186       ||>> mk_Frees "x" FRTs
   187       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Rtuple") all_sbisT
   188       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "n") HOLogic.natT
   189       ||>> mk_Frees "R" setRTs
   190       ||>> mk_Frees "R" setRTs
   191       ||>> mk_Frees "R'" setR'Ts
   192       ||>> mk_Frees "R" setsRTs
   193       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") idxT
   194       ||>> yield_singleton (mk_Frees "I") (HOLogic.mk_setT idxT)
   195       ||>> mk_Frees "Ri" (map (fn T => idxT --> T) setRTs);
   196 
   197     val passive_UNIVs = map HOLogic.mk_UNIV passiveAs;
   198     val passive_eqs = map HOLogic.eq_const passiveAs;
   199     val active_UNIVs = map HOLogic.mk_UNIV activeAs;
   200     val sum_UNIVs = map HOLogic.mk_UNIV sumBsAs;
   201     val passive_ids = map HOLogic.id_const passiveAs;
   202     val active_ids = map HOLogic.id_const activeAs;
   203     val Inls = map2 Inl_const activeBs activeAs;
   204     val fsts = map fst_const RTs;
   205     val snds = map snd_const RTs;
   206 
   207     (* thms *)
   208     val bd_card_orders = map bd_card_order_of_bnf bnfs;
   209     val bd_card_order = hd bd_card_orders
   210     val bd_Card_orders = map bd_Card_order_of_bnf bnfs;
   211     val bd_Card_order = hd bd_Card_orders;
   212     val bd_Cinfinites = map bd_Cinfinite_of_bnf bnfs;
   213     val bd_Cinfinite = hd bd_Cinfinites;
   214     val in_monos = map in_mono_of_bnf bnfs;
   215     val map_comp0s = map map_comp0_of_bnf bnfs;
   216     val sym_map_comps = map mk_sym map_comp0s;
   217     val map_comps = map map_comp_of_bnf bnfs;
   218     val map_cong0s = map map_cong0_of_bnf bnfs;
   219     val map_id0s = map map_id0_of_bnf bnfs;
   220     val map_ids = map map_id_of_bnf bnfs;
   221     val set_bdss = map set_bd_of_bnf bnfs;
   222     val set_mapss = map set_map_of_bnf bnfs;
   223     val rel_congs = map rel_cong_of_bnf bnfs;
   224     val rel_converseps = map rel_conversep_of_bnf bnfs;
   225     val rel_Grps = map rel_Grp_of_bnf bnfs;
   226     val rel_OOs = map rel_OO_of_bnf bnfs;
   227     val in_rels = map in_rel_of_bnf bnfs;
   228 
   229     val timer = time (timer "Extracted terms & thms");
   230 
   231     (* derived thms *)
   232 
   233     (*map g1 ... gm g(m+1) ... g(m+n) (map id ... id f(m+1) ... f(m+n) x) =
   234       map g1 ... gm (g(m+1) o f(m+1)) ... (g(m+n) o f(m+n)) x*)
   235     fun mk_map_comp_id x mapAsBs mapBsCs mapAsCs map_comp0 =
   236       let
   237         val lhs = Term.list_comb (mapBsCs, all_gs) $
   238           (Term.list_comb (mapAsBs, passive_ids @ fs) $ x);
   239         val rhs =
   240           Term.list_comb (mapAsCs, take m all_gs @ map HOLogic.mk_comp (drop m all_gs ~~ fs)) $ x;
   241       in
   242         Goal.prove_sorry lthy [] [] (mk_Trueprop_eq (lhs, rhs))
   243           (fn {context = ctxt, prems = _} => mk_map_comp_id_tac ctxt map_comp0)
   244         |> Thm.close_derivation
   245         |> singleton (Proof_Context.export names_lthy lthy)
   246       end;
   247 
   248     val map_comp_id_thms = map5 mk_map_comp_id xFs mapsAsBs mapsBsCs' mapsAsCs' map_comps;
   249 
   250     (*forall a : set(m+1) x. f(m+1) a = a; ...; forall a : set(m+n) x. f(m+n) a = a ==>
   251       map id ... id f(m+1) ... f(m+n) x = x*)
   252     fun mk_map_cong0L x mapAsAs sets map_cong0 map_id =
   253       let
   254         fun mk_prem set f z z' =
   255           HOLogic.mk_Trueprop
   256             (mk_Ball (set $ x) (Term.absfree z' (HOLogic.mk_eq (f $ z, z))));
   257         val prems = map4 mk_prem (drop m sets) self_fs zs zs';
   258         val goal = mk_Trueprop_eq (Term.list_comb (mapAsAs, passive_ids @ self_fs) $ x, x);
   259       in
   260         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, goal))
   261           (K (mk_map_cong0L_tac m map_cong0 map_id))
   262         |> Thm.close_derivation
   263         |> singleton (Proof_Context.export names_lthy lthy)
   264       end;
   265 
   266     val map_cong0L_thms = map5 mk_map_cong0L xFs mapsAsAs setssAs map_cong0s map_ids;
   267     val in_mono'_thms = map (fn thm =>
   268       (thm OF (replicate m subset_refl)) RS @{thm set_mp}) in_monos;
   269 
   270     val map_arg_cong_thms =
   271       let
   272         val prems = map2 (curry mk_Trueprop_eq) yFs yFs_copy;
   273         val maps = map (fn mapx => Term.list_comb (mapx, all_gs)) mapsBsCs';
   274         val concls =
   275           map3 (fn x => fn y => fn mapx => mk_Trueprop_eq (mapx $ x, mapx $ y)) yFs yFs_copy maps;
   276         val goals = map2 (fn prem => fn concl => Logic.mk_implies (prem, concl)) prems concls;
   277       in
   278         map (fn goal =>
   279           Goal.prove_sorry lthy [] [] goal (K ((hyp_subst_tac lthy THEN' rtac refl) 1))
   280           |> Thm.close_derivation
   281           |> singleton (Proof_Context.export names_lthy lthy)) goals
   282       end;
   283 
   284     val timer = time (timer "Derived simple theorems");
   285 
   286     (* coalgebra *)
   287 
   288     val coalg_bind = mk_internal_b (coN ^ algN) ;
   289     val coalg_def_bind = (Thm.def_binding coalg_bind, []);
   290 
   291     (*forall i = 1 ... n: (\<forall>x \<in> Bi. si \<in> Fi_in UNIV .. UNIV B1 ... Bn)*)
   292     val coalg_spec =
   293       let
   294         val ins = map3 mk_in (replicate n (passive_UNIVs @ Bs)) setssAs FTsAs;
   295         fun mk_coalg_conjunct B s X z z' =
   296           mk_Ball B (Term.absfree z' (HOLogic.mk_mem (s $ z, X)));
   297 
   298         val rhs = Library.foldr1 HOLogic.mk_conj (map5 mk_coalg_conjunct Bs ss ins zs zs')
   299       in
   300         fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss) rhs
   301       end;
   302 
   303     val ((coalg_free, (_, coalg_def_free)), (lthy, lthy_old)) =
   304       lthy
   305       |> Local_Theory.define ((coalg_bind, NoSyn), (coalg_def_bind, coalg_spec))
   306       ||> `Local_Theory.restore;
   307 
   308     val phi = Proof_Context.export_morphism lthy_old lthy;
   309     val coalg = fst (Term.dest_Const (Morphism.term phi coalg_free));
   310     val coalg_def = mk_unabs_def (2 * n) (Morphism.thm phi coalg_def_free RS meta_eq_to_obj_eq);
   311 
   312     fun mk_coalg Bs ss =
   313       let
   314         val args = Bs @ ss;
   315         val Ts = map fastype_of args;
   316         val coalgT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   317       in
   318         Term.list_comb (Const (coalg, coalgT), args)
   319       end;
   320 
   321     val coalg_prem = HOLogic.mk_Trueprop (mk_coalg Bs ss);
   322 
   323     val coalg_in_thms = map (fn i =>
   324       coalg_def RS iffD1 RS mk_conjunctN n i RS bspec) ks
   325 
   326     val coalg_set_thmss =
   327       let
   328         val coalg_prem = HOLogic.mk_Trueprop (mk_coalg Bs ss);
   329         fun mk_prem x B = mk_Trueprop_mem (x, B);
   330         fun mk_concl s x B set = HOLogic.mk_Trueprop (mk_leq (set $ (s $ x)) B);
   331         val prems = map2 mk_prem zs Bs;
   332         val conclss = map3 (fn s => fn x => fn sets => map2 (mk_concl s x) Bs (drop m sets))
   333           ss zs setssAs;
   334         val goalss = map2 (fn prem => fn concls => map (fn concl =>
   335           Logic.list_implies (coalg_prem :: [prem], concl)) concls) prems conclss;
   336       in
   337         map (fn goals => map (fn goal =>
   338           Goal.prove_sorry lthy [] [] goal (K (mk_coalg_set_tac coalg_def))
   339           |> Thm.close_derivation
   340           |> singleton (Proof_Context.export names_lthy lthy)) goals) goalss
   341       end;
   342 
   343     fun mk_tcoalg BTs = mk_coalg (map HOLogic.mk_UNIV BTs);
   344 
   345     val tcoalg_thm =
   346       let
   347         val goal = HOLogic.mk_Trueprop (mk_tcoalg activeAs ss)
   348       in
   349         Goal.prove_sorry lthy [] [] goal
   350           (K (rtac (coalg_def RS iffD2) 1 THEN CONJ_WRAP
   351             (K (EVERY' [rtac ballI, rtac CollectI,
   352               CONJ_WRAP' (K (EVERY' [rtac @{thm subset_UNIV}])) allAs] 1)) ss))
   353         |> Thm.close_derivation
   354         |> singleton (Proof_Context.export names_lthy lthy)
   355       end;
   356 
   357     val timer = time (timer "Coalgebra definition & thms");
   358 
   359     (* morphism *)
   360 
   361     val mor_bind = mk_internal_b morN;
   362     val mor_def_bind = (Thm.def_binding mor_bind, []);
   363 
   364     (*fbetw) forall i = 1 ... n: (\<forall>x \<in> Bi. fi x \<in> B'i)*)
   365     (*mor) forall i = 1 ... n: (\<forall>x \<in> Bi.
   366        Fi_map id ... id f1 ... fn (si x) = si' (fi x)*)
   367     val mor_spec =
   368       let
   369         fun mk_fbetw f B1 B2 z z' =
   370           mk_Ball B1 (Term.absfree z' (HOLogic.mk_mem (f $ z, B2)));
   371         fun mk_mor B mapAsBs f s s' z z' =
   372           mk_Ball B (Term.absfree z' (HOLogic.mk_eq
   373             (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ z]), s' $ (f $ z))));
   374         val rhs = HOLogic.mk_conj
   375           (Library.foldr1 HOLogic.mk_conj (map5 mk_fbetw fs Bs B's zs zs'),
   376            Library.foldr1 HOLogic.mk_conj (map7 mk_mor Bs mapsAsBs fs ss s's zs zs'))
   377       in
   378         fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss @ B's @ s's @ fs) rhs
   379       end;
   380 
   381     val ((mor_free, (_, mor_def_free)), (lthy, lthy_old)) =
   382       lthy
   383       |> Local_Theory.define ((mor_bind, NoSyn), (mor_def_bind, mor_spec))
   384       ||> `Local_Theory.restore;
   385 
   386     val phi = Proof_Context.export_morphism lthy_old lthy;
   387     val mor = fst (Term.dest_Const (Morphism.term phi mor_free));
   388     val mor_def = mk_unabs_def (5 * n) (Morphism.thm phi mor_def_free RS meta_eq_to_obj_eq);
   389 
   390     fun mk_mor Bs1 ss1 Bs2 ss2 fs =
   391       let
   392         val args = Bs1 @ ss1 @ Bs2 @ ss2 @ fs;
   393         val Ts = map fastype_of (Bs1 @ ss1 @ Bs2 @ ss2 @ fs);
   394         val morT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   395       in
   396         Term.list_comb (Const (mor, morT), args)
   397       end;
   398 
   399     val mor_prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   400 
   401     val (mor_image_thms, morE_thms) =
   402       let
   403         val prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   404         fun mk_image_goal f B1 B2 =
   405           Logic.mk_implies (prem, HOLogic.mk_Trueprop (mk_leq (mk_image f $ B1) B2));
   406         val image_goals = map3 mk_image_goal fs Bs B's;
   407         fun mk_elim_goal B mapAsBs f s s' x =
   408           Logic.list_implies ([prem, mk_Trueprop_mem (x, B)],
   409             mk_Trueprop_eq (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ x]), s' $ (f $ x)));
   410         val elim_goals = map6 mk_elim_goal Bs mapsAsBs fs ss s's zs;
   411         fun prove goal =
   412           Goal.prove_sorry lthy [] [] goal (K (mk_mor_elim_tac mor_def))
   413           |> Thm.close_derivation
   414           |> singleton (Proof_Context.export names_lthy lthy);
   415       in
   416         (map prove image_goals, map prove elim_goals)
   417       end;
   418 
   419     val mor_image'_thms = map (fn thm => @{thm set_mp} OF [thm, imageI]) mor_image_thms;
   420 
   421     val mor_incl_thm =
   422       let
   423         val prems = map2 (HOLogic.mk_Trueprop oo mk_leq) Bs Bs_copy;
   424         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss Bs_copy ss active_ids);
   425       in
   426         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, concl))
   427           (K (mk_mor_incl_tac mor_def map_ids))
   428         |> Thm.close_derivation
   429         |> singleton (Proof_Context.export names_lthy lthy)
   430       end;
   431 
   432     val mor_id_thm = mor_incl_thm OF (replicate n subset_refl);
   433 
   434     val mor_comp_thm =
   435       let
   436         val prems =
   437           [HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs),
   438            HOLogic.mk_Trueprop (mk_mor B's s's B''s s''s gs)];
   439         val concl =
   440           HOLogic.mk_Trueprop (mk_mor Bs ss B''s s''s (map2 (curry HOLogic.mk_comp) gs fs));
   441       in
   442         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, concl))
   443           (K (mk_mor_comp_tac mor_def mor_image'_thms morE_thms map_comp_id_thms))
   444         |> Thm.close_derivation
   445         |> singleton (Proof_Context.export names_lthy lthy)
   446       end;
   447 
   448     val mor_cong_thm =
   449       let
   450         val prems = map HOLogic.mk_Trueprop
   451          (map2 (curry HOLogic.mk_eq) fs_copy fs @ [mk_mor Bs ss B's s's fs])
   452         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs_copy);
   453       in
   454         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, concl))
   455           (K ((hyp_subst_tac lthy THEN' atac) 1))
   456         |> Thm.close_derivation
   457         |> singleton (Proof_Context.export names_lthy lthy)
   458       end;
   459 
   460     val mor_UNIV_thm =
   461       let
   462         fun mk_conjunct mapAsBs f s s' = HOLogic.mk_eq
   463             (HOLogic.mk_comp (Term.list_comb (mapAsBs, passive_ids @ fs), s),
   464             HOLogic.mk_comp (s', f));
   465         val lhs = mk_mor active_UNIVs ss (map HOLogic.mk_UNIV activeBs) s's fs;
   466         val rhs = Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct mapsAsBs fs ss s's);
   467       in
   468         Goal.prove_sorry lthy [] [] (mk_Trueprop_eq (lhs, rhs))
   469           (K (mk_mor_UNIV_tac morE_thms mor_def))
   470         |> Thm.close_derivation
   471         |> singleton (Proof_Context.export names_lthy lthy)
   472       end;
   473 
   474     val mor_str_thm =
   475       let
   476         val maps = map2 (fn Ds => fn bnf => Term.list_comb
   477           (mk_map_of_bnf Ds allAs (passiveAs @ FTsAs) bnf, passive_ids @ ss)) Dss bnfs;
   478       in
   479         Goal.prove_sorry lthy [] []
   480           (HOLogic.mk_Trueprop (mk_mor active_UNIVs ss (map HOLogic.mk_UNIV FTsAs) maps ss))
   481           (K (mk_mor_str_tac ks mor_UNIV_thm))
   482         |> Thm.close_derivation
   483         |> singleton (Proof_Context.export names_lthy lthy)
   484       end;
   485 
   486     val mor_case_sum_thm =
   487       let
   488         val maps = map3 (fn s => fn sum_s => fn mapx =>
   489           mk_case_sum (HOLogic.mk_comp (Term.list_comb (mapx, passive_ids @ Inls), s), sum_s))
   490           s's sum_ss map_Inls;
   491       in
   492         Goal.prove_sorry lthy [] []
   493           (HOLogic.mk_Trueprop (mk_mor (map HOLogic.mk_UNIV activeBs) s's sum_UNIVs maps Inls))
   494           (K (mk_mor_case_sum_tac ks mor_UNIV_thm))
   495         |> Thm.close_derivation
   496         |> singleton (Proof_Context.export names_lthy lthy)
   497       end;
   498 
   499     val timer = time (timer "Morphism definition & thms");
   500 
   501     (* bisimulation *)
   502 
   503     val bis_bind = mk_internal_b bisN;
   504     val bis_def_bind = (Thm.def_binding bis_bind, []);
   505 
   506     fun mk_bis_le_conjunct R B1 B2 = mk_leq R (mk_Times (B1, B2));
   507     val bis_le = Library.foldr1 HOLogic.mk_conj (map3 mk_bis_le_conjunct Rs Bs B's)
   508 
   509     val bis_spec =
   510       let
   511         val fst_args = passive_ids @ fsts;
   512         val snd_args = passive_ids @ snds;
   513         fun mk_bis R s s' b1 b2 RF map1 map2 sets =
   514           list_all_free [b1, b2] (HOLogic.mk_imp
   515             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   516             mk_Bex (mk_in (passive_UNIVs @ Rs) sets (snd (dest_Free RF)))
   517               (Term.absfree (dest_Free RF) (HOLogic.mk_conj
   518                 (HOLogic.mk_eq (Term.list_comb (map1, fst_args) $ RF, s $ b1),
   519                 HOLogic.mk_eq (Term.list_comb (map2, snd_args) $ RF, s' $ b2))))));
   520 
   521         val rhs = HOLogic.mk_conj
   522           (bis_le, Library.foldr1 HOLogic.mk_conj
   523             (map9 mk_bis Rs ss s's zs z's RFs map_fsts map_snds bis_setss))
   524       in
   525         fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss @ B's @ s's @ Rs) rhs
   526       end;
   527 
   528     val ((bis_free, (_, bis_def_free)), (lthy, lthy_old)) =
   529       lthy
   530       |> Local_Theory.define ((bis_bind, NoSyn), (bis_def_bind, bis_spec))
   531       ||> `Local_Theory.restore;
   532 
   533     val phi = Proof_Context.export_morphism lthy_old lthy;
   534     val bis = fst (Term.dest_Const (Morphism.term phi bis_free));
   535     val bis_def = mk_unabs_def (5 * n) (Morphism.thm phi bis_def_free RS meta_eq_to_obj_eq);
   536 
   537     fun mk_bis Bs1 ss1 Bs2 ss2 Rs =
   538       let
   539         val args = Bs1 @ ss1 @ Bs2 @ ss2 @ Rs;
   540         val Ts = map fastype_of args;
   541         val bisT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   542       in
   543         Term.list_comb (Const (bis, bisT), args)
   544       end;
   545 
   546     val bis_cong_thm =
   547       let
   548         val prems = map HOLogic.mk_Trueprop
   549          (mk_bis Bs ss B's s's Rs :: map2 (curry HOLogic.mk_eq) Rs_copy Rs)
   550         val concl = HOLogic.mk_Trueprop (mk_bis Bs ss B's s's Rs_copy);
   551       in
   552         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, concl))
   553           (fn {context = ctxt, prems = _} => (hyp_subst_tac ctxt THEN' atac) 1)
   554         |> Thm.close_derivation
   555         |> singleton (Proof_Context.export names_lthy lthy)
   556       end;
   557 
   558     val bis_rel_thm =
   559       let
   560         fun mk_conjunct R s s' b1 b2 rel =
   561           list_all_free [b1, b2] (HOLogic.mk_imp
   562             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   563             Term.list_comb (rel, passive_eqs @ map mk_in_rel Rs) $ (s $ b1) $ (s' $ b2)));
   564 
   565         val rhs = HOLogic.mk_conj
   566           (bis_le, Library.foldr1 HOLogic.mk_conj
   567             (map6 mk_conjunct Rs ss s's zs z's relsAsBs))
   568       in
   569         Goal.prove_sorry lthy [] [] (mk_Trueprop_eq (mk_bis Bs ss B's s's Rs, rhs))
   570           (K (mk_bis_rel_tac m bis_def in_rels map_comps map_cong0s set_mapss))
   571         |> Thm.close_derivation
   572         |> singleton (Proof_Context.export names_lthy lthy)
   573       end;
   574 
   575     val bis_converse_thm =
   576       Goal.prove_sorry lthy [] []
   577         (Logic.mk_implies (HOLogic.mk_Trueprop (mk_bis Bs ss B's s's Rs),
   578           HOLogic.mk_Trueprop (mk_bis B's s's Bs ss (map mk_converse Rs))))
   579         (K (mk_bis_converse_tac m bis_rel_thm rel_congs rel_converseps))
   580       |> Thm.close_derivation
   581       |> singleton (Proof_Context.export names_lthy lthy);
   582 
   583     val bis_O_thm =
   584       let
   585         val prems =
   586           [HOLogic.mk_Trueprop (mk_bis Bs ss B's s's Rs),
   587            HOLogic.mk_Trueprop (mk_bis B's s's B''s s''s R's)];
   588         val concl =
   589           HOLogic.mk_Trueprop (mk_bis Bs ss B''s s''s (map2 (curry mk_rel_comp) Rs R's));
   590       in
   591         Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, concl))
   592           (K (mk_bis_O_tac lthy m bis_rel_thm rel_congs rel_OOs))
   593         |> Thm.close_derivation
   594         |> singleton (Proof_Context.export names_lthy lthy)
   595       end;
   596 
   597     val bis_Gr_thm =
   598       let
   599         val concl =
   600           HOLogic.mk_Trueprop (mk_bis Bs ss B's s's (map2 mk_Gr Bs fs));
   601       in
   602         Goal.prove_sorry lthy [] [] (Logic.list_implies ([coalg_prem, mor_prem], concl))
   603           (fn {context = ctxt, prems = _} => mk_bis_Gr_tac ctxt bis_rel_thm rel_Grps mor_image_thms
   604             morE_thms coalg_in_thms)
   605         |> Thm.close_derivation
   606         |> singleton (Proof_Context.export names_lthy lthy)
   607       end;
   608 
   609     val bis_image2_thm = bis_cong_thm OF
   610       ((bis_O_thm OF [bis_Gr_thm RS bis_converse_thm, bis_Gr_thm]) ::
   611       replicate n @{thm image2_Gr});
   612 
   613     val bis_Id_on_thm = bis_cong_thm OF ((mor_id_thm RSN (2, bis_Gr_thm)) ::
   614       replicate n @{thm Id_on_Gr});
   615 
   616     val bis_Union_thm =
   617       let
   618         val prem =
   619           HOLogic.mk_Trueprop (mk_Ball Idx
   620             (Term.absfree idx' (mk_bis Bs ss B's s's (map (fn R => R $ idx) Ris))));
   621         val concl =
   622           HOLogic.mk_Trueprop (mk_bis Bs ss B's s's (map (mk_UNION Idx) Ris));
   623       in
   624         Goal.prove_sorry lthy [] [] (Logic.mk_implies (prem, concl))
   625           (fn {context = ctxt, prems = _} => mk_bis_Union_tac ctxt bis_def in_mono'_thms)
   626         |> Thm.close_derivation
   627         |> singleton (Proof_Context.export names_lthy lthy)
   628       end;
   629 
   630     (* self-bisimulation *)
   631 
   632     fun mk_sbis Bs ss Rs = mk_bis Bs ss Bs ss Rs;
   633 
   634     val sbis_prem = HOLogic.mk_Trueprop (mk_sbis Bs ss sRs);
   635 
   636     (* largest self-bisimulation *)
   637 
   638     val lsbis_binds = mk_internal_bs lsbisN;
   639     fun lsbis_bind i = nth lsbis_binds (i - 1);
   640     val lsbis_def_bind = rpair [] o Thm.def_binding o lsbis_bind;
   641 
   642     val all_sbis = HOLogic.mk_Collect (fst Rtuple', snd Rtuple', list_exists_free sRs
   643       (HOLogic.mk_conj (HOLogic.mk_eq (Rtuple, HOLogic.mk_tuple sRs), mk_sbis Bs ss sRs)));
   644 
   645     fun lsbis_spec i =
   646       fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss)
   647         (mk_UNION all_sbis (Term.absfree Rtuple' (mk_nthN n Rtuple i)));
   648 
   649     val ((lsbis_frees, (_, lsbis_def_frees)), (lthy, lthy_old)) =
   650       lthy
   651       |> fold_map (fn i => Local_Theory.define
   652         ((lsbis_bind i, NoSyn), (lsbis_def_bind i, lsbis_spec i))) ks
   653       |>> apsnd split_list o split_list
   654       ||> `Local_Theory.restore;
   655 
   656     val phi = Proof_Context.export_morphism lthy_old lthy;
   657 
   658     val lsbis_defs = map (fn def =>
   659       mk_unabs_def (2 * n) (Morphism.thm phi def RS meta_eq_to_obj_eq)) lsbis_def_frees;
   660     val lsbiss = map (fst o Term.dest_Const o Morphism.term phi) lsbis_frees;
   661 
   662     fun mk_lsbis Bs ss i =
   663       let
   664         val args = Bs @ ss;
   665         val Ts = map fastype_of args;
   666         val RT = mk_relT (`I (HOLogic.dest_setT (fastype_of (nth Bs (i - 1)))));
   667         val lsbisT = Library.foldr (op -->) (Ts, RT);
   668       in
   669         Term.list_comb (Const (nth lsbiss (i - 1), lsbisT), args)
   670       end;
   671 
   672     val sbis_lsbis_thm =
   673       Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop (mk_sbis Bs ss (map (mk_lsbis Bs ss) ks)))
   674         (K (mk_sbis_lsbis_tac lthy lsbis_defs bis_Union_thm bis_cong_thm))
   675       |> Thm.close_derivation
   676       |> singleton (Proof_Context.export names_lthy lthy);
   677 
   678     val lsbis_incl_thms = map (fn i => sbis_lsbis_thm RS
   679       (bis_def RS iffD1 RS conjunct1 RS mk_conjunctN n i)) ks;
   680     val lsbisE_thms = map (fn i => (mk_specN 2 (sbis_lsbis_thm RS
   681       (bis_def RS iffD1 RS conjunct2 RS mk_conjunctN n i))) RS mp) ks;
   682 
   683     val incl_lsbis_thms =
   684       let
   685         fun mk_concl i R = HOLogic.mk_Trueprop (mk_leq R (mk_lsbis Bs ss i));
   686         val goals = map2 (fn i => fn R => Logic.mk_implies (sbis_prem, mk_concl i R)) ks sRs;
   687       in
   688         map3 (fn goal => fn i => fn def =>
   689           Goal.prove_sorry lthy [] [] goal (K (mk_incl_lsbis_tac n i def))
   690           |> Thm.close_derivation
   691           |> singleton (Proof_Context.export names_lthy lthy)) goals ks lsbis_defs
   692       end;
   693 
   694     val equiv_lsbis_thms =
   695       let
   696         fun mk_concl i B = HOLogic.mk_Trueprop (mk_equiv B (mk_lsbis Bs ss i));
   697         val goals = map2 (fn i => fn B => Logic.mk_implies (coalg_prem, mk_concl i B)) ks Bs;
   698       in
   699         map3 (fn goal => fn l_incl => fn incl_l =>
   700           Goal.prove_sorry lthy [] [] goal
   701             (K (mk_equiv_lsbis_tac sbis_lsbis_thm l_incl incl_l
   702               bis_Id_on_thm bis_converse_thm bis_O_thm))
   703           |> Thm.close_derivation
   704           |> singleton (Proof_Context.export names_lthy lthy))
   705         goals lsbis_incl_thms incl_lsbis_thms
   706       end;
   707 
   708     val timer = time (timer "Bisimulations");
   709 
   710     (* bounds *)
   711 
   712     val (lthy, sbd, sbdT,
   713       sbd_card_order, sbd_Cinfinite, sbd_Card_order, set_sbdss) =
   714       if n = 1
   715       then (lthy, sum_bd, sum_bdT, bd_card_order, bd_Cinfinite, bd_Card_order, set_bdss)
   716       else
   717         let
   718           val sbdT_bind = mk_internal_b sum_bdTN;
   719 
   720           val ((sbdT_name, (sbdT_glob_info, sbdT_loc_info)), lthy) =
   721             typedef (sbdT_bind, sum_bdT_params', NoSyn)
   722               (HOLogic.mk_UNIV sum_bdT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
   723 
   724           val sbdT = Type (sbdT_name, sum_bdT_params);
   725           val Abs_sbdT = Const (#Abs_name sbdT_glob_info, sum_bdT --> sbdT);
   726 
   727           val sbd_bind = mk_internal_b sum_bdN;
   728           val sbd_def_bind = (Thm.def_binding sbd_bind, []);
   729 
   730           val sbd_spec = mk_dir_image sum_bd Abs_sbdT;
   731 
   732           val ((sbd_free, (_, sbd_def_free)), (lthy, lthy_old)) =
   733             lthy
   734             |> Local_Theory.define ((sbd_bind, NoSyn), (sbd_def_bind, sbd_spec))
   735             ||> `Local_Theory.restore;
   736 
   737           val phi = Proof_Context.export_morphism lthy_old lthy;
   738 
   739           val sbd_def = Morphism.thm phi sbd_def_free RS meta_eq_to_obj_eq;
   740           val sbd = Const (fst (Term.dest_Const (Morphism.term phi sbd_free)), mk_relT (`I sbdT));
   741 
   742           val Abs_sbdT_inj = mk_Abs_inj_thm (#Abs_inject sbdT_loc_info);
   743           val Abs_sbdT_bij = mk_Abs_bij_thm lthy Abs_sbdT_inj (#Abs_cases sbdT_loc_info);
   744 
   745           val sum_Cinfinite = mk_sum_Cinfinite bd_Cinfinites;
   746           val sum_Card_order = sum_Cinfinite RS conjunct2;
   747           val sum_card_order = mk_sum_card_order bd_card_orders;
   748 
   749           val sbd_ordIso = @{thm ssubst_Pair_rhs} OF
   750             [@{thm dir_image} OF [Abs_sbdT_inj, sum_Card_order], sbd_def];
   751           val sbd_card_order = @{thm iffD2[OF arg_cong[of _ _ card_order]]} OF
   752             [sbd_def, @{thm card_order_dir_image} OF [Abs_sbdT_bij, sum_card_order]];
   753           val sbd_Cinfinite = @{thm Cinfinite_cong} OF [sbd_ordIso, sum_Cinfinite];
   754           val sbd_Card_order = sbd_Cinfinite RS conjunct2;
   755 
   756           fun mk_set_sbd i bd_Card_order bds =
   757             map (fn thm => @{thm ordLeq_ordIso_trans} OF
   758               [bd_Card_order RS mk_ordLeq_csum n i thm, sbd_ordIso]) bds;
   759           val set_sbdss = map3 mk_set_sbd ks bd_Card_orders set_bdss;
   760        in
   761          (lthy, sbd, sbdT, sbd_card_order, sbd_Cinfinite, sbd_Card_order, set_sbdss)
   762        end;
   763 
   764     val sbdTs = replicate n sbdT;
   765     val sum_sbdT = mk_sumTN sbdTs;
   766     val sum_sbd_listT = HOLogic.listT sum_sbdT;
   767     val sum_sbd_list_setT = HOLogic.mk_setT sum_sbd_listT;
   768     val bdTs = passiveAs @ replicate n sbdT;
   769     val to_sbd_maps = map4 mk_map_of_bnf Dss Ass (replicate n bdTs) bnfs;
   770     val bdFTs = mk_FTs bdTs;
   771     val sbdFT = mk_sumTN bdFTs;
   772     val treeT = HOLogic.mk_prodT (sum_sbd_list_setT, sum_sbd_listT --> sbdFT);
   773     val treeQT = HOLogic.mk_setT treeT;
   774     val treeTs = passiveAs @ replicate n treeT;
   775     val treeQTs = passiveAs @ replicate n treeQT;
   776     val treeFTs = mk_FTs treeTs;
   777     val tree_maps = map4 mk_map_of_bnf Dss (replicate n bdTs) (replicate n treeTs) bnfs;
   778     val final_maps = map4 mk_map_of_bnf Dss (replicate n treeTs) (replicate n treeQTs) bnfs;
   779     val isNode_setss = mk_setss (passiveAs @ replicate n sbdT);
   780 
   781     val root = HOLogic.mk_set sum_sbd_listT [HOLogic.mk_list sum_sbdT []];
   782     val Zero = HOLogic.mk_tuple (map (fn U => absdummy U root) activeAs);
   783     val Lev_recT = fastype_of Zero;
   784 
   785     val Nil = HOLogic.mk_tuple (map3 (fn i => fn z => fn z'=>
   786       Term.absfree z' (mk_InN activeAs z i)) ks zs zs');
   787     val rv_recT = fastype_of Nil;
   788 
   789     val (((((((((((sumx, sumx'), (kks, kks')), (kl, kl')), (kl_copy, kl'_copy)), (Kl, Kl')),
   790       (lab, lab')), (Kl_lab, Kl_lab')), xs), (Lev_rec, Lev_rec')), (rv_rec, rv_rec')),
   791       names_lthy) = names_lthy
   792       |> yield_singleton (apfst (op ~~) oo mk_Frees' "sumx") sum_sbdT
   793       ||>> mk_Frees' "k" sbdTs
   794       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
   795       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
   796       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl") sum_sbd_list_setT
   797       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "lab") (sum_sbd_listT --> sbdFT)
   798       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl_lab") treeT
   799       ||>> mk_Frees "x" bdFTs
   800       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") Lev_recT
   801       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") rv_recT;
   802 
   803     val (k, k') = (hd kks, hd kks')
   804 
   805     val timer = time (timer "Bounds");
   806 
   807     (* tree coalgebra *)
   808 
   809     val isNode_binds = mk_internal_bs isNodeN;
   810     fun isNode_bind i = nth isNode_binds (i - 1);
   811     val isNode_def_bind = rpair [] o Thm.def_binding o isNode_bind;
   812 
   813     val isNodeT =
   814       Library.foldr (op -->) (map fastype_of [Kl, lab, kl], HOLogic.boolT);
   815 
   816     val Succs = map3 (fn i => fn k => fn k' =>
   817       HOLogic.mk_Collect (fst k', snd k', HOLogic.mk_mem (mk_InN sbdTs k i, mk_Succ Kl kl)))
   818       ks kks kks';
   819 
   820     fun isNode_spec sets x i =
   821       let
   822         val active_sets = drop m (map (fn set => set $ x) sets);
   823         val rhs = list_exists_free [x]
   824           (Library.foldr1 HOLogic.mk_conj (HOLogic.mk_eq (lab $ kl, mk_InN bdFTs x i) ::
   825           map2 (curry HOLogic.mk_eq) active_sets Succs));
   826       in
   827         fold_rev (Term.absfree o Term.dest_Free) [Kl, lab, kl] rhs
   828       end;
   829 
   830     val ((isNode_frees, (_, isNode_def_frees)), (lthy, lthy_old)) =
   831       lthy
   832       |> fold_map3 (fn i => fn x => fn sets => Local_Theory.define
   833         ((isNode_bind i, NoSyn), (isNode_def_bind i, isNode_spec sets x i)))
   834         ks xs isNode_setss
   835       |>> apsnd split_list o split_list
   836       ||> `Local_Theory.restore;
   837 
   838     val phi = Proof_Context.export_morphism lthy_old lthy;
   839 
   840     val isNode_defs = map (fn def =>
   841       mk_unabs_def 3 (Morphism.thm phi def RS meta_eq_to_obj_eq)) isNode_def_frees;
   842     val isNodes = map (fst o Term.dest_Const o Morphism.term phi) isNode_frees;
   843 
   844     fun mk_isNode kl i =
   845       Term.list_comb (Const (nth isNodes (i - 1), isNodeT), [Kl, lab, kl]);
   846 
   847     val isTree =
   848       let
   849         val empty = HOLogic.mk_mem (HOLogic.mk_list sum_sbdT [], Kl);
   850 
   851         val tree = mk_Ball Kl (Term.absfree kl'
   852           (Library.foldr1 HOLogic.mk_conj (map4 (fn Succ => fn i => fn k => fn k' =>
   853             mk_Ball Succ (Term.absfree k' (mk_isNode
   854               (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i])) i)))
   855           Succs ks kks kks')));
   856       in
   857         HOLogic.mk_conj (empty, tree)
   858       end;
   859 
   860     val carT_binds = mk_internal_bs carTN;
   861     fun carT_bind i = nth carT_binds (i - 1);
   862     val carT_def_bind = rpair [] o Thm.def_binding o carT_bind;
   863 
   864     fun carT_spec i =
   865       HOLogic.mk_Collect (fst Kl_lab', snd Kl_lab', list_exists_free [Kl, lab]
   866         (HOLogic.mk_conj (HOLogic.mk_eq (Kl_lab, HOLogic.mk_prod (Kl, lab)),
   867           HOLogic.mk_conj (isTree, mk_isNode (HOLogic.mk_list sum_sbdT []) i))));
   868 
   869     val ((carT_frees, (_, carT_def_frees)), (lthy, lthy_old)) =
   870       lthy
   871       |> fold_map (fn i =>
   872         Local_Theory.define ((carT_bind i, NoSyn), (carT_def_bind i, carT_spec i))) ks
   873       |>> apsnd split_list o split_list
   874       ||> `Local_Theory.restore;
   875 
   876     val phi = Proof_Context.export_morphism lthy_old lthy;
   877 
   878     val carT_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) carT_def_frees;
   879     val carTs = map (fst o Term.dest_Const o Morphism.term phi) carT_frees;
   880 
   881     fun mk_carT i = Const (nth carTs (i - 1), HOLogic.mk_setT treeT);
   882 
   883     val strT_binds = mk_internal_bs strTN;
   884     fun strT_bind i = nth strT_binds (i - 1);
   885     val strT_def_bind = rpair [] o Thm.def_binding o strT_bind;
   886 
   887     fun strT_spec mapFT FT i =
   888       let
   889         fun mk_f i k k' =
   890           let val in_k = mk_InN sbdTs k i;
   891           in Term.absfree k' (HOLogic.mk_prod (mk_Shift Kl in_k, mk_shift lab in_k)) end;
   892 
   893         val f = Term.list_comb (mapFT, passive_ids @ map3 mk_f ks kks kks');
   894         val (fTs1, fTs2) = apsnd tl (chop (i - 1) (map (fn T => T --> FT) bdFTs));
   895         val fs = map mk_undefined fTs1 @ (f :: map mk_undefined fTs2);
   896       in
   897         HOLogic.mk_split (Term.absfree Kl' (Term.absfree lab'
   898           (mk_case_sumN fs $ (lab $ HOLogic.mk_list sum_sbdT []))))
   899       end;
   900 
   901     val ((strT_frees, (_, strT_def_frees)), (lthy, lthy_old)) =
   902       lthy
   903       |> fold_map3 (fn i => fn mapFT => fn FT => Local_Theory.define
   904         ((strT_bind i, NoSyn), (strT_def_bind i, strT_spec mapFT FT i)))
   905         ks tree_maps treeFTs
   906       |>> apsnd split_list o split_list
   907       ||> `Local_Theory.restore;
   908 
   909     val phi = Proof_Context.export_morphism lthy_old lthy;
   910 
   911     val strT_defs = map (fn def =>
   912         trans OF [Morphism.thm phi def RS meta_eq_to_obj_eq RS fun_cong, @{thm prod.case}])
   913       strT_def_frees;
   914     val strTs = map (fst o Term.dest_Const o Morphism.term phi) strT_frees;
   915 
   916     fun mk_strT FT i = Const (nth strTs (i - 1), treeT --> FT);
   917 
   918     val carTAs = map mk_carT ks;
   919     val strTAs = map2 mk_strT treeFTs ks;
   920 
   921     val coalgT_thm =
   922       Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop (mk_coalg carTAs strTAs))
   923         (fn {context = ctxt, prems = _} => mk_coalgT_tac ctxt m
   924           (coalg_def :: isNode_defs @ carT_defs) strT_defs set_mapss)
   925       |> Thm.close_derivation;
   926 
   927     val timer = time (timer "Tree coalgebra");
   928 
   929     fun mk_to_sbd s x i i' =
   930       mk_toCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
   931     fun mk_from_sbd s x i i' =
   932       mk_fromCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
   933 
   934     fun mk_to_sbd_thmss thm = map (map (fn set_sbd =>
   935       thm OF [set_sbd, sbd_Card_order]) o drop m) set_sbdss;
   936 
   937     val to_sbd_inj_thmss = mk_to_sbd_thmss @{thm toCard_inj};
   938     val from_to_sbd_thmss = mk_to_sbd_thmss @{thm fromCard_toCard};
   939 
   940     val Lev_bind = mk_internal_b LevN;
   941     val Lev_def_bind = rpair [] (Thm.def_binding Lev_bind);
   942 
   943     val Lev_spec =
   944       let
   945         fun mk_Suc i s setsAs a a' =
   946           let
   947             val sets = drop m setsAs;
   948             fun mk_set i' set b =
   949               let
   950                 val Cons = HOLogic.mk_eq (kl_copy,
   951                   mk_Cons (mk_InN sbdTs (mk_to_sbd s a i i' $ b) i') kl)
   952                 val b_set = HOLogic.mk_mem (b, set $ (s $ a));
   953                 val kl_rec = HOLogic.mk_mem (kl, mk_nthN n Lev_rec i' $ b);
   954               in
   955                 HOLogic.mk_Collect (fst kl'_copy, snd kl'_copy, list_exists_free [b, kl]
   956                   (HOLogic.mk_conj (Cons, HOLogic.mk_conj (b_set, kl_rec))))
   957               end;
   958           in
   959             Term.absfree a' (Library.foldl1 mk_union (map3 mk_set ks sets zs_copy))
   960           end;
   961 
   962         val Suc = Term.absdummy HOLogic.natT (Term.absfree Lev_rec'
   963           (HOLogic.mk_tuple (map5 mk_Suc ks ss setssAs zs zs')));
   964 
   965         val rhs = mk_rec_nat Zero Suc;
   966       in
   967         fold_rev (Term.absfree o Term.dest_Free) ss rhs
   968       end;
   969 
   970     val ((Lev_free, (_, Lev_def_free)), (lthy, lthy_old)) =
   971       lthy
   972       |> Local_Theory.define ((Lev_bind, NoSyn), (Lev_def_bind, Lev_spec))
   973       ||> `Local_Theory.restore;
   974 
   975     val phi = Proof_Context.export_morphism lthy_old lthy;
   976 
   977     val Lev_def = mk_unabs_def n (Morphism.thm phi Lev_def_free RS meta_eq_to_obj_eq);
   978     val Lev = fst (Term.dest_Const (Morphism.term phi Lev_free));
   979 
   980     fun mk_Lev ss nat i =
   981       let
   982         val Ts = map fastype_of ss;
   983         val LevT = Library.foldr (op -->) (Ts, HOLogic.natT -->
   984           HOLogic.mk_tupleT (map (fn U => domain_type U --> sum_sbd_list_setT) Ts));
   985       in
   986         mk_nthN n (Term.list_comb (Const (Lev, LevT), ss) $ nat) i
   987       end;
   988 
   989     val Lev_0s = flat (mk_rec_simps n @{thm rec_nat_0_imp} [Lev_def]);
   990     val Lev_Sucs = flat (mk_rec_simps n @{thm rec_nat_Suc_imp} [Lev_def]);
   991 
   992     val rv_bind = mk_internal_b rvN;
   993     val rv_def_bind = rpair [] (Thm.def_binding rv_bind);
   994 
   995     val rv_spec =
   996       let
   997         fun mk_Cons i s b b' =
   998           let
   999             fun mk_case i' =
  1000               Term.absfree k' (mk_nthN n rv_rec i' $ (mk_from_sbd s b i i' $ k));
  1001           in
  1002             Term.absfree b' (mk_case_sumN (map mk_case ks) $ sumx)
  1003           end;
  1004 
  1005         val Cons = Term.absfree sumx' (Term.absdummy sum_sbd_listT (Term.absfree rv_rec'
  1006           (HOLogic.mk_tuple (map4 mk_Cons ks ss zs zs'))));
  1007 
  1008         val rhs = mk_rec_list Nil Cons;
  1009       in
  1010         fold_rev (Term.absfree o Term.dest_Free) ss rhs
  1011       end;
  1012 
  1013     val ((rv_free, (_, rv_def_free)), (lthy, lthy_old)) =
  1014       lthy
  1015       |> Local_Theory.define ((rv_bind, NoSyn), (rv_def_bind, rv_spec))
  1016       ||> `Local_Theory.restore;
  1017 
  1018     val phi = Proof_Context.export_morphism lthy_old lthy;
  1019 
  1020     val rv_def = mk_unabs_def n (Morphism.thm phi rv_def_free RS meta_eq_to_obj_eq);
  1021     val rv = fst (Term.dest_Const (Morphism.term phi rv_free));
  1022 
  1023     fun mk_rv ss kl i =
  1024       let
  1025         val Ts = map fastype_of ss;
  1026         val As = map domain_type Ts;
  1027         val rvT = Library.foldr (op -->) (Ts, fastype_of kl -->
  1028           HOLogic.mk_tupleT (map (fn U => U --> mk_sumTN As) As));
  1029       in
  1030         mk_nthN n (Term.list_comb (Const (rv, rvT), ss) $ kl) i
  1031       end;
  1032 
  1033     val rv_Nils = flat (mk_rec_simps n @{thm rec_list_Nil_imp} [rv_def]);
  1034     val rv_Conss = flat (mk_rec_simps n @{thm rec_list_Cons_imp} [rv_def]);
  1035 
  1036     val beh_binds = mk_internal_bs behN;
  1037     fun beh_bind i = nth beh_binds (i - 1);
  1038     val beh_def_bind = rpair [] o Thm.def_binding o beh_bind;
  1039 
  1040     fun beh_spec i z =
  1041       let
  1042         fun mk_case i to_sbd_map s k k' =
  1043           Term.absfree k' (mk_InN bdFTs
  1044             (Term.list_comb (to_sbd_map, passive_ids @ map (mk_to_sbd s k i) ks) $ (s $ k)) i);
  1045 
  1046         val Lab = Term.absfree kl'
  1047           (mk_case_sumN (map5 mk_case ks to_sbd_maps ss zs zs') $ (mk_rv ss kl i $ z));
  1048 
  1049         val rhs = HOLogic.mk_prod (mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
  1050           (Term.absfree nat' (mk_Lev ss nat i $ z)), Lab);
  1051       in
  1052         fold_rev (Term.absfree o Term.dest_Free) (ss @ [z]) rhs
  1053       end;
  1054 
  1055     val ((beh_frees, (_, beh_def_frees)), (lthy, lthy_old)) =
  1056       lthy
  1057       |> fold_map2 (fn i => fn z =>
  1058         Local_Theory.define ((beh_bind i, NoSyn), (beh_def_bind i, beh_spec i z))) ks zs
  1059       |>> apsnd split_list o split_list
  1060       ||> `Local_Theory.restore;
  1061 
  1062     val phi = Proof_Context.export_morphism lthy_old lthy;
  1063 
  1064     val beh_defs = map (fn def =>
  1065       mk_unabs_def (n + 1) (Morphism.thm phi def RS meta_eq_to_obj_eq)) beh_def_frees;
  1066     val behs = map (fst o Term.dest_Const o Morphism.term phi) beh_frees;
  1067 
  1068     fun mk_beh ss i =
  1069       let
  1070         val Ts = map fastype_of ss;
  1071         val behT = Library.foldr (op -->) (Ts, nth activeAs (i - 1) --> treeT);
  1072       in
  1073         Term.list_comb (Const (nth behs (i - 1), behT), ss)
  1074       end;
  1075 
  1076     val (length_Lev_thms, length_Lev'_thms) =
  1077       let
  1078         fun mk_conjunct i z = HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1079           HOLogic.mk_eq (mk_size kl, nat));
  1080         val goal = list_all_free (kl :: zs)
  1081           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1082 
  1083         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1084 
  1085         val length_Lev =
  1086           Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  1087             (K (mk_length_Lev_tac lthy cts Lev_0s Lev_Sucs))
  1088           |> Thm.close_derivation
  1089           |> singleton (Proof_Context.export names_lthy lthy);
  1090 
  1091         val length_Lev' = mk_specN (n + 1) length_Lev;
  1092         val length_Levs = map (fn i => length_Lev' RS mk_conjunctN n i RS mp) ks;
  1093 
  1094         fun mk_goal i z = Logic.mk_implies
  1095             (mk_Trueprop_mem (kl, mk_Lev ss nat i $ z),
  1096             mk_Trueprop_mem (kl, mk_Lev ss (mk_size kl) i $ z));
  1097         val goals = map2 mk_goal ks zs;
  1098 
  1099         val length_Levs' = map2 (fn goal => fn length_Lev =>
  1100           Goal.prove_sorry lthy [] [] goal (K (mk_length_Lev'_tac length_Lev))
  1101           |> Thm.close_derivation
  1102           |> singleton (Proof_Context.export names_lthy lthy)) goals length_Levs;
  1103       in
  1104         (length_Levs, length_Levs')
  1105       end;
  1106 
  1107     val rv_last_thmss =
  1108       let
  1109         fun mk_conjunct i z i' z_copy = list_exists_free [z_copy]
  1110           (HOLogic.mk_eq
  1111             (mk_rv ss (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i'])) i $ z,
  1112             mk_InN activeAs z_copy i'));
  1113         val goal = list_all_free (k :: zs)
  1114           (Library.foldr1 HOLogic.mk_conj (map2 (fn i => fn z =>
  1115             Library.foldr1 HOLogic.mk_conj
  1116               (map2 (mk_conjunct i z) ks zs_copy)) ks zs));
  1117 
  1118         val cTs = [SOME (certifyT lthy sum_sbdT)];
  1119         val cts = map (SOME o certify lthy) [Term.absfree kl' goal, kl];
  1120 
  1121         val rv_last =
  1122           Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  1123             (K (mk_rv_last_tac cTs cts rv_Nils rv_Conss))
  1124           |> Thm.close_derivation
  1125           |> singleton (Proof_Context.export names_lthy lthy);
  1126 
  1127         val rv_last' = mk_specN (n + 1) rv_last;
  1128       in
  1129         map (fn i => map (fn i' => rv_last' RS mk_conjunctN n i RS mk_conjunctN n i') ks) ks
  1130       end;
  1131 
  1132     val set_Lev_thmsss =
  1133       let
  1134         fun mk_conjunct i z =
  1135           let
  1136             fun mk_conjunct' i' sets s z' =
  1137               let
  1138                 fun mk_conjunct'' i'' set z'' = HOLogic.mk_imp
  1139                   (HOLogic.mk_mem (z'', set $ (s $ z')),
  1140                     HOLogic.mk_mem (mk_append (kl,
  1141                       HOLogic.mk_list sum_sbdT [mk_InN sbdTs (mk_to_sbd s z' i' i'' $ z'') i'']),
  1142                       mk_Lev ss (HOLogic.mk_Suc nat) i $ z));
  1143               in
  1144                 HOLogic.mk_imp (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z' i'),
  1145                   (Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct'' ks (drop m sets) zs_copy2)))
  1146               end;
  1147           in
  1148             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1149               Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct' ks setssAs ss zs_copy))
  1150           end;
  1151 
  1152         val goal = list_all_free (kl :: zs @ zs_copy @ zs_copy2)
  1153           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1154 
  1155         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1156 
  1157         val set_Lev =
  1158           Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  1159             (K (mk_set_Lev_tac lthy cts Lev_0s Lev_Sucs rv_Nils rv_Conss from_to_sbd_thmss))
  1160           |> Thm.close_derivation
  1161           |> singleton (Proof_Context.export names_lthy lthy);
  1162 
  1163         val set_Lev' = mk_specN (3 * n + 1) set_Lev;
  1164       in
  1165         map (fn i => map (fn i' => map (fn i'' => set_Lev' RS
  1166           mk_conjunctN n i RS mp RS
  1167           mk_conjunctN n i' RS mp RS
  1168           mk_conjunctN n i'' RS mp) ks) ks) ks
  1169       end;
  1170 
  1171     val set_image_Lev_thmsss =
  1172       let
  1173         fun mk_conjunct i z =
  1174           let
  1175             fun mk_conjunct' i' sets =
  1176               let
  1177                 fun mk_conjunct'' i'' set s z'' = HOLogic.mk_imp
  1178                   (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z'' i''),
  1179                   HOLogic.mk_mem (k, mk_image (mk_to_sbd s z'' i'' i') $ (set $ (s $ z''))));
  1180               in
  1181                 HOLogic.mk_imp (HOLogic.mk_mem
  1182                   (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i']),
  1183                     mk_Lev ss (HOLogic.mk_Suc nat) i $ z),
  1184                   (Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct'' ks sets ss zs_copy)))
  1185               end;
  1186           in
  1187             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1188               Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct' ks (drop m setssAs')))
  1189           end;
  1190 
  1191         val goal = list_all_free (kl :: k :: zs @ zs_copy)
  1192           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1193 
  1194         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1195 
  1196         val set_image_Lev =
  1197           Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  1198             (K (mk_set_image_Lev_tac lthy cts Lev_0s Lev_Sucs rv_Nils rv_Conss
  1199               from_to_sbd_thmss to_sbd_inj_thmss))
  1200           |> Thm.close_derivation
  1201           |> singleton (Proof_Context.export names_lthy lthy);
  1202 
  1203         val set_image_Lev' = mk_specN (2 * n + 2) set_image_Lev;
  1204       in
  1205         map (fn i => map (fn i' => map (fn i'' => set_image_Lev' RS
  1206           mk_conjunctN n i RS mp RS
  1207           mk_conjunctN n i'' RS mp RS
  1208           mk_conjunctN n i' RS mp) ks) ks) ks
  1209       end;
  1210 
  1211     val mor_beh_thm =
  1212       Goal.prove_sorry lthy [] []
  1213         (HOLogic.mk_Trueprop (mk_mor active_UNIVs ss carTAs strTAs (map (mk_beh ss) ks)))
  1214         (fn {context = ctxt, prems = _} => mk_mor_beh_tac ctxt m mor_def mor_cong_thm
  1215           beh_defs carT_defs strT_defs isNode_defs
  1216           to_sbd_inj_thmss from_to_sbd_thmss Lev_0s Lev_Sucs rv_Nils rv_Conss
  1217           length_Lev_thms length_Lev'_thms rv_last_thmss set_Lev_thmsss
  1218           set_image_Lev_thmsss set_mapss map_comp_id_thms map_cong0s)
  1219       |> Thm.close_derivation
  1220       |> singleton (Proof_Context.export names_lthy lthy);
  1221 
  1222     val timer = time (timer "Behavioral morphism");
  1223 
  1224     val lsbisAs = map (mk_lsbis carTAs strTAs) ks;
  1225 
  1226     fun mk_str_final i =
  1227       mk_univ (HOLogic.mk_comp (Term.list_comb (nth final_maps (i - 1),
  1228         passive_ids @ map mk_proj lsbisAs), nth strTAs (i - 1)));
  1229 
  1230     val car_finals = map2 mk_quotient carTAs lsbisAs;
  1231     val str_finals = map mk_str_final ks;
  1232 
  1233     val coalgT_set_thmss = map (map (fn thm => coalgT_thm RS thm)) coalg_set_thmss;
  1234     val equiv_LSBIS_thms = map (fn thm => coalgT_thm RS thm) equiv_lsbis_thms;
  1235 
  1236     val congruent_str_final_thms =
  1237       let
  1238         fun mk_goal R final_map strT =
  1239           HOLogic.mk_Trueprop (mk_congruent R (HOLogic.mk_comp
  1240             (Term.list_comb (final_map, passive_ids @ map mk_proj lsbisAs), strT)));
  1241 
  1242         val goals = map3 mk_goal lsbisAs final_maps strTAs;
  1243       in
  1244         map4 (fn goal => fn lsbisE => fn map_comp_id => fn map_cong0 =>
  1245           Goal.prove_sorry lthy [] [] goal
  1246             (K (mk_congruent_str_final_tac m lsbisE map_comp_id map_cong0 equiv_LSBIS_thms))
  1247           |> Thm.close_derivation)
  1248         goals lsbisE_thms map_comp_id_thms map_cong0s
  1249       end;
  1250 
  1251     val coalg_final_thm = Goal.prove_sorry lthy [] []
  1252       (HOLogic.mk_Trueprop (mk_coalg car_finals str_finals))
  1253       (K (mk_coalg_final_tac m coalg_def congruent_str_final_thms equiv_LSBIS_thms
  1254         set_mapss coalgT_set_thmss))
  1255       |> Thm.close_derivation;
  1256 
  1257     val mor_T_final_thm = Goal.prove_sorry lthy [] []
  1258       (HOLogic.mk_Trueprop (mk_mor carTAs strTAs car_finals str_finals (map mk_proj lsbisAs)))
  1259       (K (mk_mor_T_final_tac mor_def congruent_str_final_thms equiv_LSBIS_thms))
  1260       |> Thm.close_derivation;
  1261 
  1262     val mor_final_thm = mor_comp_thm OF [mor_beh_thm, mor_T_final_thm];
  1263     val in_car_final_thms = map (fn thm => thm OF [mor_final_thm, UNIV_I]) mor_image'_thms;
  1264 
  1265     val timer = time (timer "Final coalgebra");
  1266 
  1267     val ((T_names, (T_glob_infos, T_loc_infos)), lthy) =
  1268       lthy
  1269       |> fold_map4 (fn b => fn mx => fn car_final => fn in_car_final =>
  1270         typedef (b, params, mx) car_final NONE
  1271           (EVERY' [rtac exI, rtac in_car_final] 1)) bs mixfixes car_finals in_car_final_thms
  1272       |>> apsnd split_list o split_list;
  1273 
  1274     val Ts = map (fn name => Type (name, params')) T_names;
  1275     fun mk_Ts passive = map (Term.typ_subst_atomic (passiveAs ~~ passive)) Ts;
  1276     val Ts' = mk_Ts passiveBs;
  1277     val Rep_Ts = map2 (fn info => fn T => Const (#Rep_name info, T --> treeQT)) T_glob_infos Ts;
  1278     val Abs_Ts = map2 (fn info => fn T => Const (#Abs_name info, treeQT --> T)) T_glob_infos Ts;
  1279 
  1280     val Reps = map #Rep T_loc_infos;
  1281     val Rep_injects = map #Rep_inject T_loc_infos;
  1282     val Abs_inverses = map #Abs_inverse T_loc_infos;
  1283 
  1284     val timer = time (timer "THE TYPEDEFs & Rep/Abs thms");
  1285 
  1286     val UNIVs = map HOLogic.mk_UNIV Ts;
  1287     val FTs = mk_FTs (passiveAs @ Ts);
  1288     val FTs_setss = mk_setss (passiveAs @ Ts);
  1289     val map_FTs = map2 (fn Ds => mk_map_of_bnf Ds treeQTs (passiveAs @ Ts)) Dss bnfs;
  1290     val unfold_fTs = map2 (curry op -->) activeAs Ts;
  1291     val corec_sTs = map (Term.typ_subst_atomic (activeBs ~~ Ts)) sum_sTs;
  1292     val corec_maps = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls;
  1293     val corec_maps_rev = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls_rev;
  1294     val corec_Inls = map (Term.subst_atomic_types (activeBs ~~ Ts)) Inls;
  1295     val corec_UNIVs = map2 (HOLogic.mk_UNIV oo curry mk_sumT) Ts activeAs;
  1296 
  1297     val emptys = map (fn T => HOLogic.mk_set T []) passiveAs;
  1298     val Zeros = map (fn empty =>
  1299      HOLogic.mk_tuple (map (fn U => absdummy U empty) Ts)) emptys;
  1300     val hrecTs = map fastype_of Zeros;
  1301 
  1302     val (((((((((((((Jzs, Jzs'), Jz's), Jzs_copy), Jz's_copy), Jzs1), Jzs2),
  1303       TRs), unfold_fs), corec_ss), phis), dtor_set_induct_phiss), (hrecs, hrecs')),
  1304       names_lthy) = names_lthy
  1305       |> mk_Frees' "z" Ts
  1306       ||>> mk_Frees "y" Ts'
  1307       ||>> mk_Frees "z'" Ts
  1308       ||>> mk_Frees "y'" Ts'
  1309       ||>> mk_Frees "z1" Ts
  1310       ||>> mk_Frees "z2" Ts
  1311       ||>> mk_Frees "r" (map (mk_relT o `I) Ts)
  1312       ||>> mk_Frees "f" unfold_fTs
  1313       ||>> mk_Frees "s" corec_sTs
  1314       ||>> mk_Frees "P" (map2 mk_pred2T Ts Ts)
  1315       ||>> mk_Freess "P" (map (fn A => map (mk_pred2T A) Ts) passiveAs)
  1316       ||>> mk_Frees' "rec" hrecTs;
  1317 
  1318     fun dtor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (dtorN ^ "_");
  1319     val dtor_def_bind = rpair [] o Binding.conceal o Thm.def_binding o dtor_bind;
  1320 
  1321     fun dtor_spec rep str map_FT Jz Jz' =
  1322       Term.absfree Jz'
  1323         (Term.list_comb (map_FT, map HOLogic.id_const passiveAs @ Abs_Ts) $ (str $ (rep $ Jz)));
  1324 
  1325     val ((dtor_frees, (_, dtor_def_frees)), (lthy, lthy_old)) =
  1326       lthy
  1327       |> fold_map6 (fn i => fn rep => fn str => fn mapx => fn Jz => fn Jz' =>
  1328         Local_Theory.define ((dtor_bind i, NoSyn),
  1329           (dtor_def_bind i, dtor_spec rep str mapx Jz Jz')))
  1330         ks Rep_Ts str_finals map_FTs Jzs Jzs'
  1331       |>> apsnd split_list o split_list
  1332       ||> `Local_Theory.restore;
  1333 
  1334     val phi = Proof_Context.export_morphism lthy_old lthy;
  1335     fun mk_dtors passive =
  1336       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ (mk_params passive)) o
  1337         Morphism.term phi) dtor_frees;
  1338     val dtors = mk_dtors passiveAs;
  1339     val dtor's = mk_dtors passiveBs;
  1340     val dtor_defs = map (fn def =>
  1341       Morphism.thm phi def RS meta_eq_to_obj_eq RS fun_cong) dtor_def_frees;
  1342 
  1343     val coalg_final_set_thmss = map (map (fn thm => coalg_final_thm RS thm)) coalg_set_thmss;
  1344     val (mor_Rep_thm, mor_Abs_thm) =
  1345       let
  1346         val mor_Rep =
  1347           Goal.prove_sorry lthy [] []
  1348             (HOLogic.mk_Trueprop (mk_mor UNIVs dtors car_finals str_finals Rep_Ts))
  1349             (fn {context = ctxt, prems = _} => mk_mor_Rep_tac ctxt (mor_def :: dtor_defs) Reps
  1350               Abs_inverses coalg_final_set_thmss map_comp_id_thms map_cong0L_thms)
  1351           |> Thm.close_derivation;
  1352 
  1353         val mor_Abs =
  1354           Goal.prove_sorry lthy [] []
  1355             (HOLogic.mk_Trueprop (mk_mor car_finals str_finals UNIVs dtors Abs_Ts))
  1356             (fn {context = ctxt, prems = _} => mk_mor_Abs_tac ctxt (mor_def :: dtor_defs)
  1357               Abs_inverses)
  1358           |> Thm.close_derivation;
  1359       in
  1360         (mor_Rep, mor_Abs)
  1361       end;
  1362 
  1363     val timer = time (timer "dtor definitions & thms");
  1364 
  1365     fun unfold_bind i = nth external_bs (i - 1) |> Binding.prefix_name (dtor_unfoldN ^ "_");
  1366     val unfold_def_bind = rpair [] o Binding.conceal o Thm.def_binding o unfold_bind;
  1367 
  1368     fun unfold_spec abs f z = fold_rev (Term.absfree o Term.dest_Free) (ss @ [z]) (abs $ (f $ z));
  1369 
  1370     val ((unfold_frees, (_, unfold_def_frees)), (lthy, lthy_old)) =
  1371       lthy
  1372       |> fold_map4 (fn i => fn abs => fn f => fn z =>
  1373         Local_Theory.define ((unfold_bind i, NoSyn), (unfold_def_bind i, unfold_spec abs f z)))
  1374           ks Abs_Ts (map (fn i => HOLogic.mk_comp
  1375             (mk_proj (nth lsbisAs (i - 1)), mk_beh ss i)) ks) zs
  1376       |>> apsnd split_list o split_list
  1377       ||> `Local_Theory.restore;
  1378 
  1379     val phi = Proof_Context.export_morphism lthy_old lthy;
  1380     val unfolds = map (Morphism.term phi) unfold_frees;
  1381     val unfold_names = map (fst o dest_Const) unfolds;
  1382     fun mk_unfolds passives actives =
  1383       map3 (fn name => fn T => fn active =>
  1384         Const (name, Library.foldr (op -->)
  1385           (map2 (curry op -->) actives (mk_FTs (passives @ actives)), active --> T)))
  1386       unfold_names (mk_Ts passives) actives;
  1387     fun mk_unfold Ts ss i = Term.list_comb (Const (nth unfold_names (i - 1), Library.foldr (op -->)
  1388       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  1389     val unfold_defs = map (fn def =>
  1390       mk_unabs_def (n + 1) (Morphism.thm phi def RS meta_eq_to_obj_eq)) unfold_def_frees;
  1391 
  1392     val mor_unfold_thm =
  1393       let
  1394         val Abs_inverses' = map2 (curry op RS) in_car_final_thms Abs_inverses;
  1395         val morEs' = map (fn thm => (thm OF [mor_final_thm, UNIV_I]) RS sym) morE_thms;
  1396       in
  1397         Goal.prove_sorry lthy [] []
  1398           (HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs dtors (map (mk_unfold Ts ss) ks)))
  1399           (K (mk_mor_unfold_tac m mor_UNIV_thm dtor_defs unfold_defs Abs_inverses' morEs'
  1400             map_comp_id_thms map_cong0s))
  1401         |> Thm.close_derivation
  1402         |> singleton (Proof_Context.export names_lthy lthy)
  1403       end;
  1404     val dtor_unfold_thms = map (fn thm => (thm OF [mor_unfold_thm, UNIV_I]) RS sym) morE_thms;
  1405 
  1406     val (raw_coind_thms, raw_coind_thm) =
  1407       let
  1408         val prem = HOLogic.mk_Trueprop (mk_sbis UNIVs dtors TRs);
  1409         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1410           (map2 (fn R => fn T => mk_leq R (Id_const T)) TRs Ts));
  1411       in
  1412         `split_conj_thm (Goal.prove_sorry lthy [] [] (Logic.mk_implies (prem, concl))
  1413           (K (mk_raw_coind_tac bis_def bis_cong_thm bis_O_thm bis_converse_thm bis_Gr_thm
  1414             tcoalg_thm coalgT_thm mor_T_final_thm sbis_lsbis_thm
  1415             lsbis_incl_thms incl_lsbis_thms equiv_LSBIS_thms mor_Rep_thm Rep_injects))
  1416           |> Thm.close_derivation
  1417           |> singleton (Proof_Context.export names_lthy lthy))
  1418       end;
  1419 
  1420     val (unfold_unique_mor_thms, unfold_unique_mor_thm) =
  1421       let
  1422         val prem = HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs dtors unfold_fs);
  1423         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_unfold Ts ss i);
  1424         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1425           (map2 mk_fun_eq unfold_fs ks));
  1426 
  1427         val bis_thm = tcoalg_thm RSN (2, tcoalg_thm RS bis_image2_thm);
  1428         val mor_thm = mor_comp_thm OF [mor_final_thm, mor_Abs_thm];
  1429 
  1430         val unique_mor = Goal.prove_sorry lthy [] [] (Logic.mk_implies (prem, unique))
  1431           (K (mk_unfold_unique_mor_tac raw_coind_thms bis_thm mor_thm unfold_defs))
  1432           |> Thm.close_derivation
  1433           |> singleton (Proof_Context.export names_lthy lthy);
  1434       in
  1435         `split_conj_thm unique_mor
  1436       end;
  1437 
  1438     val (dtor_unfold_unique_thms, dtor_unfold_unique_thm) = `split_conj_thm (split_conj_prems n
  1439       (mor_UNIV_thm RS iffD2 RS unfold_unique_mor_thm));
  1440 
  1441     val unfold_dtor_thms = map (fn thm => mor_id_thm RS thm RS sym) unfold_unique_mor_thms;
  1442 
  1443     val unfold_o_dtor_thms =
  1444       let
  1445         val mor = mor_comp_thm OF [mor_str_thm, mor_unfold_thm];
  1446       in
  1447         map2 (fn unique => fn unfold_ctor =>
  1448           trans OF [mor RS unique, unfold_ctor]) unfold_unique_mor_thms unfold_dtor_thms
  1449       end;
  1450 
  1451     val timer = time (timer "unfold definitions & thms");
  1452 
  1453     val map_dtors = map2 (fn Ds => fn bnf =>
  1454       Term.list_comb (mk_map_of_bnf Ds (passiveAs @ Ts) (passiveAs @ FTs) bnf,
  1455         map HOLogic.id_const passiveAs @ dtors)) Dss bnfs;
  1456 
  1457     fun ctor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctorN ^ "_");
  1458     val ctor_def_bind = rpair [] o Binding.conceal o Thm.def_binding o ctor_bind;
  1459 
  1460     fun ctor_spec i = mk_unfold Ts map_dtors i;
  1461 
  1462     val ((ctor_frees, (_, ctor_def_frees)), (lthy, lthy_old)) =
  1463       lthy
  1464       |> fold_map (fn i =>
  1465         Local_Theory.define ((ctor_bind i, NoSyn), (ctor_def_bind i, ctor_spec i))) ks
  1466       |>> apsnd split_list o split_list
  1467       ||> `Local_Theory.restore;
  1468 
  1469     val phi = Proof_Context.export_morphism lthy_old lthy;
  1470     fun mk_ctors params =
  1471       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ params) o Morphism.term phi)
  1472         ctor_frees;
  1473     val ctors = mk_ctors params';
  1474     val ctor_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) ctor_def_frees;
  1475 
  1476     val ctor_o_dtor_thms = map2 (fold_thms lthy o single) ctor_defs unfold_o_dtor_thms;
  1477 
  1478     val dtor_o_ctor_thms =
  1479       let
  1480         fun mk_goal dtor ctor FT =
  1481          mk_Trueprop_eq (HOLogic.mk_comp (dtor, ctor), HOLogic.id_const FT);
  1482         val goals = map3 mk_goal dtors ctors FTs;
  1483       in
  1484         map5 (fn goal => fn ctor_def => fn unfold => fn map_comp_id => fn map_cong0L =>
  1485           Goal.prove_sorry lthy [] [] goal
  1486             (fn {context = ctxt, prems = _} => mk_dtor_o_ctor_tac ctxt ctor_def unfold map_comp_id
  1487               map_cong0L unfold_o_dtor_thms)
  1488           |> Thm.close_derivation)
  1489           goals ctor_defs dtor_unfold_thms map_comp_id_thms map_cong0L_thms
  1490       end;
  1491 
  1492     val dtor_ctor_thms = map (fn thm => thm RS @{thm pointfree_idE}) dtor_o_ctor_thms;
  1493     val ctor_dtor_thms = map (fn thm => thm RS @{thm pointfree_idE}) ctor_o_dtor_thms;
  1494 
  1495     val bij_dtor_thms =
  1496       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) ctor_o_dtor_thms dtor_o_ctor_thms;
  1497     val inj_dtor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_dtor_thms;
  1498     val surj_dtor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_dtor_thms;
  1499     val dtor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_dtor_thms;
  1500     val dtor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_dtor_thms;
  1501     val dtor_exhaust_thms = map (fn thm => thm RS exE) dtor_nchotomy_thms;
  1502 
  1503     val bij_ctor_thms =
  1504       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) dtor_o_ctor_thms ctor_o_dtor_thms;
  1505     val inj_ctor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_ctor_thms;
  1506     val surj_ctor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_ctor_thms;
  1507     val ctor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_ctor_thms;
  1508     val ctor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_ctor_thms;
  1509     val ctor_exhaust_thms = map (fn thm => thm RS exE) ctor_nchotomy_thms;
  1510 
  1511     val timer = time (timer "ctor definitions & thms");
  1512 
  1513     val corec_Inl_sum_thms =
  1514       let
  1515         val mor = mor_comp_thm OF [mor_case_sum_thm, mor_unfold_thm];
  1516       in
  1517         map2 (fn unique => fn unfold_dtor =>
  1518           trans OF [mor RS unique, unfold_dtor]) unfold_unique_mor_thms unfold_dtor_thms
  1519       end;
  1520 
  1521     fun corec_bind i = nth external_bs (i - 1) |> Binding.prefix_name (dtor_corecN ^ "_");
  1522     val corec_def_bind = rpair [] o Binding.conceal o Thm.def_binding o corec_bind;
  1523 
  1524     val corec_strs =
  1525       map3 (fn dtor => fn sum_s => fn mapx =>
  1526         mk_case_sum
  1527           (HOLogic.mk_comp (Term.list_comb (mapx, passive_ids @ corec_Inls), dtor), sum_s))
  1528       dtors corec_ss corec_maps;
  1529 
  1530     fun corec_spec i T AT =
  1531       fold_rev (Term.absfree o Term.dest_Free) corec_ss
  1532         (HOLogic.mk_comp (mk_unfold Ts corec_strs i, Inr_const T AT));
  1533 
  1534     val ((corec_frees, (_, corec_def_frees)), (lthy, lthy_old)) =
  1535       lthy
  1536       |> fold_map3 (fn i => fn T => fn AT =>
  1537         Local_Theory.define ((corec_bind i, NoSyn), (corec_def_bind i, corec_spec i T AT)))
  1538           ks Ts activeAs
  1539       |>> apsnd split_list o split_list
  1540       ||> `Local_Theory.restore;
  1541 
  1542     val phi = Proof_Context.export_morphism lthy_old lthy;
  1543     val corecs = map (Morphism.term phi) corec_frees;
  1544     val corec_names = map (fst o dest_Const) corecs;
  1545     fun mk_corec ss i = Term.list_comb (Const (nth corec_names (i - 1), Library.foldr (op -->)
  1546       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  1547     val corec_defs = map (fn def =>
  1548       mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) corec_def_frees;
  1549 
  1550     val case_sums =
  1551       map2 (fn T => fn i => mk_case_sum (HOLogic.id_const T, mk_corec corec_ss i)) Ts ks;
  1552     val dtor_corec_thms =
  1553       let
  1554         fun mk_goal i corec_s corec_map dtor z =
  1555           let
  1556             val lhs = dtor $ (mk_corec corec_ss i $ z);
  1557             val rhs = Term.list_comb (corec_map, passive_ids @ case_sums) $ (corec_s $ z);
  1558           in
  1559             mk_Trueprop_eq (lhs, rhs)
  1560           end;
  1561         val goals = map5 mk_goal ks corec_ss corec_maps_rev dtors zs;
  1562       in
  1563         map3 (fn goal => fn unfold => fn map_cong0 =>
  1564           Goal.prove_sorry lthy [] [] goal
  1565             (fn {context = ctxt, prems = _} => mk_corec_tac ctxt m corec_defs unfold map_cong0
  1566               corec_Inl_sum_thms)
  1567           |> Thm.close_derivation
  1568           |> singleton (Proof_Context.export names_lthy lthy))
  1569         goals dtor_unfold_thms map_cong0s
  1570       end;
  1571 
  1572     val corec_unique_mor_thm =
  1573       let
  1574         val id_fs = map2 (fn T => fn f => mk_case_sum (HOLogic.id_const T, f)) Ts unfold_fs;
  1575         val prem = HOLogic.mk_Trueprop (mk_mor corec_UNIVs corec_strs UNIVs dtors id_fs);
  1576         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_corec corec_ss i);
  1577         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1578           (map2 mk_fun_eq unfold_fs ks));
  1579       in
  1580         Goal.prove_sorry lthy [] [] (Logic.mk_implies (prem, unique))
  1581           (fn {context = ctxt, prems = _} => mk_corec_unique_mor_tac ctxt corec_defs
  1582             corec_Inl_sum_thms unfold_unique_mor_thm)
  1583           |> Thm.close_derivation
  1584           |> singleton (Proof_Context.export names_lthy lthy)
  1585       end;
  1586 
  1587     val map_id0s_o_id =
  1588       map (fn thm =>
  1589         mk_trans (thm RS @{thm arg_cong2[of _ _ _ _ "op o", OF _ refl]}) @{thm id_comp})
  1590       map_id0s;
  1591 
  1592     val (dtor_corec_unique_thms, dtor_corec_unique_thm) =
  1593       `split_conj_thm (split_conj_prems n
  1594         (mor_UNIV_thm RS iffD2 RS corec_unique_mor_thm)
  1595         |> Local_Defs.unfold lthy (@{thms o_case_sum comp_id id_comp comp_assoc[symmetric]
  1596            case_sum_o_inj(1)} @ map_id0s_o_id @ sym_map_comps)
  1597         OF replicate n @{thm arg_cong2[of _ _ _ _ case_sum, OF refl]});
  1598 
  1599     val timer = time (timer "corec definitions & thms");
  1600 
  1601     val (coinduct_params, dtor_coinduct_thm) =
  1602       let
  1603         val rels = map (Term.subst_atomic_types ((activeAs ~~ Ts) @ (activeBs ~~ Ts))) relsAsBs;
  1604 
  1605         fun mk_concl phi z1 z2 = HOLogic.mk_imp (phi $ z1 $ z2, HOLogic.mk_eq (z1, z2));
  1606         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1607           (map3 mk_concl phis Jzs1 Jzs2));
  1608 
  1609         fun mk_rel_prem phi dtor rel Jz Jz_copy =
  1610           let
  1611             val concl = Term.list_comb (rel, passive_eqs @ phis) $
  1612               (dtor $ Jz) $ (dtor $ Jz_copy);
  1613           in
  1614             HOLogic.mk_Trueprop
  1615               (list_all_free [Jz, Jz_copy] (HOLogic.mk_imp (phi $ Jz $ Jz_copy, concl)))
  1616           end;
  1617 
  1618         val rel_prems = map5 mk_rel_prem phis dtors rels Jzs Jzs_copy;
  1619         val dtor_coinduct_goal = Logic.list_implies (rel_prems, concl);
  1620 
  1621         val dtor_coinduct =
  1622           Goal.prove_sorry lthy [] [] dtor_coinduct_goal
  1623             (K (mk_dtor_coinduct_tac m raw_coind_thm bis_rel_thm rel_congs))
  1624           |> Thm.close_derivation
  1625           |> singleton (Proof_Context.export names_lthy lthy);
  1626       in
  1627         (rev (Term.add_tfrees dtor_coinduct_goal []), dtor_coinduct)
  1628       end;
  1629 
  1630     val timer = time (timer "coinduction");
  1631 
  1632     fun mk_dtor_map_DEADID_thm dtor_inject map_id0 =
  1633       trans OF [iffD2 OF [dtor_inject, id_apply], map_id0 RS sym];
  1634 
  1635     fun mk_dtor_Jrel_DEADID_thm dtor_inject bnf =
  1636       trans OF [rel_eq_of_bnf bnf RS @{thm predicate2_eqD}, dtor_inject] RS sym;
  1637 
  1638     val JphiTs = map2 mk_pred2T passiveAs passiveBs;
  1639     val Jpsi1Ts = map2 mk_pred2T passiveAs passiveCs;
  1640     val Jpsi2Ts = map2 mk_pred2T passiveCs passiveBs;
  1641     val prodTsTs' = map2 (curry HOLogic.mk_prodT) Ts Ts';
  1642     val fstsTsTs' = map fst_const prodTsTs';
  1643     val sndsTsTs' = map snd_const prodTsTs';
  1644     val activephiTs = map2 mk_pred2T activeAs activeBs;
  1645     val activeJphiTs = map2 mk_pred2T Ts Ts';
  1646     val (((((Jphis, Jpsi1s), Jpsi2s), activephis), activeJphis), names_lthy) = names_lthy
  1647       |> mk_Frees "R" JphiTs
  1648       ||>> mk_Frees "R" Jpsi1Ts
  1649       ||>> mk_Frees "Q" Jpsi2Ts
  1650       ||>> mk_Frees "S" activephiTs
  1651       ||>> mk_Frees "JR" activeJphiTs;
  1652     val rels = map2 (fn Ds => mk_rel_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  1653 
  1654     fun mk_Jrel_DEADID_coinduct_thm () =
  1655       mk_rel_xtor_co_induct_thm Greatest_FP rels activeJphis (map HOLogic.eq_const Ts) Jphis
  1656         Jzs Jz's dtors dtor's (fn {context = ctxt, prems} =>
  1657           (unfold_thms_tac ctxt @{thms le_fun_def le_bool_def all_simps(1,2)[symmetric]} THEN
  1658           REPEAT_DETERM (rtac allI 1) THEN rtac (dtor_coinduct_thm OF prems) 1)) lthy;
  1659 
  1660     (*register new codatatypes as BNFs*)
  1661     val (timer, Jbnfs, (dtor_Jmap_o_thms, dtor_Jmap_thms), dtor_Jset_thmss',
  1662       dtor_Jrel_thms, Jrel_coinduct_thm, Jbnf_notes, dtor_Jset_induct_thms, lthy) =
  1663       if m = 0 then
  1664         (timer, replicate n DEADID_bnf,
  1665         map_split (`(mk_pointfree lthy)) (map2 mk_dtor_map_DEADID_thm dtor_inject_thms map_ids),
  1666         replicate n [], map2 mk_dtor_Jrel_DEADID_thm dtor_inject_thms bnfs,
  1667         mk_Jrel_DEADID_coinduct_thm (), [], [], lthy)
  1668       else let
  1669         val fTs = map2 (curry op -->) passiveAs passiveBs;
  1670         val gTs = map2 (curry op -->) passiveBs passiveCs;
  1671         val uTs = map2 (curry op -->) Ts Ts';
  1672 
  1673         val (((((((((fs, fs'), fs_copy), gs), us), (Jys, Jys')), (Jys_copy, Jys'_copy)),
  1674           (ys_copy, ys'_copy)), Kss), names_lthy) = names_lthy
  1675           |> mk_Frees' "f" fTs
  1676           ||>> mk_Frees "f" fTs
  1677           ||>> mk_Frees "g" gTs
  1678           ||>> mk_Frees "u" uTs
  1679           ||>> mk_Frees' "b" Ts'
  1680           ||>> mk_Frees' "b" Ts'
  1681           ||>> mk_Frees' "y" passiveAs
  1682           ||>> mk_Freess "K" (map (fn AT => map (fn T => T --> AT) Ts) ATs);;
  1683 
  1684         val map_FTFT's = map2 (fn Ds =>
  1685           mk_map_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  1686 
  1687         fun mk_maps ATs BTs Ts mk_T =
  1688           map2 (fn Ds => mk_map_of_bnf Ds (ATs @ Ts) (BTs @ map mk_T Ts)) Dss bnfs;
  1689         fun mk_Fmap mk_const fs Ts Fmap = Term.list_comb (Fmap, fs @ map mk_const Ts);
  1690         fun mk_map mk_const mk_T Ts fs Ts' dtors mk_maps =
  1691           mk_unfold Ts' (map2 (fn dtor => fn Fmap =>
  1692             HOLogic.mk_comp (mk_Fmap mk_const fs Ts Fmap, dtor)) dtors (mk_maps Ts mk_T));
  1693         val mk_map_id = mk_map HOLogic.id_const I;
  1694         val mk_mapsAB = mk_maps passiveAs passiveBs;
  1695         val fs_maps = map (mk_map_id Ts fs Ts' dtors mk_mapsAB) ks;
  1696 
  1697         val set_bss =
  1698           map (flat o map2 (fn B => fn b =>
  1699             if member (op =) resDs (TFree B) then [] else [b]) resBs) set_bss0;
  1700 
  1701         fun col_bind j = mk_internal_b (colN ^ (if m = 1 then "" else string_of_int j));
  1702         val col_def_bind = rpair [] o Thm.def_binding o col_bind;
  1703 
  1704         fun col_spec j Zero hrec hrec' =
  1705           let
  1706             fun mk_Suc dtor sets z z' =
  1707               let
  1708                 val (set, sets) = apfst (fn xs => nth xs (j - 1)) (chop m sets);
  1709                 fun mk_UN set k = mk_UNION (set $ (dtor $ z)) (mk_nthN n hrec k);
  1710               in
  1711                 Term.absfree z'
  1712                   (mk_union (set $ (dtor $ z), Library.foldl1 mk_union (map2 mk_UN sets ks)))
  1713               end;
  1714 
  1715             val Suc = Term.absdummy HOLogic.natT (Term.absfree hrec'
  1716               (HOLogic.mk_tuple (map4 mk_Suc dtors FTs_setss Jzs Jzs')));
  1717           in
  1718             mk_rec_nat Zero Suc
  1719           end;
  1720 
  1721         val ((col_frees, (_, col_def_frees)), (lthy, lthy_old)) =
  1722           lthy
  1723           |> fold_map4 (fn j => fn Zero => fn hrec => fn hrec' => Local_Theory.define
  1724             ((col_bind j, NoSyn), (col_def_bind j, col_spec j Zero hrec hrec')))
  1725             ls Zeros hrecs hrecs'
  1726           |>> apsnd split_list o split_list
  1727           ||> `Local_Theory.restore;
  1728 
  1729         val phi = Proof_Context.export_morphism lthy_old lthy;
  1730 
  1731         val col_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) col_def_frees;
  1732         val cols = map (fst o Term.dest_Const o Morphism.term phi) col_frees;
  1733 
  1734         fun mk_col Ts nat i j T =
  1735           let
  1736             val hrecT = HOLogic.mk_tupleT (map (fn U => U --> HOLogic.mk_setT T) Ts)
  1737             val colT = HOLogic.natT --> hrecT;
  1738           in
  1739             mk_nthN n (Term.list_comb (Const (nth cols (j - 1), colT), [nat])) i
  1740           end;
  1741 
  1742         val col_0ss = mk_rec_simps n @{thm rec_nat_0_imp} col_defs;
  1743         val col_Sucss = mk_rec_simps n @{thm rec_nat_Suc_imp} col_defs;
  1744         val col_0ss' = transpose col_0ss;
  1745         val col_Sucss' = transpose col_Sucss;
  1746 
  1747         fun mk_set Ts i j T =
  1748           Abs (Name.uu, nth Ts (i - 1), mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
  1749             (Term.absfree nat' (mk_col Ts nat i j T $ Bound 1)));
  1750 
  1751         val setss = map (fn i => map2 (mk_set Ts i) ls passiveAs) ks;
  1752 
  1753         val (Jbnf_consts, lthy) =
  1754           fold_map7 (fn b => fn map_b => fn rel_b => fn set_bs => fn mapx => fn sets => fn T =>
  1755               fn lthy =>
  1756             define_bnf_consts Hardly_Inline (user_policy Note_Some lthy) false (SOME deads)
  1757               map_b rel_b set_bs
  1758               ((((((b, T), fold_rev Term.absfree fs' mapx), sets), sbd),
  1759                 [Const (@{const_name undefined}, T)]), NONE) lthy)
  1760           bs map_bs rel_bs set_bss fs_maps setss Ts lthy;
  1761 
  1762         val (_, Jconsts, Jconst_defs, mk_Jconsts) = split_list4 Jbnf_consts;
  1763         val (_, Jsetss, Jbds_Ds, _, _) = split_list5 Jconsts;
  1764         val (Jmap_defs, Jset_defss, Jbd_defs, _, Jrel_defs) = split_list5 Jconst_defs;
  1765         val (mk_Jmaps_Ds, mk_Jt_Ds, _, mk_Jrels_Ds, _) = split_list5 mk_Jconsts;
  1766 
  1767         val Jrel_unabs_defs = map (fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) Jrel_defs;
  1768         val Jset_defs = flat Jset_defss;
  1769 
  1770         fun mk_Jmaps As Bs = map (fn mk => mk deads As Bs) mk_Jmaps_Ds;
  1771         fun mk_Jsetss As = map2 (fn mk => fn Jsets => map (mk deads As) Jsets) mk_Jt_Ds Jsetss;
  1772         val Jbds = map2 (fn mk => mk deads passiveAs) mk_Jt_Ds Jbds_Ds;
  1773         fun mk_Jrels As Bs = map (fn mk => mk deads As Bs) mk_Jrels_Ds;
  1774 
  1775         val Jmaps = mk_Jmaps passiveAs passiveBs;
  1776         val fs_Jmaps = map (fn m => Term.list_comb (m, fs)) Jmaps;
  1777         val fs_copy_Jmaps = map (fn m => Term.list_comb (m, fs_copy)) Jmaps;
  1778         val gs_Jmaps = map (fn m => Term.list_comb (m, gs)) (mk_Jmaps passiveBs passiveCs);
  1779         val fgs_Jmaps = map (fn m => Term.list_comb (m, map2 (curry HOLogic.mk_comp) gs fs))
  1780           (mk_Jmaps passiveAs passiveCs);
  1781         val (Jsetss_by_range, Jsetss_by_bnf) = `transpose (mk_Jsetss passiveAs);
  1782 
  1783         val timer = time (timer "bnf constants for the new datatypes");
  1784 
  1785         val (dtor_Jmap_thms, Jmap_thms) =
  1786           let
  1787             fun mk_goal fs_Jmap map dtor dtor' = mk_Trueprop_eq (HOLogic.mk_comp (dtor', fs_Jmap),
  1788               HOLogic.mk_comp (Term.list_comb (map, fs @ fs_Jmaps), dtor));
  1789             val goals = map4 mk_goal fs_Jmaps map_FTFT's dtors dtor's;
  1790             val maps =
  1791               map5 (fn goal => fn unfold => fn map_comp => fn map_cong0 => fn map_arg_cong =>
  1792                 Goal.prove_sorry lthy [] [] goal
  1793                   (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jmap_defs THEN
  1794                      mk_map_tac m n map_arg_cong unfold map_comp map_cong0)
  1795                 |> Thm.close_derivation
  1796                 |> singleton (Proof_Context.export names_lthy lthy))
  1797               goals dtor_unfold_thms map_comps map_cong0s map_arg_cong_thms;
  1798           in
  1799             map_split (fn thm => (thm RS @{thm comp_eq_dest}, thm)) maps
  1800           end;
  1801 
  1802         val dtor_Jmap_unique_thm =
  1803           let
  1804             fun mk_prem u map dtor dtor' =
  1805               mk_Trueprop_eq (HOLogic.mk_comp (dtor', u),
  1806                 HOLogic.mk_comp (Term.list_comb (map, fs @ us), dtor));
  1807             val prems = map4 mk_prem us map_FTFT's dtors dtor's;
  1808             val goal =
  1809               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1810                 (map2 (curry HOLogic.mk_eq) us fs_Jmaps));
  1811           in
  1812             Goal.prove_sorry lthy [] [] (Logic.list_implies (prems, goal))
  1813               (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jmap_defs THEN
  1814                 mk_dtor_map_unique_tac ctxt dtor_unfold_unique_thm sym_map_comps)
  1815             |> Thm.close_derivation
  1816             |> singleton (Proof_Context.export names_lthy lthy)
  1817           end;
  1818 
  1819         val Jmap_comp0_thms =
  1820           let
  1821             val goal = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1822               (map3 (fn fmap => fn gmap => fn fgmap =>
  1823                  HOLogic.mk_eq (HOLogic.mk_comp (gmap, fmap), fgmap))
  1824               fs_Jmaps gs_Jmaps fgs_Jmaps))
  1825           in
  1826             split_conj_thm (Goal.prove_sorry lthy [] [] goal
  1827               (K (mk_map_comp0_tac Jmap_thms map_comp0s dtor_Jmap_unique_thm))
  1828               |> Thm.close_derivation
  1829               |> singleton (Proof_Context.export names_lthy lthy))
  1830           end;
  1831 
  1832         val timer = time (timer "map functions for the new codatatypes");
  1833 
  1834         val Jset_minimal_thms =
  1835           let
  1836             fun mk_passive_prem set dtor x K =
  1837               Logic.all x (HOLogic.mk_Trueprop (mk_leq (set $ (dtor $ x)) (K $ x)));
  1838 
  1839             fun mk_active_prem dtor x1 K1 set x2 K2 =
  1840               fold_rev Logic.all [x1, x2]
  1841                 (Logic.mk_implies (mk_Trueprop_mem (x2, set $ (dtor $ x1)),
  1842                   HOLogic.mk_Trueprop (mk_leq (K2 $ x2) (K1 $ x1))));
  1843 
  1844             val premss = map2 (fn j => fn Ks =>
  1845               map4 mk_passive_prem (map (fn xs => nth xs (j - 1)) FTs_setss) dtors Jzs Ks @
  1846                 flat (map4 (fn sets => fn s => fn x1 => fn K1 =>
  1847                   map3 (mk_active_prem s x1 K1) (drop m sets) Jzs_copy Ks) FTs_setss dtors Jzs Ks))
  1848               ls Kss;
  1849 
  1850             val col_minimal_thms =
  1851               let
  1852                 fun mk_conjunct j T i K x = mk_leq (mk_col Ts nat i j T $ x) (K $ x);
  1853                 fun mk_concl j T Ks = list_all_free Jzs
  1854                   (Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T) ks Ks Jzs));
  1855                 val concls = map3 mk_concl ls passiveAs Kss;
  1856 
  1857                 val goals = map2 (fn prems => fn concl =>
  1858                   Logic.list_implies (prems, HOLogic.mk_Trueprop concl)) premss concls
  1859 
  1860                 val ctss =
  1861                   map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
  1862               in
  1863                 map4 (fn goal => fn cts => fn col_0s => fn col_Sucs =>
  1864                   Goal.prove_sorry lthy [] [] goal
  1865                     (fn {context = ctxt, prems = _} => mk_col_minimal_tac ctxt m cts col_0s
  1866                       col_Sucs)
  1867                   |> Thm.close_derivation
  1868                   |> singleton (Proof_Context.export names_lthy lthy))
  1869                 goals ctss col_0ss' col_Sucss'
  1870               end;
  1871 
  1872             fun mk_conjunct set K x = mk_leq (set $ x) (K $ x);
  1873             fun mk_concl sets Ks = Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct sets Ks Jzs);
  1874             val concls = map2 mk_concl Jsetss_by_range Kss;
  1875 
  1876             val goals = map2 (fn prems => fn concl =>
  1877               Logic.list_implies (prems, HOLogic.mk_Trueprop concl)) premss concls;
  1878           in
  1879             map2 (fn goal => fn col_minimal =>
  1880               Goal.prove_sorry lthy [] [] goal
  1881                 (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jset_defs THEN
  1882                   mk_Jset_minimal_tac ctxt n col_minimal)
  1883               |> Thm.close_derivation
  1884               |> singleton (Proof_Context.export names_lthy lthy))
  1885             goals col_minimal_thms
  1886           end;
  1887 
  1888         val (dtor_Jset_incl_thmss, dtor_set_Jset_incl_thmsss) =
  1889           let
  1890             fun mk_set_incl_Jset dtor x set Jset =
  1891               HOLogic.mk_Trueprop (mk_leq (set $ (dtor $ x)) (Jset $ x));
  1892 
  1893             fun mk_set_Jset_incl_Jset dtor x y set Jset1 Jset2 =
  1894               Logic.mk_implies (mk_Trueprop_mem (x, set $ (dtor $ y)),
  1895                 HOLogic.mk_Trueprop (mk_leq (Jset1 $ x) (Jset2 $ y)));
  1896 
  1897             val set_incl_Jset_goalss =
  1898               map4 (fn dtor => fn x => fn sets => fn Jsets =>
  1899                 map2 (mk_set_incl_Jset dtor x) (take m sets) Jsets)
  1900               dtors Jzs FTs_setss Jsetss_by_bnf;
  1901 
  1902             (*x(k) : F(i)set(m+k) (dtor(i) y(i)) ==> J(k)set(j) x(k) <= J(i)set(j) y(i)*)
  1903             val set_Jset_incl_Jset_goalsss =
  1904               map4 (fn dtori => fn yi => fn sets => fn Jsetsi =>
  1905                 map3 (fn xk => fn set => fn Jsetsk =>
  1906                   map2 (mk_set_Jset_incl_Jset dtori xk yi set) Jsetsk Jsetsi)
  1907                 Jzs_copy (drop m sets) Jsetss_by_bnf)
  1908               dtors Jzs FTs_setss Jsetss_by_bnf;
  1909           in
  1910             (map2 (fn goals => fn rec_Sucs =>
  1911               map2 (fn goal => fn rec_Suc =>
  1912                 Goal.prove_sorry lthy [] [] goal
  1913                   (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jset_defs THEN
  1914                     mk_set_incl_Jset_tac rec_Suc)
  1915                 |> Thm.close_derivation
  1916                 |> singleton (Proof_Context.export names_lthy lthy))
  1917               goals rec_Sucs)
  1918             set_incl_Jset_goalss col_Sucss,
  1919             map2 (fn goalss => fn rec_Sucs =>
  1920               map2 (fn k => fn goals =>
  1921                 map2 (fn goal => fn rec_Suc =>
  1922                   Goal.prove_sorry lthy [] [] goal
  1923                     (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jset_defs THEN
  1924                       mk_set_Jset_incl_Jset_tac n rec_Suc k)
  1925                   |> Thm.close_derivation
  1926                   |> singleton (Proof_Context.export names_lthy lthy))
  1927                 goals rec_Sucs)
  1928               ks goalss)
  1929             set_Jset_incl_Jset_goalsss col_Sucss)
  1930           end;
  1931 
  1932         val set_incl_Jset_thmss' = transpose dtor_Jset_incl_thmss;
  1933         val set_Jset_incl_Jset_thmsss' = transpose (map transpose dtor_set_Jset_incl_thmsss);
  1934         val set_Jset_thmss = map (map (fn thm => thm RS @{thm set_mp})) dtor_Jset_incl_thmss;
  1935         val set_Jset_Jset_thmsss = map (map (map (fn thm => thm RS @{thm set_mp})))
  1936           dtor_set_Jset_incl_thmsss;
  1937         val set_Jset_thmss' = transpose set_Jset_thmss;
  1938         val set_Jset_Jset_thmsss' = transpose (map transpose set_Jset_Jset_thmsss);
  1939 
  1940         val dtor_Jset_induct_thms =
  1941           let
  1942             val incls =
  1943               maps (map (fn thm => thm RS @{thm subset_Collect_iff})) dtor_Jset_incl_thmss @
  1944                 @{thms subset_Collect_iff[OF subset_refl]};
  1945 
  1946             val cTs = map (SOME o certifyT lthy) params';
  1947             fun mk_induct_tinst phis jsets y y' =
  1948               map4 (fn phi => fn jset => fn Jz => fn Jz' =>
  1949                 SOME (certify lthy (Term.absfree Jz' (HOLogic.mk_Collect (fst y', snd y',
  1950                   HOLogic.mk_conj (HOLogic.mk_mem (y, jset $ Jz), phi $ y $ Jz))))))
  1951               phis jsets Jzs Jzs';
  1952           in
  1953             map6 (fn set_minimal => fn set_set_inclss => fn jsets => fn y => fn y' => fn phis =>
  1954               ((set_minimal
  1955                 |> Drule.instantiate' cTs (mk_induct_tinst phis jsets y y')
  1956                 |> unfold_thms lthy incls) OF
  1957                 (replicate n ballI @
  1958                   maps (map (fn thm => thm RS @{thm subset_CollectI})) set_set_inclss))
  1959               |> singleton (Proof_Context.export names_lthy lthy)
  1960               |> rule_by_tactic lthy (ALLGOALS (TRY o etac asm_rl)))
  1961             Jset_minimal_thms set_Jset_incl_Jset_thmsss' Jsetss_by_range ys ys' dtor_set_induct_phiss
  1962           end;
  1963 
  1964         val (dtor_Jset_thmss', dtor_Jset_thmss) =
  1965           let
  1966             fun mk_simp_goal relate pas_set act_sets sets dtor z set =
  1967               relate (set $ z, mk_union (pas_set $ (dtor $ z),
  1968                  Library.foldl1 mk_union
  1969                    (map2 (fn X => mk_UNION (X $ (dtor $ z))) act_sets sets)));
  1970             fun mk_goals eq =
  1971               map2 (fn i => fn sets =>
  1972                 map4 (fn Fsets =>
  1973                   mk_simp_goal eq (nth Fsets (i - 1)) (drop m Fsets) sets)
  1974                 FTs_setss dtors Jzs sets)
  1975               ls Jsetss_by_range;
  1976 
  1977             val le_goals = map (HOLogic.mk_Trueprop o Library.foldr1 HOLogic.mk_conj)
  1978               (mk_goals (uncurry mk_leq));
  1979             val set_le_thmss = map split_conj_thm
  1980               (map4 (fn goal => fn Jset_minimal => fn set_Jsets => fn set_Jset_Jsetss =>
  1981                 Goal.prove_sorry lthy [] [] goal
  1982                   (K (mk_set_le_tac n Jset_minimal set_Jsets set_Jset_Jsetss))
  1983                 |> Thm.close_derivation
  1984                 |> singleton (Proof_Context.export names_lthy lthy))
  1985               le_goals Jset_minimal_thms set_Jset_thmss' set_Jset_Jset_thmsss');
  1986 
  1987             val ge_goalss = map (map HOLogic.mk_Trueprop) (mk_goals (uncurry mk_leq o swap));
  1988             val set_ge_thmss =
  1989               map3 (map3 (fn goal => fn set_incl_Jset => fn set_Jset_incl_Jsets =>
  1990                 Goal.prove_sorry lthy [] [] goal
  1991                   (K (mk_set_ge_tac n set_incl_Jset set_Jset_incl_Jsets))
  1992                 |> Thm.close_derivation
  1993                 |> singleton (Proof_Context.export names_lthy lthy)))
  1994               ge_goalss set_incl_Jset_thmss' set_Jset_incl_Jset_thmsss'
  1995           in
  1996             map2 (map2 (fn le => fn ge => equalityI OF [le, ge])) set_le_thmss set_ge_thmss
  1997             |> `transpose
  1998           end;
  1999 
  2000         val timer = time (timer "set functions for the new codatatypes");
  2001 
  2002         val colss = map2 (fn j => fn T =>
  2003           map (fn i => mk_col Ts nat i j T) ks) ls passiveAs;
  2004         val colss' = map2 (fn j => fn T =>
  2005           map (fn i => mk_col Ts' nat i j T) ks) ls passiveBs;
  2006 
  2007         val col_natural_thmss =
  2008           let
  2009             fun mk_col_natural f map z col col' =
  2010               HOLogic.mk_eq (mk_image f $ (col $ z), col' $ (map $ z));
  2011 
  2012             fun mk_goal f cols cols' = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2013               (map4 (mk_col_natural f) fs_Jmaps Jzs cols cols'));
  2014 
  2015             val goals = map3 mk_goal fs colss colss';
  2016 
  2017             val ctss =
  2018               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) goals;
  2019 
  2020             val thms =
  2021               map4 (fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2022                 Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  2023                   (fn {context = ctxt, prems = _} => mk_col_natural_tac ctxt cts rec_0s rec_Sucs
  2024                     dtor_Jmap_thms set_mapss)
  2025                 |> Thm.close_derivation
  2026                 |> singleton (Proof_Context.export names_lthy lthy))
  2027               goals ctss col_0ss' col_Sucss';
  2028           in
  2029             map (split_conj_thm o mk_specN n) thms
  2030           end;
  2031 
  2032         val col_bd_thmss =
  2033           let
  2034             fun mk_col_bd z col bd = mk_ordLeq (mk_card_of (col $ z)) bd;
  2035 
  2036             fun mk_goal bds cols = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2037               (map3 mk_col_bd Jzs cols bds));
  2038 
  2039             val goals = map (mk_goal Jbds) colss;
  2040 
  2041             val ctss = map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat])
  2042               (map (mk_goal (replicate n sbd)) colss);
  2043 
  2044             val thms =
  2045               map5 (fn j => fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2046                 Goal.prove_sorry lthy [] [] (HOLogic.mk_Trueprop goal)
  2047                   (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Jbd_defs THEN
  2048                     mk_col_bd_tac m j cts rec_0s rec_Sucs sbd_Card_order sbd_Cinfinite set_sbdss)
  2049                 |> Thm.close_derivation
  2050                 |> singleton (Proof_Context.export names_lthy lthy))
  2051               ls goals ctss col_0ss' col_Sucss';
  2052           in
  2053             map (split_conj_thm o mk_specN n) thms
  2054           end;
  2055 
  2056         val map_cong0_thms =
  2057           let
  2058             val cTs = map (SOME o certifyT lthy o
  2059               Term.typ_subst_atomic (passiveAs ~~ passiveBs) o TFree) coinduct_params;
  2060 
  2061             fun mk_prem z set f g y y' =
  2062               mk_Ball (set $ z) (Term.absfree y' (HOLogic.mk_eq (f $ y, g $ y)));
  2063 
  2064             fun mk_prems sets z =
  2065               Library.foldr1 HOLogic.mk_conj (map5 (mk_prem z) sets fs fs_copy ys ys')
  2066 
  2067             fun mk_map_cong0 sets z fmap gmap =
  2068               HOLogic.mk_imp (mk_prems sets z, HOLogic.mk_eq (fmap $ z, gmap $ z));
  2069 
  2070             fun mk_coind_body sets (x, T) z fmap gmap y y_copy =
  2071               HOLogic.mk_conj
  2072                 (HOLogic.mk_mem (z, HOLogic.mk_Collect (x, T, mk_prems sets z)),
  2073                   HOLogic.mk_conj (HOLogic.mk_eq (y, fmap $ z),
  2074                     HOLogic.mk_eq (y_copy, gmap $ z)))
  2075 
  2076             fun mk_cphi sets (z' as (x, T)) z fmap gmap y' y y'_copy y_copy =
  2077               HOLogic.mk_exists (x, T, mk_coind_body sets z' z fmap gmap y y_copy)
  2078               |> Term.absfree y'_copy
  2079               |> Term.absfree y'
  2080               |> certify lthy;
  2081 
  2082             val cphis = map9 mk_cphi
  2083               Jsetss_by_bnf Jzs' Jzs fs_Jmaps fs_copy_Jmaps Jys' Jys Jys'_copy Jys_copy;
  2084 
  2085             val coinduct = Drule.instantiate' cTs (map SOME cphis) dtor_coinduct_thm;
  2086 
  2087             val goal =
  2088               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2089                 (map4 mk_map_cong0 Jsetss_by_bnf Jzs fs_Jmaps fs_copy_Jmaps));
  2090 
  2091             val thm =
  2092               Goal.prove_sorry lthy [] [] goal
  2093                 (K (mk_mcong_tac lthy m (rtac coinduct) map_comps dtor_Jmap_thms map_cong0s
  2094                   set_mapss set_Jset_thmss set_Jset_Jset_thmsss in_rels))
  2095               |> Thm.close_derivation
  2096               |>  singleton (Proof_Context.export names_lthy lthy);
  2097           in
  2098             split_conj_thm thm
  2099           end;
  2100 
  2101         val in_Jrels = map (fn def => trans OF [def, @{thm OO_Grp_alt}] RS @{thm predicate2_eqD})
  2102             Jrel_unabs_defs;
  2103 
  2104         val Jrels = mk_Jrels passiveAs passiveBs;
  2105         val Jrelphis = map (fn rel => Term.list_comb (rel, Jphis)) Jrels;
  2106         val relphis = map (fn rel => Term.list_comb (rel, Jphis @ Jrelphis)) rels;
  2107         val Jrelpsi1s = map (fn rel => Term.list_comb (rel, Jpsi1s)) (mk_Jrels passiveAs passiveCs);
  2108         val Jrelpsi2s = map (fn rel => Term.list_comb (rel, Jpsi2s)) (mk_Jrels passiveCs passiveBs);
  2109         val Jrelpsi12s = map (fn rel =>
  2110             Term.list_comb (rel, map2 (curry mk_rel_compp) Jpsi1s Jpsi2s)) Jrels;
  2111 
  2112         val dtor_Jrel_thms =
  2113           let
  2114             fun mk_goal Jz Jz' dtor dtor' Jrelphi relphi =
  2115               mk_Trueprop_eq (Jrelphi $ Jz $ Jz', relphi $ (dtor $ Jz) $ (dtor' $ Jz'));
  2116             val goals = map6 mk_goal Jzs Jz's dtors dtor's Jrelphis relphis;
  2117           in
  2118             map12 (fn i => fn goal => fn in_rel => fn map_comp0 => fn map_cong0 =>
  2119               fn dtor_map => fn dtor_sets => fn dtor_inject => fn dtor_ctor =>
  2120               fn set_map0s => fn dtor_set_incls => fn dtor_set_set_inclss =>
  2121               Goal.prove_sorry lthy [] [] goal
  2122                 (K (mk_dtor_rel_tac lthy in_Jrels i in_rel map_comp0 map_cong0 dtor_map dtor_sets
  2123                   dtor_inject dtor_ctor set_map0s dtor_set_incls dtor_set_set_inclss))
  2124               |> Thm.close_derivation
  2125               |> singleton (Proof_Context.export names_lthy lthy))
  2126             ks goals in_rels map_comps map_cong0s dtor_Jmap_thms dtor_Jset_thmss'
  2127               dtor_inject_thms dtor_ctor_thms set_mapss dtor_Jset_incl_thmss
  2128               dtor_set_Jset_incl_thmsss
  2129           end;
  2130 
  2131       val passiveABs = map2 (curry HOLogic.mk_prodT) passiveAs passiveBs;
  2132       val zip_ranTs = passiveABs @ prodTsTs';
  2133       val allJphis = Jphis @ activeJphis;
  2134       val zipFTs = mk_FTs zip_ranTs;
  2135       val zipTs = map3 (fn T => fn T' => fn FT => T --> T' --> FT) Ts Ts' zipFTs;
  2136       val zip_zTs = mk_Ts passiveABs;
  2137       val (((zips, (abs, abs')), (zip_zs, zip_zs')), names_lthy) = names_lthy
  2138         |> mk_Frees "zip" zipTs
  2139         ||>> mk_Frees' "ab" passiveABs
  2140         ||>> mk_Frees' "z" zip_zTs;
  2141 
  2142       val Iphi_sets =
  2143         map2 (fn phi => fn T => HOLogic.Collect_const T $ HOLogic.mk_split phi) allJphis zip_ranTs;
  2144       val in_phis = map2 (mk_in Iphi_sets) (mk_setss zip_ranTs) zipFTs;
  2145       val fstABs = map fst_const passiveABs;
  2146       val all_fsts = fstABs @ fstsTsTs';
  2147       val map_all_fsts = map2 (fn Ds => fn bnf =>
  2148         Term.list_comb (mk_map_of_bnf Ds zip_ranTs (passiveAs @ Ts) bnf, all_fsts)) Dss bnfs;
  2149       val Jmap_fsts = map2 (fn map => fn T => if m = 0 then HOLogic.id_const T
  2150         else Term.list_comb (map, fstABs)) (mk_Jmaps passiveABs passiveAs) Ts;
  2151 
  2152       val sndABs = map snd_const passiveABs;
  2153       val all_snds = sndABs @ sndsTsTs';
  2154       val map_all_snds = map2 (fn Ds => fn bnf =>
  2155         Term.list_comb (mk_map_of_bnf Ds zip_ranTs (passiveBs @ Ts') bnf, all_snds)) Dss bnfs;
  2156       val Jmap_snds = map2 (fn map => fn T => if m = 0 then HOLogic.id_const T
  2157         else Term.list_comb (map, sndABs)) (mk_Jmaps passiveABs passiveBs) Ts;
  2158       val zip_unfolds = map (mk_unfold zip_zTs (map HOLogic.mk_split zips)) ks;
  2159       val zip_setss = mk_Jsetss passiveABs |> transpose;
  2160 
  2161       fun Jrel_coinduct_tac {context = ctxt, prems = CIHs} =
  2162         let
  2163           fun mk_helper_prem phi in_phi zip x y map map' dtor dtor' =
  2164             let
  2165               val zipxy = zip $ x $ y;
  2166             in
  2167               HOLogic.mk_Trueprop (list_all_free [x, y]
  2168                 (HOLogic.mk_imp (phi $ x $ y, HOLogic.mk_conj (HOLogic.mk_mem (zipxy, in_phi),
  2169                   HOLogic.mk_conj (HOLogic.mk_eq (map $ zipxy, dtor $ x),
  2170                     HOLogic.mk_eq (map' $ zipxy, dtor' $ y))))))
  2171             end;
  2172           val helper_prems = map9 mk_helper_prem
  2173             activeJphis in_phis zips Jzs Jz's map_all_fsts map_all_snds dtors dtor's;
  2174           fun mk_helper_coind_phi fst phi x alt y map zip_unfold =
  2175             list_exists_free [if fst then y else x] (HOLogic.mk_conj (phi $ x $ y,
  2176               HOLogic.mk_eq (alt, map $ (zip_unfold $ HOLogic.mk_prod (x, y)))))
  2177           val coind1_phis = map6 (mk_helper_coind_phi true)
  2178             activeJphis Jzs Jzs_copy Jz's Jmap_fsts zip_unfolds;
  2179           val coind2_phis = map6 (mk_helper_coind_phi false)
  2180               activeJphis Jzs Jz's_copy Jz's Jmap_snds zip_unfolds;
  2181           fun mk_cts zs z's phis =
  2182             map3 (fn z => fn z' => fn phi =>
  2183               SOME (certify lthy (fold_rev (Term.absfree o Term.dest_Free) [z', z] phi)))
  2184             zs z's phis @
  2185             map (SOME o certify lthy) (splice z's zs);
  2186           val cts1 = mk_cts Jzs Jzs_copy coind1_phis;
  2187           val cts2 = mk_cts Jz's Jz's_copy coind2_phis;
  2188 
  2189           fun mk_helper_coind_concl z alt coind_phi =
  2190             HOLogic.mk_imp (coind_phi, HOLogic.mk_eq (alt, z));
  2191           val helper_coind1_concl =
  2192             HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2193               (map3 mk_helper_coind_concl Jzs Jzs_copy coind1_phis));
  2194           val helper_coind2_concl =
  2195             HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2196               (map3 mk_helper_coind_concl Jz's Jz's_copy coind2_phis));
  2197 
  2198           fun mk_helper_coind_thms fst concl cts =
  2199             Goal.prove_sorry lthy [] [] (Logic.list_implies (helper_prems, concl))
  2200               (fn {context = ctxt, prems = _} => mk_rel_coinduct_coind_tac ctxt fst m
  2201                 (cterm_instantiate_pos cts dtor_coinduct_thm) ks map_comps map_cong0s
  2202                 map_arg_cong_thms set_mapss dtor_unfold_thms dtor_Jmap_thms in_rels)
  2203             |> Thm.close_derivation
  2204             |> split_conj_thm
  2205             |> Proof_Context.export names_lthy lthy;
  2206 
  2207           val helper_coind1_thms = mk_helper_coind_thms true helper_coind1_concl cts1;
  2208           val helper_coind2_thms = mk_helper_coind_thms false helper_coind2_concl cts2;
  2209 
  2210           fun mk_helper_ind_phi phi ab fst snd z active_phi x y zip_unfold =
  2211             list_all_free [x, y] (HOLogic.mk_imp
  2212               (HOLogic.mk_conj (active_phi $ x $ y,
  2213                  HOLogic.mk_eq (z, zip_unfold $ HOLogic.mk_prod (x, y))),
  2214               phi $ (fst $ ab) $ (snd $ ab)));
  2215           val helper_ind_phiss =
  2216             map4 (fn Jphi => fn ab => fn fst => fn snd =>
  2217               map5 (mk_helper_ind_phi Jphi ab fst snd)
  2218               zip_zs activeJphis Jzs Jz's zip_unfolds)
  2219             Jphis abs fstABs sndABs;
  2220           val ctss = map2 (fn ab' => fn phis =>
  2221               map2 (fn z' => fn phi =>
  2222                 SOME (certify lthy (Term.absfree ab' (Term.absfree z' phi))))
  2223               zip_zs' phis @
  2224               map (SOME o certify lthy) zip_zs)
  2225             abs' helper_ind_phiss;
  2226           fun mk_helper_ind_concl ab' z ind_phi set =
  2227             mk_Ball (set $ z) (Term.absfree ab' ind_phi);
  2228 
  2229           val mk_helper_ind_concls =
  2230             map3 (fn ab' => fn ind_phis => fn zip_sets =>
  2231               map3 (mk_helper_ind_concl ab') zip_zs ind_phis zip_sets)
  2232             abs' helper_ind_phiss zip_setss
  2233             |> map (HOLogic.mk_Trueprop o Library.foldr1 HOLogic.mk_conj);
  2234 
  2235           val helper_ind_thmss = if m = 0 then replicate n [] else
  2236             map4 (fn concl => fn j => fn set_induct => fn cts =>
  2237               Goal.prove_sorry lthy [] [] (Logic.list_implies (helper_prems, concl))
  2238                 (fn {context = ctxt, prems = _} => mk_rel_coinduct_ind_tac ctxt m ks
  2239                   dtor_unfold_thms set_mapss j (cterm_instantiate_pos cts set_induct))
  2240               |> Thm.close_derivation
  2241               |> split_conj_thm
  2242               |> Proof_Context.export names_lthy lthy)
  2243             mk_helper_ind_concls ls dtor_Jset_induct_thms ctss
  2244             |> transpose;
  2245         in
  2246           mk_rel_coinduct_tac ctxt CIHs in_rels in_Jrels
  2247             helper_ind_thmss helper_coind1_thms helper_coind2_thms
  2248         end;
  2249 
  2250       val Jrel_coinduct_thm =
  2251         mk_rel_xtor_co_induct_thm Greatest_FP rels activeJphis Jrels Jphis Jzs Jz's dtors dtor's
  2252           Jrel_coinduct_tac lthy;
  2253 
  2254         val le_Jrel_OO_thm =
  2255           let
  2256             fun mk_le_Jrel_OO Jrelpsi1 Jrelpsi2 Jrelpsi12 =
  2257               mk_leq (mk_rel_compp (Jrelpsi1, Jrelpsi2)) Jrelpsi12;
  2258             val goals = map3 mk_le_Jrel_OO Jrelpsi1s Jrelpsi2s Jrelpsi12s;
  2259 
  2260             val goal = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj goals);
  2261           in
  2262             Goal.prove_sorry lthy [] [] goal
  2263               (K (mk_le_rel_OO_tac Jrel_coinduct_thm dtor_Jrel_thms rel_OOs))
  2264             |> Thm.close_derivation
  2265             |> singleton (Proof_Context.export names_lthy lthy)
  2266           end;
  2267 
  2268         val timer = time (timer "helpers for BNF properties");
  2269 
  2270         fun close_wit I wit = (I, fold_rev Term.absfree (map (nth ys') I) wit);
  2271 
  2272         val all_unitTs = replicate live HOLogic.unitT;
  2273         val unitTs = replicate n HOLogic.unitT;
  2274         val unit_funs = replicate n (Term.absdummy HOLogic.unitT HOLogic.unit);
  2275         fun mk_map_args I =
  2276           map (fn i =>
  2277             if member (op =) I i then Term.absdummy HOLogic.unitT (nth ys i)
  2278             else mk_undefined (HOLogic.unitT --> nth passiveAs i))
  2279           (0 upto (m - 1));
  2280 
  2281         fun mk_nat_wit Ds bnf (I, wit) () =
  2282           let
  2283             val passiveI = filter (fn i => i < m) I;
  2284             val map_args = mk_map_args passiveI;
  2285           in
  2286             Term.absdummy HOLogic.unitT (Term.list_comb
  2287               (mk_map_of_bnf Ds all_unitTs (passiveAs @ unitTs) bnf, map_args @ unit_funs) $ wit)
  2288           end;
  2289 
  2290         fun mk_dummy_wit Ds bnf I =
  2291           let
  2292             val map_args = mk_map_args I;
  2293           in
  2294             Term.absdummy HOLogic.unitT (Term.list_comb
  2295               (mk_map_of_bnf Ds all_unitTs (passiveAs @ unitTs) bnf, map_args @ unit_funs) $
  2296               mk_undefined (mk_T_of_bnf Ds all_unitTs bnf))
  2297           end;
  2298 
  2299         val nat_witss =
  2300           map2 (fn Ds => fn bnf => mk_wits_of_bnf (replicate (nwits_of_bnf bnf) Ds)
  2301             (replicate (nwits_of_bnf bnf) (replicate live HOLogic.unitT)) bnf
  2302             |> map (fn (I, wit) =>
  2303               (I, Lazy.lazy (mk_nat_wit Ds bnf (I, Term.list_comb (wit, map (K HOLogic.unit) I))))))
  2304           Dss bnfs;
  2305 
  2306         val nat_wit_thmss = map2 (curry op ~~) nat_witss (map wit_thmss_of_bnf bnfs)
  2307 
  2308         val Iss = map (map fst) nat_witss;
  2309 
  2310         fun filter_wits (I, wit) =
  2311           let val J = filter (fn i => i < m) I;
  2312           in (J, (length J < length I, wit)) end;
  2313 
  2314         val wit_treess = map_index (fn (i, Is) =>
  2315           map_index (finish Iss m [i+m] (i+m)) Is) Iss
  2316           |> map (minimize_wits o map filter_wits o minimize_wits o flat);
  2317 
  2318         val coind_wit_argsss =
  2319           map (map (tree_to_coind_wits nat_wit_thmss o snd o snd) o filter (fst o snd)) wit_treess;
  2320 
  2321         val nonredundant_coind_wit_argsss =
  2322           fold (fn i => fn argsss =>
  2323             nth_map (i - 1) (filter_out (fn xs =>
  2324               exists (fn ys =>
  2325                 let
  2326                   val xs' = (map (fst o fst) xs, snd (fst (hd xs)));
  2327                   val ys' = (map (fst o fst) ys, snd (fst (hd ys)));
  2328                 in
  2329                   eq_pair (subset (op =)) (eq_set (op =)) (xs', ys') andalso not (fst xs' = fst ys')
  2330                 end)
  2331               (flat argsss)))
  2332             argsss)
  2333           ks coind_wit_argsss;
  2334 
  2335         fun prepare_args args =
  2336           let
  2337             val I = snd (fst (hd args));
  2338             val (dummys, args') =
  2339               map_split (fn i =>
  2340                 (case find_first (fn arg => fst (fst arg) = i - 1) args of
  2341                   SOME (_, ((_, wit), thms)) => (NONE, (Lazy.force wit, thms))
  2342                 | NONE =>
  2343                   (SOME (i - 1), (mk_dummy_wit (nth Dss (i - 1)) (nth bnfs (i - 1)) I, []))))
  2344               ks;
  2345           in
  2346             ((I, dummys), apsnd flat (split_list args'))
  2347           end;
  2348 
  2349         fun mk_coind_wits ((I, dummys), (args, thms)) =
  2350           ((I, dummys), (map (fn i => mk_unfold Ts args i $ HOLogic.unit) ks, thms));
  2351 
  2352         val coind_witss =
  2353           maps (map (mk_coind_wits o prepare_args)) nonredundant_coind_wit_argsss;
  2354 
  2355         val witss = map2 (fn Ds => fn bnf => mk_wits_of_bnf
  2356           (replicate (nwits_of_bnf bnf) Ds)
  2357           (replicate (nwits_of_bnf bnf) (passiveAs @ Ts)) bnf) Dss bnfs;
  2358 
  2359         val ctor_witss =
  2360           map (map (uncurry close_wit o tree_to_ctor_wit ys ctors witss o snd o snd) o
  2361             filter_out (fst o snd)) wit_treess;
  2362 
  2363         fun mk_coind_wit_thms ((I, dummys), (wits, wit_thms)) =
  2364           let
  2365             fun mk_goal sets y y_copy y'_copy j =
  2366               let
  2367                 fun mk_conjunct set z dummy wit =
  2368                   mk_Ball (set $ z) (Term.absfree y'_copy
  2369                     (if dummy = NONE orelse member (op =) I (j - 1) then
  2370                       HOLogic.mk_imp (HOLogic.mk_eq (z, wit),
  2371                         if member (op =) I (j - 1) then HOLogic.mk_eq (y_copy, y)
  2372                         else @{term False})
  2373                     else @{term True}));
  2374               in
  2375                 HOLogic.mk_Trueprop
  2376                   (Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct sets Jzs dummys wits))
  2377               end;
  2378             val goals = map5 mk_goal Jsetss_by_range ys ys_copy ys'_copy ls;
  2379           in
  2380             map2 (fn goal => fn induct =>
  2381               Goal.prove_sorry lthy [] [] goal
  2382                 (fn {context = ctxt, prems = _} => mk_coind_wit_tac ctxt induct dtor_unfold_thms
  2383                   (flat set_mapss) wit_thms)
  2384               |> Thm.close_derivation
  2385               |> singleton (Proof_Context.export names_lthy lthy))
  2386             goals dtor_Jset_induct_thms
  2387             |> map split_conj_thm
  2388             |> transpose
  2389             |> map (map_filter (try (fn thm => thm RS bspec RS mp)))
  2390             |> curry op ~~ (map_index Library.I (map (close_wit I) wits))
  2391             |> filter (fn (_, thms) => length thms = m)
  2392           end;
  2393 
  2394         val coind_wit_thms = maps mk_coind_wit_thms coind_witss;
  2395 
  2396         val (wit_thmss, all_witss) =
  2397           fold (fn ((i, wit), thms) => fn witss =>
  2398             nth_map i (fn (thms', wits) => (thms @ thms', wit :: wits)) witss)
  2399           coind_wit_thms (map (pair []) ctor_witss)
  2400           |> map (apsnd (map snd o minimize_wits))
  2401           |> split_list;
  2402 
  2403         val timer = time (timer "witnesses");
  2404 
  2405         val map_id0_tacs =
  2406           map2 (K oo mk_map_id0_tac Jmap_thms) dtor_unfold_unique_thms unfold_dtor_thms;
  2407         val map_comp0_tacs = map (fn thm => K (rtac (thm RS sym) 1)) Jmap_comp0_thms;
  2408         val map_cong0_tacs = map (fn thm => fn ctxt => mk_map_cong0_tac ctxt m thm) map_cong0_thms;
  2409         val set_map0_tacss =
  2410           map (map (fn col => fn ctxt => unfold_thms_tac ctxt Jset_defs THEN mk_set_map0_tac col))
  2411             (transpose col_natural_thmss);
  2412 
  2413         val Jbd_card_orders = map (fn def => fold_thms lthy [def] sbd_card_order) Jbd_defs;
  2414         val Jbd_Cinfinites = map (fn def => fold_thms lthy [def] sbd_Cinfinite) Jbd_defs;
  2415 
  2416         val bd_co_tacs = map (fn thm => K (rtac thm 1)) Jbd_card_orders;
  2417         val bd_cinf_tacs = map (fn thm => K (rtac (thm RS conjunct1) 1)) Jbd_Cinfinites;
  2418 
  2419         val set_bd_tacss =
  2420           map2 (fn Cinf => map (fn col => fn ctxt =>
  2421             unfold_thms_tac ctxt Jset_defs THEN mk_set_bd_tac Cinf col))
  2422           Jbd_Cinfinites (transpose col_bd_thmss);
  2423 
  2424         val le_rel_OO_tacs = map (fn i => K (rtac (le_Jrel_OO_thm RS mk_conjunctN n i) 1)) ks;
  2425 
  2426         val rel_OO_Grp_tacs = map (fn def => K (rtac def 1)) Jrel_unabs_defs;
  2427 
  2428         val tacss = map9 zip_axioms map_id0_tacs map_comp0_tacs map_cong0_tacs set_map0_tacss
  2429           bd_co_tacs bd_cinf_tacs set_bd_tacss le_rel_OO_tacs rel_OO_Grp_tacs;
  2430 
  2431         fun wit_tac thms ctxt =
  2432           mk_wit_tac ctxt n dtor_ctor_thms (flat dtor_Jset_thmss) (maps wit_thms_of_bnf bnfs) thms;
  2433 
  2434         val (Jbnfs, lthy) =
  2435           fold_map7 (fn b => fn tacs => fn map_b => fn rel_b => fn set_bs => fn wit_thms =>
  2436               fn consts =>
  2437             bnf_def Hardly_Inline (user_policy Note_Some) false I tacs (wit_tac wit_thms)
  2438               (SOME deads) map_b rel_b set_bs consts
  2439             #> (fn (bnf, lthy) => (bnf, register_bnf (Local_Theory.full_name lthy b) bnf lthy)))
  2440           bs tacss map_bs rel_bs set_bss wit_thmss
  2441           ((((((replicate n Binding.empty ~~ Ts) ~~ Jmaps) ~~ Jsetss_by_bnf) ~~ Jbds) ~~
  2442             all_witss) ~~ map SOME Jrels)
  2443           lthy;
  2444 
  2445         val timer = time (timer "registered new codatatypes as BNFs");
  2446 
  2447         val ls' = if m = 1 then [0] else ls;
  2448 
  2449         val Jbnf_common_notes =
  2450           [(dtor_map_uniqueN, [dtor_Jmap_unique_thm])] @
  2451           map2 (fn i => fn thm => (mk_dtor_set_inductN i, [thm])) ls' dtor_Jset_induct_thms
  2452           |> map (fn (thmN, thms) =>
  2453             ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  2454 
  2455         val Jbnf_notes =
  2456           [(dtor_mapN, map single dtor_Jmap_thms),
  2457           (dtor_relN, map single dtor_Jrel_thms),
  2458           (dtor_set_inclN, dtor_Jset_incl_thmss),
  2459           (dtor_set_set_inclN, map flat dtor_set_Jset_incl_thmsss)] @
  2460           map2 (fn i => fn thms => (mk_dtor_setN i, map single thms)) ls' dtor_Jset_thmss
  2461           |> maps (fn (thmN, thmss) =>
  2462             map2 (fn b => fn thms =>
  2463               ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  2464             bs thmss)
  2465       in
  2466         (timer, Jbnfs, (Jmap_thms, dtor_Jmap_thms), dtor_Jset_thmss',
  2467           dtor_Jrel_thms, Jrel_coinduct_thm, Jbnf_common_notes @ Jbnf_notes, dtor_Jset_induct_thms,
  2468           lthy)
  2469       end;
  2470 
  2471     val dtor_unfold_o_Jmap_thms = mk_xtor_un_fold_o_map_thms Greatest_FP false m
  2472       dtor_unfold_unique_thm dtor_Jmap_o_thms (map (mk_pointfree lthy) dtor_unfold_thms)
  2473       sym_map_comps map_cong0s;
  2474     val dtor_corec_o_Jmap_thms = mk_xtor_un_fold_o_map_thms Greatest_FP true m
  2475       dtor_corec_unique_thm dtor_Jmap_o_thms (map (mk_pointfree lthy) dtor_corec_thms)
  2476       sym_map_comps map_cong0s;
  2477 
  2478     val rels = map2 (fn Ds => mk_rel_of_bnf Ds allAs allBs') Dss bnfs;
  2479 
  2480     val dtor_unfold_transfer_thms =
  2481       mk_un_fold_transfer_thms Greatest_FP rels activephis
  2482         (if m = 0 then map HOLogic.eq_const Ts
  2483           else map (mk_rel_of_bnf deads passiveAs passiveBs) Jbnfs) Jphis
  2484         (mk_unfolds passiveAs activeAs) (mk_unfolds passiveBs activeBs)
  2485         (fn {context = ctxt, prems = _} => mk_unfold_transfer_tac ctxt m Jrel_coinduct_thm
  2486           (map map_transfer_of_bnf bnfs) dtor_unfold_thms)
  2487         lthy;
  2488 
  2489     val timer = time (timer "relator coinduction");
  2490 
  2491     val common_notes =
  2492       [(dtor_coinductN, [dtor_coinduct_thm]),
  2493       (dtor_rel_coinductN, [Jrel_coinduct_thm]),
  2494       (dtor_unfold_transferN, dtor_unfold_transfer_thms)]
  2495       |> map (fn (thmN, thms) =>
  2496         ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
  2497 
  2498     val notes =
  2499       [(ctor_dtorN, ctor_dtor_thms),
  2500       (ctor_exhaustN, ctor_exhaust_thms),
  2501       (ctor_injectN, ctor_inject_thms),
  2502       (dtor_corecN, dtor_corec_thms),
  2503       (dtor_ctorN, dtor_ctor_thms),
  2504       (dtor_exhaustN, dtor_exhaust_thms),
  2505       (dtor_injectN, dtor_inject_thms),
  2506       (dtor_unfoldN, dtor_unfold_thms),
  2507       (dtor_unfold_uniqueN, dtor_unfold_unique_thms),
  2508       (dtor_corec_uniqueN, dtor_corec_unique_thms),
  2509       (dtor_unfold_o_mapN, dtor_unfold_o_Jmap_thms),
  2510       (dtor_corec_o_mapN, dtor_corec_o_Jmap_thms)]
  2511       |> map (apsnd (map single))
  2512       |> maps (fn (thmN, thmss) =>
  2513         map2 (fn b => fn thms =>
  2514           ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
  2515         bs thmss);
  2516 
  2517     (*FIXME: once the package exports all the necessary high-level characteristic theorems,
  2518        those should not only be concealed but rather not noted at all*)
  2519     val maybe_conceal_notes = note_all = false ? map (apfst (apfst Binding.conceal));
  2520 
  2521     val (noted, lthy') =
  2522       lthy |> Local_Theory.notes (maybe_conceal_notes (common_notes @ notes @ Jbnf_notes));
  2523 
  2524     val fp_res =
  2525       {Ts = Ts, bnfs = Jbnfs, ctors = ctors, dtors = dtors, xtor_co_recs = corecs,
  2526        xtor_co_induct = dtor_coinduct_thm, dtor_ctors = dtor_ctor_thms, ctor_dtors = ctor_dtor_thms,
  2527        ctor_injects = ctor_inject_thms, dtor_injects = dtor_inject_thms,
  2528        xtor_map_thms = dtor_Jmap_thms, xtor_set_thmss = dtor_Jset_thmss',
  2529        xtor_rel_thms = dtor_Jrel_thms, xtor_co_rec_thms = dtor_corec_thms,
  2530        xtor_co_rec_o_map_thms = dtor_corec_o_Jmap_thms, rel_xtor_co_induct_thm = Jrel_coinduct_thm,
  2531        dtor_set_induct_thms = dtor_Jset_induct_thms}
  2532       |> morph_fp_result (substitute_noted_thm noted);
  2533   in
  2534     timer; (fp_res, lthy')
  2535   end;
  2536 
  2537 val _ =
  2538   Outer_Syntax.local_theory @{command_spec "codatatype"} "define coinductive datatypes"
  2539     (parse_co_datatype_cmd Greatest_FP construct_gfp);
  2540 
  2541 end;