src/HOL/Tools/BNF/bnf_lfp.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_lfp.ML
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Author:     Andrei Popescu, TU Muenchen
blanchet@48975
     4
    Copyright   2012
blanchet@48975
     5
blanchet@58315
     6
Datatype construction.
blanchet@48975
     7
*)
blanchet@48975
     8
blanchet@48975
     9
signature BNF_LFP =
blanchet@48975
    10
sig
traytel@62324
    11
  val construct_lfp: mixfix list -> binding list -> binding list -> binding list ->
traytel@62324
    12
    binding list list -> binding list -> (string * sort) list -> typ list * typ list list ->
traytel@62324
    13
    BNF_Def.bnf list -> BNF_Comp.absT_info list -> local_theory ->
traytel@62324
    14
    BNF_FP_Util.fp_result * local_theory
blanchet@48975
    15
end;
blanchet@48975
    16
blanchet@48975
    17
structure BNF_LFP : BNF_LFP =
blanchet@48975
    18
struct
blanchet@48975
    19
blanchet@48975
    20
open BNF_Def
blanchet@48975
    21
open BNF_Util
blanchet@48975
    22
open BNF_Tactics
blanchet@49585
    23
open BNF_Comp
blanchet@51850
    24
open BNF_FP_Util
blanchet@49636
    25
open BNF_FP_Def_Sugar
blanchet@48975
    26
open BNF_LFP_Util
blanchet@48975
    27
open BNF_LFP_Tactics
blanchet@48975
    28
blanchet@49460
    29
(*all BNFs have the same lives*)
traytel@62684
    30
fun construct_lfp mixfixes map_bs rel_bs pred_bs set_bss0 bs resBs (resDs, Dss) bnfs absT_infos
traytel@62684
    31
    lthy =
blanchet@48975
    32
  let
traytel@53143
    33
    val time = time lthy;
blanchet@48975
    34
    val timer = time (Timer.startRealTimer ());
blanchet@49580
    35
blanchet@49132
    36
    val live = live_of_bnf (hd bnfs);
blanchet@48975
    37
    val n = length bnfs; (*active*)
blanchet@49132
    38
    val ks = 1 upto n;
blanchet@49460
    39
    val m = live - n; (*passive, if 0 don't generate a new BNF*)
traytel@53566
    40
wenzelm@62093
    41
    val internals = Config.get lthy bnf_internals;
blanchet@52956
    42
    val b_names = map Binding.name_of bs;
traytel@53566
    43
    val b_name = mk_common_name b_names;
traytel@53566
    44
    val b = Binding.name b_name;
blanchet@58241
    45
blanchet@58241
    46
    fun mk_internal_of_b name =
wenzelm@59859
    47
      Binding.prefix_name (name ^ "_") #> Binding.prefix true b_name #> Binding.concealed;
blanchet@58241
    48
    fun mk_internal_b name = mk_internal_of_b name b;
blanchet@58241
    49
    fun mk_internal_bs name = map (mk_internal_of_b name) bs;
traytel@53566
    50
    val external_bs = map2 (Binding.prefix false) b_names bs
wenzelm@62093
    51
      |> not internals ? map Binding.concealed;
blanchet@48975
    52
traytel@49185
    53
    val deads = fold (union (op =)) Dss resDs;
blanchet@48975
    54
    val names_lthy = fold Variable.declare_typ deads lthy;
blanchet@52956
    55
    val passives = map fst (subtract (op = o apsnd TFree) deads resBs);
blanchet@48975
    56
blanchet@48975
    57
    (* tvars *)
traytel@54841
    58
    val (((((passiveAs, activeAs), passiveBs), activeBs), passiveCs), activeCs) =
blanchet@52938
    59
      names_lthy
blanchet@52956
    60
      |> variant_tfrees passives
blanchet@52938
    61
      ||>> mk_TFrees n
blanchet@52956
    62
      ||>> variant_tfrees passives
blanchet@52938
    63
      ||>> mk_TFrees n
traytel@54841
    64
      ||>> variant_tfrees passives
blanchet@48975
    65
      ||>> mk_TFrees n
traytel@54841
    66
      |> fst;
blanchet@48975
    67
blanchet@52938
    68
    val allAs = passiveAs @ activeAs;
blanchet@52938
    69
    val allBs' = passiveBs @ activeBs;
blanchet@48975
    70
    val Ass = replicate n allAs;
blanchet@48975
    71
    val allBs = passiveAs @ activeBs;
blanchet@48975
    72
    val Bss = replicate n allBs;
blanchet@48975
    73
    val allCs = passiveAs @ activeCs;
blanchet@48975
    74
    val allCs' = passiveBs @ activeCs;
blanchet@48975
    75
    val Css' = replicate n allCs';
blanchet@48975
    76
blanchet@51866
    77
    (* types *)
traytel@49185
    78
    val dead_poss =
blanchet@52956
    79
      map (fn x => if member (op =) deads (TFree x) then SOME (TFree x) else NONE) resBs;
traytel@49185
    80
    fun mk_param NONE passive = (hd passive, tl passive)
traytel@49185
    81
      | mk_param (SOME a) passive = (a, passive);
traytel@49185
    82
    val mk_params = fold_map mk_param dead_poss #> fst;
traytel@49185
    83
blanchet@48975
    84
    fun mk_FTs Ts = map2 (fn Ds => mk_T_of_bnf Ds Ts) Dss bnfs;
traytel@49185
    85
    val (params, params') = `(map Term.dest_TFree) (mk_params passiveAs);
blanchet@48975
    86
    val FTsAs = mk_FTs allAs;
blanchet@48975
    87
    val FTsBs = mk_FTs allBs;
blanchet@48975
    88
    val FTsCs = mk_FTs allCs;
blanchet@48975
    89
    val BTs = map HOLogic.mk_setT activeAs;
blanchet@48975
    90
    val B'Ts = map HOLogic.mk_setT activeBs;
blanchet@48975
    91
    val B''Ts = map HOLogic.mk_setT activeCs;
traytel@52923
    92
    val sTs = map2 (curry op -->) FTsAs activeAs;
traytel@52923
    93
    val s'Ts = map2 (curry op -->) FTsBs activeBs;
traytel@52923
    94
    val s''Ts = map2 (curry op -->) FTsCs activeCs;
traytel@52923
    95
    val fTs = map2 (curry op -->) activeAs activeBs;
traytel@52923
    96
    val inv_fTs = map2 (curry op -->) activeBs activeAs;
traytel@52923
    97
    val self_fTs = map2 (curry op -->) activeAs activeAs;
traytel@52923
    98
    val gTs = map2 (curry op -->) activeBs activeCs;
traytel@52923
    99
    val all_gTs = map2 (curry op -->) allBs allCs';
blanchet@48975
   100
    val prodBsAs = map2 (curry HOLogic.mk_prodT) activeBs activeAs;
blanchet@48975
   101
    val prodFTs = mk_FTs (passiveAs @ prodBsAs);
traytel@52923
   102
    val prod_sTs = map2 (curry op -->) prodFTs activeAs;
blanchet@48975
   103
blanchet@48975
   104
    (* terms *)
wenzelm@58634
   105
    val mapsAsAs = @{map 4} mk_map_of_bnf Dss Ass Ass bnfs;
wenzelm@58634
   106
    val mapsAsBs = @{map 4} mk_map_of_bnf Dss Ass Bss bnfs;
wenzelm@58634
   107
    val mapsBsCs' = @{map 4} mk_map_of_bnf Dss Bss Css' bnfs;
wenzelm@58634
   108
    val mapsAsCs' = @{map 4} mk_map_of_bnf Dss Ass Css' bnfs;
wenzelm@58634
   109
    val map_fsts = @{map 4} mk_map_of_bnf Dss (replicate n (passiveAs @ prodBsAs)) Bss bnfs;
wenzelm@58634
   110
    val map_fsts_rev = @{map 4} mk_map_of_bnf Dss Bss (replicate n (passiveAs @ prodBsAs)) bnfs;
wenzelm@58634
   111
    fun mk_setss Ts = @{map 3} mk_sets_of_bnf (map (replicate live) Dss)
blanchet@48975
   112
      (map (replicate live) (replicate n Ts)) bnfs;
blanchet@48975
   113
    val setssAs = mk_setss allAs;
wenzelm@58634
   114
    val bd0s = @{map 3} mk_bd_of_bnf Dss Ass bnfs;
traytel@52635
   115
    val bds =
wenzelm@58634
   116
      @{map 3} (fn bd0 => fn Ds => fn bnf => mk_csum bd0
traytel@52635
   117
        (mk_card_of (HOLogic.mk_UNIV
traytel@52635
   118
          (mk_T_of_bnf Ds (replicate live (fst (dest_relT (fastype_of bd0)))) bnf))))
traytel@52635
   119
      bd0s Dss bnfs;
blanchet@48975
   120
    val witss = map wits_of_bnf bnfs;
blanchet@48975
   121
traytel@61334
   122
    val ((((((((zs, zs'), Bs), ss), fs), self_fs), all_gs), (xFs, xFs')), _) =
traytel@61272
   123
      lthy
blanchet@48975
   124
      |> mk_Frees' "z" activeAs
blanchet@48975
   125
      ||>> mk_Frees "B" BTs
blanchet@48975
   126
      ||>> mk_Frees "s" sTs
blanchet@48975
   127
      ||>> mk_Frees "f" fTs
blanchet@48975
   128
      ||>> mk_Frees "f" self_fTs
blanchet@48975
   129
      ||>> mk_Frees "g" all_gTs
traytel@55756
   130
      ||>> mk_Frees' "x" FTsAs;
blanchet@48975
   131
blanchet@48975
   132
    val passive_UNIVs = map HOLogic.mk_UNIV passiveAs;
blanchet@48975
   133
    val active_UNIVs = map HOLogic.mk_UNIV activeAs;
blanchet@48975
   134
    val prod_UNIVs = map HOLogic.mk_UNIV prodBsAs;
blanchet@48975
   135
    val passive_ids = map HOLogic.id_const passiveAs;
blanchet@48975
   136
    val active_ids = map HOLogic.id_const activeAs;
blanchet@48975
   137
    val fsts = map fst_const prodBsAs;
blanchet@48975
   138
blanchet@48975
   139
    (* thms *)
traytel@52635
   140
    val bd0_card_orders = map bd_card_order_of_bnf bnfs;
traytel@52635
   141
    val bd0_Card_orders = map bd_Card_order_of_bnf bnfs;
traytel@52635
   142
    val bd0_Cinfinites = map bd_Cinfinite_of_bnf bnfs;
traytel@52635
   143
    val set_bd0ss = map set_bd_of_bnf bnfs;
traytel@52635
   144
traytel@52635
   145
    val bd_Card_order = @{thm Card_order_csum};
traytel@52635
   146
    val bd_Card_orders = replicate n bd_Card_order;
traytel@52635
   147
    val bd_Cinfinites = map (fn thm => thm RS @{thm Cinfinite_csum1}) bd0_Cinfinites;
traytel@52635
   148
    val bd_Cnotzeros = map (fn thm => thm RS @{thm Cinfinite_Cnotzero}) bd_Cinfinites;
traytel@52635
   149
    val bd_Cinfinite = hd bd_Cinfinites;
traytel@52635
   150
    val set_bdss =
traytel@52635
   151
      map2 (fn set_bd0s => fn bd0_Card_order =>
traytel@52635
   152
        map (fn thm => ctrans OF [thm, bd0_Card_order RS @{thm ordLeq_csum1}]) set_bd0s)
traytel@52635
   153
      set_bd0ss bd0_Card_orders;
blanchet@48975
   154
    val in_bds = map in_bd_of_bnf bnfs;
blanchet@53287
   155
    val sym_map_comps = map (fn bnf => map_comp0_of_bnf bnf RS sym) bnfs;
blanchet@53288
   156
    val map_comps = map map_comp_of_bnf bnfs;
blanchet@51761
   157
    val map_cong0s = map map_cong0_of_bnf bnfs;
blanchet@53270
   158
    val map_id0s = map map_id0_of_bnf bnfs;
blanchet@53285
   159
    val map_ids = map map_id_of_bnf bnfs;
blanchet@53290
   160
    val set_mapss = map set_map_of_bnf bnfs;
desharna@57967
   161
    val rel_mono_strong0s = map rel_mono_strong0_of_bnf bnfs;
traytel@57726
   162
    val le_rel_OOs = map le_rel_OO_of_bnf bnfs;
blanchet@48975
   163
blanchet@48975
   164
    val timer = time (timer "Extracted terms & thms");
blanchet@48975
   165
blanchet@48975
   166
    (* nonemptiness check *)
traytel@51070
   167
    fun new_wit X (wit: nonemptiness_witness) = subset (op =) (#I wit, (0 upto m - 1) @ map snd X);
blanchet@48975
   168
blanchet@49341
   169
    val all = m upto m + n - 1;
blanchet@49341
   170
blanchet@48975
   171
    fun enrich X = map_filter (fn i =>
blanchet@48975
   172
      (case find_first (fn (_, i') => i = i') X of
blanchet@48975
   173
        NONE =>
blanchet@48975
   174
          (case find_index (new_wit X) (nth witss (i - m)) of
blanchet@48975
   175
            ~1 => NONE
blanchet@48975
   176
          | j => SOME (j, i))
blanchet@49341
   177
      | SOME ji => SOME ji)) all;
blanchet@48975
   178
    val reachable = fixpoint (op =) enrich [];
blanchet@49341
   179
    val _ = (case subtract (op =) (map snd reachable) all of
blanchet@49341
   180
        [] => ()
blanchet@49390
   181
      | i :: _ => error ("Cannot define empty datatype " ^ quote (Binding.name_of (nth bs (i - m)))));
blanchet@48975
   182
blanchet@49341
   183
    val wit_thms = flat (map2 (fn bnf => fn (j, _) => nth (wit_thmss_of_bnf bnf) j) bnfs reachable);
blanchet@48975
   184
blanchet@48975
   185
    val timer = time (timer "Checked nonemptiness");
blanchet@48975
   186
blanchet@48975
   187
    (* derived thms *)
blanchet@48975
   188
blanchet@52956
   189
    (*map g1 ... gm g(m+1) ... g(m+n) (map id ... id f(m+1) ... f(m+n) x) =
blanchet@48975
   190
      map g1 ... gm (g(m+1) o f(m+1)) ... (g(m+n) o f(m+n)) x*)
blanchet@53287
   191
    fun mk_map_comp_id x mapAsBs mapBsCs mapAsCs map_comp0 =
blanchet@48975
   192
      let
blanchet@48975
   193
        val lhs = Term.list_comb (mapBsCs, all_gs) $
blanchet@48975
   194
          (Term.list_comb (mapAsBs, passive_ids @ fs) $ x);
blanchet@48975
   195
        val rhs = Term.list_comb (mapAsCs,
blanchet@48975
   196
          take m all_gs @ map HOLogic.mk_comp (drop m all_gs ~~ fs)) $ x;
traytel@61334
   197
        val vars = fold (Variable.add_free_names lthy) [lhs, rhs] [];
blanchet@48975
   198
      in
traytel@61334
   199
        Goal.prove_sorry lthy vars [] (mk_Trueprop_eq (lhs, rhs))
traytel@55756
   200
          (fn {context = ctxt, prems = _} => mk_map_comp_id_tac ctxt map_comp0)
traytel@49109
   201
        |> Thm.close_derivation
blanchet@48975
   202
      end;
blanchet@48975
   203
wenzelm@58634
   204
    val map_comp_id_thms = @{map 5} mk_map_comp_id xFs mapsAsBs mapsBsCs' mapsAsCs' map_comps;
blanchet@48975
   205
blanchet@48975
   206
    (*forall a : set(m+1) x. f(m+1) a = a; ...; forall a : set(m+n) x. f(m+n) a = a ==>
blanchet@48975
   207
      map id ... id f(m+1) ... f(m+n) x = x*)
blanchet@53285
   208
    fun mk_map_cong0L x mapAsAs sets map_cong0 map_id =
blanchet@48975
   209
      let
blanchet@48975
   210
        fun mk_prem set f z z' = HOLogic.mk_Trueprop
blanchet@48975
   211
          (mk_Ball (set $ x) (Term.absfree z' (HOLogic.mk_eq (f $ z, z))));
wenzelm@58634
   212
        val prems = @{map 4} mk_prem (drop m sets) self_fs zs zs';
blanchet@49123
   213
        val goal = mk_Trueprop_eq (Term.list_comb (mapAsAs, passive_ids @ self_fs) $ x, x);
traytel@61334
   214
        val vars = fold (Variable.add_free_names lthy) (goal :: prems) [];
blanchet@48975
   215
      in
traytel@61334
   216
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, goal))
traytel@60728
   217
          (fn {context = ctxt, prems = _} => mk_map_cong0L_tac ctxt m map_cong0 map_id)
traytel@49109
   218
        |> Thm.close_derivation
blanchet@48975
   219
      end;
blanchet@48975
   220
wenzelm@58634
   221
    val map_cong0L_thms = @{map 5} mk_map_cong0L xFs mapsAsAs setssAs map_cong0s map_ids;
blanchet@49585
   222
    val in_mono'_thms = map (fn bnf => in_mono_of_bnf bnf OF (replicate m subset_refl)) bnfs;
blanchet@49585
   223
    val in_cong'_thms = map (fn bnf => in_cong_of_bnf bnf OF (replicate m refl)) bnfs;
blanchet@48975
   224
blanchet@48975
   225
    val timer = time (timer "Derived simple theorems");
blanchet@48975
   226
blanchet@48975
   227
    (* algebra *)
blanchet@48975
   228
traytel@53566
   229
    val alg_bind = mk_internal_b algN;
blanchet@48975
   230
    val alg_def_bind = (Thm.def_binding alg_bind, []);
blanchet@48975
   231
traytel@55541
   232
    (*forall i = 1 ... n: (\<forall>x \<in> Fi_in UNIV .. UNIV B1 ... Bn. si x \<in> Bi)*)
blanchet@48975
   233
    val alg_spec =
blanchet@48975
   234
      let
wenzelm@58634
   235
        val ins = @{map 3} mk_in (replicate n (passive_UNIVs @ Bs)) setssAs FTsAs;
blanchet@48975
   236
        fun mk_alg_conjunct B s X x x' =
blanchet@48975
   237
          mk_Ball X (Term.absfree x' (HOLogic.mk_mem (s $ x, B)));
blanchet@48975
   238
wenzelm@58634
   239
        val rhs = Library.foldr1 HOLogic.mk_conj (@{map 5} mk_alg_conjunct Bs ss ins xFs xFs')
blanchet@48975
   240
      in
traytel@55541
   241
        fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss) rhs
blanchet@48975
   242
      end;
blanchet@48975
   243
blanchet@48975
   244
    val ((alg_free, (_, alg_def_free)), (lthy, lthy_old)) =
blanchet@58332
   245
      lthy
traytel@61101
   246
      |> Local_Theory.open_target |> snd
blanchet@58332
   247
      |> Local_Theory.define ((alg_bind, NoSyn), (alg_def_bind, alg_spec))
traytel@61101
   248
      ||> `Local_Theory.close_target;
blanchet@48975
   249
blanchet@48975
   250
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   251
    val alg = fst (Term.dest_Const (Morphism.term phi alg_free));
traytel@55541
   252
    val alg_def = mk_unabs_def (2 * n) (Morphism.thm phi alg_def_free RS meta_eq_to_obj_eq);
blanchet@48975
   253
traytel@55541
   254
    fun mk_alg Bs ss =
blanchet@48975
   255
      let
traytel@55541
   256
        val args = Bs @ ss;
blanchet@48975
   257
        val Ts = map fastype_of args;
blanchet@48975
   258
        val algT = Library.foldr (op -->) (Ts, HOLogic.boolT);
blanchet@48975
   259
      in
blanchet@48975
   260
        Term.list_comb (Const (alg, algT), args)
blanchet@48975
   261
      end;
blanchet@48975
   262
traytel@61334
   263
    val ((((((((zs, zs'), Bs), B's), ss), s's), fs), (xFs, xFs')), _) =
traytel@61272
   264
      lthy
traytel@61272
   265
      |> mk_Frees' "z" activeAs
traytel@61272
   266
      ||>> mk_Frees "B" BTs
traytel@61272
   267
      ||>> mk_Frees "B'" B'Ts
traytel@61272
   268
      ||>> mk_Frees "s" sTs
traytel@61272
   269
      ||>> mk_Frees "s'" s'Ts
traytel@61272
   270
      ||>> mk_Frees "f" fTs
traytel@61272
   271
      ||>> mk_Frees' "x" FTsAs;
traytel@61272
   272
blanchet@48975
   273
    val alg_set_thms =
blanchet@48975
   274
      let
traytel@55541
   275
        val alg_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
traytel@51893
   276
        fun mk_prem x set B = HOLogic.mk_Trueprop (mk_leq (set $ x) B);
desharna@57567
   277
        fun mk_concl s x B = mk_Trueprop_mem (s $ x, B);
traytel@55541
   278
        val premss = map2 ((fn x => fn sets => map2 (mk_prem x) (drop m sets) Bs)) xFs setssAs;
wenzelm@58634
   279
        val concls = @{map 3} mk_concl ss xFs Bs;
traytel@56272
   280
        val goals = map2 (fn prems => fn concl =>
traytel@56272
   281
          Logic.list_implies (alg_prem :: prems, concl)) premss concls;
blanchet@48975
   282
      in
traytel@49109
   283
        map (fn goal =>
traytel@61334
   284
          Variable.add_free_names lthy goal []
traytel@61334
   285
          |> (fn vars => Goal.prove_sorry lthy vars [] goal (fn {context = ctxt, prems = _} =>
traytel@61334
   286
            mk_alg_set_tac ctxt alg_def))
traytel@61334
   287
          |> Thm.close_derivation)
traytel@49109
   288
        goals
blanchet@48975
   289
      end;
blanchet@48975
   290
blanchet@48975
   291
    val timer = time (timer "Algebra definition & thms");
blanchet@48975
   292
blanchet@48975
   293
    val alg_not_empty_thms =
blanchet@48975
   294
      let
blanchet@48975
   295
        val alg_prem =
traytel@55541
   296
          HOLogic.mk_Trueprop (mk_alg Bs ss);
blanchet@48975
   297
        val concls = map (HOLogic.mk_Trueprop o mk_not_empty) Bs;
blanchet@48975
   298
        val goals =
traytel@56272
   299
          map (fn concl => Logic.mk_implies (alg_prem, concl)) concls;
blanchet@48975
   300
      in
blanchet@48975
   301
        map2 (fn goal => fn alg_set =>
traytel@61334
   302
          Variable.add_free_names lthy goal []
traytel@61334
   303
          |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@61271
   304
            (fn {context = ctxt, prems = _} =>
traytel@61334
   305
              mk_alg_not_empty_tac ctxt alg_set alg_set_thms wit_thms))
traytel@61334
   306
          |> Thm.close_derivation)
traytel@49109
   307
        goals alg_set_thms
blanchet@48975
   308
      end;
blanchet@48975
   309
blanchet@48975
   310
    val timer = time (timer "Proved nonemptiness");
blanchet@48975
   311
blanchet@48975
   312
    (* morphism *)
blanchet@48975
   313
traytel@53566
   314
    val mor_bind = mk_internal_b morN;
blanchet@48975
   315
    val mor_def_bind = (Thm.def_binding mor_bind, []);
blanchet@48975
   316
blanchet@48975
   317
    (*fbetw) forall i = 1 ... n: (\<forall>x \<in> Bi. f x \<in> B'i)*)
blanchet@48975
   318
    (*mor) forall i = 1 ... n: (\<forall>x \<in> Fi_in UNIV ... UNIV B1 ... Bn.
blanchet@48975
   319
       f (s1 x) = s1' (Fi_map id ... id f1 ... fn x))*)
blanchet@48975
   320
    val mor_spec =
blanchet@48975
   321
      let
blanchet@48975
   322
        fun mk_fbetw f B1 B2 z z' =
blanchet@48975
   323
          mk_Ball B1 (Term.absfree z' (HOLogic.mk_mem (f $ z, B2)));
blanchet@48975
   324
        fun mk_mor sets mapAsBs f s s' T x x' =
blanchet@48975
   325
          mk_Ball (mk_in (passive_UNIVs @ Bs) sets T)
blanchet@48975
   326
            (Term.absfree x' (HOLogic.mk_eq (f $ (s $ x), s' $
blanchet@48975
   327
              (Term.list_comb (mapAsBs, passive_ids @ fs) $ x))));
blanchet@48975
   328
        val rhs = HOLogic.mk_conj
wenzelm@58634
   329
          (Library.foldr1 HOLogic.mk_conj (@{map 5} mk_fbetw fs Bs B's zs zs'),
blanchet@48975
   330
          Library.foldr1 HOLogic.mk_conj
wenzelm@58634
   331
            (@{map 8} mk_mor setssAs mapsAsBs fs ss s's FTsAs xFs xFs'))
blanchet@48975
   332
      in
traytel@55204
   333
        fold_rev (Term.absfree o Term.dest_Free) (Bs @ ss @ B's @ s's @ fs) rhs
blanchet@48975
   334
      end;
blanchet@48975
   335
blanchet@48975
   336
    val ((mor_free, (_, mor_def_free)), (lthy, lthy_old)) =
blanchet@59794
   337
      lthy
traytel@61101
   338
      |> Local_Theory.open_target |> snd
blanchet@59794
   339
      |> Local_Theory.define ((mor_bind, NoSyn), (mor_def_bind, mor_spec))
traytel@61101
   340
      ||> `Local_Theory.close_target;
blanchet@48975
   341
blanchet@48975
   342
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   343
    val mor = fst (Term.dest_Const (Morphism.term phi mor_free));
traytel@55204
   344
    val mor_def = mk_unabs_def (5 * n) (Morphism.thm phi mor_def_free RS meta_eq_to_obj_eq);
blanchet@48975
   345
blanchet@48975
   346
    fun mk_mor Bs1 ss1 Bs2 ss2 fs =
blanchet@48975
   347
      let
blanchet@48975
   348
        val args = Bs1 @ ss1 @ Bs2 @ ss2 @ fs;
blanchet@48975
   349
        val Ts = map fastype_of (Bs1 @ ss1 @ Bs2 @ ss2 @ fs);
blanchet@48975
   350
        val morT = Library.foldr (op -->) (Ts, HOLogic.boolT);
blanchet@48975
   351
      in
blanchet@48975
   352
        Term.list_comb (Const (mor, morT), args)
blanchet@48975
   353
      end;
blanchet@48975
   354
traytel@61272
   355
    val ((((((((((((Bs, Bs_copy), B's), B''s), ss), prod_ss), s's), s''s), fs), fs_copy), gs), xFs),
traytel@61334
   356
        _) =
traytel@61272
   357
      lthy
traytel@61272
   358
      |> mk_Frees "B" BTs
traytel@61272
   359
      ||>> mk_Frees "B" BTs
traytel@61272
   360
      ||>> mk_Frees "B'" B'Ts
traytel@61272
   361
      ||>> mk_Frees "B''" B''Ts
traytel@61272
   362
      ||>> mk_Frees "s" sTs
traytel@61272
   363
      ||>> mk_Frees "prods" prod_sTs
traytel@61272
   364
      ||>> mk_Frees "s'" s'Ts
traytel@61272
   365
      ||>> mk_Frees "s''" s''Ts
traytel@61272
   366
      ||>> mk_Frees "f" fTs
traytel@61272
   367
      ||>> mk_Frees "f" fTs
traytel@61272
   368
      ||>> mk_Frees "g" gTs
traytel@61272
   369
      ||>> mk_Frees "x" FTsAs;
traytel@61272
   370
traytel@56237
   371
    val morE_thms =
blanchet@48975
   372
      let
blanchet@48975
   373
        val prem = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs);
blanchet@48975
   374
        fun mk_elim_prem sets x T = HOLogic.mk_Trueprop
blanchet@48975
   375
          (HOLogic.mk_mem (x, mk_in (passive_UNIVs @ Bs) sets T));
blanchet@48975
   376
        fun mk_elim_goal sets mapAsBs f s s' x T =
traytel@56272
   377
          Logic.list_implies ([prem, mk_elim_prem sets x T],
traytel@56272
   378
            mk_Trueprop_eq (f $ (s $ x), s' $ Term.list_comb (mapAsBs, passive_ids @ fs @ [x])));
wenzelm@58634
   379
        val elim_goals = @{map 7} mk_elim_goal setssAs mapsAsBs fs ss s's xFs FTsAs;
traytel@61334
   380
        fun prove goal =
traytel@61334
   381
          Variable.add_free_names lthy goal []
traytel@61334
   382
          |> (fn vars => Goal.prove_sorry lthy vars [] goal (fn {context = ctxt, prems = _} =>
traytel@61334
   383
            mk_mor_elim_tac ctxt mor_def))
traytel@61334
   384
          |> Thm.close_derivation;
blanchet@48975
   385
      in
traytel@56237
   386
        map prove elim_goals
blanchet@48975
   387
      end;
blanchet@48975
   388
blanchet@48975
   389
    val mor_incl_thm =
blanchet@48975
   390
      let
traytel@51893
   391
        val prems = map2 (HOLogic.mk_Trueprop oo mk_leq) Bs Bs_copy;
blanchet@48975
   392
        val concl = HOLogic.mk_Trueprop (mk_mor Bs ss Bs_copy ss active_ids);
traytel@61334
   393
        val vars = fold (Variable.add_free_names lthy) (concl :: prems) [];
blanchet@48975
   394
      in
traytel@61334
   395
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, concl))
traytel@60728
   396
          (fn {context = ctxt, prems = _} => mk_mor_incl_tac ctxt mor_def map_ids)
traytel@49109
   397
        |> Thm.close_derivation
blanchet@48975
   398
      end;
blanchet@48975
   399
blanchet@48975
   400
    val mor_comp_thm =
blanchet@48975
   401
      let
blanchet@48975
   402
        val prems =
blanchet@48975
   403
          [HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs),
blanchet@48975
   404
           HOLogic.mk_Trueprop (mk_mor B's s's B''s s''s gs)];
blanchet@48975
   405
        val concl =
blanchet@48975
   406
          HOLogic.mk_Trueprop (mk_mor Bs ss B''s s''s (map2 (curry HOLogic.mk_comp) gs fs));
traytel@61334
   407
        val vars = fold (Variable.add_free_names lthy) (concl :: prems) [];
blanchet@48975
   408
      in
traytel@61334
   409
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, concl))
traytel@60728
   410
          (fn {context = ctxt, prems = _} => mk_mor_comp_tac ctxt mor_def set_mapss map_comp_id_thms)
traytel@49109
   411
        |> Thm.close_derivation
blanchet@48975
   412
      end;
blanchet@48975
   413
blanchet@48975
   414
    val mor_cong_thm =
blanchet@48975
   415
      let
blanchet@48975
   416
        val prems = map HOLogic.mk_Trueprop
blanchet@48975
   417
         (map2 (curry HOLogic.mk_eq) fs_copy fs @ [mk_mor Bs ss B's s's fs])
blanchet@48975
   418
        val concl = HOLogic.mk_Trueprop (mk_mor Bs ss B's s's fs_copy);
traytel@61334
   419
        val vars = fold (Variable.add_free_names lthy) (concl :: prems) [];
blanchet@48975
   420
      in
traytel@61334
   421
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, concl))
traytel@60728
   422
          (fn {context = ctxt, prems = _} => (hyp_subst_tac ctxt THEN' assume_tac ctxt) 1)
traytel@49109
   423
        |> Thm.close_derivation
blanchet@48975
   424
      end;
blanchet@48975
   425
blanchet@48975
   426
    val mor_str_thm =
blanchet@48975
   427
      let
blanchet@48975
   428
        val maps = map2 (fn Ds => fn bnf => Term.list_comb
blanchet@48975
   429
          (mk_map_of_bnf Ds (passiveAs @ FTsAs) allAs bnf, passive_ids @ ss)) Dss bnfs;
traytel@61334
   430
        val goal = HOLogic.mk_Trueprop
traytel@61334
   431
          (mk_mor (map HOLogic.mk_UNIV FTsAs) maps active_UNIVs ss ss);
traytel@61334
   432
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
   433
      in
traytel@61334
   434
        Goal.prove_sorry lthy vars [] goal
traytel@60728
   435
          (fn {context = ctxt, prems = _} => mk_mor_str_tac ctxt ks mor_def)
traytel@49109
   436
        |> Thm.close_derivation
blanchet@48975
   437
      end;
blanchet@48975
   438
blanchet@48975
   439
    val mor_convol_thm =
blanchet@48975
   440
      let
wenzelm@58634
   441
        val maps = @{map 3} (fn s => fn prod_s => fn mapx =>
blanchet@49458
   442
          mk_convol (HOLogic.mk_comp (s, Term.list_comb (mapx, passive_ids @ fsts)), prod_s))
blanchet@48975
   443
          s's prod_ss map_fsts;
traytel@61334
   444
        val goal = HOLogic.mk_Trueprop
traytel@61334
   445
          (mk_mor prod_UNIVs maps (map HOLogic.mk_UNIV activeBs) s's fsts)
traytel@61334
   446
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
   447
      in
traytel@61334
   448
        Goal.prove_sorry lthy vars [] goal
traytel@60728
   449
          (fn {context = ctxt, prems = _} => mk_mor_convol_tac ctxt ks mor_def)
traytel@49109
   450
        |> Thm.close_derivation
blanchet@48975
   451
      end;
blanchet@48975
   452
blanchet@48975
   453
    val mor_UNIV_thm =
blanchet@48975
   454
      let
blanchet@48975
   455
        fun mk_conjunct mapAsBs f s s' = HOLogic.mk_eq
blanchet@48975
   456
            (HOLogic.mk_comp (f, s),
blanchet@48975
   457
            HOLogic.mk_comp (s', Term.list_comb (mapAsBs, passive_ids @ fs)));
blanchet@48975
   458
        val lhs = mk_mor active_UNIVs ss (map HOLogic.mk_UNIV activeBs) s's fs;
wenzelm@58634
   459
        val rhs = Library.foldr1 HOLogic.mk_conj (@{map 4} mk_conjunct mapsAsBs fs ss s's);
traytel@61334
   460
        val vars = fold (Variable.add_free_names lthy) [lhs, rhs] [];
blanchet@48975
   461
      in
traytel@61334
   462
        Goal.prove_sorry lthy vars [] (mk_Trueprop_eq (lhs, rhs))
traytel@60728
   463
          (fn {context = ctxt, prems = _} => mk_mor_UNIV_tac ctxt m morE_thms mor_def)
traytel@49109
   464
        |> Thm.close_derivation
blanchet@48975
   465
      end;
blanchet@48975
   466
blanchet@48975
   467
    val timer = time (timer "Morphism definition & thms");
blanchet@48975
   468
blanchet@48975
   469
    (* bounds *)
blanchet@48975
   470
traytel@55770
   471
    val sum_bd = Library.foldr1 (uncurry mk_csum) bds;
traytel@55770
   472
    val sum_bdT = fst (dest_relT (fastype_of sum_bd));
traytel@56516
   473
    val (sum_bdT_params, sum_bdT_params') = `(map TFree) (Term.add_tfreesT sum_bdT []);
traytel@55770
   474
traytel@56350
   475
    val (lthy, sbd, sbd_Cinfinite, sbd_Card_order, set_sbdss, in_sbds) =
traytel@55770
   476
      if n = 1
traytel@56350
   477
      then (lthy, sum_bd, bd_Cinfinite, bd_Card_order, set_bdss, in_bds)
traytel@55770
   478
      else
traytel@55770
   479
        let
traytel@55770
   480
          val sbdT_bind = mk_internal_b sum_bdTN;
traytel@55770
   481
traytel@55770
   482
          val ((sbdT_name, (sbdT_glob_info, sbdT_loc_info)), lthy) =
traytel@56516
   483
            typedef (sbdT_bind, sum_bdT_params', NoSyn)
traytel@60728
   484
              (HOLogic.mk_UNIV sum_bdT) NONE (fn ctxt =>
traytel@60728
   485
                EVERY' [rtac ctxt exI, rtac ctxt UNIV_I] 1) lthy;
traytel@55770
   486
traytel@56516
   487
          val sbdT = Type (sbdT_name, sum_bdT_params);
traytel@55770
   488
          val Abs_sbdT = Const (#Abs_name sbdT_glob_info, sum_bdT --> sbdT);
traytel@55770
   489
traytel@55770
   490
          val sbd_bind = mk_internal_b sum_bdN;
traytel@55770
   491
          val sbd_def_bind = (Thm.def_binding sbd_bind, []);
traytel@55770
   492
traytel@55770
   493
          val sbd_spec = mk_dir_image sum_bd Abs_sbdT;
traytel@55770
   494
traytel@55770
   495
          val ((sbd_free, (_, sbd_def_free)), (lthy, lthy_old)) =
traytel@55770
   496
            lthy
traytel@61101
   497
            |> Local_Theory.open_target |> snd
traytel@55770
   498
            |> Local_Theory.define ((sbd_bind, NoSyn), (sbd_def_bind, sbd_spec))
traytel@61101
   499
            ||> `Local_Theory.close_target;
traytel@55770
   500
traytel@55770
   501
          val phi = Proof_Context.export_morphism lthy_old lthy;
traytel@55770
   502
traytel@55770
   503
          val sbd_def = Morphism.thm phi sbd_def_free RS meta_eq_to_obj_eq;
traytel@55770
   504
          val sbd = Const (fst (Term.dest_Const (Morphism.term phi sbd_free)), mk_relT (`I sbdT));
traytel@55770
   505
traytel@55770
   506
          val Abs_sbdT_inj = mk_Abs_inj_thm (#Abs_inject sbdT_loc_info);
blanchet@48975
   507
traytel@55770
   508
          val sum_Cinfinite = mk_sum_Cinfinite bd_Cinfinites;
traytel@55770
   509
          val sum_Card_order = sum_Cinfinite RS conjunct2;
traytel@55770
   510
traytel@55770
   511
          val sbd_ordIso = @{thm ssubst_Pair_rhs} OF
traytel@55770
   512
            [@{thm dir_image} OF [Abs_sbdT_inj, sum_Card_order], sbd_def];
traytel@55770
   513
          val sbd_Cinfinite = @{thm Cinfinite_cong} OF [sbd_ordIso, sum_Cinfinite];
traytel@55770
   514
          val sbd_Card_order = sbd_Cinfinite RS conjunct2;
blanchet@48975
   515
traytel@55770
   516
          fun mk_set_sbd i bd_Card_order bds =
traytel@55770
   517
            map (fn thm => @{thm ordLeq_ordIso_trans} OF
traytel@55770
   518
              [bd_Card_order RS mk_ordLeq_csum n i thm, sbd_ordIso]) bds;
wenzelm@58634
   519
          val set_sbdss = @{map 3} mk_set_sbd ks bd_Card_orders set_bdss;
traytel@55770
   520
traytel@55770
   521
          fun mk_in_bd_sum i Co Cnz bd =
traytel@55770
   522
            Cnz RS ((@{thm ordLeq_ordIso_trans} OF
traytel@55770
   523
              [Co RS mk_ordLeq_csum n i (Co RS @{thm ordLeq_refl}), sbd_ordIso]) RS
traytel@55770
   524
              (bd RS @{thm ordLeq_transitive[OF _ cexp_mono2_Cnotzero[OF _ Card_order_csum]]}));
wenzelm@58634
   525
          val in_sbds = @{map 4} mk_in_bd_sum ks bd_Card_orders bd_Cnotzeros in_bds;
traytel@55770
   526
       in
traytel@56350
   527
         (lthy, sbd, sbd_Cinfinite, sbd_Card_order, set_sbdss, in_sbds)
traytel@55770
   528
       end;
traytel@55770
   529
traytel@55770
   530
    val sbd_Cnotzero = sbd_Cinfinite RS @{thm Cinfinite_Cnotzero};
traytel@55770
   531
    val suc_bd = mk_cardSuc sbd;
traytel@55770
   532
blanchet@48975
   533
    val field_suc_bd = mk_Field suc_bd;
blanchet@48975
   534
    val suc_bdT = fst (dest_relT (fastype_of suc_bd));
blanchet@48975
   535
    fun mk_Asuc_bd [] = mk_cexp ctwo suc_bd
blanchet@48975
   536
      | mk_Asuc_bd As =
blanchet@48975
   537
        mk_cexp (mk_csum (Library.foldr1 (uncurry mk_csum) (map mk_card_of As)) ctwo) suc_bd;
blanchet@48975
   538
traytel@55770
   539
    val suc_bd_Card_order =  sbd_Card_order RS @{thm cardSuc_Card_order};
traytel@55770
   540
    val suc_bd_Cinfinite = sbd_Cinfinite RS @{thm Cinfinite_cardSuc};
blanchet@48975
   541
    val suc_bd_Cnotzero = suc_bd_Cinfinite RS @{thm Cinfinite_Cnotzero};
blanchet@48975
   542
    val suc_bd_worel = suc_bd_Card_order RS @{thm Card_order_wo_rel}
blanchet@48975
   543
    val basis_Asuc = if m = 0 then @{thm ordLeq_refl[OF Card_order_ctwo]}
blanchet@48975
   544
        else @{thm ordLeq_csum2[OF Card_order_ctwo]};
blanchet@48975
   545
    val Asuc_bd_Cinfinite = suc_bd_Cinfinite RS (basis_Asuc RS @{thm Cinfinite_cexp});
blanchet@48975
   546
traytel@51782
   547
    val suc_bd_Asuc_bd = @{thm ordLess_ordLeq_trans[OF ordLess_ctwo_cexp cexp_mono1]} OF
blanchet@48975
   548
      [suc_bd_Card_order, basis_Asuc, suc_bd_Card_order];
blanchet@48975
   549
traytel@55541
   550
traytel@55541
   551
    val Asuc_bd = mk_Asuc_bd passive_UNIVs;
traytel@55541
   552
    val Asuc_bdT = fst (dest_relT (fastype_of Asuc_bd));
blanchet@48975
   553
    val II_BTs = replicate n (HOLogic.mk_setT Asuc_bdT);
blanchet@48975
   554
    val II_sTs = map2 (fn Ds => fn bnf =>
blanchet@48975
   555
      mk_T_of_bnf Ds (passiveAs @ replicate n Asuc_bdT) bnf --> Asuc_bdT) Dss bnfs;
blanchet@48975
   556
traytel@61334
   557
    val ((((((Bs, ss), idxs), Asi_name), (idx, idx')), (jdx, jdx')), _) =
traytel@61272
   558
      lthy
traytel@61272
   559
      |> mk_Frees "B" BTs
traytel@61272
   560
      ||>> mk_Frees "s" sTs
traytel@61272
   561
      ||>> mk_Frees "i" (replicate n suc_bdT)
blanchet@48975
   562
      ||>> (fn ctxt => apfst the_single (mk_fresh_names ctxt 1 "Asi"))
blanchet@48975
   563
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") suc_bdT
traytel@61272
   564
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "j") suc_bdT;
blanchet@48975
   565
blanchet@48975
   566
    val suc_bd_limit_thm =
blanchet@48975
   567
      let
blanchet@48975
   568
        val prem = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
blanchet@48975
   569
          (map (fn idx => HOLogic.mk_mem (idx, field_suc_bd)) idxs));
blanchet@48975
   570
        fun mk_conjunct idx = HOLogic.mk_conj (mk_not_eq idx jdx,
blanchet@48975
   571
          HOLogic.mk_mem (HOLogic.mk_prod (idx, jdx), suc_bd));
blanchet@48975
   572
        val concl = HOLogic.mk_Trueprop (mk_Bex field_suc_bd
blanchet@48975
   573
          (Term.absfree jdx' (Library.foldr1 HOLogic.mk_conj (map mk_conjunct idxs))));
traytel@61334
   574
        val vars = fold (Variable.add_free_names lthy) [prem, concl] [];
blanchet@48975
   575
      in
traytel@61334
   576
        Goal.prove_sorry lthy vars [] (Logic.list_implies ([prem], concl))
traytel@60728
   577
          (fn {context = ctxt, prems = _} => mk_bd_limit_tac ctxt n suc_bd_Cinfinite)
traytel@49109
   578
        |> Thm.close_derivation
blanchet@48975
   579
      end;
blanchet@48975
   580
blanchet@48975
   581
    val timer = time (timer "Bounds");
blanchet@48975
   582
blanchet@48975
   583
    (* minimal algebra *)
blanchet@48975
   584
blanchet@48975
   585
    fun mk_minG Asi i k = mk_UNION (mk_underS suc_bd $ i)
blanchet@48975
   586
      (Term.absfree jdx' (mk_nthN n (Asi $ jdx) k));
blanchet@48975
   587
traytel@55541
   588
    fun mk_minH_component Asi i sets Ts s k =
blanchet@48975
   589
      HOLogic.mk_binop @{const_name "sup"}
traytel@55541
   590
      (mk_minG Asi i k, mk_image s $ mk_in (passive_UNIVs @ map (mk_minG Asi i) ks) sets Ts);
blanchet@48975
   591
traytel@55541
   592
    fun mk_min_algs ss =
blanchet@48975
   593
      let
blanchet@48975
   594
        val BTs = map (range_type o fastype_of) ss;
traytel@55541
   595
        val Ts = passiveAs @ BTs;
blanchet@48975
   596
        val (Asi, Asi') = `Free (Asi_name, suc_bdT -->
blanchet@48975
   597
          Library.foldr1 HOLogic.mk_prodT (map HOLogic.mk_setT BTs));
blanchet@48975
   598
      in
blanchet@48975
   599
         mk_worec suc_bd (Term.absfree Asi' (Term.absfree idx' (HOLogic.mk_tuple
wenzelm@58634
   600
           (@{map 4} (mk_minH_component Asi idx) (mk_setss Ts) (mk_FTs Ts) ss ks))))
blanchet@48975
   601
      end;
blanchet@48975
   602
blanchet@48975
   603
    val (min_algs_thms, min_algs_mono_thms, card_of_min_algs_thm, least_min_algs_thm) =
blanchet@48975
   604
      let
blanchet@48975
   605
        val i_field = HOLogic.mk_mem (idx, field_suc_bd);
traytel@55541
   606
        val min_algs = mk_min_algs ss;
traytel@55803
   607
blanchet@48975
   608
        val min_algss = map (fn k => mk_nthN n (min_algs $ idx) k) ks;
blanchet@48975
   609
blanchet@48975
   610
        val concl = HOLogic.mk_Trueprop
blanchet@48975
   611
          (HOLogic.mk_eq (min_algs $ idx, HOLogic.mk_tuple
wenzelm@58634
   612
            (@{map 4} (mk_minH_component min_algs idx) setssAs FTsAs ss ks)));
traytel@56272
   613
        val goal = Logic.mk_implies (HOLogic.mk_Trueprop i_field, concl);
traytel@61334
   614
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
   615
traytel@61334
   616
        val min_algs_thm = Goal.prove_sorry lthy vars [] goal
traytel@60728
   617
          (fn {context = ctxt, prems = _} => mk_min_algs_tac ctxt suc_bd_worel in_cong'_thms)
traytel@61334
   618
          |> Thm.close_derivation;
blanchet@48975
   619
blanchet@48975
   620
        val min_algs_thms = map (fn k => min_algs_thm RS mk_nthI n k) ks;
blanchet@48975
   621
blanchet@48975
   622
        fun mk_mono_goal min_alg =
traytel@56272
   623
          HOLogic.mk_Trueprop (mk_relChain suc_bd (Term.absfree idx' min_alg));
blanchet@48975
   624
traytel@49109
   625
        val monos =
traytel@49109
   626
          map2 (fn goal => fn min_algs =>
traytel@61334
   627
            Variable.add_free_names lthy goal []
traytel@61334
   628
            |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@61334
   629
              (fn {context = ctxt, prems = _} => mk_min_algs_mono_tac ctxt min_algs))
traytel@61334
   630
            |> Thm.close_derivation)
traytel@49109
   631
          (map mk_mono_goal min_algss) min_algs_thms;
blanchet@48975
   632
blanchet@48975
   633
        fun mk_card_conjunct min_alg = mk_ordLeq (mk_card_of min_alg) Asuc_bd;
blanchet@48975
   634
        val card_conjunction = Library.foldr1 HOLogic.mk_conj (map mk_card_conjunct min_algss);
wenzelm@59621
   635
        val card_cT = Thm.ctyp_of lthy suc_bdT;
wenzelm@59621
   636
        val card_ct = Thm.cterm_of lthy (Term.absfree idx' card_conjunction);
blanchet@48975
   637
traytel@56272
   638
        val card_of =
traytel@61334
   639
          let
traytel@61334
   640
            val goal = HOLogic.mk_Trueprop (HOLogic.mk_imp (i_field, card_conjunction));
traytel@61334
   641
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   642
          in
traytel@61334
   643
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   644
              (fn {context = ctxt, prems = _} => mk_min_algs_card_of_tac ctxt card_cT card_ct
traytel@61334
   645
                m suc_bd_worel min_algs_thms in_sbds
traytel@61334
   646
                sbd_Card_order sbd_Cnotzero suc_bd_Card_order suc_bd_Cinfinite suc_bd_Cnotzero
traytel@61334
   647
                suc_bd_Asuc_bd Asuc_bd_Cinfinite)
traytel@61334
   648
            |> Thm.close_derivation
traytel@61334
   649
          end;
blanchet@48975
   650
traytel@55541
   651
        val least_prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
traytel@51893
   652
        val least_conjunction = Library.foldr1 HOLogic.mk_conj (map2 mk_leq min_algss Bs);
wenzelm@59621
   653
        val least_cT = Thm.ctyp_of lthy suc_bdT;
wenzelm@59621
   654
        val least_ct = Thm.cterm_of lthy (Term.absfree idx' least_conjunction);
blanchet@48975
   655
traytel@56272
   656
        val least =
traytel@61334
   657
          let
traytel@61334
   658
            val goal = Logic.mk_implies (least_prem,
traytel@61334
   659
              HOLogic.mk_Trueprop (HOLogic.mk_imp (i_field, least_conjunction)));
traytel@61334
   660
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   661
          in
traytel@61334
   662
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   663
              (fn {context = ctxt, prems = _} => mk_min_algs_least_tac ctxt least_cT least_ct
traytel@61334
   664
                suc_bd_worel min_algs_thms alg_set_thms)
traytel@61334
   665
            |> Thm.close_derivation
traytel@61334
   666
          end;
blanchet@48975
   667
      in
blanchet@48975
   668
        (min_algs_thms, monos, card_of, least)
blanchet@48975
   669
      end;
blanchet@48975
   670
blanchet@48975
   671
    val timer = time (timer "min_algs definition & thms");
blanchet@48975
   672
traytel@53566
   673
    val min_alg_binds = mk_internal_bs min_algN;
traytel@53566
   674
    fun min_alg_bind i = nth min_alg_binds (i - 1);
blanchet@48975
   675
    val min_alg_def_bind = rpair [] o Thm.def_binding o min_alg_bind;
blanchet@48975
   676
blanchet@48975
   677
    fun min_alg_spec i =
blanchet@48975
   678
      let
blanchet@48975
   679
        val rhs = mk_UNION (field_suc_bd)
traytel@55541
   680
          (Term.absfree idx' (mk_nthN n (mk_min_algs ss $ idx) i));
blanchet@48975
   681
      in
traytel@55541
   682
        fold_rev (Term.absfree o Term.dest_Free) ss rhs
blanchet@48975
   683
      end;
blanchet@48975
   684
blanchet@48975
   685
    val ((min_alg_frees, (_, min_alg_def_frees)), (lthy, lthy_old)) =
blanchet@59794
   686
      lthy
traytel@61101
   687
      |> Local_Theory.open_target |> snd
blanchet@59794
   688
      |> fold_map (fn i => Local_Theory.define
blanchet@59794
   689
        ((min_alg_bind i, NoSyn), (min_alg_def_bind i, min_alg_spec i))) ks
blanchet@59794
   690
      |>> apsnd split_list o split_list
traytel@61101
   691
      ||> `Local_Theory.close_target;
blanchet@48975
   692
blanchet@48975
   693
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   694
    val min_algs = map (fst o Term.dest_Const o Morphism.term phi) min_alg_frees;
traytel@55204
   695
    val min_alg_defs = map (fn def =>
traytel@55541
   696
      mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) min_alg_def_frees;
blanchet@48975
   697
traytel@55541
   698
    fun mk_min_alg ss i =
blanchet@48975
   699
      let
blanchet@48975
   700
        val T = HOLogic.mk_setT (range_type (fastype_of (nth ss (i - 1))))
traytel@55541
   701
        val Ts = map fastype_of ss;
blanchet@48975
   702
        val min_algT = Library.foldr (op -->) (Ts, T);
blanchet@48975
   703
      in
traytel@55541
   704
        Term.list_comb (Const (nth min_algs (i - 1), min_algT), ss)
blanchet@48975
   705
      end;
blanchet@48975
   706
traytel@56237
   707
    val min_algs = map (mk_min_alg ss) ks;
traytel@56237
   708
traytel@61334
   709
    val ((Bs, ss), _) =
traytel@61272
   710
      lthy
traytel@61272
   711
      |> mk_Frees "B" BTs
traytel@61272
   712
      ||>> mk_Frees "s" sTs;
traytel@61272
   713
blanchet@48975
   714
    val (alg_min_alg_thm, card_of_min_alg_thms, least_min_alg_thms, mor_incl_min_alg_thm) =
blanchet@48975
   715
      let
traytel@61334
   716
        val alg_min_alg =
traytel@61334
   717
          let
traytel@61334
   718
            val goal = HOLogic.mk_Trueprop (mk_alg min_algs ss);
traytel@61334
   719
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   720
          in
traytel@61334
   721
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   722
              (fn {context = ctxt, prems = _} => mk_alg_min_alg_tac ctxt m alg_def min_alg_defs
traytel@61334
   723
                suc_bd_limit_thm sbd_Cinfinite set_sbdss min_algs_thms min_algs_mono_thms)
traytel@61334
   724
            |> Thm.close_derivation
traytel@61334
   725
          end;
blanchet@48975
   726
traytel@61334
   727
        fun mk_card_of_thm min_alg def =
traytel@61334
   728
          let
traytel@61334
   729
            val goal = HOLogic.mk_Trueprop (mk_ordLeq (mk_card_of min_alg) Asuc_bd);
traytel@61334
   730
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   731
          in
traytel@61334
   732
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   733
              (fn {context = ctxt, prems = _} => mk_card_of_min_alg_tac ctxt def card_of_min_algs_thm
traytel@61334
   734
                suc_bd_Card_order suc_bd_Asuc_bd Asuc_bd_Cinfinite)
traytel@61334
   735
            |> Thm.close_derivation
traytel@61334
   736
          end;
blanchet@48975
   737
traytel@61334
   738
        fun mk_least_thm min_alg B def =
traytel@61334
   739
          let
traytel@61334
   740
            val prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
traytel@61334
   741
            val goal = Logic.mk_implies (prem, HOLogic.mk_Trueprop (mk_leq min_alg B));
traytel@61334
   742
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   743
          in
traytel@61334
   744
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   745
              (fn {context = ctxt, prems = _} => mk_least_min_alg_tac ctxt def least_min_algs_thm)
traytel@61334
   746
            |> Thm.close_derivation
traytel@61334
   747
          end;
blanchet@48975
   748
wenzelm@58634
   749
        val leasts = @{map 3} mk_least_thm min_algs Bs min_alg_defs;
blanchet@48975
   750
traytel@61334
   751
        val incl =
traytel@61334
   752
          let
traytel@61334
   753
            val prem = HOLogic.mk_Trueprop (mk_alg Bs ss);
traytel@61334
   754
            val goal = Logic.mk_implies (prem,
traytel@61334
   755
              HOLogic.mk_Trueprop (mk_mor min_algs ss Bs ss active_ids));
traytel@61334
   756
            val vars = Variable.add_free_names lthy goal [];
traytel@61334
   757
          in
traytel@61334
   758
            Goal.prove_sorry lthy vars [] goal
traytel@61334
   759
              (fn {context = ctxt, prems = _} =>
traytel@61334
   760
                EVERY' (rtac ctxt mor_incl_thm :: map (etac ctxt) leasts) 1)
traytel@61334
   761
            |> Thm.close_derivation
traytel@61334
   762
          end;
blanchet@48975
   763
      in
traytel@49109
   764
        (alg_min_alg, map2 mk_card_of_thm min_algs min_alg_defs, leasts, incl)
blanchet@48975
   765
      end;
blanchet@48975
   766
blanchet@48975
   767
    val timer = time (timer "Minimal algebra definition & thms");
blanchet@48975
   768
blanchet@48975
   769
    val II_repT = HOLogic.mk_prodT (HOLogic.mk_tupleT II_BTs, HOLogic.mk_tupleT II_sTs);
traytel@53566
   770
    val IIT_bind = mk_internal_b IITN;
blanchet@48975
   771
blanchet@48975
   772
    val ((IIT_name, (IIT_glob_info, IIT_loc_info)), lthy) =
wenzelm@49835
   773
      typedef (IIT_bind, params, NoSyn)
traytel@60728
   774
        (HOLogic.mk_UNIV II_repT) NONE (fn ctxt => EVERY' [rtac ctxt exI, rtac ctxt UNIV_I] 1) lthy;
blanchet@48975
   775
blanchet@48975
   776
    val IIT = Type (IIT_name, params');
blanchet@48975
   777
    val Abs_IIT = Const (#Abs_name IIT_glob_info, II_repT --> IIT);
blanchet@48975
   778
    val Rep_IIT = Const (#Rep_name IIT_glob_info, IIT --> II_repT);
traytel@49228
   779
    val Abs_IIT_inverse_thm = UNIV_I RS #Abs_inverse IIT_loc_info;
blanchet@48975
   780
blanchet@48975
   781
    val initT = IIT --> Asuc_bdT;
blanchet@48975
   782
    val active_initTs = replicate n initT;
blanchet@48975
   783
    val init_FTs = map2 (fn Ds => mk_T_of_bnf Ds (passiveAs @ active_initTs)) Dss bnfs;
blanchet@48975
   784
    val init_fTs = map (fn T => initT --> T) activeAs;
blanchet@48975
   785
traytel@61272
   786
    val ((((II_Bs, II_ss), (iidx, iidx')), init_xFs), _) =
traytel@61272
   787
      lthy
traytel@61272
   788
      |> mk_Frees "IIB" II_BTs
traytel@61272
   789
      ||>> mk_Frees "IIs" II_sTs
traytel@61272
   790
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") IIT
traytel@61272
   791
      ||>> mk_Frees "x" init_FTs;
blanchet@48975
   792
blanchet@48975
   793
    val II = HOLogic.mk_Collect (fst iidx', IIT, list_exists_free (II_Bs @ II_ss)
blanchet@48975
   794
      (HOLogic.mk_conj (HOLogic.mk_eq (iidx,
blanchet@48975
   795
        Abs_IIT $ (HOLogic.mk_prod (HOLogic.mk_tuple II_Bs, HOLogic.mk_tuple II_ss))),
traytel@55541
   796
        mk_alg II_Bs II_ss)));
blanchet@48975
   797
blanchet@48975
   798
    val select_Bs = map (mk_nthN n (HOLogic.mk_fst (Rep_IIT $ iidx))) ks;
blanchet@48975
   799
    val select_ss = map (mk_nthN n (HOLogic.mk_snd (Rep_IIT $ iidx))) ks;
blanchet@48975
   800
traytel@53566
   801
    val str_init_binds = mk_internal_bs str_initN;
traytel@53566
   802
    fun str_init_bind i = nth str_init_binds (i - 1);
blanchet@48975
   803
    val str_init_def_bind = rpair [] o Thm.def_binding o str_init_bind;
blanchet@48975
   804
blanchet@48975
   805
    fun str_init_spec i =
blanchet@48975
   806
      let
blanchet@48975
   807
        val init_xF = nth init_xFs (i - 1)
blanchet@48975
   808
        val select_s = nth select_ss (i - 1);
blanchet@48975
   809
        val map = mk_map_of_bnf (nth Dss (i - 1))
blanchet@48975
   810
          (passiveAs @ active_initTs) (passiveAs @ replicate n Asuc_bdT)
blanchet@48975
   811
          (nth bnfs (i - 1));
blanchet@48975
   812
        val map_args = passive_ids @ replicate n (mk_rapp iidx Asuc_bdT);
blanchet@48975
   813
        val rhs = select_s $ (Term.list_comb (map, map_args) $ init_xF);
blanchet@48975
   814
      in
traytel@55204
   815
        fold_rev (Term.absfree o Term.dest_Free) [init_xF, iidx] rhs
blanchet@48975
   816
      end;
blanchet@48975
   817
blanchet@48975
   818
    val ((str_init_frees, (_, str_init_def_frees)), (lthy, lthy_old)) =
blanchet@48975
   819
      lthy
traytel@61101
   820
      |> Local_Theory.open_target |> snd
traytel@55204
   821
      |> fold_map (fn i => Local_Theory.define
traytel@55204
   822
        ((str_init_bind i, NoSyn), (str_init_def_bind i, str_init_spec i))) ks
blanchet@48975
   823
      |>> apsnd split_list o split_list
traytel@61101
   824
      ||> `Local_Theory.close_target;
blanchet@48975
   825
blanchet@48975
   826
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@48975
   827
    val str_inits =
blanchet@48975
   828
      map (Term.subst_atomic_types (map (`(Morphism.typ phi)) params') o Morphism.term phi)
blanchet@48975
   829
        str_init_frees;
blanchet@48975
   830
traytel@55204
   831
    val str_init_defs = map (fn def =>
traytel@55204
   832
      mk_unabs_def 2 (Morphism.thm phi def RS meta_eq_to_obj_eq)) str_init_def_frees;
blanchet@48975
   833
traytel@55541
   834
    val car_inits = map (mk_min_alg str_inits) ks;
blanchet@48975
   835
traytel@61272
   836
    val (((((((((Bs, ss), Asuc_fs), (iidx, iidx')), init_xs), (init_xFs, init_xFs')), init_fs),
traytel@61334
   837
        init_fs_copy), init_phis), _) =
traytel@61272
   838
      lthy
traytel@61272
   839
      |> mk_Frees "B" BTs
traytel@61272
   840
      ||>> mk_Frees "s" sTs
traytel@61272
   841
      ||>> mk_Frees "f" (map (fn T => Asuc_bdT --> T) activeAs)
traytel@61272
   842
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "i") IIT
traytel@61272
   843
      ||>> mk_Frees "ix" active_initTs
traytel@61272
   844
      ||>> mk_Frees' "x" init_FTs
traytel@61272
   845
      ||>> mk_Frees "f" init_fTs
traytel@61272
   846
      ||>> mk_Frees "f" init_fTs
traytel@61272
   847
      ||>> mk_Frees "P" (replicate n (mk_pred1T initT));
traytel@61272
   848
wenzelm@60784
   849
    val alg_init_thm =
wenzelm@60784
   850
      infer_instantiate' lthy (map (SOME o Thm.cterm_of lthy) str_inits) alg_min_alg_thm;
blanchet@48975
   851
wenzelm@51551
   852
    val alg_select_thm = Goal.prove_sorry lthy [] []
blanchet@48975
   853
      (HOLogic.mk_Trueprop (mk_Ball II
traytel@55541
   854
        (Term.absfree iidx' (mk_alg select_Bs select_ss))))
traytel@55197
   855
      (fn {context = ctxt, prems = _} => mk_alg_select_tac ctxt Abs_IIT_inverse_thm)
traytel@49109
   856
      |> Thm.close_derivation;
blanchet@48975
   857
blanchet@48975
   858
    val mor_select_thm =
blanchet@48975
   859
      let
desharna@57567
   860
        val i_prem = mk_Trueprop_mem (iidx, II);
traytel@56237
   861
        val mor_prem = HOLogic.mk_Trueprop (mk_mor select_Bs select_ss active_UNIVs ss Asuc_fs);
traytel@56237
   862
        val prems = [i_prem, mor_prem];
blanchet@48975
   863
        val concl = HOLogic.mk_Trueprop
traytel@56237
   864
          (mk_mor car_inits str_inits active_UNIVs ss
blanchet@48975
   865
            (map (fn f => HOLogic.mk_comp (f, mk_rapp iidx Asuc_bdT)) Asuc_fs));
traytel@61334
   866
        val vars = fold (Variable.add_free_names lthy) (concl :: prems) [];
blanchet@48975
   867
      in
traytel@61334
   868
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, concl))
traytel@60728
   869
          (fn {context = ctxt, prems = _} => mk_mor_select_tac ctxt mor_def mor_cong_thm
traytel@60728
   870
            mor_comp_thm mor_incl_min_alg_thm alg_def alg_select_thm alg_set_thms set_mapss
traytel@60728
   871
            str_init_defs)
traytel@49109
   872
        |> Thm.close_derivation
blanchet@48975
   873
      end;
blanchet@48975
   874
traytel@56237
   875
    val init_unique_mor_thms =
blanchet@48975
   876
      let
blanchet@48975
   877
        val prems = map2 (HOLogic.mk_Trueprop oo curry HOLogic.mk_mem) init_xs car_inits
blanchet@48975
   878
        val mor_prems = map HOLogic.mk_Trueprop
blanchet@48975
   879
          [mk_mor car_inits str_inits Bs ss init_fs,
blanchet@48975
   880
          mk_mor car_inits str_inits Bs ss init_fs_copy];
blanchet@48975
   881
        fun mk_fun_eq f g x = HOLogic.mk_eq (f $ x, g $ x);
blanchet@48975
   882
        val unique = HOLogic.mk_Trueprop
wenzelm@58634
   883
          (Library.foldr1 HOLogic.mk_conj (@{map 3} mk_fun_eq init_fs init_fs_copy init_xs));
wenzelm@59621
   884
        val cts = map (Thm.cterm_of lthy) ss;
traytel@61334
   885
        val all_prems = prems @ mor_prems;
traytel@61334
   886
        val vars = fold (Variable.add_free_names lthy) (unique :: all_prems) [];
traytel@56272
   887
        val unique_mor =
traytel@61334
   888
          Goal.prove_sorry lthy vars [] (Logic.list_implies (all_prems, unique))
traytel@60728
   889
            (fn {context = ctxt, prems = _} => mk_init_unique_mor_tac ctxt cts m alg_def
traytel@60728
   890
              alg_init_thm least_min_alg_thms in_mono'_thms alg_set_thms morE_thms map_cong0s)
traytel@61334
   891
          |> Thm.close_derivation;
blanchet@48975
   892
      in
traytel@56237
   893
        split_conj_thm unique_mor
blanchet@48975
   894
      end;
blanchet@48975
   895
blanchet@48975
   896
    val init_setss = mk_setss (passiveAs @ active_initTs);
blanchet@48975
   897
    val active_init_setss = map (drop m) init_setss;
blanchet@48975
   898
    val init_ins = map2 (fn sets => mk_in (passive_UNIVs @ car_inits) sets) init_setss init_FTs;
blanchet@48975
   899
blanchet@48975
   900
    fun mk_closed phis =
blanchet@48975
   901
      let
blanchet@48975
   902
        fun mk_conjunct phi str_init init_sets init_in x x' =
blanchet@48975
   903
          let
blanchet@48975
   904
            val prem = Library.foldr1 HOLogic.mk_conj
blanchet@48975
   905
              (map2 (fn set => mk_Ball (set $ x)) init_sets phis);
blanchet@48975
   906
            val concl = phi $ (str_init $ x);
blanchet@48975
   907
          in
blanchet@48975
   908
            mk_Ball init_in (Term.absfree x' (HOLogic.mk_imp (prem, concl)))
blanchet@48975
   909
          end;
blanchet@48975
   910
      in
blanchet@48975
   911
        Library.foldr1 HOLogic.mk_conj
wenzelm@58634
   912
          (@{map 6} mk_conjunct phis str_inits active_init_setss init_ins init_xFs init_xFs')
blanchet@48975
   913
      end;
blanchet@48975
   914
blanchet@48975
   915
    val init_induct_thm =
blanchet@48975
   916
      let
blanchet@48975
   917
        val prem = HOLogic.mk_Trueprop (mk_closed init_phis);
blanchet@48975
   918
        val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
blanchet@48975
   919
          (map2 mk_Ball car_inits init_phis));
traytel@61334
   920
        val vars = fold (Variable.add_free_names lthy) [concl, prem] [];
blanchet@48975
   921
      in
traytel@61334
   922
        Goal.prove_sorry lthy vars [] (Logic.mk_implies (prem, concl))
traytel@60728
   923
          (fn {context = ctxt, prems = _} => mk_init_induct_tac ctxt m alg_def alg_init_thm
traytel@60728
   924
            least_min_alg_thms alg_set_thms)
traytel@49109
   925
        |> Thm.close_derivation
blanchet@48975
   926
      end;
blanchet@48975
   927
blanchet@48975
   928
    val timer = time (timer "Initiality definition & thms");
blanchet@48975
   929
blanchet@48975
   930
    val ((T_names, (T_glob_infos, T_loc_infos)), lthy) =
blanchet@48975
   931
      lthy
wenzelm@58634
   932
      |> @{fold_map 3} (fn b => fn mx => fn car_init =>
blanchet@57093
   933
        typedef (b, params, mx) car_init NONE
wenzelm@59498
   934
          (fn ctxt =>
traytel@60728
   935
            EVERY' [rtac ctxt iffD2, rtac ctxt @{thm ex_in_conv}, resolve_tac ctxt alg_not_empty_thms,
traytel@60728
   936
            rtac ctxt alg_init_thm] 1)) bs mixfixes car_inits
blanchet@48975
   937
      |>> apsnd split_list o split_list;
blanchet@48975
   938
blanchet@48975
   939
    val Ts = map (fn name => Type (name, params')) T_names;
blanchet@48975
   940
    fun mk_Ts passive = map (Term.typ_subst_atomic (passiveAs ~~ passive)) Ts;
blanchet@48975
   941
    val Ts' = mk_Ts passiveBs;
blanchet@48975
   942
    val Rep_Ts = map2 (fn info => fn T => Const (#Rep_name info, T --> initT)) T_glob_infos Ts;
blanchet@48975
   943
    val Abs_Ts = map2 (fn info => fn T => Const (#Abs_name info, initT --> T)) T_glob_infos Ts;
blanchet@48975
   944
blanchet@48975
   945
    val type_defs = map #type_definition T_loc_infos;
blanchet@48975
   946
    val Reps = map #Rep T_loc_infos;
blanchet@48975
   947
    val Rep_inverses = map #Rep_inverse T_loc_infos;
blanchet@48975
   948
    val Abs_inverses = map #Abs_inverse T_loc_infos;
blanchet@48975
   949
blanchet@48975
   950
    val timer = time (timer "THE TYPEDEFs & Rep/Abs thms");
blanchet@48975
   951
blanchet@48975
   952
    val UNIVs = map HOLogic.mk_UNIV Ts;
blanchet@48975
   953
    val FTs = mk_FTs (passiveAs @ Ts);
blanchet@48975
   954
    val FTs' = mk_FTs (passiveBs @ Ts');
blanchet@48975
   955
    fun mk_set_Ts T = passiveAs @ replicate n (HOLogic.mk_setT T);
blanchet@48975
   956
    val setFTss = map (mk_FTs o mk_set_Ts) passiveAs;
blanchet@48975
   957
    val FTs_setss = mk_setss (passiveAs @ Ts);
blanchet@48975
   958
    val FTs'_setss = mk_setss (passiveBs @ Ts');
blanchet@48975
   959
    val map_FT_inits = map2 (fn Ds =>
blanchet@48975
   960
      mk_map_of_bnf Ds (passiveAs @ Ts) (passiveAs @ active_initTs)) Dss bnfs;
blanchet@48975
   961
    val fTs = map2 (curry op -->) Ts activeAs;
blanchet@49504
   962
    val foldT = Library.foldr1 HOLogic.mk_prodT (map2 (curry op -->) Ts activeAs);
blanchet@48975
   963
    val rec_sTs = map (Term.typ_subst_atomic (activeBs ~~ Ts)) prod_sTs;
blanchet@48975
   964
    val rec_maps = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_fsts;
blanchet@48975
   965
    val rec_maps_rev = map (Term.subst_atomic_types (activeBs ~~ Ts)) map_fsts_rev;
blanchet@48975
   966
    val rec_fsts = map (Term.subst_atomic_types (activeBs ~~ Ts)) fsts;
traytel@51739
   967
    val rec_UNIVs = map2 (HOLogic.mk_UNIV oo curry HOLogic.mk_prodT) Ts activeAs;
blanchet@48975
   968
traytel@61272
   969
    val ((ss, (fold_f, fold_f')), _) =
traytel@61272
   970
      lthy
traytel@61272
   971
      |> mk_Frees "s" sTs
traytel@61272
   972
      ||>> yield_singleton (apfst (op ~~) oo mk_Frees' "f") foldT;
blanchet@49330
   973
blanchet@54492
   974
    fun ctor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctorN ^ "_");
wenzelm@59859
   975
    val ctor_def_bind = rpair [] o Binding.concealed o Thm.def_binding o ctor_bind;
blanchet@48975
   976
traytel@55756
   977
    fun ctor_spec abs str map_FT_init =
traytel@55756
   978
      Library.foldl1 HOLogic.mk_comp [abs, str,
traytel@55756
   979
        Term.list_comb (map_FT_init, map HOLogic.id_const passiveAs @ Rep_Ts)];
blanchet@48975
   980
blanchet@49501
   981
    val ((ctor_frees, (_, ctor_def_frees)), (lthy, lthy_old)) =
blanchet@49311
   982
      lthy
traytel@61101
   983
      |> Local_Theory.open_target |> snd
wenzelm@58634
   984
      |> @{fold_map 4} (fn i => fn abs => fn str => fn mapx =>
traytel@55204
   985
        Local_Theory.define
traytel@55756
   986
          ((ctor_bind i, NoSyn), (ctor_def_bind i, ctor_spec abs str mapx)))
traytel@55756
   987
          ks Abs_Ts str_inits map_FT_inits
blanchet@49311
   988
      |>> apsnd split_list o split_list
traytel@61101
   989
      ||> `Local_Theory.close_target;
blanchet@48975
   990
blanchet@48975
   991
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49501
   992
    fun mk_ctors passive =
traytel@49185
   993
      map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ (mk_params passive)) o
blanchet@49501
   994
        Morphism.term phi) ctor_frees;
blanchet@49501
   995
    val ctors = mk_ctors passiveAs;
blanchet@49501
   996
    val ctor's = mk_ctors passiveBs;
traytel@55204
   997
    val ctor_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) ctor_def_frees;
blanchet@48975
   998
blanchet@48975
   999
    val (mor_Rep_thm, mor_Abs_thm) =
blanchet@48975
  1000
      let
traytel@56237
  1001
        val defs = mor_def :: ctor_defs;
traytel@56237
  1002
blanchet@48975
  1003
        val mor_Rep =
wenzelm@51551
  1004
          Goal.prove_sorry lthy [] []
blanchet@49501
  1005
            (HOLogic.mk_Trueprop (mk_mor UNIVs ctors car_inits str_inits Rep_Ts))
traytel@56237
  1006
            (fn {context = ctxt, prems = _} => mk_mor_Rep_tac ctxt m defs Reps Abs_inverses
traytel@56237
  1007
              alg_min_alg_thm alg_set_thms set_mapss)
traytel@49109
  1008
          |> Thm.close_derivation;
blanchet@48975
  1009
traytel@56237
  1010
        fun mk_ct initFT str abs = Term.absdummy initFT (abs $ (str $ Bound 0))
wenzelm@59621
  1011
        val cts = @{map 3} (Thm.cterm_of lthy ooo mk_ct) init_FTs str_inits Abs_Ts;
traytel@56237
  1012
blanchet@48975
  1013
        val mor_Abs =
wenzelm@51551
  1014
          Goal.prove_sorry lthy [] []
blanchet@49501
  1015
            (HOLogic.mk_Trueprop (mk_mor car_inits str_inits UNIVs ctors Abs_Ts))
traytel@56237
  1016
            (fn {context = ctxt, prems = _} => mk_mor_Abs_tac ctxt cts defs Abs_inverses
traytel@56237
  1017
              map_comp_id_thms map_cong0L_thms)
traytel@49109
  1018
          |> Thm.close_derivation;
blanchet@48975
  1019
      in
blanchet@48975
  1020
        (mor_Rep, mor_Abs)
blanchet@48975
  1021
      end;
blanchet@48975
  1022
blanchet@49501
  1023
    val timer = time (timer "ctor definitions & thms");
blanchet@48975
  1024
blanchet@49504
  1025
    val fold_fun = Term.absfree fold_f'
blanchet@49504
  1026
      (mk_mor UNIVs ctors active_UNIVs ss (map (mk_nthN n fold_f) ks));
blanchet@49504
  1027
    val foldx = HOLogic.choice_const foldT $ fold_fun;
blanchet@48975
  1028
blanchet@54492
  1029
    fun fold_bind i = nth external_bs (i - 1) |> Binding.prefix_name (ctor_foldN ^ "_");
wenzelm@59859
  1030
    val fold_def_bind = rpair [] o Binding.concealed o Thm.def_binding o fold_bind;
blanchet@48975
  1031
traytel@55204
  1032
    fun fold_spec i = fold_rev (Term.absfree o Term.dest_Free) ss (mk_nthN n foldx i);
blanchet@48975
  1033
blanchet@49504
  1034
    val ((fold_frees, (_, fold_def_frees)), (lthy, lthy_old)) =
blanchet@49311
  1035
      lthy
traytel@61101
  1036
      |> Local_Theory.open_target |> snd
traytel@55204
  1037
      |> fold_map (fn i =>
traytel@55204
  1038
        Local_Theory.define ((fold_bind i, NoSyn), (fold_def_bind i, fold_spec i))) ks
blanchet@49311
  1039
      |>> apsnd split_list o split_list
traytel@61101
  1040
      ||> `Local_Theory.close_target;
blanchet@48975
  1041
blanchet@48975
  1042
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49504
  1043
    val folds = map (Morphism.term phi) fold_frees;
blanchet@49504
  1044
    val fold_names = map (fst o dest_Const) folds;
traytel@52731
  1045
    fun mk_folds passives actives =
wenzelm@58634
  1046
      @{map 3} (fn name => fn T => fn active =>
traytel@52731
  1047
        Const (name, Library.foldr (op -->)
traytel@52923
  1048
          (map2 (curry op -->) (mk_FTs (passives @ actives)) actives, T --> active)))
traytel@52731
  1049
      fold_names (mk_Ts passives) actives;
blanchet@49504
  1050
    fun mk_fold Ts ss i = Term.list_comb (Const (nth fold_names (i - 1), Library.foldr (op -->)
blanchet@48975
  1051
      (map fastype_of ss, nth Ts (i - 1) --> range_type (fastype_of (nth ss (i - 1))))), ss);
traytel@55204
  1052
    val fold_defs = map (fn def =>
traytel@55204
  1053
      mk_unabs_def n (Morphism.thm phi def RS meta_eq_to_obj_eq)) fold_def_frees;
blanchet@48975
  1054
traytel@56237
  1055
    (* algebra copies *)
traytel@56237
  1056
traytel@61334
  1057
    val (((((((Bs, B's), ss), s's), inv_fs), fs), rec_ss), _) =
traytel@61272
  1058
      lthy
traytel@61272
  1059
      |> mk_Frees "B" BTs
traytel@61272
  1060
      ||>> mk_Frees "B'" B'Ts
traytel@61272
  1061
      ||>> mk_Frees "s" sTs
traytel@61272
  1062
      ||>> mk_Frees "s'" s'Ts
traytel@61272
  1063
      ||>> mk_Frees "f" inv_fTs
traytel@61272
  1064
      ||>> mk_Frees "f" fTs
traytel@61272
  1065
      ||>> mk_Frees "s" rec_sTs;
traytel@61272
  1066
traytel@56237
  1067
    val copy_thm =
traytel@56237
  1068
      let
traytel@56237
  1069
        val prems = HOLogic.mk_Trueprop (mk_alg Bs ss) ::
wenzelm@58634
  1070
          @{map 3} (HOLogic.mk_Trueprop ooo mk_bij_betw) inv_fs B's Bs;
traytel@56237
  1071
        val concl = HOLogic.mk_Trueprop (list_exists_free s's
traytel@56237
  1072
          (HOLogic.mk_conj (mk_alg B's s's, mk_mor B's s's Bs ss inv_fs)));
traytel@61334
  1073
        val vars = fold (Variable.add_free_names lthy) (concl :: prems) [];
traytel@56237
  1074
      in
traytel@61334
  1075
        Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, concl))
traytel@60728
  1076
          (fn {context = ctxt, prems = _} => mk_copy_tac ctxt m alg_def mor_def alg_set_thms
traytel@60728
  1077
            set_mapss)
traytel@61334
  1078
        |> Thm.close_derivation
traytel@56237
  1079
      end;
traytel@56237
  1080
traytel@56237
  1081
    val init_ex_mor_thm =
traytel@56237
  1082
      let
traytel@56237
  1083
        val goal = HOLogic.mk_Trueprop
traytel@56237
  1084
          (list_exists_free fs (mk_mor UNIVs ctors active_UNIVs ss fs));
traytel@61334
  1085
        val vars = Variable.add_free_names lthy goal [];
traytel@56237
  1086
      in
traytel@61334
  1087
        Goal.prove_sorry lthy vars [] goal
traytel@56272
  1088
          (fn {context = ctxt, prems = _} =>
traytel@56272
  1089
            mk_init_ex_mor_tac ctxt Abs_IIT_inverse_thm (alg_min_alg_thm RS copy_thm)
traytel@56272
  1090
              card_of_min_alg_thms mor_Rep_thm mor_comp_thm mor_select_thm mor_incl_thm)
traytel@56272
  1091
        |> Thm.close_derivation
traytel@56237
  1092
      end;
traytel@56237
  1093
blanchet@49504
  1094
    val mor_fold_thm =
blanchet@48975
  1095
      let
blanchet@48975
  1096
        val mor_cong = mor_cong_thm OF (map (mk_nth_conv n) ks);
wenzelm@59621
  1097
        val cT = Thm.ctyp_of lthy foldT;
wenzelm@59621
  1098
        val ct = Thm.cterm_of lthy fold_fun
traytel@61334
  1099
        val goal = HOLogic.mk_Trueprop (mk_mor UNIVs ctors active_UNIVs ss (map (mk_fold Ts ss) ks));
traytel@61334
  1100
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
  1101
      in
traytel@61334
  1102
        Goal.prove_sorry lthy vars [] goal
wenzelm@59498
  1103
          (fn {context = ctxt, ...} =>
wenzelm@59498
  1104
            mk_mor_fold_tac ctxt cT ct fold_defs init_ex_mor_thm mor_cong)
traytel@49109
  1105
        |> Thm.close_derivation
blanchet@48975
  1106
      end;
blanchet@48975
  1107
blanchet@49504
  1108
    val ctor_fold_thms = map (fn morE => rule_by_tactic lthy
traytel@60728
  1109
      ((rtac lthy CollectI THEN' CONJ_WRAP' (K (rtac lthy @{thm subset_UNIV})) (1 upto m + n)) 1)
blanchet@49504
  1110
      (mor_fold_thm RS morE)) morE_thms;
blanchet@48975
  1111
blanchet@49504
  1112
    val (fold_unique_mor_thms, fold_unique_mor_thm) =
blanchet@48975
  1113
      let
blanchet@49501
  1114
        val prem = HOLogic.mk_Trueprop (mk_mor UNIVs ctors active_UNIVs ss fs);
blanchet@49504
  1115
        fun mk_fun_eq f i = HOLogic.mk_eq (f, mk_fold Ts ss i);
blanchet@48975
  1116
        val unique = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj (map2 mk_fun_eq fs ks));
traytel@61334
  1117
        val vars = fold (Variable.add_free_names lthy) [prem, unique] [];
traytel@61334
  1118
        val unique_mor = Goal.prove_sorry lthy vars [] (Logic.mk_implies (prem, unique))
traytel@60728
  1119
          (fn {context = ctxt, prems = _} => mk_fold_unique_mor_tac ctxt type_defs
traytel@60728
  1120
            init_unique_mor_thms Reps mor_comp_thm mor_Abs_thm mor_fold_thm)
traytel@61334
  1121
          |> Thm.close_derivation;
blanchet@48975
  1122
      in
blanchet@48975
  1123
        `split_conj_thm unique_mor
blanchet@48975
  1124
      end;
blanchet@48975
  1125
traytel@52911
  1126
    val (ctor_fold_unique_thms, ctor_fold_unique_thm) =
traytel@52911
  1127
      `split_conj_thm (mk_conjIN n RS
traytel@52904
  1128
        (mor_UNIV_thm RS iffD2 RS fold_unique_mor_thm))
blanchet@48975
  1129
blanchet@49504
  1130
    val fold_ctor_thms =
blanchet@48975
  1131
      map (fn thm => (mor_incl_thm OF replicate n @{thm subset_UNIV}) RS thm RS sym)
blanchet@49504
  1132
        fold_unique_mor_thms;
blanchet@48975
  1133
blanchet@49504
  1134
    val ctor_o_fold_thms =
blanchet@48975
  1135
      let
blanchet@49504
  1136
        val mor = mor_comp_thm OF [mor_fold_thm, mor_str_thm];
blanchet@48975
  1137
      in
blanchet@49504
  1138
        map2 (fn unique => fn fold_ctor =>
blanchet@49504
  1139
          trans OF [mor RS unique, fold_ctor]) fold_unique_mor_thms fold_ctor_thms
blanchet@48975
  1140
      end;
blanchet@48975
  1141
blanchet@49504
  1142
    val timer = time (timer "fold definitions & thms");
blanchet@48975
  1143
blanchet@49501
  1144
    val map_ctors = map2 (fn Ds => fn bnf =>
blanchet@48975
  1145
      Term.list_comb (mk_map_of_bnf Ds (passiveAs @ FTs) (passiveAs @ Ts) bnf,
blanchet@49501
  1146
        map HOLogic.id_const passiveAs @ ctors)) Dss bnfs;
blanchet@48975
  1147
blanchet@54492
  1148
    fun dtor_bind i = nth external_bs (i - 1) |> Binding.prefix_name (dtorN ^ "_");
wenzelm@59859
  1149
    val dtor_def_bind = rpair [] o Binding.concealed o Thm.def_binding o dtor_bind;
blanchet@48975
  1150
traytel@55204
  1151
    fun dtor_spec i = mk_fold Ts map_ctors i;
blanchet@48975
  1152
blanchet@49501
  1153
    val ((dtor_frees, (_, dtor_def_frees)), (lthy, lthy_old)) =
blanchet@49311
  1154
      lthy
traytel@61101
  1155
      |> Local_Theory.open_target |> snd
traytel@55204
  1156
      |> fold_map (fn i =>
traytel@55204
  1157
        Local_Theory.define ((dtor_bind i, NoSyn), (dtor_def_bind i, dtor_spec i))) ks
blanchet@49311
  1158
      |>> apsnd split_list o split_list
traytel@61101
  1159
      ||> `Local_Theory.close_target;
blanchet@48975
  1160
blanchet@48975
  1161
    val phi = Proof_Context.export_morphism lthy_old lthy;
blanchet@49501
  1162
    fun mk_dtors params =
blanchet@48975
  1163
      map (Term.subst_atomic_types (map (Morphism.typ phi) params' ~~ params) o Morphism.term phi)
blanchet@49501
  1164
        dtor_frees;
blanchet@49501
  1165
    val dtors = mk_dtors params';
traytel@55204
  1166
    val dtor_defs = map (fn def => Morphism.thm phi def RS meta_eq_to_obj_eq) dtor_def_frees;
blanchet@48975
  1167
blanchet@49504
  1168
    val ctor_o_dtor_thms = map2 (fold_thms lthy o single) dtor_defs ctor_o_fold_thms;
blanchet@48975
  1169
blanchet@49501
  1170
    val dtor_o_ctor_thms =
blanchet@48975
  1171
      let
blanchet@49501
  1172
        fun mk_goal dtor ctor FT =
blanchet@49501
  1173
          mk_Trueprop_eq (HOLogic.mk_comp (dtor, ctor), HOLogic.id_const FT);
wenzelm@58634
  1174
        val goals = @{map 3} mk_goal dtors ctors FTs;
blanchet@48975
  1175
      in
wenzelm@58634
  1176
        @{map 5} (fn goal => fn dtor_def => fn foldx => fn map_comp_id => fn map_cong0L =>
wenzelm@51551
  1177
          Goal.prove_sorry lthy [] [] goal
traytel@60728
  1178
            (fn {context = ctxt, prems = _} => mk_dtor_o_ctor_tac ctxt dtor_def foldx map_comp_id
traytel@60728
  1179
              map_cong0L ctor_o_fold_thms)
traytel@49109
  1180
          |> Thm.close_derivation)
blanchet@51761
  1181
        goals dtor_defs ctor_fold_thms map_comp_id_thms map_cong0L_thms
blanchet@48975
  1182
      end;
blanchet@48975
  1183
blanchet@49501
  1184
    val dtor_ctor_thms = map (fn thm => thm RS @{thm pointfree_idE}) dtor_o_ctor_thms;
blanchet@49501
  1185
    val ctor_dtor_thms = map (fn thm => thm RS @{thm pointfree_idE}) ctor_o_dtor_thms;
blanchet@48975
  1186
blanchet@49501
  1187
    val bij_dtor_thms =
blanchet@49501
  1188
      map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) ctor_o_dtor_thms dtor_o_ctor_thms;
blanchet@49501
  1189
    val inj_dtor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_dtor_thms;
blanchet@49501
  1190
    val surj_dtor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_dtor_thms;
blanchet@49501
  1191
    val dtor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_dtor_thms;
blanchet@49501
  1192
    val dtor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_dtor_thms;
blanchet@49501
  1193
    val dtor_exhaust_thms = map (fn thm => thm RS exE) dtor_nchotomy_thms;
blanchet@48975
  1194
blanchet@49501
  1195
    val bij_ctor_thms =
blanchet@49501
  1196
      map2 (fn thm1 => fn thm2 => @{thm o_bij} OF [thm1, thm2]) dtor_o_ctor_thms ctor_o_dtor_thms;
blanchet@49501
  1197
    val inj_ctor_thms = map (fn thm => thm RS @{thm bij_is_inj}) bij_ctor_thms;
blanchet@49501
  1198
    val surj_ctor_thms = map (fn thm => thm RS @{thm bij_is_surj}) bij_ctor_thms;
blanchet@49501
  1199
    val ctor_nchotomy_thms = map (fn thm => thm RS @{thm surjD}) surj_ctor_thms;
blanchet@49501
  1200
    val ctor_inject_thms = map (fn thm => thm RS @{thm inj_eq}) inj_ctor_thms;
blanchet@49501
  1201
    val ctor_exhaust_thms = map (fn thm => thm RS exE) ctor_nchotomy_thms;
blanchet@48975
  1202
blanchet@49501
  1203
    val timer = time (timer "dtor definitions & thms");
blanchet@48975
  1204
traytel@61334
  1205
    val (((((((((Izs, (Izs1, Izs1'))), (Izs2, Izs2')), xFs), yFs), fs), rec_ss), init_phis), _) =
traytel@61272
  1206
      lthy
traytel@61272
  1207
      |> mk_Frees "z" Ts
traytel@61272
  1208
      ||>> mk_Frees' "z1" Ts
traytel@61272
  1209
      ||>> mk_Frees' "z2" Ts'
traytel@61272
  1210
      ||>> mk_Frees "x" FTs
traytel@61272
  1211
      ||>> mk_Frees "y" FTs'
traytel@61272
  1212
      ||>> mk_Frees "f" fTs
traytel@61272
  1213
      ||>> mk_Frees "s" rec_sTs
traytel@61272
  1214
      ||>> mk_Frees "P" (replicate n (mk_pred1T initT));
traytel@61272
  1215
traytel@61272
  1216
    val phis = map2 retype_const_or_free (map mk_pred1T Ts) init_phis;
traytel@61272
  1217
    val phi2s = map2 retype_const_or_free (map2 mk_pred2T Ts Ts') init_phis;
traytel@61272
  1218
blanchet@49501
  1219
    val (ctor_induct_thm, induct_params) =
blanchet@48975
  1220
      let
blanchet@49501
  1221
        fun mk_prem phi ctor sets x =
blanchet@48975
  1222
          let
blanchet@48975
  1223
            fun mk_IH phi set z =
blanchet@48975
  1224
              let
desharna@57567
  1225
                val prem = mk_Trueprop_mem (z, set $ x);
blanchet@48975
  1226
                val concl = HOLogic.mk_Trueprop (phi $ z);
blanchet@48975
  1227
              in
blanchet@48975
  1228
                Logic.all z (Logic.mk_implies (prem, concl))
blanchet@48975
  1229
              end;
blanchet@48975
  1230
wenzelm@58634
  1231
            val IHs = @{map 3} mk_IH phis (drop m sets) Izs;
blanchet@49501
  1232
            val concl = HOLogic.mk_Trueprop (phi $ (ctor $ x));
blanchet@48975
  1233
          in
blanchet@48975
  1234
            Logic.all x (Logic.list_implies (IHs, concl))
blanchet@48975
  1235
          end;
blanchet@48975
  1236
wenzelm@58634
  1237
        val prems = @{map 4} mk_prem phis ctors FTs_setss xFs;
blanchet@48975
  1238
blanchet@48975
  1239
        fun mk_concl phi z = phi $ z;
blanchet@57307
  1240
        val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj (map2 mk_concl phis Izs));
blanchet@48975
  1241
blanchet@48975
  1242
        val goal = Logic.list_implies (prems, concl);
traytel@61334
  1243
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
  1244
      in
traytel@61334
  1245
        (Goal.prove_sorry lthy vars [] goal
blanchet@62698
  1246
          (fn {context = ctxt, prems = _} =>
traytel@61271
  1247
            mk_ctor_induct_tac ctxt m set_mapss init_induct_thm morE_thms mor_Abs_thm
traytel@61271
  1248
            Rep_inverses Abs_inverses Reps)
traytel@61334
  1249
        |> Thm.close_derivation,
traytel@49109
  1250
        rev (Term.add_tfrees goal []))
blanchet@48975
  1251
      end;
blanchet@48975
  1252
wenzelm@59621
  1253
    val cTs = map (SOME o Thm.ctyp_of lthy o TFree) induct_params;
blanchet@48975
  1254
blanchet@49501
  1255
    val weak_ctor_induct_thms =
blanchet@54487
  1256
      let fun insts i = (replicate (i - 1) TrueI) @ (asm_rl :: replicate (n - i) TrueI);
blanchet@49501
  1257
      in map (fn i => (ctor_induct_thm OF insts i) RS mk_conjunctN n i) ks end;
blanchet@48975
  1258
blanchet@49501
  1259
    val (ctor_induct2_thm, induct2_params) =
blanchet@48975
  1260
      let
blanchet@49501
  1261
        fun mk_prem phi ctor ctor' sets sets' x y =
blanchet@48975
  1262
          let
blanchet@48975
  1263
            fun mk_IH phi set set' z1 z2 =
blanchet@48975
  1264
              let
desharna@57567
  1265
                val prem1 = mk_Trueprop_mem (z1, (set $ x));
desharna@57567
  1266
                val prem2 = mk_Trueprop_mem (z2, (set' $ y));
blanchet@48975
  1267
                val concl = HOLogic.mk_Trueprop (phi $ z1 $ z2);
blanchet@48975
  1268
              in
blanchet@48975
  1269
                fold_rev Logic.all [z1, z2] (Logic.list_implies ([prem1, prem2], concl))
blanchet@48975
  1270
              end;
blanchet@48975
  1271
wenzelm@58634
  1272
            val IHs = @{map 5} mk_IH phi2s (drop m sets) (drop m sets') Izs1 Izs2;
blanchet@49501
  1273
            val concl = HOLogic.mk_Trueprop (phi $ (ctor $ x) $ (ctor' $ y));
blanchet@48975
  1274
          in
blanchet@48975
  1275
            fold_rev Logic.all [x, y] (Logic.list_implies (IHs, concl))
blanchet@48975
  1276
          end;
blanchet@48975
  1277
wenzelm@58634
  1278
        val prems = @{map 7} mk_prem phi2s ctors ctor's FTs_setss FTs'_setss xFs yFs;
blanchet@48975
  1279
blanchet@48975
  1280
        fun mk_concl phi z1 z2 = phi $ z1 $ z2;
blanchet@48975
  1281
        val concl = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
wenzelm@58634
  1282
          (@{map 3} mk_concl phi2s Izs1 Izs2));
blanchet@48975
  1283
        fun mk_t phi (z1, z1') (z2, z2') =
blanchet@48975
  1284
          Term.absfree z1' (HOLogic.mk_all (fst z2', snd z2', phi $ z1 $ z2));
wenzelm@59621
  1285
        val cts = @{map 3} (SOME o Thm.cterm_of lthy ooo mk_t) phi2s (Izs1 ~~ Izs1') (Izs2 ~~ Izs2');
blanchet@48975
  1286
        val goal = Logic.list_implies (prems, concl);
traytel@61334
  1287
        val vars = Variable.add_free_names lthy goal [];
blanchet@48975
  1288
      in
traytel@61334
  1289
        (Goal.prove_sorry lthy vars [] goal
traytel@56272
  1290
          (fn {context = ctxt, prems = _} => mk_ctor_induct2_tac ctxt cTs cts ctor_induct_thm
traytel@56272
  1291
            weak_ctor_induct_thms)
traytel@61334
  1292
        |> Thm.close_derivation,
blanchet@48975
  1293
        rev (Term.add_tfrees goal []))
blanchet@48975
  1294
      end;
blanchet@48975
  1295
blanchet@48975
  1296
    val timer = time (timer "induction");
blanchet@48975
  1297
blanchet@53270
  1298
    fun mk_ctor_map_DEADID_thm ctor_inject map_id0 =
blanchet@53270
  1299
      trans OF [id_apply, iffD2 OF [ctor_inject, map_id0 RS sym]];
traytel@51917
  1300
traytel@62863
  1301
    fun mk_ctor_map_unique_DEADID_thm () =
traytel@62863
  1302
      let
traytel@62863
  1303
        val (funs, algs) =
traytel@62863
  1304
          HOLogic.conjuncts (HOLogic.dest_Trueprop (Thm.concl_of ctor_fold_unique_thm))
traytel@62863
  1305
          |> map_split HOLogic.dest_eq
traytel@62863
  1306
          ||>  snd o strip_comb o hd
traytel@62863
  1307
          |> @{apply 2} (map (fst o dest_Var));
traytel@62863
  1308
        fun mk_fun_insts T ix = Thm.cterm_of lthy (Var (ix, T --> T));
traytel@62863
  1309
        val theta =
traytel@62863
  1310
          (funs ~~ @{map 2} mk_fun_insts Ts funs) @ (algs ~~ map (Thm.cterm_of lthy) ctors);
traytel@62863
  1311
        val ctor_fold_ctors = (ctor_fold_unique_thm OF
traytel@62863
  1312
          map (fn thm => mk_trans @{thm id_o} (mk_sym (thm RS
traytel@62863
  1313
            @{thm trans[OF arg_cong2[of _ _ _ _ "op o", OF refl] o_id]}))) map_id0s)
traytel@62863
  1314
          |> split_conj_thm |> map mk_sym;
traytel@62863
  1315
      in
traytel@62863
  1316
        infer_instantiate lthy theta ctor_fold_unique_thm
traytel@62863
  1317
        |> unfold_thms lthy ctor_fold_ctors
traytel@62863
  1318
        |> Morphism.thm (Local_Theory.target_morphism lthy)
traytel@62863
  1319
      end;
traytel@62863
  1320
traytel@51917
  1321
    fun mk_ctor_Irel_DEADID_thm ctor_inject bnf =
traytel@51917
  1322
      trans OF [ctor_inject, rel_eq_of_bnf bnf RS @{thm predicate2_eqD} RS sym];
traytel@51917
  1323
traytel@51918
  1324
    val IphiTs = map2 mk_pred2T passiveAs passiveBs;
traytel@54841
  1325
    val Ipsi1Ts = map2 mk_pred2T passiveAs passiveCs;
traytel@54841
  1326
    val Ipsi2Ts = map2 mk_pred2T passiveCs passiveBs;
traytel@52731
  1327
    val activephiTs = map2 mk_pred2T activeAs activeBs;
traytel@51918
  1328
    val activeIphiTs = map2 mk_pred2T Ts Ts';
traytel@61272
  1329
traytel@51918
  1330
    val rels = map2 (fn Ds => mk_rel_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
traytel@51918
  1331
blanchet@48975
  1332
    (*register new datatypes as BNFs*)
traytel@62863
  1333
    val (timer, Ibnfs, (ctor_Imap_o_thms, ctor_Imap_thms), ctor_Imap_unique_thm, ctor_Iset_thmss',
traytel@53567
  1334
        ctor_Irel_thms, Ibnf_notes, lthy) =
blanchet@49585
  1335
      if m = 0 then
traytel@52913
  1336
        (timer, replicate n DEADID_bnf,
traytel@62863
  1337
        map_split (`(mk_pointfree lthy)) (map2 mk_ctor_map_DEADID_thm ctor_inject_thms map_ids),
traytel@62863
  1338
        mk_ctor_map_unique_DEADID_thm (),
traytel@53567
  1339
        replicate n [], map2 mk_ctor_Irel_DEADID_thm ctor_inject_thms bnfs, [], lthy)
blanchet@49585
  1340
      else let
blanchet@48975
  1341
        val fTs = map2 (curry op -->) passiveAs passiveBs;
blanchet@48975
  1342
        val uTs = map2 (curry op -->) Ts Ts';
blanchet@48975
  1343
traytel@61272
  1344
        val ((((fs, fs'), (AFss, AFss')), (ys, ys')), _) =
traytel@61272
  1345
          lthy
blanchet@48975
  1346
          |> mk_Frees' "f" fTs
traytel@61272
  1347
          ||>> mk_Freess' "z" setFTss
traytel@51918
  1348
          ||>> mk_Frees' "y" passiveAs;
blanchet@48975
  1349
blanchet@48975
  1350
        val map_FTFT's = map2 (fn Ds =>
blanchet@48975
  1351
          mk_map_of_bnf Ds (passiveAs @ Ts) (passiveBs @ Ts')) Dss bnfs;
blanchet@48975
  1352
        fun mk_passive_maps ATs BTs Ts =
blanchet@48975
  1353
          map2 (fn Ds => mk_map_of_bnf Ds (ATs @ Ts) (BTs @ Ts)) Dss bnfs;
blanchet@49504
  1354
        fun mk_map_fold_arg fs Ts ctor fmap =
blanchet@49501
  1355
          HOLogic.mk_comp (ctor, Term.list_comb (fmap, fs @ map HOLogic.id_const Ts));
blanchet@49501
  1356
        fun mk_map Ts fs Ts' ctors mk_maps =
blanchet@49504
  1357
          mk_fold Ts (map2 (mk_map_fold_arg fs Ts') ctors (mk_maps Ts'));
blanchet@48975
  1358
        val pmapsABT' = mk_passive_maps passiveAs passiveBs;
blanchet@49501
  1359
        val fs_maps = map (mk_map Ts fs Ts' ctor's pmapsABT') ks;
blanchet@48975
  1360
blanchet@48975
  1361
        val ls = 1 upto m;
blanchet@48975
  1362
        val setsss = map (mk_setss o mk_set_Ts) passiveAs;
blanchet@48975
  1363
blanchet@48975
  1364
        fun mk_col l T z z' sets =
blanchet@48975
  1365
          let
blanchet@48975
  1366
            fun mk_UN set = mk_Union T $ (set $ z);
blanchet@48975
  1367
          in
blanchet@48975
  1368
            Term.absfree z'
blanchet@48975
  1369
              (mk_union (nth sets (l - 1) $ z,
blanchet@48975
  1370
                Library.foldl1 mk_union (map mk_UN (drop m sets))))
blanchet@48975
  1371
          end;
blanchet@48975
  1372
wenzelm@58634
  1373
        val colss = @{map 5} (fn l => fn T => @{map 3} (mk_col l T)) ls passiveAs AFss AFss' setsss;
blanchet@49504
  1374
        val setss_by_range = map (fn cols => map (mk_fold Ts cols) ks) colss;
blanchet@48975
  1375
        val setss_by_bnf = transpose setss_by_range;
blanchet@48975
  1376
traytel@54841
  1377
        val set_bss =
traytel@54841
  1378
          map (flat o map2 (fn B => fn b =>
blanchet@55702
  1379
            if member (op =) deads (TFree B) then [] else [b]) resBs) set_bss0;
blanchet@48975
  1380
blanchet@49501
  1381
        val ctor_witss =
blanchet@48975
  1382
          let
blanchet@48975
  1383
            val witss = map2 (fn Ds => fn bnf => mk_wits_of_bnf
blanchet@48975
  1384
              (replicate (nwits_of_bnf bnf) Ds)
blanchet@48975
  1385
              (replicate (nwits_of_bnf bnf) (passiveAs @ Ts)) bnf) Dss bnfs;
blanchet@48975
  1386
            fun close_wit (I, wit) = fold_rev Term.absfree (map (nth ys') I) wit;
blanchet@48975
  1387
            fun wit_apply (arg_I, arg_wit) (fun_I, fun_wit) =
blanchet@48975
  1388
              (union (op =) arg_I fun_I, fun_wit $ arg_wit);
blanchet@48975
  1389
blanchet@48975
  1390
            fun gen_arg support i =
blanchet@48975
  1391
              if i < m then [([i], nth ys i)]
blanchet@49501
  1392
              else maps (mk_wit support (nth ctors (i - m)) (i - m)) (nth support (i - m))
blanchet@49501
  1393
            and mk_wit support ctor i (I, wit) =
blanchet@48975
  1394
              let val args = map (gen_arg (nth_map i (remove (op =) (I, wit)) support)) I;
blanchet@48975
  1395
              in
blanchet@48975
  1396
                (args, [([], wit)])
blanchet@48975
  1397
                |-> fold (map_product wit_apply)
blanchet@49501
  1398
                |> map (apsnd (fn t => ctor $ t))
blanchet@48975
  1399
                |> minimize_wits
blanchet@48975
  1400
              end;
blanchet@48975
  1401
          in
wenzelm@58634
  1402
            @{map 3} (fn ctor => fn i => map close_wit o minimize_wits o maps (mk_wit witss ctor i))
blanchet@49501
  1403
              ctors (0 upto n - 1) witss
blanchet@48975
  1404
          end;
blanchet@48975
  1405
traytel@56350
  1406
        val (lthy, sbd0, sbd0_card_order, sbd0_Cinfinite, set_sbd0ss) =
traytel@56350
  1407
          if n = 1
traytel@56350
  1408
          then (lthy, hd bd0s, hd bd0_card_orders, hd bd0_Cinfinites, set_bd0ss)
traytel@56350
  1409
          else
traytel@56350
  1410
            let
traytel@56350
  1411
              val sum_bd0 = Library.foldr1 (uncurry mk_csum) bd0s;
traytel@56350
  1412
              val sum_bd0T = fst (dest_relT (fastype_of sum_bd0));
traytel@56516
  1413
              val (sum_bd0T_params, sum_bd0T_params') = `(map TFree) (Term.add_tfreesT sum_bd0T []);
traytel@56350
  1414
traytel@56350
  1415
              val sbd0T_bind = mk_internal_b (sum_bdTN ^ "0");
desharna@57567
  1416
traytel@56350
  1417
              val ((sbd0T_name, (sbd0T_glob_info, sbd0T_loc_info)), lthy) =
traytel@56516
  1418
                typedef (sbd0T_bind, sum_bd0T_params', NoSyn)
traytel@60728
  1419
                  (HOLogic.mk_UNIV sum_bd0T) NONE (fn ctxt =>
traytel@60728
  1420
                    EVERY' [rtac ctxt exI, rtac ctxt UNIV_I] 1) lthy;
desharna@57567
  1421
traytel@56516
  1422
              val sbd0T = Type (sbd0T_name, sum_bd0T_params);
traytel@56350
  1423
              val Abs_sbd0T = Const (#Abs_name sbd0T_glob_info, sum_bd0T --> sbd0T);
desharna@57567
  1424
traytel@56350
  1425
              val sbd0_bind = mk_internal_b (sum_bdN ^ "0");
traytel@56350
  1426
              val sbd0_def_bind = (Thm.def_binding sbd0_bind, []);
desharna@57567
  1427
traytel@56350
  1428
              val sbd0_spec = mk_dir_image sum_bd0 Abs_sbd0T;
desharna@57567
  1429
traytel@56350
  1430
              val ((sbd0_free, (_, sbd0_def_free)), (lthy, lthy_old)) =
traytel@56350
  1431
                lthy
traytel@61101
  1432
                |> Local_Theory.open_target |> snd
traytel@56350
  1433
                |> Local_Theory.define ((sbd0_bind, NoSyn), (sbd0_def_bind, sbd0_spec))
traytel@61101
  1434
                ||> `Local_Theory.close_target;
desharna@57567
  1435
traytel@56350
  1436
              val phi = Proof_Context.export_morphism lthy_old lthy;
desharna@57567
  1437
traytel@56350
  1438
              val sbd0_def = Morphism.thm phi sbd0_def_free RS meta_eq_to_obj_eq;
traytel@56350
  1439
              val sbd0 = Const (fst (Term.dest_Const (Morphism.term phi sbd0_free)),
traytel@56350
  1440
                mk_relT (`I sbd0T));
desharna@57567
  1441
traytel@56350
  1442
              val Abs_sbd0T_inj = mk_Abs_inj_thm (#Abs_inject sbd0T_loc_info);
traytel@56350
  1443
              val Abs_sbd0T_bij = mk_Abs_bij_thm lthy Abs_sbd0T_inj (#Abs_cases sbd0T_loc_info);
desharna@57567
  1444
traytel@56350
  1445
              val sum_Cinfinite = mk_sum_Cinfinite bd0_Cinfinites;
traytel@56350
  1446
              val sum_Card_order = sum_Cinfinite RS conjunct2;
traytel@56350
  1447
              val sum_card_order = mk_sum_card_order bd0_card_orders;
desharna@57567
  1448
traytel@56350
  1449
              val sbd0_ordIso = @{thm ssubst_Pair_rhs} OF
traytel@56350
  1450
                [@{thm dir_image} OF [Abs_sbd0T_inj, sum_Card_order], sbd0_def];
traytel@56350
  1451
              val sbd0_Cinfinite = @{thm Cinfinite_cong} OF [sbd0_ordIso, sum_Cinfinite];
desharna@57567
  1452
traytel@56350
  1453
              val sbd0_card_order = @{thm iffD2[OF arg_cong[of _ _ card_order]]} OF
traytel@56350
  1454
                [sbd0_def, @{thm card_order_dir_image} OF [Abs_sbd0T_bij, sum_card_order]];
desharna@57567
  1455
traytel@56350
  1456
              fun mk_set_sbd0 i bd0_Card_order bd0s =
traytel@56350
  1457
                map (fn thm => @{thm ordLeq_ordIso_trans} OF
traytel@56350
  1458
                  [bd0_Card_order RS mk_ordLeq_csum n i thm, sbd0_ordIso]) bd0s;
wenzelm@58634
  1459
              val set_sbd0ss = @{map 3} mk_set_sbd0 ks bd0_Card_orders set_bd0ss;
traytel@56350
  1460
            in
traytel@56350
  1461
              (lthy, sbd0, sbd0_card_order, sbd0_Cinfinite, set_sbd0ss)
traytel@56350
  1462
            end;
traytel@56350
  1463
traytel@54841
  1464
        val (Ibnf_consts, lthy) =
traytel@62324
  1465
          @{fold_map 9} (fn b => fn map_b => fn rel_b => fn pred_b => fn set_bs => fn mapx =>
traytel@62324
  1466
              fn sets => fn wits => fn T => fn lthy =>
traytel@56192
  1467
            define_bnf_consts Hardly_Inline (user_policy Note_Some lthy) false (SOME deads)
traytel@62324
  1468
              map_b rel_b pred_b set_bs
traytel@62324
  1469
              (((((((b, T), fold_rev Term.absfree fs' mapx), sets), sbd0), wits), NONE), NONE) lthy)
traytel@62324
  1470
          bs map_bs rel_bs pred_bs set_bss fs_maps setss_by_bnf ctor_witss Ts lthy;
traytel@54841
  1471
traytel@61272
  1472
        val ((((((((((((((Izs, (Izs1, Izs1')), (Izs2, Izs2')), xFs), yFs))), Iphis), Ipsi1s),
traytel@61334
  1473
            Ipsi2s), fs), fs_copy), us), (ys, ys')), _) =
traytel@61272
  1474
          lthy
traytel@61272
  1475
          |> mk_Frees "z" Ts
traytel@61272
  1476
          ||>> mk_Frees' "z1" Ts
traytel@61272
  1477
          ||>> mk_Frees' "z2" Ts'
traytel@61272
  1478
          ||>> mk_Frees "x" FTs
traytel@61272
  1479
          ||>> mk_Frees "y" FTs'
traytel@61272
  1480
          ||>> mk_Frees "R" IphiTs
traytel@61272
  1481
          ||>> mk_Frees "R" Ipsi1Ts
traytel@61272
  1482
          ||>> mk_Frees "Q" Ipsi2Ts
traytel@61272
  1483
          ||>> mk_Frees "f" fTs
traytel@61272
  1484
          ||>> mk_Frees "f" fTs
traytel@61272
  1485
          ||>> mk_Frees "u" uTs
traytel@61272
  1486
          ||>> mk_Frees' "y" passiveAs;
traytel@61272
  1487
wenzelm@58634
  1488
        val (_, Iconsts, Iconst_defs, mk_Iconsts) = @{split_list 4} Ibnf_consts;
traytel@62324
  1489
        val (_, Isetss, Ibds_Ds, Iwitss_Ds, _, _) = @{split_list 6} Iconsts;
traytel@62324
  1490
        val (Imap_defs, Iset_defss, Ibd_defs, Iwit_defss, Irel_defs, Ipred_defs) =
traytel@62324
  1491
          @{split_list 6} Iconst_defs;
traytel@62324
  1492
        val (mk_Imaps_Ds, mk_It_Ds, _, mk_Irels_Ds, mk_Ipreds_Ds, _, _) =
traytel@62324
  1493
          @{split_list 7} mk_Iconsts;
traytel@54841
  1494
traytel@54841
  1495
        val Irel_unabs_defs = map (fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) Irel_defs;
traytel@62324
  1496
        val Ipred_unabs_defs = map (fn def => mk_unabs_def m (def RS meta_eq_to_obj_eq)) Ipred_defs;
traytel@54841
  1497
        val Iset_defs = flat Iset_defss;
traytel@54841
  1498
traytel@54841
  1499
        fun mk_Imaps As Bs = map (fn mk => mk deads As Bs) mk_Imaps_Ds;
traytel@54841
  1500
        fun mk_Isetss As = map2 (fn mk => fn Isets => map (mk deads As) Isets) mk_It_Ds Isetss;
traytel@54841
  1501
        val Ibds = map2 (fn mk => mk deads passiveAs) mk_It_Ds Ibds_Ds;
traytel@54841
  1502
        val Iwitss =
traytel@54841
  1503
          map2 (fn mk => fn Iwits => map (mk deads passiveAs o snd) Iwits) mk_It_Ds Iwitss_Ds;
traytel@54841
  1504
        fun mk_Irels As Bs = map (fn mk => mk deads As Bs) mk_Irels_Ds;
traytel@62324
  1505
        fun mk_Ipreds As = map (fn mk => mk deads As) mk_Ipreds_Ds;
traytel@54841
  1506
traytel@54841
  1507
        val Imaps = mk_Imaps passiveAs passiveBs;
traytel@54841
  1508
        val fs_Imaps = map (fn m => Term.list_comb (m, fs)) Imaps;
traytel@54841
  1509
        val fs_copy_Imaps = map (fn m => Term.list_comb (m, fs_copy)) Imaps;
traytel@54841
  1510
        val (Isetss_by_range, Isetss_by_bnf) = `transpose (mk_Isetss passiveAs);
traytel@54841
  1511
traytel@54841
  1512
        val map_setss = map (fn T => map2 (fn Ds =>
traytel@54841
  1513
          mk_map_of_bnf Ds (passiveAs @ Ts) (mk_set_Ts T)) Dss bnfs) passiveAs;
traytel@54841
  1514
traytel@54841
  1515
        val timer = time (timer "bnf constants for the new datatypes");
traytel@54841
  1516
traytel@54841
  1517
        val (ctor_Imap_thms, ctor_Imap_o_thms) =
traytel@54841
  1518
          let
traytel@56272
  1519
            fun mk_goal fs_map map ctor ctor' =
traytel@56272
  1520
              mk_Trueprop_eq (HOLogic.mk_comp (fs_map, ctor),
traytel@56272
  1521
                HOLogic.mk_comp (ctor', Term.list_comb (map, fs @ fs_Imaps)));
wenzelm@58634
  1522
            val goals = @{map 4} mk_goal fs_Imaps map_FTFT's ctors ctor's;
traytel@54841
  1523
            val maps =
wenzelm@58634
  1524
              @{map 4} (fn goal => fn foldx => fn map_comp_id => fn map_cong0 =>
traytel@61334
  1525
                Variable.add_free_names lthy goal []
traytel@61334
  1526
                |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@54841
  1527
                  (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Imap_defs THEN
traytel@61334
  1528
                    mk_map_tac ctxt m n foldx map_comp_id map_cong0))
traytel@61334
  1529
                |> Thm.close_derivation)
traytel@54841
  1530
              goals ctor_fold_thms map_comp_id_thms map_cong0s;
traytel@54841
  1531
          in
traytel@54841
  1532
            `(map (fn thm => thm RS @{thm comp_eq_dest})) maps
traytel@54841
  1533
          end;
blanchet@48975
  1534
traytel@54841
  1535
        val (ctor_Imap_unique_thms, ctor_Imap_unique_thm) =
traytel@54841
  1536
          let
traytel@54841
  1537
            fun mk_prem u map ctor ctor' =
traytel@54841
  1538
              mk_Trueprop_eq (HOLogic.mk_comp (u, ctor),
traytel@54841
  1539
                HOLogic.mk_comp (ctor', Term.list_comb (map, fs @ us)));
wenzelm@58634
  1540
            val prems = @{map 4} mk_prem us map_FTFT's ctors ctor's;
traytel@54841
  1541
            val goal =
traytel@54841
  1542
              HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
traytel@54841
  1543
                (map2 (curry HOLogic.mk_eq) us fs_Imaps));
traytel@61334
  1544
            val vars = fold (Variable.add_free_names lthy) (goal :: prems) [];
traytel@61334
  1545
            val unique = Goal.prove_sorry lthy vars [] (Logic.list_implies (prems, goal))
traytel@54841
  1546
              (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Imap_defs THEN
traytel@55197
  1547
                mk_ctor_map_unique_tac ctxt ctor_fold_unique_thm sym_map_comps)
traytel@61334
  1548
              |> Thm.close_derivation;
traytel@54841
  1549
          in
traytel@54841
  1550
            `split_conj_thm unique
traytel@54841
  1551
          end;
traytel@54841
  1552
traytel@54841
  1553
        val timer = time (timer "map functions for the new datatypes");
traytel@54841
  1554
traytel@54841
  1555
        val ctor_Iset_thmss =
traytel@54841
  1556
          let
traytel@54841
  1557
            fun mk_goal sets ctor set col map =
traytel@54841
  1558
              mk_Trueprop_eq (HOLogic.mk_comp (set, ctor),
traytel@54841
  1559
                HOLogic.mk_comp (col, Term.list_comb (map, passive_ids @ sets)));
traytel@54841
  1560
            val goalss =
wenzelm@58634
  1561
              @{map 3} (fn sets => @{map 4} (mk_goal sets) ctors sets)
wenzelm@58634
  1562
                Isetss_by_range colss map_setss;
traytel@54841
  1563
            val setss = map (map2 (fn foldx => fn goal =>
traytel@54841
  1564
                Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, prems = _} =>
traytel@60728
  1565
                  unfold_thms_tac ctxt Iset_defs THEN mk_set_tac ctxt foldx)
traytel@54841
  1566
                |> Thm.close_derivation)
traytel@54841
  1567
              ctor_fold_thms) goalss;
traytel@54841
  1568
traytel@54841
  1569
            fun mk_simp_goal pas_set act_sets sets ctor z set =
traytel@56272
  1570
              mk_Trueprop_eq (set $ (ctor $ z),
traytel@54841
  1571
                mk_union (pas_set $ z,
traytel@56272
  1572
                  Library.foldl1 mk_union (map2 (fn X => mk_UNION (X $ z)) act_sets sets)));
traytel@54841
  1573
            val simp_goalss =
traytel@54841
  1574
              map2 (fn i => fn sets =>
wenzelm@58634
  1575
                @{map 4} (fn Fsets => mk_simp_goal (nth Fsets (i - 1)) (drop m Fsets) sets)
traytel@54841
  1576
                  FTs_setss ctors xFs sets)
traytel@54841
  1577
                ls Isetss_by_range;
traytel@54841
  1578
wenzelm@58634
  1579
            val ctor_setss = @{map 3} (fn i => @{map 3} (fn set_nats => fn goal => fn set =>
traytel@61334
  1580
              Variable.add_free_names lthy goal []
traytel@61334
  1581
              |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@60728
  1582
                  (fn {context = ctxt, prems = _} =>
traytel@61334
  1583
                    mk_ctor_set_tac ctxt set (nth set_nats (i - 1)) (drop m set_nats)))
traytel@61334
  1584
                |> Thm.close_derivation)
traytel@54841
  1585
              set_mapss) ls simp_goalss setss;
traytel@54841
  1586
          in
traytel@54841
  1587
            ctor_setss
traytel@54841
  1588
          end;
blanchet@48975
  1589
traytel@54841
  1590
        fun mk_set_thms ctor_set = (@{thm xt1(3)} OF [ctor_set, @{thm Un_upper1}]) ::
traytel@54841
  1591
          map (fn i => (@{thm xt1(3)} OF [ctor_set, @{thm Un_upper2}]) RS
traytel@54841
  1592
            (mk_Un_upper n i RS subset_trans) RSN
traytel@54841
  1593
            (2, @{thm UN_upper} RS subset_trans))
traytel@54841
  1594
            (1 upto n);
traytel@54841
  1595
        val set_Iset_thmsss = transpose (map (map mk_set_thms) ctor_Iset_thmss);
traytel@54841
  1596
traytel@54841
  1597
        val timer = time (timer "set functions for the new datatypes");
traytel@54841
  1598
wenzelm@59621
  1599
        val cxs = map (SOME o Thm.cterm_of lthy) Izs;
traytel@54841
  1600
        val Isetss_by_range' =
traytel@54841
  1601
          map (map (Term.subst_atomic_types (passiveAs ~~ passiveBs))) Isetss_by_range;
traytel@54841
  1602
traytel@54841
  1603
        val Iset_Imap0_thmss =
traytel@54841
  1604
          let
traytel@54841
  1605
            fun mk_set_map0 f map z set set' =
traytel@54841
  1606
              HOLogic.mk_eq (mk_image f $ (set $ z), set' $ (map $ z));
traytel@54841
  1607
wenzelm@59621
  1608
            fun mk_cphi f map z set set' = Thm.cterm_of lthy
traytel@54841
  1609
              (Term.absfree (dest_Free z) (mk_set_map0 f map z set set'));
traytel@54841
  1610
wenzelm@59621
  1611
            val csetss = map (map (Thm.cterm_of lthy)) Isetss_by_range';
traytel@54841
  1612
wenzelm@58634
  1613
            val cphiss = @{map 3} (fn f => fn sets => fn sets' =>
wenzelm@58634
  1614
              (@{map 4} (mk_cphi f) fs_Imaps Izs sets sets')) fs Isetss_by_range Isetss_by_range';
blanchet@48975
  1615
traytel@54841
  1616
            val inducts = map (fn cphis =>
wenzelm@60801
  1617
              Thm.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm) cphiss;
traytel@54841
  1618
traytel@54841
  1619
            val goals =
wenzelm@58634
  1620
              @{map 3} (fn f => fn sets => fn sets' =>
traytel@54841
  1621
                HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
wenzelm@58634
  1622
                  (@{map 4} (mk_set_map0 f) fs_Imaps Izs sets sets')))
traytel@54841
  1623
                  fs Isetss_by_range Isetss_by_range';
traytel@54841
  1624
traytel@60728
  1625
            fun mk_tac ctxt induct = mk_set_nat_tac ctxt m (rtac ctxt induct) set_mapss ctor_Imap_thms;
traytel@54841
  1626
            val thms =
wenzelm@58634
  1627
              @{map 5} (fn goal => fn csets => fn ctor_sets => fn induct => fn i =>
traytel@61334
  1628
                Variable.add_free_names lthy goal []
traytel@61334
  1629
                |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@61334
  1630
                  (fn {context = ctxt, prems = _} => mk_tac ctxt induct csets ctor_sets i))
traytel@61334
  1631
                |> Thm.close_derivation)
traytel@54841
  1632
              goals csetss ctor_Iset_thmss inducts ls;
traytel@54841
  1633
          in
traytel@54841
  1634
            map split_conj_thm thms
traytel@54841
  1635
          end;
traytel@54841
  1636
traytel@54841
  1637
        val Iset_bd_thmss =
traytel@54841
  1638
          let
traytel@54841
  1639
            fun mk_set_bd z bd set = mk_ordLeq (mk_card_of (set $ z)) bd;
traytel@54841
  1640
wenzelm@59621
  1641
            fun mk_cphi z set = Thm.cterm_of lthy (Term.absfree (dest_Free z) (mk_set_bd z sbd0 set));
blanchet@48975
  1642
traytel@54841
  1643
            val cphiss = map (map2 mk_cphi Izs) Isetss_by_range;
traytel@54841
  1644
traytel@54841
  1645
            val inducts = map (fn cphis =>
wenzelm@60801
  1646
              Thm.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm) cphiss;
traytel@54841
  1647
traytel@54841
  1648
            val goals =
traytel@54841
  1649
              map (fn sets =>
traytel@54841
  1650
                HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
wenzelm@58634
  1651
                  (@{map 3} mk_set_bd Izs Ibds sets))) Isetss_by_range;
traytel@54841
  1652
traytel@60728
  1653
            fun mk_tac ctxt induct = mk_set_bd_tac ctxt m (rtac ctxt induct) sbd0_Cinfinite set_sbd0ss;
traytel@54841
  1654
            val thms =
wenzelm@58634
  1655
              @{map 4} (fn goal => fn ctor_sets => fn induct => fn i =>
traytel@61334
  1656
                Variable.add_free_names lthy goal []
traytel@61334
  1657
                |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@54841
  1658
                    (fn {context = ctxt, prems = _} => unfold_thms_tac ctxt Ibd_defs THEN
traytel@61334
  1659
                      mk_tac ctxt induct ctor_sets i))
traytel@61334
  1660
                |> Thm.close_derivation)
traytel@54841
  1661
              goals ctor_Iset_thmss inducts ls;
traytel@54841
  1662
          in
traytel@54841
  1663
            map split_conj_thm thms
traytel@54841
  1664
          end;
blanchet@48975
  1665
traytel@54841
  1666
        val Imap_cong0_thms =
traytel@54841
  1667
          let
traytel@54841
  1668
            fun mk_prem z set f g y y' =
traytel@54841
  1669
              mk_Ball (set $ z) (Term.absfree y' (HOLogic.mk_eq (f $ y, g $ y)));
traytel@54841
  1670
traytel@54841
  1671
            fun mk_map_cong0 sets z fmap gmap =
traytel@54841
  1672
              HOLogic.mk_imp
wenzelm@58634
  1673
                (Library.foldr1 HOLogic.mk_conj (@{map 5} (mk_prem z) sets fs fs_copy ys ys'),
traytel@54841
  1674
                HOLogic.mk_eq (fmap $ z, gmap $ z));
traytel@54841
  1675
traytel@54841
  1676
            fun mk_cphi sets z fmap gmap =
wenzelm@59621
  1677
              Thm.cterm_of lthy (Term.absfree (dest_Free z) (mk_map_cong0 sets z fmap gmap));
traytel@54841
  1678
wenzelm@58634
  1679
            val cphis = @{map 4} mk_cphi Isetss_by_bnf Izs fs_Imaps fs_copy_Imaps;
traytel@54841
  1680
wenzelm@60801
  1681
            val induct = Thm.instantiate' cTs (map SOME cphis @ cxs) ctor_induct_thm;
traytel@54841
  1682
traytel@54841
  1683
            val goal =
traytel@54841
  1684
              HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj
wenzelm@58634
  1685
                (@{map 4} mk_map_cong0 Isetss_by_bnf Izs fs_Imaps fs_copy_Imaps));
traytel@61334
  1686
            val vars = Variable.add_free_names lthy goal [];
traytel@54841
  1687
traytel@61334
  1688
            val thm = Goal.prove_sorry lthy vars [] goal
traytel@60728
  1689
                (fn {context = ctxt, prems = _} => mk_mcong_tac ctxt (rtac ctxt induct) set_Iset_thmsss
traytel@56272
  1690
                  map_cong0s ctor_Imap_thms)
traytel@61334
  1691
              |> Thm.close_derivation;
traytel@54841
  1692
          in
traytel@54841
  1693
            split_conj_thm thm
traytel@54841
  1694
          end;
blanchet@48975
  1695
traytel@51893
  1696
        val in_rels = map in_rel_of_bnf bnfs;
traytel@54841
  1697
        val in_Irels = map (fn def => trans OF [def, @{thm OO_Grp_alt}] RS @{thm predicate2_eqD})
traytel@54841
  1698
            Irel_unabs_defs;
traytel@54841
  1699
traytel@54841
  1700
        val ctor_Iset_incl_thmss = map (map hd) set_Iset_thmsss;
traytel@54841
  1701
        val ctor_set_Iset_incl_thmsss = map (transpose o map tl) set_Iset_thmsss;
traytel@54841
  1702
        val ctor_Iset_thmss' = transpose ctor_Iset_thmss;
blanchet@48975
  1703
traytel@54841
  1704
        val Irels = mk_Irels passiveAs passiveBs;
traytel@62324
  1705
        val Ipreds = mk_Ipreds passiveAs;
traytel@54841
  1706
        val Irelphis = map (fn rel => Term.list_comb (rel, Iphis)) Irels;
traytel@54841
  1707
        val relphis = map (fn rel => Term.list_comb (rel, Iphis @ Irelphis)) rels;
traytel@54841
  1708
        val Irelpsi1s = map (fn rel => Term.list_comb (rel, Ipsi1s)) (mk_Irels passiveAs passiveCs);
traytel@54841
  1709
        val Irelpsi2s = map (fn rel => Term.list_comb (rel, Ipsi2s)) (mk_Irels passiveCs passiveBs);
traytel@54841
  1710
        val Irelpsi12s = map (fn rel =>
traytel@54841
  1711
            Term.list_comb (rel, map2 (curry mk_rel_compp) Ipsi1s Ipsi2s)) Irels;
blanchet@48975
  1712
traytel@51893
  1713
        val ctor_Irel_thms =
blanchet@48975
  1714
          let
traytel@56272
  1715
            fun mk_goal xF yF ctor ctor' Irelphi relphi =
traytel@56272
  1716
              mk_Trueprop_eq (Irelphi $ (ctor $ xF) $ (ctor' $ yF), relphi $ xF $ yF);
wenzelm@58634
  1717
            val goals = @{map 6} mk_goal xFs yFs ctors ctor's Irelphis relphis;
blanchet@48975
  1718
          in
wenzelm@58634
  1719
            @{map 12} (fn i => fn goal => fn in_rel => fn map_comp0 => fn map_cong0 =>
blanchet@49542
  1720
              fn ctor_map => fn ctor_sets => fn ctor_inject => fn ctor_dtor =>
blanchet@53289
  1721
              fn set_map0s => fn ctor_set_incls => fn ctor_set_set_inclss =>
traytel@61334
  1722
              Variable.add_free_names lthy goal []
traytel@61334
  1723
              |> (fn vars => Goal.prove_sorry lthy vars [] goal
traytel@61271
  1724
               (fn {context = ctxt, prems = _} =>
traytel@61271
  1725
                 mk_ctor_rel_tac ctxt in_Irels i in_rel map_comp0 map_cong0 ctor_map ctor_sets
traytel@61334
  1726
                   ctor_inject ctor_dtor set_map0s ctor_set_incls ctor_set_set_inclss))
traytel@61334
  1727
              |> Thm.close_derivation)
traytel@54841
  1728
            ks goals in_rels map_comps map_cong0s ctor_Imap_thms ctor_Iset_thmss'
traytel@54841
  1729
              ctor_inject_thms ctor_dtor_thms set_mapss ctor_Iset_incl_thmss
traytel@54841
  1730
              ctor_set_Iset_incl_thmsss
traytel@54841
  1731
          end;
traytel@54841
  1732
traytel@54841
  1733
        val le_Irel_OO_thm =
traytel@54841
  1734
          let
traytel@54841
  1735
            fun mk_le_Irel_OO Irelpsi1 Irelpsi2 Irelpsi12 Iz1 Iz2 =
traytel@54841
  1736
              HOLogic.mk_imp (mk_rel_compp (Irelpsi1, Irelpsi2) $ Iz1 $ Iz2,
traytel@54841
  1737
                Irelpsi12 $ Iz1 $ Iz2);
wenzelm@58634
  1738
            val goals = @{map 5} mk_le_Irel_OO Irelpsi1s Irelpsi2s Irelpsi12s Izs1 Izs2;
traytel@54841
  1739
wenzelm@59621
  1740
            val cTs = map (SOME o Thm.ctyp_of lthy o TFree) induct2_params;
wenzelm@59621
  1741
            val cxs = map (SOME o Thm.cterm_of lthy) (splice Izs1 Izs2);
wenzelm@59621
  1742
            fun mk_cphi z1 z2 goal = SOME (Thm.cterm_of lthy (Term.absfree z1 (Term.absfree z2 goal)));
wenzelm@58634
  1743
            val cphis = @{map 3} mk_cphi Izs1' Izs2' goals;
wenzelm@60801
  1744
            val induct = Thm.instantiate' cTs (cphis @ cxs) ctor_induct2_thm;
traytel@54841
  1745
traytel@54841
  1746
            val goal = HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj goals);
traytel@61334
  1747
            val vars = Variable.add_free_names lthy goal [];
traytel@54841
  1748
          in
traytel@61334
  1749
            Goal.prove_sorry lthy vars [] goal
traytel@56272
  1750
              (fn {context = ctxt, prems = _} => mk_le_rel_OO_tac ctxt m induct ctor_nchotomy_thms
desharna@57967
  1751
                ctor_Irel_thms rel_mono_strong0s le_rel_OOs)
traytel@56272
  1752
            |> Thm.close_derivation
blanchet@48975
  1753
          end;
blanchet@48975
  1754
traytel@54841
  1755
        val timer = time (timer "helpers for BNF properties");
traytel@54841
  1756
traytel@60728
  1757
        val map_id0_tacs = map (fn thm => fn ctxt => mk_map_id0_tac ctxt map_id0s thm)
traytel@60728
  1758
          ctor_Imap_unique_thms;
traytel@54841
  1759
        val map_comp0_tacs =
traytel@60728
  1760
          map2 (fn thm => fn i => fn ctxt =>
traytel@60728
  1761
            mk_map_comp0_tac ctxt map_comps ctor_Imap_thms thm i)
traytel@60728
  1762
          ctor_Imap_unique_thms ks;
traytel@55197
  1763
        val map_cong0_tacs = map (fn thm => fn ctxt => mk_map_cong0_tac ctxt m thm) Imap_cong0_thms;
traytel@60728
  1764
        val set_map0_tacss = map (map (fn thm => fn ctxt => mk_set_map0_tac ctxt thm))
traytel@60728
  1765
          (transpose Iset_Imap0_thmss);
traytel@55197
  1766
        val bd_co_tacs = replicate n (fn ctxt =>
traytel@60728
  1767
          unfold_thms_tac ctxt Ibd_defs THEN rtac ctxt sbd0_card_order 1);
traytel@55197
  1768
        val bd_cinf_tacs = replicate n (fn ctxt =>
traytel@60728
  1769
          unfold_thms_tac ctxt Ibd_defs THEN rtac ctxt (sbd0_Cinfinite RS conjunct1) 1);
traytel@60728
  1770
        val set_bd_tacss = map (map (fn thm => fn ctxt => rtac ctxt thm 1)) (transpose Iset_bd_thmss);
traytel@60728
  1771
        val le_rel_OO_tacs = map (fn i => fn ctxt =>
traytel@60728
  1772
          (rtac ctxt @{thm predicate2I} THEN' etac ctxt (le_Irel_OO_thm RS mk_conjunctN n i RS mp)) 1) ks;
traytel@54841
  1773
traytel@60728
  1774
        val rel_OO_Grp_tacs = map (fn def => fn ctxt => rtac ctxt def 1) Irel_unabs_defs;
traytel@54841
  1775
traytel@62324
  1776
        val pred_set_tacs = map (fn def => fn ctxt => rtac ctxt def 1) Ipred_unabs_defs;
traytel@62324
  1777
traytel@62324
  1778
        val tacss = @{map 10} zip_axioms map_id0_tacs map_comp0_tacs map_cong0_tacs set_map0_tacss
traytel@62324
  1779
          bd_co_tacs bd_cinf_tacs set_bd_tacss le_rel_OO_tacs rel_OO_Grp_tacs pred_set_tacs;
traytel@54841
  1780
traytel@55197
  1781
        fun wit_tac ctxt = unfold_thms_tac ctxt (flat Iwit_defss) THEN
traytel@54841
  1782
          mk_wit_tac ctxt n (flat ctor_Iset_thmss) (maps wit_thms_of_bnf bnfs);
traytel@54841
  1783
traytel@54841
  1784
        val (Ibnfs, lthy) =
traytel@62324
  1785
          @{fold_map 6} (fn tacs => fn map_b => fn rel_b => fn pred_b => fn set_bs => fn consts =>
traytel@56016
  1786
            bnf_def Do_Inline (user_policy Note_Some) false I tacs wit_tac (SOME deads)
traytel@62324
  1787
              map_b rel_b pred_b set_bs consts)
traytel@62324
  1788
          tacss map_bs rel_bs pred_bs set_bss
traytel@62324
  1789
            (((((((replicate n Binding.empty ~~ Ts) ~~ Imaps) ~~ Isetss_by_bnf) ~~ Ibds) ~~
traytel@62324
  1790
              Iwitss) ~~ map SOME Irels) ~~ map SOME Ipreds) lthy;
traytel@54841
  1791
traytel@54841
  1792
        val timer = time (timer "registered new datatypes as BNFs");
blanchet@48975
  1793
blanchet@48975
  1794
        val ls' = if m = 1 then [0] else ls
traytel@49109
  1795
traytel@49109
  1796
        val Ibnf_common_notes =
traytel@54841
  1797
          [(ctor_map_uniqueN, [ctor_Imap_unique_thm])]
traytel@49109
  1798
          |> map (fn (thmN, thms) =>
traytel@49109
  1799
            ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
traytel@49109
  1800
traytel@49109
  1801
        val Ibnf_notes =
traytel@54841
  1802
          [(ctor_mapN, map single ctor_Imap_thms),
blanchet@49541
  1803
          (ctor_relN, map single ctor_Irel_thms),
traytel@54841
  1804
          (ctor_set_inclN, ctor_Iset_incl_thmss),
traytel@54841
  1805
          (ctor_set_set_inclN, map flat ctor_set_Iset_incl_thmsss)] @
traytel@54841
  1806
          map2 (fn i => fn thms => (mk_ctor_setN i, map single thms)) ls' ctor_Iset_thmss
traytel@49109
  1807
          |> maps (fn (thmN, thmss) =>
traytel@49109
  1808
            map2 (fn b => fn thms =>
traytel@49109
  1809
              ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
traytel@49109
  1810
            bs thmss)
blanchet@48975
  1811
      in
traytel@62863
  1812
        (timer, Ibnfs, (ctor_Imap_o_thms, ctor_Imap_thms), ctor_Imap_unique_thm, ctor_Iset_thmss',
traytel@53567
  1813
          ctor_Irel_thms, Ibnf_common_notes @ Ibnf_notes, lthy)
blanchet@48975
  1814
      end;
blanchet@48975
  1815
traytel@61272
  1816
    val ((((((xFs, yFs)), Iphis), activephis), activeIphis), _) =
traytel@61272
  1817
      lthy
traytel@61272
  1818
      |> mk_Frees "x" FTs
traytel@61272
  1819
      ||>> mk_Frees "y" FTs'
traytel@61272
  1820
      ||>> mk_Frees "R" IphiTs
traytel@61272
  1821
      ||>> mk_Frees "S" activephiTs
traytel@61272
  1822
      ||>> mk_Frees "IR" activeIphiTs;
traytel@61272
  1823
traytel@62827
  1824
    val ctor_fold_o_Imap_thms = mk_xtor_co_iter_o_map_thms Least_FP false m ctor_fold_unique_thm
blanchet@57631
  1825
      ctor_Imap_o_thms (map (mk_pointfree lthy) ctor_fold_thms) sym_map_comps map_cong0s;
traytel@52913
  1826
blanchet@57631
  1827
    val Irels = if m = 0 then map HOLogic.eq_const Ts
blanchet@57631
  1828
      else map (mk_rel_of_bnf deads passiveAs passiveBs) Ibnfs;
blanchet@57631
  1829
    val Irel_induct_thm =
desharna@58579
  1830
      mk_xtor_rel_co_induct_thm Least_FP rels activeIphis Irels Iphis xFs yFs ctors ctor's
blanchet@57631
  1831
        (fn {context = ctxt, prems = IHs} => mk_rel_induct_tac ctxt IHs m ctor_induct2_thm ks
desharna@57967
  1832
           ctor_Irel_thms rel_mono_strong0s) lthy;
traytel@51918
  1833
blanchet@57631
  1834
    val rels = map2 (fn Ds => mk_rel_of_bnf Ds allAs allBs') Dss bnfs;
blanchet@57631
  1835
    val ctor_fold_transfer_thms =
traytel@62827
  1836
      mk_xtor_co_iter_transfer_thms Least_FP rels activephis activephis Irels Iphis
blanchet@57631
  1837
        (mk_folds passiveAs activeAs) (mk_folds passiveBs activeBs)
blanchet@57631
  1838
        (fn {context = ctxt, prems = _} => mk_fold_transfer_tac ctxt m Irel_induct_thm
blanchet@57631
  1839
          (map map_transfer_of_bnf bnfs) ctor_fold_thms)
blanchet@57631
  1840
        lthy;
traytel@52731
  1841
traytel@62905
  1842
    val timer = time (timer "relator induction");
traytel@58443
  1843
traytel@62905
  1844
    fun mk_Ts As = map (typ_subst_atomic (passiveAs ~~ As)) Ts;
traytel@62905
  1845
    val export = map (Morphism.term (Local_Theory.target_morphism lthy))
traytel@62905
  1846
    val ((recs, (ctor_rec_thms, ctor_rec_unique_thm, ctor_rec_o_Imap_thms, ctor_rec_transfer_thms)),
traytel@62905
  1847
        lthy) = lthy
traytel@62905
  1848
      |> derive_xtor_co_recs Least_FP external_bs mk_Ts (Dss, resDs) bnfs
traytel@62905
  1849
        (export ctors) (export folds)
traytel@62905
  1850
        ctor_fold_unique_thm ctor_fold_thms ctor_fold_transfer_thms ctor_Imap_thms ctor_Irel_thms;
traytel@62905
  1851
traytel@62905
  1852
    val timer = time (timer "recursor");
traytel@51918
  1853
blanchet@57631
  1854
    val common_notes =
blanchet@57631
  1855
      [(ctor_inductN, [ctor_induct_thm]),
blanchet@57631
  1856
      (ctor_induct2N, [ctor_induct2_thm]),
blanchet@57631
  1857
      (ctor_rel_inductN, [Irel_induct_thm])]
blanchet@57631
  1858
      |> map (fn (thmN, thms) =>
blanchet@57631
  1859
        ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]));
traytel@49109
  1860
blanchet@57631
  1861
    val notes =
blanchet@57631
  1862
      [(ctor_dtorN, ctor_dtor_thms),
blanchet@57631
  1863
      (ctor_exhaustN, ctor_exhaust_thms),
blanchet@57631
  1864
      (ctor_foldN, ctor_fold_thms),
blanchet@59856
  1865
      (ctor_fold_o_mapN, ctor_fold_o_Imap_thms),
blanchet@59856
  1866
      (ctor_fold_transferN, ctor_fold_transfer_thms),
blanchet@57631
  1867
      (ctor_fold_uniqueN, ctor_fold_unique_thms),
blanchet@57631
  1868
      (ctor_injectN, ctor_inject_thms),
blanchet@57631
  1869
      (dtor_ctorN, dtor_ctor_thms),
blanchet@57631
  1870
      (dtor_exhaustN, dtor_exhaust_thms),
blanchet@57631
  1871
      (dtor_injectN, dtor_inject_thms)]
blanchet@57631
  1872
      |> map (apsnd (map single))
blanchet@57631
  1873
      |> maps (fn (thmN, thmss) =>
blanchet@57631
  1874
        map2 (fn b => fn thms =>
blanchet@57631
  1875
          ((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])]))
blanchet@57631
  1876
        bs thmss);
traytel@53568
  1877
wenzelm@62093
  1878
    val lthy' = lthy |> internals ? snd o Local_Theory.notes (common_notes @ notes @ Ibnf_notes);
blanchet@57631
  1879
blanchet@57631
  1880
    val fp_res =
traytel@62684
  1881
      {Ts = Ts, bnfs = Ibnfs, pre_bnfs = bnfs, absT_infos = absT_infos,
traytel@62905
  1882
       ctors = ctors, dtors = dtors, xtor_un_folds_legacy = folds, xtor_co_recs = export recs,
traytel@62684
  1883
       xtor_co_induct = ctor_induct_thm, dtor_ctors = dtor_ctor_thms,
blanchet@59819
  1884
       ctor_dtors = ctor_dtor_thms, ctor_injects = ctor_inject_thms,
blanchet@59856
  1885
       dtor_injects = dtor_inject_thms, xtor_maps = ctor_Imap_thms,
traytel@62863
  1886
       xtor_map_unique = ctor_Imap_unique_thm, xtor_setss = ctor_Iset_thmss',
blanchet@62721
  1887
       xtor_rels = ctor_Irel_thms, xtor_un_fold_thms_legacy = ctor_fold_thms,
traytel@62863
  1888
       xtor_co_rec_thms = ctor_rec_thms, xtor_un_fold_unique_legacy = ctor_fold_unique_thm,
traytel@62863
  1889
       xtor_co_rec_unique = ctor_rec_unique_thm,
blanchet@62721
  1890
       xtor_un_fold_o_maps_legacy = ctor_fold_o_Imap_thms,
blanchet@62721
  1891
       xtor_co_rec_o_maps = ctor_rec_o_Imap_thms,
blanchet@62721
  1892
       xtor_un_fold_transfers_legacy = ctor_fold_transfer_thms,
blanchet@59856
  1893
       xtor_co_rec_transfers = ctor_rec_transfer_thms, xtor_rel_co_induct = Irel_induct_thm,
blanchet@59856
  1894
       dtor_set_inducts = []};
blanchet@48975
  1895
  in
blanchet@57631
  1896
    timer; (fp_res, lthy')
blanchet@48975
  1897
  end;
blanchet@48975
  1898
blanchet@48975
  1899
val _ =
wenzelm@59936
  1900
  Outer_Syntax.local_theory @{command_keyword datatype} "define inductive datatypes"
blanchet@58305
  1901
    (parse_co_datatype_cmd Least_FP construct_lfp);
blanchet@58305
  1902
blanchet@58256
  1903
val _ = Theory.setup (fp_antiquote_setup @{binding datatype});
blanchet@58256
  1904
blanchet@48975
  1905
end;