doc-src/IsarAdvanced/Codegen/Thy/document/Codegen.tex
author haftmann
Tue Jul 15 16:02:07 2008 +0200 (2008-07-15)
changeset 27609 b23c9ad0fe7d
parent 27557 151731493264
child 28143 e5c6c4aac52c
permissions -rw-r--r--
tuned code theorem bookkeeping
haftmann@20967
     1
%
haftmann@20967
     2
\begin{isabellebody}%
haftmann@20967
     3
\def\isabellecontext{Codegen}%
haftmann@20967
     4
%
haftmann@20967
     5
\isadelimtheory
haftmann@20967
     6
\isanewline
haftmann@26513
     7
\isanewline
haftmann@20967
     8
%
haftmann@20967
     9
\endisadelimtheory
haftmann@20967
    10
%
haftmann@20967
    11
\isatagtheory
wenzelm@21172
    12
%
haftmann@20967
    13
\endisatagtheory
haftmann@20967
    14
{\isafoldtheory}%
haftmann@20967
    15
%
haftmann@20967
    16
\isadelimtheory
haftmann@20967
    17
%
haftmann@20967
    18
\endisadelimtheory
haftmann@20967
    19
%
wenzelm@21172
    20
\isadelimML
wenzelm@21172
    21
%
wenzelm@21172
    22
\endisadelimML
wenzelm@21172
    23
%
wenzelm@21172
    24
\isatagML
wenzelm@21172
    25
%
wenzelm@21172
    26
\endisatagML
wenzelm@21172
    27
{\isafoldML}%
wenzelm@21172
    28
%
wenzelm@21172
    29
\isadelimML
wenzelm@21172
    30
%
wenzelm@21172
    31
\endisadelimML
wenzelm@21172
    32
%
haftmann@20967
    33
\isamarkupchapter{Code generation from Isabelle theories%
haftmann@20967
    34
}
haftmann@20967
    35
\isamarkuptrue%
haftmann@20967
    36
%
haftmann@20967
    37
\isamarkupsection{Introduction%
haftmann@20967
    38
}
haftmann@20967
    39
\isamarkuptrue%
haftmann@20967
    40
%
wenzelm@21172
    41
\isamarkupsubsection{Motivation%
wenzelm@21172
    42
}
wenzelm@21172
    43
\isamarkuptrue%
wenzelm@21172
    44
%
haftmann@20967
    45
\begin{isamarkuptext}%
wenzelm@21172
    46
Executing formal specifications as programs is a well-established
wenzelm@21172
    47
  topic in the theorem proving community.  With increasing
wenzelm@21172
    48
  application of theorem proving systems in the area of
wenzelm@21172
    49
  software development and verification, its relevance manifests
wenzelm@21172
    50
  for running test cases and rapid prototyping.  In logical
wenzelm@21172
    51
  calculi like constructive type theory,
wenzelm@21172
    52
  a notion of executability is implicit due to the nature
haftmann@22550
    53
  of the calculus.  In contrast, specifications in Isabelle
wenzelm@21172
    54
  can be highly non-executable.  In order to bridge
wenzelm@21172
    55
  the gap between logic and executable specifications,
wenzelm@21172
    56
  an explicit non-trivial transformation has to be applied:
wenzelm@21172
    57
  code generation.
wenzelm@21172
    58
wenzelm@21172
    59
  This tutorial introduces a generic code generator for the
wenzelm@21172
    60
  Isabelle system \cite{isa-tutorial}.
wenzelm@21172
    61
  Generic in the sense that the
wenzelm@21172
    62
  \qn{target language} for which code shall ultimately be
wenzelm@21172
    63
  generated is not fixed but may be an arbitrary state-of-the-art
wenzelm@21172
    64
  functional programming language (currently, the implementation
haftmann@22550
    65
  supports SML \cite{SML}, OCaml \cite{OCaml} and Haskell
haftmann@22550
    66
  \cite{haskell-revised-report}).
wenzelm@21172
    67
  We aim to provide a
wenzelm@21172
    68
  versatile environment
wenzelm@21172
    69
  suitable for software development and verification,
wenzelm@21172
    70
  structuring the process
wenzelm@21172
    71
  of code generation into a small set of orthogonal principles
wenzelm@21172
    72
  while achieving a big coverage of application areas
haftmann@21348
    73
  with maximum flexibility.
haftmann@21348
    74
haftmann@22550
    75
  Conceptually the code generator framework is part
haftmann@22550
    76
  of Isabelle's \isa{Pure} meta logic; the object logic
haftmann@22550
    77
  \isa{HOL} which is an extension of \isa{Pure}
haftmann@22550
    78
  already comes with a reasonable framework setup and thus provides
haftmann@22550
    79
  a good working horse for raising code-generation-driven
haftmann@22550
    80
  applications.  So, we assume some familiarity and experience
haftmann@22550
    81
  with the ingredients of the \isa{HOL} \emph{Main} theory
haftmann@22550
    82
  (see also \cite{isa-tutorial}).%
haftmann@20967
    83
\end{isamarkuptext}%
haftmann@20967
    84
\isamarkuptrue%
haftmann@20967
    85
%
wenzelm@21172
    86
\isamarkupsubsection{Overview%
wenzelm@21172
    87
}
wenzelm@21172
    88
\isamarkuptrue%
wenzelm@21172
    89
%
wenzelm@21172
    90
\begin{isamarkuptext}%
wenzelm@21172
    91
The code generator aims to be usable with no further ado
wenzelm@21172
    92
  in most cases while allowing for detailed customization.
haftmann@22550
    93
  This manifests in the structure of this tutorial:
haftmann@22550
    94
  we start with a generic example \secref{sec:example}
haftmann@22550
    95
  and introduce code generation concepts \secref{sec:concept}.
haftmann@22550
    96
  Section
wenzelm@21186
    97
  \secref{sec:basics} explains how to use the framework naively,
wenzelm@21172
    98
  presuming a reasonable default setup.  Then, section
wenzelm@21172
    99
  \secref{sec:advanced} deals with advanced topics,
wenzelm@21172
   100
  introducing further aspects of the code generator framework
wenzelm@21172
   101
  in a motivation-driven manner.  Last, section \secref{sec:ml}
wenzelm@21172
   102
  introduces the framework's internal programming interfaces.
wenzelm@21172
   103
wenzelm@21172
   104
  \begin{warn}
wenzelm@21172
   105
    Ultimately, the code generator which this tutorial deals with
wenzelm@21172
   106
    is supposed to replace the already established code generator
wenzelm@21172
   107
    by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
wenzelm@21172
   108
    So, for the moment, there are two distinct code generators
wenzelm@21172
   109
    in Isabelle.
haftmann@22916
   110
    Also note that while the framework itself is
haftmann@22550
   111
    object-logic independent, only \isa{HOL} provides a reasonable
wenzelm@21172
   112
    framework setup.    
wenzelm@21172
   113
  \end{warn}%
wenzelm@21172
   114
\end{isamarkuptext}%
wenzelm@21172
   115
\isamarkuptrue%
wenzelm@21172
   116
%
haftmann@22550
   117
\isamarkupsection{An example: a simple theory of search trees \label{sec:example}%
haftmann@22479
   118
}
haftmann@22479
   119
\isamarkuptrue%
haftmann@22550
   120
%
haftmann@22550
   121
\begin{isamarkuptext}%
haftmann@22916
   122
When writing executable specifications using \isa{HOL},
haftmann@22916
   123
  it is convenient to use
haftmann@22550
   124
  three existing packages: the datatype package for defining
haftmann@22550
   125
  datatypes, the function package for (recursive) functions,
haftmann@22550
   126
  and the class package for overloaded definitions.
haftmann@22550
   127
haftmann@22550
   128
  We develope a small theory of search trees; trees are represented
haftmann@22550
   129
  as a datatype with key type \isa{{\isacharprime}a} and value type \isa{{\isacharprime}b}:%
haftmann@22550
   130
\end{isamarkuptext}%
haftmann@22550
   131
\isamarkuptrue%
haftmann@22479
   132
\isacommand{datatype}\isamarkupfalse%
haftmann@22479
   133
\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree\ {\isacharequal}\ Leaf\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}linorder{\isachardoublequoteclose}\ {\isacharprime}b\isanewline
haftmann@22550
   134
\ \ {\isacharbar}\ Branch\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharprime}a{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree{\isachardoublequoteclose}%
haftmann@22550
   135
\begin{isamarkuptext}%
haftmann@22550
   136
\noindent Note that we have constrained the type of keys
haftmann@22550
   137
  to the class of total orders, \isa{linorder}.
haftmann@22550
   138
haftmann@22550
   139
  We define \isa{find} and \isa{update} functions:%
haftmann@22550
   140
\end{isamarkuptext}%
haftmann@22550
   141
\isamarkuptrue%
haftmann@25870
   142
\isacommand{primrec}\isamarkupfalse%
haftmann@22479
   143
\isanewline
haftmann@22479
   144
\ \ find\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharprime}a{\isasymColon}linorder{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}b\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22479
   145
\ \ {\isachardoublequoteopen}find\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharequal}\ {\isacharparenleft}if\ it\ {\isacharequal}\ key\ then\ Some\ val\ else\ None{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22479
   146
\ \ {\isacharbar}\ {\isachardoublequoteopen}find\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ t{\isadigit{2}}{\isacharparenright}\ it\ {\isacharequal}\ {\isacharparenleft}if\ it\ {\isasymle}\ key\ then\ find\ t{\isadigit{1}}\ it\ else\ find\ t{\isadigit{2}}\ it{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22479
   147
\isanewline
haftmann@22479
   148
\isacommand{fun}\isamarkupfalse%
haftmann@22479
   149
\isanewline
haftmann@22479
   150
\ \ update\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}linorder\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22479
   151
\ \ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline
haftmann@22479
   152
\ \ \ \ if\ it\ {\isacharequal}\ key\ then\ Leaf\ key\ entry\isanewline
haftmann@22479
   153
\ \ \ \ \ \ else\ if\ it\ {\isasymle}\ key\isanewline
haftmann@22479
   154
\ \ \ \ \ \ then\ Branch\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\isanewline
haftmann@22479
   155
\ \ \ \ \ \ else\ Branch\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\isanewline
haftmann@22479
   156
\ \ \ {\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22479
   157
\ \ {\isacharbar}\ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ t{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline
haftmann@22479
   158
\ \ \ \ if\ it\ {\isasymle}\ key\isanewline
haftmann@22479
   159
\ \ \ \ \ \ then\ {\isacharparenleft}Branch\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{1}}{\isacharparenright}\ key\ t{\isadigit{2}}{\isacharparenright}\isanewline
haftmann@22479
   160
\ \ \ \ \ \ else\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{2}}{\isacharparenright}{\isacharparenright}\isanewline
haftmann@22550
   161
\ \ \ {\isacharparenright}{\isachardoublequoteclose}%
haftmann@22550
   162
\begin{isamarkuptext}%
haftmann@22550
   163
\noindent For testing purpose, we define a small example
haftmann@22550
   164
  using natural numbers \isa{nat} (which are a \isa{linorder})
haftmann@23132
   165
  as keys and list of nats as values:%
haftmann@22550
   166
\end{isamarkuptext}%
haftmann@22550
   167
\isamarkuptrue%
haftmann@23132
   168
\isacommand{definition}\isamarkupfalse%
haftmann@22479
   169
\isanewline
haftmann@23132
   170
\ \ example\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat{\isacharcomma}\ nat\ list{\isacharparenright}\ searchtree{\isachardoublequoteclose}\isanewline
haftmann@23132
   171
\isakeyword{where}\isanewline
haftmann@23132
   172
\ \ {\isachardoublequoteopen}example\ {\isacharequal}\ update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\ {\isacharparenleft}update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\isanewline
haftmann@23132
   173
\ \ \ \ {\isacharparenleft}update\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ {\isacharbrackleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharbrackright}{\isacharparenright}\ {\isacharparenleft}Leaf\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22550
   174
\begin{isamarkuptext}%
haftmann@22550
   175
\noindent Then we generate code%
haftmann@22550
   176
\end{isamarkuptext}%
haftmann@22550
   177
\isamarkuptrue%
haftmann@24379
   178
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   179
\ example\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}tree{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22479
   180
\begin{isamarkuptext}%
haftmann@22550
   181
\noindent which looks like:
haftmann@22550
   182
  \lstsml{Thy/examples/tree.ML}%
haftmann@22479
   183
\end{isamarkuptext}%
haftmann@22479
   184
\isamarkuptrue%
haftmann@22479
   185
%
haftmann@22550
   186
\isamarkupsection{Code generation concepts and process \label{sec:concept}%
wenzelm@21172
   187
}
wenzelm@21172
   188
\isamarkuptrue%
wenzelm@21172
   189
%
wenzelm@21172
   190
\begin{isamarkuptext}%
haftmann@21348
   191
\begin{figure}[h]
haftmann@21348
   192
  \centering
haftmann@21348
   193
  \includegraphics[width=0.7\textwidth]{codegen_process}
haftmann@21348
   194
  \caption{code generator -- processing overview}
haftmann@21348
   195
  \label{fig:process}
haftmann@21348
   196
  \end{figure}
haftmann@21348
   197
haftmann@21348
   198
  The code generator employs a notion of executability
wenzelm@21172
   199
  for three foundational executable ingredients known
wenzelm@21172
   200
  from functional programming:
haftmann@22060
   201
  \emph{defining equations}, \emph{datatypes}, and
haftmann@22060
   202
  \emph{type classes}. A defining equation as a first approximation
wenzelm@21172
   203
  is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t}
