scopeglobal-variablesschemeevaluationmit-scheme

What is the relation of parent env and the child env in MIT-Scheme?


When we call (environment-lookup system-global-environment name) where system-global-environment is the parent of top-level-environment, will it also look up in its child env?

From the doc, it seems that they are separate. If so, why we define parent env here?

The operations in this section reveal the frame-like structure of environments by permitting you to examine the bindings of a particular environment separately from those of its parent.

P.s. Is there some appropriate tag like env for this question in SO? (The recommendations like environment-variables are inappropriate here.)


Solution

  • The docs say that the name is found in the argument environment or ancestors.

    So, no, child environments are not examined.



    This follows the Principle of Least Surprise. When looking up a variable name you should not find stuff in local environments below your current scope (or context).

    For example, if you have a global x and a function (or any lambda) with a local x, should anything other than the function’s code be able to find and use the local x (barring introspection capabilities)?

    No, of course not. Look for a variable in the global context, you should only find global variables.

    But if you look for a variable in a local context, should it not be found there, you should also look in it’s parent context, and keep looking until either the variable is found or we have run out of environments (the global environment being the last or topmost environment in the current scope).