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