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