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