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