Separation Logic Goes Joost-Pieter Katoen Verified Software - - PDF document

separation logic goes
SMART_READER_LITE
LIVE PREVIEW

Separation Logic Goes Joost-Pieter Katoen Verified Software - - PDF document

Verified Software Workshop 2019 Separation Logic Goes Joost-Pieter Katoen Verified Software Workshop, Isaac Newton Institute, Cambridge 2019 Joost-Pieter Katoen Separation Logic Goes 1/68 Verified Software Workshop 2019 Background and


slide-1
SLIDE 1

Verified Software Workshop 2019

Separation Logic Goes

Joost-Pieter Katoen Verified Software Workshop, Isaac Newton Institute, Cambridge 2019

Joost-Pieter Katoen Separation Logic Goes 1/68

slide-2
SLIDE 2

Verified Software Workshop 2019 Background and Introduction

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 2/68

slide-3
SLIDE 3

Verified Software Workshop 2019 Background and Introduction

Perspective in Nature

“There are several reasons why probabilistic programming could prove to be revolutionary for machine intelligence and scientific modelling.”

Joost-Pieter Katoen Separation Logic Goes 3/68

slide-4
SLIDE 4

Verified Software Workshop 2019 Background and Introduction

Probabilistic Programming

Popular PPL: STAN (> 10,000 active users, 33 releases) Almost every PL has a probabilistic version! probabilistic-programming.org

Joost-Pieter Katoen Separation Logic Goes 4/68 Scenic

O

slide-5
SLIDE 5

Verified Software Workshop 2019 Background and Introduction

Today’s Focus

“Randomised skip list algorithms have the same asymptotic expected time bounds as balanced trees and are simpler, faster, and use less space.” [Pugh, 1989] “The expected running time of randomised splay trees is smaller than deterministic variants” [Albers and Karpinski, 2002]

Joost-Pieter Katoen Separation Logic Goes 5/68

slide-6
SLIDE 6

Verified Software Workshop 2019 Background and Introduction

Today’s Focus

Can we formally prove programs that flip coins and manipulate pointers? At the source code level. No “descend” in some operational model. No ad-hoc arguments. Enabling mechanised certification.

Joost-Pieter Katoen Separation Logic Goes 6/68

slide-7
SLIDE 7

Verified Software Workshop 2019 Background and Introduction

Practical Relevance

Joost-Pieter Katoen Separation Logic Goes 7/68

E Pugh

,

CACM

ng 8g ]

slide-8
SLIDE 8

Verified Software Workshop 2019 Background and Introduction

Practical Relevance

Joost-Pieter Katoen Separation Logic Goes 7/68

[

Carbin et

al

.

,

CACM

2016 ]

slide-9
SLIDE 9

Verified Software Workshop 2019 Background and Introduction

Practical Relevance

Joost-Pieter Katoen Separation Logic Goes 7/68

[ Ruttenberg

& Pfeffer

,

2014 ]

slide-10
SLIDE 10

Verified Software Workshop 2019 Background and Introduction

Array Randomisation

randomise(array,n) { i := 0; while (0 <= i < n) { j := uniform(i,n-1); swap(array,i,j); i++ } }

Is the probability of any fixed array configuration 1 n!?

Joost-Pieter Katoen Separation Logic Goes 8/68

slide-11
SLIDE 11

Verified Software Workshop 2019 Background and Introduction

Faulty Garbage Collector

delete(x) { if (x != 0) {{ skip // fails with probability p } [p] { // flip biased coin left := <x> ; right := <x+1>; delete(left) ; delete(right); free(x) ; free(x+1) }} }

Joost-Pieter Katoen Separation Logic Goes 9/68

*

II

left

right

slide-12
SLIDE 12

Verified Software Workshop 2019 Background and Introduction

Faulty Garbage Collector

delete(x) { if (x != 0) {{ skip // fails with probability p } [p] { // flip biased coin left := <x> ; right := <x+1>; delete(left) ; delete(right); free(x) ; free(x+1) }} }

What is the probability that on termination the heap is empty?

Joost-Pieter Katoen Separation Logic Goes 9/68

slide-13
SLIDE 13

Verified Software Workshop 2019 Background and Introduction

Pointers = Problematic

Dereferencing null pointers, aliasing, memory leaks, . . .

Joost-Pieter Katoen Separation Logic Goes 10/68

slide-14
SLIDE 14

Verified Software Workshop 2019 Background and Introduction

Probabilities = Problematic Too

“In no other branch of mathematics is it so easy to make mistakes as in probability theory”

[Henk Tijms, Understanding Probability, 2004]

Joost-Pieter Katoen Separation Logic Goes 11/68

slide-15
SLIDE 15

Verified Software Workshop 2019 Background and Introduction

Mission Impossible? Not Quite!

We will develop a weakest precondition calculus à la Dijkstra that:

  • 1. combines discrete probabilities with pointers

Joost-Pieter Katoen Separation Logic Goes 12/68

slide-16
SLIDE 16

Verified Software Workshop 2019 Background and Introduction

Mission Impossible? Not Quite!

We will develop a weakest precondition calculus à la Dijkstra that:

  • 1. combines discrete probabilities with pointers
  • 2. mixes probabilistic choices and unbounded nondeterminism
  • 3. preserves virtually all properties of both:

Z separation logic, and Z weakest pre-expectations (aka: quantitative preconditions)

though things can easily break in both worlds . . .

  • 4. is applicable to reason about actual randomised algorithms

Joost-Pieter Katoen Separation Logic Goes 12/68

slide-17
SLIDE 17

Verified Software Workshop 2019 Separation Logic

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 13/68

slide-18
SLIDE 18

Verified Software Workshop 2019 Separation Logic

“The Hoare Logic for Pointers”

SEPARATION LOGIC

John Reynolds and Peter O’Hearn

Joost-Pieter Katoen Separation Logic Goes 14/68

slide-19
SLIDE 19

Verified Software Workshop 2019 Separation Logic

Heaps

States = S = { (s, h) ∂ s ⇥ Vars Z Õ“““““““““““““““““““““““““““““““““““—“““““““““““““““““““““““““““““““““““œ valuation , h ⇥

⊆N>0

Ó“““““““““““““““–““““““““““““““““Œ dom(h) Z Õ““““““““““““““““““““““““““““““““““““““““““““““—““““““““““““““““““““““““““““““““““““““““““““““œ heap }

Joost-Pieter Katoen Separation Logic Goes 15/68

  • h

is

slide-20
SLIDE 20

Verified Software Workshop 2019 Separation Logic

Deficiency of Hoare Logic: Pointers

{P} C {Q} and Mod(C) = Vars(R) = o {P 0 R} C {Q 0 R} becomes unsound for pointers, e.g., {x ( 0} Öxã ⇥= 1 {x ( 1} {x ( 0 0 y ( 0} Öxã ⇥= 1 {x ( 1 0 y ( 0} is not valid as y could alias x

Joost-Pieter Katoen Separation Logic Goes 16/68

slide-21
SLIDE 21

Verified Software Workshop 2019 Separation Logic

The Frame Rule

{P} C {Q} and Mod(C) = Vars(R) = o {P ì R} C {Q ì R} for any heap R that is unaffected by program C. Then: {x ( 0} Öxã ⇥= 1 {x ( 1} {x ( 0 ì y ( 0} Öxã ⇥= 1 {x ( 1 ì y ( 0} is valid as the separation conjunction excludes aliasing of x and y The frame rule is the key to compositional reasoning.

Joost-Pieter Katoen Separation Logic Goes 17/68

slide-22
SLIDE 22

Verified Software Workshop 2019 Separation Logic

Pointer Programs

Heap manipulation commands: x ⇥= new(E) allocation free(E) deallocation x ⇥= ÖEã lookup ÖEã ⇥= E ¨ mutation Operational semantics: u ∈ N>0 \ dom(h) and E(s) = v Öx ⇥= new(E), s, hã Öterm, s[x/u], h B { u ⇥⇥ v }ã E(s) = u ∈ dom(h) and h(u) = v Öx ⇥= ÖEã, s, hã Öterm, s[x/v], hã E(s) = u / ∈ dom(h) and h(u) = v Öx ⇥= ÖEã, s, hã Öfault, s, hã

Joost-Pieter Katoen Separation Logic Goes 18/68

slide-23
SLIDE 23

Verified Software Workshop 2019 Separation Logic

Elementary SL Formulas

(s, h) Ï emp iff dom(h) = o (s, h) Ï [x ( y] iff dom(h) = { s(x) } and h(s(x)) = s(y)

Joost-Pieter Katoen Separation Logic Goes 19/68

  • en

p

  • erp

7

emp

Temp

slide-24
SLIDE 24

Verified Software Workshop 2019 Separation Logic

Separation Conjunction

s, h Ï F ì G iff Ωh1, h2 ⇥ h = h1 B h2 and s, h1 Ï F and s, h2 Ï G For heap h = { 4 ⇥⇥ 7 } B { 7 ⇥⇥ 4 } we have: ( ) ( )

Joost-Pieter Katoen Separation Logic Goes 20/68

I

FF

to

FF

f- G

T

T

slide-25
SLIDE 25

Verified Software Workshop 2019 Separation Logic

Separation Implication

s, h Ï F — ì G iff ºh¨ ⇥ h#h¨ and s, h¨ Ï F⌥ implies s, h B h¨ Ï G Adjointness of ì and — ì: (F ì G) R iff G (F — ì R) Modus ponens: F ì (F — ì G) G

Joost-Pieter Katoen Separation Logic Goes 21/68

  • ammmBaBaammaaga*am£
.

'

*

slide-26
SLIDE 26

Verified Software Workshop 2019 Separation Logic

Separation Implication

s, h Ï F — ì G iff ºh¨ ⇥ h#h¨ and s, h¨ Ï F⌥ implies s, h B h¨ Ï G Adjointness of ì and — ì: (F ì G) R iff G (F — ì R) Modus ponens: F ì (F — ì G) G

Joost-Pieter Katoen Separation Logic Goes 21/68

f-

F

f- F

⇐ of :

If

to

slide-27
SLIDE 27

Verified Software Workshop 2019 Separation Logic

Example SL Proof

Joost-Pieter Katoen Separation Logic Goes 22/68

slide-28
SLIDE 28

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 23/68

slide-29
SLIDE 29

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

“Dijkstra’s Weakest Preconditions Go Random”

WEAKEST PRE-EXPECTATIONS

Dexter Kozen, Annabelle McIver, and Carroll Morgan

Joost-Pieter Katoen Separation Logic Goes 24/68

slide-30
SLIDE 30

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Weakest Precondition Reasoning

Use an inductively defined backwards moving predicate transformer wp(C) ⇥ 2S 2S:

Joost-Pieter Katoen Separation Logic Goes 25/68

slide-31
SLIDE 31

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

From Predicates to Quantities

  • 1. Let program C be:

x := 5 [4/5] x := 10 The expected value of x on C’s termination is:

4 55 + 1 510 = 6

  • 2. Let program C ¨ be:

x := x+5 [4/5] x := 10 The expected value of x on C ¨’s termination is:

4 5(x+5) + 1 510 = 4x

5 + 6

  • 3. The probability that x = 10 on C ¨’s termination is:

4 5 [x+5 = 10] + 1 5 1 = 4 [x = 5] + 1 5

Joost-Pieter Katoen Separation Logic Goes 26/68

slide-32
SLIDE 32

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Expectations

Classical predicates: A predicate F maps program states onto Booleans. Let F ≤ G if and only if F G. Quantitative predicates: expectations: An expectation1 (aka: factor) f maps program states

  • nto the non-negative reals extended with infinity.

Let f ≤ g if and only if f (s) ≤ g(s) for all states s.

The set of expectations under ≤ is a complete lattice.

1≠ expectations in probability theory. Joost-Pieter Katoen Separation Logic Goes 27/68

slide-33
SLIDE 33

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Expectation Transformers

Classical predicate transformer: Maps predicates onto predicates. Dijkstra’s weakest preconditions are an instance of this. Expectation transformer: Maps expectations onto expectations. Characterising equation of a Kozen’s weakest pre-expectation: wp(C)(f ) = λs. =

S f dCs

with Cs the distribution over C’s final states when running C on state s

Joost-Pieter Katoen Separation Logic Goes 28/68

  • E. KIIT
. .
slide-34
SLIDE 34

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Pictorially

Joost-Pieter Katoen Separation Logic Goes 29/68

  • S

Ax

Eff

.

  • f
  • f
  • f
  • f
  • f

]

slide-35
SLIDE 35

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Quantitative Weakest Pre-condition Semantics

For expectation f , programs C, the wp(C)(f ) is defined by: skip f x ⇥= E f [x/E] C1; C2 wp(C1)(wp(C2)(f )) if B {C1} else {C2} [B] wp(C1)(f ) + [¬B] wp(C2)(f ) C1 [p] C2 p wp(C1)(f ) + (1p) wp(C2)(f ) while(B){C¨} lfp X. [¬B] f + [B] wp(C¨)(X) Õ““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““—““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““œ

loop unrolling

lfp is the least fixed point operator wrt. the ordering ≤ on expectations. Extensions with recursion, conditioning, liberal wp, negative expectations.

Joost-Pieter Katoen Separation Logic Goes 30/68

slide-36
SLIDE 36

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Examples

  • 1. Let program C be:

x := 5 [4/5] x := 10 For f = x, we have wp(C, x) = 4

5wp(x ⇥= 5)(x) + 1 5wp(x ⇥= 10)(x) = 4 55 + 1 510 = 6

  • 2. Let program C ¨ be:

x := x+5 [4/5] x := 10 For f = x, we have: wp(C ¨)(x) = 4

5wp(x +⇥= 5)(x)+ 1 5wp(x ⇥= 10)(x) = 4 5(x+5)+ 1 510 = 4x 5 + 6

  • 3. For program C ¨ (again) and f = [x = 10], we have:

wp(C ¨)([x=10]) =

4 5 wp(x ⇥= x+5)([x=10]) + 1 5 wp(x ⇥= 10)(x=10])

=

4 5 [x+5 = 10] + 1 5 [10 = 10]

=

4[x=5]+1 5

Joost-Pieter Katoen Separation Logic Goes 31/68

slide-37
SLIDE 37

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Properties

For all programs C and expectations f , g it holds: Z Continuity: wp(C)() is continuous. Z Monotonicity: f ≤ g implies wp(C)(f ) ≤ wp(P)(g) Z Feasibility: f ≤ k implies wp(C)(f ) ≤ k Z Linearity: wp(C)(rf + g) = rwp(C)(f ) + wp(C)(g) for every r ∈ R≥0 Z Strictness: wp(C)(0) = 0

Good to know: wp(C)(1) = termination probability of program C

Joost-Pieter Katoen Separation Logic Goes 32/68

slide-38
SLIDE 38

Verified Software Workshop 2019 Probabilistic Weakest Preconditions

Practical Relevance

Z Formal verification of randomised algorithms Z Exact inference for Bayesian networks Z Deciding program equivalence Z Proving program transformations Z Expected resource consumption Z Proving almost-sure termination

Joost-Pieter Katoen Separation Logic Goes 33/68

slide-39
SLIDE 39

Verified Software Workshop 2019 Quantitative Separation Logic

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 34/68

slide-40
SLIDE 40

Verified Software Workshop 2019 Quantitative Separation Logic

Z Assertion language Z wp-Calculus Z Theorems Z Case studies

Joost-Pieter Katoen Separation Logic Goes 35/68

slide-41
SLIDE 41

Verified Software Workshop 2019 Quantitative Separation Logic

Assertion Language: States and Expectations

Joost-Pieter Katoen Separation Logic Goes 36/68

slide-42
SLIDE 42

Verified Software Workshop 2019 Quantitative Separation Logic

Example QSL Specifications

Postexpectation f Weakest pre-expectation wp(C)(f ) λ(s, h). 1 Probability of memory-safe termination ∂x∂ Expected absolute value of x [emp] Probability of termination with an empty heap len(x, y) Expected length of list segment from x to y

Joost-Pieter Katoen Separation Logic Goes 37/68

slide-43
SLIDE 43

Verified Software Workshop 2019 Quantitative Separation Logic

Quantitative Conjunction

Classical conjunction F 0 G Quantitative conjunction f g = λ(s, h). f (s, h) g(s, h) Note: [F 0 G] = [F] [G]

Joost-Pieter Katoen Separation Logic Goes 38/68

slide-44
SLIDE 44

Verified Software Workshop 2019 Quantitative Separation Logic

Quantitative Conjunction

Classical conjunction F 0 G Quantitative conjunction f g = λ(s, h). f (s, h) g(s, h) Note: [F 0 G] = [F] [G]

Joost-Pieter Katoen Separation Logic Goes 38/68

  • Dis

dis

=

V

  • max

{

f Cs

, h )

,

g Cs

, h ))

a

  • max

{

IF ]

,

EG ] )

slide-45
SLIDE 45

Verified Software Workshop 2019 Quantitative Separation Logic

Separating Conjunction

Classical separation conjunction: (s, h) Ï F ì G iff Ωh1, h2. h = h1 ì h2 and (s, h1) Ï F and (s, h2) Ï G

Joost-Pieter Katoen Separation Logic Goes 39/68

  • h

h

,

hz

ha

hz

  • f

F

*

G

tf

EG

tf

FG

slide-46
SLIDE 46

Verified Software Workshop 2019 Quantitative Separation Logic

Separating Conjunction

Classical separation conjunction: (s, h) Ï F ì G iff Ωh1, h2. h = h1 ì h2 and (s, h1) Ï F and (s, h2) Ï G Quantitative separation conjunction: f ì g = λ(s, h). sup { f (s, h1) ì g(s, h2) ∂ h = h1 ì h2 }

Joost-Pieter Katoen Separation Logic Goes 39/68

slide-47
SLIDE 47

Verified Software Workshop 2019 Quantitative Separation Logic

Separating Implication

Classical separation implication: (s, h) Ï F — ì G iff ºh¨ with h¨#h and (s, h¨) Ï F ⇥ (s, h ì h¨) Ï G

2and not f (s, h¨) = ô = g(s, h ì h¨) Joost-Pieter Katoen Separation Logic Goes 40/68

F

FF

h

h

h

. . .
  • f-

G

FG

slide-48
SLIDE 48

Verified Software Workshop 2019 Quantitative Separation Logic

Separating Implication

Classical separation implication: (s, h) Ï F — ì G iff ºh¨ with h¨#h and (s, h¨) Ï F ⇥ (s, h ì h¨) Ï G Quantitative separation implication: f — ì g = λ(s, h). inf v g(s, h ì h¨) f (s, h¨) ∂ h¨#h and f (s, h¨) > 0 | 2 Note that: [F] — ì g = λ(s, h). inf s g(s, h ì h¨) ∂ h¨#h and (s, h¨) Ï F y

2and not f (s, h¨) = ô = g(s, h ì h¨) Joost-Pieter Katoen Separation Logic Goes 40/68

slide-49
SLIDE 49

Verified Software Workshop 2019 Quantitative Separation Logic

Adjointness

Classical adjointness: (F ì G) J iff (F (G — ì J)) Quantitative adjointness: (f ì g) ≤ j iff (f (g — ì j)) Calculus: a b ≤ c iff a ≤ b + c

+ virtually all properties in [Ishtiaq & O’Hearn 2001, Reynolds 2002]

Joost-Pieter Katoen Separation Logic Goes 41/68

B-

slide-50
SLIDE 50

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 42/68

slide-51
SLIDE 51

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 43/68

slide-52
SLIDE 52

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 44/68

slide-53
SLIDE 53

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 45/68

slide-54
SLIDE 54

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 46/68

slide-55
SLIDE 55

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 47/68

slide-56
SLIDE 56

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 48/68

slide-57
SLIDE 57

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 49/68

slide-58
SLIDE 58

Verified Software Workshop 2019 Quantitative Separation Logic

Probabilistic wp for Memory Allocation

Joost-Pieter Katoen Separation Logic Goes 50/68

slide-59
SLIDE 59

Verified Software Workshop 2019 Quantitative Separation Logic

QSL’s Weakest Pre-expectation Calculus

For expectation f in QSL, program C, the wp(C)(f ) is defined by: skip f x ⇥= E f [x/E] C1; C2 wp(C1)(wp(C2)(f )) if B {C1} else {C2} [B] wp(C1)(f ) + [¬B] wp(C2)(f ) while(B){C¨} lfp X. [¬B] f + [B] wp(C¨)(X) C1 [p] C2 p wp(C1)(f ) + (1p) wp(C2)(f ) x ⇥= new(E) infv∈Z [v ( E] — ì f [x/v] free(E) [E ( ] ì f x ⇥= ÖEã supv∈Z [E ( v] ì ([E ( v] — ì f [x/v]) ÖEã ⇥= E ¨ [E ( ] ì [E ( E ¨] — ì f ⌥

Joost-Pieter Katoen Separation Logic Goes 51/68

IN

slide-60
SLIDE 60

Verified Software Workshop 2019 Quantitative Separation Logic

Heap Manipulation: SL versus QSL

For predicate F in SL, wp(C)(F) is: x ⇥= new(E) ºv. v ( E — ì F[x/v] free(E) E ( ì F x ⇥= ÖEã Ωv. E ( v ì (E ( v — ì F[x/v]) ÖEã ⇥= E ¨ E ( ì E ( E ¨ — ì F⌥ For expectation f in QSL, wp(C)(f ) is: x ⇥= new(E) infv∈N [v ( E] — ì f [x/v] free(E) [E ( ] ì f x ⇥= ÖEã supv∈Z [E ( v] ì ([E ( v] — ì f [x/v]) ÖEã ⇥= E ¨ [E ( ] ì [E ( E ¨] — ì f ⌥

Joost-Pieter Katoen Separation Logic Goes 52/68

slide-61
SLIDE 61

Verified Software Workshop 2019 Quantitative Separation Logic

A Small Example

Joost-Pieter Katoen Separation Logic Goes 53/68

slide-62
SLIDE 62

Verified Software Workshop 2019 Quantitative Separation Logic

Theorem 1: Conservativity

QSL conservatively extends both SL and weakest preexpectations. Let F, G be SL formulas and [ [ F ] ] and [ [ G ] ] be their corresponding expectations. For all states (s, h) and all non-randomised pointer programs: (s, h) Ï F if and only if [ [ F ] ](s, h) = 1 {F} C {G} if and only if [ [ F ] ] ≤ wp(C)([ [ G ] ])

Joost-Pieter Katoen Separation Logic Goes 54/68

slide-63
SLIDE 63

Verified Software Workshop 2019 Quantitative Separation Logic

Theorem 2: Soundness

QSL’s wp is equivalent to a simple operational model. For all programs C, expectation f and state (s, h): wp(C)(f )(s, h) = expected reward w.r.t. f to reach success in MDP of C

Joost-Pieter Katoen Separation Logic Goes 55/68

automaton

slide-64
SLIDE 64

Verified Software Workshop 2019 Quantitative Separation Logic

Theorem 3: The Quantitative Frame Rule

The classical frame rule: For all SL formulas F, G and R with Mod(C) = Vars(R) = o: {F} C {G} {F ì R} C {G ì R} For F = wp(C)(G), this reduces to: wp(C)(G) ì R wp(C)(G ì R) The quantitative frame rule: For all expectations g, r with Mod(C) = Vars(r) = o it holds: wp(C)(g) ì r ≤ wp(C)(g ì r)

Joost-Pieter Katoen Separation Logic Goes 56/68

slide-65
SLIDE 65

Verified Software Workshop 2019 Quantitative Separation Logic

Remark

This variant of the frame rule is unsound: wp(C)(g) ì r ≥ wp(C)(g ì r) Counterexample with Mod(Öxã ⇥= 0) = [x 0 0] = o: wp(Öxã ⇥= 0)([emp] Õ“““““““““““““—““““““““““““““œ

g

) ì [x 0 0] Õ““““““““““““““““““—“““““““““““““““““““œ

r

/ ≥ wp(Öxã ⇥= 0)([emp] ì [x 0 0] Õ““““““““““““““““““““““““““““““““““““““““““““““““““—““““““““““““““““““““““““““““““““““““““““““““““““““œ

gìr

) Since:

wp(Öxã ⇥= 0)([emp]) = [x ( ] ì ([x ( 0] — ì [emp]) Õ“““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““—“““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““œ

=0

wp(Öxã ⇥= 0)([emp] ì [x 0 0]) = [x ( ] ì ([x ( 0] — ì ([emp] ì [x 0 0]) Õ““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““—““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““““œ

=[x00]

Joost-Pieter Katoen Separation Logic Goes 57/68

slide-66
SLIDE 66

Verified Software Workshop 2019 Case Studies

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 58/68

slide-67
SLIDE 67

Verified Software Workshop 2019 Case Studies

Example 1: Array Randomisation

randomise(array,n) { i := 0; while (0 <= i < n) { j := uniform(i,n-1); swap(array,i,j); i++ } }

The probability of any fixed array configuration is 1 n! wp(randomise(array, n))(array ( α0, . . . , αn1) = 1 n!

Joost-Pieter Katoen Separation Logic Goes 59/68

slide-68
SLIDE 68

Verified Software Workshop 2019 Case Studies

Example 2: Faulty Garbage Collector

delete (x) { if (x != 0) {{ skip // fails with probability p } [p] { // flip biased coin left := <x> ; right := <x+1>; delete(left) ; delete(right); free(x) ; free(x+1) }} }

The probability of deleting a tree with root x is at least (1p)size of heap wp(delete(x)([emp])) ≥ [tree(x)] (1p)size

Joost-Pieter Katoen Separation Logic Goes 60/68

)

9

slide-69
SLIDE 69

Verified Software Workshop 2019 Case Studies

Proof Snapshot

Joost-Pieter Katoen Separation Logic Goes 61/68

slide-70
SLIDE 70

Verified Software Workshop 2019 Case Studies

Example 3: Lossy List Reversal

lossyReversal (hd) { r := 0; while (hd != 0) { t := <hd>; { <hd> := r; r := hd} [0.5] { free(hd) }; hd := t } }

In expectation, the length of the reversed list r is at most half the length of the input list wp(lossyReversal(hd))(len(r, 0)) ≤

1 2 [hd ≠ 0] len(hd, 0)

Joost-Pieter Katoen Separation Logic Goes 62/68

slide-71
SLIDE 71

Verified Software Workshop 2019 Case Studies

Example 4: Randomised Meldable Heaps

A more formal proof of a less simple randomised data structure

randomLeaf (root) { nextL := <root>; nextR := <root+1>; if (nextL = 0 and nextR = 0) { return root } else { { next := nextL } [0.5] { next := nextR }; return randomLeaf (root) } }

[Gambin and Malinowski, 1998]

Joost-Pieter Katoen Separation Logic Goes 63/68

slide-72
SLIDE 72

Verified Software Workshop 2019 Case Studies

A Textbook Proof

Joost-Pieter Katoen Separation Logic Goes 64/68

slide-73
SLIDE 73

Verified Software Workshop 2019 Epilogue

Overview

1

Background and Introduction

2

Separation Logic

3

Probabilistic Weakest Preconditions

4

Quantitative Separation Logic

5

Case Studies

6

Epilogue

Joost-Pieter Katoen Separation Logic Goes 65/68

slide-74
SLIDE 74

Verified Software Workshop 2019 Epilogue

Mechanising QSL in

Z The assertion language of QSL has been certified in Isabelle/HOL3

Z quantitative separating connectives for general expectations Z algebraic properties (e.g., adjointness, modus ponens, monotonicity) Z conservative extension: embedding of SL into QSL Z . . . . . . Z ≈ 2,000 lines of Isabelle/HOL code, a couple of man-months

Z No errors were found; proofs could almost be taken one-to-one Z QSL’s weakest pre-expectations have not been mechanised

Z main challenge: unbounded nondeterminism (aka: memory allocation)

3courtesy Max Haslbeck, https://github.com/maxhaslbeck/QuantSepCon Joost-Pieter Katoen Separation Logic Goes 66/68

slide-75
SLIDE 75

Verified Software Workshop 2019 Epilogue

Separation Logic goes Random

  • 1. Combines discrete probabilities with pointers
  • 2. Mixes probabilistic choices and unbounded nondeterminism
  • 3. Preserves virtually all properties of both:

Z Reynolds and O’Hearn’s separation logic, and Z Kozen, McIver and Morgan’s weakest pre-expectations

  • 4. Elementary properties certified in Isabelle/HOL
  • 5. Applicable to reason about actual randomised algorithms

Z Next: verifying (dynamic) probabilistic graphical models

  • 6. Future: quantitative symbolic heaps, entailment, concurrency, . . .

Joost-Pieter Katoen Separation Logic Goes 67/68

slide-76
SLIDE 76

Verified Software Workshop 2019 Epilogue

A big thanks to my co-authors!

Hannah Arndt, Kevin Batz, Benjamin Kaminski, Christoph Matheja, Thomas Noll Further details: POPL 2019 and full version on arxiv

Joost-Pieter Katoen Separation Logic Goes 68/68