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