author | haftmann |
Tue, 17 Aug 2010 14:19:12 +0200 | |
changeset 38459 | cfe74b0eecb1 |
parent 38450 | ada5814c9d87 |
child 38505 | 2f8699695cf6 |
permissions | -rw-r--r-- |
31050 | 1 |
% |
2 |
\begin{isabellebody}% |
|
3 |
\def\isabellecontext{Adaptation}% |
|
4 |
% |
|
5 |
\isadelimtheory |
|
6 |
% |
|
7 |
\endisadelimtheory |
|
8 |
% |
|
9 |
\isatagtheory |
|
10 |
\isacommand{theory}\isamarkupfalse% |
|
11 |
\ Adaptation\isanewline |
|
12 |
\isakeyword{imports}\ Setup\isanewline |
|
13 |
\isakeyword{begin}% |
|
14 |
\endisatagtheory |
|
15 |
{\isafoldtheory}% |
|
16 |
% |
|
17 |
\isadelimtheory |
|
18 |
\isanewline |
|
19 |
% |
|
20 |
\endisadelimtheory |
|
21 |
% |
|
22 |
\isadeliminvisible |
|
23 |
\isanewline |
|
24 |
% |
|
25 |
\endisadeliminvisible |
|
26 |
% |
|
27 |
\isataginvisible |
|
28 |
\isacommand{setup}\isamarkupfalse% |
|
29 |
\ {\isacharverbatimopen}\ Code{\isacharunderscore}Target{\isachardot}extend{\isacharunderscore}target\ {\isacharparenleft}{\isachardoublequote}{\isasymSML}{\isachardoublequote}{\isacharcomma}\ {\isacharparenleft}{\isachardoublequote}SML{\isachardoublequote}{\isacharcomma}\ K\ I{\isacharparenright}{\isacharparenright}\ {\isacharverbatimclose}% |
|
30 |
\endisataginvisible |
|
31 |
{\isafoldinvisible}% |
|
32 |
% |
|
33 |
\isadeliminvisible |
|
34 |
% |
|
35 |
\endisadeliminvisible |
|
36 |
% |
|
37 |
\isamarkupsection{Adaptation to target languages \label{sec:adaptation}% |
|
38 |
} |
|
39 |
\isamarkuptrue% |
|
40 |
% |
|
41 |
\isamarkupsubsection{Adapting code generation% |
|
42 |
} |
|
43 |
\isamarkuptrue% |
|
44 |
% |
|
45 |
\begin{isamarkuptext}% |
|
46 |
The aspects of code generation introduced so far have two aspects |
|
47 |
in common: |
|
48 |
||
49 |
\begin{itemize} |
|
38450 | 50 |
|
51 |
\item They act uniformly, without reference to a specific target |
|
52 |
language. |
|
53 |
||
31050 | 54 |
\item They are \emph{safe} in the sense that as long as you trust |
55 |
the code generator meta theory and implementation, you cannot |
|
38450 | 56 |
produce programs that yield results which are not derivable in |
57 |
the logic. |
|
58 |
||
31050 | 59 |
\end{itemize} |
60 |
||
38450 | 61 |
\noindent In this section we will introduce means to \emph{adapt} |
62 |
the serialiser to a specific target language, i.e.~to print program |
|
63 |
fragments in a way which accommodates \qt{already existing} |
|
64 |
ingredients of a target language environment, for three reasons: |
|
31050 | 65 |
|
66 |
\begin{itemize} |
|
67 |
\item improving readability and aesthetics of generated code |
|
68 |
\item gaining efficiency |
|
69 |
\item interface with language parts which have no direct counterpart |
|
70 |
in \isa{HOL} (say, imperative data structures) |
|
71 |
\end{itemize} |
|
72 |
||
73 |
\noindent Generally, you should avoid using those features yourself |
|
74 |
\emph{at any cost}: |
|
75 |
||
76 |
\begin{itemize} |
|
38450 | 77 |
|
78 |
\item The safe configuration methods act uniformly on every target |
|
79 |
language, whereas for adaptation you have to treat each target |
|
80 |
language separately. |
|
81 |
||
82 |
\item Application is extremely tedious since there is no |
|
83 |
abstraction which would allow for a static check, making it easy |
|
84 |
to produce garbage. |
|
85 |
||
34155 | 86 |
\item Subtle errors can be introduced unconsciously. |
38450 | 87 |
|
31050 | 88 |
\end{itemize} |
89 |
||
38450 | 90 |
\noindent However, even if you ought refrain from setting up |
91 |
adaptation yourself, already the \isa{HOL} comes with some |
|
92 |
reasonable default adaptations (say, using target language list |
|
93 |
syntax). There also some common adaptation cases which you can |
|
94 |
setup by importing particular library theories. In order to |
|
95 |
understand these, we provide some clues here; these however are not |
|
96 |
supposed to replace a careful study of the sources.% |
|
31050 | 97 |
\end{isamarkuptext}% |
98 |
\isamarkuptrue% |
|
99 |
% |
|
100 |
\isamarkupsubsection{The adaptation principle% |
|
101 |
} |
|
102 |
\isamarkuptrue% |
|
103 |
% |
|
104 |
\begin{isamarkuptext}% |
|
38450 | 105 |
Figure \ref{fig:adaptation} illustrates what \qt{adaptation} is |
106 |
conceptually supposed to be: |
|
31050 | 107 |
|
108 |
\begin{figure}[here] |
|
109 |
\includegraphics{adaptation} |
|
110 |
\caption{The adaptation principle} |
|
111 |
\label{fig:adaptation} |
|
112 |
\end{figure} |
|
113 |
||
114 |
\noindent In the tame view, code generation acts as broker between |
|
38450 | 115 |
\isa{logic}, \isa{intermediate\ language} and \isa{target\ language} by means of \isa{translation} and \isa{serialisation}; for the latter, the serialiser has to observe the |
116 |
structure of the \isa{language} itself plus some \isa{reserved} |
|
117 |
keywords which have to be avoided for generated code. However, if |
|
118 |
you consider \isa{adaptation} mechanisms, the code generated by |
|
119 |
the serializer is just the tip of the iceberg: |
|
31050 | 120 |
|
121 |
\begin{itemize} |
|
38450 | 122 |
|
31050 | 123 |
\item \isa{serialisation} can be \emph{parametrised} such that |
124 |
logical entities are mapped to target-specific ones |
|
38450 | 125 |
(e.g. target-specific list syntax, see also |
126 |
\secref{sec:adaptation_mechanisms}) |
|
127 |
||
31050 | 128 |
\item Such parametrisations can involve references to a |
38450 | 129 |
target-specific standard \isa{library} (e.g. using the \isa{Haskell} \verb|Maybe| type instead of the \isa{HOL} |
130 |
\isa{option} type); if such are used, the corresponding |
|
131 |
identifiers (in our example, \verb|Maybe|, \verb|Nothing| and \verb|Just|) also have to be considered \isa{reserved}. |
|
132 |
||
31050 | 133 |
\item Even more, the user can enrich the library of the |
38450 | 134 |
target-language by providing code snippets (\qt{\isa{includes}}) which are prepended to any generated code (see |
135 |
\secref{sec:include}); this typically also involves further |
|
136 |
\isa{reserved} identifiers. |
|
137 |
||
31050 | 138 |
\end{itemize} |
139 |
||
38450 | 140 |
\noindent As figure \ref{fig:adaptation} illustrates, all these |
141 |
adaptation mechanisms have to act consistently; it is at the |
|
142 |
discretion of the user to take care for this.% |
|
31050 | 143 |
\end{isamarkuptext}% |
144 |
\isamarkuptrue% |
|
145 |
% |
|
146 |
\isamarkupsubsection{Common adaptation patterns% |
|
147 |
} |
|
148 |
\isamarkuptrue% |
|
149 |
% |
|
150 |
\begin{isamarkuptext}% |
|
151 |
The \hyperlink{theory.HOL}{\mbox{\isa{HOL}}} \hyperlink{theory.Main}{\mbox{\isa{Main}}} theory already provides a code |
|
38450 | 152 |
generator setup which should be suitable for most applications. |
153 |
Common extensions and modifications are available by certain |
|
154 |
theories of the \isa{HOL} library; beside being useful in |
|
155 |
applications, they may serve as a tutorial for customising the code |
|
156 |
generator setup (see below \secref{sec:adaptation_mechanisms}). |
|
31050 | 157 |
|
158 |
\begin{description} |
|
159 |
||
38450 | 160 |
\item[\hyperlink{theory.Code-Integer}{\mbox{\isa{Code{\isacharunderscore}Integer}}}] represents \isa{HOL} integers by |
161 |
big integer literals in target languages. |
|
162 |
||
163 |
\item[\hyperlink{theory.Code-Char}{\mbox{\isa{Code{\isacharunderscore}Char}}}] represents \isa{HOL} characters by |
|
31050 | 164 |
character literals in target languages. |
38450 | 165 |
|
166 |
\item[\hyperlink{theory.Code-Char-chr}{\mbox{\isa{Code{\isacharunderscore}Char{\isacharunderscore}chr}}}] like \isa{Code{\isacharunderscore}Char}, but |
|
167 |
also offers treatment of character codes; includes \hyperlink{theory.Code-Char}{\mbox{\isa{Code{\isacharunderscore}Char}}}. |
|
168 |
||
169 |
\item[\hyperlink{theory.Efficient-Nat}{\mbox{\isa{Efficient{\isacharunderscore}Nat}}}] \label{eff_nat} implements |
|
170 |
natural numbers by integers, which in general will result in |
|
171 |
higher efficiency; pattern matching with \isa{{\isadigit{0}}} / |
|
172 |
\isa{Suc} is eliminated; includes \hyperlink{theory.Code-Integer}{\mbox{\isa{Code{\isacharunderscore}Integer}}} |
|
31206 | 173 |
and \hyperlink{theory.Code-Numeral}{\mbox{\isa{Code{\isacharunderscore}Numeral}}}. |
38450 | 174 |
|
31206 | 175 |
\item[\hyperlink{theory.Code-Numeral}{\mbox{\isa{Code{\isacharunderscore}Numeral}}}] provides an additional datatype |
38450 | 176 |
\isa{index} which is mapped to target-language built-in |
177 |
integers. Useful for code setups which involve e.g.~indexing |
|
178 |
of target-language arrays. |
|
179 |
||
180 |
\item[\hyperlink{theory.String}{\mbox{\isa{String}}}] provides an additional datatype \isa{String{\isachardot}literal} which is isomorphic to strings; \isa{String{\isachardot}literal}s are mapped to target-language strings. Useful |
|
181 |
for code setups which involve e.g.~printing (error) messages. |
|
31050 | 182 |
|
183 |
\end{description} |
|
184 |
||
185 |
\begin{warn} |
|
186 |
When importing any of these theories, they should form the last |
|
38450 | 187 |
items in an import list. Since these theories adapt the code |
188 |
generator setup in a non-conservative fashion, strange effects may |
|
189 |
occur otherwise. |
|
31050 | 190 |
\end{warn}% |
191 |
\end{isamarkuptext}% |
|
192 |
\isamarkuptrue% |
|
193 |
% |
|
194 |
\isamarkupsubsection{Parametrising serialisation \label{sec:adaptation_mechanisms}% |
|
195 |
} |
|
196 |
\isamarkuptrue% |
|
197 |
% |
|
198 |
\begin{isamarkuptext}% |
|
38450 | 199 |
Consider the following function and its corresponding SML code:% |
31050 | 200 |
\end{isamarkuptext}% |
201 |
\isamarkuptrue% |
|
202 |
% |
|
203 |
\isadelimquote |
|
204 |
% |
|
205 |
\endisadelimquote |
|
206 |
% |
|
207 |
\isatagquote |
|
208 |
\isacommand{primrec}\isamarkupfalse% |
|
209 |
\ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline |
|
210 |
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}% |
|
211 |
\endisatagquote |
|
212 |
{\isafoldquote}% |
|
213 |
% |
|
214 |
\isadelimquote |
|
215 |
% |
|
216 |
\endisadelimquote |
|
217 |
% |
|
218 |
\isadeliminvisible |
|
219 |
% |
|
220 |
\endisadeliminvisible |
|
221 |
% |
|
222 |
\isataginvisible |
|
223 |
% |
|
224 |
\endisataginvisible |
|
225 |
{\isafoldinvisible}% |
|
226 |
% |
|
227 |
\isadeliminvisible |
|
228 |
% |
|
229 |
\endisadeliminvisible |
|
230 |
% |
|
231 |
\isadelimquote |
|
232 |
% |
|
233 |
\endisadelimquote |
|
234 |
% |
|
235 |
\isatagquote |
|
236 |
% |
|
237 |
\begin{isamarkuptext}% |
|
238 |
\isatypewriter% |
|
239 |
\noindent% |
|
34155 | 240 |
\hspace*{0pt}structure Example :~sig\\ |
34179 | 241 |
\hspace*{0pt} ~datatype nat = Zero{\char95}nat | Suc of nat\\ |
34155 | 242 |
\hspace*{0pt} ~datatype boola = True | False\\ |
243 |
\hspace*{0pt} ~val anda :~boola -> boola -> boola\\ |
|
244 |
\hspace*{0pt} ~val less{\char95}nat :~nat -> nat -> boola\\ |
|
245 |
\hspace*{0pt} ~val less{\char95}eq{\char95}nat :~nat -> nat -> boola\\ |
|
246 |
\hspace*{0pt} ~val in{\char95}interval :~nat * nat -> nat -> boola\\ |
|
247 |
\hspace*{0pt}end = struct\\ |
|
31050 | 248 |
\hspace*{0pt}\\ |
249 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\ |
|
250 |
\hspace*{0pt}\\ |
|
251 |
\hspace*{0pt}datatype boola = True | False;\\ |
|
252 |
\hspace*{0pt}\\ |
|
33707
68841fb382e0
dropped obsolete documentation; updated generated sources
haftmann
parents:
31206
diff
changeset
|
253 |
\hspace*{0pt}fun anda p True = p\\ |
34179 | 254 |
\hspace*{0pt} ~| anda p False = False\\ |
255 |
\hspace*{0pt} ~| anda True p = p\\ |
|
256 |
\hspace*{0pt} ~| anda False p = False;\\ |
|
31050 | 257 |
\hspace*{0pt}\\ |
34179 | 258 |
\hspace*{0pt}fun less{\char95}nat m (Suc n) = less{\char95}eq{\char95}nat m n\\ |
259 |
\hspace*{0pt} ~| less{\char95}nat n Zero{\char95}nat = False\\ |
|
260 |
\hspace*{0pt}and less{\char95}eq{\char95}nat (Suc m) n = less{\char95}nat m n\\ |
|
261 |
\hspace*{0pt} ~| less{\char95}eq{\char95}nat Zero{\char95}nat n = True;\\ |
|
31050 | 262 |
\hspace*{0pt}\\ |
34179 | 263 |
\hspace*{0pt}fun in{\char95}interval (k,~l) n = anda (less{\char95}eq{\char95}nat k n) (less{\char95}eq{\char95}nat n l);\\ |
31050 | 264 |
\hspace*{0pt}\\ |
265 |
\hspace*{0pt}end;~(*struct Example*)% |
|
266 |
\end{isamarkuptext}% |
|
267 |
\isamarkuptrue% |
|
268 |
% |
|
269 |
\endisatagquote |
|
270 |
{\isafoldquote}% |
|
271 |
% |
|
272 |
\isadelimquote |
|
273 |
% |
|
274 |
\endisadelimquote |
|
275 |
% |
|
276 |
\begin{isamarkuptext}% |
|
38450 | 277 |
\noindent Though this is correct code, it is a little bit |
278 |
unsatisfactory: boolean values and operators are materialised as |
|
279 |
distinguished entities with have nothing to do with the SML-built-in |
|
280 |
notion of \qt{bool}. This results in less readable code; |
|
281 |
additionally, eager evaluation may cause programs to loop or break |
|
282 |
which would perfectly terminate when the existing SML \verb|bool| would be used. To map the HOL \isa{bool} on SML \verb|bool|, we may use \qn{custom serialisations}:% |
|
31050 | 283 |
\end{isamarkuptext}% |
284 |
\isamarkuptrue% |
|
285 |
% |
|
286 |
\isadelimquotett |
|
287 |
% |
|
288 |
\endisadelimquotett |
|
289 |
% |
|
290 |
\isatagquotett |
|
291 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
|
292 |
\ bool\isanewline |
|
293 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
294 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
295 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
296 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
297 |
\endisatagquotett |
|
298 |
{\isafoldquotett}% |
|
299 |
% |
|
300 |
\isadelimquotett |
|
301 |
% |
|
302 |
\endisadelimquotett |
|
303 |
% |
|
304 |
\begin{isamarkuptext}% |
|
305 |
\noindent The \hyperlink{command.code-type}{\mbox{\isa{\isacommand{code{\isacharunderscore}type}}}} command takes a type constructor |
|
38450 | 306 |
as arguments together with a list of custom serialisations. Each |
307 |
custom serialisation starts with a target language identifier |
|
308 |
followed by an expression, which during code serialisation is |
|
309 |
inserted whenever the type constructor would occur. For constants, |
|
310 |
\hyperlink{command.code-const}{\mbox{\isa{\isacommand{code{\isacharunderscore}const}}}} implements the corresponding mechanism. Each |
|
311 |
``\verb|_|'' in a serialisation expression is treated as a |
|
312 |
placeholder for the type constructor's (the constant's) arguments.% |
|
31050 | 313 |
\end{isamarkuptext}% |
314 |
\isamarkuptrue% |
|
315 |
% |
|
316 |
\isadelimquote |
|
317 |
% |
|
318 |
\endisadelimquote |
|
319 |
% |
|
320 |
\isatagquote |
|
321 |
% |
|
322 |
\begin{isamarkuptext}% |
|
323 |
\isatypewriter% |
|
324 |
\noindent% |
|
34155 | 325 |
\hspace*{0pt}structure Example :~sig\\ |
34179 | 326 |
\hspace*{0pt} ~datatype nat = Zero{\char95}nat | Suc of nat\\ |
34155 | 327 |
\hspace*{0pt} ~val less{\char95}nat :~nat -> nat -> bool\\ |
328 |
\hspace*{0pt} ~val less{\char95}eq{\char95}nat :~nat -> nat -> bool\\ |
|
329 |
\hspace*{0pt} ~val in{\char95}interval :~nat * nat -> nat -> bool\\ |
|
330 |
\hspace*{0pt}end = struct\\ |
|
31050 | 331 |
\hspace*{0pt}\\ |
332 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\ |
|
333 |
\hspace*{0pt}\\ |
|
34179 | 334 |
\hspace*{0pt}fun less{\char95}nat m (Suc n) = less{\char95}eq{\char95}nat m n\\ |
335 |
\hspace*{0pt} ~| less{\char95}nat n Zero{\char95}nat = false\\ |
|
336 |
\hspace*{0pt}and less{\char95}eq{\char95}nat (Suc m) n = less{\char95}nat m n\\ |
|
337 |
\hspace*{0pt} ~| less{\char95}eq{\char95}nat Zero{\char95}nat n = true;\\ |
|
31050 | 338 |
\hspace*{0pt}\\ |
34179 | 339 |
\hspace*{0pt}fun in{\char95}interval (k,~l) n = (less{\char95}eq{\char95}nat k n) andalso (less{\char95}eq{\char95}nat n l);\\ |
31050 | 340 |
\hspace*{0pt}\\ |
341 |
\hspace*{0pt}end;~(*struct Example*)% |
|
342 |
\end{isamarkuptext}% |
|
343 |
\isamarkuptrue% |
|
344 |
% |
|
345 |
\endisatagquote |
|
346 |
{\isafoldquote}% |
|
347 |
% |
|
348 |
\isadelimquote |
|
349 |
% |
|
350 |
\endisadelimquote |
|
351 |
% |
|
352 |
\begin{isamarkuptext}% |
|
38450 | 353 |
\noindent This still is not perfect: the parentheses around the |
354 |
\qt{andalso} expression are superfluous. Though the serialiser by |
|
355 |
no means attempts to imitate the rich Isabelle syntax framework, it |
|
356 |
provides some common idioms, notably associative infixes with |
|
357 |
precedences which may be used here:% |
|
31050 | 358 |
\end{isamarkuptext}% |
359 |
\isamarkuptrue% |
|
360 |
% |
|
361 |
\isadelimquotett |
|
362 |
% |
|
363 |
\endisadelimquotett |
|
364 |
% |
|
365 |
\isatagquotett |
|
366 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
367 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
368 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
|
369 |
\endisatagquotett |
|
370 |
{\isafoldquotett}% |
|
371 |
% |
|
372 |
\isadelimquotett |
|
373 |
% |
|
374 |
\endisadelimquotett |
|
375 |
% |
|
376 |
\isadelimquote |
|
377 |
% |
|
378 |
\endisadelimquote |
|
379 |
% |
|
380 |
\isatagquote |
|
381 |
% |
|
382 |
\begin{isamarkuptext}% |
|
383 |
\isatypewriter% |
|
384 |
\noindent% |
|
34155 | 385 |
\hspace*{0pt}structure Example :~sig\\ |
34179 | 386 |
\hspace*{0pt} ~datatype nat = Zero{\char95}nat | Suc of nat\\ |
34155 | 387 |
\hspace*{0pt} ~val less{\char95}nat :~nat -> nat -> bool\\ |
388 |
\hspace*{0pt} ~val less{\char95}eq{\char95}nat :~nat -> nat -> bool\\ |
|
389 |
\hspace*{0pt} ~val in{\char95}interval :~nat * nat -> nat -> bool\\ |
|
390 |
\hspace*{0pt}end = struct\\ |
|
31050 | 391 |
\hspace*{0pt}\\ |
392 |
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\ |
|
393 |
\hspace*{0pt}\\ |
|
34179 | 394 |
\hspace*{0pt}fun less{\char95}nat m (Suc n) = less{\char95}eq{\char95}nat m n\\ |
395 |
\hspace*{0pt} ~| less{\char95}nat n Zero{\char95}nat = false\\ |
|
396 |
\hspace*{0pt}and less{\char95}eq{\char95}nat (Suc m) n = less{\char95}nat m n\\ |
|
397 |
\hspace*{0pt} ~| less{\char95}eq{\char95}nat Zero{\char95}nat n = true;\\ |
|
31050 | 398 |
\hspace*{0pt}\\ |
34179 | 399 |
\hspace*{0pt}fun in{\char95}interval (k,~l) n = less{\char95}eq{\char95}nat k n andalso less{\char95}eq{\char95}nat n l;\\ |
31050 | 400 |
\hspace*{0pt}\\ |
401 |
\hspace*{0pt}end;~(*struct Example*)% |
|
402 |
\end{isamarkuptext}% |
|
403 |
\isamarkuptrue% |
|
404 |
% |
|
405 |
\endisatagquote |
|
406 |
{\isafoldquote}% |
|
407 |
% |
|
408 |
\isadelimquote |
|
409 |
% |
|
410 |
\endisadelimquote |
|
411 |
% |
|
412 |
\begin{isamarkuptext}% |
|
38450 | 413 |
\noindent The attentive reader may ask how we assert that no |
414 |
generated code will accidentally overwrite. For this reason the |
|
415 |
serialiser has an internal table of identifiers which have to be |
|
416 |
avoided to be used for new declarations. Initially, this table |
|
417 |
typically contains the keywords of the target language. It can be |
|
418 |
extended manually, thus avoiding accidental overwrites, using the |
|
419 |
\hyperlink{command.code-reserved}{\mbox{\isa{\isacommand{code{\isacharunderscore}reserved}}}} command:% |
|
31050 | 420 |
\end{isamarkuptext}% |
421 |
\isamarkuptrue% |
|
422 |
% |
|
423 |
\isadelimquote |
|
424 |
% |
|
425 |
\endisadelimquote |
|
426 |
% |
|
427 |
\isatagquote |
|
428 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
429 |
\ {\isachardoublequoteopen}{\isasymSML}{\isachardoublequoteclose}\ bool\ true\ false\ andalso% |
|
430 |
\endisatagquote |
|
431 |
{\isafoldquote}% |
|
432 |
% |
|
433 |
\isadelimquote |
|
434 |
% |
|
435 |
\endisadelimquote |
|
436 |
% |
|
437 |
\begin{isamarkuptext}% |
|
438 |
\noindent Next, we try to map HOL pairs to SML pairs, using the |
|
439 |
infix ``\verb|*|'' type constructor and parentheses:% |
|
440 |
\end{isamarkuptext}% |
|
441 |
\isamarkuptrue% |
|
442 |
% |
|
443 |
\isadeliminvisible |
|
444 |
% |
|
445 |
\endisadeliminvisible |
|
446 |
% |
|
447 |
\isataginvisible |
|
448 |
% |
|
449 |
\endisataginvisible |
|
450 |
{\isafoldinvisible}% |
|
451 |
% |
|
452 |
\isadeliminvisible |
|
453 |
% |
|
454 |
\endisadeliminvisible |
|
455 |
% |
|
456 |
\isadelimquotett |
|
457 |
% |
|
458 |
\endisadelimquotett |
|
459 |
% |
|
460 |
\isatagquotett |
|
461 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
|
37836 | 462 |
\ prod\isanewline |
31050 | 463 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
464 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
465 |
\ Pair\isanewline |
|
466 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
467 |
\endisatagquotett |
|
468 |
{\isafoldquotett}% |
|
469 |
% |
|
470 |
\isadelimquotett |
|
471 |
% |
|
472 |
\endisadelimquotett |
|
473 |
% |
|
474 |
\begin{isamarkuptext}% |
|
475 |
\noindent The initial bang ``\verb|!|'' tells the serialiser |
|
38450 | 476 |
never to put parentheses around the whole expression (they are |
477 |
already present), while the parentheses around argument place |
|
478 |
holders tell not to put parentheses around the arguments. The slash |
|
479 |
``\verb|/|'' (followed by arbitrary white space) inserts a |
|
480 |
space which may be used as a break if necessary during pretty |
|
481 |
printing. |
|
31050 | 482 |
|
38450 | 483 |
These examples give a glimpse what mechanisms custom serialisations |
484 |
provide; however their usage requires careful thinking in order not |
|
485 |
to introduce inconsistencies -- or, in other words: custom |
|
486 |
serialisations are completely axiomatic. |
|
31050 | 487 |
|
38450 | 488 |
A further noteworthy details is that any special character in a |
489 |
custom serialisation may be quoted using ``\verb|'|''; thus, |
|
490 |
in ``\verb|fn '_ => _|'' the first ``\verb|_|'' is a |
|
491 |
proper underscore while the second ``\verb|_|'' is a |
|
492 |
placeholder.% |
|
31050 | 493 |
\end{isamarkuptext}% |
494 |
\isamarkuptrue% |
|
495 |
% |
|
496 |
\isamarkupsubsection{\isa{Haskell} serialisation% |
|
497 |
} |
|
498 |
\isamarkuptrue% |
|
499 |
% |
|
500 |
\begin{isamarkuptext}% |
|
38450 | 501 |
For convenience, the default \isa{HOL} setup for \isa{Haskell} |
502 |
maps the \isa{eq} class to its counterpart in \isa{Haskell}, |
|
503 |
giving custom serialisations for the class \isa{eq} (by command |
|
504 |
\hyperlink{command.code-class}{\mbox{\isa{\isacommand{code{\isacharunderscore}class}}}}) and its operation \isa{eq{\isacharunderscore}class{\isachardot}eq}% |
|
31050 | 505 |
\end{isamarkuptext}% |
506 |
\isamarkuptrue% |
|
507 |
% |
|
508 |
\isadelimquotett |
|
509 |
% |
|
510 |
\endisadelimquotett |
|
511 |
% |
|
512 |
\isatagquotett |
|
513 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
|
514 |
\ eq\isanewline |
|
515 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
516 |
\isanewline |
|
517 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
518 |
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline |
|
519 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
|
520 |
\endisatagquotett |
|
521 |
{\isafoldquotett}% |
|
522 |
% |
|
523 |
\isadelimquotett |
|
524 |
% |
|
525 |
\endisadelimquotett |
|
526 |
% |
|
527 |
\begin{isamarkuptext}% |
|
38450 | 528 |
\noindent A problem now occurs whenever a type which is an instance |
529 |
of \isa{eq} in \isa{HOL} is mapped on a \isa{Haskell}-built-in type which is also an instance of \isa{Haskell} |
|
530 |
\isa{Eq}:% |
|
31050 | 531 |
\end{isamarkuptext}% |
532 |
\isamarkuptrue% |
|
533 |
% |
|
534 |
\isadelimquote |
|
535 |
% |
|
536 |
\endisadelimquote |
|
537 |
% |
|
538 |
\isatagquote |
|
539 |
\isacommand{typedecl}\isamarkupfalse% |
|
540 |
\ bar\isanewline |
|
541 |
\isanewline |
|
542 |
\isacommand{instantiation}\isamarkupfalse% |
|
543 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
544 |
\isakeyword{begin}\isanewline |
|
545 |
\isanewline |
|
546 |
\isacommand{definition}\isamarkupfalse% |
|
547 |
\ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}x{\isasymColon}bar{\isacharparenright}\ y\ {\isasymlongleftrightarrow}\ x\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline |
|
548 |
\isanewline |
|
549 |
\isacommand{instance}\isamarkupfalse% |
|
550 |
\ \isacommand{by}\isamarkupfalse% |
|
551 |
\ default\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq{\isacharunderscore}bar{\isacharunderscore}def{\isacharparenright}\isanewline |
|
552 |
\isanewline |
|
553 |
\isacommand{end}\isamarkupfalse% |
|
554 |
% |
|
555 |
\endisatagquote |
|
556 |
{\isafoldquote}% |
|
557 |
% |
|
558 |
\isadelimquote |
|
559 |
% |
|
560 |
\endisadelimquote |
|
561 |
% |
|
562 |
\isadelimquotett |
|
563 |
\ % |
|
564 |
\endisadelimquotett |
|
565 |
% |
|
566 |
\isatagquotett |
|
567 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
|
568 |
\ bar\isanewline |
|
569 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
570 |
\endisatagquotett |
|
571 |
{\isafoldquotett}% |
|
572 |
% |
|
573 |
\isadelimquotett |
|
574 |
% |
|
575 |
\endisadelimquotett |
|
576 |
% |
|
577 |
\begin{isamarkuptext}% |
|
38450 | 578 |
\noindent The code generator would produce an additional instance, |
579 |
which of course is rejected by the \isa{Haskell} compiler. To |
|
580 |
suppress this additional instance, use \isa{code{\isacharunderscore}instance}:% |
|
31050 | 581 |
\end{isamarkuptext}% |
582 |
\isamarkuptrue% |
|
583 |
% |
|
584 |
\isadelimquotett |
|
585 |
% |
|
586 |
\endisadelimquotett |
|
587 |
% |
|
588 |
\isatagquotett |
|
589 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
|
590 |
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline |
|
591 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
|
592 |
\endisatagquotett |
|
593 |
{\isafoldquotett}% |
|
594 |
% |
|
595 |
\isadelimquotett |
|
596 |
% |
|
597 |
\endisadelimquotett |
|
598 |
% |
|
599 |
\isamarkupsubsection{Enhancing the target language context \label{sec:include}% |
|
600 |
} |
|
601 |
\isamarkuptrue% |
|
602 |
% |
|
603 |
\begin{isamarkuptext}% |
|
604 |
In rare cases it is necessary to \emph{enrich} the context of a |
|
38450 | 605 |
target language; this is accomplished using the \hyperlink{command.code-include}{\mbox{\isa{\isacommand{code{\isacharunderscore}include}}}} command:% |
31050 | 606 |
\end{isamarkuptext}% |
607 |
\isamarkuptrue% |
|
608 |
% |
|
609 |
\isadelimquotett |
|
610 |
% |
|
611 |
\endisadelimquotett |
|
612 |
% |
|
613 |
\isatagquotett |
|
614 |
\isacommand{code{\isacharunderscore}include}\isamarkupfalse% |
|
615 |
\ Haskell\ {\isachardoublequoteopen}Errno{\isachardoublequoteclose}\isanewline |
|
616 |
{\isacharverbatimopen}errno\ i\ {\isacharequal}\ error\ {\isacharparenleft}{\isachardoublequote}Error\ number{\isacharcolon}\ {\isachardoublequote}\ {\isacharplus}{\isacharplus}\ show\ i{\isacharparenright}{\isacharverbatimclose}\isanewline |
|
617 |
\isanewline |
|
618 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
619 |
\ Haskell\ Errno% |
|
620 |
\endisatagquotett |
|
621 |
{\isafoldquotett}% |
|
622 |
% |
|
623 |
\isadelimquotett |
|
624 |
% |
|
625 |
\endisadelimquotett |
|
626 |
% |
|
627 |
\begin{isamarkuptext}% |
|
38450 | 628 |
\noindent Such named \isa{include}s are then prepended to every |
629 |
generated code. Inspect such code in order to find out how |
|
630 |
\hyperlink{command.code-include}{\mbox{\isa{\isacommand{code{\isacharunderscore}include}}}} behaves with respect to a particular |
|
631 |
target language.% |
|
31050 | 632 |
\end{isamarkuptext}% |
633 |
\isamarkuptrue% |
|
634 |
% |
|
635 |
\isadelimtheory |
|
636 |
% |
|
637 |
\endisadelimtheory |
|
638 |
% |
|
639 |
\isatagtheory |
|
640 |
\isacommand{end}\isamarkupfalse% |
|
641 |
% |
|
642 |
\endisatagtheory |
|
643 |
{\isafoldtheory}% |
|
644 |
% |
|
645 |
\isadelimtheory |
|
646 |
% |
|
647 |
\endisadelimtheory |
|
648 |
\isanewline |
|
649 |
\end{isabellebody}% |
|
650 |
%%% Local Variables: |
|
651 |
%%% mode: latex |
|
652 |
%%% TeX-master: "root" |
|
653 |
%%% End: |