src/Doc/Codegen/Adaptation.thy
changeset 69505 cc2d676d5395
parent 69422 472af2d7835d
child 69597 ff784d5a5bfb
equal deleted inserted replaced
69504:bda7527ccf05 69505:cc2d676d5395
    32 
    32 
    33   \begin{itemize}
    33   \begin{itemize}
    34     \item improving readability and aesthetics of generated code
    34     \item improving readability and aesthetics of generated code
    35     \item gaining efficiency
    35     \item gaining efficiency
    36     \item interface with language parts which have no direct counterpart
    36     \item interface with language parts which have no direct counterpart
    37       in @{text "HOL"} (say, imperative data structures)
    37       in \<open>HOL\<close> (say, imperative data structures)
    38   \end{itemize}
    38   \end{itemize}
    39 
    39 
    40   \noindent Generally, you should avoid using those features yourself
    40   \noindent Generally, you should avoid using those features yourself
    41   \emph{at any cost}:
    41   \emph{at any cost}:
    42 
    42 
    53     \item Subtle errors can be introduced unconsciously.
    53     \item Subtle errors can be introduced unconsciously.
    54 
    54 
    55   \end{itemize}
    55   \end{itemize}
    56 
    56 
    57   \noindent However, even if you ought refrain from setting up
    57   \noindent However, even if you ought refrain from setting up
    58   adaptation yourself, already @{text "HOL"} comes with some
    58   adaptation yourself, already \<open>HOL\<close> comes with some
    59   reasonable default adaptations (say, using target language list
    59   reasonable default adaptations (say, using target language list
    60   syntax).  There also some common adaptation cases which you can
    60   syntax).  There also some common adaptation cases which you can
    61   setup by importing particular library theories.  In order to
    61   setup by importing particular library theories.  In order to
    62   understand these, we provide some clues here; these however are not
    62   understand these, we provide some clues here; these however are not
    63   supposed to replace a careful study of the sources.
    63   supposed to replace a careful study of the sources.
   110     \caption{The adaptation principle}
   110     \caption{The adaptation principle}
   111     \label{fig:adaptation}
   111     \label{fig:adaptation}
   112   \end{figure}
   112   \end{figure}
   113 
   113 
   114   \noindent In the tame view, code generation acts as broker between
   114   \noindent In the tame view, code generation acts as broker between
   115   @{text logic}, @{text "intermediate language"} and @{text "target
   115   \<open>logic\<close>, \<open>intermediate language\<close> and \<open>target
   116   language"} by means of @{text translation} and @{text
   116   language\<close> by means of \<open>translation\<close> and \<open>serialisation\<close>; for the latter, the serialiser has to observe the
   117   serialisation}; for the latter, the serialiser has to observe the
   117   structure of the \<open>language\<close> itself plus some \<open>reserved\<close>
   118   structure of the @{text language} itself plus some @{text reserved}
       
   119   keywords which have to be avoided for generated code.  However, if
   118   keywords which have to be avoided for generated code.  However, if
   120   you consider @{text adaptation} mechanisms, the code generated by
   119   you consider \<open>adaptation\<close> mechanisms, the code generated by
   121   the serializer is just the tip of the iceberg:
   120   the serializer is just the tip of the iceberg:
   122 
   121 
   123   \begin{itemize}
   122   \begin{itemize}
   124 
   123 
   125     \item @{text serialisation} can be \emph{parametrised} such that
   124     \item \<open>serialisation\<close> can be \emph{parametrised} such that
   126       logical entities are mapped to target-specific ones
   125       logical entities are mapped to target-specific ones
   127       (e.g. target-specific list syntax, see also
   126       (e.g. target-specific list syntax, see also
   128       \secref{sec:adaptation_mechanisms})
   127       \secref{sec:adaptation_mechanisms})
   129 
   128 
   130     \item Such parametrisations can involve references to a
   129     \item Such parametrisations can involve references to a
   131       target-specific standard @{text library} (e.g. using the @{text
   130       target-specific standard \<open>library\<close> (e.g. using the \<open>Haskell\<close> @{verbatim Maybe} type instead of the \<open>HOL\<close>
   132       Haskell} @{verbatim Maybe} type instead of the @{text HOL}
       
   133       @{type "option"} type); if such are used, the corresponding
   131       @{type "option"} type); if such are used, the corresponding
   134       identifiers (in our example, @{verbatim Maybe}, @{verbatim
   132       identifiers (in our example, @{verbatim Maybe}, @{verbatim
   135       Nothing} and @{verbatim Just}) also have to be considered @{text
   133       Nothing} and @{verbatim Just}) also have to be considered \<open>reserved\<close>.
   136       reserved}.
       
   137 
   134 
   138     \item Even more, the user can enrich the library of the
   135     \item Even more, the user can enrich the library of the
   139       target-language by providing code snippets (\qt{@{text
   136       target-language by providing code snippets (\qt{\<open>includes\<close>}) which are prepended to any generated code (see
   140       "includes"}}) which are prepended to any generated code (see
       
   141       \secref{sec:include}); this typically also involves further
   137       \secref{sec:include}); this typically also involves further
   142       @{text reserved} identifiers.
   138       \<open>reserved\<close> identifiers.
   143 
   139 
   144   \end{itemize}
   140   \end{itemize}
   145 
   141 
   146   \noindent As figure \ref{fig:adaptation} illustrates, all these
   142   \noindent As figure \ref{fig:adaptation} illustrates, all these
   147   adaptation mechanisms have to act consistently; it is at the
   143   adaptation mechanisms have to act consistently; it is at the
   164        types @{typ integer} and @{typ natural} isomorphic to types
   160        types @{typ integer} and @{typ natural} isomorphic to types
   165        @{typ int} and @{typ nat} respectively.  Type @{typ integer}
   161        @{typ int} and @{typ nat} respectively.  Type @{typ integer}
   166        is mapped to target-language built-in integers; @{typ natural}
   162        is mapped to target-language built-in integers; @{typ natural}
   167        is implemented as abstract type over @{typ integer}.
   163        is implemented as abstract type over @{typ integer}.
   168        Useful for code setups which involve e.g.~indexing
   164        Useful for code setups which involve e.g.~indexing
   169        of target-language arrays.  Part of @{text "HOL-Main"}.
   165        of target-language arrays.  Part of \<open>HOL-Main\<close>.
   170 
   166 
   171     \item[@{theory "HOL.String"}] provides an additional datatype @{typ
   167     \item[@{theory "HOL.String"}] provides an additional datatype @{typ
   172        String.literal} which is isomorphic to lists of 7-bit (ASCII) characters;
   168        String.literal} which is isomorphic to lists of 7-bit (ASCII) characters;
   173        @{typ String.literal}s are mapped to target-language strings.
   169        @{typ String.literal}s are mapped to target-language strings.
   174 
   170 
   175        Literal values of type @{typ String.literal} can be written
   171        Literal values of type @{typ String.literal} can be written
   176        as @{text "STR ''\<dots>''"} for sequences of printable characters and
   172        as \<open>STR ''\<dots>''\<close> for sequences of printable characters and
   177        @{text "STR 0x\<dots>"} for one single ASCII code point given
   173        \<open>STR 0x\<dots>\<close> for one single ASCII code point given
   178        as hexadecimal numeral; @{typ String.literal} supports concatenation
   174        as hexadecimal numeral; @{typ String.literal} supports concatenation
   179        @{text "\<dots> + \<dots>"} for all standard target languages.
   175        \<open>\<dots> + \<dots>\<close> for all standard target languages.
   180 
   176 
   181        Note that the particular notion of \qt{string} is target-language
   177        Note that the particular notion of \qt{string} is target-language
   182        specific (sequence of 8-bit units, sequence of unicode code points, \ldots);
   178        specific (sequence of 8-bit units, sequence of unicode code points, \ldots);
   183        hence ASCII is the only reliable common base e.g.~for
   179        hence ASCII is the only reliable common base e.g.~for
   184        printing (error) messages; more sophisticated applications
   180        printing (error) messages; more sophisticated applications
   193        not contain non-ASCII-characters, to safeguard consistency.
   189        not contain non-ASCII-characters, to safeguard consistency.
   194        On top of these, more abstract conversions like @{term_type
   190        On top of these, more abstract conversions like @{term_type
   195        String.explode} and @{term_type String.implode}
   191        String.explode} and @{term_type String.implode}
   196        are implemented.
   192        are implemented.
   197        
   193        
   198        Part of @{text "HOL-Main"}.
   194        Part of \<open>HOL-Main\<close>.
   199 
   195 
   200     \item[@{text "Code_Target_Int"}] implements type @{typ int}
   196     \item[\<open>Code_Target_Int\<close>] implements type @{typ int}
   201        by @{typ integer} and thus by target-language built-in integers.
   197        by @{typ integer} and thus by target-language built-in integers.
   202 
   198 
   203     \item[@{text "Code_Binary_Nat"}] implements type
   199     \item[\<open>Code_Binary_Nat\<close>] implements type
   204        @{typ nat} using a binary rather than a linear representation,
   200        @{typ nat} using a binary rather than a linear representation,
   205        which yields a considerable speedup for computations.
   201        which yields a considerable speedup for computations.
   206        Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated
   202        Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated
   207        by a preprocessor.\label{abstract_nat}
   203        by a preprocessor.\label{abstract_nat}
   208 
   204 
   209     \item[@{text "Code_Target_Nat"}] implements type @{typ nat}
   205     \item[\<open>Code_Target_Nat\<close>] implements type @{typ nat}
   210        by @{typ integer} and thus by target-language built-in integers.
   206        by @{typ integer} and thus by target-language built-in integers.
   211        Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated
   207        Pattern matching with @{term "0::nat"} / @{const "Suc"} is eliminated
   212        by a preprocessor.
   208        by a preprocessor.
   213 
   209 
   214     \item[@{text "Code_Target_Numeral"}] is a convenience theory
   210     \item[\<open>Code_Target_Numeral\<close>] is a convenience theory
   215        containing both @{text "Code_Target_Nat"} and
   211        containing both \<open>Code_Target_Nat\<close> and
   216        @{text "Code_Target_Int"}.
   212        \<open>Code_Target_Int\<close>.
   217 
   213 
   218     \item[@{theory "HOL-Library.IArray"}] provides a type @{typ "'a iarray"}
   214     \item[@{theory "HOL-Library.IArray"}] provides a type @{typ "'a iarray"}
   219        isomorphic to lists but implemented by (effectively immutable)
   215        isomorphic to lists but implemented by (effectively immutable)
   220        arrays \emph{in SML only}.
   216        arrays \emph{in SML only}.
   221 
   217 
   335   proper underscore while the second ``@{verbatim "_"}'' is a
   331   proper underscore while the second ``@{verbatim "_"}'' is a
   336   placeholder.
   332   placeholder.
   337 \<close>
   333 \<close>
   338 
   334 
   339 
   335 
   340 subsection \<open>@{text Haskell} serialisation\<close>
   336 subsection \<open>\<open>Haskell\<close> serialisation\<close>
   341 
   337 
   342 text \<open>
   338 text \<open>
   343   For convenience, the default @{text HOL} setup for @{text Haskell}
   339   For convenience, the default \<open>HOL\<close> setup for \<open>Haskell\<close>
   344   maps the @{class equal} class to its counterpart in @{text Haskell},
   340   maps the @{class equal} class to its counterpart in \<open>Haskell\<close>,
   345   giving custom serialisations for the class @{class equal}
   341   giving custom serialisations for the class @{class equal}
   346   and its operation @{const [source] HOL.equal}.
   342   and its operation @{const [source] HOL.equal}.
   347 \<close>
   343 \<close>
   348 
   344 
   349 code_printing %quotett
   345 code_printing %quotett
   350   type_class equal \<rightharpoonup> (Haskell) "Eq"
   346   type_class equal \<rightharpoonup> (Haskell) "Eq"
   351 | constant HOL.equal \<rightharpoonup> (Haskell) infixl 4 "=="
   347 | constant HOL.equal \<rightharpoonup> (Haskell) infixl 4 "=="
   352 
   348 
   353 text \<open>
   349 text \<open>
   354   \noindent A problem now occurs whenever a type which is an instance
   350   \noindent A problem now occurs whenever a type which is an instance
   355   of @{class equal} in @{text HOL} is mapped on a @{text
   351   of @{class equal} in \<open>HOL\<close> is mapped on a \<open>Haskell\<close>-built-in type which is also an instance of \<open>Haskell\<close>
   356   Haskell}-built-in type which is also an instance of @{text Haskell}
   352   \<open>Eq\<close>:
   357   @{text Eq}:
       
   358 \<close>
   353 \<close>
   359 
   354 
   360 typedecl %quote bar
   355 typedecl %quote bar
   361 
   356 
   362 instantiation %quote bar :: equal
   357 instantiation %quote bar :: equal
   371 (*>*) code_printing %quotett
   366 (*>*) code_printing %quotett
   372   type_constructor bar \<rightharpoonup> (Haskell) "Integer"
   367   type_constructor bar \<rightharpoonup> (Haskell) "Integer"
   373 
   368 
   374 text \<open>
   369 text \<open>
   375   \noindent The code generator would produce an additional instance,
   370   \noindent The code generator would produce an additional instance,
   376   which of course is rejected by the @{text Haskell} compiler.  To
   371   which of course is rejected by the \<open>Haskell\<close> compiler.  To
   377   suppress this additional instance:
   372   suppress this additional instance:
   378 \<close>
   373 \<close>
   379 
   374 
   380 code_printing %quotett
   375 code_printing %quotett
   381   class_instance bar :: "HOL.equal" \<rightharpoonup> (Haskell) -
   376   class_instance bar :: "HOL.equal" \<rightharpoonup> (Haskell) -