Docsity

Operational Semantics for CS415: Understanding Program Meaning - Prof. Westley Weimer, Study notes of Programming Languages

Toggle sidebar
bg1
CS415 — Discussion Section Notes 7
Claire Le Goues
March 20, 2008
Operational Semantics
So far, we’ve seen two types of judgments:
(1)
O`e0:T
T≤T0
O[T0/x]`e1:T1
O`let x:T0←e0in e1:T1
(2) W`T:type
W`PhTi:type
We’ve been using these rules to prove properties of programs, e.g. “this piece of code
has type T1” or just “this type is valid.”
Guiding question: how did these individual rules help us type check entire programs?
Ok. Now we’re interested in saying things about the meaning of programs. So instead
of “: SomeTypeName” we’ll say stuff like “5 + 7 : 12, S .” We’ve already ruled out
syntax and type errors, so we can assume any program we see is ‘legal.’
Another guiding question: how do the new rules help us? Why all this theory, anyway?
Let’s check out some opsem judgments:
sis a string literal
nis the length of s
so, E, S `s:String(n, s), S so, E, S `self :so, S
Flashback: functional programming avoids using assignment, and we try to code with-
out side-effects. But, there are side-effects in Cool. How do we handle them in these
operational semantics rules?
1
pf3

Partial preview of the text

Download Operational Semantics for CS415: Understanding Program Meaning - Prof. Westley Weimer and more Study notes Programming Languages in PDF only on Docsity!

CS415 — Discussion Section Notes 7

Claire Le Goues

March 20, 2008

Operational Semantics

So far, we’ve seen two types of judgments:

O e 0 : T T ≤ T 0 O[T 0 /x] e 1 : T 1 O ` let x : T 0 ← e 0 in e 1 : T 1

W T : type W P 〈T 〉 : type

We’ve been using these rules to prove properties of programs, e.g. “this piece of code has type T 1 ” or just “this type is valid.”

Guiding question: how did these individual rules help us type check entire programs?

Ok. Now we’re interested in saying things about the meaning of programs. So instead of “: SomeTypeName” we’ll say stuff like “5 + 7 : 12, S.” We’ve already ruled out syntax and type errors, so we can assume any program we see is ‘legal.’

Another guiding question: how do the new rules help us? Why all this theory, anyway?

Let’s check out some opsem judgments:

s is a string literal n is the length of s so, E, S s : String(n, s), S so, E, S self : so, S

Flashback: functional programming avoids using assignment, and we try to code with- out side-effects. But, there are side-effects in Cool. How do we handle them in these operational semantics rules?

Or, what’s wrong with the following rules:

so, E, S e : v, S S 1 = S[v/E(id)] so, E, S id ← e : v, S 1

so, E, S e 1 : Bool(f alse), S so, E, S while e 1 loop e 2 pool : void, S

REMEMBER SIDE EFFECTS

Consider function calls—when do we evaluate the parameters?

so = X(.. .)

T 0 =

X(.. .) if T = SELF T Y P E T otherwise class(T 0 ) = (a 1 : T 1 ← e 1 ,... , an : Tn ← en) li = newloc(S) for i = 1,... , n v = T 0 (a 1 = l 1 ,... , an = ln) E′^ = ∅[a 1 : l 1 ,... , an : ln] S 1 = S[DT 1 /l 1 ,... , DTn /ln] v, E′, S 1 {a 1 ← e 1 ;... ; an ← en; } : vn, S 2 so, E, S new T : v, S 2

This defines exactly what needs to happen on a new call. What is the output for the following piece of code?

Listing 1: init order for COOL

class A { | e3 : String <- | class Main inherits IO { { e1 <- "Bye\n"; e2; }; | main() : Object { e1 : String; | let a : A <- new A in { e2 : String <- e1; | out_string(a.gete3()); | out_string(a.gete2()); gete2() : String { e2 }; | } gete3() : String { e3 }; | }; }; | };