Logic for Formal Verification


Propositional Logic

Introduction

Can we prove that a program works for all possible inputs? In principle, yes. In practice, this approach is too time-consuming to be applied to large programs. However, it is useful to look at how proofs of correctness can be constructed:

What is a proof? A completely convincing argument that something is true. For an argument to be completely convincing, it should be made up of small steps, each of which is obviously true. In fact, each step should be so simple and obvious that we could build a computer program to check the proof. Two ingredients are required:

  1. A language for clearly expressing what we want to prove.
  2. Rules for building up an argument in steps that are obviously correct.

A logic accomplishes these two goals.

The strategy for proving programs correct will be to convert programs and their specifications into a purely logical statement that is either true or false. If the statement is true, then the program is correct. But for our proofs to be truly convincing, we need a clear understanding of what a proof is.

Curiously, mathematicians did not really study the proofs that they were constructing until the 20th century. Once they did, they discovered that logic itself was a deep topic with many implications for the rest of mathematics.

Propositions

We start with propositional logic, which is a logic built up from simple symbols representing propositions about some world. For our example, we will use the letters A, B, C, ... as propositional symbols. For example, these symbols might stand for various propositions:

It is not the job of propositional logic to assign meanings to these symbols. However, we use statements to the meanings of D and E to talk about the correctness of programs.

Syntax of Propositions

We define a grammar for propositions built up from these symbols. We use the letters P, Q, R to represent propositions (or formulas):

P,Q,R ::= ⊤                    (* true *)
        | ⊥                    (* false *)
        | A, B, C              (* propositional symbols *)
        | ¬P                   (* sugar for P⇒⊥ *)
        | P∧Q                  (* "P and Q" (conjunction) *)
        | P∨Q                  (* "P or Q" (disjunction) *)
        | P⇒Q                  (* "P implies Q" (implication) *)

Note: On some browsers, on some operating systems, in some fonts, the symbol for conjunction (and) is rendered incorrectly as a small circle. It should look like an upside-down V. In this document, it will appear variously as , ∧, or ∧.

The precedence of these forms decreases as we go down the list, so P∧Q⇒R is the same as (P∧Q)⇒R. One thing to watch out for is that ⇒ is right-associative (like →), so P⇒Q⇒R is the same as P⇒(Q⇒R). We will introduce parentheses as needed for clarity. We will use the notation for logical negation, but it is really just syntactic sugar for the implication P⇒⊥. We also write P⇔Q as syntactic sugar for (P⇒Q)∧(Q⇒P), meaning that P and Q are logically equivalent.

This grammar defines the language of propositions. With suitable propositional symbols, we can express various interesting statements, for example:

A∧B⇒C
"If I got a 90% on the final and I attended class, I will get an A"
¬C⇒(¬A∨¬B)
"If I didn't get an A in the class, then either I didn't get a 90% on the final or I didn't attend class"
C∨¬A∨¬B
"Either I got an A in the class, or I didn't get a 90% on the final or I didn't attend class"

In fact, all three of these propositions are logically equivalent, which we can determine without knowing about what finals and attendance mean.

Semantics of propositions

In order to say whether a proposition is true or not, we need to understand what it means. The truth of a proposition sometimes depends on the state of the "world". For example, proposition D above is true in a world where x=0 and y=10, but not in a world in which x=y=0. To understand the meaning of a proposition P, we need to know whether for each world, it is true. To do this, we only need to know whether P is true for each possible combination of truth or falsity of the propositional symbols A,B,C,... within it. For example, consider the proposition A∧B. This is true when both A and B are true, but otherwise false. We can draw a truth table that describes all four possible worlds compactly:

falsetrue A
falsefalsefalse
truefalsetrue
B   

This kind of table can also be used to describe the action of an operator like ∧ for a conjunction over general propositions P∧Q rather than over simple propositional symbols A and B. Here is a truth table for disjunction. Notice that in the case where both P and Q are true, we consider P∨Q to be true. The connective ∨ is inclusive rather than exclusive.

falsetrue P
falsefalsetrue
truetruetrue
Q   

We can also create a truth table for negation ¬P:

¬falsetrue P
falsetruefalse   

Implication P⇒Q is tricky. The implication seems true if P is true and Q is true, and if P is false and Q is false. And the implication is clearly false if P is true and Q is false:

falsetrue P
falsetruefalse
true?true
Q   

What about the case in which P is false and Q is true? In a sense we have no evidence about the implication as long as P is false. Logicians consider that in this case the assertion P⇒Q is true. Indeed, the proposition P⇒Q is considered vacuously true in the case where P is false, yielding this truth table:
falsetrue P
falsetruefalse
truetruetrue
Q   

