equal
deleted
inserted
replaced
8 |
8 |
9 text {* |
9 text {* |
10 We have already seen how by default equations stemming from |
10 We have already seen how by default equations stemming from |
11 @{command definition}/@{command primrec}/@{command fun} |
11 @{command definition}/@{command primrec}/@{command fun} |
12 statements are used for code generation. This default behaviour |
12 statements are used for code generation. This default behaviour |
13 can be changed, e.g. by providing different defining equations. |
13 can be changed, e.g. by providing different code equations. |
14 All kinds of customisation shown in this section is \emph{safe} |
14 All kinds of customisation shown in this section is \emph{safe} |
15 in the sense that the user does not have to worry about |
15 in the sense that the user does not have to worry about |
16 correctness -- all programs generatable that way are partially |
16 correctness -- all programs generatable that way are partially |
17 correct. |
17 correct. |
18 *} |
18 *} |
19 |
19 |
20 subsection {* Selecting code equations *} |
20 subsection {* Selecting code equations *} |
21 |
21 |
22 text {* |
22 text {* |
23 Coming back to our introductory example, we |
23 Coming back to our introductory example, we |
24 could provide an alternative defining equations for @{const dequeue} |
24 could provide an alternative code equations for @{const dequeue} |
25 explicitly: |
25 explicitly: |
26 *} |
26 *} |
27 |
27 |
28 lemma %quote [code]: |
28 lemma %quote [code]: |
29 "dequeue (Queue xs []) = |
29 "dequeue (Queue xs []) = |
34 by (cases xs, simp_all) (cases "rev xs", simp_all) |
34 by (cases xs, simp_all) (cases "rev xs", simp_all) |
35 |
35 |
36 text {* |
36 text {* |
37 \noindent The annotation @{text "[code]"} is an @{text Isar} |
37 \noindent The annotation @{text "[code]"} is an @{text Isar} |
38 @{text attribute} which states that the given theorems should be |
38 @{text attribute} which states that the given theorems should be |
39 considered as defining equations for a @{text fun} statement -- |
39 considered as code equations for a @{text fun} statement -- |
40 the corresponding constant is determined syntactically. The resulting code: |
40 the corresponding constant is determined syntactically. The resulting code: |
41 *} |
41 *} |
42 |
42 |
43 text %quote {*@{code_stmts dequeue (consts) dequeue (Haskell)}*} |
43 text %quote {*@{code_stmts dequeue (consts) dequeue (Haskell)}*} |
44 |
44 |
57 *} |
57 *} |
58 |
58 |
59 code_thms %quote dequeue |
59 code_thms %quote dequeue |
60 |
60 |
61 text {* |
61 text {* |
62 \noindent prints a table with \emph{all} defining equations |
62 \noindent prints a table with \emph{all} code equations |
63 for @{const dequeue}, including |
63 for @{const dequeue}, including |
64 \emph{all} defining equations those equations depend |
64 \emph{all} code equations those equations depend |
65 on recursively. |
65 on recursively. |
66 |
66 |
67 Similarly, the @{command code_deps} command shows a graph |
67 Similarly, the @{command code_deps} command shows a graph |
68 visualising dependencies between defining equations. |
68 visualising dependencies between code equations. |
69 *} |
69 *} |
70 |
70 |
71 subsection {* @{text class} and @{text instantiation} *} |
71 subsection {* @{text class} and @{text instantiation} *} |
72 |
72 |
73 text {* |
73 text {* |
153 out: \emph{preprocessing}. In essence, the preprocessor |
153 out: \emph{preprocessing}. In essence, the preprocessor |
154 consists of two components: a \emph{simpset} and \emph{function transformers}. |
154 consists of two components: a \emph{simpset} and \emph{function transformers}. |
155 |
155 |
156 The \emph{simpset} allows to employ the full generality of the Isabelle |
156 The \emph{simpset} allows to employ the full generality of the Isabelle |
157 simplifier. Due to the interpretation of theorems |
157 simplifier. Due to the interpretation of theorems |
158 as defining equations, rewrites are applied to the right |
158 as code equations, rewrites are applied to the right |
159 hand side and the arguments of the left hand side of an |
159 hand side and the arguments of the left hand side of an |
160 equation, but never to the constant heading the left hand side. |
160 equation, but never to the constant heading the left hand side. |
161 An important special case are \emph{inline theorems} which may be |
161 An important special case are \emph{inline theorems} which may be |
162 declared and undeclared using the |
162 declared and undeclared using the |
163 \emph{code inline} or \emph{code inline del} attribute respectively. |
163 \emph{code inline} or \emph{code inline del} attribute respectively. |
205 |
205 |
206 \noindent The current setup of the preprocessor may be inspected using |
206 \noindent The current setup of the preprocessor may be inspected using |
207 the @{command print_codesetup} command. |
207 the @{command print_codesetup} command. |
208 @{command code_thms} provides a convenient |
208 @{command code_thms} provides a convenient |
209 mechanism to inspect the impact of a preprocessor setup |
209 mechanism to inspect the impact of a preprocessor setup |
210 on defining equations. |
210 on code equations. |
211 |
211 |
212 \begin{warn} |
212 \begin{warn} |
213 The attribute \emph{code unfold} |
213 The attribute \emph{code unfold} |
214 associated with the @{text "SML code generator"} also applies to |
214 associated with the @{text "SML code generator"} also applies to |
215 the @{text "generic code generator"}: |
215 the @{text "generic code generator"}: |
349 \noindent Obviously, polymorphic equality is implemented the Haskell |
349 \noindent Obviously, polymorphic equality is implemented the Haskell |
350 way using a type class. How is this achieved? HOL introduces |
350 way using a type class. How is this achieved? HOL introduces |
351 an explicit class @{class eq} with a corresponding operation |
351 an explicit class @{class eq} with a corresponding operation |
352 @{const eq_class.eq} such that @{thm eq [no_vars]}. |
352 @{const eq_class.eq} such that @{thm eq [no_vars]}. |
353 The preprocessing framework does the rest by propagating the |
353 The preprocessing framework does the rest by propagating the |
354 @{class eq} constraints through all dependent defining equations. |
354 @{class eq} constraints through all dependent code equations. |
355 For datatypes, instances of @{class eq} are implicitly derived |
355 For datatypes, instances of @{class eq} are implicitly derived |
356 when possible. For other types, you may instantiate @{text eq} |
356 when possible. For other types, you may instantiate @{text eq} |
357 manually like any other type class. |
357 manually like any other type class. |
358 |
358 |
359 Though this @{text eq} class is designed to get rarely in |
359 Though this @{text eq} class is designed to get rarely in |
408 *} |
408 *} |
409 |
409 |
410 text %quote {*@{code_stmts "op \<le> \<Colon> _ \<times> _ \<Rightarrow> _ \<times> _ \<Rightarrow> bool" (SML)}*} |
410 text %quote {*@{code_stmts "op \<le> \<Colon> _ \<times> _ \<Rightarrow> _ \<times> _ \<Rightarrow> bool" (SML)}*} |
411 |
411 |
412 text {* |
412 text {* |
413 In some cases, the automatically derived defining equations |
413 In some cases, the automatically derived code equations |
414 for equality on a particular type may not be appropriate. |
414 for equality on a particular type may not be appropriate. |
415 As example, watch the following datatype representing |
415 As example, watch the following datatype representing |
416 monomorphic parametric types (where type constructors |
416 monomorphic parametric types (where type constructors |
417 are referred to by natural numbers): |
417 are referred to by natural numbers): |
418 *} |
418 *} |
491 @{const strict_dequeue'} is more involved since it requires |
491 @{const strict_dequeue'} is more involved since it requires |
492 a manual termination proof. Apart from that observe that |
492 a manual termination proof. Apart from that observe that |
493 on the right hand side of its first equation the constant |
493 on the right hand side of its first equation the constant |
494 @{const empty_queue} occurs which is unspecified. |
494 @{const empty_queue} occurs which is unspecified. |
495 |
495 |
496 Normally, if constants without any defining equations occur in |
496 Normally, if constants without any code equations occur in |
497 a program, the code generator complains (since in most cases |
497 a program, the code generator complains (since in most cases |
498 this is not what the user expects). But such constants can also |
498 this is not what the user expects). But such constants can also |
499 be thought of as function definitions with no equations which |
499 be thought of as function definitions with no equations which |
500 always fail, since there is never a successful pattern match |
500 always fail, since there is never a successful pattern match |
501 on the left hand side. In order to categorise a constant into |
501 on the left hand side. In order to categorise a constant into |