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