Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lexical vs dynamic scoping in terms of SICP's Environment Model of Evaluation

In Section 3.2.2 of SICP the execution of the following piece of code

(define (square x)
  (* x x))
(define (sum-of-squares x y)
  (+ (square x) (square y)))
(define (f a)
  (sum-of-squares (+ a 1) (* a 2)))

(f 5)

is explained in terms of this diagram.

Each time a function is applied, a new frame is created (labeled by E1 through E4) which represents a set of bindings between symbols and values. When a symbol is not bound in a frame, that frame's enclosing environment is queried for a binding of that particular symbol.

The interesting thing about this diagram is that all the frames labelled by E is contained in the global environment. The text explains that this is because the functions was defined in the global environment, but does not elaborate on the issue:

Notice that each frame created by square points to the global environment, since this is the environment indicated by the square procedure object.

If instead frames where contained in the environment that the function was called in, say E3 was contained in E2 which in turn was contained in E1, would that be a valid model of how a dynamically scoped language works? Also, is the way that the frames in the diagram have the same 'parent' environment because Scheme is lexically scoped?

like image 481
ddk Avatar asked Oct 06 '12 21:10

ddk


People also ask

What is lexical and dynamic scoping?

Lexical scoping refers to when the location of a function's definition determines which variables you have access to. On the other hand, dynamic scoping uses the location of the function's invocation to determine which variables are available.

What do you mean by lexical scoping explain with the help of some example?

Lexical scoping, also known as static scoping, is a convention used with many modern programming languages. It refers to setting the scope, or range of functionality, of a variable so that it may be called (referenced) from within the block of code in which it is defined.

What best defines lexical scope implementation?

Lexical Scope simply means that the region in which a variable exists is determined by where it was defined or created. Lexical Scope means that the meaning/value of a variable can only be determined by the region/environment where it was created.

What is the problem with dynamic scoping?

Another problem with dynamic scoping is inability to statically check references for nonlocal. Also, dynamic scoping makes programs much more difficult to read, because the calling sequence of subprograms must be known to determine the meaning of references to non-local variables.


1 Answers

The answer to both questions is yes. That chapter of SICP is explaining lexical scope without actually using the term. Changing the evaluation mechanism as you describe would create a dynamically-scoped model.

like image 124
itsbruce Avatar answered Oct 07 '22 02:10

itsbruce