38406
|
1 |
%
|
|
2 |
\begin{isabellebody}%
|
|
3 |
\def\isabellecontext{Refinement}%
|
|
4 |
%
|
|
5 |
\isadelimtheory
|
|
6 |
%
|
|
7 |
\endisadelimtheory
|
|
8 |
%
|
|
9 |
\isatagtheory
|
|
10 |
\isacommand{theory}\isamarkupfalse%
|
|
11 |
\ Refinement\isanewline
|
|
12 |
\isakeyword{imports}\ Setup\isanewline
|
|
13 |
\isakeyword{begin}%
|
|
14 |
\endisatagtheory
|
|
15 |
{\isafoldtheory}%
|
|
16 |
%
|
|
17 |
\isadelimtheory
|
|
18 |
%
|
|
19 |
\endisadelimtheory
|
|
20 |
%
|
|
21 |
\isamarkupsection{Program and datatype refinement \label{sec:refinement}%
|
|
22 |
}
|
|
23 |
\isamarkuptrue%
|
|
24 |
%
|
38451
|
25 |
\begin{isamarkuptext}%
|
|
26 |
Code generation by shallow embedding (cf.~\secref{sec:principle})
|
|
27 |
allows to choose code equations and datatype constructors freely,
|
|
28 |
given that some very basic syntactic properties are met; this
|
|
29 |
flexibility opens up mechanisms for refinement which allow to extend
|
|
30 |
the scope and quality of generated code dramatically.%
|
|
31 |
\end{isamarkuptext}%
|
|
32 |
\isamarkuptrue%
|
|
33 |
%
|
|
34 |
\isamarkupsubsection{Program refinement%
|
|
35 |
}
|
|
36 |
\isamarkuptrue%
|
|
37 |
%
|
|
38 |
\begin{isamarkuptext}%
|
|
39 |
Program refinement works by choosing appropriate code equations
|
|
40 |
explicitly (cf.~\label{sec:equations}); as example, we use Fibonacci
|
|
41 |
numbers:%
|
|
42 |
\end{isamarkuptext}%
|
|
43 |
\isamarkuptrue%
|
|
44 |
%
|
|
45 |
\isadelimquote
|
|
46 |
%
|
|
47 |
\endisadelimquote
|
|
48 |
%
|
|
49 |
\isatagquote
|
|
50 |
\isacommand{fun}\isamarkupfalse%
|
|
51 |
\ fib\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
52 |
\ \ \ \ {\isachardoublequoteopen}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
|
53 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
|
54 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ fib\ n\ {\isacharplus}\ fib\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isachardoublequoteclose}%
|
|
55 |
\endisatagquote
|
|
56 |
{\isafoldquote}%
|
|
57 |
%
|
|
58 |
\isadelimquote
|
|
59 |
%
|
|
60 |
\endisadelimquote
|
|
61 |
%
|
|
62 |
\begin{isamarkuptext}%
|
|
63 |
\noindent The runtime of the corresponding code grows exponential due
|
|
64 |
to two recursive calls:%
|
|
65 |
\end{isamarkuptext}%
|
|
66 |
\isamarkuptrue%
|
|
67 |
%
|
|
68 |
\isadelimquote
|
|
69 |
%
|
|
70 |
\endisadelimquote
|
|
71 |
%
|
|
72 |
\isatagquote
|
|
73 |
%
|
|
74 |
\begin{isamarkuptext}%
|
|
75 |
\isatypewriter%
|
|
76 |
\noindent%
|
39210
|
77 |
\hspace*{0pt}fib ::~Nat -> Nat;\\
|
|
78 |
\hspace*{0pt}fib Zero{\char95}nat = Zero{\char95}nat;\\
|
|
79 |
\hspace*{0pt}fib (Suc Zero{\char95}nat) = Suc Zero{\char95}nat;\\
|
|
80 |
\hspace*{0pt}fib (Suc (Suc n)) = plus{\char95}nat (fib n) (fib (Suc n));%
|
38451
|
81 |
\end{isamarkuptext}%
|
|
82 |
\isamarkuptrue%
|
|
83 |
%
|
|
84 |
\endisatagquote
|
|
85 |
{\isafoldquote}%
|
|
86 |
%
|
|
87 |
\isadelimquote
|
|
88 |
%
|
|
89 |
\endisadelimquote
|
|
90 |
%
|
|
91 |
\begin{isamarkuptext}%
|
|
92 |
\noindent A more efficient implementation would use dynamic
|
|
93 |
programming, e.g.~sharing of common intermediate results between
|
|
94 |
recursive calls. This idea is expressed by an auxiliary operation
|
|
95 |
which computes a Fibonacci number and its successor simultaneously:%
|
|
96 |
\end{isamarkuptext}%
|
|
97 |
\isamarkuptrue%
|
|
98 |
%
|
|
99 |
\isadelimquote
|
|
100 |
%
|
|
101 |
\endisadelimquote
|
|
102 |
%
|
|
103 |
\isatagquote
|
|
104 |
\isacommand{definition}\isamarkupfalse%
|
|
105 |
\ fib{\isacharunderscore}step\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymtimes}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
106 |
\ \ {\isachardoublequoteopen}fib{\isacharunderscore}step\ n\ {\isacharequal}\ {\isacharparenleft}fib\ {\isacharparenleft}Suc\ n{\isacharparenright}{\isacharcomma}\ fib\ n{\isacharparenright}{\isachardoublequoteclose}%
|
|
107 |
\endisatagquote
|
|
108 |
{\isafoldquote}%
|
|
109 |
%
|
|
110 |
\isadelimquote
|
|
111 |
%
|
|
112 |
\endisadelimquote
|
|
113 |
%
|
|
114 |
\begin{isamarkuptext}%
|
|
115 |
\noindent This operation can be implemented by recursion using
|
|
116 |
dynamic programming:%
|
|
117 |
\end{isamarkuptext}%
|
|
118 |
\isamarkuptrue%
|
|
119 |
%
|
|
120 |
\isadelimquote
|
|
121 |
%
|
|
122 |
\endisadelimquote
|
|
123 |
%
|
|
124 |
\isatagquote
|
|
125 |
\isacommand{lemma}\isamarkupfalse%
|
|
126 |
\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
127 |
\ \ {\isachardoublequoteopen}fib{\isacharunderscore}step\ {\isadigit{0}}\ {\isacharequal}\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
128 |
\ \ {\isachardoublequoteopen}fib{\isacharunderscore}step\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}m{\isacharcomma}\ q{\isacharparenright}\ {\isacharequal}\ fib{\isacharunderscore}step\ n\ in\ {\isacharparenleft}m\ {\isacharplus}\ q{\isacharcomma}\ m{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
129 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
130 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ fib{\isacharunderscore}step{\isacharunderscore}def{\isacharparenright}%
|
|
131 |
\endisatagquote
|
|
132 |
{\isafoldquote}%
|
|
133 |
%
|
|
134 |
\isadelimquote
|
|
135 |
%
|
|
136 |
\endisadelimquote
|
|
137 |
%
|
|
138 |
\begin{isamarkuptext}%
|
|
139 |
\noindent What remains is to implement \isa{fib} by \isa{fib{\isacharunderscore}step} as follows:%
|
|
140 |
\end{isamarkuptext}%
|
|
141 |
\isamarkuptrue%
|
|
142 |
%
|
|
143 |
\isadelimquote
|
|
144 |
%
|
|
145 |
\endisadelimquote
|
|
146 |
%
|
|
147 |
\isatagquote
|
|
148 |
\isacommand{lemma}\isamarkupfalse%
|
|
149 |
\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
150 |
\ \ {\isachardoublequoteopen}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
|
|
151 |
\ \ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ fst\ {\isacharparenleft}fib{\isacharunderscore}step\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
152 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
153 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ fib{\isacharunderscore}step{\isacharunderscore}def{\isacharparenright}%
|
|
154 |
\endisatagquote
|
|
155 |
{\isafoldquote}%
|
|
156 |
%
|
|
157 |
\isadelimquote
|
|
158 |
%
|
|
159 |
\endisadelimquote
|
|
160 |
%
|
|
161 |
\begin{isamarkuptext}%
|
|
162 |
\noindent The resulting code shows only linear growth of runtime:%
|
|
163 |
\end{isamarkuptext}%
|
|
164 |
\isamarkuptrue%
|
|
165 |
%
|
|
166 |
\isadelimquote
|
|
167 |
%
|
|
168 |
\endisadelimquote
|
|
169 |
%
|
|
170 |
\isatagquote
|
|
171 |
%
|
|
172 |
\begin{isamarkuptext}%
|
|
173 |
\isatypewriter%
|
|
174 |
\noindent%
|
39210
|
175 |
\hspace*{0pt}fib{\char95}step ::~Nat -> (Nat,~Nat);\\
|
|
176 |
\hspace*{0pt}fib{\char95}step (Suc n) = let {\char123}\\
|
|
177 |
\hspace*{0pt} ~~~~~~~~~~~~~~~~~~~~(m,~q) = fib{\char95}step n;\\
|
|
178 |
\hspace*{0pt} ~~~~~~~~~~~~~~~~~~{\char125}~in (plus{\char95}nat m q,~m);\\
|
|
179 |
\hspace*{0pt}fib{\char95}step Zero{\char95}nat = (Suc Zero{\char95}nat,~Zero{\char95}nat);\\
|
38451
|
180 |
\hspace*{0pt}\\
|
39210
|
181 |
\hspace*{0pt}fib ::~Nat -> Nat;\\
|
|
182 |
\hspace*{0pt}fib (Suc n) = fst (fib{\char95}step n);\\
|
|
183 |
\hspace*{0pt}fib Zero{\char95}nat = Zero{\char95}nat;%
|
38451
|
184 |
\end{isamarkuptext}%
|
|
185 |
\isamarkuptrue%
|
|
186 |
%
|
|
187 |
\endisatagquote
|
|
188 |
{\isafoldquote}%
|
|
189 |
%
|
|
190 |
\isadelimquote
|
|
191 |
%
|
|
192 |
\endisadelimquote
|
|
193 |
%
|
38459
|
194 |
\isamarkupsubsection{Datatype refinement%
|
38437
|
195 |
}
|
|
196 |
\isamarkuptrue%
|
|
197 |
%
|
|
198 |
\begin{isamarkuptext}%
|
38459
|
199 |
Selecting specific code equations \emph{and} datatype constructors
|
|
200 |
leads to datatype refinement. As an example, we will develop an
|
|
201 |
alternative representation of the queue example given in
|
|
202 |
\secref{sec:queue_example}. The amortised representation is
|
|
203 |
convenient for generating code but exposes its \qt{implementation}
|
|
204 |
details, which may be cumbersome when proving theorems about it.
|
|
205 |
Therefore, here is a simple, straightforward representation of
|
|
206 |
queues:%
|
38437
|
207 |
\end{isamarkuptext}%
|
|
208 |
\isamarkuptrue%
|
|
209 |
%
|
|
210 |
\isadelimquote
|
|
211 |
%
|
|
212 |
\endisadelimquote
|
|
213 |
%
|
|
214 |
\isatagquote
|
|
215 |
\isacommand{datatype}\isamarkupfalse%
|
|
216 |
\ {\isacharprime}a\ queue\ {\isacharequal}\ Queue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
|
|
217 |
\isanewline
|
|
218 |
\isacommand{definition}\isamarkupfalse%
|
|
219 |
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
220 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
221 |
\isanewline
|
|
222 |
\isacommand{primrec}\isamarkupfalse%
|
|
223 |
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
224 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}Queue\ xs{\isacharparenright}\ {\isacharequal}\ Queue\ {\isacharparenleft}xs\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
225 |
\isanewline
|
|
226 |
\isacommand{fun}\isamarkupfalse%
|
|
227 |
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
228 |
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
229 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ x{\isacharcomma}\ Queue\ xs{\isacharparenright}{\isachardoublequoteclose}%
|
|
230 |
\endisatagquote
|
|
231 |
{\isafoldquote}%
|
|
232 |
%
|
|
233 |
\isadelimquote
|
|
234 |
%
|
|
235 |
\endisadelimquote
|
|
236 |
%
|
|
237 |
\begin{isamarkuptext}%
|
|
238 |
\noindent This we can use directly for proving; for executing,
|
|
239 |
we provide an alternative characterisation:%
|
|
240 |
\end{isamarkuptext}%
|
|
241 |
\isamarkuptrue%
|
|
242 |
%
|
|
243 |
\isadelimquote
|
|
244 |
%
|
|
245 |
\endisadelimquote
|
|
246 |
%
|
|
247 |
\isatagquote
|
|
248 |
\isacommand{definition}\isamarkupfalse%
|
|
249 |
\ AQueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
250 |
\ \ {\isachardoublequoteopen}AQueue\ xs\ ys\ {\isacharequal}\ Queue\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
251 |
\isanewline
|
|
252 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
|
|
253 |
\ AQueue%
|
|
254 |
\endisatagquote
|
|
255 |
{\isafoldquote}%
|
|
256 |
%
|
|
257 |
\isadelimquote
|
|
258 |
%
|
|
259 |
\endisadelimquote
|
|
260 |
%
|
|
261 |
\begin{isamarkuptext}%
|
|
262 |
\noindent Here we define a \qt{constructor} \isa{AQueue} which
|
|
263 |
is defined in terms of \isa{Queue} and interprets its arguments
|
|
264 |
according to what the \emph{content} of an amortised queue is supposed
|
38459
|
265 |
to be.
|
|
266 |
|
|
267 |
The prerequisite for datatype constructors is only syntactical: a
|
|
268 |
constructor must be of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n} where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is exactly the set of \emph{all} type variables in
|
|
269 |
\isa{{\isasymtau}}; then \isa{{\isasymkappa}} is its corresponding datatype. The
|
|
270 |
HOL datatype package by default registers any new datatype with its
|
38511
|
271 |
constructors, but this may be changed using \indexdef{}{command}{code\_datatype}\hypertarget{command.code-datatype}{\hyperlink{command.code-datatype}{\mbox{\isa{\isacommand{code{\isacharunderscore}datatype}}}}}; the currently chosen constructors can be inspected
|
38459
|
272 |
using the \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
|
|
273 |
|
|
274 |
Equipped with this, we are able to prove the following equations
|
38437
|
275 |
for our primitive queue operations which \qt{implement} the simple
|
|
276 |
queues in an amortised fashion:%
|
|
277 |
\end{isamarkuptext}%
|
|
278 |
\isamarkuptrue%
|
|
279 |
%
|
|
280 |
\isadelimquote
|
|
281 |
%
|
|
282 |
\endisadelimquote
|
|
283 |
%
|
|
284 |
\isatagquote
|
|
285 |
\isacommand{lemma}\isamarkupfalse%
|
|
286 |
\ empty{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
287 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
288 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
289 |
\ AQueue{\isacharunderscore}def\ empty{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
290 |
\ simp\isanewline
|
|
291 |
\isanewline
|
|
292 |
\isacommand{lemma}\isamarkupfalse%
|
|
293 |
\ enqueue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
294 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
|
|
295 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
296 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
297 |
\ simp\isanewline
|
|
298 |
\isanewline
|
|
299 |
\isacommand{lemma}\isamarkupfalse%
|
|
300 |
\ dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
301 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
|
|
302 |
\ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
|
|
303 |
\ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
304 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
305 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
306 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
307 |
\ simp{\isacharunderscore}all%
|
|
308 |
\endisatagquote
|
|
309 |
{\isafoldquote}%
|
|
310 |
%
|
|
311 |
\isadelimquote
|
|
312 |
%
|
|
313 |
\endisadelimquote
|
|
314 |
%
|
|
315 |
\begin{isamarkuptext}%
|
|
316 |
\noindent For completeness, we provide a substitute for the
|
|
317 |
\isa{case} combinator on queues:%
|
|
318 |
\end{isamarkuptext}%
|
|
319 |
\isamarkuptrue%
|
|
320 |
%
|
|
321 |
\isadelimquote
|
|
322 |
%
|
|
323 |
\endisadelimquote
|
|
324 |
%
|
|
325 |
\isatagquote
|
|
326 |
\isacommand{lemma}\isamarkupfalse%
|
|
327 |
\ queue{\isacharunderscore}case{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
328 |
\ \ {\isachardoublequoteopen}queue{\isacharunderscore}case\ f\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ f\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
329 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
330 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
331 |
\ simp%
|
|
332 |
\endisatagquote
|
|
333 |
{\isafoldquote}%
|
|
334 |
%
|
|
335 |
\isadelimquote
|
|
336 |
%
|
|
337 |
\endisadelimquote
|
|
338 |
%
|
|
339 |
\begin{isamarkuptext}%
|
|
340 |
\noindent The resulting code looks as expected:%
|
|
341 |
\end{isamarkuptext}%
|
|
342 |
\isamarkuptrue%
|
|
343 |
%
|
|
344 |
\isadelimquote
|
|
345 |
%
|
|
346 |
\endisadelimquote
|
|
347 |
%
|
|
348 |
\isatagquote
|
|
349 |
%
|
|
350 |
\begin{isamarkuptext}%
|
|
351 |
\isatypewriter%
|
|
352 |
\noindent%
|
|
353 |
\hspace*{0pt}structure Example :~sig\\
|
38459
|
354 |
\hspace*{0pt} ~val id :~'a -> 'a\\
|
|
355 |
\hspace*{0pt} ~val fold :~('a -> 'b -> 'b) -> 'a list -> 'b -> 'b\\
|
38437
|
356 |
\hspace*{0pt} ~val rev :~'a list -> 'a list\\
|
|
357 |
\hspace*{0pt} ~val null :~'a list -> bool\\
|
|
358 |
\hspace*{0pt} ~datatype 'a queue = AQueue of 'a list * 'a list\\
|
|
359 |
\hspace*{0pt} ~val empty :~'a queue\\
|
|
360 |
\hspace*{0pt} ~val dequeue :~'a queue -> 'a option * 'a queue\\
|
|
361 |
\hspace*{0pt} ~val enqueue :~'a -> 'a queue -> 'a queue\\
|
|
362 |
\hspace*{0pt}end = struct\\
|
|
363 |
\hspace*{0pt}\\
|
38459
|
364 |
\hspace*{0pt}fun id x = (fn xa => xa) x;\\
|
38437
|
365 |
\hspace*{0pt}\\
|
38459
|
366 |
\hspace*{0pt}fun fold f [] = id\\
|
|
367 |
\hspace*{0pt} ~| fold f (x ::~xs) = fold f xs o f x;\\
|
|
368 |
\hspace*{0pt}\\
|
|
369 |
\hspace*{0pt}fun rev xs = fold (fn a => fn b => a ::~b) xs [];\\
|
38437
|
370 |
\hspace*{0pt}\\
|
|
371 |
\hspace*{0pt}fun null [] = true\\
|
|
372 |
\hspace*{0pt} ~| null (x ::~xs) = false;\\
|
|
373 |
\hspace*{0pt}\\
|
|
374 |
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\
|
|
375 |
\hspace*{0pt}\\
|
|
376 |
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[]);\\
|
|
377 |
\hspace*{0pt}\\
|
|
378 |
\hspace*{0pt}fun dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\
|
|
379 |
\hspace*{0pt} ~| dequeue (AQueue (xs,~[])) =\\
|
|
380 |
\hspace*{0pt} ~~~(if null xs then (NONE,~AQueue ([],~[]))\\
|
|
381 |
\hspace*{0pt} ~~~~~else dequeue (AQueue ([],~rev xs)));\\
|
|
382 |
\hspace*{0pt}\\
|
|
383 |
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\
|
|
384 |
\hspace*{0pt}\\
|
|
385 |
\hspace*{0pt}end;~(*struct Example*)%
|
|
386 |
\end{isamarkuptext}%
|
|
387 |
\isamarkuptrue%
|
|
388 |
%
|
|
389 |
\endisatagquote
|
|
390 |
{\isafoldquote}%
|
|
391 |
%
|
|
392 |
\isadelimquote
|
|
393 |
%
|
|
394 |
\endisadelimquote
|
|
395 |
%
|
|
396 |
\begin{isamarkuptext}%
|
38459
|
397 |
The same techniques can also be applied to types which are not
|
|
398 |
specified as datatypes, e.g.~type \isa{int} is originally specified
|
38511
|
399 |
as quotient type by means of \indexdef{}{command}{typedef}\hypertarget{command.typedef}{\hyperlink{command.typedef}{\mbox{\isa{\isacommand{typedef}}}}}, but for code
|
38459
|
400 |
generation constants allowing construction of binary numeral values
|
|
401 |
are used as constructors for \isa{int}.
|
38437
|
402 |
|
38459
|
403 |
This approach however fails if the representation of a type demands
|
|
404 |
invariants; this issue is discussed in the next section.%
|
|
405 |
\end{isamarkuptext}%
|
|
406 |
\isamarkuptrue%
|
|
407 |
%
|
39599
|
408 |
\isamarkupsubsection{Datatype refinement involving invariants \label{sec:invariant}%
|
38459
|
409 |
}
|
|
410 |
\isamarkuptrue%
|
|
411 |
%
|
|
412 |
\begin{isamarkuptext}%
|
38502
|
413 |
Datatype representation involving invariants require a dedicated
|
|
414 |
setup for the type and its primitive operations. As a running
|
|
415 |
example, we implement a type \isa{{\isacharprime}a\ dlist} of list consisting
|
|
416 |
of distinct elements.
|
|
417 |
|
|
418 |
The first step is to decide on which representation the abstract
|
|
419 |
type (in our example \isa{{\isacharprime}a\ dlist}) should be implemented.
|
|
420 |
Here we choose \isa{{\isacharprime}a\ list}. Then a conversion from the concrete
|
|
421 |
type to the abstract type must be specified, here:%
|
|
422 |
\end{isamarkuptext}%
|
|
423 |
\isamarkuptrue%
|
|
424 |
%
|
|
425 |
\isadelimquote
|
|
426 |
%
|
|
427 |
\endisadelimquote
|
|
428 |
%
|
|
429 |
\isatagquote
|
|
430 |
%
|
|
431 |
\begin{isamarkuptext}%
|
|
432 |
\isa{Dlist\ {\isasymColon}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ dlist}%
|
|
433 |
\end{isamarkuptext}%
|
|
434 |
\isamarkuptrue%
|
|
435 |
%
|
|
436 |
\endisatagquote
|
|
437 |
{\isafoldquote}%
|
|
438 |
%
|
|
439 |
\isadelimquote
|
|
440 |
%
|
|
441 |
\endisadelimquote
|
|
442 |
%
|
|
443 |
\begin{isamarkuptext}%
|
|
444 |
\noindent Next follows the specification of a suitable \emph{projection},
|
|
445 |
i.e.~a conversion from abstract to concrete type:%
|
|
446 |
\end{isamarkuptext}%
|
|
447 |
\isamarkuptrue%
|
|
448 |
%
|
|
449 |
\isadelimquote
|
|
450 |
%
|
|
451 |
\endisadelimquote
|
|
452 |
%
|
|
453 |
\isatagquote
|
|
454 |
%
|
|
455 |
\begin{isamarkuptext}%
|
|
456 |
\isa{list{\isacharunderscore}of{\isacharunderscore}dlist\ {\isasymColon}\ {\isacharprime}a\ dlist\ {\isasymRightarrow}\ {\isacharprime}a\ list}%
|
|
457 |
\end{isamarkuptext}%
|
|
458 |
\isamarkuptrue%
|
|
459 |
%
|
|
460 |
\endisatagquote
|
|
461 |
{\isafoldquote}%
|
|
462 |
%
|
|
463 |
\isadelimquote
|
|
464 |
%
|
|
465 |
\endisadelimquote
|
|
466 |
%
|
|
467 |
\begin{isamarkuptext}%
|
|
468 |
\noindent This projection must be specified such that the following
|
|
469 |
\emph{abstract datatype certificate} can be proven:%
|
|
470 |
\end{isamarkuptext}%
|
|
471 |
\isamarkuptrue%
|
|
472 |
%
|
|
473 |
\isadelimquote
|
|
474 |
%
|
|
475 |
\endisadelimquote
|
|
476 |
%
|
|
477 |
\isatagquote
|
|
478 |
\isacommand{lemma}\isamarkupfalse%
|
|
479 |
\ {\isacharbrackleft}code\ abstype{\isacharbrackright}{\isacharcolon}\isanewline
|
|
480 |
\ \ {\isachardoublequoteopen}Dlist\ {\isacharparenleft}list{\isacharunderscore}of{\isacharunderscore}dlist\ dxs{\isacharparenright}\ {\isacharequal}\ dxs{\isachardoublequoteclose}\isanewline
|
|
481 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
482 |
\ {\isacharparenleft}fact\ Dlist{\isacharunderscore}list{\isacharunderscore}of{\isacharunderscore}dlist{\isacharparenright}%
|
|
483 |
\endisatagquote
|
|
484 |
{\isafoldquote}%
|
|
485 |
%
|
|
486 |
\isadelimquote
|
|
487 |
%
|
|
488 |
\endisadelimquote
|
|
489 |
%
|
|
490 |
\begin{isamarkuptext}%
|
|
491 |
\noindent Note that so far the invariant on representations
|
|
492 |
(\isa{distinct\ {\isasymColon}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ bool}) has never been mentioned explicitly:
|
|
493 |
the invariant is only referred to implicitly: all values in
|
|
494 |
set \isa{{\isacharbraceleft}xs{\isachardot}\ list{\isacharunderscore}of{\isacharunderscore}dlist\ {\isacharparenleft}Dlist\ xs{\isacharparenright}\ {\isacharequal}\ xs{\isacharbraceright}} are invariant,
|
|
495 |
and in our example this is exactly \isa{{\isacharbraceleft}xs{\isachardot}\ distinct\ xs{\isacharbraceright}}.
|
|
496 |
|
|
497 |
The primitive operations on \isa{{\isacharprime}a\ dlist} are specified
|
|
498 |
indirectly using the projection \isa{list{\isacharunderscore}of{\isacharunderscore}dlist}. For
|
|
499 |
the empty \isa{dlist}, \isa{Dlist{\isachardot}empty}, we finally want
|
|
500 |
the code equation%
|
|
501 |
\end{isamarkuptext}%
|
|
502 |
\isamarkuptrue%
|
|
503 |
%
|
|
504 |
\isadelimquote
|
|
505 |
%
|
|
506 |
\endisadelimquote
|
|
507 |
%
|
|
508 |
\isatagquote
|
|
509 |
%
|
|
510 |
\begin{isamarkuptext}%
|
|
511 |
\isa{Dlist{\isachardot}empty\ {\isacharequal}\ Dlist\ {\isacharbrackleft}{\isacharbrackright}}%
|
|
512 |
\end{isamarkuptext}%
|
|
513 |
\isamarkuptrue%
|
|
514 |
%
|
|
515 |
\endisatagquote
|
|
516 |
{\isafoldquote}%
|
|
517 |
%
|
|
518 |
\isadelimquote
|
|
519 |
%
|
|
520 |
\endisadelimquote
|
|
521 |
%
|
|
522 |
\begin{isamarkuptext}%
|
|
523 |
\noindent This we have to prove indirectly as follows:%
|
|
524 |
\end{isamarkuptext}%
|
|
525 |
\isamarkuptrue%
|
|
526 |
%
|
|
527 |
\isadelimquote
|
|
528 |
%
|
|
529 |
\endisadelimquote
|
|
530 |
%
|
|
531 |
\isatagquote
|
|
532 |
\isacommand{lemma}\isamarkupfalse%
|
|
533 |
\ {\isacharbrackleft}code\ abstract{\isacharbrackright}{\isacharcolon}\isanewline
|
|
534 |
\ \ {\isachardoublequoteopen}list{\isacharunderscore}of{\isacharunderscore}dlist\ Dlist{\isachardot}empty\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
535 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
536 |
\ {\isacharparenleft}fact\ list{\isacharunderscore}of{\isacharunderscore}dlist{\isacharunderscore}empty{\isacharparenright}%
|
|
537 |
\endisatagquote
|
|
538 |
{\isafoldquote}%
|
|
539 |
%
|
|
540 |
\isadelimquote
|
|
541 |
%
|
|
542 |
\endisadelimquote
|
|
543 |
%
|
|
544 |
\begin{isamarkuptext}%
|
|
545 |
\noindent This equation logically encodes both the desired code
|
|
546 |
equation and that the expression \isa{Dlist} is applied to obeys
|
|
547 |
the implicit invariant. Equations for insertion and removal are
|
|
548 |
similar:%
|
|
549 |
\end{isamarkuptext}%
|
|
550 |
\isamarkuptrue%
|
|
551 |
%
|
|
552 |
\isadelimquote
|
|
553 |
%
|
|
554 |
\endisadelimquote
|
|
555 |
%
|
|
556 |
\isatagquote
|
|
557 |
\isacommand{lemma}\isamarkupfalse%
|
|
558 |
\ {\isacharbrackleft}code\ abstract{\isacharbrackright}{\isacharcolon}\isanewline
|
|
559 |
\ \ {\isachardoublequoteopen}list{\isacharunderscore}of{\isacharunderscore}dlist\ {\isacharparenleft}Dlist{\isachardot}insert\ x\ dxs{\isacharparenright}\ {\isacharequal}\ List{\isachardot}insert\ x\ {\isacharparenleft}list{\isacharunderscore}of{\isacharunderscore}dlist\ dxs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
560 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
561 |
\ {\isacharparenleft}fact\ list{\isacharunderscore}of{\isacharunderscore}dlist{\isacharunderscore}insert{\isacharparenright}\isanewline
|
|
562 |
\isanewline
|
|
563 |
\isacommand{lemma}\isamarkupfalse%
|
|
564 |
\ {\isacharbrackleft}code\ abstract{\isacharbrackright}{\isacharcolon}\isanewline
|
|
565 |
\ \ {\isachardoublequoteopen}list{\isacharunderscore}of{\isacharunderscore}dlist\ {\isacharparenleft}Dlist{\isachardot}remove\ x\ dxs{\isacharparenright}\ {\isacharequal}\ remove{\isadigit{1}}\ x\ {\isacharparenleft}list{\isacharunderscore}of{\isacharunderscore}dlist\ dxs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
566 |
\ \ \isacommand{by}\isamarkupfalse%
|
|
567 |
\ {\isacharparenleft}fact\ list{\isacharunderscore}of{\isacharunderscore}dlist{\isacharunderscore}remove{\isacharparenright}%
|
|
568 |
\endisatagquote
|
|
569 |
{\isafoldquote}%
|
|
570 |
%
|
|
571 |
\isadelimquote
|
|
572 |
%
|
|
573 |
\endisadelimquote
|
|
574 |
%
|
|
575 |
\begin{isamarkuptext}%
|
|
576 |
\noindent Then the corresponding code is as follows:%
|
|
577 |
\end{isamarkuptext}%
|
|
578 |
\isamarkuptrue%
|
|
579 |
%
|
|
580 |
\isadelimquote
|
|
581 |
%
|
|
582 |
\endisadelimquote
|
|
583 |
%
|
|
584 |
\isatagquote
|
|
585 |
%
|
|
586 |
\begin{isamarkuptext}%
|
|
587 |
\isatypewriter%
|
|
588 |
\noindent%
|
|
589 |
\hspace*{0pt}module Example where {\char123}\\
|
|
590 |
\hspace*{0pt}\\
|
|
591 |
\hspace*{0pt}newtype Dlist a = Dlist [a];\\
|
|
592 |
\hspace*{0pt}\\
|
39210
|
593 |
\hspace*{0pt}empty ::~forall a.~Dlist a;\\
|
|
594 |
\hspace*{0pt}empty = Dlist [];\\
|
38502
|
595 |
\hspace*{0pt}\\
|
|
596 |
\hspace*{0pt}member ::~forall a.~(Eq a) => [a] -> a -> Bool;\\
|
|
597 |
\hspace*{0pt}member [] y = False;\\
|
39210
|
598 |
\hspace*{0pt}member (x :~xs) y = x == y || member xs y;\\
|
38502
|
599 |
\hspace*{0pt}\\
|
39210
|
600 |
\hspace*{0pt}insert ::~forall a.~(Eq a) => a -> [a] -> [a];\\
|
|
601 |
\hspace*{0pt}insert x xs = (if member xs x then xs else x :~xs);\\
|
38502
|
602 |
\hspace*{0pt}\\
|
39210
|
603 |
\hspace*{0pt}list{\char95}of{\char95}dlist ::~forall a.~Dlist a -> [a];\\
|
|
604 |
\hspace*{0pt}list{\char95}of{\char95}dlist (Dlist x) = x;\\
|
38502
|
605 |
\hspace*{0pt}\\
|
39210
|
606 |
\hspace*{0pt}inserta ::~forall a.~(Eq a) => a -> Dlist a -> Dlist a;\\
|
|
607 |
\hspace*{0pt}inserta x dxs = Dlist (insert x (list{\char95}of{\char95}dlist dxs));\\
|
38502
|
608 |
\hspace*{0pt}\\
|
|
609 |
\hspace*{0pt}remove1 ::~forall a.~(Eq a) => a -> [a] -> [a];\\
|
|
610 |
\hspace*{0pt}remove1 x [] = [];\\
|
39210
|
611 |
\hspace*{0pt}remove1 x (y :~xs) = (if x == y then xs else y :~remove1 x xs);\\
|
38502
|
612 |
\hspace*{0pt}\\
|
39210
|
613 |
\hspace*{0pt}remove ::~forall a.~(Eq a) => a -> Dlist a -> Dlist a;\\
|
|
614 |
\hspace*{0pt}remove x dxs = Dlist (remove1 x (list{\char95}of{\char95}dlist dxs));\\
|
38502
|
615 |
\hspace*{0pt}\\
|
|
616 |
\hspace*{0pt}{\char125}%
|
|
617 |
\end{isamarkuptext}%
|
|
618 |
\isamarkuptrue%
|
|
619 |
%
|
|
620 |
\endisatagquote
|
|
621 |
{\isafoldquote}%
|
|
622 |
%
|
|
623 |
\isadelimquote
|
|
624 |
%
|
|
625 |
\endisadelimquote
|
|
626 |
%
|
|
627 |
\begin{isamarkuptext}%
|
|
628 |
Typical data structures implemented by representations involving
|
|
629 |
invariants are available in the library, e.g.~theories \hyperlink{theory.Fset}{\mbox{\isa{Fset}}} and \hyperlink{theory.Mapping}{\mbox{\isa{Mapping}}} specify sets (type \isa{{\isacharprime}a\ fset}) and
|
|
630 |
key-value-mappings (type \isa{{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ mapping}) respectively;
|
|
631 |
these can be implemented by distinct lists as presented here as
|
|
632 |
example (theory \hyperlink{theory.Dlist}{\mbox{\isa{Dlist}}}) and red-black-trees respectively
|
|
633 |
(theory \hyperlink{theory.RBT}{\mbox{\isa{RBT}}}).%
|
38437
|
634 |
\end{isamarkuptext}%
|
|
635 |
\isamarkuptrue%
|
|
636 |
%
|
38406
|
637 |
\isadelimtheory
|
|
638 |
%
|
|
639 |
\endisadelimtheory
|
|
640 |
%
|
|
641 |
\isatagtheory
|
|
642 |
\isacommand{end}\isamarkupfalse%
|
|
643 |
%
|
|
644 |
\endisatagtheory
|
|
645 |
{\isafoldtheory}%
|
|
646 |
%
|
|
647 |
\isadelimtheory
|
|
648 |
%
|
|
649 |
\endisadelimtheory
|
|
650 |
\isanewline
|
|
651 |
\end{isabellebody}%
|
|
652 |
%%% Local Variables:
|
|
653 |
%%% mode: latex
|
|
654 |
%%% TeX-master: "root"
|
|
655 |
%%% End:
|