src/Doc/Codegen/Adaptation.thy
author haftmann
Fri Feb 15 12:48:20 2013 +0100 (2013-02-15)
changeset 51162 310b94ed1815
parent 51160 599ff65b85e2
child 51171 e8b2d90da499
permissions -rw-r--r--
dropped now obsolete hint;
a few words on theory IArray;
dropped reference to obsolete theory Efficient_Nat
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@38450
   122
  theories of the @{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@51143
   139
    \item[@{text "Code_Binary_Nat"}] \label{eff_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@51143
   143
       by a preprocessor.
haftmann@51143
   144
haftmann@51143
   145
    \item[@{text "Code_Target_Nat"}] implements type @{typ int}
haftmann@51143
   146
       by @{typ integer} and thus by target-language built-in integers;
haftmann@51143
   147
       contains @{text "Code_Binary_Nat"} as a prerequisite.
haftmann@51143
   148
haftmann@51162
   149
    \item[@{text "Code_Target_Numeral"}] is a convenience theory
haftmann@51143
   150
       containing both @{text "Code_Target_Nat"} and
haftmann@51143
   151
       @{text "Code_Target_Int"}.
haftmann@38450
   152
haftmann@51143
   153
    \item[@{text "Code_Char"}] represents @{text HOL} characters by
haftmann@51143
   154
       character literals in target languages.
haftmann@38450
   155
haftmann@38450
   156
    \item[@{theory "String"}] provides an additional datatype @{typ
haftmann@38450
   157
       String.literal} which is isomorphic to strings; @{typ
haftmann@38450
   158
       String.literal}s are mapped to target-language strings.  Useful
haftmann@38450
   159
       for code setups which involve e.g.~printing (error) messages.
haftmann@46519
   160
       Part of @{text "HOL-Main"}.
haftmann@28419
   161
haftmann@51162
   162
    \item[@{theory "IArray"}] provides a type @{typ "'a iarray"}
haftmann@51162
   163
       isomorphic to lists but implemented by (effectively immutable)
haftmann@51162
   164
       arrays \emph{in SML only}.
haftmann@28419
   165
haftmann@51162
   166
  \end{description}
haftmann@28419
   167
*}
haftmann@28419
   168
haftmann@28419
   169
haftmann@31050
   170
subsection {* Parametrising serialisation \label{sec:adaptation_mechanisms} *}
haftmann@28419
   171
haftmann@28419
   172
text {*
haftmann@38450
   173
  Consider the following function and its corresponding SML code:
haftmann@28419
   174
*}
haftmann@28419
   175
haftmann@28564
   176
primrec %quote in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
haftmann@28419
   177
  "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
haftmann@28447
   178
(*<*)
haftmann@28419
   179
code_type %invisible bool
haftmann@28419
   180
  (SML)
haftmann@28419
   181
code_const %invisible True and False and "op \<and>" and Not
haftmann@28419
   182
  (SML and and and)
haftmann@28447
   183
(*>*)
haftmann@39745
   184
text %quotetypewriter {*
haftmann@39683
   185
  @{code_stmts in_interval (SML)}
haftmann@39664
   186
*}
haftmann@28419
   187
haftmann@28419
   188
text {*
haftmann@38450
   189
  \noindent Though this is correct code, it is a little bit
haftmann@38450
   190
  unsatisfactory: boolean values and operators are materialised as
haftmann@38450
   191
  distinguished entities with have nothing to do with the SML-built-in
haftmann@38450
   192
  notion of \qt{bool}.  This results in less readable code;
haftmann@38450
   193
  additionally, eager evaluation may cause programs to loop or break
haftmann@38450
   194
  which would perfectly terminate when the existing SML @{verbatim
haftmann@38450
   195
  "bool"} would be used.  To map the HOL @{typ bool} on SML @{verbatim
haftmann@38450
   196
  "bool"}, we may use \qn{custom serialisations}:
haftmann@28419
   197
*}
haftmann@28419
   198
haftmann@39745
   199
code_type %quotett bool
haftmann@28419
   200
  (SML "bool")
haftmann@39745
   201
code_const %quotett True and False and "op \<and>"
haftmann@28419
   202
  (SML "true" and "false" and "_ andalso _")
haftmann@28213
   203
haftmann@28419
   204
text {*
haftmann@38505
   205
  \noindent The @{command_def code_type} command takes a type constructor
haftmann@38450
   206
  as arguments together with a list of custom serialisations.  Each
haftmann@38450
   207
  custom serialisation starts with a target language identifier
haftmann@38450
   208
  followed by an expression, which during code serialisation is
haftmann@38450
   209
  inserted whenever the type constructor would occur.  For constants,
haftmann@38505
   210
  @{command_def code_const} implements the corresponding mechanism.  Each
haftmann@38450
   211
  ``@{verbatim "_"}'' in a serialisation expression is treated as a
haftmann@38450
   212
  placeholder for the type constructor's (the constant's) arguments.
haftmann@28419
   213
*}
haftmann@28419
   214
haftmann@39745
   215
text %quotetypewriter {*
haftmann@39683
   216
  @{code_stmts in_interval (SML)}
haftmann@39664
   217
*}
haftmann@28419
   218
haftmann@28419
   219
text {*
haftmann@38450
   220
  \noindent This still is not perfect: the parentheses around the
haftmann@38450
   221
  \qt{andalso} expression are superfluous.  Though the serialiser by
haftmann@38450
   222
  no means attempts to imitate the rich Isabelle syntax framework, it
haftmann@38450
   223
  provides some common idioms, notably associative infixes with
haftmann@38450
   224
  precedences which may be used here:
haftmann@28419
   225
*}
haftmann@28419
   226
haftmann@39745
   227
code_const %quotett "op \<and>"
haftmann@28419
   228
  (SML infixl 1 "andalso")
haftmann@28419
   229
haftmann@39745
   230
text %quotetypewriter {*
haftmann@39683
   231
  @{code_stmts in_interval (SML)}
haftmann@39664
   232
*}
haftmann@28419
   233
haftmann@28419
   234
text {*
haftmann@38450
   235
  \noindent The attentive reader may ask how we assert that no
haftmann@38450
   236
  generated code will accidentally overwrite.  For this reason the
haftmann@38450
   237
  serialiser has an internal table of identifiers which have to be
haftmann@38450
   238
  avoided to be used for new declarations.  Initially, this table
haftmann@38450
   239
  typically contains the keywords of the target language.  It can be
haftmann@38450
   240
  extended manually, thus avoiding accidental overwrites, using the
haftmann@38505
   241
  @{command_def "code_reserved"} command:
haftmann@28561
   242
*}
haftmann@28561
   243
haftmann@40351
   244
code_reserved %quote "\<SMLdummy>" bool true false andalso
haftmann@28561
   245
haftmann@28561
   246
text {*
haftmann@28447
   247
  \noindent Next, we try to map HOL pairs to SML pairs, using the
haftmann@28419
   248
  infix ``@{verbatim "*"}'' type constructor and parentheses:
haftmann@28419
   249
*}
haftmann@28447
   250
(*<*)
haftmann@37836
   251
code_type %invisible prod
haftmann@28419
   252
  (SML)
haftmann@28419
   253
code_const %invisible Pair
haftmann@28419
   254
  (SML)
haftmann@28447
   255
(*>*)
haftmann@39745
   256
code_type %quotett prod
haftmann@28419
   257
  (SML infix 2 "*")
haftmann@39745
   258
code_const %quotett Pair
haftmann@28419
   259
  (SML "!((_),/ (_))")
haftmann@28419
   260
haftmann@28419
   261
text {*
haftmann@28593
   262
  \noindent The initial bang ``@{verbatim "!"}'' tells the serialiser
haftmann@38450
   263
  never to put parentheses around the whole expression (they are
haftmann@38450
   264
  already present), while the parentheses around argument place
haftmann@38450
   265
  holders tell not to put parentheses around the arguments.  The slash
haftmann@38450
   266
  ``@{verbatim "/"}'' (followed by arbitrary white space) inserts a
haftmann@38450
   267
  space which may be used as a break if necessary during pretty
haftmann@38450
   268
  printing.
haftmann@28419
   269
haftmann@38450
   270
  These examples give a glimpse what mechanisms custom serialisations
haftmann@38450
   271
  provide; however their usage requires careful thinking in order not
haftmann@38450
   272
  to introduce inconsistencies -- or, in other words: custom
haftmann@38450
   273
  serialisations are completely axiomatic.
haftmann@28419
   274
haftmann@39643
   275
  A further noteworthy detail is that any special character in a
haftmann@38450
   276
  custom serialisation may be quoted using ``@{verbatim "'"}''; thus,
haftmann@38450
   277
  in ``@{verbatim "fn '_ => _"}'' the first ``@{verbatim "_"}'' is a
haftmann@38450
   278
  proper underscore while the second ``@{verbatim "_"}'' is a
haftmann@38450
   279
  placeholder.
haftmann@28419
   280
*}
haftmann@28419
   281
