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