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