One sentence, many worlds
Here is a theorem about complex polynomials:
Ax–Grothendieck. If a polynomial map is injective, then it is surjective.
For this is easy (a non-constant polynomial in one variable takes every value). For it is not at all obvious. Yet over a finite set it is trivial: an injective map from a finite set to itself is surjective, by counting. The proof we will give runs: the statement, for maps of bounded degree, is a single first-order sentence; it is true over the algebraic closure of every finite field because those are unions of finite fields; and any first-order sentence in the language of rings that is true in algebraically closed fields of every prime characteristic is true in . Each step is a theorem of model theory.
Or take two familiar orders, the rationals and the reals . They are not isomorphic — one is countable, the other is not — and is complete while is full of gaps. Yet no sentence of first-order logic that talks only about can tell them apart. Model theory explains why, and turns that blindness into a powerful method.
Model theory studies the relationship between theories (sets of sentences) and their models (structures in which the sentences are true): what a theory determines about its models, which sets are definable in a structure, and how structures that satisfy the same sentences can be built and compared. This lesson assumes first-order logic and the completeness, compactness and Löwenheim–Skolem theorems.
Sentences cut out classes of structures
Fix a language — a list of function, relation and constant symbols. For a set of -sentences and a class of -structures put
Both maps reverse inclusions, and : they form an antitone Galois connection. By the completeness theorem, is exactly the set of consequences of . A single structure has a complete theory : for each sentence , exactly one of , belongs to it.
Analogy, not a formal construction
In algebraic geometry, polynomial equations cut out varieties, and a set of points determines the ideal of polynomials vanishing on it; the maps and form a Galois connection of exactly the same shape. Model theory's "geometry" is the study of definable sets: subsets of of the form . For algebraically closed fields the analogy becomes literal — the definable sets are the constructible sets of algebraic geometry. In general it is only an analogy: definable sets need not look like varieties at all.
Three tools do most of the work in a first course:
- Ultraproducts average a family of structures; Łoś's theorem says a sentence holds in the average iff it holds in "most" of the factors.
- Quantifier elimination shows that, in a good theory, every definable set is defined without quantifiers — so definable sets can be understood concretely.
- Back and forth builds isomorphisms, and its finite version — Ehrenfeucht–Fraïssé games — shows exactly what first-order logic can and cannot see.
Definitions
Throughout, structures are nonempty, and denotes a finite tuple.
Definition— elementary equivalence
-structures and are elementarily equivalent, written , if they satisfy the same -sentences: .
Definition— elementary embedding and elementary substructure
A map is an elementary embedding if for every formula and every ,
If and the inclusion is elementary, is an elementary substructure of and an elementary extension of , written .
Taking shows that implies . The converse fails, and the difference is parameters: is a substructure of isomorphic to it, hence elementarily equivalent, but the formula holds of in the substructure and not in .
Theorem— Tarski–Vaught test
Let be a substructure of . Then if and only if for every formula and every from : if , then for some .
Proof
() If then by elementarity; pick with ; by elementarity again .
() We show for all formulas and tuples from , by induction on (built from atomic formulas with , , ). Atomic formulas are preserved because is a substructure: terms are evaluated the same way in and . The cases and are immediate from the induction hypothesis. If , choose a witness ; by induction , so . Conversely, if , the hypothesis gives with , and by induction .
∎
Note that the test only ever evaluates formulas in : it asks whether contains enough witnesses.
Definition— complete theory, categoricity
A consistent theory is complete if for every sentence , either or . For an infinite cardinal , is -categorical if any two models of of cardinality are isomorphic.
Definition— quantifier elimination
A theory has quantifier elimination (QE) if every formula is -equivalent to a quantifier-free formula with the same free variables: . (We count and as quantifier-free, so that sentences can be equivalent to quantifier-free sentences even in a language without constants.)
Definition— ultrafilter and ultraproduct
An ultrafilter on a set is a family of subsets of with , closed under finite intersections and supersets, and such that for every exactly one of and is in . It is principal if it consists of all sets containing a fixed . Every family of subsets with the finite intersection property extends to an ultrafilter (by Zorn's lemma); in particular, non-principal ultrafilters on exist (extend the cofinite sets).
Given -structures and an ultrafilter on , the ultraproduct is the set modulo , with functions and constants interpreted coordinatewise and
If all , this is the ultrapower , and is the diagonal map .
Closure of under intersections and supersets makes an equivalence relation and makes the interpretations independent of representatives. Think of as deciding which sets of indices are "large": a property holds in the ultraproduct when it holds on a large set of coordinates.
Definition— types
Let be a structure and . A complete -type over is a set of formulas in free variables with parameters from that is maximal subject to being satisfiable, where is with names for the elements of . The set of these is . A type is realised in an elementary extension by if for all .
Examples and non-examples
Example— Elementarily equivalent, not isomorphic
, and in fact ; both statements are proved below, from quantifier elimination for dense linear orders. They are not isomorphic, since is countable and is not. "Every bounded nonempty set has a supremum" is not first-order: it quantifies over sets.
Example— Distinguished by one sentence
- : the sentence ("there is a least element") separates them.
- : density, , separates them. So is a substructure that is not elementary.
- Finite structures of different sizes are never elementarily equivalent: "there are exactly elements" is a sentence. But sentences of bounded quantifier depth see only a bounded amount: the computation section shows that sentences with at most nested quantifiers cannot tell a linear order with elements from one with .
Example— A theory without quantifier elimination
In the formula is not equivalent to a quantifier-free formula in . A quantifier-free formula in two variables is a Boolean combination of , , , so it has the same truth value at and ; but is false and is true. (Enlarging the language by successor and predecessor functions restores quantifier elimination for .)
Similarly, the ordered field has QE in the language (Tarski), but not in the ring language : there defines , while a quantifier-free formula in one variable defines a finite or cofinite set.
Example— Hyperreals and nonstandard natural numbers
Let be a non-principal ultrafilter on . In , the element satisfies for every real : the set is cofinite, hence in . Likewise in the class of lies above every standard natural number — proved in Lean below. By Łoś's theorem the diagonal maps and are elementary, so these new elements live in structures with exactly the same first-order theory as the old ones.
Łoś's theorem and compactness
Theorem— Łoś's theorem
Let be nonempty -structures, an ultrafilter on and . For every formula and ,
Proof
Write . First, for every term , the value is the class of : this is an induction on , using that functions are interpreted coordinatewise. Now induct on , built from atomic formulas by , and .
- Atomic. For this is the definition of , and for the definition of , combined with the statement about terms.
- Negation. iff (induction) iff . This is the only step that uses that is an ultrafilter.
- Conjunction. , and a filter contains iff it contains both and .
- Existential. Suppose , witnessed by . By induction , and this set is contained in , which is therefore in . Conversely, suppose . For choose with , and for let be any element of (nonempty). Then is in , so by induction , hence .
∎
The existential step uses the axiom of choice to pick — this is where all the witnesses are assembled into one element.
Theorem— Corollary — the diagonal embedding is elementary
For every structure and ultrafilter on , the diagonal map is an elementary embedding.
Proof
For , Łoś gives iff . That set is or according as or not, and , .
∎
Theorem— Compactness theorem — second proof
If every finite subset of a theory has a model, then has a model.
Proof
Let be the set of finite subsets of , and for each choose a model . For let . These sets have the finite intersection property, since . Extend them to an ultrafilter on , and let . For , every has , so belongs to , and by Łoś.
∎
This proof is purely semantic: it does not pass through the completeness theorem or any proof system. It is the proof Mathlib formalises.
Dense linear orders: quantifier elimination and back and forth
Let be the theory, in the language , of dense linear orders without endpoints: the axioms of a strict linear order, together with , and . Its models include , , and the real algebraic numbers.
We first reduce QE in general to a single quantifier applied to a conjunction.
Theorem— Lemma — reduction to primitive formulas
Suppose that for every conjunction of atomic and negated atomic formulas, is -equivalent to a quantifier-free formula in . Then has quantifier elimination.
Proof
By induction on formulas we show each is -equivalent to a quantifier-free one. Atomic formulas are quantifier-free, and the class of formulas with quantifier-free equivalents is closed under and . For , replace by a quantifier-free equivalent and put it in disjunctive normal form , each a conjunction of literals. Since is logically equivalent to , the hypothesis applies to each disjunct.
∎
The heart of the matter is the following lemma, formalised in Lean below as dlo_elim_core.
Theorem— Key lemma
Let and let be finite. There is with for all and for all if and only if for all , .
Proof
The forward direction is transitivity. Conversely, assume for all , . If and are both nonempty, then , and density gives with . If only is nonempty, take (no least element); if only is nonempty, take (no greatest element); if both are empty, take any ( is nonempty).
∎
Theorem— Theorem — DLO has quantifier elimination
Every formula in the language is -equivalent to a quantifier-free formula.
Proof
By the reduction lemma it suffices to treat with a conjunction of literals. In a linear order, and ; substituting and redistributing, becomes a disjunction of conjunctions of atomic formulas, and distributes over . So let be a conjunction of atomic formulas. Conjuncts not mentioning can be moved outside the quantifier. A conjunct makes equivalent to ; a conjunct can be dropped. If some conjunct is (or ) with a variable other than , then . Otherwise has the form for finite sets of variables and , and by the key lemma is equivalent to — the empty conjunction being .
∎
Theorem— Corollary — completeness and model completeness
(1) is complete; in particular . (2) Every embedding between models of is elementary; in particular .
Proof
(1) The language has no constant symbols, so a quantifier-free sentence is a Boolean combination of and . By QE every sentence is -equivalent to or to , so decides it; and is consistent because is a model. (2) Embeddings preserve quantifier-free formulas, and every formula is -equivalent to a quantifier-free one.
∎
There is a second, older route to completeness, through categoricity.
Theorem— Cantor's theorem — DLO is ℵ₀-categorical
Any two countable models of are isomorphic.
Proof
Let , be countable models of DLO; they are infinite (a finite nonempty linear order has a greatest element). Enumerate and . Call a finite order-preserving bijection between a finite subset of and a finite subset of a partial isomorphism.
Extension property: if is a partial isomorphism and , there is with a partial isomorphism. If take . Otherwise let and . Every element of is below every element of , since preserves order, so the key lemma (applied in ) gives above and below ; then and preserves order. By symmetry, for every there is a matching .
Start with . At step extend so that is in the domain ("forth"); at step extend so that is in the range ("back"). The union is an order-preserving map defined on all of and onto all of : an isomorphism.
∎
Theorem— Łoś–Vaught test
Let be a consistent theory in a language with no finite models, and suppose is -categorical for some infinite cardinal . Then is complete.
Proof
If decided neither nor , both and would have models, necessarily infinite. By the upward and downward Löwenheim–Skolem theorems each has a model of cardinality . These two models of are isomorphic by categoricity, yet one satisfies and the other — a contradiction.
∎
With Cantor's theorem (, finite) this gives a second proof that DLO is complete. Mathlib proves completeness exactly this way.
Algebraically closed fields, Lefschetz and Ax–Grothendieck
Work in the language of rings . Let be the theory of algebraically closed fields — the field axioms plus, for each , the sentence . For prime, adds ; and adds for every prime .
Theorem— Theorem (Tarski) — ACF has quantifier elimination
Every formula in the language of rings is -equivalent to a quantifier-free formula.
We do not prove this here; a complete proof (by the same reduction lemma, with a careful elimination of from systems using polynomial division) is in Marker, Model Theory: An Introduction, Theorem 3.2.2. Its consequences are what matter for us.
- Constructible sets. In , a quantifier-free formula with parameters defines a finite Boolean combination of zero sets of polynomials — a constructible set. So the definable subsets of are exactly the constructible ones. Since an existential quantifier is a coordinate projection, QE implies that the projection of a constructible set is constructible: this is Chevalley's theorem for affine space over . (Conversely, a sufficiently uniform form of Chevalley's theorem yields QE; the two are closely related rather than literally the same statement.)
- Completeness of . A quantifier-free sentence is a Boolean combination of atomic sentences between closed terms, that is, of statements with . These are decided by the characteristic. Hence is complete for and for every prime . In particular , and by QE again .
Theorem— Lefschetz principle
For a sentence in the language of rings, the following are equivalent:
- ;
- ;
- for all sufficiently large primes ;
- for infinitely many primes .
Proof
(1)⇔(2): , which is complete. (2)⇒(3): has no model, so by compactness some finite subset has none; such a subset lies in for some , so every algebraically closed field of characteristic at least satisfies . (3)⇒(4) is trivial. (4)⇒(2): if , completeness gives , so by (2)⇒(3) for all large ; since each is consistent, fails in for all large , contradicting (4).
∎
Theorem— Ax–Grothendieck theorem
Let be an algebraically closed field. Every injective polynomial map is surjective.
Proof
Fix and a degree bound . "Every polynomial map whose coordinates have degree at most and which is injective is surjective" is a single first-order sentence : quantify universally over the finitely many coefficients of polynomials of degree , and write
Step 1: holds in for every prime . Let have coefficients in , be injective, and let . The finitely many coefficients of and coordinates of are algebraic over , so they generate a finite subfield . Then maps to and is injective there; as is finite, is surjective, so for some .
Step 2: transfer. As is complete and , Step 1 gives for every prime . By the Lefschetz principle . So every algebraically closed field, of any characteristic, satisfies . Every polynomial map has some degree .
∎
The algebraic closure matters: over , the map is injective but is not a cube; over , the Frobenius is injective but is not a -th power.
Note
Ultraproducts give another view of the Lefschetz principle. If is a non-principal ultrafilter on the set of primes, then is algebraically closed (Łoś, axiom by axiom) of characteristic (each sentence holds in all factors with ), and it has cardinality . Since is categorical in every uncountable cardinal (an algebraically closed field is determined by its characteristic and transcendence degree — Steinitz), this ultraproduct is isomorphic to .
Types and the Stone space
Types record how a new element could sit over a set of parameters. For , quantifier elimination says a complete 1-type over is determined by which atomic formulas , , () it contains. Over inside , the complete 1-types are therefore:
- for each (realised in );
- for each irrational , the type (not realised in , realised in by );
- for each , a type "infinitely close to from above" ( and for all rationals ), and similarly ;
- ( for all ) and .
So . The types and are realised in neither nor . All of these types are realised in the ultrapower , an elementary extension of by Łoś's theorem: by , by , by , and the cut of an irrational by the class of any sequence of rationals converging to .
The set of complete -types of a complete theory (over ) carries the Stone topology, with basic open sets . These sets are also closed (their complements are ), so is totally disconnected and Hausdorff; and it is compact precisely because of the compactness theorem: a cover by basic open sets with no finite subcover would give a set of formulas , finitely satisfiable with but not satisfiable. A type is an isolated point iff it is generated by a single formula modulo .
Model theory in Lean and Mathlib
Mathlib has a substantial model theory library: languages, structures, terms and formulas whose bound variables are indexed by Fin n (FirstOrder.Language.BoundedFormula), satisfaction M ⊨ φ, theories, elementary embeddings and substructures, ultraproducts, compactness, Löwenheim–Skolem, Fraïssé limits, and the theories of orders and algebraically closed fields. Here is what this lesson proves and what it applies.
The key lemma of quantifier elimination, proved here, following the proof above case by case. The statement uses finite sets of parameters L and U:
/-- **The key lemma of quantifier elimination for DLO.** In a nonempty dense linear order
without endpoints, a point `x` strictly above every element of the finite set `L` and strictly
below every element of the finite set `U` exists **if and only if** every element of `L` is
below every element of `U`. So the formula `∃x (⋀ aᵢ < x ∧ ⋀ x < bⱼ)` is equivalent to the
quantifier-free formula `⋀ᵢⱼ aᵢ < bⱼ`.
The proof follows the lesson: if both sets are nonempty, density gives a point between
`max L < min U`; if one is empty, "no endpoints" gives a point beyond the other; if both are
empty, nonemptiness gives any point. -/
theorem dlo_elim_core {α : Type*} [LinearOrder α] [DenselyOrdered α] [NoMinOrder α]
[NoMaxOrder α] [Nonempty α] (L U : Finset α) :
(∃ x, (∀ a ∈ L, a < x) ∧ ∀ b ∈ U, x < b) ↔ ∀ a ∈ L, ∀ b ∈ U, a < b := by
constructor
· rintro ⟨x, hL, hU⟩ a ha b hb
exact (hL a ha).trans (hU b hb)
· intro h
rcases L.eq_empty_or_nonempty with rfl | hL
· rcases U.eq_empty_or_nonempty with rfl | hU
· -- no constraints at all: any point will do
obtain ⟨x⟩ := ‹Nonempty α›
exact ⟨x, by simp, by simp⟩
· -- only upper bounds: go below the least of them (no least element)
obtain ⟨x, hx⟩ := exists_lt (U.min' hU)
exact ⟨x, by simp, fun b hb => hx.trans_le (U.min'_le b hb)⟩
· rcases U.eq_empty_or_nonempty with rfl | hU
· -- only lower bounds: go above the greatest of them (no greatest element)
obtain ⟨x, hx⟩ := exists_gt (L.max' hL)
exact ⟨x, fun a ha => (L.le_max' a ha).trans_lt hx, by simp⟩
· -- both: `max L < min U` by hypothesis, and density puts a point in between
have hlt : L.max' hL < U.min' hU := h _ (L.max'_mem hL) _ (U.min'_mem hU)
obtain ⟨x, hx₁, hx₂⟩ := exists_between hlt
exact ⟨x, fun a ha => (L.le_max' a ha).trans_lt hx₁,
fun b hb => hx₂.trans_le (U.min'_le b hb)⟩Density is genuinely needed — the elimination is false in the integers:
/-- **Density is needed.** In the integers (a linear order without endpoints, but *not* dense)
the elimination of `model_theory.dlo_elim_core` fails: `0 < 1` holds, yet nothing lies strictly
between `0` and `1`. So `∃x (0 < x ∧ x < 1)` is not equivalent to `0 < 1` in `(ℤ, <)`. -/
theorem dlo_elim_core_fails_discrete :
(∀ a ∈ ({0} : Finset ℤ), ∀ b ∈ ({1} : Finset ℤ), a < b) ∧
¬ ∃ x : ℤ, (∀ a ∈ ({0} : Finset ℤ), a < x) ∧ ∀ b ∈ ({1} : Finset ℤ), x < b := by
refine ⟨by simp, ?_⟩
rintro ⟨x, h0, h1⟩
have hx0 : 0 < x := h0 0 (by simp)
have hx1 : x < 1 := h1 1 (by simp)
omegaMathlib does not (yet) state quantifier elimination for DLO as a theorem about all formulas; the lemma above is the mathematical heart of it, and the reduction from arbitrary formulas is the syntactic induction in the human proof.
but . Mathlib's language of orders uses rather than ; the two are interdefinable. The structures are given by Mathlib's orderStructure. Elementary equivalence applies Mathlib's completeness of dlo (proved there by the Łoś–Vaught test); non-isomorphism is proved here from Cantor's uncountability of :
/-- **`(ℚ, ≤) ≡ (ℝ, ≤)`**: the rationals and the reals satisfy exactly the same first-order
sentences in the language `{≤}`. Both are models of the theory `dlo` of dense linear orders
without endpoints, and `dlo` is complete (Mathlib's `FirstOrder.Language.dlo_isComplete`, proved
by the Łoś–Vaught test from `ℵ₀`-categoricity); in a complete theory every sentence is decided,
so each model satisfies `φ` iff `dlo ⊨ φ` (`Theory.IsComplete.realize_sentence_iff`). -/
theorem rat_elem_equiv_real :
letI := orderStructure ℚ
letI := orderStructure ℝ
ℚ ≅[Language.order] ℝ := by
let := orderStructure ℚ
let := orderStructure ℝ
have : Language.order.OrderedStructure ℚ := ⟨fun _ => Iff.rfl⟩
have : Language.order.OrderedStructure ℝ := ⟨fun _ => Iff.rfl⟩
rw [elementarilyEquivalent_iff]
intro φ
rw [dlo_isComplete.realize_sentence_iff φ ℚ, dlo_isComplete.realize_sentence_iff φ ℝ]/-- **…but not isomorphic.** There is no isomorphism of `{≤}`-structures between `ℚ` and `ℝ`:
an isomorphism is in particular a bijection, which would make `ℝ` countable, contradicting
Cantor's theorem (`Cardinal.not_countable_real`). Elementary equivalence is much weaker than
isomorphism: first-order logic cannot express "countable" or "complete". -/
theorem rat_not_equiv_real :
letI := orderStructure ℚ
letI := orderStructure ℝ
¬ Nonempty (ℚ ≃[Language.order] ℝ) := by
let := orderStructure ℚ
let := orderStructure ℝ
rintro ⟨e⟩
-- `e⁻¹ : ℝ → ℚ` is injective and `ℚ` is countable, so `ℝ` would be countable.
have hinj : Function.Injective e.symm := e.symm.injective
have : Countable ℝ := hinj.countable
exact Cardinal.not_countable_real Set.countable_univBack and forth and categoricity, from Mathlib:
/-- **Cantor's theorem (back and forth).** Any two countable, nonempty, dense linear orders
without endpoints are order-isomorphic. This applies Mathlib's `Order.iso_of_countable_dense`,
whose proof is the back-and-forth construction of the lesson. -/
theorem cantor_back_and_forth (α : Type u) (β : Type v) [LinearOrder α] [LinearOrder β]
[Countable α] [DenselyOrdered α] [NoMinOrder α] [NoMaxOrder α] [Nonempty α]
[Countable β] [DenselyOrdered β] [NoMinOrder β] [NoMaxOrder β] [Nonempty β] :
Nonempty (α ≃o β) :=
Order.iso_of_countable_dense α β/-- **`DLO` is `ℵ₀`-categorical**: all countably infinite models of the theory of dense linear
orders without endpoints are isomorphic as first-order structures. This applies Mathlib's
`FirstOrder.Language.aleph0_categorical_dlo` (proved there via Fraïssé limits, a model-theoretic
packaging of back and forth). -/
theorem dlo_aleph0_categorical : Cardinal.Categorical.{0, 0, w} Cardinal.aleph0 Language.order.dlo :=
aleph0_categorical_dloŁoś and compactness. Mathlib's Filter.Product M U is the ultraproduct. Note the hypothesis that the factors are nonempty, used in the existential step of the proof:
/-- **Łoś's theorem.** For nonempty structures `M a` (indexed by `a : α`) and an ultrafilter `U`
on `α`, a sentence holds in the ultraproduct `∏ M a / U` if and only if the set of indices `a` at which
it holds in `M a` belongs to `U`. This applies Mathlib's
`FirstOrder.Language.Ultraproduct.sentence_realize`, proved by induction on formulas exactly as
in the lesson. -/
theorem los {L : Language.{u, v}} {α : Type*} (M : α → Type*) (U : Ultrafilter α)
[∀ a, L.Structure (M a)] [∀ a, Nonempty (M a)] (φ : L.Sentence) :
(U : Filter α).Product M ⊨ φ ↔ ∀ᶠ a in (U : Filter α), M a ⊨ φ :=
Ultraproduct.sentence_realize φ/-- **The compactness theorem.** A first-order theory has a model if and only if every finite
subset of it has a model. This applies Mathlib's `Theory.isSatisfiable_iff_isFinitelySatisfiable`,
whose proof is the lesson's second proof: take an ultraproduct of models of the finite subsets,
indexed by the finite subsets, along an ultrafilter extending the "eventually" filter, and apply
Łoś's theorem. -/
theorem compactness {L : Language.{u, v}} (T : L.Theory) :
T.IsSatisfiable ↔ T.IsFinitelySatisfiable :=
Theory.isSatisfiable_iff_isFinitelySatisfiableA nonstandard natural number, proved here. Filter.hyperfilter ℕ is a non-principal ultrafilter on and (hyperfilter ℕ).Germ ℕ is the ultrapower . The order on germs is "eventually less" — the atomic case of Łoś's theorem:
/-- **A nonstandard natural number.** Let `U` be a non-principal ultrafilter on `ℕ` (Mathlib's
`hyperfilter ℕ`). In the ultrapower `ℕ^ℕ/U`, the class of the identity sequence `(0, 1, 2, …)`
is strictly greater than (the class of) every constant sequence `n`. Indeed, by the definition of
the order on the ultrapower (`Filter.Germ.coe_lt`, the atomic case of Łoś) we need
`{i | n < i} ∈ U`; this set is cofinite, and a non-principal ultrafilter contains every cofinite
set (`Filter.hyperfilter_le_cofinite`). By Łoś, `ℕ^ℕ/U` satisfies the same sentences as `ℕ`,
yet it has an element above all the standard ones. -/
theorem nonstandard_natural (n : ℕ) :
(↑(fun _ : ℕ => n) : (Filter.hyperfilter ℕ : Filter ℕ).Germ ℕ) < ↑(id : ℕ → ℕ) := by
rw [Filter.Germ.coe_lt]
have h : ∀ᶠ i in (Filter.cofinite : Filter ℕ), n < i := by
rw [Nat.cofinite_eq_atTop]
exact Filter.eventually_gt_atTop n
exact Filter.hyperfilter_le_cofinite h/-- The nonstandard element `[id]` of `ℕ^ℕ/U` is different from every standard element `[n]`:
a direct consequence of `nonstandard_natural`. So the diagonal embedding `ℕ → ℕ^ℕ/U`, which is
elementary by Łoś's theorem, is not surjective. -/
theorem nonstandard_ne_standard (n : ℕ) :
(↑(fun _ : ℕ => n) : (Filter.hyperfilter ℕ : Filter ℕ).Germ ℕ) ≠ ↑(id : ℕ → ℕ) :=
(nonstandard_natural n).neLefschetz and Ax–Grothendieck, from Mathlib, whose proof of Ax–Grothendieck follows exactly the route of this lesson (Mathlib/FieldTheory/AxGrothendieck.lean). At the version used here, Mathlib does not prove quantifier elimination for ACF; it obtains completeness of from uncountable categoricity and the Łoś–Vaught test instead.
/-- **The Lefschetz principle.** A first-order sentence in the language of rings follows from
the theory `ACF₀` of algebraically closed fields of characteristic `0` if and only if it follows
from `ACF_p` for infinitely many primes `p`. This applies Mathlib's
`FirstOrder.Field.ACF_zero_realize_iff_infinite_ACF_prime_realize`, proved from compactness and
the completeness of each `ACF_p`. -/
theorem lefschetz (φ : Language.ring.Sentence) :
Theory.ACF 0 ⊨ᵇ φ ↔ Set.Infinite {p : Nat.Primes | Theory.ACF p ⊨ᵇ φ} :=
Field.ACF_zero_realize_iff_infinite_ACF_prime_realize/-- **The Ax–Grothendieck theorem.** If `K` is an algebraically closed field and `ι` is finite,
every injective polynomial map `K^ι → K^ι` is surjective. This applies Mathlib's
`ax_grothendieck_univ`, which is proved by the model-theoretic argument of the lesson: the
statement for polynomials of bounded degree is a first-order sentence; it holds in the
algebraic closure of each finite field (a union of finite fields, where injective implies
surjective by counting); so it holds in every algebraically closed field by the Lefschetz
principle and completeness of `ACF_p`. -/
theorem ax_grothendieck {K ι : Type*} [Field K] [IsAlgClosed K] [Finite ι]
(p : ι → MvPolynomial ι K) :
Function.Injective (fun v i => MvPolynomial.eval v (p i)) →
Function.Surjective (fun v i => MvPolynomial.eval v (p i)) :=
ax_grothendieck_univ pWhat is not formalised here: Łoś's theorem and compactness are applied, not re-proved; quantifier elimination is formalised only in its key step; the Ehrenfeucht–Fraïssé theorem and the theory of types are not formalised in this lesson.
Games that measure what logic can see
The quantifier rank of a formula counts nested quantifiers: atomic formulas have rank , and take the maximum, and has rank one more than . Write if and satisfy the same sentences of rank at most .
In the Ehrenfeucht–Fraïssé game , Spoiler and Duplicator play rounds; in each round Spoiler picks an element of either structure and Duplicator answers with an element of the other. Duplicator wins if the chosen pairs form a partial isomorphism. The Ehrenfeucht–Fraïssé theorem (Fraïssé 1954, Ehrenfeucht 1961) says that, for a finite relational language, if and only if Duplicator has a winning strategy in . For finite linear orders and there is an exact rule: Duplicator wins iff or . Duplicator's strategy keeps the invariant that, with rounds left, corresponding gaps between chosen points (and the ends) either have equal length or both have length at least ; chapter 3 of Libkin's Elements of Finite Model Theory proves this kind of bound in detail.
The program below solves these games by brute-force search over the game tree and compares the answers with the rule.
"""Ehrenfeucht-Fraisse games on finite linear orders, solved exhaustively.
Two players play k rounds on the linear orders {0 < 1 < ... < m-1} and
{0 < 1 < ... < n-1}. In each round Spoiler picks an element of either order and
Duplicator answers with an element of the other; Duplicator wins if the chosen
pairs always form a partial isomorphism (same order and equality pattern). By
the Ehrenfeucht-Fraisse theorem, Duplicator has a winning strategy for k rounds
exactly when the two orders satisfy the same sentences of quantifier rank <= k.
The program solves every game in a finite range by brute-force search and
compares the result with the classical rule: Duplicator wins iff m = n or
both m, n >= 2^k - 1. Evidence: exact and exhaustive over the range searched.
"""
from functools import lru_cache
def sign(x):
"""Return -1, 0 or 1 according to the sign of x."""
return (x > 0) - (x < 0)
def consistent(pairs, new):
"""True if adding the pair ``new`` keeps ``pairs`` a partial isomorphism."""
a, b = new
return all(sign(a - c) == sign(b - d) for c, d in pairs)
def duplicator_wins(m, n, k):
"""Solve the k-round EF game on linear orders of sizes m and n exactly."""
if min(m, n, k) < 0:
raise ValueError("sizes and rounds must be non-negative")
@lru_cache(maxsize=None)
def win(rounds, pairs):
if rounds == 0:
return True
for side, size, other in ((0, m, n), (1, n, m)):
for x in range(size):
answers = ((x, y) if side == 0 else (y, x) for y in range(other))
if not any(consistent(pairs, p)
and win(rounds - 1, tuple(sorted(pairs + (p,))))
for p in answers):
return False
return True
return win(k, ())
def predicted(m, n, k):
"""The classical rule for linear orders: m = n, or both are >= 2^k - 1."""
return m == n or (m >= 2 ** k - 1 and n >= 2 ** k - 1)
def check_range(k, sizes):
"""Compare the search with the rule on all pairs m <= n from ``sizes``."""
pairs = [(m, n) for m in sizes for n in sizes if m <= n]
wrong = [(m, n) for m, n in pairs
if duplicator_wins(m, n, k) != predicted(m, n, k)]
return len(pairs), wrong
def smallest_equivalent_pair(k, limit):
"""Least m with sizes m and m + 1 indistinguishable in k rounds."""
for m in range(1, limit):
if duplicator_wins(m, m + 1, k):
return m
return None
def main():
print("k-round EF games on linear orders of sizes m <= n (D = Duplicator)")
for k, sizes in [(1, range(1, 9)), (2, range(1, 9)), (3, range(1, 11))]:
total, wrong = check_range(k, list(sizes))
least = smallest_equivalent_pair(k, 10)
print(f"k = {k}: {total} games solved, sizes {sizes.start}..{sizes.stop - 1}; "
f"disagreements with the 2^k - 1 rule: {len(wrong)}")
print(f" least m such that m, m + 1 are k-equivalent: {least} "
f"(rule predicts {2 ** k - 1})")
print("Row k = 2 (sizes 1..6), entry for (m, n):")
for m in range(1, 7):
row = " ".join("D" if duplicator_wins(m, n, 2) else "." for n in range(1, 7))
print(f" m = {m}: {row}")
print("So 'the order has an even number of elements' is not expressible by")
print("one first-order sentence: a sentence of rank k cannot tell 2^k - 1")
print("elements from 2^k (one even, one odd size).")
print()
print("Epistemic status: exact, exhaustive game-tree search for the listed")
print("sizes and k <= 3. It confirms the 2^k - 1 rule there and nowhere else;")
print("the rule for all m, n, k and the link between games and sentences")
print("(the Ehrenfeucht-Fraisse theorem) are theorems, cited in the lesson.")
if __name__ == "__main__":
main()A consequence: no first-order sentence in holds in exactly the finite linear orders of even size, because if has rank it cannot separate from . The search confirms the rule only for the sizes listed; the theorem covers all of them.
The second program implements the quantifier-elimination step of the DLO proof and tests it against a direct search for witnesses in — and then runs the same elimination in , where it fails.
"""Quantifier elimination for dense linear orders, tested against search.
A primitive formula Ex (conjunction of atoms a < x, x < b, x = c, with
parameters a, b, c) is replaced by a quantifier-free formula in the
parameters alone, following the lesson's proof: substitute if there is an
equation x = c; otherwise require every lower bound to be below every upper
bound. For random formulas and random rational parameters the program checks
the eliminated formula against a direct search for a witness x in Q, and then
shows the same elimination going wrong in the integers, which are not dense.
Evidence: exact rational arithmetic; random sampling, so it is a test, not a
proof.
"""
import random
from fractions import Fraction
def eliminate(atoms):
"""Quantifier-free equivalent of Ex /\\ atoms, as a list of (s, rel, t).
Atoms are ('lt', a, 'x'), ('lt', 'x', b) or ('eq', 'x', c); parameters are
names. The result is a conjunction of atoms between parameters.
"""
eqs = [c for kind, s, c in atoms if kind == "eq"]
if eqs:
c = eqs[0]
out = [("eq", c, d) for d in eqs[1:]]
out += [("lt", c if s == "x" else s, c if t == "x" else t)
for kind, s, t in atoms if kind == "lt"]
return list(dict.fromkeys(out))
lows = [s for kind, s, t in atoms if t == "x"]
highs = [t for kind, s, t in atoms if s == "x"]
return list(dict.fromkeys(("lt", a, b) for a in lows for b in highs))
def holds(formula, values):
"""Truth of a conjunction of parameter atoms under ``values``."""
rel = {"lt": lambda u, v: u < v, "eq": lambda u, v: u == v}
return all(rel[k](values[s], values[t]) for k, s, t in formula)
def candidates(values, integers=False):
"""Finitely many x that suffice to find a witness, if there is one."""
pts = sorted(set(values.values()))
if integers:
return range(int(min(pts)) - 1, int(max(pts)) + 2)
mids = [(p + q) / 2 for p, q in zip(pts, pts[1:])]
return pts + mids + [pts[0] - 1, pts[-1] + 1]
def witness_exists(atoms, values, integers=False):
"""Search directly for x making every atom true."""
def ok(x):
env = dict(values, x=x)
return holds(atoms, env)
return any(ok(x) for x in candidates(values, integers))
def random_case(rng, names, integers=False):
"""A random primitive formula and random parameter values."""
atoms = []
for _ in range(rng.randint(1, 4)):
p = rng.choice(names)
kind = rng.choice(["lt", "lt", "lt", "eq"])
atoms.append((kind, "x", p) if kind == "eq" or rng.random() < 0.5
else ("lt", p, "x"))
pick = (lambda: rng.randint(0, 3)) if integers else (
lambda: Fraction(rng.randint(0, 6), rng.randint(1, 3)))
return list(dict.fromkeys(atoms)), {p: pick() for p in names}
def compare(trials, integers=False, seed=2024):
"""Count agreements and collect disagreements on random cases."""
rng = random.Random(seed)
agree, bad = 0, []
for _ in range(trials):
atoms, values = random_case(rng, ["a", "b", "c"], integers)
if holds(eliminate(atoms), values) == witness_exists(
atoms, values, integers):
agree += 1
else:
bad.append((atoms, values))
return agree, bad
def show(formula):
"""Pretty-print a conjunction of atoms."""
sym = {"lt": "<", "eq": "="}
return " and ".join(f"{s} {sym[k]} {t}" for k, s, t in formula) or "true"
def main():
example = [("lt", "a", "x"), ("lt", "b", "x"), ("lt", "x", "c")]
print(f"Ex ({show(example)})")
print(f" is equivalent in a DLO to: {show(eliminate(example))}")
example2 = [("eq", "x", "c"), ("lt", "a", "x")]
print(f"Ex ({show(example2)}) ~> {show(eliminate(example2))}")
print()
agree, bad = compare(2000)
print(f"In Q: 2000 random formulas, {agree} agreements, {len(bad)} failures")
agree_z, bad_z = compare(2000, integers=True)
print(f"In Z: 2000 random formulas, {agree_z} agreements, "
f"{len(bad_z)} failures")
atoms, values = min(bad_z, key=lambda case: len(case[0]))
nice = ", ".join(f"{p} = {v}" for p, v in sorted(values.items()))
print(f" e.g. Ex ({show(atoms)}) with {nice}:")
print(f" the eliminated formula '{show(eliminate(atoms))}' is true,")
print(" but no integer x works - Z is not dense.")
print()
print("Epistemic status: exact arithmetic on 2000 random formulas per order.")
print("Agreement in Q is evidence that the elimination is implemented")
print("correctly, not a proof; the proof is the lesson's key lemma (also")
print("checked in Lean). The failures in Z are genuine counterexamples.")
if __name__ == "__main__":
main()Agreement on two thousand random formulas is evidence that the implementation is right, not a proof that the elimination is valid; the proof is the key lemma (checked in Lean). The failures in , by contrast, are genuine counterexamples: density is necessary.
A square of elementary embeddings
Diagonal embeddings into ultrapowers and the inclusion fit into a commutative square. Every arrow is an elementary embedding: the vertical ones by Łoś's theorem, and the horizontal ones because every embedding between models of is elementary.
Things to check for yourself: why is the bottom map well defined and injective (a rational sequence is a real sequence, and two sequences agree on a set in in iff they do in )? Why is a model of , so that the bottom map is elementary? And which complete 1-types over from the previous section does realise? (Answer: all of them — ultrapowers by non-principal ultrafilters on are -saturated, a theorem of Keisler.)
Exercises
1. Model theory
Warm-upHumanWhich pairs of structures (each in the language ) are elementarily equivalent? Choose all that apply.
2. Model theory
CoreComputationalUsing the rule for Ehrenfeucht–Fraïssé games on finite linear orders, what is the least such that the linear orders with and elements satisfy the same sentences of quantifier rank at most ?
3. Model theory
CoreHumanIn the inductive proof of Łoś's theorem, which step is the only one that uses that is an ultrafilter (and not merely a filter)?
4. Model theory
CoreHumanThe key lemma says: in a DLO, is equivalent to . In the equivalence fails for , . Which axiom of DLO does violate that the proof used here?
5. Model theory
StretchHumanTo show that is not equivalent in to any quantifier-free formula in , which pair of inputs works?
6. Model theory
CoreFormalWith orderStructure ℚ and orderStructure ℝ as the structures, which Lean statement correctly formalises “ and are elementarily equivalent but not isomorphic”?
7. Model theory
CoreHumanThe Ax–Grothendieck theorem says injective polynomial maps are surjective when is algebraically closed. Which of these are counterexamples over a field that is not algebraically closed? Choose all that apply.
8. Model theory
Warm-upHumanHow many complete -types over does the theory DLO have? (Use quantifier elimination: a type in variables is determined by the atomic formulas it contains.)
9. Model theory
StretchComputationalMake the key lemma constructive in . Write witness(lows, highs) that takes two lists of Fractions and returns a Fraction with for every in lows and for every in highs, or None if no such exists. Follow the four cases of the proof.
Common misconceptions
Misconception
"Elementarily equivalent structures must be isomorphic." and are elementarily equivalent and not isomorphic. By the Löwenheim–Skolem theorems, every infinite structure in a countable language has elementarily equivalent structures of every infinite cardinality, so elementary equivalence can never pin down an infinite structure up to isomorphism. Only for finite structures does imply .
Misconception
"A theory with many models is vague." Having non-isomorphic models of different cardinalities is forced on every first-order theory with an infinite model. It does not mean the theory fails to decide questions: DLO and have models of every infinite cardinality and are complete. Completeness (deciding every sentence) and categoricity (one model per cardinality) are different properties.
Misconception
" and imply ." Elementarity involves formulas with parameters. is isomorphic to , but is least in the substructure and not in .
Misconception
"Łoś's theorem needs a non-principal ultrafilter." It holds for every ultrafilter. For a principal ultrafilter at the ultraproduct is just (isomorphic to) , so the theorem is true but uninteresting; non-principal ultrafilters are needed to produce new structures such as the hyperreals.
Misconception
"Quantifier elimination means the theory is decidable." QE reduces sentences to quantifier-free sentences. Decidability also needs the elimination to be effective, the axioms to be computably enumerable, and the quantifier-free sentences to be decidable — true for DLO, ACF and real closed fields, but not automatic.
Many ways of seeing
Algebra— Nullstellensatz and constructible sets
For algebraically closed fields, quantifier elimination is algebra. Abraham Robinson's proof of the weak Nullstellensatz runs as follows. Let be algebraically closed and a proper ideal, generated (by the Hilbert basis theorem) by . Choose a maximal ideal and let be an algebraic closure of . In the sentence , with parameters from , is true (take the images of the ). The embedding is elementary (model completeness of ACF, from QE), so the sentence is true in : the variety has a -point. Projections of constructible sets are constructible (Chevalley) — the same theorem seen from the geometric side. See algebraic geometry and fields and Galois theory.
Geometry— Tame geometry: o-minimality
An expansion of a dense linear order is o-minimal if every definable subset of (with parameters) is a finite union of points and open intervals. By Tarski's quantifier elimination for real closed fields, is o-minimal, and its definable sets are the semialgebraic sets; Wilkie (1996) proved that is o-minimal too. The cell decomposition theorem (Pillay–Steinhorn, Knight–Pillay–Steinhorn, 1986) says that in an o-minimal structure every definable subset of is a finite disjoint union of "cells" (graphs and bands between definable continuous functions, built up coordinate by coordinate). Consequences: definable sets have finitely many connected components, a well-behaved dimension, and no pathologies such as space-filling curves. This is the "tame topology" Grothendieck asked for, developed in van den Dries's book; o-minimal counting (Pila–Wilkie, 2006) became a key tool in Pila's 2011 proof of the André–Oort conjecture for products of modular curves.
Number theory— Ax–Kochen: p-adic fields through ultraproducts
Artin conjectured that every homogeneous form of degree over in more than variables has a nontrivial zero. Over the analogue is a theorem (Lang, 1952: is a field). Ax and Kochen (1965), and independently Ershov, proved that for a non-principal ultrafilter on the primes, as valued fields. For each fixed , "every form of degree in variables has a nontrivial zero" is a single first-order sentence; it holds in every , hence in the second ultraproduct, hence in the first, hence (Łoś) in for -almost all — and since was arbitrary, for all but finitely many primes . The finite exceptional set depends on , and the original argument gives no bound for it. The exceptions are real: Terjanian (1966) found a quartic form in variables over with no nontrivial zero, so Artin's conjecture is false as stated. See algebraic number theory.
Analysis— Nonstandard analysis
Robinson (1961, book 1966) used ultrapowers to make infinitesimals rigorous. In there is smaller than every positive real. Every function extends to coordinatewise, and the transfer principle — Łoś's theorem for expanded by symbols for all the functions and relations one cares about — says a first-order statement about these holds in iff it holds in . One then proves, for standard and : is continuous at iff is infinitesimal for every infinitesimal . Derivatives become standard parts of difference quotients with infinitesimal increments. The method has produced new results — for instance Bernstein and Robinson's 1966 invariant subspace theorem for polynomially compact operators (later given a standard proof by Halmos). Compare with – real analysis: the two are interchangeable by transfer, but the nonstandard proofs are often shorter.
Topology— Compactness is compactness
The compactness theorem is literally the compactness of a space. The Lindenbaum algebra of formulas in variables modulo -equivalence is a Boolean algebra, and its Stone space — the space of ultrafilters of — is , a compact, Hausdorff, totally disconnected space. Isolated points are the principal types. The omitting types theorem (for countable languages: a countable complete theory has a countable model omitting any given non-isolated type) is proved with the Baire category theorem, and countable models of countable theories are studied through the topology of — for example, a complete theory in a countable language with infinite models is -categorical iff every is finite (Engeler, Ryll-Nardzewski, Svenonius, 1959). See point-set topology.
Category theory— Ultraproducts as colimits
An ultraproduct is a colimit of products: , the colimit taken over ordered by reverse inclusion along the restriction maps. Since is closed under intersections, this is a filtered colimit. That much works for any filter and produces the reduced product, which preserves Horn sentences such as the axioms of groups and rings; maximality of is what lets negations, and hence all first-order sentences, pass through. The pair is a Galois connection — an adjunction between posets — and categorical logic generalises models from structures in to structures in other categories (see categorical logic).
History and global context
- Foundations (1915–1936). Leopold Löwenheim (1915) and Thoralf Skolem (1920, 1922) proved the first downward Löwenheim–Skolem theorems. Kurt Gödel's completeness theorem (1929 thesis, published 1930) included compactness for countable languages; Anatolii Malcev extended compactness to arbitrary languages (1936) and applied it to group theory (1941). Alfred Tarski gave the mathematical definition of truth in a structure (1933).
- Quantifier elimination (1930s–1950s). Tarski found quantifier elimination for real closed fields around 1930; his decision method was published by the RAND Corporation in 1948 (revised 1951). Abraham Robinson introduced model completeness and applied it to algebra in the 1950s. Robert Vaught and Jerzy Łoś independently found the Łoś–Vaught test (1954).
- Ultraproducts and games. Jerzy Łoś proved his theorem in 1955 in Poland; ultraproducts became a central tool through work of Frayne, Morel, Scott, Keisler and Kochen. Roland Fraïssé (1954) and Andrzej Ehrenfeucht (1961) introduced back-and-forth systems and games.
- Applications (1960s). Robinson created nonstandard analysis (1961; book 1966). James Ax and Simon Kochen (1965), and independently Yuri Ershov in Novosibirsk, proved the transfer theorem for -adic fields. Ax proved the Ax–Grothendieck theorem by model theory (1968–69); Alexander Grothendieck proved it independently in EGA IV (1966).
- Stability and beyond. Michael Morley's categoricity theorem (1965) — a countable theory categorical in one uncountable cardinal is categorical in all — launched Saharon Shelah's classification theory (book 1978). Later geometric model theory, through Boris Zilber, Ehud Hrushovski (whose 1996 proof of the function-field Mordell–Lang conjecture used model theory), Anand Pillay and many others, made model theory a working tool in algebraic and arithmetic geometry.
Further reading
- David Marker, Model Theory: An Introduction (2002) — the standard graduate text; QE for ACF and DLO in chapter 3, ultraproducts in chapter 2 exercises and in many applications.
- Katrin Tent and Martin Ziegler, A Course in Model Theory (2012) — concise and modern.
- Wilfrid Hodges, Model Theory (1993), and its abridgement A Shorter Model Theory (1997).
- C. C. Chang and H. J. Keisler, Model Theory (1973; 3rd edition 1990) — the classic reference, especially for ultraproducts.
- Lou van den Dries, Tame Topology and o-minimal Structures (1998).
- Leonid Libkin, Elements of Finite Model Theory (2004) — Ehrenfeucht–Fraïssé games and their limits.
- Robert Goldblatt, Lectures on the Hyperreals (1998) — nonstandard analysis via ultrapowers.
Next steps in the Lumenary: forcing and independence, which builds new models of set theory; Gödel's incompleteness theorems; and algebraic geometry.