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%
|
|
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));%
|
|
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%
|
|
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);\\
|
|
180 |
\hspace*{0pt}\\
|
|
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;%
|
|
184 |
\end{isamarkuptext}%
|
|
185 |
\isamarkuptrue%
|
|
186 |
%
|
|
187 |
\endisatagquote
|
|
188 |
{\isafoldquote}%
|
|
189 |
%
|
|
190 |
\isadelimquote
|
|
191 |
%
|
|
192 |
\endisadelimquote
|
|
193 |
%
|
38437
|
194 |
\isamarkupsubsection{Datatypes \label{sec:datatypes}%
|
|
195 |
}
|
|
196 |
\isamarkuptrue%
|
|
197 |
%
|
|
198 |
\begin{isamarkuptext}%
|
|
199 |
Conceptually, any datatype is spanned by a set of
|
|
200 |
\emph{constructors} 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
|
|
201 |
\isa{{\isasymtau}}. The HOL datatype package by default registers any new
|
|
202 |
datatype in the table of datatypes, which may be inspected using the
|
|
203 |
\hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
|
|
204 |
|
|
205 |
In some cases, it is appropriate to alter or extend this table. As
|
|
206 |
an example, we will develop an alternative representation of the
|
|
207 |
queue example given in \secref{sec:queue_example}. The amortised
|
|
208 |
representation is convenient for generating code but exposes its
|
|
209 |
\qt{implementation} details, which may be cumbersome when proving
|
|
210 |
theorems about it. Therefore, here is a simple, straightforward
|
|
211 |
representation of queues:%
|
|
212 |
\end{isamarkuptext}%
|
|
213 |
\isamarkuptrue%
|
|
214 |
%
|
|
215 |
\isadelimquote
|
|
216 |
%
|
|
217 |
\endisadelimquote
|
|
218 |
%
|
|
219 |
\isatagquote
|
|
220 |
\isacommand{datatype}\isamarkupfalse%
|
|
221 |
\ {\isacharprime}a\ queue\ {\isacharequal}\ Queue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
|
|
222 |
\isanewline
|
|
223 |
\isacommand{definition}\isamarkupfalse%
|
|
224 |
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
225 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
226 |
\isanewline
|
|
227 |
\isacommand{primrec}\isamarkupfalse%
|
|
228 |
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
229 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}Queue\ xs{\isacharparenright}\ {\isacharequal}\ Queue\ {\isacharparenleft}xs\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
230 |
\isanewline
|
|
231 |
\isacommand{fun}\isamarkupfalse%
|
|
232 |
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
233 |
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
234 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ x{\isacharcomma}\ Queue\ xs{\isacharparenright}{\isachardoublequoteclose}%
|
|
235 |
\endisatagquote
|
|
236 |
{\isafoldquote}%
|
|
237 |
%
|
|
238 |
\isadelimquote
|
|
239 |
%
|
|
240 |
\endisadelimquote
|
|
241 |
%
|
|
242 |
\begin{isamarkuptext}%
|
|
243 |
\noindent This we can use directly for proving; for executing,
|
|
244 |
we provide an alternative characterisation:%
|
|
245 |
\end{isamarkuptext}%
|
|
246 |
\isamarkuptrue%
|
|
247 |
%
|
|
248 |
\isadelimquote
|
|
249 |
%
|
|
250 |
\endisadelimquote
|
|
251 |
%
|
|
252 |
\isatagquote
|
|
253 |
\isacommand{definition}\isamarkupfalse%
|
|
254 |
\ AQueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
255 |
\ \ {\isachardoublequoteopen}AQueue\ xs\ ys\ {\isacharequal}\ Queue\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
256 |
\isanewline
|
|
257 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
|
|
258 |
\ AQueue%
|
|
259 |
\endisatagquote
|
|
260 |
{\isafoldquote}%
|
|
261 |
%
|
|
262 |
\isadelimquote
|
|
263 |
%
|
|
264 |
\endisadelimquote
|
|
265 |
%
|
|
266 |
\begin{isamarkuptext}%
|
|
267 |
\noindent Here we define a \qt{constructor} \isa{AQueue} which
|
|
268 |
is defined in terms of \isa{Queue} and interprets its arguments
|
|
269 |
according to what the \emph{content} of an amortised queue is supposed
|
|
270 |
to be. Equipped with this, we are able to prove the following equations
|
|
271 |
for our primitive queue operations which \qt{implement} the simple
|
|
272 |
queues in an amortised fashion:%
|
|
273 |
\end{isamarkuptext}%
|
|
274 |
\isamarkuptrue%
|
|
275 |
%
|
|
276 |
\isadelimquote
|
|
277 |
%
|
|
278 |
\endisadelimquote
|
|
279 |
%
|
|
280 |
\isatagquote
|
|
281 |
\isacommand{lemma}\isamarkupfalse%
|
|
282 |
\ empty{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
283 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
|
|
284 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
285 |
\ AQueue{\isacharunderscore}def\ empty{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
286 |
\ simp\isanewline
|
|
287 |
\isanewline
|
|
288 |
\isacommand{lemma}\isamarkupfalse%
|
|
289 |
\ enqueue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
290 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
|
|
291 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
292 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
293 |
\ simp\isanewline
|
|
294 |
\isanewline
|
|
295 |
\isacommand{lemma}\isamarkupfalse%
|
|
296 |
\ dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
297 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
|
|
298 |
\ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
|
|
299 |
\ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
300 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
301 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
302 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
303 |
\ simp{\isacharunderscore}all%
|
|
304 |
\endisatagquote
|
|
305 |
{\isafoldquote}%
|
|
306 |
%
|
|
307 |
\isadelimquote
|
|
308 |
%
|
|
309 |
\endisadelimquote
|
|
310 |
%
|
|
311 |
\begin{isamarkuptext}%
|
|
312 |
\noindent For completeness, we provide a substitute for the
|
|
313 |
\isa{case} combinator on queues:%
|
|
314 |
\end{isamarkuptext}%
|
|
315 |
\isamarkuptrue%
|
|
316 |
%
|
|
317 |
\isadelimquote
|
|
318 |
%
|
|
319 |
\endisadelimquote
|
|
320 |
%
|
|
321 |
\isatagquote
|
|
322 |
\isacommand{lemma}\isamarkupfalse%
|
|
323 |
\ queue{\isacharunderscore}case{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
|
|
324 |
\ \ {\isachardoublequoteopen}queue{\isacharunderscore}case\ f\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ f\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
325 |
\ \ \isacommand{unfolding}\isamarkupfalse%
|
|
326 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
|
|
327 |
\ simp%
|
|
328 |
\endisatagquote
|
|
329 |
{\isafoldquote}%
|
|
330 |
%
|
|
331 |
\isadelimquote
|
|
332 |
%
|
|
333 |
\endisadelimquote
|
|
334 |
%
|
|
335 |
\begin{isamarkuptext}%
|
|
336 |
\noindent The resulting code looks as expected:%
|
|
337 |
\end{isamarkuptext}%
|
|
338 |
\isamarkuptrue%
|
|
339 |
%
|
|
340 |
\isadelimquote
|
|
341 |
%
|
|
342 |
\endisadelimquote
|
|
343 |
%
|
|
344 |
\isatagquote
|
|
345 |
%
|
|
346 |
\begin{isamarkuptext}%
|
|
347 |
\isatypewriter%
|
|
348 |
\noindent%
|
|
349 |
\hspace*{0pt}structure Example :~sig\\
|
|
350 |
\hspace*{0pt} ~val foldl :~('a -> 'b -> 'a) -> 'a -> 'b list -> 'a\\
|
|
351 |
\hspace*{0pt} ~val rev :~'a list -> 'a list\\
|
|
352 |
\hspace*{0pt} ~val null :~'a list -> bool\\
|
|
353 |
\hspace*{0pt} ~datatype 'a queue = AQueue of 'a list * 'a list\\
|
|
354 |
\hspace*{0pt} ~val empty :~'a queue\\
|
|
355 |
\hspace*{0pt} ~val dequeue :~'a queue -> 'a option * 'a queue\\
|
|
356 |
\hspace*{0pt} ~val enqueue :~'a -> 'a queue -> 'a queue\\
|
|
357 |
\hspace*{0pt}end = struct\\
|
|
358 |
\hspace*{0pt}\\
|
|
359 |
\hspace*{0pt}fun foldl f a [] = a\\
|
|
360 |
\hspace*{0pt} ~| foldl f a (x ::~xs) = foldl f (f a x) xs;\\
|
|
361 |
\hspace*{0pt}\\
|
|
362 |
\hspace*{0pt}fun rev xs = foldl (fn xsa => fn x => x ::~xsa) [] xs;\\
|
|
363 |
\hspace*{0pt}\\
|
|
364 |
\hspace*{0pt}fun null [] = true\\
|
|
365 |
\hspace*{0pt} ~| null (x ::~xs) = false;\\
|
|
366 |
\hspace*{0pt}\\
|
|
367 |
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\
|
|
368 |
\hspace*{0pt}\\
|
|
369 |
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[]);\\
|
|
370 |
\hspace*{0pt}\\
|
|
371 |
\hspace*{0pt}fun dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\
|
|
372 |
\hspace*{0pt} ~| dequeue (AQueue (xs,~[])) =\\
|
|
373 |
\hspace*{0pt} ~~~(if null xs then (NONE,~AQueue ([],~[]))\\
|
|
374 |
\hspace*{0pt} ~~~~~else dequeue (AQueue ([],~rev xs)));\\
|
|
375 |
\hspace*{0pt}\\
|
|
376 |
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\
|
|
377 |
\hspace*{0pt}\\
|
|
378 |
\hspace*{0pt}end;~(*struct Example*)%
|
|
379 |
\end{isamarkuptext}%
|
|
380 |
\isamarkuptrue%
|
|
381 |
%
|
|
382 |
\endisatagquote
|
|
383 |
{\isafoldquote}%
|
|
384 |
%
|
|
385 |
\isadelimquote
|
|
386 |
%
|
|
387 |
\endisadelimquote
|
|
388 |
%
|
|
389 |
\begin{isamarkuptext}%
|
|
390 |
\noindent From this example, it can be glimpsed that using own
|
|
391 |
constructor sets is a little delicate since it changes the set of
|
|
392 |
valid patterns for values of that type. Without going into much
|
|
393 |
detail, here some practical hints:
|
|
394 |
|
|
395 |
\begin{itemize}
|
|
396 |
|
|
397 |
\item When changing the constructor set for datatypes, take care
|
|
398 |
to provide alternative equations for the \isa{case} combinator.
|
|
399 |
|
|
400 |
\item Values in the target language need not to be normalised --
|
|
401 |
different values in the target language may represent the same
|
|
402 |
value in the logic.
|
|
403 |
|
|
404 |
\item Usually, a good methodology to deal with the subtleties of
|
|
405 |
pattern matching is to see the type as an abstract type: provide
|
|
406 |
a set of operations which operate on the concrete representation
|
|
407 |
of the type, and derive further operations by combinations of
|
|
408 |
these primitive ones, without relying on a particular
|
|
409 |
representation.
|
|
410 |
|
|
411 |
\end{itemize}%
|
|
412 |
\end{isamarkuptext}%
|
|
413 |
\isamarkuptrue%
|
|
414 |
%
|
38406
|
415 |
\isadelimtheory
|
|
416 |
%
|
|
417 |
\endisadelimtheory
|
|
418 |
%
|
|
419 |
\isatagtheory
|
|
420 |
\isacommand{end}\isamarkupfalse%
|
|
421 |
%
|
|
422 |
\endisatagtheory
|
|
423 |
{\isafoldtheory}%
|
|
424 |
%
|
|
425 |
\isadelimtheory
|
|
426 |
%
|
|
427 |
\endisadelimtheory
|
|
428 |
\isanewline
|
|
429 |
\end{isabellebody}%
|
|
430 |
%%% Local Variables:
|
|
431 |
%%% mode: latex
|
|
432 |
%%% TeX-master: "root"
|
|
433 |
%%% End:
|