author | wenzelm |
Fri, 07 Dec 2018 21:42:08 +0100 | |
changeset 69422 | 472af2d7835d |
parent 68484 | 59793df7f853 |
child 69505 | cc2d676d5395 |
permissions | -rw-r--r-- |
31050 | 1 |
theory Adaptation |
69422
472af2d7835d
clarified session dependencies: faster build_doc/build_release;
wenzelm
parents:
68484
diff
changeset
|
2 |
imports Setup |
28213 | 3 |
begin |
4 |
||
59377 | 5 |
setup %invisible \<open>Code_Target.add_derived_target ("\<SML>", [("SML", I)]) |
6 |
#> Code_Target.add_derived_target ("\<SMLdummy>", [("Haskell", I)])\<close> |
|
28561 | 7 |
|
59377 | 8 |
section \<open>Adaptation to target languages \label{sec:adaptation}\<close> |
28419 | 9 |
|
59377 | 10 |
subsection \<open>Adapting code generation\<close> |
28561 | 11 |
|
59377 | 12 |
text \<open> |
28561 | 13 |
The aspects of code generation introduced so far have two aspects |
14 |
in common: |
|
15 |
||
16 |
\begin{itemize} |
|
38450 | 17 |
|
18 |
\item They act uniformly, without reference to a specific target |
|
19 |
language. |
|
20 |
||
28561 | 21 |
\item They are \emph{safe} in the sense that as long as you trust |
22 |
the code generator meta theory and implementation, you cannot |
|
38450 | 23 |
produce programs that yield results which are not derivable in |
24 |
the logic. |
|
25 |
||
28561 | 26 |
\end{itemize} |
27 |
||
38450 | 28 |
\noindent In this section we will introduce means to \emph{adapt} |
29 |
the serialiser to a specific target language, i.e.~to print program |
|
30 |
fragments in a way which accommodates \qt{already existing} |
|
31 |
ingredients of a target language environment, for three reasons: |
|
28561 | 32 |
|
33 |
\begin{itemize} |
|
28593 | 34 |
\item improving readability and aesthetics of generated code |
28561 | 35 |
\item gaining efficiency |
36 |
\item interface with language parts which have no direct counterpart |
|
37 |
in @{text "HOL"} (say, imperative data structures) |
|
38 |
\end{itemize} |
|
39 |
||
40 |
\noindent Generally, you should avoid using those features yourself |
|
41 |
\emph{at any cost}: |
|
42 |
||
43 |
\begin{itemize} |
|
38450 | 44 |
|
45 |
\item The safe configuration methods act uniformly on every target |
|
46 |
language, whereas for adaptation you have to treat each target |
|
47 |
language separately. |
|
48 |
||
49 |
\item Application is extremely tedious since there is no |
|
50 |
abstraction which would allow for a static check, making it easy |
|
51 |
to produce garbage. |
|
52 |
||
34155 | 53 |
\item Subtle errors can be introduced unconsciously. |
38450 | 54 |
|
28561 | 55 |
\end{itemize} |
56 |
||
38450 | 57 |
\noindent However, even if you ought refrain from setting up |
51162 | 58 |
adaptation yourself, already @{text "HOL"} comes with some |
38450 | 59 |
reasonable default adaptations (say, using target language list |
60 |
syntax). There also some common adaptation cases which you can |
|
61 |
setup by importing particular library theories. In order to |
|
62 |
understand these, we provide some clues here; these however are not |
|
63 |
supposed to replace a careful study of the sources. |
|
59377 | 64 |
\<close> |
28561 | 65 |
|
38450 | 66 |
|
59377 | 67 |
subsection \<open>The adaptation principle\<close> |
28561 | 68 |
|
59377 | 69 |
text \<open> |
38450 | 70 |
Figure \ref{fig:adaptation} illustrates what \qt{adaptation} is |
71 |
conceptually supposed to be: |
|
28601 | 72 |
|
60768 | 73 |
\begin{figure}[h] |
52742 | 74 |
\begin{tikzpicture}[scale = 0.5] |
75 |
\tikzstyle water=[color = blue, thick] |
|
76 |
\tikzstyle ice=[color = black, very thick, cap = round, join = round, fill = white] |
|
77 |
\tikzstyle process=[color = green, semithick, ->] |
|
78 |
\tikzstyle adaptation=[color = red, semithick, ->] |
|
79 |
\tikzstyle target=[color = black] |
|
80 |
\foreach \x in {0, ..., 24} |
|
81 |
\draw[style=water] (\x, 0.25) sin + (0.25, 0.25) cos + (0.25, -0.25) sin |
|
82 |
+ (0.25, -0.25) cos + (0.25, 0.25); |
|
83 |
\draw[style=ice] (1, 0) -- |
|
84 |
(3, 6) node[above, fill=white] {logic} -- (5, 0) -- cycle; |
|
85 |
\draw[style=ice] (9, 0) -- |
|
86 |
(11, 6) node[above, fill=white] {intermediate language} -- (13, 0) -- cycle; |
|
87 |
\draw[style=ice] (15, -6) -- |
|
88 |
(19, 6) node[above, fill=white] {target language} -- (23, -6) -- cycle; |
|
89 |
\draw[style=process] |
|
90 |
(3.5, 3) .. controls (7, 5) .. node[fill=white] {translation} (10.5, 3); |
|
91 |
\draw[style=process] |
|
92 |
(11.5, 3) .. controls (15, 5) .. node[fill=white] (serialisation) {serialisation} (18.5, 3); |
|
93 |
\node (adaptation) at (11, -2) [style=adaptation] {adaptation}; |
|
94 |
\node at (19, 3) [rotate=90] {generated}; |
|
95 |
\node at (19.5, -5) {language}; |
|
96 |
\node at (19.5, -3) {library}; |
|
97 |
\node (includes) at (19.5, -1) {includes}; |
|
98 |
\node (reserved) at (16.5, -3) [rotate=72] {reserved}; % proper 71.57 |
|
99 |
\draw[style=process] |
|
100 |
(includes) -- (serialisation); |
|
101 |
\draw[style=process] |
|
102 |
(reserved) -- (serialisation); |
|
103 |
\draw[style=adaptation] |
|
104 |
(adaptation) -- (serialisation); |
|
105 |
\draw[style=adaptation] |
|
106 |
(adaptation) -- (includes); |
|
107 |
\draw[style=adaptation] |
|
108 |
(adaptation) -- (reserved); |
|
109 |
\end{tikzpicture} |
|
31050 | 110 |
\caption{The adaptation principle} |
111 |
\label{fig:adaptation} |
|
28601 | 112 |
\end{figure} |
113 |
||
114 |
\noindent In the tame view, code generation acts as broker between |
|
38450 | 115 |
@{text logic}, @{text "intermediate language"} and @{text "target |
116 |
language"} by means of @{text translation} and @{text |
|
117 |
serialisation}; for the latter, the serialiser has to observe the |
|
118 |
structure of the @{text language} itself plus some @{text reserved} |
|
119 |
keywords which have to be avoided for generated code. However, if |
|
120 |
you consider @{text adaptation} mechanisms, the code generated by |
|
121 |
the serializer is just the tip of the iceberg: |
|
28601 | 122 |
|
123 |
\begin{itemize} |
|
38450 | 124 |
|
28635 | 125 |
\item @{text serialisation} can be \emph{parametrised} such that |
126 |
logical entities are mapped to target-specific ones |
|
38450 | 127 |
(e.g. target-specific list syntax, see also |
128 |
\secref{sec:adaptation_mechanisms}) |
|
129 |
||
28635 | 130 |
\item Such parametrisations can involve references to a |
38450 | 131 |
target-specific standard @{text library} (e.g. using the @{text |
132 |
Haskell} @{verbatim Maybe} type instead of the @{text HOL} |
|
133 |
@{type "option"} type); if such are used, the corresponding |
|
134 |
identifiers (in our example, @{verbatim Maybe}, @{verbatim |
|
135 |
Nothing} and @{verbatim Just}) also have to be considered @{text |
|
136 |
reserved}. |
|
137 |
||
28635 | 138 |
\item Even more, the user can enrich the library of the |
38450 | 139 |
target-language by providing code snippets (\qt{@{text |
140 |
"includes"}}) which are prepended to any generated code (see |
|
141 |
\secref{sec:include}); this typically also involves further |
|
142 |
@{text reserved} identifiers. |
|
143 |
||
28601 | 144 |
\end{itemize} |
28635 | 145 |
|
38450 | 146 |
\noindent As figure \ref{fig:adaptation} illustrates, all these |
147 |
adaptation mechanisms have to act consistently; it is at the |
|
148 |
discretion of the user to take care for this. |
|
59377 | 149 |
\<close> |
28561 | 150 |
|
65041 | 151 |
subsection \<open>Common adaptation applications \label{sec:common_adaptation}\<close> |
28419 | 152 |
|
59377 | 153 |
text \<open> |
68484 | 154 |
The @{theory Main} theory of Isabelle/HOL already provides a code |
38450 | 155 |
generator setup which should be suitable for most applications. |
156 |
Common extensions and modifications are available by certain |
|
63680 | 157 |
theories in \<^dir>\<open>~~/src/HOL/Library\<close>; beside being useful in |
38450 | 158 |
applications, they may serve as a tutorial for customising the code |
159 |
generator setup (see below \secref{sec:adaptation_mechanisms}). |
|
28419 | 160 |
|
161 |
\begin{description} |
|
162 |
||
68484 | 163 |
\item[@{theory "HOL.Code_Numeral"}] provides additional numeric |
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
164 |
types @{typ integer} and @{typ natural} isomorphic to types |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
165 |
@{typ int} and @{typ nat} respectively. Type @{typ integer} |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
166 |
is mapped to target-language built-in integers; @{typ natural} |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
167 |
is implemented as abstract type over @{typ integer}. |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
168 |
Useful for code setups which involve e.g.~indexing |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
169 |
of target-language arrays. Part of @{text "HOL-Main"}. |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
170 |
|
68484 | 171 |
\item[@{theory "HOL.String"}] provides an additional datatype @{typ |
68028 | 172 |
String.literal} which is isomorphic to lists of 7-bit (ASCII) characters; |
173 |
@{typ String.literal}s are mapped to target-language strings. |
|
174 |
||
175 |
Literal values of type @{typ String.literal} can be written |
|
176 |
as @{text "STR ''\<dots>''"} for sequences of printable characters and |
|
68033
ad4b8b6892c3
uniform tagging for printable and non-printable literals
haftmann
parents:
68028
diff
changeset
|
177 |
@{text "STR 0x\<dots>"} for one single ASCII code point given |
68028 | 178 |
as hexadecimal numeral; @{typ String.literal} supports concatenation |
179 |
@{text "\<dots> + \<dots>"} for all standard target languages. |
|
180 |
||
181 |
Note that the particular notion of \qt{string} is target-language |
|
182 |
specific (sequence of 8-bit units, sequence of unicode code points, \ldots); |
|
183 |
hence ASCII is the only reliable common base e.g.~for |
|
184 |
printing (error) messages; more sophisticated applications |
|
185 |
like verifying parsing algorithms require a dedicated |
|
186 |
target-language specific model. |
|
187 |
||
188 |
Nevertheless @{typ String.literal}s can be analyzed; the core operations |
|
189 |
for this are @{term_type String.asciis_of_literal} and |
|
190 |
@{term_type String.literal_of_asciis} which are implemented |
|
191 |
in a target-language-specific way; particularly @{const String.asciis_of_literal} |
|
192 |
checks its argument at runtime to make sure that it does |
|
193 |
not contain non-ASCII-characters, to safeguard consistency. |
|
194 |
On top of these, more abstract conversions like @{term_type |
|
195 |
String.explode} and @{term_type String.implode} |
|
196 |
are implemented. |
|
197 |
||
198 |
Part of @{text "HOL-Main"}. |
|
199 |
||
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
200 |
\item[@{text "Code_Target_Int"}] implements type @{typ int} |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
201 |
by @{typ integer} and thus by target-language built-in integers. |
38450 | 202 |
|
51171
e8b2d90da499
corrected and clarified Code_Binary_Nat vs. Code_Target_Nat
haftmann
parents:
51162
diff
changeset
|
203 |
\item[@{text "Code_Binary_Nat"}] implements type |
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
204 |
@{typ nat} using a binary rather than a linear representation, |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
205 |
which yields a considerable speedup for computations. |
61076 | 206 |
Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated |
51171
e8b2d90da499
corrected and clarified Code_Binary_Nat vs. Code_Target_Nat
haftmann
parents:
51162
diff
changeset
|
207 |
by a preprocessor.\label{abstract_nat} |
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
208 |
|
51171
e8b2d90da499
corrected and clarified Code_Binary_Nat vs. Code_Target_Nat
haftmann
parents:
51162
diff
changeset
|
209 |
\item[@{text "Code_Target_Nat"}] implements type @{typ nat} |
e8b2d90da499
corrected and clarified Code_Binary_Nat vs. Code_Target_Nat
haftmann
parents:
51162
diff
changeset
|
210 |
by @{typ integer} and thus by target-language built-in integers. |
61076 | 211 |
Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated |
51171
e8b2d90da499
corrected and clarified Code_Binary_Nat vs. Code_Target_Nat
haftmann
parents:
51162
diff
changeset
|
212 |
by a preprocessor. |
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
213 |
|
51162 | 214 |
\item[@{text "Code_Target_Numeral"}] is a convenience theory |
51143
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
215 |
containing both @{text "Code_Target_Nat"} and |
0a2371e7ced3
two target language numeral types: integer and natural, as replacement for code_numeral;
haftmann
parents:
48985
diff
changeset
|
216 |
@{text "Code_Target_Int"}. |
38450 | 217 |
|
68484 | 218 |
\item[@{theory "HOL-Library.IArray"}] provides a type @{typ "'a iarray"} |
51162 | 219 |
isomorphic to lists but implemented by (effectively immutable) |
220 |
arrays \emph{in SML only}. |
|
28419 | 221 |
|
51162 | 222 |
\end{description} |
59377 | 223 |
\<close> |
28419 | 224 |
|
225 |
||
59377 | 226 |
subsection \<open>Parametrising serialisation \label{sec:adaptation_mechanisms}\<close> |
28419 | 227 |
|
59377 | 228 |
text \<open> |
38450 | 229 |
Consider the following function and its corresponding SML code: |
59377 | 230 |
\<close> |
28419 | 231 |
|
28564 | 232 |
primrec %quote in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where |
28419 | 233 |
"in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l" |
28447 | 234 |
(*<*) |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
235 |
code_printing %invisible |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
236 |
type_constructor bool \<rightharpoonup> (SML) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
237 |
| constant True \<rightharpoonup> (SML) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
238 |
| constant False \<rightharpoonup> (SML) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
239 |
| constant HOL.conj \<rightharpoonup> (SML) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
240 |
| constant Not \<rightharpoonup> (SML) |
28447 | 241 |
(*>*) |
59377 | 242 |
text %quotetypewriter \<open> |
39683 | 243 |
@{code_stmts in_interval (SML)} |
59377 | 244 |
\<close> |
28419 | 245 |
|
59377 | 246 |
text \<open> |
38450 | 247 |
\noindent Though this is correct code, it is a little bit |
248 |
unsatisfactory: boolean values and operators are materialised as |
|
249 |
distinguished entities with have nothing to do with the SML-built-in |
|
250 |
notion of \qt{bool}. This results in less readable code; |
|
251 |
additionally, eager evaluation may cause programs to loop or break |
|
252 |
which would perfectly terminate when the existing SML @{verbatim |
|
253 |
"bool"} would be used. To map the HOL @{typ bool} on SML @{verbatim |
|
254 |
"bool"}, we may use \qn{custom serialisations}: |
|
59377 | 255 |
\<close> |
28419 | 256 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
257 |
code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
258 |
type_constructor bool \<rightharpoonup> (SML) "bool" |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
259 |
| constant True \<rightharpoonup> (SML) "true" |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
260 |
| constant False \<rightharpoonup> (SML) "false" |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
261 |
| constant HOL.conj \<rightharpoonup> (SML) "_ andalso _" |
28213 | 262 |
|
59377 | 263 |
text \<open> |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
264 |
\noindent The @{command_def code_printing} command takes a series |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
265 |
of symbols (contants, type constructor, \ldots) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
266 |
together with target-specific custom serialisations. Each |
38450 | 267 |
custom serialisation starts with a target language identifier |
268 |
followed by an expression, which during code serialisation is |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
269 |
inserted whenever the type constructor would occur. Each |
38450 | 270 |
``@{verbatim "_"}'' in a serialisation expression is treated as a |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
271 |
placeholder for the constant's or the type constructor's arguments. |
59377 | 272 |
\<close> |
28419 | 273 |
|
59377 | 274 |
text %quotetypewriter \<open> |
39683 | 275 |
@{code_stmts in_interval (SML)} |
59377 | 276 |
\<close> |
28419 | 277 |
|
59377 | 278 |
text \<open> |
38450 | 279 |
\noindent This still is not perfect: the parentheses around the |
280 |
\qt{andalso} expression are superfluous. Though the serialiser by |
|
281 |
no means attempts to imitate the rich Isabelle syntax framework, it |
|
282 |
provides some common idioms, notably associative infixes with |
|
283 |
precedences which may be used here: |
|
59377 | 284 |
\<close> |
28419 | 285 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
286 |
code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
287 |
constant HOL.conj \<rightharpoonup> (SML) infixl 1 "andalso" |
28419 | 288 |
|
59377 | 289 |
text %quotetypewriter \<open> |
39683 | 290 |
@{code_stmts in_interval (SML)} |
59377 | 291 |
\<close> |
28419 | 292 |
|
59377 | 293 |
text \<open> |
38450 | 294 |
\noindent The attentive reader may ask how we assert that no |
295 |
generated code will accidentally overwrite. For this reason the |
|
296 |
serialiser has an internal table of identifiers which have to be |
|
297 |
avoided to be used for new declarations. Initially, this table |
|
298 |
typically contains the keywords of the target language. It can be |
|
299 |
extended manually, thus avoiding accidental overwrites, using the |
|
38505 | 300 |
@{command_def "code_reserved"} command: |
59377 | 301 |
\<close> |
28561 | 302 |
|
40351 | 303 |
code_reserved %quote "\<SMLdummy>" bool true false andalso |
28561 | 304 |
|
59377 | 305 |
text \<open> |
28447 | 306 |
\noindent Next, we try to map HOL pairs to SML pairs, using the |
28419 | 307 |
infix ``@{verbatim "*"}'' type constructor and parentheses: |
59377 | 308 |
\<close> |
28447 | 309 |
(*<*) |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
310 |
code_printing %invisible |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
311 |
type_constructor prod \<rightharpoonup> (SML) |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
312 |
| constant Pair \<rightharpoonup> (SML) |
28447 | 313 |
(*>*) |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
314 |
code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
315 |
type_constructor prod \<rightharpoonup> (SML) infix 2 "*" |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
316 |
| constant Pair \<rightharpoonup> (SML) "!((_),/ (_))" |
28419 | 317 |
|
59377 | 318 |
text \<open> |
28593 | 319 |
\noindent The initial bang ``@{verbatim "!"}'' tells the serialiser |
38450 | 320 |
never to put parentheses around the whole expression (they are |
321 |
already present), while the parentheses around argument place |
|
322 |
holders tell not to put parentheses around the arguments. The slash |
|
323 |
``@{verbatim "/"}'' (followed by arbitrary white space) inserts a |
|
324 |
space which may be used as a break if necessary during pretty |
|
325 |
printing. |
|
28419 | 326 |
|
38450 | 327 |
These examples give a glimpse what mechanisms custom serialisations |
328 |
provide; however their usage requires careful thinking in order not |
|
329 |
to introduce inconsistencies -- or, in other words: custom |
|
330 |
serialisations are completely axiomatic. |
|
28419 | 331 |
|
39643 | 332 |
A further noteworthy detail is that any special character in a |
38450 | 333 |
custom serialisation may be quoted using ``@{verbatim "'"}''; thus, |
334 |
in ``@{verbatim "fn '_ => _"}'' the first ``@{verbatim "_"}'' is a |
|
335 |
proper underscore while the second ``@{verbatim "_"}'' is a |
|
336 |
placeholder. |
|
59377 | 337 |
\<close> |
28419 | 338 |
|
339 |
||
59377 | 340 |
subsection \<open>@{text Haskell} serialisation\<close> |
28419 | 341 |
|
59377 | 342 |
text \<open> |
38450 | 343 |
For convenience, the default @{text HOL} setup for @{text Haskell} |
39063 | 344 |
maps the @{class equal} class to its counterpart in @{text Haskell}, |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
345 |
giving custom serialisations for the class @{class equal} |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
346 |
and its operation @{const [source] HOL.equal}. |
59377 | 347 |
\<close> |
28419 | 348 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
349 |
code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
350 |
type_class equal \<rightharpoonup> (Haskell) "Eq" |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
351 |
| constant HOL.equal \<rightharpoonup> (Haskell) infixl 4 "==" |
28419 | 352 |
|
59377 | 353 |
text \<open> |
38450 | 354 |
\noindent A problem now occurs whenever a type which is an instance |
39063 | 355 |
of @{class equal} in @{text HOL} is mapped on a @{text |
38450 | 356 |
Haskell}-built-in type which is also an instance of @{text Haskell} |
357 |
@{text Eq}: |
|
59377 | 358 |
\<close> |
28419 | 359 |
|
28564 | 360 |
typedecl %quote bar |
28419 | 361 |
|
39063 | 362 |
instantiation %quote bar :: equal |
28419 | 363 |
begin |
364 |
||
61076 | 365 |
definition %quote "HOL.equal (x::bar) y \<longleftrightarrow> x = y" |
28419 | 366 |
|
61169 | 367 |
instance %quote by standard (simp add: equal_bar_def) |
28213 | 368 |
|
30880 | 369 |
end %quote (*<*) |
370 |
||
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
371 |
(*>*) code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
372 |
type_constructor bar \<rightharpoonup> (Haskell) "Integer" |
28419 | 373 |
|
59377 | 374 |
text \<open> |
38450 | 375 |
\noindent The code generator would produce an additional instance, |
376 |
which of course is rejected by the @{text Haskell} compiler. To |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
377 |
suppress this additional instance: |
59377 | 378 |
\<close> |
28419 | 379 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
380 |
code_printing %quotett |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
381 |
class_instance bar :: "HOL.equal" \<rightharpoonup> (Haskell) - |
28419 | 382 |
|
28561 | 383 |
|
59377 | 384 |
subsection \<open>Enhancing the target language context \label{sec:include}\<close> |
28561 | 385 |
|
59377 | 386 |
text \<open> |
28593 | 387 |
In rare cases it is necessary to \emph{enrich} the context of a |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
388 |
target language; this can also be accomplished using the @{command |
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
389 |
"code_printing"} command: |
59377 | 390 |
\<close> |
28561 | 391 |
|
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
392 |
code_printing %quotett |
59379 | 393 |
code_module "Errno" \<rightharpoonup> (Haskell) |
394 |
\<open>errno i = error ("Error number: " ++ show i)\<close> |
|
28561 | 395 |
|
39745 | 396 |
code_reserved %quotett Haskell Errno |
28561 | 397 |
|
59377 | 398 |
text \<open> |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
399 |
\noindent Such named modules are then prepended to every |
38450 | 400 |
generated code. Inspect such code in order to find out how |
52378
08dbf9ff2140
documentation on code_printing and code_identifier
haftmann
parents:
51172
diff
changeset
|
401 |
this behaves with respect to a particular |
38450 | 402 |
target language. |
59377 | 403 |
\<close> |
28561 | 404 |
|
28419 | 405 |
end |
46519 | 406 |