wenzelm@21172
   204
  (an equation headed by a constant \isa{f} with arguments
haftmann@22798
   205
  \isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}).
haftmann@22060
   206
  Code generation aims to turn defining equations
wenzelm@21172
   207
  into a functional program by running through
wenzelm@21172
   208
  a process (see figure \ref{fig:process}):
wenzelm@21172
   209
wenzelm@21172
   210
  \begin{itemize}
wenzelm@21172
   211
wenzelm@21172
   212
    \item Out of the vast collection of theorems proven in a
wenzelm@21172
   213
      \qn{theory}, a reasonable subset modeling
haftmann@22060
   214
      defining equations is \qn{selected}.
wenzelm@21172
   215
wenzelm@21172
   216
    \item On those selected theorems, certain
wenzelm@21172
   217
      transformations are carried out
wenzelm@21172
   218
      (\qn{preprocessing}).  Their purpose is to turn theorems
wenzelm@21172
   219
      representing non- or badly executable
wenzelm@21172
   220
      specifications into equivalent but executable counterparts.
wenzelm@21172
   221
      The result is a structured collection of \qn{code theorems}.
wenzelm@21172
   222
haftmann@22479
   223
    \item These \qn{code theorems} then are \qn{translated}
wenzelm@21172
   224
      into an Haskell-like intermediate
wenzelm@21172
   225
      language.
wenzelm@21172
   226
wenzelm@21172
   227
    \item Finally, out of the intermediate language the final
wenzelm@21172
   228
      code in the desired \qn{target language} is \qn{serialized}.
wenzelm@21172
   229
wenzelm@21172
   230
  \end{itemize}
wenzelm@21172
   231
wenzelm@21172
   232
  From these steps, only the two last are carried out
wenzelm@21172
   233
  outside the logic; by keeping this layer as
wenzelm@21172
   234
  thin as possible, the amount of code to trust is
wenzelm@21172
   235
  kept to a minimum.%
wenzelm@21172
   236
\end{isamarkuptext}%
wenzelm@21172
   237
\isamarkuptrue%
wenzelm@21172
   238
%
wenzelm@21172
   239
\isamarkupsection{Basics \label{sec:basics}%
haftmann@20967
   240
}
haftmann@20967
   241
\isamarkuptrue%
haftmann@20967
   242
%
haftmann@20967
   243
\isamarkupsubsection{Invoking the code generator%
haftmann@20967
   244
}
haftmann@20967
   245
\isamarkuptrue%
haftmann@20967
   246
%
wenzelm@21172
   247
\begin{isamarkuptext}%
haftmann@22916
   248
Thanks to a reasonable setup of the \isa{HOL} theories, in
wenzelm@21172
   249
  most cases code generation proceeds without further ado:%
wenzelm@21172
   250
\end{isamarkuptext}%
wenzelm@21172
   251
\isamarkuptrue%
haftmann@25870
   252
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
   253
\isanewline
haftmann@22479
   254
\ \ fac\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22479
   255
\ \ \ \ {\isachardoublequoteopen}fac\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
haftmann@22479
   256
\ \ {\isacharbar}\ {\isachardoublequoteopen}fac\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ Suc\ n\ {\isacharasterisk}\ fac\ n{\isachardoublequoteclose}%
wenzelm@21172
   257
\begin{isamarkuptext}%
haftmann@22550
   258
\noindent This executable specification is now turned to SML code:%
wenzelm@21172
   259
\end{isamarkuptext}%
wenzelm@21172
   260
\isamarkuptrue%
haftmann@24379
   261
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   262
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   263
\begin{isamarkuptext}%
haftmann@24379
   264
\noindent  The \isa{{\isasymEXPORTCODE}} command takes a space-separated list of
wenzelm@21172
   265
  constants together with \qn{serialization directives}
haftmann@22845
   266
  These start with a \qn{target language}
haftmann@22845
   267
  identifier, followed by a file specification
haftmann@22845
   268
  where to write the generated code to.
wenzelm@21172
   269
haftmann@22060
   270
  Internally, the defining equations for all selected
wenzelm@21186
   271
  constants are taken, including any transitively required
wenzelm@21172
   272
  constants, datatypes and classes, resulting in the following
wenzelm@21172
   273
  code:
wenzelm@21172
   274
wenzelm@21172
   275
  \lstsml{Thy/examples/fac.ML}
wenzelm@21172
   276
wenzelm@21172
   277
  The code generator will complain when a required
haftmann@22550
   278
  ingredient does not provide a executable counterpart,
haftmann@22550
   279
  e.g.~generating code
wenzelm@21172
   280
  for constants not yielding
haftmann@22550
   281
  a defining equation (e.g.~the Hilbert choice
haftmann@22550
   282
  operation \isa{SOME}):%
wenzelm@21172
   283
\end{isamarkuptext}%
wenzelm@21172
   284
\isamarkuptrue%
wenzelm@21172
   285
%
wenzelm@21172
   286
\isadelimML
wenzelm@21172
   287
%
wenzelm@21172
   288
\endisadelimML
wenzelm@21172
   289
%
wenzelm@21172
   290
\isatagML
wenzelm@21172
   291
%
wenzelm@21172
   292
\endisatagML
wenzelm@21172
   293
{\isafoldML}%
wenzelm@21172
   294
%
wenzelm@21172
   295
\isadelimML
wenzelm@21172
   296
%
wenzelm@21172
   297
\endisadelimML
wenzelm@21172
   298
\isacommand{definition}\isamarkupfalse%
wenzelm@21172
   299
\isanewline
haftmann@21993
   300
\ \ pick{\isacharunderscore}some\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
   301
\ \ {\isachardoublequoteopen}pick{\isacharunderscore}some\ xs\ {\isacharequal}\ {\isacharparenleft}SOME\ x{\isachardot}\ x\ {\isasymin}\ set\ xs{\isacharparenright}{\isachardoublequoteclose}%
wenzelm@21172
   302
\isadelimML
wenzelm@21172
   303
%
wenzelm@21172
   304
\endisadelimML
wenzelm@21172
   305
%
wenzelm@21172
   306
\isatagML
wenzelm@21172
   307
%
wenzelm@21172
   308
\endisatagML
wenzelm@21172
   309
{\isafoldML}%
wenzelm@21172
   310
%
wenzelm@21172
   311
\isadelimML
wenzelm@21172
   312
%
wenzelm@21172
   313
\endisadelimML
haftmann@25870
   314
\isanewline
haftmann@24379
   315
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   316
\ pick{\isacharunderscore}some\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}const{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22550
   317
\begin{isamarkuptext}%
haftmann@22550
   318
\noindent will fail.%
haftmann@22550
   319
\end{isamarkuptext}%
haftmann@22550
   320
\isamarkuptrue%
haftmann@22550
   321
%
haftmann@20967
   322
\isamarkupsubsection{Theorem selection%
haftmann@20967
   323
}
haftmann@20967
   324
\isamarkuptrue%
haftmann@20967
   325
%
wenzelm@21172
   326
\begin{isamarkuptext}%
haftmann@22060
   327
The list of all defining equations in a theory may be inspected
haftmann@22292
   328
  using the \isa{{\isasymPRINTCODESETUP}} command:%
wenzelm@21172
   329
\end{isamarkuptext}%
wenzelm@21172
   330
\isamarkuptrue%
haftmann@22292
   331
\isacommand{print{\isacharunderscore}codesetup}\isamarkupfalse%
wenzelm@21172
   332
%
wenzelm@21172
   333
\begin{isamarkuptext}%
wenzelm@21172
   334
\noindent which displays a table of constant with corresponding
haftmann@22060
   335
  defining equations (the additional stuff displayed
haftmann@22751
   336
  shall not bother us for the moment).
wenzelm@21172
   337
haftmann@22916
   338
  The typical \isa{HOL} tools are already set up in a way that
haftmann@22751
   339
  function definitions introduced by \isa{{\isasymDEFINITION}},
haftmann@25870
   340
  \isa{{\isasymPRIMREC}}, \isa{{\isasymFUN}},
haftmann@25870
   341
  \isa{{\isasymFUNCTION}}, \isa{{\isasymCONSTDEFS}},
haftmann@21348
   342
  \isa{{\isasymRECDEF}} are implicitly propagated
haftmann@22060
   343
  to this defining equation table. Specific theorems may be
wenzelm@21172
   344
  selected using an attribute: \emph{code func}. As example,
wenzelm@21172
   345
  a weight selector function:%
wenzelm@21172
   346
\end{isamarkuptext}%
wenzelm@21172
   347
\isamarkuptrue%
wenzelm@21172
   348
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
   349
\isanewline
haftmann@25870
   350
\ \ pick\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
wenzelm@21172
   351
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharequal}\ x\ in\isanewline
wenzelm@21172
   352
\ \ \ \ if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
wenzelm@21172
   353
\begin{isamarkuptext}%
haftmann@22798
   354
\noindent We want to eliminate the explicit destruction
wenzelm@21172
   355
  of \isa{x} to \isa{{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}}:%
wenzelm@21172
   356
\end{isamarkuptext}%
wenzelm@21172
   357
\isamarkuptrue%
wenzelm@21172
   358
\isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   359
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@21172
   360
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
wenzelm@21172
   361
%
wenzelm@21172
   362
\isadelimproof
wenzelm@21172
   363
\ \ %
wenzelm@21172
   364
\endisadelimproof
wenzelm@21172
   365
%
wenzelm@21172
   366
\isatagproof
wenzelm@21172
   367
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   368
\ simp%
wenzelm@21172
   369
\endisatagproof
wenzelm@21172
   370
{\isafoldproof}%
wenzelm@21172
   371
%
wenzelm@21172
   372
\isadelimproof
wenzelm@21172
   373
\isanewline
wenzelm@21172
   374
%
wenzelm@21172
   375
\endisadelimproof
wenzelm@21172
   376
\isanewline
haftmann@24379
   377
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   378
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   379
\begin{isamarkuptext}%
haftmann@22798
   380
\noindent This theorem now is used for generating code:
wenzelm@21172
   381
wenzelm@21172
   382
  \lstsml{Thy/examples/pick1.ML}
wenzelm@21172
   383
haftmann@22798
   384
  \noindent It might be convenient to remove the pointless original
haftmann@22845
   385
  equation, using the \emph{func del} attribute:%
wenzelm@21172
   386
\end{isamarkuptext}%
wenzelm@21172
   387
\isamarkuptrue%
wenzelm@21172
   388
\isacommand{lemmas}\isamarkupfalse%
haftmann@22845
   389
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline
wenzelm@21172
   390
\isanewline
haftmann@24379
   391
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   392
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   393
\begin{isamarkuptext}%
wenzelm@21172
   394
\lstsml{Thy/examples/pick2.ML}
wenzelm@21172
   395
haftmann@22798
   396
  \noindent Syntactic redundancies are implicitly dropped. For example,
wenzelm@21172
   397
  using a modified version of the \isa{fac} function
haftmann@22060
   398
  as defining equation, the then redundant (since
haftmann@22060
   399
  syntactically subsumed) original defining equations
wenzelm@21172
   400
  are dropped, resulting in a warning:%
wenzelm@21172
   401
\end{isamarkuptext}%
wenzelm@21172
   402
\isamarkuptrue%
wenzelm@21172
   403
\isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   404
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@21172
   405
\ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline
wenzelm@21172
   406
%
wenzelm@21172
   407
\isadelimproof
wenzelm@21172
   408
\ \ %
wenzelm@21172
   409
\endisadelimproof
wenzelm@21172
   410
%
wenzelm@21172
   411
\isatagproof
wenzelm@21172
   412
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   413
\ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   414
\endisatagproof
wenzelm@21172
   415
{\isafoldproof}%
wenzelm@21172
   416
%
wenzelm@21172
   417
\isadelimproof
wenzelm@21172
   418
\isanewline
wenzelm@21172
   419
%
wenzelm@21172
   420
\endisadelimproof
wenzelm@21172
   421
\isanewline
haftmann@24379
   422
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   423
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   424
\begin{isamarkuptext}%
wenzelm@21172
   425
\lstsml{Thy/examples/fac_case.ML}
wenzelm@21172
   426
wenzelm@21172
   427
  \begin{warn}
haftmann@22292
   428
    The attributes \emph{code} and \emph{code del}
wenzelm@21172
   429
    associated with the existing code generator also apply to
wenzelm@21172
   430
    the new one: \emph{code} implies \emph{code func},
haftmann@22845
   431
    and \emph{code del} implies \emph{code func del}.
wenzelm@21172
   432
  \end{warn}%
wenzelm@21172
   433
\end{isamarkuptext}%
wenzelm@21172
   434
\isamarkuptrue%
wenzelm@21172
   435
%
haftmann@20967
   436
\isamarkupsubsection{Type classes%
haftmann@20967
   437
}
haftmann@20967
   438
\isamarkuptrue%
haftmann@20967
   439
%
wenzelm@21172
   440
\begin{isamarkuptext}%
wenzelm@21172
   441
Type classes enter the game via the Isar class package.
wenzelm@21172
   442
  For a short introduction how to use it, see \cite{isabelle-classes};
wenzelm@21172
   443
  here we just illustrate its impact on code generation.
wenzelm@21172
   444
wenzelm@21172
   445
  In a target language, type classes may be represented
haftmann@22798
   446
  natively (as in the case of Haskell).  For languages
wenzelm@21172
   447
  like SML, they are implemented using \emph{dictionaries}.
wenzelm@21186
   448
  Our following example specifies a class \qt{null},
wenzelm@21172
   449
  assigning to each of its inhabitants a \qt{null} value:%
wenzelm@21172
   450
\end{isamarkuptext}%
wenzelm@21172
   451
\isamarkuptrue%
wenzelm@21172
   452
\isacommand{class}\isamarkupfalse%
haftmann@22479
   453
\ null\ {\isacharequal}\ type\ {\isacharplus}\isanewline
wenzelm@21172
   454
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
wenzelm@21172
   455
\isanewline
haftmann@25870
   456
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
   457
\isanewline
haftmann@25870
   458
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
wenzelm@21172
   459
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline
haftmann@22798
   460
\ \ {\isacharbar}\ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}%
wenzelm@21172
   461
\begin{isamarkuptext}%
wenzelm@25731
   462
\noindent  We provide some instances for our \isa{null}:%
wenzelm@21172
   463
\end{isamarkuptext}%
wenzelm@21172
   464
\isamarkuptrue%
haftmann@25533
   465
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   466
\ option\ \isakeyword{and}\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
haftmann@25533
   467
\isakeyword{begin}\isanewline
haftmann@25533
   468
\isanewline
haftmann@25533
   469
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   470
\isanewline
haftmann@25533
   471
\ \ {\isachardoublequoteopen}null\ {\isacharequal}\ None{\isachardoublequoteclose}\isanewline
haftmann@25533
   472
\isanewline
haftmann@25533
   473
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   474
\isanewline
wenzelm@25731
   475
\ \ {\isachardoublequoteopen}null\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   476
\isanewline
wenzelm@21172
   477
\isacommand{instance}\isamarkupfalse%
haftmann@25533
   478
%
wenzelm@21172
   479
\isadelimproof
wenzelm@21172
   480
\ %
wenzelm@21172
   481
\endisadelimproof
wenzelm@21172
   482
%
wenzelm@21172
   483
\isatagproof
wenzelm@21172
   484
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
wenzelm@21172
   485
%
wenzelm@21172
   486
\endisatagproof
wenzelm@21172
   487
{\isafoldproof}%
wenzelm@21172
   488
%
wenzelm@21172
   489
\isadelimproof
wenzelm@21172
   490
%
wenzelm@21172
   491
\endisadelimproof
wenzelm@21172
   492
\isanewline
wenzelm@21172
   493
\isanewline
haftmann@25533
   494
\isacommand{end}\isamarkupfalse%
wenzelm@21172
   495
%
wenzelm@21172
   496
\begin{isamarkuptext}%
wenzelm@25731
   497
\noindent Constructing a dummy example:%
wenzelm@21172
   498
\end{isamarkuptext}%
wenzelm@21172
   499
\isamarkuptrue%
wenzelm@21172
   500
\isacommand{definition}\isamarkupfalse%
wenzelm@21172
   501
\isanewline
wenzelm@21172
   502
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}%
wenzelm@21172
   503
\begin{isamarkuptext}%
wenzelm@21186
   504
Type classes offer a suitable occasion to introduce
wenzelm@21172
   505
  the Haskell serializer.  Its usage is almost the same
wenzelm@21172
   506
  as SML, but, in accordance with conventions
wenzelm@21172
   507
  some Haskell systems enforce, each module ends
wenzelm@21172
   508
  up in a single file. The module hierarchy is reflected in
haftmann@22845
   509
  the file system, with root directory given as file specification.%
wenzelm@21172
   510
\end{isamarkuptext}%
wenzelm@21172
   511
\isamarkuptrue%
haftmann@24379
   512
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   513
\ dummy\ \isakeyword{in}\ Haskell\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
wenzelm@21172
   514
\begin{isamarkuptext}%
wenzelm@21172
   515
\lsthaskell{Thy/examples/Codegen.hs}
haftmann@22798
   516
  \noindent (we have left out all other modules).
wenzelm@21172
   517
haftmann@22798
   518
  \medskip
wenzelm@21172
   519
wenzelm@21172
   520
  The whole code in SML with explicit dictionary passing:%
wenzelm@21172
   521
\end{isamarkuptext}%
wenzelm@21172
   522
\isamarkuptrue%
haftmann@24379
   523
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   524
\ dummy\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   525
\begin{isamarkuptext}%
haftmann@22798
   526
\lstsml{Thy/examples/class.ML}
haftmann@22798
   527
haftmann@22798
   528
  \medskip
haftmann@22798
   529
haftmann@22798
   530
  \noindent or in OCaml:%
haftmann@22292
   531
\end{isamarkuptext}%
haftmann@22292
   532
\isamarkuptrue%
haftmann@24379
   533
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   534
\ dummy\ \isakeyword{in}\ OCaml\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ocaml{\isachardoublequoteclose}%
haftmann@22292
   535
\begin{isamarkuptext}%
haftmann@22798
   536
\lstsml{Thy/examples/class.ocaml}
wenzelm@21172
   537
haftmann@22798
   538
  \medskip The explicit association of constants
haftmann@22845
   539
  to classes can be inspected using the \isa{{\isasymPRINTCLASSES}}
haftmann@22845
   540
  command.%
wenzelm@21172
   541
\end{isamarkuptext}%
wenzelm@21172
   542
\isamarkuptrue%
wenzelm@21172
   543
%
wenzelm@21172
   544
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}%
wenzelm@21172
   545
}
wenzelm@21172
   546
\isamarkuptrue%
wenzelm@21172
   547
%
wenzelm@21172
   548
\begin{isamarkuptext}%
wenzelm@21172
   549
In this tutorial, we do not attempt to give an exhaustive
wenzelm@21172
   550
  description of the code generator framework; instead,
wenzelm@21172
   551
  we cast a light on advanced topics by introducing
wenzelm@21172
   552
  them together with practically motivated examples.  Concerning
wenzelm@21172
   553
  further reading, see
wenzelm@21172
   554
wenzelm@21172
   555
  \begin{itemize}
wenzelm@21172
   556
wenzelm@21172
   557
  \item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
wenzelm@21172
   558
    for exhaustive syntax diagrams.
haftmann@24193
   559
  \item or \cite{Haftmann-Nipkow:2007:codegen} which deals with foundational issues
wenzelm@21172
   560
    of the code generator framework.
wenzelm@21172
   561
wenzelm@21172
   562
  \end{itemize}%
wenzelm@21172
   563
\end{isamarkuptext}%
wenzelm@21172
   564
\isamarkuptrue%
wenzelm@21172
   565
%
haftmann@22798
   566
\isamarkupsubsection{Library theories \label{sec:library}%
wenzelm@21172
   567
}
wenzelm@21172
   568
\isamarkuptrue%
wenzelm@21172
   569
%
wenzelm@21172
   570
\begin{isamarkuptext}%
haftmann@22916
   571
The \isa{HOL} \isa{Main} theory already provides a code
haftmann@22916
   572
  generator setup
wenzelm@21172
   573
  which should be suitable for most applications. Common extensions
haftmann@22916
   574
  and modifications are available by certain theories of the \isa{HOL}
wenzelm@21172
   575
  library; beside being useful in applications, they may serve
wenzelm@21186
   576
  as a tutorial for customizing the code generator setup.
wenzelm@21172
   577
wenzelm@21172
   578
  \begin{description}
wenzelm@21172
   579
wenzelm@25370
   580
    \item[\isa{Code{\isacharunderscore}Integer}] represents \isa{HOL} integers by big
haftmann@22798
   581
       integer literals in target languages.
wenzelm@25370
   582
    \item[\isa{Code{\isacharunderscore}Char}] represents \isa{HOL} characters by 
haftmann@22798
   583
       character literals in target languages.
wenzelm@25370
   584
    \item[\isa{Code{\isacharunderscore}Char{\isacharunderscore}chr}] like \isa{Code{\isacharunderscore}Char},
haftmann@22798
   585
       but also offers treatment of character codes; includes
wenzelm@25370
   586
       \isa{Code{\isacharunderscore}Integer}.
haftmann@23850
   587
    \item[\isa{Efficient{\isacharunderscore}Nat}] \label{eff_nat} implements natural numbers by integers,
wenzelm@21186
   588
       which in general will result in higher efficency; pattern
wenzelm@21172
   589
       matching with \isa{{\isadigit{0}}} / \isa{Suc}
wenzelm@25370
   590
       is eliminated;  includes \isa{Code{\isacharunderscore}Integer}.
wenzelm@25370
   591
    \item[\isa{Code{\isacharunderscore}Index}] provides an additional datatype
wenzelm@25370
   592
       \isa{index} which is mapped to target-language built-in integers.
wenzelm@25370
   593
       Useful for code setups which involve e.g. indexing of
wenzelm@25370
   594
       target-language arrays.
wenzelm@25370
   595
    \item[\isa{Code{\isacharunderscore}Message}] provides an additional datatype
wenzelm@25370
   596
       \isa{message{\isacharunderscore}string} which is isomorphic to strings;
wenzelm@25370
   597
       \isa{message{\isacharunderscore}string}s are mapped to target-language strings.
wenzelm@25370
   598
       Useful for code setups which involve e.g. printing (error) messages.
wenzelm@21172
   599
haftmann@22916
   600
  \end{description}
haftmann@22916
   601
haftmann@22916
   602
  \begin{warn}
haftmann@22916
   603
    When importing any of these theories, they should form the last
haftmann@22916
   604
    items in an import list.  Since these theories adapt the
haftmann@22916
   605
    code generator setup in a non-conservative fashion,
haftmann@22916
   606
    strange effects may occur otherwise.
haftmann@22916
   607
  \end{warn}%
wenzelm@21172
   608
\end{isamarkuptext}%
wenzelm@21172
   609
\isamarkuptrue%
wenzelm@21172
   610
%
wenzelm@21172
   611
\isamarkupsubsection{Preprocessing%
haftmann@20967
   612
}
haftmann@20967
   613
\isamarkuptrue%
haftmann@20967
   614
%
wenzelm@21172
   615
\begin{isamarkuptext}%
wenzelm@21172
   616
Before selected function theorems are turned into abstract
wenzelm@21172
   617
  code, a chain of definitional transformation steps is carried
haftmann@27609
   618
  out: \emph{preprocessing}.  In essence, the preprocessor
haftmann@27609
   619
  consists of two components: a \emph{simpset} and \emph{function transformers}.
