doc-src/TutorialI/Advanced/document/Partial.tex
author wenzelm
Fri, 28 Sep 2001 19:18:46 +0200
changeset 11627 abf9cda4a4d2
parent 11494 23a118849801
child 11866 fbd097aec213
permissions -rw-r--r--
updated;
nipkow@10654
     1
%
nipkow@10654
     2
\begin{isabellebody}%
nipkow@10654
     3
\def\isabellecontext{Partial}%
nipkow@10654
     4
%
nipkow@10654
     5
\begin{isamarkuptext}%
paulson@11494
     6
\noindent Throughout this tutorial, we have emphasized
paulson@11494
     7
that all functions in HOL are total.  We cannot hope to define
paulson@11310
     8
truly partial functions, but must make them total.  A straightforward
paulson@11310
     9
method is to lift the result type of the function from $\tau$ to
nipkow@11277
    10
$\tau$~\isa{option} (see \ref{sec:option}), where \isa{None} is
nipkow@11277
    11
returned if the function is applied to an argument not in its
nipkow@11277
    12
domain. Function \isa{assoc} in \S\ref{sec:Trie} is a simple example.
nipkow@11277
    13
We do not pursue this schema further because it should be clear
nipkow@11277
    14
how it works. Its main drawback is that the result of such a lifted
nipkow@11277
    15
function has to be unpacked first before it can be processed
nipkow@11277
    16
further. Its main advantage is that you can distinguish if the
nipkow@11277
    17
function was applied to an argument in its domain or not. If you do
nipkow@11277
    18
not need to make this distinction, for example because the function is
nipkow@11277
    19
never used outside its domain, it is easier to work with
paulson@11428
    20
\emph{underdefined}\index{functions!underdefined} functions: for
nipkow@11277
    21
certain arguments we only know that a result exists, but we do not
nipkow@11277
    22
know what it is. When defining functions that are normally considered
nipkow@11277
    23
partial, underdefinedness turns out to be a very reasonable
nipkow@11277
    24
alternative.
nipkow@10654
    25
nipkow@10654
    26
We have already seen an instance of underdefinedness by means of
nipkow@10654
    27
non-exhaustive pattern matching: the definition of \isa{last} in
nipkow@10654
    28
\S\ref{sec:recdef-examples}. The same is allowed for \isacommand{primrec}%
nipkow@10654
    29
\end{isamarkuptext}%
nipkow@10654
    30
\isacommand{consts}\ hd\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequote}\isanewline
nipkow@10654
    31
\isacommand{primrec}\ {\isachardoublequote}hd\ {\isacharparenleft}x{\isacharhash}xs{\isacharparenright}\ {\isacharequal}\ x{\isachardoublequote}%
nipkow@10654
    32
\begin{isamarkuptext}%
nipkow@10654
    33
\noindent
nipkow@10654
    34
although it generates a warning.
nipkow@10654
    35
Even ordinary definitions allow underdefinedness, this time by means of
nipkow@10654
    36
preconditions:%
nipkow@10654
    37
\end{isamarkuptext}%
nipkow@10654
    38
\isacommand{constdefs}\ minus\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
nipkow@10654
    39
{\isachardoublequote}n\ {\isasymle}\ m\ {\isasymLongrightarrow}\ minus\ m\ n\ {\isasymequiv}\ m\ {\isacharminus}\ n{\isachardoublequote}%
nipkow@10654
    40
\begin{isamarkuptext}%
nipkow@10654
    41
The rest of this section is devoted to the question of how to define
nipkow@11256
    42
partial recursive functions by other means than non-exhaustive pattern
nipkow@10654
    43
matching.%
nipkow@10654
    44
\end{isamarkuptext}%
nipkow@10654
    45
%
paulson@10878
    46
\isamarkupsubsubsection{Guarded Recursion%
nipkow@10654
    47
}
nipkow@10654
    48
%
nipkow@10654
    49
\begin{isamarkuptext}%
paulson@11494
    50
\index{recursion!guarded}%
nipkow@10654
    51
Neither \isacommand{primrec} nor \isacommand{recdef} allow to
nipkow@10654
    52
prefix an equation with a condition in the way ordinary definitions do
nipkow@10654
    53
(see \isa{minus} above). Instead we have to move the condition over
nipkow@10654
    54
to the right-hand side of the equation. Given a partial function $f$
nipkow@10654
    55
that should satisfy the recursion equation $f(x) = t$ over its domain
nipkow@10654
    56
$dom(f)$, we turn this into the \isacommand{recdef}
nipkow@10654
    57
\begin{isabelle}%
nipkow@10654
    58
\ \ \ \ \ f\ x\ {\isacharequal}\ {\isacharparenleft}if\ x\ {\isasymin}\ dom\ f\ then\ t\ else\ arbitrary{\isacharparenright}%
nipkow@10654
    59
\end{isabelle}
nipkow@10654
    60
where \isa{arbitrary} is a predeclared constant of type \isa{{\isacharprime}a}
nipkow@10654
    61
which has no definition. Thus we know nothing about its value,
nipkow@10654
    62
which is ideal for specifying underdefined functions on top of it.
nipkow@10654
    63
nipkow@10654
    64
As a simple example we define division on \isa{nat}:%
nipkow@10654
    65
\end{isamarkuptext}%
nipkow@10654
    66
\isacommand{consts}\ divi\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymtimes}\ nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
wenzelm@11627
    67
\isacommand{recdef}\ {\isacharparenleft}\isakeyword{permissive}{\isacharparenright}\ divi\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}{\isachardot}\ m{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@10654
    68
\ \ {\isachardoublequote}divi{\isacharparenleft}m{\isacharcomma}n{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ n\ {\isacharequal}\ {\isadigit{0}}\ then\ arbitrary\ else\isanewline
nipkow@10654
    69
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ if\ m\ {\isacharless}\ n\ then\ {\isadigit{0}}\ else\ divi{\isacharparenleft}m{\isacharminus}n{\isacharcomma}n{\isacharparenright}{\isacharplus}{\isadigit{1}}{\isacharparenright}{\isachardoublequote}%
nipkow@10654
    70
\begin{isamarkuptext}%
nipkow@10654
    71
\noindent Of course we could also have defined
nipkow@10654
    72
\isa{divi\ {\isacharparenleft}m{\isacharcomma}\ {\isadigit{0}}{\isacharparenright}} to be some specific number, for example 0. The
nipkow@10654
    73
latter option is chosen for the predefined \isa{div} function, which
paulson@10878
    74
simplifies proofs at the expense of deviating from the
paulson@10878
    75
standard mathematical division function.
nipkow@10654
    76
nipkow@10654
    77
As a more substantial example we consider the problem of searching a graph.
nipkow@11277
    78
For simplicity our graph is given by a function \isa{f} of
nipkow@10654
    79
type \isa{{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a} which
paulson@11494
    80
maps each node to its successor; the graph is really a tree.
nipkow@11196
    81
The task is to find the end of a chain, modelled by a node pointing to
nipkow@11196
    82
itself. Here is a first attempt:
nipkow@10654
    83
\begin{isabelle}%
nipkow@10654
    84
\ \ \ \ \ find\ {\isacharparenleft}f{\isacharcomma}\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ f\ x\ {\isacharequal}\ x\ then\ x\ else\ find\ {\isacharparenleft}f{\isacharcomma}\ f\ x{\isacharparenright}{\isacharparenright}%
nipkow@10654
    85
\end{isabelle}
nipkow@10654
    86
This may be viewed as a fixed point finder or as one half of the well known
nipkow@10654
    87
\emph{Union-Find} algorithm.
nipkow@11149
    88
The snag is that it may not terminate if \isa{f} has non-trivial cycles.
nipkow@10654
    89
Phrased differently, the relation%
nipkow@10654
    90
\end{isamarkuptext}%
nipkow@10654
    91
\isacommand{constdefs}\ step{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a{\isacharparenright}set{\isachardoublequote}\isanewline
nipkow@10654
    92
\ \ {\isachardoublequote}step{\isadigit{1}}\ f\ {\isasymequiv}\ {\isacharbraceleft}{\isacharparenleft}y{\isacharcomma}x{\isacharparenright}{\isachardot}\ y\ {\isacharequal}\ f\ x\ {\isasymand}\ y\ {\isasymnoteq}\ x{\isacharbraceright}{\isachardoublequote}%
nipkow@10654
    93
\begin{isamarkuptext}%
nipkow@10654
    94
\noindent
nipkow@10654
    95
must be well-founded. Thus we make the following definition:%
nipkow@10654
    96
\end{isamarkuptext}%
nipkow@10654
    97
\isacommand{consts}\ find\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymtimes}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequote}\isanewline
nipkow@10654
    98
\isacommand{recdef}\ find\ {\isachardoublequote}same{\isacharunderscore}fst\ {\isacharparenleft}{\isasymlambda}f{\isachardot}\ wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}{\isacharparenright}\ step{\isadigit{1}}{\isachardoublequote}\isanewline
nipkow@10654
    99
\ \ {\isachardoublequote}find{\isacharparenleft}f{\isacharcomma}x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\isanewline
nipkow@10654
   100
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ then\ if\ f\ x\ {\isacharequal}\ x\ then\ x\ else\ find{\isacharparenleft}f{\isacharcomma}\ f\ x{\isacharparenright}\isanewline
nipkow@10654
   101
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ else\ arbitrary{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@11285
   102
{\isacharparenleft}\isakeyword{hints}\ recdef{\isacharunderscore}simp{\isacharcolon}\ step{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}%
nipkow@10654
   103
\begin{isamarkuptext}%
nipkow@10654
   104
\noindent
nipkow@10654
   105
The recursion equation itself should be clear enough: it is our aborted
nipkow@10654
   106
first attempt augmented with a check that there are no non-trivial loops.
nipkow@11277
   107
To express the required well-founded relation we employ the
nipkow@11196
   108
predefined combinator \isa{same{\isacharunderscore}fst} of type
nipkow@10654
   109
\begin{isabelle}%
nipkow@10654
   110
\ \ \ \ \ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}b{\isasymtimes}{\isacharprime}b{\isacharparenright}set{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharparenleft}{\isacharprime}a{\isasymtimes}{\isacharprime}b{\isacharparenright}\ {\isasymtimes}\ {\isacharparenleft}{\isacharprime}a{\isasymtimes}{\isacharprime}b{\isacharparenright}{\isacharparenright}set%
nipkow@10654
   111
\end{isabelle}
nipkow@10654
   112
defined as
nipkow@10654
   113
\begin{isabelle}%
nipkow@10654
   114
\ \ \ \ \ same{\isacharunderscore}fst\ P\ R\ {\isasymequiv}\ {\isacharbraceleft}{\isacharparenleft}{\isacharparenleft}x{\isacharprime}{\isacharcomma}\ y{\isacharprime}{\isacharparenright}{\isacharcomma}\ x{\isacharcomma}\ y{\isacharparenright}{\isachardot}\ x{\isacharprime}\ {\isacharequal}\ x\ {\isasymand}\ P\ x\ {\isasymand}\ {\isacharparenleft}y{\isacharprime}{\isacharcomma}\ y{\isacharparenright}\ {\isasymin}\ R\ x{\isacharbraceright}%
nipkow@10654
   115
\end{isabelle}
nipkow@11196
   116
This combinator is designed for
nipkow@11196
   117
recursive functions on pairs where the first component of the argument is
nipkow@11196
   118
passed unchanged to all recursive calls. Given a constraint on the first
nipkow@11196
   119
component and a relation on the second component, \isa{same{\isacharunderscore}fst} builds the
nipkow@11196
   120
required relation on pairs.  The theorem
nipkow@11196
   121
\begin{isabelle}%
nipkow@10654
   122
\ \ \ \ \ {\isacharparenleft}{\isasymAnd}x{\isachardot}\ P\ x\ {\isasymLongrightarrow}\ wf\ {\isacharparenleft}R\ x{\isacharparenright}{\isacharparenright}\ {\isasymLongrightarrow}\ wf\ {\isacharparenleft}same{\isacharunderscore}fst\ P\ R{\isacharparenright}%
nipkow@10654
   123
\end{isabelle}
nipkow@11196
   124
is known to the well-foundedness prover of \isacommand{recdef}.  Thus
nipkow@11196
   125
well-foundedness of the relation given to \isacommand{recdef} is immediate.
nipkow@11196
   126
Furthermore, each recursive call descends along that relation: the first
nipkow@11285
   127
argument stays unchanged and the second one descends along \isa{step{\isadigit{1}}\ f}. The proof requires unfolding the definition of \isa{step{\isadigit{1}}},
nipkow@11285
   128
as specified in the \isacommand{hints} above.
nipkow@10654
   129
paulson@11494
   130
Normally you will then derive the following conditional variant from
paulson@11494
   131
the recursion equation:%
nipkow@10654
   132
\end{isamarkuptext}%
nipkow@10654
   133
\isacommand{lemma}\ {\isacharbrackleft}simp{\isacharbrackright}{\isacharcolon}\isanewline
nipkow@10654
   134
\ \ {\isachardoublequote}wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\ {\isasymLongrightarrow}\ find{\isacharparenleft}f{\isacharcomma}x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}if\ f\ x\ {\isacharequal}\ x\ then\ x\ else\ find{\isacharparenleft}f{\isacharcomma}\ f\ x{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@10654
   135
\isacommand{by}\ simp%
nipkow@10654
   136
\begin{isamarkuptext}%
paulson@11494
   137
\noindent Then you should disable the original recursion equation:%
nipkow@10654
   138
\end{isamarkuptext}%
nipkow@10654
   139
\isacommand{declare}\ find{\isachardot}simps{\isacharbrackleft}simp\ del{\isacharbrackright}%
nipkow@10654
   140
\begin{isamarkuptext}%
paulson@11494
   141
Reasoning about such underdefined functions is like that for other
paulson@11494
   142
recursive functions.  Here is a simple example of recursion induction:%
nipkow@10654
   143
\end{isamarkuptext}%
nipkow@10654
   144
\isacommand{lemma}\ {\isachardoublequote}wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\ {\isasymlongrightarrow}\ f{\isacharparenleft}find{\isacharparenleft}f{\isacharcomma}x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ find{\isacharparenleft}f{\isacharcomma}x{\isacharparenright}{\isachardoublequote}\isanewline
nipkow@10654
   145
\isacommand{apply}{\isacharparenleft}induct{\isacharunderscore}tac\ f\ x\ rule{\isacharcolon}find{\isachardot}induct{\isacharparenright}\isanewline
nipkow@10654
   146
\isacommand{apply}\ simp\isanewline
nipkow@10654
   147
\isacommand{done}%
paulson@10878
   148
\isamarkupsubsubsection{The {\tt\slshape while} Combinator%
nipkow@10654
   149
}
nipkow@10654
   150
%
nipkow@10654
   151
\begin{isamarkuptext}%
nipkow@10654
   152
If the recursive function happens to be tail recursive, its
paulson@11428
   153
definition becomes a triviality if based on the predefined \cdx{while}
paulson@10878
   154
combinator.  The latter lives in the Library theory
paulson@11428
   155
\thydx{While_Combinator}, which is not part of \isa{Main} but needs to
nipkow@10654
   156
be included explicitly among the ancestor theories.
nipkow@10654
   157
nipkow@10654
   158
Constant \isa{while} is of type \isa{{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ bool{\isacharparenright}\ {\isasymRightarrow}\ {\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a}
nipkow@10654
   159
and satisfies the recursion equation \begin{isabelle}%
nipkow@10654
   160
\ \ \ \ \ while\ b\ c\ s\ {\isacharequal}\ {\isacharparenleft}if\ b\ s\ then\ while\ b\ c\ {\isacharparenleft}c\ s{\isacharparenright}\ else\ s{\isacharparenright}%
nipkow@10654
   161
\end{isabelle}
nipkow@10654
   162
That is, \isa{while\ b\ c\ s} is equivalent to the imperative program
nipkow@10654
   163
\begin{verbatim}
nipkow@10654
   164
     x := s; while b(x) do x := c(x); return x
nipkow@10654
   165
\end{verbatim}
paulson@11494
   166
In general, \isa{s} will be a tuple or record.  As an example
paulson@11494
   167
consider the following definition of function \isa{find}:%
nipkow@10654
   168
\end{isamarkuptext}%
nipkow@10654
   169
\isacommand{constdefs}\ find{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharparenleft}{\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isacharparenright}\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequote}\isanewline
nipkow@10654
   170
\ \ {\isachardoublequote}find{\isadigit{2}}\ f\ x\ {\isasymequiv}\isanewline
nipkow@10654
   171
\ \ \ fst{\isacharparenleft}while\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}x{\isacharprime}{\isacharparenright}{\isachardot}\ x{\isacharprime}\ {\isasymnoteq}\ x{\isacharparenright}\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}x{\isacharprime}{\isacharparenright}{\isachardot}\ {\isacharparenleft}x{\isacharprime}{\isacharcomma}f\ x{\isacharprime}{\isacharparenright}{\isacharparenright}\ {\isacharparenleft}x{\isacharcomma}f\ x{\isacharparenright}{\isacharparenright}{\isachardoublequote}%
nipkow@10654
   172
\begin{isamarkuptext}%
nipkow@10654
   173
\noindent
nipkow@10654
   174
The loop operates on two ``local variables'' \isa{x} and \isa{x{\isacharprime}}
nipkow@10654
   175
containing the ``current'' and the ``next'' value of function \isa{f}.
paulson@11310
   176
They are initialized with the global \isa{x} and \isa{f\ x}. At the
nipkow@10654
   177
end \isa{fst} selects the local \isa{x}.
nipkow@10654
   178
nipkow@11158
   179
Although the definition of tail recursive functions via \isa{while} avoids
nipkow@11158
   180
termination proofs, there is no free lunch. When proving properties of
nipkow@11158
   181
functions defined by \isa{while}, termination rears its ugly head
paulson@11494
   182
again. Here is \tdx{while_rule}, the well known proof rule for total
nipkow@10654
   183
correctness of loops expressed with \isa{while}:
nipkow@10654
   184
\begin{isabelle}%
nipkow@10696
   185
\ \ \ \ \ {\isasymlbrakk}P\ s{\isacharsemicolon}\ {\isasymAnd}s{\isachardot}\ {\isasymlbrakk}P\ s{\isacharsemicolon}\ b\ s{\isasymrbrakk}\ {\isasymLongrightarrow}\ P\ {\isacharparenleft}c\ s{\isacharparenright}{\isacharsemicolon}\isanewline
wenzelm@10950
   186
