doc-src/Codegen/Thy/document/Codegen.tex
author haftmann
Thu, 14 May 2009 15:09:48 +0200
changeset 31156 90fed3d4430f
parent 30209 2f4684e2ea95
permissions -rw-r--r--
merged module code_unit.ML into code.ML
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@28143
   384
  \noindent The policy is that \emph{default equations} stemming from
haftmann@28143
   385
  \isa{{\isasymDEFINITION}},
haftmann@28143
   386
  \isa{{\isasymPRIMREC}}, \isa{{\isasymFUN}},
haftmann@28143
   387
  \isa{{\isasymFUNCTION}}, \isa{{\isasymCONSTDEFS}},
haftmann@28143
   388
  \isa{{\isasymRECDEF}} statements are discarded as soon as an
haftmann@28143
   389
  equation is explicitly selected by means of \emph{code func}.
haftmann@28143
   390
  Further applications of \emph{code func} add theorems incrementally,
haftmann@28143
   391
  but syntactic redundancies are implicitly dropped.  For example,
wenzelm@21172
   392
  using a modified version of the \isa{fac} function
haftmann@22060
   393
  as defining equation, the then redundant (since
haftmann@22060
   394
  syntactically subsumed) original defining equations
haftmann@28143
   395
  are dropped.
wenzelm@21172
   396
wenzelm@21172
   397
  \begin{warn}
haftmann@22292
   398
    The attributes \emph{code} and \emph{code del}
wenzelm@21172
   399
    associated with the existing code generator also apply to
wenzelm@21172
   400
    the new one: \emph{code} implies \emph{code func},
haftmann@22845
   401
    and \emph{code del} implies \emph{code func del}.
wenzelm@21172
   402
  \end{warn}%
wenzelm@21172
   403
\end{isamarkuptext}%
wenzelm@21172
   404
\isamarkuptrue%
wenzelm@21172
   405
%
haftmann@20967
   406
\isamarkupsubsection{Type classes%
haftmann@20967
   407
}
haftmann@20967
   408
\isamarkuptrue%
haftmann@20967
   409
%
wenzelm@21172
   410
\begin{isamarkuptext}%
wenzelm@21172
   411
Type classes enter the game via the Isar class package.
wenzelm@21172
   412
  For a short introduction how to use it, see \cite{isabelle-classes};
wenzelm@21172
   413
  here we just illustrate its impact on code generation.
wenzelm@21172
   414
wenzelm@21172
   415
  In a target language, type classes may be represented
haftmann@22798
   416
  natively (as in the case of Haskell).  For languages
wenzelm@21172
   417
  like SML, they are implemented using \emph{dictionaries}.
wenzelm@21186
   418
  Our following example specifies a class \qt{null},
wenzelm@21172
   419
  assigning to each of its inhabitants a \qt{null} value:%
wenzelm@21172
   420
\end{isamarkuptext}%
wenzelm@21172
   421
\isamarkuptrue%
wenzelm@21172
   422
\isacommand{class}\isamarkupfalse%
haftmann@22479
   423
\ null\ {\isacharequal}\ type\ {\isacharplus}\isanewline
wenzelm@21172
   424
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
wenzelm@21172
   425
\isanewline
haftmann@25870
   426
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
   427
\isanewline
haftmann@25870
   428
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
wenzelm@21172
   429
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline
haftmann@22798
   430
\ \ {\isacharbar}\ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}%
wenzelm@21172
   431
\begin{isamarkuptext}%
wenzelm@25731
   432
\noindent  We provide some instances for our \isa{null}:%
wenzelm@21172
   433
\end{isamarkuptext}%
wenzelm@21172
   434
\isamarkuptrue%
haftmann@25533
   435
\isacommand{instantiation}\isamarkupfalse%
haftmann@25533
   436
\ option\ \isakeyword{and}\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
haftmann@25533
   437
\isakeyword{begin}\isanewline
haftmann@25533
   438
\isanewline
haftmann@25533
   439
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   440
\isanewline
haftmann@25533
   441
\ \ {\isachardoublequoteopen}null\ {\isacharequal}\ None{\isachardoublequoteclose}\isanewline
haftmann@25533
   442
\isanewline
haftmann@25533
   443
\isacommand{definition}\isamarkupfalse%
haftmann@25533
   444
\isanewline
wenzelm@25731
   445
\ \ {\isachardoublequoteopen}null\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
haftmann@25533
   446
\isanewline
wenzelm@21172
   447
\isacommand{instance}\isamarkupfalse%
haftmann@25533
   448
%
wenzelm@21172
   449
\isadelimproof
wenzelm@21172
   450
\ %
wenzelm@21172
   451
\endisadelimproof
wenzelm@21172
   452
%
wenzelm@21172
   453
\isatagproof
wenzelm@21172
   454
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
wenzelm@21172
   455
%
wenzelm@21172
   456
\endisatagproof
wenzelm@21172
   457
{\isafoldproof}%
wenzelm@21172
   458
%
wenzelm@21172
   459
\isadelimproof
wenzelm@21172
   460
%
wenzelm@21172
   461
\endisadelimproof
wenzelm@21172
   462
\isanewline
wenzelm@21172
   463
\isanewline
haftmann@25533
   464
\isacommand{end}\isamarkupfalse%
wenzelm@21172
   465
%
wenzelm@21172
   466
\begin{isamarkuptext}%
wenzelm@25731
   467
\noindent Constructing a dummy example:%
wenzelm@21172
   468
\end{isamarkuptext}%
wenzelm@21172
   469
\isamarkuptrue%
wenzelm@21172
   470
\isacommand{definition}\isamarkupfalse%
wenzelm@21172
   471
\isanewline
wenzelm@21172
   472
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}%
wenzelm@21172
   473
\begin{isamarkuptext}%
wenzelm@21186
   474
Type classes offer a suitable occasion to introduce
wenzelm@21172
   475
  the Haskell serializer.  Its usage is almost the same
wenzelm@21172
   476
  as SML, but, in accordance with conventions
wenzelm@21172
   477
  some Haskell systems enforce, each module ends
wenzelm@21172
   478
  up in a single file. The module hierarchy is reflected in
haftmann@22845
   479
  the file system, with root directory given as file specification.%
wenzelm@21172
   480
\end{isamarkuptext}%
wenzelm@21172
   481
\isamarkuptrue%
haftmann@24379
   482
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   483
\ dummy\ \isakeyword{in}\ Haskell\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
wenzelm@21172
   484
\begin{isamarkuptext}%
wenzelm@21172
   485
\lsthaskell{Thy/examples/Codegen.hs}
haftmann@22798
   486
  \noindent (we have left out all other modules).
wenzelm@21172
   487
haftmann@22798
   488
  \medskip
wenzelm@21172
   489
wenzelm@21172
   490
  The whole code in SML with explicit dictionary passing:%
wenzelm@21172
   491
\end{isamarkuptext}%
wenzelm@21172
   492
\isamarkuptrue%
haftmann@24379
   493
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   494
\ dummy\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   495
\begin{isamarkuptext}%
haftmann@22798
   496
\lstsml{Thy/examples/class.ML}
haftmann@22798
   497
haftmann@22798
   498
  \medskip
haftmann@22798
   499
haftmann@22798
   500
  \noindent or in OCaml:%
haftmann@22292
   501
\end{isamarkuptext}%
haftmann@22292
   502
\isamarkuptrue%
haftmann@24379
   503
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   504
\ dummy\ \isakeyword{in}\ OCaml\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ocaml{\isachardoublequoteclose}%
haftmann@22292
   505
\begin{isamarkuptext}%
haftmann@22798
   506
\lstsml{Thy/examples/class.ocaml}
wenzelm@21172
   507
haftmann@22798
   508
  \medskip The explicit association of constants
haftmann@22845
   509
  to classes can be inspected using the \isa{{\isasymPRINTCLASSES}}
haftmann@22845
   510
  command.%
wenzelm@21172
   511
\end{isamarkuptext}%
wenzelm@21172
   512
\isamarkuptrue%
wenzelm@21172
   513
%
wenzelm@21172
   514
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}%
haftmann@20967
   515
}
haftmann@20967
   516
\isamarkuptrue%
haftmann@20967
   517
%
wenzelm@21172
   518
\begin{isamarkuptext}%
wenzelm@21172
   519
In this tutorial, we do not attempt to give an exhaustive
wenzelm@21172
   520
  description of the code generator framework; instead,
wenzelm@21172
   521
  we cast a light on advanced topics by introducing
wenzelm@21172
   522
  them together with practically motivated examples.  Concerning
wenzelm@21172
   523
  further reading, see
wenzelm@21172
   524
wenzelm@21172
   525
  \begin{itemize}
wenzelm@21172
   526
wenzelm@21172
   527
  \item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
wenzelm@21172
   528
    for exhaustive syntax diagrams.
haftmann@24193
   529
  \item or \cite{Haftmann-Nipkow:2007:codegen} which deals with foundational issues
wenzelm@21172
   530
    of the code generator framework.
wenzelm@21172
   531
wenzelm@21172
   532
  \end{itemize}%
wenzelm@21172
   533
\end{isamarkuptext}%
wenzelm@21172
   534
\isamarkuptrue%
wenzelm@21172
   535
%
haftmann@22798
   536
\isamarkupsubsection{Library theories \label{sec:library}%
haftmann@20967
   537
}
haftmann@20967
   538
