author | haftmann |
Mon, 27 Sep 2010 16:27:31 +0200 | |
changeset 39745 | 3aa2bc9c5478 |
parent 39712 | 94b1890e4e4a |
child 40351 | 090dac52cfd7 |
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 |
% |
|
39745 | 231 |
\isadelimquotetypewriter |
31050 | 232 |
% |
39745 | 233 |
\endisadelimquotetypewriter |
31050 | 234 |
% |
39745 | 235 |
\isatagquotetypewriter |
31050 | 236 |
% |
237 |
\begin{isamarkuptext}% |
|
39683 | 238 |
structure\ Example\ {\isacharcolon}\ sig\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
239 |
\ \ datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
240 |
\ \ datatype\ boola\ {\isacharequal}\ True\ {\isacharbar}\ False\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
241 |
\ \ val\ conj\ {\isacharcolon}\ boola\ {\isacharminus}{\isachargreater}\ boola\ {\isacharminus}{\isachargreater}\ boola\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
242 |
\ \ val\ less{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ boola\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
243 |
\ \ val\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ boola\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
244 |
\ \ val\ in{\isacharunderscore}interval\ {\isacharcolon}\ nat\ {\isacharasterisk}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ boola\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
245 |
end\ {\isacharequal}\ struct\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
246 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
247 |
datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
248 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
249 |
datatype\ boola\ {\isacharequal}\ True\ {\isacharbar}\ False{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
250 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
251 |
fun\ conj\ p\ True\ {\isacharequal}\ p\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
252 |
\ \ {\isacharbar}\ conj\ p\ False\ {\isacharequal}\ False\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
253 |
\ \ {\isacharbar}\ conj\ True\ p\ {\isacharequal}\ p\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
254 |
\ \ {\isacharbar}\ conj\ False\ p\ {\isacharequal}\ False{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
255 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
256 |
fun\ less{\isacharunderscore}nat\ m\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
257 |
\ \ {\isacharbar}\ less{\isacharunderscore}nat\ n\ Zero{\isacharunderscore}nat\ {\isacharequal}\ False\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
258 |
and\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ less{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
259 |
\ \ {\isacharbar}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ True{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
260 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
261 |
fun\ in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isacharequal}\ conj\ {\isacharparenleft}less{\isacharunderscore}eq{\isacharunderscore}nat\ k\ n{\isacharparenright}\ {\isacharparenleft}less{\isacharunderscore}eq{\isacharunderscore}nat\ n\ l{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
262 |
\isanewline |
39683 | 263 |
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline% |
31050 | 264 |
\end{isamarkuptext}% |
265 |
\isamarkuptrue% |
|
266 |
% |
|
39745 | 267 |
\endisatagquotetypewriter |
268 |
{\isafoldquotetypewriter}% |
|
31050 | 269 |
% |
39745 | 270 |
\isadelimquotetypewriter |
31050 | 271 |
% |
39745 | 272 |
\endisadelimquotetypewriter |
31050 | 273 |
% |
274 |
\begin{isamarkuptext}% |
|
38450 | 275 |
\noindent Though this is correct code, it is a little bit |
276 |
unsatisfactory: boolean values and operators are materialised as |
|
277 |
distinguished entities with have nothing to do with the SML-built-in |
|
278 |
notion of \qt{bool}. This results in less readable code; |
|
279 |
additionally, eager evaluation may cause programs to loop or break |
|
280 |
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 | 281 |
\end{isamarkuptext}% |
282 |
\isamarkuptrue% |
|
283 |
% |
|
39745 | 284 |
\isadelimquotett |
31050 | 285 |
% |
39745 | 286 |
\endisadelimquotett |
31050 | 287 |
% |
39745 | 288 |
\isatagquotett |
31050 | 289 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
290 |
\ bool\isanewline |
|
291 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline |
|
292 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
293 |
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
294 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}% |
|
39745 | 295 |
\endisatagquotett |
296 |
{\isafoldquotett}% |
|
31050 | 297 |
% |
39745 | 298 |
\isadelimquotett |
31050 | 299 |
% |
39745 | 300 |
\endisadelimquotett |
31050 | 301 |
% |
302 |
\begin{isamarkuptext}% |
|
38505 | 303 |
\noindent The \indexdef{}{command}{code\_type}\hypertarget{command.code-type}{\hyperlink{command.code-type}{\mbox{\isa{\isacommand{code{\isacharunderscore}type}}}}} command takes a type constructor |
38450 | 304 |
as arguments together with a list of custom serialisations. Each |
305 |
custom serialisation starts with a target language identifier |
|
306 |
followed by an expression, which during code serialisation is |
|
307 |
inserted whenever the type constructor would occur. For constants, |
|
38505 | 308 |
\indexdef{}{command}{code\_const}\hypertarget{command.code-const}{\hyperlink{command.code-const}{\mbox{\isa{\isacommand{code{\isacharunderscore}const}}}}} implements the corresponding mechanism. Each |
38450 | 309 |
``\verb|_|'' in a serialisation expression is treated as a |
310 |
placeholder for the type constructor's (the constant's) arguments.% |
|
31050 | 311 |
\end{isamarkuptext}% |
312 |
\isamarkuptrue% |
|
313 |
% |
|
39745 | 314 |
\isadelimquotetypewriter |
31050 | 315 |
% |
39745 | 316 |
\endisadelimquotetypewriter |
31050 | 317 |
% |
39745 | 318 |
\isatagquotetypewriter |
31050 | 319 |
% |
320 |
\begin{isamarkuptext}% |
|
39683 | 321 |
structure\ Example\ {\isacharcolon}\ sig\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
322 |
\ \ datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
323 |
\ \ val\ less{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
324 |
\ \ val\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
325 |
\ \ val\ in{\isacharunderscore}interval\ {\isacharcolon}\ nat\ {\isacharasterisk}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
326 |
end\ {\isacharequal}\ struct\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
327 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
328 |
datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
329 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
330 |
fun\ less{\isacharunderscore}nat\ m\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
331 |
\ \ {\isacharbar}\ less{\isacharunderscore}nat\ n\ Zero{\isacharunderscore}nat\ {\isacharequal}\ false\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
332 |
and\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ less{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
333 |
\ \ {\isacharbar}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ true{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
334 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
335 |
fun\ in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}less{\isacharunderscore}eq{\isacharunderscore}nat\ k\ n{\isacharparenright}\ andalso\ {\isacharparenleft}less{\isacharunderscore}eq{\isacharunderscore}nat\ n\ l{\isacharparenright}{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
336 |
\isanewline |
39683 | 337 |
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline% |
31050 | 338 |
\end{isamarkuptext}% |
339 |
\isamarkuptrue% |
|
340 |
% |
|
39745 | 341 |
\endisatagquotetypewriter |
342 |
{\isafoldquotetypewriter}% |
|
31050 | 343 |
% |
39745 | 344 |
\isadelimquotetypewriter |
31050 | 345 |
% |
39745 | 346 |
\endisadelimquotetypewriter |
31050 | 347 |
% |
348 |
\begin{isamarkuptext}% |
|
38450 | 349 |
\noindent This still is not perfect: the parentheses around the |
350 |
\qt{andalso} expression are superfluous. Though the serialiser by |
|
351 |
no means attempts to imitate the rich Isabelle syntax framework, it |
|
352 |
provides some common idioms, notably associative infixes with |
|
353 |
precedences which may be used here:% |
|
31050 | 354 |
\end{isamarkuptext}% |
355 |
\isamarkuptrue% |
|
356 |
% |
|
39745 | 357 |
\isadelimquotett |
31050 | 358 |
% |
39745 | 359 |
\endisadelimquotett |
31050 | 360 |
% |
39745 | 361 |
\isatagquotett |
31050 | 362 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
363 |
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline |
|
364 |
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}% |
|
39745 | 365 |
\endisatagquotett |
366 |
{\isafoldquotett}% |
|
31050 | 367 |
% |
39745 | 368 |
\isadelimquotett |
31050 | 369 |
% |
39745 | 370 |
\endisadelimquotett |
31050 | 371 |
% |
39745 | 372 |
\isadelimquotetypewriter |
31050 | 373 |
% |
39745 | 374 |
\endisadelimquotetypewriter |
31050 | 375 |
% |
39745 | 376 |
\isatagquotetypewriter |
31050 | 377 |
% |
378 |
\begin{isamarkuptext}% |
|
39683 | 379 |
structure\ Example\ {\isacharcolon}\ sig\isanewline |
39664
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
380 |
\ \ datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
381 |
\ \ val\ less{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
382 |
\ \ val\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharcolon}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
383 |
\ \ val\ in{\isacharunderscore}interval\ {\isacharcolon}\ nat\ {\isacharasterisk}\ nat\ {\isacharminus}{\isachargreater}\ nat\ {\isacharminus}{\isachargreater}\ bool\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
384 |
end\ {\isacharequal}\ struct\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
385 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
386 |
datatype\ nat\ {\isacharequal}\ Zero{\isacharunderscore}nat\ {\isacharbar}\ Suc\ of\ nat{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
387 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
388 |
fun\ less{\isacharunderscore}nat\ m\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
389 |
\ \ {\isacharbar}\ less{\isacharunderscore}nat\ n\ Zero{\isacharunderscore}nat\ {\isacharequal}\ false\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
390 |
and\ less{\isacharunderscore}eq{\isacharunderscore}nat\ {\isacharparenleft}Suc\ m{\isacharparenright}\ n\ {\isacharequal}\ less{\isacharunderscore}nat\ m\ n\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
391 |
\ \ {\isacharbar}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ Zero{\isacharunderscore}nat\ n\ {\isacharequal}\ true{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
392 |
\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
393 |
fun\ in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isacharequal}\ less{\isacharunderscore}eq{\isacharunderscore}nat\ k\ n\ andalso\ less{\isacharunderscore}eq{\isacharunderscore}nat\ n\ l{\isacharsemicolon}\isanewline |
0afaf89ab591
more canonical type setting of type writer code examples
haftmann
parents:
39643
diff
changeset
|
394 |
\isanewline |
39683 | 395 |
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline% |
31050 | 396 |
\end{isamarkuptext}% |
397 |
\isamarkuptrue% |
|
398 |
% |
|
39745 | 399 |
\endisatagquotetypewriter |
400 |
{\isafoldquotetypewriter}% |
|
31050 | 401 |
% |
39745 | 402 |
\isadelimquotetypewriter |
31050 | 403 |
% |
39745 | 404 |
\endisadelimquotetypewriter |
31050 | 405 |
% |
406 |
\begin{isamarkuptext}% |
|
38450 | 407 |
\noindent The attentive reader may ask how we assert that no |
408 |
generated code will accidentally overwrite. For this reason the |
|
409 |
serialiser has an internal table of identifiers which have to be |
|
410 |
avoided to be used for new declarations. Initially, this table |
|
411 |
typically contains the keywords of the target language. It can be |
|
412 |
extended manually, thus avoiding accidental overwrites, using the |
|
38505 | 413 |
\indexdef{}{command}{code\_reserved}\hypertarget{command.code-reserved}{\hyperlink{command.code-reserved}{\mbox{\isa{\isacommand{code{\isacharunderscore}reserved}}}}} command:% |
31050 | 414 |
\end{isamarkuptext}% |
415 |
\isamarkuptrue% |
|
416 |
% |
|
417 |
\isadelimquote |
|
418 |
% |
|
419 |
\endisadelimquote |
|
420 |
% |
|
421 |
\isatagquote |
|
422 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
423 |
\ {\isachardoublequoteopen}{\isasymSML}{\isachardoublequoteclose}\ bool\ true\ false\ andalso% |
|
424 |
\endisatagquote |
|
425 |
{\isafoldquote}% |
|
426 |
% |
|
427 |
\isadelimquote |
|
428 |
% |
|
429 |
\endisadelimquote |
|
430 |
% |
|
431 |
\begin{isamarkuptext}% |
|
432 |
\noindent Next, we try to map HOL pairs to SML pairs, using the |
|
433 |
infix ``\verb|*|'' type constructor and parentheses:% |
|
434 |
\end{isamarkuptext}% |
|
435 |
\isamarkuptrue% |
|
436 |
% |
|
437 |
\isadeliminvisible |
|
438 |
% |
|
439 |
\endisadeliminvisible |
|
440 |
% |
|
441 |
\isataginvisible |
|
442 |
% |
|
443 |
\endisataginvisible |
|
444 |
{\isafoldinvisible}% |
|
445 |
% |
|
446 |
\isadeliminvisible |
|
447 |
% |
|
448 |
\endisadeliminvisible |
|
449 |
% |
|
39745 | 450 |
\isadelimquotett |
31050 | 451 |
% |
39745 | 452 |
\endisadelimquotett |
31050 | 453 |
% |
39745 | 454 |
\isatagquotett |
31050 | 455 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
37836 | 456 |
\ prod\isanewline |
31050 | 457 |
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline |
458 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
459 |
\ Pair\isanewline |
|
460 |
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}% |
|
39745 | 461 |
\endisatagquotett |
462 |
{\isafoldquotett}% |
|
31050 | 463 |
% |
39745 | 464 |
\isadelimquotett |
31050 | 465 |
% |
39745 | 466 |
\endisadelimquotett |
31050 | 467 |
% |
468 |
\begin{isamarkuptext}% |
|
469 |
\noindent The initial bang ``\verb|!|'' tells the serialiser |
|
38450 | 470 |
never to put parentheses around the whole expression (they are |
471 |
already present), while the parentheses around argument place |
|
472 |
holders tell not to put parentheses around the arguments. The slash |
|
473 |
``\verb|/|'' (followed by arbitrary white space) inserts a |
|
474 |
space which may be used as a break if necessary during pretty |
|
475 |
printing. |
|
31050 | 476 |
|
38450 | 477 |
These examples give a glimpse what mechanisms custom serialisations |
478 |
provide; however their usage requires careful thinking in order not |
|
479 |
to introduce inconsistencies -- or, in other words: custom |
|
480 |
serialisations are completely axiomatic. |
|
31050 | 481 |
|
39643 | 482 |
A further noteworthy detail is that any special character in a |
38450 | 483 |
custom serialisation may be quoted using ``\verb|'|''; thus, |
484 |
in ``\verb|fn '_ => _|'' the first ``\verb|_|'' is a |
|
485 |
proper underscore while the second ``\verb|_|'' is a |
|
486 |
placeholder.% |
|
31050 | 487 |
\end{isamarkuptext}% |
488 |
\isamarkuptrue% |
|
489 |
% |
|
490 |
\isamarkupsubsection{\isa{Haskell} serialisation% |
|
491 |
} |
|
492 |
\isamarkuptrue% |
|
493 |
% |
|
494 |
\begin{isamarkuptext}% |
|
38450 | 495 |
For convenience, the default \isa{HOL} setup for \isa{Haskell} |
39068 | 496 |
maps the \isa{equal} class to its counterpart in \isa{Haskell}, |
497 |
giving custom serialisations for the class \isa{equal} (by command |
|
39643 | 498 |
\indexdef{}{command}{code\_class}\hypertarget{command.code-class}{\hyperlink{command.code-class}{\mbox{\isa{\isacommand{code{\isacharunderscore}class}}}}}) and its operation \isa{HOL{\isachardot}equal}% |
31050 | 499 |
\end{isamarkuptext}% |
500 |
\isamarkuptrue% |
|
501 |
% |
|
39745 | 502 |
\isadelimquotett |
31050 | 503 |
% |
39745 | 504 |
\endisadelimquotett |
31050 | 505 |
% |
39745 | 506 |
\isatagquotett |
31050 | 507 |
\isacommand{code{\isacharunderscore}class}\isamarkupfalse% |
39068 | 508 |
\ equal\isanewline |
31050 | 509 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}{\isacharparenright}\isanewline |
510 |
\isanewline |
|
511 |
\isacommand{code{\isacharunderscore}const}\isamarkupfalse% |
|
39643 | 512 |
\ {\isachardoublequoteopen}HOL{\isachardot}equal{\isachardoublequoteclose}\isanewline |
31050 | 513 |
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}% |
39745 | 514 |
\endisatagquotett |
515 |
{\isafoldquotett}% |
|
31050 | 516 |
% |
39745 | 517 |
\isadelimquotett |
31050 | 518 |
% |
39745 | 519 |
\endisadelimquotett |
31050 | 520 |
% |
521 |
\begin{isamarkuptext}% |
|
38450 | 522 |
\noindent A problem now occurs whenever a type which is an instance |
39068 | 523 |
of \isa{equal} in \isa{HOL} is mapped on a \isa{Haskell}-built-in type which is also an instance of \isa{Haskell} |
38450 | 524 |
\isa{Eq}:% |
31050 | 525 |
\end{isamarkuptext}% |
526 |
\isamarkuptrue% |
|
527 |
% |
|
528 |
\isadelimquote |
|
529 |
% |
|
530 |
\endisadelimquote |
|
531 |
% |
|
532 |
\isatagquote |
|
533 |
\isacommand{typedecl}\isamarkupfalse% |
|
534 |
\ bar\isanewline |
|
535 |
\isanewline |
|
536 |
\isacommand{instantiation}\isamarkupfalse% |
|
39068 | 537 |
\ bar\ {\isacharcolon}{\isacharcolon}\ equal\isanewline |
31050 | 538 |
\isakeyword{begin}\isanewline |
539 |
\isanewline |
|
540 |
\isacommand{definition}\isamarkupfalse% |
|
39068 | 541 |
\ {\isachardoublequoteopen}HOL{\isachardot}equal\ {\isacharparenleft}x{\isasymColon}bar{\isacharparenright}\ y\ {\isasymlongleftrightarrow}\ x\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline |
31050 | 542 |
\isanewline |
543 |
\isacommand{instance}\isamarkupfalse% |
|
544 |
\ \isacommand{by}\isamarkupfalse% |
|
39068 | 545 |
\ default\ {\isacharparenleft}simp\ add{\isacharcolon}\ equal{\isacharunderscore}bar{\isacharunderscore}def{\isacharparenright}\isanewline |
31050 | 546 |
\isanewline |
547 |
\isacommand{end}\isamarkupfalse% |
|
548 |
% |
|
549 |
\endisatagquote |
|
550 |
{\isafoldquote}% |
|
551 |
% |
|
552 |
\isadelimquote |
|
553 |
% |
|
554 |
\endisadelimquote |
|
555 |
% |
|
39745 | 556 |
\isadelimquotett |
31050 | 557 |
\ % |
39745 | 558 |
\endisadelimquotett |
31050 | 559 |
% |
39745 | 560 |
\isatagquotett |
31050 | 561 |
\isacommand{code{\isacharunderscore}type}\isamarkupfalse% |
562 |
\ bar\isanewline |
|
563 |
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}% |
|
39745 | 564 |
\endisatagquotett |
565 |
{\isafoldquotett}% |
|
31050 | 566 |
% |
39745 | 567 |
\isadelimquotett |
31050 | 568 |
% |
39745 | 569 |
\endisadelimquotett |
31050 | 570 |
% |
571 |
\begin{isamarkuptext}% |
|
38450 | 572 |
\noindent The code generator would produce an additional instance, |
573 |
which of course is rejected by the \isa{Haskell} compiler. To |
|
38509 | 574 |
suppress this additional instance, use \indexdef{}{command}{code\_instance}\hypertarget{command.code-instance}{\hyperlink{command.code-instance}{\mbox{\isa{\isacommand{code{\isacharunderscore}instance}}}}}:% |
31050 | 575 |
\end{isamarkuptext}% |
576 |
\isamarkuptrue% |
|
577 |
% |
|
39745 | 578 |
\isadelimquotett |
31050 | 579 |
% |
39745 | 580 |
\endisadelimquotett |
31050 | 581 |
% |
39745 | 582 |
\isatagquotett |
31050 | 583 |
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse% |
39068 | 584 |
\ bar\ {\isacharcolon}{\isacharcolon}\ equal\isanewline |
31050 | 585 |
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}% |
39745 | 586 |
\endisatagquotett |
587 |
{\isafoldquotett}% |
|
31050 | 588 |
% |
39745 | 589 |
\isadelimquotett |
31050 | 590 |
% |
39745 | 591 |
\endisadelimquotett |
31050 | 592 |
% |
593 |
\isamarkupsubsection{Enhancing the target language context \label{sec:include}% |
|
594 |
} |
|
595 |
\isamarkuptrue% |
|
596 |
% |
|
597 |
\begin{isamarkuptext}% |
|
598 |
In rare cases it is necessary to \emph{enrich} the context of a |
|
38505 | 599 |
target language; this is accomplished using the \indexdef{}{command}{code\_include}\hypertarget{command.code-include}{\hyperlink{command.code-include}{\mbox{\isa{\isacommand{code{\isacharunderscore}include}}}}} command:% |
31050 | 600 |
\end{isamarkuptext}% |
601 |
\isamarkuptrue% |
|
602 |
% |
|
39745 | 603 |
\isadelimquotett |
31050 | 604 |
% |
39745 | 605 |
\endisadelimquotett |
31050 | 606 |
% |
39745 | 607 |
\isatagquotett |
31050 | 608 |
\isacommand{code{\isacharunderscore}include}\isamarkupfalse% |
609 |
\ Haskell\ {\isachardoublequoteopen}Errno{\isachardoublequoteclose}\isanewline |
|
610 |
{\isacharverbatimopen}errno\ i\ {\isacharequal}\ error\ {\isacharparenleft}{\isachardoublequote}Error\ number{\isacharcolon}\ {\isachardoublequote}\ {\isacharplus}{\isacharplus}\ show\ i{\isacharparenright}{\isacharverbatimclose}\isanewline |
|
611 |
\isanewline |
|
612 |
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse% |
|
613 |
\ Haskell\ Errno% |
|
39745 | 614 |
\endisatagquotett |
615 |
{\isafoldquotett}% |
|
31050 | 616 |
% |
39745 | 617 |
\isadelimquotett |
31050 | 618 |
% |
39745 | 619 |
\endisadelimquotett |
31050 | 620 |
% |
621 |
\begin{isamarkuptext}% |
|
38450 | 622 |
\noindent Such named \isa{include}s are then prepended to every |
623 |
generated code. Inspect such code in order to find out how |
|
624 |
\hyperlink{command.code-include}{\mbox{\isa{\isacommand{code{\isacharunderscore}include}}}} behaves with respect to a particular |
|
625 |
target language.% |
|
31050 | 626 |
\end{isamarkuptext}% |
627 |
\isamarkuptrue% |
|
628 |
% |
|
629 |
\isadelimtheory |
|
630 |
% |
|
631 |
\endisadelimtheory |
|
632 |
% |
|
633 |
\isatagtheory |
|
634 |
\isacommand{end}\isamarkupfalse% |
|
635 |
% |
|
636 |
\endisatagtheory |
|
637 |
{\isafoldtheory}% |
|
638 |
% |
|
639 |
\isadelimtheory |
|
640 |
% |
|
641 |
\endisadelimtheory |
|
642 |
\isanewline |
|
643 |
\end{isabellebody}% |
|
644 |
%%% Local Variables: |
|
645 |
%%% mode: latex |
|
646 |
%%% TeX-master: "root" |
|
647 |
%%% End: |