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