We can use truth tables like these to evaluate the truth of any propositions we want. For example, the truth table for (A⇒B)∧(B⇒A) is true in the places where both implications would be:
falsetrue A
falsetruefalse
truefalsetrue
B   

In fact, this means that A and B are logically equivalent, which we write as A iff B or A⇔B. If P⇔Q, then we can replace P with Q wherever it appears in a proposition, and vice versa, without changing the meaning of the proposition. This is very handy.

Another interesting case is the proposition (A∧B)⇒B. The truth table looks like this:

(A∧B)⇒Bfalsetrue A
falsetruetrue
truetruetrue
B   

In other words, the proposition (A∧B)⇒B is true regardless of what A and B stand for. In fact, it will be true if A and B are replaced with any propositions P and Q. We call such a proposition a tautology.

Tautologies

There are a number of useful tautologies, including the following:
Associativity (P∧Q)∧R ⇔ (P∧Q)∧R (P∨Q)∨R ⇔ (P∨Q)∨R
Symmetry (P∧Q) ⇔ (Q∧P) (P∨Q) ⇔ (Q∨P)
Distributivity P∧(Q∨R) ⇔ (P∧Q)∨(P∧R) P∨(Q∧R) ⇔ (P∨Q)∧(P∨R)
Idempotency P∧P ⇔ P P∨P ⇔ P
DeMorgan's laws ¬(P∧Q) ⇔ ¬P∨¬Q ¬(P∨Q) ⇔ ¬P∧¬Q
Negation P ⇔ ¬¬P P⇒⊥ ⇔ ¬P P⇒Q ⇔ ¬P∨Q

These can all be derived from the rules we will see shortly, but they are useful to know.

Notice that we can use DeMorgan's laws to turn ∧ into ∨, and use the equivalence P⇒Q ⇔ ¬P∨Q to turn ∨ into ⇒, and the equivalence P⇒⊥ ⇔ ¬P to get rid of negation. So we can express any proposition using just implication ⇒ and the false symbol ⊥!

Inference rules

To prove whether propositions are true without testing every possible world, we need a system for deduction. We can construct proofs of a proposition by using inference rules to derive the desired proposition starting from axioms.

The most famous inference rule is known as modus ponens. Intuitively, this says that if we know P is true, and we know P⇒Q, then we can conclude Q:

P P⇒Q
Q
(modus ponens)

The propositions above the line are the premises; the proposition below the line is the conclusion. Both the premises and the conclusion may contain metavariables (in this case, P and Q), representing arbitrary propositions. When a inference rule is used as part of a proof, the metavariables are replaced in a consistent way with the appropriate kind of object (in this case, propositions).

Most rules come in one of two flavors: introduction or elimination rules. Introduction rules introduce the use of a logical operator, and elimination rules eliminate it. Modus ponens is an elimination rule for ⇒. On the right-hand side of a rule, we often write the name of the rule. This is helpful when reading proofs. In this case, we have written “modus ponens”, but it would be shorter to write (⇒E), meaning that this is the elimination rule for ⇒.

Conjunction (∧) has an introduction rule and two elimination rules:

P Q
P∧Q
(∧I)
P∧Q
P
(∧E1)
P∧Q
Q
(∧E2)

The simplest introduction rule is the one for T. Because it has no premises, this rule is an axiom: something that can start a proof:

 
T
(true)

Natural deduction

Together, a set of inference rules make up a proof system that determines what can be proved. There is still something important missing from our proof system: how can we prove an implication P⇒Q? Intuitively, the way this is proved is by assuming that P is true, and with that assumption, showing that Q is true too. In a proof, we are always allowed to introduce a new assumption P, which we do with the following rule. The name x is the name of the assumption. Each distinct assumption should have a different name.
 
[x : P]
(A)

Because it has no premises, this rule is an axiom: something that can start a proof. It can be used as if it proved the proposition P. It also gives a name to the assumption, which is important for making sure that the things proved end up being conditioned on the assumption.

We can introduce an implication P⇒Q by discharging a prior assumption [x: P]. We write x in the rule name to show which assumption is discharged:

[x: P]

Q
P⇒Q
(⇒I/x)
P P⇒Q
Q
(⇒E, modus ponens)

A proof is valid only if every assumption is discharged somewhere below all places the assumption appears. The same assumption can appear more than once.

The introduction and elimination rules for disjunction are as follows:

P
P∨Q
(∨I1)
Q
P∨Q
(∨I2)
P∨Q P⇒R Q⇒R
R
(∨E)

Finally, there are rules relating to negation:

[x: P]


¬P
(special case of ⇒I)
[x: ¬P]


P
(reductio ad absurdum, RAA)
P
(false proves everything)

Reductio ad absurdum is an interesting rule. It says that if the negation of a proposition can be used to prove falsity, the proposition must be true. This rule is present in classical logic but not in constructive logics, in which things cannot be proved true simply by showing the falsity of their negations. In constructive logics, the the law of the excluded middle, P∨¬P, does not hold. However, we will use this rule.

Proofs

A proof of proposition P in natural deduction starts from axioms and derives the judgement ⊢P; that is, it proves P under no assumptions. Every step in the proof is an instance of an inference rule with metavariables substituted consistently with expressions of the appropriate syntactic class.

Example 1

For example, here is a proof of the proposition (A⇒B⇒C) ⇒ (A∧B⇒C). For brevity, Γ₀ is used as an abbreviation for the assumptions (A⇒B⇒C), A∧B.

The final step in the proof is to derive (A⇒B⇒C) ⇒ (A∧B⇒C) from (A∧B⇒C), which is done using the rule (⇒I), discharging the assumption [x:(A⇒B⇒C)]. To see how this rule generates the proof step, substitute for the metavariables P, Q, x as follows: P = (A⇒B⇒C), Q = (A∧B⇒C) x=x. The immediately previous step uses the same rule, but with a different substitution: P = A∧B, Q = C, x = y.

This shows that a proof of proposition P is a tree generated by inference rules. The leaves of the tree are axioms and the root is P. If we represent each proposition in this proof as a node in a tree, we get the following:

Writing out proofs at this level of detail can be a bit tedious. But notice that checking the proof is completely mechanical, requiring no intelligence or insight whatever. Therefore it is a very strong argument that the thing proved is in fact true.

We can also make writing proofs like this less tedious by adding more rules that provide reasoning shortcuts. These rules are sound (they only prove true things), if there is a way to convert a proof using them into a proof using the original rules. Such added rules are called admissible.


Predicate Logic

Syntax

In propositional logic, the statements we are proving are completely abstract. To be able to prove programs correct, we need a logic that can talk about the things that programs compute on: integers, strings, tuples, datatype constructors, and functions. We'll enrich propositional logic with the ability to talk about these things, obtaining a version of predicate logic.

The syntax extends propositional logic with a few new expressions, shown in blue:

P,Q,R ::= ⊤             (* true *)
        | ⊥             (* false *)
        | A, B, C       (* propositional symbols *)
        | ¬P            (* sugar for P⇒⊥ *)
        | P∧Q           (* "P and Q" (conjunction) *)
        | P∨Q           (* "P or Q" (disjunction) *)
        | P⇒Q           (* "P implies Q" (implication) *)
        | ∀x.P          (* P is true for all x. P can mention x*)
        | ∃x.P          (* There exists some x such that P is true *)
        | e1 = e2       (* e1 is equal to e2 *)
        | p(e)          (* The predicate named p is true for e *)

e ::=     v             (* integers, tuples, other values *)
        | x             (* The name of a value, may be bound 
                            by ∀ or ∃ *)
        | f(e)          (* Result of applying function
                           named f to e *)

The formula ∀x.P means that the formula P is true for any choice of x. This is called universal quantification, and ∀ is the universal quantifier. The formula ∃x.P denotes existential quantification. It means that the formula P is true for some choice of x, though there may be more than one such x. Existential and universal quantifiers can be turned into each other using negation. The formula ∃x.P is equivalent to ¬∀x.¬P, because if there exists some x that makes P true, then clearly ¬P is not true for all x. Similarly, the formula ∀x.P is equivalent to ¬∃x.¬P. These equivalences are generalizations of DeMorgan's to existential and universal quantifiers.

We will assume that we can tell from the name of x what type of thing it is. What if we want to restrict to talking about some subset? For universal quantifiers, the trick is to use an a For example, if we want to say that all numbers greater than one are also greater than zero, we write ∀x.x>1⇒x>0. This works because the quantified formula is vacuously true for the numbers not greater than 1. If we want to limit the range in an existential, we use conjunction. For example, to say that there exists a number greater than zero that is greater than one, we write ∃x.x>0∧x>1.

Using quantifiers, we can express some interesting statements. For example, we can express the idea that a number n is prime (note m and k are integers) in various logically equivalent ways:

prime(n)∀m.1<m∧m<n⇒¬∃k.k*m = n
 ¬∃m.1<m∧m<n∧∃k.k*m = n (DeMorgan's laws)
¬∃m.∃k.1<m∧m<n∧k*m = n

This example shows one fine point of syntax: when reading quantifiers ∀x.P, the formula P extends as far to the right as possible. So (∀m.1<m∧m<n⇒¬∃k.k*m = n) is read as (∀m.1<m∧m<n⇒(¬∃k.k*m = n)) rather than as (∀m.1<m∧m<n)⇒(¬∃k.k*m = n). This is the same as for other perhaps more familiar binding constructs, such as summation ∑ and integrals ∫.

Rules for quantifiers

Introduction and elimination rules can be defined for universal and existential quantifiers. The rules for universals are as follows:

P x does not appear free in any axiom or undischarged assumption
∀x.P
(∀I)
∀x.P
P{e/x}
(∀E)

The requirement in (∀I) that x does not appear in undischarged assumptions prevents us from doing unsound generalizations like the following:

[x>1]
∀x. x>1
x>1 ⇒ ∀x. x>1

It is, however, fine to have x appear in an assumption that is discharged above the point where the (∀I) rule is used, e.g.:

[x>1]
x>0
x>1 ⇒ x>0
∀x. x>1 ⇒ x>0

The rule (∀E) specializes the formula P to a particular value of x. We require implicitly that e be of the right type to be substituted for x. Since P holds for all x, it should hold for any given choice of x, that is, e.

The rules for existentials are as follows:

P{e/x}
∃x.P
(∃I)
∃x.P [P]

Q
x does not appear free in Q or in the assumptions or axioms of the proof of Q (other than in P).
Q
(∃E)

The rule (∃I) derives ∃x.P because a witness e to the existential has been produced. The idea behind rule (∃E) is that if something (Q) can be shown without using any information about the witness x, other than what is known from P, then Q is true without existential quantification: ∃x.Q is the same as Q if Q doesn't mention x.

Reasoning with equality

The predicate logic allows the use of arbitary predicates p. Equality is a predicate that applies to two arguments; we can read e1=e2 alternatively as a predicate =(e1,e2). We support reasoning about predicates by adding rules (esp. axioms) Equality is special because when two things are equal, one can be substituted for the other in any context.

The following three rules capture that equality is an equivalence relation: it is reflexive, symmetric, and transitive.

 
e=e
(refl)
e1=e2
e2=e1
(symm)
e1=e2 e2=e3
e1=e3
(trans)

Beyond being an equivalence relation, equality preserves meaning under substitution. If two things are equal, substituting one for the other in equal terms results in equal terms. This is known as Leibniz's rule:

e1 = e2
e{e1/x} = e{e2/x}

Leibniz's rule can also be applied to show propositions are logically equivalent :
e = e'
P{e/z} ⇔ P{e'/z}
For example, suppose we know y=x+1 and x(x+1)+(x+1) = (x+1)2. Then we can use this rule to prove xy+(x+1) = y2, by applying this rule with e=x+1, e'=y, and P = (xz+(x+1) = z2).

The same idea can be applied completely at the propositional level as well. If we can prove that two formulas are equivalent, they can be substituted for one another within any other formula.

Q ⇔ R
P{Q/A} ⇔ P{R/A}

This admissible rule can be very convenient for writing proofs, though anything we can prove with it can be proved using just the basic rules. It can be very handy when there is a large “library” of logical equivalences to draw upon, because it allows rewriting of deeply nested subformulas.

Reasoning on integers and other sets

For reasoning about specific kinds of values, we need axioms that describe how those values behave. For example, the following axioms partly describe the integers and can be used to prove many facts about integers. In fact, they define a more general structure, a commutative ring, so anything proved with them holds for any commutative ring.
∀x.∀y.x+y=y+x (commutativity of +)
∀x.∀y.∀z.(x+y)+z = x+(y+z) (associativity of +)
∀x.∀y.∀z.(x*y)*z = x*(y*z) (associativity of *)
∀x.∀y.∀z.x*(y+z) = x*y+x*z (+ distributes over *)
∀x.x + 0 = x (additive identity)
∀x.x + (-x) = 0 (additive inverse)
∀x.x*1=x ∧ 1*x=x(multiplicative identity)
¬0=1
∀x.∀y.x*y=y*x (commutativity of *)

These rules use a number of functions: +, *, -, 0, and 1 (we can think of 0 and 1 as functions that take zero arguments). These symbols are represented by the metavariable f in the grammar earlier.

Proving facts about arithmetic can be tedious. For our purposes, we will write proofs that do reasonable algebraic manipulations as a single step, e.g.:
(x+2)2 = 2*x
x2 = −2*x−2
(algebra)

This proof step can be done explicitly using the rules and axioms above, but it takes several steps.