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