author | haftmann |
Tue, 14 Jul 2009 10:54:54 +0200 | |
changeset 32000 | 6f07563dc8a9 |
parent 31848 | e5ab21d14974 |
child 33707 | 68841fb382e0 |
permissions | -rw-r--r-- |
28447 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Program}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
% |
|
7 |
\endisadelimtheory |
|
8 |
% |
|
9 |
\isatagtheory |
|
10 |
\isacommand{theory}\isamarkupfalse% |
|
11 |
\ Program\isanewline |
|
12 |
\isakeyword{imports}\ Introduction\isanewline |
|
13 |
\isakeyword{begin}% |
|
14 |
\endisatagtheory |
|
15 |
{\isafoldtheory}% |
|
16 |
% |
|
17 |
\isadelimtheory |
|
18 |
% |
|
19 |
\endisadelimtheory |
|
20 |
% |
|
21 |
\isamarkupsection{Turning Theories into Programs \label{sec:program}% |
|
22 |
} |
|
23 |
\isamarkuptrue% |
|
24 |
% |
|
25 |
\isamarkupsubsection{The \isa{Isabelle{\isacharslash}HOL} default setup% |
|
26 |
} |
|
27 |
\isamarkuptrue% |
|
28 |
% |
|
29 |
\begin{isamarkuptext}% |
|
30 |
We have already seen how by default equations stemming from |
|
31 |
\hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}/\hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}}/\hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}} |
|
32 |
statements are used for code generation. This default behaviour |
|
29560 | 33 |
can be changed, e.g. by providing different code equations. |
28593 | 34 |
All kinds of customisation shown in this section is \emph{safe} |
28447 | 35 |
in the sense that the user does not have to worry about |
36 |
correctness -- all programs generatable that way are partially |
|
37 |
correct.% |
|
38 |
\end{isamarkuptext}% |
|
39 |
\isamarkuptrue% |
|
40 |
% |
|
41 |
\isamarkupsubsection{Selecting code equations% |
|
42 |
} |
|
43 |
\isamarkuptrue% |
|
44 |
% |
|
45 |
\begin{isamarkuptext}% |
|
46 |
Coming back to our introductory example, we |
|
29560 | 47 |
could provide an alternative code equations for \isa{dequeue} |
28447 | 48 |
explicitly:% |
49 |
\end{isamarkuptext}% |
|
50 |
\isamarkuptrue% |
|
51 |
% |
|
28564 | 52 |
\isadelimquote |
28447 | 53 |
% |
28564 | 54 |
\endisadelimquote |
28447 | 55 |
% |
28564 | 56 |
\isatagquote |
28447 | 57 |
\isacommand{lemma}\isamarkupfalse% |
28562 | 58 |
\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
29798 | 59 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline |
60 |
\ \ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline |
|
61 |
\ \ \ \ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
62 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline |
|
63 |
\ \ \ \ \ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
28447 | 64 |
\ \ \isacommand{by}\isamarkupfalse% |
65 |
\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\ {\isacharparenleft}cases\ {\isachardoublequoteopen}rev\ xs{\isachardoublequoteclose}{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}% |
|
28564 | 66 |
\endisatagquote |
67 |
{\isafoldquote}% |
|
28447 | 68 |
% |
28564 | 69 |
\isadelimquote |
28447 | 70 |
% |
28564 | 71 |
\endisadelimquote |
28447 | 72 |
% |
73 |
\begin{isamarkuptext}% |
|
28562 | 74 |
\noindent The annotation \isa{{\isacharbrackleft}code{\isacharbrackright}} is an \isa{Isar} |
28447 | 75 |
\isa{attribute} which states that the given theorems should be |
29560 | 76 |
considered as code equations for a \isa{fun} statement -- |
28447 | 77 |
the corresponding constant is determined syntactically. The resulting code:% |
78 |
\end{isamarkuptext}% |
|
79 |
\isamarkuptrue% |
|
80 |
% |
|
28564 | 81 |
\isadelimquote |
28447 | 82 |
% |
28564 | 83 |
\endisadelimquote |
28447 | 84 |
% |
28564 | 85 |
\isatagquote |
28447 | 86 |
% |
87 |
\begin{isamarkuptext}% |
|
28727 | 88 |
\isatypewriter% |
28447 | 89 |
\noindent% |
29297 | 90 |
\hspace*{0pt}dequeue ::~forall a.~Queue a -> (Maybe a,~Queue a);\\ |
29798 | 91 |
\hspace*{0pt}dequeue (AQueue xs (y :~ys)) = (Just y,~AQueue xs ys);\\ |
92 |
\hspace*{0pt}dequeue (AQueue xs []) =\\ |
|
93 |
\hspace*{0pt} ~(if nulla xs then (Nothing,~AQueue [] [])\\ |
|
94 |
\hspace*{0pt} ~~~else dequeue (AQueue [] (rev xs)));% |
|
28447 | 95 |
\end{isamarkuptext}% |
96 |
\isamarkuptrue% |
|
97 |
% |
|
28564 | 98 |
\endisatagquote |
99 |
{\isafoldquote}% |
|
28447 | 100 |
% |
28564 | 101 |
\isadelimquote |
28447 | 102 |
% |
28564 | 103 |
\endisadelimquote |
28447 | 104 |
% |
105 |
\begin{isamarkuptext}% |
|
106 |
\noindent You may note that the equality test \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} has been |
|
107 |
replaced by the predicate \isa{null\ xs}. This is due to the default |
|
108 |
setup in the \qn{preprocessor} to be discussed further below (\secref{sec:preproc}). |
|
109 |
||
110 |
Changing the default constructor set of datatypes is also |
|
29798 | 111 |
possible. See \secref{sec:datatypes} for an example. |
28447 | 112 |
|
113 |
As told in \secref{sec:concept}, code generation is based |
|
114 |
on a structured collection of code theorems. |
|
115 |
For explorative purpose, this collection |
|
116 |
may be inspected using the \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} command:% |
|
117 |
\end{isamarkuptext}% |
|
118 |
\isamarkuptrue% |
|
119 |
% |
|
28564 | 120 |
\isadelimquote |
28447 | 121 |
% |
28564 | 122 |
\endisadelimquote |
28447 | 123 |
% |
28564 | 124 |
\isatagquote |
28447 | 125 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
126 |
\ dequeue% |
|
28564 | 127 |
\endisatagquote |
128 |
{\isafoldquote}% |
|
28447 | 129 |
% |
28564 | 130 |
\isadelimquote |
28447 | 131 |
% |
28564 | 132 |
\endisadelimquote |
28447 | 133 |
% |
134 |
\begin{isamarkuptext}% |
|
29560 | 135 |
\noindent prints a table with \emph{all} code equations |
28447 | 136 |
for \isa{dequeue}, including |
29560 | 137 |
\emph{all} code equations those equations depend |
28447 | 138 |
on recursively. |
139 |
||
140 |
Similarly, the \hyperlink{command.code-deps}{\mbox{\isa{\isacommand{code{\isacharunderscore}deps}}}} command shows a graph |
|
29560 | 141 |
visualising dependencies between code equations.% |
28447 | 142 |
\end{isamarkuptext}% |
143 |
\isamarkuptrue% |
|
144 |
% |
|
145 |
\isamarkupsubsection{\isa{class} and \isa{instantiation}% |
|
146 |
} |
|
147 |
\isamarkuptrue% |
|
148 |
% |
|
149 |
\begin{isamarkuptext}% |
|
150 |
Concerning type classes and code generation, let us examine an example |
|
151 |
from abstract algebra:% |
|
152 |
\end{isamarkuptext}% |
|
153 |
\isamarkuptrue% |
|
154 |
% |
|
28564 | 155 |
\isadelimquote |
28447 | 156 |
% |
28564 | 157 |
\endisadelimquote |
28447 | 158 |
% |
28564 | 159 |
\isatagquote |
28447 | 160 |
\isacommand{class}\isamarkupfalse% |
29798 | 161 |
\ semigroup\ {\isacharequal}\isanewline |
28447 | 162 |
\ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline |
163 |
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
164 |
\isanewline |
|
165 |
\isacommand{class}\isamarkupfalse% |
|
166 |
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline |
|
167 |
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
168 |
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline |
|
169 |
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline |
|
170 |
\isanewline |
|
171 |
\isacommand{instantiation}\isamarkupfalse% |
|
172 |
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline |
|
173 |
\isakeyword{begin}\isanewline |
|
174 |
\isanewline |
|
175 |
\isacommand{primrec}\isamarkupfalse% |
|
176 |
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline |
|
177 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
178 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline |
|
179 |
\isanewline |
|
180 |
\isacommand{definition}\isamarkupfalse% |
|
181 |
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline |
|
182 |
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline |
|
183 |
\isanewline |
|
184 |
\isacommand{lemma}\isamarkupfalse% |
|
185 |
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline |
|
186 |
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline |
|
187 |
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline |
|
188 |
\ \ \isacommand{by}\isamarkupfalse% |
|
189 |
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline |
|
190 |
\isanewline |
|
191 |
\isacommand{instance}\isamarkupfalse% |
|
192 |
\ \isacommand{proof}\isamarkupfalse% |
|
193 |
\isanewline |
|
194 |
\ \ \isacommand{fix}\isamarkupfalse% |
|
195 |
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline |
|
196 |
\ \ \isacommand{show}\isamarkupfalse% |
|
197 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
198 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
199 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline |
|
200 |
\ \ \isacommand{show}\isamarkupfalse% |
|
201 |
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline |
|
202 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
203 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline |
|
204 |
\ \ \isacommand{show}\isamarkupfalse% |
|
205 |
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline |
|
206 |
\ \ \ \ \isacommand{by}\isamarkupfalse% |
|
207 |
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline |
|
208 |
\isacommand{qed}\isamarkupfalse% |
|
209 |
\isanewline |
|
210 |
\isanewline |
|
211 |
\isacommand{end}\isamarkupfalse% |
|
212 |
% |
|
28564 | 213 |
\endisatagquote |
214 |
{\isafoldquote}% |
|
28447 | 215 |
% |
28564 | 216 |
\isadelimquote |
28447 | 217 |
% |
28564 | 218 |
\endisadelimquote |
28447 | 219 |
% |
220 |
\begin{isamarkuptext}% |
|
221 |
\noindent We define the natural operation of the natural numbers |
|
222 |
on monoids:% |
|
223 |
\end{isamarkuptext}% |
|
224 |
\isamarkuptrue% |
|
225 |
% |
|
28564 | 226 |
\isadelimquote |
28447 | 227 |
% |
28564 | 228 |
\endisadelimquote |
28447 | 229 |
% |
28564 | 230 |
\isatagquote |
28447 | 231 |
\isacommand{primrec}\isamarkupfalse% |
232 |
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
233 |
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline |
|
234 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}% |
|
28564 | 235 |
\endisatagquote |
236 |
{\isafoldquote}% |
|
28447 | 237 |
% |
28564 | 238 |
\isadelimquote |
28447 | 239 |
% |
28564 | 240 |
\endisadelimquote |
28447 | 241 |
% |
242 |
\begin{isamarkuptext}% |
|
243 |
\noindent This we use to define the discrete exponentiation function:% |
|
244 |
\end{isamarkuptext}% |
|
245 |
\isamarkuptrue% |
|
246 |
% |
|
28564 | 247 |
\isadelimquote |
28447 | 248 |
% |
28564 | 249 |
\endisadelimquote |
28447 | 250 |
% |
28564 | 251 |
\isatagquote |
28447 | 252 |
\isacommand{definition}\isamarkupfalse% |
253 |
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
254 |
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}% |
|
28564 | 255 |
\endisatagquote |
256 |
{\isafoldquote}% |
|
28447 | 257 |
% |
28564 | 258 |
\isadelimquote |
28447 | 259 |
% |
28564 | 260 |
\endisadelimquote |
28447 | 261 |
% |
262 |
\begin{isamarkuptext}% |
|
263 |
\noindent The corresponding code:% |
|
264 |
\end{isamarkuptext}% |
|
265 |
\isamarkuptrue% |
|
266 |
% |
|
28564 | 267 |
\isadelimquote |
28447 | 268 |
% |
28564 | 269 |
\endisadelimquote |
28447 | 270 |
% |
28564 | 271 |
\isatagquote |
28447 | 272 |
% |
273 |
\begin{isamarkuptext}% |
|
28727 | 274 |
\isatypewriter% |
28447 | 275 |
\noindent% |
28714 | 276 |
\hspace*{0pt}module Example where {\char123}\\ |
277 |
\hspace*{0pt}\\ |
|
278 |
\hspace*{0pt}\\ |
|
30121 | 279 |
\hspace*{0pt}data Nat = Zero{\char95}nat | Suc Nat;\\ |
28714 | 280 |
\hspace*{0pt}\\ |
281 |
\hspace*{0pt}class Semigroup a where {\char123}\\ |
|
282 |
\hspace*{0pt} ~mult ::~a -> a -> a;\\ |
|
283 |
\hspace*{0pt}{\char125};\\ |
|
284 |
\hspace*{0pt}\\ |
|
285 |
\hspace*{0pt}class (Semigroup a) => Monoid a where {\char123}\\ |
|
286 |
\hspace*{0pt} ~neutral ::~a;\\ |
|
287 |
\hspace*{0pt}{\char125};\\ |
|
288 |
\hspace*{0pt}\\ |
|
29297 | 289 |
\hspace*{0pt}pow ::~forall a.~(Monoid a) => Nat -> a -> a;\\ |
28714 | 290 |
\hspace*{0pt}pow Zero{\char95}nat a = neutral;\\ |
291 |
\hspace*{0pt}pow (Suc n) a = mult a (pow n a);\\ |
|
292 |
\hspace*{0pt}\\ |
|
293 |
\hspace*{0pt}plus{\char95}nat ::~Nat -> Nat -> Nat;\\ |
|
294 |
\hspace*{0pt}plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n);\\ |
|
295 |
\hspace*{0pt}plus{\char95}nat Zero{\char95}nat n = n;\\ |
|
296 |
\hspace*{0pt}\\ |
|
297 |
\hspace*{0pt}neutral{\char95}nat ::~Nat;\\ |
|
298 |
\hspace*{0pt}neutral{\char95}nat = Suc Zero{\char95}nat;\\ |
|
299 |
\hspace*{0pt}\\ |
|
300 |
\hspace*{0pt}mult{\char95}nat ::~Nat -> Nat -> Nat;\\ |
|
301 |
\hspace*{0pt}mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat;\\ |
|
302 |
\hspace*{0pt}mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\ |
|
303 |
\hspace*{0pt}\\ |
|
304 |
\hspace*{0pt}instance Semigroup Nat where {\char123}\\ |
|
305 |
\hspace*{0pt} ~mult = mult{\char95}nat;\\ |
|
306 |
\hspace*{0pt}{\char125};\\ |
|
307 |
\hspace*{0pt}\\ |
|
308 |
\hspace*{0pt}instance Monoid Nat where {\char123}\\ |
|
309 |
\hspace*{0pt} ~neutral = neutral{\char95}nat;\\ |
|
310 |
\hspace*{0pt}{\char125};\\ |
|
311 |
\hspace*{0pt}\\ |
|
312 |
\hspace*{0pt}bexp ::~Nat -> Nat;\\ |
|
313 |
\hspace*{0pt}bexp n = pow n (Suc (Suc Zero{\char95}nat));\\ |
|
314 |
\hspace*{0pt}\\ |
|
315 |
\hspace*{0pt}{\char125}% |
|
28447 | 316 |
\end{isamarkuptext}% |
317 |
\isamarkuptrue% |
|
318 |
% |
|
28564 | 319 |
\endisatagquote |
320 |
{\isafoldquote}% |
|
28447 | 321 |
% |
28564 | 322 |
\isadelimquote |
28447 | 323 |
% |
28564 | 324 |
\endisadelimquote |
28447 | 325 |
% |
326 |
\begin{isamarkuptext}% |
|
327 |
\noindent This is a convenient place to show how explicit dictionary construction |
|
328 |
manifests in generated code (here, the same example in \isa{SML}):% |
|
329 |
\end{isamarkuptext}% |
|
330 |
\isamarkuptrue% |
|
331 |
% |
|
28564 | 332 |
\isadelimquote |
28447 | 333 |
% |
28564 | 334 |
\endisadelimquote |
28447 | 335 |
% |
28564 | 336 |
\isatagquote |
28447 | 337 |
% |
338 |
\begin{isamarkuptext}% |
|
28727 | 339 |
\isatypewriter% |
28447 | 340 |
\noindent% |
28714 | 341 |
\hspace*{0pt}structure Example = \\ |
342 |
\hspace*{0pt}struct\\ |
|
343 |
\hspace*{0pt}\\ |
|
30121 | 344 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\ |
28714 | 345 |
\hspace*{0pt}\\ |
346 |
\hspace*{0pt}type 'a semigroup = {\char123}mult :~'a -> 'a -> 'a{\char125};\\ |
|
347 |
\hspace*{0pt}fun mult (A{\char95}:'a semigroup) = {\char35}mult A{\char95};\\ |
|
348 |
\hspace*{0pt}\\ |
|
31544 | 349 |
\hspace*{0pt}type 'a monoid = {\char123}semigroup{\char95}monoid :~'a semigroup,~neutral :~'a{\char125};\\ |
350 |
\hspace*{0pt}fun semigroup{\char95}monoid (A{\char95}:'a monoid) = {\char35}semigroup{\char95}monoid A{\char95};\\ |
|
28714 | 351 |
\hspace*{0pt}fun neutral (A{\char95}:'a monoid) = {\char35}neutral A{\char95};\\ |
352 |
\hspace*{0pt}\\ |
|
353 |
\hspace*{0pt}fun pow A{\char95}~Zero{\char95}nat a = neutral A{\char95}\\ |
|
354 |
\hspace*{0pt} ~| pow A{\char95}~(Suc n) a = mult (semigroup{\char95}monoid A{\char95}) a (pow A{\char95}~n a);\\ |
|
355 |
\hspace*{0pt}\\ |
|
356 |
\hspace*{0pt}fun plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n)\\ |
|
357 |
\hspace*{0pt} ~| plus{\char95}nat Zero{\char95}nat n = n;\\ |
|
358 |
\hspace*{0pt}\\ |
|
29297 | 359 |
\hspace*{0pt}val neutral{\char95}nat :~nat = Suc Zero{\char95}nat\\ |
28714 | 360 |
\hspace*{0pt}\\ |
361 |
\hspace*{0pt}fun mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat\\ |
|
362 |
\hspace*{0pt} ~| mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\ |
|
363 |
\hspace*{0pt}\\ |
|
364 |
\hspace*{0pt}val semigroup{\char95}nat = {\char123}mult = mult{\char95}nat{\char125}~:~nat semigroup;\\ |
|
365 |
\hspace*{0pt}\\ |
|
31544 | 366 |
\hspace*{0pt}val monoid{\char95}nat = {\char123}semigroup{\char95}monoid = semigroup{\char95}nat,~neutral = neutral{\char95}nat{\char125}\\ |
367 |
\hspace*{0pt} ~:~nat monoid;\\ |
|
28714 | 368 |
\hspace*{0pt}\\ |
369 |
\hspace*{0pt}fun bexp n = pow monoid{\char95}nat n (Suc (Suc Zero{\char95}nat));\\ |
|
370 |
\hspace*{0pt}\\ |
|
29297 | 371 |
\hspace*{0pt}end;~(*struct Example*)% |
28447 | 372 |
\end{isamarkuptext}% |
373 |
\isamarkuptrue% |
|
374 |
% |
|
28564 | 375 |
\endisatagquote |
376 |
{\isafoldquote}% |
|
28447 | 377 |
% |
28564 | 378 |
\isadelimquote |
28447 | 379 |
% |
28564 | 380 |
\endisadelimquote |
28447 | 381 |
% |
382 |
\begin{isamarkuptext}% |
|
383 |
\noindent Note the parameters with trailing underscore (\verb|A_|) |
|
384 |
which are the dictionary parameters.% |
|
385 |
\end{isamarkuptext}% |
|
386 |
\isamarkuptrue% |
|
387 |
% |
|
388 |
\isamarkupsubsection{The preprocessor \label{sec:preproc}% |
|
389 |
} |
|
390 |
\isamarkuptrue% |
|
391 |
% |
|
392 |
\begin{isamarkuptext}% |
|
393 |
Before selected function theorems are turned into abstract |
|
394 |
code, a chain of definitional transformation steps is carried |
|
395 |
out: \emph{preprocessing}. In essence, the preprocessor |
|
396 |
consists of two components: a \emph{simpset} and \emph{function transformers}. |
|
397 |
||
32000 | 398 |
The \emph{simpset} allows to employ the full generality of the |
399 |
Isabelle simplifier. Due to the interpretation of theorems as code |
|
400 |
equations, rewrites are applied to the right hand side and the |
|
401 |
arguments of the left hand side of an equation, but never to the |
|
402 |
constant heading the left hand side. An important special case are |
|
403 |
\emph{unfold theorems} which may be declared and undeclared using |
|
404 |
the \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} or \emph{\hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} del} |
|
405 |
attribute respectively. |
|
28447 | 406 |
|
407 |
Some common applications:% |
|
408 |
\end{isamarkuptext}% |
|
409 |
\isamarkuptrue% |
|
410 |
% |
|
411 |
\begin{itemize} |
|
412 |
% |
|
413 |
\begin{isamarkuptext}% |
|
414 |
\item replacing non-executable constructs by executable ones:% |
|
415 |
\end{isamarkuptext}% |
|
416 |
\isamarkuptrue% |
|
417 |
% |
|
28564 | 418 |
\isadelimquote |
28447 | 419 |
% |
28564 | 420 |
\endisadelimquote |
28447 | 421 |
% |
28564 | 422 |
\isatagquote |
28447 | 423 |
\isacommand{lemma}\isamarkupfalse% |
32000 | 424 |
\ {\isacharbrackleft}code{\isacharunderscore}inline{\isacharbrackright}{\isacharcolon}\isanewline |
28447 | 425 |
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse% |
426 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
28564 | 427 |
\endisatagquote |
428 |
{\isafoldquote}% |
|
28447 | 429 |
% |
28564 | 430 |
\isadelimquote |
28447 | 431 |
% |
28564 | 432 |
\endisadelimquote |
28447 | 433 |
% |
434 |
\begin{isamarkuptext}% |
|
435 |
\item eliminating superfluous constants:% |
|
436 |
\end{isamarkuptext}% |
|
437 |
\isamarkuptrue% |
|
438 |
% |
|
28564 | 439 |
\isadelimquote |
28447 | 440 |
% |
28564 | 441 |
\endisadelimquote |
28447 | 442 |
% |
28564 | 443 |
\isatagquote |
28447 | 444 |
\isacommand{lemma}\isamarkupfalse% |
32000 | 445 |
\ {\isacharbrackleft}code{\isacharunderscore}inline{\isacharbrackright}{\isacharcolon}\isanewline |
28447 | 446 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse% |
447 |
\ simp% |
|
28564 | 448 |
\endisatagquote |
449 |
{\isafoldquote}% |
|
28447 | 450 |
% |
28564 | 451 |
\isadelimquote |
28447 | 452 |
% |
28564 | 453 |
\endisadelimquote |
28447 | 454 |
% |
455 |
\begin{isamarkuptext}% |
|
456 |
\item replacing executable but inconvenient constructs:% |
|
457 |
\end{isamarkuptext}% |
|
458 |
\isamarkuptrue% |
|
459 |
% |
|
28564 | 460 |
\isadelimquote |
28447 | 461 |
% |
28564 | 462 |
\endisadelimquote |
28447 | 463 |
% |
28564 | 464 |
\isatagquote |
28447 | 465 |
\isacommand{lemma}\isamarkupfalse% |
32000 | 466 |
\ {\isacharbrackleft}code{\isacharunderscore}inline{\isacharbrackright}{\isacharcolon}\isanewline |
28447 | 467 |
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse% |
468 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
28564 | 469 |
\endisatagquote |
470 |
{\isafoldquote}% |
|
28447 | 471 |
% |
28564 | 472 |
\isadelimquote |
28447 | 473 |
% |
28564 | 474 |
\endisadelimquote |
28447 | 475 |
% |
476 |
\end{itemize} |
|
477 |
% |
|
478 |
\begin{isamarkuptext}% |
|
479 |
\noindent \emph{Function transformers} provide a very general interface, |
|
480 |
transforming a list of function theorems to another |
|
481 |
list of function theorems, provided that neither the heading |
|
482 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc} |
|
483 |
pattern elimination implemented in |
|
484 |
theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this |
|
485 |
interface. |
|
486 |
||
487 |
\noindent The current setup of the preprocessor may be inspected using |
|
31254 | 488 |
the \hyperlink{command.print-codeproc}{\mbox{\isa{\isacommand{print{\isacharunderscore}codeproc}}}} command. |
28447 | 489 |
\hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} provides a convenient |
490 |
mechanism to inspect the impact of a preprocessor setup |
|
29560 | 491 |
on code equations. |
28447 | 492 |
|
493 |
\begin{warn} |
|
32000 | 494 |
|
495 |
Attribute \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} also applies to the |
|
496 |
preprocessor of the ancient \isa{SML\ code\ generator}; in case |
|
497 |
this is not what you intend, use \hyperlink{attribute.code-inline}{\mbox{\isa{code{\isacharunderscore}inline}}} instead. |
|
28447 | 498 |
\end{warn}% |
499 |
\end{isamarkuptext}% |
|
500 |
\isamarkuptrue% |
|
501 |
% |
|
502 |
\isamarkupsubsection{Datatypes \label{sec:datatypes}% |
|
503 |
} |
|
504 |
\isamarkuptrue% |
|
505 |
% |
|
506 |
\begin{isamarkuptext}% |
|
507 |
Conceptually, any datatype is spanned by a set of |
|
29798 | 508 |
\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 |
509 |
\isa{{\isasymtau}}. The HOL datatype package by default registers any new |
|
510 |
datatype in the table of datatypes, which may be inspected using the |
|
511 |
\hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command. |
|
28447 | 512 |
|
29798 | 513 |
In some cases, it is appropriate to alter or extend this table. As |
514 |
an example, we will develop an alternative representation of the |
|
515 |
queue example given in \secref{sec:intro}. The amortised |
|
516 |
representation is convenient for generating code but exposes its |
|
517 |
\qt{implementation} details, which may be cumbersome when proving |
|
518 |
theorems about it. Therefore, here a simple, straightforward |
|
519 |
representation of queues:% |
|
520 |
\end{isamarkuptext}% |
|
521 |
\isamarkuptrue% |
|
522 |
% |
|
523 |
\isadelimquote |
|
524 |
% |
|
525 |
\endisadelimquote |
|
526 |
% |
|
527 |
\isatagquote |
|
528 |
\isacommand{datatype}\isamarkupfalse% |
|
529 |
\ {\isacharprime}a\ queue\ {\isacharequal}\ Queue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline |
|
530 |
\isanewline |
|
531 |
\isacommand{definition}\isamarkupfalse% |
|
532 |
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
533 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline |
|
534 |
\isanewline |
|
535 |
\isacommand{primrec}\isamarkupfalse% |
|
536 |
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
537 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}Queue\ xs{\isacharparenright}\ {\isacharequal}\ Queue\ {\isacharparenleft}xs\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
538 |
\isanewline |
|
539 |
\isacommand{fun}\isamarkupfalse% |
|
540 |
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
541 |
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
542 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ x{\isacharcomma}\ Queue\ xs{\isacharparenright}{\isachardoublequoteclose}% |
|
543 |
\endisatagquote |
|
544 |
{\isafoldquote}% |
|
545 |
% |
|
546 |
\isadelimquote |
|
547 |
% |
|
548 |
\endisadelimquote |
|
549 |
% |
|
550 |
\begin{isamarkuptext}% |
|
551 |
\noindent This we can use directly for proving; for executing, |
|
552 |
we provide an alternative characterisation:% |
|
28447 | 553 |
\end{isamarkuptext}% |
554 |
\isamarkuptrue% |
|
555 |
% |
|
28564 | 556 |
\isadelimquote |
28447 | 557 |
% |
28564 | 558 |
\endisadelimquote |
28447 | 559 |
% |
28564 | 560 |
\isatagquote |
28447 | 561 |
\isacommand{definition}\isamarkupfalse% |
29798 | 562 |
\ AQueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
563 |
\ \ {\isachardoublequoteopen}AQueue\ xs\ ys\ {\isacharequal}\ Queue\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
28447 | 564 |
\isanewline |
29798 | 565 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse% |
566 |
\ AQueue% |
|
28564 | 567 |
\endisatagquote |
568 |
{\isafoldquote}% |
|
28447 | 569 |
% |
28564 | 570 |
\isadelimquote |
28447 | 571 |
% |
28564 | 572 |
\endisadelimquote |
28447 | 573 |
% |
574 |
\begin{isamarkuptext}% |
|
30227 | 575 |
\noindent Here we define a \qt{constructor} \isa{AQueue} which |
29798 | 576 |
is defined in terms of \isa{Queue} and interprets its arguments |
577 |
according to what the \emph{content} of an amortised queue is supposed |
|
578 |
to be. Equipped with this, we are able to prove the following equations |
|
579 |
for our primitive queue operations which \qt{implement} the simple |
|
580 |
queues in an amortised fashion:% |
|
28447 | 581 |
\end{isamarkuptext}% |
582 |
\isamarkuptrue% |
|
583 |
% |
|
28564 | 584 |
\isadelimquote |
28447 | 585 |
% |
28564 | 586 |
\endisadelimquote |
28447 | 587 |
% |
28564 | 588 |
\isatagquote |
28447 | 589 |
\isacommand{lemma}\isamarkupfalse% |
29798 | 590 |
\ empty{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
591 |
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline |
|
592 |
\ \ \isacommand{unfolding}\isamarkupfalse% |
|
593 |
\ AQueue{\isacharunderscore}def\ empty{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
|
594 |
\ simp\isanewline |
|
595 |
\isanewline |
|
596 |
\isacommand{lemma}\isamarkupfalse% |
|
597 |
\ enqueue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
|
598 |
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline |
|
599 |
\ \ \isacommand{unfolding}\isamarkupfalse% |
|
600 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
|
601 |
\ simp\isanewline |
|
602 |
\isanewline |
|
603 |
\isacommand{lemma}\isamarkupfalse% |
|
604 |
\ dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
|
605 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline |
|
606 |
\ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline |
|
607 |
\ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
608 |
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
609 |
\ \ \isacommand{unfolding}\isamarkupfalse% |
|
610 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
|
611 |
\ simp{\isacharunderscore}all% |
|
28564 | 612 |
\endisatagquote |
613 |
{\isafoldquote}% |
|
28447 | 614 |
% |
28564 | 615 |
\isadelimquote |
28447 | 616 |
% |
28564 | 617 |
\endisadelimquote |
28447 | 618 |
% |
619 |
\begin{isamarkuptext}% |
|
29798 | 620 |
\noindent For completeness, we provide a substitute for the |
621 |
\isa{case} combinator on queues:% |
|
28447 | 622 |
\end{isamarkuptext}% |
623 |
\isamarkuptrue% |
|
624 |
% |
|
28564 | 625 |
\isadelimquote |
28447 | 626 |
% |
28564 | 627 |
\endisadelimquote |
28447 | 628 |
% |
28564 | 629 |
\isatagquote |
29798 | 630 |
\isacommand{lemma}\isamarkupfalse% |
30227 | 631 |
\ queue{\isacharunderscore}case{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
632 |
\ \ {\isachardoublequoteopen}queue{\isacharunderscore}case\ f\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ f\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
29798 | 633 |
\ \ \isacommand{unfolding}\isamarkupfalse% |
30227 | 634 |
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
29798 | 635 |
\ simp% |
28564 | 636 |
\endisatagquote |
637 |
{\isafoldquote}% |
|
28447 | 638 |
% |
28564 | 639 |
\isadelimquote |
28447 | 640 |
% |
28564 | 641 |
\endisadelimquote |
28447 | 642 |
% |
643 |
\begin{isamarkuptext}% |
|
29798 | 644 |
\noindent The resulting code looks as expected:% |
28447 | 645 |
\end{isamarkuptext}% |
646 |
\isamarkuptrue% |
|
647 |
% |
|
28564 | 648 |
\isadelimquote |
28447 | 649 |
% |
28564 | 650 |
\endisadelimquote |
28447 | 651 |
% |
28564 | 652 |
\isatagquote |
28447 | 653 |
% |
654 |
\begin{isamarkuptext}% |
|
28727 | 655 |
\isatypewriter% |
28447 | 656 |
\noindent% |
28714 | 657 |
\hspace*{0pt}structure Example = \\ |
658 |
\hspace*{0pt}struct\\ |
|
659 |
\hspace*{0pt}\\ |
|
29798 | 660 |
\hspace*{0pt}fun foldl f a [] = a\\ |
661 |
\hspace*{0pt} ~| foldl f a (x ::~xs) = foldl f (f a x) xs;\\ |
|
662 |
\hspace*{0pt}\\ |
|
663 |
\hspace*{0pt}fun rev xs = foldl (fn xsa => fn x => x ::~xsa) [] xs;\\ |
|
664 |
\hspace*{0pt}\\ |
|
665 |
\hspace*{0pt}fun null [] = true\\ |
|
666 |
\hspace*{0pt} ~| null (x ::~xs) = false;\\ |
|
28714 | 667 |
\hspace*{0pt}\\ |
29798 | 668 |
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\ |
669 |
\hspace*{0pt}\\ |
|
670 |
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[])\\ |
|
671 |
\hspace*{0pt}\\ |
|
672 |
\hspace*{0pt}fun dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\ |
|
673 |
\hspace*{0pt} ~| dequeue (AQueue (xs,~[])) =\\ |
|
674 |
\hspace*{0pt} ~~~(if null xs then (NONE,~AQueue ([],~[]))\\ |
|
675 |
\hspace*{0pt} ~~~~~else dequeue (AQueue ([],~rev xs)));\\ |
|
676 |
\hspace*{0pt}\\ |
|
677 |
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\ |
|
28714 | 678 |
\hspace*{0pt}\\ |
29297 | 679 |
\hspace*{0pt}end;~(*struct Example*)% |
28447 | 680 |
\end{isamarkuptext}% |
681 |
\isamarkuptrue% |
|
682 |
% |
|
28564 | 683 |
\endisatagquote |
684 |
{\isafoldquote}% |
|
28447 | 685 |
% |
28564 | 686 |
\isadelimquote |
28447 | 687 |
% |
28564 | 688 |
\endisadelimquote |
28447 | 689 |
% |
690 |
\begin{isamarkuptext}% |
|
29798 | 691 |
\noindent From this example, it can be glimpsed that using own |
692 |
constructor sets is a little delicate since it changes the set of |
|
693 |
valid patterns for values of that type. Without going into much |
|
694 |
detail, here some practical hints: |
|
28447 | 695 |
|
696 |
\begin{itemize} |
|
29798 | 697 |
|
698 |
\item When changing the constructor set for datatypes, take care |
|
30227 | 699 |
to provide alternative equations for the \isa{case} combinator. |
29798 | 700 |
|
701 |
\item Values in the target language need not to be normalised -- |
|
702 |
different values in the target language may represent the same |
|
703 |
value in the logic. |
|
704 |
||
705 |
\item Usually, a good methodology to deal with the subtleties of |
|
706 |
pattern matching is to see the type as an abstract type: provide |
|
707 |
a set of operations which operate on the concrete representation |
|
708 |
of the type, and derive further operations by combinations of |
|
709 |
these primitive ones, without relying on a particular |
|
710 |
representation. |
|
711 |
||
28447 | 712 |
\end{itemize}% |
713 |
\end{isamarkuptext}% |
|
714 |
\isamarkuptrue% |
|
715 |
% |
|
30938
c6c9359e474c
wellsortedness is no issue for a user manual any more
haftmann
parents:
30227
diff
changeset
|
716 |
\isamarkupsubsection{Equality% |
28447 | 717 |
} |
718 |
\isamarkuptrue% |
|
719 |
% |
|
720 |
\begin{isamarkuptext}% |
|
721 |
Surely you have already noticed how equality is treated |
|
722 |
by the code generator:% |
|
723 |
\end{isamarkuptext}% |
|
724 |
\isamarkuptrue% |
|
725 |
% |
|
28564 | 726 |
\isadelimquote |
28447 | 727 |
% |
28564 | 728 |
\endisadelimquote |
28447 | 729 |
% |
28564 | 730 |
\isatagquote |
28447 | 731 |
\isacommand{primrec}\isamarkupfalse% |
732 |
\ collect{\isacharunderscore}duplicates\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
733 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline |
|
734 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline |
|
735 |
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline |
|
736 |
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline |
|
737 |
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline |
|
738 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}% |
|
28564 | 739 |
\endisatagquote |
740 |
{\isafoldquote}% |
|
28447 | 741 |
% |
28564 | 742 |
\isadelimquote |
28447 | 743 |
% |
28564 | 744 |
\endisadelimquote |
28447 | 745 |
% |
746 |
\begin{isamarkuptext}% |
|
747 |
\noindent The membership test during preprocessing is rewritten, |
|
748 |
resulting in \isa{op\ mem}, which itself |
|
749 |
performs an explicit equality check.% |
|
750 |
\end{isamarkuptext}% |
|
751 |
\isamarkuptrue% |
|
752 |
% |
|
28564 | 753 |
\isadelimquote |
28447 | 754 |
% |
28564 | 755 |
\endisadelimquote |
28447 | 756 |
% |
28564 | 757 |
\isatagquote |
28447 | 758 |
% |
759 |
\begin{isamarkuptext}% |
|
28727 | 760 |
\isatypewriter% |
28447 | 761 |
\noindent% |
28714 | 762 |
\hspace*{0pt}structure Example = \\ |
763 |
\hspace*{0pt}struct\\ |
|
764 |
\hspace*{0pt}\\ |
|
765 |
\hspace*{0pt}type 'a eq = {\char123}eq :~'a -> 'a -> bool{\char125};\\ |
|
766 |
\hspace*{0pt}fun eq (A{\char95}:'a eq) = {\char35}eq A{\char95};\\ |
|
767 |
\hspace*{0pt}\\ |
|
31045 | 768 |
\hspace*{0pt}fun eqa A{\char95}~a b = eq A{\char95}~a b;\\ |
28714 | 769 |
\hspace*{0pt}\\ |
770 |
\hspace*{0pt}fun member A{\char95}~x [] = false\\ |
|
31045 | 771 |
\hspace*{0pt} ~| member A{\char95}~x (y ::~ys) = eqa A{\char95}~x y orelse member A{\char95}~x ys;\\ |
28714 | 772 |
\hspace*{0pt}\\ |
773 |
\hspace*{0pt}fun collect{\char95}duplicates A{\char95}~xs ys [] = xs\\ |
|
774 |
\hspace*{0pt} ~| collect{\char95}duplicates A{\char95}~xs ys (z ::~zs) =\\ |
|
775 |
\hspace*{0pt} ~~~(if member A{\char95}~z xs\\ |
|
776 |
\hspace*{0pt} ~~~~~then (if member A{\char95}~z ys then collect{\char95}duplicates A{\char95}~xs ys zs\\ |
|
777 |
\hspace*{0pt} ~~~~~~~~~~~~else collect{\char95}duplicates A{\char95}~xs (z ::~ys) zs)\\ |
|
778 |
\hspace*{0pt} ~~~~~else collect{\char95}duplicates A{\char95}~(z ::~xs) (z ::~ys) zs);\\ |
|
779 |
\hspace*{0pt}\\ |
|
29297 | 780 |
\hspace*{0pt}end;~(*struct Example*)% |
28447 | 781 |
\end{isamarkuptext}% |
782 |
\isamarkuptrue% |
|
783 |
% |
|
28564 | 784 |
\endisatagquote |
785 |
{\isafoldquote}% |
|
28447 | 786 |
% |
28564 | 787 |
\isadelimquote |
28447 | 788 |
% |
28564 | 789 |
\endisadelimquote |
28447 | 790 |
% |
791 |
\begin{isamarkuptext}% |
|
792 |
\noindent Obviously, polymorphic equality is implemented the Haskell |
|
793 |
way using a type class. How is this achieved? HOL introduces |
|
794 |
an explicit class \isa{eq} with a corresponding operation |
|
795 |
\isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharequal}\ op\ {\isacharequal}}. |
|
796 |
The preprocessing framework does the rest by propagating the |
|
29560 | 797 |
\isa{eq} constraints through all dependent code equations. |
28447 | 798 |
For datatypes, instances of \isa{eq} are implicitly derived |
799 |
when possible. For other types, you may instantiate \isa{eq} |
|
800 |
manually like any other type class. |
|
801 |
||
802 |
Though this \isa{eq} class is designed to get rarely in |
|
30938
c6c9359e474c
wellsortedness is no issue for a user manual any more
haftmann
parents:
30227
diff
changeset
|
803 |
the way, in some cases the automatically derived code equations |
28447 | 804 |
for equality on a particular type may not be appropriate. |
805 |
As example, watch the following datatype representing |
|
806 |
monomorphic parametric types (where type constructors |
|
807 |
are referred to by natural numbers):% |
|
808 |
\end{isamarkuptext}% |
|
809 |
\isamarkuptrue% |
|
810 |
% |
|
28564 | 811 |
\isadelimquote |
28447 | 812 |
% |
28564 | 813 |
\endisadelimquote |
28447 | 814 |
% |
28564 | 815 |
\isatagquote |
28447 | 816 |
\isacommand{datatype}\isamarkupfalse% |
817 |
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}% |
|
28564 | 818 |
\endisatagquote |
819 |
{\isafoldquote}% |
|
28447 | 820 |
% |
28564 | 821 |
\isadelimquote |
28447 | 822 |
% |
28564 | 823 |
\endisadelimquote |
28447 | 824 |
% |
825 |
\isadelimproof |
|
826 |
% |
|
827 |
\endisadelimproof |
|
828 |
% |
|
829 |
\isatagproof |
|
830 |
% |
|
831 |
\endisatagproof |
|
832 |
{\isafoldproof}% |
|
833 |
% |
|
834 |
\isadelimproof |
|
835 |
% |
|
836 |
\endisadelimproof |
|
837 |
% |
|
838 |
\begin{isamarkuptext}% |
|
28462 | 839 |
\noindent Then code generation for SML would fail with a message |
28447 | 840 |
that the generated code contains illegal mutual dependencies: |
28462 | 841 |
the theorem \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymequiv}\ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}} already requires the |
28447 | 842 |
instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires |
28462 | 843 |
\isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymequiv}\ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}}; Haskell has no problem with mutually |
844 |
recursive \isa{instance} and \isa{function} definitions, |
|
28593 | 845 |
but the SML serialiser does not support this. |
28447 | 846 |
|
847 |
In such cases, you have to provide your own equality equations |
|
848 |
involving auxiliary constants. In our case, |
|
849 |
\isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:% |
|
850 |
\end{isamarkuptext}% |
|
851 |
\isamarkuptrue% |
|
852 |
% |
|
28564 | 853 |
\isadelimquote |
28447 | 854 |
% |
28564 | 855 |
\endisadelimquote |
28447 | 856 |
% |
28564 | 857 |
\isatagquote |
28447 | 858 |
\isacommand{lemma}\isamarkupfalse% |
28562 | 859 |
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
28447 | 860 |
\ \ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline |
28462 | 861 |
\ \ \ \ \ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ list{\isacharunderscore}all{\isadigit{2}}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}{\isachardoublequoteclose}\isanewline |
28447 | 862 |
\ \ \isacommand{by}\isamarkupfalse% |
863 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}% |
|
28564 | 864 |
\endisatagquote |
865 |
{\isafoldquote}% |
|
28447 | 866 |
% |
28564 | 867 |
\isadelimquote |
28447 | 868 |
% |
28564 | 869 |
\endisadelimquote |
28447 | 870 |
% |
871 |
\begin{isamarkuptext}% |
|
872 |
\noindent does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:% |
|
873 |
\end{isamarkuptext}% |
|
874 |
\isamarkuptrue% |
|
875 |
% |
|
28564 | 876 |
\isadelimquote |
28447 | 877 |
% |
28564 | 878 |
\endisadelimquote |
28447 | 879 |
% |
28564 | 880 |
\isatagquote |
28447 | 881 |
% |
882 |
\begin{isamarkuptext}% |
|
28727 | 883 |
\isatypewriter% |
28447 | 884 |
\noindent% |
28714 | 885 |
\hspace*{0pt}structure Example = \\ |
886 |
\hspace*{0pt}struct\\ |
|
887 |
\hspace*{0pt}\\ |
|
30121 | 888 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\ |
28714 | 889 |
\hspace*{0pt}\\ |
890 |
\hspace*{0pt}fun null [] = true\\ |
|
891 |
\hspace*{0pt} ~| null (x ::~xs) = false;\\ |
|
892 |
\hspace*{0pt}\\ |
|
31150 | 893 |
\hspace*{0pt}fun eq{\char95}nat (Suc nat') Zero{\char95}nat = false\\ |
894 |
\hspace*{0pt} ~| eq{\char95}nat Zero{\char95}nat (Suc nat') = false\\ |
|
28714 | 895 |
\hspace*{0pt} ~| eq{\char95}nat (Suc nat) (Suc nat') = eq{\char95}nat nat nat'\\ |
896 |
\hspace*{0pt} ~| eq{\char95}nat Zero{\char95}nat Zero{\char95}nat = true;\\ |
|
897 |
\hspace*{0pt}\\ |
|
898 |
\hspace*{0pt}datatype monotype = Mono of nat * monotype list;\\ |
|
899 |
\hspace*{0pt}\\ |
|
900 |
\hspace*{0pt}fun list{\char95}all2 p (x ::~xs) (y ::~ys) = p x y andalso list{\char95}all2 p xs ys\\ |
|
901 |
\hspace*{0pt} ~| list{\char95}all2 p xs [] = null xs\\ |
|
902 |
\hspace*{0pt} ~| list{\char95}all2 p [] ys = null ys;\\ |
|
903 |
\hspace*{0pt}\\ |
|
29297 | 904 |
\hspace*{0pt}fun eq{\char95}monotype (Mono (tyco1,~typargs1)) (Mono (tyco2,~typargs2)) =\\ |
28714 | 905 |
\hspace*{0pt} ~eq{\char95}nat tyco1 tyco2 andalso list{\char95}all2 eq{\char95}monotype typargs1 typargs2;\\ |
906 |
\hspace*{0pt}\\ |
|
29297 | 907 |
\hspace*{0pt}end;~(*struct Example*)% |
28447 | 908 |
\end{isamarkuptext}% |
909 |
\isamarkuptrue% |
|
910 |
% |
|
28564 | 911 |
\endisatagquote |
912 |
{\isafoldquote}% |
|
28447 | 913 |
% |
28564 | 914 |
\isadelimquote |
28447 | 915 |
% |
28564 | 916 |
\endisadelimquote |
28447 | 917 |
% |
28462 | 918 |
\isamarkupsubsection{Explicit partiality% |
28447 | 919 |
} |
920 |
\isamarkuptrue% |
|
921 |
% |
|
922 |
\begin{isamarkuptext}% |
|
28462 | 923 |
Partiality usually enters the game by partial patterns, as |
924 |
in the following example, again for amortised queues:% |
|
925 |
\end{isamarkuptext}% |
|
926 |
\isamarkuptrue% |
|
927 |
% |
|
28564 | 928 |
\isadelimquote |
28462 | 929 |
% |
28564 | 930 |
\endisadelimquote |
28462 | 931 |
% |
28564 | 932 |
\isatagquote |
29798 | 933 |
\isacommand{definition}\isamarkupfalse% |
28462 | 934 |
\ strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
29798 | 935 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ q\ {\isacharequal}\ {\isacharparenleft}case\ dequeue\ q\isanewline |
936 |
\ \ \ \ of\ {\isacharparenleft}Some\ x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
937 |
\isanewline |
|
938 |
\isacommand{lemma}\isamarkupfalse% |
|
939 |
\ strict{\isacharunderscore}dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
|
940 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
941 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline |
|
942 |
\ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
943 |
\ \ \isacommand{by}\isamarkupfalse% |
|
944 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharunderscore}def\ dequeue{\isacharunderscore}AQueue\ split{\isacharcolon}\ list{\isachardot}splits{\isacharparenright}% |
|
28564 | 945 |
\endisatagquote |
946 |
{\isafoldquote}% |
|
28462 | 947 |
% |
28564 | 948 |
\isadelimquote |
28462 | 949 |
% |
28564 | 950 |
\endisadelimquote |
28462 | 951 |
% |
952 |
\begin{isamarkuptext}% |
|
953 |
\noindent In the corresponding code, there is no equation |
|
30227 | 954 |
for the pattern \isa{AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}}:% |
28462 | 955 |
\end{isamarkuptext}% |
956 |
\isamarkuptrue% |
|
957 |
% |
|
28564 | 958 |
\isadelimquote |
28462 | 959 |
% |
28564 | 960 |
\endisadelimquote |
28462 | 961 |
% |
28564 | 962 |
\isatagquote |
28462 | 963 |
% |
964 |
\begin{isamarkuptext}% |
|
28727 | 965 |
\isatypewriter% |
28462 | 966 |
\noindent% |
29297 | 967 |
\hspace*{0pt}strict{\char95}dequeue ::~forall a.~Queue a -> (a,~Queue a);\\ |
29798 | 968 |
\hspace*{0pt}strict{\char95}dequeue (AQueue xs []) =\\ |
31848 | 969 |
\hspace*{0pt} ~let {\char123}\\ |
28714 | 970 |
\hspace*{0pt} ~~~(y :~ys) = rev xs;\\ |
31848 | 971 |
\hspace*{0pt} ~{\char125}~in (y,~AQueue [] ys);\\ |
29798 | 972 |
\hspace*{0pt}strict{\char95}dequeue (AQueue xs (y :~ys)) = (y,~AQueue xs ys);% |
28462 | 973 |
\end{isamarkuptext}% |
974 |
\isamarkuptrue% |
|
975 |
% |
|
28564 | 976 |
\endisatagquote |
977 |
{\isafoldquote}% |
|
28462 | 978 |
% |
28564 | 979 |
\isadelimquote |
28462 | 980 |
% |
28564 | 981 |
\endisadelimquote |
28462 | 982 |
% |
983 |
\begin{isamarkuptext}% |
|
984 |
\noindent In some cases it is desirable to have this |
|
985 |
pseudo-\qt{partiality} more explicitly, e.g.~as follows:% |
|
986 |
\end{isamarkuptext}% |
|
987 |
\isamarkuptrue% |
|
988 |
% |
|
28564 | 989 |
\isadelimquote |
28462 | 990 |
% |
28564 | 991 |
\endisadelimquote |
28462 | 992 |
% |
28564 | 993 |
\isatagquote |
28462 | 994 |
\isacommand{axiomatization}\isamarkupfalse% |
995 |
\ empty{\isacharunderscore}queue\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline |
|
996 |
\isanewline |
|
29798 | 997 |
\isacommand{definition}\isamarkupfalse% |
28462 | 998 |
\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
29798 | 999 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ q\ {\isacharequal}\ {\isacharparenleft}case\ dequeue\ q\ of\ {\isacharparenleft}Some\ x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isacharbar}\ {\isacharunderscore}\ {\isasymRightarrow}\ empty{\isacharunderscore}queue{\isacharparenright}{\isachardoublequoteclose}\isanewline |
28462 | 1000 |
\isanewline |
29798 | 1001 |
\isacommand{lemma}\isamarkupfalse% |
1002 |
\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline |
|
1003 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ empty{\isacharunderscore}queue\isanewline |
|
1004 |
\ \ \ \ \ else\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
1005 |
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline |
|
1006 |
\ \ \ \ \ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
1007 |
\ \ \isacommand{by}\isamarkupfalse% |
|
1008 |
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}def\ dequeue{\isacharunderscore}AQueue\ split{\isacharcolon}\ list{\isachardot}splits{\isacharparenright}% |
|
28564 | 1009 |
\endisatagquote |
1010 |
{\isafoldquote}% |
|
28462 | 1011 |
% |
28564 | 1012 |
\isadelimquote |
28462 | 1013 |
% |
28564 | 1014 |
\endisadelimquote |
28462 | 1015 |
% |
1016 |
\begin{isamarkuptext}% |
|
29798 | 1017 |
Observe that on the right hand side of the definition of \isa{strict{\isacharunderscore}dequeue{\isacharprime}} the constant \isa{empty{\isacharunderscore}queue} occurs |
1018 |
which is unspecified. |
|
28462 | 1019 |
|
29798 | 1020 |
Normally, if constants without any code equations occur in a |
1021 |
program, the code generator complains (since in most cases this is |
|
1022 |
not what the user expects). But such constants can also be thought |
|
1023 |
of as function definitions with no equations which always fail, |
|
1024 |
since there is never a successful pattern match on the left hand |
|
1025 |
side. In order to categorise a constant into that category |
|
1026 |
explicitly, use \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}:% |
|
28462 | 1027 |
\end{isamarkuptext}% |
1028 |
\isamarkuptrue% |
|
1029 |
% |
|
28564 | 1030 |
\isadelimquote |
28462 | 1031 |
% |
28564 | 1032 |
\endisadelimquote |
28462 | 1033 |
% |
28564 | 1034 |
\isatagquote |
28462 | 1035 |
\isacommand{code{\isacharunderscore}abort}\isamarkupfalse% |
1036 |
\ empty{\isacharunderscore}queue% |
|
28564 | 1037 |
\endisatagquote |
1038 |
{\isafoldquote}% |
|
28462 | 1039 |
% |
28564 | 1040 |
\isadelimquote |
28462 | 1041 |
% |
28564 | 1042 |
\endisadelimquote |
28462 | 1043 |
% |
1044 |
\begin{isamarkuptext}% |
|
1045 |
\noindent Then the code generator will just insert an error or |
|
1046 |
exception at the appropriate position:% |
|
1047 |
\end{isamarkuptext}% |
|
1048 |
\isamarkuptrue% |
|
1049 |
% |
|
28564 | 1050 |
\isadelimquote |
28462 | 1051 |
% |
28564 | 1052 |
\endisadelimquote |
28462 | 1053 |
% |
28564 | 1054 |
\isatagquote |
28462 | 1055 |
% |
1056 |
\begin{isamarkuptext}% |
|
28727 | 1057 |
\isatypewriter% |
28462 | 1058 |
\noindent% |
29297 | 1059 |
\hspace*{0pt}empty{\char95}queue ::~forall a.~a;\\ |
28714 | 1060 |
\hspace*{0pt}empty{\char95}queue = error {\char34}empty{\char95}queue{\char34};\\ |
1061 |
\hspace*{0pt}\\ |
|
29297 | 1062 |
\hspace*{0pt}strict{\char95}dequeue' ::~forall a.~Queue a -> (a,~Queue a);\\ |
29798 | 1063 |
\hspace*{0pt}strict{\char95}dequeue' (AQueue xs (y :~ys)) = (y,~AQueue xs ys);\\ |
1064 |
\hspace*{0pt}strict{\char95}dequeue' (AQueue xs []) =\\ |
|
1065 |
\hspace*{0pt} ~(if nulla xs then empty{\char95}queue\\ |
|
1066 |
\hspace*{0pt} ~~~else strict{\char95}dequeue' (AQueue [] (rev xs)));% |
|
28462 | 1067 |
\end{isamarkuptext}% |
1068 |
\isamarkuptrue% |
|
1069 |
% |
|
28564 | 1070 |
\endisatagquote |
1071 |
{\isafoldquote}% |
|
28462 | 1072 |
% |
28564 | 1073 |
\isadelimquote |
28462 | 1074 |
% |
28564 | 1075 |
\endisadelimquote |
28462 | 1076 |
% |
1077 |
\begin{isamarkuptext}% |
|
1078 |
\noindent This feature however is rarely needed in practice. |
|
1079 |
Note also that the \isa{HOL} default setup already declares |
|
1080 |
\isa{undefined} as \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}, which is most |
|
1081 |
likely to be used in such situations.% |
|
28447 | 1082 |
\end{isamarkuptext}% |
1083 |
\isamarkuptrue% |
|
1084 |
% |
|
1085 |
\isadelimtheory |
|
1086 |
% |
|
1087 |
\endisadelimtheory |
|
1088 |
% |
|
1089 |
\isatagtheory |
|
1090 |
\isacommand{end}\isamarkupfalse% |
|
1091 |
% |
|
1092 |
\endisatagtheory |
|
1093 |
{\isafoldtheory}% |
|
1094 |
% |
|
1095 |
\isadelimtheory |
|
1096 |
% |
|
1097 |
\endisadelimtheory |
|
1098 |
\isanewline |
|
28462 | 1099 |
\ \end{isabellebody}% |
28447 | 1100 |
%%% Local Variables: |
1101 |
%%% mode: latex |
|
1102 |
%%% TeX-master: "root" |
|
1103 |
%%% End: |