Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Objectives You should be able to... Variables have many different attributes. These attributes can become Variables bound to the variable at different times. ◮ Know the difference between static and dynamic binding... Dr. Mattox Beckman ◮ of value ◮ of types University of Illinois at Urbana-Champaign ◮ of location Department of Computer Science ◮ of scoping (!) ◮ Know the difference between implicit and explicit declaration. ◮ Know what aliasing is. Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping What is a Variable? Static vs. Dynamic Binding Static Binding Attribute is bound at compile time. Mathematically ◮ Allows the compiler to “hard code” information about the variable Variables represent a (possibly unknown) quantity or value. They usually into the executable code are part of a model (or abstraction) of some concept or system. ◮ Allows the compiler to perform optimizations based on its knowledge of the variable. f ( x ) = 2 i π − x Dynamic Binding Programming Attribute is bound at run time. Variables are implementations of mathematical variables. (Has anyone ◮ A variable’s attribute could change during the course of execution, here read Plato?) or remain undetermined—very fmexible. ◮ Information about the variable is usually stored with it. ◮ Sometimes we don’t know the value of the attribute at compile time.
val id : ' a -> ' a = < fun > #!/usr/bin/perl int sqr( int i) { return i * i; } movi r1, val(i) movi r2, val(i) multi r1,r2,r3 string s = "hi"; bool b = true; if s then printf("4") else printf("9"); $i = "The answer is "; return i; print "$i"; $i = 42; print "$i\n"; int identity( int i) { return i; } double identity( double x) { return x; } template < class T > T ident(T &i) { return i; } # let id x = x;; } pushi r3 int foo( int j) { return i * j; } i = foo(i); const int i = 2; int bar() { int i = 10; Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Value Static Typing ◮ The value attribute of a variable is most likely to be dynamic. ◮ Sometimes we want the value to be static. (Not to be confused with ◮ Static Typing: the type of variables are known at compile time. the static keyword in C.) ◮ This makes many operations very effjcient. Static Value 1 1 2 2 1 3 3 2 4 4 3 ◮ The compiler can catch errors: improving programmer reliability. 4 5 1 6 2 7 3 8 9 Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Dynamic Typing Polymorphism ◮ We can have both the advantages of strong typing and dynamic typing at the same time! Some languages (e.g., BASIC, Perl most shell languages, TCL) use dynamic typing. Overloading 1 2 3 4 Parameterized 5 6 7 Actually, Perl types are partially dynamic. Scalars, arrays, and hashes Automatic are represented with different syntax.
void inc( int &x) { int length() { x = x + 1; } // after this i and x will be the same! } int i = 20; return i + length(s); String s = new String("hello"); int i = 10; ... inc(i) ... Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Location Fortran ◮ Heap allocated variables — completely dynamic The Problem ◮ Stack allocated variables — partially static “stack relative” ◮ First released on the IBM 704 in 1957. It had core memory allocation (equivalent to 18,432 bytes ) and a 12k FLOP processor. 1 ◮ Can we use a high level language and translate it to machine code? 2 3 The Solution: Hard-code variable locations 4 ◮ This made Fortran almost as fast as assembly. 5 ◮ Still the language of choice for numerical computation. Weird Language ◮ Downside—you don’t get recursion. (Modern Fortran fjxes this.) There is one language in which all variables — even function arguments — are allocated statically! Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Aliasing Bad Aliasing It is possible for multiple variables to refer to the same location. 1 Knowing about aliasing and storage is critical. Never forget that your 2 variables are representations only. 3 4 Do the Aliasing Bug activity. 5 6 7 Use with extreme caution!
return foo(); int foo( int i) { int bar( int i) { } int i = 2; } int foo() { return i * i; } return j + 10; int bar() { int j = 10; int i = 10; } int j = 20; ( setq i 2) ;; global variable i = 2 (defun foo () (* i i)) (defun bar () ( let ((i 10)) ;; local variable i = 10 (foo))) ;; call function foo return foo(j) + foo(i); Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Lifetime Example in C ◮ Variables have a certain scope in the program for which they are Consider the following program: valid. 1 ◮ This allows us to have multiple variables with the same name. 2 ◮ Usually the scope (or lifetime ) is determined syntactically. 3 4 1 5 2 6 3 7 4 8 5 ◮ What value will function bar return? 6 7 ◮ 4 ◮ 100 8 9 Introduction Static vs. Dynamic Binding Value Typing Location Scoping Introduction Static vs. Dynamic Binding Value Typing Location Scoping Example in Emacs Lisp Static vs. Dynamic Scoping 1 ◮ Most languages use static scoping . 2 ◮ The fjrst Lisp implementations used dynamic scoping . 3 ◮ Today it is considered to be a Bad Thing TM by most sentient 4 5 life-forms. ◮ As always, some disagree... 6 ◮ It’s too easy to modify the behavior of a function. 7 8 ◮ Correct use requires knowledge of a function’s internals. ◮ What value will expression (bar) return? Still used by Emacs Lisp ! ◮ 4 ◮ 100
Recommend
More recommend