and bindings (part 2) lecture 5: names, scopes ... · 14 prof. dr. michael pradel software lab,...

43
14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes, and Bindings (Part 2)

Upload: others

Post on 20-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

14

Prof. Dr. Michael Pradel

Software Lab, University of StuttgartWinter 2019/2020

Programming ParadigmsLecture 5: Names, Scopes,and Bindings (Part 2)

Page 2: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

15 - 1

Wake-up Exercise

What does the following Java code print?

Integer foo; Integer bar;

foo = 1000; bar = 1000;System.out.println(foo <= bar);System.out.println(foo == bar);

foo = 42; bar = 42;System.out.println(foo <= bar);System.out.println(foo == bar);

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 3: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

15 - 2

Wake-up Exercise

What does the following Java code print?

Integer foo; Integer bar;

foo = 1000; bar = 1000;System.out.println(foo <= bar);System.out.println(foo == bar);

foo = 42; bar = 42;System.out.println(foo <= bar);System.out.println(foo == bar);

Result: true, false, true, truehttps://ilias3.uni-stuttgart.de/vote/0ZT9

Page 4: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

15 - 3

Wake-up Exercise

What does the following Java code print?

Integer foo; Integer bar;

foo = 1000; bar = 1000;System.out.println(foo <= bar);System.out.println(foo == bar);

foo = 42; bar = 42;System.out.println(foo <= bar);System.out.println(foo == bar);

Result: true, false, true, true

Integer objects thatwrap a primitive value

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 5: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

15 - 4

Wake-up Exercise

What does the following Java code print?

Integer foo; Integer bar;

foo = 1000; bar = 1000;System.out.println(foo <= bar);System.out.println(foo == bar);

foo = 42; bar = 42;System.out.println(foo <= bar);System.out.println(foo == bar);

Result: true, false, true, truehttps://ilias3.uni-stuttgart.de/vote/0ZT9

Compares thewrappedprimitives: true

Page 6: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

15 - 5

Wake-up Exercise

What does the following Java code print?

Integer foo; Integer bar;

foo = 1000; bar = 1000;System.out.println(foo <= bar);System.out.println(foo == bar);

foo = 42; bar = 42;System.out.println(foo <= bar);System.out.println(foo == bar);

Result: true, false, true, truehttps://ilias3.uni-stuttgart.de/vote/0ZT9

Compares theobjects:� Different objects,

hence false� But: Java caches

wrapped integersbetween -128 and128, hence true

Page 7: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

16

Overview

� Object lifetime and storagemanagement

� Scopes

� Aliasing and overloading

� Binding of referencing environments

Page 8: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

17

Scoping Rules

� Scoping rules: Define which bindingsare active� I.e., what’s the meaning of a name at a given

program point?

� Each PL defines its scoping rules

� E.g., Basic has only one scope

� Most PLs have nested scopes for subroutines

Page 9: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

18

Nested Scopes

� Common for nested subroutines

� Each subroutine has its own scope

� Closest nested scope rule

� Name is known in scope where it is declared

and all internally nested scopes

� Inner scopes can hide names from outer

scopes

Page 10: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

26

Page 11: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

20

Static vs. Dynamic Scoping

Static scoping

� Binding of a name can be derived from program

text

� Most common in today’s PLs

Dynamic scoping

� Binding of a name depends on control flow

� I.e., not known statically (in general)

Page 12: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

21 - 1

Example

global x = 1fun a() {

local x = 3b()

}fun b() {

y = x}a()

Pseudo code:

Page 13: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

21 - 2

Example

global x = 1fun a() {

local x = 3b()

}fun b() {

y = x}a()

Pseudo code:

Static scoping:y gets value 1 becauseb doesn’t have a localvariable called x andthe surrounding staticscope provides theglobal variable x

Page 14: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

21 - 3

Example

global x = 1fun a() {

local x = 3b()

}fun b() {

y = x}a()

Pseudo code:

Dynamic scoping:y gets value 3 becauseb doesn’t have a localvariable called x andthe dynamically closestscope provides thelocal variable x of a

Page 15: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

22

Function Stack vs. Static Scopes

How to resolve bindings outside ofcurrent scope?

� Each allocation frame has a static link to its

parent scope

� Push allocationframes on calls

� Pop frames onreturns

� Not affected bywhich functionsget called

Page 16: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

27

Page 17: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

24

Built-in Objects

Many PLs have built-in (or predefined)objects

� E.g., for built-in types and APIs

� Invisible, outer-most scope

� Accessible from all scopes, except if hidden

Page 18: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

25

Example: Monkey Patching

[Demo: Monkey patching in JavaScript]

Page 19: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

26 - 1

Quiz: Scopes

What does this Python code print?(Hint: Python uses static scoping)

https://ilias3.uni-stuttgart.de/vote/0ZT9

x = "a"def f():

def g():print(x)

def h():g()x = "c"print(x)

x = "b"h()print(x)

f()print(x)

Page 20: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

26 - 2

Quiz: Scopes

x = "a"def f():

def g():print(x) # (1) x in f : "b"

def h():g()x = "c"print(x) # (2) x in h : "c"

x = "b"h()print(x) # (3) x in f : "b"

f()print(x) # (4) x in main: "a"

What does this Python code print?(Hint: Python uses static scoping)

https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 21: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

27

Overview

� Object lifetime and storagemanagement

� Scopes

� Aliasing and overloading

� Binding of referencing environments

Page 22: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

28

Aliasing and Overloading

Aliasing: Two more more names refer tothe same object

Overloading: A name refers to two moreobjects

name1name2

object1

name1 object2object1

Page 23: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

29 - 1

Aliasing: Example#include <stdio.h>

void half(double& a){ // argument passed by reference

a = a / 2;}

int main( int argc, const char* argv[] ){

double n = 5.0;double *p = &n; // pointer to value stored in n

half(n);half(*p);

printf("%f\n", n);}

Page 24: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

29 - 2

Aliasing: Example#include <stdio.h>

void half(double& a){ // argument passed by reference

a = a / 2;}

int main( int argc, const char* argv[] ){

double n = 5.0;double *p = &n; // pointer to value stored in n

half(n);half(*p);

printf("%f\n", n);} Result: 1.250000

Aliases to samememory object

Page 25: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

30 - 1

Overloading: Example

class Overloading{void foo() {}void foo(int n) {}void foo(String s) {}

public static void main(String[] args) {Overloading o = new Overloading();o.foo(...);

}}

Page 26: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

30 - 2

Overloading: Example

class Overloading{void foo() {}void foo(int n) {}void foo(String s) {}

public static void main(String[] args) {Overloading o = new Overloading();o.foo(...);

}}

Three methods,all with name“foo”

Resolution of namedepends on arguments

Page 27: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

31

Overview

� Object lifetime and storagemanagement

� Scopes

� Aliasing and overloading

� Binding of referencing environments

Page 28: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

32

Referencing Environment

Complete set of bindings at a point in theprogram

� Determined by scoping rules (e.g., static or

dynamic scoping)

What if we create a reference to afunction?

� When to apply the scoping rules?

Page 29: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

33 - 1

Example

Pseudo code:

function a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

Page 30: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

33 - 2

Example

Pseudo code:

function a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

Reference toa function

Page 31: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

33 - 3

Example

Pseudo code:

function a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

Reference toa function

Functioncalled here

Page 32: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

33 - 4

Example

Pseudo code:

function a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

What memory objectis x bound to?

Page 33: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

34 - 1

Shallow Binding

Referencing environment created whenfunction is called

� Common in languages with dynamic scoping

Page 34: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

34 - 2

Shallow Binding

Referencing environment created whenfunction is called

� Common in languages with dynamic scopingfunction a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

Page 35: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

34 - 3

Shallow Binding

Referencing environment created whenfunction is called

� Common in languages with dynamic scopingfunction a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

x bound to the globalvariable initialized to 5;code prints 5

Page 36: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

35 - 1

Deep Binding

Referencing environment created whenthe reference to the function is created

� Common in languages with static scopingfunction a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

Page 37: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

35 - 2

Deep Binding

Referencing environment created whenthe reference to the function is created

� Common in languages with static scopingfunction a() {var x = 23;function b() {console.log(x);

}x = 42;return b;

}b = a();var x = 5;b();

x bound to the localvariable initialized to 23;code prints 42, as this isthe most recent value of x

Page 38: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

36

Closure

� Implementation of deep binding

� Closure = Representation ofreferencing environment + functionitself

� When creating reference to function,closure it created

Page 39: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

37

Example: Closures

function outer(k, fun) {function inner() {

console.log(k);}

if (k > 0)fun();

elseouter(k + 1, inner)

}

function other() {}

outer(-1, other);

Page 40: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

28

Page 41: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

39 - 1

Quiz: Scopes and Bindings

Which of the following statements istrue?

� Scoping rules (e.g., static or dynamic) determine

how names are bound to memory objects.

� Built-in objects can be thought of as an invisible,

outer scope.

� Languages with pointers don’t have any aliasing.

� Closures implement referencing environments

created via shallow binding.https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 42: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

39 - 2

Quiz: Scopes and Bindings

Which of the following statements istrue?

� Scoping rules (e.g., static or dynamic) determine

how names are bound to memory objects.

� Built-in objects can be thought of as an invisible,

outer scope.

� Languages with pointers don’t have any aliasing.

� Closures implement referencing environments

created via shallow binding.https://ilias3.uni-stuttgart.de/vote/0ZT9

Page 43: and Bindings (Part 2) Lecture 5: Names, Scopes ... · 14 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Winter 2019/2020 Programming Paradigms Lecture 5: Names, Scopes,

40

Overview

� Object lifetime and storagemanagement

� Scopes

� Aliasing and overloading

� Binding of referencing environments 4