doc-src/Codegen/Thy/document/Program.tex
author haftmann
Mon, 14 Jun 2010 15:27:09 +0200
changeset 37428 b3d94253e7f2
parent 37212 b8e02ce2559f
child 37432 e732b4f8fddf
permissions -rw-r--r--
updated generated code
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28447
haftmann
parents:
diff changeset
     1
%
haftmann
parents:
diff changeset
     2
\begin{isabellebody}%
haftmann
parents:
diff changeset
     3
\def\isabellecontext{Program}%
haftmann
parents:
diff changeset
     4
%
haftmann
parents:
diff changeset
     5
\isadelimtheory
haftmann
parents:
diff changeset
     6
%
haftmann
parents:
diff changeset
     7
\endisadelimtheory
haftmann
parents:
diff changeset
     8
%
haftmann
parents:
diff changeset
     9
\isatagtheory
haftmann
parents:
diff changeset
    10
\isacommand{theory}\isamarkupfalse%
haftmann
parents:
diff changeset
    11
\ Program\isanewline
haftmann
parents:
diff changeset
    12
\isakeyword{imports}\ Introduction\isanewline
haftmann
parents:
diff changeset
    13
\isakeyword{begin}%
haftmann
parents:
diff changeset
    14
\endisatagtheory
haftmann
parents:
diff changeset
    15
{\isafoldtheory}%
haftmann
parents:
diff changeset
    16
%
haftmann
parents:
diff changeset
    17
\isadelimtheory
haftmann
parents:
diff changeset
    18
%
haftmann
parents:
diff changeset
    19
\endisadelimtheory
haftmann
parents:
diff changeset
    20
%
haftmann
parents:
diff changeset
    21
\isamarkupsection{Turning Theories into Programs \label{sec:program}%
haftmann
parents:
diff changeset
    22
}
haftmann
parents:
diff changeset
    23
\isamarkuptrue%
haftmann
parents:
diff changeset
    24
%
haftmann
parents:
diff changeset
    25
\isamarkupsubsection{The \isa{Isabelle{\isacharslash}HOL} default setup%
haftmann
parents:
diff changeset
    26
}
haftmann
parents:
diff changeset
    27
\isamarkuptrue%
haftmann
parents:
diff changeset
    28
%
haftmann
parents:
diff changeset
    29
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    30
We have already seen how by default equations stemming from
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
    31
  \hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}, \hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}} and \hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}}
28447
haftmann
parents:
diff changeset
    32
  statements are used for code generation.  This default behaviour
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
    33
  can be changed, e.g.\ by providing different code equations.
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
    34
  The customisations shown in this section are \emph{safe}
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
    35
  as regards correctness: all programs that can be generated are partially
28447
haftmann
parents:
diff changeset
    36
  correct.%
haftmann
parents:
diff changeset
    37
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    38
\isamarkuptrue%
haftmann
parents:
diff changeset
    39
%
haftmann
parents:
diff changeset
    40
\isamarkupsubsection{Selecting code equations%
haftmann
parents:
diff changeset
    41
}
haftmann
parents:
diff changeset
    42
\isamarkuptrue%
haftmann
parents:
diff changeset
    43
%
haftmann
parents:
diff changeset
    44
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
    45
Coming back to our introductory example, we
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
    46
  could provide an alternative code equations for \isa{dequeue}
28447
haftmann
parents:
diff changeset
    47
  explicitly:%
haftmann
parents:
diff changeset
    48
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    49
\isamarkuptrue%
haftmann
parents:
diff changeset
    50
%
28564
haftmann
parents: 28562
diff changeset
    51
\isadelimquote
28447
haftmann
parents:
diff changeset
    52
%
28564
haftmann
parents: 28562
diff changeset
    53
\endisadelimquote
28447
haftmann
parents:
diff changeset
    54
%
28564
haftmann
parents: 28562
diff changeset
    55
\isatagquote
28447
haftmann
parents:
diff changeset
    56
\isacommand{lemma}\isamarkupfalse%
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28462
diff changeset
    57
\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    58
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    59
\ \ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    60
\ \ \ \ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    61
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    62
\ \ \ \ \ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
28447
haftmann
parents:
diff changeset
    63
\ \ \isacommand{by}\isamarkupfalse%
haftmann
parents:
diff changeset
    64
\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\ {\isacharparenleft}cases\ {\isachardoublequoteopen}rev\ xs{\isachardoublequoteclose}{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
    65
\endisatagquote
haftmann
parents: 28562
diff changeset
    66
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
    67
%
28564
haftmann
parents: 28562
diff changeset
    68
\isadelimquote
28447
haftmann
parents:
diff changeset
    69
%
28564
haftmann
parents: 28562
diff changeset
    70
\endisadelimquote
28447
haftmann
parents:
diff changeset
    71
%
haftmann
parents:
diff changeset
    72
\begin{isamarkuptext}%
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28462
diff changeset
    73
\noindent The annotation \isa{{\isacharbrackleft}code{\isacharbrackright}} is an \isa{Isar}
28447
haftmann
parents:
diff changeset
    74
  \isa{attribute} which states that the given theorems should be
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
    75
  considered as code equations for a \isa{fun} statement --
28447
haftmann
parents:
diff changeset
    76
  the corresponding constant is determined syntactically.  The resulting code:%
haftmann
parents:
diff changeset
    77
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    78
\isamarkuptrue%
haftmann
parents:
diff changeset
    79
%
28564
haftmann
parents: 28562
diff changeset
    80
\isadelimquote
28447
haftmann
parents:
diff changeset
    81
%
28564
haftmann
parents: 28562
diff changeset
    82
\endisadelimquote
28447
haftmann
parents:
diff changeset
    83
%
28564
haftmann
parents: 28562
diff changeset
    84
\isatagquote
28447
haftmann
parents:
diff changeset
    85
%
haftmann
parents:
diff changeset
    86
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
    87
\isatypewriter%
28447
haftmann
parents:
diff changeset
    88
\noindent%
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
    89
\hspace*{0pt}dequeue ::~forall a.~Queue a -> (Maybe a,~Queue a);\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
    90
\hspace*{0pt}dequeue (AQueue xs (y :~ys)) = (Just y,~AQueue xs ys);\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
    91
\hspace*{0pt}dequeue (AQueue xs []) =\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
    92
\hspace*{0pt} ~(if nulla xs then (Nothing,~AQueue [] [])\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
    93
\hspace*{0pt} ~~~else dequeue (AQueue [] (rev xs)));%
28447
haftmann
parents:
diff changeset
    94
\end{isamarkuptext}%
haftmann
parents:
diff changeset
    95
\isamarkuptrue%
haftmann
parents:
diff changeset
    96
%
28564
haftmann
parents: 28562
diff changeset
    97
\endisatagquote
haftmann
parents: 28562
diff changeset
    98
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
    99
%
28564
haftmann
parents: 28562
diff changeset
   100
\isadelimquote
28447
haftmann
parents:
diff changeset
   101
%
28564
haftmann
parents: 28562
diff changeset
   102
\endisadelimquote
28447
haftmann
parents:
diff changeset
   103
%
haftmann
parents:
diff changeset
   104
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   105
\noindent You may note that the equality test \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} has been
haftmann
parents:
diff changeset
   106
  replaced by the predicate \isa{null\ xs}.  This is due to the default
haftmann
parents:
diff changeset
   107
  setup in the \qn{preprocessor} to be discussed further below (\secref{sec:preproc}).
haftmann
parents:
diff changeset
   108
haftmann
parents:
diff changeset
   109
  Changing the default constructor set of datatypes is also
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   110
  possible.  See \secref{sec:datatypes} for an example.
28447
haftmann
parents:
diff changeset
   111
haftmann
parents:
diff changeset
   112
  As told in \secref{sec:concept}, code generation is based
haftmann
parents:
diff changeset
   113
  on a structured collection of code theorems.
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   114
  This collection
28447
haftmann
parents:
diff changeset
   115
  may be inspected using the \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} command:%
haftmann
parents:
diff changeset
   116
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   117
\isamarkuptrue%
haftmann
parents:
diff changeset
   118
%
28564
haftmann
parents: 28562
diff changeset
   119
\isadelimquote
28447
haftmann
parents:
diff changeset
   120
%
28564
haftmann
parents: 28562
diff changeset
   121
\endisadelimquote
28447
haftmann
parents:
diff changeset
   122
%
28564
haftmann
parents: 28562
diff changeset
   123
\isatagquote
28447
haftmann
parents:
diff changeset
   124
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
haftmann
parents:
diff changeset
   125
\ dequeue%
28564
haftmann
parents: 28562
diff changeset
   126
\endisatagquote
haftmann
parents: 28562
diff changeset
   127
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   128
%
28564
haftmann
parents: 28562
diff changeset
   129
\isadelimquote
28447
haftmann
parents:
diff changeset
   130
%
28564
haftmann
parents: 28562
diff changeset
   131
\endisadelimquote
28447
haftmann
parents:
diff changeset
   132
%
haftmann
parents:
diff changeset
   133
\begin{isamarkuptext}%
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   134
\noindent prints a table with \emph{all} code equations
28447
haftmann
parents:
diff changeset
   135
  for \isa{dequeue}, including
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   136
  \emph{all} code equations those equations depend
28447
haftmann
parents:
diff changeset
   137
  on recursively.
haftmann
parents:
diff changeset
   138
  
haftmann
parents:
diff changeset
   139
  Similarly, the \hyperlink{command.code-deps}{\mbox{\isa{\isacommand{code{\isacharunderscore}deps}}}} command shows a graph
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   140
  visualising dependencies between code equations.%
28447
haftmann
parents:
diff changeset
   141
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   142
\isamarkuptrue%
haftmann
parents:
diff changeset
   143
%
haftmann
parents:
diff changeset
   144
\isamarkupsubsection{\isa{class} and \isa{instantiation}%
haftmann
parents:
diff changeset
   145
}
haftmann
parents:
diff changeset
   146
\isamarkuptrue%
haftmann
parents:
diff changeset
   147
%
haftmann
parents:
diff changeset
   148
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   149
Concerning type classes and code generation, let us examine an example
haftmann
parents:
diff changeset
   150
  from abstract algebra:%
haftmann
parents:
diff changeset
   151
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   152
\isamarkuptrue%
haftmann
parents:
diff changeset
   153
%
28564
haftmann
parents: 28562
diff changeset
   154
\isadelimquote
28447
haftmann
parents:
diff changeset
   155
%
28564
haftmann
parents: 28562
diff changeset
   156
\endisadelimquote
28447
haftmann
parents:
diff changeset
   157
%
28564
haftmann
parents: 28562
diff changeset
   158
\isatagquote
28447
haftmann
parents:
diff changeset
   159
\isacommand{class}\isamarkupfalse%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   160
\ semigroup\ {\isacharequal}\isanewline
28447
haftmann
parents:
diff changeset
   161
\ \ \isakeyword{fixes}\ mult\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ {\isacharparenleft}\isakeyword{infixl}\ {\isachardoublequoteopen}{\isasymotimes}{\isachardoublequoteclose}\ {\isadigit{7}}{\isadigit{0}}{\isacharparenright}\isanewline
haftmann
parents:
diff changeset
   162
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   163
\isanewline
haftmann
parents:
diff changeset
   164
\isacommand{class}\isamarkupfalse%
haftmann
parents:
diff changeset
   165
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
haftmann
parents:
diff changeset
   166
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
haftmann
parents:
diff changeset
   167
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   168
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   169
\isanewline
haftmann
parents:
diff changeset
   170
\isacommand{instantiation}\isamarkupfalse%
haftmann
parents:
diff changeset
   171
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline
haftmann
parents:
diff changeset
   172
\isakeyword{begin}\isanewline
haftmann
parents:
diff changeset
   173
\isanewline
haftmann
parents:
diff changeset
   174
\isacommand{primrec}\isamarkupfalse%
haftmann
parents:
diff changeset
   175
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   176
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   177
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   178
\isanewline
haftmann
parents:
diff changeset
   179
\isacommand{definition}\isamarkupfalse%
haftmann
parents:
diff changeset
   180
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   181
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   182
\isanewline
haftmann
parents:
diff changeset
   183
\isacommand{lemma}\isamarkupfalse%
haftmann
parents:
diff changeset
   184
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline
haftmann
parents:
diff changeset
   185
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann
parents:
diff changeset
   186
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   187
\ \ \isacommand{by}\isamarkupfalse%
haftmann
parents:
diff changeset
   188
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
haftmann
parents:
diff changeset
   189
\isanewline
haftmann
parents:
diff changeset
   190
\isacommand{instance}\isamarkupfalse%
haftmann
parents:
diff changeset
   191
\ \isacommand{proof}\isamarkupfalse%
haftmann
parents:
diff changeset
   192
\isanewline
haftmann
parents:
diff changeset
   193
\ \ \isacommand{fix}\isamarkupfalse%
haftmann
parents:
diff changeset
   194
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann
parents:
diff changeset
   195
\ \ \isacommand{show}\isamarkupfalse%
haftmann
parents:
diff changeset
   196
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   197
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann
parents:
diff changeset
   198
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline
haftmann
parents:
diff changeset
   199
\ \ \isacommand{show}\isamarkupfalse%
haftmann
parents:
diff changeset
   200
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   201
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann
parents:
diff changeset
   202
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
haftmann
parents:
diff changeset
   203
\ \ \isacommand{show}\isamarkupfalse%
haftmann
parents:
diff changeset
   204
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   205
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann
parents:
diff changeset
   206
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
haftmann
parents:
diff changeset
   207
\isacommand{qed}\isamarkupfalse%
haftmann
parents:
diff changeset
   208
\isanewline
haftmann
parents:
diff changeset
   209
\isanewline
haftmann
parents:
diff changeset
   210
\isacommand{end}\isamarkupfalse%
haftmann
parents:
diff changeset
   211
%
28564
haftmann
parents: 28562
diff changeset
   212
\endisatagquote
haftmann
parents: 28562
diff changeset
   213
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   214
%
28564
haftmann
parents: 28562
diff changeset
   215
\isadelimquote
28447
haftmann
parents:
diff changeset
   216
%
28564
haftmann
parents: 28562
diff changeset
   217
\endisadelimquote
28447
haftmann
parents:
diff changeset
   218
%
haftmann
parents:
diff changeset
   219
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   220
\noindent We define the natural operation of the natural numbers
haftmann
parents:
diff changeset
   221
  on monoids:%
haftmann
parents:
diff changeset
   222
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   223
\isamarkuptrue%
haftmann
parents:
diff changeset
   224
%
28564
haftmann
parents: 28562
diff changeset
   225
\isadelimquote
28447
haftmann
parents:
diff changeset
   226
%
28564
haftmann
parents: 28562
diff changeset
   227
\endisadelimquote
28447
haftmann
parents:
diff changeset
   228
%
28564
haftmann
parents: 28562
diff changeset
   229
\isatagquote
28447
haftmann
parents:
diff changeset
   230
\isacommand{primrec}\isamarkupfalse%
haftmann
parents:
diff changeset
   231
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   232
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   233
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}%
28564
haftmann
parents: 28562
diff changeset
   234
\endisatagquote
haftmann
parents: 28562
diff changeset
   235
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   236
%
28564
haftmann
parents: 28562
diff changeset
   237
\isadelimquote
28447
haftmann
parents:
diff changeset
   238
%
28564
haftmann
parents: 28562
diff changeset
   239
\endisadelimquote
28447
haftmann
parents:
diff changeset
   240
%
haftmann
parents:
diff changeset
   241
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   242
\noindent This we use to define the discrete exponentiation function:%
haftmann
parents:
diff changeset
   243
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   244
\isamarkuptrue%
haftmann
parents:
diff changeset
   245
%
28564
haftmann
parents: 28562
diff changeset
   246
\isadelimquote
28447
haftmann
parents:
diff changeset
   247
%
28564
haftmann
parents: 28562
diff changeset
   248
\endisadelimquote
28447
haftmann
parents:
diff changeset
   249
%
28564
haftmann
parents: 28562
diff changeset
   250
\isatagquote
28447
haftmann
parents:
diff changeset
   251
\isacommand{definition}\isamarkupfalse%
haftmann
parents:
diff changeset
   252
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann
parents:
diff changeset
   253
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
28564
haftmann
parents: 28562
diff changeset
   254
\endisatagquote
haftmann
parents: 28562
diff changeset
   255
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   256
%
28564
haftmann
parents: 28562
diff changeset
   257
\isadelimquote
28447
haftmann
parents:
diff changeset
   258
%
28564
haftmann
parents: 28562
diff changeset
   259
\endisadelimquote
28447
haftmann
parents:
diff changeset
   260
%
haftmann
parents:
diff changeset
   261
\begin{isamarkuptext}%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   262
\noindent The corresponding code in Haskell uses that language's native classes:%
28447
haftmann
parents:
diff changeset
   263
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   264
\isamarkuptrue%
haftmann
parents:
diff changeset
   265
%
28564
haftmann
parents: 28562
diff changeset
   266
\isadelimquote
28447
haftmann
parents:
diff changeset
   267
%
28564
haftmann
parents: 28562
diff changeset
   268
\endisadelimquote
28447
haftmann
parents:
diff changeset
   269
%
28564
haftmann
parents: 28562
diff changeset
   270
\isatagquote
28447
haftmann
parents:
diff changeset
   271
%
haftmann
parents:
diff changeset
   272
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   273
\isatypewriter%
28447
haftmann
parents:
diff changeset
   274
\noindent%
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   275
\hspace*{0pt}module Example where {\char123}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   276
\hspace*{0pt}\\
30121
5c7bcb296600 updated generated files;
wenzelm
parents: 29798
diff changeset
   277
\hspace*{0pt}data Nat = Zero{\char95}nat | Suc Nat;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   278
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   279
\hspace*{0pt}class Semigroup a where {\char123}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   280
\hspace*{0pt} ~mult ::~a -> a -> a;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   281
\hspace*{0pt}{\char125};\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   282
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   283
\hspace*{0pt}class (Semigroup a) => Monoid a where {\char123}\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   284
\hspace*{0pt} ~neutral ::~a;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   285
\hspace*{0pt}{\char125};\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   286
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   287
\hspace*{0pt}pow ::~forall a.~(Monoid a) => Nat -> a -> a;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   288
\hspace*{0pt}pow Zero{\char95}nat a = neutral;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   289
\hspace*{0pt}pow (Suc n) a = mult a (pow n a);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   290
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   291
\hspace*{0pt}plus{\char95}nat ::~Nat -> Nat -> Nat;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   292
\hspace*{0pt}plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   293
\hspace*{0pt}plus{\char95}nat Zero{\char95}nat n = n;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   294
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   295
\hspace*{0pt}neutral{\char95}nat ::~Nat;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   296
\hspace*{0pt}neutral{\char95}nat = Suc Zero{\char95}nat;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   297
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   298
\hspace*{0pt}mult{\char95}nat ::~Nat -> Nat -> Nat;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   299
\hspace*{0pt}mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   300
\hspace*{0pt}mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   301
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   302
\hspace*{0pt}instance Semigroup Nat where {\char123}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   303
\hspace*{0pt} ~mult = mult{\char95}nat;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   304
\hspace*{0pt}{\char125};\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   305
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   306
\hspace*{0pt}instance Monoid Nat where {\char123}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   307
\hspace*{0pt} ~neutral = neutral{\char95}nat;\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   308
\hspace*{0pt}{\char125};\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   309
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   310
\hspace*{0pt}bexp ::~Nat -> Nat;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   311
\hspace*{0pt}bexp n = pow n (Suc (Suc Zero{\char95}nat));\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   312
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   313
\hspace*{0pt}{\char125}%
28447
haftmann
parents:
diff changeset
   314
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   315
\isamarkuptrue%
haftmann
parents:
diff changeset
   316
%
28564
haftmann
parents: 28562
diff changeset
   317
\endisatagquote
haftmann
parents: 28562
diff changeset
   318
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   319
%
28564
haftmann
parents: 28562
diff changeset
   320
\isadelimquote
28447
haftmann
parents:
diff changeset
   321
%
28564
haftmann
parents: 28562
diff changeset
   322
\endisadelimquote
28447
haftmann
parents:
diff changeset
   323
%
haftmann
parents:
diff changeset
   324
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   325
\noindent This is a convenient place to show how explicit dictionary construction
37428
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   326
  manifests in generated code (here, the same example in \isa{SML})
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   327
  \cite{Haftmann-Nipkow:2010:code}:%
28447
haftmann
parents:
diff changeset
   328
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   329
\isamarkuptrue%
haftmann
parents:
diff changeset
   330
%
28564
haftmann
parents: 28562
diff changeset
   331
\isadelimquote
28447
haftmann
parents:
diff changeset
   332
%
28564
haftmann
parents: 28562
diff changeset
   333
\endisadelimquote
28447
haftmann
parents:
diff changeset
   334
%
28564
haftmann
parents: 28562
diff changeset
   335
\isatagquote
28447
haftmann
parents:
diff changeset
   336
%
haftmann
parents:
diff changeset
   337
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   338
\isatypewriter%
28447
haftmann
parents:
diff changeset
   339
\noindent%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   340
\hspace*{0pt}structure Example :~sig\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   341
\hspace*{0pt} ~datatype nat = Zero{\char95}nat | Suc of nat\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   342
\hspace*{0pt} ~type 'a semigroup\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   343
\hspace*{0pt} ~val mult :~'a semigroup -> 'a -> 'a -> 'a\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   344
\hspace*{0pt} ~type 'a monoid\\
37428
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   345
\hspace*{0pt} ~val monoid{\char95}semigroup :~'a monoid -> 'a semigroup\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   346
\hspace*{0pt} ~val neutral :~'a monoid -> 'a\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   347
\hspace*{0pt} ~val pow :~'a monoid -> nat -> 'a -> 'a\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   348
\hspace*{0pt} ~val plus{\char95}nat :~nat -> nat -> nat\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   349
\hspace*{0pt} ~val neutral{\char95}nat :~nat\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   350
\hspace*{0pt} ~val mult{\char95}nat :~nat -> nat -> nat\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   351
\hspace*{0pt} ~val semigroup{\char95}nat :~nat semigroup\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   352
\hspace*{0pt} ~val monoid{\char95}nat :~nat monoid\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   353
\hspace*{0pt} ~val bexp :~nat -> nat\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   354
\hspace*{0pt}end = struct\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   355
\hspace*{0pt}\\
30121
5c7bcb296600 updated generated files;
wenzelm
parents: 29798
diff changeset
   356
\hspace*{0pt}datatype nat = Zero{\char95}nat | Suc of nat;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   357
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   358
\hspace*{0pt}type 'a semigroup = {\char123}mult :~'a -> 'a -> 'a{\char125};\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   359
\hspace*{0pt}val mult = {\char35}mult :~'a semigroup -> 'a -> 'a -> 'a;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   360
\hspace*{0pt}\\
37428
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   361
\hspace*{0pt}type 'a monoid = {\char123}monoid{\char95}semigroup :~'a semigroup,~neutral :~'a{\char125};\\
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   362
\hspace*{0pt}val monoid{\char95}semigroup = {\char35}monoid{\char95}semigroup :~'a monoid -> 'a semigroup;\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   363
\hspace*{0pt}val neutral = {\char35}neutral :~'a monoid -> 'a;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   364
\hspace*{0pt}\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   365
\hspace*{0pt}fun pow A{\char95}~Zero{\char95}nat a = neutral A{\char95}\\
37428
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   366
\hspace*{0pt} ~| pow A{\char95}~(Suc n) a = mult (monoid{\char95}semigroup A{\char95}) a (pow A{\char95}~n a);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   367
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   368
\hspace*{0pt}fun plus{\char95}nat (Suc m) n = plus{\char95}nat m (Suc n)\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   369
\hspace*{0pt} ~| plus{\char95}nat Zero{\char95}nat n = n;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   370
\hspace*{0pt}\\
33707
68841fb382e0 dropped obsolete documentation; updated generated sources
haftmann
parents: 32000
diff changeset
   371
\hspace*{0pt}val neutral{\char95}nat :~nat = Suc Zero{\char95}nat;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   372
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   373
\hspace*{0pt}fun mult{\char95}nat Zero{\char95}nat n = Zero{\char95}nat\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   374
\hspace*{0pt} ~| mult{\char95}nat (Suc m) n = plus{\char95}nat n (mult{\char95}nat m n);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   375
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   376
\hspace*{0pt}val semigroup{\char95}nat = {\char123}mult = mult{\char95}nat{\char125}~:~nat semigroup;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   377
\hspace*{0pt}\\
37428
b3d94253e7f2 updated generated code
haftmann
parents: 37212
diff changeset
   378
\hspace*{0pt}val monoid{\char95}nat = {\char123}monoid{\char95}semigroup = semigroup{\char95}nat,~neutral = neutral{\char95}nat{\char125}\\
31544
19b77b1de188 updated generated files;
wenzelm
parents: 31254
diff changeset
   379
\hspace*{0pt} ~:~nat monoid;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   380
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   381
\hspace*{0pt}fun bexp n = pow monoid{\char95}nat n (Suc (Suc Zero{\char95}nat));\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   382
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   383
\hspace*{0pt}end;~(*struct Example*)%
28447
haftmann
parents:
diff changeset
   384
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   385
\isamarkuptrue%
haftmann
parents:
diff changeset
   386
%
28564
haftmann
parents: 28562
diff changeset
   387
\endisatagquote
haftmann
parents: 28562
diff changeset
   388
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   389
%
28564
haftmann
parents: 28562
diff changeset
   390
\isadelimquote
28447
haftmann
parents:
diff changeset
   391
%
28564
haftmann
parents: 28562
diff changeset
   392
\endisadelimquote
28447
haftmann
parents:
diff changeset
   393
%
haftmann
parents:
diff changeset
   394
\begin{isamarkuptext}%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   395
\noindent Note the parameters with trailing underscore (\verb|A_|),
28447
haftmann
parents:
diff changeset
   396
    which are the dictionary parameters.%
haftmann
parents:
diff changeset
   397
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   398
\isamarkuptrue%
haftmann
parents:
diff changeset
   399
%
haftmann
parents:
diff changeset
   400
\isamarkupsubsection{The preprocessor \label{sec:preproc}%
haftmann
parents:
diff changeset
   401
}
haftmann
parents:
diff changeset
   402
\isamarkuptrue%
haftmann
parents:
diff changeset
   403
%
haftmann
parents:
diff changeset
   404
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   405
Before selected function theorems are turned into abstract
haftmann
parents:
diff changeset
   406
  code, a chain of definitional transformation steps is carried
haftmann
parents:
diff changeset
   407
  out: \emph{preprocessing}.  In essence, the preprocessor
haftmann
parents:
diff changeset
   408
  consists of two components: a \emph{simpset} and \emph{function transformers}.
haftmann
parents:
diff changeset
   409
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   410
  The \emph{simpset} can apply the full generality of the
32000
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   411
  Isabelle simplifier.  Due to the interpretation of theorems as code
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   412
  equations, rewrites are applied to the right hand side and the
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   413
  arguments of the left hand side of an equation, but never to the
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   414
  constant heading the left hand side.  An important special case are
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   415
  \emph{unfold theorems},  which may be declared and removed using
32000
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   416
  the \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} or \emph{\hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} del}
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   417
  attribute, respectively.
28447
haftmann
parents:
diff changeset
   418
haftmann
parents:
diff changeset
   419
  Some common applications:%
haftmann
parents:
diff changeset
   420
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   421
\isamarkuptrue%
haftmann
parents:
diff changeset
   422
%
haftmann
parents:
diff changeset
   423
\begin{itemize}
haftmann
parents:
diff changeset
   424
%
haftmann
parents:
diff changeset
   425
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   426
\item replacing non-executable constructs by executable ones:%
haftmann
parents:
diff changeset
   427
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   428
\isamarkuptrue%
haftmann
parents:
diff changeset
   429
%
28564
haftmann
parents: 28562
diff changeset
   430
\isadelimquote
28447
haftmann
parents:
diff changeset
   431
%
28564
haftmann
parents: 28562
diff changeset
   432
\endisadelimquote
28447
haftmann
parents:
diff changeset
   433
%
28564
haftmann
parents: 28562
diff changeset
   434
\isatagquote
28447
haftmann
parents:
diff changeset
   435
\isacommand{lemma}\isamarkupfalse%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   436
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   437
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ member\ xs\ x{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   438
\ {\isacharparenleft}fact\ in{\isacharunderscore}set{\isacharunderscore}code{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
   439
\endisatagquote
haftmann
parents: 28562
diff changeset
   440
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   441
%
28564
haftmann
parents: 28562
diff changeset
   442
\isadelimquote
28447
haftmann
parents:
diff changeset
   443
%
28564
haftmann
parents: 28562
diff changeset
   444
\endisadelimquote
28447
haftmann
parents:
diff changeset
   445
%
haftmann
parents:
diff changeset
   446
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   447
\item eliminating superfluous constants:%
haftmann
parents:
diff changeset
   448
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   449
\isamarkuptrue%
haftmann
parents:
diff changeset
   450
%
28564
haftmann
parents: 28562
diff changeset
   451
\isadelimquote
28447
haftmann
parents:
diff changeset
   452
%
28564
haftmann
parents: 28562
diff changeset
   453
\endisadelimquote
28447
haftmann
parents:
diff changeset
   454
%
28564
haftmann
parents: 28562
diff changeset
   455
\isatagquote
28447
haftmann
parents:
diff changeset
   456
\isacommand{lemma}\isamarkupfalse%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   457
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
28447
haftmann
parents:
diff changeset
   458
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   459
\ {\isacharparenleft}fact\ One{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
   460
\endisatagquote
haftmann
parents: 28562
diff changeset
   461
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   462
%
28564
haftmann
parents: 28562
diff changeset
   463
\isadelimquote
28447
haftmann
parents:
diff changeset
   464
%
28564
haftmann
parents: 28562
diff changeset
   465
\endisadelimquote
28447
haftmann
parents:
diff changeset
   466
%
haftmann
parents:
diff changeset
   467
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   468
\item replacing executable but inconvenient constructs:%
haftmann
parents:
diff changeset
   469
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   470
\isamarkuptrue%
haftmann
parents:
diff changeset
   471
%
28564
haftmann
parents: 28562
diff changeset
   472
\isadelimquote
28447
haftmann
parents:
diff changeset
   473
%
28564
haftmann
parents: 28562
diff changeset
   474
\endisadelimquote
28447
haftmann
parents:
diff changeset
   475
%
28564
haftmann
parents: 28562
diff changeset
   476
\isatagquote
28447
haftmann
parents:
diff changeset
   477
\isacommand{lemma}\isamarkupfalse%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   478
\ {\isacharbrackleft}code{\isacharunderscore}unfold{\isacharbrackright}{\isacharcolon}\isanewline
28447
haftmann
parents:
diff changeset
   479
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   480
\ {\isacharparenleft}fact\ empty{\isacharunderscore}null{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
   481
\endisatagquote
haftmann
parents: 28562
diff changeset
   482
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   483
%
28564
haftmann
parents: 28562
diff changeset
   484
\isadelimquote
28447
haftmann
parents:
diff changeset
   485
%
28564
haftmann
parents: 28562
diff changeset
   486
\endisadelimquote
28447
haftmann
parents:
diff changeset
   487
%
haftmann
parents:
diff changeset
   488
\end{itemize}
haftmann
parents:
diff changeset
   489
%
haftmann
parents:
diff changeset
   490
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   491
\noindent \emph{Function transformers} provide a very general interface,
haftmann
parents:
diff changeset
   492
  transforming a list of function theorems to another
haftmann
parents:
diff changeset
   493
  list of function theorems, provided that neither the heading
haftmann
parents:
diff changeset
   494
  constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
haftmann
parents:
diff changeset
   495
  pattern elimination implemented in
haftmann
parents:
diff changeset
   496
  theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this
haftmann
parents:
diff changeset
   497
  interface.
haftmann
parents:
diff changeset
   498
haftmann
parents:
diff changeset
   499
  \noindent The current setup of the preprocessor may be inspected using
31254
03a35fbc9dc6 documented print_codeproc command
haftmann
parents: 31150
diff changeset
   500
  the \hyperlink{command.print-codeproc}{\mbox{\isa{\isacommand{print{\isacharunderscore}codeproc}}}} command.
28447
haftmann
parents:
diff changeset
   501
  \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} provides a convenient
haftmann
parents:
diff changeset
   502
  mechanism to inspect the impact of a preprocessor setup
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   503
  on code equations.
28447
haftmann
parents:
diff changeset
   504
haftmann
parents:
diff changeset
   505
  \begin{warn}
32000
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   506
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   507
    Attribute \hyperlink{attribute.code-unfold}{\mbox{\isa{code{\isacharunderscore}unfold}}} also applies to the
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   508
    preprocessor of the ancient \isa{SML\ code\ generator}; in case
6f07563dc8a9 updated to changes in sources; tuned
haftmann
parents: 31848
diff changeset
   509
    this is not what you intend, use \hyperlink{attribute.code-inline}{\mbox{\isa{code{\isacharunderscore}inline}}} instead.
28447
haftmann
parents:
diff changeset
   510
  \end{warn}%
haftmann
parents:
diff changeset
   511
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   512
\isamarkuptrue%
haftmann
parents:
diff changeset
   513
%
haftmann
parents:
diff changeset
   514
\isamarkupsubsection{Datatypes \label{sec:datatypes}%
haftmann
parents:
diff changeset
   515
}
haftmann
parents:
diff changeset
   516
\isamarkuptrue%
haftmann
parents:
diff changeset
   517
%
haftmann
parents:
diff changeset
   518
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   519
Conceptually, any datatype is spanned by a set of
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   520
  \emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n} where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is exactly the set of \emph{all} type variables in
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   521
  \isa{{\isasymtau}}.  The HOL datatype package by default registers any new
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   522
  datatype in the table of datatypes, which may be inspected using the
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   523
  \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
28447
haftmann
parents:
diff changeset
   524
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   525
  In some cases, it is appropriate to alter or extend this table.  As
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   526
  an example, we will develop an alternative representation of the
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   527
  queue example given in \secref{sec:intro}.  The amortised
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   528
  representation is convenient for generating code but exposes its
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   529
  \qt{implementation} details, which may be cumbersome when proving
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   530
  theorems about it.  Therefore, here is a simple, straightforward
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   531
  representation of queues:%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   532
\end{isamarkuptext}%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   533
\isamarkuptrue%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   534
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   535
\isadelimquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   536
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   537
\endisadelimquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   538
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   539
\isatagquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   540
\isacommand{datatype}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   541
\ {\isacharprime}a\ queue\ {\isacharequal}\ Queue\ {\isachardoublequoteopen}{\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   542
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   543
\isacommand{definition}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   544
\ empty\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   545
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   546
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   547
\isacommand{primrec}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   548
\ enqueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   549
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}Queue\ xs{\isacharparenright}\ {\isacharequal}\ Queue\ {\isacharparenleft}xs\ {\isacharat}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   550
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   551
\isacommand{fun}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   552
\ dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ option\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   553
\ \ \ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   554
\ \ {\isacharbar}\ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ x{\isacharcomma}\ Queue\ xs{\isacharparenright}{\isachardoublequoteclose}%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   555
\endisatagquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   556
{\isafoldquote}%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   557
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   558
\isadelimquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   559
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   560
\endisadelimquote
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   561
%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   562
\begin{isamarkuptext}%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   563
\noindent This we can use directly for proving;  for executing,
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   564
  we provide an alternative characterisation:%
28447
haftmann
parents:
diff changeset
   565
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   566
\isamarkuptrue%
haftmann
parents:
diff changeset
   567
%
28564
haftmann
parents: 28562
diff changeset
   568
\isadelimquote
28447
haftmann
parents:
diff changeset
   569
%
28564
haftmann
parents: 28562
diff changeset
   570
\endisadelimquote
28447
haftmann
parents:
diff changeset
   571
%
28564
haftmann
parents: 28562
diff changeset
   572
\isatagquote
28447
haftmann
parents:
diff changeset
   573
\isacommand{definition}\isamarkupfalse%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   574
\ AQueue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   575
\ \ {\isachardoublequoteopen}AQueue\ xs\ ys\ {\isacharequal}\ Queue\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
28447
haftmann
parents:
diff changeset
   576
\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   577
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   578
\ AQueue%
28564
haftmann
parents: 28562
diff changeset
   579
\endisatagquote
haftmann
parents: 28562
diff changeset
   580
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   581
%
28564
haftmann
parents: 28562
diff changeset
   582
\isadelimquote
28447
haftmann
parents:
diff changeset
   583
%
28564
haftmann
parents: 28562
diff changeset
   584
\endisadelimquote
28447
haftmann
parents:
diff changeset
   585
%
haftmann
parents:
diff changeset
   586
\begin{isamarkuptext}%
30227
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   587
\noindent Here we define a \qt{constructor} \isa{AQueue} which
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   588
  is defined in terms of \isa{Queue} and interprets its arguments
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   589
  according to what the \emph{content} of an amortised queue is supposed
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   590
  to be.  Equipped with this, we are able to prove the following equations
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   591
  for our primitive queue operations which \qt{implement} the simple
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   592
  queues in an amortised fashion:%
28447
haftmann
parents:
diff changeset
   593
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   594
\isamarkuptrue%
haftmann
parents:
diff changeset
   595
%
28564
haftmann
parents: 28562
diff changeset
   596
\isadelimquote
28447
haftmann
parents:
diff changeset
   597
%
28564
haftmann
parents: 28562
diff changeset
   598
\endisadelimquote
28447
haftmann
parents:
diff changeset
   599
%
28564
haftmann
parents: 28562
diff changeset
   600
\isatagquote
28447
haftmann
parents:
diff changeset
   601
\isacommand{lemma}\isamarkupfalse%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   602
\ empty{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   603
\ \ {\isachardoublequoteopen}empty\ {\isacharequal}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   604
\ \ \isacommand{unfolding}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   605
\ AQueue{\isacharunderscore}def\ empty{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   606
\ simp\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   607
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   608
\isacommand{lemma}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   609
\ enqueue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   610
\ \ {\isachardoublequoteopen}enqueue\ x\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ AQueue\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   611
\ \ \isacommand{unfolding}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   612
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   613
\ simp\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   614
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   615
\isacommand{lemma}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   616
\ dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   617
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   618
\ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   619
\ \ \ \ else\ dequeue\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   620
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}Some\ y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   621
\ \ \isacommand{unfolding}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   622
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   623
\ simp{\isacharunderscore}all%
28564
haftmann
parents: 28562
diff changeset
   624
\endisatagquote
haftmann
parents: 28562
diff changeset
   625
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   626
%
28564
haftmann
parents: 28562
diff changeset
   627
\isadelimquote
28447
haftmann
parents:
diff changeset
   628
%
28564
haftmann
parents: 28562
diff changeset
   629
\endisadelimquote
28447
haftmann
parents:
diff changeset
   630
%
haftmann
parents:
diff changeset
   631
\begin{isamarkuptext}%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   632
\noindent For completeness, we provide a substitute for the
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   633
  \isa{case} combinator on queues:%
28447
haftmann
parents:
diff changeset
   634
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   635
\isamarkuptrue%
haftmann
parents:
diff changeset
   636
%
28564
haftmann
parents: 28562
diff changeset
   637
\isadelimquote
28447
haftmann
parents:
diff changeset
   638
%
28564
haftmann
parents: 28562
diff changeset
   639
\endisadelimquote
28447
haftmann
parents:
diff changeset
   640
%
28564
haftmann
parents: 28562
diff changeset
   641
\isatagquote
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   642
\isacommand{lemma}\isamarkupfalse%
30227
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   643
\ queue{\isacharunderscore}case{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   644
\ \ {\isachardoublequoteopen}queue{\isacharunderscore}case\ f\ {\isacharparenleft}AQueue\ xs\ ys{\isacharparenright}\ {\isacharequal}\ f\ {\isacharparenleft}ys\ {\isacharat}\ rev\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   645
\ \ \isacommand{unfolding}\isamarkupfalse%
30227
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   646
\ AQueue{\isacharunderscore}def\ \isacommand{by}\isamarkupfalse%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   647
\ simp%
28564
haftmann
parents: 28562
diff changeset
   648
\endisatagquote
haftmann
parents: 28562
diff changeset
   649
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   650
%
28564
haftmann
parents: 28562
diff changeset
   651
\isadelimquote
28447
haftmann
parents:
diff changeset
   652
%
28564
haftmann
parents: 28562
diff changeset
   653
\endisadelimquote
28447
haftmann
parents:
diff changeset
   654
%
haftmann
parents:
diff changeset
   655
\begin{isamarkuptext}%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   656
\noindent The resulting code looks as expected:%
28447
haftmann
parents:
diff changeset
   657
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   658
\isamarkuptrue%
haftmann
parents:
diff changeset
   659
%
28564
haftmann
parents: 28562
diff changeset
   660
\isadelimquote
28447
haftmann
parents:
diff changeset
   661
%
28564
haftmann
parents: 28562
diff changeset
   662
\endisadelimquote
28447
haftmann
parents:
diff changeset
   663
%
28564
haftmann
parents: 28562
diff changeset
   664
\isatagquote
28447
haftmann
parents:
diff changeset
   665
%
haftmann
parents:
diff changeset
   666
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   667
\isatypewriter%
28447
haftmann
parents:
diff changeset
   668
\noindent%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   669
\hspace*{0pt}structure Example :~sig\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   670
\hspace*{0pt} ~val foldl :~('a -> 'b -> 'a) -> 'a -> 'b list -> 'a\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   671
\hspace*{0pt} ~val rev :~'a list -> 'a list\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   672
\hspace*{0pt} ~val null :~'a list -> bool\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   673
\hspace*{0pt} ~datatype 'a queue = AQueue of 'a list * 'a list\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   674
\hspace*{0pt} ~val empty :~'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   675
\hspace*{0pt} ~val dequeue :~'a queue -> 'a option * 'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   676
\hspace*{0pt} ~val enqueue :~'a -> 'a queue -> 'a queue\\
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   677
\hspace*{0pt}end = struct\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   678
\hspace*{0pt}\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   679
\hspace*{0pt}fun foldl f a [] = a\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   680
\hspace*{0pt} ~| foldl f a (x ::~xs) = foldl f (f a x) xs;\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   681
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   682
\hspace*{0pt}fun rev xs = foldl (fn xsa => fn x => x ::~xsa) [] xs;\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   683
\hspace*{0pt}\\
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   684
\hspace*{0pt}fun null [] = true\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   685
\hspace*{0pt} ~| null (x ::~xs) = false;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   686
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   687
\hspace*{0pt}datatype 'a queue = AQueue of 'a list * 'a list;\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   688
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   689
\hspace*{0pt}val empty :~'a queue = AQueue ([],~[]);\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   690
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   691
\hspace*{0pt}fun dequeue (AQueue (xs,~y ::~ys)) = (SOME y,~AQueue (xs,~ys))\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   692
\hspace*{0pt} ~| dequeue (AQueue (xs,~[])) =\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   693
\hspace*{0pt} ~~~(if null xs then (NONE,~AQueue ([],~[]))\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   694
\hspace*{0pt} ~~~~~else dequeue (AQueue ([],~rev xs)));\\
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   695
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   696
\hspace*{0pt}fun enqueue x (AQueue (xs,~ys)) = AQueue (x ::~xs,~ys);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   697
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   698
\hspace*{0pt}end;~(*struct Example*)%
28447
haftmann
parents:
diff changeset
   699
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   700
\isamarkuptrue%
haftmann
parents:
diff changeset
   701
%
28564
haftmann
parents: 28562
diff changeset
   702
\endisatagquote
haftmann
parents: 28562
diff changeset
   703
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   704
%
28564
haftmann
parents: 28562
diff changeset
   705
\isadelimquote
28447
haftmann
parents:
diff changeset
   706
%
28564
haftmann
parents: 28562
diff changeset
   707
\endisadelimquote
28447
haftmann
parents:
diff changeset
   708
%
haftmann
parents:
diff changeset
   709
\begin{isamarkuptext}%
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   710
\noindent From this example, it can be glimpsed that using own
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   711
  constructor sets is a little delicate since it changes the set of
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   712
  valid patterns for values of that type.  Without going into much
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   713
  detail, here some practical hints:
28447
haftmann
parents:
diff changeset
   714
haftmann
parents:
diff changeset
   715
  \begin{itemize}
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   716
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   717
    \item When changing the constructor set for datatypes, take care
30227
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   718
      to provide alternative equations for the \isa{case} combinator.
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   719
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   720
    \item Values in the target language need not to be normalised --
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   721
      different values in the target language may represent the same
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   722
      value in the logic.
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   723
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   724
    \item Usually, a good methodology to deal with the subtleties of
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   725
      pattern matching is to see the type as an abstract type: provide
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   726
      a set of operations which operate on the concrete representation
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   727
      of the type, and derive further operations by combinations of
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   728
      these primitive ones, without relying on a particular
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   729
      representation.
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   730
28447
haftmann
parents:
diff changeset
   731
  \end{itemize}%
haftmann
parents:
diff changeset
   732
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   733
\isamarkuptrue%
haftmann
parents:
diff changeset
   734
%
30938
c6c9359e474c wellsortedness is no issue for a user manual any more
haftmann
parents: 30227
diff changeset
   735
\isamarkupsubsection{Equality%
28447
haftmann
parents:
diff changeset
   736
}
haftmann
parents:
diff changeset
   737
\isamarkuptrue%
haftmann
parents:
diff changeset
   738
%
haftmann
parents:
diff changeset
   739
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   740
Surely you have already noticed how equality is treated
haftmann
parents:
diff changeset
   741
  by the code generator:%
haftmann
parents:
diff changeset
   742
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   743
\isamarkuptrue%
haftmann
parents:
diff changeset
   744
%
28564
haftmann
parents: 28562
diff changeset
   745
\isadelimquote
28447
haftmann
parents:
diff changeset
   746
%
28564
haftmann
parents: 28562
diff changeset
   747
\endisadelimquote
28447
haftmann
parents:
diff changeset
   748
%
28564
haftmann
parents: 28562
diff changeset
   749
\isatagquote
28447
haftmann
parents:
diff changeset
   750
\isacommand{primrec}\isamarkupfalse%
haftmann
parents:
diff changeset
   751
\ 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
parents:
diff changeset
   752
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
haftmann
parents:
diff changeset
   753
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
haftmann
parents:
diff changeset
   754
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
haftmann
parents:
diff changeset
   755
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
haftmann
parents:
diff changeset
   756
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
haftmann
parents:
diff changeset
   757
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
28564
haftmann
parents: 28562
diff changeset
   758
\endisatagquote
haftmann
parents: 28562
diff changeset
   759
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   760
%
28564
haftmann
parents: 28562
diff changeset
   761
\isadelimquote
28447
haftmann
parents:
diff changeset
   762
%
28564
haftmann
parents: 28562
diff changeset
   763
\endisadelimquote
28447
haftmann
parents:
diff changeset
   764
%
haftmann
parents:
diff changeset
   765
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   766
\noindent The membership test during preprocessing is rewritten,
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   767
  resulting in \isa{member}, which itself
28447
haftmann
parents:
diff changeset
   768
  performs an explicit equality check.%
haftmann
parents:
diff changeset
   769
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   770
\isamarkuptrue%
haftmann
parents:
diff changeset
   771
%
28564
haftmann
parents: 28562
diff changeset
   772
\isadelimquote
28447
haftmann
parents:
diff changeset
   773
%
28564
haftmann
parents: 28562
diff changeset
   774
\endisadelimquote
28447
haftmann
parents:
diff changeset
   775
%
28564
haftmann
parents: 28562
diff changeset
   776
\isatagquote
28447
haftmann
parents:
diff changeset
   777
%
haftmann
parents:
diff changeset
   778
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   779
\isatypewriter%
28447
haftmann
parents:
diff changeset
   780
\noindent%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   781
\hspace*{0pt}structure Example :~sig\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   782
\hspace*{0pt} ~type 'a eq\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   783
\hspace*{0pt} ~val eq :~'a eq -> 'a -> 'a -> bool\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   784
\hspace*{0pt} ~val eqa :~'a eq -> 'a -> 'a -> bool\\
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   785
\hspace*{0pt} ~val member :~'a eq -> 'a list -> 'a -> bool\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   786
\hspace*{0pt} ~val collect{\char95}duplicates :\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   787
\hspace*{0pt} ~~~'a eq -> 'a list -> 'a list -> 'a list -> 'a list\\
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   788
\hspace*{0pt}end = struct\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   789
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   790
\hspace*{0pt}type 'a eq = {\char123}eq :~'a -> 'a -> bool{\char125};\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   791
\hspace*{0pt}val eq = {\char35}eq :~'a eq -> 'a -> 'a -> bool;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   792
\hspace*{0pt}\\
31045
f0c7607bb295 updated generated file
haftmann
parents: 30938
diff changeset
   793
\hspace*{0pt}fun eqa A{\char95}~a b = eq A{\char95}~a b;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   794
\hspace*{0pt}\\
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   795
\hspace*{0pt}fun member A{\char95}~[] y = false\\
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   796
\hspace*{0pt} ~| member A{\char95}~(x ::~xs) y = eqa A{\char95}~x y orelse member A{\char95}~xs y;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   797
\hspace*{0pt}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   798
\hspace*{0pt}fun collect{\char95}duplicates A{\char95}~xs ys [] = xs\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   799
\hspace*{0pt} ~| collect{\char95}duplicates A{\char95}~xs ys (z ::~zs) =\\
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   800
\hspace*{0pt} ~~~(if member A{\char95}~xs z\\
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   801
\hspace*{0pt} ~~~~~then (if member A{\char95}~ys z then collect{\char95}duplicates A{\char95}~xs ys zs\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   802
\hspace*{0pt} ~~~~~~~~~~~~else collect{\char95}duplicates A{\char95}~xs (z ::~ys) zs)\\
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   803
\hspace*{0pt} ~~~~~else collect{\char95}duplicates A{\char95}~(z ::~xs) (z ::~ys) zs);\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   804
\hspace*{0pt}\\
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   805
\hspace*{0pt}end;~(*struct Example*)%
28447
haftmann
parents:
diff changeset
   806
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   807
\isamarkuptrue%
haftmann
parents:
diff changeset
   808
%
28564
haftmann
parents: 28562
diff changeset
   809
\endisatagquote
haftmann
parents: 28562
diff changeset
   810
{\isafoldquote}%
28447
haftmann
parents:
diff changeset
   811
%
28564
haftmann
parents: 28562
diff changeset
   812
\isadelimquote
28447
haftmann
parents:
diff changeset
   813
%
28564
haftmann
parents: 28562
diff changeset
   814
\endisadelimquote
28447
haftmann
parents:
diff changeset
   815
%
haftmann
parents:
diff changeset
   816
\begin{isamarkuptext}%
haftmann
parents:
diff changeset
   817
\noindent Obviously, polymorphic equality is implemented the Haskell
haftmann
parents:
diff changeset
   818
  way using a type class.  How is this achieved?  HOL introduces
haftmann
parents:
diff changeset
   819
  an explicit class \isa{eq} with a corresponding operation
haftmann
parents:
diff changeset
   820
  \isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharequal}\ op\ {\isacharequal}}.
haftmann
parents:
diff changeset
   821
  The preprocessing framework does the rest by propagating the
29560
fa6c5d62adf5 "code equation" replaces "defining equation"
haftmann
parents: 29297
diff changeset
   822
  \isa{eq} constraints through all dependent code equations.
28447
haftmann
parents:
diff changeset
   823
  For datatypes, instances of \isa{eq} are implicitly derived
haftmann
parents:
diff changeset
   824
  when possible.  For other types, you may instantiate \isa{eq}
33707
68841fb382e0 dropped obsolete documentation; updated generated sources
haftmann
parents: 32000
diff changeset
   825
  manually like any other type class.%
28447
haftmann
parents:
diff changeset
   826
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   827
\isamarkuptrue%
haftmann
parents:
diff changeset
   828
%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   829
\isamarkupsubsection{Explicit partiality%
28447
haftmann
parents:
diff changeset
   830
}
haftmann
parents:
diff changeset
   831
\isamarkuptrue%
haftmann
parents:
diff changeset
   832
%
haftmann
parents:
diff changeset
   833
\begin{isamarkuptext}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   834
Partiality usually enters the game by partial patterns, as
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   835
  in the following example, again for amortised queues:%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   836
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   837
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   838
%
28564
haftmann
parents: 28562
diff changeset
   839
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   840
%
28564
haftmann
parents: 28562
diff changeset
   841
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   842
%
28564
haftmann
parents: 28562
diff changeset
   843
\isatagquote
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   844
\isacommand{definition}\isamarkupfalse%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   845
\ strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   846
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ q\ {\isacharequal}\ {\isacharparenleft}case\ dequeue\ q\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   847
\ \ \ \ of\ {\isacharparenleft}Some\ x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}x{\isacharcomma}\ q{\isacharprime}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   848
\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   849
\isacommand{lemma}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   850
\ strict{\isacharunderscore}dequeue{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   851
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   852
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   853
\ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}y{\isacharcomma}\ AQueue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   854
\ \ \isacommand{by}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   855
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharunderscore}def\ dequeue{\isacharunderscore}AQueue\ split{\isacharcolon}\ list{\isachardot}splits{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
   856
\endisatagquote
haftmann
parents: 28562
diff changeset
   857
{\isafoldquote}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   858
%
28564
haftmann
parents: 28562
diff changeset
   859
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   860
%
28564
haftmann
parents: 28562
diff changeset
   861
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   862
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   863
\begin{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   864
\noindent In the corresponding code, there is no equation
30227
853abb4853cc tuned manuals
haftmann
parents: 30226
diff changeset
   865
  for the pattern \isa{AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}}:%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   866
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   867
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   868
%
28564
haftmann
parents: 28562
diff changeset
   869
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   870
%
28564
haftmann
parents: 28562
diff changeset
   871
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   872
%
28564
haftmann
parents: 28562
diff changeset
   873
\isatagquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   874
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   875
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   876
\isatypewriter%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   877
\noindent%
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   878
\hspace*{0pt}strict{\char95}dequeue ::~forall a.~Queue a -> (a,~Queue a);\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   879
\hspace*{0pt}strict{\char95}dequeue (AQueue xs []) =\\
31848
e5ab21d14974 updated generated document
haftmann
parents: 31544
diff changeset
   880
\hspace*{0pt} ~let {\char123}\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   881
\hspace*{0pt} ~~~(y :~ys) = rev xs;\\
31848
e5ab21d14974 updated generated document
haftmann
parents: 31544
diff changeset
   882
\hspace*{0pt} ~{\char125}~in (y,~AQueue [] ys);\\
34179
5490151d1052 updated generated document sources
haftmann
parents: 34155
diff changeset
   883
\hspace*{0pt}strict{\char95}dequeue (AQueue xs (y :~ys)) = (y,~AQueue xs ys);%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   884
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   885
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   886
%
28564
haftmann
parents: 28562
diff changeset
   887
\endisatagquote
haftmann
parents: 28562
diff changeset
   888
{\isafoldquote}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   889
%
28564
haftmann
parents: 28562
diff changeset
   890
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   891
%
28564
haftmann
parents: 28562
diff changeset
   892
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   893
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   894
\begin{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   895
\noindent In some cases it is desirable to have this
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   896
  pseudo-\qt{partiality} more explicitly, e.g.~as follows:%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   897
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   898
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   899
%
28564
haftmann
parents: 28562
diff changeset
   900
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   901
%
28564
haftmann
parents: 28562
diff changeset
   902
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   903
%
28564
haftmann
parents: 28562
diff changeset
   904
\isatagquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   905
\isacommand{axiomatization}\isamarkupfalse%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   906
\ empty{\isacharunderscore}queue\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   907
\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   908
\isacommand{definition}\isamarkupfalse%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   909
\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   910
\ \ {\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
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   911
\isanewline
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   912
\isacommand{lemma}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   913
\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}AQueue\ {\isacharbrackleft}code{\isacharbrackright}{\isacharcolon}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   914
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ empty{\isacharunderscore}queue\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   915
\ \ \ \ \ else\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   916
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}AQueue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   917
\ \ \ \ \ {\isacharparenleft}y{\isacharcomma}\ AQueue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   918
\ \ \isacommand{by}\isamarkupfalse%
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   919
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ strict{\isacharunderscore}dequeue{\isacharprime}{\isacharunderscore}def\ dequeue{\isacharunderscore}AQueue\ split{\isacharcolon}\ list{\isachardot}splits{\isacharparenright}%
28564
haftmann
parents: 28562
diff changeset
   920
\endisatagquote
haftmann
parents: 28562
diff changeset
   921
{\isafoldquote}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   922
%
28564
haftmann
parents: 28562
diff changeset
   923
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   924
%
28564
haftmann
parents: 28562
diff changeset
   925
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   926
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   927
\begin{isamarkuptext}%
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   928
Observe that on the right hand side of the definition of \isa{strict{\isacharunderscore}dequeue{\isacharprime}}, the unspecified constant \isa{empty{\isacharunderscore}queue} occurs.
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   929
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   930
  Normally, if constants without any code equations occur in a
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   931
  program, the code generator complains (since in most cases this is
34155
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   932
  indeed an error).  But such constants can also be thought
14aaccb399b3 Polishing up the English
paulson
parents: 33926
diff changeset
   933
  of as function definitions which always fail,
29798
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   934
  since there is never a successful pattern match on the left hand
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   935
  side.  In order to categorise a constant into that category
6df726203e39 proper datatype abstraction example
haftmann
parents: 29560
diff changeset
   936
  explicitly, use \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}:%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   937
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   938
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   939
%
28564
haftmann
parents: 28562
diff changeset
   940
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   941
%
28564
haftmann
parents: 28562
diff changeset
   942
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   943
%
28564
haftmann
parents: 28562
diff changeset
   944
\isatagquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   945
\isacommand{code{\isacharunderscore}abort}\isamarkupfalse%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   946
\ empty{\isacharunderscore}queue%
28564
haftmann
parents: 28562
diff changeset
   947
\endisatagquote
haftmann
parents: 28562
diff changeset
   948
{\isafoldquote}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   949
%
28564
haftmann
parents: 28562
diff changeset
   950
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   951
%
28564
haftmann
parents: 28562
diff changeset
   952
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   953
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   954
\begin{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   955
\noindent Then the code generator will just insert an error or
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   956
  exception at the appropriate position:%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   957
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   958
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   959
%
28564
haftmann
parents: 28562
diff changeset
   960
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   961
%
28564
haftmann
parents: 28562
diff changeset
   962
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   963
%
28564
haftmann
parents: 28562
diff changeset
   964
\isatagquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   965
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   966
\begin{isamarkuptext}%
28727
185110a4b97a clarified verbatim vs. typewriter
haftmann
parents: 28714
diff changeset
   967
\isatypewriter%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   968
\noindent%
29297
62e0f892e525 updated generated files;
wenzelm
parents: 28727
diff changeset
   969
\hspace*{0pt}empty{\char95}queue ::~forall a.~a;\\
28714
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   970
\hspace*{0pt}empty{\char95}queue = error {\char34}empty{\char95}queue{\char34};\\
1992553cccfe improved verbatim mechanism
haftmann
parents: 28593
diff changeset
   971
\hspace*{0pt}\\
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   972
\hspace*{0pt}strict{\char95}dequeue ::~forall a.~Queue a -> (a,~Queue a);\\
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   973
\hspace*{0pt}strict{\char95}dequeue (AQueue xs (y :~ys)) = (y,~AQueue xs ys);\\
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   974
\hspace*{0pt}strict{\char95}dequeue (AQueue xs []) =\\
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
   975
\hspace*{0pt} ~(if nulla xs then empty{\char95}queue else strict{\char95}dequeue (AQueue [] (rev xs)));%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   976
\end{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   977
\isamarkuptrue%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   978
%
28564
haftmann
parents: 28562
diff changeset
   979
\endisatagquote
haftmann
parents: 28562
diff changeset
   980
{\isafoldquote}%
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   981
%
28564
haftmann
parents: 28562
diff changeset
   982
\isadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   983
%
28564
haftmann
parents: 28562
diff changeset
   984
\endisadelimquote
28462
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   985
%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   986
\begin{isamarkuptext}%
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   987
\noindent This feature however is rarely needed in practice.
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   988
  Note also that the \isa{HOL} default setup already declares
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   989
  \isa{undefined} as \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}, which is most
6ec603695aaf added partiality section
haftmann
parents: 28456
diff changeset
   990
  likely to be used in such situations.%
28447
haftmann
parents:
diff changeset
   991
\end{isamarkuptext}%
haftmann
parents:
diff changeset
   992
\isamarkuptrue%
haftmann
parents:
diff changeset
   993
%
33926
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   994
\isamarkupsubsection{Inductive Predicates%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   995
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   996
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   997
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   998
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
   999
To execute inductive predicates, a special preprocessor, the predicate
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1000
 compiler, generates code equations from the introduction rules of the predicates.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1001
The mechanisms of this compiler are described in \cite{Berghofer-Bulwahn-Haftmann:2009:TPHOL}.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1002
Consider the simple predicate \isa{append} given by these two
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1003
introduction rules:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1004
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1005
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1006
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1007
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1008
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1009
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1010
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1011
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1012
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1013
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1014
\isa{append\ {\isacharbrackleft}{\isacharbrackright}\ ys\ ys}\\
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1015
\noindent\isa{append\ xs\ ys\ zs\ {\isasymLongrightarrow}\ append\ {\isacharparenleft}x\ {\isacharhash}\ xs{\isacharparenright}\ ys\ {\isacharparenleft}x\ {\isacharhash}\ zs{\isacharparenright}}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1016
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1017
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1018
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1019
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1020
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1021
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1022
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1023
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1024
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1025
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1026
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1027
\noindent To invoke the compiler, simply use \indexdef{}{command}{code\_pred}\hypertarget{command.code-pred}{\hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}}}:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1028
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1029
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1030
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1031
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1032
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1033
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1034
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1035
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1036
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1037
\ append\ \isacommand{{\isachardot}}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1038
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1039
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1040
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1041
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1042
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1043
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1044
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1045
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1046
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1047
\noindent The \hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}} command takes the name
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1048
of the inductive predicate and then you put a period to discharge
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1049
a trivial correctness proof. 
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1050
The compiler infers possible modes 
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1051
for the predicate and produces the derived code equations.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1052
Modes annotate which (parts of the) arguments are to be taken as input,
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1053
and which output. Modes are similar to types, but use the notation \isa{i}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1054
for input and \isa{o} for output.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1055
 
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1056
For \isa{append}, the compiler can infer the following modes:
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1057
\begin{itemize}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1058
\item \isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1059
\item \isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1060
\item \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1061
\end{itemize}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1062
You can compute sets of predicates using \indexdef{}{command}{values}\hypertarget{command.values}{\hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}}}:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1063
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1064
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1065
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1066
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1067
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1068
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1069
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1070
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1071
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1072
\ {\isachardoublequoteopen}{\isacharbraceleft}zs{\isachardot}\ append\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{2}}{\isacharcomma}{\isadigit{3}}{\isacharbrackright}\ {\isacharbrackleft}{\isadigit{4}}{\isacharcomma}{\isadigit{5}}{\isacharbrackright}\ zs{\isacharbraceright}{\isachardoublequoteclose}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1073
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1074
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1075
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1076
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1077
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1078
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1079
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1080
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1081
\noindent outputs \isa{{\isacharbraceleft}{\isacharbrackleft}{\isadigit{1}}{\isacharcomma}\ {\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharcomma}\ {\isadigit{4}}{\isacharcomma}\ {\isadigit{5}}{\isacharbrackright}{\isacharbraceright}}, and%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1082
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1083
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1084
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1085
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1086
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1087
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1088
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1089
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1090
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1091
\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{2}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{3}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1092
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1093
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1094
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1095
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1096
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1097
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1098
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1099
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1100
\noindent outputs \isa{{\isacharbraceleft}{\isacharparenleft}{\isacharbrackleft}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}{\isacharparenright}{\isacharcomma}\ {\isacharparenleft}{\isacharbrackleft}{\isadigit{2}}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isadigit{3}}{\isacharbrackright}{\isacharparenright}{\isacharcomma}\ {\isacharparenleft}{\isacharbrackleft}{\isadigit{2}}{\isacharcomma}\ {\isadigit{3}}{\isacharbrackright}{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}{\isacharbraceright}}.%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1101
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1102
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1103
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1104
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1105
\noindent If you are only interested in the first elements of the set
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1106
comprehension (with respect to a depth-first search on the introduction rules), you can
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1107
pass an argument to
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1108
\hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} to specify the number of elements you want:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1109
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1110
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1111
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1112
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1113
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1114
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1115
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1116
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1117
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1118
\ {\isadigit{1}}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{2}}{\isacharcomma}{\isadigit{3}}{\isacharcomma}{\isadigit{4}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1119
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1120
\ {\isadigit{3}}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardot}\ append\ xs\ ys\ {\isacharbrackleft}{\isacharparenleft}{\isadigit{1}}{\isacharcolon}{\isacharcolon}nat{\isacharparenright}{\isacharcomma}{\isadigit{2}}{\isacharcomma}{\isadigit{3}}{\isacharcomma}{\isadigit{4}}{\isacharbrackright}{\isacharbraceright}{\isachardoublequoteclose}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1121
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1122
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1123
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1124
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1125
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1126
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1127
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1128
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1129
\noindent The \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} command can only compute set
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1130
 comprehensions for which a mode has been inferred.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1131
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1132
The code equations for a predicate are made available as theorems with
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1133
 the suffix \isa{equation}, and can be inspected with:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1134
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1135
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1136
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1137
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1138
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1139
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1140
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1141
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1142
\isacommand{thm}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1143
\ append{\isachardot}equation%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1144
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1145
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1146
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1147
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1148
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1149
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1150
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1151
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1152
\noindent More advanced options are described in the following subsections.%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1153
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1154
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1155
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1156
\isamarkupsubsubsection{Alternative names for functions%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1157
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1158
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1159
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1160
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1161
By default, the functions generated from a predicate are named after the predicate with the
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1162
mode mangled into the name (e.g., \isa{append{\isacharunderscore}i{\isacharunderscore}i{\isacharunderscore}o}).
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1163
You can specify your own names as follows:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1164
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1165
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1166
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1167
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1168
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1169
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1170
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1171
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1172
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1173
\ {\isacharparenleft}modes{\isacharcolon}\ i\ {\isacharequal}{\isachargreater}\ i\ {\isacharequal}{\isachargreater}\ o\ {\isacharequal}{\isachargreater}\ bool\ as\ concat{\isacharcomma}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1174
\ \ o\ {\isacharequal}{\isachargreater}\ o\ {\isacharequal}{\isachargreater}\ i\ {\isacharequal}{\isachargreater}\ bool\ as\ split{\isacharcomma}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1175
\ \ i\ {\isacharequal}{\isachargreater}\ o\ {\isacharequal}{\isachargreater}\ i\ {\isacharequal}{\isachargreater}\ bool\ as\ suffix{\isacharparenright}\ append\ \isacommand{{\isachardot}}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1176
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1177
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1178
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1179
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1180
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1181
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1182
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1183
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1184
\isamarkupsubsubsection{Alternative introduction rules%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1185
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1186
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1187
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1188
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1189
Sometimes the introduction rules of an predicate are not executable because they contain
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1190
non-executable constants or specific modes could not be inferred.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1191
It is also possible that the introduction rules yield a function that loops forever
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1192
due to the execution in a depth-first search manner.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1193
Therefore, you can declare alternative introduction rules for predicates with the attribute
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1194
\hyperlink{attribute.code-pred-intro}{\mbox{\isa{code{\isacharunderscore}pred{\isacharunderscore}intro}}}.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1195
For example, the transitive closure is defined by:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1196
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1197
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1198
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1199
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1200
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1201
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1202
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1203
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1204
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1205
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1206
\isa{r\ a\ b\ {\isasymLongrightarrow}\ r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ a\ b}\\
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1207
\noindent \isa{{\isasymlbrakk}r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ a\ b{\isacharsemicolon}\ r\ b\ c{\isasymrbrakk}\ {\isasymLongrightarrow}\ r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ a\ c}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1208
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1209
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1210
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1211
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1212
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1213
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1214
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1215
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1216
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1217
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1218
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1219
\noindent These rules do not suit well for executing the transitive closure 
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1220
with the mode \isa{{\isacharparenleft}i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}, as the second rule will
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1221
cause an infinite loop in the recursive call.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1222
This can be avoided using the following alternative rules which are
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1223
declared to the predicate compiler by the attribute \hyperlink{attribute.code-pred-intro}{\mbox{\isa{code{\isacharunderscore}pred{\isacharunderscore}intro}}}:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1224
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1225
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1226
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1227
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1228
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1229
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1230
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1231
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1232
\isacommand{lemma}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1233
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1234
\ \ {\isachardoublequoteopen}r\ a\ b\ {\isasymLongrightarrow}\ r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ a\ b{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1235
\ \ {\isachardoublequoteopen}r\ a\ b\ {\isasymLongrightarrow}\ r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ b\ c\ {\isasymLongrightarrow}\ r\isactrlsup {\isacharplus}\isactrlsup {\isacharplus}\ a\ c{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1236
\isacommand{by}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1237
\ auto%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1238
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1239
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1240
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1241
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1242
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1243
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1244
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1245
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1246
\noindent After declaring all alternative rules for the transitive closure,
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1247
you invoke \hyperlink{command.code-pred}{\mbox{\isa{\isacommand{code{\isacharunderscore}pred}}}} as usual.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1248
As you have declared alternative rules for the predicate, you are urged to prove that these
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1249
introduction rules are complete, i.e., that you can derive an elimination rule for the
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1250
alternative rules:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1251
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1252
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1253
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1254
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1255
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1256
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1257
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1258
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1259
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1260
\ tranclp\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1261
\isacommand{proof}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1262
\ {\isacharminus}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1263
\ \ \isacommand{case}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1264
\ tranclp\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1265
\ \ \isacommand{from}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1266
\ this\ converse{\isacharunderscore}tranclpE{\isacharbrackleft}OF\ this{\isacharparenleft}{\isadigit{1}}{\isacharparenright}{\isacharbrackright}\ \isacommand{show}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1267
\ thesis\ \isacommand{by}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1268
\ metis\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1269
\isacommand{qed}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1270
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1271
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1272
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1273
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1274
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1275
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1276
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1277
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1278
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1279
\noindent Alternative rules can also be used for constants that have not
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1280
been defined inductively. For example, the lexicographic order which
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1281
is defined as:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1282
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1283
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1284
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1285
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1286
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1287
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1288
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1289
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1290
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1291
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1292
\begin{isabelle}%
37212
b8e02ce2559f updated generated files
haftmann
parents: 34179
diff changeset
  1293
lexord\ r\ {\isacharequal}\isanewline
33926
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1294
{\isacharbraceleft}{\isacharparenleft}x{\isacharcomma}\ y{\isacharparenright}{\isachardot}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1295
\isaindent{\ }{\isasymexists}a\ v{\isachardot}\ y\ {\isacharequal}\ x\ {\isacharat}\ a\ {\isacharhash}\ v\ {\isasymor}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1296
\isaindent{\ {\isasymexists}a\ v{\isachardot}\ }{\isacharparenleft}{\isasymexists}u\ a\ b\ v\ w{\isachardot}\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\ {\isasymin}\ r\ {\isasymand}\ x\ {\isacharequal}\ u\ {\isacharat}\ a\ {\isacharhash}\ v\ {\isasymand}\ y\ {\isacharequal}\ u\ {\isacharat}\ b\ {\isacharhash}\ w{\isacharparenright}{\isacharbraceright}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1297
\end{isabelle}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1298
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1299
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1300
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1301
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1302
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1303
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1304
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1305
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1306
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1307
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1308
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1309
\noindent To make it executable, you can derive the following two rules and prove the
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1310
elimination rule:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1311
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1312
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1313
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1314
\isadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1315
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1316
\endisadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1317
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1318
\isatagproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1319
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1320
\endisatagproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1321
{\isafoldproof}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1322
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1323
\isadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1324
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1325
\endisadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1326
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1327
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1328
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1329
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1330
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1331
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1332
\isacommand{lemma}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1333
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1334
\ \ {\isachardoublequoteopen}append\ xs\ {\isacharparenleft}a\ {\isacharhash}\ v{\isacharparenright}\ ys\ {\isasymLongrightarrow}\ lexord\ r\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1335
\isacommand{lemma}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1336
\ {\isacharbrackleft}code{\isacharunderscore}pred{\isacharunderscore}intro{\isacharbrackright}{\isacharcolon}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1337
\ \ {\isachardoublequoteopen}append\ u\ {\isacharparenleft}a\ {\isacharhash}\ v{\isacharparenright}\ xs\ {\isasymLongrightarrow}\ append\ u\ {\isacharparenleft}b\ {\isacharhash}\ w{\isacharparenright}\ ys\ {\isasymLongrightarrow}\ r\ {\isacharparenleft}a{\isacharcomma}\ b{\isacharparenright}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1338
\ \ {\isasymLongrightarrow}\ lexord\ r\ {\isacharparenleft}xs{\isacharcomma}\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1339
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1340
\ lexord%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1341
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1342
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1343
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1344
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1345
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1346
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1347
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1348
\isamarkupsubsubsection{Options for values%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1349
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1350
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1351
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1352
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1353
In the presence of higher-order predicates, multiple modes for some predicate could be inferred
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1354
that are not disambiguated by the pattern of the set comprehension.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1355
To disambiguate the modes for the arguments of a predicate, you can state
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1356
the modes explicitly in the \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} command. 
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1357
Consider the simple predicate \isa{succ}:%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1358
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1359
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1360
\isacommand{inductive}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1361
\ succ\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ bool{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1362
\isakeyword{where}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1363
\ \ {\isachardoublequoteopen}succ\ {\isadigit{0}}\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1364
{\isacharbar}\ {\isachardoublequoteopen}succ\ x\ y\ {\isasymLongrightarrow}\ succ\ {\isacharparenleft}Suc\ x{\isacharparenright}\ {\isacharparenleft}Suc\ y{\isacharparenright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1365
\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1366
\isacommand{code{\isacharunderscore}pred}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1367
\ succ%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1368
\isadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1369
\ %
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1370
\endisadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1371
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1372
\isatagproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1373
\isacommand{{\isachardot}}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1374
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1375
\endisatagproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1376
{\isafoldproof}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1377
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1378
\isadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1379
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1380
\endisadelimproof
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1381
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1382
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1383
\noindent For this, the predicate compiler can infer modes \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}, \isa{i\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool},
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1384
  \isa{o\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool} and \isa{i\ {\isasymRightarrow}\ i\ {\isasymRightarrow}\ bool}.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1385
The invocation of \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} \isa{{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ {\isadigit{1}}{\isadigit{0}}\ n{\isacharbraceright}} loops, as multiple
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1386
modes for the predicate \isa{succ} are possible and here the first mode \isa{o\ {\isasymRightarrow}\ o\ {\isasymRightarrow}\ bool}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1387
is chosen. To choose another mode for the argument, you can declare the mode for the argument between
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1388
the \hyperlink{command.values}{\mbox{\isa{\isacommand{values}}}} and the number of elements.%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1389
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1390
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1391
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1392
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1393
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1394
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1395
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1396
\isatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1397
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1398
\ {\isacharbrackleft}mode{\isacharcolon}\ i\ {\isacharequal}{\isachargreater}\ o\ {\isacharequal}{\isachargreater}\ bool{\isacharbrackright}\ {\isadigit{2}}{\isadigit{0}}\ {\isachardoublequoteopen}{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ {\isadigit{1}}{\isadigit{0}}\ n{\isacharbraceright}{\isachardoublequoteclose}\isanewline
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1399
\isacommand{values}\isamarkupfalse%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1400
\ {\isacharbrackleft}mode{\isacharcolon}\ o\ {\isacharequal}{\isachargreater}\ i\ {\isacharequal}{\isachargreater}\ bool{\isacharbrackright}\ {\isadigit{1}}{\isadigit{0}}\ {\isachardoublequoteopen}{\isacharbraceleft}n{\isachardot}\ tranclp\ succ\ n\ {\isadigit{1}}{\isadigit{0}}{\isacharbraceright}{\isachardoublequoteclose}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1401
\endisatagquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1402
{\isafoldquote}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1403
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1404
\isadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1405
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1406
\endisadelimquote
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1407
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1408
\isamarkupsubsubsection{Embedding into functional code within Isabelle/HOL%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1409
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1410
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1411
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1412
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1413
To embed the computation of an inductive predicate into functions that are defined in Isabelle/HOL,
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1414
you have a number of options:
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1415
\begin{itemize}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1416
\item You want to use the first-order predicate with the mode
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1417
  where all arguments are input. Then you can use the predicate directly, e.g.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1418
\begin{quote}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1419
  \isa{valid{\isacharunderscore}suffix\ ys\ zs\ {\isacharequal}}\\
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1420
  \isa{{\isacharparenleft}if\ append\ {\isacharbrackleft}Suc\ {\isadigit{0}}{\isacharcomma}\ {\isadigit{2}}{\isacharbrackright}\ ys\ zs\ then\ Some\ ys\ else\ None{\isacharparenright}}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1421
\end{quote}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1422
\item If you know that the execution returns only one value (it is deterministic), then you can
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1423
  use the combinator \isa{Predicate{\isachardot}the}, e.g., a functional concatenation of lists
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1424
  is defined with
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1425
\begin{quote}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1426
  \isa{functional{\isacharunderscore}concat\ xs\ ys\ {\isacharequal}\ Predicate{\isachardot}the\ {\isacharparenleft}append{\isacharunderscore}i{\isacharunderscore}i{\isacharunderscore}o\ xs\ ys{\isacharparenright}}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1427
\end{quote}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1428
  Note that if the evaluation does not return a unique value, it raises a run-time error
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1429
  \isa{not{\isacharunderscore}unique}.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1430
\end{itemize}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1431
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1432
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1433
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1434
\isamarkupsubsubsection{Further Examples%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1435
}
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1436
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1437
%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1438
\begin{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1439
Further examples for compiling inductive predicates can be found in
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1440
the \isa{HOL{\isacharslash}ex{\isacharslash}Predicate{\isacharunderscore}Compile{\isacharunderscore}ex} theory file.
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1441
There are also some examples in the Archive of Formal Proofs, notably
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1442
in the \isa{POPLmark{\isacharminus}deBruijn} and the \isa{FeatherweightJava} sessions.%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1443
\end{isamarkuptext}%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1444
\isamarkuptrue%
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1445
%
28447
haftmann
parents:
diff changeset
  1446
\isadelimtheory
haftmann
parents:
diff changeset
  1447
%
haftmann
parents:
diff changeset
  1448
\endisadelimtheory
haftmann
parents:
diff changeset
  1449
%
haftmann
parents:
diff changeset
  1450
\isatagtheory
haftmann
parents:
diff changeset
  1451
\isacommand{end}\isamarkupfalse%
haftmann
parents:
diff changeset
  1452
%
haftmann
parents:
diff changeset
  1453
\endisatagtheory
haftmann
parents:
diff changeset
  1454
{\isafoldtheory}%
haftmann
parents:
diff changeset
  1455
%
haftmann
parents:
diff changeset
  1456
\isadelimtheory
haftmann
parents:
diff changeset
  1457
%
haftmann
parents:
diff changeset
  1458
\endisadelimtheory
haftmann
parents:
diff changeset
  1459
\isanewline
33926
dd017d9db05f adding subsection about the predicate compiler to the code generator tutorial
bulwahn
parents: 33707
diff changeset
  1460
\end{isabellebody}%
28447
haftmann
parents:
diff changeset
  1461
%%% Local Variables:
haftmann
parents:
diff changeset
  1462
%%% mode: latex
haftmann
parents:
diff changeset
  1463
%%% TeX-master: "root"
haftmann
parents:
diff changeset
  1464
%%% End: