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