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
|
|
72 |
with maximum flexibility.%
|
20967
|
73 |
\end{isamarkuptext}%
|
|
74 |
\isamarkuptrue%
|
|
75 |
%
|
21172
|
76 |
\isamarkupsubsection{Overview%
|
|
77 |
}
|
|
78 |
\isamarkuptrue%
|
|
79 |
%
|
|
80 |
\begin{isamarkuptext}%
|
|
81 |
The code generator aims to be usable with no further ado
|
|
82 |
in most cases while allowing for detailed customization.
|
|
83 |
This manifests in the structure of this tutorial: this introduction
|
|
84 |
continues with a short introduction of concepts. Section
|
21186
|
85 |
\secref{sec:basics} explains how to use the framework naively,
|
21172
|
86 |
presuming a reasonable default setup. Then, section
|
|
87 |
\secref{sec:advanced} deals with advanced topics,
|
|
88 |
introducing further aspects of the code generator framework
|
|
89 |
in a motivation-driven manner. Last, section \secref{sec:ml}
|
|
90 |
introduces the framework's internal programming interfaces.
|
|
91 |
|
|
92 |
\begin{warn}
|
|
93 |
Ultimately, the code generator which this tutorial deals with
|
|
94 |
is supposed to replace the already established code generator
|
|
95 |
by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
|
|
96 |
So, for the moment, there are two distinct code generators
|
|
97 |
in Isabelle.
|
|
98 |
Also note that while the framework itself is largely
|
|
99 |
object-logic independent, only HOL provides a reasonable
|
|
100 |
framework setup.
|
|
101 |
\end{warn}%
|
|
102 |
\end{isamarkuptext}%
|
|
103 |
\isamarkuptrue%
|
|
104 |
%
|
|
105 |
\isamarkupsubsection{Code generation process%
|
|
106 |
}
|
|
107 |
\isamarkuptrue%
|
|
108 |
%
|
|
109 |
\begin{isamarkuptext}%
|
|
110 |
The code generator employs a notion of executability
|
|
111 |
for three foundational executable ingredients known
|
|
112 |
from functional programming:
|
|
113 |
\emph{function equations}, \emph{datatypes}, and
|
|
114 |
\emph{type classes}. A function equation as a first approximation
|
|
115 |
is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t}
|
|
116 |
(an equation headed by a constant \isa{f} with arguments
|
|
117 |
\isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}.
|
|
118 |
Code generation aims to turn function equations
|
|
119 |
into a functional program by running through
|
|
120 |
a process (see figure \ref{fig:process}):
|
|
121 |
|
|
122 |
\begin{itemize}
|
|
123 |
|
|
124 |
\item Out of the vast collection of theorems proven in a
|
|
125 |
\qn{theory}, a reasonable subset modeling
|
|
126 |
function equations is \qn{selected}.
|
|
127 |
|
|
128 |
\item On those selected theorems, certain
|
|
129 |
transformations are carried out
|
|
130 |
(\qn{preprocessing}). Their purpose is to turn theorems
|
|
131 |
representing non- or badly executable
|
|
132 |
specifications into equivalent but executable counterparts.
|
|
133 |
The result is a structured collection of \qn{code theorems}.
|
|
134 |
|
|
135 |
\item These \qn{code theorems} then are extracted
|
|
136 |
into an Haskell-like intermediate
|
|
137 |
language.
|
|
138 |
|
|
139 |
\item Finally, out of the intermediate language the final
|
|
140 |
code in the desired \qn{target language} is \qn{serialized}.
|
|
141 |
|
|
142 |
\end{itemize}
|
|
143 |
|
|
144 |
\begin{figure}[h]
|
|
145 |
\centering
|
|
146 |
\includegraphics[width=0.3\textwidth]{codegen_process}
|
|
147 |
\caption{code generator -- processing overview}
|
|
148 |
\label{fig:process}
|
|
149 |
\end{figure}
|
|
150 |
|
|
151 |
From these steps, only the two last are carried out
|
|
152 |
outside the logic; by keeping this layer as
|
|
153 |
thin as possible, the amount of code to trust is
|
|
154 |
kept to a minimum.%
|
|
155 |
\end{isamarkuptext}%
|
|
156 |
\isamarkuptrue%
|
|
157 |
%
|
|
158 |
\isamarkupsection{Basics \label{sec:basics}%
|
20967
|
159 |
}
|
|
160 |
\isamarkuptrue%
|
|
161 |
%
|
|
162 |
\isamarkupsubsection{Invoking the code generator%
|
|
163 |
}
|
|
164 |
\isamarkuptrue%
|
|
165 |
%
|
21172
|
166 |
\begin{isamarkuptext}%
|
|
167 |
Thanks to a reasonable setup of the HOL theories, in
|
|
168 |
most cases code generation proceeds without further ado:%
|
|
169 |
\end{isamarkuptext}%
|
|
170 |
\isamarkuptrue%
|
|
171 |
\isacommand{consts}\isamarkupfalse%
|
|
172 |
\isanewline
|
|
173 |
\ \ fac\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
|
|
174 |
\isanewline
|
|
175 |
\isacommand{primrec}\isamarkupfalse%
|
|
176 |
\isanewline
|
|
177 |
\ \ {\isachardoublequoteopen}fac\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
|
|
178 |
\ \ {\isachardoublequoteopen}fac\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ Suc\ n\ {\isacharasterisk}\ fac\ n{\isachardoublequoteclose}%
|
|
179 |
\begin{isamarkuptext}%
|
|
180 |
This executable specification is now turned to SML code:%
|
|
181 |
\end{isamarkuptext}%
|
|
182 |
\isamarkuptrue%
|
|
183 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
184 |
\ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
185 |
\begin{isamarkuptext}%
|
21186
|
186 |
The \isasymCODEGEN command takes a space-separated list of
|
21172
|
187 |
constants together with \qn{serialization directives}
|
|
188 |
in parentheses. These start with a \qn{target language}
|
21186
|
189 |
identifier, followed by arguments, their semantics
|
21172
|
190 |
depending on the target. In the SML case, a filename
|
|
191 |
is given where to write the generated code to.
|
|
192 |
|
|
193 |
Internally, the function equations for all selected
|
21186
|
194 |
constants are taken, including any transitively required
|
21172
|
195 |
constants, datatypes and classes, resulting in the following
|
|
196 |
code:
|
|
197 |
|
|
198 |
\lstsml{Thy/examples/fac.ML}
|
|
199 |
|
|
200 |
The code generator will complain when a required
|
|
201 |
ingredient does not provide a executable counterpart.
|
|
202 |
This is the case if an involved type is not a datatype:%
|
|
203 |
\end{isamarkuptext}%
|
|
204 |
\isamarkuptrue%
|
|
205 |
%
|
|
206 |
\isadelimML
|
|
207 |
%
|
|
208 |
\endisadelimML
|
|
209 |
%
|
|
210 |
\isatagML
|
|
211 |
%
|
|
212 |
\endisatagML
|
|
213 |
{\isafoldML}%
|
|
214 |
%
|
|
215 |
\isadelimML
|
|
216 |
\isanewline
|
|
217 |
%
|
|
218 |
\endisadelimML
|
|
219 |
\isacommand{typedecl}\isamarkupfalse%
|
|
220 |
\ {\isacharprime}a\ foo\isanewline
|
|
221 |
\isanewline
|
|
222 |
\isacommand{definition}\isamarkupfalse%
|
|
223 |
\isanewline
|
|
224 |
\ \ bar\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ foo\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
|
|
225 |
\ \ {\isachardoublequoteopen}bar\ x\ y\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
|
|
226 |
%
|
|
227 |
\isadelimML
|
|
228 |
%
|
|
229 |
\endisadelimML
|
|
230 |
%
|
|
231 |
\isatagML
|
|
232 |
%
|
|
233 |
\endisatagML
|
|
234 |
{\isafoldML}%
|
|
235 |
%
|
|
236 |
\isadelimML
|
|
237 |
%
|
|
238 |
\endisadelimML
|
|
239 |
\isanewline
|
|
240 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
241 |
\ bar\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}type{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
242 |
\begin{isamarkuptext}%
|
|
243 |
\noindent will result in an error. Likewise, generating code
|
|
244 |
for constants not yielding
|
|
245 |
a function equation will fail, e.g.~the Hilbert choice
|
|
246 |
operation \isa{SOME}:%
|
|
247 |
\end{isamarkuptext}%
|
|
248 |
\isamarkuptrue%
|
|
249 |
%
|
|
250 |
\isadelimML
|
|
251 |
%
|
|
252 |
\endisadelimML
|
|
253 |
%
|
|
254 |
\isatagML
|
|
255 |
%
|
|
256 |
\endisatagML
|
|
257 |
{\isafoldML}%
|
|
258 |
%
|
|
259 |
\isadelimML
|
|
260 |
\isanewline
|
|
261 |
%
|
|
262 |
\endisadelimML
|
|
263 |
\isacommand{definition}\isamarkupfalse%
|
|
264 |
\isanewline
|
|
265 |
\ \ pick{\isacharunderscore}some\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
|
|
266 |
\ \ {\isachardoublequoteopen}pick{\isacharunderscore}some\ xs\ {\isacharequal}\ {\isacharparenleft}SOME\ x{\isachardot}\ x\ {\isasymin}\ set\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
267 |
%
|
|
268 |
\isadelimML
|
|
269 |
%
|
|
270 |
\endisadelimML
|
|
271 |
%
|
|
272 |
\isatagML
|
|
273 |
%
|
|
274 |
\endisatagML
|
|
275 |
{\isafoldML}%
|
|
276 |
%
|
|
277 |
\isadelimML
|
|
278 |
%
|
|
279 |
\endisadelimML
|
|
280 |
\isanewline
|
|
281 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
282 |
\ pick{\isacharunderscore}some\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}const{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
20967
|
283 |
\isamarkupsubsection{Theorem selection%
|
|
284 |
}
|
|
285 |
\isamarkuptrue%
|
|
286 |
%
|
21172
|
287 |
\begin{isamarkuptext}%
|
|
288 |
The list of all function equations in a theory may be inspected
|
|
289 |
using the \isasymPRINTCODETHMS command:%
|
|
290 |
\end{isamarkuptext}%
|
|
291 |
\isamarkuptrue%
|
|
292 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
|
|
293 |
%
|
|
294 |
\begin{isamarkuptext}%
|
|
295 |
\noindent which displays a table of constant with corresponding
|
|
296 |
function equations (the additional stuff displayed
|
|
297 |
shall not bother us for the moment). If this table does
|
|
298 |
not provide at least one function
|
21186
|
299 |
equation, the table of primitive definitions is searched
|
21172
|
300 |
whether it provides one.
|
|
301 |
|
|
302 |
The typical HOL tools are already set up in a way that
|
|
303 |
function definitions introduced by \isasymFUN, \isasymFUNCTION,
|
|
304 |
\isasymPRIMREC, \isasymRECDEF are implicitly propagated
|
|
305 |
to this function equation table. Specific theorems may be
|
|
306 |
selected using an attribute: \emph{code func}. As example,
|
|
307 |
a weight selector function:%
|
|
308 |
\end{isamarkuptext}%
|
|
309 |
\isamarkuptrue%
|
|
310 |
\isacommand{consts}\isamarkupfalse%
|
|
311 |
\isanewline
|
|
312 |
\ \ pick\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
|
|
313 |
\isanewline
|
|
314 |
\isacommand{primrec}\isamarkupfalse%
|
|
315 |
\isanewline
|
|
316 |
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharequal}\ x\ in\isanewline
|
|
317 |
\ \ \ \ if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
|
|
318 |
\begin{isamarkuptext}%
|
|
319 |
We want to eliminate the explicit destruction
|
|
320 |
of \isa{x} to \isa{{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}}:%
|
|
321 |
\end{isamarkuptext}%
|
|
322 |
\isamarkuptrue%
|
|
323 |
\isacommand{lemma}\isamarkupfalse%
|
|
324 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
325 |
\ \ {\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
|
|
326 |
%
|
|
327 |
\isadelimproof
|
|
328 |
\ \ %
|
|
329 |
\endisadelimproof
|
|
330 |
%
|
|
331 |
\isatagproof
|
|
332 |
\isacommand{by}\isamarkupfalse%
|
|
333 |
\ simp%
|
|
334 |
\endisatagproof
|
|
335 |
{\isafoldproof}%
|
|
336 |
%
|
|
337 |
\isadelimproof
|
|
338 |
\isanewline
|
|
339 |
%
|
|
340 |
\endisadelimproof
|
|
341 |
\isanewline
|
|
342 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
343 |
\ pick\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
344 |
\begin{isamarkuptext}%
|
|
345 |
This theorem is now added to the function equation table:
|
|
346 |
|
|
347 |
\lstsml{Thy/examples/pick1.ML}
|
|
348 |
|
|
349 |
It might be convenient to remove the pointless original
|
|
350 |
equation, using the \emph{nofunc} attribute:%
|
|
351 |
\end{isamarkuptext}%
|
|
352 |
\isamarkuptrue%
|
|
353 |
\isacommand{lemmas}\isamarkupfalse%
|
|
354 |
\ {\isacharbrackleft}code\ nofunc{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline
|
|
355 |
\isanewline
|
|
356 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
357 |
\ pick\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
358 |
\begin{isamarkuptext}%
|
|
359 |
\lstsml{Thy/examples/pick2.ML}
|
|
360 |
|
|
361 |
Syntactic redundancies are implicitly dropped. For example,
|
|
362 |
using a modified version of the \isa{fac} function
|
|
363 |
as function equation, the then redundant (since
|
|
364 |
syntactically subsumed) original function equations
|
|
365 |
are dropped, resulting in a warning:%
|
|
366 |
\end{isamarkuptext}%
|
|
367 |
\isamarkuptrue%
|
|
368 |
\isacommand{lemma}\isamarkupfalse%
|
|
369 |
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
|
|
370 |
\ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
371 |
%
|
|
372 |
\isadelimproof
|
|
373 |
\ \ %
|
|
374 |
\endisadelimproof
|
|
375 |
%
|
|
376 |
\isatagproof
|
|
377 |
\isacommand{by}\isamarkupfalse%
|
|
378 |
\ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all%
|
|
379 |
\endisatagproof
|
|
380 |
{\isafoldproof}%
|
|
381 |
%
|
|
382 |
\isadelimproof
|
|
383 |
\isanewline
|
|
384 |
%
|
|
385 |
\endisadelimproof
|
|
386 |
\isanewline
|
|
387 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
388 |
\ fac\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
389 |
\begin{isamarkuptext}%
|
|
390 |
\lstsml{Thy/examples/fac_case.ML}
|
|
391 |
|
|
392 |
\begin{warn}
|
|
393 |
Some statements in this section have to be treated with some
|
|
394 |
caution. First, since the HOL function package is still
|
|
395 |
under development, its setup with respect to code generation
|
|
396 |
may differ from what is presumed here.
|
|
397 |
Further, the attributes \emph{code} and \emph{code del}
|
|
398 |
associated with the existing code generator also apply to
|
|
399 |
the new one: \emph{code} implies \emph{code func},
|
|
400 |
and \emph{code del} implies \emph{code nofunc}.
|
|
401 |
\end{warn}%
|
|
402 |
\end{isamarkuptext}%
|
|
403 |
\isamarkuptrue%
|
|
404 |
%
|
20967
|
405 |
\isamarkupsubsection{Type classes%
|
|
406 |
}
|
|
407 |
\isamarkuptrue%
|
|
408 |
%
|
21172
|
409 |
\begin{isamarkuptext}%
|
|
410 |
Type classes enter the game via the Isar class package.
|
|
411 |
For a short introduction how to use it, see \cite{isabelle-classes};
|
|
412 |
here we just illustrate its impact on code generation.
|
|
413 |
|
|
414 |
In a target language, type classes may be represented
|
21186
|
415 |
natively (as in the case of Haskell). For languages
|
21172
|
416 |
like SML, they are implemented using \emph{dictionaries}.
|
21186
|
417 |
Our following example specifies a class \qt{null},
|
21172
|
418 |
assigning to each of its inhabitants a \qt{null} value:%
|
|
419 |
\end{isamarkuptext}%
|
|
420 |
\isamarkuptrue%
|
|
421 |
\isacommand{class}\isamarkupfalse%
|
|
422 |
\ null\ {\isacharequal}\isanewline
|
|
423 |
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
|
|
424 |
\isanewline
|
|
425 |
\isacommand{consts}\isamarkupfalse%
|
|
426 |
\isanewline
|
|
427 |
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
|
|
428 |
\isanewline
|
|
429 |
\isacommand{primrec}\isamarkupfalse%
|
|
430 |
\isanewline
|
|
431 |
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline
|
|
432 |
\ \ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}%
|
|
433 |
\begin{isamarkuptext}%
|
|
434 |
We provide some instances for our \isa{null}:%
|
|
435 |
\end{isamarkuptext}%
|
|
436 |
\isamarkuptrue%
|
|
437 |
\isacommand{instance}\isamarkupfalse%
|
|
438 |
\ option\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
|
|
439 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ None{\isachardoublequoteclose}%
|
|
440 |
\isadelimproof
|
|
441 |
\ %
|
|
442 |
\endisadelimproof
|
|
443 |
%
|
|
444 |
\isatagproof
|
|
445 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
|
446 |
%
|
|
447 |
\endisatagproof
|
|
448 |
{\isafoldproof}%
|
|
449 |
%
|
|
450 |
\isadelimproof
|
|
451 |
%
|
|
452 |
\endisadelimproof
|
|
453 |
\isanewline
|
|
454 |
\isanewline
|
|
455 |
\isacommand{instance}\isamarkupfalse%
|
|
456 |
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
|
|
457 |
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}%
|
|
458 |
\isadelimproof
|
|
459 |
\ %
|
|
460 |
\endisadelimproof
|
|
461 |
%
|
|
462 |
\isatagproof
|
|
463 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
|
464 |
%
|
|
465 |
\endisatagproof
|
|
466 |
{\isafoldproof}%
|
|
467 |
%
|
|
468 |
\isadelimproof
|
|
469 |
%
|
|
470 |
\endisadelimproof
|
|
471 |
%
|
|
472 |
\begin{isamarkuptext}%
|
|
473 |
Constructing a dummy example:%
|
|
474 |
\end{isamarkuptext}%
|
|
475 |
\isamarkuptrue%
|
|
476 |
\isacommand{definition}\isamarkupfalse%
|
|
477 |
\isanewline
|
|
478 |
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}%
|
|
479 |
\begin{isamarkuptext}%
|
21186
|
480 |
Type classes offer a suitable occasion to introduce
|
21172
|
481 |
the Haskell serializer. Its usage is almost the same
|
|
482 |
as SML, but, in accordance with conventions
|
|
483 |
some Haskell systems enforce, each module ends
|
|
484 |
up in a single file. The module hierarchy is reflected in
|
|
485 |
the file system, with root given by the user.%
|
|
486 |
\end{isamarkuptext}%
|
|
487 |
\isamarkuptrue%
|
|
488 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
489 |
\ dummy\ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}{\isacharparenright}%
|
|
490 |
\begin{isamarkuptext}%
|
|
491 |
\lsthaskell{Thy/examples/Codegen.hs}
|
|
492 |
|
|
493 |
(we have left out all other modules).
|
|
494 |
|
|
495 |
The whole code in SML with explicit dictionary passing:%
|
|
496 |
\end{isamarkuptext}%
|
|
497 |
\isamarkuptrue%
|
|
498 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
499 |
\ dummy\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
500 |
\begin{isamarkuptext}%
|
|
501 |
\lstsml{Thy/examples/class.ML}%
|
|
502 |
\end{isamarkuptext}%
|
20967
|
503 |
\isamarkuptrue%
|
|
504 |
%
|
|
505 |
\isamarkupsubsection{Incremental code generation%
|
|
506 |
}
|
|
507 |
\isamarkuptrue%
|
|
508 |
%
|
21172
|
509 |
\begin{isamarkuptext}%
|
|
510 |
Code generation is \emph{incremental}: theorems
|
|
511 |
and abstract intermediate code are cached and extended on demand.
|
|
512 |
The cache may be partially or fully dropped if the underlying
|
|
513 |
executable content of the theory changes.
|
|
514 |
Implementation of caching is supposed to transparently
|
|
515 |
hid away the details from the user. Anyway, caching
|
|
516 |
reaches the surface by using a slightly more general form
|
|
517 |
of the \isasymCODEGEN: either the list of constants or the
|
|
518 |
list of serialization expressions may be dropped. If no
|
|
519 |
serialization expressions are given, only abstract code
|
|
520 |
is generated and cached; if no constants are given, the
|
|
521 |
current cache is serialized.
|
|
522 |
|
|
523 |
For explorative reasons, an extended version of the
|
|
524 |
\isasymCODEGEN command may prove useful:%
|
|
525 |
\end{isamarkuptext}%
|
|
526 |
\isamarkuptrue%
|
|
527 |
\isacommand{print{\isacharunderscore}codethms}\isamarkupfalse%
|
|
528 |
\ {\isacharparenleft}{\isacharparenright}%
|
|
529 |
\begin{isamarkuptext}%
|
|
530 |
\noindent print all cached function equations (i.e.~\emph{after}
|
|
531 |
any applied transformation. Inside the brackets a
|
|
532 |
list of constants may be given; their function
|
21186
|
533 |
equations are added to the cache if not already present.%
|
21172
|
534 |
\end{isamarkuptext}%
|
|
535 |
\isamarkuptrue%
|
|
536 |
%
|
|
537 |
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}%
|
|
538 |
}
|
|
539 |
\isamarkuptrue%
|
|
540 |
%
|
|
541 |
\begin{isamarkuptext}%
|
|
542 |
In this tutorial, we do not attempt to give an exhaustive
|
|
543 |
description of the code generator framework; instead,
|
|
544 |
we cast a light on advanced topics by introducing
|
|
545 |
them together with practically motivated examples. Concerning
|
|
546 |
further reading, see
|
|
547 |
|
|
548 |
\begin{itemize}
|
|
549 |
|
|
550 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
|
|
551 |
for exhaustive syntax diagrams.
|
|
552 |
\item or \fixme{ref} which deals with foundational issues
|
|
553 |
of the code generator framework.
|
|
554 |
|
|
555 |
\end{itemize}%
|
|
556 |
\end{isamarkuptext}%
|
|
557 |
\isamarkuptrue%
|
|
558 |
%
|
|
559 |
\isamarkupsubsection{Library theories%
|
|
560 |
}
|
|
561 |
\isamarkuptrue%
|
|
562 |
%
|
|
563 |
\begin{isamarkuptext}%
|
|
564 |
The HOL \emph{Main} theory already provides a code generator setup
|
|
565 |
which should be suitable for most applications. Common extensions
|
|
566 |
and modifications are available by certain theories of the HOL
|
|
567 |
library; beside being useful in applications, they may serve
|
21186
|
568 |
as a tutorial for customizing the code generator setup.
|
21172
|
569 |
|
|
570 |
\begin{description}
|
|
571 |
|
|
572 |
\item[ExecutableSet] allows to generate code
|
|
573 |
for finite sets using lists.
|
|
574 |
\item[ExecutableRat] implements rational
|
|
575 |
numbers as triples \isa{{\isacharparenleft}sign{\isacharcomma}\ enumerator{\isacharcomma}\ denominator{\isacharparenright}}.
|
|
576 |
\item[EfficientNat] implements natural numbers by integers,
|
21186
|
577 |
which in general will result in higher efficency; pattern
|
21172
|
578 |
matching with \isa{{\isadigit{0}}} / \isa{Suc}
|
|
579 |
is eliminated. \label{eff_nat}
|
|
580 |
\item[MLString] provides an additional datatype \isa{mlstring};
|
|
581 |
in the HOL default setup, strings in HOL are mapped to list
|
|
582 |
of chars in SML; values of type \isa{mlstring} are
|
|
583 |
mapped to strings in SML.
|
|
584 |
|
|
585 |
\end{description}%
|
|
586 |
\end{isamarkuptext}%
|
|
587 |
\isamarkuptrue%
|
|
588 |
%
|
|
589 |
\isamarkupsubsection{Preprocessing%
|
20967
|
590 |
}
|
|
591 |
\isamarkuptrue%
|
|
592 |
%
|
21172
|
593 |
\begin{isamarkuptext}%
|
|
594 |
Before selected function theorems are turned into abstract
|
|
595 |
code, a chain of definitional transformation steps is carried
|
21186
|
596 |
out: \emph{preprocessing}. There are three possibilities
|
21172
|
597 |
to customize preprocessing: \emph{inline theorems},
|
|
598 |
\emph{inline procedures} and \emph{generic preprocessors}.
|
|
599 |
|
|
600 |
\emph{Inline theorems} are rewriting rules applied to each
|
|
601 |
function equation. Due to the interpretation of theorems
|
|
602 |
of function equations, rewrites are applied to the right
|
|
603 |
hand side and the arguments of the left hand side of an
|
|
604 |
equation, but never to the constant heading the left hand side.
|
|
605 |
Inline theorems may be declared an undeclared using the
|
21186
|
606 |
\emph{code inline} or \emph{code noinline} attribute respectively.
|
21172
|
607 |
|
|
608 |
Some common applications:%
|
|
609 |
\end{isamarkuptext}%
|
|
610 |
\isamarkuptrue%
|
|
611 |
%
|
|
612 |
\begin{itemize}
|
|
613 |
\item replacing non-executable constructs by executable ones: \\
|
|
614 |
\isacommand{lemma}\isamarkupfalse%
|
|
615 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
616 |
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}%
|
|
617 |
\isadelimproof
|
|
618 |
\ %
|
|
619 |
\endisadelimproof
|
|
620 |
%
|
|
621 |
\isatagproof
|
|
622 |
\isacommand{by}\isamarkupfalse%
|
|
623 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
|
|
624 |
\endisatagproof
|
|
625 |
{\isafoldproof}%
|
|
626 |
%
|
|
627 |
\isadelimproof
|
|
628 |
%
|
|
629 |
\endisadelimproof
|
|
630 |
%
|
|
631 |
\item eliminating superfluous constants: \\
|
|
632 |
\isacommand{lemma}\isamarkupfalse%
|
|
633 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
634 |
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}%
|
|
635 |
\isadelimproof
|
|
636 |
\ %
|
|
637 |
\endisadelimproof
|
|
638 |
%
|
|
639 |
\isatagproof
|
|
640 |
\isacommand{by}\isamarkupfalse%
|
|
641 |
\ simp%
|
|
642 |
\endisatagproof
|
|
643 |
{\isafoldproof}%
|
|
644 |
%
|
|
645 |
\isadelimproof
|
|
646 |
%
|
|
647 |
\endisadelimproof
|
|
648 |
%
|
|
649 |
\item replacing executable but inconvenient constructs: \\
|
|
650 |
\isacommand{lemma}\isamarkupfalse%
|
|
651 |
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
|
|
652 |
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}%
|
|
653 |
\isadelimproof
|
|
654 |
\ %
|
|
655 |
\endisadelimproof
|
|
656 |
%
|
|
657 |
\isatagproof
|
|
658 |
\isacommand{by}\isamarkupfalse%
|
|
659 |
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
|
|
660 |
\endisatagproof
|
|
661 |
{\isafoldproof}%
|
|
662 |
%
|
|
663 |
\isadelimproof
|
|
664 |
%
|
|
665 |
\endisadelimproof
|
|
666 |
%
|
|
667 |
\end{itemize}
|
|
668 |
%
|
|
669 |
\begin{isamarkuptext}%
|
|
670 |
The current set of inline theorems may be inspected using
|
|
671 |
the \isasymPRINTCODETHMS command.
|
|
672 |
|
|
673 |
\emph{Inline procedures} are a generalized version of inline
|
|
674 |
theorems written in ML -- rewrite rules are generated dependent
|
|
675 |
on the function theorems for a certain function. One
|
|
676 |
application is the implicit expanding of \isa{nat} numerals
|
|
677 |
to \isa{{\isadigit{0}}} / \isa{Suc} representation. See further
|
|
678 |
\secref{sec:ml}
|
|
679 |
|
|
680 |
\emph{Generic preprocessors} provide a most general interface,
|
|
681 |
transforming a list of function theorems to another
|
|
682 |
list of function theorems, provided that neither the heading
|
|
683 |
constant nor its type change. The \isa{{\isadigit{0}}} / \isa{Suc}
|
|
684 |
pattern elimination implemented in \secref{eff_nat} uses this
|
|
685 |
interface.
|
|
686 |
|
|
687 |
\begin{warn}
|
|
688 |
The order in which single preprocessing steps are carried
|
|
689 |
out currently is not specified; in particular, preprocessing
|
21186
|
690 |
is \emph{no} fix point process. Keep this in mind when
|
21172
|
691 |
setting up the preprocessor.
|
|
692 |
|
|
693 |
Further, the attribute \emph{code unfold}
|
|
694 |
associated with the existing code generator also applies to
|
|
695 |
the new one: \emph{code unfold} implies \emph{code inline}.
|
|
696 |
\end{warn}%
|
|
697 |
\end{isamarkuptext}%
|
|
698 |
\isamarkuptrue%
|
|
699 |
%
|
|
700 |
\isamarkupsubsection{Customizing serialization%
|
|
701 |
}
|
|
702 |
\isamarkuptrue%
|
|
703 |
%
|
|
704 |
\begin{isamarkuptext}%
|
|
705 |
Consider the following function and its corresponding
|
|
706 |
SML code:%
|
|
707 |
\end{isamarkuptext}%
|
|
708 |
\isamarkuptrue%
|
|
709 |
\isacommand{fun}\isamarkupfalse%
|
|
710 |
\isanewline
|
|
711 |
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
712 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}\isanewline
|
|
713 |
\isacommand{termination}\isamarkupfalse%
|
|
714 |
%
|
|
715 |
\isadelimproof
|
|
716 |
\ %
|
|
717 |
\endisadelimproof
|
|
718 |
%
|
|
719 |
\isatagproof
|
|
720 |
\isacommand{by}\isamarkupfalse%
|
|
721 |
\ {\isacharparenleft}auto{\isacharunderscore}term\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}{\isacharparenright}%
|
|
722 |
\endisatagproof
|
|
723 |
{\isafoldproof}%
|
|
724 |
%
|
|
725 |
\isadelimproof
|
|
726 |
%
|
|
727 |
\endisadelimproof
|
|
728 |
\isanewline
|
|
729 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
730 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
731 |
\begin{isamarkuptext}%
|
|
732 |
\lstsml{Thy/examples/bool1.ML}
|
|
733 |
|
|
734 |
Though this is correct code, it is a little bit unsatisfactory:
|
|
735 |
boolean values and operators are materialized as distinguished
|
|
736 |
entities with have nothing to do with the SML-builtin notion
|
|
737 |
of \qt{bool}. This results in less readable code;
|
|
738 |
additionally, eager evaluation may cause programs to
|
|
739 |
loop or break which would perfectly terminate when
|
|
740 |
the existing SML \qt{bool} would be used. To map
|
|
741 |
the HOL \qt{bool} on SML \qt{bool}, we may use
|
|
742 |
\qn{custom serializations}:%
|
|
743 |
\end{isamarkuptext}%
|
|
744 |
\isamarkuptrue%
|
|
745 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
746 |
\ bool\isanewline
|
|
747 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
748 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
749 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
|
|
750 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}%
|
|
751 |
\begin{isamarkuptext}%
|
|
752 |
The \isasymCODETYPE commad takes a type constructor
|
|
753 |
as arguments together with a list of custom serializations.
|
|
754 |
Each custom serialization starts with a target language
|
|
755 |
identifier followed by an expression, which during
|
|
756 |
code serialization is inserted whenever the type constructor
|
|
757 |
would occur. For constants, \isasymCODECONST implements
|
|
758 |
the corresponding mechanism. Each \qt{\_} in
|
|
759 |
a serialization expression is treated as a placeholder
|
|
760 |
for the type constructor's (the constant's) arguments.%
|
|
761 |
\end{isamarkuptext}%
|
|
762 |
\isamarkuptrue%
|
|
763 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse%
|
|
764 |
\ SML\isanewline
|
|
765 |
\ \ bool\ true\ false%
|
|
766 |
\begin{isamarkuptext}%
|
|
767 |
To assert that the existing \qt{bool}, \qt{true} and \qt{false}
|
|
768 |
is not used for generated code, we use \isasymCODERESERVED.
|
|
769 |
|
|
770 |
After this setup, code looks quite more readable:%
|
|
771 |
\end{isamarkuptext}%
|
|
772 |
\isamarkuptrue%
|
|
773 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
774 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
775 |
\begin{isamarkuptext}%
|
|
776 |
\lstsml{Thy/examples/bool2.ML}
|
|
777 |
|
|
778 |
This still is not perfect: the parentheses
|
21186
|
779 |
around \qt{andalso} are superfluous. Though the serializer
|
21172
|
780 |
by no means attempts to imitate the rich Isabelle syntax
|
|
781 |
framework, it provides some common idioms, notably
|
|
782 |
associative infixes with precedences which may be used here:%
|
|
783 |
\end{isamarkuptext}%
|
|
784 |
\isamarkuptrue%
|
|
785 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
786 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
|
|
787 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
788 |
\isanewline
|
|
789 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
790 |
\ in{\isacharunderscore}interval\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isadigit{3}}{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
791 |
\begin{isamarkuptext}%
|
|
792 |
\lstsml{Thy/examples/bool3.ML}
|
|
793 |
|
|
794 |
Next, we try to map HOL pairs to SML pairs, using the
|
|
795 |
infix \qt{ * } type constructor and parentheses:%
|
|
796 |
\end{isamarkuptext}%
|
|
797 |
\isamarkuptrue%
|
|
798 |
\isanewline
|
|
799 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
800 |
\ {\isacharasterisk}\isanewline
|
|
801 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
802 |
\isanewline
|
|
803 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
804 |
\ Pair\isanewline
|
|
805 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
|
806 |
\begin{isamarkuptext}%
|
|
807 |
The initial bang \qt{!} tells the serializer to never put
|
|
808 |
parentheses around the whole expression (they are already present),
|
|
809 |
while the parentheses around argument place holders
|
|
810 |
tell not to put parentheses around the arguments.
|
|
811 |
The slash \qt{/} (followed by arbitrary white space)
|
|
812 |
inserts a space which may be used as a break if necessary
|
|
813 |
during pretty printing.
|
|
814 |
|
21186
|
815 |
So far, we did only provide more idiomatic serializations for
|
|
816 |
constructs which would be executable on their own. Target-specific
|
|
817 |
serializations may also be used to \emph{implement} constructs
|
|
818 |
which have no implicit notion of executability. For example,
|
|
819 |
take the HOL integers:%
|
|
820 |
\end{isamarkuptext}%
|
|
821 |
\isamarkuptrue%
|
|
822 |
\isacommand{definition}\isamarkupfalse%
|
|
823 |
\isanewline
|
|
824 |
\ \ double{\isacharunderscore}inc\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline
|
|
825 |
\ \ {\isachardoublequoteopen}double{\isacharunderscore}inc\ k\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ k\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
|
|
826 |
\isanewline
|
|
827 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
828 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
829 |
\begin{isamarkuptext}%
|
|
830 |
will fail: \isa{int} in HOL is implemented using a quotient
|
|
831 |
type, which does not provide any notion of executability.
|
|
832 |
\footnote{Eventually, we also want to provide executability
|
|
833 |
for quotients.}. However, we could use the SML builtin
|
|
834 |
integers:%
|
21172
|
835 |
\end{isamarkuptext}%
|
|
836 |
\isamarkuptrue%
|
|
837 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
838 |
\ int\isanewline
|
|
839 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}IntInf{\isachardot}int{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
840 |
\isanewline
|
|
841 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
842 |
\ {\isachardoublequoteopen}op\ {\isacharplus}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline
|
|
843 |
\ \ \ \ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isacharasterisk}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ int{\isachardoublequoteclose}\isanewline
|
21186
|
844 |
\ \ {\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}\isanewline
|
|
845 |
\isanewline
|
|
846 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
847 |
\ double{\isacharunderscore}inc\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}integers{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
848 |
\begin{isamarkuptext}%
|
|
849 |
resulting in:
|
|
850 |
|
|
851 |
\lstsml{Thy/examples/integers.ML}%
|
|
852 |
\end{isamarkuptext}%
|
|
853 |
\isamarkuptrue%
|
|
854 |
%
|
|
855 |
\begin{isamarkuptext}%
|
|
856 |
These examples give a glimpse what powerful mechanisms
|
|
857 |
custom serializations provide; however their usage
|
|
858 |
requires careful thinking in order not to introduce
|
|
859 |
inconsistencies -- or, in other words:
|
|
860 |
custom serializations are completely axiomatic.
|
|
861 |
|
|
862 |
A further noteworthy details is that any special
|
|
863 |
character in a custom serialization may be quoted
|
|
864 |
using \qt{'}; thus, in \qt{fn '\_ => \_} the first
|
|
865 |
\qt{'\_} is a proper underscore while the
|
|
866 |
second \qt{\_} is a placeholder.
|
|
867 |
|
|
868 |
The HOL theories provide further
|
|
869 |
examples for custom serializations and form
|
|
870 |
a recommended tutorial on how to use them properly.%
|
|
871 |
\end{isamarkuptext}%
|
21172
|
872 |
\isamarkuptrue%
|
|
873 |
%
|
|
874 |
\isamarkupsubsection{Concerning operational equality%
|
20967
|
875 |
}
|
|
876 |
\isamarkuptrue%
|
|
877 |
%
|
21172
|
878 |
\begin{isamarkuptext}%
|
|
879 |
Surely you have already noticed how equality is treated
|
|
880 |
by the code generator:%
|
|
881 |
\end{isamarkuptext}%
|
|
882 |
\isamarkuptrue%
|
|
883 |
\isacommand{fun}\isamarkupfalse%
|
|
884 |
\isanewline
|
|
885 |
\ \ 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
|
|
886 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
|
|
887 |
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
|
|
888 |
\ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
|
|
889 |
\ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
|
|
890 |
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
|
|
891 |
\ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}\isanewline
|
|
892 |
\isacommand{termination}\isamarkupfalse%
|
|
893 |
%
|
|
894 |
\isadelimproof
|
|
895 |
\ %
|
|
896 |
\endisadelimproof
|
|
897 |
%
|
|
898 |
\isatagproof
|
|
899 |
\isacommand{by}\isamarkupfalse%
|
|
900 |
\ {\isacharparenleft}auto{\isacharunderscore}term\ {\isachardoublequoteopen}measure\ {\isacharparenleft}length\ o\ snd\ o\ snd{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
|
901 |
\endisatagproof
|
|
902 |
{\isafoldproof}%
|
|
903 |
%
|
|
904 |
\isadelimproof
|
|
905 |
%
|
|
906 |
\endisadelimproof
|
|
907 |
%
|
|
908 |
\begin{isamarkuptext}%
|
21186
|
909 |
The membership test during preprocessing is rewriting,
|
21172
|
910 |
resulting in \isa{op\ mem}, which itself
|
|
911 |
performs an explicit equality check.%
|
|
912 |
\end{isamarkuptext}%
|
|
913 |
\isamarkuptrue%
|
|
914 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
915 |
\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
916 |
\begin{isamarkuptext}%
|
|
917 |
\lstsml{Thy/examples/collect_duplicates.ML}%
|
|
918 |
\end{isamarkuptext}%
|
|
919 |
\isamarkuptrue%
|
|
920 |
%
|
|
921 |
\begin{isamarkuptext}%
|
|
922 |
Obviously, polymorphic equality is implemented the Haskell
|
|
923 |
way using a type class. How is this achieved? By an
|
|
924 |
almost trivial definition in the HOL setup:%
|
|
925 |
\end{isamarkuptext}%
|
|
926 |
\isamarkuptrue%
|
|
927 |
%
|
|
928 |
\isadelimML
|
|
929 |
%
|
|
930 |
\endisadelimML
|
|
931 |
%
|
|
932 |
\isatagML
|
|
933 |
%
|
|
934 |
\endisatagML
|
|
935 |
{\isafoldML}%
|
|
936 |
%
|
|
937 |
\isadelimML
|
|
938 |
\isanewline
|
|
939 |
%
|
|
940 |
\endisadelimML
|
|
941 |
\isacommand{class}\isamarkupfalse%
|
|
942 |
\ eq\ {\isacharequal}\isanewline
|
|
943 |
\ \ \isakeyword{fixes}\ eq\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
|
|
944 |
\isanewline
|
|
945 |
\isacommand{defs}\isamarkupfalse%
|
|
946 |
\isanewline
|
21186
|
947 |
\ \ eq\ {\isacharcolon}\ {\isachardoublequoteopen}eq\ {\isasymequiv}\ {\isacharparenleft}op\ {\isacharequal}{\isacharparenright}{\isachardoublequoteclose}%
|
21172
|
948 |
\begin{isamarkuptext}%
|
|
949 |
This merely introduces a class \isa{eq} with corresponding
|
|
950 |
operation \isa{eq}, which by definition is isomorphic
|
|
951 |
to \isa{op\ {\isacharequal}}; the preprocessing framework does the rest.%
|
|
952 |
\end{isamarkuptext}%
|
|
953 |
\isamarkuptrue%
|
|
954 |
%
|
|
955 |
\isadelimML
|
|
956 |
%
|
|
957 |
\endisadelimML
|
|
958 |
%
|
|
959 |
\isatagML
|
|
960 |
%
|
|
961 |
\endisatagML
|
|
962 |
{\isafoldML}%
|
|
963 |
%
|
|
964 |
\isadelimML
|
|
965 |
%
|
|
966 |
\endisadelimML
|
|
967 |
%
|
|
968 |
\begin{isamarkuptext}%
|
|
969 |
For datatypes, instances of \isa{eq} are implicitly derived
|
|
970 |
when possible.
|
|
971 |
|
|
972 |
Though this class is designed to get rarely in the way, there
|
|
973 |
are some cases when it suddenly comes to surface:%
|
|
974 |
\end{isamarkuptext}%
|
|
975 |
\isamarkuptrue%
|
|
976 |
%
|
21186
|
977 |
\isamarkupsubsubsection{code lemmas and customary serializations for equality%
|
|
978 |
}
|
|
979 |
\isamarkuptrue%
|
|
980 |
%
|
|
981 |
\begin{isamarkuptext}%
|
|
982 |
Examine the following:%
|
|
983 |
\end{isamarkuptext}%
|
|
984 |
\isamarkuptrue%
|
21172
|
985 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
986 |
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
|
|
987 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharunderscore}\ {\isacharcolon}\ IntInf{\isachardot}int\ {\isacharequal}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
21186
|
988 |
\begin{isamarkuptext}%
|
|
989 |
What is wrong here? Since \isa{op\ {\isacharequal}} is nothing else then
|
21172
|
990 |
a plain constant, this customary serialization will refer
|
|
991 |
to polymorphic equality \isa{op\ {\isacharequal}}.
|
|
992 |
Instead, we want the specific equality on \isa{int},
|
21186
|
993 |
by using the overloaded constant \isa{Code{\isacharunderscore}Generator{\isachardot}eq}:%
|
|
994 |
\end{isamarkuptext}%
|
|
995 |
\isamarkuptrue%
|
21172
|
996 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
997 |
\ {\isachardoublequoteopen}Code{\isacharunderscore}Generator{\isachardot}eq\ {\isasymColon}\ int\ {\isasymRightarrow}\ int\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
|
|
998 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharunderscore}\ {\isacharcolon}\ IntInf{\isachardot}int\ {\isacharequal}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
21186
|
999 |
\isamarkupsubsubsection{typedecls interpretated by customary serializations%
|
|
1000 |
}
|
|
1001 |
\isamarkuptrue%
|
|
1002 |
%
|
|
1003 |
\begin{isamarkuptext}%
|
|
1004 |
A common idiom is to use unspecified types for formalizations
|
|
1005 |
and interpret them for a specific target language:%
|
|
1006 |
\end{isamarkuptext}%
|
|
1007 |
\isamarkuptrue%
|
21172
|
1008 |
\isacommand{typedecl}\isamarkupfalse%
|
|
1009 |
\ key\isanewline
|
|
1010 |
\isanewline
|
|
1011 |
\isacommand{fun}\isamarkupfalse%
|
|
1012 |
\isanewline
|
|
1013 |
\ \ lookup\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}key\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
|
|
1014 |
\ \ {\isachardoublequoteopen}lookup\ {\isacharbrackleft}{\isacharbrackright}\ l\ {\isacharequal}\ None{\isachardoublequoteclose}\isanewline
|
|
1015 |
\ \ {\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
|
|
1016 |
\isacommand{termination}\isamarkupfalse%
|
|
1017 |
%
|
|
1018 |
\isadelimproof
|
|
1019 |
\ %
|
|
1020 |
\endisadelimproof
|
|
1021 |
%
|
|
1022 |
\isatagproof
|
|
1023 |
\isacommand{by}\isamarkupfalse%
|
|
1024 |
\ {\isacharparenleft}auto{\isacharunderscore}term\ {\isachardoublequoteopen}measure\ {\isacharparenleft}length\ o\ fst{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
|
1025 |
\endisatagproof
|
|
1026 |
{\isafoldproof}%
|
|
1027 |
%
|
|
1028 |
\isadelimproof
|
|
1029 |
%
|
|
1030 |
\endisadelimproof
|
|
1031 |
\isanewline
|
|
1032 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
1033 |
\ key\isanewline
|
|
1034 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}string{\isachardoublequoteclose}{\isacharparenright}%
|
21186
|
1035 |
\begin{isamarkuptext}%
|
|
1036 |
This, though, is not sufficient: \isa{key} is no instance
|
21172
|
1037 |
of \isa{eq} since \isa{key} is no datatype; the instance
|
|
1038 |
has to be declared manually, including a serialization
|
21186
|
1039 |
for the particular instance of \isa{Code{\isacharunderscore}Generator{\isachardot}eq}:%
|
|
1040 |
\end{isamarkuptext}%
|
|
1041 |
\isamarkuptrue%
|
21172
|
1042 |
\isacommand{instance}\isamarkupfalse%
|
|
1043 |
\ key\ {\isacharcolon}{\isacharcolon}\ eq%
|
|
1044 |
\isadelimproof
|
|
1045 |
\ %
|
|
1046 |
\endisadelimproof
|
|
1047 |
%
|
|
1048 |
\isatagproof
|
|
1049 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
|
1050 |
%
|
|
1051 |
\endisatagproof
|
|
1052 |
{\isafoldproof}%
|
|
1053 |
%
|
|
1054 |
\isadelimproof
|
|
1055 |
%
|
|
1056 |
\endisadelimproof
|
|
1057 |
\isanewline
|
|
1058 |
\isanewline
|
|
1059 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
1060 |
\ {\isachardoublequoteopen}Code{\isacharunderscore}Generator{\isachardot}eq\ {\isasymColon}\ key\ {\isasymRightarrow}\ key\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
|
|
1061 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharunderscore}\ {\isacharcolon}\ string\ {\isacharequal}\ {\isacharunderscore}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
|
21186
|
1062 |
\begin{isamarkuptext}%
|
|
1063 |
Then everything goes fine:%
|
|
1064 |
\end{isamarkuptext}%
|
|
1065 |
\isamarkuptrue%
|
21172
|
1066 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
1067 |
\ lookup\ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lookup{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
1068 |
\begin{isamarkuptext}%
|
|
1069 |
\lstsml{Thy/examples/lookup.ML}%
|
|
1070 |
\end{isamarkuptext}%
|
|
1071 |
\isamarkuptrue%
|
|
1072 |
%
|
21186
|
1073 |
\isamarkupsubsubsection{lexicographic orderings and coregularity%
|
|
1074 |
}
|
|
1075 |
\isamarkuptrue%
|
|
1076 |
%
|
|
1077 |
\begin{isamarkuptext}%
|
|
1078 |
Another subtlety
|
21172
|
1079 |
enters the stage when definitions of overloaded constants
|
|
1080 |
are dependent on operational equality. For example, let
|
21186
|
1081 |
us define a lexicographic ordering on tuples: \\%
|
|
1082 |
\end{isamarkuptext}%
|
|
1083 |
\isamarkuptrue%
|
|
1084 |
%
|
|
1085 |
\isadelimML
|
|
1086 |
%
|
|
1087 |
\endisadelimML
|
|
1088 |
%
|
|
1089 |
\isatagML
|
|
1090 |
%
|
|
1091 |
\endisatagML
|
|
1092 |
{\isafoldML}%
|
|
1093 |
%
|
|
1094 |
\isadelimML
|
|
1095 |
%
|
|
1096 |
\endisadelimML
|
21172
|
1097 |
\isanewline
|
21186
|
1098 |
\isacommand{instance}\isamarkupfalse%
|
|
1099 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline
|
|
1100 |
\ \ {\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
|
|
1101 |
\ \ \ \ 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
|
|
1102 |
\ \ {\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}%
|
|
1103 |
\isadelimproof
|
|
1104 |
\ %
|
|
1105 |
\endisadelimproof
|
|
1106 |
%
|
|
1107 |
\isatagproof
|
|
1108 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
21172
|
1109 |
\isanewline
|
|
1110 |
%
|
21186
|
1111 |
\endisatagproof
|
|
1112 |
{\isafoldproof}%
|
|
1113 |
%
|
|
1114 |
\isadelimproof
|
|
1115 |
%
|
|
1116 |
\endisadelimproof
|
|
1117 |
%
|
|
1118 |
\isadelimML
|
|
1119 |
%
|
|
1120 |
\endisadelimML
|
|
1121 |
%
|
|
1122 |
\isatagML
|
|
1123 |
%
|
|
1124 |
\endisatagML
|
|
1125 |
{\isafoldML}%
|
|
1126 |
%
|
|
1127 |
\isadelimML
|
|
1128 |
%
|
|
1129 |
\endisadelimML
|
|
1130 |
%
|
|
1131 |
\begin{isamarkuptext}%
|
|
1132 |
Then code generation will fail. Why? The definition
|
21172
|
1133 |
of \isa{op\ {\isasymle}} depends on equality on both arguments,
|
21186
|
1134 |
which are polymorphic and impose an additional \isa{eq}
|
21172
|
1135 |
class constraint, thus violating the type discipline
|
|
1136 |
for class operations.
|
|
1137 |
|
21186
|
1138 |
The solution is to add \isa{eq} to both sort arguments:%
|
|
1139 |
\end{isamarkuptext}%
|
|
1140 |
\isamarkuptrue%
|
21172
|
1141 |
\isacommand{instance}\isamarkupfalse%
|
|
1142 |
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}{\isachardoublequoteopen}{\isacharbraceleft}eq{\isacharcomma}\ ord{\isacharbraceright}{\isachardoublequoteclose}{\isacharcomma}\ {\isachardoublequoteopen}{\isacharbraceleft}eq{\isacharcomma}\ ord{\isacharbraceright}{\isachardoublequoteclose}{\isacharparenright}\ ord\isanewline
|
21186
|
1143 |
\ \ {\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
|
1144 |
\ \ \ \ 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
|
1145 |
\ \ {\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
|
1146 |
\isadelimproof
|
|
1147 |
\ %
|
|
1148 |
\endisadelimproof
|
|
1149 |
%
|
|
1150 |
\isatagproof
|
|
1151 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
|
1152 |
%
|
|
1153 |
\endisatagproof
|
|
1154 |
{\isafoldproof}%
|
|
1155 |
%
|
|
1156 |
\isadelimproof
|
|
1157 |
%
|
|
1158 |
\endisadelimproof
|
|
1159 |
%
|
21186
|
1160 |
\begin{isamarkuptext}%
|
|
1161 |
Then code generation succeeds:%
|
|
1162 |
\end{isamarkuptext}%
|
|
1163 |
\isamarkuptrue%
|
21172
|
1164 |
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
|
|
1165 |
\ {\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
|
|
1166 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}{\isacharparenright}%
|
|
1167 |
\begin{isamarkuptext}%
|
|
1168 |
\lstsml{Thy/examples/lexicographic.ML}%
|
|
1169 |
\end{isamarkuptext}%
|
|
1170 |
\isamarkuptrue%
|
|
1171 |
%
|
21186
|
1172 |
\isamarkupsubsubsection{Haskell serialization%
|
|
1173 |
}
|
|
1174 |
\isamarkuptrue%
|
|
1175 |
%
|
|
1176 |
\begin{isamarkuptext}%
|
|
1177 |
For convenience, the default
|
|
1178 |
HOL setup for Haskell maps the \isa{eq} class to
|
|
1179 |
its counterpart in Haskell, giving custom serializations
|
|
1180 |
for the class (\isasymCODECLASS) and its operation:%
|
|
1181 |
\end{isamarkuptext}%
|
|
1182 |
\isamarkuptrue%
|
|
1183 |
%
|
|
1184 |
\isadelimML
|
|
1185 |
%
|
|
1186 |
\endisadelimML
|
|
1187 |
%
|
|
1188 |
\isatagML
|
|
1189 |
%
|
|
1190 |
\endisatagML
|
|
1191 |
{\isafoldML}%
|
|
1192 |
%
|
|
1193 |
\isadelimML
|
|
1194 |
%
|
|
1195 |
\endisadelimML
|
|
1196 |
\isanewline
|
|
1197 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse%
|
|
1198 |
\ eq\isanewline
|
|
1199 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ eq\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
1200 |
\isanewline
|
|
1201 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
1202 |
\ eq\isanewline
|
|
1203 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
1204 |
%
|
|
1205 |
\isadelimML
|
|
1206 |
%
|
|
1207 |
\endisadelimML
|
|
1208 |
%
|
|
1209 |
\isatagML
|
|
1210 |
%
|
|
1211 |
\endisatagML
|
|
1212 |
{\isafoldML}%
|
|
1213 |
%
|
|
1214 |
\isadelimML
|
|
1215 |
%
|
|
1216 |
\endisadelimML
|
21172
|
1217 |
%
|
21186
|
1218 |
\begin{isamarkuptext}%
|
|
1219 |
A problem now occurs whenever a type which
|
|
1220 |
is an instance of \isa{eq} in HOL is mapped
|
|
1221 |
on a Haskell-builtin type which is also an instance
|
|
1222 |
of Haskell \isa{Eq}:%
|
|
1223 |
\end{isamarkuptext}%
|
|
1224 |
\isamarkuptrue%
|
|
1225 |
\isacommand{typedecl}\isamarkupfalse%
|
|
1226 |
\ bar\isanewline
|
|
1227 |
\isanewline
|
|
1228 |
\isacommand{instance}\isamarkupfalse%
|
|
1229 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq%
|
|
1230 |
\isadelimproof
|
|
1231 |
\ %
|
|
1232 |
\endisadelimproof
|
|
1233 |
%
|
|
1234 |
\isatagproof
|
|
1235 |
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
|
|
1236 |
%
|
|
1237 |
\endisatagproof
|
|
1238 |
{\isafoldproof}%
|
|
1239 |
%
|
|
1240 |
\isadelimproof
|
21172
|
1241 |
%
|
21186
|
1242 |
\endisadelimproof
|
|
1243 |
\isanewline
|
|
1244 |
\isanewline
|
|
1245 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
1246 |
\ bar\isanewline
|
|
1247 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}%
|
|
1248 |
\begin{isamarkuptext}%
|
|
1249 |
The code generator would produce
|
|
1250 |
an additional instance, which of course is rejected.
|
|
1251 |
To suppress this additional instance, use
|
|
1252 |
\isasymCODEINSTANCE:%
|
|
1253 |
\end{isamarkuptext}%
|
|
1254 |
\isamarkuptrue%
|
|
1255 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse%
|
|
1256 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
|
|
1257 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}%
|
|
1258 |
\isamarkupsubsection{Types matter%
|
|
1259 |
}
|
|
1260 |
\isamarkuptrue%
|
|
1261 |
%
|
|
1262 |
\begin{isamarkuptext}%
|
|
1263 |
Imagine the following quick-and-dirty setup for implementing
|
|
1264 |
some sets as lists:%
|
|
1265 |
\end{isamarkuptext}%
|
|
1266 |
\isamarkuptrue%
|
|
1267 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
|
|
1268 |
\ set\isanewline
|
|
1269 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharunderscore}\ list{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
1270 |
\isanewline
|
|
1271 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
|
|
1272 |
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}\ \isakeyword{and}\ insert\isanewline
|
|
1273 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\ \isakeyword{and}\ \isakeyword{infixl}\ {\isadigit{7}}\ {\isachardoublequoteopen}{\isacharcolon}{\isacharcolon}{\isachardoublequoteclose}{\isacharparenright}\isanewline
|
|
1274 |
\isanewline
|
|
1275 |
\isacommand{definition}\isamarkupfalse%
|
|
1276 |
\isanewline
|
|
1277 |
\ \ dummy{\isacharunderscore}set\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ set{\isachardoublequoteclose}\isanewline
|
|
1278 |
\ \ {\isachardoublequoteopen}dummy{\isacharunderscore}set\ {\isacharequal}\ {\isacharbraceleft}{\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbraceright}{\isachardoublequoteclose}%
|
21172
|
1279 |
\isamarkupsubsection{Axiomatic extensions%
|
20967
|
1280 |
}
|
|
1281 |
\isamarkuptrue%
|
|
1282 |
%
|
21172
|
1283 |
\begin{isamarkuptext}%
|
|
1284 |
\begin{warn}
|
|
1285 |
The extensions introduced in this section, though working
|
|
1286 |
in practice, are not the cream of the crop. They will
|
|
1287 |
eventually be replaced by more mature approaches.
|
|
1288 |
\end{warn}%
|
|
1289 |
\end{isamarkuptext}%
|
|
1290 |
\isamarkuptrue%
|
|
1291 |
%
|
|
1292 |
\isamarkupsection{ML interfaces \label{sec:ml}%
|
|
1293 |
}
|
|
1294 |
\isamarkuptrue%
|
|
1295 |
%
|
|
1296 |
\isamarkupsubsection{Constants with type discipline: codegen\_consts.ML%
|
|
1297 |
}
|
|
1298 |
\isamarkuptrue%
|
|
1299 |
%
|
|
1300 |
\isadelimmlref
|
|
1301 |
%
|
|
1302 |
\endisadelimmlref
|
|
1303 |
%
|
|
1304 |
\isatagmlref
|
|
1305 |
%
|
|
1306 |
\begin{isamarkuptext}%
|
|
1307 |
\begin{mldecls}
|
|
1308 |
\indexmltype{CodegenConsts.const}\verb|type CodegenConsts.const| \\
|
|
1309 |
\indexml{CodegenConsts.inst-of-typ}\verb|CodegenConsts.inst_of_typ: theory -> string * typ -> CodegenConsts.const| \\
|
|
1310 |
\indexml{CodegenConsts.typ-of-inst}\verb|CodegenConsts.typ_of_inst: theory -> CodegenConsts.const -> string * typ| \\
|
|
1311 |
\indexml{CodegenConsts.norm}\verb|CodegenConsts.norm: theory -> CodegenConsts.const -> CodegenConsts.const| \\
|
|
1312 |
\indexml{CodegenConsts.norm-of-typ}\verb|CodegenConsts.norm_of_typ: theory -> string * typ -> CodegenConsts.const|
|
|
1313 |
\end{mldecls}%
|
|
1314 |
\end{isamarkuptext}%
|
|
1315 |
\isamarkuptrue%
|
|
1316 |
%
|
|
1317 |
\endisatagmlref
|
|
1318 |
{\isafoldmlref}%
|
|
1319 |
%
|
|
1320 |
\isadelimmlref
|
|
1321 |
%
|
|
1322 |
\endisadelimmlref
|
|
1323 |
%
|
|
1324 |
\isamarkupsubsection{Executable theory content: codegen\_data.ML%
|
|
1325 |
}
|
|
1326 |
\isamarkuptrue%
|
|
1327 |
%
|
|
1328 |
\begin{isamarkuptext}%
|
|
1329 |
This Pure module implements the core notions of
|
|
1330 |
executable content of a theory.%
|
|
1331 |
\end{isamarkuptext}%
|
|
1332 |
\isamarkuptrue%
|
|
1333 |
%
|
|
1334 |
\isamarkupsubsubsection{Suspended theorems%
|
|
1335 |
}
|
|
1336 |
\isamarkuptrue%
|
|
1337 |
%
|
|
1338 |
\isadelimmlref
|
|
1339 |
%
|
|
1340 |
\endisadelimmlref
|
|
1341 |
%
|
|
1342 |
\isatagmlref
|
|
1343 |
%
|
|
1344 |
\begin{isamarkuptext}%
|
|
1345 |
\begin{mldecls}
|
|
1346 |
\indexmltype{CodegenData.lthms}\verb|type CodegenData.lthms| \\
|
|
1347 |
\indexml{CodegenData.lazy}\verb|CodegenData.lazy: (unit -> thm list) -> CodegenData.lthms|
|
|
1348 |
\end{mldecls}%
|
|
1349 |
\end{isamarkuptext}%
|
|
1350 |
\isamarkuptrue%
|
|
1351 |
%
|
|
1352 |
\endisatagmlref
|
|
1353 |
{\isafoldmlref}%
|
|
1354 |
%
|
|
1355 |
\isadelimmlref
|
|
1356 |
%
|
|
1357 |
\endisadelimmlref
|
|
1358 |
%
|
|
1359 |
\isamarkupsubsubsection{Executable content%
|
20967
|
1360 |
}
|
|
1361 |
\isamarkuptrue%
|
|
1362 |
%
|
21172
|
1363 |
\isadelimmlref
|
|
1364 |
%
|
|
1365 |
\endisadelimmlref
|
|
1366 |
%
|
|
1367 |
\isatagmlref
|
|
1368 |
%
|
|
1369 |
\begin{isamarkuptext}%
|
|
1370 |
\begin{mldecls}
|
|
1371 |
\indexml{CodegenData.add-func}\verb|CodegenData.add_func: thm -> theory -> theory| \\
|
|
1372 |
\indexml{CodegenData.del-func}\verb|CodegenData.del_func: thm -> theory -> theory| \\
|
|
1373 |
\indexml{CodegenData.add-funcl}\verb|CodegenData.add_funcl: CodegenConsts.const * CodegenData.lthms -> theory -> theory| \\
|
|
1374 |
\indexml{CodegenData.add-datatype}\verb|CodegenData.add_datatype: string * (((string * sort) list * (string * typ list) list) * CodegenData.lthms) -> theory -> theory| \\
|
|
1375 |
\indexml{CodegenData.del-datatype}\verb|CodegenData.del_datatype: string -> theory -> theory| \\
|
|
1376 |
\indexml{CodegenData.add-inline}\verb|CodegenData.add_inline: thm -> theory -> theory| \\
|
|
1377 |
\indexml{CodegenData.del-inline}\verb|CodegenData.del_inline: thm -> theory -> theory| \\
|
|
1378 |
\indexml{CodegenData.add-inline-proc}\verb|CodegenData.add_inline_proc: (theory -> cterm list -> thm list) -> theory -> theory| \\
|
|
1379 |
\indexml{CodegenData.add-preproc}\verb|CodegenData.add_preproc: (theory -> thm list -> thm list) -> theory -> theory| \\
|
|
1380 |
\indexml{CodegenData.these-funcs}\verb|CodegenData.these_funcs: theory -> CodegenConsts.const -> thm list| \\
|
|
1381 |
\indexml{CodegenData.get-datatype}\verb|CodegenData.get_datatype: theory -> string -> ((string * sort) list * (string * typ list) list) option| \\
|
|
1382 |
\indexml{CodegenData.get-datatype-of-constr}\verb|CodegenData.get_datatype_of_constr: theory -> CodegenConsts.const -> string option|
|
|
1383 |
\end{mldecls}
|
|
1384 |
|
|
1385 |
\begin{description}
|
|
1386 |
|
|
1387 |
\item \verb|CodegenData.add_func|~\isa{thm}
|
|
1388 |
|
|
1389 |
\end{description}%
|
|
1390 |
\end{isamarkuptext}%
|
|
1391 |
\isamarkuptrue%
|
|
1392 |
%
|
|
1393 |
\endisatagmlref
|
|
1394 |
{\isafoldmlref}%
|
|
1395 |
%
|
|
1396 |
\isadelimmlref
|
|
1397 |
%
|
|
1398 |
\endisadelimmlref
|
|
1399 |
%
|
|
1400 |
\isamarkupsubsection{Further auxiliary%
|
|
1401 |
}
|
|
1402 |
\isamarkuptrue%
|
|
1403 |
%
|
|
1404 |
\isadelimmlref
|
|
1405 |
%
|
|
1406 |
\endisadelimmlref
|
|
1407 |
%
|
|
1408 |
\isatagmlref
|
|
1409 |
%
|
|
1410 |
\begin{isamarkuptext}%
|
|
1411 |
\begin{mldecls}
|
|
1412 |
\indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\
|
|
1413 |
\indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\
|
|
1414 |
\indexml{CodegenConsts.consts-of}\verb|CodegenConsts.consts_of: theory -> term -> CodegenConsts.const list| \\
|
|
1415 |
\indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\
|
|
1416 |
\indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\
|
|
1417 |
\indexml{CodegenData.typ-func}\verb|CodegenData.typ_func: theory -> thm -> typ| \\
|
|
1418 |
\indexml{CodegenData.rewrite-func}\verb|CodegenData.rewrite_func: thm list -> thm -> thm| \\
|
|
1419 |
\end{mldecls}%
|
|
1420 |
\end{isamarkuptext}%
|
|
1421 |
\isamarkuptrue%
|
|
1422 |
%
|
|
1423 |
\endisatagmlref
|
|
1424 |
{\isafoldmlref}%
|
|
1425 |
%
|
|
1426 |
\isadelimmlref
|
|
1427 |
%
|
|
1428 |
\endisadelimmlref
|
|
1429 |
%
|
20967
|
1430 |
\isamarkupsubsection{Implementing code generator applications%
|
|
1431 |
}
|
|
1432 |
\isamarkuptrue%
|
|
1433 |
%
|
21172
|
1434 |
\begin{isamarkuptext}%
|
|
1435 |
\begin{warn}
|
|
1436 |
Some interfaces discussed here have not reached
|
|
1437 |
a final state yet.
|
|
1438 |
Changes likely to occur in future.
|
|
1439 |
\end{warn}%
|
|
1440 |
\end{isamarkuptext}%
|
|
1441 |
\isamarkuptrue%
|
|
1442 |
%
|
|
1443 |
\isamarkupsubsubsection{Data depending on the theory's executable content%
|
|
1444 |
}
|
|
1445 |
\isamarkuptrue%
|
|
1446 |
%
|
|
1447 |
\isamarkupsubsubsection{Datatype hooks%
|
|
1448 |
}
|
|
1449 |
\isamarkuptrue%
|
|
1450 |
%
|
|
1451 |
\begin{isamarkuptext}%
|
|
1452 |
\emph{Happy proving, happy hacking!}%
|
|
1453 |
\end{isamarkuptext}%
|
|
1454 |
\isamarkuptrue%
|
|
1455 |
%
|
20967
|
1456 |
\isadelimtheory
|
|
1457 |
%
|
|
1458 |
\endisadelimtheory
|
|
1459 |
%
|
|
1460 |
\isatagtheory
|
|
1461 |
\isacommand{end}\isamarkupfalse%
|
|
1462 |
%
|
|
1463 |
\endisatagtheory
|
|
1464 |
{\isafoldtheory}%
|
|
1465 |
%
|
|
1466 |
\isadelimtheory
|
|
1467 |
%
|
|
1468 |
\endisadelimtheory
|
|
1469 |
\isanewline
|
|
1470 |
\end{isabellebody}%
|
|
1471 |
%%% Local Variables:
|
|
1472 |
%%% mode: latex
|
|
1473 |
%%% TeX-master: "root"
|
|
1474 |
%%% End:
|