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:
- Every administrator can delete every document.
- Alice is an administrator, and the quarterly report is a document.
- Therefore Alice can delete the quarterly report.
In propositional logic each sentence becomes an atom, say , and , and the argument becomes the formula . Its truth table:
| A | B | C | A ∧ B ⟶ C |
|---|---|---|---|
| T | T | T | T |
| T | T | F | F |
| T | F | T | T |
| T | F | F | T |
| F | T | T | T |
| F | T | F | T |
| F | F | T | T |
| F | F | F | T |
Satisfiable but not valid: true in 7 rows.
The second row makes the formula false: and true, 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:
Here means “ 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, , uses predicates, functions and equality in this way. Isabelle supplies their typed definitions in its richer logic, HOL.
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.
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: , , ;
- function symbols name operations that give an object: (one argument: the owner of a document), (two arguments);
- predicate symbols name properties and relations, which hold or do not hold: (one argument), (two), (two).
Most logic books write , and . This series writes them as Isabelle does: the symbol, then its arguments, separated by spaces: , , . Parentheses only group: applies to two arguments, and . Symbols such as and stay between their arguments, as usual.
Terms
A term is either
- a variable (, , , …) or a constant, or
- , where is a function symbol with arguments and are terms.
A term names an object. It is never true or false.
, , , and are terms. names the owner of whichever document stands for, and 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
An atomic formula is either , where is a predicate symbol with arguments and are terms, or an equation between two terms.
, , and 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.
Terms and formulas live on different levels, and one cannot stand where the other belongs. is not a formula: needs an object, and is a statement. Nor is : joins statements, and is an object. Isabelle rejects such expressions, as Section 3.8 shows.
Quantifiers
The new ingredient is quantification over objects. Here (the Greek letter phi) and (psi) stand for any formulas.
- reads “for all , ”. It says that holds whichever object stands for. is the universal quantifier.
- reads “there exists an such that ”, or “for some , ”. It says that holds for at least one object. is the existential quantifier.
One quantifier may introduce several variables: is short for .
The formulas over a vocabulary are built by these rules:
- Every atomic formula is a formula, and so are and .
- If and are formulas, so are , , , and , with parentheses where the priorities of Module 2 need them.
- If is a formula and is a variable, then and are formulas.
Reading formulas fluently takes practice. Read them aloud, symbol by symbol, then say what they mean in plain English.
| Formula | Read aloud | In plain English |
|---|---|---|
| for all , if is an admin then is on call | every admin is on call | |
| there is an such that is an admin and is not on call | some admin is not on call | |
| for all there is a such that trusts | everybody trusts somebody | |
| there is a such that for all , trusts | somebody is trusted by everybody | |
| for all and , if then there is a with and | 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 the formula is the scope of the quantifier: the region in which 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 means . To end a scope earlier, use parentheses: .
Many logic textbooks use the opposite convention: there a quantifier binds as tightly as , and means . 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.
An occurrence of a variable in a formula is bound if it lies in the scope of a quantifier or ; 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.
Scopes in . The scope of runs to the end, so every is bound. The parentheses close the scope of , so the last is free: a different variable from the bound , 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: is neither true nor false until has a value, while 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 and say the same thing.
- : the scope of is , and both occurrences of are bound. A sentence.
- : the scope is only . The in is free.
- : and are bound, is free. Not a sentence.
- : the inner binds only the of . The scope of runs to the end, so the in belongs to . A sentence, but a confusing one: give each quantifier a variable of its own.
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:
lemma "∀x. (P x ⟶ Q x)"proof (prove)
goal (1 subgoal):
1. ∀x. P x ⟶ Q xThe 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:
oopslemma "(∀x. P x) ⟶ Q x"proof (prove)
goal (1 subgoal):
1. (∀x. P x) ⟶ Q xThese 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.
oopsStructures and truth
Is true? The question has no answer yet. If and range over the natural numbers and is , it is true: every number has a larger one. If 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.
A structure for a vocabulary consists of
- a non-empty set , the domain: the objects that variables range over;
- for each constant , an element of ;
- for each function symbol with arguments, a function that maps every -tuple of elements of to an element of ;
- for each predicate symbol with arguments, a set of -tuples of elements of : the tuples for which holds.
An -tuple is a list of elements, in order; a 2-tuple is a pair. Read as “ in ”: what makes the symbol mean; likewise and . 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.
An assignment gives every variable a value in . The value of a term is computed from the values of its variables and constants, using the functions of . A formula is true in under according to these rules:
- is true when the tuple of the values of is in ; is true when the two terms have the same value.
- is true and is false; , , , and follow their truth tables from Module 2.
- is true when is true for every value of in , the other variables keeping their values.
- is true when is true for at least one value of in .
For a sentence the assignment makes no difference. You then write , read “ is true in ” or “ is a model of ”.
Over a finite domain, a quantifier is a loop. is a loop that succeeds only if every round
succeeds; succeeds as soon as one round does. In Python,
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, and , and a two-place predicate .
The structure . Its domain has three elements; holds for and , for only; an arrow from to means that holds.
Written out, has the domain , with , and . If a concrete reading helps, think of three servers, of as “is in production”, as “is backed up”, and as “ forwards its logs to ”. The evaluation does not depend on the reading.
-
, “every is a ”. Try each element. For , is , which is false. One failure is enough: the formula is false in , and is a counterexample.
-
, “some is a ”. For , is true. The formula is true, and is a witness.
-
, “some element has an arrow to itself”. True, with the witness .
-
, “every has an arrow to a ”. This one needs a table, with a row for each value of :
an arrow from to a ? true yes: and true true no: its only arrow goes to , and is false false false no true The row for is false, so the formula is false in , with the counterexample .
In , the sentence says that every element has an outgoing arrow. It does: , and . Each has its own witness .
says that some element receives an arrow from every element, itself included. Try each candidate : receives no arrow; receives an arrow from only; receives arrows from and , but not from . No candidate works, so the sentence is false.
The two sentences differ only in the order of and . In the first, may depend on ; in the second, one must serve every .
A ∀-sentence can also be true for a less obvious reason. In a structure where no element has the property , the sentence is true whatever is: for every the condition 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.
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:
value "∀x::bool. ∃y. y ≠ x""True"
:: "bool"value "∃y::bool. ∀x. y ≠ x""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.
Validity, satisfiability and countermodels
The central notions of Module 2 carry over, with “every row of the truth table” replaced by “every structure”.
A formula is
- valid, written , 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) entails , written , if is true in every structure and under every assignment that make all the formulas in true. is then a logical consequence of .
As in Module 2, is valid exactly when 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.
A countermodel of is a structure, together with an assignment for the free variables of , in which is false. A formula is valid exactly when it has no countermodel.
Is valid? It says: “if everything is or , then everything is or everything is ”. That sounds plausible until you picture one element that is only and another that is only : , , . The premise is true, since is and is . But fails at and fails at , 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 .
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.
Claim: is valid. Take any structure . If the left-hand side of the implication is false in , the implication is true. Otherwise both conjuncts are true. The first says that holds for every value of , in particular for the element ; the second says that holds for . Together they give . Either way the formula is true in , and since 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 , and many that propositional logic cannot explain, such as .
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:
lemma "(∀x. P x ⟶ Q x) ∧ P c ⟶ Q c"
by blastThe 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:
lemma "P a ⟶ (∀x. P x)"
nitpick [card 'a = 2]Nitpicking formula...
Nitpick found a counterexample for card 'a = 2:
Free variables:
P = (λx. _)(a1 := False, a2 := True)
a = a2
Skolem constant:
x = a1Read Nitpick’s answer from the top.
card 'a = 2: the domain has two elements, which Nitpick calls a₁ and a₂. The name'acomes from Isabelle: it gavexandathe 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₁isFalseandP a₂isTrue. SoPholds for a₂ only.a = a₂is the value of the free variablea.- Under Skolem constant, Nitpick shows values that it chose for bound variables:
x = a₁is the element at which∀x. P xfails.
So P a is true and ∀x. P x is false: a countermodel, as in Definition 3.9.
oopsWithout 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₁}.
“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.
Reasoning with quantifiers
Two formulas are equivalent, written , if they are true in the same structures under the same assignments, that is, if 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 , means , and means . So ∀ behaves like a long conjunction and ∃ like a long disjunction, and the laws of Module 2 for ∧ and ∨ have quantifier versions.
“Not everything is ” means “something is not ”, and “nothing is ” means “everything is not ”. These are De Morgan’s laws for quantifiers. Pushing a negation inwards turns each quantifier it passes into the other one.
A requirement and its violation are negations of each other. Take “every request is answered”:
Its negation describes what a test, or a model checker, must find to show a violation:
“Some request is not answered.” The last step uses from Module 2. With nested quantifiers the negation passes through each one in turn: , “somebody trusts nobody”.
The other two combinations hold in one direction only:
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 with and .
If does not occur free in :
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 , if 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.
Quantifiers of the same kind can be swapped; ∀ and ∃ cannot. If one works for every , then every has a , namely that one. The converse fails because the may depend on : 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 is true and false.
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:
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 blastFor a law that fails, Nitpick finds a countermodel. Here is the converse of the last lemma:
lemma "(∀x. ∃y. R x y) ⟶ (∃y. ∀x. R x y)"
nitpick [card = 2]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.
oopsScope 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:
lemma "∀x. P x ∧ Q x ⟷ (∀x. P x) ∧ (∀x. Q x)"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:
nitpick [card 'a = 2]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 = a2In 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.
oopsSubstitution and capture
From you may conclude 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 by a term.
, read “ with for ”, is the formula obtained from by replacing every free occurrence of the variable with the term , renaming bound variables first if necessary so that no free variable of becomes bound. Bound occurrences of are not replaced.
For example, is : only the first is free.
provided that no variable of becomes bound when is put in place of .
The proviso is not a technicality. Let be , “ has an -successor”. Then says that everything has a successor. Put for carelessly and you get , “something is its own successor”: the you put in has been captured by , and the meaning has changed. With the natural numbers as domain and as , is true and is false, so the careless step leads from a truth to a falsehood. The cure is to rename the bound variable first. is equivalent to , and is , “ has a successor”, which does follow.
Formulas that differ only in the names of their bound variables, such as and , are called α-equivalent (alpha-equivalent). They mean the same, and substitution is always understood up to such renaming.
Compute . The term contains , which the formula binds, so putting in directly would capture its . First rename the bound to a fresh , which gives , then substitute:
The in is still free, as it was in the term you put in.
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:
lemma "(∀x. ∃y. R x y) ⟶ (∃z. R y z)"
by blastand Nitpick refutes the captured one:
lemma "(∀x. ∃y. R x y) ⟶ (∃y. R y y)"
nitpick [card 'a = 2]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.
oopsEquality
Equality is a predicate with a fixed meaning. A structure chooses what and mean, but not what means: in every structure, is true exactly when and stand for the same element. That is why counts as a logical symbol. Its laws hold in every structure.
- Reflexivity: .
- Symmetry: .
- Transitivity: .
- Substitution of equals: .
The last law says that equal objects have the same properties. If and , then .
With equality you can count. Two variables need not stand for different objects: is true as soon as a single element has , with and both standing for it. To say “two”, you must say that they differ.
| English | Formula |
|---|---|
| at least one | |
| at least two | |
| at most one | |
| exactly one |
abbreviates . “At most one” does not claim that any exists: when nothing is , it is vacuously true. “Exactly one” combines “at least one” with “at most one”. Isabelle has a quantifier for it: reads “there is exactly one such that ”.
Two details of = in Isabelle catch many beginners.
First, = binds more tightly than ¬. So ¬ x = y means ¬ (x = y), and Isabelle prints it with ≠:
lemma "¬ x = y"proof (prove)
goal (1 subgoal):
1. x ≠ y oopsSecond, 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:
lemma "(A = B ∧ C) ⟷ (A ⟷ B ∧ C)"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:
nitpickNitpicking formula...
Nitpick found a counterexample:
Free variables:
A = False
B = True
C = FalseWith 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.
oopsThe ∃! quantifier is written as in the text (type ∃, then !), and blast confirms its meaning:
lemma "(∃!x. P x) ⟷ (∃x. P x ∧ (∀y. P y ⟶ y = x))"
by blastWriting specifications
Turning requirements into formulas is a craft, and a formula is only as good as the translation behind it. A routine helps:
- Fix the domain and the vocabulary: which objects, which properties and relations, which functions.
- Translate each requirement on its own, using the patterns below.
- Read the formula back in plain English, literally, symbol by symbol, and compare it with the requirement.
- 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. and stand for properties.
| English | Formula | Note |
|---|---|---|
| every is | ∀ goes with ⟶ | |
| some is | ∃ goes with ∧ | |
| no is | the same as | |
| only s are | “only” reverses the arrow | |
| at most one | Section 3.6 | |
| exactly one | Section 3.6 |
∀ with ∧. “Every admin is on call” is not : 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 : 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 ∧.
“Only”. “Only admins can delete documents” means that whoever can delete a document is an admin: . 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 ≠. , meant as “no two users hold the lock”, forbids even a single user to hold it: take to be . Write .
A document system has users and documents. In this example both live in one domain, and the predicates and say which kind an object is; such predicates are called guards. The other symbols are , , and .
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 , and likewise for ownership, with corresponding restrictions for the two permission predicates.
- Every document has exactly one owner, and the owner is a user:
- Only the owner of a document or an admin can delete it:
- Admins can read every document:
- Some admin owns no document:
Now read them back. Requirement 1 is the “exactly one” pattern inside a ∀. Requirement 2 contains “only”, so the arrow points from to the condition. Requirement 3 needs the guard : without it, it would also say that admins can read users. Requirement 4 is the “some is ” pattern, with “owns no document” written as a negated ∃.
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:
lemma "(∃x. holds x ∧ (∀y. holds y ⟶ y = x)) ⟷
(∃x. holds x) ∧ (∀x y. holds x ∧ holds y ⟶ x = y)"
by blastAnd here is the correct translation of “some admin is on call” against the mistaken one:
lemma "(∃x. admin x ∧ on_call x) ⟷ (∃x. admin x ⟶ on_call x)"
nitpick [card 'a = 1]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.
oopsFor more practice in translating, work through Logic, quantifiers, and specifications in the Mathematical Foundations series.
Types: from many-sorted logic to HOL
The policy of Example 3.10 needed the guards and 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 and you have claimed that every object, users included, is owned by something. And a function symbol such as must give a value even for a user, because a structure interprets it by a function on the whole domain.
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: reads “for every document there is a user who owns ”. 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: becomes , and becomes . 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:
typedecl user
typedecl doc
consts
admin :: "user ⇒ bool"
owns :: "user ⇒ doc ⇒ bool"
can_delete :: "user ⇒ doc ⇒ bool"
alice :: user
report :: doctypedecl 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:
lemma "(∀u d. admin u ⟶ can_delete u d) ∧ admin alice ⟶ can_delete alice report"
by blastThe 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:
lemma "(∀u d. owns u d ⟶ can_delete u d) ⟶ can_delete alice report"
nitpick [card user = 2, card doc = 1, show_consts]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 = d1Nitpick 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.
oopsTypes catch mistakes like those of the pitfall in Section 3.2, and more. Swap the arguments of owns:
lemma "owns report alice"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:
lemma "(∀x. P x) ⟶ (∃x. P x)"
by blastThe 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
Pwith one argument in one place and two in another, and Isabelle cannot give it a type:lemma "P x ⟶ P x y"Type unification failed: Clash of types "_ ⇒ _" and "bool" Type error in application: operator not of function type Operator: P x :: bool Operand: y :: ??'aFrom
P x, Isabelle decided thatP xis a formula, of typebool, and a formula cannot be applied toy;??'astands 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
fileis one of them:typedecl fileOuter syntax error⌂: name expected, but keyword file⌂ was foundChoose another name, such as
doc.
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.
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.
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.
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 is unsatisfiable exactly when 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 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
Here irreflexive means that no element is related to itself, and transitive means that and together imply . The formula says: “If is irreflexive and transitive, then some element has no -successor.” It is not valid: with the natural numbers as domain and as , 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.
Both of Isabelle’s tools give up on this formula. Nitpick tries domains of 1 to 10 elements:
lemma "(∀x. ¬ R x x) ∧ (∀x y z. R x y ∧ R y z ⟶ R x z) ⟶ ¬ (∀x. ∃y. R x y)"
nitpickNitpicking formula...
Nitpick found no counterexample oopsand blast fails:
lemma "(∀x. ¬ R x x) ∧ (∀x y z. R x y ∧ R y z ⟶ R x z) ⟶ ¬ (∀x. ∃y. R x y)"
by blastFailed 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.
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.
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;typedeclandconstsdeclare a vocabulary; free variables of a lemma are implicitly universal;blastproves first-order goals andnitpickfinds 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.
Exercises
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.
Show solution
- The scope of is all the rest of the formula. In , the belongs to and the is free. In , the belongs to and the to . Not a sentence, because of the first .
- The parentheses close the scope of after , so the in is free. Not a sentence.
- The scope of runs to the end, so both occurrences of belong to it. A sentence. Formulas 2 and 3 differ only in their parentheses, and they mean different things.
- Both occurrences of in belong to the inner , whose scope ends at the closing parenthesis. The in belongs to . A sentence.
Let have the domain , (“is odd”), and let hold when . Decide whether each sentence is true in . Give a witness for each true ∃ and a counterexample for each false ∀.
Show solution
- False. Counterexample : no element of the domain is larger than 3.
- True. Witness : is false for every , since nothing is larger than 3.
- False. Counterexample : 3 is odd, and no element is larger. (For the witness works, and for the implication is vacuously true.)
- True. Witness : for , holds; for and , holds.
- True. and never hold together.
You can check all five in the model explorer of Section 3.3 on a copy of : let , and stand for 1, 2 and 3, tick for and , and switch on for the pairs , and .
The vocabulary has the one-place predicates and , the two-place predicate (“train occupies block ”) and the constant , which denotes a block. Translate each sentence independently; do not assume the other sentences yet:
- Every train occupies some block.
- No block is occupied by two different trains.
- Some block is free, that is, occupied by no train.
- Only trains occupy blocks.
- Exactly one train occupies the depot.
Show solution
- . An equivalent form follows the English more closely: .
- : anything occupying a block must be a train.
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.
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].
Show solution
- Valid. A witness for is a witness for both and .
- Not valid: , , . Something is and something is , but nothing is both.
- Valid. Take a witness of ; the premise gives for it, so it is a witness of .
- Not valid. “Everybody trusts somebody” does not imply “everybody is trusted by somebody”: with . Everybody trusts , and nobody trusts .
In 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 blastlemma "(∃x. P x) ∧ (∃x. Q x) ⟶ (∃x. P x ∧ Q x)"
nitpick [card = 2]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 oopslemma "(∀x. ∃y. R x y) ⟶ (∀x. ∃y. R y x)"
nitpick [card = 2]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) oopsIn 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.
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.
Show solution
- : some request is handled by no server. The steps: becomes ; becomes ; becomes ; and becomes , an equivalence that a truth table confirms.
- : every server fails to handle some request, so no server handles them all.
- : two different users hold the lock.
- Prove with
blastthat the negation of “at most one element satisfiesP” is “at least two elements satisfyP”, using the formulas of Example 3.9. - 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
-
Push the negation through: becomes , and becomes .
lemma "¬ (∀x y. P x ∧ P y ⟶ x = y) ⟷ (∃x y. x ≠ y ∧ P x ∧ P y)" by blast -
Nitpick finds a structure in which the two formulas differ:
lemma "(∃x. ∀y. P y ⟶ y = x) ⟷ (∃!x. P x)" nitpick [card 'a = 1]Nitpicking formula... Nitpick found a counterexample for card 'a = 1: Free variable: P = (λx. _)(a1 := False)oopsIn a one-element domain where
Pholds for nothing,∃x. ∀y. P y ⟶ y = xis true (vacuously, for everyx), but∃!x. P xis false. The formula says “at most one element satisfiesP”: it forgets to say that one does.
Compute each substitution, renaming bound variables where necessary:
Then explain what goes wrong if you conclude from by putting for , and in which structures the premise is true.
Show solution
- . The term contains no bound variable of the formula, so nothing needs renaming.
- Putting in directly would give , which captures it. Rename first: .
- The term contains , which the formula binds. Rename the bound to , giving , then substitute: .
- Only the first is free, and the term contains no variable that the formula binds: .
The premise says that every element has a different element, which is true in every structure with at least two elements. Putting for without renaming captures the and gives , which is false in every structure. The correct instance renames first: , “something differs from ”, which does follow.
Show that the formula of Section 3.9, , is true in every finite structure. Hint: suppose a finite structure in which is irreflexive and transitive and every element has an -successor, start at any element and keep following successors.
Show solution
Suppose, to the contrary, that is finite, is irreflexive and transitive in , and every element has an -successor. Pick any element , then with , then with , and so on. Applying transitivity repeatedly gives whenever . The domain is finite, so the sequence must repeat: for some . Then is , 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 goes on for ever. That is the infinite countermodel of Section 3.9.
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.
- Prove with
blast: if, in addition, Alice owns the report, then Alice can read the report. - Use Nitpick to show that the requirements and
admin bobdo not imply that Bob can read the report. Explain the countermodel.
Show solution
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 blastAlice owns the report, so she can delete it, so she can read it: two instances of the ∀-requirements,
which blast finds.
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]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)) oopsIn 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.
Check yourself
¬ x = y ∧ P x?= binds more tightly than ¬, and ¬ more tightly than ∧. Isabelle prints the formula as x ≠ y ∧ P x.