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