author | haftmann |
Sat, 10 Feb 2007 09:26:10 +0100 | |
changeset 22292 | 3b118010ec08 |
parent 22188 | a63889770d57 |
child 22385 | cc2be3315e72 |
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 |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
64 |
supports SML \cite{SML}, OCaml \cite{OCaml} and Haskell \cite{haskell-revised-report}). |
21172 | 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 |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
74 |
with the ingredients |
21348 | 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 |
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
292 |
using the \isa{{\isasymPRINTCODESETUP}} command:% |
21172 | 293 |
\end{isamarkuptext}% |
294 |
\isamarkuptrue% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
295 |
\isacommand{print{\isacharunderscore}codesetup}\isamarkupfalse% |
21172 | 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} |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
397 |
The attributes \emph{code} and \emph{code del} |
21172 | 398 |
associated with the existing code generator also apply to |
399 |
the new one: \emph{code} implies \emph{code func}, |
|
400 |
and \emph{code del} implies \emph{code nofunc}. |
|
401 |
\end{warn}% |
|
402 |
\end{isamarkuptext}% |
|
403 |
\isamarkuptrue% |
|
404 |
% |
|
20967 | 405 |
\isamarkupsubsection{Type classes% |
406 |
} |
|
407 |
\isamarkuptrue% |
|
408 |
% |
|
21172 | 409 |
\begin{isamarkuptext}% |
410 |
Type classes enter the game via the Isar class package. |
|
411 |
For a short introduction how to use it, see \cite{isabelle-classes}; |
|
412 |
here we just illustrate its impact on code generation. |
|
413 |
||
414 |
In a target language, type classes may be represented |
|
21186 | 415 |
natively (as in the case of Haskell). For languages |
21172 | 416 |
like SML, they are implemented using \emph{dictionaries}. |
21186 | 417 |
Our following example specifies a class \qt{null}, |
21172 | 418 |
assigning to each of its inhabitants a \qt{null} value:% |
419 |
\end{isamarkuptext}% |
|
420 |
\isamarkuptrue% |
|
421 |
\isacommand{class}\isamarkupfalse% |
|
422 |
\ null\ {\isacharequal}\isanewline |
|
423 |
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline |
|
424 |
\isanewline |
|
425 |
\isacommand{consts}\isamarkupfalse% |
|
426 |
\isanewline |
|
427 |
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline |
|
428 |
\isanewline |
|
429 |
\isacommand{primrec}\isamarkupfalse% |
|
430 |
\isanewline |
|
431 |
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline |
|
432 |
\ \ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}% |
|
433 |
\begin{isamarkuptext}% |
|
434 |
We provide some instances for our \isa{null}:% |
|
435 |
\end{isamarkuptext}% |
|
436 |
\isamarkuptrue% |
|
437 |
\isacommand{instance}\isamarkupfalse% |
|
438 |
\ option\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
439 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ None{\isachardoublequoteclose}% |
|
440 |
\isadelimproof |
|
441 |
\ % |
|
442 |
\endisadelimproof |
|
443 |
% |
|
444 |
\isatagproof |
|
445 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
446 |
% |
|
447 |
\endisatagproof |
|
448 |
{\isafoldproof}% |
|
449 |
% |
|
450 |
\isadelimproof |
|
451 |
% |
|
452 |
\endisadelimproof |
|
453 |
\isanewline |
|
454 |
\isanewline |
|
455 |
\isacommand{instance}\isamarkupfalse% |
|
456 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
457 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}% |
|
458 |
\isadelimproof |
|
459 |
\ % |
|
460 |
\endisadelimproof |
|
461 |
% |
|
462 |
\isatagproof |
|
463 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
464 |
% |
|
465 |
\endisatagproof |
|
466 |
{\isafoldproof}% |
|
467 |
% |
|
468 |
\isadelimproof |
|
469 |
% |
|
470 |
\endisadelimproof |
|
471 |
% |
|
472 |
\begin{isamarkuptext}% |
|
473 |
Constructing a dummy example:% |
|
474 |
\end{isamarkuptext}% |
|
475 |
\isamarkuptrue% |
|
476 |
\isacommand{definition}\isamarkupfalse% |
|
477 |
\isanewline |
|
478 |
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}% |
|
479 |
\begin{isamarkuptext}% |
|
21186 | 480 |
Type classes offer a suitable occasion to introduce |
21172 | 481 |
the Haskell serializer. Its usage is almost the same |
482 |
as SML, but, in accordance with conventions |
|
483 |
some Haskell systems enforce, each module ends |
|
484 |
up in a single file. The module hierarchy is reflected in |
|
485 |
the file system, with root given by the user.% |
|
486 |
\end{isamarkuptext}% |
|
487 |
\isamarkuptrue% |
|
488 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
489 |
\ dummy\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}% |
|
490 |
\begin{isamarkuptext}% |
|
491 |
\lsthaskell{Thy/examples/Codegen.hs} |
|
492 |
||
493 |
(we have left out all other modules). |
|
494 |
||
495 |
The whole code in SML with explicit dictionary passing:% |
|
496 |
\end{isamarkuptext}% |
|
497 |
\isamarkuptrue% |
|
498 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
499 |
\ dummy\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
500 |
\begin{isamarkuptext}% |
|
501 |
\lstsml{Thy/examples/class.ML}% |
|
502 |
\end{isamarkuptext}% |
|
20967 | 503 |
\isamarkuptrue% |
504 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
505 |
\begin{isamarkuptext}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
506 |
or in OCaml:% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
507 |
\end{isamarkuptext}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
508 |
\isamarkuptrue% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
509 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
510 |
\ dummy\ {\isacharparenleft}OCaml\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ocaml{\isachardoublequoteclose}{\isacharparenright}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
511 |
\begin{isamarkuptext}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
512 |
\lstsml{Thy/examples/class.ocaml}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
513 |
\end{isamarkuptext}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
514 |
\isamarkuptrue% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
515 |
% |
20967 | 516 |
\isamarkupsubsection{Incremental code generation% |
517 |
} |
|
518 |
\isamarkuptrue% |
|
519 |
% |
|
21172 | 520 |
\begin{isamarkuptext}% |
521 |
Code generation is \emph{incremental}: theorems |
|
522 |
and abstract intermediate code are cached and extended on demand. |
|
523 |
The cache may be partially or fully dropped if the underlying |
|
524 |
executable content of the theory changes. |
|
525 |
Implementation of caching is supposed to transparently |
|
526 |
hid away the details from the user. Anyway, caching |
|
527 |
reaches the surface by using a slightly more general form |
|
21348 | 528 |
of the \isa{{\isasymCODEGEN}}: either the list of constants or the |
21172 | 529 |
list of serialization expressions may be dropped. If no |
530 |
serialization expressions are given, only abstract code |
|
531 |
is generated and cached; if no constants are given, the |
|
532 |
current cache is serialized. |
|
533 |
||
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
534 |
For explorative purpose, the |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
535 |
\isa{{\isasymCODETHMS}} command may prove useful:% |
21172 | 536 |
\end{isamarkuptext}% |
537 |
\isamarkuptrue% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
538 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
539 |
% |
21172 | 540 |
\begin{isamarkuptext}% |
22060 | 541 |
\noindent print all cached defining equations (i.e.~\emph{after} |
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
542 |
any applied transformation). A |
21172 | 543 |
list of constants may be given; their function |
21186 | 544 |
equations are added to the cache if not already present.% |
21172 | 545 |
\end{isamarkuptext}% |
546 |
\isamarkuptrue% |
|
547 |
% |
|
548 |
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}% |
|
549 |
} |
|
550 |
\isamarkuptrue% |
|
551 |
% |
|
552 |
\begin{isamarkuptext}% |
|
553 |
In this tutorial, we do not attempt to give an exhaustive |
|
554 |
description of the code generator framework; instead, |
|
555 |
we cast a light on advanced topics by introducing |
|
556 |
them together with practically motivated examples. Concerning |
|
557 |
further reading, see |
|
558 |
||
559 |
\begin{itemize} |
|
560 |
||
561 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} |
|
562 |
for exhaustive syntax diagrams. |
|
21348 | 563 |
\item or \fixme[ref] which deals with foundational issues |
21172 | 564 |
of the code generator framework. |
565 |
||
566 |
\end{itemize}% |
|
567 |
\end{isamarkuptext}% |
|
568 |
\isamarkuptrue% |
|
569 |
% |
|
570 |
\isamarkupsubsection{Library theories% |
|
571 |
} |
|
572 |
\isamarkuptrue% |
|
573 |
% |
|
574 |
\begin{isamarkuptext}% |
|
575 |
The HOL \emph{Main} theory already provides a code generator setup |
|
576 |
which should be suitable for most applications. Common extensions |
|
577 |
and modifications are available by certain theories of the HOL |
|
578 |
library; beside being useful in applications, they may serve |
|
21186 | 579 |
as a tutorial for customizing the code generator setup. |
21172 | 580 |
|
581 |
\begin{description} |
|
582 |
||
21452 | 583 |
\item[\isa{ExecutableSet}] allows to generate code |
21172 | 584 |
for finite sets using lists. |
21452 | 585 |
\item[\isa{ExecutableRat}] \label{exec_rat} implements rational |
21172 | 586 |
numbers as triples \isa{{\isacharparenleft}sign{\isacharcomma}\ enumerator{\isacharcomma}\ denominator{\isacharparenright}}. |
21452 | 587 |
\item[\isa{EfficientNat}] \label{eff_nat} implements natural numbers by integers, |
21186 | 588 |
which in general will result in higher efficency; pattern |
21172 | 589 |
matching with \isa{{\isadigit{0}}} / \isa{Suc} |
21348 | 590 |
is eliminated. |
21452 | 591 |
\item[\isa{MLString}] provides an additional datatype \isa{mlstring}; |
21172 | 592 |
in the HOL default setup, strings in HOL are mapped to list |
593 |
of chars in SML; values of type \isa{mlstring} are |
|
594 |
mapped to strings in SML. |
|
595 |
||
596 |
\end{description}% |
|
597 |
\end{isamarkuptext}% |
|
598 |
\isamarkuptrue% |
|
599 |
% |
|
600 |
\isamarkupsubsection{Preprocessing% |
|
20967 | 601 |
} |
602 |
\isamarkuptrue% |
|
603 |
% |
|
21172 | 604 |
\begin{isamarkuptext}% |
605 |
Before selected function theorems are turned into abstract |
|
606 |
code, a chain of definitional transformation steps is carried |
|
21186 | 607 |
out: \emph{preprocessing}. There are three possibilities |
21172 | 608 |
to customize preprocessing: \emph{inline theorems}, |
609 |
\emph{inline procedures} and \emph{generic preprocessors}. |
|
610 |
||
611 |
\emph{Inline theorems} are rewriting rules applied to each |
|
22060 | 612 |
defining equation. Due to the interpretation of theorems |
613 |
of defining equations, rewrites are applied to the right |
|
21172 | 614 |
hand side and the arguments of the left hand side of an |
615 |
equation, but never to the constant heading the left hand side. |
|
616 |
Inline theorems may be declared an undeclared using the |
|
21186 | 617 |
\emph{code inline} or \emph{code noinline} attribute respectively. |
21172 | 618 |
|
619 |
Some common applications:% |
|
620 |
\end{isamarkuptext}% |
|
621 |
\isamarkuptrue% |
|
622 |
% |
|
623 |
\begin{itemize} |
|
624 |
\item replacing non-executable constructs by executable ones: \\ |
|
625 |
\isacommand{lemma}\isamarkupfalse% |
|
626 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
627 |
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
|
628 |
\isadelimproof |
|
629 |
\ % |
|
630 |
\endisadelimproof |
|
631 |
% |
|
632 |
\isatagproof |
|
633 |
\isacommand{by}\isamarkupfalse% |
|
634 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
635 |
\endisatagproof |
|
636 |
{\isafoldproof}% |
|
637 |
% |
|
638 |
\isadelimproof |
|
639 |
% |
|
640 |
\endisadelimproof |
|
641 |
% |
|
642 |
\item eliminating superfluous constants: \\ |
|
643 |
\isacommand{lemma}\isamarkupfalse% |
|
644 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
645 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}% |
|
646 |
\isadelimproof |
|
647 |
\ % |
|
648 |
\endisadelimproof |
|
649 |
% |
|
650 |
\isatagproof |
|
651 |
\isacommand{by}\isamarkupfalse% |
|
652 |
\ simp% |
|
653 |
\endisatagproof |
|
654 |
{\isafoldproof}% |
|
655 |
% |
|
656 |
\isadelimproof |
|
657 |
% |
|
658 |
\endisadelimproof |
|
659 |
% |
|
660 |
\item replacing executable but inconvenient constructs: \\ |
|
661 |
\isacommand{lemma}\isamarkupfalse% |
|
662 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
|
663 |
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}% |
|
664 |
\isadelimproof |
|
665 |
\ % |
|
666 |
\endisadelimproof |
|
667 |
% |
|
668 |
\isatagproof |
|
669 |
\isacommand{by}\isamarkupfalse% |
|
670 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
671 |
\endisatagproof |
|
672 |
{\isafoldproof}% |
|
673 |
% |
|
674 |
\isadelimproof |
|
675 |
% |
|
676 |
\endisadelimproof |
|
677 |
% |
|
678 |
\end{itemize} |
|
679 |
% |
|
680 |
\begin{isamarkuptext}% |
|
681 |
The current set of inline theorems may be inspected using |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
682 |
the \isa{{\isasymPRINTCODESETUP}} command. |
21172 | 683 |
|
684 |
\emph{Inline procedures} are a generalized version of inline |
|
685 |
theorems written in ML -- rewrite rules are generated dependent |
|
686 |
on the function theorems for a certain function. One |
|
687 |
application is the implicit expanding of \isa{nat} numerals |
|
688 |
to \isa{{\isadigit{0}}} / \isa{Suc} representation. See further |
|
689 |
\secref{sec:ml} |
|
690 |
||
691 |
\emph{Generic preprocessors} provide a most general interface, |
|
692 |
transforming a list of function theorems to another |
|
693 |
list of function theorems, provided that neither the heading |
|
694 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc} |
|
21348 | 695 |
pattern elimination implemented in |
21452 | 696 |
theory \isa{EfficientNat} (\secref{eff_nat}) uses this |
21172 | 697 |
interface. |
698 |
||
699 |
\begin{warn} |
|
700 |
The order in which single preprocessing steps are carried |
|
701 |
out currently is not specified; in particular, preprocessing |
|
21186 | 702 |
is \emph{no} fix point process. Keep this in mind when |
21172 | 703 |
setting up the preprocessor. |
704 |
||
705 |
Further, the attribute \emph{code unfold} |
|
706 |
associated with the existing code generator also applies to |
|
707 |
the new one: \emph{code unfold} implies \emph{code inline}. |
|
708 |
\end{warn}% |
|
709 |
\end{isamarkuptext}% |
|
710 |
\isamarkuptrue% |
|
711 |
% |
|
712 |
\isamarkupsubsection{Customizing serialization% |
|
713 |
} |
|
714 |
\isamarkuptrue% |
|
715 |
% |
|
716 |
\begin{isamarkuptext}% |
|
717 |
Consider the following function and its corresponding |
|
718 |
SML code:% |
|
719 |
\end{isamarkuptext}% |
|
720 |
\isamarkuptrue% |
|
721 |
\isacommand{fun}\isamarkupfalse% |
|
722 |
\isanewline |
|
723 |
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
21348 | 724 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}% |
725 |
\isadelimtt |
|
21172 | 726 |
% |
21348 | 727 |
\endisadelimtt |
728 |
% |
|
729 |
\isatagtt |
|
21172 | 730 |
% |
21348 | 731 |
\endisatagtt |
732 |
{\isafoldtt}% |
|
733 |
% |
|
734 |
\isadelimtt |
|
735 |
\isanewline |
|
21172 | 736 |
% |
21348 | 737 |
\endisadelimtt |
21172 | 738 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
21348 | 739 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 740 |
\begin{isamarkuptext}% |
21348 | 741 |
\lstsml{Thy/examples/bool_literal.ML} |
21172 | 742 |
|
743 |
Though this is correct code, it is a little bit unsatisfactory: |
|
744 |
boolean values and operators are materialized as distinguished |
|
745 |
entities with have nothing to do with the SML-builtin notion |
|
746 |
of \qt{bool}. This results in less readable code; |
|
747 |
additionally, eager evaluation may cause programs to |
|
748 |
loop or break which would perfectly terminate when |
|
749 |
the existing SML \qt{bool} would be used. To map |
|
750 |
the HOL \qt{bool} on SML \qt{bool}, we may use |
|
751 |
\qn{custom serializations}:% |
|
752 |
\end{isamarkuptext}% |
|
753 |
\isamarkuptrue% |
|
21348 | 754 |
% |
755 |
\isadelimtt |
|
756 |
% |
|
757 |
\endisadelimtt |
|
758 |
% |
|
759 |
\isatagtt |
|
21172 | 760 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
761 |
\ bool\isanewline |
|
762 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
763 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
764 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
765 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 766 |
\endisatagtt |
767 |
{\isafoldtt}% |
|
768 |
% |
|
769 |
\isadelimtt |
|
770 |
% |
|
771 |
\endisadelimtt |
|
772 |
% |
|
21172 | 773 |
\begin{isamarkuptext}% |
21348 | 774 |
The \isa{{\isasymCODETYPE}} commad takes a type constructor |
21172 | 775 |
as arguments together with a list of custom serializations. |
776 |
Each custom serialization starts with a target language |
|
777 |
identifier followed by an expression, which during |
|
778 |
code serialization is inserted whenever the type constructor |
|
21348 | 779 |
would occur. For constants, \isa{{\isasymCODECONST}} implements |
780 |
the corresponding mechanism. Each ``\verb|_|'' in |
|
21172 | 781 |
a serialization expression is treated as a placeholder |
782 |
for the type constructor's (the constant's) arguments.% |
|
783 |
\end{isamarkuptext}% |
|
784 |
\isamarkuptrue% |
|
785 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
786 |
\ SML\isanewline |
|
787 |
\ \ bool\ true\ false% |
|
788 |
\begin{isamarkuptext}% |
|
789 |
To assert that the existing \qt{bool}, \qt{true} and \qt{false} |
|
21348 | 790 |
is not used for generated code, we use \isa{{\isasymCODERESERVED}}. |
21172 | 791 |
|
792 |
After this setup, code looks quite more readable:% |
|
793 |
\end{isamarkuptext}% |
|
794 |
\isamarkuptrue% |
|
795 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
21348 | 796 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 797 |
\begin{isamarkuptext}% |
21348 | 798 |
\lstsml{Thy/examples/bool_mlbool.ML} |
21172 | 799 |
|
800 |
This still is not perfect: the parentheses |
|
21348 | 801 |
around the \qt{andalso} expression are superfluous. |
802 |
Though the serializer |
|
21172 | 803 |
by no means attempts to imitate the rich Isabelle syntax |
804 |
framework, it provides some common idioms, notably |
|
805 |
associative infixes with precedences which may be used here:% |
|
806 |
\end{isamarkuptext}% |
|
807 |
\isamarkuptrue% |
|
21348 | 808 |
% |
809 |
\isadelimtt |
|
810 |
% |
|
811 |
\endisadelimtt |
|
812 |
% |
|
813 |
\isatagtt |
|
21172 | 814 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
815 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
21348 | 816 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
817 |
\endisatagtt |
|
818 |
{\isafoldtt}% |
|
819 |
% |
|
820 |
\isadelimtt |
|
821 |
% |
|
822 |
\endisadelimtt |
|
823 |
\isanewline |
|
21172 | 824 |
\isanewline |
825 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
21348 | 826 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 827 |
\begin{isamarkuptext}% |
21348 | 828 |
\lstsml{Thy/examples/bool_infix.ML} |
21172 | 829 |
|
830 |
Next, we try to map HOL pairs to SML pairs, using the |
|
21348 | 831 |
infix ``\verb|*|'' type constructor and parentheses:% |
21172 | 832 |
\end{isamarkuptext}% |
833 |
\isamarkuptrue% |
|
834 |
\isanewline |
|
21348 | 835 |
% |
836 |
\isadelimtt |
|
837 |
% |
|
838 |
\endisadelimtt |
|
839 |
% |
|
840 |
\isatagtt |
|
21172 | 841 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
842 |
\ {\isacharasterisk}\isanewline |
|
843 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
844 |
\isanewline |
|
845 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
846 |
\ Pair\isanewline |
|
847 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 848 |
\endisatagtt |
849 |
{\isafoldtt}% |
|
850 |
% |
|
851 |
\isadelimtt |
|
852 |
% |
|
853 |
\endisadelimtt |
|
854 |
% |
|
21172 | 855 |
\begin{isamarkuptext}% |
21348 | 856 |
The initial bang ``\verb|!|'' tells the serializer to never put |
21172 | 857 |
parentheses around the whole expression (they are already present), |
858 |
while the parentheses around argument place holders |
|
859 |
tell not to put parentheses around the arguments. |
|
21348 | 860 |
The slash ``\verb|/|'' (followed by arbitrary white space) |
21172 | 861 |
inserts a space which may be used as a break if necessary |
862 |
during pretty printing. |
|
863 |
||
21186 | 864 |
So far, we did only provide more idiomatic serializations for |
865 |
constructs which would be executable on their own. Target-specific |
|
866 |
serializations may also be used to \emph{implement} constructs |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
867 |
which have no explicit notion of executability. For example, |
21186 | 868 |
take the HOL integers:% |
869 |
\end{isamarkuptext}% |
|
870 |
\isamarkuptrue% |
|
871 |
\isacommand{definition}\isamarkupfalse% |
|
872 |
\isanewline |
|
21993 | 873 |
\ \ double{\isacharunderscore}inc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21186 | 874 |
\ \ {\isachardoublequoteopen}double{\isacharunderscore}inc\ k\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ k\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline |
875 |
\isanewline |
|
876 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
877 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
878 |
\begin{isamarkuptext}% |
|
879 |
will fail: \isa{int} in HOL is implemented using a quotient |
|
880 |
type, which does not provide any notion of executability. |
|
881 |
\footnote{Eventually, we also want to provide executability |
|
882 |
for quotients.}. However, we could use the SML builtin |
|
883 |
integers:% |
|
21172 | 884 |
\end{isamarkuptext}% |
885 |
\isamarkuptrue% |
|
21348 | 886 |
% |
887 |
\isadelimtt |
|
888 |
% |
|
889 |
\endisadelimtt |
|
890 |
% |
|
891 |
\isatagtt |
|
21172 | 892 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
893 |
\ int\isanewline |
|
894 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}IntInf{\isachardot}int{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
895 |
\isanewline |
|
896 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
897 |
\ {\isachardoublequoteopen}op\ {\isacharplus}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline |
|
898 |
\ \ \ \ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isacharasterisk}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline |
|
21348 | 899 |
\ \ {\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}% |
900 |
\endisatagtt |
|
901 |
{\isafoldtt}% |
|
902 |
% |
|
903 |
\isadelimtt |
|
904 |
% |
|
905 |
\endisadelimtt |
|
906 |
\isanewline |
|
21186 | 907 |
\isanewline |
908 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
909 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
910 |
\begin{isamarkuptext}% |
|
911 |
resulting in: |
|
912 |
||
913 |
\lstsml{Thy/examples/integers.ML}% |
|
914 |
\end{isamarkuptext}% |
|
915 |
\isamarkuptrue% |
|
916 |
% |
|
917 |
\begin{isamarkuptext}% |
|
918 |
These examples give a glimpse what powerful mechanisms |
|
919 |
custom serializations provide; however their usage |
|
920 |
requires careful thinking in order not to introduce |
|
921 |
inconsistencies -- or, in other words: |
|
922 |
custom serializations are completely axiomatic. |
|
923 |
||
924 |
A further noteworthy details is that any special |
|
925 |
character in a custom serialization may be quoted |
|
21348 | 926 |
using ``\verb|'|''; thus, in |
927 |
``\verb|fn '_ => _|'' the first |
|
928 |
``\verb|_|'' is a proper underscore while the |
|
929 |
second ``\verb|_|'' is a placeholder. |
|
21186 | 930 |
|
931 |
The HOL theories provide further |
|
932 |
examples for custom serializations and form |
|
933 |
a recommended tutorial on how to use them properly.% |
|
934 |
\end{isamarkuptext}% |
|
21172 | 935 |
\isamarkuptrue% |
936 |
% |
|
937 |
\isamarkupsubsection{Concerning operational equality% |
|
20967 | 938 |
} |
939 |
\isamarkuptrue% |
|
940 |
% |
|
21172 | 941 |
\begin{isamarkuptext}% |
942 |
Surely you have already noticed how equality is treated |
|
943 |
by the code generator:% |
|
944 |
\end{isamarkuptext}% |
|
945 |
\isamarkuptrue% |
|
946 |
\isacommand{fun}\isamarkupfalse% |
|
947 |
\isanewline |
|
948 |
\ \ 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 |
|
949 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline |
|
950 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline |
|
951 |
\ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline |
|
952 |
\ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline |
|
953 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline |
|
21348 | 954 |
\ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}% |
21172 | 955 |
\begin{isamarkuptext}% |
21348 | 956 |
The membership test during preprocessing is rewritten, |
21172 | 957 |
resulting in \isa{op\ mem}, which itself |
958 |
performs an explicit equality check.% |
|
959 |
\end{isamarkuptext}% |
|
960 |
\isamarkuptrue% |
|
961 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
962 |
\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
963 |
\begin{isamarkuptext}% |
|
964 |
\lstsml{Thy/examples/collect_duplicates.ML}% |
|
965 |
\end{isamarkuptext}% |
|
966 |
\isamarkuptrue% |
|
967 |
% |
|
968 |
\begin{isamarkuptext}% |
|
969 |
Obviously, polymorphic equality is implemented the Haskell |
|
970 |
way using a type class. How is this achieved? By an |
|
971 |
almost trivial definition in the HOL setup:% |
|
972 |
\end{isamarkuptext}% |
|
973 |
\isamarkuptrue% |
|
974 |
% |
|
975 |
\isadelimML |
|
976 |
% |
|
977 |
\endisadelimML |
|
978 |
% |
|
979 |
\isatagML |
|
980 |
% |
|
981 |
\endisatagML |
|
982 |
{\isafoldML}% |
|
983 |
% |
|
984 |
\isadelimML |
|
985 |
% |
|
986 |
\endisadelimML |
|
987 |
\isanewline |
|
21452 | 988 |
\isacommand{axclass}\isamarkupfalse% |
989 |
\ eq\ {\isasymsubseteq}\ type\isanewline |
|
21993 | 990 |
\ \ {\isacharparenleft}\isakeyword{attach}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
21172 | 991 |
\begin{isamarkuptext}% |
992 |
This merely introduces a class \isa{eq} with corresponding |
|
21994 | 993 |
operation \isa{op\ {\isacharequal}}; |
21452 | 994 |
the preprocessing framework does the rest.% |
21172 | 995 |
\end{isamarkuptext}% |
996 |
\isamarkuptrue% |
|
997 |
% |
|
998 |
\isadelimML |
|
999 |
% |
|
1000 |
\endisadelimML |
|
1001 |
% |
|
1002 |
\isatagML |
|
1003 |
% |
|
1004 |
\endisatagML |
|
1005 |
{\isafoldML}% |
|
1006 |
% |
|
1007 |
\isadelimML |
|
1008 |
% |
|
1009 |
\endisadelimML |
|
1010 |
% |
|
1011 |
\begin{isamarkuptext}% |
|
1012 |
For datatypes, instances of \isa{eq} are implicitly derived |
|
1013 |
when possible. |
|
1014 |
||
1015 |
Though this class is designed to get rarely in the way, there |
|
1016 |
are some cases when it suddenly comes to surface:% |
|
1017 |
\end{isamarkuptext}% |
|
1018 |
\isamarkuptrue% |
|
1019 |
% |
|
21348 | 1020 |
\isamarkupsubsubsection{typedecls interpreted by customary serializations% |
21186 | 1021 |
} |
1022 |
\isamarkuptrue% |
|
1023 |
% |
|
1024 |
\begin{isamarkuptext}% |
|
1025 |
A common idiom is to use unspecified types for formalizations |
|
1026 |
and interpret them for a specific target language:% |
|
1027 |
\end{isamarkuptext}% |
|
1028 |
\isamarkuptrue% |
|
21172 | 1029 |
\isacommand{typedecl}\isamarkupfalse% |
1030 |
\ key\isanewline |
|
1031 |
\isanewline |
|
1032 |
\isacommand{fun}\isamarkupfalse% |
|
1033 |
\isanewline |
|
1034 |
\ \ lookup\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}key\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
1035 |
\ \ {\isachardoublequoteopen}lookup\ {\isacharbrackleft}{\isacharbrackright}\ l\ {\isacharequal}\ None{\isachardoublequoteclose}\isanewline |
|
1036 |
\ \ {\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 |
|
1037 |
% |
|
21348 | 1038 |
\isadelimtt |
21172 | 1039 |
% |
21348 | 1040 |
\endisadelimtt |
21172 | 1041 |
% |
21348 | 1042 |
\isatagtt |
21172 | 1043 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1044 |
\ key\isanewline |
|
1045 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}string{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1046 |
\endisatagtt |
1047 |
{\isafoldtt}% |
|
1048 |
% |
|
1049 |
\isadelimtt |
|
1050 |
% |
|
1051 |
\endisadelimtt |
|
1052 |
% |
|
21186 | 1053 |
\begin{isamarkuptext}% |
1054 |
This, though, is not sufficient: \isa{key} is no instance |
|
21172 | 1055 |
of \isa{eq} since \isa{key} is no datatype; the instance |
1056 |
has to be declared manually, including a serialization |
|
21452 | 1057 |
for the particular instance of \isa{op\ {\isacharequal}}:% |
21186 | 1058 |
\end{isamarkuptext}% |
1059 |
\isamarkuptrue% |
|
21172 | 1060 |
\isacommand{instance}\isamarkupfalse% |
1061 |
\ key\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1062 |
\isadelimproof |
|
1063 |
\ % |
|
1064 |
\endisadelimproof |
|
1065 |
% |
|
1066 |
\isatagproof |
|
1067 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1068 |
% |
|
1069 |
\endisatagproof |
|
1070 |
{\isafoldproof}% |
|
1071 |
% |
|
1072 |
\isadelimproof |
|
1073 |
% |
|
1074 |
\endisadelimproof |
|
1075 |
\isanewline |
|
21348 | 1076 |
% |
1077 |
\isadelimtt |
|
21172 | 1078 |
\isanewline |
21348 | 1079 |
% |
1080 |
\endisadelimtt |
|
1081 |
% |
|
1082 |
\isatagtt |
|
21172 | 1083 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
21452 | 1084 |
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isasymColon}\ key\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline |
1085 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}\ {\isacharcolon}\ string{\isacharparenright}\ {\isacharequal}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1086 |
\endisatagtt |
1087 |
{\isafoldtt}% |
|
1088 |
% |
|
1089 |
\isadelimtt |
|
1090 |
% |
|
1091 |
\endisadelimtt |
|
1092 |
% |
|
21186 | 1093 |
\begin{isamarkuptext}% |
1094 |
Then everything goes fine:% |
|
1095 |
\end{isamarkuptext}% |
|
1096 |
\isamarkuptrue% |
|
21172 | 1097 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
1098 |
\ lookup\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lookup{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1099 |
\begin{isamarkuptext}% |
|
1100 |
\lstsml{Thy/examples/lookup.ML}% |
|
1101 |
\end{isamarkuptext}% |
|
1102 |
\isamarkuptrue% |
|
1103 |
% |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1104 |
\isamarkupsubsubsection{lexicographic orderings% |
21186 | 1105 |
} |
1106 |
\isamarkuptrue% |
|
1107 |
% |
|
1108 |
\begin{isamarkuptext}% |
|
1109 |
Another subtlety |
|
21172 | 1110 |
enters the stage when definitions of overloaded constants |
1111 |
are dependent on operational equality. For example, let |
|
21348 | 1112 |
us define a lexicographic ordering on tuples:% |
21186 | 1113 |
\end{isamarkuptext}% |
1114 |
\isamarkuptrue% |
|
1115 |
\isacommand{instance}\isamarkupfalse% |
|
1116 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline |
|
1117 |
\ \ {\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 |
|
1118 |
\ \ \ \ 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
|
1119 |
\ \ {\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
|
1120 |
\ \ \ \ 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 | 1121 |
\isadelimproof |
1122 |
\ % |
|
1123 |
\endisadelimproof |
|
1124 |
% |
|
1125 |
\isatagproof |
|
1126 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1127 |
% |
|
1128 |
\endisatagproof |
|
1129 |
{\isafoldproof}% |
|
1130 |
% |
|
1131 |
\isadelimproof |
|
1132 |
% |
|
1133 |
\endisadelimproof |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1134 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1135 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1136 |
\isacommand{lemma}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1137 |
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1138 |
\ \ {\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
|
1139 |
\ \ {\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
|
1140 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1141 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1142 |
\ \ % |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1143 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1144 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1145 |
\isatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1146 |
\isacommand{unfolding}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1147 |
\ {\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
|
1148 |
\ simp{\isacharunderscore}all% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1149 |
\endisatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1150 |
{\isafoldproof}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1151 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1152 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1153 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1154 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1155 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1156 |
\begin{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1157 |
Then code generation will fail. Why? The definition |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1158 |
of \isa{op\ {\isasymle}} depends on equality on both arguments, |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1159 |
which are polymorphic and impose an additional \isa{eq} |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1160 |
class constraint, thus violating the type discipline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1161 |
for class operations. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1162 |
|
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1163 |
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
|
1164 |
code theorems:% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1165 |
\end{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1166 |
\isamarkuptrue% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1167 |
\isanewline |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1168 |
\isacommand{lemma}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1169 |
\ 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
|
1170 |
\ \ {\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
|
1171 |
\ \ {\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
|
1172 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1173 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1174 |
\ \ % |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1175 |
\endisadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1176 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1177 |
\isatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1178 |
\isacommand{unfolding}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1179 |
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1180 |
\ rule{\isacharplus}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1181 |
\endisatagproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1182 |
{\isafoldproof}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1183 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1184 |
\isadelimproof |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1185 |
% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1186 |
\endisadelimproof |
21172 | 1187 |
% |
21186 | 1188 |
\begin{isamarkuptext}% |
1189 |
Then code generation succeeds:% |
|
1190 |
\end{isamarkuptext}% |
|
1191 |
\isamarkuptrue% |
|
21172 | 1192 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1193 |
\ {\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 | 1194 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
1195 |
\begin{isamarkuptext}% |
|
1196 |
\lstsml{Thy/examples/lexicographic.ML}% |
|
1197 |
\end{isamarkuptext}% |
|
1198 |
\isamarkuptrue% |
|
1199 |
% |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1200 |
\begin{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1201 |
In general, code theorems for overloaded constants may have more |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1202 |
restrictive sort constraints than the underlying instance relation |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1203 |
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
|
1204 |
constraints is coregular; code theorems violating coregularity |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1205 |
are rejected immediately.% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1206 |
\end{isamarkuptext}% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1207 |
\isamarkuptrue% |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1208 |
% |
21186 | 1209 |
\isamarkupsubsubsection{Haskell serialization% |
1210 |
} |
|
1211 |
\isamarkuptrue% |
|
1212 |
% |
|
1213 |
\begin{isamarkuptext}% |
|
1214 |
For convenience, the default |
|
1215 |
HOL setup for Haskell maps the \isa{eq} class to |
|
1216 |
its counterpart in Haskell, giving custom serializations |
|
21348 | 1217 |
for the class (\isa{{\isasymCODECLASS}}) and its operation:% |
21186 | 1218 |
\end{isamarkuptext}% |
1219 |
\isamarkuptrue% |
|
1220 |
% |
|
1221 |
\isadelimML |
|
1222 |
% |
|
1223 |
\endisadelimML |
|
1224 |
% |
|
1225 |
\isatagML |
|
1226 |
% |
|
1227 |
\endisatagML |
|
1228 |
{\isafoldML}% |
|
1229 |
% |
|
1230 |
\isadelimML |
|
1231 |
% |
|
1232 |
\endisadelimML |
|
1233 |
\isanewline |
|
21348 | 1234 |
% |
1235 |
\isadelimtt |
|
1236 |
% |
|
1237 |
\endisadelimtt |
|
1238 |
% |
|
1239 |
\isatagtt |
|
21186 | 1240 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
1241 |
\ eq\isanewline |
|
1242 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ eq\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1243 |
\isanewline |
|
1244 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1245 |
\ eq\isanewline |
|
1246 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1247 |
% |
|
21348 | 1248 |
\endisatagtt |
1249 |
{\isafoldtt}% |
|
1250 |
% |
|
1251 |
\isadelimtt |
|
1252 |
% |
|
1253 |
\endisadelimtt |
|
1254 |
% |
|
21186 | 1255 |
\isadelimML |
1256 |
% |
|
1257 |
\endisadelimML |
|
1258 |
% |
|
1259 |
\isatagML |
|
1260 |
% |
|
1261 |
\endisatagML |
|
1262 |
{\isafoldML}% |
|
1263 |
% |
|
1264 |
\isadelimML |
|
1265 |
% |
|
1266 |
\endisadelimML |
|
21172 | 1267 |
% |
21186 | 1268 |
\begin{isamarkuptext}% |
1269 |
A problem now occurs whenever a type which |
|
1270 |
is an instance of \isa{eq} in HOL is mapped |
|
1271 |
on a Haskell-builtin type which is also an instance |
|
1272 |
of Haskell \isa{Eq}:% |
|
1273 |
\end{isamarkuptext}% |
|
1274 |
\isamarkuptrue% |
|
1275 |
\isacommand{typedecl}\isamarkupfalse% |
|
1276 |
\ bar\isanewline |
|
1277 |
\isanewline |
|
1278 |
\isacommand{instance}\isamarkupfalse% |
|
1279 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1280 |
\isadelimproof |
|
1281 |
\ % |
|
1282 |
\endisadelimproof |
|
1283 |
% |
|
1284 |
\isatagproof |
|
1285 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1286 |
% |
|
1287 |
\endisatagproof |
|
1288 |
{\isafoldproof}% |
|
1289 |
% |
|
1290 |
\isadelimproof |
|
21172 | 1291 |
% |
21186 | 1292 |
\endisadelimproof |
1293 |
\isanewline |
|
21348 | 1294 |
% |
1295 |
\isadelimtt |
|
21186 | 1296 |
\isanewline |
21348 | 1297 |
% |
1298 |
\endisadelimtt |
|
1299 |
% |
|
1300 |
\isatagtt |
|
21186 | 1301 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1302 |
\ bar\isanewline |
|
1303 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1304 |
\endisatagtt |
1305 |
{\isafoldtt}% |
|
1306 |
% |
|
1307 |
\isadelimtt |
|
1308 |
% |
|
1309 |
\endisadelimtt |
|
1310 |
% |
|
21186 | 1311 |
\begin{isamarkuptext}% |
1312 |
The code generator would produce |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1313 |
an additional instance, which of course is rejected. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1314 |
To suppress this additional instance, use |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1315 |
\isa{{\isasymCODEINSTANCE}}:% |
21186 | 1316 |
\end{isamarkuptext}% |
1317 |
\isamarkuptrue% |
|
21348 | 1318 |
% |
1319 |
\isadelimtt |
|
1320 |
% |
|
1321 |
\endisadelimtt |
|
1322 |
% |
|
1323 |
\isatagtt |
|
21186 | 1324 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
1325 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
1326 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
|
21348 | 1327 |
\endisatagtt |
1328 |
{\isafoldtt}% |
|
1329 |
% |
|
1330 |
\isadelimtt |
|
1331 |
% |
|
1332 |
\endisadelimtt |
|
1333 |
% |
|
21186 | 1334 |
\isamarkupsubsection{Types matter% |
1335 |
} |
|
1336 |
\isamarkuptrue% |
|
1337 |
% |
|
1338 |
\begin{isamarkuptext}% |
|
1339 |
Imagine the following quick-and-dirty setup for implementing |
|
21348 | 1340 |
some kind of sets as lists in SML:% |
21186 | 1341 |
\end{isamarkuptext}% |
1342 |
\isamarkuptrue% |
|
21348 | 1343 |
% |
1344 |
\isadelimtt |
|
1345 |
% |
|
1346 |
\endisadelimtt |
|
1347 |
% |
|
1348 |
\isatagtt |
|
21186 | 1349 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1350 |
\ set\isanewline |
|
1351 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharunderscore}\ list{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1352 |
\isanewline |
|
1353 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1354 |
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}\ \isakeyword{and}\ insert\isanewline |
|
21348 | 1355 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ \isakeyword{and}\ \isakeyword{infixl}\ {\isadigit{7}}\ {\isachardoublequoteopen}{\isacharcolon}{\isacharcolon}{\isachardoublequoteclose}{\isacharparenright}% |
1356 |
\endisatagtt |
|
1357 |
{\isafoldtt}% |
|
1358 |
% |
|
1359 |
\isadelimtt |
|
1360 |
% |
|
1361 |
\endisadelimtt |
|
1362 |
\isanewline |
|
21186 | 1363 |
\isanewline |
1364 |
\isacommand{definition}\isamarkupfalse% |
|
1365 |
\isanewline |
|
21993 | 1366 |
\ \ dummy{\isacharunderscore}set\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymRightarrow}\ nat{\isacharparenright}\ set{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1367 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}set\ {\isacharequal}\ {\isacharbraceleft}Suc{\isacharbraceright}{\isachardoublequoteclose}% |
1368 |
\begin{isamarkuptext}% |
|
1369 |
Then code generation for \isa{dummy{\isacharunderscore}set} will fail. |
|
22060 | 1370 |
Why? A glimpse at the defining equations will offer:% |
21348 | 1371 |
\end{isamarkuptext}% |
1372 |
\isamarkuptrue% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1373 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1374 |
\ insert% |
21348 | 1375 |
\begin{isamarkuptext}% |
22060 | 1376 |
This reveals the defining equation \isa{insert\ {\isacharquery}a\ {\isacharquery}B\ {\isasymequiv}\ {\isacharbraceleft}x{\isachardot}\ x\ {\isacharequal}\ {\isacharquery}a{\isacharbraceright}\ {\isasymunion}\ {\isacharquery}B} |
21348 | 1377 |
for \isa{insert}, which is operationally meaningless |
1378 |
but forces an equality constraint on the set members |
|
1379 |
(which is not satisfiable if the set members are functions). |
|
1380 |
Even when using set of natural numbers (which are an instance |
|
1381 |
of \emph{eq}), we run into a problem:% |
|
1382 |
\end{isamarkuptext}% |
|
1383 |
\isamarkuptrue% |
|
1384 |
\isacommand{definition}\isamarkupfalse% |
|
1385 |
\isanewline |
|
21993 | 1386 |
\ \ foobar{\isacharunderscore}set\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ set{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1387 |
\ \ {\isachardoublequoteopen}foobar{\isacharunderscore}set\ {\isacharequal}\ {\isacharbraceleft}{\isadigit{0}}{\isacharcomma}\ {\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharbraceright}{\isachardoublequoteclose}% |
1388 |
\begin{isamarkuptext}% |
|
1389 |
In this case the serializer would complain that \isa{insert} |
|
1390 |
expects dictionaries (namely an \emph{eq} dictionary) but |
|
1391 |
has also been given a customary serialization. |
|
1392 |
||
1393 |
The solution to this dilemma:% |
|
1394 |
\end{isamarkuptext}% |
|
1395 |
\isamarkuptrue% |
|
1396 |
\isacommand{lemma}\isamarkupfalse% |
|
1397 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
1398 |
\ \ {\isachardoublequoteopen}insert\ {\isacharequal}\ insert{\isachardoublequoteclose}% |
|
1399 |
\isadelimproof |
|
1400 |
\ % |
|
1401 |
\endisadelimproof |
|
1402 |
% |
|
1403 |
\isatagproof |
|
1404 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1405 |
% |
|
1406 |
\endisatagproof |
|
1407 |
{\isafoldproof}% |
|
1408 |
% |
|
1409 |
\isadelimproof |
|
1410 |
% |
|
1411 |
\endisadelimproof |
|
1412 |
\isanewline |
|
1413 |
\isanewline |
|
1414 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
1415 |
\ dummy{\isacharunderscore}set\ foobar{\isacharunderscore}set\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}dirty{\isacharunderscore}set{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1416 |
\begin{isamarkuptext}% |
|
1417 |
\lstsml{Thy/examples/dirty_set.ML} |
|
1418 |
||
22060 | 1419 |
Reflexive defining equations by convention are dropped. |
21348 | 1420 |
But their presence prevents primitive definitions to be |
22060 | 1421 |
used as defining equations:% |
21348 | 1422 |
\end{isamarkuptext}% |
1423 |
\isamarkuptrue% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1424 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1425 |
\ insert% |
21348 | 1426 |
\begin{isamarkuptext}% |
22060 | 1427 |
will show \emph{no} defining equations for insert. |
21348 | 1428 |
|
1429 |
Note that the sort constraints of reflexive equations |
|
1430 |
are considered; so% |
|
1431 |
\end{isamarkuptext}% |
|
1432 |
\isamarkuptrue% |
|
1433 |
\isacommand{lemma}\isamarkupfalse% |
|
1434 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
1435 |
\ \ {\isachardoublequoteopen}{\isacharparenleft}insert\ {\isasymColon}\ {\isacharprime}a{\isasymColon}eq\ {\isasymRightarrow}\ {\isacharprime}a\ set\ {\isasymRightarrow}\ {\isacharprime}a\ set{\isacharparenright}\ {\isacharequal}\ insert{\isachardoublequoteclose}% |
|
1436 |
\isadelimproof |
|
1437 |
\ % |
|
1438 |
\endisadelimproof |
|
1439 |
% |
|
1440 |
\isatagproof |
|
1441 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1442 |
% |
|
1443 |
\endisatagproof |
|
1444 |
{\isafoldproof}% |
|
1445 |
% |
|
1446 |
\isadelimproof |
|
1447 |
% |
|
1448 |
\endisadelimproof |
|
1449 |
% |
|
1450 |
\begin{isamarkuptext}% |
|
1451 |
would mean nothing else than to introduce the evil |
|
1452 |
sort constraint by hand.% |
|
1453 |
\end{isamarkuptext}% |
|
1454 |
\isamarkuptrue% |
|
1455 |
% |
|
1456 |
\isamarkupsubsection{Cyclic module dependencies% |
|
1457 |
} |
|
1458 |
\isamarkuptrue% |
|
1459 |
% |
|
1460 |
\begin{isamarkuptext}% |
|
1461 |
Sometimes the awkward situation occurs that dependencies |
|
1462 |
between definitions introduce cyclic dependencies |
|
1463 |
between modules, which in the Haskell world leaves |
|
1464 |
you to the mercy of the Haskell implementation you are using, |
|
1465 |
while for SML code generation is not possible. |
|
1466 |
||
1467 |
A solution is to declare module names explicitly. |
|
1468 |
Let use assume the three cyclically dependent |
|
1469 |
modules are named \emph{A}, \emph{B} and \emph{C}. |
|
1470 |
Then, by stating% |
|
1471 |
\end{isamarkuptext}% |
|
1472 |
\isamarkuptrue% |
|
1473 |
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse% |
|
1474 |
\ SML\isanewline |
|
1475 |
\ \ A\ ABC\isanewline |
|
1476 |
\ \ B\ ABC\isanewline |
|
1477 |
\ \ C\ ABC% |
|
1478 |
\begin{isamarkuptext}% |
|
1479 |
we explicitly map all those modules on \emph{ABC}, |
|
1480 |
resulting in an ad-hoc merge of this three modules |
|
1481 |
at serialization time.% |
|
1482 |
\end{isamarkuptext}% |
|
1483 |
\isamarkuptrue% |
|
1484 |
% |
|
21172 | 1485 |
\isamarkupsubsection{Axiomatic extensions% |
20967 | 1486 |
} |
1487 |
\isamarkuptrue% |
|
1488 |
% |
|
21172 | 1489 |
\begin{isamarkuptext}% |
1490 |
\begin{warn} |
|
1491 |
The extensions introduced in this section, though working |
|
21348 | 1492 |
in practice, are not the cream of the crop, as you |
1493 |
will notice during reading. They will |
|
21172 | 1494 |
eventually be replaced by more mature approaches. |
21348 | 1495 |
\end{warn} |
1496 |
||
1497 |
Sometimes equalities are taken for granted which are |
|
1498 |
not derivable inside the HOL logic but are silently assumed |
|
1499 |
to hold for executable code. For example, we may want |
|
1500 |
to identify the famous HOL constant \isa{arbitrary} |
|
1501 |
of type \isa{{\isacharprime}a\ option} with \isa{None}. |
|
1502 |
By brute force:% |
|
1503 |
\end{isamarkuptext}% |
|
1504 |
\isamarkuptrue% |
|
1505 |
\isacommand{axiomatization}\isamarkupfalse% |
|
1506 |
\ \isakeyword{where}\isanewline |
|
1507 |
\ \ {\isachardoublequoteopen}arbitrary\ {\isacharequal}\ None{\isachardoublequoteclose}% |
|
1508 |
\begin{isamarkuptext}% |
|
1509 |
However this has to be considered harmful since this axiom, |
|
1510 |
though probably justifiable for generated code, could |
|
1511 |
introduce serious inconsistencies into the logic. |
|
1512 |
||
1513 |
So, there is a distinguished construct for stating axiomatic |
|
1514 |
equalities of constants which apply only for code generation. |
|
1515 |
Before introducing this, here is a convenient place to describe |
|
1516 |
shortly how to deal with some restrictions the type discipline |
|
1517 |
imposes. |
|
1518 |
||
1519 |
By itself, the constant \isa{arbitrary} is a non-overloaded |
|
1520 |
polymorphic constant. So, there is no way to distinguish |
|
1521 |
different versions of \isa{arbitrary} for different types |
|
1522 |
inside the code generator framework. However, inlining |
|
1523 |
theorems together with auxiliary constants provide a solution:% |
|
1524 |
\end{isamarkuptext}% |
|
1525 |
\isamarkuptrue% |
|
1526 |
\isacommand{definition}\isamarkupfalse% |
|
1527 |
\isanewline |
|
21993 | 1528 |
\ \ arbitrary{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
21348 | 1529 |
\ \ {\isacharbrackleft}symmetric{\isacharcomma}\ code\ inline{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}arbitrary{\isacharunderscore}option\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}% |
1530 |
\begin{isamarkuptext}% |
|
1531 |
By that, we replace any \isa{arbitrary} with option type |
|
22060 | 1532 |
by \isa{arbitrary{\isacharunderscore}option} in defining equations. |
21348 | 1533 |
|
1534 |
For technical reasons, we further have to provide a |
|
1535 |
synonym for \isa{None} which in code generator view |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1536 |
is a function rather than a term constructor:% |
21348 | 1537 |
\end{isamarkuptext}% |
1538 |
\isamarkuptrue% |
|
1539 |
\isacommand{definition}\isamarkupfalse% |
|
1540 |
\isanewline |
|
1541 |
\ \ {\isachardoublequoteopen}None{\isacharprime}\ {\isacharequal}\ None{\isachardoublequoteclose}% |
|
1542 |
\begin{isamarkuptext}% |
|
1543 |
Then finally we are enabled to use \isa{{\isasymCODEAXIOMS}}:% |
|
1544 |
\end{isamarkuptext}% |
|
1545 |
\isamarkuptrue% |
|
1546 |
\isacommand{code{\isacharunderscore}axioms}\isamarkupfalse% |
|
1547 |
\isanewline |
|
1548 |
\ \ arbitrary{\isacharunderscore}option\ {\isasymequiv}\ None{\isacharprime}% |
|
1549 |
\begin{isamarkuptext}% |
|
1550 |
A dummy example:% |
|
1551 |
\end{isamarkuptext}% |
|
1552 |
\isamarkuptrue% |
|
1553 |
\isacommand{fun}\isamarkupfalse% |
|
1554 |
\isanewline |
|
1555 |
\ \ dummy{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
1556 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ Some\ x{\isachardoublequoteclose}\isanewline |
|
1557 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}\isanewline |
|
1558 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
1559 |
\ dummy{\isacharunderscore}option\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}arbitrary{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}% |
|
1560 |
\begin{isamarkuptext}% |
|
1561 |
\lstsml{Thy/examples/arbitrary.ML} |
|
1562 |
||
1563 |
Another axiomatic extension is code generation |
|
1564 |
for abstracted types. For this, the |
|
21452 | 1565 |
\isa{ExecutableRat} (see \secref{exec_rat}) |
21348 | 1566 |
forms a good example.% |
21172 | 1567 |
\end{isamarkuptext}% |
1568 |
\isamarkuptrue% |
|
1569 |
% |
|
1570 |
\isamarkupsection{ML interfaces \label{sec:ml}% |
|
1571 |
} |
|
1572 |
\isamarkuptrue% |
|
1573 |
% |
|
21348 | 1574 |
\begin{isamarkuptext}% |
1575 |
Since the code generator framework not only aims to provide |
|
1576 |
a nice Isar interface but also to form a base for |
|
1577 |
code-generation-based applications, here a short |
|
1578 |
description of the most important ML interfaces.% |
|
1579 |
\end{isamarkuptext}% |
|
1580 |
\isamarkuptrue% |
|
1581 |
% |
|
21172 | 1582 |
\isamarkupsubsection{Constants with type discipline: codegen\_consts.ML% |
1583 |
} |
|
1584 |
\isamarkuptrue% |
|
1585 |
% |
|
21348 | 1586 |
\begin{isamarkuptext}% |
1587 |
This Pure module manages identification of (probably overloaded) |
|
1588 |
constants by unique identifiers.% |
|
1589 |
\end{isamarkuptext}% |
|
1590 |
\isamarkuptrue% |
|
1591 |
% |
|
21172 | 1592 |
\isadelimmlref |
1593 |
% |
|
1594 |
\endisadelimmlref |
|
1595 |
% |
|
1596 |
\isatagmlref |
|
1597 |
% |
|
1598 |
\begin{isamarkuptext}% |
|
1599 |
\begin{mldecls} |
|
21348 | 1600 |
\indexmltype{CodegenConsts.const}\verb|type CodegenConsts.const = string * typ list| \\ |
1601 |
\indexml{CodegenConsts.norm-of-typ}\verb|CodegenConsts.norm_of_typ: theory -> string * typ -> CodegenConsts.const| \\ |
|
21172 | 1602 |
\indexml{CodegenConsts.typ-of-inst}\verb|CodegenConsts.typ_of_inst: theory -> CodegenConsts.const -> string * typ| \\ |
21348 | 1603 |
\end{mldecls} |
1604 |
||
1605 |
\begin{description} |
|
1606 |
||
1607 |
\item \verb|CodegenConsts.const| is the identifier type: |
|
1608 |
the product of a \emph{string} with a list of \emph{typs}. |
|
1609 |
The \emph{string} is the constant name as represented inside Isabelle; |
|
1610 |
the \emph{typs} are a type instantiation in the sense of System F, |
|
1611 |
with canonical names for type variables. |
|
1612 |
||
1613 |
\item \verb|CodegenConsts.norm_of_typ|~\isa{thy}~\isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} |
|
1614 |
maps a constant expression \isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} to its canonical identifier. |
|
1615 |
||
1616 |
\item \verb|CodegenConsts.typ_of_inst|~\isa{thy}~\isa{const} |
|
1617 |
maps a canonical identifier \isa{const} to a constant |
|
1618 |
expression with appropriate type. |
|
1619 |
||
1620 |
\end{description}% |
|
21172 | 1621 |
\end{isamarkuptext}% |
1622 |
\isamarkuptrue% |
|
1623 |
% |
|
1624 |
\endisatagmlref |
|
1625 |
{\isafoldmlref}% |
|
1626 |
% |
|
1627 |
\isadelimmlref |
|
1628 |
% |
|
1629 |
\endisadelimmlref |
|
1630 |
% |
|
1631 |
\isamarkupsubsection{Executable theory content: codegen\_data.ML% |
|
1632 |
} |
|
1633 |
\isamarkuptrue% |
|
1634 |
% |
|
1635 |
\begin{isamarkuptext}% |
|
1636 |
This Pure module implements the core notions of |
|
1637 |
executable content of a theory.% |
|
1638 |
\end{isamarkuptext}% |
|
1639 |
\isamarkuptrue% |
|
1640 |
% |
|
1641 |
\isamarkupsubsubsection{Suspended theorems% |
|
1642 |
} |
|
1643 |
\isamarkuptrue% |
|
1644 |
% |
|
1645 |
\isadelimmlref |
|
1646 |
% |
|
1647 |
\endisadelimmlref |
|
1648 |
% |
|
1649 |
\isatagmlref |
|
1650 |
% |
|
1651 |
\begin{isamarkuptext}% |
|
1652 |
\begin{mldecls} |
|
21348 | 1653 |
\indexml{CodegenData.lazy}\verb|CodegenData.lazy: (unit -> thm list) -> thm list Susp.T| |
1654 |
\end{mldecls} |
|
1655 |
||
1656 |
\begin{description} |
|
1657 |
||
1658 |
\item \verb|CodegenData.lazy|~\isa{f} turns an abstract |
|
1659 |
theorem computation \isa{f} into a suspension of theorems. |
|
1660 |
||
1661 |
\end{description}% |
|
21172 | 1662 |
\end{isamarkuptext}% |
1663 |
\isamarkuptrue% |
|
1664 |
% |
|
1665 |
\endisatagmlref |
|
1666 |
{\isafoldmlref}% |
|
1667 |
% |
|
1668 |
\isadelimmlref |
|
1669 |
% |
|
1670 |
\endisadelimmlref |
|
1671 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1672 |
\isamarkupsubsubsection{Managing executable content% |
20967 | 1673 |
} |
1674 |
\isamarkuptrue% |
|
1675 |
% |
|
21172 | 1676 |
\isadelimmlref |
1677 |
% |
|
1678 |
\endisadelimmlref |
|
1679 |
% |
|
1680 |
\isatagmlref |
|
1681 |
% |
|
1682 |
\begin{isamarkuptext}% |
|
1683 |
\begin{mldecls} |
|
1684 |
\indexml{CodegenData.add-func}\verb|CodegenData.add_func: thm -> theory -> theory| \\ |
|
1685 |
\indexml{CodegenData.del-func}\verb|CodegenData.del_func: thm -> theory -> theory| \\ |
|
21348 | 1686 |
\indexml{CodegenData.add-funcl}\verb|CodegenData.add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory| \\ |
21172 | 1687 |
\indexml{CodegenData.add-inline}\verb|CodegenData.add_inline: thm -> theory -> theory| \\ |
1688 |
\indexml{CodegenData.del-inline}\verb|CodegenData.del_inline: thm -> theory -> theory| \\ |
|
22060 | 1689 |
\indexml{CodegenData.add-inline-proc}\verb|CodegenData.add_inline_proc: string * (theory -> cterm list -> thm list)|\isasep\isanewline% |
21348 | 1690 |
\verb| -> theory -> theory| \\ |
22060 | 1691 |
\indexml{CodegenData.del-inline-proc}\verb|CodegenData.del_inline_proc: string -> theory -> theory| \\ |
1692 |
\indexml{CodegenData.add-preproc}\verb|CodegenData.add_preproc: string * (theory -> thm list -> thm list)|\isasep\isanewline% |
|
21348 | 1693 |
\verb| -> theory -> theory| \\ |
22060 | 1694 |
\indexml{CodegenData.del-preproc}\verb|CodegenData.del_preproc: string -> theory -> theory| \\ |
21348 | 1695 |
\indexml{CodegenData.add-datatype}\verb|CodegenData.add_datatype: string * (((string * sort) list * (string * typ list) list)|\isasep\isanewline% |
1696 |
\verb| * thm list Susp.T) -> theory -> theory| \\ |
|
1697 |
\indexml{CodegenData.del-datatype}\verb|CodegenData.del_datatype: string -> theory -> theory| \\ |
|
1698 |
\indexml{CodegenData.get-datatype}\verb|CodegenData.get_datatype: theory -> string|\isasep\isanewline% |
|
1699 |
\verb| -> ((string * sort) list * (string * typ list) list) option| \\ |
|
21172 | 1700 |
\indexml{CodegenData.get-datatype-of-constr}\verb|CodegenData.get_datatype_of_constr: theory -> CodegenConsts.const -> string option| |
1701 |
\end{mldecls} |
|
1702 |
||
1703 |
\begin{description} |
|
1704 |
||
21348 | 1705 |
\item \verb|CodegenData.add_func|~\isa{thm}~\isa{thy} adds function |
1706 |
theorem \isa{thm} to executable content. |
|
1707 |
||
1708 |
\item \verb|CodegenData.del_func|~\isa{thm}~\isa{thy} removes function |
|
1709 |
theorem \isa{thm} from executable content, if present. |
|
1710 |
||
1711 |
\item \verb|CodegenData.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds |
|
22060 | 1712 |
suspended defining equations \isa{lthms} for constant |
21348 | 1713 |
\isa{const} to executable content. |
1714 |
||
1715 |
\item \verb|CodegenData.add_inline|~\isa{thm}~\isa{thy} adds |
|
1716 |
inlining theorem \isa{thm} to executable content. |
|
1717 |
||
1718 |
\item \verb|CodegenData.del_inline|~\isa{thm}~\isa{thy} remove |
|
1719 |
inlining theorem \isa{thm} from executable content, if present. |
|
1720 |
||
22060 | 1721 |
\item \verb|CodegenData.add_inline_proc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
1722 |
inline procedure \isa{f} (named \isa{name}) to executable content; |
|
21348 | 1723 |
\isa{f} is a computation of rewrite rules dependent on |
1724 |
the current theory context and the list of all arguments |
|
22060 | 1725 |
and right hand sides of the defining equations belonging |
21348 | 1726 |
to a certain function definition. |
1727 |
||
22060 | 1728 |
\item \verb|CodegenData.del_inline_proc|~\isa{name}~\isa{thy} removes |
1729 |
inline procedure named \isa{name} from executable content. |
|
1730 |
||
1731 |
\item \verb|CodegenData.add_preproc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
|
1732 |
generic preprocessor \isa{f} (named \isa{name}) to executable content; |
|
1733 |
\isa{f} is a transformation of the defining equations belonging |
|
21348 | 1734 |
to a certain function definition, depending on the |
1735 |
current theory context. |
|
1736 |
||
22060 | 1737 |
\item \verb|CodegenData.del_preproc|~\isa{name}~\isa{thy} removes |
1738 |
generic preprcoessor named \isa{name} from executable content. |
|
1739 |
||
21348 | 1740 |
\item \verb|CodegenData.add_datatype|~\isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}spec{\isacharcomma}\ cert{\isacharparenright}{\isacharparenright}}~\isa{thy} adds |
1741 |
a datatype to executable content, with type constructor |
|
1742 |
\isa{name} and specification \isa{spec}; \isa{spec} is |
|
1743 |
a pair consisting of a list of type variable with sort |
|
1744 |
constraints and a list of constructors with name |
|
1745 |
and types of arguments. The addition as datatype |
|
1746 |
has to be justified giving a certificate of suspended |
|
1747 |
theorems as witnesses for injectiveness and distinctness. |
|
1748 |
||
1749 |
\item \verb|CodegenData.del_datatype|~\isa{name}~\isa{thy} |
|
1750 |
remove a datatype from executable content, if present. |
|
1751 |
||
1752 |
\item \verb|CodegenData.get_datatype_of_constr|~\isa{thy}~\isa{const} |
|
1753 |
returns type constructor corresponding to |
|
1754 |
constructor \isa{const}; returns \isa{NONE} |
|
1755 |
if \isa{const} is no constructor. |
|
1756 |
||
1757 |
\end{description}% |
|
1758 |
\end{isamarkuptext}% |
|
1759 |
\isamarkuptrue% |
|
1760 |
% |
|
1761 |
\endisatagmlref |
|
1762 |
{\isafoldmlref}% |
|
1763 |
% |
|
1764 |
\isadelimmlref |
|
1765 |
% |
|
1766 |
\endisadelimmlref |
|
1767 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1768 |
\isamarkupsubsection{Auxiliary% |
21172 | 1769 |
} |
1770 |
\isamarkuptrue% |
|
1771 |
% |
|
1772 |
\isadelimmlref |
|
1773 |
% |
|
1774 |
\endisadelimmlref |
|
1775 |
% |
|
1776 |
\isatagmlref |
|
1777 |
% |
|
1778 |
\begin{isamarkuptext}% |
|
1779 |
\begin{mldecls} |
|
1780 |
\indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\ |
|
1781 |
\indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\ |
|
1782 |
\indexml{CodegenConsts.consts-of}\verb|CodegenConsts.consts_of: theory -> term -> CodegenConsts.const list| \\ |
|
1783 |
\indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\ |
|
1784 |
\indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\ |
|
22060 | 1785 |
\indexml{CodegenFunc.typ-func}\verb|CodegenFunc.typ_func: thm -> typ| \\ |
1786 |
\indexml{CodegenFunc.rewrite-func}\verb|CodegenFunc.rewrite_func: thm list -> thm -> thm| \\ |
|
21348 | 1787 |
\end{mldecls} |
1788 |
||
1789 |
\begin{description} |
|
1790 |
||
1791 |
\item \verb|CodegenConsts.const_ord|,~\verb|CodegenConsts.eq_const| |
|
1792 |
provide order and equality on constant identifiers. |
|
1793 |
||
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1794 |
\item \verb|CodegenConsts.Consttab| |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1795 |
provides table structures with constant identifiers as keys. |
21348 | 1796 |
|
1797 |
\item \verb|CodegenConsts.consts_of|~\isa{thy}~\isa{t} |
|
1798 |
returns all constant identifiers mentioned in a term \isa{t}. |
|
1799 |
||
1800 |
\item \verb|CodegenConsts.read_const|~\isa{thy}~\isa{s} |
|
1801 |
reads a constant as a concrete term expression \isa{s}. |
|
1802 |
||
22060 | 1803 |
\item \verb|CodegenFunc.typ_func|~\isa{thm} |
1804 |
extracts the type of a constant in a defining equation \isa{thm}. |
|
21348 | 1805 |
|
22060 | 1806 |
\item \verb|CodegenFunc.rewrite_func|~\isa{rews}~\isa{thm} |
1807 |
rewrites a defining equation \isa{thm} with a set of rewrite |
|
21348 | 1808 |
rules \isa{rews}; only arguments and right hand side are rewritten, |
22060 | 1809 |
not the head of the defining equation. |
21348 | 1810 |
|
1811 |
\end{description}% |
|
21172 | 1812 |
\end{isamarkuptext}% |
1813 |
\isamarkuptrue% |
|
1814 |
% |
|
1815 |
\endisatagmlref |
|
1816 |
{\isafoldmlref}% |
|
1817 |
% |
|
1818 |
\isadelimmlref |
|
1819 |
% |
|
1820 |
\endisadelimmlref |
|
1821 |
% |
|
20967 | 1822 |
\isamarkupsubsection{Implementing code generator applications% |
1823 |
} |
|
1824 |
\isamarkuptrue% |
|
1825 |
% |
|
21172 | 1826 |
\begin{isamarkuptext}% |
21348 | 1827 |
Implementing code generator applications on top |
1828 |
of the framework set out so far usually not only |
|
1829 |
involves using those primitive interfaces |
|
1830 |
but also storing code-dependent data and various |
|
1831 |
other things. |
|
1832 |
||
1833 |
\begin{warn} |
|
21172 | 1834 |
Some interfaces discussed here have not reached |
1835 |
a final state yet. |
|
1836 |
Changes likely to occur in future. |
|
21452 | 1837 |
\end{warn}% |
21172 | 1838 |
\end{isamarkuptext}% |
1839 |
\isamarkuptrue% |
|
1840 |
% |
|
1841 |
\isamarkupsubsubsection{Data depending on the theory's executable content% |
|
1842 |
} |
|
1843 |
\isamarkuptrue% |
|
1844 |
% |
|
21348 | 1845 |
\begin{isamarkuptext}% |
21452 | 1846 |
Due to incrementality of code generation, changes in the |
1847 |
theory's executable content have to be propagated in a |
|
1848 |
certain fashion. Additionally, such changes may occur |
|
1849 |
not only during theory extension but also during theory |
|
1850 |
merge, which is a little bit nasty from an implementation |
|
1851 |
point of view. The framework provides a solution |
|
1852 |
to this technical challenge by providing a functorial |
|
1853 |
data slot \verb|CodeDataFun|; on instantiation |
|
1854 |
of this functor, the following types and operations |
|
1855 |
are required: |
|
1856 |
||
1857 |
\medskip |
|
21348 | 1858 |
\begin{tabular}{l} |
1859 |
\isa{val\ name{\isacharcolon}\ string} \\ |
|
1860 |
\isa{type\ T} \\ |
|
1861 |
\isa{val\ empty{\isacharcolon}\ T} \\ |
|
1862 |
\isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\ |
|
1863 |
\isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodegenConsts{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T} |
|
1864 |
\end{tabular} |
|
1865 |
||
21452 | 1866 |
\begin{description} |
1867 |
||
1868 |
\item \isa{name} is a system-wide unique name identifying the data. |
|
1869 |
||
1870 |
\item \isa{T} the type of data to store. |
|
1871 |
||
1872 |
\item \isa{empty} initial (empty) data. |
|
1873 |
||
1874 |
\item \isa{merge} merging two data slots. |
|
1875 |
||
1876 |
\item \isa{purge}~\isa{thy}~\isa{cs} propagates changes in executable content; |
|
1877 |
if possible, the current theory context is handed over |
|
1878 |
as argument \isa{thy} (if there is no current theory context (e.g.~during |
|
1879 |
theory merge, \verb|NONE|); \isa{cs} indicates the kind |
|
1880 |
of change: \verb|NONE| stands for a fundamental change |
|
1881 |
which invalidates any existing code, \isa{SOME\ cs} |
|
1882 |
hints that executable content for constants \isa{cs} |
|
1883 |
has changed. |
|
1884 |
||
1885 |
\end{description} |
|
1886 |
||
1887 |
An instance of \verb|CodeDataFun| provides the following |
|
1888 |
interface: |
|
1889 |
||
21348 | 1890 |
\medskip |
1891 |
\begin{tabular}{l} |
|
1892 |
\isa{init{\isacharcolon}\ theory\ {\isasymrightarrow}\ theory} \\ |
|
1893 |
\isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\ |
|
1894 |
\isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\ |
|
1895 |
\isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T} |
|
21452 | 1896 |
\end{tabular} |
1897 |
||
1898 |
\begin{description} |
|
1899 |
||
1900 |
\item \isa{init} initialization during theory setup. |
|
1901 |
||
1902 |
\item \isa{get} retrieval of the current data. |
|
1903 |
||
1904 |
\item \isa{change} update of current data (cached!) |
|
1905 |
by giving a continuation. |
|
1906 |
||
1907 |
\item \isa{change{\isacharunderscore}yield} update with side result. |
|
1908 |
||
1909 |
\end{description}% |
|
1910 |
\end{isamarkuptext}% |
|
1911 |
\isamarkuptrue% |
|
1912 |
% |
|
1913 |
\isamarkupsubsubsection{Datatype hooks% |
|
1914 |
} |
|
1915 |
\isamarkuptrue% |
|
1916 |
% |
|
1917 |
\begin{isamarkuptext}% |
|
1918 |
Isabelle/HOL's datatype package provides a mechanism to |
|
1919 |
extend theories depending on datatype declarations: |
|
1920 |
\emph{datatype hooks}. For example, when declaring a new |
|
22060 | 1921 |
datatype, a hook proves defining equations for equality on |
21452 | 1922 |
that datatype (if possible).% |
21348 | 1923 |
\end{isamarkuptext}% |
1924 |
\isamarkuptrue% |
|
1925 |
% |
|
1926 |
\isadelimmlref |
|
1927 |
% |
|
1928 |
\endisadelimmlref |
|
1929 |
% |
|
1930 |
\isatagmlref |
|
1931 |
% |
|
1932 |
\begin{isamarkuptext}% |
|
1933 |
\begin{mldecls} |
|
21452 | 1934 |
\indexmltype{DatatypeHooks.hook}\verb|type DatatypeHooks.hook = string list -> theory -> theory| \\ |
1935 |
\indexml{DatatypeHooks.add}\verb|DatatypeHooks.add: DatatypeHooks.hook -> theory -> theory| |
|
21348 | 1936 |
\end{mldecls} |
1937 |
||
1938 |
\begin{description} |
|
1939 |
||
21452 | 1940 |
\item \verb|DatatypeHooks.hook| specifies the interface |
1941 |
of \emph{datatype hooks}: a theory update |
|
1942 |
depending on the list of newly introduced |
|
1943 |
datatype names. |
|
1944 |
||
1945 |
\item \verb|DatatypeHooks.add| adds a hook to the |
|
1946 |
chain of all hooks. |
|
21348 | 1947 |
|
1948 |
\end{description}% |
|
1949 |
\end{isamarkuptext}% |
|
1950 |
\isamarkuptrue% |
|
1951 |
% |
|
1952 |
\endisatagmlref |
|
1953 |
{\isafoldmlref}% |
|
1954 |
% |
|
1955 |
\isadelimmlref |
|
1956 |
% |
|
1957 |
\endisadelimmlref |
|
1958 |
% |
|
21452 | 1959 |
\isamarkupsubsubsection{Trivial typedefs -- type copies% |
21172 | 1960 |
} |
1961 |
\isamarkuptrue% |
|
1962 |
% |
|
21452 | 1963 |
\begin{isamarkuptext}% |
1964 |
Sometimes packages will introduce new types |
|
1965 |
as \emph{marked type copies} similar to Haskell's |
|
1966 |
\isa{newtype} declaration (e.g. the HOL record package) |
|
1967 |
\emph{without} tinkering with the overhead of datatypes. |
|
1968 |
Technically, these type copies are trivial forms of typedefs. |
|
1969 |
Since these type copies in code generation view are nothing |
|
1970 |
else than datatypes, they have been given a own package |
|
1971 |
in order to faciliate code generation:% |
|
1972 |
\end{isamarkuptext}% |
|
1973 |
\isamarkuptrue% |
|
1974 |
% |
|
21348 | 1975 |
\isadelimmlref |
1976 |
% |
|
1977 |
\endisadelimmlref |
|
1978 |
% |
|
1979 |
\isatagmlref |
|
1980 |
% |
|
21172 | 1981 |
\begin{isamarkuptext}% |
21348 | 1982 |
\begin{mldecls} |
21452 | 1983 |
\indexmltype{TypecopyPackage.info}\verb|type TypecopyPackage.info| \\ |
1984 |
\indexml{TypecopyPackage.add-typecopy}\verb|TypecopyPackage.add_typecopy: |\isasep\isanewline% |
|
1985 |
\verb| bstring * string list -> typ -> (bstring * bstring) option|\isasep\isanewline% |
|
1986 |
\verb| -> theory -> (string * TypecopyPackage.info) * theory| \\ |
|
1987 |
\indexml{TypecopyPackage.get-typecopy-info}\verb|TypecopyPackage.get_typecopy_info: theory|\isasep\isanewline% |
|
1988 |
\verb| -> string -> TypecopyPackage.info option| \\ |
|
1989 |
\indexml{TypecopyPackage.get-spec}\verb|TypecopyPackage.get_spec: theory -> string|\isasep\isanewline% |
|
1990 |
\verb| -> (string * sort) list * (string * typ list) list| \\ |
|
1991 |
\indexmltype{TypecopyPackage.hook}\verb|type TypecopyPackage.hook = string * TypecopyPackage.info -> theory -> theory| \\ |
|
1992 |
\indexml{TypecopyPackage.add-hook}\verb|TypecopyPackage.add_hook: TypecopyPackage.hook -> theory -> theory| \\ |
|
1993 |
\end{mldecls} |
|
1994 |
||
1995 |
\begin{description} |
|
1996 |
||
1997 |
\item \verb|TypecopyPackage.info| a record containing |
|
1998 |
the specification and further data of a type copy. |
|
1999 |
||
2000 |
\item \verb|TypecopyPackage.add_typecopy| defines a new |
|
2001 |
type copy. |
|
2002 |
||
2003 |
\item \verb|TypecopyPackage.get_typecopy_info| retrieves |
|
2004 |
data of an existing type copy. |
|
2005 |
||
2006 |
\item \verb|TypecopyPackage.get_spec| retrieves datatype-like |
|
2007 |
specification of a type copy. |
|
2008 |
||
2009 |
\item \verb|TypecopyPackage.hook|,~\verb|TypecopyPackage.add_hook| |
|
2010 |
provide a hook mechanism corresponding to the hook mechanism |
|
2011 |
on datatypes. |
|
2012 |
||
2013 |
\end{description}% |
|
21348 | 2014 |
\end{isamarkuptext}% |
2015 |
\isamarkuptrue% |
|
2016 |
% |
|
21452 | 2017 |
\endisatagmlref |
2018 |
{\isafoldmlref}% |
|
2019 |
% |
|
2020 |
\isadelimmlref |
|
2021 |
% |
|
2022 |
\endisadelimmlref |
|
2023 |
% |
|
2024 |
\isamarkupsubsubsection{Unifying type copies and datatypes% |
|
2025 |
} |
|
2026 |
\isamarkuptrue% |
|
2027 |
% |
|
2028 |
\begin{isamarkuptext}% |
|
2029 |
Since datatypes and type copies are mapped to the same concept (datatypes) |
|
2030 |
by code generation, the view on both is unified \qt{code types}:% |
|
2031 |
\end{isamarkuptext}% |
|
2032 |
\isamarkuptrue% |
|
2033 |
% |
|
2034 |
\isadelimmlref |
|
2035 |
% |
|
2036 |
\endisadelimmlref |
|
2037 |
% |
|
2038 |
\isatagmlref |
|
2039 |
% |
|
21348 | 2040 |
\begin{isamarkuptext}% |
2041 |
\begin{mldecls} |
|
21452 | 2042 |
\indexmltype{DatatypeCodegen.hook}\verb|type DatatypeCodegen.hook = (string * (bool * ((string * sort) list|\isasep\isanewline% |
2043 |
\verb| * (string * typ list) list))) list|\isasep\isanewline% |
|
21348 | 2044 |
\verb| -> theory -> theory| \\ |
2045 |
\indexml{DatatypeCodegen.add-codetypes-hook-bootstrap}\verb|DatatypeCodegen.add_codetypes_hook_bootstrap: |\isasep\isanewline% |
|
2046 |
\verb| DatatypeCodegen.hook -> theory -> theory| |
|
2047 |
\end{mldecls}% |
|
2048 |
\end{isamarkuptext}% |
|
2049 |
\isamarkuptrue% |
|
2050 |
% |
|
2051 |
\endisatagmlref |
|
2052 |
{\isafoldmlref}% |
|
2053 |
% |
|
2054 |
\isadelimmlref |
|
2055 |
% |
|
2056 |
\endisadelimmlref |
|
2057 |
% |
|
2058 |
\begin{isamarkuptext}% |
|
21452 | 2059 |
\begin{description} |
2060 |
||
2061 |
\item \verb|DatatypeCodegen.hook| specifies the code type hook |
|
2062 |
interface: a theory transformation depending on a list of |
|
2063 |
mutual recursive code types; each entry in the list |
|
2064 |
has the structure \isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}is{\isacharunderscore}data{\isacharcomma}\ {\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}{\isacharparenright}{\isacharparenright}} |
|
2065 |
where \isa{name} is the name of the code type, \isa{is{\isacharunderscore}data} |
|
2066 |
is true iff \isa{name} is a datatype rather then a type copy, |
|
2067 |
and \isa{{\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}} is the specification of the code type. |
|
2068 |
||
2069 |
\item \verb|DatatypeCodegen.add_codetypes_hook_bootstrap| adds a code |
|
2070 |
type hook; the hook is immediately processed for all already |
|
2071 |
existing datatypes, in blocks of mutual recursive datatypes, |
|
2072 |
where all datatypes a block depends on are processed before |
|
2073 |
the block. |
|
2074 |
||
2075 |
\end{description} |
|
2076 |
||
2077 |
\emph{Happy proving, happy hacking!}% |
|
21172 | 2078 |
\end{isamarkuptext}% |
2079 |
\isamarkuptrue% |
|
2080 |
% |
|
20967 | 2081 |
\isadelimtheory |
2082 |
% |
|
2083 |
\endisadelimtheory |
|
2084 |
% |
|
2085 |
\isatagtheory |
|
2086 |
\isacommand{end}\isamarkupfalse% |
|
2087 |
% |
|
2088 |
\endisatagtheory |
|
2089 |
{\isafoldtheory}% |
|
2090 |
% |
|
2091 |
\isadelimtheory |
|
2092 |
% |
|
2093 |
\endisadelimtheory |
|
2094 |
\isanewline |
|
2095 |
\end{isabellebody}% |
|
2096 |
%%% Local Variables: |
|
2097 |
%%% mode: latex |
|
2098 |
%%% TeX-master: "root" |
|
2099 |
%%% End: |