Ad-hoc Frame Conditions are Logical, Separately

Posted on November 9, 2015

The main direction of my research thus far has been to exploit linear types to make purely functional, equational reasoning possible for systems programming. I’ve talked about the difficulty of verifying imperative programs before, but my past entries about linear types did not discuss the advantages that they can bring to such a domain.

Chiefly, they allow us to reason about our program as if all data structures are immutable, with all of the benefits that implies, while the actual implementation performs efficient destructive updates to mutable data structures. This is achieved simply by statically ruling out every program where the difference between the immutable and the mutable interpretations can be observed, by requiring that every variable that refers, directly or indirectly, to a heap data structure, must be used exactly once. As variables cannot be used multiple times, this implies that for any allocated heap object, there is exactly one live, usable reference that exists at any one time. This is called the uniqueness property of linear types.

This is a very simple restriction, but it proves a considerable burden when trying to actually write programs. For example, a naïve definition of a linear array would become unusable after just one element was accessed! Other data structures, with complex reference layouts that involve multiple aliasing references and sharing, simply cannot be expressed.

For this reason, when designing the linear systems language Cogent for my research, I allowed parts of the program to be written in unsafe, imperative C, and those C snippets are able to manipulate opaque types that are abstract in the purely functional portion. The author of the code would then have to prove that the C code doesn’t do anything too unsafe, that would violate the invariants of the linear type system.

Specifically, Cogent extends the (dynamic) typing relation for values to include sets of locations which can be accessed from a value1. For example, the typing rule of for tuple values is:

 \dfrac{ x : \tau_1\ \langle p \rangle \quad                y : \tau_2\ \langle q \rangle \quad                p \cap q = \emptyset               }{ (x, y) : \tau_1 \times \tau_2\ \langle p \cup q \rangle }

Observe how we have used these pointer sets to enforce that there is no internal aliasing in the structure. It also gives us the information necessary to precisely state the conditions under which a C program is safe to execute. We define stores, denoted \sigma, as a partial mapping from a location or pointer \ell to a value v.

Assuming a C-implemented function f : \tau \rightarrow \rho is evaluated with an input value v : \tau\ \langle p \rangle and an input store \sigma, the return value f\ v : \rho\ \langle p&39; \rangle and output store \sigma&39; must satisfy the following three properties for all locations \ell:

Assuming these three things, it’s possible to show that the two semantic interpretations of linear typed programs are equivalent, even if they depend on unsafe, imperative C code. I called these three conditions together the frame conditions, named after the frame problem, from the field of knowledge representation. The frame problem is a common issue that comes up in many formalisations of stateful processes. Specifically, it refers to the difficulty of local reasoning for many of these formalisations. The state or store is typically represented (as in our Cogent formalisation above) as a large, monolithic blob. Therefore, whenever any part of the state is updated, every invariant about the state must be re-established, even if it has nothing to do with the part of the state that was updated. The above conditions allow us to state that the C program does not affect any part of the state except those it is permitted (by virtue of the linear references it recieved) to modify, thus allowing us to enforce the type system invariants across the whole program.

Presenting such proof obligations in terms of stores and references as described above, however, is extremely tedious and difficult to work with when formally reasoning about imperative programs, particularly if the invariants we are trying to show are initially broken and only later re-established. Typically, imperative programs lend themselves to axiomatic semantics for verification, the most obvious example being Hoare Logic [Hoare 1969], which provides a proof calculus for a judgement written \mu \models \{ \phi \} P \{ \psi \}, which states that, assuming the initial state \mu (which maps variables to values) satisfies an assertion \phi, then the resultant state of running P on \mu, satisfies \psi.

When our assertions involve references and aliasing, however, Hoare Logic doesn’t buy us much over just reasoning about the operational semantics directly. A variety of ad-hoc operators have to be added to the logic, for example to say that references do not alias, references point to free space, or that references point to valid values. To make this cleaner, we turn instead to the Separation Logic [Reynolds 2002]. Separation logic is a variant of Hoare Logic that is specifically designed to accommodate programming with references and aliasing. It augments the state of Hoare Logic with a mutable store \sigma, and the following additional assertions:

Crucially, Separation Logic includes the frame rule, its own solution to the frame problem, where an unrelated assertion \phi_r can be added to both the pre- and the post-condition of a given program in a separating conjunction:

 \dfrac{ \{\phi\}\ P\ \{\psi\} }{ \{\phi \ast \phi_r\}\ P\ \{\psi \ast \phi_r\} }

This allows much the same local reasoning that we desired before: The program P can be verified to work for a store that satisfies \phi, but otherwise contains no other values. Then that program may be freely used with a larger state and we automatically learn, from the frame rule, that any unrelated bit of state cannot affect, and is not affected by the program P.

Separation logic makes expressing these obligations substantially simpler. For example, given a program P with an input pointers p and and output pointers p&39;, we can express all three frame conditions as a single triple:

   \left \{ \bigast_{\scriptstyle \ell \in p} \exists v.\ \ell \mapsto v \right \} P \left \{ \bigast_{\ell \in p&39;} \exists v.\ \ell \mapsto v \right \}

Here is a sketch of a proof that this implies the frame conditions listed above. Assume an input store \sigma. Split \sigma into disjoint stores \sigma_1 and \sigma_2 such that \sigma_1 \models  \mathop{\!\,\scalebox{1.5}{\begin{math}\ast\end{math}}}_{\ell \in p} \exists v.\ \ell \mapsto v\ \ (*). Let the output store of running P with \sigma_1 be \sigma_1&39;. Note that by the triple above, we have that \sigma_1&39; \models  \mathop{\!\,\scalebox{1.5}{\begin{math}\ast\end{math}}}_{\ell \in p&39;} \exists v.\ \ell \mapsto v\ \ (*\!*).

We have by the frame rule that the output of running P with the full store \sigma is \sigma&39; = \sigma_1&39; \cup \sigma_2 where \text{dom}(\sigma_1&39;) \cap \text{dom}(\sigma_2) = \emptyset.

I think this is a much cleaner and easier way to state the frame conditions.

My next item to investigate is how I might integrate this into a seamless language and verification framework. My current thinking is to take a lambda calculus with linear types and refinement types, and augment it with an imperative embedded language, which allows several of the guarantees of the linear type system to be suspended. The imperative embedded language might resemble the Hoare-state monad [Swierstra 2009], only using Separation Logic rather than Hoare Logic, but I am still figuring out all the details.

References

Hoare, C.A.R., 1969. An axiomatic basis for computer programming. Communications of the ACM, 12(10), pp.576–580.

Reynolds, J.C., 2002. Separation logic: A logic for shared mutable data structures. In Proceedings of the 17th Annual Ieee Symposium on Logic in Computer Science. LICS ’02. Washington, DC, USA: IEEE Computer Society, pp. 55–74.

Swierstra, W., 2009. A hoare logic for the state monad. In Theorem Proving in Higher Order Logics. Lecture Notes in Computer Science. Springer Berlin Heidelberg, pp. 440–451.


  1. The real formalisation is a bit more complicated, allowing nonlinear read-only pointers as well as linear, writable ones.