5
$\begingroup$

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?

  • 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!

$\endgroup$
4
$\begingroup$

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 main(): fun(lambda().n+m), and in the body of the function fun you would replace x by the call x().

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Wait, in dynamic scope, why is m=100? Shouldn't it be 5? $\endgroup$ – Addem Dec 14 '19 at 17:55
  • $\begingroup$ @Addem You are quite correct. I confused the global n which is 100 and global m which is 5. So Call by name and dynamic scope should first print 25 (for (10+5)+10), then 220. $\endgroup$ – babou Dec 14 '19 at 18:13
-3
$\begingroup$

resault of static and dynamic scope:

step 1 : print 115

then n=200 , m=20 it is not affect on x so :

step 2 : print 105

| cite | improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ can you explain how you got these results? anyways the code two print statements per execution so there should be four results! $\endgroup$ – mellartach Oct 13 '13 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.