wenzelm@21172
   620
haftmann@27557
   621
  The \emph{simpset} allows to employ the full generality of the Isabelle
haftmann@27557
   622
  simplifier.  Due to the interpretation of theorems
haftmann@22060
   623
  of defining equations, rewrites are applied to the right
wenzelm@21172
   624
  hand side and the arguments of the left hand side of an
wenzelm@21172
   625
  equation, but never to the constant heading the left hand side.
haftmann@27557
   626
  An important special case are \emph{inline theorems} which may be
haftmann@27557
   627
  declared an undeclared using the
haftmann@22845
   628
  \emph{code inline} or \emph{code inline del} attribute respectively.
wenzelm@21172
   629
  Some common applications:%
wenzelm@21172
   630
\end{isamarkuptext}%
wenzelm@21172
   631
\isamarkuptrue%
wenzelm@21172
   632
%
wenzelm@21172
   633
\begin{itemize}
haftmann@22845
   634
%
haftmann@22845
   635
\begin{isamarkuptext}%
haftmann@22845
   636
\item replacing non-executable constructs by executable ones:%
haftmann@22845
   637
\end{isamarkuptext}%
haftmann@22845
   638
\isamarkuptrue%
haftmann@22845
   639
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   640
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   641
\ \ \ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}%
wenzelm@21172
   642
\isadelimproof
wenzelm@21172
   643
\ %
wenzelm@21172
   644
\endisadelimproof
wenzelm@21172
   645
%
wenzelm@21172
   646
\isatagproof
wenzelm@21172
   647
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   648
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   649
\endisatagproof
wenzelm@21172
   650
{\isafoldproof}%
wenzelm@21172
   651
%
wenzelm@21172
   652
\isadelimproof
wenzelm@21172
   653
%
wenzelm@21172
   654
\endisadelimproof
wenzelm@21172
   655
%
haftmann@22845
   656
\begin{isamarkuptext}%
haftmann@22845
   657
\item eliminating superfluous constants:%
haftmann@22845
   658
\end{isamarkuptext}%
haftmann@22845
   659
\isamarkuptrue%
haftmann@22845
   660
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   661
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   662
\ \ \ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}%
wenzelm@21172
   663
\isadelimproof
wenzelm@21172
   664
\ %
wenzelm@21172
   665
\endisadelimproof
wenzelm@21172
   666
%
wenzelm@21172
   667
\isatagproof
wenzelm@21172
   668
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   669
\ simp%
wenzelm@21172
   670
\endisatagproof
wenzelm@21172
   671
{\isafoldproof}%
wenzelm@21172
   672
%
wenzelm@21172
   673
\isadelimproof
wenzelm@21172
   674
%
wenzelm@21172
   675
\endisadelimproof
wenzelm@21172
   676
%
haftmann@22845
   677
\begin{isamarkuptext}%
haftmann@22845
   678
\item replacing executable but inconvenient constructs:%
haftmann@22845
   679
\end{isamarkuptext}%
haftmann@22845
   680
\isamarkuptrue%
haftmann@22845
   681
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   682
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   683
\ \ \ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}%
wenzelm@21172
   684
\isadelimproof
wenzelm@21172
   685
\ %
wenzelm@21172
   686
\endisadelimproof
wenzelm@21172
   687
%
wenzelm@21172
   688
\isatagproof
wenzelm@21172
   689
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   690
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   691
\endisatagproof
wenzelm@21172
   692
{\isafoldproof}%
wenzelm@21172
   693
%
wenzelm@21172
   694
\isadelimproof
wenzelm@21172
   695
%
wenzelm@21172
   696
\endisadelimproof
wenzelm@21172
   697
%
wenzelm@21172
   698
\end{itemize}
wenzelm@21172
   699
%
wenzelm@21172
   700
\begin{isamarkuptext}%
haftmann@27609
   701
\emph{Function transformers} provide a very general interface,
wenzelm@21172
   702
  transforming a list of function theorems to another
wenzelm@21172
   703
  list of function theorems, provided that neither the heading
wenzelm@21172
   704
  constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
haftmann@21348
   705
  pattern elimination implemented in
haftmann@27609
   706
  theory \isa{Efficient{\isacharunderscore}Nat} (\secref{eff_nat}) uses this
wenzelm@21172
   707
  interface.
wenzelm@21172
   708
haftmann@27557
   709
  \noindent The current setup of the preprocessor may be inspected using
haftmann@27557
   710
  the \isa{{\isasymPRINTCODESETUP}} command.
haftmann@27557
   711
wenzelm@21172
   712
  \begin{warn}
haftmann@27609
   713
    The attribute \emph{code unfold}
wenzelm@21172
   714
    associated with the existing code generator also applies to
wenzelm@21172
   715
    the new one: \emph{code unfold} implies \emph{code inline}.
wenzelm@21172
   716
  \end{warn}%
wenzelm@21172
   717
\end{isamarkuptext}%
wenzelm@21172
   718
\isamarkuptrue%
wenzelm@21172
   719
%
haftmann@22798
   720
\isamarkupsubsection{Concerning operational equality%
haftmann@22798
   721
}
haftmann@22798
   722
\isamarkuptrue%
haftmann@22798
   723
%
haftmann@22798
   724
\begin{isamarkuptext}%
haftmann@22798
   725
Surely you have already noticed how equality is treated
haftmann@22798
   726
  by the code generator:%
haftmann@22798
   727
\end{isamarkuptext}%
haftmann@22798
   728
\isamarkuptrue%
haftmann@25870
   729
\isacommand{primrec}\isamarkupfalse%
haftmann@22798
   730
\isanewline
haftmann@22798
   731
\ \ collect{\isacharunderscore}duplicates\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
   732
\ \ \ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
haftmann@22798
   733
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
haftmann@22798
   734
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
haftmann@22798
   735
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
haftmann@22798
   736
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
haftmann@22798
   737
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22798
   738
\begin{isamarkuptext}%
haftmann@22798
   739
The membership test during preprocessing is rewritten,
haftmann@22798
   740
  resulting in \isa{op\ mem}, which itself
haftmann@22798
   741
  performs an explicit equality check.%
haftmann@22798
   742
\end{isamarkuptext}%
haftmann@22798
   743
\isamarkuptrue%
haftmann@24379
   744
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   745
\ collect{\isacharunderscore}duplicates\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   746
\begin{isamarkuptext}%
haftmann@22798
   747
\lstsml{Thy/examples/collect_duplicates.ML}%
haftmann@22798
   748
\end{isamarkuptext}%
haftmann@22798
   749
\isamarkuptrue%
haftmann@22798
   750
%
haftmann@22798
   751
\begin{isamarkuptext}%
haftmann@22798
   752
Obviously, polymorphic equality is implemented the Haskell
haftmann@26513
   753
  way using a type class.  How is this achieved?  HOL introduces
haftmann@26513
   754
  an explicit class \isa{eq} with a corresponding operation
haftmann@26513
   755
  \isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ x\ y\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ y{\isacharparenright}}.
haftmann@26513
   756
  The preprocessing framework does the rest.
haftmann@22798
   757
  For datatypes, instances of \isa{eq} are implicitly derived
haftmann@26513
   758
  when possible.  For other types, you may instantiate \isa{eq}
haftmann@26513
   759
  manually like any other type class.
haftmann@22798
   760
haftmann@22798
   761
  Though this \isa{eq} class is designed to get rarely in
haftmann@22798
   762
  the way, a subtlety
haftmann@22798
   763
  enters the stage when definitions of overloaded constants
haftmann@22798
   764
  are dependent on operational equality.  For example, let
haftmann@22798
   765
  us define a lexicographic ordering on tuples:%
haftmann@22798
   766
\end{isamarkuptext}%
haftmann@22798
   767
\isamarkuptrue%
haftmann@25870
   768
\isacommand{instantiation}\isamarkupfalse%
haftmann@22798
   769
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline
haftmann@25870
   770
\isakeyword{begin}\isanewline
haftmann@25870
   771
\isanewline
haftmann@25870
   772
\isacommand{definition}\isamarkupfalse%
haftmann@25870
   773
\isanewline
haftmann@25870
   774
\ \ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isacharless}\ p{\isadigit{2}}\ {\isasymlongleftrightarrow}\ {\isacharparenleft}let\ {\isacharparenleft}x{\isadigit{1}}{\isacharcomma}\ y{\isadigit{1}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline
haftmann@25870
   775
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25870
   776
\isanewline
haftmann@25870
   777
\isacommand{definition}\isamarkupfalse%
haftmann@25870
   778
\isanewline
haftmann@25870
   779
\ \ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isasymle}\ p{\isadigit{2}}\ {\isasymlongleftrightarrow}\ {\isacharparenleft}let\ {\isacharparenleft}x{\isadigit{1}}{\isacharcomma}\ y{\isadigit{1}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline
haftmann@25870
   780
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@25870
   781
\isanewline
haftmann@25870
   782
\isacommand{instance}\isamarkupfalse%
haftmann@25870
   783
%
haftmann@22798
   784
\isadelimproof
haftmann@22798
   785
\ %
haftmann@22798
   786
\endisadelimproof
haftmann@22798
   787
%
haftmann@22798
   788
\isatagproof
haftmann@22798
   789
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
haftmann@22798
   790
%
haftmann@22798
   791
\endisatagproof
haftmann@22798
   792
{\isafoldproof}%
haftmann@22798
   793
%
haftmann@22798
   794
\isadelimproof
haftmann@22798
   795
%
haftmann@22798
   796
\endisadelimproof
haftmann@22798
   797
\isanewline
haftmann@22798
   798
\isanewline
haftmann@25870
   799
\isacommand{end}\isamarkupfalse%
haftmann@25870
   800
\isanewline
haftmann@22798
   801
\isanewline
haftmann@22798
   802
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   803
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   804
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22798
   805
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22798
   806
%
haftmann@22798
   807
\isadelimproof
haftmann@22798
   808
\ \ %
haftmann@22798
   809
\endisadelimproof
haftmann@22798
   810
%
haftmann@22798
   811
\isatagproof
haftmann@22798
   812
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   813
\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   814
\ simp{\isacharunderscore}all%
haftmann@22798
   815
\endisatagproof
haftmann@22798
   816
{\isafoldproof}%
haftmann@22798
   817
%
haftmann@22798
   818
\isadelimproof
haftmann@22798
   819
%
haftmann@22798
   820
\endisadelimproof
haftmann@22798
   821
%
haftmann@22798
   822
\begin{isamarkuptext}%
haftmann@22798
   823
Then code generation will fail.  Why?  The definition
haftmann@22798
   824
  of \isa{op\ {\isasymle}} depends on equality on both arguments,
haftmann@22798
   825
  which are polymorphic and impose an additional \isa{eq}
haftmann@22798
   826
  class constraint, thus violating the type discipline
haftmann@22798
   827
  for class operations.
haftmann@22798
   828
haftmann@22798
   829
  The solution is to add \isa{eq} explicitly to the first sort arguments in the
haftmann@22798
   830
  code theorems:%
haftmann@22798
   831
\end{isamarkuptext}%
haftmann@22798
   832
\isamarkuptrue%
haftmann@22798
   833
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   834
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   835
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}ord{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@22798
   836
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22798
   837
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}ord{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@22798
   838
\ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ {\isacharparenleft}x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22798
   839
%
haftmann@22798
   840
\isadelimproof
haftmann@22798
   841
\ \ %
haftmann@22798
   842
\endisadelimproof
haftmann@22798
   843
%
haftmann@22798
   844
\isatagproof
haftmann@22798
   845
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   846
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   847
\ rule{\isacharplus}%
haftmann@22798
   848
\endisatagproof
haftmann@22798
   849
{\isafoldproof}%
haftmann@22798
   850
%
haftmann@22798
   851
\isadelimproof
haftmann@22798
   852
%
haftmann@22798
   853
\endisadelimproof
haftmann@22798
   854
%
haftmann@22798
   855
\begin{isamarkuptext}%
haftmann@22798
   856
\noindent Then code generation succeeds:%
haftmann@22798
   857
\end{isamarkuptext}%
haftmann@22798
   858
\isamarkuptrue%
haftmann@24379
   859
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22798
   860
\ {\isachardoublequoteopen}op\ {\isasymle}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}eq{\isacharcomma}\ ord{\isacharbraceright}\ {\isasymtimes}\ {\isacharprime}b{\isasymColon}ord\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}b\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
haftmann@22845
   861
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   862
\begin{isamarkuptext}%
haftmann@22798
   863
