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