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