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