6.035: semantics6.035.scripts.mit.edu/sp17/slides/s17-lecture-08.pdf · 2017-03-18 · •define...

25
6.035: Semantics Closures

Upload: others

Post on 11-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

6.035:SemanticsClosures

Page 2: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

WhatisaClosure?

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

Page 3: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

WhatisaClosure?

• Afunction andascope(inwhichtorunthefunction)• First-class:aclosureisavalue• Higher-Order:functionsmaytakeclosures asarguments

var f = fun(x) {print(x);

};f(1)

var x = 1;var f = fun(y) {

print(x);};f(2);

Output:1

var x = 1;var f = fun() {

x = 2};var g = fun(x) {

x();}g(f);print(x);

Output:1

Outputs:2

Page 4: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

Why?

Page 5: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

Challenge:ManagingScopes

Aclosuremayrefertovalueofvariableinfromacontextthathasbeenpoppedfromthestack

Scope’sframemaylivelongerthanthethesingleexecutionofthescope’scode

Strategy:closurecontainsapointertotheframeinwhichitwascreated

var f = 0;{ var x = 1;f = fun(y) {print(x);x = x + 1;

};}f();f();f();

Output:123

Page 6: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

ExtendingIMPwithClosures

• Syntax:createandcallclosures

• Representation:closuresarevaluesandframesalong-lived

• Semantics:creatingandcalling closures

Page 7: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

ExtendingIMPwithClosures

• Syntax:createandcallclosures

• Representation:closuresarevaluesandframesalong-lived

• Semantics:creatingandcalling closures

Page 8: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

AddClosures:Grammar

𝑆 → var𝑥=𝐸 ∣ 𝑥=𝐸∣ var𝑥=fun(𝑥){𝑆∗} ∣ 𝑥=fun(𝑥){𝑆∗}∣ 𝑥(E)∣ if (E)𝐵𝑆else𝐵𝑆∣ 𝐵𝑆∣ while (E)𝐵𝑆

𝐸 → 𝑛 ∣ 𝑥 ∣ −𝐸∣ 𝐸 + 𝐸 ∣ 𝐸– 𝐸∣ 𝐸 ∗ 𝐸 ∣ 𝐸/𝐸

∣ 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒∣ 𝐸 < 𝐸 ∣ 𝐸 == 𝐸∣ ! 𝐸|𝐸&&𝐸 ∣ 𝐸 ∣∣ 𝐸

𝐵𝑆 → {𝑆∗}(createclosure)(callclosure)

Page 9: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

ExtendingIMPwithClosures

• Syntax:createandcallclosures

• Representation:closuresarevaluesandframesalong-lived

• Semantics:creatingandcalling closures

Page 10: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

LinkedStacks(Boardwork)

1: var x = 1;2: {3: var x = 2;4: { 5: x = 3;6: }7: }

Page 11: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

• Domainofparentfields:𝑃 = {𝜌}• Fieldtoaccessparentofframe

• Domainofframes:𝜎 ∈ Σ = (𝑋 ∪ 𝑃) → 𝐴• Aframe 𝜎 isanelementofthedomainΣ,whichissetofall(partial)functionsthatmapavariable𝑥fromthedomainofallvariables𝑋toanaddress

• Domainofclosures:𝑓 ∈ 𝐹 = 𝐴×𝑋×𝑆∗• Aclosureisatupleconsistingofanframeaddress,variable,andsequenceofstatements

• Domainofheaps:ℎ ∈ 𝐻 = 𝐴 → 𝑉 where𝑉 = ℕ ∪ 𝔹 ∪ Σ ∪ 𝐹• A heapℎ isanelementofthedomainH,whichissetofall(partial)functionsthatmapanaddresstoavalue(integerorboolean)

Frames,Heaps,andLinkedStacks

Page 12: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

EvaluationRelations(withheapsandstacks)

𝛾, 𝒉, 𝒔 → (𝜸, 𝒉)𝛾, 𝒉, 𝒆 → 𝒗 𝛾, 𝒉, 𝒃𝒔 → (𝜸, 𝒉)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenastack,andheap,thetermevaluatestoaresult

Page 13: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

EvaluationRelations(withlinkedstacks)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframepointer,andheap,thetermevaluatestoaresult

𝑎, 𝒉, 𝒔 → 𝒉𝑎, 𝒉, 𝒆 → 𝒗 𝑎, 𝒉, 𝒃𝒔 → 𝒉

Page 14: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

𝑙𝑜𝑜𝑘𝑢𝑝 𝛾, 𝑥 = 𝜎𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣\𝛾, ℎ, 𝑥 → 𝑣\

Expressions:InferenceRules

𝑛 = 𝑛\𝛾, ℎ, 𝑛 → 𝑛\

IntegerConstant

VariableReference

𝑙𝑜𝑜𝑘𝑢𝑝 𝑎], 𝑥 = 𝜎𝜎 𝑥 = 𝑎^ℎ(𝑎^) = 𝑣\𝑎], ℎ, 𝑥 → 𝑣\

𝑛 = 𝑛\𝑎, ℎ, 𝑛 → 𝑛\

𝛾, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝛾, ℎ, −𝑒 → 𝑛\

UnaryMinus

Page 15: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

Lookup

• 𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎 :lookupvariablebindingbytraversingthestack

¬ 𝑎 = 0 ℎ 𝑎 = 𝜎¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑙𝑜𝑜𝑘𝑢𝑝 𝜎(𝜌), 𝑥 = 𝜎′𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎g

¬ 𝑎 = 0 ℎ 𝑎 = 𝜎𝑥 ∈ 𝑑𝑜𝑚(𝜎)𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎

Page 16: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

𝑙𝑜𝑜𝑘𝑢𝑝 𝛾, 𝑥 = 𝜎𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣\𝛾, ℎ, 𝑥 → 𝑣\

Expressions:InferenceRules

𝑛 = 𝑛\𝛾, ℎ, 𝑛 → 𝑛\

IntegerConstant

VariableReference

𝑙𝑜𝑜𝑘𝑢𝑝 𝑎], 𝑥 = 𝜎𝜎 𝑥 = 𝑎^ℎ(𝑎^) = 𝑣\𝑎], ℎ, 𝑥 → 𝑣\

𝑛 = 𝑛\𝑎, ℎ, 𝑛 → 𝑛\

𝛾, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝛾, ℎ, −𝑒 → 𝑛\

UnaryMinus

𝑎, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝑎, ℎ, −𝑒 → 𝑛\

Page 17: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

var𝑥=𝐸

InferenceRules:Declaration

ℎ 𝑎] = 𝜎 ¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑎, ℎ, 𝑒 → 𝑣

¬(𝑎^ ∈ 𝑑𝑜𝑚 ℎ ) ℎ 𝑎]: 𝜎g, 𝑎^: 𝑣 = ℎ′

𝑎], ℎ, var𝑥 = 𝑒 → ℎ′

𝜎 𝑥 ∶ 𝑎^ = 𝜎g

Page 18: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

𝑥=𝐸

InferenceRules:Assignment

𝑎, ℎ, 𝑒 → 𝑣𝑢𝑝𝑑𝑎𝑡𝑒 𝑎, ℎ, 𝑥, 𝑣, ℎ′𝑎, ℎ, 𝑥 = 𝑒 → ℎ′

Page 19: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

Update

• 𝑢𝑝𝑑𝑎𝑡𝑒 𝑎, ℎ, 𝑥, 𝑣, ℎ′ =

• Recursivelysearchthestackforx,andupdatex,returnnewheap

ℎ 𝑎] = 𝜎¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑢𝑝𝑑𝑎𝑡𝑒 𝜎 𝜌 , ℎ, 𝑥, 𝑣, ℎg

𝑢𝑝𝑑𝑎𝑡𝑒 𝑎], ℎ, 𝑥, 𝑣, ℎg

ℎ 𝑎] = 𝜎𝑥 ∈ 𝑑𝑜𝑚 𝜎 ¬ 𝑎^ ∈ 𝑑𝑜𝑚 ℎ 𝜎 𝑥 ∶ 𝑎^ = 𝜎gℎ[𝑎] ∶ 𝜎g, 𝑎 ∶ 𝑣] = ℎ′𝑢𝑝𝑑𝑎𝑡𝑒 𝑎], ℎ, 𝑥, 𝑣, ℎg

Page 20: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

InferenceRules:BlockScope

BlockScope{𝑆∗} :

SequentialComposition𝑆∗:

𝜎 = 𝜌 ∶ 𝑎] ¬ 𝑎l ∈ 𝑑𝑜𝑚 ℎ ℎ 𝑎l ∶ 𝜎 = ℎg 𝑎l,ℎ′, 𝑆∗ → ℎg′𝑎], ℎ, 𝑏𝑠𝑆∗ → ℎgg

𝑎, ℎ, [] → ℎ

𝑎, ℎ, 𝑆 → ℎ′ 𝑎, ℎg, 𝑆\ → ℎ′′𝑎, 𝑆 ∷ 𝑆\ → ℎ′′

Page 21: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

ExtendingIMPwithClosures

• Syntax:createandcallclosures

• Representation:closuresarevaluesandframesalong-lived

• Semantics:creatingandcalling closures

Page 22: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

Declarationvar𝑥=fun(𝑥){𝑆∗}:

• Executingaclosure:captureframepointerandstoreclosureinheap

InferenceRules:ClosureCreation(Declaration)

ℎ 𝑎] = 𝜎 ¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎

¬(𝑎^ ∈ 𝑑𝑜𝑚 ℎ ) ℎ 𝑎]: 𝜎g, 𝑎^: 𝑓 = ℎ′

𝑎], ℎ, var𝑥=fun(𝑥o){𝑆∗} → ℎ′

𝜎 𝑥 ∶ 𝑎^ = 𝜎g

𝑎], 𝑥o, 𝑆∗ = 𝑓

Page 23: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

ManagingScope(Revisited)

1: var f = 0;2: { 3: var x = 1;4: f = fun(y) {5: print(x);6: x = x + 1;7: };8: }9: f();10: f();

Line FP(a) Heap(h)1 200 {100:0,200:{p:0,f:100}}3 208 {100:0,108:1,200 :{p:0,f:100},208:{p:200,x:108}}7 208 {100:0,108:1,116:(208, y,{print(x);x=x+1}),

200:{p:0,f:116},208:{p:200,x:108}}8 200 {100:0,108:1,116:(208, y,{print(x);x=x+1}),

200:{p:0,f:116},208:{p:200,x:108}}9 200 {100:0,108:1,116:(208, y,{print(x);x=x+1}),124:2,

200:{p:0,f:116},208:{p:200,x:124}}10 200 {100:0,108:1,116:(208, y,{print(x);x=x+1}),124:2,132:3

200:{p:0,f:116},208:{p:208,x:132}}

Page 24: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

InferenceRules:ClosureExecution

ClosureCall:𝑥(𝑒)

𝑎], ℎ, 𝑥 → 𝑎p, 𝑥o, 𝑆∗

𝑎], ℎ, 𝑒 → 𝑣

¬(𝑎l∈ 𝑑𝑜𝑚(ℎ)) 𝜎 = {𝜌 ∶ 𝑎p, 𝑥o ∶ 𝑣}

ℎ′ = ℎ[𝑎l: 𝜎]

xEvaluatestoaclosure

Eevaluatestoavalue

Createanewframe

Evaluatebodyofclosure𝑎l, ℎg, 𝑆∗ → ℎ′′

𝑎], ℎ, 𝑥(𝑒) → ℎ′′

Page 25: 6.035: Semantics6.035.scripts.mit.edu/sp17/slides/S17-lecture-08.pdf · 2017-03-18 · •Define the semantics of each term in our language (E, B, and S) with an evaluation relation

TheEnd