author | haftmann |
Mon, 12 Nov 2007 14:20:21 +0100 | |
changeset 25411 | ac31c92e4bf5 |
parent 25370 | 8b1aa4357320 |
child 25533 | 0140cc7b26ad |
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. |
|
22916 | 109 |
Also note that while the framework itself is |
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}% |
|
22916 | 121 |
When writing executable specifications using \isa{HOL}, |
122 |
it is convenient to use |
|
22550 | 123 |
three existing packages: the datatype package for defining |
124 |
datatypes, the function package for (recursive) functions, |
|
125 |
and the class package for overloaded definitions. |
|
126 |
||
127 |
We develope a small theory of search trees; trees are represented |
|
128 |
as a datatype with key type \isa{{\isacharprime}a} and value type \isa{{\isacharprime}b}:% |
|
129 |
\end{isamarkuptext}% |
|
130 |
\isamarkuptrue% |
|
22479 | 131 |
\isacommand{datatype}\isamarkupfalse% |
132 |
\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree\ {\isacharequal}\ Leaf\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}linorder{\isachardoublequoteclose}\ {\isacharprime}b\isanewline |
|
22550 | 133 |
\ \ {\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}% |
134 |
\begin{isamarkuptext}% |
|
135 |
\noindent Note that we have constrained the type of keys |
|
136 |
to the class of total orders, \isa{linorder}. |
|
137 |
||
138 |
We define \isa{find} and \isa{update} functions:% |
|
139 |
\end{isamarkuptext}% |
|
140 |
\isamarkuptrue% |
|
22479 | 141 |
\isacommand{fun}\isamarkupfalse% |
142 |
\isanewline |
|
143 |
\ \ 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 |
|
144 |
\ \ {\isachardoublequoteopen}find\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharequal}\ {\isacharparenleft}if\ it\ {\isacharequal}\ key\ then\ Some\ val\ else\ None{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
145 |
\ \ {\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 |
|
146 |
\isanewline |
|
147 |
\isacommand{fun}\isamarkupfalse% |
|
148 |
\isanewline |
|
149 |
\ \ 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 |
|
150 |
\ \ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline |
|
151 |
\ \ \ \ if\ it\ {\isacharequal}\ key\ then\ Leaf\ key\ entry\isanewline |
|
152 |
\ \ \ \ \ \ else\ if\ it\ {\isasymle}\ key\isanewline |
|
153 |
\ \ \ \ \ \ then\ Branch\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\isanewline |
|
154 |
\ \ \ \ \ \ else\ Branch\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\isanewline |
|
155 |
\ \ \ {\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
156 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ t{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline |
|
157 |
\ \ \ \ if\ it\ {\isasymle}\ key\isanewline |
|
158 |
\ \ \ \ \ \ then\ {\isacharparenleft}Branch\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{1}}{\isacharparenright}\ key\ t{\isadigit{2}}{\isacharparenright}\isanewline |
|
159 |
\ \ \ \ \ \ else\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{2}}{\isacharparenright}{\isacharparenright}\isanewline |
|
22550 | 160 |
\ \ \ {\isacharparenright}{\isachardoublequoteclose}% |
161 |
\begin{isamarkuptext}% |
|
162 |
\noindent For testing purpose, we define a small example |
|
163 |
using natural numbers \isa{nat} (which are a \isa{linorder}) |
|
23132 | 164 |
as keys and list of nats as values:% |
22550 | 165 |
\end{isamarkuptext}% |
166 |
\isamarkuptrue% |
|
23132 | 167 |
\isacommand{definition}\isamarkupfalse% |
22479 | 168 |
\isanewline |
23132 | 169 |
\ \ example\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat{\isacharcomma}\ nat\ list{\isacharparenright}\ searchtree{\isachardoublequoteclose}\isanewline |
170 |
\isakeyword{where}\isanewline |
|
171 |
\ \ {\isachardoublequoteopen}example\ {\isacharequal}\ update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\ {\isacharparenleft}update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\isanewline |
|
172 |
\ \ \ \ {\isacharparenleft}update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\ {\isacharparenleft}Leaf\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}% |
|
22550 | 173 |
\begin{isamarkuptext}% |
174 |
\noindent Then we generate code% |
|
175 |
\end{isamarkuptext}% |
|
176 |
\isamarkuptrue% |
|
24379 | 177 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 178 |
\ example\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}tree{\isachardot}ML{\isachardoublequoteclose}% |
22479 | 179 |
\begin{isamarkuptext}% |
22550 | 180 |
\noindent which looks like: |
181 |
\lstsml{Thy/examples/tree.ML}% |
|
22479 | 182 |
\end{isamarkuptext}% |
183 |
\isamarkuptrue% |
|
184 |
% |
|
22550 | 185 |
\isamarkupsection{Code generation concepts and process \label{sec:concept}% |
21172 | 186 |
} |
187 |
\isamarkuptrue% |
|
188 |
% |
|
189 |
\begin{isamarkuptext}% |
|
21348 | 190 |
\begin{figure}[h] |
191 |
\centering |
|
192 |
\includegraphics[width=0.7\textwidth]{codegen_process} |
|
193 |
\caption{code generator -- processing overview} |
|
194 |
\label{fig:process} |
|
195 |
\end{figure} |
|
196 |
||
197 |
The code generator employs a notion of executability |
|
21172 | 198 |
for three foundational executable ingredients known |
199 |
from functional programming: |
|
22060 | 200 |
\emph{defining equations}, \emph{datatypes}, and |
201 |
\emph{type classes}. A defining equation as a first approximation |
|
21172 | 202 |
is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t} |
203 |
(an equation headed by a constant \isa{f} with arguments |
|
22798 | 204 |
\isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}). |
22060 | 205 |
Code generation aims to turn defining equations |
21172 | 206 |
into a functional program by running through |
207 |
a process (see figure \ref{fig:process}): |
|
208 |
||
209 |
\begin{itemize} |
|
210 |
||
211 |
\item Out of the vast collection of theorems proven in a |
|
212 |
\qn{theory}, a reasonable subset modeling |
|
22060 | 213 |
defining equations is \qn{selected}. |
21172 | 214 |
|
215 |
\item On those selected theorems, certain |
|
216 |
transformations are carried out |
|
217 |
(\qn{preprocessing}). Their purpose is to turn theorems |
|
218 |
representing non- or badly executable |
|
219 |
specifications into equivalent but executable counterparts. |
|
220 |
The result is a structured collection of \qn{code theorems}. |
|
221 |
||
22479 | 222 |
\item These \qn{code theorems} then are \qn{translated} |
21172 | 223 |
into an Haskell-like intermediate |
224 |
language. |
|
225 |
||
226 |
\item Finally, out of the intermediate language the final |
|
227 |
code in the desired \qn{target language} is \qn{serialized}. |
|
228 |
||
229 |
\end{itemize} |
|
230 |
||
231 |
From these steps, only the two last are carried out |
|
232 |
outside the logic; by keeping this layer as |
|
233 |
thin as possible, the amount of code to trust is |
|
234 |
kept to a minimum.% |
|
235 |
\end{isamarkuptext}% |
|
236 |
\isamarkuptrue% |
|
237 |
% |
|
238 |
\isamarkupsection{Basics \label{sec:basics}% |
|
20967 | 239 |
} |
240 |
\isamarkuptrue% |
|
241 |
% |
|
242 |
\isamarkupsubsection{Invoking the code generator% |
|
243 |
} |
|
244 |
\isamarkuptrue% |
|
245 |
% |
|
21172 | 246 |
\begin{isamarkuptext}% |
22916 | 247 |
Thanks to a reasonable setup of the \isa{HOL} theories, in |
21172 | 248 |
most cases code generation proceeds without further ado:% |
249 |
\end{isamarkuptext}% |
|
250 |
\isamarkuptrue% |
|
22479 | 251 |
\isacommand{fun}\isamarkupfalse% |
21172 | 252 |
\isanewline |
22479 | 253 |
\ \ fac\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
254 |
\ \ \ \ {\isachardoublequoteopen}fac\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline |
|
255 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}fac\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ Suc\ n\ {\isacharasterisk}\ fac\ n{\isachardoublequoteclose}% |
|
21172 | 256 |
\begin{isamarkuptext}% |
22550 | 257 |
\noindent This executable specification is now turned to SML code:% |
21172 | 258 |
\end{isamarkuptext}% |
259 |
\isamarkuptrue% |
|
24379 | 260 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 261 |
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 262 |
\begin{isamarkuptext}% |
24379 | 263 |
\noindent The \isa{{\isasymEXPORTCODE}} command takes a space-separated list of |
21172 | 264 |
constants together with \qn{serialization directives} |
22845 | 265 |
These start with a \qn{target language} |
266 |
identifier, followed by a file specification |
|
267 |
where to write the generated code to. |
|
21172 | 268 |
|
22060 | 269 |
Internally, the defining equations for all selected |
21186 | 270 |
constants are taken, including any transitively required |
21172 | 271 |
constants, datatypes and classes, resulting in the following |
272 |
code: |
|
273 |
||
274 |
\lstsml{Thy/examples/fac.ML} |
|
275 |
||
276 |
The code generator will complain when a required |
|
22550 | 277 |
ingredient does not provide a executable counterpart, |
278 |
e.g.~generating code |
|
21172 | 279 |
for constants not yielding |
22550 | 280 |
a defining equation (e.g.~the Hilbert choice |
281 |
operation \isa{SOME}):% |
|
21172 | 282 |
\end{isamarkuptext}% |
283 |
\isamarkuptrue% |
|
284 |
% |
|
285 |
\isadelimML |
|
286 |
% |
|
287 |
\endisadelimML |
|
288 |
% |
|
289 |
\isatagML |
|
290 |
% |
|
291 |
\endisatagML |
|
292 |
{\isafoldML}% |
|
293 |
% |
|
294 |
\isadelimML |
|
295 |
% |
|
296 |
\endisadelimML |
|
297 |
\isacommand{definition}\isamarkupfalse% |
|
298 |
\isanewline |
|
21993 | 299 |
\ \ pick{\isacharunderscore}some\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
22798 | 300 |
\ \ {\isachardoublequoteopen}pick{\isacharunderscore}some\ xs\ {\isacharequal}\ {\isacharparenleft}SOME\ x{\isachardot}\ x\ {\isasymin}\ set\ xs{\isacharparenright}{\isachardoublequoteclose}% |
21172 | 301 |
\isadelimML |
302 |
% |
|
303 |
\endisadelimML |
|
304 |
% |
|
305 |
\isatagML |
|
306 |
% |
|
307 |
\endisatagML |
|
308 |
{\isafoldML}% |
|
309 |
% |
|
310 |
\isadelimML |
|
311 |
% |
|
312 |
\endisadelimML |
|
24379 | 313 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 314 |
\ pick{\isacharunderscore}some\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}const{\isachardot}ML{\isachardoublequoteclose}% |
22550 | 315 |
\begin{isamarkuptext}% |
316 |
\noindent will fail.% |
|
317 |
\end{isamarkuptext}% |
|
318 |
\isamarkuptrue% |
|
319 |
% |
|
20967 | 320 |
\isamarkupsubsection{Theorem selection% |
321 |
} |
|
322 |
\isamarkuptrue% |
|
323 |
% |
|
21172 | 324 |
\begin{isamarkuptext}% |
22060 | 325 |
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
|
326 |
using the \isa{{\isasymPRINTCODESETUP}} command:% |
21172 | 327 |
\end{isamarkuptext}% |
328 |
\isamarkuptrue% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
329 |
\isacommand{print{\isacharunderscore}codesetup}\isamarkupfalse% |
21172 | 330 |
% |
331 |
\begin{isamarkuptext}% |
|
332 |
\noindent which displays a table of constant with corresponding |
|
22060 | 333 |
defining equations (the additional stuff displayed |
22751 | 334 |
shall not bother us for the moment). |
21172 | 335 |
|
22916 | 336 |
The typical \isa{HOL} tools are already set up in a way that |
22751 | 337 |
function definitions introduced by \isa{{\isasymDEFINITION}}, |
338 |
\isa{{\isasymFUN}}, |
|
22798 | 339 |
\isa{{\isasymFUNCTION}}, \isa{{\isasymPRIMREC}}, |
21348 | 340 |
\isa{{\isasymRECDEF}} are implicitly propagated |
22060 | 341 |
to this defining equation table. Specific theorems may be |
21172 | 342 |
selected using an attribute: \emph{code func}. As example, |
343 |
a weight selector function:% |
|
344 |
\end{isamarkuptext}% |
|
345 |
\isamarkuptrue% |
|
346 |
\isacommand{consts}\isamarkupfalse% |
|
347 |
\isanewline |
|
348 |
\ \ pick\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline |
|
349 |
\isanewline |
|
350 |
\isacommand{primrec}\isamarkupfalse% |
|
351 |
\isanewline |
|
352 |
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharequal}\ x\ in\isanewline |
|
353 |
\ \ \ \ if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}% |
|
354 |
\begin{isamarkuptext}% |
|
22798 | 355 |
\noindent We want to eliminate the explicit destruction |
21172 | 356 |
of \isa{x} to \isa{{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}}:% |
357 |
\end{isamarkuptext}% |
|
358 |
\isamarkuptrue% |
|
359 |
\isacommand{lemma}\isamarkupfalse% |
|
360 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
361 |
\ \ {\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 |
|
362 |
% |
|
363 |
\isadelimproof |
|
364 |
\ \ % |
|
365 |
\endisadelimproof |
|
366 |
% |
|
367 |
\isatagproof |
|
368 |
\isacommand{by}\isamarkupfalse% |
|
369 |
\ simp% |
|
370 |
\endisatagproof |
|
371 |
{\isafoldproof}% |
|
372 |
% |
|
373 |
\isadelimproof |
|
374 |
\isanewline |
|
375 |
% |
|
376 |
\endisadelimproof |
|
377 |
\isanewline |
|
24379 | 378 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 379 |
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 380 |
\begin{isamarkuptext}% |
22798 | 381 |
\noindent This theorem now is used for generating code: |
21172 | 382 |
|
383 |
\lstsml{Thy/examples/pick1.ML} |
|
384 |
||
22798 | 385 |
\noindent It might be convenient to remove the pointless original |
22845 | 386 |
equation, using the \emph{func del} attribute:% |
21172 | 387 |
\end{isamarkuptext}% |
388 |
\isamarkuptrue% |
|
389 |
\isacommand{lemmas}\isamarkupfalse% |
|
22845 | 390 |
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline |
21172 | 391 |
\isanewline |
24379 | 392 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 393 |
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 394 |
\begin{isamarkuptext}% |
395 |
\lstsml{Thy/examples/pick2.ML} |
|
396 |
||
22798 | 397 |
\noindent Syntactic redundancies are implicitly dropped. For example, |
21172 | 398 |
using a modified version of the \isa{fac} function |
22060 | 399 |
as defining equation, the then redundant (since |
400 |
syntactically subsumed) original defining equations |
|
21172 | 401 |
are dropped, resulting in a warning:% |
402 |
\end{isamarkuptext}% |
|
403 |
\isamarkuptrue% |
|
404 |
\isacommand{lemma}\isamarkupfalse% |
|
405 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
406 |
\ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
407 |
% |
|
408 |
\isadelimproof |
|
409 |
\ \ % |
|
410 |
\endisadelimproof |
|
411 |
% |
|
412 |
\isatagproof |
|
413 |
\isacommand{by}\isamarkupfalse% |
|
414 |
\ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all% |
|
415 |
\endisatagproof |
|
416 |
{\isafoldproof}% |
|
417 |
% |
|
418 |
\isadelimproof |
|
419 |
\isanewline |
|
420 |
% |
|
421 |
\endisadelimproof |
|
422 |
\isanewline |
|
24379 | 423 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 424 |
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 425 |
\begin{isamarkuptext}% |
426 |
\lstsml{Thy/examples/fac_case.ML} |
|
427 |
||
428 |
\begin{warn} |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
429 |
The attributes \emph{code} and \emph{code del} |
21172 | 430 |
associated with the existing code generator also apply to |
431 |
the new one: \emph{code} implies \emph{code func}, |
|
22845 | 432 |
and \emph{code del} implies \emph{code func del}. |
21172 | 433 |
\end{warn}% |
434 |
\end{isamarkuptext}% |
|
435 |
\isamarkuptrue% |
|
436 |
% |
|
20967 | 437 |
\isamarkupsubsection{Type classes% |
438 |
} |
|
439 |
\isamarkuptrue% |
|
440 |
% |
|
21172 | 441 |
\begin{isamarkuptext}% |
442 |
Type classes enter the game via the Isar class package. |
|
443 |
For a short introduction how to use it, see \cite{isabelle-classes}; |
|
444 |
here we just illustrate its impact on code generation. |
|
445 |
||
446 |
In a target language, type classes may be represented |
|
22798 | 447 |
natively (as in the case of Haskell). For languages |
21172 | 448 |
like SML, they are implemented using \emph{dictionaries}. |
21186 | 449 |
Our following example specifies a class \qt{null}, |
21172 | 450 |
assigning to each of its inhabitants a \qt{null} value:% |
451 |
\end{isamarkuptext}% |
|
452 |
\isamarkuptrue% |
|
453 |
\isacommand{class}\isamarkupfalse% |
|
22479 | 454 |
\ null\ {\isacharequal}\ type\ {\isacharplus}\isanewline |
21172 | 455 |
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline |
456 |
\isanewline |
|
22798 | 457 |
\isacommand{fun}\isamarkupfalse% |
21172 | 458 |
\isanewline |
459 |
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline |
|
22798 | 460 |
\isakeyword{where}\isanewline |
21172 | 461 |
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline |
22798 | 462 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}% |
21172 | 463 |
\begin{isamarkuptext}% |
464 |
We provide some instances for our \isa{null}:% |
|
465 |
\end{isamarkuptext}% |
|
466 |
\isamarkuptrue% |
|
467 |
\isacommand{instance}\isamarkupfalse% |
|
468 |
\ option\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
469 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ None{\isachardoublequoteclose}% |
|
470 |
\isadelimproof |
|
471 |
\ % |
|
472 |
\endisadelimproof |
|
473 |
% |
|
474 |
\isatagproof |
|
475 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
476 |
% |
|
477 |
\endisatagproof |
|
478 |
{\isafoldproof}% |
|
479 |
% |
|
480 |
\isadelimproof |
|
481 |
% |
|
482 |
\endisadelimproof |
|
483 |
\isanewline |
|
484 |
\isanewline |
|
485 |
\isacommand{instance}\isamarkupfalse% |
|
486 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline |
|
487 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}% |
|
488 |
\isadelimproof |
|
489 |
\ % |
|
490 |
\endisadelimproof |
|
491 |
% |
|
492 |
\isatagproof |
|
493 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
494 |
% |
|
495 |
\endisatagproof |
|
496 |
{\isafoldproof}% |
|
497 |
% |
|
498 |
\isadelimproof |
|
499 |
% |
|
500 |
\endisadelimproof |
|
501 |
% |
|
502 |
\begin{isamarkuptext}% |
|
503 |
Constructing a dummy example:% |
|
504 |
\end{isamarkuptext}% |
|
505 |
\isamarkuptrue% |
|
506 |
\isacommand{definition}\isamarkupfalse% |
|
507 |
\isanewline |
|
508 |
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}% |
|
509 |
\begin{isamarkuptext}% |
|
21186 | 510 |
Type classes offer a suitable occasion to introduce |
21172 | 511 |
the Haskell serializer. Its usage is almost the same |
512 |
as SML, but, in accordance with conventions |
|
513 |
some Haskell systems enforce, each module ends |
|
514 |
up in a single file. The module hierarchy is reflected in |
|
22845 | 515 |
the file system, with root directory given as file specification.% |
21172 | 516 |
\end{isamarkuptext}% |
517 |
\isamarkuptrue% |
|
24379 | 518 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 519 |
\ dummy\ \isakeyword{in}\ Haskell\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}% |
21172 | 520 |
\begin{isamarkuptext}% |
521 |
\lsthaskell{Thy/examples/Codegen.hs} |
|
22798 | 522 |
\noindent (we have left out all other modules). |
21172 | 523 |
|
22798 | 524 |
\medskip |
21172 | 525 |
|
526 |
The whole code in SML with explicit dictionary passing:% |
|
527 |
\end{isamarkuptext}% |
|
528 |
\isamarkuptrue% |
|
24379 | 529 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 530 |
\ dummy\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 531 |
\begin{isamarkuptext}% |
22798 | 532 |
\lstsml{Thy/examples/class.ML} |
533 |
||
534 |
\medskip |
|
535 |
||
536 |
\noindent or in OCaml:% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
537 |
\end{isamarkuptext}% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
538 |
\isamarkuptrue% |
24379 | 539 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 540 |
\ 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
|
541 |
\begin{isamarkuptext}% |
22798 | 542 |
\lstsml{Thy/examples/class.ocaml} |
21172 | 543 |
|
22798 | 544 |
\medskip The explicit association of constants |
22845 | 545 |
to classes can be inspected using the \isa{{\isasymPRINTCLASSES}} |
546 |
command.% |
|
21172 | 547 |
\end{isamarkuptext}% |
548 |
\isamarkuptrue% |
|
549 |
% |
|
550 |
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}% |
|
551 |
} |
|
552 |
\isamarkuptrue% |
|
553 |
% |
|
554 |
\begin{isamarkuptext}% |
|
555 |
In this tutorial, we do not attempt to give an exhaustive |
|
556 |
description of the code generator framework; instead, |
|
557 |
we cast a light on advanced topics by introducing |
|
558 |
them together with practically motivated examples. Concerning |
|
559 |
further reading, see |
|
560 |
||
561 |
\begin{itemize} |
|
562 |
||
563 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} |
|
564 |
for exhaustive syntax diagrams. |
|
24193 | 565 |
\item or \cite{Haftmann-Nipkow:2007:codegen} which deals with foundational issues |
21172 | 566 |
of the code generator framework. |
567 |
||
568 |
\end{itemize}% |
|
569 |
\end{isamarkuptext}% |
|
570 |
\isamarkuptrue% |
|
571 |
% |
|
22798 | 572 |
\isamarkupsubsection{Library theories \label{sec:library}% |
21172 | 573 |
} |
574 |
\isamarkuptrue% |
|
575 |
% |
|
576 |
\begin{isamarkuptext}% |
|
22916 | 577 |
The \isa{HOL} \isa{Main} theory already provides a code |
578 |
generator setup |
|
21172 | 579 |
which should be suitable for most applications. Common extensions |
22916 | 580 |
and modifications are available by certain theories of the \isa{HOL} |
21172 | 581 |
library; beside being useful in applications, they may serve |
21186 | 582 |
as a tutorial for customizing the code generator setup. |
21172 | 583 |
|
584 |
\begin{description} |
|
585 |
||
25370 | 586 |
\item[\isa{Code{\isacharunderscore}Integer}] represents \isa{HOL} integers by big |
22798 | 587 |
integer literals in target languages. |
25370 | 588 |
\item[\isa{Code{\isacharunderscore}Char}] represents \isa{HOL} characters by |
22798 | 589 |
character literals in target languages. |
25370 | 590 |
\item[\isa{Code{\isacharunderscore}Char{\isacharunderscore}chr}] like \isa{Code{\isacharunderscore}Char}, |
22798 | 591 |
but also offers treatment of character codes; includes |
25370 | 592 |
\isa{Code{\isacharunderscore}Integer}. |
23850 | 593 |
\item[\isa{Efficient{\isacharunderscore}Nat}] \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} |
25370 | 596 |
is eliminated; includes \isa{Code{\isacharunderscore}Integer}. |
597 |
\item[\isa{Code{\isacharunderscore}Index}] provides an additional datatype |
|
598 |
\isa{index} which is mapped to target-language built-in integers. |
|
599 |
Useful for code setups which involve e.g. indexing of |
|
600 |
target-language arrays. |
|
601 |
\item[\isa{Code{\isacharunderscore}Message}] provides an additional datatype |
|
602 |
\isa{message{\isacharunderscore}string} which is isomorphic to strings; |
|
603 |
\isa{message{\isacharunderscore}string}s are mapped to target-language strings. |
|
604 |
Useful for code setups which involve e.g. printing (error) messages. |
|
21172 | 605 |
|
22916 | 606 |
\end{description} |
607 |
||
608 |
\begin{warn} |
|
609 |
When importing any of these theories, they should form the last |
|
610 |
items in an import list. Since these theories adapt the |
|
611 |
code generator setup in a non-conservative fashion, |
|
612 |
strange effects may occur otherwise. |
|
613 |
\end{warn}% |
|
21172 | 614 |
\end{isamarkuptext}% |
615 |
\isamarkuptrue% |
|
616 |
% |
|
617 |
\isamarkupsubsection{Preprocessing% |
|
20967 | 618 |
} |
619 |
\isamarkuptrue% |
|
620 |
% |
|
21172 | 621 |
\begin{isamarkuptext}% |
622 |
Before selected function theorems are turned into abstract |
|
623 |
code, a chain of definitional transformation steps is carried |
|
21186 | 624 |
out: \emph{preprocessing}. There are three possibilities |
21172 | 625 |
to customize preprocessing: \emph{inline theorems}, |
626 |
\emph{inline procedures} and \emph{generic preprocessors}. |
|
627 |
||
628 |
\emph{Inline theorems} are rewriting rules applied to each |
|
22060 | 629 |
defining equation. Due to the interpretation of theorems |
630 |
of defining equations, rewrites are applied to the right |
|
21172 | 631 |
hand side and the arguments of the left hand side of an |
632 |
equation, but never to the constant heading the left hand side. |
|
633 |
Inline theorems may be declared an undeclared using the |
|
22845 | 634 |
\emph{code inline} or \emph{code inline del} attribute respectively. |
21172 | 635 |
Some common applications:% |
636 |
\end{isamarkuptext}% |
|
637 |
\isamarkuptrue% |
|
638 |
% |
|
639 |
\begin{itemize} |
|
22845 | 640 |
% |
641 |
\begin{isamarkuptext}% |
|
642 |
\item replacing non-executable constructs by executable ones:% |
|
643 |
\end{isamarkuptext}% |
|
644 |
\isamarkuptrue% |
|
645 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 646 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 647 |
\ \ \ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
21172 | 648 |
\isadelimproof |
649 |
\ % |
|
650 |
\endisadelimproof |
|
651 |
% |
|
652 |
\isatagproof |
|
653 |
\isacommand{by}\isamarkupfalse% |
|
654 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
655 |
\endisatagproof |
|
656 |
{\isafoldproof}% |
|
657 |
% |
|
658 |
\isadelimproof |
|
659 |
% |
|
660 |
\endisadelimproof |
|
661 |
% |
|
22845 | 662 |
\begin{isamarkuptext}% |
663 |
\item eliminating superfluous constants:% |
|
664 |
\end{isamarkuptext}% |
|
665 |
\isamarkuptrue% |
|
666 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 667 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 668 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}% |
21172 | 669 |
\isadelimproof |
670 |
\ % |
|
671 |
\endisadelimproof |
|
672 |
% |
|
673 |
\isatagproof |
|
674 |
\isacommand{by}\isamarkupfalse% |
|
675 |
\ simp% |
|
676 |
\endisatagproof |
|
677 |
{\isafoldproof}% |
|
678 |
% |
|
679 |
\isadelimproof |
|
680 |
% |
|
681 |
\endisadelimproof |
|
682 |
% |
|
22845 | 683 |
\begin{isamarkuptext}% |
684 |
\item replacing executable but inconvenient constructs:% |
|
685 |
\end{isamarkuptext}% |
|
686 |
\isamarkuptrue% |
|
687 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 688 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 689 |
\ \ \ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}% |
21172 | 690 |
\isadelimproof |
691 |
\ % |
|
692 |
\endisadelimproof |
|
693 |
% |
|
694 |
\isatagproof |
|
695 |
\isacommand{by}\isamarkupfalse% |
|
696 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
697 |
\endisatagproof |
|
698 |
{\isafoldproof}% |
|
699 |
% |
|
700 |
\isadelimproof |
|
701 |
% |
|
702 |
\endisadelimproof |
|
703 |
% |
|
704 |
\end{itemize} |
|
705 |
% |
|
706 |
\begin{isamarkuptext}% |
|
22845 | 707 |
\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
|
708 |
the \isa{{\isasymPRINTCODESETUP}} command. |
21172 | 709 |
|
710 |
\emph{Inline procedures} are a generalized version of inline |
|
711 |
theorems written in ML -- rewrite rules are generated dependent |
|
712 |
on the function theorems for a certain function. One |
|
713 |
application is the implicit expanding of \isa{nat} numerals |
|
714 |
to \isa{{\isadigit{0}}} / \isa{Suc} representation. See further |
|
715 |
\secref{sec:ml} |
|
716 |
||
717 |
\emph{Generic preprocessors} provide a most general interface, |
|
718 |
transforming a list of function theorems to another |
|
719 |
list of function theorems, provided that neither the heading |
|
720 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc} |
|
21348 | 721 |
pattern elimination implemented in |
21452 | 722 |
theory \isa{EfficientNat} (\secref{eff_nat}) uses this |
21172 | 723 |
interface. |
724 |
||
725 |
\begin{warn} |
|
726 |
The order in which single preprocessing steps are carried |
|
727 |
out currently is not specified; in particular, preprocessing |
|
21186 | 728 |
is \emph{no} fix point process. Keep this in mind when |
21172 | 729 |
setting up the preprocessor. |
730 |
||
731 |
Further, the attribute \emph{code unfold} |
|
732 |
associated with the existing code generator also applies to |
|
733 |
the new one: \emph{code unfold} implies \emph{code inline}. |
|
734 |
\end{warn}% |
|
735 |
\end{isamarkuptext}% |
|
736 |
\isamarkuptrue% |
|
737 |
% |
|
22798 | 738 |
\isamarkupsubsection{Concerning operational equality% |
739 |
} |
|
740 |
\isamarkuptrue% |
|
741 |
% |
|
742 |
\begin{isamarkuptext}% |
|
743 |
Surely you have already noticed how equality is treated |
|
744 |
by the code generator:% |
|
745 |
\end{isamarkuptext}% |
|
746 |
\isamarkuptrue% |
|
747 |
\isacommand{fun}\isamarkupfalse% |
|
748 |
\isanewline |
|
749 |
\ \ 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 |
|
750 |
\ \ \ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline |
|
751 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline |
|
752 |
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline |
|
753 |
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline |
|
754 |
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline |
|
755 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}% |
|
756 |
\begin{isamarkuptext}% |
|
757 |
The membership test during preprocessing is rewritten, |
|
758 |
resulting in \isa{op\ mem}, which itself |
|
759 |
performs an explicit equality check.% |
|
760 |
\end{isamarkuptext}% |
|
761 |
\isamarkuptrue% |
|
24379 | 762 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 763 |
\ collect{\isacharunderscore}duplicates\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}% |
22798 | 764 |
\begin{isamarkuptext}% |
765 |
\lstsml{Thy/examples/collect_duplicates.ML}% |
|
766 |
\end{isamarkuptext}% |
|
767 |
\isamarkuptrue% |
|
768 |
% |
|
769 |
\begin{isamarkuptext}% |
|
770 |
Obviously, polymorphic equality is implemented the Haskell |
|
771 |
way using a type class. How is this achieved? By an |
|
772 |
almost trivial definition in the HOL setup:% |
|
773 |
\end{isamarkuptext}% |
|
774 |
\isamarkuptrue% |
|
775 |
% |
|
776 |
\isadelimML |
|
777 |
% |
|
778 |
\endisadelimML |
|
779 |
% |
|
780 |
\isatagML |
|
781 |
% |
|
782 |
\endisatagML |
|
783 |
{\isafoldML}% |
|
784 |
% |
|
785 |
\isadelimML |
|
786 |
% |
|
787 |
\endisadelimML |
|
788 |
\isacommand{class}\isamarkupfalse% |
|
789 |
\ eq\ {\isacharparenleft}\isakeyword{attach}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\ {\isacharequal}\ type% |
|
790 |
\begin{isamarkuptext}% |
|
791 |
This merely introduces a class \isa{eq} with corresponding |
|
792 |
operation \isa{op\ {\isacharequal}}; |
|
793 |
the preprocessing framework does the rest. |
|
794 |
For datatypes, instances of \isa{eq} are implicitly derived |
|
795 |
when possible. |
|
796 |
||
797 |
Though this \isa{eq} class is designed to get rarely in |
|
798 |
the way, a subtlety |
|
799 |
enters the stage when definitions of overloaded constants |
|
800 |
are dependent on operational equality. For example, let |
|
801 |
us define a lexicographic ordering on tuples:% |
|
802 |
\end{isamarkuptext}% |
|
803 |
\isamarkuptrue% |
|
804 |
% |
|
805 |
\isadelimML |
|
806 |
% |
|
807 |
\endisadelimML |
|
808 |
% |
|
809 |
\isatagML |
|
810 |
% |
|
811 |
\endisatagML |
|
812 |
{\isafoldML}% |
|
813 |
% |
|
814 |
\isadelimML |
|
815 |
% |
|
816 |
\endisadelimML |
|
817 |
\isacommand{instance}\isamarkupfalse% |
|
818 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline |
|
819 |
\ \ less{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline |
|
820 |
\ \ \ \ {\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 |
|
821 |
\ \ \ \ 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 |
|
822 |
\ \ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline |
|
823 |
\ \ \ \ {\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 |
|
824 |
\ \ \ \ 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}% |
|
825 |
\isadelimproof |
|
826 |
\ % |
|
827 |
\endisadelimproof |
|
828 |
% |
|
829 |
\isatagproof |
|
830 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
831 |
% |
|
832 |
\endisatagproof |
|
833 |
{\isafoldproof}% |
|
834 |
% |
|
835 |
\isadelimproof |
|
836 |
% |
|
837 |
\endisadelimproof |
|
838 |
\isanewline |
|
839 |
\isanewline |
|
840 |
\isacommand{lemmas}\isamarkupfalse% |
|
22845 | 841 |
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\isanewline |
22798 | 842 |
\isanewline |
843 |
\isacommand{lemma}\isamarkupfalse% |
|
844 |
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
845 |
\ \ {\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 |
|
846 |
\ \ {\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 |
|
847 |
% |
|
848 |
\isadelimproof |
|
849 |
\ \ % |
|
850 |
\endisadelimproof |
|
851 |
% |
|
852 |
\isatagproof |
|
853 |
\isacommand{unfolding}\isamarkupfalse% |
|
854 |
\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
|
855 |
\ simp{\isacharunderscore}all% |
|
856 |
\endisatagproof |
|
857 |
{\isafoldproof}% |
|
858 |
% |
|
859 |
\isadelimproof |
|
860 |
% |
|
861 |
\endisadelimproof |
|
862 |
% |
|
863 |
\begin{isamarkuptext}% |
|
864 |
Then code generation will fail. Why? The definition |
|
865 |
of \isa{op\ {\isasymle}} depends on equality on both arguments, |
|
866 |
which are polymorphic and impose an additional \isa{eq} |
|
867 |
class constraint, thus violating the type discipline |
|
868 |
for class operations. |
|
869 |
||
870 |
The solution is to add \isa{eq} explicitly to the first sort arguments in the |
|
871 |
code theorems:% |
|
872 |
\end{isamarkuptext}% |
|
873 |
\isamarkuptrue% |
|
874 |
\isacommand{lemma}\isamarkupfalse% |
|
875 |
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
876 |
\ \ {\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 |
|
877 |
\ \ \ \ 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 |
|
878 |
\ \ {\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 |
|
879 |
\ \ \ \ 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 |
|
880 |
% |
|
881 |
\isadelimproof |
|
882 |
\ \ % |
|
883 |
\endisadelimproof |
|
884 |
% |
|
885 |
\isatagproof |
|
886 |
\isacommand{unfolding}\isamarkupfalse% |
|
887 |
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse% |
|
888 |
\ rule{\isacharplus}% |
|
889 |
\endisatagproof |
|
890 |
{\isafoldproof}% |
|
891 |
% |
|
892 |
\isadelimproof |
|
893 |
% |
|
894 |
\endisadelimproof |
|
895 |
% |
|
896 |
\begin{isamarkuptext}% |
|
897 |
\noindent Then code generation succeeds:% |
|
898 |
\end{isamarkuptext}% |
|
899 |
\isamarkuptrue% |
|
24379 | 900 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22798 | 901 |
\ {\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 | 902 |
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}% |
22798 | 903 |
\begin{isamarkuptext}% |
904 |
\lstsml{Thy/examples/lexicographic.ML}% |
|
905 |
\end{isamarkuptext}% |
|
906 |
\isamarkuptrue% |
|
907 |
% |
|
908 |
\begin{isamarkuptext}% |
|
909 |
In general, code theorems for overloaded constants may have more |
|
910 |
restrictive sort constraints than the underlying instance relation |
|
911 |
between class and type constructor as long as the whole system of |
|
912 |
constraints is coregular; code theorems violating coregularity |
|
913 |
are rejected immediately. Consequently, it might be necessary |
|
914 |
to delete disturbing theorems in the code theorem table, |
|
915 |
as we have done here with the original definitions \isa{less{\isacharunderscore}prod{\isacharunderscore}def} |
|
22885 | 916 |
and \isa{less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def}. |
917 |
||
918 |
In some cases, the automatically derived defining equations |
|
919 |
for equality on a particular type may not be appropriate. |
|
920 |
As example, watch the following datatype representing |
|
23132 | 921 |
monomorphic parametric types (where type constructors |
922 |
are referred to by natural numbers):% |
|
22885 | 923 |
\end{isamarkuptext}% |
924 |
\isamarkuptrue% |
|
925 |
\isacommand{datatype}\isamarkupfalse% |
|
23132 | 926 |
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}% |
22885 | 927 |
\isadelimproof |
928 |
% |
|
929 |
\endisadelimproof |
|
930 |
% |
|
931 |
\isatagproof |
|
932 |
% |
|
933 |
\endisatagproof |
|
934 |
{\isafoldproof}% |
|
935 |
% |
|
936 |
\isadelimproof |
|
937 |
% |
|
938 |
\endisadelimproof |
|
939 |
% |
|
940 |
\begin{isamarkuptext}% |
|
941 |
Then code generation for SML would fail with a message |
|
942 |
that the generated code conains illegal mutual dependencies: |
|
23132 | 943 |
the theorem \isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}} already requires the |
22885 | 944 |
instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires |
23132 | 945 |
\isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}}; Haskell has no problem with mutually |
22885 | 946 |
recursive \isa{instance} and \isa{function} definitions, |
947 |
but the SML serializer does not support this. |
|
948 |
||
949 |
In such cases, you have to provide you own equality equations |
|
950 |
involving auxiliary constants. In our case, |
|
951 |
\isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:% |
|
952 |
\end{isamarkuptext}% |
|
953 |
\isamarkuptrue% |
|
954 |
\isacommand{lemma}\isamarkupfalse% |
|
955 |
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
956 |
\ \ {\isachardoublequoteopen}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymlongleftrightarrow}\isanewline |
|
957 |
\ \ \ \ \ 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 |
|
958 |
% |
|
959 |
\isadelimproof |
|
960 |
\ \ % |
|
961 |
\endisadelimproof |
|
962 |
% |
|
963 |
\isatagproof |
|
964 |
\isacommand{by}\isamarkupfalse% |
|
965 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}% |
|
966 |
\endisatagproof |
|
967 |
{\isafoldproof}% |
|
968 |
% |
|
969 |
\isadelimproof |
|
970 |
% |
|
971 |
\endisadelimproof |
|
972 |
% |
|
973 |
\begin{isamarkuptext}% |
|
974 |
does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:% |
|
975 |
\end{isamarkuptext}% |
|
976 |
\isamarkuptrue% |
|
24379 | 977 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22885 | 978 |
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isacharcolon}{\isacharcolon}\ monotype\ {\isasymRightarrow}\ monotype\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline |
979 |
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}monotype{\isachardot}ML{\isachardoublequoteclose}% |
|
980 |
\begin{isamarkuptext}% |
|
981 |
\lstsml{Thy/examples/monotype.ML}% |
|
22798 | 982 |
\end{isamarkuptext}% |
983 |
\isamarkuptrue% |
|
984 |
% |
|
985 |
\isamarkupsubsection{Programs as sets of theorems% |
|
986 |
} |
|
987 |
\isamarkuptrue% |
|
988 |
% |
|
989 |
\begin{isamarkuptext}% |
|
990 |
As told in \secref{sec:concept}, code generation is based |
|
991 |
on a structured collection of code theorems. |
|
992 |
For explorative purpose, this collection |
|
993 |
may be inspected using the \isa{{\isasymCODETHMS}} command:% |
|
994 |
\end{isamarkuptext}% |
|
995 |
\isamarkuptrue% |
|
996 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
|
997 |
\ {\isachardoublequoteopen}op\ mod\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}% |
|
998 |
\begin{isamarkuptext}% |
|
999 |
\noindent prints a table with \emph{all} defining equations |
|
1000 |
for \isa{op\ mod}, including |
|
1001 |
\emph{all} defining equations those equations depend |
|
1002 |
on recursivly. \isa{{\isasymCODETHMS}} provides a convenient |
|
1003 |
mechanism to inspect the impact of a preprocessor setup |
|
1004 |
on defining equations. |
|
1005 |
||
1006 |
Similarly, the \isa{{\isasymCODEDEPS}} command shows a graph |
|
1007 |
visualizing dependencies between defining equations.% |
|
1008 |
\end{isamarkuptext}% |
|
1009 |
\isamarkuptrue% |
|
1010 |
% |
|
25411 | 1011 |
\isamarkupsubsection{Constructor sets for datatypes% |
1012 |
} |
|
1013 |
\isamarkuptrue% |
|
1014 |
% |
|
1015 |
\begin{isamarkuptext}% |
|
1016 |
Conceptually, any datatype is spanned by a set of |
|
1017 |
\emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n} |
|
1018 |
where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all} |
|
1019 |
type variables in \isa{{\isasymtau}}. The HOL datatype package |
|
1020 |
by default registers any new datatype in the table |
|
1021 |
of datatypes, which may be inspected using |
|
1022 |
the \isa{{\isasymPRINTCODESETUP}} command. |
|
1023 |
||
1024 |
In some cases, it may be convenient to alter or |
|
1025 |
extend this table; as an example, we show |
|
1026 |
how to implement finite sets by lists |
|
1027 |
using the conversion \isa{{\isachardoublequote}set\ {\isasymColon}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ set{\isachardoublequote}} |
|
1028 |
as constructor:% |
|
1029 |
\end{isamarkuptext}% |
|
1030 |
\isamarkuptrue% |
|
1031 |
\ % |
|
1032 |
\isadelimML |
|
1033 |
% |
|
1034 |
\endisadelimML |
|
1035 |
% |
|
1036 |
\isatagML |
|
1037 |
% |
|
1038 |
\endisatagML |
|
1039 |
{\isafoldML}% |
|
1040 |
% |
|
1041 |
\isadelimML |
|
1042 |
% |
|
1043 |
\endisadelimML |
|
1044 |
\isanewline |
|
1045 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse% |
|
1046 |
\ set\isanewline |
|
1047 |
\isanewline |
|
1048 |
\isacommand{lemma}\isamarkupfalse% |
|
1049 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}\ {\isacharequal}\ set\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}% |
|
1050 |
\isadelimproof |
|
1051 |
\ % |
|
1052 |
\endisadelimproof |
|
1053 |
% |
|
1054 |
\isatagproof |
|
1055 |
\isacommand{by}\isamarkupfalse% |
|
1056 |
\ simp% |
|
1057 |
\endisatagproof |
|
1058 |
{\isafoldproof}% |
|
1059 |
% |
|
1060 |
\isadelimproof |
|
1061 |
% |
|
1062 |
\endisadelimproof |
|
1063 |
\isanewline |
|
1064 |
\isanewline |
|
1065 |
\isacommand{lemma}\isamarkupfalse% |
|
1066 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}insert\ x\ {\isacharparenleft}set\ xs{\isacharparenright}\ {\isacharequal}\ set\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}{\isachardoublequoteclose}% |
|
1067 |
\isadelimproof |
|
1068 |
\ % |
|
1069 |
\endisadelimproof |
|
1070 |
% |
|
1071 |
\isatagproof |
|
1072 |
\isacommand{by}\isamarkupfalse% |
|
1073 |
\ simp% |
|
1074 |
\endisatagproof |
|
1075 |
{\isafoldproof}% |
|
1076 |
% |
|
1077 |
\isadelimproof |
|
1078 |
% |
|
1079 |
\endisadelimproof |
|
1080 |
\isanewline |
|
1081 |
\isanewline |
|
1082 |
\isacommand{lemma}\isamarkupfalse% |
|
1083 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
|
1084 |
\isadelimproof |
|
1085 |
\ % |
|
1086 |
\endisadelimproof |
|
1087 |
% |
|
1088 |
\isatagproof |
|
1089 |
\isacommand{by}\isamarkupfalse% |
|
1090 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
1091 |
\endisatagproof |
|
1092 |
{\isafoldproof}% |
|
1093 |
% |
|
1094 |
\isadelimproof |
|
1095 |
% |
|
1096 |
\endisadelimproof |
|
1097 |
\isanewline |
|
1098 |
\isanewline |
|
1099 |
\isacommand{lemma}\isamarkupfalse% |
|
1100 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}xs\ {\isasymunion}\ set\ ys\ {\isacharequal}\ foldr\ insert\ ys\ xs{\isachardoublequoteclose}% |
|
1101 |
\isadelimproof |
|
1102 |
\ % |
|
1103 |
\endisadelimproof |
|
1104 |
% |
|
1105 |
\isatagproof |
|
1106 |
\isacommand{by}\isamarkupfalse% |
|
1107 |
\ {\isacharparenleft}induct\ ys{\isacharparenright}\ simp{\isacharunderscore}all% |
|
1108 |
\endisatagproof |
|
1109 |
{\isafoldproof}% |
|
1110 |
% |
|
1111 |
\isadelimproof |
|
1112 |
% |
|
1113 |
\endisadelimproof |
|
1114 |
\isanewline |
|
1115 |
\isanewline |
|
1116 |
\isacommand{lemma}\isamarkupfalse% |
|
1117 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymUnion}set\ xs\ {\isacharequal}\ foldr\ {\isacharparenleft}op\ {\isasymunion}{\isacharparenright}\ xs\ {\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}% |
|
1118 |
\isadelimproof |
|
1119 |
\ % |
|
1120 |
\endisadelimproof |
|
1121 |
% |
|
1122 |
\isatagproof |
|
1123 |
\isacommand{by}\isamarkupfalse% |
|
1124 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
1125 |
\endisatagproof |
|
1126 |
{\isafoldproof}% |
|
1127 |
% |
|
1128 |
\isadelimproof |
|
1129 |
% |
|
1130 |
\endisadelimproof |
|
1131 |
\isanewline |
|
1132 |
\isanewline |
|
1133 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
|
1134 |
\ {\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}% |
|
1135 |
\begin{isamarkuptext}% |
|
1136 |
\lstsml{Thy/examples/set_list.ML} |
|
1137 |
||
1138 |
\medskip |
|
1139 |
||
1140 |
Changing the representation of existing datatypes requires |
|
1141 |
some care with respect to pattern matching and such.% |
|
1142 |
\end{isamarkuptext}% |
|
1143 |
\isamarkuptrue% |
|
1144 |
% |
|
21172 | 1145 |
\isamarkupsubsection{Customizing serialization% |
1146 |
} |
|
1147 |
\isamarkuptrue% |
|
1148 |
% |
|
22798 | 1149 |
\isamarkupsubsubsection{Basics% |
1150 |
} |
|
1151 |
\isamarkuptrue% |
|
1152 |
% |
|
21172 | 1153 |
\begin{isamarkuptext}% |
1154 |
Consider the following function and its corresponding |
|
1155 |
SML code:% |
|
1156 |
\end{isamarkuptext}% |
|
1157 |
\isamarkuptrue% |
|
1158 |
\isacommand{fun}\isamarkupfalse% |
|
1159 |
\isanewline |
|
1160 |
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
22798 | 1161 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}% |
21348 | 1162 |
\isadelimtt |
21172 | 1163 |
% |
21348 | 1164 |
\endisadelimtt |
1165 |
% |
|
1166 |
\isatagtt |
|
21172 | 1167 |
% |
21348 | 1168 |
\endisatagtt |
1169 |
{\isafoldtt}% |
|
1170 |
% |
|
1171 |
\isadelimtt |
|
21172 | 1172 |
% |
21348 | 1173 |
\endisadelimtt |
24379 | 1174 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 1175 |
\ in{\isacharunderscore}interval\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1176 |
\begin{isamarkuptext}% |
21348 | 1177 |
\lstsml{Thy/examples/bool_literal.ML} |
21172 | 1178 |
|
22798 | 1179 |
\noindent Though this is correct code, it is a little bit unsatisfactory: |
21172 | 1180 |
boolean values and operators are materialized as distinguished |
1181 |
entities with have nothing to do with the SML-builtin notion |
|
1182 |
of \qt{bool}. This results in less readable code; |
|
1183 |
additionally, eager evaluation may cause programs to |
|
1184 |
loop or break which would perfectly terminate when |
|
1185 |
the existing SML \qt{bool} would be used. To map |
|
1186 |
the HOL \qt{bool} on SML \qt{bool}, we may use |
|
1187 |
\qn{custom serializations}:% |
|
1188 |
\end{isamarkuptext}% |
|
1189 |
\isamarkuptrue% |
|
21348 | 1190 |
% |
1191 |
\isadelimtt |
|
1192 |
% |
|
1193 |
\endisadelimtt |
|
1194 |
% |
|
1195 |
\isatagtt |
|
21172 | 1196 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1197 |
\ bool\isanewline |
|
1198 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1199 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1200 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
1201 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1202 |
\endisatagtt |
1203 |
{\isafoldtt}% |
|
1204 |
% |
|
1205 |
\isadelimtt |
|
1206 |
% |
|
1207 |
\endisadelimtt |
|
1208 |
% |
|
21172 | 1209 |
\begin{isamarkuptext}% |
21348 | 1210 |
The \isa{{\isasymCODETYPE}} commad takes a type constructor |
21172 | 1211 |
as arguments together with a list of custom serializations. |
1212 |
Each custom serialization starts with a target language |
|
1213 |
identifier followed by an expression, which during |
|
1214 |
code serialization is inserted whenever the type constructor |
|
21348 | 1215 |
would occur. For constants, \isa{{\isasymCODECONST}} implements |
1216 |
the corresponding mechanism. Each ``\verb|_|'' in |
|
21172 | 1217 |
a serialization expression is treated as a placeholder |
1218 |
for the type constructor's (the constant's) arguments.% |
|
1219 |
\end{isamarkuptext}% |
|
1220 |
\isamarkuptrue% |
|
24379 | 1221 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 1222 |
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1223 |
\begin{isamarkuptext}% |
21348 | 1224 |
\lstsml{Thy/examples/bool_mlbool.ML} |
21172 | 1225 |
|
22798 | 1226 |
\noindent This still is not perfect: the parentheses |
21348 | 1227 |
around the \qt{andalso} expression are superfluous. |
1228 |
Though the serializer |
|
21172 | 1229 |
by no means attempts to imitate the rich Isabelle syntax |
1230 |
framework, it provides some common idioms, notably |
|
1231 |
associative infixes with precedences which may be used here:% |
|
1232 |
\end{isamarkuptext}% |
|
1233 |
\isamarkuptrue% |
|
21348 | 1234 |
% |
1235 |
\isadelimtt |
|
1236 |
% |
|
1237 |
\endisadelimtt |
|
1238 |
% |
|
1239 |
\isatagtt |
|
21172 | 1240 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
1241 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
21348 | 1242 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
1243 |
\endisatagtt |
|
1244 |
{\isafoldtt}% |
|
1245 |
% |
|
1246 |
\isadelimtt |
|
1247 |
% |
|
1248 |
\endisadelimtt |
|
1249 |
\isanewline |
|
21172 | 1250 |
\isanewline |
24379 | 1251 |
\isacommand{export{\isacharunderscore}code}\isamarkupfalse% |
22845 | 1252 |
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1253 |
\begin{isamarkuptext}% |
21348 | 1254 |
\lstsml{Thy/examples/bool_infix.ML} |
21172 | 1255 |
|
22798 | 1256 |
\medskip |
1257 |
||
21172 | 1258 |
Next, we try to map HOL pairs to SML pairs, using the |
21348 | 1259 |
infix ``\verb|*|'' type constructor and parentheses:% |
21172 | 1260 |
\end{isamarkuptext}% |
1261 |
\isamarkuptrue% |
|
21348 | 1262 |
% |
1263 |
\isadelimtt |
|
1264 |
% |
|
1265 |
\endisadelimtt |
|
1266 |
% |
|
1267 |
\isatagtt |
|
21172 | 1268 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1269 |
\ {\isacharasterisk}\isanewline |
|
1270 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1271 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1272 |
\ Pair\isanewline |
|
1273 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1274 |
\endisatagtt |
1275 |
{\isafoldtt}% |
|
1276 |
% |
|
1277 |
\isadelimtt |
|
1278 |
% |
|
1279 |
\endisadelimtt |
|
1280 |
% |
|
21172 | 1281 |
\begin{isamarkuptext}% |
21348 | 1282 |
The initial bang ``\verb|!|'' tells the serializer to never put |
21172 | 1283 |
parentheses around the whole expression (they are already present), |
1284 |
while the parentheses around argument place holders |
|
1285 |
tell not to put parentheses around the arguments. |
|
21348 | 1286 |
The slash ``\verb|/|'' (followed by arbitrary white space) |
21172 | 1287 |
inserts a space which may be used as a break if necessary |
1288 |
during pretty printing. |
|
1289 |
||
22798 | 1290 |
These examples give a glimpse what mechanisms |
21186 | 1291 |
custom serializations provide; however their usage |
1292 |
requires careful thinking in order not to introduce |
|
1293 |
inconsistencies -- or, in other words: |
|
1294 |
custom serializations are completely axiomatic. |
|
1295 |
||
1296 |
A further noteworthy details is that any special |
|
1297 |
character in a custom serialization may be quoted |
|
21348 | 1298 |
using ``\verb|'|''; thus, in |
1299 |
``\verb|fn '_ => _|'' the first |
|
1300 |
``\verb|_|'' is a proper underscore while the |
|
1301 |
second ``\verb|_|'' is a placeholder. |
|
21186 | 1302 |
|
1303 |
The HOL theories provide further |
|
25411 | 1304 |
examples for custom serializations.% |
21186 | 1305 |
\end{isamarkuptext}% |
21172 | 1306 |
\isamarkuptrue% |
1307 |
% |
|
21186 | 1308 |
\isamarkupsubsubsection{Haskell serialization% |
1309 |
} |
|
1310 |
\isamarkuptrue% |
|
1311 |
% |
|
1312 |
\begin{isamarkuptext}% |
|
1313 |
For convenience, the default |
|
1314 |
HOL setup for Haskell maps the \isa{eq} class to |
|
1315 |
its counterpart in Haskell, giving custom serializations |
|
21348 | 1316 |
for the class (\isa{{\isasymCODECLASS}}) and its operation:% |
21186 | 1317 |
\end{isamarkuptext}% |
1318 |
\isamarkuptrue% |
|
1319 |
% |
|
21348 | 1320 |
\isadelimtt |
1321 |
% |
|
1322 |
\endisadelimtt |
|
1323 |
% |
|
1324 |
\isatagtt |
|
21186 | 1325 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
1326 |
\ eq\isanewline |
|
22798 | 1327 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
21186 | 1328 |
\isanewline |
1329 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
22798 | 1330 |
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline |
1331 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1332 |
\endisatagtt |
1333 |
{\isafoldtt}% |
|
1334 |
% |
|
1335 |
\isadelimtt |
|
1336 |
% |
|
1337 |
\endisadelimtt |
|
1338 |
% |
|
21186 | 1339 |
\begin{isamarkuptext}% |
1340 |
A problem now occurs whenever a type which |
|
1341 |
is an instance of \isa{eq} in HOL is mapped |
|
1342 |
on a Haskell-builtin type which is also an instance |
|
1343 |
of Haskell \isa{Eq}:% |
|
1344 |
\end{isamarkuptext}% |
|
1345 |
\isamarkuptrue% |
|
1346 |
\isacommand{typedecl}\isamarkupfalse% |
|
1347 |
\ bar\isanewline |
|
1348 |
\isanewline |
|
1349 |
\isacommand{instance}\isamarkupfalse% |
|
1350 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1351 |
\isadelimproof |
|
1352 |
\ % |
|
1353 |
\endisadelimproof |
|
1354 |
% |
|
1355 |
\isatagproof |
|
1356 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1357 |
% |
|
1358 |
\endisatagproof |
|
1359 |
{\isafoldproof}% |
|
1360 |
% |
|
1361 |
\isadelimproof |
|
21172 | 1362 |
% |
21186 | 1363 |
\endisadelimproof |
1364 |
\isanewline |
|
21348 | 1365 |
% |
1366 |
\isadelimtt |
|
21186 | 1367 |
\isanewline |
21348 | 1368 |
% |
1369 |
\endisadelimtt |
|
1370 |
% |
|
1371 |
\isatagtt |
|
21186 | 1372 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1373 |
\ bar\isanewline |
|
1374 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1375 |
\endisatagtt |
1376 |
{\isafoldtt}% |
|
1377 |
% |
|
1378 |
\isadelimtt |
|
1379 |
% |
|
1380 |
\endisadelimtt |
|
1381 |
% |
|
21186 | 1382 |
\begin{isamarkuptext}% |
1383 |
The code generator would produce |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1384 |
an additional instance, which of course is rejected. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1385 |
To suppress this additional instance, use |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1386 |
\isa{{\isasymCODEINSTANCE}}:% |
21186 | 1387 |
\end{isamarkuptext}% |
1388 |
\isamarkuptrue% |
|
21348 | 1389 |
% |
1390 |
\isadelimtt |
|
1391 |
% |
|
1392 |
\endisadelimtt |
|
1393 |
% |
|
1394 |
\isatagtt |
|
21186 | 1395 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
1396 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
1397 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
|
21348 | 1398 |
\endisatagtt |
1399 |
{\isafoldtt}% |
|
1400 |
% |
|
1401 |
\isadelimtt |
|
1402 |
% |
|
1403 |
\endisadelimtt |
|
1404 |
% |
|
22798 | 1405 |
\isamarkupsubsubsection{Pretty printing% |
1406 |
} |
|
1407 |
\isamarkuptrue% |
|
1408 |
% |
|
1409 |
\begin{isamarkuptext}% |
|
1410 |
The serializer provides ML interfaces to set up |
|
1411 |
pretty serializations for expressions like lists, numerals |
|
1412 |
and characters; these are |
|
1413 |
monolithic stubs and should only be used with the |
|
1414 |
theories introduces in \secref{sec:library}.% |
|
1415 |
\end{isamarkuptext}% |
|
1416 |
\isamarkuptrue% |
|
1417 |
% |
|
21348 | 1418 |
\isamarkupsubsection{Cyclic module dependencies% |
1419 |
} |
|
1420 |
\isamarkuptrue% |
|
1421 |
% |
|
1422 |
\begin{isamarkuptext}% |
|
1423 |
Sometimes the awkward situation occurs that dependencies |
|
1424 |
between definitions introduce cyclic dependencies |
|
1425 |
between modules, which in the Haskell world leaves |
|
1426 |
you to the mercy of the Haskell implementation you are using, |
|
1427 |
while for SML code generation is not possible. |
|
1428 |
||
1429 |
A solution is to declare module names explicitly. |
|
1430 |
Let use assume the three cyclically dependent |
|
1431 |
modules are named \emph{A}, \emph{B} and \emph{C}. |
|
1432 |
Then, by stating% |
|
1433 |
\end{isamarkuptext}% |
|
1434 |
\isamarkuptrue% |
|
1435 |
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse% |
|
1436 |
\ SML\isanewline |
|
1437 |
\ \ A\ ABC\isanewline |
|
1438 |
\ \ B\ ABC\isanewline |
|
1439 |
\ \ C\ ABC% |
|
1440 |
\begin{isamarkuptext}% |
|
1441 |
we explicitly map all those modules on \emph{ABC}, |
|
1442 |
resulting in an ad-hoc merge of this three modules |
|
1443 |
at serialization time.% |
|
1444 |
\end{isamarkuptext}% |
|
1445 |
\isamarkuptrue% |
|
1446 |
% |
|
22798 | 1447 |
\isamarkupsubsection{Incremental code generation% |
1448 |
} |
|
1449 |
\isamarkuptrue% |
|
1450 |
% |
|
1451 |
\begin{isamarkuptext}% |
|
1452 |
Code generation is \emph{incremental}: theorems |
|
1453 |
and abstract intermediate code are cached and extended on demand. |
|
1454 |
The cache may be partially or fully dropped if the underlying |
|
1455 |
executable content of the theory changes. |
|
1456 |
Implementation of caching is supposed to transparently |
|
1457 |
hid away the details from the user. Anyway, caching |
|
1458 |
reaches the surface by using a slightly more general form |
|
1459 |
of the \isa{{\isasymCODETHMS}}, \isa{{\isasymCODEDEPS}} |
|
24379 | 1460 |
and \isa{{\isasymEXPORTCODE}} commands: the list of constants |
22798 | 1461 |
may be omitted. Then, all constants with code theorems |
1462 |
in the current cache are referred to.% |
|
1463 |
\end{isamarkuptext}% |
|
1464 |
\isamarkuptrue% |
|
1465 |
% |
|
21172 | 1466 |
\isamarkupsection{ML interfaces \label{sec:ml}% |
1467 |
} |
|
1468 |
\isamarkuptrue% |
|
1469 |
% |
|
21348 | 1470 |
\begin{isamarkuptext}% |
1471 |
Since the code generator framework not only aims to provide |
|
1472 |
a nice Isar interface but also to form a base for |
|
1473 |
code-generation-based applications, here a short |
|
1474 |
description of the most important ML interfaces.% |
|
1475 |
\end{isamarkuptext}% |
|
1476 |
\isamarkuptrue% |
|
1477 |
% |
|
24217 | 1478 |
\isamarkupsubsection{Executable theory content: \isa{Code}% |
21172 | 1479 |
} |
1480 |
\isamarkuptrue% |
|
1481 |
% |
|
1482 |
\begin{isamarkuptext}% |
|
1483 |
This Pure module implements the core notions of |
|
1484 |
executable content of a theory.% |
|
1485 |
\end{isamarkuptext}% |
|
1486 |
\isamarkuptrue% |
|
1487 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1488 |
\isamarkupsubsubsection{Managing executable content% |
20967 | 1489 |
} |
1490 |
\isamarkuptrue% |
|
1491 |
% |
|
21172 | 1492 |
\isadelimmlref |
1493 |
% |
|
1494 |
\endisadelimmlref |
|
1495 |
% |
|
1496 |
\isatagmlref |
|
1497 |
% |
|
1498 |
\begin{isamarkuptext}% |
|
1499 |
\begin{mldecls} |
|
25370 | 1500 |
\indexml{Code.add-func}\verb|Code.add_func: thm -> theory -> theory| \\ |
24217 | 1501 |
\indexml{Code.del-func}\verb|Code.del_func: thm -> theory -> theory| \\ |
24421 | 1502 |
\indexml{Code.add-funcl}\verb|Code.add_funcl: string * thm list Susp.T -> theory -> theory| \\ |
24217 | 1503 |
\indexml{Code.add-inline}\verb|Code.add_inline: thm -> theory -> theory| \\ |
1504 |
\indexml{Code.del-inline}\verb|Code.del_inline: thm -> theory -> theory| \\ |
|
1505 |
\indexml{Code.add-inline-proc}\verb|Code.add_inline_proc: string * (theory -> cterm list -> thm list)|\isasep\isanewline% |
|
21348 | 1506 |
\verb| -> theory -> theory| \\ |
24217 | 1507 |
\indexml{Code.del-inline-proc}\verb|Code.del_inline_proc: string -> theory -> theory| \\ |
1508 |
\indexml{Code.add-preproc}\verb|Code.add_preproc: string * (theory -> thm list -> thm list)|\isasep\isanewline% |
|
21348 | 1509 |
\verb| -> theory -> theory| \\ |
24217 | 1510 |
\indexml{Code.del-preproc}\verb|Code.del_preproc: string -> theory -> theory| \\ |
24421 | 1511 |
\indexml{Code.add-datatype}\verb|Code.add_datatype: (string * typ) list -> theory -> theory| \\ |
24217 | 1512 |
\indexml{Code.get-datatype}\verb|Code.get_datatype: theory -> string|\isasep\isanewline% |
22479 | 1513 |
\verb| -> (string * sort) list * (string * typ list) list| \\ |
24421 | 1514 |
\indexml{Code.get-datatype-of-constr}\verb|Code.get_datatype_of_constr: theory -> string -> string option| |
21172 | 1515 |
\end{mldecls} |
1516 |
||
1517 |
\begin{description} |
|
1518 |
||
24217 | 1519 |
\item \verb|Code.add_func|~\isa{thm}~\isa{thy} adds function |
21348 | 1520 |
theorem \isa{thm} to executable content. |
1521 |
||
24217 | 1522 |
\item \verb|Code.del_func|~\isa{thm}~\isa{thy} removes function |
21348 | 1523 |
theorem \isa{thm} from executable content, if present. |
1524 |
||
24217 | 1525 |
\item \verb|Code.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds |
22060 | 1526 |
suspended defining equations \isa{lthms} for constant |
21348 | 1527 |
\isa{const} to executable content. |
1528 |
||
24217 | 1529 |
\item \verb|Code.add_inline|~\isa{thm}~\isa{thy} adds |
21348 | 1530 |
inlining theorem \isa{thm} to executable content. |
1531 |
||
24217 | 1532 |
\item \verb|Code.del_inline|~\isa{thm}~\isa{thy} remove |
21348 | 1533 |
inlining theorem \isa{thm} from executable content, if present. |
1534 |
||
24217 | 1535 |
\item \verb|Code.add_inline_proc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
22060 | 1536 |
inline procedure \isa{f} (named \isa{name}) to executable content; |
21348 | 1537 |
\isa{f} is a computation of rewrite rules dependent on |
1538 |
the current theory context and the list of all arguments |
|
22060 | 1539 |
and right hand sides of the defining equations belonging |
21348 | 1540 |
to a certain function definition. |
1541 |
||
24217 | 1542 |
\item \verb|Code.del_inline_proc|~\isa{name}~\isa{thy} removes |
22060 | 1543 |
inline procedure named \isa{name} from executable content. |
1544 |
||
24217 | 1545 |
\item \verb|Code.add_preproc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
22060 | 1546 |
generic preprocessor \isa{f} (named \isa{name}) to executable content; |
1547 |
\isa{f} is a transformation of the defining equations belonging |
|
21348 | 1548 |
to a certain function definition, depending on the |
1549 |
current theory context. |
|
1550 |
||
24217 | 1551 |
\item \verb|Code.del_preproc|~\isa{name}~\isa{thy} removes |
22060 | 1552 |
generic preprcoessor named \isa{name} from executable content. |
1553 |
||
24421 | 1554 |
\item \verb|Code.add_datatype|~\isa{cs}~\isa{thy} adds |
1555 |
a datatype to executable content, with generation |
|
1556 |
set \isa{cs}. |
|
21348 | 1557 |
|
24217 | 1558 |
\item \verb|Code.get_datatype_of_constr|~\isa{thy}~\isa{const} |
21348 | 1559 |
returns type constructor corresponding to |
1560 |
constructor \isa{const}; returns \isa{NONE} |
|
1561 |
if \isa{const} is no constructor. |
|
1562 |
||
1563 |
\end{description}% |
|
1564 |
\end{isamarkuptext}% |
|
1565 |
\isamarkuptrue% |
|
1566 |
% |
|
1567 |
\endisatagmlref |
|
1568 |
{\isafoldmlref}% |
|
1569 |
% |
|
1570 |
\isadelimmlref |
|
1571 |
% |
|
1572 |
\endisadelimmlref |
|
1573 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1574 |
\isamarkupsubsection{Auxiliary% |
21172 | 1575 |
} |
1576 |
\isamarkuptrue% |
|
1577 |
% |
|
1578 |
\isadelimmlref |
|
1579 |
% |
|
1580 |
\endisadelimmlref |
|
1581 |
% |
|
1582 |
\isatagmlref |
|
1583 |
% |
|
1584 |
\begin{isamarkuptext}% |
|
1585 |
\begin{mldecls} |
|
24421 | 1586 |
\indexml{CodeUnit.read-const}\verb|CodeUnit.read_const: theory -> string -> string| \\ |
1587 |
\indexml{CodeUnit.head-func}\verb|CodeUnit.head_func: thm -> string * typ| \\ |
|
24217 | 1588 |
\indexml{CodeUnit.rewrite-func}\verb|CodeUnit.rewrite_func: thm list -> thm -> thm| \\ |
21348 | 1589 |
\end{mldecls} |
1590 |
||
1591 |
\begin{description} |
|
1592 |
||
24217 | 1593 |
\item \verb|CodeUnit.read_const|~\isa{thy}~\isa{s} |
21348 | 1594 |
reads a constant as a concrete term expression \isa{s}. |
1595 |
||
24217 | 1596 |
\item \verb|CodeUnit.head_func|~\isa{thm} |
22751 | 1597 |
extracts the constant and its type from a defining equation \isa{thm}. |
21348 | 1598 |
|
24217 | 1599 |
\item \verb|CodeUnit.rewrite_func|~\isa{rews}~\isa{thm} |
22060 | 1600 |
rewrites a defining equation \isa{thm} with a set of rewrite |
21348 | 1601 |
rules \isa{rews}; only arguments and right hand side are rewritten, |
22060 | 1602 |
not the head of the defining equation. |
21348 | 1603 |
|
1604 |
\end{description}% |
|
21172 | 1605 |
\end{isamarkuptext}% |
1606 |
\isamarkuptrue% |
|
1607 |
% |
|
1608 |
\endisatagmlref |
|
1609 |
{\isafoldmlref}% |
|
1610 |
% |
|
1611 |
\isadelimmlref |
|
1612 |
% |
|
1613 |
\endisadelimmlref |
|
1614 |
% |
|
20967 | 1615 |
\isamarkupsubsection{Implementing code generator applications% |
1616 |
} |
|
1617 |
\isamarkuptrue% |
|
1618 |
% |
|
21172 | 1619 |
\begin{isamarkuptext}% |
21348 | 1620 |
Implementing code generator applications on top |
1621 |
of the framework set out so far usually not only |
|
1622 |
involves using those primitive interfaces |
|
1623 |
but also storing code-dependent data and various |
|
1624 |
other things. |
|
1625 |
||
1626 |
\begin{warn} |
|
21172 | 1627 |
Some interfaces discussed here have not reached |
1628 |
a final state yet. |
|
1629 |
Changes likely to occur in future. |
|
21452 | 1630 |
\end{warn}% |
21172 | 1631 |
\end{isamarkuptext}% |
1632 |
\isamarkuptrue% |
|
1633 |
% |
|
1634 |
\isamarkupsubsubsection{Data depending on the theory's executable content% |
|
1635 |
} |
|
1636 |
\isamarkuptrue% |
|
1637 |
% |
|
21348 | 1638 |
\begin{isamarkuptext}% |
21452 | 1639 |
Due to incrementality of code generation, changes in the |
1640 |
theory's executable content have to be propagated in a |
|
1641 |
certain fashion. Additionally, such changes may occur |
|
1642 |
not only during theory extension but also during theory |
|
1643 |
merge, which is a little bit nasty from an implementation |
|
1644 |
point of view. The framework provides a solution |
|
1645 |
to this technical challenge by providing a functorial |
|
1646 |
data slot \verb|CodeDataFun|; on instantiation |
|
1647 |
of this functor, the following types and operations |
|
1648 |
are required: |
|
1649 |
||
1650 |
\medskip |
|
21348 | 1651 |
\begin{tabular}{l} |
1652 |
\isa{type\ T} \\ |
|
1653 |
\isa{val\ empty{\isacharcolon}\ T} \\ |
|
1654 |
\isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\ |
|
24217 | 1655 |
\isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodeUnit{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T} |
21348 | 1656 |
\end{tabular} |
1657 |
||
21452 | 1658 |
\begin{description} |
1659 |
||
1660 |
\item \isa{T} the type of data to store. |
|
1661 |
||
1662 |
\item \isa{empty} initial (empty) data. |
|
1663 |
||
1664 |
\item \isa{merge} merging two data slots. |
|
1665 |
||
22798 | 1666 |
\item \isa{purge}~\isa{thy}~\isa{consts} propagates changes in executable content; |
21452 | 1667 |
if possible, the current theory context is handed over |
1668 |
as argument \isa{thy} (if there is no current theory context (e.g.~during |
|
22798 | 1669 |
theory merge, \verb|NONE|); \isa{consts} indicates the kind |
21452 | 1670 |
of change: \verb|NONE| stands for a fundamental change |
22798 | 1671 |
which invalidates any existing code, \isa{SOME\ consts} |
1672 |
hints that executable content for constants \isa{consts} |
|
21452 | 1673 |
has changed. |
1674 |
||
1675 |
\end{description} |
|
1676 |
||
1677 |
An instance of \verb|CodeDataFun| provides the following |
|
1678 |
interface: |
|
1679 |
||
21348 | 1680 |
\medskip |
1681 |
\begin{tabular}{l} |
|
1682 |
\isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\ |
|
1683 |
\isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\ |
|
1684 |
\isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T} |
|
21452 | 1685 |
\end{tabular} |
1686 |
||
1687 |
\begin{description} |
|
1688 |
||
1689 |
\item \isa{get} retrieval of the current data. |
|
1690 |
||
1691 |
\item \isa{change} update of current data (cached!) |
|
1692 |
by giving a continuation. |
|
1693 |
||
1694 |
\item \isa{change{\isacharunderscore}yield} update with side result. |
|
1695 |
||
1696 |
\end{description}% |
|
1697 |
\end{isamarkuptext}% |
|
1698 |
\isamarkuptrue% |
|
1699 |
% |
|
1700 |
\begin{isamarkuptext}% |
|
24628 | 1701 |
\emph{Happy proving, happy hacking!}% |
21172 | 1702 |
\end{isamarkuptext}% |
1703 |
\isamarkuptrue% |
|
1704 |
% |
|
20967 | 1705 |
\isadelimtheory |
1706 |
% |
|
1707 |
\endisadelimtheory |
|
1708 |
% |
|
1709 |
\isatagtheory |
|
1710 |
\isacommand{end}\isamarkupfalse% |
|
1711 |
% |
|
1712 |
\endisatagtheory |
|
1713 |
{\isafoldtheory}% |
|
1714 |
% |
|
1715 |
\isadelimtheory |
|
1716 |
% |
|
1717 |
\endisadelimtheory |
|
1718 |
\isanewline |
|
1719 |
\end{isabellebody}% |
|
1720 |
%%% Local Variables: |
|
1721 |
%%% mode: latex |
|
1722 |
%%% TeX-master: "root" |
|
1723 |
%%% End: |