5
$\begingroup$

I am designing a small DSL and I know that implementing dynamic scoping (with a simple global stack) is easier then using full lexical scoping (each function needs its own scope/closure). What kind of features can be added that do not care how scope is implemented and what features would require a particular kind of scope implementation?

For example, if I have a very simple language that does not allow nested functions or function arguments I feel that both implementation choices are likely equivalent. On the other end of the spectrum, if I wanted to support returning inner functions then I'm pretty sure we would need to allow functions to save their own closures. However, I am not really sure what would happen in the middle if say, I alowed nested functions while not allowing them to be returned or if I allowed functions to be passed as arguments to other functions.

$\endgroup$
7
$\begingroup$

If you allow one function to call another, then you have enough power to distinguish a lexically defined variable from a dynamically defined one.

def scope {
  var x = 10
  def f() {
    return g()
  }
}
def g() {
  return x;
}
scope.f()

If x is dynamically scoped, the code returns 10. If it is lexically defined, the result is an error.

Thus if you want a language where dynamic and lexical scoping behave the same, it will be a rather weak language.

$\endgroup$
  • $\begingroup$ Makes sense. Do you know, by any chance, if there is a book or something that talks about this kind of stuff? $\endgroup$ – hugomg Sep 23 '12 at 16:33
  • $\begingroup$ Robert Harper's book-to-be does, though I'm not sure to what depth. I learnt mostly from programming and research papers. $\endgroup$ – Dave Clarke Sep 23 '12 at 18:41

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.