\isamarkuptrue%
haftmann@20967
   539
%
wenzelm@21172
   540
\begin{isamarkuptext}%
haftmann@22916
   541
The \isa{HOL} \isa{Main} theory already provides a code
haftmann@22916
   542
  generator setup
wenzelm@21172
   543
  which should be suitable for most applications. Common extensions
haftmann@22916
   544
  and modifications are available by certain theories of the \isa{HOL}
wenzelm@21172
   545
  library; beside being useful in applications, they may serve
wenzelm@21186
   546
  as a tutorial for customizing the code generator setup.
wenzelm@21172
   547
wenzelm@21172
   548
  \begin{description}
wenzelm@21172
   549
wenzelm@25370
   550
    \item[\isa{Code{\isacharunderscore}Integer}] represents \isa{HOL} integers by big
haftmann@22798
   551
       integer literals in target languages.
wenzelm@25370
   552
    \item[\isa{Code{\isacharunderscore}Char}] represents \isa{HOL} characters by 
haftmann@22798
   553
       character literals in target languages.
wenzelm@25370
   554
    \item[\isa{Code{\isacharunderscore}Char{\isacharunderscore}chr}] like \isa{Code{\isacharunderscore}Char},
haftmann@22798
   555
       but also offers treatment of character codes; includes
wenzelm@25370
   556
       \isa{Code{\isacharunderscore}Integer}.
haftmann@23850
   557
    \item[\isa{Efficient{\isacharunderscore}Nat}] \label{eff_nat} implements natural numbers by integers,
wenzelm@21186
   558
       which in general will result in higher efficency; pattern
wenzelm@21172
   559
       matching with \isa{{\isadigit{0}}} / \isa{Suc}
wenzelm@25370
   560
       is eliminated;  includes \isa{Code{\isacharunderscore}Integer}.
wenzelm@25370
   561
    \item[\isa{Code{\isacharunderscore}Index}] provides an additional datatype
wenzelm@25370
   562
       \isa{index} which is mapped to target-language built-in integers.
wenzelm@25370
   563
       Useful for code setups which involve e.g. indexing of
wenzelm@25370
   564
       target-language arrays.
wenzelm@25370
   565
    \item[\isa{Code{\isacharunderscore}Message}] provides an additional datatype
wenzelm@25370
   566
       \isa{message{\isacharunderscore}string} which is isomorphic to strings;
wenzelm@25370
   567
       \isa{message{\isacharunderscore}string}s are mapped to target-language strings.
wenzelm@25370
   568
       Useful for code setups which involve e.g. printing (error) messages.
wenzelm@21172
   569
haftmann@22916
   570
  \end{description}
haftmann@22916
   571
haftmann@22916
   572
  \begin{warn}
haftmann@22916
   573
    When importing any of these theories, they should form the last
haftmann@22916
   574
    items in an import list.  Since these theories adapt the
haftmann@22916
   575
    code generator setup in a non-conservative fashion,
haftmann@22916
   576
    strange effects may occur otherwise.
haftmann@22916
   577
  \end{warn}%
wenzelm@21172
   578
\end{isamarkuptext}%
wenzelm@21172
   579
\isamarkuptrue%
wenzelm@21172
   580
%
wenzelm@21172
   581
\isamarkupsubsection{Preprocessing%
haftmann@20967
   582
}
haftmann@20967
   583
\isamarkuptrue%
haftmann@20967
   584
%
wenzelm@21172
   585
\begin{isamarkuptext}%
wenzelm@21172
   586
Before selected function theorems are turned into abstract
wenzelm@21172
   587
  code, a chain of definitional transformation steps is carried
haftmann@27609
   588
  out: \emph{preprocessing}.  In essence, the preprocessor
haftmann@27609
   589
  consists of two components: a \emph{simpset} and \emph{function transformers}.
wenzelm@21172
   590
haftmann@27557
   591
  The \emph{simpset} allows to employ the full generality of the Isabelle
haftmann@27557
   592
  simplifier.  Due to the interpretation of theorems
haftmann@28143
   593
  as defining equations, rewrites are applied to the right
wenzelm@21172
   594
  hand side and the arguments of the left hand side of an
wenzelm@21172
   595
  equation, but never to the constant heading the left hand side.
haftmann@27557
   596
  An important special case are \emph{inline theorems} which may be
haftmann@27557
   597
  declared an undeclared using the
haftmann@22845
   598
  \emph{code inline} or \emph{code inline del} attribute respectively.
wenzelm@21172
   599
  Some common applications:%
wenzelm@21172
   600
\end{isamarkuptext}%
wenzelm@21172
   601
\isamarkuptrue%
wenzelm@21172
   602
%
wenzelm@21172
   603
\begin{itemize}
haftmann@22845
   604
%
haftmann@22845
   605
\begin{isamarkuptext}%
haftmann@22845
   606
\item replacing non-executable constructs by executable ones:%
haftmann@22845
   607
\end{isamarkuptext}%
haftmann@22845
   608
\isamarkuptrue%
haftmann@22845
   609
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   610
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   611
\ \ \ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}%
wenzelm@21172
   612
\isadelimproof
wenzelm@21172
   613
\ %
wenzelm@21172
   614
\endisadelimproof
wenzelm@21172
   615
%
wenzelm@21172
   616
\isatagproof
wenzelm@21172
   617
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   618
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   619
\endisatagproof
wenzelm@21172
   620
{\isafoldproof}%
wenzelm@21172
   621
%
wenzelm@21172
   622
\isadelimproof
wenzelm@21172
   623
%
wenzelm@21172
   624
\endisadelimproof
wenzelm@21172
   625
%
haftmann@22845
   626
\begin{isamarkuptext}%
haftmann@22845
   627
\item eliminating superfluous constants:%
haftmann@22845
   628
\end{isamarkuptext}%
haftmann@22845
   629
\isamarkuptrue%
haftmann@22845
   630
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   631
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   632
\ \ \ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}%
wenzelm@21172
   633
\isadelimproof
wenzelm@21172
   634
\ %
wenzelm@21172
   635
\endisadelimproof
wenzelm@21172
   636
%
wenzelm@21172
   637
\isatagproof
wenzelm@21172
   638
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   639
\ simp%
wenzelm@21172
   640
\endisatagproof
wenzelm@21172
   641
{\isafoldproof}%
wenzelm@21172
   642
%
wenzelm@21172
   643
\isadelimproof
wenzelm@21172
   644
%
wenzelm@21172
   645
\endisadelimproof
wenzelm@21172
   646
%
haftmann@22845
   647
\begin{isamarkuptext}%
haftmann@22845
   648
\item replacing executable but inconvenient constructs:%
haftmann@22845
   649
\end{isamarkuptext}%
haftmann@22845
   650
\isamarkuptrue%
haftmann@22845
   651
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   652
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   653
\ \ \ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}%
wenzelm@21172
   654
\isadelimproof
wenzelm@21172
   655
\ %
wenzelm@21172
   656
\endisadelimproof
wenzelm@21172
   657
%
wenzelm@21172
   658
\isatagproof
wenzelm@21172
   659
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   660
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   661
\endisatagproof
wenzelm@21172
   662
{\isafoldproof}%
wenzelm@21172
   663
%
wenzelm@21172
   664
\isadelimproof
wenzelm@21172
   665
%
wenzelm@21172
   666
\endisadelimproof
wenzelm@21172
   667
%
wenzelm@21172
   668
\end{itemize}
wenzelm@21172
   669
%
wenzelm@21172
   670
\begin{isamarkuptext}%
haftmann@27609
   671
\emph{Function transformers} provide a very general interface,
wenzelm@21172
   672
  transforming a list of function theorems to another
wenzelm@21172
   673
  list of function theorems, provided that neither the heading
wenzelm@21172
   674
  constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
haftmann@21348
   675
  pattern elimination implemented in
haftmann@28143
   676
  theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this
wenzelm@21172
   677
  interface.
wenzelm@21172
   678
haftmann@27557
   679
  \noindent The current setup of the preprocessor may be inspected using
haftmann@27557
   680
  the \isa{{\isasymPRINTCODESETUP}} command.
haftmann@27557
   681
wenzelm@21172
   682
  \begin{warn}
haftmann@27609
   683
    The attribute \emph{code unfold}
wenzelm@21172
   684
    associated with the existing code generator also applies to
wenzelm@21172
   685
    the new one: \emph{code unfold} implies \emph{code inline}.
wenzelm@21172
   686
  \end{warn}%
wenzelm@21172
   687
\end{isamarkuptext}%
wenzelm@21172
   688
\isamarkuptrue%
wenzelm@21172
   689
%
haftmann@22798
   690
\isamarkupsubsection{Concerning operational equality%
haftmann@22798
   691
}
haftmann@22798
   692
\isamarkuptrue%
haftmann@22798
   693
%
haftmann@22798
   694
\begin{isamarkuptext}%
haftmann@22798
   695
Surely you have already noticed how equality is treated
haftmann@22798
   696
  by the code generator:%
haftmann@22798
   697
\end{isamarkuptext}%
haftmann@22798
   698
\isamarkuptrue%
haftmann@25870
   699
\isacommand{primrec}\isamarkupfalse%
haftmann@22798
   700
\isanewline
haftmann@22798
   701
\ \ 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
   702
