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