General Computer Science 320201 GenCS I & II Lecture ... - Kwarc
General Computer Science 320201 GenCS I & II Lecture ... - Kwarc
General Computer Science 320201 GenCS I & II Lecture ... - Kwarc
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Termination<br />
Definition 172 Let R ⊆ A 2 be a binary relation, an infinite chain in R is a sequence<br />
a1, a2, . . . in A, such that ∀n ∈ N1.〈an, an+1〉 ∈ R.<br />
We say that R terminates (on a ∈ A), iff there is no infinite chain in R (that begins with a).<br />
We say that P diverges (on a ∈ A), iff it does not terminate on a.<br />
Theorem 173 Let P = 〈f::A → R ; R〉 be an abstract procedure and a ∈ TA(A, Σ; V),<br />
then P terminates on a, iff the recursion relation of P does.<br />
Definition 174 Let P = 〈f::A → R ; R〉 be an abstract procedure, then we call the function<br />
{〈a, b〉 | a ∈ TA(A, Σ; V) and P terminates for a with b} in A ⇀ B the result function of P.<br />
Theorem 175 Let P = 〈f::A → B ; D〉 be a terminating abstract procedure, then its result<br />
function satisfies the equations in D.<br />
c○: Michael Kohlhase 104<br />
We should read Theorem 175 as the final clue that abstract procedures really do encode functions<br />
(under reasonable conditions like termination). This legitimizes the whole theory we have<br />
developed in this section.<br />
Abstract vs. Concrete Procedures vs. Functions<br />
An abstract procedure P can be realized as concrete procedure P ′ in a programming language<br />
Correctness assumptions (this is the best we can hope for)<br />
If the P ′ terminates on a, then the P terminates and yields the same result on a.<br />
If the P diverges, then the P ′ diverges or is aborted (e.g. memory exhaustion or buffer<br />
overflow)<br />
Procedures are not mathematical functions (differing identity conditions)<br />
compare σ : N1 → N1 with σ(o) o, σ(s(n)) n + σ(n)<br />
with σ ′ : N1 → N1 with σ ′ (o) 0, σ ′ (s(n)) ns(n)/2<br />
these have the same result function, but σ is recursive while σ ′ is not!<br />
Two functions are equal, iff they are equal as sets, iff they give the same results on all<br />
arguments<br />
c○: Michael Kohlhase 105<br />
2.3.5 More SML: Recursion in the Real World<br />
We will now look at some concrete SML functions in more detail. The problem we will consider is<br />
that of computing the n th Fibonacci number. In the famous Fibonacci sequence, the n th element<br />
is obtained by adding the two immediately preceding ones.<br />
This makes the function extremely simple and straightforward to write down in SML. If we look<br />
at the recursion relation of this procedure, then we see that it can be visualized a tree, as each<br />
natural number has two successors (as the the function fib has two recursive calls in the step<br />
case).<br />
Consider the Fibonacci numbers<br />
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . . .<br />
57