| 
9722
 | 
     1  | 
%
  | 
| 
 | 
     2  | 
\begin{isabellebody}%
 | 
| 
9924
 | 
     3  | 
\def\isabellecontext{examples}%
 | 
| 
17056
 | 
     4  | 
%
  | 
| 
 | 
     5  | 
\isadelimtheory
  | 
| 
 | 
     6  | 
%
  | 
| 
 | 
     7  | 
\endisadelimtheory
  | 
| 
 | 
     8  | 
%
  | 
| 
 | 
     9  | 
\isatagtheory
  | 
| 
 | 
    10  | 
%
  | 
| 
 | 
    11  | 
\endisatagtheory
  | 
| 
 | 
    12  | 
{\isafoldtheory}%
 | 
| 
 | 
    13  | 
%
  | 
| 
 | 
    14  | 
\isadelimtheory
  | 
| 
 | 
    15  | 
%
  | 
| 
 | 
    16  | 
\endisadelimtheory
  | 
| 
8749
 | 
    17  | 
%
  | 
| 
 | 
    18  | 
\begin{isamarkuptext}%
 | 
| 
11458
 | 
    19  | 
Here is a simple example, the \rmindex{Fibonacci function}:%
 | 
| 
8749
 | 
    20  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    21  | 
\isamarkuptrue%
  | 
| 
 | 
    22  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    23  | 
\ fib\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}nat\ {\isasymRightarrow}\ nat{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    24  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    25  | 
\ fib\ {\isachardoublequoteopen}measure{\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    26  | 
\ \ {\isachardoublequoteopen}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    27  | 
\ \ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc\ {\isadigit{0}}{\isacharparenright}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    28  | 
\ \ {\isachardoublequoteopen}fib\ {\isacharparenleft}Suc{\isacharparenleft}Suc\ x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ fib\ x\ {\isacharplus}\ fib\ {\isacharparenleft}Suc\ x{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
8749
 | 
    29  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    30  | 
\noindent
  | 
| 
11458
 | 
    31  | 
\index{measure functions}%
 | 
| 
 | 
    32  | 
The definition of \isa{fib} is accompanied by a \textbf{measure function}
 | 
| 
9792
 | 
    33  | 
\isa{{\isasymlambda}n{\isachardot}\ n} which maps the argument of \isa{fib} to a
 | 
| 
8749
 | 
    34  | 
natural number. The requirement is that in each equation the measure of the
  | 
| 
 | 
    35  | 
argument on the left-hand side is strictly greater than the measure of the
  | 
| 
 | 
    36  | 
argument of each recursive call. In the case of \isa{fib} this is
 | 
| 
 | 
    37  | 
obviously true because the measure function is the identity and
  | 
| 
9792
 | 
    38  | 
\isa{Suc\ {\isacharparenleft}Suc\ x{\isacharparenright}} is strictly greater than both \isa{x} and
 | 
| 
 | 
    39  | 
\isa{Suc\ x}.
 | 
| 
8749
 | 
    40  | 
  | 
| 
 | 
    41  | 
Slightly more interesting is the insertion of a fixed element
  | 
| 
 | 
    42  | 
between any two elements of a list:%
  | 
| 
 | 
    43  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    44  | 
\isamarkuptrue%
  | 
| 
 | 
    45  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    46  | 
\ sep\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    47  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    48  | 
\ sep\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}a{\isacharcomma}xs{\isacharparenright}{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    49  | 
\ \ {\isachardoublequoteopen}sep{\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    50  | 
\ \ {\isachardoublequoteopen}sep{\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ \ \ \ {\isacharequal}\ {\isacharbrackleft}x{\isacharbrackright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    51  | 
\ \ {\isachardoublequoteopen}sep{\isacharparenleft}a{\isacharcomma}\ x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep{\isacharparenleft}a{\isacharcomma}y{\isacharhash}zs{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
8749
 | 
    52  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    53  | 
\noindent
  | 
| 
 | 
    54  | 
This time the measure is the length of the list, which decreases with the
  | 
| 
 | 
    55  | 
recursive call; the first component of the argument tuple is irrelevant.
  | 
| 
10654
 | 
    56  | 
The details of tupled $\lambda$-abstractions \isa{{\isasymlambda}{\isacharparenleft}x\isactrlsub {\isadigit{1}}{\isacharcomma}{\isasymdots}{\isacharcomma}x\isactrlsub n{\isacharparenright}} are
 | 
| 
 | 
    57  | 
explained in \S\ref{sec:products}, but for now your intuition is all you need.
 | 
| 
8749
 | 
    58  | 
  | 
| 
11458
 | 
    59  | 
Pattern matching\index{pattern matching!and \isacommand{recdef}}
 | 
| 
 | 
    60  | 
need not be exhaustive:%
  | 
| 
8749
 | 
    61  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    62  | 
\isamarkuptrue%
  | 
| 
 | 
    63  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    64  | 
\ last\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    65  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    66  | 
\ last\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}xs{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    67  | 
\ \ {\isachardoublequoteopen}last\ {\isacharbrackleft}x{\isacharbrackright}\ \ \ \ \ \ {\isacharequal}\ x{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    68  | 
\ \ {\isachardoublequoteopen}last\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ last\ {\isacharparenleft}y{\isacharhash}zs{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
8749
 | 
    69  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    70  | 
Overlapping patterns are disambiguated by taking the order of equations into
  | 
| 
 | 
    71  | 
account, just as in functional programming:%
  | 
| 
 | 
    72  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    73  | 
\isamarkuptrue%
  | 
| 
 | 
    74  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    75  | 
\ sep{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    76  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
    77  | 
\ sep{\isadigit{1}}\ {\isachardoublequoteopen}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}a{\isacharcomma}xs{\isacharparenright}{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    78  | 
\ \ {\isachardoublequoteopen}sep{\isadigit{1}}{\isacharparenleft}a{\isacharcomma}\ x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep{\isadigit{1}}{\isacharparenleft}a{\isacharcomma}y{\isacharhash}zs{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
    79  | 
\ \ {\isachardoublequoteopen}sep{\isadigit{1}}{\isacharparenleft}a{\isacharcomma}\ xs{\isacharparenright}\ \ \ \ \ {\isacharequal}\ xs{\isachardoublequoteclose}%
 | 
| 
8749
 | 
    80  | 
\begin{isamarkuptext}%
 | 
| 
 | 
    81  | 
\noindent
  | 
| 
11160
 | 
    82  | 
To guarantee that the second equation can only be applied if the first
  | 
| 
 | 
    83  | 
one does not match, Isabelle internally replaces the second equation
  | 
| 
 | 
    84  | 
by the two possibilities that are left: \isa{sep{\isadigit{1}}\ {\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} and
 | 
| 
 | 
    85  | 
\isa{sep{\isadigit{1}}\ {\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharbrackleft}x{\isacharbrackright}}.  Thus the functions \isa{sep} and
 | 
| 
 | 
    86  | 
\isa{sep{\isadigit{1}}} are identical.
 | 
| 
8749
 | 
    87  | 
  | 
| 
 | 
    88  | 
\begin{warn}
 | 
| 
 | 
    89  | 
  \isacommand{recdef} only takes the first argument of a (curried)
 | 
| 
 | 
    90  | 
  recursive function into account. This means both the termination measure
  | 
| 
 | 
    91  | 
  and pattern matching can only use that first argument. In general, you will
  | 
| 
 | 
    92  | 
  therefore have to combine several arguments into a tuple. In case only one
  | 
| 
 | 
    93  | 
  argument is relevant for termination, you can also rearrange the order of
  | 
| 
 | 
    94  | 
  arguments as in the following definition:
  | 
| 
 | 
    95  | 
\end{warn}%
 | 
| 
 | 
    96  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
    97  | 
\isamarkuptrue%
  | 
| 
 | 
    98  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
    99  | 
\ sep{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   100  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
   101  | 
\ sep{\isadigit{2}}\ {\isachardoublequoteopen}measure\ length{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   102  | 
\ \ {\isachardoublequoteopen}sep{\isadigit{2}}\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ {\isacharparenleft}{\isasymlambda}a{\isachardot}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep{\isadigit{2}}\ {\isacharparenleft}y{\isacharhash}zs{\isacharparenright}\ a{\isacharparenright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   103  | 
\ \ {\isachardoublequoteopen}sep{\isadigit{2}}\ xs\ \ \ \ \ \ \ {\isacharequal}\ {\isacharparenleft}{\isasymlambda}a{\isachardot}\ xs{\isacharparenright}{\isachardoublequoteclose}%
 | 
| 
8749
 | 
   104  | 
\begin{isamarkuptext}%
 | 
| 
 | 
   105  | 
Because of its pattern-matching syntax, \isacommand{recdef} is also useful
 | 
| 
11231
 | 
   106  | 
for the definition of non-recursive functions, where the termination measure
  | 
| 
 | 
   107  | 
degenerates to the empty set \isa{{\isacharbraceleft}{\isacharbraceright}}:%
 | 
| 
8749
 | 
   108  | 
\end{isamarkuptext}%
 | 
| 
17175
 | 
   109  | 
\isamarkuptrue%
  | 
| 
 | 
   110  | 
\isacommand{consts}\isamarkupfalse%
 | 
| 
 | 
   111  | 
\ swap{\isadigit{1}}{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequoteopen}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   112  | 
\isacommand{recdef}\isamarkupfalse%
 | 
| 
 | 
   113  | 
\ swap{\isadigit{1}}{\isadigit{2}}\ {\isachardoublequoteopen}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   114  | 
\ \ {\isachardoublequoteopen}swap{\isadigit{1}}{\isadigit{2}}\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ y{\isacharhash}x{\isacharhash}zs{\isachardoublequoteclose}\isanewline
 | 
| 
 | 
   115  | 
\ \ {\isachardoublequoteopen}swap{\isadigit{1}}{\isadigit{2}}\ zs\ \ \ \ \ \ \ {\isacharequal}\ zs{\isachardoublequoteclose}\isanewline
 | 
| 
17056
 | 
   116  | 
%
  | 
| 
 | 
   117  | 
\isadelimtheory
  | 
| 
 | 
   118  | 
%
  | 
| 
 | 
   119  | 
\endisadelimtheory
  | 
| 
 | 
   120  | 
%
  | 
| 
 | 
   121  | 
\isatagtheory
  | 
| 
 | 
   122  | 
%
  | 
| 
 | 
   123  | 
\endisatagtheory
  | 
| 
 | 
   124  | 
{\isafoldtheory}%
 | 
| 
 | 
   125  | 
%
  | 
| 
 | 
   126  | 
\isadelimtheory
  | 
| 
 | 
   127  | 
%
  | 
| 
 | 
   128  | 
\endisadelimtheory
  | 
| 
9722
 | 
   129  | 
\end{isabellebody}%
 | 
| 
9145
 | 
   130  | 
%%% Local Variables:
  | 
| 
 | 
   131  | 
%%% mode: latex
  | 
| 
 | 
   132  | 
%%% TeX-master: "root"
  | 
| 
 | 
   133  | 
%%% End:
  |