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