src/HOL/Tools/datatype_prop.ML
author wenzelm
Thu Mar 27 14:41:09 2008 +0100 (2008-03-27)
changeset 26424 a6cad32a27b0
parent 25154 6155f2faf23e
child 26969 cf3f998d0631
permissions -rw-r--r--
eliminated theory ProtoPure;
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_prop.ML
berghofe@5177
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@5177
     4
wenzelm@11539
     5
Characteristic properties of datatypes.
berghofe@5177
     6
*)
berghofe@5177
     7
berghofe@5177
     8
signature DATATYPE_PROP =
berghofe@5177
     9
sig
wenzelm@24112
    10
  val distinctness_limit : int Config.T
wenzelm@24098
    11
  val distinctness_limit_setup : theory -> theory
wenzelm@8434
    12
  val indexify_names: string list -> string list
berghofe@13465
    13
  val make_tnames: typ list -> string list
berghofe@13641
    14
  val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
berghofe@13641
    15
  val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
berghofe@13641
    16
  val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
berghofe@15459
    17
  val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
berghofe@15459
    18
    string list -> typ list * typ list
berghofe@13641
    19
  val make_primrecs : string list -> DatatypeAux.descr list ->
berghofe@13641
    20
    (string * sort) list -> theory -> term list
berghofe@13641
    21
  val make_cases : string list -> DatatypeAux.descr list ->
berghofe@13641
    22
    (string * sort) list -> theory -> term list list
berghofe@13641
    23
  val make_distincts : string list -> DatatypeAux.descr list ->
berghofe@13641
    24
    (string * sort) list -> theory -> term list list
berghofe@13641
    25
  val make_splits : string list -> DatatypeAux.descr list ->
berghofe@13641
    26
    (string * sort) list -> theory -> (term * term) list
berghofe@13641
    27
  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    28
    (string * sort) list -> theory -> term list
berghofe@13641
    29
  val make_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    30
    (string * sort) list -> theory -> term list
berghofe@13641
    31
  val make_nchotomys : DatatypeAux.descr list ->
berghofe@13641
    32
    (string * sort) list -> term list
berghofe@5177
    33
end;
berghofe@5177
    34
berghofe@5177
    35
structure DatatypeProp : DATATYPE_PROP =
berghofe@5177
    36
struct
berghofe@5177
    37
berghofe@5177
    38
open DatatypeAux;
berghofe@5177
    39
berghofe@5177
    40
(*the kind of distinctiveness axioms depends on number of constructors*)
wenzelm@24098
    41
val (distinctness_limit, distinctness_limit_setup) =
wenzelm@24112
    42
  Attrib.config_int "datatype_distinctness_limit" 7;
berghofe@5177
    43
wenzelm@8434
    44
fun indexify_names names =
wenzelm@8434
    45
  let
wenzelm@8434
    46
    fun index (x :: xs) tab =
haftmann@17521
    47
      (case AList.lookup (op =) tab x of
haftmann@25154
    48
        NONE => if member (op =) xs x then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
haftmann@25154
    49
      | SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab))
wenzelm@8434
    50
    | index [] _ = [];
wenzelm@8434
    51
  in index names [] end;
wenzelm@8434
    52
berghofe@5177
    53
fun make_tnames Ts =
berghofe@5177
    54
  let
berghofe@5177
    55
    fun type_name (TFree (name, _)) = implode (tl (explode name))
berghofe@5177
    56
      | type_name (Type (name, _)) = 
berghofe@5177
    57
          let val name' = Sign.base_name name
wenzelm@8434
    58
          in if Syntax.is_identifier name' then name' else "x" end;
wenzelm@8434
    59
  in indexify_names (map type_name Ts) end;
berghofe@5177
    60
berghofe@5177
    61
berghofe@5177
    62
(************************* injectivity of constructors ************************)
berghofe@5177
    63
berghofe@5177
    64
fun make_injs descr sorts =
berghofe@5177
    65
  let
haftmann@21078
    66
    val descr' = flat descr;
haftmann@21078
    67
    fun make_inj T (cname, cargs) =
haftmann@21078
    68
      if null cargs then I else
berghofe@5177
    69
        let
berghofe@5177
    70
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
    71
          val constr_t = Const (cname, Ts ---> T);
berghofe@5177
    72
          val tnames = make_tnames Ts;
berghofe@5177
    73
          val frees = map Free (tnames ~~ Ts);
