For two consecutive years I have been responsible for teaching the program derivation course in the FLOLAC summer school. It was an exam question last year to derive a linear-time solution computing the Fibonacci function, whose definition we all know very well:
I would like the students to see the derivation as an instance of tupling: to generalise the targeted function by having it return more results, hoping that the generalisation can actually be performed faster. For this problem, one may consider the generalisation:
from which we may easily construct a recursive definition for
Tupling and Fold Fusion
As a practice, however, I instructed the students to see this as an instance of fold fusion with the fold on natural numbers:
The derivation goes like
To derive
Therefore, we have
Oleg Kiselyov also used a similar definition of
Accumulating Parameters
I was perhaps so happy with seeing
When I taught the students about accumulating parameters, I also told them to “look for some generalisation by adding more parameters.” The canonical example is perhaps generalising quadratic time
From this definition, however, a linear-time implementation of
which can be proved by induction on
The property (1) came from some slides Kung Chen showed me, originated from John Hughes. John Hughes actually suggested to start with (1) as the specification. Either way, I wonder how one could come up with (1) in the first place. Given the definition of
Tail-Recursive Fibonacci
Meanwhile, Josh Ko gave me this generalisation:
The motivation came from, perhaps, the observation that every
Given the definition, it is immediate that
Therefore we have derived:
which is more or less what one would expect. It differs from the derived linear-time implementation of
Finally, since
The proof proceeds by induction on
thus completes the proof.
2 Comments
I think I’ve spotted two typos: In the definition of the fusioned fib2 there’s a fold instead of a foldN. Later there’s gfib instead of a gib.
But nice read, indeed, for I’ve been thinkering around with such small multi-recursive problems lately.
Thanks! I will correct them now.