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