doc-src/IsarAdvanced/Codegen/Thy/document/Program.tex
author haftmann
Thu, 02 Oct 2008 17:18:36 +0200
changeset 28462 6ec603695aaf
parent 28456 7a558c872104
child 28562 4e74209f113e
permissions -rw-r--r--
added partiality section
haftmann@28447
     1
%
haftmann@28447
     2
\begin{isabellebody}%
haftmann@28447
     3
\def\isabellecontext{Program}%
haftmann@28447
     4
%
haftmann@28447
     5
\isadelimtheory
haftmann@28447
     6
%
haftmann@28447
     7
\endisadelimtheory
haftmann@28447
     8
%
haftmann@28447
     9
\isatagtheory
haftmann@28447
    10
\isacommand{theory}\isamarkupfalse%
haftmann@28447
    11
\ Program\isanewline
haftmann@28447
    12
\isakeyword{imports}\ Introduction\isanewline
haftmann@28447
    13
\isakeyword{begin}%
haftmann@28447
    14
\endisatagtheory
haftmann@28447
    15
{\isafoldtheory}%
haftmann@28447
    16
%
haftmann@28447
    17
\isadelimtheory
haftmann@28447
    18
%
haftmann@28447
    19
\endisadelimtheory
haftmann@28447
    20
%
haftmann@28447
    21
\isamarkupsection{Turning Theories into Programs \label{sec:program}%
haftmann@28447
    22
}
haftmann@28447
    23
\isamarkuptrue%
haftmann@28447
    24
%
haftmann@28447
    25
\isamarkupsubsection{The \isa{Isabelle{\isacharslash}HOL} default setup%
haftmann@28447
    26
}
haftmann@28447
    27
\isamarkuptrue%
haftmann@28447
    28
%
haftmann@28447
    29
\begin{isamarkuptext}%
haftmann@28447
    30
We have already seen how by default equations stemming from
haftmann@28447
    31
  \hyperlink{command.definition}{\mbox{\isa{\isacommand{definition}}}}/\hyperlink{command.primrec}{\mbox{\isa{\isacommand{primrec}}}}/\hyperlink{command.fun}{\mbox{\isa{\isacommand{fun}}}}
haftmann@28447
    32
  statements are used for code generation.  This default behaviour
haftmann@28447
    33
  can be changed, e.g. by providing different defining equations.
haftmann@28447
    34
  All kinds of customization shown in this section is \emph{safe}
haftmann@28447
    35
  in the sense that the user does not have to worry about
haftmann@28447
    36
  correctness -- all programs generatable that way are partially
haftmann@28447
    37
  correct.%
haftmann@28447
    38
\end{isamarkuptext}%
haftmann@28447
    39
\isamarkuptrue%
haftmann@28447
    40
%
haftmann@28447
    41
\isamarkupsubsection{Selecting code equations%
haftmann@28447
    42
}
haftmann@28447
    43
\isamarkuptrue%
haftmann@28447
    44
%
haftmann@28447
    45
\begin{isamarkuptext}%
haftmann@28447
    46
Coming back to our introductory example, we
haftmann@28447
    47
  could provide an alternative defining equations for \isa{dequeue}
haftmann@28447
    48
  explicitly:%
haftmann@28447
    49
\end{isamarkuptext}%
haftmann@28447
    50
\isamarkuptrue%
haftmann@28447
    51
%
haftmann@28447
    52
\isadelimquoteme
haftmann@28447
    53
%
haftmann@28447
    54
\endisadelimquoteme
haftmann@28447
    55
%
haftmann@28447
    56
\isatagquoteme
haftmann@28447
    57
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
    58
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
    59
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
haftmann@28456
    60
\ \ \ \ \ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ {\isacharparenleft}None{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\isanewline
haftmann@28456
    61
\ \ \ \ \ \ \ else\ dequeue\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
    62
\ \ {\isachardoublequoteopen}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
haftmann@28447
    63
\ \ \ \ \ {\isacharparenleft}Some\ y{\isacharcomma}\ Queue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
    64
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
    65
\ {\isacharparenleft}cases\ xs{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}\ {\isacharparenleft}cases\ {\isachardoublequoteopen}rev\ xs{\isachardoublequoteclose}{\isacharcomma}\ simp{\isacharunderscore}all{\isacharparenright}%
haftmann@28447
    66
\endisatagquoteme
haftmann@28447
    67
{\isafoldquoteme}%
haftmann@28447
    68
%
haftmann@28447
    69
\isadelimquoteme
haftmann@28447
    70
%
haftmann@28447
    71
\endisadelimquoteme
haftmann@28447
    72
%
haftmann@28447
    73
\begin{isamarkuptext}%
haftmann@28447
    74
\noindent The annotation \isa{{\isacharbrackleft}code\ func{\isacharbrackright}} is an \isa{Isar}
haftmann@28447
    75
  \isa{attribute} which states that the given theorems should be
haftmann@28447
    76
  considered as defining equations for a \isa{fun} statement --
haftmann@28447
    77
  the corresponding constant is determined syntactically.  The resulting code:%
haftmann@28447
    78
\end{isamarkuptext}%
haftmann@28447
    79
\isamarkuptrue%
haftmann@28447
    80
%
haftmann@28447
    81
\isadelimquoteme
haftmann@28447
    82
%
haftmann@28447
    83
\endisadelimquoteme
haftmann@28447
    84
%
haftmann@28447
    85
\isatagquoteme
haftmann@28447
    86
%
haftmann@28447
    87
\begin{isamarkuptext}%
haftmann@28447
    88
\isaverbatim%
haftmann@28447
    89
\noindent%
haftmann@28447
    90
\verb|dequeue :: forall a. Queue a -> (Maybe a, Queue a);|\newline%
haftmann@28447
    91
\verb|dequeue (Queue xs (y : ys)) = (Just y, Queue xs ys);|\newline%
haftmann@28447
    92
\verb|dequeue (Queue xs []) =|\newline%
haftmann@28447
    93
\verb|  (if nulla xs then (Nothing, Queue [] [])|\newline%
haftmann@28456
    94
\verb|    else dequeue (Queue [] (rev xs)));|%
haftmann@28447
    95
\end{isamarkuptext}%
haftmann@28447
    96
\isamarkuptrue%
haftmann@28447
    97
%
haftmann@28447
    98
\endisatagquoteme
haftmann@28447
    99
{\isafoldquoteme}%
haftmann@28447
   100
%
haftmann@28447
   101
\isadelimquoteme
haftmann@28447
   102
%
haftmann@28447
   103
\endisadelimquoteme
haftmann@28447
   104
%
haftmann@28447
   105
\begin{isamarkuptext}%
haftmann@28447
   106
\noindent You may note that the equality test \isa{xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} has been
haftmann@28447
   107
  replaced by the predicate \isa{null\ xs}.  This is due to the default
haftmann@28447
   108
  setup in the \qn{preprocessor} to be discussed further below (\secref{sec:preproc}).
haftmann@28447
   109
haftmann@28447
   110
  Changing the default constructor set of datatypes is also
haftmann@28447
   111
  possible but rarely desired in practice.  See \secref{sec:datatypes} for an example.
haftmann@28447
   112
haftmann@28447
   113
  As told in \secref{sec:concept}, code generation is based
haftmann@28447
   114
  on a structured collection of code theorems.
haftmann@28447
   115
  For explorative purpose, this collection
haftmann@28447
   116
  may be inspected using the \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} command:%
haftmann@28447
   117
\end{isamarkuptext}%
haftmann@28447
   118
\isamarkuptrue%
haftmann@28447
   119
%
haftmann@28447
   120
\isadelimquoteme
haftmann@28447
   121
%
haftmann@28447
   122
\endisadelimquoteme
haftmann@28447
   123
%
haftmann@28447
   124
\isatagquoteme
haftmann@28447
   125
\isacommand{code{\isacharunderscore}thms}\isamarkupfalse%
haftmann@28447
   126
\ dequeue%
haftmann@28447
   127
\endisatagquoteme
haftmann@28447
   128
{\isafoldquoteme}%
haftmann@28447
   129
%
haftmann@28447
   130
\isadelimquoteme
haftmann@28447
   131
%
haftmann@28447
   132
\endisadelimquoteme
haftmann@28447
   133
%
haftmann@28447
   134
\begin{isamarkuptext}%
haftmann@28447
   135
\noindent prints a table with \emph{all} defining equations
haftmann@28447
   136
  for \isa{dequeue}, including
haftmann@28447
   137
  \emph{all} defining equations those equations depend
haftmann@28447
   138
  on recursively.
haftmann@28447
   139
  
haftmann@28447
   140
  Similarly, the \hyperlink{command.code-deps}{\mbox{\isa{\isacommand{code{\isacharunderscore}deps}}}} command shows a graph
haftmann@28447
   141
  visualising dependencies between defining equations.%
haftmann@28447
   142
\end{isamarkuptext}%
haftmann@28447
   143
\isamarkuptrue%
haftmann@28447
   144
%
haftmann@28447
   145
\isamarkupsubsection{\isa{class} and \isa{instantiation}%
haftmann@28447
   146
}
haftmann@28447
   147
\isamarkuptrue%
haftmann@28447
   148
%
haftmann@28447
   149
\begin{isamarkuptext}%
haftmann@28447
   150
Concerning type classes and code generation, let us examine an example
haftmann@28447
   151
  from abstract algebra:%
haftmann@28447
   152
\end{isamarkuptext}%
haftmann@28447
   153
\isamarkuptrue%
haftmann@28447
   154
%
haftmann@28447
   155
\isadelimquoteme
haftmann@28447
   156
%
haftmann@28447
   157
\endisadelimquoteme
haftmann@28447
   158
%
haftmann@28447
   159
\isatagquoteme
haftmann@28447
   160
\isacommand{class}\isamarkupfalse%
haftmann@28447
   161
\ semigroup\ {\isacharequal}\ type\ {\isacharplus}\isanewline
haftmann@28447
   162
\ \ \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@28447
   163
\ \ \isakeyword{assumes}\ assoc{\isacharcolon}\ {\isachardoublequoteopen}{\isacharparenleft}x\ {\isasymotimes}\ y{\isacharparenright}\ {\isasymotimes}\ z\ {\isacharequal}\ x\ {\isasymotimes}\ {\isacharparenleft}y\ {\isasymotimes}\ z{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   164
\isanewline
haftmann@28447
   165
\isacommand{class}\isamarkupfalse%
haftmann@28447
   166
\ monoid\ {\isacharequal}\ semigroup\ {\isacharplus}\isanewline
haftmann@28447
   167
\ \ \isakeyword{fixes}\ neutral\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\ {\isacharparenleft}{\isachardoublequoteopen}{\isasymone}{\isachardoublequoteclose}{\isacharparenright}\isanewline
haftmann@28447
   168
\ \ \isakeyword{assumes}\ neutl{\isacharcolon}\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ x\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
haftmann@28447
   169
\ \ \ \ \isakeyword{and}\ neutr{\isacharcolon}\ {\isachardoublequoteopen}x\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
haftmann@28447
   170
\isanewline
haftmann@28447
   171
\isacommand{instantiation}\isamarkupfalse%
haftmann@28447
   172
\ nat\ {\isacharcolon}{\isacharcolon}\ monoid\isanewline
haftmann@28447
   173
\isakeyword{begin}\isanewline
haftmann@28447
   174
\isanewline
haftmann@28447
   175
\isacommand{primrec}\isamarkupfalse%
haftmann@28447
   176
\ mult{\isacharunderscore}nat\ \isakeyword{where}\isanewline
haftmann@28447
   177
\ \ \ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isasymotimes}\ n\ {\isacharequal}\ {\isacharparenleft}{\isadigit{0}}{\isasymColon}nat{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   178
\ \ {\isacharbar}\ {\isachardoublequoteopen}Suc\ m\ {\isasymotimes}\ n\ {\isacharequal}\ n\ {\isacharplus}\ m\ {\isasymotimes}\ n{\isachardoublequoteclose}\isanewline
haftmann@28447
   179
\isanewline
haftmann@28447
   180
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   181
\ neutral{\isacharunderscore}nat\ \isakeyword{where}\isanewline
haftmann@28447
   182
\ \ {\isachardoublequoteopen}{\isasymone}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   183
\isanewline
haftmann@28447
   184
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   185
\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharcolon}\isanewline
haftmann@28447
   186
\ \ \isakeyword{fixes}\ n\ m\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann@28447
   187
\ \ \isakeyword{shows}\ {\isachardoublequoteopen}{\isacharparenleft}n\ {\isacharplus}\ m{\isacharparenright}\ {\isasymotimes}\ q\ {\isacharequal}\ n\ {\isasymotimes}\ q\ {\isacharplus}\ m\ {\isasymotimes}\ q{\isachardoublequoteclose}\isanewline
haftmann@28447
   188
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   189
\ {\isacharparenleft}induct\ n{\isacharparenright}\ simp{\isacharunderscore}all\isanewline
haftmann@28447
   190
\isanewline
haftmann@28447
   191
\isacommand{instance}\isamarkupfalse%
haftmann@28447
   192
\ \isacommand{proof}\isamarkupfalse%
haftmann@28447
   193
\isanewline
haftmann@28447
   194
\ \ \isacommand{fix}\isamarkupfalse%
haftmann@28447
   195
\ m\ n\ q\ {\isacharcolon}{\isacharcolon}\ nat\isanewline
haftmann@28447
   196
\ \ \isacommand{show}\isamarkupfalse%
haftmann@28447
   197
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ n\ {\isasymotimes}\ q\ {\isacharequal}\ m\ {\isasymotimes}\ {\isacharparenleft}n\ {\isasymotimes}\ q{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   198
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   199
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ add{\isacharunderscore}mult{\isacharunderscore}distrib{\isacharparenright}\isanewline
haftmann@28447
   200
\ \ \isacommand{show}\isamarkupfalse%
haftmann@28447
   201
\ {\isachardoublequoteopen}{\isasymone}\ {\isasymotimes}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@28447
   202
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   203
\ {\isacharparenleft}simp\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
haftmann@28447
   204
\ \ \isacommand{show}\isamarkupfalse%
haftmann@28447
   205
\ {\isachardoublequoteopen}m\ {\isasymotimes}\ {\isasymone}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
haftmann@28447
   206
\ \ \ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   207
\ {\isacharparenleft}induct\ m{\isacharparenright}\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ neutral{\isacharunderscore}nat{\isacharunderscore}def{\isacharparenright}\isanewline
haftmann@28447
   208
\isacommand{qed}\isamarkupfalse%
haftmann@28447
   209
\isanewline
haftmann@28447
   210
\isanewline
haftmann@28447
   211
\isacommand{end}\isamarkupfalse%
haftmann@28447
   212
%
haftmann@28447
   213
\endisatagquoteme
haftmann@28447
   214
{\isafoldquoteme}%
haftmann@28447
   215
%
haftmann@28447
   216
\isadelimquoteme
haftmann@28447
   217
%
haftmann@28447
   218
\endisadelimquoteme
haftmann@28447
   219
%
haftmann@28447
   220
\begin{isamarkuptext}%
haftmann@28447
   221
\noindent We define the natural operation of the natural numbers
haftmann@28447
   222
  on monoids:%
haftmann@28447
   223
\end{isamarkuptext}%
haftmann@28447
   224
\isamarkuptrue%
haftmann@28447
   225
%
haftmann@28447
   226
\isadelimquoteme
haftmann@28447
   227
%
haftmann@28447
   228
\endisadelimquoteme
haftmann@28447
   229
%
haftmann@28447
   230
\isatagquoteme
haftmann@28447
   231
\isacommand{primrec}\isamarkupfalse%
haftmann@28447
   232
\ {\isacharparenleft}\isakeyword{in}\ monoid{\isacharparenright}\ pow\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28447
   233
\ \ \ \ {\isachardoublequoteopen}pow\ {\isadigit{0}}\ a\ {\isacharequal}\ {\isasymone}{\isachardoublequoteclose}\isanewline
haftmann@28447
   234
\ \ {\isacharbar}\ {\isachardoublequoteopen}pow\ {\isacharparenleft}Suc\ n{\isacharparenright}\ a\ {\isacharequal}\ a\ {\isasymotimes}\ pow\ n\ a{\isachardoublequoteclose}%
haftmann@28447
   235
\endisatagquoteme
haftmann@28447
   236
{\isafoldquoteme}%
haftmann@28447
   237
%
haftmann@28447
   238
\isadelimquoteme
haftmann@28447
   239
%
haftmann@28447
   240
\endisadelimquoteme
haftmann@28447
   241
%
haftmann@28447
   242
\begin{isamarkuptext}%
haftmann@28447
   243
\noindent This we use to define the discrete exponentiation function:%
haftmann@28447
   244
\end{isamarkuptext}%
haftmann@28447
   245
\isamarkuptrue%
haftmann@28447
   246
%
haftmann@28447
   247
\isadelimquoteme
haftmann@28447
   248
%
haftmann@28447
   249
\endisadelimquoteme
haftmann@28447
   250
%
haftmann@28447
   251
\isatagquoteme
haftmann@28447
   252
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   253
\ bexp\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28447
   254
\ \ {\isachardoublequoteopen}bexp\ n\ {\isacharequal}\ pow\ n\ {\isacharparenleft}Suc\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28447
   255
\endisatagquoteme
haftmann@28447
   256
{\isafoldquoteme}%
haftmann@28447
   257
%
haftmann@28447
   258
\isadelimquoteme
haftmann@28447
   259
%
haftmann@28447
   260
\endisadelimquoteme
haftmann@28447
   261
%
haftmann@28447
   262
\begin{isamarkuptext}%
haftmann@28447
   263
\noindent The corresponding code:%
haftmann@28447
   264
\end{isamarkuptext}%
haftmann@28447
   265
\isamarkuptrue%
haftmann@28447
   266
%
haftmann@28447
   267
\isadelimquoteme
haftmann@28447
   268
%
haftmann@28447
   269
\endisadelimquoteme
haftmann@28447
   270
%
haftmann@28447
   271
\isatagquoteme
haftmann@28447
   272
%
haftmann@28447
   273
\begin{isamarkuptext}%
haftmann@28447
   274
\isaverbatim%
haftmann@28447
   275
\noindent%
haftmann@28447
   276
\verb|module Example where {|\newline%
haftmann@28447
   277
\newline%
haftmann@28447
   278
\newline%
haftmann@28447
   279
\verb|data Nat = Suc Nat |\verb,|,\verb| Zero_nat;|\newline%
haftmann@28447
   280
\newline%
haftmann@28447
   281
\verb|class Semigroup a where {|\newline%
haftmann@28447
   282
\verb|  mult :: a -> a -> a;|\newline%
haftmann@28447
   283
\verb|};|\newline%
haftmann@28447
   284
\newline%
haftmann@28447
   285
\verb|class (Semigroup a) => Monoid a where {|\newline%
haftmann@28447
   286
\verb|  neutral :: a;|\newline%
haftmann@28447
   287
\verb|};|\newline%
haftmann@28447
   288
\newline%
haftmann@28447
   289
\verb|pow :: forall a. (Monoid a) => Nat -> a -> a;|\newline%
haftmann@28447
   290
\verb|pow (Suc n) a = mult a (pow n a);|\newline%
haftmann@28447
   291
\verb|pow Zero_nat a = neutral;|\newline%
haftmann@28447
   292
\newline%
haftmann@28447
   293
\verb|plus_nat :: Nat -> Nat -> Nat;|\newline%
haftmann@28447
   294
\verb|plus_nat (Suc m) n = plus_nat m (Suc n);|\newline%
haftmann@28447
   295
\verb|plus_nat Zero_nat n = n;|\newline%
haftmann@28447
   296
\newline%
haftmann@28447
   297
\verb|neutral_nat :: Nat;|\newline%
haftmann@28447
   298
\verb|neutral_nat = Suc Zero_nat;|\newline%
haftmann@28447
   299
\newline%
haftmann@28447
   300
\verb|mult_nat :: Nat -> Nat -> Nat;|\newline%
haftmann@28447
   301
\verb|mult_nat (Suc m) n = plus_nat n (mult_nat m n);|\newline%
haftmann@28447
   302
\verb|mult_nat Zero_nat n = Zero_nat;|\newline%
haftmann@28447
   303
\newline%
haftmann@28447
   304
\verb|instance Semigroup Nat where {|\newline%
haftmann@28447
   305
\verb|  mult = mult_nat;|\newline%
haftmann@28447
   306
\verb|};|\newline%
haftmann@28447
   307
\newline%
haftmann@28447
   308
\verb|instance Monoid Nat where {|\newline%
haftmann@28447
   309
\verb|  neutral = neutral_nat;|\newline%
haftmann@28447
   310
\verb|};|\newline%
haftmann@28447
   311
\newline%
haftmann@28447
   312
\verb|bexp :: Nat -> Nat;|\newline%
haftmann@28447
   313
\verb|bexp n = pow n (Suc (Suc Zero_nat));|\newline%
haftmann@28447
   314
\newline%
haftmann@28447
   315
\verb|}|%
haftmann@28447
   316
\end{isamarkuptext}%
haftmann@28447
   317
\isamarkuptrue%
haftmann@28447
   318
%
haftmann@28447
   319
\endisatagquoteme
haftmann@28447
   320
{\isafoldquoteme}%
haftmann@28447
   321
%
haftmann@28447
   322
\isadelimquoteme
haftmann@28447
   323
%
haftmann@28447
   324
\endisadelimquoteme
haftmann@28447
   325
%
haftmann@28447
   326
\begin{isamarkuptext}%
haftmann@28447
   327
\noindent This is a convenient place to show how explicit dictionary construction
haftmann@28447
   328
  manifests in generated code (here, the same example in \isa{SML}):%
haftmann@28447
   329
\end{isamarkuptext}%
haftmann@28447
   330
\isamarkuptrue%
haftmann@28447
   331
%
haftmann@28447
   332
\isadelimquoteme
haftmann@28447
   333
%
haftmann@28447
   334
\endisadelimquoteme
haftmann@28447
   335
%
haftmann@28447
   336
\isatagquoteme
haftmann@28447
   337
%
haftmann@28447
   338
\begin{isamarkuptext}%
haftmann@28447
   339
\isaverbatim%
haftmann@28447
   340
\noindent%
haftmann@28447
   341
\verb|structure Example = |\newline%
haftmann@28447
   342
\verb|struct|\newline%
haftmann@28447
   343
\newline%
haftmann@28447
   344
\verb|datatype nat = Suc of nat |\verb,|,\verb| Zero_nat;|\newline%
haftmann@28447
   345
\newline%
haftmann@28447
   346
\verb|type 'a semigroup = {mult : 'a -> 'a -> 'a};|\newline%
haftmann@28447
   347
