meeting06 scope.6up

7
1 Scope Prof. Evan Chang Meeting 6, CSCI 3155, Fall 2009 Announcements Assignment 2 due tonight at 11:55pm – Submit individually Assignment 3 out tonight – PL-Detective questions • I select the semantics of MYSTERY and you guess them. • You have a limited number of trials. • Experimentation is not a proof but gives some evidence, so explanation of your reasoning is critical 2 Feedback Please feel free to tell/e-mail me any concerns that come up – Recitations? 3 Feedback from HW1 Time spent: mean 1.26 hrs, median 1 hr Easy but first homework 4 Next Unit More “doing” projects? 5 Review

Upload: ravi

Post on 31-Jan-2016

223 views

Category:

Documents


0 download

DESCRIPTION

ppl scope

TRANSCRIPT

Page 1: Meeting06 Scope.6up

1

Scope

Prof. Evan Chang

Meeting 6, CSCI 3155, Fall 2009

Announcements

• Assignment 2 due tonight at 11:55pm– Submit individually

• Assignment 3 out tonight– PL-Detective questions

• I select the semantics of MYSTERY and you guess them.

• You have a limited number of trials.

• Experimentation is not a proof but gives some evidence, so explanation of your reasoning is critical

2

Feedback

• Please feel free to tell/e-mail me any concerns that come up– Recitations?

3

Feedback from HW1

• Time spent: mean 1.26 hrs, median 1 hr

• Easy but first homework

4

Next Unit

• More “doing” projects?

5

Review

Page 2: Meeting06 Scope.6up

2

Variables

• Attributes (Sebesta)– Name

– Address

– Type

– Value

– Lifetime

– Scope7

Aliasing

int* x; int* y; …

// have cells pointed to by x and y

// create alias

8

Binding

• A mapping from a name to a value

• Name?

• Value?

9

End of ReviewFinish Binding

Example: Static and Dynamic Typing

• static: type bound to variable at compile-time

• dynamic: type bound to variable at run-time

11

Example: Static and Dynamic Typing

x = 3;

y = x + 1;

x = [7, 9, 2];

y = x + 1

Ok with static or dynamic typing?

12

Page 3: Meeting06 Scope.6up

3

Example: Static and Dynamic Typing

• With this view, what is wrong with dynamic typing?

• Dynamic typing forces the overhead of tag checking always

13

Static typing includes dynamic typingStatic typing includes dynamic typing

Example: Static and Dynamic Typing

• Can we havex = 3; x = [10.2, 5, “bob”]; ?

14

Type Inference (ML)

• Static and dynamic typing is orthogonal to explicit declarations

15 16

Storage Binding

• Storage binding determines lifetime– Why?

• Kinds of storage binding– static

– stack dynamic

– explicit heap dynamic

– implicit heap dynamic

17

Storage Binding: Static

• What is it? Example? Advantages?

18

Page 4: Meeting06 Scope.6up

4

Storage Binding: Stack Dynamic

• What is it? Example? Advantages?

19

Storage Binding: Stack Dynamic

• Suppose function f has local x, g has y, and hhas z. Function f calls g and g calls h. What is the lifetime of each variable?

20

Storage Binding: Explicit Heap Dynamic

• What is it? Example? Advantages?

21

Storage Binding: Explicit Heap Dynamic

22

Storage Binding: Implicit Heap Dynamic

• What is it? Example? Advantages?

23

When are things freed?

24

Page 5: Meeting06 Scope.6up

5

End of BindingOn to Scope

Preview

• What is scope?

• What is static scoping?

• What is dynamic scoping?

26

Static scoping: Let’s extend arithmetic with a binding construct

e ::= n | e1 + e2 | e1 * e2 | let x = e1 in e2 | x

• let binds a new variable x to e1

for use in e2

– variable x is bound in the body e2

– the scope of x is the body e2

27

What is meant by “new”?

• Every instance of let binds a variable that uniquely identified with it.

28

Example expressions

29

How do resolve variables?

•• Lexical scope ruleLexical scope rule:– A variable is bound by the nearest enclosing binding• Operationally, how?

30

Page 6: Meeting06 Scope.6up

6

Can we do a program transformation to make it clear where a variable is bound?

• How?

31

Rename bound variables

• We identify terms up to renaming (α-conversion)

32

HigherHigher--Order Abstract SyntaxOrder Abstract Syntax:HigherHigher--Order Abstract SyntaxOrder Abstract Syntax:All terms that differ only in names of bound variables are considered equivalent.

Examples

33

Dynamic Scoping

• “Scope depends on the calling sequence of functions”

• Within a function, is there any difference with static scoping?

35

Exercise: Which x is used?

PROCEDURE f() =

VAR x : INTEGER;

PROCEDURE g() =

VAR x : INTEGER; BEGIN … END;

PROCEDURE h() = BEGIN … x … END;

BEGIN BEGIN

h(); g(); g(); h();

END END

36

Page 7: Meeting06 Scope.6up

7

Summary: Dynamic Scoping

• No variable is local making programs difficult to debug– e.g., in LISP, TCL, TeX

• Generally, considered a bug in PL implementation– (but watch out, very easy to do!)

37

For Next Time

• Reading

• Online discussion forum– ≥1 substantive question, comment, or answer each week

• Homework assignment 3– Start early!

38