I'm studying dynamic/static scope with deep/shallow binding and running code manually to see how these different scopes/bindings actually work. I read the theory and googled some example exercises and the ones I found are very simple (like this one which was very helpful with dynamic scoping) But I'm having trouble understanding how static scope works.
Here I post an exercise I did to check if I got the right solution:
considering the following program written in pseudocode:
int u = 42;
int v = 69;
int w = 17;
proc add( z:int )
u := v + u + z
proc bar( fun:proc )
int u := w;
fun(v)
proc foo( x:int, w:int )
int v := x;
bar(add)
main
foo(u,13)
print(u)
end;
What is printed to screen
a) using static scope? answer=180
b) using dynamic scope and deep binding? answer=69 (sum for u = 126 but it's foo's local v, right?)
c) using dynamic scope and shallow binding? answer=69 (sum for u = 101 but it's foo's local v, right?)
PS: I'm trying to practice doing some exercises like this if you know where I can find these types of problems (preferable with solutions) please give the link, thanks!
Your answer for lexical (static) scope is correct. Your answers for dynamic scope are wrong, but if I'm reading your explanations right, it's because you got confused between
u
andv
, rather than because of any real misunderstanding about how deep and shallow binding work. (I'm assuming that youru
/v
confusion was just accidental, and not due to a strange confusion about values vs. references in the call tofoo
.)Correct.
Your parenthetical explanation is right, but your answer is wrong:
u
is indeed set to126
, andfoo
indeed localizesv
, but sincemain
printsu
, notv
, the answer is126
.The sum for
u
is actually97
(42+13+42
), but sincebar
localizesu
, the answer is42
. (Your parenthetical explanation is wrong for this one — you seem to have used the global variablew
, which is17
, in interpreting the statementint u := w
in the definition ofbar
; but that statement actually refers tofoo
's local variablew
, its second parameter, which is13
. But that doesn't actually affect the answer. Your answer is wrong for this one only becausemain
printsu
, notv
.)For lexical scope, it's pretty easy to check your answers by translating the pseudo-code into a language with lexical scope. Likewise dynamic scope with shallow binding. (In fact, if you use Perl, you can test both ways almost at once, since it supports both; just use
my
for lexical scope, then do a find-and-replace to change it tolocal
for dynamic scope. But even if you use, say, JavaScript for lexical scope and Bash for dynamic scope, it should be quick to test both.)Dynamic scope with deep binding is much trickier, since few widely-deployed languages support it. If you use Perl, you can implement it manually by using a hash (an associative array) that maps from variable-names to scalar-refs, and passing this hash from function to function. Everywhere that the pseudocode declares a local variable, you save the existing scalar-reference in a Perl lexical variable, then put the new mapping in the hash; and at the end of the function, you restore the original scalar-reference. To support the binding, you create a wrapper function that creates a copy of the hash, and passes that to its wrapped function. Here is a dynamically-scoped, deeply-binding implementation of your program in Perl, using that approach:
and indeed it prints
126
. It's obviously messy and error-prone, but it also really helps you understand what's going on, so for educational purposes I think it's worth it!Static binding, also known as lexical scope, refers to the scoping mechanism found in most modern languages.
In "lexical scope", the final value for u is neither 180 or 119, which are wrong answers.
The correct answer is u=101.
Please see standard Perl code below to understand why.
Regarding shallow binding versus deep binding, both mechanisms date from the former LISP era.
Both mechanisms are meant to achieve dynamic binding (versus lexical scope binding) and therefore they produce identical results !
The differences between shallow binding and deep binding do not reside in semantics, which are identical, but in the implementation of dynamic binding.
With deep binding, variable bindings are set within a stack as "varname => varvalue" pairs.
Please see the the code below for a Perl implementation of deep-binding dynamic scope.
The result is u=97
Nevertheless, this constant traversal of the binding stack is costly : 0(n) complexity !
Shallow binding brings a wonderful O(1) enhanced performance over the previous implementation !
Shallow binding is improving the former mechanism by assigning each variable its own "cell", storing the value of the variable within the cell.
Please see the the code below for a trivial Perl implementation of shallow-binding dynamic scope.
As you shall see, the result is still u=97
As a conclusion, remember two things :
shallow binding produces the same results as deep binding, but runs faster, since there is never a need to search for a binding.
The problem is not shallow binding versus deep binding versus
static binding BUT lexical scope versus dynamic scope (implemented either with deep or shallow binding).
Simple and deep binding are Lisp interpreter viewpoints of the pseudocode. Scoping is just pointer arithmetic. Dynamic scope and static scope are the same if there are no free variables.
Static scope relies on a pointer to memory. Empty environments hold no symbol to value associations; denoted by word "End." Each time the interpreter reads an assignment, it makes space for association between a symbol and value.
The environment pointer is updated to point to the last association constructed.
Let me record this environment memory location as AAA. In my Lisp interpreter, when meeting a procedure, we take the environment pointer and put it our pocket.
That's pretty much all there is until the procedure
add
is called. Interestingly, ifadd
is never called, you just cost yourself a pointer.Suppose the program calls
add(8)
. OK, let's roll. The environment AAA is made current. Environment is->[w,17]->[v,69]->[u,42]->End
.Procedure parameters of
add
are added to the front of the environment. The environment becomes[z,8]->[w,17]->[v,69]->[u,42]->End
.Now the procedure body of
add
is executed. Free variablev
will have value 69. Free variableu
will have value 42.z
will have the value 8.u
will be assigned the value of 69 + 42 + 8 becomeing 119.The environment will reflect this:
[z,8]->[w,17]->[v,69]->[u,119]->End
.Assume procedure
add
has completed its task. Now the environment gets restored to its previous value.Notice how the procedure
add
has had a side effect of changing the value of free variableu
. Awesome!Regarding dynamic scoping: it just ensures closure leaves out dynamic symbols, thereby avoiding being captured and becoming dynamic.
Then put assignment to dynamic at top of code. If dynamic is same as parameter name, it gets masked by parameter value passed in.
Suppose I had a dynamic variable called
z
. When I calledadd(8)
,z
would have been set to 8 regardless of what I wanted. That's probably why dynamic variables have longer names.Rumour has it that dynamic variables are useful for things like backtracking, using let Lisp constructs.