\ \ \ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
haftmann@22798
   703
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
haftmann@22798
   704
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
haftmann@22798
   705
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
haftmann@22798
   706
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
haftmann@22798
   707
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22798
   708
\begin{isamarkuptext}%
haftmann@22798
   709
The membership test during preprocessing is rewritten,
haftmann@22798
   710
  resulting in \isa{op\ mem}, which itself
haftmann@22798
   711
  performs an explicit equality check.%
haftmann@22798
   712
\end{isamarkuptext}%
haftmann@22798
   713
\isamarkuptrue%
haftmann@24379
   714
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
   715
\ collect{\isacharunderscore}duplicates\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   716
\begin{isamarkuptext}%
haftmann@22798
   717
\lstsml{Thy/examples/collect_duplicates.ML}%
haftmann@22798
   718
\end{isamarkuptext}%
haftmann@22798
   719
\isamarkuptrue%
haftmann@22798
   720
%
haftmann@22798
   721
\begin{isamarkuptext}%
haftmann@22798
   722
Obviously, polymorphic equality is implemented the Haskell
haftmann@26513
   723
  way using a type class.  How is this achieved?  HOL introduces
haftmann@26513
   724
  an explicit class \isa{eq} with a corresponding operation
haftmann@26513
   725
  \isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ x\ y\ {\isacharequal}\ {\isacharparenleft}x\ {\isacharequal}\ y{\isacharparenright}}.
haftmann@26513
   726
  The preprocessing framework does the rest.
haftmann@22798
   727
  For datatypes, instances of \isa{eq} are implicitly derived
haftmann@26513
   728
  when possible.  For other types, you may instantiate \isa{eq}
haftmann@26513
   729
  manually like any other type class.
haftmann@22798
   730
haftmann@22798
   731
  Though this \isa{eq} class is designed to get rarely in
haftmann@22798
   732
  the way, a subtlety
haftmann@22798
   733
  enters the stage when definitions of overloaded constants
haftmann@22798
   734
  are dependent on operational equality.  For example, let
haftmann@22798
   735
  us define a lexicographic ordering on tuples:%
haftmann@22798
   736
\end{isamarkuptext}%
haftmann@22798
   737
\isamarkuptrue%
haftmann@25870
   738
\isacommand{instantiation}\isamarkupfalse%
haftmann@25870
   739
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline
haftmann@25870
   740
\isakeyword{begin}\isanewline
haftmann@25870
   741
\isanewline
haftmann@25870
   742
\isacommand{definition}\isamarkupfalse%
haftmann@25870
   743
\isanewline
haftmann@25870
   744
\ \ {\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
   745
\ \ \ \ 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
   746
\isanewline
haftmann@25870
   747
\isacommand{definition}\isamarkupfalse%
haftmann@25870
   748
\isanewline
haftmann@25870
   749
\ \ {\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
   750
\ \ \ \ 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
   751
\isanewline
haftmann@22798
   752
\isacommand{instance}\isamarkupfalse%
haftmann@25870
   753
%
haftmann@22798
   754
\isadelimproof
haftmann@22798
   755
\ %
haftmann@22798
   756
\endisadelimproof
haftmann@22798
   757
%
haftmann@22798
   758
\isatagproof
haftmann@22798
   759
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
haftmann@22798
   760
%
haftmann@22798
   761
\endisatagproof
haftmann@22798
   762
{\isafoldproof}%
haftmann@22798
   763
%
haftmann@22798
   764
\isadelimproof
haftmann@22798
   765
%
haftmann@22798
   766
\endisadelimproof
haftmann@22798
   767
\isanewline
haftmann@22798
   768
\isanewline
haftmann@25870
   769
\isacommand{end}\isamarkupfalse%
haftmann@25870
   770
\isanewline
haftmann@22798
   771
\isanewline
haftmann@22798
   772
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   773
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   774
\ \ {\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
   775
\ \ {\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
   776
%
haftmann@22798
   777
\isadelimproof
haftmann@22798
   778
\ \ %
haftmann@22798
   779
\endisadelimproof
haftmann@22798
   780
%
haftmann@22798
   781
\isatagproof
haftmann@22798
   782
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   783
\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   784
\ simp{\isacharunderscore}all%
haftmann@22798
   785
\endisatagproof
haftmann@22798
   786
{\isafoldproof}%
haftmann@22798
   787
%
haftmann@22798
   788
\isadelimproof
haftmann@22798
   789
%
haftmann@22798
   790
\endisadelimproof
haftmann@22798
   791
%
haftmann@22798
   792
\begin{isamarkuptext}%
haftmann@22798
   793
Then code generation will fail.  Why?  The definition
haftmann@22798
   794
  of \isa{op\ {\isasymle}} depends on equality on both arguments,
haftmann@22798
   795
  which are polymorphic and impose an additional \isa{eq}
haftmann@22798
   796
  class constraint, thus violating the type discipline
haftmann@22798
   797
  for class operations.
haftmann@22798
   798
haftmann@22798
   799
  The solution is to add \isa{eq} explicitly to the first sort arguments in the
haftmann@22798
   800
  code theorems:%
haftmann@22798
   801
\end{isamarkuptext}%
haftmann@22798
   802
\isamarkuptrue%
haftmann@22798
   803
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   804
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   805
\ \ {\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
   806
\ \ \ \ 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
   807
\ \ {\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
   808
\ \ \ \ 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
   809
%
haftmann@22798
   810
\isadelimproof
haftmann@22798
   811
\ \ %
haftmann@22798
   812
\endisadelimproof
haftmann@22798
   813
%
haftmann@22798
   814
\isatagproof
haftmann@22798
   815
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   816
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   817
\ rule{\isacharplus}%
haftmann@22798
   818
\endisatagproof
haftmann@22798
   819
{\isafoldproof}%
haftmann@22798
   820
%
haftmann@22798
   821
\isadelimproof
haftmann@22798
   822
%
haftmann@22798
   823
\endisadelimproof
haftmann@22798
   824
%
haftmann@22798
   825
\begin{isamarkuptext}%
haftmann@22798
   826
\noindent Then code generation succeeds:%
haftmann@22798
   827
\end{isamarkuptext}%
haftmann@22798
   828
\isamarkuptrue%
haftmann@24379
   829
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22798
   830
\ {\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
   831
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   832
\begin{isamarkuptext}%
haftmann@22798
   833
\lstsml{Thy/examples/lexicographic.ML}%
haftmann@22798
   834
\end{isamarkuptext}%
haftmann@22798
   835
\isamarkuptrue%
haftmann@22798
   836
%
haftmann@22798
   837
\begin{isamarkuptext}%
haftmann@22798
   838
In general, code theorems for overloaded constants may have more
haftmann@22798
   839
  restrictive sort constraints than the underlying instance relation
haftmann@22798
   840
  between class and type constructor as long as the whole system of
haftmann@22798
   841
  constraints is coregular; code theorems violating coregularity
haftmann@22798
   842
  are rejected immediately.  Consequently, it might be necessary
haftmann@22798
   843
  to delete disturbing theorems in the code theorem table,
haftmann@22798
   844
  as we have done here with the original definitions \isa{less{\isacharunderscore}prod{\isacharunderscore}def}
haftmann@22885
   845
  and \isa{less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def}.
haftmann@22885
   846
haftmann@22885
   847
  In some cases, the automatically derived defining equations
haftmann@22885
   848
  for equality on a particular type may not be appropriate.
haftmann@22885
   849
  As example, watch the following datatype representing
haftmann@23132
   850
  monomorphic parametric types (where type constructors
haftmann@23132
   851
  are referred to by natural numbers):%
haftmann@22885
   852
\end{isamarkuptext}%
haftmann@22885
   853
\isamarkuptrue%
haftmann@22885
   854
\isacommand{datatype}\isamarkupfalse%
haftmann@23132
   855
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}%
haftmann@22885
   856
\isadelimproof
haftmann@22885
   857
%
haftmann@22885
   858
\endisadelimproof
haftmann@22885
   859
%
haftmann@22885
   860
\isatagproof
haftmann@22885
   861
%
haftmann@22885
   862
\endisatagproof
haftmann@22885
   863
{\isafoldproof}%
haftmann@22885
   864
%
haftmann@22885
   865
\isadelimproof
haftmann@22885
   866
%
haftmann@22885
   867
\endisadelimproof
haftmann@22885
   868
%
haftmann@22885
   869
\begin{isamarkuptext}%
haftmann@22885
   870
Then code generation for SML would fail with a message
haftmann@22885
   871
  that the generated code conains illegal mutual dependencies:
haftmann@23132
   872
  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
   873
  instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires
haftmann@23132
   874
  \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
   875
  recursive \isa{instance} and \isa{function} definitions,
haftmann@22885
   876
  but the SML serializer does not support this.
haftmann@22885
   877
haftmann@22885
   878
  In such cases, you have to provide you own equality equations
haftmann@22885
   879
  involving auxiliary constants.  In our case,
haftmann@22885
   880
  \isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:%
haftmann@22885
   881
\end{isamarkuptext}%
haftmann@22885
   882
\isamarkuptrue%
haftmann@22885
   883
\isacommand{lemma}\isamarkupfalse%
haftmann@22885
   884
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22885
   885
\ \ {\isachardoublequoteopen}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymlongleftrightarrow}\isanewline
haftmann@22885
   886
\ \ \ \ \ 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
   887
%
haftmann@22885
   888
\isadelimproof
haftmann@22885
   889
\ \ %
haftmann@22885
   890
\endisadelimproof
haftmann@22885
   891
%
haftmann@22885
   892
\isatagproof
haftmann@22885
   893
\isacommand{by}\isamarkupfalse%
haftmann@22885
   894
\ {\isacharparenleft}simp\ add{\isacharcolon}\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}%
haftmann@22885
   895
\endisatagproof
haftmann@22885
   896
{\isafoldproof}%
haftmann@22885
   897
%
haftmann@22885
   898
\isadelimproof
haftmann@22885
   899
%
haftmann@22885
   900
\endisadelimproof
haftmann@22885
   901
%
haftmann@22885
   902
\begin{isamarkuptext}%
haftmann@22885
   903
does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:%
haftmann@22885
   904
\end{isamarkuptext}%
haftmann@22885
   905
\isamarkuptrue%
haftmann@24379
   906
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22885
   907
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isacharcolon}{\isacharcolon}\ monotype\ {\isasymRightarrow}\ monotype\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
haftmann@22885
   908
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}monotype{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22885
   909
\begin{isamarkuptext}%
haftmann@22885
   910
\lstsml{Thy/examples/monotype.ML}%
haftmann@22798
   911
\end{isamarkuptext}%
haftmann@22798
   912
\isamarkuptrue%
haftmann@22798
   913
%
haftmann@22798
   914
\isamarkupsubsection{Programs as sets of theorems%
haftmann@22798
   915
}
haftmann@22798
   916
\isamarkuptrue%
haftmann@22798
   917
%
haftmann@22798
   918
\begin{isamarkuptext}%
haftmann@22798
   919
As told in \secref{sec:concept}, code generation is based
haftmann@22798
   920
  on a structured collection of code theorems.
haftmann@22798
   921
  For explorative purpose, this collection
haftmann@22798
   922
  may be inspected using the \isa{{\isasymCODETHMS}} command:%
haftmann@22798
   923
\end{isamarkuptext}%
haftmann@22798
   924
\isamarkuptrue%
haftmann@22798
   925
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
haftmann@22798
   926
\ {\isachardoublequoteopen}op\ mod\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}%
haftmann@22798
   927
\begin{isamarkuptext}%
haftmann@22798
   928
\noindent prints a table with \emph{all} defining equations
haftmann@22798
   929
  for \isa{op\ mod}, including
haftmann@22798
   930
  \emph{all} defining equations those equations depend
haftmann@22798
   931
  on recursivly.  \isa{{\isasymCODETHMS}} provides a convenient
haftmann@22798
   932
  mechanism to inspect the impact of a preprocessor setup
haftmann@22798
   933
  on defining equations.
haftmann@22798
   934
  
haftmann@22798
   935
  Similarly, the \isa{{\isasymCODEDEPS}} command shows a graph
haftmann@22798
   936
  visualizing dependencies between defining equations.%
haftmann@22798
   937
\end{isamarkuptext}%
haftmann@22798
   938
\isamarkuptrue%
haftmann@22798
   939
%
haftmann@25411
   940
\isamarkupsubsection{Constructor sets for datatypes%
haftmann@25411
   941
}
haftmann@25411
   942
\isamarkuptrue%
haftmann@25411
   943
%
haftmann@25411
   944
\begin{isamarkuptext}%
haftmann@25411
   945
Conceptually, any datatype is spanned by a set of
haftmann@25411
   946
  \emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n}
haftmann@25411
   947
  where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all}
haftmann@25411
   948
  type variables in \isa{{\isasymtau}}.  The HOL datatype package
haftmann@25411
   949
  by default registers any new datatype in the table
haftmann@25411
   950
  of datatypes, which may be inspected using
haftmann@25411
   951
  the \isa{{\isasymPRINTCODESETUP}} command.
haftmann@25411
   952
haftmann@25411
   953
  In some cases, it may be convenient to alter or
haftmann@26999
   954
  extend this table;  as an example, we will develope an alternative
haftmann@26999
   955
  representation of natural numbers as binary digits, whose
haftmann@26999
   956
  size does increase logarithmically with its value, not linear
haftmann@28143
   957
  \footnote{Indeed, the \isa{Efficient{\isacharunderscore}Nat} theory (see \ref{eff_nat})
haftmann@26999
   958
    does something similar}.  First, the digit representation:%
haftmann@26999
   959
\end{isamarkuptext}%
haftmann@26999
   960
\isamarkuptrue%
haftmann@26999
   961
\isacommand{definition}\isamarkupfalse%
haftmann@26999
   962
\ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@26999
   963
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ n\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
   964
\isanewline
haftmann@26999
   965
\isacommand{definition}\isamarkupfalse%
haftmann@26999
   966
\ Dig{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@26999
   967
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}{\isachardoublequoteclose}%
haftmann@26999
   968
\begin{isamarkuptext}%
haftmann@26999
   969
\noindent We will use these two ">digits"< to represent natural numbers
haftmann@26999
   970
  in binary digits, e.g.:%
haftmann@26999
   971
\end{isamarkuptext}%
haftmann@26999
   972
\isamarkuptrue%
haftmann@26999
   973
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
   974
