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
fun you would replace
x by the call