\lstsml{Thy/examples/lexicographic.ML}%
haftmann@22798
   864
\end{isamarkuptext}%
haftmann@22798
   865
\isamarkuptrue%
haftmann@22798
   866
%
haftmann@22798
   867
\begin{isamarkuptext}%
haftmann@22798
   868
In general, code theorems for overloaded constants may have more
haftmann@22798
   869
  restrictive sort constraints than the underlying instance relation
haftmann@22798
   870
  between class and type constructor as long as the whole system of
haftmann@22798
   871
  constraints is coregular; code theorems violating coregularity
haftmann@22798
   872
  are rejected immediately.  Consequently, it might be necessary
haftmann@22798
   873
  to delete disturbing theorems in the code theorem table,
haftmann@22798
   874
  as we have done here with the original definitions \isa{less{\isacharunderscore}prod{\isacharunderscore}def}
haftmann@22885
   875
  and \isa{less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def}.
haftmann@22885
   876
haftmann@22885
   877
  In some cases, the automatically derived defining equations
haftmann@22885
   878
  for equality on a particular type may not be appropriate.
haftmann@22885
   879
  As example, watch the following datatype representing
haftmann@23132
   880
  monomorphic parametric types (where type constructors
haftmann@23132
   881
  are referred to by natural numbers):%
haftmann@22885
   882
\end{isamarkuptext}%
haftmann@22885
   883
\isamarkuptrue%
haftmann@22885
   884
\isacommand{datatype}\isamarkupfalse%
haftmann@23132
   885
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}%
haftmann@22885
   886
\isadelimproof
haftmann@22885
   887
%
haftmann@22885
   888
\endisadelimproof
haftmann@22885
   889
%
haftmann@22885
   890
\isatagproof
haftmann@22885
   891
%
haftmann@22885
   892
\endisatagproof
haftmann@22885
   893
{\isafoldproof}%
haftmann@22885
   894
%
haftmann@22885
   895
\isadelimproof
haftmann@22885
   896
%
haftmann@22885
   897
\endisadelimproof
haftmann@22885
   898
%
haftmann@22885
   899
\begin{isamarkuptext}%
haftmann@22885
   900
Then code generation for SML would fail with a message
haftmann@22885
   901
  that the generated code conains illegal mutual dependencies:
haftmann@23132
   902
  the theorem \isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}} already requires the
haftmann@22885
   903
  instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires
haftmann@23132
   904
  \isa{Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymequiv}\ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ typargs{\isadigit{1}}\ {\isacharequal}\ typargs{\isadigit{2}}};  Haskell has no problem with mutually
haftmann@22885
   905
  recursive \isa{instance} and \isa{function} definitions,
haftmann@22885
   906
  but the SML serializer does not support this.
haftmann@22885
   907
haftmann@22885
   908
  In such cases, you have to provide you own equality equations
haftmann@22885
   909
  involving auxiliary constants.  In our case,
haftmann@22885
   910
  \isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:%
haftmann@22885
   911
\end{isamarkuptext}%
haftmann@22885
   912
\isamarkuptrue%
haftmann@22885
   913
\isacommand{lemma}\isamarkupfalse%
haftmann@22885
   914
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22885
   915
\ \ {\isachardoublequoteopen}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymlongleftrightarrow}\isanewline
haftmann@22885
   916
\ \ \ \ \ tyco{\isadigit{1}}\ {\isacharequal}\ tyco{\isadigit{2}}\ {\isasymand}\ list{\isacharunderscore}all{\isadigit{2}}\ {\isacharparenleft}op\ {\isacharequal}{\isacharparenright}\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@22885
   917
%
haftmann@22885
   918
\isadelimproof
haftmann@22885
   919
\ \ %
haftmann@22885
   920
\endisadelimproof
haftmann@22885
   921
%
haftmann@22885
   922
\isatagproof
haftmann@22885
   923
\isacommand{by}\isamarkupfalse%
haftmann@22885
   924
\ {\isacharparenleft}simp\ add{\isacharcolon}\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}%
haftmann@22885
   925
\endisatagproof
haftmann@22885
   926
{\isafoldproof}%
haftmann@22885
   927
%
haftmann@22885
   928
\isadelimproof
haftmann@22885
   929
%
haftmann@22885
   930
\endisadelimproof
haftmann@22885
   931
%
haftmann@22885
   932
\begin{isamarkuptext}%
haftmann@22885
   933
does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:%
haftmann@22885
   934
\end{isamarkuptext}%
haftmann@22885
   935
\isamarkuptrue%
haftmann@24379
   936
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22885
   937
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isacharcolon}{\isacharcolon}\ monotype\ {\isasymRightarrow}\ monotype\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
haftmann@22885
   938
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}monotype{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22885
   939
\begin{isamarkuptext}%
haftmann@22885
   940
\lstsml{Thy/examples/monotype.ML}%
haftmann@22798
   941
\end{isamarkuptext}%
haftmann@22798
   942
\isamarkuptrue%
haftmann@22798
   943
%
haftmann@22798
   944
\isamarkupsubsection{Programs as sets of theorems%
haftmann@22798
   945
}
haftmann@22798
   946
\isamarkuptrue%
haftmann@22798
   947
%
haftmann@22798
   948
\begin{isamarkuptext}%
haftmann@22798
   949
As told in \secref{sec:concept}, code generation is based
haftmann@22798
   950
  on a structured collection of code theorems.
haftmann@22798
   951
  For explorative purpose, this collection
haftmann@22798
   952
  may be inspected using the \isa{{\isasymCODETHMS}} command:%
haftmann@22798
   953
\end{isamarkuptext}%
haftmann@22798
   954
\isamarkuptrue%
haftmann@22798
   955
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
haftmann@22798
   956
\ {\isachardoublequoteopen}op\ mod\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}%
haftmann@22798
   957
\begin{isamarkuptext}%
haftmann@22798
   958
\noindent prints a table with \emph{all} defining equations
haftmann@22798
   959
  for \isa{op\ mod}, including
haftmann@22798
   960
  \emph{all} defining equations those equations depend
haftmann@22798
   961
  on recursivly.  \isa{{\isasymCODETHMS}} provides a convenient
haftmann@22798
   962
  mechanism to inspect the impact of a preprocessor setup
haftmann@22798
   963
  on defining equations.
haftmann@22798
   964
  
haftmann@22798
   965
  Similarly, the \isa{{\isasymCODEDEPS}} command shows a graph
haftmann@22798
   966
  visualizing dependencies between defining equations.%
haftmann@22798
   967
\end{isamarkuptext}%
haftmann@22798
   968
\isamarkuptrue%
haftmann@22798
   969
%
haftmann@25411
   970
\isamarkupsubsection{Constructor sets for datatypes%
haftmann@25411
   971
}
haftmann@25411
   972
\isamarkuptrue%
haftmann@25411
   973
%
haftmann@25411
   974
\begin{isamarkuptext}%
haftmann@25411
   975
Conceptually, any datatype is spanned by a set of
haftmann@25411
   976
  \emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n}
haftmann@25411
   977
  where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all}
haftmann@25411
   978
  type variables in \isa{{\isasymtau}}.  The HOL datatype package
haftmann@25411
   979
  by default registers any new datatype in the table
haftmann@25411
   980
  of datatypes, which may be inspected using
haftmann@25411
   981
  the \isa{{\isasymPRINTCODESETUP}} command.
haftmann@25411
   982
haftmann@25411
   983
  In some cases, it may be convenient to alter or
haftmann@26999
   984
  extend this table;  as an example, we will develope an alternative
haftmann@26999
   985
  representation of natural numbers as binary digits, whose
haftmann@26999
   986
  size does increase logarithmically with its value, not linear
haftmann@26999
   987
  \footnote{Indeed, the \isa{Efficient{\isacharunderscore}Nat} theory \ref{eff_nat}
haftmann@26999
   988
    does something similar}.  First, the digit representation:%
haftmann@26999
   989
\end{isamarkuptext}%
haftmann@26999
   990
\isamarkuptrue%
haftmann@26999
   991
\isacommand{definition}\isamarkupfalse%
haftmann@26999
   992
\ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@26999
   993
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ n\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
   994
\isanewline
haftmann@26999
   995
\isacommand{definition}\isamarkupfalse%
haftmann@26999
   996
\ Dig{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@26999
   997
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}{\isachardoublequoteclose}%
haftmann@26999
   998
\begin{isamarkuptext}%
haftmann@26999
   999
\noindent We will use these two ">digits"< to represent natural numbers
haftmann@26999
  1000
  in binary digits, e.g.:%
haftmann@26999
  1001
\end{isamarkuptext}%
haftmann@26999
  1002
\isamarkuptrue%
haftmann@26999
  1003
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1004
\ {\isadigit{4}}{\isadigit{2}}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{4}}{\isadigit{2}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1005
%
haftmann@26999
  1006
\isadelimproof
haftmann@26999
  1007
\ \ %
haftmann@26999
  1008
\endisadelimproof
haftmann@26999
  1009
%
haftmann@26999
  1010
\isatagproof
haftmann@26999
  1011
\isacommand{by}\isamarkupfalse%
haftmann@26999
  1012
\ {\isacharparenleft}simp\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@26999
  1013
\endisatagproof
haftmann@26999
  1014
{\isafoldproof}%
haftmann@26999
  1015
%
haftmann@26999
  1016
\isadelimproof
haftmann@26999
  1017
%
haftmann@26999
  1018
\endisadelimproof
haftmann@26999
  1019
%
haftmann@26999
  1020
\begin{isamarkuptext}%
haftmann@26999
  1021
\noindent Of course we also have to provide proper code equations for
haftmann@26999
  1022
  the operations, e.g. \isa{op\ {\isacharplus}}:%
haftmann@26999
  1023
\end{isamarkuptext}%
haftmann@26999
  1024
\isamarkuptrue%
haftmann@26999
  1025
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1026
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@26999
  1027
\ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
  1028
\ \ {\isachardoublequoteopen}m\ {\isacharplus}\ {\isadigit{0}}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
haftmann@26999
  1029
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
  1030
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{1}}\ m{\isachardoublequoteclose}\isanewline
haftmann@26999
  1031
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1032
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1033
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1034
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1035
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1036
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1037
%
haftmann@26999
  1038
\isadelimproof
haftmann@26999
  1039
\ \ %
haftmann@26999
  1040
\endisadelimproof
haftmann@26999
  1041
%
haftmann@26999
  1042
\isatagproof
haftmann@26999
  1043
\isacommand{by}\isamarkupfalse%
haftmann@26999
  1044
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@26999
  1045
\endisatagproof
haftmann@26999
  1046
{\isafoldproof}%
haftmann@26999
  1047
%
haftmann@26999
  1048
\isadelimproof
haftmann@26999
  1049
%
haftmann@26999
  1050
\endisadelimproof
haftmann@26999
  1051
%
haftmann@26999
  1052
\begin{isamarkuptext}%
haftmann@26999
  1053
\noindent We then instruct the code generator to view \isa{{\isadigit{0}}},
haftmann@26999
  1054
  \isa{{\isadigit{1}}}, \isa{Dig{\isadigit{0}}} and \isa{Dig{\isadigit{1}}} as
haftmann@26999
  1055
  datatype constructors:%
haftmann@26999
  1056
\end{isamarkuptext}%
haftmann@26999
  1057
\isamarkuptrue%
haftmann@26999
  1058
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@26999
  1059
\ {\isachardoublequoteopen}{\isadigit{0}}{\isasymColon}nat{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isadigit{1}}{\isasymColon}nat{\isachardoublequoteclose}\ Dig{\isadigit{0}}\ Dig{\isadigit{1}}%
haftmann@26999
  1060
\begin{isamarkuptext}%
haftmann@26999
  1061
\noindent For the former constructor \isa{Suc}, we provide a code
haftmann@26999
  1062
  equation and remove some parts of the default code generator setup
haftmann@26999
  1063
  which are an obstacle here:%
haftmann@26999
  1064
\end{isamarkuptext}%
haftmann@26999
  1065