\ {\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
   975
%
haftmann@26999
   976
\isadelimproof
haftmann@26999
   977
\ \ %
haftmann@26999
   978
\endisadelimproof
haftmann@26999
   979
%
haftmann@26999
   980
\isatagproof
haftmann@26999
   981
\isacommand{by}\isamarkupfalse%
haftmann@26999
   982
\ {\isacharparenleft}simp\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@26999
   983
\endisatagproof
haftmann@26999
   984
{\isafoldproof}%
haftmann@26999
   985
%
haftmann@26999
   986
\isadelimproof
haftmann@26999
   987
%
haftmann@26999
   988
\endisadelimproof
haftmann@26999
   989
%
haftmann@26999
   990
\begin{isamarkuptext}%
haftmann@26999
   991
\noindent Of course we also have to provide proper code equations for
haftmann@26999
   992
  the operations, e.g. \isa{op\ {\isacharplus}}:%
haftmann@26999
   993
\end{isamarkuptext}%
haftmann@26999
   994
\isamarkuptrue%
haftmann@26999
   995
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
   996
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@26999
   997
\ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
   998
\ \ {\isachardoublequoteopen}m\ {\isacharplus}\ {\isadigit{0}}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
haftmann@26999
   999
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ n{\isachardoublequoteclose}\isanewline
haftmann@26999
  1000
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{1}}\ m{\isachardoublequoteclose}\isanewline
haftmann@26999
  1001
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1002
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1003
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1004
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1005
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1006
\ \ {\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
  1007
%
haftmann@26999
  1008
\isadelimproof
haftmann@26999
  1009
\ \ %
haftmann@26999
  1010
\endisadelimproof
haftmann@26999
  1011
%
haftmann@26999
  1012
\isatagproof
haftmann@26999
  1013
\isacommand{by}\isamarkupfalse%
haftmann@26999
  1014
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@26999
  1015
\endisatagproof
haftmann@26999
  1016
{\isafoldproof}%
haftmann@26999
  1017
%
haftmann@26999
  1018
\isadelimproof
haftmann@26999
  1019
%
haftmann@26999
  1020
\endisadelimproof
haftmann@26999
  1021
%
haftmann@26999
  1022
\begin{isamarkuptext}%
haftmann@26999
  1023
\noindent We then instruct the code generator to view \isa{{\isadigit{0}}},
haftmann@26999
  1024
  \isa{{\isadigit{1}}}, \isa{Dig{\isadigit{0}}} and \isa{Dig{\isadigit{1}}} as
haftmann@26999
  1025
  datatype constructors:%
haftmann@26999
  1026
\end{isamarkuptext}%
haftmann@26999
  1027
\isamarkuptrue%
haftmann@26999
  1028
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@26999
  1029
\ {\isachardoublequoteopen}{\isadigit{0}}{\isasymColon}nat{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isadigit{1}}{\isasymColon}nat{\isachardoublequoteclose}\ Dig{\isadigit{0}}\ Dig{\isadigit{1}}%
haftmann@26999
  1030
\begin{isamarkuptext}%
haftmann@26999
  1031
\noindent For the former constructor \isa{Suc}, we provide a code
haftmann@26999
  1032
  equation and remove some parts of the default code generator setup
haftmann@26999
  1033
  which are an obstacle here:%
haftmann@26999
  1034
\end{isamarkuptext}%
haftmann@26999
  1035
\isamarkuptrue%
haftmann@26999
  1036
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1037
\ Suc{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@26999
  1038
\ \ {\isachardoublequoteopen}Suc\ n\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
haftmann@26999
  1039
%
haftmann@26999
  1040
\isadelimproof
haftmann@26999
  1041
\ \ %
haftmann@26999
  1042
\endisadelimproof
haftmann@26999
  1043
%
haftmann@26999
  1044
\isatagproof
haftmann@26999
  1045
\isacommand{by}\isamarkupfalse%
haftmann@26999
  1046
\ simp%
haftmann@26999
  1047
\endisatagproof
haftmann@26999
  1048
{\isafoldproof}%
haftmann@26999
  1049
%
haftmann@26999
  1050
\isadelimproof
haftmann@26999
  1051
\isanewline
haftmann@26999
  1052
%
haftmann@26999
  1053
\endisadelimproof
haftmann@26999
  1054
\isanewline
haftmann@26999
  1055
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1056
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline\ del{\isacharbrackright}\isanewline
haftmann@26999
  1057
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1058
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}%
haftmann@26999
  1059
\begin{isamarkuptext}%
haftmann@26999
  1060
\noindent This yields the following code:%
haftmann@26999
  1061
\end{isamarkuptext}%
haftmann@26999
  1062
\isamarkuptrue%
haftmann@26999
  1063
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@26999
  1064
\ {\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
  1065
\begin{isamarkuptext}%
haftmann@26999
  1066
\lstsml{Thy/examples/nat_binary.ML}%
haftmann@25411
  1067
\end{isamarkuptext}%
haftmann@25411
  1068
\isamarkuptrue%
haftmann@25411
  1069
%
haftmann@25411
  1070
\begin{isamarkuptext}%
haftmann@26973
  1071
\medskip
haftmann@25411
  1072
haftmann@26999
  1073
  From this example, it can be easily glimpsed that using own constructor sets
haftmann@26999
  1074
  is a little delicate since it changes the set of valid patterns for values
haftmann@26999
  1075
  of that type.  Without going into much detail, here some practical hints:
haftmann@26999
  1076
haftmann@26999
  1077
  \begin{itemize}
haftmann@26999
  1078
    \item When changing the constuctor set for datatypes, take care to
haftmann@26999
  1079
      provide an alternative for the \isa{case} combinator (e.g. by replacing
haftmann@26999
  1080
      it using the preprocessor).
haftmann@26999
  1081
    \item Values in the target language need not to be normalized -- different
haftmann@26999
  1082
      values in the target language may represent the same value in the
haftmann@26999
  1083
      logic (e.g. \isa{Dig{\isadigit{1}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}}).
haftmann@26999
  1084
    \item Usually, a good methodology to deal with the subleties of pattern
haftmann@26999
  1085
      matching is to see the type as an abstract type: provide a set
haftmann@26999
  1086
      of operations which operate on the concrete representation of the type,
haftmann@26999
  1087
      and derive further operations by combinations of these primitive ones,
haftmann@26999
  1088
      without relying on a particular representation.
haftmann@26999
  1089
  \end{itemize}%
haftmann@25411
  1090
\end{isamarkuptext}%
haftmann@25411
  1091
\isamarkuptrue%
haftmann@25411
  1092
%
haftmann@26999
  1093
\isadeliminvisible
haftmann@26999
  1094
%
haftmann@26999
  1095
\endisadeliminvisible
haftmann@26999
  1096
%
haftmann@26999
  1097
\isataginvisible
haftmann@26999
  1098
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@26999
  1099
\ {\isachardoublequoteopen}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isachardoublequoteclose}\ Suc\isanewline
haftmann@26999
  1100
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1101
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\isanewline
haftmann@26999
  1102
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1103
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline{\isacharbrackright}\isanewline
haftmann@26999
  1104
\isacommand{declare}\isamarkupfalse%
haftmann@26999
  1105
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func{\isacharbrackright}\ \isanewline
haftmann@26999
  1106
\isacommand{lemma}\isamarkupfalse%
haftmann@26999
  1107
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ {\isacharparenleft}n\ {\isasymColon}\ nat{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@26999
  1108
\ simp%
haftmann@26999
  1109
\endisataginvisible
haftmann@26999
  1110
{\isafoldinvisible}%
haftmann@26999
  1111
%
haftmann@26999
  1112
\isadeliminvisible
haftmann@26999
  1113
%
haftmann@26999
  1114
\endisadeliminvisible
haftmann@26999
  1115
%
wenzelm@21172
  1116
\isamarkupsubsection{Customizing serialization%
haftmann@20967
  1117
}
haftmann@20967
  1118
\isamarkuptrue%
haftmann@20967
  1119
%
haftmann@22798
  1120
\isamarkupsubsubsection{Basics%
haftmann@22798
  1121
}
haftmann@22798
  1122
\isamarkuptrue%
haftmann@22798
  1123
%
wenzelm@21172
  1124
\begin{isamarkuptext}%
wenzelm@21172
  1125
Consider the following function and its corresponding
wenzelm@21172
  1126
  SML code:%
wenzelm@21172
  1127
\end{isamarkuptext}%
wenzelm@21172
  1128
\isamarkuptrue%
haftmann@25870
  1129
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
  1130
\isanewline
wenzelm@21172
  1131
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
  1132
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}%
haftmann@21348
  1133
\isadelimtt
wenzelm@21172
  1134
%
haftmann@21348
  1135
\endisadelimtt
wenzelm@21172
  1136
%
haftmann@21348
  1137
\isatagtt
wenzelm@21172
  1138
%
haftmann@21348
  1139
\endisatagtt
haftmann@21348
  1140
{\isafoldtt}%
wenzelm@21172
  1141
%
haftmann@21348
  1142
\isadelimtt
haftmann@21348
  1143
%
haftmann@21348
  1144
\endisadelimtt
haftmann@24379
  1145
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1146
\ in{\isacharunderscore}interval\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1147
\begin{isamarkuptext}%
haftmann@21348
  1148
\lstsml{Thy/examples/bool_literal.ML}
wenzelm@21172
  1149
haftmann@22798
  1150
  \noindent Though this is correct code, it is a little bit unsatisfactory:
wenzelm@21172
  1151
  boolean values and operators are materialized as distinguished
wenzelm@21172
  1152
  entities with have nothing to do with the SML-builtin notion
wenzelm@21172
  1153
  of \qt{bool}.  This results in less readable code;
wenzelm@21172
  1154
  additionally, eager evaluation may cause programs to
wenzelm@21172
  1155
  loop or break which would perfectly terminate when
wenzelm@21172
  1156
  the existing SML \qt{bool} would be used.  To map
wenzelm@21172
  1157
  the HOL \qt{bool} on SML \qt{bool}, we may use
wenzelm@21172
  1158
  \qn{custom serializations}:%
wenzelm@21172
  1159
\end{isamarkuptext}%
wenzelm@21172
  1160
\isamarkuptrue%
haftmann@21348
  1161
%
haftmann@21348
  1162
\isadelimtt
haftmann@21348
  1163
%
haftmann@21348
  1164
\endisadelimtt
haftmann@21348
  1165
%
haftmann@21348
  1166
\isatagtt
wenzelm@21172
  1167
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1168
\ bool\isanewline
wenzelm@21172
  1169
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1170
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1171
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
wenzelm@21172
  1172
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1173
\endisatagtt
haftmann@21348
  1174
{\isafoldtt}%
haftmann@21348
  1175
%
haftmann@21348
  1176
\isadelimtt
haftmann@21348
  1177
%
haftmann@21348
  1178
\endisadelimtt
haftmann@21348
  1179
%
wenzelm@21172
  1180
\begin{isamarkuptext}%
haftmann@21348
  1181
The \isa{{\isasymCODETYPE}} commad takes a type constructor
wenzelm@21172
  1182
  as arguments together with a list of custom serializations.
wenzelm@21172
  1183
  Each custom serialization starts with a target language
wenzelm@21172
  1184
  identifier followed by an expression, which during
wenzelm@21172
  1185
  code serialization is inserted whenever the type constructor
haftmann@21348
  1186
  would occur.  For constants, \isa{{\isasymCODECONST}} implements
haftmann@21348
  1187
  the corresponding mechanism.  Each ``\verb|_|'' in
wenzelm@21172
  1188
  a serialization expression is treated as a placeholder
wenzelm@21172
  1189
  for the type constructor's (the constant's) arguments.%
wenzelm@21172
  1190
\end{isamarkuptext}%
wenzelm@21172
  1191
\isamarkuptrue%
haftmann@24379
  1192
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1193
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1194
\begin{isamarkuptext}%
haftmann@21348
  1195
\lstsml{Thy/examples/bool_mlbool.ML}
wenzelm@21172
  1196
haftmann@22798
  1197
  \noindent This still is not perfect: the parentheses
haftmann@21348
  1198
  around the \qt{andalso} expression are superfluous.
haftmann@21348
  1199
  Though the serializer
wenzelm@21172
  1200
  by no means attempts to imitate the rich Isabelle syntax
wenzelm@21172
  1201
  framework, it provides some common idioms, notably
wenzelm@21172
  1202
  associative infixes with precedences which may be used here:%
wenzelm@21172
  1203
\end{isamarkuptext}%
wenzelm@21172
  1204
\isamarkuptrue%
haftmann@21348
  1205
%
haftmann@21348
  1206
\isadelimtt
haftmann@21348
  1207
%
haftmann@21348
  1208
\endisadelimtt
haftmann@21348
  1209
%
haftmann@21348
  1210
\isatagtt
wenzelm@21172
  1211
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1212
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
haftmann@21348
  1213
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1214
\endisatagtt
haftmann@21348
  1215
{\isafoldtt}%
haftmann@21348
  1216
%
haftmann@21348
  1217
\isadelimtt
haftmann@21348
  1218
%
haftmann@21348
  1219
\endisadelimtt
haftmann@21348
  1220
\isanewline
wenzelm@21172
  1221
\isanewline
haftmann@24379
  1222
\isacommand{export{\isacharunderscore}code}\isamarkupfalse%
haftmann@22845
  1223
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1224
\begin{isamarkuptext}%
haftmann@21348
  1225
