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