SICP Exercise 4.14 map as compound vs. primitive type

Exercise 4.14.  Eva Lu Ator and Louis Reasoner are each experimenting with the metacircular evaluator. Eva types in the definition of map, and runs some test programs that use it. They work fine. Louis, in contrast, has installed the system version of map as a primitive for the metacircular evaluator. When he tries it, things go terribly wrong. Explain why Louis's map fails even though Eva's works.

EXPLANATION & SOLUTION

Part a of this problem: I implement Eva's way of explicitly typing in the definition of 'map' into the metacircular evaluator's input prompt. I also type in the definition of an 'inc' procedure to increment its (numerical) input. And also make use of Racket's primitive procedure 'abs'. 'inc' and 'abs' are the procedure arguments to 'map' in different tests. As can be seen from the test results, everything works as expected. Note that in this program, 'map' and 'inc' are executed by the metacircular evaluator. 'abs' is executed as a primitive procedure. The code and tests are here.

Part b of this problem: Here I implement Louis' way of installing the system version of 'map' as a primitive for the metacircular evaluator. See the results at the bottom of the program. The evaluator does not work as expected.

Note that one of the arguments to 'map' is a procedure. The metacircular evaluator constructs all the arguments to be passed in to any procedures (compound or primitive) that it executes. So the proc argument that it passes to the primitive 'map' is represented syntactically and semantically in the form that the metacircular evaluator understands. The underlying Racket interpreter does not know anything about the meta-circular evaluator's representations. The Racket version of 'map' expects a Racket procedure as its argument which it does not find when invoked by the metacircular evaluator. Hence it fails with 'contract violation' error messages. The code and tests are here.


Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions