You are on page 1of 7

Announcements

Scope

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

• Assignment 3 out tonight

Prof. Evan Chang
Meeting 6, CSCI 3155, Fall 2009

– 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

Feedback from HW1

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

• Time spent: mean 1.26 hrs, median 1 hr
• Easy but first homework

– Recitations?

3

4

Next Unit
• More “doing” projects?

Review

5

1

y=x+1 Ok with static or dynamic typing? 11 12 2 . x = [7.Variables Aliasing • Attributes (Sebesta) int* x. y = x + 1. 9. … // have cells pointed to by x and y – Name – Address // create alias – Type – Value – Lifetime – Scope 7 8 Binding • A mapping from a name to a value • Name? End of Review Finish Binding • Value? 9 Example: Static and Dynamic Typing Example: Static and Dynamic Typing • static: type bound to variable at compile-time • dynamic: type bound to variable at run-time x = 3. int* y. 2].

? • With this view.Example: Static and Dynamic Typing Example: Static and Dynamic Typing • Can we have Static typing includes dynamic typing x = 3.2. “bob”]. x = [10. what is wrong with dynamic typing? • Dynamic typing forces the overhead of tag checking always 13 14 15 16 Type Inference (ML) • Static and dynamic typing is orthogonal to explicit declarations Storage Binding Storage Binding: Static • Storage binding determines lifetime • What is it? Example? Advantages? – Why? • Kinds of storage binding – – – – static stack dynamic explicit heap dynamic implicit heap dynamic 17 18 3 . 5.

and h has z. Function f calls g and g calls h.Storage Binding: Stack Dynamic Storage Binding: Stack Dynamic • What is it? Example? Advantages? • Suppose function f has local x. What is the lifetime of each variable? 19 Storage Binding: Explicit Heap Dynamic 20 Storage Binding: Explicit Heap Dynamic • What is it? Example? Advantages? 21 Storage Binding: Implicit Heap Dynamic 22 When are things freed? • What is it? Example? Advantages? 23 24 4 . g has y.

how? 29 30 5 .Preview • What is scope? End of Binding On to 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 What is meant by “new”? • Every instance of let binds a variable that uniquely identified with it. • 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 Example expressions 28 How do resolve variables? • Lexical scope rule rule: – A variable is bound by the nearest enclosing binding • Operationally.

PROCEDURE h() = BEGIN … x … END. END 35 BEGIN g(). • Within a function. g().Can we do a program transformation to make it clear where a variable is bound? Rename bound variables • We identify terms up to renaming (αconversion) • How? Higher-Order Abstract Syntax: HigherSyntax All terms that differ only in names of bound variables are considered equivalent. PROCEDURE g() = VAR x : INTEGER. is there any difference with static scoping? BEGIN h(). 31 32 Examples 33 Dynamic Scoping Exercise: Which x is used? • “Scope depends on the calling sequence of functions” PROCEDURE f() = VAR x : INTEGER. END 36 6 . BEGIN … END. h().

. TeX – ≥1 substantive question. TCL. or answer each week • Generally.Summary: Dynamic Scoping For Next Time • No variable is local making programs difficult to debug • Reading • Online discussion forum – e.g. in LISP. very easy to do!) 37 38 7 . considered a bug in PL implementation • Homework assignment 3 – Start early! – (but watch out. comment.