Lecture 6 · First-Order Logic: Syntax

Introduction to Automated Reasoning · Lecture 06。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

Maps to: Stanford CS 257 Introduction to Automated Reasoning (Winter 2026), Lecture 6 · Thu 01/22/2026 · First-order logic: syntax (Hanna Lachnitt). Reading: Bradley & Manna, The Calculus of Computation (CC) §2.1; Enderton, A Mathematical Introduction to Logic (MI) §2.1. Basis: The Stanford slides are on Canvas and not public. This lecture follows the syntax half of the public Iowa deck First-order Logic: Syntax and Semantics (CS:4980, Cesare Tinelli, Spring 2024), plus the substitution notation from the deck Proof systems for First-order Logic. Both decks say they are adapted from slides by Clark Barrett, Caroline Trippel and Andrew (Haoze) Wu (Stanford). “The slides” below means those decks. Reused from them:

  • the motivating example;
  • many-sorted signatures with ranks;
  • terms as generated sets, and their well-formedness and well-sortedness examples;
  • the sort system Γ⊢t:σ\Gamma \vdash t : \sigma;
  • formula-building operations and notational conventions;
  • free variables;
  • the substitution examples;
  • the English-to-FOL examples.

These notes add:

  • a machine check of every example, which finds one inconsistent slide and two typos;
  • exact counts for the slides’ Venn-diagram exercise;
  • variable capture, capture-avoiding substitution and α-equivalence, with their laws tested on random formulas;
  • relativization (one sort is enough);
  • a catalogue of translation slips.

The experiments and their code were designed for these notes and do not reflect what was said in class. Code: code/lecture06/ (4 scripts + 1 module; Python standard library only; each runs in about a second) Reading time: 2–3 hours including running the code


0. How to read this lecture

0.1 Learning goals

After this lecture you should be able to:

  1. Explain what first-order logic (FOL) can say that propositional logic cannot.
  2. Write down a signature: sorts, function symbols with ranks, constants and predicates as special cases.
  3. Decide whether an expression is a well-formed term, and whether it is well-sorted, by building a derivation in the sort system.
  4. Build formulas with connectives and quantifiers, and read them under the notational conventions.
  5. Compute free variables, recognise sentences, and rename bound variables apart.
  6. Apply a substitution without capturing variables, and explain why naive substitution is unsound.
  7. Translate English sentences into FOL and spot the classic slips.

0.2 Prerequisites and scope

AssumedIntroduced hereDeveloped later
propositional syntax, structural induction (Lecture 1)signatures, terms, sorts, quantifierssemantics: structures and satisfaction (Lecture 7)
substitution in PL (Lecture 1, §6)free/bound variables, capture-avoiding substitutionunification: solving equations between terms (Lecture 7)
proof systems as rules (Lecture 3)the sort system as a proof systemresolution with variables (Lecture 8); theories (Lecture 8, 10 on)

0.3 Map

§1  why first-order logic                                                       ← code 01
§2  signatures                                                                   ← code 01
§3  terms: an inductive definition; which strings are terms?                    ← code 01
§4  the sort system Γ ⊢ t : σ                                                    ← code 01
§5  formulas, and the conventions for reading them                              ← code 02
§6  free and bound variables                                                     ← code 02
§7  substitution and variable capture                                           ← code 03
§8  one sort is enough                                                           ← code 02
§9  from English to FOL                                                          ← code 04
§10 summary   §11 common pitfalls   §12 exercises   §13 further reading   §14 next lecture

0.4 Notation

SymbolMeaning
Σ=⟨ΣS,ΣF⟩\Sigma = \langle \Sigma^S, \Sigma^F \ranglea signature: sorts and function symbols
rank(f)=⟨σ1,…,σn,σ⟩\mathrm{rank}(f) = \langle \sigma_1, \dots, \sigma_n, \sigma \rangleff takes nn arguments of sorts σi\sigma_i and returns sort σ\sigma
≐σ\doteq_\sigma (in code: =)equality on sort σ\sigma, present in every signature
Γ={x1:σ1,… }\Gamma = \{x_1 : \sigma_1, \dots\}a sort context: sorts for the free variables
Γ⊢t:σ\Gamma \vdash t : \sigma“in context Γ\Gamma, term tt has sort σ\sigma”
FV(α)\mathrm{FV}(\alpha)the free variables of α\alpha
ε[v←t]\varepsilon[v \leftarrow t]ε\varepsilon with the free occurrences of vv replaced by tt (capture-avoiding, §7)
α≡αβ\alpha \equiv_\alpha \betaα\alpha and β\beta differ only in the names of bound variables

0.5 The code of this lecture

One module, fol.py, the syntax layer of every first-order lecture that follows. It has:

  • Signature, with the slides’ example signatures NAT, SETS, ELEM_SET, PERS;
  • one immutable class Expr for terms and formulas;
  • parse_prefix_term, which decides well-formedness in the slides’ prefix notation;
  • sort_derivation, which prints derivations Γ⊢t:σ\Gamma \vdash t : \sigma;
  • a readable parser and printer, parse and to_str;
  • variable functions: free_vars, occurrences, rename_apart, alpha_equal;
  • substitution: substitute (capture-avoiding), substitute_naive and captures;
  • relativize, lint, and random formulas for tests.

1. Why first-order logic

The slides start from two sentences:

Englishpropositional logicfirst-order logic (sort Nat)
Every natural number is greater than 0pp∀x:Nat. 0<x\forall x{:}\mathrm{Nat}.\ 0 < x
1 is a natural number not equal to 0¬q\neg q1≠01 \neq 0
1 is greater than 0rr (a new, unrelated atom)0<10 < 1

In propositional logic each sentence is an opaque atom. The only consequences of p∧¬qp \wedge \neg q are propositional ones, like pp, ¬q\neg q and p∨rp \vee r. The conclusion “1 is greater than 0” cannot even be stated in terms of pp and qq.

First-order logic opens the atoms:

  • terms denote objects: 00, 11, xx, S(x)+yS(x) + y;
  • predicates state properties of objects and relations between them: x<yx < y;
  • quantifiers speak about all objects of a sort (∀\forall) or about some object (∃\exists).

Now the conclusion is an instance of the first premise: (0<x)[x←1]=(0<1)(0 < x)[x \leftarrow 1] = (0 < 1) (script 01). The deduction is one substitution, the operation of §7.

(The first premise is false in the natural numbers, for x=0x = 0. Logic does not care: it derives what follows from the premises.)

What FOL is for.

  • It is the language of mathematics: every theorem of algebra and analysis can be written in it.
  • It is the language of specifications. “Every request is eventually answered” and “the array is sorted” are both first-order statements.
  • It is the input language of SMT solvers (Lectures 10–18), whose formulas are first-order formulas over fixed theories.

The price comes in Lectures 7 and 8. Validity of propositional formulas is decidable, by truth tables. Validity of first-order formulas is only semi-decidable: no algorithm can decide it for all formulas. This lecture covers only syntax, which is still entirely mechanical.


2. Signatures

A first-order language is not one fixed language: it is defined relative to a signature Σ=⟨ΣS,ΣF⟩\Sigma = \langle \Sigma^S, \Sigma^F \rangle.

  • ΣS\Sigma^S is a set of sorts, the “types” of objects: Nat, Int, Set, Pers.
  • ΣF\Sigma^F is a set of function symbols. Each has an arity nn and a rank ⟨σ1,…,σn,σ⟩\langle \sigma_1, \dots, \sigma_n, \sigma \rangle: ff takes nn arguments of sorts σ1..σn\sigma_1..\sigma_n and returns a value of sort σ\sigma.

Every signature also contains the sort Bool, the constants ⊤ and ⊥, and equality ≐σ\doteq_\sigma with rank ⟨σ,σ,Bool⟩\langle \sigma, \sigma, \mathrm{Bool} \rangle for every sort σ\sigma. Two kinds of function symbols get special names:

  • constants are symbols of arity 0: 0, ∅, John;
  • predicates are symbols with result sort Bool: <, ∈, ≐\doteq.

Script 01 prints the slides’ example signatures:

  elementary number theory:  Σ^S = {Bool, Nat}
      rank(0) = ⟨Nat⟩
      rank(S) = ⟨Nat, Nat⟩
      rank(+) = ⟨Nat, Nat, Nat⟩
      rank(*) = ⟨Nat, Nat, Nat⟩
      rank(<) = ⟨Nat, Nat, Bool⟩

Signatures for set theory, sets with elements, and people (pa, ma, mar) follow the same pattern.

Propositional logic is the special case with the single sort Bool. Each propositional variable is a Bool constant, and the connectives are Bool functions.

What a signature does not say. It fixes the vocabulary, nothing more: which symbols exist and how they combine. Whether < means “less than”, “equals” or “divides” is decided by an interpretation (Lecture 7). Keeping the two apart is the central idea of logic: syntax is mechanical, meaning is assigned separately.

Why sorts? Many textbooks (Enderton, and the slides’ ML reference) use one-sorted FOL. The slides use many sorts because SMT does: a formula about integers, arrays of integers and bit-vectors mixes several sorts, and sorts reject nonsense like “the successor of a truth value” before any reasoning starts. §8 shows that sorts add no expressive power.


3. Terms: an inductive definition

The slides define terms exactly like propositional formulas in Lecture 1, as a generated set:

  • Base set BB: all variables and all constant symbols.
  • Operations: for each function symbol ff of arity n>0n > 0, Tf(ε1,…,εn):=(f ε1⋯εn)T_f(\varepsilon_1, \dots, \varepsilon_n) := (f\ \varepsilon_1 \cdots \varepsilon_n).
  • Terms are the expressions generated from BB by these operations.

In the official prefix notation a term is therefore either a symbol of BB, or ”(”, then a function symbol of arity n>0n > 0, then exactly nn terms, then ”)”. parse_prefix_term implements this definition and reports why a string fails. Script 01 runs the slides’ examples:

expressionslidesoursreason
(+ x2 (S 0))✓✓
(S (S (S (S 0))))✓✓
(S (0 0))✗✗‘0’ is a constant (arity 0): it cannot be applied
(x2 + 0)✗✗‘x2’ is a variable, not a function symbol: it cannot be applied
(S 0 0)✗✗‘S’ has arity 1 but is applied to 2 argument(s)
(S (< 0 0))✓✓
(+ x2 ⊥)✓✓
(S ⊥)✓✓
(= 0 ⊥)✓✓

Well-formedness knows arities, not sorts. (S (< 0 0)) is well-formed: S is applied to one term. That the term is a truth value is a different question (§4). (x2 + 0) fails because the official syntax is prefix. Infix notation like x2+0x_2 + 0 is a notational convention layered on top, which the readable parser of fol.py accepts.

How rare are terms? The slides’ exercise asks for a Venn diagram of terms, well-formed terms, well-sorted terms and atomic formulas. Script 01 answers it with numbers. It uses strings over the 8 tokens ( ) S 0 + < x b, where xx : Nat and bb : Bool are variables. The counts come from a dynamic program over the inductive definition, checked against brute force over all 8n8^n strings for n≤6n \le 6:

tokensexpressionswell-formed termswell-sorted termsatomic formulas (Bool)well-sorted / well-formed
18331100.0%
53.3⋅1043.3 \cdot 10^4188444.4%
91.3⋅1081.3 \cdot 10^8216321614.8%
135.5⋅10115.5 \cdot 10^{11}3,243162805.0%
162.8⋅10142.8 \cdot 10^{14}22,6831,0424804.6%
  • At 16 tokens, only one string in 1.2⋅10101.2 \cdot 10^{10} is a well-formed term. Syntax is a tiny, highly structured corner of all symbol sequences.
  • Among well-formed terms, the share that is well-sorted falls with length, because every extra application is another chance to put an argument of the wrong sort. The exceptions are lengths 4, 7 and 10, which contain only the chains (S … (S t)) and stay at 2/3.

4. The sort system

Which well-formed terms are meaningful? The slides answer with a proof system over judgements Γ⊢t:σ\Gamma \vdash t : \sigma, “in context Γ\Gamma, tt has sort σ\sigma”. The context Γ\Gamma gives sorts to the free variables.

x:σ∈ΓΓ⊢x:σ \textscVarrank(c)=⟨σ⟩Γ⊢c:σ \textscConstrank(f)=⟨σ1,…,σn,σ⟩Γ⊢t1:σ1 ⋯ Γ⊢tn:σnΓ⊢(f t1⋯tn):σ \textscFun\frac{x : \sigma \in \Gamma}{\Gamma \vdash x : \sigma}\ \textsc{Var} \qquad \frac{\mathrm{rank}(c) = \langle \sigma \rangle}{\Gamma \vdash c : \sigma}\ \textsc{Const} \qquad \frac{\mathrm{rank}(f) = \langle \sigma_1, \dots, \sigma_n, \sigma \rangle \quad \Gamma \vdash t_1 : \sigma_1 \ \cdots\ \Gamma \vdash t_n : \sigma_n}{\Gamma \vdash (f\ t_1 \cdots t_n) : \sigma}\ \textsc{Fun}

A term is well-sorted if such a judgement is derivable. This is the type checking of programming languages: one rule per kind of term, applied from the leaves up. sort_derivation prints the derivation, or the premise that fails:

  (+ (+ 0 x1) x2)   (slides: ✗; derivation: NOT well-sorted)
      Γ ⊢ 0 + x1 + x2 : Nat                        by FUN, rank(+) = ⟨Nat, Nat, Nat⟩
        Γ ⊢ 0 + x1 : Nat                           by FUN, rank(+) = ⟨Nat, Nat, Nat⟩
          Γ ⊢ 0 : Nat                              by CONST
          Γ ⊢ x1 : Bool                            by VAR (x1 : Bool ∈ Γ)
        ✗ argument x1 of + has sort Bool, but rank(+) requires Nat

An inconsistent slide. The slides ask which of five terms are well-sorted in Γ={x1:Bool,x2:Nat,x3:Nat}\Gamma = \{x_1 : \mathrm{Bool}, x_2 : \mathrm{Nat}, x_3 : \mathrm{Nat}\}. Their answers are ✓ ✗ ✓ ✓ ✓. Script 01 derives each:

#termslidesderivation in Γ\Gamma
1(+ 0 x2)✓✓ sort Nat
2(+ (+ 0 x1) x2)✗✗ x1x_1 : Bool is an argument of +
3(S (+ 0 x5))✓✗ x5x_5 has no sort in Γ\Gamma
4(< (S x3) (+ (S 0) x1))✓✗ x1x_1 : Bool is an argument of +
5(≐ (S x3) (+ (S 0) x1))✓✗ x1x_1 : Bool is an argument of +

Items 4 and 5 add x1x_1 to a number, and that is precisely why item 2 is rejected. Script 01 tries every context for x1x_1 and x5x_5 (Bool, Nat or absent). No context reproduces the slides’ answers:

  • with x1x_1 : Nat, items 2, 4 and 5 are all well-sorted;
  • with x1x_1 : Bool, none of them is.

The answers ✓ for 3–5 presumably assume x1,x5x_1, x_5 : Nat, and the ✗ for 2 assumes x1x_1 : Bool.

Why a proof system? A sort derivation is a certificate: a short, mechanically checkable reason why a term is meaningful, exactly in the sense of Lecture 3. Type checkers in compilers, proof assistants (Lecture 15) and SMT-LIB parsers all work this way.


5. Formulas

Atomic formulas are Σ-terms of sort Bool (under some context). Examples are (≐Nat 0 (S 0))(\doteq_{\mathrm{Nat}}\ 0\ (S\ 0)) and (< (S x3) (+ (S 0) x1))(<\ (S\ x_3)\ (+\ (S\ 0)\ x_1)).

Formulas are generated from the atomic formulas by these operations:

  • the connectives ¬,∧,∨,⇒,⇔\neg, \wedge, \vee, \Rightarrow, \Leftrightarrow, as in propositional logic;
  • for each variable xx and sort σ\sigma, the quantifiers ∃x:σ. α\exists x{:}\sigma.\ \alpha and ∀x:σ. α\forall x{:}\sigma.\ \alpha.

Well-sorted formulas extend the sort system with four rules:

  • BCONST: ⊤ and ⊥ have sort Bool;
  • NOT and CONN: the arguments of a connective must be of sort Bool;
  • QUANT: the body is checked in the context extended by the quantified variable, Γ[x:σ]\Gamma[x : \sigma].
Γ[x:σ]⊢α:Boolσ∈ΣSQ∈{∀,∃}Γ⊢(Q x:σ. α):Bool \textscQuant\frac{\Gamma[x : \sigma] \vdash \alpha : \mathrm{Bool} \qquad \sigma \in \Sigma^S \qquad Q \in \{\forall, \exists\}}{\Gamma \vdash (Q\, x{:}\sigma.\ \alpha) : \mathrm{Bool}}\ \textsc{Quant}

The slides’ six examples (script 02):

expressionwell-formed (slides / ours)well-sorted (all xix_i : Nat)why
(≐Nat (+ x1 0) x2)✓ / ✓✓
(≐Nat (+ x1 0) x2) ⇒ ⊥✓ / ✓✓
(+ 0 x3) ∧ (< 0 (S 0))✗ / ✗–(+ 0 x3) has sort Nat: not an atom
∀x3:Nat. (+ (+ 0 x3) x2)✗ / ✗–the body has sort Nat
∀x3:Bool. (≐Nat (+ 0 x3) x2)✓ / ✓✗QUANT checks the body with x3x_3 : Bool
¬∃x0:Nat. (< 0 x0 (S 0))✗ / ✗–< applied to 3 arguments: not a term

The fifth line separates the two notions. Its atom is Bool under the context x3x_3 : Nat, so the formula is well-formed. But the quantifier declares x3x_3 : Bool, so it is not well-sorted.

Notational conventions. As in the slides:

  • infix notation for ≐,<,+\doteq, <, +;
  • sorts omitted when clear;
  • ∧ ∨ ⇒ ⇔ group to the right;
  • ¬ and the quantifiers bind most tightly. So ¬∀x. p(x)∧q(x)\neg\forall x.\ p(x) \wedge q(x) means (¬(∀x. p(x)))∧q(x)(\neg(\forall x.\ p(x))) \wedge q(x), and the xx in q(x)q(x) is free.

To quantify over a conjunction, write ∀x. (p(x)∧q(x))\forall x.\ (p(x) \wedge q(x)). Some textbooks use the opposite convention (“the scope extends as far right as possible”); when in doubt, parenthesize. Script 02 prints the parse trees, and fol.parse and to_str follow these conventions exactly.


6. Free and bound variables

The slides define the free variables by induction:

FV(α)={all variables of αα atomicFV(β)α=¬βFV(β)∪FV(γ)α=β⋈γFV(β)∖{v}α=Q v:σ. β\mathrm{FV}(\alpha) = \begin{cases} \text{all variables of } \alpha & \alpha \text{ atomic} \\ \mathrm{FV}(\beta) & \alpha = \neg\beta \\ \mathrm{FV}(\beta) \cup \mathrm{FV}(\gamma) & \alpha = \beta \bowtie \gamma \\ \mathrm{FV}(\beta) \setminus \{v\} & \alpha = Q\, v{:}\sigma.\ \beta \end{cases}

In Q v. βQ\, v.\ \beta, vv is bound, and β\beta is the scope of the quantifier. A formula without free variables is closed: a sentence. Script 02 applies the definition subformula by subformula:

  subformula                                      FV
  x < S(0) + y                                    {x, y}
  x = z                                           {x, z}
  ∃z:Nat. x = z                                   {x}
  x < S(0) + y ∧ ∃z:Nat. x = z                    {x, y}
  ∀x:Nat. (x < S(0) + y ∧ ∃z:Nat. x = z)          {y}
  x = z                                           {x, z}
  ∀x:Nat. (x < S(0) + y ∧ ∃z:Nat. x = z) ∨ x = z  {x, y, z}

One name, two variables. In x<x⇒∀x:Nat. 0<xx < x \Rightarrow \forall x{:}\mathrm{Nat}.\ 0 < x, the first two xx are free and the last is bound by ∀x\forall x. They are different variables that happen to share a name.

Renaming apart makes this visible: x<x⇒∀x′. 0<x′x < x \Rightarrow \forall x'.\ 0 < x'. The slides recommend it (“we typically rename the bound variables … so that they are distinct from its free variables”), and it is the first step of the prenex transformation in Lecture 7.

A renamed formula is α-equivalent to the original: equal up to the names of bound variables. Nothing about its meaning changes (Lecture 7 proves this).

Why care? Free variables are the “inputs” of a formula. Lecture 7 shows that the truth of a formula depends only on the interpretation of its symbols and of its free variables. A sentence is true or false in an interpretation, with no further information needed.


7. Substitution and variable capture

ε[v←t]\varepsilon[v \leftarrow t] replaces every free occurrence of vv in ε\varepsilon by the term tt. The slides’ examples, all reproduced by script 03:

  • (x+y)[x←y]=y+y(x + y)[x \leftarrow y] = y + y;
  • (x≐x)[x←S(x)]=S(x)≐S(x)(x \doteq x)[x \leftarrow S(x)] = S(x) \doteq S(x);
  • (x≐y∨∀x. x<y)[x←S(y)]=S(y)≐y∨∀x. x<y(x \doteq y \vee \forall x.\ x < y)[x \leftarrow S(y)] = S(y) \doteq y \vee \forall x.\ x < y. The xx under ∀x\forall x is bound, so it stays.

The trap. The obvious recursive definition, “replace free occurrences everywhere”, is wrong under quantifiers:

formulasubstitutionnaive resultcapture-free result
∃y. x<y\exists y.\ x < yx←yx \leftarrow y∃y. y<y\exists y.\ y < y∃y′. y<y′\exists y'.\ y < y'
∀y. ∃z. x<y+z\forall y.\ \exists z.\ x < y + zx←y+zx \leftarrow y + z∀y. ∃z. y+z<y+z\forall y.\ \exists z.\ y + z < y + z∀y′. ∃z′. y+z<y′+z′\forall y'.\ \exists z'.\ y + z < y' + z'
∃y. (x<y∧y<z)\exists y.\ (x < y \wedge y < z)x←z, z←yx \leftarrow z,\ z \leftarrow y∃y. (z<y∧y<y)\exists y.\ (z < y \wedge y < y)∃y′. (z<y′∧y′<y)\exists y'.\ (z < y' \wedge y' < y)

In the first row, the substituted yy is a free variable: “the number yy”. Naively it lands under ∃y\exists y and is captured, becoming the bound variable. The meaning changes from ”yy has a larger number” to “some number is smaller than itself”.

Why this matters for proofs. The slides’ quantifier rule for semantic arguments is

I⊨∀v. αI⊨α[v←t] (m)for any term t.\frac{I \models \forall v.\ \alpha}{I \models \alpha[v \leftarrow t]}\ (m) \quad \text{for any term } t.

Apply it to ∀x. ∃y. x<y\forall x.\ \exists y.\ x < y, which is true in the natural numbers, with t=yt = y:

  • naive substitution yields ∃y. y<y\exists y.\ y < y, which is false;
  • capture-avoiding substitution yields ∃y′. y<y′\exists y'.\ y < y', which is true.

A prover with naive substitution is unsound: it can derive falsehoods from truths.

The fix: capture-avoiding substitution. Entering Q y. βQ\, y.\ \beta with a substitution whose terms contain yy free (and whose variable is free in β\beta), first rename yy to a fresh y′y' throughout β\beta, then continue. The renaming changes nothing by itself (α-equivalence), and afterwards no capture is possible.

fol.substitute implements this; substitute_naive is the broken version, kept for comparison.

The laws, tested (script 03; 3,000 random formula/term/variable triples over number theory):

propertyresult
naive substitution would capture a variable770 (25.7%)
FV(α[v←t])=(FV(α)∖{v})∪FV(t)\mathrm{FV}(\alpha[v \leftarrow t]) = (\mathrm{FV}(\alpha) \setminus \{v\}) \cup \mathrm{FV}(t) when v∈FV(α)v \in \mathrm{FV}(\alpha): capture-free1,795 / 1,795
the same law for naive substitution1,423 / 1,795
… failures without a capture0
without capture, naive and capture-free results are identical2,230 / 2,230
α-equivalent inputs give α-equivalent results3,000 / 3,000

What the table shows:

  • Capture is common: a quarter of random substitutions are at risk.
  • Naive substitution breaks the free-variable law only by capturing. Not every capture shows up in FV, though: of the 770 captures, only 372 change the set of free variables. When the captured variable also occurs free elsewhere, FV looks right while the meaning has changed. A check on FV cannot detect capture.
  • Renaming is triggered only when needed: without a possible capture, both definitions give identical results.

Simultaneous vs sequential. (x<y)[x←y,y←x]=y<x(x < y)[x \leftarrow y, y \leftarrow x] = y < x swaps the two variables. Done one after the other, [x←y][x \leftarrow y] then [y←x][y \leftarrow x] gives x<xx < x.

Sequential substitutions compose into one simultaneous substitution: [x←S(y)][x \leftarrow S(y)] followed by [y←0][y \leftarrow 0] equals [x←S(0), y←0][x \leftarrow S(0),\ y \leftarrow 0]. Unification (Lecture 7) builds its answers by exactly such compositions.


8. One sort is enough

The slides claim that “many-sorted FOL is not more expressive than one-sorted FOL”. The translation is called relativization (fol.relativize, script 02):

  • each sort σ\sigma becomes a unary predicate isσ\mathit{is}_\sigma;
  • ∀x:σ. α\forall x{:}\sigma.\ \alpha becomes ∀x. (isσ(x)⇒α)\forall x.\ (\mathit{is}_\sigma(x) \Rightarrow \alpha);
  • ∃x:σ. α\exists x{:}\sigma.\ \alpha becomes ∃x. (isσ(x)∧α)\exists x.\ (\mathit{is}_\sigma(x) \wedge \alpha).

Axioms say that every sort is non-empty, and that every function symbol maps arguments of the right sorts to a result of the right sort:

  many-sorted:  ∀x:Nat. 0 < S(x)
  one-sorted:   ∀x. (is_Nat(x) ⇒ 0 < S(x))
  with the axioms:
      ∃x. is_Nat(x)
      is_Nat(0)
      ∀x1. (is_Nat(x1) ⇒ is_Nat(S(x1)))
      ∀x1. ∀x2. (is_Nat(x1) ∧ is_Nat(x2) ⇒ is_Nat(x1 + x2))
      ∀x1. ∀x2. (is_Nat(x1) ∧ is_Nat(x2) ⇒ is_Nat(x1 * x2))

A many-sorted formula is satisfiable iff its relativization plus the axioms is satisfiable. Sorts add convenience, catching nonsense early and making formulas shorter, not expressive power.

Note the guards: ∀ goes with ⇒, ∃ goes with ∧. That is exactly the pattern of §9.


9. From English to FOL

Script 04 parses, sort-checks and lints all seventeen translations of the slides’ From English to FOL examples. All seven number-theory sentences are well-sorted sentences. A few remarks:

  • Item 3 (“equal only if their successors are equal”, x≐y⇒S(x)≐S(y)x \doteq y \Rightarrow S(x) \doteq S(y)) is valid in every interpretation, so it says nothing about the naturals.
  • Item 4 (injectivity of SS) is a genuine axiom of arithmetic.
  • Item 5 is the contrapositive of item 4.

Two typos on the people slide.

#as printed on the slidecheckintended
8∀x. ¬∃y. ¬∃z. (x≐pa(y)∧z≐ma(z))\forall x.\ \neg\exists y.\ \neg\exists z.\ (x \doteq pa(y) \wedge z \doteq ma(z))✓ well-sorted sentence∀x. ¬(∃y. x≐pa(y)∧∃z. x≐ma(z))\forall x.\ \neg(\exists y.\ x \doteq pa(y) \wedge \exists z.\ x \doteq ma(z))
10∃x. ∀y. (¬(x≐pa(y))∧∧(x≐ma(y)))\exists x.\ \forall y.\ (\neg(x \doteq pa(y)) \wedge \wedge (x \doteq ma(y)))✗ syntax error at the second ∧∃x. ∀y. (x≠pa(y)∧x≠ma(y))\exists x.\ \forall y.\ (x \neq pa(y) \wedge x \neq ma(y))

Syntax catches item 10, which is not even well-formed. Item 8 is a perfectly good sentence that says something else. Only semantics can expose it, by evaluating both versions in small families, which Lecture 7 does.

Classic slips (script 04, signature with student, passed, likes, constant a):

Englishcorrectsliplint
Every student passed∀x. (st(x)⇒pa(x))\forall x.\ (st(x) \Rightarrow pa(x))∀x. (st(x)∧pa(x))\forall x.\ (st(x) \wedge pa(x))not detected
Some student passed∃x. (st(x)∧pa(x))\exists x.\ (st(x) \wedge pa(x))∃x. (st(x)⇒pa(x))\exists x.\ (st(x) \Rightarrow pa(x))∃x. (A ⇒ B): did you mean ∧?
Everyone likes someone∀x. ∃y. lk(x,y)\forall x.\ \exists y.\ lk(x, y)∀x. ∃y. lk(x,x)\forall x.\ \exists y.\ lk(x, x)vacuous quantifier ∃y
a likes every student∀x. (st(x)⇒lk(a,x))\forall x.\ (st(x) \Rightarrow lk(a, x))∀x. (st(x)⇒∀x. lk(a,x))\forall x.\ (st(x) \Rightarrow \forall x.\ lk(a, x))∀x shadows an outer x
Every student likes someone∀x. (st(x)⇒∃y. lk(x,y))\forall x.\ (st(x) \Rightarrow \exists y.\ lk(x, y))∀x. st(x)⇒∃y. lk(x,y)\forall x.\ st(x) \Rightarrow \exists y.\ lk(x, y)the last xx is free (sort check fails)

(The table abbreviates student, passed and likes to stst, papa, lklk.)

  • The rule of thumb: ∀ goes with ⇒, ∃ goes with ∧. ∀x. (A∧B)\forall x.\ (A \wedge B) claims everything is an A. ∃x. (A⇒B)\exists x.\ (A \Rightarrow B) is true as soon as something is not an A.
  • Four of the five slips are visible in the syntax. The first is not: it is a good sentence that says the wrong thing.

Quantifier order.

  • ∀x. ∃y. x<y\forall x.\ \exists y.\ x < y (“every number has a larger one”) is true in ℕ.
  • ∃y. ∀x. x<y\exists y.\ \forall x.\ x < y (“one number is larger than all”) is false in ℕ.

In ∀∃\forall\exists the witness may depend on xx; in ∃∀\exists\forall one witness must serve all. The direction ∃∀⊨∀∃\exists\forall \models \forall\exists always holds, the converse does not (Lecture 7 checks both). Swapping quantifiers is the most common formalization error, and syntax cannot flag it.


10. Summary

  1. FOL opens the atoms of propositional logic: terms name objects, predicates relate them, quantifiers range over them. Deduction then involves substitution.
  2. A signature fixes the vocabulary (sorts, ranked symbols), not the meaning. Constants and predicates are special function symbols; Bool, ⊤, ⊥ and ≐σ\doteq_\sigma are always present.
  3. Terms are a generated set. Well-formedness is about shape and arity; well-sortedness is decided by a small proof system Γ⊢t:σ\Gamma \vdash t : \sigma whose derivations are certificates.
  4. Formulas are generated from Bool-sorted atoms by connectives and sorted quantifiers. Conventions: ¬ and quantifiers bind most tightly; binary connectives group to the right.
  5. Free variables are computed by induction; sentences have none. Renaming bound variables apart gives α-equivalent formulas.
  6. Substitution must avoid capture. Naive substitution is unsound; it threatened a quarter of random substitutions. Capture-avoiding substitution renames bound variables only when needed.
  7. Sorts add convenience, not power: relativization translates many-sorted into one-sorted FOL.
  8. Translating English: ∀ with ⇒, ∃ with ∧. Watch quantifier order. Syntax catches some slips (and two typos on the slides); semantics is needed for the rest.

11. Common pitfalls

  • “Constants are variables.” A constant is part of the signature and has a fixed meaning in each interpretation; a variable can be quantified. In fol.parse without a signature, names like x, y, z are variables and a, b, c are constants.
  • “(S (< 0 0)) is not a term.” It is a well-formed term, just not well-sorted.
  • “A predicate is not a function.” In the slides’ setting it is a function into Bool, which is why atomic formulas are terms.
  • “∀x. p(x) ∧ q(x) quantifies both.” Under the slides’ convention the quantifier binds only p(x)p(x).
  • Substituting into bound occurrences. (∀x. x<y)[x←0](\forall x.\ x < y)[x \leftarrow 0] is unchanged: bound occurrences are not “the same xx”.
  • Naive substitution. Correct when no capture is possible, wrong otherwise. The bug surfaces rarely in tests that look only at free variables (§7).
  • Sequential = simultaneous. Not in general: the swap example.
  • “Item 8 parses, so it is right.” Well-formed and well-sorted say nothing about whether a formula means what you intended.

12. Exercises

Answers are collapsed; try each exercise first.

1. (terms) Which of the following are well-formed terms of number theory, and which are well-sorted in Γ={x:Nat,b:Bool}\Gamma = \{x : \mathrm{Nat}, b : \mathrm{Bool}\}? (S (S x)), (+ x), (< x (S b)), (+ (< x x) 0), (S x x).

Answer
  • (S (S x)): well-formed and well-sorted, sort Nat.
  • (+ x): not well-formed; + has arity 2.
  • (< x (S b)): well-formed but not well-sorted; S needs a Nat argument and bb : Bool.
  • (+ (< x x) 0): well-formed but not well-sorted; the first argument of + has sort Bool.
  • (S x x): not well-formed; S has arity 1.

2. (sort derivation) Write the derivation of Γ⊢(< (S x3) (+ (S 0) x2)):Bool\Gamma \vdash (<\ (S\ x_3)\ (+\ (S\ 0)\ x_2)) : \mathrm{Bool} for Γ={x2:Nat,x3:Nat}\Gamma = \{x_2 : \mathrm{Nat}, x_3 : \mathrm{Nat}\}.

Answer
Γ ⊢ S(x3) < S(0) + x2 : Bool        FUN, rank(<) = ⟨Nat, Nat, Bool⟩
  Γ ⊢ S(x3) : Nat                   FUN, rank(S) = ⟨Nat, Nat⟩
    Γ ⊢ x3 : Nat                    VAR
  Γ ⊢ S(0) + x2 : Nat               FUN, rank(+) = ⟨Nat, Nat, Nat⟩
    Γ ⊢ S(0) : Nat                  FUN
      Γ ⊢ 0 : Nat                   CONST
    Γ ⊢ x2 : Nat                    VAR

3. (the inconsistent slide) Explain in two sentences why no context makes the slides’ answers ✓ ✗ ✓ ✓ ✓ correct.

Answer

Item 2, (+ (+ 0 x1) x2), is ill-sorted only if x1x_1 is not Nat. Items 4 and 5 contain (+ (S 0) x1), which is well-sorted only if x1x_1 : Nat. So items 2, 4 and 5 are either all ✓ or all ✗.

4. (free variables) Compute FV of ∀x. (p(x,y)⇒∃y. q(y,z))∨r(x)\forall x.\ (p(x, y) \Rightarrow \exists y.\ q(y, z)) \vee r(x). Is it a sentence? Rename it apart.

Answer
  • Precedence. By the conventions, ∀x\forall x covers the parenthesized implication, and r(x)r(x) is outside its scope.
  • Inside. FV(p(x,y))={x,y}\mathrm{FV}(p(x,y)) = \{x, y\} and FV(∃y. q(y,z))={z}\mathrm{FV}(\exists y.\ q(y,z)) = \{z\}. So the implication has {x,y,z}\{x, y, z\}, and after ∀x\forall x: {y,z}\{y, z\}.
  • Whole formula. Joining with r(x)r(x) gives {x,y,z}\{x, y, z\}. It is not a sentence.
  • Renamed apart: ∀x′. (p(x′,y)⇒∃y′. q(y′,z))∨r(x)\forall x'.\ (p(x', y) \Rightarrow \exists y'.\ q(y', z)) \vee r(x).

