/- Copyright (c) 2026 Mathias Stout. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Crighton, John Nicholson, Mathias Stout -/ module public import MultisortedLogic.PartialMaps public import MultisortedLogic.Separation public import Mathlib.Order.Zorn /-! # The embedding test To show that `T` eliminates quantifiers relative to `R`, it suffices that every partial map preserving the formulas quantifier-free relative to `R`, defined on a proper subset of a model of `T`, extends to some new element after passing to an elementary extension of the target. An extension of a partial map `f : A → N` to `C ⊇ A` is a model of the set of sentences `extDiagram f C`, with parameters in `N ⊕ M`: the elementary diagram of `N`, the `Δ`-diagram of `C` in `M`, and the equations `a = f a`. This set grows with `C`, and its satisfiability passes to unions of chains by compactness, which gives Zorn's lemma its upper bounds. Two extensions come for free, so the extension property need only be checked on substructures, and on substructures containing every element of every sort of `R`. * A partial map extends to the substructure generated by its domain (`PartialMap.closure`, in `PartialMaps.lean`). * It extends to any element of a sort of `R` (`PartialMap.extendsTo_insertAt_of_mem`), since an existential over a sort of `R` keeps a formula quantifier-free relative to `R`. ## Main definitions * `PartialMap.ExtendsTo g D h`: the partial map `g` extends to `D`, which contains its domain by `h`, after passing to an elementary extension of its target. * `extDiagram f C`: the sentences whose models are the extensions of `f` to `C`. ## Main results * `PartialMap.extendsTo_iff_isSatisfiable`: `f` extends to `C` iff `extDiagram f C` is satisfiable. * `PartialMap.extendsTo_top`: under the one-step extension hypothesis, `f` extends to all of `M`. * `Theory.hasQERelTo_of_exists_extension`: the embedding test. * `Theory.hasQERelTo_of_exists_extension_closed`, `hasQERelTo_of_exists_extension_substructure`, `hasQERelTo_of_exists_extension_substructure_of_rel`: the embedding test on substructures. * `Theory.hasQERelTo_of_substructure_extension`, `hasQERelTo_of_subset_extension`: the extension criteria, in which every partial map extends to every element of a sort outside `R`. -/ @[expose] public section universe u v w w' z namespace ManySorted.Language open Arity Structure FormulaClass variable {S : Type z} {L : Language.{u, v} S} variable {M : S → Type w} {N : S → Type w'} [L.Structure M] [L.Structure N] {Δ : L.FormulaClass} /-! ### Extensions of partial maps -/ /-- The partial map `g` on `C` extends to `D`, which contains `C` by `h`, after passing to an elementary extension of the target: there are an elementary extension `j : N ↪ₑ[L] P` and a partial map `g'` from `D` to `P` with `g' a = j (g a)` for `a` in `C`. -/ def PartialMap.ExtendsTo {C : ∀ s, Set (M s)} (g : PartialMap Δ C N) (D : ∀ s, Set (M s)) (h : C ≤ D) : Prop := ∃ (P : S → Type (max u v z)) (_ : L.Structure P) (_ : ∀ s, Nonempty (P s)) (j : N ↪ₑ[L] P) (g' : PartialMap Δ D P), ∀ s (a : C s), g' s ⟨a.1, h s a.2⟩ = j s (g s a) /-! ### Extension diagrams -/ variable (Δ) in /-- The `Δ`-diagram of `C ⊆ M`, with parameters in `N ⊕ M`: every formula of `Δ` true of a tuple from `C`, with its positions named by that tuple. -/ def deltaDiagram (C : ∀ s, Set (M s)) : L[[fun s => N s ⊕ M s]].Theory := {ψ | ∃ (σ : Arity S) (φ : L.Formula σ) (x : (fun s => ↥(C s)) [^] σ), φ ∈ Δ σ ∧ φ.Realize (x.map fun _ a => a.1) ∧ ψ = φ.ofNames fun s i => .inr (x.get s i).1} variable {A : ∀ s, Set (M s)} /-- The equations `a = f a` between the parameter for `a ∈ A` and the parameter for its image. -/ def agreeDiagram (f : PartialMap Δ A N) : L[[fun s => N s ⊕ M s]].Theory := {ψ | ∃ (s : S) (a : A s), ψ = (Term.con L (.inr a.1) =' Term.con L (.inl (f s a)))} /-- The sentences whose models are the extensions of `f` to `C` into elementary extensions of `N`. -/ def extDiagram (f : PartialMap Δ A N) (C : ∀ s, Set (M s)) : L[[fun s => N s ⊕ M s]].Theory := Formula.mapParams (fun _ => .inl) '' elementaryDiagram L N ∪ deltaDiagram Δ C ∪ agreeDiagram f omit [L.Structure N] in theorem mem_deltaDiagram {C : ∀ s, Set (M s)} {ψ : L[[fun s => N s ⊕ M s]].Sentence} : ψ ∈ deltaDiagram Δ C ↔ ∃ (σ : Arity S) (φ : L.Formula σ) (x : (fun s => ↥(C s)) [^] σ), φ ∈ Δ σ ∧ φ.Realize (x.map fun _ a => a.1) ∧ ψ = φ.ofNames fun s i => .inr (x.get s i).1 := Iff.rfl theorem mem_agreeDiagram {f : PartialMap Δ A N} {ψ : L[[fun s => N s ⊕ M s]].Sentence} : ψ ∈ agreeDiagram f ↔ ∃ (s : S) (a : A s), ψ = (Term.con L (.inr a.1) =' Term.con L (.inl (f s a))) := Iff.rfl theorem mem_extDiagram {f : PartialMap Δ A N} {C : ∀ s, Set (M s)} {ψ : L[[fun s => N s ⊕ M s]].Sentence} : ψ ∈ extDiagram f C ↔ (ψ ∈ Formula.mapParams (fun _ => .inl) '' elementaryDiagram L N ∨ ψ ∈ deltaDiagram Δ C) ∨ ψ ∈ agreeDiagram f := Iff.rfl omit [L.Structure N] in theorem deltaDiagram_mono {C C' : ∀ s, Set (M s)} (h : C ≤ C') : deltaDiagram (N := N) Δ C ⊆ deltaDiagram Δ C' := by rintro ψ ⟨σ, φ, x, hφ, hx, rfl⟩ refine ⟨σ, φ, x.map fun s c => ⟨c.1, Set.mem_of_mem_of_subset c.2 (h s)⟩, hφ, ?_, ?_⟩ · rw [Interpret.map_map] exact hx · simp [Interpret.get_map] omit [L.Structure N] in theorem top_mem_deltaDiagram (h : ⊤ ∈ Δ ⦃⦄) (C : ∀ s, Set (M s)) : ⊤ ∈ deltaDiagram (N := N) Δ C := ⟨⦃⦄, ⊤, PUnit.unit, h, Formula.realize_top.2 trivial, (Formula.ofNames_top _).symm⟩ omit [L.Structure N] in /-- The `Δ`-diagram is closed under conjunction when `Δ` is closed under conjunction and relabelling: the conjunction of `φ₁(x₁)` and `φ₂(x₂)` is `(φ₁ ⊓ φ₂)(x₁, x₂)`. -/ theorem inf_mem_deltaDiagram (hinf : ∀ {σ : Arity S} {φ ψ : L.Formula σ}, φ ∈ Δ σ → ψ ∈ Δ σ → φ ⊓ ψ ∈ Δ σ) (hrel : ∀ {σ τ : Arity S} (g : ∀ s, σ.Idx s → τ.Idx s) {φ : L.Formula σ}, φ ∈ Δ σ → φ.relabel g ∈ Δ τ) {C : ∀ s, Set (M s)} {ψ₁ ψ₂ : L[[fun s => N s ⊕ M s]].Sentence} (h₁ : ψ₁ ∈ deltaDiagram Δ C) (h₂ : ψ₂ ∈ deltaDiagram Δ C) : ψ₁ ⊓ ψ₂ ∈ deltaDiagram Δ C := by obtain ⟨σ₁, φ₁, x₁, hφ₁, hx₁, rfl⟩ := h₁ obtain ⟨σ₂, φ₂, x₂, hφ₂, hx₂, rfl⟩ := h₂ refine ⟨σ₁ ⨯ σ₂, (φ₁.relabel fun _ => .left) ⊓ (φ₂.relabel fun _ => .right), (x₁, x₂), hinf (hrel _ hφ₁) (hrel _ hφ₂), ?_, ?_⟩ · simp only [Formula.realize_inf, Formula.realize_relabel, Interpret.map_mk, Interpret.comap_left, Interpret.comap_right] exact ⟨hx₁, hx₂⟩ · rw [Formula.ofNames_inf, Formula.ofNames_relabel, Formula.ofNames_relabel] rfl omit [L.Structure N] in /-- The `Δ`-diagram contains the conjunction of finitely many of its members, when `Δ` is closed under finite conjunctions and relabelling. -/ theorem iInf_mem_deltaDiagram (hΔ : ∀ σ, IsFragment (Δ σ)) (hrel : ∀ {σ τ : Arity S} (g : ∀ s, σ.Idx s → τ.Idx s) {φ : L.Formula σ}, φ ∈ Δ σ → φ.relabel g ∈ Δ τ) {C : ∀ s, Set (M s)} (F : Finset (L[[fun s => N s ⊕ M s]].Sentence)) (hF : ↑F ⊆ deltaDiagram (N := N) Δ C) : Formula.iInf F id ∈ deltaDiagram Δ C := by suffices ∀ l : List (L[[fun s => N s ⊕ M s]].Sentence), (∀ ψ ∈ l, ψ ∈ deltaDiagram Δ C) → l.foldr (· ⊓ ·) ⊤ ∈ deltaDiagram Δ C from this _ fun ψ hψ => hF (by simpa using hψ) intro l hl induction l with | nil => exact top_mem_deltaDiagram (hΔ _).top_mem C | cons ψ l ih => exact inf_mem_deltaDiagram (fun h₁ h₂ => (hΔ _).inf_mem h₁ h₂) hrel (hl ψ (by simp)) (ih fun χ hχ => hl χ (by simp [hχ])) theorem extDiagram_mono (f : PartialMap Δ A N) {C C' : ∀ s, Set (M s)} (h : C ≤ C') : extDiagram f C ⊆ extDiagram f C' := Set.union_subset_union_left _ (Set.union_subset_union_right _ (deltaDiagram_mono h)) /-- A tuple from the union of a nonempty chain already lies in one member of the chain. -/ theorem exists_mem_chain {c : Set (∀ s, Set (M s))} (hc : IsChain (· ≤ ·) c) (hne : c.Nonempty) {σ : Arity S} (x : (fun s => ↥(sSup c s)) [^] σ) : ∃ C ∈ c, ∀ s i, (x.get s i).1 ∈ C s := by have : Nonempty c := hne.to_subtype obtain ⟨⟨C, hC⟩, h⟩ := Interpret.exists_forall_mem_of_directed (X := fun C : c => C.1) hc.directedOn.directed_val (x := x.map fun _ a => a.1) fun s i => by -- Simplify membership for a bare element: `simp` on `(x.get s i).2` would also rewrite -- `sSup c s` inside the type of `x`. have key : ∀ m ∈ sSup c s, ∃ C ∈ c, m ∈ C s := fun m hm => by simpa using hm obtain ⟨C, hC, hx⟩ := key _ (x.get s i).2 exact ⟨⟨C, hC⟩, by rw [Interpret.get_map]; exact hx⟩ exact ⟨C, hC, fun s i => by have := h s i rwa [Interpret.get_map] at this⟩ omit [L.Structure N] in /-- Every formula in the `Δ`-diagram of the union of a nonempty chain already belongs to the `Δ`-diagram of one member. -/ theorem deltaDiagram_sSup_subset {c : Set (∀ s, Set (M s))} (hc : IsChain (· ≤ ·) c) (hne : c.Nonempty) : deltaDiagram (N := N) Δ (sSup c) ⊆ ⋃ C ∈ c, deltaDiagram Δ C := by rintro ψ ⟨σ, φ, x, hφ, hx, rfl⟩ obtain ⟨C, hC, hxC⟩ := exists_mem_chain hc hne x let x' : (fun s => ↥(C s)) [^] σ := Interpret.ofFn fun s i => ⟨(x.get s i).1, hxC s i⟩ have hval : x'.map (fun _ a => a.1) = x.map fun _ a => a.1 := Interpret.ext fun s i => by rw [Interpret.get_map, Interpret.get_map]; simp [x'] refine Set.mem_iUnion₂.2 ⟨C, hC, σ, φ, x', hφ, hval ▸ hx, ?_⟩ simp [x'] /-- Satisfiability of `extDiagram` passes to the union of a nonempty chain. -/ theorem isSatisfiable_extDiagram_sSup (f : PartialMap Δ A N) {c : Set (∀ s, Set (M s))} (hc : IsChain (· ≤ ·) c) (hne : c.Nonempty) (hsat : ∀ C ∈ c, (extDiagram f C).IsSatisfiable) : (extDiagram f (sSup c)).IsSatisfiable := by obtain ⟨C₀, hC₀⟩ := hne have : Nonempty c := ⟨⟨C₀, hC₀⟩⟩ have hsub : extDiagram f (sSup c) ⊆ ⋃ C : c, extDiagram f C := by rintro ψ ((hψ | hψ) | hψ) · exact Set.mem_iUnion.2 ⟨⟨C₀, hC₀⟩, Or.inl (Or.inl hψ)⟩ · obtain ⟨C, hC, hψC⟩ := Set.mem_iUnion₂.1 (deltaDiagram_sSup_subset hc ⟨C₀, hC₀⟩ hψ) exact Set.mem_iUnion.2 ⟨⟨C, hC⟩, Or.inl (Or.inr hψC)⟩ · exact Set.mem_iUnion.2 ⟨⟨C₀, hC₀⟩, Or.inr hψ⟩ have hdir : Directed (· ⊆ ·) fun C : c => extDiagram f C := fun a b => by rcases hc.total a.2 b.2 with h | h · exact ⟨b, extDiagram_mono f h, subset_rfl⟩ · exact ⟨a, subset_rfl, extDiagram_mono f h⟩ exact (FormulaSet.isSatisfiable_iUnion hdir fun C => hsat C C.2).mono hsub section Models variable {M N : S → Type (max u v z)} [L.Structure M] [L.Structure N] {A : ∀ s, Set (M s)} /-- **Extensions are models of the extension diagram.** A partial map `f` extends to `C`, after passing to an elementary extension of `N`, iff `extDiagram f C` is satisfiable. -/ theorem PartialMap.extendsTo_iff_isSatisfiable (f : PartialMap Δ A N) {C : ∀ s, Set (M s)} (hAC : A ≤ C) : f.ExtendsTo C hAC ↔ (extDiagram f C).IsSatisfiable := by classical constructor · rintro ⟨P, _, _, j, g, hg⟩ let w : ∀ s, M s → P s := fun s m => if h : m ∈ C s then g s ⟨m, h⟩ else Classical.arbitrary _ have hw : ∀ s (c : C s), w s c.1 = g s c := fun s c => by simp [w, c.2] refine ⟨@FormulaSet.Realization.mk _ _ _ _ P (withConstantsStructure fun s => Sum.elim (j s) (w s)) inferInstance PUnit.unit ?_⟩ rintro ψ ((⟨φ, hφ, rfl⟩ | ⟨σ, φ, x, hφ, hx, rfl⟩) | ⟨s, a, rfl⟩) · exact (Formula.realize_mapParams _ _ φ _).2 (j.realize_elementaryDiagram φ hφ) · change (Formula.ofNames _ _).RealizeWith _ _ rw [Formula.realize_ofNames] convert g.map_formula hφ hx using 1 exact Interpret.ext fun s i => by simp [Interpret.get_map, Interpret.get_ofFn, hw] · change Formula.RealizeWith (Term.con L _ =' Term.con L _) _ _ simp only [Formula.RealizeWith, Formula.realize_equal, Term.realize_con, Sum.elim_inl, Sum.elim_inr] exact (hw s ⟨a.1, hAC s a.2⟩).trans (hg s a) · rintro ⟨r⟩ let : L.Structure r.carrier := (L.lhomWithConstants fun s => N s ⊕ M s).reduct r.carrier have hreal := r.realize rw [← withConstantsStructure_reduct r.str] at hreal set v := constants (L := L) (M := r.carrier) (α := fun s => N s ⊕ M s) have hreal : (extDiagram f C).RealizeWith v PUnit.unit := hreal have hdiag : (elementaryDiagram L N).RealizeWith (fun s a => v s (.inl a)) PUnit.unit := fun ψ hψ => (Formula.realize_mapParams _ v ψ _).1 (hreal.realize_of_mem (mem_extDiagram.2 (.inl (.inl (Set.mem_image_of_mem _ hψ))))) refine ⟨r.carrier, inferInstance, inferInstance, .ofElementaryDiagram hdiag, ⟨fun s c => v s (.inr c.1), fun {σ} φ hφ x hx => ?_⟩, fun s a => ?_⟩ · have := (Formula.realize_ofNames v φ _).1 (hreal.realize_of_mem (mem_extDiagram.2 (.inl (.inr (mem_deltaDiagram.2 ⟨σ, φ, x, hφ, hx, rfl⟩))))) convert this using 1 exact Interpret.ext fun s i => by simp [Interpret.get_map, Interpret.get_ofFn] · have := hreal.realize_of_mem (mem_extDiagram.2 (.inr (mem_agreeDiagram.2 ⟨s, a, rfl⟩))) simp only [Formula.RealizeWith, Formula.realize_equal, Term.realize_con] at this exact this /-! ### The Zorn step -/ /-- **Zorn step.** If every partial `Δ`-map on a proper subset of a model of `T` extends to some new element, into an elementary extension, then every partial `Δ`-map `f : A → N` extends to all of `M`. Implementation note: the element that witnesses the proper inclusion is not necessarily the element we extend `A` by. -/ theorem PartialMap.extendsTo_top (T : L.Theory) (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (C : ∀ s, Set (M s)) (g : PartialMap Δ C N), (∃ s, ∃ x : M s, x ∉ C s) → ∃ (s : S) (b : M s), b ∉ C s ∧ g.ExtendsTo (insertAt C s b) (le_insertAt C s b)) [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (f : PartialMap Δ A N) : f.ExtendsTo ⊤ le_top := by classical let SS : Set (∀ s, Set (M s)) := {C | A ≤ C ∧ (extDiagram f C).IsSatisfiable} have hA : A ∈ SS := ⟨le_rfl, (f.extendsTo_iff_isSatisfiable le_rfl).1 ⟨N, inferInstance, inferInstance, .refl L N, f, fun _ _ => rfl⟩⟩ obtain ⟨C, -, hmax⟩ := zorn_le_nonempty₀ SS (fun c hcS hc y hy => ⟨sSup c, ⟨(hcS hy).1.trans (le_sSup hy), isSatisfiable_extDiagram_sSup f hc ⟨y, hy⟩ fun C hC => (hcS hC).2⟩, fun _ hz => le_sSup hz⟩) A hA obtain ⟨hAC, hCsat⟩ := hmax.prop have htop : ⊤ ≤ C := by by_contra hne obtain ⟨P, _, _, j, g, hg⟩ := (f.extendsTo_iff_isSatisfiable hAC).2 hCsat have : P ⊨ T := j.theory_model obtain ⟨s, b, hb, P', _, _, j', g', hg'⟩ := h_ext M P C g (by by_contra hall push Not at hall exact hne fun s x _ => hall s x) have hins : insertAt C s b ∈ SS := ⟨hAC.trans (le_insertAt C s b), (f.extendsTo_iff_isSatisfiable (hAC.trans (le_insertAt C s b))).1 ⟨P', inferInstance, inferInstance, j'.comp j, g', fun s' a => (hg' s' ⟨a.1, hAC s' a.2⟩).trans (congrArg (j' s') (hg s' a))⟩⟩ exact hb ((hmax.eq_of_le hins (le_insertAt C s b)) ▸ mem_insertAt_self C s b) obtain rfl : C = ⊤ := le_antisymm le_top htop exact (f.extendsTo_iff_isSatisfiable hAC).2 hCsat end Models /-! ### Extension along the sorts of `R` -/ section ExtendAlongR variable {M : S → Type w} {R : Set S} {C : ∀ s, Set (M s)} {s : S} {b : M s} set_option linter.tacticCheckInstances true /-- A tuple from `C ∪ {b}` is a tuple from `C` next to `b`, relabelled. -/ theorem exists_eq_comap_of_insertAt : ∀ {σ : Arity S} (x : (fun t => ↥(insertAt C s b t)) [^] σ), ∃ (Γ : Arity S) (c : (fun t => ↥(C t)) [^] Γ) (pos : ∀ t, σ.Idx t → (Γ ⨯ ⦃s⦄).Idx t), x = Interpret.comap (σ := σ) (τ := Γ ⨯ ⦃s⦄) pos (c.map fun _ a => ⟨a.1, mem_insertAt_of_mem a.2⟩, ⟨b, mem_insertAt_self C s b⟩) | .nil, _ => ⟨⦃⦄, PUnit.unit, (fun _ i => nomatch i), rfl⟩ | .of t, ⟨x, hx⟩ => by by_cases hxC : x ∈ C t · exact ⟨⦃t⦄, ⟨x, hxC⟩, fun _ i => .left i, rfl⟩ · rw [mem_insertAt] at hx obtain hx | hx := hx · exact absurd hx hxC · cases hx exact ⟨⦃⦄, PUnit.unit, fun _ i => .right i, rfl⟩ | .prod _ _, x => by obtain ⟨Γ₁, c₁, pos₁, h₁⟩ := exists_eq_comap_of_insertAt x.1 obtain ⟨Γ₂, c₂, pos₂, h₂⟩ := exists_eq_comap_of_insertAt x.2 refine ⟨Γ₁ ⨯ Γ₂, (c₁, c₂), Idx.elim (fun t i => Idx.extendRight (fun _ => .left) t (pos₁ t i)) (fun t i => Idx.extendRight (fun _ => .right) t (pos₂ t i)), Prod.ext ?_ ?_⟩ · refine h₁.trans (Interpret.ext fun t i => ?_) rw [← Interpret.get_left, Interpret.get_comap, Interpret.get_comap, Idx.elim_left] generalize pos₁ t i = k cases k with | left j => rfl | right k => cases k; rfl · refine h₂.trans (Interpret.ext fun t i => ?_) rw [← Interpret.get_right, Interpret.get_comap, Interpret.get_comap, Idx.elim_right] generalize pos₂ t i = k cases k with | left j => rfl | right k => cases k; rfl variable {M N : S → Type (max u v z)} [L.Structure M] [L.Structure N] {C : ∀ s, Set (M s)} /-- **Extension along the sorts of `R`**: a partial map for the formulas quantifier-free relative to `R` extends to any element `b` of a sort of `R`, after passing to an elementary extension. The facts about `b` combine into one formula `∃ y, θ(c, y)` over `C`, which is still quantifier-free relative to `R`, and so transfers along `g`. -/ theorem PartialMap.extendsTo_insertAt_of_mem [∀ s, Nonempty (N s)] (g : PartialMap (qfRelTo L R) C N) {s : S} (hs : s ∈ R) (b : M s) : g.ExtendsTo (insertAt C s b) (le_insertAt C s b) := by classical by_cases hb : b ∈ C s · have hle : insertAt C s b ≤ C := fun t x hx => by rw [mem_insertAt] at hx exact hx.elim id fun h => by cases h; exact hb exact ⟨N, inferInstance, inferInstance, .refl L N, g.restrict hle, fun _ _ => rfl⟩ refine (g.extendsTo_iff_isSatisfiable (le_insertAt C s b)).2 (FormulaSet.IsFinitelySatisfiable.isSatisfiable fun F hF => ?_) -- The facts of `F` about `C ∪ {b}` have a conjunction, itself a fact `φ(x)`; split `x` into `c` -- from `C` and `b`. set F' := F.filter (· ∈ deltaDiagram (N := N) (qfRelTo L R) (insertAt C s b)) obtain ⟨σ, φ, x, hφ, hx, hF'⟩ := iInf_mem_deltaDiagram (fun _ => Formula.isFragment_qfRelTo R) (fun g _ h => Formula.IsQFRelTo.relabel g h) F' fun ψ hψ => (Finset.mem_filter.1 hψ).2 obtain ⟨Γ, c, pos, hxc⟩ := exists_eq_comap_of_insertAt x have hmap : ∀ {P : S → Type (max u v z)} (H : ∀ t, insertAt C s b t → P t), x.map H = Interpret.comap (σ := σ) (τ := Γ ⨯ ⦃s⦄) pos (c.map fun t a => H t ⟨a.1, mem_insertAt_of_mem a.2⟩, H s ⟨b, mem_insertAt_self C s b⟩) := fun H => hxc ▸ Interpret.ext fun t i => by simp only [Interpret.get_map, Interpret.get_comap] generalize pos t i = k cases k with | left j => simp [Interpret.get_map] | right k => cases k; rfl -- `∃ y, φ(c, y)` holds in `M`, so in `N` at `g c`. have hΘ := g.map_formula (x := c) ((hφ.relabel pos).ex (.of hs)) (Formula.realize_ex.2 ⟨b, by rw [Formula.realize_relabel]; simpa [hmap] using hx⟩) obtain ⟨y, hy⟩ := Formula.realize_ex.1 hΘ rw [Formula.realize_relabel] at hy -- The values: `g` on `C`, and `y` for `b`. let dflt : ∀ t, N t := Function.update (fun t => Classical.arbitrary (N t)) s y let h : ∀ t, M t → N t := fun t m => if hm : m ∈ C t then g t ⟨m, hm⟩ else dflt t refine ⟨@FormulaSet.Realization.mk _ _ _ _ N (withConstantsStructure fun t => Sum.elim id (h t)) inferInstance PUnit.unit fun ψ hψ => ?_⟩ rcases hF hψ with ((⟨χ, hχ, rfl⟩ | hψD) | ⟨t, a, rfl⟩) · exact (Formula.realize_mapParams _ _ χ _).2 ((ElementaryEmbedding.refl L N).realize_elementaryDiagram χ hχ) · -- `ψ` is a conjunct of `φ(x)`, which holds since `φ(c, y)` does. refine (Formula.realizeWith_iInf _ F' id _).1 ?_ ψ (Finset.mem_filter.2 ⟨hψ, hψD⟩) rw [hF', Formula.realize_ofNames] convert hy using 1 refine (Interpret.ext fun t i => ?_ : _ = x.map fun t a => h t a.1).trans ?_ · simp [Interpret.get_map] rw [hmap] congr 1 refine Prod.ext (Interpret.ext fun t i => ?_) ?_ · simp [Interpret.get_map, h] · simp [h, hb, dflt] · change Formula.RealizeWith (Term.con L _ =' Term.con L _) _ _ simp [Formula.RealizeWith, Formula.realize_equal, Term.realize_con, h, a.2] end ExtendAlongR /-! ### The embedding test -/ namespace Theory variable {T : L.Theory} {R : Set S} /-- **The embedding test.** `T` has quantifier elimination relative to `R` if every partial map preserving the formulas quantifier-free relative to `R`, defined on a proper subset of a model of `T`, extends to some new element after passing to an elementary extension of the target. -/ theorem hasQERelTo_of_exists_extension (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (C : ∀ s, Set (M s)) (g : PartialMap (qfRelTo L R) C N), (∃ s, ∃ x : M s, x ∉ C s) → ∃ (s : S) (b : M s), b ∉ C s ∧ g.ExtendsTo (insertAt C s b) (le_insertAt C s b)) : T.HasQERelTo R := by refine hasQERelTo_of_tuple_extension fun σ r r' htp s _ b => ?_ obtain ⟨P, _, _, j, g, hg⟩ := (PartialMap.ofTypeEq htp).extendsTo_top T h_ext let z : (fun s => ↥((⊤ : ∀ s, Set (r.carrier s)) s)) [^] (σ ⨯ ⦃s⦄) := (Interpret.ofFn fun s i => ⟨r.xs.get s i, Set.mem_univ _⟩, ⟨b, Set.mem_univ _⟩) have hzM : z.map (fun _ a => a.1) = ((r.xs, b) : r.carrier [^] (σ ⨯ ⦃s⦄)) := Interpret.ext fun t i => by cases i <;> simp [z] have hzP : z.map g = ((r'.xs.map j, g s ⟨b, Set.mem_univ _⟩) : P [^] (σ ⨯ ⦃s⦄)) := Interpret.ext fun t i => by cases i with | left i => simp only [z, Interpret.get_left, Interpret.map_fst, Interpret.get_map, Interpret.get_ofFn] exact (hg t ⟨r.xs.get t i, i, rfl⟩).trans (congrArg (j t) (PartialMap.ofTypeEq_get htp t i)) | right i => cases i; rfl refine ⟨P, inferInstance, j, g s ⟨b, Set.mem_univ _⟩, (Formula.typeOf_subset_iff_eq _).1 fun ψ hψ => ⟨hψ.1, ?_⟩⟩ rw [← hzP] exact g.map_formula hψ.1 (hzM ▸ hψ.2) /-! ### The embedding test on substructures -/ /-- **The embedding test, for domains closed under the function symbols.** It suffices that the extension property of `hasQERelTo_of_exists_extension` holds when the domain contains the substructure it generates. -/ theorem hasQERelTo_of_exists_extension_closed (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (C : ∀ s, Set (M s)) (g : PartialMap (qfRelTo L R) C N), ⇑(Substructure.closure L C) ≤ C → (∃ s, ∃ x : M s, x ∉ C s) → ∃ (s : S) (b : M s), b ∉ C s ∧ g.ExtendsTo (insertAt C s b) (le_insertAt C s b)) : T.HasQERelTo R := by classical refine hasQERelTo_of_exists_extension fun M N _ _ _ _ _ _ C g hC => ?_ by_cases hcl : ⇑(Substructure.closure L C) ≤ C · exact h_ext M N C g hcl hC · obtain ⟨s, b, hbcl, hbC⟩ : ∃ s, ∃ b : M s, b ∈ Substructure.closure L C s ∧ b ∉ C s := by by_contra hall push Not at hall exact hcl fun s b hb => hall s b hb have hsub : insertAt C s b ≤ ⇑(Substructure.closure L C) := fun t x hx => by rw [mem_insertAt] at hx exact hx.elim (fun h => Substructure.subset_closure h) fun h => by cases h; exact hbcl exact ⟨s, b, hbC, N, inferInstance, inferInstance, .refl L N, g.closure.restrict hsub, fun s' a => g.closure_apply a⟩ /-- **The embedding test on substructures.** It suffices that the extension property of `hasQERelTo_of_exists_extension` holds when the domain is a substructure. -/ theorem hasQERelTo_of_exists_extension_substructure (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (A : L.Substructure M) (g : PartialMap (qfRelTo L R) ⇑A N), (∃ s, ∃ x : M s, x ∉ A s) → ∃ (s : S) (b : M s), b ∉ A s ∧ g.ExtendsTo (insertAt ⇑A s b) (le_insertAt ⇑A s b)) : T.HasQERelTo R := by refine hasQERelTo_of_exists_extension_closed fun M N _ _ _ _ _ _ C g hcl hC => ?_ have hge : C ≤ ⇑(Substructure.closure L C) := fun _ _ hx => Substructure.subset_closure hx obtain ⟨s, b, hb, P, _, _, j, g', hg'⟩ := h_ext M N (Substructure.closure L C) (g.restrict hcl) (let ⟨s, x, hx⟩ := hC; ⟨s, x, fun h => hx (hcl s h)⟩) have hins : insertAt C s b ≤ insertAt ⇑(Substructure.closure L C) s b := fun t x hx => by rw [mem_insertAt] at hx ⊢ exact hx.imp (fun h => hge t h) id exact ⟨s, b, fun h => hb (hge s h), P, inferInstance, inferInstance, j, g'.restrict hins, fun s' a => hg' s' ⟨a.1, hge s' a.2⟩⟩ /-- **The embedding test on substructures, relative form.** It suffices that the extension property holds when the domain is a substructure containing every element of every sort of `R`. -/ theorem hasQERelTo_of_exists_extension_substructure_of_rel (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (A : L.Substructure M) (g : PartialMap (qfRelTo L R) ⇑A N), (∀ s ∈ R, ∀ x : M s, x ∈ A s) → (∃ s, ∃ x : M s, x ∉ A s) → ∃ (s : S) (b : M s), b ∉ A s ∧ g.ExtendsTo (insertAt ⇑A s b) (le_insertAt ⇑A s b)) : T.HasQERelTo R := by classical refine hasQERelTo_of_exists_extension_substructure fun M N _ _ _ _ _ _ A g hne => ?_ by_cases hR : ∀ s ∈ R, ∀ x : M s, x ∈ A s · exact h_ext M N A g hR hne · push Not at hR obtain ⟨s, hs, b, hb⟩ := hR exact ⟨s, b, hb, g.extendsTo_insertAt_of_mem hs b⟩ /-- **Substructure extension criterion.** `T` has quantifier elimination relative to `R` if every partial map on a substructure of a model of `T` extends to every element of a sort outside `R`, after passing to an elementary extension. -/ theorem hasQERelTo_of_substructure_extension (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (A : L.Substructure M) (g : PartialMap (qfRelTo L R) ⇑A N) (s : S), s ∉ R → ∀ b : M s, g.ExtendsTo (insertAt ⇑A s b) (le_insertAt ⇑A s b)) : T.HasQERelTo R := by classical refine hasQERelTo_of_exists_extension_substructure fun M N _ _ _ _ _ _ A g ⟨s, b, hb⟩ => ⟨s, b, hb, ?_⟩ by_cases hs : s ∈ R · exact g.extendsTo_insertAt_of_mem hs b · exact h_ext M N A g s hs b /-- **Subset extension criterion**: as `hasQERelTo_of_substructure_extension`, for partial maps on arbitrary subsets. -/ theorem hasQERelTo_of_subset_extension (h_ext : ∀ (M N : S → Type (max u v z)) [L.Structure M] [L.Structure N] [∀ s, Nonempty (M s)] [∀ s, Nonempty (N s)] [M ⊨ T] [N ⊨ T] (C : ∀ s, Set (M s)) (g : PartialMap (qfRelTo L R) C N) (s : S), s ∉ R → ∀ b : M s, g.ExtendsTo (insertAt C s b) (le_insertAt C s b)) : T.HasQERelTo R := hasQERelTo_of_substructure_extension fun M N _ _ _ _ _ _ A g s hs b => h_ext M N ⇑A g s hs b end Theory end ManySorted.Language