\lstsml{Thy/examples/bool_infix.ML}
wenzelm@21172
  1226
haftmann@22798
  1227
  \medskip
haftmann@22798
  1228
wenzelm@21172
  1229
  Next, we try to map HOL pairs to SML pairs, using the
haftmann@21348
  1230
  infix ``\verb|*|'' type constructor and parentheses:%
wenzelm@21172
  1231
\end{isamarkuptext}%
wenzelm@21172
  1232
\isamarkuptrue%
haftmann@21348
  1233
%
haftmann@21348
  1234
\isadelimtt
haftmann@21348
  1235
%
haftmann@21348
  1236
\endisadelimtt
haftmann@21348
  1237
%
haftmann@21348
  1238
\isatagtt
wenzelm@21172
  1239
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1240
\ {\isacharasterisk}\isanewline
wenzelm@21172
  1241
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1242
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1243
\ Pair\isanewline
wenzelm@21172
  1244
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1245
\endisatagtt
haftmann@21348
  1246
{\isafoldtt}%
haftmann@21348
  1247
%
haftmann@21348
  1248
\isadelimtt
haftmann@21348
  1249
%
haftmann@21348
  1250
\endisadelimtt
haftmann@21348
  1251
%
wenzelm@21172
  1252
\begin{isamarkuptext}%
haftmann@21348
  1253
The initial bang ``\verb|!|'' tells the serializer to never put
wenzelm@21172
  1254
  parentheses around the whole expression (they are already present),
wenzelm@21172
  1255
  while the parentheses around argument place holders
wenzelm@21172
  1256
  tell not to put parentheses around the arguments.
haftmann@21348
  1257
  The slash ``\verb|/|'' (followed by arbitrary white space)
wenzelm@21172
  1258
  inserts a space which may be used as a break if necessary
wenzelm@21172
  1259
  during pretty printing.
wenzelm@21172
  1260
haftmann@22798
  1261
  These examples give a glimpse what mechanisms
wenzelm@21186
  1262
  custom serializations provide; however their usage
wenzelm@21186
  1263
  requires careful thinking in order not to introduce
wenzelm@21186
  1264
  inconsistencies -- or, in other words:
wenzelm@21186
  1265
  custom serializations are completely axiomatic.
wenzelm@21186
  1266
wenzelm@21186
  1267
  A further noteworthy details is that any special
wenzelm@21186
  1268
  character in a custom serialization may be quoted
haftmann@21348
  1269
  using ``\verb|'|''; thus, in
haftmann@21348
  1270
  ``\verb|fn '_ => _|'' the first
haftmann@21348
  1271
  ``\verb|_|'' is a proper underscore while the
haftmann@21348
  1272
  second ``\verb|_|'' is a placeholder.
wenzelm@21186
  1273
wenzelm@21186
  1274
  The HOL theories provide further
haftmann@25411
  1275
  examples for custom serializations.%
wenzelm@21186
  1276
\end{isamarkuptext}%
wenzelm@21172
  1277
\isamarkuptrue%
wenzelm@21172
  1278
%
wenzelm@21186
  1279
\isamarkupsubsubsection{Haskell serialization%
wenzelm@21186
  1280
}
wenzelm@21186
  1281
\isamarkuptrue%
wenzelm@21172
  1282
%
wenzelm@21186
  1283
\begin{isamarkuptext}%
wenzelm@21186
  1284
For convenience, the default
wenzelm@21186
  1285
  HOL setup for Haskell maps the \isa{eq} class to
wenzelm@21186
  1286
  its counterpart in Haskell, giving custom serializations
haftmann@21348
  1287
  for the class (\isa{{\isasymCODECLASS}}) and its operation:%
wenzelm@21186
  1288
\end{isamarkuptext}%
wenzelm@21186
  1289
\isamarkuptrue%
wenzelm@21172
  1290
%
haftmann@21348
  1291
\isadelimtt
haftmann@21348
  1292
%
haftmann@21348
  1293
\endisadelimtt
haftmann@21348
  1294
%
haftmann@21348
  1295
\isatagtt
wenzelm@21186
  1296
\isacommand{code{\isacharunderscore}class}\isamarkupfalse%
wenzelm@21186
  1297
\ eq\isanewline
haftmann@22798
  1298
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21186
  1299
\isanewline
wenzelm@21186
  1300
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
haftmann@22798
  1301
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline
haftmann@22798
  1302
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1303
\endisatagtt
haftmann@21348
  1304
{\isafoldtt}%
haftmann@21348
  1305
%
haftmann@21348
  1306
\isadelimtt
haftmann@21348
  1307
%
haftmann@21348
  1308
\endisadelimtt
haftmann@21348
  1309
%
wenzelm@21186
  1310
\begin{isamarkuptext}%
wenzelm@21186
  1311
A problem now occurs whenever a type which
wenzelm@21186
  1312
  is an instance of \isa{eq} in HOL is mapped
wenzelm@21186
  1313
  on a Haskell-builtin type which is also an instance
wenzelm@21186
  1314
  of Haskell \isa{Eq}:%
wenzelm@21186
  1315
\end{isamarkuptext}%
wenzelm@21186
  1316
\isamarkuptrue%
wenzelm@21186
  1317
\isacommand{typedecl}\isamarkupfalse%
wenzelm@21186
  1318
\ bar\isanewline
wenzelm@21186
  1319
\isanewline
haftmann@26513
  1320
\isacommand{instantiation}\isamarkupfalse%
haftmann@26513
  1321
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
haftmann@26513
  1322
\isakeyword{begin}\isanewline
haftmann@26513
  1323
\isanewline
haftmann@26513
  1324
\isacommand{definition}\isamarkupfalse%
haftmann@26732
  1325
\ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}x{\isasymColon}bar{\isacharparenright}\ y\ {\isasymlongleftrightarrow}\ x\ {\isacharequal}\ y{\isachardoublequoteclose}\isanewline
haftmann@26513
  1326
\isanewline
wenzelm@21186
  1327
\isacommand{instance}\isamarkupfalse%
haftmann@26513
  1328
%
wenzelm@21186
  1329
\isadelimproof
wenzelm@21186
  1330
\ %
wenzelm@21186
  1331
\endisadelimproof
wenzelm@21186
  1332
%
wenzelm@21186
  1333
\isatagproof
haftmann@26513
  1334
\isacommand{by}\isamarkupfalse%
haftmann@26513
  1335
\ default\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq{\isacharunderscore}bar{\isacharunderscore}def{\isacharparenright}%
wenzelm@21186
  1336
\endisatagproof
wenzelm@21186
  1337
{\isafoldproof}%
wenzelm@21186
  1338
%
wenzelm@21186
  1339
\isadelimproof
wenzelm@21186
  1340
%
wenzelm@21186
  1341
\endisadelimproof
wenzelm@21186
  1342
\isanewline
haftmann@26513
  1343
\isanewline
haftmann@26513
  1344
\isacommand{end}\isamarkupfalse%
haftmann@26513
  1345
\isanewline
haftmann@21348
  1346
%
haftmann@21348
  1347
\isadelimtt
wenzelm@21186
  1348
\isanewline
haftmann@21348
  1349
%
haftmann@21348
  1350
\endisadelimtt
haftmann@21348
  1351
%
haftmann@21348
  1352
\isatagtt
wenzelm@21186
  1353
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21186
  1354
\ bar\isanewline
wenzelm@21186
  1355
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1356
\endisatagtt
haftmann@21348
  1357
{\isafoldtt}%
haftmann@21348
  1358
%
haftmann@21348
  1359
\isadelimtt
haftmann@21348
  1360
%
haftmann@21348
  1361
\endisadelimtt
haftmann@21348
  1362
%
wenzelm@21186
  1363
\begin{isamarkuptext}%
wenzelm@21186
  1364
The code generator would produce
haftmann@22188
  1365
  an additional instance, which of course is rejected.
haftmann@22188
  1366
  To suppress this additional instance, use
haftmann@22188
  1367
  \isa{{\isasymCODEINSTANCE}}:%
wenzelm@21186
  1368
\end{isamarkuptext}%
wenzelm@21186
  1369
\isamarkuptrue%
haftmann@21348
  1370
%
haftmann@21348
  1371
\isadelimtt
haftmann@21348
  1372
%
haftmann@21348
  1373
\endisadelimtt
haftmann@21348
  1374
%
haftmann@21348
  1375
\isatagtt
wenzelm@21186
  1376
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse%
wenzelm@21186
  1377
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
wenzelm@21186
  1378
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}%
haftmann@21348
  1379
\endisatagtt
haftmann@21348
  1380
{\isafoldtt}%
haftmann@21348
  1381
%
haftmann@21348
  1382
\isadelimtt
haftmann@21348
  1383
%
haftmann@21348
  1384
\endisadelimtt
haftmann@21348
  1385
%
haftmann@22798
  1386
\isamarkupsubsubsection{Pretty printing%
wenzelm@21186
  1387
}
wenzelm@21186
  1388
\isamarkuptrue%
wenzelm@21186
  1389
%
wenzelm@21186
  1390
\begin{isamarkuptext}%
haftmann@22798
  1391
The serializer provides ML interfaces to set up
haftmann@22798
  1392
  pretty serializations for expressions like lists, numerals
haftmann@22798
  1393
  and characters;  these are
