doc-src/Codegen/Thy/Adaptation.thy
author haftmann
Wed May 06 16:01:07 2009 +0200 (2009-05-06)
changeset 31050 555b56b66fcf
parent 30882 doc-src/Codegen/Thy/Adaption.thy@d15725e84091
child 31205 98370b26c2ce
permissions -rw-r--r--
adaptation replaces adaption
haftmann@31050
     1
theory Adaptation
haftmann@28213
     2
imports Setup
haftmann@28213
     3
begin
haftmann@28213
     4
haftmann@28679
     5
setup %invisible {* Code_Target.extend_target ("\<SML>", ("SML", K I)) *}
haftmann@28561
     6
haftmann@31050
     7
section {* Adaptation to target languages \label{sec:adaptation} *}
haftmann@28419
     8
haftmann@28561
     9
subsection {* Adapting code generation *}
haftmann@28561
    10
haftmann@28561
    11
text {*
haftmann@28561
    12
  The aspects of code generation introduced so far have two aspects
haftmann@28561
    13
  in common:
haftmann@28561
    14
haftmann@28561
    15
  \begin{itemize}
haftmann@28561
    16
    \item They act uniformly, without reference to a specific
haftmann@28561
    17
       target language.
haftmann@28561
    18
    \item They are \emph{safe} in the sense that as long as you trust
haftmann@28561
    19
       the code generator meta theory and implementation, you cannot
haftmann@28561
    20
       produce programs that yield results which are not derivable
haftmann@28561
    21
       in the logic.
haftmann@28561
    22
  \end{itemize}
haftmann@28561
    23
haftmann@28561
    24
  \noindent In this section we will introduce means to \emph{adapt} the serialiser
haftmann@28561
    25
  to a specific target language, i.e.~to print program fragments
haftmann@28593
    26
  in a way which accommodates \qt{already existing} ingredients of
haftmann@28561
    27
  a target language environment, for three reasons:
haftmann@28561
    28
haftmann@28561
    29
  \begin{itemize}
haftmann@28593
    30
    \item improving readability and aesthetics of generated code
haftmann@28561
    31
    \item gaining efficiency
haftmann@28561
    32
    \item interface with language parts which have no direct counterpart
haftmann@28561
    33
      in @{text "HOL"} (say, imperative data structures)
haftmann@28561
    34
  \end{itemize}
haftmann@28561
    35
haftmann@28561
    36
  \noindent Generally, you should avoid using those features yourself
haftmann@28561
    37
  \emph{at any cost}:
haftmann@28561
    38
haftmann@28561
    39
  \begin{itemize}
haftmann@28561
    40
    \item The safe configuration methods act uniformly on every target language,
haftmann@31050
    41
      whereas for adaptation you have to treat each target language separate.
haftmann@28561
    42
    \item Application is extremely tedious since there is no abstraction
haftmann@28593
    43
      which would allow for a static check, making it easy to produce garbage.
haftmann@28593
    44
    \item More or less subtle errors can be introduced unconsciously.
haftmann@28561
    45
  \end{itemize}
haftmann@28561
    46
haftmann@31050
    47
  \noindent However, even if you ought refrain from setting up adaptation
haftmann@28561
    48
  yourself, already the @{text "HOL"} comes with some reasonable default
haftmann@31050
    49
  adaptations (say, using target language list syntax).  There also some
haftmann@31050
    50
  common adaptation cases which you can setup by importing particular
haftmann@28561
    51
  library theories.  In order to understand these, we provide some clues here;
haftmann@28561
    52
  these however are not supposed to replace a careful study of the sources.
haftmann@28561
    53
*}
haftmann@28561
    54
haftmann@31050
    55
subsection {* The adaptation principle *}
haftmann@28561
    56
haftmann@28561
    57
text {*
haftmann@31050
    58
  Figure \ref{fig:adaptation} illustrates what \qt{adaptation} is conceptually
haftmann@28601
    59
  supposed to be:
haftmann@28601
    60
haftmann@28601
    61
  \begin{figure}[here]
haftmann@31050
    62
    \includegraphics{adaptation}
haftmann@31050
    63
    \caption{The adaptation principle}
haftmann@31050
    64
    \label{fig:adaptation}
haftmann@28601
    65
  \end{figure}
haftmann@28601
    66
haftmann@28601
    67
  \noindent In the tame view, code generation acts as broker between
haftmann@28601
    68
  @{text logic}, @{text "intermediate language"} and
haftmann@28601
    69
  @{text "target language"} by means of @{text translation} and
haftmann@28601
    70
  @{text serialisation};  for the latter, the serialiser has to observe
haftmann@28601
    71
  the structure of the @{text language} itself plus some @{text reserved}
haftmann@28601
    72
  keywords which have to be avoided for generated code.
haftmann@31050
    73
  However, if you consider @{text adaptation} mechanisms, the code generated
haftmann@28601
    74
  by the serializer is just the tip of the iceberg:
haftmann@28601
    75
haftmann@28601
    76
  \begin{itemize}
haftmann@28635
    77
    \item @{text serialisation} can be \emph{parametrised} such that
haftmann@28635
    78
      logical entities are mapped to target-specific ones
haftmann@28635
    79
      (e.g. target-specific list syntax,
haftmann@31050
    80
        see also \secref{sec:adaptation_mechanisms})
haftmann@28635
    81
    \item Such parametrisations can involve references to a
haftmann@28635
    82
      target-specific standard @{text library} (e.g. using
haftmann@28635
    83
      the @{text Haskell} @{verbatim Maybe} type instead
haftmann@28635
    84
      of the @{text HOL} @{type "option"} type);
haftmann@28635
    85
      if such are used, the corresponding identifiers
haftmann@28635
    86
      (in our example, @{verbatim Maybe}, @{verbatim Nothing}
haftmann@28635
    87
      and @{verbatim Just}) also have to be considered @{text reserved}.
haftmann@28635
    88
    \item Even more, the user can enrich the library of the
haftmann@28635
    89
      target-language by providing code snippets
haftmann@28635
    90
      (\qt{@{text "includes"}}) which are prepended to
haftmann@28635
    91
      any generated code (see \secref{sec:include});  this typically
haftmann@28635
    92
      also involves further @{text reserved} identifiers.
haftmann@28601
    93
  \end{itemize}
haftmann@28635
    94
haftmann@31050
    95
  \noindent As figure \ref{fig:adaptation} illustrates, all these adaptation mechanisms
haftmann@28635
    96
  have to act consistently;  it is at the discretion of the user
haftmann@28635
    97
  to take care for this.
haftmann@28561
    98
*}
haftmann@28561
    99
haftmann@31050
   100
subsection {* Common adaptation patterns *}
haftmann@28419
   101
haftmann@28419
   102
text {*
haftmann@28428
   103
  The @{theory HOL} @{theory Main} theory already provides a code
haftmann@28419
   104
  generator setup
haftmann@28593
   105
  which should be suitable for most applications.  Common extensions
haftmann@28419
   106
  and modifications are available by certain theories of the @{text HOL}
haftmann@28419
   107
  library; beside being useful in applications, they may serve
haftmann@28419
   108
  as a tutorial for customising the code generator setup (see below
haftmann@31050
   109
  \secref{sec:adaptation_mechanisms}).
haftmann@28419
   110
haftmann@28419
   111
  \begin{description}
haftmann@28419
   112
haftmann@28419
   113
    \item[@{theory "Code_Integer"}] represents @{text HOL} integers by big
haftmann@28419
   114
       integer literals in target languages.
haftmann@28419
   115
    \item[@{theory "Code_Char"}] represents @{text HOL} characters by 
haftmann@28419
   116
       character literals in target languages.
haftmann@28419
   117
    \item[@{theory "Code_Char_chr"}] like @{text "Code_Char"},
haftmann@28419
   118
       but also offers treatment of character codes; includes
haftmann@28561
   119
       @{theory "Code_Char"}.
haftmann@28419
   120
    \item[@{theory "Efficient_Nat"}] \label{eff_nat} implements natural numbers by integers,
haftmann@28419
   121
       which in general will result in higher efficiency; pattern
haftmann@28419
   122
       matching with @{term "0\<Colon>nat"} / @{const "Suc"}
haftmann@28561
   123
       is eliminated;  includes @{theory "Code_Integer"}
haftmann@28561
   124
       and @{theory "Code_Index"}.
haftmann@28419
   125
    \item[@{theory "Code_Index"}] provides an additional datatype
haftmann@28419
   126
       @{typ index} which is mapped to target-language built-in integers.
haftmann@28419
   127
       Useful for code setups which involve e.g. indexing of
haftmann@28419
   128
       target-language arrays.
haftmann@31050
   129
    \item[@{theory "String"}] provides an additional datatype
haftmann@28419
   130
       @{typ message_string} which is isomorphic to strings;
haftmann@28419
   131
       @{typ message_string}s are mapped to target-language strings.
haftmann@28419
   132
       Useful for code setups which involve e.g. printing (error) messages.
haftmann@28419
   133
haftmann@28419
   134
  \end{description}
haftmann@28419
   135
haftmann@28419
   136
  \begin{warn}
haftmann@28419
   137
    When importing any of these theories, they should form the last
haftmann@28419
   138
    items in an import list.  Since these theories adapt the
haftmann@28419
   139
    code generator setup in a non-conservative fashion,
haftmann@28419
   140
    strange effects may occur otherwise.
haftmann@28419
   141
  \end{warn}
haftmann@28419
   142
*}
haftmann@28419
   143
haftmann@28419
   144
haftmann@31050
   145
subsection {* Parametrising serialisation \label{sec:adaptation_mechanisms} *}
haftmann@28419
   146
haftmann@28419
   147
text {*
haftmann@28561
   148
  Consider the following function and its corresponding
haftmann@28419
   149
  SML code:
haftmann@28419
   150
*}
haftmann@28419
   151
haftmann@28564
   152
primrec %quote in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
haftmann@28419
   153
  "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
haftmann@28447
   154
(*<*)
haftmann@28419
   155
code_type %invisible bool
haftmann@28419
   156
  (SML)
haftmann@28419
   157
code_const %invisible True and False and "op \<and>" and Not
haftmann@28419
   158
  (SML and and and)
haftmann@28447
   159
(*>*)
haftmann@28564
   160
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   161
haftmann@28419
   162
text {*
haftmann@28419
   163
  \noindent Though this is correct code, it is a little bit unsatisfactory:
haftmann@28419
   164
  boolean values and operators are materialised as distinguished
haftmann@28419
   165
  entities with have nothing to do with the SML-built-in notion
haftmann@28419
   166
  of \qt{bool}.  This results in less readable code;
haftmann@28419
   167
  additionally, eager evaluation may cause programs to
haftmann@28419
   168
  loop or break which would perfectly terminate when
haftmann@28419
   169
  the existing SML @{verbatim "bool"} would be used.  To map
haftmann@28419
   170
  the HOL @{typ bool} on SML @{verbatim "bool"}, we may use
haftmann@28419
   171
  \qn{custom serialisations}:
haftmann@28419
   172
*}
haftmann@28419
   173
haftmann@28564
   174
code_type %quotett bool
haftmann@28419
   175
  (SML "bool")
haftmann@28564
   176
code_const %quotett True and False and "op \<and>"
haftmann@28419
   177
  (SML "true" and "false" and "_ andalso _")
haftmann@28213
   178
haftmann@28419
   179
text {*
haftmann@28447
   180
  \noindent The @{command code_type} command takes a type constructor
haftmann@28419
   181
  as arguments together with a list of custom serialisations.
haftmann@28419
   182
  Each custom serialisation starts with a target language
haftmann@28419
   183
  identifier followed by an expression, which during
haftmann@28419
   184
  code serialisation is inserted whenever the type constructor
haftmann@28419
   185
  would occur.  For constants, @{command code_const} implements
haftmann@28419
   186
  the corresponding mechanism.  Each ``@{verbatim "_"}'' in
haftmann@28419
   187
  a serialisation expression is treated as a placeholder
haftmann@28419
   188
  for the type constructor's (the constant's) arguments.
haftmann@28419
   189
*}
haftmann@28419
   190
haftmann@28564
   191
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   192
haftmann@28419
   193
text {*
haftmann@28419
   194
  \noindent This still is not perfect: the parentheses
haftmann@28419
   195
  around the \qt{andalso} expression are superfluous.
haftmann@28593
   196
  Though the serialiser
haftmann@28419
   197
  by no means attempts to imitate the rich Isabelle syntax
haftmann@28419
   198
  framework, it provides some common idioms, notably
haftmann@28419
   199
  associative infixes with precedences which may be used here:
haftmann@28419
   200
*}
haftmann@28419
   201
haftmann@28564
   202
code_const %quotett "op \<and>"
haftmann@28419
   203
  (SML infixl 1 "andalso")
haftmann@28419
   204
haftmann@28564
   205
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   206
haftmann@28419
   207
text {*
haftmann@28561
   208
  \noindent The attentive reader may ask how we assert that no generated
haftmann@28561
   209
  code will accidentally overwrite.  For this reason the serialiser has
haftmann@28561
   210
  an internal table of identifiers which have to be avoided to be used
haftmann@28561
   211
  for new declarations.  Initially, this table typically contains the
haftmann@28561
   212
  keywords of the target language.  It can be extended manually, thus avoiding
haftmann@28561
   213
  accidental overwrites, using the @{command "code_reserved"} command:
haftmann@28561
   214
*}
haftmann@28561
   215
haftmann@28601
   216
code_reserved %quote "\<SML>" bool true false andalso
haftmann@28561
   217
haftmann@28561
   218
text {*
haftmann@28447
   219
  \noindent Next, we try to map HOL pairs to SML pairs, using the
haftmann@28419
   220
  infix ``@{verbatim "*"}'' type constructor and parentheses:
haftmann@28419
   221
*}
haftmann@28447
   222
(*<*)
haftmann@28419
   223
code_type %invisible *
haftmann@28419
   224
  (SML)
haftmann@28419
   225
code_const %invisible Pair
haftmann@28419
   226
  (SML)
haftmann@28447
   227
(*>*)
haftmann@28564
   228
code_type %quotett *
haftmann@28419
   229
  (SML infix 2 "*")
haftmann@28564
   230
code_const %quotett Pair
haftmann@28419
   231
  (SML "!((_),/ (_))")
haftmann@28419
   232
haftmann@28419
   233
text {*
haftmann@28593
   234
  \noindent The initial bang ``@{verbatim "!"}'' tells the serialiser
haftmann@28561
   235
  never to put
haftmann@28419
   236
  parentheses around the whole expression (they are already present),
haftmann@28419
   237
  while the parentheses around argument place holders
haftmann@28419
   238
  tell not to put parentheses around the arguments.
haftmann@28419
   239
  The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
haftmann@28419
   240
  inserts a space which may be used as a break if necessary
haftmann@28419
   241
  during pretty printing.
haftmann@28419
   242
haftmann@28419
   243
  These examples give a glimpse what mechanisms
haftmann@28419
   244
  custom serialisations provide; however their usage
haftmann@28419
   245
  requires careful thinking in order not to introduce
haftmann@28419
   246
  inconsistencies -- or, in other words:
haftmann@28419
   247
  custom serialisations are completely axiomatic.
haftmann@28419
   248
haftmann@28419
   249
  A further noteworthy details is that any special
haftmann@28419
   250
  character in a custom serialisation may be quoted
haftmann@28419
   251
  using ``@{verbatim "'"}''; thus, in
haftmann@28419
   252
  ``@{verbatim "fn '_ => _"}'' the first
haftmann@28419
   253
  ``@{verbatim "_"}'' is a proper underscore while the
haftmann@28419
   254
  second ``@{verbatim "_"}'' is a placeholder.
haftmann@28419
   255
*}
haftmann@28419
   256