\verb|fun mult (A_:'a semigroup) = #mult A_;|\newline%
haftmann@28447
   348
\newline%
haftmann@28447
   349
\verb|type 'a monoid = {Program__semigroup_monoid : 'a semigroup, neutral : 'a};|\newline%
haftmann@28447
   350
\verb|fun semigroup_monoid (A_:'a monoid) = #Program__semigroup_monoid A_;|\newline%
haftmann@28447
   351
\verb|fun neutral (A_:'a monoid) = #neutral A_;|\newline%
haftmann@28447
   352
\newline%
haftmann@28447
   353
\verb|fun pow A_ (Suc n) a = mult (semigroup_monoid A_) a (pow A_ n a)|\newline%
haftmann@28447
   354
\verb|  |\verb,|,\verb| pow A_ Zero_nat a = neutral A_;|\newline%
haftmann@28447
   355
\newline%
haftmann@28447
   356
\verb|fun plus_nat (Suc m) n = plus_nat m (Suc n)|\newline%
haftmann@28447
   357
\verb|  |\verb,|,\verb| plus_nat Zero_nat n = n;|\newline%
haftmann@28447
   358
\newline%
haftmann@28447
   359
\verb|val neutral_nat : nat = Suc Zero_nat;|\newline%
haftmann@28447
   360
\newline%
haftmann@28447
   361
\verb|fun mult_nat (Suc m) n = plus_nat n (mult_nat m n)|\newline%
haftmann@28447
   362
\verb|  |\verb,|,\verb| mult_nat Zero_nat n = Zero_nat;|\newline%
haftmann@28447
   363
\newline%
haftmann@28447
   364
\verb|val semigroup_nat = {mult = mult_nat} : nat semigroup;|\newline%
haftmann@28447
   365
\newline%
haftmann@28447
   366
\verb|val monoid_nat =|\newline%
haftmann@28447
   367
\verb|  {Program__semigroup_monoid = semigroup_nat, neutral = neutral_nat} :|\newline%
haftmann@28447
   368
\verb|  nat monoid;|\newline%
haftmann@28447
   369
\newline%
haftmann@28447
   370
\verb|fun bexp n = pow monoid_nat n (Suc (Suc Zero_nat));|\newline%
haftmann@28447
   371
\newline%
haftmann@28447
   372
\verb|end; (*struct Example*)|%
haftmann@28447
   373
\end{isamarkuptext}%
haftmann@28447
   374
\isamarkuptrue%
haftmann@28447
   375
%
haftmann@28447
   376
\endisatagquoteme
haftmann@28447
   377
{\isafoldquoteme}%
haftmann@28447
   378
%
haftmann@28447
   379
\isadelimquoteme
haftmann@28447
   380
%
haftmann@28447
   381
\endisadelimquoteme
haftmann@28447
   382
%
haftmann@28447
   383
\begin{isamarkuptext}%
haftmann@28447
   384
\noindent Note the parameters with trailing underscore (\verb|A_|)
haftmann@28447
   385
    which are the dictionary parameters.%
haftmann@28447
   386
\end{isamarkuptext}%
haftmann@28447
   387
\isamarkuptrue%
haftmann@28447
   388
%
haftmann@28447
   389
\isamarkupsubsection{The preprocessor \label{sec:preproc}%
haftmann@28447
   390
}
haftmann@28447
   391
\isamarkuptrue%
haftmann@28447
   392
%
haftmann@28447
   393
\begin{isamarkuptext}%
haftmann@28447
   394
Before selected function theorems are turned into abstract
haftmann@28447
   395
  code, a chain of definitional transformation steps is carried
haftmann@28447
   396
  out: \emph{preprocessing}.  In essence, the preprocessor
haftmann@28447
   397
  consists of two components: a \emph{simpset} and \emph{function transformers}.
haftmann@28447
   398
haftmann@28447
   399
  The \emph{simpset} allows to employ the full generality of the Isabelle
haftmann@28447
   400
  simplifier.  Due to the interpretation of theorems
haftmann@28447
   401
  as defining equations, rewrites are applied to the right
haftmann@28447
   402
  hand side and the arguments of the left hand side of an
haftmann@28447
   403
  equation, but never to the constant heading the left hand side.
haftmann@28447
   404
  An important special case are \emph{inline theorems} which may be
haftmann@28447
   405
  declared and undeclared using the
haftmann@28447
   406
  \emph{code inline} or \emph{code inline del} attribute respectively.
haftmann@28447
   407
haftmann@28447
   408
  Some common applications:%
haftmann@28447
   409
\end{isamarkuptext}%
haftmann@28447
   410
\isamarkuptrue%
haftmann@28447
   411
%
haftmann@28447
   412
\begin{itemize}
haftmann@28447
   413
%
haftmann@28447
   414
\begin{isamarkuptext}%
haftmann@28447
   415
\item replacing non-executable constructs by executable ones:%
haftmann@28447
   416
\end{isamarkuptext}%
haftmann@28447
   417
\isamarkuptrue%
haftmann@28447
   418
%
haftmann@28447
   419
\isadelimquoteme
haftmann@28447
   420
%
haftmann@28447
   421
\endisadelimquoteme
haftmann@28447
   422
%
haftmann@28447
   423
\isatagquoteme
haftmann@28447
   424
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   425
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   426
\ \ {\isachardoublequoteopen}x\ {\isasymin}\ set\ xs\ {\isasymlongleftrightarrow}\ x\ mem\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@28447
   427
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@28447
   428
\endisatagquoteme
haftmann@28447
   429
{\isafoldquoteme}%
haftmann@28447
   430
%
haftmann@28447
   431
\isadelimquoteme
haftmann@28447
   432
%
haftmann@28447
   433
\endisadelimquoteme
haftmann@28447
   434
%
haftmann@28447
   435
\begin{isamarkuptext}%
haftmann@28447
   436
\item eliminating superfluous constants:%
haftmann@28447
   437
\end{isamarkuptext}%
haftmann@28447
   438
\isamarkuptrue%
haftmann@28447
   439
%
haftmann@28447
   440
\isadelimquoteme
haftmann@28447
   441
%
haftmann@28447
   442
\endisadelimquoteme
haftmann@28447
   443
%
haftmann@28447
   444
\isatagquoteme
haftmann@28447
   445
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   446
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   447
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharequal}\ Suc\ {\isadigit{0}}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@28447
   448
\ simp%
haftmann@28447
   449
\endisatagquoteme
haftmann@28447
   450
{\isafoldquoteme}%
haftmann@28447
   451
%
haftmann@28447
   452
\isadelimquoteme
haftmann@28447
   453
%
haftmann@28447
   454
\endisadelimquoteme
haftmann@28447
   455
%
haftmann@28447
   456
\begin{isamarkuptext}%
haftmann@28447
   457
\item replacing executable but inconvenient constructs:%
haftmann@28447
   458
\end{isamarkuptext}%
haftmann@28447
   459
\isamarkuptrue%
haftmann@28447
   460
%
haftmann@28447
   461
\isadelimquoteme
haftmann@28447
   462
%
haftmann@28447
   463
\endisadelimquoteme
haftmann@28447
   464
%
haftmann@28447
   465
\isatagquoteme
haftmann@28447
   466
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   467
\ {\isacharbrackleft}code\ inline{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   468
\ \ {\isachardoublequoteopen}xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ {\isasymlongleftrightarrow}\ List{\isachardot}null\ xs{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@28447
   469
\ {\isacharparenleft}induct\ xs{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@28447
   470
\endisatagquoteme
haftmann@28447
   471
{\isafoldquoteme}%
haftmann@28447
   472
%
haftmann@28447
   473
\isadelimquoteme
haftmann@28447
   474
%
haftmann@28447
   475
\endisadelimquoteme
haftmann@28447
   476
%
haftmann@28447
   477
\end{itemize}
haftmann@28447
   478
%
haftmann@28447
   479
\begin{isamarkuptext}%
haftmann@28447
   480
\noindent \emph{Function transformers} provide a very general interface,
haftmann@28447
   481
  transforming a list of function theorems to another
haftmann@28447
   482
  list of function theorems, provided that neither the heading
haftmann@28447
   483
  constant nor its type change.  The \isa{{\isadigit{0}}} / \isa{Suc}
haftmann@28447
   484
  pattern elimination implemented in
haftmann@28447
   485
  theory \isa{Efficient{\isacharunderscore}Nat} (see \secref{eff_nat}) uses this
haftmann@28447
   486
  interface.
haftmann@28447
   487
haftmann@28447
   488
  \noindent The current setup of the preprocessor may be inspected using
haftmann@28447
   489
  the \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
haftmann@28447
   490
  \hyperlink{command.code-thms}{\mbox{\isa{\isacommand{code{\isacharunderscore}thms}}}} provides a convenient
haftmann@28447
   491
  mechanism to inspect the impact of a preprocessor setup
haftmann@28447
   492
  on defining equations.
haftmann@28447
   493
haftmann@28447
   494
  \begin{warn}
haftmann@28447
   495
    The attribute \emph{code unfold}
haftmann@28447
   496
    associated with the \isa{SML\ code\ generator} also applies to
haftmann@28447
   497
    the \isa{generic\ code\ generator}:
haftmann@28447
   498
    \emph{code unfold} implies \emph{code inline}.
haftmann@28447
   499
  \end{warn}%
haftmann@28447
   500
\end{isamarkuptext}%
haftmann@28447
   501
\isamarkuptrue%
haftmann@28447
   502
%
haftmann@28447
   503
\isamarkupsubsection{Datatypes \label{sec:datatypes}%
haftmann@28447
   504
}
haftmann@28447
   505
\isamarkuptrue%
haftmann@28447
   506
%
haftmann@28447
   507
\begin{isamarkuptext}%
haftmann@28447
   508
Conceptually, any datatype is spanned by a set of
haftmann@28447
   509
  \emph{constructors} of type \isa{{\isasymtau}\ {\isacharequal}\ {\isasymdots}\ {\isasymRightarrow}\ {\isasymkappa}\ {\isasymalpha}\isactrlisub {\isadigit{1}}\ {\isasymdots}\ {\isasymalpha}\isactrlisub n}
haftmann@28447
   510
  where \isa{{\isacharbraceleft}{\isasymalpha}\isactrlisub {\isadigit{1}}{\isacharcomma}\ {\isasymdots}{\isacharcomma}\ {\isasymalpha}\isactrlisub n{\isacharbraceright}} is excactly the set of \emph{all}
haftmann@28447
   511
  type variables in \isa{{\isasymtau}}.  The HOL datatype package
haftmann@28447
   512
  by default registers any new datatype in the table
haftmann@28447
   513
  of datatypes, which may be inspected using
haftmann@28447
   514
  the \hyperlink{command.print-codesetup}{\mbox{\isa{\isacommand{print{\isacharunderscore}codesetup}}}} command.
haftmann@28447
   515
haftmann@28447
   516
  In some cases, it may be convenient to alter or
haftmann@28447
   517
  extend this table;  as an example, we will develop an alternative
haftmann@28447
   518
  representation of natural numbers as binary digits, whose
haftmann@28447
   519
  size does increase logarithmically with its value, not linear
haftmann@28447
   520
  \footnote{Indeed, the \hyperlink{theory.Efficient-Nat}{\mbox{\isa{Efficient{\isacharunderscore}Nat}}} theory (see \ref{eff_nat})
haftmann@28447
   521
    does something similar}.  First, the digit representation:%
haftmann@28447
   522
\end{isamarkuptext}%
haftmann@28447
   523
\isamarkuptrue%
haftmann@28447
   524
%
haftmann@28447
   525
\isadelimquoteme
haftmann@28447
   526
%
haftmann@28447
   527
\endisadelimquoteme
haftmann@28447
   528
%
haftmann@28447
   529
\isatagquoteme
haftmann@28447
   530
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   531
\ Dig{\isadigit{0}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28447
   532
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ n\ {\isacharequal}\ {\isadigit{2}}\ {\isacharasterisk}\ n{\isachardoublequoteclose}\isanewline
haftmann@28447
   533
\isanewline
haftmann@28447
   534
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   535
\ \ Dig{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28447
   536
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ n\ {\isacharequal}\ Suc\ {\isacharparenleft}{\isadigit{2}}\ {\isacharasterisk}\ n{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28447
   537
\endisatagquoteme
haftmann@28447
   538
{\isafoldquoteme}%
haftmann@28447
   539
%
haftmann@28447
   540
\isadelimquoteme
haftmann@28447
   541
%
haftmann@28447
   542
\endisadelimquoteme
haftmann@28447
   543
%
haftmann@28447
   544
\begin{isamarkuptext}%
haftmann@28447
   545
\noindent We will use these two \qt{digits} to represent natural numbers
haftmann@28447
   546
  in binary digits, e.g.:%
haftmann@28447
   547
\end{isamarkuptext}%
haftmann@28447
   548
\isamarkuptrue%
haftmann@28447
   549
%
haftmann@28447
   550
\isadelimquoteme
haftmann@28447
   551
%
haftmann@28447
   552
\endisadelimquoteme
haftmann@28447
   553
%
haftmann@28447
   554
\isatagquoteme
haftmann@28447
   555
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   556
\ {\isadigit{4}}{\isadigit{2}}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{4}}{\isadigit{2}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isacharparenleft}Dig{\isadigit{1}}\ {\isacharparenleft}Dig{\isadigit{0}}\ {\isadigit{1}}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   557
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   558
\ {\isacharparenleft}simp\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@28447
   559
\endisatagquoteme
haftmann@28447
   560
{\isafoldquoteme}%
haftmann@28447
   561
%
haftmann@28447
   562
\isadelimquoteme
haftmann@28447
   563
%
haftmann@28447
   564
\endisadelimquoteme
haftmann@28447
   565
%
haftmann@28447
   566
\begin{isamarkuptext}%
haftmann@28447
   567
\noindent Of course we also have to provide proper code equations for
haftmann@28447
   568
  the operations, e.g. \isa{op\ {\isacharplus}}:%
haftmann@28447
   569
\end{isamarkuptext}%
haftmann@28447
   570
\isamarkuptrue%
haftmann@28447
   571
%
haftmann@28447
   572
\isadelimquoteme
haftmann@28447
   573
%
haftmann@28447
   574
\endisadelimquoteme
haftmann@28447
   575
%
haftmann@28447
   576
\isatagquoteme
haftmann@28447
   577
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   578
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   579
\ \ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ n{\isachardoublequoteclose}\isanewline
haftmann@28447
   580
\ \ {\isachardoublequoteopen}m\ {\isacharplus}\ {\isadigit{0}}\ {\isacharequal}\ m{\isachardoublequoteclose}\isanewline
haftmann@28447
   581
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ n{\isachardoublequoteclose}\isanewline
haftmann@28447
   582
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{1}}\ m{\isachardoublequoteclose}\isanewline
haftmann@28447
   583
\ \ {\isachardoublequoteopen}{\isadigit{1}}\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   584
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ {\isadigit{1}}\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   585
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   586
\ \ {\isachardoublequoteopen}Dig{\isadigit{0}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   587
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{0}}\ n\ {\isacharequal}\ Dig{\isadigit{1}}\ {\isacharparenleft}m\ {\isacharplus}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   588
\ \ {\isachardoublequoteopen}Dig{\isadigit{1}}\ m\ {\isacharplus}\ Dig{\isadigit{1}}\ n\ {\isacharequal}\ Dig{\isadigit{0}}\ {\isacharparenleft}m\ {\isacharplus}\ n\ {\isacharplus}\ {\isadigit{1}}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28447
   589
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   590
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ Dig{\isadigit{0}}{\isacharunderscore}def\ Dig{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
haftmann@28447
   591
\endisatagquoteme
haftmann@28447
   592
{\isafoldquoteme}%
haftmann@28447
   593
%
haftmann@28447
   594
\isadelimquoteme
haftmann@28447
   595
%
haftmann@28447
   596
\endisadelimquoteme
haftmann@28447
   597
%
haftmann@28447
   598
\begin{isamarkuptext}%
haftmann@28447
   599
\noindent We then instruct the code generator to view \isa{{\isadigit{0}}},
haftmann@28447
   600
  \isa{{\isadigit{1}}}, \isa{Dig{\isadigit{0}}} and \isa{Dig{\isadigit{1}}} as
haftmann@28447
   601
  datatype constructors:%
haftmann@28447
   602
\end{isamarkuptext}%
haftmann@28447
   603
\isamarkuptrue%
haftmann@28447
   604
%
haftmann@28447
   605
\isadelimquoteme
haftmann@28447
   606
%
haftmann@28447
   607
\endisadelimquoteme
haftmann@28447
   608
%
haftmann@28447
   609
\isatagquoteme
haftmann@28447
   610
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@28447
   611
\ {\isachardoublequoteopen}{\isadigit{0}}{\isasymColon}nat{\isachardoublequoteclose}\ {\isachardoublequoteopen}{\isadigit{1}}{\isasymColon}nat{\isachardoublequoteclose}\ Dig{\isadigit{0}}\ Dig{\isadigit{1}}%
haftmann@28447
   612
\endisatagquoteme
haftmann@28447
   613
{\isafoldquoteme}%
haftmann@28447
   614
%
haftmann@28447
   615
\isadelimquoteme
haftmann@28447
   616
%
haftmann@28447
   617
\endisadelimquoteme
haftmann@28447
   618
%
haftmann@28447
   619
\begin{isamarkuptext}%
haftmann@28447
   620
\noindent For the former constructor \isa{Suc}, we provide a code
haftmann@28447
   621
  equation and remove some parts of the default code generator setup
haftmann@28447
   622
  which are an obstacle here:%
haftmann@28447
   623
\end{isamarkuptext}%
haftmann@28447
   624
\isamarkuptrue%
haftmann@28447
   625
%
haftmann@28447
   626
\isadelimquoteme
haftmann@28447
   627
%
haftmann@28447
   628
\endisadelimquoteme
haftmann@28447
   629
%
haftmann@28447
   630
\isatagquoteme
haftmann@28447
   631
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   632
\ Suc{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   633
\ \ {\isachardoublequoteopen}Suc\ n\ {\isacharequal}\ n\ {\isacharplus}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   634
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   635
\ simp\isanewline
haftmann@28447
   636
\isanewline
haftmann@28447
   637
\isacommand{declare}\isamarkupfalse%
haftmann@28447
   638
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline\ del{\isacharbrackright}\isanewline
haftmann@28447
   639
\isacommand{declare}\isamarkupfalse%
haftmann@28447
   640
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}%
haftmann@28447
   641
\endisatagquoteme
haftmann@28447
   642
{\isafoldquoteme}%
haftmann@28447
   643
%
haftmann@28447
   644
\isadelimquoteme
haftmann@28447
   645
%
haftmann@28447
   646
\endisadelimquoteme
haftmann@28447
   647
%
haftmann@28447
   648
\begin{isamarkuptext}%
haftmann@28447
   649
\noindent This yields the following code:%
haftmann@28447
   650
\end{isamarkuptext}%
haftmann@28447
   651
\isamarkuptrue%
haftmann@28447
   652
%
haftmann@28447
   653
\isadelimquoteme
haftmann@28447
   654
%
haftmann@28447
   655
\endisadelimquoteme
haftmann@28447
   656
%
haftmann@28447
   657
\isatagquoteme
haftmann@28447
   658
%
haftmann@28447
   659
\begin{isamarkuptext}%
haftmann@28447
   660
\isaverbatim%
haftmann@28447
   661
\noindent%
haftmann@28447
   662
\verb|structure Example = |\newline%
haftmann@28447
   663
\verb|struct|\newline%
haftmann@28447
   664
\newline%
haftmann@28447
   665
\verb|datatype nat = Dig1 of nat |\verb,|,\verb| Dig0 of nat |\verb,|,\verb| One_nat |\verb,|,\verb| Zero_nat;|\newline%
haftmann@28447
   666
\newline%
haftmann@28447
   667
\verb|fun plus_nat (Dig1 m) (Dig1 n) = Dig0 (plus_nat (plus_nat m n) One_nat)|\newline%
haftmann@28447
   668
\verb|  |\verb,|,\verb| plus_nat (Dig1 m) (Dig0 n) = Dig1 (plus_nat m n)|\newline%
haftmann@28447
   669
\verb|  |\verb,|,\verb| plus_nat (Dig0 m) (Dig1 n) = Dig1 (plus_nat m n)|\newline%
haftmann@28447
   670
\verb|  |\verb,|,\verb| plus_nat (Dig0 m) (Dig0 n) = Dig0 (plus_nat m n)|\newline%
haftmann@28447
   671
\verb|  |\verb,|,\verb| plus_nat (Dig1 m) One_nat = Dig0 (plus_nat m One_nat)|\newline%
haftmann@28447
   672
\verb|  |\verb,|,\verb| plus_nat One_nat (Dig1 n) = Dig0 (plus_nat n One_nat)|\newline%
haftmann@28447
   673
\verb|  |\verb,|,\verb| plus_nat (Dig0 m) One_nat = Dig1 m|\newline%
haftmann@28447
   674
\verb|  |\verb,|,\verb| plus_nat One_nat (Dig0 n) = Dig1 n|\newline%
haftmann@28447
   675
\verb|  |\verb,|,\verb| plus_nat m Zero_nat = m|\newline%
haftmann@28447
   676
\verb|  |\verb,|,\verb| plus_nat Zero_nat n = n;|\newline%
haftmann@28447
   677
\newline%
haftmann@28447
   678
\verb|end; (*struct Example*)|%
haftmann@28447
   679
\end{isamarkuptext}%
haftmann@28447
   680
\isamarkuptrue%
haftmann@28447
   681
%
haftmann@28447
   682
\endisatagquoteme
haftmann@28447
   683
{\isafoldquoteme}%
haftmann@28447
   684
%
haftmann@28447
   685
\isadelimquoteme
haftmann@28447
   686
%
haftmann@28447
   687
\endisadelimquoteme
haftmann@28447
   688
%
haftmann@28447
   689
\begin{isamarkuptext}%
haftmann@28447
   690
\noindent From this example, it can be easily glimpsed that using own constructor sets
haftmann@28447
   691
  is a little delicate since it changes the set of valid patterns for values
haftmann@28447
   692
  of that type.  Without going into much detail, here some practical hints:
haftmann@28447
   693
haftmann@28447
   694
  \begin{itemize}
haftmann@28447
   695
    \item When changing the constructor set for datatypes, take care to
haftmann@28447
   696
      provide an alternative for the \isa{case} combinator (e.g.~by replacing
haftmann@28447
   697
      it using the preprocessor).
haftmann@28447
   698
    \item Values in the target language need not to be normalised -- different
haftmann@28447
   699
      values in the target language may represent the same value in the
haftmann@28447
   700
      logic (e.g. \isa{Dig{\isadigit{1}}\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{1}}}).
haftmann@28447
   701
    \item Usually, a good methodology to deal with the subtleties of pattern
haftmann@28447
   702
      matching is to see the type as an abstract type: provide a set
haftmann@28447
   703
      of operations which operate on the concrete representation of the type,
haftmann@28447
   704
      and derive further operations by combinations of these primitive ones,
haftmann@28447
   705
      without relying on a particular representation.
haftmann@28447
   706
  \end{itemize}%
haftmann@28447
   707
\end{isamarkuptext}%
haftmann@28447
   708
\isamarkuptrue%
haftmann@28447
   709
%
haftmann@28447
   710
\isadeliminvisible
haftmann@28447
   711
%
haftmann@28447
   712
\endisadeliminvisible
haftmann@28447
   713
%
haftmann@28447
   714
\isataginvisible
haftmann@28447
   715
\isacommand{code{\isacharunderscore}datatype}\isamarkupfalse%
haftmann@28447
   716
\ {\isachardoublequoteopen}{\isadigit{0}}{\isacharcolon}{\isacharcolon}nat{\isachardoublequoteclose}\ Suc\isanewline
haftmann@28447
   717
\isacommand{declare}\isamarkupfalse%
haftmann@28447
   718
\ plus{\isacharunderscore}Dig\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}\isanewline
haftmann@28447
   719
\isacommand{declare}\isamarkupfalse%
haftmann@28447
   720
\ One{\isacharunderscore}nat{\isacharunderscore}def\ {\isacharbrackleft}code\ inline{\isacharbrackright}\isanewline
haftmann@28447
   721
\isacommand{declare}\isamarkupfalse%
haftmann@28447
   722
\ add{\isacharunderscore}Suc{\isacharunderscore}shift\ {\isacharbrackleft}code\ func{\isacharbrackright}\ \isanewline
haftmann@28447
   723
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   724
\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\ {\isachardoublequoteopen}{\isadigit{0}}\ {\isacharplus}\ n\ {\isacharequal}\ {\isacharparenleft}n\ {\isasymColon}\ nat{\isacharparenright}{\isachardoublequoteclose}\ \isacommand{by}\isamarkupfalse%
haftmann@28447
   725
\ simp%
haftmann@28447
   726
\endisataginvisible
haftmann@28447
   727
{\isafoldinvisible}%
haftmann@28447
   728
%
haftmann@28447
   729
\isadeliminvisible
haftmann@28447
   730
%
haftmann@28447
   731
\endisadeliminvisible
haftmann@28447
   732
%
haftmann@28447
   733
\isamarkupsubsection{Equality and wellsortedness%
haftmann@28447
   734
}
haftmann@28447
   735
\isamarkuptrue%
haftmann@28447
   736
%
haftmann@28447
   737
\begin{isamarkuptext}%
haftmann@28447
   738
Surely you have already noticed how equality is treated
haftmann@28447
   739
  by the code generator:%
haftmann@28447
   740
\end{isamarkuptext}%
haftmann@28447
   741
\isamarkuptrue%
haftmann@28447
   742
%
haftmann@28447
   743
\isadelimquoteme
haftmann@28447
   744
%
haftmann@28447
   745
\endisadelimquoteme
haftmann@28447
   746
%
haftmann@28447
   747
\isatagquoteme
haftmann@28447
   748
\isacommand{primrec}\isamarkupfalse%
haftmann@28447
   749
\ 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@28447
   750
\ \ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharequal}\ xs{\isachardoublequoteclose}\isanewline
haftmann@28447
   751
\ \ {\isacharbar}\ {\isachardoublequoteopen}collect{\isacharunderscore}duplicates\ xs\ ys\ {\isacharparenleft}z{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ z\ {\isasymin}\ set\ xs\isanewline
haftmann@28447
   752
\ \ \ \ \ \ then\ if\ z\ {\isasymin}\ set\ ys\isanewline
haftmann@28447
   753
\ \ \ \ \ \ \ \ then\ collect{\isacharunderscore}duplicates\ xs\ ys\ zs\isanewline
haftmann@28447
   754
\ \ \ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ xs\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs\isanewline
haftmann@28447
   755
\ \ \ \ \ \ else\ collect{\isacharunderscore}duplicates\ {\isacharparenleft}z{\isacharhash}xs{\isacharparenright}\ {\isacharparenleft}z{\isacharhash}ys{\isacharparenright}\ zs{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28447
   756
\endisatagquoteme
haftmann@28447
   757
{\isafoldquoteme}%
haftmann@28447
   758
%
haftmann@28447
   759
\isadelimquoteme
haftmann@28447
   760
%
haftmann@28447
   761
\endisadelimquoteme
haftmann@28447
   762
%
haftmann@28447
   763
\begin{isamarkuptext}%
haftmann@28447
   764
\noindent The membership test during preprocessing is rewritten,
haftmann@28447
   765
  resulting in \isa{op\ mem}, which itself
haftmann@28447
   766
  performs an explicit equality check.%
haftmann@28447
   767
\end{isamarkuptext}%
haftmann@28447
   768
\isamarkuptrue%
haftmann@28447
   769
%
haftmann@28447
   770
\isadelimquoteme
haftmann@28447
   771
%
haftmann@28447
   772
\endisadelimquoteme
haftmann@28447
   773
%
haftmann@28447
   774
\isatagquoteme
haftmann@28447
   775
%
haftmann@28447
   776
\begin{isamarkuptext}%
haftmann@28447
   777
\isaverbatim%
haftmann@28447
   778
\noindent%
haftmann@28447
   779
\verb|structure Example = |\newline%
haftmann@28447
   780
\verb|struct|\newline%
haftmann@28447
   781
\newline%
haftmann@28447
   782
\verb|type 'a eq = {eq : 'a -> 'a -> bool};|\newline%
haftmann@28447
   783
\verb|fun eq (A_:'a eq) = #eq A_;|\newline%
haftmann@28447
   784
\newline%
haftmann@28447
   785
\verb|fun eqop A_ a b = eq A_ a b;|\newline%
haftmann@28447
   786
\newline%
haftmann@28462
   787
\verb|fun member A_ x (y :: ys) = eqop A_ x y orelse member A_ x ys|\newline%
haftmann@28447
   788
\verb|  |\verb,|,\verb| member A_ x [] = false;|\newline%
haftmann@28447
   789
\newline%
haftmann@28447
   790
\verb|fun collect_duplicates A_ xs ys (z :: zs) =|\newline%
haftmann@28447
   791
\verb|  (if member A_ z xs|\newline%
haftmann@28447
   792
\verb|    then (if member A_ z ys then collect_duplicates A_ xs ys zs|\newline%
haftmann@28447
   793
\verb|           else collect_duplicates A_ xs (z :: ys) zs)|\newline%
haftmann@28447
   794
\verb|    else collect_duplicates A_ (z :: xs) (z :: ys) zs)|\newline%
haftmann@28447
   795
\verb|  |\verb,|,\verb| collect_duplicates A_ xs ys [] = xs;|\newline%
haftmann@28447
   796
\newline%
haftmann@28447
   797
\verb|end; (*struct Example*)|%
haftmann@28447
   798
\end{isamarkuptext}%
haftmann@28447
   799
\isamarkuptrue%
haftmann@28447
   800
%
haftmann@28447
   801
\endisatagquoteme
haftmann@28447
   802
{\isafoldquoteme}%
haftmann@28447
   803
%
haftmann@28447
   804
\isadelimquoteme
haftmann@28447
   805
%
haftmann@28447
   806
\endisadelimquoteme
haftmann@28447
   807
%
haftmann@28447
   808
\begin{isamarkuptext}%
haftmann@28447
   809
\noindent Obviously, polymorphic equality is implemented the Haskell
haftmann@28447
   810
  way using a type class.  How is this achieved?  HOL introduces
haftmann@28447
   811
  an explicit class \isa{eq} with a corresponding operation
haftmann@28447
   812
  \isa{eq{\isacharunderscore}class{\isachardot}eq} such that \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharequal}\ op\ {\isacharequal}}.
haftmann@28447
   813
  The preprocessing framework does the rest by propagating the
haftmann@28447
   814
  \isa{eq} constraints through all dependent defining equations.
haftmann@28447
   815
  For datatypes, instances of \isa{eq} are implicitly derived
haftmann@28447
   816
  when possible.  For other types, you may instantiate \isa{eq}
haftmann@28447
   817
  manually like any other type class.
haftmann@28447
   818
haftmann@28447
   819
  Though this \isa{eq} class is designed to get rarely in
haftmann@28447
   820
  the way, a subtlety
haftmann@28447
   821
  enters the stage when definitions of overloaded constants
haftmann@28447
   822
  are dependent on operational equality.  For example, let
haftmann@28447
   823
  us define a lexicographic ordering on tuples
haftmann@28447
   824
  (also see theory \hyperlink{theory.Product-ord}{\mbox{\isa{Product{\isacharunderscore}ord}}}):%
haftmann@28447
   825
\end{isamarkuptext}%
haftmann@28447
   826
\isamarkuptrue%
haftmann@28447
   827
%
haftmann@28447
   828
\isadelimquoteme
haftmann@28447
   829
%
haftmann@28447
   830
\endisadelimquoteme
haftmann@28447
   831
%
haftmann@28447
   832
\isatagquoteme
haftmann@28447
   833
\isacommand{instantiation}\isamarkupfalse%
haftmann@28447
   834
\ {\isachardoublequoteopen}{\isacharasterisk}{\isachardoublequoteclose}\ {\isacharcolon}{\isacharcolon}\ {\isacharparenleft}order{\isacharcomma}\ order{\isacharparenright}\ order\isanewline
haftmann@28447
   835
\isakeyword{begin}\isanewline
haftmann@28447
   836
\isanewline
haftmann@28447
   837
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   838
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   839
\ \ {\isachardoublequoteopen}x\ {\isasymle}\ y\ {\isasymlongleftrightarrow}\ fst\ x\ {\isacharless}\ fst\ y\ {\isasymor}\ fst\ x\ {\isacharequal}\ fst\ y\ {\isasymand}\ snd\ x\ {\isasymle}\ snd\ y{\isachardoublequoteclose}\isanewline
haftmann@28447
   840
\isanewline
haftmann@28447
   841
\isacommand{definition}\isamarkupfalse%
haftmann@28447
   842
\ {\isacharbrackleft}code\ func\ del{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   843
\ \ {\isachardoublequoteopen}x\ {\isacharless}\ y\ {\isasymlongleftrightarrow}\ fst\ x\ {\isacharless}\ fst\ y\ {\isasymor}\ fst\ x\ {\isacharequal}\ fst\ y\ {\isasymand}\ snd\ x\ {\isacharless}\ snd\ y{\isachardoublequoteclose}\isanewline
haftmann@28447
   844
\isanewline
haftmann@28447
   845
\isacommand{instance}\isamarkupfalse%
haftmann@28447
   846
\ \isacommand{proof}\isamarkupfalse%
haftmann@28447
   847
\isanewline
haftmann@28447
   848
\isacommand{qed}\isamarkupfalse%
haftmann@28447
   849
\ {\isacharparenleft}auto\ simp{\isacharcolon}\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}prod{\isacharunderscore}def\ intro{\isacharcolon}\ order{\isacharunderscore}less{\isacharunderscore}trans{\isacharparenright}\isanewline
haftmann@28447
   850
\isanewline
haftmann@28447
   851
\isacommand{end}\isamarkupfalse%
haftmann@28447
   852
\isanewline
haftmann@28447
   853
\isanewline
haftmann@28447
   854
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   855
\ order{\isacharunderscore}prod\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   856
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}order{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@28447
   857
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   858
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}order{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@28447
   859
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   860
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   861
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharparenright}%
haftmann@28447
   862
\endisatagquoteme
haftmann@28447
   863
{\isafoldquoteme}%
haftmann@28447
   864
%
haftmann@28447
   865
\isadelimquoteme
haftmann@28447
   866
%
haftmann@28447
   867
\endisadelimquoteme
haftmann@28447
   868
%
haftmann@28447
   869
\begin{isamarkuptext}%
haftmann@28447
   870
\noindent Then code generation will fail.  Why?  The definition
haftmann@28447
   871
  of \isa{op\ {\isasymle}} depends on equality on both arguments,
haftmann@28447
   872
  which are polymorphic and impose an additional \isa{eq}
haftmann@28447
   873
  class constraint, which the preprocessor does not propagate
haftmann@28447
   874
  (for technical reasons).
haftmann@28447
   875
haftmann@28447
   876
  The solution is to add \isa{eq} explicitly to the first sort arguments in the
haftmann@28447
   877
  code theorems:%
haftmann@28447
   878
\end{isamarkuptext}%
haftmann@28447
   879
\isamarkuptrue%
haftmann@28447
   880
%
haftmann@28447
   881
\isadelimquoteme
haftmann@28447
   882
%
haftmann@28447
   883
\endisadelimquoteme
haftmann@28447
   884
%
haftmann@28447
   885
\isatagquoteme
haftmann@28447
   886
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
   887
\ order{\isacharunderscore}prod{\isacharunderscore}code\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
   888
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}order{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isacharless}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@28447
   889
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isacharless}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   890
\ \ {\isachardoublequoteopen}{\isacharparenleft}x{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}a{\isasymColon}{\isacharbraceleft}order{\isacharcomma}\ eq{\isacharbraceright}{\isacharcomma}\ y{\isadigit{1}}\ {\isasymColon}\ {\isacharprime}b{\isasymColon}order{\isacharparenright}\ {\isasymle}\ {\isacharparenleft}x{\isadigit{2}}{\isacharcomma}\ y{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@28447
   891
\ \ \ \ \ x{\isadigit{1}}\ {\isacharless}\ x{\isadigit{2}}\ {\isasymor}\ x{\isadigit{1}}\ {\isacharequal}\ x{\isadigit{2}}\ {\isasymand}\ y{\isadigit{1}}\ {\isasymle}\ y{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@28447
   892
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
   893
\ {\isacharparenleft}simp{\isacharunderscore}all\ add{\isacharcolon}\ less{\isacharunderscore}prod{\isacharunderscore}def\ less{\isacharunderscore}eq{\isacharunderscore}prod{\isacharunderscore}def{\isacharparenright}%
haftmann@28447
   894
\endisatagquoteme
haftmann@28447
   895
{\isafoldquoteme}%
haftmann@28447
   896
%
haftmann@28447
   897
\isadelimquoteme
haftmann@28447
   898
%
haftmann@28447
   899
\endisadelimquoteme
haftmann@28447
   900
%
haftmann@28447
   901
\begin{isamarkuptext}%
haftmann@28447
   902
\noindent Then code generation succeeds:%
haftmann@28447
   903
\end{isamarkuptext}%
haftmann@28447
   904
\isamarkuptrue%
haftmann@28447
   905
%
haftmann@28447
   906
\isadelimquoteme
haftmann@28447
   907
%
haftmann@28447
   908
\endisadelimquoteme
haftmann@28447
   909
%
haftmann@28447
   910
\isatagquoteme
haftmann@28447
   911
%
haftmann@28447
   912
\begin{isamarkuptext}%
haftmann@28447
   913
\isaverbatim%
haftmann@28447
   914
\noindent%
haftmann@28447
   915
\verb|structure Example = |\newline%
haftmann@28447
   916
\verb|struct|\newline%
haftmann@28447
   917
\newline%
haftmann@28447
   918
\verb|type 'a eq = {eq : 'a -> 'a -> bool};|\newline%
haftmann@28447
   919
\verb|fun eq (A_:'a eq) = #eq A_;|\newline%
haftmann@28447
   920
\newline%
haftmann@28447
   921
\verb|type 'a ord = {less_eq : 'a -> 'a -> bool, less : 'a -> 'a -> bool};|\newline%
haftmann@28447
   922
\verb|fun less_eq (A_:'a ord) = #less_eq A_;|\newline%
haftmann@28447
   923
\verb|fun less (A_:'a ord) = #less A_;|\newline%
haftmann@28447
   924
\newline%
haftmann@28447
   925
\verb|fun eqop A_ a b = eq A_ a b;|\newline%
haftmann@28447
   926
\newline%
haftmann@28447
   927
\verb|type 'a preorder = {Orderings__ord_preorder : 'a ord};|\newline%
haftmann@28447
   928
\verb|fun ord_preorder (A_:'a preorder) = #Orderings__ord_preorder A_;|\newline%
haftmann@28447
   929
\newline%
haftmann@28447
   930
\verb|type 'a order = {Orderings__preorder_order : 'a preorder};|\newline%
haftmann@28447
   931
\verb|fun preorder_order (A_:'a order) = #Orderings__preorder_order A_;|\newline%
haftmann@28447
   932
\newline%
haftmann@28447
   933
\verb|fun less_eqa (A1_, A2_) B_ (x1, y1) (x2, y2) =|\newline%
haftmann@28447
   934
\verb|  less ((ord_preorder o preorder_order) A2_) x1 x2 orelse|\newline%
haftmann@28447
   935
\verb|    eqop A1_ x1 x2 andalso|\newline%
haftmann@28447
   936
\verb|      less_eq ((ord_preorder o preorder_order) B_) y1 y2|\newline%
haftmann@28447
   937
\verb|  |\verb,|,\verb| less_eqa (A1_, A2_) B_ (x1, y1) (x2, y2) =|\newline%
haftmann@28447
   938
\verb|    less ((ord_preorder o preorder_order) A2_) x1 x2 orelse|\newline%
haftmann@28447
   939
\verb|      eqop A1_ x1 x2 andalso|\newline%
haftmann@28447
   940
\verb|        less_eq ((ord_preorder o preorder_order) B_) y1 y2;|\newline%
haftmann@28447
   941
\newline%
haftmann@28447
   942
\verb|end; (*struct Example*)|%
haftmann@28447
   943
\end{isamarkuptext}%
haftmann@28447
   944
\isamarkuptrue%
haftmann@28447
   945
%
haftmann@28447
   946
\endisatagquoteme
haftmann@28447
   947
{\isafoldquoteme}%
haftmann@28447
   948
%
haftmann@28447
   949
\isadelimquoteme
haftmann@28447
   950
%
haftmann@28447
   951
\endisadelimquoteme
haftmann@28447
   952
%
haftmann@28447
   953
\begin{isamarkuptext}%
haftmann@28447
   954
In some cases, the automatically derived defining equations
haftmann@28447
   955
  for equality on a particular type may not be appropriate.
haftmann@28447
   956
  As example, watch the following datatype representing
haftmann@28447
   957
  monomorphic parametric types (where type constructors
haftmann@28447
   958
  are referred to by natural numbers):%
haftmann@28447
   959
\end{isamarkuptext}%
haftmann@28447
   960
\isamarkuptrue%
haftmann@28447
   961
%
haftmann@28447
   962
\isadelimquoteme
haftmann@28447
   963
%
haftmann@28447
   964
\endisadelimquoteme
haftmann@28447
   965
%
haftmann@28447
   966
\isatagquoteme
haftmann@28447
   967
\isacommand{datatype}\isamarkupfalse%
haftmann@28447
   968
\ monotype\ {\isacharequal}\ Mono\ nat\ {\isachardoublequoteopen}monotype\ list{\isachardoublequoteclose}%
haftmann@28447
   969
\endisatagquoteme
haftmann@28447
   970
{\isafoldquoteme}%
haftmann@28447
   971
%
haftmann@28447
   972
\isadelimquoteme
haftmann@28447
   973
%
haftmann@28447
   974
\endisadelimquoteme
haftmann@28447
   975
%
haftmann@28447
   976
\isadelimproof
haftmann@28447
   977
%
haftmann@28447
   978
\endisadelimproof
haftmann@28447
   979
%
haftmann@28447
   980
\isatagproof
haftmann@28447
   981
%
haftmann@28447
   982
\endisatagproof
haftmann@28447
   983
{\isafoldproof}%
haftmann@28447
   984
%
haftmann@28447
   985
\isadelimproof
haftmann@28447
   986
%
haftmann@28447
   987
\endisadelimproof
haftmann@28447
   988
%
haftmann@28447
   989
\begin{isamarkuptext}%
haftmann@28462
   990
\noindent Then code generation for SML would fail with a message
haftmann@28447
   991
  that the generated code contains illegal mutual dependencies:
haftmann@28462
   992
  the theorem \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymequiv}\ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}} already requires the
haftmann@28447
   993
  instance \isa{monotype\ {\isasymColon}\ eq}, which itself requires
haftmann@28462
   994
  \isa{eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymequiv}\ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}};  Haskell has no problem with mutually
haftmann@28462
   995
  recursive \isa{instance} and \isa{function} definitions,
haftmann@28447
   996
  but the SML serializer does not support this.
haftmann@28447
   997
haftmann@28447
   998
  In such cases, you have to provide your own equality equations
haftmann@28447
   999
  involving auxiliary constants.  In our case,
haftmann@28447
  1000
  \isa{list{\isacharunderscore}all{\isadigit{2}}} can do the job:%
haftmann@28447
  1001
\end{isamarkuptext}%
haftmann@28447
  1002
\isamarkuptrue%
haftmann@28447
  1003
%
haftmann@28447
  1004
\isadelimquoteme
haftmann@28447
  1005
%
haftmann@28447
  1006
\endisadelimquoteme
haftmann@28447
  1007
%
haftmann@28447
  1008
\isatagquoteme
haftmann@28447
  1009
\isacommand{lemma}\isamarkupfalse%
haftmann@28447
  1010
\ monotype{\isacharunderscore}eq{\isacharunderscore}list{\isacharunderscore}all{\isadigit{2}}\ {\isacharbrackleft}code\ func{\isacharbrackright}{\isacharcolon}\isanewline
haftmann@28447
  1011
\ \ {\isachardoublequoteopen}eq{\isacharunderscore}class{\isachardot}eq\ {\isacharparenleft}Mono\ tyco{\isadigit{1}}\ typargs{\isadigit{1}}{\isacharparenright}\ {\isacharparenleft}Mono\ tyco{\isadigit{2}}\ typargs{\isadigit{2}}{\isacharparenright}\ {\isasymlongleftrightarrow}\isanewline
haftmann@28462
  1012
\ \ \ \ \ eq{\isacharunderscore}class{\isachardot}eq\ tyco{\isadigit{1}}\ tyco{\isadigit{2}}\ {\isasymand}\ list{\isacharunderscore}all{\isadigit{2}}\ eq{\isacharunderscore}class{\isachardot}eq\ typargs{\isadigit{1}}\ typargs{\isadigit{2}}{\isachardoublequoteclose}\isanewline
haftmann@28447
  1013
\ \ \isacommand{by}\isamarkupfalse%
haftmann@28447
  1014
\ {\isacharparenleft}simp\ add{\isacharcolon}\ eq\ list{\isacharunderscore}all{\isadigit{2}}{\isacharunderscore}eq\ {\isacharbrackleft}symmetric{\isacharbrackright}{\isacharparenright}%
haftmann@28447
  1015
\endisatagquoteme
haftmann@28447
  1016
{\isafoldquoteme}%
haftmann@28447
  1017
%
haftmann@28447
  1018
\isadelimquoteme
haftmann@28447
  1019
%
haftmann@28447
  1020
\endisadelimquoteme
haftmann@28447
  1021
%
haftmann@28447
  1022
\begin{isamarkuptext}%
haftmann@28447
  1023
\noindent does not depend on instance \isa{monotype\ {\isasymColon}\ eq}:%
haftmann@28447
  1024
\end{isamarkuptext}%
haftmann@28447
  1025
\isamarkuptrue%
haftmann@28447
  1026
%
haftmann@28447
  1027
\isadelimquoteme
haftmann@28447
  1028
%
haftmann@28447
  1029
\endisadelimquoteme
haftmann@28447
  1030
%
haftmann@28447
  1031
\isatagquoteme
haftmann@28447
  1032
%
haftmann@28447
  1033
\begin{isamarkuptext}%
haftmann@28447
  1034
\isaverbatim%
haftmann@28447
  1035
\noindent%
haftmann@28447
  1036
\verb|structure Example = |\newline%
haftmann@28447
  1037
\verb|struct|\newline%
haftmann@28447
  1038
\newline%
haftmann@28447
  1039
\verb|datatype nat = Suc of nat |\verb,|,\verb| Zero_nat;|\newline%
haftmann@28447
  1040
\newline%
haftmann@28447
  1041
\verb|fun null (x :: xs) = false|\newline%
haftmann@28447
  1042
\verb|  |\verb,|,\verb| null [] = true;|\newline%
haftmann@28447
  1043
\newline%
haftmann@28447
  1044
\verb|fun eq_nat (Suc a) Zero_nat = false|\newline%
haftmann@28447
  1045
\verb|  |\verb,|,\verb| eq_nat Zero_nat (Suc a) = false|\newline%
haftmann@28447
  1046
\verb|  |\verb,|,\verb| eq_nat (Suc nat) (Suc nat') = eq_nat nat nat'|\newline%
haftmann@28447
  1047
\verb|  |\verb,|,\verb| eq_nat Zero_nat Zero_nat = true;|\newline%
haftmann@28447
  1048
\newline%
haftmann@28447
  1049
\verb|datatype monotype = Mono of nat * monotype list;|\newline%
haftmann@28447
  1050
\newline%
haftmann@28447
  1051
\verb|fun list_all2 p (x :: xs) (y :: ys) = p x y andalso list_all2 p xs ys|\newline%
haftmann@28447
  1052
\verb|  |\verb,|,\verb| list_all2 p xs [] = null xs|\newline%
haftmann@28447
  1053
\verb|  |\verb,|,\verb| list_all2 p [] ys = null ys;|\newline%
haftmann@28447
  1054
\newline%
haftmann@28447
  1055
\verb|fun eq_monotype (Mono (tyco1, typargs1)) (Mono (tyco2, typargs2)) =|\newline%
haftmann@28462
  1056
\verb|  eq_nat tyco1 tyco2 andalso list_all2 eq_monotype typargs1 typargs2;|\newline%
haftmann@28447
  1057
\newline%
haftmann@28447
  1058
\verb|end; (*struct Example*)|%
haftmann@28447
  1059
\end{isamarkuptext}%
haftmann@28447
  1060
\isamarkuptrue%
haftmann@28447
  1061
%
haftmann@28447
  1062
\endisatagquoteme
haftmann@28447
  1063
{\isafoldquoteme}%
haftmann@28447
  1064
%
haftmann@28447
  1065
\isadelimquoteme
haftmann@28447
  1066
%
haftmann@28447
  1067
\endisadelimquoteme
haftmann@28447
  1068
%
haftmann@28462
  1069
\isamarkupsubsection{Explicit partiality%
haftmann@28447
  1070
}
haftmann@28447
  1071
\isamarkuptrue%
haftmann@28447
  1072
%
haftmann@28447
  1073
\begin{isamarkuptext}%
haftmann@28462
  1074
Partiality usually enters the game by partial patterns, as
haftmann@28462
  1075
  in the following example, again for amortised queues:%
haftmann@28462
  1076
\end{isamarkuptext}%
haftmann@28462
  1077
\isamarkuptrue%
haftmann@28462
  1078
%
haftmann@28462
  1079
\isadelimquoteme
haftmann@28462
  1080
%
haftmann@28462
  1081
\endisadelimquoteme
haftmann@28462
  1082
%
haftmann@28462
  1083
\isatagquoteme
haftmann@28462
  1084
\isacommand{fun}\isamarkupfalse%
haftmann@28462
  1085
\ strict{\isacharunderscore}dequeue\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28462
  1086
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}\ Queue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28462
  1087
\ \ {\isacharbar}\ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue\ {\isacharparenleft}Queue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\isanewline
haftmann@28462
  1088
\ \ \ \ \ \ {\isacharparenleft}case\ rev\ xs\ of\ y\ {\isacharhash}\ ys\ {\isasymRightarrow}\ {\isacharparenleft}y{\isacharcomma}\ Queue\ {\isacharbrackleft}{\isacharbrackright}\ ys{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}%
haftmann@28462
  1089
\endisatagquoteme
haftmann@28462
  1090
{\isafoldquoteme}%
haftmann@28462
  1091
%
haftmann@28462
  1092
\isadelimquoteme
haftmann@28462
  1093
%
haftmann@28462
  1094
\endisadelimquoteme
haftmann@28462
  1095
%
haftmann@28462
  1096
\begin{isamarkuptext}%
haftmann@28462
  1097
\noindent In the corresponding code, there is no equation
haftmann@28462
  1098
  for the pattern \isa{Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharbrackleft}{\isacharbrackright}}:%
haftmann@28462
  1099
\end{isamarkuptext}%
haftmann@28462
  1100
\isamarkuptrue%
haftmann@28462
  1101
%
haftmann@28462
  1102
\isadelimquoteme
haftmann@28462
  1103
%
haftmann@28462
  1104
\endisadelimquoteme
haftmann@28462
  1105
%
haftmann@28462
  1106
\isatagquoteme
haftmann@28462
  1107
%
haftmann@28462
  1108
\begin{isamarkuptext}%
haftmann@28462
  1109
\isaverbatim%
haftmann@28462
  1110
\noindent%
haftmann@28462
  1111
\verb|strict_dequeue :: forall a. Queue a -> (a, Queue a);|\newline%
haftmann@28462
  1112
\verb|strict_dequeue (Queue xs (y : ys)) = (y, Queue xs ys);|\newline%
haftmann@28462
  1113
\verb|strict_dequeue (Queue xs []) =|\newline%
haftmann@28462
  1114
\verb|  let {|\newline%
haftmann@28462
  1115
\verb|    (y : ys) = rev xs;|\newline%
haftmann@28462
  1116
\verb|  } in (y, Queue [] ys);|%
haftmann@28462
  1117
\end{isamarkuptext}%
haftmann@28462
  1118
\isamarkuptrue%
haftmann@28462
  1119
%
haftmann@28462
  1120
\endisatagquoteme
haftmann@28462
  1121
{\isafoldquoteme}%
haftmann@28462
  1122
%
haftmann@28462
  1123
\isadelimquoteme
haftmann@28462
  1124
%
haftmann@28462
  1125
\endisadelimquoteme
haftmann@28462
  1126
%
haftmann@28462
  1127
\begin{isamarkuptext}%
haftmann@28462
  1128
\noindent In some cases it is desirable to have this
haftmann@28462
  1129
  pseudo-\qt{partiality} more explicitly, e.g.~as follows:%
haftmann@28462
  1130
\end{isamarkuptext}%
haftmann@28462
  1131
\isamarkuptrue%
haftmann@28462
  1132
%
haftmann@28462
  1133
\isadelimquoteme
haftmann@28462
  1134
%
haftmann@28462
  1135
\endisadelimquoteme
haftmann@28462
  1136
%
haftmann@28462
  1137
\isatagquoteme
haftmann@28462
  1138
\isacommand{axiomatization}\isamarkupfalse%
haftmann@28462
  1139
\ empty{\isacharunderscore}queue\ {\isacharcolon}{\isacharcolon}\ {\isacharprime}a\isanewline
haftmann@28462
  1140
\isanewline
haftmann@28462
  1141
\isacommand{function}\isamarkupfalse%
haftmann@28462
  1142
\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ queue\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ queue{\isachardoublequoteclose}\ \isakeyword{where}\isanewline
haftmann@28462
  1143
\ \ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}Queue\ xs\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ xs\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}\ then\ empty{\isacharunderscore}queue\isanewline
haftmann@28462
  1144
\ \ \ \ \ else\ strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}Queue\ {\isacharbrackleft}{\isacharbrackright}\ {\isacharparenleft}rev\ xs{\isacharparenright}{\isacharparenright}{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28462
  1145
\ \ {\isacharbar}\ {\isachardoublequoteopen}strict{\isacharunderscore}dequeue{\isacharprime}\ {\isacharparenleft}Queue\ xs\ {\isacharparenleft}y\ {\isacharhash}\ ys{\isacharparenright}{\isacharparenright}\ {\isacharequal}\isanewline
haftmann@28462
  1146
\ \ \ \ \ \ \ {\isacharparenleft}y{\isacharcomma}\ Queue\ xs\ ys{\isacharparenright}{\isachardoublequoteclose}\isanewline
haftmann@28462
  1147
\isacommand{by}\isamarkupfalse%
haftmann@28462
  1148
\ pat{\isacharunderscore}completeness\ auto\isanewline
haftmann@28462
  1149
\isanewline
haftmann@28462
  1150
\isacommand{termination}\isamarkupfalse%
haftmann@28462
  1151
\ strict{\isacharunderscore}dequeue{\isacharprime}\isanewline
haftmann@28462
  1152
\isacommand{by}\isamarkupfalse%
haftmann@28462
  1153
\ {\isacharparenleft}relation\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}q{\isacharcolon}{\isacharcolon}{\isacharprime}a\ queue{\isachardot}\ case\ q\ of\ Queue\ xs\ ys\ {\isasymRightarrow}\ length\ xs{\isacharparenright}{\isachardoublequoteclose}{\isacharparenright}\ simp{\isacharunderscore}all%
haftmann@28462
  1154
\endisatagquoteme
haftmann@28462
  1155
{\isafoldquoteme}%
haftmann@28462
  1156
%
haftmann@28462
  1157
\isadelimquoteme
haftmann@28462
  1158
%
haftmann@28462
  1159
\endisadelimquoteme
haftmann@28462
  1160
%
haftmann@28462
  1161
\begin{isamarkuptext}%
haftmann@28462
  1162
\noindent For technical reasons the definition of
haftmann@28462
  1163
  \isa{strict{\isacharunderscore}dequeue{\isacharprime}} is more involved since it requires
haftmann@28462
  1164
  a manual termination proof.  Apart from that observe that
haftmann@28462
  1165
  on the right hand side of its first equation the constant
haftmann@28462
  1166
  \isa{empty{\isacharunderscore}queue} occurs which is unspecified.
haftmann@28462
  1167
haftmann@28462
  1168
  Normally, if constants without any defining equations occur in
haftmann@28462
  1169
  a program, the code generator complains (since in most cases
haftmann@28462
  1170
  this is not what the user expects).  But such constants can also
haftmann@28462
  1171
  be thought of as function definitions with no equations which
haftmann@28462
  1172
  always fail, since there is never a successful pattern match
haftmann@28462
  1173
  on the left hand side.  In order to categorize a constant into
haftmann@28462
  1174
  that category explicitly, use \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}:%
haftmann@28462
  1175
\end{isamarkuptext}%
haftmann@28462
  1176
\isamarkuptrue%
haftmann@28462
  1177
%
haftmann@28462
  1178
\isadelimquoteme
haftmann@28462
  1179
%
haftmann@28462
  1180
\endisadelimquoteme
haftmann@28462
  1181
%
haftmann@28462
  1182
\isatagquoteme
haftmann@28462
  1183
\isacommand{code{\isacharunderscore}abort}\isamarkupfalse%
haftmann@28462
  1184
\ empty{\isacharunderscore}queue%
haftmann@28462
  1185
\endisatagquoteme
haftmann@28462
  1186
{\isafoldquoteme}%
haftmann@28462
  1187
%
haftmann@28462
  1188
\isadelimquoteme
haftmann@28462
  1189
%
haftmann@28462
  1190
\endisadelimquoteme
haftmann@28462
  1191
%
haftmann@28462
  1192
\begin{isamarkuptext}%
haftmann@28462
  1193
\noindent Then the code generator will just insert an error or
haftmann@28462
  1194
  exception at the appropriate position:%
haftmann@28462
  1195
\end{isamarkuptext}%
haftmann@28462
  1196
\isamarkuptrue%
haftmann@28462
  1197
