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