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