src/HOL/Tools/datatype_prop.ML
author paulson
Tue May 22 17:56:06 2007 +0200 (2007-05-22)
changeset 23075 69e30a7e8880
parent 22994 02440636214f
child 24098 f1eb34ae33af
permissions -rw-r--r--
Some hacks for SPASS format
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
berghofe@7015
    10
  val dtK : int ref
wenzelm@8434
    11
  val indexify_names: string list -> string list
berghofe@13465
    12
  val make_tnames: typ list -> string list
berghofe@13641
    13
  val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
berghofe@13641
    14
  val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
berghofe@13641
    15
  val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
berghofe@15459
    16
  val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
berghofe@15459
    17
    string list -> typ list * typ list
berghofe@13641
    18
  val make_primrecs : string list -> DatatypeAux.descr list ->
berghofe@13641
    19
    (string * sort) list -> theory -> term list
berghofe@13641
    20
  val make_cases : string list -> DatatypeAux.descr list ->
berghofe@13641
    21
    (string * sort) list -> theory -> term list list
berghofe@13641
    22
  val make_distincts : string list -> DatatypeAux.descr list ->
berghofe@13641
    23
    (string * sort) list -> theory -> term list list
berghofe@13641
    24
  val make_splits : string list -> DatatypeAux.descr list ->
berghofe@13641
    25
    (string * sort) list -> theory -> (term * term) list
berghofe@13641
    26
  val make_size : DatatypeAux.descr list -> (string * sort) list ->
berghofe@13641
    27
    theory -> term list
berghofe@13641
    28
  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    29
    (string * sort) list -> theory -> term list
berghofe@13641
    30
  val make_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    31
    (string * sort) list -> theory -> term list
berghofe@13641
    32
  val make_nchotomys : DatatypeAux.descr list ->
berghofe@13641
    33
    (string * sort) list -> term list
berghofe@5177
    34
end;
berghofe@5177
    35
berghofe@5177
    36
structure DatatypeProp : DATATYPE_PROP =
berghofe@5177
    37
struct
berghofe@5177
    38
berghofe@5177
    39
open DatatypeAux;
berghofe@5177
    40
berghofe@5177
    41
(*the kind of distinctiveness axioms depends on number of constructors*)
berghofe@7015
    42
val dtK = ref 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
skalberg@15531
    48
        NONE => if x mem xs then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
skalberg@15531
    49
      | SOME i => (x ^ Library.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
berghofe@5177
    63
(************************* injectivity of constructors ************************)
berghofe@5177
    64
berghofe@5177
    65
fun make_injs descr sorts =
berghofe@5177
    66
  let
haftmann@21078
    67
    val descr' = flat descr;
haftmann@21078
    68
    fun make_inj T (cname, cargs) =
haftmann@21078
    69
      if null cargs then I else
berghofe@5177
    70
        let
berghofe@5177
    71
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
    72
          val constr_t = Const (cname, Ts ---> T);
berghofe@5177
    73
          val tnames = make_tnames Ts;
berghofe@5177
    74
          val frees = map Free (tnames ~~ Ts);
berghofe@5177
    75
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
haftmann@21078
    76
        in cons (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
    77
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
berghofe@5177
    78
           foldr1 (HOLogic.mk_binop "op &")
haftmann@21078
    79
             (map HOLogic.mk_eq (frees ~~ frees')))))
berghofe@5177
    80
        end;
haftmann@21078
    81
  in
haftmann@21078
    82
    map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) [])
haftmann@21078
    83
      (hd descr) (Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
    84
  end;
berghofe@5177
    85
berghofe@5177
    86
(********************************* induction **********************************)
berghofe@5177
    87
berghofe@5177
    88
fun make_ind descr sorts =
berghofe@5177
    89
  let
skalberg@15570
    90
    val descr' = List.concat descr;
berghofe@5177
    91
    val recTs = get_rec_types descr' sorts;
berghofe@5177
    92
    val pnames = if length descr' = 1 then ["P"]
berghofe@5177
    93
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
berghofe@5177
    94
berghofe@5177
    95
    fun make_pred i T =
berghofe@5177
    96
      let val T' = T --> HOLogic.boolT
skalberg@15570
    97
      in Free (List.nth (pnames, i), T') end;
berghofe@5177
    98
berghofe@5177
    99
    fun make_ind_prem k T (cname, cargs) =
berghofe@5177
   100
      let
berghofe@13641
   101
        fun mk_prem ((dt, s), T) =
berghofe@13641
   102
          let val (Us, U) = strip_type T
berghofe@13641
   103
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
berghofe@13641
   104
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
berghofe@13641
   105
          end;
berghofe@7015
   106
skalberg@15570
   107
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   108
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   109
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
wenzelm@20071
   110
        val tnames = Name.variant_list pnames (make_tnames Ts);
skalberg@15570
   111
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   112
        val frees = tnames ~~ Ts;
berghofe@7015
   113
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@5177
   114
berghofe@5177
   115
      in list_all_free (frees, Logic.list_implies (prems,
berghofe@5177
   116
        HOLogic.mk_Trueprop (make_pred k T $ 
berghofe@5177
   117
          list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@5177
   118
      end;
berghofe@5177
   119
skalberg@15570
   120
    val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
berghofe@5177
   121
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
berghofe@5177
   122
    val tnames = make_tnames recTs;
berghofe@5177
   123
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
   124
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
berghofe@5177
   125
        (descr' ~~ recTs ~~ tnames)))
berghofe@5177
   126
berghofe@5177
   127
  in Logic.list_implies (prems, concl) end;
berghofe@5177
   128
berghofe@5177
   129
(******************************* case distinction *****************************)
berghofe@5177
   130
berghofe@5177
   131
fun make_casedists descr sorts =
berghofe@5177
   132
  let
skalberg@15570
   133
    val descr' = List.concat descr;
berghofe@5177
   134
berghofe@5177
   135
    fun make_casedist_prem T (cname, cargs) =
berghofe@5177
   136
      let
berghofe@5177
   137
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   138
        val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
berghofe@5177
   139
        val free_ts = map Free frees
berghofe@5177
   140
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
berghofe@5177
   141
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
berghofe@5177
   142
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
berghofe@5177
   143
      end;
berghofe@5177
   144
berghofe@5177
   145
    fun make_casedist ((_, (_, _, constrs)), T) =
berghofe@5177
   146
      let val prems = map (make_casedist_prem T) constrs
berghofe@5177
   147
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
berghofe@5177
   148
      end
berghofe@5177
   149
berghofe@5177
   150
  in map make_casedist
skalberg@15570
   151
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
   152
  end;
berghofe@5177
   153
berghofe@5177
   154
(*************** characteristic equations for primrec combinator **************)
berghofe@5177
   155
berghofe@15459
   156
fun make_primrec_Ts descr sorts used =
berghofe@5177
   157
  let
skalberg@15570
   158
    val descr' = List.concat descr;
berghofe@5177
   159
wenzelm@20071
   160
    val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
wenzelm@12338
   161
      replicate (length descr') HOLogic.typeS);
berghofe@5177
   162
skalberg@15570
   163
    val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   164
      map (fn (_, cargs) =>
berghofe@5177
   165
        let
berghofe@7015
   166
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
skalberg@15570
   167
          val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
berghofe@7015
   168
berghofe@13641
   169
          fun mk_argT (dt, T) =
skalberg@15570
   170
            binder_types T ---> List.nth (rec_result_Ts, body_index dt);
berghofe@7015
   171
berghofe@7015
   172
          val argTs = Ts @ map mk_argT recs
skalberg@15570
   173
        in argTs ---> List.nth (rec_result_Ts, i)
berghofe@5177
   174
        end) constrs) descr');
berghofe@5177
   175
berghofe@15459
   176
  in (rec_result_Ts, reccomb_fn_Ts) end;
berghofe@15459
   177
berghofe@15459
   178
fun make_primrecs new_type_names descr sorts thy =
berghofe@15459
   179
  let
skalberg@15570
   180
    val descr' = List.concat descr;
berghofe@15459
   181
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   182
    val used = foldr add_typ_tfree_names [] recTs;
berghofe@15459
   183
berghofe@15459
   184
    val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
berghofe@15459
   185
berghofe@5177
   186
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   187
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@5177
   188
berghofe@5177
   189
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
wenzelm@22578
   190
    val reccomb_names = map (Sign.intern_const thy)
berghofe@5177
   191
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   192
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   193
          (1 upto (length descr'))));
berghofe@5177
   194
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   195
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   196
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   197
berghofe@5177
   198
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
berghofe@5177
   199
      let
skalberg@15570
   200
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   201
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   202
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   203
        val tnames = make_tnames Ts;
skalberg@15570
   204
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   205
        val frees = map Free (tnames ~~ Ts);
berghofe@5177
   206
        val frees' = map Free (rec_tnames ~~ recTs');
berghofe@7015
   207
berghofe@13641
   208
        fun mk_reccomb ((dt, T), t) =
berghofe@13641
   209
          let val (Us, U) = strip_type T
berghofe@13641
   210
          in list_abs (map (pair "x") Us,
skalberg@15570
   211
            List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
berghofe@13641
   212
          end;
berghofe@7015
   213
berghofe@13641
   214
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
berghofe@5177
   215
berghofe@5177
   216
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   217
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@13641
   218
         list_comb (f, frees @ reccombs')))], fs)
berghofe@5177
   219
      end
berghofe@5177
   220
skalberg@15570
   221
  in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
skalberg@15570
   222
    Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
berghofe@5177
   223
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
berghofe@5177
   224
  end;
berghofe@5177
   225
berghofe@5177
   226
(****************** make terms of form  t_case f1 ... fn  *********************)
berghofe@5177
   227
berghofe@5177
   228
fun make_case_combs new_type_names descr sorts thy fname =
berghofe@5177
   229
  let
skalberg@15570
   230
    val descr' = List.concat descr;
berghofe@5177
   231
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   232
    val used = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   233
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   234
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
berghofe@5177
   235
berghofe@5177
   236
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   237
      map (fn (_, cargs) =>
berghofe@5177
   238
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   239
        in Ts ---> T' end) constrs) (hd descr);
berghofe@5177
   240
berghofe@5177
   241
    val case_names = map (fn s =>
wenzelm@22578
   242
      Sign.intern_const thy (s ^ "_case")) new_type_names
berghofe@5177
   243
  in
berghofe@5177
   244
    map (fn ((name, Ts), T) => list_comb
berghofe@5177
   245
      (Const (name, Ts @ [T] ---> T'),
berghofe@5177
   246
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
berghofe@5177
   247
          (case_names ~~ case_fn_Ts ~~ newTs)
berghofe@5177
   248
  end;
berghofe@5177
   249
berghofe@5177
   250
(**************** characteristic equations for case combinator ****************)
berghofe@5177
   251
berghofe@5177
   252
fun make_cases new_type_names descr sorts thy =
berghofe@5177
   253
  let
skalberg@15570
   254
    val descr' = List.concat descr;
berghofe@5177
   255
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   256
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   257
berghofe@5177
   258
    fun make_case T comb_t ((cname, cargs), f) =
berghofe@5177
   259
      let
berghofe@5177
   260
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   261
        val frees = map Free ((make_tnames Ts) ~~ Ts)
berghofe@5177
   262
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   263
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@5177
   264
         list_comb (f, frees)))
berghofe@5177
   265
      end
berghofe@5177
   266
berghofe@5177
   267
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
berghofe@5177
   268
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
berghofe@5177
   269
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   270
  end;
berghofe@5177
   271
berghofe@5177
   272
(************************* distinctness of constructors ***********************)
berghofe@5177
   273
berghofe@5177
   274
fun make_distincts new_type_names descr sorts thy =
berghofe@5177
   275
  let
skalberg@15570
   276
    val descr' = List.concat descr;
berghofe@5177
   277
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   278
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   279
berghofe@5177
   280
    (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
berghofe@5177
   281
berghofe@5177
   282
    fun make_distincts_1 _ [] = []
berghofe@5177
   283
      | make_distincts_1 T ((cname, cargs)::constrs) =
berghofe@5177
   284
          let
berghofe@5177
   285
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   286
            val frees = map Free ((make_tnames Ts) ~~ Ts);
berghofe@5177
   287
            val t = list_comb (Const (cname, Ts ---> T), frees);
berghofe@5177
   288
berghofe@5177
   289
            fun make_distincts' [] = []
berghofe@5177
   290
              | make_distincts' ((cname', cargs')::constrs') =
berghofe@5177
   291
                  let
berghofe@5177
   292
                    val Ts' = map (typ_of_dtyp descr' sorts) cargs';
berghofe@5177
   293
                    val frees' = map Free ((map ((op ^) o (rpair "'"))
berghofe@5177
   294
                      (make_tnames Ts')) ~~ Ts');
berghofe@5177
   295
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
berghofe@5177
   296
                  in
wenzelm@8434
   297
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
wenzelm@8434
   298
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
berghofe@5177
   299
                      (make_distincts' constrs')
berghofe@5177
   300
                  end
berghofe@5177
   301
berghofe@5177
   302
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
berghofe@5177
   303
          end;
berghofe@5177
   304
berghofe@5177
   305
  in map (fn (((_, (_, _, constrs)), T), tname) =>
berghofe@7015
   306
      if length constrs < !dtK 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
berghofe@5177
   352
(******************************* size functions *******************************)
berghofe@5177
   353
berghofe@9739
   354
fun make_size descr sorts thy =
berghofe@5177
   355
  let
haftmann@22994
   356
    val descr' = flat descr;
berghofe@5177
   357
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   358
haftmann@22994
   359
    val Const (size_name, _) = HOLogic.size_const dummyT;
berghofe@5177
   360
    val size_names = replicate (length (hd descr)) size_name @
wenzelm@22578
   361
      map (Sign.intern_const thy) (indexify_names
skalberg@15570
   362
        (map (fn T => name_of_typ T ^ "_size") (Library.drop (length (hd descr), recTs))));
berghofe@5177
   363
    val size_consts = map (fn (s, T) =>
berghofe@5177
   364
      Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
berghofe@5177
   365
haftmann@22994
   366
    fun plus (t1, t2) = Const ("HOL.plus_class.plus", HOLogic.natT --> HOLogic.natT --> HOLogic.natT) $ t1 $ t2;
berghofe@5177
   367
berghofe@5177
   368
    fun make_size_eqn size_const T (cname, cargs) =
berghofe@5177
   369
      let
haftmann@22994
   370
        val recs = filter is_rec_type cargs;
berghofe@5177
   371
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   372
        val recTs = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   373
        val tnames = make_tnames Ts;
haftmann@22994
   374
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
haftmann@22994
   375
        val ts = map (fn ((r, s), T) => nth size_consts (dest_DtRec r) $
berghofe@5177
   376
          Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
berghofe@5177
   377
        val t = if ts = [] then HOLogic.zero else
haftmann@21621
   378
          foldl1 plus (ts @ [HOLogic.Suc_zero])
berghofe@5177
   379
      in
berghofe@5177
   380
        HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
berghofe@5177
   381
          list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
berghofe@5177
   382
      end
berghofe@5177
   383
berghofe@5177
   384
  in
skalberg@15570
   385
    List.concat (map (fn (((_, (_, _, constrs)), size_const), T) =>
berghofe@5177
   386
      map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
berghofe@5177
   387
  end;
berghofe@5177
   388
berghofe@5177
   389
(************************* additional rules for TFL ***************************)
berghofe@5177
   390
nipkow@8601
   391
fun make_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   392
  let
nipkow@8601
   393
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
nipkow@8601
   394
nipkow@8601
   395
    fun mk_case_cong comb =
nipkow@8601
   396
      let 
nipkow@8601
   397
        val Type ("fun", [T, _]) = fastype_of comb;
nipkow@8601
   398
        val M = Free ("M", T);
nipkow@8601
   399
        val M' = Free ("M'", T);
nipkow@8601
   400
      in
nipkow@8601
   401
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
nipkow@8601
   402
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
nipkow@8601
   403
      end
nipkow@8601
   404
  in
nipkow@8601
   405
    map mk_case_cong case_combs
nipkow@8601
   406
  end;
nipkow@8601
   407
 
nipkow@8601
   408
berghofe@5177
   409
(*---------------------------------------------------------------------------
berghofe@5177
   410
 * Structure of case congruence theorem looks like this:
berghofe@5177
   411
 *
berghofe@5177
   412
 *    (M = M') 
berghofe@5177
   413
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
berghofe@5177
   414
 *    ==> ... 
berghofe@5177
   415
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
berghofe@5177
   416
 *    ==>
berghofe@5177
   417
 *      (ty_case f1..fn M = ty_case g1..gn M')
berghofe@5177
   418
 *---------------------------------------------------------------------------*)
berghofe@5177
   419
berghofe@5177
   420
fun make_case_congs new_type_names descr sorts thy =
berghofe@5177
   421
  let
berghofe@5177
   422
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
berghofe@5177
   423
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
berghofe@5177
   424
berghofe@5177
   425
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
berghofe@5177
   426
      let
berghofe@5177
   427
        val Type ("fun", [T, _]) = fastype_of comb;
berghofe@5177
   428
        val (_, fs) = strip_comb comb;
berghofe@5177
   429
        val (_, gs) = strip_comb comb';
berghofe@5177
   430
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
berghofe@5177
   431
        val M = Free ("M", T);
berghofe@5177
   432
        val M' = Free ("M'", T);
berghofe@5177
   433
berghofe@5177
   434
        fun mk_clause ((f, g), (cname, _)) =
berghofe@5177
   435
          let
berghofe@5177
   436
            val (Ts, _) = strip_type (fastype_of f);
wenzelm@20071
   437
            val tnames = Name.variant_list used (make_tnames Ts);
berghofe@5177
   438
            val frees = map Free (tnames ~~ Ts)
berghofe@5177
   439
          in
berghofe@5177
   440
            list_all_free (tnames ~~ Ts, Logic.mk_implies
berghofe@5177
   441
              (HOLogic.mk_Trueprop
berghofe@5177
   442
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
berghofe@5177
   443
               HOLogic.mk_Trueprop
berghofe@5177
   444
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
berghofe@5177
   445
          end
berghofe@5177
   446
berghofe@5177
   447
      in
berghofe@5177
   448
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
berghofe@5177
   449
          map mk_clause (fs ~~ gs ~~ constrs),
berghofe@5177
   450
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
berghofe@5177
   451
      end
berghofe@5177
   452
berghofe@5177
   453
  in
berghofe@5177
   454
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
berghofe@5177
   455
  end;
berghofe@5177
   456
berghofe@5177
   457
(*---------------------------------------------------------------------------
berghofe@5177
   458
 * Structure of exhaustion theorem looks like this:
berghofe@5177
   459
 *
berghofe@5177
   460
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
berghofe@5177
   461
 *---------------------------------------------------------------------------*)
berghofe@5177
   462
berghofe@5177
   463
fun make_nchotomys descr sorts =
berghofe@5177
   464
  let
skalberg@15570
   465
    val descr' = List.concat descr;
berghofe@5177
   466
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   467
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   468
berghofe@5177
   469
    fun mk_eqn T (cname, cargs) =
berghofe@5177
   470
      let
berghofe@5177
   471
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   472
        val tnames = Name.variant_list ["v"] (make_tnames Ts);
berghofe@5177
   473
        val frees = tnames ~~ Ts
berghofe@5177
   474
      in
skalberg@15574
   475
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
skalberg@15574
   476
          (HOLogic.mk_eq (Free ("v", T),
skalberg@15574
   477
            list_comb (Const (cname, Ts ---> T), map Free frees))) frees
berghofe@5177
   478
      end
berghofe@5177
   479
berghofe@5177
   480
  in map (fn ((_, (_, _, constrs)), T) =>
berghofe@5177
   481
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
berghofe@5177
   482
      (hd descr ~~ newTs)
berghofe@5177
   483
  end;
berghofe@5177
   484
berghofe@5177
   485
end;