se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-h/tc-0239/slides/sv2008_seplogic.pdf · syntax...

92
An Introduction to Separation Logic Cristiano Calcagno (thanks to Matthew Parkinson) Imperial College London – p. 1/22

Upload: others

Post on 23-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

An Introduction to Separation Logic

Cristiano Calcagno (thanks to Matthew Parkinson)Imperial College London

– p. 1/22

Page 2: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Overview

Introduction• Motivation• The Logic• Some examples

– p. 2/22

Page 3: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Motivation

– p. 3/22

Page 4: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

– p. 4/22

Page 5: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X

3 3

Stack

Heap

– p. 4/22

Page 6: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

3 3 4 4

Stack

Heap

– p. 4/22

Page 7: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

3 4 4

Stack

Heap

– p. 4/22

Page 8: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

3 4

Stack

Heap

– p. 4/22

Page 9: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

3 4

Stack

Heap

– p. 4/22

Page 10: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

4

Stack

Heap

– p. 4/22

Page 11: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example program

Motivation

x := cons(3,3);y := cons(4,4);[x+1] := y;[y+1] := x;y := x+1;dispose x;y := [y];

X Y

4

Stack

Heap

– p. 4/22

Page 12: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Why Separation Logic?

Motivation

Consider the following piece of code[Note: read [x] as indirect through x to the heap.]

[y] := 4;

[z] := 5;

Guarantee([y] != [z])

Need to know locations are different.

– p. 5/22

Page 13: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Why Separation Logic?

Motivation

Consider the following piece of code[Note: read [x] as indirect through x to the heap.]

Assume(y != z)

[y] := 4;

[z] := 5;

Guarantee([y] != [z])

Need to know locations are different.• Add assertions?

– p. 5/22

Page 14: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Why Separation Logic?

Motivation

Consider the following piece of code[Note: read [x] as indirect through x to the heap.]

Assume([x] = 3)

Assume(y != z)

[y] := 4;

[z] := 5;

Guarantee([y] != [z])

Guarantee([x] = 3)

Need to know locations are different.• Add assertions?

We need to know when things stay the same but how?

– p. 5/22

Page 15: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Why Separation Logic?

Motivation

Consider the following piece of code[Note: read [x] as indirect through x to the heap.]

Assume([x] = 3 ∧ x != y ∧ x != z)

Assume(y != z)

[y] := 4;

[z] := 5;

Guarantee([y] != [z])

Guarantee([x] = 3)

Need to know locations are different.• Add assertions?

We need to know when things stay the same but how?

• Add assertions?

– p. 5/22

Page 16: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Framing

Motivation

We want a general concept of things not being affected.

{P}C{Q}{[x] = 3 ∧ P}C{Q ∧ [x] = 3}

– p. 6/22

Page 17: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Framing

Motivation

We want a general concept of things not being affected.

{P}C{Q}{R ∧ P}C{Q ∧ R}

What are the conditions on C and R?• Very hard to define if reasoning about a heap and aliasing

– p. 6/22

Page 18: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Framing

Motivation

We want a general concept of things not being affected.

{P}C{Q}{R ∧ P}C{Q ∧ R}

What are the conditions on C and R?• Very hard to define if reasoning about a heap and aliasing

This is where separation logic comes in

{P}C{Q}{R ∗ P}C{Q ∗ R}

Introduces new connective ∗ used to separate state.

– p. 6/22

Page 19: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Separation Logic

– p. 7/22

Page 20: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Syntax

Separation Logic

P,Q ::= false Logical false| P ∧ Q Classical conjunction| P ∨ Q Classical disjunction| P ⇒ Q Classical implication| P ∗ Q Separating conjunction| P −∗ Q Separating implication| E = E Expression value equality| E �→ E points to| empty empty heap| ∃x.P existential quantifier

We use E to range over integer expressions (E does not containindirection through the heap), x over variables and C over commands.

– p. 8/22

Page 21: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 22: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 23: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 24: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 25: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 26: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 1/2

Separation Logic

Assertions are given with respect to a heap, H, and stack, S.

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

S,H |= false never satisfied

S,H |= P ∧ Q iff S,H |= P ∧ S,H |= Q

S,H |= P ∨ Q iff S,H |= P ∨ S,H |= Q

S,H |= P ⇒ Q iff S,H |= P ⇒ S,H |= Q

S,H |= E = E′ iff �E�S = �E′�S

S,H |= empty iff H = {}

We use �E�S to mean evaluation with respect to the stack, S.

– p. 9/22

Page 27: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 2/2

Separation Logic

Now for more complicated semantics ;)

S,H |= E �→ E′

iff dom(H) = {�E�S} ∧ H(�E�S) = �E′�S

S,H |= P ∗ Q

iff ∃H1H2.(H1⊥H2) ∧ (H1 ◦ H2 = H) ∧ (S,H1 |= P ) ∧ (S,H2 |= Q)

S,H |= P −∗ Q

iff ∀H ′.(H⊥H ′) ∧ (S,H ′ |= P ) ⇒ S,H ◦ H ′ |= Q

where H⊥H ′ means disjoint domains,and H ◦ H ′ means disjoint function composition.

– p. 10/22

Page 28: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 2/2

Separation Logic

Now for more complicated semantics ;)

S,H |= E �→ E′

iff dom(H) = {�E�S} ∧ H(�E�S) = �E′�S

S,H |= P ∗ Q

iff ∃H1H2.(H1⊥H2) ∧ (H1 ◦ H2 = H) ∧ (S,H1 |= P ) ∧ (S,H2 |= Q)

S,H |= P −∗ Q

iff ∀H ′.(H⊥H ′) ∧ (S,H ′ |= P ) ⇒ S,H ◦ H ′ |= Q

where H⊥H ′ means disjoint domains,and H ◦ H ′ means disjoint function composition.

– p. 10/22

Page 29: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Semantics 2/2

Separation Logic

Now for more complicated semantics ;)

S,H |= E �→ E′

iff dom(H) = {�E�S} ∧ H(�E�S) = �E′�S

S,H |= P ∗ Q

iff ∃H1H2.(H1⊥H2) ∧ (H1 ◦ H2 = H) ∧ (S,H1 |= P ) ∧ (S,H2 |= Q)

S,H |= P −∗ Q

iff ∀H ′.(H⊥H ′) ∧ (S,H ′ |= P ) ⇒ S,H ◦ H ′ |= Q

where H⊥H ′ means disjoint domains,and H ◦ H ′ means disjoint function composition.

– p. 10/22

Page 30: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Example heaps

Separation Logic

Stack

Heap

X

44

Y

44

X

44

Y

A BA B

x �→ 4, 4 x �x ↪→ 4, 4 � �(x �→ 4, 4) ∗ (y �→ 4, 4) � x

(x �→ 4, 4) ∧ (y �→ 4, 4) x �(x ↪→ 4, 4) ∧ (y ↪→ 4, 4) � �

where(E �→ E0, . . . , En) def= (E �→ E0) ∗ (E + 1 �→ E1) ∗ . . . (E + n �→ En)

and E ↪→ E′ def= (E �→ E′) ∗ true

– p. 11/22

Page 31: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

∧ versus ∗

Separation Logic

Similarities

P ∧ Q ⇔ Q ∧ P P ∗ Q ⇔ Q ∗ P

P ∧ true ⇔ P P ∗ empty ⇔ P

P ∧ (P ⇒ Q) ⇒ Q P ∗ (P −∗ Q) ⇒ Q

– p. 12/22

Page 32: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

∧ versus ∗

Separation Logic

Similarities

P ∧ Q ⇔ Q ∧ P P ∗ Q ⇔ Q ∗ P

P ∧ true ⇔ P P ∗ empty ⇔ P

P ∧ (P ⇒ Q) ⇒ Q P ∗ (P −∗ Q) ⇒ Q

Differences

P ⇒ P ∧ P one ⇒ one ∗ one

P ∧ P ⇒ P one ∗ one ⇒ one

where onedef= ∃x, y.(x �→ y).

– p. 12/22

Page 33: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Commands

Separation Logic

S : Var → Int H : Loc ⇀ Int where Loc ⊆ Int

(S,H, [E] := E′) ⇓ error if �E�S /∈ dom(H)(S,H, [E] := E′) ⇓ (S, (H | l → �E′�S)) if �E�S = l ∈ dom(H)

(S,H, x := [E]) ⇓ error if �E�S /∈ dom(H)(S,H, x := [E]) ⇓ ((S | x → H(l)),H) if �E�S = l ∈ dom(H)

(S,H, dispose(E)) ⇓ error if �E�S /∈ dom(H)(S,H, dispose(E)) ⇓ (S,H − l) if �E�S = l ∈ dom(H)

(S,H, x := cons(E0, . . . , En)) ⇓((S | x → l), (H | l → �E0�S · · · l + n → �En�S)) if l, . . . , l + n /∈ dom(H)

We say that (S,H,C) is safe if (S,H,C) ⇓ error.

– p. 13/22

Page 34: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Small Axioms

Separation Logic

{E �→ _} [E] := E′ {E �→ E′}

{X = x ∧ (E �→ Y )} x := [E] {(E[X/x] �→ Y ) ∧ Y = x}

{E �→ _} dispose(E) {empty}

{empty} x := cons(E0, . . . , En) {(x �→ E0, . . . , En)}

We use E �→ _ as a shorthand for ∃x.E �→ x.

– p. 14/22

Page 35: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Frame Rule

Separation Logic

The most important rule

{P} C {Q}{P ∗ R} C {Q ∗ R}

where FV (R) ∩ modifies(C) = ∅.

– p. 15/22

Page 36: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Frame Rule

Separation Logic

The most important rule

{P} C {Q}{P ∗ R} C {Q ∗ R}

where FV (R) ∩ modifies(C) = ∅.

Why modifies?

modifies([E] := E′) = modifies(dispose(E)) = ∅modifies(x := [E]) = modifies(x := cons(E0, . . . , En)) = {x}

Otherwise

{(x �→ 4)} y := [x] {(x �→ 4) ∧ y = 4}{(x �→ 4) ∗ (y = 3)} y := [x] {((x �→ 4) ∧ y = 4) ∗ (y = 3)}

– p. 15/22

Page 37: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Frame Rule

Separation Logic

The most important rule

{P} C {Q}{P ∗ R} C {Q ∗ R}

where FV (R) ∩ modifies(C) = ∅.

The semantics of a triple, |= {P} C {Q}, is ∀S,H if (S,H |= P ), then(S,H,C) is safe and if (S,H,C) ⇓ (S′,H ′) then S′,H ′ |= Q

– p. 15/22

Page 38: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Frame Rule

Separation Logic

The most important rule

{P} C {Q}{P ∗ R} C {Q ∗ R}

where FV (R) ∩ modifies(C) = ∅.

The semantics of a triple, |= {P} C {Q}, is ∀S,H if (S,H |= P ), then(S,H,C) is safe and if (S,H,C) ⇓ (S′,H ′) then S′,H ′ |= Q

Tight interpretation!

– p. 15/22

Page 39: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Frame Rule

Separation Logic

The most important rule

{P} C {Q}{P ∗ R} C {Q ∗ R}

where FV (R) ∩ modifies(C) = ∅.

The semantics of a triple, |= {P} C {Q}, is ∀S,H if (S,H |= P ), then(S,H,C) is safe and if (S,H,C) ⇓ (S′,H ′) then S′,H ′ |= Q

Tight interpretation!Why safe? Otherwise

{true} [x] := 7 {true}{true ∗ (x �→ 4)} [x] := 7 {true ∗ (x �→ 4)}

– p. 15/22

Page 40: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider binary trees

τdef= ε | (τ1, a, τ2)

– p. 16/22

Page 41: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider binary trees

τdef= ε | (τ1, a, τ2)

We can give the definition of a binary tree predicate as

tree ε i ≡ empty ∧ i = nil

tree (τ1, a, τ2) i ≡ ∃j, k. (i �→ j, a, k) ∗ (tree τ1 j) ∗ (tree τ2 k)

– p. 16/22

Page 42: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider binary trees

τdef= ε | (τ1, a, τ2)

We can give the definition of a binary tree predicate as

tree ε i ≡ empty ∧ i = nil

tree (τ1, a, τ2) i ≡ ∃j, k. (i �→ j, a, k) ∗ (tree τ1 j) ∗ (tree τ2 k)

Properties

(33 �→ 41, a, nil) ∗ (41 �→ nil, b, nil) =⇒ tree ((ε, b, ε), a, ε) 33tree τ i =⇒ (τ = ε) ⇔ empty ⇔ (i = nil)(tree _ i) ∧ (i = nil) =⇒ ∃j, k. (i �→ j, _, k) ∗ (tree _ j) ∗ (tree _ k)

– p. 16/22

Page 43: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p)}proc dispTree(p)

newvar i,jif p!=nil

i := [p];j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

endifendproc{empty}

– p. 17/22

Page 44: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p)}proc dispTree(p)

newvar i,jif p!=nil

{(tree _ p) ∧ p = nil}i := [p];j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}endif

endproc{empty}

– p. 17/22

Page 45: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 46: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 47: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}{∃i, j. (p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}

i := [p];j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 48: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}{∃i, j. (p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(p �→ i′) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i′) ∗ (tree _ j)}

i := [p]; {X = x ∧ (E �→ Y )} x := [E] {(E[X/x] �→ Y ) ∧ Y = x}j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 49: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}{∃i, j. (p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(p �→ i′) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i′) ∗ (tree _ j)}

{i = i ∧ (p �→ i′)}i := [p]; {X = x ∧ (E �→ Y )} x := [E] {(E[X/x] �→ Y ) ∧ Y = x}{(p �→ i′) ∧ i = i′}

j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 50: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}{∃i, j. (p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(p �→ i′) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i′) ∗ (tree _ j)}

{i = i ∧ (p �→ i′)}i := [p];{(p �→ i′) ∧ i = i′}

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 51: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];dispTree(i);dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 52: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i); {(tree _ p)} dispTree(p) {empty}dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 53: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(tree _ i)}

dispTree(i); {(tree _ p)} dispTree(p) {empty}{empty}

dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 54: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(tree _ i)}

dispTree(i); {(tree _ p)} dispTree(p) {empty}{empty}

{(p �→ i, _, j) ∗ empty ∗ (tree _ j)}dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 55: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}{(tree _ i)}

dispTree(i);{empty}

{(p �→ i, _, j) ∗ empty ∗ (tree _ j)}{(p �→ i, _, j) ∗ (tree _ j)}

dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 56: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);dispose(p+2);dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 57: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);

{(p �→ i, _, j)}dispose(p+2); {E �→ _} dispose(E) {empty}dispose(p+1);dispose(p);

{empty}

– p. 17/22

Page 58: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);

{(p �→ i, _, j)}dispose(p+2);

{(p �→ i, _)}dispose(p+1); {E �→ _} dispose(E) {empty}dispose(p);

{empty}

– p. 17/22

Page 59: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);

{(p �→ i, _, j)}dispose(p+2);

{(p �→ i, _)}dispose(p+1);

{(p �→ i)}dispose(p); {E �→ _} dispose(E) {empty}

{empty}

– p. 17/22

Page 60: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);

{(p �→ i, _, j)}dispose(p+2);

{(p �→ i, _)}dispose(p+1);

{(p �→ i)}dispose(p);

{empty}

�– p. 17/22

Page 61: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Tree dispose

Separation Logic

{(tree _ p) ∧ p = nil}i := [p];

{(p �→ i) ∗ ∃j. (p + 1 �→ _, j) ∗ (tree _ i) ∗ (tree _ j)}j := [p+2];

{(p �→ i, _, j) ∗ (tree _ i) ∗ (tree _ j)}dispTree(i);

{(p �→ i, _, j) ∗ (tree _ j)}dispTree(j);

{(p �→ i, _, j)}dispose(p+2);

{(p �→ i, _)}dispose(p+1);

{(p �→ i)}dispose(p);

{empty}

Frame rule is key to the proof! �– p. 17/22

Page 62: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider sequences of integers σdef= [] | a :: σ and the recursive formula

list [] i ≡ empty ∧ i = nil

list (a ::σ) i ≡ ∃j. (i �→ a, j) ∗ (list σ j)

This formula defines a linked list.

– p. 18/22

Page 63: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider sequences of integers σdef= [] | a :: σ and the recursive formula

list [] i ≡ empty ∧ i = nil

list (a ::σ) i ≡ ∃j. (i �→ a, j) ∗ (list σ j)

This formula defines a linked list. We will also need list segments

lseg [] i k ≡ empty ∧ i = k

lseg (a ::σ) i k ≡ ∃j. (i �→ a, j) ∗ (list σ j k)

– p. 18/22

Page 64: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Data types

Separation Logic

Consider sequences of integers σdef= [] | a :: σ and the recursive formula

list [] i ≡ empty ∧ i = nil

list (a ::σ) i ≡ ∃j. (i �→ a, j) ∗ (list σ j)

This formula defines a linked list. We will also need list segments

lseg [] i k ≡ empty ∧ i = k

lseg (a ::σ) i k ≡ ∃j. (i �→ a, j) ∗ (list σ j k)

Properties

(33 �→ a, 41) ∗ (41 �→ b, 11) ⇐⇒ lseg [a :: b] 33 11list σ i ⇐⇒ lseg σ i nil

(lseg σ1 i j) ∗ (lseg σ2 j k) ⇒ lseg (σ1 :: σ2) i k

– p. 18/22

Page 65: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{(list σ1 x) ∗ (list σ2 y)}proc append(x,y)

newvar h,c,n;if x=nil then return y;h:= x;c:= x;n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;return h;

end proc{list (σ1 :: σ2) ret}

– p. 19/22

Page 66: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{(list σ1 x) ∗ (list σ2 y)}proc append(x,y)

newvar h,c,n;if x=nil then return y;

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}h:= x;c:= x;n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

return h;end proc{list (σ1 :: σ2) ret}

– p. 19/22

Page 67: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}h:= x;c:= x;n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 68: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 69: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 70: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{(∃i. (c �→ a, i) ∗ (list σ′

1 i) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ list σ2 y}

n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 71: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{(∃i. (c �→ a, i) ∗ (list σ′

1 i) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ list σ2 y}

{((c �→ a, i) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 72: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{(∃i. (c �→ a, i) ∗ (list σ′

1 i) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ list σ2 y}

{((c �→ a, i) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{c + 1 �→ i}n:= [c+1];{(c + 1 �→ i) ∧ i = n}

while(n!=nil)c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 73: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{(∃i. (c �→ a, i) ∗ (list σ′

1 i) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ list σ2 y}

{((c �→ a, i) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{c + 1 �→ i}n:= [c+1];{(c + 1 �→ i) ∧ i = n}

{((c �→ a, i) ∧ i = n) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 74: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{(∃i. (c �→ a, i) ∗ (list σ′

1 i) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ list σ2 y}

{((c �→ a, i) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{c + 1 �→ i}n:= [c+1];{(c + 1 �→ i) ∧ i = n}

{((c �→ a, i) ∧ i = n) ∗ (list σ′1 i) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{((c �→ a, n) ∗ (list σ′

1 n) ∧ σ1 = a :: σ′1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 75: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}while(n!=nil)

c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 76: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 77: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{((empty ∧ h = c) ∗ (c �→ a, n) ∗ (list σ′

1 n)∧ σ1 = a :: σ′

1

)∗ (list σ2 y)

}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 78: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{((lseg [] h c ∧ h = c) ∗ (c �→ a, n) ∗ (list σ′

1 n)∧ σ1 = a :: σ′

1

)∗ (list σ2 y)

}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 79: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}{((lseg [] h c) ∗ (c �→ a, n) ∗ (list σ′

1 n)∧ σ1 = [] :: a :: σ′

1

)∗ (list σ2 y)

}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 80: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

[c+1] := y;{list (σ1 :: σ2) h}

– p. 19/22

Page 81: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];{(

(lseg σ′ h c) ∗ (c �→ a′, nil) ∧ (σ1 = σ′ :: a′))∗ (list σ2 y)

}[c+1] := y;

{list (σ1 :: σ2) h}

– p. 19/22

Page 82: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];{(

(lseg σ′ h c) ∗ (c �→ a′, nil) ∧ (σ1 = σ′ :: a′))∗ (list σ2 y)

}[c+1] := y;{((lseg σ′ h c) ∗ (c �→ a′, y) ∧ (σ1 = σ′ :: a′)

)∗ (list σ2 y)

}{list (σ1 :: σ2) h}

– p. 19/22

Page 83: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{((list σ1 x) ∧ x! = nil) ∗ (list σ2 y)}{((list (a :: σ′

1) x) ∧ σ1 = a :: σ′1) ∗ (list σ2 y)}

h:= x;c:= x;

{((list (a :: σ′1) c) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}n:= [c+1];

{((c �→ a, n) ∗ (list σ′1 n) ∧ σ1 = a :: σ′

1 ∧ h = c) ∗ (list σ2 y)}

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];{(

(lseg σ′ h c) ∗ (c �→ a′, nil) ∧ (σ1 = σ′ :: a′))∗ (list σ2 y)

}[c+1] := y;{((lseg σ′ h c) ∗ (c �→ a′, y) ∧ (σ1 = σ′ :: a′)

)∗ (list σ2 y)

}{(lseg σ1 h y) ∗ (list σ2 y)}{list (σ1 :: σ2) h}

– p. 19/22

Page 84: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

while(n!=nil)

{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];

– p. 19/22

Page 85: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{n = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;n:= [c+1];{

∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

– p. 19/22

Page 86: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{n = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;{

c = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c′) ∗ (c′ �→ a′, c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

n:= [c+1];{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

– p. 19/22

Page 87: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{n = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;{

c = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c′) ∗ (c′ �→ a′, c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}{

c = nil ∧ ∃σ′, σ′′.

((lseg σ′ h c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: σ′′)

)}

n:= [c+1];{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

– p. 19/22

Page 88: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{n = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;{

c = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c′) ∗ (c′ �→ a′, c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}{

c = nil ∧ ∃σ′, σ′′.

((lseg σ′ h c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: σ′′)

)}{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n′) ∗ (list σ′′ n′)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

n:= [c+1];{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

– p. 19/22

Page 89: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

List append

Separation Logic

{n = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}c:= n;{

c = nil ∧ ∃σ′, a′, σ′′.

((lseg σ′ h c′) ∗ (c′ �→ a′, c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}{

c = nil ∧ ∃σ′, σ′′.

((lseg σ′ h c) ∗ (list σ′′ c)

∧ (σ1 = σ′ :: σ′′)

)}{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n′) ∗ (list σ′′ n′)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

n:= [c+1];{∃σ′, a′, σ′′.

((lseg σ′ h c) ∗ (c �→ a′, n) ∗ (list σ′′ n)

∧ (σ1 = σ′ :: a′ :: σ′′)

)}

�– p. 19/22

Page 90: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Weakest Preconditions

Separation Logic

Are small axioms and frame rule enough?

From small axiom

{E �→ _} [E] := E′ {E �→ E′}apply frame (E �→ E′) −∗ Q to obtain

{(E �→ _) ∗ ((E �→ E′) −∗ Q)

}[E] := E′ {(E �→ E′) ∗ ((E �→ E′) −∗ Q)

}then consequence, to get the weakest precondition

{(E �→ _) ∗ ((E �→ E′) −∗ Q)

}[E] := E′ {Q}

Weakest precondition: if {P} [E] := E′ {Q} holds,then P ⇒ (E �→ _) ∗ ((E �→ E′) −∗ Q).

– p. 20/22

Page 91: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

Conclusions

Separation Logic

• Tight specifications• Dangling pointers• Local surgeries• Frame rule

– p. 21/22

Page 92: se.inf.ethz.chse.inf.ethz.ch/old/teaching/2008-H/tc-0239/slides/SV2008_seplogic.pdf · Syntax Separation Logic P,Q ::= false Logical false | P ∧Q Classical conjunction | P ∨Q

References

Separation Logic

The references for this part of the course can all be found on:http://www.dcs.qmw.ac.uk/~ohearn/localreasoning.html

– p. 22/22