doc-src/IsarAdvanced/Codegen/Thy/Adaption.thy
author haftmann
Wed Oct 15 16:25:31 2008 +0200 (2008-10-15)
changeset 28601 b72589374396
parent 28593 f087237af65d
child 28608 77ffacd6df76
permissions -rw-r--r--
figure for adaption
haftmann@28213
     1
theory Adaption
haftmann@28213
     2
imports Setup
haftmann@28213
     3
begin
haftmann@28213
     4
haftmann@28601
     5
setup %invisible {* Code_Target.extend_target ("\<SML>", ("SML", I)) *}
haftmann@28561
     6
haftmann@28419
     7
section {* Adaption to target languages \label{sec:adaption} *}
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@28561
    41
      whereas for adaption 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@28561
    47
  \noindent However, even if you ought refrain from setting up adaption
haftmann@28561
    48
  yourself, already the @{text "HOL"} comes with some reasonable default
haftmann@28561
    49
  adaptions (say, using target language list syntax).  There also some
haftmann@28561
    50
  common adaption 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@28561
    55
subsection {* The adaption principle *}
haftmann@28561
    56
haftmann@28561
    57
text {*
haftmann@28601
    58
  The following figure illustrates what \qt{adaption} is conceptually
haftmann@28601
    59
  supposed to be:
haftmann@28601
    60
haftmann@28601
    61
  \begin{figure}[here]
haftmann@28601
    62
    \begin{tikzpicture}[scale = 0.5]
haftmann@28601
    63
      \tikzstyle water=[color = blue, thick]
haftmann@28601
    64
      \tikzstyle ice=[color = black, very thick, cap = round, join = round, fill = white]
haftmann@28601
    65
      \tikzstyle process=[color = green, semithick, ->]
haftmann@28601
    66
      \tikzstyle adaption=[color = red, semithick, ->]
haftmann@28601
    67
      \tikzstyle target=[color = black]
haftmann@28601
    68
      \foreach \x in {0, ..., 24}
haftmann@28601
    69
        \draw[style=water] (\x, 0.25) sin + (0.25, 0.25) cos + (0.25, -0.25) sin
haftmann@28601
    70
          + (0.25, -0.25) cos + (0.25, 0.25);
haftmann@28601
    71
      \draw[style=ice] (1, 0) --
haftmann@28601
    72
        (3, 6) node[above, fill=white] {logic} -- (5, 0) -- cycle;
haftmann@28601
    73
      \draw[style=ice] (9, 0) --
haftmann@28601
    74
        (11, 6) node[above, fill=white] {intermediate language} -- (13, 0) -- cycle;
haftmann@28601
    75
      \draw[style=ice] (15, -6) --
haftmann@28601
    76
        (19, 6) node[above, fill=white] {target language} -- (23, -6) -- cycle;
haftmann@28601
    77
      \draw[style=process]
haftmann@28601
    78
        (3.5, 3) .. controls (7, 5) .. node[fill=white] {translation} (10.5, 3);
haftmann@28601
    79
      \draw[style=process]
haftmann@28601
    80
        (11.5, 3) .. controls (15, 5) .. node[fill=white] (serialisation) {serialisation} (18.5, 3);
haftmann@28601
    81
      \node (adaption) at (11, -2) [style=adaption] {adaption};
haftmann@28601
    82
      \node at (19, 3) [rotate=90] {generated};
haftmann@28601
    83
      \node at (19.5, -5) {language};
haftmann@28601
    84
      \node at (19.5, -3) {library};
haftmann@28601
    85
      \node (includes) at (19.5, -1) {includes};
haftmann@28601
    86
      \node (reserved) at (16.5, -3) [rotate=71.57] {reserved};
haftmann@28601
    87
      \draw[style=process]
haftmann@28601
    88
        (includes) -- (serialisation);
haftmann@28601
    89
      \draw[style=process]
haftmann@28601
    90
        (reserved) -- (serialisation);
haftmann@28601
    91
      \draw[style=adaption]
haftmann@28601
    92
        (adaption) -- (serialisation);
haftmann@28601
    93
      \draw[style=adaption]
haftmann@28601
    94
        (adaption) -- (includes);
haftmann@28601
    95
      \draw[style=adaption]
haftmann@28601
    96
        (adaption) -- (reserved);
haftmann@28601
    97
    \end{tikzpicture}
haftmann@28601
    98
    \caption{The adaption principle}
haftmann@28601
    99
    \label{fig:adaption}
haftmann@28601
   100
  \end{figure}
haftmann@28601
   101
haftmann@28601
   102
  \noindent In the tame view, code generation acts as broker between
haftmann@28601
   103
  @{text logic}, @{text "intermediate language"} and
haftmann@28601
   104
  @{text "target language"} by means of @{text translation} and
haftmann@28601
   105
  @{text serialisation};  for the latter, the serialiser has to observe
haftmann@28601
   106
  the structure of the @{text language} itself plus some @{text reserved}
haftmann@28601
   107
  keywords which have to be avoided for generated code.
haftmann@28601
   108
  However, if you consider @{text adaption} mechanisms, the code generated
haftmann@28601
   109
  by the serializer is just the tip of the iceberg:
haftmann@28601
   110
haftmann@28601
   111
  \begin{itemize}
haftmann@28601
   112
    \item parametrise @{text serialisation}
haftmann@28601
   113
    \item @{text library} @{text reserved}
haftmann@28601
   114
    \item @{text "includes"} @{text reserved}
haftmann@28601
   115
  \end{itemize}
haftmann@28561
   116
*}
haftmann@28561
   117
