src/HOL/Tools/datatype_prop.ML
author berghofe
Thu Oct 10 14:26:50 2002 +0200 (2002-10-10)
changeset 13641 63d1790a43ed
parent 13585 db4005b40cc6
child 14799 a405aadff16c
permissions -rw-r--r--
Reimplemented parts of datatype package dealing with datatypes involving
function types. Now also supports functions with more than one argument.
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_prop.ML
berghofe@5177
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
wenzelm@11539
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
berghofe@5177
     5
wenzelm@11539
     6
Characteristic properties of datatypes.
berghofe@5177
     7
*)
berghofe@5177
     8
berghofe@5177
     9
signature DATATYPE_PROP =
berghofe@5177
    10
sig
berghofe@7015
    11
  val dtK : int ref
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@13641
    17
  val make_primrecs : string list -> DatatypeAux.descr list ->
berghofe@13641
    18
    (string * sort) list -> theory -> term list
berghofe@13641
    19
  val make_cases : string list -> DatatypeAux.descr list ->
berghofe@13641
    20
    (string * sort) list -> theory -> term list list
berghofe@13641
    21
  val make_distincts : string list -> DatatypeAux.descr list ->
berghofe@13641
    22
    (string * sort) list -> theory -> term list list
berghofe@13641
    23
  val make_splits : string list -> DatatypeAux.descr list ->
berghofe@13641
    24
    (string * sort) list -> theory -> (term * term) list
berghofe@13641
    25
  val make_case_trrules : string list -> DatatypeAux.descr list ->
berghofe@13641
    26
    ast Syntax.trrule list
berghofe@13641
    27
  val make_size : DatatypeAux.descr list -> (string * sort) list ->
berghofe@13641
    28
    theory -> term list
berghofe@13641
    29
  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    30
    (string * sort) list -> theory -> term list
berghofe@13641
    31
  val make_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    32
    (string * sort) list -> theory -> term list
berghofe@13641
    33
  val make_nchotomys : DatatypeAux.descr list ->
berghofe@13641
    34
    (string * sort) list -> term list
berghofe@5177
    35
end;
berghofe@5177
    36
berghofe@5177
    37
structure DatatypeProp : DATATYPE_PROP =
berghofe@5177
    38
struct
berghofe@5177
    39
berghofe@5177
    40
open DatatypeAux;
berghofe@5177
    41
berghofe@5177
    42
(*the kind of distinctiveness axioms depends on number of constructors*)
berghofe@7015
    43
val dtK = ref 7;
berghofe@5177
    44
wenzelm@8434
    45
fun indexify_names names =
wenzelm@8434
    46
  let
wenzelm@8434
    47
    fun index (x :: xs) tab =
wenzelm@8434
    48
      (case assoc (tab, x) of
wenzelm@8434
    49
        None => if x mem xs then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
wenzelm@8434
    50
      | Some i => (x ^ Library.string_of_int i) :: index xs ((x, i + 1) :: tab))
wenzelm@8434
    51
    | index [] _ = [];
wenzelm@8434
    52
  in index names [] end;
wenzelm@8434
    53
berghofe@5177
    54
fun make_tnames Ts =
berghofe@5177
    55
  let
berghofe@5177
    56
    fun type_name (TFree (name, _)) = implode (tl (explode name))
berghofe@5177
    57
      | type_name (Type (name, _)) = 
berghofe@5177
    58
          let val name' = Sign.base_name name
wenzelm@8434
    59
          in if Syntax.is_identifier name' then name' else "x" end;
wenzelm@8434
    60
  in indexify_names (map type_name Ts) end;
berghofe@5177
    61
berghofe@5177
    62
berghofe@5177
    63
berghofe@5177
    64
(************************* injectivity of constructors ************************)
berghofe@5177
    65
berghofe@5177
    66
fun make_injs descr sorts =
berghofe@5177
    67
  let
berghofe@5177
    68
    val descr' = flat descr;
berghofe@5177
    69
berghofe@5177
    70
    fun make_inj T ((cname, cargs), injs) =
berghofe@5177
    71
      if null cargs then injs else
berghofe@5177
    72
        let
berghofe@5177
    73
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
    74
          val constr_t = Const (cname, Ts ---> T);
berghofe@5177
    75
          val tnames = make_tnames Ts;
berghofe@5177
    76
          val frees = map Free (tnames ~~ Ts);
berghofe@5177
    77
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
berghofe@5177
    78
        in (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
    79
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
berghofe@5177
    80
           foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
    81
             (map HOLogic.mk_eq (frees ~~ frees')))))::injs
berghofe@5177
    82
        end;
berghofe@5177
    83
berghofe@5177
    84
  in map (fn (d, T) => foldr (make_inj T) (#3 (snd d), []))
berghofe@5177
    85
    ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
    86
  end;
berghofe@5177
    87
berghofe@5177
    88
(********************************* induction **********************************)
berghofe@5177
    89
berghofe@5177
    90
fun make_ind descr sorts =
berghofe@5177
    91
  let
berghofe@5177
    92
    val descr' = flat descr;
berghofe@5177
    93
    val recTs = get_rec_types descr' sorts;
berghofe@5177
    94
    val pnames = if length descr' = 1 then ["P"]
berghofe@5177
    95
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
berghofe@5177
    96
berghofe@5177
    97
    fun make_pred i T =
berghofe@5177
    98
      let val T' = T --> HOLogic.boolT
berghofe@5177
    99
      in Free (nth_elem (i, pnames), T') end;
berghofe@5177
   100
berghofe@5177
   101
    fun make_ind_prem k T (cname, cargs) =
berghofe@5177
   102
      let
berghofe@13641
   103
        fun mk_prem ((dt, s), T) =
berghofe@13641
   104
          let val (Us, U) = strip_type T
berghofe@13641
   105
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
berghofe@13641
   106
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
berghofe@13641
   107
          end;
berghofe@7015
   108
berghofe@5177
   109
        val recs = filter is_rec_type cargs;
berghofe@5177
   110
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   111
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   112
        val tnames = variantlist (make_tnames Ts, pnames);
berghofe@5177
   113
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   114
        val frees = tnames ~~ Ts;
berghofe@7015
   115
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@5177
   116
berghofe@5177
   117
      in list_all_free (frees, Logic.list_implies (prems,
berghofe@5177
   118
        HOLogic.mk_Trueprop (make_pred k T $ 
berghofe@5177
   119
          list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@5177
   120
      end;
berghofe@5177
   121
berghofe@5177
   122
    val prems = flat (map (fn ((i, (_, _, constrs)), T) =>
berghofe@5177
   123
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
berghofe@5177
   124
    val tnames = make_tnames recTs;
berghofe@5177
   125
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
   126
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
berghofe@5177
   127
        (descr' ~~ recTs ~~ tnames)))
berghofe@5177
   128
berghofe@5177
   129
  in Logic.list_implies (prems, concl) end;
berghofe@5177
   130
berghofe@5177
   131
(******************************* case distinction *****************************)
berghofe@5177
   132
berghofe@5177
   133
fun make_casedists descr sorts =
berghofe@5177
   134
  let
berghofe@5177
   135
    val descr' = flat descr;
berghofe@5177
   136
berghofe@5177
   137
    fun make_casedist_prem T (cname, cargs) =
berghofe@5177
   138
      let
berghofe@5177
   139
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   140
        val frees = variantlist (make_tnames Ts, ["P", "y"]) ~~ Ts;
berghofe@5177
   141
        val free_ts = map Free frees
berghofe@5177
   142
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
berghofe@5177
   143
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
berghofe@5177
   144
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
berghofe@5177
   145
      end;
berghofe@5177
   146
berghofe@5177
   147
    fun make_casedist ((_, (_, _, constrs)), T) =
berghofe@5177
   148
      let val prems = map (make_casedist_prem T) constrs
berghofe@5177
   149
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
berghofe@5177
   150
      end
berghofe@5177
   151
berghofe@5177
   152
  in map make_casedist
berghofe@5177
   153
    ((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
   154
  end;
berghofe@5177
   155
berghofe@5177
   156
(*************** characteristic equations for primrec combinator **************)
berghofe@5177
   157
berghofe@5177
   158
fun make_primrecs new_type_names descr sorts thy =
berghofe@5177
   159
  let
wenzelm@6394
   160
    val sign = Theory.sign_of thy;
berghofe@5177
   161
berghofe@5177
   162
    val descr' = flat descr;
berghofe@5177
   163
    val recTs = get_rec_types descr' sorts;
berghofe@5578
   164
    val used = foldr add_typ_tfree_names (recTs, []);
berghofe@5177
   165
berghofe@5578
   166
    val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~
wenzelm@12338
   167
      replicate (length descr') HOLogic.typeS);
berghofe@5177
   168
berghofe@5177
   169
    val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   170
      map (fn (_, cargs) =>
berghofe@5177
   171
        let
berghofe@7015
   172
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@7015
   173
          val recs = filter (is_rec_type o fst) (cargs ~~ Ts);
berghofe@7015
   174
berghofe@13641
   175
          fun mk_argT (dt, T) =
berghofe@13641
   176
            binder_types T ---> nth_elem (body_index dt, rec_result_Ts);
berghofe@7015
   177
berghofe@7015
   178
          val argTs = Ts @ map mk_argT recs
berghofe@5177
   179
        in argTs ---> nth_elem (i, rec_result_Ts)
berghofe@5177
   180
        end) constrs) descr');
berghofe@5177
   181
berghofe@5177
   182
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   183
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@5177
   184
berghofe@5177
   185
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
berghofe@5177
   186
    val reccomb_names = map (Sign.intern_const sign)
berghofe@5177
   187
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   188
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   189
          (1 upto (length descr'))));
berghofe@5177
   190
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   191
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   192
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   193
berghofe@5177
   194
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
berghofe@5177
   195
      let
berghofe@5177
   196
        val recs = filter is_rec_type cargs;
berghofe@5177
   197
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   198
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   199
        val tnames = make_tnames Ts;
berghofe@5177
   200
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   201
        val frees = map Free (tnames ~~ Ts);
berghofe@5177
   202
        val frees' = map Free (rec_tnames ~~ recTs');
berghofe@7015
   203
berghofe@13641
   204
        fun mk_reccomb ((dt, T), t) =
berghofe@13641
   205
          let val (Us, U) = strip_type T
berghofe@13641
   206
          in list_abs (map (pair "x") Us,
berghofe@13641
   207
            nth_elem (body_index dt, reccombs) $ app_bnds t (length Us))
berghofe@13641
   208
          end;
berghofe@7015
   209
berghofe@13641
   210
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
berghofe@5177
   211
berghofe@5177
   212
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   213
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@13641
   214
         list_comb (f, frees @ reccombs')))], fs)
berghofe@5177
   215
      end
berghofe@5177
   216
berghofe@5177
   217
  in fst (foldl (fn (x, ((dt, T), comb_t)) =>
berghofe@5177
   218
    foldl (make_primrec T comb_t) (x, #3 (snd dt)))
berghofe@5177
   219
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
berghofe@5177
   220
  end;
berghofe@5177
   221
berghofe@5177
   222
(****************** make terms of form  t_case f1 ... fn  *********************)
berghofe@5177
   223
berghofe@5177
   224
fun make_case_combs new_type_names descr sorts thy fname =
berghofe@5177
   225
  let
berghofe@5177
   226
    val descr' = flat descr;
berghofe@5177
   227
    val recTs = get_rec_types descr' sorts;
berghofe@5578
   228
    val used = foldr add_typ_tfree_names (recTs, []);
berghofe@5177
   229
    val newTs = take (length (hd descr), recTs);
wenzelm@12338
   230
    val T' = TFree (variant used "'t", HOLogic.typeS);
berghofe@5177
   231
berghofe@5177
   232
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   233
      map (fn (_, cargs) =>
berghofe@5177
   234
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   235
        in Ts ---> T' end) constrs) (hd descr);
berghofe@5177
   236
berghofe@5177
   237
    val case_names = map (fn s =>
wenzelm@6394
   238
      Sign.intern_const (Theory.sign_of thy) (s ^ "_case")) new_type_names
berghofe@5177
   239
  in
berghofe@5177
   240
    map (fn ((name, Ts), T) => list_comb
berghofe@5177
   241
      (Const (name, Ts @ [T] ---> T'),
berghofe@5177
   242
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
berghofe@5177
   243
          (case_names ~~ case_fn_Ts ~~ newTs)
berghofe@5177
   244
  end;
berghofe@5177
   245
berghofe@5177
   246
(**************** characteristic equations for case combinator ****************)
berghofe@5177
   247
berghofe@5177
   248
fun make_cases new_type_names descr sorts thy =
berghofe@5177
   249
  let
berghofe@5177
   250
    val descr' = flat descr;
berghofe@5177
   251
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   252
    val newTs = take (length (hd descr), recTs);
berghofe@5177
   253
berghofe@5177
   254
    fun make_case T comb_t ((cname, cargs), f) =
berghofe@5177
   255
      let
berghofe@5177
   256
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   257
        val frees = map Free ((make_tnames Ts) ~~ Ts)
berghofe@5177
   258
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   259
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@5177
   260
         list_comb (f, frees)))
berghofe@5177
   261
      end
berghofe@5177
   262
berghofe@5177
   263
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
berghofe@5177
   264
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
berghofe@5177
   265
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   266
  end;
berghofe@5177
   267
berghofe@5177
   268
(************************* distinctness of constructors ***********************)
berghofe@5177
   269
berghofe@5177
   270
fun make_distincts new_type_names descr sorts thy =
berghofe@5177
   271
  let
berghofe@5177
   272
    val descr' = flat descr;
berghofe@5177
   273
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   274
    val newTs = take (length (hd descr), recTs);
berghofe@5177
   275
berghofe@5177
   276
    (**** number of constructors < dtK : C_i ... ~= C_j ... ****)
berghofe@5177
   277
berghofe@5177
   278
    fun make_distincts_1 _ [] = []
berghofe@5177
   279
      | make_distincts_1 T ((cname, cargs)::constrs) =
berghofe@5177
   280
          let
berghofe@5177
   281
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   282
            val frees = map Free ((make_tnames Ts) ~~ Ts);
berghofe@5177
   283
            val t = list_comb (Const (cname, Ts ---> T), frees);
berghofe@5177
   284
berghofe@5177
   285
            fun make_distincts' [] = []
berghofe@5177
   286
              | make_distincts' ((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 ((map ((op ^) o (rpair "'"))
berghofe@5177
   290
                      (make_tnames Ts')) ~~ Ts');
berghofe@5177
   291
                    val t' = list_comb (Const (cname', Ts' ---> T), frees')
berghofe@5177
   292
                  in
wenzelm@8434
   293
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')))::
wenzelm@8434
   294
                    (HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t)))::
berghofe@5177
   295
                      (make_distincts' constrs')
berghofe@5177
   296
                  end
berghofe@5177
   297
berghofe@5177
   298
          in (make_distincts' constrs) @ (make_distincts_1 T constrs)
berghofe@5177
   299
          end;
berghofe@5177
   300
berghofe@5177
   301
  in map (fn (((_, (_, _, constrs)), T), tname) =>
berghofe@7015
   302
      if length constrs < !dtK then make_distincts_1 T constrs else [])
berghofe@5177
   303
        ((hd descr) ~~ newTs ~~ new_type_names)
berghofe@5177
   304
  end;
berghofe@5177
   305
berghofe@7015
   306
berghofe@5177
   307
(*************************** the "split" - equations **************************)
berghofe@5177
   308
berghofe@5177
   309
fun make_splits new_type_names descr sorts thy =
berghofe@5177
   310
  let
berghofe@5177
   311
    val descr' = flat descr;
berghofe@5177
   312
    val recTs = get_rec_types descr' sorts;
berghofe@5578
   313
    val used' = foldr add_typ_tfree_names (recTs, []);
berghofe@5177
   314
    val newTs = take (length (hd descr), recTs);
wenzelm@12338
   315
    val T' = TFree (variant used' "'t", HOLogic.typeS);
berghofe@5177
   316
    val P = Free ("P", T' --> HOLogic.boolT);
berghofe@5177
   317
berghofe@5177
   318
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
berghofe@5177
   319
      let
berghofe@5177
   320
        val (_, fs) = strip_comb comb_t;
berghofe@5177
   321
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
berghofe@5177
   322
berghofe@5177
   323
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
berghofe@5177
   324
          let
berghofe@5177
   325
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   326
            val frees = map Free (variantlist (make_tnames Ts, used) ~~ Ts);
berghofe@5177
   327
            val eqn = HOLogic.mk_eq (Free ("x", T),
berghofe@5177
   328
              list_comb (Const (cname, Ts ---> T), frees));
berghofe@5177
   329
            val P' = P $ list_comb (f, frees)
berghofe@5177
   330
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
berghofe@5177
   331
                (frees, HOLogic.imp $ eqn $ P'))::t1s,
berghofe@5177
   332
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
wenzelm@8434
   333
                (frees, HOLogic.conj $ eqn $ (HOLogic.Not $ P')))::t2s)
berghofe@5177
   334
          end;
berghofe@5177
   335
berghofe@5177
   336
        val (t1s, t2s) = foldr process_constr (constrs ~~ fs, ([], []));
berghofe@5177
   337
        val lhs = P $ (comb_t $ Free ("x", T))
berghofe@5177
   338
      in
berghofe@5177
   339
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
wenzelm@8434
   340
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
berghofe@5177
   341
      end
berghofe@5177
   342
berghofe@5177
   343
  in map make_split ((hd descr) ~~ newTs ~~
berghofe@5177
   344
    (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   345
  end;
berghofe@5177
   346
berghofe@5177
   347
(************************ translation rules for case **************************)
berghofe@5177
   348
berghofe@5177
   349
fun make_case_trrules new_type_names descr =
berghofe@5177
   350
  let
berghofe@5177
   351
    fun mk_asts i j ((cname, cargs)::constrs) =
berghofe@5177
   352
      let
berghofe@5177
   353
        val k = length cargs;
berghofe@5177
   354
        val xs = map (fn i => Variable ("x" ^ string_of_int i)) (i upto i + k - 1);
berghofe@5177
   355
        val t = Variable ("t" ^ string_of_int j);
wenzelm@9060
   356
        val ast = Syntax.mk_appl (Constant "_case1")
wenzelm@5695
   357
          [Syntax.mk_appl (Constant (Sign.base_name cname)) xs, t];
berghofe@5177
   358
        val ast' = foldr (fn (x, y) =>
wenzelm@5695
   359
          Syntax.mk_appl (Constant "_abs") [x, y]) (xs, t)
berghofe@5177
   360
      in
berghofe@5177
   361
        (case constrs of
berghofe@5177
   362
            [] => (ast, [ast'])
berghofe@5177
   363
          | cs => let val (ast'', asts) = mk_asts (i + k) (j + 1) cs
wenzelm@9060
   364
              in (Syntax.mk_appl (Constant "_case2") [ast, ast''],
berghofe@5177
   365
                  ast'::asts)
berghofe@5177
   366
              end)
berghofe@5177
   367
      end;
berghofe@5177
   368
berghofe@5177
   369
    fun mk_trrule ((_, (_, _, constrs)), tname) =
berghofe@5177
   370
      let val (ast, asts) = mk_asts 1 1 constrs
berghofe@5177
   371
      in Syntax.ParsePrintRule
wenzelm@9060
   372
        (Syntax.mk_appl (Constant "_case_syntax") [Variable "t", ast],
wenzelm@5695
   373
         Syntax.mk_appl (Constant (tname ^ "_case"))
berghofe@5177
   374
           (asts @ [Variable "t"]))
berghofe@5177
   375
      end
berghofe@5177
   376
berghofe@5177
   377
  in
berghofe@5177
   378
    map mk_trrule (hd descr ~~ new_type_names)
berghofe@5177
   379
  end;
berghofe@5177
   380
berghofe@5177
   381
(******************************* size functions *******************************)
berghofe@5177
   382
berghofe@9739
   383
fun make_size descr sorts thy =
berghofe@5177
   384
  let
berghofe@5177
   385
    val descr' = flat descr;
berghofe@5177
   386
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   387
wenzelm@11957
   388
    val size_name = "Nat.size";
berghofe@5177
   389
    val size_names = replicate (length (hd descr)) size_name @
berghofe@9739
   390
      map (Sign.intern_const (Theory.sign_of thy)) (indexify_names
berghofe@9739
   391
        (map (fn T => name_of_typ T ^ "_size") (drop (length (hd descr), recTs))));
berghofe@5177
   392
    val size_consts = map (fn (s, T) =>
berghofe@5177
   393
      Const (s, T --> HOLogic.natT)) (size_names ~~ recTs);
berghofe@5177
   394
wenzelm@7704
   395
    fun plus (t1, t2) = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT) $ t1 $ t2;
berghofe@5177
   396
berghofe@5177
   397
    fun make_size_eqn size_const T (cname, cargs) =
berghofe@5177
   398
      let
berghofe@5177
   399
        val recs = filter is_rec_type cargs;
berghofe@5177
   400
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   401
        val recTs = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   402
        val tnames = make_tnames Ts;
berghofe@5177
   403
        val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   404
        val ts = map (fn ((r, s), T) => nth_elem (dest_DtRec r, size_consts) $
berghofe@5177
   405
          Free (s, T)) (recs ~~ rec_tnames ~~ recTs);
berghofe@5177
   406
        val t = if ts = [] then HOLogic.zero else
wenzelm@7704
   407
          foldl1 plus (ts @ [HOLogic.mk_nat 1])
berghofe@5177
   408
      in
berghofe@5177
   409
        HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $
berghofe@5177
   410
          list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t))
berghofe@5177
   411
      end
berghofe@5177
   412
berghofe@5177
   413
  in
berghofe@5177
   414
    flat (map (fn (((_, (_, _, constrs)), size_const), T) =>
berghofe@5177
   415
      map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs))
berghofe@5177
   416
  end;
berghofe@5177
   417
berghofe@5177
   418
(************************* additional rules for TFL ***************************)
berghofe@5177
   419
nipkow@8601
   420
fun make_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   421
  let
nipkow@8601
   422
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
nipkow@8601
   423
nipkow@8601
   424
    fun mk_case_cong comb =
nipkow@8601
   425
      let 
nipkow@8601
   426
        val Type ("fun", [T, _]) = fastype_of comb;
nipkow@8601
   427
        val M = Free ("M", T);
nipkow@8601
   428
        val M' = Free ("M'", T);
nipkow@8601
   429
      in
nipkow@8601
   430
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
nipkow@8601
   431
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
nipkow@8601
   432
      end
nipkow@8601
   433
  in
nipkow@8601
   434
    map mk_case_cong case_combs
nipkow@8601
   435
  end;
nipkow@8601
   436
 
nipkow@8601
   437
berghofe@5177
   438
(*---------------------------------------------------------------------------
berghofe@5177
   439
 * Structure of case congruence theorem looks like this:
berghofe@5177
   440
 *
berghofe@5177
   441
 *    (M = M') 
berghofe@5177
   442
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
berghofe@5177
   443
 *    ==> ... 
berghofe@5177
   444
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
berghofe@5177
   445
 *    ==>
berghofe@5177
   446
 *      (ty_case f1..fn M = ty_case g1..gn M')
berghofe@5177
   447
 *---------------------------------------------------------------------------*)
berghofe@5177
   448
berghofe@5177
   449
fun make_case_congs new_type_names descr sorts thy =
berghofe@5177
   450
  let
berghofe@5177
   451
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
berghofe@5177
   452
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
berghofe@5177
   453
berghofe@5177
   454
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
berghofe@5177
   455
      let
berghofe@5177
   456
        val Type ("fun", [T, _]) = fastype_of comb;
berghofe@5177
   457
        val (_, fs) = strip_comb comb;
berghofe@5177
   458
        val (_, gs) = strip_comb comb';
berghofe@5177
   459
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
berghofe@5177
   460
        val M = Free ("M", T);
berghofe@5177
   461
        val M' = Free ("M'", T);
berghofe@5177
   462
berghofe@5177
   463
        fun mk_clause ((f, g), (cname, _)) =
berghofe@5177
   464
          let
berghofe@5177
   465
            val (Ts, _) = strip_type (fastype_of f);
berghofe@5177
   466
            val tnames = variantlist (make_tnames Ts, used);
berghofe@5177
   467
            val frees = map Free (tnames ~~ Ts)
berghofe@5177
   468
          in
berghofe@5177
   469
            list_all_free (tnames ~~ Ts, Logic.mk_implies
berghofe@5177
   470
              (HOLogic.mk_Trueprop
berghofe@5177
   471
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
berghofe@5177
   472
               HOLogic.mk_Trueprop
berghofe@5177
   473
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
berghofe@5177
   474
          end
berghofe@5177
   475
berghofe@5177
   476
      in
berghofe@5177
   477
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
berghofe@5177
   478
          map mk_clause (fs ~~ gs ~~ constrs),
berghofe@5177
   479
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
berghofe@5177
   480
      end
berghofe@5177
   481
berghofe@5177
   482
  in
berghofe@5177
   483
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
berghofe@5177
   484
  end;
berghofe@5177
   485
berghofe@5177
   486
(*---------------------------------------------------------------------------
berghofe@5177
   487
 * Structure of exhaustion theorem looks like this:
berghofe@5177
   488
 *
berghofe@5177
   489
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
berghofe@5177
   490
 *---------------------------------------------------------------------------*)
berghofe@5177
   491
berghofe@5177
   492
fun make_nchotomys descr sorts =
berghofe@5177
   493
  let
berghofe@5177
   494
    val descr' = flat descr;
berghofe@5177
   495
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   496
    val newTs = take (length (hd descr), recTs);
berghofe@5177
   497
berghofe@5177
   498
    fun mk_eqn T (cname, cargs) =
berghofe@5177
   499
      let
berghofe@5177
   500
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   501
        val tnames = variantlist (make_tnames Ts, ["v"]);
berghofe@5177
   502
        val frees = tnames ~~ Ts
berghofe@5177
   503
      in
berghofe@5177
   504
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
berghofe@5177
   505
          (frees, HOLogic.mk_eq (Free ("v", T),
berghofe@5177
   506
            list_comb (Const (cname, Ts ---> T), map Free frees)))
berghofe@5177
   507
      end
berghofe@5177
   508
berghofe@5177
   509
  in map (fn ((_, (_, _, constrs)), T) =>
berghofe@5177
   510
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
berghofe@5177
   511
      (hd descr ~~ newTs)
berghofe@5177
   512
  end;
berghofe@5177
   513
berghofe@5177
   514
end;