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