11388
|
1 |
\section{Records}
|
|
2 |
\label{sec:records}
|
|
3 |
|
11427
|
4 |
\index{records|(}%
|
11388
|
5 |
Records are familiar from programming languages. A record of $n$
|
|
6 |
fields is essentially an $n$-tuple, but the record's components have
|
|
7 |
names, which can make expressions easier to read and reduces the risk
|
|
8 |
of confusing one field for another.
|
|
9 |
|
|
10 |
A basic Isabelle record has a fixed set of fields, with access
|
|
11 |
and update operations. Each field has a specified type, which may be
|
|
12 |
polymorphic. The field names are part of the record type, and the
|
|
13 |
order of the fields is significant --- as it is in Pascal but not in
|
|
14 |
Standard ML. If two different record types have fields in common,
|
|
15 |
then the ambiguity is resolved in the usual way, by qualified names.
|
|
16 |
|
|
17 |
Record types can also be defined by extending other record types.
|
11427
|
18 |
Extensible records make use of the reserved field \cdx{more}, which is
|
11388
|
19 |
present in every record type. Generic methods, or operations that
|
|
20 |
work on all possible extensions of a given record, can be expressed by
|
|
21 |
definitions involving \isa{more}, but the details are complicated.
|
|
22 |
|
|
23 |
\subsection{Record Basics}
|
|
24 |
|
|
25 |
Record types are not primitive in Isabelle and have a complex internal
|
11427
|
26 |
representation. A \commdx{record} declaration
|
11388
|
27 |
introduces a new record type:
|
|
28 |
\begin{isabelle}
|
|
29 |
\isacommand{record}\ point\ =\isanewline
|
|
30 |
\ \ Xcoord\ ::\ int\isanewline
|
|
31 |
\ \ Ycoord\ ::\ int
|
|
32 |
\end{isabelle}
|
|
33 |
|
|
34 |
Records of type \isa{point} have two fields named \isa{Xcoord} and \isa{Ycoord},
|
|
35 |
both of type~\isa{int}. We now declare a constant of type
|
|
36 |
\isa{point}:
|
|
37 |
\begin{isabelle}
|
|
38 |
\isacommand{constdefs}\ \ \ pt1\ ::\ point\isanewline
|
|
39 |
\ \ \ \ \ \ \ \ \ \ \ \ "pt1\ ==\ (|\ Xcoord\ =\ \#999,\ Ycoord\ =\ \#23\ |)"
|
|
40 |
\end{isabelle}
|
|
41 |
We see above the ASCII notation for record brackets. You can also use
|
|
42 |
the symbolic brackets \isa{\isasymlparr} and \isa{\isasymrparr}.
|
|
43 |
Record types can be written directly, rather than referring to
|
|
44 |
previously declared names:
|
|
45 |
\begin{isabelle}
|
|
46 |
\isacommand{constdefs}\ \ \ pt2\ ::\ "(|\ Xcoord\ ::\ int,\ Ycoord\ ::\ int\
|
|
47 |
|)"\ \isanewline
|
|
48 |
\ \ \ \ \ \ \ \ \ \ \ \ "pt2\ ==\ (|\ Xcoord\ =\ \#-45,\ Ycoord\ =\ \#97\ |)"
|
|
49 |
\end{isabelle}
|
|
50 |
|
|
51 |
For each field, there is a \emph{selector} function of the same name. For
|
|
52 |
example, if \isa{p} has type \isa{point} then \isa{Xcoord p} denotes the
|
|
53 |
value of the \isa{Xcoord} field of~\isa{p}. Expressions involving field
|
|
54 |
selection are simplified automatically:
|
|
55 |
\begin{isabelle}
|
|
56 |
\isacommand{lemma}\ "Xcoord\ (|\ Xcoord\ =\ a,\ Ycoord\ =\ b\ |)\ =\ a"\isanewline
|
|
57 |
\isacommand{by}\ simp
|
|
58 |
\end{isabelle}
|
|
59 |
The \emph{update} operation is functional. For example,
|
|
60 |
\isa{p\isasymlparr Xcoord:=\#0\isasymrparr} is a record whose \isa{Xcoord} value
|
|
61 |
is zero and whose
|
|
62 |
\isa{Ycoord} value is copied from~\isa{p}. Updates are also simplified
|
|
63 |
automatically:
|
|
64 |
\begin{isabelle}
|
|
65 |
\isacommand{lemma}\ "(|\ Xcoord\ =\ a,\ Ycoord\ =\ b\ |)\ (|\ Xcoord:=\ \#0\ |)\
|
|
66 |
=\isanewline
|
|
67 |
\ \ \ \ \ \ \ \ (|\ Xcoord\ =\ 0,\ Ycoord\ =\ b\ |)"\isanewline
|
|
68 |
\isacommand{by}\ simp
|
|
69 |
\end{isabelle}
|
|
70 |
|
|
71 |
\begin{warn}
|
|
72 |
Field names are declared as constants and can no longer be
|
|
73 |
used as variables. It would be unwise, for example, to call the fields
|
11427
|
74 |
of type \isa{point} simply \isa{x} and~\isa{y}. Each record
|
|
75 |
declaration introduces a constant \cdx{more}.
|
11388
|
76 |
\end{warn}
|
|
77 |
|
|
78 |
|
|
79 |
\subsection{Extensible Records and Generic Operations}
|
|
80 |
|
11427
|
81 |
\index{records!extensible|(}%
|
11388
|
82 |
Now, let us define coloured points
|
|
83 |
(type \isa{cpoint}) to be points extended with a field \isa{col} of type
|
|
84 |
\isa{colour}:
|
|
85 |
\begin{isabelle}
|
|
86 |
\isacommand{datatype}\ colour\ =\ Red\ |\ Green\ |\
|
|
87 |
Blue\isanewline
|
|
88 |
\isanewline
|
|
89 |
\isacommand{record}\ cpoint\ =\ point\ +\isanewline
|
|
90 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ col\ ::\ colour
|
|
91 |
\end{isabelle}
|
|
92 |
|
|
93 |
The fields of this new type are \isa{Xcoord}, \isa{Ycoord} and \isa{col}, in that
|
|
94 |
order:
|
|
95 |
\begin{isabelle}
|
|
96 |
\isacommand{constdefs}\ \ \ cpt1\ ::\ cpoint\isanewline
|
|
97 |
\ \ \ \ \ \ \ \ \ \ \ \ "cpt1\ ==\ (|\ Xcoord\ =\ \#999,\ Ycoord\ =\ \#23,\ col\
|
|
98 |
=\ Green\ |)"
|
|
99 |
\end{isabelle}
|
|
100 |
|
|
101 |
Unfortunately, there are no built-in conversions between types
|
|
102 |
\isa{point} and \isa{cpoint}: to add a colour to
|
|
103 |
a point, or to convert a \isa{cpoint} to a \isa{point} by forgetting
|
|
104 |
its colour, we must define operations that copy across the other
|
|
105 |
fields. However, we can define generic operations
|
|
106 |
that work on type
|
|
107 |
\isa{point} and all extensions of it.
|
|
108 |
|
11427
|
109 |
Every record structure has an implicit field, \cdx{more}, to allow
|
11388
|
110 |
extension. Its type is completely polymorphic:~\isa{'a}. When a
|
|
111 |
record value is expressed using just its standard fields, the value of
|
|
112 |
\isa{more} is implicitly set to \isa{()}, the empty tuple, which has
|
|
113 |
type \isa{unit}. Within the record brackets, you can refer to the
|
11427
|
114 |
\isa{more} field by writing \isa{...} (three periods):
|
11388
|
115 |
\begin{isabelle}
|
|
116 |
\isacommand{lemma}\ "Xcoord\ (|\ Xcoord\ =\ a,\ Ycoord\ =\ b,\ ...\ =\ p\ |)\ =\ a"
|
|
117 |
\end{isabelle}
|
|
118 |
This lemma (trivially proved using \isa{simp}) applies to any
|
|
119 |
record whose first two fields are \isa{Xcoord} and~\isa{Ycoord}. Field
|
|
120 |
\isa{more} can be selected in the usual way, but as all records share
|
|
121 |
this field, the identifier must be qualified:
|
|
122 |
\begin{isabelle}
|
|
123 |
\isacommand{lemma}\ "point.more\ cpt1\ =\ \isasymlparr col\ =\ Green\isasymrparr "\isanewline
|
|
124 |
\isacommand{by}\ (simp\ add:\ cpt1_def)
|
|
125 |
\end{isabelle}
|
|
126 |
%
|
|
127 |
We see that the colour attached to this \isa{point} is a record in its
|
|
128 |
own right, namely
|
|
129 |
\isa{\isasymlparr col\ =\ Green\isasymrparr}.
|
|
130 |
|
|
131 |
To define generic operations, we need to know a bit more about records.
|
|
132 |
Our declaration of \isa{point} above generated two type
|
|
133 |
abbreviations:
|
|
134 |
\begin{isabelle}
|
|
135 |
\ \ \ \ point\ =\ (|\ Xcoord\ ::\ int,\ Ycoord\ ::\ int\ |)\isanewline
|
|
136 |
\ \ \ \ 'a\ point_scheme\ =\ (|\ Xcoord\ ::\ int,\ Ycoord\ ::\ int,\ ...\ ::\ 'a\ |)
|
|
137 |
\end{isabelle}
|
|
138 |
%
|
|
139 |
Type \isa{point} is for rigid records having the two fields
|
|
140 |
\isa{Xcoord} and~\isa{Ycoord}, while the polymorphic type \isa{'a\ point_scheme}
|
|
141 |
comprises all possible extensions to those two fields. For example,
|
11427
|
142 |
let us define two operations --- methods, if we regard records as
|
|
143 |
objects --- to get and set any point's
|
|
144 |
\isa{Xcoord} field. The sort constraint in \isa{'a::more} is
|
|
145 |
required, since all extensions must belong to the type class
|
|
146 |
\isa{more}.%
|
11388
|
147 |
\REMARK{Why, and what does this imply in practice?}
|
|
148 |
\begin{isabelle}
|
|
149 |
\ \ getX\ ::\ "('a::more)\ point_scheme\ \isasymRightarrow \ int"\isanewline
|
|
150 |
\ \ \ "getX\ r\ ==\ Xcoord\ r"\isanewline
|
|
151 |
\ \ setX\ ::\ "[('a::more)\ point_scheme,\ int]\ \isasymRightarrow \ 'a\ point_scheme"\isanewline
|
|
152 |
\ \ \ "setX\ r\ a\ ==\ r\ (|\ Xcoord\ :=\ a\ |)"
|
|
153 |
\end{isabelle}
|
|
154 |
|
|
155 |
Here is a generic method that modifies a point, incrementing its
|
|
156 |
\isa{Xcoord} field. The \isa{Ycoord} and \isa{more} fields
|
|
157 |
are copied across. It works for type \isa{point} and any of its
|
|
158 |
extensions, such as \isa{cpoint}:
|
|
159 |
\begin{isabelle}
|
|
160 |
\isacommand{constdefs}\isanewline
|
|
161 |
\ \ incX\ ::\ "('a::more)\ point_scheme\ \isasymRightarrow \ 'a\ point_scheme"\isanewline
|
|
162 |
\ \ "incX\ r\ ==\ \isasymlparr Xcoord\ =\ (Xcoord\ r)\ +\
|
|
163 |
\#1,\isanewline
|
|
164 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ Ycoord\ =\ Ycoord\ r,\isanewline
|
|
165 |
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \isasymdots \ =\ point.more\
|
|
166 |
r\isasymrparr"
|
|
167 |
\end{isabelle}
|
|
168 |
|
|
169 |
Generic theorems can be proved about generic methods. This trivial
|
|
170 |
lemma relates \isa{incX} to \isa{getX} and \isa{setX}:
|
|
171 |
\begin{isabelle}
|
|
172 |
\isacommand{lemma}\ "incX\ r\ =\ setX\ r\ ((getX\ r)\ +\ \#1)"\isanewline
|
|
173 |
\isacommand{by}\ (simp\ add:\ getX_def\ setX_def\ incX_def)
|
|
174 |
\end{isabelle}
|
|
175 |
|
|
176 |
\begin{warn}
|
|
177 |
If you use the symbolic record brackets \isa{\isasymlparr} and
|
|
178 |
\isa{\isasymrparr}, then you must also use the symbolic ellipsis,
|
|
179 |
\isa{\isasymdots}, rather than three consecutive periods,
|
|
180 |
\isa{...}. Mixing the ASCII and symbolic versions
|
|
181 |
causes a syntax error. (The two versions are more
|
|
182 |
distinct on screen than they are on paper.)
|
11427
|
183 |
\end{warn}%
|
|
184 |
\index{records!extensible|)}
|
11388
|
185 |
|
|
186 |
|
|
187 |
\subsection{Record Equality}
|
|
188 |
|
11427
|
189 |
Two records are equal\index{equality!of records}
|
|
190 |
if all pairs of corresponding fields are equal.
|
11388
|
191 |
Record equalities are simplified automatically:
|
|
192 |
\begin{isabelle}
|
|
193 |
\isacommand{lemma}\ "(\isasymlparr Xcoord\ =\ a,\ Ycoord\ =\ b\isasymrparr \ =\ \isasymlparr Xcoord\ =\ a',\ Ycoord\ =\ b'\isasymrparr )\
|
|
194 |
=\isanewline
|
|
195 |
\ \ \ \ \ \ \ \ (a\ =\ a'\ \&\ b\ =\ b')"\isanewline
|
|
196 |
\isacommand{by}\ simp
|
|
197 |
\end{isabelle}
|
|
198 |
|
|
199 |
The following equality is similar, but generic, in that \isa{r} can
|
|
200 |
be any instance of \isa{point_scheme}:
|
|
201 |
\begin{isabelle}
|
|
202 |
\isacommand{lemma}\ "r\ \isasymlparr Xcoord\ :=\ a,\ Ycoord\ :=\
|
|
203 |
b\isasymrparr \ =\ r\ \isasymlparr Ycoord\ :=\ b,\ Xcoord\ :=\
|
|
204 |
a\isasymrparr "\isanewline
|
|
205 |
\isacommand{by}\ simp
|
|
206 |
\end{isabelle}
|
|
207 |
We see above the syntax for iterated updates. We could equivalently
|
|
208 |
have written the left-hand side as
|
|
209 |
\isa{r\ \isasymlparr Xcoord\ :=\ a\isasymrparr \ \isasymlparr
|
|
210 |
Ycoord\ :=\ b\isasymrparr}.
|
|
211 |
|
11427
|
212 |
Record equality is \emph{extensional}:
|
|
213 |
\index{extensionality!for records}
|
|
214 |
a record is determined entirely by the values of its fields.
|
11388
|
215 |
\begin{isabelle}
|
|
216 |
\isacommand{lemma}\ "r\ =\ \isasymlparr Xcoord\ =\ Xcoord\ r,\ Ycoord\ =\
|
|
217 |
Ycoord\ r\isasymrparr "\isanewline
|
|
218 |
\isacommand{by}\ simp
|
|
219 |
\end{isabelle}
|
|
220 |
The generic version of this equality includes the field \isa{more}:
|
|
221 |
\begin{isabelle}
|
|
222 |
\isacommand{lemma}\ "r\ =\ \isasymlparr Xcoord\ =\ Xcoord\ r,\ Ycoord\ =\ Ycoord\ r,\ \isasymdots \ =\ point.more\
|
|
223 |
r\isasymrparr"
|
|
224 |
\end{isabelle}
|
|
225 |
|
|
226 |
\medskip
|
|
227 |
The simplifier can prove many record equalities automatically,
|
|
228 |
but general equality reasoning can be tricky. Consider proving this
|
|
229 |
obvious fact:
|
|
230 |
\begin{isabelle}
|
|
231 |
\isacommand{lemma}\ "r\ \isasymlparr Xcoord\ :=\ a\isasymrparr \ =\ r\ \isasymlparr Xcoord\ :=\ a'\isasymrparr \ \isasymLongrightarrow \ a\ =\
|
|
232 |
a'"
|
|
233 |
\end{isabelle}
|
|
234 |
The simplifier can do nothing. One way to proceed is by an explicit
|
|
235 |
forward step that applies the selector \isa{Xcoord} to both sides
|
|
236 |
of the assumed record equality:
|
|
237 |
\begin{isabelle}
|
|
238 |
\isacommand{apply}\ (drule_tac\ f=Xcoord\ \isakeyword{in}\ arg_cong)\isanewline
|
|
239 |
\ 1.\ Xcoord\ (r\isasymlparr Xcoord\ :=\ a\isasymrparr )\ =\ Xcoord\
|
|
240 |
(r\isasymlparr Xcoord\ :=\ a'\isasymrparr )\ \isasymLongrightarrow \
|
|
241 |
a\ =\ a'
|
|
242 |
\end{isabelle}
|
|
243 |
Now, \isa{simp} will reduce the assumption to the desired
|
|
244 |
conclusion.
|
|
245 |
|
|
246 |
An alternative to such forward steps is record splitting. A record
|
|
247 |
variable can be split only if it is bound in the subgoal by the
|
|
248 |
meta-quantifier \isa{\isasymAnd}, or \isa{!!} in ASCII\@. So,
|
|
249 |
we enter the lemma again:
|
|
250 |
\begin{isabelle}
|
|
251 |
\isacommand{lemma}\ "!!r.\ r\ \isasymlparr Xcoord\ :=\ a\isasymrparr \ =\ r\ \isasymlparr Xcoord\ :=\ a'\isasymrparr \ \isasymLongrightarrow \ a\ =\
|
|
252 |
a'"\isanewline
|
|
253 |
\end{isabelle}
|
11427
|
254 |
The \methdx{record_split} method replaces the record variable by an
|
11388
|
255 |
explicit record, listing all fields. Even the field \isa{more} is
|
|
256 |
included, since the record equality is generic.
|
|
257 |
\begin{isabelle}
|
|
258 |
\isacommand{apply}\ record_split\isanewline
|
|
259 |
\ 1.\ \isasymAnd Xcoord\ Ycoord\ more.\isanewline
|
|
260 |
\isaindent{\ 1.\ \ \ \ }\isasymlparr Xcoord\ =\ Xcoord,\ Ycoord\ =\ Ycoord,\ \isasymdots \ =\ more\isasymrparr \isanewline
|
|
261 |
\isaindent{\ 1.\ \ \ \ }\isasymlparr Xcoord\ :=\ a\isasymrparr \ =\isanewline
|
|
262 |
\isaindent{\ 1.\ \ \ \ }\isasymlparr Xcoord\ =\ Xcoord,\ Ycoord\ =\ Ycoord,\ \isasymdots \ =\ more\isasymrparr \isanewline
|
|
263 |
\isaindent{\ 1.\ \ \ \ }\isasymlparr Xcoord\ :=\ a'\isasymrparr \ \isasymLongrightarrow \isanewline
|
|
264 |
\isaindent{\ 1.\ \ \ \ }a\ =\ a'
|
|
265 |
\end{isabelle}
|
|
266 |
Again, \isa{simp} finishes the proof. Because the records have
|
|
267 |
been split, the updates can be applied and the record equality can be
|
|
268 |
replaced by equality of the corresponding fields.
|
|
269 |
|
|
270 |
\begin{exercise}
|
|
271 |
\REMARK{There should be some, but I can't think of any.}
|
|
272 |
\end{exercise}
|
11427
|
273 |
\index{records|)}
|
11388
|
274 |
|
|
275 |
\endinput
|
|
276 |
|