## Dynamic/Lexical Scoping with Substitution

Kineese
BASIC-Programmierer
Beiträge: 111
Registriert: 14. Feb 2008 18:33

### Dynamic/Lexical Scoping with Substitution

Hi there,
in the todays practice I thought I mentioned out that immediate substitution always uses static scoping, because each substitution traverses the whole Sub-AST (that is what I noticed). But on an other page of my notes I have written that immediate substitution never uses static scoping.

My opinion is that my first assumption (immediate subst. --> static scoping) is the correct one, caused of the traversation of the whole AST (guess that isn't the real reason).

I try to explain it with the following example:

Code: Alles auswählen

n=23
f()
print(n)
def f(){
n = 42
}
The n = 23 expression could be seen as an With-Expression. With immediate substitution, we try to replace all (free!) occurrences of n with 23.

Code: Alles auswählen


f()
print(23)
def f(){
n =42
}
At least we see that the function f don't have any effects (static scoping) on n, so that the print(n) will result to 23.

Does someone agrees my explaination?

greets
K.

Edit:
In addition, if we currently use deferred substitution with dynamic scope and want to switch it to static scoping, we need the introduce the closure, so the environment wont be lost at function-definition. Am I right???

Seldon
BASIC-Programmierer
Beiträge: 119
Registriert: 19. Apr 2012 18:12

### Re: Dynamic/Lexical Scoping with Substitution

Yes, your reasoning is right (cf. lecture 2, slides 36 and 77 et seqq.).

Kineese
BASIC-Programmierer
Beiträge: 111
Registriert: 14. Feb 2008 18:33

### Re: Dynamic/Lexical Scoping with Substitution

Seldon hat geschrieben:Yes, your reasoning is right (cf. lecture 2, slides 36 and 77 et seqq.).
Thanks for you answer.. but I guess you mean my first Question ( Immediate Subst. --> Static Scoping)
Can you agree with my 2nd answer? (Deffered Subst. and dynamic scope switch to static --> Closure)

Seldon
BASIC-Programmierer
Beiträge: 119
Registriert: 19. Apr 2012 18:12

### Re: Dynamic/Lexical Scoping with Substitution

I mean both, p. 77 and onwards talks about the scoping problems with first class functions, introducing closures With first order functions, all we had to do to eliminate dynamic scoping was to start with a fresh environment.

Kineese
BASIC-Programmierer
Beiträge: 111
Registriert: 14. Feb 2008 18:33