cs345h: programming languages lecture 8: operational ...tdillig/cs345h/lecture8.pdfthomas dillig,...

108
CS345H: Programming Languages Lecture 8: Operational Semantics II Thomas Dillig Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 1/29

Upload: others

Post on 22-Jan-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

CS345H: Programming Languages

Lecture 8: Operational Semantics II

Thomas Dillig

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 1/29

Page 2: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Outline

I We will discuss semantics of remining (interesting) Lexpressions

I Will look at one more formalism for specifying meaning today

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 2/29

Page 3: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Outline

I We will discuss semantics of remining (interesting) Lexpressions

I Will look at one more formalism for specifying meaning today

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 2/29

Page 4: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Back to Operational Semantics

I We are still missing semantics for key constructs in the Lprogramming language

I Let’s start with the if expression: if e1 then e2 else e3.

I Recall meaning: If e1 evaluates to a non-zero integer, themeaning of the expression is e2, otherwise e3

I Any ideas on how to write this as an operational semanticsrule?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 3/29

Page 5: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Back to Operational Semantics

I We are still missing semantics for key constructs in the Lprogramming language

I Let’s start with the if expression: if e1 then e2 else e3.

I Recall meaning: If e1 evaluates to a non-zero integer, themeaning of the expression is e2, otherwise e3

I Any ideas on how to write this as an operational semanticsrule?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 3/29

Page 6: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Back to Operational Semantics

I We are still missing semantics for key constructs in the Lprogramming language

I Let’s start with the if expression: if e1 then e2 else e3.

I Recall meaning: If e1 evaluates to a non-zero integer, themeaning of the expression is e2, otherwise e3

I Any ideas on how to write this as an operational semanticsrule?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 3/29

Page 7: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Back to Operational Semantics

I We are still missing semantics for key constructs in the Lprogramming language

I Let’s start with the if expression: if e1 then e2 else e3.

I Recall meaning: If e1 evaluates to a non-zero integer, themeaning of the expression is e2, otherwise e3

I Any ideas on how to write this as an operational semanticsrule?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 3/29

Page 8: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals

I Difficulty: What happens depends on whether e1 evaluates to0 or not.

I Solution: Write two rules, one for the case where e1 evaluatesto 0 and one for the case whenre e1 evaluates to a non-zerointeger.

I What if e1 evaluates to 0?

E ` e1 : 0E ` e3 : e

E ` if e1 then e2 else e3 : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 4/29

Page 9: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals

I Difficulty: What happens depends on whether e1 evaluates to0 or not.

I Solution: Write two rules, one for the case where e1 evaluatesto 0 and one for the case whenre e1 evaluates to a non-zerointeger.

I What if e1 evaluates to 0?

E ` e1 : 0E ` e3 : e

E ` if e1 then e2 else e3 : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 4/29

Page 10: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals

I Difficulty: What happens depends on whether e1 evaluates to0 or not.

I Solution: Write two rules, one for the case where e1 evaluatesto 0 and one for the case whenre e1 evaluates to a non-zerointeger.

I What if e1 evaluates to 0?

E ` e1 : 0E ` e3 : e

E ` if e1 then e2 else e3 : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 4/29

Page 11: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals

I Difficulty: What happens depends on whether e1 evaluates to0 or not.

I Solution: Write two rules, one for the case where e1 evaluatesto 0 and one for the case whenre e1 evaluates to a non-zerointeger.

I What if e1 evaluates to 0?

E ` e1 : 0E ` e3 : e

E ` if e1 then e2 else e3 : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 4/29

Page 12: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals Cont.

I What if e1 evaluates to a non-zero integer?

E ` e1 : non-zero integerE ` e2 : e

E ` if e1 then e2 else e3 : e ′

I Upshot: Can encode choice by giving multiple rules for sameconstruct

I But need to make sure at most one rule can apply at anypoint for deterministic semantics

I Deterministic Semantics: Every program evaluates to at mostone value

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 5/29

Page 13: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals Cont.

I What if e1 evaluates to a non-zero integer?

E ` e1 : non-zero integerE ` e2 : e

E ` if e1 then e2 else e3 : e ′

I Upshot: Can encode choice by giving multiple rules for sameconstruct

I But need to make sure at most one rule can apply at anypoint for deterministic semantics

I Deterministic Semantics: Every program evaluates to at mostone value

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 5/29

Page 14: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals Cont.

I What if e1 evaluates to a non-zero integer?

E ` e1 : non-zero integerE ` e2 : e

E ` if e1 then e2 else e3 : e ′

I Upshot: Can encode choice by giving multiple rules for sameconstruct

I But need to make sure at most one rule can apply at anypoint for deterministic semantics

I Deterministic Semantics: Every program evaluates to at mostone value

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 5/29

Page 15: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals Cont.

I What if e1 evaluates to a non-zero integer?

E ` e1 : non-zero integerE ` e2 : e

E ` if e1 then e2 else e3 : e ′

I Upshot: Can encode choice by giving multiple rules for sameconstruct

I But need to make sure at most one rule can apply at anypoint for deterministic semantics

I Deterministic Semantics: Every program evaluates to at mostone value

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 5/29

Page 16: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Conditionals Cont.

I What if e1 evaluates to a non-zero integer?

E ` e1 : non-zero integerE ` e2 : e

E ` if e1 then e2 else e3 : e ′

I Upshot: Can encode choice by giving multiple rules for sameconstruct

I But need to make sure at most one rule can apply at anypoint for deterministic semantics

I Deterministic Semantics: Every program evaluates to at mostone value

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 5/29

Page 17: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Function Definitions

I Recall: In L, function definitions of the formfun f with x1,...,xn=e in... are equivalent tolet f = lambda x1...lambda xn.e in ...

I To define the meaning of a function definition, we can eitherrepeat the lambda and let binding rules in one rule or rewritethe function definition into let and lambda’s and invoke theexisting rules

I We will do the latter:

E ` let f = lambda x1. . . . lambda xn .e1 in e2 : e

E ` fun f with x1, . . . , xn = e1 in e2 : e

I This only works if there are no circular reductions!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 6/29

Page 18: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Function Definitions

I Recall: In L, function definitions of the formfun f with x1,...,xn=e in... are equivalent tolet f = lambda x1...lambda xn.e in ...

I To define the meaning of a function definition, we can eitherrepeat the lambda and let binding rules in one rule or rewritethe function definition into let and lambda’s and invoke theexisting rules

I We will do the latter:

E ` let f = lambda x1. . . . lambda xn .e1 in e2 : e

E ` fun f with x1, . . . , xn = e1 in e2 : e

I This only works if there are no circular reductions!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 6/29

Page 19: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Function Definitions

I Recall: In L, function definitions of the formfun f with x1,...,xn=e in... are equivalent tolet f = lambda x1...lambda xn.e in ...

I To define the meaning of a function definition, we can eitherrepeat the lambda and let binding rules in one rule or rewritethe function definition into let and lambda’s and invoke theexisting rules

I We will do the latter:

E ` let f = lambda x1. . . . lambda xn .e1 in e2 : e

E ` fun f with x1, . . . , xn = e1 in e2 : e

I This only works if there are no circular reductions!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 6/29

Page 20: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Function Definitions

I Recall: In L, function definitions of the formfun f with x1,...,xn=e in... are equivalent tolet f = lambda x1...lambda xn.e in ...

I To define the meaning of a function definition, we can eitherrepeat the lambda and let binding rules in one rule or rewritethe function definition into let and lambda’s and invoke theexisting rules

I We will do the latter:

E ` let f = lambda x1. . . . lambda xn .e1 in e2 : e

E ` fun f with x1, . . . , xn = e1 in e2 : e

I This only works if there are no circular reductions!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 6/29

Page 21: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I The trick we just used to give meaning to function definitionsis also useful for giving meaning to variable-length expressions.

I Consider the following grammar for a list of integers:

S → [E ]E → int E | int

I Example strings in L(S): [3], [2 3 4], [1 3],...

I Suppose we want to define the meaning of a list of integers astheir sum: How can we write operational semantics for thismini-language?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29

Page 22: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I The trick we just used to give meaning to function definitionsis also useful for giving meaning to variable-length expressions.

I Consider the following grammar for a list of integers:

S → [E ]E → int E | int

I Example strings in L(S): [3], [2 3 4], [1 3],...

I Suppose we want to define the meaning of a list of integers astheir sum: How can we write operational semantics for thismini-language?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29

Page 23: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I The trick we just used to give meaning to function definitionsis also useful for giving meaning to variable-length expressions.

I Consider the following grammar for a list of integers:

S → [E ]E → int E | int

I Example strings in L(S): [3], [2 3 4], [1 3],...

I Suppose we want to define the meaning of a list of integers astheir sum: How can we write operational semantics for thismini-language?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29

Page 24: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I The trick we just used to give meaning to function definitionsis also useful for giving meaning to variable-length expressions.

I Consider the following grammar for a list of integers:

S → [E ]E → int E | int

I Example strings in L(S): [3], [2 3 4], [1 3],...

I Suppose we want to define the meaning of a list of integers astheir sum: How can we write operational semantics for thismini-language?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29

Page 25: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I Observation: Difficulty caused by unknown length of list

I Let’s write operational semantics for a list of length 2:

` [i1 i2] : i1 + i2

I Solution: Think recursively! The sum of a list of k integerscan be obtained by removing the first integer, computing thesum of the remainder and adding the two values

I This translates into two rules: Base case and inductive case

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 8/29

Page 26: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I Observation: Difficulty caused by unknown length of list

I Let’s write operational semantics for a list of length 2:

` [i1 i2] : i1 + i2

I Solution: Think recursively! The sum of a list of k integerscan be obtained by removing the first integer, computing thesum of the remainder and adding the two values

I This translates into two rules: Base case and inductive case

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 8/29

Page 27: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I Observation: Difficulty caused by unknown length of list

I Let’s write operational semantics for a list of length 2:

` [i1 i2] : i1 + i2

I Solution: Think recursively! The sum of a list of k integerscan be obtained by removing the first integer, computing thesum of the remainder and adding the two values

I This translates into two rules: Base case and inductive case

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 8/29

Page 28: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I Observation: Difficulty caused by unknown length of list

I Let’s write operational semantics for a list of length 2:

` [i1 i2] : i1 + i2

I Solution: Think recursively! The sum of a list of k integerscan be obtained by removing the first integer, computing thesum of the remainder and adding the two values

I This translates into two rules: Base case and inductive case

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 8/29

Page 29: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length Expressions

I Observation: Difficulty caused by unknown length of list

I Let’s write operational semantics for a list of length 2:

` [i1 i2] : i1 + i2

I Solution: Think recursively! The sum of a list of k integerscan be obtained by removing the first integer, computing thesum of the remainder and adding the two values

I This translates into two rules: Base case and inductive case

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 8/29

Page 30: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 31: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 32: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 33: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 34: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 35: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Variable-Length ExpressionsI Base case: List with one integer

` [i ] : i

I Inductive Case: List with at least two integers

` [R] : i2` [i1,R] : i1 + i2

I Upshot: To give semantics to variable-length expression,decompose recursively into inductive case(s) and base case(s)

I Observe that it is possible to encode computation in thisformalism, we will (briefly) see this again towards the end ofthe class

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 9/29

Page 36: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Alternative Semantics

I We can also define the meaning of a list program as follows:Base case:

` i : i

Inductive case:` e1 : i1 `: e2 : i2` e1 + e2 : i1 + i2

Removing the brackets:

` e : i

` [e] : i

I Are these two semantics equivalent?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 10/29

Page 37: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Alternative Semantics

I We can also define the meaning of a list program as follows:Base case:

` i : i

Inductive case:` e1 : i1 `: e2 : i2` e1 + e2 : i1 + i2

Removing the brackets:

` e : i

` [e] : i

I Are these two semantics equivalent?

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 10/29

Page 38: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in LI Last time we only gave operational semantics for the

application base case: Two expressions:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I But the application can have any number of expressions in L.Example: (x y z) is a valid L expression with meaning ((x

y) z)

I Solution: Write inductive case for more than two expressions!

E ` e1 : lambda x .e ′1E ` e ′1[e2/x ] : eE ` (e R) : e ′

E ` (e1 e2 R) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 11/29

Page 39: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in LI Last time we only gave operational semantics for the

application base case: Two expressions:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I But the application can have any number of expressions in L.Example: (x y z) is a valid L expression with meaning ((x

y) z)

I Solution: Write inductive case for more than two expressions!

E ` e1 : lambda x .e ′1E ` e ′1[e2/x ] : eE ` (e R) : e ′

E ` (e1 e2 R) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 11/29

Page 40: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in LI Last time we only gave operational semantics for the

application base case: Two expressions:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I But the application can have any number of expressions in L.Example: (x y z) is a valid L expression with meaning ((x

y) z)

I Solution: Write inductive case for more than two expressions!

E ` e1 : lambda x .e ′1E ` e ′1[e2/x ] : eE ` (e R) : e ′

