# Logic for Computer Science/First-Order Logic

## First-Order Logic

In propositional logic, we considered formulas made about atomic objects, which could only be either true or false. First-order logic, the topic of this chapter, builds upon propositional logic and allows you to look inside the objects discussed in formulas. We can provide this more refined level of granularity by discussing objects as elements of sets that can be larger than just the set ${\displaystyle \{0,1\}}$, and also include arbitrarily complex relationships with each other.

We begin first by defining the syntax of first-order (FO) logic, and then give these structures meaning.

### Syntax

The domain of discourse for first order logic is first-order structures or models. A first-order structure contains

1. Relations,
2. Functions, and
3. Constants (functions of arity 0).

The vocabulary of first-order logic is

1. a set of relation symbols with associated arities, and
2. a set of function symbols with associated arities.

Here are some example first-order logic vocabularies:

1. A graph
• Relation Set = { ${\displaystyle V}$ : unary, ${\displaystyle E}$ : binary }
• Function Set = { ${\displaystyle {\mathit {next}}}$ : unary }
2. Arithmetic
• Relation Set = { ${\displaystyle +}$ : ternary, ${\displaystyle \times }$ : ternary, ${\displaystyle \uparrow }$ : ternary, ${\displaystyle =}$ : binary, ${\displaystyle <}$ : binary}
• Function Set = { ${\displaystyle {\mathit {next}}}$ : unary, ${\displaystyle 0}$ : const }

Here, a graph is a set of vertices ${\displaystyle V}$ and a set of edges ${\displaystyle E}$. For the arithmetic set note that the use of ${\displaystyle +}$ and ${\displaystyle \times }$ is purely syntactic and we could have used the symbols "plus" and "times" instead. We haven't provided the symbols with any meaning yet.

A term denotes an element in the first-order structure. A term is used to refer to the elements in our domain of discourse. Here are the rules that describe what a term is:

• every variable is a term, where a variable is simply another set of symbols
• every constant is a term
• If ${\displaystyle t_{1},t_{2},\ldots ,t_{k}}$ are terms, and ${\displaystyle f}$ is a ${\displaystyle k}$-ary function then ${\displaystyle f(t_{1},t_{2},\ldots ,t_{k})}$ is a term.

For example, if ${\displaystyle f}$ is a binary function and ${\displaystyle g}$ is a ternary function then the following are all terms: ${\displaystyle x,a,f(x,y),g(x,f(x,y),a)}$.

An atomic formula is

${\displaystyle R(t_{1},t_{2},\ldots ,t_{k})}$,

where ${\displaystyle R}$ is a ${\displaystyle k}$-ary relation and ${\displaystyle t_{1},t_{2},\ldots ,t_{k}}$ are terms. When viewing ${\displaystyle R}$ as a set, then this is just another way of saying that the tuple

${\displaystyle (t_{1},t_{2},\ldots ,t_{k})\in R}$.

A special relation ${\displaystyle =}$ means "equal" and cannot be interpreted otherwise. For example, ${\displaystyle t_{1}=t_{2}}$ stands for

${\displaystyle =(t_{1},t_{2})}$.

A first-order formula is an expression built using a given first-order vocabulary and variables and the symbols ${\displaystyle (,),\neg ,\vee ,\wedge ,\rightarrow ,\exists ,\forall }$. The set of first-order formulas is the minimum set satisfying the following:

• atomic formulas are first order formulas
• If ${\displaystyle \phi }$ and ${\displaystyle \psi }$ are formulas and ${\displaystyle x}$ is a variable, then the following are also formulas:
1. ${\displaystyle (\phi \vee \psi )}$
2. ${\displaystyle (\phi \wedge \psi )}$
3. ${\displaystyle (\neg \phi )}$
4. ${\displaystyle (\phi \rightarrow \psi )}$
5. ${\displaystyle (\exists x\phi )}$
6. ${\displaystyle (\forall x\phi )}$

### Semantics

A first-order structure over a given vocabulary consists of

1. A domain, which is a set of elements (also known as a universe)
2. A mapping associating to every ${\displaystyle k}$-ary relation symbol in the vocabulary a ${\displaystyle k}$-ary relation over the domain, and to every ${\displaystyle k}$-ary function symbol a ${\displaystyle k}$-ary function over the domain.

These components give meaning to the symbols.

Example:

Relation Set = { ${\displaystyle +}$ : ternary, ${\displaystyle \times }$ : ternary, ${\displaystyle \uparrow }$ : ternary, ${\displaystyle =}$ : binary, ${\displaystyle <}$ : binary }
Function Set = { ${\displaystyle next}$ : unary, ${\displaystyle 0}$ : const }

A first-order structure over this vocabulary is:

1. Domain: the set of integers
2. Mapping : ${\displaystyle +\rightarrow }$ addition, ${\displaystyle \times \rightarrow }$ multiplication, ${\displaystyle \uparrow \rightarrow }$ exponentiation, ${\displaystyle <\rightarrow }$ ordering, ${\displaystyle {\mathit {next}}\rightarrow i+1}$

In this structure, the formula

${\displaystyle \exists x\forall y\forall z[\times (y,z,x)\rightarrow ((y=1)\vee (z=1))]}$

expresses the statement "there exists a prime number" (the number 1 also satisfies this statement).

Note here that ${\displaystyle \times (y,z,x)}$ is equivalent to ${\displaystyle (x=y\times z)}$.

#### Scope of Quantifiers

In the formula ${\displaystyle (\forall x\phi )}$ or ${\displaystyle (\exists x\phi )}$, ${\displaystyle \phi }$ is referred to as the scope of quantification of the variable ${\displaystyle x}$. An occurrence of a variable in a formula is bound if it is in the scope of quantification of that variable, otherwise the occurrence is said to be free. You can consider a quantifier use as a variable declaration.

A sentence is a formula with no free variables (i.e., all variables are bound). A sentence is either true or false.

A formula with free variable(s) can be considered as describing the properties of the free variable(s). For example, ${\displaystyle \phi (x)}$ denotes a formula with ${\displaystyle x}$ occurring free and describes the properties of ${\displaystyle x}$.

If a sentence ${\displaystyle \phi }$ evaluates to true over a structure ${\displaystyle I}$, we say ${\displaystyle I}$ satisfies ${\displaystyle \phi }$ and denote this by ${\displaystyle I\models \phi }$.

• ${\displaystyle I\models R(t_{1},t_{2},\ldots ,t_{k})}$ iff ${\displaystyle (t_{1},t_{2},\ldots ,t_{k})\in I(R)}$, where ${\displaystyle I(R)}$ is the interpretation of ${\displaystyle R}$ by ${\displaystyle I}$.
• ${\displaystyle I\models \phi \wedge \psi }$ iff ${\displaystyle I\models \phi }$ and ${\displaystyle I\models \psi }$. (Similar for ${\displaystyle \vee }$.)
• ${\displaystyle I\models \neg \varphi }$ iff ${\displaystyle I\not \models \varphi }$.
• ${\displaystyle I\models \exists x\phi (x)}$ if ${\displaystyle I\models \phi (x\leftarrow c)}$ for some ${\displaystyle c}$ in the domain.
• ${\displaystyle I\models \forall x\phi (x)}$ if ${\displaystyle I\models \phi (x\leftarrow c)}$ for every ${\displaystyle c}$ in the domain.

Note: ${\displaystyle \phi (x\leftarrow c)}$ denotes the result of substituting ${\displaystyle c}$ for every free occurrence of ${\displaystyle x}$ in ${\displaystyle \phi }$. Substitution is covered further later in this chapter.

### Axiomatic approach

The axioms are a set of sentences meant to distinguish "desired" models from others. But typically, also have "undesired" models, which are called non-standard models.

Examples: Consider the vocabulary ${\displaystyle ({\mathit {next}},+,\times ,\uparrow ,0,=,<)}$, where the symbols have the usual meaning (defined by FO sentences over this vocabulary). The standard interpretation for this set of axioms (see the end of this chapter) is the integers, but these are also satisfied by the set of integers modulo ${\displaystyle p}$. This possibility is ruled out by adding the following sentence to the axioms: ${\displaystyle \forall x(x<{\mathit {next}}(x))}$

Question: Can all non-standard models be axiomatized away? The answer is no. Consider the model shown in [TODO: import figure] for the vocabulary containing only ${\displaystyle 0,<,\sigma }$ (all elements in the upper line are larger than those on the lower line).

There is no set of first-order sentences that can distinguish this model from the natural numbers. Intuitively, the reason is that we cannot "backtrack" arbitrarily (towards ${\displaystyle 0}$) in a first-order sentence. A similar non-standard model can be obtained for the full vocabulary above.

### Evaluating FO Sentences

Given a first-order structure ${\displaystyle I}$ and a FO sentence ${\displaystyle \phi }$, can we tell if ${\displaystyle I\models \phi }$?

This problem is decidable for finite structures. For example, suppose ${\displaystyle E}$ is a binary relation representing the edges of a graph, and the given sentence is

${\displaystyle \exists x_{1}\exists x_{2}\exists x_{3}(E(x_{1},x_{2})\wedge E(x_{2},x_{3})\wedge E(x_{3},x_{1})\wedge x_{1}\neq x_{2}\wedge x_{2}\neq x_{3}\wedge x_{3}\neq x_{1})}$.

We can evaluate the truth of this sentence by trying all possible values for ${\displaystyle x_{1},x_{2}}$ and ${\displaystyle x_{3}}$. (Naive evaluation: "nested loop".) This is polynomial time (in domain size) but exponential in the size of the formula.

On infinite domains, we may or may not be able to evaluate the truth of a sentence. With the vocabulary ${\displaystyle (N,0,\sigma ,<,+)}$, where ${\displaystyle N}$ is the set of natural numbers, it is decidable if a sentence is true. (This is known as Presburger arithmetic.) However if we include multiplication, the truth of a sentence becomes undecidable.

### A Deductive System for FO

A set ${\displaystyle \Delta }$ of FO sentences is

• Satisfiable if there is some structure ${\displaystyle I}$ such that ${\displaystyle I\models \Delta }$
• Unsatisfiable if it is not satisfiable
• Valid if ${\displaystyle I\models \Delta }$ for all structure ${\displaystyle I}$

Given a set of FO sentences ${\displaystyle \Sigma }$ and a sentence ${\displaystyle \phi }$

• ${\displaystyle \Sigma }$ entails ${\displaystyle \phi }$ (denoted ${\displaystyle \Sigma \models \phi }$), if every structure that satisfies ${\displaystyle \Sigma }$ also satisfies ${\displaystyle \phi }$.
• ${\displaystyle \Sigma \models \phi }$ iff ${\displaystyle \Sigma \cup \{\neg \phi \}}$ is unsatisfiable.
• If ${\displaystyle \Sigma }$ is finite then, ${\displaystyle \Sigma \models \phi }$ iff ${\displaystyle \neg \Sigma \lor \phi }$ is valid.
• If a formula ${\displaystyle \phi }$ has free variables ${\displaystyle {\vec {X}}=(x_{1},\ldots ,x_{k})}$, ${\displaystyle \phi }$ is valid iff ${\displaystyle \forall {\vec {X}}\phi }$ is valid.

#### Axioms for Validity

The axioms for validity are from three categories:

1. Axioms for boolean validity. These are inherited from propositional logic.
2. Axioms for equality.
3. Axioms for quantification.
##### Boolean Validity

Given a FO formula ${\displaystyle \varphi }$, a boolean form of ${\displaystyle \varphi }$ a propositional formula ${\displaystyle \psi }$ such that ${\displaystyle \varphi }$ is obtained from ${\displaystyle \psi }$ by replacing each propositional variable in ${\displaystyle \psi }$ by a subformula of ${\displaystyle \varphi }$.

The set of Boolean forms of ${\displaystyle \varphi }$ is denoted ${\displaystyle BF(\varphi )}$.

Examples:

• For the FO formula ${\displaystyle \forall xP(x)\lor \neg \forall xP(x)}$, the boolean form is ${\displaystyle p\lor \neg p}$.
• If ${\displaystyle \varphi \equiv \forall xG(x,y)\land \exists xG(x,y)\land (G(z,x)\lor \forall xG(x,y))}$, then ${\displaystyle BF(\varphi )\equiv x_{1}\land x_{3}\land (x_{2}\lor x_{1})}$ where ${\displaystyle x_{1}=\forall xG(x,y),x_{2}=G(z,x),x_{3}=\exists xG(x,y)}$.

Claim: If ${\displaystyle \psi \in BF(\varphi )}$ and ${\displaystyle \psi }$ is valid, then ${\displaystyle \varphi }$ is valid.

##### Equality Axioms

Let ${\displaystyle t,t_{1},\ldots ,t_{k},t'_{1},\ldots ,t'_{k}}$ be terms. The following are valid formulas.

• ${\displaystyle t=t}$
• ${\displaystyle (t_{1}=t'_{1}\land \cdots \land t_{k}=t'_{k})\rightarrow (f(t_{1},\ldots ,t_{k})=f(t'_{1},\ldots ,t'_{k}))}$, where ${\displaystyle f}$ is a ${\displaystyle k}$-ary function.
• ${\displaystyle (t_{1}=t'_{1}\land \cdots \land t_{k}=t'_{k})\rightarrow (R(t_{1},\ldots ,t_{k})\rightarrow R(t'_{1},\ldots ,t'_{k}))}$, where ${\displaystyle R}$ is a ${\displaystyle k}$-ary relation.
##### Substitutions

Given a formula ${\displaystyle \varphi }$ in which variable ${\displaystyle x}$ occurs free (denoted by ${\displaystyle \varphi (x)}$) and a term ${\displaystyle t}$, we define the substitution of ${\displaystyle t}$ for ${\displaystyle x}$ in ${\displaystyle \varphi }$, denoted ${\displaystyle \varphi (x\leftarrow t)}$, as the formula that results from replacing every free occurrence of ${\displaystyle x}$ in ${\displaystyle \varphi }$ by ${\displaystyle t}$, subject to the constraint that ${\displaystyle t}$ contains no variable ${\displaystyle y}$ quantified in ${\displaystyle \varphi }$ such that ${\displaystyle x}$ occurs free within the scope of quantification of ${\displaystyle y}$. If ${\displaystyle x}$ does not occur free ${\displaystyle \varphi }$, then ${\displaystyle \varphi (x\leftarrow t)}$ is defined as ${\displaystyle \varphi }$.

Example: Let ${\displaystyle \varphi }$ be

${\displaystyle ((x=1)\rightarrow \exists x(x=y))}$,

then

• ${\displaystyle \varphi (x\leftarrow (y+1))}$ is a valid substitution
• ${\displaystyle \varphi (y\leftarrow (x+1))}$ is not a valid substitution.
##### Quantification Axioms
1. ${\displaystyle \forall x\varphi \rightarrow \varphi (x\leftarrow t)}$, where ${\displaystyle t}$ is a term and ${\displaystyle \varphi (x\leftarrow t)}$ is a valid substitution
2. ${\displaystyle (\forall x(\varphi \rightarrow \psi ))\rightarrow ((\forall x\varphi )\rightarrow (\forall x\psi ))}$
3. ${\displaystyle \varphi \rightarrow \forall x\varphi }$
4. ${\displaystyle \exists x\varphi \leftrightarrow \neg \forall x\neg \varphi }$

In summary, the axioms for validity are ${\displaystyle {\mathbf {Ax} }}$:

1. Axioms for boolean validity. These are inherited from propositional logic.
2. Axioms for equality.
3. Axioms for quantification.

Definition: A proof is a sequence ${\displaystyle \phi _{1},\phi _{2},\ldots ,\phi _{k}}$ of FO sentences such that for each ${\displaystyle i\in \{1,\ldots ,k\}}$ either ${\displaystyle \phi _{i}\in {\mathbf {Ax} }}$ or ${\displaystyle \exists j,k such that ${\displaystyle \phi _{j}\equiv \psi }$ and ${\displaystyle \phi _{k}\equiv (\psi \rightarrow \phi _{i})}$.

Notation: If there is a proof of ${\displaystyle \phi }$ using ${\displaystyle {\mathbf {Ax} }}$, we denote this fact by ${\displaystyle \vdash _{\mathbf {Ax} }\phi }$.

Fact (Soundness): If ${\displaystyle \vdash _{\mathbf {Ax} }\phi }$, then ${\displaystyle \phi }$ is valid.

Remark: The set of formulas which can be proven valid is recursively enumerable.

Example: Proof of the formula ${\displaystyle \forall x(\phi \land \psi )\rightarrow (\forall x\phi \land \forall x\psi )}$

${\displaystyle \forall x(\phi \land \psi )}$
${\displaystyle \forall x(\phi \land \psi )\rightarrow (\phi \land \psi )(x\leftarrow x)}$
${\displaystyle \phi \land \psi }$
${\displaystyle \phi }$
${\displaystyle \forall x(\phi \land \psi )\rightarrow \phi }$
${\displaystyle \forall x(\forall x(\phi \land \psi )\rightarrow \phi )}$
${\displaystyle \forall x\forall x(\phi \land \psi )\rightarrow \forall x\phi }$
${\displaystyle \forall x(\phi \land \psi )\rightarrow \forall x\phi }$
${\displaystyle \forall x\phi }$

The proof of ${\displaystyle \forall x\psi }$ is symmetrical.

${\displaystyle \forall x\phi \land \forall x\psi }$
${\displaystyle \forall x(\phi \land \psi )\rightarrow (\forall x\phi \land \forall x\psi )}$

#### Useful Equivalences

• ${\displaystyle \forall x(\phi \land \psi )\leftrightarrow (\forall x\phi )\land (\forall x\psi )}$
• ${\displaystyle \forall x(\phi \lor \psi )\not \leftrightarrow (\forall x\phi )\lor (\forall x\psi )}$ e.g. ${\displaystyle \phi (x)\approx }$ "x is even" and ${\displaystyle \psi (x)\approx }$ "x is odd"
• ${\displaystyle \forall x(\phi (x)\lor \neg \phi (x))\not \leftrightarrow (\forall x\phi (x)\lor \forall x\neg \phi (x))}$
• ${\displaystyle \exists x(\phi \lor \psi )\leftrightarrow (\exists x\phi )\lor (\exists x\psi )}$
• ${\displaystyle \neg \exists x\phi \leftrightarrow \forall x\neg \phi }$
• ${\displaystyle \neg \forall x\phi \leftrightarrow \exists x\neg \phi }$

#### Prenex Normal Form

Claim: Every FO sentence is equivalent to a FO sentence of the form ${\displaystyle Q_{1}x_{1}\cdots Q_{k}x_{k}\phi }$, where ${\displaystyle Q_{i}\in \{\exists ,\forall \}}$ and ${\displaystyle \phi }$ is quantifier free.

This is proved using the distributive properties of quantifiers. It may be necessary to rename variables, to avoid ambiguities. Although you can always move all quantifiers to the left, the resulting formula can actually be exponentially larger than the original one.

Example:

${\displaystyle \phi \equiv \forall x(G(x,x)\land (\forall yG(x,y)\lor \exists y\neg G(y,y)))\land G(x,0)}$
${\displaystyle \equiv (\forall x(G(x,x)\land (\forall yG(x,y)\lor \exists y\neg G(y,y))))\land G(w,0)}$
${\displaystyle \equiv \forall x(G(x,x)\land (\forall yG(x,y)\lor \exists y\neg G(y,y))\land G(w,0))}$
${\displaystyle \equiv \forall x(G(x,x)\land (\forall yG(x,y)\lor \exists z\neg G(z,z))\land G(w,0))}$
${\displaystyle \equiv \forall x(G(x,x)\land \forall y(G(x,y)\lor \exists z\neg G(z,z))\land G(w,0))}$
${\displaystyle \equiv \forall x\forall y(G(x,x)\land (G(x,y)\lor \exists z\neg G(z,z))\land G(w,0))}$
${\displaystyle \equiv \forall x\forall y(G(x,x)\land \exists z(G(x,y)\lor \neg G(z,z))\land G(w,0))}$
${\displaystyle \equiv \forall x\forall y\exists z(G(x,x)\land (G(x,y)\lor \neg G(z,z))\land G(w,0))}$

#### Recall the Axiomatic Method

1. Describe the desired model as closely as possible using a (possibly infinite but recursive) set ${\displaystyle \Delta }$ of axioms (FO sentences).
1. Prove things using deduction.

Notation: We say ${\displaystyle \phi }$ is a valid consequence of ${\displaystyle \Delta }$ (denoted ${\displaystyle \Delta \models \phi }$), if every model satisfying ${\displaystyle \Delta }$ also satisfies ${\displaystyle \phi }$.

Fact: ${\displaystyle \Sigma \models \phi }$ iff ${\displaystyle \Sigma \cup \{\neg \phi \}}$ is unsatisfiable.

### Aside: Nonlogical axioms for number theory

The following fourteen first-order axioms describe the properties of arithmetic and numbers, i.e. addition (${\displaystyle +}$), multiplication (${\displaystyle \times }$), exponentiation (${\displaystyle \uparrow }$, equality (${\displaystyle =}$), ordering (${\displaystyle <}$), successor function (${\displaystyle \sigma }$) and remainder (mod). This example shows the expressive power of first-order statements, which were originally hoped to provide a basis for the "one true mathematics."

Question: Why are they called "nonlogical" axioms?

NT1: ${\displaystyle \forall x(\sigma (x)\neq 0)}$
NT2: ${\displaystyle \forall x\forall y(\sigma (x)=\sigma (y)\rightarrow x=y)}$
NT3: ${\displaystyle \forall x(x=0\vee \exists y\sigma (y)=x)}$
NT4: ${\displaystyle \forall x(x+0=x)}$
NT5: ${\displaystyle \forall x\forall y(x+\sigma (y)=\sigma (x+y))}$
NT6: ${\displaystyle \forall x(x\times 0=0)}$
NT7: ${\displaystyle \forall x\forall y(x\times \sigma (y)=(x\times y)+x)}$
NT8: ${\displaystyle \forall x(x\uparrow 0=\sigma (0))}$
NT9: ${\displaystyle \forall x\forall y(x\uparrow \sigma (y)=(x\uparrow y)\times x)}$
NT10: ${\displaystyle \forall x(x<\sigma (x))}$
NT11: ${\displaystyle \forall x\forall y(x
NT12: ${\displaystyle \forall x\forall y(\neg (x
NT13: ${\displaystyle \forall x\forall y\forall z(((x
NT14: ${\displaystyle \forall x\forall y\forall z\forall z'(mod(x,y,z)\wedge mod(x,y,z')\rightarrow z=z')}$