The While language

Big-step

Meaning

Small-step

The While programming language Matthew Hennessy

January 28, 2015

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

The language While

B ∈ Bool ::= true | false | E = E | B&B | ¬ B | · · · E ∈ Arith ::= l ∈ Locs | n ∈ Nums | (E + E ) | · · · C ∈ Com ::= l := E | if B then C else C |

C ; C | skip | while B do C

l, k from a set of locations Locs

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Example program

l2 := 1; l3 := 0; while ¬ (l1 = l2 ) do l2 := l2 + 1; l3 := l3 + 1; How do we describe the behaviour of these programs?

How can we prescribe how these programs should be executed?

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Dependencies Behaviour of commands in Com depend on behaviour of Boolean expressions in Bool I

if l = k then l1 := k else l2 := l

I

while ¬ (l1 = l2 ) do l2 := l2 + 1 ; l3 := l3 + 1

Behaviour of Boolean in Bool depend on behaviour of expressions in Arith I

(l + 1) = (k + 2)

I

(l2 + l) = k

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Evaluating expressions

Value of expressions depend on current values in locations k+l−1

I

Value depends on current values of locations k and l

Values stored at locations change as programs are executed

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

States I

A state (of the memory) is a function from locations to numerals, s : Locs → Nums.

I

The state s[k 7→ n] is defined by  n if k = l s[k 7→ n](l) = s(l) otherwise

I

Behaviour of commands is relative to a state

I

The state changes as the execution of a command proceeds

I

Complete execution of a command transforms an initial state into a terminal state

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Big-step semantics of arithmetic expressions Judgements: hE , si ⇓ n meaning: value of expression E relative to the state s is n

Alternative: hE , si ⇓ hn, s 0 i meaning: I

E relative to the state s evaluates to n

I

the evaluation changes the state from s to s 0

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Big-step semantics of arithmetics

(b-num)

hn, si ⇓ n (b-add)

hE1 , si ⇓ n1

hE2 , si ⇓ n2

hE1 + E2 , si ⇓ n3

n3 = add(n1 , n2 )

(b-loc)

hl, si ⇓ s(l)

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Assignments Evaluate the command (l := E ) relative to state s? Intuition: (1) evaluate E relative to state s to some value n (2) update location l with new value n

Inference rule: (b-assign)

hE , si ⇓ n hl := E , si ⇓ s[l 7→ n]

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Sequential composition Evaluate command C1 ; C2 relative to state s? Intuition: (1) evaluate C1 relative to state s, to get new state s1 (2) then evaluate C2 relative to new state s1

Rule: (b-seq.s)

hC1 , si ⇓ s1

hC2 , s1 i ⇓ s 0

hC1 ; C2 , si ⇓ s 0

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

If commands Evaluate command (if B then C1 else C2 ) relative to state s? Intuition: (1) first evaluate B to some boolean value bv (2) if true evaluate C1 relative to state s (3) if false evaluate C2 relative to state s

Rules: (b-if.t)

(b-if.f)

hB, si ⇓ true hC1 , si ⇓ s 0

hB, si ⇓ false hC2 , si ⇓ s 0

hif B then C1 else C2 , si ⇓ s 0

hif B then C1 else C2 , si ⇓ s 0

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

While commands Evaluate command (while B do C ) relative to state s? Intuition: (1) first evaluate B to some boolean value bv (2) if false nothing to be done (3) if true evaluate C relative to state s to get new state s1 (4) then evaluate original (while B do C ) relative to s1

Rules:

hB, si ⇓ false

hB, si ⇓ true hC , si ⇓ s1 hwhile B do C , s1 i ⇓ s2

hwhile B do C , si ⇓ s

hwhile B do C , si ⇓ s2

(b-while.f)

While language

(b-while.t)

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

The skip command Evaluate command skip relative to state s? Intuition: (1) nothing to do

Rule:

(b-skip)

hskip, si ⇓ s

While language

The While language

Matthew Hennessy

Big-step

Meaning

Small-step

Properties of big-step semantics

Normalisation: For every state s and every command C there exists some state s 0 such that `big hC , si ⇓ s 0 False

Determinacy: If `big hC , si ⇓ s1 and `big hC , si ⇓ s2 then s1 = s2

True

Proof requires rule induction

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Non-termination in big-step semantics

I

Let C be while ¬ (l = 0) do l := l + 1

I

Let s(l) = 1

I

How can we derive hC , si ⇓ s 0 for any s 0 when s(l) > 0?

I

What is the shortest proof of judgement of the form hC , si ⇓ s 0 ? where s(l) > 0

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

The meaning of commands l2 := 1; l3 := 0; while ¬ (l1 = l2 ) do l2 := l2 + 1; l3 := l3 + 1; l1 := l3 What does this program do?

I

A program transforms an initial state in a terminal state

I

For some initial states there may be no terminal state

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Partial functions f :A*B

Meaning: f calculates an element of B for some elements of A

Notation: I

A is the domain of f

I

B is the range of f

Note: f (a) may not be defined for some a in A

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

The meaning of commands

[[−]] : Com → (States * States) [[C ]] transforms an initial state s into a terminal state

Definition:( [[C ]](s) =

s 0, undefined,

if hC , si ⇓ s 0 otherwise

Determinacy ensures this is a proper definition

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Example Let C denote l2 := 1; l3 := 0; while ¬ (l1 = l2 ) do l2 := l2 + 1; l3 := l3 + 1; l1 := l3 How do we describe [[C ]] : (States * States) ? [[C ]](s) is only defined when s(l1 ) > 0: [[C ]](s)(l1 ) = s(l1 ) − 1 [[C ]](s)(l2 ) = s(l1 ) [[C ]](s)(l3 ) = s(l1 ) − 1 [[C ]](s)(l) = s(l) otherwise While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Small-step semantics for While Judgements: hC , si → hC 0 , s 0 i

Meaning: I

starting from state s

I

when executing command C

one step of computation leads to I

state s 0

I

with command C 0 remaining to be executed

What is a step? Depends While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

What is in a step? Decision: I I

Ignore how expressions, Booleans, are evaluated One step consists of: I I

memory update or branching decision

Concentrate on execution of commands

Terminal configurations: I

hskip, si is terminal

I

hskip, si → hC , s 0 i not possible

for every s

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Assignment How to execute one step of command (l := E )

relative to the state s

?

Intuition: I

Evaluate E relative to state s

I

Update state s with resulting value

Inference rule: (b-ass)

hE , si ⇓ n hl := E , si → hskip, s[l 7→ n]i One step suffices for entire execution – ignoring evaluation of E While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Conditional How to execute one step of (if B then C1 else C2 )

relative to state s

Intuition: I

Evaluate B relative to state s

I

If true start evaluating command C1

I

If false start evaluating command C2

Inference rule: (b-cond.t)

hB, si ⇓ true hif B then C1 else C2 , si → hC1 , si (b-cond.f)

hB, si ⇓ false hif B then C1 else C2 , si → hC2 , si While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Sequential composition How to execute one step of command (C1 ; C2 )

relative to state s

Intuition: I

Execute one step of C1 relative to state s

I

If C1 has terminated start executing C2

skip indicates termination

Inference rule: (b-seq.left)

hC1 , si → hC10 , s 0 i hC1 ; C2 , si → hC10 ; C2 , s 0 i (b-seq.skip)

hskip ; C2 , si → hC2 , si While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

While commands How to execute one step of command (while B do C )

relative to state s

Intuition: I

Evaluate B relative to s

I

If false then terminate

I

if true then execute one step of C . . . . . .

Inference rule: (b-while.f)

hB, si ⇓ false hwhile B do C , si → hskip, si (b-while.t)

hB, si ⇓ true hwhile B do C , si → hC ; while B do C , si While language

Matthew Hennessy

The While language

Big-step

While loops:

alternative

Meaning

Small-step

the unwinding rule

How to execute one step of command (while B do C )

relative to state s

Intuition: I

combination of (if B then C else . . .) and sequential composition

Inference rule:

(b-while)

hwhile B do C , si→ hif B then (C ; while B do C ) else skip, si

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Running commands

To run command C from state s: Find state s 0 such that hC , si →∗ hskip, s 0 i

Example: I

See Course Notes page 49

I

See McGusker notes, slide 50

Configurations hskip, si are terminal

While language

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Running commands: Problems can occur Infinite loops: Let C be command while true do skip I

hC , si →3 hC , si →3 hC , si →3 hC , si → . . .

I

No state s 0 such that C →∗ hskip, s 0 i

Progress property: I

Configurations hskip, si are terminal

I

Either hC , si is terminal or hC , si → hC 0 , s 0 i

While language

for some configuration hC 0 , s 0 i

Matthew Hennessy

The While language

Big-step

Meaning

Small-step

Questions Questions Questions I

Determinacy: I

I

hC , si →∗ hskip, s1 i and hC , si →∗ hskip, s2 i implies s1 = s2 ?

Consistency with big-step semantics: I I

hC , si ⇓ s 0 implies hC , si →∗ hskip, s 0 i ? hC , si →∗ hskip, s 0 i implies hC , si ⇓ s 0 ?

Proof strategy: Similiar to that used for expression language Exp More powerful proof principle required

While language

Matthew Hennessy