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