Big-Step Operational Semantics - userpages.uni-kob...

3 downloads 983 Views 8MB Size Report
S ::= x := a | skip | S1;S2. | if b then S1 else S2. | while b do S ii.1. This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” ...
x=1

let x = 1 in ... x(1). x.set(1)

!x(1)

Programming Paradigms and Formal Semantics

Big-Step Operational Semantics Ralf Lämmel

A big-step operational semantics for While

© Ralf Lämmel, 2009-2011 unless noted otherwise

33

n ∈ Num This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

• variables x ∈ Var

Syntactic categories • arithmetic expressions Aexp of the Whileaa ∈::=language n | x | a +a

Syntactic Categories for While language • numerals n ∈ Num • variables x ∈ Var • arithmetic expressions a ∈ Aexp a ::= n | x | a1 + a2 | a1 ∗ a2 | a1 − a2 • booleans expressions b ∈ Bexp b ::= true | false | a1 = a2 | a1 ≤ a2 noted | ¬otherwise b | b1 ∧ b2 © Ralf Lämmel, 2009-2011 unless

1

2

| a1 ∗ a2 | a1 − a2

• booleans expressions b ∈ Bexp b ::= true | false | a1 = a2 | a1 ≤ a2 | ¬ b | b1 ∧ b2 • statements S ∈ Stm S ::= x := a | skip | S1 ; S2 | if b then S1 else S2 | while b do S II.1

34

N = {0, 1, 2, · · ·}

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Semantic categories of the While language Truth values T = {tt, ff} States

Semantic Categories Semantic Categories

• State = Var → N • State" = (Var × N)∗

Natural numbers Natural numbers

• State"" = Var∗ × N∗

N=N {0,=1,{0, 2, ·1,· ·} 2, · · ·}

Lookup in a state: s x

TruthTruth values values

Update a state: s" = s[y %→ v]

= ff} {tt, ff} T=T {tt,

 

StatesStates

s" x = 

• State =→ VarN→ N • State = Var

II.2

" " • State = (Var ×∗ N)∗ • State = (Var × N) "" ∗ ∗ • State =∗ Var "" • State = Var × N×∗ N Lookup in a state: s x Lookup in a state: s x © Ralf Lämmel, 2009-2011 unless noted Update a state: s" =otherwise s[y %→

"

Update a state: s = s[y %→ v]

s x if x &= y v if x = y

v]

35

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Meanings of syntactic categories Meanings of the syntactic categories

Numerals N : Num → N Variables s ∈ State = Var → N Arithmetic expressions A : Aexp → (State → N) Boolean expressions B : Bexp → (State → T) Statements S : Stm → (State !→ State) © Ralf Lämmel, 2009-2011 unless noted otherwise

36

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Semantics of arithmetic expressions A : Aexp → State → N

A[n]s

= N [n]

A[x]s

= sx

A[a1 + a2 ]s

= A[a1 ]s + A[a2 ]s

A[a1 ∗ a2 ]s = A[a1 ]s ∗ A[a2 ]s A[a1 − a2 ]s

© Ralf Lämmel, 2009-2011 unless noted otherwise

= A[a1 ]s − A[a2 ]s

37

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Semantics of boolean expressions B : Bexp → State → T

B[true]s

= tt

B[false]s

= ff

B[a1 = a2 ]s =

B[a1 ≤ a2 ]s =

B[¬b]s

=

B[b1 ∧ b2 ]s =

  

tt if A[a1 ]s = A[a2 ]s

 

ff if A[a1 ]s "= A[a2 ]s

  

tt if A[a1 ]s ≤ A[a2 ]s

 

ff if A[a1 ]s "≤ A[a2 ]s

  

tt if B[b]s = ff

 

ff if B[b]s = tt

        

tt if B[b1]s = tt and B[b2]s = tt

        © Ralf Lämmel, 2009-2011 unless noted otherwise

ff if B[b1]s = ff or B[b2 ]s = ff 38

II.5

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Semantics of statements

© Ralf Lämmel, 2009-2011 unless noted otherwise

39

Transition systems in semantics

© Ralf Lämmel, 2009-2011 unless noted otherwise

40

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Semantics of statements Statements

Syntactic Category S ::= x := a | skip | S1 ; S2 | if b then S1 else S2 | while b do S Meaning of the syntactic category: S : Stm → (State !→ State) Two operational semantics • Natural Semantics • Structural Operational Semantics specified by transition systems

© Ralf Lämmel, 2009-2011 unless noted otherwise

41

II.7

Transition System

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Transition systems

(Γ, T, >) • Γ: a set of configurations • T : a set of terminal configurations T ⊆Γ • >: a transition relation >⊆Γ × Γ

© Ralf Lämmel, 2009-2011 unless noted otherwise

42

Small step vs. big step • Small-step semantics !

aka Structured Operational Semantics (SOS)

!

Many transitions

!

Computation steps modeled by transitions

• Big-step semantics !

aka Natural semantics

!

One (fewer) transition(s)

!

Computation steps modeled by derivation tree

© Ralf Lämmel, 2009-2011 unless noted otherwise

43

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Natural semantics

Natural semantics: describe how the “final” describe how the overall result of the result ofIdea: the computation is obtained. computation is obtained Transition system: (Γ, T, →) • Γ = {(S, s) | S ∈ While, s ∈ State} ∪ State • T = State • → ⊆ {(S, s) | S ∈ While, s ∈ State} × State Typical transition: (S, s) → s& where S is the program s is the initial state s& is the final state © Ralf Lämmel, 2009-2011 unless noted otherwise

44

II.9

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Small step vs. big step Program configuration Transitions = small steps

Final state © Ralf Lämmel, 2009-2011 unless noted otherwise

45

Variable assignments (States)

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Small step vs. big step

Derivation tree

© Ralf Lämmel, 2009-2011 unless noted otherwise

Transition = big step

46

Compositionality

© Ralf Lämmel, 2009-2011 unless noted otherwise

47

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Recall semantics of arithmetic expressions A : Aexp → State → N

A[n]s

= N [n]

A[x]s

= sx

A[a1 + a2 ]s

= A[a1 ]s + A[a2 ]s

A[a1 ∗ a2 ]s = A[a1 ]s ∗ A[a2 ]s A[a1 − a2 ]s

© Ralf Lämmel, 2009-2011 unless noted otherwise

= A[a1 ]s − A[a2 ]s

48

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

© Ralf Lämmel, 2009-2011 unless noted otherwise

49

Properties of semantics and induction proofs

© Ralf Lämmel, 2009-2011 unless noted otherwise

50

Free variables in arithmetic expressions

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

FV(n)

= ∅

One property of the semantics

FV(x)

= {x}

Proof by Structural Induction

FV(a1 + a2 ) = FV(a1 ) ∪ FV(a2 ) FV(a1 ∗ a2)

Intuitively: The value of an arithmetic expression only depends on the values of the variables that occur in it.

= FV(a1 ) ∪ FV(a2 )

FV(a1 − a2 ) = FV(a1 ) ∪ FV(a2 )

Lemma [1.11] Lemma 1.11:

Free variables in arithmetic expressions

Let s and s% be two states satisfying s x = s% x for all x ∈ FV(a). Then % A[a]s Proof = byA[a]s Structural Induction

FV(n)

= ∅

FV(x)

= {x}

FV(a1 + a2 ) = FV(a1 ) ∪ FV(a2 )

IV.5

FV(a1 ∗ a2)

Intuitively: The value of an arithmetic expression only depends on the values of the variables that occur in it.

= FV(a1 ) ∪ FV(a2 )

FV(a1 − a2 ) = FV(a1 ) ∪ FV(a2 ) Lemma 1.11:

Free variables in arithmetic expressions

Proof

FV(n)

= ∅

FV(x)

= {x}

Let s and s% be two states satisfying s x = s% x by structural induction for all xon∈ the FV(a). Then arithmetic expressionsA[a]s = A[a]s%

FV(a1 + a2 ) = FV(a1 ) ∪ FV(a2 )

© Ralf Lämmel, 2009-2011 unless noted otherwise FV(a1 ∗ a2) = FV(a1 ) ∪ FV(a2 )

IV.5

51

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

© Ralf Lämmel, 2009-2011 unless noted otherwise

52

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Let s and s% be two states satisfying s x = s% x for all x ∈ FV(a). Then A[a]s = A[a]s%

Proofs for basis elements

Proof by structural induction on the arithmetic expressions © Ralf Lämmel, 2009-2011 unless noted otherwise

53

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Let s and s% be two states satisfying s x = s% x for all x ∈ FV(a). Then A[a]s = A[a]s%

Proofs for composite elements

Proof by structural induction on the arithmetic expressions © Ralf Lämmel, 2009-2011 unless noted otherwise

54

Proof by Ind. on Shape of Derivation Trees This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Another property Theorem 2.9: of the semantics Theorem [2.9]

Proof

Theorem 2.9: The natural semantics of While is determinThe natural of WhileSis of deterministic, that is semantics for all statements While ! statements istic,allthat is for S of While and states s, sall and s!! if (S, s) → and all states s, ss!! and and (S, s!! s) → s!! ! then =→ s!!s. ! and (S, s) → s!! if (S,ss) then s! = s!! . Proof: Proof: We assume (S, s) → s! . !! ! !! We that We prove assume (S,ifs)(S, →s)s!→ . s then s = s . prove that (S, s) →ons!!the then s! = s!! . We proceed by ifinduction inference ! of (S, s) → s . induction on the inference We proceed by

of (S, s) → s! .

Proof by induction on the shape of derivation trees IV.7 © Ralf Lämmel, 2009-2011 unless noted otherwise

55

IV.7

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Induction on the shape of derivation trees

Structural induction on syntactical categories is not applicable because of the non-compositional semantics of while!

© Ralf Lämmel, 2009-2011 unless noted otherwise

56

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

© Ralf Lämmel, 2009-2011 unless noted otherwise

57

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Theorem 2.9:

Theorem [2.9]

The natural semantics of While is deterministic, that is for all statements S of While and all states s, s! and s!! if (S, s) → s! and (S, s) → s!! then s! = s!! . Proof: We assume (S, s) → s! . We prove that if (S, s) → s!! then s! = s!! . We proceed by induction on the inference of (S, s) → s! .

IV.7

Proof by induction on the shape of derivation trees © Ralf Lämmel, 2009-2011 unless noted otherwise

58

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Proof by induction on the shape of derivation trees © Ralf Lämmel, 2009-2011 unless noted otherwise

59

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Proof by induction on the shape of derivation trees © Ralf Lämmel, 2009-2011 unless noted otherwise

60

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Non-compositional semantics is Ok for this proof scheme.

Proof by induction on the shape of derivation trees © Ralf Lämmel, 2009-2011 unless noted otherwise

61

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Yet another property of the semantics Lemma [2.5]

Proof

Part 1: (*)

(**)

Part II: (**)

(*)

© Ralf Lämmel, 2009-2011 unless noted otherwise

62

This slide is derived from the book & slides by Nielson & Nielson: “Semantics with applications” (1991 & 1999+).

Proof only (*)

(**)

only for tt

No induction needed here.

© Ralf Lämmel, 2009-2011 unless noted otherwise

63

• Summary: Big-step operational semantics ! Models relations between syntax, states, values. ! Uses deduction rules (conclusion, premises). ! Computations are derivation trees. ! Induction proofs are the key tool in semantics. • Prepping: “Semantics with applications” ! Chapter 1 and Chapter 2.1 • Outlook: ! Small-step semantics ! Type systems © Ralf Lämmel, 2009-2011 unless noted otherwise

64

Suggest Documents