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