haftmann@28419
   118
subsection {* Common adaption cases *}
haftmann@28419
   119
haftmann@28419
   120
text {*
haftmann@28428
   121
  The @{theory HOL} @{theory Main} theory already provides a code
haftmann@28419
   122
  generator setup
haftmann@28593
   123
  which should be suitable for most applications.  Common extensions
haftmann@28419
   124
  and modifications are available by certain theories of the @{text HOL}
haftmann@28419
   125
  library; beside being useful in applications, they may serve
haftmann@28419
   126
  as a tutorial for customising the code generator setup (see below
haftmann@28419
   127
  \secref{sec:adaption_mechanisms}).
haftmann@28419
   128
haftmann@28419
   129
  \begin{description}
haftmann@28419
   130
haftmann@28419
   131
    \item[@{theory "Code_Integer"}] represents @{text HOL} integers by big
haftmann@28419
   132
       integer literals in target languages.
haftmann@28419
   133
    \item[@{theory "Code_Char"}] represents @{text HOL} characters by 
haftmann@28419
   134
       character literals in target languages.
haftmann@28419
   135
    \item[@{theory "Code_Char_chr"}] like @{text "Code_Char"},
haftmann@28419
   136
       but also offers treatment of character codes; includes
haftmann@28561
   137
       @{theory "Code_Char"}.
haftmann@28419
   138
    \item[@{theory "Efficient_Nat"}] \label{eff_nat} implements natural numbers by integers,
haftmann@28419
   139
       which in general will result in higher efficiency; pattern
haftmann@28419
   140
       matching with @{term "0\<Colon>nat"} / @{const "Suc"}
haftmann@28561
   141
       is eliminated;  includes @{theory "Code_Integer"}
haftmann@28561
   142
       and @{theory "Code_Index"}.
haftmann@28419
   143
    \item[@{theory "Code_Index"}] provides an additional datatype
haftmann@28419
   144
       @{typ index} which is mapped to target-language built-in integers.
haftmann@28419
   145
       Useful for code setups which involve e.g. indexing of
haftmann@28419
   146
       target-language arrays.
haftmann@28419
   147
    \item[@{theory "Code_Message"}] provides an additional datatype
haftmann@28419
   148
       @{typ message_string} which is isomorphic to strings;
haftmann@28419
   149
       @{typ message_string}s are mapped to target-language strings.
haftmann@28419
   150
       Useful for code setups which involve e.g. printing (error) messages.
haftmann@28419
   151
haftmann@28419
   152
  \end{description}
haftmann@28419
   153
haftmann@28419
   154
  \begin{warn}
haftmann@28419
   155
    When importing any of these theories, they should form the last
haftmann@28419
   156
    items in an import list.  Since these theories adapt the
haftmann@28419
   157
    code generator setup in a non-conservative fashion,
haftmann@28419
   158
    strange effects may occur otherwise.
haftmann@28419
   159
  \end{warn}
haftmann@28419
   160
*}
haftmann@28419
   161
haftmann@28419
   162
haftmann@28419
   163
subsection {* Adaption mechanisms \label{sec:adaption_mechanisms} *}
haftmann@28419
   164
haftmann@28419
   165
text {*
haftmann@28561
   166
  Consider the following function and its corresponding
haftmann@28419
   167
  SML code:
haftmann@28419
   168
*}
haftmann@28419
   169
haftmann@28564
   170
primrec %quote in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
haftmann@28419
   171
  "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
haftmann@28447
   172
(*<*)
haftmann@28419
   173
code_type %invisible bool
haftmann@28419
   174
  (SML)
haftmann@28419
   175
code_const %invisible True and False and "op \<and>" and Not
haftmann@28419
   176
  (SML and and and)
haftmann@28447
   177
(*>*)
haftmann@28564
   178
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   179
haftmann@28419
   180
text {*
haftmann@28419
   181
  \noindent Though this is correct code, it is a little bit unsatisfactory:
haftmann@28419
   182
  boolean values and operators are materialised as distinguished
haftmann@28419
   183
  entities with have nothing to do with the SML-built-in notion
haftmann@28419
   184
  of \qt{bool}.  This results in less readable code;
haftmann@28419
   185
  additionally, eager evaluation may cause programs to
haftmann@28419
   186
  loop or break which would perfectly terminate when
haftmann@28419
   187
  the existing SML @{verbatim "bool"} would be used.  To map
haftmann@28419
   188
  the HOL @{typ bool} on SML @{verbatim "bool"}, we may use
haftmann@28419
   189
  \qn{custom serialisations}:
haftmann@28419
   190
*}
haftmann@28419
   191
haftmann@28564
   192
code_type %quotett bool
haftmann@28419
   193
  (SML "bool")
haftmann@28564
   194
code_const %quotett True and False and "op \<and>"
haftmann@28419
   195
  (SML "true" and "false" and "_ andalso _")
haftmann@28213
   196
haftmann@28419
   197
text {*
haftmann@28447
   198
  \noindent The @{command code_type} command takes a type constructor
haftmann@28419
   199
  as arguments together with a list of custom serialisations.
haftmann@28419
   200
  Each custom serialisation starts with a target language
haftmann@28419
   201
  identifier followed by an expression, which during
haftmann@28419
   202
  code serialisation is inserted whenever the type constructor
haftmann@28419
   203
  would occur.  For constants, @{command code_const} implements
haftmann@28419
   204
  the corresponding mechanism.  Each ``@{verbatim "_"}'' in
haftmann@28419
   205
  a serialisation expression is treated as a placeholder
haftmann@28419
   206
  for the type constructor's (the constant's) arguments.
haftmann@28419
   207
*}
haftmann@28419
   208
haftmann@28564
   209
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   210
haftmann@28419
   211
text {*
haftmann@28419
   212
  \noindent This still is not perfect: the parentheses
haftmann@28419
   213
  around the \qt{andalso} expression are superfluous.
haftmann@28593
   214
  Though the serialiser
haftmann@28419
   215
  by no means attempts to imitate the rich Isabelle syntax
haftmann@28419
   216
  framework, it provides some common idioms, notably
haftmann@28419
   217
  associative infixes with precedences which may be used here:
haftmann@28419
   218
*}
haftmann@28419
   219
haftmann@28564
   220
code_const %quotett "op \<and>"
haftmann@28419
   221
  (SML infixl 1 "andalso")
haftmann@28419
   222
haftmann@28564
   223
text %quote {*@{code_stmts in_interval (SML)}*}
haftmann@28419
   224
haftmann@28419
   225
text {*
haftmann@28561
   226
  \noindent The attentive reader may ask how we assert that no generated
haftmann@28561
   227
  code will accidentally overwrite.  For this reason the serialiser has
haftmann@28561
   228
  an internal table of identifiers which have to be avoided to be used
haftmann@28561
   229
  for new declarations.  Initially, this table typically contains the
haftmann@28561
   230
  keywords of the target language.  It can be extended manually, thus avoiding
haftmann@28561
   231
  accidental overwrites, using the @{command "code_reserved"} command:
haftmann@28561
   232
*}
haftmann@28561
   233
haftmann@28601
   234
code_reserved %quote "\<SML>" bool true false andalso
haftmann@28561
   235
haftmann@28561
   236
text {*
haftmann@28447
   237
  \noindent Next, we try to map HOL pairs to SML pairs, using the
haftmann@28419
   238
  infix ``@{verbatim "*"}'' type constructor and parentheses:
haftmann@28419
   239
*}
haftmann@28447
   240
(*<*)
haftmann@28419
   241
code_type %invisible *
haftmann@28419
   242
  (SML)
haftmann@28419
   243
code_const %invisible Pair
haftmann@28419
   244
  (SML)
haftmann@28447
   245
(*>*)
haftmann@28564
   246
code_type %quotett *
haftmann@28419
   247
  (SML infix 2 "*")
haftmann@28564
   248
code_const %quotett Pair
haftmann@28419
   249
  (SML "!((_),/ (_))")
haftmann@28419
   250
haftmann@28419
   251
