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