/- 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.DeltaMaps public import MultisortedLogic.Substructures /-! # Partial maps A partial `Δ`-map from a subset `C` of a structure `M` to a structure `N` is a family of maps on `C` that preserves the formulas of `Δ`. The formulas are evaluated in `M`, not in `C`, which need not be closed under the function symbols. Partial maps are what the embedding test of `EmbeddingTest.lean` extends one element at a time. For the formulas quantifier-free relative to a set of sorts `R`: * on a substructure, and for `R = ∅`, partial maps are the embeddings of the induced structure (`PartialMap.toEmbedding`, `Embedding.toPartialMap`); * a partial map extends, uniquely, to the substructure generated by its domain (`PartialMap.closure`). An element of that substructure is the value `t(x)` of a term at a tuple from the domain, and it goes to `t(g x)`. ## Main definitions * `insertAt C s b`: the family of sets `C` with one more element `b` of sort `s`. * `PartialMap Δ C N`: a family of maps on a family of sets `C ⊆ M` preserving the formulas of `Δ`. * `PartialMap.restrict`: the restriction to a smaller family of sets. * `PartialMap.ofTypeEq`: for tuples `x` and `y` with the same type in the formulas quantifier-free relative to `R`, the partial map sending each entry of `x` to the corresponding entry of `y`. * `PartialMap.toEmbedding`, `Embedding.toPartialMap`: partial maps on a substructure, for the formulas quantifier-free relative to no sorts, are the embeddings of the induced structure. * `PartialMap.closure`: the extension to the substructure generated by the domain. ## Implementation notes A subset of `M` is a family `C : ∀ s, Set (M s)`, and its elements form `fun s => ↥(C s)`. A substructure `A` is such a family, `⇑A`, so partial maps on it are `PartialMap Δ ⇑A N`. -/ @[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] /-! ### Partial maps -/ /-- `C` with one more element `b : M s`. -/ def insertAt (C : ∀ t, Set (M t)) (s : S) (b : M s) : ∀ t, Set (M t) := fun t => {x | x ∈ C t ∨ (⟨t, x⟩ : Σ t, M t) = ⟨s, b⟩} @[simp] theorem mem_insertAt {C : ∀ t, Set (M t)} {s t : S} {b : M s} {x : M t} : x ∈ insertAt C s b t ↔ x ∈ C t ∨ (⟨t, x⟩ : Σ t, M t) = ⟨s, b⟩ := Iff.rfl theorem mem_insertAt_of_mem {C : ∀ t, Set (M t)} {s t : S} {b : M s} {x : M t} (h : x ∈ C t) : x ∈ insertAt C s b t := mem_insertAt.2 (.inl h) theorem le_insertAt (C : ∀ t, Set (M t)) (s : S) (b : M s) : C ≤ insertAt C s b := fun _ _ => mem_insertAt_of_mem theorem mem_insertAt_self (C : ∀ t, Set (M t)) (s : S) (b : M s) : b ∈ insertAt C s b s := mem_insertAt.2 (.inr rfl) variable (Δ : L.FormulaClass) /-- A partial `Δ`-map from `C ⊆ M` to `N`: a family of maps on the elements of `C` that preserves the formulas of `Δ`. -/ structure PartialMap (C : ∀ s, Set (M s)) (N : S → Type w') [L.Structure N] where /-- The underlying family of maps. -/ toFun : ∀ s, C s → N s /-- The maps preserve the formulas of `Δ`. -/ map_formula' : ∀ {σ : Arity S} (φ : L.Formula σ), φ ∈ Δ σ → ∀ x : (fun s => ↥(C s)) [^] σ, φ.Realize (x.map fun _ a => a.1) → φ.Realize (x.map toFun) variable {Δ} namespace PartialMap variable {C D : ∀ s, Set (M s)} instance : DFunLike (PartialMap Δ C N) S fun s => C s → N s where coe g := g.toFun coe_injective g k h := by cases g; cases k; cases h; rfl @[simp] theorem toFun_eq_coe (g : PartialMap Δ C N) : g.toFun = ⇑g := rfl /-- A partial `Δ`-map preserves the formulas of `Δ`. -/ theorem map_formula (g : PartialMap Δ C N) {σ : Arity S} {φ : L.Formula σ} (hφ : φ ∈ Δ σ) {x : (fun s => ↥(C s)) [^] σ} : φ.Realize (x.map fun _ a => a.1) → φ.Realize (x.map g) := g.map_formula' φ hφ x /-- The restriction of a partial map to a smaller family of sets. -/ def restrict (g : PartialMap Δ C N) (h : D ≤ C) : PartialMap Δ D N where toFun s a := g s ⟨a.1, h s a.2⟩ map_formula' φ hφ x hx := by have := g.map_formula (x := x.map fun s a => ⟨a.1, h s a.2⟩) hφ (by rwa [Interpret.map_map]) rwa [Interpret.map_map] at this @[simp] theorem restrict_apply (g : PartialMap Δ C N) (h : D ≤ C) {s : S} (a : D s) : g.restrict h s a = g s ⟨a.1, h s a.2⟩ := rfl end PartialMap /-- The restriction of a `Δ`-map to a family of sets, as a partial `Δ`-map. -/ def DeltaMap.toPartialMap (f : M →[Δ]ᶠ N) (C : ∀ s, Set (M s)) : PartialMap Δ C N where toFun s a := f s a.1 map_formula' _ hφ x hx := by have := f.map_formula hφ hx rwa [Interpret.map_map] at this @[simp] theorem DeltaMap.toPartialMap_apply (f : M →[Δ]ᶠ N) (C : ∀ s, Set (M s)) {s : S} (a : C s) : f.toPartialMap C s a = f s a.1 := rfl /-! ### The partial map between tuples with the same type -/ /-- For tuples `x` and `y` with the same type in formulas quantifier-free relative to `R`, the map sending each entry of `x` to the corresponding entry of `y` is a partial map. -/ noncomputable def PartialMap.ofTypeEq {R : Set S} {σ : Arity S} {x : M [^] σ} {y : N [^] σ} (htp : Formula.typeOf (qfRelTo L R σ) x = Formula.typeOf (qfRelTo L R σ) y) : PartialMap (qfRelTo L R) (fun s => Set.range (x.get s)) N where toFun s a := y.get s (Classical.choose a.2) map_formula' {τ} φ hφ z hz := by let g : ∀ s, τ.Idx s → σ.Idx s := fun s i => Classical.choose (z.get s i).2 have hx : z.map (fun _ a => a.1) = x.comap g := Interpret.ext fun s i => by simp [g, Classical.choose_spec (z.get s i).2] have hy : z.map (fun s a => y.get s (Classical.choose a.2)) = y.comap g := Interpret.ext fun s i => by simp [g] rw [hx, ← Formula.realize_relabel] at hz rw [hy, ← Formula.realize_relabel] exact (Formula.realize_iff_of_typeOf_eq htp (Formula.IsQFRelTo.relabel g hφ)).1 hz theorem PartialMap.ofTypeEq_get {R : Set S} {σ : Arity S} {x : M [^] σ} {y : N [^] σ} (htp : Formula.typeOf (qfRelTo L R σ) x = Formula.typeOf (qfRelTo L R σ) y) (s : S) (i : σ.Idx s) : PartialMap.ofTypeEq htp s ⟨x.get s i, i, rfl⟩ = y.get s i := by have hi : x.get s (Classical.choose (⟨i, rfl⟩ : ∃ k, x.get s k = x.get s i)) = x.get s i := Classical.choose_spec (⟨i, rfl⟩ : ∃ k, x.get s k = x.get s i) exact (Formula.realize_iff_of_typeOf_eq htp (ψ := .equal (.var (Classical.choose (⟨i, rfl⟩ : ∃ k, x.get s k = x.get s i))) (.var i)) (Formula.IsQFRelTo.of_isAtomic (.equal _ _))).1 hi /-! ### Partial maps on a substructure -/ section Substructure variable {A : L.Substructure M} /-- A partial map on a substructure, for the formulas quantifier-free relative to no sorts, is a `Δ`-map of the induced structure. -/ def PartialMap.toDeltaMap (f : PartialMap (qfRelTo L ∅) ⇑A N) : (fun s => ↥(A s)) →[qfRelTo L ∅]ᶠ N where toFun := f map_formula' _ hφ x hx := f.map_formula hφ ((A.subtype.realize_of_isQFRelTo_empty hφ x).2 hx) /-- A partial map on a substructure, for the formulas quantifier-free relative to no sorts, is an embedding of the induced structure. -/ def PartialMap.toEmbedding (f : PartialMap (qfRelTo L ∅) ⇑A N) : (fun s => ↥(A s)) ↪[L] N := f.toDeltaMap.toEmbedding @[simp] theorem PartialMap.coe_toEmbedding (f : PartialMap (qfRelTo L ∅) ⇑A N) : ⇑f.toEmbedding = ⇑f := rfl /-- An embedding of the induced structure is a partial map on the substructure, for the formulas quantifier-free relative to no sorts. -/ def Embedding.toPartialMap (g : (fun s => ↥(A s)) ↪[L] N) : PartialMap (qfRelTo L ∅) ⇑A N where toFun := g map_formula' _ hφ x hx := (g.realize_of_isQFRelTo_empty hφ x).2 ((A.subtype.realize_of_isQFRelTo_empty hφ x).1 hx) @[simp] theorem Embedding.coe_toPartialMap (g : (fun s => ↥(A s)) ↪[L] N) : ⇑g.toPartialMap = ⇑g := rfl end Substructure /-! ### Extension to the generated substructure -/ section Closure variable {R : Set S} {C : ∀ s, Set (M s)} namespace PartialMap /-- Two terms with the same value at tuples from the domain have the same value at their images. -/ theorem realize_eq_of_realize_eq (g : PartialMap (qfRelTo L R) C N) {Γ Γ' : Arity S} {s : S} {xs : (fun s => ↥(C s)) [^] Γ} {xs' : (fun s => ↥(C s)) [^] Γ'} {t : L.Term Γ ⦃s⦄} {t' : L.Term Γ' ⦃s⦄} (h : t.realize (xs.map fun _ a => a.1) = t'.realize (xs'.map fun _ a => a.1)) : t.realize (xs.map g) = t'.realize (xs'.map g) := by have := g.map_formula (x := ((xs, xs') : (fun s => ↥(C s)) [^] (Γ ⨯ Γ'))) (φ := (t.relabel fun _ => .left) =' (t'.relabel fun _ => .right)) (Formula.IsQFRelTo.of_isAtomic (.equal _ _)) (by simpa using h) simpa using this /-- An element of the substructure generated by `C`, as the value of a term at a tuple from `C`. -/ theorem exists_term_of_mem_closure {s : S} (x : Substructure.closure L C s) : ∃ w : Σ Γ : Arity S, (fun s => ↥(C s)) [^] Γ × L.Term Γ ⦃s⦄, w.2.2.realize (w.2.1.map fun _ a => a.1) = x.1 := let ⟨Γ, xs, t, h⟩ := Substructure.mem_closure_iff_exists_term.1 x.2 ⟨⟨Γ, xs, t⟩, h⟩ /-- The extension of `g` to the substructure generated by its domain, as a family of maps: `t(x)` goes to `t(g x)`. -/ noncomputable def closureMap (g : PartialMap (qfRelTo L R) C N) (s : S) (x : Substructure.closure L C s) : N s := let w := Classical.choose (exists_term_of_mem_closure x) w.2.2.realize (w.2.1.map g) /-- `closureMap g` sends `t(x)` to `t(g x)`, for every term `t` and tuple `x` from `C`. -/ theorem closureMap_eq (g : PartialMap (qfRelTo L R) C N) {s : S} {x : Substructure.closure L C s} {Γ : Arity S} {xs : (fun s => ↥(C s)) [^] Γ} {t : L.Term Γ ⦃s⦄} (h : t.realize (xs.map fun _ a => a.1) = x.1) : closureMap g s x = t.realize (xs.map g) := g.realize_eq_of_realize_eq ((Classical.choose_spec (exists_term_of_mem_closure x)).trans h.symm) /-- **A partial map extends to the substructure generated by its domain.** -/ noncomputable def closure (g : PartialMap (qfRelTo L R) C N) : PartialMap (qfRelTo L R) ⇑(Substructure.closure L C) N where toFun := closureMap g map_formula' {σ} φ hφ z hz := by obtain ⟨Γ, xs, ts, hts⟩ := Substructure.exists_term_realize_eq (X := C) (y := z.map fun _ a => a.1) fun s i => by obtain ⟨⟨Γ, xs, t⟩, h⟩ := exists_term_of_mem_closure (z.get s i) exact ⟨Γ, xs, t, h.trans (Interpret.get_map (fun _ a => a.1) z s i).symm⟩ have hzN : z.map (closureMap g) = ts.realize (xs.map g) := Interpret.ext fun s i => by rw [Interpret.get_map, ← Term.realize_get] exact g.closureMap_eq (by rw [Term.realize_get, hts, Interpret.get_map]) have hM : (φ.subst ts).Realize (xs.map fun _ a => a.1) := by rwa [Formula.realize_subst, hts] have := g.map_formula (Formula.IsQFRelTo.subst ts hφ) hM rwa [Formula.realize_subst, ← hzN] at this /-- The extension to the generated substructure extends `g`. -/ @[simp] theorem closure_apply (g : PartialMap (qfRelTo L R) C N) {s : S} (a : C s) : g.closure s ⟨a.1, Substructure.subset_closure a.2⟩ = g s a := g.closureMap_eq (x := ⟨a.1, Substructure.subset_closure a.2⟩) (Γ := ⦃s⦄) (xs := a) (t := .var .var) rfl end PartialMap end Closure end ManySorted.Language