Ran Wei/ Formal methods/Module 3
中文
Formal Methods with Isabelle · Part I · Foundations

Module 3Predicate logic

Say what holds for every object or for some, work out whether a formula is true in a structure, find countermodels, and turn requirements into precise formulas, on paper and in Isabelle.

⏱ 240 minYou need: Modules 1 and 2

In this module you will

  • Read and write formulas with predicates, functions, ∀ and ∃, and tell free variables from bound ones
  • Work out by hand whether a formula is true in a small structure, and build countermodels
  • Use the quantifier laws, and recognise the ones that fail
  • Translate English requirements into predicate logic without the classic mistakes
  • Check first-order claims in Isabelle with blast and nitpick, and declare a typed vocabulary
  • Explain why no program can decide validity in predicate logic, and what that means for tools
1

Beyond propositional logic

Module 2 treated every statement as an indivisible atom. That is enough for rules such as “if the door is open and the alarm is armed, the siren sounds”. Most requirements, though, talk about many objects at once: every request, some server, no two trains, the owner of a file. Here is an access-control policy and a conclusion that anyone would draw from it:

  1. Every administrator can delete every document.
  2. Alice is an administrator, and the quarterly report is a document.
  3. Therefore Alice can delete the quarterly report.

In propositional logic each sentence becomes an atom, say AA⁠, BB and CC⁠, and the argument becomes the formula A∧B⟶CA \land B \imp C⁠. Its truth table:

ABCA ∧ B ⟶ C
TTTT
TTFF
TFTT
TFFT
FTTT
FTFT
FFTT
FFFT

Satisfiable but not valid: true in 7 rows.

The second row makes the formula false: AA and BB true, CC false. As far as propositional logic can tell, the conclusion does not follow. Yet it does, for reasons that lie inside the sentences: the Alice of sentence 2 is the Alice of sentence 3, “every administrator” in sentence 1 includes her, and “every document” includes the report. To capture such reasons, a logic needs

  • names for objects, such as Alice and the report;
  • properties and relations of objects, such as “is an administrator” and “can delete”;
  • quantifiers, the words of quantity “every” and “some”, with variables that range over the objects.

Here predicate logic means first-order logic: its variables range over objects, rather than over predicates or relations themselves. It adds the vocabulary and quantifiers above to the connectives of Module 2. Gottlob Frege introduced quantifiers in his Begriffsschrift of 1879, and predicate logic has since become the standard language for stating mathematics precisely. Written in it, the whole argument is one formula:

(∀u d. admin u∧doc d⟶can_delete u d)∧admin alice∧doc report⟶can_delete alice report(\forall u\ d.\ \mathit{admin}\ u \land \mathit{doc}\ d \imp \mathit{can\_delete}\ u\ d) \land \mathit{admin}\ \mathit{alice} \land \mathit{doc}\ \mathit{report} \imp \mathit{can\_delete}\ \mathit{alice}\ \mathit{report}

Here doc d\mathit{doc}\ d means “dd is a document”. Read the formula as “if every administrator can delete every document, Alice is an administrator, and the report is a document, then Alice can delete the report”. By the end of this module you will know what it means for this formula to be true however its words are interpreted, and Isabelle will prove it in one line (Section 3.8).

Predicate logic is one language for specifications. The sorting condition of Module 1, sorted ys∧mset ys=mset xs\mathit{sorted}\ ys \land \mathit{mset}\ ys = \mathit{mset}\ xs⁠, uses predicates, functions and equality in this way. Isabelle supplies their typed definitions in its richer logic, HOL.

2

Terms, predicates and formulas

A formula of predicate logic is built from two kinds of symbol. The logical symbols are the same in every application: the connectives of Module 2, the quantifiers, equality, and variables. The other symbols name the things of one particular application, such as users, documents and permissions. They form its vocabulary.

Definition 3.1Vocabulary

A vocabulary (also called a signature) lists the non-logical symbols of a language, each with its number of arguments, its arity:

  • constants name single objects: alice\mathit{alice}⁠, report\mathit{report}⁠, 00⁠;
  • function symbols name operations that give an object: owner\mathit{owner} (one argument: the owner of a document), ++ (two arguments);
  • predicate symbols name properties and relations, which hold or do not hold: admin\mathit{admin} (one argument), can_delete\mathit{can\_delete} (two), << (two).

Most logic books write P(x)P(x)⁠, R(x,y)R(x, y) and f(x)f(x)⁠. This series writes them as Isabelle does: the symbol, then its arguments, separated by spaces: P xP\ x⁠, R x yR\ x\ y⁠, f xf\ x⁠. Parentheses only group: can_delete (owner d) d\mathit{can\_delete}\ (\mathit{owner}\ d)\ d applies can_delete\mathit{can\_delete} to two arguments, owner d\mathit{owner}\ d and dd⁠. Symbols such as ++ and << stay between their arguments, as usual.

Terms

Definition 3.2Term

A term is either

  1. a variable (xx⁠, yy⁠, zz⁠, …) or a constant, or
  2. f t1 … tnf\ t_1\ \dots\ t_n⁠, where ff is a function symbol with nn arguments and t1,…,tnt_1, \dots, t_n are terms.

A term names an object. It is never true or false.

alice\mathit{alice}⁠, dd⁠, owner d\mathit{owner}\ d⁠, x+1x + 1 and 2∗(x+y)2 * (x + y) are terms. owner d\mathit{owner}\ d names the owner of whichever document dd stands for, and x+1x + 1 names a number. Terms are like the expressions of a programming language in that they denote values. An interpretation need not supply an executable algorithm for computing them.

Atomic formulas

Definition 3.3Atomic formula

An atomic formula is either P t1 … tnP\ t_1\ \dots\ t_n⁠, where PP is a predicate symbol with nn arguments and t1,…,tnt_1, \dots, t_n are terms, or an equation t1=t2t_1 = t_2 between two terms.

admin alice\mathit{admin}\ \mathit{alice}⁠, can_delete x report\mathit{can\_delete}\ x\ \mathit{report}⁠, x<y+1x < y + 1 and owner report=alice\mathit{owner}\ \mathit{report} = \mathit{alice} are atomic formulas. Each becomes true or false once you know which objects its terms stand for. A predicate symbol with no arguments is a propositional atom of Module 2, so every formula of propositional logic is also a formula of predicate logic.

Watch out

Terms and formulas live on different levels, and one cannot stand where the other belongs. admin (can_delete alice report)\mathit{admin}\ (\mathit{can\_delete}\ \mathit{alice}\ \mathit{report}) is not a formula: admin\mathit{admin} needs an object, and can_delete alice report\mathit{can\_delete}\ \mathit{alice}\ \mathit{report} is a statement. Nor is owner report∧admin alice\mathit{owner}\ \mathit{report} \land \mathit{admin}\ \mathit{alice}⁠: ∧\land joins statements, and owner report\mathit{owner}\ \mathit{report} is an object. Isabelle rejects such expressions, as Section 3.8 shows.

Quantifiers

The new ingredient is quantification over objects. Here φ\varphi (the Greek letter phi) and ψ\psi (psi) stand for any formulas.

  • ∀x. φ\forall x.\ \varphi reads “for all xx⁠, φ\varphi”. It says that φ\varphi holds whichever object xx stands for. ∀\forall is the universal quantifier.
  • ∃x. φ\exists x.\ \varphi reads “there exists an xx such that φ\varphi”, or “for some xx⁠, φ\varphi”. It says that φ\varphi holds for at least one object. ∃\exists is the existential quantifier.

One quantifier may introduce several variables: ∀x y. φ\forall x\ y.\ \varphi is short for ∀x. ∀y. φ\forall x.\ \forall y.\ \varphi⁠.

Definition 3.4Formula

The formulas over a vocabulary are built by these rules:

  1. Every atomic formula is a formula, and so are True\mathit{True} and False\mathit{False}⁠.
  2. If φ\varphi and ψ\psi are formulas, so are ¬φ\lnot \varphi⁠, φ∧ψ\varphi \land \psi⁠, φ∨ψ\varphi \lor \psi⁠, φ⟶ψ\varphi \imp \psi and φ⟷ψ\varphi \iff \psi⁠, with parentheses where the priorities of Module 2 need them.
  3. If φ\varphi is a formula and xx is a variable, then ∀x. φ\forall x.\ \varphi and ∃x. φ\exists x.\ \varphi are formulas.

Reading formulas fluently takes practice. Read them aloud, symbol by symbol, then say what they mean in plain English.

Example 3.1Reading formulas
Formula Read aloud In plain English
∀x. admin x⟶on_call x\forall x.\ \mathit{admin}\ x \imp \mathit{on\_call}\ x for all xx⁠, if xx is an admin then xx is on call every admin is on call
∃x. admin x∧¬ on_call x\exists x.\ \mathit{admin}\ x \land \lnot\, \mathit{on\_call}\ x there is an xx such that xx is an admin and xx is not on call some admin is not on call
∀x. ∃y. trusts x y\forall x.\ \exists y.\ \mathit{trusts}\ x\ y for all xx there is a yy such that xx trusts yy everybody trusts somebody
∃y. ∀x. trusts x y\exists y.\ \forall x.\ \mathit{trusts}\ x\ y there is a yy such that for all xx⁠, xx trusts yy somebody is trusted by everybody
∀x y. x<y⟶(∃z. x<z∧z<y)\forall x\ y.\ x < y \imp (\exists z.\ x < z \land z < y) for all xx and yy⁠, if x<yx < y then there is a zz with x<zx < z and z<yz < y between two different numbers lies a third

The third and fourth rows differ only in the order of the quantifiers, yet they say very different things. Sections 3.3 and 3.5 come back to this. The last row is true of the fractions but false of the whole numbers: there is no whole number between 1 and 2.

Isabelle writes the quantifiers as here: ∀ or ∃, a variable, a dot. Type them as \forall and \exists (Module 1).

Scope, free and bound variables

In ∀x. φ\forall x.\ \varphi the formula φ\varphi is the scope of the quantifier: the region in which xx refers to it. When a formula has no parentheses, how far does a scope extend? This series follows Isabelle: the scope of a quantifier extends as far to the right as possible. So ∀x. P x⟶Q x\forall x.\ P\ x \imp Q\ x means ∀x. (P x⟶Q x)\forall x.\ (P\ x \imp Q\ x)⁠. To end a scope earlier, use parentheses: (∀x. P x)⟶Q x(\forall x.\ P\ x) \imp Q\ x⁠.

Watch out