\isaindent{\ \ \ \ \ \ \ \ }{\isasymAnd}s{\isachardot}\ {\isasymlbrakk}P\ s{\isacharsemicolon}\ {\isasymnot}\ b\ s{\isasymrbrakk}\ {\isasymLongrightarrow}\ Q\ s{\isacharsemicolon}\ wf\ r{\isacharsemicolon}\isanewline
wenzelm@10950
   187
\isaindent{\ \ \ \ \ \ \ \ }{\isasymAnd}s{\isachardot}\ {\isasymlbrakk}P\ s{\isacharsemicolon}\ b\ s{\isasymrbrakk}\ {\isasymLongrightarrow}\ {\isacharparenleft}c\ s{\isacharcomma}\ s{\isacharparenright}\ {\isasymin}\ r{\isasymrbrakk}\isanewline
wenzelm@10950
   188
\isaindent{\ \ \ \ \ }{\isasymLongrightarrow}\ Q\ {\isacharparenleft}while\ b\ c\ s{\isacharparenright}%
nipkow@11158
   189
\end{isabelle} \isa{P} needs to be true of
nipkow@11158
   190
the initial state \isa{s} and invariant under \isa{c} (premises 1
nipkow@11158
   191
and~2). The post-condition \isa{Q} must become true when leaving the loop
nipkow@11158
   192
(premise~3). And each loop iteration must descend along a well-founded
nipkow@11158
   193
relation \isa{r} (premises 4 and~5).
nipkow@10654
   194
nipkow@10654
   195
Let us now prove that \isa{find{\isadigit{2}}} does indeed find a fixed point. Instead
nipkow@10654
   196
of induction we apply the above while rule, suitably instantiated.
nipkow@10654
   197
Only the final premise of \isa{while{\isacharunderscore}rule} is left unproved
nipkow@10654
   198
by \isa{auto} but falls to \isa{simp}:%
nipkow@10654
   199
\end{isamarkuptext}%
nipkow@11277
   200
\isacommand{lemma}\ lem{\isacharcolon}\ {\isachardoublequote}wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\ {\isasymLongrightarrow}\isanewline
nipkow@11277
   201
\ \ {\isasymexists}y{\isachardot}\ while\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}x{\isacharprime}{\isacharparenright}{\isachardot}\ x{\isacharprime}\ {\isasymnoteq}\ x{\isacharparenright}\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}x{\isacharprime}{\isacharparenright}{\isachardot}\ {\isacharparenleft}x{\isacharprime}{\isacharcomma}f\ x{\isacharprime}{\isacharparenright}{\isacharparenright}\ {\isacharparenleft}x{\isacharcomma}f\ x{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}y{\isacharcomma}y{\isacharparenright}\ {\isasymand}\isanewline
paulson@10878
   202
\ \ \ \ \ \ \ f\ y\ {\isacharequal}\ y{\isachardoublequote}\isanewline
nipkow@10654
   203
\isacommand{apply}{\isacharparenleft}rule{\isacharunderscore}tac\ P\ {\isacharequal}\ {\isachardoublequote}{\isasymlambda}{\isacharparenleft}x{\isacharcomma}x{\isacharprime}{\isacharparenright}{\isachardot}\ x{\isacharprime}\ {\isacharequal}\ f\ x{\isachardoublequote}\ \isakeyword{and}\isanewline
nipkow@10654
   204
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r\ {\isacharequal}\ {\isachardoublequote}inv{\isacharunderscore}image\ {\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\ fst{\isachardoublequote}\ \isakeyword{in}\ while{\isacharunderscore}rule{\isacharparenright}\isanewline
nipkow@10654
   205
\isacommand{apply}\ auto\isanewline
nipkow@10654
   206
\isacommand{apply}{\isacharparenleft}simp\ add{\isacharcolon}inv{\isacharunderscore}image{\isacharunderscore}def\ step{\isadigit{1}}{\isacharunderscore}def{\isacharparenright}\isanewline
nipkow@10654
   207
\isacommand{done}%
nipkow@10654
   208
\begin{isamarkuptext}%
nipkow@10654
   209
The theorem itself is a simple consequence of this lemma:%
nipkow@10654
   210
\end{isamarkuptext}%
nipkow@10654
   211
\isacommand{theorem}\ {\isachardoublequote}wf{\isacharparenleft}step{\isadigit{1}}\ f{\isacharparenright}\ {\isasymLongrightarrow}\ f{\isacharparenleft}find{\isadigit{2}}\ f\ x{\isacharparenright}\ {\isacharequal}\ find{\isadigit{2}}\ f\ x{\isachardoublequote}\isanewline
nipkow@10654
   212
\isacommand{apply}{\isacharparenleft}drule{\isacharunderscore}tac\ x\ {\isacharequal}\ x\ \isakeyword{in}\ lem{\isacharparenright}\isanewline
nipkow@10654
   213
\isacommand{apply}{\isacharparenleft}auto\ simp\ add{\isacharcolon}find{\isadigit{2}}{\isacharunderscore}def{\isacharparenright}\isanewline
nipkow@10654
   214
\isacommand{done}%
nipkow@10654
   215
\begin{isamarkuptext}%
nipkow@10654
   216
Let us conclude this section on partial functions by a
nipkow@10654
   217
discussion of the merits of the \isa{while} combinator. We have
paulson@11494
   218
already seen that the advantage of not having to
paulson@11310
   219
provide a termination argument when defining a function via \isa{while} merely puts off the evil hour. On top of that, tail recursive
nipkow@10654
   220
functions tend to be more complicated to reason about. So why use
nipkow@10654
   221
\isa{while} at all? The only reason is executability: the recursion
nipkow@10654
   222
equation for \isa{while} is a directly executable functional
nipkow@10654
   223
program. This is in stark contrast to guarded recursion as introduced
nipkow@10654
   224
above which requires an explicit test \isa{x\ {\isasymin}\ dom\ f} in the
nipkow@10654
   225
function body.  Unless \isa{dom} is trivial, this leads to a
nipkow@11196
   226
definition that is impossible to execute or prohibitively slow.
paulson@10878
   227
Thus, if you are aiming for an efficiently executable definition
nipkow@10654
   228
of a partial function, you are likely to need \isa{while}.%
nipkow@10654
   229
\end{isamarkuptext}%
nipkow@10654
   230
\end{isabellebody}%
nipkow@10654
   231
%%% Local Variables:
nipkow@10654
   232
%%% mode: latex
nipkow@10654
   233
%%% TeX-master: "root"
nipkow@10654
   234
%%% End: