1 of 24

Closures

Today: Scopes

2 of 24

Where are we now?

  • IMP
  • Heaps
  • Types
  • Closures
    • Scopes (Stacks)
    • Long-lived scopes (Linked Stacks)
    • Functions

3 of 24

What is a Closure?

f = lambda x, y : x + y

var f = function(x, y) {

return x + y;

}

var f = (x, y) => {

return x + y;

}

auto f = [](int x, int y) {

return x + y;

};

BinaryOperator<int> f = (int x, int y) -> {

return x + y;

};

Python

C++

Java

Javascript

4 of 24

What is a Closure?

  • A function and a scope (in which to run the function)
  • First-class: a closure is a value
  • Higher-Order: functions may take closures as arguments

var f = fun(x) {

print(x);

};

f(1)

var x = 1;

var f = fun() {

print(x);

};

f();

Output: 1

var x = 1;

var f = fun() {

x = 2;

};

print(x)

var g = fun(h) {

h();

}

print(x)

g(f);

print(x);

Output: 1

Outputs: �1�1�2

5 of 24

Why?

Callbacks

Anonymous helper functions

6 of 24

What is a Closure?

  • A function and a scope (in which to run the function)
  • First-class: a closure is a value
  • Higher-Order: functions may take closures as arguments

var f = fun(x) {

print(x);

};

f(1)

var x = 1;

var f = fun() {

print(x);

};

f();

Output: 1

var x = 1;

var f = fun() {

x = 2;

};

print(x)

var g = fun(h) {

h();

}

print(x)

g(f);

print(x);

Output: 1

Outputs: �1�1�2

7 of 24

Scoping

  • Which variables do the references to x and y refer to?

class Test

{

void foo()

{

int x = 1;

int y = x;

}

}

...

y = x;

...

class Test

{

void foo(int x)

{

int y = x;

}

}

class Test

{

void foo(int x, int y)

{

{

int y = x;

}

print(y);

}

}

8 of 24

Scoping

  • Which variables do the references to x and y refer to?

class Test

{

static int y = 1;

void foo(int x)

{

y = x;

}

}

class Test

{

int y = 1;

void foo(int x)

{

y = x;

}

}

9 of 24

Block Scopes for IMP (MITScript is different)

  • Scope: a defined context that holds a set of variables
  • Block Scope: introduce a new local frame (i.e., scope) via braces
    • var declares a new variable in frame

  • Enables user-controllable scoping
  • Is also a basic building block for other program constructs (next slide)

var x = 1;

{

var x = 2;

print(x);

}

print(x)

var x = 1;

{

x = 2;

print(x);

}

print(x)

output:

2

1

output:

2

2

10 of 24

Scoped For Loop Translation

{

int i = 0;

while (i < n) {

...

++i;

}

}

{

i = 0;

while (i < n) {

...

++i;

}

}

for (int i = 0; i < n; ++i) {

...

}

for (i = 0; i < n; ++i) {

...

}

11 of 24

Add Block Scopes: Grammar

 

 

 

12 of 24

Add Block Scopes: Grammar

 

 

 

 

13 of 24

Block Scopes: Semantic Meaning

  • Introduce a new local frame
    • var introduces new variable in frame

  • Multiple representation options: multiple bindings for each variable or multiple frames. Trick, determine which x to update
  • Solution: keep stack of frames.

var x = 1;

{

var x = 2;

print(x);

}

print(x)

var x = 1;

{

x = 2;

print(x);

}

print(x)

Add x = 1 to scope

Modify x in scope

Add x = 1 to scope

Add x = 2 to scope

14 of 24

Frames, Heaps, and Stacks

  •  

15 of 24

Evaluation Relations (old)

  • Define the semantics of each term in our language (E, B, and S) with an evaluation relation:

  • Meaning: given a frame and heap, the term evaluates to a result

 

 

16 of 24

Evaluation Relations (with heaps and stacks)

  • Define the semantics of each term in our language (E, B, and S) with an evaluation relation:

  • Meaning: given a stack, and heap, the term evaluates to a result

 

 

 

17 of 24

Statements: Inference Rules

 

 

 

 

18 of 24

Expressions: Inference Rules

Integer

Constant

Variable

Reference

 

 

Unary

Minus

 

 

 

19 of 24

Lookup

  •  

 

 

20 of 24

Expressions: Inference Rules

 

Integer

Constant

Variable

Reference

Unary

Minus

 

 

 

 

 

21 of 24

Statements: Inference Rules

 

 

{

var x = 1;

var x = 2;

}

22 of 24

Statements: Inference Rules

 

 

23 of 24

Update

  •  

 

 

24 of 24

Managing Scopes (Example)

1: var f = 0;

2: {

3: var x = 1;

4: f = 5;

5: }

Line

Heap (h)

1

{ 100 : 0 }

2

{ 100 : 0 }

3

{ 100 : 0, 101 : 1 }

4

{ 100 : 0, 101 : 1, 102 : 5 }

5

{ 100 : 0, 101 : 1, 102 : 5 }