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