author | haftmann |
Fri, 26 Jan 2007 09:24:35 +0100 | |
changeset 22188 | a63889770d57 |
parent 22060 | 8a37090726e8 |
child 22292 | 3b118010ec08 |
permissions | -rw-r--r-- |
20967 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Codegen}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
\isanewline |
|
7 |
% |
|
8 |
\endisadelimtheory |
|
9 |
% |
|
10 |
\isatagtheory |
|
21172 | 11 |
% |
20967 | 12 |
\endisatagtheory |
13 |
{\isafoldtheory}% |
|
14 |
% |
|
15 |
\isadelimtheory |
|
16 |
% |
|
17 |
\endisadelimtheory |
|
18 |
% |
|
21172 | 19 |
\isadelimML |
20 |
% |
|
21 |
\endisadelimML |
|
22 |
% |
|
23 |
\isatagML |
|
24 |
% |
|
25 |
\endisatagML |
|
26 |
{\isafoldML}% |
|
27 |
% |
|
28 |
\isadelimML |
|
29 |
% |
|
30 |
\endisadelimML |
|
31 |
% |
|
20967 | 32 |
\isamarkupchapter{Code generation from Isabelle theories% |
33 |
} |
|
34 |
\isamarkuptrue% |
|
35 |
% |
|
36 |
\isamarkupsection{Introduction% |
|
37 |
} |
|
38 |
\isamarkuptrue% |
|
39 |
% |
|
21172 | 40 |
\isamarkupsubsection{Motivation% |
41 |
} |
|
42 |
\isamarkuptrue% |
|
43 |
% |
|
20967 | 44 |
\begin{isamarkuptext}% |
21172 | 45 |
Executing formal specifications as programs is a well-established |
46 |
topic in the theorem proving community. With increasing |
|
47 |
application of theorem proving systems in the area of |
|
48 |
software development and verification, its relevance manifests |
|
49 |
for running test cases and rapid prototyping. In logical |
|
50 |
calculi like constructive type theory, |
|
51 |
a notion of executability is implicit due to the nature |
|
52 |
of the calculus. In contrast, specifications in Isabelle/HOL |
|
53 |
can be highly non-executable. In order to bridge |
|
54 |
the gap between logic and executable specifications, |
|
55 |
an explicit non-trivial transformation has to be applied: |
|
56 |
code generation. |
|
57 |
||
58 |
This tutorial introduces a generic code generator for the |
|
59 |
Isabelle system \cite{isa-tutorial}. |
|
60 |
Generic in the sense that the |
|
61 |
\qn{target language} for which code shall ultimately be |
|
62 |
generated is not fixed but may be an arbitrary state-of-the-art |
|
63 |
functional programming language (currently, the implementation |
|
64 |
supports SML \cite{web:sml} and Haskell \cite{web:haskell}). |
|
65 |
We aim to provide a |
|
66 |
versatile environment |
|
67 |
suitable for software development and verification, |
|
68 |
structuring the process |
|
69 |
of code generation into a small set of orthogonal principles |
|
70 |
while achieving a big coverage of application areas |
|
21348 | 71 |
with maximum flexibility. |
72 |
||
73 |
For readers, some familiarity and experience |
|
74 |
with the the ingredients |
|
75 |
of the HOL \emph{Main} theory is assumed.% |
|
20967 | 76 |
\end{isamarkuptext}% |
77 |
\isamarkuptrue% |
|
78 |
% |
|
21172 | 79 |
\isamarkupsubsection{Overview% |
80 |
} |
|
81 |
\isamarkuptrue% |
|
82 |
% |
|
83 |
\begin{isamarkuptext}% |
|
84 |
The code generator aims to be usable with no further ado |
|
85 |
in most cases while allowing for detailed customization. |
|
86 |
This manifests in the structure of this tutorial: this introduction |
|
87 |
continues with a short introduction of concepts. Section |
|
21186 | 88 |
\secref{sec:basics} explains how to use the framework naively, |
21172 | 89 |
presuming a reasonable default setup. Then, section |
90 |
\secref{sec:advanced} deals with advanced topics, |
|
91 |
introducing further aspects of the code generator framework |
|
92 |
in a motivation-driven manner. Last, section \secref{sec:ml} |
|
93 |
introduces the framework's internal programming interfaces. |
|
94 |
||
95 |
\begin{warn} |
|
96 |
Ultimately, the code generator which this tutorial deals with |
|
97 |
is supposed to replace the already established code generator |
|
98 |
by Stefan Berghofer \cite{Berghofer-Nipkow:2002}. |
|
99 |
So, for the moment, there are two distinct code generators |
|
100 |
in Isabelle. |
|
101 |
Also note that while the framework itself is largely |
|
102 |
object-logic independent, only HOL provides a reasonable |
|
103 |
framework setup. |
|
104 |
\end{warn}% |
|
105 |
\end{isamarkuptext}% |
|
106 |
\isamarkuptrue% |
|
107 |
% |
|
108 |
\isamarkupsubsection{Code generation process% |
|
109 |
} |
|
110 |
\isamarkuptrue% |
|
111 |
% |
|
112 |
\begin{isamarkuptext}% |
|
21348 | 113 |
\begin{figure}[h] |
114 |
\centering |
|
115 |
\includegraphics[width=0.7\textwidth]{codegen_process} |
|
116 |
\caption{code generator -- processing overview} |
|
117 |
\label{fig:process} |
|
118 |
\end{figure} |
|
119 |
||
120 |
The code generator employs a notion of executability |
|
21172 | 121 |
for three foundational executable ingredients known |
122 |
from functional programming: |
|
22060 | 123 |
\emph{defining equations}, \emph{datatypes}, and |
124 |
\emph{type classes}. A defining equation as a first approximation |
|
21172 | 125 |
is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t} |
126 |
(an equation headed by a constant \isa{f} with arguments |
|
127 |
\isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}. |
|
22060 | 128 |
Code generation aims to turn defining equations |
21172 | 129 |
into a functional program by running through |
130 |
a process (see figure \ref{fig:process}): |
|
131 |
||
132 |
\begin{itemize} |
|
133 |
||
134 |
\item Out of the vast collection of theorems proven in a |
|
135 |
\qn{theory}, a reasonable subset modeling |
|
22060 | 136 |
defining equations is \qn{selected}. |
21172 | 137 |
|
138 |
\item On those selected theorems, certain |
|
139 |
transformations are carried out |
|
140 |
(\qn{preprocessing}). Their purpose is to turn theorems |
|
141 |
representing non- or badly executable |
|
142 |
specifications into equivalent but executable counterparts. |
|
143 |
The result is a structured collection of \qn{code theorems}. |
|
144 |
||
145 |
\item These \qn{code theorems} then are extracted |
|
146 |
into an Haskell-like intermediate |
|
147 |
language. |
|
148 |
||
149 |
\item Finally, out of the intermediate language the final |
|
150 |
code in the desired \qn{target language} is \qn{serialized}. |
|
151 |
||
152 |
\end{itemize} |
|
153 |
||
154 |
From these steps, only the two last are carried out |
|
155 |
outside the logic; by keeping this layer as |
|
156 |
thin as possible, the amount of code to trust is |
|
157 |
kept to a minimum.% |
|
158 |
\end{isamarkuptext}% |
|
159 |
\isamarkuptrue% |
|
160 |
% |
|
161 |
\isamarkupsection{Basics \label{sec:basics}% |
|
20967 | 162 |
} |
163 |
\isamarkuptrue% |
|
164 |
% |
|
165 |
\isamarkupsubsection{Invoking the code generator% |
|
166 |
} |
|
167 |
\isamarkuptrue% |
|
168 |
% |
|
21172 | 169 |
\begin{isamarkuptext}% |
170 |
Thanks to a reasonable setup of the HOL theories, in |
|
171 |
most cases code generation proceeds without further ado:% |
|
172 |
\end{isamarkuptext}% |
|
173 |
\isamarkuptrue% |
|
174 |
\isacommand{consts}\isamarkupfalse% |
|
175 |
\isanewline |
|
176 |
\ \ fac\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline |
|
177 |
\isanewline |
|
178 |
\isacommand{primrec}\isamarkupfalse% |
|
179 |
\isanewline |
|
180 |
\ \ {\isachardoublequoteopen}fac\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline |
|
181 |
\ \ {\isachardoublequoteopen}fac\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ Suc\ n\ {\isacharasterisk}\ fac\ n{\isachardoublequoteclose}% |
|
182 |
\begin{isamarkuptext}% |
|
183 |
This executable specification is now turned to SML code:% |
|
184 |
\end{isamarkuptext}% |
|
185 |
\isamarkuptrue% |
|
186 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
187 |
\ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
188 |
\begin{isamarkuptext}% |
|
21348 | 189 |
The \isa{{\isasymCODEGEN}} command takes a space-separated list of |
21172 | 190 |
constants together with \qn{serialization directives} |
191 |
in parentheses. These start with a \qn{target language} |
|
21186 | 192 |
identifier, followed by arguments, their semantics |
21172 | 193 |
depending on the target. In the SML case, a filename |
194 |
is given where to write the generated code to. |
|
195 |
||
22060 | 196 |
Internally, the defining equations for all selected |
21186 | 197 |
constants are taken, including any transitively required |
21172 | 198 |
constants, datatypes and classes, resulting in the following |
199 |
code: |
|
200 |
||
201 |
\lstsml{Thy/examples/fac.ML} |
|
202 |
||
203 |
The code generator will complain when a required |
|
204 |
ingredient does not provide a executable counterpart. |
|
205 |
This is the case if an involved type is not a datatype:% |
|
206 |
\end{isamarkuptext}% |
|
207 |
\isamarkuptrue% |
|
208 |
% |
|
209 |
\isadelimML |
|
210 |
% |
|
211 |
\endisadelimML |
|
212 |
% |
|
213 |
\isatagML |
|
214 |
% |
|
215 |
\endisatagML |
|
216 |
{\isafoldML}% |
|
217 |
% |
|
218 |
\isadelimML |
|
219 |
\isanewline |
|
220 |
% |
|
221 |
\endisadelimML |
|
222 |
\isacommand{typedecl}\isamarkupfalse% |
|
223 |
\ {\isacharprime}a\ foo\isanewline |
|
224 |
\isanewline |
|
225 |
\isacommand{definition}\isamarkupfalse% |
|
226 |
\isanewline |
|
21993 | 227 |
\ \ bar\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ foo\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21172 | 228 |
\ \ {\isachardoublequoteopen}bar\ x\ y\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline |
229 |
% |
|
230 |
\isadelimML |
|
231 |
% |
|
232 |
\endisadelimML |
|
233 |
% |
|
234 |
\isatagML |
|
235 |
% |
|
236 |
\endisatagML |
|
237 |
{\isafoldML}% |
|
238 |
% |
|
239 |
\isadelimML |
|
240 |
% |
|
241 |
\endisadelimML |
|
242 |
\isanewline |
|
243 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
244 |
\ bar\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}type{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
245 |
\begin{isamarkuptext}% |
|
246 |
\noindent will result in an error. Likewise, generating code |
|
247 |
for constants not yielding |
|
22060 | 248 |
a defining equation will fail, e.g.~the Hilbert choice |
21172 | 249 |
operation \isa{SOME}:% |
250 |
\end{isamarkuptext}% |
|
251 |
\isamarkuptrue% |
|
252 |
% |
|
253 |
\isadelimML |
|
254 |
% |
|
255 |
\endisadelimML |
|
256 |
% |
|
257 |
\isatagML |
|
258 |
% |
|
259 |
\endisatagML |
|
260 |
{\isafoldML}% |
|
261 |
% |
|
262 |
\isadelimML |
|
263 |
\isanewline |
|
264 |
% |
|
265 |
\endisadelimML |
|
266 |
\isacommand{definition}\isamarkupfalse% |
|
267 |
\isanewline |
|
21993 | 268 |
\ \ pick{\isacharunderscore}some\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21172 | 269 |
\ \ {\isachardoublequoteopen}pick{\isacharunderscore}some\ xs\ {\isacharequal}\ {\isacharparenleft}SOME\ x{\isachardot}\ x\ {\isasymin}\ set\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline |
270 |
% |
|
271 |
\isadelimML |
|
272 |
% |
|
273 |
\endisadelimML |
|
274 |
% |
|
275 |
\isatagML |
|
276 |
% |
|
277 |
\endisatagML |
|
278 |
{\isafoldML}% |
|
279 |
% |
|
280 |
\isadelimML |
|
281 |
% |
|
282 |
\endisadelimML |
|
283 |
\isanewline |
|
284 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
285 |
\ pick{\isacharunderscore}some\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}const{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
20967 | 286 |
\isamarkupsubsection{Theorem selection% |
287 |
} |
|
288 |
\isamarkuptrue% |
|
289 |
% |
|
21172 | 290 |
\begin{isamarkuptext}% |
22060 | 291 |
The list of all defining equations in a theory may be inspected |
21348 | 292 |
using the \isa{{\isasymPRINTCODETHMS}} command:% |
21172 | 293 |
\end{isamarkuptext}% |
294 |
\isamarkuptrue% |
|
295 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse% |
|
296 |
% |
|
297 |
\begin{isamarkuptext}% |
|
298 |
\noindent which displays a table of constant with corresponding |
|
22060 | 299 |
defining equations (the additional stuff displayed |
21172 | 300 |
shall not bother us for the moment). If this table does |
301 |
not provide at least one function |
|
21186 | 302 |
equation, the table of primitive definitions is searched |
21172 | 303 |
whether it provides one. |
304 |
||
305 |
The typical HOL tools are already set up in a way that |
|
21348 | 306 |
function definitions introduced by \isa{{\isasymFUN}}, |
307 |
\isa{{\isasymFUNCTION}}, \isa{{\isasymPRIMREC}} |
|
308 |
\isa{{\isasymRECDEF}} are implicitly propagated |
|
22060 | 309 |
to this defining equation table. Specific theorems may be |
21172 | 310 |
selected using an attribute: \emph{code func}. As example, |
311 |
a weight selector function:% |
|
312 |
\end{isamarkuptext}% |
|
313 |
\isamarkuptrue% |
|
314 |
\isacommand{consts}\isamarkupfalse% |
|
315 |
\isanewline |
|
316 |
\ \ pick\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline |
|
317 |
\isanewline |
|
318 |
\isacommand{primrec}\isamarkupfalse% |
|
319 |
\isanewline |
|
320 |
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharequal}\ x\ in\isanewline |
|
321 |
\ \ \ \ if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}% |
|
322 |
\begin{isamarkuptext}% |
|
323 |
We want to eliminate the explicit destruction |
|
324 |
of \isa{x} to \isa{{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}}:% |
|
325 |
\end{isamarkuptext}% |
|
326 |
\isamarkuptrue% |
|
327 |
\isacommand{lemma}\isamarkupfalse% |
|
328 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
329 |
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
330 |
% |
|
331 |
\isadelimproof |
|
332 |
\ \ % |
|
333 |
\endisadelimproof |
|
334 |
% |
|
335 |
\isatagproof |
|
336 |
\isacommand{by}\isamarkupfalse% |
|
337 |
\ simp% |
|
338 |
\endisatagproof |
|
339 |
{\isafoldproof}% |
|
340 |
% |
|
341 |
\isadelimproof |
|
342 |
\isanewline |
|
343 |
% |
|
344 |
\endisadelimproof |
|
345 |
\isanewline |
|
346 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
347 |
\ pick\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
348 |
\begin{isamarkuptext}% |
|
22060 | 349 |
This theorem is now added to the defining equation table: |
21172 | 350 |
|
351 |
\lstsml{Thy/examples/pick1.ML} |
|
352 |
||
353 |
It might be convenient to remove the pointless original |
|
354 |
equation, using the \emph{nofunc} attribute:% |
|
355 |
\end{isamarkuptext}% |
|
356 |
\isamarkuptrue% |
|
357 |
\isacommand{lemmas}\isamarkupfalse% |
|
358 |
\ {\isacharbrackleft}code\ nofunc{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline |
|
359 |
\isanewline |
|
360 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
361 |
\ pick\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
362 |
\begin{isamarkuptext}% |
|
363 |
\lstsml{Thy/examples/pick2.ML} |
|
364 |
||
365 |
Syntactic redundancies are implicitly dropped. For example, |
|
366 |
using a modified version of the \isa{fac} function |
|
22060 | 367 |
as defining equation, the then redundant (since |
368 |
syntactically subsumed) original defining equations |
|
21172 | 369 |
are dropped, resulting in a warning:% |
370 |
\end{isamarkuptext}% |
|
371 |
\isamarkuptrue% |
|
372 |
\isacommand{lemma}\isamarkupfalse% |
|
373 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
374 |
\ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
375 |
% |
|
376 |
\isadelimproof |
|
377 |
\ \ % |
|
378 |
\endisadelimproof |
|
379 |
% |
|
380 |
\isatagproof |
|
381 |
\isacommand{by}\isamarkupfalse% |
|
382 |
\ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all% |
|
383 |
\endisatagproof |
|
384 |
{\isafoldproof}% |
|
385 |
% |
|
386 |
\isadelimproof |
|
387 |
\isanewline |
|
388 |
% |
|
389 |
\endisadelimproof |
|
390 |
\isanewline |
|
391 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
392 |
\ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
393 |
\begin{isamarkuptext}% |
|
394 |
\lstsml{Thy/examples/fac_case.ML} |
|
395 |
||
396 |
\begin{warn} |
|
397 |
Some statements in this section have to be treated with some |
|
398 |
caution. First, since the HOL function package is still |
|
399 |
under development, its setup with respect to code generation |
|
400 |
may differ from what is presumed here. |
|
401 |
Further, the attributes \emph{code} and \emph{code del} |
|
402 |
associated with the existing code generator also apply to |
|
403 |
the new one: \emph{code} implies \emph{code func}, |
|
404 |
and \emph{code del} implies \emph{code nofunc}. |
|
405 |
\end{warn}% |
|
406 |
\end{isamarkuptext}% |
|
407 |
\isamarkuptrue% |
|
408 |
% |
|
20967 | 409 |
\isamarkupsubsection{Type classes% |
410 |
} |
|
411 |
\isamarkuptrue% |
|
412 |
% |
|
21172 | 413 |
\begin{isamarkuptext}% |
414 |
Type classes enter the game via the Isar class package. |
|
415 |
For a short introduction how to use it, see \cite{isabelle-classes}; |
|
416 |
here we just illustrate its impact on code generation. |
|
417 |
||
418 |
In a target language, type classes may be represented |
|
21186 | 419 |
natively (as in the case of Haskell). For languages |
21172 | 420 |
like SML, they are implemented using \emph{dictionaries}. |
21186 | 421 |
Our following example specifies a class \qt{null}, |
21172 | 422 |
assigning to each of its inhabitants a \qt{null} value:% |
423 |
\end{isamarkuptext}% |
|
424 |
\isamarkuptrue% |
|
425 |
\isacommand{class}\isamarkupfalse% |
|
426 |
\ null\ {\isacharequal}\isanewline |
|
427 |
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline |
|
428 |
\isanewline |
|
429 |
\isacommand{consts}\isamarkupfalse% |
|
430 |
\isanewline |
|
431 |
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline |
|
432 |
\isanewline |
|
433 |
\isacommand{primrec}\isamarkupfalse% |
|
434 |
\isanewline |
|
435 |
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline |
|
436 |
\ \ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}% |
|
437 |
\begin{isamarkuptext}% |
|
438 |
We provide some instances for our \isa{null}:% |
|
439 |
\end{isamarkuptext}% |
|
440 |
\isamarkuptrue% |
|
441 |
\isacommand{instance}\isamarkupfalse% |
|
442 |
\ option\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
443 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ None{\isachardoublequoteclose}% |
|
444 |
\isadelimproof |
|
445 |
\ % |
|
446 |
\endisadelimproof |
|
447 |
% |
|
448 |
\isatagproof |
|
449 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
450 |
% |
|
451 |
\endisatagproof |
|
452 |
{\isafoldproof}% |
|
453 |
% |
|
454 |
\isadelimproof |
|
455 |
% |
|
456 |
\endisadelimproof |
|
457 |
\isanewline |
|
458 |
\isanewline |
|
459 |
\isacommand{instance}\isamarkupfalse% |
|
460 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
461 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}% |
|
462 |
\isadelimproof |
|
463 |
\ % |
|
464 |
\endisadelimproof |
|
465 |
% |
|
466 |
\isatagproof |
|
467 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
468 |
% |
|
469 |
\endisatagproof |
|
470 |
{\isafoldproof}% |
|
471 |
% |
|
472 |
\isadelimproof |
|
473 |
% |
|
474 |
\endisadelimproof |
|
475 |
% |
|
476 |
\begin{isamarkuptext}% |
|
477 |
Constructing a dummy example:% |
|
478 |
\end{isamarkuptext}% |
|
479 |
\isamarkuptrue% |
|
480 |
\isacommand{definition}\isamarkupfalse% |
|
481 |
\isanewline |
|
482 |
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}% |
|
483 |
\begin{isamarkuptext}% |
|
21186 | 484 |
Type classes offer a suitable occasion to introduce |
21172 | 485 |
the Haskell serializer. Its usage is almost the same |
486 |
as SML, but, in accordance with conventions |
|
487 |
some Haskell systems enforce, each module ends |
|
488 |
up in a single file. The module hierarchy is reflected in |
|
489 |
the file system, with root given by the user.% |
|
490 |
\end{isamarkuptext}% |
|
491 |
\isamarkuptrue% |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
492 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
493 |
\isadelimML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
494 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
495 |
\endisadelimML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
496 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
497 |
\isatagML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
498 |
\isacommand{ML}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
499 |
\ {\isacharverbatimopen}\ set\ Toplevel{\isachardot}debug\ {\isacharverbatimclose}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
500 |
\endisatagML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
501 |
{\isafoldML}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
502 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
503 |
\isadelimML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
504 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
505 |
\endisadelimML |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
506 |
\isanewline |
21172 | 507 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
508 |
\ dummy\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}% |
|
509 |
\begin{isamarkuptext}% |
|
510 |
\lsthaskell{Thy/examples/Codegen.hs} |
|
511 |
||
512 |
(we have left out all other modules). |
|
513 |
||
514 |
The whole code in SML with explicit dictionary passing:% |
|
515 |
\end{isamarkuptext}% |
|
516 |
\isamarkuptrue% |
|
517 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
518 |
\ dummy\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
519 |
\begin{isamarkuptext}% |
|
520 |
\lstsml{Thy/examples/class.ML}% |
|
521 |
\end{isamarkuptext}% |
|
20967 | 522 |
\isamarkuptrue% |
523 |
% |
|
524 |
\isamarkupsubsection{Incremental code generation% |
|
525 |
} |
|
526 |
\isamarkuptrue% |
|
527 |
% |
|
21172 | 528 |
\begin{isamarkuptext}% |
529 |
Code generation is \emph{incremental}: theorems |
|
530 |
and abstract intermediate code are cached and extended on demand. |
|
531 |
The cache may be partially or fully dropped if the underlying |
|
532 |
executable content of the theory changes. |
|
533 |
Implementation of caching is supposed to transparently |
|
534 |
hid away the details from the user. Anyway, caching |
|
535 |
reaches the surface by using a slightly more general form |
|
21348 | 536 |
of the \isa{{\isasymCODEGEN}}: either the list of constants or the |
21172 | 537 |
list of serialization expressions may be dropped. If no |
538 |
serialization expressions are given, only abstract code |
|
539 |
is generated and cached; if no constants are given, the |
|
540 |
current cache is serialized. |
|
541 |
||
21348 | 542 |
For explorative purpose, an extended version of the |
543 |
\isa{{\isasymCODEGEN}} command may prove useful:% |
|
21172 | 544 |
\end{isamarkuptext}% |
545 |
\isamarkuptrue% |
|
546 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse% |
|
547 |
\ {\isacharparenleft}{\isacharparenright}% |
|
548 |
\begin{isamarkuptext}% |
|
22060 | 549 |
\noindent print all cached defining equations (i.e.~\emph{after} |
21452 | 550 |
any applied transformation). Inside the brackets a |
21172 | 551 |
list of constants may be given; their function |
21186 | 552 |
equations are added to the cache if not already present.% |
21172 | 553 |
\end{isamarkuptext}% |
554 |
\isamarkuptrue% |
|
555 |
% |
|
556 |
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}% |
|
557 |
} |
|
558 |
\isamarkuptrue% |
|
559 |
% |
|
560 |
\begin{isamarkuptext}% |
|
561 |
In this tutorial, we do not attempt to give an exhaustive |
|
562 |
description of the code generator framework; instead, |
|
563 |
we cast a light on advanced topics by introducing |
|
564 |
them together with practically motivated examples. Concerning |
|
565 |
further reading, see |
|
566 |
||
567 |
\begin{itemize} |
|
568 |
||
569 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} |
|
570 |
for exhaustive syntax diagrams. |
|
21348 | 571 |
\item or \fixme[ref] which deals with foundational issues |
21172 | 572 |
of the code generator framework. |
573 |
||
574 |
\end{itemize}% |
|
575 |
\end{isamarkuptext}% |
|
576 |
\isamarkuptrue% |
|
577 |
% |
|
578 |
\isamarkupsubsection{Library theories% |
|
579 |
} |
|
580 |
\isamarkuptrue% |
|
581 |
% |
|
582 |
\begin{isamarkuptext}% |
|
583 |
The HOL \emph{Main} theory already provides a code generator setup |
|
584 |
which should be suitable for most applications. Common extensions |
|
585 |
and modifications are available by certain theories of the HOL |
|
586 |
library; beside being useful in applications, they may serve |
|
21186 | 587 |
as a tutorial for customizing the code generator setup. |
21172 | 588 |
|
589 |
\begin{description} |
|
590 |
||
21452 | 591 |
\item[\isa{ExecutableSet}] allows to generate code |
21172 | 592 |
for finite sets using lists. |
21452 | 593 |
\item[\isa{ExecutableRat}] \label{exec_rat} implements rational |
21172 | 594 |
numbers as triples \isa{{\isacharparenleft}sign{\isacharcomma}\ enumerator{\isacharcomma}\ denominator{\isacharparenright}}. |
21452 | 595 |
\item[\isa{EfficientNat}] \label{eff_nat} implements natural numbers by integers, |
21186 | 596 |
which in general will result in higher efficency; pattern |
21172 | 597 |
matching with \isa{{\isadigit{0}}} / \isa{Suc} |
21348 | 598 |
is eliminated. |
21452 | 599 |
\item[\isa{MLString}] provides an additional datatype \isa{mlstring}; |
21172 | 600 |
in the HOL default setup, strings in HOL are mapped to list |
601 |
of chars in SML; values of type \isa{mlstring} are |
|
602 |
mapped to strings in SML. |
|
603 |
||
604 |
\end{description}% |
|
605 |
\end{isamarkuptext}% |
|
606 |
\isamarkuptrue% |
|
607 |
% |
|
608 |
\isamarkupsubsection{Preprocessing% |
|
20967 | 609 |
} |
610 |
\isamarkuptrue% |
|
611 |
% |
|
21172 | 612 |
\begin{isamarkuptext}% |
613 |
Before selected function theorems are turned into abstract |
|
614 |
code, a chain of definitional transformation steps is carried |
|
21186 | 615 |
out: \emph{preprocessing}. There are three possibilities |
21172 | 616 |
to customize preprocessing: \emph{inline theorems}, |
617 |
\emph{inline procedures} and \emph{generic preprocessors}. |
|
618 |
||
619 |
\emph{Inline theorems} are rewriting rules applied to each |
|
22060 | 620 |
defining equation. Due to the interpretation of theorems |
621 |
of defining equations, rewrites are applied to the right |
|
21172 | 622 |
hand side and the arguments of the left hand side of an |
623 |
equation, but never to the constant heading the left hand side. |
|
624 |
Inline theorems may be declared an undeclared using the |
|
21186 | 625 |
\emph{code inline} or \emph{code noinline} attribute respectively. |
21172 | 626 |
|
627 |
Some common applications:% |
|
628 |
\end{isamarkuptext}% |
|
629 |
\isamarkuptrue% |
|
630 |
% |
|
631 |
\begin{itemize} |
|
632 |
\item replacing non-executable constructs by executable ones: \\ |
|
633 |
\isacommand{lemma}\isamarkupfalse% |
|
634 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
635 |
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
|
636 |
\isadelimproof |
|
637 |
\ % |
|
638 |
\endisadelimproof |
|
639 |
% |
|
640 |
\isatagproof |
|
641 |
\isacommand{by}\isamarkupfalse% |
|
642 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
643 |
\endisatagproof |
|
644 |
{\isafoldproof}% |
|
645 |
% |
|
646 |
\isadelimproof |
|
647 |
% |
|
648 |
\endisadelimproof |
|
649 |
% |
|
650 |
\item eliminating superfluous constants: \\ |
|
651 |
\isacommand{lemma}\isamarkupfalse% |
|
652 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
653 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}% |
|
654 |
\isadelimproof |
|
655 |
\ % |
|
656 |
\endisadelimproof |
|
657 |
% |
|
658 |
\isatagproof |
|
659 |
\isacommand{by}\isamarkupfalse% |
|
660 |
\ simp% |
|
661 |
\endisatagproof |
|
662 |
{\isafoldproof}% |
|
663 |
% |
|
664 |
\isadelimproof |
|
665 |
% |
|
666 |
\endisadelimproof |
|
667 |
% |
|
668 |
\item replacing executable but inconvenient constructs: \\ |
|
669 |
\isacommand{lemma}\isamarkupfalse% |
|
670 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
671 |
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}% |
|
672 |
\isadelimproof |
|
673 |
\ % |
|
674 |
\endisadelimproof |
|
675 |
% |
|
676 |
\isatagproof |
|
677 |
\isacommand{by}\isamarkupfalse% |
|
678 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
679 |
\endisatagproof |
|
680 |
{\isafoldproof}% |
|
681 |
% |
|
682 |
\isadelimproof |
|
683 |
% |
|
684 |
\endisadelimproof |
|
685 |
% |
|
686 |
\end{itemize} |
|
687 |
% |
|
688 |
\begin{isamarkuptext}% |
|
689 |
The current set of inline theorems may be inspected using |
|
21348 | 690 |
the \isa{{\isasymPRINTCODETHMS}} command. |
21172 | 691 |
|
692 |
\emph{Inline procedures} are a generalized version of inline |
|
693 |
theorems written in ML -- rewrite rules are generated dependent |
|
694 |
on the function theorems for a certain function. One |
|
695 |
application is the implicit expanding of \isa{nat} numerals |
|
696 |
to \isa{{\isadigit{0}}} / \isa{Suc} representation. See further |
|
697 |
\secref{sec:ml} |
|
698 |
||
699 |
\emph{Generic preprocessors} provide a most general interface, |
|
700 |
transforming a list of function theorems to another |
|
701 |
list of function theorems, provided that neither the heading |
|
702 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc} |
|
21348 | 703 |
pattern elimination implemented in |
21452 | 704 |
theory \isa{EfficientNat} (\secref{eff_nat}) uses this |
21172 | 705 |
interface. |
706 |
||
707 |
\begin{warn} |
|
708 |
The order in which single preprocessing steps are carried |
|
709 |
out currently is not specified; in particular, preprocessing |
|
21186 | 710 |
is \emph{no} fix point process. Keep this in mind when |
21172 | 711 |
setting up the preprocessor. |
712 |
||
713 |
Further, the attribute \emph{code unfold} |
|
714 |
associated with the existing code generator also applies to |
|
715 |
the new one: \emph{code unfold} implies \emph{code inline}. |
|
716 |
\end{warn}% |
|
717 |
\end{isamarkuptext}% |
|
718 |
\isamarkuptrue% |
|
719 |
% |
|
720 |
\isamarkupsubsection{Customizing serialization% |
|
721 |
} |
|
722 |
\isamarkuptrue% |
|
723 |
% |
|
724 |
\begin{isamarkuptext}% |
|
725 |
Consider the following function and its corresponding |
|
726 |
SML code:% |
|
727 |
\end{isamarkuptext}% |
|
728 |
\isamarkuptrue% |
|
729 |
\isacommand{fun}\isamarkupfalse% |
|
730 |
\isanewline |
|
731 |
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
21348 | 732 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}% |
733 |
\isadelimtt |
|
21172 | 734 |
% |
21348 | 735 |
\endisadelimtt |
736 |
% |
|
737 |
\isatagtt |
|
21172 | 738 |
% |
21348 | 739 |
\endisatagtt |
740 |
{\isafoldtt}% |
|
741 |
% |
|
742 |
\isadelimtt |
|
743 |
\isanewline |
|
21172 | 744 |
% |
21348 | 745 |
\endisadelimtt |
21172 | 746 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
21348 | 747 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 748 |
\begin{isamarkuptext}% |
21348 | 749 |
\lstsml{Thy/examples/bool_literal.ML} |
21172 | 750 |
|
751 |
Though this is correct code, it is a little bit unsatisfactory: |
|
752 |
boolean values and operators are materialized as distinguished |
|
753 |
entities with have nothing to do with the SML-builtin notion |
|
754 |
of \qt{bool}. This results in less readable code; |
|
755 |
additionally, eager evaluation may cause programs to |
|
756 |
loop or break which would perfectly terminate when |
|
757 |
the existing SML \qt{bool} would be used. To map |
|
758 |
the HOL \qt{bool} on SML \qt{bool}, we may use |
|
759 |
\qn{custom serializations}:% |
|
760 |
\end{isamarkuptext}% |
|
761 |
\isamarkuptrue% |
|
21348 | 762 |
% |
763 |
\isadelimtt |
|
764 |
% |
|
765 |
\endisadelimtt |
|
766 |
% |
|
767 |
\isatagtt |
|
21172 | 768 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
769 |
\ bool\isanewline |
|
770 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
771 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
772 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
773 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 774 |
\endisatagtt |
775 |
{\isafoldtt}% |
|
776 |
% |
|
777 |
\isadelimtt |
|
778 |
% |
|
779 |
\endisadelimtt |
|
780 |
% |
|
21172 | 781 |
\begin{isamarkuptext}% |
21348 | 782 |
The \isa{{\isasymCODETYPE}} commad takes a type constructor |
21172 | 783 |
as arguments together with a list of custom serializations. |
784 |
Each custom serialization starts with a target language |
|
785 |
identifier followed by an expression, which during |
|
786 |
code serialization is inserted whenever the type constructor |
|
21348 | 787 |
would occur. For constants, \isa{{\isasymCODECONST}} implements |
788 |
the corresponding mechanism. Each ``\verb|_|'' in |
|
21172 | 789 |
a serialization expression is treated as a placeholder |
790 |
for the type constructor's (the constant's) arguments.% |
|
791 |
\end{isamarkuptext}% |
|
792 |
\isamarkuptrue% |
|
793 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
794 |
\ SML\isanewline |
|
795 |
\ \ bool\ true\ false% |
|
796 |
\begin{isamarkuptext}% |
|
797 |
To assert that the existing \qt{bool}, \qt{true} and \qt{false} |
|
21348 | 798 |
is not used for generated code, we use \isa{{\isasymCODERESERVED}}. |
21172 | 799 |
|
800 |
After this setup, code looks quite more readable:% |
|
801 |
\end{isamarkuptext}% |
|
802 |
\isamarkuptrue% |
|
803 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
21348 | 804 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 805 |
\begin{isamarkuptext}% |
21348 | 806 |
\lstsml{Thy/examples/bool_mlbool.ML} |
21172 | 807 |
|
808 |
This still is not perfect: the parentheses |
|
21348 | 809 |
around the \qt{andalso} expression are superfluous. |
810 |
Though the serializer |
|
21172 | 811 |
by no means attempts to imitate the rich Isabelle syntax |
812 |
framework, it provides some common idioms, notably |
|
813 |
associative infixes with precedences which may be used here:% |
|
814 |
\end{isamarkuptext}% |
|
815 |
\isamarkuptrue% |
|
21348 | 816 |
% |
817 |
\isadelimtt |
|
818 |
% |
|
819 |
\endisadelimtt |
|
820 |
% |
|
821 |
\isatagtt |
|
21172 | 822 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
823 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
21348 | 824 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
825 |
\endisatagtt |
|
826 |
{\isafoldtt}% |
|
827 |
% |
|
828 |
\isadelimtt |
|
829 |
% |
|
830 |
\endisadelimtt |
|
831 |
\isanewline |
|
21172 | 832 |
\isanewline |
833 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
21348 | 834 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 835 |
\begin{isamarkuptext}% |
21348 | 836 |
\lstsml{Thy/examples/bool_infix.ML} |
21172 | 837 |
|
838 |
Next, we try to map HOL pairs to SML pairs, using the |
|
21348 | 839 |
infix ``\verb|*|'' type constructor and parentheses:% |
21172 | 840 |
\end{isamarkuptext}% |
841 |
\isamarkuptrue% |
|
842 |
\isanewline |
|
21348 | 843 |
% |
844 |
\isadelimtt |
|
845 |
% |
|
846 |
\endisadelimtt |
|
847 |
% |
|
848 |
\isatagtt |
|
21172 | 849 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
850 |
\ {\isacharasterisk}\isanewline |
|
851 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
852 |
\isanewline |
|
853 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
854 |
\ Pair\isanewline |
|
855 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 856 |
\endisatagtt |
857 |
{\isafoldtt}% |
|
858 |
% |
|
859 |
\isadelimtt |
|
860 |
% |
|
861 |
\endisadelimtt |
|
862 |
% |
|
21172 | 863 |
\begin{isamarkuptext}% |
21348 | 864 |
The initial bang ``\verb|!|'' tells the serializer to never put |
21172 | 865 |
parentheses around the whole expression (they are already present), |
866 |
while the parentheses around argument place holders |
|
867 |
tell not to put parentheses around the arguments. |
|
21348 | 868 |
The slash ``\verb|/|'' (followed by arbitrary white space) |
21172 | 869 |
inserts a space which may be used as a break if necessary |
870 |
during pretty printing. |
|
871 |
||
21186 | 872 |
So far, we did only provide more idiomatic serializations for |
873 |
constructs which would be executable on their own. Target-specific |
|
874 |
serializations may also be used to \emph{implement} constructs |
|
875 |
which have no implicit notion of executability. For example, |
|
876 |
take the HOL integers:% |
|
877 |
\end{isamarkuptext}% |
|
878 |
\isamarkuptrue% |
|
879 |
\isacommand{definition}\isamarkupfalse% |
|
880 |
\isanewline |
|
21993 | 881 |
\ \ double{\isacharunderscore}inc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21186 | 882 |
\ \ {\isachardoublequoteopen}double{\isacharunderscore}inc\ k\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ k\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline |
883 |
\isanewline |
|
884 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
885 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
886 |
\begin{isamarkuptext}% |
|
887 |
will fail: \isa{int} in HOL is implemented using a quotient |
|
888 |
type, which does not provide any notion of executability. |
|
889 |
\footnote{Eventually, we also want to provide executability |
|
890 |
for quotients.}. However, we could use the SML builtin |
|
891 |
integers:% |
|
21172 | 892 |
\end{isamarkuptext}% |
893 |
\isamarkuptrue% |
|
21348 | 894 |
% |
895 |
\isadelimtt |
|
896 |
% |
|
897 |
\endisadelimtt |
|
898 |
% |
|
899 |
\isatagtt |
|
21172 | 900 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
901 |
\ int\isanewline |
|
902 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}IntInf{\isachardot}int{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
903 |
\isanewline |
|
904 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
905 |
\ {\isachardoublequoteopen}op\ {\isacharplus}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline |
|
906 |
\ \ \ \ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isacharasterisk}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline |
|
21348 | 907 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}IntInf{\isachardot}{\isacharplus}\ {\isacharparenleft}{\isacharunderscore}{\isacharcomma}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}IntInf{\isachardot}{\isacharasterisk}\ {\isacharparenleft}{\isacharunderscore}{\isacharcomma}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
908 |
\endisatagtt |
|
909 |
{\isafoldtt}% |
|
910 |
% |
|
911 |
\isadelimtt |
|
912 |
% |
|
913 |
\endisadelimtt |
|
914 |
\isanewline |
|
21186 | 915 |
\isanewline |
916 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
917 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
918 |
\begin{isamarkuptext}% |
|
919 |
resulting in: |
|
920 |
||
921 |
\lstsml{Thy/examples/integers.ML}% |
|
922 |
\end{isamarkuptext}% |
|
923 |
\isamarkuptrue% |
|
924 |
% |
|
925 |
\begin{isamarkuptext}% |
|
926 |
These examples give a glimpse what powerful mechanisms |
|
927 |
custom serializations provide; however their usage |
|
928 |
requires careful thinking in order not to introduce |
|
929 |
inconsistencies -- or, in other words: |
|
930 |
custom serializations are completely axiomatic. |
|
931 |
||
932 |
A further noteworthy details is that any special |
|
933 |
character in a custom serialization may be quoted |
|
21348 | 934 |
using ``\verb|'|''; thus, in |
935 |
``\verb|fn '_ => _|'' the first |
|
936 |
``\verb|_|'' is a proper underscore while the |
|
937 |
second ``\verb|_|'' is a placeholder. |
|
21186 | 938 |
|
939 |
The HOL theories provide further |
|
940 |
examples for custom serializations and form |
|
941 |
a recommended tutorial on how to use them properly.% |
|
942 |
\end{isamarkuptext}% |
|
21172 | 943 |
\isamarkuptrue% |
944 |
% |
|
945 |
\isamarkupsubsection{Concerning operational equality% |
|
20967 | 946 |
} |
947 |
\isamarkuptrue% |
|
948 |
% |
|
21172 | 949 |
\begin{isamarkuptext}% |
950 |
Surely you have already noticed how equality is treated |
|
951 |
by the code generator:% |
|
952 |
\end{isamarkuptext}% |
|
953 |
\isamarkuptrue% |
|
954 |
\isacommand{fun}\isamarkupfalse% |
|
955 |
\isanewline |
|
956 |
\ \ 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 |
|
957 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline |
|
958 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline |
|
959 |
\ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline |
|
960 |
\ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline |
|
961 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline |
|
21348 | 962 |
\ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}% |
21172 | 963 |
\begin{isamarkuptext}% |
21348 | 964 |
The membership test during preprocessing is rewritten, |
21172 | 965 |
resulting in \isa{op\ mem}, which itself |
966 |
performs an explicit equality check.% |
|
967 |
\end{isamarkuptext}% |
|
968 |
\isamarkuptrue% |
|
969 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
970 |
\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
971 |
\begin{isamarkuptext}% |
|
972 |
\lstsml{Thy/examples/collect_duplicates.ML}% |
|
973 |
\end{isamarkuptext}% |
|
974 |
\isamarkuptrue% |
|
975 |
% |
|
976 |
\begin{isamarkuptext}% |
|
977 |
Obviously, polymorphic equality is implemented the Haskell |
|
978 |
way using a type class. How is this achieved? By an |
|
979 |
almost trivial definition in the HOL setup:% |
|
980 |
\end{isamarkuptext}% |
|
981 |
\isamarkuptrue% |
|
982 |
% |
|
983 |
\isadelimML |
|
984 |
% |
|
985 |
\endisadelimML |
|
986 |
% |
|
987 |
\isatagML |
|
988 |
% |
|
989 |
\endisatagML |
|
990 |
{\isafoldML}% |
|
991 |
% |
|
992 |
\isadelimML |
|
993 |
% |
|
994 |
\endisadelimML |
|
995 |
\isanewline |
|
21452 | 996 |
\isacommand{axclass}\isamarkupfalse% |
997 |
\ eq\ {\isasymsubseteq}\ type\isanewline |
|
21993 | 998 |
\ \ {\isacharparenleft}\isakeyword{attach}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 999 |
\begin{isamarkuptext}% |
1000 |
This merely introduces a class \isa{eq} with corresponding |
|
21994 | 1001 |
operation \isa{op\ {\isacharequal}}; |
21452 | 1002 |
the preprocessing framework does the rest.% |
21172 | 1003 |
\end{isamarkuptext}% |
1004 |
\isamarkuptrue% |
|
1005 |
% |
|
1006 |
\isadelimML |
|
1007 |
% |
|
1008 |
\endisadelimML |
|
1009 |
% |
|
1010 |
\isatagML |
|
1011 |
% |
|
1012 |
\endisatagML |
|
1013 |
{\isafoldML}% |
|
1014 |
% |
|
1015 |
\isadelimML |
|
1016 |
% |
|
1017 |
\endisadelimML |
|
1018 |
% |
|
1019 |
\begin{isamarkuptext}% |
|
1020 |
For datatypes, instances of \isa{eq} are implicitly derived |
|
1021 |
when possible. |
|
1022 |
||
1023 |
Though this class is designed to get rarely in the way, there |
|
1024 |
are some cases when it suddenly comes to surface:% |
|
1025 |
\end{isamarkuptext}% |
|
1026 |
\isamarkuptrue% |
|
1027 |
% |
|
21348 | 1028 |
\isamarkupsubsubsection{typedecls interpreted by customary serializations% |
21186 | 1029 |
} |
1030 |
\isamarkuptrue% |
|
1031 |
% |
|
1032 |
\begin{isamarkuptext}% |
|
1033 |
A common idiom is to use unspecified types for formalizations |
|
1034 |
and interpret them for a specific target language:% |
|
1035 |
\end{isamarkuptext}% |
|
1036 |
\isamarkuptrue% |
|
21172 | 1037 |
\isacommand{typedecl}\isamarkupfalse% |
1038 |
\ key\isanewline |
|
1039 |
\isanewline |
|
1040 |
\isacommand{fun}\isamarkupfalse% |
|
1041 |
\isanewline |
|
1042 |
\ \ lookup\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}key\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
1043 |
\ \ {\isachardoublequoteopen}lookup\ {\isacharbrackleft}{\isacharbrackright}\ l\ {\isacharequal}\ None{\isachardoublequoteclose}\isanewline |
|
1044 |
\ \ {\isachardoublequoteopen}lookup\ {\isacharparenleft}{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharhash}\ xs{\isacharparenright}\ l\ {\isacharequal}\ {\isacharparenleft}if\ k\ {\isacharequal}\ l\ then\ Some\ v\ else\ lookup\ xs\ l{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
1045 |
% |
|
21348 | 1046 |
\isadelimtt |
21172 | 1047 |
% |
21348 | 1048 |
\endisadelimtt |
21172 | 1049 |
% |
21348 | 1050 |
\isatagtt |
21172 | 1051 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1052 |
\ key\isanewline |
|
1053 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}string{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1054 |
\endisatagtt |
1055 |
{\isafoldtt}% |
|
1056 |
% |
|
1057 |
\isadelimtt |
|
1058 |
% |
|
1059 |
\endisadelimtt |
|
1060 |
% |
|
21186 | 1061 |
\begin{isamarkuptext}% |
1062 |
This, though, is not sufficient: \isa{key} is no instance |
|
21172 | 1063 |
of \isa{eq} since \isa{key} is no datatype; the instance |
1064 |
has to be declared manually, including a serialization |
|
21452 | 1065 |
for the particular instance of \isa{op\ {\isacharequal}}:% |
21186 | 1066 |
\end{isamarkuptext}% |
1067 |
\isamarkuptrue% |
|
21172 | 1068 |
\isacommand{instance}\isamarkupfalse% |
1069 |
\ key\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1070 |
\isadelimproof |
|
1071 |
\ % |
|
1072 |
\endisadelimproof |
|
1073 |
% |
|
1074 |
\isatagproof |
|
1075 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1076 |
% |
|
1077 |
\endisatagproof |
|
1078 |
{\isafoldproof}% |
|
1079 |
% |
|
1080 |
\isadelimproof |
|
1081 |
% |
|
1082 |
\endisadelimproof |
|
1083 |
\isanewline |
|
21348 | 1084 |
% |
1085 |
\isadelimtt |
|
21172 | 1086 |
\isanewline |
21348 | 1087 |
% |
1088 |
\endisadelimtt |
|
1089 |
% |
|
1090 |
\isatagtt |
|
21172 | 1091 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
21452 | 1092 |
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isasymColon}\ key\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline |
1093 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}\ {\isacharcolon}\ string{\isacharparenright}\ {\isacharequal}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1094 |
\endisatagtt |
1095 |
{\isafoldtt}% |
|
1096 |
% |
|
1097 |
\isadelimtt |
|
1098 |
% |
|
1099 |
\endisadelimtt |
|
1100 |
% |
|
21186 | 1101 |
\begin{isamarkuptext}% |
1102 |
Then everything goes fine:% |
|
1103 |
\end{isamarkuptext}% |
|
1104 |
\isamarkuptrue% |
|
21172 | 1105 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
1106 |
\ lookup\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lookup{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1107 |
\begin{isamarkuptext}% |
|
1108 |
\lstsml{Thy/examples/lookup.ML}% |
|
1109 |
\end{isamarkuptext}% |
|
1110 |
\isamarkuptrue% |
|
1111 |
% |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1112 |
\isamarkupsubsubsection{lexicographic orderings% |
21186 | 1113 |
} |
1114 |
\isamarkuptrue% |
|
1115 |
% |
|
1116 |
\begin{isamarkuptext}% |
|
1117 |
Another subtlety |
|
21172 | 1118 |
enters the stage when definitions of overloaded constants |
1119 |
are dependent on operational equality. For example, let |
|
21348 | 1120 |
us define a lexicographic ordering on tuples:% |
21186 | 1121 |
\end{isamarkuptext}% |
1122 |
\isamarkuptrue% |
|
1123 |
\isacommand{instance}\isamarkupfalse% |
|
1124 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline |
|
1125 |
\ \ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isacharless}\ p{\isadigit{2}}\ {\isasymequiv}\ let\ {\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline |
|
1126 |
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1127 |
\ \ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isasymle}\ p{\isadigit{2}}\ {\isasymequiv}\ let\ {\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1128 |
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}% |
21172 | 1129 |
\isadelimproof |
1130 |
\ % |
|
1131 |
\endisadelimproof |
|
1132 |
% |
|
1133 |
\isatagproof |
|
1134 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1135 |
% |
|
1136 |
\endisatagproof |
|
1137 |
{\isafoldproof}% |
|
1138 |
% |
|
1139 |
\isadelimproof |
|
1140 |
% |
|
1141 |
\endisadelimproof |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1142 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1143 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1144 |
\isacommand{lemma}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1145 |
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1146 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1147 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1148 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1149 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1150 |
\ \ % |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1151 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1152 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1153 |
\isatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1154 |
\isacommand{unfolding}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1155 |
\ {\isachardoublequoteopen}less{\isacharunderscore}eq{\isacharunderscore}{\isacharasterisk}{\isacharunderscore}def{\isachardoublequoteclose}\ {\isachardoublequoteopen}less{\isacharunderscore}{\isacharasterisk}{\isacharunderscore}def{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1156 |
\ simp{\isacharunderscore}all% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1157 |
\endisatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1158 |
{\isafoldproof}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1159 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1160 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1161 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1162 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1163 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1164 |
\begin{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1165 |
Then code generation will fail. Why? The definition |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1166 |
of \isa{op\ {\isasymle}} depends on equality on both arguments, |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1167 |
which are polymorphic and impose an additional \isa{eq} |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1168 |
class constraint, thus violating the type discipline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1169 |
for class operations. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1170 |
|
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1171 |
The solution is to add \isa{eq} explicitly to the first sort arguments in the |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1172 |
code theorems:% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1173 |
\end{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1174 |
\isamarkuptrue% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1175 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1176 |
\isacommand{lemma}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1177 |
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1178 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}ord{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1179 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}ord{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1180 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1181 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1182 |
\ \ % |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1183 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1184 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1185 |
\isatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1186 |
\isacommand{unfolding}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1187 |
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1188 |
\ rule{\isacharplus}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1189 |
\endisatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1190 |
{\isafoldproof}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1191 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1192 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1193 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1194 |
\endisadelimproof |
21172 | 1195 |
% |
21186 | 1196 |
\begin{isamarkuptext}% |
1197 |
Then code generation succeeds:% |
|
1198 |
\end{isamarkuptext}% |
|
1199 |
\isamarkuptrue% |
|
21172 | 1200 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1201 |
\ {\isachardoublequoteopen}op\ {\isasymle}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}eq{\isacharcomma}\ ord{\isacharbraceright}\ {\isasymtimes}\ {\isacharprime}b{\isasymColon}ord\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline |
21172 | 1202 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
1203 |
\begin{isamarkuptext}% |
|
1204 |
\lstsml{Thy/examples/lexicographic.ML}% |
|
1205 |
\end{isamarkuptext}% |
|
1206 |
\isamarkuptrue% |
|
1207 |
% |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1208 |
\begin{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1209 |
In general, code theorems for overloaded constants may have more |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1210 |
restrictive sort constraints than the underlying instance relation |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1211 |
between class and type constructor as long as the whole system of |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1212 |
constraints is coregular; code theorems violating coregularity |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1213 |
are rejected immediately.% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1214 |
\end{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1215 |
\isamarkuptrue% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1216 |
% |
21186 | 1217 |
\isamarkupsubsubsection{Haskell serialization% |
1218 |
} |
|
1219 |
\isamarkuptrue% |
|
1220 |
% |
|
1221 |
\begin{isamarkuptext}% |
|
1222 |
For convenience, the default |
|
1223 |
HOL setup for Haskell maps the \isa{eq} class to |
|
1224 |
its counterpart in Haskell, giving custom serializations |
|
21348 | 1225 |
for the class (\isa{{\isasymCODECLASS}}) and its operation:% |
21186 | 1226 |
\end{isamarkuptext}% |
1227 |
\isamarkuptrue% |
|
1228 |
% |
|
1229 |
\isadelimML |
|
1230 |
% |
|
1231 |
\endisadelimML |
|
1232 |
% |
|
1233 |
\isatagML |
|
1234 |
% |
|
1235 |
\endisatagML |
|
1236 |
{\isafoldML}% |
|
1237 |
% |
|
1238 |
\isadelimML |
|
1239 |
% |
|
1240 |
\endisadelimML |
|
1241 |
\isanewline |
|
21348 | 1242 |
% |
1243 |
\isadelimtt |
|
1244 |
% |
|
1245 |
\endisadelimtt |
|
1246 |
% |
|
1247 |
\isatagtt |
|
21186 | 1248 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
1249 |
\ eq\isanewline |
|
1250 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ eq\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1251 |
\isanewline |
|
1252 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1253 |
\ eq\isanewline |
|
1254 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1255 |
% |
|
21348 | 1256 |
\endisatagtt |
1257 |
{\isafoldtt}% |
|
1258 |
% |
|
1259 |
\isadelimtt |
|
1260 |
% |
|
1261 |
\endisadelimtt |
|
1262 |
% |
|
21186 | 1263 |
\isadelimML |
1264 |
% |
|
1265 |
\endisadelimML |
|
1266 |
% |
|
1267 |
\isatagML |
|
1268 |
% |
|
1269 |
\endisatagML |
|
1270 |
{\isafoldML}% |
|
1271 |
% |
|
1272 |
\isadelimML |
|
1273 |
% |
|
1274 |
\endisadelimML |
|
21172 | 1275 |
% |
21186 | 1276 |
\begin{isamarkuptext}% |
1277 |
A problem now occurs whenever a type which |
|
1278 |
is an instance of \isa{eq} in HOL is mapped |
|
1279 |
on a Haskell-builtin type which is also an instance |
|
1280 |
of Haskell \isa{Eq}:% |
|
1281 |
\end{isamarkuptext}% |
|
1282 |
\isamarkuptrue% |
|
1283 |
\isacommand{typedecl}\isamarkupfalse% |
|
1284 |
\ bar\isanewline |
|
1285 |
\isanewline |
|
1286 |
\isacommand{instance}\isamarkupfalse% |
|
1287 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1288 |
\isadelimproof |
|
1289 |
\ % |
|
1290 |
\endisadelimproof |
|
1291 |
% |
|
1292 |
\isatagproof |
|
1293 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1294 |
% |
|
1295 |
\endisatagproof |
|
1296 |
{\isafoldproof}% |
|
1297 |
% |
|
1298 |
\isadelimproof |
|
21172 | 1299 |
% |
21186 | 1300 |
\endisadelimproof |
1301 |
\isanewline |
|
21348 | 1302 |
% |
1303 |
\isadelimtt |
|
21186 | 1304 |
\isanewline |
21348 | 1305 |
% |
1306 |
\endisadelimtt |
|
1307 |
% |
|
1308 |
\isatagtt |
|
21186 | 1309 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1310 |
\ bar\isanewline |
|
1311 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1312 |
\endisatagtt |
1313 |
{\isafoldtt}% |
|
1314 |
% |
|
1315 |
\isadelimtt |
|
1316 |
% |
|
1317 |
\endisadelimtt |
|
1318 |
% |
|
21186 | 1319 |
\begin{isamarkuptext}% |
1320 |
The code generator would produce |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1321 |
an additional instance, which of course is rejected. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1322 |
To suppress this additional instance, use |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1323 |
\isa{{\isasymCODEINSTANCE}}:% |
21186 | 1324 |
\end{isamarkuptext}% |
1325 |
\isamarkuptrue% |
|
21348 | 1326 |
% |
1327 |
\isadelimtt |
|
1328 |
% |
|
1329 |
\endisadelimtt |
|
1330 |
% |
|
1331 |
\isatagtt |
|
21186 | 1332 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
1333 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
1334 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
|
21348 | 1335 |
\endisatagtt |
1336 |
{\isafoldtt}% |
|
1337 |
% |
|
1338 |
\isadelimtt |
|
1339 |
% |
|
1340 |
\endisadelimtt |
|
1341 |
% |
|
21186 | 1342 |
\isamarkupsubsection{Types matter% |
1343 |
} |
|
1344 |
\isamarkuptrue% |
|
1345 |
% |
|
1346 |
\begin{isamarkuptext}% |
|
1347 |
Imagine the following quick-and-dirty setup for implementing |
|
21348 | 1348 |
some kind of sets as lists in SML:% |
21186 | 1349 |
\end{isamarkuptext}% |
1350 |
\isamarkuptrue% |
|
21348 | 1351 |
% |
1352 |
\isadelimtt |
|
1353 |
% |
|
1354 |
\endisadelimtt |
|
1355 |
% |
|
1356 |
\isatagtt |
|
21186 | 1357 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1358 |
\ set\isanewline |
|
1359 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharunderscore}\ list{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1360 |
\isanewline |
|
1361 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1362 |
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}\ \isakeyword{and}\ insert\isanewline |
|
21348 | 1363 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ \isakeyword{and}\ \isakeyword{infixl}\ {\isadigit{7}}\ {\isachardoublequoteopen}{\isacharcolon}{\isacharcolon}{\isachardoublequoteclose}{\isacharparenright}% |
1364 |
\endisatagtt |
|
1365 |
{\isafoldtt}% |
|
1366 |
% |
|
1367 |
\isadelimtt |
|
1368 |
% |
|
1369 |
\endisadelimtt |
|
1370 |
\isanewline |
|
21186 | 1371 |
\isanewline |
1372 |
\isacommand{definition}\isamarkupfalse% |
|
1373 |
\isanewline |
|
21993 | 1374 |
\ \ dummy{\isacharunderscore}set\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ set{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1375 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}set\ {\isacharequal}\ {\isacharbraceleft}Suc{\isacharbraceright}{\isachardoublequoteclose}% |
1376 |
\begin{isamarkuptext}% |
|
1377 |
Then code generation for \isa{dummy{\isacharunderscore}set} will fail. |
|
22060 | 1378 |
Why? A glimpse at the defining equations will offer:% |
21348 | 1379 |
\end{isamarkuptext}% |
1380 |
\isamarkuptrue% |
|
1381 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse% |
|
1382 |
\ {\isacharparenleft}insert{\isacharparenright}% |
|
1383 |
\begin{isamarkuptext}% |
|
22060 | 1384 |
This reveals the defining equation \isa{insert\ {\isacharquery}a\ {\isacharquery}B\ {\isasymequiv}\ {\isacharbraceleft}x{\isachardot}\ x\ {\isacharequal}\ {\isacharquery}a{\isacharbraceright}\ {\isasymunion}\ {\isacharquery}B} |
21348 | 1385 |
for \isa{insert}, which is operationally meaningless |
1386 |
but forces an equality constraint on the set members |
|
1387 |
(which is not satisfiable if the set members are functions). |
|
1388 |
Even when using set of natural numbers (which are an instance |
|
1389 |
of \emph{eq}), we run into a problem:% |
|
1390 |
\end{isamarkuptext}% |
|
1391 |
\isamarkuptrue% |
|
1392 |
\isacommand{definition}\isamarkupfalse% |
|
1393 |
\isanewline |
|
21993 | 1394 |
\ \ foobar{\isacharunderscore}set\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ set{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1395 |
\ \ {\isachardoublequoteopen}foobar{\isacharunderscore}set\ {\isacharequal}\ {\isacharbraceleft}{\isadigit{0}}{\isacharcomma}\ {\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharbraceright}{\isachardoublequoteclose}% |
1396 |
\begin{isamarkuptext}% |
|
1397 |
In this case the serializer would complain that \isa{insert} |
|
1398 |
expects dictionaries (namely an \emph{eq} dictionary) but |
|
1399 |
has also been given a customary serialization. |
|
1400 |
||
1401 |
The solution to this dilemma:% |
|
1402 |
\end{isamarkuptext}% |
|
1403 |
\isamarkuptrue% |
|
1404 |
\isacommand{lemma}\isamarkupfalse% |
|
1405 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
1406 |
\ \ {\isachardoublequoteopen}insert\ {\isacharequal}\ insert{\isachardoublequoteclose}% |
|
1407 |
\isadelimproof |
|
1408 |
\ % |
|
1409 |
\endisadelimproof |
|
1410 |
% |
|
1411 |
\isatagproof |
|
1412 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1413 |
% |
|
1414 |
\endisatagproof |
|
1415 |
{\isafoldproof}% |
|
1416 |
% |
|
1417 |
\isadelimproof |
|
1418 |
% |
|
1419 |
\endisadelimproof |
|
1420 |
\isanewline |
|
1421 |
\isanewline |
|
1422 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
1423 |
\ dummy{\isacharunderscore}set\ foobar{\isacharunderscore}set\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}dirty{\isacharunderscore}set{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1424 |
\begin{isamarkuptext}% |
|
1425 |
\lstsml{Thy/examples/dirty_set.ML} |
|
1426 |
||
22060 | 1427 |
Reflexive defining equations by convention are dropped. |
21348 | 1428 |
But their presence prevents primitive definitions to be |
22060 | 1429 |
used as defining equations:% |
21348 | 1430 |
\end{isamarkuptext}% |
1431 |
\isamarkuptrue% |
|
1432 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse% |
|
1433 |
\ {\isacharparenleft}insert{\isacharparenright}% |
|
1434 |
\begin{isamarkuptext}% |
|
22060 | 1435 |
will show \emph{no} defining equations for insert. |
21348 | 1436 |
|
1437 |
Note that the sort constraints of reflexive equations |
|
1438 |
are considered; so% |
|
1439 |
\end{isamarkuptext}% |
|
1440 |
\isamarkuptrue% |
|
1441 |
\isacommand{lemma}\isamarkupfalse% |
|
1442 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
1443 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}insert\ {\isasymColon}\ {\isacharprime}a{\isasymColon}eq\ {\isasymRightarrow}\ {\isacharprime}a\ set\ {\isasymRightarrow}\ {\isacharprime}a\ set{\isacharparenright}\ {\isacharequal}\ insert{\isachardoublequoteclose}% |
|
1444 |
\isadelimproof |
|
1445 |
\ % |
|
1446 |
\endisadelimproof |
|
1447 |
% |
|
1448 |
\isatagproof |
|
1449 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1450 |
% |
|
1451 |
\endisatagproof |
|
1452 |
{\isafoldproof}% |
|
1453 |
% |
|
1454 |
\isadelimproof |
|
1455 |
% |
|
1456 |
\endisadelimproof |
|
1457 |
% |
|
1458 |
\begin{isamarkuptext}% |
|
1459 |
would mean nothing else than to introduce the evil |
|
1460 |
sort constraint by hand.% |
|
1461 |
\end{isamarkuptext}% |
|
1462 |
\isamarkuptrue% |
|
1463 |
% |
|
1464 |
\isamarkupsubsection{Cyclic module dependencies% |
|
1465 |
} |
|
1466 |
\isamarkuptrue% |
|
1467 |
% |
|
1468 |
\begin{isamarkuptext}% |
|
1469 |
Sometimes the awkward situation occurs that dependencies |
|
1470 |
between definitions introduce cyclic dependencies |
|
1471 |
between modules, which in the Haskell world leaves |
|
1472 |
you to the mercy of the Haskell implementation you are using, |
|
1473 |
while for SML code generation is not possible. |
|
1474 |
||
1475 |
A solution is to declare module names explicitly. |
|
1476 |
Let use assume the three cyclically dependent |
|
1477 |
modules are named \emph{A}, \emph{B} and \emph{C}. |
|
1478 |
Then, by stating% |
|
1479 |
\end{isamarkuptext}% |
|
1480 |
\isamarkuptrue% |
|
1481 |
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse% |
|
1482 |
\ SML\isanewline |
|
1483 |
\ \ A\ ABC\isanewline |
|
1484 |
\ \ B\ ABC\isanewline |
|
1485 |
\ \ C\ ABC% |
|
1486 |
\begin{isamarkuptext}% |
|
1487 |
we explicitly map all those modules on \emph{ABC}, |
|
1488 |
resulting in an ad-hoc merge of this three modules |
|
1489 |
at serialization time.% |
|
1490 |
\end{isamarkuptext}% |
|
1491 |
\isamarkuptrue% |
|
1492 |
% |
|
21172 | 1493 |
\isamarkupsubsection{Axiomatic extensions% |
20967 | 1494 |
} |
1495 |
\isamarkuptrue% |
|
1496 |
% |
|
21172 | 1497 |
\begin{isamarkuptext}% |
1498 |
\begin{warn} |
|
1499 |
The extensions introduced in this section, though working |
|
21348 | 1500 |
in practice, are not the cream of the crop, as you |
1501 |
will notice during reading. They will |
|
21172 | 1502 |
eventually be replaced by more mature approaches. |
21348 | 1503 |
\end{warn} |
1504 |
||
1505 |
Sometimes equalities are taken for granted which are |
|
1506 |
not derivable inside the HOL logic but are silently assumed |
|
1507 |
to hold for executable code. For example, we may want |
|
1508 |
to identify the famous HOL constant \isa{arbitrary} |
|
1509 |
of type \isa{{\isacharprime}a\ option} with \isa{None}. |
|
1510 |
By brute force:% |
|
1511 |
\end{isamarkuptext}% |
|
1512 |
\isamarkuptrue% |
|
1513 |
\isacommand{axiomatization}\isamarkupfalse% |
|
1514 |
\ \isakeyword{where}\isanewline |
|
1515 |
\ \ {\isachardoublequoteopen}arbitrary\ {\isacharequal}\ None{\isachardoublequoteclose}% |
|
1516 |
\begin{isamarkuptext}% |
|
1517 |
However this has to be considered harmful since this axiom, |
|
1518 |
though probably justifiable for generated code, could |
|
1519 |
introduce serious inconsistencies into the logic. |
|
1520 |
||
1521 |
So, there is a distinguished construct for stating axiomatic |
|
1522 |
equalities of constants which apply only for code generation. |
|
1523 |
Before introducing this, here is a convenient place to describe |
|
1524 |
shortly how to deal with some restrictions the type discipline |
|
1525 |
imposes. |
|
1526 |
||
1527 |
By itself, the constant \isa{arbitrary} is a non-overloaded |
|
1528 |
polymorphic constant. So, there is no way to distinguish |
|
1529 |
different versions of \isa{arbitrary} for different types |
|
1530 |
inside the code generator framework. However, inlining |
|
1531 |
theorems together with auxiliary constants provide a solution:% |
|
1532 |
\end{isamarkuptext}% |
|
1533 |
\isamarkuptrue% |
|
1534 |
\isacommand{definition}\isamarkupfalse% |
|
1535 |
\isanewline |
|
21993 | 1536 |
\ \ arbitrary{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1537 |
\ \ {\isacharbrackleft}symmetric{\isacharcomma}\ code\ inline{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}arbitrary{\isacharunderscore}option\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}% |
1538 |
\begin{isamarkuptext}% |
|
1539 |
By that, we replace any \isa{arbitrary} with option type |
|
22060 | 1540 |
by \isa{arbitrary{\isacharunderscore}option} in defining equations. |
21348 | 1541 |
|
1542 |
For technical reasons, we further have to provide a |
|
1543 |
synonym for \isa{None} which in code generator view |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1544 |
is a function rather than a term constructor:% |
21348 | 1545 |
\end{isamarkuptext}% |
1546 |
\isamarkuptrue% |
|
1547 |
\isacommand{definition}\isamarkupfalse% |
|
1548 |
\isanewline |
|
1549 |
\ \ {\isachardoublequoteopen}None{\isacharprime}\ {\isacharequal}\ None{\isachardoublequoteclose}% |
|
1550 |
\begin{isamarkuptext}% |
|
1551 |
Then finally we are enabled to use \isa{{\isasymCODEAXIOMS}}:% |
|
1552 |
\end{isamarkuptext}% |
|
1553 |
\isamarkuptrue% |
|
1554 |
\isacommand{code{\isacharunderscore}axioms}\isamarkupfalse% |
|
1555 |
\isanewline |
|
1556 |
\ \ arbitrary{\isacharunderscore}option\ {\isasymequiv}\ None{\isacharprime}% |
|
1557 |
\begin{isamarkuptext}% |
|
1558 |
A dummy example:% |
|
1559 |
\end{isamarkuptext}% |
|
1560 |
\isamarkuptrue% |
|
1561 |
\isacommand{fun}\isamarkupfalse% |
|
1562 |
\isanewline |
|
1563 |
\ \ dummy{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
1564 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ Some\ x{\isachardoublequoteclose}\isanewline |
|
1565 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}\isanewline |
|
1566 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
1567 |
\ dummy{\isacharunderscore}option\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}arbitrary{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1568 |
\begin{isamarkuptext}% |
|
1569 |
\lstsml{Thy/examples/arbitrary.ML} |
|
1570 |
||
1571 |
Another axiomatic extension is code generation |
|
1572 |
for abstracted types. For this, the |
|
21452 | 1573 |
\isa{ExecutableRat} (see \secref{exec_rat}) |
21348 | 1574 |
forms a good example.% |
21172 | 1575 |
\end{isamarkuptext}% |
1576 |
\isamarkuptrue% |
|
1577 |
% |
|
1578 |
\isamarkupsection{ML interfaces \label{sec:ml}% |
|
1579 |
} |
|
1580 |
\isamarkuptrue% |
|
1581 |
% |
|
21348 | 1582 |
\begin{isamarkuptext}% |
1583 |
Since the code generator framework not only aims to provide |
|
1584 |
a nice Isar interface but also to form a base for |
|
1585 |
code-generation-based applications, here a short |
|
1586 |
description of the most important ML interfaces.% |
|
1587 |
\end{isamarkuptext}% |
|
1588 |
\isamarkuptrue% |
|
1589 |
% |
|
21172 | 1590 |
\isamarkupsubsection{Constants with type discipline: codegen\_consts.ML% |
1591 |
} |
|
1592 |
\isamarkuptrue% |
|
1593 |
% |
|
21348 | 1594 |
\begin{isamarkuptext}% |
1595 |
This Pure module manages identification of (probably overloaded) |
|
1596 |
constants by unique identifiers.% |
|
1597 |
\end{isamarkuptext}% |
|
1598 |
\isamarkuptrue% |
|
1599 |
% |
|
21172 | 1600 |
\isadelimmlref |
1601 |
% |
|
1602 |
\endisadelimmlref |
|
1603 |
% |
|
1604 |
\isatagmlref |
|
1605 |
% |
|
1606 |
\begin{isamarkuptext}% |
|
1607 |
\begin{mldecls} |
|
21348 | 1608 |
\indexmltype{CodegenConsts.const}\verb|type CodegenConsts.const = string * typ list| \\ |
1609 |
\indexml{CodegenConsts.norm-of-typ}\verb|CodegenConsts.norm_of_typ: theory -> string * typ -> CodegenConsts.const| \\ |
|
21172 | 1610 |
\indexml{CodegenConsts.typ-of-inst}\verb|CodegenConsts.typ_of_inst: theory -> CodegenConsts.const -> string * typ| \\ |
21348 | 1611 |
\end{mldecls} |
1612 |
||
1613 |
\begin{description} |
|
1614 |
||
1615 |
\item \verb|CodegenConsts.const| is the identifier type: |
|
1616 |
the product of a \emph{string} with a list of \emph{typs}. |
|
1617 |
The \emph{string} is the constant name as represented inside Isabelle; |
|
1618 |
the \emph{typs} are a type instantiation in the sense of System F, |
|
1619 |
with canonical names for type variables. |
|
1620 |
||
1621 |
\item \verb|CodegenConsts.norm_of_typ|~\isa{thy}~\isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} |
|
1622 |
maps a constant expression \isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} to its canonical identifier. |
|
1623 |
||
1624 |
\item \verb|CodegenConsts.typ_of_inst|~\isa{thy}~\isa{const} |
|
1625 |
maps a canonical identifier \isa{const} to a constant |
|
1626 |
expression with appropriate type. |
|
1627 |
||
1628 |
\end{description}% |
|
21172 | 1629 |
\end{isamarkuptext}% |
1630 |
\isamarkuptrue% |
|
1631 |
% |
|
1632 |
\endisatagmlref |
|
1633 |
{\isafoldmlref}% |
|
1634 |
% |
|
1635 |
\isadelimmlref |
|
1636 |
% |
|
1637 |
\endisadelimmlref |
|
1638 |
% |
|
1639 |
\isamarkupsubsection{Executable theory content: codegen\_data.ML% |
|
1640 |
} |
|
1641 |
\isamarkuptrue% |
|
1642 |
% |
|
1643 |
\begin{isamarkuptext}% |
|
1644 |
This Pure module implements the core notions of |
|
1645 |
executable content of a theory.% |
|
1646 |
\end{isamarkuptext}% |
|
1647 |
\isamarkuptrue% |
|
1648 |
% |
|
1649 |
\isamarkupsubsubsection{Suspended theorems% |
|
1650 |
} |
|
1651 |
\isamarkuptrue% |
|
1652 |
% |
|
1653 |
\isadelimmlref |
|
1654 |
% |
|
1655 |
\endisadelimmlref |
|
1656 |
% |
|
1657 |
\isatagmlref |
|
1658 |
% |
|
1659 |
\begin{isamarkuptext}% |
|
1660 |
\begin{mldecls} |
|
21348 | 1661 |
\indexml{CodegenData.lazy}\verb|CodegenData.lazy: (unit -> thm list) -> thm list Susp.T| |
1662 |
\end{mldecls} |
|
1663 |
||
1664 |
\begin{description} |
|
1665 |
||
1666 |
\item \verb|CodegenData.lazy|~\isa{f} turns an abstract |
|
1667 |
theorem computation \isa{f} into a suspension of theorems. |
|
1668 |
||
1669 |
\end{description}% |
|
21172 | 1670 |
\end{isamarkuptext}% |
1671 |
\isamarkuptrue% |
|
1672 |
% |
|
1673 |
\endisatagmlref |
|
1674 |
{\isafoldmlref}% |
|
1675 |
% |
|
1676 |
\isadelimmlref |
|
1677 |
% |
|
1678 |
\endisadelimmlref |
|
1679 |
% |
|
1680 |
\isamarkupsubsubsection{Executable content% |
|
20967 | 1681 |
} |
1682 |
\isamarkuptrue% |
|
1683 |
% |
|
21172 | 1684 |
\isadelimmlref |
1685 |
% |
|
1686 |
\endisadelimmlref |
|
1687 |
% |
|
1688 |
\isatagmlref |
|
1689 |
% |
|
1690 |
\begin{isamarkuptext}% |
|
1691 |
\begin{mldecls} |
|
1692 |
\indexml{CodegenData.add-func}\verb|CodegenData.add_func: thm -> theory -> theory| \\ |
|
1693 |
\indexml{CodegenData.del-func}\verb|CodegenData.del_func: thm -> theory -> theory| \\ |
|
21348 | 1694 |
\indexml{CodegenData.add-funcl}\verb|CodegenData.add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory| \\ |
21172 | 1695 |
\indexml{CodegenData.add-inline}\verb|CodegenData.add_inline: thm -> theory -> theory| \\ |
1696 |
\indexml{CodegenData.del-inline}\verb|CodegenData.del_inline: thm -> theory -> theory| \\ |
|
22060 | 1697 |
\indexml{CodegenData.add-inline-proc}\verb|CodegenData.add_inline_proc: string * (theory -> cterm list -> thm list)|\isasep\isanewline% |
21348 | 1698 |
\verb| -> theory -> theory| \\ |
22060 | 1699 |
\indexml{CodegenData.del-inline-proc}\verb|CodegenData.del_inline_proc: string -> theory -> theory| \\ |
1700 |
\indexml{CodegenData.add-preproc}\verb|CodegenData.add_preproc: string * (theory -> thm list -> thm list)|\isasep\isanewline% |
|
21348 | 1701 |
\verb| -> theory -> theory| \\ |
22060 | 1702 |
\indexml{CodegenData.del-preproc}\verb|CodegenData.del_preproc: string -> theory -> theory| \\ |
21348 | 1703 |
\indexml{CodegenData.add-datatype}\verb|CodegenData.add_datatype: string * (((string * sort) list * (string * typ list) list)|\isasep\isanewline% |
1704 |
\verb| * thm list Susp.T) -> theory -> theory| \\ |
|
1705 |
\indexml{CodegenData.del-datatype}\verb|CodegenData.del_datatype: string -> theory -> theory| \\ |
|
1706 |
\indexml{CodegenData.get-datatype}\verb|CodegenData.get_datatype: theory -> string|\isasep\isanewline% |
|
1707 |
\verb| -> ((string * sort) list * (string * typ list) list) option| \\ |
|
21172 | 1708 |
\indexml{CodegenData.get-datatype-of-constr}\verb|CodegenData.get_datatype_of_constr: theory -> CodegenConsts.const -> string option| |
1709 |
\end{mldecls} |
|
1710 |
||
1711 |
\begin{description} |
|
1712 |
||
21348 | 1713 |
\item \verb|CodegenData.add_func|~\isa{thm}~\isa{thy} adds function |
1714 |
theorem \isa{thm} to executable content. |
|
1715 |
||
1716 |
\item \verb|CodegenData.del_func|~\isa{thm}~\isa{thy} removes function |
|
1717 |
theorem \isa{thm} from executable content, if present. |
|
1718 |
||
1719 |
\item \verb|CodegenData.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds |
|
22060 | 1720 |
suspended defining equations \isa{lthms} for constant |
21348 | 1721 |
\isa{const} to executable content. |
1722 |
||
1723 |
\item \verb|CodegenData.add_inline|~\isa{thm}~\isa{thy} adds |
|
1724 |
inlining theorem \isa{thm} to executable content. |
|
1725 |
||
1726 |
\item \verb|CodegenData.del_inline|~\isa{thm}~\isa{thy} remove |
|
1727 |
inlining theorem \isa{thm} from executable content, if present. |
|
1728 |
||
22060 | 1729 |
\item \verb|CodegenData.add_inline_proc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
1730 |
inline procedure \isa{f} (named \isa{name}) to executable content; |
|
21348 | 1731 |
\isa{f} is a computation of rewrite rules dependent on |
1732 |
the current theory context and the list of all arguments |
|
22060 | 1733 |
and right hand sides of the defining equations belonging |
21348 | 1734 |
to a certain function definition. |
1735 |
||
22060 | 1736 |
\item \verb|CodegenData.del_inline_proc|~\isa{name}~\isa{thy} removes |
1737 |
inline procedure named \isa{name} from executable content. |
|
1738 |
||
1739 |
\item \verb|CodegenData.add_preproc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
|
1740 |
generic preprocessor \isa{f} (named \isa{name}) to executable content; |
|
1741 |
\isa{f} is a transformation of the defining equations belonging |
|
21348 | 1742 |
to a certain function definition, depending on the |
1743 |
current theory context. |
|
1744 |
||
22060 | 1745 |
\item \verb|CodegenData.del_preproc|~\isa{name}~\isa{thy} removes |
1746 |
generic preprcoessor named \isa{name} from executable content. |
|
1747 |
||
21348 | 1748 |
\item \verb|CodegenData.add_datatype|~\isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}spec{\isacharcomma}\ cert{\isacharparenright}{\isacharparenright}}~\isa{thy} adds |
1749 |
a datatype to executable content, with type constructor |
|
1750 |
\isa{name} and specification \isa{spec}; \isa{spec} is |
|
1751 |
a pair consisting of a list of type variable with sort |
|
1752 |
constraints and a list of constructors with name |
|
1753 |
and types of arguments. The addition as datatype |
|
1754 |
has to be justified giving a certificate of suspended |
|
1755 |
theorems as witnesses for injectiveness and distinctness. |
|
1756 |
||
1757 |
\item \verb|CodegenData.del_datatype|~\isa{name}~\isa{thy} |
|
1758 |
remove a datatype from executable content, if present. |
|
1759 |
||
1760 |
\item \verb|CodegenData.get_datatype_of_constr|~\isa{thy}~\isa{const} |
|
1761 |
returns type constructor corresponding to |
|
1762 |
constructor \isa{const}; returns \isa{NONE} |
|
1763 |
if \isa{const} is no constructor. |
|
1764 |
||
1765 |
\end{description}% |
|
1766 |
\end{isamarkuptext}% |
|
1767 |
\isamarkuptrue% |
|
1768 |
% |
|
1769 |
\endisatagmlref |
|
1770 |
{\isafoldmlref}% |
|
1771 |
% |
|
1772 |
\isadelimmlref |
|
1773 |
% |
|
1774 |
\endisadelimmlref |
|
1775 |
% |
|
22060 | 1776 |
\isamarkupsubsection{defining equation systems: codegen\_funcgr.ML% |
21348 | 1777 |
} |
1778 |
\isamarkuptrue% |
|
1779 |
% |
|
1780 |
\begin{isamarkuptext}% |
|
1781 |
Out of the executable content of a theory, a normalized |
|
22060 | 1782 |
defining equation systems may be constructed containing |
21348 | 1783 |
function definitions for constants. The system is cached |
1784 |
until its underlying executable content changes.% |
|
1785 |
\end{isamarkuptext}% |
|
1786 |
\isamarkuptrue% |
|
1787 |
% |
|
1788 |
\isadelimmlref |
|
1789 |
% |
|
1790 |
\endisadelimmlref |
|
1791 |
% |
|
1792 |
\isatagmlref |
|
1793 |
% |
|
1794 |
\begin{isamarkuptext}% |
|
1795 |
\begin{mldecls} |
|
1796 |
\indexmltype{CodegenFuncgr.T}\verb|type CodegenFuncgr.T| \\ |
|
1797 |
\indexml{CodegenFuncgr.make}\verb|CodegenFuncgr.make: theory -> CodegenConsts.const list -> CodegenFuncgr.T| \\ |
|
1798 |
\indexml{CodegenFuncgr.funcs}\verb|CodegenFuncgr.funcs: CodegenFuncgr.T -> CodegenConsts.const -> thm list| \\ |
|
1799 |
\indexml{CodegenFuncgr.typ}\verb|CodegenFuncgr.typ: CodegenFuncgr.T -> CodegenConsts.const -> typ| \\ |
|
1800 |
\indexml{CodegenFuncgr.deps}\verb|CodegenFuncgr.deps: CodegenFuncgr.T|\isasep\isanewline% |
|
1801 |
\verb| -> CodegenConsts.const list -> CodegenConsts.const list list| \\ |
|
1802 |
\indexml{CodegenFuncgr.all}\verb|CodegenFuncgr.all: CodegenFuncgr.T -> CodegenConsts.const list| |
|
1803 |
\end{mldecls} |
|
1804 |
||
1805 |
\begin{description} |
|
1806 |
||
1807 |
\item \verb|CodegenFuncgr.T| represents |
|
22060 | 1808 |
a normalized defining equation system. |
21348 | 1809 |
|
1810 |
\item \verb|CodegenFuncgr.make|~\isa{thy}~\isa{cs} |
|
22060 | 1811 |
returns a normalized defining equation system, |
21348 | 1812 |
with the assertion that it contains any function |
1813 |
definition for constants \isa{cs} (if existing). |
|
1814 |
||
1815 |
\item \verb|CodegenFuncgr.funcs|~\isa{funcgr}~\isa{c} |
|
1816 |
retrieves function definition for constant \isa{c}. |
|
1817 |
||
1818 |
\item \verb|CodegenFuncgr.typ|~\isa{funcgr}~\isa{c} |
|
1819 |
retrieves function type for constant \isa{c}. |
|
1820 |
||
1821 |
\item \verb|CodegenFuncgr.deps|~\isa{funcgr}~\isa{cs} |
|
1822 |
returns the transitive closure of dependencies for |
|
1823 |
constants \isa{cs} as a partitioning where each partition |
|
1824 |
corresponds to a strongly connected component of |
|
1825 |
dependencies and any partition does \emph{not} |
|
1826 |
depend on partitions further left. |
|
1827 |
||
1828 |
\item \verb|CodegenFuncgr.all|~\isa{funcgr} |
|
1829 |
returns all currently represented constants. |
|
21172 | 1830 |
|
1831 |
\end{description}% |
|
1832 |
\end{isamarkuptext}% |
|
1833 |
\isamarkuptrue% |
|
1834 |
% |
|
1835 |
\endisatagmlref |
|
1836 |
{\isafoldmlref}% |
|
1837 |
% |
|
1838 |
\isadelimmlref |
|
1839 |
% |
|
1840 |
\endisadelimmlref |
|
1841 |
% |
|
1842 |
\isamarkupsubsection{Further auxiliary% |
|
1843 |
} |
|
1844 |
\isamarkuptrue% |
|
1845 |
% |
|
1846 |
\isadelimmlref |
|
1847 |
% |
|
1848 |
\endisadelimmlref |
|
1849 |
% |
|
1850 |
\isatagmlref |
|
1851 |
% |
|
1852 |
\begin{isamarkuptext}% |
|
1853 |
\begin{mldecls} |
|
1854 |
\indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\ |
|
1855 |
\indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\ |
|
1856 |
\indexml{CodegenConsts.consts-of}\verb|CodegenConsts.consts_of: theory -> term -> CodegenConsts.const list| \\ |
|
1857 |
\indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\ |
|
1858 |
\indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\ |
|
21348 | 1859 |
\indexmlstructure{CodegenFuncgr.Constgraph}\verb|structure CodegenFuncgr.Constgraph| \\ |
22060 | 1860 |
\indexml{CodegenFunc.typ-func}\verb|CodegenFunc.typ_func: thm -> typ| \\ |
1861 |
\indexml{CodegenFunc.rewrite-func}\verb|CodegenFunc.rewrite_func: thm list -> thm -> thm| \\ |
|
21348 | 1862 |
\end{mldecls} |
1863 |
||
1864 |
\begin{description} |
|
1865 |
||
1866 |
\item \verb|CodegenConsts.const_ord|,~\verb|CodegenConsts.eq_const| |
|
1867 |
provide order and equality on constant identifiers. |
|
1868 |
||
1869 |
\item \verb|CodegenConsts.Consttab|,~\verb|CodegenFuncgr.Constgraph| |
|
1870 |
provide advanced data structures with constant identifiers as keys. |
|
1871 |
||
1872 |
\item \verb|CodegenConsts.consts_of|~\isa{thy}~\isa{t} |
|
1873 |
returns all constant identifiers mentioned in a term \isa{t}. |
|
1874 |
||
1875 |
\item \verb|CodegenConsts.read_const|~\isa{thy}~\isa{s} |
|
1876 |
reads a constant as a concrete term expression \isa{s}. |
|
1877 |
||
22060 | 1878 |
\item \verb|CodegenFunc.typ_func|~\isa{thm} |
1879 |
extracts the type of a constant in a defining equation \isa{thm}. |
|
21348 | 1880 |
|
22060 | 1881 |
\item \verb|CodegenFunc.rewrite_func|~\isa{rews}~\isa{thm} |
1882 |
rewrites a defining equation \isa{thm} with a set of rewrite |
|
21348 | 1883 |
rules \isa{rews}; only arguments and right hand side are rewritten, |
22060 | 1884 |
not the head of the defining equation. |
21348 | 1885 |
|
1886 |
\end{description}% |
|
21172 | 1887 |
\end{isamarkuptext}% |
1888 |
\isamarkuptrue% |
|
1889 |
% |
|
1890 |
\endisatagmlref |
|
1891 |
{\isafoldmlref}% |
|
1892 |
% |
|
1893 |
\isadelimmlref |
|
1894 |
% |
|
1895 |
\endisadelimmlref |
|
1896 |
% |
|
20967 | 1897 |
\isamarkupsubsection{Implementing code generator applications% |
1898 |
} |
|
1899 |
\isamarkuptrue% |
|
1900 |
% |
|
21172 | 1901 |
\begin{isamarkuptext}% |
21348 | 1902 |
Implementing code generator applications on top |
1903 |
of the framework set out so far usually not only |
|
1904 |
involves using those primitive interfaces |
|
1905 |
but also storing code-dependent data and various |
|
1906 |
other things. |
|
1907 |
||
1908 |
\begin{warn} |
|
21172 | 1909 |
Some interfaces discussed here have not reached |
1910 |
a final state yet. |
|
1911 |
Changes likely to occur in future. |
|
21452 | 1912 |
\end{warn}% |
21172 | 1913 |
\end{isamarkuptext}% |
1914 |
\isamarkuptrue% |
|
1915 |
% |
|
1916 |
\isamarkupsubsubsection{Data depending on the theory's executable content% |
|
1917 |
} |
|
1918 |
\isamarkuptrue% |
|
1919 |
% |
|
21348 | 1920 |
\begin{isamarkuptext}% |
21452 | 1921 |
Due to incrementality of code generation, changes in the |
1922 |
theory's executable content have to be propagated in a |
|
1923 |
certain fashion. Additionally, such changes may occur |
|
1924 |
not only during theory extension but also during theory |
|
1925 |
merge, which is a little bit nasty from an implementation |
|
1926 |
point of view. The framework provides a solution |
|
1927 |
to this technical challenge by providing a functorial |
|
1928 |
data slot \verb|CodeDataFun|; on instantiation |
|
1929 |
of this functor, the following types and operations |
|
1930 |
are required: |
|
1931 |
||
1932 |
\medskip |
|
21348 | 1933 |
\begin{tabular}{l} |
1934 |
\isa{val\ name{\isacharcolon}\ string} \\ |
|
1935 |
\isa{type\ T} \\ |
|
1936 |
\isa{val\ empty{\isacharcolon}\ T} \\ |
|
1937 |
\isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\ |
|
1938 |
\isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodegenConsts{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T} |
|
1939 |
\end{tabular} |
|
1940 |
||
21452 | 1941 |
\begin{description} |
1942 |
||
1943 |
\item \isa{name} is a system-wide unique name identifying the data. |
|
1944 |
||
1945 |
\item \isa{T} the type of data to store. |
|
1946 |
||
1947 |
\item \isa{empty} initial (empty) data. |
|
1948 |
||
1949 |
\item \isa{merge} merging two data slots. |
|
1950 |
||
1951 |
\item \isa{purge}~\isa{thy}~\isa{cs} propagates changes in executable content; |
|
1952 |
if possible, the current theory context is handed over |
|
1953 |
as argument \isa{thy} (if there is no current theory context (e.g.~during |
|
1954 |
theory merge, \verb|NONE|); \isa{cs} indicates the kind |
|
1955 |
of change: \verb|NONE| stands for a fundamental change |
|
1956 |
which invalidates any existing code, \isa{SOME\ cs} |
|
1957 |
hints that executable content for constants \isa{cs} |
|
1958 |
has changed. |
|
1959 |
||
1960 |
\end{description} |
|
1961 |
||
1962 |
An instance of \verb|CodeDataFun| provides the following |
|
1963 |
interface: |
|
1964 |
||
21348 | 1965 |
\medskip |
1966 |
\begin{tabular}{l} |
|
1967 |
\isa{init{\isacharcolon}\ theory\ {\isasymrightarrow}\ theory} \\ |
|
1968 |
\isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\ |
|
1969 |
\isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\ |
|
1970 |
\isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T} |
|
21452 | 1971 |
\end{tabular} |
1972 |
||
1973 |
\begin{description} |
|
1974 |
||
1975 |
\item \isa{init} initialization during theory setup. |
|
1976 |
||
1977 |
\item \isa{get} retrieval of the current data. |
|
1978 |
||
1979 |
\item \isa{change} update of current data (cached!) |
|
1980 |
by giving a continuation. |
|
1981 |
||
1982 |
\item \isa{change{\isacharunderscore}yield} update with side result. |
|
1983 |
||
1984 |
\end{description}% |
|
1985 |
\end{isamarkuptext}% |
|
1986 |
\isamarkuptrue% |
|
1987 |
% |
|
1988 |
\isamarkupsubsubsection{Datatype hooks% |
|
1989 |
} |
|
1990 |
\isamarkuptrue% |
|
1991 |
% |
|
1992 |
\begin{isamarkuptext}% |
|
1993 |
Isabelle/HOL's datatype package provides a mechanism to |
|
1994 |
extend theories depending on datatype declarations: |
|
1995 |
\emph{datatype hooks}. For example, when declaring a new |
|
22060 | 1996 |
datatype, a hook proves defining equations for equality on |
21452 | 1997 |
that datatype (if possible).% |
21348 | 1998 |
\end{isamarkuptext}% |
1999 |
\isamarkuptrue% |
|
2000 |
% |
|
2001 |
\isadelimmlref |
|
2002 |
% |
|
2003 |
\endisadelimmlref |
|
2004 |
% |
|
2005 |
\isatagmlref |
|
2006 |
% |
|
2007 |
\begin{isamarkuptext}% |
|
2008 |
\begin{mldecls} |
|
21452 | 2009 |
\indexmltype{DatatypeHooks.hook}\verb|type DatatypeHooks.hook = string list -> theory -> theory| \\ |
2010 |
\indexml{DatatypeHooks.add}\verb|DatatypeHooks.add: DatatypeHooks.hook -> theory -> theory| |
|
21348 | 2011 |
\end{mldecls} |
2012 |
||
2013 |
\begin{description} |
|
2014 |
||
21452 | 2015 |
\item \verb|DatatypeHooks.hook| specifies the interface |
2016 |
of \emph{datatype hooks}: a theory update |
|
2017 |
depending on the list of newly introduced |
|
2018 |
datatype names. |
|
2019 |
||
2020 |
\item \verb|DatatypeHooks.add| adds a hook to the |
|
2021 |
chain of all hooks. |
|
21348 | 2022 |
|
2023 |
\end{description}% |
|
2024 |
\end{isamarkuptext}% |
|
2025 |
\isamarkuptrue% |
|
2026 |
% |
|
2027 |
\endisatagmlref |
|
2028 |
{\isafoldmlref}% |
|
2029 |
% |
|
2030 |
\isadelimmlref |
|
2031 |
% |
|
2032 |
\endisadelimmlref |
|
2033 |
% |
|
21452 | 2034 |
\isamarkupsubsubsection{Trivial typedefs -- type copies% |
21172 | 2035 |
} |
2036 |
\isamarkuptrue% |
|
2037 |
% |
|
21452 | 2038 |
\begin{isamarkuptext}% |
2039 |
Sometimes packages will introduce new types |
|
2040 |
as \emph{marked type copies} similar to Haskell's |
|
2041 |
\isa{newtype} declaration (e.g. the HOL record package) |
|
2042 |
\emph{without} tinkering with the overhead of datatypes. |
|
2043 |
Technically, these type copies are trivial forms of typedefs. |
|
2044 |
Since these type copies in code generation view are nothing |
|
2045 |
else than datatypes, they have been given a own package |
|
2046 |
in order to faciliate code generation:% |
|
2047 |
\end{isamarkuptext}% |
|
2048 |
\isamarkuptrue% |
|
2049 |
% |
|
21348 | 2050 |
\isadelimmlref |
2051 |
% |
|
2052 |
\endisadelimmlref |
|
2053 |
% |
|
2054 |
\isatagmlref |
|
2055 |
% |
|
21172 | 2056 |
\begin{isamarkuptext}% |
21348 | 2057 |
\begin{mldecls} |
21452 | 2058 |
\indexmltype{TypecopyPackage.info}\verb|type TypecopyPackage.info| \\ |
2059 |
\indexml{TypecopyPackage.add-typecopy}\verb|TypecopyPackage.add_typecopy: |\isasep\isanewline% |
|
2060 |
\verb| bstring * string list -> typ -> (bstring * bstring) option|\isasep\isanewline% |
|
2061 |
\verb| -> theory -> (string * TypecopyPackage.info) * theory| \\ |
|
2062 |
\indexml{TypecopyPackage.get-typecopy-info}\verb|TypecopyPackage.get_typecopy_info: theory|\isasep\isanewline% |
|
2063 |
\verb| -> string -> TypecopyPackage.info option| \\ |
|
2064 |
\indexml{TypecopyPackage.get-spec}\verb|TypecopyPackage.get_spec: theory -> string|\isasep\isanewline% |
|
2065 |
\verb| -> (string * sort) list * (string * typ list) list| \\ |
|
2066 |
\indexmltype{TypecopyPackage.hook}\verb|type TypecopyPackage.hook = string * TypecopyPackage.info -> theory -> theory| \\ |
|
2067 |
\indexml{TypecopyPackage.add-hook}\verb|TypecopyPackage.add_hook: TypecopyPackage.hook -> theory -> theory| \\ |
|
2068 |
\end{mldecls} |
|
2069 |
||
2070 |
\begin{description} |
|
2071 |
||
2072 |
\item \verb|TypecopyPackage.info| a record containing |
|
2073 |
the specification and further data of a type copy. |
|
2074 |
||
2075 |
\item \verb|TypecopyPackage.add_typecopy| defines a new |
|
2076 |
type copy. |
|
2077 |
||
2078 |
\item \verb|TypecopyPackage.get_typecopy_info| retrieves |
|
2079 |
data of an existing type copy. |
|
2080 |
||
2081 |
\item \verb|TypecopyPackage.get_spec| retrieves datatype-like |
|
2082 |
specification of a type copy. |
|
2083 |
||
2084 |
\item \verb|TypecopyPackage.hook|,~\verb|TypecopyPackage.add_hook| |
|
2085 |
provide a hook mechanism corresponding to the hook mechanism |
|
2086 |
on datatypes. |
|
2087 |
||
2088 |
\end{description}% |
|
21348 | 2089 |
\end{isamarkuptext}% |
2090 |
\isamarkuptrue% |
|
2091 |
% |
|
21452 | 2092 |
\endisatagmlref |
2093 |
{\isafoldmlref}% |
|
2094 |
% |
|
2095 |
\isadelimmlref |
|
2096 |
% |
|
2097 |
\endisadelimmlref |
|
2098 |
% |
|
2099 |
\isamarkupsubsubsection{Unifying type copies and datatypes% |
|
2100 |
} |
|
2101 |
\isamarkuptrue% |
|
2102 |
% |
|
2103 |
\begin{isamarkuptext}% |
|
2104 |
Since datatypes and type copies are mapped to the same concept (datatypes) |
|
2105 |
by code generation, the view on both is unified \qt{code types}:% |
|
2106 |
\end{isamarkuptext}% |
|
2107 |
\isamarkuptrue% |
|
2108 |
% |
|
2109 |
\isadelimmlref |
|
2110 |
% |
|
2111 |
\endisadelimmlref |
|
2112 |
% |
|
2113 |
\isatagmlref |
|
2114 |
% |
|
21348 | 2115 |
\begin{isamarkuptext}% |
2116 |
\begin{mldecls} |
|
21452 | 2117 |
\indexmltype{DatatypeCodegen.hook}\verb|type DatatypeCodegen.hook = (string * (bool * ((string * sort) list|\isasep\isanewline% |
2118 |
\verb| * (string * typ list) list))) list|\isasep\isanewline% |
|
21348 | 2119 |
\verb| -> theory -> theory| \\ |
2120 |
\indexml{DatatypeCodegen.add-codetypes-hook-bootstrap}\verb|DatatypeCodegen.add_codetypes_hook_bootstrap: |\isasep\isanewline% |
|
2121 |
\verb| DatatypeCodegen.hook -> theory -> theory| |
|
2122 |
\end{mldecls}% |
|
2123 |
\end{isamarkuptext}% |
|
2124 |
\isamarkuptrue% |
|
2125 |
% |
|
2126 |
\endisatagmlref |
|
2127 |
{\isafoldmlref}% |
|
2128 |
% |
|
2129 |
\isadelimmlref |
|
2130 |
% |
|
2131 |
\endisadelimmlref |
|
2132 |
% |
|
2133 |
\begin{isamarkuptext}% |
|
21452 | 2134 |
\begin{description} |
2135 |
||
2136 |
\item \verb|DatatypeCodegen.hook| specifies the code type hook |
|
2137 |
interface: a theory transformation depending on a list of |
|
2138 |
mutual recursive code types; each entry in the list |
|
2139 |
has the structure \isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}is{\isacharunderscore}data{\isacharcomma}\ {\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}{\isacharparenright}{\isacharparenright}} |
|
2140 |
where \isa{name} is the name of the code type, \isa{is{\isacharunderscore}data} |
|
2141 |
is true iff \isa{name} is a datatype rather then a type copy, |
|
2142 |
and \isa{{\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}} is the specification of the code type. |
|
2143 |
||
2144 |
\item \verb|DatatypeCodegen.add_codetypes_hook_bootstrap| adds a code |
|
2145 |
type hook; the hook is immediately processed for all already |
|
2146 |
existing datatypes, in blocks of mutual recursive datatypes, |
|
2147 |
where all datatypes a block depends on are processed before |
|
2148 |
the block. |
|
2149 |
||
2150 |
\end{description} |
|
2151 |
||
2152 |
\emph{Happy proving, happy hacking!}% |
|
21172 | 2153 |
\end{isamarkuptext}% |
2154 |
\isamarkuptrue% |
|
2155 |
% |
|
20967 | 2156 |
\isadelimtheory |
2157 |
% |
|
2158 |
\endisadelimtheory |
|
2159 |
% |
|
2160 |
\isatagtheory |
|
2161 |
\isacommand{end}\isamarkupfalse% |
|
2162 |
% |
|
2163 |
\endisatagtheory |
|
2164 |
{\isafoldtheory}% |
|
2165 |
% |
|
2166 |
\isadelimtheory |
|
2167 |
% |
|
2168 |
\endisadelimtheory |
|
2169 |
\isanewline |
|
2170 |
\end{isabellebody}% |
|
2171 |
%%% Local Variables: |
|
2172 |
%%% mode: latex |
|
2173 |
%%% TeX-master: "root" |
|
2174 |
%%% End: |