20948
|
1 |
|
|
2 |
(* $Id$ *)
|
|
3 |
|
21147
|
4 |
(*<*)
|
20948
|
5 |
theory Codegen
|
|
6 |
imports Main
|
21147
|
7 |
uses "../../../IsarImplementation/Thy/setup.ML"
|
20948
|
8 |
begin
|
|
9 |
|
21147
|
10 |
ML {*
|
|
11 |
CodegenSerializer.sml_code_width := 74;
|
|
12 |
*}
|
|
13 |
|
|
14 |
(*>*)
|
|
15 |
|
20948
|
16 |
chapter {* Code generation from Isabelle theories *}
|
|
17 |
|
|
18 |
section {* Introduction *}
|
|
19 |
|
21058
|
20 |
subsection {* Motivation *}
|
|
21 |
|
20948
|
22 |
text {*
|
21058
|
23 |
Executing formal specifications as programs is a well-established
|
|
24 |
topic in the theorem proving community. With increasing
|
|
25 |
application of theorem proving systems in the area of
|
|
26 |
software development and verification, its relevance manifests
|
|
27 |
for running test cases and rapid prototyping. In logical
|
|
28 |
calculi like constructive type theory,
|
|
29 |
a notion of executability is implicit due to the nature
|
|
30 |
of the calculus. In contrast, specifications in Isabelle/HOL
|
|
31 |
can be highly non-executable. In order to bridge
|
|
32 |
the gap between logic and executable specifications,
|
|
33 |
an explicit non-trivial transformation has to be applied:
|
|
34 |
code generation.
|
|
35 |
|
|
36 |
This tutorial introduces a generic code generator for the
|
|
37 |
Isabelle system \cite{isa-tutorial}.
|
|
38 |
Generic in the sense that the
|
|
39 |
\qn{target language} for which code shall ultimately be
|
|
40 |
generated is not fixed but may be an arbitrary state-of-the-art
|
|
41 |
functional programming language (currently, the implementation
|
|
42 |
supports SML \cite{web:sml} and Haskell \cite{web:haskell}).
|
|
43 |
We aim to provide a
|
|
44 |
versatile environment
|
|
45 |
suitable for software development and verification,
|
|
46 |
structuring the process
|
|
47 |
of code generation into a small set of orthogonal principles
|
|
48 |
while achieving a big coverage of application areas
|
|
49 |
with maximum flexibility.
|
|
50 |
*}
|
|
51 |
|
|
52 |
|
|
53 |
subsection {* Overview *}
|
|
54 |
|
|
55 |
text {*
|
|
56 |
The code generator aims to be usable with no further ado
|
|
57 |
in most cases while allowing for detailed customization.
|
|
58 |
This manifests in the structure of this tutorial: this introduction
|
21147
|
59 |
continues with a short introduction of concepts. Section
|
21178
|
60 |
\secref{sec:basics} explains how to use the framework naively,
|
21058
|
61 |
presuming a reasonable default setup. Then, section
|
|
62 |
\secref{sec:advanced} deals with advanced topics,
|
|
63 |
introducing further aspects of the code generator framework
|
|
64 |
in a motivation-driven manner. Last, section \secref{sec:ml}
|
|
65 |
introduces the framework's internal programming interfaces.
|
20948
|
66 |
|
21058
|
67 |
\begin{warn}
|
|
68 |
Ultimately, the code generator which this tutorial deals with
|
|
69 |
is supposed to replace the already established code generator
|
|
70 |
by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
|
21147
|
71 |
So, for the moment, there are two distinct code generators
|
21058
|
72 |
in Isabelle.
|
|
73 |
Also note that while the framework itself is largely
|
|
74 |
object-logic independent, only HOL provides a reasonable
|
|
75 |
framework setup.
|
|
76 |
\end{warn}
|
|
77 |
*}
|
|
78 |
|
|
79 |
|
|
80 |
subsection {* Code generation process *}
|
|
81 |
|
|
82 |
text {*
|
|
83 |
The code generator employs a notion of executability
|
|
84 |
for three foundational executable ingredients known
|
|
85 |
from functional programming:
|
|
86 |
\emph{function equations}, \emph{datatypes}, and
|
|
87 |
\emph{type classes}. A function equation as a first approximation
|
|
88 |
is a theorem of the form @{text "f t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n \<equiv> t"}
|
|
89 |
(an equation headed by a constant @{text f} with arguments
|
|
90 |
@{text "t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n"} and right hand side @{text t}.
|
|
91 |
Code generation aims to turn function equations
|
|
92 |
into a functional program by running through
|
|
93 |
a process (see figure \ref{fig:process}):
|
|
94 |
|
|
95 |
\begin{itemize}
|
|
96 |
|
|
97 |
\item Out of the vast collection of theorems proven in a
|
|
98 |
\qn{theory}, a reasonable subset modeling
|
|
99 |
function equations is \qn{selected}.
|
|
100 |
|
|
101 |
\item On those selected theorems, certain
|
|
102 |
transformations are carried out
|
|
103 |
(\qn{preprocessing}). Their purpose is to turn theorems
|
|
104 |
representing non- or badly executable
|
|
105 |
specifications into equivalent but executable counterparts.
|
|
106 |
The result is a structured collection of \qn{code theorems}.
|
|
107 |
|
|
108 |
\item These \qn{code theorems} then are extracted
|
|
109 |
into an Haskell-like intermediate
|
|
110 |
language.
|
|
111 |
|
|
112 |
\item Finally, out of the intermediate language the final
|
|
113 |
code in the desired \qn{target language} is \qn{serialized}.
|
|
114 |
|
|
115 |
\end{itemize}
|
|
116 |
|
|
117 |
\begin{figure}[h]
|
|
118 |
\centering
|
|
119 |
\includegraphics[width=0.3\textwidth]{codegen_process}
|
|
120 |
\caption{code generator -- processing overview}
|
|
121 |
\label{fig:process}
|
|
122 |
\end{figure}
|
|
123 |
|
|
124 |
From these steps, only the two last are carried out
|
|
125 |
outside the logic; by keeping this layer as
|
|
126 |
thin as possible, the amount of code to trust is
|
|
127 |
kept to a minimum.
|
|
128 |
*}
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
section {* Basics \label{sec:basics} *}
|
20948
|
133 |
|
|
134 |
subsection {* Invoking the code generator *}
|
|
135 |
|
21058
|
136 |
text {*
|
|
137 |
Thanks to a reasonable setup of the HOL theories, in
|
|
138 |
most cases code generation proceeds without further ado:
|
|
139 |
*}
|
|
140 |
|
|
141 |
consts
|
|
142 |
fac :: "nat \<Rightarrow> nat"
|
|
143 |
|
|
144 |
primrec
|
|
145 |
"fac 0 = 1"
|
|
146 |
"fac (Suc n) = Suc n * fac n"
|
|
147 |
|
|
148 |
text {*
|
|
149 |
This executable specification is now turned to SML code:
|
|
150 |
*}
|
|
151 |
|
|
152 |
code_gen fac (SML "examples/fac.ML")
|
|
153 |
|
|
154 |
text {*
|
21178
|
155 |
The \isasymCODEGEN command takes a space-separated list of
|
21058
|
156 |
constants together with \qn{serialization directives}
|
|
157 |
in parentheses. These start with a \qn{target language}
|
21178
|
158 |
identifier, followed by arguments, their semantics
|
21058
|
159 |
depending on the target. In the SML case, a filename
|
|
160 |
is given where to write the generated code to.
|
|
161 |
|
|
162 |
Internally, the function equations for all selected
|
21178
|
163 |
constants are taken, including any transitively required
|
21058
|
164 |
constants, datatypes and classes, resulting in the following
|
|
165 |
code:
|
|
166 |
|
|
167 |
\lstsml{Thy/examples/fac.ML}
|
|
168 |
|
|
169 |
The code generator will complain when a required
|
|
170 |
ingredient does not provide a executable counterpart.
|
|
171 |
This is the case if an involved type is not a datatype:
|
|
172 |
*}
|
|
173 |
|
|
174 |
(*<*)
|
|
175 |
setup {* Sign.add_path "foo" *}
|
|
176 |
(*>*)
|
|
177 |
|
|
178 |
typedecl 'a foo
|
|
179 |
|
|
180 |
definition
|
|
181 |
bar :: "'a foo \<Rightarrow> 'a \<Rightarrow> 'a"
|
|
182 |
"bar x y = y"
|
|
183 |
|
|
184 |
(*<*)
|
|
185 |
hide type foo
|
|
186 |
hide const bar
|
|
187 |
|
|
188 |
setup {* Sign.parent_path *}
|
|
189 |
|
|
190 |
datatype 'a foo = Foo
|
|
191 |
|
|
192 |
definition
|
|
193 |
bar :: "'a foo \<Rightarrow> 'a \<Rightarrow> 'a"
|
|
194 |
"bar x y = y"
|
|
195 |
(*>*)
|
|
196 |
|
|
197 |
code_gen bar (SML "examples/fail_type.ML")
|
|
198 |
|
|
199 |
text {*
|
|
200 |
\noindent will result in an error. Likewise, generating code
|
|
201 |
for constants not yielding
|
|
202 |
a function equation will fail, e.g.~the Hilbert choice
|
|
203 |
operation @{text "SOME"}:
|
|
204 |
*}
|
|
205 |
|
|
206 |
(*<*)
|
|
207 |
setup {* Sign.add_path "foo" *}
|
|
208 |
(*>*)
|
|
209 |
|
|
210 |
definition
|
|
211 |
pick_some :: "'a list \<Rightarrow> 'a"
|
|
212 |
"pick_some xs = (SOME x. x \<in> set xs)"
|
|
213 |
|
|
214 |
(*<*)
|
|
215 |
hide const pick_some
|
|
216 |
|
|
217 |
setup {* Sign.parent_path *}
|
|
218 |
|
|
219 |
definition
|
|
220 |
pick_some :: "'a list \<Rightarrow> 'a"
|
|
221 |
"pick_some = hd"
|
|
222 |
(*>*)
|
|
223 |
|
|
224 |
code_gen pick_some (SML "examples/fail_const.ML")
|
|
225 |
|
20948
|
226 |
subsection {* Theorem selection *}
|
|
227 |
|
21058
|
228 |
text {*
|
|
229 |
The list of all function equations in a theory may be inspected
|
|
230 |
using the \isasymPRINTCODETHMS command:
|
|
231 |
*}
|
|
232 |
|
|
233 |
print_codethms
|
|
234 |
|
|
235 |
text {*
|
|
236 |
\noindent which displays a table of constant with corresponding
|
|
237 |
function equations (the additional stuff displayed
|
|
238 |
shall not bother us for the moment). If this table does
|
|
239 |
not provide at least one function
|
21178
|
240 |
equation, the table of primitive definitions is searched
|
21058
|
241 |
whether it provides one.
|
|
242 |
|
|
243 |
The typical HOL tools are already set up in a way that
|
|
244 |
function definitions introduced by \isasymFUN, \isasymFUNCTION,
|
|
245 |
\isasymPRIMREC, \isasymRECDEF are implicitly propagated
|
|
246 |
to this function equation table. Specific theorems may be
|
|
247 |
selected using an attribute: \emph{code func}. As example,
|
|
248 |
a weight selector function:
|
|
249 |
*}
|
|
250 |
|
|
251 |
consts
|
|
252 |
pick :: "(nat \<times> 'a) list \<Rightarrow> nat \<Rightarrow> 'a"
|
|
253 |
|
|
254 |
primrec
|
|
255 |
"pick (x#xs) n = (let (k, v) = x in
|
|
256 |
if n < k then v else pick xs (n - k))"
|
|
257 |
|
|
258 |
text {*
|
|
259 |
We want to eliminate the explicit destruction
|
|
260 |
of @{term x} to @{term "(k, v)"}:
|
|
261 |
*}
|
|
262 |
|
|
263 |
lemma [code func]:
|
|
264 |
"pick ((k, v)#xs) n = (if n < k then v else pick xs (n - k))"
|
|
265 |
by simp
|
|
266 |
|
|
267 |
code_gen pick (SML "examples/pick1.ML")
|
|
268 |
|
|
269 |
text {*
|
|
270 |
This theorem is now added to the function equation table:
|
|
271 |
|
|
272 |
\lstsml{Thy/examples/pick1.ML}
|
|
273 |
|
|
274 |
It might be convenient to remove the pointless original
|
|
275 |
equation, using the \emph{nofunc} attribute:
|
|
276 |
*}
|
|
277 |
|
|
278 |
lemmas [code nofunc] = pick.simps
|
|
279 |
|
|
280 |
code_gen pick (SML "examples/pick2.ML")
|
|
281 |
|
|
282 |
text {*
|
|
283 |
\lstsml{Thy/examples/pick2.ML}
|
|
284 |
|
|
285 |
Syntactic redundancies are implicitly dropped. For example,
|
|
286 |
using a modified version of the @{const fac} function
|
|
287 |
as function equation, the then redundant (since
|
21075
|
288 |
syntactically subsumed) original function equations
|
21058
|
289 |
are dropped, resulting in a warning:
|
|
290 |
*}
|
|
291 |
|
|
292 |
lemma [code func]:
|
|
293 |
"fac n = (case n of 0 \<Rightarrow> 1 | Suc m \<Rightarrow> n * fac m)"
|
|
294 |
by (cases n) simp_all
|
|
295 |
|
|
296 |
code_gen fac (SML "examples/fac_case.ML")
|
|
297 |
|
|
298 |
text {*
|
|
299 |
\lstsml{Thy/examples/fac_case.ML}
|
|
300 |
|
|
301 |
\begin{warn}
|
|
302 |
Some statements in this section have to be treated with some
|
|
303 |
caution. First, since the HOL function package is still
|
|
304 |
under development, its setup with respect to code generation
|
|
305 |
may differ from what is presumed here.
|
|
306 |
Further, the attributes \emph{code} and \emph{code del}
|
|
307 |
associated with the existing code generator also apply to
|
|
308 |
the new one: \emph{code} implies \emph{code func},
|
|
309 |
and \emph{code del} implies \emph{code nofunc}.
|
|
310 |
\end{warn}
|
|
311 |
*}
|
20948
|
312 |
|
|
313 |
subsection {* Type classes *}
|
|
314 |
|
21058
|
315 |
text {*
|
|
316 |
Type classes enter the game via the Isar class package.
|
21075
|
317 |
For a short introduction how to use it, see \cite{isabelle-classes};
|
21147
|
318 |
here we just illustrate its impact on code generation.
|
21058
|
319 |
|
|
320 |
In a target language, type classes may be represented
|
21178
|
321 |
natively (as in the case of Haskell). For languages
|
21058
|
322 |
like SML, they are implemented using \emph{dictionaries}.
|
21178
|
323 |
Our following example specifies a class \qt{null},
|
21058
|
324 |
assigning to each of its inhabitants a \qt{null} value:
|
|
325 |
*}
|
|
326 |
|
|
327 |
class null =
|
|
328 |
fixes null :: 'a
|
|
329 |
|
|
330 |
consts
|
|
331 |
head :: "'a\<Colon>null list \<Rightarrow> 'a"
|
|
332 |
|
|
333 |
primrec
|
|
334 |
"head [] = null"
|
|
335 |
"head (x#xs) = x"
|
|
336 |
|
|
337 |
text {*
|
|
338 |
We provide some instances for our @{text null}:
|
|
339 |
*}
|
|
340 |
|
|
341 |
instance option :: (type) null
|
|
342 |
"null \<equiv> None" ..
|
|
343 |
|
|
344 |
instance list :: (type) null
|
|
345 |
"null \<equiv> []" ..
|
|
346 |
|
|
347 |
text {*
|
|
348 |
Constructing a dummy example:
|
|
349 |
*}
|
|
350 |
|
|
351 |
definition
|
|
352 |
"dummy = head [Some (Suc 0), None]"
|
|
353 |
|
|
354 |
text {*
|
21178
|
355 |
Type classes offer a suitable occasion to introduce
|
21058
|
356 |
the Haskell serializer. Its usage is almost the same
|
21075
|
357 |
as SML, but, in accordance with conventions
|
|
358 |
some Haskell systems enforce, each module ends
|
|
359 |
up in a single file. The module hierarchy is reflected in
|
|
360 |
the file system, with root given by the user.
|
21058
|
361 |
*}
|
|
362 |
|
21075
|
363 |
code_gen dummy (Haskell "examples/")
|
21147
|
364 |
(* NOTE: you may use Haskell only once in this document, otherwise
|
|
365 |
you have to work in distinct subdirectories *)
|
21058
|
366 |
|
|
367 |
text {*
|
|
368 |
\lsthaskell{Thy/examples/Codegen.hs}
|
|
369 |
|
|
370 |
(we have left out all other modules).
|
|
371 |
|
|
372 |
The whole code in SML with explicit dictionary passing:
|
|
373 |
*}
|
|
374 |
|
|
375 |
code_gen dummy (SML "examples/class.ML")
|
|
376 |
|
|
377 |
text {*
|
|
378 |
\lstsml{Thy/examples/class.ML}
|
|
379 |
*}
|
|
380 |
|
|
381 |
subsection {* Incremental code generation *}
|
|
382 |
|
21075
|
383 |
text {*
|
|
384 |
Code generation is \emph{incremental}: theorems
|
|
385 |
and abstract intermediate code are cached and extended on demand.
|
|
386 |
The cache may be partially or fully dropped if the underlying
|
|
387 |
executable content of the theory changes.
|
|
388 |
Implementation of caching is supposed to transparently
|
|
389 |
hid away the details from the user. Anyway, caching
|
|
390 |
reaches the surface by using a slightly more general form
|
|
391 |
of the \isasymCODEGEN: either the list of constants or the
|
|
392 |
list of serialization expressions may be dropped. If no
|
|
393 |
serialization expressions are given, only abstract code
|
|
394 |
is generated and cached; if no constants are given, the
|
|
395 |
current cache is serialized.
|
|
396 |
|
|
397 |
For explorative reasons, an extended version of the
|
|
398 |
\isasymCODEGEN command may prove useful:
|
|
399 |
*}
|
|
400 |
|
|
401 |
print_codethms ()
|
|
402 |
|
|
403 |
text {*
|
|
404 |
\noindent print all cached function equations (i.e.~\emph{after}
|
|
405 |
any applied transformation. Inside the brackets a
|
|
406 |
list of constants may be given; their function
|
21178
|
407 |
equations are added to the cache if not already present.
|
21075
|
408 |
*}
|
|
409 |
|
21058
|
410 |
|
|
411 |
section {* Recipes and advanced topics \label{sec:advanced} *}
|
|
412 |
|
21089
|
413 |
text {*
|
|
414 |
In this tutorial, we do not attempt to give an exhaustive
|
|
415 |
description of the code generator framework; instead,
|
|
416 |
we cast a light on advanced topics by introducing
|
|
417 |
them together with practically motivated examples. Concerning
|
|
418 |
further reading, see
|
|
419 |
|
|
420 |
\begin{itemize}
|
|
421 |
|
|
422 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
|
|
423 |
for exhaustive syntax diagrams.
|
|
424 |
\item or \fixme{ref} which deals with foundational issues
|
|
425 |
of the code generator framework.
|
|
426 |
|
|
427 |
\end{itemize}
|
|
428 |
*}
|
21058
|
429 |
|
|
430 |
subsection {* Library theories *}
|
|
431 |
|
21089
|
432 |
text {*
|
|
433 |
The HOL \emph{Main} theory already provides a code generator setup
|
|
434 |
which should be suitable for most applications. Common extensions
|
|
435 |
and modifications are available by certain theories of the HOL
|
|
436 |
library; beside being useful in applications, they may serve
|
21178
|
437 |
as a tutorial for customizing the code generator setup.
|
21089
|
438 |
|
|
439 |
\begin{description}
|
|
440 |
|
|
441 |
\item[ExecutableSet] allows to generate code
|
|
442 |
for finite sets using lists.
|
|
443 |
\item[ExecutableRat] implements rational
|
|
444 |
numbers as triples @{text "(sign, enumerator, denominator)"}.
|
|
445 |
\item[EfficientNat] implements natural numbers by integers,
|
21178
|
446 |
which in general will result in higher efficency; pattern
|
21089
|
447 |
matching with @{const "0\<Colon>nat"} / @{const "Suc"}
|
21147
|
448 |
is eliminated. \label{eff_nat}
|
21089
|
449 |
\item[MLString] provides an additional datatype @{text "mlstring"};
|
|
450 |
in the HOL default setup, strings in HOL are mapped to list
|
|
451 |
of chars in SML; values of type @{text "mlstring"} are
|
|
452 |
mapped to strings in SML.
|
|
453 |
|
|
454 |
\end{description}
|
|
455 |
*}
|
|
456 |
|
20948
|
457 |
subsection {* Preprocessing *}
|
|
458 |
|
21089
|
459 |
text {*
|
21147
|
460 |
Before selected function theorems are turned into abstract
|
|
461 |
code, a chain of definitional transformation steps is carried
|
21178
|
462 |
out: \emph{preprocessing}. There are three possibilities
|
21147
|
463 |
to customize preprocessing: \emph{inline theorems},
|
|
464 |
\emph{inline procedures} and \emph{generic preprocessors}.
|
|
465 |
|
|
466 |
\emph{Inline theorems} are rewriting rules applied to each
|
|
467 |
function equation. Due to the interpretation of theorems
|
|
468 |
of function equations, rewrites are applied to the right
|
|
469 |
hand side and the arguments of the left hand side of an
|
|
470 |
equation, but never to the constant heading the left hand side.
|
|
471 |
Inline theorems may be declared an undeclared using the
|
21178
|
472 |
\emph{code inline} or \emph{code noinline} attribute respectively.
|
21147
|
473 |
|
|
474 |
Some common applications:
|
|
475 |
*}
|
|
476 |
|
|
477 |
text_raw {*
|
|
478 |
\begin{itemize}
|
|
479 |
\item replacing non-executable constructs by executable ones: \\
|
|
480 |
*}
|
|
481 |
|
|
482 |
lemma [code inline]:
|
|
483 |
"x \<in> set xs \<longleftrightarrow> x mem xs" by (induct xs) simp_all
|
|
484 |
|
|
485 |
text_raw {*
|
|
486 |
\item eliminating superfluous constants: \\
|
|
487 |
*}
|
|
488 |
|
|
489 |
lemma [code inline]:
|
|
490 |
"1 = Suc 0" by simp
|
|
491 |
|
|
492 |
text_raw {*
|
|
493 |
\item replacing executable but inconvenient constructs: \\
|
21089
|
494 |
*}
|
|
495 |
|
21147
|
496 |
lemma [code inline]:
|
|
497 |
"xs = [] \<longleftrightarrow> List.null xs" by (induct xs) simp_all
|
|
498 |
|
|
499 |
text_raw {*
|
|
500 |
\end{itemize}
|
|
501 |
*}
|
|
502 |
|
|
503 |
text {*
|
|
504 |
The current set of inline theorems may be inspected using
|
|
505 |
the \isasymPRINTCODETHMS command.
|
|
506 |
|
|
507 |
\emph{Inline procedures} are a generalized version of inline
|
|
508 |
theorems written in ML -- rewrite rules are generated dependent
|
|
509 |
on the function theorems for a certain function. One
|
|
510 |
application is the implicit expanding of @{typ nat} numerals
|
|
511 |
to @{const "0\<Colon>nat"} / @{const Suc} representation. See further
|
|
512 |
\secref{sec:ml}
|
|
513 |
|
|
514 |
\emph{Generic preprocessors} provide a most general interface,
|
|
515 |
transforming a list of function theorems to another
|
|
516 |
list of function theorems, provided that neither the heading
|
|
517 |
constant nor its type change. The @{const "0\<Colon>nat"} / @{const Suc}
|
|
518 |
pattern elimination implemented in \secref{eff_nat} uses this
|
|
519 |
interface.
|
|
520 |
|
|
521 |
\begin{warn}
|
|
522 |
The order in which single preprocessing steps are carried
|
|
523 |
out currently is not specified; in particular, preprocessing
|
21178
|
524 |
is \emph{no} fix point process. Keep this in mind when
|
21147
|
525 |
setting up the preprocessor.
|
|
526 |
|
|
527 |
Further, the attribute \emph{code unfold}
|
|
528 |
associated with the existing code generator also applies to
|
|
529 |
the new one: \emph{code unfold} implies \emph{code inline}.
|
|
530 |
\end{warn}
|
|
531 |
*}
|
20948
|
532 |
|
21058
|
533 |
subsection {* Customizing serialization *}
|
20948
|
534 |
|
21147
|
535 |
text {*
|
|
536 |
Consider the following function and its corresponding
|
|
537 |
SML code:
|
|
538 |
*}
|
|
539 |
|
|
540 |
fun
|
|
541 |
in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
|
|
542 |
"in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
|
|
543 |
termination by (auto_term "{}")
|
|
544 |
(*<*)
|
|
545 |
declare in_interval.simps [code func]
|
|
546 |
(*>*)
|
|
547 |
|
|
548 |
(*<*)
|
|
549 |
code_type bool
|
|
550 |
(SML)
|
|
551 |
code_const True and False and "op \<and>" and Not
|
|
552 |
(SML and and and)
|
|
553 |
(*>*)
|
|
554 |
|
|
555 |
code_gen in_interval (SML "examples/bool1.ML")
|
|
556 |
|
|
557 |
text {*
|
|
558 |
\lstsml{Thy/examples/bool1.ML}
|
|
559 |
|
|
560 |
Though this is correct code, it is a little bit unsatisfactory:
|
|
561 |
boolean values and operators are materialized as distinguished
|
|
562 |
entities with have nothing to do with the SML-builtin notion
|
|
563 |
of \qt{bool}. This results in less readable code;
|
|
564 |
additionally, eager evaluation may cause programs to
|
|
565 |
loop or break which would perfectly terminate when
|
|
566 |
the existing SML \qt{bool} would be used. To map
|
|
567 |
the HOL \qt{bool} on SML \qt{bool}, we may use
|
|
568 |
\qn{custom serializations}:
|
|
569 |
*}
|
|
570 |
|
|
571 |
code_type bool
|
|
572 |
(SML "bool")
|
|
573 |
code_const True and False and "op \<and>"
|
|
574 |
(SML "true" and "false" and "_ andalso _")
|
|
575 |
|
|
576 |
text {*
|
|
577 |
The \isasymCODETYPE commad takes a type constructor
|
|
578 |
as arguments together with a list of custom serializations.
|
|
579 |
Each custom serialization starts with a target language
|
|
580 |
identifier followed by an expression, which during
|
|
581 |
code serialization is inserted whenever the type constructor
|
|
582 |
would occur. For constants, \isasymCODECONST implements
|
|
583 |
the corresponding mechanism. Each \qt{\_} in
|
|
584 |
a serialization expression is treated as a placeholder
|
|
585 |
for the type constructor's (the constant's) arguments.
|
|
586 |
*}
|
|
587 |
|
|
588 |
code_reserved SML
|
|
589 |
bool true false
|
|
590 |
|
|
591 |
text {*
|
|
592 |
To assert that the existing \qt{bool}, \qt{true} and \qt{false}
|
|
593 |
is not used for generated code, we use \isasymCODERESERVED.
|
|
594 |
|
|
595 |
After this setup, code looks quite more readable:
|
|
596 |
*}
|
|
597 |
|
|
598 |
code_gen in_interval (SML "examples/bool2.ML")
|
|
599 |
|
|
600 |
text {*
|
|
601 |
\lstsml{Thy/examples/bool2.ML}
|
|
602 |
|
|
603 |
This still is not perfect: the parentheses
|
21178
|
604 |
around \qt{andalso} are superfluous. Though the serializer
|
21147
|
605 |
by no means attempts to imitate the rich Isabelle syntax
|
|
606 |
framework, it provides some common idioms, notably
|
|
607 |
associative infixes with precedences which may be used here:
|
|
608 |
*}
|
|
609 |
|
|
610 |
code_const "op \<and>"
|
|
611 |
(SML infixl 1 "andalso")
|
|
612 |
|
|
613 |
code_gen in_interval (SML "examples/bool3.ML")
|
|
614 |
|
|
615 |
text {*
|
|
616 |
\lstsml{Thy/examples/bool3.ML}
|
|
617 |
|
|
618 |
Next, we try to map HOL pairs to SML pairs, using the
|
|
619 |
infix \qt{ * } type constructor and parentheses:
|
|
620 |
*}
|
|
621 |
|
|
622 |
(*<*)
|
|
623 |
code_type *
|
|
624 |
(SML)
|
|
625 |
code_const Pair
|
|
626 |
(SML)
|
|
627 |
(*>*)
|
|
628 |
|
|
629 |
code_type *
|
|
630 |
(SML infix 2 "*")
|
|
631 |
|
|
632 |
code_const Pair
|
|
633 |
(SML "!((_),/ (_))")
|
|
634 |
|
|
635 |
text {*
|
|
636 |
The initial bang \qt{!} tells the serializer to never put
|
|
637 |
parentheses around the whole expression (they are already present),
|
|
638 |
while the parentheses around argument place holders
|
|
639 |
tell not to put parentheses around the arguments.
|
|
640 |
The slash \qt{/} (followed by arbitrary white space)
|
|
641 |
inserts a space which may be used as a break if necessary
|
|
642 |
during pretty printing.
|
|
643 |
|
21178
|
644 |
So far, we did only provide more idiomatic serializations for
|
|
645 |
constructs which would be executable on their own. Target-specific
|
|
646 |
serializations may also be used to \emph{implement} constructs
|
|
647 |
which have no implicit notion of executability. For example,
|
|
648 |
take the HOL integers:
|
|
649 |
*}
|
|
650 |
|
|
651 |
definition
|
|
652 |
double_inc :: "int \<Rightarrow> int"
|
|
653 |
"double_inc k = 2 * k + 1"
|
|
654 |
|
|
655 |
code_gen double_inc (SML "examples/integers.ML")
|
|
656 |
|
|
657 |
text {*
|
|
658 |
will fail: @{typ int} in HOL is implemented using a quotient
|
|
659 |
type, which does not provide any notion of executability.
|
|
660 |
\footnote{Eventually, we also want to provide executability
|
|
661 |
for quotients.}. However, we could use the SML builtin
|
|
662 |
integers:
|
21147
|
663 |
*}
|
|
664 |
|
|
665 |
code_type int
|
|
666 |
(SML "IntInf.int")
|
|
667 |
|
|
668 |
code_const "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int"
|
|
669 |
and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int"
|
21178
|
670 |
(SML "IntInf.+ (_, _)" and "IntInf.* (_, _)")
|
|
671 |
|
|
672 |
code_gen double_inc (SML "examples/integers.ML")
|
21147
|
673 |
|
21178
|
674 |
text {*
|
|
675 |
resulting in:
|
21147
|
676 |
|
21178
|
677 |
\lstsml{Thy/examples/integers.ML}
|
|
678 |
*}
|
21147
|
679 |
|
21178
|
680 |
text {*
|
|
681 |
These examples give a glimpse what powerful mechanisms
|
|
682 |
custom serializations provide; however their usage
|
|
683 |
requires careful thinking in order not to introduce
|
|
684 |
inconsistencies -- or, in other words:
|
|
685 |
custom serializations are completely axiomatic.
|
21147
|
686 |
|
21178
|
687 |
A further noteworthy details is that any special
|
|
688 |
character in a custom serialization may be quoted
|
|
689 |
using \qt{'}; thus, in \qt{fn '\_ => \_} the first
|
|
690 |
\qt{'\_} is a proper underscore while the
|
|
691 |
second \qt{\_} is a placeholder.
|
21147
|
692 |
|
21178
|
693 |
The HOL theories provide further
|
|
694 |
examples for custom serializations and form
|
|
695 |
a recommended tutorial on how to use them properly.
|
|
696 |
*}
|
21147
|
697 |
|
|
698 |
subsection {* Concerning operational equality *}
|
|
699 |
|
|
700 |
text {*
|
|
701 |
Surely you have already noticed how equality is treated
|
|
702 |
by the code generator:
|
|
703 |
*}
|
|
704 |
|
|
705 |
fun
|
|
706 |
collect_duplicates :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
|
|
707 |
"collect_duplicates xs ys [] = xs"
|
|
708 |
"collect_duplicates xs ys (z#zs) = (if z \<in> set xs
|
|
709 |
then if z \<in> set ys
|
|
710 |
then collect_duplicates xs ys zs
|
|
711 |
else collect_duplicates xs (z#ys) zs
|
|
712 |
else collect_duplicates (z#xs) (z#ys) zs)"
|
|
713 |
termination by (auto_term "measure (length o snd o snd)")
|
|
714 |
(*<*)
|
|
715 |
lemmas [code func] = collect_duplicates.simps
|
|
716 |
(*>*)
|
|
717 |
|
|
718 |
text {*
|
21178
|
719 |
The membership test during preprocessing is rewriting,
|
21147
|
720 |
resulting in @{const List.memberl}, which itself
|
|
721 |
performs an explicit equality check.
|
|
722 |
*}
|
|
723 |
|
|
724 |
code_gen collect_duplicates (SML "examples/collect_duplicates.ML")
|
|
725 |
|
|
726 |
text {*
|
|
727 |
\lstsml{Thy/examples/collect_duplicates.ML}
|
|
728 |
*}
|
|
729 |
|
|
730 |
text {*
|
|
731 |
Obviously, polymorphic equality is implemented the Haskell
|
|
732 |
way using a type class. How is this achieved? By an
|
|
733 |
almost trivial definition in the HOL setup:
|
|
734 |
*}
|
|
735 |
|
|
736 |
(*<*)
|
|
737 |
setup {* Sign.add_path "foo" *}
|
|
738 |
(*>*)
|
|
739 |
|
|
740 |
class eq =
|
|
741 |
fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
742 |
|
|
743 |
defs
|
21178
|
744 |
eq (*[symmetric, code inline, code func]*): "eq \<equiv> (op =)"
|
21147
|
745 |
|
|
746 |
text {*
|
|
747 |
This merely introduces a class @{text eq} with corresponding
|
|
748 |
operation @{const eq}, which by definition is isomorphic
|
|
749 |
to @{const "op ="}; the preprocessing framework does the rest.
|
|
750 |
*}
|
|
751 |
|
|
752 |
(*<*)
|
|
753 |
lemmas [code noinline] = eq
|
|
754 |
|
|
755 |
hide (open) "class" eq
|
|
756 |
hide (open) const eq
|
|
757 |
|
|
758 |
lemmas [symmetric, code func] = eq_def
|
|
759 |
|
|
760 |
setup {* Sign.parent_path *}
|
|
761 |
(*>*)
|
|
762 |
|
|
763 |
text {*
|
|
764 |
For datatypes, instances of @{text eq} are implicitly derived
|
|
765 |
when possible.
|
|
766 |
|
|
767 |
Though this class is designed to get rarely in the way, there
|
|
768 |
are some cases when it suddenly comes to surface:
|
|
769 |
*}
|
|
770 |
|
21178
|
771 |
subsubsection {* code lemmas and customary serializations for equality *}
|
|
772 |
|
|
773 |
text {*
|
|
774 |
Examine the following:
|
21147
|
775 |
*}
|
|
776 |
|
|
777 |
code_const "op = \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
|
|
778 |
(SML "!(_ : IntInf.int = _)")
|
|
779 |
|
21178
|
780 |
text {*
|
|
781 |
What is wrong here? Since @{const "op ="} is nothing else then
|
21147
|
782 |
a plain constant, this customary serialization will refer
|
|
783 |
to polymorphic equality @{const "op = \<Colon> 'a \<Rightarrow> 'a \<Rightarrow> bool"}.
|
|
784 |
Instead, we want the specific equality on @{typ int},
|
21178
|
785 |
by using the overloaded constant @{const "Code_Generator.eq"}:
|
21147
|
786 |
*}
|
|
787 |
|
|
788 |
code_const "Code_Generator.eq \<Colon> int \<Rightarrow> int \<Rightarrow> bool"
|
|
789 |
(SML "!(_ : IntInf.int = _)")
|
|
790 |
|
21178
|
791 |
subsubsection {* typedecls interpretated by customary serializations *}
|
|
792 |
|
|
793 |
text {*
|
|
794 |
A common idiom is to use unspecified types for formalizations
|
|
795 |
and interpret them for a specific target language:
|
21147
|
796 |
*}
|
|
797 |
|
|
798 |
typedecl key
|
|
799 |
|
|
800 |
fun
|
|
801 |
lookup :: "(key \<times> 'a) list \<Rightarrow> key \<Rightarrow> 'a option" where
|
|
802 |
"lookup [] l = None"
|
|
803 |
"lookup ((k, v) # xs) l = (if k = l then Some v else lookup xs l)"
|
|
804 |
termination by (auto_term "measure (length o fst)")
|
|
805 |
(*<*)
|
|
806 |
lemmas [code func] = lookup.simps
|
|
807 |
(*>*)
|
|
808 |
|
|
809 |
code_type key
|
|
810 |
(SML "string")
|
|
811 |
|
21178
|
812 |
text {*
|
|
813 |
This, though, is not sufficient: @{typ key} is no instance
|
21147
|
814 |
of @{text eq} since @{typ key} is no datatype; the instance
|
|
815 |
has to be declared manually, including a serialization
|
21178
|
816 |
for the particular instance of @{const "Code_Generator.eq"}:
|
21147
|
817 |
*}
|
|
818 |
|
|
819 |
instance key :: eq ..
|
|
820 |
|
|
821 |
code_const "Code_Generator.eq \<Colon> key \<Rightarrow> key \<Rightarrow> bool"
|
|
822 |
(SML "!(_ : string = _)")
|
|
823 |
|
21178
|
824 |
text {*
|
|
825 |
Then everything goes fine:
|
21147
|
826 |
*}
|
|
827 |
|
|
828 |
code_gen lookup (SML "examples/lookup.ML")
|
|
829 |
|
|
830 |
text {*
|
|
831 |
\lstsml{Thy/examples/lookup.ML}
|
|
832 |
*}
|
|
833 |
|
21178
|
834 |
subsubsection {* lexicographic orderings and coregularity *}
|
|
835 |
|
|
836 |
text {*
|
|
837 |
Another subtlety
|
21147
|
838 |
enters the stage when definitions of overloaded constants
|
|
839 |
are dependent on operational equality. For example, let
|
|
840 |
us define a lexicographic ordering on tuples: \\
|
|
841 |
*}
|
|
842 |
|
|
843 |
(*<*)
|
21178
|
844 |
setup {* Sign.add_path "foobar" *}
|
21147
|
845 |
|
21178
|
846 |
class eq = fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
847 |
class ord =
|
|
848 |
fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" ("(_/ \<^loc>\<le> _)" [50, 51] 50)
|
|
849 |
fixes less :: "'a \<Rightarrow> 'a \<Rightarrow> bool" ("(_/ \<^loc>< _)" [50, 51] 50)
|
21147
|
850 |
(*>*)
|
|
851 |
|
21178
|
852 |
instance * :: (ord, ord) ord
|
|
853 |
"p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in
|
21147
|
854 |
x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
|
21178
|
855 |
"p1 \<le> p2 \<equiv> p1 < p2 \<or> (p1 \<Colon> 'a\<Colon>ord \<times> 'b\<Colon>ord) = p2" ..
|
21147
|
856 |
|
|
857 |
(*<*)
|
21178
|
858 |
hide "class" eq ord
|
|
859 |
hide const eq less_eq less
|
|
860 |
setup {* Sign.parent_path *}
|
21147
|
861 |
(*>*)
|
|
862 |
|
21178
|
863 |
text {*
|
|
864 |
Then code generation will fail. Why? The definition
|
21147
|
865 |
of @{const "op \<le>"} depends on equality on both arguments,
|
21178
|
866 |
which are polymorphic and impose an additional @{text eq}
|
21147
|
867 |
class constraint, thus violating the type discipline
|
|
868 |
for class operations.
|
|
869 |
|
21178
|
870 |
The solution is to add @{text eq} to both sort arguments:
|
21147
|
871 |
*}
|
|
872 |
|
|
873 |
instance * :: ("{eq, ord}", "{eq, ord}") ord
|
21178
|
874 |
"p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>{eq, ord}, y1 \<Colon> 'b\<Colon>{eq, ord}) = p1; (x2, y2) = p2 in
|
21147
|
875 |
x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)"
|
21178
|
876 |
"p1 \<le> p2 \<equiv> p1 < p2 \<or> (p1 \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>{eq, ord}) = p2" ..
|
21147
|
877 |
|
21178
|
878 |
text {*
|
|
879 |
Then code generation succeeds:
|
21147
|
880 |
*}
|
|
881 |
|
|
882 |
code_gen "op \<le> \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>{eq, ord} \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool"
|
|
883 |
(SML "examples/lexicographic.ML")
|
|
884 |
|
|
885 |
text {*
|
|
886 |
\lstsml{Thy/examples/lexicographic.ML}
|
|
887 |
*}
|
|
888 |
|
21178
|
889 |
subsubsection {* Haskell serialization *}
|
|
890 |
|
|
891 |
text {*
|
|
892 |
For convenience, the default
|
|
893 |
HOL setup for Haskell maps the @{text eq} class to
|
|
894 |
its counterpart in Haskell, giving custom serializations
|
|
895 |
for the class (\isasymCODECLASS) and its operation:
|
|
896 |
*}
|
|
897 |
|
|
898 |
(*<*)
|
|
899 |
setup {* Sign.add_path "bar" *}
|
|
900 |
class eq = fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
|
|
901 |
(*>*)
|
|
902 |
|
|
903 |
code_class eq
|
|
904 |
(Haskell "Eq" where eq \<equiv> "(==)")
|
|
905 |
|
|
906 |
code_const eq
|
|
907 |
(Haskell infixl 4 "==")
|
|
908 |
|
|
909 |
(*<*)
|
|
910 |
hide "class" eq
|
|
911 |
hide const eq
|
|
912 |
setup {* Sign.parent_path *}
|
|
913 |
(*>*)
|
|
914 |
|
|
915 |
text {*
|
|
916 |
A problem now occurs whenever a type which
|
|
917 |
is an instance of @{text eq} in HOL is mapped
|
|
918 |
on a Haskell-builtin type which is also an instance
|
|
919 |
of Haskell @{text Eq}:
|
21147
|
920 |
*}
|
|
921 |
|
21178
|
922 |
typedecl bar
|
|
923 |
|
|
924 |
instance bar :: eq ..
|
|
925 |
|
|
926 |
code_type bar
|
|
927 |
(Haskell "Integer")
|
|
928 |
|
|
929 |
text {*
|
|
930 |
The code generator would produce
|
|
931 |
an additional instance, which of course is rejected.
|
|
932 |
To suppress this additional instance, use
|
|
933 |
\isasymCODEINSTANCE:
|
21147
|
934 |
*}
|
|
935 |
|
21178
|
936 |
code_instance bar :: eq
|
|
937 |
(Haskell -)
|
|
938 |
|
|
939 |
subsection {* Types matter *}
|
|
940 |
|
|
941 |
text {*
|
|
942 |
Imagine the following quick-and-dirty setup for implementing
|
|
943 |
some sets as lists:
|
|
944 |
*}
|
|
945 |
|
|
946 |
code_type set
|
|
947 |
(SML "_ list")
|
|
948 |
|
|
949 |
code_const "{}" and insert
|
|
950 |
(SML "![]" and infixl 7 "::")
|
|
951 |
|
|
952 |
definition
|
|
953 |
dummy_set :: "nat set"
|
|
954 |
"dummy_set = {1, 2, 3}"
|
|
955 |
|
|
956 |
(*print_codethms (dummy_set)
|
|
957 |
code_gen dummy_set (SML -)*)
|
|
958 |
|
|
959 |
|
|
960 |
(* shadowing by user-defined serializations, understanding the type game,
|
|
961 |
reflexive equations, dangerous equations *)
|
|
962 |
|
21147
|
963 |
|
|
964 |
subsection {* Axiomatic extensions *}
|
|
965 |
|
|
966 |
text {*
|
|
967 |
\begin{warn}
|
|
968 |
The extensions introduced in this section, though working
|
|
969 |
in practice, are not the cream of the crop. They will
|
|
970 |
eventually be replaced by more mature approaches.
|
|
971 |
\end{warn}
|
|
972 |
*}
|
|
973 |
|
21178
|
974 |
(* code_modulename *)
|
21147
|
975 |
(* existing libraries, code inline code_constsubst, code_abstype*)
|
20948
|
976 |
|
21058
|
977 |
section {* ML interfaces \label{sec:ml} *}
|
20948
|
978 |
|
21147
|
979 |
subsection {* Constants with type discipline: codegen\_consts.ML *}
|
|
980 |
|
|
981 |
text %mlref {*
|
|
982 |
\begin{mldecls}
|
|
983 |
@{index_ML_type CodegenConsts.const} \\
|
|
984 |
@{index_ML CodegenConsts.inst_of_typ: "theory -> string * typ -> CodegenConsts.const"} \\
|
|
985 |
@{index_ML CodegenConsts.typ_of_inst: "theory -> CodegenConsts.const -> string * typ"} \\
|
|
986 |
@{index_ML CodegenConsts.norm: "theory -> CodegenConsts.const -> CodegenConsts.const"} \\
|
|
987 |
@{index_ML CodegenConsts.norm_of_typ: "theory -> string * typ -> CodegenConsts.const"}
|
|
988 |
\end{mldecls}
|
|
989 |
*}
|
|
990 |
|
|
991 |
subsection {* Executable theory content: codegen\_data.ML *}
|
|
992 |
|
|
993 |
text {*
|
|
994 |
This Pure module implements the core notions of
|
|
995 |
executable content of a theory.
|
|
996 |
*}
|
|
997 |
|
|
998 |
subsubsection {* Suspended theorems *}
|
|
999 |
|
|
1000 |
text %mlref {*
|
|
1001 |
\begin{mldecls}
|
|
1002 |
@{index_ML_type CodegenData.lthms} \\
|
|
1003 |
@{index_ML CodegenData.lazy: "(unit -> thm list) -> CodegenData.lthms"}
|
|
1004 |
\end{mldecls}
|
|
1005 |
*}
|
|
1006 |
|
|
1007 |
subsubsection {* Executable content *}
|
20948
|
1008 |
|
21147
|
1009 |
text %mlref {*
|
|
1010 |
\begin{mldecls}
|
|
1011 |
@{index_ML CodegenData.add_func: "thm -> theory -> theory"} \\
|
|
1012 |
@{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\
|
|
1013 |
@{index_ML CodegenData.add_funcl: "CodegenConsts.const * CodegenData.lthms -> theory -> theory"} \\
|
|
1014 |
@{index_ML CodegenData.add_datatype: "string * (((string * sort) list * (string * typ list) list) * CodegenData.lthms) -> theory -> theory"} \\
|
|
1015 |
@{index_ML CodegenData.del_datatype: "string -> theory -> theory"} \\
|
|
1016 |
@{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\
|
|
1017 |
@{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\
|
|
1018 |
@{index_ML CodegenData.add_inline_proc: "(theory -> cterm list -> thm list) -> theory -> theory"} \\
|
|
1019 |
@{index_ML CodegenData.add_preproc: "(theory -> thm list -> thm list) -> theory -> theory"} \\
|
|
1020 |
@{index_ML CodegenData.these_funcs: "theory -> CodegenConsts.const -> thm list"} \\
|
|
1021 |
@{index_ML CodegenData.get_datatype: "theory -> string -> ((string * sort) list * (string * typ list) list) option"} \\
|
|
1022 |
@{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"}
|
|
1023 |
\end{mldecls}
|
|
1024 |
|
|
1025 |
\begin{description}
|
|
1026 |
|
|
1027 |
\item @{ML CodegenData.add_func}~@{text "thm"}
|
|
1028 |
|
|
1029 |
\end{description}
|
|
1030 |
*}
|
|
1031 |
|
|
1032 |
subsection {* Further auxiliary *}
|
|
1033 |
|
|
1034 |
text %mlref {*
|
|
1035 |
\begin{mldecls}
|
|
1036 |
@{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\
|
|
1037 |
@{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\
|
|
1038 |
@{index_ML CodegenConsts.consts_of: "theory -> term -> CodegenConsts.const list"} \\
|
|
1039 |
@{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\
|
|
1040 |
@{index_ML_structure CodegenConsts.Consttab} \\
|
|
1041 |
@{index_ML CodegenData.typ_func: "theory -> thm -> typ"} \\
|
|
1042 |
@{index_ML CodegenData.rewrite_func: "thm list -> thm -> thm"} \\
|
|
1043 |
\end{mldecls}
|
|
1044 |
*}
|
20948
|
1045 |
|
|
1046 |
subsection {* Implementing code generator applications *}
|
|
1047 |
|
21147
|
1048 |
text {*
|
|
1049 |
\begin{warn}
|
|
1050 |
Some interfaces discussed here have not reached
|
|
1051 |
a final state yet.
|
|
1052 |
Changes likely to occur in future.
|
|
1053 |
\end{warn}
|
|
1054 |
*}
|
|
1055 |
|
|
1056 |
subsubsection {* Data depending on the theory's executable content *}
|
|
1057 |
|
|
1058 |
subsubsection {* Datatype hooks *}
|
|
1059 |
|
|
1060 |
text {*
|
|
1061 |
\emph{Happy proving, happy hacking!}
|
|
1062 |
*}
|
21058
|
1063 |
|
20948
|
1064 |
end
|