haftmann@22798
  1394
  monolithic stubs and should only be used with the
haftmann@28143
  1395
  theories introduced in \secref{sec:library}.%
haftmann@21348
  1396
\end{isamarkuptext}%
haftmann@21348
  1397
\isamarkuptrue%
haftmann@21348
  1398
%
haftmann@21348
  1399
\isamarkupsubsection{Cyclic module dependencies%
haftmann@21348
  1400
}
haftmann@21348
  1401
\isamarkuptrue%
haftmann@21348
  1402
%
haftmann@21348
  1403
\begin{isamarkuptext}%
haftmann@21348
  1404
Sometimes the awkward situation occurs that dependencies
haftmann@21348
  1405
  between definitions introduce cyclic dependencies
haftmann@21348
  1406
  between modules, which in the Haskell world leaves
haftmann@21348
  1407
  you to the mercy of the Haskell implementation you are using,
haftmann@21348
  1408
  while for SML code generation is not possible.
haftmann@21348
  1409
haftmann@21348
  1410
  A solution is to declare module names explicitly.
haftmann@21348
  1411
  Let use assume the three cyclically dependent
haftmann@21348
  1412
  modules are named \emph{A}, \emph{B} and \emph{C}.
haftmann@21348
  1413
  Then, by stating%
haftmann@21348
  1414
\end{isamarkuptext}%
haftmann@21348
  1415
\isamarkuptrue%
haftmann@21348
  1416
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse%
haftmann@21348
  1417
\ SML\isanewline
haftmann@21348
  1418
\ \ A\ ABC\isanewline
haftmann@21348
  1419
\ \ B\ ABC\isanewline
haftmann@21348
  1420
\ \ C\ ABC%
haftmann@21348
  1421
\begin{isamarkuptext}%
haftmann@21348
  1422
we explicitly map all those modules on \emph{ABC},
haftmann@21348
  1423
  resulting in an ad-hoc merge of this three modules
haftmann@21348
  1424
  at serialization time.%
haftmann@21348
  1425
\end{isamarkuptext}%
haftmann@21348
  1426
\isamarkuptrue%
haftmann@21348
  1427
%
haftmann@22798
  1428
\isamarkupsubsection{Incremental code generation%
haftmann@22798
  1429
}
haftmann@22798
  1430
\isamarkuptrue%
haftmann@22798
  1431
%
haftmann@22798
  1432
\begin{isamarkuptext}%
haftmann@22798
  1433
Code generation is \emph{incremental}: theorems
haftmann@22798
  1434
  and abstract intermediate code are cached and extended on demand.
haftmann@22798
  1435
  The cache may be partially or fully dropped if the underlying
haftmann@22798
  1436
  executable content of the theory changes.
haftmann@22798
  1437
  Implementation of caching is supposed to transparently
haftmann@22798
  1438
  hid away the details from the user.  Anyway, caching
haftmann@22798
  1439
  reaches the surface by using a slightly more general form
haftmann@22798
  1440
  of the \isa{{\isasymCODETHMS}}, \isa{{\isasymCODEDEPS}}
haftmann@24379
  1441
  and \isa{{\isasymEXPORTCODE}} commands: the list of constants
haftmann@22798
  1442
  may be omitted.  Then, all constants with code theorems
haftmann@22798
  1443
  in the current cache are referred to.%
haftmann@22798
  1444
\end{isamarkuptext}%
haftmann@22798
  1445
\isamarkuptrue%
haftmann@22798
  1446
%
wenzelm@21172
  1447
\isamarkupsection{ML interfaces \label{sec:ml}%
wenzelm@21172
  1448
}
wenzelm@21172
  1449
\isamarkuptrue%
wenzelm@21172
  1450
%
haftmann@21348
  1451
\begin{isamarkuptext}%
haftmann@21348
  1452
Since the code generator framework not only aims to provide
haftmann@21348
  1453
  a nice Isar interface but also to form a base for
haftmann@21348
  1454
  code-generation-based applications, here a short
haftmann@21348
  1455
  description of the most important ML interfaces.%
haftmann@21348
  1456
\end{isamarkuptext}%
haftmann@21348
  1457
\isamarkuptrue%
haftmann@21348
  1458
%
haftmann@24217
  1459
\isamarkupsubsection{Executable theory content: \isa{Code}%
wenzelm@21172
  1460
}
wenzelm@21172
  1461
\isamarkuptrue%
wenzelm@21172
  1462
%
wenzelm@21172
  1463
\begin{isamarkuptext}%
wenzelm@21172
  1464
This Pure module implements the core notions of
wenzelm@21172
  1465
  executable content of a theory.%
wenzelm@21172
  1466
\end{isamarkuptext}%
wenzelm@21172
  1467
\isamarkuptrue%
wenzelm@21172
  1468
%
haftmann@22292
  1469
\isamarkupsubsubsection{Managing executable content%
wenzelm@21172
  1470
}
wenzelm@21172
  1471
\isamarkuptrue%
wenzelm@21172
  1472
%
wenzelm@21172
  1473
\isadelimmlref
wenzelm@21172
  1474
%
wenzelm@21172
  1475
\endisadelimmlref
wenzelm@21172
  1476
%
wenzelm@21172
  1477
\isatagmlref
wenzelm@21172
  1478
%
wenzelm@21172
  1479
\begin{isamarkuptext}%
wenzelm@21172
  1480
\begin{mldecls}
wenzelm@26856
  1481
  \indexml{Code.add\_func}\verb|Code.add_func: thm -> theory -> theory| \\
wenzelm@26856
  1482
  \indexml{Code.del\_func}\verb|Code.del_func: thm -> theory -> theory| \\
wenzelm@26856
  1483
  \indexml{Code.add\_funcl}\verb|Code.add_funcl: string * thm list Susp.T -> theory -> theory| \\
haftmann@27557
  1484
  \indexml{Code.map\_pre}\verb|Code.map_pre: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory| \\
haftmann@27557
  1485
  \indexml{Code.map\_post}\verb|Code.map_post: (MetaSimplifier.simpset -> MetaSimplifier.simpset) -> theory -> theory| \\
haftmann@27609
  1486
  \indexml{Code.add\_functrans}\verb|Code.add_functrans: string * (theory -> thm list -> thm list option)|\isasep\isanewline%
haftmann@21348
  1487
\verb|    -> theory -> theory| \\
haftmann@27557
  1488
  \indexml{Code.del\_functrans}\verb|Code.del_functrans: string -> theory -> theory| \\
wenzelm@26856
  1489
  \indexml{Code.add\_datatype}\verb|Code.add_datatype: (string * typ) list -> theory -> theory| \\
wenzelm@26856
  1490
  \indexml{Code.get\_datatype}\verb|Code.get_datatype: theory -> string|\isasep\isanewline%
haftmann@22479
  1491
\verb|    -> (string * sort) list * (string * typ list) list| \\
wenzelm@26856
  1492
  \indexml{Code.get\_datatype\_of\_constr}\verb|Code.get_datatype_of_constr: theory -> string -> string option|
wenzelm@21172
  1493
  \end{mldecls}
wenzelm@21172
  1494
wenzelm@21172
  1495
  \begin{description}
wenzelm@21172
  1496
haftmann@24217
  1497
  \item \verb|Code.add_func|~\isa{thm}~\isa{thy} adds function
haftmann@21348
  1498
     theorem \isa{thm} to executable content.
haftmann@21348
  1499
haftmann@24217
  1500
  \item \verb|Code.del_func|~\isa{thm}~\isa{thy} removes function
haftmann@21348
  1501
     theorem \isa{thm} from executable content, if present.
haftmann@21348
  1502
haftmann@24217
  1503
  \item \verb|Code.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds
haftmann@22060
  1504
     suspended defining equations \isa{lthms} for constant
haftmann@21348
  1505
     \isa{const} to executable content.
haftmann@21348
  1506
haftmann@27557
  1507
  \item \verb|Code.map_pre|~\isa{f}~\isa{thy} changes
haftmann@27557
  1508
     the preprocessor simpset.
haftmann@21348
  1509
haftmann@28143
  1510
  \item \verb|Code.add_functrans|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
haftmann@27609
  1511
     function transformer \isa{f} (named \isa{name}) to executable content;
haftmann@27609
  1512
     \isa{f} is a transformer of the defining equations belonging
haftmann@21348
  1513
     to a certain function definition, depending on the
haftmann@27609
  1514
     current theory context.  Returning \isa{NONE} indicates that no
haftmann@27609
  1515
     transformation took place;  otherwise, the whole process will be iterated
haftmann@27609
  1516
     with the new defining equations.
haftmann@21348
  1517
haftmann@27557
  1518
  \item \verb|Code.del_functrans|~\isa{name}~\isa{thy} removes
haftmann@27609
  1519
     function transformer named \isa{name} from executable content.
haftmann@22060
  1520
haftmann@24421
  1521
  \item \verb|Code.add_datatype|~\isa{cs}~\isa{thy} adds
haftmann@24421
  1522
     a datatype to executable content, with generation
haftmann@24421
  1523
     set \isa{cs}.
haftmann@21348
  1524
haftmann@24217
  1525
  \item \verb|Code.get_datatype_of_constr|~\isa{thy}~\isa{const}
haftmann@21348
  1526
     returns type constructor corresponding to
haftmann@21348
  1527
     constructor \isa{const}; returns \isa{NONE}
haftmann@21348
  1528
     if \isa{const} is no constructor.
haftmann@21348
  1529
haftmann@21348
  1530
  \end{description}%
haftmann@21348
  1531
\end{isamarkuptext}%
haftmann@21348
  1532
\isamarkuptrue%
haftmann@21348
  1533
%
haftmann@21348
  1534
\endisatagmlref
haftmann@21348
  1535
{\isafoldmlref}%
haftmann@21348
  1536
%
haftmann@21348
  1537
\isadelimmlref
haftmann@21348
  1538
%
haftmann@21348
  1539
\endisadelimmlref
haftmann@21348
  1540
%
haftmann@22292
  1541
\isamarkupsubsection{Auxiliary%
wenzelm@21172
  1542
}
wenzelm@21172
  1543
\isamarkuptrue%
wenzelm@21172
  1544
%
wenzelm@21172
  1545
\isadelimmlref
wenzelm@21172
  1546
%
wenzelm@21172
  1547
\endisadelimmlref
wenzelm@21172
  1548
%
wenzelm@21172
  1549
\isatagmlref
wenzelm@21172
  1550
%
wenzelm@21172
  1551
\begin{isamarkuptext}%
wenzelm@21172
  1552
\begin{mldecls}
haftmann@31156
  1553
  \indexml{Code\_Unit.read\_const}\verb|Code.read_const: theory -> string -> string| \\
haftmann@31156
  1554
  \indexml{Code\_Unit.head\_func}\verb|Code.head_func: thm -> string * ((string * sort) list * typ)| \\
haftmann@31156
  1555
  \indexml{Code\_Unit.rewrite\_func}\verb|Code.rewrite_func: MetaSimplifier.simpset -> thm -> thm| \\
haftmann@21348
  1556
  \end{mldecls}
haftmann@21348
  1557
haftmann@21348
  1558
  \begin{description}
haftmann@21348
  1559
haftmann@31156
  1560
  \item \verb|Code.read_const|~\isa{thy}~\isa{s}
haftmann@21348
  1561
     reads a constant as a concrete term expression \isa{s}.
haftmann@21348
  1562
haftmann@31156
  1563
  \item \verb|Code.head_func|~\isa{thm}
haftmann@22751
  1564
     extracts the constant and its type from a defining equation \isa{thm}.
haftmann@21348
  1565
haftmann@31156
  1566
  \item \verb|Code.rewrite_func|~\isa{ss}~\isa{thm}
haftmann@27557
  1567
     rewrites a defining equation \isa{thm} with a simpset \isa{ss};
haftmann@27557
  1568
     only arguments and right hand side are rewritten,
haftmann@22060
  1569
     not the head of the defining equation.
haftmann@21348
  1570
haftmann@21348
  1571
  \end{description}%
wenzelm@21172
  1572
\end{isamarkuptext}%
wenzelm@21172
  1573
\isamarkuptrue%
wenzelm@21172
  1574
%
wenzelm@21172
  1575
\endisatagmlref
wenzelm@21172
  1576
{\isafoldmlref}%
wenzelm@21172
  1577
%
wenzelm@21172
  1578
\isadelimmlref
wenzelm@21172
  1579
%
wenzelm@21172
  1580
\endisadelimmlref
wenzelm@21172
  1581
%
haftmann@20967
  1582
\isamarkupsubsection{Implementing code generator applications%
haftmann@20967
  1583
}
haftmann@20967
  1584
\isamarkuptrue%
haftmann@20967
  1585
%
wenzelm@21172
  1586
\begin{isamarkuptext}%
haftmann@21348
  1587
Implementing code generator applications on top
haftmann@21348
  1588
  of the framework set out so far usually not only
haftmann@21348
  1589
  involves using those primitive interfaces
haftmann@21348
  1590
  but also storing code-dependent data and various
haftmann@21348
  1591
  other things.
haftmann@21348
  1592
haftmann@21348
  1593
  \begin{warn}
wenzelm@21172
  1594
    Some interfaces discussed here have not reached
wenzelm@21172
  1595
    a final state yet.
wenzelm@21172
  1596
    Changes likely to occur in future.
haftmann@21452
  1597
  \end{warn}%
wenzelm@21172
  1598
\end{isamarkuptext}%
wenzelm@21172
  1599
\isamarkuptrue%
wenzelm@21172
  1600
%
wenzelm@21172
  1601
\isamarkupsubsubsection{Data depending on the theory's executable content%
wenzelm@21172
  1602
}
wenzelm@21172
  1603
\isamarkuptrue%
wenzelm@21172
  1604
%
haftmann@21348
  1605
\begin{isamarkuptext}%
haftmann@21452
  1606
Due to incrementality of code generation, changes in the
haftmann@21452
  1607
  theory's executable content have to be propagated in a
haftmann@21452
  1608
  certain fashion.  Additionally, such changes may occur
haftmann@21452
  1609
  not only during theory extension but also during theory
haftmann@21452
  1610
  merge, which is a little bit nasty from an implementation
haftmann@21452
  1611
  point of view.  The framework provides a solution
haftmann@21452
  1612
  to this technical challenge by providing a functorial
haftmann@21452
  1613
  data slot \verb|CodeDataFun|; on instantiation
haftmann@21452
  1614
  of this functor, the following types and operations
haftmann@21452
  1615
  are required:
haftmann@21452
  1616
haftmann@21452
  1617
  \medskip
haftmann@21348
  1618
  \begin{tabular}{l}
haftmann@21348
  1619
  \isa{type\ T} \\
haftmann@21348
  1620
  \isa{val\ empty{\isacharcolon}\ T} \\
haftmann@21348
  1621
  \isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\
haftmann@24217
  1622
  \isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodeUnit{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T}
haftmann@21348
  1623
  \end{tabular}
haftmann@21348
  1624
haftmann@21452
  1625
  \begin{description}
haftmann@21452
  1626
haftmann@21452
  1627
  \item \isa{T} the type of data to store.
haftmann@21452
  1628
haftmann@21452
  1629
  \item \isa{empty} initial (empty) data.
haftmann@21452
  1630
haftmann@21452
  1631
  \item \isa{merge} merging two data slots.
haftmann@21452
  1632
haftmann@22798
  1633
  \item \isa{purge}~\isa{thy}~\isa{consts} propagates changes in executable content;
haftmann@21452
  1634
    if possible, the current theory context is handed over
haftmann@21452
  1635
    as argument \isa{thy} (if there is no current theory context (e.g.~during
haftmann@22798
  1636
    theory merge, \verb|NONE|); \isa{consts} indicates the kind
haftmann@21452
  1637
    of change: \verb|NONE| stands for a fundamental change
haftmann@22798
  1638
    which invalidates any existing code, \isa{SOME\ consts}
haftmann@22798
  1639
    hints that executable content for constants \isa{consts}
haftmann@21452
  1640
    has changed.
haftmann@21452
  1641
haftmann@21452
  1642
  \end{description}
haftmann@21452
  1643
haftmann@21452
  1644
  An instance of \verb|CodeDataFun| provides the following
haftmann@21452
  1645
  interface:
haftmann@21452
  1646
haftmann@21348
  1647
  \medskip
haftmann@21348
  1648
  \begin{tabular}{l}
haftmann@21348
  1649
  \isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1650
  \isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1651
  \isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T}
haftmann@21452
  1652
  \end{tabular}
haftmann@21452
  1653
haftmann@21452
  1654
  \begin{description}
haftmann@21452
  1655
haftmann@21452
  1656
  \item \isa{get} retrieval of the current data.
haftmann@21452
  1657
haftmann@21452
  1658
  \item \isa{change} update of current data (cached!)
haftmann@21452
  1659
    by giving a continuation.
haftmann@21452
  1660
haftmann@21452
  1661
  \item \isa{change{\isacharunderscore}yield} update with side result.
haftmann@21452
  1662
haftmann@21452
  1663
  \end{description}%
haftmann@21452
  1664
\end{isamarkuptext}%
haftmann@21452
  1665
\isamarkuptrue%
haftmann@21452
  1666
%
haftmann@21452
  1667
\begin{isamarkuptext}%
haftmann@24628
  1668
\emph{Happy proving, happy hacking!}%
wenzelm@21172
  1669
\end{isamarkuptext}%
wenzelm@21172
  1670
\isamarkuptrue%
wenzelm@21172
  1671
%
haftmann@20967
  1672
\isadelimtheory
haftmann@20967
  1673
%
haftmann@20967
  1674
\endisadelimtheory
haftmann@20967
  1675
%
haftmann@20967
  1676
\isatagtheory
haftmann@20967
  1677
\isacommand{end}\isamarkupfalse%
haftmann@20967
  1678
%
haftmann@20967
  1679
\endisatagtheory
haftmann@20967
  1680
{\isafoldtheory}%
haftmann@20967
  1681
%
haftmann@20967
  1682
\isadelimtheory
haftmann@20967
  1683
%
haftmann@20967
  1684
\endisadelimtheory
haftmann@20967
  1685
\isanewline
haftmann@20967
  1686
\end{isabellebody}%
haftmann@20967
  1687
%%% Local Variables:
haftmann@20967
  1688
%%% mode: latex
haftmann@20967
  1689
%%% TeX-master: "root"
haftmann@20967
  1690
%%% End: