Accumulators, Examples of proving w/accumulators
Plans
Rest of Sem. Sched.
Outline next project, due dates
Project presentations
Next class? Next, next class!
Discussed earlier plans. Those plans upon us. I can’t imagine enjoying a final exam more.
So meeting w/folk re: these.
Accumulators + Reasoning w/Accumulators
Recap what we saw w/accumulators on Last Mon.
Defining functions w/accs
- Start with a function
f. - Define
ft, a tail-recursive version offwith an accumulator. - Define
f*, a non-recursive function that callsftand is logically equivalent tof, i.e., the following is a theoremhyps ⇒ (f* ...) = (f ...)
Proofs w/, wrt/ accumulator fns.
-
Identify a lemma that relates
fttof. It should have the following form:hyps ⇒ (ft ... acc) = ... (f ...) ...Remember that you have to generalize, so all arguments toftshould be variables (no constants). The RHS should includeacc. -
Assuming that the lemma in 4 is true, and using only equational reasoning, prove the main theorem
hyps ⇒ (f* ...) = (f ... )If you have to prove lemmas, prove them later. (Like an adult!)
-
Prove the lemma in 4. Use the induction scheme of
ft. -
Prove any remaining lemmas. (Like an adult!)
Example
(definec powof3 (b)
(if (zp b) 1
(* 3 (powof3 (1- b)))))
(definec powof32h (b)
(powof32h b 1))
(definec powof32h (b c)
(if (zp b) c
(powof32h (1- b) (* 3 c))))
The lemma of 4
(pow32 b c) = (* (pow3 b) c)