doc-src/IsarAdvanced/Codegen/Thy/document/Codegen.tex
author haftmann
Wed, 09 May 2007 07:53:04 +0200
changeset 22885 ebde66a71ab0
parent 22845 5f9138bcb3d7
child 22916 8caf6da610e2
permissions -rw-r--r--
continued
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@20967
     7
%
haftmann@20967
     8
\endisadelimtheory
haftmann@20967
     9
%
haftmann@20967
    10
\isatagtheory
wenzelm@21172
    11
%
haftmann@20967
    12
\endisatagtheory
haftmann@20967
    13
{\isafoldtheory}%
haftmann@20967
    14
%
haftmann@20967
    15
\isadelimtheory
haftmann@20967
    16
%
haftmann@20967
    17
\endisadelimtheory
haftmann@20967
    18
%
wenzelm@21172
    19
\isadelimML
wenzelm@21172
    20
%
wenzelm@21172
    21
\endisadelimML
wenzelm@21172
    22
%
wenzelm@21172
    23
\isatagML
wenzelm@21172
    24
%
wenzelm@21172
    25
\endisatagML
wenzelm@21172
    26
{\isafoldML}%
wenzelm@21172
    27
%
wenzelm@21172
    28
\isadelimML
wenzelm@21172
    29
%
wenzelm@21172
    30
\endisadelimML
wenzelm@21172
    31
%
haftmann@20967
    32
\isamarkupchapter{Code generation from Isabelle theories%
haftmann@20967
    33
}
haftmann@20967
    34
\isamarkuptrue%
haftmann@20967
    35
%
haftmann@20967
    36
\isamarkupsection{Introduction%
haftmann@20967
    37
}
haftmann@20967
    38
\isamarkuptrue%
haftmann@20967
    39
%
wenzelm@21172
    40
\isamarkupsubsection{Motivation%
wenzelm@21172
    41
}
wenzelm@21172
    42
\isamarkuptrue%
wenzelm@21172
    43
%
haftmann@20967
    44
\begin{isamarkuptext}%
wenzelm@21172
    45
Executing formal specifications as programs is a well-established
wenzelm@21172
    46
  topic in the theorem proving community.  With increasing
wenzelm@21172
    47
  application of theorem proving systems in the area of
wenzelm@21172
    48
  software development and verification, its relevance manifests
wenzelm@21172
    49
  for running test cases and rapid prototyping.  In logical
wenzelm@21172
    50
  calculi like constructive type theory,
wenzelm@21172
    51
  a notion of executability is implicit due to the nature
haftmann@22550
    52
  of the calculus.  In contrast, specifications in Isabelle
wenzelm@21172
    53
  can be highly non-executable.  In order to bridge
wenzelm@21172
    54
  the gap between logic and executable specifications,
wenzelm@21172
    55
  an explicit non-trivial transformation has to be applied:
wenzelm@21172
    56
  code generation.
wenzelm@21172
    57
wenzelm@21172
    58
  This tutorial introduces a generic code generator for the
wenzelm@21172
    59
  Isabelle system \cite{isa-tutorial}.
wenzelm@21172
    60
  Generic in the sense that the
wenzelm@21172
    61
  \qn{target language} for which code shall ultimately be
wenzelm@21172
    62
  generated is not fixed but may be an arbitrary state-of-the-art
wenzelm@21172
    63
  functional programming language (currently, the implementation
haftmann@22550
    64
  supports SML \cite{SML}, OCaml \cite{OCaml} and Haskell
haftmann@22550
    65
  \cite{haskell-revised-report}).
wenzelm@21172
    66
  We aim to provide a
wenzelm@21172
    67
  versatile environment
wenzelm@21172
    68
  suitable for software development and verification,
wenzelm@21172
    69
  structuring the process
wenzelm@21172
    70
  of code generation into a small set of orthogonal principles
wenzelm@21172
    71
  while achieving a big coverage of application areas
haftmann@21348
    72
  with maximum flexibility.
haftmann@21348
    73
haftmann@22550
    74
  Conceptually the code generator framework is part
haftmann@22550
    75
  of Isabelle's \isa{Pure} meta logic; the object logic
haftmann@22550
    76
  \isa{HOL} which is an extension of \isa{Pure}
haftmann@22550
    77
  already comes with a reasonable framework setup and thus provides
haftmann@22550
    78
  a good working horse for raising code-generation-driven
haftmann@22550
    79
  applications.  So, we assume some familiarity and experience
haftmann@22550
    80
  with the ingredients of the \isa{HOL} \emph{Main} theory
haftmann@22550
    81
  (see also \cite{isa-tutorial}).%
haftmann@20967
    82
\end{isamarkuptext}%
haftmann@20967
    83
\isamarkuptrue%
haftmann@20967
    84
%
wenzelm@21172
    85
\isamarkupsubsection{Overview%
wenzelm@21172
    86
}
wenzelm@21172
    87
\isamarkuptrue%
wenzelm@21172
    88
%
wenzelm@21172
    89
\begin{isamarkuptext}%
wenzelm@21172
    90
The code generator aims to be usable with no further ado
wenzelm@21172
    91
  in most cases while allowing for detailed customization.
haftmann@22550
    92
  This manifests in the structure of this tutorial:
haftmann@22550
    93
  we start with a generic example \secref{sec:example}
haftmann@22550
    94
  and introduce code generation concepts \secref{sec:concept}.
haftmann@22550
    95
  Section
wenzelm@21186
    96
  \secref{sec:basics} explains how to use the framework naively,
wenzelm@21172
    97
  presuming a reasonable default setup.  Then, section
wenzelm@21172
    98
  \secref{sec:advanced} deals with advanced topics,
wenzelm@21172
    99
  introducing further aspects of the code generator framework
wenzelm@21172
   100
  in a motivation-driven manner.  Last, section \secref{sec:ml}
wenzelm@21172
   101
  introduces the framework's internal programming interfaces.
wenzelm@21172
   102
wenzelm@21172
   103
  \begin{warn}
wenzelm@21172
   104
    Ultimately, the code generator which this tutorial deals with
wenzelm@21172
   105
    is supposed to replace the already established code generator
wenzelm@21172
   106
    by Stefan Berghofer \cite{Berghofer-Nipkow:2002}.
wenzelm@21172
   107
    So, for the moment, there are two distinct code generators
wenzelm@21172
   108
    in Isabelle.
wenzelm@21172
   109
    Also note that while the framework itself is largely
haftmann@22550
   110
    object-logic independent, only \isa{HOL} provides a reasonable
wenzelm@21172
   111
    framework setup.    
wenzelm@21172
   112
  \end{warn}%
wenzelm@21172
   113
\end{isamarkuptext}%
wenzelm@21172
   114
\isamarkuptrue%
wenzelm@21172
   115
%
haftmann@22550
   116
\isamarkupsection{An example: a simple theory of search trees \label{sec:example}%
haftmann@22479
   117
}
haftmann@22479
   118
\isamarkuptrue%
haftmann@22550
   119
%
haftmann@22550
   120
\begin{isamarkuptext}%
haftmann@22550
   121
When writing executable specifications, it is convenient to use
haftmann@22550
   122
  three existing packages: the datatype package for defining
haftmann@22550
   123
  datatypes, the function package for (recursive) functions,
haftmann@22550
   124
  and the class package for overloaded definitions.
haftmann@22550
   125
haftmann@22550
   126
  We develope a small theory of search trees; trees are represented
haftmann@22550
   127
  as a datatype with key type \isa{{\isacharprime}a} and value type \isa{{\isacharprime}b}:%
haftmann@22550
   128
\end{isamarkuptext}%
haftmann@22550
   129
\isamarkuptrue%
haftmann@22479
   130
\isacommand{datatype}\isamarkupfalse%
haftmann@22479
   131
\ {\isacharparenleft}{\isacharprime}a{\isacharcomma}\ {\isacharprime}b{\isacharparenright}\ searchtree\ {\isacharequal}\ Leaf\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}linorder{\isachardoublequoteclose}\ {\isacharprime}b\isanewline
haftmann@22550
   132
\ \ {\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
   133
\begin{isamarkuptext}%
haftmann@22550
   134
\noindent Note that we have constrained the type of keys
haftmann@22550
   135
  to the class of total orders, \isa{linorder}.
haftmann@22550
   136
haftmann@22550
   137
  We define \isa{find} and \isa{update} functions:%
haftmann@22550
   138
\end{isamarkuptext}%
haftmann@22550
   139
\isamarkuptrue%
haftmann@22479
   140
\isacommand{fun}\isamarkupfalse%
haftmann@22479
   141
\isanewline
haftmann@22479
   142
\ \ 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
   143
\ \ {\isachardoublequoteopen}find\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharequal}\ {\isacharparenleft}if\ it\ {\isacharequal}\ key\ then\ Some\ val\ else\ None{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22479
   144
\ \ {\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
   145
\isanewline
haftmann@22479
   146
\isacommand{fun}\isamarkupfalse%
haftmann@22479
   147
\isanewline
haftmann@22479
   148
\ \ 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
   149
\ \ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline
haftmann@22479
   150
\ \ \ \ if\ it\ {\isacharequal}\ key\ then\ Leaf\ key\ entry\isanewline
haftmann@22479
   151
\ \ \ \ \ \ else\ if\ it\ {\isasymle}\ key\isanewline
haftmann@22479
   152
\ \ \ \ \ \ then\ Branch\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\isanewline
haftmann@22479
   153
\ \ \ \ \ \ else\ Branch\ {\isacharparenleft}Leaf\ key\ val{\isacharparenright}\ it\ {\isacharparenleft}Leaf\ it\ entry{\isacharparenright}\isanewline
haftmann@22479
   154
\ \ \ {\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@22479
   155
\ \ {\isacharbar}\ {\isachardoublequoteopen}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ t{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}\isanewline
haftmann@22479
   156
\ \ \ \ if\ it\ {\isasymle}\ key\isanewline
haftmann@22479
   157
\ \ \ \ \ \ then\ {\isacharparenleft}Branch\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{1}}{\isacharparenright}\ key\ t{\isadigit{2}}{\isacharparenright}\isanewline
haftmann@22479
   158
\ \ \ \ \ \ else\ {\isacharparenleft}Branch\ t{\isadigit{1}}\ key\ {\isacharparenleft}update\ {\isacharparenleft}it{\isacharcomma}\ entry{\isacharparenright}\ t{\isadigit{2}}{\isacharparenright}{\isacharparenright}\isanewline
haftmann@22550
   159
\ \ \ {\isacharparenright}{\isachardoublequoteclose}%
haftmann@22550
   160
\begin{isamarkuptext}%
haftmann@22550
   161
\noindent For testing purpose, we define a small example
haftmann@22550
   162
  using natural numbers \isa{nat} (which are a \isa{linorder})
haftmann@22550
   163
  as keys and strings values:%
haftmann@22550
   164
\end{isamarkuptext}%
haftmann@22550
   165
\isamarkuptrue%
haftmann@22479
   166
\isacommand{fun}\isamarkupfalse%
haftmann@22479
   167
\isanewline
haftmann@22479
   168
\ \ example\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharparenleft}nat{\isacharcomma}\ string{\isacharparenright}\ searchtree{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22550
   169
\ \ {\isachardoublequoteopen}example\ n\ {\isacharequal}\ update\ {\isacharparenleft}n{\isacharcomma}\ {\isacharprime}{\isacharprime}bar{\isacharprime}{\isacharprime}{\isacharparenright}\ {\isacharparenleft}Leaf\ {\isadigit{0}}\ {\isacharprime}{\isacharprime}foo{\isacharprime}{\isacharprime}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22550
   170
\begin{isamarkuptext}%
haftmann@22550
   171
\noindent Then we generate code%
haftmann@22550
   172
\end{isamarkuptext}%
haftmann@22550
   173
\isamarkuptrue%
haftmann@22479
   174
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   175
\ example\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}tree{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22479
   176
\begin{isamarkuptext}%
haftmann@22550
   177
\noindent which looks like:
haftmann@22550
   178
  \lstsml{Thy/examples/tree.ML}%
haftmann@22479
   179
\end{isamarkuptext}%
haftmann@22479
   180
\isamarkuptrue%
haftmann@22479
   181
%
haftmann@22550
   182
\isamarkupsection{Code generation concepts and process \label{sec:concept}%
wenzelm@21172
   183
}
wenzelm@21172
   184
\isamarkuptrue%
wenzelm@21172
   185
%
wenzelm@21172
   186
\begin{isamarkuptext}%
haftmann@21348
   187
\begin{figure}[h]
haftmann@21348
   188
  \centering
haftmann@21348
   189
  \includegraphics[width=0.7\textwidth]{codegen_process}
haftmann@21348
   190
  \caption{code generator -- processing overview}
haftmann@21348
   191
  \label{fig:process}
haftmann@21348
   192
  \end{figure}
haftmann@21348
   193
haftmann@21348
   194
  The code generator employs a notion of executability
wenzelm@21172
   195
  for three foundational executable ingredients known
wenzelm@21172
   196
  from functional programming:
haftmann@22060
   197
  \emph{defining equations}, \emph{datatypes}, and
haftmann@22060
   198
  \emph{type classes}. A defining equation as a first approximation
wenzelm@21172
   199
  is a theorem of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t}
wenzelm@21172
   200
  (an equation headed by a constant \isa{f} with arguments
haftmann@22798
   201
  \isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right hand side \isa{t}).
haftmann@22060
   202
  Code generation aims to turn defining equations
wenzelm@21172
   203
  into a functional program by running through
wenzelm@21172
   204
  a process (see figure \ref{fig:process}):
wenzelm@21172
   205
wenzelm@21172
   206
  \begin{itemize}
wenzelm@21172
   207
wenzelm@21172
   208
    \item Out of the vast collection of theorems proven in a
wenzelm@21172
   209
      \qn{theory}, a reasonable subset modeling
haftmann@22060
   210
      defining equations is \qn{selected}.
wenzelm@21172
   211
wenzelm@21172
   212
    \item On those selected theorems, certain
wenzelm@21172
   213
      transformations are carried out
wenzelm@21172
   214
      (\qn{preprocessing}).  Their purpose is to turn theorems
wenzelm@21172
   215
      representing non- or badly executable
wenzelm@21172
   216
      specifications into equivalent but executable counterparts.
wenzelm@21172
   217
      The result is a structured collection of \qn{code theorems}.
wenzelm@21172
   218
haftmann@22479
   219
    \item These \qn{code theorems} then are \qn{translated}
wenzelm@21172
   220
      into an Haskell-like intermediate
wenzelm@21172
   221
      language.
wenzelm@21172
   222
wenzelm@21172
   223
    \item Finally, out of the intermediate language the final
wenzelm@21172
   224
      code in the desired \qn{target language} is \qn{serialized}.
wenzelm@21172
   225
wenzelm@21172
   226
  \end{itemize}
wenzelm@21172
   227
wenzelm@21172
   228
  From these steps, only the two last are carried out
wenzelm@21172
   229
  outside the logic; by keeping this layer as
wenzelm@21172
   230
  thin as possible, the amount of code to trust is
wenzelm@21172
   231
  kept to a minimum.%
wenzelm@21172
   232
\end{isamarkuptext}%
wenzelm@21172
   233
\isamarkuptrue%
wenzelm@21172
   234
%
wenzelm@21172
   235
\isamarkupsection{Basics \label{sec:basics}%
haftmann@20967
   236
}
haftmann@20967
   237
\isamarkuptrue%
haftmann@20967
   238
%
haftmann@20967
   239
\isamarkupsubsection{Invoking the code generator%
haftmann@20967
   240
}
haftmann@20967
   241
\isamarkuptrue%
haftmann@20967
   242
%
wenzelm@21172
   243
\begin{isamarkuptext}%
wenzelm@21172
   244
Thanks to a reasonable setup of the HOL theories, in
wenzelm@21172
   245
  most cases code generation proceeds without further ado:%
wenzelm@21172
   246
\end{isamarkuptext}%
wenzelm@21172
   247
\isamarkuptrue%
haftmann@22479
   248
\isacommand{fun}\isamarkupfalse%
wenzelm@21172
   249
\isanewline
haftmann@22479
   250
\ \ fac\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22479
   251
\ \ \ \ {\isachardoublequoteopen}fac\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
haftmann@22479
   252
\ \ {\isacharbar}\ {\isachardoublequoteopen}fac\ {\isacharparenleft}Suc\ n{\isacharparenright}\ {\isacharequal}\ Suc\ n\ {\isacharasterisk}\ fac\ n{\isachardoublequoteclose}%
wenzelm@21172
   253
\begin{isamarkuptext}%
haftmann@22550
   254
\noindent This executable specification is now turned to SML code:%
wenzelm@21172
   255
\end{isamarkuptext}%
wenzelm@21172
   256
\isamarkuptrue%
wenzelm@21172
   257
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   258
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   259
\begin{isamarkuptext}%
haftmann@22550
   260
\noindent  The \isa{{\isasymCODEGEN}} command takes a space-separated list of
wenzelm@21172
   261
  constants together with \qn{serialization directives}
haftmann@22845
   262
  These start with a \qn{target language}
haftmann@22845
   263
  identifier, followed by a file specification
haftmann@22845
   264
  where to write the generated code to.
wenzelm@21172
   265
haftmann@22060
   266
  Internally, the defining equations for all selected
wenzelm@21186
   267
  constants are taken, including any transitively required
wenzelm@21172
   268
  constants, datatypes and classes, resulting in the following
wenzelm@21172
   269
  code:
wenzelm@21172
   270
wenzelm@21172
   271
  \lstsml{Thy/examples/fac.ML}
wenzelm@21172
   272
wenzelm@21172
   273
  The code generator will complain when a required
haftmann@22550
   274
  ingredient does not provide a executable counterpart,
haftmann@22550
   275
  e.g.~generating code
wenzelm@21172
   276
  for constants not yielding
haftmann@22550
   277
  a defining equation (e.g.~the Hilbert choice
haftmann@22550
   278
  operation \isa{SOME}):%
wenzelm@21172
   279
\end{isamarkuptext}%
wenzelm@21172
   280
\isamarkuptrue%
wenzelm@21172
   281
%
wenzelm@21172
   282
\isadelimML
wenzelm@21172
   283
%
wenzelm@21172
   284
\endisadelimML
wenzelm@21172
   285
%
wenzelm@21172
   286
\isatagML
wenzelm@21172
   287
%
wenzelm@21172
   288
\endisatagML
wenzelm@21172
   289
{\isafoldML}%
wenzelm@21172
   290
%
wenzelm@21172
   291
\isadelimML
wenzelm@21172
   292
%
wenzelm@21172
   293
\endisadelimML
wenzelm@21172
   294
\isacommand{definition}\isamarkupfalse%
wenzelm@21172
   295
\isanewline
haftmann@21993
   296
\ \ pick{\isacharunderscore}some\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
   297
\ \ {\isachardoublequoteopen}pick{\isacharunderscore}some\ xs\ {\isacharequal}\ {\isacharparenleft}SOME\ x{\isachardot}\ x\ {\isasymin}\ set\ xs{\isacharparenright}{\isachardoublequoteclose}%
wenzelm@21172
   298
\isadelimML
wenzelm@21172
   299
%
wenzelm@21172
   300
\endisadelimML
wenzelm@21172
   301
%
wenzelm@21172
   302
\isatagML
wenzelm@21172
   303
%
wenzelm@21172
   304
\endisatagML
wenzelm@21172
   305
{\isafoldML}%
wenzelm@21172
   306
%
wenzelm@21172
   307
\isadelimML
wenzelm@21172
   308
%
wenzelm@21172
   309
\endisadelimML
wenzelm@21172
   310
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   311
\ pick{\isacharunderscore}some\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fail{\isacharunderscore}const{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22550
   312
\begin{isamarkuptext}%
haftmann@22550
   313
\noindent will fail.%
haftmann@22550
   314
\end{isamarkuptext}%
haftmann@22550
   315
\isamarkuptrue%
haftmann@22550
   316
%
haftmann@20967
   317
\isamarkupsubsection{Theorem selection%
haftmann@20967
   318
}
haftmann@20967
   319
\isamarkuptrue%
haftmann@20967
   320
%
wenzelm@21172
   321
\begin{isamarkuptext}%
haftmann@22060
   322
The list of all defining equations in a theory may be inspected
haftmann@22292
   323
  using the \isa{{\isasymPRINTCODESETUP}} command:%
wenzelm@21172
   324
\end{isamarkuptext}%
wenzelm@21172
   325
\isamarkuptrue%
haftmann@22292
   326
\isacommand{print{\isacharunderscore}codesetup}\isamarkupfalse%
wenzelm@21172
   327
%
wenzelm@21172
   328
\begin{isamarkuptext}%
wenzelm@21172
   329
\noindent which displays a table of constant with corresponding
haftmann@22060
   330
  defining equations (the additional stuff displayed
haftmann@22751
   331
  shall not bother us for the moment).
wenzelm@21172
   332
wenzelm@21172
   333
  The typical HOL tools are already set up in a way that
haftmann@22751
   334
  function definitions introduced by \isa{{\isasymDEFINITION}},
haftmann@22751
   335
  \isa{{\isasymFUN}},
haftmann@22798
   336
  \isa{{\isasymFUNCTION}}, \isa{{\isasymPRIMREC}},
haftmann@21348
   337
  \isa{{\isasymRECDEF}} are implicitly propagated
haftmann@22060
   338
  to this defining equation table. Specific theorems may be
wenzelm@21172
   339
  selected using an attribute: \emph{code func}. As example,
wenzelm@21172
   340
  a weight selector function:%
wenzelm@21172
   341
\end{isamarkuptext}%
wenzelm@21172
   342
\isamarkuptrue%
wenzelm@21172
   343
\isacommand{consts}\isamarkupfalse%
wenzelm@21172
   344
\isanewline
wenzelm@21172
   345
\ \ pick\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}nat\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}\ list\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
wenzelm@21172
   346
\isanewline
wenzelm@21172
   347
\isacommand{primrec}\isamarkupfalse%
wenzelm@21172
   348
\isanewline
wenzelm@21172
   349
\ \ {\isachardoublequoteopen}pick\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ n\ {\isacharequal}\ {\isacharparenleft}let\ {\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}\ {\isacharequal}\ x\ in\isanewline
wenzelm@21172
   350
\ \ \ \ if\ n\ {\isacharless}\ k\ then\ v\ else\ pick\ xs\ {\isacharparenleft}n\ {\isacharminus}\ k{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
wenzelm@21172
   351
\begin{isamarkuptext}%
haftmann@22798
   352
\noindent We want to eliminate the explicit destruction
wenzelm@21172
   353
  of \isa{x} to \isa{{\isacharparenleft}k{\isacharcomma}\ v{\isacharparenright}}:%
wenzelm@21172
   354
\end{isamarkuptext}%
wenzelm@21172
   355
\isamarkuptrue%
wenzelm@21172
   356
\isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   357
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@21172
   358
\ \ {\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
   359
%
wenzelm@21172
   360
\isadelimproof
wenzelm@21172
   361
\ \ %
wenzelm@21172
   362
\endisadelimproof
wenzelm@21172
   363
%
wenzelm@21172
   364
\isatagproof
wenzelm@21172
   365
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   366
\ simp%
wenzelm@21172
   367
\endisatagproof
wenzelm@21172
   368
{\isafoldproof}%
wenzelm@21172
   369
%
wenzelm@21172
   370
\isadelimproof
wenzelm@21172
   371
\isanewline
wenzelm@21172
   372
%
wenzelm@21172
   373
\endisadelimproof
wenzelm@21172
   374
\isanewline
wenzelm@21172
   375
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   376
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{1}}{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   377
\begin{isamarkuptext}%
haftmann@22798
   378
\noindent This theorem now is used for generating code:
wenzelm@21172
   379
wenzelm@21172
   380
  \lstsml{Thy/examples/pick1.ML}
wenzelm@21172
   381
haftmann@22798
   382
  \noindent It might be convenient to remove the pointless original
haftmann@22845
   383
  equation, using the \emph{func del} attribute:%
wenzelm@21172
   384
\end{isamarkuptext}%
wenzelm@21172
   385
\isamarkuptrue%
wenzelm@21172
   386
\isacommand{lemmas}\isamarkupfalse%
haftmann@22845
   387
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ pick{\isachardot}simps\ \isanewline
wenzelm@21172
   388
\isanewline
wenzelm@21172
   389
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   390
\ pick\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}pick{\isadigit{2}}{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   391
\begin{isamarkuptext}%
wenzelm@21172
   392
\lstsml{Thy/examples/pick2.ML}
wenzelm@21172
   393
haftmann@22798
   394
  \noindent Syntactic redundancies are implicitly dropped. For example,
wenzelm@21172
   395
  using a modified version of the \isa{fac} function
haftmann@22060
   396
  as defining equation, the then redundant (since
haftmann@22060
   397
  syntactically subsumed) original defining equations
wenzelm@21172
   398
  are dropped, resulting in a warning:%
wenzelm@21172
   399
\end{isamarkuptext}%
wenzelm@21172
   400
\isamarkuptrue%
wenzelm@21172
   401
\isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   402
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
wenzelm@21172
   403
\ \ {\isachardoublequoteopen}fac\ n\ {\isacharequal}\ {\isacharparenleft}case\ n\ of\ {\isadigit{0}}\ {\isasymRightarrow}\ {\isadigit{1}}\ {\isacharbar}\ Suc\ m\ {\isasymRightarrow}\ n\ {\isacharasterisk}\ fac\ m{\isacharparenright}{\isachardoublequoteclose}\isanewline
wenzelm@21172
   404
%
wenzelm@21172
   405
\isadelimproof
wenzelm@21172
   406
\ \ %
wenzelm@21172
   407
\endisadelimproof
wenzelm@21172
   408
%
wenzelm@21172
   409
\isatagproof
wenzelm@21172
   410
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   411
\ {\isacharparenleft}cases\ n{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   412
\endisatagproof
wenzelm@21172
   413
{\isafoldproof}%
wenzelm@21172
   414
%
wenzelm@21172
   415
\isadelimproof
wenzelm@21172
   416
\isanewline
wenzelm@21172
   417
%
wenzelm@21172
   418
\endisadelimproof
wenzelm@21172
   419
\isanewline
wenzelm@21172
   420
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   421
\ fac\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}fac{\isacharunderscore}case{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   422
\begin{isamarkuptext}%
wenzelm@21172
   423
\lstsml{Thy/examples/fac_case.ML}
wenzelm@21172
   424
wenzelm@21172
   425
  \begin{warn}
haftmann@22292
   426
    The attributes \emph{code} and \emph{code del}
wenzelm@21172
   427
    associated with the existing code generator also apply to
wenzelm@21172
   428
    the new one: \emph{code} implies \emph{code func},
haftmann@22845
   429
    and \emph{code del} implies \emph{code func del}.
wenzelm@21172
   430
  \end{warn}%
wenzelm@21172
   431
\end{isamarkuptext}%
wenzelm@21172
   432
\isamarkuptrue%
wenzelm@21172
   433
%
haftmann@20967
   434
\isamarkupsubsection{Type classes%
haftmann@20967
   435
}
haftmann@20967
   436
\isamarkuptrue%
haftmann@20967
   437
%
wenzelm@21172
   438
\begin{isamarkuptext}%
wenzelm@21172
   439
Type classes enter the game via the Isar class package.
wenzelm@21172
   440
  For a short introduction how to use it, see \cite{isabelle-classes};
wenzelm@21172
   441
  here we just illustrate its impact on code generation.
wenzelm@21172
   442
wenzelm@21172
   443
  In a target language, type classes may be represented
haftmann@22798
   444
  natively (as in the case of Haskell).  For languages
wenzelm@21172
   445
  like SML, they are implemented using \emph{dictionaries}.
wenzelm@21186
   446
  Our following example specifies a class \qt{null},
wenzelm@21172
   447
  assigning to each of its inhabitants a \qt{null} value:%
wenzelm@21172
   448
\end{isamarkuptext}%
wenzelm@21172
   449
\isamarkuptrue%
wenzelm@21172
   450
\isacommand{class}\isamarkupfalse%
haftmann@22479
   451
\ null\ {\isacharequal}\ type\ {\isacharplus}\isanewline
wenzelm@21172
   452
\ \ \isakeyword{fixes}\ null\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
wenzelm@21172
   453
\isanewline
haftmann@22798
   454
\isacommand{fun}\isamarkupfalse%
wenzelm@21172
   455
\isanewline
wenzelm@21172
   456
\ \ head\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a{\isasymColon}null\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
haftmann@22798
   457
\isakeyword{where}\isanewline
wenzelm@21172
   458
\ \ {\isachardoublequoteopen}head\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ null{\isachardoublequoteclose}\isanewline
haftmann@22798
   459
\ \ {\isacharbar}\ {\isachardoublequoteopen}head\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequoteclose}%
wenzelm@21172
   460
\begin{isamarkuptext}%
wenzelm@21172
   461
We provide some instances for our \isa{null}:%
wenzelm@21172
   462
\end{isamarkuptext}%
wenzelm@21172
   463
\isamarkuptrue%
wenzelm@21172
   464
\isacommand{instance}\isamarkupfalse%
wenzelm@21172
   465
\ option\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
wenzelm@21172
   466
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ None{\isachardoublequoteclose}%
wenzelm@21172
   467
\isadelimproof
wenzelm@21172
   468
\ %
wenzelm@21172
   469
\endisadelimproof
wenzelm@21172
   470
%
wenzelm@21172
   471
\isatagproof
wenzelm@21172
   472
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
wenzelm@21172
   473
%
wenzelm@21172
   474
\endisatagproof
wenzelm@21172
   475
{\isafoldproof}%
wenzelm@21172
   476
%
wenzelm@21172
   477
\isadelimproof
wenzelm@21172
   478
%
wenzelm@21172
   479
\endisadelimproof
wenzelm@21172
   480
\isanewline
wenzelm@21172
   481
\isanewline
wenzelm@21172
   482
\isacommand{instance}\isamarkupfalse%
wenzelm@21172
   483
\ list\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}type{\isacharparenright}\ null\isanewline
wenzelm@21172
   484
\ \ {\isachardoublequoteopen}null\ {\isasymequiv}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}%
wenzelm@21172
   485
\isadelimproof
wenzelm@21172
   486
\ %
wenzelm@21172
   487
\endisadelimproof
wenzelm@21172
   488
%
wenzelm@21172
   489
\isatagproof
wenzelm@21172
   490
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
wenzelm@21172
   491
%
wenzelm@21172
   492
\endisatagproof
wenzelm@21172
   493
{\isafoldproof}%
wenzelm@21172
   494
%
wenzelm@21172
   495
\isadelimproof
wenzelm@21172
   496
%
wenzelm@21172
   497
\endisadelimproof
wenzelm@21172
   498
%
wenzelm@21172
   499
\begin{isamarkuptext}%
wenzelm@21172
   500
Constructing a dummy example:%
wenzelm@21172
   501
\end{isamarkuptext}%
wenzelm@21172
   502
\isamarkuptrue%
wenzelm@21172
   503
\isacommand{definition}\isamarkupfalse%
wenzelm@21172
   504
\isanewline
wenzelm@21172
   505
\ \ {\isachardoublequoteopen}dummy\ {\isacharequal}\ head\ {\isacharbrackleft}Some\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharcomma}\ None{\isacharbrackright}{\isachardoublequoteclose}%
wenzelm@21172
   506
\begin{isamarkuptext}%
wenzelm@21186
   507
Type classes offer a suitable occasion to introduce
wenzelm@21172
   508
  the Haskell serializer.  Its usage is almost the same
wenzelm@21172
   509
  as SML, but, in accordance with conventions
wenzelm@21172
   510
  some Haskell systems enforce, each module ends
wenzelm@21172
   511
  up in a single file. The module hierarchy is reflected in
haftmann@22845
   512
  the file system, with root directory given as file specification.%
wenzelm@21172
   513
\end{isamarkuptext}%
wenzelm@21172
   514
\isamarkuptrue%
wenzelm@21172
   515
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   516
\ dummy\ \isakeyword{in}\ Haskell\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}{\isachardoublequoteclose}%
wenzelm@21172
   517
\begin{isamarkuptext}%
wenzelm@21172
   518
\lsthaskell{Thy/examples/Codegen.hs}
haftmann@22798
   519
  \noindent (we have left out all other modules).
wenzelm@21172
   520
haftmann@22798
   521
  \medskip
wenzelm@21172
   522
wenzelm@21172
   523
  The whole code in SML with explicit dictionary passing:%
wenzelm@21172
   524
\end{isamarkuptext}%
wenzelm@21172
   525
\isamarkuptrue%
wenzelm@21172
   526
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   527
\ dummy\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
   528
\begin{isamarkuptext}%
haftmann@22798
   529
\lstsml{Thy/examples/class.ML}
haftmann@22798
   530
haftmann@22798
   531
  \medskip
haftmann@22798
   532
haftmann@22798
   533
  \noindent or in OCaml:%
haftmann@22292
   534
\end{isamarkuptext}%
haftmann@22292
   535
\isamarkuptrue%
haftmann@22292
   536
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   537
\ dummy\ \isakeyword{in}\ OCaml\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}class{\isachardot}ocaml{\isachardoublequoteclose}%
haftmann@22292
   538
\begin{isamarkuptext}%
haftmann@22798
   539
\lstsml{Thy/examples/class.ocaml}
wenzelm@21172
   540
haftmann@22798
   541
  \medskip The explicit association of constants
haftmann@22845
   542
  to classes can be inspected using the \isa{{\isasymPRINTCLASSES}}
haftmann@22845
   543
  command.%
wenzelm@21172
   544
\end{isamarkuptext}%
wenzelm@21172
   545
\isamarkuptrue%
wenzelm@21172
   546
%
wenzelm@21172
   547
\isamarkupsection{Recipes and advanced topics \label{sec:advanced}%
haftmann@20967
   548
}
haftmann@20967
   549
\isamarkuptrue%
haftmann@20967
   550
%
wenzelm@21172
   551
\begin{isamarkuptext}%
wenzelm@21172
   552
In this tutorial, we do not attempt to give an exhaustive
wenzelm@21172
   553
  description of the code generator framework; instead,
wenzelm@21172
   554
  we cast a light on advanced topics by introducing
wenzelm@21172
   555
  them together with practically motivated examples.  Concerning
wenzelm@21172
   556
  further reading, see
wenzelm@21172
   557
wenzelm@21172
   558
  \begin{itemize}
wenzelm@21172
   559
wenzelm@21172
   560
  \item the Isabelle/Isar Reference Manual \cite{isabelle-isar-ref}
wenzelm@21172
   561
    for exhaustive syntax diagrams.
haftmann@21348
   562
  \item or \fixme[ref] which deals with foundational issues
wenzelm@21172
   563
    of the code generator framework.
wenzelm@21172
   564
wenzelm@21172
   565
  \end{itemize}%
wenzelm@21172
   566
\end{isamarkuptext}%
wenzelm@21172
   567
\isamarkuptrue%
wenzelm@21172
   568
%
haftmann@22798
   569
\isamarkupsubsection{Library theories \label{sec:library}%
haftmann@20967
   570
}
haftmann@20967
   571
\isamarkuptrue%
haftmann@20967
   572
%
wenzelm@21172
   573
\begin{isamarkuptext}%
wenzelm@21172
   574
The HOL \emph{Main} theory already provides a code generator setup
wenzelm@21172
   575
  which should be suitable for most applications. Common extensions
wenzelm@21172
   576
  and modifications are available by certain theories of the HOL
wenzelm@21172
   577
  library; beside being useful in applications, they may serve
wenzelm@21186
   578
  as a tutorial for customizing the code generator setup.
wenzelm@21172
   579
wenzelm@21172
   580
  \begin{description}
wenzelm@21172
   581
haftmann@22798
   582
    \item[\isa{Pretty{\isacharunderscore}Int}] represents HOL integers by big
haftmann@22798
   583
       integer literals in target languages.
haftmann@22798
   584
    \item[\isa{Pretty{\isacharunderscore}Char}] represents HOL characters by 
haftmann@22798
   585
       character literals in target languages.
haftmann@22798
   586
    \item[\isa{Pretty{\isacharunderscore}Char{\isacharunderscore}chr}] like \isa{Pretty{\isacharunderscore}Char{\isacharunderscore}chr},
haftmann@22798
   587
       but also offers treatment of character codes; includes
haftmann@22798
   588
       \isa{Pretty{\isacharunderscore}Int}.
haftmann@21452
   589
    \item[\isa{ExecutableSet}] allows to generate code
wenzelm@21172
   590
       for finite sets using lists.
haftmann@21452
   591
    \item[\isa{ExecutableRat}] \label{exec_rat} implements rational
wenzelm@21172
   592
       numbers as triples \isa{{\isacharparenleft}sign{\isacharcomma}\ enumerator{\isacharcomma}\ denominator{\isacharparenright}}.
haftmann@21452
   593
    \item[\isa{EfficientNat}] \label{eff_nat} implements natural numbers by integers,
wenzelm@21186
   594
       which in general will result in higher efficency; pattern
wenzelm@21172
   595
       matching with \isa{{\isadigit{0}}} / \isa{Suc}
haftmann@22798
   596
       is eliminated;  includes \isa{Pretty{\isacharunderscore}Int}.
haftmann@21452
   597
    \item[\isa{MLString}] provides an additional datatype \isa{mlstring};
wenzelm@21172
   598
       in the HOL default setup, strings in HOL are mapped to list
haftmann@22798
   599
       of HOL characters in SML; values of type \isa{mlstring} are
wenzelm@21172
   600
       mapped to strings in SML.
wenzelm@21172
   601
wenzelm@21172
   602
  \end{description}%
wenzelm@21172
   603
\end{isamarkuptext}%
wenzelm@21172
   604
\isamarkuptrue%
wenzelm@21172
   605
%
wenzelm@21172
   606
\isamarkupsubsection{Preprocessing%
haftmann@20967
   607
}
haftmann@20967
   608
\isamarkuptrue%
haftmann@20967
   609
%
wenzelm@21172
   610
\begin{isamarkuptext}%
wenzelm@21172
   611
Before selected function theorems are turned into abstract
wenzelm@21172
   612
  code, a chain of definitional transformation steps is carried
wenzelm@21186
   613
  out: \emph{preprocessing}. There are three possibilities
wenzelm@21172
   614
  to customize preprocessing: \emph{inline theorems},
wenzelm@21172
   615
  \emph{inline procedures} and \emph{generic preprocessors}.
wenzelm@21172
   616
wenzelm@21172
   617
  \emph{Inline theorems} are rewriting rules applied to each
haftmann@22060
   618
  defining equation.  Due to the interpretation of theorems
haftmann@22060
   619
  of defining equations, rewrites are applied to the right
wenzelm@21172
   620
  hand side and the arguments of the left hand side of an
wenzelm@21172
   621
  equation, but never to the constant heading the left hand side.
wenzelm@21172
   622
  Inline theorems may be declared an undeclared using the
haftmann@22845
   623
  \emph{code inline} or \emph{code inline del} attribute respectively.
wenzelm@21172
   624
  Some common applications:%
wenzelm@21172
   625
\end{isamarkuptext}%
wenzelm@21172
   626
\isamarkuptrue%
wenzelm@21172
   627
%
wenzelm@21172
   628
\begin{itemize}
haftmann@22845
   629
%
haftmann@22845
   630
\begin{isamarkuptext}%
haftmann@22845
   631
\item replacing non-executable constructs by executable ones:%
haftmann@22845
   632
\end{isamarkuptext}%
haftmann@22845
   633
\isamarkuptrue%
haftmann@22845
   634
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   635
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   636
\ \ \ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}%
wenzelm@21172
   637
\isadelimproof
wenzelm@21172
   638
\ %
wenzelm@21172
   639
\endisadelimproof
wenzelm@21172
   640
%
wenzelm@21172
   641
\isatagproof
wenzelm@21172
   642
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   643
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   644
\endisatagproof
wenzelm@21172
   645
{\isafoldproof}%
wenzelm@21172
   646
%
wenzelm@21172
   647
\isadelimproof
wenzelm@21172
   648
%
wenzelm@21172
   649
\endisadelimproof
wenzelm@21172
   650
%
haftmann@22845
   651
\begin{isamarkuptext}%
haftmann@22845
   652
\item eliminating superfluous constants:%
haftmann@22845
   653
\end{isamarkuptext}%
haftmann@22845
   654
\isamarkuptrue%
haftmann@22845
   655
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   656
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   657
\ \ \ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}%
wenzelm@21172
   658
\isadelimproof
wenzelm@21172
   659
\ %
wenzelm@21172
   660
\endisadelimproof
wenzelm@21172
   661
%
wenzelm@21172
   662
\isatagproof
wenzelm@21172
   663
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   664
\ simp%
wenzelm@21172
   665
\endisatagproof
wenzelm@21172
   666
{\isafoldproof}%
wenzelm@21172
   667
%
wenzelm@21172
   668
\isadelimproof
wenzelm@21172
   669
%
wenzelm@21172
   670
\endisadelimproof
wenzelm@21172
   671
%
haftmann@22845
   672
\begin{isamarkuptext}%
haftmann@22845
   673
\item replacing executable but inconvenient constructs:%
haftmann@22845
   674
\end{isamarkuptext}%
haftmann@22845
   675
\isamarkuptrue%
haftmann@22845
   676
\ \ \isacommand{lemma}\isamarkupfalse%
wenzelm@21172
   677
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22845
   678
\ \ \ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}%
wenzelm@21172
   679
\isadelimproof
wenzelm@21172
   680
\ %
wenzelm@21172
   681
\endisadelimproof
wenzelm@21172
   682
%
wenzelm@21172
   683
\isatagproof
wenzelm@21172
   684
\isacommand{by}\isamarkupfalse%
wenzelm@21172
   685
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
wenzelm@21172
   686
\endisatagproof
wenzelm@21172
   687
{\isafoldproof}%
wenzelm@21172
   688
%
wenzelm@21172
   689
\isadelimproof
wenzelm@21172
   690
%
wenzelm@21172
   691
\endisadelimproof
wenzelm@21172
   692
%
wenzelm@21172
   693
\end{itemize}
wenzelm@21172
   694
%
wenzelm@21172
   695
\begin{isamarkuptext}%
haftmann@22845
   696
\noindent The current set of inline theorems may be inspected using
haftmann@22292
   697
  the \isa{{\isasymPRINTCODESETUP}} command.
wenzelm@21172
   698
wenzelm@21172
   699
  \emph{Inline procedures} are a generalized version of inline
wenzelm@21172
   700
  theorems written in ML -- rewrite rules are generated dependent
wenzelm@21172
   701
  on the function theorems for a certain function.  One
wenzelm@21172
   702
  application is the implicit expanding of \isa{nat} numerals
wenzelm@21172
   703
  to \isa{{\isadigit{0}}} / \isa{Suc} representation.  See further
wenzelm@21172
   704
  \secref{sec:ml}
wenzelm@21172
   705
wenzelm@21172
   706
  \emph{Generic preprocessors} provide a most general interface,
wenzelm@21172
   707
  transforming a list of function theorems to another
wenzelm@21172
   708
  list of function theorems, provided that neither the heading
wenzelm@21172
   709
  constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
haftmann@21348
   710
  pattern elimination implemented in
haftmann@21452
   711
  theory \isa{EfficientNat} (\secref{eff_nat}) uses this
wenzelm@21172
   712
  interface.
wenzelm@21172
   713
wenzelm@21172
   714
  \begin{warn}
wenzelm@21172
   715
    The order in which single preprocessing steps are carried
wenzelm@21172
   716
    out currently is not specified; in particular, preprocessing
wenzelm@21186
   717
    is \emph{no} fix point process.  Keep this in mind when
wenzelm@21172
   718
    setting up the preprocessor.
wenzelm@21172
   719
wenzelm@21172
   720
    Further, the attribute \emph{code unfold}
wenzelm@21172
   721
    associated with the existing code generator also applies to
wenzelm@21172
   722
    the new one: \emph{code unfold} implies \emph{code inline}.
wenzelm@21172
   723
  \end{warn}%
wenzelm@21172
   724
\end{isamarkuptext}%
wenzelm@21172
   725
\isamarkuptrue%
wenzelm@21172
   726
%
haftmann@22798
   727
\isamarkupsubsection{Concerning operational equality%
haftmann@22798
   728
}
haftmann@22798
   729
\isamarkuptrue%
haftmann@22798
   730
%
haftmann@22798
   731
\begin{isamarkuptext}%
haftmann@22798
   732
Surely you have already noticed how equality is treated
haftmann@22798
   733
  by the code generator:%
haftmann@22798
   734
\end{isamarkuptext}%
haftmann@22798
   735
\isamarkuptrue%
haftmann@22798
   736
\isacommand{fun}\isamarkupfalse%
haftmann@22798
   737
\isanewline
haftmann@22798
   738
\ \ 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
   739
\ \ \ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
haftmann@22798
   740
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
haftmann@22798
   741
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
haftmann@22798
   742
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
haftmann@22798
   743
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
haftmann@22798
   744
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22798
   745
\begin{isamarkuptext}%
haftmann@22798
   746
The membership test during preprocessing is rewritten,
haftmann@22798
   747
  resulting in \isa{op\ mem}, which itself
haftmann@22798
   748
  performs an explicit equality check.%
haftmann@22798
   749
\end{isamarkuptext}%
haftmann@22798
   750
\isamarkuptrue%
haftmann@22798
   751
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
   752
\ collect{\isacharunderscore}duplicates\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}collect{\isacharunderscore}duplicates{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   753
\begin{isamarkuptext}%
haftmann@22798
   754
\lstsml{Thy/examples/collect_duplicates.ML}%
haftmann@22798
   755
\end{isamarkuptext}%
haftmann@22798
   756
\isamarkuptrue%
haftmann@22798
   757
%
haftmann@22798
   758
\begin{isamarkuptext}%
haftmann@22798
   759
Obviously, polymorphic equality is implemented the Haskell
haftmann@22798
   760
  way using a type class.  How is this achieved?  By an
haftmann@22798
   761
  almost trivial definition in the HOL setup:%
haftmann@22798
   762
\end{isamarkuptext}%
haftmann@22798
   763
\isamarkuptrue%
haftmann@22798
   764
%
haftmann@22798
   765
\isadelimML
haftmann@22798
   766
%
haftmann@22798
   767
\endisadelimML
haftmann@22798
   768
%
haftmann@22798
   769
\isatagML
haftmann@22798
   770
%
haftmann@22798
   771
\endisatagML
haftmann@22798
   772
{\isafoldML}%
haftmann@22798
   773
%
haftmann@22798
   774
\isadelimML
haftmann@22798
   775
%
haftmann@22798
   776
\endisadelimML
haftmann@22798
   777
\isacommand{class}\isamarkupfalse%
haftmann@22798
   778
\ eq\ {\isacharparenleft}\isakeyword{attach}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}{\isacharparenright}\ {\isacharequal}\ type%
haftmann@22798
   779
\begin{isamarkuptext}%
haftmann@22798
   780
This merely introduces a class \isa{eq} with corresponding
haftmann@22798
   781
  operation \isa{op\ {\isacharequal}};
haftmann@22798
   782
  the preprocessing framework does the rest.
haftmann@22798
   783
  For datatypes, instances of \isa{eq} are implicitly derived
haftmann@22798
   784
  when possible.
haftmann@22798
   785
haftmann@22798
   786
  Though this \isa{eq} class is designed to get rarely in
haftmann@22798
   787
  the way, a subtlety
haftmann@22798
   788
  enters the stage when definitions of overloaded constants
haftmann@22798
   789
  are dependent on operational equality.  For example, let
haftmann@22798
   790
  us define a lexicographic ordering on tuples:%
haftmann@22798
   791
\end{isamarkuptext}%
haftmann@22798
   792
\isamarkuptrue%
haftmann@22798
   793
%
haftmann@22798
   794
\isadelimML
haftmann@22798
   795
%
haftmann@22798
   796
\endisadelimML
haftmann@22798
   797
%
haftmann@22798
   798
\isatagML
haftmann@22798
   799
%
haftmann@22798
   800
\endisatagML
haftmann@22798
   801
{\isafoldML}%
haftmann@22798
   802
%
haftmann@22798
   803
\isadelimML
haftmann@22798
   804
%
haftmann@22798
   805
\endisadelimML
haftmann@22798
   806
\isacommand{instance}\isamarkupfalse%
haftmann@22798
   807
\ {\isacharasterisk}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}ord{\isacharcomma}\ ord{\isacharparenright}\ ord\isanewline
haftmann@22798
   808
\ \ less{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline
haftmann@22798
   809
\ \ \ \ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isacharless}\ p{\isadigit{2}}\ {\isasymequiv}\ let\ {\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline
haftmann@22798
   810
\ \ \ \ 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
   811
\ \ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharcolon}\isanewline
haftmann@22798
   812
\ \ \ \ {\isachardoublequoteopen}p{\isadigit{1}}\ {\isasymle}\ p{\isadigit{2}}\ {\isasymequiv}\ let\ {\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}ord{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}ord{\isacharparenright}\ {\isacharequal}\ p{\isadigit{1}}{\isacharsemicolon}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isacharequal}\ p{\isadigit{2}}\ in\isanewline
haftmann@22798
   813
\ \ \ \ 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}%
haftmann@22798
   814
\isadelimproof
haftmann@22798
   815
\ %
haftmann@22798
   816
\endisadelimproof
haftmann@22798
   817
%
haftmann@22798
   818
\isatagproof
haftmann@22798
   819
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
haftmann@22798
   820
%
haftmann@22798
   821
\endisatagproof
haftmann@22798
   822
{\isafoldproof}%
haftmann@22798
   823
%
haftmann@22798
   824
\isadelimproof
haftmann@22798
   825
%
haftmann@22798
   826
\endisadelimproof
haftmann@22798
   827
\isanewline
haftmann@22798
   828
\isanewline
haftmann@22798
   829
\isacommand{lemmas}\isamarkupfalse%
haftmann@22845
   830
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\ {\isacharequal}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\isanewline
haftmann@22798
   831
\isanewline
haftmann@22798
   832
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   833
\ ord{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   834
\ \ {\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
   835
\ \ {\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
   836
%
haftmann@22798
   837
\isadelimproof
haftmann@22798
   838
\ \ %
haftmann@22798
   839
\endisadelimproof
haftmann@22798
   840
%
haftmann@22798
   841
\isatagproof
haftmann@22798
   842
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   843
\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   844
\ simp{\isacharunderscore}all%
haftmann@22798
   845
\endisatagproof
haftmann@22798
   846
{\isafoldproof}%
haftmann@22798
   847
%
haftmann@22798
   848
\isadelimproof
haftmann@22798
   849
%
haftmann@22798
   850
\endisadelimproof
haftmann@22798
   851
%
haftmann@22798
   852
\begin{isamarkuptext}%
haftmann@22798
   853
Then code generation will fail.  Why?  The definition
haftmann@22798
   854
  of \isa{op\ {\isasymle}} depends on equality on both arguments,
haftmann@22798
   855
  which are polymorphic and impose an additional \isa{eq}
haftmann@22798
   856
  class constraint, thus violating the type discipline
haftmann@22798
   857
  for class operations.
haftmann@22798
   858
haftmann@22798
   859
  The solution is to add \isa{eq} explicitly to the first sort arguments in the
haftmann@22798
   860
  code theorems:%
haftmann@22798
   861
\end{isamarkuptext}%
haftmann@22798
   862
\isamarkuptrue%
haftmann@22798
   863
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
   864
\ ord{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22798
   865
\ \ {\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
   866
\ \ \ \ 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
   867
\ \ {\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
   868
\ \ \ \ 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
   869
%
haftmann@22798
   870
\isadelimproof
haftmann@22798
   871
\ \ %
haftmann@22798
   872
\endisadelimproof
haftmann@22798
   873
%
haftmann@22798
   874
\isatagproof
haftmann@22798
   875
\isacommand{unfolding}\isamarkupfalse%
haftmann@22798
   876
\ ord{\isacharunderscore}prod\ \isacommand{by}\isamarkupfalse%
haftmann@22798
   877
\ rule{\isacharplus}%
haftmann@22798
   878
\endisatagproof
haftmann@22798
   879
{\isafoldproof}%
haftmann@22798
   880
%
haftmann@22798
   881
\isadelimproof
haftmann@22798
   882
%
haftmann@22798
   883
\endisadelimproof
haftmann@22798
   884
%
haftmann@22798
   885
\begin{isamarkuptext}%
haftmann@22798
   886
\noindent Then code generation succeeds:%
haftmann@22798
   887
\end{isamarkuptext}%
haftmann@22798
   888
\isamarkuptrue%
haftmann@22798
   889
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22798
   890
\ {\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
   891
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}lexicographic{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
   892
\begin{isamarkuptext}%
haftmann@22798
   893
\lstsml{Thy/examples/lexicographic.ML}%
haftmann@22798
   894
\end{isamarkuptext}%
haftmann@22798
   895
\isamarkuptrue%
haftmann@22798
   896
%
haftmann@22798
   897
\begin{isamarkuptext}%
haftmann@22798
   898
In general, code theorems for overloaded constants may have more
haftmann@22798
   899
  restrictive sort constraints than the underlying instance relation
haftmann@22798
   900
  between class and type constructor as long as the whole system of
haftmann@22798
   901
  constraints is coregular; code theorems violating coregularity
haftmann@22798
   902
  are rejected immediately.  Consequently, it might be necessary
haftmann@22798
   903
  to delete disturbing theorems in the code theorem table,
haftmann@22798
   904
  as we have done here with the original definitions \isa{less{\isacharunderscore}prod{\isacharunderscore}def}
haftmann@22885
   905
  and \isa{less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def}.
haftmann@22885
   906
haftmann@22885
   907
  In some cases, the automatically derived defining equations
haftmann@22885
   908
  for equality on a particular type may not be appropriate.
haftmann@22885
   909
  As example, watch the following datatype representing
haftmann@22885
   910
  monomorphic parametric types:%
haftmann@22885
   911
\end{isamarkuptext}%
haftmann@22885
   912
\isamarkuptrue%
haftmann@22885
   913
\isacommand{datatype}\isamarkupfalse%
haftmann@22885
   914
\ monotype\ {\isacharequal}\ Mono\ string\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}%
haftmann@22885
   915
\isadelimproof
haftmann@22885
   916
%
haftmann@22885
   917
\endisadelimproof
haftmann@22885
   918
%
haftmann@22885
   919
\isatagproof
haftmann@22885
   920
%
haftmann@22885
   921
\endisatagproof
haftmann@22885
   922
{\isafoldproof}%
haftmann@22885
   923
%
haftmann@22885
   924
\isadelimproof
haftmann@22885
   925
%
haftmann@22885
   926
\endisadelimproof
haftmann@22885
   927
%
haftmann@22885
   928
\begin{isamarkuptext}%
haftmann@22885
   929
Then code generation for SML would fail with a message
haftmann@22885
   930
  that the generated code conains illegal mutual dependencies:
haftmann@22885
   931
  the theorem \isa{Mono\ {\isacharquery}tyco{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharquery}typargs{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ Mono\ {\isacharquery}tyco{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isacharquery}typargs{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isasymequiv}\ {\isacharquery}tyco{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ {\isacharquery}tyco{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isasymand}\ {\isacharquery}typargs{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ {\isacharquery}typargs{\isadigit{2}}{\isachardot}{\isadigit{0}}} already requires the
haftmann@22885
   932
  instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires
haftmann@22885
   933
  \isa{Mono\ {\isacharquery}tyco{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharquery}typargs{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ Mono\ {\isacharquery}tyco{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isacharquery}typargs{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isasymequiv}\ {\isacharquery}tyco{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ {\isacharquery}tyco{\isadigit{2}}{\isachardot}{\isadigit{0}}\ {\isasymand}\ {\isacharquery}typargs{\isadigit{1}}{\isachardot}{\isadigit{0}}\ {\isacharequal}\ {\isacharquery}typargs{\isadigit{2}}{\isachardot}{\isadigit{0}}};  Haskell has no problem with mutually
haftmann@22885
   934
  recursive \isa{instance} and \isa{function} definitions,
haftmann@22885
   935
  but the SML serializer does not support this.
haftmann@22885
   936
haftmann@22885
   937
  In such cases, you have to provide you own equality equations
haftmann@22885
   938
  involving auxiliary constants.  In our case,
haftmann@22885
   939
  \isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:%
haftmann@22885
   940
\end{isamarkuptext}%
haftmann@22885
   941
\isamarkuptrue%
haftmann@22885
   942
\isacommand{lemma}\isamarkupfalse%
haftmann@22885
   943
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@22885
   944
\ \ {\isachardoublequoteopen}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}\ {\isacharequal}\ Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}\ {\isasymlongleftrightarrow}\isanewline
haftmann@22885
   945
\ \ \ \ \ 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
   946
%
haftmann@22885
   947
\isadelimproof
haftmann@22885
   948
\ \ %
haftmann@22885
   949
\endisadelimproof
haftmann@22885
   950
%
haftmann@22885
   951
\isatagproof
haftmann@22885
   952
\isacommand{by}\isamarkupfalse%
haftmann@22885
   953
\ {\isacharparenleft}simp\ add{\isacharcolon}\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}%
haftmann@22885
   954
\endisatagproof
haftmann@22885
   955
{\isafoldproof}%
haftmann@22885
   956
%
haftmann@22885
   957
\isadelimproof
haftmann@22885
   958
%
haftmann@22885
   959
\endisadelimproof
haftmann@22885
   960
%
haftmann@22885
   961
\begin{isamarkuptext}%
haftmann@22885
   962
does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:%
haftmann@22885
   963
\end{isamarkuptext}%
haftmann@22885
   964
\isamarkuptrue%
haftmann@22885
   965
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22885
   966
\ {\isachardoublequoteopen}op\ {\isacharequal}\ {\isacharcolon}{\isacharcolon}\ monotype\ {\isasymRightarrow}\ monotype\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
haftmann@22885
   967
\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}monotype{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22885
   968
\begin{isamarkuptext}%
haftmann@22885
   969
\lstsml{Thy/examples/monotype.ML}%
haftmann@22798
   970
\end{isamarkuptext}%
haftmann@22798
   971
\isamarkuptrue%
haftmann@22798
   972
%
haftmann@22798
   973
\isamarkupsubsection{Programs as sets of theorems%
haftmann@22798
   974
}
haftmann@22798
   975
\isamarkuptrue%
haftmann@22798
   976
%
haftmann@22798
   977
\begin{isamarkuptext}%
haftmann@22798
   978
As told in \secref{sec:concept}, code generation is based
haftmann@22798
   979
  on a structured collection of code theorems.
haftmann@22798
   980
  For explorative purpose, this collection
haftmann@22798
   981
  may be inspected using the \isa{{\isasymCODETHMS}} command:%
haftmann@22798
   982
\end{isamarkuptext}%
haftmann@22798
   983
\isamarkuptrue%
haftmann@22798
   984
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
haftmann@22798
   985
\ {\isachardoublequoteopen}op\ mod\ {\isacharcolon}{\isacharcolon}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}%
haftmann@22798
   986
\begin{isamarkuptext}%
haftmann@22798
   987
\noindent prints a table with \emph{all} defining equations
haftmann@22798
   988
  for \isa{op\ mod}, including
haftmann@22798
   989
  \emph{all} defining equations those equations depend
haftmann@22798
   990
  on recursivly.  \isa{{\isasymCODETHMS}} provides a convenient
haftmann@22798
   991
  mechanism to inspect the impact of a preprocessor setup
haftmann@22798
   992
  on defining equations.
haftmann@22798
   993
  
haftmann@22798
   994
  Similarly, the \isa{{\isasymCODEDEPS}} command shows a graph
haftmann@22798
   995
  visualizing dependencies between defining equations.%
haftmann@22798
   996
\end{isamarkuptext}%
haftmann@22798
   997
\isamarkuptrue%
haftmann@22798
   998
%
wenzelm@21172
   999
\isamarkupsubsection{Customizing serialization%
haftmann@20967
  1000
}
haftmann@20967
  1001
\isamarkuptrue%
haftmann@20967
  1002
%
haftmann@22798
  1003
\isamarkupsubsubsection{Basics%
haftmann@22798
  1004
}
haftmann@22798
  1005
\isamarkuptrue%
haftmann@22798
  1006
%
wenzelm@21172
  1007
\begin{isamarkuptext}%
wenzelm@21172
  1008
Consider the following function and its corresponding
wenzelm@21172
  1009
  SML code:%
wenzelm@21172
  1010
\end{isamarkuptext}%
wenzelm@21172
  1011
\isamarkuptrue%
wenzelm@21172
  1012
\isacommand{fun}\isamarkupfalse%
wenzelm@21172
  1013
\isanewline
wenzelm@21172
  1014
\ \ in{\isacharunderscore}interval\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22798
  1015
\ \ {\isachardoublequoteopen}in{\isacharunderscore}interval\ {\isacharparenleft}k{\isacharcomma}\ l{\isacharparenright}\ n\ {\isasymlongleftrightarrow}\ k\ {\isasymle}\ n\ {\isasymand}\ n\ {\isasymle}\ l{\isachardoublequoteclose}%
haftmann@21348
  1016
\isadelimtt
wenzelm@21172
  1017
%
haftmann@21348
  1018
\endisadelimtt
wenzelm@21172
  1019
%
haftmann@21348
  1020
\isatagtt
wenzelm@21172
  1021
%
haftmann@21348
  1022
\endisatagtt
haftmann@21348
  1023
{\isafoldtt}%
wenzelm@21172
  1024
%
haftmann@21348
  1025
\isadelimtt
haftmann@21348
  1026
%
haftmann@21348
  1027
\endisadelimtt
wenzelm@21172
  1028
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
  1029
\ in{\isacharunderscore}interval\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}literal{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1030
\begin{isamarkuptext}%
haftmann@21348
  1031
\lstsml{Thy/examples/bool_literal.ML}
wenzelm@21172
  1032
haftmann@22798
  1033
  \noindent Though this is correct code, it is a little bit unsatisfactory:
wenzelm@21172
  1034
  boolean values and operators are materialized as distinguished
wenzelm@21172
  1035
  entities with have nothing to do with the SML-builtin notion
wenzelm@21172
  1036
  of \qt{bool}.  This results in less readable code;
wenzelm@21172
  1037
  additionally, eager evaluation may cause programs to
wenzelm@21172
  1038
  loop or break which would perfectly terminate when
wenzelm@21172
  1039
  the existing SML \qt{bool} would be used.  To map
wenzelm@21172
  1040
  the HOL \qt{bool} on SML \qt{bool}, we may use
wenzelm@21172
  1041
  \qn{custom serializations}:%
wenzelm@21172
  1042
\end{isamarkuptext}%
wenzelm@21172
  1043
\isamarkuptrue%
haftmann@21348
  1044
%
haftmann@21348
  1045
\isadelimtt
haftmann@21348
  1046
%
haftmann@21348
  1047
\endisadelimtt
haftmann@21348
  1048
%
haftmann@21348
  1049
\isatagtt
wenzelm@21172
  1050
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1051
\ bool\isanewline
wenzelm@21172
  1052
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}bool{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1053
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1054
\ True\ \isakeyword{and}\ False\ \isakeyword{and}\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
wenzelm@21172
  1055
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}true{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}false{\isachardoublequoteclose}\ \isakeyword{and}\ {\isachardoublequoteopen}{\isacharunderscore}\ andalso\ {\isacharunderscore}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1056
\endisatagtt
haftmann@21348
  1057
{\isafoldtt}%
haftmann@21348
  1058
%
haftmann@21348
  1059
\isadelimtt
haftmann@21348
  1060
%
haftmann@21348
  1061
\endisadelimtt
haftmann@21348
  1062
%
wenzelm@21172
  1063
\begin{isamarkuptext}%
haftmann@21348
  1064
The \isa{{\isasymCODETYPE}} commad takes a type constructor
wenzelm@21172
  1065
  as arguments together with a list of custom serializations.
wenzelm@21172
  1066
  Each custom serialization starts with a target language
wenzelm@21172
  1067
  identifier followed by an expression, which during
wenzelm@21172
  1068
  code serialization is inserted whenever the type constructor
haftmann@21348
  1069
  would occur.  For constants, \isa{{\isasymCODECONST}} implements
haftmann@21348
  1070
  the corresponding mechanism.  Each ``\verb|_|'' in
wenzelm@21172
  1071
  a serialization expression is treated as a placeholder
wenzelm@21172
  1072
  for the type constructor's (the constant's) arguments.%
wenzelm@21172
  1073
\end{isamarkuptext}%
wenzelm@21172
  1074
\isamarkuptrue%
wenzelm@21172
  1075
\isacommand{code{\isacharunderscore}reserved}\isamarkupfalse%
wenzelm@21172
  1076
\ SML\isanewline
wenzelm@21172
  1077
\ \ bool\ true\ false%
wenzelm@21172
  1078
\begin{isamarkuptext}%
wenzelm@21172
  1079
To assert that the existing \qt{bool}, \qt{true} and \qt{false}
haftmann@21348
  1080
  is not used for generated code, we use \isa{{\isasymCODERESERVED}}.
wenzelm@21172
  1081
wenzelm@21172
  1082
  After this setup, code looks quite more readable:%
wenzelm@21172
  1083
\end{isamarkuptext}%
wenzelm@21172
  1084
\isamarkuptrue%
wenzelm@21172
  1085
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
  1086
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}mlbool{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1087
\begin{isamarkuptext}%
haftmann@21348
  1088
\lstsml{Thy/examples/bool_mlbool.ML}
wenzelm@21172
  1089
haftmann@22798
  1090
  \noindent This still is not perfect: the parentheses
haftmann@21348
  1091
  around the \qt{andalso} expression are superfluous.
haftmann@21348
  1092
  Though the serializer
wenzelm@21172
  1093
  by no means attempts to imitate the rich Isabelle syntax
wenzelm@21172
  1094
  framework, it provides some common idioms, notably
wenzelm@21172
  1095
  associative infixes with precedences which may be used here:%
wenzelm@21172
  1096
\end{isamarkuptext}%
wenzelm@21172
  1097
\isamarkuptrue%
haftmann@21348
  1098
%
haftmann@21348
  1099
\isadelimtt
haftmann@21348
  1100
%
haftmann@21348
  1101
\endisadelimtt
haftmann@21348
  1102
%
haftmann@21348
  1103
\isatagtt
wenzelm@21172
  1104
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1105
\ {\isachardoublequoteopen}op\ {\isasymand}{\isachardoublequoteclose}\isanewline
haftmann@21348
  1106
\ \ {\isacharparenleft}SML\ \isakeyword{infixl}\ {\isadigit{1}}\ {\isachardoublequoteopen}andalso{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1107
\endisatagtt
haftmann@21348
  1108
{\isafoldtt}%
haftmann@21348
  1109
%
haftmann@21348
  1110
\isadelimtt
haftmann@21348
  1111
%
haftmann@21348
  1112
\endisadelimtt
haftmann@21348
  1113
\isanewline
wenzelm@21172
  1114
\isanewline
wenzelm@21172
  1115
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
  1116
\ in{\isacharunderscore}interval\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}bool{\isacharunderscore}infix{\isachardot}ML{\isachardoublequoteclose}%
wenzelm@21172
  1117
\begin{isamarkuptext}%
haftmann@21348
  1118
\lstsml{Thy/examples/bool_infix.ML}
wenzelm@21172
  1119
haftmann@22798
  1120
  \medskip
haftmann@22798
  1121
wenzelm@21172
  1122
  Next, we try to map HOL pairs to SML pairs, using the
haftmann@21348
  1123
  infix ``\verb|*|'' type constructor and parentheses:%
wenzelm@21172
  1124
\end{isamarkuptext}%
wenzelm@21172
  1125
\isamarkuptrue%
haftmann@21348
  1126
%
haftmann@21348
  1127
\isadelimtt
haftmann@21348
  1128
%
haftmann@21348
  1129
\endisadelimtt
haftmann@21348
  1130
%
haftmann@21348
  1131
\isatagtt
wenzelm@21172
  1132
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21172
  1133
\ {\isacharasterisk}\isanewline
wenzelm@21172
  1134
\ \ {\isacharparenleft}SML\ \isakeyword{infix}\ {\isadigit{2}}\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21172
  1135
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
wenzelm@21172
  1136
\ Pair\isanewline
wenzelm@21172
  1137
\ \ {\isacharparenleft}SML\ {\isachardoublequoteopen}{\isacharbang}{\isacharparenleft}{\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharcomma}{\isacharslash}\ {\isacharparenleft}{\isacharunderscore}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1138
\endisatagtt
haftmann@21348
  1139
{\isafoldtt}%
haftmann@21348
  1140
%
haftmann@21348
  1141
\isadelimtt
haftmann@21348
  1142
%
haftmann@21348
  1143
\endisadelimtt
haftmann@21348
  1144
%
wenzelm@21172
  1145
\begin{isamarkuptext}%
haftmann@21348
  1146
The initial bang ``\verb|!|'' tells the serializer to never put
wenzelm@21172
  1147
  parentheses around the whole expression (they are already present),
wenzelm@21172
  1148
  while the parentheses around argument place holders
wenzelm@21172
  1149
  tell not to put parentheses around the arguments.
haftmann@21348
  1150
  The slash ``\verb|/|'' (followed by arbitrary white space)
wenzelm@21172
  1151
  inserts a space which may be used as a break if necessary
wenzelm@21172
  1152
  during pretty printing.
wenzelm@21172
  1153
haftmann@22798
  1154
  These examples give a glimpse what mechanisms
wenzelm@21186
  1155
  custom serializations provide; however their usage
wenzelm@21186
  1156
  requires careful thinking in order not to introduce
wenzelm@21186
  1157
  inconsistencies -- or, in other words:
wenzelm@21186
  1158
  custom serializations are completely axiomatic.
wenzelm@21186
  1159
wenzelm@21186
  1160
  A further noteworthy details is that any special
wenzelm@21186
  1161
  character in a custom serialization may be quoted
haftmann@21348
  1162
  using ``\verb|'|''; thus, in
haftmann@21348
  1163
  ``\verb|fn '_ => _|'' the first
haftmann@21348
  1164
  ``\verb|_|'' is a proper underscore while the
haftmann@21348
  1165
  second ``\verb|_|'' is a placeholder.
wenzelm@21186
  1166
wenzelm@21186
  1167
  The HOL theories provide further
wenzelm@21186
  1168
  examples for custom serializations and form
wenzelm@21186
  1169
  a recommended tutorial on how to use them properly.%
wenzelm@21186
  1170
\end{isamarkuptext}%
wenzelm@21172
  1171
\isamarkuptrue%
wenzelm@21172
  1172
%
wenzelm@21186
  1173
\isamarkupsubsubsection{Haskell serialization%
wenzelm@21186
  1174
}
wenzelm@21186
  1175
\isamarkuptrue%
wenzelm@21172
  1176
%
wenzelm@21186
  1177
\begin{isamarkuptext}%
wenzelm@21186
  1178
For convenience, the default
wenzelm@21186
  1179
  HOL setup for Haskell maps the \isa{eq} class to
wenzelm@21186
  1180
  its counterpart in Haskell, giving custom serializations
haftmann@21348
  1181
  for the class (\isa{{\isasymCODECLASS}}) and its operation:%
wenzelm@21186
  1182
\end{isamarkuptext}%
wenzelm@21186
  1183
\isamarkuptrue%
wenzelm@21172
  1184
%
haftmann@21348
  1185
\isadelimtt
haftmann@21348
  1186
%
haftmann@21348
  1187
\endisadelimtt
haftmann@21348
  1188
%
haftmann@21348
  1189
\isatagtt
wenzelm@21186
  1190
\isacommand{code{\isacharunderscore}class}\isamarkupfalse%
wenzelm@21186
  1191
\ eq\isanewline
haftmann@22798
  1192
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Eq{\isachardoublequoteclose}\ \isakeyword{where}\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\ {\isasymequiv}\ {\isachardoublequoteopen}{\isacharparenleft}{\isacharequal}{\isacharequal}{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\isanewline
wenzelm@21186
  1193
\isanewline
wenzelm@21186
  1194
\isacommand{code{\isacharunderscore}const}\isamarkupfalse%
haftmann@22798
  1195
\ {\isachardoublequoteopen}op\ {\isacharequal}{\isachardoublequoteclose}\isanewline
haftmann@22798
  1196
\ \ {\isacharparenleft}Haskell\ \isakeyword{infixl}\ {\isadigit{4}}\ {\isachardoublequoteopen}{\isacharequal}{\isacharequal}{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1197
\endisatagtt
haftmann@21348
  1198
{\isafoldtt}%
haftmann@21348
  1199
%
haftmann@21348
  1200
\isadelimtt
haftmann@21348
  1201
%
haftmann@21348
  1202
\endisadelimtt
haftmann@21348
  1203
%
wenzelm@21186
  1204
\begin{isamarkuptext}%
wenzelm@21186
  1205
A problem now occurs whenever a type which
wenzelm@21186
  1206
  is an instance of \isa{eq} in HOL is mapped
wenzelm@21186
  1207
  on a Haskell-builtin type which is also an instance
wenzelm@21186
  1208
  of Haskell \isa{Eq}:%
wenzelm@21186
  1209
\end{isamarkuptext}%
wenzelm@21186
  1210
\isamarkuptrue%
wenzelm@21186
  1211
\isacommand{typedecl}\isamarkupfalse%
wenzelm@21186
  1212
\ bar\isanewline
wenzelm@21186
  1213
\isanewline
wenzelm@21186
  1214
\isacommand{instance}\isamarkupfalse%
wenzelm@21186
  1215
\ bar\ {\isacharcolon}{\isacharcolon}\ eq%
wenzelm@21186
  1216
\isadelimproof
wenzelm@21186
  1217
\ %
wenzelm@21186
  1218
\endisadelimproof
wenzelm@21186
  1219
%
wenzelm@21186
  1220
\isatagproof
wenzelm@21186
  1221
\isacommand{{\isachardot}{\isachardot}}\isamarkupfalse%
wenzelm@21186
  1222
%
wenzelm@21186
  1223
\endisatagproof
wenzelm@21186
  1224
{\isafoldproof}%
wenzelm@21186
  1225
%
wenzelm@21186
  1226
\isadelimproof
wenzelm@21186
  1227
%
wenzelm@21186
  1228
\endisadelimproof
wenzelm@21186
  1229
\isanewline
haftmann@21348
  1230
%
haftmann@21348
  1231
\isadelimtt
wenzelm@21186
  1232
\isanewline
haftmann@21348
  1233
%
haftmann@21348
  1234
\endisadelimtt
haftmann@21348
  1235
%
haftmann@21348
  1236
\isatagtt
wenzelm@21186
  1237
\isacommand{code{\isacharunderscore}type}\isamarkupfalse%
wenzelm@21186
  1238
\ bar\isanewline
wenzelm@21186
  1239
\ \ {\isacharparenleft}Haskell\ {\isachardoublequoteopen}Integer{\isachardoublequoteclose}{\isacharparenright}%
haftmann@21348
  1240
\endisatagtt
haftmann@21348
  1241
{\isafoldtt}%
haftmann@21348
  1242
%
haftmann@21348
  1243
\isadelimtt
haftmann@21348
  1244
%
haftmann@21348
  1245
\endisadelimtt
haftmann@21348
  1246
%
wenzelm@21186
  1247
\begin{isamarkuptext}%
wenzelm@21186
  1248
The code generator would produce
haftmann@22188
  1249
  an additional instance, which of course is rejected.
haftmann@22188
  1250
  To suppress this additional instance, use
haftmann@22188
  1251
  \isa{{\isasymCODEINSTANCE}}:%
wenzelm@21186
  1252
\end{isamarkuptext}%
wenzelm@21186
  1253
\isamarkuptrue%
haftmann@21348
  1254
%
haftmann@21348
  1255
\isadelimtt
haftmann@21348
  1256
%
haftmann@21348
  1257
\endisadelimtt
haftmann@21348
  1258
%
haftmann@21348
  1259
\isatagtt
wenzelm@21186
  1260
\isacommand{code{\isacharunderscore}instance}\isamarkupfalse%
wenzelm@21186
  1261
\ bar\ {\isacharcolon}{\isacharcolon}\ eq\isanewline
wenzelm@21186
  1262
\ \ {\isacharparenleft}Haskell\ {\isacharminus}{\isacharparenright}%
haftmann@21348
  1263
\endisatagtt
haftmann@21348
  1264
{\isafoldtt}%
haftmann@21348
  1265
%
haftmann@21348
  1266
\isadelimtt
haftmann@21348
  1267
%
haftmann@21348
  1268
\endisadelimtt
haftmann@21348
  1269
%
haftmann@22798
  1270
\isamarkupsubsubsection{Pretty printing%
wenzelm@21186
  1271
}
wenzelm@21186
  1272
\isamarkuptrue%
wenzelm@21186
  1273
%
wenzelm@21186
  1274
\begin{isamarkuptext}%
haftmann@22798
  1275
The serializer provides ML interfaces to set up
haftmann@22798
  1276
  pretty serializations for expressions like lists, numerals
haftmann@22798
  1277
  and characters;  these are
haftmann@22798
  1278
  monolithic stubs and should only be used with the
haftmann@22798
  1279
  theories introduces in \secref{sec:library}.%
haftmann@21348
  1280
\end{isamarkuptext}%
haftmann@21348
  1281
\isamarkuptrue%
haftmann@21348
  1282
%
haftmann@22550
  1283
\isamarkupsubsection{Constructor sets for datatypes%
haftmann@22550
  1284
}
haftmann@22550
  1285
\isamarkuptrue%
haftmann@22550
  1286
%
haftmann@22550
  1287
\begin{isamarkuptext}%
haftmann@22798
  1288
Conceptually, any datatype is spanned by a set of
haftmann@22798
  1289
  \emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n}
haftmann@22798
  1290
  where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all}
haftmann@22798
  1291
  type variables in \isa{{\isasymtau}}.  The HOL datatype package
haftmann@22798
  1292
  by default registers any new datatype in the table
haftmann@22798
  1293
  of datatypes, which may be inspected using
haftmann@22798
  1294
  the \isa{{\isasymPRINTCODESETUP}} command.
haftmann@22798
  1295
haftmann@22798
  1296
  In some cases, it may be convenient to alter or
haftmann@22798
  1297
  extend this table;  as an example, we show
haftmann@22798
  1298
  how to implement finite sets by lists
haftmann@22798
  1299
  using the conversion \isa{{\isachardoublequote}set\ {\isasymColon}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ set{\isachardoublequote}}
haftmann@22798
  1300
  as constructor:%
haftmann@22798
  1301
\end{isamarkuptext}%
haftmann@22798
  1302
\isamarkuptrue%
haftmann@22798
  1303
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@22798
  1304
\ set\isanewline
haftmann@22798
  1305
\isanewline
haftmann@22798
  1306
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
  1307
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}\ {\isacharequal}\ set\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}%
haftmann@22798
  1308
\isadelimproof
haftmann@22798
  1309
\ %
haftmann@22798
  1310
\endisadelimproof
haftmann@22798
  1311
%
haftmann@22798
  1312
\isatagproof
haftmann@22798
  1313
\isacommand{by}\isamarkupfalse%
haftmann@22798
  1314
\ simp%
haftmann@22798
  1315
\endisatagproof
haftmann@22798
  1316
{\isafoldproof}%
haftmann@22798
  1317
%
haftmann@22798
  1318
\isadelimproof
haftmann@22798
  1319
%
haftmann@22798
  1320
\endisadelimproof
haftmann@22798
  1321
\isanewline
haftmann@22798
  1322
\isanewline
haftmann@22798
  1323
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
  1324
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}insert\ x\ {\isacharparenleft}set\ xs{\isacharparenright}\ {\isacharequal}\ set\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}{\isachardoublequoteclose}%
haftmann@22798
  1325
\isadelimproof
haftmann@22798
  1326
\ %
haftmann@22798
  1327
\endisadelimproof
haftmann@22798
  1328
%
haftmann@22798
  1329
\isatagproof
haftmann@22798
  1330
\isacommand{by}\isamarkupfalse%
haftmann@22798
  1331
\ simp%
haftmann@22798
  1332
\endisatagproof
haftmann@22798
  1333
{\isafoldproof}%
haftmann@22798
  1334
%
haftmann@22798
  1335
\isadelimproof
haftmann@22798
  1336
%
haftmann@22798
  1337
\endisadelimproof
haftmann@22798
  1338
\isanewline
haftmann@22798
  1339
\isanewline
haftmann@22798
  1340
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
  1341
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}%
haftmann@22798
  1342
\isadelimproof
haftmann@22798
  1343
\ %
haftmann@22798
  1344
\endisadelimproof
haftmann@22798
  1345
%
haftmann@22798
  1346
\isatagproof
haftmann@22798
  1347
\isacommand{by}\isamarkupfalse%
haftmann@22798
  1348
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@22798
  1349
\endisatagproof
haftmann@22798
  1350
{\isafoldproof}%
haftmann@22798
  1351
%
haftmann@22798
  1352
\isadelimproof
haftmann@22798
  1353
%
haftmann@22798
  1354
\endisadelimproof
haftmann@22798
  1355
\isanewline
haftmann@22798
  1356
\isanewline
haftmann@22798
  1357
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
  1358
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}xs\ {\isasymunion}\ set\ ys\ {\isacharequal}\ foldr\ insert\ ys\ xs{\isachardoublequoteclose}%
haftmann@22798
  1359
\isadelimproof
haftmann@22798
  1360
\ %
haftmann@22798
  1361
\endisadelimproof
haftmann@22798
  1362
%
haftmann@22798
  1363
\isatagproof
haftmann@22798
  1364
\isacommand{by}\isamarkupfalse%
haftmann@22798
  1365
\ {\isacharparenleft}induct\ ys{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@22798
  1366
\endisatagproof
haftmann@22798
  1367
{\isafoldproof}%
haftmann@22798
  1368
%
haftmann@22798
  1369
\isadelimproof
haftmann@22798
  1370
%
haftmann@22798
  1371
\endisadelimproof
haftmann@22798
  1372
\isanewline
haftmann@22798
  1373
\isanewline
haftmann@22798
  1374
\isacommand{lemma}\isamarkupfalse%
haftmann@22798
  1375
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isasymUnion}set\ xs\ {\isacharequal}\ foldr\ {\isacharparenleft}op\ {\isasymunion}{\isacharparenright}\ xs\ {\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}%
haftmann@22798
  1376
\isadelimproof
haftmann@22798
  1377
\ %
haftmann@22798
  1378
\endisadelimproof
haftmann@22798
  1379
%
haftmann@22798
  1380
\isatagproof
haftmann@22798
  1381
\isacommand{by}\isamarkupfalse%
haftmann@22798
  1382
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@22798
  1383
\endisatagproof
haftmann@22798
  1384
{\isafoldproof}%
haftmann@22798
  1385
%
haftmann@22798
  1386
\isadelimproof
haftmann@22798
  1387
%
haftmann@22798
  1388
\endisadelimproof
haftmann@22798
  1389
\isanewline
haftmann@22798
  1390
\isanewline
haftmann@22798
  1391
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
  1392
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}\ insert\ {\isachardoublequoteopen}op\ {\isasymin}{\isachardoublequoteclose}\ {\isachardoublequoteopen}op\ {\isasymunion}{\isachardoublequoteclose}\ {\isachardoublequoteopen}Union{\isachardoublequoteclose}\ \ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}set{\isacharunderscore}list{\isachardot}ML{\isachardoublequoteclose}%
haftmann@22798
  1393
\begin{isamarkuptext}%
haftmann@22798
  1394
\lstsml{Thy/examples/set_list.ML}
haftmann@22798
  1395
haftmann@22798
  1396
  \medskip
haftmann@22798
  1397
haftmann@22798
  1398
  Changing the representation of existing datatypes requires
haftmann@22798
  1399
  some care with respect to pattern matching and such.%
haftmann@22550
  1400
\end{isamarkuptext}%
haftmann@22550
  1401
\isamarkuptrue%
haftmann@22550
  1402
%
haftmann@21348
  1403
