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