doc-src/Codegen/Thy/document/Foundations.tex
author haftmann
Thu, 23 Sep 2010 15:46:17 +0200
changeset 39664 0afaf89ab591
parent 39210 985b13c5a61d
child 39683 f75a01ee6c41
permissions -rw-r--r--
more canonical type setting of type writer code examples
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     1
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     2
\begin{isabellebody}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     3
\def\isabellecontext{Foundations}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     4
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     5
\isadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     6
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     7
\endisadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     8
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
     9
\isatagtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    10
\isacommand{theory}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    11
\ Foundations\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    12
\isakeyword{imports}\ Introduction\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    13
\isakeyword{begin}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    14
\endisatagtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    15
{\isafoldtheory}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    16
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    17
\isadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    18
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    19
\endisadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    20
%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    21
\isamarkupsection{Code generation foundations \label{sec:foundations}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    22
}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    23
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    24
%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    25
\isamarkupsubsection{Code generator architecture \label{sec:architecture}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    26
}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    27
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    28
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    29
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    30
The code generator is actually a framework consisting of different
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    31
  components which can be customised individually.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    32
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    33
  Conceptually all components operate on Isabelle's logic framework
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    34
  \hyperlink{theory.Pure}{\mbox{\isa{Pure}}}.  Practically, the object logic \hyperlink{theory.HOL}{\mbox{\isa{HOL}}}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    35
  provides the necessary facilities to make use of the code generator,
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    36
  mainly since it is an extension of \hyperlink{theory.Pure}{\mbox{\isa{Pure}}}.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    37
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    38
  The constellation of the different components is visualized in the
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    39
  following picture.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    40
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    41
  \begin{figure}[h]
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    42
    \includegraphics{architecture}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    43
    \caption{Code generator architecture}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    44
    \label{fig:arch}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    45
  \end{figure}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    46
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    47
  \noindent Central to code generation is the notion of \emph{code
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    48
  equations}.  A code equation as a first approximation is a theorem
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    49
  of the form \isa{f\ t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n\ {\isasymequiv}\ t} (an equation headed by a
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    50
  constant \isa{f} with arguments \isa{t\isactrlisub {\isadigit{1}}\ t\isactrlisub {\isadigit{2}}\ {\isasymdots}\ t\isactrlisub n} and right
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    51
  hand side \isa{t}).
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    52
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    53
  \begin{itemize}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    54
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    55
    \item Starting point of code generation is a collection of (raw)
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    56
      code equations in a theory. It is not relevant where they stem
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    57
      from, but typically they were either produced by specification
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    58
      tools or proved explicitly by the user.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    59
      
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    60
    \item These raw code equations can be subjected to theorem
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    61
      transformations.  This \qn{preprocessor} (see
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    62
      \secref{sec:preproc}) can apply the full expressiveness of
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    63
      ML-based theorem transformations to code generation.  The result
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    64
      of preprocessing is a structured collection of code equations.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    65
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    66
    \item These code equations are \qn{translated} to a program in an
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    67
      abstract intermediate language.  Think of it as a kind of
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    68
      \qt{Mini-Haskell} with four \qn{statements}: \isa{data} (for
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    69
      datatypes), \isa{fun} (stemming from code equations), also
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    70
      \isa{class} and \isa{inst} (for type classes).
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    71
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    72
    \item Finally, the abstract program is \qn{serialised} into
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    73
      concrete source code of a target language.  This step only
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    74
      produces concrete syntax but does not change the program in
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    75
      essence; all conceptual transformations occur in the translation
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    76
      step.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    77
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    78
  \end{itemize}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    79
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    80
  \noindent From these steps, only the last two are carried out
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    81
  outside the logic; by keeping this layer as thin as possible, the
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    82
  amount of code to trust is kept to a minimum.%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    83
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    84
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    85
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    86
\isamarkupsubsection{The preprocessor \label{sec:preproc}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    87
}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    88
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    89
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    90
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    91
Before selected function theorems are turned into abstract code, a
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    92
  chain of definitional transformation steps is carried out:
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    93
  \emph{preprocessing}.  The preprocessor consists of two
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    94
  components: a \emph{simpset} and \emph{function transformers}.
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    95
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    96
  The \emph{simpset} can apply the full generality of the Isabelle
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
    97
  simplifier.  Due to the interpretation of theorems as code
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    98
  equations, rewrites are applied to the right hand side and the
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
    99
  arguments of the left hand side of an equation, but never to the
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   100
  constant heading the left hand side.  An important special case are
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   101
  \emph{unfold theorems}, which may be declared and removed using the
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   102
  \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} or \emph{\hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} del}
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   103
  attribute, respectively.
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   104
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   105
  Some common applications:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   106
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   107
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   108
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   109
\begin{itemize}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   110
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   111
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   112
\item replacing non-executable constructs by executable ones:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   113
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   114
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   115
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   116
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   117
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   118
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   119
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   120
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   121
\isacommand{lemma}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   122
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   123
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ List{\isachardot}member\ xs\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   124
\ {\isacharparenleft}fact\ in{\isacharunderscore}set{\isacharunderscore}member{\isacharparenright}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   125
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   126
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   127
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   128
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   129
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   130
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   131
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   132
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   133
\item replacing executable but inconvenient constructs:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   134
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   135
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   136
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   137
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   138
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   139
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   140
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   141
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   142
\isacommand{lemma}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   143
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   144
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   145
\ {\isacharparenleft}fact\ eq{\isacharunderscore}Nil{\isacharunderscore}null{\isacharparenright}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   146
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   147
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   148
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   149
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   150
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   151
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   152
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   153
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   154
\item eliminating disturbing expressions:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   155
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   156
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   157
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   158
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   159
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   160
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   161
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   162
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   163
\isacommand{lemma}\isamarkupfalse%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   164
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   165
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   166
\ {\isacharparenleft}fact\ One{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   167
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   168
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   169
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   170
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   171
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   172
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   173
%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   174
\end{itemize}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   175
%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   176
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   177
\noindent \emph{Function transformers} provide a very general
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   178
  interface, transforming a list of function theorems to another list
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   179
  of function theorems, provided that neither the heading constant nor
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   180
  its type change.  The \isa{{\isadigit{0}}} / \isa{Suc} pattern
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   181
  elimination implemented in theory \hyperlink{theory.Efficient-Nat}{\mbox{\isa{Efficient{\isacharunderscore}Nat}}} (see
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   182
  \secref{eff_nat}) uses this interface.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   183
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   184
  \noindent The current setup of the preprocessor may be inspected
38505
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   185
  using the \indexdef{}{command}{print\_codeproc}\hypertarget{command.print-codeproc}{\hyperlink{command.print-codeproc}{\mbox{\isa{\isacommand{print{\isacharunderscore}codeproc}}}}} command.  \indexdef{}{command}{code\_thms}\hypertarget{command.code-thms}{\hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}}} (see \secref{sec:equations}) provides a convenient
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   186
  mechanism to inspect the impact of a preprocessor setup on code
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   187
  equations.
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   188
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   189
  \begin{warn}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   190
    Attribute \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} also applies to the
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   191
    preprocessor of the ancient \isa{SML\ code\ generator}; in case
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   192
    this is not what you intend, use \hyperlink{attribute.code-inline}{\mbox{\isa{code{\isacharunderscore}inline}}} instead.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   193
  \end{warn}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   194
\end{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   195
\isamarkuptrue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   196
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   197
\isamarkupsubsection{Understanding code equations \label{sec:equations}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   198
}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   199
\isamarkuptrue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   200
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   201
\begin{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   202
As told in \secref{sec:principle}, the notion of code equations is
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   203
  vital to code generation.  Indeed most problems which occur in
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   204
  practice can be resolved by an inspection of the underlying code
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   205
  equations.
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   206
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   207
  It is possible to exchange the default code equations for constants
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   208
  by explicitly proving alternative ones:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   209
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   210
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   211
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   212
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   213
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   214
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   215
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   216
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   217
\isacommand{lemma}\isamarkupfalse%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   218
\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   219
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   220
\ \ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   221
\ \ \ \ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   222
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   223
\ \ \ \ \ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   224
\ \ \isacommand{by}\isamarkupfalse%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   225
\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\ {\isacharparenleft}cases\ {\isachardoublequoteopen}rev\ xs{\isachardoublequoteclose}{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   226
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   227
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   228
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   229
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   230
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   231
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   232
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   233
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   234
\noindent The annotation \isa{{\isacharbrackleft}code{\isacharbrackright}} is an \isa{attribute}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   235
  which states that the given theorems should be considered as code
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   236
  equations for a \isa{fun} statement -- the corresponding constant
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   237
  is determined syntactically.  The resulting code:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   238
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   239
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   240
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   241
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   242
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   243
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   244
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   245
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   246
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   247
\begin{isamarkuptext}%
39664
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   248
\begin{typewriter}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   249
    dequeue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ Queue\ a\ {\isacharminus}{\isachargreater}\ {\isacharparenleft}Maybe\ a{\isacharcomma}\ Queue\ a{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   250
dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Just\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   251
dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   252
\ \ {\isacharparenleft}if\ null\ xs\ then\ {\isacharparenleft}Nothing{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   253
\ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}reverse\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharsemicolon}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   254
  \end{typewriter}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   255
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   256
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   257
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   258
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   259
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   260
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   261
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   262
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   263
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   264
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   265
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   266
\noindent You may note that the equality test \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} has
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   267
  been replaced by the predicate \isa{List{\isachardot}null\ xs}.  This is due
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   268
  to the default setup of the \qn{preprocessor}.
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   269
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   270
  This possibility to select arbitrary code equations is the key
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   271
  technique for program and datatype refinement (see
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   272
  \secref{sec:refinement}.
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   273
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   274
  Due to the preprocessor, there is the distinction of raw code
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   275
  equations (before preprocessing) and code equations (after
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   276
  preprocessing).
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   277
38505
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   278
  The first can be listed (among other data) using the \indexdef{}{command}{print\_codesetup}\hypertarget{command.print-codesetup}{\hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}}} command.
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   279
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   280
  The code equations after preprocessing are already are blueprint of
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   281
  the generated program and can be inspected using the \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} command:%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   282
\end{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   283
\isamarkuptrue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   284
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   285
\isadelimquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   286
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   287
\endisadelimquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   288
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   289
\isatagquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   290
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   291
\ dequeue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   292
\endisatagquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   293
{\isafoldquote}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   294
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   295
\isadelimquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   296
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   297
\endisadelimquote
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   298
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   299
\begin{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   300
\noindent This prints a table with the code equations for \isa{dequeue}, including \emph{all} code equations those equations depend
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   301
  on recursively.  These dependencies themselves can be visualized using
38505
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   302
  the \indexdef{}{command}{code\_deps}\hypertarget{command.code-deps}{\hyperlink{command.code-deps}{\mbox{\isa{\isacommand{code{\isacharunderscore}deps}}}}} command.%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   303
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   304
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   305
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   306
\isamarkupsubsection{Equality%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   307
}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   308
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   309
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   310
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   311
Implementation of equality deserves some attention.  Here an example
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   312
  function involving polymorphic equality:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   313
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   314
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   315
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   316
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   317
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   318
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   319
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   320
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   321
\isacommand{primrec}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   322
\ 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
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   323
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   324
{\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   325
\ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   326
\ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   327
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   328
\ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   329
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   330
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   331
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   332
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   333
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   334
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   335
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   336
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   337
\noindent During preprocessing, the membership test is rewritten,
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   338
  resulting in \isa{List{\isachardot}member}, which itself performs an explicit
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   339
  equality check, as can be seen in the corresponding \isa{SML} code:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   340
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   341
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   342
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   343
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   344
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   345
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   346
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   347
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   348
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   349
\begin{isamarkuptext}%
39664
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   350
\begin{typewriter}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   351
    structure\ Example\ {\isacharcolon}\ sig\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   352
\ \ type\ {\isacharprime}a\ equal\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   353
\ \ val\ equal\ {\isacharcolon}\ {\isacharprime}a\ equal\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ bool\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   354
\ \ val\ eq\ {\isacharcolon}\ {\isacharprime}a\ equal\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ bool\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   355
\ \ val\ member\ {\isacharcolon}\ {\isacharprime}a\ equal\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ bool\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   356
\ \ val\ collect{\isacharunderscore}duplicates\ {\isacharcolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   357
\ \ \ \ {\isacharprime}a\ equal\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ list\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   358
end\ {\isacharequal}\ struct\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   359
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   360
type\ {\isacharprime}a\ equal\ {\isacharequal}\ {\isacharbraceleft}equal\ {\isacharcolon}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ bool{\isacharbraceright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   361
val\ equal\ {\isacharequal}\ {\isacharhash}equal\ {\isacharcolon}\ {\isacharprime}a\ equal\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ {\isacharprime}a\ {\isacharminus}{\isachargreater}\ bool{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   362
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   363
fun\ eq\ A{\isacharunderscore}\ a\ b\ {\isacharequal}\ equal\ A{\isacharunderscore}\ a\ b{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   364
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   365
fun\ member\ A{\isacharunderscore}\ {\isacharbrackleft}{\isacharbrackright}\ y\ {\isacharequal}\ false\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   366
\ \ {\isacharbar}\ member\ A{\isacharunderscore}\ {\isacharparenleft}x\ {\isacharcolon}{\isacharcolon}\ xs{\isacharparenright}\ y\ {\isacharequal}\ eq\ A{\isacharunderscore}\ x\ y\ orelse\ member\ A{\isacharunderscore}\ xs\ y{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   367
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   368
fun\ collect{\isacharunderscore}duplicates\ A{\isacharunderscore}\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   369
\ \ {\isacharbar}\ collect{\isacharunderscore}duplicates\ A{\isacharunderscore}\ xs\ ys\ {\isacharparenleft}z\ {\isacharcolon}{\isacharcolon}\ zs{\isacharparenright}\ {\isacharequal}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   370
\ \ \ \ {\isacharparenleft}if\ member\ A{\isacharunderscore}\ xs\ z\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   371
\ \ \ \ \ \ then\ {\isacharparenleft}if\ member\ A{\isacharunderscore}\ ys\ z\ then\ collect{\isacharunderscore}duplicates\ A{\isacharunderscore}\ xs\ ys\ zs\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   372
\ \ \ \ \ \ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ A{\isacharunderscore}\ xs\ {\isacharparenleft}z\ {\isacharcolon}{\isacharcolon}\ ys{\isacharparenright}\ zs{\isacharparenright}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   373
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ A{\isacharunderscore}\ {\isacharparenleft}z\ {\isacharcolon}{\isacharcolon}\ xs{\isacharparenright}\ {\isacharparenleft}z\ {\isacharcolon}{\isacharcolon}\ ys{\isacharparenright}\ zs{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   374
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   375
end{\isacharsemicolon}\ {\isacharparenleft}{\isacharasterisk}struct\ Example{\isacharasterisk}{\isacharparenright}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   376
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   377
  \end{typewriter}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   378
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   379
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   380
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   381
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   382
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   383
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   384
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   385
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   386
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   387
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   388
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   389
\noindent Obviously, polymorphic equality is implemented the Haskell
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   390
  way using a type class.  How is this achieved?  HOL introduces an
39070
352bcd845998 updated
haftmann
parents: 38505
diff changeset
   391
  explicit class \isa{equal} with a corresponding operation \isa{equal{\isacharunderscore}class{\isachardot}equal} such that \isa{equal{\isacharunderscore}class{\isachardot}equal\ {\isacharequal}\ op\ {\isacharequal}}.  The preprocessing
352bcd845998 updated
haftmann
parents: 38505
diff changeset
   392
  framework does the rest by propagating the \isa{equal} constraints
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   393
  through all dependent code equations.  For datatypes, instances of
39070
352bcd845998 updated
haftmann
parents: 38505
diff changeset
   394
  \isa{equal} are implicitly derived when possible.  For other types,
352bcd845998 updated
haftmann
parents: 38505
diff changeset
   395
  you may instantiate \isa{equal} manually like any other type class.%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   396
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   397
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   398
%
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   399
\isamarkupsubsection{Explicit partiality \label{sec:partiality}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   400
}
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   401
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   402
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   403
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   404
Partiality usually enters the game by partial patterns, as
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   405
  in the following example, again for amortised queues:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   406
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   407
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   408
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   409
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   410
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   411
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   412
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   413
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   414
\isacommand{definition}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   415
\ strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   416
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ q\ {\isacharequal}\ {\isacharparenleft}case\ dequeue\ q\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   417
\ \ \ \ of\ {\isacharparenleft}Some\ x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   418
\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   419
\isacommand{lemma}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   420
\ strict{\isacharunderscore}dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   421
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   422
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   423
\ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   424
\ \ \isacommand{by}\isamarkupfalse%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   425
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharunderscore}def{\isacharparenright}\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all\ split{\isacharcolon}\ list{\isachardot}split{\isacharparenright}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   426
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   427
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   428
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   429
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   430
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   431
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   432
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   433
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   434
\noindent In the corresponding code, there is no equation
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   435
  for the pattern \isa{AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}}:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   436
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   437
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   438
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   439
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   440
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   441
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   442
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   443
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   444
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   445
\begin{isamarkuptext}%
39664
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   446
\begin{typewriter}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   447
    strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ Queue\ a\ {\isacharminus}{\isachargreater}\ {\isacharparenleft}a{\isacharcomma}\ Queue\ a{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   448
strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   449
\ \ let\ {\isacharbraceleft}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   450
\ \ \ \ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}\ {\isacharequal}\ reverse\ xs{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   451
\ \ {\isacharbraceright}\ in\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   452
strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isacharsemicolon}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   453
  \end{typewriter}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   454
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   455
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   456
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   457
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   458
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   459
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   460
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   461
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   462
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   463
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   464
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   465
\noindent In some cases it is desirable to have this
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   466
  pseudo-\qt{partiality} more explicitly, e.g.~as follows:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   467
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   468
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   469
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   470
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   471
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   472
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   473
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   474
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   475
\isacommand{axiomatization}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   476
\ empty{\isacharunderscore}queue\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   477
\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   478
\isacommand{definition}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   479
\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   480
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ q\ {\isacharequal}\ {\isacharparenleft}case\ dequeue\ q\ of\ {\isacharparenleft}Some\ x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isacharbar}\ {\isacharunderscore}\ {\isasymRightarrow}\ empty{\isacharunderscore}queue{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   481
\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   482
\isacommand{lemma}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   483
\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   484
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ empty{\isacharunderscore}queue\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   485
\ \ \ \ \ else\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   486
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   487
\ \ \ \ \ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   488
\ \ \isacommand{by}\isamarkupfalse%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   489
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}def\ split{\isacharcolon}\ list{\isachardot}splits{\isacharparenright}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   490
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   491
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   492
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   493
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   494
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   495
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   496
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   497
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   498
Observe that on the right hand side of the definition of \isa{strict{\isacharunderscore}dequeue{\isacharprime}}, the unspecified constant \isa{empty{\isacharunderscore}queue} occurs.
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   499
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   500
  Normally, if constants without any code equations occur in a
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   501
  program, the code generator complains (since in most cases this is
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   502
  indeed an error).  But such constants can also be thought
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   503
  of as function definitions which always fail,
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   504
  since there is never a successful pattern match on the left hand
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   505
  side.  In order to categorise a constant into that category
38505
2f8699695cf6 use command_def vs. command more consciously
haftmann
parents: 38440
diff changeset
   506
  explicitly, use \indexdef{}{command}{code\_abort}\hypertarget{command.code-abort}{\hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}}:%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   507
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   508
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   509
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   510
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   511
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   512
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   513
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   514
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   515
\isacommand{code{\isacharunderscore}abort}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   516
\ empty{\isacharunderscore}queue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   517
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   518
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   519
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   520
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   521
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   522
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   523
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   524
\begin{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   525
\noindent Then the code generator will just insert an error or
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   526
  exception at the appropriate position:%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   527
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   528
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   529
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   530
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   531
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   532
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   533
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   534
\isatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   535
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   536
\begin{isamarkuptext}%
39664
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   537
\begin{typewriter}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   538
    empty{\isacharunderscore}queue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ a{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   539
empty{\isacharunderscore}queue\ {\isacharequal}\ error\ {\isachardoublequote}empty{\isacharunderscore}queue{\isachardoublequote}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   540
\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   541
strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ forall\ a{\isachardot}\ Queue\ a\ {\isacharminus}{\isachargreater}\ {\isacharparenleft}a{\isacharcomma}\ Queue\ a{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   542
strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharcolon}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isacharsemicolon}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   543
strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   544
\ \ {\isacharparenleft}if\ null\ xs\ then\ empty{\isacharunderscore}queue\isanewline
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   545
\ \ \ \ else\ strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}reverse\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharsemicolon}
0afaf89ab591 more canonical type setting of type writer code examples
haftmann
parents: 39210
diff changeset
   546
  \end{typewriter}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   547
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   548
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   549
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   550
\endisatagquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   551
{\isafoldquote}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   552
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   553
\isadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   554
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   555
\endisadelimquote
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   556
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   557
\begin{isamarkuptext}%
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   558
\noindent This feature however is rarely needed in practice.  Note
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   559
  also that the HOL default setup already declares \isa{undefined}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   560
  as \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}, which is most likely to be used in such
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   561
  situations.%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   562
\end{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   563
\isamarkuptrue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   564
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   565
\isamarkupsubsection{If something goes utterly wrong \label{sec:utterly_wrong}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   566
}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   567
\isamarkuptrue%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   568
%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   569
\begin{isamarkuptext}%
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   570
Under certain circumstances, the code generator fails to produce
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   571
  code entirely.  To debug these, the following hints may prove
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   572
  helpful:
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   573
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   574
  \begin{description}
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   575
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   576
    \ditem{\emph{Check with a different target language}.}  Sometimes
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   577
      the situation gets more clear if you switch to another target
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   578
      language; the code generated there might give some hints what
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   579
      prevents the code generator to produce code for the desired
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   580
      language.
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   581
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   582
    \ditem{\emph{Inspect code equations}.}  Code equations are the central
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   583
      carrier of code generation.  Most problems occuring while generation
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   584
      code can be traced to single equations which are printed as part of
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   585
      the error message.  A closer inspection of those may offer the key
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   586
      for solving issues (cf.~\secref{sec:equations}).
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   587
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   588
    \ditem{\emph{Inspect preprocessor setup}.}  The preprocessor might
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   589
      transform code equations unexpectedly; to understand an
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   590
      inspection of its setup is necessary (cf.~\secref{sec:preproc}).
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   591
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   592
    \ditem{\emph{Generate exceptions}.}  If the code generator
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   593
      complains about missing code equations, in can be helpful to
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   594
      implement the offending constants as exceptions
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   595
      (cf.~\secref{sec:partiality}); this allows at least for a formal
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   596
      generation of code, whose inspection may then give clues what is
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   597
      wrong.
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   598
38440
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   599
    \ditem{\emph{Remove offending code equations}.}  If code
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   600
      generation is prevented by just a single equation, this can be
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   601
      removed (cf.~\secref{sec:equations}) to allow formal code
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   602
      generation, whose result in turn can be used to trace the
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   603
      problem.  The most prominent case here are mismatches in type
6c0d02f416ba section "if something goes utterly wrong"
haftmann
parents: 38437
diff changeset
   604
      class signatures (\qt{wellsortedness error}).
38437
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   605
ffb1c5bf0425 adaptation to new outline
haftmann
parents: 38406
diff changeset
   606
  \end{description}%
38406
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   607
\end{isamarkuptext}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   608
\isamarkuptrue%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   609
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   610
\isadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   611
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   612
\endisadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   613
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   614
\isatagtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   615
\isacommand{end}\isamarkupfalse%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   616
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   617
\endisatagtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   618
{\isafoldtheory}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   619
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   620
\isadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   621
%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   622
\endisadelimtheory
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   623
\isanewline
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   624
\end{isabellebody}%
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   625
%%% Local Variables:
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   626
%%% mode: latex
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   627
%%% TeX-master: "root"
bbb02b67caac sketch of new outline
haftmann
parents:
diff changeset
   628
%%% End: