3.6. Separation Logic

Hoare logic lets us reason about imperative programs by means of triples \{P\}\ c\ \{Q\}. It works very well for languages such as IMP, in which every variable has a name and two distinct variables never denote the same memory location. In low-level programs, however, memory is accessed through computed addresses. The instruction STORE R1 R0 writes to the location whose address is in R1; if another register, say R2, holds the same address, the write also changes whatever "is pointed to by" R2. This phenomenon is known as aliasing, and it makes classical Hoare logic very laborious: after every write we have to prove that all the other facts about memory still hold, which requires knowing, for every pair of addresses, whether or not they coincide.

Separation logic, proposed by John Reynolds and Peter O'Hearn in the early 2000s, solves this problem by changing the meaning of assertions. Instead of describing the whole memory, an assertion describes only a fragment of it, namely the one the program owns. The central connective is the separating conjunction P \ast Q, which states that memory can be split into two disjoint parts, one satisfying P and the other satisfying Q. With it, the absence of aliasing becomes part of the assertion itself, and we obtain the famous frame rule:

\frac{\{P\}\ c\ \{Q\}}{\{P \ast R\}\ c\ \{Q \ast R\}}\,(\textsc{Frame})

The rule says that if c works correctly using only the memory described by P, then it keeps working when more memory (R) is available, and that this extra memory is left unchanged. This enables local reasoning: we specify and verify each piece of code by mentioning only the memory it uses.

In this chapter we build separation logic for the assembly language of the previous chapter. We begin with the model of heaps, define the assertions, and prove their algebraic properties; we then connect the assertions to the operational semantics by means of Hoare triples, prove local specifications for LOAD and STORE, and prove a version of the frame rule. In the next chapter we will see a second formalization, in the style of weakest preconditions, in which the frame rule is proved in full generality.

3.6.1. The heap model

We will use all the definitions of the previous chapter (registers, instructions, Ch21.MachineState, Ch21.step, and Ch21.Reaches), which we bring into scope by opening the Ch21 namespace.

A heap is a partial map from addresses to values. It is the same type we used for the machine's memory, but read differently: a heap represents a fragment of memory, and the addresses at which it is none are those about which the fragment says nothing. The empty heap has no addresses at all.

namespace Ch22 open Ch21 /-- A heap as a partial map from addresses -/ abbrev Heap := Nat → Option Int /-- Empty heap -/ def Heap.emp : Heap := fun _ => none

Two operations on heaps are essential. Two heaps are disjoint when no address belongs to both. The union of two heaps combines their addresses; if both define the same address, the definition gives precedence to the left heap, but we will only use union for disjoint heaps, in which case this choice is irrelevant.

/-- Two heaps are disjoint when their domains do not overlap -/ def Heap.disjoint (h1 h2 : Heap) : Prop := ∀ a, h1 a = none ∨ h2 a = none /-- Union of two heaps (h1 takes precedence) -/ def Heap.union (h1 h2 : Heap) : Heap := fun a => match h1 a with | some v => some v | none => h2 a

Mathematically, we write h_1 \perp h_2 for disjointness and h_1 \uplus h_2 for the union of disjoint heaps.

3.6.1.1. Properties of union

The following lemmas describe how to read an address in a union: if it is in the left heap, the left value applies; otherwise, the right one does. We also characterize when an address is absent from the union.

theorem Heap.union_left {h1 h2 : Heap} {a : Nat} {v : Int} (h : h1 a = some v) : Heap.union h1 h2 a = some v := h1:Heaph2:Heapa:ℕv:ℤh:h1 a = some v⊢ h1.union h2 a = some v All goals completed! 🐙 theorem Heap.union_right {h1 h2 : Heap} {a : Nat} (h : h1 a = none) : Heap.union h1 h2 a = h2 a := h1:Heaph2:Heapa:ℕh:h1 a = none⊢ h1.union h2 a = h2 a All goals completed! 🐙 theorem Heap.union_eq_none {h1 h2 : Heap} {a : Nat} : Heap.union h1 h2 a = none ↔ h1 a = none ∧ h2 a = none := h1:Heaph2:Heapa:ℕ⊢ h1.union h2 a = none ↔ h1 a = none ∧ h2 a = none h1:Heaph2:Heapa:ℕ⊢ (match h1 a with | some v => some v | none => h2 a) = none ↔ h1 a = none ∧ h2 a = none h1:Heaph2:Heapa:ℕ⊢ (match none with | some v => some v | none => h2 a) = none ↔ none = none ∧ h2 a = noneh1:Heaph2:Heapa:ℕval:ℤ⊢ (match some val with | some v => some v | none => h2 a) = none ↔ some val = none ∧ h2 a = none h1:Heaph2:Heapa:ℕ⊢ (match none with | some v => some v | none => h2 a) = none ↔ none = none ∧ h2 a = noneh1:Heaph2:Heapa:ℕval:ℤ⊢ (match some val with | some v => some v | none => h2 a) = none ↔ some val = none ∧ h2 a = none All goals completed! 🐙

The empty heap is a neutral element for union and is disjoint from every heap. The proofs use function extensionality (funext): to show that two heaps are equal, we show that they agree at every address.

theorem Heap.union_emp_l (h : Heap) : Heap.union Heap.emp h = h := h:Heap⊢ emp.union h = h h:Heapa:ℕ⊢ emp.union h a = h a; All goals completed! 🐙 theorem Heap.union_emp_r (h : Heap) : Heap.union h Heap.emp = h := h:Heap⊢ h.union emp = h h:Heapa:ℕ⊢ h.union emp a = h a; h:Heapa:ℕ⊢ (match h a with | some v => some v | none => none) = h a h:Heapa:ℕ⊢ (match none with | some v => some v | none => none) = noneh:Heapa:ℕval:ℤ⊢ (match some val with | some v => some v | none => none) = some val h:Heapa:ℕ⊢ (match none with | some v => some v | none => none) = noneh:Heapa:ℕval:ℤ⊢ (match some val with | some v => some v | none => none) = some val All goals completed! 🐙 theorem Heap.disjoint_emp_l (h : Heap) : Heap.disjoint Heap.emp h := fun _ => Or.inl rfl theorem Heap.disjoint_emp_r (h : Heap) : Heap.disjoint h Heap.emp := fun _ => Or.inr rfl theorem Heap.disjoint_comm {h1 h2 : Heap} (hd : Heap.disjoint h1 h2) : Heap.disjoint h2 h1 := fun a => (hd a).symm

Union is always associative, but it is commutative only for disjoint heaps: if both define address a with different values, the order of the operands determines which value wins. In the proof of commutativity, we analyze, at each address, which of the two heaps is undefined.

theorem Heap.union_comm {h1 h2 : Heap} (hd : Heap.disjoint h1 h2) : Heap.union h1 h2 = Heap.union h2 h1 := h1:Heaph2:Heaphd:h1.disjoint h2⊢ h1.union h2 = h2.union h1 h1:Heaph2:Heaphd:h1.disjoint h2a:ℕ⊢ h1.union h2 a = h2.union h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕ⊢ (match h1 a with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = none⊢ (match h1 a with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => h1 ah1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = none⊢ (match h1 a with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = none⊢ (match h1 a with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => h1 ah1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = none⊢ (match h1 a with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = none⊢ (match h1 a with | some v => some v | none => none) = match none with | some v => some v | none => h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = none⊢ (match none with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = none⊢ (match h1 a with | some v => some v | none => none) = match none with | some v => some v | none => h1 a h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneh':h1 a = none⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some val⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some val h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneh':h1 a = none⊢ (match none with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneval:ℤh':h1 a = some val⊢ (match none with | some v => some v | none => h2 a) = match h2 a with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneh':h1 a = none⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some val⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some val h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some valh'':h2 a = none⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some valh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some valval_1:ℤh'':h2 a = some val_1⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some val h1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneh':h1 a = noneh'':h2 a = none⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneh':h1 a = noneval:ℤh'':h2 a = some val⊢ (match none with | some v => some v | none => some val) = match some val with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneval:ℤh':h1 a = some valh'':h2 a = none⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h1 a = noneval:ℤh':h1 a = some valval_1:ℤh'':h2 a = some val_1⊢ (match none with | some v => some v | none => some val_1) = match some val_1 with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneh':h1 a = noneh'':h2 a = none⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneh':h1 a = noneval:ℤh'':h2 a = some val⊢ (match none with | some v => some v | none => none) = match none with | some v => some v | none => noneh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some valh'':h2 a = none⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some valh1:Heaph2:Heaphd:h1.disjoint h2a:ℕh:h2 a = noneval:ℤh':h1 a = some valval_1:ℤh'':h2 a = some val_1⊢ (match some val with | some v => some v | none => none) = match none with | some v => some v | none => some val All goals completed! 🐙 theorem Heap.union_assoc (h1 h2 h3 : Heap) : Heap.union (Heap.union h1 h2) h3 = Heap.union h1 (Heap.union h2 h3) := h1:Heaph2:Heaph3:Heap⊢ (h1.union h2).union h3 = h1.union (h2.union h3) h1:Heaph2:Heaph3:Heapa:ℕ⊢ (h1.union h2).union h3 a = h1.union (h2.union h3) a h1:Heaph2:Heaph3:Heapa:ℕ⊢ (match match h1 a with | some v => some v | none => h2 a with | some v => some v | none => h3 a) = match h1 a with | some v => some v | none => match h2 a with | some v => some v | none => h3 a h1:Heaph2:Heaph3:Heapa:ℕ⊢ (match match none with | some v => some v | none => h2 a with | some v => some v | none => h3 a) = match none with | some v => some v | none => match h2 a with | some v => some v | none => h3 ah1:Heaph2:Heaph3:Heapa:ℕval:ℤ⊢ (match match some val with | some v => some v | none => h2 a with | some v => some v | none => h3 a) = match some val with | some v => some v | none => match h2 a with | some v => some v | none => h3 a h1:Heaph2:Heaph3:Heapa:ℕ⊢ (match match none with | some v => some v | none => h2 a with | some v => some v | none => h3 a) = match none with | some v => some v | none => match h2 a with | some v => some v | none => h3 ah1:Heaph2:Heaph3:Heapa:ℕval:ℤ⊢ (match match some val with | some v => some v | none => h2 a with | some v => some v | none => h3 a) = match some val with | some v => some v | none => match h2 a with | some v => some v | none => h3 a All goals completed! 🐙

Finally, we need to know how disjointness interacts with union: a heap is disjoint from h_1 \uplus h_2 if and only if it is disjoint from each of the parts. These two lemmas will be the key to the proof of associativity of the separating conjunction.

theorem Heap.disjoint_union_left {h1 h2 h3 : Heap} : Heap.disjoint (Heap.union h1 h2) h3 ↔ Heap.disjoint h1 h3 ∧ Heap.disjoint h2 h3 := h1:Heaph2:Heaph3:Heap⊢ (h1.union h2).disjoint h3 ↔ h1.disjoint h3 ∧ h2.disjoint h3 h1:Heaph2:Heaph3:Heap⊢ (h1.union h2).disjoint h3 → h1.disjoint h3 ∧ h2.disjoint h3h1:Heaph2:Heaph3:Heap⊢ h1.disjoint h3 ∧ h2.disjoint h3 → (h1.union h2).disjoint h3 h1:Heaph2:Heaph3:Heap⊢ (h1.union h2).disjoint h3 → h1.disjoint h3 ∧ h2.disjoint h3 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3⊢ h1.disjoint h3 ∧ h2.disjoint h3 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3⊢ h1.disjoint h3h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3⊢ h2.disjoint h3 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3⊢ h1.disjoint h3h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3⊢ h2.disjoint h3 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕ⊢ h2 a = none ∨ h3 a = none h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕ⊢ h1 a = none ∨ h3 a = noneh1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕ⊢ h2 a = none ∨ h3 a = none h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h1.union h2 a = none⊢ h2 a = none ∨ h3 a = noneh1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h3 a = none⊢ h2 a = none ∨ h3 a = none h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h1.union h2 a = none⊢ h1 a = none ∨ h3 a = none All goals completed! 🐙 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h3 a = none⊢ h1 a = none ∨ h3 a = none All goals completed! 🐙 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h1.union h2 a = none⊢ h2 a = none ∨ h3 a = none All goals completed! 🐙 h1:Heaph2:Heaph3:Heaphd:(h1.union h2).disjoint h3a:ℕh:h3 a = none⊢ h2 a = none ∨ h3 a = none All goals completed! 🐙 h1:Heaph2:Heaph3:Heap⊢ h1.disjoint h3 ∧ h2.disjoint h3 → (h1.union h2).disjoint h3 h1:Heaph2:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕ⊢ h1.union h2 a = none ∨ h3 a = none h1✝:Heaph2:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = none⊢ h1.union h2 a = none ∨ h3 a = noneh1✝:Heaph2:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h3 a = none⊢ h1.union h2 a = none ∨ h3 a = none h1✝:Heaph2:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = none⊢ h1.union h2 a = none ∨ h3 a = none h1✝:Heaph2✝:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = noneh2:h2✝ a = none⊢ h1.union h2 a = none ∨ h3 a = noneh1✝:Heaph2✝:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = noneh2:h3 a = none⊢ h1.union h2 a = none ∨ h3 a = none h1✝:Heaph2✝:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = noneh2:h2✝ a = none⊢ h1.union h2 a = none ∨ h3 a = none All goals completed! 🐙 h1✝:Heaph2✝:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h1✝ a = noneh2:h3 a = none⊢ h1.union h2 a = none ∨ h3 a = none All goals completed! 🐙 h1✝:Heaph2:Heaph3:Heaphd1:h1.disjoint h3hd2:h2.disjoint h3a:ℕh1:h3 a = none⊢ h1.union h2 a = none ∨ h3 a = none All goals completed! 🐙 theorem Heap.disjoint_union_right {h1 h2 h3 : Heap} : Heap.disjoint h1 (Heap.union h2 h3) ↔ Heap.disjoint h1 h2 ∧ Heap.disjoint h1 h3 := h1:Heaph2:Heaph3:Heap⊢ h1.disjoint (h2.union h3) ↔ h1.disjoint h2 ∧ h1.disjoint h3 h1:Heaph2:Heaph3:Heap⊢ h1.disjoint (h2.union h3) → h1.disjoint h2 ∧ h1.disjoint h3h1:Heaph2:Heaph3:Heap⊢ h1.disjoint h2 ∧ h1.disjoint h3 → h1.disjoint (h2.union h3) h1:Heaph2:Heaph3:Heap⊢ h1.disjoint (h2.union h3) → h1.disjoint h2 ∧ h1.disjoint h3 h1:Heaph2:Heaph3:Heaphd:h1.disjoint (h2.union h3)⊢ h1.disjoint h2 ∧ h1.disjoint h3 h1:Heaph2:Heaph3:Heaphd:h1.disjoint (h2.union h3)this:h2.disjoint h1 ∧ h3.disjoint h1⊢ h1.disjoint h2 ∧ h1.disjoint h3 All goals completed! 🐙 h1:Heaph2:Heaph3:Heap⊢ h1.disjoint h2 ∧ h1.disjoint h3 → h1.disjoint (h2.union h3) h1:Heaph2:Heaph3:Heaphd1:h1.disjoint h2hd2:h1.disjoint h3⊢ h1.disjoint (h2.union h3) All goals completed! 🐙

3.6.2. Separation logic assertions

In classical Hoare logic, an assertion is a predicate on the whole state. In separation logic, assertions that talk about memory are predicates on heaps, that is, on fragments of memory. We call the type of these assertions HProp.

/-- The type of assertions about heaps -/ abbrev HProp := Heap → Prop

The simplest assertion is \mathsf{emp}, satisfied only by the empty heap. The assertion a \mapsto v (read "a points to v") is satisfied by a heap that contains exactly one address, a, whose value is v. It is this exactness that gives separation logic its ownership character: whoever holds a \mapsto v has the exclusive right to read and write location a.

/-- Empty heap -/ def emp : HProp := fun h => h = Heap.emp /-- Address a contains v, and the heap has only that address -/ def pointsTo (a : Nat) (v : Int) : HProp := fun h => h a = some v ∧ ∀ a', a' ≠ a → h a' = none

The separating conjunction P \ast Q holds in a heap h when h can be decomposed into two disjoint parts h_1 and h_2, with P holding in h_1 and Q holding in h_2:

h \models P \ast Q \iff \exists h_1\, h_2.\ h_1 \perp h_2 \ \land\ h = h_1 \uplus h_2 \ \land\ h_1 \models P \ \land\ h_2 \models Q

The separating implication (or magic wand) P \mathrel{-\!\!\ast} Q is the adjoint of the separating conjunction: a heap satisfies it if, whenever it is extended with any disjoint heap satisfying P, the result satisfies Q. Intuitively, P \mathrel{-\!\!\ast} Q describes a heap that is "missing a piece P" in order to satisfy Q.

h \models P \mathrel{-\!\!\ast} Q \iff \forall h'.\ h \perp h' \ \land\ h' \models P \implies h \uplus h' \models Q

We also define the lifting of pure propositions (which say nothing about memory) and existential quantification over assertions.

/-- Separating conjunction: the heap splits into two disjoint parts -/ def sepConj (P Q : HProp) : HProp := fun h => ∃ h1 h2, Heap.disjoint h1 h2 ∧ h = Heap.union h1 h2 ∧ P h1 ∧ Q h2 /-- Magic wand: adding a heap that satisfies P yields Q -/ def sepWand (P Q : HProp) : HProp := fun h => ∀ h', Heap.disjoint h h' → P h' → Q (Heap.union h h') /-- Pure proposition, over the empty heap -/ def pureProp (p : Prop) : HProp := fun h => p ∧ h = Heap.emp /-- Existential quantification over HProp -/ def hexists {α : Type} (P : α → HProp) : HProp := fun h => ∃ x, P x h

We introduce notation for the three main connectives. They are declared scoped, so that they are active only inside this chapter's namespace and do not interfere, for instance, with the use of ↦ in fun x ↦ ... in other chapters.

scoped notation:60 a " ↦ " v => pointsTo a v scoped infixl:55 " ⋆ " => sepConj scoped infixr:50 " -⋆ " => sepWand

3.6.3. Algebraic properties

Separation logic assertions, together with \ast and \mathsf{emp}, form a commutative monoid. We prove the four laws as equalities between assertions, using function extensionality (funext) and propositional extensionality (propext).

The strategy is always the same: we take apart the decomposition on the left-hand side and reassemble a decomposition for the right-hand side. The rfl pattern of rintro is very convenient here: when it meets a hypothesis h = h_1 \uplus h_2, it replaces h with h_1 \uplus h_2 throughout the context.

For the unit laws, the decomposition of \mathsf{emp} \ast P is necessarily \emptyset \uplus h:

-- emp is a left unit of ⋆ theorem sepConj_emp_l (P : HProp) : (emp ⋆ P) = P := P:HProp⊢ emp ⋆ P = P P:HProph:Heap⊢ (emp ⋆ P) h = P h P:HProph:Heap⊢ (emp ⋆ P) h ↔ P h P:HProph:Heap⊢ (emp ⋆ P) h → P hP:HProph:Heap⊢ P h → (emp ⋆ P) h P:HProph:Heap⊢ (emp ⋆ P) h → P h P:HProph2:Heaphp:P h2left:Heap.emp.disjoint h2⊢ P (Heap.emp.union h2) P:HProph2:Heaphp:P h2left:Heap.emp.disjoint h2⊢ P h2; All goals completed! 🐙 P:HProph:Heap⊢ P h → (emp ⋆ P) h P:HProph:Heaphp:P h⊢ (emp ⋆ P) h All goals completed! 🐙 -- emp is a right unit of ⋆ theorem sepConj_emp_r (P : HProp) : (P ⋆ emp) = P := P:HProp⊢ P ⋆ emp = P P:HProph:Heap⊢ (P ⋆ emp) h = P h P:HProph:Heap⊢ (P ⋆ emp) h ↔ P h P:HProph:Heap⊢ (P ⋆ emp) h → P hP:HProph:Heap⊢ P h → (P ⋆ emp) h P:HProph:Heap⊢ (P ⋆ emp) h → P h P:HProph1:Heaphp:P h1left:h1.disjoint Heap.emp⊢ P (h1.union Heap.emp) P:HProph1:Heaphp:P h1left:h1.disjoint Heap.emp⊢ P h1; All goals completed! 🐙 P:HProph:Heap⊢ P h → (P ⋆ emp) h P:HProph:Heaphp:P h⊢ (P ⋆ emp) h All goals completed! 🐙

Commutativity uses the fact that the union of disjoint heaps is commutative; it suffices to swap the two parts of the decomposition.

-- ⋆ is commutative theorem sepConj_comm (P Q : HProp) : (P ⋆ Q) = (Q ⋆ P) := P:HPropQ:HProp⊢ P ⋆ Q = Q ⋆ P P:HPropQ:HProph:Heap⊢ (P ⋆ Q) h = (Q ⋆ P) h P:HPropQ:HProph:Heap⊢ (P ⋆ Q) h ↔ (Q ⋆ P) h P:HPropQ:HProph:Heap⊢ (P ⋆ Q) h → (Q ⋆ P) hP:HPropQ:HProph:Heap⊢ (Q ⋆ P) h → (P ⋆ Q) h P:HPropQ:HProph:Heap⊢ (P ⋆ Q) h → (Q ⋆ P) h P:HPropQ:HProph1:Heaph2:Heaphd:h1.disjoint h2hp:P h1hq:Q h2⊢ (Q ⋆ P) (h1.union h2) All goals completed! 🐙 P:HPropQ:HProph:Heap⊢ (Q ⋆ P) h → (P ⋆ Q) h P:HPropQ:HProph1:Heaph2:Heaphd:h1.disjoint h2hq:Q h1hp:P h2⊢ (P ⋆ Q) (h1.union h2) All goals completed! 🐙

Associativity is the most laborious. Starting from (h_1 \uplus h_2) \uplus h_3 with h_1 \perp h_2 and (h_1 \uplus h_2) \perp h_3, we need to regroup it as h_1 \uplus (h_2 \uplus h_3) and prove the new disjointness conditions, h_2 \perp h_3 and h_1 \perp (h_2 \uplus h_3). The lemmas Heap.disjoint_union_left and Heap.disjoint_union_right turn these conditions into facts about pairs of heaps, and the rest is rearrangement.

-- ⋆ is associative theorem sepConj_assoc (P Q R : HProp) : ((P ⋆ Q) ⋆ R) = (P ⋆ (Q ⋆ R)) := P:HPropQ:HPropR:HProp⊢ P ⋆ Q ⋆ R = P ⋆ (Q ⋆ R) P:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ Q ⋆ R) h = (P ⋆ (Q ⋆ R)) h P:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ Q ⋆ R) h ↔ (P ⋆ (Q ⋆ R)) h P:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ Q ⋆ R) h → (P ⋆ (Q ⋆ R)) hP:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ (Q ⋆ R)) h → (P ⋆ Q ⋆ R) h P:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ Q ⋆ R) h → (P ⋆ (Q ⋆ R)) h P:HPropQ:HPropR:HProph3:Heaphr:R h3h1:Heaph2:Heaphd12:h1.disjoint h2hd:(h1.union h2).disjoint h3hp:P h1hq:Q h2⊢ (P ⋆ (Q ⋆ R)) ((h1.union h2).union h3) P:HPropQ:HPropR:HProph3:Heaphr:R h3h1:Heaph2:Heaphd12:h1.disjoint h2hd:h1.disjoint h3 ∧ h2.disjoint h3hp:P h1hq:Q h2⊢ (P ⋆ (Q ⋆ R)) ((h1.union h2).union h3) All goals completed! 🐙 P:HPropQ:HPropR:HProph:Heap⊢ (P ⋆ (Q ⋆ R)) h → (P ⋆ Q ⋆ R) h P:HPropQ:HPropR:HProph1:Heaphp:P h1h2:Heaph3:Heaphd23:h2.disjoint h3hd:h1.disjoint (h2.union h3)hq:Q h2hr:R h3⊢ (P ⋆ Q ⋆ R) (h1.union (h2.union h3)) P:HPropQ:HPropR:HProph1:Heaphp:P h1h2:Heaph3:Heaphd23:h2.disjoint h3hd:h1.disjoint h2 ∧ h1.disjoint h3hq:Q h2hr:R h3⊢ (P ⋆ Q ⋆ R) (h1.union (h2.union h3)) All goals completed! 🐙

The separating conjunction is also monotone in both arguments: we can weaken each component separately. This property is what allows us to use the rule of consequence "inside" a \ast.

theorem sepConj_mono {P P' Q Q' : HProp} (hP : ∀ h, P h → P' h) (hQ : ∀ h, Q h → Q' h) : ∀ h, (P ⋆ Q) h → (P' ⋆ Q') h := P:HPropP':HPropQ:HPropQ':HProphP:∀ (h : Heap), P h → P' hhQ:∀ (h : Heap), Q h → Q' h⊢ ∀ (h : Heap), (P ⋆ Q) h → (P' ⋆ Q') h P:HPropP':HPropQ:HPropQ':HProphP:∀ (h : Heap), P h → P' hhQ:∀ (h : Heap), Q h → Q' hh:Heaph1:Heaph2:Heaphd:h1.disjoint h2hu:h = h1.union h2hp:P h1hq:Q h2⊢ (P' ⋆ Q') h All goals completed! 🐙

3.6.3.1. The separating implication

The magic wand is to \ast what implication is to conjunction. In particular, a form of modus ponens holds: if we have a heap with P and, separately, a heap that is missing P in order to satisfy Q, then the whole satisfies Q.

\frac{}{P \ast (P \mathrel{-\!\!\ast} Q) \vdash Q} \qquad \frac{R \ast P \vdash Q}{R \vdash P \mathrel{-\!\!\ast} Q}

The rule on the right is wand introduction, analogous to implication introduction. The proofs follow directly from the definitions.

theorem wand_elim (P Q : HProp) (h : Heap) : (P ⋆ (P -⋆ Q)) h → Q h := P:HPropQ:HProph:Heap⊢ (P ⋆ (P -⋆ Q)) h → Q h P:HPropQ:HProph1:Heaph2:Heaphd:h1.disjoint h2hp:P h1hw:(P -⋆ Q) h2⊢ Q (h1.union h2) P:HPropQ:HProph1:Heaph2:Heaphd:h1.disjoint h2hp:P h1hw:(P -⋆ Q) h2⊢ Q (h2.union h1) All goals completed! 🐙 theorem wand_intro (P Q R : HProp) (himp : ∀ h, (R ⋆ P) h → Q h) : ∀ h, R h → (P -⋆ Q) h := P:HPropQ:HPropR:HProphimp:∀ (h : Heap), (R ⋆ P) h → Q h⊢ ∀ (h : Heap), R h → (P -⋆ Q) h intro h P:HPropQ:HPropR:HProphimp:∀ (h : Heap), (R ⋆ P) h → Q hh:Heaphr:R h⊢ (P -⋆ Q) h P:HPropQ:HPropR:HProphimp:∀ (h : Heap), (R ⋆ P) h → Q hh:Heaphr:R hh':Heap⊢ h.disjoint h' → P h' → Q (h.union h') P:HPropQ:HPropR:HProphimp:∀ (h : Heap), (R ⋆ P) h → Q hh:Heaphr:R hh':Heaphd:h.disjoint h'⊢ P h' → Q (h.union h') P:HPropQ:HPropR:HProphimp:∀ (h : Heap), (R ⋆ P) h → Q hh:Heaphr:R hh':Heaphd:h.disjoint h'hp:P h'⊢ Q (h.union h') All goals completed! 🐙

3.6.3.2. Properties of points-to

If a heap satisfies a \mapsto v, then reading it at a returns v, and the value pointed to is unique.

theorem pointsTo_lookup (a : Nat) (v : Int) (h : Heap) (hp : (a ↦ v) h) : h a = some v := hp.1 theorem pointsTo_unique (a : Nat) (v w : Int) (h : Heap) (hv : (a ↦ v) h) (hw : (a ↦ w) h) : v = w := a:ℕv:ℤw:ℤh:Heaphv:(a ↦ v) hhw:(a ↦ w) h⊢ v = w a:ℕv:ℤw:ℤh:Heaphv:(a ↦ v) hhw:(a ↦ w) hh1:h a = some v⊢ v = w a:ℕv:ℤw:ℤh:Heaphv:(a ↦ v) hhw:(a ↦ w) hh1:some w = some v⊢ v = w All goals completed! 🐙

The next lemma shows how the separating conjunction lets us read two cells at once: in a heap satisfying (a \mapsto v) \ast (b \mapsto w), address a contains v and address b contains w. Note that we did not need to assume a \neq b.

theorem pointsTo_sep (a b : Nat) (v w : Int) (h : Heap) (hs : ((a ↦ v) ⋆ (b ↦ w)) h) : h a = some v ∧ h b = some w := a:ℕb:ℕv:ℤw:ℤh:Heaphs:((a ↦ v) ⋆ b ↦ w) h⊢ h a = some v ∧ h b = some w a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ h1.union h2 a = some v ∧ h1.union h2 b = some w a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ h1.union h2 a = some va:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ h1.union h2 b = some w a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ h1.union h2 a = some v All goals completed! 🐙 a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ h1.union h2 b = some w have h1b : h1 b = none := a:ℕb:ℕv:ℤw:ℤh:Heaphs:((a ↦ v) ⋆ b ↦ w) h⊢ h a = some v ∧ h b = some w a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh:h1 b = none⊢ h1 b = nonea:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh:h2 b = none⊢ h1 b = none a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh:h1 b = none⊢ h1 b = none All goals completed! 🐙 a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh:h2 b = none⊢ h1 b = none a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh:some w = none⊢ h1 b = none; All goals completed! 🐙 a:ℕb:ℕv:ℤw:ℤh1:Heaph2:Heaphd:h1.disjoint h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = noneh1b:h1 b = none⊢ h2 b = some w; All goals completed! 🐙

