I am reading EOPL 3rd edition. Now I am stuck on exercise 6.31.
Exercise 6.31 Write a translator that takes the output of
cps-of-program
and produces an equivalent program in which all the continuations are represented by data structures, as in chapter 5. Represent data structures like those constructed usingdefine-datatype
as lists. Since our language does not have symbols, you can use an integer tag in the car position to distinguish the variants of a data type.
I can't solve this one because I have no idea what the result program should look like. For example, consider the program below:
+(1, (f x), (g y))
After CPS we got the following program:
(f x
(proc (v1)
(g y
(proc (v2)
(proc (v0) v0)
+(1, v1, v2)))))
Here, the procedure in the position K
as in (f x K)
is the continuation. The
question is I have a very vague view on how the data structure version of K
looks like. One possibility is:
(f x
(call-cont g
(y)
(sum-cont 1
?
(end-cont))))
However, because simple expressions are regarded as a whole, I don't know how to
convert +(1, v1, v2)
to something like sum-cont1
, sum-cont2
as in
chapter 5. As a result, I can only use a question mark ?
in the data structure
representation. Without knowing how the equivalent program looks like, it is
impossible to solve this exercise for me. Could anyone please give some hints?
Thanks!