\isamarkupsubsection{Cyclic module dependencies%
haftmann@21348
  1404
}
haftmann@21348
  1405
\isamarkuptrue%
haftmann@21348
  1406
%
haftmann@21348
  1407
\begin{isamarkuptext}%
haftmann@21348
  1408
Sometimes the awkward situation occurs that dependencies
haftmann@21348
  1409
  between definitions introduce cyclic dependencies
haftmann@21348
  1410
  between modules, which in the Haskell world leaves
haftmann@21348
  1411
  you to the mercy of the Haskell implementation you are using,
haftmann@21348
  1412
  while for SML code generation is not possible.
haftmann@21348
  1413
haftmann@21348
  1414
  A solution is to declare module names explicitly.
haftmann@21348
  1415
  Let use assume the three cyclically dependent
haftmann@21348
  1416
  modules are named \emph{A}, \emph{B} and \emph{C}.
haftmann@21348
  1417
  Then, by stating%
haftmann@21348
  1418
\end{isamarkuptext}%
haftmann@21348
  1419
\isamarkuptrue%
haftmann@21348
  1420
\isacommand{code{\isacharunderscore}modulename}\isamarkupfalse%
haftmann@21348
  1421
\ SML\isanewline
haftmann@21348
  1422
\ \ A\ ABC\isanewline
haftmann@21348
  1423
\ \ B\ ABC\isanewline
haftmann@21348
  1424
\ \ C\ ABC%
haftmann@21348
  1425
\begin{isamarkuptext}%
haftmann@21348
  1426
we explicitly map all those modules on \emph{ABC},
haftmann@21348
  1427
  resulting in an ad-hoc merge of this three modules
haftmann@21348
  1428
  at serialization time.%
haftmann@21348
  1429
\end{isamarkuptext}%
haftmann@21348
  1430
\isamarkuptrue%
haftmann@21348
  1431
%
haftmann@22798
  1432
\isamarkupsubsection{Incremental code generation%
haftmann@22798
  1433
}
haftmann@22798
  1434
\isamarkuptrue%
haftmann@22798
  1435
%
haftmann@22798
  1436
\begin{isamarkuptext}%
haftmann@22798
  1437
Code generation is \emph{incremental}: theorems
haftmann@22798
  1438
  and abstract intermediate code are cached and extended on demand.
haftmann@22798
  1439
  The cache may be partially or fully dropped if the underlying
haftmann@22798
  1440
  executable content of the theory changes.
haftmann@22798
  1441
  Implementation of caching is supposed to transparently
haftmann@22798
  1442
  hid away the details from the user.  Anyway, caching
haftmann@22798
  1443
  reaches the surface by using a slightly more general form
haftmann@22798
  1444
  of the \isa{{\isasymCODETHMS}}, \isa{{\isasymCODEDEPS}}
haftmann@22798
  1445
  and \isa{{\isasymCODEGEN}} commands: the list of constants
haftmann@22798
  1446
  may be omitted.  Then, all constants with code theorems
haftmann@22798
  1447
  in the current cache are referred to.%
haftmann@22798
  1448
\end{isamarkuptext}%
haftmann@22798
  1449
\isamarkuptrue%
haftmann@22798
  1450
%
wenzelm@21172
  1451
\isamarkupsubsection{Axiomatic extensions%
wenzelm@21172
  1452
}
wenzelm@21172
  1453
\isamarkuptrue%
wenzelm@21172
  1454
%
wenzelm@21172
  1455
\begin{isamarkuptext}%
wenzelm@21172
  1456
\begin{warn}
wenzelm@21172
  1457
    The extensions introduced in this section, though working
haftmann@21348
  1458
    in practice, are not the cream of the crop, as you
haftmann@21348
  1459
    will notice during reading.  They will
wenzelm@21172
  1460
    eventually be replaced by more mature approaches.
haftmann@21348
  1461
  \end{warn}
haftmann@21348
  1462
haftmann@21348
  1463
  Sometimes equalities are taken for granted which are
haftmann@21348
  1464
  not derivable inside the HOL logic but are silently assumed
haftmann@21348
  1465
  to hold for executable code.  For example, we may want
haftmann@21348
  1466
  to identify the famous HOL constant \isa{arbitrary}
haftmann@21348
  1467
  of type \isa{{\isacharprime}a\ option} with \isa{None}.
haftmann@21348
  1468
  By brute force:%
haftmann@21348
  1469
\end{isamarkuptext}%
haftmann@21348
  1470
\isamarkuptrue%
haftmann@21348
  1471
\isacommand{axiomatization}\isamarkupfalse%
haftmann@21348
  1472
\ \isakeyword{where}\isanewline
haftmann@21348
  1473
\ \ {\isachardoublequoteopen}arbitrary\ {\isacharequal}\ None{\isachardoublequoteclose}%
haftmann@21348
  1474
\begin{isamarkuptext}%
haftmann@21348
  1475
However this has to be considered harmful since this axiom,
haftmann@21348
  1476
  though probably justifiable for generated code, could
haftmann@21348
  1477
  introduce serious inconsistencies into the logic.
haftmann@21348
  1478
haftmann@21348
  1479
  So, there is a distinguished construct for stating axiomatic
haftmann@21348
  1480
  equalities of constants which apply only for code generation.
haftmann@21348
  1481
  Before introducing this, here is a convenient place to describe
haftmann@21348
  1482
  shortly how to deal with some restrictions the type discipline
haftmann@21348
  1483
  imposes.
haftmann@21348
  1484
haftmann@21348
  1485
  By itself, the constant \isa{arbitrary} is a non-overloaded
haftmann@21348
  1486
  polymorphic constant.  So, there is no way to distinguish
haftmann@21348
  1487
  different versions of \isa{arbitrary} for different types
haftmann@21348
  1488
  inside the code generator framework.  However, inlining
haftmann@21348
  1489
  theorems together with auxiliary constants provide a solution:%
haftmann@21348
  1490
\end{isamarkuptext}%
haftmann@21348
  1491
\isamarkuptrue%
haftmann@21348
  1492
\isacommand{definition}\isamarkupfalse%
haftmann@21348
  1493
\isanewline
haftmann@21993
  1494
\ \ arbitrary{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@21348
  1495
\ \ {\isacharbrackleft}symmetric{\isacharcomma}\ code\ inline{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}arbitrary{\isacharunderscore}option\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}%
haftmann@21348
  1496
\begin{isamarkuptext}%
haftmann@21348
  1497
By that, we replace any \isa{arbitrary} with option type
haftmann@22060
  1498
  by \isa{arbitrary{\isacharunderscore}option} in defining equations.
haftmann@21348
  1499
haftmann@21348
  1500
  For technical reasons, we further have to provide a
haftmann@21348
  1501
  synonym for \isa{None} which in code generator view
haftmann@22188
  1502
  is a function rather than a term constructor:%
haftmann@21348
  1503
\end{isamarkuptext}%
haftmann@21348
  1504
\isamarkuptrue%
haftmann@21348
  1505
\isacommand{definition}\isamarkupfalse%
haftmann@21348
  1506
\isanewline
haftmann@21348
  1507
\ \ {\isachardoublequoteopen}None{\isacharprime}\ {\isacharequal}\ None{\isachardoublequoteclose}%
haftmann@21348
  1508
\begin{isamarkuptext}%
haftmann@21348
  1509
Then finally we are enabled to use \isa{{\isasymCODEAXIOMS}}:%
haftmann@21348
  1510
\end{isamarkuptext}%
haftmann@21348
  1511
\isamarkuptrue%
haftmann@21348
  1512
\isacommand{code{\isacharunderscore}axioms}\isamarkupfalse%
haftmann@21348
  1513
\isanewline
haftmann@21348
  1514
\ \ arbitrary{\isacharunderscore}option\ {\isasymequiv}\ None{\isacharprime}%
haftmann@21348
  1515
\begin{isamarkuptext}%
haftmann@21348
  1516
A dummy example:%
haftmann@21348
  1517
\end{isamarkuptext}%
haftmann@21348
  1518
\isamarkuptrue%
haftmann@21348
  1519
\isacommand{fun}\isamarkupfalse%
haftmann@21348
  1520
\isanewline
haftmann@21348
  1521
\ \ dummy{\isacharunderscore}option\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ option{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@22479
  1522
\ \ \ \ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ Some\ x{\isachardoublequoteclose}\isanewline
haftmann@22479
  1523
\ \ {\isacharbar}\ {\isachardoublequoteopen}dummy{\isacharunderscore}option\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ arbitrary{\isachardoublequoteclose}\isanewline
haftmann@22479
  1524
\isanewline
haftmann@21348
  1525
\isacommand{code{\isacharunderscore}gen}\isamarkupfalse%
haftmann@22845
  1526
\ dummy{\isacharunderscore}option\ \isakeyword{in}\ SML\ \isakeyword{file}\ {\isachardoublequoteopen}examples{\isacharslash}arbitrary{\isachardot}ML{\isachardoublequoteclose}%
haftmann@21348
  1527
\begin{isamarkuptext}%
haftmann@21348
  1528
\lstsml{Thy/examples/arbitrary.ML}
haftmann@21348
  1529
haftmann@22798
  1530
  \medskip
haftmann@22798
  1531
haftmann@21348
  1532
  Another axiomatic extension is code generation
haftmann@21348
  1533
  for abstracted types.  For this, the  
haftmann@21452
  1534
  \isa{ExecutableRat} (see \secref{exec_rat})
haftmann@21348
  1535
  forms a good example.%
wenzelm@21172
  1536
\end{isamarkuptext}%
wenzelm@21172
  1537
\isamarkuptrue%
wenzelm@21172
  1538
%
wenzelm@21172
  1539
\isamarkupsection{ML interfaces \label{sec:ml}%
wenzelm@21172
  1540
}
wenzelm@21172
  1541
\isamarkuptrue%
wenzelm@21172
  1542
%
haftmann@21348
  1543
\begin{isamarkuptext}%
haftmann@21348
  1544
Since the code generator framework not only aims to provide
haftmann@21348
  1545
  a nice Isar interface but also to form a base for
haftmann@21348
  1546
  code-generation-based applications, here a short
haftmann@21348
  1547
  description of the most important ML interfaces.%
haftmann@21348
  1548
\end{isamarkuptext}%
haftmann@21348
  1549
\isamarkuptrue%
haftmann@21348
  1550
%
wenzelm@21172
  1551
\isamarkupsubsection{Constants with type discipline: codegen\_consts.ML%
wenzelm@21172
  1552
}
wenzelm@21172
  1553
\isamarkuptrue%
wenzelm@21172
  1554
%
haftmann@21348
  1555
\begin{isamarkuptext}%
haftmann@21348
  1556
This Pure module manages identification of (probably overloaded)
haftmann@21348
  1557
  constants by unique identifiers.%
haftmann@21348
  1558
\end{isamarkuptext}%
haftmann@21348
  1559
\isamarkuptrue%
haftmann@21348
  1560
%
wenzelm@21172
  1561
\isadelimmlref
wenzelm@21172
  1562
%
wenzelm@21172
  1563
\endisadelimmlref
wenzelm@21172
  1564
%
wenzelm@21172
  1565
\isatagmlref
wenzelm@21172
  1566
%
wenzelm@21172
  1567
\begin{isamarkuptext}%
wenzelm@21172
  1568
\begin{mldecls}
haftmann@22550
  1569
  \indexmltype{CodegenConsts.const}\verb|type CodegenConsts.const = string * string option| \\
haftmann@22550
  1570
  \indexml{CodegenConsts.const-of-cexpr}\verb|CodegenConsts.const_of_cexpr: theory -> string * typ -> CodegenConsts.const| \\
haftmann@21348
  1571
 \end{mldecls}
haftmann@21348
  1572
haftmann@21348
  1573
  \begin{description}
haftmann@21348
  1574
haftmann@21348
  1575
  \item \verb|CodegenConsts.const| is the identifier type:
haftmann@21348
  1576
     the product of a \emph{string} with a list of \emph{typs}.
haftmann@21348
  1577
     The \emph{string} is the constant name as represented inside Isabelle;
haftmann@22550
  1578
     for overloaded constants, the attached \emph{string option}
haftmann@22550
  1579
     is either \isa{SOME} type constructor denoting an instance,
haftmann@22550
  1580
     or \isa{NONE} for the polymorphic constant.
haftmann@21348
  1581
haftmann@22550
  1582
  \item \verb|CodegenConsts.const_of_cexpr|~\isa{thy}~\isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}}
haftmann@22550
  1583
     maps a constant expression \isa{{\isacharparenleft}constname{\isacharcomma}\ typ{\isacharparenright}}
haftmann@22550
  1584
     to its canonical identifier.
haftmann@21348
  1585
haftmann@21348
  1586
  \end{description}%
wenzelm@21172
  1587
\end{isamarkuptext}%
wenzelm@21172
  1588
\isamarkuptrue%
wenzelm@21172
  1589
%
wenzelm@21172
  1590
\endisatagmlref
wenzelm@21172
  1591
{\isafoldmlref}%
wenzelm@21172
  1592
%
wenzelm@21172
  1593
\isadelimmlref
wenzelm@21172
  1594
%
wenzelm@21172
  1595
\endisadelimmlref
wenzelm@21172
  1596
%
wenzelm@21172
  1597
\isamarkupsubsection{Executable theory content: codegen\_data.ML%
wenzelm@21172
  1598
}
wenzelm@21172
  1599
\isamarkuptrue%
wenzelm@21172
  1600
%
wenzelm@21172
  1601
\begin{isamarkuptext}%
wenzelm@21172
  1602
This Pure module implements the core notions of
wenzelm@21172
  1603
  executable content of a theory.%
wenzelm@21172
  1604
\end{isamarkuptext}%
wenzelm@21172
  1605
\isamarkuptrue%
wenzelm@21172
  1606
%
wenzelm@21172
  1607
\isamarkupsubsubsection{Suspended theorems%
wenzelm@21172
  1608
}
wenzelm@21172
  1609
\isamarkuptrue%
wenzelm@21172
  1610
%
wenzelm@21172
  1611
\isadelimmlref
wenzelm@21172
  1612
%
wenzelm@21172
  1613
\endisadelimmlref
wenzelm@21172
  1614
%
wenzelm@21172
  1615
\isatagmlref
wenzelm@21172
  1616
%
wenzelm@21172
  1617
\begin{isamarkuptext}%
wenzelm@21172
  1618
\begin{mldecls}
haftmann@22751
  1619
  \indexml{CodegenData.lazy-thms}\verb|CodegenData.lazy_thms: (unit -> thm list) -> thm list Susp.T|
haftmann@21348
  1620
  \end{mldecls}
haftmann@21348
  1621
haftmann@21348
  1622
  \begin{description}
haftmann@21348
  1623
haftmann@22751
  1624
  \item \verb|CodegenData.lazy_thms|~\isa{f} turns an abstract
haftmann@21348
  1625
     theorem computation \isa{f} into a suspension of theorems.
haftmann@21348
  1626
haftmann@21348
  1627
  \end{description}%
wenzelm@21172
  1628
\end{isamarkuptext}%
wenzelm@21172
  1629
\isamarkuptrue%
wenzelm@21172
  1630
%
wenzelm@21172
  1631
\endisatagmlref
wenzelm@21172
  1632
{\isafoldmlref}%
wenzelm@21172
  1633
%
wenzelm@21172
  1634
\isadelimmlref
wenzelm@21172
  1635
%
wenzelm@21172
  1636
\endisadelimmlref
wenzelm@21172
  1637
%
haftmann@22292
  1638
\isamarkupsubsubsection{Managing executable content%
wenzelm@21172
  1639
}
wenzelm@21172
  1640
\isamarkuptrue%
wenzelm@21172
  1641
%
wenzelm@21172
  1642
\isadelimmlref
wenzelm@21172
  1643
%
wenzelm@21172
  1644
\endisadelimmlref
wenzelm@21172
  1645
%
wenzelm@21172
  1646
\isatagmlref
wenzelm@21172
  1647
%
wenzelm@21172
  1648
\begin{isamarkuptext}%
wenzelm@21172
  1649
\begin{mldecls}
haftmann@22550
  1650
  \indexml{CodegenData.add-func}\verb|CodegenData.add_func: bool -> thm -> theory -> theory| \\
wenzelm@21172
  1651
  \indexml{CodegenData.del-func}\verb|CodegenData.del_func: thm -> theory -> theory| \\
haftmann@21348
  1652
  \indexml{CodegenData.add-funcl}\verb|CodegenData.add_funcl: CodegenConsts.const * thm list Susp.T -> theory -> theory| \\
wenzelm@21172
  1653
  \indexml{CodegenData.add-inline}\verb|CodegenData.add_inline: thm -> theory -> theory| \\
wenzelm@21172
  1654
  \indexml{CodegenData.del-inline}\verb|CodegenData.del_inline: thm -> theory -> theory| \\
haftmann@22060
  1655
  \indexml{CodegenData.add-inline-proc}\verb|CodegenData.add_inline_proc: string * (theory -> cterm list -> thm list)|\isasep\isanewline%
haftmann@21348
  1656
\verb|    -> theory -> theory| \\
haftmann@22060
  1657
  \indexml{CodegenData.del-inline-proc}\verb|CodegenData.del_inline_proc: string -> theory -> theory| \\
haftmann@22060
  1658
  \indexml{CodegenData.add-preproc}\verb|CodegenData.add_preproc: string * (theory -> thm list -> thm list)|\isasep\isanewline%
haftmann@21348
  1659
\verb|    -> theory -> theory| \\
haftmann@22060
  1660
  \indexml{CodegenData.del-preproc}\verb|CodegenData.del_preproc: string -> theory -> theory| \\
haftmann@22479
  1661
  \indexml{CodegenData.add-datatype}\verb|CodegenData.add_datatype: string * ((string * sort) list * (string * typ list) list)|\isasep\isanewline%
haftmann@22479
  1662
\verb|    -> theory -> theory| \\
haftmann@21348
  1663
  \indexml{CodegenData.get-datatype}\verb|CodegenData.get_datatype: theory -> string|\isasep\isanewline%
haftmann@22479
  1664
\verb|    -> (string * sort) list * (string * typ list) list| \\
wenzelm@21172
  1665
  \indexml{CodegenData.get-datatype-of-constr}\verb|CodegenData.get_datatype_of_constr: theory -> CodegenConsts.const -> string option|
wenzelm@21172
  1666
  \end{mldecls}
wenzelm@21172
  1667
wenzelm@21172
  1668
  \begin{description}
wenzelm@21172
  1669
haftmann@21348
  1670
  \item \verb|CodegenData.add_func|~\isa{thm}~\isa{thy} adds function
haftmann@21348
  1671
     theorem \isa{thm} to executable content.
haftmann@21348
  1672
haftmann@21348
  1673
  \item \verb|CodegenData.del_func|~\isa{thm}~\isa{thy} removes function
haftmann@21348
  1674
     theorem \isa{thm} from executable content, if present.
haftmann@21348
  1675
haftmann@21348
  1676
  \item \verb|CodegenData.add_funcl|~\isa{{\isacharparenleft}const{\isacharcomma}\ lthms{\isacharparenright}}~\isa{thy} adds
haftmann@22060
  1677
     suspended defining equations \isa{lthms} for constant
haftmann@21348
  1678
     \isa{const} to executable content.
haftmann@21348
  1679
haftmann@21348
  1680
  \item \verb|CodegenData.add_inline|~\isa{thm}~\isa{thy} adds
haftmann@21348
  1681
     inlining theorem \isa{thm} to executable content.
haftmann@21348
  1682
haftmann@21348
  1683
  \item \verb|CodegenData.del_inline|~\isa{thm}~\isa{thy} remove
haftmann@21348
  1684
     inlining theorem \isa{thm} from executable content, if present.
haftmann@21348
  1685
haftmann@22060
  1686
  \item \verb|CodegenData.add_inline_proc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
haftmann@22060
  1687
     inline procedure \isa{f} (named \isa{name}) to executable content;
haftmann@21348
  1688
     \isa{f} is a computation of rewrite rules dependent on
haftmann@21348
  1689
     the current theory context and the list of all arguments
haftmann@22060
  1690
     and right hand sides of the defining equations belonging
haftmann@21348
  1691
     to a certain function definition.
haftmann@21348
  1692
haftmann@22060
  1693
  \item \verb|CodegenData.del_inline_proc|~\isa{name}~\isa{thy} removes
haftmann@22060
  1694
     inline procedure named \isa{name} from executable content.
haftmann@22060
  1695
haftmann@22060
  1696
  \item \verb|CodegenData.add_preproc|~\isa{{\isacharparenleft}name{\isacharcomma}\ f{\isacharparenright}}~\isa{thy} adds
haftmann@22060
  1697
     generic preprocessor \isa{f} (named \isa{name}) to executable content;
haftmann@22060
  1698
     \isa{f} is a transformation of the defining equations belonging
haftmann@21348
  1699
     to a certain function definition, depending on the
haftmann@21348
  1700
     current theory context.
haftmann@21348
  1701
haftmann@22060
  1702
  \item \verb|CodegenData.del_preproc|~\isa{name}~\isa{thy} removes
haftmann@22060
  1703
     generic preprcoessor named \isa{name} from executable content.
haftmann@22060
  1704
haftmann@22479
  1705
  \item \verb|CodegenData.add_datatype|~\isa{{\isacharparenleft}name{\isacharcomma}\ spec{\isacharparenright}}~\isa{thy} adds
haftmann@21348
  1706
     a datatype to executable content, with type constructor
haftmann@21348
  1707
     \isa{name} and specification \isa{spec}; \isa{spec} is
haftmann@21348
  1708
     a pair consisting of a list of type variable with sort
haftmann@21348
  1709
     constraints and a list of constructors with name
haftmann@22479
  1710
     and types of arguments.
haftmann@21348
  1711
haftmann@21348
  1712
  \item \verb|CodegenData.get_datatype_of_constr|~\isa{thy}~\isa{const}
haftmann@21348
  1713
     returns type constructor corresponding to
haftmann@21348
  1714
     constructor \isa{const}; returns \isa{NONE}
haftmann@21348
  1715
     if \isa{const} is no constructor.
haftmann@21348
  1716
haftmann@21348
  1717
  \end{description}%
haftmann@21348
  1718
\end{isamarkuptext}%
haftmann@21348
  1719
\isamarkuptrue%
haftmann@21348
  1720
%
haftmann@21348
  1721
\endisatagmlref
haftmann@21348
  1722
{\isafoldmlref}%
haftmann@21348
  1723
%
haftmann@21348
  1724
\isadelimmlref
haftmann@21348
  1725
%
haftmann@21348
  1726
\endisadelimmlref
haftmann@21348
  1727
%
haftmann@22292
  1728
\isamarkupsubsection{Auxiliary%
wenzelm@21172
  1729
}
wenzelm@21172
  1730
\isamarkuptrue%
wenzelm@21172
  1731
%
wenzelm@21172
  1732
\isadelimmlref
wenzelm@21172
  1733
%
wenzelm@21172
  1734
\endisadelimmlref
wenzelm@21172
  1735
%
wenzelm@21172
  1736
\isatagmlref
wenzelm@21172
  1737
%
wenzelm@21172
  1738
\begin{isamarkuptext}%
wenzelm@21172
  1739
\begin{mldecls}
wenzelm@21172
  1740
  \indexml{CodegenConsts.const-ord}\verb|CodegenConsts.const_ord: CodegenConsts.const * CodegenConsts.const -> order| \\
wenzelm@21172
  1741
  \indexml{CodegenConsts.eq-const}\verb|CodegenConsts.eq_const: CodegenConsts.const * CodegenConsts.const -> bool| \\
wenzelm@21172
  1742
  \indexml{CodegenConsts.read-const}\verb|CodegenConsts.read_const: theory -> string -> CodegenConsts.const| \\
wenzelm@21172
  1743
  \indexmlstructure{CodegenConsts.Consttab}\verb|structure CodegenConsts.Consttab| \\
haftmann@22751
  1744
  \indexml{CodegenFunc.head-func}\verb|CodegenFunc.head_func: thm -> CodegenConsts.const * typ| \\
haftmann@22060
  1745
  \indexml{CodegenFunc.rewrite-func}\verb|CodegenFunc.rewrite_func: thm list -> thm -> thm| \\
haftmann@21348
  1746
  \end{mldecls}
haftmann@21348
  1747
haftmann@21348
  1748
  \begin{description}
haftmann@21348
  1749
haftmann@21348
  1750
  \item \verb|CodegenConsts.const_ord|,~\verb|CodegenConsts.eq_const|
haftmann@21348
  1751
     provide order and equality on constant identifiers.
haftmann@21348
  1752
haftmann@22292
  1753
  \item \verb|CodegenConsts.Consttab|
haftmann@22292
  1754
     provides table structures with constant identifiers as keys.
haftmann@21348
  1755
haftmann@21348
  1756
  \item \verb|CodegenConsts.read_const|~\isa{thy}~\isa{s}
haftmann@21348
  1757
     reads a constant as a concrete term expression \isa{s}.
haftmann@21348
  1758
haftmann@22751
  1759
  \item \verb|CodegenFunc.head_func|~\isa{thm}
haftmann@22751
  1760
     extracts the constant and its type from a defining equation \isa{thm}.
haftmann@21348
  1761
haftmann@22060
  1762
  \item \verb|CodegenFunc.rewrite_func|~\isa{rews}~\isa{thm}
haftmann@22060
  1763
     rewrites a defining equation \isa{thm} with a set of rewrite
haftmann@21348
  1764
     rules \isa{rews}; only arguments and right hand side are rewritten,
haftmann@22060
  1765
     not the head of the defining equation.
haftmann@21348
  1766
haftmann@21348
  1767
  \end{description}%
wenzelm@21172
  1768
\end{isamarkuptext}%
wenzelm@21172
  1769
\isamarkuptrue%
wenzelm@21172
  1770
%
wenzelm@21172
  1771
\endisatagmlref
wenzelm@21172
  1772
{\isafoldmlref}%
wenzelm@21172
  1773
%
wenzelm@21172
  1774
\isadelimmlref
wenzelm@21172
  1775
%
wenzelm@21172
  1776
\endisadelimmlref
wenzelm@21172
  1777
%
haftmann@20967
  1778
\isamarkupsubsection{Implementing code generator applications%
haftmann@20967
  1779
}
haftmann@20967
  1780
\isamarkuptrue%
haftmann@20967
  1781
%
wenzelm@21172
  1782
\begin{isamarkuptext}%
haftmann@21348
  1783
Implementing code generator applications on top
haftmann@21348
  1784
  of the framework set out so far usually not only
haftmann@21348
  1785
  involves using those primitive interfaces
haftmann@21348
  1786
  but also storing code-dependent data and various
haftmann@21348
  1787
  other things.
haftmann@21348
  1788
haftmann@21348
  1789
  \begin{warn}
wenzelm@21172
  1790
    Some interfaces discussed here have not reached
wenzelm@21172
  1791
    a final state yet.
wenzelm@21172
  1792
    Changes likely to occur in future.
haftmann@21452
  1793
  \end{warn}%
wenzelm@21172
  1794
\end{isamarkuptext}%
wenzelm@21172
  1795
\isamarkuptrue%
wenzelm@21172
  1796
%
wenzelm@21172
  1797
\isamarkupsubsubsection{Data depending on the theory's executable content%
wenzelm@21172
  1798
}
wenzelm@21172
  1799
\isamarkuptrue%
wenzelm@21172
  1800
%
haftmann@21348
  1801
\begin{isamarkuptext}%
haftmann@21452
  1802
Due to incrementality of code generation, changes in the
haftmann@21452
  1803
  theory's executable content have to be propagated in a
haftmann@21452
  1804
  certain fashion.  Additionally, such changes may occur
haftmann@21452
  1805
  not only during theory extension but also during theory
haftmann@21452
  1806
  merge, which is a little bit nasty from an implementation
haftmann@21452
  1807
  point of view.  The framework provides a solution
haftmann@21452
  1808
  to this technical challenge by providing a functorial
haftmann@21452
  1809
  data slot \verb|CodeDataFun|; on instantiation
haftmann@21452
  1810
  of this functor, the following types and operations
haftmann@21452
  1811
  are required:
haftmann@21452
  1812
haftmann@21452
  1813
  \medskip
haftmann@21348
  1814
  \begin{tabular}{l}
haftmann@21348
  1815
  \isa{val\ name{\isacharcolon}\ string} \\
haftmann@21348
  1816
  \isa{type\ T} \\
haftmann@21348
  1817
  \isa{val\ empty{\isacharcolon}\ T} \\
haftmann@21348
  1818
  \isa{val\ merge{\isacharcolon}\ Pretty{\isachardot}pp\ {\isasymrightarrow}\ T\ {\isacharasterisk}\ T\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1819
  \isa{val\ purge{\isacharcolon}\ theory\ option\ {\isasymrightarrow}\ CodegenConsts{\isachardot}const\ list\ option\ {\isasymrightarrow}\ T\ {\isasymrightarrow}\ T}
haftmann@21348
  1820
  \end{tabular}
haftmann@21348
  1821
haftmann@21452
  1822
  \begin{description}
haftmann@21452
  1823
haftmann@21452
  1824
  \item \isa{name} is a system-wide unique name identifying the data.
haftmann@21452
  1825
haftmann@21452
  1826
  \item \isa{T} the type of data to store.
haftmann@21452
  1827
haftmann@21452
  1828
  \item \isa{empty} initial (empty) data.
haftmann@21452
  1829
haftmann@21452
  1830
  \item \isa{merge} merging two data slots.
haftmann@21452
  1831
haftmann@22798
  1832
  \item \isa{purge}~\isa{thy}~\isa{consts} propagates changes in executable content;
haftmann@21452
  1833
    if possible, the current theory context is handed over
haftmann@21452
  1834
    as argument \isa{thy} (if there is no current theory context (e.g.~during
haftmann@22798
  1835
    theory merge, \verb|NONE|); \isa{consts} indicates the kind
haftmann@21452
  1836
    of change: \verb|NONE| stands for a fundamental change
haftmann@22798
  1837
    which invalidates any existing code, \isa{SOME\ consts}
haftmann@22798
  1838
    hints that executable content for constants \isa{consts}
haftmann@21452
  1839
    has changed.
haftmann@21452
  1840
haftmann@21452
  1841
  \end{description}
haftmann@21452
  1842
haftmann@21452
  1843
  An instance of \verb|CodeDataFun| provides the following
haftmann@21452
  1844
  interface:
haftmann@21452
  1845
haftmann@21348
  1846
  \medskip
haftmann@21348
  1847
  \begin{tabular}{l}
haftmann@21348
  1848
  \isa{init{\isacharcolon}\ theory\ {\isasymrightarrow}\ theory} \\
haftmann@21348
  1849
  \isa{get{\isacharcolon}\ theory\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1850
  \isa{change{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ T{\isacharparenright}\ {\isasymrightarrow}\ T} \\
haftmann@21348
  1851
  \isa{change{\isacharunderscore}yield{\isacharcolon}\ theory\ {\isasymrightarrow}\ {\isacharparenleft}T\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T{\isacharparenright}\ {\isasymrightarrow}\ {\isacharprime}a\ {\isacharasterisk}\ T}
haftmann@21452
  1852
  \end{tabular}
haftmann@21452
  1853
haftmann@21452
  1854
  \begin{description}
haftmann@21452
  1855
haftmann@21452
  1856
  \item \isa{init} initialization during theory setup.
haftmann@21452
  1857
haftmann@21452
  1858
  \item \isa{get} retrieval of the current data.
haftmann@21452
  1859
haftmann@21452
  1860
  \item \isa{change} update of current data (cached!)
haftmann@21452
  1861
    by giving a continuation.
haftmann@21452
  1862
haftmann@21452
  1863
  \item \isa{change{\isacharunderscore}yield} update with side result.
haftmann@21452
  1864
haftmann@21452
  1865
  \end{description}%
haftmann@21452
  1866
\end{isamarkuptext}%
haftmann@21452
  1867
\isamarkuptrue%
haftmann@21452
  1868
%
haftmann@22798
  1869
\isamarkupsubsubsection{Building implementable systems fo defining equations%
haftmann@22798
  1870
}
haftmann@22798
  1871
\isamarkuptrue%
haftmann@22798
  1872
%
haftmann@22798
  1873
\begin{isamarkuptext}%
haftmann@22798
  1874
Out of the executable content of a theory, a normalized
haftmann@22798
  1875
  defining equation systems may be constructed containing
haftmann@22798
  1876
  function definitions for constants.  The system is cached
haftmann@22798
  1877
  until its underlying executable content changes.
haftmann@22798
  1878
haftmann@22798
  1879
  Defining equations are retrieved by instantiation
haftmann@22798
  1880
  of the functor \verb|CodegenFuncgrRetrieval|
haftmann@22798
  1881
  which takes two arguments:
haftmann@22798
  1882
haftmann@22798
  1883
  \medskip
haftmann@22798
  1884
  \begin{tabular}{l}
haftmann@22798
  1885
  \isa{val\ name{\isacharcolon}\ string} \\
haftmann@22798
  1886
  \isa{val\ rewrites{\isacharcolon}\ theory\ {\isasymrightarrow}\ thm\ list}
haftmann@22798
  1887
  \end{tabular}
haftmann@22798
  1888
haftmann@22798
  1889
  \begin{description}
haftmann@22798
  1890
haftmann@22798
  1891
  \item \isa{name} is a system-wide unique name identifying
haftmann@22798
  1892
    this particular system of defining equations.
haftmann@22798
  1893
haftmann@22798
  1894
  \item \isa{rewrites} specifies a set of theory-dependent
haftmann@22798
  1895
    rewrite rules which are added to the preprocessor setup;
haftmann@22798
  1896
    if no additional preprocessing is required, pass
haftmann@22798
  1897
    a function returning an empty list.
haftmann@22798
  1898
haftmann@22798
  1899
  \end{description}
haftmann@22798
  1900
haftmann@22798
  1901
  An instance of \verb|CodegenFuncgrRetrieval| in essence
haftmann@22798
  1902
  provides the following interface:
haftmann@22798
  1903
haftmann@22798
  1904
  \medskip
haftmann@22798
  1905
  \begin{tabular}{l}
haftmann@22798
  1906
  \isa{make{\isacharcolon}\ theory\ {\isasymrightarrow}\ CodegenConsts{\isachardot}const\ list\ {\isasymrightarrow}\ CodegenFuncgr{\isachardot}T} \\
haftmann@22798
  1907
  \end{tabular}
haftmann@22798
  1908
haftmann@22798
  1909
  \begin{description}
haftmann@22798
  1910
haftmann@22798
  1911
  \item \isa{make}~\isa{thy}~\isa{consts} returns
haftmann@22798
  1912
    a system of defining equations which is guaranteed
haftmann@22798
  1913
    to contain all defining equations for constants \isa{consts}
haftmann@22798
  1914
    including all defining equations any defining equation
haftmann@22798
  1915
    for any constant in \isa{consts} depends on.
haftmann@22798
  1916
haftmann@22798
  1917
  \end{description}
haftmann@22798
  1918
haftmann@22798
  1919
  Systems of defining equations are graphs accesible by the
haftmann@22798
  1920
  following operations:%
haftmann@22798
  1921
\end{isamarkuptext}%
haftmann@22798
  1922
\isamarkuptrue%
haftmann@22798
  1923
%
haftmann@22798
  1924
\isadelimmlref
haftmann@22798
  1925
%
haftmann@22798
  1926
\endisadelimmlref
haftmann@22798
  1927
%
haftmann@22798
  1928
\isatagmlref
haftmann@22798
  1929
%
haftmann@22798
  1930
\begin{isamarkuptext}%
haftmann@22798
  1931
\begin{mldecls}
haftmann@22798
  1932
  \indexmltype{CodegenFuncgr.T}\verb|type CodegenFuncgr.T| \\
haftmann@22798
  1933
  \indexml{CodegenFuncgr.funcs}\verb|CodegenFuncgr.funcs: CodegenFuncgr.T -> CodegenConsts.const -> thm list| \\
haftmann@22798
  1934
  \indexml{CodegenFuncgr.typ}\verb|CodegenFuncgr.typ: CodegenFuncgr.T -> CodegenConsts.const -> typ| \\
haftmann@22798
  1935
  \indexml{CodegenFuncgr.deps}\verb|CodegenFuncgr.deps: CodegenFuncgr.T|\isasep\isanewline%
haftmann@22798
  1936
\verb|    -> CodegenConsts.const list -> CodegenConsts.const list list| \\
haftmann@22798
  1937
  \indexml{CodegenFuncgr.all}\verb|CodegenFuncgr.all: CodegenFuncgr.T -> CodegenConsts.const list|
haftmann@22798
  1938
  \end{mldecls}
haftmann@22798
  1939
haftmann@22798
  1940
  \begin{description}
haftmann@22798
  1941
haftmann@22798
  1942
  \item \verb|CodegenFuncgr.T| represents
haftmann@22798
  1943
    a normalized defining equation system.
haftmann@22798
  1944
haftmann@22798
  1945
  \item \verb|CodegenFuncgr.funcs|~\isa{funcgr}~\isa{const}
haftmann@22798
  1946
    retrieves defining equiations for constant \isa{const}.
haftmann@22798
  1947
haftmann@22798
  1948
  \item \verb|CodegenFuncgr.typ|~\isa{funcgr}~\isa{const}
haftmann@22798
  1949
    retrieves function type for constant \isa{const}.
haftmann@22798
  1950
haftmann@22798
  1951
  \item \verb|CodegenFuncgr.deps|~\isa{funcgr}~\isa{consts}
haftmann@22798
  1952
    returns the transitive closure of dependencies for
haftmann@22798
  1953
    constants \isa{consts} as a partitioning where each partition
haftmann@22798
  1954
    corresponds to a strongly connected component of
haftmann@22798
  1955
    dependencies and any partition does \emph{not}
haftmann@22798
  1956
    depend on partitions further left.
haftmann@22798
  1957
haftmann@22798
  1958
  \item \verb|CodegenFuncgr.all|~\isa{funcgr}
haftmann@22798
  1959
    returns all currently represented constants.
haftmann@22798
  1960
haftmann@22798
  1961
  \end{description}%
haftmann@22798
  1962
\end{isamarkuptext}%
haftmann@22798
  1963
\isamarkuptrue%
haftmann@22798
  1964
%
haftmann@22798
  1965
\endisatagmlref
haftmann@22798
  1966
{\isafoldmlref}%
haftmann@22798
  1967
%
haftmann@22798
  1968
\isadelimmlref
haftmann@22798
  1969
%
haftmann@22798
  1970
\endisadelimmlref
haftmann@22798
  1971
%
haftmann@21452
  1972
\isamarkupsubsubsection{Datatype hooks%
haftmann@21452
  1973
}
haftmann@21452
  1974
\isamarkuptrue%
haftmann@21452
  1975
%
haftmann@21452
  1976
\begin{isamarkuptext}%
haftmann@21452
  1977
Isabelle/HOL's datatype package provides a mechanism to
haftmann@21452
  1978
  extend theories depending on datatype declarations:
haftmann@21452
  1979
  \emph{datatype hooks}.  For example, when declaring a new
haftmann@22060
  1980
  datatype, a hook proves defining equations for equality on
haftmann@21452
  1981
  that datatype (if possible).%
haftmann@21348
  1982
\end{isamarkuptext}%
haftmann@21348
  1983
\isamarkuptrue%
haftmann@21348
  1984
%
haftmann@21348
  1985
\isadelimmlref
haftmann@21348
  1986
%
haftmann@21348
  1987
\endisadelimmlref
haftmann@21348
  1988
%
haftmann@21348
  1989
\isatagmlref
haftmann@21348
  1990
%
haftmann@21348
  1991
\begin{isamarkuptext}%
haftmann@21348
  1992
\begin{mldecls}
haftmann@21452
  1993
  \indexmltype{DatatypeHooks.hook}\verb|type DatatypeHooks.hook = string list -> theory -> theory| \\
haftmann@21452
  1994
  \indexml{DatatypeHooks.add}\verb|DatatypeHooks.add: DatatypeHooks.hook -> theory -> theory|
haftmann@21348
  1995
  \end{mldecls}
haftmann@21348
  1996
haftmann@21348
  1997
  \begin{description}
haftmann@21348
  1998
haftmann@21452
  1999
  \item \verb|DatatypeHooks.hook| specifies the interface
haftmann@21452
  2000
     of \emph{datatype hooks}: a theory update
haftmann@21452
  2001
     depending on the list of newly introduced
haftmann@21452
  2002
     datatype names.
haftmann@21452
  2003
haftmann@21452
  2004
  \item \verb|DatatypeHooks.add| adds a hook to the
haftmann@21452
  2005
     chain of all hooks.
haftmann@21348
  2006
haftmann@21348
  2007
  \end{description}%
haftmann@21348
  2008
\end{isamarkuptext}%
haftmann@21348
  2009
\isamarkuptrue%
haftmann@21348
  2010
%
haftmann@21348
  2011
\endisatagmlref
haftmann@21348
  2012
{\isafoldmlref}%
haftmann@21348
  2013
%
haftmann@21348
  2014
\isadelimmlref
haftmann@21348
  2015
%
haftmann@21348
  2016
\endisadelimmlref
haftmann@21348
  2017
%
haftmann@21452
  2018
\isamarkupsubsubsection{Trivial typedefs -- type copies%
wenzelm@21172
  2019
}
wenzelm@21172
  2020
\isamarkuptrue%
wenzelm@21172
  2021
%
haftmann@21452
  2022
\begin{isamarkuptext}%
haftmann@21452
  2023
Sometimes packages will introduce new types
haftmann@21452
  2024
  as \emph{marked type copies} similar to Haskell's
haftmann@21452
  2025
  \isa{newtype} declaration (e.g. the HOL record package)
haftmann@21452
  2026
  \emph{without} tinkering with the overhead of datatypes.
haftmann@21452
  2027
  Technically, these type copies are trivial forms of typedefs.
haftmann@21452
  2028
  Since these type copies in code generation view are nothing
haftmann@21452
  2029
  else than datatypes, they have been given a own package
haftmann@21452
  2030
  in order to faciliate code generation:%
haftmann@21452
  2031
\end{isamarkuptext}%
haftmann@21452
  2032
\isamarkuptrue%
haftmann@21452
  2033
%
haftmann@21348
  2034
\isadelimmlref
haftmann@21348
  2035
%
haftmann@21348
  2036
\endisadelimmlref
haftmann@21348
  2037
%
haftmann@21348
  2038
\isatagmlref
haftmann@21348
  2039
%
wenzelm@21172
  2040
\begin{isamarkuptext}%
haftmann@21348
  2041
\begin{mldecls}
haftmann@21452
  2042
  \indexmltype{TypecopyPackage.info}\verb|type TypecopyPackage.info| \\
haftmann@21452
  2043
  \indexml{TypecopyPackage.add-typecopy}\verb|TypecopyPackage.add_typecopy: |\isasep\isanewline%
haftmann@21452
  2044
\verb|    bstring * string list -> typ -> (bstring * bstring) option|\isasep\isanewline%
haftmann@21452
  2045
\verb|    -> theory -> (string * TypecopyPackage.info) * theory| \\
haftmann@21452
  2046
  \indexml{TypecopyPackage.get-typecopy-info}\verb|TypecopyPackage.get_typecopy_info: theory|\isasep\isanewline%
haftmann@21452
  2047
\verb|    -> string -> TypecopyPackage.info option| \\
haftmann@21452
  2048
  \indexml{TypecopyPackage.get-spec}\verb|TypecopyPackage.get_spec: theory -> string|\isasep\isanewline%
haftmann@21452
  2049
\verb|    -> (string * sort) list * (string * typ list) list| \\
haftmann@21452
  2050
  \indexmltype{TypecopyPackage.hook}\verb|type TypecopyPackage.hook = string * TypecopyPackage.info -> theory -> theory| \\
haftmann@21452
  2051
  \indexml{TypecopyPackage.add-hook}\verb|TypecopyPackage.add_hook: TypecopyPackage.hook -> theory -> theory| \\
haftmann@21452
  2052
  \end{mldecls}
haftmann@21452
  2053
haftmann@21452
  2054
  \begin{description}
haftmann@21452
  2055
haftmann@21452
  2056
  \item \verb|TypecopyPackage.info| a record containing
haftmann@21452
  2057
     the specification and further data of a type copy.
haftmann@21452
  2058
haftmann@21452
  2059
  \item \verb|TypecopyPackage.add_typecopy| defines a new
haftmann@21452
  2060
     type copy.
haftmann@21452
  2061
haftmann@21452
  2062
  \item \verb|TypecopyPackage.get_typecopy_info| retrieves
haftmann@21452
  2063
     data of an existing type copy.
haftmann@21452
  2064
haftmann@21452
  2065
  \item \verb|TypecopyPackage.get_spec| retrieves datatype-like
haftmann@21452
  2066
     specification of a type copy.
haftmann@21452
  2067
haftmann@21452
  2068
  \item \verb|TypecopyPackage.hook|,~\verb|TypecopyPackage.add_hook|
haftmann@21452
  2069
     provide a hook mechanism corresponding to the hook mechanism
haftmann@21452
  2070
     on datatypes.
haftmann@21452
  2071
haftmann@21452
  2072
  \end{description}%
haftmann@21348
  2073
\end{isamarkuptext}%
haftmann@21348
  2074
\isamarkuptrue%
haftmann@21348
  2075
%
haftmann@21452
  2076
\endisatagmlref
haftmann@21452
  2077
{\isafoldmlref}%
haftmann@21452
  2078
%
haftmann@21452
  2079
\isadelimmlref
haftmann@21452
  2080
%
haftmann@21452
  2081
\endisadelimmlref
haftmann@21452
  2082
%
haftmann@21452
  2083
\isamarkupsubsubsection{Unifying type copies and datatypes%
haftmann@21452
  2084
}
haftmann@21452
  2085
\isamarkuptrue%
haftmann@21452
  2086
%
haftmann@21348
  2087
\begin{isamarkuptext}%
haftmann@21452
  2088
Since datatypes and type copies are mapped to the same concept (datatypes)
haftmann@21452
  2089
  by code generation, the view on both is unified \qt{code types}:%
haftmann@21348
  2090
\end{isamarkuptext}%
haftmann@21348
  2091
\isamarkuptrue%
haftmann@21348
  2092
%
haftmann@21452
  2093
\isadelimmlref
haftmann@21452
  2094
%
haftmann@21452
  2095
\endisadelimmlref
haftmann@21452
  2096
%
haftmann@21452
  2097
\isatagmlref
haftmann@21452
  2098
%
haftmann@21348
  2099
\begin{isamarkuptext}%
haftmann@21348
  2100
\begin{mldecls}
haftmann@21452
  2101
  \indexmltype{DatatypeCodegen.hook}\verb|type DatatypeCodegen.hook = (string * (bool * ((string * sort) list|\isasep\isanewline%
haftmann@21452
  2102
\verb|    * (string * typ list) list))) list|\isasep\isanewline%
haftmann@21348
  2103
\verb|    -> theory -> theory| \\
haftmann@21348
  2104
  \indexml{DatatypeCodegen.add-codetypes-hook-bootstrap}\verb|DatatypeCodegen.add_codetypes_hook_bootstrap: |\isasep\isanewline%
haftmann@21348
  2105
\verb|      DatatypeCodegen.hook -> theory -> theory|
haftmann@21348
  2106
  \end{mldecls}%
haftmann@21348
  2107
\end{isamarkuptext}%
haftmann@21348
  2108
\isamarkuptrue%
haftmann@21348
  2109
%
haftmann@21348
  2110
\endisatagmlref
haftmann@21348
  2111
{\isafoldmlref}%
haftmann@21348
  2112
%
haftmann@21348
  2113
\isadelimmlref
haftmann@21348
  2114
%
haftmann@21348
  2115
\endisadelimmlref
haftmann@21348
  2116
%
haftmann@21348
  2117
\begin{isamarkuptext}%
haftmann@21452
  2118
\begin{description}
haftmann@21452
  2119
haftmann@21452
  2120
  \item \verb|DatatypeCodegen.hook| specifies the code type hook
haftmann@21452
  2121
     interface: a theory transformation depending on a list of
haftmann@21452
  2122
     mutual recursive code types; each entry in the list
haftmann@21452
  2123
     has the structure \isa{{\isacharparenleft}name{\isacharcomma}\ {\isacharparenleft}is{\isacharunderscore}data{\isacharcomma}\ {\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}{\isacharparenright}{\isacharparenright}}
haftmann@21452
  2124
     where \isa{name} is the name of the code type, \isa{is{\isacharunderscore}data}
haftmann@21452
  2125
     is true iff \isa{name} is a datatype rather then a type copy,
haftmann@21452
  2126
     and \isa{{\isacharparenleft}vars{\isacharcomma}\ cons{\isacharparenright}} is the specification of the code type.
haftmann@21452
  2127
haftmann@21452
  2128
  \item \verb|DatatypeCodegen.add_codetypes_hook_bootstrap| adds a code
haftmann@21452
  2129
     type hook;  the hook is immediately processed for all already
haftmann@21452
  2130
     existing datatypes, in blocks of mutual recursive datatypes,
haftmann@21452
  2131
     where all datatypes a block depends on are processed before
haftmann@21452
  2132
     the block.
haftmann@21452
  2133
haftmann@21452
  2134
  \end{description}
haftmann@21452
  2135
haftmann@21452
  2136
  \emph{Happy proving, happy hacking!}%
wenzelm@21172
  2137
\end{isamarkuptext}%
wenzelm@21172
  2138
\isamarkuptrue%
wenzelm@21172
  2139
%
haftmann@20967
  2140
\isadelimtheory
haftmann@20967
  2141
%
haftmann@20967
  2142
\endisadelimtheory
haftmann@20967
  2143
%
haftmann@20967
  2144
\isatagtheory
haftmann@20967
  2145
\isacommand{end}\isamarkupfalse%
haftmann@20967
  2146
%
haftmann@20967
  2147
\endisatagtheory
haftmann@20967
  2148
{\isafoldtheory}%
haftmann@20967
  2149
%
haftmann@20967
  2150
\isadelimtheory
haftmann@20967
  2151
%
haftmann@20967
  2152
\endisadelimtheory
haftmann@20967
  2153
\isanewline
haftmann@20967
  2154
\end{isabellebody}%
haftmann@20967
  2155
%%% Local Variables:
haftmann@20967
  2156
%%% mode: latex
haftmann@20967
  2157
%%% TeX-master: "root"
haftmann@20967
  2158
%%% End: