src/HOL/Tools/datatype_prop.ML
author haftmann
Fri Jun 20 21:00:22 2008 +0200 (2008-06-20)
changeset 27300 4cb3101d2bf7
parent 27002 215d64dc971e
child 29270 0eade173f77e
permissions -rw-r--r--
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
berghofe@5177
     1
(*  Title:      HOL/Tools/datatype_prop.ML
berghofe@5177
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@5177
     4
wenzelm@11539
     5
Characteristic properties of datatypes.
berghofe@5177
     6
*)
berghofe@5177
     7
berghofe@5177
     8
signature DATATYPE_PROP =
berghofe@5177
     9
sig
wenzelm@8434
    10
  val indexify_names: string list -> string list
berghofe@13465
    11
  val make_tnames: typ list -> string list
berghofe@13641
    12
  val make_injs : DatatypeAux.descr list -> (string * sort) list -> term list list
haftmann@26969
    13
  val make_distincts : DatatypeAux.descr list ->
haftmann@27300
    14
    (string * sort) list -> (int * term list) list (*no symmetric inequalities*)
berghofe@13641
    15
  val make_ind : DatatypeAux.descr list -> (string * sort) list -> term
berghofe@13641
    16
  val make_casedists : DatatypeAux.descr list -> (string * sort) list -> term list
berghofe@15459
    17
  val make_primrec_Ts : DatatypeAux.descr list -> (string * sort) list ->
berghofe@15459
    18
    string list -> typ list * typ list
berghofe@13641
    19
  val make_primrecs : string list -> DatatypeAux.descr list ->
berghofe@13641
    20
    (string * sort) list -> theory -> term list
berghofe@13641
    21
  val make_cases : string list -> DatatypeAux.descr list ->
berghofe@13641
    22
    (string * sort) list -> theory -> term list list
berghofe@13641
    23
  val make_splits : string list -> DatatypeAux.descr list ->
berghofe@13641
    24
    (string * sort) list -> theory -> (term * term) list
berghofe@13641
    25
  val make_weak_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    26
    (string * sort) list -> theory -> term list
berghofe@13641
    27
  val make_case_congs : string list -> DatatypeAux.descr list ->
berghofe@13641
    28
    (string * sort) list -> theory -> term list
berghofe@13641
    29
  val make_nchotomys : DatatypeAux.descr list ->
berghofe@13641
    30
    (string * sort) list -> term list
berghofe@5177
    31
end;
berghofe@5177
    32
berghofe@5177
    33
structure DatatypeProp : DATATYPE_PROP =
berghofe@5177
    34
struct
berghofe@5177
    35
berghofe@5177
    36
open DatatypeAux;
berghofe@5177
    37
wenzelm@8434
    38
fun indexify_names names =
wenzelm@8434
    39
  let
wenzelm@8434
    40
    fun index (x :: xs) tab =
haftmann@17521
    41
      (case AList.lookup (op =) tab x of
haftmann@25154
    42
        NONE => if member (op =) xs x then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab
haftmann@25154
    43
      | SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab))
wenzelm@8434
    44
    | index [] _ = [];
wenzelm@8434
    45
  in index names [] end;
wenzelm@8434
    46
berghofe@5177
    47
fun make_tnames Ts =
berghofe@5177
    48
  let
berghofe@5177
    49
    fun type_name (TFree (name, _)) = implode (tl (explode name))
berghofe@5177
    50
      | type_name (Type (name, _)) = 
berghofe@5177
    51
          let val name' = Sign.base_name name
wenzelm@8434
    52
          in if Syntax.is_identifier name' then name' else "x" end;
wenzelm@8434
    53
  in indexify_names (map type_name Ts) end;
berghofe@5177
    54
berghofe@5177
    55
berghofe@5177
    56
(************************* injectivity of constructors ************************)
berghofe@5177
    57
berghofe@5177
    58
fun make_injs descr sorts =
berghofe@5177
    59
  let
haftmann@21078
    60
    val descr' = flat descr;
haftmann@21078
    61
    fun make_inj T (cname, cargs) =
haftmann@21078
    62
      if null cargs then I else
berghofe@5177
    63
        let
berghofe@5177
    64
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
    65
          val constr_t = Const (cname, Ts ---> T);
berghofe@5177
    66
          val tnames = make_tnames Ts;