%
haftmann@28462
  1198
\isadelimquoteme
haftmann@28462
  1199
%
haftmann@28462
  1200
\endisadelimquoteme
haftmann@28462
  1201
%
haftmann@28462
  1202
\isatagquoteme
haftmann@28462
  1203
%
haftmann@28462
  1204
\begin{isamarkuptext}%
haftmann@28462
  1205
\isaverbatim%
haftmann@28462
  1206
\noindent%
haftmann@28462
  1207
\verb|empty_queue :: forall a. a;|\newline%
haftmann@28462
  1208
\verb|empty_queue = error "empty_queue";|\newline%
haftmann@28462
  1209
\newline%
haftmann@28462
  1210
\verb|strict_dequeue' :: forall a. Queue a -> (a, Queue a);|\newline%
haftmann@28462
  1211
\verb|strict_dequeue' (Queue xs []) =|\newline%
haftmann@28462
  1212
\verb|  (if nulla xs then empty_queue else strict_dequeue' (Queue [] (rev xs)));|\newline%
haftmann@28462
  1213
\verb|strict_dequeue' (Queue xs (y : ys)) = (y, Queue xs ys);|%
haftmann@28462
  1214
\end{isamarkuptext}%
haftmann@28462
  1215
\isamarkuptrue%
haftmann@28462
  1216
%
haftmann@28462
  1217
\endisatagquoteme
haftmann@28462
  1218
{\isafoldquoteme}%
haftmann@28462
  1219
%
haftmann@28462
  1220
\isadelimquoteme
haftmann@28462
  1221
%
haftmann@28462
  1222
\endisadelimquoteme
haftmann@28462
  1223
%
haftmann@28462
  1224
\begin{isamarkuptext}%
haftmann@28462
  1225
\noindent This feature however is rarely needed in practice.
haftmann@28462
  1226
  Note also that the \isa{HOL} default setup already declares
haftmann@28462
  1227
  \isa{undefined} as \hyperlink{command.code-abort}{\mbox{\isa{\isacommand{code{\isacharunderscore}abort}}}}, which is most
haftmann@28462
  1228
  likely to be used in such situations.%
haftmann@28447
  1229
\end{isamarkuptext}%
haftmann@28447
  1230
\isamarkuptrue%
haftmann@28447
  1231
%
haftmann@28447
  1232
\isadelimtheory
haftmann@28447
  1233
%
haftmann@28447
  1234
\endisadelimtheory
haftmann@28447
  1235
%
haftmann@28447
  1236
\isatagtheory
haftmann@28447
  1237
\isacommand{end}\isamarkupfalse%
haftmann@28447
  1238
%
haftmann@28447
  1239
\endisatagtheory
haftmann@28447
  1240
{\isafoldtheory}%
haftmann@28447
  1241
%
haftmann@28447
  1242
\isadelimtheory
haftmann@28447
  1243
%
haftmann@28447
  1244
\endisadelimtheory
haftmann@28447
  1245
\isanewline
haftmann@28462
  1246
\ \end{isabellebody}%
haftmann@28447
  1247
%%% Local Variables:
haftmann@28447
  1248
%%% mode: latex
haftmann@28447
  1249
%%% TeX-master: "root"
haftmann@28447
  1250
%%% End: