src/HOL/Tools/datatype_prop.ML
author wenzelm
Thu Aug 03 17:30:36 2006 +0200 (2006-08-03)
changeset 20328 5b240a4216b0
parent 20071 8f3e1ddb50e6
child 21078 101aefd61aac
permissions -rw-r--r--
RuleInsts.bires_inst_tac;
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
skalberg@15570
    67
    val descr' = List.concat descr;
berghofe@5177
    68
berghofe@5177
    69
    fun make_inj T ((cname, cargs), injs) =
berghofe@5177
    70
      if null cargs then injs else
berghofe@5177
    71
        let
berghofe@5177
    72
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
    73
          val constr_t = Const (cname, Ts ---> T);
berghofe@5177
    74
          val tnames = make_tnames Ts;
berghofe@5177
    75
          val frees = map Free (tnames ~~ Ts);
berghofe@5177
    76
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
berghofe@5177
    77
        in (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
    78
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
berghofe@5177
    79
           foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
    80
             (map HOLogic.mk_eq (frees ~~ frees')))))::injs
berghofe@5177
    81
        end;
berghofe@5177
    82
skalberg@15574
    83
  in map (fn (d, T) => foldr (make_inj T) [] (#3 (snd d)))
skalberg@15570
    84
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
    85
  end;
berghofe@5177
    86
berghofe@5177
    87
(********************************* induction **********************************)
berghofe@5177
    88
berghofe@5177
    89
fun make_ind descr sorts =
berghofe@5177
    90
  let
skalberg@15570
    91
    val descr' = List.concat descr;
berghofe@5177
    92
    val recTs = get_rec_types descr' sorts;
berghofe@5177
    93
    val pnames = if length descr' = 1 then ["P"]
berghofe@5177
    94
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
berghofe@5177
    95
berghofe@5177
    96
    fun make_pred i T =
berghofe@5177
    97
      let val T' = T --> HOLogic.boolT
skalberg@15570
    98
      in Free (List.nth (pnames, i), T') end;
berghofe@5177
    99
berghofe@5177
   100
    fun make_ind_prem k T (cname, cargs) =
berghofe@5177
   101
      let
berghofe@13641
   102
        fun mk_prem ((dt, s), T) =
berghofe@13641
   103
          let val (Us, U) = strip_type T
berghofe@13641
   104
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
berghofe@13641
   105
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
berghofe@13641
   106
          end;
berghofe@7015
   107
skalberg@15570
   108
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   109
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   110
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
wenzelm@20071
   111
        val tnames = Name.variant_list pnames (make_tnames Ts);
skalberg@15570
   112
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   113
        val frees = tnames ~~ Ts;
berghofe@7015
   114
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@5177
   115
berghofe@5177
   116
      in list_all_free (frees, Logic.list_implies (prems,
berghofe@5177
   117
        HOLogic.mk_Trueprop (make_pred k T $ 
berghofe@5177
   118
          list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@5177
   119
      end;
berghofe@5177
   120
skalberg@15570
   121
    val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
berghofe@5177
   122
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
berghofe@5177
   123
    val tnames = make_tnames recTs;
berghofe@5177
   124
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
   125
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
berghofe@5177
   126
        (descr' ~~ recTs ~~ tnames)))
berghofe@5177
   127
berghofe@5177
   128
  in Logic.list_implies (prems, concl) end;
berghofe@5177
   129
berghofe@5177
   130
(******************************* case distinction *****************************)
berghofe@5177
   131
berghofe@5177
   132
fun make_casedists descr sorts =
berghofe@5177
   133
  let
skalberg@15570
   134
    val descr' = List.concat descr;
berghofe@5177
   135
berghofe@5177
   136
    fun make_casedist_prem T (cname, cargs) =
berghofe@5177
   137
      let
berghofe@5177
   138
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   139
        val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
berghofe@5177
   140
        val free_ts = map Free frees
berghofe@5177
   141
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
berghofe@5177
   142
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
berghofe@5177
   143
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
berghofe@5177
   144
      end;
berghofe@5177
   145
berghofe@5177
   146
    fun make_casedist ((_, (_, _, constrs)), T) =
berghofe@5177
   147
      let val prems = map (make_casedist_prem T) constrs
berghofe@5177
   148
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
berghofe@5177
   149
      end
berghofe@5177
   150
berghofe@5177
   151
  in map make_casedist
skalberg@15570
   152
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
   153
  end;
berghofe@5177
   154
berghofe@5177
   155
(*************** characteristic equations for primrec combinator **************)
berghofe@5177
   156
berghofe@15459
   157
fun make_primrec_Ts descr sorts used =
berghofe@5177
   158
  let
skalberg@15570
   159
    val descr' = List.concat descr;
berghofe@5177
   160
wenzelm@20071
   161
    val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
wenzelm@12338
   162
      replicate (length descr') HOLogic.typeS);
berghofe@5177
   163
skalberg@15570
   164
    val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   165
      map (fn (_, cargs) =>
berghofe@5177
   166
        let
berghofe@7015
   167
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
skalberg@15570
   168
          val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
berghofe@7015
   169
berghofe@13641
   170
          fun mk_argT (dt, T) =
skalberg@15570
   171
            binder_types T ---> List.nth (rec_result_Ts, body_index dt);
berghofe@7015
   172
berghofe@7015
   173
          val argTs = Ts @ map mk_argT recs
skalberg@15570
   174
        in argTs ---> List.nth (rec_result_Ts, i)
berghofe@5177
   175
        end) constrs) descr');
berghofe@5177
   176
berghofe@15459
   177
  in (rec_result_Ts, reccomb_fn_Ts) end;
berghofe@15459
   178
berghofe@15459
   179
fun make_primrecs new_type_names descr sorts thy =
berghofe@15459
   180
  let
berghofe@15459
   181
    val sign = Theory.sign_of thy;
berghofe@15459
   182
skalberg@15570
   183
    val descr' = List.concat descr;
berghofe@15459
   184
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   185
    val used = foldr add_typ_tfree_names [] recTs;
berghofe@15459
   186
berghofe@15459
   187
    val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
berghofe@15459
   188
berghofe@5177
   189
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   190
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@5177
   191
berghofe@5177
   192
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
berghofe@5177
   193
    val reccomb_names = map (Sign.intern_const sign)
berghofe@5177
   194
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   195
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   196
          (1 upto (length descr'))));
berghofe@5177
   197
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   198
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   199
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   200
berghofe@5177
   201
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
berghofe@5177
   202
      let
skalberg@15570
   203
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   204
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   205
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   206
        val tnames = make_tnames Ts;
skalberg@15570
   207
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   208
        val frees = map Free (tnames ~~ Ts);
berghofe@5177
   209
        val frees' = map Free (rec_tnames ~~ recTs');
berghofe@7015
   210
berghofe@13641
   211
        fun mk_reccomb ((dt, T), t) =
berghofe@13641
   212
          let val (Us, U) = strip_type T
berghofe@13641
   213
          in list_abs (map (pair "x") Us,
skalberg@15570
   214
            List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
berghofe@13641
   215
          end;
berghofe@7015
   216
berghofe@13641
   217
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
berghofe@5177
   218
berghofe@5177
   219
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   220
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@13641
   221
         list_comb (f, frees @ reccombs')))], fs)
berghofe@5177
   222
      end
berghofe@5177
   223
skalberg@15570
   224
  in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
skalberg@15570
   225
    Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
berghofe@5177
   226
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
berghofe@5177
   227
  end;
berghofe@5177
   228
berghofe@5177
   229
(****************** make terms of form  t_case f1 ... fn  *********************)
berghofe@5177
   230
berghofe@5177
   231
fun make_case_combs new_type_names descr sorts thy fname =
berghofe@5177
   232
  let
skalberg@15570
   233
    val descr' = List.concat descr;
berghofe@5177
   234
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   235
    val used = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   236
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   237
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
berghofe@5177
   238
berghofe@5177
   239
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   240
      map (fn (_, cargs) =>
berghofe@5177
   241
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   242
        in Ts ---> T' end) constrs) (hd descr);
berghofe@5177
   243
berghofe@5177
   244
    val case_names = map (fn s =>
wenzelm@6394
   245
      Sign.intern_const (Theory.sign_of thy) (s ^ "_case")) new_type_names
berghofe@5177
   246
  in
berghofe@5177
   247
    map (fn ((name, Ts), T) => list_comb
berghofe@5177
   248
      (Const (name, Ts @ [T] ---> T'),
berghofe@5177
   249
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
berghofe@5177
   250
          (case_names ~~ case_fn_Ts ~~ newTs)
berghofe@5177
   251
  end;
berghofe@5177
   252
berghofe@5177
   253
(**************** characteristic equations for case combinator ****************)
berghofe@5177
   254
berghofe@5177
   255
fun make_cases new_type_names descr sorts thy =
berghofe@5177
   256
  let
skalberg@15570
   257
    val descr' = List.concat descr;
berghofe@5177
   258
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   259
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   260
berghofe@5177
   261
    fun make_case T comb_t ((cname, cargs), f) =
berghofe@5177
   262
      let
berghofe@5177
   263
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   264
        val frees = map Free ((make_tnames Ts) ~~ Ts)
berghofe@5177
   265
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   266
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@5177
   267
         list_comb (f, frees)))
berghofe@5177
   268
      end
berghofe@5177
   269
berghofe@5177
   270
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
berghofe@5177
   271
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
berghofe@5177
   272
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   273
  end;
berghofe@5177
   274
berghofe@5177
   275
(************************* distinctness of constructors ***********************)
berghofe@5177
   276
berghofe@5177
   277
fun make_distincts new_type_names descr sorts thy =
berghofe@5177
   278
  let
skalberg@15570
   279
    val descr' = List.concat descr;
berghofe@5177
   280
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   281
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   282
berghofe@5177
   283
    (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
berghofe@5177
   284
berghofe@5177
   285
    fun make_distincts_1 _ [] = []
berghofe@5177
   286
      | make_distincts_1 T ((cname, cargs)::constrs) =
berghofe@5177
   287
          let
berghofe@5177
   288
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   289
            val frees = map Free ((make_tnames Ts) ~~ Ts);
berghofe@5177
   290
            val t = list_comb (Const (cname, Ts ---> T), frees);
berghofe@5177
   291
berghofe@5177
   292
            fun make_distincts' [] = []
berghofe@5177
   293
              | make_distincts' ((cname', cargs')::constrs') =
berghofe@5177
   294
                  let
berghofe@5177
   295
                    val Ts' = map (typ_of_dtyp descr' sorts) cargs';
berghofe@5177
   296
                    val frees' = map Free ((map ((op ^) o (rpair "'"))
berghofe@5177
   297
                      (make_tnames Ts')) ~~ Ts');
berghofe@5177
   298
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
berghofe@5177
   299
                  in
wenzelm@8434
   300
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
wenzelm@8434
   301
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
berghofe@5177
   302
                      (make_distincts' constrs')
berghofe@5177
   303
                  end
berghofe@5177
   304
berghofe@5177
   305
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
berghofe@5177
   306
          end;
berghofe@5177
   307
berghofe@5177
   308
  in map (fn (((_, (_, _, constrs)), T), tname) =>
berghofe@7015
   309
      if length constrs < !dtK then make_distincts_1 T constrs else [])
berghofe@5177
   310
        ((hd descr) ~~ newTs ~~ new_type_names)
berghofe@5177
   311
  end;
berghofe@5177
   312
berghofe@7015
   313
berghofe@5177
   314
(*************************** the "split" - equations **************************)
berghofe@5177
   315
berghofe@5177
   316
fun make_splits new_type_names descr sorts thy =
berghofe@5177
   317
  let
skalberg@15570
   318
    val descr' = List.concat descr;
berghofe@5177
   319
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   320
    val used' = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   321
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   322
    val T' = TFree (Name.variant used' "'t", HOLogic.typeS);
berghofe@5177
   323
    val P = Free ("P", T' --> HOLogic.boolT);
berghofe@5177
   324
berghofe@5177
   325
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
berghofe@5177
   326
      let
berghofe@5177
   327
        val (_, fs) = strip_comb comb_t;
berghofe@5177
   328
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
berghofe@5177
   329
berghofe@5177
   330
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
berghofe@5177
   331
          let
berghofe@5177
   332
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   333
            val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts);
berghofe@5177
   334
            val eqn = HOLogic.mk_eq (Free ("x", T),
berghofe@5177
   335
              list_comb (Const (cname, Ts ---> T), frees));
berghofe@5177
   336
            val P' = P $ list_comb (f, frees)
skalberg@15574
   337
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
skalberg@15574
   338
                (HOLogic.imp $ eqn $ P') frees)::t1s,
skalberg@15574
   339
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
skalberg@15574
   340
                (HOLogic.conj $ eqn $ (HOLogic.Not $ P')) frees)::t2s)
berghofe@5177
   341
          end;
berghofe@5177
   342
skalberg@15574
   343
        val (t1s, t2s) = foldr process_constr ([], []) (constrs ~~ fs);
berghofe@5177
   344
        val lhs = P $ (comb_t $ Free ("x", T))
berghofe@5177
   345
      in
berghofe@5177
   346
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
wenzelm@8434
   347
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
berghofe@5177
   348
      end
berghofe@5177
   349
berghofe@5177
   350
  in map make_split ((hd descr) ~~ newTs ~~
berghofe@5177
   351
    (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   352
  end;
berghofe@5177
   353
berghofe@5177
   354
berghofe@5177
   355
(******************************* size functions *******************************)
berghofe@5177
   356
berghofe@9739
   357
fun make_size descr sorts thy =
berghofe@5177
   358
  let
skalberg@15570
   359
    val descr' = List.concat descr;
berghofe@5177
   360
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   361
wenzelm@11957
   362
    val size_name = "Nat.size";
berghofe@5177
   363
    val size_names = replicate (length (hd descr)) size_name @
berghofe@9739
   364
      map (Sign.intern_const (Theory.sign_of thy)) (indexify_names
skalberg@15570
   365
        (map (fn T => name_of_typ T ^ "_size") (Library.drop (length (hd descr), recTs))));
berghofe@5177
   366
    val size_consts = map (fn (s, T) =>
berghofe@5177
   367
      Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
berghofe@5177
   368
haftmann@19233
   369
    fun plus (t1, t2) = Const ("HOL.plus", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT) $ t1 $ t2;
berghofe@5177
   370
berghofe@5177
   371
    fun make_size_eqn size_const T (cname, cargs) =
berghofe@5177
   372
      let
skalberg@15570
   373
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   374
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   375
        val recTs = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   376
        val tnames = make_tnames Ts;
skalberg@15570
   377
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
skalberg@15570
   378
        val ts = map (fn ((r, s), T) => List.nth (size_consts, dest_DtRec r) $
berghofe@5177
   379
          Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
berghofe@5177
   380
        val t = if ts = [] then HOLogic.zero else
wenzelm@7704
   381
          foldl1 plus (ts @ [HOLogic.mk_nat 1])
berghofe@5177
   382
      in
berghofe@5177
   383
        HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
berghofe@5177
   384
          list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
berghofe@5177
   385
      end
berghofe@5177
   386
berghofe@5177
   387
  in
skalberg@15570
   388
    List.concat (map (fn (((_, (_, _, constrs)), size_const), T) =>
berghofe@5177
   389
      map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
berghofe@5177
   390
  end;
berghofe@5177
   391
berghofe@5177
   392
(************************* additional rules for TFL ***************************)
berghofe@5177
   393
nipkow@8601
   394
fun make_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   395
  let
nipkow@8601
   396
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
nipkow@8601
   397
nipkow@8601
   398
    fun mk_case_cong comb =
nipkow@8601
   399
      let 
nipkow@8601
   400
        val Type ("fun", [T, _]) = fastype_of comb;
nipkow@8601
   401
        val M = Free ("M", T);
nipkow@8601
   402
        val M' = Free ("M'", T);
nipkow@8601
   403
      in
nipkow@8601
   404
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
nipkow@8601
   405
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
nipkow@8601
   406
      end
nipkow@8601
   407
  in
nipkow@8601
   408
    map mk_case_cong case_combs
nipkow@8601
   409
  end;
nipkow@8601
   410
 
nipkow@8601
   411
berghofe@5177
   412
(*---------------------------------------------------------------------------
berghofe@5177
   413
 * Structure of case congruence theorem looks like this:
berghofe@5177
   414
 *
berghofe@5177
   415
 *    (M = M') 
berghofe@5177
   416
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
berghofe@5177
   417
 *    ==> ... 
berghofe@5177
   418
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
berghofe@5177
   419
 *    ==>
berghofe@5177
   420
 *      (ty_case f1..fn M = ty_case g1..gn M')
berghofe@5177
   421
 *---------------------------------------------------------------------------*)
berghofe@5177
   422
berghofe@5177
   423
fun make_case_congs new_type_names descr sorts thy =
berghofe@5177
   424
  let
berghofe@5177
   425
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
berghofe@5177
   426
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
berghofe@5177
   427
berghofe@5177
   428
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
berghofe@5177
   429
      let
berghofe@5177
   430
        val Type ("fun", [T, _]) = fastype_of comb;
berghofe@5177
   431
        val (_, fs) = strip_comb comb;
berghofe@5177
   432
        val (_, gs) = strip_comb comb';
berghofe@5177
   433
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
berghofe@5177
   434
        val M = Free ("M", T);
berghofe@5177
   435
        val M' = Free ("M'", T);
berghofe@5177
   436
berghofe@5177
   437
        fun mk_clause ((f, g), (cname, _)) =
berghofe@5177
   438
          let
berghofe@5177
   439
            val (Ts, _) = strip_type (fastype_of f);
wenzelm@20071
   440
            val tnames = Name.variant_list used (make_tnames Ts);
berghofe@5177
   441
            val frees = map Free (tnames ~~ Ts)
berghofe@5177
   442
          in
berghofe@5177
   443
            list_all_free (tnames ~~ Ts, Logic.mk_implies
berghofe@5177
   444
              (HOLogic.mk_Trueprop
berghofe@5177
   445
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
berghofe@5177
   446
               HOLogic.mk_Trueprop
berghofe@5177
   447
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
berghofe@5177
   448
          end
berghofe@5177
   449
berghofe@5177
   450
      in
berghofe@5177
   451
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
berghofe@5177
   452
          map mk_clause (fs ~~ gs ~~ constrs),
berghofe@5177
   453
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
berghofe@5177
   454
      end
berghofe@5177
   455
berghofe@5177
   456
  in
berghofe@5177
   457
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
berghofe@5177
   458
  end;
berghofe@5177
   459
berghofe@5177
   460
(*---------------------------------------------------------------------------
berghofe@5177
   461
 * Structure of exhaustion theorem looks like this:
berghofe@5177
   462
 *
berghofe@5177
   463
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
berghofe@5177
   464
 *---------------------------------------------------------------------------*)
berghofe@5177
   465
berghofe@5177
   466
fun make_nchotomys descr sorts =
berghofe@5177
   467
  let
skalberg@15570
   468
    val descr' = List.concat descr;
berghofe@5177
   469
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   470
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   471
berghofe@5177
   472
    fun mk_eqn T (cname, cargs) =
berghofe@5177
   473
      let
berghofe@5177
   474
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   475
        val tnames = Name.variant_list ["v"] (make_tnames Ts);
berghofe@5177
   476
        val frees = tnames ~~ Ts
berghofe@5177
   477
      in
skalberg@15574
   478
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
skalberg@15574
   479
          (HOLogic.mk_eq (Free ("v", T),
skalberg@15574
   480
            list_comb (Const (cname, Ts ---> T), map Free frees))) frees
berghofe@5177
   481
      end
berghofe@5177
   482
berghofe@5177
   483
  in map (fn ((_, (_, _, constrs)), T) =>
berghofe@5177
   484
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
berghofe@5177
   485
      (hd descr ~~ newTs)
berghofe@5177
   486
  end;
berghofe@5177
   487
berghofe@5177
   488
end;