6.035: semantics6.035.scripts.mit.edu/sp17/slides/s17-lecture-06.pdf•define the semantics of each...

28
6.035: Semantics Heaps and Types

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-06.pdf•Define the semantics of each term in our language (E, B, and S) with an evaluation relation : •Meaning: given

6.035:SemanticsHeapsandTypes

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

Whatis“Semantics”?

• Goal:Provideaspecification ofthebehaviorofaprogram

• Precise,Complete,Unambiguous

x = 1 + 2 * 3;

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

IMP:asimpleimperativelanguage

𝑆 → 𝑥=𝐸∣ if (BE) {𝑆} else {𝑆}∣ 𝑆∗∣ while (BE) { 𝑆}

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

𝐵𝐸 → 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒∣ 𝐸 < 𝐸 ∣ 𝐸 == 𝐸

∣ ! 𝐵𝐸∣ 𝐵𝐸&&𝐵𝐸 ∣ 𝐵𝐸 ∣∣ 𝐵𝐸

(assignment)(if-then-else)(sequentialcomposition)(loop)

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

SemanticNotation

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe,thetermevaluatestoaresult

𝝈, 𝒔 → 𝝈’𝝈, 𝒆 → 𝒏 𝝈, 𝒃𝒆 → 𝒃

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

Statements:InferenceRules

• 𝑆 → x = E ∣ if (BE) { S } else { S } ∣ 𝑆∗ ∣ while (BE) { s }

• x = E

• if (BE) { S } else { S }

𝜎, 𝑒 → 𝑛𝜎 𝑥 ∶ 𝑛 = 𝜎′𝜎, 𝑥 = 𝑒 → 𝜎E

𝜎, 𝑏𝑒 → 𝑇𝑟𝑢𝑒 𝜎, 𝑆G → 𝜎′𝜎, 𝑖𝑓 𝑏𝑒 𝑆G𝑆J → 𝜎E

𝜎, 𝑏𝑒 → 𝐹𝑎𝑙𝑠𝑒 𝜎, 𝑆J → 𝜎′𝜎, 𝑖𝑓 𝑏𝑒 𝑆G𝑆J → 𝜎E

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

ExtensionsforToday

• Heaps

• Types

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

• Domainofframes:𝜎 ∈ Σ = 𝑋 → ℕ• Aframe 𝜎 isanelementofthedomainΣ,whichissetofallfunctionsthatmapavariable𝑥fromthedomainofallvariables𝑋toaninteger

• Aframecapturesthebindingoflocalvariablestovalues

Frames

𝜎 = 𝑥 ∶ 1, 𝑦 ∶ 2, 𝑧 ∶ 3 x = 1;y = 2;z = 3;

𝝈, 𝐸 → 𝑛

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

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

• Domainofheaps:ℎ ∈ 𝐻 = 𝐴 → ℕ• A heapℎ isanelementofthedomainH,whichissetofall(partial)functionsthatmapanaddresstoaninteger

FramesandHeaps

𝜎 = 𝑥 ∶ 100, 𝑦 ∶ 101, 𝑧 ∶ 102

x = 1;y = 2;z = 3;

ℎ = 100 ∶ 1, 101: 2, 102 ∶ 3

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

EvaluationRelations

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe,thetermevaluatestoaresult

𝝈, 𝒔 → 𝝈′𝝈, 𝒆 → 𝒏 𝝈, 𝒃𝒆 → 𝒃

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

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

𝝈, 𝒉, 𝒔 → (𝝈′, 𝒉′)𝝈, 𝒉, 𝒆 → 𝒏 𝝈, 𝒉, 𝒃𝒆 → 𝒃

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

Expressions:InferenceRules

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

𝑛 = 𝑛]𝜎, 𝑛 → 𝑛]

𝜎, 𝑒 → 𝑛 − 𝑛 = 𝑛]𝜎, −𝑒 → 𝑛]

𝜎 𝑥 = 𝑛]𝜎, 𝑥 → 𝑛]

𝑛 = 𝑛]𝜎, ℎ, 𝑛 → 𝑛]

𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑛]𝜎, ℎ, 𝑥 → 𝑛]

𝜎, ℎ, 𝑒 → 𝑛 − 𝑛 = 𝑛]𝜎, ℎ, −𝑒 → 𝑛]

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

BooleanExpressions:InferenceRules

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

𝜎, ℎ, 𝑇𝑟𝑢𝑒 → 𝑇𝑟𝑢𝑒

𝜎, ℎ, 𝑒G → 𝑛G 𝜎, ℎ, 𝑒J → 𝑛J𝑙𝑡(𝑛G, 𝑛J) = 𝑏]𝜎, ℎ, 𝑒G < 𝑒J → 𝑏]

𝜎, ℎ, 𝑒G → 𝑛G 𝜎, ℎ, 𝑒J → 𝑛J𝑒𝑞 𝑛G, 𝑛J = 𝑏]𝜎, ℎ, 𝑒G == 𝑒J → 𝑏]

𝜎, ℎ, 𝑏𝑒 → 𝑏𝑛𝑜𝑡(𝑏) = 𝑏]𝜎, ℎ, ! 𝑏𝑒 → 𝑏]

𝜎, ℎ, 𝐹𝑎𝑙𝑠𝑒 → 𝐹𝑎𝑙𝑠𝑒

𝜎, ℎ, 𝑏𝑒G → 𝑏G 𝜎, ℎ, 𝑏𝑒J → 𝑏J𝑎𝑛𝑑(𝑏G, 𝑏J) = 𝑏]𝜎, ℎ, 𝑏𝑒G&&𝑏𝑒J → 𝑏]

𝜎, ℎ, 𝑏𝑒G → 𝑏G 𝜎, ℎ, 𝑏𝑒J → 𝑏J𝑜𝑟(𝑏G, 𝑏J) = 𝑏]𝜎, ℎ, 𝑏𝑒G ∣∣ 𝑏𝑒J → 𝑏]

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

Statements:InferenceRules

𝑆 → x = E ∣ if (BE) { S } else { S } ∣ 𝑆∗ ∣ while (BE) { s }

• x = E 𝜎, 𝑒 → 𝑛𝜎 𝑥 ∶ 𝑛 = 𝜎′𝜎, 𝑥 = 𝑒 → 𝜎E

𝜎, ℎ, 𝑒 → 𝑛𝜎 𝑥 ∶ 𝑎 = 𝜎Eℎ 𝑎 ∶ 𝑛 = ℎE¬(𝑎 ∈ 𝑑𝑜𝑚 ℎ )𝜎, ℎ, 𝑥 = 𝑒 → (𝜎E, ℎ′)

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

ExtensionsforToday

• Heaps

• Types

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

AddBooleanValues

if (x == 1){

y = 2;}

b = x == 1;if (b){

y = 2;}

• Whatneedtochange?x = Truey = 1z = x + y

𝘹

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

AddBooleans:Grammar

𝑆 → 𝑥=𝐸∣ if (BE) {𝑆} else {𝑆}∣ 𝑆∗∣ while (BE) { 𝑆}

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

𝐵𝐸 → 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒∣ 𝐸 < 𝐸 ∣ 𝐸 == 𝐸

∣ ! 𝐵𝐸∣ 𝐵𝐸&&𝐵𝐸 ∣ 𝐵𝐸 ∣∣ 𝐵𝐸

(assignment)(if-then-else)(sequentialcomposition)(loop)

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

AddBooleans:Grammar

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

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

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

(assignment)(if-then-else)(sequentialcomposition)(loop)

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

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

• Domainofheaps:ℎ ∈ 𝐻 = 𝐴 → ℕ• A heapℎ isanelementofthedomainH,whichissetofall(partial)functionsthatmapanaddresstoaninteger

AddBooleans:FramesandHeaps

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

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

• Domainofheaps:ℎ ∈ 𝐻 = 𝐴 → ℕ ∪ 𝔹• A heapℎ isanelementofthedomainH,whichissetofall(partial)functionsthatmapanaddresstoanintegerorboolean

AddBooleans:FramesandHeaps

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

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

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

AddBooleans:FramesandHeaps

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

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

𝝈, 𝒉, 𝒔 → (𝝈, 𝒉)𝝈, 𝒉, 𝒆 → 𝒏 𝝈, 𝒉, 𝒃𝒆 → 𝒃

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

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

𝝈, 𝒉, 𝒔 → (𝝈, 𝒉)𝝈, 𝒉, 𝒃𝒆 → 𝒃𝝈, 𝒉, 𝒆 → 𝒗

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

Expressions:InferenceRules

𝐸 → 𝑛 ∣ 𝑥 ∣ −𝐸 ∣ 𝐸 + 𝐸 ∣ 𝐸– 𝐸 ∣ 𝐸 ∗ 𝐸 ∣ 𝐸/𝐸 ∣ 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒|𝐸 < 𝐸 𝐸 == 𝐸 ! 𝐸 ∣ 𝐸&&𝐸 ∣ 𝐸 ∣∣ 𝐸

𝑛 = 𝑛]𝜎, ℎ, 𝑛 → 𝑛]

𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣]𝜎, ℎ, 𝑥 → 𝑣]

𝑛 = 𝑛]𝜎, ℎ, 𝑛 → 𝑛]

𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑛]𝜎, ℎ, 𝑥 → 𝑛]

𝜎, ℎ, 𝑒 → 𝑛 − 𝑛 = 𝑛]𝜎, ℎ, −𝑒 → 𝑛]

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

TypeChecking• Remembertherubyexample

• Needtodefinethebehavioreachoperationforeachinputtype

test1.rb:_____________________________

printARGV[0]*2.2>rubytest1.rb100>100100

test2.rb:_____________________________

print2.2*ARGV[0]

>rubytest2.rb100>*':Stringcan'tbecoercedintoFloat(TypeError)

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

Expressions:InferenceRules

𝐸 → 𝑛 ∣ 𝑥 ∣ −𝐸 ∣ 𝐸 + 𝐸 ∣ 𝐸– 𝐸 ∣ 𝐸 ∗ 𝐸 ∣ 𝐸/𝐸 ∣ 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒|𝐸 < 𝐸 𝐸 == 𝐸 ! 𝐸 ∣ 𝐸&&𝐸 ∣ 𝐸 ∣∣ 𝐸

𝑛 = 𝑛]𝜎, ℎ, 𝑛 → 𝑛]

𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑣]𝜎, ℎ, 𝑥 → 𝑣]

𝑛 = 𝑛]𝜎, ℎ, 𝑛 → 𝑛]

𝜎 𝑥 = 𝑎ℎ(𝑎) = 𝑛]𝜎, ℎ, 𝑥 → 𝑛]

𝜎, ℎ, 𝑒 → 𝑛 − 𝑛 = 𝑛]𝜎, ℎ, −𝑒 → 𝑛]

𝜎, ℎ, 𝑒 → 𝑣𝑡𝑦𝑝𝑒 𝑣 = 𝑖𝑛𝑡 − 𝑖𝑛𝑡(𝑣) = 𝑛]𝜎, ℎ, −𝑒 → 𝑛]

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

Booleans:InferenceRules

• 𝐸 → 𝑛 ∣ 𝑥 ∣ −𝐸 ∣ 𝐸 + 𝐸 ∣ 𝐸– 𝐸 ∣ 𝐸 ∗ 𝐸 ∣ 𝐸/𝐸• ∣ 𝑇𝑟𝑢𝑒 ∣ 𝐹𝑎𝑙𝑠𝑒|𝐸 < 𝐸 𝐸 == 𝐸 ! 𝐸 ∣ 𝐸&&𝐸 ∣ 𝐸 ∣∣ 𝐸

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

Statements:InferenceRules

𝑆 → x = E ∣ if (E) { S } else { S } ∣ 𝑆∗ ∣ while (E) { s }

x = E 𝜎, ℎ, 𝑒 → 𝑛𝑓𝑟𝑒𝑠ℎ 𝑎, ℎ 𝜎 𝑥 ∶ 𝑎 = 𝜎Eℎ 𝑎 ∶ 𝑛 = ℎ′

𝜎, ℎ, 𝑥 = 𝑒 → (𝜎E, ℎ′)

𝜎, ℎ, 𝑒 → 𝑣𝑓𝑟𝑒𝑠ℎ 𝑎, ℎ 𝜎 𝑥 ∶ 𝑎 = 𝜎Eℎ 𝑎 ∶ 𝑣 = ℎ′𝜎, ℎ, 𝑥 = 𝑒 → (𝜎E, ℎ′)

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

TheEnd