| 9722 |      1 | %
 | 
|  |      2 | \begin{isabellebody}%
 | 
| 10267 |      3 | \def\isabellecontext{Nested{\isadigit{1}}}%
 | 
| 11866 |      4 | \isamarkupfalse%
 | 
| 10186 |      5 | %
 | 
| 9698 |      6 | \begin{isamarkuptext}%
 | 
|  |      7 | \noindent
 | 
| 11277 |      8 | Although the definition of \isa{trev} below is quite natural, we will have
 | 
| 10878 |      9 | to overcome a minor difficulty in convincing Isabelle of its termination.
 | 
| 9754 |     10 | It is precisely this difficulty that is the \textit{raison d'\^etre} of
 | 
| 9698 |     11 | this subsection.
 | 
|  |     12 | 
 | 
|  |     13 | Defining \isa{trev} by \isacommand{recdef} rather than \isacommand{primrec}
 | 
|  |     14 | simplifies matters because we are now free to use the recursion equation
 | 
|  |     15 | suggested at the end of \S\ref{sec:nested-datatype}:%
 | 
|  |     16 | \end{isamarkuptext}%
 | 
| 11866 |     17 | \isamarkuptrue%
 | 
| 11636 |     18 | \isacommand{recdef}\ trev\ {\isachardoublequote}measure\ size{\isachardoublequote}\isanewline
 | 
|  |     19 | \ {\isachardoublequote}trev\ {\isacharparenleft}Var\ x{\isacharparenright}\ \ \ \ {\isacharequal}\ Var\ x{\isachardoublequote}\isanewline
 | 
| 11866 |     20 | \ {\isachardoublequote}trev\ {\isacharparenleft}App\ f\ ts{\isacharparenright}\ {\isacharequal}\ App\ f\ {\isacharparenleft}rev{\isacharparenleft}map\ trev\ ts{\isacharparenright}{\isacharparenright}{\isachardoublequote}\isamarkupfalse%
 | 
|  |     21 | %
 | 
| 11636 |     22 | \begin{isamarkuptext}%
 | 
|  |     23 | \noindent
 | 
|  |     24 | Remember that function \isa{size} is defined for each \isacommand{datatype}.
 | 
|  |     25 | However, the definition does not succeed. Isabelle complains about an
 | 
|  |     26 | unproved termination condition
 | 
|  |     27 | \begin{isabelle}%
 | 
|  |     28 | \ \ \ \ \ t\ {\isasymin}\ set\ ts\ {\isasymlongrightarrow}\ size\ t\ {\isacharless}\ Suc\ {\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}%
 | 
|  |     29 | \end{isabelle}
 | 
|  |     30 | where \isa{set} returns the set of elements of a list
 | 
|  |     31 | and \isa{term{\isacharunderscore}list{\isacharunderscore}size\ {\isacharcolon}{\isacharcolon}\ term\ list\ {\isasymRightarrow}\ nat} is an auxiliary
 | 
|  |     32 | function automatically defined by Isabelle
 | 
|  |     33 | (while processing the declaration of \isa{term}).  Why does the
 | 
|  |     34 | recursive call of \isa{trev} lead to this
 | 
|  |     35 | condition?  Because \isacommand{recdef} knows that \isa{map}
 | 
|  |     36 | will apply \isa{trev} only to elements of \isa{ts}. Thus the 
 | 
|  |     37 | condition expresses that the size of the argument \isa{t\ {\isasymin}\ set\ ts} of any
 | 
|  |     38 | recursive call of \isa{trev} is strictly less than \isa{size\ {\isacharparenleft}App\ f\ ts{\isacharparenright}},
 | 
|  |     39 | which equals \isa{Suc\ {\isacharparenleft}term{\isacharunderscore}list{\isacharunderscore}size\ ts{\isacharparenright}}.  We will now prove the termination condition and
 | 
|  |     40 | continue with our definition.  Below we return to the question of how
 | 
| 12491 |     41 | \isacommand{recdef} knows about \isa{map}.
 | 
|  |     42 | 
 | 
|  |     43 | The termination condition is easily proved by induction:%
 | 
| 11636 |     44 | \end{isamarkuptext}%
 | 
| 11866 |     45 | \isamarkuptrue%
 | 
|  |     46 | \isamarkupfalse%
 | 
| 11636 |     47 | \end{isabellebody}%
 | 
| 9698 |     48 | %%% Local Variables:
 | 
|  |     49 | %%% mode: latex
 | 
|  |     50 | %%% TeX-master: "root"
 | 
|  |     51 | %%% End:
 |