doc-src/TutorialI/Types/document/Records.tex
author wenzelm
Thu, 20 Dec 2001 21:14:59 +0100
changeset 12572 f8ad8cfb8309
child 12585 e3cb192aa6ee
permissions -rw-r--r--
generated text;
wenzelm@12572
     1
%
wenzelm@12572
     2
\begin{isabellebody}%
wenzelm@12572
     3
\def\isabellecontext{Records}%
wenzelm@12572
     4
%
wenzelm@12572
     5
\isamarkupheader{Records \label{sec:records}%
wenzelm@12572
     6
}
wenzelm@12572
     7
\isamarkuptrue%
wenzelm@12572
     8
\isamarkupfalse%
wenzelm@12572
     9
%
wenzelm@12572
    10
\begin{isamarkuptext}%
wenzelm@12572
    11
\index{records|(}%
wenzelm@12572
    12
  Records are familiar from programming languages.  A record of $n$
wenzelm@12572
    13
  fields is essentially an $n$-tuple, but the record's components have
wenzelm@12572
    14
  names, which can make expressions easier to read and reduces the
wenzelm@12572
    15
  risk of confusing one field for another.
wenzelm@12572
    16
wenzelm@12572
    17
  A basic Isabelle record covers a certain set of fields, with select
wenzelm@12572
    18
  and update operations.  Each field has a specified type, which may
wenzelm@12572
    19
  be polymorphic.  The field names are part of the record type, and
wenzelm@12572
    20
  the order of the fields is significant --- as it is in Pascal but
wenzelm@12572
    21
  not in Standard ML.  If two different record types have field names
wenzelm@12572
    22
  in common, then the ambiguity is resolved in the usual way, by
wenzelm@12572
    23
  qualified names.
wenzelm@12572
    24
wenzelm@12572
    25
  Record types can also be defined by extending other record types.
wenzelm@12572
    26
  Extensible records make use of the reserved pseudo-field \cdx{more},
wenzelm@12572
    27
  which is present in every record type.  Generic record operations
wenzelm@12572
    28
  work on all possible extensions of a given type scheme; naive
wenzelm@12572
    29
  polymorphism takes care of structural sub-typing behind the scenes.
wenzelm@12572
    30
  There are also explicit coercion functions between fixed record
wenzelm@12572
    31
  types.%
wenzelm@12572
    32
\end{isamarkuptext}%
wenzelm@12572
    33
\isamarkuptrue%
wenzelm@12572
    34
%
wenzelm@12572
    35
\isamarkupsubsection{Record Basics%
wenzelm@12572
    36
}
wenzelm@12572
    37
\isamarkuptrue%
wenzelm@12572
    38
%
wenzelm@12572
    39
\begin{isamarkuptext}%
wenzelm@12572
    40
Record types are not primitive in Isabelle and have a subtle
wenzelm@12572
    41
  internal representation based on nested copies of the primitive
wenzelm@12572
    42
  product type.  A \commdx{record} declaration introduces a new record
wenzelm@12572
    43
  type scheme by specifying its fields, which are packaged internally
wenzelm@12572
    44
  to hold up the perception of records as a separate concept.%
wenzelm@12572
    45
\end{isamarkuptext}%
wenzelm@12572
    46
\isamarkuptrue%
wenzelm@12572
    47
\isacommand{record}\ point\ {\isacharequal}\isanewline
wenzelm@12572
    48
\ \ Xcoord\ {\isacharcolon}{\isacharcolon}\ int\isanewline
wenzelm@12572
    49
\ \ Ycoord\ {\isacharcolon}{\isacharcolon}\ int\isamarkupfalse%
wenzelm@12572
    50
%
wenzelm@12572
    51
\begin{isamarkuptext}%
wenzelm@12572
    52
Records of type \isa{point} have two fields named \isa{Xcoord}
wenzelm@12572
    53
  and \isa{Ycoord}, both of type~\isa{int}.  We now define a
wenzelm@12572
    54
  constant of type \isa{point}:%
wenzelm@12572
    55
\end{isamarkuptext}%
wenzelm@12572
    56
\isamarkuptrue%
wenzelm@12572
    57
\isacommand{constdefs}\isanewline
wenzelm@12572
    58
\ \ pt{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ point\isanewline
wenzelm@12572
    59
\ \ {\isachardoublequote}pt{\isadigit{1}}\ {\isasymequiv}\ {\isacharparenleft}{\isacharbar}\ Xcoord\ {\isacharequal}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharcomma}\ Ycoord\ {\isacharequal}\ {\isadigit{2}}{\isadigit{3}}\ {\isacharbar}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
    60
%
wenzelm@12572
    61
\begin{isamarkuptext}%
wenzelm@12572
    62
We see above the ASCII notation for record brackets.  You can also
wenzelm@12572
    63
  use the symbolic brackets \isa{{\isasymlparr}} and \isa{{\isasymrparr}}.  Record type
wenzelm@12572
    64
  expressions can be written directly as well, without referring to
wenzelm@12572
    65
  previously declared names (which happen to be mere type
wenzelm@12572
    66
  abbreviations):%
wenzelm@12572
    67
\end{isamarkuptext}%
wenzelm@12572
    68
\isamarkuptrue%
wenzelm@12572
    69
\isacommand{constdefs}\isanewline
wenzelm@12572
    70
\ \ pt{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharcolon}\ int{\isacharcomma}\ Ycoord\ {\isacharcolon}{\isacharcolon}\ int{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
    71
\ \ {\isachardoublequote}pt{\isadigit{2}}\ {\isasymequiv}\ {\isasymlparr}Xcoord\ {\isacharequal}\ {\isacharminus}{\isadigit{4}}{\isadigit{5}}{\isacharcomma}\ Ycoord\ {\isacharequal}\ {\isadigit{9}}{\isadigit{7}}{\isasymrparr}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
    72
%
wenzelm@12572
    73
\begin{isamarkuptext}%
wenzelm@12572
    74
For each field, there is a \emph{selector} function of the same
wenzelm@12572
    75
  name.  For example, if \isa{p} has type \isa{point} then \isa{Xcoord\ p} denotes the value of the \isa{Xcoord} field of~\isa{p}.  Expressions involving field selection of explicit records are
wenzelm@12572
    76
  simplified automatically:%
wenzelm@12572
    77
\end{isamarkuptext}%
wenzelm@12572
    78
\isamarkuptrue%
wenzelm@12572
    79
\isacommand{lemma}\ {\isachardoublequote}Xcoord\ {\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isasymrparr}\ {\isacharequal}\ a{\isachardoublequote}\isanewline
wenzelm@12572
    80
\ \ \isamarkupfalse%
wenzelm@12572
    81
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
    82
%
wenzelm@12572
    83
\begin{isamarkuptext}%
wenzelm@12572
    84
The \emph{update} operation is functional.  For example, \isa{p{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ {\isadigit{0}}{\isasymrparr}} is a record whose \isa{Xcoord} value is zero
wenzelm@12572
    85
  and whose \isa{Ycoord} value is copied from~\isa{p}.  Updates
wenzelm@12572
    86
  are also simplified automatically:%
wenzelm@12572
    87
\end{isamarkuptext}%
wenzelm@12572
    88
\isamarkuptrue%
wenzelm@12572
    89
\isacommand{lemma}\ {\isachardoublequote}{\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isasymrparr}{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ {\isadigit{0}}{\isasymrparr}\ {\isacharequal}\isanewline
wenzelm@12572
    90
\ \ \ \ {\isasymlparr}Xcoord\ {\isacharequal}\ {\isadigit{0}}{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
    91
\ \ \isamarkupfalse%
wenzelm@12572
    92
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
    93
%
wenzelm@12572
    94
\begin{isamarkuptext}%
wenzelm@12572
    95
\begin{warn}
wenzelm@12572
    96
  Field names are declared as constants and can no longer be used as
wenzelm@12572
    97
  variables.  It would be unwise, for example, to call the fields of
wenzelm@12572
    98
  type \isa{point} simply \isa{x} and~\isa{y}.  Each record
wenzelm@12572
    99
  declaration introduces a constant \cdx{more}.
wenzelm@12572
   100
  \end{warn}%
wenzelm@12572
   101
\end{isamarkuptext}%
wenzelm@12572
   102
\isamarkuptrue%
wenzelm@12572
   103
%
wenzelm@12572
   104
\isamarkupsubsection{Extensible Records and Generic Operations%
wenzelm@12572
   105
}
wenzelm@12572
   106
\isamarkuptrue%
wenzelm@12572
   107
%
wenzelm@12572
   108
\begin{isamarkuptext}%
wenzelm@12572
   109
\index{records!extensible|(}%
wenzelm@12572
   110
wenzelm@12572
   111
  Now, let us define coloured points (type \isa{cpoint}) to be
wenzelm@12572
   112
  points extended with a field \isa{col} of type \isa{colour}:%
wenzelm@12572
   113
\end{isamarkuptext}%
wenzelm@12572
   114
\isamarkuptrue%
wenzelm@12572
   115
\isacommand{datatype}\ colour\ {\isacharequal}\ Red\ {\isacharbar}\ Green\ {\isacharbar}\ Blue\isanewline
wenzelm@12572
   116
\isanewline
wenzelm@12572
   117
\isamarkupfalse%
wenzelm@12572
   118
\isacommand{record}\ cpoint\ {\isacharequal}\ point\ {\isacharplus}\isanewline
wenzelm@12572
   119
\ \ col\ {\isacharcolon}{\isacharcolon}\ colour\isamarkupfalse%
wenzelm@12572
   120
%
wenzelm@12572
   121
\begin{isamarkuptext}%
wenzelm@12572
   122
The fields of this new type are \isa{Xcoord}, \isa{Ycoord} and
wenzelm@12572
   123
  \isa{col}, in that order:%
wenzelm@12572
   124
\end{isamarkuptext}%
wenzelm@12572
   125
\isamarkuptrue%
wenzelm@12572
   126
\isacommand{constdefs}\isanewline
wenzelm@12572
   127
\ \ cpt{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ cpoint\isanewline
wenzelm@12572
   128
\ \ {\isachardoublequote}cpt{\isadigit{1}}\ {\isasymequiv}\ {\isasymlparr}Xcoord\ {\isacharequal}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}{\isacharcomma}\ Ycoord\ {\isacharequal}\ {\isadigit{2}}{\isadigit{3}}{\isacharcomma}\ col\ {\isacharequal}\ Green{\isasymrparr}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
   129
%
wenzelm@12572
   130
\begin{isamarkuptext}%
wenzelm@12572
   131
We can define generic operations that work on arbitrary instances of
wenzelm@12572
   132
  a record scheme, e.g.\ covering \isa{point}, \isa{cpoint} and any
wenzelm@12572
   133
  further extensions.  Every record structure has an implicit
wenzelm@12572
   134
  pseudo-field, \cdx{more}, that keeps the extension as an explicit
wenzelm@12572
   135
  value.  Its type is declared as completely polymorphic:~\isa{{\isacharprime}a}.
wenzelm@12572
   136
  When a fixed record value is expressed using just its standard
wenzelm@12572
   137
  fields, the value of \isa{more} is implicitly set to \isa{{\isacharparenleft}{\isacharparenright}},
wenzelm@12572
   138
  the empty tuple, which has type \isa{unit}.  Within the record
wenzelm@12572
   139
  brackets, you can refer to the \isa{more} field by writing \isa{{\isasymdots}} (three dots):%
wenzelm@12572
   140
\end{isamarkuptext}%
wenzelm@12572
   141
\isamarkuptrue%
wenzelm@12572
   142
\isacommand{lemma}\ {\isachardoublequote}Xcoord\ {\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ p{\isasymrparr}\ {\isacharequal}\ a{\isachardoublequote}\isanewline
wenzelm@12572
   143
\ \ \isamarkupfalse%
wenzelm@12572
   144
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
   145
%
wenzelm@12572
   146
\begin{isamarkuptext}%
wenzelm@12572
   147
This lemma applies to any record whose first two fields are \isa{Xcoord} and~\isa{Ycoord}.  Note that \isa{{\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ {\isacharparenleft}{\isacharparenright}{\isasymrparr}} is actually the same as \isa{{\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isasymrparr}}.
wenzelm@12572
   148
wenzelm@12572
   149
  The pseudo-field \isa{more} can be selected in the usual way, but
wenzelm@12572
   150
  the identifier must be qualified:%
wenzelm@12572
   151
\end{isamarkuptext}%
wenzelm@12572
   152
\isamarkuptrue%
wenzelm@12572
   153
\isacommand{lemma}\ {\isachardoublequote}point{\isachardot}more\ cpt{\isadigit{1}}\ {\isacharequal}\ {\isasymlparr}col\ {\isacharequal}\ Green{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
   154
\ \ \isamarkupfalse%
wenzelm@12572
   155
\isacommand{by}\ {\isacharparenleft}simp\ add{\isacharcolon}\ cpt{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   156
%
wenzelm@12572
   157
\begin{isamarkuptext}%
wenzelm@12572
   158
We see that the colour attached to this \isa{point} is a
wenzelm@12572
   159
  (rudimentary) record in its own right, namely \isa{{\isasymlparr}col\ {\isacharequal}\ Green{\isasymrparr}}.  In order to select or update \isa{col} in the above
wenzelm@12572
   160
  fragment, \isa{{\isasymlparr}col\ {\isacharequal}\ Green{\isasymrparr}} needs to be put back into the
wenzelm@12572
   161
  context of its parent type scheme, say as \isa{more} part of a
wenzelm@12572
   162
  \isa{point}.
wenzelm@12572
   163
wenzelm@12572
   164
  To define generic operations, we need to know a bit more about
wenzelm@12572
   165
  records.  Our declaration of \isa{point} above generated two type
wenzelm@12572
   166
  abbreviations:
wenzelm@12572
   167
wenzelm@12572
   168
  \smallskip
wenzelm@12572
   169
  \begin{tabular}{l}
wenzelm@12572
   170
  \isa{point}~\isa{{\isacharequal}}~\isa{{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharcolon}\ int{\isacharcomma}\ Ycoord\ {\isacharcolon}{\isacharcolon}\ int{\isasymrparr}} \\
wenzelm@12572
   171
  \isa{{\isacharprime}a\ point{\isacharunderscore}scheme}~\isa{{\isacharequal}}~\isa{{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharcolon}\ int{\isacharcomma}\ Ycoord\ {\isacharcolon}{\isacharcolon}\ int{\isacharcomma}\ {\isasymdots}\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a{\isasymrparr}} \\
wenzelm@12572
   172
  \end{tabular}
wenzelm@12572
   173
  \smallskip
wenzelm@12572
   174
wenzelm@12572
   175
  Type \isa{point} is for rigid records having exactly the two fields
wenzelm@12572
   176
  \isa{Xcoord} and~\isa{Ycoord}, while the polymorphic type \isa{{\isacharprime}a\ point{\isacharunderscore}scheme} comprises all possible extensions to those two
wenzelm@12572
   177
  fields, recall that \isa{unit\ point{\isacharunderscore}scheme} coincides with \isa{point}.  For example, let us define two operations --- methods, if
wenzelm@12572
   178
  we regard records as objects --- to get and set any point's \isa{Xcoord} field.%
wenzelm@12572
   179
\end{isamarkuptext}%
wenzelm@12572
   180
\isamarkuptrue%
wenzelm@12572
   181
\isacommand{constdefs}\isanewline
wenzelm@12572
   182
\ \ getX\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ point{\isacharunderscore}scheme\ {\isasymRightarrow}\ int{\isachardoublequote}\isanewline
wenzelm@12572
   183
\ \ {\isachardoublequote}getX\ r\ {\isasymequiv}\ Xcoord\ r{\isachardoublequote}\isanewline
wenzelm@12572
   184
\ \ setX\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ point{\isacharunderscore}scheme\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ {\isacharprime}a\ point{\isacharunderscore}scheme{\isachardoublequote}\isanewline
wenzelm@12572
   185
\ \ {\isachardoublequote}setX\ r\ a\ {\isasymequiv}\ r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
   186
%
wenzelm@12572
   187
\begin{isamarkuptext}%
wenzelm@12572
   188
Here is a generic method that modifies a point, incrementing its
wenzelm@12572
   189
  \isa{Xcoord} field.  The \isa{Ycoord} and \isa{more} fields
wenzelm@12572
   190
  are copied across.  It works for any record type scheme derived from
wenzelm@12572
   191
  \isa{point}, such as \isa{cpoint}:%
wenzelm@12572
   192
\end{isamarkuptext}%
wenzelm@12572
   193
\isamarkuptrue%
wenzelm@12572
   194
\isacommand{constdefs}\isanewline
wenzelm@12572
   195
\ \ incX\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ point{\isacharunderscore}scheme\ {\isasymRightarrow}\ {\isacharprime}a\ point{\isacharunderscore}scheme{\isachardoublequote}\isanewline
wenzelm@12572
   196
\ \ {\isachardoublequote}incX\ r\ {\isasymequiv}\ {\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ r\ {\isacharplus}\ {\isadigit{1}}{\isacharcomma}\isanewline
wenzelm@12572
   197
\ \ \ \ Ycoord\ {\isacharequal}\ Ycoord\ r{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ point{\isachardot}more\ r{\isasymrparr}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
   198
%
wenzelm@12572
   199
\begin{isamarkuptext}%
wenzelm@12572
   200
Generic theorems can be proved about generic methods.  This trivial
wenzelm@12572
   201
  lemma relates \isa{incX} to \isa{getX} and \isa{setX}:%
wenzelm@12572
   202
\end{isamarkuptext}%
wenzelm@12572
   203
\isamarkuptrue%
wenzelm@12572
   204
\isacommand{lemma}\ {\isachardoublequote}incX\ r\ {\isacharequal}\ setX\ r\ {\isacharparenleft}getX\ r\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@12572
   205
\ \ \isamarkupfalse%
wenzelm@12572
   206
\isacommand{by}\ {\isacharparenleft}simp\ add{\isacharcolon}\ getX{\isacharunderscore}def\ setX{\isacharunderscore}def\ incX{\isacharunderscore}def{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   207
%
wenzelm@12572
   208
\begin{isamarkuptext}%
wenzelm@12572
   209
\begin{warn}
wenzelm@12572
   210
  If you use the symbolic record brackets \isa{{\isasymlparr}} and \isa{{\isasymrparr}},
wenzelm@12572
   211
  then you must also use the symbolic ellipsis, ``\isa{{\isasymdots}}'', rather
wenzelm@12572
   212
  than three consecutive periods, ``\isa{{\isachardot}{\isachardot}{\isachardot}}''.  Mixing the ASCII
wenzelm@12572
   213
  and symbolic versions causes a syntax error.  (The two versions are
wenzelm@12572
   214
  more distinct on screen than they are on paper.)
wenzelm@12572
   215
  \end{warn}%\index{records!extensible|)}%
wenzelm@12572
   216
\end{isamarkuptext}%
wenzelm@12572
   217
\isamarkuptrue%
wenzelm@12572
   218
%
wenzelm@12572
   219
\isamarkupsubsection{Record Equality%
wenzelm@12572
   220
}
wenzelm@12572
   221
\isamarkuptrue%
wenzelm@12572
   222
%
wenzelm@12572
   223
\begin{isamarkuptext}%
wenzelm@12572
   224
Two records are equal\index{equality!of records} if all pairs of
wenzelm@12572
   225
  corresponding fields are equal.  Record equalities are simplified
wenzelm@12572
   226
  automatically:%
wenzelm@12572
   227
\end{isamarkuptext}%
wenzelm@12572
   228
\isamarkuptrue%
wenzelm@12572
   229
\isacommand{lemma}\ {\isachardoublequote}{\isacharparenleft}{\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isasymrparr}\ {\isacharequal}\ {\isasymlparr}Xcoord\ {\isacharequal}\ a{\isacharprime}{\isacharcomma}\ Ycoord\ {\isacharequal}\ b{\isacharprime}{\isasymrparr}{\isacharparenright}\ {\isacharequal}\isanewline
wenzelm@12572
   230
\ \ \ \ {\isacharparenleft}a\ {\isacharequal}\ a{\isacharprime}\ {\isasymand}\ b\ {\isacharequal}\ b{\isacharprime}{\isacharparenright}{\isachardoublequote}\isanewline
wenzelm@12572
   231
\ \ \isamarkupfalse%
wenzelm@12572
   232
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
   233
%
wenzelm@12572
   234
\begin{isamarkuptext}%
wenzelm@12572
   235
The following equality is similar, but generic, in that \isa{r}
wenzelm@12572
   236
  can be any instance of \isa{point{\isacharunderscore}scheme}:%
wenzelm@12572
   237
\end{isamarkuptext}%
wenzelm@12572
   238
\isamarkuptrue%
wenzelm@12572
   239
\isacommand{lemma}\ {\isachardoublequote}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharcolon}{\isacharequal}\ b{\isasymrparr}\ {\isacharequal}\ r{\isasymlparr}Ycoord\ {\isacharcolon}{\isacharequal}\ b{\isacharcomma}\ Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
   240
\ \ \isamarkupfalse%
wenzelm@12572
   241
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
   242
%
wenzelm@12572
   243
\begin{isamarkuptext}%
wenzelm@12572
   244
We see above the syntax for iterated updates.  We could equivalently
wenzelm@12572
   245
  have written the left-hand side as \isa{r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharcomma}\ Ycoord\ {\isacharcolon}{\isacharequal}\ b{\isasymrparr}}.
wenzelm@12572
   246
wenzelm@12572
   247
  Record equality is \emph{extensional}: \index{extensionality!for
wenzelm@12572
   248
  records} a record is determined entirely by the values of its
wenzelm@12572
   249
  fields.%
wenzelm@12572
   250
\end{isamarkuptext}%
wenzelm@12572
   251
\isamarkuptrue%
wenzelm@12572
   252
\isacommand{lemma}\ {\isachardoublequote}r\ {\isacharequal}\ {\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ r{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
   253
\ \ \isamarkupfalse%
wenzelm@12572
   254
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
   255
%
wenzelm@12572
   256
\begin{isamarkuptext}%
wenzelm@12572
   257
The generic version of this equality includes the field \isa{more}:%
wenzelm@12572
   258
\end{isamarkuptext}%
wenzelm@12572
   259
\isamarkuptrue%
wenzelm@12572
   260
\isacommand{lemma}\ {\isachardoublequote}r\ {\isacharequal}\ {\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ r{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ point{\isachardot}more\ r{\isasymrparr}{\isachardoublequote}\isanewline
wenzelm@12572
   261
\ \ \isamarkupfalse%
wenzelm@12572
   262
\isacommand{by}\ simp\isamarkupfalse%
wenzelm@12572
   263
%
wenzelm@12572
   264
\begin{isamarkuptext}%
wenzelm@12572
   265
Note that the \isa{r} is of a different (more general) type than
wenzelm@12572
   266
  the previous one.
wenzelm@12572
   267
wenzelm@12572
   268
  \medskip The simplifier can prove many record equalities
wenzelm@12572
   269
  automatically, but general equality reasoning can be tricky.
wenzelm@12572
   270
  Consider proving this obvious fact:%
wenzelm@12572
   271
\end{isamarkuptext}%
wenzelm@12572
   272
\isamarkuptrue%
wenzelm@12572
   273
\isacommand{lemma}\ {\isachardoublequote}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}\ {\isacharequal}\ r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharprime}{\isasymrparr}\ {\isasymLongrightarrow}\ a\ {\isacharequal}\ a{\isacharprime}{\isachardoublequote}\isanewline
wenzelm@12572
   274
\ \ \isamarkupfalse%
wenzelm@12572
   275
\isacommand{apply}\ simp{\isacharquery}\isanewline
wenzelm@12572
   276
\ \ \isamarkupfalse%
wenzelm@12572
   277
\isacommand{oops}\isamarkupfalse%
wenzelm@12572
   278
%
wenzelm@12572
   279
\begin{isamarkuptext}%
wenzelm@12572
   280
The simplifier can do nothing, since general record equality is not
wenzelm@12572
   281
  eliminated automatically.  One way to proceed is by an explicit
wenzelm@12572
   282
  forward step that applies the selector \isa{Xcoord} to both sides
wenzelm@12572
   283
  of the assumed record equality:%
wenzelm@12572
   284
\end{isamarkuptext}%
wenzelm@12572
   285
\isamarkuptrue%
wenzelm@12572
   286
\isacommand{lemma}\ {\isachardoublequote}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}\ {\isacharequal}\ r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharprime}{\isasymrparr}\ {\isasymLongrightarrow}\ a\ {\isacharequal}\ a{\isacharprime}{\isachardoublequote}\isanewline
wenzelm@12572
   287
\ \ \isamarkupfalse%
wenzelm@12572
   288
\isacommand{apply}\ {\isacharparenleft}drule{\isacharunderscore}tac\ f\ {\isacharequal}\ Xcoord\ \isakeyword{in}\ arg{\isacharunderscore}cong{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   289
%
wenzelm@12572
   290
\begin{isamarkuptxt}%
wenzelm@12572
   291
\begin{isabelle}%
wenzelm@12572
   292
\ {\isadigit{1}}{\isachardot}\ Xcoord\ {\isacharparenleft}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}{\isacharparenright}\ {\isacharequal}\ Xcoord\ {\isacharparenleft}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharprime}{\isasymrparr}{\isacharparenright}\ {\isasymLongrightarrow}\ a\ {\isacharequal}\ a{\isacharprime}%
wenzelm@12572
   293
\end{isabelle}
wenzelm@12572
   294
    Now, \isa{simp} will reduce the assumption to the desired
wenzelm@12572
   295
    conclusion.%
wenzelm@12572
   296
\end{isamarkuptxt}%
wenzelm@12572
   297
\ \ \isamarkuptrue%
wenzelm@12572
   298
\isacommand{apply}\ simp\isanewline
wenzelm@12572
   299
\ \ \isamarkupfalse%
wenzelm@12572
   300
\isacommand{done}\isamarkupfalse%
wenzelm@12572
   301
%
wenzelm@12572
   302
\begin{isamarkuptext}%
wenzelm@12572
   303
The \isa{cases} method is preferable to such a forward proof.
wenzelm@12572
   304
  State the desired lemma again:%
wenzelm@12572
   305
\end{isamarkuptext}%
wenzelm@12572
   306
\isamarkuptrue%
wenzelm@12572
   307
\isacommand{lemma}\ {\isachardoublequote}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}\ {\isacharequal}\ r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharprime}{\isasymrparr}\ {\isasymLongrightarrow}\ a\ {\isacharequal}\ a{\isacharprime}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
   308
%
wenzelm@12572
   309
\begin{isamarkuptxt}%
wenzelm@12572
   310
The \methdx{cases} method adds an equality to replace the named
wenzelm@12572
   311
    record variable by an explicit record, listing all fields.  It
wenzelm@12572
   312
    even includes the pseudo-field \isa{more}, since the record
wenzelm@12572
   313
    equality stated above is generic.%
wenzelm@12572
   314
\end{isamarkuptxt}%
wenzelm@12572
   315
\ \ \isamarkuptrue%
wenzelm@12572
   316
\isacommand{apply}\ {\isacharparenleft}cases\ r{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   317
%
wenzelm@12572
   318
\begin{isamarkuptxt}%
wenzelm@12572
   319
\begin{isabelle}%
wenzelm@12572
   320
\ {\isadigit{1}}{\isachardot}\ {\isasymAnd}Xcoord\ Ycoord\ more{\isachardot}\isanewline
wenzelm@12572
   321
\isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }{\isasymlbrakk}r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isasymrparr}\ {\isacharequal}\ r{\isasymlparr}Xcoord\ {\isacharcolon}{\isacharequal}\ a{\isacharprime}{\isasymrparr}{\isacharsemicolon}\isanewline
wenzelm@12572
   322
\isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ \ \ \ }r\ {\isacharequal}\ {\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ more{\isasymrparr}{\isasymrbrakk}\isanewline
wenzelm@12572
   323
\isaindent{\ {\isadigit{1}}{\isachardot}\ \ \ \ }{\isasymLongrightarrow}\ a\ {\isacharequal}\ a{\isacharprime}%
wenzelm@12572
   324
\end{isabelle}
wenzelm@12572
   325
    Again, \isa{simp} finishes the proof.  Because \isa{r} has
wenzelm@12572
   326
    become an explicit record expression, the updates can be applied
wenzelm@12572
   327
    and the record equality can be replaced by equality of the
wenzelm@12572
   328
    corresponding fields (due to injectivity).%
wenzelm@12572
   329
\end{isamarkuptxt}%
wenzelm@12572
   330
\ \ \isamarkuptrue%
wenzelm@12572
   331
\isacommand{apply}\ simp\isanewline
wenzelm@12572
   332
\ \ \isamarkupfalse%
wenzelm@12572
   333
\isacommand{done}\isamarkupfalse%
wenzelm@12572
   334
%
wenzelm@12572
   335
\isamarkupsubsection{Extending and Truncating Records%
wenzelm@12572
   336
}
wenzelm@12572
   337
\isamarkuptrue%
wenzelm@12572
   338
%
wenzelm@12572
   339
\begin{isamarkuptext}%
wenzelm@12572
   340
Each record declaration introduces functions to refer collectively
wenzelm@12572
   341
  to a record's fields and to convert between related record types.
wenzelm@12572
   342
  They can, for instance, convert between types \isa{point} and \isa{cpoint}.  We can add a colour to a point or to convert a \isa{cpoint} to a \isa{point} by forgetting its colour.
wenzelm@12572
   343
wenzelm@12572
   344
  \begin{itemize}
wenzelm@12572
   345
wenzelm@12572
   346
  \item Function \cdx{make} takes as arguments all of the record's
wenzelm@12572
   347
  fields.  It returns the corresponding record.
wenzelm@12572
   348
wenzelm@12572
   349
  \item Function \cdx{fields} takes the record's new fields and
wenzelm@12572
   350
  returns a record fragment consisting of just those fields.  This may
wenzelm@12572
   351
  be filled into the \isa{more} part of the parent record scheme.
wenzelm@12572
   352
wenzelm@12572
   353
  \item Function \cdx{extend} takes two arguments: a record to be
wenzelm@12572
   354
  extended and a record containing the new fields.
wenzelm@12572
   355
wenzelm@12572
   356
  \item Function \cdx{truncate} takes a record (possibly an extension
wenzelm@12572
   357
  of the original record type) and returns a fixed record, removing
wenzelm@12572
   358
  any additional fields.
wenzelm@12572
   359
wenzelm@12572
   360
  \end{itemize}
wenzelm@12572
   361
wenzelm@12572
   362
  These functions merely provide handsome abbreviations for standard
wenzelm@12572
   363
  record expressions involving constructors and selectors.  The
wenzelm@12572
   364
  definitions, which are \emph{not} unfolded by default, are made
wenzelm@12572
   365
  available by the collective name of \isa{defs} (e.g.\ \isa{point{\isachardot}defs} or \isa{cpoint{\isachardot}defs}).
wenzelm@12572
   366
wenzelm@12572
   367
  For example, here are the versions of those functions generated for
wenzelm@12572
   368
  record \isa{point}.  We omit \isa{point{\isachardot}fields}, which happens to
wenzelm@12572
   369
  be the same as \isa{point{\isachardot}make}.
wenzelm@12572
   370
wenzelm@12572
   371
  \begin{isabelle}%
wenzelm@12572
   372
point{\isachardot}make\ {\isacharquery}Xcoord\ {\isacharquery}Ycoord\ {\isasymequiv}\ {\isasymlparr}Xcoord\ {\isacharequal}\ {\isacharquery}Xcoord{\isacharcomma}\ Ycoord\ {\isacharequal}\ {\isacharquery}Ycoord{\isasymrparr}\isanewline
wenzelm@12572
   373
point{\isachardot}extend\ {\isacharquery}r\ {\isacharquery}more\ {\isasymequiv}\isanewline
wenzelm@12572
   374
{\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ {\isacharquery}r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ {\isacharquery}r{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ {\isacharquery}more{\isasymrparr}\isanewline
wenzelm@12572
   375
point{\isachardot}truncate\ {\isacharquery}r\ {\isasymequiv}\ {\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ {\isacharquery}r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ {\isacharquery}r{\isasymrparr}%
wenzelm@12572
   376
\end{isabelle}
wenzelm@12572
   377
wenzelm@12572
   378
  Contrast those with the corresponding functions for record \isa{cpoint}.  Observe \isa{cpoint{\isachardot}fields} in particular.
wenzelm@12572
   379
wenzelm@12572
   380
  \begin{isabelle}%
wenzelm@12572
   381
cpoint{\isachardot}make\ {\isacharquery}Xcoord\ {\isacharquery}Ycoord\ {\isacharquery}col\ {\isasymequiv}\isanewline
wenzelm@12572
   382
{\isasymlparr}Xcoord\ {\isacharequal}\ {\isacharquery}Xcoord{\isacharcomma}\ Ycoord\ {\isacharequal}\ {\isacharquery}Ycoord{\isacharcomma}\ col\ {\isacharequal}\ {\isacharquery}col{\isasymrparr}\isanewline
wenzelm@12572
   383
cpoint{\isachardot}extend\ {\isacharquery}r\ {\isacharquery}more\ {\isasymequiv}\isanewline
wenzelm@12572
   384
{\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ {\isacharquery}r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ {\isacharquery}r{\isacharcomma}\ col\ {\isacharequal}\ col\ {\isacharquery}r{\isacharcomma}\ {\isasymdots}\ {\isacharequal}\ {\isacharquery}more{\isasymrparr}\isanewline
wenzelm@12572
   385
cpoint{\isachardot}truncate\ {\isacharquery}r\ {\isasymequiv}\isanewline
wenzelm@12572
   386
{\isasymlparr}Xcoord\ {\isacharequal}\ Xcoord\ {\isacharquery}r{\isacharcomma}\ Ycoord\ {\isacharequal}\ Ycoord\ {\isacharquery}r{\isacharcomma}\ col\ {\isacharequal}\ col\ {\isacharquery}r{\isasymrparr}%
wenzelm@12572
   387
\end{isabelle}
wenzelm@12572
   388
wenzelm@12572
   389
  To demonstrate these functions, we declare a new coloured point by
wenzelm@12572
   390
  extending an ordinary point.  Function \isa{point{\isachardot}extend} augments
wenzelm@12572
   391
  \isa{pt{\isadigit{1}}} with a colour, which is converted into an appropriate
wenzelm@12572
   392
  record fragment by \isa{cpoint{\isachardot}fields}.%
wenzelm@12572
   393
\end{isamarkuptext}%
wenzelm@12572
   394
\isamarkuptrue%
wenzelm@12572
   395
\isacommand{constdefs}\isanewline
wenzelm@12572
   396
\ \ cpt{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ cpoint\isanewline
wenzelm@12572
   397
\ \ {\isachardoublequote}cpt{\isadigit{2}}\ {\isasymequiv}\ point{\isachardot}extend\ pt{\isadigit{1}}\ {\isacharparenleft}cpoint{\isachardot}fields\ Green{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
wenzelm@12572
   398
%
wenzelm@12572
   399
\begin{isamarkuptext}%
wenzelm@12572
   400
The coloured points \isa{cpt{\isadigit{1}}} and \isa{cpt{\isadigit{2}}} are equal.  The
wenzelm@12572
   401
  proof is trivial, by unfolding all the definitions.  We deliberately
wenzelm@12572
   402
  omit the definition of~\isa{pt{\isadigit{1}}} in order to reveal the underlying
wenzelm@12572
   403
  comparison on type \isa{point}.%
wenzelm@12572
   404
\end{isamarkuptext}%
wenzelm@12572
   405
\isamarkuptrue%
wenzelm@12572
   406
\isacommand{lemma}\ {\isachardoublequote}cpt{\isadigit{1}}\ {\isacharequal}\ cpt{\isadigit{2}}{\isachardoublequote}\isanewline
wenzelm@12572
   407
\ \ \isamarkupfalse%
wenzelm@12572
   408
\isacommand{apply}\ {\isacharparenleft}simp\ add{\isacharcolon}\ cpt{\isadigit{1}}{\isacharunderscore}def\ cpt{\isadigit{2}}{\isacharunderscore}def\ point{\isachardot}defs\ cpoint{\isachardot}defs{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   409
%
wenzelm@12572
   410
\begin{isamarkuptxt}%
wenzelm@12572
   411
\begin{isabelle}%
wenzelm@12572
   412
\ {\isadigit{1}}{\isachardot}\ Xcoord\ pt{\isadigit{1}}\ {\isacharequal}\ {\isadigit{9}}{\isadigit{9}}{\isadigit{9}}\ {\isasymand}\ Ycoord\ pt{\isadigit{1}}\ {\isacharequal}\ {\isadigit{2}}{\isadigit{3}}%
wenzelm@12572
   413
\end{isabelle}%
wenzelm@12572
   414
\end{isamarkuptxt}%
wenzelm@12572
   415
\ \ \isamarkuptrue%
wenzelm@12572
   416
\isacommand{apply}\ {\isacharparenleft}simp\ add{\isacharcolon}\ pt{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}\isanewline
wenzelm@12572
   417
\ \ \isamarkupfalse%
wenzelm@12572
   418
\isacommand{done}\isamarkupfalse%
wenzelm@12572
   419
%
wenzelm@12572
   420
\begin{isamarkuptext}%
wenzelm@12572
   421
In the example below, a coloured point is truncated to leave a
wenzelm@12572
   422
  point.  We must use the \isa{truncate} function of the shorter
wenzelm@12572
   423
  record.%
wenzelm@12572
   424
\end{isamarkuptext}%
wenzelm@12572
   425
\isamarkuptrue%
wenzelm@12572
   426
\isacommand{lemma}\ {\isachardoublequote}point{\isachardot}truncate\ cpt{\isadigit{2}}\ {\isacharequal}\ pt{\isadigit{1}}{\isachardoublequote}\isanewline
wenzelm@12572
   427
\ \ \isamarkupfalse%
wenzelm@12572
   428
\isacommand{by}\ {\isacharparenleft}simp\ add{\isacharcolon}\ pt{\isadigit{1}}{\isacharunderscore}def\ cpt{\isadigit{2}}{\isacharunderscore}def\ point{\isachardot}defs{\isacharparenright}\isamarkupfalse%
wenzelm@12572
   429
%
wenzelm@12572
   430
\begin{isamarkuptext}%
wenzelm@12572
   431
\begin{exercise}
wenzelm@12572
   432
  Extend record \isa{cpoint} to have a further field, \isa{intensity}, of type~\isa{nat}.  Experiment with coercions among the
wenzelm@12572
   433
  three record types.
wenzelm@12572
   434
  \end{exercise}
wenzelm@12572
   435
wenzelm@12572
   436
  \begin{exercise}
wenzelm@12572
   437
  (For Java programmers.)
wenzelm@12572
   438
  Model a small class hierarchy using records.
wenzelm@12572
   439
  \end{exercise}
wenzelm@12572
   440
  \index{records|)}%
wenzelm@12572
   441
\end{isamarkuptext}%
wenzelm@12572
   442
\isamarkuptrue%
wenzelm@12572
   443
\isamarkupfalse%
wenzelm@12572
   444
\end{isabellebody}%
wenzelm@12572
   445
%%% Local Variables:
wenzelm@12572
   446
%%% mode: latex
wenzelm@12572
   447
%%% TeX-master: "root"
wenzelm@12572
   448
%%% End: