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

Post on 11-Jul-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

6.035:SemanticsHeapsandTypes

Whatis“Semantics”?

• Goal:Provideaspecification ofthebehaviorofaprogram

• Precise,Complete,Unambiguous

x = 1 + 2 * 3;

IMP:asimpleimperativelanguage

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

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

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

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

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

SemanticNotation

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe,thetermevaluatestoaresult

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

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

ExtensionsforToday

• Heaps

• Types

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

• Aframecapturesthebindingoflocalvariablestovalues

Frames

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

𝝈, 𝐸 → 𝑛

• 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

EvaluationRelations

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe,thetermevaluatestoaresult

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

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

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

Expressions:InferenceRules

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

𝑛 = 𝑛]𝜎, 𝑛 → 𝑛]

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

𝜎 𝑥 = 𝑛]𝜎, 𝑥 → 𝑛]

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

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

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

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 → 𝑏]

Statements:InferenceRules

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

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

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

ExtensionsforToday

• Heaps

• Types

AddBooleanValues

if (x == 1){

y = 2;}

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

y = 2;}

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

𝘹

AddBooleans:Grammar

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

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

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

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

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

AddBooleans:Grammar

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

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

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

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

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

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

AddBooleans:FramesandHeaps

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

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

AddBooleans:FramesandHeaps

• Domainofaddresses:𝐴• Locationofanintegervalueinmemory

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

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

AddBooleans:FramesandHeaps

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

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

EvaluationRelations(withheaps)

• Definethesemanticsofeachterminourlanguage(E,B,andS)withanevaluationrelation:

• Meaning:givenaframe andheap,thetermevaluatestoaresult

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

Expressions:InferenceRules

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

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

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

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

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

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

TypeChecking• Remembertherubyexample

• Needtodefinethebehavioreachoperationforeachinputtype

test1.rb:_____________________________

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

test2.rb:_____________________________

print2.2*ARGV[0]

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

Expressions:InferenceRules

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

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

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

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

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

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

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

Booleans:InferenceRules

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

Statements:InferenceRules

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

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

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

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

TheEnd

top related