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