There was a question regarding this exercise in this forum, but it dose not answer my specific question. This exercise asks to draw the environmental diagrams for
(define x (cons 1 2))
(define z (cons x x))
(set-car! (cdr z) 17)
(car x)
where cons
, set-car!
and car
are defined as
(define (cons x y)
(define (set-x! v) (set! x v))
(define (set-y! v) (set! y v))
(define (dispatch m)
(cond ((eq? m 'car) x)
((eq? m 'cdr) y)
((eq? m 'set-car!) set-x!)
((eq? m 'set-cdr!) set-y!)
(else (error "Undefined operation -- CONS" m))))
dispatch)
(define (car z) (z 'car))
(define (cdr z) (z 'cdr))
(define (set-car! z new-value)
((z 'set-car!) new-value)
z)
(define (set-cdr! z new-value)
((z 'set-cdr!) new-value)
z)
The first two are very straightforward. My question is regarding the third one (set-car! (cdr z) 17)
The Environmental Diagram I obtained is like this
Based on SICP textbook (section 3.2.1): To apply a procedure to arguments, create a new environment containing a frame that binds the parameters to the values of the arguments. The enclosing environment of this frame is the environment specified by the procedure.
Thus (define x (cons 1 2))
creates environment E1. (define z (cons x x))
creates E2.
The following parts I am not so sure, what I thought is: because procedure set-car! pointed to global environment, I think (set-car! (cdr z) 17)
should create E3 which is enclosed in global. With the same logic, (cdr z)
should create E4 under global, as cdr
is also defined under global and points to global.
Then, evaluating (cdr z)
invokes (z 'cdr)
. Because z points to E2, E5 is created under E2, with body of function dispatch and formal parameter m as 'cdr. This is evaluated to x which has its binding in global environment.
Thus the formal parameters of set-car! are z bonded to x whose binding can be found through E3 to global E and new-value bonded to 17 in E3 directly.
Then by evaluating (set-car! z new-value)
, (z 'set-car!)
evaluates first. As z is bonded to x pointed to E1, E6 is created with its formal parameter bonded to 'set-car!
and the function body is dispatch in E1. The return value is procedure set-x!
whose binding is found in E1. Evaluating of set-x! create E7 under E1 and new-value is assigned to its formal parameter v.
My question is how set-x! finds the value of new-value which assigned in a separate environment E3? It we trace the parents environments from E7 to E1 then global, it will never guide to E3 where the new-value bonded to 17.
Based on the sentence in SICP, E3 must be created under global when applying set-car!
. Some solutions online skip the creation of E3 and E4 under global and assign 17 directly in E7, which I think is not correct. Because it is written clearly in SICP that when applying a procedure a new environment is created under the environment specified by the procedure.
Please help me to understand this. Thank you.
Update
To be more clear, I translate the code to python and run it under PyTutor http://www.pythontutor.com/. What I do not understand is between step 34 and 35 as shown in the pictures below
As you can see from step 34, setcar(cdr(z), 17)
created a environment under global environment with the name newvalue
bound to 17. In the next step (35), the evaluation of setx
created a separate environment under the parent f1 (created by cons(1,2)
). These are all clear for me.
What I do not understand is how it is possible in this environment created by setx
, the binding of newvalue
which is in a separate environment (setcar
) can be found and assign to formal parameter of setx
, v
, as 17.
As I understand from SICP, the procedures will look in their own environments and their parents sequentially for name binding. But here, the environment that setcar
pointed to is independent from the environment setx
pointed to and its parent environment (f1). How the cross environments look-up is possible here?
Below is the python code can be tested in PyTutor with the link I gave above.
def cons(x, y):
def setx(v):
nonlocal x
x=v
def sety(v):
nonlocal y
y=v
def dispatch(m):
if m == 'car': return x
elif m == 'cdr': return y
elif m == 'setcar': return setx
elif m == 'setcdr': return sety
else: print("Undefined operation -- CONS", m)
return dispatch
def car(z):
return z('car')
def cdr(z):
return z('cdr')
def setcar(z, newvalue):
z('setcar')(newvalue)
return z
def setcdr(z, newvalue):
z('setcdr')(newvalue)
return z
x = cons(1,2)
z = cons(x,x)
setcar(cdr(z), 17)
car(x)
Updated 2
Thanks to Will Ness's brilliant answer, the problem is clarified and below is my update for the environment diagram
(((cons 1 2) 'set-car!) 17)
, it is progressively interpreted as ...". What is unclear there? – Will Ness