Search code examples
evalapplyprocedureprimitivesicp

How is a primitive procedure tagged in SICP's evaluator (chapter 4)?


I am reading chapter 4 of SICP. In the eval procedure, there is a procedure application. This procedure checks whether the expression is tagged with the symbol 'primitive or 'procedure.

I can see where the symbol 'procedure is added. (It is when evaluating a lambda expression.).

I am not able to find where the tag 'primitive is added? Clearly, when I supply a program to the evaluator, I supply (+ 1 2) and not ('primitive + 1 2). I would guess the 'primitive tag is added somewhere (like 'procedure), but I cannot find where.


Solution

  • Take a look at the primitive-procedure-objects procedure, that's where the 'primitive tag is added to the elements of the primitive-procedures list, which contains the primitive operations available to the interpreter.

    In turn, primitive-procedure-objects is called inside setup-environment, which is used for creating the initial environment for the interpreter.

    When evaluating an expression such as (+ 1 2), the evaluator simply goes all the way down in the case analysis of eval, matching the application? predicate, which invokes apply and then (eval (operator exp) env) on the first element of the expression. In turn, this matches variable? in the case analysis, which calls lookup-variable-value, that returns a procedure, which we tagged with 'primitive in setup-environment. Whew!