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