7 subsection {* Further reading *} |
7 subsection {* Further reading *} |
8 |
8 |
9 text {* |
9 text {* |
10 Do dive deeper into the issue of code generation, you should visit |
10 Do dive deeper into the issue of code generation, you should visit |
11 the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} which |
11 the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref} which |
12 constains exhaustive syntax diagrams. |
12 contains exhaustive syntax diagrams. |
13 *} |
13 *} |
14 |
14 |
15 subsection {* Modules *} |
15 subsection {* Modules *} |
16 |
16 |
17 text {* |
17 text {* |
42 at serialisation time. |
42 at serialisation time. |
43 *} |
43 *} |
44 |
44 |
45 subsection {* Evaluation oracle *} |
45 subsection {* Evaluation oracle *} |
46 |
46 |
|
47 text {* |
|
48 Code generation may also be used to \emph{evaluate} expressions |
|
49 (using @{text SML} as target language of course). |
|
50 For instance, the @{command value} allows to reduce an expression to a |
|
51 normal form with respect to the underlying code equations: |
|
52 *} |
|
53 |
|
54 value %quote "42 / (12 :: rat)" |
|
55 |
|
56 text {* |
|
57 \noindent will display @{term "7 / (2 :: rat)"}. |
|
58 |
|
59 The @{method eval} method tries to reduce a goal by code generation to @{term True} |
|
60 and solves it in that case, but fails otherwise: |
|
61 *} |
|
62 |
|
63 lemma %quote "42 / (12 :: rat) = 7 / 2" |
|
64 by %quote eval |
|
65 |
|
66 text {* |
|
67 \noindent The soundness of the @{method eval} method depends crucially |
|
68 on the correctness of the code generator; this is one of the reasons |
|
69 why you should not use adaption (see \secref{sec:adaption}) frivolously. |
|
70 *} |
|
71 |
47 subsection {* Code antiquotation *} |
72 subsection {* Code antiquotation *} |
48 |
73 |
49 subsection {* Creating new targets *} |
74 text {* |
|
75 In scenarios involving techniques like reflection it is quite common |
|
76 that code generated from a theory forms the basis for implementing |
|
77 a proof procedure in @{text SML}. To facilitate interfacing of generated code |
|
78 with system code, the code generator provides a @{text code} antiquotation: |
|
79 *} |
50 |
80 |
51 text {* extending targets, adding targets *} |
81 datatype %quote form = T | F | And form form | Or form form |
|
82 |
|
83 ML %quote {* |
|
84 fun eval_form @{code T} = true |
|
85 | eval_form @{code F} = false |
|
86 | eval_form (@{code And} (p, q)) = |
|
87 eval_form p andalso eval_form q |
|
88 | eval_form (@{code Or} (p, q)) = |
|
89 eval_form p orelse eval_form q; |
|
90 *} |
|
91 |
|
92 text {* |
|
93 \noindent @{text code} takes as argument the name of a constant; after the |
|
94 whole @{text SML} is read, the necessary code is generated transparently |
|
95 and the corresponding constant names are inserted. This technique also |
|
96 allows to use pattern matching on constructors stemming from compiled |
|
97 @{text datatypes}. |
|
98 |
|
99 For a less simplistic example, theory @{theory ReflectedFerrack} is |
|
100 a good reference. |
|
101 *} |
52 |
102 |
53 subsection {* Imperative data structures *} |
103 subsection {* Imperative data structures *} |
54 |
104 |
|
105 text {* |
|
106 If you consider imperative data structures as inevitable for a specific |
|
107 application, you should consider |
|
108 \emph{Imperative Functional Programming with Isabelle/HOL} |
|
109 (\cite{bulwahn-et-al:2008:imperative}); |
|
110 the framework described there is available in theory @{theory Imperative_HOL}. |
|
111 *} |
|
112 |
55 end |
113 end |