src/HOL/ex/Records.thy
author wenzelm
Thu Sep 21 15:58:13 2000 +0200 (2000-09-21)
changeset 10052 5fa8d8d5c852
child 10357 0d0cac129618
permissions -rw-r--r--
renamed HOL/ex/Points to HOL/ex/Records;
wenzelm@10052
     1
(*  Title:      HOL/ex/Records.thy
wenzelm@10052
     2
    ID:         $Id$
wenzelm@10052
     3
    Author:     Wolfgang Naraschewski and Markus Wenzel, TU Muenchen
wenzelm@10052
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@10052
     5
*)
wenzelm@10052
     6
wenzelm@10052
     7
header {* Using extensible records in HOL -- points and coloured points *}
wenzelm@10052
     8
wenzelm@10052
     9
theory Records = Main:
wenzelm@10052
    10
wenzelm@10052
    11
subsection {* Points *}
wenzelm@10052
    12
wenzelm@10052
    13
record point =
wenzelm@10052
    14
  x :: nat
wenzelm@10052
    15
  y :: nat
wenzelm@10052
    16
wenzelm@10052
    17
text {*
wenzelm@10052
    18
 Apart many other things, above record declaration produces the
wenzelm@10052
    19
 following theorems:
wenzelm@10052
    20
*}
wenzelm@10052
    21
wenzelm@10052
    22
thm "point.simps"
wenzelm@10052
    23
thm "point.iffs"
wenzelm@10052
    24
thm "point.update_defs"
wenzelm@10052
    25
wenzelm@10052
    26
text {*
wenzelm@10052
    27
 The set of theorems "point.simps" is added automatically to the
wenzelm@10052
    28
 standard simpset, "point.iffs" is added to the claset and simpset.
wenzelm@10052
    29
*}
wenzelm@10052
    30
wenzelm@10052
    31
text {*
wenzelm@10052
    32
  Record declarations define new type abbreviations:
wenzelm@10052
    33
wenzelm@10052
    34
    point = "(| x :: nat, y :: nat |)"
wenzelm@10052
    35
    'a point_scheme = "(| x :: nat, y :: nat, ... :: 'a |)"
wenzelm@10052
    36
wenzelm@10052
    37
  Extensions `...' must be in type class `more'!
wenzelm@10052
    38
*}
wenzelm@10052
    39
wenzelm@10052
    40
consts foo1 :: point
wenzelm@10052
    41
consts foo2 :: "(| x :: nat, y :: nat |)"
wenzelm@10052
    42
consts foo3 :: "'a => ('a::more) point_scheme"
wenzelm@10052
    43
consts foo4 :: "'a => (| x :: nat, y :: nat, ... :: 'a |)"
wenzelm@10052
    44
wenzelm@10052
    45
wenzelm@10052
    46
subsubsection {* Introducing concrete records and record schemes *}
wenzelm@10052
    47
wenzelm@10052
    48
defs
wenzelm@10052
    49
  foo1_def: "foo1 == (| x = 1, y = 0 |)"
wenzelm@10052
    50
  foo3_def: "foo3 ext == (| x = 1, y = 0, ... = ext |)"
wenzelm@10052
    51
wenzelm@10052
    52
wenzelm@10052
    53
subsubsection {* Record selection and record update *}
wenzelm@10052
    54
wenzelm@10052
    55
constdefs
wenzelm@10052
    56
  getX :: "('a::more) point_scheme => nat"
wenzelm@10052
    57
  "getX r == x r"
wenzelm@10052
    58
  setX :: "('a::more) point_scheme => nat => 'a point_scheme"
wenzelm@10052
    59
  "setX r n == r (| x := n |)"
wenzelm@10052
    60
wenzelm@10052
    61
wenzelm@10052
    62
subsubsection {* Some lemmas about records *}
wenzelm@10052
    63
wenzelm@10052
    64
text {* Basic simplifications *}
wenzelm@10052
    65
wenzelm@10052
    66
lemma "point.make n p = (| x = n, y = p |)"
wenzelm@10052
    67
  by simp
wenzelm@10052
    68
wenzelm@10052
    69
