src/HOL/ex/Records.thy
author wenzelm
Mon Oct 30 18:24:42 2000 +0100 (2000-10-30)
changeset 10357 0d0cac129618
parent 10052 5fa8d8d5c852
child 11701 3d51fbf81c17
permissions -rw-r--r--
tuned;
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@10357
    27
 The set of theorems @{thm [source] point.simps} is added
wenzelm@10357
    28
 automatically to the standard simpset, @{thm [source] point.iffs} is
wenzelm@10357
    29
 added to the Classical Reasoner and Simplifier context.
wenzelm@10052
    30
*}
wenzelm@10052
    31
wenzelm@10052
    32
text {*
wenzelm@10052
    33
  Record declarations define new type abbreviations:
wenzelm@10357
    34
  @{text [display]
wenzelm@10357
    35
"    point = (| x :: nat, y :: nat |)
wenzelm@10357
    36
    'a point_scheme = (| x :: nat, y :: nat, ... :: 'a |)"}
wenzelm@10357
    37
  Extensions `...' must be in type class @{text 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@10357
    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@10357
    76
text {* \medskip 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@10357
    99
text {* \medskip 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@10357
   108
text {*
wenzelm@10357
   109
 \medskip Splitting quantifiers: the @{text "!!r"} is \emph{necessary}
wenzelm@10357
   110
 here!
wenzelm@10357
   111
*}
wenzelm@10052
   112
wenzelm@10052
   113
lemma "!!r. r (| x := n |) (| y := m |) = r (| y := m |) (| x := n |)"
wenzelm@10052
   114
proof record_split
wenzelm@10052
   115
  fix x y more
wenzelm@10052
   116
  show "(| x = x, y = y, ... = more |)(| x := n, y := m |) =
wenzelm@10052
   117
        (| x = x, y = y, ... = more |)(| y := m, x := n |)"
wenzelm@10052
   118
    by simp
wenzelm@10052
   119
qed
wenzelm@10052
   120
wenzelm@10052
   121
lemma "!!r. r (| x := n |) (| x := m |) = r (| x := m |)"
wenzelm@10052
   122
proof record_split
wenzelm@10052
   123
  fix x y more
wenzelm@10052
   124
  show "(| x = x, y = y, ... = more |)(| x := n, x := m |) =
wenzelm@10052
   125
        (| x = x, y = y, ... = more |)(| x := m |)"
wenzelm@10052
   126
    by simp
wenzelm@10052
   127
qed
wenzelm@10052
   128
wenzelm@10052
   129
wenzelm@10357
   130
text {*
wenzelm@10357
   131
 \medskip Concrete records are type instances of record schemes.
wenzelm@10357
   132
*}
wenzelm@10052
   133
wenzelm@10052
   134
constdefs
wenzelm@10052
   135
  foo5 :: nat
wenzelm@10052
   136
  "foo5 == getX (| x = 1, y = 0 |)"
wenzelm@10052
   137
wenzelm@10052
   138
wenzelm@10357
   139
text {* \medskip Manipulating the `...' (more) part. *}
wenzelm@10052
   140
wenzelm@10052
   141
constdefs
wenzelm@10052
   142
  incX :: "('a::more) point_scheme => 'a point_scheme"
wenzelm@10052
   143
  "incX r == (| x = Suc (x r), y = y r, ... = point.more r |)"
wenzelm@10052
   144
wenzelm@10052
   145
lemma "!!r n. incX r = setX r (Suc (getX r))"
wenzelm@10052
   146
proof (unfold getX_def setX_def incX_def)
wenzelm@10052
   147
  show "!!r n. (| x = Suc (x r), y = y r, ... = more r |) = r(| x := Suc (x r) |)"
wenzelm@10052
   148
    by record_split simp
wenzelm@10052
   149
qed
wenzelm@10052
   150
wenzelm@10357
   151
text {* An alternative definition. *}
wenzelm@10052
   152
wenzelm@10052
   153
constdefs
wenzelm@10052
   154
  incX' :: "('a::more) point_scheme => 'a point_scheme"
wenzelm@10052
   155
  "incX' r == r (| x := Suc (x r) |)"
wenzelm@10052
   156
wenzelm@10052
   157
wenzelm@10052
   158
subsection {* Coloured points: record extension *}
wenzelm@10052
   159
wenzelm@10052
   160
datatype colour = Red | Green | Blue
wenzelm@10052
   161
wenzelm@10052
   162
record cpoint = point +
wenzelm@10052
   163
  colour :: colour
wenzelm@10052
   164
wenzelm@10052
   165
wenzelm@10052
   166
text {*
wenzelm@10052
   167
  The record declaration defines new type constructors:
wenzelm@10357
   168
  @{text [display]
wenzelm@10357
   169
"    cpoint = (| x :: nat, y :: nat, colour :: colour |)
wenzelm@10357
   170
    'a cpoint_scheme = (| x :: nat, y :: nat, colour :: colour, ... :: 'a |)"}
wenzelm@10052
   171
*}
wenzelm@10052
   172
wenzelm@10052
   173
consts foo6 :: cpoint
wenzelm@10052
   174
consts foo7 :: "(| x :: nat, y :: nat, colour :: colour |)"
wenzelm@10052
   175
consts foo8 :: "('a::more) cpoint_scheme"
wenzelm@10052
   176
consts foo9 :: "(| x :: nat, y :: nat, colour :: colour, ... :: 'a |)"
wenzelm@10052
   177
wenzelm@10052
   178
wenzelm@10357
   179
text {*
wenzelm@10357
   180
 Functions on @{text point} schemes work for @{text cpoints} as well.
wenzelm@10357
   181
*}
wenzelm@10052
   182
wenzelm@10052
   183
constdefs
wenzelm@10052
   184
  foo10 :: nat
wenzelm@10052
   185
  "foo10 == getX (| x = 2, y = 0, colour = Blue |)"
wenzelm@10052
   186
wenzelm@10052
   187
wenzelm@10052
   188
subsubsection {* Non-coercive structural subtyping *}
wenzelm@10052
   189
wenzelm@10357
   190
text {*
wenzelm@10357
   191
 Term @{term foo11} has type @{typ cpoint}, not type @{typ point} ---
wenzelm@10357
   192
 Great!
wenzelm@10357
   193
*}
wenzelm@10052
   194
wenzelm@10052
   195
constdefs
wenzelm@10052
   196
  foo11 :: cpoint
wenzelm@10052
   197
  "foo11 == setX (| x = 2, y = 0, colour = Blue |) 0"
wenzelm@10052
   198
wenzelm@10052
   199
wenzelm@10052
   200
subsection {* Other features *}
wenzelm@10052
   201
wenzelm@10357
   202
text {* Field names contribute to record identity. *}
wenzelm@10052
   203
wenzelm@10052
   204
record point' =
wenzelm@10052
   205
  x' :: nat
wenzelm@10052
   206
  y' :: nat
wenzelm@10052
   207
wenzelm@10357
   208
text {*
wenzelm@10357
   209
 \noindent May not apply @{term getX} to 
wenzelm@10357
   210
 @{term [source] "(| x' = 2, y' = 0 |)"}.
wenzelm@10357
   211
*}
wenzelm@10052
   212
wenzelm@10357
   213
text {* \medskip Polymorphic records. *}
wenzelm@10052
   214
wenzelm@10052
   215
record 'a point'' = point +
wenzelm@10052
   216
  content :: 'a
wenzelm@10052
   217
wenzelm@10052
   218
types cpoint'' = "colour point''"
wenzelm@10052
   219
wenzelm@10052
   220
end