In fact, the distinctness of the addresses is a consequence of the assertion: if (a \mapsto v) \ast (b \mapsto w) is satisfied, then a \neq b. This is the essential point of separation logic. In classical Hoare logic we would have to carry the hypothesis a \neq b explicitly in every specification; here it is built into the \ast connective.

theorem pointsTo_sep_ne (a b : Nat) (v w : Int) (h : Heap) (hs : ((a ↦ v) ⋆ (b ↦ w)) h) : a ≠ b := a:ℕb:ℕv:ℤw:ℤh:Heaphs:((a ↦ v) ⋆ b ↦ w) h⊢ a ≠ b a:ℕb:ℕv:ℤw:ℤh:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = none⊢ a ≠ b a:ℕb:ℕv:ℤw:ℤh:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 b = some wright:∀ (a' : ℕ), a' ≠ b → h2 a' = nonehab:a = b⊢ False a:ℕv:ℤw:ℤh:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = none⊢ False a:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:h1 a = none⊢ Falsea:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:h2 a = none⊢ False a:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:h1 a = none⊢ False a:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:some v = none⊢ False; All goals completed! 🐙 a:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:h2 a = none⊢ False a:ℕv:ℤw:ℤh✝:Heaph1:Heaph2:Heaphd:h1.disjoint h2left:h = h1.union h2hv:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = nonehw:h2 a = some wright:∀ (a' : ℕ), a' ≠ a → h2 a' = noneh:some w = none⊢ False; All goals completed! 🐙

3.6.4. Hoare triples for assembly

To connect assertions to the semantics, we need assertions about complete machine states, which may also mention registers and the PC. A heap assertion is lifted to a state assertion by applying it to the memory.

/-- Lifts a heap assertion to an assertion about states -/ def liftHP (P : HProp) : MachineState → Prop := fun s => P s.mem

A triple \{P\}\ \mathit{prog} @ pc\ \{Q\} asserts the partial correctness of the program starting from program counter pc: for every state s whose PC equals pc and which satisfies P, if execution reaches a terminal state s' (that is, a state from which the machine takes no further steps), then s' satisfies Q. As throughout the previous chapter, "terminal" includes both HALT and faults.

/-- Hoare triple (partial correctness) starting from pc_start -/ def HoareTriple (prog : Program) (pc_start : Nat) (P Q : MachineState → Prop) : Prop := ∀ s s', s.pc = pc_start → P s → Reaches prog s s' → step prog s' = none → -- s' is terminal Q s'

We use the notation ⟪ P ⟫ prog @ pc ⟪ Q ⟫ for triples.The usual braces { P } clash with Lean's syntax for structures and sets, which is why we use ⟪ ⟫.

scoped notation:40 "⟪" P "⟫ " prog:max " @ " pc:max " ⟪" Q "⟫" => HoareTriple prog pc P Q

The rule of consequence holds as in any Hoare logic, and its proof is immediate from the definition.

\frac{P \Rightarrow P' \qquad \{P'\}\ \mathit{prog} @ pc\ \{Q'\} \qquad Q' \Rightarrow Q}{\{P\}\ \mathit{prog} @ pc\ \{Q\}}\,(\textsc{Cons})

theorem hoare_consequence (prog : Program) (pc : Nat) (P P' Q Q' : MachineState → Prop) (hPP' : ∀ s, P s → P' s) (hQ'Q : ∀ s, Q' s → Q s) (h : ⟪P'⟫ prog @ pc ⟪Q'⟫) : ⟪P⟫ prog @ pc ⟪Q⟫ := prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫⊢ ⟪P⟫ prog @ pc ⟪Q⟫ intro s prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫s:MachineStates':MachineState⊢ s.pc = pc → P s → Reaches prog s s' → step prog s' = none → Q s' prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫s:MachineStates':MachineStatehpc:s.pc = pc⊢ P s → Reaches prog s s' → step prog s' = none → Q s' prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫s:MachineStates':MachineStatehpc:s.pc = pchp:P s⊢ Reaches prog s s' → step prog s' = none → Q s' prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫s:MachineStates':MachineStatehpc:s.pc = pchp:P shr:Reaches prog s s'⊢ step prog s' = none → Q s' prog:Programpc:ℕP:MachineState → PropP':MachineState → PropQ:MachineState → PropQ':MachineState → ProphPP':∀ (s : MachineState), P s → P' shQ'Q:∀ (s : MachineState), Q' s → Q sh:⟪P'⟫ prog @ pc ⟪Q'⟫s:MachineStates':MachineStatehpc:s.pc = pchp:P shr:Reaches prog s s'hterminal:step prog s' = none⊢ Q s' All goals completed! 🐙

3.6.4.1. A first example

Before using separation logic, let us see how a triple can be proved directly from the semantics. We show that Ch21.prog_add ends with R0 = 7, whatever the initial state (as long as the PC is zero).

The proof strategy is to symbolically execute the program. We do case analysis on the Ch21.Reaches derivation: in the refl case, the initial state would be terminal, which contradicts the fact that there is an instruction at PC 0; in the next case, simp computes the next state and we substitute it into the context. We repeat the process until we reach the HALT.

theorem prog_add_spec : ⟪ fun s => s.pc = 0 ⟫ prog_add @ 0 ⟪ fun s' => s'.regs .R0 = 7 ⟫ := ⊢ ⟪fun s => s.pc = 0⟫ prog_add @ 0 ⟪fun s' => s'.regs Reg.R0 = 7⟫ intro s s:MachineStates':MachineState⊢ s.pc = 0 → (fun s => s.pc = 0) s → Reaches prog_add s s' → step prog_add s' = none → (fun s' => s'.regs Reg.R0 = 7) s' s:MachineStates':MachineStatehpc:s.pc = 0⊢ (fun s => s.pc = 0) s → Reaches prog_add s s' → step prog_add s' = none → (fun s' => s'.regs Reg.R0 = 7) s' s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0⊢ Reaches prog_add s s' → step prog_add s' = none → (fun s' => s'.regs Reg.R0 = 7) s' s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hr:Reaches prog_add s s'⊢ step prog_add s' = none → (fun s' => s'.regs Reg.R0 = 7) s' s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hr:Reaches prog_add s s'hterm:step prog_add s' = none⊢ s'.regs Reg.R0 = 7 cases hr with s:MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s = none⊢ s.regs Reg.R0 = 7 All goals completed! 🐙 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet1:MachineStatehs1:step prog_add s = some t1hr1:Reaches prog_add t1 s'⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet1:MachineStatehr1:Reaches prog_add t1 s'hs1:{ regs := updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3)), flags := s.flags, mem := s.mem, pc := 1 } = t1⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonehr1:Reaches prog_add { regs := updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3)), flags := s.flags, mem := s.mem, pc := 1 } s'⊢ s'.regs Reg.R0 = 7 cases hr1 with s:MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add { regs := updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3)), flags := s.flags, mem := s.mem, pc := 1 } = none⊢ { regs := updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3)), flags := s.flags, mem := s.mem, pc := 1 }.regs Reg.R0 = 7 All goals completed! 🐙 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet2:MachineStatehs2:step prog_add { regs := updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3)), flags := s.flags, mem := s.mem, pc := 1 } = some t2hr2:Reaches prog_add t2 s'⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet2:MachineStatehr2:Reaches prog_add t2 s'hs2:{ regs := updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)), flags := s.flags, mem := s.mem, pc := 2 } = t2⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonehr2:Reaches prog_add { regs := updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)), flags := s.flags, mem := s.mem, pc := 2 } s'⊢ s'.regs Reg.R0 = 7 cases hr2 with s:MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add { regs := updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)), flags := s.flags, mem := s.mem, pc := 2 } = none⊢ { regs := updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)), flags := s.flags, mem := s.mem, pc := 2 }.regs Reg.R0 = 7 All goals completed! 🐙 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet3:MachineStatehs3:step prog_add { regs := updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)), flags := s.flags, mem := s.mem, pc := 2 } = some t3hr3:Reaches prog_add t3 s'⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet3:MachineStatehr3:Reaches prog_add t3 s'hs3:{ regs := updateReg (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) Reg.R0 (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), flags := setArithFlags s.flags (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), mem := s.mem, pc := 3 } = t3⊢ s'.regs Reg.R0 = 7 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonehr3:Reaches prog_add { regs := updateReg (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) Reg.R0 (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), flags := setArithFlags s.flags (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), mem := s.mem, pc := 3 } s'⊢ s'.regs Reg.R0 = 7 cases hr3 with s:MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add { regs := updateReg (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) Reg.R0 (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), flags := setArithFlags s.flags (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), mem := s.mem, pc := 3 } = none⊢ { regs := updateReg (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) Reg.R0 (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), flags := setArithFlags s.flags (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), mem := s.mem, pc := 3 }.regs Reg.R0 = 7 All goals completed! 🐙 s:MachineStates':MachineStatehpc:s.pc = 0a:s.pc = 0hterm:step prog_add s' = nonet4:MachineStatehs4:step prog_add { regs := updateReg (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) Reg.R0 (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), flags := setArithFlags s.flags (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4)) Reg.R0 + evalOperand (updateReg (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) Reg.R1 (evalOperand (updateReg s.regs Reg.R0 (evalOperand s.regs (Operand.imm 3))) (Operand.imm 4))) (Operand.reg Reg.R1)), mem := s.mem, pc := 3 } = some t4a_1:Reaches prog_add t4 s'⊢ s'.regs Reg.R0 = 7 All goals completed! 🐙

This technique works for programs without loops and without memory access, but it soon becomes impractical. For instructions that manipulate memory, this is where separation logic comes into play.

3.6.5. Local specifications of LOAD and STORE

The starting point of separation logic is the local specifications (or small axioms) of the instructions that access memory. Each one mentions only the cell being accessed:

\{a \mapsto v \ast R\}\ \mathtt{STORE}\ d\ s\ \{a \mapsto R(s) \ast R\} \qquad (R(d) = a)

\{a \mapsto v \ast R\}\ \mathtt{LOAD}\ d\ s\ \{(a \mapsto v \ast R) \land d = v\} \qquad (R(s) = a)

Here we have already built in an arbitrary frame R: the rest of memory, whatever it is, is not affected by the instruction.

We first prove the version for a single STORE step. We start from a decomposition h_1 \uplus h_2 of memory in which h_1 satisfies a \mapsto v and h_2 satisfies R. After the write, the new memory decomposes as h_1[a \mapsto w] \uplus h_2: the part h_1 has been updated and h_2 has remained intact. For this to work, it is crucial that a does not belong to h_2, which follows from disjointness, since a belongs to h_1. This is exactly the argument that, in classical Hoare logic, would require a non-aliasing hypothesis.

theorem store_local (prog : Program) (s s' : MachineState) (dst src : Reg) (a : Nat) (v : Int) (R : HProp) (hf : fetch prog s.pc = some (.STORE dst src)) (ha : (s.regs dst).toNat = a) (hpre : ((a ↦ v) ⋆ R) s.mem) (hstep : step prog s = some s') : ((a ↦ s.regs src) ⋆ R) s'.mem := prog:Programs:MachineStates':MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ahpre:((a ↦ v) ⋆ R) s.memhstep:step prog s = some s'⊢ ((a ↦ s.regs src) ⋆ R) s'.mem prog:Programs:MachineStates':MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ahpre:((a ↦ v) ⋆ R) s.memhstep:{ regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs dst).toNat (s.regs src), pc := s.pc + 1 } = s'⊢ ((a ↦ s.regs src) ⋆ R) s'.mem prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ahpre:((a ↦ v) ⋆ R) s.mem⊢ ((a ↦ s.regs src) ⋆ R) { regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs dst).toNat (s.regs src), pc := s.pc + 1 }.mem prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = none⊢ ((a ↦ s.regs src) ⋆ R) { regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs dst).toNat (s.regs src), pc := s.pc + 1 }.mem -- a is not in h2, since it is in h1 have h2a : h2 a = none := prog:Programs:MachineStates':MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ahpre:((a ↦ v) ⋆ R) s.memhstep:step prog s = some s'⊢ ((a ↦ s.regs src) ⋆ R) s'.mem prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh:h1 a = none⊢ h2 a = noneprog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh:h2 a = none⊢ h2 a = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh:h1 a = none⊢ h2 a = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh:some v = none⊢ h2 a = none; All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh:h2 a = none⊢ h2 a = none All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ Heap.disjoint (updateMem h1 a (s.regs src)) h2prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ { regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs dst).toNat (s.regs src), pc := s.pc + 1 }.mem = Heap.union (updateMem h1 a (s.regs src)) h2prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ updateMem h1 a (s.regs src) a = some (s.regs src)prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ ∀ (a' : ℕ), a' ≠ a → updateMem h1 a (s.regs src) a' = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ Heap.disjoint (updateMem h1 a (s.regs src)) h2 -- disjointness is preserved prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕ⊢ updateMem h1 a (s.regs src) b = none ∨ h2 b = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:b = a⊢ updateMem h1 a (s.regs src) b = none ∨ h2 b = noneprog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:¬b = a⊢ updateMem h1 a (s.regs src) b = none ∨ h2 b = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:b = a⊢ updateMem h1 a (s.regs src) b = none ∨ h2 b = none prog:Programs:MachineStatedst:Regsrc:Regv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)h1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2b:ℕha:(s.regs dst).toNat = bh1a:h1 b = some vh1o:∀ (a' : ℕ), a' ≠ b → h1 a' = noneh2a:h2 b = none⊢ updateMem h1 b (s.regs src) b = none ∨ h2 b = none; All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:¬b = a⊢ updateMem h1 a (s.regs src) b = none ∨ h2 b = none prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:¬b = a⊢ updateMem h1 a (s.regs src) b = none; All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ { regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs dst).toNat (s.regs src), pc := s.pc + 1 }.mem = Heap.union (updateMem h1 a (s.regs src)) h2 -- the new memory is the union of the parts prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ updateMem (h1.union h2) a (s.regs src) = Heap.union (updateMem h1 a (s.regs src)) h2 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕ⊢ updateMem (h1.union h2) a (s.regs src) b = Heap.union (updateMem h1 a (s.regs src)) h2 b prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:b = a⊢ updateMem (h1.union h2) a (s.regs src) b = Heap.union (updateMem h1 a (s.regs src)) h2 bprog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:¬b = a⊢ updateMem (h1.union h2) a (s.regs src) b = Heap.union (updateMem h1 a (s.regs src)) h2 b prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:b = a⊢ updateMem (h1.union h2) a (s.regs src) b = Heap.union (updateMem h1 a (s.regs src)) h2 b prog:Programs:MachineStatedst:Regsrc:Regv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)h1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2b:ℕha:(s.regs dst).toNat = bh1a:h1 b = some vh1o:∀ (a' : ℕ), a' ≠ b → h1 a' = noneh2a:h2 b = none⊢ updateMem (h1.union h2) b (s.regs src) b = Heap.union (updateMem h1 b (s.regs src)) h2 b; All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:¬b = a⊢ updateMem (h1.union h2) a (s.regs src) b = Heap.union (updateMem h1 a (s.regs src)) h2 b All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ updateMem h1 a (s.regs src) a = some (s.regs src) -- the new part satisfies a ↦ w All goals completed! 🐙 prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = none⊢ ∀ (a' : ℕ), a' ≠ a → updateMem h1 a (s.regs src) a' = none intro b prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.STORE dst src)ha:(s.regs dst).toNat = ah1:Heaph2:Heaphd:h1.disjoint h2hm:s.mem = h1.union h2hR:R h2h1a:h1 a = some vh1o:∀ (a' : ℕ), a' ≠ a → h1 a' = noneh2a:h2 a = noneb:ℕhb:b ≠ a⊢ updateMem h1 a (s.regs src) b = none; All goals completed! 🐙

For LOAD, the local specification guarantees that the read succeeds (the address is allocated, since it belongs to the fragment a \mapsto v) and describes the next state exactly: the destination register receives v and memory does not change.

theorem load_local (prog : Program) (s : MachineState) (dst src : Reg) (a : Nat) (v : Int) (R : HProp) (hf : fetch prog s.pc = some (.LOAD dst src)) (ha : (s.regs src).toNat = a) (hpre : ((a ↦ v) ⋆ R) s.mem) : step prog s = some { s with regs := updateReg s.regs dst v, pc := s.pc + 1 } := prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.LOAD dst src)ha:(s.regs src).toNat = ahpre:((a ↦ v) ⋆ R) s.mem⊢ step prog s = some { regs := updateReg s.regs dst v, flags := s.flags, mem := s.mem, pc := s.pc + 1 } have hv : s.mem a = some v := prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.LOAD dst src)ha:(s.regs src).toNat = ahpre:((a ↦ v) ⋆ R) s.mem⊢ step prog s = some { regs := updateReg s.regs dst v, flags := s.flags, mem := s.mem, pc := s.pc + 1 } prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.LOAD dst src)ha:(s.regs src).toNat = ah1:Heaph2:Heapleft:h1.disjoint h2hm:s.mem = h1.union h2right:R h2h1a:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = none⊢ s.mem a = some v prog:Programs:MachineStatedst:Regsrc:Rega:ℕv:ℤR:HProphf:fetch prog s.pc = some (Instr.LOAD dst src)ha:(s.regs src).toNat = ah1:Heaph2:Heapleft:h1.disjoint h2hm:s.mem = h1.union h2right:R h2h1a:h1 a = some vright_1:∀ (a' : ℕ), a' ≠ a → h1 a' = none⊢ h1.union h2 a = some v; All goals completed! 🐙 All goals completed! 🐙

Note how the precondition a \mapsto v \ast R plays the role of a permission: without it, we could not guarantee that the LOAD does not fail.

3.6.5.1. A triple involving memory

Combining store_local with the symbolic execution technique, we prove a triple for a program that writes to memory. The frame R is arbitrary: the specification holds whatever the rest of memory is, and it guarantees that this rest is not modified.

def prog_store : Program := [.STORE .R1 .R0, .HALT] theorem prog_store_spec (a : Nat) (v w : Int) (R : HProp) : ⟪ fun s => s.regs .R1 = a ∧ s.regs .R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s ⟫ prog_store @ 0 ⟪ liftHP ((a ↦ w) ⋆ R) ⟫ := a:ℕv:ℤw:ℤR:HProp⊢ ⟪fun s => s.regs Reg.R1 = ↑a ∧ s.regs Reg.R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s⟫ prog_store @ 0 ⟪liftHP ((a ↦ w) ⋆ R)⟫ intro s a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineState⊢ s.pc = 0 → (fun s => s.regs Reg.R1 = ↑a ∧ s.regs Reg.R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s) s → Reaches prog_store s s' → step prog_store s' = none → liftHP ((a ↦ w) ⋆ R) s' a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0⊢ (fun s => s.regs Reg.R1 = ↑a ∧ s.regs Reg.R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s) s → Reaches prog_store s s' → step prog_store s' = none → liftHP ((a ↦ w) ⋆ R) s' a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) s⊢ Reaches prog_store s s' → step prog_store s' = none → liftHP ((a ↦ w) ⋆ R) s' a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shr:Reaches prog_store s s'⊢ step prog_store s' = none → liftHP ((a ↦ w) ⋆ R) s' a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shr:Reaches prog_store s s'hterm:step prog_store s' = none⊢ liftHP ((a ↦ w) ⋆ R) s' have hf : fetch prog_store s.pc = some (.STORE .R1 .R0) := a:ℕv:ℤw:ℤR:HProp⊢ ⟪fun s => s.regs Reg.R1 = ↑a ∧ s.regs Reg.R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s⟫ prog_store @ 0 ⟪liftHP ((a ↦ w) ⋆ R)⟫ All goals completed! 🐙 cases hr with a:ℕv:ℤw:ℤR:HProps:MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)hterm:step prog_store s = none⊢ liftHP ((a ↦ w) ⋆ R) s All goals completed! 🐙 a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)s1:MachineStatehs:step prog_store s = some s1hr':Reaches prog_store s1 s'⊢ liftHP ((a ↦ w) ⋆ R) s' have hpost := store_local _ _ _ _ _ a v R hf (a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)s1:MachineStatehs:step prog_store s = some s1hr':Reaches prog_store s1 s'⊢ (s.regs Reg.R1).toNat = a All goals completed! 🐙) hpre hs a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)s1:MachineStatehs:step prog_store s = some s1hr':Reaches prog_store s1 s'hpost:((a ↦ w) ⋆ R) s1.mem⊢ liftHP ((a ↦ w) ⋆ R) s' have hs1pc : s1.pc = 1 := a:ℕv:ℤw:ℤR:HProp⊢ ⟪fun s => s.regs Reg.R1 = ↑a ∧ s.regs Reg.R0 = w ∧ liftHP ((a ↦ v) ⋆ R) s⟫ prog_store @ 0 ⟪liftHP ((a ↦ w) ⋆ R)⟫ a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)s1:MachineStatehr':Reaches prog_store s1 s'hpost:((a ↦ w) ⋆ R) s1.memhs:{ regs := s.regs, flags := s.flags, mem := updateMem s.mem (s.regs Reg.R1).toNat (s.regs Reg.R0), pc := s.pc + 1 } = s1⊢ s1.pc = 1; All goals completed! 🐙 cases hr' with a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)hs:step prog_store s = some s'hpost:((a ↦ w) ⋆ R) s'.memhs1pc:s'.pc = 1⊢ liftHP ((a ↦ w) ⋆ R) s' All goals completed! 🐙 a:ℕv:ℤw:ℤR:HProps:MachineStates':MachineStatehpc:s.pc = 0h1:s.regs Reg.R1 = ↑ah0:s.regs Reg.R0 = whpre:liftHP ((a ↦ v) ⋆ R) shterm:step prog_store s' = nonehf:fetch prog_store s.pc = some (Instr.STORE Reg.R1 Reg.R0)s1:MachineStatehs:step prog_store s = some s1hpost:((a ↦ w) ⋆ R) s1.memhs1pc:s1.pc = 1s2:MachineStatehs2:step prog_store s1 = some s2a_1:Reaches prog_store s2 s'⊢ liftHP ((a ↦ w) ⋆ R) s' All goals completed! 🐙

3.6.6. The frame rule

The frame rule is the central property of separation logic. To prove it in full generality, we need to show that each instruction accesses only the memory addresses its precondition authorizes (the small footprint property). In this section we prove a simpler version that already captures the idea: if a memory assertion R is preserved by every step of the machine, then it can be added to the pre- and postcondition of any triple.

First, an auxiliary lemma: instructions other than STORE do not change memory. The proof is by cases on the fetched instruction. For LOAD we reuse Ch21.step_LOAD_mem from the previous chapter; the STORE case is ruled out by the hypothesis; and all the remaining cases are handled by the same combination of tactics, which we collect with the _ pattern.

theorem step_mem_preserved (prog : Program) (s s' : MachineState) (hstep : step prog s = some s') (hnostore : ∀ dst src, fetch prog s.pc ≠ some (.STORE dst src)) : s'.mem = s.mem := prog:Programs:MachineStates':MachineStatehstep:step prog s = some s'hnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)⊢ s'.mem = s.mem prog:Programs:MachineStates':MachineStatehstep:(fetch prog s.pc).bind (stepInstr s) = some s'hnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)⊢ s'.mem = s.mem cases hfetch : fetch prog s.pc with prog:Programs:MachineStates':MachineStatehstep:(fetch prog s.pc).bind (stepInstr s) = some s'hnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)hfetch:fetch prog s.pc = none⊢ s'.mem = s.mem All goals completed! 🐙 prog:Programs:MachineStates':MachineStatehstep:(fetch prog s.pc).bind (stepInstr s) = some s'hnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)i:Instrhfetch:fetch prog s.pc = some i⊢ s'.mem = s.mem prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)i:Instrhstep:(some i).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some i⊢ s'.mem = s.mem cases i with prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)dst:Regsrc:Reghstep:(some (Instr.LOAD dst src)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.LOAD dst src)⊢ s'.mem = s.mem exact step_LOAD_mem prog s s' dst src hfetch (prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)dst:Regsrc:Reghstep:(some (Instr.LOAD dst src)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.LOAD dst src)⊢ step prog s = some s' All goals completed! 🐙) prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)dst:Regsrc:Reghstep:(some (Instr.STORE dst src)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.STORE dst src)⊢ s'.mem = s.mem All goals completed! 🐙 prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)hstep:(some Instr.HALT).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some Instr.HALT⊢ s'.mem = s.mem All goals completed! 🐙 prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Labelhstep:(some (Instr.JLT a)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.JLT a)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Labelhstep:(some (Instr.JNZ a)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.JNZ a)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Labelhstep:(some (Instr.JZ a)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.JZ a)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Labelhstep:(some (Instr.JMP a)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.JMP a)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Reghstep:(some (Instr.NEG a)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.NEG a)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Rega_1:Operandhstep:(some (Instr.MUL a a_1)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.MUL a a_1)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Rega_1:Operandhstep:(some (Instr.SUB a a_1)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.SUB a a_1)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Rega_1:Operandhstep:(some (Instr.ADD a a_1)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.ADD a a_1)⊢ s'.mem = s.memprog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Rega_1:Operandhstep:(some (Instr.MOV a a_1)).bind (stepInstr s) = some s'hfetch:fetch prog s.pc = some (Instr.MOV a a_1)⊢ s'.mem = s.mem prog:Programs:MachineStates':MachineStatehnostore:∀ (dst src : Reg), fetch prog s.pc ≠ some (Instr.STORE dst src)a:Labelhfetch:fetch prog s.pc = some (Instr.JLT a)hstep:{ regs := s.regs, flags := s.flags, mem := s.mem, pc := if s.flags Flag.SF = s.flags Flag.OF then s.pc + 1 else a } = s'⊢ s'.mem = s.mem; All goals completed! 🐙

The simplified frame rule is then:

\frac{\{P\}\ \mathit{prog} @ pc\ \{Q\} \qquad \forall s\, s'.\ s \to s' \land R(s.\mathit{mem}) \Rightarrow R(s'.\mathit{mem})}{\{P \land R\}\ \mathit{prog} @ pc\ \{Q \land R\}}

The first half of the postcondition comes directly from the original triple. The second half is proved by induction on the Ch21.Reaches derivation: R holds in the initial state and is preserved by every step, so it holds in the final state. Before the induction, we use clear to discard the hypotheses that mention the initial state and are not needed, so that the induction hypothesis has the right shape.

theorem frame_rule_mem (prog : Program) (pc : Nat) (P Q : MachineState → Prop) (R : HProp) (h : ⟪P⟫ prog @ pc ⟪Q⟫) (hframe : ∀ s s', step prog s = some s' → liftHP R s → liftHP R s') : ⟪ fun s => P s ∧ liftHP R s ⟫ prog @ pc ⟪ fun s' => Q s' ∧ liftHP R s' ⟫ := prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s'⊢ ⟪fun s => P s ∧ liftHP R s⟫ prog @ pc ⟪fun s' => Q s' ∧ liftHP R s'⟫ intro s prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineState⊢ s.pc = pc → (fun s => P s ∧ liftHP R s) s → Reaches prog s s' → step prog s' = none → (fun s' => Q s' ∧ liftHP R s') s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pc⊢ (fun s => P s ∧ liftHP R s) s → Reaches prog s s' → step prog s' = none → (fun s' => Q s' ∧ liftHP R s') s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R s⊢ Reaches prog s s' → step prog s' = none → (fun s' => Q s' ∧ liftHP R s') s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'⊢ step prog s' = none → (fun s' => Q s' ∧ liftHP R s') s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'hterminal:step prog s' = none⊢ Q s' ∧ liftHP R s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'hterminal:step prog s' = none⊢ Q s'prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'hterminal:step prog s' = none⊢ liftHP R s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'hterminal:step prog s' = none⊢ Q s' All goals completed! 🐙 prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehpc:s.pc = pchp:P shr:liftHP R shreach:Reaches prog s s'hterminal:step prog s' = none⊢ liftHP R s' prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStatehr:liftHP R shreach:Reaches prog s s'⊢ liftHP R s' induction hreach with prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStates_1:MachineStatehr:liftHP R s_1⊢ liftHP R s_1 All goals completed! 🐙 prog:Programpc:ℕP:MachineState → PropQ:MachineState → PropR:HProph:⟪P⟫ prog @ pc ⟪Q⟫hframe:∀ (s s' : MachineState), step prog s = some s' → liftHP R s → liftHP R s's:MachineStates':MachineStates0:MachineStatesmid:MachineStates0':MachineStatehst:step prog s0 = some smida:Reaches prog smid s0'ih:liftHP R smid → liftHP R s0'hr:liftHP R s0⊢ liftHP R s0' All goals completed! 🐙

This version uses ordinary conjunction \land and shifts to the user the obligation of proving that R is preserved. The real frame rule, with \ast, dispenses with this hypothesis: preservation of the frame is a consequence of the fact that the program, having been verified with precondition P, accesses only the memory described by P. Proving this requires keeping track, throughout execution, of which part of memory is "owned" by the program. In the next chapter we do exactly that, for a formulation of triples as a rule system in the style of weakest preconditions.

end Ch22

3.6.7. Exercises

1. Prove that pureProp behaves as expected:
   (pureProp p ⋆ P) h ↔ p ∧ P h.
2. Prove that the separating conjunction of two cells with the same
   address is unsatisfiable:
   ¬ ((a ↦ v) ⋆ (a ↦ w)) h.
   (Hint: use pointsTo_sep_ne.)
3. Prove monotonicity of the magic wand:
   (∀ h, P' h → P h) → (∀ h, Q h → Q' h) →
   ∀ h, (P -⋆ Q) h → (P' -⋆ Q') h.
   Note that it is contravariant in its first argument.
4. Prove that ⋆ distributes over the existential:
   (hexists (fun x => P x) ⋆ Q) h ↔ hexists (fun x => P x ⋆ Q) h.
5. Prove a version of store_local without a frame, that is, with R = emp:
   if memory satisfies a ↦ v and the PC points to STORE dst src with
   (s.regs dst).toNat = a, then after the step memory satisfies
   a ↦ s.regs src. (Hint: use sepConj_emp_r.)
6. Prove the specification of prog_loadstore (previous chapter):
   ⟪ fun s => s.pc = 0 ⟫ prog_loadstore @ 0
   ⟪ fun s' => s'.regs .R2 = 42 ∧ s'.mem 100 = some 42 ⟫
   using the symbolic execution technique of prog_add_spec.
7. Consider the program [.LOAD .R2 .R1, .HALT]. State and prove a
   triple whose precondition is
   s.regs .R1 = a ∧ liftHP ((a ↦ v) ⋆ R) s
   and whose postcondition states that R2 = v and that memory still
   satisfies (a ↦ v) ⋆ R. Use load_local.