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

Module 4Sets, relations and functions

Describe collections with sets, connect their elements with relations, find the states a system can reach, compare the sizes of sets with functions, and see why some infinite sets are larger than others, with examples and proofs checked in Isabelle.

⏱ 210 minYou need: Modules 1–3

In this module you will

  • Describe sets by listing and by set-builder notation, and prove set laws by turning them into propositional logic
  • Decide whether a relation is reflexive, symmetric, antisymmetric, transitive or total, with a witness whenever a property fails
  • Recognise equivalence relations and their partitions, partial orders and their Hasse diagrams, and well-founded relations
  • Compute the closures of a relation and use them to find the states a system can reach
  • Classify functions as injective, surjective or bijective, and use them to compare the sizes of sets
  • Explain the pigeonhole principle and Cantor’s diagonal argument, and check both in Isabelle
1

Sets

Specifications talk about collections all the time: the users who may read a file, the states a system can reach, the messages in transit. Mathematics has one notion for all of them, the set, and many specification languages are built on it: Z and B on set theory, Alloy on relations, TLA+ on sets and functions. This module builds the vocabulary in layers: sets, then relations, which connect elements, then functions, and finally the sizes of sets. Everything rests on the logic of Modules 2 and 3; you will see that the laws of sets are laws of logic in disguise. For more practice with the mathematics, see “Sets, relations, and discrete structures” in Mathematical Foundations.

Elements and set-builder notation

A set is a collection of things, called its elements, determined entirely by which things belong to it. x∈Ax \in A reads “x is an element of A”, or “x is in A”; x∉Ax \notin A reads “x is not in A” and abbreviates ¬(x∈A)\lnot (x \in A)⁠.

There are two ways to describe a set. A listing names its elements between braces: {2,3,5,7}\{2, 3, 5, 7\}⁠. Set-builder notation describes them by a property: {x.  P x}\{x.\; P\,x\} reads “the set of all x such that P x”. The primes below 10 form the set {x.  x is prime∧x<10}\{x.\; x \text{ is prime} \land x < 10\}⁠, which is {2,3,5,7}\{2, 3, 5, 7\} again. Many books write {x∣P(x)}\{x \mid P(x)\}⁠; this series writes {x.  P x}\{x.\; P\,x\}⁠, as Isabelle does. A common variant takes the elements from a given set: {x∈A.  P x}\{x \in A.\; P\,x\} means {x.  x∈A∧P x}\{x.\; x \in A \land P\,x\}⁠.

Set-builder notation connects sets with the predicates of Module 3. Something belongs to {x.  P x}\{x.\; P\,x\} exactly when it has the property PP⁠:

a∈{x.  P x}⟷P aa \in \{x.\; P\,x\} \iff P\,a

So a set and a predicate carry the same information. In Isabelle/HOL, a set of elements of some type is, in effect, a predicate on that type.

Definition 4.1Set

A set is a collection of elements; x∈Ax \in A means that xx is an element of AA⁠. A set can be given by a listing such as {1,2,3}\{1, 2, 3\}⁠, or by set-builder notation {x.  P x}\{x.\; P\,x\}⁠, the set of all xx for which P xP\,x holds. In Isabelle/HOL every set has an element type: a set of natural numbers has type nat set, and all its elements are natural numbers.

When are two sets equal? When they have the same elements. Nothing else counts: not the order in which you list them, not how you describe them.

Definition 4.2Extensionality

Two sets are equal when they have the same elements:

A=B⟷(∀x.  x∈A⟷x∈B)A = B \iff (\forall x.\; x \in A \iff x \in B)

So {1,2,3}={3,1,2}={1,1,2,3}\{1, 2, 3\} = \{3, 1, 2\} = \{1, 1, 2, 3\}⁠, and {x.  x∈Z∧x2=1}={−1,1}\{x.\; x \in \Z \land x^2 = 1\} = \{-1, 1\}⁠.

Definition 4.3Subset, empty set, universe

AA is a subset of BB⁠, written A⊆BA \subseteq B⁠, when every element of AA is an element of BB⁠: ∀x.  x∈A⟶x∈B\forall x.\; x \in A \imp x \in B⁠. It is a proper subset, A⊂BA \subset B⁠, when also A≠BA \neq B⁠. The empty set ∅\emptyset has no elements; Isabelle writes it {}. The universe contains every element of the type; Isabelle calls it UNIV.

Fact 4.1Subsets and equality

For all sets AA and BB⁠:

  1. ∅⊆A\emptyset \subseteq A and A⊆AA \subseteq A⁠.
  2. A=B⟷A⊆B∧B⊆AA = B \iff A \subseteq B \land B \subseteq A⁠.

For the first, x∈∅x \in \emptyset is false for every xx⁠, so x∈∅⟶x∈Ax \in \emptyset \imp x \in A is vacuously true (Module 2); vacuous truth will come back in this module. And x∈A⟶x∈Ax \in A \imp x \in A holds whatever xx is. The second combines extensionality with the law (p⟷q)⟷(p⟶q)∧(q⟶p)(p \iff q) \iff (p \imp q) \land (q \imp p)⁠. It is also how you usually prove two sets equal: show that each is a subset of the other.

Because HOL is typed (Module 3), a universe is always the universe of one type: UNIV :: bool set is {True,False}\{\mathit{True}, \mathit{False}\}⁠, and UNIV :: nat set contains all natural numbers. There is no set of absolutely everything, and Section 4.7 shows why that is a good thing.

Example 4.1Who may do what

A file server has the users alice, bob, carol and dave. Those who may read a certain file form the set R={alice,bob,carol}R = \{\mathit{alice}, \mathit{bob}, \mathit{carol}\}⁠, those who may write it W={bob,dave}W = \{\mathit{bob}, \mathit{dave}\}⁠, and the administrators D={bob}D = \{\mathit{bob}\}⁠. Then D⊆WD \subseteq W holds: every administrator may write. The policy “writers must be able to read” is the statement W⊆RW \subseteq R⁠, and it is violated, with dave as the witness: dave∈W\mathit{dave} \in W but dave∉R\mathit{dave} \notin R⁠.

Watch out

Keep ∈\in and ⊆\subseteq apart. 1∈{1,2}1 \in \{1, 2\} is about an element, {1}⊆{1,2}\{1\} \subseteq \{1, 2\} about a set, and both are true. In HOL, {1}∈{1,2}\{1\} \in \{1, 2\} is not even false: it is a type error, because the elements of {1,2}\{1, 2\} are numbers and {1}\{1\} is a set. In the same way, ∅\emptyset and {∅}\{\emptyset\} differ: the first has no elements, the second has one.

Operations on sets

Each operation on sets is a logical connective applied to membership.

Definition 4.4Union, intersection, difference, complement
A∪B={x.  x∈A∨x∈B}unionA∩B={x.  x∈A∧x∈B}intersectionA−B={x.  x∈A∧x∉B}difference−A={x.  x∉A}complement\begin{aligned} A \cup B &= \{x.\; x \in A \lor x \in B\} && \text{union}\\ A \cap B &= \{x.\; x \in A \land x \in B\} && \text{intersection}\\ A - B &= \{x.\; x \in A \land x \notin B\} && \text{difference}\\ -A &= \{x.\; x \notin A\} && \text{complement} \end{aligned}

AA and BB are disjoint when A∩B=∅A \cap B = \emptyset⁠. Isabelle writes A ∪ B, A ∩ B, A - B and - A.

Read them as “A union B”, “A intersect B”, “A minus B” and “the complement of A”. The complement contains everything of the type that is not in AA⁠. Figure 4.1 draws the four operations.

Union, intersection, difference and complement as Venn diagramsunionA ∪ BABUNIVx ∈ A ∨ x ∈ BintersectionA ∩ BABUNIVx ∈ A ∧ x ∈ BdifferenceA - BABUNIVx ∈ A ∧ x ∉ Bcomplement- AAUNIVx ∉ A
Figure 4.1

The four operations as Venn diagrams. The rectangle is the universe, each circle a set, and the shaded region the result. Under each diagram is the condition on xx that the region stands for.

On the file server, R∩W={bob}R \cap W = \{\mathit{bob}\} may read and write, R−WR - W (alice and carol) may only read, and W−R={dave}W - R = \{\mathit{dave}\} is the user who violates the policy.

Definition 4.5Power set

The power set P(A)\pow(A) of a set AA is the set of all its subsets: P(A)={B.  B⊆A}\pow(A) = \{B.\; B \subseteq A\}⁠. Isabelle writes Pow A.

P({1,2,3})\pow(\{1, 2, 3\}) has eight elements: ∅\emptyset⁠, {1}\{1\}⁠, {2}\{2\}⁠, {3}\{3\}⁠, {1,2}\{1, 2\}⁠, {1,3}\{1, 3\}⁠, {2,3}\{2, 3\} and {1,2,3}\{1, 2, 3\}⁠. Choosing a subset means deciding for each element whether it is in or out, so a set of nn elements has 2n2^n subsets. Power sets grow fast: a program with forty on-off feature flags has 2402^{40} configurations, more than a million million, one for each subset of the flags.

Set laws are logic in disguise

Since every operation is defined by a connective, every law of the connectives (Module 2) gives a law of sets. Each law in the table also has a dual, with ∪\cup and ∩\cap swapped (and ∅\emptyset and UNIV\mathit{UNIV}⁠), that holds as well.

Name Law of sets Law of logic behind it
Commutativity A∪B=B∪AA \cup B = B \cup A p∨q⟷q∨pp \lor q \iff q \lor p
Associativity A∩(B∩C)=(A∩B)∩CA \cap (B \cap C) = (A \cap B) \cap C p∧(q∧r)⟷(p∧q)∧rp \land (q \land r) \iff (p \land q) \land r
Distributivity A∩(B∪C)=(A∩B)∪(A∩C)A \cap (B \cup C) = (A \cap B) \cup (A \cap C) p∧(q∨r)⟷(p∧q)∨(p∧r)p \land (q \lor r) \iff (p \land q) \lor (p \land r)
De Morgan −(A∪B)=−A∩−B-(A \cup B) = -A \cap -B ¬(p∨q)⟷¬p∧¬q\lnot (p \lor q) \iff \lnot p \land \lnot q
Identity A∪∅=AA \cup \emptyset = A p∨False⟷pp \lor \mathit{False} \iff p
Complement A∪−A=UNIVA \cup -A = \mathit{UNIV} p∨¬p⟷Truep \lor \lnot p \iff \mathit{True}
Double complement −(−A)=A-(-A) = A ¬¬p⟷p\lnot \lnot p \iff p
Absorption A∪(A∩B)=AA \cup (A \cap B) = A p∨(p∧q)⟷pp \lor (p \land q) \iff p
Fact 4.2Set laws from propositional laws

Translate an equation between sets built with ∪\cup⁠, ∩\cap⁠, −- and complement into propositional logic: replace each set AA by a proposition pAp_A that stands for “x∈Ax \in A”, ∪\cup by ∨\lor⁠, ∩\cap by ∧\land⁠, A−BA - B by pA∧¬pBp_A \land \lnot p_B⁠, complement by ¬\lnot⁠, ∅\emptyset by False\mathit{False} and UNIV\mathit{UNIV} by True\mathit{True}⁠. The equation holds for all sets exactly when the two formulas are equivalent, that is, when their biconditional is a tautology.

If the formulas are equivalent, every xx is in the left set exactly when it is in the right one, and extensionality makes the sets equal. If they are not, the truth table has a row where they differ: put a single element xx into exactly those sets whose propositions are true in that row, and xx is in one side but not the other. So a falsifying row is a counterexample with one element.

Example 4.2Distributivity, step by step

To prove A∩(B∪C)=(A∩B)∪(A∩C)A \cap (B \cup C) = (A \cap B) \cup (A \cap C)⁠, take any xx and rewrite its membership:

x∈A∩(B∪C)⟷x∈A∧(x∈B∨x∈C)definitions of ∩ and ∪⟷(x∈A∧x∈B)∨(x∈A∧x∈C)distributivity of ∧ over ∨⟷x∈(A∩B)∪(A∩C)definitions of ∩ and ∪\begin{aligned} x \in A \cap (B \cup C) &\iff x \in A \land (x \in B \lor x \in C) && \text{definitions of } \cap \text{ and } \cup\\ &\iff (x \in A \land x \in B) \lor (x \in A \land x \in C) && \text{distributivity of } \land \text{ over } \lor\\ &\iff x \in (A \cap B) \cup (A \cap C) && \text{definitions of } \cap \text{ and } \cup \end{aligned}

This holds for every xx⁠, so the sets are equal by extensionality. The middle step is the only real work, and it is propositional. Write AA⁠, BB and CC for “x∈Ax \in A”, “x∈Bx \in B” and “x∈Cx \in C”, and the build checks it with a truth table:

ABCA ∧ (B ∨ C)A ∧ B ∨ A ∧ CA ∧ (B ∨ C) ⟷ A ∧ B ∨ A ∧ C
TTTTTT
TTFTTT
TFTTTT
TFFFFT
FTTFFT
FTFFFT
FFTFFT
FFFFFT

Tautology: true in every row.

Each row is one region of a Venn diagram with three circles: the row where AA and CC are true and BB is false is the region of the elements in AA and CC but not in BB⁠. A tautology says that the two sides agree on every region.

Example 4.3A law that fails

Is A−(B−C)=(A−B)−CA - (B - C) = (A - B) - C⁠? Translate both sides:

ABCA ∧ ¬(B ∧ ¬C)A ∧ ¬B ∧ ¬CA ∧ ¬(B ∧ ¬C) ⟷ A ∧ ¬B ∧ ¬C
TTTTFF
TTFFFT
TFTTFF
TFFTTT
FTTFFT
FTFFFT
FFTFFT
FFFFFT

Satisfiable but not valid: true in 6 rows.

The formulas differ in the two rows where AA and CC are true. Take the one with BB false: an element xx in AA and CC but not in BB⁠. With A={x}A = \{x\}⁠, B=∅B = \emptyset and C={x}C = \{x\}⁠, the left side is {x}−(∅−{x})={x}\{x\} - (\emptyset - \{x\}) = \{x\} and the right side is ({x}−∅)−{x}=∅(\{x\} - \emptyset) - \{x\} = \emptyset⁠. Set difference, like the subtraction of numbers, is not associative.

Sets in Isabelle

Check it in Isabelle

Type the new symbols with their names after a backslash, or with the abbreviations in the table. Difference and complement use the ordinary minus sign -.

Symbol Type Meaning
∈ \in or : is an element of
∉ \notin or ~: is not an element of
⊆ \subseteq or (= is a subset of
∪ \union or Un union
∩ \inter or Int intersection

value computes with finite sets, as in Module 1:

Isabelle
value "{1, 2, 3} ∪ {3, 4 :: nat}"
Output
"{2, 1, 3, 4}"
  :: "nat set"

One annotation, :: nat, fixes the element type of the whole expression. Isabelle lists the elements in whatever order its computation produced them; by extensionality the order does not matter. Set-builder notation works when the elements come from a finite set. {0..10} is the set of numbers from 0 to 10, and mod gives the remainder:

Isabelle
value "{x ∈ {0..10}. x mod 3 = (0::nat)}"
Output
"{0, 3, 6, 9}"
  :: "nat set"

The power set of a set of numbers is a set of sets of numbers, a nat set set:

Isabelle
value "Pow {1, 2, 3 :: nat}"
Output
"{{2, 3}, {2}, {}, {3}, {1, 3}, {1}, {1, 2}, {1, 2, 3}}"
  :: "nat set set"

Mixing up ∈\in and ⊆\subseteq is a type error, as the pitfall above said:

Isabellefails on purpose
value "{1} ∈ {1, 2 :: nat}"
Output
Type unification failed: Clash of types "nat" and "_ set"

Type error in application: incompatible operand type

Operator:  (∈) {1} :: ??'a set set ⇒ bool
Operand:   {1, 2} :: nat set

The operator (∈) {1} (“is {1} an element of …”) needs a set of sets, a ??'a set set; the operand is a set of numbers. Write {1} ⊆ {1, 2} or 1 ∈ {1, 2}, depending on what you mean.

Now prove a law. State it:

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

Isabelle prints A ∩ B ∪ A ∩ C without brackets because ∩\cap binds more tightly than ∪\cup⁠, as ∧\land binds more tightly than ∨\lor⁠. A, B and C are free, so the lemma claims the law for all sets (Module 3). Prove it:

Isabelle
  by auto

auto does what the paper proof did: it applies extensionality, replaces each membership by its definition and finishes with propositional reasoning. blast works as well; on sets these two are the usual tools.

Isabelle
lemma "- (A ∪ B) = - A ∩ - B"
  by auto

lemma "x ∈ {y. P y} ⟷ P x"
  by simp

A false law cannot be proved, but Nitpick (Module 2) finds a counterexample. The option card 'a = 1 makes it search a universe with one element, which is enough, as you saw above:

Isabelle
lemma "A - (B - C) = (A - B) - (C :: 'a set)"
  nitpick [card 'a = 1]
  oops
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 1:
  Free variables:
    A = {a1}
    B = {}
    C = {a1}

The counterexample is the row of the truth table found above: a₁ is in A and C but not in B. Isabelle/jEdit’s automatic Quickcheck may flag the lemma as soon as you type it.

Watch out

The annotation (C :: 'a set) matters. Of the operations above, only - also exists for other types, so without it Isabelle reads the lemma as a claim about subtraction in general. Nitpick then finds a counterexample in which A, B and C are not sets at all, and suspects the problem:

Isabelle
lemma "A - (B - C) = A - B - C"
  nitpick [card 'a = 2]
  oops
Output
Nitpicking formula...

Nitpick found a counterexample for card 'a = 2:
  Free variables:
    A = a1
    B = a1
    C = a1

Hint: Maybe you forgot a type constraint?

∪, ∩, ⊆ and ∈ exist only for sets, so a lemma that uses one of them needs no annotation.

If something went wrong
  • Clash of types "nat" and "bool" in set-builder notation. In Isabelle, | is a way to type ∨\lor⁠, so the book notation {x∣x<5}\{x \mid x < 5\} does not work:

    Isabellefails on purpose
    value "{x | x < (5::nat)}"
    Output
    Type unification failed: Clash of types "nat" and "bool"
    
    Type error in application: incompatible operand type
    
    Operator:  (<) x :: bool ⇒ bool
    Operand:   5 :: nat
    

    Isabelle read the set with the single element x ∨ x < 5, so x would have to be both a truth value and a number. Write {x. x < 5}, with a dot.

  • Wellsortedness error … not of sort enum from value. value cannot compute {x. x < (5::nat)} with this representation: the evaluator has no finite list over which to search. Take the elements from a finite set, as in {x ∈ {0..10}. x < 5}. The issue is executable representation and code equations, rather than a prohibition on reasoning about infinite sets. For example, membership in some infinite sets is computable even though listing every element is not.

2

Relations

A set says which things there are. A relation says how they are connected: which user has which role, which function calls which, which state of a system can follow which.

Pairs and products

An ordered pair (a,b)(a, b) has a first component aa and a second component bb⁠. Unlike a set, a pair has an order: (1,2)≠(2,1)(1, 2) \neq (2, 1)⁠, although {1,2}={2,1}\{1, 2\} = \{2, 1\}⁠. Two pairs are equal when their components are: (a,b)=(c,d)⟷a=c∧b=d(a, b) = (c, d) \iff a = c \land b = d⁠. In Isabelle a pair (a, b) has a type such as nat × string, read “nat times string”.

Definition 4.6Cartesian product

The Cartesian product of AA and BB is the set of all pairs whose first component is in AA and whose second component is in BB⁠:

A×B={(a,b).  a∈A∧b∈B}A \times B = \{(a, b).\; a \in A \land b \in B\}

For example, {1,2}×{x,y}={(1,x),(1,y),(2,x),(2,y)}\{1, 2\} \times \{x, y\} = \{(1, x), (1, y), (2, x), (2, y)\}⁠. A product of finite sets has ∣A∣⋅∣B∣|A| \cdot |B| elements, where ∣A∣|A| is the number of elements of AA⁠. A database table with the columns name and age is a subset of Names×Ages\mathit{Names} \times \mathit{Ages}⁠.

Relations as sets of pairs

Definition 4.7Relation

A (binary) relation from AA to BB is a subset R⊆A×BR \subseteq A \times B⁠; a relation on AA is a subset of A×AA \times A⁠. Instead of (x,y)∈R(x, y) \in R one also writes xRyx \mathrel{R} y⁠, “x is related to y by R”. In Isabelle a relation on a type 'a has the type 'a rel, short for ('a × 'a) set.

A relation is nothing but the set of the pairs that are related. The order << on {1,2,3}\{1, 2, 3\} is the set {(1,2),(1,3),(2,3)}\{(1, 2), (1, 3), (2, 3)\}⁠. Equality on AA is the identity relation IdA={(x,x).  x∈A}\mathit{Id}_A = \{(x, x).\; x \in A\}⁠. “function ff calls function gg” is the call graph of a program, and “the system can move from state ss to state tt in one step” is its transition relation (Section 4.5).

A relation on a small finite set can be drawn as a directed graph, with an arrow from xx to yy for each pair (x,y)(x, y) and a loop at xx for (x,x)(x, x)⁠, or as a matrix, with a mark in row xx and column yy for each pair. The widget below shows both.

Properties of relations

Six properties come up again and again. They use the bounded quantifier ∀x∈A.  P x\forall x \in A.\; P\,x⁠, “for all x in A, P x”, which means ∀x.  x∈A⟶P x\forall x.\; x \in A \imp P\,x⁠.

Definition 4.8Properties of relations

A relation RR on AA is

reflexive on Aif ∀x∈A.  (x,x)∈Rirreflexive on Aif ∀x∈A.  (x,x)∉Rsymmetricif ∀x y.  (x,y)∈R⟶(y,x)∈Rantisymmetricif ∀x y.  (x,y)∈R∧(y,x)∈R⟶x=ytransitiveif ∀x y z.  (x,y)∈R∧(y,z)∈R⟶(x,z)∈Rtotal on Aif ∀x∈A.  ∀y∈A.  x≠y⟶(x,y)∈R∨(y,x)∈R\begin{aligned} &\textbf{reflexive} \text{ on } A && \text{if } \forall x \in A.\; (x, x) \in R\\ &\textbf{irreflexive} \text{ on } A && \text{if } \forall x \in A.\; (x, x) \notin R\\ &\textbf{symmetric} && \text{if } \forall x\, y.\; (x, y) \in R \imp (y, x) \in R\\ &\textbf{antisymmetric} && \text{if } \forall x\, y.\; (x, y) \in R \land (y, x) \in R \imp x = y\\ &\textbf{transitive} && \text{if } \forall x\, y\, z.\; (x, y) \in R \land (y, z) \in R \imp (x, z) \in R\\ &\textbf{total} \text{ on } A && \text{if } \forall x \in A.\; \forall y \in A.\; x \neq y \imp (x, y) \in R \lor (y, x) \in R \end{aligned}

In the graph, each property has a shape you can check by eye:

Property In the graph
Reflexive a loop at every node
Irreflexive no loops at all
Symmetric every arrow has a partner going back
Antisymmetric no pair of arrows going both ways between different nodes
Transitive every path of two arrows x→y→zx \to y \to z has a shortcut x→zx \to z
Total any two different nodes are joined by an arrow, in at least one direction

Some books ask totality also for x=yx = y⁠; this series follows Isabelle and asks it only for different elements, so that << counts as total. To show that a property holds you check every case; to show that it fails, one witness is enough. Each property is a ∀\forall-statement, and by the law ¬(∀x.  P x)⟷(∃x.  ¬P x)\lnot (\forall x.\; P\,x) \iff (\exists x.\; \lnot P\,x) of Module 3 it fails exactly when some instance fails. A witness against transitivity is a triple x,y,zx, y, z with (x,y)(x, y) and (y,z)(y, z) in RR but (x,z)(x, z) not.

Example 4.4Three relations on {1, 2, 3}
  1. ≤\le⁠, that is {(1,1),(2,2),(3,3),(1,2),(1,3),(2,3)}\{(1,1),\allowbreak (2,2),\allowbreak (3,3),\allowbreak (1,2),\allowbreak (1,3),\allowbreak (2,3)\}⁠, is reflexive, antisymmetric, transitive and total. It is not symmetric: (1,2)(1, 2) is in it and (2,1)(2, 1) is not.
  2. “one less than”, {(1,2),(2,3)}\{(1, 2), (2, 3)\}⁠, is irreflexive and antisymmetric. It is not transitive: (1,2)(1, 2) and (2,3)(2, 3) are in it but (1,3)(1, 3) is not. Nor is it total: neither (1,3)(1, 3) nor (3,1)(3, 1) is in it.
  3. “has the same parity as”, {(1,1),(2,2),(3,3),(1,3),(3,1)}\{(1,1), (2,2), (3,3), (1,3), (3,1)\}⁠, is reflexive, symmetric and transitive. It is not antisymmetric: (1,3)(1, 3) and (3,1)(3, 1) are in it, but 1≠31 \neq 3⁠.
Watch out
  • Antisymmetric is not “not symmetric”. Equality is both symmetric and antisymmetric; {(1,2),(2,1),(1,3)}\{(1, 2), (2, 1), (1, 3)\} is neither. Likewise, {(1,1)}\{(1, 1)\} on {1,2}\{1, 2\} is neither reflexive nor irreflexive.
  • Vacuous truth. The empty relation is symmetric, antisymmetric and transitive: these properties are implications whose premises never hold.
  • Transitivity includes the case x=zx = z⁠. {(1,2),(2,1)}\{(1, 2), (2, 1)\} is not transitive: (1,2)(1, 2) and (2,1)(2, 1) demand (1,1)(1, 1)⁠.
Try it

Click the cells of the grid to add or remove pairs; the graph and the properties update as you click, with a witness for every property that fails. Load Cycle: which properties fail, and why? Then add the transitive closure (Section 4.5) and see which pairs it adds. Can you build a relation that is symmetric and antisymmetric but not empty?

Composition and converse

Definition 4.9Converse, composition and image

The converse (or inverse) of RR reverses every pair: R−1={(y,x).  (x,y)∈R}R^{-1} = \{(y, x).\; (x, y) \in R\}⁠. The composition of RR and SS takes an RR-step followed by an SS-step:

ROS={(x,z).  ∃y.  (x,y)∈R∧(y,z)∈S}R \mathbin{O} S = \{(x, z).\; \exists y.\; (x, y) \in R \land (y, z) \in S\}

The image of a set XX under RR is the set of everything related to an element of XX⁠: R[X]={y.  ∃x∈X.  (x,y)∈R}R[X] = \{y.\; \exists x \in X.\; (x, y) \in R\}⁠. Isabelle writes r¯ (type ^-1), r O s, and for the image r `` X, with two backquotes.

The converse of << is >>⁠, and the converse of “is a parent of” is “is a child of”; in the graph, it turns every arrow round. Composing “is a parent of” with itself gives “is a grandparent of”. Books also write composition as R;SR \mathbin{;} S⁠, or as S∘RS \circ R by analogy with functions (Section 4.6), which reverses the order; Isabelle’s ROSR \mathbin{O} S reads from left to right.

Example 4.5Roles and permissions

Many systems assign users roles and assign permissions to those roles; this is role-based access control. With

UR={(alice,admin),(bob,guest)}RP={(admin,read),(admin,write),(guest,read)}\begin{aligned} \mathit{UR} &= \{(\mathit{alice}, \mathit{admin}), (\mathit{bob}, \mathit{guest})\}\\ \mathit{RP} &= \{(\mathit{admin}, \mathit{read}), (\mathit{admin}, \mathit{write}), (\mathit{guest}, \mathit{read})\} \end{aligned}

the permissions of the users are the composition URORP\mathit{UR} \mathbin{O} \mathit{RP}⁠, which relates alice to read and write, and bob to read.

Fact 4.3Properties as equations

For relations RR⁠, SS and TT⁠:

  1. (ROS)OT=RO(SOT)(R \mathbin{O} S) \mathbin{O} T = R \mathbin{O} (S \mathbin{O} T) and (ROS)−1=S−1OR−1(R \mathbin{O} S)^{-1} = S^{-1} \mathbin{O} R^{-1}⁠.
  2. RR is symmetric exactly when R−1=RR^{-1} = R⁠.
  3. RR is transitive exactly when ROR⊆RR \mathbin{O} R \subseteq R⁠.
  4. RR is reflexive on AA exactly when IdA⊆R\mathit{Id}_A \subseteq R⁠.

Take the third. ROR⊆RR \mathbin{O} R \subseteq R says that whenever some yy has (x,y)∈R(x, y) \in R and (y,z)∈R(y, z) \in R⁠, then (x,z)∈R(x, z) \in R⁠. That is transitivity, with the ∃y\exists y moved out of the premise by the law ((∃y.  P y)⟶Q)⟷(∀y.  P y⟶Q)((\exists y.\; P\,y) \imp Q) \iff (\forall y.\; P\,y \imp Q) of Module 3.

Check it in Isabelle

A string in Isabelle is written between two pairs of single quotes, ''alice''; its type prints as char list, a list of characters. Products need × (type \times or <*>):

Isabelle
value "{1, 2 :: nat} × {''x'', ''y''}"
Output
"{(1, ''x''), (1, ''y''), (2, ''x''), (2, ''y'')}"
  :: "(nat × char list) set"

Isabelle names the six properties refl_on A r, irrefl_on A r, sym_on A r, antisym_on A r, trans_on A r and total_on A r, where A is the set the relation is on. Without _on and the set, as in refl r, sym r and trans r, they speak about the whole type. For finite relations, value decides them:

Isabelle
value "refl_on {1, 2, 3} {(1::nat, 1), (2, 2), (1, 2)}"
value "trans_on {1, 2, 3} {(1::nat, 2), (2, 3)}"
value "trans_on {1, 2, 3} {(1::nat, 2), (2, 3), (1, 3)}"
Output
"False"
  :: "bool"

"False"
  :: "bool"

"True"
  :: "bool"

The first relation lacks (3, 3); the second lacks the shortcut (1, 3), which the third has. Composition computes too, and a line break inside the quotes is allowed:

Isabelle
value "{(''alice'', ''admin''), (''bob'', ''guest'')}
     O {(''admin'', ''read''), (''admin'', ''write''), (''guest'', ''read'')}"
Output
"{(''alice'', ''read''), (''alice'', ''write''), (''bob'', ''read'')}"
  :: "(char list × char list) set"

To prove the facts above for all relations, auto must see the definitions of trans and sym: by itself, it does not replace trans r by what it means. The library states the definitions as theorems, trans_def and sym_def, and auto simp: trans_def hands one of them to auto, which then rewrites trans r into the ∀-formula of the definition and proves the rest as before. Module 9 explains this mechanism in depth.

Isabelle
lemma "trans r ⟷ r O r ⊆ r"
  by (auto simp: trans_def)

lemma "sym r ⟷ r¯ = r"
  by (auto simp: sym_def)

lemma "(r O s)¯ = s¯ O r¯"
  by auto
3

Equivalence relations and partitions

Specifications often care about things only up to some detail: two account numbers may belong to the same customer, two inputs may have to be treated alike, two times may show the same position on a clock. “Has the same … as” relations share three properties.

Definition 4.10Equivalence relation and class

An equivalence relation on AA is a relation on AA that is reflexive on AA⁠, symmetric and transitive. The equivalence class of x∈Ax \in A is the set of the elements related to it, the image of {x}\{x\}⁠:

[x]=R[{x}]={y.  (x,y)∈R}[x] = R[\{x\}] = \{y.\; (x, y) \in R\}
Example 4.6Remainders

On {0,1,…,8}\{0, 1, \dots, 8\}⁠, let x∼yx \sim y when x mod 3=y mod 3x \bmod 3 = y \bmod 3⁠: xx and yy leave the same remainder on division by 3. Every number has the same remainder as itself, “the same” does not depend on the order, and two equalities chain, so this is an equivalence relation. Its classes are

[0]={0,3,6}[1]={1,4,7}[2]={2,5,8}[0] = \{0, 3, 6\} \qquad [1] = \{1, 4, 7\} \qquad [2] = \{2, 5, 8\}

A class has many names, [4]=[1]=[7][4] = [1] = [7]⁠: any element of a class can represent it.

The classes do not overlap, and together they cover the set. This is no accident.

Definition 4.11Partition

A partition of AA is a set of non-empty subsets of AA⁠, its blocks, such that every element of AA is in exactly one block: the blocks are pairwise disjoint and their union is AA⁠.

Fact 4.4Equivalence relations and partitions

The equivalence classes of an equivalence relation on AA form a partition of AA⁠. Conversely, every partition of AA defines an equivalence relation, “is in the same block as”, whose classes are the blocks.

Here is the proof of the first half. The classes are subsets of AA⁠, because RR is a relation on AA⁠. Reflexivity puts every xx into its own class, so the classes are non-empty and cover AA⁠. It remains to show that two classes that meet are equal. Suppose zz is in [x][x] and in [y][y]⁠, so (x,z)∈R(x, z) \in R and (y,z)∈R(y, z) \in R⁠. By symmetry (z,y)∈R(z, y) \in R⁠, and by transitivity (x,y)∈R(x, y) \in R⁠. Now for any w∈[y]w \in [y]⁠, transitivity turns (x,y)(x, y) and (y,w)(y, w) into (x,w)(x, w)⁠, so w∈[x]w \in [x]⁠. Hence [y]⊆[x][y] \subseteq [x]⁠; the same argument with xx and yy swapped gives [x]⊆[y][x] \subseteq [y]⁠, and Fact 4.1 gives [x]=[y][x] = [y]⁠. For the second half: every element is in some block (reflexive), the wording is symmetric, and two blocks that share an element are the same block (transitive).

Equivalence relations appear wherever software ignores a detail. Equivalence partitioning, a standard way of designing tests, splits the inputs into classes that the program should treat alike (parcels up to 1 kg, up to 5 kg, heavier) and tests one representative of each, plus the boundaries. And for any function ff⁠, “has the same value under ff” is an equivalence relation. In Module 12, ff is an abstraction function, which maps each data structure to the abstract value it represents: two structures that represent the same value are equivalent in this sense.

Check it in Isabelle

Isabelle writes equiv A r for “r is an equivalence relation on A”, r `` {x} for the class of x, and A // r (“A modulo r”) for the set of all classes, the partition. Here they are for the example, with the relation built by set-builder notation over a product:

Isabelle
value "{(x, y) ∈ {0..8::nat} × {0..8}. x mod 3 = y mod 3} `` {4}"
Output
"{1, 4, 7}"
  :: "nat set"
Isabelle
value "{0..8::nat} // {(x, y) ∈ {0..8} × {0..8}. x mod 3 = y mod 3}"
Output
"{{2, 5, 8}, {1, 4, 7}, {0, 3, 6}}"
  :: "nat set set"

For all natural numbers the relation is an equivalence; the proof unfolds the definitions:

Isabelle
lemma "equiv UNIV {(x, y). x mod 3 = (y::nat) mod 3}"
  by (auto simp: equiv_def refl_on_def sym_def trans_def)

The library proves the main fact of this section once and for all, as quotient_disj: two classes are equal or disjoint. With simp add: you hand a theorem to the simplifier:

Isabelle
lemma "equiv A r ⟶ X ∈ A // r ⟶ Y ∈ A // r ⟶ X = Y ∨ X ∩ Y = {}"
  by (simp add: quotient_disj)
4

Orders and well-foundedness

Orders compare things: version numbers, priorities, deadlines, “must happen before” in a schedule. Some compare any two elements; many do not.

Partial and total orders

Definition 4.12Partial, total and strict orders

A partial order on AA is a relation on AA that is reflexive on AA⁠, antisymmetric and transitive. A total order (or linear order) is a partial order that is also total on AA⁠. A strict order is a relation that is irreflexive and transitive.

Partial orders are usually written ≤\le and strict orders <<⁠, and each gives the other: x<y⟷x≤y∧x≠yx < y \iff x \le y \land x \neq y⁠, and x≤y⟷x<y∨x=yx \le y \iff x < y \lor x = y⁠. A strict order is asymmetric: x<yx < y and y<xy < x would give x<xx < x by transitivity, which irreflexivity forbids.

Example 4.7Orders, total and partial
  • ≤\le on numbers is a total order.
  • ⊆\subseteq on P(A)\pow(A) is a partial order. If AA contains two different elements, it is not total: for example, when 1,2∈A1, 2 \in A⁠, {1}\{1\} and {2}\{2\} are incomparable, as neither is a subset of the other. If AA has at most one element, its subsets do form a total order.
  • Divisibility on the positive integers, a∣ba \mid b when b=k⋅ab = k \cdot a for some integer kk⁠, is a partial order, and 2 and 3 are incomparable.

Hasse diagrams

The graph of a partial order has many arrows: a loop at every node, and a shortcut for every path. A Hasse diagram of a partial order on a finite set leaves out what the properties imply. It puts smaller elements lower, draws no loops and no arrowheads, and draws a line from xx up to yy only when yy covers xx⁠: x<yx < y with nothing strictly between them. Then x≤yx \le y exactly when an upward path leads from xx to yy⁠, allowing a path of zero edges when x=yx = y⁠.

Hasse diagrams of two partial orderssubsets of {1, 2, 3}, ordered by ⊆divisors of 12, ordered by divisibility∅{1}{2}{3}{1, 2}{1, 3}{2, 3}{1, 2, 3}1234612
Figure 4.2

Two Hasse diagrams. Left: the subsets of {1,2,3}\{1, 2, 3\}⁠, ordered by ⊆\subseteq⁠. Right: the divisors of 12, ordered by divisibility. Each line goes up from an element to an element that covers it.

In a partial order, mm is minimal when no element is strictly below it, and least when it is below every element; maximal and greatest are the same notions upside down. In Figure 4.2, ∅\emptyset and 1 are least, {1,2,3}\{1, 2, 3\} and 12 greatest. A partial order can have several minimal elements and no least one: in {2,3,4,6}\{2, 3, 4, 6\} ordered by divisibility, 2 and 3 are minimal, 4 and 6 maximal. In a project plan with no cyclic dependencies, “is a prerequisite of” is a strict order; adding equality gives a partial order. Its minimal tasks have no unfinished prerequisites. Listing tasks in an order that respects the dependencies, a topological sort, gives a valid dependency order; durations and resource constraints are further scheduling questions.

Well-founded relations

On the natural numbers, every descending chain n>m>k>…n > m > k > \dots stops after at most nn steps. On the integers, 0>−1>−2>…0 > -1 > -2 > \dots never stops. This difference is what makes induction and termination proofs work.

Definition 4.13Well-founded relation

Read (y,x)∈R(y, x) \in R as “yy is smaller than xx”. RR is well-founded when there is no infinite descending chain: no infinite sequence x0,x1,x2,…x_0, x_1, x_2, \dots with (xi+1,xi)∈R(x_{i+1}, x_i) \in R for every ii⁠.

Equivalently, a fact this series uses without proof, every non-empty set SS has an RR-minimal element: some m∈Sm \in S such that no y∈Sy \in S has (y,m)∈R(y, m) \in R⁠.

  • << on N\N is well-founded. << on Z\Z is not, nor is << on the non-negative fractions: 1>12>14>…1 > \tfrac{1}{2} > \tfrac{1}{4} > \dots never stops.
  • A well-founded relation is irreflexive, since (x,x)∈R(x, x) \in R gives the chain x,x,x,…x, x, x, \dots⁠. So << on N\N is well-founded and ≤\le is not.
  • A relation on a finite set is well-founded exactly when it has no cycle. (The library proves this as wf_iff_acyclic_if_finite.)
Key idea

Well-founded relations are why programs terminate and why induction is sound.

  • Termination. If each iteration of a loop, or each recursive call, moves to a smaller value in a well-founded relation, the computation stops: an infinite computation would be an infinite descending chain, provided the work between decreases also terminates. Euclid’s algorithm returns mm when n=0n = 0⁠; otherwise it makes the recursive call gcd n (m mod n). Its second argument decreases because m mod n<nm \bmod n < n whenever n>0n > 0⁠. When you define a function with fun, Isabelle looks for such a relation; that is the “termination order” it reported in Module 1.
  • Induction. To prove P xP\,x for all xx⁠, it suffices to prove P xP\,x assuming that PP holds for everything smaller than xx (this proof is the induction step). If PP failed somewhere, the counterexamples would have a minimal element mm⁠; PP holds below mm⁠, so the induction step proves P mP\,m⁠, a contradiction.

Module 5 builds both ideas on this foundation.

Check it in Isabelle

Isabelle writes wf r for “r is well-founded”, with the same reading: (y, x) ∈ r means that y is smaller. The library proves that < on the natural numbers is well-founded, as wf_less:

Isabelle
lemma "wf {(x, y). x < (y::nat)}"
  by (simp add: wf_less)

The command thm displays a theorem of the library by its name. This one says that well-founded means “no infinite descending chain”:

Isabelle
thm wf_iff_no_infinite_down_chain
Output
wf ?r = (∄f. ∀i. (f (Suc i), f i) ∈ ?r)

The chain is a function f on the natural numbers, f 0, f 1, f 2, … , and Suc i is i + 1. ∄ reads “there is no”, and = between two truth values is ⟷\iff (Module 3). The question mark in ?r marks a variable of the theorem, which can be any relation; Module 7 says more. Finally, two incomparable sets show that ⊆\subseteq is not total:

Isabelle
value "{1::nat} ⊆ {2} ∨ {2} ⊆ {1::nat}"
Output
"False"
  :: "bool"
5

Closures and reachability

A relation often lacks a property you need. “calls” is not transitive, but “can call, directly or indirectly” is; “can move in one step” is not, but “can reach” is. A closure adds the pairs that are missing, and nothing else.

Closures

Definition 4.14Closures

The reflexive, symmetric or transitive closure of RR is the smallest relation that contains RR and has the property; “smallest” means that it is contained in every other such relation. For the paper definitions, take R⊆A×AR \subseteq A \times A and keep closures on the chosen carrier AA⁠.

  • Reflexive closure on AA⁠: R∪IdAR \cup \mathit{Id}_A⁠. Symmetric closure: R∪R−1R \cup R^{-1}⁠.
  • Transitive closure: R+=R∪(ROR)∪(ROROR)∪…R^+ = R \cup (R \mathbin{O} R) \cup (R \mathbin{O} R \mathbin{O} R) \cup \dots⁠, so (x,y)∈R+(x, y) \in R^+ when a path of one or more RR-steps leads from xx to yy⁠.
  • Reflexive-transitive closure on AA⁠: R∗=IdA∪R+R^* = \mathit{Id}_A \cup R^+⁠, paths of zero or more steps.

Isabelle writes r⇧+ and r⇧* (type r^+ and r^*), and calls them trancl and rtrancl.

Isabelle’s r⇧* uses the whole element type as its carrier, so it includes (x, x) even for values outside a finite set being drawn. To restrict it to AA⁠, take its intersection with A×AA \times A⁠. For reachability from initial states in AA⁠, a relation contained in A×AA \times A cannot leave AA⁠, so this distinction does not change the reachable states.

Example 4.8A chain and its closure

For R={(1,2),(2,3),(3,4)}R = \{(1, 2), (2, 3), (3, 4)\}⁠, paths of two steps give ROR={(1,3),(2,4)}R \mathbin{O} R = \{(1, 3), (2, 4)\}⁠, paths of three steps give {(1,4)}\{(1, 4)\}⁠, and there are none longer. So

R+={(1,2),(2,3),(3,4),(1,3),(2,4),(1,4)}R^+ = \{(1, 2), (2, 3), (3, 4), (1, 3), (2, 4), (1, 4)\}

which is << on {1,2,3,4}\{1, 2, 3, 4\}⁠; R∗R^* adds the pairs (x,x)(x, x) and gives ≤\le⁠. Figure 4.3, left, draws RR with the pairs that R+R^+ adds.

On a set with nn elements, every reachable pair has a path of at most nn steps. For different endpoints, remove repeated loops to get a path of at most n−1n-1 steps. For equal endpoints in R+R^+⁠, keep a shortest non-empty cycle, which has at most nn steps; a zero-step path would not suffice for R+R^+⁠. So you can compute R+R^+ by adding the pairs of RORR \mathbin{O} R⁠, then of RORORR \mathbin{O} R \mathbin{O} R⁠, and so on, until nothing changes. The Transitive closure button of the widget in Section 4.2 computes the same relation.

Watch out
  • Zero steps count in R∗R^*⁠. (x,x)∈R∗(x, x) \in R^* for every xx⁠, even when RR has no loop at xx⁠; R+R^+ contains (x,x)(x, x) only when a cycle passes through xx⁠.
  • The order of closures matters. The smallest equivalence relation containing RR is (R∪R−1)∗(R \cup R^{-1})^*⁠: symmetric first, then reflexive and transitive. For R={(1,3),(2,3)}R = \{(1, 3), (2, 3)\} it relates 1 and 2, through 1→3→21 \to 3 \to 2⁠, but R∗∪(R∗)−1R^* \cup (R^*)^{-1} does not.

Reachability in a state machine

Module 1 described model checkers, which explore every reachable state of a system. Reachability is the reflexive-transitive closure.

Definition 4.15State machine and reachability

A state machine consists of a set of states, a set II of initial states, and a transition relation TT⁠: (s,t)∈T(s, t) \in T when the system can move from ss to tt in one step. The reachable states are those tt with (s,t)∈T∗(s, t) \in T^* for some s∈Is \in I⁠, that is, the image T∗[I]T^*[I]⁠. A safety property says that no reachable state is bad.

Example 4.9A door with a lock

A door is open or closed, and its bolt locked or unlocked: four states, CL (closed and locked), CU (closed, unlocked), OU (open, unlocked) and OL (open and locked). OL is bad: the bolt sticks out of an open door and will damage the frame. The transitions are unlock (CL to CU), lock (CU to CL), open (CU to OU) and close (OU to CU), and the door starts in CL.

From CL the door can only go to CU, from CU to CL or OU, and from OU back to CU. So the reachable states are CL, CU and OU, and the safety property “the door is never open and locked” holds. Now suppose a designer makes the lock button work in every state. That adds a transition from OU to OL, and OL becomes reachable along CL, CU, OU, OL: a counterexample trace, the kind of answer a model checker gives when a property fails.

A transitive closure, and the reachable states of a door with a lockR and the pairs R⁺ adds1234in Radded by R⁺the door: reachable states in greenCLclosed, lockedCUclosed, unlockedOUopen, unlockedOLopen, locked: badstartunlocklockopencloselock (added)
Figure 4.3

Left: the relation {(1,2),(2,3),(3,4)}\{(1, 2), (2, 3), (3, 4)\} (solid) and the pairs its transitive closure adds (dashed). Right: the door as a state machine. The green states are reachable from CL; the red dashed transition is the one the designer added, which makes the bad state OL reachable.

Real systems have billions of states, or infinitely many, which is why model checkers compute reachability by machine and why Module 13 proves safety properties with invariants instead.

Check it in Isabelle

To give the door’s transition relation a name, use definition. Like fun in Module 1, it introduces a new constant with its type and a defining equation, but it allows no recursion, and a set written out pair by pair needs none (Module 8 says more about both commands):

Isabelle
definition door :: "(string × string) set" where
  "door = {(''CL'', ''CU''), (''CU'', ''CL''), (''CU'', ''OU''), (''OU'', ''CU'')}"
Output
consts
  door :: "(char list × char list) set"

The reachable states are the image of the initial state under the reflexive-transitive closure:

Isabelle
value "door* `` {''CL''}"
Output
"{''OU'', ''CU'', ''CL''}"
  :: "char list set"

The bad state is not reachable, until the extra transition is added:

Isabelle
value "''OL'' ∈ door* `` {''CL''}"
value "''OL'' ∈ (door ∪ {(''OU'', ''OL'')})* `` {''CL''}"
Output
"False"
  :: "bool"

"True"
  :: "bool"

The transitive closure of the chain:

Isabelle
value "{(1::nat, 2), (2, 3), (3, 4)}+"
Output
"{(1, 2), (2, 3), (3, 4), (2, 4), (1, 3), (1, 4)}"
  :: "(nat × nat) set"

And for all relations, R+R^+ contains RR and is transitive, and taking the reflexive-transitive closure twice adds nothing:

Isabelle
lemma "r ⊆ r+"
  by auto

lemma "trans (r+)"
  by simp

lemma "(r*)* = r*"
  by simp
6

Functions

Mathematically, a function is a relation in which every input has exactly one output.

Functions as relations

Definition 4.16Function

A function ff from AA to BB⁠, written f:A→Bf : A \to B⁠, is a relation f⊆A×Bf \subseteq A \times B in which every a∈Aa \in A is related to exactly one b∈Bb \in B⁠, written f a=bf\,a = b⁠. AA is the domain of ff and BB its codomain. The image of a set S⊆AS \subseteq A is f[S]={f a.  a∈S}f[S] = \{f\,a.\; a \in S\}⁠, and the image of the whole domain is the range of ff⁠.

The codomain says where the values may lie, the range where they do lie: for squaring on N\N⁠, the codomain is N\N and the range is {0,1,4,9,… }\{0, 1, 4, 9, \dots\}⁠.

In HOL, functions are not sets of pairs: nat ⇒ nat is a type of its own (Module 1), and its elements are written with λ\lambda⁠. λn. 2 * n is the function that takes n to 2 * n, written n↦2nn \mapsto 2n in mathematics (type \lambda or %). Every HOL function is total: it has a value for every element of its argument type. (This sense of “total” has nothing to do with the total relations of Section 4.2.) A relation in which every input has at most one output is a partial function, such as the input-output relation of a deterministic program that may not return a result. A mathematical partial function can be represented in HOL by a total function returning the type option of Module 8: either a value or None. This representation records undefinedness; it does not make a potentially non-terminating program executable as a terminating HOL function. Isabelle writes the image of a set as f ` A (one backquote) and the range as range f.

Injective, surjective, bijective

Definition 4.17Injective, surjective, bijective

A function f:A→Bf : A \to B is

  • injective (one-to-one) when different inputs give different outputs: ∀x∈A.  ∀y∈A.  f x=f y⟶x=y\forall x \in A.\; \forall y \in A.\; f\,x = f\,y \imp x = y⁠;
  • surjective (onto BB⁠) when every element of BB is an output: ∀b∈B.  ∃a∈A.  f a=b\forall b \in B.\; \exists a \in A.\; f\,a = b⁠, that is, f[A]=Bf[A] = B⁠;
  • bijective when it is both: it pairs the elements of AA and BB one to one.
Example 4.10Four functions on numbers
  • n↦2nn \mapsto 2n on N\N is injective, since 2x=2y2x = 2y implies x=yx = y⁠, but not surjective: 1 is not an output.
  • n↦ndiv⁡2n \mapsto n \operatorname{div} 2 on N\N (division rounding down) is surjective but not injective: 0 and 1 both give 0.
  • n↦n+1n \mapsto n + 1 on N\N is injective but not surjective: 0 is not an output.
  • n↦n+1n \mapsto n + 1 on Z\Z is bijective: every integer mm is the output for exactly one input, m−1m - 1⁠.

The last two have the same formula and different properties: surjectivity depends on the domain and codomain. In software, injectivity means “no collisions”. For example, for a fixed key, deterministic encryption with exact decryption must map different plaintexts to different ciphertexts; otherwise decryption could not recover both messages correctly.

Inverses and composition

Definition 4.18Composition and inverse

The composition of f:A→Bf : A \to B and g:B→Cg : B \to C is g∘f:A→Cg \circ f : A \to C⁠, with (g∘f) x=g (f x)(g \circ f)\,x = g\,(f\,x)⁠: first ff⁠, then gg⁠, read “g after f”. A function g:B→Ag : B \to A is an inverse of ff when g (f a)=ag\,(f\,a) = a for all a∈Aa \in A and f (g b)=bf\,(g\,b) = b for all b∈Bb \in B⁠.

Fact 4.5Composition and inverses
  1. If ff and gg are injective, so is g∘fg \circ f⁠; if they are surjective, so is g∘fg \circ f⁠. So the composition of two bijections is a bijection.
  2. A function has an inverse exactly when it is bijective. The inverse is then unique, written f−1f^{-1}⁠, and its graph is the converse of the graph of ff⁠.
  3. For bijections, (g∘f)−1=f−1∘g−1(g \circ f)^{-1} = f^{-1} \circ g^{-1}⁠.

For injectivity in the first part: if g (f x)=g (f y)g\,(f\,x) = g\,(f\,y)⁠, then f x=f yf\,x = f\,y because gg is injective, and x=yx = y because ff is. The third part is the socks-and-shoes rule: socks go on first and come off last. A message that was compressed and then encrypted is decrypted first.

Check it in Isabelle

Isabelle’s names are inj f, surj f and bij f for functions on whole types, inj_on f A and bij_betw f A B for functions on sets, inv f for the inverse and g ∘ f for composition (type \circ). State that doubling is injective:

Isabelle
lemma "inj (λn::nat. 2 * n)"
Output
proof (prove)
goal (1 subgoal):
 1. inj ((*) 2)

Isabelle prints λn. 2 * n in a shorter form, (*) 2: multiplication with its first argument fixed to 2. As in Section 4.2, the proof needs the definition, here inj_def:

Isabelle
  by (auto simp: inj_def)

More of the examples, and the first part of the fact:

Isabelle
lemma "¬ surj (λn::nat. n + 1)"
  by (auto simp: surj_def)

lemma "bij (λn::int. n + 1)"
  by (auto simp: bij_def inj_def surj_def)

lemma "inj f ⟶ inj g ⟶ inj (g ∘ f)"
  by (auto simp: inj_def)

inv f is defined for every HOL function. Injectivity guarantees that it undoes f on every input:

Isabelle
lemma "inj f ⟶ inv f (f x) = x"
  by simp

On finite sets value decides these properties. Adding 1 and taking the remainder by 3 rotates {0,1,2}\{0, 1, 2\}⁠, a bijection of the set onto itself:

Isabelle
value "bij_betw (λx::nat. (x + 1) mod 3) {0, 1, 2} {0, 1, 2}"
Output
"True"
  :: "bool"
Watch out

When f is not surjective, inv f y still has a value for a y outside the range, because every HOL function is total, but no inverse law is guaranteed there. Injectivity gives the left-inverse law inv f (f x) = x; surjectivity gives the right-inverse law f (inv f y) = y. Bijectivity gives both.

7

Counting: cardinality and Cantor’s theorem

For finite sets you count. For infinite sets you cannot, but you can still compare sizes with functions, and the comparison holds a surprise.

Finite sets

Definition 4.19Cardinality

The cardinality ∣A∣|A| of a finite set AA is its number of elements. Isabelle writes card A, and finite A for “A is finite”.

Fact 4.6Counting rules

For finite sets AA and BB⁠:

  1. ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|⁠, so ∣A∪B∣=∣A∣+∣B∣|A \cup B| = |A| + |B| when AA and BB are disjoint.
  2. ∣A×B∣=∣A∣⋅∣B∣|A \times B| = |A| \cdot |B| and ∣P(A)∣=2∣A∣|\pow(A)| = 2^{|A|}⁠.
  3. If there is an injective function from AA to BB⁠, then ∣A∣≤∣B∣|A| \le |B|⁠; if there is a bijection, then ∣A∣=∣B∣|A| = |B|⁠.

The first rule is inclusion–exclusion: ∣A∣+∣B∣|A| + |B| counts the elements of A∩BA \cap B twice. On the file server, ∣R∪W∣=3+2−1=4|R \cup W| = 3 + 2 - 1 = 4⁠. The third rule compares sizes without counting, by pairing off elements, and that idea still works for infinite sets.

The pigeonhole principle

Fact 4.7The pigeonhole principle

If AA and BB are finite and ∣A∣>∣B∣|A| > |B|⁠, then no function f:A→Bf : A \to B is injective: two different elements of AA have the same image. When BB is non-empty, every function f:A→Bf : A \to B maps at least ⌈∣A∣/∣B∣⌉\lceil |A| / |B| \rceil elements of AA to some one element of BB⁠. If BB is empty and AA is not, there is no function A→BA \to B at all.

If more pigeons than holes fly into the holes, some hole holds two pigeons. Among any 13 people, two were born in the same month. A hash table with 1000 keys and 256 buckets has a bucket with at least ⌈1000/256⌉=4\lceil 1000/256 \rceil = 4 keys, whatever the hash function. And no lossless compression program makes every file shorter: there are 2n2^n files of nn bits but only 20+21+⋯+2n−1=2n−12^0 + 2^1 + \dots + 2^{n-1} = 2^n - 1 shorter ones, so two files would get the same compressed form.

Check it in Isabelle

card counts, and repeated elements count once:

Isabelle
value "card {1, 2, 3, 2, 1 :: nat}"
value "card (Pow {1, 2, 3 :: nat})"
Output
"3"
  :: "nat"

"8"
  :: "nat"

Thirteen numbers have only twelve possible remainders by 12, so the remainder is not injective on them:

Isabelle
value "inj_on (λn::nat. n mod 12) {0..12}"
Output
"False"
  :: "bool"

The library proves the counting rules for all finite sets. The rule for power sets is card_Pow:

Isabelle
lemma "finite A ⟶ card (Pow A) = 2 ^ card A"
  by (simp add: card_Pow)

The pigeonhole principle is pigeonhole. It speaks about the image f ` A of Section 4.6: if the image has fewer elements than A, two elements of A share an image, so f is not injective on A.

Isabelle
lemma "card (f ` A) < card A ⟶ ¬ inj_on f A"
  by (simp add: pigeonhole)
Watch out

In Isabelle, card of an infinite set is 0. That keeps card a total function, but statements about card usually need the assumption finite A, as in the lemma about power sets above.

Isabelle
lemma "card (UNIV :: nat set) = 0"
  by simp

Countable sets

For infinite sets, take the third counting rule as the definition: two sets have the same cardinality when there is a bijection between them. Strange things happen at once: n↦2nn \mapsto 2n is a bijection from N\N to the even numbers, so a proper subset of N\N is as large as N\N itself.

Definition 4.20Countable set

A set AA is countable when there is an injective function from AA to N\N⁠. Equivalently, AA is finite or its elements can be listed as a0,a1,a2,…a_0, a_1, a_2, \dots⁠, each appearing somewhere in the list.

The integers are countable: list them as 0,1,−1,2,−2,…0, 1, -1, 2, -2, \dots⁠. So are the pairs of natural numbers, listed diagonal by diagonal: (0,0)(0, 0)⁠, (0,1)(0, 1)⁠, (1,0)(1, 0)⁠, (0,2)(0, 2)⁠, (1,1)(1, 1)⁠, (2,0)(2, 0)⁠, … . So are the finite strings over a finite alphabet, listed by length and then alphabetically; in particular, the programs of any programming language form a countable set. Is every set countable, then? No, and the proof is one of the most famous in mathematics.

Cantor’s theorem

Fact 4.8Cantor’s theorem

For every set AA⁠, no function f:A→P(A)f : A \to \pow(A) is surjective.

Since a↦{a}a \mapsto \{a\} is injective, P(A)\pow(A) is at least as large as AA⁠; Cantor’s theorem says it is strictly larger. Georg Cantor published the proof, the diagonal argument, in 1891.

Take any f:A→P(A)f : A \to \pow(A)⁠. Each f xf\,x is a set that may or may not contain xx itself. Collect the elements that are not in their own set:

D={x∈A.  x∉f x}D = \{x \in A.\; x \notin f\,x\}

D⊆AD \subseteq A⁠, so D∈P(A)D \in \pow(A)⁠. Suppose ff were surjective. Then D=f dD = f\,d for some d∈Ad \in A⁠. Is dd in DD⁠? The first step below is the definition of DD (it applies because d∈Ad \in A⁠); the second replaces f df\,d by DD⁠, which is equal to it:

d∈D⟷d∉f d⟷d∉Dd \in D \iff d \notin f\,d \iff d \notin D

So d∈D⟷d∉Dd \in D \iff d \notin D⁠, and a proposition equivalent to its own negation is impossible:

PP ⟷ ¬P
TF
FF

Contradiction: false in every row.

The assumption that ff is surjective has led to a contradiction, so it is false: no dd has f d=Df\,d = D⁠, and ff is not surjective. Figure 4.4 shows where the name comes from.

Cantor's diagonal argumentIs n in the set?01234…nf 0even numbers10101…f 1the empty set00000…f 2all of ℕ11111…f 3the primes00110…f 4{1, 3}01010…⋮DD = {n. n ∉ f n}flip the diagonal01001…D differs fromevery row: fromrow i in column i.So D is not f i,for any i.
Figure 4.4

The diagonal argument for A=NA = \N⁠. Row ii shows which numbers are in the set f if\,i (1 for “in”, 0 for “not in”). DD flips the diagonal, so it differs from the set f if\,i at position ii⁠, for every ii⁠: DD is in no row.

A subset of N\N describes a decision problem, such as “is this number prime?”. A program decides such a set only if it halts with the correct yes-or-no answer on every input. There are countably many programs, so at most countably many decidable sets, while P(N)\pow(\N) is uncountable. Thus undecidable problems must exist. This is a counting argument: it does not give an algorithm that lists exactly the programs which halt on every input. Turing’s proof that the halting problem is undecidable (Module 1) is a diagonal argument too.

Good to know

Bertrand Russell found in 1901 that the same trick breaks naive set theory, which lets every property define a set: then R={x.  x∉x}R = \{x.\; x \notin x\}⁠, the set of all sets that are not elements of themselves, would be a set, and R∈R⟷R∉RR \in R \iff R \notin R⁠. HOL avoids the paradox with types: x ∈ x is a type error, because a set and its elements have different types, and for the same reason there is no set of everything (Section 4.1).

Check it in Isabelle

The heart of the proof is that the diagonal set is not a value of f. range f is the set of all values of f:

Isabelle
lemma diagonal: "{x. x ∉ f x} ∉ range f"
  by auto

Cantor’s theorem for every type follows: a surjective f would have every set in its range, the diagonal set included. auto does not find this proof, but force does. force is a relative of auto that searches harder, and more slowly, and either proves the goal completely or fails; Module 9 introduces it properly. Here it even finds the diagonal set by itself:

Isabelle
lemma cantor: "¬ surj (f :: 'a ⇒ 'a set)"
  by force

So no sequence f 0, f 1, f 2, … of sets of natural numbers contains them all; the name cantor lets a later proof use the lemma:

Isabelle
lemma "¬ surj (f :: nat ⇒ nat set)"
  by (simp add: cantor)

The library has the theorem for every set A, as Cantors_theorem:

Isabelle
thm Cantors_theorem
Output
∄f. f ` ?A = Pow ?A
8

What you learned

  • A set is determined by its elements (extensionality), and a∈{x.  P x}⟷P aa \in \{x.\; P\,x\} \iff P\,a⁠. In HOL every set has an element type.
  • Union, intersection, difference and complement are ∨\lor⁠, ∧\land⁠, “and not” and ¬\lnot applied to membership, so set laws are propositional laws, which Isabelle proves with auto.
  • A relation is a set of pairs. It can be reflexive, irreflexive, symmetric, antisymmetric, transitive or total; a property fails exactly when a witness exists. Converse and composition build new relations.
  • Equivalence relations correspond to partitions. Partial orders are drawn as Hasse diagrams. Well-founded relations have no infinite descending chains, which is why programs terminate and induction works.
  • The closures R+R^+ and R∗R^* (r⇧+, r⇧*) add the pairs joined by paths, and the reachable states of a state machine are the image of its initial states under T∗T^*⁠.
  • A function can be injective, surjective, both (bijective), or neither. It has a two-sided inverse exactly when it is bijective. Isabelle’s inv is defined more generally; inverse laws require the appropriate assumptions (inj, surj, bij).
  • Cardinality counts finite sets (card, finite). The pigeonhole principle forbids injections into smaller sets, and Cantor’s diagonal argument shows that no set can be mapped onto its power set: some sets are uncountable, and some problems have no program.
9

Exercises

Exercise 4.1★Sets by hand

Let A={1,2,3,4}A = \{1, 2, 3, 4\}⁠, B={3,4,5}B = \{3, 4, 5\} and C={1,5}C = \{1, 5\}⁠.

  1. Compute A−BA - B⁠, B−AB - A⁠, A∩B∩CA \cap B \cap C⁠, C×{0,1}C \times \{0, 1\} and P(C)\pow(C)⁠, and check your answers with value.
  2. Which of these are true: C⊆A∪BC \subseteq A \cup B⁠, ∅∈P(C)\emptyset \in \pow(C)⁠, {5}∈P(C)\{5\} \in \pow(C)⁠?
Show solution
  1. A−B={1,2}A - B = \{1, 2\}⁠, B−A={5}B - A = \{5\}⁠, A∩B∩C=∅A \cap B \cap C = \emptyset⁠, C×{0,1}={(1,0),(1,1),(5,0),(5,1)}C \times \{0, 1\} = \{(1, 0), (1, 1), (5, 0), (5, 1)\} and P(C)={∅,{1},{5},{1,5}}\pow(C) = \{\emptyset, \{1\}, \{5\}, \{1, 5\}\}⁠. In Isabelle:
Isabelle
value "{1, 2, 3, 4} - {3, 4, 5 :: nat}"
value "{3, 4, 5} - {1, 2, 3, 4 :: nat}"
value "{1, 2, 3, 4} ∩ {3, 4, 5} ∩ {1, 5 :: nat}"
value "{1, 5 :: nat} × {0, 1 :: nat}"
value "Pow {1, 5 :: nat}"
Output
"{1, 2}"
  :: "nat set"

"{5}"
  :: "nat set"

"{}"
  :: "nat set"

"{(1, 0), (1, 1), (5, 0), (5, 1)}"
  :: "(nat × nat) set"

"{{5}, {}, {1}, {1, 5}}"
  :: "nat set set"
  1. All three are true. C⊆A∪B={1,2,3,4,5}C \subseteq A \cup B = \{1, 2, 3, 4, 5\}⁠, and ∅\emptyset and {5}\{5\} are subsets of CC⁠, so they are elements of P(C)\pow(C)⁠.
Exercise 4.2★Properties of relations

Which of the six properties of Section 4.2 do these relations on {1,2,3,4}\{1, 2, 3, 4\} have? For every property that fails, give a witness, and check your answers with the widget.

  1. R1={(1,1),(2,2),(3,3),(4,4),(1,2),(2,1)}R_1 = \{(1, 1),\allowbreak (2, 2),\allowbreak (3, 3),\allowbreak (4, 4),\allowbreak (1, 2),\allowbreak (2, 1)\}
  2. R2={(1,2),(2,3),(3,1)}R_2 = \{(1, 2), (2, 3), (3, 1)\}
  3. R3={(1,2),(1,3),(1,4)}R_3 = \{(1, 2), (1, 3), (1, 4)\}
Show solution
  1. R1R_1 is reflexive, symmetric and transitive: an equivalence relation with the classes {1,2}\{1, 2\}⁠, {3}\{3\} and {4}\{4\}⁠. It is not irreflexive (witness (1,1)(1, 1)⁠), not antisymmetric ((1,2)(1, 2) and (2,1)(2, 1)⁠, but 1≠21 \neq 2⁠) and not total (neither (1,3)(1, 3) nor (3,1)(3, 1)⁠).
  2. R2R_2⁠, a cycle, is irreflexive and antisymmetric. It is not reflexive (no (1,1)(1, 1)⁠), not symmetric ((1,2)(1, 2) without (2,1)(2, 1)⁠), not transitive ((1,2)(1, 2) and (2,3)(2, 3) without (1,3)(1, 3)⁠) and not total (neither (1,4)(1, 4) nor (4,1)(4, 1)⁠).
  3. R3R_3 is irreflexive, antisymmetric and transitive; transitivity holds vacuously, as no path has two steps. It is not reflexive (no (1,1)(1, 1)⁠), not symmetric ((1,2)(1, 2) without (2,1)(2, 1)⁠) and not total (neither (2,3)(2, 3) nor (3,2)(3, 2)⁠). It is a strict order.
Exercise 4.3★★A set law, two ways
  1. Prove A−(B∪C)=(A−B)∩(A−C)A - (B \cup C) = (A - B) \cap (A - C) on paper by translating it into propositional logic, and then in Isabelle.
  2. Someone claims (A∪B)−C=A∪(B−C)(A \cup B) - C = A \cup (B - C)⁠. Find a counterexample with the method of Section 4.1, then let Nitpick find one, and compare.
Show solution
  1. x∈A−(B∪C)x \in A - (B \cup C) translates to pA∧¬(pB∨pC)p_A \land \lnot (p_B \lor p_C)⁠, which De Morgan’s law turns into pA∧¬pB∧¬pCp_A \land \lnot p_B \land \lnot p_C⁠. The right side translates to (pA∧¬pB)∧(pA∧¬pC)(p_A \land \lnot p_B) \land (p_A \land \lnot p_C)⁠, the same formula, because p∧p⟷pp \land p \iff p⁠. In Isabelle, no type annotation is needed, since ∪ and ∩ exist only for sets:

    Isabelle
    lemma "A - (B ∪ C) = (A - B) ∩ (A - C)"
      by auto
  2. (pA∨pB)∧¬pC(p_A \lor p_B) \land \lnot p_C and pA∨(pB∧¬pC)p_A \lor (p_B \land \lnot p_C) differ exactly when pAp_A and pCp_C are true: then the left side is false and the right side true. With A=C={x}A = C = \{x\} and B=∅B = \emptyset⁠, the left side is ∅\emptyset and the right side {x}\{x\}⁠. Nitpick:

    Isabelle
    lemma "(A ∪ B) - C = A ∪ (B - C)"
      nitpick [card 'a = 1]
      oops
    Output
    Nitpicking formula...
    
    Nitpick found a counterexample for card 'a = 1:
      Free variables:
        A = {a1}
        B = {}
        C = {a1}

    It is the same counterexample, with a₁ for xx⁠.

Exercise 4.4★★Who calls whom

A program has the functions main, parse, lex, eval and apply. main calls parse and eval, parse calls lex, eval calls apply, and apply calls eval.

  1. Which functions can main call, directly or indirectly?
  2. A function is recursive when it can call itself, directly or indirectly. Express this with the transitive closure, and find the recursive functions.
  3. Check both answers in Isabelle.
Show solution
  1. parse, lex, eval and apply: the image of {main}\{\mathit{main}\} under calls+\mathit{calls}^+⁠.
  2. ff is recursive when (f,f)∈calls+(f, f) \in \mathit{calls}^+⁠. eval and apply are, through the cycle eval, apply, eval; main, parse and lex are not.
  3. With a definition for the call graph:
Isabelle
definition calls :: "(string × string) set" where
  "calls = {(''main'', ''parse''), (''main'', ''eval''), (''parse'', ''lex''),
            (''eval'', ''apply''), (''apply'', ''eval'')}"

value "calls+ `` {''main''}"
value "(''apply'', ''apply'') ∈ calls+"
value "(''main'', ''main'') ∈ calls+"
Output
consts
  calls :: "(char list × char list) set"

"{''parse'', ''lex'', ''apply'', ''eval''}"
  :: "char list set"

"True"
  :: "bool"

"False"
  :: "bool"
Exercise 4.5★★Injective or surjective?

Classify each function as injective, surjective, both or neither, and give the inverse of each bijection. Check the fourth with value.

  1. f:N→Nf : \N \to \N⁠, f n=n+3f\,n = n + 3
  2. g:Z→Zg : \Z \to \Z⁠, g n=−ng\,n = -n
  3. h:N→Nh : \N \to \N⁠, h n=ndiv⁡2h\,n = n \operatorname{div} 2
  4. k:{0,1,2,3}→{0,1,2,3}k : \{0, 1, 2, 3\} \to \{0, 1, 2, 3\}⁠, k n=3n mod 4k\,n = 3n \bmod 4
Show solution
  1. Injective, not surjective: 0, 1 and 2 are not outputs.
  2. Bijective, and its own inverse: g (g n)=ng\,(g\,n) = n⁠.
  3. Surjective, since m=h (2m)m = h\,(2m)⁠; not injective, since h 0=h 1=0h\,0 = h\,1 = 0⁠.
  4. Bijective: kk maps 0, 1, 2, 3 to 0, 3, 2, 1. It is its own inverse, because 9n mod 4=n mod 49n \bmod 4 = n \bmod 4⁠.
Isabelle
value "bij_betw (λn::nat. 3 * n mod 4) {0, 1, 2, 3} {0, 1, 2, 3}"
Output
"True"
  :: "bool"
Exercise 4.6★★★Combining equivalence relations

Let RR and SS be equivalence relations on AA⁠.

  1. Prove that R∩SR \cap S is an equivalence relation on AA⁠, on paper and in Isabelle.
  2. Show with an example on {1,2,3}\{1, 2, 3\} that R∪SR \cup S need not be one, and check it with value.
  3. What is the smallest equivalence relation on AA that contains R∪SR \cup S⁠?
Show solution
  1. Reflexive: for x∈Ax \in A⁠, (x,x)(x, x) is in RR and in SS⁠. Symmetric: if (x,y)(x, y) is in both, so is (y,x)(y, x)⁠, because both are symmetric. Transitive: if (x,y)(x, y) and (y,z)(y, z) are in both, so is (x,z)(x, z)⁠, because both are transitive. In Isabelle, sym_Int and trans_Int are the library’s versions of the last two steps:

    Isabelle
    lemma "equiv A R ⟶ equiv A S ⟶ equiv A (R ∩ S)"
      by (auto simp: equiv_def refl_on_def sym_Int trans_Int)
  2. Let RR relate 1 and 2, and SS relate 2 and 3, each with all pairs (x,x)(x, x)⁠. Then R∪SR \cup S contains (1,2)(1, 2) and (2,3)(2, 3) but not (1,3)(1, 3)⁠:

    Isabelle
    value "trans_on {1, 2, 3}
             ({(1::nat, 1), (2, 2), (3, 3), (1, 2), (2, 1)} ∪ {(1, 1), (2, 2), (3, 3), (2, 3), (3, 2)})"
    Output
    "False"
      :: "bool"
  3. (R∪S)+(R \cup S)^+⁠. It is reflexive on AA because it contains RR⁠, symmetric because a path read backwards is again a path of R∪SR \cup S⁠, and transitive. An equivalence relation that contains R∪SR \cup S is transitive, so it contains every path, that is, all of (R∪S)+(R \cup S)^+⁠.

Exercise 4.7★★★Infinite bit sequences

An infinite bit sequence is a function from N\N to {0,1}\{0, 1\}⁠, in HOL a function of type nat ⇒ bool. Adapt Cantor’s diagonal argument to show that no list f 0,f 1,f 2,…f\,0, f\,1, f\,2, \dots of such sequences contains them all: give the missing sequence and explain why it is missing. Then prove in Isabelle that it is not in the range of f. (Hint: two functions are equal when they agree on every argument; the library calls this fun_eq_iff.)

Show solution

Let dd be the sequence whose nn-th bit is the opposite of the nn-th bit of f nf\,n⁠: d n=¬(f n n)d\,n = \lnot (f\,n\,n)⁠. If d=f md = f\,m for some mm⁠, the two functions agree at mm⁠: ¬(f m m)=f m m\lnot (f\,m\,m) = f\,m\,m⁠, which is impossible. So dd is not in the list, and the set of infinite bit sequences is uncountable. This is Figure 4.4 again: a set of natural numbers and the bit sequence “1 at nn when nn is in the set” describe the same thing. (A similar argument shows that the real numbers are uncountable.)

Isabelle
lemma "(λn. ¬ f n n) ∉ range (f :: nat ⇒ nat ⇒ bool)"
  by (auto simp: fun_eq_iff)

fun_eq_iff turns the equation between functions into one about all arguments, and auto then finds the argument mm on its own.

10

Check yourself

1
Which statement holds for every set AA⁠?
2
Which law of logic proves −(A∩B)=−A∪−B-(A \cap B) = -A \cup -B⁠?
3
A relation RR on {1,2,3}\{1, 2, 3\} is both symmetric and antisymmetric. What follows?
4
RR is an equivalence relation, and the classes [x][x] and [y][y] have an element in common. What can you conclude?
5
TT is the transition relation of a state machine and ss its initial state. Which set contains exactly the states reachable from ss⁠?
6
What does Cantor’s theorem say about a function ff from N\N to P(N)\pow(\N)⁠?
Finished this module?