scope chapter tenmodern programming languages 1. outline definitions and scope scoping with blocks...
TRANSCRIPT
ScopeScope
Chapter Ten Modern Programming Languages 1
OutlineOutlineDefinitions and scopeScoping with blocksScoping with labeled
namespacesScoping with primitive
namespacesDynamic scoping
Chapter Ten Modern Programming Languages 2
DefinitionsDefinitionsWhen there are different variables
with the same name, there are different possible bindings for that name
Not just variables: type names, constant names, function names, etc.◦Anything you can define
A definition is anything that establishes a possible binding for a name
Chapter Ten Modern Programming Languages 3
ExamplesExamples
Chapter Ten Modern Programming Languages 4
fun square n = n * n;
fun square square = square * square;
We’ll discuss the scope of bindings…
OutlineOutlineDefinitions and scopeScoping with blocksScoping with labeled
namespacesScoping with primitive
namespacesDynamic scoping
Chapter Ten Modern Programming Languages 5
BlocksBlocksA block is any language construct
that contains definitions, and also contains the region of the program where those definitions apply
Chapter Ten Modern Programming Languages 6
let val x = 1; val y = 2;in x+yend
Different ML BlocksDifferent ML BlocksThe let is just a block: no other
purposeA fun definition includes a block:
Multiple alternatives have multiple blocks:
Each rule in a match is a block:
Chapter Ten Modern Programming Languages 7
fun cube x = x*x*x;
fun f (a::b::_) = a+b| f [a] = a| f [] = 0;
case x of (a,0) => a | (_,b) => b
OutlineOutlineDefinitions and scopeScoping with blocksScoping with labeled
namespacesScoping with primitive
namespacesDynamic scoping
Chapter Ten Modern Programming Languages 8
Labeled NamespacesLabeled Namespaces
A labeled namespace is any language construct that contains definitions and a region of the program where those definitions apply, and also has a name that can be used to access those definitions from outside the construct
ML has one called a structure…
Chapter Ten Modern Programming Languages 9
ML StructuresML Structures
A little like a block: a can be used anywhere from definition to the end
But the definitions are also available outside, using the structure name: Fred.a and Fred.f
Chapter Ten Modern Programming Languages 10
structure Fred = struct val a = 1; fun f x = x + a;end;
Other Labeled Other Labeled NamespacesNamespacesNamespaces that are just
namespaces:◦C++ namespace◦Modula-3 module◦Ada package◦Java package
Namespaces that serve other purposes too:◦Class definitions in class-based
object-oriented languages
Chapter Ten Modern Programming Languages 11
OutlineOutlineDefinitions and scopeScoping with blocksScoping with labeled
namespacesScoping with primitive
namespacesDynamic scoping
Chapter Ten Modern Programming Languages 12
Do Not Try This At HomeDo Not Try This At Home
It is legal to have a variable named intML is not confusedYou can even do this (ML understands
that int*int is not a type here):
Chapter Ten Modern Programming Languages 13
- val int = 3;val int = 3 : int
- fun f int = int*int;val f = fn : int -> int- f 3;val it = 9 : int
Primitive NamespacesPrimitive NamespacesML’s syntax keeps types and
expressions separatedML always knows whether it is
looking for a type or for something else
There is a separate namespace for types
Chapter Ten Modern Programming Languages 14
fun f(int:int) = (int:int)*(int:int);
These are in thenamespace for types
These are in theordinary namespace
Primitive NamespacesPrimitive NamespacesNot explicitly created using the
language (like primitive types can’t)◦ They are part of the language definition
Some languages have several separate primitive namespaces◦ See scope.cpp
Java: packages, types, methods, fields, and statement labels are in separate primitive namespaces
C++ is different: all class members are in the same namespace (that class)
Chapter Ten Modern Programming Languages 15
OutlineOutlineDefinitions and scopeScoping with blocksScoping with labeled
namespacesScoping with primitive
namespacesDynamic scoping
Chapter Ten Modern Programming Languages 16
When Is Scoping When Is Scoping Resolved?Resolved?All scoping tools we have seen so
far are static◦They answer the question (whether a
given occurrence of a name is in the scope of a given definition) at compile time
Some languages postpone the decision until runtime: dynamic scoping
Chapter Ten Modern Programming Languages 17
Dynamic ScopingDynamic ScopingEach function has an environment
of definitionsIf a name that occurs in a function
is not found in its environment, its caller’s environment is searched
And if not found there, the search continues back through the chain of callers
This generates a rather odd scope rule…
Chapter Ten Modern Programming Languages 18
Classic Dynamic Scope Classic Dynamic Scope RuleRule
The scope of a definition is the function containing that definition, from the point of definition to the end of the function, along with any functions when they are called (even indirectly) from within that scope—minus the scopes of any redefinitions of the same name in those called functions
Somewhat like a global variable◦hence the caution to avoid them :-)
Chapter Ten Modern Programming Languages 19
ExampleExample
Chapter Ten Modern Programming Languages 20
fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end;
What is the value ofg 5 using ML’s classicblock scope rule?
Block Scope (Static)Block Scope (Static)
Chapter Ten Modern Programming Languages 21
fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end;
With block scope,the reference to inc isbound to the previousdefinition in the sameblock. The definition inf’s caller’s environmentis inaccessible.
g 5 = 6 in ML
Dynamic ScopeDynamic Scope
Chapter Ten Modern Programming Languages 22
fun g x = let val inc = 1; fun f y = y+inc; fun h z = let val inc = 2; in f z end; in h x end;
With dynamic scope,the reference to inc isbound to the definition in the caller’s environment.
g 5 = 7 if ML useddynamic scope
Where It ArisesWhere It ArisesOnly in a few languages: some
dialects of Lisp and APLAdvantage:
◦“Dynamic scoping is useful as a substitute for globally scoped variables. A function can say "let current_numeric_base = 16; call other functions;" and the other functions will all print in hexadecimal. Then when they return, and the base-setting function returns, the base will return to whatever it was.” (c2.com)
Chapter Ten Modern Programming Languages 23
Where It ArisesWhere It ArisesDrawbacks:
◦Difficult to implement efficiently◦Creates large and complicated scopes,
since scopes extend into called functions
◦Choice of variable name in caller can affect behavior of called function kills referential transparency!
Chapter Ten Modern Programming Languages 24