How is a primitive procedure tagged in SICP's evaluator (chapter 4)?
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.
eval
application
'primitive
'procedure
I can see where the symbol 'procedure is added. (It is when evaluating a lambda expression.).
'procedure
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.
'primitive
(+ 1 2)
('primitive + 1 2)
'primitive
'procedure
1 Answer
1
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.
primitive-procedure-objects
'primitive
primitive-procedures
In turn, primitive-procedure-objects is called inside setup-environment, which is used for creating the initial environment for the interpreter.
primitive-procedure-objects
setup-environment
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!
(+ 1 2)
eval
application?
apply
(eval (operator exp) env)
variable?
lookup-variable-value
'primitive
setup-environment
eval
(+ 1 2)
primitive-procedure?
'primitive
@Gradient there you go :)
– Óscar López
Jul 2 at 19:55
That is what I was looking for! Thank you!
– Gradient
Jul 2 at 19:56
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Yes, I have seen that. But, how is it "reached" from
eval. I mean, if I supply(+ 1 2)to the evaluator, how does it end up being seen as aprimitive-procedure?if there is no'primitivetag in what I supplied to the evaluator?– Gradient
Jul 2 at 19:42