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 did not depend on these local variables (because
statically bound to the global ones), and it is already evaluated to 105
anyway, which is printed by the second print.
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=100,
i.e. 110, and added 10 to print 120. Local n=200 and global m=20. The
parameter x is reevaluated as 200+20, i.e. 220, which gets printed.
result: 120 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
fun you would replace
x by the call