author | haftmann |
Wed, 15 Aug 2007 08:57:38 +0200 | |
changeset 24279 | 165648d5679f |
parent 24217 | 5c4913b478f5 |
child 24379 | 823ffe1fdf67 |
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% |
|
22479 | 177 |
\isacommand{code{\isacharunderscore}gen}\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% |
|
260 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
22845 | 261 |
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 262 |
\begin{isamarkuptext}% |
22550 | 263 |
\noindent The \isa{{\isasymCODEGEN}} 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 |
|
313 |
\isacommand{code{\isacharunderscore}gen}\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 |
|
378 |
\isacommand{code{\isacharunderscore}gen}\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 |
392 |
\isacommand{code{\isacharunderscore}gen}\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 |
|
423 |
\isacommand{code{\isacharunderscore}gen}\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% |
|
518 |
\isacommand{code{\isacharunderscore}gen}\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% |
|
529 |
\isacommand{code{\isacharunderscore}gen}\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% |
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
539 |
\isacommand{code{\isacharunderscore}gen}\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 |
||
22916 | 586 |
\item[\isa{Pretty{\isacharunderscore}Int}] represents \isa{HOL} integers by big |
22798 | 587 |
integer literals in target languages. |
22916 | 588 |
\item[\isa{Pretty{\isacharunderscore}Char}] represents \isa{HOL} characters by |
22798 | 589 |
character literals in target languages. |
23132 | 590 |
\item[\isa{Pretty{\isacharunderscore}Char{\isacharunderscore}chr}] like \isa{Pretty{\isacharunderscore}Char}, |
22798 | 591 |
but also offers treatment of character codes; includes |
592 |
\isa{Pretty{\isacharunderscore}Int}. |
|
24279 | 593 |
\item[\isa{Executable{\isacharunderscore}Set}] allows to generate code |
21172 | 594 |
for finite sets using lists. |
24217 | 595 |
\item[\isa{Executable{\isacharunderscore}Rat}] implements rational |
596 |
numbers. |
|
23016 | 597 |
\item[\isa{Executable{\isacharunderscore}Real}] implements a subset of real numbers, |
598 |
namly those representable by rational numbers. |
|
23850 | 599 |
\item[\isa{Efficient{\isacharunderscore}Nat}] \label{eff_nat} implements natural numbers by integers, |
21186 | 600 |
which in general will result in higher efficency; pattern |
21172 | 601 |
matching with \isa{{\isadigit{0}}} / \isa{Suc} |
22798 | 602 |
is eliminated; includes \isa{Pretty{\isacharunderscore}Int}. |
23850 | 603 |
\item[\isa{ML{\isacharunderscore}String}] provides an additional datatype \isa{mlstring}; |
22916 | 604 |
in the \isa{HOL} default setup, strings in HOL are mapped to list |
605 |
of \isa{HOL} characters in SML; values of type \isa{mlstring} are |
|
21172 | 606 |
mapped to strings in SML. |
607 |
||
22916 | 608 |
\end{description} |
609 |
||
610 |
\begin{warn} |
|
611 |
When importing any of these theories, they should form the last |
|
612 |
items in an import list. Since these theories adapt the |
|
613 |
code generator setup in a non-conservative fashion, |
|
614 |
strange effects may occur otherwise. |
|
615 |
\end{warn}% |
|
21172 | 616 |
\end{isamarkuptext}% |
617 |
\isamarkuptrue% |
|
618 |
% |
|
619 |
\isamarkupsubsection{Preprocessing% |
|
20967 | 620 |
} |
621 |
\isamarkuptrue% |
|
622 |
% |
|
21172 | 623 |
\begin{isamarkuptext}% |
624 |
Before selected function theorems are turned into abstract |
|
625 |
code, a chain of definitional transformation steps is carried |
|
21186 | 626 |
out: \emph{preprocessing}. There are three possibilities |
21172 | 627 |
to customize preprocessing: \emph{inline theorems}, |
628 |
\emph{inline procedures} and \emph{generic preprocessors}. |
|
629 |
||
630 |
\emph{Inline theorems} are rewriting rules applied to each |
|
22060 | 631 |
defining equation. Due to the interpretation of theorems |
632 |
of defining equations, rewrites are applied to the right |
|
21172 | 633 |
hand side and the arguments of the left hand side of an |
634 |
equation, but never to the constant heading the left hand side. |
|
635 |
Inline theorems may be declared an undeclared using the |
|
22845 | 636 |
\emph{code inline} or \emph{code inline del} attribute respectively. |
21172 | 637 |
Some common applications:% |
638 |
\end{isamarkuptext}% |
|
639 |
\isamarkuptrue% |
|
640 |
% |
|
641 |
\begin{itemize} |
|
22845 | 642 |
% |
643 |
\begin{isamarkuptext}% |
|
644 |
\item replacing non-executable constructs by executable ones:% |
|
645 |
\end{isamarkuptext}% |
|
646 |
\isamarkuptrue% |
|
647 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 648 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 649 |
\ \ \ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
21172 | 650 |
\isadelimproof |
651 |
\ % |
|
652 |
\endisadelimproof |
|
653 |
% |
|
654 |
\isatagproof |
|
655 |
\isacommand{by}\isamarkupfalse% |
|
656 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
657 |
\endisatagproof |
|
658 |
{\isafoldproof}% |
|
659 |
% |
|
660 |
\isadelimproof |
|
661 |
% |
|
662 |
\endisadelimproof |
|
663 |
% |
|
22845 | 664 |
\begin{isamarkuptext}% |
665 |
\item eliminating superfluous constants:% |
|
666 |
\end{isamarkuptext}% |
|
667 |
\isamarkuptrue% |
|
668 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 669 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 670 |
\ \ \ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}% |
21172 | 671 |
\isadelimproof |
672 |
\ % |
|
673 |
\endisadelimproof |
|
674 |
% |
|
675 |
\isatagproof |
|
676 |
\isacommand{by}\isamarkupfalse% |
|
677 |
\ simp% |
|
678 |
\endisatagproof |
|
679 |
{\isafoldproof}% |
|
680 |
% |
|
681 |
\isadelimproof |
|
682 |
% |
|
683 |
\endisadelimproof |
|
684 |
% |
|
22845 | 685 |
\begin{isamarkuptext}% |
686 |
\item replacing executable but inconvenient constructs:% |
|
687 |
\end{isamarkuptext}% |
|
688 |
\isamarkuptrue% |
|
689 |
\ \ \isacommand{lemma}\isamarkupfalse% |
|
21172 | 690 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline |
22845 | 691 |
\ \ \ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}% |
21172 | 692 |
\isadelimproof |
693 |
\ % |
|
694 |
\endisadelimproof |
|
695 |
% |
|
696 |
\isatagproof |
|
697 |
\isacommand{by}\isamarkupfalse% |
|
698 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
699 |
\endisatagproof |
|
700 |
{\isafoldproof}% |
|
701 |
% |
|
702 |
\isadelimproof |
|
703 |
% |
|
704 |
\endisadelimproof |
|
705 |
% |
|
706 |
\end{itemize} |
|
707 |
% |
|
708 |
\begin{isamarkuptext}% |
|
22845 | 709 |
\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
|
710 |
the \isa{{\isasymPRINTCODESETUP}} command. |
21172 | 711 |
|
712 |
\emph{Inline procedures} are a generalized version of inline |
|
713 |
theorems written in ML -- rewrite rules are generated dependent |
|
714 |
on the function theorems for a certain function. One |
|
715 |
application is the implicit expanding of \isa{nat} numerals |
|
716 |
to \isa{{\isadigit{0}}} / \isa{Suc} representation. See further |
|
717 |
\secref{sec:ml} |
|
718 |
||
719 |
\emph{Generic preprocessors} provide a most general interface, |
|
720 |
transforming a list of function theorems to another |
|
721 |
list of function theorems, provided that neither the heading |
|
722 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc} |
|
21348 | 723 |
pattern elimination implemented in |
21452 | 724 |
theory \isa{EfficientNat} (\secref{eff_nat}) uses this |
21172 | 725 |
interface. |
726 |
||
727 |
\begin{warn} |
|
728 |
The order in which single preprocessing steps are carried |
|
729 |
out currently is not specified; in particular, preprocessing |
|
21186 | 730 |
is \emph{no} fix point process. Keep this in mind when |
21172 | 731 |
setting up the preprocessor. |
732 |
||
733 |
Further, the attribute \emph{code unfold} |
|
734 |
associated with the existing code generator also applies to |
|
735 |
the new one: \emph{code unfold} implies \emph{code inline}. |
|
736 |
\end{warn}% |
|
737 |
\end{isamarkuptext}% |
|
738 |
\isamarkuptrue% |
|
739 |
% |
|
22798 | 740 |
\isamarkupsubsection{Concerning operational equality% |
741 |
} |
|
742 |
\isamarkuptrue% |
|
743 |
% |
|
744 |
\begin{isamarkuptext}% |
|
745 |
Surely you have already noticed how equality is treated |
|
746 |
by the code generator:% |
|
747 |
\end{isamarkuptext}% |
|
748 |
\isamarkuptrue% |
|
749 |
\isacommand{fun}\isamarkupfalse% |
|
750 |
\isanewline |
|
751 |
\ \ 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 |
|
752 |
\ \ \ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline |
|
753 |
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline |
|
754 |
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline |
|
755 |
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline |
|
756 |
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline |
|
757 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}% |
|
758 |
\begin{isamarkuptext}% |
|
759 |
The membership test during preprocessing is rewritten, |
|
760 |
resulting in \isa{op\ mem}, which itself |
|
761 |
performs an explicit equality check.% |
|
762 |
\end{isamarkuptext}% |
|
763 |
\isamarkuptrue% |
|
764 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
22845 | 765 |
\ collect{\isacharunderscore}duplicates\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}% |
22798 | 766 |
\begin{isamarkuptext}% |
767 |
\lstsml{Thy/examples/collect_duplicates.ML}% |
|
768 |
\end{isamarkuptext}% |
|
769 |
\isamarkuptrue% |
|
770 |
% |
|
771 |
\begin{isamarkuptext}% |
|
772 |
Obviously, polymorphic equality is implemented the Haskell |
|
773 |
way using a type class. How is this achieved? By an |
|
774 |
almost trivial definition in the HOL setup:% |
|
775 |
\end{isamarkuptext}% |
|
776 |
\isamarkuptrue% |
|
777 |
% |
|
778 |
\isadelimML |
|
779 |
% |
|
780 |
\endisadelimML |
|
781 |
% |
|
782 |
\isatagML |
|
783 |
% |
|
784 |
\endisatagML |
|
785 |
{\isafoldML}% |
|
786 |
% |
|
787 |
\isadelimML |
|
788 |
% |
|
789 |
\endisadelimML |
|
790 |
\isacommand{class}\isamarkupfalse% |
|
791 |
\ eq\ {\isacharparenleft}\isakeyword{attach}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\ {\isacharequal}\ type% |
|
792 |
\begin{isamarkuptext}% |
|
793 |
This merely introduces a class \isa{eq} with corresponding |
|
794 |
operation \isa{op\ {\isacharequal}}; |
|
795 |
the preprocessing framework does the rest. |
|
796 |
For datatypes, instances of \isa{eq} are implicitly derived |
|
797 |
when possible. |
|
798 |
||
799 |
Though this \isa{eq} class is designed to get rarely in |
|
800 |
the way, a subtlety |
|
801 |
enters the stage when definitions of overloaded constants |
|
802 |
are dependent on operational equality. For example, let |
|
803 |
us define a lexicographic ordering on tuples:% |
|
804 |
\end{isamarkuptext}% |
|
805 |
\isamarkuptrue% |
|
806 |
% |
|
807 |
\isadelimML |
|
808 |
% |
|
809 |
\endisadelimML |
|
810 |
% |
|
811 |
\isatagML |
|
812 |
% |
|
813 |
\endisatagML |
|
814 |
{\isafoldML}% |
|
815 |
% |
|
816 |
\isadelimML |
|
817 |
% |
|
818 |
\endisadelimML |
|
819 |
\isacommand{instance}\isamarkupfalse% |
|
820 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline |
|
821 |
\ \ less{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline |
|
822 |
\ \ \ \ {\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 |
|
823 |
\ \ \ \ 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 |
|
824 |
\ \ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline |
|
825 |
\ \ \ \ {\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 |
|
826 |
\ \ \ \ 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}% |
|
827 |
\isadelimproof |
|
828 |
\ % |
|
829 |
\endisadelimproof |
|
830 |
% |
|
831 |
\isatagproof |
|
832 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
833 |
% |
|
834 |
\endisatagproof |
|
835 |
{\isafoldproof}% |
|
836 |
% |
|
837 |
\isadelimproof |
|
838 |
% |
|
839 |
\endisadelimproof |
|
840 |
\isanewline |
|
841 |
\isanewline |
|
842 |
\isacommand{lemmas}\isamarkupfalse% |
|
22845 | 843 |
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\isanewline |
22798 | 844 |
\isanewline |
845 |
\isacommand{lemma}\isamarkupfalse% |
|
846 |
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
847 |
\ \ {\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 |
|
848 |
\ \ {\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 |
|
849 |
% |
|
850 |
\isadelimproof |
|
851 |
\ \ % |
|
852 |
\endisadelimproof |
|
853 |
% |
|
854 |
\isatagproof |
|
855 |
\isacommand{unfolding}\isamarkupfalse% |
|
856 |
\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse% |
|
857 |
\ simp{\isacharunderscore}all% |
|
858 |
\endisatagproof |
|
859 |
{\isafoldproof}% |
|
860 |
% |
|
861 |
\isadelimproof |
|
862 |
% |
|
863 |
\endisadelimproof |
|
864 |
% |
|
865 |
\begin{isamarkuptext}% |
|
866 |
Then code generation will fail. Why? The definition |
|
867 |
of \isa{op\ {\isasymle}} depends on equality on both arguments, |
|
868 |
which are polymorphic and impose an additional \isa{eq} |
|
869 |
class constraint, thus violating the type discipline |
|
870 |
for class operations. |
|
871 |
||
872 |
The solution is to add \isa{eq} explicitly to the first sort arguments in the |
|
873 |
code theorems:% |
|
874 |
\end{isamarkuptext}% |
|
875 |
\isamarkuptrue% |
|
876 |
\isacommand{lemma}\isamarkupfalse% |
|
877 |
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\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}\ {\isacharless}\ {\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}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline |
|
880 |
\ \ {\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 |
|
881 |
\ \ \ \ 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 |
|
882 |
% |
|
883 |
\isadelimproof |
|
884 |
\ \ % |
|
885 |
\endisadelimproof |
|
886 |
% |
|
887 |
\isatagproof |
|
888 |
\isacommand{unfolding}\isamarkupfalse% |
|
889 |
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse% |
|
890 |
\ rule{\isacharplus}% |
|
891 |
\endisatagproof |
|
892 |
{\isafoldproof}% |
|
893 |
% |
|
894 |
\isadelimproof |
|
895 |
% |
|
896 |
\endisadelimproof |
|
897 |
% |
|
898 |
\begin{isamarkuptext}% |
|
899 |
\noindent Then code generation succeeds:% |
|
900 |
\end{isamarkuptext}% |
|
901 |
\isamarkuptrue% |
|
902 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
903 |
\ {\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 | 904 |
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}% |
22798 | 905 |
\begin{isamarkuptext}% |
906 |
\lstsml{Thy/examples/lexicographic.ML}% |
|
907 |
\end{isamarkuptext}% |
|
908 |
\isamarkuptrue% |
|
909 |
% |
|
910 |
\begin{isamarkuptext}% |
|
911 |
In general, code theorems for overloaded constants may have more |
|
912 |
restrictive sort constraints than the underlying instance relation |
|
913 |
between class and type constructor as long as the whole system of |
|
914 |
constraints is coregular; code theorems violating coregularity |
|
915 |
are rejected immediately. Consequently, it might be necessary |
|
916 |
to delete disturbing theorems in the code theorem table, |
|
917 |
as we have done here with the original definitions \isa{less{\isacharunderscore}prod{\isacharunderscore}def} |
|
22885 | 918 |
and \isa{less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def}. |
919 |
||
920 |
In some cases, the automatically derived defining equations |
|
921 |
for equality on a particular type may not be appropriate. |
|
922 |
As example, watch the following datatype representing |
|
23132 | 923 |
monomorphic parametric types (where type constructors |
924 |
are referred to by natural numbers):% |
|
22885 | 925 |
\end{isamarkuptext}% |
926 |
\isamarkuptrue% |
|
927 |
\isacommand{datatype}\isamarkupfalse% |
|
23132 | 928 |
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}% |
22885 | 929 |
\isadelimproof |
930 |
% |
|
931 |
\endisadelimproof |
|
932 |
% |
|
933 |
\isatagproof |
|
934 |
% |
|
935 |
\endisatagproof |
|
936 |
{\isafoldproof}% |
|
937 |
% |
|
938 |
\isadelimproof |
|
939 |
% |
|
940 |
\endisadelimproof |
|
941 |
% |
|
942 |
\begin{isamarkuptext}% |
|
943 |
Then code generation for SML would fail with a message |
|
944 |
that the generated code conains illegal mutual dependencies: |
|
23132 | 945 |
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 | 946 |
instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires |
23132 | 947 |
\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 | 948 |
recursive \isa{instance} and \isa{function} definitions, |
949 |
but the SML serializer does not support this. |
|
950 |
||
951 |
In such cases, you have to provide you own equality equations |
|
952 |
involving auxiliary constants. In our case, |
|
953 |
\isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:% |
|
954 |
\end{isamarkuptext}% |
|
955 |
\isamarkuptrue% |
|
956 |
\isacommand{lemma}\isamarkupfalse% |
|
957 |
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline |
|
958 |
\ \ {\isachardoublequoteopen}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymlongleftrightarrow}\isanewline |
|
959 |
\ \ \ \ \ 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 |
|
960 |
% |
|
961 |
\isadelimproof |
|
962 |
\ \ % |
|
963 |
\endisadelimproof |
|
964 |
% |
|
965 |
\isatagproof |
|
966 |
\isacommand{by}\isamarkupfalse% |
|
967 |
\ {\isacharparenleft}simp\ add{\isacharcolon}\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}% |
|
968 |
\endisatagproof |
|
969 |
{\isafoldproof}% |
|
970 |
% |
|
971 |
\isadelimproof |
|
972 |
% |
|
973 |
\endisadelimproof |
|
974 |
% |
|
975 |
\begin{isamarkuptext}% |
|
976 |
does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:% |
|
977 |
\end{isamarkuptext}% |
|
978 |
\isamarkuptrue% |
|
979 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
980 |
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isacharcolon}{\isacharcolon}\ monotype\ {\isasymRightarrow}\ monotype\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline |
|
981 |
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}monotype{\isachardot}ML{\isachardoublequoteclose}% |
|
982 |
\begin{isamarkuptext}% |
|
983 |
\lstsml{Thy/examples/monotype.ML}% |
|
22798 | 984 |
\end{isamarkuptext}% |
985 |
\isamarkuptrue% |
|
986 |
% |
|
987 |
\isamarkupsubsection{Programs as sets of theorems% |
|
988 |
} |
|
989 |
\isamarkuptrue% |
|
990 |
% |
|
991 |
\begin{isamarkuptext}% |
|
992 |
As told in \secref{sec:concept}, code generation is based |
|
993 |
on a structured collection of code theorems. |
|
994 |
For explorative purpose, this collection |
|
995 |
may be inspected using the \isa{{\isasymCODETHMS}} command:% |
|
996 |
\end{isamarkuptext}% |
|
997 |
\isamarkuptrue% |
|
998 |
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse% |
|
999 |
\ {\isachardoublequoteopen}op\ mod\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}% |
|
1000 |
\begin{isamarkuptext}% |
|
1001 |
\noindent prints a table with \emph{all} defining equations |
|
1002 |
for \isa{op\ mod}, including |
|
1003 |
\emph{all} defining equations those equations depend |
|
1004 |
on recursivly. \isa{{\isasymCODETHMS}} provides a convenient |
|
1005 |
mechanism to inspect the impact of a preprocessor setup |
|
1006 |
on defining equations. |
|
1007 |
||
1008 |
Similarly, the \isa{{\isasymCODEDEPS}} command shows a graph |
|
1009 |
visualizing dependencies between defining equations.% |
|
1010 |
\end{isamarkuptext}% |
|
1011 |
\isamarkuptrue% |
|
1012 |
% |
|
21172 | 1013 |
\isamarkupsubsection{Customizing serialization% |
1014 |
} |
|
1015 |
\isamarkuptrue% |
|
1016 |
% |
|
22798 | 1017 |
\isamarkupsubsubsection{Basics% |
1018 |
} |
|
1019 |
\isamarkuptrue% |
|
1020 |
% |
|
21172 | 1021 |
\begin{isamarkuptext}% |
1022 |
Consider the following function and its corresponding |
|
1023 |
SML code:% |
|
1024 |
\end{isamarkuptext}% |
|
1025 |
\isamarkuptrue% |
|
1026 |
\isacommand{fun}\isamarkupfalse% |
|
1027 |
\isanewline |
|
1028 |
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
22798 | 1029 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}% |
21348 | 1030 |
\isadelimtt |
21172 | 1031 |
% |
21348 | 1032 |
\endisadelimtt |
1033 |
% |
|
1034 |
\isatagtt |
|
21172 | 1035 |
% |
21348 | 1036 |
\endisatagtt |
1037 |
{\isafoldtt}% |
|
1038 |
% |
|
1039 |
\isadelimtt |
|
21172 | 1040 |
% |
21348 | 1041 |
\endisadelimtt |
21172 | 1042 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
22845 | 1043 |
\ in{\isacharunderscore}interval\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1044 |
\begin{isamarkuptext}% |
21348 | 1045 |
\lstsml{Thy/examples/bool_literal.ML} |
21172 | 1046 |
|
22798 | 1047 |
\noindent Though this is correct code, it is a little bit unsatisfactory: |
21172 | 1048 |
boolean values and operators are materialized as distinguished |
1049 |
entities with have nothing to do with the SML-builtin notion |
|
1050 |
of \qt{bool}. This results in less readable code; |
|
1051 |
additionally, eager evaluation may cause programs to |
|
1052 |
loop or break which would perfectly terminate when |
|
1053 |
the existing SML \qt{bool} would be used. To map |
|
1054 |
the HOL \qt{bool} on SML \qt{bool}, we may use |
|
1055 |
\qn{custom serializations}:% |
|
1056 |
\end{isamarkuptext}% |
|
1057 |
\isamarkuptrue% |
|
21348 | 1058 |
% |
1059 |
\isadelimtt |
|
1060 |
% |
|
1061 |
\endisadelimtt |
|
1062 |
% |
|
1063 |
\isatagtt |
|
21172 | 1064 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1065 |
\ bool\isanewline |
|
1066 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1067 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1068 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
1069 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1070 |
\endisatagtt |
1071 |
{\isafoldtt}% |
|
1072 |
% |
|
1073 |
\isadelimtt |
|
1074 |
% |
|
1075 |
\endisadelimtt |
|
1076 |
% |
|
21172 | 1077 |
\begin{isamarkuptext}% |
21348 | 1078 |
The \isa{{\isasymCODETYPE}} commad takes a type constructor |
21172 | 1079 |
as arguments together with a list of custom serializations. |
1080 |
Each custom serialization starts with a target language |
|
1081 |
identifier followed by an expression, which during |
|
1082 |
code serialization is inserted whenever the type constructor |
|
21348 | 1083 |
would occur. For constants, \isa{{\isasymCODECONST}} implements |
1084 |
the corresponding mechanism. Each ``\verb|_|'' in |
|
21172 | 1085 |
a serialization expression is treated as a placeholder |
1086 |
for the type constructor's (the constant's) arguments.% |
|
1087 |
\end{isamarkuptext}% |
|
1088 |
\isamarkuptrue% |
|
1089 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
22845 | 1090 |
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1091 |
\begin{isamarkuptext}% |
21348 | 1092 |
\lstsml{Thy/examples/bool_mlbool.ML} |
21172 | 1093 |
|
22798 | 1094 |
\noindent This still is not perfect: the parentheses |
21348 | 1095 |
around the \qt{andalso} expression are superfluous. |
1096 |
Though the serializer |
|
21172 | 1097 |
by no means attempts to imitate the rich Isabelle syntax |
1098 |
framework, it provides some common idioms, notably |
|
1099 |
associative infixes with precedences which may be used here:% |
|
1100 |
\end{isamarkuptext}% |
|
1101 |
\isamarkuptrue% |
|
21348 | 1102 |
% |
1103 |
\isadelimtt |
|
1104 |
% |
|
1105 |
\endisadelimtt |
|
1106 |
% |
|
1107 |
\isatagtt |
|
21172 | 1108 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
1109 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
21348 | 1110 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
1111 |
\endisatagtt |
|
1112 |
{\isafoldtt}% |
|
1113 |
% |
|
1114 |
\isadelimtt |
|
1115 |
% |
|
1116 |
\endisadelimtt |
|
1117 |
\isanewline |
|
21172 | 1118 |
\isanewline |
1119 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
22845 | 1120 |
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}% |
21172 | 1121 |
\begin{isamarkuptext}% |
21348 | 1122 |
\lstsml{Thy/examples/bool_infix.ML} |
21172 | 1123 |
|
22798 | 1124 |
\medskip |
1125 |
||
21172 | 1126 |
Next, we try to map HOL pairs to SML pairs, using the |
21348 | 1127 |
infix ``\verb|*|'' type constructor and parentheses:% |
21172 | 1128 |
\end{isamarkuptext}% |
1129 |
\isamarkuptrue% |
|
21348 | 1130 |
% |
1131 |
\isadelimtt |
|
1132 |
% |
|
1133 |
\endisadelimtt |
|
1134 |
% |
|
1135 |
\isatagtt |
|
21172 | 1136 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1137 |
\ {\isacharasterisk}\isanewline |
|
1138 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
1139 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
1140 |
\ Pair\isanewline |
|
1141 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1142 |
\endisatagtt |
1143 |
{\isafoldtt}% |
|
1144 |
% |
|
1145 |
\isadelimtt |
|
1146 |
% |
|
1147 |
\endisadelimtt |
|
1148 |
% |
|
21172 | 1149 |
\begin{isamarkuptext}% |
21348 | 1150 |
The initial bang ``\verb|!|'' tells the serializer to never put |
21172 | 1151 |
parentheses around the whole expression (they are already present), |
1152 |
while the parentheses around argument place holders |
|
1153 |
tell not to put parentheses around the arguments. |
|
21348 | 1154 |
The slash ``\verb|/|'' (followed by arbitrary white space) |
21172 | 1155 |
inserts a space which may be used as a break if necessary |
1156 |
during pretty printing. |
|
1157 |
||
22798 | 1158 |
These examples give a glimpse what mechanisms |
21186 | 1159 |
custom serializations provide; however their usage |
1160 |
requires careful thinking in order not to introduce |
|
1161 |
inconsistencies -- or, in other words: |
|
1162 |
custom serializations are completely axiomatic. |
|
1163 |
||
1164 |
A further noteworthy details is that any special |
|
1165 |
character in a custom serialization may be quoted |
|
21348 | 1166 |
using ``\verb|'|''; thus, in |
1167 |
``\verb|fn '_ => _|'' the first |
|
1168 |
``\verb|_|'' is a proper underscore while the |
|
1169 |
second ``\verb|_|'' is a placeholder. |
|
21186 | 1170 |
|
1171 |
The HOL theories provide further |
|
1172 |
examples for custom serializations and form |
|
1173 |
a recommended tutorial on how to use them properly.% |
|
1174 |
\end{isamarkuptext}% |
|
21172 | 1175 |
\isamarkuptrue% |
1176 |
% |
|
21186 | 1177 |
\isamarkupsubsubsection{Haskell serialization% |
1178 |
} |
|
1179 |
\isamarkuptrue% |
|
1180 |
% |
|
1181 |
\begin{isamarkuptext}% |
|
1182 |
For convenience, the default |
|
1183 |
HOL setup for Haskell maps the \isa{eq} class to |
|
1184 |
its counterpart in Haskell, giving custom serializations |
|
21348 | 1185 |
for the class (\isa{{\isasymCODECLASS}}) and its operation:% |
21186 | 1186 |
\end{isamarkuptext}% |
1187 |
\isamarkuptrue% |
|
1188 |
% |
|
21348 | 1189 |
\isadelimtt |
1190 |
% |
|
1191 |
\endisadelimtt |
|
1192 |
% |
|
1193 |
\isatagtt |
|
21186 | 1194 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
1195 |
\ eq\isanewline |
|
22798 | 1196 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
21186 | 1197 |
\isanewline |
1198 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
22798 | 1199 |
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline |
1200 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1201 |
\endisatagtt |
1202 |
{\isafoldtt}% |
|
1203 |
% |
|
1204 |
\isadelimtt |
|
1205 |
% |
|
1206 |
\endisadelimtt |
|
1207 |
% |
|
21186 | 1208 |
\begin{isamarkuptext}% |
1209 |
A problem now occurs whenever a type which |
|
1210 |
is an instance of \isa{eq} in HOL is mapped |
|
1211 |
on a Haskell-builtin type which is also an instance |
|
1212 |
of Haskell \isa{Eq}:% |
|
1213 |
\end{isamarkuptext}% |
|
1214 |
\isamarkuptrue% |
|
1215 |
\isacommand{typedecl}\isamarkupfalse% |
|
1216 |
\ bar\isanewline |
|
1217 |
\isanewline |
|
1218 |
\isacommand{instance}\isamarkupfalse% |
|
1219 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq% |
|
1220 |
\isadelimproof |
|
1221 |
\ % |
|
1222 |
\endisadelimproof |
|
1223 |
% |
|
1224 |
\isatagproof |
|
1225 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse% |
|
1226 |
% |
|
1227 |
\endisatagproof |
|
1228 |
{\isafoldproof}% |
|
1229 |
% |
|
1230 |
\isadelimproof |
|
21172 | 1231 |
% |
21186 | 1232 |
\endisadelimproof |
1233 |
\isanewline |
|
21348 | 1234 |
% |
1235 |
\isadelimtt |
|
21186 | 1236 |
\isanewline |
21348 | 1237 |
% |
1238 |
\endisadelimtt |
|
1239 |
% |
|
1240 |
\isatagtt |
|
21186 | 1241 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
1242 |
\ bar\isanewline |
|
1243 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
21348 | 1244 |
\endisatagtt |
1245 |
{\isafoldtt}% |
|
1246 |
% |
|
1247 |
\isadelimtt |
|
1248 |
% |
|
1249 |
\endisadelimtt |
|
1250 |
% |
|
21186 | 1251 |
\begin{isamarkuptext}% |
1252 |
The code generator would produce |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1253 |
an additional instance, which of course is rejected. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1254 |
To suppress this additional instance, use |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22060
diff
changeset
|
1255 |
\isa{{\isasymCODEINSTANCE}}:% |
21186 | 1256 |
\end{isamarkuptext}% |
1257 |
\isamarkuptrue% |
|
21348 | 1258 |
% |
1259 |
\isadelimtt |
|
1260 |
% |
|
1261 |
\endisadelimtt |
|
1262 |
% |
|
1263 |
\isatagtt |
|
21186 | 1264 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
1265 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
1266 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
|
21348 | 1267 |
\endisatagtt |
1268 |
{\isafoldtt}% |
|
1269 |
% |
|
1270 |
\isadelimtt |
|
1271 |
% |
|
1272 |
\endisadelimtt |
|
1273 |
% |
|
22798 | 1274 |
\isamarkupsubsubsection{Pretty printing% |
1275 |
} |
|
1276 |
\isamarkuptrue% |
|
1277 |
% |
|
1278 |
\begin{isamarkuptext}% |
|
1279 |
The serializer provides ML interfaces to set up |
|
1280 |
pretty serializations for expressions like lists, numerals |
|
1281 |
and characters; these are |
|
1282 |
monolithic stubs and should only be used with the |
|
1283 |
theories introduces in \secref{sec:library}.% |
|
1284 |
\end{isamarkuptext}% |
|
1285 |
\isamarkuptrue% |
|
1286 |
% |
|
1287 |
\isamarkupsubsection{Constructor sets for datatypes% |
|
21186 | 1288 |
} |
1289 |
\isamarkuptrue% |
|
1290 |
% |
|
1291 |
\begin{isamarkuptext}% |
|
22798 | 1292 |
Conceptually, any datatype is spanned by a set of |
1293 |
\emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n} |
|
1294 |
where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all} |
|
1295 |
type variables in \isa{{\isasymtau}}. The HOL datatype package |
|
1296 |
by default registers any new datatype in the table |
|
1297 |
of datatypes, which may be inspected using |
|
1298 |
the \isa{{\isasymPRINTCODESETUP}} command. |
|
1299 |
||
1300 |
In some cases, it may be convenient to alter or |
|
1301 |
extend this table; as an example, we show |
|
1302 |
how to implement finite sets by lists |
|
1303 |
using the conversion \isa{{\isachardoublequote}set\ {\isasymColon}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ set{\isachardoublequote}} |
|
1304 |
as constructor:% |
|
21186 | 1305 |
\end{isamarkuptext}% |
1306 |
\isamarkuptrue% |
|
22798 | 1307 |
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse% |
21186 | 1308 |
\ set\isanewline |
1309 |
\isanewline |
|
21348 | 1310 |
\isacommand{lemma}\isamarkupfalse% |
22798 | 1311 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}\ {\isacharequal}\ set\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}% |
21348 | 1312 |
\isadelimproof |
1313 |
\ % |
|
1314 |
\endisadelimproof |
|
1315 |
% |
|
1316 |
\isatagproof |
|
22798 | 1317 |
\isacommand{by}\isamarkupfalse% |
1318 |
\ simp% |
|
1319 |
\endisatagproof |
|
1320 |
{\isafoldproof}% |
|
1321 |
% |
|
1322 |
\isadelimproof |
|
1323 |
% |
|
1324 |
\endisadelimproof |
|
1325 |
\isanewline |
|
1326 |
\isanewline |
|
1327 |
\isacommand{lemma}\isamarkupfalse% |
|
1328 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}insert\ x\ {\isacharparenleft}set\ xs{\isacharparenright}\ {\isacharequal}\ set\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}{\isachardoublequoteclose}% |
|
1329 |
\isadelimproof |
|
1330 |
\ % |
|
1331 |
\endisadelimproof |
|
1332 |
% |
|
1333 |
\isatagproof |
|
1334 |
\isacommand{by}\isamarkupfalse% |
|
1335 |
\ simp% |
|
1336 |
\endisatagproof |
|
1337 |
{\isafoldproof}% |
|
1338 |
% |
|
1339 |
\isadelimproof |
|
1340 |
% |
|
1341 |
\endisadelimproof |
|
1342 |
\isanewline |
|
1343 |
\isanewline |
|
1344 |
\isacommand{lemma}\isamarkupfalse% |
|
1345 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}% |
|
1346 |
\isadelimproof |
|
1347 |
\ % |
|
1348 |
\endisadelimproof |
|
21348 | 1349 |
% |
22798 | 1350 |
\isatagproof |
1351 |
\isacommand{by}\isamarkupfalse% |
|
1352 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
1353 |
\endisatagproof |
|
1354 |
{\isafoldproof}% |
|
1355 |
% |
|
1356 |
\isadelimproof |
|
1357 |
% |
|
1358 |
\endisadelimproof |
|
1359 |
\isanewline |
|
1360 |
\isanewline |
|
1361 |
\isacommand{lemma}\isamarkupfalse% |
|
1362 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}xs\ {\isasymunion}\ set\ ys\ {\isacharequal}\ foldr\ insert\ ys\ xs{\isachardoublequoteclose}% |
|
1363 |
\isadelimproof |
|
1364 |
\ % |
|
1365 |
\endisadelimproof |
|
1366 |
% |
|
1367 |
\isatagproof |
|
1368 |
\isacommand{by}\isamarkupfalse% |
|
1369 |
\ {\isacharparenleft}induct\ ys{\isacharparenright}\ simp{\isacharunderscore}all% |
|
1370 |
\endisatagproof |
|
1371 |
{\isafoldproof}% |
|
1372 |
% |
|
1373 |
\isadelimproof |
|
1374 |
% |
|
1375 |
\endisadelimproof |
|
1376 |
\isanewline |
|
1377 |
\isanewline |
|
1378 |
\isacommand{lemma}\isamarkupfalse% |
|
1379 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymUnion}set\ xs\ {\isacharequal}\ foldr\ {\isacharparenleft}op\ {\isasymunion}{\isacharparenright}\ xs\ {\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}% |
|
1380 |
\isadelimproof |
|
1381 |
\ % |
|
1382 |
\endisadelimproof |
|
1383 |
% |
|
1384 |
\isatagproof |
|
1385 |
\isacommand{by}\isamarkupfalse% |
|
1386 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all% |
|
21348 | 1387 |
\endisatagproof |
1388 |
{\isafoldproof}% |
|
1389 |
% |
|
1390 |
\isadelimproof |
|
1391 |
% |
|
1392 |
\endisadelimproof |
|
1393 |
\isanewline |
|
1394 |
\isanewline |
|
1395 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse% |
|
22845 | 1396 |
\ {\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 | 1397 |
\begin{isamarkuptext}% |
22798 | 1398 |
\lstsml{Thy/examples/set_list.ML} |
21348 | 1399 |
|
22798 | 1400 |
\medskip |
1401 |
||
1402 |
Changing the representation of existing datatypes requires |
|
1403 |
some care with respect to pattern matching and such.% |
|
22550 | 1404 |
\end{isamarkuptext}% |
1405 |
\isamarkuptrue% |
|
1406 |
% |
|
21348 | 1407 |
\isamarkupsubsection{Cyclic module dependencies% |
1408 |
} |
|
1409 |
\isamarkuptrue% |
|
1410 |
% |
|
1411 |
\begin{isamarkuptext}% |
|
1412 |
Sometimes the awkward situation occurs that dependencies |
|
1413 |
between definitions introduce cyclic dependencies |
|
1414 |
between modules, which in the Haskell world leaves |
|
1415 |
you to the mercy of the Haskell implementation you are using, |
|
1416 |
while for SML code generation is not possible. |
|
1417 |
||
1418 |
A solution is to declare module names explicitly. |
|
1419 |
Let use assume the three cyclically dependent |
|
1420 |
modules are named \emph{A}, \emph{B} and \emph{C}. |
|
1421 |
Then, by stating% |
|
1422 |
\end{isamarkuptext}% |
|
1423 |
\isamarkuptrue% |
|
1424 |
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse% |
|
1425 |
\ SML\isanewline |
|
1426 |
\ \ A\ ABC\isanewline |
|
1427 |
\ \ B\ ABC\isanewline |
|
1428 |
\ \ C\ ABC% |
|
1429 |
\begin{isamarkuptext}% |
|
1430 |
we explicitly map all those modules on \emph{ABC}, |
|
1431 |
resulting in an ad-hoc merge of this three modules |
|
1432 |
at serialization time.% |
|
1433 |
\end{isamarkuptext}% |
|
1434 |
\isamarkuptrue% |
|
1435 |
% |
|
22798 | 1436 |
\isamarkupsubsection{Incremental code generation% |
1437 |
} |
|
1438 |
\isamarkuptrue% |
|
1439 |
% |
|
1440 |
\begin{isamarkuptext}% |
|
1441 |
Code generation is \emph{incremental}: theorems |
|
1442 |
and abstract intermediate code are cached and extended on demand. |
|
1443 |
The cache may be partially or fully dropped if the underlying |
|
1444 |
executable content of the theory changes. |
|
1445 |
Implementation of caching is supposed to transparently |
|
1446 |
hid away the details from the user. Anyway, caching |
|
1447 |
reaches the surface by using a slightly more general form |
|
1448 |
of the \isa{{\isasymCODETHMS}}, \isa{{\isasymCODEDEPS}} |
|
1449 |
and \isa{{\isasymCODEGEN}} commands: the list of constants |
|
1450 |
may be omitted. Then, all constants with code theorems |
|
1451 |
in the current cache are referred to.% |
|
1452 |
\end{isamarkuptext}% |
|
1453 |
\isamarkuptrue% |
|
1454 |
% |
|
24279 | 1455 |
\isamarkupsubsection{Code generation and proof extraction% |
20967 | 1456 |
} |
1457 |
\isamarkuptrue% |
|
1458 |
% |
|
21172 | 1459 |
\begin{isamarkuptext}% |
24279 | 1460 |
\fixme% |
21172 | 1461 |
\end{isamarkuptext}% |
1462 |
\isamarkuptrue% |
|
1463 |
% |
|
1464 |
\isamarkupsection{ML interfaces \label{sec:ml}% |
|
1465 |
} |
|
1466 |
\isamarkuptrue% |
|
1467 |
% |
|
21348 | 1468 |
\begin{isamarkuptext}% |
1469 |
Since the code generator framework not only aims to provide |
|
1470 |
a nice Isar interface but also to form a base for |
|
1471 |
code-generation-based applications, here a short |
|
1472 |
description of the most important ML interfaces.% |
|
1473 |
\end{isamarkuptext}% |
|
1474 |
\isamarkuptrue% |
|
1475 |
% |
|
24217 | 1476 |
\isamarkupsubsection{Basics: \isa{CodeUnit}% |
21172 | 1477 |
} |
1478 |
\isamarkuptrue% |
|
1479 |
% |
|
21348 | 1480 |
\begin{isamarkuptext}% |
24217 | 1481 |
This module provides identification of (probably overloaded) |
21348 | 1482 |
constants by unique identifiers.% |
1483 |
\end{isamarkuptext}% |
|
1484 |
\isamarkuptrue% |
|
1485 |
% |
|
21172 | 1486 |
\isadelimmlref |
1487 |
% |
|
1488 |
\endisadelimmlref |
|
1489 |
% |
|
1490 |
\isatagmlref |
|
1491 |
% |
|
1492 |
\begin{isamarkuptext}% |
|
1493 |
\begin{mldecls} |
|
24217 | 1494 |
\indexmltype{CodeUnit.const}\verb|type CodeUnit.const = string * string option| \\ |
1495 |
\indexml{CodeUnit.const-of-cexpr}\verb|CodeUnit.const_of_cexpr: theory -> string * typ -> CodeUnit.const| \\ |
|
21348 | 1496 |
\end{mldecls} |
1497 |
||
1498 |
\begin{description} |
|
1499 |
||
24217 | 1500 |
\item \verb|CodeUnit.const| is the identifier type: |
21348 | 1501 |
the product of a \emph{string} with a list of \emph{typs}. |
1502 |
The \emph{string} is the constant name as represented inside Isabelle; |
|
22550 | 1503 |
for overloaded constants, the attached \emph{string option} |
1504 |
is either \isa{SOME} type constructor denoting an instance, |
|
1505 |
or \isa{NONE} for the polymorphic constant. |
|
21348 | 1506 |
|
24217 | 1507 |
\item \verb|CodeUnit.const_of_cexpr|~\isa{thy}~\isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} |
22550 | 1508 |
maps a constant expression \isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}} |
1509 |
to its canonical identifier. |
|
21348 | 1510 |
|
1511 |
\end{description}% |
|
21172 | 1512 |
\end{isamarkuptext}% |
1513 |
\isamarkuptrue% |
|
1514 |
% |
|
1515 |
\endisatagmlref |
|
1516 |
{\isafoldmlref}% |
|
1517 |
% |
|
1518 |
\isadelimmlref |
|
1519 |
% |
|
1520 |
\endisadelimmlref |
|
1521 |
% |
|
24217 | 1522 |
\isamarkupsubsection{Executable theory content: \isa{Code}% |
21172 | 1523 |
} |
1524 |
\isamarkuptrue% |
|
1525 |
% |
|
1526 |
\begin{isamarkuptext}% |
|
1527 |
This Pure module implements the core notions of |
|
1528 |
executable content of a theory.% |
|
1529 |
\end{isamarkuptext}% |
|
1530 |
\isamarkuptrue% |
|
1531 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1532 |
\isamarkupsubsubsection{Managing executable content% |
20967 | 1533 |
} |
1534 |
\isamarkuptrue% |
|
1535 |
% |
|
21172 | 1536 |
\isadelimmlref |
1537 |
% |
|
1538 |
\endisadelimmlref |
|
1539 |
% |
|
1540 |
\isatagmlref |
|
1541 |
% |
|
1542 |
\begin{isamarkuptext}% |
|
1543 |
\begin{mldecls} |
|
24217 | 1544 |
\indexml{Code.add-func}\verb|Code.add_func: bool -> thm -> theory -> theory| \\ |
1545 |
\indexml{Code.del-func}\verb|Code.del_func: thm -> theory -> theory| \\ |
|
1546 |
\indexml{Code.add-funcl}\verb|Code.add_funcl: CodeUnit.const * thm list Susp.T -> theory -> theory| \\ |
|
1547 |
\indexml{Code.add-inline}\verb|Code.add_inline: thm -> theory -> theory| \\ |
|
1548 |
\indexml{Code.del-inline}\verb|Code.del_inline: thm -> theory -> theory| \\ |
|
1549 |
\indexml{Code.add-inline-proc}\verb|Code.add_inline_proc: string * (theory -> cterm list -> thm list)|\isasep\isanewline% |
|
21348 | 1550 |
\verb| -> theory -> theory| \\ |
24217 | 1551 |
\indexml{Code.del-inline-proc}\verb|Code.del_inline_proc: string -> theory -> theory| \\ |
1552 |
\indexml{Code.add-preproc}\verb|Code.add_preproc: string * (theory -> thm list -> thm list)|\isasep\isanewline% |
|
21348 | 1553 |
\verb| -> theory -> theory| \\ |
24217 | 1554 |
\indexml{Code.del-preproc}\verb|Code.del_preproc: string -> theory -> theory| \\ |
1555 |
\indexml{Code.add-datatype}\verb|Code.add_datatype: string * ((string * sort) list * (string * typ list) list)|\isasep\isanewline% |
|
22479 | 1556 |
\verb| -> theory -> theory| \\ |
24217 | 1557 |
\indexml{Code.get-datatype}\verb|Code.get_datatype: theory -> string|\isasep\isanewline% |
22479 | 1558 |
\verb| -> (string * sort) list * (string * typ list) list| \\ |
24217 | 1559 |
\indexml{Code.get-datatype-of-constr}\verb|Code.get_datatype_of_constr: theory -> CodeUnit.const -> string option| |
21172 | 1560 |
\end{mldecls} |
1561 |
||
1562 |
\begin{description} |
|
1563 |
||
24217 | 1564 |
\item \verb|Code.add_func|~\isa{thm}~\isa{thy} adds function |
21348 | 1565 |
theorem \isa{thm} to executable content. |
1566 |
||
24217 | 1567 |
\item \verb|Code.del_func|~\isa{thm}~\isa{thy} removes function |
21348 | 1568 |
theorem \isa{thm} from executable content, if present. |
1569 |
||
24217 | 1570 |
\item \verb|Code.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds |
22060 | 1571 |
suspended defining equations \isa{lthms} for constant |
21348 | 1572 |
\isa{const} to executable content. |
1573 |
||
24217 | 1574 |
\item \verb|Code.add_inline|~\isa{thm}~\isa{thy} adds |
21348 | 1575 |
inlining theorem \isa{thm} to executable content. |
1576 |
||
24217 | 1577 |
\item \verb|Code.del_inline|~\isa{thm}~\isa{thy} remove |
21348 | 1578 |
inlining theorem \isa{thm} from executable content, if present. |
1579 |
||
24217 | 1580 |
\item \verb|Code.add_inline_proc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
22060 | 1581 |
inline procedure \isa{f} (named \isa{name}) to executable content; |
21348 | 1582 |
\isa{f} is a computation of rewrite rules dependent on |
1583 |
the current theory context and the list of all arguments |
|
22060 | 1584 |
and right hand sides of the defining equations belonging |
21348 | 1585 |
to a certain function definition. |
1586 |
||
24217 | 1587 |
\item \verb|Code.del_inline_proc|~\isa{name}~\isa{thy} removes |
22060 | 1588 |
inline procedure named \isa{name} from executable content. |
1589 |
||
24217 | 1590 |
\item \verb|Code.add_preproc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds |
22060 | 1591 |
generic preprocessor \isa{f} (named \isa{name}) to executable content; |
1592 |
\isa{f} is a transformation of the defining equations belonging |
|
21348 | 1593 |
to a certain function definition, depending on the |
1594 |
current theory context. |
|
1595 |
||
24217 | 1596 |
\item \verb|Code.del_preproc|~\isa{name}~\isa{thy} removes |
22060 | 1597 |
generic preprcoessor named \isa{name} from executable content. |
1598 |
||
24217 | 1599 |
\item \verb|Code.add_datatype|~\isa{{\isacharparenleft}name{\isacharcomma}\ spec{\isacharparenright}}~\isa{thy} adds |
21348 | 1600 |
a datatype to executable content, with type constructor |
1601 |
\isa{name} and specification \isa{spec}; \isa{spec} is |
|
1602 |
a pair consisting of a list of type variable with sort |
|
1603 |
constraints and a list of constructors with name |
|
22479 | 1604 |
and types of arguments. |
21348 | 1605 |
|
24217 | 1606 |
\item \verb|Code.get_datatype_of_constr|~\isa{thy}~\isa{const} |
21348 | 1607 |
returns type constructor corresponding to |
1608 |
constructor \isa{const}; returns \isa{NONE} |
|
1609 |
if \isa{const} is no constructor. |
|
1610 |
||
1611 |
\end{description}% |
|
1612 |
\end{isamarkuptext}% |
|
1613 |
\isamarkuptrue% |
|
1614 |
% |
|
1615 |
\endisatagmlref |
|
1616 |
{\isafoldmlref}% |
|
1617 |
% |
|
1618 |
\isadelimmlref |
|
1619 |
% |
|
1620 |
\endisadelimmlref |
|
1621 |
% |
|
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1622 |
\isamarkupsubsection{Auxiliary% |
21172 | 1623 |
} |
1624 |
\isamarkuptrue% |
|
1625 |
% |
|
1626 |
\isadelimmlref |
|
1627 |
% |
|
1628 |
\endisadelimmlref |
|
1629 |
% |
|
1630 |
\isatagmlref |
|
1631 |
% |
|
1632 |
\begin{isamarkuptext}% |
|
1633 |
\begin{mldecls} |
|
24217 | 1634 |
\indexml{CodeUnit.const-ord}\verb|CodeUnit.const_ord: CodeUnit.const * CodeUnit.const -> order| \\ |
1635 |
\indexml{CodeUnit.eq-const}\verb|CodeUnit.eq_const: CodeUnit.const * CodeUnit.const -> bool| \\ |
|
1636 |
\indexml{CodeUnit.read-const}\verb|CodeUnit.read_const: theory -> string -> CodeUnit.const| \\ |
|
1637 |
\indexmlstructure{CodeUnit.Consttab}\verb|structure CodeUnit.Consttab| \\ |
|
1638 |
\indexml{CodeUnit.head-func}\verb|CodeUnit.head_func: thm -> CodeUnit.const * typ| \\ |
|
1639 |
\indexml{CodeUnit.rewrite-func}\verb|CodeUnit.rewrite_func: thm list -> thm -> thm| \\ |
|
21348 | 1640 |
\end{mldecls} |
1641 |
||
1642 |
\begin{description} |
|
1643 |
||
24217 | 1644 |
\item \verb|CodeUnit.const_ord|,~\verb|CodeUnit.eq_const| |
21348 | 1645 |
provide order and equality on constant identifiers. |
1646 |
||
24217 | 1647 |
\item \verb|CodeUnit.Consttab| |
22292
3b118010ec08
adjusted to new code generator Isar commands and changes in implementation
haftmann
parents:
22188
diff
changeset
|
1648 |
provides table structures with constant identifiers as keys. |
21348 | 1649 |
|
24217 | 1650 |
\item \verb|CodeUnit.read_const|~\isa{thy}~\isa{s} |
21348 | 1651 |
reads a constant as a concrete term expression \isa{s}. |
1652 |
||
24217 | 1653 |
\item \verb|CodeUnit.head_func|~\isa{thm} |
22751 | 1654 |
extracts the constant and its type from a defining equation \isa{thm}. |
21348 | 1655 |
|
24217 | 1656 |
\item \verb|CodeUnit.rewrite_func|~\isa{rews}~\isa{thm} |
22060 | 1657 |
rewrites a defining equation \isa{thm} with a set of rewrite |
21348 | 1658 |
rules \isa{rews}; only arguments and right hand side are rewritten, |
22060 | 1659 |
not the head of the defining equation. |
21348 | 1660 |
|
1661 |
\end{description}% |
|
21172 | 1662 |
\end{isamarkuptext}% |
1663 |
\isamarkuptrue% |
|
1664 |
% |
|
1665 |
\endisatagmlref |
|
1666 |
{\isafoldmlref}% |
|
1667 |
% |
|
1668 |
\isadelimmlref |
|
1669 |
% |
|
1670 |
\endisadelimmlref |
|
1671 |
% |
|
20967 | 1672 |
\isamarkupsubsection{Implementing code generator applications% |
1673 |
} |
|
1674 |
\isamarkuptrue% |
|
1675 |
% |
|
21172 | 1676 |
\begin{isamarkuptext}% |
21348 | 1677 |
Implementing code generator applications on top |
1678 |
of the framework set out so far usually not only |
|
1679 |
involves using those primitive interfaces |
|
1680 |
but also storing code-dependent data and various |
|
1681 |
other things. |
|
1682 |
||
1683 |
\begin{warn} |
|
21172 | 1684 |
Some interfaces discussed here have not reached |
1685 |
a final state yet. |
|
1686 |
Changes likely to occur in future. |
|
21452 | 1687 |
\end{warn}% |
21172 | 1688 |
\end{isamarkuptext}% |
1689 |
\isamarkuptrue% |
|
1690 |
% |
|
1691 |
\isamarkupsubsubsection{Data depending on the theory's executable content% |
|
1692 |
} |
|
1693 |
\isamarkuptrue% |
|
1694 |
% |
|
21348 | 1695 |
\begin{isamarkuptext}% |
21452 | 1696 |
Due to incrementality of code generation, changes in the |
1697 |
theory's executable content have to be propagated in a |
|
1698 |
certain fashion. Additionally, such changes may occur |
|
1699 |
not only during theory extension but also during theory |
|
1700 |
merge, which is a little bit nasty from an implementation |
|
1701 |
point of view. The framework provides a solution |
|
1702 |
to this technical challenge by providing a functorial |
|
1703 |
data slot \verb|CodeDataFun|; on instantiation |
|
1704 |
of this functor, the following types and operations |
|
1705 |
are required: |
|
1706 |
||
1707 |
\medskip |
|
21348 | 1708 |
\begin{tabular}{l} |
1709 |
\isa{type\ T} \\ |
|
1710 |
\isa{val\ empty{\isacharcolon}\ T} \\ |
|
1711 |
\isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\ |
|
24217 | 1712 |
\isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodeUnit{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T} |
21348 | 1713 |
\end{tabular} |
1714 |
||
21452 | 1715 |
\begin{description} |
1716 |
||
1717 |
\item \isa{T} the type of data to store. |
|
1718 |
||
1719 |
\item \isa{empty} initial (empty) data. |
|
1720 |
||
1721 |
\item \isa{merge} merging two data slots. |
|
1722 |
||
22798 | 1723 |
\item \isa{purge}~\isa{thy}~\isa{consts} propagates changes in executable content; |
21452 | 1724 |
if possible, the current theory context is handed over |
1725 |
as argument \isa{thy} (if there is no current theory context (e.g.~during |
|
22798 | 1726 |
theory merge, \verb|NONE|); \isa{consts} indicates the kind |
21452 | 1727 |
of change: \verb|NONE| stands for a fundamental change |
22798 | 1728 |
which invalidates any existing code, \isa{SOME\ consts} |
1729 |
hints that executable content for constants \isa{consts} |
|
21452 | 1730 |
has changed. |
1731 |
||
1732 |
\end{description} |
|
1733 |
||
1734 |
An instance of \verb|CodeDataFun| provides the following |
|
1735 |
interface: |
|
1736 |
||
21348 | 1737 |
\medskip |
1738 |
\begin{tabular}{l} |
|
1739 |
\isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\ |
|
1740 |
\isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\ |
|
1741 |
\isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T} |
|
21452 | 1742 |
\end{tabular} |
1743 |
||
1744 |
\begin{description} |
|
1745 |
||
1746 |
\item \isa{get} retrieval of the current data. |
|
1747 |
||
1748 |
\item \isa{change} update of current data (cached!) |
|
1749 |
by giving a continuation. |
|
1750 |
||
1751 |
\item \isa{change{\isacharunderscore}yield} update with side result. |
|
1752 |
||
1753 |
\end{description}% |
|
1754 |
\end{isamarkuptext}% |
|
1755 |
\isamarkuptrue% |
|
1756 |
% |
|
1757 |
\isamarkupsubsubsection{Datatype hooks% |
|
1758 |
} |
|
1759 |
\isamarkuptrue% |
|
1760 |
% |
|
1761 |
\begin{isamarkuptext}% |
|
1762 |
Isabelle/HOL's datatype package provides a mechanism to |
|
1763 |
extend theories depending on datatype declarations: |
|
1764 |
\emph{datatype hooks}. For example, when declaring a new |
|
22060 | 1765 |
datatype, a hook proves defining equations for equality on |
21452 | 1766 |
that datatype (if possible).% |
21348 | 1767 |
\end{isamarkuptext}% |
1768 |
\isamarkuptrue% |
|
1769 |
% |
|
1770 |
\isadelimmlref |
|
1771 |
% |
|
1772 |
\endisadelimmlref |
|
1773 |
% |
|
1774 |
\isatagmlref |
|
1775 |
% |
|
1776 |
\begin{isamarkuptext}% |
|
1777 |
\begin{mldecls} |
|
21452 | 1778 |
\indexmltype{DatatypeHooks.hook}\verb|type DatatypeHooks.hook = string list -> theory -> theory| \\ |
1779 |
\indexml{DatatypeHooks.add}\verb|DatatypeHooks.add: DatatypeHooks.hook -> theory -> theory| |
|
21348 | 1780 |
\end{mldecls} |
1781 |
||
1782 |
\begin{description} |
|
1783 |
||
21452 | 1784 |
\item \verb|DatatypeHooks.hook| specifies the interface |
1785 |
of \emph{datatype hooks}: a theory update |
|
1786 |
depending on the list of newly introduced |
|
1787 |
datatype names. |
|
1788 |
||
1789 |
\item \verb|DatatypeHooks.add| adds a hook to the |
|
1790 |
chain of all hooks. |
|
21348 | 1791 |
|
1792 |
\end{description}% |
|
1793 |
\end{isamarkuptext}% |
|
1794 |
\isamarkuptrue% |
|
1795 |
% |
|
1796 |
\endisatagmlref |
|
1797 |
{\isafoldmlref}% |
|
1798 |
% |
|
1799 |
\isadelimmlref |
|
1800 |
% |
|
1801 |
\endisadelimmlref |
|
1802 |
% |
|
21452 | 1803 |
\isamarkupsubsubsection{Trivial typedefs -- type copies% |
21172 | 1804 |
} |
1805 |
\isamarkuptrue% |
|
1806 |
% |
|
21452 | 1807 |
\begin{isamarkuptext}% |
1808 |
Sometimes packages will introduce new types |
|
1809 |
as \emph{marked type copies} similar to Haskell's |
|
1810 |
\isa{newtype} declaration (e.g. the HOL record package) |
|
1811 |
\emph{without} tinkering with the overhead of datatypes. |
|
1812 |
Technically, these type copies are trivial forms of typedefs. |
|
1813 |
Since these type copies in code generation view are nothing |
|
1814 |
else than datatypes, they have been given a own package |
|
1815 |
in order to faciliate code generation:% |
|
1816 |
\end{isamarkuptext}% |
|
1817 |
\isamarkuptrue% |
|
1818 |
% |
|
21348 | 1819 |
\isadelimmlref |
1820 |
% |
|
1821 |
\endisadelimmlref |
|
1822 |
% |
|
1823 |
\isatagmlref |
|
1824 |
% |
|
21172 | 1825 |
\begin{isamarkuptext}% |
21348 | 1826 |
\begin{mldecls} |
21452 | 1827 |
\indexmltype{TypecopyPackage.info}\verb|type TypecopyPackage.info| \\ |
1828 |
\indexml{TypecopyPackage.add-typecopy}\verb|TypecopyPackage.add_typecopy: |\isasep\isanewline% |
|
1829 |
\verb| bstring * string list -> typ -> (bstring * bstring) option|\isasep\isanewline% |
|
1830 |
\verb| -> theory -> (string * TypecopyPackage.info) * theory| \\ |
|
1831 |
\indexml{TypecopyPackage.get-typecopy-info}\verb|TypecopyPackage.get_typecopy_info: theory|\isasep\isanewline% |
|
1832 |
\verb| -> string -> TypecopyPackage.info option| \\ |
|
1833 |
\indexml{TypecopyPackage.get-spec}\verb|TypecopyPackage.get_spec: theory -> string|\isasep\isanewline% |
|
1834 |
\verb| -> (string * sort) list * (string * typ list) list| \\ |
|
1835 |
\indexmltype{TypecopyPackage.hook}\verb|type TypecopyPackage.hook = string * TypecopyPackage.info -> theory -> theory| \\ |
|
1836 |
\indexml{TypecopyPackage.add-hook}\verb|TypecopyPackage.add_hook: TypecopyPackage.hook -> theory -> theory| \\ |
|
1837 |
\end{mldecls} |
|
1838 |
||
1839 |
\begin{description} |
|
1840 |
||
1841 |
\item \verb|TypecopyPackage.info| a record containing |
|
1842 |
the specification and further data of a type copy. |
|
1843 |
||
1844 |
\item \verb|TypecopyPackage.add_typecopy| defines a new |
|
1845 |
type copy. |
|
1846 |
||
1847 |
\item \verb|TypecopyPackage.get_typecopy_info| retrieves |
|
1848 |
data of an existing type copy. |
|
1849 |
||
1850 |
\item \verb|TypecopyPackage.get_spec| retrieves datatype-like |
|
1851 |
specification of a type copy. |
|
1852 |
||
1853 |
\item \verb|TypecopyPackage.hook|,~\verb|TypecopyPackage.add_hook| |
|
1854 |
provide a hook mechanism corresponding to the hook mechanism |
|
1855 |
on datatypes. |
|
1856 |
||
1857 |
\end{description}% |
|
21348 | 1858 |
\end{isamarkuptext}% |
1859 |
\isamarkuptrue% |
|
1860 |
% |
|
21452 | 1861 |
\endisatagmlref |
1862 |
{\isafoldmlref}% |
|
1863 |
% |
|
1864 |
\isadelimmlref |
|
1865 |
% |
|
1866 |
\endisadelimmlref |
|
1867 |
% |
|
1868 |
\isamarkupsubsubsection{Unifying type copies and datatypes% |
|
1869 |
} |
|
1870 |
\isamarkuptrue% |
|
1871 |
% |
|
1872 |
\begin{isamarkuptext}% |
|
1873 |
Since datatypes and type copies are mapped to the same concept (datatypes) |
|
1874 |
by code generation, the view on both is unified \qt{code types}:% |
|
1875 |
\end{isamarkuptext}% |
|
1876 |
\isamarkuptrue% |
|
1877 |
% |
|
1878 |
\isadelimmlref |
|
1879 |
% |
|
1880 |
\endisadelimmlref |
|
1881 |
% |
|
1882 |
\isatagmlref |
|
1883 |
% |
|
21348 | 1884 |
\begin{isamarkuptext}% |
1885 |
\begin{mldecls} |
|
21452 | 1886 |
\indexmltype{DatatypeCodegen.hook}\verb|type DatatypeCodegen.hook = (string * (bool * ((string * sort) list|\isasep\isanewline% |
1887 |
\verb| * (string * typ list) list))) list|\isasep\isanewline% |
|
21348 | 1888 |
\verb| -> theory -> theory| \\ |
1889 |
\indexml{DatatypeCodegen.add-codetypes-hook-bootstrap}\verb|DatatypeCodegen.add_codetypes_hook_bootstrap: |\isasep\isanewline% |
|
1890 |
\verb| DatatypeCodegen.hook -> theory -> theory| |
|
1891 |
\end{mldecls}% |
|
1892 |
\end{isamarkuptext}% |
|
1893 |
\isamarkuptrue% |
|
1894 |
% |
|
1895 |
\endisatagmlref |
|
1896 |
{\isafoldmlref}% |
|
1897 |
% |
|
1898 |
\isadelimmlref |
|
1899 |
% |
|
1900 |
\endisadelimmlref |
|
1901 |
% |
|
1902 |
\begin{isamarkuptext}% |
|
21452 | 1903 |
\begin{description} |
1904 |
||
1905 |
\item \verb|DatatypeCodegen.hook| specifies the code type hook |
|
1906 |
interface: a theory transformation depending on a list of |
|
1907 |
mutual recursive code types; each entry in the list |
|
1908 |
has the structure \isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}is{\isacharunderscore}data{\isacharcomma}\ {\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}{\isacharparenright}{\isacharparenright}} |
|
1909 |
where \isa{name} is the name of the code type, \isa{is{\isacharunderscore}data} |
|
1910 |
is true iff \isa{name} is a datatype rather then a type copy, |
|
1911 |
and \isa{{\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}} is the specification of the code type. |
|
1912 |
||
1913 |
\item \verb|DatatypeCodegen.add_codetypes_hook_bootstrap| adds a code |
|
1914 |
type hook; the hook is immediately processed for all already |
|
1915 |
existing datatypes, in blocks of mutual recursive datatypes, |
|
1916 |
where all datatypes a block depends on are processed before |
|
1917 |
the block. |
|
1918 |
||
1919 |
\end{description} |
|
1920 |
||
1921 |
\emph{Happy proving, happy hacking!}% |
|
21172 | 1922 |
\end{isamarkuptext}% |
1923 |
\isamarkuptrue% |
|
1924 |
% |
|
20967 | 1925 |
\isadelimtheory |
1926 |
% |
|
1927 |
\endisadelimtheory |
|
1928 |
% |
|
1929 |
\isatagtheory |
|
1930 |
\isacommand{end}\isamarkupfalse% |
|
1931 |
% |
|
1932 |
\endisatagtheory |
|
1933 |
{\isafoldtheory}% |
|
1934 |
% |
|
1935 |
\isadelimtheory |
|
1936 |
% |
|
1937 |
\endisadelimtheory |
|
1938 |
\isanewline |
|
1939 |
\end{isabellebody}% |
|
1940 |
%%% Local Variables: |
|
1941 |
%%% mode: latex |
|
1942 |
%%% TeX-master: "root" |
|
1943 |
%%% End: |