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