\isamarkuptrue%
haftmann@26999
  1066
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1067
\ Suc{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@26999
  1068
\ \ {\isachardoublequoteopen}Suc\ n\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1069
%
haftmann@26999
  1070
\isadelimproof
haftmann@26999
  1071
\ \ %
haftmann@26999
  1072
\endisadelimproof
haftmann@26999
  1073
%
haftmann@26999
  1074
\isatagproof
haftmann@26999
  1075
\isacommand{by}\isamarkupfalse%
haftmann@26999
  1076
\ simp%
haftmann@26999
  1077
\endisatagproof
haftmann@26999
  1078
{\isafoldproof}%
haftmann@26999
  1079
%
haftmann@26999
  1080
\isadelimproof
haftmann@26999
  1081
\isanewline
haftmann@26999
  1082
%
haftmann@26999
  1083
\endisadelimproof
haftmann@26999
  1084
\isanewline
haftmann@26999
  1085
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1086
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline\ del{\isacharbrackright}\isanewline
haftmann@26999
  1087
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1088
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}%
haftmann@26999
  1089
\begin{isamarkuptext}%
haftmann@26999
  1090
\noindent This yields the following code:%
haftmann@26999
  1091
\end{isamarkuptext}%
haftmann@26999
  1092
\isamarkuptrue%
haftmann@26999
  1093
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@26999
  1094
\ {\isachardoublequoteopen}op\ {\isacharplus}\ {\isasymColon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}nat{\isacharunderscore}binary{\isachardot}ML{\isachardoublequoteclose}%
haftmann@26999
  1095
\begin{isamarkuptext}%
haftmann@26999
  1096
\lstsml{Thy/examples/nat_binary.ML}%
haftmann@25411
  1097
\end{isamarkuptext}%
haftmann@25411
  1098
\isamarkuptrue%
haftmann@25411
  1099
%
haftmann@25411
  1100
\begin{isamarkuptext}%
haftmann@26973
  1101
\medskip
haftmann@25411
  1102
haftmann@26999
  1103
  From this example, it can be easily glimpsed that using own constructor sets
haftmann@26999
  1104
  is a little delicate since it changes the set of valid patterns for values
haftmann@26999
  1105
  of that type.  Without going into much detail, here some practical hints:
haftmann@26999
  1106
haftmann@26999
  1107
  \begin{itemize}
haftmann@26999
  1108
    \item When changing the constuctor set for datatypes, take care to
haftmann@26999
  1109
      provide an alternative for the \isa{case} combinator (e.g. by replacing
haftmann@26999
  1110
      it using the preprocessor).
haftmann@26999
  1111
    \item Values in the target language need not to be normalized -- different
haftmann@26999
  1112
      values in the target language may represent the same value in the
haftmann@26999
  1113
      logic (e.g. \isa{Dig{\isadigit{1}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}}).
haftmann@26999
  1114
    \item Usually, a good methodology to deal with the subleties of pattern
haftmann@26999
  1115
      matching is to see the type as an abstract type: provide a set
haftmann@26999
  1116
      of operations which operate on the concrete representation of the type,
haftmann@26999
  1117
      and derive further operations by combinations of these primitive ones,
haftmann@26999
  1118
      without relying on a particular representation.
haftmann@26999
  1119
  \end{itemize}%
haftmann@25411
  1120
\end{isamarkuptext}%
haftmann@25411
  1121
\isamarkuptrue%
haftmann@25411
  1122
%
haftmann@26999
  1123
\isadeliminvisible
haftmann@26999
  1124
%
haftmann@26999
  1125
\endisadeliminvisible
haftmann@26999
  1126
%
haftmann@26999
  1127
\isataginvisible
haftmann@26999
  1128
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@26999
  1129
\ {\isachardoublequoteopen}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isachardoublequoteclose}\ Suc\isanewline
haftmann@26999
  1130
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1131
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\isanewline
haftmann@26999
  1132
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1133
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline{\isacharbrackright}\isanewline
haftmann@26999
  1134
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1135
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func{\isacharbrackright}\ \isanewline
haftmann@26999
  1136
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1137
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ {\isacharparenleft}n\ {\isasymColon}\ nat{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@26999
  1138
\ simp%
haftmann@26999
  1139
\endisataginvisible
haftmann@26999
  1140
{\isafoldinvisible}%
haftmann@26999
  1141
%
haftmann@26999
  1142
\isadeliminvisible
haftmann@26999
  1143
%
haftmann@26999
  1144
\endisadeliminvisible
haftmann@26999
  1145
%
wenzelm@21172
  1146
\isamarkupsubsection{Customizing serialization%
wenzelm@21172
  1147
}
wenzelm@21172
  1148
\isamarkuptrue%
wenzelm@21172
  1149
%
haftmann@22798
  1150
\isamarkupsubsubsection{Basics%
haftmann@22798
  1151
}
haftmann@22798
  1152
\isamarkuptrue%
haftmann@22798
  1153
%
wenzelm@21172
  1154
\begin{isamarkuptext}%
wenzelm@21172
  1155
Consider the following function and its corresponding
wenzelm@21172
  1156
  SML code:%
wenzelm@21172
  1157
\end{isamarkuptext}%
wenzelm@21172
  1158
\isamarkuptrue%
haftmann@25870
  1159
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
  1160
\isanewline
wenzelm@21172
  1161
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
  1162
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}%
haftmann@21348
  1163
\isadelimtt
wenzelm@21172
  1164
%
haftmann@21348
  1165
\endisadelimtt
haftmann@21348
  1166
%
haftmann@21348
  1167
\isatagtt
wenzelm@21172
  1168
%
haftmann@21348
  1169
\endisatagtt
haftmann@21348
  1170
{\isafoldtt}%
haftmann@21348
  1171
%
haftmann@21348
  1172
\isadelimtt
wenzelm@21172
  1173
%
haftmann@21348
  1174
\endisadelimtt
haftmann@24379
  1175
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1176
\ in{\isacharunderscore}interval\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1177
\begin{isamarkuptext}%
haftmann@21348
  1178
\lstsml{Thy/examples/bool_literal.ML}
wenzelm@21172
  1179
haftmann@22798
  1180
  \noindent Though this is correct code, it is a little bit unsatisfactory:
wenzelm@21172
  1181
  boolean values and operators are materialized as distinguished
wenzelm@21172
  1182
  entities with have nothing to do with the SML-builtin notion
wenzelm@21172
  1183
  of \qt{bool}.  This results in less readable code;
wenzelm@21172
  1184
  additionally, eager evaluation may cause programs to
wenzelm@21172
  1185
  loop or break which would perfectly terminate when
wenzelm@21172
  1186
  the existing SML \qt{bool} would be used.  To map
wenzelm@21172
  1187
  the HOL \qt{bool} on SML \qt{bool}, we may use
wenzelm@21172
  1188
  \qn{custom serializations}:%
wenzelm@21172
  1189
\end{isamarkuptext}%
wenzelm@21172
  1190
\isamarkuptrue%
haftmann@21348
  1191
%
haftmann@21348
  1192
\isadelimtt
haftmann@21348
  1193
%
haftmann@21348
  1194
\endisadelimtt
haftmann@21348
  1195
%
haftmann@21348
  1196
\isatagtt
wenzelm@21172
  1197
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1198
\ bool\isanewline
wenzelm@21172
  1199
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1200
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1201
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
wenzelm@21172
  1202
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1203
\endisatagtt
haftmann@21348
  1204
{\isafoldtt}%
haftmann@21348
  1205
%
haftmann@21348
  1206
\isadelimtt
haftmann@21348
  1207
%
haftmann@21348
  1208
\endisadelimtt
haftmann@21348
  1209
%
wenzelm@21172
  1210
\begin{isamarkuptext}%
haftmann@21348
  1211
The \isa{{\isasymCODETYPE}} commad takes a type constructor
wenzelm@21172
  1212
  as arguments together with a list of custom serializations.
wenzelm@21172
  1213
  Each custom serialization starts with a target language
wenzelm@21172
  1214
  identifier followed by an expression, which during
wenzelm@21172
  1215
  code serialization is inserted whenever the type constructor
haftmann@21348
  1216
  would occur.  For constants, \isa{{\isasymCODECONST}} implements
haftmann@21348
  1217
  the corresponding mechanism.  Each ``\verb|_|'' in
wenzelm@21172
  1218
  a serialization expression is treated as a placeholder
wenzelm@21172
  1219
  for the type constructor's (the constant's) arguments.%
wenzelm@21172
  1220
\end{isamarkuptext}%
wenzelm@21172
  1221
\isamarkuptrue%
haftmann@24379
  1222
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1223
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1224
\begin{isamarkuptext}%
haftmann@21348
  1225
\lstsml{Thy/examples/bool_mlbool.ML}
wenzelm@21172
  1226
haftmann@22798
  1227
  \noindent This still is not perfect: the parentheses
haftmann@21348
  1228
  around the \qt{andalso} expression are superfluous.
haftmann@21348
  1229
  Though the serializer
wenzelm@21172
  1230
  by no means attempts to imitate the rich Isabelle syntax
wenzelm@21172
  1231
  framework, it provides some common idioms, notably
wenzelm@21172
  1232
  associative infixes with precedences which may be used here:%
wenzelm@21172
  1233
\end{isamarkuptext}%
wenzelm@21172
  1234
\isamarkuptrue%
haftmann@21348
  1235
%
haftmann@21348
  1236
\isadelimtt
haftmann@21348
  1237
%
haftmann@21348
  1238
\endisadelimtt
haftmann@21348
  1239
%
haftmann@21348
  1240
\isatagtt
wenzelm@21172
  1241
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1242
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
haftmann@21348
  1243
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1244
\endisatagtt
haftmann@21348
  1245
{\isafoldtt}%
haftmann@21348
  1246
%
haftmann@21348
  1247
\isadelimtt
haftmann@21348
  1248
%
haftmann@21348
  1249
\endisadelimtt
haftmann@21348
  1250
\isanewline
wenzelm@21172
  1251
\isanewline
haftmann@24379
  1252
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1253
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1254
\begin{isamarkuptext}%
haftmann@21348
  1255
\lstsml{Thy/examples/bool_infix.ML}
wenzelm@21172
  1256
haftmann@22798
  1257
  \medskip
haftmann@22798
  1258
wenzelm@21172
  1259
  Next, we try to map HOL pairs to SML pairs, using the
haftmann@21348
  1260
  infix ``\verb|*|'' type constructor and parentheses:%
wenzelm@21172
  1261
\end{isamarkuptext}%
wenzelm@21172
  1262
\isamarkuptrue%
haftmann@21348
  1263
%
haftmann@21348
  1264
\isadelimtt
haftmann@21348
  1265
%
haftmann@21348
  1266
\endisadelimtt
haftmann@21348
  1267
%
haftmann@21348
  1268
\isatagtt
wenzelm@21172
  1269
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1270
\ {\isacharasterisk}\isanewline
wenzelm@21172
  1271
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1272
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1273
\ Pair\isanewline
wenzelm@21172
  1274
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1275
\endisatagtt
haftmann@21348
  1276
{\isafoldtt}%
haftmann@21348
  1277
%
haftmann@21348
  1278
\isadelimtt
haftmann@21348
  1279
%
haftmann@21348
  1280
\endisadelimtt
haftmann@21348
  1281
%
wenzelm@21172
  1282
\begin{isamarkuptext}%
haftmann@21348
  1283
The initial bang ``\verb|!|'' tells the serializer to never put
wenzelm@21172
  1284
  parentheses around the whole expression (they are already present),
wenzelm@21172
  1285
  while the parentheses around argument place holders
wenzelm@21172
  1286
  tell not to put parentheses around the arguments.
haftmann@21348
  1287
  The slash ``\verb|/|'' (followed by arbitrary white space)
wenzelm@21172
  1288
  inserts a space which may be used as a break if necessary
wenzelm@21172
  1289
  during pretty printing.
wenzelm@21172
  1290
haftmann@22798
  1291
  These examples give a glimpse what mechanisms
wenzelm@21186
  1292
  custom serializations provide; however their usage
wenzelm@21186
  1293
  requires careful thinking in order not to introduce
wenzelm@21186
  1294
  inconsistencies -- or, in other words:
wenzelm@21186
  1295
  custom serializations are completely axiomatic.
wenzelm@21186
  1296
wenzelm@21186
  1297
  A further noteworthy details is that any special
wenzelm@21186
  1298
  character in a custom serialization may be quoted
haftmann@21348
  1299
  using ``\verb|'|''; thus, in
haftmann@21348
  1300
  ``\verb|fn '_ => _|'' the first
haftmann@21348
  1301
  ``\verb|_|'' is a proper underscore while the
haftmann@21348
  1302
  second ``\verb|_|'' is a placeholder.
wenzelm@21186
  1303
wenzelm@21186
  1304
  The HOL theories provide further
haftmann@25411
  1305
  examples for custom serializations.%
wenzelm@21186
  1306
\end{isamarkuptext}%
wenzelm@21172
  1307
\isamarkuptrue%
wenzelm@21172
  1308
%
wenzelm@21186
  1309
\isamarkupsubsubsection{Haskell serialization%
wenzelm@21186
  1310
}
wenzelm@21186
  1311
\isamarkuptrue%
wenzelm@21186
  1312
%
wenzelm@21186
  1313
\begin{isamarkuptext}%
wenzelm@21186
  1314
For convenience, the default
wenzelm@21186
  1315
  HOL setup for Haskell maps the \isa{eq} class to
wenzelm@21186
  1316
  its counterpart in Haskell, giving custom serializations
haftmann@21348
  1317
  for the class (\isa{{\isasymCODECLASS}}) and its operation:%
wenzelm@21186
  1318
\end{isamarkuptext}%
wenzelm@21186
  1319
\isamarkuptrue%
wenzelm@21186
  1320
%
haftmann@21348
  1321
\isadelimtt
haftmann@21348
  1322
%
haftmann@21348
  1323
\endisadelimtt
haftmann@21348
  1324
%
haftmann@21348
  1325
\isatagtt
wenzelm@21186
  1326
\isacommand{code{\isacharunderscore}class}\isamarkupfalse%
wenzelm@21186
  1327
\ eq\isanewline
haftmann@22798
  1328
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21186
  1329
\isanewline
wenzelm@21186
  1330
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
haftmann@22798
  1331
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline
haftmann@22798
  1332
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1333
\endisatagtt
haftmann@21348
  1334
{\isafoldtt}%
haftmann@21348
  1335
%
haftmann@21348
  1336
\isadelimtt
haftmann@21348
  1337
%
haftmann@21348
  1338
\endisadelimtt
haftmann@21348
  1339
%
wenzelm@21186
  1340
\begin{isamarkuptext}%
wenzelm@21186
  1341
A problem now occurs whenever a type which
wenzelm@21186
  1342
  is an instance of \isa{eq} in HOL is mapped
wenzelm@21186
  1343
  on a Haskell-builtin type which is also an instance
wenzelm@21186
  1344
  of Haskell \isa{Eq}:%
wenzelm@21186
  1345
\end{isamarkuptext}%
wenzelm@21186
  1346
\isamarkuptrue%
wenzelm@21186
  1347
\isacommand{typedecl}\isamarkupfalse%
wenzelm@21186
  1348
\ bar\isanewline
wenzelm@21186
  1349
\isanewline
haftmann@26513
  1350
\isacommand{instantiation}\isamarkupfalse%
haftmann@26513
  1351
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
haftmann@26513
  1352
\isakeyword{begin}\isanewline
haftmann@26513
  1353
\isanewline
haftmann@26513
  1354
\isacommand{definition}\isamarkupfalse%
haftmann@26732
  1355
\ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}x{\isasymColon}bar{\isacharparenright}\ y\ {\isasymlongleftrightarrow}\ x\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
haftmann@26513
  1356
\isanewline
wenzelm@21186
  1357
\isacommand{instance}\isamarkupfalse%
haftmann@26513
  1358
%
wenzelm@21186
  1359
\isadelimproof
wenzelm@21186
  1360
\ %
wenzelm@21186
  1361
\endisadelimproof
wenzelm@21186
  1362
%
wenzelm@21186
  1363
\isatagproof
haftmann@26513
  1364
\isacommand{by}\isamarkupfalse%
haftmann@26513
  1365
\ default\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq{\isacharunderscore}bar{\isacharunderscore}def{\isacharparenright}%
wenzelm@21186
  1366
\endisatagproof
wenzelm@21186
  1367
{\isafoldproof}%
wenzelm@21186
  1368
%
wenzelm@21186
  1369
\isadelimproof
wenzelm@21172
  1370
%
wenzelm@21186
  1371
\endisadelimproof
wenzelm@21186
  1372
\isanewline
haftmann@26513
  1373
\isanewline
haftmann@26513
  1374
\isacommand{end}\isamarkupfalse%
haftmann@26513
  1375
\isanewline
haftmann@21348
  1376
%
haftmann@21348
  1377
\isadelimtt
wenzelm@21186
  1378
\isanewline
haftmann@21348
  1379
%
haftmann@21348
  1380
\endisadelimtt
haftmann@21348
  1381
%
haftmann@21348
  1382
\isatagtt
wenzelm@21186
  1383
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21186
  1384
\ bar\isanewline
wenzelm@21186
  1385
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1386
\endisatagtt
haftmann@21348
  1387
{\isafoldtt}%
haftmann@21348
  1388
%
haftmann@21348
  1389
\isadelimtt
haftmann@21348
  1390
%
haftmann@21348
  1391
\endisadelimtt
haftmann@21348
  1392
%
wenzelm@21186
  1393
\begin{isamarkuptext}%
wenzelm@21186
  1394
The code generator would produce
haftmann@22188
  1395
  an additional instance, which of course is rejected.
haftmann@22188
  1396
  To suppress this additional instance, use
haftmann@22188
  1397
  \isa{{\isasymCODEINSTANCE}}:%
wenzelm@21186
  1398
\end{isamarkuptext}%
wenzelm@21186
  1399
\isamarkuptrue%
haftmann@21348
  1400
%
haftmann@21348
  1401
\isadelimtt
haftmann@21348
  1402
%
haftmann@21348
  1403
\endisadelimtt
haftmann@21348
  1404
%
haftmann@21348
  1405
\isatagtt
wenzelm@21186
  1406
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse%
wenzelm@21186
  1407
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
wenzelm@21186
  1408
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}%
haftmann@21348
  1409
\endisatagtt
haftmann@21348
  1410
{\isafoldtt}%
haftmann@21348
  1411
%
haftmann@21348
  1412
\isadelimtt
haftmann@21348
  1413
%
haftmann@21348
  1414
\endisadelimtt
haftmann@21348
  1415
%
haftmann@22798
  1416
\isamarkupsubsubsection{Pretty printing%
haftmann@22798
  1417
}
haftmann@22798
  1418
\isamarkuptrue%
haftmann@22798
  1419
%
haftmann@22798
  1420
\begin{isamarkuptext}%
haftmann@22798
  1421
The serializer provides ML interfaces to set up
haftmann@22798
  1422
  pretty serializations for expressions like lists, numerals
haftmann@22798
  1423
  and characters;  these are
haftmann@22798
  1424
  monolithic stubs and should only be used with the
haftmann@22798
  1425
  theories introduces in \secref{sec:library}.%
haftmann@22798
  1426
\end{isamarkuptext}%
haftmann@22798
  1427
\isamarkuptrue%
haftmann@22798
  1428
%
haftmann@21348
  1429
\isamarkupsubsection{Cyclic module dependencies%
haftmann@21348
  1430
}
haftmann@21348
  1431
\isamarkuptrue%
haftmann@21348
  1432
%
haftmann@21348
  1433
\begin{isamarkuptext}%
haftmann@21348
  1434
Sometimes the awkward situation occurs that dependencies
haftmann@21348
  1435
  between definitions introduce cyclic dependencies
haftmann@21348
  1436
  between modules, which in the Haskell world leaves
haftmann@21348
  1437
  you to the mercy of the Haskell implementation you are using,
haftmann@21348
  1438
  while for SML code generation is not possible.
haftmann@21348
  1439
haftmann@21348
  1440
  A solution is to declare module names explicitly.
haftmann@21348
  1441
  Let use assume the three cyclically dependent
haftmann@21348
  1442
  modules are named \emph{A}, \emph{B} and \emph{C}.
haftmann@21348
  1443
  Then, by stating%
haftmann@21348
  1444
\end{isamarkuptext}%
haftmann@21348
  1445
\isamarkuptrue%
haftmann@21348
  1446
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse%
haftmann@21348
  1447
\ SML\isanewline
haftmann@21348
  1448
\ \ A\ ABC\isanewline
haftmann@21348
  1449
\ \ B\ ABC\isanewline
haftmann@21348
  1450
\ \ C\ ABC%
haftmann@21348
  1451
\begin{isamarkuptext}%
haftmann@21348
  1452
we explicitly map all those modules on \emph{ABC},
haftmann@21348
  1453
  resulting in an ad-hoc merge of this three modules
haftmann@21348
  1454
  at serialization time.%
haftmann@21348
  1455
\end{isamarkuptext}%
haftmann@21348
  1456
\isamarkuptrue%
haftmann@21348
  1457
%
haftmann@22798
  1458
\isamarkupsubsection{Incremental code generation%
haftmann@22798
  1459
}
haftmann@22798
  1460
\isamarkuptrue%
haftmann@22798
  1461
%
haftmann@22798
  1462
\begin{isamarkuptext}%
haftmann@22798
  1463
Code generation is \emph{incremental}: theorems
haftmann@22798
  1464
  and abstract intermediate code are cached and extended on demand.
haftmann@22798
  1465
  The cache may be partially or fully dropped if the underlying
haftmann@22798
  1466
  executable content of the theory changes.
haftmann@22798
  1467
  Implementation of caching is supposed to transparently
haftmann@22798
  1468
  hid away the details from the user.  Anyway, caching
haftmann@22798
  1469
  reaches the surface by using a slightly more general form
haftmann@22798
  1470
  of the \isa{{\isasymCODETHMS}}, \isa{{\isasymCODEDEPS}}
haftmann@24379
  1471
  and \isa{{\isasymEXPORTCODE}} commands: the list of constants
haftmann@22798
  1472
  may be omitted.  Then, all constants with code theorems
haftmann@22798
  1473
  in the current cache are referred to.%
haftmann@22798
  1474
\end{isamarkuptext}%
haftmann@22798
  1475
\isamarkuptrue%
haftmann@22798
  1476
%
wenzelm@21172
  1477
\isamarkupsection{ML interfaces \label{sec:ml}%
wenzelm@21172
  1478
}
wenzelm@21172
  1479
\isamarkuptrue%
wenzelm@21172
  1480
%
haftmann@21348
  1481
\begin{isamarkuptext}%
haftmann@21348
  1482
Since the code generator framework not only aims to provide
haftmann@21348
  1483
  a nice Isar interface but also to form a base for
haftmann@21348
  1484
  code-generation-based applications, here a short
haftmann@21348
  1485
  description of the most important ML interfaces.%
haftmann@21348
  1486
\end{isamarkuptext}%
haftmann@21348
  1487
\isamarkuptrue%
haftmann@21348
  1488
%
haftmann@24217
  1489
\isamarkupsubsection{Executable theory content: \isa{Code}%
wenzelm@21172
  1490
}
wenzelm@21172
  1491
\isamarkuptrue%
wenzelm@21172
  1492
%
wenzelm@21172
  1493
\begin{isamarkuptext}%
wenzelm@21172
  1494
This Pure module implements the core notions of
wenzelm@21172
  1495
  executable content of a theory.%
wenzelm@21172
  1496
\end{isamarkuptext}%
wenzelm@21172
  1497
\isamarkuptrue%
wenzelm@21172
  1498
%
haftmann@22292
  1499
\isamarkupsubsubsection{Managing executable content%
haftmann@20967
  1500
}
haftmann@20967
  1501
\isamarkuptrue%
haftmann@20967
  1502
%
wenzelm@21172
  1503
\isadelimmlref
wenzelm@21172
  1504
%
wenzelm@21172
  1505
\endisadelimmlref
wenzelm@21172
  1506
%
wenzelm@21172
  1507
\isatagmlref
wenzelm@21172
  1508
%
wenzelm@21172
  1509
\begin{isamarkuptext}%
wenzelm@21172
  1510
\begin{mldecls}
wenzelm@26856
  1511
  \indexml{Code.add\_func}\verb|Code.add_func: thm -> theory -> theory| \\
wenzelm@26856
  1512
  \indexml{Code.del\_func}\verb|Code.del_func: thm -> theory -> theory| \\
wenzelm@26856
  1513
  \indexml{Code.add\_funcl}\verb|Code.add_funcl: string * thm list Susp.T -> theory -> theory| \\
haftmann@27557
  1514
  \indexml{Code.map\_pre}\verb|Code.map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory| \\
haftmann@27557
  1515
  \indexml{Code.map\_post}\verb|Code.map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory| \\
haftmann@27609
  1516
  \indexml{Code.add\_functrans}\verb|Code.add_functrans: string * (theory -> thm list -> thm list option)|\isasep\isanewline%
