CS 6520 Homework 8

Exercise 8.1

Which of the following expressions are constant-space loops in the CEK machine?
• (Y f . x . f x) 0
• (Y f . x . if0 x (f 10) (f (- x 1))) 0
• (Y f . x . if0 x (f 10) (- (f x) 1)) 0
• mk-odd = f . x . if0 x false (not (f (- x 1)))

odd = Y mk-odd

(odd n) for any n
• mk-oe = f . z . x . if0 x z (f (not z) (- x 1))

odd = Y f . mk-oe f false

(odd n) for any n

Exercise 8.2

The extension of CEK with continuations that was presented in class (see the SECD handout) could be more efficient with simple changes. For example, the cc form of continuations needlessly keeps a sub-continiation that is never used.

Suggest revisions to the K grammar and the reduction rules to streamline the machine.

Exercise 8.3

Somewhat difficult. Collaboration is encouraged.

Given:
• next = k . x . (cc k (mk-pair k x))
• call = f . y . (f (next (fst y)) (snd y))
• Z = f . x . (call f (letcc k (mk-pair k x)))
What is the value of (Z mk-odd 1)?

Justify your answer, but don't try to write down a complete CEK reduction.

Exercise 8.4

Optional challenge (difficult).

Define callcc as
• callcc = f . (letcc k (f (v . (cc k v))))