doc-src/Codegen/Thy/document/Further.tex
author haftmann
Tue, 07 Sep 2010 16:58:01 +0200
changeset 39440 985b13c5a61d
parent 39304 352bcd845998
child 39802 e7d4923b9b1c
permissions -rw-r--r--
updated generated document
     1 %
     2 \begin{isabellebody}%
     3 \def\isabellecontext{Further}%
     4 %
     5 \isadelimtheory
     6 %
     7 \endisadelimtheory
     8 %
     9 \isatagtheory
    10 \isacommand{theory}\isamarkupfalse%
    11 \ Further\isanewline
    12 \isakeyword{imports}\ Setup\isanewline
    13 \isakeyword{begin}%
    14 \endisatagtheory
    15 {\isafoldtheory}%
    16 %
    17 \isadelimtheory
    18 %
    19 \endisadelimtheory
    20 %
    21 \isamarkupsection{Further issues \label{sec:further}%
    22 }
    23 \isamarkuptrue%
    24 %
    25 \isamarkupsubsection{Modules namespace%
    26 }
    27 \isamarkuptrue%
    28 %
    29 \begin{isamarkuptext}%
    30 When invoking the \hyperlink{command.export-code}{\mbox{\isa{\isacommand{export{\isacharunderscore}code}}}} command it is possible to leave
    31   out the \hyperlink{keyword.module-name}{\mbox{\isa{\isakeyword{module{\isacharunderscore}name}}}} part;  then code is distributed over
    32   different modules, where the module name space roughly is induced
    33   by the Isabelle theory name space.
    34 
    35   Then sometimes the awkward situation occurs that dependencies between
    36   definitions introduce cyclic dependencies between modules, which in the
    37   \isa{Haskell} world leaves you to the mercy of the \isa{Haskell} implementation
    38   you are using,  while for \isa{SML}/\isa{OCaml} code generation is not possible.
    39 
    40   A solution is to declare module names explicitly.
    41   Let use assume the three cyclically dependent
    42   modules are named \emph{A}, \emph{B} and \emph{C}.
    43   Then, by stating%
    44 \end{isamarkuptext}%
    45 \isamarkuptrue%
    46 %
    47 \isadelimquote
    48 %
    49 \endisadelimquote
    50 %
    51 \isatagquote
    52 \isacommand{code{\isacharunderscore}modulename}\isamarkupfalse%
    53 \ SML\isanewline
    54 \ \ A\ ABC\isanewline
    55 \ \ B\ ABC\isanewline
    56 \ \ C\ ABC%
    57 \endisatagquote
    58 {\isafoldquote}%
    59 %
    60 \isadelimquote
    61 %
    62 \endisadelimquote
    63 %
    64 \begin{isamarkuptext}%
    65 \noindent
    66   we explicitly map all those modules on \emph{ABC},
    67   resulting in an ad-hoc merge of this three modules
    68   at serialisation time.%
    69 \end{isamarkuptext}%
    70 \isamarkuptrue%
    71 %
    72 \isamarkupsubsection{Locales and interpretation%
    73 }
    74 \isamarkuptrue%
    75 %
    76 \begin{isamarkuptext}%
    77 A technical issue comes to surface when generating code from
    78   specifications stemming from locale interpretation.
    79 
    80   Let us assume a locale specifying a power operation
    81   on arbitrary types:%
    82 \end{isamarkuptext}%
    83 \isamarkuptrue%
    84 %
    85 \isadelimquote
    86 %
    87 \endisadelimquote
    88 %
    89 \isatagquote
    90 \isacommand{locale}\isamarkupfalse%
    91 \ power\ {\isacharequal}\isanewline
    92 \ \ \isakeyword{fixes}\ power\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}b{\isachardoublequoteclose}\isanewline
    93 \ \ \isakeyword{assumes}\ power{\isacharunderscore}commute{\isacharcolon}\ {\isachardoublequoteopen}power\ x\ {\isasymcirc}\ power\ y\ {\isacharequal}\ power\ y\ {\isasymcirc}\ power\ x{\isachardoublequoteclose}\isanewline
    94 \isakeyword{begin}%
    95 \endisatagquote
    96 {\isafoldquote}%
    97 %
    98 \isadelimquote
    99 %
   100 \endisadelimquote
   101 %
   102 \begin{isamarkuptext}%
   103 \noindent Inside that locale we can lift \isa{power} to exponent lists
   104   by means of specification relative to that locale:%
   105 \end{isamarkuptext}%
   106 \isamarkuptrue%
   107 %
   108 \isadelimquote
   109 %
   110 \endisadelimquote
   111 %
   112 \isatagquote
   113 \isacommand{primrec}\isamarkupfalse%
   114 \ powers\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharprime}b{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
   115 \ \ {\isachardoublequoteopen}powers\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ id{\isachardoublequoteclose}\isanewline
   116 {\isacharbar}\ {\isachardoublequoteopen}powers\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ {\isacharequal}\ power\ x\ {\isasymcirc}\ powers\ xs{\isachardoublequoteclose}\isanewline
   117 \isanewline
   118 \isacommand{lemma}\isamarkupfalse%
   119 \ powers{\isacharunderscore}append{\isacharcolon}\isanewline
   120 \ \ {\isachardoublequoteopen}powers\ {\isacharparenleft}xs\ {\isacharat}\ ys{\isacharparenright}\ {\isacharequal}\ powers\ xs\ {\isasymcirc}\ powers\ ys{\isachardoublequoteclose}\isanewline
   121 \ \ \isacommand{by}\isamarkupfalse%
   122 \ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
   123 \isanewline
   124 \isacommand{lemma}\isamarkupfalse%
   125 \ powers{\isacharunderscore}power{\isacharcolon}\isanewline
   126 \ \ {\isachardoublequoteopen}powers\ xs\ {\isasymcirc}\ power\ x\ {\isacharequal}\ power\ x\ {\isasymcirc}\ powers\ xs{\isachardoublequoteclose}\isanewline
   127 \ \ \isacommand{by}\isamarkupfalse%
   128 \ {\isacharparenleft}induct\ xs{\isacharparenright}\isanewline
   129 \ \ \ \ {\isacharparenleft}simp{\isacharunderscore}all\ del{\isacharcolon}\ o{\isacharunderscore}apply\ id{\isacharunderscore}apply\ add{\isacharcolon}\ o{\isacharunderscore}assoc\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharcomma}\isanewline
   130 \ \ \ \ \ \ simp\ del{\isacharcolon}\ o{\isacharunderscore}apply\ add{\isacharcolon}\ o{\isacharunderscore}assoc\ power{\isacharunderscore}commute{\isacharparenright}\isanewline
   131 \isanewline
   132 \isacommand{lemma}\isamarkupfalse%
   133 \ powers{\isacharunderscore}rev{\isacharcolon}\isanewline
   134 \ \ {\isachardoublequoteopen}powers\ {\isacharparenleft}rev\ xs{\isacharparenright}\ {\isacharequal}\ powers\ xs{\isachardoublequoteclose}\isanewline
   135 \ \ \ \ \isacommand{by}\isamarkupfalse%
   136 \ {\isacharparenleft}induct\ xs{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ powers{\isacharunderscore}append\ powers{\isacharunderscore}power{\isacharparenright}\isanewline
   137 \isanewline
   138 \isacommand{end}\isamarkupfalse%
   139 %
   140 \endisatagquote
   141 {\isafoldquote}%
   142 %
   143 \isadelimquote
   144 %
   145 \endisadelimquote
   146 %
   147 \begin{isamarkuptext}%
   148 After an interpretation of this locale (say, \indexdef{}{command}{interpretation}\hypertarget{command.interpretation}{\hyperlink{command.interpretation}{\mbox{\isa{\isacommand{interpretation}}}}} \isa{fun{\isacharunderscore}power{\isacharcolon}} \isa{{\isachardoublequote}power\ {\isacharparenleft}{\isasymlambda}n\ {\isacharparenleft}f\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}{\isachardot}\ f\ {\isacharcircum}{\isacharcircum}\ n{\isacharparenright}{\isachardoublequote}}), one would expect to have a constant \isa{fun{\isacharunderscore}power{\isachardot}powers\ {\isacharcolon}{\isacharcolon}\ nat\ list\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a} for which code
   149   can be generated.  But this not the case: internally, the term
   150   \isa{fun{\isacharunderscore}power{\isachardot}powers} is an abbreviation for the foundational
   151   term \isa{{\isachardoublequote}power{\isachardot}powers\ {\isacharparenleft}{\isasymlambda}n\ {\isacharparenleft}f\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}{\isachardot}\ f\ {\isacharcircum}{\isacharcircum}\ n{\isacharparenright}{\isachardoublequote}}
   152   (see \cite{isabelle-locale} for the details behind).
   153 
   154   Fortunately, with minor effort the desired behaviour can be achieved.
   155   First, a dedicated definition of the constant on which the local \isa{powers}
   156   after interpretation is supposed to be mapped on:%
   157 \end{isamarkuptext}%
   158 \isamarkuptrue%
   159 %
   160 \isadelimquote
   161 %
   162 \endisadelimquote
   163 %
   164 \isatagquote
   165 \isacommand{definition}\isamarkupfalse%
   166 \ funpows\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ list\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
   167 \ \ {\isacharbrackleft}code\ del{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}funpows\ {\isacharequal}\ power{\isachardot}powers\ {\isacharparenleft}{\isasymlambda}n\ f{\isachardot}\ f\ {\isacharcircum}{\isacharcircum}\ n{\isacharparenright}{\isachardoublequoteclose}%
   168 \endisatagquote
   169 {\isafoldquote}%
   170 %
   171 \isadelimquote
   172 %
   173 \endisadelimquote
   174 %
   175 \begin{isamarkuptext}%
   176 \noindent In general, the pattern is \isa{c\ {\isacharequal}\ t} where \isa{c} is
   177   the name of the future constant and \isa{t} the foundational term
   178   corresponding to the local constant after interpretation.
   179 
   180   The interpretation itself is enriched with an equation \isa{t\ {\isacharequal}\ c}:%
   181 \end{isamarkuptext}%
   182 \isamarkuptrue%
   183 %
   184 \isadelimquote
   185 %
   186 \endisadelimquote
   187 %
   188 \isatagquote
   189 \isacommand{interpretation}\isamarkupfalse%
   190 \ fun{\isacharunderscore}power{\isacharcolon}\ power\ {\isachardoublequoteopen}{\isasymlambda}n\ {\isacharparenleft}f\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}{\isachardot}\ f\ {\isacharcircum}{\isacharcircum}\ n{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
   191 \ \ {\isachardoublequoteopen}power{\isachardot}powers\ {\isacharparenleft}{\isasymlambda}n\ f{\isachardot}\ f\ {\isacharcircum}{\isacharcircum}\ n{\isacharparenright}\ {\isacharequal}\ funpows{\isachardoublequoteclose}\isanewline
   192 \ \ \isacommand{by}\isamarkupfalse%
   193 \ unfold{\isacharunderscore}locales\isanewline
   194 \ \ \ \ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ expand{\isacharunderscore}fun{\isacharunderscore}eq\ funpow{\isacharunderscore}mult\ mult{\isacharunderscore}commute\ funpows{\isacharunderscore}def{\isacharparenright}%
   195 \endisatagquote
   196 {\isafoldquote}%
   197 %
   198 \isadelimquote
   199 %
   200 \endisadelimquote
   201 %
   202 \begin{isamarkuptext}%
   203 \noindent This additional equation is trivially proved by the definition
   204   itself.
   205 
   206   After this setup procedure, code generation can continue as usual:%
   207 \end{isamarkuptext}%
   208 \isamarkuptrue%
   209 %
   210 \isadelimquote
   211 %
   212 \endisadelimquote
   213 %
   214 \isatagquote
   215 %
   216 \begin{isamarkuptext}%
   217 \isatypewriter%
   218 \noindent%
   219 \hspace*{0pt}funpow ::~forall a.~Nat -> (a -> a) -> a -> a;\\
   220 \hspace*{0pt}funpow Zero{\char95}nat f = id;\\
   221 \hspace*{0pt}funpow (Suc n) f = f .~funpow n f;\\
   222 \hspace*{0pt}\\
   223 \hspace*{0pt}funpows ::~forall a.~[Nat] -> (a -> a) -> a -> a;\\
   224 \hspace*{0pt}funpows [] = id;\\
   225 \hspace*{0pt}funpows (x :~xs) = funpow x .~funpows xs;%
   226 \end{isamarkuptext}%
   227 \isamarkuptrue%
   228 %
   229 \endisatagquote
   230 {\isafoldquote}%
   231 %
   232 \isadelimquote
   233 %
   234 \endisadelimquote
   235 %
   236 \isamarkupsubsection{Imperative data structures%
   237 }
   238 \isamarkuptrue%
   239 %
   240 \begin{isamarkuptext}%
   241 If you consider imperative data structures as inevitable for a
   242   specific application, you should consider \emph{Imperative
   243   Functional Programming with Isabelle/HOL}
   244   \cite{bulwahn-et-al:2008:imperative}; the framework described there
   245   is available in session \isa{Imperative{\isacharunderscore}HOL}.%
   246 \end{isamarkuptext}%
   247 \isamarkuptrue%
   248 %
   249 \isamarkupsubsection{ML system interfaces \label{sec:ml}%
   250 }
   251 \isamarkuptrue%
   252 %
   253 \begin{isamarkuptext}%
   254 Since the code generator framework not only aims to provide a nice
   255   Isar interface but also to form a base for code-generation-based
   256   applications, here a short description of the most fundamental ML
   257   interfaces.%
   258 \end{isamarkuptext}%
   259 \isamarkuptrue%
   260 %
   261 \isamarkupsubsubsection{Managing executable content%
   262 }
   263 \isamarkuptrue%
   264 %
   265 \isadelimmlref
   266 %
   267 \endisadelimmlref
   268 %
   269 \isatagmlref
   270 %
   271 \begin{isamarkuptext}%
   272 \begin{mldecls}
   273   \indexdef{}{ML}{Code.read\_const}\verb|Code.read_const: theory -> string -> string| \\
   274   \indexdef{}{ML}{Code.add\_eqn}\verb|Code.add_eqn: thm -> theory -> theory| \\
   275   \indexdef{}{ML}{Code.del\_eqn}\verb|Code.del_eqn: thm -> theory -> theory| \\
   276   \indexdef{}{ML}{Code\_Preproc.map\_pre}\verb|Code_Preproc.map_pre: (simpset -> simpset) -> theory -> theory| \\
   277   \indexdef{}{ML}{Code\_Preproc.map\_post}\verb|Code_Preproc.map_post: (simpset -> simpset) -> theory -> theory| \\
   278   \indexdef{}{ML}{Code\_Preproc.add\_functrans}\verb|Code_Preproc.add_functrans: |\isasep\isanewline%
   279 \verb|    string * (theory -> (thm * bool) list -> (thm * bool) list option)|\isasep\isanewline%
   280 \verb|      -> theory -> theory| \\
   281   \indexdef{}{ML}{Code\_Preproc.del\_functrans}\verb|Code_Preproc.del_functrans: string -> theory -> theory| \\
   282   \indexdef{}{ML}{Code.add\_datatype}\verb|Code.add_datatype: (string * typ) list -> theory -> theory| \\
   283   \indexdef{}{ML}{Code.get\_type}\verb|Code.get_type: theory -> string|\isasep\isanewline%
   284 \verb|    -> (string * sort) list * ((string * string list) * typ list) list| \\
   285   \indexdef{}{ML}{Code.get\_type\_of\_constr\_or\_abstr}\verb|Code.get_type_of_constr_or_abstr: theory -> string -> (string * bool) option|
   286   \end{mldecls}
   287 
   288   \begin{description}
   289 
   290   \item \verb|Code.read_const|~\isa{thy}~\isa{s}
   291      reads a constant as a concrete term expression \isa{s}.
   292 
   293   \item \verb|Code.add_eqn|~\isa{thm}~\isa{thy} adds function
   294      theorem \isa{thm} to executable content.
   295 
   296   \item \verb|Code.del_eqn|~\isa{thm}~\isa{thy} removes function
   297      theorem \isa{thm} from executable content, if present.
   298 
   299   \item \verb|Code_Preproc.map_pre|~\isa{f}~\isa{thy} changes
   300      the preprocessor simpset.
   301 
   302   \item \verb|Code_Preproc.add_functrans|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
   303      function transformer \isa{f} (named \isa{name}) to executable content;
   304      \isa{f} is a transformer of the code equations belonging
   305      to a certain function definition, depending on the
   306      current theory context.  Returning \isa{NONE} indicates that no
   307      transformation took place;  otherwise, the whole process will be iterated
   308      with the new code equations.
   309 
   310   \item \verb|Code_Preproc.del_functrans|~\isa{name}~\isa{thy} removes
   311      function transformer named \isa{name} from executable content.
   312 
   313   \item \verb|Code.add_datatype|~\isa{cs}~\isa{thy} adds
   314      a datatype to executable content, with generation
   315      set \isa{cs}.
   316 
   317   \item \verb|Code.get_type_of_constr_or_abstr|~\isa{thy}~\isa{const}
   318      returns type constructor corresponding to
   319      constructor \isa{const}; returns \isa{NONE}
   320      if \isa{const} is no constructor.
   321 
   322   \end{description}%
   323 \end{isamarkuptext}%
   324 \isamarkuptrue%
   325 %
   326 \endisatagmlref
   327 {\isafoldmlref}%
   328 %
   329 \isadelimmlref
   330 %
   331 \endisadelimmlref
   332 %
   333 \isamarkupsubsubsection{Data depending on the theory's executable content%
   334 }
   335 \isamarkuptrue%
   336 %
   337 \begin{isamarkuptext}%
   338 Implementing code generator applications on top
   339   of the framework set out so far usually not only
   340   involves using those primitive interfaces
   341   but also storing code-dependent data and various
   342   other things.
   343 
   344   Due to incrementality of code generation, changes in the
   345   theory's executable content have to be propagated in a
   346   certain fashion.  Additionally, such changes may occur
   347   not only during theory extension but also during theory
   348   merge, which is a little bit nasty from an implementation
   349   point of view.  The framework provides a solution
   350   to this technical challenge by providing a functorial
   351   data slot \verb|Code_Data|; on instantiation
   352   of this functor, the following types and operations
   353   are required:
   354 
   355   \medskip
   356   \begin{tabular}{l}
   357   \isa{type\ T} \\
   358   \isa{val\ empty{\isacharcolon}\ T} \\
   359   \end{tabular}
   360 
   361   \begin{description}
   362 
   363   \item \isa{T} the type of data to store.
   364 
   365   \item \isa{empty} initial (empty) data.
   366 
   367   \end{description}
   368 
   369   \noindent An instance of \verb|Code_Data| provides the following
   370   interface:
   371 
   372   \medskip
   373   \begin{tabular}{l}
   374   \isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\
   375   \isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T}
   376   \end{tabular}
   377 
   378   \begin{description}
   379 
   380   \item \isa{change} update of current data (cached!)
   381     by giving a continuation.
   382 
   383   \item \isa{change{\isacharunderscore}yield} update with side result.
   384 
   385   \end{description}%
   386 \end{isamarkuptext}%
   387 \isamarkuptrue%
   388 %
   389 \isadelimtheory
   390 %
   391 \endisadelimtheory
   392 %
   393 \isatagtheory
   394 \isacommand{end}\isamarkupfalse%
   395 %
   396 \endisatagtheory
   397 {\isafoldtheory}%
   398 %
   399 \isadelimtheory
   400 %
   401 \endisadelimtheory
   402 \isanewline
   403 \end{isabellebody}%
   404 %%% Local Variables:
   405 %%% mode: latex
   406 %%% TeX-master: "root"
   407 %%% End: