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