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