doc-src/TutorialI/Recdef/document/examples.tex
author paulson
Fri, 03 Aug 2001 18:04:55 +0200
changeset 11458 09a6c44a48ea
parent 11231 30d96882f915
child 11480 0fba0357c04c
permissions -rw-r--r--
numerous stylistic changes and indexing
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     1
%
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
     2
\begin{isabellebody}%
9924
3370f6aa3200 updated;
wenzelm
parents: 9792
diff changeset
     3
\def\isabellecontext{examples}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     4
%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     5
\begin{isamarkuptext}%
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11231
diff changeset
     6
Here is a simple example, the \rmindex{Fibonacci function}:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
     7
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
     8
\isacommand{consts}\ fib\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}nat\ {\isasymRightarrow}\ nat{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
     9
\isacommand{recdef}\ fib\ {\isachardoublequote}measure{\isacharparenleft}{\isasymlambda}n{\isachardot}\ n{\isacharparenright}{\isachardoublequote}\isanewline
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    10
\ \ {\isachardoublequote}fib\ {\isadigit{0}}\ {\isacharequal}\ {\isadigit{0}}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    11
\ \ {\isachardoublequote}fib\ {\isadigit{1}}\ {\isacharequal}\ {\isadigit{1}}{\isachardoublequote}\isanewline
9674
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    12
\ \ {\isachardoublequote}fib\ {\isacharparenleft}Suc{\isacharparenleft}Suc\ x{\isacharparenright}{\isacharparenright}\ {\isacharequal}\ fib\ x\ {\isacharplus}\ fib\ {\isacharparenleft}Suc\ x{\isacharparenright}{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    13
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    14
\noindent
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11231
diff changeset
    15
\index{measure functions}%
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11231
diff changeset
    16
The definition of \isa{fib} is accompanied by a \textbf{measure function}
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    17
\isa{{\isasymlambda}n{\isachardot}\ n} which maps the argument of \isa{fib} to a
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    18
natural number. The requirement is that in each equation the measure of the
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    19
argument on the left-hand side is strictly greater than the measure of the
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    20
argument of each recursive call. In the case of \isa{fib} this is
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    21
obviously true because the measure function is the identity and
9792
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    22
\isa{Suc\ {\isacharparenleft}Suc\ x{\isacharparenright}} is strictly greater than both \isa{x} and
bbefb6ce5cb2 *** empty log message ***
nipkow
parents: 9722
diff changeset
    23
\isa{Suc\ x}.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    24
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    25
Slightly more interesting is the insertion of a fixed element
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    26
between any two elements of a list:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    27
\end{isamarkuptext}%
9933
9feb1e0c4cb3 *** empty log message ***
nipkow
parents: 9924
diff changeset
    28
\isacommand{consts}\ sep\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
9674
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    29
\isacommand{recdef}\ sep\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}a{\isacharcomma}xs{\isacharparenright}{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    30
\ \ {\isachardoublequote}sep{\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ \ \ \ \ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    31
\ \ {\isachardoublequote}sep{\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ \ \ \ {\isacharequal}\ {\isacharbrackleft}x{\isacharbrackright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    32
\ \ {\isachardoublequote}sep{\isacharparenleft}a{\isacharcomma}\ x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ x\ {\isacharhash}\ a\ {\isacharhash}\ sep{\isacharparenleft}a{\isacharcomma}y{\isacharhash}zs{\isacharparenright}{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    33
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    34
\noindent
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    35
This time the measure is the length of the list, which decreases with the
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    36
recursive call; the first component of the argument tuple is irrelevant.
10654
458068404143 *** empty log message ***
nipkow
parents: 10362
diff changeset
    37
The details of tupled $\lambda$-abstractions \isa{{\isasymlambda}{\isacharparenleft}x\isactrlsub {\isadigit{1}}{\isacharcomma}{\isasymdots}{\isacharcomma}x\isactrlsub n{\isacharparenright}} are
458068404143 *** empty log message ***
nipkow
parents: 10362
diff changeset
    38
explained in \S\ref{sec:products}, but for now your intuition is all you need.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    39
11458
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11231
diff changeset
    40
Pattern matching\index{pattern matching!and \isacommand{recdef}}
09a6c44a48ea numerous stylistic changes and indexing
paulson
parents: 11231
diff changeset
    41
need not be exhaustive:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    42
\end{isamarkuptext}%
9674
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    43
\isacommand{consts}\ last\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    44
\isacommand{recdef}\ last\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}xs{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    45
\ \ {\isachardoublequote}last\ {\isacharbrackleft}x{\isacharbrackright}\ \ \ \ \ \ {\isacharequal}\ x{\isachardoublequote}\isanewline
f789d2490669 updated;
wenzelm
parents: 9644
diff changeset
    46
\ \ {\isachardoublequote}last\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ last\ {\isacharparenleft}y{\isacharhash}zs{\isacharparenright}{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    47
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    48
Overlapping patterns are disambiguated by taking the order of equations into
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    49
account, just as in functional programming:%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    50
\end{isamarkuptext}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    51
\isacommand{consts}\ sep{\isadigit{1}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ {\isasymtimes}\ {\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    52
\isacommand{recdef}\ sep{\isadigit{1}}\ {\isachardoublequote}measure\ {\isacharparenleft}{\isasymlambda}{\isacharparenleft}a{\isacharcomma}xs{\isacharparenright}{\isachardot}\ length\ xs{\isacharparenright}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    53
\ \ {\isachardoublequote}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}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    54
\ \ {\isachardoublequote}sep{\isadigit{1}}{\isacharparenleft}a{\isacharcomma}\ xs{\isacharparenright}\ \ \ \ \ {\isacharequal}\ xs{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    55
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    56
\noindent
11160
e0ab13bec5c8 *** empty log message ***
nipkow
parents: 10654
diff changeset
    57
To guarantee that the second equation can only be applied if the first
e0ab13bec5c8 *** empty log message ***
nipkow
parents: 10654
diff changeset
    58
one does not match, Isabelle internally replaces the second equation
e0ab13bec5c8 *** empty log message ***
nipkow
parents: 10654
diff changeset
    59
by the two possibilities that are left: \isa{sep{\isadigit{1}}\ {\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharbrackleft}{\isacharbrackright}} and
e0ab13bec5c8 *** empty log message ***
nipkow
parents: 10654
diff changeset
    60
\isa{sep{\isadigit{1}}\ {\isacharparenleft}a{\isacharcomma}\ {\isacharbrackleft}x{\isacharbrackright}{\isacharparenright}\ {\isacharequal}\ {\isacharbrackleft}x{\isacharbrackright}}.  Thus the functions \isa{sep} and
e0ab13bec5c8 *** empty log message ***
nipkow
parents: 10654
diff changeset
    61
\isa{sep{\isadigit{1}}} are identical.
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    62
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    63
\begin{warn}
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    64
  \isacommand{recdef} only takes the first argument of a (curried)
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    65
  recursive function into account. This means both the termination measure
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    66
  and pattern matching can only use that first argument. In general, you will
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    67
  therefore have to combine several arguments into a tuple. In case only one
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    68
  argument is relevant for termination, you can also rearrange the order of
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    69
  arguments as in the following definition:
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    70
\end{warn}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    71
\end{isamarkuptext}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    72
\isacommand{consts}\ sep{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    73
\isacommand{recdef}\ sep{\isadigit{2}}\ {\isachardoublequote}measure\ length{\isachardoublequote}\isanewline
10362
c6b197ccf1f1 *** empty log message ***
nipkow
parents: 10187
diff changeset
    74
\ \ {\isachardoublequote}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}{\isachardoublequote}\isanewline
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    75
\ \ {\isachardoublequote}sep{\isadigit{2}}\ xs\ \ \ \ \ \ \ {\isacharequal}\ {\isacharparenleft}{\isasymlambda}a{\isachardot}\ xs{\isacharparenright}{\isachardoublequote}%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    76
\begin{isamarkuptext}%
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    77
Because of its pattern-matching syntax, \isacommand{recdef} is also useful
11231
30d96882f915 *** empty log message ***
nipkow
parents: 11160
diff changeset
    78
for the definition of non-recursive functions, where the termination measure
30d96882f915 *** empty log message ***
nipkow
parents: 11160
diff changeset
    79
degenerates to the empty set \isa{{\isacharbraceleft}{\isacharbraceright}}:%
8749
2665170f104a Adding generated files
nipkow
parents:
diff changeset
    80
\end{isamarkuptext}%
10187
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    81
\isacommand{consts}\ swap{\isadigit{1}}{\isadigit{2}}\ {\isacharcolon}{\isacharcolon}\ {\isachardoublequote}{\isacharprime}a\ list\ {\isasymRightarrow}\ {\isacharprime}a\ list{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    82
\isacommand{recdef}\ swap{\isadigit{1}}{\isadigit{2}}\ {\isachardoublequote}{\isacharbraceleft}{\isacharbraceright}{\isachardoublequote}\isanewline
0376cccd9118 *** empty log message ***
nipkow
parents: 9933
diff changeset
    83
\ \ {\isachardoublequote}swap{\isadigit{1}}{\isadigit{2}}\ {\isacharparenleft}x{\isacharhash}y{\isacharhash}zs{\isacharparenright}\ {\isacharequal}\ y{\isacharhash}x{\isacharhash}zs{\isachardoublequote}\isanewline
11231
30d96882f915 *** empty log message ***
nipkow
parents: 11160
diff changeset
    84
\ \ {\isachardoublequote}swap{\isadigit{1}}{\isadigit{2}}\ zs\ \ \ \ \ \ \ {\isacharequal}\ zs{\isachardoublequote}\isanewline
9722
a5f86aed785b *** empty log message ***
nipkow
parents: 9721
diff changeset
    85
\end{isabellebody}%
9145
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    86
%%% Local Variables:
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    87
%%% mode: latex
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    88
%%% TeX-master: "root"
9f7b8de5bfaf updated;
wenzelm
parents: 8771
diff changeset
    89
%%% End: