Top Banner
CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006 CLAUS BRABRAND © 2005-2006, University of Aarhus [ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ] SEMANTICS (Q1,’06) WEEK 2: STRUCTURAL OPERATIONAL SEMANTICS
60

C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ [email protected] ] [ brabrand

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

CLAUS BRABRAND

© 2005-2006, University of Aarhus

[ [email protected] ] [ http://www.daimi.au.dk/~brabrand/ ]

SEMANTICS (Q1,’06)

WEEK 2: ”STRUCTURAL OPERATIONAL SEMANTICS”

Page 2: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 2 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Week 2 - Outline

Syntax vs. Semantics Virtual Machine Semantics Structural Operational Semantics

Expressions (Exp) Big-step vs. small-step semantics Side-effects Behavior and Equivalence, …

Boolean Expressions (BExp) Lazy evaluation, …

Commands (Com) …

Page 3: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 3 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Active vs. Passive Learning: Correlation:

Investment ~ Benefit Example:

Studying ~ Exam result

Active vs. Passive Learning

Main Entry: os·mo·sis Pronunciation: äz-'mO-s&s, äs-Function: nounEtymology: New Latin, short for endosmosis1 : movement of a solvent through a semipermeable membrane (as of a living cell) […]2 : a process of absorption […] usually effortless often unconscious assimilation <learned a number of languages by osmosis >

Active Learning Passive Learningaka. "Learning-by-Osmosis"

vs.

Page 4: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 4 ]

SEMANTICS (Q1,’06) SEP 7, 2006

"Learning by Osmosis"

It’s Amazing…

Do Active Learning: Study = read + make exercises + reflect

(for your own sake)!

“Learn while you sleep!”

“The Semantics Pillow”

Only$19,95

Page 5: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

WHY SEMANTICS?!?

Keywords:

Why bother with semantics?!?

Page 6: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 6 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Why bother learning Semantics?!?

Why Semantics?!?

Fully understand

a prog. lang.

Semantics-preserving

prog. changes

designnewprog. lang.s

rapidly

learn new

prog. lang.debuggingprograms

cross-

compilationautomatedoptimization

automatederror detection

writefaster

programs

write"better"

programs

$$

$$$$

Pass thiscourse

Page 7: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

SYNTAX VS SEMANTICS

Keywords:

Structure vs. Meaning

Page 8: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 8 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Syntax and Semantics

Syntax: = Structure

vs.

Semantics: = Meaning

"Eats shoots and leaves"

"Eats shoots and leaves"

"Colorless Green Ideas Sleep Furiously"-- N. Chomsky (1957)

The syntactic structure affects our interpretation

; ;

Has legal syntactic structure, but no meaning

( )

, ,

Page 9: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 9 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Syntax vs. Semantics (cont'd)

In natural languages (English, Danish, ...): Syntax Semantics

In programming languages (C, Java, ML, ...): Syntax Semantics

By design (for very good reasons...):

"Time flies like an arrow "

"Fruit flies like a banana "

---------N --------V -----------N-------P

-------------------N -------V ---------------N

But, if I replace

two nouns(syntactic)

replace noun replace noun

Page 10: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 10 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Level of Abstraction

Semantic specification(s):

Appropriate level of abstraction: ...so that we can use the semantics for something !!!

is_busy is_busy is_done

high

er le

vel o

f ab

stra

ctio

n

..., transistor57824 = 5V , transistor57825 = 0V , ...

..., transistor57824 = 5V , transistor57825 = 5V , ...

zzz

Page 11: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 11 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Glasses of Abstraction…

It’s Amazing…

See behind concrete details; Perceive only what is relevant and at the appropriate

level of abstraction)!

Only$12,95

“See only the relevant!”

“The Glasses of Abstraction”

Page 12: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

THE LANGUAGE “L”

Keywords:

Expressions, Boolean Expressions, Commands, Abstract Syntax, Concrete Syntax

Page 13: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 13 ]

SEMANTICS (Q1,’06) SEP 7, 2006

The Language ”L” Basic Syntactic Sets:

Truthvalues: Set ranged over by: t , t’, t0, …

Numbers: Set ranged over by: m, n, …

Variables: Set ranged over by: v, v’,

T = {tt, ff}

N = {0, 1, 2, …}

VAR = {a, b, c, …, z}

Meta-variables

Page 14: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 14 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Alice in Wonderland

Different levels of abstraction: Things ‘A Sitting on a Gate’ Names of things ‘The Aged Aged Man.’ Things are called something ‘Ways and Means’ Names are called something ‘Haddocks’ Eyes.’

"[...] The name of the song is called ‘Haddocks’ Eyes.’”

“Oh, that’s the name of the song, is it?” Alice said, trying to feel interested.“No, you don’t understand,” the Kinght said, looking a little vexed. “That’s what the name is called. The name really is ‘The Aged Aged Man.’”

“Then I ought to have said, ‘That’s what the song is called’?” Alice corrected herself.

“No, you oughtn’t: that’s another thing. The song is called ‘Ways and Means’: but that’s only what it’s called, you know!”

“Well, what is the song, then?” said Alice, who was by this time completely bewildered.

“I was coming to that,” the Knight said. “The song really is ‘A Sitting on a Gate’: and the tune’s my own invention.”

Page 15: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 15 ]

SEMANTICS (Q1,’06) SEP 7, 2006

The Language ”L” Derived Syntactic Sets:

Arithmetic Expressions (e Exp):

Boolean Expressions (b BExp):

Commands (c Com):

e ::= n | v | e + e’ | e – e’ | e e’

b ::= t | e = e’ | b or b’ | ~ b

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

Page 16: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

VIRTUAL MACHINE SEMANTICS

Keywords:

The “SMC” Machine, Virtual Machine Semantics, Machine Code Semantics

'The world before 1981'

Page 17: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 17 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Value Stack: Set ranged over by: S

Memories: Functions ranged over by: M

Control Stack: Set ranged over by: C

SMC Machine (Trans. Sys. Semantics): Configurations:

Virtual Machines (SMC Expressions)

= Value Stack Memories Control Stack

( T N ...)*

VAR N

( Exp { +, –, } ...)*

Initially commands/expressions, and later bits of commands/expr's

For accumulating partial resultsand other pieces needed

Page 18: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 18 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Configurations:

Notation: , M VAR N

i.e., “Memory Update” More common syntax for this:

Notation: Memory Update

=

( T N ... )* VAR N ( Exp { +, –, } ...)*

m , if v = v’M’(v’) = M(v’) , otherwise

M[m/v]

M[m/v] = M’ where

M[mv]

Page 19: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 19 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Virtual Machine Transitions

Configurations:

Transitions: Defined "by case" according to top of control stack:

[case n]: < S , M , n C > < n S , M , C >

[case v]: < S , M , v C > < M(v) S , M , C >

[case ee’]: < S , M , e e’ C > < S , M , e e’ C >

[case ]: < m’ m S , M , C > < n S , M , C > …where n = m m’

Syntactic ‘+’

Semantic ‘+’

=

( T N ... )* VAR N ( Exp { +, –, } ...)*

Page 20: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 20 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Virtual Machine: Example

Given program: and memory:(((x + 1) – y) 7)

< , M, (((x + 1) – y) 7) >

< , M, (x + 1) ; y ; – ; 7 ; >

< , M, ((x + 1) – y) ; 7 ; >

M = [x=9,y=4]

< , M, x ; 1 ; + ; y ; – ; 7 ; >

< , M, ((x + 1) – y) ; 7 ; > < , M, (x + 1) ; y ; – ; 7 ; >

< , M, x ; 1 ; + ; y ; – ; 7 ; >

< 9, M, 1 ; + ; y ; – ; 7 ; >

< 9, M, 1 ; + ; y ; – ; 7 ; > < 1 ; 9, M, + ; y ; – ; 7 ; >

< 9 ; 1, M, + ; y ; – ; 7 ; > < 10, M, y ; – ; 7 ; >

< 10, M, y ; – ; 7 ; > < 4 ; 10, M, – ; 7 ; >

< 4 ; 10, M, – ; 7 ; > < 6, M, 7 ; >

< 6, M, 7 ; > < 7 ; 6, M, >

< 7 ; 6, M, > < 42, M, >

Page 21: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 21 ]

SEMANTICS (Q1,’06) SEP 7, 2006

VM Semantics: Major Drawbacks!

Advantage: Easy to implement (and efficient)

Drawbacks:

Non-intuitive Too concrete (e.g., stack for computation fragments) Indirect semantics (not syntax directed) Computational step?

“High-level language understood in terms of low-level machine code”

“Many other machine along these lines […]. They all have a tendency to pull the syntax into pieces or at any rate to wander around the syntax creating various complex symbolic structures which do not seem particularly forced by the demands of the language itself”

- Gordon Plotkin, ‘81

Page 22: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

ARITHMETIC EXPRESSIONS

Keywords:

structural operational semantics (sos) big-step & small-step semantics, side-effects

Page 23: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 23 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Given program: and memory:

VM: Processing of Additions

< , M, ((1 + (2 + 3)) + 4) > < , M, (1 + (2 + 3)) 4 + >

< , M, (1 + (2 + 3)) 4 + > < , M, 1 (2 + 3) + 4 + >

< , M, 1 (2 + 3) + 4 + > < 1, M, (2 + 3) + 4 + >

< 1, M, (2 + 3) + 4 + > < 1, M, 2 3 + + 4 + >

< 1, M, 2 3 + + 4 + > < 2 1, M, 3 + + 4 + >

< 2 1, M, 3 + + 4 + > < 3 2 1, M, + + 4 + >

< 3 2 1, M, + + 4 + > < 5 1, M, + 4 + >

< 5 1, M, + 4 + > < 6, M, 4 + >

< 6, M, 4 + > < 4 6, M, + >

< 4 6, M, + > < 10, M, >

((1 + (2 + 3)) + 4) M

rearrange

rearrange

addition1!

addition2!

rearrange

addition3!

rearrange

rearrange

rearrange

rearrange

12 3

Note: Only three transitions are of real interest as “system events”

Page 24: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 24 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Ideally we would like:

…i.e., the following transition sequence:

Ideal Processing of Additions

((1 + (2 + 3)) + 4) ((1 + 5) + 4)

explanation

((1 + 5) + 4)

explanation

(6 + 4)

(6 + 4)

explanation

10

((1 + (2 + 3)) + 4)

explanation

((1 + 5) + 4)

explanation

(6 + 4)

explanation

10

(aka. derivation sequence)(aka. reduction sequence)

Page 25: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 25 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Informal vs. Formal Specification

Informally: “One evaluates from left to right...”

Description (pseudo-formally): CONSTANTS:

Any constant, n, is already evaluated (with itself as value)

SUMS: Step 1. Evaluate to obtain result ; Step 2. Evaluate to obtain result ; Step 3. Add and to obtain final result .

n

e0 + e1

e0 n0

e1 n1

n0 n1 m

Page 26: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 26 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Inference System Semantics

Inference System Semantics: Abbreviate as

Meaning: “e terminates and evaluates to v”

e v

‘L’ Exp N

(e,v) ‘L’

Q: Did I just solve the halting problem here?!?

A: No; nobody can decide “e v” automatically!

A: Actually, for Exp termination is decidable…

Page 27: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 27 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Inference System Sem. (cont’d)

Inference System Semantics:

e0 + e1 mm = n0 + n1

e0 n0 e1 n1[SUM][CON] n n

‘L’ Exp N

Syntactic ‘+’Semantic ‘+’

Live exercise: explain difference between syntactic and semantic entities above...

Page 28: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 28 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Example Revisited (big-step)

Inference Tree:

((1 + (2 + 3)) + 4) 10

(1 + (2 + 3)) 6 4 4

1 1 (2 + 3) 5

2 2 3 3

e0 + e1 mm = n0 + n1

e0 n0 e1 n1[SUM][CON] n n

[SUM]

[CON]

[CON]

[SUM]

[SUM]

[CON] [CON]“big-step semantics”

“small-step semantics”vs.

Page 29: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 29 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Small-step Description

Description:

CONSTANTS: Any constant, n, is already evaluated (with itself as value)

SUMS: Step 1. If is not a constant,

evaluate it (one step) to obtain result ; Step 2. If is a constant, but is not,

evaluate it (one step) to obtain result ; Step 3. If and are both constants,

add them to obtain result, say .

n

e0 + e1

e0

e0’

e0 e1

e1’

e0 e1

m

Page 30: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 30 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Small-step Formalization

Transition System Semantics: Configurations: Final Configurations: Transition Relation:

Abbreviate as Meaning: “e evaluates to e’ in one step”

L := Exp

TL := N Exp

L Exp Exp

e e’(e,e’) ‘L’

Page 31: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 31 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Small-step Semantics

Transition System Semantics:

L := Exp

TL := N Exp

L Exp Exp

[SUM1]

e0 + e1 e0’ + e1

e0 e0’

[SUM2]

n0 + e1 n0 + e1’

e1 e1’

[SUM3]

n0 + n1 mm = n0 + n1

We call this a STRUCTURAL OPERATIONAL SEMANTICS

Page 32: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 32 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Example Revisited (small-step)

Transition sequence (with explanation):

((1 + (2 + 3)) + 4) ((1 + 5) + 4)

(1 + (2 + 3)) (1 + 5)

(2 + 3) 5

[SUM1]

[SUM2]

[SUM3]

“order of discovery”

find predicates make conclusions

Page 33: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 33 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Transition Sequence Information

A transition sequence

…specifies two things:

1. A “sequence of steps” themselves (here additions)

2. …and reasons why they should be performed

e e’ e’’ … m

((1 + (2 + 3)) + 4)

explanation

((1 + 5) + 4)

Page 34: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 34 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Adding Variables

Language L’: Arithmetic Expressions (e Exp)

Structural Operational Semantics: Configurations:

Final Configurations:

Transition Relation…

e ::= n | v | e0 + e1

L’ := Exp Store

TL’ := L’

Note the change in terminology: Store <--- Memory <--- MThe term Store is more generally accepted (as an abstraction of a computer’s physical memory)

Store = Var N (= Memory)where

A configuration now looks like: <e,><e,> L’ <e’,'>

Page 35: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 35 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Small-step Semantics w/ Stores

Structural Operational Semantics (w/ Stores):

[SUM1]< e0 + e1 , > < e0’ + e1 , ’ >

< e0 , > < e0’ , ’ >

[SUM2]

[SUM3]< n0 + n1 , > < m , >

m = n0 + n1

< n0 + e1 , > < n0 + e1’ , ’ >< e1 , > < e1’ , ’ >

< v , > < m , >[VAR]

Store = Var Nm = (v)

Page 36: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 36 ]

SEMANTICS (Q1,’06) SEP 7, 2006

In fact: we have no Side-Effects!

No side-effects!

[SUM1]< e0 + e1 , > < e0’ + e1 , ’ >

< e0 , > < e0’ , ’ >

[SUM2]

[SUM3]< n0 + n1 , > < m , >

m = n0 + n1

< n0 + e1 , > < n0 + e1’ , ’ >< e1 , > < e1’ , ’ >

< v , > < m , >[VAR]

Store = Var Nm = (v)

e,: <e,> <e’,’> => = ’Easily proved by structural induction [later…]

Page 37: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 37 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Explicit Absence of Side-effects

SOS Semantics:

[SUM1]

|_ e0 + e1 e0’ + e1

|_ e0 e0’

[SUM2]

[SUM3] |_ n0 + n1 m

m = n0 + n1

|_ n0 + e1 n0 + e1’

|_ e1 e1’

|_ v m

[VAR]Store = Var Nm = (v)

|_ e e’ <e,> <e’,>

Note: Absence of side-effects is now explicit

for

Page 38: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 38 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Explicit Absence of Side-effects

Terminology (more common):

[SUM1]

|_ e0 + e1 e0’ + e1

|_ e0 e0’

[SUM2]

[SUM3] |_ n0 + n1 m

m = n0 + n1

|_ n0 + e1 n0 + e1’

|_ e1 e1’

|_ v m

[VAR]Env = Var N (= Store)m = (v)

Note the change in terminology: Env <--- Store <---

Page 39: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 39 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Exp: Behavior and Equivalence

Definitions: DEF: "Behavior" (eval):

Note: eval is only a partial function(e.g., if we have division; div-by-zero)

DEF: "Program equivalence" (''):

Examples:

eval(e,) = m <=>def <e,> E* <m,>

e e’ <=>def : eval(e,) = eval(e’,)

1+1 2 x+y y+x z+z 2*z

Live Exercise: Does "" depend on the semantics (if so, how/where)?

Page 40: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

BOOLEAN EXPRESSIONS

Keywords:

eager evaluation, lazy evaluation,

parallel evaluation

Page 41: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 41 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Boolean Expressions BExp

Language B: Boolean Expressions (b BExp):

Structural Operational Semantics: Configurations:

where

Final Configurations:

Transition Relation… short-hand for

b ::= t | e = e’ | b or b’ | ~ b

B := BExp Env

TB := B

Env = Var N

|_ b B b’

We could also have used a Store (we actually have to if BExp had had side-effects)

((b,),(b’,)) B

Page 42: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 42 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Boolean Expressions BExp (cont’d)

Structural Operational Semantics:

Note that: The (this) boolean expression transition system...:

…uses the arithmetic expression transition system…:

...and does it transitively (E*) in “one step”!

|_ e0 = e1 B t

[EQ]B |_ e0 E

* n0 |_ e1 E* n1

B , TB , B

E , TE , E

t = tt, n0 = n1

ff, n0 n1

See [Plotkin, p. 42-44] for alternative semantics (with a smaller step size)

Page 43: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 43 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Disjunction: “or”

SOS for Disjunction (“or”):

What about the boolean expression: ...?!?

|_ b0 or b1 B b0’ or b1

|_ b0 B b0’[OR1]B

|_ t0 or b1 B t0 or b1’

|_ b1 B b1’[OR2]B

|_ t0 or t1 B t

[OR3]B t = t0 t1

"tt or b"

Live Exercise:syntax vs semanticoperators?

Page 44: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 44 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Disjunction: Lazy Evaluation

Lazy Semantics for Disjunction (“or”):

|_ b0 or b1 L b0’ or b1

|_ b0 L b0’[OR1]L

[OR2]L

|_ ff or b1 L b1

[OR3]L

b: tt or b tt |_ tt or b1 L tt...exploiting:

b: ff or b b

...exploiting:

Page 45: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 45 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Eager vs. Lazy Semantics for “or”

Eager Semantics: ‘B’ Lazy Semantics: ‘L’

Relationship: ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination?

b := tt or (3 = 1-2)

b,: |_ b B* t <=> |_ b L

* t

b := tt or (1 = 2/0)

b := tt or (x := tt)“side-effects”

b := tt or loop()

Page 46: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 46 ]

SEMANTICS (Q1,’06) SEP 7, 2006

For the language, BExp, we have that:

Whereas:

Eager vs. Lazy Semantics (cont’d)

b,: <b,> B* <t,’> ==> <b,> L

* <t,”>

b,: <b,> B* <t,’> <== <b,> L

* <t,”>

Can be proved by structural induction [later…]

Easily disproved by a counterexample (e.g. )

Relationship: ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination?

b := tt or (3 = 1-2)

b,: |_ b B* t <=> |_ b L

* t

b := tt or (1 = 2/0)

b := tt or (x := tt)“side-effects”

b := tt or loop()

b := tt or (3 = 1-2)

Page 47: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 47 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Parallel Evaluation of Disjunction

Live exercise: Evaluate BExps non-deterministically (via. interleaving

or in parallel) left / right operands to or:

[Think 3 mins; then interactively on the blackboard]

‘P’

b := tt or (3 = 1-2)

b := tt or (1 = 2/0)

b := tt or (x := tt)“side-effects”

b := tt or loop()

Relationship: ? Stuck configurations? …e.g. div-by-zero? Assignment? Non-termination?

b,: |_ b B* t <=> |_ b P

* t

Page 48: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 48 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Negation: “~b”

Boolean Expressions (b BExp):

Live exercise: [Think 3 mins; then interactively on the blackboard]

b ::= t | e = e’ | b or b’ | ~ b

Page 49: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 49 ]

SEMANTICS (Q1,’06) SEP 7, 2006

BExp: Behavior and Equivalence

Definitions: DEF: "Boolean Expression Behavior":

Note: eval is only a partial function(e.g., if we have division; div-by-zero)

DEF: "Boolean Expression Equivalence":

Examples:

eval(b,) = t <=>def <b,> B* t

b b’ <=>def : eval(b,) = eval(b’,)

b ~~b 1 = 2 ff x = x tt

Page 50: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

COMMANDS

Keywords:

Assignments, Conditionals, Loops,

Control flow

Page 51: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 51 ]

SEMANTICS (Q1,’06) SEP 7, 2006

SOS of Commands

Commands (c Com):

Structural Operational Semantics: Configurations:

where

Final Configurations: (?)

Transition Relation… for

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

C := (Com Store)

TC := C

Store = Var N

<c,> C <c’,’>

Here we need a Store (since we have to model side-effects)

((c,),(c’,’)) C

Problem: how do we end computation?!?

Page 52: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 52 ]

SEMANTICS (Q1,’06) SEP 7, 2006

SOS of Commands (cont’d)

Commands (c Com):

Structural Operational Semantics: Configurations:

where

Final Configurations: (!)

Transition Relation… for

c ::= nil | v := e | c ; c’ | if b then c else c’ | while b do c

C := (Com Store) Store

TC := Store

Store = Var N

<c,> C <c’,’> ((c,),(c’,’)) C

<c,> C ’ ((c,),’) C(!)forand

Here we need a Store (since we have to model side-effects)

Page 53: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 53 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Nil and Assignment (SOS)

SOS for nil:

SOS for assignment:

v := e

< v := e , > C ’

< e , > A* < m , >

[ASS]C

’ = [m/v]

< nil , > C [NIL]C

Page 54: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 54 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Sequential Composition (SOS)

SOS for Sequential Composition:

Diagramatically: “(Control) flow-charts”

c0 ; c1

< c0 ; c1 , > C < c0’ ; c1 , ’ >

< c0 , > C < c0’ , ’ >[SEQ1]C

< c0 ; c1 , > C < c1 , ’ >

< c0 , > C ’[SEQ2]C

c c’

Page 55: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 55 ]

SEMANTICS (Q1,’06) SEP 7, 2006

If-then-else (SOS)

SOS for “if-then-else”:

Diagramatically: “(Control) flow-charts”

if b then c else c’

< if b then c else c’ , > C < c , >

< b , > B* < tt , >[IF1]C

< if b then c else c’ , > C < c’ , >

< b , > B* < ff , >[IF2]C

c

c’

b

tt

ff

Page 56: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 56 ]

SEMANTICS (Q1,’06) SEP 7, 2006

While-do (SOS)

SOS for “while-do”:

Diagramatically: “(Control) flow-charts”

while b do c

< while b do c, > C < c ; while b do c , >

< b , > B* < tt , >[WH1]C

< while b do c , > C

< b , > B* < ff , >[WH2]C

tt

ff

cb

Page 57: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 57 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Com: Behavior and Equivalence

Definitions: DEF: "Command Behavior":

Note: exec is only a partial function(e.g. non-termination, div-by-zero, …)

DEF: "Command Equivalence":

Examples:

exec(c,) = ’ <=>def <c,> C* ’

c c’ <=>def : exec(c,) = exec(c’,)

if b then c else c’ if ~b then c’ else c

while ff do c nil

Page 58: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 58 ]

SEMANTICS (Q1,’06) SEP 7, 2006

"Three minutes paper"

Please spend three minutes writing down the most important things that you have learned today (now).

After 1 dayAfter 1 week

After 3 weeksAfter 2 weeks

Immediately

Page 59: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND[ 59 ]

SEMANTICS (Q1,’06) SEP 7, 2006

Page 60: C LAUS B RABRAND S EMANTICS (Q1,’06) S EP 7, 2006 C LAUS B RABRAND © 2005-2006, University of Aarhus [ brabrand@daimi.au.dk ] [ brabrand/

CLAUS BRABRAND SEMANTICS (Q1,’06) SEP 7, 2006

Next week: big-step vs. small-step,errors, type checking

Any Questions?