haftmann@28419
   282
haftmann@28419
   283
subsection {* @{text Haskell} serialisation *}
haftmann@28419
   284
haftmann@28419
   285
text {*
haftmann@38450
   286
  For convenience, the default @{text HOL} setup for @{text Haskell}
haftmann@39063
   287
  maps the @{class equal} class to its counterpart in @{text Haskell},
haftmann@39063
   288
  giving custom serialisations for the class @{class equal} (by command
haftmann@39643
   289
  @{command_def code_class}) and its operation @{const [source] HOL.equal}
haftmann@28419
   290
*}
haftmann@28419
   291
haftmann@39745
   292
code_class %quotett equal
haftmann@28714
   293
  (Haskell "Eq")
haftmann@28419
   294
haftmann@39745
   295
code_const %quotett "HOL.equal"
haftmann@28419
   296
  (Haskell infixl 4 "==")
haftmann@28419
   297
haftmann@28419
   298
text {*
haftmann@38450
   299
  \noindent A problem now occurs whenever a type which is an instance
haftmann@39063
   300
  of @{class equal} in @{text HOL} is mapped on a @{text
haftmann@38450
   301
  Haskell}-built-in type which is also an instance of @{text Haskell}
haftmann@38450
   302
  @{text Eq}:
haftmann@28419
   303
*}
haftmann@28419
   304
haftmann@28564
   305
typedecl %quote bar
haftmann@28419
   306
haftmann@39063
   307
instantiation %quote bar :: equal
haftmann@28419
   308
begin
haftmann@28419
   309
haftmann@39063
   310
definition %quote "HOL.equal (x\<Colon>bar) y \<longleftrightarrow> x = y"
haftmann@28419
   311
haftmann@39063
   312
instance %quote by default (simp add: equal_bar_def)
haftmann@28213
   313
haftmann@30880
   314
end %quote (*<*)
haftmann@30880
   315
haftmann@39745
   316
(*>*) code_type %quotett bar
haftmann@28419
   317
  (Haskell "Integer")
haftmann@28419
   318
haftmann@28419
   319
text {*
haftmann@38450
   320
  \noindent The code generator would produce an additional instance,
haftmann@38450
   321
  which of course is rejected by the @{text Haskell} compiler.  To
haftmann@38506
   322
  suppress this additional instance, use @{command_def "code_instance"}:
haftmann@28419
   323
*}
haftmann@28419
   324
haftmann@39745
   325
code_instance %quotett bar :: equal
haftmann@28419
   326
  (Haskell -)
haftmann@28419
   327
haftmann@28561
   328
haftmann@28635
   329
subsection {* Enhancing the target language context \label{sec:include} *}
haftmann@28561
   330
haftmann@28561
   331
text {*
haftmann@28593
   332
  In rare cases it is necessary to \emph{enrich} the context of a
haftmann@38505
   333
  target language; this is accomplished using the @{command_def
haftmann@38450
   334
  "code_include"} command:
haftmann@28561
   335
*}
haftmann@28561
   336
haftmann@39745
   337
code_include %quotett Haskell "Errno"
haftmann@28561
   338
{*errno i = error ("Error number: " ++ show i)*}
haftmann@28561
   339
haftmann@39745
   340
code_reserved %quotett Haskell Errno
haftmann@28561
   341
haftmann@28561
   342
text {*
haftmann@38450
   343
  \noindent Such named @{text include}s are then prepended to every
haftmann@38450
   344
  generated code.  Inspect such code in order to find out how
haftmann@38450
   345
  @{command "code_include"} behaves with respect to a particular
haftmann@38450
   346
  target language.
haftmann@28561
   347
*}
haftmann@28561
   348
haftmann@28419
   349
end
haftmann@46519
   350