haftmann@28419
   257
haftmann@28419
   258
subsection {* @{text Haskell} serialisation *}
haftmann@28419
   259
haftmann@28419
   260
text {*
haftmann@28419
   261
  For convenience, the default
haftmann@28419
   262
  @{text HOL} setup for @{text Haskell} maps the @{class eq} class to
haftmann@28419
   263
  its counterpart in @{text Haskell}, giving custom serialisations
haftmann@28419
   264
  for the class @{class eq} (by command @{command code_class}) and its operation
haftmann@28419
   265
  @{const HOL.eq}
haftmann@28419
   266
*}
haftmann@28419
   267
haftmann@28564
   268
code_class %quotett eq
haftmann@28714
   269
  (Haskell "Eq")
haftmann@28419
   270
haftmann@28564
   271
code_const %quotett "op ="
haftmann@28419
   272
  (Haskell infixl 4 "==")
haftmann@28419
   273
haftmann@28419
   274
text {*
haftmann@28447
   275
  \noindent A problem now occurs whenever a type which
haftmann@28419
   276
  is an instance of @{class eq} in @{text HOL} is mapped
haftmann@28419
   277
  on a @{text Haskell}-built-in type which is also an instance
haftmann@28419
   278
  of @{text Haskell} @{text Eq}:
haftmann@28419
   279
*}
haftmann@28419
   280
haftmann@28564
   281
typedecl %quote bar
haftmann@28419
   282
haftmann@28564
   283
instantiation %quote bar :: eq
haftmann@28419
   284
begin
haftmann@28419
   285
haftmann@28564
   286
definition %quote "eq_class.eq (x\<Colon>bar) y \<longleftrightarrow> x = y"
haftmann@28419
   287
haftmann@28564
   288
instance %quote by default (simp add: eq_bar_def)
haftmann@28213
   289
haftmann@30880
   290
end %quote (*<*)
haftmann@30880
   291
haftmann@30880
   292
(*>*) code_type %quotett bar
haftmann@28419
   293
  (Haskell "Integer")
haftmann@28419
   294
haftmann@28419
   295
text {*
haftmann@28447
   296
  \noindent The code generator would produce
haftmann@28593
   297
  an additional instance, which of course is rejected by the @{text Haskell}
haftmann@28419
   298
  compiler.
haftmann@28419
   299
  To suppress this additional instance, use
haftmann@28419
   300
  @{text "code_instance"}:
haftmann@28419
   301
*}
haftmann@28419
   302
haftmann@28564
   303
code_instance %quotett bar :: eq
haftmann@28419
   304
  (Haskell -)
haftmann@28419
   305
haftmann@28561
   306
haftmann@28635
   307
subsection {* Enhancing the target language context \label{sec:include} *}
haftmann@28561
   308
haftmann@28561
   309
text {*
haftmann@28593
   310
  In rare cases it is necessary to \emph{enrich} the context of a
haftmann@28561
   311
  target language;  this is accomplished using the @{command "code_include"}
haftmann@28561
   312
  command:
haftmann@28561
   313
*}
haftmann@28561
   314
haftmann@28564
   315
code_include %quotett Haskell "Errno"
haftmann@28561
   316
{*errno i = error ("Error number: " ++ show i)*}
haftmann@28561
   317
haftmann@28564
   318
code_reserved %quotett Haskell Errno
haftmann@28561
   319
haftmann@28561
   320
text {*
haftmann@28561
   321
  \noindent Such named @{text include}s are then prepended to every generated code.
haftmann@28561
   322
  Inspect such code in order to find out how @{command "code_include"} behaves
haftmann@28561
   323
  with respect to a particular target language.
haftmann@28561
   324
*}
haftmann@28561
   325
haftmann@28419
   326
end