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