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