5. (capture) Compute (∀y. (x<y⇒∃x. x<z))[x←y,z←x](\forall y.\ (x < y \Rightarrow \exists x.\ x < z))[x \leftarrow y, z \leftarrow x] correctly.

Answer
  • Entering ∀y\forall y. The substitution is used inside: xx is free in the body, and zz is free inside ∃x\exists x. The terms yy and xx have free variables {y,x}\{y, x\}, so the bound yy would capture the yy substituted for xx. Rename it: ∀y′. (x<y′⇒∃x. x<z)\forall y'.\ (x < y' \Rightarrow \exists x.\ x < z).
  • First conjunct. Substituting gives y<y′y < y'.
  • Entering ∃x\exists x. The entry for xx is dropped, since xx is bound there. The remaining entry, z←xz \leftarrow x, would put a free xx under ∃x\exists x: capture again. Rename the bound xx to x′x'.
  • Result: ∀y′. (y<y′⇒∃x′. x′<x)\forall y'.\ (y < y' \Rightarrow \exists x'.\ x' < x).

substitute in fol.py computes the same.

6. (the substitution lemma) Prove by induction on α\alpha that capture-avoiding substitution satisfies FV(α[v←t])=(FV(α)∖{v})∪FV(t)\mathrm{FV}(\alpha[v \leftarrow t]) = (\mathrm{FV}(\alpha) \setminus \{v\}) \cup \mathrm{FV}(t) when v∈FV(α)v \in \mathrm{FV}(\alpha).

