6.035: semantics6.035.scripts.mit.edu/sp17/slides/s17-lecture-08.pdf · 2017-03-18 · •define...
TRANSCRIPT
6.035:SemanticsClosures
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
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
Why?
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
ExtendingIMPwithClosures
• Syntax:createandcallclosures
• Representation:closuresarevaluesandframesalong-lived
• Semantics:creatingandcalling closures
ExtendingIMPwithClosures
• Syntax:createandcallclosures
• Representation:closuresarevaluesandframesalong-lived
• Semantics:creatingandcalling closures
AddClosures:Grammar
𝑆 → var𝑥=𝐸 ∣ 𝑥=𝐸∣ var𝑥=fun(𝑥){𝑆∗} ∣ 𝑥=fun(𝑥){𝑆∗}∣ 𝑥(E)∣ if (E)𝐵𝑆else𝐵𝑆∣ 𝐵𝑆∣ while (E)𝐵𝑆
𝐸 → 𝑛 ∣ 𝑥 ∣ −𝐸∣ 𝐸 + 𝐸 ∣ 𝐸– 𝐸∣ 𝐸 ∗ 𝐸 ∣ 𝐸/𝐸
∣ 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒∣ 𝐸 < 𝐸 ∣ 𝐸 == 𝐸∣ ! 𝐸|𝐸&&𝐸 ∣ 𝐸 ∣∣ 𝐸
𝐵𝑆 → {𝑆∗}(createclosure)(callclosure)
ExtendingIMPwithClosures
• Syntax:createandcallclosures
• Representation:closuresarevaluesandframesalong-lived
• Semantics:creatingandcalling closures
LinkedStacks(Boardwork)
1: var x = 1;2: {3: var x = 2;4: { 5: x = 3;6: }7: }
• 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
EvaluationRelations(withheapsandstacks)
𝛾, 𝒉, 𝒔 → (𝜸, 𝒉)𝛾, 𝒉, 𝒆 → 𝒗 𝛾, 𝒉, 𝒃𝒔 → (𝜸, 𝒉)
• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:
• Meaning:givenastack,andheap,thetermevaluatestoaresult
EvaluationRelations(withlinkedstacks)
• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:
• Meaning:givenaframepointer,andheap,thetermevaluatestoaresult
𝑎, 𝒉, 𝒔 → 𝒉𝑎, 𝒉, 𝒆 → 𝒗 𝑎, 𝒉, 𝒃𝒔 → 𝒉
𝑙𝑜𝑜𝑘𝑢𝑝 𝛾, 𝑥 = 𝜎𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣\𝛾, ℎ, 𝑥 → 𝑣\
Expressions:InferenceRules
𝑛 = 𝑛\𝛾, ℎ, 𝑛 → 𝑛\
IntegerConstant
VariableReference
𝑙𝑜𝑜𝑘𝑢𝑝 𝑎], 𝑥 = 𝜎𝜎 𝑥 = 𝑎^ℎ(𝑎^) = 𝑣\𝑎], ℎ, 𝑥 → 𝑣\
𝑛 = 𝑛\𝑎, ℎ, 𝑛 → 𝑛\
𝛾, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝛾, ℎ, −𝑒 → 𝑛\
UnaryMinus
Lookup
• 𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎 :lookupvariablebindingbytraversingthestack
¬ 𝑎 = 0 ℎ 𝑎 = 𝜎¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑙𝑜𝑜𝑘𝑢𝑝 𝜎(𝜌), 𝑥 = 𝜎′𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎g
¬ 𝑎 = 0 ℎ 𝑎 = 𝜎𝑥 ∈ 𝑑𝑜𝑚(𝜎)𝑙𝑜𝑜𝑘𝑢𝑝 𝑎, 𝑥 = 𝜎
𝑙𝑜𝑜𝑘𝑢𝑝 𝛾, 𝑥 = 𝜎𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣\𝛾, ℎ, 𝑥 → 𝑣\
Expressions:InferenceRules
𝑛 = 𝑛\𝛾, ℎ, 𝑛 → 𝑛\
IntegerConstant
VariableReference
𝑙𝑜𝑜𝑘𝑢𝑝 𝑎], 𝑥 = 𝜎𝜎 𝑥 = 𝑎^ℎ(𝑎^) = 𝑣\𝑎], ℎ, 𝑥 → 𝑣\
𝑛 = 𝑛\𝑎, ℎ, 𝑛 → 𝑛\
𝛾, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝛾, ℎ, −𝑒 → 𝑛\
UnaryMinus
𝑎, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛\𝑎, ℎ, −𝑒 → 𝑛\
var𝑥=𝐸
InferenceRules:Declaration
ℎ 𝑎] = 𝜎 ¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑎, ℎ, 𝑒 → 𝑣
¬(𝑎^ ∈ 𝑑𝑜𝑚 ℎ ) ℎ 𝑎]: 𝜎g, 𝑎^: 𝑣 = ℎ′
𝑎], ℎ, var𝑥 = 𝑒 → ℎ′
𝜎 𝑥 ∶ 𝑎^ = 𝜎g
𝑥=𝐸
InferenceRules:Assignment
𝑎, ℎ, 𝑒 → 𝑣𝑢𝑝𝑑𝑎𝑡𝑒 𝑎, ℎ, 𝑥, 𝑣, ℎ′𝑎, ℎ, 𝑥 = 𝑒 → ℎ′
Update
• 𝑢𝑝𝑑𝑎𝑡𝑒 𝑎, ℎ, 𝑥, 𝑣, ℎ′ =
• Recursivelysearchthestackforx,andupdatex,returnnewheap
ℎ 𝑎] = 𝜎¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎 𝑢𝑝𝑑𝑎𝑡𝑒 𝜎 𝜌 , ℎ, 𝑥, 𝑣, ℎg
𝑢𝑝𝑑𝑎𝑡𝑒 𝑎], ℎ, 𝑥, 𝑣, ℎg
ℎ 𝑎] = 𝜎𝑥 ∈ 𝑑𝑜𝑚 𝜎 ¬ 𝑎^ ∈ 𝑑𝑜𝑚 ℎ 𝜎 𝑥 ∶ 𝑎^ = 𝜎gℎ[𝑎] ∶ 𝜎g, 𝑎 ∶ 𝑣] = ℎ′𝑢𝑝𝑑𝑎𝑡𝑒 𝑎], ℎ, 𝑥, 𝑣, ℎg
InferenceRules:BlockScope
BlockScope{𝑆∗} :
SequentialComposition𝑆∗:
𝜎 = 𝜌 ∶ 𝑎] ¬ 𝑎l ∈ 𝑑𝑜𝑚 ℎ ℎ 𝑎l ∶ 𝜎 = ℎg 𝑎l,ℎ′, 𝑆∗ → ℎg′𝑎], ℎ, 𝑏𝑠𝑆∗ → ℎgg
𝑎, ℎ, [] → ℎ
𝑎, ℎ, 𝑆 → ℎ′ 𝑎, ℎg, 𝑆\ → ℎ′′𝑎, 𝑆 ∷ 𝑆\ → ℎ′′
ExtendingIMPwithClosures
• Syntax:createandcallclosures
• Representation:closuresarevaluesandframesalong-lived
• Semantics:creatingandcalling closures
Declarationvar𝑥=fun(𝑥){𝑆∗}:
• Executingaclosure:captureframepointerandstoreclosureinheap
InferenceRules:ClosureCreation(Declaration)
ℎ 𝑎] = 𝜎 ¬ 𝑥 ∈ 𝑑𝑜𝑚 𝜎
¬(𝑎^ ∈ 𝑑𝑜𝑚 ℎ ) ℎ 𝑎]: 𝜎g, 𝑎^: 𝑓 = ℎ′
𝑎], ℎ, var𝑥=fun(𝑥o){𝑆∗} → ℎ′
𝜎 𝑥 ∶ 𝑎^ = 𝜎g
𝑎], 𝑥o, 𝑆∗ = 𝑓
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}}
InferenceRules:ClosureExecution
ClosureCall:𝑥(𝑒)
𝑎], ℎ, 𝑥 → 𝑎p, 𝑥o, 𝑆∗
𝑎], ℎ, 𝑒 → 𝑣
¬(𝑎l∈ 𝑑𝑜𝑚(ℎ)) 𝜎 = {𝜌 ∶ 𝑎p, 𝑥o ∶ 𝑣}
ℎ′ = ℎ[𝑎l: 𝜎]
xEvaluatestoaclosure
Eevaluatestoavalue
Createanewframe
Evaluatebodyofclosure𝑎l, ℎg, 𝑆∗ → ℎ′′
𝑎], ℎ, 𝑥(𝑒) → ℎ′′
TheEnd