author | berghofe |
Wed, 07 Feb 2007 17:30:53 +0100 | |
changeset 22264 | 6a65e9b2ae05 |
parent 22188 | a63889770d57 |
child 22292 | 3b118010ec08 |
permissions | -rw-r--r-- |
20948 | 1 |
(* $Id$ *) |
2 |
||
21147 | 3 |
(*<*) |
20948 | 4 |
theory Codegen |
5 |
imports Main |
|
21452 | 6 |
uses "../../../antiquote_setup.ML" |
20948 | 7 |
begin |
8 |
||
21147 | 9 |
ML {* |
22015 | 10 |
CodegenSerializer.code_width := 74; |
21147 | 11 |
*} |
12 |
||
13 |
(*>*) |
|
14 |
||
20948 | 15 |
chapter {* Code generation from Isabelle theories *} |
16 |
||
17 |
section {* Introduction *} |
|
18 |
||
21058 | 19 |
subsection {* Motivation *} |
20 |
||
20948 | 21 |
text {* |
21058 | 22 |
Executing formal specifications as programs is a well-established |
23 |
topic in the theorem proving community. With increasing |
|
24 |
application of theorem proving systems in the area of |
|
25 |
software development and verification, its relevance manifests |
|
26 |
for running test cases and rapid prototyping. In logical |
|
27 |
calculi like constructive type theory, |
|
28 |
a notion of executability is implicit due to the nature |
|
29 |
of the calculus. In contrast, specifications in Isabelle/HOL |
|
30 |
can be highly non-executable. In order to bridge |
|
31 |
the gap between logic and executable specifications, |
|
32 |
an explicit non-trivial transformation has to be applied: |
|
33 |
code generation. |
|
34 |
||
35 |
This tutorial introduces a generic code generator for the |
|
36 |
Isabelle system \cite{isa-tutorial}. |
|
37 |
Generic in the sense that the |
|
38 |
\qn{target language} for which code shall ultimately be |
|
39 |
generated is not fixed but may be an arbitrary state-of-the-art |
|
40 |
functional programming language (currently, the implementation |
|
41 |
supports SML \cite{web:sml} and Haskell \cite{web:haskell}). |
|
42 |
We aim to provide a |
|
43 |
versatile environment |
|
44 |
suitable for software development and verification, |
|
45 |
structuring the process |
|
46 |
of code generation into a small set of orthogonal principles |
|
47 |
while achieving a big coverage of application areas |
|
48 |
with maximum flexibility. |
|
21189 | 49 |
|
50 |
For readers, some familiarity and experience |
|
51 |
with the the ingredients |
|
52 |
of the HOL \emph{Main} theory is assumed. |
|
21058 | 53 |
*} |
54 |
||
55 |
||
56 |
subsection {* Overview *} |
|
57 |
||
58 |
text {* |
|
59 |
The code generator aims to be usable with no further ado |
|
60 |
in most cases while allowing for detailed customization. |
|
61 |
This manifests in the structure of this tutorial: this introduction |
|
21147 | 62 |
continues with a short introduction of concepts. Section |
21178 | 63 |
\secref{sec:basics} explains how to use the framework naively, |
21058 | 64 |
presuming a reasonable default setup. Then, section |
65 |
\secref{sec:advanced} deals with advanced topics, |
|
66 |
introducing further aspects of the code generator framework |
|
67 |
in a motivation-driven manner. Last, section \secref{sec:ml} |
|
68 |
introduces the framework's internal programming interfaces. |
|
20948 | 69 |
|
21058 | 70 |
\begin{warn} |
71 |
Ultimately, the code generator which this tutorial deals with |
|
72 |
is supposed to replace the already established code generator |
|
73 |
by Stefan Berghofer \cite{Berghofer-Nipkow:2002}. |
|
21147 | 74 |
So, for the moment, there are two distinct code generators |
21058 | 75 |
in Isabelle. |
76 |
Also note that while the framework itself is largely |
|
77 |
object-logic independent, only HOL provides a reasonable |
|
78 |
framework setup. |
|
79 |
\end{warn} |
|
80 |
*} |
|
81 |
||
82 |
||
83 |
subsection {* Code generation process *} |
|
84 |
||
85 |
text {* |
|
21189 | 86 |
\begin{figure}[h] |
87 |
\centering |
|
88 |
\includegraphics[width=0.7\textwidth]{codegen_process} |
|
89 |
\caption{code generator -- processing overview} |
|
90 |
\label{fig:process} |
|
91 |
\end{figure} |
|
92 |
||
21058 | 93 |
The code generator employs a notion of executability |
94 |
for three foundational executable ingredients known |
|
95 |
from functional programming: |
|
22060 | 96 |
\emph{defining equations}, \emph{datatypes}, and |
97 |
\emph{type classes}. A defining equation as a first approximation |
|
21058 | 98 |
is a theorem of the form @{text "f t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n \<equiv> t"} |
99 |
(an equation headed by a constant @{text f} with arguments |
|
100 |
@{text "t\<^isub>1 t\<^isub>2 \<dots> t\<^isub>n"} and right hand side @{text t}. |
|
22060 | 101 |
Code generation aims to turn defining equations |
21058 | 102 |
into a functional program by running through |
103 |
a process (see figure \ref{fig:process}): |
|
104 |
||
105 |
\begin{itemize} |
|
106 |
||
107 |
\item Out of the vast collection of theorems proven in a |
|
108 |
\qn{theory}, a reasonable subset modeling |
|
22060 | 109 |
defining equations is \qn{selected}. |
21058 | 110 |
|
111 |
\item On those selected theorems, certain |
|
112 |
transformations are carried out |
|
113 |
(\qn{preprocessing}). Their purpose is to turn theorems |
|
114 |
representing non- or badly executable |
|
115 |
specifications into equivalent but executable counterparts. |
|
116 |
The result is a structured collection of \qn{code theorems}. |
|
117 |
||
118 |
\item These \qn{code theorems} then are extracted |
|
119 |
into an Haskell-like intermediate |
|
120 |
language. |
|
121 |
||
122 |
\item Finally, out of the intermediate language the final |
|
123 |
code in the desired \qn{target language} is \qn{serialized}. |
|
124 |
||
125 |
\end{itemize} |
|
126 |
||
127 |
From these steps, only the two last are carried out |
|
128 |
outside the logic; by keeping this layer as |
|
129 |
thin as possible, the amount of code to trust is |
|
130 |
kept to a minimum. |
|
131 |
*} |
|
132 |
||
133 |
||
134 |
||
135 |
section {* Basics \label{sec:basics} *} |
|
20948 | 136 |
|
137 |
subsection {* Invoking the code generator *} |
|
138 |
||
21058 | 139 |
text {* |
140 |
Thanks to a reasonable setup of the HOL theories, in |
|
141 |
most cases code generation proceeds without further ado: |
|
142 |
*} |
|
143 |
||
144 |
consts |
|
145 |
fac :: "nat \<Rightarrow> nat" |
|
146 |
||
147 |
primrec |
|
148 |
"fac 0 = 1" |
|
149 |
"fac (Suc n) = Suc n * fac n" |
|
150 |
||
151 |
text {* |
|
152 |
This executable specification is now turned to SML code: |
|
153 |
*} |
|
154 |
||
21545 | 155 |
code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac.ML") |
21058 | 156 |
|
157 |
text {* |
|
21323 | 158 |
The @{text "\<CODEGEN>"} command takes a space-separated list of |
21058 | 159 |
constants together with \qn{serialization directives} |
160 |
in parentheses. These start with a \qn{target language} |
|
21178 | 161 |
identifier, followed by arguments, their semantics |
21058 | 162 |
depending on the target. In the SML case, a filename |
163 |
is given where to write the generated code to. |
|
164 |
||
22060 | 165 |
Internally, the defining equations for all selected |
21178 | 166 |
constants are taken, including any transitively required |
21058 | 167 |
constants, datatypes and classes, resulting in the following |
168 |
code: |
|
169 |
||
170 |
\lstsml{Thy/examples/fac.ML} |
|
171 |
||
172 |
The code generator will complain when a required |
|
173 |
ingredient does not provide a executable counterpart. |
|
174 |
This is the case if an involved type is not a datatype: |
|
175 |
*} |
|
176 |
||
177 |
(*<*) |
|
178 |
setup {* Sign.add_path "foo" *} |
|
179 |
(*>*) |
|
180 |
||
181 |
typedecl 'a foo |
|
182 |
||
183 |
definition |
|
21993 | 184 |
bar :: "'a foo \<Rightarrow> 'a \<Rightarrow> 'a" where |
21058 | 185 |
"bar x y = y" |
186 |
||
187 |
(*<*) |
|
188 |
hide type foo |
|
189 |
hide const bar |
|
190 |
||
191 |
setup {* Sign.parent_path *} |
|
192 |
||
193 |
datatype 'a foo = Foo |
|
194 |
||
195 |
definition |
|
21993 | 196 |
bar :: "'a foo \<Rightarrow> 'a \<Rightarrow> 'a" where |
21058 | 197 |
"bar x y = y" |
198 |
(*>*) |
|
199 |
||
200 |
code_gen bar (SML "examples/fail_type.ML") |
|
201 |
||
202 |
text {* |
|
203 |
\noindent will result in an error. Likewise, generating code |
|
204 |
for constants not yielding |
|
22060 | 205 |
a defining equation will fail, e.g.~the Hilbert choice |
21058 | 206 |
operation @{text "SOME"}: |
207 |
*} |
|
208 |
||
209 |
(*<*) |
|
210 |
setup {* Sign.add_path "foo" *} |
|
211 |
(*>*) |
|
212 |
||
213 |
definition |
|
21993 | 214 |
pick_some :: "'a list \<Rightarrow> 'a" where |
21058 | 215 |
"pick_some xs = (SOME x. x \<in> set xs)" |
216 |
||
217 |
(*<*) |
|
218 |
hide const pick_some |
|
219 |
||
220 |
setup {* Sign.parent_path *} |
|
221 |
||
222 |
definition |
|
21993 | 223 |
pick_some :: "'a list \<Rightarrow> 'a" where |
21058 | 224 |
"pick_some = hd" |
225 |
(*>*) |
|
226 |
||
227 |
code_gen pick_some (SML "examples/fail_const.ML") |
|
228 |
||
20948 | 229 |
subsection {* Theorem selection *} |
230 |
||
21058 | 231 |
text {* |
22060 | 232 |
The list of all defining equations in a theory may be inspected |
21323 | 233 |
using the @{text "\<PRINTCODETHMS>"} command: |
21058 | 234 |
*} |
235 |
||
236 |
print_codethms |
|
237 |
||
238 |
text {* |
|
239 |
\noindent which displays a table of constant with corresponding |
|
22060 | 240 |
defining equations (the additional stuff displayed |
21058 | 241 |
shall not bother us for the moment). If this table does |
242 |
not provide at least one function |
|
21178 | 243 |
equation, the table of primitive definitions is searched |
21058 | 244 |
whether it provides one. |
245 |
||
246 |
The typical HOL tools are already set up in a way that |
|
21323 | 247 |
function definitions introduced by @{text "\<FUN>"}, |
248 |
@{text "\<FUNCTION>"}, @{text "\<PRIMREC>"} |
|
249 |
@{text "\<RECDEF>"} are implicitly propagated |
|
22060 | 250 |
to this defining equation table. Specific theorems may be |
21058 | 251 |
selected using an attribute: \emph{code func}. As example, |
252 |
a weight selector function: |
|
253 |
*} |
|
254 |
||
255 |
consts |
|
256 |
pick :: "(nat \<times> 'a) list \<Rightarrow> nat \<Rightarrow> 'a" |
|
257 |
||
258 |
primrec |
|
259 |
"pick (x#xs) n = (let (k, v) = x in |
|
260 |
if n < k then v else pick xs (n - k))" |
|
261 |
||
262 |
text {* |
|
263 |
We want to eliminate the explicit destruction |
|
264 |
of @{term x} to @{term "(k, v)"}: |
|
265 |
*} |
|
266 |
||
267 |
lemma [code func]: |
|
268 |
"pick ((k, v)#xs) n = (if n < k then v else pick xs (n - k))" |
|
269 |
by simp |
|
270 |
||
21545 | 271 |
code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick1.ML") |
21058 | 272 |
|
273 |
text {* |
|
22060 | 274 |
This theorem is now added to the defining equation table: |
21058 | 275 |
|
276 |
\lstsml{Thy/examples/pick1.ML} |
|
277 |
||
278 |
It might be convenient to remove the pointless original |
|
279 |
equation, using the \emph{nofunc} attribute: |
|
280 |
*} |
|
281 |
||
282 |
lemmas [code nofunc] = pick.simps |
|
283 |
||
21545 | 284 |
code_gen pick (*<*)(SML #)(*>*)(SML "examples/pick2.ML") |
21058 | 285 |
|
286 |
text {* |
|
287 |
\lstsml{Thy/examples/pick2.ML} |
|
288 |
||
289 |
Syntactic redundancies are implicitly dropped. For example, |
|
290 |
using a modified version of the @{const fac} function |
|
22060 | 291 |
as defining equation, the then redundant (since |
292 |
syntactically subsumed) original defining equations |
|
21058 | 293 |
are dropped, resulting in a warning: |
294 |
*} |
|
295 |
||
296 |
lemma [code func]: |
|
297 |
"fac n = (case n of 0 \<Rightarrow> 1 | Suc m \<Rightarrow> n * fac m)" |
|
298 |
by (cases n) simp_all |
|
299 |
||
21545 | 300 |
code_gen fac (*<*)(SML #)(*>*)(SML "examples/fac_case.ML") |
21058 | 301 |
|
302 |
text {* |
|
303 |
\lstsml{Thy/examples/fac_case.ML} |
|
304 |
||
305 |
\begin{warn} |
|
306 |
Some statements in this section have to be treated with some |
|
307 |
caution. First, since the HOL function package is still |
|
308 |
under development, its setup with respect to code generation |
|
309 |
may differ from what is presumed here. |
|
310 |
Further, the attributes \emph{code} and \emph{code del} |
|
311 |
associated with the existing code generator also apply to |
|
312 |
the new one: \emph{code} implies \emph{code func}, |
|
313 |
and \emph{code del} implies \emph{code nofunc}. |
|
314 |
\end{warn} |
|
315 |
*} |
|
20948 | 316 |
|
317 |
subsection {* Type classes *} |
|
318 |
||
21058 | 319 |
text {* |
320 |
Type classes enter the game via the Isar class package. |
|
21075 | 321 |
For a short introduction how to use it, see \cite{isabelle-classes}; |
21147 | 322 |
here we just illustrate its impact on code generation. |
21058 | 323 |
|
324 |
In a target language, type classes may be represented |
|
21178 | 325 |
natively (as in the case of Haskell). For languages |
21058 | 326 |
like SML, they are implemented using \emph{dictionaries}. |
21178 | 327 |
Our following example specifies a class \qt{null}, |
21058 | 328 |
assigning to each of its inhabitants a \qt{null} value: |
329 |
*} |
|
330 |
||
331 |
class null = |
|
332 |
fixes null :: 'a |
|
333 |
||
334 |
consts |
|
335 |
head :: "'a\<Colon>null list \<Rightarrow> 'a" |
|
336 |
||
337 |
primrec |
|
338 |
"head [] = null" |
|
339 |
"head (x#xs) = x" |
|
340 |
||
341 |
text {* |
|
342 |
We provide some instances for our @{text null}: |
|
343 |
*} |
|
344 |
||
345 |
instance option :: (type) null |
|
346 |
"null \<equiv> None" .. |
|
347 |
||
348 |
instance list :: (type) null |
|
349 |
"null \<equiv> []" .. |
|
350 |
||
351 |
text {* |
|
352 |
Constructing a dummy example: |
|
353 |
*} |
|
354 |
||
355 |
definition |
|
356 |
"dummy = head [Some (Suc 0), None]" |
|
357 |
||
358 |
text {* |
|
21178 | 359 |
Type classes offer a suitable occasion to introduce |
21058 | 360 |
the Haskell serializer. Its usage is almost the same |
21075 | 361 |
as SML, but, in accordance with conventions |
362 |
some Haskell systems enforce, each module ends |
|
363 |
up in a single file. The module hierarchy is reflected in |
|
364 |
the file system, with root given by the user. |
|
21058 | 365 |
*} |
366 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
367 |
ML {* set Toplevel.debug *} |
21075 | 368 |
code_gen dummy (Haskell "examples/") |
21147 | 369 |
(* NOTE: you may use Haskell only once in this document, otherwise |
370 |
you have to work in distinct subdirectories *) |
|
21058 | 371 |
|
372 |
text {* |
|
373 |
\lsthaskell{Thy/examples/Codegen.hs} |
|
374 |
||
375 |
(we have left out all other modules). |
|
376 |
||
377 |
The whole code in SML with explicit dictionary passing: |
|
378 |
*} |
|
379 |
||
21545 | 380 |
code_gen dummy (*<*)(SML #)(*>*)(SML "examples/class.ML") |
21058 | 381 |
|
382 |
text {* |
|
383 |
\lstsml{Thy/examples/class.ML} |
|
384 |
*} |
|
385 |
||
386 |
subsection {* Incremental code generation *} |
|
387 |
||
21075 | 388 |
text {* |
389 |
Code generation is \emph{incremental}: theorems |
|
390 |
and abstract intermediate code are cached and extended on demand. |
|
391 |
The cache may be partially or fully dropped if the underlying |
|
392 |
executable content of the theory changes. |
|
393 |
Implementation of caching is supposed to transparently |
|
394 |
hid away the details from the user. Anyway, caching |
|
395 |
reaches the surface by using a slightly more general form |
|
21323 | 396 |
of the @{text "\<CODEGEN>"}: either the list of constants or the |
21075 | 397 |
list of serialization expressions may be dropped. If no |
398 |
serialization expressions are given, only abstract code |
|
399 |
is generated and cached; if no constants are given, the |
|
400 |
current cache is serialized. |
|
401 |
||
21323 | 402 |
For explorative purpose, an extended version of the |
403 |
@{text "\<CODEGEN>"} command may prove useful: |
|
21075 | 404 |
*} |
405 |
||
406 |
print_codethms () |
|
407 |
||
408 |
text {* |
|
22060 | 409 |
\noindent print all cached defining equations (i.e.~\emph{after} |
21452 | 410 |
any applied transformation). Inside the brackets a |
21075 | 411 |
list of constants may be given; their function |
21178 | 412 |
equations are added to the cache if not already present. |
21075 | 413 |
*} |
414 |
||
21058 | 415 |
|
416 |
section {* Recipes and advanced topics \label{sec:advanced} *} |
|
417 |
||
21089 | 418 |
text {* |
419 |
In this tutorial, we do not attempt to give an exhaustive |
|
420 |
description of the code generator framework; instead, |
|
421 |
we cast a light on advanced topics by introducing |
|
422 |
them together with practically motivated examples. Concerning |
|
423 |
further reading, see |
|
424 |
||
425 |
\begin{itemize} |
|
426 |
||
427 |
\item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} |
|
428 |
for exhaustive syntax diagrams. |
|
21222 | 429 |
\item or \fixme[ref] which deals with foundational issues |
21089 | 430 |
of the code generator framework. |
431 |
||
432 |
\end{itemize} |
|
433 |
*} |
|
21058 | 434 |
|
435 |
subsection {* Library theories *} |
|
436 |
||
21089 | 437 |
text {* |
438 |
The HOL \emph{Main} theory already provides a code generator setup |
|
439 |
which should be suitable for most applications. Common extensions |
|
440 |
and modifications are available by certain theories of the HOL |
|
441 |
library; beside being useful in applications, they may serve |
|
21178 | 442 |
as a tutorial for customizing the code generator setup. |
21089 | 443 |
|
444 |
\begin{description} |
|
445 |
||
21452 | 446 |
\item[@{text "ExecutableSet"}] allows to generate code |
21089 | 447 |
for finite sets using lists. |
21452 | 448 |
\item[@{text "ExecutableRat"}] \label{exec_rat} implements rational |
21089 | 449 |
numbers as triples @{text "(sign, enumerator, denominator)"}. |
21452 | 450 |
\item[@{text "EfficientNat"}] \label{eff_nat} implements natural numbers by integers, |
21178 | 451 |
which in general will result in higher efficency; pattern |
21089 | 452 |
matching with @{const "0\<Colon>nat"} / @{const "Suc"} |
21189 | 453 |
is eliminated. |
21452 | 454 |
\item[@{text "MLString"}] provides an additional datatype @{text "mlstring"}; |
21089 | 455 |
in the HOL default setup, strings in HOL are mapped to list |
456 |
of chars in SML; values of type @{text "mlstring"} are |
|
457 |
mapped to strings in SML. |
|
458 |
||
459 |
\end{description} |
|
460 |
*} |
|
461 |
||
20948 | 462 |
subsection {* Preprocessing *} |
463 |
||
21089 | 464 |
text {* |
21147 | 465 |
Before selected function theorems are turned into abstract |
466 |
code, a chain of definitional transformation steps is carried |
|
21178 | 467 |
out: \emph{preprocessing}. There are three possibilities |
21147 | 468 |
to customize preprocessing: \emph{inline theorems}, |
469 |
\emph{inline procedures} and \emph{generic preprocessors}. |
|
470 |
||
471 |
\emph{Inline theorems} are rewriting rules applied to each |
|
22060 | 472 |
defining equation. Due to the interpretation of theorems |
473 |
of defining equations, rewrites are applied to the right |
|
21147 | 474 |
hand side and the arguments of the left hand side of an |
475 |
equation, but never to the constant heading the left hand side. |
|
476 |
Inline theorems may be declared an undeclared using the |
|
21178 | 477 |
\emph{code inline} or \emph{code noinline} attribute respectively. |
21147 | 478 |
|
479 |
Some common applications: |
|
480 |
*} |
|
481 |
||
482 |
text_raw {* |
|
483 |
\begin{itemize} |
|
484 |
\item replacing non-executable constructs by executable ones: \\ |
|
485 |
*} |
|
486 |
||
487 |
lemma [code inline]: |
|
488 |
"x \<in> set xs \<longleftrightarrow> x mem xs" by (induct xs) simp_all |
|
489 |
||
490 |
text_raw {* |
|
491 |
\item eliminating superfluous constants: \\ |
|
492 |
*} |
|
493 |
||
494 |
lemma [code inline]: |
|
495 |
"1 = Suc 0" by simp |
|
496 |
||
497 |
text_raw {* |
|
498 |
\item replacing executable but inconvenient constructs: \\ |
|
21089 | 499 |
*} |
500 |
||
21147 | 501 |
lemma [code inline]: |
502 |
"xs = [] \<longleftrightarrow> List.null xs" by (induct xs) simp_all |
|
503 |
||
504 |
text_raw {* |
|
505 |
\end{itemize} |
|
506 |
*} |
|
507 |
||
508 |
text {* |
|
509 |
The current set of inline theorems may be inspected using |
|
21323 | 510 |
the @{text "\<PRINTCODETHMS>"} command. |
21147 | 511 |
|
512 |
\emph{Inline procedures} are a generalized version of inline |
|
513 |
theorems written in ML -- rewrite rules are generated dependent |
|
514 |
on the function theorems for a certain function. One |
|
515 |
application is the implicit expanding of @{typ nat} numerals |
|
516 |
to @{const "0\<Colon>nat"} / @{const Suc} representation. See further |
|
517 |
\secref{sec:ml} |
|
518 |
||
519 |
\emph{Generic preprocessors} provide a most general interface, |
|
520 |
transforming a list of function theorems to another |
|
521 |
list of function theorems, provided that neither the heading |
|
522 |
constant nor its type change. The @{const "0\<Colon>nat"} / @{const Suc} |
|
21323 | 523 |
pattern elimination implemented in |
21452 | 524 |
theory @{text "EfficientNat"} (\secref{eff_nat}) uses this |
21147 | 525 |
interface. |
526 |
||
527 |
\begin{warn} |
|
528 |
The order in which single preprocessing steps are carried |
|
529 |
out currently is not specified; in particular, preprocessing |
|
21178 | 530 |
is \emph{no} fix point process. Keep this in mind when |
21147 | 531 |
setting up the preprocessor. |
532 |
||
533 |
Further, the attribute \emph{code unfold} |
|
534 |
associated with the existing code generator also applies to |
|
535 |
the new one: \emph{code unfold} implies \emph{code inline}. |
|
536 |
\end{warn} |
|
537 |
*} |
|
20948 | 538 |
|
21058 | 539 |
subsection {* Customizing serialization *} |
20948 | 540 |
|
21147 | 541 |
text {* |
542 |
Consider the following function and its corresponding |
|
543 |
SML code: |
|
544 |
*} |
|
545 |
||
546 |
fun |
|
547 |
in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where |
|
548 |
"in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l" |
|
549 |
(*<*) |
|
550 |
declare in_interval.simps [code func] |
|
551 |
(*>*) |
|
552 |
||
553 |
(*<*) |
|
21323 | 554 |
code_type %tt bool |
21147 | 555 |
(SML) |
21323 | 556 |
code_const %tt True and False and "op \<and>" and Not |
21147 | 557 |
(SML and and and) |
558 |
(*>*) |
|
559 |
||
21545 | 560 |
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_literal.ML") |
21147 | 561 |
|
562 |
text {* |
|
21323 | 563 |
\lstsml{Thy/examples/bool_literal.ML} |
21147 | 564 |
|
565 |
Though this is correct code, it is a little bit unsatisfactory: |
|
566 |
boolean values and operators are materialized as distinguished |
|
567 |
entities with have nothing to do with the SML-builtin notion |
|
568 |
of \qt{bool}. This results in less readable code; |
|
569 |
additionally, eager evaluation may cause programs to |
|
570 |
loop or break which would perfectly terminate when |
|
571 |
the existing SML \qt{bool} would be used. To map |
|
572 |
the HOL \qt{bool} on SML \qt{bool}, we may use |
|
573 |
\qn{custom serializations}: |
|
574 |
*} |
|
575 |
||
21323 | 576 |
code_type %tt bool |
21147 | 577 |
(SML "bool") |
21323 | 578 |
code_const %tt True and False and "op \<and>" |
21147 | 579 |
(SML "true" and "false" and "_ andalso _") |
580 |
||
581 |
text {* |
|
21323 | 582 |
The @{text "\<CODETYPE>"} commad takes a type constructor |
21147 | 583 |
as arguments together with a list of custom serializations. |
584 |
Each custom serialization starts with a target language |
|
585 |
identifier followed by an expression, which during |
|
586 |
code serialization is inserted whenever the type constructor |
|
21323 | 587 |
would occur. For constants, @{text "\<CODECONST>"} implements |
588 |
the corresponding mechanism. Each ``@{verbatim "_"}'' in |
|
21147 | 589 |
a serialization expression is treated as a placeholder |
590 |
for the type constructor's (the constant's) arguments. |
|
591 |
*} |
|
592 |
||
593 |
code_reserved SML |
|
594 |
bool true false |
|
595 |
||
596 |
text {* |
|
597 |
To assert that the existing \qt{bool}, \qt{true} and \qt{false} |
|
21323 | 598 |
is not used for generated code, we use @{text "\<CODERESERVED>"}. |
21147 | 599 |
|
600 |
After this setup, code looks quite more readable: |
|
601 |
*} |
|
602 |
||
21545 | 603 |
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_mlbool.ML") |
21147 | 604 |
|
605 |
text {* |
|
21323 | 606 |
\lstsml{Thy/examples/bool_mlbool.ML} |
21147 | 607 |
|
608 |
This still is not perfect: the parentheses |
|
21323 | 609 |
around the \qt{andalso} expression are superfluous. |
610 |
Though the serializer |
|
21147 | 611 |
by no means attempts to imitate the rich Isabelle syntax |
612 |
framework, it provides some common idioms, notably |
|
613 |
associative infixes with precedences which may be used here: |
|
614 |
*} |
|
615 |
||
21323 | 616 |
code_const %tt "op \<and>" |
21147 | 617 |
(SML infixl 1 "andalso") |
618 |
||
21545 | 619 |
code_gen in_interval (*<*)(SML #)(*>*)(SML "examples/bool_infix.ML") |
21147 | 620 |
|
621 |
text {* |
|
21323 | 622 |
\lstsml{Thy/examples/bool_infix.ML} |
21147 | 623 |
|
624 |
Next, we try to map HOL pairs to SML pairs, using the |
|
21323 | 625 |
infix ``@{verbatim "*"}'' type constructor and parentheses: |
21147 | 626 |
*} |
627 |
||
628 |
(*<*) |
|
629 |
code_type * |
|
630 |
(SML) |
|
631 |
code_const Pair |
|
632 |
(SML) |
|
633 |
(*>*) |
|
634 |
||
21323 | 635 |
code_type %tt * |
21147 | 636 |
(SML infix 2 "*") |
637 |
||
21323 | 638 |
code_const %tt Pair |
21147 | 639 |
(SML "!((_),/ (_))") |
640 |
||
641 |
text {* |
|
21323 | 642 |
The initial bang ``@{verbatim "!"}'' tells the serializer to never put |
21147 | 643 |
parentheses around the whole expression (they are already present), |
644 |
while the parentheses around argument place holders |
|
645 |
tell not to put parentheses around the arguments. |
|
21323 | 646 |
The slash ``@{verbatim "/"}'' (followed by arbitrary white space) |
21147 | 647 |
inserts a space which may be used as a break if necessary |
648 |
during pretty printing. |
|
649 |
||
21178 | 650 |
So far, we did only provide more idiomatic serializations for |
651 |
constructs which would be executable on their own. Target-specific |
|
652 |
serializations may also be used to \emph{implement} constructs |
|
653 |
which have no implicit notion of executability. For example, |
|
654 |
take the HOL integers: |
|
655 |
*} |
|
656 |
||
657 |
definition |
|
21993 | 658 |
double_inc :: "int \<Rightarrow> int" where |
21178 | 659 |
"double_inc k = 2 * k + 1" |
660 |
||
661 |
code_gen double_inc (SML "examples/integers.ML") |
|
662 |
||
663 |
text {* |
|
664 |
will fail: @{typ int} in HOL is implemented using a quotient |
|
665 |
type, which does not provide any notion of executability. |
|
666 |
\footnote{Eventually, we also want to provide executability |
|
667 |
for quotients.}. However, we could use the SML builtin |
|
668 |
integers: |
|
21147 | 669 |
*} |
670 |
||
21323 | 671 |
code_type %tt int |
21147 | 672 |
(SML "IntInf.int") |
673 |
||
21323 | 674 |
code_const %tt "op + \<Colon> int \<Rightarrow> int \<Rightarrow> int" |
21147 | 675 |
and "op * \<Colon> int \<Rightarrow> int \<Rightarrow> int" |
21178 | 676 |
(SML "IntInf.+ (_, _)" and "IntInf.* (_, _)") |
677 |
||
21545 | 678 |
code_gen double_inc (*<*)(SML #)(*>*)(SML "examples/integers.ML") |
21147 | 679 |
|
21178 | 680 |
text {* |
681 |
resulting in: |
|
21147 | 682 |
|
21178 | 683 |
\lstsml{Thy/examples/integers.ML} |
684 |
*} |
|
21147 | 685 |
|
21178 | 686 |
text {* |
687 |
These examples give a glimpse what powerful mechanisms |
|
688 |
custom serializations provide; however their usage |
|
689 |
requires careful thinking in order not to introduce |
|
690 |
inconsistencies -- or, in other words: |
|
691 |
custom serializations are completely axiomatic. |
|
21147 | 692 |
|
21178 | 693 |
A further noteworthy details is that any special |
694 |
character in a custom serialization may be quoted |
|
21323 | 695 |
using ``@{verbatim "'"}''; thus, in |
696 |
``@{verbatim "fn '_ => _"}'' the first |
|
697 |
``@{verbatim "_"}'' is a proper underscore while the |
|
698 |
second ``@{verbatim "_"}'' is a placeholder. |
|
21147 | 699 |
|
21178 | 700 |
The HOL theories provide further |
701 |
examples for custom serializations and form |
|
702 |
a recommended tutorial on how to use them properly. |
|
703 |
*} |
|
21147 | 704 |
|
705 |
subsection {* Concerning operational equality *} |
|
706 |
||
707 |
text {* |
|
708 |
Surely you have already noticed how equality is treated |
|
709 |
by the code generator: |
|
710 |
*} |
|
711 |
||
712 |
fun |
|
713 |
collect_duplicates :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where |
|
714 |
"collect_duplicates xs ys [] = xs" |
|
715 |
"collect_duplicates xs ys (z#zs) = (if z \<in> set xs |
|
716 |
then if z \<in> set ys |
|
717 |
then collect_duplicates xs ys zs |
|
718 |
else collect_duplicates xs (z#ys) zs |
|
719 |
else collect_duplicates (z#xs) (z#ys) zs)" |
|
720 |
(*<*) |
|
721 |
lemmas [code func] = collect_duplicates.simps |
|
722 |
(*>*) |
|
723 |
||
724 |
text {* |
|
21217 | 725 |
The membership test during preprocessing is rewritten, |
21147 | 726 |
resulting in @{const List.memberl}, which itself |
727 |
performs an explicit equality check. |
|
728 |
*} |
|
729 |
||
21545 | 730 |
code_gen collect_duplicates (*<*)(SML #)(*>*)(SML "examples/collect_duplicates.ML") |
21147 | 731 |
|
732 |
text {* |
|
733 |
\lstsml{Thy/examples/collect_duplicates.ML} |
|
734 |
*} |
|
735 |
||
736 |
text {* |
|
737 |
Obviously, polymorphic equality is implemented the Haskell |
|
738 |
way using a type class. How is this achieved? By an |
|
739 |
almost trivial definition in the HOL setup: |
|
740 |
*} |
|
741 |
||
742 |
(*<*) |
|
743 |
setup {* Sign.add_path "foo" *} |
|
21452 | 744 |
consts "op =" :: "'a" |
21147 | 745 |
(*>*) |
746 |
||
21452 | 747 |
axclass eq \<subseteq> type |
21993 | 748 |
(attach "op =") |
21147 | 749 |
|
750 |
text {* |
|
751 |
This merely introduces a class @{text eq} with corresponding |
|
21993 | 752 |
operation @{text "op ="}; |
21452 | 753 |
the preprocessing framework does the rest. |
21147 | 754 |
*} |
755 |
||
756 |
(*<*) |
|
757 |
hide (open) "class" eq |
|
21452 | 758 |
hide (open) const "op =" |
21147 | 759 |
setup {* Sign.parent_path *} |
760 |
(*>*) |
|
761 |
||
762 |
text {* |
|
763 |
For datatypes, instances of @{text eq} are implicitly derived |
|
764 |
when possible. |
|
765 |
||
766 |
Though this class is designed to get rarely in the way, there |
|
767 |
are some cases when it suddenly comes to surface: |
|
768 |
*} |
|
769 |
||
21223 | 770 |
subsubsection {* typedecls interpreted by customary serializations *} |
21178 | 771 |
|
772 |
text {* |
|
773 |
A common idiom is to use unspecified types for formalizations |
|
774 |
and interpret them for a specific target language: |
|
21147 | 775 |
*} |
776 |
||
777 |
typedecl key |
|
778 |
||
779 |
fun |
|
780 |
lookup :: "(key \<times> 'a) list \<Rightarrow> key \<Rightarrow> 'a option" where |
|
781 |
"lookup [] l = None" |
|
782 |
"lookup ((k, v) # xs) l = (if k = l then Some v else lookup xs l)" |
|
783 |
(*<*) |
|
784 |
lemmas [code func] = lookup.simps |
|
785 |
(*>*) |
|
786 |
||
21323 | 787 |
code_type %tt key |
21147 | 788 |
(SML "string") |
789 |
||
21178 | 790 |
text {* |
791 |
This, though, is not sufficient: @{typ key} is no instance |
|
21147 | 792 |
of @{text eq} since @{typ key} is no datatype; the instance |
793 |
has to be declared manually, including a serialization |
|
21452 | 794 |
for the particular instance of @{const "op ="}: |
21147 | 795 |
*} |
796 |
||
797 |
instance key :: eq .. |
|
798 |
||
21452 | 799 |
code_const %tt "op = \<Colon> key \<Rightarrow> key \<Rightarrow> bool" |
800 |
(SML "!((_ : string) = _)") |
|
21147 | 801 |
|
21178 | 802 |
text {* |
803 |
Then everything goes fine: |
|
21147 | 804 |
*} |
805 |
||
21545 | 806 |
code_gen lookup (*<*)(SML #)(*>*)(SML "examples/lookup.ML") |
21147 | 807 |
|
808 |
text {* |
|
809 |
\lstsml{Thy/examples/lookup.ML} |
|
810 |
*} |
|
811 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
812 |
subsubsection {* lexicographic orderings *} |
21178 | 813 |
|
814 |
text {* |
|
815 |
Another subtlety |
|
21147 | 816 |
enters the stage when definitions of overloaded constants |
817 |
are dependent on operational equality. For example, let |
|
21189 | 818 |
us define a lexicographic ordering on tuples: |
21147 | 819 |
*} |
820 |
||
21178 | 821 |
instance * :: (ord, ord) ord |
822 |
"p1 < p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
|
21147 | 823 |
x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
824 |
"p1 \<le> p2 \<equiv> let (x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) = p1; (x2, y2) = p2 in |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
825 |
x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" .. |
21147 | 826 |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
827 |
lemma ord_prod [code func]: |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
828 |
"(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
829 |
"(x1 \<Colon> 'a\<Colon>ord, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
830 |
unfolding "less_eq_*_def" "less_*_def" by simp_all |
21147 | 831 |
|
21178 | 832 |
text {* |
833 |
Then code generation will fail. Why? The definition |
|
21147 | 834 |
of @{const "op \<le>"} depends on equality on both arguments, |
21178 | 835 |
which are polymorphic and impose an additional @{text eq} |
21147 | 836 |
class constraint, thus violating the type discipline |
837 |
for class operations. |
|
838 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
839 |
The solution is to add @{text eq} explicitly to the first sort arguments in the |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
840 |
code theorems: |
21147 | 841 |
*} |
842 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
843 |
(*<*) |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
844 |
declare ord_prod [code del] |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
845 |
(*>*) |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
846 |
|
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
847 |
lemma ord_prod_code [code func]: |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
848 |
"(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) < (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 < y2)" |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
849 |
"(x1 \<Colon> 'a\<Colon>{ord, eq}, y1 \<Colon> 'b\<Colon>ord) \<le> (x2, y2) \<longleftrightarrow> x1 < x2 \<or> (x1 = x2 \<and> y1 \<le> y2)" |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
850 |
unfolding ord_prod by rule+ |
21147 | 851 |
|
21178 | 852 |
text {* |
853 |
Then code generation succeeds: |
|
21147 | 854 |
*} |
855 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
856 |
code_gen "op \<le> \<Colon> 'a\<Colon>{eq, ord} \<times> 'b\<Colon>ord \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool" |
21545 | 857 |
(*<*)(SML #)(*>*)(SML "examples/lexicographic.ML") |
21147 | 858 |
|
859 |
text {* |
|
860 |
\lstsml{Thy/examples/lexicographic.ML} |
|
861 |
*} |
|
862 |
||
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
863 |
text {* |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
864 |
In general, code theorems for overloaded constants may have more |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
865 |
restrictive sort constraints than the underlying instance relation |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
866 |
between class and type constructor as long as the whole system of |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
867 |
constraints is coregular; code theorems violating coregularity |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
868 |
are rejected immediately. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
869 |
*} |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
870 |
|
21178 | 871 |
subsubsection {* Haskell serialization *} |
872 |
||
873 |
text {* |
|
874 |
For convenience, the default |
|
875 |
HOL setup for Haskell maps the @{text eq} class to |
|
876 |
its counterpart in Haskell, giving custom serializations |
|
21323 | 877 |
for the class (@{text "\<CODECLASS>"}) and its operation: |
21178 | 878 |
*} |
879 |
||
880 |
(*<*) |
|
881 |
setup {* Sign.add_path "bar" *} |
|
882 |
class eq = fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool" |
|
883 |
(*>*) |
|
884 |
||
21323 | 885 |
code_class %tt eq |
21178 | 886 |
(Haskell "Eq" where eq \<equiv> "(==)") |
887 |
||
21323 | 888 |
code_const %tt eq |
21178 | 889 |
(Haskell infixl 4 "==") |
890 |
||
891 |
(*<*) |
|
892 |
hide "class" eq |
|
893 |
hide const eq |
|
894 |
setup {* Sign.parent_path *} |
|
895 |
(*>*) |
|
896 |
||
897 |
text {* |
|
898 |
A problem now occurs whenever a type which |
|
899 |
is an instance of @{text eq} in HOL is mapped |
|
900 |
on a Haskell-builtin type which is also an instance |
|
901 |
of Haskell @{text Eq}: |
|
21147 | 902 |
*} |
903 |
||
21178 | 904 |
typedecl bar |
905 |
||
906 |
instance bar :: eq .. |
|
907 |
||
21323 | 908 |
code_type %tt bar |
21178 | 909 |
(Haskell "Integer") |
910 |
||
911 |
text {* |
|
22188
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
912 |
The code generator would produce |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
913 |
an additional instance, which of course is rejected. |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
914 |
To suppress this additional instance, use |
a63889770d57
adjusted manual to improved treatment of overloaded constants
haftmann
parents:
22175
diff
changeset
|
915 |
@{text "\<CODEINSTANCE>"}: |
21147 | 916 |
*} |
917 |
||
21323 | 918 |
code_instance %tt bar :: eq |
21178 | 919 |
(Haskell -) |
920 |
||
921 |
subsection {* Types matter *} |
|
922 |
||
923 |
text {* |
|
924 |
Imagine the following quick-and-dirty setup for implementing |
|
21189 | 925 |
some kind of sets as lists in SML: |
21178 | 926 |
*} |
927 |
||
21323 | 928 |
code_type %tt set |
21178 | 929 |
(SML "_ list") |
930 |
||
21323 | 931 |
code_const %tt "{}" and insert |
21178 | 932 |
(SML "![]" and infixl 7 "::") |
933 |
||
934 |
definition |
|
21993 | 935 |
dummy_set :: "(nat \<Rightarrow> nat) set" where |
21189 | 936 |
"dummy_set = {Suc}" |
937 |
||
938 |
text {* |
|
939 |
Then code generation for @{const dummy_set} will fail. |
|
22060 | 940 |
Why? A glimpse at the defining equations will offer: |
21189 | 941 |
*} |
942 |
||
943 |
print_codethms (insert) |
|
944 |
||
945 |
text {* |
|
22060 | 946 |
This reveals the defining equation @{thm insert_def} |
21223 | 947 |
for @{const insert}, which is operationally meaningless |
21189 | 948 |
but forces an equality constraint on the set members |
21223 | 949 |
(which is not satisfiable if the set members are functions). |
21189 | 950 |
Even when using set of natural numbers (which are an instance |
951 |
of \emph{eq}), we run into a problem: |
|
952 |
*} |
|
953 |
||
954 |
definition |
|
21993 | 955 |
foobar_set :: "nat set" where |
21189 | 956 |
"foobar_set = {0, 1, 2}" |
957 |
||
958 |
text {* |
|
959 |
In this case the serializer would complain that @{const insert} |
|
960 |
expects dictionaries (namely an \emph{eq} dictionary) but |
|
961 |
has also been given a customary serialization. |
|
962 |
||
963 |
The solution to this dilemma: |
|
964 |
*} |
|
965 |
||
966 |
lemma [code func]: |
|
967 |
"insert = insert" .. |
|
968 |
||
21545 | 969 |
code_gen dummy_set foobar_set (*<*)(SML #)(*>*)(SML "examples/dirty_set.ML") |
21189 | 970 |
|
971 |
text {* |
|
972 |
\lstsml{Thy/examples/dirty_set.ML} |
|
21178 | 973 |
|
22060 | 974 |
Reflexive defining equations by convention are dropped. |
21189 | 975 |
But their presence prevents primitive definitions to be |
22060 | 976 |
used as defining equations: |
21189 | 977 |
*} |
978 |
||
979 |
print_codethms (insert) |
|
980 |
||
981 |
text {* |
|
22060 | 982 |
will show \emph{no} defining equations for insert. |
21178 | 983 |
|
21189 | 984 |
Note that the sort constraints of reflexive equations |
985 |
are considered; so |
|
986 |
*} |
|
987 |
||
988 |
lemma [code func]: |
|
989 |
"(insert \<Colon> 'a\<Colon>eq \<Rightarrow> 'a set \<Rightarrow> 'a set) = insert" .. |
|
990 |
||
991 |
text {* |
|
992 |
would mean nothing else than to introduce the evil |
|
993 |
sort constraint by hand. |
|
994 |
*} |
|
995 |
||
996 |
subsection {* Cyclic module dependencies *} |
|
21178 | 997 |
|
21189 | 998 |
text {* |
999 |
Sometimes the awkward situation occurs that dependencies |
|
1000 |
between definitions introduce cyclic dependencies |
|
1001 |
between modules, which in the Haskell world leaves |
|
1002 |
you to the mercy of the Haskell implementation you are using, |
|
1003 |
while for SML code generation is not possible. |
|
21178 | 1004 |
|
21189 | 1005 |
A solution is to declare module names explicitly. |
1006 |
Let use assume the three cyclically dependent |
|
1007 |
modules are named \emph{A}, \emph{B} and \emph{C}. |
|
1008 |
Then, by stating |
|
1009 |
*} |
|
1010 |
||
1011 |
code_modulename SML |
|
1012 |
A ABC |
|
1013 |
B ABC |
|
1014 |
C ABC |
|
1015 |
||
1016 |
text {* |
|
1017 |
we explicitly map all those modules on \emph{ABC}, |
|
1018 |
resulting in an ad-hoc merge of this three modules |
|
1019 |
at serialization time. |
|
1020 |
*} |
|
21147 | 1021 |
|
1022 |
subsection {* Axiomatic extensions *} |
|
1023 |
||
1024 |
text {* |
|
1025 |
\begin{warn} |
|
1026 |
The extensions introduced in this section, though working |
|
21189 | 1027 |
in practice, are not the cream of the crop, as you |
1028 |
will notice during reading. They will |
|
21147 | 1029 |
eventually be replaced by more mature approaches. |
1030 |
\end{warn} |
|
21189 | 1031 |
|
1032 |
Sometimes equalities are taken for granted which are |
|
1033 |
not derivable inside the HOL logic but are silently assumed |
|
1034 |
to hold for executable code. For example, we may want |
|
1035 |
to identify the famous HOL constant @{const arbitrary} |
|
1036 |
of type @{typ "'a option"} with @{const None}. |
|
1037 |
By brute force: |
|
1038 |
*} |
|
1039 |
||
21323 | 1040 |
axiomatization where |
1041 |
"arbitrary = None" |
|
21189 | 1042 |
|
1043 |
text {* |
|
1044 |
However this has to be considered harmful since this axiom, |
|
1045 |
though probably justifiable for generated code, could |
|
1046 |
introduce serious inconsistencies into the logic. |
|
1047 |
||
1048 |
So, there is a distinguished construct for stating axiomatic |
|
1049 |
equalities of constants which apply only for code generation. |
|
1050 |
Before introducing this, here is a convenient place to describe |
|
1051 |
shortly how to deal with some restrictions the type discipline |
|
1052 |
imposes. |
|
1053 |
||
1054 |
By itself, the constant @{const arbitrary} is a non-overloaded |
|
1055 |
polymorphic constant. So, there is no way to distinguish |
|
1056 |
different versions of @{const arbitrary} for different types |
|
1057 |
inside the code generator framework. However, inlining |
|
1058 |
theorems together with auxiliary constants provide a solution: |
|
21147 | 1059 |
*} |
1060 |
||
21189 | 1061 |
definition |
21993 | 1062 |
arbitrary_option :: "'a option" where |
21189 | 1063 |
[symmetric, code inline]: "arbitrary_option = arbitrary" |
1064 |
||
1065 |
text {* |
|
1066 |
By that, we replace any @{const arbitrary} with option type |
|
22060 | 1067 |
by @{const arbitrary_option} in defining equations. |
21189 | 1068 |
|
1069 |
For technical reasons, we further have to provide a |
|
1070 |
synonym for @{const None} which in code generator view |
|
22175 | 1071 |
is a function rather than a term constructor: |
21189 | 1072 |
*} |
1073 |
||
1074 |
definition |
|
1075 |
"None' = None" |
|
1076 |
||
1077 |
text {* |
|
21323 | 1078 |
Then finally we are enabled to use @{text "\<CODEAXIOMS>"}: |
21189 | 1079 |
*} |
1080 |
||
1081 |
code_axioms |
|
1082 |
arbitrary_option \<equiv> None' |
|
1083 |
||
1084 |
text {* |
|
1085 |
A dummy example: |
|
1086 |
*} |
|
1087 |
||
1088 |
fun |
|
1089 |
dummy_option :: "'a list \<Rightarrow> 'a option" where |
|
1090 |
"dummy_option (x#xs) = Some x" |
|
1091 |
"dummy_option [] = arbitrary" |
|
1092 |
(*<*) |
|
1093 |
declare dummy_option.simps [code func] |
|
1094 |
(*>*) |
|
1095 |
||
21545 | 1096 |
code_gen dummy_option (*<*)(SML #)(*>*)(SML "examples/arbitrary.ML") |
21189 | 1097 |
|
1098 |
text {* |
|
1099 |
\lstsml{Thy/examples/arbitrary.ML} |
|
1100 |
||
1101 |
Another axiomatic extension is code generation |
|
1102 |
for abstracted types. For this, the |
|
21452 | 1103 |
@{text "ExecutableRat"} (see \secref{exec_rat}) |
21189 | 1104 |
forms a good example. |
1105 |
*} |
|
1106 |
||
20948 | 1107 |
|
21058 | 1108 |
section {* ML interfaces \label{sec:ml} *} |
20948 | 1109 |
|
21189 | 1110 |
text {* |
1111 |
Since the code generator framework not only aims to provide |
|
1112 |
a nice Isar interface but also to form a base for |
|
1113 |
code-generation-based applications, here a short |
|
1114 |
description of the most important ML interfaces. |
|
1115 |
*} |
|
1116 |
||
21147 | 1117 |
subsection {* Constants with type discipline: codegen\_consts.ML *} |
1118 |
||
21189 | 1119 |
text {* |
1120 |
This Pure module manages identification of (probably overloaded) |
|
1121 |
constants by unique identifiers. |
|
1122 |
*} |
|
1123 |
||
21147 | 1124 |
text %mlref {* |
1125 |
\begin{mldecls} |
|
21323 | 1126 |
@{index_ML_type CodegenConsts.const: "string * typ list"} \\ |
21189 | 1127 |
@{index_ML CodegenConsts.norm_of_typ: "theory -> string * typ -> CodegenConsts.const"} \\ |
21147 | 1128 |
@{index_ML CodegenConsts.typ_of_inst: "theory -> CodegenConsts.const -> string * typ"} \\ |
21189 | 1129 |
\end{mldecls} |
1130 |
||
1131 |
\begin{description} |
|
1132 |
||
1133 |
\item @{ML_type CodegenConsts.const} is the identifier type: |
|
1134 |
the product of a \emph{string} with a list of \emph{typs}. |
|
1135 |
The \emph{string} is the constant name as represented inside Isabelle; |
|
21223 | 1136 |
the \emph{typs} are a type instantiation in the sense of System F, |
21189 | 1137 |
with canonical names for type variables. |
1138 |
||
1139 |
\item @{ML CodegenConsts.norm_of_typ}~@{text thy}~@{text "(constname, typ)"} |
|
1140 |
maps a constant expression @{text "(constname, typ)"} to its canonical identifier. |
|
1141 |
||
1142 |
\item @{ML CodegenConsts.typ_of_inst}~@{text thy}~@{text const} |
|
1143 |
maps a canonical identifier @{text const} to a constant |
|
1144 |
expression with appropriate type. |
|
1145 |
||
1146 |
\end{description} |
|
21147 | 1147 |
*} |
1148 |
||
1149 |
subsection {* Executable theory content: codegen\_data.ML *} |
|
1150 |
||
1151 |
text {* |
|
1152 |
This Pure module implements the core notions of |
|
1153 |
executable content of a theory. |
|
1154 |
*} |
|
1155 |
||
1156 |
subsubsection {* Suspended theorems *} |
|
1157 |
||
1158 |
text %mlref {* |
|
1159 |
\begin{mldecls} |
|
21341 | 1160 |
@{index_ML CodegenData.lazy: "(unit -> thm list) -> thm list Susp.T"} |
21147 | 1161 |
\end{mldecls} |
21189 | 1162 |
|
1163 |
\begin{description} |
|
1164 |
||
1165 |
\item @{ML CodegenData.lazy}~@{text f} turns an abstract |
|
21323 | 1166 |
theorem computation @{text f} into a suspension of theorems. |
21189 | 1167 |
|
1168 |
\end{description} |
|
21147 | 1169 |
*} |
1170 |
||
1171 |
subsubsection {* Executable content *} |
|
20948 | 1172 |
|
21147 | 1173 |
text %mlref {* |
1174 |
\begin{mldecls} |
|
1175 |
@{index_ML CodegenData.add_func: "thm -> theory -> theory"} \\ |
|
1176 |
@{index_ML CodegenData.del_func: "thm -> theory -> theory"} \\ |
|
21341 | 1177 |
@{index_ML CodegenData.add_funcl: "CodegenConsts.const * thm list Susp.T -> theory -> theory"} \\ |
21147 | 1178 |
@{index_ML CodegenData.add_inline: "thm -> theory -> theory"} \\ |
1179 |
@{index_ML CodegenData.del_inline: "thm -> theory -> theory"} \\ |
|
22046 | 1180 |
@{index_ML CodegenData.add_inline_proc: "string * (theory -> cterm list -> thm list) |
21189 | 1181 |
-> theory -> theory"} \\ |
22046 | 1182 |
@{index_ML CodegenData.del_inline_proc: "string -> theory -> theory"} \\ |
1183 |
@{index_ML CodegenData.add_preproc: "string * (theory -> thm list -> thm list) |
|
21189 | 1184 |
-> theory -> theory"} \\ |
22046 | 1185 |
@{index_ML CodegenData.del_preproc: "string -> theory -> theory"} \\ |
21189 | 1186 |
@{index_ML CodegenData.add_datatype: "string * (((string * sort) list * (string * typ list) list) |
21341 | 1187 |
* thm list Susp.T) -> theory -> theory"} \\ |
21189 | 1188 |
@{index_ML CodegenData.del_datatype: "string -> theory -> theory"} \\ |
1189 |
@{index_ML CodegenData.get_datatype: "theory -> string |
|
1190 |
-> ((string * sort) list * (string * typ list) list) option"} \\ |
|
21147 | 1191 |
@{index_ML CodegenData.get_datatype_of_constr: "theory -> CodegenConsts.const -> string option"} |
1192 |
\end{mldecls} |
|
1193 |
||
1194 |
\begin{description} |
|
1195 |
||
21189 | 1196 |
\item @{ML CodegenData.add_func}~@{text "thm"}~@{text "thy"} adds function |
1197 |
theorem @{text "thm"} to executable content. |
|
1198 |
||
1199 |
\item @{ML CodegenData.del_func}~@{text "thm"}~@{text "thy"} removes function |
|
1200 |
theorem @{text "thm"} from executable content, if present. |
|
1201 |
||
1202 |
\item @{ML CodegenData.add_funcl}~@{text "(const, lthms)"}~@{text "thy"} adds |
|
22060 | 1203 |
suspended defining equations @{text lthms} for constant |
21189 | 1204 |
@{text const} to executable content. |
1205 |
||
1206 |
\item @{ML CodegenData.add_inline}~@{text "thm"}~@{text "thy"} adds |
|
21223 | 1207 |
inlining theorem @{text thm} to executable content. |
21189 | 1208 |
|
1209 |
\item @{ML CodegenData.del_inline}~@{text "thm"}~@{text "thy"} remove |
|
1210 |
inlining theorem @{text thm} from executable content, if present. |
|
1211 |
||
22046 | 1212 |
\item @{ML CodegenData.add_inline_proc}~@{text "(name, f)"}~@{text "thy"} adds |
1213 |
inline procedure @{text f} (named @{text name}) to executable content; |
|
21189 | 1214 |
@{text f} is a computation of rewrite rules dependent on |
1215 |
the current theory context and the list of all arguments |
|
22060 | 1216 |
and right hand sides of the defining equations belonging |
21189 | 1217 |
to a certain function definition. |
1218 |
||
22046 | 1219 |
\item @{ML CodegenData.del_inline_proc}~@{text "name"}~@{text "thy"} removes |
1220 |
inline procedure named @{text name} from executable content. |
|
1221 |
||
1222 |
\item @{ML CodegenData.add_preproc}~@{text "(name, f)"}~@{text "thy"} adds |
|
1223 |
generic preprocessor @{text f} (named @{text name}) to executable content; |
|
22060 | 1224 |
@{text f} is a transformation of the defining equations belonging |
21189 | 1225 |
to a certain function definition, depending on the |
1226 |
current theory context. |
|
1227 |
||
22060 | 1228 |
\item @{ML CodegenData.del_preproc}~@{text "name"}~@{text "thy"} removes |
22046 | 1229 |
generic preprcoessor named @{text name} from executable content. |
1230 |
||
21189 | 1231 |
\item @{ML CodegenData.add_datatype}~@{text "(name, (spec, cert))"}~@{text "thy"} adds |
1232 |
a datatype to executable content, with type constructor |
|
1233 |
@{text name} and specification @{text spec}; @{text spec} is |
|
1234 |
a pair consisting of a list of type variable with sort |
|
21223 | 1235 |
constraints and a list of constructors with name |
21189 | 1236 |
and types of arguments. The addition as datatype |
1237 |
has to be justified giving a certificate of suspended |
|
21223 | 1238 |
theorems as witnesses for injectiveness and distinctness. |
21189 | 1239 |
|
1240 |
\item @{ML CodegenData.del_datatype}~@{text "name"}~@{text "thy"} |
|
1241 |
remove a datatype from executable content, if present. |
|
1242 |
||
1243 |
\item @{ML CodegenData.get_datatype_of_constr}~@{text "thy"}~@{text "const"} |
|
1244 |
returns type constructor corresponding to |
|
1245 |
constructor @{text const}; returns @{text NONE} |
|
1246 |
if @{text const} is no constructor. |
|
21147 | 1247 |
|
1248 |
\end{description} |
|
1249 |
*} |
|
1250 |
||
22060 | 1251 |
subsection {* defining equation systems: codegen\_funcgr.ML *} |
21189 | 1252 |
|
1253 |
text {* |
|
21217 | 1254 |
Out of the executable content of a theory, a normalized |
22060 | 1255 |
defining equation systems may be constructed containing |
21217 | 1256 |
function definitions for constants. The system is cached |
1257 |
until its underlying executable content changes. |
|
21189 | 1258 |
*} |
1259 |
||
1260 |
text %mlref {* |
|
1261 |
\begin{mldecls} |
|
1262 |
@{index_ML_type CodegenFuncgr.T} \\ |
|
1263 |
@{index_ML CodegenFuncgr.make: "theory -> CodegenConsts.const list -> CodegenFuncgr.T"} \\ |
|
1264 |
@{index_ML CodegenFuncgr.funcs: "CodegenFuncgr.T -> CodegenConsts.const -> thm list"} \\ |
|
1265 |
@{index_ML CodegenFuncgr.typ: "CodegenFuncgr.T -> CodegenConsts.const -> typ"} \\ |
|
1266 |
@{index_ML CodegenFuncgr.deps: "CodegenFuncgr.T |
|
1267 |
-> CodegenConsts.const list -> CodegenConsts.const list list"} \\ |
|
1268 |
@{index_ML CodegenFuncgr.all: "CodegenFuncgr.T -> CodegenConsts.const list"} |
|
1269 |
\end{mldecls} |
|
21217 | 1270 |
|
1271 |
\begin{description} |
|
1272 |
||
1273 |
\item @{ML_type CodegenFuncgr.T} represents |
|
22060 | 1274 |
a normalized defining equation system. |
21217 | 1275 |
|
1276 |
\item @{ML CodegenFuncgr.make}~@{text thy}~@{text cs} |
|
22060 | 1277 |
returns a normalized defining equation system, |
21217 | 1278 |
with the assertion that it contains any function |
21223 | 1279 |
definition for constants @{text cs} (if existing). |
21217 | 1280 |
|
1281 |
\item @{ML CodegenFuncgr.funcs}~@{text funcgr}~@{text c} |
|
1282 |
retrieves function definition for constant @{text c}. |
|
1283 |
||
1284 |
\item @{ML CodegenFuncgr.typ}~@{text funcgr}~@{text c} |
|
1285 |
retrieves function type for constant @{text c}. |
|
1286 |
||
1287 |
\item @{ML CodegenFuncgr.deps}~@{text funcgr}~@{text cs} |
|
1288 |
returns the transitive closure of dependencies for |
|
1289 |
constants @{text cs} as a partitioning where each partition |
|
1290 |
corresponds to a strongly connected component of |
|
1291 |
dependencies and any partition does \emph{not} |
|
1292 |
depend on partitions further left. |
|
1293 |
||
1294 |
\item @{ML CodegenFuncgr.all}~@{text funcgr} |
|
1295 |
returns all currently represented constants. |
|
1296 |
||
1297 |
\end{description} |
|
21189 | 1298 |
*} |
1299 |
||
21147 | 1300 |
subsection {* Further auxiliary *} |
1301 |
||
1302 |
text %mlref {* |
|
1303 |
\begin{mldecls} |
|
1304 |
@{index_ML CodegenConsts.const_ord: "CodegenConsts.const * CodegenConsts.const -> order"} \\ |
|
1305 |
@{index_ML CodegenConsts.eq_const: "CodegenConsts.const * CodegenConsts.const -> bool"} \\ |
|
1306 |
@{index_ML CodegenConsts.consts_of: "theory -> term -> CodegenConsts.const list"} \\ |
|
1307 |
@{index_ML CodegenConsts.read_const: "theory -> string -> CodegenConsts.const"} \\ |
|
1308 |
@{index_ML_structure CodegenConsts.Consttab} \\ |
|
21189 | 1309 |
@{index_ML_structure CodegenFuncgr.Constgraph} \\ |
22060 | 1310 |
@{index_ML CodegenFunc.typ_func: "thm -> typ"} \\ |
1311 |
@{index_ML CodegenFunc.rewrite_func: "thm list -> thm -> thm"} \\ |
|
21147 | 1312 |
\end{mldecls} |
21217 | 1313 |
|
1314 |
\begin{description} |
|
1315 |
||
1316 |
\item @{ML CodegenConsts.const_ord},~@{ML CodegenConsts.eq_const} |
|
1317 |
provide order and equality on constant identifiers. |
|
1318 |
||
1319 |
\item @{ML_struct CodegenConsts.Consttab},~@{ML_struct CodegenFuncgr.Constgraph} |
|
1320 |
provide advanced data structures with constant identifiers as keys. |
|
1321 |
||
1322 |
\item @{ML CodegenConsts.consts_of}~@{text thy}~@{text t} |
|
1323 |
returns all constant identifiers mentioned in a term @{text t}. |
|
1324 |
||
1325 |
\item @{ML CodegenConsts.read_const}~@{text thy}~@{text s} |
|
1326 |
reads a constant as a concrete term expression @{text s}. |
|
1327 |
||
22060 | 1328 |
\item @{ML CodegenFunc.typ_func}~@{text thm} |
1329 |
extracts the type of a constant in a defining equation @{text thm}. |
|
21217 | 1330 |
|
22060 | 1331 |
\item @{ML CodegenFunc.rewrite_func}~@{text rews}~@{text thm} |
1332 |
rewrites a defining equation @{text thm} with a set of rewrite |
|
21217 | 1333 |
rules @{text rews}; only arguments and right hand side are rewritten, |
22060 | 1334 |
not the head of the defining equation. |
21217 | 1335 |
|
1336 |
\end{description} |
|
1337 |
||
21147 | 1338 |
*} |
20948 | 1339 |
|
1340 |
subsection {* Implementing code generator applications *} |
|
1341 |
||
21147 | 1342 |
text {* |
21217 | 1343 |
Implementing code generator applications on top |
1344 |
of the framework set out so far usually not only |
|
1345 |
involves using those primitive interfaces |
|
1346 |
but also storing code-dependent data and various |
|
1347 |
other things. |
|
1348 |
||
21147 | 1349 |
\begin{warn} |
1350 |
Some interfaces discussed here have not reached |
|
1351 |
a final state yet. |
|
1352 |
Changes likely to occur in future. |
|
1353 |
\end{warn} |
|
1354 |
*} |
|
1355 |
||
1356 |
subsubsection {* Data depending on the theory's executable content *} |
|
1357 |
||
21217 | 1358 |
text {* |
21452 | 1359 |
Due to incrementality of code generation, changes in the |
1360 |
theory's executable content have to be propagated in a |
|
1361 |
certain fashion. Additionally, such changes may occur |
|
1362 |
not only during theory extension but also during theory |
|
1363 |
merge, which is a little bit nasty from an implementation |
|
1364 |
point of view. The framework provides a solution |
|
1365 |
to this technical challenge by providing a functorial |
|
1366 |
data slot @{ML_functor CodeDataFun}; on instantiation |
|
1367 |
of this functor, the following types and operations |
|
1368 |
are required: |
|
1369 |
||
21217 | 1370 |
\medskip |
1371 |
\begin{tabular}{l} |
|
1372 |
@{text "val name: string"} \\ |
|
1373 |
@{text "type T"} \\ |
|
1374 |
@{text "val empty: T"} \\ |
|
1375 |
@{text "val merge: Pretty.pp \<rightarrow> T * T \<rightarrow> T"} \\ |
|
1376 |
@{text "val purge: theory option \<rightarrow> CodegenConsts.const list option \<rightarrow> T \<rightarrow> T"} |
|
1377 |
\end{tabular} |
|
1378 |
||
21452 | 1379 |
\begin{description} |
1380 |
||
1381 |
\item @{text name} is a system-wide unique name identifying the data. |
|
1382 |
||
1383 |
\item @{text T} the type of data to store. |
|
1384 |
||
1385 |
\item @{text empty} initial (empty) data. |
|
1386 |
||
1387 |
\item @{text merge} merging two data slots. |
|
1388 |
||
1389 |
\item @{text purge}~@{text thy}~@{text cs} propagates changes in executable content; |
|
1390 |
if possible, the current theory context is handed over |
|
1391 |
as argument @{text thy} (if there is no current theory context (e.g.~during |
|
1392 |
theory merge, @{ML NONE}); @{text cs} indicates the kind |
|
1393 |
of change: @{ML NONE} stands for a fundamental change |
|
1394 |
which invalidates any existing code, @{text "SOME cs"} |
|
1395 |
hints that executable content for constants @{text cs} |
|
1396 |
has changed. |
|
1397 |
||
1398 |
\end{description} |
|
1399 |
||
1400 |
An instance of @{ML_functor CodeDataFun} provides the following |
|
1401 |
interface: |
|
1402 |
||
21217 | 1403 |
\medskip |
1404 |
\begin{tabular}{l} |
|
1405 |
@{text "init: theory \<rightarrow> theory"} \\ |
|
1406 |
@{text "get: theory \<rightarrow> T"} \\ |
|
1407 |
@{text "change: theory \<rightarrow> (T \<rightarrow> T) \<rightarrow> T"} \\ |
|
1408 |
@{text "change_yield: theory \<rightarrow> (T \<rightarrow> 'a * T) \<rightarrow> 'a * T"} |
|
1409 |
\end{tabular} |
|
1410 |
||
1411 |
\begin{description} |
|
1412 |
||
21452 | 1413 |
\item @{text init} initialization during theory setup. |
1414 |
||
1415 |
\item @{text get} retrieval of the current data. |
|
1416 |
||
1417 |
\item @{text change} update of current data (cached!) |
|
1418 |
by giving a continuation. |
|
1419 |
||
1420 |
\item @{text change_yield} update with side result. |
|
21217 | 1421 |
|
1422 |
\end{description} |
|
1423 |
*} |
|
1424 |
||
21147 | 1425 |
subsubsection {* Datatype hooks *} |
1426 |
||
21452 | 1427 |
text {* |
1428 |
Isabelle/HOL's datatype package provides a mechanism to |
|
1429 |
extend theories depending on datatype declarations: |
|
1430 |
\emph{datatype hooks}. For example, when declaring a new |
|
22060 | 1431 |
datatype, a hook proves defining equations for equality on |
21452 | 1432 |
that datatype (if possible). |
1433 |
*} |
|
1434 |
||
21217 | 1435 |
text %mlref {* |
1436 |
\begin{mldecls} |
|
21323 | 1437 |
@{index_ML_type DatatypeHooks.hook: "string list -> theory -> theory"} \\ |
21217 | 1438 |
@{index_ML DatatypeHooks.add: "DatatypeHooks.hook -> theory -> theory"} |
1439 |
\end{mldecls} |
|
21452 | 1440 |
|
1441 |
\begin{description} |
|
1442 |
||
1443 |
\item @{ML_type DatatypeHooks.hook} specifies the interface |
|
1444 |
of \emph{datatype hooks}: a theory update |
|
1445 |
depending on the list of newly introduced |
|
1446 |
datatype names. |
|
1447 |
||
1448 |
\item @{ML DatatypeHooks.add} adds a hook to the |
|
1449 |
chain of all hooks. |
|
1450 |
||
1451 |
\end{description} |
|
1452 |
*} |
|
1453 |
||
1454 |
subsubsection {* Trivial typedefs -- type copies *} |
|
1455 |
||
1456 |
text {* |
|
1457 |
Sometimes packages will introduce new types |
|
1458 |
as \emph{marked type copies} similar to Haskell's |
|
1459 |
@{text newtype} declaration (e.g. the HOL record package) |
|
1460 |
\emph{without} tinkering with the overhead of datatypes. |
|
1461 |
Technically, these type copies are trivial forms of typedefs. |
|
1462 |
Since these type copies in code generation view are nothing |
|
1463 |
else than datatypes, they have been given a own package |
|
1464 |
in order to faciliate code generation: |
|
21147 | 1465 |
*} |
21058 | 1466 |
|
21217 | 1467 |
text %mlref {* |
1468 |
\begin{mldecls} |
|
21452 | 1469 |
@{index_ML_type TypecopyPackage.info} \\ |
21217 | 1470 |
@{index_ML TypecopyPackage.add_typecopy: " |
1471 |
bstring * string list -> typ -> (bstring * bstring) option |
|
1472 |
-> theory -> (string * TypecopyPackage.info) * theory"} \\ |
|
1473 |
@{index_ML TypecopyPackage.get_typecopy_info: "theory |
|
1474 |
-> string -> TypecopyPackage.info option"} \\ |
|
1475 |
@{index_ML TypecopyPackage.get_spec: "theory -> string |
|
21452 | 1476 |
-> (string * sort) list * (string * typ list) list"} \\ |
1477 |
@{index_ML_type TypecopyPackage.hook: "string * TypecopyPackage.info -> theory -> theory"} \\ |
|
1478 |
@{index_ML TypecopyPackage.add_hook: |
|
1479 |
"TypecopyPackage.hook -> theory -> theory"} \\ |
|
21217 | 1480 |
\end{mldecls} |
21452 | 1481 |
|
1482 |
\begin{description} |
|
1483 |
||
1484 |
\item @{ML_type TypecopyPackage.info} a record containing |
|
1485 |
the specification and further data of a type copy. |
|
1486 |
||
1487 |
\item @{ML TypecopyPackage.add_typecopy} defines a new |
|
1488 |
type copy. |
|
1489 |
||
1490 |
\item @{ML TypecopyPackage.get_typecopy_info} retrieves |
|
1491 |
data of an existing type copy. |
|
1492 |
||
1493 |
\item @{ML TypecopyPackage.get_spec} retrieves datatype-like |
|
1494 |
specification of a type copy. |
|
1495 |
||
1496 |
\item @{ML_type TypecopyPackage.hook},~@{ML TypecopyPackage.add_hook} |
|
1497 |
provide a hook mechanism corresponding to the hook mechanism |
|
1498 |
on datatypes. |
|
1499 |
||
1500 |
\end{description} |
|
1501 |
*} |
|
1502 |
||
1503 |
subsubsection {* Unifying type copies and datatypes *} |
|
1504 |
||
1505 |
text {* |
|
1506 |
Since datatypes and type copies are mapped to the same concept (datatypes) |
|
1507 |
by code generation, the view on both is unified \qt{code types}: |
|
21217 | 1508 |
*} |
1509 |
||
1510 |
text %mlref {* |
|
1511 |
\begin{mldecls} |
|
21452 | 1512 |
@{index_ML_type DatatypeCodegen.hook: "(string * (bool * ((string * sort) list |
1513 |
* (string * typ list) list))) list |
|
21323 | 1514 |
-> theory -> theory"} \\ |
21217 | 1515 |
@{index_ML DatatypeCodegen.add_codetypes_hook_bootstrap: " |
1516 |
DatatypeCodegen.hook -> theory -> theory"} |
|
1517 |
\end{mldecls} |
|
1518 |
*} |
|
1519 |
||
21222 | 1520 |
text {* |
21452 | 1521 |
\begin{description} |
1522 |
||
1523 |
\item @{ML_type DatatypeCodegen.hook} specifies the code type hook |
|
1524 |
interface: a theory transformation depending on a list of |
|
1525 |
mutual recursive code types; each entry in the list |
|
1526 |
has the structure @{text "(name, (is_data, (vars, cons)))"} |
|
1527 |
where @{text name} is the name of the code type, @{text is_data} |
|
1528 |
is true iff @{text name} is a datatype rather then a type copy, |
|
1529 |
and @{text "(vars, cons)"} is the specification of the code type. |
|
1530 |
||
1531 |
\item @{ML DatatypeCodegen.add_codetypes_hook_bootstrap} adds a code |
|
1532 |
type hook; the hook is immediately processed for all already |
|
1533 |
existing datatypes, in blocks of mutual recursive datatypes, |
|
1534 |
where all datatypes a block depends on are processed before |
|
1535 |
the block. |
|
1536 |
||
1537 |
\end{description} |
|
1538 |
||
1539 |
\emph{Happy proving, happy hacking!} |
|
21222 | 1540 |
*} |
21217 | 1541 |
|
20948 | 1542 |
end |