berghofe@5177
    67
          val frees = map Free (tnames ~~ Ts);
berghofe@5177
    68
          val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts);
haftmann@21078
    69
        in cons (HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
    70
          (HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')),
berghofe@5177
    71
           foldr1 (HOLogic.mk_binop "op &")
haftmann@21078
    72
             (map HOLogic.mk_eq (frees ~~ frees')))))
berghofe@5177
    73
        end;
haftmann@21078
    74
  in
haftmann@21078
    75
    map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) [])
haftmann@21078
    76
      (hd descr) (Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
    77
  end;
berghofe@5177
    78
haftmann@27300
    79
haftmann@27300
    80
(************************* distinctness of constructors ***********************)
haftmann@27300
    81
haftmann@27300
    82
fun make_distincts descr sorts =
haftmann@27300
    83
  let
haftmann@27300
    84
    val descr' = flat descr;
haftmann@27300
    85
    val recTs = get_rec_types descr' sorts;
haftmann@27300
    86
    val newTs = Library.take (length (hd descr), recTs);
haftmann@27300
    87
haftmann@27300
    88
    fun prep_constr (cname, cargs) = (cname, map (typ_of_dtyp descr' sorts) cargs);
haftmann@27300
    89
haftmann@27300
    90
    fun make_distincts' _ [] = []
haftmann@27300
    91
      | make_distincts' T ((cname, cargs)::constrs) =
haftmann@27300
    92
          let
haftmann@27300
    93
            val frees = map Free ((make_tnames cargs) ~~ cargs);
haftmann@27300
    94
            val t = list_comb (Const (cname, cargs ---> T), frees);
haftmann@27300
    95
haftmann@27300
    96
            fun make_distincts'' (cname', cargs') =
haftmann@27300
    97
              let
haftmann@27300
    98
                val frees' = map Free ((map ((op ^) o (rpair "'"))
haftmann@27300
    99
                  (make_tnames cargs')) ~~ cargs');
haftmann@27300
   100
                val t' = list_comb (Const (cname', cargs' ---> T), frees')
haftmann@27300
   101
              in
haftmann@27300
   102
                HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t'))
haftmann@27300
   103
              end
haftmann@27300
   104
haftmann@27300
   105
          in map make_distincts'' constrs @ make_distincts' T constrs end;
haftmann@27300
   106
haftmann@27300
   107
  in
haftmann@27300
   108
    map2 (fn ((_, (_, _, constrs))) => fn T =>
haftmann@27300
   109
      (length constrs, make_distincts' T (map prep_constr constrs))) (hd descr) newTs
haftmann@27300
   110
  end;
haftmann@27300
   111
haftmann@27300
   112
berghofe@5177
   113
(********************************* induction **********************************)
berghofe@5177
   114
berghofe@5177
   115
fun make_ind descr sorts =
berghofe@5177
   116
  let
skalberg@15570
   117
    val descr' = List.concat descr;
berghofe@5177
   118
    val recTs = get_rec_types descr' sorts;
berghofe@5177
   119
    val pnames = if length descr' = 1 then ["P"]
berghofe@5177
   120
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr');
berghofe@5177
   121
berghofe@5177
   122
    fun make_pred i T =
berghofe@5177
   123
      let val T' = T --> HOLogic.boolT
skalberg@15570
   124
      in Free (List.nth (pnames, i), T') end;
berghofe@5177
   125
berghofe@5177
   126
    fun make_ind_prem k T (cname, cargs) =
berghofe@5177
   127
      let
berghofe@13641
   128
        fun mk_prem ((dt, s), T) =
berghofe@13641
   129
          let val (Us, U) = strip_type T
berghofe@13641
   130
          in list_all (map (pair "x") Us, HOLogic.mk_Trueprop
berghofe@13641
   131
            (make_pred (body_index dt) U $ app_bnds (Free (s, T)) (length Us)))
berghofe@13641
   132
          end;
berghofe@7015
   133
skalberg@15570
   134
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   135
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   136
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
wenzelm@20071
   137
        val tnames = Name.variant_list pnames (make_tnames Ts);
skalberg@15570
   138
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   139
        val frees = tnames ~~ Ts;
berghofe@7015
   140
        val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs');
berghofe@5177
   141
berghofe@5177
   142
      in list_all_free (frees, Logic.list_implies (prems,
berghofe@5177
   143
        HOLogic.mk_Trueprop (make_pred k T $ 
berghofe@5177
   144
          list_comb (Const (cname, Ts ---> T), map Free frees))))
berghofe@5177
   145
      end;
berghofe@5177
   146
skalberg@15570
   147
    val prems = List.concat (map (fn ((i, (_, _, constrs)), T) =>
berghofe@5177
   148
      map (make_ind_prem i T) constrs) (descr' ~~ recTs));
berghofe@5177
   149
    val tnames = make_tnames recTs;
berghofe@5177
   150
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
berghofe@5177
   151
      (map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T))
berghofe@5177
   152
        (descr' ~~ recTs ~~ tnames)))
berghofe@5177
   153
berghofe@5177
   154
  in Logic.list_implies (prems, concl) end;
berghofe@5177
   155
berghofe@5177
   156
(******************************* case distinction *****************************)
berghofe@5177
   157
berghofe@5177
   158
fun make_casedists descr sorts =
berghofe@5177
   159
  let
skalberg@15570
   160
    val descr' = List.concat descr;
berghofe@5177
   161
berghofe@5177
   162
    fun make_casedist_prem T (cname, cargs) =
berghofe@5177
   163
      let
berghofe@5177
   164
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   165
        val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts;
berghofe@5177
   166
        val free_ts = map Free frees
berghofe@5177
   167
      in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
berghofe@5177
   168
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
berghofe@5177
   169
          HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))))
berghofe@5177
   170
      end;
berghofe@5177
   171
berghofe@5177
   172
    fun make_casedist ((_, (_, _, constrs)), T) =
berghofe@5177
   173
      let val prems = map (make_casedist_prem T) constrs
berghofe@5177
   174
      in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))
berghofe@5177
   175
      end
berghofe@5177
   176
berghofe@5177
   177
  in map make_casedist
skalberg@15570
   178
    ((hd descr) ~~ Library.take (length (hd descr), get_rec_types descr' sorts))
berghofe@5177
   179
  end;
berghofe@5177
   180
berghofe@5177
   181
(*************** characteristic equations for primrec combinator **************)
berghofe@5177
   182
berghofe@15459
   183
fun make_primrec_Ts descr sorts used =
berghofe@5177
   184
  let
skalberg@15570
   185
    val descr' = List.concat descr;
berghofe@5177
   186
wenzelm@20071
   187
    val rec_result_Ts = map TFree (Name.variant_list used (replicate (length descr') "'t") ~~
wenzelm@12338
   188
      replicate (length descr') HOLogic.typeS);
berghofe@5177
   189
skalberg@15570
   190
    val reccomb_fn_Ts = List.concat (map (fn (i, (_, _, constrs)) =>
berghofe@5177
   191
      map (fn (_, cargs) =>
berghofe@5177
   192
        let
berghofe@7015
   193
          val Ts = map (typ_of_dtyp descr' sorts) cargs;
skalberg@15570
   194
          val recs = List.filter (is_rec_type o fst) (cargs ~~ Ts);
berghofe@7015
   195
berghofe@13641
   196
          fun mk_argT (dt, T) =
skalberg@15570
   197
            binder_types T ---> List.nth (rec_result_Ts, body_index dt);
berghofe@7015
   198
berghofe@7015
   199
          val argTs = Ts @ map mk_argT recs
skalberg@15570
   200
        in argTs ---> List.nth (rec_result_Ts, i)
berghofe@5177
   201
        end) constrs) descr');
berghofe@5177
   202
berghofe@15459
   203
  in (rec_result_Ts, reccomb_fn_Ts) end;
berghofe@15459
   204
berghofe@15459
   205
fun make_primrecs new_type_names descr sorts thy =
berghofe@15459
   206
  let
skalberg@15570
   207
    val descr' = List.concat descr;
berghofe@15459
   208
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   209
    val used = foldr add_typ_tfree_names [] recTs;
berghofe@15459
   210
berghofe@15459
   211
    val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used;
berghofe@15459
   212
berghofe@5177
   213
    val rec_fns = map (uncurry (mk_Free "f"))
berghofe@5177
   214
      (reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts)));
berghofe@5177
   215
berghofe@5177
   216
    val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec";
wenzelm@22578
   217
    val reccomb_names = map (Sign.intern_const thy)
berghofe@5177
   218
      (if length descr' = 1 then [big_reccomb_name] else
berghofe@5177
   219
        (map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int)
berghofe@5177
   220
          (1 upto (length descr'))));
berghofe@5177
   221
    val reccombs = map (fn ((name, T), T') => list_comb
berghofe@5177
   222
      (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns))
berghofe@5177
   223
        (reccomb_names ~~ recTs ~~ rec_result_Ts);
berghofe@5177
   224
berghofe@5177
   225
    fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) =
berghofe@5177
   226
      let
skalberg@15570
   227
        val recs = List.filter is_rec_type cargs;
berghofe@5177
   228
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   229
        val recTs' = map (typ_of_dtyp descr' sorts) recs;
berghofe@5177
   230
        val tnames = make_tnames Ts;
skalberg@15570
   231
        val rec_tnames = map fst (List.filter (is_rec_type o snd) (tnames ~~ cargs));
berghofe@5177
   232
        val frees = map Free (tnames ~~ Ts);
berghofe@5177
   233
        val frees' = map Free (rec_tnames ~~ recTs');
berghofe@7015
   234
berghofe@13641
   235
        fun mk_reccomb ((dt, T), t) =
berghofe@13641
   236
          let val (Us, U) = strip_type T
berghofe@13641
   237
          in list_abs (map (pair "x") Us,
skalberg@15570
   238
            List.nth (reccombs, body_index dt) $ app_bnds t (length Us))
berghofe@13641
   239
          end;
berghofe@7015
   240
berghofe@13641
   241
        val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees')
berghofe@5177
   242
berghofe@5177
   243
      in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   244
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@13641
   245
         list_comb (f, frees @ reccombs')))], fs)
berghofe@5177
   246
      end
berghofe@5177
   247
skalberg@15570
   248
  in fst (Library.foldl (fn (x, ((dt, T), comb_t)) =>
skalberg@15570
   249
    Library.foldl (make_primrec T comb_t) (x, #3 (snd dt)))
berghofe@5177
   250
      (([], rec_fns), descr' ~~ recTs ~~ reccombs))
berghofe@5177
   251
  end;
berghofe@5177
   252
berghofe@5177
   253
(****************** make terms of form  t_case f1 ... fn  *********************)
berghofe@5177
   254
berghofe@5177
   255
fun make_case_combs new_type_names descr sorts thy fname =
berghofe@5177
   256
  let
skalberg@15570
   257
    val descr' = List.concat descr;
berghofe@5177
   258
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   259
    val used = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   260
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   261
    val T' = TFree (Name.variant used "'t", HOLogic.typeS);
berghofe@5177
   262
berghofe@5177
   263
    val case_fn_Ts = map (fn (i, (_, _, constrs)) =>
berghofe@5177
   264
      map (fn (_, cargs) =>
berghofe@5177
   265
        let val Ts = map (typ_of_dtyp descr' sorts) cargs
berghofe@5177
   266
        in Ts ---> T' end) constrs) (hd descr);
berghofe@5177
   267
berghofe@5177
   268
    val case_names = map (fn s =>
wenzelm@22578
   269
      Sign.intern_const thy (s ^ "_case")) new_type_names
berghofe@5177
   270
  in
berghofe@5177
   271
    map (fn ((name, Ts), T) => list_comb
berghofe@5177
   272
      (Const (name, Ts @ [T] ---> T'),
berghofe@5177
   273
        map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts))))
berghofe@5177
   274
          (case_names ~~ case_fn_Ts ~~ newTs)
berghofe@5177
   275
  end;
berghofe@5177
   276
berghofe@5177
   277
(**************** characteristic equations for case combinator ****************)
berghofe@5177
   278
berghofe@5177
   279
fun make_cases new_type_names descr sorts thy =
berghofe@5177
   280
  let
skalberg@15570
   281
    val descr' = List.concat descr;
berghofe@5177
   282
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   283
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   284
berghofe@5177
   285
    fun make_case T comb_t ((cname, cargs), f) =
berghofe@5177
   286
      let
berghofe@5177
   287
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
berghofe@5177
   288
        val frees = map Free ((make_tnames Ts) ~~ Ts)
berghofe@5177
   289
      in HOLogic.mk_Trueprop (HOLogic.mk_eq
berghofe@5177
   290
        (comb_t $ list_comb (Const (cname, Ts ---> T), frees),
berghofe@5177
   291
         list_comb (f, frees)))
berghofe@5177
   292
      end
berghofe@5177
   293
berghofe@5177
   294
  in map (fn (((_, (_, _, constrs)), T), comb_t) =>
berghofe@5177
   295
    map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t))))
berghofe@5177
   296
      ((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   297
  end;
berghofe@5177
   298
berghofe@7015
   299
berghofe@5177
   300
(*************************** the "split" - equations **************************)
berghofe@5177
   301
berghofe@5177
   302
fun make_splits new_type_names descr sorts thy =
berghofe@5177
   303
  let
skalberg@15570
   304
    val descr' = List.concat descr;
berghofe@5177
   305
    val recTs = get_rec_types descr' sorts;
skalberg@15574
   306
    val used' = foldr add_typ_tfree_names [] recTs;
skalberg@15570
   307
    val newTs = Library.take (length (hd descr), recTs);
wenzelm@20071
   308
    val T' = TFree (Name.variant used' "'t", HOLogic.typeS);
berghofe@5177
   309
    val P = Free ("P", T' --> HOLogic.boolT);
berghofe@5177
   310
berghofe@5177
   311
    fun make_split (((_, (_, _, constrs)), T), comb_t) =
berghofe@5177
   312
      let
berghofe@5177
   313
        val (_, fs) = strip_comb comb_t;
berghofe@5177
   314
        val used = ["P", "x"] @ (map (fst o dest_Free) fs);
berghofe@5177
   315
berghofe@5177
   316
        fun process_constr (((cname, cargs), f), (t1s, t2s)) =
berghofe@5177
   317
          let
berghofe@5177
   318
            val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   319
            val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts);
berghofe@5177
   320
            val eqn = HOLogic.mk_eq (Free ("x", T),
berghofe@5177
   321
              list_comb (Const (cname, Ts ---> T), frees));
berghofe@5177
   322
            val P' = P $ list_comb (f, frees)
skalberg@15574
   323
          in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t))
skalberg@15574
   324
                (HOLogic.imp $ eqn $ P') frees)::t1s,
skalberg@15574
   325
              (foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t))
skalberg@15574
   326
                (HOLogic.conj $ eqn $ (HOLogic.Not $ P')) frees)::t2s)
berghofe@5177
   327
          end;
berghofe@5177
   328
skalberg@15574
   329
        val (t1s, t2s) = foldr process_constr ([], []) (constrs ~~ fs);
berghofe@5177
   330
        val lhs = P $ (comb_t $ Free ("x", T))
berghofe@5177
   331
      in
berghofe@5177
   332
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)),
wenzelm@8434
   333
         HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s)))
berghofe@5177
   334
      end
berghofe@5177
   335
berghofe@5177
   336
  in map make_split ((hd descr) ~~ newTs ~~
berghofe@5177
   337
    (make_case_combs new_type_names descr sorts thy "f"))
berghofe@5177
   338
  end;
berghofe@5177
   339
berghofe@5177
   340
(************************* additional rules for TFL ***************************)
berghofe@5177
   341
nipkow@8601
   342
fun make_weak_case_congs new_type_names descr sorts thy =
nipkow@8601
   343
  let
nipkow@8601
   344
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
nipkow@8601
   345
nipkow@8601
   346
    fun mk_case_cong comb =
nipkow@8601
   347
      let 
nipkow@8601
   348
        val Type ("fun", [T, _]) = fastype_of comb;
nipkow@8601
   349
        val M = Free ("M", T);
nipkow@8601
   350
        val M' = Free ("M'", T);
nipkow@8601
   351
      in
nipkow@8601
   352
        Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')),
nipkow@8601
   353
          HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M')))
nipkow@8601
   354
      end
nipkow@8601
   355
  in
nipkow@8601
   356
    map mk_case_cong case_combs
nipkow@8601
   357
  end;
nipkow@8601
   358
 
nipkow@8601
   359
berghofe@5177
   360
(*---------------------------------------------------------------------------
berghofe@5177
   361
 * Structure of case congruence theorem looks like this:
berghofe@5177
   362
 *
berghofe@5177
   363
 *    (M = M') 
berghofe@5177
   364
 *    ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) 
berghofe@5177
   365
 *    ==> ... 
berghofe@5177
   366
 *    ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) 
berghofe@5177
   367
 *    ==>
berghofe@5177
   368
 *      (ty_case f1..fn M = ty_case g1..gn M')
berghofe@5177
   369
 *---------------------------------------------------------------------------*)
berghofe@5177
   370
berghofe@5177
   371
fun make_case_congs new_type_names descr sorts thy =
berghofe@5177
   372
  let
berghofe@5177
   373
    val case_combs = make_case_combs new_type_names descr sorts thy "f";
berghofe@5177
   374
    val case_combs' = make_case_combs new_type_names descr sorts thy "g";
berghofe@5177
   375
berghofe@5177
   376
    fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) =
berghofe@5177
   377
      let
berghofe@5177
   378
        val Type ("fun", [T, _]) = fastype_of comb;
berghofe@5177
   379
        val (_, fs) = strip_comb comb;
berghofe@5177
   380
        val (_, gs) = strip_comb comb';
berghofe@5177
   381
        val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs);
berghofe@5177
   382
        val M = Free ("M", T);
berghofe@5177
   383
        val M' = Free ("M'", T);
berghofe@5177
   384
berghofe@5177
   385
        fun mk_clause ((f, g), (cname, _)) =
berghofe@5177
   386
          let
berghofe@5177
   387
            val (Ts, _) = strip_type (fastype_of f);
wenzelm@20071
   388
            val tnames = Name.variant_list used (make_tnames Ts);
berghofe@5177
   389
            val frees = map Free (tnames ~~ Ts)
berghofe@5177
   390
          in
berghofe@5177
   391
            list_all_free (tnames ~~ Ts, Logic.mk_implies
berghofe@5177
   392
              (HOLogic.mk_Trueprop
berghofe@5177
   393
                (HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))),
berghofe@5177
   394
               HOLogic.mk_Trueprop
berghofe@5177
   395
                (HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees)))))
berghofe@5177
   396
          end
berghofe@5177
   397
berghofe@5177
   398
      in
berghofe@5177
   399
        Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) ::
berghofe@5177
   400
          map mk_clause (fs ~~ gs ~~ constrs),
berghofe@5177
   401
            HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M')))
berghofe@5177
   402
      end
berghofe@5177
   403
berghofe@5177
   404
  in
berghofe@5177
   405
    map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr)
berghofe@5177
   406
  end;
berghofe@5177
   407
berghofe@5177
   408
(*---------------------------------------------------------------------------
berghofe@5177
   409
 * Structure of exhaustion theorem looks like this:
berghofe@5177
   410
 *
berghofe@5177
   411
 *    !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj)
berghofe@5177
   412
 *---------------------------------------------------------------------------*)
berghofe@5177
   413
berghofe@5177
   414
fun make_nchotomys descr sorts =
berghofe@5177
   415
  let
skalberg@15570
   416
    val descr' = List.concat descr;
berghofe@5177
   417
    val recTs = get_rec_types descr' sorts;
skalberg@15570
   418
    val newTs = Library.take (length (hd descr), recTs);
berghofe@5177
   419
berghofe@5177
   420
    fun mk_eqn T (cname, cargs) =
berghofe@5177
   421
      let
berghofe@5177
   422
        val Ts = map (typ_of_dtyp descr' sorts) cargs;
wenzelm@20071
   423
        val tnames = Name.variant_list ["v"] (make_tnames Ts);
berghofe@5177
   424
        val frees = tnames ~~ Ts
berghofe@5177
   425
      in
skalberg@15574
   426
        foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t))
skalberg@15574
   427
          (HOLogic.mk_eq (Free ("v", T),
skalberg@15574
   428
            list_comb (Const (cname, Ts ---> T), map Free frees))) frees
berghofe@5177
   429
      end
berghofe@5177
   430
berghofe@5177
   431
  in map (fn ((_, (_, _, constrs)), T) =>
berghofe@5177
   432
    HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs))))
berghofe@5177
   433
      (hd descr ~~ newTs)
berghofe@5177
   434
  end;
berghofe@5177
   435
berghofe@5177
   436
end;