berghofe@5177
    74
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
haftmann@21078
    75
        in cons (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
    76
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
berghofe@5177
    77
           foldr1 (HOLogic.mk_binop "op &")
haftmann@21078
    78
             (map HOLogic.mk_eq (frees ~~ frees')))))
berghofe@5177
    79
        end;
haftmann@21078
    80
  in
haftmann@21078
    81
    map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) [])
haftmann@21078
    82
      (hd descr) (Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
    83
  end;
berghofe@5177
    84
berghofe@5177
    85
(********************************* induction **********************************)
berghofe@5177
    86
berghofe@5177
    87
fun make_ind descr sorts =
berghofe@5177
    88
  let
skalberg@15570
    89
    val descr' = List.concat descr;
berghofe@5177
    90
    val recTs = get_rec_types descr' sorts;
berghofe@5177
    91
    val pnames = if length descr' = 1 then ["P"]
berghofe@5177
    92
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
berghofe@5177
    93
berghofe@5177
    94
    fun make_pred i T =
berghofe@5177
    95
      let val T' = T --> HOLogic.boolT
skalberg@15570
    96
      in Free (List.nth (pnames, i), T') end;
berghofe@5177
    97
berghofe@5177
    98
    fun make_ind_prem k T (cname, cargs) =
berghofe@5177
    99
      let
berghofe@13641
   100
        fun mk_prem ((dt, s), T) =
berghofe@13641
   101
          let val (Us, U) = strip_type T
berghofe@13641
   102
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
berghofe@13641
   103
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
berghofe@13641
   104
          end;
berghofe@7015
   105
skalberg@15570
   106
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   107
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   108
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
wenzelm@20071
   109
        val tnames = Name.variant_list pnames (make_tnames Ts);
skalberg@15570
   110
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   111
        val frees = tnames ~~ Ts;
berghofe@7015
   112
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@5177
   113
berghofe@5177
   114
      in list_all_free (frees, Logic.list_implies (prems,
berghofe@5177
   115
        HOLogic.mk_Trueprop (make_pred k T $ 
berghofe@5177
   116
          list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@5177
   117
      end;
berghofe@5177
   118
skalberg@15570
   119
    val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
berghofe@5177
   120
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
berghofe@5177
   121
    val tnames = make_tnames recTs;
berghofe@5177
   122
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
   123
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
berghofe@5177
   124
        (descr' ~~ recTs ~~ tnames)))
berghofe@5177
   125
berghofe@5177
   126
  in Logic.list_implies (prems, concl) end;
berghofe@5177
   127
berghofe@5177
   128
(******************************* case distinction *****************************)
berghofe@5177
   129
berghofe@5177
   130
fun make_casedists descr sorts =
berghofe@5177
   131
  let
skalberg@15570
   132
    val descr' = List.concat descr;
berghofe@5177
   133
berghofe@5177
   134
    fun make_casedist_prem T (cname, cargs) =
berghofe@5177
   135
      let
berghofe@5177
   136
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   137
        val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
berghofe@5177
   138
        val free_ts = map Free frees
berghofe@5177
   139
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
berghofe@5177
   140
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
berghofe@5177
   141
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
berghofe@5177
   142
      end;
berghofe@5177
   143
berghofe@5177
   144
    fun make_casedist ((_, (_, _, constrs)), T) =
berghofe@5177
   145
      let val prems = map (make_casedist_prem T) constrs
berghofe@5177
   146
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
berghofe@5177
   147
      end
berghofe@5177
   148
berghofe@5177
   149
  in map make_casedist
skalberg@15570
   150
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
   151
  end;
berghofe@5177
   152
berghofe@5177
   153
(*************** characteristic equations for primrec combinator **************)
berghofe@5177
   154
berghofe@15459
   155
fun make_primrec_Ts descr sorts used =
berghofe@5177
   156
  let
skalberg@15570
   157
    val descr' = List.concat descr;
berghofe@5177
   158
wenzelm@20071
   159
    val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
wenzelm@12338
   160
      replicate (length descr') HOLogic.typeS);
berghofe@5177
   161
skalberg@15570
   162
    val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   163
      map (fn (_, cargs) =>
berghofe@5177
   164
        let
berghofe@7015
   165
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
skalberg@15570
   166
          val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
berghofe@7015
   167
berghofe@13641
   168
          fun mk_argT (dt, T) =
skalberg@15570
   169
            binder_types T ---> List.nth (rec_result_Ts, body_index dt);
berghofe@7015
   170
berghofe@7015
   171
          val argTs = Ts @ map mk_argT recs
skalberg@15570
   172
        in argTs ---> List.nth (rec_result_Ts, i)
berghofe@5177
   173
        end) constrs) descr');
berghofe@5177
   174
berghofe@15459
   175
  in (rec_result_Ts, reccomb_fn_Ts) end;
berghofe@15459
   176
berghofe@15459
   177
fun make_primrecs new_type_names descr sorts thy =
berghofe@15459
   178
  let
skalberg@15570
   179
    val descr' = List.concat descr;
berghofe@15459
   180
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   181
    val used = foldr add_typ_tfree_names [] recTs;
berghofe@15459
   182
berghofe@15459
   183
    val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
berghofe@15459
   184
berghofe@5177
   185
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   186
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@5177
   187
berghofe@5177
   188
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
wenzelm@22578
   189
    val reccomb_names = map (Sign.intern_const thy)
berghofe@5177
   190
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   191
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   192
          (1 upto (length descr'))));
berghofe@5177
   193
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   194
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   195
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   196
berghofe@5177
   197
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
berghofe@5177
   198
      let
skalberg@15570
   199
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   200
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   201
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   202
        val tnames = make_tnames Ts;
skalberg@15570
   203
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   204
        val frees = map Free (tnames ~~ Ts);
berghofe@5177
   205
        val frees' = map Free (rec_tnames ~~ recTs');
berghofe@7015
   206
berghofe@13641
   207
        fun mk_reccomb ((dt, T), t) =
berghofe@13641
   208
          let val (Us, U) = strip_type T
berghofe@13641
   209
          in list_abs (map (pair "x") Us,
skalberg@15570
   210
            List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
berghofe@13641
   211
          end;
berghofe@7015
   212
berghofe@13641
   213
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
berghofe@5177
   214
berghofe@5177
   215
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   216
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@13641
   217
         list_comb (f, frees @ reccombs')))], fs)
berghofe@5177
   218
      end
berghofe@5177
   219
skalberg@15570
   220
  in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
skalberg@15570
   221
    Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
berghofe@5177
   222
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
berghofe@5177
   223
  end;
berghofe@5177
   224
berghofe@5177
   225
(****************** make terms of form  t_case f1 ... fn  *********************)
berghofe@5177
   226
berghofe@5177
   227
fun make_case_combs new_type_names descr sorts thy fname =
berghofe@5177
   228
  let
skalberg@15570
   229
    val descr' = List.concat descr;
berghofe@5177
   230
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   231
    val used = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   232
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   233
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
berghofe@5177
   234
berghofe@5177
   235
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   236
      map (fn (_, cargs) =>
berghofe@5177
   237
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   238
        in Ts ---> T' end) constrs) (hd descr);
berghofe@5177
   239
berghofe@5177
   240
    val case_names = map (fn s =>
wenzelm@22578
   241
      Sign.intern_const thy (s ^ "_case")) new_type_names
berghofe@5177
   242
  in
berghofe@5177
   243
    map (fn ((name, Ts), T) => list_comb
berghofe@5177
   244
      (Const (name, Ts @ [T] ---> T'),
berghofe@5177
   245
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
berghofe@5177
   246
          (case_names ~~ case_fn_Ts ~~ newTs)
berghofe@5177
   247
  end;
berghofe@5177
   248
berghofe@5177
   249
(**************** characteristic equations for case combinator ****************)
berghofe@5177
   250
berghofe@5177
   251
fun make_cases new_type_names descr sorts thy =
berghofe@5177
   252
  let
skalberg@15570
   253
    val descr' = List.concat descr;
berghofe@5177
   254
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   255
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   256
berghofe@5177
   257
    fun make_case T comb_t ((cname, cargs), f) =
berghofe@5177
   258
      let
berghofe@5177
   259
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   260
        val frees = map Free ((make_tnames Ts) ~~ Ts)
berghofe@5177
   261
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   262
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@5177
   263
         list_comb (f, frees)))
berghofe@5177
   264
      end
berghofe@5177
   265
berghofe@5177
   266
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
berghofe@5177
   267
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
berghofe@5177
   268
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   269
  end;
berghofe@5177
   270
berghofe@5177
   271
(************************* distinctness of constructors ***********************)
berghofe@5177
   272
berghofe@5177
   273
fun make_distincts new_type_names descr sorts thy =
berghofe@5177
   274
  let
skalberg@15570
   275
    val descr' = List.concat descr;
berghofe@5177
   276
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   277
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   278
wenzelm@24098
   279
    (**** number of constructors < distinctness_limit : C_i ... ~= C_j ... ****)
berghofe@5177
   280
berghofe@5177
   281
    fun make_distincts_1 _ [] = []
berghofe@5177
   282
      | make_distincts_1 T ((cname, cargs)::constrs) =
berghofe@5177
   283
          let
berghofe@5177
   284
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   285
            val frees = map Free ((make_tnames Ts) ~~ Ts);
berghofe@5177
   286
            val t = list_comb (Const (cname, Ts ---> T), frees);
berghofe@5177
   287
berghofe@5177
   288
            fun make_distincts' [] = []
berghofe@5177
   289
              | make_distincts' ((cname', cargs')::constrs') =
berghofe@5177
   290
                  let
berghofe@5177
   291
                    val Ts' = map (typ_of_dtyp descr' sorts) cargs';
berghofe@5177
   292
                    val frees' = map Free ((map ((op ^) o (rpair "'"))
berghofe@5177
   293
                      (make_tnames Ts')) ~~ Ts');
berghofe@5177
   294
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
berghofe@5177
   295
                  in
wenzelm@8434
   296
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
wenzelm@8434
   297
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
berghofe@5177
   298
                      (make_distincts' constrs')
berghofe@5177
   299
                  end
berghofe@5177
   300
berghofe@5177
   301
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
berghofe@5177
   302
          end;
berghofe@5177
   303
berghofe@5177
   304
  in map (fn (((_, (_, _, constrs)), T), tname) =>
wenzelm@24112
   305
      if length constrs < Config.get_thy thy distinctness_limit
wenzelm@24098
   306
      then make_distincts_1 T constrs else [])
berghofe@5177
   307
        ((hd descr) ~~ newTs ~~ new_type_names)
berghofe@5177
   308
  end;
berghofe@5177
   309
berghofe@7015
   310
berghofe@5177
   311
(*************************** the "split" - equations **************************)
berghofe@5177
   312
berghofe@5177
   313
fun make_splits new_type_names descr sorts thy =
berghofe@5177
   314
  let
skalberg@15570
   315
    val descr' = List.concat descr;
berghofe@5177
   316
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   317
    val used' = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   318
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   319
    val T' = TFree (Name.variant used' "'t", HOLogic.typeS);
berghofe@5177
   320
    val P = Free ("P", T' --> HOLogic.boolT);
berghofe@5177
   321
berghofe@5177
   322
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
berghofe@5177
   323
      let
berghofe@5177
   324
        val (_, fs) = strip_comb comb_t;
berghofe@5177
   325
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
berghofe@5177
   326
berghofe@5177
   327
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
berghofe@5177
   328
          let
berghofe@5177
   329
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   330
            val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts);
berghofe@5177
   331
            val eqn = HOLogic.mk_eq (Free ("x", T),
berghofe@5177
   332
              list_comb (Const (cname, Ts ---> T), frees));
berghofe@5177
   333
            val P' = P $ list_comb (f, frees)
skalberg@15574
   334
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
skalberg@15574
   335
                (HOLogic.imp $ eqn $ P') frees)::t1s,
skalberg@15574
   336
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
skalberg@15574
   337
                (HOLogic.conj $ eqn $ (HOLogic.Not $ P')) frees)::t2s)
berghofe@5177
   338
          end;
berghofe@5177
   339
skalberg@15574
   340
        val (t1s, t2s) = foldr process_constr ([], []) (constrs ~~ fs);
berghofe@5177
   341
        val lhs = P $ (comb_t $ Free ("x", T))
berghofe@5177
   342
      in
berghofe@5177
   343
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
wenzelm@8434
   344
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
berghofe@5177
   345
      end
berghofe@5177
   346
berghofe@5177
   347
  in map make_split ((hd descr) ~~ newTs ~~
berghofe@5177
   348
    (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   349
  end;
berghofe@5177
   350
berghofe@5177
   351
(************************* additional rules for TFL ***************************)
berghofe@5177
   352
nipkow@8601
   353
fun make_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   354
  let
nipkow@8601
   355
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
nipkow@8601
   356
nipkow@8601
   357
    fun mk_case_cong comb =
nipkow@8601
   358
      let 
nipkow@8601
   359
        val Type ("fun", [T, _]) = fastype_of comb;
nipkow@8601
   360
        val M = Free ("M", T);
nipkow@8601
   361
        val M' = Free ("M'", T);
nipkow@8601
   362
      in
nipkow@8601
   363
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
nipkow@8601
   364
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
nipkow@8601
   365
      end
nipkow@8601
   366
  in
nipkow@8601
   367
    map mk_case_cong case_combs
nipkow@8601
   368
  end;
nipkow@8601
   369
 
nipkow@8601
   370
berghofe@5177
   371
(*---------------------------------------------------------------------------
berghofe@5177
   372
 * Structure of case congruence theorem looks like this:
berghofe@5177
   373
 *
berghofe@5177
   374
 *    (M = M') 
berghofe@5177
   375
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
berghofe@5177
   376
 *    ==> ... 
berghofe@5177
   377
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
berghofe@5177
   378
 *    ==>
berghofe@5177
   379
 *      (ty_case f1..fn M = ty_case g1..gn M')
berghofe@5177
   380
 *---------------------------------------------------------------------------*)
berghofe@5177
   381
berghofe@5177
   382
fun make_case_congs new_type_names descr sorts thy =
berghofe@5177
   383
  let
berghofe@5177
   384
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
berghofe@5177
   385
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
berghofe@5177
   386
berghofe@5177
   387
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
berghofe@5177
   388
      let
berghofe@5177
   389
        val Type ("fun", [T, _]) = fastype_of comb;
berghofe@5177
   390
        val (_, fs) = strip_comb comb;
berghofe@5177
   391
        val (_, gs) = strip_comb comb';
berghofe@5177
   392
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
berghofe@5177
   393
        val M = Free ("M", T);
berghofe@5177
   394
        val M' = Free ("M'", T);
berghofe@5177
   395
berghofe@5177
   396
        fun mk_clause ((f, g), (cname, _)) =
berghofe@5177
   397
          let
berghofe@5177
   398
            val (Ts, _) = strip_type (fastype_of f);
wenzelm@20071
   399
            val tnames = Name.variant_list used (make_tnames Ts);
berghofe@5177
   400
            val frees = map Free (tnames ~~ Ts)
berghofe@5177
   401
          in
berghofe@5177
   402
            list_all_free (tnames ~~ Ts, Logic.mk_implies
berghofe@5177
   403
              (HOLogic.mk_Trueprop
berghofe@5177
   404
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
berghofe@5177
   405
               HOLogic.mk_Trueprop
berghofe@5177
   406
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
berghofe@5177
   407
          end
berghofe@5177
   408
berghofe@5177
   409
      in
berghofe@5177
   410
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
berghofe@5177
   411
          map mk_clause (fs ~~ gs ~~ constrs),
berghofe@5177
   412
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
berghofe@5177
   413
      end
berghofe@5177
   414
berghofe@5177
   415
  in
berghofe@5177
   416
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
berghofe@5177
   417
  end;
berghofe@5177
   418
berghofe@5177
   419
(*---------------------------------------------------------------------------
berghofe@5177
   420
 * Structure of exhaustion theorem looks like this:
berghofe@5177
   421
 *
berghofe@5177
   422
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
berghofe@5177
   423
 *---------------------------------------------------------------------------*)
berghofe@5177
   424
berghofe@5177
   425
fun make_nchotomys descr sorts =
berghofe@5177
   426
  let
skalberg@15570
   427
    val descr' = List.concat descr;
berghofe@5177
   428
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   429
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   430
berghofe@5177
   431
    fun mk_eqn T (cname, cargs) =
berghofe@5177
   432
      let
berghofe@5177
   433
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   434
        val tnames = Name.variant_list ["v"] (make_tnames Ts);
berghofe@5177
   435
        val frees = tnames ~~ Ts
berghofe@5177
   436
      in
skalberg@15574
   437
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
skalberg@15574
   438
          (HOLogic.mk_eq (Free ("v", T),
skalberg@15574
   439
            list_comb (Const (cname, Ts ---> T), map Free frees))) frees
berghofe@5177
   440
      end
berghofe@5177
   441
berghofe@5177
   442
  in map (fn ((_, (_, _, constrs)), T) =>
berghofe@5177
   443
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
berghofe@5177
   444
      (hd descr ~~ newTs)
berghofe@5177
   445
  end;
berghofe@5177
   446
berghofe@5177
   447
end;