haftmann@21348
  1517
\verb|    -> theory -> theory| \\
haftmann@27557
  1518
  \indexml{Code.del\_functrans}\verb|Code.del_functrans: string -> theory -> theory| \\
wenzelm@26856
  1519
  \indexml{Code.add\_datatype}\verb|Code.add_datatype: (string * typ) list -> theory -> theory| \\
wenzelm@26856
  1520
  \indexml{Code.get\_datatype}\verb|Code.get_datatype: theory -> string|\isasep\isanewline%
haftmann@22479
  1521
\verb|    -> (string * sort) list * (string * typ list) list| \\
wenzelm@26856
  1522
  \indexml{Code.get\_datatype\_of\_constr}\verb|Code.get_datatype_of_constr: theory -> string -> string option|
wenzelm@21172
  1523
  \end{mldecls}
wenzelm@21172
  1524
wenzelm@21172
  1525
  \begin{description}
wenzelm@21172
  1526
haftmann@24217
  1527
  \item \verb|Code.add_func|~\isa{thm}~\isa{thy} adds function
haftmann@21348
  1528
     theorem \isa{thm} to executable content.
haftmann@21348
  1529
haftmann@24217
  1530
  \item \verb|Code.del_func|~\isa{thm}~\isa{thy} removes function
haftmann@21348
  1531
     theorem \isa{thm} from executable content, if present.
haftmann@21348
  1532
haftmann@24217
  1533
  \item \verb|Code.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds
haftmann@22060
  1534
     suspended defining equations \isa{lthms} for constant
haftmann@21348
  1535
     \isa{const} to executable content.
haftmann@21348
  1536
haftmann@27557
  1537
  \item \verb|Code.map_pre|~\isa{f}~\isa{thy} changes
haftmann@27557
  1538
     the preprocessor simpset.
haftmann@21348
  1539
haftmann@27557
  1540
    \item \verb|Code.add_functrans|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
haftmann@27609
  1541
     function transformer \isa{f} (named \isa{name}) to executable content;
haftmann@27609
  1542
     \isa{f} is a transformer of the defining equations belonging
haftmann@21348
  1543
     to a certain function definition, depending on the
haftmann@27609
  1544
     current theory context.  Returning \isa{NONE} indicates that no
haftmann@27609
  1545
     transformation took place;  otherwise, the whole process will be iterated
haftmann@27609
  1546
     with the new defining equations.
haftmann@21348
  1547
haftmann@27557
  1548
  \item \verb|Code.del_functrans|~\isa{name}~\isa{thy} removes
haftmann@27609
  1549
     function transformer named \isa{name} from executable content.
haftmann@22060
  1550
haftmann@24421
  1551
  \item \verb|Code.add_datatype|~\isa{cs}~\isa{thy} adds
haftmann@24421
  1552
     a datatype to executable content, with generation
haftmann@24421
  1553
     set \isa{cs}.
haftmann@21348
  1554
haftmann@24217
  1555
  \item \verb|Code.get_datatype_of_constr|~\isa{thy}~\isa{const}
haftmann@21348
  1556
     returns type constructor corresponding to
haftmann@21348
  1557
     constructor \isa{const}; returns \isa{NONE}
haftmann@21348
  1558
     if \isa{const} is no constructor.
haftmann@21348
  1559
haftmann@21348
  1560
  \end{description}%
haftmann@21348
  1561
\end{isamarkuptext}%
haftmann@21348
  1562
\isamarkuptrue%
haftmann@21348
  1563
%
haftmann@21348
  1564
\endisatagmlref
haftmann@21348
  1565
{\isafoldmlref}%
haftmann@21348
  1566
%
haftmann@21348
  1567
\isadelimmlref
haftmann@21348
  1568
%
haftmann@21348
  1569
\endisadelimmlref
haftmann@21348
  1570
%
haftmann@22292
  1571
\isamarkupsubsection{Auxiliary%
wenzelm@21172
  1572
}
wenzelm@21172
  1573
\isamarkuptrue%
wenzelm@21172
  1574
%
wenzelm@21172
  1575
\isadelimmlref
wenzelm@21172
  1576
%
wenzelm@21172
  1577
\endisadelimmlref
wenzelm@21172
  1578
%
wenzelm@21172
  1579
\isatagmlref
wenzelm@21172
  1580
%
wenzelm@21172
  1581
\begin{isamarkuptext}%
wenzelm@21172
  1582
\begin{mldecls}
wenzelm@26856
  1583
  \indexml{CodeUnit.read\_const}\verb|CodeUnit.read_const: theory -> string -> string| \\
haftmann@26973
  1584
  \indexml{CodeUnit.head\_func}\verb|CodeUnit.head_func: thm -> string * ((string * sort) list * typ)| \\
haftmann@27557
  1585
  \indexml{CodeUnit.rewrite\_func}\verb|CodeUnit.rewrite_func: MetaSimplifier.simpset -> thm -> thm| \\
haftmann@21348
  1586
  \end{mldecls}
haftmann@21348
  1587
haftmann@21348
  1588
  \begin{description}
haftmann@21348
  1589
haftmann@24217
  1590
  \item \verb|CodeUnit.read_const|~\isa{thy}~\isa{s}
haftmann@21348
  1591
     reads a constant as a concrete term expression \isa{s}.
haftmann@21348
  1592
haftmann@24217
  1593
  \item \verb|CodeUnit.head_func|~\isa{thm}
haftmann@22751
  1594
     extracts the constant and its type from a defining equation \isa{thm}.
haftmann@21348
  1595
haftmann@27557
  1596
  \item \verb|CodeUnit.rewrite_func|~\isa{ss}~\isa{thm}
haftmann@27557
  1597
     rewrites a defining equation \isa{thm} with a simpset \isa{ss};
haftmann@27557
  1598
     only arguments and right hand side are rewritten,
haftmann@22060
  1599
     not the head of the defining equation.
haftmann@21348
  1600
haftmann@21348
  1601
  \end{description}%
wenzelm@21172
  1602
\end{isamarkuptext}%
wenzelm@21172
  1603
\isamarkuptrue%
wenzelm@21172
  1604
%
wenzelm@21172
  1605
\endisatagmlref
wenzelm@21172
  1606
{\isafoldmlref}%
wenzelm@21172
  1607
%
wenzelm@21172
  1608
\isadelimmlref
wenzelm@21172
  1609
%
wenzelm@21172
  1610
\endisadelimmlref
wenzelm@21172
  1611
%
haftmann@20967
  1612
\isamarkupsubsection{Implementing code generator applications%
haftmann@20967
  1613
}
haftmann@20967
  1614
\isamarkuptrue%
haftmann@20967
  1615
%
wenzelm@21172
  1616
\begin{isamarkuptext}%
haftmann@21348
  1617
Implementing code generator applications on top
haftmann@21348
  1618
  of the framework set out so far usually not only
haftmann@21348
  1619
  involves using those primitive interfaces
haftmann@21348
  1620
  but also storing code-dependent data and various
haftmann@21348
  1621
  other things.
haftmann@21348
  1622
haftmann@21348
  1623
  \begin{warn}
wenzelm@21172
  1624
    Some interfaces discussed here have not reached
wenzelm@21172
  1625
    a final state yet.
wenzelm@21172
  1626
    Changes likely to occur in future.
haftmann@21452
  1627
  \end{warn}%
wenzelm@21172
  1628
\end{isamarkuptext}%
wenzelm@21172
  1629
\isamarkuptrue%
wenzelm@21172
  1630
%
wenzelm@21172
  1631
\isamarkupsubsubsection{Data depending on the theory's executable content%
wenzelm@21172
  1632
}
wenzelm@21172
  1633
\isamarkuptrue%
wenzelm@21172
  1634
%
haftmann@21348
  1635
\begin{isamarkuptext}%
haftmann@21452
  1636
Due to incrementality of code generation, changes in the
haftmann@21452
  1637
  theory's executable content have to be propagated in a
haftmann@21452
  1638
  certain fashion.  Additionally, such changes may occur
haftmann@21452
  1639
  not only during theory extension but also during theory
haftmann@21452
  1640
  merge, which is a little bit nasty from an implementation
haftmann@21452
  1641
  point of view.  The framework provides a solution
haftmann@21452
  1642
  to this technical challenge by providing a functorial
haftmann@21452
  1643
  data slot \verb|CodeDataFun|; on instantiation
haftmann@21452
  1644
  of this functor, the following types and operations
haftmann@21452
  1645
  are required:
haftmann@21452
  1646
haftmann@21452
  1647
  \medskip
haftmann@21348
  1648
  \begin{tabular}{l}
haftmann@21348
  1649
  \isa{type\ T} \\
haftmann@21348
  1650
  \isa{val\ empty{\isacharcolon}\ T} \\
haftmann@21348
  1651
  \isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\
haftmann@24217
  1652
  \isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodeUnit{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T}
haftmann@21348
  1653
  \end{tabular}
haftmann@21348
  1654
haftmann@21452
  1655
  \begin{description}
haftmann@21452
  1656
haftmann@21452
  1657
  \item \isa{T} the type of data to store.
haftmann@21452
  1658
haftmann@21452
  1659
  \item \isa{empty} initial (empty) data.
haftmann@21452
  1660
haftmann@21452
  1661
  \item \isa{merge} merging two data slots.
haftmann@21452
  1662
haftmann@22798
  1663
  \item \isa{purge}~\isa{thy}~\isa{consts} propagates changes in executable content;
haftmann@21452
  1664
    if possible, the current theory context is handed over
haftmann@21452
  1665
    as argument \isa{thy} (if there is no current theory context (e.g.~during
haftmann@22798
  1666
    theory merge, \verb|NONE|); \isa{consts} indicates the kind
haftmann@21452
  1667
    of change: \verb|NONE| stands for a fundamental change
haftmann@22798
  1668
    which invalidates any existing code, \isa{SOME\ consts}
haftmann@22798
  1669
    hints that executable content for constants \isa{consts}
haftmann@21452
  1670
    has changed.
haftmann@21452
  1671
haftmann@21452
  1672
  \end{description}
haftmann@21452
  1673
haftmann@21452
  1674
  An instance of \verb|CodeDataFun| provides the following
haftmann@21452
  1675
  interface:
haftmann@21452
  1676
haftmann@21348
  1677
  \medskip
haftmann@21348
  1678
  \begin{tabular}{l}
haftmann@21348
  1679
  \isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1680
  \isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1681
  \isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T}
haftmann@21452
  1682
  \end{tabular}
haftmann@21452
  1683
haftmann@21452
  1684
  \begin{description}
haftmann@21452
  1685
haftmann@21452
  1686
  \item \isa{get} retrieval of the current data.
haftmann@21452
  1687
haftmann@21452
  1688
  \item \isa{change} update of current data (cached!)
haftmann@21452
  1689
    by giving a continuation.
haftmann@21452
  1690
haftmann@21452
  1691
  \item \isa{change{\isacharunderscore}yield} update with side result.
haftmann@21452
  1692
haftmann@21452
  1693
  \end{description}%
haftmann@21452
  1694
\end{isamarkuptext}%
haftmann@21452
  1695
\isamarkuptrue%
haftmann@21452
  1696
%
haftmann@21452
  1697
\begin{isamarkuptext}%
haftmann@24628
  1698
\emph{Happy proving, happy hacking!}%
wenzelm@21172
  1699
\end{isamarkuptext}%
wenzelm@21172
  1700
\isamarkuptrue%
wenzelm@21172
  1701
%
haftmann@20967
  1702
\isadelimtheory
haftmann@20967
  1703
%
haftmann@20967
  1704
\endisadelimtheory
haftmann@20967
  1705
%
haftmann@20967
  1706
\isatagtheory
haftmann@20967
  1707
\isacommand{end}\isamarkupfalse%
haftmann@20967
  1708
%
haftmann@20967
  1709
\endisatagtheory
haftmann@20967
  1710
{\isafoldtheory}%
haftmann@20967
  1711
%
haftmann@20967
  1712
\isadelimtheory
haftmann@20967
  1713
%
haftmann@20967
  1714
\endisadelimtheory
haftmann@20967
  1715
\isanewline
haftmann@20967
  1716
\end{isabellebody}%
haftmann@20967
  1717
%%% Local Variables:
haftmann@20967
  1718
%%% mode: latex
haftmann@20967
  1719
%%% TeX-master: "root"
haftmann@20967
  1720
%%% End: