equal
deleted
inserted
replaced
1 (*<*) |
1 (*<*) |
2 theory examples = Main:; |
2 theory examples = Main:; |
3 (*>*) |
3 (*>*) |
4 |
4 |
5 text{* |
5 text{* |
6 Here is a simple example, the Fibonacci function: |
6 Here is a simple example, the \rmindex{Fibonacci function}: |
7 *} |
7 *} |
8 |
8 |
9 consts fib :: "nat \<Rightarrow> nat"; |
9 consts fib :: "nat \<Rightarrow> nat"; |
10 recdef fib "measure(\<lambda>n. n)" |
10 recdef fib "measure(\<lambda>n. n)" |
11 "fib 0 = 0" |
11 "fib 0 = 0" |
12 "fib 1 = 1" |
12 "fib 1 = 1" |
13 "fib (Suc(Suc x)) = fib x + fib (Suc x)"; |
13 "fib (Suc(Suc x)) = fib x + fib (Suc x)"; |
14 |
14 |
15 text{*\noindent |
15 text{*\noindent |
16 The definition of @{term"fib"} is accompanied by a \bfindex{measure function} |
16 \index{measure functions}% |
|
17 The definition of @{term"fib"} is accompanied by a \textbf{measure function} |
17 @{term"%n. n"} which maps the argument of @{term"fib"} to a |
18 @{term"%n. n"} which maps the argument of @{term"fib"} to a |
18 natural number. The requirement is that in each equation the measure of the |
19 natural number. The requirement is that in each equation the measure of the |
19 argument on the left-hand side is strictly greater than the measure of the |
20 argument on the left-hand side is strictly greater than the measure of the |
20 argument of each recursive call. In the case of @{term"fib"} this is |
21 argument of each recursive call. In the case of @{term"fib"} this is |
21 obviously true because the measure function is the identity and |
22 obviously true because the measure function is the identity and |
36 This time the measure is the length of the list, which decreases with the |
37 This time the measure is the length of the list, which decreases with the |
37 recursive call; the first component of the argument tuple is irrelevant. |
38 recursive call; the first component of the argument tuple is irrelevant. |
38 The details of tupled $\lambda$-abstractions @{text"\<lambda>(x\<^sub>1,\<dots>,x\<^sub>n)"} are |
39 The details of tupled $\lambda$-abstractions @{text"\<lambda>(x\<^sub>1,\<dots>,x\<^sub>n)"} are |
39 explained in \S\ref{sec:products}, but for now your intuition is all you need. |
40 explained in \S\ref{sec:products}, but for now your intuition is all you need. |
40 |
41 |
41 Pattern matching need not be exhaustive: |
42 Pattern matching\index{pattern matching!and \isacommand{recdef}} |
|
43 need not be exhaustive: |
42 *} |
44 *} |
43 |
45 |
44 consts last :: "'a list \<Rightarrow> 'a"; |
46 consts last :: "'a list \<Rightarrow> 'a"; |
45 recdef last "measure (\<lambda>xs. length xs)" |
47 recdef last "measure (\<lambda>xs. length xs)" |
46 "last [x] = x" |
48 "last [x] = x" |