Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Program Logics, Continued

Last lecture, we saw the rules for a program logic for IMP. This class, we will see some more examples, and then turn to a concrete implementation of program logic using Dafny.

\[ \newcommand{\assert}[1]{{\color{blue}{\{#1\}}}} \newcommand{\comment}[1]{\qquad {\color{gray}{\textit{// #1}}}} \]

Proof Outlines

To describe program logic proofs, one thing we can do is to write down the whole derivation tree. For example, one possible derivation tree for \(\assert{\text{True}}\ x := 1; y := x + 2\ \assert{y \geq 3}\) is:

\[ \dfrac{ \dfrac{}{\text{True} \implies \text{True}} \quad \dfrac{ \mathcal{D}_1 \qquad \mathcal{D}_2 }{ \{\text{True}\}\ x := 1; y := x + 2\ \{x = 1 \land y = 3\} } \text{(R-Seq)} \quad \dfrac{}{x = 1 \land y = 3 \implies y \geq 3} }{ \{\text{True}\}\ x := 1; y := x + 2\ \{y \geq 3\} } \text{(R-Conseq)} \]

At the bottom, (R-Conseq) weakens the postcondition \(x = 1 \land y = 3\) to \(y \geq 3\). Above that, we use (R-Seq) with the intermediate condition \(x = 1\), which leaves two subderivations. The first, \(\mathcal{D}_1\), proves \(\{\text{True}\}\ x := 1\ \{x = 1\}\):

\[ \mathcal{D}_1 = \dfrac{ \dfrac{}{\text{True} \implies 1 = 1} \quad \dfrac{}{\{1 = 1\}\ x := 1\ \{x = 1\}} \text{(R-Assign)} \quad \dfrac{}{x = 1 \implies x = 1} }{ \{\text{True}\}\ x := 1\ \{x = 1\} } \text{(R-Conseq)} \]

Here, (R-Assign) gives us the precondition \((x = 1)[1/x] = (1 = 1)\), which is implied by \(\text{True}\). The second, \(\mathcal{D}_2\), proves \(\{x = 1\}\ y := x + 2\ \{x = 1 \land y = 3\}\):

\[ \mathcal{D}_2 = \dfrac{ \dfrac{}{x = 1 \implies x = 1 \land x + 2 = 3} \quad \dfrac{}{\{x = 1 \land x + 2 = 3\}\ y := x + 2\ \{x = 1 \land y = 3\}} \text{(R-Assign)} \quad \dfrac{}{x = 1 \land y = 3 \implies x = 1 \land y = 3} }{ \{x = 1\}\ y := x + 2\ \{x = 1 \land y = 3\} } \text{(R-Conseq)} \]

Here, (R-Assign) gives us the precondition \((x = 1 \land y = 3)[x + 2/y] = (x = 1 \land x + 2 = 3)\), which is implied by \(x = 1\).

This is a very difficult way to understand a proof. Instead, we will present proof outlines, which sequence pre- and postconditions in between commands:

\[ \begin{array}{l} \assert{\text{True}} \\ x := 1; \\ \assert{x = 1} \\ y := x + 2 \\ \assert{x = 1 \land y = 3} \\ \assert{y \geq 3} \end{array} \]

The basic idea of a proof outline is that we interleave assertions between program fragments. We will write all assertions in blue. The assertion at the top is the precondition for the whole code block, while the assertion at the bottom is a the postcondition. An intermediate assertion, such as \(\assert{x = 1},\) serves as both a postcondition for the statement above the assertion (\(x := 1\)) as well as a precondition for the statement below it (\(y := x + 2\)). If we write two assertions side-by-side, such as \(\assert{x = 1 \land y = 3}\) and \(\assert{y \geq 3}\), then the latter one is a logical consequence of the former one. A proof outline is valid if every triple \(\assert{P}\ c\ \assert{Q}\) in the outline is a valid Hoare triple.

Note that strictly speaking, the above is only a proof outline, since certain low-level steps aren’t being shown. For example, a fully formal proof of \(\assert{x = 1}\ y := x + 2\ \assert{x = 1 \land y = 3}\) requires using the rule of consequence, as in \(\mathcal{D}_2\). However, we can justifiably “bottom out” here in the proof, as it is usually clear by inspection whether a Hoare triple of the form \(\assert{P}\ x := e\ \assert{Q}\) is true.

For the rest of this course, we will primarily work with proof outlines. However, understanding how the underlying rules of Hoare logic work from last time are necessary to understand how to work with the proof outlines.

If/Then/Else. To handle an if-then-else in a proof outline, you need to precede the entire if/then/else block with a precondition, and ensure the same postcondition holds on both branches. However, when you descend into each branch, you get to learn whether the condition is true or not.

\[ \begin{array}{l} \assert{\text{True}} \\ \textbf{if}\ x \geq y\ \textbf{then} \\ \quad \assert{\text{True} \land x \geq y} \comment{Precondition and guard is true} \\ \quad \mathit{max} := x \\ \quad \assert{\mathit{max} = x \land x \geq y} \\ \quad \assert{\mathit{max} \geq x \land \mathit{max} \geq y} \\ \textbf{else} \\ \quad \assert{\text{True} \land x < y} \comment{Precondition and guard is false} \\ \quad \mathit{max} := y \\ \quad \assert{\mathit{max} = y \land x < y} \\ \quad \assert{\mathit{max} \geq x \land \mathit{max} \geq y} \\ \assert{\mathit{max} \geq x \land \mathit{max} \geq y} \end{array} \]

While Loops. To handle while loops in a proof outline, you need to identiy a loop invariant which holds upon loop entry, is preserved by the loop when the loop condition is true, and implies the desired postcondition when the loop condition is false. To make the proof clear, we will notate loop invariants explicitly in the program: our while loops will now look like

while e do 
    invariant P
    s

where P is the loop invariant.

For example, here is a complete proof outline for a loop counting up to the value of \(x\):

\[ \begin{array}{ll} \assert{x \geq 0} & \\ y := 0; & \\ \assert{x \geq 0 \land y = 0} & \\ \textbf{while}\ y < x\ \textbf{do} & \\ \quad \textbf{invariant}\ \assert{y \leq x} & \\ \quad \assert{y < x \land y \leq x} & \comment{Loop condition and loop invariant} \\ \quad y := y + 1; & \\ \quad \assert{y \leq x} & \comment{Why is this true?} \\ \assert{\lnot (y < x) \land y \leq x} & \comment{Negated loop condition and loop invariant} \\ \assert{y = x} & \comment{By simplification} \end{array} \]

Let’s do another one, for computing the factorial function:

\[ \begin{array}{ll} \assert{n \geq 0} & \\ i := 0; & \\ \mathit{result} := 1; & \\ \assert{n \geq 0 \land i = 0 \land \mathit{result} = 1} & \\ \textbf{while}\ i < n\ \textbf{do} & \\ \quad \textbf{invariant}\ \assert{i \leq n \land \mathit{result} = i!} & \\ \quad \assert{i < n \land i \leq n \land \mathit{result} = i!} & \\ \quad i := i + 1; & \\ \quad \assert{i \leq n \land \mathit{result} = (i-1)!} & \\ \quad \mathit{result} := \mathit{result} * i; & \\ \quad \assert{i \leq n \land \mathit{result} = i!} & \\ \assert{\lnot (i < n) \land i \leq n \land \mathit{result} = i!} & \\ \assert{\mathit{result} = n!} & \end{array} \]

Implementations of Program Logics

In practice, researchers and programmers don’t sit down and write program logic proof outlines on paper; instead, they use formal verification tools which can automatically validate that a given proof is correct.

There are lots of tools in this space, which broadly fall into two categories:

  • Tools which are libraries embedded in general-purpose theorem provers, such as Iris, VST, or vcgen; and
  • Standalone tools, which use constraint solvers to check program logic proofs. Two popular tools here are Dafny, which is its own programming language, and Verus, a program logic for Rust.

Today, we will look into Dafny. The easiest way to install Danfy is using its VSCode extension. The rest of today’s lecture will will be following along the Dafny tutorial, which covers the subset of Dafny that overlaps with what we have learned in class.

How Dafny Works. Dafny is a semi-automated tool for performing program logic proofs. The user writes a piece of code (called a method) along with pre and postconditions, and Dafny will either return that the method is verified, or that it cannot complete the proof. Crucially, if Dafny cannot complete the proof, that does not mean that the statement is wrong! It can also mean that the user needs to supply more proof annotations for Dafny to check the proof.

The fragment of Dafny we are considering today is quite similar to IMP, but with the addition of methods. Since verification of IMP with integers is undecidable, Dafny requires you to annotate each loop with an invariant; without loop invariants, Dafny won’t be able to reason about the postcondition of a loop.

The real power of tools like Dafny comes from its powerful proof automation. For example, the following piece of code is valid Dafny (i.e., it verifies successfully):

method MakeUpperBound(x : int, y : int) returns (z : int)
    ensures z >= x && z >= y
{
    z := (x * x) + (y * y);
}

From vanilla Hoare logic alone, we get this triple:

\[ \assert{True}\ z := (x * x) + (y * y)\ \assert{z = x^2 + y^2} \] However, moving from this triple to the actual postcondition requires the following logical deduction (using the rule of consequence):

\[ (z = x^2 + y^2) \implies (z \geq x \land z \geq y). \] This requires a nontrivial mathematical argument, based on the properties of multiplication and \(\geq\). Fortunately, Dafny can decide certain simple mathematical properties like these automatically. It does so by using constraint solvers, which are lower-level programs which allow you to encode a collection of mathematical constraints, and decide whether or not it’s satisfiable. These constraint solvers can then be used to check whether a particular mathematical implication is correct. (Note: When reasoning about nonlinear arithmetic (such as multiplication) and quantifiers, automatic constraint solvers quickly become incomplete. In practice, you often have to help them out by stating extra intermediate assertions.)