text {*
haftmann@28593
   252
  \noindent The initial bang ``@{verbatim "!"}'' tells the serialiser
haftmann@28561
   253
  never to put
haftmann@28419
   254
  parentheses around the whole expression (they are already present),
haftmann@28419
   255
  while the parentheses around argument place holders
haftmann@28419
   256
  tell not to put parentheses around the arguments.
haftmann@28419
   257
  The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
haftmann@28419
   258
  inserts a space which may be used as a break if necessary
haftmann@28419
   259
  during pretty printing.
haftmann@28419
   260
haftmann@28419
   261
  These examples give a glimpse what mechanisms
haftmann@28419
   262
  custom serialisations provide; however their usage
haftmann@28419
   263
  requires careful thinking in order not to introduce
haftmann@28419
   264
  inconsistencies -- or, in other words:
haftmann@28419
   265
  custom serialisations are completely axiomatic.
haftmann@28419
   266
haftmann@28419
   267
  A further noteworthy details is that any special
haftmann@28419
   268
  character in a custom serialisation may be quoted
haftmann@28419
   269
  using ``@{verbatim "'"}''; thus, in
haftmann@28419
   270
  ``@{verbatim "fn '_ => _"}'' the first
haftmann@28419
   271
  ``@{verbatim "_"}'' is a proper underscore while the
haftmann@28419
   272
  second ``@{verbatim "_"}'' is a placeholder.
haftmann@28419
   273
*}
haftmann@28419
   274
haftmann@28419
   275
haftmann@28419
   276
subsection {* @{text Haskell} serialisation *}
haftmann@28419
   277
haftmann@28419
   278
text {*
haftmann@28419
   279
  For convenience, the default
haftmann@28419
   280
  @{text HOL} setup for @{text Haskell} maps the @{class eq} class to
haftmann@28419
   281
  its counterpart in @{text Haskell}, giving custom serialisations
haftmann@28419
   282
  for the class @{class eq} (by command @{command code_class}) and its operation
haftmann@28419
   283
  @{const HOL.eq}
haftmann@28419
   284
*}
haftmann@28419
   285
haftmann@28564
   286
code_class %quotett eq
haftmann@28419
   287
  (Haskell "Eq" where "HOL.eq" \<equiv> "(==)")
haftmann@28419
   288
haftmann@28564
   289
code_const %quotett "op ="
haftmann@28419
   290
  (Haskell infixl 4 "==")
haftmann@28419
   291
haftmann@28419
   292
text {*
haftmann@28447
   293
  \noindent A problem now occurs whenever a type which
haftmann@28419
   294
  is an instance of @{class eq} in @{text HOL} is mapped
haftmann@28419
   295
  on a @{text Haskell}-built-in type which is also an instance
haftmann@28419
   296
  of @{text Haskell} @{text Eq}:
haftmann@28419
   297
*}
haftmann@28419
   298
haftmann@28564
   299
typedecl %quote bar
haftmann@28419
   300
haftmann@28564
   301
instantiation %quote bar :: eq
haftmann@28419
   302
begin
haftmann@28419
   303
haftmann@28564
   304
definition %quote "eq_class.eq (x\<Colon>bar) y \<longleftrightarrow> x = y"
haftmann@28419
   305
haftmann@28564
   306
instance %quote by default (simp add: eq_bar_def)
haftmann@28213
   307
haftmann@28564
   308
end %quote
haftmann@28419
   309
haftmann@28564
   310
code_type %quotett bar
haftmann@28419
   311
  (Haskell "Integer")
haftmann@28419
   312
haftmann@28419
   313
text {*
haftmann@28447
   314
  \noindent The code generator would produce
haftmann@28593
   315
  an additional instance, which of course is rejected by the @{text Haskell}
haftmann@28419
   316
  compiler.
haftmann@28419
   317
  To suppress this additional instance, use
haftmann@28419
   318
  @{text "code_instance"}:
haftmann@28419
   319
*}
haftmann@28419
   320
haftmann@28564
   321
code_instance %quotett bar :: eq
haftmann@28419
   322
  (Haskell -)
haftmann@28419
   323
haftmann@28561
   324
haftmann@28561
   325
subsection {* Enhancing the target language context *}
haftmann@28561
   326
haftmann@28561
   327
text {*
haftmann@28593
   328
  In rare cases it is necessary to \emph{enrich} the context of a
haftmann@28561
   329
  target language;  this is accomplished using the @{command "code_include"}
haftmann@28561
   330
  command:
haftmann@28561
   331
*}
haftmann@28561
   332
haftmann@28564
   333
code_include %quotett Haskell "Errno"
haftmann@28561
   334
{*errno i = error ("Error number: " ++ show i)*}
haftmann@28561
   335
haftmann@28564
   336
code_reserved %quotett Haskell Errno
haftmann@28561
   337
haftmann@28561
   338
text {*
haftmann@28561
   339
  \noindent Such named @{text include}s are then prepended to every generated code.
haftmann@28561
   340
  Inspect such code in order to find out how @{command "code_include"} behaves
haftmann@28561
   341
  with respect to a particular target language.
haftmann@28561
   342
*}
haftmann@28561
   343
haftmann@28419
   344
end