Program Verification

Formally checking that a program is correct

Usually: that it meets a specification

Testing is not enough

Sure, you could iteratively debug, but for programs which are more critical, testing is not enough:

Even if your code covers…:

Verification

Not Perfect

  • Difficult to get right, even for small programs
  • Automated tools can help (but then you have to trust those..)
  • Have to get the specification right as well

sort example, tempted to say the return value of a sort subroutine is just a sorted list, but that doesn’t cut it:

  • More correct: Permutation of input list in ascending order

Static types can be seen as a form of verification:

OCaml: sort : int list -> int list

  • Weak form of verification for our sort function, only guarantees our output value is of the correct type, not necessarily the correct value

Coq: sort : forall (l1 : list int), exists (l2: int list), Sorted l2 /\ Permutation l1 l2

Connection logical specs and formal semantics

Can think of logical specs as C assertions.

#include <assert.h>

void f(int x) {
    int y;
    if (x > 0) {
        y = x * 2;
    } else {
        y = x * - 2;
    }
    assert(y >= 0);
}

How do we know that this code does what it does?

  • Semantics of the language
  • C semantics does not necessarily match our expectation in this case:
    • Integer overflow..
    • Floating point adherence

Formal specifications are mathematical descriptions of what the code does

Programming?

Logistics

Help resources:

Syntax, Semantics, and Equality

Syntactic (program in written form) equality: (\(\equiv\)) written the same (disregarding parens)

\(2 + 2 - 3 \equiv 2 + 2 - 3 \equiv (2 + 2) - 3\)

\(2 + 2 - 3 \not\equiv 1\)

\(1 + 2 \not\equiv 2 + 1\)

Semantic (what a program means) equality: (=) has the same meaning

\(2 + 2 - 3 = (2 + 2) - 3 = 4 - 3 = 1\)

\(1 + 2 = 2 + 1\)

Propositional Logic

Precedence

Semantics of a proposition are their truth values in different states

Conditionals:

Consider: if a then b

To determine truth value, a state must be proper: that it is defines truth values for all variables in the proposition

A proposition can be a tautology, contradiction, or contingency: