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