E ` (e1 e2 R) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 11/29

Page 41: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in LI Last time we only gave operational semantics for the

application base case: Two expressions:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I But the application can have any number of expressions in L.Example: (x y z) is a valid L expression with meaning ((x

y) z)

I Solution: Write inductive case for more than two expressions!

E ` e1 : lambda x .e ′1E ` e ′1[e2/x ] : eE ` (e R) : e ′

E ` (e1 e2 R) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 11/29

Page 42: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in L

I What about an application with one expression, such as (x)?

I This is not an application

I Observe: L syntax allows this to indicate associativity andprecedence

I Question: What is the meaning (operational semantics rule)for (x)?

I Answer:E ` e : e ′

E ` (e) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 12/29

Page 43: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in L

I What about an application with one expression, such as (x)?

I This is not an application

I Observe: L syntax allows this to indicate associativity andprecedence

I Question: What is the meaning (operational semantics rule)for (x)?

I Answer:E ` e : e ′

E ` (e) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 12/29

Page 44: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in L

I What about an application with one expression, such as (x)?

I This is not an application

I Observe: L syntax allows this to indicate associativity andprecedence

I Question: What is the meaning (operational semantics rule)for (x)?

I Answer:E ` e : e ′

E ` (e) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 12/29

Page 45: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in L

I What about an application with one expression, such as (x)?

I This is not an application

I Observe: L syntax allows this to indicate associativity andprecedence

I Question: What is the meaning (operational semantics rule)for (x)?

I Answer:E ` e : e ′

E ` (e) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 12/29

Page 46: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics of Application in L

I What about an application with one expression, such as (x)?

I This is not an application

I Observe: L syntax allows this to indicate associativity andprecedence

I Question: What is the meaning (operational semantics rule)for (x)?

I Answer:E ` e : e ′

E ` (e) : e ′

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 12/29

Page 47: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:

E ` e : [e1, e2]

E `!e : e1

I e is not a list:

E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 48: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:

E ` e : [e1, e2]

E `!e : e1

I e is not a list:

E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 49: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:

E ` e : [e1, e2]

E `!e : e1

I e is not a list:

E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 50: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:E ` e : [e1, e2]

E `!e : e1

I e is not a list:

E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 51: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:E ` e : [e1, e2]

E `!e : e1

I e is not a list:

E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 52: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I Let’s also take a brief look at semantics for some listoperations:

I Consider !e, which evaluated to the head of the list if e is alist and to e otherwise

I e is a list:E ` e : [e1, e2]

E `!e : e1

I e is not a list:E ` e : e1 (e1 not a list)

E `!e : e1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 13/29

Page 53: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I What about e1@e2, which evaluated to the list [e1, e2]?

E ` e1 : e′1

E ` e2 : e′2(e

′2 not Nil)

E ` e1@e2 : [e ′1, e′2]

I e2 evaluates to Nil:

E ` e1 : e′1

E ` e2 : Nil

E ` e1@e2 : e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 14/29

Page 54: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I What about e1@e2, which evaluated to the list [e1, e2]?

E ` e1 : e′1

E ` e2 : e′2(e

′2 not Nil)

E ` e1@e2 : [e ′1, e′2]

I e2 evaluates to Nil:

E ` e1 : e′1

E ` e2 : Nil

E ` e1@e2 : e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 14/29

Page 55: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I What about e1@e2, which evaluated to the list [e1, e2]?

E ` e1 : e′1

E ` e2 : e′2(e

′2 not Nil)

E ` e1@e2 : [e ′1, e′2]

I e2 evaluates to Nil:

E ` e1 : e′1

E ` e2 : Nil

E ` e1@e2 : e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 14/29

Page 56: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

List Operations

I What about e1@e2, which evaluated to the list [e1, e2]?

E ` e1 : e′1

E ` e2 : e′2(e

′2 not Nil)

E ` e1@e2 : [e ′1, e′2]

I e2 evaluates to Nil:

E ` e1 : e′1

E ` e2 : Nil

E ` e1@e2 : e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 14/29

Page 57: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Congratulations!

I You can now understand every page in the L reference manual.

I For PA3, you will need to refer to the operational semantics ofL in the manual to implement your interpreter.

I The manual is the official source for the semantics of L, notthe reference interpreter!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 15/29

Page 58: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Congratulations!

I You can now understand every page in the L reference manual.

I For PA3, you will need to refer to the operational semantics ofL in the manual to implement your interpreter.

I The manual is the official source for the semantics of L, notthe reference interpreter!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 15/29

Page 59: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Congratulations!

I You can now understand every page in the L reference manual.

I For PA3, you will need to refer to the operational semantics ofL in the manual to implement your interpreter.

I The manual is the official source for the semantics of L, notthe reference interpreter!

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 15/29

Page 60: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics

I The rules we have written are known as large-step operationalsemantics

I They are called large step because each rule completelyevaluates an expression, taking as many steps as necessary.

I Example: The plus rule

E ` e1 : i1 (integer)E ` e2 : i2 (integer)

E ` e1 + e2 : i1 + i2

I Here, we evaluate both e1 and e2 to compute the final valuein one (big) step

I Alternate formalism for giving semantics: small-stepoperational semantics

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 16/29

Page 61: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics

I The rules we have written are known as large-step operationalsemantics

I They are called large step because each rule completelyevaluates an expression, taking as many steps as necessary.

I Example: The plus rule

E ` e1 : i1 (integer)E ` e2 : i2 (integer)

E ` e1 + e2 : i1 + i2

I Here, we evaluate both e1 and e2 to compute the final valuein one (big) step

I Alternate formalism for giving semantics: small-stepoperational semantics

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 16/29

Page 62: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics

I The rules we have written are known as large-step operationalsemantics

I They are called large step because each rule completelyevaluates an expression, taking as many steps as necessary.

I Example: The plus rule

E ` e1 : i1 (integer)E ` e2 : i2 (integer)

E ` e1 + e2 : i1 + i2

I Here, we evaluate both e1 and e2 to compute the final valuein one (big) step

I Alternate formalism for giving semantics: small-stepoperational semantics

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 16/29

Page 63: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics

I The rules we have written are known as large-step operationalsemantics

I They are called large step because each rule completelyevaluates an expression, taking as many steps as necessary.

I Example: The plus rule

E ` e1 : i1 (integer)E ` e2 : i2 (integer)

E ` e1 + e2 : i1 + i2

I Here, we evaluate both e1 and e2 to compute the final valuein one (big) step

I Alternate formalism for giving semantics: small-stepoperational semantics

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 16/29

Page 64: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Operational Semantics

I The rules we have written are known as large-step operationalsemantics

I They are called large step because each rule completelyevaluates an expression, taking as many steps as necessary.

I Example: The plus rule

E ` e1 : i1 (integer)E ` e2 : i2 (integer)

E ` e1 + e2 : i1 + i2

I Here, we evaluate both e1 and e2 to compute the final valuein one (big) step

I Alternate formalism for giving semantics: small-stepoperational semantics

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 16/29

Page 65: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small Step Operational Semantics

I Small-step operational semantics perform only one step ofcomputation per rule invocation

I You can think of SSOS as “decomposing” all operations thathappen in one rule in LSOS into individual steps

I This means: Each rule in SSOS has at most one precondition

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 17/29

Page 66: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small Step Operational Semantics

I Small-step operational semantics perform only one step ofcomputation per rule invocation

I You can think of SSOS as “decomposing” all operations thathappen in one rule in LSOS into individual steps

I This means: Each rule in SSOS has at most one precondition

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 17/29

Page 67: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small Step Operational Semantics

I Small-step operational semantics perform only one step ofcomputation per rule invocation

I You can think of SSOS as “decomposing” all operations thathappen in one rule in LSOS into individual steps

I This means: Each rule in SSOS has at most one precondition

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 17/29

Page 68: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step Operational Semantics

I SSOS are easiest understood by an example. Consider theinteger plus in L written in SSOS:

I Rule 1: Adding two integers

〈c1 + c2,E 〉 → 〈c1 + c2,E 〉

I Rule 2: Reducing first expression to an integer

〈e1,E 〉 → 〈c,E ′〉〈e1 + e2,E 〉 → 〈c + e2,E ′〉

I Rule 3: Reducing second expression to an integer

〈e,E 〉 → 〈c2,E ′〉〈c1 + e,E 〉 → 〈c1 + c2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 18/29

Page 69: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step Operational Semantics

I SSOS are easiest understood by an example. Consider theinteger plus in L written in SSOS:

I Rule 1: Adding two integers

〈c1 + c2,E 〉 → 〈c1 + c2,E 〉

I Rule 2: Reducing first expression to an integer

〈e1,E 〉 → 〈c,E ′〉〈e1 + e2,E 〉 → 〈c + e2,E ′〉

I Rule 3: Reducing second expression to an integer

〈e,E 〉 → 〈c2,E ′〉〈c1 + e,E 〉 → 〈c1 + c2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 18/29

Page 70: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step Operational Semantics

I SSOS are easiest understood by an example. Consider theinteger plus in L written in SSOS:

I Rule 1: Adding two integers

〈c1 + c2,E 〉 → 〈c1 + c2,E 〉

I Rule 2: Reducing first expression to an integer

〈e1,E 〉 → 〈c,E ′〉〈e1 + e2,E 〉 → 〈c + e2,E ′〉

I Rule 3: Reducing second expression to an integer

〈e,E 〉 → 〈c2,E ′〉〈c1 + e,E 〉 → 〈c1 + c2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 18/29

Page 71: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step Operational Semantics

I SSOS are easiest understood by an example. Consider theinteger plus in L written in SSOS:

I Rule 1: Adding two integers

〈c1 + c2,E 〉 → 〈c1 + c2,E 〉

I Rule 2: Reducing first expression to an integer

〈e1,E 〉 → 〈c,E ′〉〈e1 + e2,E 〉 → 〈c + e2,E ′〉

I Rule 3: Reducing second expression to an integer

〈e,E 〉 → 〈c2,E ′〉〈c1 + e,E 〉 → 〈c1 + c2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 18/29

Page 72: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS in Action

I Let’s use these rules to prove what the value of (2 + 4) + 6 is:

I 〈(2 + 4) + 6, 〉 → 〈6 + 6, 〉 → 〈12, 〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 19/29

Page 73: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS in Action

I Let’s use these rules to prove what the value of (2 + 4) + 6 is:

I 〈(2 + 4) + 6, 〉 → 〈6 + 6, 〉 → 〈12, 〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 19/29

Page 74: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS

I You can tell small-step operational semantics by the 〈〉 →notation

I In contrast, LSOS have the `: notation (at least in this class)

I SSOS are really (conditional) rewrite rules

I The β reduction of λ-calculus is a small-step semantics rule

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 20/29

Page 75: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS

I You can tell small-step operational semantics by the 〈〉 →notation

I In contrast, LSOS have the `: notation (at least in this class)

I SSOS are really (conditional) rewrite rules

I The β reduction of λ-calculus is a small-step semantics rule

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 20/29

Page 76: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS

I You can tell small-step operational semantics by the 〈〉 →notation

I In contrast, LSOS have the `: notation (at least in this class)

I SSOS are really (conditional) rewrite rules

I The β reduction of λ-calculus is a small-step semantics rule

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 20/29

Page 77: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS

I You can tell small-step operational semantics by the 〈〉 →notation

I In contrast, LSOS have the `: notation (at least in this class)

I SSOS are really (conditional) rewrite rules

I The β reduction of λ-calculus is a small-step semantics rule

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 20/29

Page 78: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What are equivalent SSOS?

〈e ′1[e2/x ],E 〉 → 〈e3,E ′〉〈(lambda x .e ′1 e2),E 〉 → 〈e3,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 21/29

Page 79: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What are equivalent SSOS?

〈e ′1[e2/x ],E 〉 → 〈e3,E ′〉〈(lambda x .e ′1 e2),E 〉 → 〈e3,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 21/29

Page 80: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What are equivalent SSOS?

〈e ′1[e2/x ],E 〉 → 〈e3,E ′〉〈(lambda x .e ′1 e2),E 〉 → 〈e3,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 21/29

Page 81: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics, evaluating e1made a difference:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What about in SSOS?

I For SSOS, other rules will rewrite the expression until itmatches the form lambda x . e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 22/29

Page 82: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics, evaluating e1made a difference:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What about in SSOS?

I For SSOS, other rules will rewrite the expression until itmatches the form lambda x . e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 22/29

Page 83: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of the Application

I Recall the large-step operational semantics, evaluating e1made a difference:

E ` e1 : lambda x . e ′1E ` e ′1[e2/x ] : e

E ` (e1 e2) : e

I What about in SSOS?

I For SSOS, other rules will rewrite the expression until itmatches the form lambda x . e ′1

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 22/29

Page 84: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let

I First try:〈e2,E [x ← e1]〉 → 〈e3, 〉

〈let x = e1 in e2,E 〉 → 〈e3,E 〉

I But we want eager semantics: We want to evaluate e1 beforeadding to the environment.

I We want a rule that evaluates e1 as much as possible and onlythen applies the let rule:

I Notation: We will write e to indicate that expression e hasbeen evaluated as much as possible.

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 23/29

Page 85: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let

I First try:〈e2,E [x ← e1]〉 → 〈e3, 〉

〈let x = e1 in e2,E 〉 → 〈e3,E 〉

I But we want eager semantics: We want to evaluate e1 beforeadding to the environment.

I We want a rule that evaluates e1 as much as possible and onlythen applies the let rule:

I Notation: We will write e to indicate that expression e hasbeen evaluated as much as possible.

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 23/29

Page 86: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let

I First try:〈e2,E [x ← e1]〉 → 〈e3, 〉

〈let x = e1 in e2,E 〉 → 〈e3,E 〉

I But we want eager semantics: We want to evaluate e1 beforeadding to the environment.

I We want a rule that evaluates e1 as much as possible and onlythen applies the let rule:

I Notation: We will write e to indicate that expression e hasbeen evaluated as much as possible.

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 23/29

Page 87: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let

I First try:〈e2,E [x ← e1]〉 → 〈e3, 〉

〈let x = e1 in e2,E 〉 → 〈e3,E 〉

I But we want eager semantics: We want to evaluate e1 beforeadding to the environment.

I We want a rule that evaluates e1 as much as possible and onlythen applies the let rule:

I Notation: We will write e to indicate that expression e hasbeen evaluated as much as possible.

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 23/29

Page 88: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let cont.

I Here are the two rules for eager let in SSOS:

〈e2,E [x ← e1]〉 → 〈e2, 〉〈let x = e1 in e2,E 〉 → 〈e3,E 〉

〈e1,E 〉 → 〈e1,E ′〉〈let x = e1 in e2,E 〉 → 〈let x = e1 in e2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 24/29

Page 89: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

SSOS of let cont.

I Here are the two rules for eager let in SSOS:

〈e2,E [x ← e1]〉 → 〈e2, 〉〈let x = e1 in e2,E 〉 → 〈e3,E 〉

〈e1,E 〉 → 〈e1,E ′〉〈let x = e1 in e2,E 〉 → 〈let x = e1 in e2,E ′〉

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 24/29

Page 90: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step vs. Big-step Semantics

I In big-step semantics, any rule may invoke any number ofother rules in the hypothesis

I This means any derivation is a tree.

I In small-step semantics, each rule only performs one step ofcomputation

I This means any derivation is a line

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 25/29

Page 91: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step vs. Big-step Semantics

I In big-step semantics, any rule may invoke any number ofother rules in the hypothesis

I This means any derivation is a tree.

I In small-step semantics, each rule only performs one step ofcomputation

I This means any derivation is a line

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 25/29

Page 92: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step vs. Big-step Semantics

I In big-step semantics, any rule may invoke any number ofother rules in the hypothesis

I This means any derivation is a tree.

I In small-step semantics, each rule only performs one step ofcomputation

I This means any derivation is a line

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 25/29

Page 93: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Small-step vs. Big-step Semantics

I In big-step semantics, any rule may invoke any number ofother rules in the hypothesis

I This means any derivation is a tree.

I In small-step semantics, each rule only performs one step ofcomputation

I This means any derivation is a line

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 25/29

Page 94: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS

I The main advantage of SSOS is that it allows us todistinguish between non-terminating computation andundefined computation

I Recall: In BSOS, encountering an undefined expression, suchas 3+"duck" got us “stuck”, i.e., we could never satisfy thehypothesis to reach a conclusion

I In SSOS, undefined expressions also get stuck,i.e. no ruleapplies

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 26/29

Page 95: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS

I The main advantage of SSOS is that it allows us todistinguish between non-terminating computation andundefined computation

I Recall: In BSOS, encountering an undefined expression, suchas 3+"duck" got us “stuck”, i.e., we could never satisfy thehypothesis to reach a conclusion

I In SSOS, undefined expressions also get stuck,i.e. no ruleapplies

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 26/29

Page 96: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS

I The main advantage of SSOS is that it allows us todistinguish between non-terminating computation andundefined computation

I Recall: In BSOS, encountering an undefined expression, suchas 3+"duck" got us “stuck”, i.e., we could never satisfy thehypothesis to reach a conclusion

I In SSOS, undefined expressions also get stuck,i.e. no ruleapplies

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 26/29

Page 97: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS Cont.

I But, consider the following program: fun f with x = (fx) in (f 1).

I In BSOS, we will “get stuck”, i.e. we will never satisfy allhypothesis of the function invocation

I In SSOS, we will have an infinite derivation line

I Upshot: SSOS allow us to distinguish non-termination fromerrors

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 27/29

Page 98: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS Cont.

I But, consider the following program: fun f with x = (fx) in (f 1).

I In BSOS, we will “get stuck”, i.e. we will never satisfy allhypothesis of the function invocation

I In SSOS, we will have an infinite derivation line

I Upshot: SSOS allow us to distinguish non-termination fromerrors

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 27/29

Page 99: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS Cont.

I But, consider the following program: fun f with x = (fx) in (f 1).

I In BSOS, we will “get stuck”, i.e. we will never satisfy allhypothesis of the function invocation

I In SSOS, we will have an infinite derivation line

I Upshot: SSOS allow us to distinguish non-termination fromerrors

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 27/29

Page 100: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Advantages of SSOS Cont.

I But, consider the following program: fun f with x = (fx) in (f 1).

I In BSOS, we will “get stuck”, i.e. we will never satisfy allhypothesis of the function invocation

I In SSOS, we will have an infinite derivation line

I Upshot: SSOS allow us to distinguish non-termination fromerrors

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 27/29

Page 101: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Big vs. Small-Step Semantics

I The other big difference is that we can quantify the cost of acomputation with the number of steps in a small-stepderivation

I This allows us to talk about (some) notions of complexitywhen analyzing small-step semantics

I Main disadvantage of small step semantics is that they areless intuitive and and usually harder to write

I SSOS also always force one order, even if we would like toleave an order undefined

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 28/29

Page 102: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Big vs. Small-Step Semantics

I The other big difference is that we can quantify the cost of acomputation with the number of steps in a small-stepderivation

I This allows us to talk about (some) notions of complexitywhen analyzing small-step semantics

I Main disadvantage of small step semantics is that they areless intuitive and and usually harder to write

I SSOS also always force one order, even if we would like toleave an order undefined

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 28/29

Page 103: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Big vs. Small-Step Semantics

I The other big difference is that we can quantify the cost of acomputation with the number of steps in a small-stepderivation

I This allows us to talk about (some) notions of complexitywhen analyzing small-step semantics

I Main disadvantage of small step semantics is that they areless intuitive and and usually harder to write

I SSOS also always force one order, even if we would like toleave an order undefined

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 28/29

Page 104: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Big vs. Small-Step Semantics

I The other big difference is that we can quantify the cost of acomputation with the number of steps in a small-stepderivation

I This allows us to talk about (some) notions of complexitywhen analyzing small-step semantics

I Main disadvantage of small step semantics is that they areless intuitive and and usually harder to write

I SSOS also always force one order, even if we would like toleave an order undefined

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 28/29

Page 105: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Conclusion

I We have seen two formalisms for specifying meaning ofprograms

I There are at least two more in common use: DenotationalSemantics and Axiomatic Semantics

I However, operational semantics seem to be winning the“semantics wars”

I Why: Easier to understand and easier to prove (most)properties with them

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 29/29

Page 106: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Conclusion

I We have seen two formalisms for specifying meaning ofprograms

I There are at least two more in common use: DenotationalSemantics and Axiomatic Semantics

I However, operational semantics seem to be winning the“semantics wars”

I Why: Easier to understand and easier to prove (most)properties with them

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 29/29

Page 107: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Conclusion

I We have seen two formalisms for specifying meaning ofprograms

I There are at least two more in common use: DenotationalSemantics and Axiomatic Semantics

I However, operational semantics seem to be winning the“semantics wars”

I Why: Easier to understand and easier to prove (most)properties with them

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 29/29

Page 108: CS345H: Programming Languages Lecture 8: Operational ...tdillig/cs345H/lecture8.pdfThomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 7/29. Operational

Conclusion

I We have seen two formalisms for specifying meaning ofprograms

I There are at least two more in common use: DenotationalSemantics and Axiomatic Semantics

I However, operational semantics seem to be winning the“semantics wars”

I Why: Easier to understand and easier to prove (most)properties with them

Thomas Dillig, CS345H: Programming Languages Lecture 8: Operational Semantics II 29/29