Charge! a framework for higher-order separation logic in Coq Lars - - PowerPoint PPT Presentation

charge a framework for higher order separation logic in
SMART_READER_LITE
LIVE PREVIEW

Charge! a framework for higher-order separation logic in Coq Lars - - PowerPoint PPT Presentation

Charge! a framework for higher-order separation logic in Coq Lars Birkedal Logic and Semantics Group Dept. of Comp. Science, Aarhus University (Joint work with J. Bengtson, J.B. Jensen, H. Mehnert, P . Sestoft, F . Sieczkowski) April 2013


slide-1
SLIDE 1

Charge! a framework for higher-order separation logic in Coq

Lars Birkedal

Logic and Semantics Group

  • Dept. of Comp. Science, Aarhus University

(Joint work with J. Bengtson, J.B. Jensen, H. Mehnert, P . Sestoft, F . Sieczkowski)

April 2013

Lars Birkedal (AU) Charge! Types 2013, Toulouse 1 / 35

slide-2
SLIDE 2

Separation Logic

Program Logic a la Hoare Logic for reasoning about programs with pointers (or references to shared mutable data) [Reynolds, O’Hearn, . . ., 2000+] Main feature: it facilitates modular reasoning, formalized via so-called frame rule, using a connective called separating conjunction.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 2 / 35

slide-3
SLIDE 3

Hoare Logic - a recap

Programming Language: imperative while-language Assertion Language: first-order logic w. equality Specifications for partial correctness:

{P}C{Q} if s P and C, s → s′, then s′ Q.

Rules for deriving specifications (including rules for first-order logic).

Lars Birkedal (AU) Charge! Types 2013, Toulouse 3 / 35

slide-4
SLIDE 4

Separation Logic

Programming Language: as before but now with C-like pointers Specifications for partial correctness:

{P}C{Q} if s, h P and C, s, h → s′, h′, then s′, h′ Q.

Assertion Language: first-order logic w. equality + BI connectives:

s, h emp iff h is the empty heap s, h x → 5 iff h is the singleton heap with one location s(x) with value 5. s, h P ∗ Q iff h can be split into h1 and h2, with disjoint domains, such that s, h1 P and s, h2 Q. s, h P − − ∗ Q iff . . ..

Lars Birkedal (AU) Charge! Types 2013, Toulouse 4 / 35

slide-5
SLIDE 5

Example of “small” axiom

{x → −}dispose(x){emp}

Modular Reasoning via Frame Rule:

{P} C {Q} {P ∗ R} C {Q ∗ R} (assuming modifies(C) ∩ freevar(R) = ∅).

Lars Birkedal (AU) Charge! Types 2013, Toulouse 5 / 35

slide-6
SLIDE 6

Higher-Order Separation Logic Example

Developed HOSL in ESOP-2005 paper.

Stack ADT

stackspec = ∃α : Type.∃inv : α × N list → Prop. {emp} new() {s : α. inv(s, [])} × ∀s : α.∀x : N.∀l : N list {inv(s, l)} push(s, x) {inv(s, x :: l)} × ∀s : α.∀x : N.∀l : N list {inv(s, x :: l)}pop(s){y : N. inv(s, l) ∧ y = x}. Modularity: clients can use the spec without knowing anything about how the stack is implemented (since abstract in the inv predicate). Different stack implementations can meet this spec.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 6 / 35

slide-7
SLIDE 7

Charge!

Aims tool for verification of OO (Java / C#) code machine-checkable correctness proofs proofs similar to pen and paper proofs in separation logic automate tedious first-order reasoning where possible Idea: test the theory on larger examples (hard to do with pen and paper) make it available for students to experiment with Real-life test cases: from the C5 collection library for C#

uses interfaces heavily to parameterize modules on each other (“interfaces” is the OO way of programming with unknown code)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 7 / 35

slide-8
SLIDE 8

Other approaches

When we started work on Charge: For similar prog. language and logic: Verifast / jStar

specialized tools, Verifast: larger TCB (own specialized theorem prover). We focus more on modular reasoning about modular code, using facilities of Coq jStar: no guarantee of soundness since based on user-declared theories

Language designed arround type theory: Hoare Type Theory / Ynot [Nanevski, Morrisett, Birkedal, Chlipala, et. al.]

now we wanted to test ideas for logic for existing mainstream programming language

C-like language: Appel / McCreight macros.

useful ideas that we build on, exposes the model of separation logic to user, reasoning about heaps directly, which we seeked to avoid

Lars Birkedal (AU) Charge! Types 2013, Toulouse 8 / 35

slide-9
SLIDE 9

Overview of Charge!

Shallow embedding of HOSL in Coq. Support for program variables in assertions Step-indexed specification logic with quantifiers Nested triples, i.e. step-indexed specifications in assertions Hoare triple defined on semantic commands Building blocks for defining control flow constructs: id seq ˆ c1 ˆ c2 ˆ c1 + ˆ c2 ˆ c∗ assume P Instantiation to Java / C# like OO language. Static types replaced by specifications Tactics to automate part of the reasoning. Test applications

Pattern for interface specifications (since C5 relies on interfaces) Snapshotable trees (datastructure with sharing)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 9 / 35

slide-10
SLIDE 10

Uniform Predicates

Definition (Separation Algebra)

A separation algebra is a partial, cancellative, commutative monoid (Σ, ◦, 1) where Σ is the carrier, ◦ is the monoid operator, and 1 is the unit element. Generalizes heaps, so we often refer to elements of Σ as heaps. h1 ⊑ h2, if there exists an h3 such that h2 = h1 ◦ h3.

Definition (Uniform Predicate)

UPred(Σ) {p ⊆ Σ × N | ∀g, m. ∀h ⊒ g. ∀n ≤ m. (g, m) ∈ p → (h, n) ∈ p)}

Lars Birkedal (AU) Charge! Types 2013, Toulouse 10 / 35

slide-11
SLIDE 11

Uniform Predicates, II

Lemma

UPred(Σ) forms a complete BI algebra. true Σ × N false ∅ p ∧ q p ∩ q p ∨ q p ∪ q ∀x : U. f

x:U f x

∃x : U. f

  • x:U f x

p → q {(h, n) | ∀g ⊒ h. ∀m ≤ n. (g, m) ∈ p → (g, m) ∈ q} p ∗ q {(h1 ◦ h2, n) | h1 # h2 ∧ (h1, n) ∈ p ∧ (h2, n) ∈ q} p − ∗ q {(h, n) | ∀m ≤ n. ∀h1#h. (h1, m) ∈ p → (h ◦ h1, m) ∈ q} For the quantifiers, U is of type Type, i.e. the sort of types in Coq, and f is any Coq function from U to UPred(Σ). This allows us to quantify

  • ver any member of Type in Coq.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 11 / 35

slide-12
SLIDE 12

Stacks and Assertions

Definition

stack var → val. s ≃V s′ ∀x ∈ V. s x = s′ x. Stack monad: sm T {(f : stack → T, V : P(var)) | ∀s, s′. s ≃V s′ → f s = f s′} (intuition: V over-approx of free variables in f).

Definition

expr sm val pure sm Prop asn(Σ) sm UPred(Σ)

Lemma

asn(Σ) forms a complete BI algebra.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 12 / 35

slide-13
SLIDE 13

Stacks and Substitution

Stack monad used to define map f → f If f : T → U, then f : sm T → sm U. Example

Given R : val → T → UPred(heap), then

  • R : sm val → sm T → sm UPred(heap), i.e.

expr → sm T → asn(heap).

Make lifting explicit in specifications since restricts how program variables behave under substitution.

( f e)[e′/x] = f (e[e′/x]) for any f : val → UPred(Σ), But do NOT have (g e)[e′/x] = g (e[e′/x]) for any g : expr → asn(Σ), because g e may have more free program variables than those appearing in e. Hence in specs we typically quantify over functions into UPred(Σ), which we then lift to asn(Σ) as needed.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 13 / 35

slide-14
SLIDE 14

Semantic Commands

Definition (pre-command)

A pre-command ˜ c relates an initial state to either a terminal state or the special err state: precmd P(stack × Σ × ((stack × Σ) ⊎ {err}) × N) We write (s, h, ˜ c) n x to mean that (s, h, x, n) ∈ ˜ c.

Definition (Frame property)

A pre-command ˜ c has the frame property in case the following holds. If (s, h1, ˜ c) n err and (s, h1 ◦ h2, ˜ c) n (s′, h′) then there exists h′

1 such

that h′ = h′

1 ◦ h2 and (s, h1, ˜

c) n (s′, h′

1).

Lars Birkedal (AU) Charge! Types 2013, Toulouse 14 / 35

slide-15
SLIDE 15

Semantic Commands, II

Definition (Semantic command)

A semantic command satisfies the frame property and does not evaluate to anything in zero steps. semcmd {ˆ c ∈ precmd | ˆ c has the frame property ∧ ∀s, h, x. (s, h, ˆ c) 0 The following are semantic commands: id seq ˆ c1 ˆ c2 ˆ c1 + ˆ c2 ˆ c∗ assume P check P (The check-command works like the id-command if pure assertion P can be inferred.)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 15 / 35

slide-16
SLIDE 16

Specification Logic

Definition

spec {S ⊆ N | ∀m, n. m ≤ n ∧ n ∈ S → m ∈ S} Gives an intuitionistic specification logic:

Lemma

(spec, ⊆) is a cHA.

Definition (Hoare Triple)

{P}ˆ c{Q} {n | ∀m ≤ n. ∀k ≤ m. ∀s, h. (h, m) ∈ P s → (s, h, ˆ c) k err ∧ ∀h′, s′. (s, h, ˆ c) k (s′, h′) → (h′, m − k) ∈ Q s′}

Lars Birkedal (AU) Charge! Types 2013, Toulouse 16 / 35

slide-17
SLIDE 17

Nested Specifications

Nested specifications for reasoning about higher-order / unknown code. FunI : spec → UPred(Σ) λS. Σ × S.

Lemma

FunI is monotone, preserves implication, and has a left and a right adjoint. So all specification logic connectives preserved. (Could merge specifiation and assertion logic.)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 17 / 35

slide-18
SLIDE 18

Recursion

Step-indexing used for mutually recursive specifications. ⊲S {n + 1 | n ∈ S} ∪ {0} Γ ∧ ⊲S | = S 0 ∈ Γ → 0 ∈ S Γ | = S Löb

Lars Birkedal (AU) Charge! Types 2013, Toulouse 18 / 35

slide-19
SLIDE 19

Instantiation to OO lang

Shallow embedding of expressions (recall expr = sm val); deep embedding of other syntax: P ::= C∗ f ∈ (field names) C ::= class C f ∗ (m(¯ x){c; return e})∗ c ::= x := alloc C | x := e | x := y.f | x.f := e | x := y.m(¯ e) | x := C::m(¯ e) | skip | c1; c2 | if e then c1 else c2 | while e do c | assert e Σ = heap (ptr × field)

fin

⇀ val Semantics defined via (functional) relation c ∼sem ˆ c, relating syntactic command to semantic command.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 19 / 35

slide-20
SLIDE 20

Syntactic Hoare Triples

Definition

{P}c{Q} ∀ˆ

  • c. c ∼sem ˆ

c → {P}ˆ c{Q}.

Definition

C::m(¯ p) → {P}_{r. Q} ∃c, e. wf(¯ p, r, P, Q, c) ∧ C::m(¯ p){c; return e} ∈ P ∧ {P}c{Q[e/r]}, (where wf expresses that method parameter names do not clash; that pre- and postconditions do not use any stack variables other than the method parameters and this (the postcondition may also use the return variable); the method body does not modify the values of the method parameters or this).

Lars Birkedal (AU) Charge! Types 2013, Toulouse 20 / 35

slide-21
SLIDE 21

Example Proof Rules

Rule for method call uses ⊲, often used in connection with Löb induction. Γ | = ⊲C::m(¯ p) → {P}_{r. Q} |¯ p| = |y, ¯ e| Γ | = {y : C ∧ P[y, ¯ e/¯ p]}x := y.m(¯ e){∃v. Q[x, y[v/x], ¯ e[v/x]/r, ¯ p]} Frame Rule (uses notion of free vars for assertions) ∀x ∈ fv R. c does not modify x {P}c{Q} | = {P ∗ R}c{Q ∗ R} All proof rules proved sound in Coq.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 21 / 35

slide-22
SLIDE 22

Example Java program

interface ICell { /∗∗ ∗ Should return the last ∗ value passed to set() ∗/ int get(); void set(int v); } static void proxySet(ICell c, int v) { c.set(v); } c := new Recell(); c.set(1); proxySet(c, 2); c.undo(); assert c.get() = 1; Note: unknown code in proxySet. Modularity point: specify and verify proxySet before knowing about Recell instance, and still be able to prove client program.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 22 / 35

slide-23
SLIDE 23

Recell implementation

interface IRecell extends ICell { void undo(); } class Recell implements IRecell { Cell cell; int bak; Recell() { this.cell = new Cell(); } int get() { return this.cell.get(); } void set(int v) { this.bak = this.cell.get(); this.cell.set(v); } void undo() { this.cell.set(this.bak); } }

Lars Birkedal (AU) Charge! Types 2013, Toulouse 23 / 35

slide-24
SLIDE 24

Interfaces as specifications

ICell C T R g s is a predicate in the specification logic. ICell λC : classname. λT : Type. λR : val → T → UPred(heap). λg : T → val. λs : T → val → T. (∀t : T. C::get(this) → { R this t}_{x. R this t ∧ x = g t}) ∧ (∀t : T. C::set(this, x) → { R this t}_{ R this ( s t x)}) ∧ (∀t : T, v : val. g (s t v) = v) proxySet_spec ∀C, T, R, g, s. ICell C T R g s → ∀t : T. proxySet(c, x) → {c : C ∧ R c t}_{ R c ( s t x)}

Lars Birkedal (AU) Charge! Types 2013, Toulouse 24 / 35

slide-25
SLIDE 25

Cell instance

Cell_spec ∃R : val → val → UPred(heap). ICell Cell val R (λv. v) (λ_, v. v) ∧ Cell::new() → {true}_{ret. R ret _} Unfold definitions to get Cell_spec = ∃R : val → val → UPred(heap). (∀t : T. Cell::get(this) → { R this t}_{x. R this t ∧ x = t}) ∧ (∀t : T. Cell::set(this, x) → { R this t}_{ R this x}) ∧ Cell::new() → {true}_{ret. R ret _}

Lars Birkedal (AU) Charge! Types 2013, Toulouse 25 / 35

slide-26
SLIDE 26

Recell instance

Recell_spec ∃R : val → val × val → UPred(heap). ICell Recell (val × val) R π1 (λ(v, _), v′. (v′, v)) ∧ Recell::new() → {true}_{ret. R ret (_, _)} ∧ (∀v, b. Recell::undo(this) → { R this (v, b)}_{ R this (b, b)})

Lars Birkedal (AU) Charge! Types 2013, Toulouse 26 / 35

slide-27
SLIDE 27

Proof of client now possible

even though proxySet verified independently (in fact before) of Recell. so OO style inheritance among interfaces can be specified in HOSL (no special features added to support inheritance) formalization borrows types, logic variable handling and higher-order features from Coq

avoids having to build ad-hoc copies of those features program variable handling is more manual and interacts with higher-order featues (using the explicit lifting)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 27 / 35

slide-28
SLIDE 28

Tactics

Challenge: we would like the user to reason in separation logic Coq tactics work on Coq logic Need some tactics to support reasoning in the shallowly embedded program logic. Our tactics [ITP-2012] work on any assertion logic, over general separation algebras. Implementation uses reflection for fragments of separation logic formulas Also uses hint databases (to add simplification rules to Coq database that the Coq auto-tactic can use) Most tactics require that formulas are of certain normal forms Net result is that user mostly has to give method specs and invariants.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 28 / 35

slide-29
SLIDE 29

Case Study: Snapshotable trees [VSTTE-2012]

Algorithm internally uses a lot of sharing. Challenging for separation logic. We used approach based on ramification:

∀ϕ. inv({Tree(this, τ) ⊎ Snap(. . .) ⊎ ϕ})

Suggested fictional separation logic. Development size (after VSTTE): Code spec Code proof Code total

  • Incl. model

pre-tactics 226 3325 3713 5083 post-tactics 156 671 963 2202 So a dramatic reduction in proof size due to tactics, but still non-trivial effort.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 29 / 35

slide-30
SLIDE 30

Some general lessons

For complicated model structures (as needed for snapshotable trees, e.g.) that are not already part of Coq library, most of the effort seems to be on the mathematical model! Observe: unlike most work in prog. lang., we are not only using Coq for meta-theory, but would also like to use the embedded logic. Need more efficient ways of reasoning in embedded logics!

Lars Birkedal (AU) Charge! Types 2013, Toulouse 30 / 35

slide-31
SLIDE 31

Kopitiam — Eclipse plug-in for Java and Coq

Lars Birkedal (AU) Charge! Types 2013, Toulouse 31 / 35

slide-32
SLIDE 32

Selected Ongoing / Future

Extend Charge! with fictional separation logic Extend Charge! to experiment with higher-order frame rules Formalize the specification of Joins library in Charge! Extend type theory with guarded recursion to make it easier to formalize step-indexed models (cf. LICS 2011 and LICS 2013 papers)

Lars Birkedal (AU) Charge! Types 2013, Toulouse 32 / 35

slide-33
SLIDE 33

Advertising!

I recently moved to Aarhus, starting new group in Logic and Semantics. Open position as Associate Professor, announced on types mailing list. Also looking for postdocs and PhD students! Please talk to me later if you know of potentially interested candidates.

Lars Birkedal (AU) Charge! Types 2013, Toulouse 33 / 35

slide-34
SLIDE 34

References

  • J. Bengtson, J.B. Jensen, F

. Sieczkowski, and L. Birkedal: Verifying object-oriented programs with higher-order separation logic in Coq. [ITP-2011]

  • J. Bengtson, J.B. Jensen, and L. Birkedal: Charge! a framework

for higher-order separation logic in Coq. [ITP-2012]

  • H. Mehnert, F

. Sieczkowski, L. Birkedal, and P . Sestoft: Formalized verification of snapshotable trees: Separation and

  • sharing. [VSTTE-2012]

Lars Birkedal (AU) Charge! Types 2013, Toulouse 34 / 35