lemma "x (| x = m, y = n, ... = p |) = m"
wenzelm@10052
    70
  by simp
wenzelm@10052
    71
wenzelm@10052
    72
lemma "(| x = m, y = n, ... = p |) (| x:= 0 |) = (| x = 0, y = n, ... = p |)"
wenzelm@10052
    73
  by simp
wenzelm@10052
    74
wenzelm@10052
    75
wenzelm@10052
    76
text {* Equality of records *}
wenzelm@10052
    77
wenzelm@10052
    78
lemma "n = n' ==> p = p' ==> (| x = n, y = p |) = (| x = n', y = p' |)"
wenzelm@10052
    79
  -- "introduction of concrete record equality"
wenzelm@10052
    80
  by simp
wenzelm@10052
    81
wenzelm@10052
    82
lemma "(| x = n, y = p |) = (| x = n', y = p' |) ==> n = n'"
wenzelm@10052
    83
  -- "elimination of concrete record equality"
wenzelm@10052
    84
  by simp
wenzelm@10052
    85
wenzelm@10052
    86
lemma "r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)"
wenzelm@10052
    87
  -- "introduction of abstract record equality"
wenzelm@10052
    88
  by simp
wenzelm@10052
    89
wenzelm@10052
    90
lemma "r (| x := n |) = r (| x := n' |) ==> n = n'"
wenzelm@10052
    91
  -- "elimination of abstract record equality (manual proof)"
wenzelm@10052
    92
proof -
wenzelm@10052
    93
  assume "r (| x := n |) = r (| x := n' |)" (is "?lhs = ?rhs")
wenzelm@10052
    94
  hence "x ?lhs = x ?rhs" by simp
wenzelm@10052
    95
  thus ?thesis by simp
wenzelm@10052
    96
qed
wenzelm@10052
    97
wenzelm@10052
    98
wenzelm@10052
    99
text {* Surjective pairing *}
wenzelm@10052
   100
wenzelm@10052
   101
lemma "r = (| x = x r, y = y r |)"
wenzelm@10052
   102
  by simp
wenzelm@10052
   103
wenzelm@10052
   104
lemma "r = (| x = x r, y = y r, ... = more r |)"
wenzelm@10052
   105
  by simp
wenzelm@10052
   106
wenzelm@10052
   107
wenzelm@10052
   108
text {* Splitting quantifiers: the !!r is NECESSARY here *}
wenzelm@10052
   109
wenzelm@10052
   110
lemma "!!r. r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)"
wenzelm@10052
   111
proof record_split
wenzelm@10052
   112
  fix x y more
wenzelm@10052
   113
  show "(| x = x, y = y, ... = more |)(| x := n, y := m |) =
wenzelm@10052
   114
        (| x = x, y = y, ... = more |)(| y := m, x := n |)"
wenzelm@10052
   115
    by simp
wenzelm@10052
   116
qed
wenzelm@10052
   117
wenzelm@10052
   118
lemma "!!r. r (| x := n |) (| x := m |) = r (| x := m |)"
wenzelm@10052
   119
proof record_split
wenzelm@10052
   120
  fix x y more
wenzelm@10052
   121
  show "(| x = x, y = y, ... = more |)(| x := n, x := m |) =
wenzelm@10052
   122
        (| x = x, y = y, ... = more |)(| x := m |)"
wenzelm@10052
   123
    by simp
wenzelm@10052
   124
qed
wenzelm@10052
   125
wenzelm@10052
   126
wenzelm@10052
   127
wenzelm@10052
   128
text {* Concrete records are type instances of record schemes *}
wenzelm@10052
   129
wenzelm@10052
   130
constdefs
wenzelm@10052
   131
  foo5 :: nat
wenzelm@10052
   132
  "foo5 == getX (| x = 1, y = 0 |)"
wenzelm@10052
   133
wenzelm@10052
   134
wenzelm@10052
   135
