patternMinor
Static scope and dynamic scope
Viewed 0 times
andscopestaticdynamic
Problem
I am perpetually confused when it comes to static scoping and dynamic scoping and their differences and Wikipedia isn't of any help, so here's my question:
Given the following pseudo code in a hypothetical programming environment, what are the outputs under the following conditions?
Additionally, what would be two environments where I can run this code to check the output with the conditions? C uses lexical scope, but it does not support call by need while Bash supports dynamic scope, I guess!
Given the following pseudo code in a hypothetical programming environment, what are the outputs under the following conditions?
- call by need using static scope
- call by name using dynamic scope
global int n = 100, m = 5;
void fun(x){
int n = 10;
print(x + 10);
n = 200;
m = 20;
print(x);
}
main(){
fun(n + m);
}Additionally, what would be two environments where I can run this code to check the output with the conditions? C uses lexical scope, but it does not support call by need while Bash supports dynamic scope, I guess!
Solution
Here is the simulation by hand.
-
call by need using static scope
The function receives as x parameter the thunk [n+m] bound to the
globals. Then local n gets 10. The print statement requires evaluation
of the thunk, which becomes 100+5, i.e. 105. It prints 105+10,
i.e. 115. Local n and global m are changed, but this cannot affect x for two
reasons: the thunk does not depend on the local variable n (because
statically bound to the global variables n and m), and it is already evaluated to 105
anyway, which is printed by the second print.
Note that, if the first print statement were removed, then x would be evaluated after changing the global variable m, which would then be 20 instead of 5, and the answer for the remaining print statement (formerly the second one) would be 120.
result: 115 105
-
call by name using dynamic scope
The function receives as x parameter the unbound expression [n+m]. Then
local n gets 10. Then x gets evaluated, with local n=10 and global m=5,
i.e. 15, and added 10 to print 25. Then local n=200 and global m=20. The
parameter x is reevaluated as 200+20, i.e. 220, which gets printed.
result: 25 220
How to test it? One would have to check various implementations of
programming languages. I do not remember off hand.
Call by need is usually found in functional languages, which normally
do not have assignment. But some still allow it. So I would look for
that regarding the first case. I think CAML may provide it.
Call by name is fairly rare, and rather weird in a dynamic scope
context. I do not know of any language that will offer that
combination. The best is to use a dynamic binding langage and simulate
the example by explicitly replacing the call by name argument by a
parameterless function that has the parameter as body.
In the example, you would call in
in the body of the function
-
call by need using static scope
The function receives as x parameter the thunk [n+m] bound to the
globals. Then local n gets 10. The print statement requires evaluation
of the thunk, which becomes 100+5, i.e. 105. It prints 105+10,
i.e. 115. Local n and global m are changed, but this cannot affect x for two
reasons: the thunk does not depend on the local variable n (because
statically bound to the global variables n and m), and it is already evaluated to 105
anyway, which is printed by the second print.
Note that, if the first print statement were removed, then x would be evaluated after changing the global variable m, which would then be 20 instead of 5, and the answer for the remaining print statement (formerly the second one) would be 120.
result: 115 105
-
call by name using dynamic scope
The function receives as x parameter the unbound expression [n+m]. Then
local n gets 10. Then x gets evaluated, with local n=10 and global m=5,
i.e. 15, and added 10 to print 25. Then local n=200 and global m=20. The
parameter x is reevaluated as 200+20, i.e. 220, which gets printed.
result: 25 220
How to test it? One would have to check various implementations of
programming languages. I do not remember off hand.
Call by need is usually found in functional languages, which normally
do not have assignment. But some still allow it. So I would look for
that regarding the first case. I think CAML may provide it.
Call by name is fairly rare, and rather weird in a dynamic scope
context. I do not know of any language that will offer that
combination. The best is to use a dynamic binding langage and simulate
the example by explicitly replacing the call by name argument by a
parameterless function that has the parameter as body.
In the example, you would call in
main(): fun(lambda().n+m), andin the body of the function
fun you would replace x by the call x().Context
StackExchange Computer Science Q#14625, answer score: 4
Revisions (0)
No revisions yet.