doc-src/IsarAdvanced/Codegen/Thy/Adaption.thy
changeset 28419 f65e8b318581
parent 28213 b52f9205a02d
child 28420 293b166c45c5
equal deleted inserted replaced
28418:4ffb62675ade 28419:f65e8b318581
     1 theory Adaption
     1 theory Adaption
     2 imports Setup
     2 imports Setup
     3 begin
     3 begin
     4 
     4 
     5 section {* Adaption to target languages *}
     5 section {* Adaption to target languages \label{sec:adaption} *}
     6 
     6 
     7 subsection {* \ldots *}
     7 subsection {* Common adaption cases *}
       
     8 
       
     9 text {*
       
    10   The @{text HOL} @{text Main} theory already provides a code
       
    11   generator setup
       
    12   which should be suitable for most applications. Common extensions
       
    13   and modifications are available by certain theories of the @{text HOL}
       
    14   library; beside being useful in applications, they may serve
       
    15   as a tutorial for customising the code generator setup (see below
       
    16   \secref{sec:adaption_mechanisms}).
       
    17 
       
    18   \begin{description}
       
    19 
       
    20     \item[@{theory "Code_Integer"}] represents @{text HOL} integers by big
       
    21        integer literals in target languages.
       
    22     \item[@{theory "Code_Char"}] represents @{text HOL} characters by 
       
    23        character literals in target languages.
       
    24     \item[@{theory "Code_Char_chr"}] like @{text "Code_Char"},
       
    25        but also offers treatment of character codes; includes
       
    26        @{theory "Code_Char_chr"}.
       
    27     \item[@{theory "Efficient_Nat"}] \label{eff_nat} implements natural numbers by integers,
       
    28        which in general will result in higher efficiency; pattern
       
    29        matching with @{term "0\<Colon>nat"} / @{const "Suc"}
       
    30        is eliminated;  includes @{theory "Code_Integer"}.
       
    31     \item[@{theory "Code_Index"}] provides an additional datatype
       
    32        @{typ index} which is mapped to target-language built-in integers.
       
    33        Useful for code setups which involve e.g. indexing of
       
    34        target-language arrays.
       
    35     \item[@{theory "Code_Message"}] provides an additional datatype
       
    36        @{typ message_string} which is isomorphic to strings;
       
    37        @{typ message_string}s are mapped to target-language strings.
       
    38        Useful for code setups which involve e.g. printing (error) messages.
       
    39 
       
    40   \end{description}
       
    41 
       
    42   \begin{warn}
       
    43     When importing any of these theories, they should form the last
       
    44     items in an import list.  Since these theories adapt the
       
    45     code generator setup in a non-conservative fashion,
       
    46     strange effects may occur otherwise.
       
    47   \end{warn}
       
    48 *}
       
    49 
       
    50 
       
    51 subsection {* Adaption mechanisms \label{sec:adaption_mechanisms} *}
       
    52 
       
    53 text {*
       
    54   \begin{warn}
       
    55     The mechanisms shown here are especially for the curious;  the user
       
    56     rarely needs to do anything on his own beyond the defaults in @{text HOL}.
       
    57     Adaption is a delicated task which requires a lot of dilligence since
       
    58     it happend \emph{completely} outside the logic.
       
    59   \end{warn}
       
    60 *}
       
    61 
       
    62 text {*
       
    63   Consider the following function and its corresponding
       
    64   SML code:
       
    65 *}
       
    66 
       
    67 primrec %quoteme in_interval :: "nat \<times> nat \<Rightarrow> nat \<Rightarrow> bool" where
       
    68   "in_interval (k, l) n \<longleftrightarrow> k \<le> n \<and> n \<le> l"
       
    69 
       
    70 code_type %invisible bool
       
    71   (SML)
       
    72 code_const %invisible True and False and "op \<and>" and Not
       
    73   (SML and and and)
       
    74 
       
    75 text %quoteme {*@{code_stmts in_interval (SML)}*}
       
    76 
       
    77 text {*
       
    78   \noindent Though this is correct code, it is a little bit unsatisfactory:
       
    79   boolean values and operators are materialised as distinguished
       
    80   entities with have nothing to do with the SML-built-in notion
       
    81   of \qt{bool}.  This results in less readable code;
       
    82   additionally, eager evaluation may cause programs to
       
    83   loop or break which would perfectly terminate when
       
    84   the existing SML @{verbatim "bool"} would be used.  To map
       
    85   the HOL @{typ bool} on SML @{verbatim "bool"}, we may use
       
    86   \qn{custom serialisations}:
       
    87 *}
       
    88 
       
    89 code_type %tt bool
       
    90   (SML "bool")
       
    91 code_const %tt True and False and "op \<and>"
       
    92   (SML "true" and "false" and "_ andalso _")
       
    93 
       
    94 text {*
       
    95   The @{command code_type} command takes a type constructor
       
    96   as arguments together with a list of custom serialisations.
       
    97   Each custom serialisation starts with a target language
       
    98   identifier followed by an expression, which during
       
    99   code serialisation is inserted whenever the type constructor
       
   100   would occur.  For constants, @{command code_const} implements
       
   101   the corresponding mechanism.  Each ``@{verbatim "_"}'' in
       
   102   a serialisation expression is treated as a placeholder
       
   103   for the type constructor's (the constant's) arguments.
       
   104 *}
       
   105 
       
   106 text %quoteme {*@{code_stmts in_interval (SML)}*}
       
   107 
       
   108 text {*
       
   109   \lstsml{Thy/examples/bool_mlbool.ML}
       
   110 
       
   111   \noindent This still is not perfect: the parentheses
       
   112   around the \qt{andalso} expression are superfluous.
       
   113   Though the serializer
       
   114   by no means attempts to imitate the rich Isabelle syntax
       
   115   framework, it provides some common idioms, notably
       
   116   associative infixes with precedences which may be used here:
       
   117 *}
       
   118 
       
   119 code_const %tt "op \<and>"
       
   120   (SML infixl 1 "andalso")
       
   121 
       
   122 text %quoteme {*@{code_stmts in_interval (SML)}*}
       
   123 
       
   124 text {*
       
   125   Next, we try to map HOL pairs to SML pairs, using the
       
   126   infix ``@{verbatim "*"}'' type constructor and parentheses:
       
   127 *}
       
   128 
       
   129 code_type %invisible *
       
   130   (SML)
       
   131 code_const %invisible Pair
       
   132   (SML)
       
   133 
       
   134 code_type %tt *
       
   135   (SML infix 2 "*")
       
   136 code_const %tt Pair
       
   137   (SML "!((_),/ (_))")
       
   138 
       
   139 text {*
       
   140   The initial bang ``@{verbatim "!"}'' tells the serializer to never put
       
   141   parentheses around the whole expression (they are already present),
       
   142   while the parentheses around argument place holders
       
   143   tell not to put parentheses around the arguments.
       
   144   The slash ``@{verbatim "/"}'' (followed by arbitrary white space)
       
   145   inserts a space which may be used as a break if necessary
       
   146   during pretty printing.
       
   147 
       
   148   These examples give a glimpse what mechanisms
       
   149   custom serialisations provide; however their usage
       
   150   requires careful thinking in order not to introduce
       
   151   inconsistencies -- or, in other words:
       
   152   custom serialisations are completely axiomatic.
       
   153 
       
   154   A further noteworthy details is that any special
       
   155   character in a custom serialisation may be quoted
       
   156   using ``@{verbatim "'"}''; thus, in
       
   157   ``@{verbatim "fn '_ => _"}'' the first
       
   158   ``@{verbatim "_"}'' is a proper underscore while the
       
   159   second ``@{verbatim "_"}'' is a placeholder.
       
   160 
       
   161   The HOL theories provide further
       
   162   examples for custom serialisations.
       
   163 *}
       
   164 
       
   165 
       
   166 subsection {* @{text Haskell} serialisation *}
       
   167 
       
   168 text {*
       
   169   For convenience, the default
       
   170   @{text HOL} setup for @{text Haskell} maps the @{class eq} class to
       
   171   its counterpart in @{text Haskell}, giving custom serialisations
       
   172   for the class @{class eq} (by command @{command code_class}) and its operation
       
   173   @{const HOL.eq}
       
   174 *}
       
   175 
       
   176 code_class %tt eq
       
   177   (Haskell "Eq" where "HOL.eq" \<equiv> "(==)")
       
   178 
       
   179 code_const %tt "op ="
       
   180   (Haskell infixl 4 "==")
       
   181 
       
   182 text {*
       
   183   A problem now occurs whenever a type which
       
   184   is an instance of @{class eq} in @{text HOL} is mapped
       
   185   on a @{text Haskell}-built-in type which is also an instance
       
   186   of @{text Haskell} @{text Eq}:
       
   187 *}
       
   188 
       
   189 typedecl %quoteme bar
       
   190 
       
   191 instantiation %quoteme bar :: eq
       
   192 begin
       
   193 
       
   194 definition %quoteme "eq_class.eq (x\<Colon>bar) y \<longleftrightarrow> x = y"
       
   195 
       
   196 instance %quoteme by default (simp add: eq_bar_def)
     8 
   197 
     9 end
   198 end
       
   199 
       
   200 code_type %tt bar
       
   201   (Haskell "Integer")
       
   202 
       
   203 text {*
       
   204   The code generator would produce
       
   205   an additional instance, which of course is rejectedby the @{text Haskell}
       
   206   compiler.
       
   207   To suppress this additional instance, use
       
   208   @{text "code_instance"}:
       
   209 *}
       
   210 
       
   211 code_instance %tt bar :: eq
       
   212   (Haskell -)
       
   213 
       
   214 end