src/HOL/Codatatype/Tools/bnf_gfp.ML
author blanchet
Thu Aug 30 14:27:26 2012 +0200 (2012-08-30)
changeset 49029 f0ecfa9575a9
parent 49018 b2884253b421
child 49074 d8af889dcbe3
permissions -rw-r--r--
generate "disc_exhaust" property
     1 (*  Title:      HOL/Codatatype/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 bnf_gfp: binding list -> typ list list -> BNF_Def.BNF list -> Proof.context -> Proof.context
    13 end;
    14 
    15 structure BNF_GFP : BNF_GFP =
    16 struct
    17 
    18 open BNF_Def
    19 open BNF_Util
    20 open BNF_Tactics
    21 open BNF_FP_Util
    22 open BNF_GFP_Util
    23 open BNF_GFP_Tactics
    24 
    25 (*all bnfs have the same lives*)
    26 fun bnf_gfp bs Dss_insts bnfs lthy =
    27   let
    28     val timer = time (Timer.startRealTimer ());
    29 
    30     val live = live_of_bnf (hd bnfs);
    31     val n = length bnfs; (*active*)
    32     val ks = 1 upto n;
    33     val m = live - n (*passive, if 0 don't generate a new bnf*);
    34     val ls = 1 upto m;
    35     val b = Binding.name (fold_rev (fn b => fn s => Binding.name_of b ^ s) bs "");
    36 
    37     fun note thmN thms = snd o Local_Theory.note
    38       ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), thms);
    39     fun notes thmN thmss = fold2 (fn b => fn thms => snd o Local_Theory.note
    40       ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), thms)) bs thmss;
    41 
    42     (* TODO: check if m, n etc are sane *)
    43 
    44     val Dss = map (fn Ds => map TFree (fold Term.add_tfreesT Ds [])) Dss_insts;
    45     val deads = distinct (op =) (flat Dss);
    46     val names_lthy = fold Variable.declare_typ deads lthy;
    47 
    48     (* tvars *)
    49     val ((((((((passiveAs, activeAs), allAs)), (passiveBs, activeBs)),
    50       (passiveCs, activeCs)), passiveXs), passiveYs), idxT) = names_lthy
    51       |> mk_TFrees live
    52       |> apfst (`(chop m))
    53       ||> mk_TFrees live
    54       ||>> apfst (chop m)
    55       ||> mk_TFrees live
    56       ||>> apfst (chop m)
    57       ||>> mk_TFrees m
    58       ||>> mk_TFrees m
    59       ||> fst o mk_TFrees 1
    60       ||> the_single;
    61 
    62     val Ass = replicate n allAs;
    63     val allBs = passiveAs @ activeBs;
    64     val Bss = replicate n allBs;
    65     val allCs = passiveAs @ activeCs;
    66     val allCs' = passiveBs @ activeCs;
    67     val Css' = replicate n allCs';
    68 
    69     (* typs *)
    70     fun mk_FTs Ts = map2 (fn Ds => mk_T_of_bnf Ds Ts) Dss bnfs;
    71     val (params, params') = `(map dest_TFree) (deads @ passiveAs);
    72     val FTsAs = mk_FTs allAs;
    73     val FTsBs = mk_FTs allBs;
    74     val FTsCs = mk_FTs allCs;
    75     val ATs = map HOLogic.mk_setT passiveAs;
    76     val BTs = map HOLogic.mk_setT activeAs;
    77     val B'Ts = map HOLogic.mk_setT activeBs;
    78     val B''Ts = map HOLogic.mk_setT activeCs;
    79     val sTs = map2 (fn T => fn U => T --> U) activeAs FTsAs;
    80     val s'Ts = map2 (fn T => fn U => T --> U) activeBs FTsBs;
    81     val s''Ts = map2 (fn T => fn U => T --> U) activeCs FTsCs;
    82     val fTs = map2 (fn T => fn U => T --> U) activeAs activeBs;
    83     val all_fTs = map2 (fn T => fn U => T --> U) allAs allBs;
    84     val self_fTs = map (fn T => T --> T) activeAs;
    85     val gTs = map2 (fn T => fn U => T --> U) activeBs activeCs;
    86     val all_gTs = map2 (fn T => fn U => T --> U) allBs allCs';
    87     val RTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeBs;
    88     val sRTs = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeAs activeAs;
    89     val R'Ts = map2 (fn T => fn U => HOLogic.mk_prodT (T, U)) activeBs activeCs;
    90     val setsRTs = map HOLogic.mk_setT sRTs;
    91     val setRTs = map HOLogic.mk_setT RTs;
    92     val all_sbisT = HOLogic.mk_tupleT setsRTs;
    93     val setR'Ts = map HOLogic.mk_setT R'Ts;
    94     val FRTs = mk_FTs (passiveAs @ RTs);
    95     val sumBsAs = map2 (curry mk_sumT) activeBs activeAs;
    96     val sumFTs = mk_FTs (passiveAs @ sumBsAs);
    97     val sum_sTs = map2 (fn T => fn U => T --> U) activeAs sumFTs;
    98 
    99     (* terms *)
   100     val mapsAsAs = map4 mk_map_of_bnf Dss Ass Ass bnfs;
   101     val mapsAsBs = map4 mk_map_of_bnf Dss Ass Bss bnfs;
   102     val mapsBsCs' = map4 mk_map_of_bnf Dss Bss Css' bnfs;
   103     val mapsAsCs' = map4 mk_map_of_bnf Dss Ass Css' bnfs;
   104     val map_Inls = map4 mk_map_of_bnf Dss Bss (replicate n (passiveAs @ sumBsAs)) bnfs;
   105     val map_Inls_rev = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ sumBsAs)) Bss bnfs;
   106     val map_fsts = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Ass bnfs;
   107     val map_snds = map4 mk_map_of_bnf Dss (replicate n (passiveAs @ RTs)) Bss bnfs;
   108     fun mk_setss Ts = map3 mk_sets_of_bnf (map (replicate live) Dss)
   109       (map (replicate live) (replicate n Ts)) bnfs;
   110     val setssAs = mk_setss allAs;
   111     val setssAs' = transpose setssAs;
   112     val bis_setss = mk_setss (passiveAs @ RTs);
   113     val relsAsBs = map4 mk_rel_of_bnf Dss Ass Bss bnfs;
   114     val bds = map3 mk_bd_of_bnf Dss Ass bnfs;
   115     val sum_bd = Library.foldr1 (uncurry mk_csum) bds;
   116     val sum_bdT = fst (dest_relT (fastype_of sum_bd));
   117     val witss = map wits_of_bnf bnfs;
   118 
   119     val emptys = map (fn T => HOLogic.mk_set T []) passiveAs;
   120     val Zeros = map (fn empty =>
   121      HOLogic.mk_tuple (map (fn U => absdummy U empty) activeAs)) emptys;
   122     val hrecTs = map fastype_of Zeros;
   123     val hsetTs = map (fn hrecT => Library.foldr (op -->) (sTs, HOLogic.natT --> hrecT)) hrecTs;
   124 
   125     val (((((((((((((((((((((((((((((((((((zs, zs'), zs_copy), zs_copy2),
   126       z's), As), As_copy), Bs), Bs_copy), B's), B''s), ss), sum_ss), s's), s''s), fs), fs_copy),
   127       self_fs), all_fs), gs), all_gs), xFs), xFs_copy), RFs), (Rtuple, Rtuple')), (hrecs, hrecs')),
   128       (nat, nat')), Rs), Rs_copy), R's), sRs), (idx, idx')), Idx), Ris), Kss),
   129       names_lthy) = lthy
   130       |> mk_Frees' "b" activeAs
   131       ||>> mk_Frees "b" activeAs
   132       ||>> mk_Frees "b" activeAs
   133       ||>> mk_Frees "b" activeBs
   134       ||>> mk_Frees "A" ATs
   135       ||>> mk_Frees "A" ATs
   136       ||>> mk_Frees "B" BTs
   137       ||>> mk_Frees "B" BTs
   138       ||>> mk_Frees "B'" B'Ts
   139       ||>> mk_Frees "B''" B''Ts
   140       ||>> mk_Frees "s" sTs
   141       ||>> mk_Frees "sums" sum_sTs
   142       ||>> mk_Frees "s'" s'Ts
   143       ||>> mk_Frees "s''" s''Ts
   144       ||>> mk_Frees "f" fTs
   145       ||>> mk_Frees "f" fTs
   146       ||>> mk_Frees "f" self_fTs
   147       ||>> mk_Frees "f" all_fTs
   148       ||>> mk_Frees "g" gTs
   149       ||>> mk_Frees "g" all_gTs
   150       ||>> mk_Frees "x" FTsAs
   151       ||>> mk_Frees "x" FTsAs
   152       ||>> mk_Frees "x" FRTs
   153       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Rtuple") all_sbisT
   154       ||>> mk_Frees' "rec" hrecTs
   155       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "n") HOLogic.natT
   156       ||>> mk_Frees "R" setRTs
   157       ||>> mk_Frees "R" setRTs
   158       ||>> mk_Frees "R'" setR'Ts
   159       ||>> mk_Frees "R" setsRTs
   160       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") idxT
   161       ||>> yield_singleton (mk_Frees "I") (HOLogic.mk_setT idxT)
   162       ||>> mk_Frees "Ri" (map (fn T => idxT --> T) setRTs)
   163       ||>> mk_Freess "K" (map (fn AT => map (fn T => T --> AT) activeAs) ATs);
   164 
   165     val passive_UNIVs = map HOLogic.mk_UNIV passiveAs;
   166     val passive_diags = map mk_diag As;
   167     val active_UNIVs = map HOLogic.mk_UNIV activeAs;
   168     val sum_UNIVs = map HOLogic.mk_UNIV sumBsAs;
   169     val passive_ids = map HOLogic.id_const passiveAs;
   170     val active_ids = map HOLogic.id_const activeAs;
   171     val Inls = map2 Inl_const activeBs activeAs;
   172     val fsts = map fst_const RTs;
   173     val snds = map snd_const RTs;
   174 
   175     (* thms *)
   176     val bd_card_orders = map bd_card_order_of_bnf bnfs;
   177     val bd_card_order = hd bd_card_orders
   178     val bd_Card_orders = map bd_Card_order_of_bnf bnfs;
   179     val bd_Card_order = hd bd_Card_orders;
   180     val bd_Cinfinites = map bd_Cinfinite_of_bnf bnfs;
   181     val bd_Cinfinite = hd bd_Cinfinites;
   182     val bd_Cnotzeros = map bd_Cnotzero_of_bnf bnfs;
   183     val bd_Cnotzero = hd bd_Cnotzeros;
   184     val in_bds = map in_bd_of_bnf bnfs;
   185     val in_monos = map in_mono_of_bnf bnfs;
   186     val map_comps = map map_comp_of_bnf bnfs;
   187     val map_comp's = map map_comp'_of_bnf bnfs;
   188     val map_congs = map map_cong_of_bnf bnfs;
   189     val map_id's = map map_id'_of_bnf bnfs;
   190     val pred_defs = map pred_def_of_bnf bnfs;
   191     val rel_congs = map rel_cong_of_bnf bnfs;
   192     val rel_converses = map rel_converse_of_bnf bnfs;
   193     val rel_defs = map rel_def_of_bnf bnfs;
   194     val rel_Grs = map rel_Gr_of_bnf bnfs;
   195     val rel_Ids = map rel_Id_of_bnf bnfs;
   196     val rel_monos = map rel_mono_of_bnf bnfs;
   197     val rel_Os = map rel_O_of_bnf bnfs;
   198     val map_wpulls = map map_wpull_of_bnf bnfs;
   199     val set_bdss = map set_bd_of_bnf bnfs;
   200     val set_natural'ss = map set_natural'_of_bnf bnfs;
   201 
   202     val timer = time (timer "Extracted terms & thms");
   203 
   204     (* derived thms *)
   205 
   206     (*map g1 ... gm g(m+1) ... g(m+n) (map id ... id f(m+1) ... f(m+n) x)=
   207       map g1 ... gm (g(m+1) o f(m+1)) ... (g(m+n) o f(m+n)) x*)
   208     fun mk_map_comp_id x mapAsBs mapBsCs mapAsCs map_comp =
   209       let
   210         val lhs = Term.list_comb (mapBsCs, all_gs) $
   211           (Term.list_comb (mapAsBs, passive_ids @ fs) $ x);
   212         val rhs =
   213           Term.list_comb (mapAsCs, take m all_gs @ map HOLogic.mk_comp (drop m all_gs ~~ fs)) $ x;
   214       in
   215         Skip_Proof.prove lthy [] []
   216           (fold_rev Logic.all (x :: fs @ all_gs) (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))))
   217           (K (mk_map_comp_id_tac map_comp))
   218       end;
   219 
   220     val map_comp_id_thms = map5 mk_map_comp_id xFs mapsAsBs mapsBsCs' mapsAsCs' map_comp's;
   221 
   222     (*forall a : set(m+1) x. f(m+1) a = a; ...; forall a : set(m+n) x. f(m+n) a = a ==>
   223       map id ... id f(m+1) ... f(m+n) x = x*)
   224     fun mk_map_congL x mapAsAs sets map_cong map_id' =
   225       let
   226         fun mk_prem set f z z' =
   227           HOLogic.mk_Trueprop
   228             (mk_Ball (set $ x) (Term.absfree z' (HOLogic.mk_eq (f $ z, z))));
   229         val prems = map4 mk_prem (drop m sets) self_fs zs zs';
   230         val goal = HOLogic.mk_Trueprop (HOLogic.mk_eq
   231          (Term.list_comb (mapAsAs, passive_ids @ self_fs) $ x, x))
   232       in
   233         Skip_Proof.prove lthy [] []
   234           (fold_rev Logic.all (x :: self_fs) (Logic.list_implies (prems, goal)))
   235           (K (mk_map_congL_tac m map_cong map_id'))
   236       end;
   237 
   238     val map_congL_thms = map5 mk_map_congL xFs mapsAsAs setssAs map_congs map_id's;
   239     val in_mono'_thms = map (fn thm =>
   240       (thm OF (replicate m subset_refl)) RS @{thm set_mp}) in_monos;
   241     val in_cong'_thms = map (fn bnf => in_cong_of_bnf bnf OF (replicate m refl)) bnfs;
   242 
   243     val map_arg_cong_thms =
   244       let
   245         val prems = map2 (fn x => fn y =>
   246           HOLogic.mk_Trueprop (HOLogic.mk_eq (x, y))) xFs xFs_copy;
   247         val maps = map (fn map => Term.list_comb (map, all_fs)) mapsAsBs;
   248         val concls = map3 (fn x => fn y => fn map =>
   249           HOLogic.mk_Trueprop (HOLogic.mk_eq (map $ x, map $ y))) xFs xFs_copy maps;
   250         val goals =
   251           map4 (fn prem => fn concl => fn x => fn y =>
   252             fold_rev Logic.all (x :: y :: all_fs) (Logic.mk_implies (prem, concl)))
   253           prems concls xFs xFs_copy;
   254       in
   255         map (fn goal => Skip_Proof.prove lthy [] [] goal
   256           (K ((hyp_subst_tac THEN' rtac refl) 1))) goals
   257       end;
   258 
   259     val timer = time (timer "Derived simple theorems");
   260 
   261     (* coalgebra *)
   262 
   263     val coalg_bind = Binding.suffix_name ("_" ^ coN ^ algN) b;
   264     val coalg_name = Binding.name_of coalg_bind;
   265     val coalg_def_bind = (Thm.def_binding coalg_bind, []);
   266 
   267     (*forall i = 1 ... n: (\<forall>x \<in> Bi. si \<in> Fi_in A1 .. Am B1 ... Bn)*)
   268     val coalg_spec =
   269       let
   270         val coalgT = Library.foldr (op -->) (ATs @ BTs @ sTs, HOLogic.boolT);
   271 
   272         val ins = map3 mk_in (replicate n (As @ Bs)) setssAs FTsAs;
   273         fun mk_coalg_conjunct B s X z z' =
   274           mk_Ball B (Term.absfree z' (HOLogic.mk_mem (s $ z, X)));
   275 
   276         val lhs = Term.list_comb (Free (coalg_name, coalgT), As @ Bs @ ss);
   277         val rhs = Library.foldr1 HOLogic.mk_conj (map5 mk_coalg_conjunct Bs ss ins zs zs')
   278       in
   279         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   280       end;
   281 
   282     val ((coalg_free, (_, coalg_def_free)), (lthy, lthy_old)) =
   283         lthy
   284         |> Specification.definition (SOME (coalg_bind, NONE, NoSyn), (coalg_def_bind, coalg_spec))
   285         ||> `Local_Theory.restore;
   286 
   287     (*transforms defined frees into consts*)
   288     val phi = Proof_Context.export_morphism lthy_old lthy;
   289     val coalg = fst (Term.dest_Const (Morphism.term phi coalg_free));
   290     val coalg_def = Morphism.thm phi coalg_def_free;
   291 
   292     fun mk_coalg As Bs ss =
   293       let
   294         val args = As @ Bs @ ss;
   295         val Ts = map fastype_of args;
   296         val coalgT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   297       in
   298         Term.list_comb (Const (coalg, coalgT), args)
   299       end;
   300 
   301     val coalg_prem = HOLogic.mk_Trueprop (mk_coalg As Bs ss);
   302 
   303     val coalg_in_thms = map (fn i =>
   304       coalg_def RS @{thm subst[of _ _ "%x. x"]} RS mk_conjunctN n i RS bspec) ks
   305 
   306     val coalg_set_thmss =
   307       let
   308         val coalg_prem = HOLogic.mk_Trueprop (mk_coalg As Bs ss);
   309         fun mk_prem x B = HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B));
   310         fun mk_concl s x B set = HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) B);
   311         val prems = map2 mk_prem zs Bs;
   312         val conclss = map3 (fn s => fn x => fn sets => map2 (mk_concl s x) (As @ Bs) sets)
   313           ss zs setssAs;
   314         val goalss = map3 (fn x => fn prem => fn concls => map (fn concl =>
   315           fold_rev Logic.all (x :: As @ Bs @ ss)
   316             (Logic.list_implies (coalg_prem :: [prem], concl))) concls) zs prems conclss;
   317       in
   318         map (fn goals => map (fn goal => Skip_Proof.prove lthy [] [] goal
   319           (K (mk_coalg_set_tac coalg_def))) goals) goalss
   320       end;
   321 
   322     val coalg_set_thmss' = transpose coalg_set_thmss;
   323 
   324     fun mk_tcoalg ATs BTs = mk_coalg (map HOLogic.mk_UNIV ATs) (map HOLogic.mk_UNIV BTs);
   325 
   326     val tcoalg_thm =
   327       let
   328         val goal = fold_rev Logic.all ss
   329           (HOLogic.mk_Trueprop (mk_tcoalg passiveAs activeAs ss))
   330       in
   331         Skip_Proof.prove lthy [] [] goal
   332           (K (stac coalg_def 1 THEN CONJ_WRAP
   333             (K (EVERY' [rtac ballI, rtac CollectI,
   334               CONJ_WRAP' (K (EVERY' [rtac @{thm subset_UNIV}])) allAs] 1)) ss))
   335       end;
   336 
   337     val timer = time (timer "Coalgebra definition & thms");
   338 
   339     (* morphism *)
   340 
   341     val mor_bind = Binding.suffix_name ("_" ^ morN) b;
   342     val mor_name = Binding.name_of mor_bind;
   343     val mor_def_bind = (Thm.def_binding mor_bind, []);
   344 
   345     (*fbetw) forall i = 1 ... n: (\<forall>x \<in> Bi. fi x \<in> B'i)*)
   346     (*mor) forall i = 1 ... n: (\<forall>x \<in> Bi.
   347        Fi_map id ... id f1 ... fn (si x) = si' (fi x)*)
   348     val mor_spec =
   349       let
   350         val morT = Library.foldr (op -->) (BTs @ sTs @ B'Ts @ s'Ts @ fTs, HOLogic.boolT);
   351 
   352         fun mk_fbetw f B1 B2 z z' =
   353           mk_Ball B1 (Term.absfree z' (HOLogic.mk_mem (f $ z, B2)));
   354         fun mk_mor B mapAsBs f s s' z z' =
   355           mk_Ball B (Term.absfree z' (HOLogic.mk_eq
   356             (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ z]), s' $ (f $ z))));
   357         val lhs = Term.list_comb (Free (mor_name, morT), Bs @ ss @ B's @ s's @ fs);
   358         val rhs = HOLogic.mk_conj
   359           (Library.foldr1 HOLogic.mk_conj (map5 mk_fbetw fs Bs B's zs zs'),
   360            Library.foldr1 HOLogic.mk_conj (map7 mk_mor Bs mapsAsBs fs ss s's zs zs'))
   361       in
   362         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   363       end;
   364 
   365     val ((mor_free, (_, mor_def_free)), (lthy, lthy_old)) =
   366         lthy
   367         |> Specification.definition (SOME (mor_bind, NONE, NoSyn), (mor_def_bind, mor_spec))
   368         ||> `Local_Theory.restore;
   369 
   370     (*transforms defined frees into consts*)
   371     val phi = Proof_Context.export_morphism lthy_old lthy;
   372     val mor = fst (Term.dest_Const (Morphism.term phi mor_free));
   373     val mor_def = Morphism.thm phi mor_def_free;
   374 
   375     fun mk_mor Bs1 ss1 Bs2 ss2 fs =
   376       let
   377         val args = Bs1 @ ss1 @ Bs2 @ ss2 @ fs;
   378         val Ts = map fastype_of (Bs1 @ ss1 @ Bs2 @ ss2 @ fs);
   379         val morT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   380       in
   381         Term.list_comb (Const (mor, morT), args)
   382       end;
   383 
   384     val mor_prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   385 
   386     val (mor_image_thms, morE_thms) =
   387       let
   388         val prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
   389         fun mk_image_goal f B1 B2 = fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs)
   390           (Logic.mk_implies (prem, HOLogic.mk_Trueprop (mk_subset (mk_image f $ B1) B2)));
   391         val image_goals = map3 mk_image_goal fs Bs B's;
   392         fun mk_elim_goal B mapAsBs f s s' x =
   393           fold_rev Logic.all (x :: Bs @ ss @ B's @ s's @ fs)
   394             (Logic.list_implies ([prem, HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B))],
   395               HOLogic.mk_Trueprop (HOLogic.mk_eq
   396                (Term.list_comb (mapAsBs, passive_ids @ fs @ [s $ x]), s' $ (f $ x)))));
   397         val elim_goals = map6 mk_elim_goal Bs mapsAsBs fs ss s's zs;
   398         fun prove goal =
   399           Skip_Proof.prove lthy [] [] goal (K (mk_mor_elim_tac mor_def));
   400       in
   401         (map prove image_goals, map prove elim_goals)
   402       end;
   403 
   404     val mor_image'_thms = map (fn thm => @{thm set_mp} OF [thm, imageI]) mor_image_thms;
   405 
   406     val mor_incl_thm =
   407       let
   408         val prems = map2 (HOLogic.mk_Trueprop oo mk_subset) Bs Bs_copy;
   409         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss Bs_copy ss active_ids);
   410       in
   411         Skip_Proof.prove lthy [] []
   412           (fold_rev Logic.all (Bs @ ss @ Bs_copy) (Logic.list_implies (prems, concl)))
   413           (K (mk_mor_incl_tac mor_def map_id's))
   414       end;
   415 
   416     val mor_id_thm = mor_incl_thm OF (replicate n subset_refl);
   417 
   418     val mor_comp_thm =
   419       let
   420         val prems =
   421           [HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs),
   422            HOLogic.mk_Trueprop (mk_mor B's s's B''s s''s gs)];
   423         val concl =
   424           HOLogic.mk_Trueprop (mk_mor Bs ss B''s s''s (map2 (curry HOLogic.mk_comp) gs fs));
   425       in
   426         Skip_Proof.prove lthy [] []
   427           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ B''s @ s''s @ fs @ gs)
   428             (Logic.list_implies (prems, concl)))
   429           (K (mk_mor_comp_tac mor_def mor_image'_thms morE_thms map_comp_id_thms))
   430       end;
   431 
   432     val mor_cong_thm =
   433       let
   434         val prems = map HOLogic.mk_Trueprop
   435          (map2 (curry HOLogic.mk_eq) fs_copy fs @ [mk_mor Bs ss B's s's fs])
   436         val concl = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs_copy);
   437       in
   438         Skip_Proof.prove lthy [] []
   439           (fold_rev Logic.all (Bs @ ss @ B's @ s's @ fs @ fs_copy)
   440             (Logic.list_implies (prems, concl)))
   441           (K ((hyp_subst_tac THEN' atac) 1))
   442       end;
   443 
   444     val mor_UNIV_thm =
   445       let
   446         fun mk_conjunct mapAsBs f s s' = HOLogic.mk_eq
   447             (HOLogic.mk_comp (Term.list_comb (mapAsBs, passive_ids @ fs), s),
   448             HOLogic.mk_comp (s', f));
   449         val lhs = mk_mor active_UNIVs ss (map HOLogic.mk_UNIV activeBs) s's fs;
   450         val rhs = Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct mapsAsBs fs ss s's);
   451       in
   452         Skip_Proof.prove lthy [] []
   453           (fold_rev Logic.all (ss @ s's @ fs) (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))))
   454           (K (mk_mor_UNIV_tac morE_thms mor_def))
   455       end;
   456 
   457     val mor_str_thm =
   458       let
   459         val maps = map2 (fn Ds => fn bnf => Term.list_comb
   460           (mk_map_of_bnf Ds allAs (passiveAs @ FTsAs) bnf, passive_ids @ ss)) Dss bnfs;
   461       in
   462         Skip_Proof.prove lthy [] []
   463           (fold_rev Logic.all ss (HOLogic.mk_Trueprop
   464             (mk_mor active_UNIVs ss (map HOLogic.mk_UNIV FTsAs) maps ss)))
   465           (K (mk_mor_str_tac ks mor_UNIV_thm))
   466       end;
   467 
   468     val mor_sum_case_thm =
   469       let
   470         val maps = map3 (fn s => fn sum_s => fn map =>
   471           mk_sum_case (HOLogic.mk_comp (Term.list_comb (map, passive_ids @ Inls), s)) sum_s)
   472           s's sum_ss map_Inls;
   473       in
   474         Skip_Proof.prove lthy [] []
   475           (fold_rev Logic.all (s's @ sum_ss) (HOLogic.mk_Trueprop
   476             (mk_mor (map HOLogic.mk_UNIV activeBs) s's sum_UNIVs maps Inls)))
   477           (K (mk_mor_sum_case_tac ks mor_UNIV_thm))
   478       end;
   479 
   480     val timer = time (timer "Morphism definition & thms");
   481 
   482     fun hset_rec_bind j = Binding.suffix_name ("_" ^ hset_recN ^ (if m = 1 then "" else
   483       string_of_int j)) b;
   484     val hset_rec_name = Binding.name_of o hset_rec_bind;
   485     val hset_rec_def_bind = rpair [] o Thm.def_binding o hset_rec_bind;
   486 
   487     fun hset_rec_spec j Zero hsetT hrec hrec' =
   488       let
   489         fun mk_Suc s setsAs z z' =
   490           let
   491             val (set, sets) = apfst (fn xs => nth xs (j - 1)) (chop m setsAs);
   492             fun mk_UN set k = mk_UNION (set $ (s $ z)) (mk_nthN n hrec k);
   493           in
   494             Term.absfree z'
   495               (mk_union (set $ (s $ z), Library.foldl1 mk_union (map2 mk_UN sets ks)))
   496           end;
   497 
   498         val Suc = Term.absdummy HOLogic.natT (Term.absfree hrec'
   499           (HOLogic.mk_tuple (map4 mk_Suc ss setssAs zs zs')));
   500 
   501         val lhs = Term.list_comb (Free (hset_rec_name j, hsetT), ss);
   502         val rhs = mk_nat_rec Zero Suc;
   503       in
   504         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   505       end;
   506 
   507     val ((hset_rec_frees, (_, hset_rec_def_frees)), (lthy, lthy_old)) =
   508       lthy
   509       |> fold_map5 (fn j => fn Zero => fn hsetT => fn hrec => fn hrec' => Specification.definition
   510         (SOME (hset_rec_bind j, NONE, NoSyn),
   511           (hset_rec_def_bind j, hset_rec_spec j Zero hsetT hrec hrec')))
   512         ls Zeros hsetTs hrecs hrecs'
   513       |>> apsnd split_list o split_list
   514       ||> `Local_Theory.restore;
   515 
   516     (*transforms defined frees into consts*)
   517     val phi = Proof_Context.export_morphism lthy_old lthy;
   518 
   519     val hset_rec_defs = map (Morphism.thm phi) hset_rec_def_frees;
   520     val hset_recs = map (fst o Term.dest_Const o Morphism.term phi) hset_rec_frees;
   521 
   522     fun mk_hset_rec ss nat i j T =
   523       let
   524         val args = ss @ [nat];
   525         val Ts = map fastype_of ss;
   526         val bTs = map domain_type Ts;
   527         val hrecT = HOLogic.mk_tupleT (map (fn U => U --> HOLogic.mk_setT T) bTs)
   528         val hset_recT = Library.foldr (op -->) (Ts, HOLogic.natT --> hrecT);
   529       in
   530         mk_nthN n (Term.list_comb (Const (nth hset_recs (j - 1), hset_recT), args)) i
   531       end;
   532 
   533     val hset_rec_0ss = mk_rec_simps n @{thm nat_rec_0} hset_rec_defs;
   534     val hset_rec_Sucss = mk_rec_simps n @{thm nat_rec_Suc} hset_rec_defs;
   535     val hset_rec_0ss' = transpose hset_rec_0ss;
   536     val hset_rec_Sucss' = transpose hset_rec_Sucss;
   537 
   538     fun hset_bind i j = Binding.suffix_name ("_" ^ hsetN ^
   539       (if m = 1 then "" else string_of_int j)) (nth bs (i - 1));
   540     val hset_name = Binding.name_of oo hset_bind;
   541     val hset_def_bind = rpair [] o Thm.def_binding oo hset_bind;
   542 
   543     fun hset_spec i j =
   544       let
   545         val U = nth activeAs (i - 1);
   546         val z = nth zs (i - 1);
   547         val T = nth passiveAs (j - 1);
   548         val setT = HOLogic.mk_setT T;
   549         val hsetT = Library.foldr (op -->) (sTs, U --> setT);
   550 
   551         val lhs = Term.list_comb (Free (hset_name i j, hsetT), ss @ [z]);
   552         val rhs = mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
   553           (Term.absfree nat' (mk_hset_rec ss nat i j T $ z));
   554       in
   555         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   556       end;
   557 
   558     val ((hset_frees, (_, hset_def_frees)), (lthy, lthy_old)) =
   559       lthy
   560       |> fold_map (fn i => fold_map (fn j => Specification.definition
   561         (SOME (hset_bind i j, NONE, NoSyn), (hset_def_bind i j, hset_spec i j))) ls) ks
   562       |>> map (apsnd split_list o split_list)
   563       |>> apsnd split_list o split_list
   564       ||> `Local_Theory.restore;
   565 
   566     (*transforms defined frees into consts*)
   567     val phi = Proof_Context.export_morphism lthy_old lthy;
   568 
   569     val hset_defss = map (map (Morphism.thm phi)) hset_def_frees;
   570     val hset_defss' = transpose hset_defss;
   571     val hset_namess = map (map (fst o Term.dest_Const o Morphism.term phi)) hset_frees;
   572 
   573     fun mk_hset ss i j T =
   574       let
   575         val Ts = map fastype_of ss;
   576         val bTs = map domain_type Ts;
   577         val hsetT = Library.foldr (op -->) (Ts, nth bTs (i - 1) --> HOLogic.mk_setT T);
   578       in
   579         Term.list_comb (Const (nth (nth hset_namess (i - 1)) (j - 1), hsetT), ss)
   580       end;
   581 
   582     val hsetssAs = map (fn i => map2 (mk_hset ss i) ls passiveAs) ks;
   583 
   584     val (set_incl_hset_thmss, set_hset_incl_hset_thmsss) =
   585       let
   586         fun mk_set_incl_hset s x set hset = fold_rev Logic.all (x :: ss)
   587           (HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (hset $ x)));
   588 
   589         fun mk_set_hset_incl_hset s x y set hset1 hset2 =
   590           fold_rev Logic.all (x :: y :: ss)
   591             (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (x, set $ (s $ y))),
   592             HOLogic.mk_Trueprop (mk_subset (hset1 $ x) (hset2 $ y))));
   593 
   594         val set_incl_hset_goalss =
   595           map4 (fn s => fn x => fn sets => fn hsets =>
   596             map2 (mk_set_incl_hset s x) (take m sets) hsets)
   597           ss zs setssAs hsetssAs;
   598 
   599         (*xk : F(i)set(m+k) (si yi) ==> F(k)_hset(j) s1 ... sn xk <= F(i)_hset(j) s1 ... sn yi*)
   600         val set_hset_incl_hset_goalsss =
   601           map4 (fn si => fn yi => fn sets => fn hsetsi =>
   602             map3 (fn xk => fn set => fn hsetsk =>
   603               map2 (mk_set_hset_incl_hset si xk yi set) hsetsk hsetsi)
   604             zs_copy (drop m sets) hsetssAs)
   605           ss zs setssAs hsetssAs;
   606       in
   607         (map3 (fn goals => fn defs => fn rec_Sucs =>
   608           map3 (fn goal => fn def => fn rec_Suc =>
   609             Skip_Proof.prove lthy [] [] goal
   610               (K (mk_set_incl_hset_tac def rec_Suc)))
   611           goals defs rec_Sucs)
   612         set_incl_hset_goalss hset_defss hset_rec_Sucss,
   613         map3 (fn goalss => fn defsi => fn rec_Sucs =>
   614           map3 (fn k => fn goals => fn defsk =>
   615             map4 (fn goal => fn defk => fn defi => fn rec_Suc =>
   616               Skip_Proof.prove lthy [] [] goal
   617                 (K (mk_set_hset_incl_hset_tac n [defk, defi] rec_Suc k)))
   618             goals defsk defsi rec_Sucs)
   619           ks goalss hset_defss)
   620         set_hset_incl_hset_goalsss hset_defss hset_rec_Sucss)
   621       end;
   622 
   623     val set_incl_hset_thmss' = transpose set_incl_hset_thmss;
   624     val set_hset_incl_hset_thmsss' = transpose (map transpose set_hset_incl_hset_thmsss);
   625     val set_hset_incl_hset_thmsss'' = map transpose set_hset_incl_hset_thmsss';
   626     val set_hset_thmss = map (map (fn thm => thm RS @{thm set_mp})) set_incl_hset_thmss;
   627     val set_hset_hset_thmsss = map (map (map (fn thm => thm RS @{thm set_mp})))
   628       set_hset_incl_hset_thmsss;
   629     val set_hset_thmss' = transpose set_hset_thmss;
   630     val set_hset_hset_thmsss' = transpose (map transpose set_hset_hset_thmsss);
   631 
   632     val set_incl_hin_thmss =
   633       let
   634         fun mk_set_incl_hin s x hsets1 set hsets2 T =
   635           fold_rev Logic.all (x :: ss @ As)
   636             (Logic.list_implies
   637               (map2 (fn hset => fn A => HOLogic.mk_Trueprop (mk_subset (hset $ x) A)) hsets1 As,
   638               HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (mk_in As hsets2 T))));
   639 
   640         val set_incl_hin_goalss =
   641           map4 (fn s => fn x => fn sets => fn hsets =>
   642             map3 (mk_set_incl_hin s x hsets) (drop m sets) hsetssAs activeAs)
   643           ss zs setssAs hsetssAs;
   644       in
   645         map2 (map2 (fn goal => fn thms =>
   646           Skip_Proof.prove lthy [] [] goal (K (mk_set_incl_hin_tac thms))))
   647         set_incl_hin_goalss set_hset_incl_hset_thmsss
   648       end;
   649 
   650     val hset_minimal_thms =
   651       let
   652         fun mk_passive_prem set s x K =
   653           Logic.all x (HOLogic.mk_Trueprop (mk_subset (set $ (s $ x)) (K $ x)));
   654 
   655         fun mk_active_prem s x1 K1 set x2 K2 =
   656           fold_rev Logic.all [x1, x2]
   657             (Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_mem (x2, set $ (s $ x1))),
   658               HOLogic.mk_Trueprop (mk_subset (K2 $ x2) (K1 $ x1))));
   659 
   660         val premss = map2 (fn j => fn Ks =>
   661           map4 mk_passive_prem (map (fn xs => nth xs (j - 1)) setssAs) ss zs Ks @
   662             flat (map4 (fn sets => fn s => fn x1 => fn K1 =>
   663               map3 (mk_active_prem s x1 K1) (drop m sets) zs_copy Ks) setssAs ss zs Ks))
   664           ls Kss;
   665 
   666         val hset_rec_minimal_thms =
   667           let
   668             fun mk_conjunct j T i K x = mk_subset (mk_hset_rec ss nat i j T $ x) (K $ x);
   669             fun mk_concl j T Ks = list_all_free zs
   670               (Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T) ks Ks zs));
   671             val concls = map3 mk_concl ls passiveAs Kss;
   672 
   673             val goals = map2 (fn prems => fn concl =>
   674               Logic.list_implies (prems, HOLogic.mk_Trueprop concl)) premss concls
   675 
   676             val ctss =
   677               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
   678           in
   679             map4 (fn goal => fn cts => fn hset_rec_0s => fn hset_rec_Sucs =>
   680               singleton (Proof_Context.export names_lthy lthy)
   681                 (Skip_Proof.prove lthy [] [] goal
   682                   (mk_hset_rec_minimal_tac m cts hset_rec_0s hset_rec_Sucs)))
   683             goals ctss hset_rec_0ss' hset_rec_Sucss'
   684           end;
   685 
   686         fun mk_conjunct j T i K x = mk_subset (mk_hset ss i j T $ x) (K $ x);
   687         fun mk_concl j T Ks = 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 = map3 (fn Ks => fn prems => fn concl =>
   691           fold_rev Logic.all (Ks @ ss @ zs)
   692             (Logic.list_implies (prems, HOLogic.mk_Trueprop concl))) Kss premss concls;
   693       in
   694         map3 (fn goal => fn hset_defs => fn hset_rec_minimal =>
   695           Skip_Proof.prove lthy [] [] goal
   696             (mk_hset_minimal_tac n hset_defs hset_rec_minimal))
   697         goals hset_defss' hset_rec_minimal_thms
   698       end;
   699 
   700     val mor_hset_thmss =
   701       let
   702         val mor_hset_rec_thms =
   703           let
   704             fun mk_conjunct j T i f x B =
   705               HOLogic.mk_imp (HOLogic.mk_mem (x, B), HOLogic.mk_eq
   706                (mk_hset_rec s's nat i j T $ (f $ x), mk_hset_rec ss nat i j T $ x));
   707 
   708             fun mk_concl j T = list_all_free zs
   709               (Library.foldr1 HOLogic.mk_conj (map4 (mk_conjunct j T) ks fs zs Bs));
   710             val concls = map2 mk_concl ls passiveAs;
   711 
   712             val ctss =
   713               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
   714 
   715             val goals = map (fn concl =>
   716               Logic.list_implies ([coalg_prem, mor_prem], HOLogic.mk_Trueprop concl)) concls;
   717           in
   718             map5 (fn j => fn goal => fn cts => fn hset_rec_0s => fn hset_rec_Sucs =>
   719               singleton (Proof_Context.export names_lthy lthy)
   720                 (Skip_Proof.prove lthy [] [] goal
   721                   (K (mk_mor_hset_rec_tac m n cts j hset_rec_0s hset_rec_Sucs
   722                     morE_thms set_natural'ss coalg_set_thmss))))
   723             ls goals ctss hset_rec_0ss' hset_rec_Sucss'
   724           end;
   725 
   726         val mor_hset_rec_thmss = map (fn thm => map (fn i =>
   727           mk_specN n thm RS mk_conjunctN n i RS mp) ks) mor_hset_rec_thms;
   728 
   729         fun mk_prem x B = HOLogic.mk_Trueprop (HOLogic.mk_mem (x, B));
   730 
   731         fun mk_concl j T i f x = HOLogic.mk_Trueprop (HOLogic.mk_eq
   732           (mk_hset s's i j T $ (f $ x), mk_hset ss i j T $ x));
   733 
   734         val goalss = map2 (fn j => fn T => map4 (fn i => fn f => fn x => fn B =>
   735           fold_rev Logic.all (x :: As @ Bs @ ss @ B's @ s's @ fs)
   736             (Logic.list_implies ([coalg_prem, mor_prem,
   737               mk_prem x B], mk_concl j T i f x))) ks fs zs Bs) ls passiveAs;
   738       in
   739         map3 (map3 (fn goal => fn hset_def => fn mor_hset_rec =>
   740           Skip_Proof.prove lthy [] [] goal
   741             (K (mk_mor_hset_tac hset_def mor_hset_rec))))
   742         goalss hset_defss' mor_hset_rec_thmss
   743       end;
   744 
   745     val timer = time (timer "Hereditary sets");
   746 
   747     (* bisimulation *)
   748 
   749     val bis_bind = Binding.suffix_name ("_" ^ bisN) b;
   750     val bis_name = Binding.name_of bis_bind;
   751     val bis_def_bind = (Thm.def_binding bis_bind, []);
   752 
   753     fun mk_bis_le_conjunct R B1 B2 = mk_subset R (mk_Times (B1, B2));
   754     val bis_le = Library.foldr1 HOLogic.mk_conj (map3 mk_bis_le_conjunct Rs Bs B's)
   755 
   756     val bis_spec =
   757       let
   758         val bisT = Library.foldr (op -->) (ATs @ BTs @ sTs @ B'Ts @ s'Ts @ setRTs, HOLogic.boolT);
   759 
   760         val fst_args = passive_ids @ fsts;
   761         val snd_args = passive_ids @ snds;
   762         fun mk_bis R s s' b1 b2 RF map1 map2 sets =
   763           list_all_free [b1, b2] (HOLogic.mk_imp
   764             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   765             mk_Bex (mk_in (As @ Rs) sets (snd (dest_Free RF))) (Term.absfree (dest_Free RF)
   766               (HOLogic.mk_conj
   767                 (HOLogic.mk_eq (Term.list_comb (map1, fst_args) $ RF, s $ b1),
   768                 HOLogic.mk_eq (Term.list_comb (map2, snd_args) $ RF, s' $ b2))))));
   769 
   770         val lhs = Term.list_comb (Free (bis_name, bisT), As @ Bs @ ss @ B's @ s's @ Rs);
   771         val rhs = HOLogic.mk_conj
   772           (bis_le, Library.foldr1 HOLogic.mk_conj
   773             (map9 mk_bis Rs ss s's zs z's RFs map_fsts map_snds bis_setss))
   774       in
   775         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   776       end;
   777 
   778     val ((bis_free, (_, bis_def_free)), (lthy, lthy_old)) =
   779         lthy
   780         |> Specification.definition (SOME (bis_bind, NONE, NoSyn), (bis_def_bind, bis_spec))
   781         ||> `Local_Theory.restore;
   782 
   783     (*transforms defined frees into consts*)
   784     val phi = Proof_Context.export_morphism lthy_old lthy;
   785     val bis = fst (Term.dest_Const (Morphism.term phi bis_free));
   786     val bis_def = Morphism.thm phi bis_def_free;
   787 
   788     fun mk_bis As Bs1 ss1 Bs2 ss2 Rs =
   789       let
   790         val args = As @ Bs1 @ ss1 @ Bs2 @ ss2 @ Rs;
   791         val Ts = map fastype_of args;
   792         val bisT = Library.foldr (op -->) (Ts, HOLogic.boolT);
   793       in
   794         Term.list_comb (Const (bis, bisT), args)
   795       end;
   796 
   797     val bis_cong_thm =
   798       let
   799         val prems = map HOLogic.mk_Trueprop
   800          (mk_bis As Bs ss B's s's Rs :: map2 (curry HOLogic.mk_eq) Rs_copy Rs)
   801         val concl = HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs_copy);
   802       in
   803         Skip_Proof.prove lthy [] []
   804           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs @ Rs_copy)
   805             (Logic.list_implies (prems, concl)))
   806           (K ((hyp_subst_tac THEN' atac) 1))
   807       end;
   808 
   809     val bis_rel_thm =
   810       let
   811         fun mk_conjunct R s s' b1 b2 rel =
   812           list_all_free [b1, b2] (HOLogic.mk_imp
   813             (HOLogic.mk_mem (HOLogic.mk_prod (b1, b2), R),
   814             HOLogic.mk_mem (HOLogic.mk_prod (s $ b1, s' $ b2),
   815               Term.list_comb (rel, passive_diags @ Rs))));
   816 
   817         val rhs = HOLogic.mk_conj
   818           (bis_le, Library.foldr1 HOLogic.mk_conj
   819             (map6 mk_conjunct Rs ss s's zs z's relsAsBs))
   820       in
   821         Skip_Proof.prove lthy [] []
   822           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs)
   823             (HOLogic.mk_Trueprop (HOLogic.mk_eq (mk_bis As Bs ss B's s's Rs, rhs))))
   824           (K (mk_bis_rel_tac m bis_def rel_defs map_comp's map_congs set_natural'ss))
   825       end;
   826 
   827     val bis_converse_thm =
   828       Skip_Proof.prove lthy [] []
   829         (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ Rs)
   830           (Logic.mk_implies
   831             (HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs),
   832             HOLogic.mk_Trueprop (mk_bis As B's s's Bs ss (map mk_converse Rs)))))
   833       (K (mk_bis_converse_tac m bis_rel_thm rel_congs rel_converses));
   834 
   835     val bis_O_thm =
   836       let
   837         val prems =
   838           [HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's Rs),
   839            HOLogic.mk_Trueprop (mk_bis As B's s's B''s s''s R's)];
   840         val concl =
   841           HOLogic.mk_Trueprop (mk_bis As Bs ss B''s s''s (map2 (curry mk_rel_comp) Rs R's));
   842       in
   843         Skip_Proof.prove lthy [] []
   844           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ B''s @ s''s @ Rs @ R's)
   845             (Logic.list_implies (prems, concl)))
   846           (K (mk_bis_O_tac m bis_rel_thm rel_congs rel_Os))
   847       end;
   848 
   849     val bis_Gr_thm =
   850       let
   851         val concl =
   852           HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's (map2 mk_Gr Bs fs));
   853       in
   854         Skip_Proof.prove lthy [] []
   855           (fold_rev Logic.all (As @ Bs @ ss @ B's @ s's @ fs)
   856             (Logic.list_implies ([coalg_prem, mor_prem], concl)))
   857           (mk_bis_Gr_tac bis_rel_thm rel_Grs mor_image_thms morE_thms coalg_in_thms)
   858       end;
   859 
   860     val bis_image2_thm = bis_cong_thm OF
   861       ((bis_O_thm OF [bis_Gr_thm RS bis_converse_thm, bis_Gr_thm]) ::
   862       replicate n @{thm image2_Gr});
   863 
   864     val bis_diag_thm = bis_cong_thm OF ((mor_id_thm RSN (2, bis_Gr_thm)) ::
   865       replicate n @{thm diag_Gr});
   866 
   867     val bis_Union_thm =
   868       let
   869         val prem =
   870           HOLogic.mk_Trueprop (mk_Ball Idx
   871             (Term.absfree idx' (mk_bis As Bs ss B's s's (map (fn R => R $ idx) Ris))));
   872         val concl =
   873           HOLogic.mk_Trueprop (mk_bis As Bs ss B's s's (map (mk_UNION Idx) Ris));
   874       in
   875         Skip_Proof.prove lthy [] []
   876           (fold_rev Logic.all (Idx :: As @ Bs @ ss @ B's @ s's @ Ris)
   877             (Logic.mk_implies (prem, concl)))
   878           (mk_bis_Union_tac bis_def in_mono'_thms)
   879       end;
   880 
   881     (* self-bisimulation *)
   882 
   883     fun mk_sbis As Bs ss Rs = mk_bis As Bs ss Bs ss Rs;
   884 
   885     val sbis_prem = HOLogic.mk_Trueprop (mk_sbis As Bs ss sRs);
   886 
   887     (* largest self-bisimulation *)
   888 
   889     fun lsbis_bind i = Binding.suffix_name ("_" ^ lsbisN ^ (if n = 1 then "" else
   890       string_of_int i)) b;
   891     val lsbis_name = Binding.name_of o lsbis_bind;
   892     val lsbis_def_bind = rpair [] o Thm.def_binding o lsbis_bind;
   893 
   894     val all_sbis = HOLogic.mk_Collect (fst Rtuple', snd Rtuple', list_exists_free sRs
   895       (HOLogic.mk_conj (HOLogic.mk_eq (Rtuple, HOLogic.mk_tuple sRs), mk_sbis As Bs ss sRs)));
   896 
   897     fun lsbis_spec i RT =
   898       let
   899         fun mk_lsbisT RT =
   900           Library.foldr (op -->) (map fastype_of (As @ Bs @ ss), RT);
   901         val lhs = Term.list_comb (Free (lsbis_name i, mk_lsbisT RT), As @ Bs @ ss);
   902         val rhs = mk_UNION all_sbis (Term.absfree Rtuple' (mk_nthN n Rtuple i));
   903       in
   904         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
   905       end;
   906 
   907     val ((lsbis_frees, (_, lsbis_def_frees)), (lthy, lthy_old)) =
   908       lthy
   909       |> fold_map2 (fn i => fn RT => Specification.definition
   910         (SOME (lsbis_bind i, NONE, NoSyn), (lsbis_def_bind i, lsbis_spec i RT))) ks setsRTs
   911       |>> apsnd split_list o split_list
   912       ||> `Local_Theory.restore;
   913 
   914     (*transforms defined frees into consts*)
   915     val phi = Proof_Context.export_morphism lthy_old lthy;
   916 
   917     val lsbis_defs = map (Morphism.thm phi) lsbis_def_frees;
   918     val lsbiss = map (fst o Term.dest_Const o Morphism.term phi) lsbis_frees;
   919 
   920     fun mk_lsbis As Bs ss i =
   921       let
   922         val args = As @ Bs @ ss;
   923         val Ts = map fastype_of args;
   924         val RT = mk_relT (`I (HOLogic.dest_setT (fastype_of (nth Bs (i - 1)))));
   925         val lsbisT = Library.foldr (op -->) (Ts, RT);
   926       in
   927         Term.list_comb (Const (nth lsbiss (i - 1), lsbisT), args)
   928       end;
   929 
   930     val sbis_lsbis_thm =
   931       Skip_Proof.prove lthy [] []
   932         (fold_rev Logic.all (As @ Bs @ ss)
   933           (HOLogic.mk_Trueprop (mk_sbis As Bs ss (map (mk_lsbis As Bs ss) ks))))
   934         (K (mk_sbis_lsbis_tac lsbis_defs bis_Union_thm bis_cong_thm));
   935 
   936     val lsbis_incl_thms = map (fn i => sbis_lsbis_thm RS
   937       (bis_def RS @{thm subst[of _ _ "%x. x"]} RS conjunct1 RS mk_conjunctN n i)) ks;
   938     val lsbisE_thms = map (fn i => (mk_specN 2 (sbis_lsbis_thm RS
   939       (bis_def RS @{thm subst[of _ _ "%x. x"]} RS conjunct2 RS mk_conjunctN n i))) RS mp) ks;
   940 
   941     val incl_lsbis_thms =
   942       let
   943         fun mk_concl i R = HOLogic.mk_Trueprop (mk_subset R (mk_lsbis As Bs ss i));
   944         val goals = map2 (fn i => fn R => fold_rev Logic.all (As @ Bs @ ss @ sRs)
   945           (Logic.mk_implies (sbis_prem, mk_concl i R))) ks sRs;
   946       in
   947         map3 (fn goal => fn i => fn def => Skip_Proof.prove lthy [] [] goal
   948           (K (mk_incl_lsbis_tac n i def))) goals ks lsbis_defs
   949       end;
   950 
   951     val equiv_lsbis_thms =
   952       let
   953         fun mk_concl i B = HOLogic.mk_Trueprop (mk_equiv B (mk_lsbis As Bs ss i));
   954         val goals = map2 (fn i => fn B => fold_rev Logic.all (As @ Bs @ ss)
   955           (Logic.mk_implies (coalg_prem, mk_concl i B))) ks Bs;
   956       in
   957         map3 (fn goal => fn l_incl => fn incl_l =>
   958           Skip_Proof.prove lthy [] [] goal
   959             (K (mk_equiv_lsbis_tac sbis_lsbis_thm l_incl incl_l
   960               bis_diag_thm bis_converse_thm bis_O_thm)))
   961         goals lsbis_incl_thms incl_lsbis_thms
   962       end;
   963 
   964     val timer = time (timer "Bisimulations");
   965 
   966     (* bounds *)
   967 
   968     val (lthy, sbd, sbdT,
   969       sbd_card_order, sbd_Cinfinite, sbd_Cnotzero, sbd_Card_order, set_sbdss, in_sbds) =
   970       if n = 1
   971       then (lthy, sum_bd, sum_bdT,
   972         bd_card_order, bd_Cinfinite, bd_Cnotzero, bd_Card_order, set_bdss, in_bds)
   973       else
   974         let
   975           val sbdT_bind = Binding.suffix_name ("_" ^ sum_bdTN) b;
   976 
   977           val ((sbdT_name, (sbdT_glob_info, sbdT_loc_info)), lthy) =
   978             typedef true NONE (sbdT_bind, params, NoSyn)
   979               (HOLogic.mk_UNIV sum_bdT) NONE (EVERY' [rtac exI, rtac UNIV_I] 1) lthy;
   980 
   981           val sbdT = Type (sbdT_name, params');
   982           val Abs_sbdT = Const (#Abs_name sbdT_glob_info, sum_bdT --> sbdT);
   983 
   984           val sbd_bind = Binding.suffix_name ("_" ^ sum_bdN) b;
   985           val sbd_name = Binding.name_of sbd_bind;
   986           val sbd_def_bind = (Thm.def_binding sbd_bind, []);
   987 
   988           val sbd_spec = HOLogic.mk_Trueprop
   989             (HOLogic.mk_eq (Free (sbd_name, mk_relT (`I sbdT)), mk_dir_image sum_bd Abs_sbdT));
   990 
   991           val ((sbd_free, (_, sbd_def_free)), (lthy, lthy_old)) =
   992             lthy
   993             |> Specification.definition (SOME (sbd_bind, NONE, NoSyn), (sbd_def_bind, sbd_spec))
   994             ||> `Local_Theory.restore;
   995 
   996           (*transforms defined frees into consts*)
   997           val phi = Proof_Context.export_morphism lthy_old lthy;
   998 
   999           val sbd_def = Morphism.thm phi sbd_def_free;
  1000           val sbd = Const (fst (Term.dest_Const (Morphism.term phi sbd_free)), mk_relT (`I sbdT));
  1001 
  1002           val sbdT_set_def = the (#set_def sbdT_loc_info);
  1003           val sbdT_Abs_inject = #Abs_inject sbdT_loc_info;
  1004           val sbdT_Abs_cases = #Abs_cases sbdT_loc_info;
  1005 
  1006           val Abs_sbdT_inj = mk_Abs_inj_thm sbdT_set_def sbdT_Abs_inject;
  1007           val Abs_sbdT_bij = mk_Abs_bij_thm lthy sbdT_set_def sbdT_Abs_inject sbdT_Abs_cases;
  1008 
  1009           fun mk_sum_Cinfinite [thm] = thm
  1010             | mk_sum_Cinfinite (thm :: thms) =
  1011               @{thm Cinfinite_csum_strong} OF [thm, mk_sum_Cinfinite thms];
  1012 
  1013           val sum_Cinfinite = mk_sum_Cinfinite bd_Cinfinites;
  1014           val sum_Card_order = sum_Cinfinite RS conjunct2;
  1015 
  1016           fun mk_sum_card_order [thm] = thm
  1017             | mk_sum_card_order (thm :: thms) =
  1018               @{thm card_order_csum} OF [thm, mk_sum_card_order thms];
  1019 
  1020           val sum_card_order = mk_sum_card_order bd_card_orders;
  1021 
  1022           val sbd_ordIso = Local_Defs.fold lthy [sbd_def]
  1023             (@{thm dir_image} OF [Abs_sbdT_inj, sum_Card_order]);
  1024           val sbd_card_order =  Local_Defs.fold lthy [sbd_def]
  1025             (@{thm card_order_dir_image} OF [Abs_sbdT_bij, sum_card_order]);
  1026           val sbd_Cinfinite = @{thm Cinfinite_cong} OF [sbd_ordIso, sum_Cinfinite];
  1027           val sbd_Cnotzero = sbd_Cinfinite RS @{thm Cinfinite_Cnotzero};
  1028           val sbd_Card_order = sbd_Cinfinite RS conjunct2;
  1029 
  1030           fun mk_set_sbd i bd_Card_order bds =
  1031             map (fn thm => @{thm ordLeq_ordIso_trans} OF
  1032               [bd_Card_order RS mk_ordLeq_csum n i thm, sbd_ordIso]) bds;
  1033           val set_sbdss = map3 mk_set_sbd ks bd_Card_orders set_bdss;
  1034 
  1035           fun mk_in_sbd i Co Cnz bd =
  1036             Cnz RS ((@{thm ordLeq_ordIso_trans} OF
  1037               [(Co RS mk_ordLeq_csum n i (Co RS @{thm ordLeq_refl})), sbd_ordIso]) RS
  1038               (bd RS @{thm ordLeq_transitive[OF _
  1039                 cexp_mono2_Cnotzero[OF _ csum_Cnotzero2[OF ctwo_Cnotzero]]]}));
  1040           val in_sbds = map4 mk_in_sbd ks bd_Card_orders bd_Cnotzeros in_bds;
  1041        in
  1042          (lthy, sbd, sbdT,
  1043            sbd_card_order, sbd_Cinfinite, sbd_Cnotzero, sbd_Card_order, set_sbdss, in_sbds)
  1044        end;
  1045 
  1046     fun mk_sbd_sbd 1 = sbd_Card_order RS @{thm ordIso_refl}
  1047       | mk_sbd_sbd n = @{thm csum_absorb1} OF
  1048           [sbd_Cinfinite, mk_sbd_sbd (n - 1) RS @{thm ordIso_imp_ordLeq}];
  1049 
  1050     val sbd_sbd_thm = mk_sbd_sbd n;
  1051 
  1052     val sbdTs = replicate n sbdT;
  1053     val sum_sbd = Library.foldr1 (uncurry mk_csum) (replicate n sbd);
  1054     val sum_sbdT = mk_sumTN sbdTs;
  1055     val sum_sbd_listT = HOLogic.listT sum_sbdT;
  1056     val sum_sbd_list_setT = HOLogic.mk_setT sum_sbd_listT;
  1057     val bdTs = passiveAs @ replicate n sbdT;
  1058     val to_sbd_maps = map4 mk_map_of_bnf Dss Ass (replicate n bdTs) bnfs;
  1059     val bdFTs = mk_FTs bdTs;
  1060     val sbdFT = mk_sumTN bdFTs;
  1061     val treeT = HOLogic.mk_prodT (sum_sbd_list_setT, sum_sbd_listT --> sbdFT);
  1062     val treeQT = HOLogic.mk_setT treeT;
  1063     val treeTs = passiveAs @ replicate n treeT;
  1064     val treeQTs = passiveAs @ replicate n treeQT;
  1065     val treeFTs = mk_FTs treeTs;
  1066     val tree_maps = map4 mk_map_of_bnf Dss (replicate n bdTs) (replicate n treeTs) bnfs;
  1067     val final_maps = map4 mk_map_of_bnf Dss (replicate n treeTs) (replicate n treeQTs) bnfs;
  1068     val tree_setss = mk_setss treeTs;
  1069     val isNode_setss = mk_setss (passiveAs @ replicate n sbdT);
  1070 
  1071     val root = HOLogic.mk_set sum_sbd_listT [HOLogic.mk_list sum_sbdT []];
  1072     val Zero = HOLogic.mk_tuple (map (fn U => absdummy U root) activeAs);
  1073     val Lev_recT = fastype_of Zero;
  1074     val LevT = Library.foldr (op -->) (sTs, HOLogic.natT --> Lev_recT);
  1075 
  1076     val Nil = HOLogic.mk_tuple (map3 (fn i => fn z => fn z'=>
  1077       Term.absfree z' (mk_InN activeAs z i)) ks zs zs');
  1078     val rv_recT = fastype_of Nil;
  1079     val rvT = Library.foldr (op -->) (sTs, sum_sbd_listT --> rv_recT);
  1080 
  1081     val (((((((((((sumx, sumx'), (kks, kks')), (kl, kl')), (kl_copy, kl'_copy)), (Kl, Kl')),
  1082       (lab, lab')), (Kl_lab, Kl_lab')), xs), (Lev_rec, Lev_rec')), (rv_rec, rv_rec')),
  1083       names_lthy) = names_lthy
  1084       |> yield_singleton (apfst (op ~~) oo mk_Frees' "sumx") sum_sbdT
  1085       ||>> mk_Frees' "k" sbdTs
  1086       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
  1087       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "kl") sum_sbd_listT
  1088       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl") sum_sbd_list_setT
  1089       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "lab") (sum_sbd_listT --> sbdFT)
  1090       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "Kl_lab") treeT
  1091       ||>> mk_Frees "x" bdFTs
  1092       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") Lev_recT
  1093       ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "rec") rv_recT;
  1094 
  1095     val (k, k') = (hd kks, hd kks')
  1096 
  1097     val timer = time (timer "Bounds");
  1098 
  1099     (* tree coalgebra *)
  1100 
  1101     fun isNode_bind i = Binding.suffix_name ("_" ^ isNodeN ^ (if n = 1 then "" else
  1102       string_of_int i)) b;
  1103     val isNode_name = Binding.name_of o isNode_bind;
  1104     val isNode_def_bind = rpair [] o Thm.def_binding o isNode_bind;
  1105 
  1106     val isNodeT =
  1107       Library.foldr (op -->) (map fastype_of (As @ [Kl, lab, kl]), HOLogic.boolT);
  1108 
  1109     val Succs = map3 (fn i => fn k => fn k' =>
  1110       HOLogic.mk_Collect (fst k', snd k', HOLogic.mk_mem (mk_InN sbdTs k i, mk_Succ Kl kl)))
  1111       ks kks kks';
  1112 
  1113     fun isNode_spec sets x i =
  1114       let
  1115         val (passive_sets, active_sets) = chop m (map (fn set => set $ x) sets);
  1116         val lhs = Term.list_comb (Free (isNode_name i, isNodeT), As @ [Kl, lab, kl]);
  1117         val rhs = list_exists_free [x]
  1118           (Library.foldr1 HOLogic.mk_conj (HOLogic.mk_eq (lab $ kl, mk_InN bdFTs x i) ::
  1119           map2 mk_subset passive_sets As @ map2 (curry HOLogic.mk_eq) active_sets Succs));
  1120       in
  1121         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1122       end;
  1123 
  1124     val ((isNode_frees, (_, isNode_def_frees)), (lthy, lthy_old)) =
  1125       lthy
  1126       |> fold_map3 (fn i => fn x => fn sets => Specification.definition
  1127         (SOME (isNode_bind i, NONE, NoSyn), (isNode_def_bind i, isNode_spec sets x i)))
  1128         ks xs isNode_setss
  1129       |>> apsnd split_list o split_list
  1130       ||> `Local_Theory.restore;
  1131 
  1132     (*transforms defined frees into consts*)
  1133     val phi = Proof_Context.export_morphism lthy_old lthy;
  1134 
  1135     val isNode_defs = map (Morphism.thm phi) isNode_def_frees;
  1136     val isNodes = map (fst o Term.dest_Const o Morphism.term phi) isNode_frees;
  1137 
  1138     fun mk_isNode As kl i =
  1139       Term.list_comb (Const (nth isNodes (i - 1), isNodeT), As @ [Kl, lab, kl]);
  1140 
  1141     val isTree =
  1142       let
  1143         val empty = HOLogic.mk_mem (HOLogic.mk_list sum_sbdT [], Kl);
  1144         val Field = mk_subset Kl (mk_Field (mk_clists sum_sbd));
  1145         val prefCl = mk_prefCl Kl;
  1146 
  1147         val tree = mk_Ball Kl (Term.absfree kl'
  1148           (HOLogic.mk_conj
  1149             (Library.foldr1 HOLogic.mk_disj (map (mk_isNode As kl) ks),
  1150             Library.foldr1 HOLogic.mk_conj (map4 (fn Succ => fn i => fn k => fn k' =>
  1151               mk_Ball Succ (Term.absfree k' (mk_isNode As
  1152                 (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i])) i)))
  1153             Succs ks kks kks'))));
  1154 
  1155         val undef = list_all_free [kl] (HOLogic.mk_imp
  1156           (HOLogic.mk_not (HOLogic.mk_mem (kl, Kl)),
  1157           HOLogic.mk_eq (lab $ kl, mk_undefined sbdFT)));
  1158       in
  1159         Library.foldr1 HOLogic.mk_conj [empty, Field, prefCl, tree, undef]
  1160       end;
  1161 
  1162     fun carT_bind i = Binding.suffix_name ("_" ^ carTN ^ (if n = 1 then "" else
  1163       string_of_int i)) b;
  1164     val carT_name = Binding.name_of o carT_bind;
  1165     val carT_def_bind = rpair [] o Thm.def_binding o carT_bind;
  1166 
  1167     fun carT_spec i =
  1168       let
  1169         val carTT = Library.foldr (op -->) (ATs, HOLogic.mk_setT treeT);
  1170 
  1171         val lhs = Term.list_comb (Free (carT_name i, carTT), As);
  1172         val rhs = HOLogic.mk_Collect (fst Kl_lab', snd Kl_lab', list_exists_free [Kl, lab]
  1173           (HOLogic.mk_conj (HOLogic.mk_eq (Kl_lab, HOLogic.mk_prod (Kl, lab)),
  1174             HOLogic.mk_conj (isTree, mk_isNode As (HOLogic.mk_list sum_sbdT []) i))));
  1175       in
  1176         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1177       end;
  1178 
  1179     val ((carT_frees, (_, carT_def_frees)), (lthy, lthy_old)) =
  1180       lthy
  1181       |> fold_map (fn i => Specification.definition
  1182         (SOME (carT_bind i, NONE, NoSyn), (carT_def_bind i, carT_spec i))) ks
  1183       |>> apsnd split_list o split_list
  1184       ||> `Local_Theory.restore;
  1185 
  1186     (*transforms defined frees into consts*)
  1187     val phi = Proof_Context.export_morphism lthy_old lthy;
  1188 
  1189     val carT_defs = map (Morphism.thm phi) carT_def_frees;
  1190     val carTs = map (fst o Term.dest_Const o Morphism.term phi) carT_frees;
  1191 
  1192     fun mk_carT As i = Term.list_comb
  1193       (Const (nth carTs (i - 1),
  1194          Library.foldr (op -->) (map fastype_of As, HOLogic.mk_setT treeT)), As);
  1195 
  1196     fun strT_bind i = Binding.suffix_name ("_" ^ strTN ^ (if n = 1 then "" else
  1197       string_of_int i)) b;
  1198     val strT_name = Binding.name_of o strT_bind;
  1199     val strT_def_bind = rpair [] o Thm.def_binding o strT_bind;
  1200 
  1201     fun strT_spec mapFT FT i =
  1202       let
  1203         val strTT = treeT --> FT;
  1204 
  1205         fun mk_f i k k' =
  1206           let val in_k = mk_InN sbdTs k i;
  1207           in Term.absfree k' (HOLogic.mk_prod (mk_Shift Kl in_k, mk_shift lab in_k)) end;
  1208 
  1209         val f = Term.list_comb (mapFT, passive_ids @ map3 mk_f ks kks kks');
  1210         val (fTs1, fTs2) = apsnd tl (chop (i - 1) (map (fn T => T --> FT) bdFTs));
  1211         val fs = map mk_undefined fTs1 @ (f :: map mk_undefined fTs2);
  1212         val lhs = Free (strT_name i, strTT);
  1213         val rhs = HOLogic.mk_split (Term.absfree Kl' (Term.absfree lab'
  1214           (mk_sum_caseN fs $ (lab $ HOLogic.mk_list sum_sbdT []))));
  1215       in
  1216         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1217       end;
  1218 
  1219     val ((strT_frees, (_, strT_def_frees)), (lthy, lthy_old)) =
  1220       lthy
  1221       |> fold_map3 (fn i => fn mapFT => fn FT => Specification.definition
  1222         (SOME (strT_bind i, NONE, NoSyn), (strT_def_bind i, strT_spec mapFT FT i)))
  1223         ks tree_maps treeFTs
  1224       |>> apsnd split_list o split_list
  1225       ||> `Local_Theory.restore;
  1226 
  1227     (*transforms defined frees into consts*)
  1228     val phi = Proof_Context.export_morphism lthy_old lthy;
  1229 
  1230     val strT_defs = map ((fn def => trans OF [def RS fun_cong, @{thm prod.cases}]) o
  1231       Morphism.thm phi) strT_def_frees;
  1232     val strTs = map (fst o Term.dest_Const o Morphism.term phi) strT_frees;
  1233 
  1234     fun mk_strT FT i = Const (nth strTs (i - 1), treeT --> FT);
  1235 
  1236     val carTAs = map (mk_carT As) ks;
  1237     val carTAs_copy = map (mk_carT As_copy) ks;
  1238     val strTAs = map2 mk_strT treeFTs ks;
  1239     val hset_strTss = map (fn i => map2 (mk_hset strTAs i) ls passiveAs) ks;
  1240 
  1241     val coalgT_thm =
  1242       Skip_Proof.prove lthy [] []
  1243         (fold_rev Logic.all As (HOLogic.mk_Trueprop (mk_coalg As carTAs strTAs)))
  1244         (mk_coalgT_tac m (coalg_def :: isNode_defs @ carT_defs) strT_defs set_natural'ss);
  1245 
  1246     val card_of_carT_thms =
  1247       let
  1248         val lhs = mk_card_of
  1249           (HOLogic.mk_Collect (fst Kl_lab', snd Kl_lab', list_exists_free [Kl, lab]
  1250             (HOLogic.mk_conj (HOLogic.mk_eq (Kl_lab, HOLogic.mk_prod (Kl, lab)), isTree))));
  1251         val rhs = mk_cexp
  1252           (if m = 0 then ctwo else
  1253             (mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo))
  1254             (mk_cexp sbd sbd);
  1255         val card_of_carT =
  1256           Skip_Proof.prove lthy [] []
  1257             (fold_rev Logic.all As (HOLogic.mk_Trueprop (mk_ordLeq lhs rhs)))
  1258             (K (mk_card_of_carT_tac m isNode_defs sbd_sbd_thm
  1259               sbd_card_order sbd_Card_order sbd_Cinfinite sbd_Cnotzero in_sbds))
  1260       in
  1261         map (fn def => @{thm ordLeq_transitive[OF
  1262           card_of_mono1[OF ord_eq_le_trans[OF _ Collect_restrict']]]} OF [def, card_of_carT])
  1263         carT_defs
  1264       end;
  1265 
  1266     val carT_set_thmss =
  1267       let
  1268         val Kl_lab = HOLogic.mk_prod (Kl, lab);
  1269         fun mk_goal carT strT set k i =
  1270           fold_rev Logic.all (sumx :: Kl :: lab :: k :: kl :: As)
  1271             (Logic.list_implies (map HOLogic.mk_Trueprop
  1272               [HOLogic.mk_mem (Kl_lab, carT), HOLogic.mk_mem (mk_Cons sumx kl, Kl),
  1273               HOLogic.mk_eq (sumx, mk_InN sbdTs k i)],
  1274             HOLogic.mk_Trueprop (HOLogic.mk_mem
  1275               (HOLogic.mk_prod (mk_Shift Kl sumx, mk_shift lab sumx),
  1276               set $ (strT $ Kl_lab)))));
  1277 
  1278         val goalss = map3 (fn carT => fn strT => fn sets =>
  1279           map3 (mk_goal carT strT) (drop m sets) kks ks) carTAs strTAs tree_setss;
  1280       in
  1281         map6 (fn i => fn goals =>
  1282             fn carT_def => fn strT_def => fn isNode_def => fn set_naturals =>
  1283           map2 (fn goal => fn set_natural =>
  1284             Skip_Proof.prove lthy [] [] goal
  1285             (mk_carT_set_tac n i carT_def strT_def isNode_def set_natural))
  1286           goals (drop m set_naturals))
  1287         ks goalss carT_defs strT_defs isNode_defs set_natural'ss
  1288       end;
  1289 
  1290     val carT_set_thmss' = transpose carT_set_thmss;
  1291 
  1292     val isNode_hset_thmss =
  1293       let
  1294         val Kl_lab = HOLogic.mk_prod (Kl, lab);
  1295         fun mk_Kl_lab carT = HOLogic.mk_mem (Kl_lab, carT);
  1296 
  1297         val strT_hset_thmsss =
  1298           let
  1299             val strT_hset_thms =
  1300               let
  1301                 fun mk_lab_kl i x = HOLogic.mk_eq (lab $ kl, mk_InN bdFTs x i);
  1302 
  1303                 fun mk_inner_conjunct j T i x set i' carT =
  1304                   HOLogic.mk_imp (HOLogic.mk_conj (mk_Kl_lab carT, mk_lab_kl i x),
  1305                     mk_subset (set $ x) (mk_hset strTAs i' j T $ Kl_lab));
  1306 
  1307                 fun mk_conjunct j T i x set =
  1308                   Library.foldr1 HOLogic.mk_conj (map2 (mk_inner_conjunct j T i x set) ks carTAs);
  1309 
  1310                 fun mk_concl j T = list_all_free (Kl :: lab :: xs @ As)
  1311                   (HOLogic.mk_imp (HOLogic.mk_mem (kl, Kl),
  1312                     Library.foldr1 HOLogic.mk_conj (map3 (mk_conjunct j T)
  1313                       ks xs (map (fn xs => nth xs (j - 1)) isNode_setss))));
  1314                 val concls = map2 mk_concl ls passiveAs;
  1315 
  1316                 val cTs = [SOME (certifyT lthy sum_sbdT)];
  1317                 val arg_cong_cTs = map (SOME o certifyT lthy) treeFTs;
  1318                 val ctss =
  1319                   map (fn phi => map (SOME o certify lthy) [Term.absfree kl' phi, kl]) concls;
  1320 
  1321                 val goals = map HOLogic.mk_Trueprop concls;
  1322               in
  1323                 map5 (fn j => fn goal => fn cts => fn set_incl_hsets => fn set_hset_incl_hsetss =>
  1324                   singleton (Proof_Context.export names_lthy lthy)
  1325                     (Skip_Proof.prove lthy [] [] goal
  1326                       (K (mk_strT_hset_tac n m j arg_cong_cTs cTs cts
  1327                         carT_defs strT_defs isNode_defs
  1328                         set_incl_hsets set_hset_incl_hsetss coalg_set_thmss' carT_set_thmss'
  1329                         coalgT_thm set_natural'ss))))
  1330                 ls goals ctss set_incl_hset_thmss' set_hset_incl_hset_thmsss''
  1331               end;
  1332 
  1333             val strT_hset'_thms = map (fn thm => mk_specN (2 + n + m) thm RS mp) strT_hset_thms;
  1334           in
  1335             map (fn thm => map (fn i => map (fn i' =>
  1336               thm RS mk_conjunctN n i RS mk_conjunctN n i' RS mp) ks) ks) strT_hset'_thms
  1337           end;
  1338 
  1339         val carT_prems = map (fn carT =>
  1340           HOLogic.mk_Trueprop (HOLogic.mk_mem (Kl_lab, carT))) carTAs_copy;
  1341         val prem = HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, Kl));
  1342         val in_prems = map (fn hsets =>
  1343           HOLogic.mk_Trueprop (HOLogic.mk_mem (Kl_lab, mk_in As hsets treeT))) hset_strTss;
  1344         val isNode_premss = replicate n (map (HOLogic.mk_Trueprop o mk_isNode As_copy kl) ks);
  1345         val conclss = replicate n (map (HOLogic.mk_Trueprop o mk_isNode As kl) ks);
  1346       in
  1347         map5 (fn carT_prem => fn isNode_prems => fn in_prem => fn concls => fn strT_hset_thmss =>
  1348           map4 (fn isNode_prem => fn concl => fn isNode_def => fn strT_hset_thms =>
  1349             Skip_Proof.prove lthy [] []
  1350             (fold_rev Logic.all (Kl :: lab :: kl :: As @ As_copy)
  1351               (Logic.list_implies ([carT_prem, prem, isNode_prem, in_prem], concl)))
  1352             (mk_isNode_hset_tac n isNode_def strT_hset_thms))
  1353           isNode_prems concls isNode_defs
  1354           (if m = 0 then replicate n [] else transpose strT_hset_thmss))
  1355         carT_prems isNode_premss in_prems conclss
  1356         (if m = 0 then replicate n [] else transpose (map transpose strT_hset_thmsss))
  1357       end;
  1358 
  1359     val timer = time (timer "Tree coalgebra");
  1360 
  1361     fun mk_to_sbd s x i i' =
  1362       mk_toCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
  1363     fun mk_from_sbd s x i i' =
  1364       mk_fromCard (nth (nth setssAs (i - 1)) (m + i' - 1) $ (s $ x)) sbd;
  1365 
  1366     fun mk_to_sbd_thmss thm = map (map (fn set_sbd =>
  1367       thm OF [set_sbd, sbd_Card_order]) o drop m) set_sbdss;
  1368 
  1369     val to_sbd_inj_thmss = mk_to_sbd_thmss @{thm toCard_inj};
  1370     val to_sbd_thmss = mk_to_sbd_thmss @{thm toCard};
  1371     val from_to_sbd_thmss = mk_to_sbd_thmss @{thm fromCard_toCard};
  1372 
  1373     val Lev_bind = Binding.suffix_name ("_" ^ LevN) b;
  1374     val Lev_name = Binding.name_of Lev_bind;
  1375     val Lev_def_bind = rpair [] (Thm.def_binding Lev_bind);
  1376 
  1377     val Lev_spec =
  1378       let
  1379         fun mk_Suc i s setsAs a a' =
  1380           let
  1381             val sets = drop m setsAs;
  1382             fun mk_set i' set b =
  1383               let
  1384                 val Cons = HOLogic.mk_eq (kl_copy,
  1385                   mk_Cons (mk_InN sbdTs (mk_to_sbd s a i i' $ b) i') kl)
  1386                 val b_set = HOLogic.mk_mem (b, set $ (s $ a));
  1387                 val kl_rec = HOLogic.mk_mem (kl, mk_nthN n Lev_rec i' $ b);
  1388               in
  1389                 HOLogic.mk_Collect (fst kl'_copy, snd kl'_copy, list_exists_free [b, kl]
  1390                   (HOLogic.mk_conj (Cons, HOLogic.mk_conj (b_set, kl_rec))))
  1391               end;
  1392           in
  1393             Term.absfree a' (Library.foldl1 mk_union (map3 mk_set ks sets zs_copy))
  1394           end;
  1395 
  1396         val Suc = Term.absdummy HOLogic.natT (Term.absfree Lev_rec'
  1397           (HOLogic.mk_tuple (map5 mk_Suc ks ss setssAs zs zs')));
  1398 
  1399         val lhs = Term.list_comb (Free (Lev_name, LevT), ss);
  1400         val rhs = mk_nat_rec Zero Suc;
  1401       in
  1402         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1403       end;
  1404 
  1405     val ((Lev_free, (_, Lev_def_free)), (lthy, lthy_old)) =
  1406       lthy
  1407       |> Specification.definition (SOME (Lev_bind, NONE, NoSyn), (Lev_def_bind, Lev_spec))
  1408       ||> `Local_Theory.restore;
  1409 
  1410     (*transforms defined frees into consts*)
  1411     val phi = Proof_Context.export_morphism lthy_old lthy;
  1412 
  1413     val Lev_def = Morphism.thm phi Lev_def_free;
  1414     val Lev = fst (Term.dest_Const (Morphism.term phi Lev_free));
  1415 
  1416     fun mk_Lev ss nat i =
  1417       let
  1418         val Ts = map fastype_of ss;
  1419         val LevT = Library.foldr (op -->) (Ts, HOLogic.natT -->
  1420           HOLogic.mk_tupleT (map (fn U => domain_type U --> sum_sbd_list_setT) Ts));
  1421       in
  1422         mk_nthN n (Term.list_comb (Const (Lev, LevT), ss) $ nat) i
  1423       end;
  1424 
  1425     val Lev_0s = flat (mk_rec_simps n @{thm nat_rec_0} [Lev_def]);
  1426     val Lev_Sucs = flat (mk_rec_simps n @{thm nat_rec_Suc} [Lev_def]);
  1427 
  1428     val rv_bind = Binding.suffix_name ("_" ^ rvN) b;
  1429     val rv_name = Binding.name_of rv_bind;
  1430     val rv_def_bind = rpair [] (Thm.def_binding rv_bind);
  1431 
  1432     val rv_spec =
  1433       let
  1434         fun mk_Cons i s b b' =
  1435           let
  1436             fun mk_case i' =
  1437               Term.absfree k' (mk_nthN n rv_rec i' $ (mk_from_sbd s b i i' $ k));
  1438           in
  1439             Term.absfree b' (mk_sum_caseN (map mk_case ks) $ sumx)
  1440           end;
  1441 
  1442         val Cons = Term.absfree sumx' (Term.absdummy sum_sbd_listT (Term.absfree rv_rec'
  1443           (HOLogic.mk_tuple (map4 mk_Cons ks ss zs zs'))));
  1444 
  1445         val lhs = Term.list_comb (Free (rv_name, rvT), ss);
  1446         val rhs = mk_list_rec Nil Cons;
  1447       in
  1448         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1449       end;
  1450 
  1451     val ((rv_free, (_, rv_def_free)), (lthy, lthy_old)) =
  1452       lthy
  1453       |> Specification.definition (SOME (rv_bind, NONE, NoSyn), (rv_def_bind, rv_spec))
  1454       ||> `Local_Theory.restore;
  1455 
  1456     (*transforms defined frees into consts*)
  1457     val phi = Proof_Context.export_morphism lthy_old lthy;
  1458 
  1459     val rv_def = Morphism.thm phi rv_def_free;
  1460     val rv = fst (Term.dest_Const (Morphism.term phi rv_free));
  1461 
  1462     fun mk_rv ss kl i =
  1463       let
  1464         val Ts = map fastype_of ss;
  1465         val As = map domain_type Ts;
  1466         val rvT = Library.foldr (op -->) (Ts, fastype_of kl -->
  1467           HOLogic.mk_tupleT (map (fn U => U --> mk_sumTN As) As));
  1468       in
  1469         mk_nthN n (Term.list_comb (Const (rv, rvT), ss) $ kl) i
  1470       end;
  1471 
  1472     val rv_Nils = flat (mk_rec_simps n @{thm list_rec_Nil} [rv_def]);
  1473     val rv_Conss = flat (mk_rec_simps n @{thm list_rec_Cons} [rv_def]);
  1474 
  1475     fun beh_bind i = Binding.suffix_name ("_" ^ behN ^ (if n = 1 then "" else
  1476       string_of_int i)) b;
  1477     val beh_name = Binding.name_of o beh_bind;
  1478     val beh_def_bind = rpair [] o Thm.def_binding o beh_bind;
  1479 
  1480     fun beh_spec i z =
  1481       let
  1482         val mk_behT = Library.foldr (op -->) (map fastype_of (ss @ [z]), treeT);
  1483 
  1484         fun mk_case i to_sbd_map s k k' =
  1485           Term.absfree k' (mk_InN bdFTs
  1486             (Term.list_comb (to_sbd_map, passive_ids @ map (mk_to_sbd s k i) ks) $ (s $ k)) i);
  1487 
  1488         val Lab = Term.absfree kl' (mk_If
  1489           (HOLogic.mk_mem (kl, mk_Lev ss (mk_size kl) i $ z))
  1490           (mk_sum_caseN (map5 mk_case ks to_sbd_maps ss zs zs') $ (mk_rv ss kl i $ z))
  1491           (mk_undefined sbdFT));
  1492 
  1493         val lhs = Term.list_comb (Free (beh_name i, mk_behT), ss) $ z;
  1494         val rhs = HOLogic.mk_prod (mk_UNION (HOLogic.mk_UNIV HOLogic.natT)
  1495           (Term.absfree nat' (mk_Lev ss nat i $ z)), Lab);
  1496       in
  1497         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1498       end;
  1499 
  1500     val ((beh_frees, (_, beh_def_frees)), (lthy, lthy_old)) =
  1501       lthy
  1502       |> fold_map2 (fn i => fn z => Specification.definition
  1503         (SOME (beh_bind i, NONE, NoSyn), (beh_def_bind i, beh_spec i z))) ks zs
  1504       |>> apsnd split_list o split_list
  1505       ||> `Local_Theory.restore;
  1506 
  1507     (*transforms defined frees into consts*)
  1508     val phi = Proof_Context.export_morphism lthy_old lthy;
  1509 
  1510     val beh_defs = map (Morphism.thm phi) beh_def_frees;
  1511     val behs = map (fst o Term.dest_Const o Morphism.term phi) beh_frees;
  1512 
  1513     fun mk_beh ss i =
  1514       let
  1515         val Ts = map fastype_of ss;
  1516         val behT = Library.foldr (op -->) (Ts, nth activeAs (i - 1) --> treeT);
  1517       in
  1518         Term.list_comb (Const (nth behs (i - 1), behT), ss)
  1519       end;
  1520 
  1521     val Lev_sbd_thms =
  1522       let
  1523         fun mk_conjunct i z = mk_subset (mk_Lev ss nat i $ z) (mk_Field (mk_clists sum_sbd));
  1524         val goal = list_all_free zs
  1525           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1526 
  1527         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1528 
  1529         val Lev_sbd = singleton (Proof_Context.export names_lthy lthy)
  1530           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1531             (K (mk_Lev_sbd_tac cts Lev_0s Lev_Sucs to_sbd_thmss)));
  1532 
  1533         val Lev_sbd' = mk_specN n Lev_sbd;
  1534       in
  1535         map (fn i => Lev_sbd' RS mk_conjunctN n i) ks
  1536       end;
  1537 
  1538     val (length_Lev_thms, length_Lev'_thms) =
  1539       let
  1540         fun mk_conjunct i z = HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1541           HOLogic.mk_eq (mk_size kl, nat));
  1542         val goal = list_all_free (kl :: zs)
  1543           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1544 
  1545         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1546 
  1547         val length_Lev = singleton (Proof_Context.export names_lthy lthy)
  1548           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1549             (K (mk_length_Lev_tac cts Lev_0s Lev_Sucs)));
  1550 
  1551         val length_Lev' = mk_specN (n + 1) length_Lev;
  1552         val length_Levs = map (fn i => length_Lev' RS mk_conjunctN n i RS mp) ks;
  1553 
  1554         fun mk_goal i z = fold_rev Logic.all (z :: kl :: nat :: ss) (Logic.mk_implies
  1555             (HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z)),
  1556             HOLogic.mk_Trueprop (HOLogic.mk_mem (kl, mk_Lev ss (mk_size kl) i $ z))));
  1557         val goals = map2 mk_goal ks zs;
  1558 
  1559         val length_Levs' = map2 (fn goal => fn length_Lev =>
  1560           Skip_Proof.prove lthy [] [] goal
  1561             (K (mk_length_Lev'_tac length_Lev))) goals length_Levs;
  1562       in
  1563         (length_Levs, length_Levs')
  1564       end;
  1565 
  1566     val prefCl_Lev_thms =
  1567       let
  1568         fun mk_conjunct i z = HOLogic.mk_imp
  1569           (HOLogic.mk_conj (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z), mk_subset kl_copy kl),
  1570           HOLogic.mk_mem (kl_copy, mk_Lev ss (mk_size kl_copy) i $ z));
  1571         val goal = list_all_free (kl :: kl_copy :: zs)
  1572           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1573 
  1574         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1575 
  1576         val prefCl_Lev = singleton (Proof_Context.export names_lthy lthy)
  1577           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1578             (K (mk_prefCl_Lev_tac cts Lev_0s Lev_Sucs)));
  1579 
  1580         val prefCl_Lev' = mk_specN (n + 2) prefCl_Lev;
  1581       in
  1582         map (fn i => prefCl_Lev' RS mk_conjunctN n i RS mp) ks
  1583       end;
  1584 
  1585     val rv_last_thmss =
  1586       let
  1587         fun mk_conjunct i z i' z_copy = list_exists_free [z_copy]
  1588           (HOLogic.mk_eq
  1589             (mk_rv ss (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i'])) i $ z,
  1590             mk_InN activeAs z_copy i'));
  1591         val goal = list_all_free (k :: zs)
  1592           (Library.foldr1 HOLogic.mk_conj (map2 (fn i => fn z =>
  1593             Library.foldr1 HOLogic.mk_conj
  1594               (map2 (mk_conjunct i z) ks zs_copy)) ks zs));
  1595 
  1596         val cTs = [SOME (certifyT lthy sum_sbdT)];
  1597         val cts = map (SOME o certify lthy) [Term.absfree kl' goal, kl];
  1598 
  1599         val rv_last = singleton (Proof_Context.export names_lthy lthy)
  1600           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1601             (K (mk_rv_last_tac cTs cts rv_Nils rv_Conss)));
  1602 
  1603         val rv_last' = mk_specN (n + 1) rv_last;
  1604       in
  1605         map (fn i => map (fn i' => rv_last' RS mk_conjunctN n i RS mk_conjunctN n i') ks) ks
  1606       end;
  1607 
  1608     val set_rv_Lev_thmsss = if m = 0 then replicate n (replicate n []) else
  1609       let
  1610         fun mk_case s sets z z_free = Term.absfree z_free (Library.foldr1 HOLogic.mk_conj
  1611           (map2 (fn set => fn A => mk_subset (set $ (s $ z)) A) (take m sets) As));
  1612 
  1613         fun mk_conjunct i z B = HOLogic.mk_imp
  1614           (HOLogic.mk_conj (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z), HOLogic.mk_mem (z, B)),
  1615           mk_sum_caseN (map4 mk_case ss setssAs zs zs') $ (mk_rv ss kl i $ z));
  1616 
  1617         val goal = list_all_free (kl :: zs)
  1618           (Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct ks zs Bs));
  1619 
  1620         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1621 
  1622         val set_rv_Lev = singleton (Proof_Context.export names_lthy lthy)
  1623           (Skip_Proof.prove lthy [] []
  1624             (Logic.mk_implies (coalg_prem, HOLogic.mk_Trueprop goal))
  1625             (K (mk_set_rv_Lev_tac m cts Lev_0s Lev_Sucs rv_Nils rv_Conss
  1626               coalg_set_thmss from_to_sbd_thmss)));
  1627 
  1628         val set_rv_Lev' = mk_specN (n + 1) set_rv_Lev;
  1629       in
  1630         map (fn i => map (fn i' =>
  1631           split_conj_thm (if n = 1 then set_rv_Lev' RS mk_conjunctN n i RS mp
  1632             else set_rv_Lev' RS mk_conjunctN n i RS mp RSN
  1633               (2, @{thm sum_case_cong} RS @{thm subst[of _ _ "%x. x"]}) RS
  1634               (mk_sum_casesN n i' RS @{thm subst[of _ _ "%x. x"]}))) ks) ks
  1635       end;
  1636 
  1637     val set_Lev_thmsss =
  1638       let
  1639         fun mk_conjunct i z =
  1640           let
  1641             fun mk_conjunct' i' sets s z' =
  1642               let
  1643                 fun mk_conjunct'' i'' set z'' = HOLogic.mk_imp
  1644                   (HOLogic.mk_mem (z'', set $ (s $ z')),
  1645                     HOLogic.mk_mem (mk_append (kl,
  1646                       HOLogic.mk_list sum_sbdT [mk_InN sbdTs (mk_to_sbd s z' i' i'' $ z'') i'']),
  1647                       mk_Lev ss (HOLogic.mk_Suc nat) i $ z));
  1648               in
  1649                 HOLogic.mk_imp (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z' i'),
  1650                   (Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct'' ks (drop m sets) zs_copy2)))
  1651               end;
  1652           in
  1653             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1654               Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct' ks setssAs ss zs_copy))
  1655           end;
  1656 
  1657         val goal = list_all_free (kl :: zs @ zs_copy @ zs_copy2)
  1658           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1659 
  1660         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1661 
  1662         val set_Lev = singleton (Proof_Context.export names_lthy lthy)
  1663           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1664             (K (mk_set_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss from_to_sbd_thmss)));
  1665 
  1666         val set_Lev' = mk_specN (3 * n + 1) set_Lev;
  1667       in
  1668         map (fn i => map (fn i' => map (fn i'' => set_Lev' RS
  1669           mk_conjunctN n i RS mp RS
  1670           mk_conjunctN n i' RS mp RS
  1671           mk_conjunctN n i'' RS mp) ks) ks) ks
  1672       end;
  1673 
  1674     val set_image_Lev_thmsss =
  1675       let
  1676         fun mk_conjunct i z =
  1677           let
  1678             fun mk_conjunct' i' sets =
  1679               let
  1680                 fun mk_conjunct'' i'' set s z'' = HOLogic.mk_imp
  1681                   (HOLogic.mk_eq (mk_rv ss kl i $ z, mk_InN activeAs z'' i''),
  1682                   HOLogic.mk_mem (k, mk_image (mk_to_sbd s z'' i'' i') $ (set $ (s $ z''))));
  1683               in
  1684                 HOLogic.mk_imp (HOLogic.mk_mem
  1685                   (mk_append (kl, HOLogic.mk_list sum_sbdT [mk_InN sbdTs k i']),
  1686                     mk_Lev ss (HOLogic.mk_Suc nat) i $ z),
  1687                   (Library.foldr1 HOLogic.mk_conj (map4 mk_conjunct'' ks sets ss zs_copy)))
  1688               end;
  1689           in
  1690             HOLogic.mk_imp (HOLogic.mk_mem (kl, mk_Lev ss nat i $ z),
  1691               Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct' ks (drop m setssAs')))
  1692           end;
  1693 
  1694         val goal = list_all_free (kl :: k :: zs @ zs_copy)
  1695           (Library.foldr1 HOLogic.mk_conj (map2 mk_conjunct ks zs));
  1696 
  1697         val cts = map (SOME o certify lthy) [Term.absfree nat' goal, nat];
  1698 
  1699         val set_image_Lev = singleton (Proof_Context.export names_lthy lthy)
  1700           (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  1701             (K (mk_set_image_Lev_tac cts Lev_0s Lev_Sucs rv_Nils rv_Conss
  1702               from_to_sbd_thmss to_sbd_inj_thmss)));
  1703 
  1704         val set_image_Lev' = mk_specN (2 * n + 2) set_image_Lev;
  1705       in
  1706         map (fn i => map (fn i' => map (fn i'' => set_image_Lev' RS
  1707           mk_conjunctN n i RS mp RS
  1708           mk_conjunctN n i'' RS mp RS
  1709           mk_conjunctN n i' RS mp) ks) ks) ks
  1710       end;
  1711 
  1712     val mor_beh_thm =
  1713       Skip_Proof.prove lthy [] []
  1714         (fold_rev Logic.all (As @ Bs @ ss) (Logic.mk_implies (coalg_prem,
  1715           HOLogic.mk_Trueprop (mk_mor Bs ss carTAs strTAs (map (mk_beh ss) ks)))))
  1716         (mk_mor_beh_tac m mor_def mor_cong_thm
  1717           beh_defs carT_defs strT_defs isNode_defs
  1718           to_sbd_inj_thmss from_to_sbd_thmss Lev_0s Lev_Sucs rv_Nils rv_Conss Lev_sbd_thms
  1719           length_Lev_thms length_Lev'_thms prefCl_Lev_thms rv_last_thmss
  1720           set_rv_Lev_thmsss set_Lev_thmsss set_image_Lev_thmsss
  1721           set_natural'ss coalg_set_thmss map_comp_id_thms map_congs map_arg_cong_thms);
  1722 
  1723     val timer = time (timer "Behavioral morphism");
  1724 
  1725     fun mk_LSBIS As i = mk_lsbis As (map (mk_carT As) ks) strTAs i;
  1726     fun mk_car_final As i =
  1727       mk_quotient (mk_carT As i) (mk_LSBIS As i);
  1728     fun mk_str_final As i =
  1729       mk_univ (HOLogic.mk_comp (Term.list_comb (nth final_maps (i - 1),
  1730         passive_ids @ map (mk_proj o mk_LSBIS As) ks), nth strTAs (i - 1)));
  1731 
  1732     val car_finalAs = map (mk_car_final As) ks;
  1733     val str_finalAs = map (mk_str_final As) ks;
  1734     val car_finals = map (mk_car_final passive_UNIVs) ks;
  1735     val str_finals = map (mk_str_final passive_UNIVs) ks;
  1736 
  1737     val coalgT_set_thmss = map (map (fn thm => coalgT_thm RS thm)) coalg_set_thmss;
  1738     val equiv_LSBIS_thms = map (fn thm => coalgT_thm RS thm) equiv_lsbis_thms;
  1739 
  1740     val congruent_str_final_thms =
  1741       let
  1742         fun mk_goal R final_map strT =
  1743           fold_rev Logic.all As (HOLogic.mk_Trueprop
  1744             (mk_congruent R (HOLogic.mk_comp
  1745               (Term.list_comb (final_map, passive_ids @ map (mk_proj o mk_LSBIS As) ks), strT))));
  1746 
  1747         val goals = map3 mk_goal (map (mk_LSBIS As) ks) final_maps strTAs;
  1748       in
  1749         map4 (fn goal => fn lsbisE => fn map_comp_id => fn map_cong =>
  1750           Skip_Proof.prove lthy [] [] goal
  1751             (K (mk_congruent_str_final_tac m lsbisE map_comp_id map_cong equiv_LSBIS_thms)))
  1752         goals lsbisE_thms map_comp_id_thms map_congs
  1753       end;
  1754 
  1755     val coalg_final_thm = Skip_Proof.prove lthy [] [] (fold_rev Logic.all As
  1756       (HOLogic.mk_Trueprop (mk_coalg As car_finalAs str_finalAs)))
  1757       (K (mk_coalg_final_tac m coalg_def congruent_str_final_thms equiv_LSBIS_thms
  1758         set_natural'ss coalgT_set_thmss));
  1759 
  1760     val mor_T_final_thm = Skip_Proof.prove lthy [] [] (fold_rev Logic.all As
  1761       (HOLogic.mk_Trueprop (mk_mor carTAs strTAs car_finalAs str_finalAs
  1762         (map (mk_proj o mk_LSBIS As) ks))))
  1763       (K (mk_mor_T_final_tac mor_def congruent_str_final_thms equiv_LSBIS_thms));
  1764 
  1765     val mor_final_thm = mor_comp_thm OF [mor_beh_thm, mor_T_final_thm];
  1766     val in_car_final_thms = map (fn mor_image' => mor_image' OF
  1767       [tcoalg_thm RS mor_final_thm, UNIV_I]) mor_image'_thms;
  1768 
  1769     val timer = time (timer "Final coalgebra");
  1770 
  1771     val ((T_names, (T_glob_infos, T_loc_infos)), lthy) =
  1772       lthy
  1773       |> fold_map3 (fn b => fn car_final => fn in_car_final =>
  1774         typedef false NONE (b, params, NoSyn) car_final NONE
  1775           (EVERY' [rtac exI, rtac in_car_final] 1)) bs car_finals in_car_final_thms
  1776       |>> apsnd split_list o split_list;
  1777 
  1778     val Ts = map (fn name => Type (name, params')) T_names;
  1779     fun mk_Ts passive = map (Term.typ_subst_atomic (passiveAs ~~ passive)) Ts;
  1780     val Ts' = mk_Ts passiveBs;
  1781     val Ts'' = mk_Ts passiveCs;
  1782     val Rep_Ts = map2 (fn info => fn T => Const (#Rep_name info, T --> treeQT)) T_glob_infos Ts;
  1783     val Abs_Ts = map2 (fn info => fn T => Const (#Abs_name info, treeQT --> T)) T_glob_infos Ts;
  1784 
  1785     val Reps = map #Rep T_loc_infos;
  1786     val Rep_injects = map #Rep_inject T_loc_infos;
  1787     val Rep_inverses = map #Rep_inverse T_loc_infos;
  1788     val Abs_inverses = map #Abs_inverse T_loc_infos;
  1789 
  1790     val timer = time (timer "THE TYPEDEFs & Rep/Abs thms");
  1791 
  1792     val UNIVs = map HOLogic.mk_UNIV Ts;
  1793     val FTs = mk_FTs (passiveAs @ Ts);
  1794     val FTs' = mk_FTs (passiveBs @ Ts);
  1795     val prodTs = map (HOLogic.mk_prodT o `I) Ts;
  1796     val prodFTs = mk_FTs (passiveAs @ prodTs);
  1797     val FTs_setss = mk_setss (passiveAs @ Ts);
  1798     val FTs'_setss = mk_setss (passiveBs @ Ts);
  1799     val prodFT_setss = mk_setss (passiveAs @ prodTs);
  1800     val map_FTs = map2 (fn Ds => mk_map_of_bnf Ds treeQTs (passiveAs @ Ts)) Dss bnfs;
  1801     val map_FT_nths = map2 (fn Ds =>
  1802       mk_map_of_bnf Ds (passiveAs @ prodTs) (passiveAs @ Ts)) Dss bnfs;
  1803     val fstsTs = map fst_const prodTs;
  1804     val sndsTs = map snd_const prodTs;
  1805     val unfTs = map2 (curry (op -->)) Ts FTs;
  1806     val fldTs = map2 (curry (op -->)) FTs Ts;
  1807     val coiter_fTs = map2 (curry op -->) activeAs Ts;
  1808     val corec_sTs = map (Term.typ_subst_atomic (activeBs ~~ Ts)) sum_sTs;
  1809     val corec_maps = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls;
  1810     val corec_maps_rev = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_Inls_rev;
  1811     val corec_Inls = map (Term.subst_atomic_types (activeBs ~~ Ts)) Inls;
  1812 
  1813     val (((((((((((((Jzs, Jzs'), (Jz's, Jz's')), Jzs_copy), Jzs1), Jzs2), Jpairs),
  1814       FJzs), TRs), coiter_fs), coiter_fs_copy), corec_ss), phis), names_lthy) = names_lthy
  1815       |> mk_Frees' "z" Ts
  1816       ||>> mk_Frees' "z" Ts'
  1817       ||>> mk_Frees "z" Ts
  1818       ||>> mk_Frees "z1" Ts
  1819       ||>> mk_Frees "z2" Ts
  1820       ||>> mk_Frees "j" (map2 (curry HOLogic.mk_prodT) Ts Ts')
  1821       ||>> mk_Frees "x" prodFTs
  1822       ||>> mk_Frees "R" (map (mk_relT o `I) Ts)
  1823       ||>> mk_Frees "f" coiter_fTs
  1824       ||>> mk_Frees "g" coiter_fTs
  1825       ||>> mk_Frees "s" corec_sTs
  1826       ||>> mk_Frees "phi" (map (fn T => T --> T --> HOLogic.boolT) Ts);
  1827 
  1828     fun unf_bind i = Binding.suffix_name ("_" ^ unfN) (nth bs (i - 1));
  1829     val unf_name = Binding.name_of o unf_bind;
  1830     val unf_def_bind = rpair [] o Thm.def_binding o unf_bind;
  1831 
  1832     fun unf_spec i rep str map_FT unfT Jz Jz' =
  1833       let
  1834         val lhs = Free (unf_name i, unfT);
  1835         val rhs = Term.absfree Jz'
  1836           (Term.list_comb (map_FT, map HOLogic.id_const passiveAs @ Abs_Ts) $
  1837             (str $ (rep $ Jz)));
  1838       in
  1839         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1840       end;
  1841 
  1842     val ((unf_frees, (_, unf_def_frees)), (lthy, lthy_old)) =
  1843       lthy
  1844       |> fold_map7 (fn i => fn rep => fn str => fn map => fn unfT => fn Jz => fn Jz' =>
  1845         Specification.definition
  1846           (SOME (unf_bind i, NONE, NoSyn), (unf_def_bind i, unf_spec i rep str map unfT Jz Jz')))
  1847           ks Rep_Ts str_finals map_FTs unfTs Jzs Jzs'
  1848       |>> apsnd split_list o split_list
  1849       ||> `Local_Theory.restore;
  1850 
  1851     (*transforms defined frees into consts*)
  1852     val phi = Proof_Context.export_morphism lthy_old lthy;
  1853     fun mk_unfs passive =
  1854       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ (deads @ passive)) o
  1855         Morphism.term phi) unf_frees;
  1856     val unfs = mk_unfs passiveAs;
  1857     val unf's = mk_unfs passiveBs;
  1858     val unf_defs = map ((fn thm => thm RS fun_cong) o Morphism.thm phi) unf_def_frees;
  1859 
  1860     val coalg_final_set_thmss = map (map (fn thm => coalg_final_thm RS thm)) coalg_set_thmss;
  1861     val (mor_Rep_thm, mor_Abs_thm) =
  1862       let
  1863         val mor_Rep =
  1864           Skip_Proof.prove lthy [] []
  1865             (HOLogic.mk_Trueprop (mk_mor UNIVs unfs car_finals str_finals Rep_Ts))
  1866             (mk_mor_Rep_tac m (mor_def :: unf_defs) Reps Abs_inverses coalg_final_set_thmss
  1867               map_comp_id_thms map_congL_thms);
  1868 
  1869         val mor_Abs =
  1870           Skip_Proof.prove lthy [] []
  1871             (HOLogic.mk_Trueprop (mk_mor car_finals str_finals UNIVs unfs Abs_Ts))
  1872             (mk_mor_Abs_tac (mor_def :: unf_defs) Abs_inverses);
  1873       in
  1874         (mor_Rep, mor_Abs)
  1875       end;
  1876 
  1877     val timer = time (timer "unf definitions & thms");
  1878 
  1879     fun coiter_bind i = Binding.suffix_name ("_" ^ coN ^ iterN) (nth bs (i - 1));
  1880     val coiter_name = Binding.name_of o coiter_bind;
  1881     val coiter_def_bind = rpair [] o Thm.def_binding o coiter_bind;
  1882 
  1883     fun coiter_spec i T AT abs f z z' =
  1884       let
  1885         val coiterT = Library.foldr (op -->) (sTs, AT --> T);
  1886 
  1887         val lhs = Term.list_comb (Free (coiter_name i, coiterT), ss);
  1888         val rhs = Term.absfree z' (abs $ (f $ z));
  1889       in
  1890         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  1891       end;
  1892 
  1893     val ((coiter_frees, (_, coiter_def_frees)), (lthy, lthy_old)) =
  1894       lthy
  1895       |> fold_map7 (fn i => fn T => fn AT => fn abs => fn f => fn z => fn z' =>
  1896         Specification.definition
  1897           (SOME (coiter_bind i, NONE, NoSyn), (coiter_def_bind i, coiter_spec i T AT abs f z z')))
  1898           ks Ts activeAs Abs_Ts (map (fn i => HOLogic.mk_comp
  1899             (mk_proj (mk_LSBIS passive_UNIVs i), mk_beh ss i)) ks) zs zs'
  1900       |>> apsnd split_list o split_list
  1901       ||> `Local_Theory.restore;
  1902 
  1903     (*transforms defined frees into consts*)
  1904     val phi = Proof_Context.export_morphism lthy_old lthy;
  1905     val coiters = map (fst o dest_Const o Morphism.term phi) coiter_frees;
  1906     fun mk_coiter Ts ss i = Term.list_comb (Const (nth coiters (i - 1), Library.foldr (op -->)
  1907       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  1908     val coiter_defs = map ((fn thm => thm RS fun_cong) o Morphism.thm phi) coiter_def_frees;
  1909 
  1910     val mor_coiter_thm =
  1911       let
  1912         val Abs_inverses' = map2 (curry op RS) in_car_final_thms Abs_inverses;
  1913         val morEs' = map (fn thm =>
  1914           (thm OF [tcoalg_thm RS mor_final_thm, UNIV_I]) RS sym) morE_thms;
  1915       in
  1916         Skip_Proof.prove lthy [] []
  1917           (fold_rev Logic.all ss
  1918             (HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs unfs (map (mk_coiter Ts ss) ks))))
  1919           (K (mk_mor_coiter_tac m mor_UNIV_thm unf_defs coiter_defs Abs_inverses' morEs'
  1920             map_comp_id_thms map_congs))
  1921       end;
  1922     val coiter_thms = map (fn thm => (thm OF [mor_coiter_thm, UNIV_I]) RS sym) morE_thms;
  1923 
  1924     val (raw_coind_thms, raw_coind_thm) =
  1925       let
  1926         val prem = HOLogic.mk_Trueprop (mk_sbis passive_UNIVs UNIVs unfs TRs);
  1927         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1928           (map2 (fn R => fn T => mk_subset R (Id_const T)) TRs Ts));
  1929         val goal = fold_rev Logic.all TRs (Logic.mk_implies (prem, concl));
  1930       in
  1931         `split_conj_thm (Skip_Proof.prove lthy [] [] goal
  1932           (K (mk_raw_coind_tac bis_def bis_cong_thm bis_O_thm bis_converse_thm bis_Gr_thm
  1933             tcoalg_thm coalgT_thm mor_T_final_thm sbis_lsbis_thm
  1934             lsbis_incl_thms incl_lsbis_thms equiv_LSBIS_thms mor_Rep_thm Rep_injects)))
  1935       end;
  1936 
  1937     val unique_mor_thms =
  1938       let
  1939         val prems = [HOLogic.mk_Trueprop (mk_coalg passive_UNIVs Bs ss), HOLogic.mk_Trueprop
  1940           (HOLogic.mk_conj (mk_mor Bs ss UNIVs unfs coiter_fs,
  1941             mk_mor Bs ss UNIVs unfs coiter_fs_copy))];
  1942         fun mk_fun_eq B f g z = HOLogic.mk_imp
  1943           (HOLogic.mk_mem (z, B), HOLogic.mk_eq (f $ z, g $ z));
  1944         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1945           (map4 mk_fun_eq Bs coiter_fs coiter_fs_copy zs));
  1946 
  1947         val unique_mor = Skip_Proof.prove lthy [] []
  1948           (fold_rev Logic.all (Bs @ ss @ coiter_fs @ coiter_fs_copy @ zs)
  1949             (Logic.list_implies (prems, unique)))
  1950           (K (mk_unique_mor_tac raw_coind_thms bis_image2_thm));
  1951       in
  1952         map (fn thm => conjI RSN (2, thm RS mp)) (split_conj_thm unique_mor)
  1953       end;
  1954 
  1955     val (coiter_unique_mor_thms, coiter_unique_mor_thm) =
  1956       let
  1957         val prem = HOLogic.mk_Trueprop (mk_mor active_UNIVs ss UNIVs unfs coiter_fs);
  1958         fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_coiter Ts ss i);
  1959         val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  1960           (map2 mk_fun_eq coiter_fs ks));
  1961 
  1962         val bis_thm = tcoalg_thm RSN (2, tcoalg_thm RS bis_image2_thm);
  1963         val mor_thm = mor_comp_thm OF [tcoalg_thm RS mor_final_thm, mor_Abs_thm];
  1964 
  1965         val unique_mor = Skip_Proof.prove lthy [] []
  1966           (fold_rev Logic.all (ss @ coiter_fs) (Logic.mk_implies (prem, unique)))
  1967           (K (mk_coiter_unique_mor_tac raw_coind_thms bis_thm mor_thm coiter_defs));
  1968       in
  1969         `split_conj_thm unique_mor
  1970       end;
  1971 
  1972     val (coiter_unique_thms, coiter_unique_thm) = `split_conj_thm (split_conj_prems n
  1973       (mor_UNIV_thm RS @{thm ssubst[of _ _ "%x. x"]} RS coiter_unique_mor_thm));
  1974 
  1975     val coiter_unf_thms = map (fn thm => mor_id_thm RS thm RS sym) coiter_unique_mor_thms;
  1976 
  1977     val coiter_o_unf_thms =
  1978       let
  1979         val mor = mor_comp_thm OF [mor_str_thm, mor_coiter_thm];
  1980       in
  1981         map2 (fn unique => fn coiter_fld =>
  1982           trans OF [mor RS unique, coiter_fld]) coiter_unique_mor_thms coiter_unf_thms
  1983       end;
  1984 
  1985     val timer = time (timer "coiter definitions & thms");
  1986 
  1987     val map_unfs = map2 (fn Ds => fn bnf =>
  1988       Term.list_comb (mk_map_of_bnf Ds (passiveAs @ Ts) (passiveAs @ FTs) bnf,
  1989         map HOLogic.id_const passiveAs @ unfs)) Dss bnfs;
  1990 
  1991     fun fld_bind i = Binding.suffix_name ("_" ^ fldN) (nth bs (i - 1));
  1992     val fld_name = Binding.name_of o fld_bind;
  1993     val fld_def_bind = rpair [] o Thm.def_binding o fld_bind;
  1994 
  1995     fun fld_spec i fldT =
  1996       let
  1997         val lhs = Free (fld_name i, fldT);
  1998         val rhs = mk_coiter Ts map_unfs i;
  1999       in
  2000         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  2001       end;
  2002 
  2003     val ((fld_frees, (_, fld_def_frees)), (lthy, lthy_old)) =
  2004         lthy
  2005         |> fold_map2 (fn i => fn fldT =>
  2006           Specification.definition
  2007             (SOME (fld_bind i, NONE, NoSyn), (fld_def_bind i, fld_spec i fldT))) ks fldTs
  2008         |>> apsnd split_list o split_list
  2009         ||> `Local_Theory.restore;
  2010 
  2011     (*transforms defined frees into consts*)
  2012     val phi = Proof_Context.export_morphism lthy_old lthy;
  2013     fun mk_flds params =
  2014       map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ params) o Morphism.term phi)
  2015         fld_frees;
  2016     val flds = mk_flds params';
  2017     val fld_defs = map (Morphism.thm phi) fld_def_frees;
  2018 
  2019     val fld_o_unf_thms = map2 (Local_Defs.fold lthy o single) fld_defs coiter_o_unf_thms;
  2020 
  2021     val unf_o_fld_thms =
  2022       let
  2023         fun mk_goal unf fld FT =
  2024           HOLogic.mk_Trueprop (HOLogic.mk_eq (HOLogic.mk_comp (unf, fld), HOLogic.id_const FT));
  2025         val goals = map3 mk_goal unfs flds FTs;
  2026       in
  2027         map5 (fn goal => fn fld_def => fn coiter => fn map_comp_id => fn map_congL =>
  2028           Skip_Proof.prove lthy [] [] goal
  2029             (mk_unf_o_fld_tac fld_def coiter map_comp_id map_congL coiter_o_unf_thms))
  2030           goals fld_defs coiter_thms map_comp_id_thms map_congL_thms
  2031       end;
  2032 
  2033     val unf_fld_thms = map (fn thm => thm RS @{thm pointfree_idE}) unf_o_fld_thms;
  2034     val fld_unf_thms = map (fn thm => thm RS @{thm pointfree_idE}) fld_o_unf_thms;
  2035 
  2036     val bij_unf_thms =
  2037       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) fld_o_unf_thms unf_o_fld_thms;
  2038     val inj_unf_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_unf_thms;
  2039     val surj_unf_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_unf_thms;
  2040     val unf_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_unf_thms;
  2041     val unf_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_unf_thms;
  2042     val unf_exhaust_thms = map (fn thm => thm RS exE) unf_nchotomy_thms;
  2043 
  2044     val bij_fld_thms =
  2045       map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) unf_o_fld_thms fld_o_unf_thms;
  2046     val inj_fld_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_fld_thms;
  2047     val surj_fld_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_fld_thms;
  2048     val fld_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_fld_thms;
  2049     val fld_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_fld_thms;
  2050     val fld_exhaust_thms = map (fn thm => thm RS exE) fld_nchotomy_thms;
  2051 
  2052     val fld_coiter_thms = map3 (fn unf_inject => fn coiter => fn unf_fld =>
  2053       iffD1 OF [unf_inject, trans  OF [coiter, unf_fld RS sym]])
  2054       unf_inject_thms coiter_thms unf_fld_thms;
  2055 
  2056     val timer = time (timer "fld definitions & thms");
  2057 
  2058     val corec_Inl_sum_thms =
  2059       let
  2060         val mor = mor_comp_thm OF [mor_sum_case_thm, mor_coiter_thm];
  2061       in
  2062         map2 (fn unique => fn coiter_unf =>
  2063           trans OF [mor RS unique, coiter_unf]) coiter_unique_mor_thms coiter_unf_thms
  2064       end;
  2065 
  2066     fun corec_bind i = Binding.suffix_name ("_" ^ coN ^ recN) (nth bs (i - 1));
  2067     val corec_name = Binding.name_of o corec_bind;
  2068     val corec_def_bind = rpair [] o Thm.def_binding o corec_bind;
  2069 
  2070     fun corec_spec i T AT =
  2071       let
  2072         val corecT = Library.foldr (op -->) (corec_sTs, AT --> T);
  2073         val maps = map3 (fn unf => fn sum_s => fn map => mk_sum_case
  2074             (HOLogic.mk_comp (Term.list_comb (map, passive_ids @ corec_Inls), unf)) sum_s)
  2075           unfs corec_ss corec_maps;
  2076 
  2077         val lhs = Term.list_comb (Free (corec_name i, corecT), corec_ss);
  2078         val rhs = HOLogic.mk_comp (mk_coiter Ts maps i, Inr_const T AT);
  2079       in
  2080         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs))
  2081       end;
  2082 
  2083     val ((corec_frees, (_, corec_def_frees)), (lthy, lthy_old)) =
  2084         lthy
  2085         |> fold_map3 (fn i => fn T => fn AT =>
  2086           Specification.definition
  2087             (SOME (corec_bind i, NONE, NoSyn), (corec_def_bind i, corec_spec i T AT)))
  2088             ks Ts activeAs
  2089         |>> apsnd split_list o split_list
  2090         ||> `Local_Theory.restore;
  2091 
  2092     (*transforms defined frees into consts*)
  2093     val phi = Proof_Context.export_morphism lthy_old lthy;
  2094     val corecs = map (fst o dest_Const o Morphism.term phi) corec_frees;
  2095     fun mk_corec ss i = Term.list_comb (Const (nth corecs (i - 1), Library.foldr (op -->)
  2096       (map fastype_of ss, domain_type (fastype_of (nth ss (i - 1))) --> nth Ts (i - 1))), ss);
  2097     val corec_defs = map (Morphism.thm phi) corec_def_frees;
  2098 
  2099     val sum_cases =
  2100       map2 (fn T => fn i => mk_sum_case (HOLogic.id_const T) (mk_corec corec_ss i)) Ts ks;
  2101     val corec_thms =
  2102       let
  2103         fun mk_goal i corec_s corec_map unf z =
  2104           let
  2105             val lhs = unf $ (mk_corec corec_ss i $ z);
  2106             val rhs = Term.list_comb (corec_map, passive_ids @ sum_cases) $ (corec_s $ z);
  2107           in
  2108             fold_rev Logic.all (z :: corec_ss) (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs)))
  2109           end;
  2110         val goals = map5 mk_goal ks corec_ss corec_maps_rev unfs zs;
  2111       in
  2112         map3 (fn goal => fn coiter => fn map_cong =>
  2113           Skip_Proof.prove lthy [] [] goal
  2114             (mk_corec_tac m corec_defs coiter map_cong corec_Inl_sum_thms))
  2115           goals coiter_thms map_congs
  2116       end;
  2117 
  2118     val timer = time (timer "corec definitions & thms");
  2119 
  2120     val (unf_coinduct_thm, coinduct_params, rel_coinduct_thm, pred_coinduct_thm,
  2121          unf_coinduct_upto_thm, rel_coinduct_upto_thm, pred_coinduct_upto_thm) =
  2122       let
  2123         val zs = Jzs1 @ Jzs2;
  2124         val frees = phis @ zs;
  2125 
  2126         fun mk_Ids Id = if Id then map Id_const passiveAs else map mk_diag passive_UNIVs;
  2127 
  2128         fun mk_phi upto_eq phi z1 z2 = if upto_eq
  2129           then Term.absfree (dest_Free z1) (Term.absfree (dest_Free z2)
  2130             (HOLogic.mk_disj (phi $ z1 $ z2, HOLogic.mk_eq (z1, z2))))
  2131           else phi;
  2132 
  2133         fun phi_rels upto_eq = map4 (fn phi => fn T => fn z1 => fn z2 =>
  2134           HOLogic.Collect_const (HOLogic.mk_prodT (T, T)) $
  2135             HOLogic.mk_split (mk_phi upto_eq phi z1 z2)) phis Ts Jzs1 Jzs2;
  2136 
  2137         val rels = map (Term.subst_atomic_types ((activeAs ~~ Ts) @ (activeBs ~~ Ts))) relsAsBs;
  2138 
  2139         fun mk_concl phi z1 z2 = HOLogic.mk_imp (phi $ z1 $ z2, HOLogic.mk_eq (z1, z2));
  2140         val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2141           (map3 mk_concl phis Jzs1 Jzs2));
  2142 
  2143         fun mk_rel_prem upto_eq phi unf rel Jz Jz_copy =
  2144           let
  2145             val concl = HOLogic.mk_mem (HOLogic.mk_tuple [unf $ Jz, unf $ Jz_copy],
  2146               Term.list_comb (rel, mk_Ids upto_eq @ phi_rels upto_eq));
  2147           in
  2148             HOLogic.mk_Trueprop
  2149               (list_all_free [Jz, Jz_copy] (HOLogic.mk_imp (phi $ Jz $ Jz_copy, concl)))
  2150           end;
  2151 
  2152         val rel_prems = map5 (mk_rel_prem false) phis unfs rels Jzs Jzs_copy;
  2153         val rel_upto_prems = map5 (mk_rel_prem true) phis unfs rels Jzs Jzs_copy;
  2154 
  2155         val rel_coinduct_goal = fold_rev Logic.all frees (Logic.list_implies (rel_prems, concl));
  2156         val coinduct_params = rev (Term.add_tfrees rel_coinduct_goal []);
  2157 
  2158         val rel_coinduct = Local_Defs.unfold lthy @{thms diag_UNIV}
  2159           (Skip_Proof.prove lthy [] [] rel_coinduct_goal
  2160             (K (mk_rel_coinduct_tac ks raw_coind_thm bis_rel_thm)));
  2161 
  2162         fun mk_unf_prem upto_eq phi unf map_nth sets Jz Jz_copy FJz =
  2163           let
  2164             val xs = [Jz, Jz_copy];
  2165 
  2166             fun mk_map_conjunct nths x =
  2167               HOLogic.mk_eq (Term.list_comb (map_nth, passive_ids @ nths) $ FJz, unf $ x);
  2168 
  2169             fun mk_set_conjunct set phi z1 z2 =
  2170               list_all_free [z1, z2]
  2171                 (HOLogic.mk_imp (HOLogic.mk_mem (HOLogic.mk_prod (z1, z2), set $ FJz),
  2172                   mk_phi upto_eq phi z1 z2 $ z1 $ z2));
  2173 
  2174             val concl = list_exists_free [FJz] (HOLogic.mk_conj
  2175               (Library.foldr1 HOLogic.mk_conj (map2 mk_map_conjunct [fstsTs, sndsTs] xs),
  2176               Library.foldr1 HOLogic.mk_conj
  2177                 (map4 mk_set_conjunct (drop m sets) phis Jzs1 Jzs2)));
  2178           in
  2179             fold_rev Logic.all xs (Logic.mk_implies
  2180               (HOLogic.mk_Trueprop (Term.list_comb (phi, xs)), HOLogic.mk_Trueprop concl))
  2181           end;
  2182 
  2183         fun mk_unf_prems upto_eq =
  2184           map7 (mk_unf_prem upto_eq) phis unfs map_FT_nths prodFT_setss Jzs Jzs_copy FJzs
  2185 
  2186         val unf_prems = mk_unf_prems false;
  2187         val unf_upto_prems = mk_unf_prems true;
  2188 
  2189         val unf_coinduct_goal = fold_rev Logic.all frees (Logic.list_implies (unf_prems, concl));
  2190         val unf_coinduct = Skip_Proof.prove lthy [] [] unf_coinduct_goal
  2191           (K (mk_unf_coinduct_tac m ks raw_coind_thm bis_def));
  2192 
  2193         val cTs = map (SOME o certifyT lthy o TFree) coinduct_params;
  2194         val cts = map3 (SOME o certify lthy ooo mk_phi true) phis Jzs1 Jzs2;
  2195 
  2196         val rel_coinduct_upto = singleton (Proof_Context.export names_lthy lthy)
  2197           (Skip_Proof.prove lthy [] []
  2198             (fold_rev Logic.all zs (Logic.list_implies (rel_upto_prems, concl)))
  2199             (K (mk_rel_coinduct_upto_tac m cTs cts rel_coinduct rel_monos rel_Ids)));
  2200 
  2201         val unf_coinduct_upto = singleton (Proof_Context.export names_lthy lthy)
  2202           (Skip_Proof.prove lthy [] []
  2203             (fold_rev Logic.all zs (Logic.list_implies (unf_upto_prems, concl)))
  2204             (K (mk_unf_coinduct_upto_tac ks cTs cts unf_coinduct bis_def
  2205               (tcoalg_thm RS bis_diag_thm))));
  2206 
  2207         val pred_coinduct = rel_coinduct
  2208           |> Local_Defs.unfold lthy @{thms Id_def'}
  2209           |> Local_Defs.fold lthy pred_defs;
  2210         val pred_coinduct_upto = rel_coinduct_upto
  2211           |> Local_Defs.unfold lthy @{thms Id_def'}
  2212           |> Local_Defs.fold lthy pred_defs;
  2213       in
  2214         (unf_coinduct, rev (Term.add_tfrees unf_coinduct_goal []), rel_coinduct, pred_coinduct,
  2215          unf_coinduct_upto, rel_coinduct_upto, pred_coinduct_upto)
  2216       end;
  2217 
  2218     val timer = time (timer "coinduction");
  2219 
  2220     (*register new codatatypes as BNFs*)
  2221     val lthy = if m = 0 then lthy else
  2222       let
  2223         val fTs = map2 (curry op -->) passiveAs passiveBs;
  2224         val gTs = map2 (curry op -->) passiveBs passiveCs;
  2225         val f1Ts = map2 (curry op -->) passiveAs passiveYs;
  2226         val f2Ts = map2 (curry op -->) passiveBs passiveYs;
  2227         val p1Ts = map2 (curry op -->) passiveXs passiveAs;
  2228         val p2Ts = map2 (curry op -->) passiveXs passiveBs;
  2229         val pTs = map2 (curry op -->) passiveXs passiveCs;
  2230         val uTs = map2 (curry op -->) Ts Ts';
  2231         val JRTs = map2 (curry mk_relT) passiveAs passiveBs;
  2232         val JphiTs = map2 (fn T => fn U => T --> U --> HOLogic.boolT) passiveAs passiveBs;
  2233         val prodTs = map2 (curry HOLogic.mk_prodT) Ts Ts';
  2234         val B1Ts = map HOLogic.mk_setT passiveAs;
  2235         val B2Ts = map HOLogic.mk_setT passiveBs;
  2236         val AXTs = map HOLogic.mk_setT passiveXs;
  2237         val XTs = mk_Ts passiveXs;
  2238         val YTs = mk_Ts passiveYs;
  2239 
  2240         val ((((((((((((((((((((fs, fs'), (fs_copy, fs'_copy)), (gs, gs')), us),
  2241           (Jys, Jys')), (Jys_copy, Jys'_copy)), set_induct_phiss), JRs), Jphis),
  2242           B1s), B2s), AXs), Xs), f1s), f2s), p1s), p2s), ps), (ys, ys')), names_lthy) = names_lthy
  2243           |> mk_Frees' "f" fTs
  2244           ||>> mk_Frees' "f" fTs
  2245           ||>> mk_Frees' "g" gTs
  2246           ||>> mk_Frees "u" uTs
  2247           ||>> mk_Frees' "b" Ts'
  2248           ||>> mk_Frees' "b" Ts'
  2249           ||>> mk_Freess "phi" (map (fn T => map (fn U => T --> U --> HOLogic.boolT) Ts) passiveAs)
  2250           ||>> mk_Frees "R" JRTs
  2251           ||>> mk_Frees "phi" JphiTs
  2252           ||>> mk_Frees "B1" B1Ts
  2253           ||>> mk_Frees "B2" B2Ts
  2254           ||>> mk_Frees "A" AXTs
  2255           ||>> mk_Frees "x" XTs
  2256           ||>> mk_Frees "f1" f1Ts
  2257           ||>> mk_Frees "f2" f2Ts
  2258           ||>> mk_Frees "p1" p1Ts
  2259           ||>> mk_Frees "p2" p2Ts
  2260           ||>> mk_Frees "p" pTs
  2261           ||>> mk_Frees' "y" passiveAs;
  2262 
  2263         val map_FTFT's = map2 (fn Ds =>
  2264           mk_map_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2265 
  2266         fun mk_maps ATs BTs Ts mk_T =
  2267           map2 (fn Ds => mk_map_of_bnf Ds (ATs @ Ts) (BTs @ map mk_T Ts)) Dss bnfs;
  2268         fun mk_Fmap mk_const fs Ts Fmap = Term.list_comb (Fmap, fs @ map mk_const Ts);
  2269         fun mk_map mk_const mk_T Ts fs Ts' unfs mk_maps =
  2270           mk_coiter Ts' (map2 (fn unf => fn Fmap =>
  2271             HOLogic.mk_comp (mk_Fmap mk_const fs Ts Fmap, unf)) unfs (mk_maps Ts mk_T));
  2272         val mk_map_id = mk_map HOLogic.id_const I;
  2273         val mk_mapsAB = mk_maps passiveAs passiveBs;
  2274         val mk_mapsBC = mk_maps passiveBs passiveCs;
  2275         val mk_mapsAC = mk_maps passiveAs passiveCs;
  2276         val mk_mapsAY = mk_maps passiveAs passiveYs;
  2277         val mk_mapsBY = mk_maps passiveBs passiveYs;
  2278         val mk_mapsXA = mk_maps passiveXs passiveAs;
  2279         val mk_mapsXB = mk_maps passiveXs passiveBs;
  2280         val mk_mapsXC = mk_maps passiveXs passiveCs;
  2281         val fs_maps = map (mk_map_id Ts fs Ts' unfs mk_mapsAB) ks;
  2282         val fs_copy_maps = map (mk_map_id Ts fs_copy Ts' unfs mk_mapsAB) ks;
  2283         val gs_maps = map (mk_map_id Ts' gs Ts'' unf's mk_mapsBC) ks;
  2284         val fgs_maps =
  2285           map (mk_map_id Ts (map2 (curry HOLogic.mk_comp) gs fs) Ts'' unfs mk_mapsAC) ks;
  2286         val Xunfs = mk_unfs passiveXs;
  2287         val UNIV's = map HOLogic.mk_UNIV Ts';
  2288         val CUNIVs = map HOLogic.mk_UNIV passiveCs;
  2289         val UNIV''s = map HOLogic.mk_UNIV Ts'';
  2290         val fstsTsTs' = map fst_const prodTs;
  2291         val sndsTsTs' = map snd_const prodTs;
  2292         val unf''s = mk_unfs passiveCs;
  2293         val f1s_maps = map (mk_map_id Ts f1s YTs unfs mk_mapsAY) ks;
  2294         val f2s_maps = map (mk_map_id Ts' f2s YTs unf's mk_mapsBY) ks;
  2295         val pid_maps = map (mk_map_id XTs ps Ts'' Xunfs mk_mapsXC) ks;
  2296         val pfst_Fmaps =
  2297           map (mk_Fmap fst_const p1s prodTs) (mk_mapsXA prodTs (fst o HOLogic.dest_prodT));
  2298         val psnd_Fmaps =
  2299           map (mk_Fmap snd_const p2s prodTs) (mk_mapsXB prodTs (snd o HOLogic.dest_prodT));
  2300         val p1id_Fmaps = map (mk_Fmap HOLogic.id_const p1s prodTs) (mk_mapsXA prodTs I);
  2301         val p2id_Fmaps = map (mk_Fmap HOLogic.id_const p2s prodTs) (mk_mapsXB prodTs I);
  2302         val pid_Fmaps = map (mk_Fmap HOLogic.id_const ps prodTs) (mk_mapsXC prodTs I);
  2303 
  2304         val (map_simp_thms, map_thms) =
  2305           let
  2306             fun mk_goal fs_map map unf unf' = fold_rev Logic.all fs
  2307               (HOLogic.mk_Trueprop (HOLogic.mk_eq (HOLogic.mk_comp (unf', fs_map),
  2308                 HOLogic.mk_comp (Term.list_comb (map, fs @ fs_maps), unf))));
  2309             val goals = map4 mk_goal fs_maps map_FTFT's unfs unf's;
  2310             val cTs = map (SOME o certifyT lthy) FTs';
  2311             val maps = map5 (fn goal => fn cT => fn coiter => fn map_comp' => fn map_cong =>
  2312               Skip_Proof.prove lthy [] [] goal
  2313                 (K (mk_map_tac m n cT coiter map_comp' map_cong)))
  2314               goals cTs coiter_thms map_comp's map_congs;
  2315           in
  2316             map_split (fn thm => (thm RS @{thm pointfreeE}, thm)) maps
  2317           end;
  2318 
  2319         val map_comp_thms =
  2320           let
  2321             val goal = fold_rev Logic.all (fs @ gs)
  2322               (HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2323                 (map3 (fn fmap => fn gmap => fn fgmap =>
  2324                    HOLogic.mk_eq (HOLogic.mk_comp (gmap, fmap), fgmap))
  2325                 fs_maps gs_maps fgs_maps)))
  2326           in
  2327             split_conj_thm (Skip_Proof.prove lthy [] [] goal
  2328               (K (mk_map_comp_tac m n map_thms map_comps map_congs coiter_unique_thm)))
  2329           end;
  2330 
  2331         val (map_unique_thms, map_unique_thm) =
  2332           let
  2333             fun mk_prem u map unf unf' =
  2334               HOLogic.mk_Trueprop (HOLogic.mk_eq (HOLogic.mk_comp (unf', u),
  2335                 HOLogic.mk_comp (Term.list_comb (map, fs @ us), unf)));
  2336             val prems = map4 mk_prem us map_FTFT's unfs unf's;
  2337             val goal =
  2338               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2339                 (map2 (curry HOLogic.mk_eq) us fs_maps));
  2340             val unique = Skip_Proof.prove lthy [] []
  2341               (fold_rev Logic.all (us @ fs) (Logic.list_implies (prems, goal)))
  2342               (mk_map_unique_tac coiter_unique_thm map_comps);
  2343           in
  2344             `split_conj_thm unique
  2345           end;
  2346 
  2347         val timer = time (timer "map functions for the new codatatypes");
  2348 
  2349         val bd = mk_ccexp sbd sbd;
  2350 
  2351         val timer = time (timer "bounds for the new codatatypes");
  2352 
  2353         fun mk_set_Ts T = passiveAs @ replicate n (HOLogic.mk_setT T);
  2354         val setsss = map (mk_setss o mk_set_Ts) passiveAs;
  2355         val map_setss = map (fn T => map2 (fn Ds =>
  2356           mk_map_of_bnf Ds (passiveAs @ Ts) (mk_set_Ts T)) Dss bnfs) passiveAs;
  2357 
  2358         val setss_by_bnf = map (fn i => map2 (mk_hset unfs i) ls passiveAs) ks;
  2359         val setss_by_bnf' = map (fn i => map2 (mk_hset unf's i) ls passiveBs) ks;
  2360         val setss_by_range = transpose setss_by_bnf;
  2361 
  2362         val set_simp_thmss =
  2363           let
  2364             fun mk_simp_goal relate pas_set act_sets sets unf z set =
  2365               relate (set $ z, mk_union (pas_set $ (unf $ z),
  2366                  Library.foldl1 mk_union
  2367                    (map2 (fn X => mk_UNION (X $ (unf $ z))) act_sets sets)));
  2368             fun mk_goals eq =
  2369               map2 (fn i => fn sets =>
  2370                 map4 (fn Fsets =>
  2371                   mk_simp_goal eq (nth Fsets (i - 1)) (drop m Fsets) sets)
  2372                 FTs_setss unfs Jzs sets)
  2373               ls setss_by_range;
  2374 
  2375             val le_goals = map
  2376               (fold_rev Logic.all Jzs o HOLogic.mk_Trueprop o Library.foldr1 HOLogic.mk_conj)
  2377               (mk_goals (uncurry mk_subset));
  2378             val set_le_thmss = map split_conj_thm
  2379               (map4 (fn goal => fn hset_minimal => fn set_hsets => fn set_hset_hsetss =>
  2380                 Skip_Proof.prove lthy [] [] goal
  2381                   (K (mk_set_le_tac n hset_minimal set_hsets set_hset_hsetss)))
  2382               le_goals hset_minimal_thms set_hset_thmss' set_hset_hset_thmsss');
  2383 
  2384             val simp_goalss = map (map2 (fn z => fn goal =>
  2385                 Logic.all z (HOLogic.mk_Trueprop goal)) Jzs)
  2386               (mk_goals HOLogic.mk_eq);
  2387           in
  2388             map4 (map4 (fn goal => fn set_le => fn set_incl_hset => fn set_hset_incl_hsets =>
  2389               Skip_Proof.prove lthy [] [] goal
  2390                 (K (mk_set_simp_tac n set_le set_incl_hset set_hset_incl_hsets))))
  2391             simp_goalss set_le_thmss set_incl_hset_thmss' set_hset_incl_hset_thmsss'
  2392           end;
  2393 
  2394         val timer = time (timer "set functions for the new codatatypes");
  2395 
  2396         val colss = map2 (fn j => fn T =>
  2397           map (fn i => mk_hset_rec unfs nat i j T) ks) ls passiveAs;
  2398         val colss' = map2 (fn j => fn T =>
  2399           map (fn i => mk_hset_rec unf's nat i j T) ks) ls passiveBs;
  2400         val Xcolss = map2 (fn j => fn T =>
  2401           map (fn i => mk_hset_rec Xunfs nat i j T) ks) ls passiveXs;
  2402 
  2403         val col_natural_thmss =
  2404           let
  2405             fun mk_col_natural f map z col col' =
  2406               HOLogic.mk_eq (mk_image f $ (col $ z), col' $ (map $ z));
  2407 
  2408             fun mk_goal f cols cols' = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2409               (map4 (mk_col_natural f) fs_maps Jzs cols cols'));
  2410 
  2411             val goals = map3 mk_goal fs colss colss';
  2412 
  2413             val ctss =
  2414               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) goals;
  2415 
  2416             val thms = map4 (fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2417               singleton (Proof_Context.export names_lthy lthy)
  2418                 (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  2419                   (mk_col_natural_tac cts rec_0s rec_Sucs map_simp_thms set_natural'ss)))
  2420               goals ctss hset_rec_0ss' hset_rec_Sucss';
  2421           in
  2422             map (split_conj_thm o mk_specN n) thms
  2423           end;
  2424 
  2425         val col_bd_thmss =
  2426           let
  2427             fun mk_col_bd z col = mk_ordLeq (mk_card_of (col $ z)) sbd;
  2428 
  2429             fun mk_goal cols = list_all_free Jzs (Library.foldr1 HOLogic.mk_conj
  2430               (map2 mk_col_bd Jzs cols));
  2431 
  2432             val goals = map mk_goal colss;
  2433 
  2434             val ctss =
  2435               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) goals;
  2436 
  2437             val thms = map5 (fn j => fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2438               singleton (Proof_Context.export names_lthy lthy)
  2439                 (Skip_Proof.prove lthy [] [] (HOLogic.mk_Trueprop goal)
  2440                   (K (mk_col_bd_tac m j cts rec_0s rec_Sucs
  2441                     sbd_Card_order sbd_Cinfinite set_sbdss))))
  2442               ls goals ctss hset_rec_0ss' hset_rec_Sucss';
  2443           in
  2444             map (split_conj_thm o mk_specN n) thms
  2445           end;
  2446 
  2447         val map_cong_thms =
  2448           let
  2449             val cTs = map (SOME o certifyT lthy o
  2450               Term.typ_subst_atomic (passiveAs ~~ passiveBs) o TFree) coinduct_params;
  2451 
  2452             fun mk_prem z set f g y y' =
  2453               mk_Ball (set $ z) (Term.absfree y' (HOLogic.mk_eq (f $ y, g $ y)));
  2454 
  2455             fun mk_prems sets z =
  2456               Library.foldr1 HOLogic.mk_conj (map5 (mk_prem z) sets fs fs_copy ys ys')
  2457 
  2458             fun mk_map_cong sets z fmap gmap =
  2459               HOLogic.mk_imp (mk_prems sets z, HOLogic.mk_eq (fmap $ z, gmap $ z));
  2460 
  2461             fun mk_coind_body sets (x, T) z fmap gmap y y_copy =
  2462               HOLogic.mk_conj
  2463                 (HOLogic.mk_mem (z, HOLogic.mk_Collect (x, T, mk_prems sets z)),
  2464                   HOLogic.mk_conj (HOLogic.mk_eq (y, fmap $ z),
  2465                     HOLogic.mk_eq (y_copy, gmap $ z)))
  2466 
  2467             fun mk_cphi sets (z' as (x, T)) z fmap gmap y' y y'_copy y_copy =
  2468               HOLogic.mk_exists (x, T, mk_coind_body sets z' z fmap gmap y y_copy)
  2469               |> Term.absfree y'_copy
  2470               |> Term.absfree y'
  2471               |> certify lthy;
  2472 
  2473             val cphis =
  2474               map9 mk_cphi setss_by_bnf Jzs' Jzs fs_maps fs_copy_maps Jys' Jys Jys'_copy Jys_copy;
  2475 
  2476             val coinduct = Drule.instantiate' cTs (map SOME cphis) unf_coinduct_thm;
  2477 
  2478             val goal =
  2479               HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2480                 (map4 mk_map_cong setss_by_bnf Jzs fs_maps fs_copy_maps));
  2481 
  2482             val thm = singleton (Proof_Context.export names_lthy lthy)
  2483               (Skip_Proof.prove lthy [] [] goal
  2484               (K (mk_mcong_tac m (rtac coinduct) map_comp's map_simp_thms map_congs set_natural'ss
  2485               set_hset_thmss set_hset_hset_thmsss)))
  2486           in
  2487             split_conj_thm thm
  2488           end;
  2489 
  2490         val B1_ins = map2 (mk_in B1s) setss_by_bnf Ts;
  2491         val B2_ins = map2 (mk_in B2s) setss_by_bnf' Ts';
  2492         val thePulls = map4 mk_thePull B1_ins B2_ins f1s_maps f2s_maps;
  2493         val thePullTs = passiveXs @ map2 (curry HOLogic.mk_prodT) Ts Ts';
  2494         val thePull_ins = map2 (mk_in (AXs @ thePulls)) (mk_setss thePullTs) (mk_FTs thePullTs);
  2495         val pickFs = map5 mk_pickWP thePull_ins pfst_Fmaps psnd_Fmaps
  2496           (map2 (curry (op $)) unfs Jzs) (map2 (curry (op $)) unf's Jz's);
  2497         val pickF_ss = map3 (fn pickF => fn z => fn z' =>
  2498           HOLogic.mk_split (Term.absfree z (Term.absfree z' pickF))) pickFs Jzs' Jz's';
  2499         val picks = map (mk_coiter XTs pickF_ss) ks;
  2500 
  2501         val wpull_prem = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
  2502           (map8 mk_wpull AXs B1s B2s f1s f2s (replicate m NONE) p1s p2s));
  2503 
  2504         val map_eq_thms = map2 (fn simp => fn diff => box_equals OF [diff RS iffD2, simp, simp])
  2505           map_simp_thms unf_inject_thms;
  2506         val map_wpull_thms = map (fn thm => thm OF
  2507           (replicate m asm_rl @ replicate n @{thm wpull_thePull})) map_wpulls;
  2508         val pickWP_assms_tacs =
  2509           map3 mk_pickWP_assms_tac set_incl_hset_thmss set_incl_hin_thmss map_eq_thms;
  2510 
  2511         val coalg_thePull_thm =
  2512           let
  2513             val coalg = HOLogic.mk_Trueprop
  2514               (mk_coalg CUNIVs thePulls (map2 (curry HOLogic.mk_comp) pid_Fmaps pickF_ss));
  2515             val goal = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s @ ps)
  2516               (Logic.mk_implies (wpull_prem, coalg));
  2517           in
  2518             Skip_Proof.prove lthy [] [] goal (mk_coalg_thePull_tac m coalg_def map_wpull_thms
  2519               set_natural'ss pickWP_assms_tacs)
  2520           end;
  2521 
  2522         val (mor_thePull_fst_thm, mor_thePull_snd_thm, mor_thePull_pick_thm) =
  2523           let
  2524             val mor_fst = HOLogic.mk_Trueprop
  2525               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) p1id_Fmaps pickF_ss)
  2526                 UNIVs unfs fstsTsTs');
  2527             val mor_snd = HOLogic.mk_Trueprop
  2528               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) p2id_Fmaps pickF_ss)
  2529                 UNIV's unf's sndsTsTs');
  2530             val mor_pick = HOLogic.mk_Trueprop
  2531               (mk_mor thePulls (map2 (curry HOLogic.mk_comp) pid_Fmaps pickF_ss)
  2532                 UNIV''s unf''s (map2 (curry HOLogic.mk_comp) pid_maps picks));
  2533 
  2534             val goal_fst = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
  2535               (Logic.mk_implies (wpull_prem, mor_fst));
  2536             val goal_snd = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s)
  2537               (Logic.mk_implies (wpull_prem, mor_snd));
  2538             val goal_pick = fold_rev Logic.all (AXs @ B1s @ B2s @ f1s @ f2s @ p1s @ p2s @ ps)
  2539               (Logic.mk_implies (wpull_prem, mor_pick));
  2540           in
  2541             (Skip_Proof.prove lthy [] [] goal_fst (mk_mor_thePull_fst_tac m mor_def map_wpull_thms
  2542               map_comp's pickWP_assms_tacs),
  2543             Skip_Proof.prove lthy [] [] goal_snd (mk_mor_thePull_snd_tac m mor_def map_wpull_thms
  2544               map_comp's pickWP_assms_tacs),
  2545             Skip_Proof.prove lthy [] [] goal_pick (mk_mor_thePull_pick_tac mor_def coiter_thms
  2546               map_comp's))
  2547           end;
  2548 
  2549         val pick_col_thmss =
  2550           let
  2551             fun mk_conjunct AX Jpair pick thePull col =
  2552               HOLogic.mk_imp (HOLogic.mk_mem (Jpair, thePull), mk_subset (col $ (pick $ Jpair)) AX);
  2553 
  2554             fun mk_concl AX cols =
  2555               list_all_free Jpairs (Library.foldr1 HOLogic.mk_conj
  2556                 (map4 (mk_conjunct AX) Jpairs picks thePulls cols));
  2557 
  2558             val concls = map2 mk_concl AXs Xcolss;
  2559 
  2560             val ctss =
  2561               map (fn phi => map (SOME o certify lthy) [Term.absfree nat' phi, nat]) concls;
  2562 
  2563             val goals =
  2564               map (fn concl => Logic.mk_implies (wpull_prem, HOLogic.mk_Trueprop concl)) concls;
  2565 
  2566             val thms = map5 (fn j => fn goal => fn cts => fn rec_0s => fn rec_Sucs =>
  2567               singleton (Proof_Context.export names_lthy lthy) (Skip_Proof.prove lthy [] [] goal
  2568                 (mk_pick_col_tac m j cts rec_0s rec_Sucs coiter_thms set_natural'ss map_wpull_thms
  2569                   pickWP_assms_tacs)))
  2570               ls goals ctss hset_rec_0ss' hset_rec_Sucss';
  2571           in
  2572             map (map (fn thm => thm RS mp) o split_conj_thm o mk_specN n) thms
  2573           end;
  2574 
  2575         val timer = time (timer "helpers for BNF properties");
  2576 
  2577         val map_id_tacs = map2 (K oo mk_map_id_tac map_thms) coiter_unique_thms coiter_unf_thms;
  2578         val map_comp_tacs = map (fn thm => K (rtac (thm RS sym) 1)) map_comp_thms;
  2579         val map_cong_tacs = map (mk_map_cong_tac m) map_cong_thms;
  2580         val set_nat_tacss =
  2581           map2 (map2 (K oo mk_set_natural_tac)) hset_defss (transpose col_natural_thmss);
  2582 
  2583         val bd_co_tacs = replicate n (K (mk_bd_card_order_tac sbd_card_order));
  2584         val bd_cinf_tacs = replicate n (K (mk_bd_cinfinite_tac sbd_Cinfinite));
  2585 
  2586         val set_bd_tacss =
  2587           map2 (map2 (K oo mk_set_bd_tac sbd_Cinfinite)) hset_defss (transpose col_bd_thmss);
  2588 
  2589         val in_bd_tacs = map7 (fn i => fn isNode_hsets => fn carT_def =>
  2590             fn card_of_carT => fn mor_image => fn Rep_inverse => fn mor_hsets =>
  2591           K (mk_in_bd_tac (nth isNode_hsets (i - 1)) isNode_hsets carT_def
  2592             card_of_carT mor_image Rep_inverse mor_hsets
  2593             sbd_Cnotzero sbd_Card_order mor_Rep_thm coalgT_thm mor_T_final_thm tcoalg_thm))
  2594           ks isNode_hset_thmss carT_defs card_of_carT_thms
  2595           mor_image'_thms Rep_inverses (transpose mor_hset_thmss);
  2596 
  2597         val map_wpull_tacs =
  2598           map3 (K ooo mk_wpull_tac m coalg_thePull_thm mor_thePull_fst_thm mor_thePull_snd_thm
  2599             mor_thePull_pick_thm) unique_mor_thms (transpose pick_col_thmss) hset_defss;
  2600 
  2601         val tacss = map9 mk_tactics map_id_tacs map_comp_tacs map_cong_tacs set_nat_tacss bd_co_tacs
  2602           bd_cinf_tacs set_bd_tacss in_bd_tacs map_wpull_tacs;
  2603 
  2604         val fld_witss =
  2605           let
  2606             val witss = map2 (fn Ds => fn bnf => mk_wits_of_bnf
  2607               (replicate (nwits_of_bnf bnf) Ds)
  2608               (replicate (nwits_of_bnf bnf) (passiveAs @ Ts)) bnf) Dss bnfs;
  2609             fun close_wit (I, wit) = fold_rev Term.absfree (map (nth ys') I) wit;
  2610             fun wit_apply (arg_I, arg_wit) (fun_I, fun_wit) =
  2611               (union (op =) arg_I fun_I, fun_wit $ arg_wit);
  2612 
  2613             fun gen_arg support i =
  2614               if i < m then [([i], nth ys i)]
  2615               else maps (mk_wit support (nth flds (i - m)) (i - m)) (nth support (i - m))
  2616             and mk_wit support fld i (I, wit) =
  2617               let val args = map (gen_arg (nth_map i (remove (op =) (I, wit)) support)) I;
  2618               in
  2619                 (args, [([], wit)])
  2620                 |-> fold (map_product wit_apply)
  2621                 |> map (apsnd (fn t => fld $ t))
  2622                 |> minimize_wits
  2623               end;
  2624           in
  2625             map3 (fn fld => fn i => map close_wit o minimize_wits o maps (mk_wit witss fld i))
  2626               flds (0 upto n - 1) witss
  2627           end;
  2628 
  2629         val wit_tac = mk_wit_tac n unf_fld_thms (flat set_simp_thmss) (maps wit_thms_of_bnf bnfs);
  2630 
  2631         val (Jbnfs, lthy) =
  2632           fold_map6 (fn tacs => fn b => fn map => fn sets => fn T => fn wits =>
  2633             bnf_def Dont_Inline user_policy I tacs wit_tac (SOME deads)
  2634               ((((b, fold_rev Term.absfree fs' map), sets), absdummy T bd), wits))
  2635           tacss bs fs_maps setss_by_bnf Ts fld_witss lthy;
  2636 
  2637         val fold_maps = Local_Defs.fold lthy (map (fn bnf =>
  2638           mk_unabs_def m (map_def_of_bnf bnf RS @{thm meta_eq_to_obj_eq})) Jbnfs);
  2639 
  2640         val fold_sets = Local_Defs.fold lthy (maps (fn bnf =>
  2641          map (fn thm => thm RS @{thm meta_eq_to_obj_eq}) (set_defs_of_bnf bnf)) Jbnfs);
  2642 
  2643         val timer = time (timer "registered new codatatypes as BNFs");
  2644 
  2645         val (set_incl_thmss, set_set_incl_thmsss, set_induct_thms) =
  2646           let
  2647             fun tinst_of unf =
  2648               map (SOME o certify lthy) (unf :: remove (op =) unf unfs);
  2649             fun tinst_of' unf = case tinst_of unf of t :: ts => t :: NONE :: ts;
  2650             val Tinst = map (pairself (certifyT lthy))
  2651               (map Logic.varifyT_global (deads @ allAs) ~~ (deads @ passiveAs @ Ts));
  2652             val set_incl_thmss =
  2653               map2 (fn unf => map (singleton (Proof_Context.export names_lthy lthy) o
  2654                 fold_sets o Drule.instantiate' [] (tinst_of' unf) o
  2655                 Thm.instantiate (Tinst, []) o Drule.zero_var_indexes))
  2656               unfs set_incl_hset_thmss;
  2657 
  2658             val tinst = interleave (map (SOME o certify lthy) unfs) (replicate n NONE)
  2659             val set_minimal_thms =
  2660               map (fold_sets o Drule.instantiate' [] tinst o Thm.instantiate (Tinst, []) o
  2661                 Drule.zero_var_indexes)
  2662               hset_minimal_thms;
  2663 
  2664             val set_set_incl_thmsss =
  2665               map2 (fn unf => map (map (singleton (Proof_Context.export names_lthy lthy) o
  2666                 fold_sets o Drule.instantiate' [] (NONE :: tinst_of' unf) o
  2667                 Thm.instantiate (Tinst, []) o Drule.zero_var_indexes)))
  2668               unfs set_hset_incl_hset_thmsss;
  2669 
  2670             val set_set_incl_thmsss' = transpose (map transpose set_set_incl_thmsss);
  2671 
  2672             val incls =
  2673               maps (map (fn thm => thm RS @{thm subset_Collect_iff})) set_incl_thmss @
  2674                 @{thms subset_Collect_iff[OF subset_refl]};
  2675 
  2676             fun mk_induct_tinst phis jsets y y' =
  2677               map4 (fn phi => fn jset => fn Jz => fn Jz' =>
  2678                 SOME (certify lthy (Term.absfree Jz' (HOLogic.mk_Collect (fst y', snd y',
  2679                   HOLogic.mk_conj (HOLogic.mk_mem (y, jset $ Jz), phi $ y $ Jz))))))
  2680               phis jsets Jzs Jzs';
  2681             val set_induct_thms =
  2682               map6 (fn set_minimal => fn set_set_inclss => fn jsets => fn y => fn y' => fn phis =>
  2683                 ((set_minimal
  2684                   |> Drule.instantiate' [] (mk_induct_tinst phis jsets y y')
  2685                   |> fold_sets |> Local_Defs.unfold lthy incls) OF
  2686                   (replicate n ballI @
  2687                     maps (map (fn thm => thm RS @{thm subset_CollectI})) set_set_inclss))
  2688                 |> singleton (Proof_Context.export names_lthy lthy)
  2689                 |> rule_by_tactic lthy (ALLGOALS (TRY o etac asm_rl)))
  2690               set_minimal_thms set_set_incl_thmsss' setss_by_range ys ys' set_induct_phiss
  2691           in
  2692             (set_incl_thmss, set_set_incl_thmsss, set_induct_thms)
  2693           end;
  2694 
  2695         val rels = map2 (fn Ds => mk_rel_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2696         val Jrels = map (mk_rel_of_bnf deads passiveAs passiveBs) Jbnfs;
  2697         val preds = map2 (fn Ds => mk_pred_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
  2698         val Jpreds = map (mk_pred_of_bnf deads passiveAs passiveBs) Jbnfs;
  2699 
  2700         val JrelRs = map (fn Jrel => Term.list_comb (Jrel, JRs)) Jrels;
  2701         val relRs = map (fn rel => Term.list_comb (rel, JRs @ JrelRs)) rels;
  2702         val Jpredphis = map (fn Jrel => Term.list_comb (Jrel, Jphis)) Jpreds;
  2703         val predphis = map (fn rel => Term.list_comb (rel, Jphis @ Jpredphis)) preds;
  2704 
  2705         val in_rels = map in_rel_of_bnf bnfs;
  2706         val in_Jrels = map in_rel_of_bnf Jbnfs;
  2707         val Jpred_defs =
  2708           map (Drule.abs_def o (fn thm => thm RS @{thm eq_reflection}) o pred_def_of_bnf) Jbnfs;
  2709 
  2710         val folded_map_simp_thms = map fold_maps map_simp_thms;
  2711         val folded_set_simp_thmss = map (map fold_sets) set_simp_thmss;
  2712         val folded_set_simp_thmss' = transpose folded_set_simp_thmss;
  2713 
  2714         val Jrel_unfold_thms =
  2715           let
  2716             fun mk_goal Jz Jz' unf unf' JrelR relR = fold_rev Logic.all (Jz :: Jz' :: JRs)
  2717               (HOLogic.mk_Trueprop (HOLogic.mk_eq
  2718                 (HOLogic.mk_mem (HOLogic.mk_prod (Jz, Jz'), JrelR),
  2719                   HOLogic.mk_mem (HOLogic.mk_prod (unf $ Jz, unf' $ Jz'), relR))));
  2720             val goals = map6 mk_goal Jzs Jz's unfs unf's JrelRs relRs;
  2721           in
  2722             map12 (fn i => fn goal => fn in_rel => fn map_comp => fn map_cong =>
  2723               fn map_simp => fn set_simps => fn unf_inject => fn unf_fld =>
  2724               fn set_naturals => fn set_incls => fn set_set_inclss =>
  2725               Skip_Proof.prove lthy [] [] goal
  2726                (K (mk_rel_unfold_tac in_Jrels i in_rel map_comp map_cong map_simp set_simps
  2727                  unf_inject unf_fld set_naturals set_incls set_set_inclss)))
  2728             ks goals in_rels map_comp's map_congs folded_map_simp_thms folded_set_simp_thmss'
  2729               unf_inject_thms unf_fld_thms set_natural'ss set_incl_thmss set_set_incl_thmsss
  2730           end;
  2731 
  2732         val Jpred_unfold_thms =
  2733           let
  2734             fun mk_goal Jz Jz' unf unf' Jpredphi predphi = fold_rev Logic.all (Jz :: Jz' :: Jphis)
  2735               (HOLogic.mk_Trueprop (HOLogic.mk_eq
  2736                 (Jpredphi $ Jz $ Jz', predphi $ (unf $ Jz) $ (unf' $ Jz'))));
  2737             val goals = map6 mk_goal Jzs Jz's unfs unf's Jpredphis predphis;
  2738           in
  2739             map3 (fn goal => fn pred_def => fn Jrel_unfold =>
  2740               Skip_Proof.prove lthy [] [] goal (mk_pred_unfold_tac pred_def Jpred_defs Jrel_unfold))
  2741             goals pred_defs Jrel_unfold_thms
  2742           end;
  2743 
  2744         val timer = time (timer "additional properties");
  2745 
  2746         val ls' = if m = 1 then [0] else ls;
  2747       in
  2748         lthy
  2749         |> note map_uniqueN [fold_maps map_unique_thm]
  2750         |> notes map_simpsN (map single folded_map_simp_thms)
  2751         |> fold2 (fn i => notes (mk_set_simpsN i) o map single) ls' folded_set_simp_thmss
  2752         |> notes set_inclN set_incl_thmss
  2753         |> notes set_set_inclN (map flat set_set_incl_thmsss) (* nicer names? *)
  2754         |> fold2 (fn i => note (mk_set_inductN i) o single) ls' set_induct_thms
  2755         |> notes rel_unfoldN (map single Jrel_unfold_thms)
  2756         |> notes pred_unfoldN (map single Jpred_unfold_thms)
  2757       end;
  2758   in
  2759     lthy
  2760     |> notes coiterN (map single coiter_thms)
  2761     |> notes coiter_uniqueN (map single coiter_unique_thms)
  2762     |> notes corecN (map single corec_thms)
  2763     |> notes unf_fldN (map single unf_fld_thms)
  2764     |> notes fld_unfN (map single fld_unf_thms)
  2765     |> notes unf_injectN (map single unf_inject_thms)
  2766     |> notes unf_exhaustN (map single unf_exhaust_thms)
  2767     |> notes fld_injectN (map single fld_inject_thms)
  2768     |> notes fld_exhaustN (map single fld_exhaust_thms)
  2769     |> notes fld_coiterN (map single fld_coiter_thms)
  2770     |> note unf_coinductN [unf_coinduct_thm]
  2771     |> note rel_coinductN [rel_coinduct_thm]
  2772     |> note pred_coinductN [pred_coinduct_thm]
  2773     |> note unf_coinduct_uptoN [unf_coinduct_upto_thm]
  2774     |> note rel_coinduct_uptoN [rel_coinduct_upto_thm]
  2775     |> note pred_coinduct_uptoN [pred_coinduct_upto_thm]
  2776   end;
  2777 
  2778 val _ =
  2779   Outer_Syntax.local_theory @{command_spec "bnf_codata"} "greatest fixed points for BNF equations"
  2780     (Parse.and_list1
  2781       ((Parse.binding --| Parse.$$$ ":") -- (Parse.typ --| Parse.$$$ "=" -- Parse.typ)) >>
  2782       (fp_bnf_cmd bnf_gfp o apsnd split_list o split_list));
  2783 
  2784 end;