Answer sketch

Atoms. Replacing the occurrences of vv by tt removes vv and adds FV(t)\mathrm{FV}(t).

Connectives. Take the union; for a subformula without vv, nothing changes.

α=Q y. β\alpha = Q\, y.\ \beta with v≠yv \ne y (if v=yv = y, then v∉FV(α)v \notin \mathrm{FV}(\alpha)).

  • If needed, yy is first renamed to a fresh y′y'. This does not change FV(α)\mathrm{FV}(\alpha).
  • By the induction hypothesis, FV(β′[v←t])=(FV(β′)∖{v})∪FV(t)\mathrm{FV}(\beta'[v \leftarrow t]) = (\mathrm{FV}(\beta') \setminus \{v\}) \cup \mathrm{FV}(t).
  • Removing y′y' gives (FV(α)∖{v})∪(FV(t)∖{y′})(\mathrm{FV}(\alpha) \setminus \{v\}) \cup (\mathrm{FV}(t) \setminus \{y'\}).
  • y′∉FV(t)y' \notin \mathrm{FV}(t) by freshness, so this is the claim.

Where naive substitution fails. The last step: when y∈FV(t)y \in \mathrm{FV}(t) and no renaming happens, yy is wrongly removed.

7. (relativization) Relativize ∃s:Set. ∀x:Elem. ¬member(x,s)\exists s{:}\mathrm{Set}.\ \forall x{:}\mathrm{Elem}.\ \neg\mathit{member}(x, s) and list the axioms for the signature ELEM_SET.

Answer

∃s. (isSet(s)∧∀x. (isElem(x)⇒¬member(x,s)))\exists s.\ (\mathit{is}_{Set}(s) \wedge \forall x.\ (\mathit{is}_{Elem}(x) \Rightarrow \neg\mathit{member}(x, s))).

Axioms: ∃x. isElem(x)\exists x.\ \mathit{is}_{Elem}(x), ∃x. isSet(x)\exists x.\ \mathit{is}_{Set}(x), isSet(∅)\mathit{is}_{Set}(\emptyset). The predicate member needs no axiom.

8. (formalization) Translate into FOL with pa, ma, mar: (a) “Everyone’s mother is married to their father”; (b) “Somebody is their own grandmother”; (c) “No one is married to two different people”.

Answer
  • (a) ∀x. mar(ma(x),pa(x))\forall x.\ \mathit{mar}(ma(x), pa(x)).
  • (b) ∃x. (x≐ma(ma(x))∨x≐ma(pa(x)))\exists x.\ (x \doteq ma(ma(x)) \vee x \doteq ma(pa(x))).
  • (c) ∀x. ∀y. ∀z. (mar(x,y)∧mar(x,z)⇒y≐z)\forall x.\ \forall y.\ \forall z.\ (\mathit{mar}(x, y) \wedge \mathit{mar}(x, z) \Rightarrow y \doteq z). This is the slides’ item 6.

9. (slips) Which lint warning, if any, does each produce? (a) ∃x. (dog(x)⇒barks(x))\exists x.\ (\mathit{dog}(x) \Rightarrow \mathit{barks}(x)); (b) ∀x. ∃y. likes(x,z)\forall x.\ \exists y.\ \mathit{likes}(x, z); (c) ∀x. (dog(x)∧barks(x))\forall x.\ (\mathit{dog}(x) \wedge \mathit{barks}(x)).

Answer
  • (a) “∃x. (A ⇒ B): did you mean ∧?”
  • (b) A vacuous ∃y, and the free variable zz.
  • (c) None. It is a well-formed sentence claiming that everything is a barking dog. Only semantics can tell it apart from the intended ∀x. (dog(x)⇒barks(x))\forall x.\ (\mathit{dog}(x) \Rightarrow \mathit{barks}(x)).

10. (counting, programming) Extend dp_counts in script 01 with the symbol * (rank ⟨Nat, Nat, Nat⟩) and recount. Which columns change, and why does the ratio well-sorted / well-formed fall even faster?

Answer sketch

With * added, the alphabet has 9 tokens, so the expressions column becomes 9n9^n.

The recurrences. The binary symbols now contribute three times: W(n)W(n) gains a third ∑W(a)W(b)\sum W(a)W(b) term. N(n)N(n) gains a second ∑N(a)N(b)\sum N(a)N(b) term, since both + and * produce Nat. B(n)=∑N(a)N(b)B(n) = \sum N(a)N(b) keeps its formula (only < produces Bool), but it grows too, because there are now more Nat terms to feed into <.

The ratio. It falls faster because every additional binary node multiplies the ill-sorted possibilities: each argument may be the Bool variable bb or a <-term.

To check, run the brute-force comparison for n≤5n \le 5.


13. Further reading

  • A. Bradley, Z. Manna, The Calculus of Computation, Chapter 2 (first-order logic: syntax, semantics, substitution, normal forms).
  • H. Enderton, A Mathematical Introduction to Logic, §2.1–2.2: the standard one-sorted presentation, including the definition of substitutability.
  • M. Huth, M. Ryan, Logic in Computer Science, Chapter 2: a gentle introduction with many translation exercises.
  • N. G. de Bruijn, “Lambda calculus notation with nameless dummies”, Indagationes Mathematicae 1972: the index notation that avoids capture by construction.
  • The SMT-LIB standard (smt-lib.org), version 2.6: many-sorted first-order logic as a concrete input language, with the same sort system.

14. Next lecture

Lecture 7: semantics, normal forms, resolution, unification I. Lecture 7 gives these formulas meaning.

  • Semantics. An interpretation gives each sort a non-empty domain, each function symbol a function, and each free variable a value. A formula is then true or false.
  • Modules. We build fol_semantics.py: finite structures and evaluation, plus a finite model finder that grounds formulas into propositional logic and calls Lecture 5’s CDCL solver.
  • Checks. With it, we check the slides’ entailment exercise, the two people-slide typos, and the translation slips of §9.
  • Normal forms. We then turn formulas into prenex form and clauses (Skolemization).
  • Unification. We meet unification, the algorithm that finds the substitution making two terms equal. It is what makes first-order resolution practical.
← 返回课程目录