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