Many logic textbooks use the opposite convention: there a quantifier binds as tightly as ¬\lnot⁠, and ∀x P(x)⟶Q(x)\forall x\, P(x) \imp Q(x) means (∀x P(x))⟶Q(x)(\forall x\, P(x)) \imp Q(x)⁠. When you read other books, check their convention. When you write for Isabelle, remember that a quantifier’s scope runs to the end of the formula, or to the closing parenthesis of the group the quantifier stands in.

Definition 3.5Free and bound variables

An occurrence of a variable xx in a formula is bound if it lies in the scope of a quantifier ∀x\forall x or ∃x\exists x⁠; it then belongs to the nearest such quantifier around it. Otherwise the occurrence is free. A formula in which no variable occurs free is a sentence, or closed formula.

Figure 3.1 marks the scopes in a formula in which the same variable name occurs both bound and free.

Scope and binding in a formulascope of ∀x∀x.Px⟶(∃y.Rxy)∧Qyscope of ∃yfree: outsidethe scope of ∃ybound by ∀xbound by ∃yfree
Figure 3.1

Scopes in ∀x. P x⟶(∃y. R x y)∧Q y\forall x.\ P\ x \imp (\exists y.\ R\ x\ y) \land Q\ y⁠. The scope of ∀x\forall x runs to the end, so every xx is bound. The parentheses close the scope of ∃y\exists y⁠, so the last yy is free: a different variable from the bound yy⁠, with the same name.

A sentence is true or false once its symbols have a meaning. A formula with free variables also depends on what they stand for: x<3x < 3 is neither true nor false until xx has a value, while ∃x. x<3\exists x.\ x < 3 is a sentence, true of the natural numbers. A free variable is like a parameter of a function, a bound variable like a local variable: renaming a bound variable throughout its scope, with a fresh name, changes nothing, so ∀x. P x\forall x.\ P\ x and ∀y. P y\forall y.\ P\ y say the same thing.

Example 3.2Finding scopes and free variables
  1. ∀x. P x⟶Q x\forall x.\ P\ x \imp Q\ x⁠: the scope of ∀x\forall x is P x⟶Q xP\ x \imp Q\ x⁠, and both occurrences of xx are bound. A sentence.
  2. (∀x. P x)⟶Q x(\forall x.\ P\ x) \imp Q\ x⁠: the scope is only P xP\ x⁠. The xx in Q xQ\ x is free.
  3. ∀x. ∃y. R x y∧¬ R y z\forall x.\ \exists y.\ R\ x\ y \land \lnot\, R\ y\ z⁠: xx and yy are bound, zz is free. Not a sentence.
  4. ∃x. P x∧(∀x. Q x)⟶R x\exists x.\ P\ x \land (\forall x.\ Q\ x) \imp R\ x⁠: the inner ∀x\forall x binds only the xx of Q xQ\ x⁠. The scope of ∃x\exists x runs to the end, so the xx in R xR\ x belongs to ∃x\exists x⁠. A sentence, but a confusing one: give each quantifier a variable of its own.
Check it in Isabelle

To see how Isabelle reads a formula, state it as a lemma and look at the goal in the Output panel. Isabelle prints formulas with as few parentheses as it can, so the goal shows which of your parentheses mattered:

Isabelle
lemma "∀x. (P x ⟶ Q x)"
Output
proof (prove)
goal (1 subgoal):
 1. ∀x. P x ⟶ Q x

The parentheses are gone: the scope of ∀x runs to the end anyway, so they changed nothing. Abandon the lemma with oops and move the parenthesis:

Isabelle
  oops
Isabelle
lemma "(∀x. P x) ⟶ Q x"
Output
proof (prove)
goal (1 subgoal):
 1. (∀x. P x) ⟶ Q x

These parentheses stay, because they end the scope of ∀x early. The x in Q x is free; what a free variable means in a lemma is the subject of Section 3.4.

Isabelle
  oops
3

Structures and truth

Is ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y true? The question has no answer yet. If xx and yy range over the natural numbers and RR is <<⁠, it is true: every number has a larger one. If RR is >>⁠, it is false: no natural number is smaller than 0. A formula gets a truth value only once you say what its variables range over and what its symbols mean. That information is a structure.

Definition 3.6Structure

A structure M\mathcal M for a vocabulary consists of

  1. a non-empty set DD⁠, the domain: the objects that variables range over;
  2. for each constant cc⁠, an element cMc^{\mathcal M} of DD⁠;
  3. for each function symbol ff with nn arguments, a function fMf^{\mathcal M} that maps every nn-tuple of elements of DD to an element of DD⁠;
  4. for each predicate symbol PP with nn arguments, a set PMP^{\mathcal M} of nn-tuples of elements of DD⁠: the tuples for which PP holds.

An nn-tuple is a list (d1,…,dn)(d_1, \dots, d_n) of nn elements, in order; a 2-tuple is a pair. Read cMc^{\mathcal M} as “cc in M\mathcal M”: what M\mathcal M makes the symbol cc mean; likewise fMf^{\mathcal M} and PMP^{\mathcal M}⁠. Items 2 to 4 are the interpretation of the vocabulary. Structures are also called models or interpretations.

The domain may be finite or infinite, but by convention it is never empty; Isabelle follows the same convention. A function symbol is interpreted by a function that gives a value for every argument, even for arguments where the value makes no sense, such as the owner of something that is not a document. Section 3.8 shows how types avoid such nonsense.

To evaluate a formula with free variables, you also need values for them.

Definition 3.7Truth in a structure

An assignment ss gives every variable a value in DD⁠. The value of a term is computed from the values of its variables and constants, using the functions of M\mathcal M⁠. A formula is true in M\mathcal M under ss according to these rules:

  • P t1 … tnP\ t_1\ \dots\ t_n is true when the tuple of the values of t1,…,tnt_1, \dots, t_n is in PMP^{\mathcal M}⁠; t1=t2t_1 = t_2 is true when the two terms have the same value.
  • True\mathit{True} is true and False\mathit{False} is false; ¬\lnot⁠, ∧\land⁠, ∨\lor⁠, ⟶\imp and ⟷\iff follow their truth tables from Module 2.
  • ∀x. φ\forall x.\ \varphi is true when φ\varphi is true for every value of xx in DD⁠, the other variables keeping their values.
  • ∃x. φ\exists x.\ \varphi is true when φ\varphi is true for at least one value of xx in DD⁠.

For a sentence the assignment makes no difference. You then write M⊨φ\mathcal M \models \varphi⁠, read “φ\varphi is true in M\mathcal M” or “M\mathcal M is a model of φ\varphi”.

Think of it like this

Over a finite domain, a quantifier is a loop. ∀x\forall x is a loop that succeeds only if every round succeeds; ∃x\exists x succeeds as soon as one round does. In Python, ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y over a list D is all(any(R(x, y) for y in D) for x in D). Over an infinite domain, enumeration is not a general decision procedure: a search may find a witness or a counterexample, but it cannot finish inspecting every element. General reasoning can settle claims that enumeration cannot.

Now an example by hand. Figure 3.2 draws a structure for a vocabulary with two one-place predicates, PP and QQ⁠, and a two-place predicate RR⁠.

The structure M₁domain DabcPPQD = {a, b, c}P = {a, b}Q = {b}R = {(a, b), (b, c), (c, c)}PP holdsQQ holdsxyR x y holds
Figure 3.2

The structure M1\mathcal M_1⁠. Its domain has three elements; PP holds for aa and bb⁠, QQ for bb only; an arrow from xx to yy means that R x yR\ x\ y holds.

Written out, M1\mathcal M_1 has the domain D={a,b,c}D = \{a, b, c\}⁠, with PM1={a,b}P^{\mathcal M_1} = \{a, b\}⁠, QM1={b}Q^{\mathcal M_1} = \{b\} and RM1={(a,b),(b,c),(c,c)}R^{\mathcal M_1} = \{(a, b), (b, c), (c, c)\}⁠. If a concrete reading helps, think of three servers, of PP as “is in production”, QQ as “is backed up”, and R x yR\ x\ y as “xx forwards its logs to yy”. The evaluation does not depend on the reading.

Example 3.3Evaluating formulas by hand
  1. ∀x. P x⟶Q x\forall x.\ P\ x \imp Q\ x⁠, “every PP is a QQ”. Try each element. For aa⁠, P a⟶Q aP\ a \imp Q\ a is True⟶False\mathit{True} \imp \mathit{False}⁠, which is false. One failure is enough: the formula is false in M1\mathcal M_1⁠, and aa is a counterexample.

  2. ∃x. P x∧Q x\exists x.\ P\ x \land Q\ x⁠, “some PP is a QQ”. For bb⁠, P b∧Q bP\ b \land Q\ b is true. The formula is true, and bb is a witness.

  3. ∃x. R x x\exists x.\ R\ x\ x⁠, “some element has an arrow to itself”. True, with the witness cc⁠.

  4. ∀x. P x⟶(∃y. R x y∧Q y)\forall x.\ P\ x \imp (\exists y.\ R\ x\ y \land Q\ y)⁠, “every PP has an arrow to a QQ”. This one needs a table, with a row for each value of xx⁠:

    xx P xP\ x an arrow from xx to a QQ⁠? P x⟶(∃y. R x y∧Q y)P\ x \imp (\exists y.\ R\ x\ y \land Q\ y)
    aa true yes: R a bR\ a\ b and Q bQ\ b true
    bb true no: its only arrow goes to cc⁠, and Q cQ\ c is false false
    cc false no true

    The row for bb is false, so the formula is false in M1\mathcal M_1⁠, with the counterexample bb⁠.

Example 3.4The order of quantifiers

In M1\mathcal M_1⁠, the sentence ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y says that every element has an outgoing arrow. It does: a→ba \to b⁠, b→cb \to c and c→cc \to c⁠. Each xx has its own witness yy⁠.

∃y. ∀x. R x y\exists y.\ \forall x.\ R\ x\ y says that some element receives an arrow from every element, itself included. Try each candidate yy⁠: aa receives no arrow; bb receives an arrow from aa only; cc receives arrows from bb and cc⁠, but not from aa⁠. No candidate works, so the sentence is false.

The two sentences differ only in the order of ∀x\forall x and ∃y\exists y⁠. In the first, yy may depend on xx⁠; in the second, one yy must serve every xx⁠.

A ∀-sentence can also be true for a less obvious reason. In a structure where no element has the property PP⁠, the sentence ∀x. P x⟶Q x\forall x.\ P\ x \imp Q\ x is true whatever QQ is: for every xx the condition P xP\ x is false, so the implication is true. Such a sentence is vacuously true. “Every admin is on call” holds in a company without admins, and “every request was answered” holds for a server that received no requests. Specifications must reckon with this.

