CS 334

Ex.: If define
 fun updatehd newhd (head::tail) = newhd :: tail;then get sharing:
Safe, because list elt's not updatable!
Ex. Obvious recursive def in ML:
 fun fib 0 : int = 1  fib 1 = 1  fib n = fib (n2) + fib (n1);Iterative solution in Pascal  faster!
Function fastfib (n:integer):integer; val a,b : integer; begin a := 1; b := 1; while n > 0 do begin a := b; b := a + b; n := n1 (* all done in parallel, else wrong! *) end; fib := a end;
ML equivalent
fun fastfib n = let fun fibLoop a b 0 = a  fibLoop a b n : int = fibLoop b (a+b) (n1) in fibLoop 1 1 n end;
Let's see how you can give a proof of correctness of a functional program:
fun fastfib n : int = let fun fibLoop a b 0 = a  fibLoop a b n : int = fibLoop b (a+b) (n1) in fibLoop 1 1 n end;Prove fastfib n = fib n where
fun fib 0 = 1  fib 1 = 1  fib n = fib (n2) + fib (n1);
Let a_{i} = fib i, for all i.
Therefore a_{0} = a_{1} = 1, and a_{i} + a_{i+1} = a_{i+2} for all i >= 0, by def of fib.
Theorem: For all i, fibLoop a_{i} a_{i+1} n = a_{i+n}.
Pf by induction on n:
If n = 0, fibLoop a_{i} a_{i+1} 0 = a_{i} = a_{i+0} by def.
Suppose true for n  1:
Then
fibLoop a_{i} a_{i+1} n = fibLoop a_{i+1} (a_{i} + a_{i+1}) (n  1) = fibLoop a_{i+1} a_{i+2} (n  1) = a_{i+1+(n1)} = a_{i+n}.Now
fastfib n = fibLoop 1 1 n = fibLoop a_{0} a_{1} n = a_{0+n} = a_{n}by the Theorem.
Therefore, for all n, fastfib n = fib n.
Similar proofs can be given for other facts, e.g.,
nlength (append l1 l2) = nlength(l1) + nlength(l2)where
fun nlength [] = 0  nlength (h::rest) = 1 + nlength restand
fun append [] l2 = l2  append (h::rest) l2 = h :: (append rest l2)