ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of...

9
CSE 230: Winter 2010 CSE 230: Winter 2010 Principles of Principles of Programming Languages Lecture 2: Bi t O ti l S ti R jit Jh l Big-step Operational Semantics Ranjit Jhala UC San Diego Administrative Factoids Administrative Factoids Make-Up Lectures Make-Up Lectures Fri Jan 8, 12:30 – 3:00pm (Tomorrow!) M J 25 12 30 3 00 Mon Jan 25, 12:30 3:00pm Location: CSE 1202 ARITH: A Simple Expression Language syntax syntax and operational semantics ARITH: Arithmetic Expressions Concrete syntax ARITH: Arithmetic Expressions Rules to express programs as strings of characters relevant to readability, familiarity, etc. Issues and choices Should semicolon separate or terminate statements ? How should comments be indicated ? “Solved Problem” Finite automata and context-free grammars. Automatic parser generators Irrelevant to 230

Transcript of ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of...

Page 1: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

CSE 230: Winter 2010CSE 230: Winter 2010

Principles of Principles of Programming Languages

Lecture 2: Bi t O ti l S ti

R jit Jh l

Big-step Operational Semantics

Ranjit JhalaUC San Diego

Administrative FactoidsAdministrative Factoids

• Make-Up Lectures • Make-Up Lectures – Fri Jan 8, 12:30 – 3:00pm (Tomorrow!)

M J 25 12 30 3 00– Mon Jan 25, 12:30 – 3:00pm

• Location: CSE 1202

ARITH: A Simple Expression Language

syntax syntax and

operational semantics

ARITH: Arithmetic Expressions

Concrete syntax

ARITH: Arithmetic Expressions

y• Rules to express programs as strings of characters

– relevant to readability, familiarity, etc.

• Issues and choices– Should semicolon separate or terminate statements ?– How should comments be indicated ?

• “Solved Problem”– Finite automata and context-free grammars.– Automatic parser generators

Irrelevant to 230

Page 2: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Programs = Abstract Syntax TreesPrograms Abstract Syntax Trees

AST is a parse treep– Amenable to formal, algorithmic manipulation– Fairly independent of concrete syntaxFairly independent of concrete syntax

Abstract syntax for ARITHAbstract syntax for ARITHe ::= n integer literals

|| e1 + e2 sum

| e1 * e2 product

Analysis of ARITHAnalysis of ARITH

Questions to answer:

– What is the meaning of an ARITH expression?

– How would we evaluate an ARITH expression?

– How are the evaluator and the meaning related?

An Operational SemanticsAn Operational Semantics

• Specifies how to evaluate expressionsSpecifies how to evaluate expressions• Defined by cases on the form of expressions:

– n evaluates to n• n is a normal form, no need to evaluate further

– e1 + e2 evaluates to n if• e1 evaluates to n1

• e2 evaluates to n2

• and n is the sum of n1 and n21 2

– e1 * e2 evaluates to n if• e1 evaluates to n1

l t t • e2 evaluates to n2

• and n is the product of n1 and n2

Another FormulationAnother Formulation• Notation: e ⇓ n means that e evaluates to n

– Judgment: stmt about relation between e and n

• Allows us to write evaluation rules concisely⇓– n ⇓ n

– e1 + e2 ⇓ n if•e ⇓ n•e1 ⇓ n1

•e2 ⇓ n2

• and n is the sum of n1 and n2and n is the sum of n1 and n2

– e1 * e2 ⇓ n if•e1 ⇓ n1

•e2 ⇓ n2

• and n is the product of n1 and n2

Page 3: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Operational Semantics as Inference RulesOperational Semantics as Inference Rulesn ⇓ n

e1+e2 ⇓ n

e1 ⇓ n1 e2 ⇓ n2 n is the sum of n1 and n2

e1⇓ n1 e2 ⇓ n2 n is the product of n1 and n2

R l “ b th li ” i li “b l th li ”

e1*e2 ⇓ n

• Rule: “above the line” implies “below the line”• Evaluation rules for big-step op. semantics

D i ti l f th j d t ⇓• Derivation rules for the judgement e⇓ n

How to Read the Rules?How to Read the Rules?

Ans 1: Forward as inference rules:If hypothesis judgments (“numerator”) hold then conclusion judgment (“denominator”) holds

e1+e2 ⇓ n

e1⇓n1 e2⇓n2 n is the sum of n1 and n2

e.g.,If we know that e1 ⇓ 5 and e2 ⇓ 7,then we can infer that e + e ⇓ 12then we can infer that e1 + e2 ⇓ 12

No “numerator” ? ⇓ ⇓

No numerator ? Denominator is a fact

n ⇓ n 5 ⇓ 5

How to Read the Rules?How to Read the Rules?Ans 2: Backward, as evaluation rules:Evaluating : find n s t ⇓ n is derivable from rulesEvaluating e : find n s.t. e⇓ n is derivable from rules

e.g. how to evaluate e1*e2 ? g 1 2

Find n s.t. e1*e2⇓n is derivable• Last step in derivation must be multiplication rule:p p

(Conclusions of other rules do not match)• Recursively find n1 and n2 s.t. e1 ⇓ n1 and e2 ⇓ n2 derivable

– “Stitch” derivations together with product rule

⇓ ? ⇓ ? i th d t f d

D1 D2

e1*e2 ⇓ ?

e1⇓ ? e2⇓ ?n1 n is the product of n1 and n2n2

n

How to Read the Rules?How to Read the Rules?

Ans 2: Backward, as evaluation rules:,Evaluating e : find n s.t. e⇓ n is derivable from rules

Rules are syntax-directed: • exactly one rule for each kind of expression

At each step at most one rule applies• Simple evaluation procedure • Simple evaluation procedure

Called reasoning by inversion on the derivation rulesCalled reasoning by inversion on the derivation rules

Page 4: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Ex: evaluate (3+5)*(4+2)Ex: evaluate (3+5) (4+2)

3⇓? 5⇓? 4⇓? 2⇓?3 5 4 2

(3+5)*(4+2)⇓ ?

(3+5)⇓ ? (4+2)⇓ ?8 6

48(3+5) (4+2)⇓ ?48

ARITH Op Semantics in OcamlARITH Op. Semantics in Ocamltype aexp =

| Int of int | f *| Sum of aexp * aexp| Prod of aexp * aexp;;

let anexp = Prod(Sum(Int 3,Int 5),Sum(Int 4, Int 2));;

let rec eval e =match e with| Int n -> n|| Sum (e1,e2) ->

let n1 = eval(e1) inlet n2 = eval(e2) in

n1 + n2n1 + n2| Prod (e1,e2) ->

let n1 = eval(e1) inlet n2 = eval(e2) in

1 * 2n1 * n2;;

eval anexp;;

Semantic Properties: UniquenessSemantic Properties: Uniqueness

ARITH is a deterministic language: all e prs eval ate to at most one val eall exprs evaluate to at most one value

’ ⇓ ⇓ ’ ’∀e. ∀n. ∀n’ . e ⇓ n ∧ e ⇓ n’ ⇒ n = n’

Q: How would we prove such a theorem ?

IMP: An Imperative Language

syntax syntax and

operational semantics

Page 5: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

IMP Syntactic EntitiesIMP Syntactic Entities

• Int integer literals nInt integer literals n

• Bool booleans {true,false}

• Loc locations x,y,z,…( i bl i bl )(assignable variables)

• Aexp arithmetic expressions e

• Bexp boolean expressions b

• Comm commands c

Abstract Syntax: Arith Expressions (Aexp)Abstract Syntax: Arith Expressions (Aexp)

e ::= n for n ∈ Int| x for x ∈ Loc| e1 + e2 for e1, e2 ∈ Aexp f A| e1 - e2 for e1, e2 ∈ Aexp

| e1 * e2 for e1, e2 ∈ Aexp

Note:• Variables are not declared• All variables have integer type• There are no side-effects

Abstract Syntax: Bool Expressions (Bexp)

true ::= true

Abstract Syntax: Bool Expressions (Bexp)

true :: true| false| f A| e1 = e2 for e1,e2 ∈ Aexp| e1 < e2 for e1,e2 ∈ Aexp| ¬ b for b ∈ Bexp| b1 Ç b2 for e1, e2 ∈ Bexp| b1 Ç b2 for e1, e2 ∈ Bexp| b1 ∧ b2 for e1, e2 ∈ Bexp

Abstract Syntax: Commands (Comm)Abstract Syntax: Commands (Comm)c ::= skip

| x:= e for x ∈ L & e ∈ Aexp| x:= e for x ∈ L & e ∈ Aexp| c1;c2 for c1,c2 ∈ Comm| if b then c1 else c2 for b ∈Bexp & c1,c2∈ Comm| b t e c1 e se c2 b p c1,c2 C| while b do c for c ∈ Comm & b ∈ Bexp

Note:• Typing rules embedded in syntax definition

– Other checks may not be context-free– need to be specified separately (e.g., vars are declared)

Commands contain all the side effects in the language• Commands contain all the side-effects in the language• (Missing: pointers, function calls, exceptions ….)

Page 6: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Semantics of IMP: StatesSemantics of IMP: States

• Meaning of IMP expressions depends on • Meaning of IMP expressions depends on the values of variables

• A state σ is a function from Loc to Int– Value of variables at a given moment– Set of all states is Σ = Loc → Int

Operational Semantics of IMPOperational Semantics of IMP

Evaluation judgment for expressions: j g p• Ternary relation on expression, a state, and a value:• We write: <e, σ> ⇓ n

“E pression in state e al ates to n”“Expression e in state σ evaluates to n”

Q: Why no state on the right ?Q: W y o state o t e g t ?– Evaluation of expressions has no side-effects:– i.e., state unchanged by evaluating an expression

Q: Can we view judgment as a function of 2 args e, σ ? – Only if there is a unique derivation … y q

Operational Semantics of IMPOperational Semantics of IMP

Evaluation judgement for commandsj g• Ternary relation on expression, state, and a new state• We write: <c, σ> ⇓ σ’

“Executing cmd c from state σ takes system into state σ’ ”

• Evaluation of a command has effect– but no direct value– So, “result” of a command is a new state σ’

Note: evaluation of a command may not terminate

Q: Can we view judgment as a function of 2 args e, σ ? – Only if there is a unique successor state …

Evaluation Rules (for Aexp)Evaluation Rules (for Aexp)

<n, σ> ⇓ n <x, σ> ⇓ σ(x)

<e + e σ> ⇓ n + n

<e1, σ> ⇓ n1 <e2, σ> ⇓ n2

<e e σ> ⇓ n n

<e1, σ> ⇓ n1 <e2, σ> ⇓ n2

<e1 + e2, σ> ⇓ n1 + n2 <e1 - e2, σ> ⇓ n1 - n2

<e * e σ> ⇓ n * n

<e1, σ> ⇓ n1 <e2, σ> ⇓ n2

<e1 * e2, σ> ⇓ n1 n2

Page 7: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Evaluation Rules (for Bexp)Evaluation Rules (for Bexp)<true, σ> ⇓ true <false, σ> ⇓ false

<e1, σ>⇓n1 <e2, σ>⇓n2 p is n1=n2 <e1, σ>⇓n1 <e2, σ>⇓n2 p is n1<n2

<e1 = e2, σ> ⇓ p <e1 < e2, σ> ⇓ p

<b1, σ> ⇓ p1 <b2, σ> ⇓ p2

<b1 Æ b2, σ> ⇓ p1Æ p2

<b1, σ> ⇓ p1 <b2, σ> ⇓ p2

<b1 Ç b2, σ> ⇓ p1Ç p2

<b, σ> ⇓ p

b ⇓< ¬b, σ> ⇓ ¬ p

Evaluation Rules (for Comm)Evaluation Rules (for Comm)

<skip σ> ⇓ σ<skip , σ> ⇓ σ

⇓ ⇓<c1, σ> ⇓ σ’ <c2, σ’> ⇓ σ’’

<c1;c2, σ> ⇓ σ’’1 2

Define σ[xa n] as:σ[xa n](x) = n

[ ]( ) ( )

<e, σ> ⇓ n<x := e, σ> ⇓ σ[xa n]

σ[xa n](y) = σ(y), [ ]

Evaluation Rules (for Comm)Evaluation Rules (for Comm)

<b σ> ⇓ true <c σ> ⇓ σ’<b, σ> ⇓ true <c1, σ> ⇓ σ’

<if b then c1 else c2, σ> ⇓ σ’

<b, σ> ⇓ false <c2, σ> ⇓ σ’

<if b then c else c σ> ⇓ σ’<if b then c1 else c2, σ> ⇓ σ

Page 8: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Evaluation Rules (for Comm)Evaluation Rules (for Comm)

<b σ> ⇓ false<b, σ> ⇓ false

<while b do c, σ> ⇓ σ

<b, σ> ⇓ true <c;while b do c, σ> ⇓ σ’⇓<while b do c, σ > ⇓ σ’

Evaluation of CommandsEvaluation of Commands

Order of evaluation is important and stipulated:Order of evaluation is important and stipulated:

• c1 is evaluated before c2 in c1;c2c1 is evaluated before c2 in c1;c2

• c not evaluated in if true then c else c• c2 not evaluated in if true then c1 else c2

• c is not evaluated in while false do c• c is not evaluated in while false do c

Evaluation of CommandsEvaluation of Commands

Evaluation rules are not syntax-directedy• Multi rules for conditional constructs if , while• Only one can be applied at one timeOnly one can be applied at one time

– Depends on value of condition (semantics-directed)

Evaluate from the zero-initialized store:while x<5 do x := x + 1

• i.e. Given σ maps all vars to 0, find σ ’ s.t.– <while x<5 do x:=x+1 σ > ⇓ σ ’<while x<5 do x:=x+1 , σ > ⇓ σ

Page 9: ARITH: Arithmetic Expressionscseweb.ucsd.edu/classes/wi10/cse230/lectures/lec2.pdf · Meaning of IMP expressions depends on the values of variables A state V is a function from Loc

Homework Homework

• Implement op semantics in OcamlImplement op semantics in Ocaml– IMP + exceptions

• Some guidelines– types for locations,AExp, BExp, Commtypes for locations,AExp, BExp, Comm– stores are ML map from vars to integers

• Documentation on Web page

Strongly recommend you do it asap

Homework SkeletonHomework Skeletontype loc = string;;type state = n LocMap.t;;

let lookup sigma x = try LocMap.find x sigma with Not_found -> 0

let update (sigma:state) (x:loc) (v:n) : state = LocMap.add x n sigma;;

type aexp = | Int of int | Loc of variable| ...;;

type bexp = ...;;type comm = ...;;

let rec eval_aexp (e:aexp) (sigma:state) = ...l t l b (b b ) ( i t t )let rec eval_bexp (b:bexp) (sigma:state) = ...let rec eval_comm (c:comm) (sigma:state) = ...

let c = While(Leq(Loc "x",Int 5), Asgn("x",Sum(Loc "x",Int 1)));;let sigma0 = initial state () ;;_let sigma = eval_comm c sigma0 ;;lookup sigma "z";;lookup sigma "x";;