8749
|
1 |
\begin{isabelle}%
|
|
2 |
%
|
|
3 |
\begin{isamarkuptext}%
|
|
4 |
Here is a simple example, the Fibonacci function:%
|
|
5 |
\end{isamarkuptext}%
|
9541
|
6 |
\isacommand{consts}\ fib\ ::\ {"}nat\ {\isasymRightarrow}\ nat{"}\isanewline
|
|
7 |
\isacommand{recdef}\ fib\ {"}measure({\isasymlambda}n.\ n){"}\isanewline
|
|
8 |
\ \ {"}fib\ 0\ =\ 0{"}\isanewline
|
|
9 |
\ \ {"}fib\ 1\ =\ 1{"}\isanewline
|
|
10 |
\ \ {"}fib\ (Suc(Suc\ x))\ =\ fib\ x\ +\ fib\ (Suc\ x){"}%
|
8749
|
11 |
\begin{isamarkuptext}%
|
|
12 |
\noindent
|
|
13 |
The definition of \isa{fib} is accompanied by a \bfindex{measure function}
|
9644
|
14 |
\isa{{\isasymlambda}\mbox{n}.\ \mbox{n}} which maps the argument of \isa{fib} to a
|
8749
|
15 |
natural number. The requirement is that in each equation the measure of the
|
|
16 |
argument on the left-hand side is strictly greater than the measure of the
|
|
17 |
argument of each recursive call. In the case of \isa{fib} this is
|
|
18 |
obviously true because the measure function is the identity and
|
9644
|
19 |
\isa{Suc\ (Suc\ \mbox{x})} is strictly greater than both \isa{x} and
|
|
20 |
\isa{Suc\ \mbox{x}}.
|
8749
|
21 |
|
|
22 |
Slightly more interesting is the insertion of a fixed element
|
|
23 |
between any two elements of a list:%
|
|
24 |
\end{isamarkuptext}%
|
9541
|
25 |
\isacommand{consts}\ sep\ ::\ {"}'a\ *\ 'a\ list\ {\isasymRightarrow}\ 'a\ list{"}\isanewline
|
|
26 |
\isacommand{recdef}\ sep\ {"}measure\ ({\isasymlambda}(a,xs).\ length\ xs){"}\isanewline
|
|
27 |
\ \ {"}sep(a,\ [])\ \ \ \ \ =\ []{"}\isanewline
|
|
28 |
\ \ {"}sep(a,\ [x])\ \ \ \ =\ [x]{"}\isanewline
|
|
29 |
\ \ {"}sep(a,\ x\#y\#zs)\ =\ x\ \#\ a\ \#\ sep(a,y\#zs){"}%
|
8749
|
30 |
\begin{isamarkuptext}%
|
|
31 |
\noindent
|
|
32 |
This time the measure is the length of the list, which decreases with the
|
|
33 |
recursive call; the first component of the argument tuple is irrelevant.
|
|
34 |
|
|
35 |
Pattern matching need not be exhaustive:%
|
|
36 |
\end{isamarkuptext}%
|
9541
|
37 |
\isacommand{consts}\ last\ ::\ {"}'a\ list\ {\isasymRightarrow}\ 'a{"}\isanewline
|
|
38 |
\isacommand{recdef}\ last\ {"}measure\ ({\isasymlambda}xs.\ length\ xs){"}\isanewline
|
|
39 |
\ \ {"}last\ [x]\ \ \ \ \ \ =\ x{"}\isanewline
|
|
40 |
\ \ {"}last\ (x\#y\#zs)\ =\ last\ (y\#zs){"}%
|
8749
|
41 |
\begin{isamarkuptext}%
|
|
42 |
Overlapping patterns are disambiguated by taking the order of equations into
|
|
43 |
account, just as in functional programming:%
|
|
44 |
\end{isamarkuptext}%
|
9541
|
45 |
\isacommand{consts}\ sep1\ ::\ {"}'a\ *\ 'a\ list\ {\isasymRightarrow}\ 'a\ list{"}\isanewline
|
|
46 |
\isacommand{recdef}\ sep1\ {"}measure\ ({\isasymlambda}(a,xs).\ length\ xs){"}\isanewline
|
|
47 |
\ \ {"}sep1(a,\ x\#y\#zs)\ =\ x\ \#\ a\ \#\ sep1(a,y\#zs){"}\isanewline
|
|
48 |
\ \ {"}sep1(a,\ xs)\ \ \ \ \ =\ xs{"}%
|
8749
|
49 |
\begin{isamarkuptext}%
|
|
50 |
\noindent
|
|
51 |
This defines exactly the same function as \isa{sep} above, i.e.\
|
|
52 |
\isa{sep1 = sep}.
|
|
53 |
|
|
54 |
\begin{warn}
|
|
55 |
\isacommand{recdef} only takes the first argument of a (curried)
|
|
56 |
recursive function into account. This means both the termination measure
|
|
57 |
and pattern matching can only use that first argument. In general, you will
|
|
58 |
therefore have to combine several arguments into a tuple. In case only one
|
|
59 |
argument is relevant for termination, you can also rearrange the order of
|
|
60 |
arguments as in the following definition:
|
|
61 |
\end{warn}%
|
|
62 |
\end{isamarkuptext}%
|
9541
|
63 |
\isacommand{consts}\ sep2\ ::\ {"}'a\ list\ {\isasymRightarrow}\ 'a\ {\isasymRightarrow}\ 'a\ list{"}\isanewline
|
|
64 |
\isacommand{recdef}\ sep2\ {"}measure\ length{"}\isanewline
|
|
65 |
\ \ {"}sep2\ (x\#y\#zs)\ =\ ({\isasymlambda}a.\ x\ \#\ a\ \#\ sep2\ zs\ a){"}\isanewline
|
|
66 |
\ \ {"}sep2\ xs\ \ \ \ \ \ \ =\ ({\isasymlambda}a.\ xs){"}%
|
8749
|
67 |
\begin{isamarkuptext}%
|
|
68 |
Because of its pattern-matching syntax, \isacommand{recdef} is also useful
|
|
69 |
for the definition of non-recursive functions:%
|
|
70 |
\end{isamarkuptext}%
|
9541
|
71 |
\isacommand{consts}\ swap12\ ::\ {"}'a\ list\ {\isasymRightarrow}\ 'a\ list{"}\isanewline
|
|
72 |
\isacommand{recdef}\ swap12\ {"}{\isabraceleft}{\isabraceright}{"}\isanewline
|
|
73 |
\ \ {"}swap12\ (x\#y\#zs)\ =\ y\#x\#zs{"}\isanewline
|
|
74 |
\ \ {"}swap12\ zs\ \ \ \ \ \ \ =\ zs{"}%
|
8749
|
75 |
\begin{isamarkuptext}%
|
|
76 |
\noindent
|
8771
|
77 |
For non-recursive functions the termination measure degenerates to the empty
|
8749
|
78 |
set \isa{\{\}}.%
|
|
79 |
\end{isamarkuptext}%
|
|
80 |
\end{isabelle}%
|
9145
|
81 |
%%% Local Variables:
|
|
82 |
%%% mode: latex
|
|
83 |
%%% TeX-master: "root"
|
|
84 |
%%% End:
|