1
$\begingroup$

Given the following (arbitrary language, although I think it is close to Algol 60) program:

program main;                               // A main parent level
  var i : integer;                          // A 'global' variable

  (* Note that all parameters are passed by value here *)

  function f1 (j : integer) : integer;      // A Child function
  begin { f1 }
    i := i + 3;
    f1 := 2 * j - i;
  end; { f1 }

  function f2 (k : integer) : integer;      // Another Child function, same level as f1
    var i : integer;                        // Here, there is a variable that is declared
  begin { f2 }                                 // but no value assigned
    i := k / 2;
    f2 := f1(i) + f1(k);
  end; { f2 }

begin { main }                              // Running/Calling/Executing the code
  i := 8;
  i := i + f2(i);
  writeln(i);
end. { main }

How would you trace the values of variables throughout the program when it is interpreted using Dynamic scoping of free variables, when the arguments appearing in expressions are evaluated left to right, and when they are evaluated right to left, so that the user can watch what happens

I have created a JS plnkr for Static Scoping with Left to Right evaluation and another for Static Scoping with Right to Left evaluation. Feel free to adapt these answers (if possible) for Dynamic Scoping, with L->R and R->L evaluation.

I chose plnkrs because I knew I could get the Static/lexical side using JS, but am unsure of how to make it happen dynamically or in another interactive environment (preferably not one I have to install).

I learn a bit slower on some problems like this where the values are asked of the output, but don't show the value states throughout the program, and trying to get a better understanding, especially in an example I can play around with interactively, as the book examples are really bad. In the code above, it also gets challenging, because it appears that the variable i in line 2 is allocated, but would be undefined. But that may be my imperative/functional brain making it more complicated than it is...

$\endgroup$
  • $\begingroup$ I tried to edit and clarify your question. Is the new form in agreement with what you wanted to ask? I can then try to answer it. But in short, it is hard to follow all variables in dynamic scoping (but one can simulate by hand and give the answers), and the answers are somewhat undefined anyway, when the mode of communication between formal and actual parameters of functions is not clearly defined. Note that while scoping may have an effect on evaluation, evaluation has none on scoping. $\endgroup$ – babou Oct 25 '14 at 16:38
  • $\begingroup$ @babou Yes, this is good. Thank you! $\endgroup$ – chris Frisina Oct 25 '14 at 21:45
1
$\begingroup$

I do not know other environments in existing languages, whether statically or dynamically scoped. Also, that would be language specific, hence off topic here.

Without a system environment, the way I would do this would be to insert print statement in my program, either by hand if I am poor, or with a some programmable program transformation system. I actually did it often a very long time ago for debugging purposes, applying program transformations I had developed in such a program transformation system.

Your example is rather simple as the formal parameters j and k are never modified, so that everything can be assumed to be call by value. But things can get very hairy when formal parameters get modified in the function, depending on the parameter passing mode.

I am also a bit surprised that your system gives you the post value of i in f2 at the beginning rather than at the end of the trace.

You may try to trace the values of variable that are not visible in the function currently executed, for example by printing them with a function defined at the right place. But that can only work when the function uses static scoping.

My basic philosophy is that dynamic scoping is an endless source of problems because you cannot know what a free variable stands for, as it may vary from call to call. I know that some people like it and say it is useful. I will not comment.

Dynamic scoping was invented as a bug in the first Lisp interpreter, in the late fifties. When they realized what they had done, the beast was out. Scheme corrected that, but much later.

I am not sure I answered you. But I now realize this question is borderline on this site.

$\endgroup$

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.