Try it

Change the structure by clicking: add or remove elements, tick where PP and QQ hold, and switch the arrows of RR on and off in the grid (row xx⁠, column yy⁠). Every formula is evaluated again after each click; a false ∀ shows a counterexample and a true ∃ a witness. Try to make ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y true while ∃y. ∀x. R x y\exists y.\ \forall x.\ R\ x\ y stays false. Then make ∀x. P x⟶Q x\forall x.\ P\ x \imp Q\ x true while ∃x. P x∧Q x\exists x.\ P\ x \land Q\ x is false; there is only one way to choose PP⁠. You can add formulas of your own, in Isabelle’s notation or in ASCII (ALL, EX, ~, &, |, -->).

Check it in Isabelle

Isabelle can evaluate a quantified formula when the domain is a type whose elements it can list. The type bool has two elements, True and False. Take ≠ (“is different from”) as the relation and compare the two orders of quantifiers:

Isabelle
value "∀x::bool. ∃y. y ≠ x"
Output
"True"
  :: "bool"
Isabelle
value "∃y::bool. ∀x. y ≠ x"
Output
"False"
  :: "bool"

x::bool in a quantifier says that x ranges over bool. Every x has a y different from it, but no y is different from every x, because nothing is different from itself. A type with infinitely many elements, such as nat, cannot be run through: value "∀x::nat. x < x + 1" stops with an error saying that nat is not of sort enum, the class of types whose elements Isabelle can list. For such types you need a proof.

4

Validity, satisfiability and countermodels

The central notions of Module 2 carry over, with “every row of the truth table” replaced by “every structure”.

Definition 3.8Validity and satisfiability

A formula φ\varphi is

  • valid, written ⊨φ\models \varphi⁠, if it is true in every structure under every assignment;
  • satisfiable if it is true in at least one structure under at least one assignment;
  • unsatisfiable if it is true in none.

A set of formulas Γ\Gamma (gamma) entails φ\varphi⁠, written Γ⊨φ\Gamma \models \varphi⁠, if φ\varphi is true in every structure and under every assignment that make all the formulas in Γ\Gamma true. φ\varphi is then a logical consequence of Γ\Gamma⁠.

As in Module 2, φ\varphi is valid exactly when ¬φ\lnot \varphi is unsatisfiable. The difference lies in the number of cases: a truth table has finitely many rows, but there are infinitely many structures, of every size, and you cannot inspect them all. To show that a formula is not valid, though, one structure is enough.

Definition 3.9Countermodel

A countermodel of φ\varphi is a structure, together with an assignment for the free variables of φ\varphi⁠, in which φ\varphi is false. A formula is valid exactly when it has no countermodel.

Example 3.5A countermodel

Is (∀x. P x∨Q x)⟶(∀x. P x)∨(∀x. Q x)(\forall x.\ P\ x \lor Q\ x) \imp (\forall x.\ P\ x) \lor (\forall x.\ Q\ x) valid? It says: “if everything is PP or QQ⁠, then everything is PP or everything is QQ”. That sounds plausible until you picture one element that is only PP and another that is only QQ⁠: D={a,b}D = \{a, b\}⁠, P={a}P = \{a\}⁠, Q={b}Q = \{b\}⁠. The premise is true, since aa is PP and bb is QQ⁠. But ∀x. P x\forall x.\ P\ x fails at bb and ∀x. Q x\forall x.\ Q\ x fails at aa⁠, so the conclusion is false. This structure is a countermodel, and the formula is not valid. It is satisfiable, though: it is true in every structure where everything is PP⁠.

To show that a formula is valid, you argue about an arbitrary structure: the argument may use nothing about the domain and the interpretation except what the formula itself says.

Example 3.6Proving validity

Claim: (∀x. P x⟶Q x)∧P c⟶Q c(\forall x.\ P\ x \imp Q\ x) \land P\ c \imp Q\ c is valid. Take any structure M\mathcal M⁠. If the left-hand side of the implication is false in M\mathcal M⁠, the implication is true. Otherwise both conjuncts are true. The first says that P x⟶Q xP\ x \imp Q\ x holds for every value of xx⁠, in particular for the element cMc^{\mathcal M}⁠; the second says that PP holds for cMc^{\mathcal M}⁠. Together they give Q cQ\ c⁠. Either way the formula is true in M\mathcal M⁠, and since M\mathcal M was arbitrary, the formula is valid. This is the argument of Section 3.1 in miniature.

Valid formulas include every instance of a propositional tautology, such as P x∨¬ P xP\ x \lor \lnot\, P\ x⁠, and many that propositional logic cannot explain, such as (∀x. P x)⟶P c(\forall x.\ P\ x) \imp P\ c⁠.

Check it in Isabelle

In an Isabelle lemma, free variables are implicitly universal: the lemma claims its formula for every value of them of the inferred types. In the first-order example below, P and Q are arbitrary predicates, c is an arbitrary element, and the element type is unconstrained. This represents a validity claim. A lemma using a particular type such as nat, defined operations, or additional assumptions makes a claim in that context; it does not reinterpret those operations arbitrarily. blast, which Module 2 used for tautologies, also searches for proofs in predicate logic:

Isabelle
lemma "(∀x. P x ⟶ Q x) ∧ P c ⟶ Q c"
  by blast

The proof succeeds: Example 3.6 is checked. Now a claim that is not valid. From P a for one a, you cannot conclude that P holds for everything. Nitpick searches for a countermodel:

Isabelle
lemma "P a ⟶ (∀x. P x)"
  nitpick [card 'a = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2:
  Free variables:
    P = (λx. _)(a1 := False, a2 := True)
    a = a2
  Skolem constant:
    x = a1

Read Nitpick’s answer from the top.

  • card 'a = 2: the domain has two elements, which Nitpick calls a₁ and a₂. The name 'a comes from Isabelle: it gave x and a the type 'a (read “alpha”), a type variable that stands for any type, so the lemma is about every domain at once. The option [card 'a = 2] told Nitpick to try a domain of exactly two elements.
  • P = (λx. _)(a₁ := False, a₂ := True) is Nitpick’s notation for a function given by a table: P a₁ is False and P a₂ is True. So P holds for a₂ only.
  • a = a₂ is the value of the free variable a.
  • Under Skolem constant, Nitpick shows values that it chose for bound variables: x = a₁ is the element at which ∀x. P x fails.

So P a is true and ∀x. P x is false: a countermodel, as in Definition 3.9.

Isabelle
  oops

Without an option, Nitpick tries domains of 1 to 10 elements in parallel, and the size of the countermodel it reports first can change from run to run. [card 'a = 2] fixes the domain size, but does not promise the same printed witnesses or element labels on every run. With default settings Isabelle/jEdit also tries Quickcheck on new lemmas, so a counterexample may appear before you call Nitpick. Quickcheck writes a predicate as the set of elements where it is true, such as P = {a₁}.

Watch out

“Nitpick found no counterexample” does not mean that a lemma is true. Nitpick searches only domains up to a size limit, and Section 3.9 shows a formula that is not valid but whose countermodels are all infinite. Only a proof shows that a formula is valid.

5

Reasoning with quantifiers

Two formulas are equivalent, written φ≡ψ\varphi \equiv \psi⁠, if they are true in the same structures under the same assignments, that is, if φ⟷ψ\varphi \iff \psi is valid. Equivalences let you rewrite formulas: to simplify a specification, to negate a requirement, to follow a proof. This section collects the laws you will use most, and the ones that look right but fail.

Quantifier laws

Over a two-element domain {a,b}\{a, b\}⁠, ∀x. P x\forall x.\ P\ x means P a∧P bP\ a \land P\ b⁠, and ∃x. P x\exists x.\ P\ x means P a∨P bP\ a \lor P\ b⁠. So ∀ behaves like a long conjunction and ∃ like a long disjunction, and the laws of Module 2 for ∧ and ∨ have quantifier versions.

Fact 3.1Negating quantifiers
¬(∀x. φ)≡∃x. ¬φ¬(∃x. φ)≡∀x. ¬φ\lnot (\forall x.\ \varphi) \equiv \exists x.\ \lnot \varphi \qquad\qquad \lnot (\exists x.\ \varphi) \equiv \forall x.\ \lnot \varphi

“Not everything is φ\varphi” means “something is not φ\varphi”, and “nothing is φ\varphi” means “everything is not φ\varphi”. These are De Morgan’s laws for quantifiers. Pushing a negation inwards turns each quantifier it passes into the other one.

Example 3.7Negating a requirement

A requirement and its violation are negations of each other. Take “every request is answered”:

∀r. request r⟶answered r\forall r.\ \mathit{request}\ r \imp \mathit{answered}\ r

Its negation describes what a test, or a model checker, must find to show a violation:

¬(∀r. request r⟶answered r)  ≡  ∃r. ¬(request r⟶answered r)  ≡  ∃r. request r∧¬ answered r\lnot (\forall r.\ \mathit{request}\ r \imp \mathit{answered}\ r) \;\equiv\; \exists r.\ \lnot (\mathit{request}\ r \imp \mathit{answered}\ r) \;\equiv\; \exists r.\ \mathit{request}\ r \land \lnot\, \mathit{answered}\ r

“Some request is not answered.” The last step uses ¬(A⟶B)≡A∧¬B\lnot (A \imp B) \equiv A \land \lnot B from Module 2. With nested quantifiers the negation passes through each one in turn: ¬(∀x. ∃y. trusts x y)≡∃x. ∀y. ¬ trusts x y\lnot (\forall x.\ \exists y.\ \mathit{trusts}\ x\ y) \equiv \exists x.\ \forall y.\ \lnot\, \mathit{trusts}\ x\ y⁠, “somebody trusts nobody”.

Fact 3.2Distribution
∀x. (φ∧ψ)≡(∀x. φ)∧(∀x. ψ)∃x. (φ∨ψ)≡(∃x. φ)∨(∃x. ψ)\forall x.\ (\varphi \land \psi) \equiv (\forall x.\ \varphi) \land (\forall x.\ \psi) \qquad\qquad \exists x.\ (\varphi \lor \psi) \equiv (\exists x.\ \varphi) \lor (\exists x.\ \psi)

The other two combinations hold in one direction only:

(∀x. φ)∨(∀x. ψ)⊨∀x. (φ∨ψ)∃x. (φ∧ψ)⊨(∃x. φ)∧(∃x. ψ)(\forall x.\ \varphi) \lor (\forall x.\ \psi) \models \forall x.\ (\varphi \lor \psi) \qquad\qquad \exists x.\ (\varphi \land \psi) \models (\exists x.\ \varphi) \land (\exists x.\ \psi)

Example 3.5 showed that the first one-way law cannot be reversed. Nor can the second: “somebody is an admin and somebody is on call” does not mean that some admin is on call. The countermodel is again D={a,b}D = \{a, b\} with P={a}P = \{a\} and Q={b}Q = \{b\}⁠.

Fact 3.3Quantifiers and formulas without the variable

If xx does not occur free in ψ\psi⁠:

∀x. (ψ⟶φ)≡ψ⟶(∀x. φ)∀x. (φ⟶ψ)≡(∃x. φ)⟶ψ\forall x.\ (\psi \imp \varphi) \equiv \psi \imp (\forall x.\ \varphi) \qquad\qquad \forall x.\ (\varphi \imp \psi) \equiv (\exists x.\ \varphi) \imp \psi
∃x. (φ∧ψ)≡(∃x. φ)∧ψ∀x. (φ∨ψ)≡(∀x. φ)∨ψ\exists x.\ (\varphi \land \psi) \equiv (\exists x.\ \varphi) \land \psi \qquad\qquad \forall x.\ (\varphi \lor \psi) \equiv (\forall x.\ \varphi) \lor \psi

The second law surprises at first: a ∀ that moves out of the left-hand side of an implication becomes an ∃. An example makes it plausible: “for every user xx⁠, if xx is logged in, the server is busy” says the same as “if some user is logged in, the server is busy”. The law is the idea behind the proof rule for ∃ in Modules 6 and 7.

Fact 3.4Order of quantifiers
∀x. ∀y. φ≡∀y. ∀x. φ∃x. ∃y. φ≡∃y. ∃x. φ\forall x.\ \forall y.\ \varphi \equiv \forall y.\ \forall x.\ \varphi \qquad\qquad \exists x.\ \exists y.\ \varphi \equiv \exists y.\ \exists x.\ \varphi
∃y. ∀x. φ⊨∀x. ∃y. φ, but not conversely.\exists y.\ \forall x.\ \varphi \models \forall x.\ \exists y.\ \varphi, \text{ but not conversely.}

Quantifiers of the same kind can be swapped; ∀ and ∃ cannot. If one yy works for every xx⁠, then every xx has a yy⁠, namely that one. The converse fails because the yy may depend on xx⁠: every lock has a key that opens it, but there need not be a master key that opens every lock. Example 3.4 gave a structure in which ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y is true and ∃y. ∀x. R x y\exists y.\ \forall x.\ R\ x\ y false.

Check it in Isabelle

blast proves the laws. Here are four of them. S is a free variable of type bool, so it stands for a formula in which x does not occur:

Isabelle
lemma "¬ (∀x. P x) ⟷ (∃x. ¬ P x)"
  by blast

lemma "(∀x. P x ∧ Q x) ⟷ (∀x. P x) ∧ (∀x. Q x)"
  by blast

lemma "(∀x. P x ⟶ S) ⟷ ((∃x. P x) ⟶ S)"
  by blast

lemma "(∃y. ∀x. R x y) ⟶ (∀x. ∃y. R x y)"
  by blast

For a law that fails, Nitpick finds a countermodel. Here is the converse of the last lemma:

Isabelle
lemma "(∀x. ∃y. R x y) ⟶ (∃y. ∀x. R x y)"
  nitpick [card = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2 and card 'b = 2:
  Free variable:
    R = (λx. _)
        (a1 := (λx. _)(b1 := False, b2 := True),
           a2 := (λx. _)(b1 := True, b2 := False))
  Skolem constants:
    λy. x = (λx. _)(b1 := a1, b2 := a2)
    λx. y = (λx. _)(a1 := b2, a2 := b1)

This time there are two type variables. Nothing in the formula says that x and y are the same kind of thing, so Isabelle gave them different types, 'a and 'b, and R relates elements of 'a to elements of 'b. The option [card = 2] gives every type two elements. R is a table of tables: R a₁ is the function that is true only at b₂, so R a₁ b₂ holds, and likewise R a₂ b₁; all other pairs are false. Every x has a y, but no single y serves both. A countermodel with two types is not always one with a single domain, but this one is: read b₁ as a₁ and b₂ as a₂, and R relates each element of {a₁, a₂} to the other. The Skolem constants show the dependence: λx. y is the y that Nitpick chose for each x to make the left-hand side true, a function of x; λy. x gives, for each y, an x at which ∀x. R x y fails.

Isabelle
  oops
Watch out

Scope strikes when you type a law into Isabelle without the parentheses of the fact box. Leave out the parentheses around ∀x. P x ∧ Q x in the second lemma above:

Isabelle
lemma "∀x. P x ∧ Q x ⟷ (∀x. P x) ∧ (∀x. Q x)"
Output
proof (prove)
goal (1 subgoal):
 1. ∀x. (P x ∧ Q x) = ((∀x. P x) ∧ (∀x. Q x))

The goal shows Isabelle’s reading. The first ∀x takes everything to its right as its scope, so the lemma now claims, for every x, that P x ∧ Q x is equivalent to (∀x. P x) ∧ (∀x. Q x). (Isabelle prints ⟷ as =; Section 3.6 explains why.) That is a different claim, and a false one:

Isabelle
  nitpick [card 'a = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2:
  Free variables:
    P = (λx. _)(a1 := False, a2 := True)
    Q = (λx. _)(a1 := False, a2 := True)
  Skolem constant:
    x = a2

In this countermodel P and Q both hold for a₂ only. Take x = a₂: P x ∧ Q x is true, but ∀x. P x is false because of a₁, so the two sides of the equivalence differ. Put the parentheses back and blast proves the law.

Isabelle
  oops

Substitution and capture

From ∀x. φ\forall x.\ \varphi you may conclude φ\varphi for any particular object: from “every admin is on call” follows “if Alice is an admin, Alice is on call”. The step replaces the variable xx by a term.

Definition 3.10Substitution

φ[t/x]\varphi[t/x]⁠, read “φ\varphi with tt for xx”, is the formula obtained from φ\varphi by replacing every free occurrence of the variable xx with the term tt⁠, renaming bound variables first if necessary so that no free variable of tt becomes bound. Bound occurrences of xx are not replaced.

For example, (P x∧(∀x. Q x))[a/x](P\ x \land (\forall x.\ Q\ x))[a/x] is P a∧(∀x. Q x)P\ a \land (\forall x.\ Q\ x)⁠: only the first xx is free.

Fact 3.5Instantiation
∀x. φ⊨φ[t/x]φ[t/x]⊨∃x. φ\forall x.\ \varphi \models \varphi[t/x] \qquad\qquad \varphi[t/x] \models \exists x.\ \varphi

provided that no variable of tt becomes bound when tt is put in place of xx⁠.

The proviso is not a technicality. Let φ\varphi be ∃y. R x y\exists y.\ R\ x\ y⁠, “xx has an RR-successor”. Then ∀x. φ\forall x.\ \varphi says that everything has a successor. Put yy for xx carelessly and you get ∃y. R y y\exists y.\ R\ y\ y⁠, “something is its own successor”: the yy you put in has been captured by ∃y\exists y⁠, and the meaning has changed. With the natural numbers as domain and << as RR⁠, ∀x. ∃y. x<y\forall x.\ \exists y.\ x < y is true and ∃y. y<y\exists y.\ y < y is false, so the careless step leads from a truth to a falsehood. The cure is to rename the bound variable first. ∃y. R x y\exists y.\ R\ x\ y is equivalent to ∃z. R x z\exists z.\ R\ x\ z⁠, and (∃z. R x z)[y/x](\exists z.\ R\ x\ z)[y/x] is ∃z. R y z\exists z.\ R\ y\ z⁠, “yy has a successor”, which does follow.

Formulas that differ only in the names of their bound variables, such as ∃y. R x y\exists y.\ R\ x\ y and ∃z. R x z\exists z.\ R\ x\ z⁠, are called α-equivalent (alpha-equivalent). They mean the same, and substitution is always understood up to such renaming.

Example 3.8Substitution with renaming

Compute (∀y. R x y⟶P y)[f y/x](\forall y.\ R\ x\ y \imp P\ y)[f\ y/x]⁠. The term f yf\ y contains yy⁠, which the formula binds, so putting f yf\ y in directly would capture its yy⁠. First rename the bound yy to a fresh zz⁠, which gives ∀z. R x z⟶P z\forall z.\ R\ x\ z \imp P\ z⁠, then substitute:

∀z. R (f y) z⟶P z\forall z.\ R\ (f\ y)\ z \imp P\ z

The yy in f yf\ y is still free, as it was in the term you put in.

Check it in Isabelle

Isabelle never captures a variable when it substitutes. Inside Isabelle a bound variable is not stored by its name but as a number that counts the quantifiers between it and its own quantifier (a de Bruijn index); names are kept only for printing. blast proves the correct instance:

Isabelle
lemma "(∀x. ∃y. R x y) ⟶ (∃z. R y z)"
  by blast

and Nitpick refutes the captured one:

Isabelle
lemma "(∀x. ∃y. R x y) ⟶ (∃y. R y y)"
  nitpick [card 'a = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2:
  Free variable:
    R = (λx. _)
        (a1 := (λx. _)(a1 := False, a2 := True),
           a2 := (λx. _)(a1 := True, a2 := False))
  Skolem constant:
    λx. y = (λx. _)(a1 := a2, a2 := a1)

In this countermodel R relates a₁ to a₂ and a₂ to a₁: every element has a successor, and none is its own successor.

Isabelle
  oops
6

Equality

Equality is a predicate with a fixed meaning. A structure chooses what PP and RR mean, but not what == means: in every structure, t1=t2t_1 = t_2 is true exactly when t1t_1 and t2t_2 stand for the same element. That is why == counts as a logical symbol. Its laws hold in every structure.

Fact 3.6Laws of equality
  • Reflexivity: t=tt = t⁠.
  • Symmetry: t=u⟶u=tt = u \imp u = t⁠.
  • Transitivity: t=u∧u=v⟶t=vt = u \land u = v \imp t = v⁠.
  • Substitution of equals: t=u⟶(φ[t/x]⟶φ[u/x])t = u \imp (\varphi[t/x] \imp \varphi[u/x])⁠.

The last law says that equal objects have the same properties. If owner report=alice\mathit{owner}\ \mathit{report} = \mathit{alice} and admin (owner report)\mathit{admin}\ (\mathit{owner}\ \mathit{report})⁠, then admin alice\mathit{admin}\ \mathit{alice}⁠.

With equality you can count. Two variables need not stand for different objects: ∃x y. P x∧P y\exists x\ y.\ P\ x \land P\ y is true as soon as a single element has PP⁠, with xx and yy both standing for it. To say “two”, you must say that they differ.

Example 3.9Counting with equality
English Formula
at least one PP ∃x. P x\exists x.\ P\ x
at least two PP ∃x y. x≠y∧P x∧P y\exists x\ y.\ x \neq y \land P\ x \land P\ y
at most one PP ∀x y. P x∧P y⟶x=y\forall x\ y.\ P\ x \land P\ y \imp x = y
exactly one PP ∃x. P x∧(∀y. P y⟶y=x)\exists x.\ P\ x \land (\forall y.\ P\ y \imp y = x)

x≠yx \neq y abbreviates ¬(x=y)\lnot (x = y)⁠. “At most one” does not claim that any PP exists: when nothing is PP⁠, it is vacuously true. “Exactly one” combines “at least one” with “at most one”. Isabelle has a quantifier for it: ∃! x. P x\exists!\, x.\ P\ x reads “there is exactly one xx such that P xP\ x”.

Check it in Isabelle

Two details of = in Isabelle catch many beginners.

First, = binds more tightly than ¬. So ¬ x = y means ¬ (x = y), and Isabelle prints it with ≠:

Isabelle
lemma "¬ x = y"
Output
proof (prove)
goal (1 subgoal):
 1. x ≠ y
Isabelle
  oops

Second, between two formulas = means “if and only if”: on the type bool of formulas, = and ⟷ are the same function. Isabelle even prints ⟷ as =, as you saw in Section 3.5. But the parser gives the two symbols different priorities: = binds more tightly than ∧, while ⟷ binds more loosely, like ⟶. Compare:

Isabelle
lemma "(A = B ∧ C) ⟷ (A ⟷ B ∧ C)"
Output
proof (prove)
goal (1 subgoal):
 1. (A = B ∧ C) = (A = (B ∧ C))

The goal shows Isabelle’s reading. A = B ∧ C is (A = B) ∧ C, but A ⟷ B ∧ C is A ⟷ (B ∧ C), which Isabelle prints as A = (B ∧ C). The two are not equivalent:

Isabelle
  nitpick
Output
Nitpicking formula...

Nitpick found a counterexample:
  Free variables:
    A = False
    B = True
    C = False

With A false, B true and C false, the left-hand side is false and the right-hand side true. Hence the rule: write = between terms, which stand for objects, and ⟷ between formulas.

Isabelle
  oops

The ∃! quantifier is written as in the text (type ∃, then !), and blast confirms its meaning:

Isabelle
lemma "(∃!x. P x) ⟷ (∃x. P x ∧ (∀y. P y ⟶ y = x))"
  by blast
7

Writing specifications

Turning requirements into formulas is a craft, and a formula is only as good as the translation behind it. A routine helps:

  1. Fix the domain and the vocabulary: which objects, which properties and relations, which functions.
  2. Translate each requirement on its own, using the patterns below.
  3. Read the formula back in plain English, literally, symbol by symbol, and compare it with the requirement.
  4. Test it on small structures: one that should satisfy it and one that should not. The model explorer of Section 3.3 and Nitpick both help.

Most requirements follow a handful of patterns. AA and BB stand for properties.

English Formula Note
every AA is BB ∀x. A x⟶B x\forall x.\ A\ x \imp B\ x ∀ goes with ⟶
some AA is BB ∃x. A x∧B x\exists x.\ A\ x \land B\ x ∃ goes with ∧
no AA is BB ∀x. A x⟶¬ B x\forall x.\ A\ x \imp \lnot\, B\ x the same as ¬(∃x. A x∧B x)\lnot (\exists x.\ A\ x \land B\ x)
only AAs are BB ∀x. B x⟶A x\forall x.\ B\ x \imp A\ x “only” reverses the arrow
at most one AA ∀x y. A x∧A y⟶x=y\forall x\ y.\ A\ x \land A\ y \imp x = y Section 3.6
exactly one AA ∃x. A x∧(∀y. A y⟶y=x)\exists x.\ A\ x \land (\forall y.\ A\ y \imp y = x) Section 3.6
Watch out

∀ with ∧. “Every admin is on call” is not ∀x. admin x∧on_call x\forall x.\ \mathit{admin}\ x \land \mathit{on\_call}\ x⁠: that says that everybody is an admin and everybody is on call. With ∀, the restriction to admins is a condition, so it goes in front of ⟶.

∃ with ⟶. “Some admin is on call” is not ∃x. admin x⟶on_call x\exists x.\ \mathit{admin}\ x \imp \mathit{on\_call}\ x⁠: that is true as soon as one person is not an admin, because for that person the implication is vacuously true. With ∃, the restriction is part of what must hold, so it is joined with ∧.

Watch out

“Only”. “Only admins can delete documents” means that whoever can delete a document is an admin: ∀u d. can_delete u d⟶admin u\forall u\ d.\ \mathit{can\_delete}\ u\ d \imp \mathit{admin}\ u⁠. It does not say that admins can delete anything; that would be a separate requirement, with the arrow the other way round.

“At most one” without ≠. ¬(∃x y. holds x∧holds y)\lnot (\exists x\ y.\ \mathit{holds}\ x \land \mathit{holds}\ y)⁠, meant as “no two users hold the lock”, forbids even a single user to hold it: take yy to be xx⁠. Write ∀x y. holds x∧holds y⟶x=y\forall x\ y.\ \mathit{holds}\ x \land \mathit{holds}\ y \imp x = y⁠.

Example 3.10An access policy, formalised

A document system has users and documents. In this example both live in one domain, and the predicates user\mathit{user} and doc\mathit{doc} say which kind an object is; such predicates are called guards. The other symbols are admin u\mathit{admin}\ u⁠, owns u d\mathit{owns}\ u\ d⁠, can_read u d\mathit{can\_read}\ u\ d and can_delete u d\mathit{can\_delete}\ u\ d⁠.

We assume that admins are users and that ownership and permissions relate users to documents. These are modelling assumptions, not consequences of the predicate names. For example, we require ∀u. admin u⟶user u\forall u.\ \mathit{admin}\ u \imp \mathit{user}\ u⁠, and likewise ∀u d. owns u d⟶user u∧doc d\forall u\ d.\ \mathit{owns}\ u\ d \imp \mathit{user}\ u \land \mathit{doc}\ d for ownership, with corresponding restrictions for the two permission predicates.

  1. Every document has exactly one owner, and the owner is a user:
    ∀d. doc d⟶(∃u. user u∧owns u d∧(∀v. owns v d⟶v=u))\forall d.\ \mathit{doc}\ d \imp (\exists u.\ \mathit{user}\ u \land \mathit{owns}\ u\ d \land (\forall v.\ \mathit{owns}\ v\ d \imp v = u))
  2. Only the owner of a document or an admin can delete it:
    ∀u d. can_delete u d⟶owns u d∨admin u\forall u\ d.\ \mathit{can\_delete}\ u\ d \imp \mathit{owns}\ u\ d \lor \mathit{admin}\ u
  3. Admins can read every document:
    ∀u d. admin u∧doc d⟶can_read u d\forall u\ d.\ \mathit{admin}\ u \land \mathit{doc}\ d \imp \mathit{can\_read}\ u\ d
  4. Some admin owns no document:
    ∃u. admin u∧¬(∃d. doc d∧owns u d)\exists u.\ \mathit{admin}\ u \land \lnot (\exists d.\ \mathit{doc}\ d \land \mathit{owns}\ u\ d)

Now read them back. Requirement 1 is the “exactly one” pattern inside a ∀. Requirement 2 contains “only”, so the arrow points from can_delete\mathit{can\_delete} to the condition. Requirement 3 needs the guard doc d\mathit{doc}\ d⁠: without it, it would also say that admins can read users. Requirement 4 is the “some AA is BB” pattern, with “owns no document” written as a negated ∃.

Check it in Isabelle

Isabelle can compare two translations. If they are equivalent, blast proves the ⟷. If they are not, Nitpick may find a structure that tells them apart. Comparing that structure with the intended requirement helps you judge which translation is appropriate; it does not make that decision for you. Here are the two ways of saying “exactly one” from Section 3.6:

Isabelle
lemma "(∃x. holds x ∧ (∀y. holds y ⟶ y = x)) ⟷
       (∃x. holds x) ∧ (∀x y. holds x ∧ holds y ⟶ x = y)"
  by blast

And here is the correct translation of “some admin is on call” against the mistaken one:

Isabelle
lemma "(∃x. admin x ∧ on_call x) ⟷ (∃x. admin x ⟶ on_call x)"
  nitpick [card 'a = 1]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 1:
  Free variables:
    admin = (λx. _)(a1 := False)
    on_call = (λx. _)(a1 := False)

A single element that is not an admin: the mistaken formula is true there, and the correct one false.

Isabelle
  oops

For more practice in translating, work through Logic, quantifiers, and specifications in the Mathematical Foundations series.

8

Types: from many-sorted logic to HOL

The policy of Example 3.10 needed the guards user\mathit{user} and doc\mathit{doc} because one domain held two kinds of object. Guards are tedious, and forgetting one changes the meaning without any warning. Leave the guard out of requirement 3 and it says that admins can read users. Write “every document has an owner” as ∀d. ∃u. owns u d\forall d.\ \exists u.\ \mathit{owns}\ u\ d and you have claimed that every object, users included, is owned by something. And a function symbol such as owner\mathit{owner} must give a value even for a user, because a structure interprets it by a function on the whole domain.

Definition 3.11Many-sorted logic

In a many-sorted or typed logic, the vocabulary gives each constant, each argument of a function or predicate symbol and each function result a sort, also called a type. A structure has one non-empty domain per sort in the convention used here, and each variable ranges over the domain of one sort: ∀d::doc. ∃u::user. owns u d\forall d{::}\mathit{doc}.\ \exists u{::}\mathit{user}.\ \mathit{owns}\ u\ d reads “for every document dd there is a user uu who owns dd”. An expression that puts an argument of the wrong sort somewhere is not a formula at all.

Many-sorted first-order logic can be translated into one-domain first-order logic. Quantifiers become guarded quantifiers: ∀d::doc. φ\forall d{::}\mathit{doc}.\ \varphi becomes ∀d. doc d⟶φ\forall d.\ \mathit{doc}\ d \imp \varphi⁠, and ∃u::user. φ\exists u{::}\mathit{user}.\ \varphi becomes ∃u. user u∧φ\exists u.\ \mathit{user}\ u \land \varphi⁠. The translation also needs assumptions that the sorts are non-empty and that constants and functions respect their declared sorts. Types make such restrictions part of the language, so wrong-sort arguments become errors that a computer finds before proof. This translation concerns first-order sorts; HOL also adds higher-order quantification.

Isabelle/HOL is typed through and through. Every term has a type. Formulas are terms of type bool, whose two values are True and False. A predicate on objects of type τ (tau) is a function of type τ ⇒ bool, which maps each object to True or False, and a relation between τ and σ (sigma) is a function of type τ ⇒ σ ⇒ bool. So P x in Isabelle is a function applied to an argument, and the result is a formula. Because predicates and functions are values like any other, HOL also lets you quantify over them, as in ∀P. P x ⟶ P x: that is what makes it a higher-order logic. Module 8 covers HOL’s types and functions in depth.

To declare a vocabulary in Isabelle, you declare its types with typedecl and its symbols with consts:

Isabelle
typedecl user
typedecl doc

consts
  admin :: "user ⇒ bool"
  owns :: "user ⇒ doc ⇒ bool"
  can_delete :: "user ⇒ doc ⇒ bool"
  alice :: user
  report :: doc

typedecl user declares a new type about which nothing is known except that it has at least one element: a domain. consts declares constants with their types but without definitions. They are uninterpreted, exactly like the symbols of a vocabulary: a structure decides what they mean, and Isabelle proves a lemma about them only if it holds whatever they mean. Here, at last, is the argument of Section 3.1:

Isabelle
lemma "(∀u d. admin u ⟶ can_delete u d) ∧ admin alice ⟶ can_delete alice report"
  by blast

The variables u and d need no guards: their types follow from admin u and can_delete u d. Now something that does not follow. If owners can delete their documents, can Alice delete the report? That permission would follow if she owned it, but we have not assumed that she does. The premise also does not forbid other ways of obtaining permission. Nitpick needs the option show_consts to display the values of constants:

Isabelle
lemma "(∀u d. owns u d ⟶ can_delete u d) ⟶ can_delete alice report"
  nitpick [card user = 2, card doc = 1, show_consts]
Output
Nitpicking formula...

Nitpick found a counterexample for card doc = 1 and card user = 2:
  Constants:
    alice = u1
    can_delete =
      (λx. _)(u1 := (λx. _)(d1 := False), u2 := (λx. _)(d1 := False))
    owns = (λx. _)(u1 := (λx. _)(d1 := False), u2 := (λx. _)(d1 := False))
    report = d1

Nitpick names the elements after their types: u₁ and u₂ are users, d₁ is a document. In this structure nobody owns anything, so the premise is vacuously true, and nobody can delete anything.

Isabelle
  oops

Types catch mistakes like those of the pitfall in Section 3.2, and more. Swap the arguments of owns:

Isabellefails on purpose
lemma "owns report alice"
Output
Type unification failed: Clash of types "doc" and "user"

Type error in application: incompatible operand type

Operator:  owns :: user ⇒ doc ⇒ bool
Operand:   report :: doc

Isabelle refuses the statement before trying to prove anything. The message names the function (Operator) and the argument that does not fit (Operand): owns expects a user first, and report is a doc.

When you write a lemma without declarations, Isabelle infers the most general types. In (∀x. P x) ⟶ (∃x. P x) nothing says what x is, so x gets a type variable 'a, and P the type 'a ⇒ bool. A lemma proved for a type variable holds for every type, that is, for every domain:

Isabelle
lemma "(∀x. P x) ⟶ (∃x. P x)"
  by blast

The formula is valid because every type, like every domain of a structure, has at least one element. Over an empty domain, ∀x. P x would be vacuously true and ∃x. P x false.

If something went wrong
  • A predicate with too many arguments. Use P with one argument in one place and two in another, and Isabelle cannot give it a type:

    Isabellefails on purpose
    lemma "P x ⟶ P x y"
    Output
    Type unification failed: Clash of types "_ ⇒ _" and "bool"
    
    Type error in application: operator not of function type
    
    Operator:  P x :: bool
    Operand:   y :: ??'a
    

    From P x, Isabelle decided that P x is a formula, of type bool, and a formula cannot be applied to y; ??'a stands for a type that Isabelle had not yet worked out. Give each predicate the same number of arguments everywhere.

  • A name that is a keyword. Some English words are keywords of Isabelle’s command language, and file is one of them:

    Isabellefails on purpose
    typedecl file
    Output
    Outer syntax error⌂: name expected,
    but keyword file⌂ was found

    Choose another name, such as doc.

9

Decidability: what tools can and cannot do

For propositional logic, Module 2 gave an algorithm that decides validity: compute the truth table. It may be slow, but it always finishes with the right answer. Is there such an algorithm for predicate logic? David Hilbert and Wilhelm Ackermann asked this in 1928, as the Entscheidungsproblem, the “decision problem”. The answer came in two parts.

Fact 3.7Gödel’s completeness theorem

There are proof systems for predicate logic, natural deduction among them (Module 6), in which a formula can be proved exactly when it is valid. Kurt Gödel proved this for one such system in his doctoral thesis of 1929, published in 1930.

Completeness has a practical consequence. Proofs are finite texts that a program can check, so a program can list all proofs, shortest first, together with the formulas they prove. Every valid formula appears in the list sooner or later. To find out whether a formula is valid, run the program and wait for the formula to appear: if it is valid, the answer “yes” comes in the end. If it is not, you wait for ever, and at no point do you know whether waiting longer would help.

Fact 3.8Undecidability of predicate logic

No algorithm decides, for every formula of predicate logic, whether it is valid. Alonzo Church and Alan Turing proved this independently in 1936. Validity stays undecidable even when the vocabulary has a single predicate symbol, with two arguments.

Turing’s proof connects to the halting problem that Module 1 mentioned: for every program you can write a formula that is valid exactly when the program halts, so an algorithm for validity would decide halting, which no algorithm can.

Definition 3.12Decidable and semi-decidable

A question about inputs is decidable if some algorithm answers it correctly for every input and always terminates. It is semi-decidable if some algorithm answers “yes” for every input whose answer is yes, but may run for ever on the others.

Validity in predicate logic is semi-decidable but not decidable. The same holds for unsatisfiability, since φ\varphi is unsatisfiable exactly when ¬φ\lnot \varphi is valid; satisfiability is not even semi-decidable.

Two theorems of Gödel are easily confused. The completeness theorem (1929/1930) is about the valid formulas, those true in every structure: all of them can be proved. His incompleteness theorems (1931) concern the formulas true in one particular structure, the natural numbers: no proof system whose axioms a program can check, and which proves only true statements, proves all of them. HOL can describe the natural numbers, so incompleteness applies to Isabelle’s logic too.

What proof search and countermodel search can settlesystematic proof search(enumerating all proofs)ValidA proof exists(Gödel, 1929/30).Exhaustive proof searcheventually finds one.for example(∃y. ∀x. R x y) ⟶(∀x. ∃y. R x y)finite-model search(enumerating structures)Not valid,finite countermodelSearching finite structuresfinds a countermodelin the end.for example(∀x. ∃y. R x y) ⟶(∃y. ∀x. R x y)neither exhaustivesearch settles itNot valid, onlyinfinite countermodelsNo first-order proof,and no finite countermodel.for exampleR irreflexive, transitive⟶ ¬ (∀x. ∃y. R x y)No algorithm decides, for every formula, which box it belongs to (Church, Turing, 1936).
Figure 3.3

What a search can settle in principle. Valid formulas have proofs, and a systematic proof search finds one in the end. Formulas with a finite countermodel are refuted by searching finite structures. For formulas whose countermodels are all infinite, neither search ever stops. Isabelle’s tools search only up to a limit, so in practice they can also give up on formulas of the first two kinds.

Why do some formulas defeat both kinds of search? Because a countermodel can be infinite. Take

(∀x. ¬ R x x)∧(∀x y z. R x y∧R y z⟶R x z)⟶¬(∀x. ∃y. R x y)(\forall x.\ \lnot\, R\ x\ x) \land (\forall x\ y\ z.\ R\ x\ y \land R\ y\ z \imp R\ x\ z) \imp \lnot (\forall x.\ \exists y.\ R\ x\ y)

Here irreflexive means that no element is related to itself, and transitive means that R x yR\ x\ y and R y zR\ y\ z together imply R x zR\ x\ z⁠. The formula says: “If RR is irreflexive and transitive, then some element has no RR-successor.” It is not valid: with the natural numbers as domain and << as RR⁠, both premises hold, and every number has a larger one. But it is true in every finite structure (Exercise 3.8 asks you to show why), so a search through finite structures never finds a countermodel, and a proof search never finds a proof, because there is none. Such formulas must exist. If every formula that is not valid had a finite countermodel, you could run a proof search and a countermodel search side by side; one of them would always finish, and validity would be decidable, which Church and Turing showed it is not.

Check it in Isabelle

Both of Isabelle’s tools give up on this formula. Nitpick tries domains of 1 to 10 elements:

Isabelle
lemma "(∀x. ¬ R x x) ∧ (∀x y z. R x y ∧ R y z ⟶ R x z) ⟶ ¬ (∀x. ∃y. R x y)"
  nitpick
Output
Nitpicking formula...

Nitpick found no counterexample
Isabelle
  oops

and blast fails:

Isabellefails on purpose
lemma "(∀x. ¬ R x x) ∧ (∀x y z. R x y ∧ R y z ⟶ R x z) ⟶ ¬ (∀x. ∃y. R x y)"
  by blast
Output
Failed to apply initial proof method⌂:
goal (1 subgoal):
 1. (∀x. ¬ R x x) ∧ (∀x y z. R x y ∧ R y z ⟶ R x z) ⟶ ¬ (∀x. ∃y. R x y)

blast searches for a proof with a growing bound on its “unsafe” steps, such as choosing an instance of a ∀ that it may need again later, from 0 up to 20 by default, and then gives up with this message. Neither message settles the question. You need to think, and here the thought is the infinite countermodel above.

What this means in practice:

  • Proof search (simp, blast, auto, and the provers that Sledgehammer calls in Module 9) can close a goal, leave subgoals, fail or reach a resource limit. A completed proof gives a theorem; an unfinished attempt does not establish that the claim is false.
  • Countermodel search (Nitpick and Quickcheck) is the other half: a genuine counterexample shows that a formula is not valid; finding none shows nothing.
  • When both give up, the work is yours: find the idea of the proof and guide Isabelle through it, as Parts II and III teach, or find a countermodel that the tools cannot see.

Not everything is undecidable. Propositional logic is decidable, and so is the part of predicate logic in which every predicate takes one argument and there are no function symbols. SAT and SMT solvers (Module 1) can solve many useful instances of decidable fragments efficiently, although no practical running time is guaranteed for every instance. That is why Isabelle’s tools call them for help.

Key idea

Validity in predicate logic is semi-decidable: a systematic search finds a proof whenever one exists, but no program can always tell you that none exists. That is why Isabelle offers both provers and counterexample finders, why automation sometimes gives up on true statements, and why the rest of this series teaches you to guide proofs yourself.

10

What you learned

  • Predicate logic adds terms, which name objects, predicates, which state properties and relations, and the quantifiers ∀ (“for all”) and ∃ (“there exists”) to propositional logic.
  • In Isabelle a quantifier’s scope extends as far to the right as possible. Occurrences of a variable in the scope of its quantifier are bound, the others free; a sentence has no free variables.
  • A structure is a non-empty domain with an interpretation of the vocabulary. A false ∀ has a counterexample, a true ∃ a witness.
  • A formula is valid if it is true in every structure, satisfiable if it is true in some. One countermodel refutes validity; validity needs an argument about every structure.
  • Negation swaps ∀ and ∃; ∀ distributes over ∧ and ∃ over ∨, but not the other way round; ∃y. ∀x implies ∀x. ∃y, but not conversely. Substitution must not capture variables.
  • For restricted statements such as “every admin is on call”, use ∀ with ⟶; for “some admin is on call”, use ∃ with ∧. These are translation patterns, not restrictions on which connectives may occur under a quantifier. “Only” introduces a necessary condition, and counting uses equality.
  • In Isabelle, predicates are functions to bool; typedecl and consts declare a vocabulary; free variables of a lemma are implicitly universal; blast proves first-order goals and nitpick finds countermodels. = binds more tightly than ¬ and ∧, ⟷ as loosely as ⟶.
  • Validity is semi-decidable (Gödel’s completeness theorem) but undecidable (Church and Turing, 1936): provers may give up on valid formulas, and countermodel finders miss infinite countermodels.
11

Exercises

Exercise 3.1★Free or bound?

For each formula, say which occurrences of variables are free, and which quantifier each bound occurrence belongs to. Which formulas are sentences? Use Isabelle’s convention for scopes.

  1. ∀x. R x y⟶(∃y. R y x)\forall x.\ R\ x\ y \imp (\exists y.\ R\ y\ x)
  2. (∃x. P x)∧Q x(\exists x.\ P\ x) \land Q\ x
  3. ∃x. P x∧Q x\exists x.\ P\ x \land Q\ x
  4. ∀x. (∃x. R x x)⟶P x\forall x.\ (\exists x.\ R\ x\ x) \imp P\ x
Show solution
  1. The scope of ∀x\forall x is all the rest of the formula. In R x yR\ x\ y⁠, the xx belongs to ∀x\forall x and the yy is free. In ∃y. R y x\exists y.\ R\ y\ x⁠, the yy belongs to ∃y\exists y and the xx to ∀x\forall x⁠. Not a sentence, because of the first yy⁠.
  2. The parentheses close the scope of ∃x\exists x after P xP\ x⁠, so the xx in Q xQ\ x is free. Not a sentence.
  3. The scope of ∃x\exists x runs to the end, so both occurrences of xx belong to it. A sentence. Formulas 2 and 3 differ only in their parentheses, and they mean different things.
  4. Both occurrences of xx in R x xR\ x\ x belong to the inner ∃x\exists x⁠, whose scope ends at the closing parenthesis. The xx in P xP\ x belongs to ∀x\forall x⁠. A sentence.
Exercise 3.2★True in a structure?

Let M2\mathcal M_2 have the domain {1,2,3}\{1, 2, 3\}⁠, PM2={1,3}P^{\mathcal M_2} = \{1, 3\} (“is odd”), and let R x yR\ x\ y hold when x<yx < y⁠. Decide whether each sentence is true in M2\mathcal M_2⁠. Give a witness for each true ∃ and a counterexample for each false ∀.

  1. ∀x. ∃y. R x y\forall x.\ \exists y.\ R\ x\ y
  2. ∃y. ∀x. ¬ R y x\exists y.\ \forall x.\ \lnot\, R\ y\ x
  3. ∀x. P x⟶(∃y. R x y)\forall x.\ P\ x \imp (\exists y.\ R\ x\ y)
  4. ∃x. ∀y. x=y∨R x y\exists x.\ \forall y.\ x = y \lor R\ x\ y
  5. ∀x y. R x y⟶¬ R y x\forall x\ y.\ R\ x\ y \imp \lnot\, R\ y\ x
Show solution
  1. False. Counterexample x=3x = 3⁠: no element of the domain is larger than 3.
  2. True. Witness y=3y = 3⁠: R 3 xR\ 3\ x is false for every xx⁠, since nothing is larger than 3.
  3. False. Counterexample x=3x = 3⁠: 3 is odd, and no element is larger. (For x=1x = 1 the witness y=2y = 2 works, and for x=2x = 2 the implication is vacuously true.)
  4. True. Witness x=1x = 1⁠: for y=1y = 1⁠, x=yx = y holds; for y=2y = 2 and y=3y = 3⁠, R 1 yR\ 1\ y holds.
  5. True. x<yx < y and y<xy < x never hold together.

You can check all five in the model explorer of Section 3.3 on a copy of M2\mathcal M_2⁠: let aa⁠, bb and cc stand for 1, 2 and 3, tick PP for aa and cc⁠, and switch RR on for the pairs (a,b)(a, b)⁠, (a,c)(a, c) and (b,c)(b, c)⁠.

Exercise 3.3★From English to formulas

The vocabulary has the one-place predicates train\mathit{train} and block\mathit{block}⁠, the two-place predicate occupies t b\mathit{occupies}\ t\ b (“train tt occupies block bb”) and the constant depot\mathit{depot}⁠, which denotes a block. Translate each sentence independently; do not assume the other sentences yet:

  1. Every train occupies some block.
  2. No block is occupied by two different trains.
  3. Some block is free, that is, occupied by no train.
  4. Only trains occupy blocks.
  5. Exactly one train occupies the depot.
Show solution
  1. ∀t. train t⟶(∃b. block b∧occupies t b)\forall t.\ \mathit{train}\ t \imp (\exists b.\ \mathit{block}\ b \land \mathit{occupies}\ t\ b)
  2. ∀b t u. block b∧train t∧train u∧occupies t b∧occupies u b⟶t=u\forall b\ t\ u.\ \mathit{block}\ b \land \mathit{train}\ t \land \mathit{train}\ u \land \mathit{occupies}\ t\ b \land \mathit{occupies}\ u\ b \imp t = u⁠. An equivalent form follows the English more closely: ¬(∃b t u. block b∧train t∧train u∧t≠u∧occupies t b∧occupies u b)\lnot (\exists b\ t\ u.\ \mathit{block}\ b \land \mathit{train}\ t \land \mathit{train}\ u \land t \neq u \land \mathit{occupies}\ t\ b \land \mathit{occupies}\ u\ b)⁠.
  3. ∃b. block b∧¬(∃t. train t∧occupies t b)\exists b.\ \mathit{block}\ b \land \lnot (\exists t.\ \mathit{train}\ t \land \mathit{occupies}\ t\ b)
  4. ∀x b. block b∧occupies x b⟶train x\forall x\ b.\ \mathit{block}\ b \land \mathit{occupies}\ x\ b \imp \mathit{train}\ x⁠: anything occupying a block must be a train.
  5. ∃t. train t∧occupies t depot∧(∀u. train u∧occupies u depot⟶u=t)\exists t.\ \mathit{train}\ t \land \mathit{occupies}\ t\ \mathit{depot} \land (\forall u.\ \mathit{train}\ u \land \mathit{occupies}\ u\ \mathit{depot} \imp u = t)

In 1 and 3, ∀ goes with ⟶ and ∃ with ∧, as the patterns of Section 3.7 say. In 2, the train guards restrict the statement to trains as requested. If sentence 4 is also assumed, those guards become redundant for occupants of blocks, but they are needed when translating sentence 2 on its own.

Exercise 3.4★★Valid or not?

Decide whether each formula is valid. For a valid one, give a short argument; for the others, give a countermodel. Then check your answers in Isabelle with blast or with nitpick [card = 2].

  1. (∃x. P x∧Q x)⟶(∃x. P x)∧(∃x. Q x)(\exists x.\ P\ x \land Q\ x) \imp (\exists x.\ P\ x) \land (\exists x.\ Q\ x)
  2. (∃x. P x)∧(∃x. Q x)⟶(∃x. P x∧Q x)(\exists x.\ P\ x) \land (\exists x.\ Q\ x) \imp (\exists x.\ P\ x \land Q\ x)
  3. (∀x. P x⟶Q x)⟶(∃x. P x)⟶(∃x. Q x)(\forall x.\ P\ x \imp Q\ x) \imp (\exists x.\ P\ x) \imp (\exists x.\ Q\ x)
  4. (∀x. ∃y. R x y)⟶(∀x. ∃y. R y x)(\forall x.\ \exists y.\ R\ x\ y) \imp (\forall x.\ \exists y.\ R\ y\ x)
Show solution
  1. Valid. A witness for ∃x. P x∧Q x\exists x.\ P\ x \land Q\ x is a witness for both ∃x. P x\exists x.\ P\ x and ∃x. Q x\exists x.\ Q\ x⁠.
  2. Not valid: D={a,b}D = \{a, b\}⁠, P={a}P = \{a\}⁠, Q={b}Q = \{b\}⁠. Something is PP and something is QQ⁠, but nothing is both.
  3. Valid. Take a witness xx of P xP\ x⁠; the premise gives Q xQ\ x for it, so it is a witness of ∃x. Q x\exists x.\ Q\ x⁠.
  4. Not valid. “Everybody trusts somebody” does not imply “everybody is trusted by somebody”: D={a,b}D = \{a, b\} with R={(a,a),(b,a)}R = \{(a, a), (b, a)\}⁠. Everybody trusts aa⁠, and nobody trusts bb⁠.

In Isabelle:

Isabelle
lemma "(∃x. P x ∧ Q x) ⟶ (∃x. P x) ∧ (∃x. Q x)"
  by blast

lemma "(∀x. P x ⟶ Q x) ⟶ (∃x. P x) ⟶ (∃x. Q x)"
  by blast
Isabelle
lemma "(∃x. P x) ∧ (∃x. Q x) ⟶ (∃x. P x ∧ Q x)"
  nitpick [card = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2:
  Free variables:
    P = (λx. _)(a1 := False, a2 := True)
    Q = (λx. _)(a1 := True, a2 := False)
  Skolem constants:
    x = a2
    x = a1
Isabelle
  oops
Isabelle
lemma "(∀x. ∃y. R x y) ⟶ (∀x. ∃y. R y x)"
  nitpick [card = 2]
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2 and card 'b = 2:
  Free variable:
    R = (λx. _)
        (a1 := (λx. _)(b1 := False, b2 := True),
           a2 := (λx. _)(b1 := False, b2 := True))
  Skolem constants:
    x = b1
    λx. y = (λx. _)(a1 := b2, a2 := b2)
Isabelle
  oops

In formula 4, nothing forces x and y to have the same type, so Isabelle gives them two types, as in Section 3.5, and Nitpick’s countermodel relates the elements of 'a to those of 'b; the option [card = 2] fixes the size of both. Read b₁ as a₁ and b₂ as a₂ to get a countermodel with one domain: everybody trusts a₂, and nobody trusts a₁. Nitpick’s countermodels may differ from yours in which element is which; any countermodel will do.

Exercise 3.5★★Negate a specification

Negate each requirement and push the negation inwards until it stands only in front of atomic formulas. Then say in English what a violation looks like.

  1. ∀r. request r⟶(∃s. server s∧handles s r)\forall r.\ \mathit{request}\ r \imp (\exists s.\ \mathit{server}\ s \land \mathit{handles}\ s\ r)
  2. ∃s. server s∧(∀r. request r⟶handles s r)\exists s.\ \mathit{server}\ s \land (\forall r.\ \mathit{request}\ r \imp \mathit{handles}\ s\ r)
  3. ∀x y. holds x∧holds y⟶x=y\forall x\ y.\ \mathit{holds}\ x \land \mathit{holds}\ y \imp x = y
Show solution
  1. ∃r. request r∧(∀s. server s⟶¬ handles s r)\exists r.\ \mathit{request}\ r \land (\forall s.\ \mathit{server}\ s \imp \lnot\, \mathit{handles}\ s\ r)⁠: some request is handled by no server. The steps: ¬∀\lnot \forall becomes ∃¬\exists \lnot⁠; ¬(A⟶B)\lnot (A \imp B) becomes A∧¬BA \land \lnot B⁠; ¬∃\lnot \exists becomes ∀¬\forall \lnot⁠; and ¬(C∧D)\lnot (C \land D) becomes C⟶¬DC \imp \lnot D⁠, an equivalence that a truth table confirms.
  2. ∀s. server s⟶(∃r. request r∧¬ handles s r)\forall s.\ \mathit{server}\ s \imp (\exists r.\ \mathit{request}\ r \land \lnot\, \mathit{handles}\ s\ r)⁠: every server fails to handle some request, so no server handles them all.
  3. ∃x y. holds x∧holds y∧x≠y\exists x\ y.\ \mathit{holds}\ x \land \mathit{holds}\ y \land x \neq y⁠: two different users hold the lock.
Exercise 3.6★★Counting in Isabelle
  1. Prove with blast that the negation of “at most one element satisfies P” is “at least two elements satisfy P”, using the formulas of Example 3.9.
  2. Somebody writes “exactly one element satisfies P” as ∃x. ∀y. P y ⟶ y = x. Use Nitpick to compare this formula with ∃!x. P x, and explain what the formula really says.
Show solution
  1. Push the negation through: ¬∀\lnot \forall becomes ∃¬\exists \lnot⁠, and ¬(P x∧P y⟶x=y)\lnot (P\ x \land P\ y \imp x = y) becomes P x∧P y∧x≠yP\ x \land P\ y \land x \neq y⁠.

    Isabelle
    lemma "¬ (∀x y. P x ∧ P y ⟶ x = y) ⟷
           (∃x y. x ≠ y ∧ P x ∧ P y)"
      by blast
  2. Nitpick finds a structure in which the two formulas differ:

    Isabelle
    lemma "(∃x. ∀y. P y ⟶ y = x) ⟷ (∃!x. P x)"
      nitpick [card 'a = 1]
    Output
    Nitpicking formula...
    
    Nitpick found a counterexample for card 'a = 1:
      Free variable:
        P = (λx. _)(a1 := False)
    Isabelle
      oops

    In a one-element domain where P holds for nothing, ∃x. ∀y. P y ⟶ y = x is true (vacuously, for every x), but ∃!x. P x is false. The formula says “at most one element satisfies P”: it forgets to say that one does.

Exercise 3.7★★★Substitution

Compute each substitution, renaming bound variables where necessary:

  1. (∀y. R x y)[z/x](\forall y.\ R\ x\ y)[z/x]
  2. (∀y. R x y)[y/x](\forall y.\ R\ x\ y)[y/x]
  3. (∃x. R x y∧P x)[f x/y](\exists x.\ R\ x\ y \land P\ x)[f\ x/y]
  4. (P x∧(∃x. R x y))[g y/x](P\ x \land (\exists x.\ R\ x\ y))[g\ y/x]

Then explain what goes wrong if you conclude ∃y. y≠y\exists y.\ y \neq y from ∀x. ∃y. y≠x\forall x.\ \exists y.\ y \neq x by putting yy for xx⁠, and in which structures the premise is true.

Show solution
  1. ∀y. R z y\forall y.\ R\ z\ y⁠. The term zz contains no bound variable of the formula, so nothing needs renaming.
  2. Putting yy in directly would give ∀y. R y y\forall y.\ R\ y\ y⁠, which captures it. Rename first: ∀w. R y w\forall w.\ R\ y\ w⁠.
  3. The term f xf\ x contains xx⁠, which the formula binds. Rename the bound xx to ww⁠, giving ∃w. R w y∧P w\exists w.\ R\ w\ y \land P\ w⁠, then substitute: ∃w. R w (f x)∧P w\exists w.\ R\ w\ (f\ x) \land P\ w⁠.
  4. Only the first xx is free, and the term g yg\ y contains no variable that the formula binds: P (g y)∧(∃x. R x y)P\ (g\ y) \land (\exists x.\ R\ x\ y)⁠.

The premise ∀x. ∃y. y≠x\forall x.\ \exists y.\ y \neq x says that every element has a different element, which is true in every structure with at least two elements. Putting yy for xx without renaming captures the yy and gives ∃y. y≠y\exists y.\ y \neq y⁠, which is false in every structure. The correct instance renames first: ∃w. w≠y\exists w.\ w \neq y⁠, “something differs from yy”, which does follow.

Exercise 3.8★★★Only infinite countermodels

Show that the formula of Section 3.9, (∀x. ¬ R x x)∧(∀x y z. R x y∧R y z⟶R x z)⟶¬(∀x. ∃y. R x y)(\forall x.\ \lnot\, R\ x\ x) \land (\forall x\ y\ z.\ R\ x\ y \land R\ y\ z \imp R\ x\ z) \imp \lnot (\forall x.\ \exists y.\ R\ x\ y)⁠, is true in every finite structure. Hint: suppose a finite structure in which RR is irreflexive and transitive and every element has an RR-successor, start at any element and keep following successors.

Show solution

Suppose, to the contrary, that M\mathcal M is finite, RR is irreflexive and transitive in M\mathcal M⁠, and every element has an RR-successor. Pick any element x0x_0⁠, then x1x_1 with R x0 x1R\ x_0\ x_1⁠, then x2x_2 with R x1 x2R\ x_1\ x_2⁠, and so on. Applying transitivity repeatedly gives R xi xjR\ x_i\ x_j whenever i<ji < j⁠. The domain is finite, so the sequence must repeat: xi=xjx_i = x_j for some i<ji < j⁠. Then R xi xjR\ x_i\ x_j is R xi xiR\ x_i\ x_i⁠, which irreflexivity forbids. So in a finite structure where the premises hold, some element has no successor, and the formula is true.

The argument fails for an infinite domain, because the sequence need not repeat: in the natural numbers with <<⁠, the sequence 0,1,2,…0, 1, 2, \dots goes on for ever. That is the infinite countermodel of Section 3.9.

Exercise 3.9★★★A typed policy in Isabelle

Extend the vocabulary of Section 3.8 with can_read :: "user ⇒ doc ⇒ bool" and a second user bob :: user. Different constant names do not by themselves require different users; do not assume bob ≠ alice. Take these requirements: nobody can delete a document they cannot read, and owners can delete their documents.

  1. Prove with blast: if, in addition, Alice owns the report, then Alice can read the report.
  2. Use Nitpick to show that the requirements and admin bob do not imply that Bob can read the report. Explain the countermodel.
Show solution
Isabelle
consts
  can_read :: "user ⇒ doc ⇒ bool"
  bob :: user

lemma "(∀u d. can_delete u d ⟶ can_read u d) ∧
       (∀u d. owns u d ⟶ can_delete u d) ∧
       owns alice report ⟶ can_read alice report"
  by blast

Alice owns the report, so she can delete it, so she can read it: two instances of the ∀-requirements, which blast finds.

Isabelle
lemma "(∀u d. can_delete u d ⟶ can_read u d) ∧
       (∀u d. owns u d ⟶ can_delete u d) ∧
       admin bob ⟶ can_read bob report"
  nitpick [card user = 1, card doc = 1, show_consts]
Output
Nitpicking formula...

Nitpick found a counterexample for card doc = 1 and card user = 1:
  Constants:
    admin = (λx. _)(u1 := True)
    can_delete = (λx. _)(u1 := (λx. _)(d1 := False))
    owns = (λx. _)(u1 := (λx. _)(d1 := False))
    report = d1
    bob = u1
    can_read = (λx. _)(u1 := (λx. _)(d1 := False))
Isabelle
  oops

In the one-user countermodel, Alice and Bob denote the same user. Bob is an admin, but nobody owns anything and nobody can read or delete anything; both requirements hold, vacuously. Nothing in the requirements gives admins any rights. If admins should read everything, that is a further requirement, such as requirement 3 of Example 3.10.

12

Check yourself

1
Which formula says “every admin is on call”?
2
With Isabelle’s scope convention, which occurrence is free in ∀x. P x⟶(∃y. R x y)∧Q y\forall x.\ P\ x \imp (\exists y.\ R\ x\ y) \land Q\ y⁠?
3
Which formula is valid?
4
What does a countermodel of a formula show?
5
How does Isabelle read ¬ x = y ∧ P x?
6
Nitpick answers “Nitpick found no counterexample” for a lemma. What do you know?
Finished this module?