text {* Manipulating the `...' (more) part *}
wenzelm@10052
   136
wenzelm@10052
   137
constdefs
wenzelm@10052
   138
  incX :: "('a::more) point_scheme => 'a point_scheme"
wenzelm@10052
   139
  "incX r == (| x = Suc (x r), y = y r, ... = point.more r |)"
wenzelm@10052
   140
wenzelm@10052
   141
lemma "!!r n. incX r = setX r (Suc (getX r))"
wenzelm@10052
   142
proof (unfold getX_def setX_def incX_def)
wenzelm@10052
   143
  show "!!r n. (| x = Suc (x r), y = y r, ... = more r |) = r(| x := Suc (x r) |)"
wenzelm@10052
   144
    by record_split simp
wenzelm@10052
   145
qed
wenzelm@10052
   146
wenzelm@10052
   147
wenzelm@10052
   148
text {* alternative definition *}
wenzelm@10052
   149
wenzelm@10052
   150
constdefs
wenzelm@10052
   151
  incX' :: "('a::more) point_scheme => 'a point_scheme"
wenzelm@10052
   152
  "incX' r == r (| x := Suc (x r) |)"
wenzelm@10052
   153
wenzelm@10052
   154
wenzelm@10052
   155
subsection {* Coloured points: record extension *}
wenzelm@10052
   156
wenzelm@10052
   157
datatype colour = Red | Green | Blue
wenzelm@10052
   158
wenzelm@10052
   159
record cpoint = point +
wenzelm@10052
   160
  colour :: colour
wenzelm@10052
   161
wenzelm@10052
   162
wenzelm@10052
   163
text {*
wenzelm@10052
   164
  The record declaration defines new type constructors:
wenzelm@10052
   165
wenzelm@10052
   166
    cpoint = (| x :: nat, y :: nat, colour :: colour |)
wenzelm@10052
   167
    'a cpoint_scheme = (| x :: nat, y :: nat, colour :: colour, ... :: 'a |)
wenzelm@10052
   168
*}
wenzelm@10052
   169
wenzelm@10052
   170
consts foo6 :: cpoint
wenzelm@10052
   171
consts foo7 :: "(| x :: nat, y :: nat, colour :: colour |)"
wenzelm@10052
   172
consts foo8 :: "('a::more) cpoint_scheme"
wenzelm@10052
   173
consts foo9 :: "(| x :: nat, y :: nat, colour :: colour, ... :: 'a |)"
wenzelm@10052
   174
wenzelm@10052
   175
wenzelm@10052
   176
text {* Functions on point schemes work for cpoints as well *}
wenzelm@10052
   177
wenzelm@10052
   178
constdefs
wenzelm@10052
   179
  foo10 :: nat
wenzelm@10052
   180
  "foo10 == getX (| x = 2, y = 0, colour = Blue |)"
wenzelm@10052
   181
wenzelm@10052
   182
wenzelm@10052
   183
subsubsection {* Non-coercive structural subtyping *}
wenzelm@10052
   184
wenzelm@10052
   185
text {* foo11 has type cpoint, not type point --- Great! *}
wenzelm@10052
   186
wenzelm@10052
   187
constdefs
wenzelm@10052
   188
  foo11 :: cpoint
wenzelm@10052
   189
  "foo11 == setX (| x = 2, y = 0, colour = Blue |) 0"
wenzelm@10052
   190
wenzelm@10052
   191
wenzelm@10052
   192
subsection {* Other features *}
wenzelm@10052
   193
wenzelm@10052
   194
text {* field names contribute to record identity *}
wenzelm@10052
   195
wenzelm@10052
   196
record point' =
wenzelm@10052
   197
  x' :: nat
wenzelm@10052
   198
  y' :: nat
wenzelm@10052
   199
wenzelm@10052
   200
text {* May not apply @{term getX} to @{term "(| x' = 2, y' = 0 |)"} *}
wenzelm@10052
   201
wenzelm@10052
   202
wenzelm@10052
   203
text {* Polymorphic records *}
wenzelm@10052
   204
wenzelm@10052
   205
record 'a point'' = point +
wenzelm@10052
   206
  content :: 'a
wenzelm@10052
   207
wenzelm@10052
   208
types cpoint'' = "colour point''"
wenzelm@10052
   209
wenzelm@10052
   210
end