source
stringlengths
17
118
lean4
stringlengths
0
335k
.lake/packages/mathlib/Mathlib/Geometry/Manifold/ContMDiff/Atlas.lean
import Mathlib.Geometry.Manifold.ContMDiff.Basic /-! ## Smoothness of charts and local structomorphisms We show that the model with corners, charts, extended charts and their inverses are `C^n`, and that local structomorphisms are `C^n` with `C^n` inverses. -/ open Set ChartedSpace IsManifold open scoped Manifold ContDiff variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] -- declare a `C^n` manifold `M` over the pair `(E, H)`. {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] {n : WithTop ℕ∞} [IsManifold I n M] -- declare a topological space `M'`. {M' : Type*} [TopologicalSpace M'] -- declare functions, sets, points and smoothness indices {e : OpenPartialHomeomorph M H} {x : M} /-! ### Atlas members are `C^n` -/ section Atlas theorem contMDiff_model : ContMDiff I 𝓘(𝕜, E) n I := by intro x refine contMDiffAt_iff.mpr ⟨I.continuousAt, ?_⟩ simp only [mfld_simps] refine contDiffWithinAt_id.congr_of_eventuallyEq ?_ ?_ · exact Filter.eventuallyEq_of_mem self_mem_nhdsWithin fun x₂ => I.right_inv simp_rw [Function.comp_apply, I.left_inv, Function.id_def] theorem contMDiffOn_model_symm : ContMDiffOn 𝓘(𝕜, E) I n I.symm (range I) := by rw [contMDiffOn_iff] refine ⟨I.continuousOn_symm, fun x y => ?_⟩ simp only [mfld_simps] exact contDiffOn_id.congr fun x' => I.right_inv /-- An atlas member is `C^n` for any `n`. -/ theorem contMDiffOn_of_mem_maximalAtlas (h : e ∈ maximalAtlas I n M) : ContMDiffOn I I n e e.source := (contDiffWithinAt_localInvariantProp n).liftPropOn_of_mem_maximalAtlas contDiffWithinAtProp_id h /-- The inverse of an atlas member is `C^n` for any `n`. -/ theorem contMDiffOn_symm_of_mem_maximalAtlas (h : e ∈ maximalAtlas I n M) : ContMDiffOn I I n e.symm e.target := (contDiffWithinAt_localInvariantProp n).liftPropOn_symm_of_mem_maximalAtlas contDiffWithinAtProp_id h theorem contMDiffAt_of_mem_maximalAtlas (h : e ∈ maximalAtlas I n M) (hx : x ∈ e.source) : ContMDiffAt I I n e x := (contMDiffOn_of_mem_maximalAtlas h).contMDiffAt <| e.open_source.mem_nhds hx theorem contMDiffAt_symm_of_mem_maximalAtlas {x : H} (h : e ∈ maximalAtlas I n M) (hx : x ∈ e.target) : ContMDiffAt I I n e.symm x := (contMDiffOn_symm_of_mem_maximalAtlas h).contMDiffAt <| e.open_target.mem_nhds hx theorem contMDiffOn_chart : ContMDiffOn I I n (chartAt H x) (chartAt H x).source := contMDiffOn_of_mem_maximalAtlas <| chart_mem_maximalAtlas x theorem contMDiffOn_chart_symm : ContMDiffOn I I n (chartAt H x).symm (chartAt H x).target := contMDiffOn_symm_of_mem_maximalAtlas <| chart_mem_maximalAtlas x theorem contMDiffAt_extend {x : M} (he : e ∈ maximalAtlas I n M) (hx : x ∈ e.source) : ContMDiffAt I 𝓘(𝕜, E) n (e.extend I) x := (contMDiff_model _).comp x <| contMDiffAt_of_mem_maximalAtlas he hx theorem contMDiffAt_extChartAt' {x' : M} (h : x' ∈ (chartAt H x).source) : ContMDiffAt I 𝓘(𝕜, E) n (extChartAt I x) x' := contMDiffAt_extend (chart_mem_maximalAtlas x) h omit [IsManifold I n M] in theorem contMDiffAt_extChartAt : ContMDiffAt I 𝓘(𝕜, E) n (extChartAt I x) x := by rw [contMDiffAt_iff_source] apply contMDiffWithinAt_id.congr_of_eventuallyEq_of_mem _ (by simp) filter_upwards [extChartAt_target_mem_nhdsWithin x] with y hy exact PartialEquiv.right_inv (extChartAt I x) hy theorem contMDiffOn_extChartAt : ContMDiffOn I 𝓘(𝕜, E) n (extChartAt I x) (chartAt H x).source := fun _x' hx' => (contMDiffAt_extChartAt' hx').contMDiffWithinAt theorem contMDiffOn_extend_symm (he : e ∈ maximalAtlas I n M) : ContMDiffOn 𝓘(𝕜, E) I n (e.extend I).symm (I '' e.target) := by refine (contMDiffOn_symm_of_mem_maximalAtlas he).comp (contMDiffOn_model_symm.mono <| image_subset_range _ _) ?_ simp_rw [image_subset_iff, PartialEquiv.restr_coe_symm, I.toPartialEquiv_coe_symm, preimage_preimage, I.left_inv, preimage_id']; rfl theorem contMDiffOn_extChartAt_symm (x : M) : ContMDiffOn 𝓘(𝕜, E) I n (extChartAt I x).symm (extChartAt I x).target := by convert contMDiffOn_extend_symm (chart_mem_maximalAtlas (I := I) x) · rw [extChartAt_target, I.image_eq] · infer_instance · infer_instance theorem contMDiffWithinAt_extChartAt_symm_target (x : M) {y : E} (hy : y ∈ (extChartAt I x).target) : ContMDiffWithinAt 𝓘(𝕜, E) I n (extChartAt I x).symm (extChartAt I x).target y := contMDiffOn_extChartAt_symm x y hy theorem contMDiffWithinAt_extChartAt_symm_range (x : M) {y : E} (hy : y ∈ (extChartAt I x).target) : ContMDiffWithinAt 𝓘(𝕜, E) I n (extChartAt I x).symm (range I) y := (contMDiffWithinAt_extChartAt_symm_target x hy).mono_of_mem_nhdsWithin (extChartAt_target_mem_nhdsWithin_of_mem hy) omit [IsManifold I n M] in theorem contMDiffWithinAt_extChartAt_symm_target_self (x : M) : ContMDiffWithinAt 𝓘(𝕜, E) I n (extChartAt I x).symm (extChartAt I x).target (extChartAt I x x) := by rw [contMDiffWithinAt_iff_target] constructor · apply ContinuousAt.continuousWithinAt apply ContinuousAt.comp _ I.continuousAt_symm exact (chartAt H x).symm.continuousAt (by simp) · apply contMDiffWithinAt_id.congr_of_mem (fun y hy ↦ ?_) (by simp) convert PartialEquiv.right_inv (extChartAt I x) hy simp omit [IsManifold I n M] in theorem contMDiffWithinAt_extChartAt_symm_range_self (x : M) : ContMDiffWithinAt 𝓘(𝕜, E) I n (extChartAt I x).symm (range I) (extChartAt I x x) := (contMDiffWithinAt_extChartAt_symm_target_self x).mono_of_mem_nhdsWithin (extChartAt_target_mem_nhdsWithin x) /-- An element of `contDiffGroupoid n I` is `C^n`. -/ theorem contMDiffOn_of_mem_contDiffGroupoid {e' : OpenPartialHomeomorph H H} (h : e' ∈ contDiffGroupoid n I) : ContMDiffOn I I n e' e'.source := (contDiffWithinAt_localInvariantProp n).liftPropOn_of_mem_groupoid contDiffWithinAtProp_id h end Atlas /-! ### (local) structomorphisms are `C^n` -/ section IsLocalStructomorph variable [ChartedSpace H M'] [IsM' : IsManifold I n M'] theorem isLocalStructomorphOn_contDiffGroupoid_iff_aux {f : OpenPartialHomeomorph M M'} (hf : LiftPropOn (contDiffGroupoid n I).IsLocalStructomorphWithinAt f f.source) : ContMDiffOn I I n f f.source := by -- It suffices to show regularity near each `x` apply contMDiffOn_of_locally_contMDiffOn intro x hx let c := chartAt H x let c' := chartAt H (f x) obtain ⟨-, hxf⟩ := hf x hx -- Since `f` is a local structomorph, it is locally equal to some transferred element `e` of -- the `contDiffGroupoid`. obtain ⟨e, he, he' : EqOn (c' ∘ f ∘ c.symm) e (c.symm ⁻¹' f.source ∩ e.source), hex : c x ∈ e.source⟩ := hxf (by simp only [hx, mfld_simps]) -- We choose a convenient set `s` in `M`. let s : Set M := (f.trans c').source ∩ ((c.trans e).trans c'.symm).source refine ⟨s, (f.trans c').open_source.inter ((c.trans e).trans c'.symm).open_source, ?_, ?_⟩ · simp only [s, mfld_simps] rw [← he'] <;> simp only [c, c', hx, hex, mfld_simps] -- We need to show `f` is `ContMDiffOn` the domain `s ∩ f.source`. We show this in two -- steps: `f` is equal to `c'.symm ∘ e ∘ c` on that domain and that function is -- `ContMDiffOn` it. have H₁ : ContMDiffOn I I n (c'.symm ∘ e ∘ c) s := by have hc' : ContMDiffOn I I n c'.symm _ := contMDiffOn_chart_symm have he'' : ContMDiffOn I I n e _ := contMDiffOn_of_mem_contDiffGroupoid he have hc : ContMDiffOn I I n c _ := contMDiffOn_chart refine (hc'.comp' (he''.comp' hc)).mono ?_ dsimp [s, c, c'] mfld_set_tac have H₂ : EqOn f (c'.symm ∘ e ∘ c) s := by intro y hy simp only [s, mfld_simps] at hy have hy₁ : f y ∈ c'.source := by simp only [hy, mfld_simps] have hy₂ : y ∈ c.source := by simp only [hy, mfld_simps] have hy₃ : c y ∈ c.symm ⁻¹' f.source ∩ e.source := by simp only [hy, mfld_simps] calc f y = c'.symm (c' (f y)) := by rw [c'.left_inv hy₁] _ = c'.symm (c' (f (c.symm (c y)))) := by rw [c.left_inv hy₂] _ = c'.symm (e (c y)) := by rw [← he' hy₃]; rfl refine (H₁.congr H₂).mono ?_ mfld_set_tac /-- Let `M` and `M'` be manifolds with the same model-with-corners, `I`. Then `f : M → M'` is a local structomorphism for `I`, if and only if it is manifold-`C^n` on the domain of definition in both directions. -/ theorem isLocalStructomorphOn_contDiffGroupoid_iff (f : OpenPartialHomeomorph M M') : LiftPropOn (contDiffGroupoid n I).IsLocalStructomorphWithinAt f f.source ↔ ContMDiffOn I I n f f.source ∧ ContMDiffOn I I n f.symm f.target := by constructor · intro h refine ⟨isLocalStructomorphOn_contDiffGroupoid_iff_aux h, isLocalStructomorphOn_contDiffGroupoid_iff_aux ?_⟩ -- todo: we can generalize this part of the proof to a lemma intro X hX let x := f.symm X have hx : x ∈ f.source := f.symm.mapsTo hX let c := chartAt H x let c' := chartAt H X obtain ⟨-, hxf⟩ := h x hx refine ⟨(f.symm.continuousAt hX).continuousWithinAt, fun h2x => ?_⟩ obtain ⟨e, he, h2e, hef, hex⟩ : ∃ e : OpenPartialHomeomorph H H, e ∈ contDiffGroupoid n I ∧ e.source ⊆ (c.symm ≫ₕ f ≫ₕ c').source ∧ EqOn (c' ∘ f ∘ c.symm) e e.source ∧ c x ∈ e.source := by have h1 : c' = chartAt H (f x) := by simp only [x, c', f.right_inv hX] have h2 : c' ∘ f ∘ c.symm = ⇑(c.symm ≫ₕ f ≫ₕ c') := rfl have hcx : c x ∈ c.symm ⁻¹' f.source := by simp only [c, hx, mfld_simps] rw [h2] rw [← h1, h2, OpenPartialHomeomorph.isLocalStructomorphWithinAt_iff'] at hxf · exact hxf hcx · dsimp [x, c]; mfld_set_tac · apply Or.inl simp only [c, hx, h1, mfld_simps] have h2X : c' X = e (c (f.symm X)) := by rw [← hef hex] dsimp only [Function.comp_def] have hfX : f.symm X ∈ c.source := by simp only [c, x, mfld_simps] rw [c.left_inv hfX, f.right_inv hX] have h3e : EqOn (c ∘ f.symm ∘ c'.symm) e.symm (c'.symm ⁻¹' f.target ∩ e.target) := by have h1 : EqOn (c.symm ≫ₕ f ≫ₕ c').symm e.symm (e.target ∩ e.target) := by apply EqOn.symm refine e.isImage_source_target.symm_eqOn_of_inter_eq_of_eqOn ?_ ?_ · rw [inter_self, inter_eq_right.mpr h2e] · rw [inter_self]; exact hef.symm have h2 : e.target ⊆ (c.symm ≫ₕ f ≫ₕ c').target := by intro x hx; rw [← e.right_inv hx, ← hef (e.symm.mapsTo hx)] exact OpenPartialHomeomorph.mapsTo _ (h2e <| e.symm.mapsTo hx) rw [inter_self] at h1 rwa [inter_eq_right.mpr] refine h2.trans ?_ mfld_set_tac refine ⟨e.symm, StructureGroupoid.symm _ he, h3e, ?_⟩ rw [h2X]; exact e.mapsTo hex · -- We now show the converse: an open partial homeomorphism `f : M → M'` which is `C^n` in both -- directions is a local structomorphism. We do this by proposing -- `((chart_at H x).symm.trans f).trans (chart_at H (f x))` as a candidate for a structomorphism -- of `H`. rintro ⟨h₁, h₂⟩ x hx refine ⟨(h₁ x hx).continuousWithinAt, ?_⟩ let c := chartAt H x let c' := chartAt H (f x) rintro (hx' : c x ∈ c.symm ⁻¹' f.source) -- propose `(c.symm.trans f).trans c'` as a candidate for a local structomorphism of `H` refine ⟨(c.symm.trans f).trans c', ⟨?_, ?_⟩, (?_ : EqOn (c' ∘ f ∘ c.symm) _ _), ?_⟩ · -- regularity of the candidate local structomorphism in the forward direction intro y hy simp only [mfld_simps] at hy have H : ContMDiffWithinAt I I n f (f ≫ₕ c').source ((extChartAt I x).symm y) := by refine (h₁ ((extChartAt I x).symm y) ?_).mono ?_ · simp only [c, hy, mfld_simps] · mfld_set_tac have hy' : (extChartAt I x).symm y ∈ c.source := by simp only [c, hy, mfld_simps] have hy'' : f ((extChartAt I x).symm y) ∈ c'.source := by simp only [c, hy, mfld_simps] rw [contMDiffWithinAt_iff_of_mem_source hy' hy''] at H convert H.2.mono _ · simp only [c, hy, mfld_simps] · dsimp [c, c']; mfld_set_tac · -- regularity of the candidate local structomorphism in the reverse direction intro y hy simp only [mfld_simps] at hy have H : ContMDiffWithinAt I I n f.symm (f.symm ≫ₕ c).source ((extChartAt I (f x)).symm y) := by refine (h₂ ((extChartAt I (f x)).symm y) ?_).mono ?_ · simp only [c', hy, mfld_simps] · mfld_set_tac have hy' : (extChartAt I (f x)).symm y ∈ c'.source := by simp only [c', hy, mfld_simps] have hy'' : f.symm ((extChartAt I (f x)).symm y) ∈ c.source := by simp only [c', hy, mfld_simps] rw [contMDiffWithinAt_iff_of_mem_source hy' hy''] at H convert H.2.mono _ · simp only [c', hy, mfld_simps] · dsimp [c, c']; mfld_set_tac -- now check the candidate local structomorphism agrees with `f` where it is supposed to · simp only [mfld_simps]; apply eqOn_refl · simp only [c, c', hx', mfld_simps] end IsLocalStructomorph
.lake/packages/mathlib/Mathlib/Geometry/Manifold/ContMDiff/Basic.lean
import Mathlib.Geometry.Manifold.ContMDiff.Defs /-! ## Basic properties of `C^n` functions between manifolds In this file, we show that standard operations on `C^n` maps between manifolds are `C^n` : * `ContMDiffOn.comp` gives the invariance of the `Cⁿ` property under composition * `contMDiff_id` gives the smoothness of the identity * `contMDiff_const` gives the smoothness of constant functions * `contMDiff_inclusion` shows that the inclusion between open sets of a topological space is `C^n` * `contMDiff_isOpenEmbedding` shows that if `M` has a `ChartedSpace` structure induced by an open embedding `e : M → H`, then `e` is `C^n`. ## Tags chain rule, manifolds, higher derivative -/ open Filter Function Set Topology open scoped Manifold ContDiff variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] -- declare the prerequisites for a charted space `M` over the pair `(E, H)`. {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] -- declare the prerequisites for a charted space `M'` over the pair `(E', H')`. {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M' : Type*} [TopologicalSpace M'] -- declare the prerequisites for a charted space `M''` over the pair `(E'', H'')`. {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace 𝕜 E''] {H'' : Type*} [TopologicalSpace H''] {I'' : ModelWithCorners 𝕜 E'' H''} {M'' : Type*} [TopologicalSpace M''] section ChartedSpace variable [ChartedSpace H M] [ChartedSpace H' M'] [ChartedSpace H'' M''] -- declare functions, sets, points and smoothness indices {f : M → M'} {s : Set M} {x : M} {n : WithTop ℕ∞} /-! ### Regularity of the composition of `C^n` functions between manifolds -/ section Composition /-- The composition of `C^n` functions within domains at points is `C^n`. -/ theorem ContMDiffWithinAt.comp {t : Set M'} {g : M' → M''} (x : M) (hg : ContMDiffWithinAt I' I'' n g t (f x)) (hf : ContMDiffWithinAt I I' n f s x) (st : MapsTo f s t) : ContMDiffWithinAt I I'' n (g ∘ f) s x := by rw [contMDiffWithinAt_iff] at hg hf ⊢ refine ⟨hg.1.comp hf.1 st, ?_⟩ set e := extChartAt I x set e' := extChartAt I' (f x) have : e' (f x) = (writtenInExtChartAt I I' x f) (e x) := by simp only [e, e', mfld_simps] rw [this] at hg have A : ∀ᶠ y in 𝓝[e.symm ⁻¹' s ∩ range I] e x, f (e.symm y) ∈ t ∧ f (e.symm y) ∈ e'.source := by simp only [e, ← map_extChartAt_nhdsWithin, eventually_map] filter_upwards [hf.1.tendsto (extChartAt_source_mem_nhds (I := I') (f x)), inter_mem_nhdsWithin s (extChartAt_source_mem_nhds (I := I) x)] rintro x' (hfx' : f x' ∈ e'.source) ⟨hx's, hx'⟩ simp only [e, true_and, e.left_inv hx', st hx's, *] refine ((hg.2.comp _ (hf.2.mono inter_subset_right) ((mapsTo_preimage _ _).mono_left inter_subset_left)).mono_of_mem_nhdsWithin (inter_mem ?_ self_mem_nhdsWithin)).congr_of_eventuallyEq ?_ ?_ · filter_upwards [A] rintro x' ⟨ht, hfx'⟩ simp only [*, e, e',mem_preimage, writtenInExtChartAt, (· ∘ ·), mem_inter_iff, e'.left_inv, true_and] exact mem_range_self _ · filter_upwards [A] rintro x' ⟨-, hfx'⟩ simp only [*, e, e', (· ∘ ·), writtenInExtChartAt, e'.left_inv] · simp only [e, e', writtenInExtChartAt, (· ∘ ·), mem_extChartAt_source, e.left_inv, e'.left_inv] /-- See note [comp_of_eq lemmas] -/ theorem ContMDiffWithinAt.comp_of_eq {t : Set M'} {g : M' → M''} {x : M} {y : M'} (hg : ContMDiffWithinAt I' I'' n g t y) (hf : ContMDiffWithinAt I I' n f s x) (st : MapsTo f s t) (hx : f x = y) : ContMDiffWithinAt I I'' n (g ∘ f) s x := by subst hx; exact hg.comp x hf st /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem ContMDiffOn.comp {t : Set M'} {g : M' → M''} (hg : ContMDiffOn I' I'' n g t) (hf : ContMDiffOn I I' n f s) (st : s ⊆ f ⁻¹' t) : ContMDiffOn I I'' n (g ∘ f) s := fun x hx => (hg _ (st hx)).comp x (hf x hx) st /-- The composition of `C^n` functions on domains is `C^n`. -/ theorem ContMDiffOn.comp' {t : Set M'} {g : M' → M''} (hg : ContMDiffOn I' I'' n g t) (hf : ContMDiffOn I I' n f s) : ContMDiffOn I I'' n (g ∘ f) (s ∩ f ⁻¹' t) := hg.comp (hf.mono inter_subset_left) inter_subset_right /-- The composition of `C^n` functions is `C^n`. -/ theorem ContMDiff.comp {g : M' → M''} (hg : ContMDiff I' I'' n g) (hf : ContMDiff I I' n f) : ContMDiff I I'' n (g ∘ f) := by rw [← contMDiffOn_univ] at hf hg ⊢ exact hg.comp hf subset_preimage_univ /-- The composition of `C^n` functions within domains at points is `C^n`. -/ theorem ContMDiffWithinAt.comp' {t : Set M'} {g : M' → M''} (x : M) (hg : ContMDiffWithinAt I' I'' n g t (f x)) (hf : ContMDiffWithinAt I I' n f s x) : ContMDiffWithinAt I I'' n (g ∘ f) (s ∩ f ⁻¹' t) x := hg.comp x (hf.mono inter_subset_left) inter_subset_right /-- `g ∘ f` is `C^n` within `s` at `x` if `g` is `C^n` at `f x` and `f` is `C^n` within `s` at `x`. -/ theorem ContMDiffAt.comp_contMDiffWithinAt {g : M' → M''} (x : M) (hg : ContMDiffAt I' I'' n g (f x)) (hf : ContMDiffWithinAt I I' n f s x) : ContMDiffWithinAt I I'' n (g ∘ f) s x := hg.comp x hf (mapsTo_univ _ _) /-- `g ∘ f` is `C^n` within `s` at `x` if `g` is `C^n` at `f x` and `f` is `C^n` within `s` at `x`. -/ theorem ContMDiffAt.comp_contMDiffWithinAt_of_eq {g : M' → M''} {x : M} {y : M'} (hg : ContMDiffAt I' I'' n g y) (hf : ContMDiffWithinAt I I' n f s x) (hx : f x = y) : ContMDiffWithinAt I I'' n (g ∘ f) s x := by subst hx; exact hg.comp_contMDiffWithinAt x hf /-- The composition of `C^n` functions at points is `C^n`. -/ nonrec theorem ContMDiffAt.comp {g : M' → M''} (x : M) (hg : ContMDiffAt I' I'' n g (f x)) (hf : ContMDiffAt I I' n f x) : ContMDiffAt I I'' n (g ∘ f) x := hg.comp x hf (mapsTo_univ _ _) /-- See note [comp_of_eq lemmas] -/ theorem ContMDiffAt.comp_of_eq {g : M' → M''} {x : M} {y : M'} (hg : ContMDiffAt I' I'' n g y) (hf : ContMDiffAt I I' n f x) (hx : f x = y) : ContMDiffAt I I'' n (g ∘ f) x := by subst hx; exact hg.comp x hf theorem ContMDiff.comp_contMDiffOn {f : M → M'} {g : M' → M''} {s : Set M} (hg : ContMDiff I' I'' n g) (hf : ContMDiffOn I I' n f s) : ContMDiffOn I I'' n (g ∘ f) s := hg.contMDiffOn.comp hf Set.subset_preimage_univ theorem ContMDiffOn.comp_contMDiff {t : Set M'} {g : M' → M''} (hg : ContMDiffOn I' I'' n g t) (hf : ContMDiff I I' n f) (ht : ∀ x, f x ∈ t) : ContMDiff I I'' n (g ∘ f) := contMDiffOn_univ.mp <| hg.comp hf.contMDiffOn fun x _ => ht x end Composition /-! ### The identity is `C^n` -/ section id theorem contMDiff_id : ContMDiff I I n (id : M → M) := ContMDiff.of_le ((contDiffWithinAt_localInvariantProp ⊤).liftProp_id contDiffWithinAtProp_id) le_top theorem contMDiffOn_id : ContMDiffOn I I n (id : M → M) s := contMDiff_id.contMDiffOn theorem contMDiffAt_id : ContMDiffAt I I n (id : M → M) x := contMDiff_id.contMDiffAt theorem contMDiffWithinAt_id : ContMDiffWithinAt I I n (id : M → M) s x := contMDiffAt_id.contMDiffWithinAt end id /-! ### Iterated functions -/ section Iterate /-- The iterates of `C^n` functions on domains are `C^n`. -/ theorem ContMDiffOn.iterate {f : M → M} (hf : ContMDiffOn I I n f s) (hmaps : Set.MapsTo f s s) (k : ℕ) : ContMDiffOn I I n (f^[k]) s := by induction k with | zero => simpa using contMDiffOn_id | succ k h => simpa using h.comp hf hmaps /-- The iterates of `C^n` functions are `C^n`. -/ theorem ContMDiff.iterate {f : M → M} (hf : ContMDiff I I n f) (k : ℕ) : ContMDiff I I n (f^[k]) := contMDiffOn_univ.mp ((contMDiffOn_univ.mpr hf).iterate (univ.mapsTo_univ f) k) end Iterate /-! ### Constants are `C^n` -/ section const variable {c : M'} theorem contMDiff_const : ContMDiff I I' n fun _ : M => c := by intro x refine ⟨continuousWithinAt_const, ?_⟩ simp only [ContDiffWithinAtProp, Function.comp_def] exact contDiffWithinAt_const @[to_additive] theorem contMDiff_one [One M'] : ContMDiff I I' n (1 : M → M') := by simp only [Pi.one_def, contMDiff_const] theorem contMDiffOn_const : ContMDiffOn I I' n (fun _ : M => c) s := contMDiff_const.contMDiffOn @[to_additive] theorem contMDiffOn_one [One M'] : ContMDiffOn I I' n (1 : M → M') s := contMDiff_one.contMDiffOn theorem contMDiffAt_const : ContMDiffAt I I' n (fun _ : M => c) x := contMDiff_const.contMDiffAt @[to_additive] theorem contMDiffAt_one [One M'] : ContMDiffAt I I' n (1 : M → M') x := contMDiff_one.contMDiffAt theorem contMDiffWithinAt_const : ContMDiffWithinAt I I' n (fun _ : M => c) s x := contMDiffAt_const.contMDiffWithinAt @[to_additive] theorem contMDiffWithinAt_one [One M'] : ContMDiffWithinAt I I' n (1 : M → M') s x := contMDiffAt_const.contMDiffWithinAt @[nontriviality] theorem contMDiff_of_subsingleton [Subsingleton M'] : ContMDiff I I' n f := by intro x rw [Subsingleton.elim f fun _ => (f x)] exact contMDiffAt_const @[nontriviality] theorem contMDiffAt_of_subsingleton [Subsingleton M'] : ContMDiffAt I I' n f x := contMDiff_of_subsingleton.contMDiffAt @[nontriviality] theorem contMDiffWithinAt_of_subsingleton [Subsingleton M'] : ContMDiffWithinAt I I' n f s x := contMDiffAt_of_subsingleton.contMDiffWithinAt @[nontriviality] theorem contMDiffOn_of_subsingleton [Subsingleton M'] : ContMDiffOn I I' n f s := contMDiff_of_subsingleton.contMDiffOn lemma contMDiff_of_discreteTopology [DiscreteTopology M] : ContMDiff I I' n f := by intro x -- f is locally constant, and constant functions are smooth. apply contMDiff_const (c := f x).contMDiffAt.congr_of_eventuallyEq simp [EventuallyEq] end const /-- `f` is continuously differentiable if it is cont. differentiable at each `x ∈ mulTSupport f`. -/ @[to_additive /-- `f` is continuously differentiable if it is continuously differentiable at each `x ∈ tsupport f`. See also `contMDiff_section_of_tsupport` for a similar result for sections of vector bundles. -/] theorem contMDiff_of_mulTSupport [One M'] {f : M → M'} (hf : ∀ x ∈ mulTSupport f, ContMDiffAt I I' n f x) : ContMDiff I I' n f := by intro x by_cases hx : x ∈ mulTSupport f · exact hf x hx · exact ContMDiffAt.congr_of_eventuallyEq contMDiffAt_const (notMem_mulTSupport_iff_eventuallyEq.1 hx) @[to_additive contMDiffWithinAt_of_notMem] theorem contMDiffWithinAt_of_notMem_mulTSupport {f : M → M'} [One M'] {x : M} (hx : x ∉ mulTSupport f) (n : WithTop ℕ∞) (s : Set M) : ContMDiffWithinAt I I' n f s x := by apply contMDiffWithinAt_const.congr_of_eventuallyEq (eventually_nhdsWithin_of_eventually_nhds <| notMem_mulTSupport_iff_eventuallyEq.mp hx) (image_eq_one_of_notMem_mulTSupport hx) @[deprecated (since := "2025-05-23")] alias contMDiffWithinAt_of_not_mem := contMDiffWithinAt_of_notMem @[to_additive existing contMDiffWithinAt_of_not_mem, deprecated (since := "2025-05-23")] alias contMDiffWithinAt_of_not_mem_mulTSupport := contMDiffWithinAt_of_notMem_mulTSupport /-- `f` is continuously differentiable at each point outside of its `mulTSupport`. -/ @[to_additive contMDiffAt_of_notMem] theorem contMDiffAt_of_notMem_mulTSupport {f : M → M'} [One M'] {x : M} (hx : x ∉ mulTSupport f) (n : WithTop ℕ∞) : ContMDiffAt I I' n f x := contMDiffWithinAt_of_notMem_mulTSupport hx n univ @[deprecated (since := "2025-05-23")] alias contMDiffAt_of_not_mem := contMDiffAt_of_notMem @[to_additive existing contMDiffAt_of_not_mem, deprecated (since := "2025-05-23")] alias contMDiffAt_of_not_mem_mulTSupport := contMDiffAt_of_notMem_mulTSupport /-- Given two `C^n` functions `f` and `g` which coincide locally around the frontier of a set `s`, then the piecewise function defined using `f` on `s` and `g` elsewhere is `C^n`. -/ lemma ContMDiff.piecewise {f g : M → M'} {s : Set M} [DecidablePred (· ∈ s)] (hf : ContMDiff I I' n f) (hg : ContMDiff I I' n g) (hfg : ∀ x ∈ frontier s, f =ᶠ[𝓝 x] g) : ContMDiff I I' n (piecewise s f g) := by intro x by_cases hx : x ∈ interior s · apply (hf x).congr_of_eventuallyEq filter_upwards [isOpen_interior.mem_nhds hx] with y hy rw [piecewise_eq_of_mem] apply interior_subset hy by_cases h'x : x ∈ closure s · have : x ∈ frontier s := ⟨h'x, hx⟩ apply (hf x).congr_of_eventuallyEq filter_upwards [hfg x this] with y hy simp [Set.piecewise, hy] · apply (hg x).congr_of_eventuallyEq filter_upwards [isClosed_closure.isOpen_compl.mem_nhds h'x] with y hy rw [piecewise_eq_of_notMem] contrapose! hy simpa using subset_closure hy /-- Given two `C^n` functions `f` and `g` from `ℝ` to a real manifold which coincide locally around a point `s`, then the piecewise function using `f` before `t` and `g` after is `C^n`. -/ lemma ContMDiff.piecewise_Iic {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners ℝ E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] {f g : ℝ → M} {s : ℝ} (hf : ContMDiff 𝓘(ℝ) I n f) (hg : ContMDiff 𝓘(ℝ) I n g) (hfg : f =ᶠ[𝓝 s] g) : ContMDiff 𝓘(ℝ) I n (Set.piecewise (Iic s) f g) := hf.piecewise hg (by simpa using hfg) /-! ### Being `C^k` on a union of open sets can be tested on each set -/ section contMDiff_union variable {s t : Set M} /-- If a function is `C^k` on two open sets, it is also `C^n` on their union. -/ lemma ContMDiffOn.union_of_isOpen (hf : ContMDiffOn I I' n f s) (hf' : ContMDiffOn I I' n f t) (hs : IsOpen s) (ht : IsOpen t) : ContMDiffOn I I' n f (s ∪ t) := by intro x hx obtain (hx | hx) := hx · exact (hf x hx).contMDiffAt (hs.mem_nhds hx) |>.contMDiffWithinAt · exact (hf' x hx).contMDiffAt (ht.mem_nhds hx) |>.contMDiffWithinAt /-- A function is `C^k` on two open sets iff it is `C^k` on their union. -/ lemma contMDiffOn_union_iff_of_isOpen (hs : IsOpen s) (ht : IsOpen t) : ContMDiffOn I I' n f (s ∪ t) ↔ ContMDiffOn I I' n f s ∧ ContMDiffOn I I' n f t := ⟨fun h ↦ ⟨h.mono subset_union_left, h.mono subset_union_right⟩, fun ⟨hfs, hft⟩ ↦ ContMDiffOn.union_of_isOpen hfs hft hs ht⟩ lemma contMDiff_of_contMDiffOn_union_of_isOpen (hf : ContMDiffOn I I' n f s) (hf' : ContMDiffOn I I' n f t) (hst : s ∪ t = univ) (hs : IsOpen s) (ht : IsOpen t) : ContMDiff I I' n f := by rw [← contMDiffOn_univ, ← hst] exact hf.union_of_isOpen hf' hs ht /-- If a function is `C^k` on open sets `s i`, it is `C^k` on their union -/ lemma ContMDiffOn.iUnion_of_isOpen {ι : Type*} {s : ι → Set M} (hf : ∀ i : ι, ContMDiffOn I I' n f (s i)) (hs : ∀ i, IsOpen (s i)) : ContMDiffOn I I' n f (⋃ i, s i) := by rintro x ⟨si, ⟨i, rfl⟩, hxsi⟩ exact (hf i).contMDiffAt ((hs i).mem_nhds hxsi) |>.contMDiffWithinAt /-- A function is `C^k` on a union of open sets `s i` iff it is `C^k` on each `s i`. -/ lemma contMDiffOn_iUnion_iff_of_isOpen {ι : Type*} {s : ι → Set M} (hs : ∀ i, IsOpen (s i)) : ContMDiffOn I I' n f (⋃ i, s i) ↔ ∀ i : ι, ContMDiffOn I I' n f (s i) := ⟨fun h i ↦ h.mono <| subset_iUnion_of_subset i fun _ a ↦ a, fun h ↦ ContMDiffOn.iUnion_of_isOpen h hs⟩ lemma contMDiff_of_contMDiffOn_iUnion_of_isOpen {ι : Type*} {s : ι → Set M} (hf : ∀ i : ι, ContMDiffOn I I' n f (s i)) (hs : ∀ i, IsOpen (s i)) (hs' : ⋃ i, s i = univ) : ContMDiff I I' n f := by rw [← contMDiffOn_univ, ← hs'] exact ContMDiffOn.iUnion_of_isOpen hf hs end contMDiff_union /-! ### The inclusion map from one open set to another is `C^n` -/ section Inclusion open TopologicalSpace theorem contMDiffAt_subtype_iff {n : WithTop ℕ∞} {U : Opens M} {f : M → M'} {x : U} : ContMDiffAt I I' n (fun x : U ↦ f x) x ↔ ContMDiffAt I I' n f x := ((contDiffWithinAt_localInvariantProp n).liftPropAt_iff_comp_subtype_val _ _).symm theorem contMDiff_subtype_val {n : WithTop ℕ∞} {U : Opens M} : ContMDiff I I n (Subtype.val : U → M) := fun _ ↦ contMDiffAt_subtype_iff.mpr contMDiffAt_id @[to_additive] theorem ContMDiff.extend_one [T2Space M] [One M'] {n : WithTop ℕ∞} {U : Opens M} {f : U → M'} (supp : HasCompactMulSupport f) (diff : ContMDiff I I' n f) : ContMDiff I I' n (Subtype.val.extend f 1) := fun x ↦ by refine contMDiff_of_mulTSupport (fun x h ↦ ?_) _ lift x to U using Subtype.coe_image_subset _ _ (supp.mulTSupport_extend_one_subset continuous_subtype_val h) rw [← contMDiffAt_subtype_iff] simp_rw [← comp_def] rw [extend_comp Subtype.val_injective] exact diff.contMDiffAt theorem contMDiff_inclusion {n : WithTop ℕ∞} {U V : Opens M} (h : U ≤ V) : ContMDiff I I n (Opens.inclusion h : U → V) := by rintro ⟨x, hx : x ∈ U⟩ apply (contDiffWithinAt_localInvariantProp n).liftProp_inclusion intro y dsimp only [ContDiffWithinAtProp, id_comp, preimage_univ] rw [Set.univ_inter] exact contDiffWithinAt_id.congr I.rightInvOn (congr_arg I (I.left_inv y)) end Inclusion end ChartedSpace /-! ### Open embeddings and their inverses are `C^n` -/ section variable {e : M → H} (h : IsOpenEmbedding e) {n : WithTop ℕ∞} /-- If the `ChartedSpace` structure on a manifold `M` is given by an open embedding `e : M → H`, then `e` is `C^n`. -/ lemma contMDiff_isOpenEmbedding [Nonempty M] : haveI := h.singletonChartedSpace; ContMDiff I I n e := by haveI := h.isManifold_singleton (I := I) (n := ω) rw [@contMDiff_iff _ _ _ _ _ _ _ _ _ _ h.singletonChartedSpace] use h.continuous intro x y -- show the function is actually the identity on the range of I ∘ e apply contDiffOn_id.congr intro z hz -- factorise into the chart `e` and the model `id` simp only [mfld_simps] rw [h.toOpenPartialHomeomorph_right_inv] · rw [I.right_inv] apply mem_of_subset_of_mem _ hz.1 exact letI := h.singletonChartedSpace; extChartAt_target_subset_range (I := I) x · -- `hz` implies that `z ∈ range (I ∘ e)` have := hz.1 rw [@extChartAt_target _ _ _ _ _ _ _ _ _ _ h.singletonChartedSpace] at this have := this.1 rw [mem_preimage, OpenPartialHomeomorph.singletonChartedSpace_chartAt_eq, h.toOpenPartialHomeomorph_target] at this exact this /-- If the `ChartedSpace` structure on a manifold `M` is given by an open embedding `e : M → H`, then the inverse of `e` is `C^n`. -/ lemma contMDiffOn_isOpenEmbedding_symm [Nonempty M] : haveI := h.singletonChartedSpace; ContMDiffOn I I n (IsOpenEmbedding.toOpenPartialHomeomorph e h).symm (range e) := by haveI := h.isManifold_singleton (I := I) (n := ω) rw [@contMDiffOn_iff] constructor · rw [← h.toOpenPartialHomeomorph_target] exact (h.toOpenPartialHomeomorph e).continuousOn_symm · intro z hz -- show the function is actually the identity on the range of I ∘ e apply contDiffOn_id.congr intro z hz -- factorise into the chart `e` and the model `id` simp only [mfld_simps] have : I.symm z ∈ range e := by rw [ModelWithCorners.symm, ← mem_preimage] exact hz.2.1 rw [h.toOpenPartialHomeomorph_right_inv e this] apply I.right_inv exact mem_of_subset_of_mem (extChartAt_target_subset_range _) hz.1 variable [ChartedSpace H M] variable [Nonempty M'] {e' : M' → H'} (h' : IsOpenEmbedding e') /-- Let `M'` be a manifold whose chart structure is given by an open embedding `e'` into its model space `H'`. If `e' ∘ f : M → H'` is `C^n`, then `f` is `C^n`. This is useful, for example, when `e' ∘ f = g ∘ e` for smooth maps `e : M → X` and `g : X → H'`. -/ lemma ContMDiff.of_comp_isOpenEmbedding {f : M → M'} (hf : ContMDiff I I' n (e' ∘ f)) : haveI := h'.singletonChartedSpace; ContMDiff I I' n f := by have : f = (h'.toOpenPartialHomeomorph e').symm ∘ e' ∘ f := by ext rw [Function.comp_apply, Function.comp_apply, IsOpenEmbedding.toOpenPartialHomeomorph_left_inv] rw [this] apply @ContMDiffOn.comp_contMDiff _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ h'.singletonChartedSpace _ _ (range e') _ (contMDiffOn_isOpenEmbedding_symm h') hf simp end
.lake/packages/mathlib/Mathlib/Geometry/Manifold/ContMDiff/Constructions.lean
import Mathlib.Geometry.Manifold.ContMDiff.Basic /-! ## Smoothness of standard maps associated to the product of manifolds This file contains results about smoothness of standard maps associated to products and sums (disjoint unions) of smooth manifolds: - if `f` and `g` are `C^n`, so is their point-wise product. - the component projections from a product of manifolds are smooth. - functions into a product (*pi type*) are `C^n` iff their components are - if `M` and `N` are manifolds modelled over the same space, `Sum.inl` and `Sum.inr` are `C^n`, as are `Sum.elim`, `Sum.map` and `Sum.swap`. -/ open Set Function Filter ChartedSpace open scoped Topology Manifold variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] -- declare a charted space `M` over the pair `(E, H)`. {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] -- declare a charted space `M'` over the pair `(E', H')`. {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] -- declare a charted space `N` over the pair `(F, G)`. {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {G : Type*} [TopologicalSpace G] {J : ModelWithCorners 𝕜 F G} {N : Type*} [TopologicalSpace N] [ChartedSpace G N] -- declare a charted space `N'` over the pair `(F', G')`. {F' : Type*} [NormedAddCommGroup F'] [NormedSpace 𝕜 F'] {G' : Type*} [TopologicalSpace G'] {J' : ModelWithCorners 𝕜 F' G'} {N' : Type*} [TopologicalSpace N'] [ChartedSpace G' N'] -- declare a few vector spaces {F₁ : Type*} [NormedAddCommGroup F₁] [NormedSpace 𝕜 F₁] {F₂ : Type*} [NormedAddCommGroup F₂] [NormedSpace 𝕜 F₂] -- declare functions, sets, points and smoothness indices {f : M → M'} {s : Set M} {x : M} {n : WithTop ℕ∞} section ProdMk theorem ContMDiffWithinAt.prodMk {f : M → M'} {g : M → N'} (hf : ContMDiffWithinAt I I' n f s x) (hg : ContMDiffWithinAt I J' n g s x) : ContMDiffWithinAt I (I'.prod J') n (fun x => (f x, g x)) s x := by rw [contMDiffWithinAt_iff] at * exact ⟨hf.1.prodMk hg.1, hf.2.prodMk hg.2⟩ theorem ContMDiffWithinAt.prodMk_space {f : M → E'} {g : M → F'} (hf : ContMDiffWithinAt I 𝓘(𝕜, E') n f s x) (hg : ContMDiffWithinAt I 𝓘(𝕜, F') n g s x) : ContMDiffWithinAt I 𝓘(𝕜, E' × F') n (fun x => (f x, g x)) s x := by rw [contMDiffWithinAt_iff] at * exact ⟨hf.1.prodMk hg.1, hf.2.prodMk hg.2⟩ nonrec theorem ContMDiffAt.prodMk {f : M → M'} {g : M → N'} (hf : ContMDiffAt I I' n f x) (hg : ContMDiffAt I J' n g x) : ContMDiffAt I (I'.prod J') n (fun x => (f x, g x)) x := hf.prodMk hg nonrec theorem ContMDiffAt.prodMk_space {f : M → E'} {g : M → F'} (hf : ContMDiffAt I 𝓘(𝕜, E') n f x) (hg : ContMDiffAt I 𝓘(𝕜, F') n g x) : ContMDiffAt I 𝓘(𝕜, E' × F') n (fun x => (f x, g x)) x := hf.prodMk_space hg theorem ContMDiffOn.prodMk {f : M → M'} {g : M → N'} (hf : ContMDiffOn I I' n f s) (hg : ContMDiffOn I J' n g s) : ContMDiffOn I (I'.prod J') n (fun x => (f x, g x)) s := fun x hx => (hf x hx).prodMk (hg x hx) theorem ContMDiffOn.prodMk_space {f : M → E'} {g : M → F'} (hf : ContMDiffOn I 𝓘(𝕜, E') n f s) (hg : ContMDiffOn I 𝓘(𝕜, F') n g s) : ContMDiffOn I 𝓘(𝕜, E' × F') n (fun x => (f x, g x)) s := fun x hx => (hf x hx).prodMk_space (hg x hx) nonrec theorem ContMDiff.prodMk {f : M → M'} {g : M → N'} (hf : ContMDiff I I' n f) (hg : ContMDiff I J' n g) : ContMDiff I (I'.prod J') n fun x => (f x, g x) := fun x => (hf x).prodMk (hg x) theorem ContMDiff.prodMk_space {f : M → E'} {g : M → F'} (hf : ContMDiff I 𝓘(𝕜, E') n f) (hg : ContMDiff I 𝓘(𝕜, F') n g) : ContMDiff I 𝓘(𝕜, E' × F') n fun x => (f x, g x) := fun x => (hf x).prodMk_space (hg x) end ProdMk section Projections theorem contMDiffWithinAt_fst {s : Set (M × N)} {p : M × N} : ContMDiffWithinAt (I.prod J) I n Prod.fst s p := by /- porting note: `simp` fails to apply lemmas to `ModelProd`. Was rw [contMDiffWithinAt_iff'] refine' ⟨continuousWithinAt_fst, _⟩ refine' contDiffWithinAt_fst.congr (fun y hy => _) _ · simp only [mfld_simps] at hy simp only [hy, mfld_simps] · simp only [mfld_simps] -/ rw [contMDiffWithinAt_iff'] refine ⟨continuousWithinAt_fst, contDiffWithinAt_fst.congr (fun y hy => ?_) ?_⟩ · exact (extChartAt I p.1).right_inv ⟨hy.1.1.1, hy.1.2.1⟩ · exact (extChartAt I p.1).right_inv <| (extChartAt I p.1).map_source (mem_extChartAt_source _) theorem ContMDiffWithinAt.fst {f : N → M × M'} {s : Set N} {x : N} (hf : ContMDiffWithinAt J (I.prod I') n f s x) : ContMDiffWithinAt J I n (fun x => (f x).1) s x := contMDiffWithinAt_fst.comp x hf (mapsTo_image f s) theorem contMDiffAt_fst {p : M × N} : ContMDiffAt (I.prod J) I n Prod.fst p := contMDiffWithinAt_fst theorem contMDiffOn_fst {s : Set (M × N)} : ContMDiffOn (I.prod J) I n Prod.fst s := fun _ _ => contMDiffWithinAt_fst theorem contMDiff_fst : ContMDiff (I.prod J) I n (@Prod.fst M N) := fun _ => contMDiffAt_fst theorem ContMDiffAt.fst {f : N → M × M'} {x : N} (hf : ContMDiffAt J (I.prod I') n f x) : ContMDiffAt J I n (fun x => (f x).1) x := contMDiffAt_fst.comp x hf theorem ContMDiff.fst {f : N → M × M'} (hf : ContMDiff J (I.prod I') n f) : ContMDiff J I n fun x => (f x).1 := contMDiff_fst.comp hf theorem contMDiffWithinAt_snd {s : Set (M × N)} {p : M × N} : ContMDiffWithinAt (I.prod J) J n Prod.snd s p := by /- porting note: `simp` fails to apply lemmas to `ModelProd`. Was rw [contMDiffWithinAt_iff'] refine' ⟨continuousWithinAt_snd, _⟩ refine' contDiffWithinAt_snd.congr (fun y hy => _) _ · simp only [mfld_simps] at hy simp only [hy, mfld_simps] · simp only [mfld_simps] -/ rw [contMDiffWithinAt_iff'] refine ⟨continuousWithinAt_snd, contDiffWithinAt_snd.congr (fun y hy => ?_) ?_⟩ · exact (extChartAt J p.2).right_inv ⟨hy.1.1.2, hy.1.2.2⟩ · exact (extChartAt J p.2).right_inv <| (extChartAt J p.2).map_source (mem_extChartAt_source _) theorem ContMDiffWithinAt.snd {f : N → M × M'} {s : Set N} {x : N} (hf : ContMDiffWithinAt J (I.prod I') n f s x) : ContMDiffWithinAt J I' n (fun x => (f x).2) s x := contMDiffWithinAt_snd.comp x hf (mapsTo_image f s) theorem contMDiffAt_snd {p : M × N} : ContMDiffAt (I.prod J) J n Prod.snd p := contMDiffWithinAt_snd theorem contMDiffOn_snd {s : Set (M × N)} : ContMDiffOn (I.prod J) J n Prod.snd s := fun _ _ => contMDiffWithinAt_snd theorem contMDiff_snd : ContMDiff (I.prod J) J n (@Prod.snd M N) := fun _ => contMDiffAt_snd theorem ContMDiffAt.snd {f : N → M × M'} {x : N} (hf : ContMDiffAt J (I.prod I') n f x) : ContMDiffAt J I' n (fun x => (f x).2) x := contMDiffAt_snd.comp x hf theorem ContMDiff.snd {f : N → M × M'} (hf : ContMDiff J (I.prod I') n f) : ContMDiff J I' n fun x => (f x).2 := contMDiff_snd.comp hf end Projections theorem contMDiffWithinAt_prod_iff (f : M → M' × N') : ContMDiffWithinAt I (I'.prod J') n f s x ↔ ContMDiffWithinAt I I' n (Prod.fst ∘ f) s x ∧ ContMDiffWithinAt I J' n (Prod.snd ∘ f) s x := ⟨fun h => ⟨h.fst, h.snd⟩, fun h => h.1.prodMk h.2⟩ theorem contMDiffWithinAt_prod_module_iff (f : M → F₁ × F₂) : ContMDiffWithinAt I 𝓘(𝕜, F₁ × F₂) n f s x ↔ ContMDiffWithinAt I 𝓘(𝕜, F₁) n (Prod.fst ∘ f) s x ∧ ContMDiffWithinAt I 𝓘(𝕜, F₂) n (Prod.snd ∘ f) s x := by rw [modelWithCornersSelf_prod, ← chartedSpaceSelf_prod] exact contMDiffWithinAt_prod_iff f theorem contMDiffAt_prod_iff (f : M → M' × N') : ContMDiffAt I (I'.prod J') n f x ↔ ContMDiffAt I I' n (Prod.fst ∘ f) x ∧ ContMDiffAt I J' n (Prod.snd ∘ f) x := by simp_rw [← contMDiffWithinAt_univ]; exact contMDiffWithinAt_prod_iff f theorem contMDiffAt_prod_module_iff (f : M → F₁ × F₂) : ContMDiffAt I 𝓘(𝕜, F₁ × F₂) n f x ↔ ContMDiffAt I 𝓘(𝕜, F₁) n (Prod.fst ∘ f) x ∧ ContMDiffAt I 𝓘(𝕜, F₂) n (Prod.snd ∘ f) x := by rw [modelWithCornersSelf_prod, ← chartedSpaceSelf_prod] exact contMDiffAt_prod_iff f theorem contMDiffOn_prod_iff (f : M → M' × N') : ContMDiffOn I (I'.prod J') n f s ↔ ContMDiffOn I I' n (Prod.fst ∘ f) s ∧ ContMDiffOn I J' n (Prod.snd ∘ f) s := ⟨fun h ↦ ⟨fun x hx ↦ ((contMDiffWithinAt_prod_iff f).1 (h x hx)).1, fun x hx ↦ ((contMDiffWithinAt_prod_iff f).1 (h x hx)).2⟩, fun h x hx ↦ (contMDiffWithinAt_prod_iff f).2 ⟨h.1 x hx, h.2 x hx⟩⟩ theorem contMDiffOn_prod_module_iff (f : M → F₁ × F₂) : ContMDiffOn I 𝓘(𝕜, F₁ × F₂) n f s ↔ ContMDiffOn I 𝓘(𝕜, F₁) n (Prod.fst ∘ f) s ∧ ContMDiffOn I 𝓘(𝕜, F₂) n (Prod.snd ∘ f) s := by rw [modelWithCornersSelf_prod, ← chartedSpaceSelf_prod] exact contMDiffOn_prod_iff f theorem contMDiff_prod_iff (f : M → M' × N') : ContMDiff I (I'.prod J') n f ↔ ContMDiff I I' n (Prod.fst ∘ f) ∧ ContMDiff I J' n (Prod.snd ∘ f) := ⟨fun h => ⟨h.fst, h.snd⟩, fun h => by convert h.1.prodMk h.2⟩ theorem contMDiff_prod_module_iff (f : M → F₁ × F₂) : ContMDiff I 𝓘(𝕜, F₁ × F₂) n f ↔ ContMDiff I 𝓘(𝕜, F₁) n (Prod.fst ∘ f) ∧ ContMDiff I 𝓘(𝕜, F₂) n (Prod.snd ∘ f) := by rw [modelWithCornersSelf_prod, ← chartedSpaceSelf_prod] exact contMDiff_prod_iff f theorem contMDiff_prod_assoc : ContMDiff ((I.prod I').prod J) (I.prod (I'.prod J)) n fun x : (M × M') × N => (x.1.1, x.1.2, x.2) := contMDiff_fst.fst.prodMk <| contMDiff_fst.snd.prodMk contMDiff_snd /-- `ContMDiffWithinAt.comp` for a function of two arguments. -/ theorem ContMDiffWithinAt.comp₂ {h : M' × N' → N} {f : M → M'} {g : M → N'} {x : M} {t : Set (M' × N')} (ha : ContMDiffWithinAt (I'.prod J') J n h t (f x, g x)) (fa : ContMDiffWithinAt I I' n f s x) (ga : ContMDiffWithinAt I J' n g s x) (st : MapsTo (fun x ↦ (f x, g x)) s t) : ContMDiffWithinAt I J n (fun x ↦ h (f x, g x)) s x := ha.comp (f := fun x ↦ (f x, g x)) _ (fa.prodMk ga) st /-- `ContMDiffWithinAt.comp₂`, with a separate argument for point equality. -/ theorem ContMDiffWithinAt.comp₂_of_eq {h : M' × N' → N} {f : M → M'} {g : M → N'} {x : M} {y : M' × N'} {t : Set (M' × N')} (ha : ContMDiffWithinAt (I'.prod J') J n h t y) (fa : ContMDiffWithinAt I I' n f s x) (ga : ContMDiffWithinAt I J' n g s x) (e : (f x, g x) = y) (st : MapsTo (fun x ↦ (f x, g x)) s t) : ContMDiffWithinAt I J n (fun x ↦ h (f x, g x)) s x := by rw [← e] at ha exact ha.comp₂ fa ga st /-- `ContMDiffAt.comp` for a function of two arguments. -/ theorem ContMDiffAt.comp₂ {h : M' × N' → N} {f : M → M'} {g : M → N'} {x : M} (ha : ContMDiffAt (I'.prod J') J n h (f x, g x)) (fa : ContMDiffAt I I' n f x) (ga : ContMDiffAt I J' n g x) : ContMDiffAt I J n (fun x ↦ h (f x, g x)) x := ha.comp (f := fun x ↦ (f x, g x)) _ (fa.prodMk ga) /-- `ContMDiffAt.comp₂`, with a separate argument for point equality. -/ theorem ContMDiffAt.comp₂_of_eq {h : M' × N' → N} {f : M → M'} {g : M → N'} {x : M} {y : M' × N'} (ha : ContMDiffAt (I'.prod J') J n h y) (fa : ContMDiffAt I I' n f x) (ga : ContMDiffAt I J' n g x) (e : (f x, g x) = y) : ContMDiffAt I J n (fun x ↦ h (f x, g x)) x := by rw [← e] at ha exact ha.comp₂ fa ga /-- Curried `C^n` functions are `C^n` in the first coordinate. -/ theorem ContMDiffWithinAt.curry_left {f : M → M' → N} {x : M} {y : M'} {s : Set (M × M')} (fa : ContMDiffWithinAt (I.prod I') J n (uncurry f) s (x, y)) : ContMDiffWithinAt I J n (fun x ↦ f x y) {x | (x, y) ∈ s} x := fa.comp₂ contMDiffWithinAt_id contMDiffWithinAt_const (fun _ h ↦ h) alias ContMDiffWithinAt.along_fst := ContMDiffWithinAt.curry_left /-- Curried `C^n` functions are `C^n` in the second coordinate. -/ theorem ContMDiffWithinAt.curry_right {f : M → M' → N} {x : M} {y : M'} {s : Set (M × M')} (fa : ContMDiffWithinAt (I.prod I') J n (uncurry f) s (x, y)) : ContMDiffWithinAt I' J n (fun y ↦ f x y) {y | (x, y) ∈ s} y := fa.comp₂ contMDiffWithinAt_const contMDiffWithinAt_id (fun _ h ↦ h) alias ContMDiffWithinAt.along_snd := ContMDiffWithinAt.curry_right /-- Curried `C^n` functions are `C^n` in the first coordinate. -/ theorem ContMDiffAt.curry_left {f : M → M' → N} {x : M} {y : M'} (fa : ContMDiffAt (I.prod I') J n (uncurry f) (x, y)) : ContMDiffAt I J n (fun x ↦ f x y) x := fa.comp₂ contMDiffAt_id contMDiffAt_const alias ContMDiffAt.along_fst := ContMDiffAt.curry_left /-- Curried `C^n` functions are `C^n` in the second coordinate. -/ theorem ContMDiffAt.curry_right {f : M → M' → N} {x : M} {y : M'} (fa : ContMDiffAt (I.prod I') J n (uncurry f) (x, y)) : ContMDiffAt I' J n (fun y ↦ f x y) y := fa.comp₂ contMDiffAt_const contMDiffAt_id alias ContMDiffAt.along_snd := ContMDiffAt.curry_right /-- Curried `C^n` functions are `C^n` in the first coordinate. -/ theorem ContMDiffOn.curry_left {f : M → M' → N} {s : Set (M × M')} (fa : ContMDiffOn (I.prod I') J n (uncurry f) s) {y : M'} : ContMDiffOn I J n (fun x ↦ f x y) {x | (x, y) ∈ s} := fun x m ↦ (fa (x, y) m).along_fst alias ContMDiffOn.along_fst := ContMDiffOn.curry_left /-- Curried `C^n` functions are `C^n` in the second coordinate. -/ theorem ContMDiffOn.curry_right {f : M → M' → N} {x : M} {s : Set (M × M')} (fa : ContMDiffOn (I.prod I') J n (uncurry f) s) : ContMDiffOn I' J n (fun y ↦ f x y) {y | (x, y) ∈ s} := fun y m ↦ (fa (x, y) m).along_snd alias ContMDiffOn.along_snd := ContMDiffOn.curry_right /-- Curried `C^n` functions are `C^n` in the first coordinate. -/ theorem ContMDiff.curry_left {f : M → M' → N} (fa : ContMDiff (I.prod I') J n (uncurry f)) {y : M'} : ContMDiff I J n (fun x ↦ f x y) := fun _ ↦ (fa _).along_fst alias ContMDiff.along_fst := ContMDiff.curry_left /-- Curried `C^n` functions are `C^n` in the second coordinate. -/ theorem ContMDiff.curry_right {f : M → M' → N} {x : M} (fa : ContMDiff (I.prod I') J n (uncurry f)) : ContMDiff I' J n (fun y ↦ f x y) := fun _ ↦ (fa _).along_snd alias ContMDiff.along_snd := ContMDiff.curry_right section prodMap variable {g : N → N'} {r : Set N} {y : N} /-- The product map of two `C^n` functions within a set at a point is `C^n` within the product set at the product point. -/ theorem ContMDiffWithinAt.prodMap' {p : M × N} (hf : ContMDiffWithinAt I I' n f s p.1) (hg : ContMDiffWithinAt J J' n g r p.2) : ContMDiffWithinAt (I.prod J) (I'.prod J') n (Prod.map f g) (s ×ˢ r) p := (hf.comp p contMDiffWithinAt_fst mapsTo_fst_prod).prodMk <| hg.comp p contMDiffWithinAt_snd mapsTo_snd_prod theorem ContMDiffWithinAt.prodMap (hf : ContMDiffWithinAt I I' n f s x) (hg : ContMDiffWithinAt J J' n g r y) : ContMDiffWithinAt (I.prod J) (I'.prod J') n (Prod.map f g) (s ×ˢ r) (x, y) := ContMDiffWithinAt.prodMap' hf hg theorem ContMDiffAt.prodMap (hf : ContMDiffAt I I' n f x) (hg : ContMDiffAt J J' n g y) : ContMDiffAt (I.prod J) (I'.prod J') n (Prod.map f g) (x, y) := by simp only [← contMDiffWithinAt_univ, ← univ_prod_univ] at * exact hf.prodMap hg theorem ContMDiffAt.prodMap' {p : M × N} (hf : ContMDiffAt I I' n f p.1) (hg : ContMDiffAt J J' n g p.2) : ContMDiffAt (I.prod J) (I'.prod J') n (Prod.map f g) p := hf.prodMap hg theorem ContMDiffOn.prodMap (hf : ContMDiffOn I I' n f s) (hg : ContMDiffOn J J' n g r) : ContMDiffOn (I.prod J) (I'.prod J') n (Prod.map f g) (s ×ˢ r) := (hf.comp contMDiffOn_fst mapsTo_fst_prod).prodMk <| hg.comp contMDiffOn_snd mapsTo_snd_prod theorem ContMDiff.prodMap (hf : ContMDiff I I' n f) (hg : ContMDiff J J' n g) : ContMDiff (I.prod J) (I'.prod J') n (Prod.map f g) := by intro p exact (hf p.1).prodMap' (hg p.2) end prodMap section PiSpace /-! ### Regularity of functions with codomain `Π i, F i` We have no `ModelWithCorners.pi` yet, so we prove lemmas about functions `f : M → Π i, F i` and use `𝓘(𝕜, Π i, F i)` as the model space. -/ variable {ι : Type*} [Fintype ι] {Fi : ι → Type*} [∀ i, NormedAddCommGroup (Fi i)] [∀ i, NormedSpace 𝕜 (Fi i)] {φ : M → ∀ i, Fi i} theorem contMDiffWithinAt_pi_space : ContMDiffWithinAt I 𝓘(𝕜, ∀ i, Fi i) n φ s x ↔ ∀ i, ContMDiffWithinAt I 𝓘(𝕜, Fi i) n (fun x => φ x i) s x := by simp only [contMDiffWithinAt_iff, continuousWithinAt_pi, contDiffWithinAt_pi, forall_and, extChartAt_model_space_eq_id, Function.comp_def, PartialEquiv.refl_coe, id] theorem contMDiffOn_pi_space : ContMDiffOn I 𝓘(𝕜, ∀ i, Fi i) n φ s ↔ ∀ i, ContMDiffOn I 𝓘(𝕜, Fi i) n (fun x => φ x i) s := ⟨fun h i x hx => contMDiffWithinAt_pi_space.1 (h x hx) i, fun h x hx => contMDiffWithinAt_pi_space.2 fun i => h i x hx⟩ theorem contMDiffAt_pi_space : ContMDiffAt I 𝓘(𝕜, ∀ i, Fi i) n φ x ↔ ∀ i, ContMDiffAt I 𝓘(𝕜, Fi i) n (fun x => φ x i) x := contMDiffWithinAt_pi_space theorem contMDiff_pi_space : ContMDiff I 𝓘(𝕜, ∀ i, Fi i) n φ ↔ ∀ i, ContMDiff I 𝓘(𝕜, Fi i) n fun x => φ x i := ⟨fun h i x => contMDiffAt_pi_space.1 (h x) i, fun h x => contMDiffAt_pi_space.2 fun i => h i x⟩ end PiSpace section disjointUnion variable {M' : Type*} [TopologicalSpace M'] [ChartedSpace H M'] {n : WithTop ℕ∞} {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {J : Type*} {J : ModelWithCorners 𝕜 E' H'} {N N' : Type*} [TopologicalSpace N] [TopologicalSpace N'] [ChartedSpace H' N] [ChartedSpace H' N'] open Topology lemma ContMDiff.inl : ContMDiff I I n (@Sum.inl M M') := by intro x rw [contMDiffAt_iff] refine ⟨continuous_inl.continuousAt, ?_⟩ -- In extended charts, .inl equals the identity (on the chart sources). apply contDiffWithinAt_id.congr_of_eventuallyEq; swap · simp [ChartedSpace.sum_chartAt_inl] congr apply Sum.inl_injective.extend_apply (chartAt _ x) set C := chartAt H x with hC have : I.symm ⁻¹' C.target ∩ range I ∈ 𝓝[range I] (extChartAt I x) x := by rw [← I.image_eq (chartAt H x).target] exact (chartAt H x).extend_image_target_mem_nhds (mem_chart_source _ x) filter_upwards [this] with y hy simp [extChartAt, sum_chartAt_inl, ← hC, Sum.inl_injective.extend_apply C, C.right_inv hy.1, I.right_inv hy.2] lemma ContMDiff.inr : ContMDiff I I n (@Sum.inr M M') := by intro x rw [contMDiffAt_iff] refine ⟨continuous_inr.continuousAt, ?_⟩ -- In extended charts, .inl equals the identity (on the chart sources). apply contDiffWithinAt_id.congr_of_eventuallyEq; swap · simp only [mfld_simps, sum_chartAt_inr] congr apply Sum.inr_injective.extend_apply (chartAt _ x) set C := chartAt H x with hC have : I.symm ⁻¹' (chartAt H x).target ∩ range I ∈ 𝓝[range I] (extChartAt I x) x := by rw [← I.image_eq (chartAt H x).target] exact (chartAt H x).extend_image_target_mem_nhds (mem_chart_source _ x) filter_upwards [this] with y hy simp [extChartAt, sum_chartAt_inr, ← hC, Sum.inr_injective.extend_apply C, C.right_inv hy.1, I.right_inv hy.2] lemma extChartAt_inl_apply {x y : M} : (extChartAt I (.inl x : M ⊕ M')) (Sum.inl y) = (extChartAt I x) y := by simp lemma extChartAt_inr_apply {x y : M'} : (extChartAt I (.inr x : M ⊕ M')) (Sum.inr y) = (extChartAt I x) y := by simp lemma ContMDiff.sumElim {f : M → N} {g : M' → N} (hf : ContMDiff I J n f) (hg : ContMDiff I J n g) : ContMDiff I J n (Sum.elim f g) := by intro p rw [contMDiffAt_iff] refine ⟨(Continuous.sumElim hf.continuous hg.continuous).continuousAt, ?_⟩ cases p with | inl x => -- In charts around x : M, the map f ⊔ g looks like f. -- This is how they both look like in extended charts. have : ContDiffWithinAt 𝕜 n ((extChartAt J (f x)) ∘ f ∘ (extChartAt I x).symm) (range I) ((extChartAt I (.inl x : M ⊕ M')) (Sum.inl x)) := by let hf' := hf x rw [contMDiffAt_iff] at hf' simpa using hf'.2 apply this.congr_of_eventuallyEq · simp only [extChartAt, Sum.elim_inl, ChartedSpace.sum_chartAt_inl] filter_upwards with a congr · -- They agree at the image of x. simp only [extChartAt, ChartedSpace.sum_chartAt_inl, Sum.elim_inl] congr | inr x => -- In charts around x : M, the map f ⊔ g looks like g. -- This is how they both look like in extended charts. have : ContDiffWithinAt 𝕜 n ((extChartAt J (g x)) ∘ g ∘ (extChartAt I x).symm) (range I) ((extChartAt I (.inr x : M ⊕ M')) (Sum.inr x)) := by let hg' := hg x rw [contMDiffAt_iff] at hg' simpa using hg'.2 apply this.congr_of_eventuallyEq · simp only [extChartAt, Sum.elim_inr, ChartedSpace.sum_chartAt_inr] filter_upwards with a congr · -- They agree at the image of x. simp only [extChartAt, ChartedSpace.sum_chartAt_inr, Sum.elim_inr] congr lemma ContMDiff.sumMap {f : M → N} {g : M' → N'} (hf : ContMDiff I J n f) (hg : ContMDiff I J n g) : ContMDiff I J n (Sum.map f g) := ContMDiff.sumElim (ContMDiff.inl.comp hf) (ContMDiff.inr.comp hg) lemma contMDiff_of_contMDiff_inl {f : M → N} (h : ContMDiff I J n ((@Sum.inl N N') ∘ f)) : ContMDiff I J n f := by nontriviality N inhabit N let aux : N ⊕ N' → N := Sum.elim (@id N) (fun _ ↦ inhabited_h.default) have : aux ∘ (@Sum.inl N N') ∘ f = f := by ext; simp [aux] rw [← this] rw [← contMDiffOn_univ] at h ⊢ apply (contMDiff_id.sumElim contMDiff_const).contMDiffOn (s := @Sum.inl N N' '' univ).comp h intro x _hx rw [mem_preimage, Function.comp_apply] use f x, trivial lemma contMDiff_of_contMDiff_inr {g : M' → N'} (h : ContMDiff I J n ((@Sum.inr N N') ∘ g)) : ContMDiff I J n g := by nontriviality N' inhabit N' let aux : N ⊕ N' → N' := Sum.elim (fun _ ↦ inhabited_h.default) (@id N') have : aux ∘ (@Sum.inr N N') ∘ g = g := by ext; simp [aux] rw [← this] rw [← contMDiffOn_univ] at h ⊢ apply ((contMDiff_const.sumElim contMDiff_id).contMDiffOn (s := Sum.inr '' univ)).comp h intro x _hx rw [mem_preimage, Function.comp_apply] use g x, trivial lemma contMDiff_sum_map {f : M → N} {g : M' → N'} : ContMDiff I J n (Sum.map f g) ↔ ContMDiff I J n f ∧ ContMDiff I J n g := ⟨fun h ↦ ⟨contMDiff_of_contMDiff_inl (h.comp ContMDiff.inl), contMDiff_of_contMDiff_inr (h.comp ContMDiff.inr)⟩, fun h ↦ ContMDiff.sumMap h.1 h.2⟩ lemma ContMDiff.swap : ContMDiff I I n (@Sum.swap M M') := ContMDiff.sumElim inr inl end disjointUnion
.lake/packages/mathlib/Mathlib/Geometry/Manifold/ContMDiff/Defs.lean
import Mathlib.Geometry.Manifold.IsManifold.ExtChartAt import Mathlib.Geometry.Manifold.LocalInvariantProperties /-! # `C^n` functions between manifolds We define `Cⁿ` functions between manifolds, as functions which are `Cⁿ` in charts, and prove basic properties of these notions. Here, `n` can be finite, or `∞`, or `ω`. ## Main definitions and statements Let `M` and `M'` be two manifolds, with respect to models with corners `I` and `I'`. Let `f : M → M'`. * `ContMDiffWithinAt I I' n f s x` states that the function `f` is `Cⁿ` within the set `s` around the point `x`. * `ContMDiffAt I I' n f x` states that the function `f` is `Cⁿ` around `x`. * `ContMDiffOn I I' n f s` states that the function `f` is `Cⁿ` on the set `s` * `ContMDiff I I' n f` states that the function `f` is `Cⁿ`. We also give some basic properties of `Cⁿ` functions between manifolds, following the API of `C^n` functions between vector spaces. See `Basic.lean` for further basic properties of `Cⁿ` functions between manifolds, `NormedSpace.lean` for the equivalence of manifold-smoothness to usual smoothness, `Product.lean` for smoothness results related to the product of manifolds and `Atlas.lean` for smoothness of atlas members and local structomorphisms. ## Implementation details Many properties follow for free from the corresponding properties of functions in vector spaces, as being `Cⁿ` is a local property invariant under the `Cⁿ` groupoid. We take advantage of the general machinery developed in `LocalInvariantProperties.lean` to get these properties automatically. For instance, the fact that being `Cⁿ` does not depend on the chart one considers is given by `liftPropWithinAt_indep_chart`. For this to work, the definition of `ContMDiffWithinAt` and friends has to follow definitionally the setup of local invariant properties. Still, we recast the definition in terms of extended charts in `contMDiffOn_iff` and `contMDiff_iff`. -/ open Set Function Filter ChartedSpace IsManifold open scoped Topology Manifold ContDiff /-! ### Definition of `Cⁿ` functions between manifolds -/ variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] -- Prerequisite typeclasses to say that `M` is a manifold over the pair `(E, H)` {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] -- Prerequisite typeclasses to say that `M'` is a manifold over the pair `(E', H')` {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M'] -- Prerequisite typeclasses to say that `M''` is a manifold over the pair `(E'', H'')` {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace 𝕜 E''] {H'' : Type*} [TopologicalSpace H''] {I'' : ModelWithCorners 𝕜 E'' H''} {M'' : Type*} [TopologicalSpace M''] [ChartedSpace H'' M''] -- declare functions, sets, points and smoothness indices {e : OpenPartialHomeomorph M H} {e' : OpenPartialHomeomorph M' H'} {f f₁ : M → M'} {s s₁ t : Set M} {x : M} {m n : WithTop ℕ∞} variable (I I') in /-- Property in the model space of a model with corners of being `C^n` within at set at a point, when read in the model vector space. This property will be lifted to manifolds to define `C^n` functions between manifolds. -/ def ContDiffWithinAtProp (n : WithTop ℕ∞) (f : H → H') (s : Set H) (x : H) : Prop := ContDiffWithinAt 𝕜 n (I' ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) theorem contDiffWithinAtProp_self_source {f : E → H'} {s : Set E} {x : E} : ContDiffWithinAtProp 𝓘(𝕜, E) I' n f s x ↔ ContDiffWithinAt 𝕜 n (I' ∘ f) s x := by simp_rw [ContDiffWithinAtProp, modelWithCornersSelf_coe, range_id, inter_univ, modelWithCornersSelf_coe_symm, CompTriple.comp_eq, preimage_id_eq, id_eq] theorem contDiffWithinAtProp_self {f : E → E'} {s : Set E} {x : E} : ContDiffWithinAtProp 𝓘(𝕜, E) 𝓘(𝕜, E') n f s x ↔ ContDiffWithinAt 𝕜 n f s x := contDiffWithinAtProp_self_source theorem contDiffWithinAtProp_self_target {f : H → E'} {s : Set H} {x : H} : ContDiffWithinAtProp I 𝓘(𝕜, E') n f s x ↔ ContDiffWithinAt 𝕜 n (f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) := Iff.rfl /-- Being `Cⁿ` in the model space is a local property, invariant under `Cⁿ` maps. Therefore, it lifts nicely to manifolds. -/ theorem contDiffWithinAt_localInvariantProp_of_le (n m : WithTop ℕ∞) (hmn : m ≤ n) : (contDiffGroupoid n I).LocalInvariantProp (contDiffGroupoid n I') (ContDiffWithinAtProp I I' m) where is_local {s x u f} u_open xu := by have : I.symm ⁻¹' (s ∩ u) ∩ range I = I.symm ⁻¹' s ∩ range I ∩ I.symm ⁻¹' u := by simp only [inter_right_comm, preimage_inter] rw [ContDiffWithinAtProp, ContDiffWithinAtProp, this] symm apply contDiffWithinAt_inter have : u ∈ 𝓝 (I.symm (I x)) := by rw [ModelWithCorners.left_inv] exact u_open.mem_nhds xu apply ContinuousAt.preimage_mem_nhds I.continuous_symm.continuousAt this right_invariance' {s x f e} he hx h := by rw [ContDiffWithinAtProp] at h ⊢ have : I x = (I ∘ e.symm ∘ I.symm) (I (e x)) := by simp only [hx, mfld_simps] rw [this] at h have : I (e x) ∈ I.symm ⁻¹' e.target ∩ range I := by simp only [hx, mfld_simps] have := (mem_groupoid_of_pregroupoid.2 he).2.contDiffWithinAt this convert (h.comp_inter _ (this.of_le hmn)).mono_of_mem_nhdsWithin _ using 1 · ext y; simp only [mfld_simps] refine mem_nhdsWithin.mpr ⟨I.symm ⁻¹' e.target, e.open_target.preimage I.continuous_symm, by simp_rw [mem_preimage, I.left_inv, e.mapsTo hx], ?_⟩ mfld_set_tac congr_of_forall {s x f g} h hx hf := by apply hf.congr · intro y hy simp only [mfld_simps] at hy simp only [h, hy, mfld_simps] · simp only [hx, mfld_simps] left_invariance' {s x f e'} he' hs hx h := by rw [ContDiffWithinAtProp] at h ⊢ have A : (I' ∘ f ∘ I.symm) (I x) ∈ I'.symm ⁻¹' e'.source ∩ range I' := by simp only [hx, mfld_simps] have := (mem_groupoid_of_pregroupoid.2 he').1.contDiffWithinAt A convert (this.of_le hmn).comp _ h _ · ext y; simp only [mfld_simps] · intro y hy; simp only [mfld_simps] at hy; simpa only [hy, mfld_simps] using hs hy.1 /-- Being `Cⁿ` in the model space is a local property, invariant under `C^n` maps. Therefore, it lifts nicely to manifolds. -/ theorem contDiffWithinAt_localInvariantProp (n : WithTop ℕ∞) : (contDiffGroupoid n I).LocalInvariantProp (contDiffGroupoid n I') (ContDiffWithinAtProp I I' n) := contDiffWithinAt_localInvariantProp_of_le n n le_rfl theorem contDiffWithinAtProp_mono_of_mem_nhdsWithin (n : WithTop ℕ∞) ⦃s x t⦄ ⦃f : H → H'⦄ (hts : s ∈ 𝓝[t] x) (h : ContDiffWithinAtProp I I' n f s x) : ContDiffWithinAtProp I I' n f t x := by refine h.mono_of_mem_nhdsWithin ?_ refine inter_mem ?_ (mem_of_superset self_mem_nhdsWithin inter_subset_right) rwa [← Filter.mem_map, ← I.image_eq, I.symm_map_nhdsWithin_image] theorem contDiffWithinAtProp_id (x : H) : ContDiffWithinAtProp I I n id univ x := by simp only [ContDiffWithinAtProp, id_comp, preimage_univ, univ_inter] have : ContDiffWithinAt 𝕜 n id (range I) (I x) := contDiff_id.contDiffAt.contDiffWithinAt refine this.congr (fun y hy => ?_) ?_ · simp only [ModelWithCorners.right_inv I hy, mfld_simps] · simp only [mfld_simps] variable (I I') in /-- A function is `n` times continuously differentiable within a set at a point in a manifold if it is continuous and it is `n` times continuously differentiable in this set around this point, when read in the preferred chart at this point. -/ def ContMDiffWithinAt (n : WithTop ℕ∞) (f : M → M') (s : Set M) (x : M) := LiftPropWithinAt (ContDiffWithinAtProp I I' n) f s x variable (I I') in /-- A function is `n` times continuously differentiable at a point in a manifold if it is continuous and it is `n` times continuously differentiable around this point, when read in the preferred chart at this point. -/ def ContMDiffAt (n : WithTop ℕ∞) (f : M → M') (x : M) := ContMDiffWithinAt I I' n f univ x theorem contMDiffAt_iff {n : WithTop ℕ∞} {f : M → M'} {x : M} : ContMDiffAt I I' n f x ↔ ContinuousAt f x ∧ ContDiffWithinAt 𝕜 n (extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm) (range I) (extChartAt I x x) := liftPropAt_iff.trans <| by rw [ContDiffWithinAtProp, preimage_univ, univ_inter]; rfl variable (I I') in /-- A function is `n` times continuously differentiable in a set of a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable on this set in the charts around these points. -/ def ContMDiffOn (n : WithTop ℕ∞) (f : M → M') (s : Set M) := ∀ x ∈ s, ContMDiffWithinAt I I' n f s x variable (I I') in /-- A function is `n` times continuously differentiable in a manifold if it is continuous and, for any pair of points, it is `n` times continuously differentiable in the charts around these points. -/ def ContMDiff (n : WithTop ℕ∞) (f : M → M') := ∀ x, ContMDiffAt I I' n f x /-! ### Deducing smoothness from higher smoothness -/ theorem ContMDiffWithinAt.of_le (hf : ContMDiffWithinAt I I' n f s x) (le : m ≤ n) : ContMDiffWithinAt I I' m f s x := by simp only [ContMDiffWithinAt] at hf ⊢ exact ⟨hf.1, hf.2.of_le (mod_cast le)⟩ theorem ContMDiffAt.of_le (hf : ContMDiffAt I I' n f x) (le : m ≤ n) : ContMDiffAt I I' m f x := ContMDiffWithinAt.of_le hf le theorem ContMDiffOn.of_le (hf : ContMDiffOn I I' n f s) (le : m ≤ n) : ContMDiffOn I I' m f s := fun x hx => (hf x hx).of_le le theorem ContMDiff.of_le (hf : ContMDiff I I' n f) (le : m ≤ n) : ContMDiff I I' m f := fun x => (hf x).of_le le /-! ### Basic properties of `C^n` functions between manifolds -/ theorem ContMDiff.contMDiffAt (h : ContMDiff I I' n f) : ContMDiffAt I I' n f x := h x theorem contMDiffWithinAt_univ : ContMDiffWithinAt I I' n f univ x ↔ ContMDiffAt I I' n f x := Iff.rfl @[simp] theorem contMDiffOn_empty : ContMDiffOn I I' n f ∅ := fun _x hx ↦ hx.elim theorem contMDiffOn_univ : ContMDiffOn I I' n f univ ↔ ContMDiff I I' n f := by simp only [ContMDiffOn, ContMDiff, contMDiffWithinAt_univ, forall_prop_of_true, mem_univ] /-- One can reformulate being `C^n` within a set at a point as continuity within this set at this point, and being `C^n` in the corresponding extended chart. -/ theorem contMDiffWithinAt_iff : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContDiffWithinAt 𝕜 n (extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x) := by simp_rw [ContMDiffWithinAt, liftPropWithinAt_iff']; rfl /-- One can reformulate being `Cⁿ` within a set at a point as continuity within this set at this point, and being `Cⁿ` in the corresponding extended chart. This form states regularity of `f` written in such a way that the set is restricted to lie within the domain/codomain of the corresponding charts. Even though this expression is more complicated than the one in `contMDiffWithinAt_iff`, it is a smaller set, but their germs at `extChartAt I x x` are equal. It is sometimes useful to rewrite using this in the goal. -/ theorem contMDiffWithinAt_iff' : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContDiffWithinAt 𝕜 n (extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' (f x)).source)) (extChartAt I x x) := by simp only [ContMDiffWithinAt, liftPropWithinAt_iff'] exact and_congr_right fun hc => contDiffWithinAt_congr_set <| hc.extChartAt_symm_preimage_inter_range_eventuallyEq /-- One can reformulate being `Cⁿ` within a set at a point as continuity within this set at this point, and being `Cⁿ` in the corresponding extended chart in the target. -/ theorem contMDiffWithinAt_iff_target : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContMDiffWithinAt I 𝓘(𝕜, E') n (extChartAt I' (f x) ∘ f) s x := by simp_rw [ContMDiffWithinAt, liftPropWithinAt_iff', ← and_assoc] have cont : ContinuousWithinAt f s x ∧ ContinuousWithinAt (extChartAt I' (f x) ∘ f) s x ↔ ContinuousWithinAt f s x := and_iff_left_of_imp <| (continuousAt_extChartAt _).comp_continuousWithinAt simp_rw [cont, ContDiffWithinAtProp, extChartAt, OpenPartialHomeomorph.extend, PartialEquiv.coe_trans, ModelWithCorners.toPartialEquiv_coe, OpenPartialHomeomorph.coe_coe, modelWithCornersSelf_coe, chartAt_self_eq, OpenPartialHomeomorph.refl_apply, id_comp] rfl theorem contMDiffAt_iff_target {x : M} : ContMDiffAt I I' n f x ↔ ContinuousAt f x ∧ ContMDiffAt I 𝓘(𝕜, E') n (extChartAt I' (f x) ∘ f) x := by rw [ContMDiffAt, ContMDiffAt, contMDiffWithinAt_iff_target, continuousWithinAt_univ] /-- One can reformulate being `Cⁿ` within a set at a point as being `Cⁿ` in the source space when composing with the extended chart. -/ theorem contMDiffWithinAt_iff_source : ContMDiffWithinAt I I' n f s x ↔ ContMDiffWithinAt 𝓘(𝕜, E) I' n (f ∘ (extChartAt I x).symm) ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x) := by simp_rw [ContMDiffWithinAt, liftPropWithinAt_iff'] have : ContinuousWithinAt f s x ↔ ContinuousWithinAt (f ∘ ↑(extChartAt I x).symm) (↑(extChartAt I x).symm ⁻¹' s ∩ range ↑I) (extChartAt I x x) := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · apply h.comp_of_eq · exact (continuousAt_extChartAt_symm x).continuousWithinAt · exact (mapsTo_preimage _ _).mono_left inter_subset_left · exact extChartAt_to_inv x · rw [← continuousWithinAt_inter (extChartAt_source_mem_nhds (I := I) x)] have : ContinuousWithinAt ((f ∘ ↑(extChartAt I x).symm) ∘ ↑(extChartAt I x)) (s ∩ (extChartAt I x).source) x := by apply h.comp (continuousAt_extChartAt x).continuousWithinAt intro y hy have : (chartAt H x).symm ((chartAt H x) y) = y := OpenPartialHomeomorph.left_inv _ (by simpa using hy.2) simpa [this] using hy.1 apply this.congr · intro y hy have : (chartAt H x).symm ((chartAt H x) y) = y := OpenPartialHomeomorph.left_inv _ (by simpa using hy.2) simp [this] · simp rw [← this] simp only [ContDiffWithinAtProp, mfld_simps, preimage_comp, comp_assoc] /-- One can reformulate being `Cⁿ` at a point as being `Cⁿ` in the source space when composing with the extended chart. -/ theorem contMDiffAt_iff_source : ContMDiffAt I I' n f x ↔ ContMDiffWithinAt 𝓘(𝕜, E) I' n (f ∘ (extChartAt I x).symm) (range I) (extChartAt I x x) := by rw [← contMDiffWithinAt_univ, contMDiffWithinAt_iff_source] simp section IsManifold theorem contMDiffWithinAt_iff_source_of_mem_maximalAtlas [IsManifold I n M] (he : e ∈ maximalAtlas I n M) (hx : x ∈ e.source) : ContMDiffWithinAt I I' n f s x ↔ ContMDiffWithinAt 𝓘(𝕜, E) I' n (f ∘ (e.extend I).symm) ((e.extend I).symm ⁻¹' s ∩ range I) (e.extend I x) := by have h2x := hx; rw [← e.extend_source (I := I)] at h2x simp_rw [ContMDiffWithinAt, (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_indep_chart_source he hx, StructureGroupoid.liftPropWithinAt_self_source, e.extend_symm_continuousWithinAt_comp_right_iff, contDiffWithinAtProp_self_source, ContDiffWithinAtProp, Function.comp, e.left_inv hx, (e.extend I).left_inv h2x] rfl theorem contMDiffWithinAt_iff_source_of_mem_source [IsManifold I n M] {x' : M} (hx' : x' ∈ (chartAt H x).source) : ContMDiffWithinAt I I' n f s x' ↔ ContMDiffWithinAt 𝓘(𝕜, E) I' n (f ∘ (extChartAt I x).symm) ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x') := contMDiffWithinAt_iff_source_of_mem_maximalAtlas (chart_mem_maximalAtlas x) hx' theorem contMDiffAt_iff_source_of_mem_source [IsManifold I n M] {x' : M} (hx' : x' ∈ (chartAt H x).source) : ContMDiffAt I I' n f x' ↔ ContMDiffWithinAt 𝓘(𝕜, E) I' n (f ∘ (extChartAt I x).symm) (range I) (extChartAt I x x') := by simp_rw [ContMDiffAt, contMDiffWithinAt_iff_source_of_mem_source hx', preimage_univ, univ_inter] theorem contMDiffWithinAt_iff_target_of_mem_source [IsManifold I' n M'] {x : M} {y : M'} (hy : f x ∈ (chartAt H' y).source) : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContMDiffWithinAt I 𝓘(𝕜, E') n (extChartAt I' y ∘ f) s x := by simp_rw [ContMDiffWithinAt] rw [(contDiffWithinAt_localInvariantProp n).liftPropWithinAt_indep_chart_target (chart_mem_maximalAtlas y) hy, and_congr_right] intro hf simp_rw [StructureGroupoid.liftPropWithinAt_self_target] simp_rw [((chartAt H' y).continuousAt hy).comp_continuousWithinAt hf] rw [← extChartAt_source (I := I')] at hy simp_rw [(continuousAt_extChartAt' hy).comp_continuousWithinAt hf] rfl theorem contMDiffAt_iff_target_of_mem_source [IsManifold I' n M'] {x : M} {y : M'} (hy : f x ∈ (chartAt H' y).source) : ContMDiffAt I I' n f x ↔ ContinuousAt f x ∧ ContMDiffAt I 𝓘(𝕜, E') n (extChartAt I' y ∘ f) x := by rw [ContMDiffAt, contMDiffWithinAt_iff_target_of_mem_source hy, continuousWithinAt_univ, ContMDiffAt] variable [IsManifold I n M] [IsManifold I' n M'] theorem contMDiffWithinAt_iff_of_mem_maximalAtlas {x : M} (he : e ∈ maximalAtlas I n M) (he' : e' ∈ maximalAtlas I' n M') (hx : x ∈ e.source) (hy : f x ∈ e'.source) : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContDiffWithinAt 𝕜 n (e'.extend I' ∘ f ∘ (e.extend I).symm) ((e.extend I).symm ⁻¹' s ∩ range I) (e.extend I x) := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_indep_chart he hx he' hy /-- An alternative formulation of `contMDiffWithinAt_iff_of_mem_maximalAtlas` if the set if `s` lies in `e.source`. -/ theorem contMDiffWithinAt_iff_image {x : M} (he : e ∈ maximalAtlas I n M) (he' : e' ∈ maximalAtlas I' n M') (hs : s ⊆ e.source) (hx : x ∈ e.source) (hy : f x ∈ e'.source) : ContMDiffWithinAt I I' n f s x ↔ ContinuousWithinAt f s x ∧ ContDiffWithinAt 𝕜 n (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s) (e.extend I x) := by rw [contMDiffWithinAt_iff_of_mem_maximalAtlas he he' hx hy, and_congr_right_iff] refine fun _ => contDiffWithinAt_congr_set ?_ simp_rw [e.extend_symm_preimage_inter_range_eventuallyEq hs hx] /-- One can reformulate being `C^n` within a set at a point as continuity within this set at this point, and being `C^n` in any chart containing that point. -/ theorem contMDiffWithinAt_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) : ContMDiffWithinAt I I' n f s x' ↔ ContinuousWithinAt f s x' ∧ ContDiffWithinAt 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x') := contMDiffWithinAt_iff_of_mem_maximalAtlas (chart_mem_maximalAtlas x) (chart_mem_maximalAtlas y) hx hy theorem contMDiffWithinAt_iff_of_mem_source' {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) : ContMDiffWithinAt I I' n f s x' ↔ ContinuousWithinAt f s x' ∧ ContDiffWithinAt 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' y).source)) (extChartAt I x x') := by refine (contMDiffWithinAt_iff_of_mem_source hx hy).trans ?_ rw [← extChartAt_source I] at hx rw [← extChartAt_source I'] at hy rw [and_congr_right_iff] set e := extChartAt I x; set e' := extChartAt I' (f x) refine fun hc => contDiffWithinAt_congr_set ?_ rw [← nhdsWithin_eq_iff_eventuallyEq, ← e.image_source_inter_eq', ← map_extChartAt_nhdsWithin_eq_image' hx, ← map_extChartAt_nhdsWithin' hx, inter_comm, nhdsWithin_inter_of_mem] exact hc (extChartAt_source_mem_nhds' hy) theorem contMDiffAt_iff_of_mem_source {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) : ContMDiffAt I I' n f x' ↔ ContinuousAt f x' ∧ ContDiffWithinAt 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (range I) (extChartAt I x x') := (contMDiffWithinAt_iff_of_mem_source hx hy).trans <| by rw [continuousWithinAt_univ, preimage_univ, univ_inter] theorem contMDiffOn_iff_of_mem_maximalAtlas (he : e ∈ maximalAtlas I n M) (he' : e' ∈ maximalAtlas I' n M') (hs : s ⊆ e.source) (h2s : MapsTo f s e'.source) : ContMDiffOn I I' n f s ↔ ContinuousOn f s ∧ ContDiffOn 𝕜 n (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s) := by simp_rw [ContinuousOn, ContDiffOn, Set.forall_mem_image, ← forall_and, ContMDiffOn] exact forall₂_congr fun x hx => contMDiffWithinAt_iff_image he he' hs (hs hx) (h2s hx) theorem contMDiffOn_iff_of_mem_maximalAtlas' (he : e ∈ maximalAtlas I n M) (he' : e' ∈ maximalAtlas I' n M') (hs : s ⊆ e.source) (h2s : MapsTo f s e'.source) : ContMDiffOn I I' n f s ↔ ContDiffOn 𝕜 n (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s) := (contMDiffOn_iff_of_mem_maximalAtlas he he' hs h2s).trans <| and_iff_right_of_imp fun h ↦ (e.continuousOn_writtenInExtend_iff hs h2s).1 h.continuousOn /-- If the set where you want `f` to be `C^n` lies entirely in a single chart, and `f` maps it into a single chart, the fact that `f` is `C^n` on that set can be expressed by purely looking in these charts. Note: this lemma uses `extChartAt I x '' s` instead of `(extChartAt I x).symm ⁻¹' s` to ensure that this set lies in `(extChartAt I x).target`. -/ theorem contMDiffOn_iff_of_subset_source {x : M} {y : M'} (hs : s ⊆ (chartAt H x).source) (h2s : MapsTo f s (chartAt H' y).source) : ContMDiffOn I I' n f s ↔ ContinuousOn f s ∧ ContDiffOn 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (extChartAt I x '' s) := contMDiffOn_iff_of_mem_maximalAtlas (chart_mem_maximalAtlas x) (chart_mem_maximalAtlas y) hs h2s /-- If the set where you want `f` to be `C^n` lies entirely in a single chart, and `f` maps it into a single chart, the fact that `f` is `C^n` on that set can be expressed by purely looking in these charts. Note: this lemma uses `extChartAt I x '' s` instead of `(extChartAt I x).symm ⁻¹' s` to ensure that this set lies in `(extChartAt I x).target`. -/ theorem contMDiffOn_iff_of_subset_source' {x : M} {y : M'} (hs : s ⊆ (extChartAt I x).source) (h2s : MapsTo f s (extChartAt I' y).source) : ContMDiffOn I I' n f s ↔ ContDiffOn 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (extChartAt I x '' s) := by rw [extChartAt_source] at hs h2s exact contMDiffOn_iff_of_mem_maximalAtlas' (chart_mem_maximalAtlas x) (chart_mem_maximalAtlas y) hs h2s /-- One can reformulate being `C^n` on a set as continuity on this set, and being `C^n` in any extended chart. -/ theorem contMDiffOn_iff : ContMDiffOn I I' n f s ↔ ContinuousOn f s ∧ ∀ (x : M) (y : M'), ContDiffOn 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' y).source)) := by constructor · intro h refine ⟨fun x hx => (h x hx).1, fun x y z hz => ?_⟩ simp only [mfld_simps] at hz let w := (extChartAt I x).symm z have : w ∈ s := by simp only [w, hz, mfld_simps] specialize h w this have w1 : w ∈ (chartAt H x).source := by simp only [w, hz, mfld_simps] have w2 : f w ∈ (chartAt H' y).source := by simp only [w, hz, mfld_simps] convert ((contMDiffWithinAt_iff_of_mem_source w1 w2).mp h).2.mono _ · simp only [w, hz, mfld_simps] · mfld_set_tac · rintro ⟨hcont, hdiff⟩ x hx refine (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_iff.mpr ?_ refine ⟨hcont x hx, ?_⟩ dsimp [ContDiffWithinAtProp] convert hdiff x (f x) (extChartAt I x x) (by simp only [hx, mfld_simps]) using 1 mfld_set_tac /-- zero-smoothness on a set is equivalent to continuity on this set. -/ theorem contMDiffOn_zero_iff : ContMDiffOn I I' 0 f s ↔ ContinuousOn f s := by rw [contMDiffOn_iff] refine ⟨fun h ↦ h.1, fun h ↦ ⟨h, ?_⟩⟩ intro x y rw [contDiffOn_zero] apply (continuousOn_extChartAt _).comp · apply h.comp ((continuousOn_extChartAt_symm _).mono inter_subset_left) (fun z hz ↦ ?_) simp only [preimage_inter, mem_inter_iff, mem_preimage] at hz exact hz.2.1 · intro z hz simp only [preimage_inter, mem_inter_iff, mem_preimage] at hz exact hz.2.2 /-- One can reformulate being `C^n` on a set as continuity on this set, and being `C^n` in any extended chart in the target. -/ theorem contMDiffOn_iff_target : ContMDiffOn I I' n f s ↔ ContinuousOn f s ∧ ∀ y : M', ContMDiffOn I 𝓘(𝕜, E') n (extChartAt I' y ∘ f) (s ∩ f ⁻¹' (extChartAt I' y).source) := by simp only [contMDiffOn_iff, ModelWithCorners.source_eq, chartAt_self_eq, OpenPartialHomeomorph.refl_partialEquiv, PartialEquiv.refl_trans, extChartAt, OpenPartialHomeomorph.extend, Set.preimage_univ, Set.inter_univ, and_congr_right_iff] intro h constructor · refine fun h' y => ⟨?_, fun x _ => h' x y⟩ have h'' : ContinuousOn _ univ := (ModelWithCorners.continuous I').continuousOn convert (h''.comp_inter (chartAt H' y).continuousOn_toFun).comp_inter h simp · exact fun h' x y => (h' y).2 x 0 /-- One can reformulate being `C^n` as continuity and being `C^n` in any extended chart. -/ theorem contMDiff_iff : ContMDiff I I' n f ↔ Continuous f ∧ ∀ (x : M) (y : M'), ContDiffOn 𝕜 n (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (f ⁻¹' (extChartAt I' y).source)) := by simp [← contMDiffOn_univ, contMDiffOn_iff, continuousOn_univ] /-- One can reformulate being `C^n` as continuity and being `C^n` in any extended chart in the target. -/ theorem contMDiff_iff_target : ContMDiff I I' n f ↔ Continuous f ∧ ∀ y : M', ContMDiffOn I 𝓘(𝕜, E') n (extChartAt I' y ∘ f) (f ⁻¹' (extChartAt I' y).source) := by rw [← contMDiffOn_univ, contMDiffOn_iff_target] simp [continuousOn_univ] /-- zero-smoothness is equivalent to continuity. -/ theorem contMDiff_zero_iff : ContMDiff I I' 0 f ↔ Continuous f := by rw [← contMDiffOn_univ, ← continuousOn_univ, contMDiffOn_zero_iff] end IsManifold /-! ### `C^(n+1)` functions are `C^n` -/ theorem ContMDiffWithinAt.of_succ (h : ContMDiffWithinAt I I' (n + 1) f s x) : ContMDiffWithinAt I I' n f s x := h.of_le le_self_add theorem ContMDiffAt.of_succ (h : ContMDiffAt I I' (n + 1) f x) : ContMDiffAt I I' n f x := ContMDiffWithinAt.of_succ h theorem ContMDiffOn.of_succ (h : ContMDiffOn I I' (n + 1) f s) : ContMDiffOn I I' n f s := fun x hx => (h x hx).of_succ theorem ContMDiff.of_succ (h : ContMDiff I I' (n + 1) f) : ContMDiff I I' n f := fun x => (h x).of_succ /-! ### `C^n` functions are continuous -/ theorem ContMDiffWithinAt.continuousWithinAt (hf : ContMDiffWithinAt I I' n f s x) : ContinuousWithinAt f s x := hf.1 theorem ContMDiffAt.continuousAt (hf : ContMDiffAt I I' n f x) : ContinuousAt f x := (continuousWithinAt_univ _ _).1 <| ContMDiffWithinAt.continuousWithinAt hf theorem ContMDiffOn.continuousOn (hf : ContMDiffOn I I' n f s) : ContinuousOn f s := fun x hx => (hf x hx).continuousWithinAt theorem ContMDiff.continuous (hf : ContMDiff I I' n f) : Continuous f := continuous_iff_continuousAt.2 fun x => (hf x).continuousAt /-! ### `C^∞` functions -/ theorem contMDiffWithinAt_infty : ContMDiffWithinAt I I' ∞ f s x ↔ ∀ n : ℕ, ContMDiffWithinAt I I' n f s x := ⟨fun h n => ⟨h.1, contDiffWithinAt_infty.1 h.2 n⟩, fun H => ⟨(H 0).1, contDiffWithinAt_infty.2 fun n => (H n).2⟩⟩ theorem contMDiffAt_infty : ContMDiffAt I I' ∞ f x ↔ ∀ n : ℕ, ContMDiffAt I I' n f x := contMDiffWithinAt_infty theorem contMDiffOn_infty : ContMDiffOn I I' ∞ f s ↔ ∀ n : ℕ, ContMDiffOn I I' n f s := ⟨fun h _ => h.of_le (mod_cast le_top), fun h x hx => contMDiffWithinAt_infty.2 fun n => h n x hx⟩ theorem contMDiff_infty : ContMDiff I I' ∞ f ↔ ∀ n : ℕ, ContMDiff I I' n f := ⟨fun h _ => h.of_le (mod_cast le_top), fun h x => contMDiffWithinAt_infty.2 fun n => h n x⟩ theorem contMDiffWithinAt_iff_nat {n : ℕ∞} : ContMDiffWithinAt I I' n f s x ↔ ∀ m : ℕ, (m : ℕ∞) ≤ n → ContMDiffWithinAt I I' m f s x := by refine ⟨fun h m hm => h.of_le (mod_cast hm), fun h => ?_⟩ obtain - | n := n · exact contMDiffWithinAt_infty.2 fun n => h n le_top · exact h n le_rfl theorem contMDiffAt_iff_nat {n : ℕ∞} : ContMDiffAt I I' n f x ↔ ∀ m : ℕ, (m : ℕ∞) ≤ n → ContMDiffAt I I' m f x := by simp [← contMDiffWithinAt_univ, contMDiffWithinAt_iff_nat] /-- A function is `C^n` within a set at a point iff it is `C^m` within this set at this point, for any `m ≤ n` which is different from `∞`. This result is useful because, when `m ≠ ∞`, being `C^m` extends locally to a neighborhood, giving flexibility for local proofs. -/ theorem contMDiffWithinAt_iff_le_ne_infty : ContMDiffWithinAt I I' n f s x ↔ ∀ m, m ≤ n → m ≠ ∞ → ContMDiffWithinAt I I' m f s x := by refine ⟨fun h m hm h'm ↦ h.of_le hm, fun h ↦ ?_⟩ cases n with | top => exact h _ le_rfl (by simp) | coe n => exact contMDiffWithinAt_iff_nat.2 (fun m hm ↦ h _ (mod_cast hm) (by simp)) /-- A function is `C^n`at a point iff it is `C^m`at this point, for any `m ≤ n` which is different from `∞`. This result is useful because, when `m ≠ ∞`, being `C^m` extends locally to a neighborhood, giving flexibility for local proofs. -/ theorem contMDiffAt_iff_le_ne_infty : ContMDiffAt I I' n f x ↔ ∀ m, m ≤ n → m ≠ ∞ → ContMDiffAt I I' m f x := by simp only [← contMDiffWithinAt_univ] rw [contMDiffWithinAt_iff_le_ne_infty] /-! ### Restriction to a smaller set -/ theorem ContMDiffWithinAt.mono_of_mem_nhdsWithin (hf : ContMDiffWithinAt I I' n f s x) (hts : s ∈ 𝓝[t] x) : ContMDiffWithinAt I I' n f t x := StructureGroupoid.LocalInvariantProp.liftPropWithinAt_mono_of_mem_nhdsWithin (contDiffWithinAtProp_mono_of_mem_nhdsWithin n) hf hts theorem ContMDiffWithinAt.mono (hf : ContMDiffWithinAt I I' n f s x) (hts : t ⊆ s) : ContMDiffWithinAt I I' n f t x := hf.mono_of_mem_nhdsWithin <| mem_of_superset self_mem_nhdsWithin hts theorem contMDiffWithinAt_congr_set (h : s =ᶠ[𝓝 x] t) : ContMDiffWithinAt I I' n f s x ↔ ContMDiffWithinAt I I' n f t x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_set h theorem ContMDiffWithinAt.congr_set (h : ContMDiffWithinAt I I' n f s x) (hst : s =ᶠ[𝓝 x] t) : ContMDiffWithinAt I I' n f t x := (contMDiffWithinAt_congr_set hst).1 h theorem contMDiffWithinAt_insert_self : ContMDiffWithinAt I I' n f (insert x s) x ↔ ContMDiffWithinAt I I' n f s x := by simp only [contMDiffWithinAt_iff, continuousWithinAt_insert_self] refine Iff.rfl.and <| (contDiffWithinAt_congr_set ?_).trans contDiffWithinAt_insert_self simp only [← map_extChartAt_nhdsWithin, nhdsWithin_insert, Filter.map_sup, Filter.map_pure, ← nhdsWithin_eq_iff_eventuallyEq] alias ⟨ContMDiffWithinAt.of_insert, _⟩ := contMDiffWithinAt_insert_self -- TODO: use `alias` again once it can make protected theorems protected theorem ContMDiffWithinAt.insert (h : ContMDiffWithinAt I I' n f s x) : ContMDiffWithinAt I I' n f (insert x s) x := contMDiffWithinAt_insert_self.2 h /-- Being `C^n` in a set only depends on the germ of the set. Version where one only requires the two sets to coincide locally in the complement of a point `y`. -/ theorem contMDiffWithinAt_congr_set' (y : M) (h : s =ᶠ[𝓝[{y}ᶜ] x] t) : ContMDiffWithinAt I I' n f s x ↔ ContMDiffWithinAt I I' n f t x := by have : T1Space M := I.t1Space M rw [← contMDiffWithinAt_insert_self (s := s), ← contMDiffWithinAt_insert_self (s := t)] exact contMDiffWithinAt_congr_set (eventuallyEq_insert h) protected theorem ContMDiffAt.contMDiffWithinAt (hf : ContMDiffAt I I' n f x) : ContMDiffWithinAt I I' n f s x := ContMDiffWithinAt.mono hf (subset_univ _) theorem ContMDiffOn.mono (hf : ContMDiffOn I I' n f s) (hts : t ⊆ s) : ContMDiffOn I I' n f t := fun x hx => (hf x (hts hx)).mono hts protected theorem ContMDiff.contMDiffOn (hf : ContMDiff I I' n f) : ContMDiffOn I I' n f s := fun x _ => (hf x).contMDiffWithinAt theorem contMDiffWithinAt_inter' (ht : t ∈ 𝓝[s] x) : ContMDiffWithinAt I I' n f (s ∩ t) x ↔ ContMDiffWithinAt I I' n f s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_inter' ht theorem contMDiffWithinAt_inter (ht : t ∈ 𝓝 x) : ContMDiffWithinAt I I' n f (s ∩ t) x ↔ ContMDiffWithinAt I I' n f s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_inter ht protected theorem ContMDiffWithinAt.contMDiffAt (h : ContMDiffWithinAt I I' n f s x) (ht : s ∈ 𝓝 x) : ContMDiffAt I I' n f x := (contDiffWithinAt_localInvariantProp n).liftPropAt_of_liftPropWithinAt h ht protected theorem ContMDiffOn.contMDiffAt (h : ContMDiffOn I I' n f s) (hx : s ∈ 𝓝 x) : ContMDiffAt I I' n f x := (h x (mem_of_mem_nhds hx)).contMDiffAt hx theorem contMDiffOn_iff_source_of_mem_maximalAtlas [IsManifold I n M] (he : e ∈ maximalAtlas I n M) (hs : s ⊆ e.source) : ContMDiffOn I I' n f s ↔ ContMDiffOn 𝓘(𝕜, E) I' n (f ∘ (e.extend I).symm) (e.extend I '' s) := by simp_rw [ContMDiffOn, Set.forall_mem_image] refine forall₂_congr fun x hx => ?_ rw [contMDiffWithinAt_iff_source_of_mem_maximalAtlas he (hs hx)] apply contMDiffWithinAt_congr_set simp_rw [e.extend_symm_preimage_inter_range_eventuallyEq hs (hs hx)] /-- A function is `C^n` within a set at a point, for `n : ℕ` or `n = ω`, if and only if it is `C^n` on a neighborhood of this point. -/ theorem contMDiffWithinAt_iff_contMDiffOn_nhds [IsManifold I n M] [IsManifold I' n M'] (hn : n ≠ ∞) : ContMDiffWithinAt I I' n f s x ↔ ∃ u ∈ 𝓝[insert x s] x, ContMDiffOn I I' n f u := by -- WLOG, `x ∈ s`, otherwise we add `x` to `s` wlog hxs : x ∈ s generalizing s · rw [← contMDiffWithinAt_insert_self, this (mem_insert _ _), insert_idem] rw [insert_eq_of_mem hxs] -- The `←` implication is trivial refine ⟨fun h ↦ ?_, fun ⟨u, hmem, hu⟩ ↦ (hu _ (mem_of_mem_nhdsWithin hxs hmem)).mono_of_mem_nhdsWithin hmem⟩ -- The property is true in charts. Let `v` be a good neighborhood in the chart where the function -- is `Cⁿ`. rcases (contMDiffWithinAt_iff'.1 h).2.contDiffOn le_rfl (by simp [hn]) with ⟨v, hmem, hsub, hv⟩ have hxs' : extChartAt I x x ∈ (extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' (f x)).source) := ⟨(extChartAt I x).map_source (mem_extChartAt_source _), by rwa [extChartAt_to_inv], by rw [extChartAt_to_inv]; apply mem_extChartAt_source⟩ rw [insert_eq_of_mem hxs'] at hmem hsub -- Then `(extChartAt I x).symm '' v` is the neighborhood we are looking for. refine ⟨(extChartAt I x).symm '' v, ?_, ?_⟩ · rw [← map_extChartAt_symm_nhdsWithin (I := I), h.1.nhdsWithin_extChartAt_symm_preimage_inter_range (I := I) (I' := I')] exact image_mem_map hmem · have hv₁ : (extChartAt I x).symm '' v ⊆ (extChartAt I x).source := image_subset_iff.2 fun y hy ↦ (extChartAt I x).map_target (hsub hy).1 have hv₂ : MapsTo f ((extChartAt I x).symm '' v) (extChartAt I' (f x)).source := by rintro _ ⟨y, hy, rfl⟩ exact (hsub hy).2.2 rwa [contMDiffOn_iff_of_subset_source' hv₁ hv₂, PartialEquiv.image_symm_image_of_subset_target] exact hsub.trans inter_subset_left /-- If a function is `C^m` within a set at a point, for some finite `m`, then it is `C^m` within this set on an open set around the basepoint. -/ theorem ContMDiffWithinAt.contMDiffOn' [IsManifold I n M] [IsManifold I' n M'] (hm : m ≤ n) (h' : m = ∞ → n = ω) (h : ContMDiffWithinAt I I' n f s x) : ∃ u, IsOpen u ∧ x ∈ u ∧ ContMDiffOn I I' m f (insert x s ∩ u) := by have : IsManifold I m M := .of_le hm have : IsManifold I' m M' := .of_le hm match m with | (m : ℕ) | ω => rcases (contMDiffWithinAt_iff_contMDiffOn_nhds (by simp)).1 (h.of_le hm) with ⟨t, ht, h't⟩ rcases mem_nhdsWithin.1 ht with ⟨u, u_open, xu, hu⟩ rw [inter_comm] at hu exact ⟨u, u_open, xu, h't.mono hu⟩ | ∞ => rcases (contMDiffWithinAt_iff_contMDiffOn_nhds (by simp [h'])).1 h with ⟨t, ht, h't⟩ rcases mem_nhdsWithin.1 ht with ⟨u, u_open, xu, hu⟩ rw [inter_comm] at hu exact ⟨u, u_open, xu, (h't.mono hu).of_le hm⟩ /-- If a function is `C^m` within a set at a point, for some finite `m`, then it is `C^m` within this set on a neighborhood of the basepoint. -/ theorem ContMDiffWithinAt.contMDiffOn [IsManifold I n M] [IsManifold I' n M'] (hm : m ≤ n) (h' : m = ∞ → n = ω) (h : ContMDiffWithinAt I I' n f s x) : ∃ u ∈ 𝓝[insert x s] x, u ⊆ insert x s ∧ ContMDiffOn I I' m f u := by let ⟨_u, uo, xu, h⟩ := h.contMDiffOn' hm h' exact ⟨_, inter_mem_nhdsWithin _ (uo.mem_nhds xu), inter_subset_left, h⟩ /-- A function is `C^n` at a point, for `n : ℕ`, if and only if it is `C^n` on a neighborhood of this point. -/ theorem contMDiffAt_iff_contMDiffOn_nhds [IsManifold I n M] [IsManifold I' n M'] (hn : n ≠ ∞) : ContMDiffAt I I' n f x ↔ ∃ u ∈ 𝓝 x, ContMDiffOn I I' n f u := by simp [← contMDiffWithinAt_univ, contMDiffWithinAt_iff_contMDiffOn_nhds hn, nhdsWithin_univ] /-- Note: This does not hold for `n = ∞`. `f` being `C^∞` at `x` means that for every `n`, `f` is `C^n` on some neighborhood of `x`, but this neighborhood can depend on `n`. -/ theorem contMDiffAt_iff_contMDiffAt_nhds [IsManifold I n M] [IsManifold I' n M'] (hn : n ≠ ∞) : ContMDiffAt I I' n f x ↔ ∀ᶠ x' in 𝓝 x, ContMDiffAt I I' n f x' := by refine ⟨?_, fun h => h.self_of_nhds⟩ rw [contMDiffAt_iff_contMDiffOn_nhds hn] rintro ⟨u, hu, h⟩ refine (eventually_mem_nhds_iff.mpr hu).mono fun x' hx' => ?_ exact (h x' <| mem_of_mem_nhds hx').contMDiffAt hx' /-- Note: This does not hold for `n = ∞`. `f` being `C^∞` at `x` means that for every `n`, `f` is `C^n` on some neighborhood of `x`, but this neighborhood can depend on `n`. -/ theorem contMDiffWithinAt_iff_contMDiffWithinAt_nhdsWithin [IsManifold I n M] [IsManifold I' n M'] (hn : n ≠ ∞) : ContMDiffWithinAt I I' n f s x ↔ ∀ᶠ x' in 𝓝[insert x s] x, ContMDiffWithinAt I I' n f s x' := by refine ⟨?_, fun h ↦ mem_of_mem_nhdsWithin (mem_insert x s) h⟩ rw [contMDiffWithinAt_iff_contMDiffOn_nhds hn] rintro ⟨u, hu, h⟩ filter_upwards [hu, eventually_mem_nhdsWithin_iff.mpr hu] with x' h'x' hx' apply (h x' h'x').mono_of_mem_nhdsWithin exact nhdsWithin_mono _ (subset_insert x s) hx' /-! ### Congruence lemmas -/ theorem ContMDiffWithinAt.congr (h : ContMDiffWithinAt I I' n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : ContMDiffWithinAt I I' n f₁ s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr h h₁ hx /-- Version of `ContMDiffWithinAt.congr` where `x` need not be contained in `s`, but `f` and `f₁` are equal on a set containing both. -/ theorem ContMDiffWithinAt.congr' (h : ContMDiffWithinAt I I' n f s x) (h₁ : ∀ y ∈ t, f₁ y = f y) (hst : s ⊆ t) (hxt : x ∈ t) : ContMDiffWithinAt I I' n f₁ s x := h.congr (fun _y hy ↦ h₁ _ (hst hy)) (h₁ x hxt) theorem contMDiffWithinAt_congr (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) : ContMDiffWithinAt I I' n f₁ s x ↔ ContMDiffWithinAt I I' n f s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_iff h₁ hx theorem ContMDiffWithinAt.congr_of_mem (h : ContMDiffWithinAt I I' n f s x) (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) : ContMDiffWithinAt I I' n f₁ s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_of_mem h h₁ hx theorem contMDiffWithinAt_congr_of_mem (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) : ContMDiffWithinAt I I' n f₁ s x ↔ ContMDiffWithinAt I I' n f s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_iff_of_mem h₁ hx theorem ContMDiffWithinAt.congr_of_eventuallyEq (h : ContMDiffWithinAt I I' n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : ContMDiffWithinAt I I' n f₁ s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_of_eventuallyEq h h₁ hx theorem ContMDiffWithinAt.congr_of_eventuallyEq_of_mem (h : ContMDiffWithinAt I I' n f s x) (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : x ∈ s) : ContMDiffWithinAt I I' n f₁ s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_of_eventuallyEq_of_mem h h₁ hx theorem Filter.EventuallyEq.contMDiffWithinAt_iff (h₁ : f₁ =ᶠ[𝓝[s] x] f) (hx : f₁ x = f x) : ContMDiffWithinAt I I' n f₁ s x ↔ ContMDiffWithinAt I I' n f s x := (contDiffWithinAt_localInvariantProp n).liftPropWithinAt_congr_iff_of_eventuallyEq h₁ hx theorem ContMDiffAt.congr_of_eventuallyEq (h : ContMDiffAt I I' n f x) (h₁ : f₁ =ᶠ[𝓝 x] f) : ContMDiffAt I I' n f₁ x := (contDiffWithinAt_localInvariantProp n).liftPropAt_congr_of_eventuallyEq h h₁ theorem Filter.EventuallyEq.contMDiffAt_iff (h₁ : f₁ =ᶠ[𝓝 x] f) : ContMDiffAt I I' n f₁ x ↔ ContMDiffAt I I' n f x := (contDiffWithinAt_localInvariantProp n).liftPropAt_congr_iff_of_eventuallyEq h₁ theorem ContMDiffOn.congr (h : ContMDiffOn I I' n f s) (h₁ : ∀ y ∈ s, f₁ y = f y) : ContMDiffOn I I' n f₁ s := (contDiffWithinAt_localInvariantProp n).liftPropOn_congr h h₁ theorem contMDiffOn_congr (h₁ : ∀ y ∈ s, f₁ y = f y) : ContMDiffOn I I' n f₁ s ↔ ContMDiffOn I I' n f s := (contDiffWithinAt_localInvariantProp n).liftPropOn_congr_iff h₁ theorem ContMDiffOn.congr_mono (hf : ContMDiffOn I I' n f s) (h₁ : ∀ y ∈ s₁, f₁ y = f y) (hs : s₁ ⊆ s) : ContMDiffOn I I' n f₁ s₁ := (hf.mono hs).congr h₁ theorem ContMDiff.congr (h : ContMDiff I I' n f) (h₁ : ∀ y, f₁ y = f y) : ContMDiff I I' n f₁ := by rw [← contMDiffOn_univ] at h ⊢ exact (contMDiffOn_congr fun y _ ↦ h₁ y).mpr h theorem contMDiff_congr (h₁ : ∀ y, f₁ y = f y) : ContMDiff I I' n f₁ ↔ ContMDiff I I' n f := by simp_rw [← contMDiffOn_univ] exact contMDiffOn_congr fun y _ ↦ h₁ y /-! ### Locality -/ /-- Being `C^n` is a local property. -/ theorem contMDiffOn_of_locally_contMDiffOn (h : ∀ x ∈ s, ∃ u, IsOpen u ∧ x ∈ u ∧ ContMDiffOn I I' n f (s ∩ u)) : ContMDiffOn I I' n f s := (contDiffWithinAt_localInvariantProp n).liftPropOn_of_locally_liftPropOn h theorem contMDiff_of_locally_contMDiffOn (h : ∀ x, ∃ u, IsOpen u ∧ x ∈ u ∧ ContMDiffOn I I' n f u) : ContMDiff I I' n f := (contDiffWithinAt_localInvariantProp n).liftProp_of_locally_liftPropOn h
.lake/packages/mathlib/Mathlib/Geometry/Manifold/ContMDiff/NormedSpace.lean
import Mathlib.Geometry.Manifold.ContMDiff.Constructions import Mathlib.Analysis.Normed.Operator.Prod /-! ## Equivalence of smoothness with the basic definition for functions between vector spaces * `contMDiff_iff_contDiff`: for functions between vector spaces, manifold-smoothness is equivalent to usual smoothness. * `ContinuousLinearMap.contMDiff`: continuous linear maps between normed spaces are smooth * `smooth_smul`: multiplication by scalars is a smooth operation -/ open Set ChartedSpace open scoped Topology Manifold variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] -- declare a charted space `M` over the pair `(E, H)`. {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] -- declare normed spaces `E'`, `F`, `F'`, `F₁`, `F₂`, `F₃`, `F₄`. {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {F' : Type*} [NormedAddCommGroup F'] [NormedSpace 𝕜 F'] {F₁ : Type*} [NormedAddCommGroup F₁] [NormedSpace 𝕜 F₁] {F₂ : Type*} [NormedAddCommGroup F₂] [NormedSpace 𝕜 F₂] {F₃ : Type*} [NormedAddCommGroup F₃] [NormedSpace 𝕜 F₃] {F₄ : Type*} [NormedAddCommGroup F₄] [NormedSpace 𝕜 F₄] -- declare functions, sets, points and smoothness indices {s : Set M} {x : M} {n : WithTop ℕ∞} section Module theorem contMDiffWithinAt_iff_contDiffWithinAt {f : E → E'} {s : Set E} {x : E} : ContMDiffWithinAt 𝓘(𝕜, E) 𝓘(𝕜, E') n f s x ↔ ContDiffWithinAt 𝕜 n f s x := by simp +contextual only [ContMDiffWithinAt, liftPropWithinAt_iff', ContDiffWithinAtProp, iff_def, mfld_simps] exact ContDiffWithinAt.continuousWithinAt alias ⟨ContMDiffWithinAt.contDiffWithinAt, ContDiffWithinAt.contMDiffWithinAt⟩ := contMDiffWithinAt_iff_contDiffWithinAt theorem contMDiffAt_iff_contDiffAt {f : E → E'} {x : E} : ContMDiffAt 𝓘(𝕜, E) 𝓘(𝕜, E') n f x ↔ ContDiffAt 𝕜 n f x := by rw [← contMDiffWithinAt_univ, contMDiffWithinAt_iff_contDiffWithinAt, contDiffWithinAt_univ] alias ⟨ContMDiffAt.contDiffAt, ContDiffAt.contMDiffAt⟩ := contMDiffAt_iff_contDiffAt theorem contMDiffOn_iff_contDiffOn {f : E → E'} {s : Set E} : ContMDiffOn 𝓘(𝕜, E) 𝓘(𝕜, E') n f s ↔ ContDiffOn 𝕜 n f s := forall_congr' <| by simp [contMDiffWithinAt_iff_contDiffWithinAt] alias ⟨ContMDiffOn.contDiffOn, ContDiffOn.contMDiffOn⟩ := contMDiffOn_iff_contDiffOn theorem contMDiff_iff_contDiff {f : E → E'} : ContMDiff 𝓘(𝕜, E) 𝓘(𝕜, E') n f ↔ ContDiff 𝕜 n f := by rw [← contDiffOn_univ, ← contMDiffOn_univ, contMDiffOn_iff_contDiffOn] alias ⟨ContMDiff.contDiff, ContDiff.contMDiff⟩ := contMDiff_iff_contDiff theorem ContDiffWithinAt.comp_contMDiffWithinAt {g : F → F'} {f : M → F} {s : Set M} {t : Set F} {x : M} (hg : ContDiffWithinAt 𝕜 n g t (f x)) (hf : ContMDiffWithinAt I 𝓘(𝕜, F) n f s x) (h : s ⊆ f ⁻¹' t) : ContMDiffWithinAt I 𝓘(𝕜, F') n (g ∘ f) s x := hg.contMDiffWithinAt.comp x hf h theorem ContDiffAt.comp_contMDiffWithinAt {g : F → F'} {f : M → F} {s : Set M} {x : M} (hg : ContDiffAt 𝕜 n g (f x)) (hf : ContMDiffWithinAt I 𝓘(𝕜, F) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, F') n (g ∘ f) s x := hg.contMDiffAt.comp_contMDiffWithinAt x hf theorem ContDiffAt.comp_contMDiffAt {g : F → F'} {f : M → F} {x : M} (hg : ContDiffAt 𝕜 n g (f x)) (hf : ContMDiffAt I 𝓘(𝕜, F) n f x) : ContMDiffAt I 𝓘(𝕜, F') n (g ∘ f) x := hg.comp_contMDiffWithinAt hf theorem ContDiff.comp_contMDiffWithinAt {g : F → F'} {f : M → F} {s : Set M} {x : M} (hg : ContDiff 𝕜 n g) (hf : ContMDiffWithinAt I 𝓘(𝕜, F) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, F') n (g ∘ f) s x := hg.contDiffAt.comp_contMDiffWithinAt hf theorem ContDiff.comp_contMDiffAt {g : F → F'} {f : M → F} {x : M} (hg : ContDiff 𝕜 n g) (hf : ContMDiffAt I 𝓘(𝕜, F) n f x) : ContMDiffAt I 𝓘(𝕜, F') n (g ∘ f) x := hg.comp_contMDiffWithinAt hf theorem ContDiff.comp_contMDiff {g : F → F'} {f : M → F} (hg : ContDiff 𝕜 n g) (hf : ContMDiff I 𝓘(𝕜, F) n f) : ContMDiff I 𝓘(𝕜, F') n (g ∘ f) := fun x => hg.contDiffAt.comp_contMDiffAt (hf x) end Module /-! ### Linear maps between normed spaces are smooth -/ theorem ContinuousLinearMap.contMDiff (L : E →L[𝕜] F) : ContMDiff 𝓘(𝕜, E) 𝓘(𝕜, F) n L := L.contDiff.contMDiff theorem ContinuousLinearMap.contMDiffAt (L : E →L[𝕜] F) {x} : ContMDiffAt 𝓘(𝕜, E) 𝓘(𝕜, F) n L x := L.contMDiff _ theorem ContinuousLinearMap.contMDiffWithinAt (L : E →L[𝕜] F) {s x} : ContMDiffWithinAt 𝓘(𝕜, E) 𝓘(𝕜, F) n L s x := L.contMDiffAt.contMDiffWithinAt theorem ContinuousLinearMap.contMDiffOn (L : E →L[𝕜] F) {s} : ContMDiffOn 𝓘(𝕜, E) 𝓘(𝕜, F) n L s := L.contMDiff.contMDiffOn theorem ContMDiffWithinAt.clm_precomp {f : M → F₁ →L[𝕜] F₂} {s : Set M} {x : M} (hf : ContMDiffWithinAt I 𝓘(𝕜, F₁ →L[𝕜] F₂) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).precomp F₃ : M → (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) s x := ContDiff.comp_contMDiffWithinAt (g := (ContinuousLinearMap.compL 𝕜 F₁ F₂ F₃).flip) (ContinuousLinearMap.contDiff _) hf nonrec theorem ContMDiffAt.clm_precomp {f : M → F₁ →L[𝕜] F₂} {x : M} (hf : ContMDiffAt I 𝓘(𝕜, F₁ →L[𝕜] F₂) n f x) : ContMDiffAt I 𝓘(𝕜, (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).precomp F₃ : M → (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) x := hf.clm_precomp theorem ContMDiffOn.clm_precomp {f : M → F₁ →L[𝕜] F₂} {s : Set M} (hf : ContMDiffOn I 𝓘(𝕜, F₁ →L[𝕜] F₂) n f s) : ContMDiffOn I 𝓘(𝕜, (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).precomp F₃ : M → (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) s := fun x hx ↦ (hf x hx).clm_precomp theorem ContMDiff.clm_precomp {f : M → F₁ →L[𝕜] F₂} (hf : ContMDiff I 𝓘(𝕜, F₁ →L[𝕜] F₂) n f) : ContMDiff I 𝓘(𝕜, (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).precomp F₃ : M → (F₂ →L[𝕜] F₃) →L[𝕜] (F₁ →L[𝕜] F₃)) := fun x ↦ (hf x).clm_precomp theorem ContMDiffWithinAt.clm_postcomp {f : M → F₂ →L[𝕜] F₃} {s : Set M} {x : M} (hf : ContMDiffWithinAt I 𝓘(𝕜, F₂ →L[𝕜] F₃) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).postcomp F₁ : M → (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) s x := ContDiff.comp_contMDiffWithinAt (F' := (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) (g := ContinuousLinearMap.compL 𝕜 F₁ F₂ F₃) (ContinuousLinearMap.contDiff _) hf nonrec theorem ContMDiffAt.clm_postcomp {f : M → F₂ →L[𝕜] F₃} {x : M} (hf : ContMDiffAt I 𝓘(𝕜, F₂ →L[𝕜] F₃) n f x) : ContMDiffAt I 𝓘(𝕜, (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).postcomp F₁ : M → (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) x := hf.clm_postcomp nonrec theorem ContMDiffOn.clm_postcomp {f : M → F₂ →L[𝕜] F₃} {s : Set M} (hf : ContMDiffOn I 𝓘(𝕜, F₂ →L[𝕜] F₃) n f s) : ContMDiffOn I 𝓘(𝕜, (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).postcomp F₁ : M → (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) s := fun x hx ↦ (hf x hx).clm_postcomp theorem ContMDiff.clm_postcomp {f : M → F₂ →L[𝕜] F₃} (hf : ContMDiff I 𝓘(𝕜, F₂ →L[𝕜] F₃) n f) : ContMDiff I 𝓘(𝕜, (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) n (fun y ↦ (f y).postcomp F₁ : M → (F₁ →L[𝕜] F₂) →L[𝕜] (F₁ →L[𝕜] F₃)) := fun x ↦ (hf x).clm_postcomp theorem ContMDiffWithinAt.clm_comp {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₁} {s : Set M} {x : M} (hg : ContMDiffWithinAt I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g s x) (hf : ContMDiffWithinAt I 𝓘(𝕜, F₂ →L[𝕜] F₁) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, F₂ →L[𝕜] F₃) n (fun x => (g x).comp (f x)) s x := ContDiff.comp_contMDiffWithinAt (g := fun x : (F₁ →L[𝕜] F₃) × (F₂ →L[𝕜] F₁) => x.1.comp x.2) (f := fun x => (g x, f x)) (contDiff_fst.clm_comp contDiff_snd) (hg.prodMk_space hf) theorem ContMDiffAt.clm_comp {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₁} {x : M} (hg : ContMDiffAt I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g x) (hf : ContMDiffAt I 𝓘(𝕜, F₂ →L[𝕜] F₁) n f x) : ContMDiffAt I 𝓘(𝕜, F₂ →L[𝕜] F₃) n (fun x => (g x).comp (f x)) x := (hg.contMDiffWithinAt.clm_comp hf.contMDiffWithinAt).contMDiffAt Filter.univ_mem theorem ContMDiffOn.clm_comp {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₁} {s : Set M} (hg : ContMDiffOn I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g s) (hf : ContMDiffOn I 𝓘(𝕜, F₂ →L[𝕜] F₁) n f s) : ContMDiffOn I 𝓘(𝕜, F₂ →L[𝕜] F₃) n (fun x => (g x).comp (f x)) s := fun x hx => (hg x hx).clm_comp (hf x hx) theorem ContMDiff.clm_comp {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₁} (hg : ContMDiff I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g) (hf : ContMDiff I 𝓘(𝕜, F₂ →L[𝕜] F₁) n f) : ContMDiff I 𝓘(𝕜, F₂ →L[𝕜] F₃) n fun x => (g x).comp (f x) := fun x => (hg x).clm_comp (hf x) /-- Applying a linear map to a vector is smooth within a set. Version in vector spaces. For versions in nontrivial vector bundles, see `ContMDiffWithinAt.clm_apply_of_inCoordinates` and `ContMDiffWithinAt.clm_bundle_apply`. -/ theorem ContMDiffWithinAt.clm_apply {g : M → F₁ →L[𝕜] F₂} {f : M → F₁} {s : Set M} {x : M} (hg : ContMDiffWithinAt I 𝓘(𝕜, F₁ →L[𝕜] F₂) n g s x) (hf : ContMDiffWithinAt I 𝓘(𝕜, F₁) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, F₂) n (fun x => g x (f x)) s x := ContDiffWithinAt.comp_contMDiffWithinAt (t := univ) (g := fun x : (F₁ →L[𝕜] F₂) × F₁ => x.1 x.2) (by apply ContDiff.contDiffAt; exact contDiff_fst.clm_apply contDiff_snd) (hg.prodMk_space hf) (by simp_rw [preimage_univ, subset_univ]) /-- Applying a linear map to a vector is smooth. Version in vector spaces. For versions in nontrivial vector bundles, see `ContMDiffAt.clm_apply_of_inCoordinates` and `ContMDiffAt.clm_bundle_apply`. -/ nonrec theorem ContMDiffAt.clm_apply {g : M → F₁ →L[𝕜] F₂} {f : M → F₁} {x : M} (hg : ContMDiffAt I 𝓘(𝕜, F₁ →L[𝕜] F₂) n g x) (hf : ContMDiffAt I 𝓘(𝕜, F₁) n f x) : ContMDiffAt I 𝓘(𝕜, F₂) n (fun x => g x (f x)) x := hg.clm_apply hf theorem ContMDiffOn.clm_apply {g : M → F₁ →L[𝕜] F₂} {f : M → F₁} {s : Set M} (hg : ContMDiffOn I 𝓘(𝕜, F₁ →L[𝕜] F₂) n g s) (hf : ContMDiffOn I 𝓘(𝕜, F₁) n f s) : ContMDiffOn I 𝓘(𝕜, F₂) n (fun x => g x (f x)) s := fun x hx => (hg x hx).clm_apply (hf x hx) theorem ContMDiff.clm_apply {g : M → F₁ →L[𝕜] F₂} {f : M → F₁} (hg : ContMDiff I 𝓘(𝕜, F₁ →L[𝕜] F₂) n g) (hf : ContMDiff I 𝓘(𝕜, F₁) n f) : ContMDiff I 𝓘(𝕜, F₂) n fun x => g x (f x) := fun x => (hg x).clm_apply (hf x) theorem ContMDiffWithinAt.cle_arrowCongr {f : M → F₁ ≃L[𝕜] F₂} {g : M → F₃ ≃L[𝕜] F₄} {s : Set M} {x : M} (hf : ContMDiffWithinAt I 𝓘(𝕜, F₂ →L[𝕜] F₁) n (fun x ↦ ((f x).symm : F₂ →L[𝕜] F₁)) s x) (hg : ContMDiffWithinAt I 𝓘(𝕜, F₃ →L[𝕜] F₄) n (fun x ↦ (g x : F₃ →L[𝕜] F₄)) s x) : ContMDiffWithinAt I 𝓘(𝕜, (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) n (fun y ↦ (f y).arrowCongr (g y) : M → (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) s x := show ContMDiffWithinAt I 𝓘(𝕜, (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) n (fun y ↦ (((f y).symm : F₂ →L[𝕜] F₁).precomp F₄).comp ((g y : F₃ →L[𝕜] F₄).postcomp F₁)) s x from hf.clm_precomp (F₃ := F₄) |>.clm_comp <| hg.clm_postcomp (F₁ := F₁) nonrec theorem ContMDiffAt.cle_arrowCongr {f : M → F₁ ≃L[𝕜] F₂} {g : M → F₃ ≃L[𝕜] F₄} {x : M} (hf : ContMDiffAt I 𝓘(𝕜, F₂ →L[𝕜] F₁) n (fun x ↦ ((f x).symm : F₂ →L[𝕜] F₁)) x) (hg : ContMDiffAt I 𝓘(𝕜, F₃ →L[𝕜] F₄) n (fun x ↦ (g x : F₃ →L[𝕜] F₄)) x) : ContMDiffAt I 𝓘(𝕜, (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) n (fun y ↦ (f y).arrowCongr (g y) : M → (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) x := hf.cle_arrowCongr hg theorem ContMDiffOn.cle_arrowCongr {f : M → F₁ ≃L[𝕜] F₂} {g : M → F₃ ≃L[𝕜] F₄} {s : Set M} (hf : ContMDiffOn I 𝓘(𝕜, F₂ →L[𝕜] F₁) n (fun x ↦ ((f x).symm : F₂ →L[𝕜] F₁)) s) (hg : ContMDiffOn I 𝓘(𝕜, F₃ →L[𝕜] F₄) n (fun x ↦ (g x : F₃ →L[𝕜] F₄)) s) : ContMDiffOn I 𝓘(𝕜, (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) n (fun y ↦ (f y).arrowCongr (g y) : M → (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) s := fun x hx ↦ (hf x hx).cle_arrowCongr (hg x hx) theorem ContMDiff.cle_arrowCongr {f : M → F₁ ≃L[𝕜] F₂} {g : M → F₃ ≃L[𝕜] F₄} (hf : ContMDiff I 𝓘(𝕜, F₂ →L[𝕜] F₁) n (fun x ↦ ((f x).symm : F₂ →L[𝕜] F₁))) (hg : ContMDiff I 𝓘(𝕜, F₃ →L[𝕜] F₄) n (fun x ↦ (g x : F₃ →L[𝕜] F₄))) : ContMDiff I 𝓘(𝕜, (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) n (fun y ↦ (f y).arrowCongr (g y) : M → (F₁ →L[𝕜] F₃) →L[𝕜] (F₂ →L[𝕜] F₄)) := fun x ↦ (hf x).cle_arrowCongr (hg x) theorem ContMDiffWithinAt.clm_prodMap {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₄} {s : Set M} {x : M} (hg : ContMDiffWithinAt I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g s x) (hf : ContMDiffWithinAt I 𝓘(𝕜, F₂ →L[𝕜] F₄) n f s x) : ContMDiffWithinAt I 𝓘(𝕜, F₁ × F₂ →L[𝕜] F₃ × F₄) n (fun x => (g x).prodMap (f x)) s x := ContDiff.comp_contMDiffWithinAt (g := fun x : (F₁ →L[𝕜] F₃) × (F₂ →L[𝕜] F₄) => x.1.prodMap x.2) (f := fun x => (g x, f x)) (ContinuousLinearMap.prodMapL 𝕜 F₁ F₃ F₂ F₄).contDiff (hg.prodMk_space hf) nonrec theorem ContMDiffAt.clm_prodMap {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₄} {x : M} (hg : ContMDiffAt I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g x) (hf : ContMDiffAt I 𝓘(𝕜, F₂ →L[𝕜] F₄) n f x) : ContMDiffAt I 𝓘(𝕜, F₁ × F₂ →L[𝕜] F₃ × F₄) n (fun x => (g x).prodMap (f x)) x := hg.clm_prodMap hf theorem ContMDiffOn.clm_prodMap {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₄} {s : Set M} (hg : ContMDiffOn I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g s) (hf : ContMDiffOn I 𝓘(𝕜, F₂ →L[𝕜] F₄) n f s) : ContMDiffOn I 𝓘(𝕜, F₁ × F₂ →L[𝕜] F₃ × F₄) n (fun x => (g x).prodMap (f x)) s := fun x hx => (hg x hx).clm_prodMap (hf x hx) theorem ContMDiff.clm_prodMap {g : M → F₁ →L[𝕜] F₃} {f : M → F₂ →L[𝕜] F₄} (hg : ContMDiff I 𝓘(𝕜, F₁ →L[𝕜] F₃) n g) (hf : ContMDiff I 𝓘(𝕜, F₂ →L[𝕜] F₄) n f) : ContMDiff I 𝓘(𝕜, F₁ × F₂ →L[𝕜] F₃ × F₄) n fun x => (g x).prodMap (f x) := fun x => (hg x).clm_prodMap (hf x) /-! ### Smoothness of scalar multiplication -/ variable {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] /-- On any vector space, multiplication by a scalar is a smooth operation. -/ theorem contMDiff_smul : ContMDiff (𝓘(𝕜).prod 𝓘(𝕜, V)) 𝓘(𝕜, V) ⊤ fun p : 𝕜 × V => p.1 • p.2 := contMDiff_iff.2 ⟨continuous_smul, fun _ _ => contDiff_smul.contDiffOn⟩ theorem ContMDiffWithinAt.smul {f : M → 𝕜} {g : M → V} (hf : ContMDiffWithinAt I 𝓘(𝕜) n f s x) (hg : ContMDiffWithinAt I 𝓘(𝕜, V) n g s x) : ContMDiffWithinAt I 𝓘(𝕜, V) n (fun p => f p • g p) s x := (contMDiff_smul.of_le le_top).contMDiffAt.comp_contMDiffWithinAt x (hf.prodMk hg) nonrec theorem ContMDiffAt.smul {f : M → 𝕜} {g : M → V} (hf : ContMDiffAt I 𝓘(𝕜) n f x) (hg : ContMDiffAt I 𝓘(𝕜, V) n g x) : ContMDiffAt I 𝓘(𝕜, V) n (fun p => f p • g p) x := hf.smul hg theorem ContMDiffOn.smul {f : M → 𝕜} {g : M → V} (hf : ContMDiffOn I 𝓘(𝕜) n f s) (hg : ContMDiffOn I 𝓘(𝕜, V) n g s) : ContMDiffOn I 𝓘(𝕜, V) n (fun p => f p • g p) s := fun x hx => (hf x hx).smul (hg x hx) theorem ContMDiff.smul {f : M → 𝕜} {g : M → V} (hf : ContMDiff I 𝓘(𝕜) n f) (hg : ContMDiff I 𝓘(𝕜, V) n g) : ContMDiff I 𝓘(𝕜, V) n fun p => f p • g p := fun x => (hf x).smul (hg x)
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Sheaf/LocallyRingedSpace.lean
import Mathlib.Geometry.Manifold.Sheaf.Smooth import Mathlib.Geometry.RingedSpace.LocallyRingedSpace /-! # Smooth manifolds as locally ringed spaces This file equips a smooth manifold with the structure of a locally ringed space. ## Main results * `smoothSheafCommRing.isUnit_stalk_iff`: The units of the stalk at `x` of the sheaf of smooth functions from a smooth manifold `M` to its scalar field `𝕜`, considered as a sheaf of commutative rings, are the functions whose values at `x` are nonzero. ## Main definitions * `IsManifold.locallyRingedSpace`: A smooth manifold can be considered as a locally ringed space. ## TODO Characterize morphisms-of-locally-ringed-spaces (`AlgebraicGeometry.LocallyRingedSpace.Hom`) between smooth manifolds. -/ noncomputable section universe u open scoped ContDiff variable {𝕜 : Type u} [NontriviallyNormedField 𝕜] {EM : Type*} [NormedAddCommGroup EM] [NormedSpace 𝕜 EM] {HM : Type*} [TopologicalSpace HM] (IM : ModelWithCorners 𝕜 EM HM) {M : Type u} [TopologicalSpace M] [ChartedSpace HM M] open AlgebraicGeometry Manifold TopologicalSpace Topology /-- The units of the stalk at `x` of the sheaf of smooth functions from `M` to `𝕜`, considered as a sheaf of commutative rings, are the functions whose values at `x` are nonzero. -/ theorem smoothSheafCommRing.isUnit_stalk_iff {x : M} (f : (smoothSheafCommRing IM 𝓘(𝕜) M 𝕜).presheaf.stalk x) : IsUnit f ↔ f ∉ RingHom.ker (smoothSheafCommRing.eval IM 𝓘(𝕜) M 𝕜 x) := by constructor · rintro ⟨⟨f, g, hf, hg⟩, rfl⟩ (h' : smoothSheafCommRing.eval IM 𝓘(𝕜) M 𝕜 x f = 0) simpa [h'] using congr_arg (smoothSheafCommRing.eval IM 𝓘(𝕜) M 𝕜 x) hf · let S := (smoothSheafCommRing IM 𝓘(𝕜) M 𝕜).presheaf -- Suppose that `f`, in the stalk at `x`, is nonzero at `x` rintro (hf : _ ≠ 0) -- Represent `f` as the germ of some function (also called `f`) on an open neighbourhood `U` of -- `x`, which is nonzero at `x` obtain ⟨U : Opens M, hxU, f : C^∞⟮IM, U; 𝓘(𝕜), 𝕜⟯, rfl⟩ := S.germ_exist x f have hf' : f ⟨x, hxU⟩ ≠ 0 := by convert hf exact (smoothSheafCommRing.eval_germ U x hxU f).symm -- In fact, by continuity, `f` is nonzero on a neighbourhood `V` of `x` have H : ∀ᶠ (z : U) in 𝓝 ⟨x, hxU⟩, f z ≠ 0 := f.2.continuous.continuousAt.eventually_ne hf' rw [eventually_nhds_iff] at H obtain ⟨V₀, hV₀f, hV₀, hxV₀⟩ := H let V : Opens M := ⟨Subtype.val '' V₀, U.2.isOpenMap_subtype_val V₀ hV₀⟩ have hUV : V ≤ U := Subtype.coe_image_subset (U : Set M) V₀ have hV : V₀ = Set.range (Set.inclusion hUV) := by convert (Set.range_inclusion hUV).symm ext y change _ ↔ y ∈ Subtype.val ⁻¹' (Subtype.val '' V₀) rw [Set.preimage_image_eq _ Subtype.coe_injective] clear_value V subst hV have hxV : x ∈ (V : Set M) := by obtain ⟨x₀, hxx₀⟩ := hxV₀ convert x₀.2 exact congr_arg Subtype.val hxx₀.symm have hVf : ∀ y : V, f (Set.inclusion hUV y) ≠ 0 := fun y ↦ hV₀f (Set.inclusion hUV y) (Set.mem_range_self y) -- Let `g` be the pointwise inverse of `f` on `V`, which is smooth since `f` is nonzero there let g : C^∞⟮IM, V; 𝓘(𝕜), 𝕜⟯ := ⟨(f ∘ Set.inclusion hUV)⁻¹, ?_⟩ -- The germ of `g` is inverse to the germ of `f`, so `f` is a unit · refine ⟨⟨S.germ _ x (hxV) (ContMDiffMap.restrictRingHom IM 𝓘(𝕜) 𝕜 hUV f), S.germ _ x hxV g, ?_, ?_⟩, S.germ_res_apply hUV.hom x hxV f⟩ · rw [← map_mul] -- Qualified the name to avoid Lean not finding a `OneHomClass` https://github.com/leanprover-community/mathlib4/pull/8386 convert RingHom.map_one _ apply Subtype.ext ext y apply mul_inv_cancel₀ exact hVf y · rw [← map_mul] -- Qualified the name to avoid Lean not finding a `OneHomClass` https://github.com/leanprover-community/mathlib4/pull/8386 convert RingHom.map_one _ apply Subtype.ext ext y apply inv_mul_cancel₀ exact hVf y · intro y exact (((contDiffAt_inv _ (hVf y)).contMDiffAt).comp y (f.contMDiff.comp (contMDiff_inclusion hUV)).contMDiffAt :) /-- The non-units of the stalk at `x` of the sheaf of smooth functions from `M` to `𝕜`, considered as a sheaf of commutative rings, are the functions whose values at `x` are zero. -/ theorem smoothSheafCommRing.nonunits_stalk (x : M) : nonunits ((smoothSheafCommRing IM 𝓘(𝕜) M 𝕜).presheaf.stalk x) = RingHom.ker (smoothSheafCommRing.eval IM 𝓘(𝕜) M 𝕜 x) := by ext1 f rw [mem_nonunits_iff, not_iff_comm, Iff.comm] apply smoothSheafCommRing.isUnit_stalk_iff /-- The stalks of the structure sheaf of a smooth manifold are local rings. -/ instance smoothSheafCommRing.instLocalRing_stalk (x : M) : IsLocalRing ((smoothSheafCommRing IM 𝓘(𝕜) M 𝕜).presheaf.stalk x) := by apply IsLocalRing.of_nonunits_add rw [smoothSheafCommRing.nonunits_stalk] intro f g exact Ideal.add_mem _ variable (M) /-- A smooth manifold can be considered as a locally ringed space. -/ def IsManifold.locallyRingedSpace : LocallyRingedSpace where carrier := TopCat.of M presheaf := smoothPresheafCommRing IM 𝓘(𝕜) M 𝕜 IsSheaf := (smoothSheafCommRing IM 𝓘(𝕜) M 𝕜).cond isLocalRing x := smoothSheafCommRing.instLocalRing_stalk IM x
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Sheaf/Basic.lean
import Mathlib.Geometry.Manifold.LocalInvariantProperties import Mathlib.Topology.Sheaves.LocalPredicate /-! # Generic construction of a sheaf from a `LocalInvariantProp` on a manifold This file constructs the sheaf-of-types of functions `f : M → M'` (for charted spaces `M`, `M'`) which satisfy the lifted property `LiftProp P` associated to some locally invariant (in the sense of `StructureGroupoid.LocalInvariantProp`) property `P` on the model spaces of `M` and `M'`. For example, differentiability and smoothness are locally invariant properties in this sense, so this construction can be used to construct the sheaf of differentiable functions on a manifold and the sheaf of smooth functions on a manifold. The mathematical work is in associating a `TopCat.LocalPredicate` to a `StructureGroupoid.LocalInvariantProp`: that is, showing that a differential-geometric "locally invariant" property is preserved under restriction and gluing. ## Main definitions * `StructureGroupoid.LocalInvariantProp.localPredicate`: the `TopCat.LocalPredicate` (in the sheaf-theoretic sense) on functions from open subsets of `M` into `M'`, which states whether such functions satisfy `LiftProp P`. * `StructureGroupoid.LocalInvariantProp.sheaf`: the sheaf-of-types of functions `f : M → M'` which satisfy the lifted property `LiftProp P`. -/ open scoped Manifold Topology open Set TopologicalSpace StructureGroupoid StructureGroupoid.LocalInvariantProp Opposite universe u variable {H : Type*} [TopologicalSpace H] {H' : Type*} [TopologicalSpace H'] {G : StructureGroupoid H} {G' : StructureGroupoid H'} {P : (H → H') → Set H → H → Prop} (M : Type u) [TopologicalSpace M] [ChartedSpace H M] (M' : Type u) [TopologicalSpace M'] [ChartedSpace H' M'] instance TopCat.of.chartedSpace : ChartedSpace H (TopCat.of M) := (inferInstance : ChartedSpace H M) instance TopCat.of.hasGroupoid [HasGroupoid M G] : HasGroupoid (TopCat.of M) G := (inferInstance : HasGroupoid M G) /-- Let `P` be a `LocalInvariantProp` for functions between spaces with the groupoids `G`, `G'` and let `M`, `M'` be charted spaces modelled on the model spaces of those groupoids. Then there is an induced `LocalPredicate` on the functions from `M` to `M'`, given by `LiftProp P`. -/ def StructureGroupoid.LocalInvariantProp.localPredicate (hG : LocalInvariantProp G G' P) : TopCat.LocalPredicate fun _ : TopCat.of M => M' where pred {U : Opens (TopCat.of M)} := fun f : U → M' => ChartedSpace.LiftProp P f res := by intro U V i f h x have hUV : U ≤ V := CategoryTheory.leOfHom i change ChartedSpace.LiftPropAt P (f ∘ Opens.inclusion hUV) x rw [← hG.liftPropAt_iff_comp_inclusion hUV] apply h locality := by intro V f h x obtain ⟨U, hxU, i, hU : ChartedSpace.LiftProp P (f ∘ _)⟩ := h x let x' : U := ⟨x, hxU⟩ have hUV : U ≤ V := CategoryTheory.leOfHom i have : ChartedSpace.LiftPropAt P f (Opens.inclusion hUV x') := by rw [hG.liftPropAt_iff_comp_inclusion hUV] exact hU x' convert this /-- Let `P` be a `LocalInvariantProp` for functions between spaces with the groupoids `G`, `G'` and let `M`, `M'` be charted spaces modelled on the model spaces of those groupoids. Then there is a sheaf of types on `M` which, to each open set `U` in `M`, associates the type of bundled functions from `U` to `M'` satisfying the lift of `P`. -/ def StructureGroupoid.LocalInvariantProp.sheaf (hG : LocalInvariantProp G G' P) : TopCat.Sheaf (Type u) (TopCat.of M) := TopCat.subsheafToTypes (hG.localPredicate M M') instance StructureGroupoid.LocalInvariantProp.sheafHasCoeToFun (hG : LocalInvariantProp G G' P) (U : (Opens (TopCat.of M))ᵒᵖ) : CoeFun ((hG.sheaf M M').val.obj U) fun _ => ↑(unop U) → M' where coe a := a.1 theorem StructureGroupoid.LocalInvariantProp.section_spec (hG : LocalInvariantProp G G' P) (U : (Opens (TopCat.of M))ᵒᵖ) (f : (hG.sheaf M M').val.obj U) : ChartedSpace.LiftProp P f := f.2
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Sheaf/Smooth.lean
import Mathlib.Algebra.Category.Ring.Colimits import Mathlib.Algebra.Category.Ring.FilteredColimits import Mathlib.Algebra.Category.Ring.Limits import Mathlib.CategoryTheory.Sites.Whiskering import Mathlib.Geometry.Manifold.Algebra.SmoothFunctions import Mathlib.Geometry.Manifold.Sheaf.Basic /-! # The sheaf of smooth functions on a manifold The sheaf of `𝕜`-smooth functions from a manifold `M` to a manifold `N` can be defined as a sheaf of types using the construction `StructureGroupoid.LocalInvariantProp.sheaf` from the file `Mathlib/Geometry/Manifold/Sheaf/Basic.lean`. In this file we write that down (a one-liner), then do the work of upgrading this to a sheaf of [groups]/[abelian groups]/[rings]/[commutative rings] when `N` carries more algebraic structure. For example, if `N` is `𝕜` then the sheaf of smooth functions from `M` to `𝕜` is a sheaf of commutative rings, the *structure sheaf* of `M`. ## Main definitions * `smoothSheaf`: The sheaf of smooth functions from `M` to `N`, as a sheaf of types * `smoothSheaf.eval`: Canonical map onto `N` from the stalk of `smoothSheaf IM I M N` at `x`, given by evaluating sections at `x` * `smoothSheafGroup`, `smoothSheafCommGroup`, `smoothSheafRing`, `smoothSheafCommRing`: The sheaf of smooth functions into a [Lie group]/[abelian Lie group]/[smooth ring]/[smooth commutative ring], as a sheaf of [groups]/[abelian groups]/[rings]/[commutative rings] * `smoothSheafCommRing.forgetStalk`: Identify the stalk at a point of the sheaf-of-commutative-rings of functions from `M` to `R` (for `R` a smooth ring) with the stalk at that point of the corresponding sheaf of types. * `smoothSheafCommRing.eval`: upgrade `smoothSheaf.eval` to a ring homomorphism when considering the sheaf of smooth functions into a smooth commutative ring * `smoothSheafCommGroup.compLeft`: For a manifold `M` and a smooth homomorphism `φ` between abelian Lie groups `A`, `A'`, the 'postcomposition-by-`φ`' morphism of sheaves from `smoothSheafCommGroup IM I M A` to `smoothSheafCommGroup IM I' M A'` ## Main results * `smoothSheaf.eval_surjective`: `smoothSheaf.eval` is surjective. * `smoothSheafCommRing.eval_surjective`: `smoothSheafCommRing.eval` is surjective. ## TODO There are variants of `smoothSheafCommGroup.compLeft` for `GrpCat`, `RingCat`, `CommRingCat`; this is just boilerplate and can be added as needed. Similarly, there are variants of `smoothSheafCommRing.forgetStalk` and `smoothSheafCommRing.eval` for `GrpCat`, `CommGrpCat` and `RingCat` which can be added as needed. Currently there is a universe restriction: one can consider the sheaf of smooth functions from `M` to `N` only if `M` and `N` are in the same universe. For example, since `ℂ` is in `Type`, we can only consider the structure sheaf of complex manifolds in `Type`, which is unsatisfactory. The obstacle here is in the underlying category theory constructions, which are not sufficiently universe polymorphic. A direct attempt to generalize the universes worked in Lean 3 but was reverted because it was hard to port to Lean 4, see https://github.com/leanprover-community/mathlib/pull/19230 The current (Oct 2023) proposal to permit these generalizations is to use the new `UnivLE` typeclass, and some (but not all) of the underlying category theory constructions have now been generalized by this method: see https://github.com/leanprover-community/mathlib4/pull/5724, https://github.com/leanprover-community/mathlib4/pull/5726. -/ noncomputable section open TopologicalSpace Opposite open scoped ContDiff universe u variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {EM : Type*} [NormedAddCommGroup EM] [NormedSpace 𝕜 EM] {HM : Type*} [TopologicalSpace HM] (IM : ModelWithCorners 𝕜 EM HM) variable {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 E H') (M : Type u) [TopologicalSpace M] [ChartedSpace HM M] (N G A A' R : Type u) [TopologicalSpace N] [ChartedSpace H N] [TopologicalSpace G] [ChartedSpace H G] [TopologicalSpace A] [ChartedSpace H A] [TopologicalSpace A'] [ChartedSpace H' A'] [TopologicalSpace R] [ChartedSpace H R] section TypeCat /-- The sheaf of smooth functions from `M` to `N`, as a sheaf of types. -/ def smoothSheaf : TopCat.Sheaf (Type u) (TopCat.of M) := (contDiffWithinAt_localInvariantProp (I := IM) (I' := I) ∞).sheaf M N variable {M} instance smoothSheaf.coeFun (U : (Opens (TopCat.of M))ᵒᵖ) : CoeFun ((smoothSheaf IM I M N).presheaf.obj U) (fun _ ↦ ↑(unop U) → N) := (contDiffWithinAt_localInvariantProp ∞).sheafHasCoeToFun _ _ _ open Manifold in /-- The object of `smoothSheaf IM I M N` for the open set `U` in `M` is `C^∞⟮IM, (unop U : Opens M); I, N⟯`, the `(IM, I)`-smooth functions from `U` to `N`. This is not just a "moral" equality but a literal and definitional equality! -/ lemma smoothSheaf.obj_eq (U : (Opens (TopCat.of M))ᵒᵖ) : (smoothSheaf IM I M N).presheaf.obj U = C^∞⟮IM, (unop U : Opens M); I, N⟯ := rfl /-- Canonical map from the stalk of `smoothSheaf IM I M N` at `x` to `N`, given by evaluating sections at `x`. -/ def smoothSheaf.eval (x : M) : (smoothSheaf IM I M N).presheaf.stalk x → N := TopCat.stalkToFiber (StructureGroupoid.LocalInvariantProp.localPredicate M N _) x /-- Canonical map from the stalk of `smoothSheaf IM I M N` at `x` to `N`, given by evaluating sections at `x`, considered as a morphism in the category of types. -/ def smoothSheaf.evalHom (x : TopCat.of M) : (smoothSheaf IM I M N).presheaf.stalk x ⟶ N := TopCat.stalkToFiber (StructureGroupoid.LocalInvariantProp.localPredicate M N _) x open CategoryTheory Limits /-- Given manifolds `M`, `N` and an open neighbourhood `U` of a point `x : M`, the evaluation-at-`x` map to `N` from smooth functions from `U` to `N`. -/ def smoothSheaf.evalAt (x : TopCat.of M) (U : OpenNhds x) (i : (smoothSheaf IM I M N).presheaf.obj (Opposite.op U.obj)) : N := i.1 ⟨x, U.2⟩ @[simp, reassoc, elementwise] lemma smoothSheaf.ι_evalHom (x : TopCat.of M) (U) : colimit.ι ((OpenNhds.inclusion x).op ⋙ (smoothSheaf IM I M N).val) U ≫ smoothSheaf.evalHom IM I N x = smoothSheaf.evalAt _ _ _ _ _ := colimit.ι_desc _ _ /-- The `eval` map is surjective at `x`. -/ lemma smoothSheaf.eval_surjective (x : M) : Function.Surjective (smoothSheaf.eval IM I N x) := by apply TopCat.stalkToFiber_surjective intro n exact ⟨⊤, fun _ ↦ n, contMDiff_const, rfl⟩ instance [Nontrivial N] (x : M) : Nontrivial ((smoothSheaf IM I M N).presheaf.stalk x) := (smoothSheaf.eval_surjective IM I N x).nontrivial variable {IM I N} @[simp] lemma smoothSheaf.eval_germ (U : Opens M) (x : M) (hx : x ∈ U) (f : (smoothSheaf IM I M N).presheaf.obj (op U)) : smoothSheaf.eval IM I N (x : M) ((smoothSheaf IM I M N).presheaf.germ U x hx f) = f ⟨x, hx⟩ := TopCat.stalkToFiber_germ ((contDiffWithinAt_localInvariantProp ∞).localPredicate M N) _ _ _ _ lemma smoothSheaf.contMDiff_section {U : (Opens (TopCat.of M))ᵒᵖ} (f : (smoothSheaf IM I M N).presheaf.obj U) : ContMDiff IM I ∞ f := (contDiffWithinAt_localInvariantProp ∞).section_spec _ _ _ _ end TypeCat section LieGroup variable [Group G] [LieGroup I ∞ G] open Manifold in @[to_additive] noncomputable instance (U : (Opens (TopCat.of M))ᵒᵖ) : Group ((smoothSheaf IM I M G).presheaf.obj U) := (ContMDiffMap.group : Group C^∞⟮IM, (unop U : Opens M); I, G⟯) /-- The presheaf of smooth functions from `M` to `G`, for `G` a Lie group, as a presheaf of groups. -/ @[to_additive /-- The presheaf of smooth functions from `M` to `G`, for `G` an additive Lie group, as a presheaf of additive groups. -/] noncomputable def smoothPresheafGroup : TopCat.Presheaf GrpCat.{u} (TopCat.of M) := { obj := fun U ↦ GrpCat.of ((smoothSheaf IM I M G).presheaf.obj U) map := fun h ↦ GrpCat.ofHom <| ContMDiffMap.restrictMonoidHom IM I G <| CategoryTheory.leOfHom h.unop map_id := fun _ ↦ rfl map_comp := fun _ _ ↦ rfl } /-- The sheaf of smooth functions from `M` to `G`, for `G` a Lie group, as a sheaf of groups. -/ @[to_additive /-- The sheaf of smooth functions from `M` to `G`, for `G` an additive Lie group, as a sheaf of additive groups. -/] noncomputable def smoothSheafGroup : TopCat.Sheaf GrpCat.{u} (TopCat.of M) := { val := smoothPresheafGroup IM I M G cond := by rw [CategoryTheory.Presheaf.isSheaf_iff_isSheaf_forget _ _ (CategoryTheory.forget GrpCat)] exact CategoryTheory.Sheaf.cond (smoothSheaf IM I M G) } end LieGroup section CommLieGroup variable [CommGroup A] [CommGroup A'] [LieGroup I ∞ A] [LieGroup I' ∞ A'] open Manifold in @[to_additive] noncomputable instance (U : (Opens (TopCat.of M))ᵒᵖ) : CommGroup ((smoothSheaf IM I M A).presheaf.obj U) := (ContMDiffMap.commGroup : CommGroup C^∞⟮IM, (unop U : Opens M); I, A⟯) /-- The presheaf of smooth functions from `M` to `A`, for `A` an abelian Lie group, as a presheaf of abelian groups. -/ @[to_additive /-- The presheaf of smooth functions from `M` to `A`, for `A` an additive abelian Lie group, as a presheaf of additive abelian groups. -/] noncomputable def smoothPresheafCommGroup : TopCat.Presheaf CommGrpCat.{u} (TopCat.of M) := { obj := fun U ↦ CommGrpCat.of ((smoothSheaf IM I M A).presheaf.obj U) map := fun h ↦ CommGrpCat.ofHom <| ContMDiffMap.restrictMonoidHom IM I A <| CategoryTheory.leOfHom h.unop map_id := fun _ ↦ rfl map_comp := fun _ _ ↦ rfl } /-- The sheaf of smooth functions from `M` to `A`, for `A` an abelian Lie group, as a sheaf of abelian groups. -/ @[to_additive /-- The sheaf of smooth functions from `M` to `A`, for `A` an abelian additive Lie group, as a sheaf of abelian additive groups. -/] noncomputable def smoothSheafCommGroup : TopCat.Sheaf CommGrpCat.{u} (TopCat.of M) := { val := smoothPresheafCommGroup IM I M A cond := by rw [CategoryTheory.Presheaf.isSheaf_iff_isSheaf_forget _ _ (CategoryTheory.forget CommGrpCat)] exact CategoryTheory.Sheaf.cond (smoothSheaf IM I M A) } /-- For a manifold `M` and a smooth homomorphism `φ` between abelian Lie groups `A`, `A'`, the 'left-composition-by-`φ`' morphism of sheaves from `smoothSheafCommGroup IM I M A` to `smoothSheafCommGroup IM I' M A'`. -/ @[to_additive /-- For a manifold `M` and a smooth homomorphism `φ` between abelian additive Lie groups `A`, `A'`, the 'left-composition-by-`φ`' morphism of sheaves from `smoothSheafAddCommGroup IM I M A` to `smoothSheafAddCommGroup IM I' M A'`. -/] noncomputable def smoothSheafCommGroup.compLeft (φ : A →* A') (hφ : ContMDiff I I' ∞ φ) : smoothSheafCommGroup IM I M A ⟶ smoothSheafCommGroup IM I' M A' := CategoryTheory.Sheaf.Hom.mk <| { app := fun _ ↦ CommGrpCat.ofHom <| ContMDiffMap.compLeftMonoidHom _ _ φ hφ naturality := fun _ _ _ ↦ rfl } end CommLieGroup section ContMDiffRing variable [Ring R] [ContMDiffRing I ∞ R] open Manifold in instance (U : (Opens (TopCat.of M))ᵒᵖ) : Ring ((smoothSheaf IM I M R).presheaf.obj U) := (ContMDiffMap.ring : Ring C^∞⟮IM, (unop U : Opens M); I, R⟯) /-- The presheaf of smooth functions from `M` to `R`, for `R` a smooth ring, as a presheaf of rings. -/ def smoothPresheafRing : TopCat.Presheaf RingCat.{u} (TopCat.of M) := { obj := fun U ↦ RingCat.of ((smoothSheaf IM I M R).presheaf.obj U) map := fun h ↦ RingCat.ofHom <| ContMDiffMap.restrictRingHom IM I R <| CategoryTheory.leOfHom h.unop map_id := fun _ ↦ rfl map_comp := fun _ _ ↦ rfl } /-- The sheaf of smooth functions from `M` to `R`, for `R` a smooth ring, as a sheaf of rings. -/ def smoothSheafRing : TopCat.Sheaf RingCat.{u} (TopCat.of M) := { val := smoothPresheafRing IM I M R cond := by rw [CategoryTheory.Presheaf.isSheaf_iff_isSheaf_forget _ _ (CategoryTheory.forget RingCat)] exact CategoryTheory.Sheaf.cond (smoothSheaf IM I M R) } end ContMDiffRing section SmoothCommRing variable [CommRing R] [ContMDiffRing I ∞ R] open Manifold in instance (U : (Opens (TopCat.of M))ᵒᵖ) : CommRing ((smoothSheaf IM I M R).presheaf.obj U) := (ContMDiffMap.commRing : CommRing C^∞⟮IM, (unop U : Opens M); I, R⟯) /-- The presheaf of smooth functions from `M` to `R`, for `R` a smooth commutative ring, as a presheaf of commutative rings. -/ def smoothPresheafCommRing : TopCat.Presheaf CommRingCat.{u} (TopCat.of M) := { obj := fun U ↦ CommRingCat.of ((smoothSheaf IM I M R).presheaf.obj U) map := fun h ↦ CommRingCat.ofHom <| ContMDiffMap.restrictRingHom IM I R <| CategoryTheory.leOfHom h.unop map_id := fun _ ↦ rfl map_comp := fun _ _ ↦ rfl } /-- The sheaf of smooth functions from `M` to `R`, for `R` a smooth commutative ring, as a sheaf of commutative rings. -/ def smoothSheafCommRing : TopCat.Sheaf CommRingCat.{u} (TopCat.of M) := { val := smoothPresheafCommRing IM I M R cond := by rw [CategoryTheory.Presheaf.isSheaf_iff_isSheaf_forget _ _ (CategoryTheory.forget CommRingCat)] exact CategoryTheory.Sheaf.cond (smoothSheaf IM I M R) } -- sanity check: applying the `CommRingCat`-to-`TypeCat` forgetful functor to the sheaf-of-rings of -- smooth functions gives the sheaf-of-types of smooth functions. example : (CategoryTheory.sheafCompose _ (CategoryTheory.forget CommRingCat.{u})).obj (smoothSheafCommRing IM I M R) = (smoothSheaf IM I M R) := rfl instance smoothSheafCommRing.coeFun (U : (Opens (TopCat.of M))ᵒᵖ) : CoeFun ((smoothSheafCommRing IM I M R).presheaf.obj U) (fun _ ↦ ↑(unop U) → R) := (contDiffWithinAt_localInvariantProp ∞).sheafHasCoeToFun _ _ _ open CategoryTheory Limits /-- Identify the stalk at a point of the sheaf-of-commutative-rings of functions from `M` to `R` (for `R` a smooth ring) with the stalk at that point of the corresponding sheaf of types. -/ def smoothSheafCommRing.forgetStalk (x : TopCat.of M) : ((smoothSheafCommRing IM I M R).presheaf.stalk x).carrier ≅ (smoothSheaf IM I M R).presheaf.stalk x := preservesColimitIso (forget _) _ @[simp, reassoc, elementwise] lemma smoothSheafCommRing.ι_forgetStalk_hom (x : TopCat.of M) (U) : CategoryStruct.comp (Z := (smoothSheaf IM I M R).presheaf.stalk x) (DFunLike.coe (α := (((smoothSheafCommRing IM I M R).presheaf.obj (op ((OpenNhds.inclusion x).obj U.unop))))) (colimit.ι ((OpenNhds.inclusion x).op ⋙ (smoothSheafCommRing IM I M R).presheaf) U).hom) (forgetStalk IM I M R x).hom = colimit.ι ((OpenNhds.inclusion x).op ⋙ (smoothSheaf IM I M R).presheaf) U := ι_preservesColimitIso_hom (forget CommRingCat) _ _ @[simp, reassoc, elementwise] lemma smoothSheafCommRing.ι_forgetStalk_inv (x : TopCat.of M) (U) : colimit.ι ((OpenNhds.inclusion x).op ⋙ (smoothSheaf IM I M R).presheaf) U ≫ (smoothSheafCommRing.forgetStalk IM I M R x).inv = ⇑(colimit.ι ((OpenNhds.inclusion x).op ⋙ (smoothSheafCommRing IM I M R).presheaf) U) := by rw [Iso.comp_inv_eq, ← smoothSheafCommRing.ι_forgetStalk_hom] simp_rw [Functor.comp_obj, Functor.op_obj] /-- Given a smooth commutative ring `R` and a manifold `M`, and an open neighbourhood `U` of a point `x : M`, the evaluation-at-`x` map to `R` from smooth functions from `U` to `R`. -/ def smoothSheafCommRing.evalAt (x : TopCat.of M) (U : OpenNhds x) : (smoothSheafCommRing IM I M R).presheaf.obj (Opposite.op U.1) ⟶ CommRingCat.of R := CommRingCat.ofHom (ContMDiffMap.evalRingHom ⟨x, U.2⟩) /-- Canonical ring homomorphism from the stalk of `smoothSheafCommRing IM I M R` at `x` to `R`, given by evaluating sections at `x`, considered as a morphism in the category of commutative rings. -/ def smoothSheafCommRing.evalHom (x : TopCat.of M) : (smoothSheafCommRing IM I M R).presheaf.stalk x ⟶ CommRingCat.of R := by refine CategoryTheory.Limits.colimit.desc _ ⟨_, ⟨fun U ↦ ?_, ?_⟩⟩ · apply smoothSheafCommRing.evalAt · cat_disch /-- Canonical ring homomorphism from the stalk of `smoothSheafCommRing IM I M R` at `x` to `R`, given by evaluating sections at `x`. -/ def smoothSheafCommRing.eval (x : M) : (smoothSheafCommRing IM I M R).presheaf.stalk x →+* R := (smoothSheafCommRing.evalHom IM I M R x).hom @[simp, reassoc, elementwise] lemma smoothSheafCommRing.ι_evalHom (x : TopCat.of M) (U) : colimit.ι ((OpenNhds.inclusion x).op ⋙ _) U ≫ smoothSheafCommRing.evalHom IM I M R x = smoothSheafCommRing.evalAt _ _ _ _ _ _ := colimit.ι_desc _ _ @[simp] lemma smoothSheafCommRing.evalHom_germ (U : Opens (TopCat.of M)) (x : M) (hx : x ∈ U) (f : (smoothSheafCommRing IM I M R).presheaf.obj (op U)) : smoothSheafCommRing.evalHom IM I M R (x : TopCat.of M) ((smoothSheafCommRing IM I M R).presheaf.germ U x hx f) = f ⟨x, hx⟩ := congr_arg (fun a ↦ a f) <| smoothSheafCommRing.ι_evalHom IM I M R x ⟨U, hx⟩ @[simp, reassoc, elementwise] lemma smoothSheafCommRing.forgetStalk_inv_comp_eval (x : TopCat.of M) : (smoothSheafCommRing.forgetStalk IM I M R x).inv ≫ (DFunLike.coe (smoothSheafCommRing.evalHom IM I M R x).hom) = smoothSheaf.evalHom _ _ _ _ := by apply Limits.colimit.hom_ext intro U change (colimit.ι _ U) ≫ _ = colimit.ι ((OpenNhds.inclusion x).op ⋙ _) U ≫ _ rw [smoothSheafCommRing.ι_forgetStalk_inv_assoc, smoothSheaf.ι_evalHom] ext x exact CategoryTheory.congr_fun (smoothSheafCommRing.ι_evalHom ..) x @[simp, reassoc, elementwise] lemma smoothSheafCommRing.forgetStalk_hom_comp_evalHom (x : TopCat.of M) : (smoothSheafCommRing.forgetStalk IM I M R x).hom ≫ (smoothSheaf.evalHom IM I R x) = ⇑(smoothSheafCommRing.evalHom _ _ _ _ _) := by simp_rw [← CategoryTheory.Iso.eq_inv_comp] rw [← smoothSheafCommRing.forgetStalk_inv_comp_eval] lemma smoothSheafCommRing.eval_surjective (x) : Function.Surjective (smoothSheafCommRing.eval IM I M R x) := by intro r obtain ⟨y, rfl⟩ := smoothSheaf.eval_surjective IM I R x r use (smoothSheafCommRing.forgetStalk IM I M R x).inv y apply smoothSheafCommRing.forgetStalk_inv_comp_eval_apply instance [Nontrivial R] (x : M) : Nontrivial ((smoothSheafCommRing IM I M R).presheaf.stalk x) := (smoothSheafCommRing.eval_surjective IM I M R x).nontrivial variable {IM I M R} @[simp] lemma smoothSheafCommRing.eval_germ (U : Opens M) (x : M) (hx : x ∈ U) (f : (smoothSheafCommRing IM I M R).presheaf.obj (op U)) : smoothSheafCommRing.eval IM I M R x ((smoothSheafCommRing IM I M R).presheaf.germ U x hx f) = f ⟨x, hx⟩ := smoothSheafCommRing.evalHom_germ IM I M R U x hx f end SmoothCommRing
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Instances/Icc.lean
import Mathlib.Analysis.InnerProductSpace.Calculus import Mathlib.Geometry.Manifold.ContMDiff.Basic import Mathlib.Geometry.Manifold.Instances.Real import Mathlib.Geometry.Manifold.MFDeriv.FDeriv /-! # Manifold structure on real intervals The manifold structure on real intervals is defined in `Mathlib.Geometry.Manifold.Instances.Real`. We relate it to the manifold structure on the real line, by showing that the inclusion (`contMDiff_subtype_coe_Icc`) and projection (`contMDiffOn_projIcc`) are smooth, and showing that a function defined on the interval is smooth iff its composition with the projection is smooth on the interval in `ℝ` (see `contMDiffOn_comp_projIcc_iff` and friends). We also define `1 : TangentSpace (𝓡∂ 1) z`, and relate it to `1` in the real line. ## TODO This file can be thoroughly rewritten once mathlib has a good theory of smooth immersions and embeddings. Once this is done, - the inclusion `Icc x y → ℝ` is a smooth embedding, and in particular smooth - deduce the dual result: a function `f : M → Icc x y` is smooth iff its composition with the inclusion into `ℝ` is smooth - prove the projection `ℝ → Icc x y` is a smooth submersion, hence smooth - use this to simplify the proof that `f : Icc x y → M` is smooth iff the composition `ℝ → M` with the projection `ℝ → Icc x y` is -/ open Set WithLp open scoped Manifold Topology noncomputable section variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners ℝ E H} {n : WithTop ℕ∞} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] instance (x : ℝ) : One (TangentSpace 𝓘(ℝ) x) where one := (1 : ℝ) /-- Unit vector in the tangent space to a segment, as the image of the unit vector in the real line under the canonical projection. It is also mapped to the unit vector in the real line through the canonical injection, see `mfderiv_subtype_coe_Icc_one`. Note that one cannot abuse defeqs for this definition: this is *not* the same as the vector `fun _ ↦ 1` in `EuclideanSpace ℝ (Fin 1)` through defeqs, as one of the charts of `Icc x y` is orientation-reversing. -/ irreducible_def oneTangentSpaceIcc {x y : ℝ} [h : Fact (x < y)] (z : Icc x y) : TangentSpace (𝓡∂ 1) z := mfderivWithin 𝓘(ℝ) (𝓡∂ 1) (Set.projIcc x y h.out.le) (Icc x y) z 1 instance {x y : ℝ} [h : Fact (x < y)] (z : Icc x y) : One (TangentSpace (𝓡∂ 1) z) where one := oneTangentSpaceIcc z variable {x y : ℝ} [h : Fact (x < y)] {n : WithTop ℕ∞} /-- The inclusion map from of a closed segment to `ℝ` is smooth in the manifold sense. -/ lemma contMDiff_subtype_coe_Icc : ContMDiff (𝓡∂ 1) 𝓘(ℝ) n (fun (z : Icc x y) ↦ (z : ℝ)) := by intro z rw [contMDiffAt_iff] refine ⟨by fun_prop, ?_⟩ -- We come back to the definition: we should check that, in each chart, the map is smooth. -- There are two charts, and we check things separately in each of them using the -- explicit formulas. suffices ContDiffWithinAt ℝ n _ (range ↑(𝓡∂ 1)) _ by simpa split_ifs with hz · simp? [IccLeftChart, Function.comp_def, modelWithCornersEuclideanHalfSpace] says simp only [IccLeftChart, Fin.isValue, OpenPartialHomeomorph.mk_coe_symm, PartialEquiv.coe_symm_mk, modelWithCornersEuclideanHalfSpace, ModelWithCorners.mk_symm, Function.comp_def, Function.update_self, ModelWithCorners.mk_coe, OpenPartialHomeomorph.mk_coe] rw [Subtype.range_val_subtype] have : ContDiff ℝ n (fun (z : EuclideanSpace ℝ (Fin 1)) ↦ z 0 + x) := by fun_prop apply this.contDiffWithinAt.congr_of_eventuallyEq_of_mem; swap · simpa using z.2.1 have : {w : EuclideanSpace ℝ (Fin 1) | w 0 < y - x} ∈ 𝓝 (toLp 2 fun i ↦ z - x) := by apply (isOpen_lt (PiLp.continuous_apply 2 _ 0) continuous_const).mem_nhds simpa using hz filter_upwards [self_mem_nhdsWithin, nhdsWithin_le_nhds this] with w hw h'w rw [max_eq_left hw, min_eq_left] linarith · simp only [not_lt] at hz simp? [IccRightChart, Function.comp_def, modelWithCornersEuclideanHalfSpace] says simp only [IccRightChart, Fin.isValue, OpenPartialHomeomorph.mk_coe_symm, PartialEquiv.coe_symm_mk, modelWithCornersEuclideanHalfSpace, ModelWithCorners.mk_symm, Function.comp_def, Function.update_self, ModelWithCorners.mk_coe, OpenPartialHomeomorph.mk_coe] rw [Subtype.range_val_subtype] have : ContDiff ℝ n (fun (z : EuclideanSpace ℝ (Fin 1)) ↦ y - z 0) := by fun_prop apply this.contDiffWithinAt.congr_of_eventuallyEq_of_mem; swap · simpa using z.2.2 have : {w : EuclideanSpace ℝ (Fin 1) | w 0 < y - x} ∈ 𝓝 (toLp 2 fun i ↦ y - z) := by apply (isOpen_lt (PiLp.continuous_apply 2 _ 0) continuous_const).mem_nhds simpa using h.out.trans_le hz filter_upwards [self_mem_nhdsWithin, nhdsWithin_le_nhds this] with w hw h'w rw [max_eq_left hw, max_eq_left] linarith /-- The projection from `ℝ` to a closed segment is smooth on the segment, in the manifold sense. -/ lemma contMDiffOn_projIcc : ContMDiffOn 𝓘(ℝ) (𝓡∂ 1) n (Set.projIcc x y h.out.le) (Icc x y) := by intro z hz rw [contMDiffWithinAt_iff] refine ⟨by apply ContinuousAt.continuousWithinAt; fun_prop, ?_⟩ -- We come back to the definition: we should check that, in each chart, the map is smooth -- There are two charts, and we check things separately in each of them using the -- explicit formulas. suffices ContDiffWithinAt ℝ n _ (Icc x y) z by simpa split_ifs with h'z · have : ContDiff ℝ n (fun (w : ℝ) ↦ (show EuclideanSpace ℝ (Fin 1) from toLp 2 fun (_ : Fin 1) ↦ w - x)) := by dsimp apply contDiff_euclidean.2 (fun i ↦ by fun_prop) apply this.contDiffWithinAt.congr_of_eventuallyEq_of_mem _ hz filter_upwards [self_mem_nhdsWithin] with w hw ext i suffices max x (min y w) - x = w - x by simpa [modelWithCornersEuclideanHalfSpace, IccLeftChart] rw [max_eq_right, min_eq_right hw.2] simp [hw.1, h.out.le] · have : ContDiff ℝ n (fun (w : ℝ) ↦ (show EuclideanSpace ℝ (Fin 1) from toLp 2 fun (_ : Fin 1) ↦ y - w)) := by dsimp apply contDiff_euclidean.2 (fun i ↦ by fun_prop) apply this.contDiffWithinAt.congr_of_eventuallyEq_of_mem _ hz filter_upwards [self_mem_nhdsWithin] with w hw ext i suffices y - max x (min y w) = y - w by simpa [modelWithCornersEuclideanHalfSpace, IccRightChart] rw [max_eq_right, min_eq_right hw.2] simp [hw.1, h.out.le] lemma contMDiffOn_comp_projIcc_iff {f : Icc x y → M} : ContMDiffOn 𝓘(ℝ) I n (f ∘ (Set.projIcc x y h.out.le)) (Icc x y) ↔ ContMDiff (𝓡∂ 1) I n f := by refine ⟨fun hf ↦ ?_, fun hf ↦ hf.comp_contMDiffOn contMDiffOn_projIcc⟩ convert hf.comp_contMDiff (contMDiff_subtype_coe_Icc (x := x) (y := y)) (fun z ↦ z.2) ext z simp lemma contMDiffWithinAt_comp_projIcc_iff {f : Icc x y → M} {w : Icc x y} : ContMDiffWithinAt 𝓘(ℝ) I n (f ∘ (Set.projIcc x y h.out.le)) (Icc x y) w ↔ ContMDiffAt (𝓡∂ 1) I n f w := by refine ⟨fun hf ↦ ?_, fun hf ↦ hf.comp_contMDiffWithinAt_of_eq (contMDiffOn_projIcc w w.2) (by simp)⟩ have A := contMDiff_subtype_coe_Icc (x := x) (y := y) (n := n) w rw [← contMDiffWithinAt_univ] at A ⊢ convert hf.comp _ A (fun z hz ↦ z.2) ext z simp lemma mdifferentiableWithinAt_comp_projIcc_iff {f : Icc x y → M} {w : Icc x y} : MDifferentiableWithinAt 𝓘(ℝ) I (f ∘ (Set.projIcc x y h.out.le)) (Icc x y) w ↔ MDifferentiableAt (𝓡∂ 1) I f w := by refine ⟨fun hf ↦ ?_, fun hf ↦ ?_⟩ · have A := (contMDiff_subtype_coe_Icc (x := x) (y := y) (n := 1) w).mdifferentiableAt le_rfl rw [← mdifferentiableWithinAt_univ] at A ⊢ convert hf.comp _ A (fun z hz ↦ z.2) ext z simp · have := (contMDiffOn_projIcc (x := x) (y := y) (n := 1) w w.2).mdifferentiableWithinAt le_rfl exact MDifferentiableAt.comp_mdifferentiableWithinAt_of_eq (w : ℝ) hf this (by simp) lemma mfderivWithin_projIcc_one {z : ℝ} (hz : z ∈ Icc x y) : mfderivWithin 𝓘(ℝ) (𝓡∂ 1) (Set.projIcc x y h.out.le) (Icc x y) z 1 = 1 := by change _ = oneTangentSpaceIcc (Set.projIcc x y h.out.le z) simp only [oneTangentSpaceIcc] congr simp [projIcc_of_mem h.out.le hz] lemma mfderivWithin_comp_projIcc_one {f : Icc x y → M} {w : Icc x y} : mfderivWithin 𝓘(ℝ) I (f ∘ (projIcc x y h.out.le)) (Icc x y) w 1 = mfderiv (𝓡∂ 1) I f w 1 := by by_cases hw : MDifferentiableAt (𝓡∂ 1) I f w; swap · rw [mfderiv_zero_of_not_mdifferentiableAt hw, mfderivWithin_zero_of_not_mdifferentiableWithinAt] · rfl · rwa [mdifferentiableWithinAt_comp_projIcc_iff] rw [mfderiv_comp_mfderivWithin (I' := 𝓡∂ 1)]; rotate_left · simp [hw] · exact (contMDiffOn_projIcc _ w.2).mdifferentiableWithinAt le_rfl · exact (uniqueDiffOn_Icc h.out _ w.2).uniqueMDiffWithinAt simp only [Function.comp_apply, ContinuousLinearMap.coe_comp'] have : w = projIcc x y h.out.le (w : ℝ) := by rw [projIcc_of_mem] rw [projIcc_of_mem _ w.2] congr 1 convert mfderivWithin_projIcc_one w.2 lemma mfderiv_subtype_coe_Icc_one (z : Icc x y) : mfderiv (𝓡∂ 1) 𝓘(ℝ) (Subtype.val : Icc x y → ℝ) z 1 = 1 := by have A : mfderivWithin 𝓘(ℝ) 𝓘(ℝ) (Subtype.val ∘ (projIcc x y h.out.le)) (Icc x y) z 1 = mfderivWithin 𝓘(ℝ) 𝓘(ℝ) id (Icc x y) z 1 := by congr 1 apply mfderivWithin_congr_of_mem _ z.2 intro z hz simp [projIcc_of_mem h.out.le hz] rw [← mfderivWithin_comp_projIcc_one, A] simp only [id_eq, mfderivWithin_eq_fderivWithin] rw [fderivWithin_id (uniqueDiffOn_Icc h.out _ z.2)] rfl
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Instances/Sphere.lean
import Mathlib.Analysis.Calculus.Deriv.Inv import Mathlib.Analysis.Complex.Circle import Mathlib.Analysis.Normed.Module.Ball.Action import Mathlib.Analysis.SpecialFunctions.ExpDeriv import Mathlib.Analysis.InnerProductSpace.Calculus import Mathlib.Analysis.InnerProductSpace.PiL2 import Mathlib.Geometry.Manifold.Algebra.LieGroup import Mathlib.Geometry.Manifold.Instances.Real import Mathlib.Geometry.Manifold.MFDeriv.Basic import Mathlib.LinearAlgebra.Complex.FiniteDimensional import Mathlib.Tactic.Module /-! # Manifold structure on the sphere This file defines stereographic projection from the sphere in an inner product space `E`, and uses it to put an analytic manifold structure on the sphere. ## Main results For a unit vector `v` in `E`, the definition `stereographic` gives the stereographic projection centred at `v`, an open partial homeomorphism from the sphere to `(ℝ ∙ v)ᗮ` (the orthogonal complement of `v`). For finite-dimensional `E`, we then construct an analytic manifold instance on the sphere; the charts here are obtained by composing the open partial homeomorphisms `stereographic` with arbitrary isometries from `(ℝ ∙ v)ᗮ` to Euclidean space. We prove two lemmas about `C^n` maps: * `contMDiff_coe_sphere` states that the coercion map from the sphere into `E` is analytic; this is a useful tool for constructing smooth maps *from* the sphere. * `contMDiff.codRestrict_sphere` states that a map from a manifold into the sphere is `C^m` if its lift to a map to `E` is `C^m`; this is a useful tool for constructing `C^m` maps *to* the sphere. As an application we prove `contMDiffNegSphere`, that the antipodal map is analytic. Finally, we equip the `Circle` (defined in `Analysis.Complex.Circle` to be the sphere in `ℂ` centred at `0` of radius `1`) with the following structure: * a charted space with model space `EuclideanSpace ℝ (Fin 1)` (inherited from `Metric.Sphere`) * an analytic Lie group with model with corners `𝓡 1` We furthermore show that `Circle.exp` (defined in `Analysis.Complex.Circle` to be the natural map `fun t ↦ exp (t * I)` from `ℝ` to `Circle`) is analytic. ## Implementation notes The model space for the charted space instance is `EuclideanSpace ℝ (Fin n)`, where `n` is a natural number satisfying the typeclass assumption `[Fact (finrank ℝ E = n + 1)]`. This may seem a little awkward, but it is designed to circumvent the problem that the literal expression for the dimension of the model space (up to definitional equality) determines the type. If one used the naive expression `EuclideanSpace ℝ (Fin (finrank ℝ E - 1))` for the model space, then the sphere in `ℂ` would be a manifold with model space `EuclideanSpace ℝ (Fin (2 - 1))` but not with model space `EuclideanSpace ℝ (Fin 1)`. ## TODO Relate the stereographic projection to the inversion of the space. -/ variable {E : Type*} [NormedAddCommGroup E] [InnerProductSpace ℝ E] noncomputable section open Metric Module Function open scoped Manifold ContDiff RealInnerProductSpace section StereographicProjection variable (v : E) /-! ### Construction of the stereographic projection -/ /-- Stereographic projection, forward direction. This is a map from an inner product space `E` to the orthogonal complement of an element `v` of `E`. It is smooth away from the affine hyperplane through `v` parallel to the orthogonal complement. It restricts on the sphere to the stereographic projection. -/ def stereoToFun (x : E) : (ℝ ∙ v)ᗮ := (2 / ((1 : ℝ) - innerSL ℝ v x)) • (ℝ ∙ v)ᗮ.orthogonalProjection x variable {v} @[simp] theorem stereoToFun_apply (x : E) : stereoToFun v x = (2 / ((1 : ℝ) - innerSL ℝ v x)) • (ℝ ∙ v)ᗮ.orthogonalProjection x := rfl theorem contDiffOn_stereoToFun {n : WithTop ℕ∞} : ContDiffOn ℝ n (stereoToFun v) {x : E | innerSL _ v x ≠ (1 : ℝ)} := by refine ContDiffOn.smul ?_ (ℝ ∙ v)ᗮ.orthogonalProjection.contDiff.contDiffOn refine contDiff_const.contDiffOn.div ?_ ?_ · exact (contDiff_const.sub (innerSL ℝ v).contDiff).contDiffOn · intro x h h' exact h (sub_eq_zero.mp h').symm theorem continuousOn_stereoToFun : ContinuousOn (stereoToFun v) {x : E | innerSL _ v x ≠ (1 : ℝ)} := (contDiffOn_stereoToFun (n := 0)).continuousOn variable (v) in /-- Auxiliary function for the construction of the reverse direction of the stereographic projection. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to `E`; we will later prove that it takes values in the unit sphere. For most purposes, use `stereoInvFun`, not `stereoInvFunAux`. -/ def stereoInvFunAux (w : E) : E := (‖w‖ ^ 2 + 4)⁻¹ • ((4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v) @[simp] theorem stereoInvFunAux_apply (w : E) : stereoInvFunAux v w = (‖w‖ ^ 2 + 4)⁻¹ • ((4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v) := rfl theorem stereoInvFunAux_mem (hv : ‖v‖ = 1) {w : E} (hw : w ∈ (ℝ ∙ v)ᗮ) : stereoInvFunAux v w ∈ sphere (0 : E) 1 := by have h₁ : (0 : ℝ) < ‖w‖ ^ 2 + 4 := by positivity suffices ‖(4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v‖ = ‖w‖ ^ 2 + 4 by simp only [mem_sphere_zero_iff_norm, norm_smul, Real.norm_eq_abs, abs_inv, this, abs_of_pos h₁, stereoInvFunAux_apply, inv_mul_cancel₀ h₁.ne'] suffices ‖(4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v‖ ^ 2 = (‖w‖ ^ 2 + 4) ^ 2 by simpa only [sq_eq_sq_iff_abs_eq_abs, abs_norm, abs_of_pos h₁] using this rw [Submodule.mem_orthogonal_singleton_iff_inner_left] at hw simp [norm_add_sq_real, norm_smul, inner_smul_left, inner_smul_right, hw, mul_pow, Real.norm_eq_abs, hv] ring theorem hasFDerivAt_stereoInvFunAux (v : E) : HasFDerivAt (stereoInvFunAux v) (ContinuousLinearMap.id ℝ E) 0 := by have h₀ : HasFDerivAt (fun w : E => ‖w‖ ^ 2) (0 : StrongDual ℝ E) 0 := by convert (hasStrictFDerivAt_norm_sq (0 : E)).hasFDerivAt simp only [map_zero, smul_zero] have h₁ : HasFDerivAt (fun w : E => (‖w‖ ^ 2 + 4)⁻¹) (0 : StrongDual ℝ E) 0 := by convert (hasFDerivAt_inv _).comp _ (h₀.add (hasFDerivAt_const 4 0)) <;> simp have h₂ : HasFDerivAt (fun w => (4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v) ((4 : ℝ) • ContinuousLinearMap.id ℝ E) 0 := by convert ((hasFDerivAt_const (4 : ℝ) 0).smul (hasFDerivAt_id 0)).add ((h₀.sub (hasFDerivAt_const (4 : ℝ) 0)).smul (hasFDerivAt_const v 0)) using 1 ext w simp convert h₁.smul h₂ using 1 ext w simp theorem hasFDerivAt_stereoInvFunAux_comp_coe (v : E) : HasFDerivAt (stereoInvFunAux v ∘ ((↑) : (ℝ ∙ v)ᗮ → E)) (ℝ ∙ v)ᗮ.subtypeL 0 := by have : HasFDerivAt (stereoInvFunAux v) (ContinuousLinearMap.id ℝ E) ((ℝ ∙ v)ᗮ.subtypeL 0) := hasFDerivAt_stereoInvFunAux v refine this.comp (0 : (ℝ ∙ v)ᗮ) (by apply ContinuousLinearMap.hasFDerivAt) theorem contDiff_stereoInvFunAux {m : WithTop ℕ∞} : ContDiff ℝ m (stereoInvFunAux v) := by have h₀ : ContDiff ℝ ω fun w : E => ‖w‖ ^ 2 := contDiff_norm_sq ℝ have h₁ : ContDiff ℝ ω fun w : E => (‖w‖ ^ 2 + 4)⁻¹ := by refine (h₀.add contDiff_const).inv ?_ intro x nlinarith have h₂ : ContDiff ℝ ω fun w => (4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v := by refine (contDiff_const.smul contDiff_id).add ?_ exact (h₀.sub contDiff_const).smul contDiff_const exact (h₁.smul h₂).of_le le_top /-- Stereographic projection, reverse direction. This is a map from the orthogonal complement of a unit vector `v` in an inner product space `E` to the unit sphere in `E`. -/ def stereoInvFun (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : sphere (0 : E) 1 := ⟨stereoInvFunAux v (w : E), stereoInvFunAux_mem hv w.2⟩ @[simp] theorem stereoInvFun_apply (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : (stereoInvFun hv w : E) = (‖w‖ ^ 2 + 4)⁻¹ • ((4 : ℝ) • w + (‖w‖ ^ 2 - 4) • v) := rfl open scoped InnerProductSpace in theorem stereoInvFun_ne_north_pole (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : stereoInvFun hv w ≠ (⟨v, by simp [hv]⟩ : sphere (0 : E) 1) := by refine Subtype.coe_ne_coe.1 ?_ rw [← inner_lt_one_iff_real_of_norm_eq_one _ hv] · have hw : ⟪v, w⟫_ℝ = 0 := Submodule.mem_orthogonal_singleton_iff_inner_right.mp w.2 have hw' : (‖(w : E)‖ ^ 2 + 4)⁻¹ * (‖(w : E)‖ ^ 2 - 4) < 1 := by rw [inv_mul_lt_iff₀'] · linarith positivity simpa [real_inner_comm, inner_add_right, inner_smul_right, real_inner_self_eq_norm_mul_norm, hw, hv] using hw' · simpa using stereoInvFunAux_mem hv w.2 theorem continuous_stereoInvFun (hv : ‖v‖ = 1) : Continuous (stereoInvFun hv) := continuous_induced_rng.2 ((contDiff_stereoInvFunAux (m := 0)).continuous.comp continuous_subtype_val) open scoped InnerProductSpace in attribute [-simp] AddSubgroupClass.coe_norm Submodule.coe_norm in theorem stereo_left_inv (hv : ‖v‖ = 1) {x : sphere (0 : E) 1} (hx : (x : E) ≠ v) : stereoInvFun hv (stereoToFun v x) = x := by ext simp only [stereoToFun_apply, stereoInvFun_apply, smul_add] -- name two frequently-occurring quantities and write down their basic properties set a : ℝ := innerSL _ v x set y := (ℝ ∙ v)ᗮ.orthogonalProjection x have split : ↑x = a • v + ↑y := by rw [← ((ℝ ∙ v).starProjection_add_starProjection_orthogonal x), Submodule.starProjection_unit_singleton ℝ hv x] rfl have hvy : ⟪v, y⟫_ℝ = 0 := Submodule.mem_orthogonal_singleton_iff_inner_right.mp y.2 have pythag : 1 = a ^ 2 + ‖y‖ ^ 2 := by have hvy' : ⟪a • v, y⟫_ℝ = 0 := by simp only [inner_smul_left, hvy, mul_zero] convert norm_add_sq_eq_norm_sq_add_norm_sq_of_inner_eq_zero _ _ hvy' using 2 · simp [← split] · simp [norm_smul, hv, ← sq, sq_abs] · exact sq _ -- a fact which will be helpful for clearing denominators in the main calculation have ha : 0 < 1 - a := by have : a < 1 := (inner_lt_one_iff_real_of_norm_eq_one hv (by simp)).mpr hx.symm linarith rw [split, Submodule.coe_smul_of_tower] simp only [norm_smul, norm_div, RCLike.norm_ofNat, Real.norm_eq_abs, abs_of_nonneg ha.le] match_scalars · field_simp linear_combination 4 * pythag · field_simp linear_combination 4 * (a - 1) * pythag theorem stereo_right_inv (hv : ‖v‖ = 1) (w : (ℝ ∙ v)ᗮ) : stereoToFun v (stereoInvFun hv w) = w := by simp only [stereoToFun, stereoInvFun, stereoInvFunAux, smul_add, map_add, map_smul, innerSL_apply_apply, Submodule.orthogonalProjection_mem_subspace_eq_self] have h₁ : (ℝ ∙ v)ᗮ.orthogonalProjection v = 0 := Submodule.orthogonalProjection_orthogonalComplement_singleton_eq_zero v have h₂ : ⟪v, w⟫ = 0 := Submodule.mem_orthogonal_singleton_iff_inner_right.mp w.2 have h₃ : ⟪v, v⟫ = 1 := by simp [hv] rw [h₁, h₂, h₃] match_scalars simp [field] ring /-- Stereographic projection from the unit sphere in `E`, centred at a unit vector `v` in `E`; this is the version as an open partial homeomorphism. -/ def stereographic (hv : ‖v‖ = 1) : OpenPartialHomeomorph (sphere (0 : E) 1) (ℝ ∙ v)ᗮ where toFun := stereoToFun v ∘ (↑) invFun := stereoInvFun hv source := {⟨v, by simp [hv]⟩}ᶜ target := Set.univ map_source' := by simp map_target' {w} _ := fun h => (stereoInvFun_ne_north_pole hv w) (Set.eq_of_mem_singleton h) left_inv' x hx := stereo_left_inv hv fun h => hx (by rw [← h] at hv apply Subtype.ext dsimp exact h) right_inv' w _ := stereo_right_inv hv w open_source := isOpen_compl_singleton open_target := isOpen_univ continuousOn_toFun := continuousOn_stereoToFun.comp continuous_subtype_val.continuousOn fun w h => by dsimp exact h ∘ Subtype.ext ∘ Eq.symm ∘ (inner_eq_one_iff_of_norm_eq_one hv (by simp)).mp continuousOn_invFun := (continuous_stereoInvFun hv).continuousOn theorem stereographic_apply (hv : ‖v‖ = 1) (x : sphere (0 : E) 1) : stereographic hv x = (2 / ((1 : ℝ) - ⟪v, x⟫)) • (ℝ ∙ v)ᗮ.orthogonalProjection x := rfl @[simp] theorem stereographic_source (hv : ‖v‖ = 1) : (stereographic hv).source = {⟨v, by simp [hv]⟩}ᶜ := rfl @[simp] theorem stereographic_target (hv : ‖v‖ = 1) : (stereographic hv).target = Set.univ := rfl @[simp] theorem stereographic_apply_neg (v : sphere (0 : E) 1) : stereographic (norm_eq_of_mem_sphere v) (-v) = 0 := by simp [stereographic_apply, Submodule.orthogonalProjection_orthogonalComplement_singleton_eq_zero] @[simp] theorem stereographic_neg_apply (v : sphere (0 : E) 1) : stereographic (norm_eq_of_mem_sphere (-v)) v = 0 := by convert stereographic_apply_neg (-v) ext1 simp theorem surjective_stereographic (hv : ‖v‖ = 1) : Surjective (stereographic hv) := (stereographic hv).surjective_of_target_eq_univ rfl @[simp] theorem range_stereographic_symm (hv : ‖v‖ = 1) (hv' : v ∈ sphere 0 1 := by simpa) : Set.range (stereographic hv).symm = {⟨v, hv'⟩}ᶜ := by refine le_antisymm ?_ (stereographic hv).symm.target_subset_range rintro x ⟨y, rfl⟩ suffices y ∈ (stereographic hv).target from (fun _ ↦ (stereographic hv).map_target) y this simp lemma isOpenEmbedding_stereographic_symm (hv : ‖v‖ = 1) : Topology.IsOpenEmbedding (stereographic hv).symm := (stereographic hv).symm.to_isOpenEmbedding (by simp) end StereographicProjection section ChartedSpace /-! ### Charted space structure on the sphere In this section we construct a charted space structure on the unit sphere in a finite-dimensional real inner product space `E`; that is, we show that it is locally homeomorphic to the Euclidean space of dimension one less than `E`. The restriction to finite dimension is for convenience. The most natural `ChartedSpace` structure for the sphere uses the stereographic projection from the antipodes of a point as the canonical chart at this point. However, the codomain of the stereographic projection constructed in the previous section is `(ℝ ∙ v)ᗮ`, the orthogonal complement of the vector `v` in `E` which is the "north pole" of the projection, so a priori these charts all have different codomains. So it is necessary to prove that these codomains are all continuously linearly equivalent to a fixed normed space. This could be proved in general by a simple case of Gram-Schmidt orthogonalization, but in the finite-dimensional case it follows more easily by dimension-counting. -/ /-- Variant of the stereographic projection, for the sphere in an `n + 1`-dimensional inner product space `E`. This version has codomain the Euclidean space of dimension `n`, and is obtained by composing the original stereographic projection (`stereographic`) with an arbitrary linear isometry from `(ℝ ∙ v)ᗮ` to the Euclidean space. -/ def stereographic' (n : ℕ) [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : OpenPartialHomeomorph (sphere (0 : E) 1) (EuclideanSpace ℝ (Fin n)) := stereographic (norm_eq_of_mem_sphere v) ≫ₕ (OrthonormalBasis.fromOrthogonalSpanSingleton n (ne_zero_of_mem_unit_sphere v)).repr.toHomeomorph.toOpenPartialHomeomorph @[simp] theorem stereographic'_source {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : (stereographic' n v).source = {v}ᶜ := by simp [stereographic'] @[simp] theorem stereographic'_target {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : (stereographic' n v).target = Set.univ := by simp [stereographic'] /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is a charted space modelled on the Euclidean space of dimension `n`. -/ instance EuclideanSpace.instChartedSpaceSphere {n : ℕ} [Fact (finrank ℝ E = n + 1)] : ChartedSpace (EuclideanSpace ℝ (Fin n)) (sphere (0 : E) 1) where atlas := {f | ∃ v : sphere (0 : E) 1, f = stereographic' n v} chartAt v := stereographic' n (-v) mem_chart_source v := by simpa using ne_neg_of_mem_unit_sphere ℝ v chart_mem_atlas v := ⟨-v, rfl⟩ instance (n : ℕ) : ChartedSpace (EuclideanSpace ℝ (Fin n)) (sphere (0 : EuclideanSpace ℝ (Fin (n + 1))) 1) := have := Fact.mk (@finrank_euclideanSpace_fin ℝ _ (n + 1)) EuclideanSpace.instChartedSpaceSphere end ChartedSpace section ContMDiffManifold open scoped InnerProductSpace theorem sphere_ext_iff (u v : sphere (0 : E) 1) : u = v ↔ ⟪(u : E), v⟫_ℝ = 1 := by simp [Subtype.ext_iff, inner_eq_one_iff_of_norm_eq_one] theorem stereographic'_symm_apply {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) (x : EuclideanSpace ℝ (Fin n)) : ((stereographic' n v).symm x : E) = let U : (ℝ ∙ (v : E))ᗮ ≃ₗᵢ[ℝ] EuclideanSpace ℝ (Fin n) := (OrthonormalBasis.fromOrthogonalSpanSingleton n (ne_zero_of_mem_unit_sphere v)).repr (‖(U.symm x : E)‖ ^ 2 + 4)⁻¹ • (4 : ℝ) • (U.symm x : E) + (‖(U.symm x : E)‖ ^ 2 + 4)⁻¹ • (‖(U.symm x : E)‖ ^ 2 - 4) • v.val := by simp [stereographic, stereographic', ← Submodule.coe_norm] /-! ### Analytic manifold structure on the sphere -/ /-- The unit sphere in an `n + 1`-dimensional inner product space `E` is an analytic manifold, modelled on the Euclidean space of dimension `n`. -/ instance EuclideanSpace.instIsManifoldSphere {n : ℕ} [Fact (finrank ℝ E = n + 1)] : IsManifold (𝓡 n) ω (sphere (0 : E) 1) := isManifold_of_contDiffOn (𝓡 n) ω (sphere (0 : E) 1) (by rintro _ _ ⟨v, rfl⟩ ⟨v', rfl⟩ let U := (-- Removed type ascription, and this helped for some reason with timeout issues? OrthonormalBasis.fromOrthogonalSpanSingleton (𝕜 := ℝ) n (ne_zero_of_mem_unit_sphere v)).repr let U' := (-- Removed type ascription, and this helped for some reason with timeout issues? OrthonormalBasis.fromOrthogonalSpanSingleton (𝕜 := ℝ) n (ne_zero_of_mem_unit_sphere v')).repr have H₁ := U'.contDiff.comp_contDiffOn (contDiffOn_stereoToFun (n := ω)) -- Porting note: need to help with implicit variables again have H₂ := (contDiff_stereoInvFunAux (m := ω) (v := v.val)|>.comp (ℝ ∙ (v : E))ᗮ.subtypeL.contDiff).comp U.symm.contDiff convert H₁.comp_inter (H₂.contDiffOn : ContDiffOn ℝ ω _ Set.univ) using 1 -- -- squeezed from `ext, simp [sphere_ext_iff, stereographic'_symm_apply, real_inner_comm]` simp only [OpenPartialHomeomorph.trans_toPartialEquiv, OpenPartialHomeomorph.symm_toPartialEquiv, PartialEquiv.trans_source, PartialEquiv.symm_source, stereographic'_target, stereographic'_source] simp only [modelWithCornersSelf_coe, modelWithCornersSelf_coe_symm, Set.range_id, Set.inter_univ, Set.univ_inter, Set.compl_singleton_eq, Set.preimage_setOf_eq] simp only [id, comp_apply, Submodule.subtypeL_apply, OpenPartialHomeomorph.coe_coe_symm, innerSL_apply_apply, Ne, sphere_ext_iff, real_inner_comm (v' : E)] rfl) instance (n : ℕ) : IsManifold (𝓡 n) ω (sphere (0 : EuclideanSpace ℝ (Fin (n + 1))) 1) := haveI := Fact.mk (@finrank_euclideanSpace_fin ℝ _ (n + 1)) EuclideanSpace.instIsManifoldSphere /-- The inclusion map (i.e., `coe`) from the sphere in `E` to `E` is analytic. -/ theorem contMDiff_coe_sphere {m : WithTop ℕ∞} {n : ℕ} [Fact (finrank ℝ E = n + 1)] : ContMDiff (𝓡 n) 𝓘(ℝ, E) m ((↑) : sphere (0 : E) 1 → E) := by rw [contMDiff_iff] constructor · exact continuous_subtype_val · intro v _ let U : _ ≃ₗᵢ[ℝ] _ := (-- Again, partially removing type ascription... OrthonormalBasis.fromOrthogonalSpanSingleton n (ne_zero_of_mem_unit_sphere (-v))).repr exact ((contDiff_stereoInvFunAux.comp (ℝ ∙ (-v : E))ᗮ.subtypeL.contDiff).comp U.symm.contDiff).contDiffOn variable {m : WithTop ℕ∞} {F : Type*} [NormedAddCommGroup F] [NormedSpace ℝ F] variable {H : Type*} [TopologicalSpace H] {I : ModelWithCorners ℝ F H} variable {M : Type*} [TopologicalSpace M] [ChartedSpace H M] [IsManifold I m M] /-- If a `C^m` function `f : M → E`, where `M` is some manifold, takes values in the sphere, then it restricts to a `C^m` function from `M` to the sphere. -/ theorem ContMDiff.codRestrict_sphere {n : ℕ} [Fact (finrank ℝ E = n + 1)] {f : M → E} (hf : ContMDiff I 𝓘(ℝ, E) m f) (hf' : ∀ x, f x ∈ sphere (0 : E) 1) : ContMDiff I (𝓡 n) m (Set.codRestrict _ _ hf' : M → sphere (0 : E) 1) := by rw [contMDiff_iff_target] refine ⟨continuous_induced_rng.2 hf.continuous, ?_⟩ intro v let U : _ ≃ₗᵢ[ℝ] _ := (-- Again, partially removing type ascription... Weird that this helps! OrthonormalBasis.fromOrthogonalSpanSingleton n (ne_zero_of_mem_unit_sphere (-v))).repr have h : ContDiffOn ℝ ω _ Set.univ := U.contDiff.contDiffOn have H₁ := (h.comp_inter contDiffOn_stereoToFun).contMDiffOn have H₂ : ContMDiffOn _ _ _ _ Set.univ := hf.contMDiffOn convert (H₁.of_le le_top).comp' H₂ using 1 ext x have hfxv : f x = -↑v ↔ ⟪f x, -↑v⟫_ℝ = 1 := by have hfx : ‖f x‖ = 1 := by simpa using hf' x rw [inner_eq_one_iff_of_norm_eq_one hfx] exact norm_eq_of_mem_sphere (-v) simp [chartAt, ChartedSpace.chartAt, Subtype.ext_iff, hfxv, real_inner_comm] /-- The antipodal map is analytic. -/ theorem contMDiff_neg_sphere {m : WithTop ℕ∞} {n : ℕ} [Fact (finrank ℝ E = n + 1)] : ContMDiff (𝓡 n) (𝓡 n) m fun x : sphere (0 : E) 1 => -x := by -- this doesn't elaborate well in term mode apply ContMDiff.codRestrict_sphere apply contDiff_neg.contMDiff.comp _ exact contMDiff_coe_sphere private lemma stereographic'_neg {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : stereographic' n (-v) v = 0 := by dsimp [stereographic'] simp only [EmbeddingLike.map_eq_zero_iff] apply stereographic_neg_apply /-- Consider the differential of the inclusion of the sphere in `E` at the point `v` as a continuous linear map from `TangentSpace (𝓡 n) v` to `E`. The range of this map is the orthogonal complement of `v` in `E`. Note that there is an abuse here of the defeq between `E` and the tangent space to `E` at `(v:E`). In general this defeq is not canonical, but in this case (the tangent space of a vector space) it is canonical. -/ theorem range_mfderiv_coe_sphere {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : LinearMap.range (mfderiv (𝓡 n) 𝓘(ℝ, E) ((↑) : sphere (0 : E) 1 → E) v : TangentSpace (𝓡 n) v →L[ℝ] E) = (ℝ ∙ (v : E))ᗮ := by rw [((contMDiff_coe_sphere v).mdifferentiableAt le_top).mfderiv] dsimp [chartAt] simp only [fderivWithin_univ, mfld_simps] let U := (OrthonormalBasis.fromOrthogonalSpanSingleton (𝕜 := ℝ) n (ne_zero_of_mem_unit_sphere (-v))).repr -- Porting note: this `suffices` was a `change` suffices LinearMap.range (fderiv ℝ ((stereoInvFunAux (-v : E) ∘ (↑)) ∘ U.symm) 0) = (ℝ ∙ (v : E))ᗮ by convert this using 3 apply stereographic'_neg have : HasFDerivAt (stereoInvFunAux (-v : E) ∘ (Subtype.val : (ℝ ∙ (↑(-v) : E))ᗮ → E)) (ℝ ∙ (↑(-v) : E))ᗮ.subtypeL (U.symm 0) := by convert hasFDerivAt_stereoInvFunAux_comp_coe (-v : E) simp convert congrArg LinearMap.range (this.comp 0 U.symm.toContinuousLinearEquiv.hasFDerivAt).fderiv symm convert (U.symm : EuclideanSpace ℝ (Fin n) ≃ₗᵢ[ℝ] (ℝ ∙ (↑(-v) : E))ᗮ).range_comp (ℝ ∙ (↑(-v) : E))ᗮ.subtype using 1 simp only [Submodule.range_subtype, coe_neg_sphere] congr 1 -- we must show `Submodule.span ℝ {v} = Submodule.span ℝ {-v}` apply Submodule.span_eq_span · simp only [Set.singleton_subset_iff, SetLike.mem_coe] rw [← Submodule.neg_mem_iff] exact Submodule.mem_span_singleton_self (-v : E) · simp only [Set.singleton_subset_iff, SetLike.mem_coe] rw [Submodule.neg_mem_iff] exact Submodule.mem_span_singleton_self (v : E) /-- Consider the differential of the inclusion of the sphere in `E` at the point `v` as a continuous linear map from `TangentSpace (𝓡 n) v` to `E`. This map is injective. -/ theorem mfderiv_coe_sphere_injective {n : ℕ} [Fact (finrank ℝ E = n + 1)] (v : sphere (0 : E) 1) : Injective (mfderiv (𝓡 n) 𝓘(ℝ, E) ((↑) : sphere (0 : E) 1 → E) v) := by rw [((contMDiff_coe_sphere v).mdifferentiableAt le_top).mfderiv] simp only [chartAt, fderivWithin_univ, mfld_simps] let U := (OrthonormalBasis.fromOrthogonalSpanSingleton (𝕜 := ℝ) n (ne_zero_of_mem_unit_sphere (-v))).repr suffices Injective (fderiv ℝ ((stereoInvFunAux (-v : E) ∘ (↑)) ∘ U.symm) 0) by convert this using 3 apply stereographic'_neg have : HasFDerivAt (stereoInvFunAux (-v : E) ∘ (Subtype.val : (ℝ ∙ (↑(-v) : E))ᗮ → E)) (ℝ ∙ (↑(-v) : E))ᗮ.subtypeL (U.symm 0) := by convert hasFDerivAt_stereoInvFunAux_comp_coe (-v : E) simp have := congr_arg DFunLike.coe <| (this.comp 0 U.symm.toContinuousLinearEquiv.hasFDerivAt).fderiv refine Eq.subst this.symm ?_ rw [ContinuousLinearMap.coe_comp', ContinuousLinearEquiv.coe_coe] simpa [- Subtype.val_injective] using Subtype.val_injective end ContMDiffManifold section Circle open Complex /-- Local instance to help the typeclass system to figure out `1 + 1 = 2`. -/ theorem finrank_real_complex_fact' : Fact (finrank ℝ ℂ = 1 + 1) := finrank_real_complex_fact attribute [local instance] finrank_real_complex_fact' /-- The unit circle in `ℂ` is a charted space modelled on `EuclideanSpace ℝ (Fin 1)`. This follows by definition from the corresponding result for `Metric.Sphere`. -/ instance : ChartedSpace (EuclideanSpace ℝ (Fin 1)) Circle := EuclideanSpace.instChartedSpaceSphere instance : IsManifold (𝓡 1) ω Circle := EuclideanSpace.instIsManifoldSphere (E := ℂ) /-- The unit circle in `ℂ` is an analytic Lie group. -/ instance : LieGroup (𝓡 1) ω Circle where contMDiff_mul := by apply ContMDiff.codRestrict_sphere let c : Circle → ℂ := (↑) have h₂ : ContMDiff (𝓘(ℝ, ℂ).prod 𝓘(ℝ, ℂ)) 𝓘(ℝ, ℂ) ω fun z : ℂ × ℂ => z.fst * z.snd := by rw [contMDiff_iff] exact ⟨continuous_mul, fun x y => contDiff_mul.contDiffOn⟩ suffices h₁ : ContMDiff _ _ _ (Prod.map c c) from h₂.comp h₁ apply ContMDiff.prodMap <;> exact contMDiff_coe_sphere contMDiff_inv := by apply ContMDiff.codRestrict_sphere simp only [← Circle.coe_inv, Circle.coe_inv_eq_conj] exact Complex.conjCLE.contDiff.contMDiff.comp contMDiff_coe_sphere /-- The map `fun t ↦ exp (t * I)` from `ℝ` to the unit circle in `ℂ` is analytic. -/ theorem contMDiff_circleExp {m : WithTop ℕ∞} : ContMDiff 𝓘(ℝ, ℝ) (𝓡 1) m Circle.exp := (contDiff_exp.comp (contDiff_id.smul contDiff_const)).contMDiff.codRestrict_sphere _ end Circle
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Instances/UnitsOfNormedAlgebra.lean
import Mathlib.Geometry.Manifold.Algebra.LieGroup /-! # Units of a normed algebra We construct the Lie group structure on the group of units of a complete normed `𝕜`-algebra `R`. The group of units `Rˣ` has a natural `C^n` manifold structure modelled on `R` given by its embedding into `R`. Together with the smoothness of the multiplication and inverse of its elements, `Rˣ` forms a Lie group. An important special case of this construction is the general linear group. For a normed space `V` over a field `𝕜`, the `𝕜`-linear endomorphisms of `V` are a normed `𝕜`-algebra (see `ContinuousLinearMap.toNormedAlgebra`), so this construction provides a Lie group structure on its group of units, the general linear group GL(`𝕜`, `V`), as demonstrated by: ``` example {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] [CompleteSpace V] : LieGroup 𝓘(𝕜, V →L[𝕜] V) (V →L[𝕜] V)ˣ := inferInstance ``` -/ noncomputable section open scoped Manifold ContDiff namespace Units variable {R : Type*} [NormedRing R] [CompleteSpace R] {n : WithTop ℕ∞} instance : ChartedSpace R Rˣ := isOpenEmbedding_val.singletonChartedSpace theorem chartAt_apply {a : Rˣ} {b : Rˣ} : chartAt R a b = b := rfl theorem chartAt_source {a : Rˣ} : (chartAt R a).source = Set.univ := rfl variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] [NormedAlgebra 𝕜 R] instance : IsManifold 𝓘(𝕜, R) n Rˣ := isOpenEmbedding_val.isManifold_singleton /-- For a complete normed ring `R`, the embedding of the units `Rˣ` into `R` is a `C^n` map between manifolds. -/ lemma contMDiff_val : ContMDiff 𝓘(𝕜, R) 𝓘(𝕜, R) n (val : Rˣ → R) := contMDiff_isOpenEmbedding Units.isOpenEmbedding_val /-- The units of a complete normed ring form a Lie group. -/ instance : LieGroup 𝓘(𝕜, R) n Rˣ where contMDiff_mul := by apply ContMDiff.of_comp_isOpenEmbedding Units.isOpenEmbedding_val have : (val : Rˣ → R) ∘ (fun x : Rˣ × Rˣ => x.1 * x.2) = (fun x : R × R => x.1 * x.2) ∘ (fun x : Rˣ × Rˣ => (x.1, x.2)) := by ext; simp rw [this] have : ContMDiff (𝓘(𝕜, R).prod 𝓘(𝕜, R)) 𝓘(𝕜, R × R) n (fun x : Rˣ × Rˣ => ((x.1 : R), (x.2 : R))) := (contMDiff_val.comp contMDiff_fst).prodMk_space (contMDiff_val.comp contMDiff_snd) refine ContMDiff.comp ?_ this rw [contMDiff_iff_contDiff] exact contDiff_mul contMDiff_inv := by apply ContMDiff.of_comp_isOpenEmbedding Units.isOpenEmbedding_val have : (val : Rˣ → R) ∘ (fun x : Rˣ => x⁻¹) = Ring.inverse ∘ val := by ext; simp rw [this, ContMDiff] refine fun x => ContMDiffAt.comp x ?_ (contMDiff_val x) rw [contMDiffAt_iff_contDiffAt] exact contDiffAt_ringInverse _ _ end Units
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Instances/Real.lean
import Mathlib.Analysis.Calculus.ContDiff.WithLp import Mathlib.Analysis.InnerProductSpace.PiL2 import Mathlib.Geometry.Manifold.IsManifold.InteriorBoundary /-! # Constructing examples of manifolds over ℝ We introduce the necessary bits to be able to define manifolds modelled over `ℝ^n`, boundaryless or with boundary or with corners. As a concrete example, we construct explicitly the manifold with boundary structure on the real interval `[x, y]`, and prove that its boundary is indeed `{x,y}` whenever `x < y`. As a corollary, a product `M × [x, y]` with a manifold `M` without boundary has boundary `M × {x, y}`. More specifically, we introduce * `modelWithCornersEuclideanHalfSpace n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanHalfSpace n)` for the model space used to define `n`-dimensional real manifolds with boundary * `modelWithCornersEuclideanQuadrant n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanQuadrant n)` for the model space used to define `n`-dimensional real manifolds with corners ## Notation In the scope `Manifold`, we introduce the notations * `𝓡 n` for the identity model with corners on `EuclideanSpace ℝ (Fin n)` * `𝓡∂ n` for `modelWithCornersEuclideanHalfSpace n`. For instance, if a manifold `M` is boundaryless, smooth and modelled on `EuclideanSpace ℝ (Fin m)`, and `N` is smooth with boundary modelled on `EuclideanHalfSpace n`, and `f : M → N` is a smooth map, then the derivative of `f` can be written simply as `mfderiv (𝓡 m) (𝓡∂ n) f` (as to why the model with corners cannot be implicit, see the discussion in `Geometry.Manifold.IsManifold`). ## Implementation notes The manifold structure on the interval `[x, y] = Icc x y` requires the assumption `x < y` as a typeclass. We provide it as `[Fact (x < y)]`. -/ noncomputable section open Set Function WithLp open scoped Manifold ContDiff ENNReal /-- The half-space in `ℝ^n`, used to model manifolds with boundary. We only define it when `1 ≤ n`, as the definition only makes sense in this case. -/ def EuclideanHalfSpace (n : ℕ) [NeZero n] : Type := { x : EuclideanSpace ℝ (Fin n) // 0 ≤ x 0 } /-- The quadrant in `ℝ^n`, used to model manifolds with corners, made of all vectors with nonnegative coordinates. -/ def EuclideanQuadrant (n : ℕ) : Type := { x : EuclideanSpace ℝ (Fin n) // ∀ i : Fin n, 0 ≤ x i } section /- Register class instances for Euclidean half-space and quadrant, that cannot be noticed without the following reducibility attribute (which is only set in this section). -/ variable {n : ℕ} instance [NeZero n] : TopologicalSpace (EuclideanHalfSpace n) := instTopologicalSpaceSubtype instance : TopologicalSpace (EuclideanQuadrant n) := instTopologicalSpaceSubtype instance {n : ℕ} [NeZero n] : Zero (EuclideanHalfSpace n) := ⟨⟨0, by simp⟩⟩ instance {n : ℕ} : Zero (EuclideanQuadrant n) := ⟨⟨0, by simp⟩⟩ instance [NeZero n] : Inhabited (EuclideanHalfSpace n) := ⟨0⟩ instance : Inhabited (EuclideanQuadrant n) := ⟨0⟩ @[ext] theorem EuclideanQuadrant.ext (x y : EuclideanQuadrant n) (h : x.1 = y.1) : x = y := Subtype.eq h @[ext] theorem EuclideanHalfSpace.ext [NeZero n] (x y : EuclideanHalfSpace n) (h : x.1 = y.1) : x = y := Subtype.eq h theorem EuclideanHalfSpace.convex [NeZero n] : Convex ℝ { x : EuclideanSpace ℝ (Fin n) | 0 ≤ x 0 } := fun _ hx _ hy _ _ _ _ _ ↦ by dsimp at hx hy ⊢; positivity theorem EuclideanQuadrant.convex : Convex ℝ { x : EuclideanSpace ℝ (Fin n) | ∀ i, 0 ≤ x i } := fun _ hx _ hy _ _ _ _ _ i ↦ by dsimp at hx hy ⊢; specialize hx i; specialize hy i; positivity instance EuclideanHalfSpace.pathConnectedSpace [NeZero n] : PathConnectedSpace (EuclideanHalfSpace n) := isPathConnected_iff_pathConnectedSpace.mp <| convex.isPathConnected ⟨0, by simp⟩ instance EuclideanQuadrant.pathConnectedSpace : PathConnectedSpace (EuclideanQuadrant n) := isPathConnected_iff_pathConnectedSpace.mp <| convex.isPathConnected ⟨0, by simp⟩ instance [NeZero n] : LocPathConnectedSpace (EuclideanHalfSpace n) := EuclideanHalfSpace.convex.locPathConnectedSpace instance : LocPathConnectedSpace (EuclideanQuadrant n) := EuclideanQuadrant.convex.locPathConnectedSpace theorem range_euclideanHalfSpace (n : ℕ) [NeZero n] : range (Subtype.val : EuclideanHalfSpace n → _) = { y | 0 ≤ y 0 } := Subtype.range_val @[simp] theorem interior_halfSpace {n : ℕ} (p : ℝ≥0∞) (a : ℝ) (i : Fin n) : interior { y : PiLp p (fun _ : Fin n ↦ ℝ) | a ≤ y i } = { y | a < y i } := by let f : PiLp p (fun _ : Fin n ↦ ℝ) → ℝ := fun x ↦ x i change interior (f ⁻¹' Ici a) = f ⁻¹' Ioi a rw [← (PiLp.isOpenMap_apply p _ i).preimage_interior_eq_interior_preimage, interior_Ici] fun_prop @[simp] theorem closure_halfSpace {n : ℕ} (p : ℝ≥0∞) (a : ℝ) (i : Fin n) : closure { y : PiLp p (fun _ : Fin n ↦ ℝ) | a ≤ y i } = { y | a ≤ y i } := by let f : PiLp p (fun _ : Fin n ↦ ℝ) → ℝ := fun x ↦ x i change closure (f ⁻¹' Ici a) = f ⁻¹' Ici a rw [← (PiLp.isOpenMap_apply p _ i).preimage_closure_eq_closure_preimage, closure_Ici] fun_prop @[simp] theorem closure_open_halfSpace {n : ℕ} (p : ℝ≥0∞) (a : ℝ) (i : Fin n) : closure { y : PiLp p (fun _ : Fin n ↦ ℝ) | a < y i } = { y | a ≤ y i } := by let f : PiLp p (fun _ : Fin n ↦ ℝ) → ℝ := fun x ↦ x i change closure (f ⁻¹' Ioi a) = f ⁻¹' Ici a rw [← (PiLp.isOpenMap_apply p _ i).preimage_closure_eq_closure_preimage, closure_Ioi] fun_prop @[simp] theorem frontier_halfSpace {n : ℕ} (p : ℝ≥0∞) (a : ℝ) (i : Fin n) : frontier { y : PiLp p (fun _ : Fin n ↦ ℝ) | a ≤ y i } = { y | a = y i } := by rw [frontier, closure_halfSpace, interior_halfSpace] ext y simpa only [mem_diff, mem_setOf_eq, not_lt] using antisymm_iff theorem range_euclideanQuadrant (n : ℕ) : range (Subtype.val : EuclideanQuadrant n → _) = { y | ∀ i : Fin n, 0 ≤ y i } := Subtype.range_val theorem interior_euclideanQuadrant (n : ℕ) (p : ℝ≥0∞) (a : ℝ) : interior { y : PiLp p (fun _ : Fin n ↦ ℝ) | ∀ i : Fin n, a ≤ y i } = { y | ∀ i : Fin n, a < y i } := by let f i : PiLp p (fun _ : Fin n ↦ ℝ) → ℝ := fun x ↦ x i have h : { y : PiLp p (fun _ : Fin n ↦ ℝ) | ∀ i : Fin n, a ≤ y i } = ⋂ i, (f i) ⁻¹' Ici a := by ext; simp; rfl have h' : { y : PiLp p (fun _ : Fin n ↦ ℝ) | ∀ i : Fin n, a < y i } = ⋂ i, (f i )⁻¹' Ioi a := by ext; simp; rfl rw [h, h', interior_iInter_of_finite] apply iInter_congr fun i ↦ ?_ rw [← (PiLp.isOpenMap_apply p _ i).preimage_interior_eq_interior_preimage, interior_Ici] fun_prop end /-- Definition of the model with corners `(EuclideanSpace ℝ (Fin n), EuclideanHalfSpace n)`, used as a model for manifolds with boundary. In the scope `Manifold`, use the shortcut `𝓡∂ n`. -/ def modelWithCornersEuclideanHalfSpace (n : ℕ) [NeZero n] : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanHalfSpace n) where toFun := Subtype.val invFun x := ⟨toLp 2 (update x 0 (max (x 0) 0)), by simp⟩ source := univ target := { x | 0 ≤ x 0 } map_source' x _ := x.property map_target' _ _ := mem_univ _ left_inv' := fun ⟨xval, xprop⟩ _ => by rw [Subtype.mk_eq_mk, ← WithLp.equiv_symm_apply, Equiv.symm_apply_eq, update_eq_iff] exact ⟨max_eq_left xprop, fun i _ => rfl⟩ right_inv' _ hx := by rw [Subtype.coe_mk, ← WithLp.equiv_symm_apply, Equiv.symm_apply_eq, update_eq_iff] exact ⟨max_eq_left hx, fun _ _ => rfl⟩ source_eq := rfl convex_range' := by simp only [instIsRCLikeNormedField, ↓reduceDIte] apply Convex.convex_isRCLikeNormedField rw [range_euclideanHalfSpace n] exact EuclideanHalfSpace.convex (n := n) nonempty_interior' := by rw [range_euclideanHalfSpace, interior_halfSpace] exact ⟨toLp 2 fun i ↦ 1, by simp⟩ continuous_toFun := continuous_subtype_val continuous_invFun := by exact ((PiLp.continuous_toLp 2 _).comp <| (PiLp.continuous_ofLp 2 _).update 0 <| (PiLp.continuous_apply 2 _ 0).max continuous_const).subtype_mk _ /-- Definition of the model with corners `(EuclideanSpace ℝ (Fin n), EuclideanQuadrant n)`, used as a model for manifolds with corners -/ def modelWithCornersEuclideanQuadrant (n : ℕ) : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanQuadrant n) where toFun := Subtype.val invFun x := ⟨toLp 2 fun i ↦ max (x i) 0, fun i ↦ by simp only [le_sup_right]⟩ source := univ target := { x | ∀ i, 0 ≤ x i } map_source' x _ := x.property map_target' _ _ := mem_univ _ left_inv' x _ := by ext i; simp only [x.2 i, sup_of_le_left] right_inv' x hx := by ext1 i; simp only [hx i, sup_of_le_left] source_eq := rfl convex_range' := by simp only [instIsRCLikeNormedField, ↓reduceDIte] apply Convex.convex_isRCLikeNormedField rw [range_euclideanQuadrant] exact EuclideanQuadrant.convex nonempty_interior' := by rw [range_euclideanQuadrant, interior_euclideanQuadrant] exact ⟨toLp 2 fun i ↦ 1, by simp⟩ continuous_toFun := continuous_subtype_val continuous_invFun := Continuous.subtype_mk ((PiLp.continuous_toLp 2 _).comp <| (continuous_pi fun i ↦ ((PiLp.continuous_apply 2 _ i).max continuous_const))) _ /-- The model space used to define `n`-dimensional real manifolds without boundary. -/ scoped[Manifold] notation3 "𝓡 " n => (modelWithCornersSelf ℝ (EuclideanSpace ℝ (Fin n)) : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanSpace ℝ (Fin n))) /-- The model space used to define `n`-dimensional real manifolds with boundary. -/ scoped[Manifold] notation3 "𝓡∂ " n => (modelWithCornersEuclideanHalfSpace n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanHalfSpace n)) lemma modelWithCornersEuclideanHalfSpace_zero {n : ℕ} [NeZero n] : (𝓡∂ n) 0 = 0 := rfl lemma range_modelWithCornersEuclideanHalfSpace (n : ℕ) [NeZero n] : range (𝓡∂ n) = { y | 0 ≤ y 0 } := range_euclideanHalfSpace n lemma interior_range_modelWithCornersEuclideanHalfSpace (n : ℕ) [NeZero n] : interior (range (𝓡∂ n)) = { y | 0 < y 0 } := by calc interior (range (𝓡∂ n)) _ = interior ({ y | 0 ≤ y 0}) := by congr! apply range_euclideanHalfSpace _ = { y | 0 < y 0 } := interior_halfSpace _ _ _ lemma frontier_range_modelWithCornersEuclideanHalfSpace (n : ℕ) [NeZero n] : frontier (range (𝓡∂ n)) = { y | 0 = y 0 } := by calc frontier (range (𝓡∂ n)) _ = frontier ({ y | 0 ≤ y 0 }) := by congr! apply range_euclideanHalfSpace _ = { y | 0 = y 0 } := frontier_halfSpace 2 _ _ /-- The left chart for the topological space `[x, y]`, defined on `[x,y)` and sending `x` to `0` in `EuclideanHalfSpace 1`. -/ def IccLeftChart (x y : ℝ) [h : Fact (x < y)] : OpenPartialHomeomorph (Icc x y) (EuclideanHalfSpace 1) where source := { z : Icc x y | z.val < y } target := { z : EuclideanHalfSpace 1 | z.val 0 < y - x } toFun := fun z : Icc x y => ⟨toLp 2 fun _ ↦ z.val - x, sub_nonneg.mpr z.property.1⟩ invFun z := ⟨min (z.val 0 + x) y, by simp [z.prop, h.out.le]⟩ map_source' := by simp only [mem_setOf_eq, Fin.isValue, sub_lt_sub_iff_right, imp_self, implies_true] map_target' := by simp only [min_lt_iff, mem_setOf_eq]; intro z hz; left linarith left_inv' := by rintro ⟨z, hz⟩ h'z simp only [mem_setOf_eq, mem_Icc] at hz h'z simp only [Fin.isValue, sub_add_cancel, hz, inf_of_le_left] right_inv' := by rintro ⟨z, hz⟩ h'z rw [Subtype.mk_eq_mk] ext i dsimp at hz h'z have A : x + z 0 ≤ y := by linarith rw [Subsingleton.elim i 0] simp only [Fin.isValue, add_comm, A, inf_of_le_left, add_sub_cancel_left] open_source := haveI : IsOpen { z : ℝ | z < y } := isOpen_Iio this.preimage continuous_subtype_val open_target := by have : IsOpen { z : ℝ | z < y - x } := isOpen_Iio have : IsOpen { z : EuclideanSpace ℝ (Fin 1) | z 0 < y - x } := this.preimage (@PiLp.continuous_apply 2 (Fin 1) (fun _ => ℝ) _ 0) exact this.preimage continuous_subtype_val continuousOn_toFun := by apply Continuous.continuousOn apply Continuous.subtype_mk have : Continuous fun (z : ℝ) (_ : Fin 1) => z - x := Continuous.sub (continuous_pi fun _ => continuous_id) continuous_const exact (PiLp.continuous_toLp 2 _).comp <| this.comp continuous_subtype_val continuousOn_invFun := by apply Continuous.continuousOn apply Continuous.subtype_mk have A : Continuous fun z : ℝ => min (z + x) y := (continuous_id.add continuous_const).min continuous_const have B : Continuous fun z : EuclideanSpace ℝ (Fin 1) ↦ z 0 := PiLp.continuous_apply 2 _ 0 exact (A.comp B).comp continuous_subtype_val variable {x y : ℝ} [hxy : Fact (x < y)] namespace Fact.Manifold scoped instance : Fact (x ≤ y) := Fact.mk hxy.out.le end Fact.Manifold open Fact.Manifold lemma IccLeftChart_extend_bot : (IccLeftChart x y).extend (𝓡∂ 1) ⊥ = 0 := by norm_num [IccLeftChart, modelWithCornersEuclideanHalfSpace_zero] congr lemma iccLeftChart_extend_zero {p : Set.Icc x y} : (IccLeftChart x y).extend (𝓡∂ 1) p 0 = p.val - x := rfl lemma IccLeftChart_extend_interior_pos {p : Set.Icc x y} (hp : x < p.val ∧ p.val < y) : 0 < (IccLeftChart x y).extend (𝓡∂ 1) p 0 := by simp_rw [iccLeftChart_extend_zero] norm_num [hp.1] lemma IccLeftChart_extend_bot_mem_frontier : (IccLeftChart x y).extend (𝓡∂ 1) ⊥ ∈ frontier (range (𝓡∂ 1)) := by rw [IccLeftChart_extend_bot, frontier_range_modelWithCornersEuclideanHalfSpace, mem_setOf, PiLp.zero_apply] /-- The right chart for the topological space `[x, y]`, defined on `(x,y]` and sending `y` to `0` in `EuclideanHalfSpace 1`. -/ def IccRightChart (x y : ℝ) [h : Fact (x < y)] : OpenPartialHomeomorph (Icc x y) (EuclideanHalfSpace 1) where source := { z : Icc x y | x < z.val } target := { z : EuclideanHalfSpace 1 | z.val 0 < y - x } toFun z := ⟨toLp 2 fun _ ↦ y - z.val, sub_nonneg.mpr z.property.2⟩ invFun z := ⟨max (y - z.val 0) x, by simp [z.prop, h.out.le, sub_eq_add_neg]⟩ map_source' := by simp only [mem_setOf_eq, Fin.isValue, sub_lt_sub_iff_left, imp_self, implies_true] map_target' := by simp only [lt_max_iff, mem_setOf_eq]; intro z hz; left linarith left_inv' := by rintro ⟨z, hz⟩ h'z simp only [mem_setOf_eq, mem_Icc] at hz h'z simp only [Fin.isValue, sub_eq_add_neg, neg_add_rev, neg_neg, add_neg_cancel_comm_assoc, hz, sup_of_le_left] right_inv' := by rintro ⟨z, hz⟩ h'z rw [Subtype.mk_eq_mk] ext i dsimp at hz h'z have A : x ≤ y - z 0 := by linarith rw [Subsingleton.elim i 0] simp only [Fin.isValue, A, sup_of_le_left, sub_sub_cancel] open_source := haveI : IsOpen { z : ℝ | x < z } := isOpen_Ioi this.preimage continuous_subtype_val open_target := by have : IsOpen { z : ℝ | z < y - x } := isOpen_Iio have : IsOpen { z : EuclideanSpace ℝ (Fin 1) | z 0 < y - x } := this.preimage (@PiLp.continuous_apply 2 (Fin 1) (fun _ ↦ ℝ) _ 0) exact this.preimage continuous_subtype_val continuousOn_toFun := by apply Continuous.continuousOn apply Continuous.subtype_mk have : Continuous fun (z : ℝ) (_ : Fin 1) => y - z := continuous_const.sub (continuous_pi fun _ => continuous_id) exact (PiLp.continuous_toLp 2 _).comp <| this.comp continuous_subtype_val continuousOn_invFun := by apply Continuous.continuousOn apply Continuous.subtype_mk have A : Continuous fun z : ℝ => max (y - z) x := (continuous_const.sub continuous_id).max continuous_const have B : Continuous fun z : EuclideanSpace ℝ (Fin 1) => z 0 := PiLp.continuous_apply 2 _ 0 exact (A.comp B).comp continuous_subtype_val lemma IccRightChart_extend_top : (IccRightChart x y).extend (𝓡∂ 1) ⊤ = 0 := by norm_num [IccRightChart, modelWithCornersEuclideanHalfSpace_zero] congr lemma IccRightChart_extend_top_mem_frontier : (IccRightChart x y).extend (𝓡∂ 1) ⊤ ∈ frontier (range (𝓡∂ 1)) := by rw [IccRightChart_extend_top, frontier_range_modelWithCornersEuclideanHalfSpace, mem_setOf, PiLp.zero_apply] /-- Charted space structure on `[x, y]`, using only two charts taking values in `EuclideanHalfSpace 1`. -/ instance instIccChartedSpace (x y : ℝ) [h : Fact (x < y)] : ChartedSpace (EuclideanHalfSpace 1) (Icc x y) where atlas := {IccLeftChart x y, IccRightChart x y} chartAt z := if z.val < y then IccLeftChart x y else IccRightChart x y mem_chart_source z := by by_cases h' : z.val < y · simp only [h', if_true] exact h' · simp only [h', if_false] apply lt_of_lt_of_le h.out simpa only [not_lt] using h' chart_mem_atlas z := by by_cases h' : (z : ℝ) < y <;> simp [h'] @[simp] lemma Icc_chartedSpaceChartAt {z : Set.Icc x y} : chartAt _ z = if z.val < y then IccLeftChart x y else IccRightChart x y := rfl lemma Icc_chartedSpaceChartAt_of_le_top {z : Set.Icc x y} (h : z.val < y) : chartAt _ z = IccLeftChart x y := by simp [Icc_chartedSpaceChartAt, h] lemma Icc_chartedSpaceChartAt_of_top_le {z : Set.Icc x y} (h : y ≤ z.val) : chartAt _ z = IccRightChart x y := by simp [Icc_chartedSpaceChartAt, reduceIte, not_lt.mpr h] lemma Icc_isBoundaryPoint_bot : (𝓡∂ 1).IsBoundaryPoint (⊥ : Set.Icc x y) := by rw [ModelWithCorners.isBoundaryPoint_iff, extChartAt, Icc_chartedSpaceChartAt_of_le_top (by simp [hxy.out])] exact IccLeftChart_extend_bot_mem_frontier lemma Icc_isBoundaryPoint_top : (𝓡∂ 1).IsBoundaryPoint (⊤ : Set.Icc x y) := by rw [ModelWithCorners.isBoundaryPoint_iff, extChartAt, Icc_chartedSpaceChartAt_of_top_le (by simp)] exact IccRightChart_extend_top_mem_frontier lemma Icc_isInteriorPoint_interior {p : Set.Icc x y} (hp : x < p.val ∧ p.val < y) : (𝓡∂ 1).IsInteriorPoint p := by rw [ModelWithCorners.IsInteriorPoint, extChartAt, Icc_chartedSpaceChartAt_of_le_top hp.2, interior_range_modelWithCornersEuclideanHalfSpace] exact IccLeftChart_extend_interior_pos hp lemma boundary_Icc : (𝓡∂ 1).boundary (Icc x y) = {⊥, ⊤} := by ext p rcases Set.eq_endpoints_or_mem_Ioo_of_mem_Icc p.2 with (hp | hp | hp) · have : p = ⊥ := SetCoe.ext hp rw [this] apply iff_of_true Icc_isBoundaryPoint_bot (mem_insert ⊥ {⊤}) · have : p = ⊤ := SetCoe.ext hp rw [this] apply iff_of_true Icc_isBoundaryPoint_top (mem_insert_of_mem ⊥ rfl) · apply iff_of_false · simpa [← mem_compl_iff, ModelWithCorners.compl_boundary] using Icc_isInteriorPoint_interior hp · rw [mem_insert_iff, mem_singleton_iff] push_neg constructor <;> by_contra h <;> rw [congrArg Subtype.val h] at hp exacts [left_mem_Ioo.mp hp, right_mem_Ioo.mp hp] variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners ℝ E H) {M : Type*} [TopologicalSpace M] [ChartedSpace H M] /-- A product `M × [x,y]` for `M` boundaryless has boundary `M × {x, y}`. -/ lemma boundary_product [I.Boundaryless] : (I.prod (𝓡∂ 1)).boundary (M × Icc x y) = Set.prod univ {⊥, ⊤} := by rw [I.boundary_of_boundaryless_left, boundary_Icc] /-- The manifold structure on `[x, y]` is smooth. -/ instance instIsManifoldIcc (x y : ℝ) [Fact (x < y)] {n : WithTop ℕ∞} : IsManifold (𝓡∂ 1) n (Icc x y) := by have M : ContDiff ℝ n (show EuclideanSpace ℝ (Fin 1) → EuclideanSpace ℝ (Fin 1) from fun z ↦ toLp 2 fun i ↦ -z i + (y - x)) := PiLp.contDiff_toLp.comp <| PiLp.contDiff_ofLp.neg.add contDiff_const apply isManifold_of_contDiffOn intro e e' he he' simp only [atlas] at he he' /- We need to check that any composition of two charts gives a `C^∞` function. Each chart can be either the left chart or the right chart, leaving 4 possibilities that we handle successively. -/ rcases he with (rfl | rfl) <;> rcases he' with (rfl | rfl) · -- `e = left chart`, `e' = left chart` exact (mem_groupoid_of_pregroupoid.mpr (symm_trans_mem_contDiffGroupoid _)).1 · -- `e = left chart`, `e' = right chart` apply M.contDiffOn.congr rintro _ ⟨⟨hz₁, hz₂⟩, ⟨⟨z, hz₀⟩, rfl⟩⟩ simp only [modelWithCornersEuclideanHalfSpace, IccLeftChart, IccRightChart, update_self, max_eq_left, hz₀, lt_sub_iff_add_lt, mfld_simps] at hz₁ hz₂ rw [min_eq_left hz₁.le, lt_add_iff_pos_left] at hz₂ ext i rw [Subsingleton.elim i 0] simp only [modelWithCornersEuclideanHalfSpace, IccLeftChart, IccRightChart, *, max_eq_left, min_eq_left hz₁.le, update_self, mfld_simps] abel · -- `e = right chart`, `e' = left chart` apply M.contDiffOn.congr rintro _ ⟨⟨hz₁, hz₂⟩, ⟨z, hz₀⟩, rfl⟩ simp only [modelWithCornersEuclideanHalfSpace, IccLeftChart, IccRightChart, max_lt_iff, update_self, max_eq_left hz₀, mfld_simps] at hz₁ hz₂ rw [lt_sub_comm] at hz₁ ext i rw [Subsingleton.elim i 0] simp only [modelWithCornersEuclideanHalfSpace, IccLeftChart, IccRightChart, update_self, max_eq_left, hz₀, hz₁.le, mfld_simps] abel ·-- `e = right chart`, `e' = right chart` exact (mem_groupoid_of_pregroupoid.mpr (symm_trans_mem_contDiffGroupoid _)).1 /-! Register the manifold structure on `Icc 0 1`. These are merely special cases of `instIccChartedSpace` and `instIsManifoldIcc`. -/ section instance : ChartedSpace (EuclideanHalfSpace 1) (Icc (0 : ℝ) 1) := by infer_instance instance {n : WithTop ℕ∞} : IsManifold (𝓡∂ 1) n (Icc (0 : ℝ) 1) := by infer_instance end
.lake/packages/mathlib/Mathlib/Geometry/Manifold/IsManifold/InteriorBoundary.lean
import Mathlib.Geometry.Manifold.IsManifold.ExtChartAt /-! # Interior and boundary of a manifold Define the interior and boundary of a manifold. ## Main definitions - **IsInteriorPoint x**: `p ∈ M` is an interior point if, for `φ` being the preferred chart at `x`, `φ x` is an interior point of `φ.target`. - **IsBoundaryPoint x**: `p ∈ M` is a boundary point if, `(extChartAt I x) x ∈ frontier (range I)`. - **interior I M** is the **interior** of `M`, the set of its interior points. - **boundary I M** is the **boundary** of `M`, the set of its boundary points. ## Main results - `ModelWithCorners.univ_eq_interior_union_boundary`: `M` is the union of its interior and boundary - `ModelWithCorners.interior_boundary_disjoint`: interior and boundary of `M` are disjoint - `BoundarylessManifold.isInteriorPoint`: if `M` is boundaryless, every point is an interior point - `ModelWithCorners.Boundaryless.boundary_eq_empty` and `of_boundary_eq_empty`: `M` is boundaryless if and only if its boundary is empty - `ModelWithCorners.interior_prod`: the interior of `M × N` is the product of the interiors of `M` and `N`. - `ModelWithCorners.boundary_prod`: the boundary of `M × N` is `∂M × N ∪ (M × ∂N)`. - `ModelWithCorners.BoundarylessManifold.prod`: if `M` and `N` are boundaryless, so is `M × N` - `ModelWithCorners.interior_disjointUnion`: the interior of a disjoint union `M ⊔ M'` is the union of the interior of `M` and `M'` - `ModelWithCorners.boundary_disjointUnion`: the boundary of a disjoint union `M ⊔ M'` is the union of the boundaries of `M` and `M'` - `ModelWithCorners.boundaryless_disjointUnion`: if `M` and `M'` are boundaryless, so is their disjoint union `M ⊔ M'` ## Tags manifold, interior, boundary ## TODO - `x` is an interior point iff *any* chart around `x` maps it to `interior (range I)`; similarly for the boundary. - the interior of `M` is open, hence the boundary is closed (and nowhere dense) In finite dimensions, this requires e.g. the homology of spheres. - the interior of `M` is a manifold without boundary - `boundary M` is a submanifold (possibly with boundary and corners): follows from the corresponding statement for the model with corners `I`; this requires a definition of submanifolds - if `M` is finite-dimensional, its boundary has measure zero -/ open Set open scoped Topology -- Let `M` be a manifold with corners over the pair `(E, H)`. variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] namespace ModelWithCorners variable (I) in /-- `p ∈ M` is an interior point of a manifold `M` iff its image in the extended chart lies in the interior of the model space. -/ def IsInteriorPoint (x : M) := extChartAt I x x ∈ interior (range I) variable (I) in /-- `p ∈ M` is a boundary point of a manifold `M` iff its image in the extended chart lies on the boundary of the model space. -/ def IsBoundaryPoint (x : M) := extChartAt I x x ∈ frontier (range I) variable (M) in /-- The **interior** of a manifold `M` is the set of its interior points. -/ protected def interior : Set M := { x : M | I.IsInteriorPoint x } lemma isInteriorPoint_iff {x : M} : I.IsInteriorPoint x ↔ extChartAt I x x ∈ interior (extChartAt I x).target := ⟨fun h ↦ (chartAt H x).mem_interior_extend_target (mem_chart_target H x) h, fun h ↦ OpenPartialHomeomorph.interior_extend_target_subset_interior_range _ h⟩ variable (M) in /-- The **boundary** of a manifold `M` is the set of its boundary points. -/ protected def boundary : Set M := { x : M | I.IsBoundaryPoint x } lemma isBoundaryPoint_iff {x : M} : I.IsBoundaryPoint x ↔ extChartAt I x x ∈ frontier (range I) := Iff.rfl /-- Every point is either an interior or a boundary point. -/ lemma isInteriorPoint_or_isBoundaryPoint (x : M) : I.IsInteriorPoint x ∨ I.IsBoundaryPoint x := by rw [IsInteriorPoint, or_iff_not_imp_left, I.isBoundaryPoint_iff, ← closure_diff_interior, I.isClosed_range.closure_eq, mem_diff] exact fun h ↦ ⟨mem_range_self _, h⟩ /-- A manifold decomposes into interior and boundary. -/ lemma interior_union_boundary_eq_univ : (I.interior M) ∪ (I.boundary M) = (univ : Set M) := eq_univ_of_forall fun x => (mem_union _ _ _).mpr (I.isInteriorPoint_or_isBoundaryPoint x) /-- The interior and boundary of a manifold `M` are disjoint. -/ lemma disjoint_interior_boundary : Disjoint (I.interior M) (I.boundary M) := by by_contra h -- Choose some x in the intersection of interior and boundary. obtain ⟨x, h1, h2⟩ := not_disjoint_iff.mp h rw [← mem_empty_iff_false (extChartAt I x x), ← disjoint_iff_inter_eq_empty.mp disjoint_interior_frontier, mem_inter_iff] exact ⟨h1, h2⟩ lemma isInteriorPoint_iff_not_isBoundaryPoint (x : M) : I.IsInteriorPoint x ↔ ¬I.IsBoundaryPoint x := by refine ⟨?_, by simpa only [or_iff_not_imp_right] using isInteriorPoint_or_isBoundaryPoint x (I := I)⟩ by_contra! h rw [← mem_empty_iff_false (extChartAt I x x), ← disjoint_iff_inter_eq_empty.mp disjoint_interior_frontier, mem_inter_iff] exact h /-- The boundary is the complement of the interior. -/ lemma compl_interior : (I.interior M)ᶜ = I.boundary M:= by apply compl_unique ?_ I.interior_union_boundary_eq_univ exact disjoint_iff_inter_eq_empty.mp I.disjoint_interior_boundary /-- The interior is the complement of the boundary. -/ lemma compl_boundary : (I.boundary M)ᶜ = I.interior M:= by rw [← compl_interior, compl_compl] lemma _root_.range_mem_nhds_isInteriorPoint {x : M} (h : I.IsInteriorPoint x) : range I ∈ 𝓝 (extChartAt I x x) := by rw [mem_nhds_iff] exact ⟨interior (range I), interior_subset, isOpen_interior, h⟩ /-- Type class for manifold without boundary. This differs from `ModelWithCorners.Boundaryless`, which states that the `ModelWithCorners` maps to the whole model vector space. -/ class _root_.BoundarylessManifold {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (M : Type*) [TopologicalSpace M] [ChartedSpace H M] : Prop where isInteriorPoint' : ∀ x : M, IsInteriorPoint I x section Boundaryless variable [I.Boundaryless] /-- Boundaryless `ModelWithCorners` implies boundaryless manifold. -/ instance : BoundarylessManifold I M where isInteriorPoint' x := by let r := ((chartAt H x).isOpen_extend_target (I := I)).interior_eq have : extChartAt I x = (chartAt H x).extend I := rfl rw [← this] at r rw [isInteriorPoint_iff, r] exact PartialEquiv.map_source _ (mem_extChartAt_source _) end Boundaryless section BoundarylessManifold /-- The empty manifold is boundaryless. -/ instance BoundarylessManifold.of_empty [IsEmpty M] : BoundarylessManifold I M where isInteriorPoint' x := (IsEmpty.false x).elim lemma _root_.BoundarylessManifold.isInteriorPoint {x : M} [BoundarylessManifold I M] : IsInteriorPoint I x := BoundarylessManifold.isInteriorPoint' x /-- If `I` is boundaryless, `M` has full interior. -/ lemma interior_eq_univ [BoundarylessManifold I M] : I.interior M = univ := eq_univ_of_forall fun _ => BoundarylessManifold.isInteriorPoint /-- Boundaryless manifolds have empty boundary. -/ lemma Boundaryless.boundary_eq_empty [BoundarylessManifold I M] : I.boundary M = ∅ := by rw [← I.compl_interior, I.interior_eq_univ, compl_empty_iff] instance [BoundarylessManifold I M] : IsEmpty (I.boundary M) := isEmpty_coe_sort.mpr Boundaryless.boundary_eq_empty /-- `M` is boundaryless iff its boundary is empty. -/ lemma Boundaryless.iff_boundary_eq_empty : I.boundary M = ∅ ↔ BoundarylessManifold I M := by refine ⟨fun h ↦ { isInteriorPoint' := ?_ }, fun a ↦ boundary_eq_empty⟩ intro x change x ∈ I.interior M rw [← compl_interior, compl_empty_iff] at h rw [h] trivial /-- Manifolds with empty boundary are boundaryless. -/ lemma Boundaryless.of_boundary_eq_empty (h : I.boundary M = ∅) : BoundarylessManifold I M := (Boundaryless.iff_boundary_eq_empty (I := I)).mp h end BoundarylessManifold /-! Interior and boundary of the product of two manifolds. -/ section prod variable {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {N : Type*} [TopologicalSpace N] [ChartedSpace H' N] {J : ModelWithCorners 𝕜 E' H'} {x : M} {y : N} /-- The interior of `M × N` is the product of the interiors of `M` and `N`. -/ lemma interior_prod : (I.prod J).interior (M × N) = (I.interior M) ×ˢ (J.interior N) := by ext p have aux : (interior (range ↑I)) ×ˢ (interior (range J)) = interior (range (I.prod J)) := by rw [← interior_prod_eq, ← range_prodMap, modelWithCorners_prod_coe] constructor <;> intro hp · replace hp : (I.prod J).IsInteriorPoint p := hp rw [IsInteriorPoint, ← aux] at hp exact hp · change (I.prod J).IsInteriorPoint p rw [IsInteriorPoint, ← aux, mem_prod] obtain h := Set.mem_prod.mp hp rw [ModelWithCorners.interior] at h exact h /-- The boundary of `M × N` is `∂M × N ∪ (M × ∂N)`. -/ lemma boundary_prod : (I.prod J).boundary (M × N) = Set.prod univ (J.boundary N) ∪ Set.prod (I.boundary M) univ := by let h := calc (I.prod J).boundary (M × N) _ = ((I.prod J).interior (M × N))ᶜ := compl_interior.symm _ = ((I.interior M) ×ˢ (J.interior N))ᶜ := by rw [interior_prod] _ = (I.interior M)ᶜ ×ˢ univ ∪ univ ×ˢ (J.interior N)ᶜ := by rw [compl_prod_eq_union] rw [h, I.compl_interior, J.compl_interior, union_comm] rfl /-- If `M` is boundaryless, `∂(M×N) = M × ∂N`. -/ lemma boundary_of_boundaryless_left [BoundarylessManifold I M] : (I.prod J).boundary (M × N) = Set.prod (univ : Set M) (J.boundary N) := by rw [boundary_prod, Boundaryless.boundary_eq_empty (I := I)] have : Set.prod (∅ : Set M) (univ : Set N) = ∅ := Set.empty_prod rw [this, union_empty] /-- If `N` is boundaryless, `∂(M×N) = ∂M × N`. -/ lemma boundary_of_boundaryless_right [BoundarylessManifold J N] : (I.prod J).boundary (M × N) = Set.prod (I.boundary M) (univ : Set N) := by rw [boundary_prod, Boundaryless.boundary_eq_empty (I := J)] have : Set.prod (univ : Set M) (∅ : Set N) = ∅ := Set.prod_empty rw [this, empty_union] /-- The product of two boundaryless manifolds is boundaryless. -/ instance BoundarylessManifold.prod [BoundarylessManifold I M] [BoundarylessManifold J N] : BoundarylessManifold (I.prod J) (M × N) := by apply Boundaryless.of_boundary_eq_empty simp only [boundary_prod, Boundaryless.boundary_eq_empty, union_empty_iff] -- These are simp lemmas, but `simp` does not apply them on its own: -- presumably because of the distinction between `Prod` and `ModelProd` exact ⟨Set.prod_empty, Set.empty_prod⟩ end prod section disjointUnion variable {M' : Type*} [TopologicalSpace M'] [ChartedSpace H M'] {n : WithTop ℕ∞} {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {J : Type*} {J : ModelWithCorners 𝕜 E' H'} {N N' : Type*} [TopologicalSpace N] [TopologicalSpace N'] [ChartedSpace H' N] [ChartedSpace H' N'] open Topology lemma interiorPoint_inl (x : M) (hx : I.IsInteriorPoint x) : I.IsInteriorPoint (.inl x: M ⊕ M') := by rw [I.isInteriorPoint_iff, extChartAt, ChartedSpace.sum_chartAt_inl] dsimp rw [Sum.inl_injective.extend_apply (chartAt H x)] simpa [I.isInteriorPoint_iff, extChartAt] using hx lemma boundaryPoint_inl (x : M) (hx : I.IsBoundaryPoint x) : I.IsBoundaryPoint (.inl x: M ⊕ M') := by rw [I.isBoundaryPoint_iff, extChartAt, ChartedSpace.sum_chartAt_inl] dsimp rw [Sum.inl_injective.extend_apply (chartAt H x)] simpa [I.isBoundaryPoint_iff, extChartAt] using hx lemma interiorPoint_inr (x : M') (hx : I.IsInteriorPoint x) : I.IsInteriorPoint (.inr x : M ⊕ M') := by rw [I.isInteriorPoint_iff, extChartAt, ChartedSpace.sum_chartAt_inr] dsimp rw [Sum.inr_injective.extend_apply (chartAt H x)] simpa [I.isInteriorPoint_iff, extChartAt] using hx lemma boundaryPoint_inr (x : M') (hx : I.IsBoundaryPoint x) : I.IsBoundaryPoint (.inr x : M ⊕ M') := by rw [I.isBoundaryPoint_iff, extChartAt, ChartedSpace.sum_chartAt_inr] dsimp rw [Sum.inr_injective.extend_apply (chartAt H x)] simpa [I.isBoundaryPoint_iff, extChartAt] using hx -- Converse to the previous direction: if `x` were not an interior point, -- it had to be a boundary point, hence `p` were a boundary point also, contradiction. lemma isInteriorPoint_disjointUnion_left {p : M ⊕ M'} (hp : I.IsInteriorPoint p) (hleft : Sum.isLeft p) : I.IsInteriorPoint (Sum.getLeft p hleft) := by by_contra h set x := Sum.getLeft p hleft rw [isInteriorPoint_iff_not_isBoundaryPoint x, not_not] at h rw [isInteriorPoint_iff_not_isBoundaryPoint p] at hp have := boundaryPoint_inl (M' := M') x (by tauto) rw [← Sum.eq_left_getLeft_of_isLeft hleft] at this exact hp this lemma isInteriorPoint_disjointUnion_right {p : M ⊕ M'} (hp : I.IsInteriorPoint p) (hright : Sum.isRight p) : I.IsInteriorPoint (Sum.getRight p hright) := by by_contra h set x := Sum.getRight p hright rw [← mem_empty_iff_false p, ← (disjoint_interior_boundary (I := I)).inter_eq] constructor · rw [ModelWithCorners.interior, mem_setOf]; exact hp · rw [ModelWithCorners.boundary, mem_setOf, Sum.eq_right_getRight_of_isRight hright] have := isInteriorPoint_or_isBoundaryPoint (I := I) x exact boundaryPoint_inr (M' := M') x (by tauto) lemma interior_disjointUnion : ModelWithCorners.interior (I := I) (M ⊕ M') = Sum.inl '' (ModelWithCorners.interior (I := I) M) ∪ Sum.inr '' (ModelWithCorners.interior (I := I) M') := by ext p constructor · intro hp by_cases h : Sum.isLeft p · left exact ⟨Sum.getLeft p h, isInteriorPoint_disjointUnion_left hp h, Sum.inl_getLeft p h⟩ · replace h := Sum.not_isLeft.mp h right exact ⟨Sum.getRight p h, isInteriorPoint_disjointUnion_right hp h, Sum.inr_getRight p h⟩ · intro hp by_cases h : Sum.isLeft p · set x := Sum.getLeft p h with x_eq rw [Sum.eq_left_getLeft_of_isLeft h] apply interiorPoint_inl x have hp : p ∈ Sum.inl '' (ModelWithCorners.interior (I := I) M) := by obtain (good | ⟨y, hy, hxy⟩) := hp exacts [good, (not_isLeft_and_isRight ⟨h, by rw [← hxy]; exact rfl⟩).elim] obtain ⟨x', hx', hx'p⟩ := hp simpa [x_eq, ← hx'p, Sum.getLeft_inl] · set x := Sum.getRight p (Sum.not_isLeft.mp h) with x_eq rw [Sum.eq_right_getRight_of_isRight (Sum.not_isLeft.mp h)] apply interiorPoint_inr x have hp : p ∈ Sum.inr '' (ModelWithCorners.interior (I := I) M') := by obtain (⟨y, hy, hxy⟩ | good) := hp exacts [(not_isLeft_and_isRight ⟨by rw [← hxy]; exact rfl, Sum.not_isLeft.mp h⟩).elim, good] obtain ⟨x', hx', hx'p⟩ := hp simpa [x_eq, ← hx'p, Sum.getRight_inr] lemma boundary_disjointUnion : ModelWithCorners.boundary (I := I) (M ⊕ M') = Sum.inl '' (ModelWithCorners.boundary (I := I) M) ∪ Sum.inr '' (ModelWithCorners.boundary (I := I) M') := by simp only [← ModelWithCorners.compl_interior, interior_disjointUnion, inl_compl_union_inr_compl] /-- If `M` and `M'` are boundaryless, so is their disjoint union `M ⊔ M'`. -/ instance boundaryless_disjointUnion [hM : BoundarylessManifold I M] [hM' : BoundarylessManifold I M'] : BoundarylessManifold I (M ⊕ M') := by rw [← Boundaryless.iff_boundary_eq_empty] at hM hM' ⊢ simp [boundary_disjointUnion, hM, hM'] end disjointUnion end ModelWithCorners
.lake/packages/mathlib/Mathlib/Geometry/Manifold/IsManifold/ExtChartAt.lean
import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Geometry.Manifold.IsManifold.Basic /-! # Extended charts in smooth manifolds In a `C^n` manifold with corners with the model `I` on `(E, H)`, the charts take values in the model space `H`. However, we also need to use extended charts taking values in the model vector space `E`. These extended charts are not `OpenPartialHomeomorph` as the target is not open in `E` in general, but we can still register them as `PartialEquiv`. ## Main definitions * `OpenPartialHomeomorph.extend`: compose an open partial homeomorphism into `H` with the model `I`, to obtain a `PartialEquiv` into `E`. Extended charts are an example of this. * `extChartAt I x`: the extended chart at `x`, obtained by composing the `chartAt H x` with `I`. Since the target is in general not open, this is not an open partial homeomorphism in general, but we register them as `PartialEquiv`s. ## Main results * `contDiffOn_extend_coord_change`: if `f` and `f'` lie in the maximal atlas on `M`, `f.extend I ∘ (f'.extend I).symm` is continuous on its source * `contDiffOn_ext_coord_change`: for `x x : M`, the coordinate change `(extChartAt I x').symm ≫ extChartAt I x` is continuous on its source * `Manifold.locallyCompact_of_finiteDimensional`: a finite-dimensional manifold modelled on a locally compact field (such as ℝ, ℂ or the `p`-adic numbers) is locally compact * `LocallyCompactSpace.of_locallyCompact_manifold`: a locally compact manifold must be modelled on a locally compact space. * `FiniteDimensional.of_locallyCompact_manifold`: a locally compact manifolds must be modelled on a finite-dimensional space -/ noncomputable section open Set Filter Function open scoped Manifold Topology variable {𝕜 E M H E' M' H' : Type*} [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] [TopologicalSpace H] [TopologicalSpace M] {n : WithTop ℕ∞} (f f' : OpenPartialHomeomorph M H) {I : ModelWithCorners 𝕜 E H} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] [TopologicalSpace H'] [TopologicalSpace M'] {I' : ModelWithCorners 𝕜 E' H'} {s t : Set M} section ExtendedCharts namespace OpenPartialHomeomorph variable (I) in /-- Given a chart `f` on a manifold with corners, `f.extend I` is the extended chart to the model vector space. -/ @[simp, mfld_simps] def extend : PartialEquiv M E := f.toPartialEquiv ≫ I.toPartialEquiv theorem extend_coe : ⇑(f.extend I) = I ∘ f := rfl theorem extend_coe_symm : ⇑(f.extend I).symm = f.symm ∘ I.symm := rfl theorem extend_source : (f.extend I).source = f.source := by rw [extend, PartialEquiv.trans_source, I.source_eq, preimage_univ, inter_univ] theorem isOpen_extend_source : IsOpen (f.extend I).source := by rw [extend_source] exact f.open_source theorem extend_target : (f.extend I).target = I.symm ⁻¹' f.target ∩ range I := by simp_rw [extend, PartialEquiv.trans_target, I.target_eq, I.toPartialEquiv_coe_symm, inter_comm] theorem extend_target' : (f.extend I).target = I '' f.target := by rw [extend, PartialEquiv.trans_target'', I.source_eq, univ_inter, I.toPartialEquiv_coe] lemma isOpen_extend_target [I.Boundaryless] : IsOpen (f.extend I).target := by rw [extend_target, I.range_eq_univ, inter_univ] exact I.continuous_symm.isOpen_preimage _ f.open_target theorem mapsTo_extend (hs : s ⊆ f.source) : MapsTo (f.extend I) s ((f.extend I).symm ⁻¹' s ∩ range I) := by rw [mapsTo_iff_image_subset, extend_coe, extend_coe_symm, preimage_comp, ← I.image_eq, image_comp, f.image_eq_target_inter_inv_preimage hs] exact image_mono inter_subset_right theorem extend_left_inv {x : M} (hxf : x ∈ f.source) : (f.extend I).symm (f.extend I x) = x := (f.extend I).left_inv <| by rwa [f.extend_source] /-- Variant of `f.extend_left_inv I`, stated in terms of images. -/ lemma extend_left_inv' (ht : t ⊆ f.source) : ((f.extend I).symm ∘ (f.extend I)) '' t = t := EqOn.image_eq_self (fun _ hx ↦ f.extend_left_inv (ht hx)) theorem extend_source_mem_nhds {x : M} (h : x ∈ f.source) : (f.extend I).source ∈ 𝓝 x := (isOpen_extend_source f).mem_nhds <| by rwa [f.extend_source] theorem extend_source_mem_nhdsWithin {x : M} (h : x ∈ f.source) : (f.extend I).source ∈ 𝓝[s] x := mem_nhdsWithin_of_mem_nhds <| extend_source_mem_nhds f h theorem continuousOn_extend : ContinuousOn (f.extend I) (f.extend I).source := by refine I.continuous.comp_continuousOn ?_ rw [extend_source] exact f.continuousOn theorem continuousAt_extend {x : M} (h : x ∈ f.source) : ContinuousAt (f.extend I) x := (continuousOn_extend f).continuousAt <| extend_source_mem_nhds f h theorem map_extend_nhds {x : M} (hy : x ∈ f.source) : map (f.extend I) (𝓝 x) = 𝓝[range I] f.extend I x := by rwa [extend_coe, comp_apply, ← I.map_nhds_eq, ← f.map_nhds_eq, map_map] theorem map_extend_nhds_of_mem_interior_range {x : M} (hx : x ∈ f.source) (h'x : f.extend I x ∈ interior (range I)) : map (f.extend I) (𝓝 x) = 𝓝 (f.extend I x) := by rw [f.map_extend_nhds hx, nhdsWithin_eq_nhds] exact mem_of_superset (isOpen_interior.mem_nhds h'x) interior_subset theorem map_extend_nhds_of_boundaryless [I.Boundaryless] {x : M} (hx : x ∈ f.source) : map (f.extend I) (𝓝 x) = 𝓝 (f.extend I x) := by rw [f.map_extend_nhds hx, I.range_eq_univ, nhdsWithin_univ] theorem extend_target_mem_nhdsWithin {y : M} (hy : y ∈ f.source) : (f.extend I).target ∈ 𝓝[range I] f.extend I y := by rw [← PartialEquiv.image_source_eq_target, ← map_extend_nhds f hy] exact image_mem_map (extend_source_mem_nhds _ hy) lemma extend_image_target_mem_nhds {x : M} (hx : x ∈ f.source) : I '' f.target ∈ 𝓝[range I] (f.extend I) x := by rw [← f.map_extend_nhds hx, Filter.mem_map, f.extend_coe, Set.preimage_comp, I.preimage_image f.target] exact (f.continuousAt hx).preimage_mem_nhds (f.open_target.mem_nhds (f.map_source hx)) theorem extend_image_nhds_mem_nhds_of_boundaryless [I.Boundaryless] {x} (hx : x ∈ f.source) {s : Set M} (h : s ∈ 𝓝 x) : (f.extend I) '' s ∈ 𝓝 ((f.extend I) x) := by rw [← f.map_extend_nhds_of_boundaryless hx, Filter.mem_map] filter_upwards [h] using subset_preimage_image (f.extend I) s @[deprecated (since := "2025-05-22")] alias extend_image_nhd_mem_nhds_of_boundaryless := extend_image_nhds_mem_nhds_of_boundaryless theorem extend_image_nhds_mem_nhds_of_mem_interior_range {x} (hx : x ∈ f.source) (h'x : f.extend I x ∈ interior (range I)) {s : Set M} (h : s ∈ 𝓝 x) : (f.extend I) '' s ∈ 𝓝 ((f.extend I) x) := by rw [← f.map_extend_nhds_of_mem_interior_range hx h'x, Filter.mem_map] filter_upwards [h] using subset_preimage_image (f.extend I) s @[deprecated (since := "2025-05-22")] alias extend_image_nhd_mem_nhds_of_mem_interior_range := extend_image_nhds_mem_nhds_of_mem_interior_range theorem extend_target_subset_range : (f.extend I).target ⊆ range I := by simp only [mfld_simps] lemma interior_extend_target_subset_interior_range : interior (f.extend I).target ⊆ interior (range I) := by rw [f.extend_target, interior_inter, (f.open_target.preimage I.continuous_symm).interior_eq] exact inter_subset_right /-- If `y ∈ f.target` and `I y ∈ interior (range I)`, then `I y` is an interior point of `(I ∘ f).target`. -/ lemma mem_interior_extend_target {y : H} (hy : y ∈ f.target) (hy' : I y ∈ interior (range I)) : I y ∈ interior (f.extend I).target := by rw [f.extend_target, interior_inter, (f.open_target.preimage I.continuous_symm).interior_eq, mem_inter_iff, mem_preimage] exact ⟨mem_of_eq_of_mem (I.left_inv (y)) hy, hy'⟩ theorem nhdsWithin_extend_target_eq {y : M} (hy : y ∈ f.source) : 𝓝[(f.extend I).target] f.extend I y = 𝓝[range I] f.extend I y := (nhdsWithin_mono _ (extend_target_subset_range _)).antisymm <| nhdsWithin_le_of_mem (extend_target_mem_nhdsWithin _ hy) theorem extend_target_eventuallyEq {y : M} (hy : y ∈ f.source) : (f.extend I).target =ᶠ[𝓝 (f.extend I y)] range I := nhdsWithin_eq_iff_eventuallyEq.1 (nhdsWithin_extend_target_eq _ hy) theorem continuousAt_extend_symm' {x : E} (h : x ∈ (f.extend I).target) : ContinuousAt (f.extend I).symm x := (f.continuousAt_symm h.2).comp I.continuous_symm.continuousAt theorem continuousAt_extend_symm {x : M} (h : x ∈ f.source) : ContinuousAt (f.extend I).symm (f.extend I x) := continuousAt_extend_symm' f <| (f.extend I).map_source <| by rwa [f.extend_source] theorem continuousOn_extend_symm : ContinuousOn (f.extend I).symm (f.extend I).target := fun _ h => (continuousAt_extend_symm' _ h).continuousWithinAt theorem extend_symm_continuousWithinAt_comp_right_iff {X} [TopologicalSpace X] {g : M → X} {s : Set M} {x : M} : ContinuousWithinAt (g ∘ (f.extend I).symm) ((f.extend I).symm ⁻¹' s ∩ range I) (f.extend I x) ↔ ContinuousWithinAt (g ∘ f.symm) (f.symm ⁻¹' s) (f x) := by rw [← I.symm_continuousWithinAt_comp_right_iff]; rfl theorem isOpen_extend_preimage' {s : Set E} (hs : IsOpen s) : IsOpen ((f.extend I).source ∩ f.extend I ⁻¹' s) := (continuousOn_extend f).isOpen_inter_preimage (isOpen_extend_source _) hs theorem isOpen_extend_preimage {s : Set E} (hs : IsOpen s) : IsOpen (f.source ∩ f.extend I ⁻¹' s) := by rw [← extend_source f (I := I)]; exact isOpen_extend_preimage' f hs theorem map_extend_nhdsWithin_eq_image {y : M} (hy : y ∈ f.source) : map (f.extend I) (𝓝[s] y) = 𝓝[f.extend I '' ((f.extend I).source ∩ s)] f.extend I y := by set e := f.extend I calc map e (𝓝[s] y) = map e (𝓝[e.source ∩ s] y) := congr_arg (map e) (nhdsWithin_inter_of_mem (extend_source_mem_nhdsWithin f hy)).symm _ = 𝓝[e '' (e.source ∩ s)] e y := ((f.extend I).leftInvOn.mono inter_subset_left).map_nhdsWithin_eq ((f.extend I).left_inv <| by rwa [f.extend_source]) (continuousAt_extend_symm f hy).continuousWithinAt (continuousAt_extend f hy).continuousWithinAt theorem map_extend_nhdsWithin_eq_image_of_subset {y : M} (hy : y ∈ f.source) (hs : s ⊆ f.source) : map (f.extend I) (𝓝[s] y) = 𝓝[f.extend I '' s] f.extend I y := by rw [map_extend_nhdsWithin_eq_image _ hy, inter_eq_self_of_subset_right] rwa [extend_source] theorem map_extend_nhdsWithin {y : M} (hy : y ∈ f.source) : map (f.extend I) (𝓝[s] y) = 𝓝[(f.extend I).symm ⁻¹' s ∩ range I] f.extend I y := by rw [map_extend_nhdsWithin_eq_image f hy, nhdsWithin_inter, ← nhdsWithin_extend_target_eq _ hy, ← nhdsWithin_inter, (f.extend I).image_source_inter_eq', inter_comm] theorem map_extend_symm_nhdsWithin {y : M} (hy : y ∈ f.source) : map (f.extend I).symm (𝓝[(f.extend I).symm ⁻¹' s ∩ range I] f.extend I y) = 𝓝[s] y := by rw [← map_extend_nhdsWithin f hy, map_map, Filter.map_congr, map_id] exact (f.extend I).leftInvOn.eqOn.eventuallyEq_of_mem (extend_source_mem_nhdsWithin _ hy) theorem map_extend_symm_nhdsWithin_range {y : M} (hy : y ∈ f.source) : map (f.extend I).symm (𝓝[range I] f.extend I y) = 𝓝 y := by rw [← nhdsWithin_univ, ← map_extend_symm_nhdsWithin f (I := I) hy, preimage_univ, univ_inter] theorem tendsto_extend_comp_iff {α : Type*} {l : Filter α} {g : α → M} (hg : ∀ᶠ z in l, g z ∈ f.source) {y : M} (hy : y ∈ f.source) : Tendsto (f.extend I ∘ g) l (𝓝 (f.extend I y)) ↔ Tendsto g l (𝓝 y) := by refine ⟨fun h u hu ↦ mem_map.2 ?_, (continuousAt_extend _ hy).tendsto.comp⟩ have := (f.continuousAt_extend_symm hy).tendsto.comp h rw [extend_left_inv _ hy] at this filter_upwards [hg, mem_map.1 (this hu)] with z hz hzu simpa only [(· ∘ ·), extend_left_inv _ hz, mem_preimage] using hzu -- there is no definition `writtenInExtend` but we already use some made-up names in this file theorem continuousWithinAt_writtenInExtend_iff {f' : OpenPartialHomeomorph M' H'} {g : M → M'} {y : M} (hy : y ∈ f.source) (hgy : g y ∈ f'.source) (hmaps : MapsTo g s f'.source) : ContinuousWithinAt (f'.extend I' ∘ g ∘ (f.extend I).symm) ((f.extend I).symm ⁻¹' s ∩ range I) (f.extend I y) ↔ ContinuousWithinAt g s y := by unfold ContinuousWithinAt simp only [comp_apply] rw [extend_left_inv _ hy, f'.tendsto_extend_comp_iff _ hgy, ← f.map_extend_symm_nhdsWithin (I := I) hy, tendsto_map'_iff] rw [← f.map_extend_nhdsWithin (I := I) hy, eventually_map] filter_upwards [inter_mem_nhdsWithin _ (f.open_source.mem_nhds hy)] with z hz rw [comp_apply, extend_left_inv _ hz.2] exact hmaps hz.1 -- there is no definition `writtenInExtend` but we already use some made-up names in this file /-- If `s ⊆ f.source` and `g x ∈ f'.source` whenever `x ∈ s`, then `g` is continuous on `s` if and only if `g` written in charts `f.extend I` and `f'.extend I'` is continuous on `f.extend I '' s`. -/ theorem continuousOn_writtenInExtend_iff {f' : OpenPartialHomeomorph M' H'} {g : M → M'} (hs : s ⊆ f.source) (hmaps : MapsTo g s f'.source) : ContinuousOn (f'.extend I' ∘ g ∘ (f.extend I).symm) (f.extend I '' s) ↔ ContinuousOn g s := by refine forall_mem_image.trans <| forall₂_congr fun x hx ↦ ?_ refine (continuousWithinAt_congr_set ?_).trans (continuousWithinAt_writtenInExtend_iff _ (hs hx) (hmaps hx) hmaps) rw [← nhdsWithin_eq_iff_eventuallyEq, ← map_extend_nhdsWithin_eq_image_of_subset, ← map_extend_nhdsWithin] exacts [hs hx, hs hx, hs] theorem extend_preimage_mem_nhds_of_mem_nhdsWithin {s : Set E} {x : M} (hx : x ∈ f.source) (hs : s ∈ 𝓝[range I] (f.extend I x)) : (f.extend I) ⁻¹' s ∈ 𝓝 x := by rwa [← map_extend_nhds (I := I) f hx] at hs /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ theorem extend_preimage_mem_nhdsWithin {x : M} (h : x ∈ f.source) (ht : t ∈ 𝓝[s] x) : (f.extend I).symm ⁻¹' t ∈ 𝓝[(f.extend I).symm ⁻¹' s ∩ range I] f.extend I x := by rwa [← map_extend_symm_nhdsWithin f (I := I) h, mem_map] at ht theorem extend_preimage_mem_nhds {x : M} (h : x ∈ f.source) (ht : t ∈ 𝓝 x) : (f.extend I).symm ⁻¹' t ∈ 𝓝 (f.extend I x) := by apply (continuousAt_extend_symm f h).preimage_mem_nhds rwa [(f.extend I).left_inv] rwa [f.extend_source] /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ theorem extend_preimage_inter_eq : (f.extend I).symm ⁻¹' (s ∩ t) ∩ range I = (f.extend I).symm ⁻¹' s ∩ range I ∩ (f.extend I).symm ⁻¹' t := by mfld_set_tac @[deprecated "Removed without replacement" (since := "2025-08-27")] theorem extend_symm_preimage_inter_range_eventuallyEq_aux {s : Set M} {x : M} (hx : x ∈ f.source) : ((f.extend I).symm ⁻¹' s ∩ range I : Set _) =ᶠ[𝓝 (f.extend I x)] ((f.extend I).target ∩ (f.extend I).symm ⁻¹' s : Set _) := by rw [f.extend_target, inter_assoc, inter_comm (range I)] conv => congr · skip rw [← univ_inter (_ ∩ range I)] refine (eventuallyEq_univ.mpr ?_).symm.inter EventuallyEq.rfl refine I.continuousAt_symm.preimage_mem_nhds (f.open_target.mem_nhds ?_) simp_rw [f.extend_coe, Function.comp_apply, I.left_inv, f.mapsTo hx] theorem extend_symm_preimage_inter_range_eventuallyEq {s : Set M} {x : M} (hs : s ⊆ f.source) (hx : x ∈ f.source) : ((f.extend I).symm ⁻¹' s ∩ range I : Set _) =ᶠ[𝓝 (f.extend I x)] f.extend I '' s := by rw [← nhdsWithin_eq_iff_eventuallyEq, ← map_extend_nhdsWithin _ hx, map_extend_nhdsWithin_eq_image_of_subset _ hx hs] /-! We use the name `extend_coord_change` for `(f'.extend I).symm ≫ f.extend I`. -/ theorem extend_coord_change_source : ((f.extend I).symm ≫ f'.extend I).source = I '' (f.symm ≫ₕ f').source := by simp_rw [PartialEquiv.trans_source, I.image_eq, extend_source, PartialEquiv.symm_source, extend_target, inter_right_comm _ (range I)] rfl theorem extend_image_source_inter : f.extend I '' (f.source ∩ f'.source) = ((f.extend I).symm ≫ f'.extend I).source := by simp_rw [f.extend_coord_change_source, f.extend_coe, image_comp I f, trans_source'', symm_symm, symm_target] theorem extend_coord_change_source_mem_nhdsWithin {x : E} (hx : x ∈ ((f.extend I).symm ≫ f'.extend I).source) : ((f.extend I).symm ≫ f'.extend I).source ∈ 𝓝[range I] x := by rw [f.extend_coord_change_source] at hx ⊢ obtain ⟨x, hx, rfl⟩ := hx refine I.image_mem_nhdsWithin ?_ exact (OpenPartialHomeomorph.open_source _).mem_nhds hx theorem extend_coord_change_source_mem_nhdsWithin' {x : M} (hxf : x ∈ f.source) (hxf' : x ∈ f'.source) : ((f.extend I).symm ≫ f'.extend I).source ∈ 𝓝[range I] f.extend I x := by apply extend_coord_change_source_mem_nhdsWithin rw [← extend_image_source_inter] exact mem_image_of_mem _ ⟨hxf, hxf'⟩ variable {f f'} open IsManifold theorem contDiffOn_extend_coord_change [ChartedSpace H M] (hf : f ∈ maximalAtlas I n M) (hf' : f' ∈ maximalAtlas I n M) : ContDiffOn 𝕜 n (f.extend I ∘ (f'.extend I).symm) ((f'.extend I).symm ≫ f.extend I).source := by rw [extend_coord_change_source, I.image_eq] exact (StructureGroupoid.compatible_of_mem_maximalAtlas hf' hf).1 theorem contDiffWithinAt_extend_coord_change [ChartedSpace H M] (hf : f ∈ maximalAtlas I n M) (hf' : f' ∈ maximalAtlas I n M) {x : E} (hx : x ∈ ((f'.extend I).symm ≫ f.extend I).source) : ContDiffWithinAt 𝕜 n (f.extend I ∘ (f'.extend I).symm) (range I) x := by apply (contDiffOn_extend_coord_change hf hf' x hx).mono_of_mem_nhdsWithin rw [extend_coord_change_source] at hx ⊢ obtain ⟨z, hz, rfl⟩ := hx exact I.image_mem_nhdsWithin ((OpenPartialHomeomorph.open_source _).mem_nhds hz) theorem contDiffWithinAt_extend_coord_change' [ChartedSpace H M] (hf : f ∈ maximalAtlas I n M) (hf' : f' ∈ maximalAtlas I n M) {x : M} (hxf : x ∈ f.source) (hxf' : x ∈ f'.source) : ContDiffWithinAt 𝕜 n (f.extend I ∘ (f'.extend I).symm) (range I) (f'.extend I x) := by refine contDiffWithinAt_extend_coord_change hf hf' ?_ rw [← extend_image_source_inter] exact mem_image_of_mem _ ⟨hxf', hxf⟩ end OpenPartialHomeomorph open OpenPartialHomeomorph variable [ChartedSpace H M] [ChartedSpace H' M'] variable (I) in /-- The preferred extended chart on a manifold with corners around a point `x`, from a neighborhood of `x` to the model vector space. -/ @[simp, mfld_simps] def extChartAt (x : M) : PartialEquiv M E := (chartAt H x).extend I theorem extChartAt_coe (x : M) : ⇑(extChartAt I x) = I ∘ chartAt H x := rfl theorem extChartAt_coe_symm (x : M) : ⇑(extChartAt I x).symm = (chartAt H x).symm ∘ I.symm := rfl variable (I) in theorem extChartAt_source (x : M) : (extChartAt I x).source = (chartAt H x).source := extend_source _ theorem isOpen_extChartAt_source (x : M) : IsOpen (extChartAt I x).source := isOpen_extend_source _ theorem mem_extChartAt_source (x : M) : x ∈ (extChartAt I x).source := by simp only [extChartAt_source, mem_chart_source] theorem mem_extChartAt_target (x : M) : extChartAt I x x ∈ (extChartAt I x).target := (extChartAt I x).map_source <| mem_extChartAt_source _ variable (I) in theorem extChartAt_target (x : M) : (extChartAt I x).target = I.symm ⁻¹' (chartAt H x).target ∩ range I := extend_target _ theorem uniqueDiffOn_extChartAt_target (x : M) : UniqueDiffOn 𝕜 (extChartAt I x).target := by rw [extChartAt_target] exact I.uniqueDiffOn_preimage (chartAt H x).open_target theorem uniqueDiffWithinAt_extChartAt_target (x : M) : UniqueDiffWithinAt 𝕜 (extChartAt I x).target (extChartAt I x x) := uniqueDiffOn_extChartAt_target x _ <| mem_extChartAt_target x theorem extChartAt_to_inv (x : M) : (extChartAt I x).symm ((extChartAt I x) x) = x := (extChartAt I x).left_inv (mem_extChartAt_source x) theorem mapsTo_extChartAt {x : M} (hs : s ⊆ (chartAt H x).source) : MapsTo (extChartAt I x) s ((extChartAt I x).symm ⁻¹' s ∩ range I) := mapsTo_extend _ hs theorem extChartAt_source_mem_nhds' {x x' : M} (h : x' ∈ (extChartAt I x).source) : (extChartAt I x).source ∈ 𝓝 x' := extend_source_mem_nhds _ <| by rwa [← extChartAt_source I] theorem extChartAt_source_mem_nhds (x : M) : (extChartAt I x).source ∈ 𝓝 x := extChartAt_source_mem_nhds' (mem_extChartAt_source x) theorem extChartAt_source_mem_nhdsWithin' {x x' : M} (h : x' ∈ (extChartAt I x).source) : (extChartAt I x).source ∈ 𝓝[s] x' := mem_nhdsWithin_of_mem_nhds (extChartAt_source_mem_nhds' h) theorem extChartAt_source_mem_nhdsWithin (x : M) : (extChartAt I x).source ∈ 𝓝[s] x := mem_nhdsWithin_of_mem_nhds (extChartAt_source_mem_nhds x) theorem continuousOn_extChartAt (x : M) : ContinuousOn (extChartAt I x) (extChartAt I x).source := continuousOn_extend _ theorem continuousAt_extChartAt' {x x' : M} (h : x' ∈ (extChartAt I x).source) : ContinuousAt (extChartAt I x) x' := continuousAt_extend _ <| by rwa [← extChartAt_source I] theorem continuousAt_extChartAt (x : M) : ContinuousAt (extChartAt I x) x := continuousAt_extChartAt' (mem_extChartAt_source x) theorem map_extChartAt_nhds' {x y : M} (hy : y ∈ (extChartAt I x).source) : map (extChartAt I x) (𝓝 y) = 𝓝[range I] extChartAt I x y := map_extend_nhds _ <| by rwa [← extChartAt_source I] theorem map_extChartAt_nhds (x : M) : map (extChartAt I x) (𝓝 x) = 𝓝[range I] extChartAt I x x := map_extChartAt_nhds' <| mem_extChartAt_source x theorem map_extChartAt_nhds_of_boundaryless [I.Boundaryless] (x : M) : map (extChartAt I x) (𝓝 x) = 𝓝 (extChartAt I x x) := by rw [extChartAt] exact map_extend_nhds_of_boundaryless (chartAt H x) (mem_chart_source H x) theorem extChartAt_image_nhds_mem_nhds_of_mem_interior_range {x y} (hx : y ∈ (extChartAt I x).source) (h'x : extChartAt I x y ∈ interior (range I)) {s : Set M} (h : s ∈ 𝓝 y) : (extChartAt I x) '' s ∈ 𝓝 (extChartAt I x y) := by rw [extChartAt] exact extend_image_nhds_mem_nhds_of_mem_interior_range _ (by simpa using hx) h'x h @[deprecated (since := "2025-05-22")] alias extChartAt_image_nhd_mem_nhds_of_mem_interior_range := extChartAt_image_nhds_mem_nhds_of_mem_interior_range variable {x} in theorem extChartAt_image_nhds_mem_nhds_of_boundaryless [I.Boundaryless] {x : M} (hx : s ∈ 𝓝 x) : extChartAt I x '' s ∈ 𝓝 (extChartAt I x x) := by rw [extChartAt] exact extend_image_nhds_mem_nhds_of_boundaryless _ (mem_chart_source H x) hx @[deprecated (since := "2025-05-22")] alias extChartAt_image_nhd_mem_nhds_of_boundaryless := extChartAt_image_nhds_mem_nhds_of_boundaryless theorem extChartAt_target_mem_nhdsWithin' {x y : M} (hy : y ∈ (extChartAt I x).source) : (extChartAt I x).target ∈ 𝓝[range I] extChartAt I x y := extend_target_mem_nhdsWithin _ <| by rwa [← extChartAt_source I] theorem extChartAt_target_mem_nhdsWithin (x : M) : (extChartAt I x).target ∈ 𝓝[range I] extChartAt I x x := extChartAt_target_mem_nhdsWithin' (mem_extChartAt_source x) theorem extChartAt_target_mem_nhdsWithin_of_mem {x : M} {y : E} (hy : y ∈ (extChartAt I x).target) : (extChartAt I x).target ∈ 𝓝[range I] y := by rw [← (extChartAt I x).right_inv hy] apply extChartAt_target_mem_nhdsWithin' exact (extChartAt I x).map_target hy theorem extChartAt_target_union_compl_range_mem_nhds_of_mem {y : E} {x : M} (hy : y ∈ (extChartAt I x).target) : (extChartAt I x).target ∪ (range I)ᶜ ∈ 𝓝 y := by rw [← nhdsWithin_univ, ← union_compl_self (range I), nhdsWithin_union] exact Filter.union_mem_sup (extChartAt_target_mem_nhdsWithin_of_mem hy) self_mem_nhdsWithin /-- If we're boundaryless, `extChartAt` has open target -/ theorem isOpen_extChartAt_target [I.Boundaryless] (x : M) : IsOpen (extChartAt I x).target := by simp_rw [extChartAt_target, I.range_eq_univ, inter_univ] exact (OpenPartialHomeomorph.open_target _).preimage I.continuous_symm /-- If we're boundaryless, `(extChartAt I x).target` is a neighborhood of the key point -/ theorem extChartAt_target_mem_nhds [I.Boundaryless] (x : M) : (extChartAt I x).target ∈ 𝓝 (extChartAt I x x) := by convert extChartAt_target_mem_nhdsWithin x simp only [I.range_eq_univ, nhdsWithin_univ] /-- If we're boundaryless, `(extChartAt I x).target` is a neighborhood of any of its points -/ theorem extChartAt_target_mem_nhds' [I.Boundaryless] {x : M} {y : E} (m : y ∈ (extChartAt I x).target) : (extChartAt I x).target ∈ 𝓝 y := (isOpen_extChartAt_target x).mem_nhds m theorem extChartAt_target_subset_range (x : M) : (extChartAt I x).target ⊆ range I := by simp only [mfld_simps] /-- Around the image of a point in the source, the neighborhoods are the same within `(extChartAt I x).target` and within `range I`. -/ theorem nhdsWithin_extChartAt_target_eq' {x y : M} (hy : y ∈ (extChartAt I x).source) : 𝓝[(extChartAt I x).target] extChartAt I x y = 𝓝[range I] extChartAt I x y := nhdsWithin_extend_target_eq _ <| by rwa [← extChartAt_source I] /-- Around a point in the target, the neighborhoods are the same within `(extChartAt I x).target` and within `range I`. -/ theorem nhdsWithin_extChartAt_target_eq_of_mem {x : M} {z : E} (hz : z ∈ (extChartAt I x).target) : 𝓝[(extChartAt I x).target] z = 𝓝[range I] z := by rw [← PartialEquiv.right_inv (extChartAt I x) hz] exact nhdsWithin_extChartAt_target_eq' ((extChartAt I x).map_target hz) /-- Around the image of the base point, the neighborhoods are the same within `(extChartAt I x).target` and within `range I`. -/ theorem nhdsWithin_extChartAt_target_eq (x : M) : 𝓝[(extChartAt I x).target] (extChartAt I x) x = 𝓝[range I] (extChartAt I x) x := nhdsWithin_extChartAt_target_eq' (mem_extChartAt_source x) /-- Around the image of a point in the source, `(extChartAt I x).target` and `range I` coincide locally. -/ theorem extChartAt_target_eventuallyEq' {x y : M} (hy : y ∈ (extChartAt I x).source) : (extChartAt I x).target =ᶠ[𝓝 (extChartAt I x y)] range I := nhdsWithin_eq_iff_eventuallyEq.1 (nhdsWithin_extChartAt_target_eq' hy) /-- Around a point in the target, `(extChartAt I x).target` and `range I` coincide locally. -/ theorem extChartAt_target_eventuallyEq_of_mem {x : M} {z : E} (hz : z ∈ (extChartAt I x).target) : (extChartAt I x).target =ᶠ[𝓝 z] range I := nhdsWithin_eq_iff_eventuallyEq.1 (nhdsWithin_extChartAt_target_eq_of_mem hz) /-- Around the image of the base point, `(extChartAt I x).target` and `range I` coincide locally. -/ theorem extChartAt_target_eventuallyEq {x : M} : (extChartAt I x).target =ᶠ[𝓝 (extChartAt I x x)] range I := nhdsWithin_eq_iff_eventuallyEq.1 (nhdsWithin_extChartAt_target_eq x) theorem continuousAt_extChartAt_symm'' {x : M} {y : E} (h : y ∈ (extChartAt I x).target) : ContinuousAt (extChartAt I x).symm y := continuousAt_extend_symm' _ h theorem continuousAt_extChartAt_symm' {x x' : M} (h : x' ∈ (extChartAt I x).source) : ContinuousAt (extChartAt I x).symm (extChartAt I x x') := continuousAt_extChartAt_symm'' <| (extChartAt I x).map_source h theorem continuousAt_extChartAt_symm (x : M) : ContinuousAt (extChartAt I x).symm ((extChartAt I x) x) := continuousAt_extChartAt_symm' (mem_extChartAt_source x) theorem continuousOn_extChartAt_symm (x : M) : ContinuousOn (extChartAt I x).symm (extChartAt I x).target := fun _y hy => (continuousAt_extChartAt_symm'' hy).continuousWithinAt lemma extChartAt_target_subset_closure_interior {x : M} : (extChartAt I x).target ⊆ closure (interior (extChartAt I x).target) := by intro y hy rw [mem_closure_iff_nhds] intro t ht have A : t ∩ ((extChartAt I x).target ∪ (range I)ᶜ) ∈ 𝓝 y := inter_mem ht (extChartAt_target_union_compl_range_mem_nhds_of_mem hy) have B : y ∈ closure (interior (range I)) := by apply I.range_subset_closure_interior (extChartAt_target_subset_range x hy) obtain ⟨z, ⟨tz, h'z⟩, hz⟩ : (t ∩ ((extChartAt I x).target ∪ (range ↑I)ᶜ) ∩ interior (range I)).Nonempty := mem_closure_iff_nhds.1 B _ A refine ⟨z, ⟨tz, ?_⟩⟩ have h''z : z ∈ (extChartAt I x).target := by simpa [interior_subset hz] using h'z exact (extChartAt_target_eventuallyEq_of_mem h''z).symm.mem_interior hz variable (I) in theorem interior_extChartAt_target_nonempty (x : M) : (interior (extChartAt I x).target).Nonempty := by by_contra! H have := extChartAt_target_subset_closure_interior (mem_extChartAt_target (I := I) x) simp only [H, closure_empty, mem_empty_iff_false] at this lemma extChartAt_mem_closure_interior {x₀ x : M} (hx : x ∈ closure (interior s)) (h'x : x ∈ (extChartAt I x₀).source) : extChartAt I x₀ x ∈ closure (interior ((extChartAt I x₀).symm ⁻¹' s ∩ (extChartAt I x₀).target)) := by simp_rw [mem_closure_iff, interior_inter, ← inter_assoc] intro o o_open ho obtain ⟨y, ⟨yo, hy⟩, ys⟩ : ((extChartAt I x₀) ⁻¹' o ∩ (extChartAt I x₀).source ∩ interior s).Nonempty := by have : (extChartAt I x₀) ⁻¹' o ∈ 𝓝 x := by apply (continuousAt_extChartAt' h'x).preimage_mem_nhds (o_open.mem_nhds ho) refine (mem_closure_iff_nhds.1 hx) _ (inter_mem this ?_) apply (isOpen_extChartAt_source x₀).mem_nhds h'x have A : interior (↑(extChartAt I x₀).symm ⁻¹' s) ∈ 𝓝 (extChartAt I x₀ y) := by simp only [interior_mem_nhds] apply (continuousAt_extChartAt_symm' hy).preimage_mem_nhds simp only [hy, PartialEquiv.left_inv] exact mem_interior_iff_mem_nhds.mp ys have B : (extChartAt I x₀) y ∈ closure (interior (extChartAt I x₀).target) := by apply extChartAt_target_subset_closure_interior (x := x₀) exact (extChartAt I x₀).map_source hy exact mem_closure_iff_nhds.1 B _ (inter_mem (o_open.mem_nhds yo) A) theorem isOpen_extChartAt_preimage' (x : M) {s : Set E} (hs : IsOpen s) : IsOpen ((extChartAt I x).source ∩ extChartAt I x ⁻¹' s) := isOpen_extend_preimage' _ hs theorem isOpen_extChartAt_preimage (x : M) {s : Set E} (hs : IsOpen s) : IsOpen ((chartAt H x).source ∩ extChartAt I x ⁻¹' s) := by rw [← extChartAt_source I] exact isOpen_extChartAt_preimage' x hs theorem map_extChartAt_nhdsWithin_eq_image' {x y : M} (hy : y ∈ (extChartAt I x).source) : map (extChartAt I x) (𝓝[s] y) = 𝓝[extChartAt I x '' ((extChartAt I x).source ∩ s)] extChartAt I x y := map_extend_nhdsWithin_eq_image _ <| by rwa [← extChartAt_source I] theorem map_extChartAt_nhdsWithin_eq_image (x : M) : map (extChartAt I x) (𝓝[s] x) = 𝓝[extChartAt I x '' ((extChartAt I x).source ∩ s)] extChartAt I x x := map_extChartAt_nhdsWithin_eq_image' (mem_extChartAt_source x) theorem map_extChartAt_nhdsWithin' {x y : M} (hy : y ∈ (extChartAt I x).source) : map (extChartAt I x) (𝓝[s] y) = 𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] extChartAt I x y := map_extend_nhdsWithin _ <| by rwa [← extChartAt_source I] theorem map_extChartAt_nhdsWithin (x : M) : map (extChartAt I x) (𝓝[s] x) = 𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] extChartAt I x x := map_extChartAt_nhdsWithin' (mem_extChartAt_source x) theorem map_extChartAt_symm_nhdsWithin' {x y : M} (hy : y ∈ (extChartAt I x).source) : map (extChartAt I x).symm (𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] extChartAt I x y) = 𝓝[s] y := map_extend_symm_nhdsWithin _ <| by rwa [← extChartAt_source I] theorem map_extChartAt_symm_nhdsWithin_range' {x y : M} (hy : y ∈ (extChartAt I x).source) : map (extChartAt I x).symm (𝓝[range I] extChartAt I x y) = 𝓝 y := map_extend_symm_nhdsWithin_range _ <| by rwa [← extChartAt_source I] theorem map_extChartAt_symm_nhdsWithin (x : M) : map (extChartAt I x).symm (𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] extChartAt I x x) = 𝓝[s] x := map_extChartAt_symm_nhdsWithin' (mem_extChartAt_source x) theorem map_extChartAt_symm_nhdsWithin_range (x : M) : map (extChartAt I x).symm (𝓝[range I] extChartAt I x x) = 𝓝 x := map_extChartAt_symm_nhdsWithin_range' (mem_extChartAt_source x) theorem extChartAt_preimage_mem_nhds_of_mem_nhdsWithin {s : Set E} {x x' : M} (hx : x' ∈ (extChartAt I x).source) (hs : s ∈ 𝓝[range I] (extChartAt I x x')) : (extChartAt I x) ⁻¹' s ∈ 𝓝 x' := extend_preimage_mem_nhds_of_mem_nhdsWithin _ (by simpa using hx) hs /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point in the source is a neighborhood of the preimage, within a set. -/ theorem extChartAt_preimage_mem_nhdsWithin' {x x' : M} (h : x' ∈ (extChartAt I x).source) (ht : t ∈ 𝓝[s] x') : (extChartAt I x).symm ⁻¹' t ∈ 𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x) x' := by rwa [← map_extChartAt_symm_nhdsWithin' h, mem_map] at ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of the base point is a neighborhood of the preimage, within a set. -/ theorem extChartAt_preimage_mem_nhdsWithin {x : M} (ht : t ∈ 𝓝[s] x) : (extChartAt I x).symm ⁻¹' t ∈ 𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x) x := extChartAt_preimage_mem_nhdsWithin' (mem_extChartAt_source x) ht theorem extChartAt_preimage_mem_nhds' {x x' : M} (h : x' ∈ (extChartAt I x).source) (ht : t ∈ 𝓝 x') : (extChartAt I x).symm ⁻¹' t ∈ 𝓝 (extChartAt I x x') := extend_preimage_mem_nhds _ (by rwa [← extChartAt_source I]) ht /-- Technical lemma ensuring that the preimage under an extended chart of a neighborhood of a point is a neighborhood of the preimage. -/ theorem extChartAt_preimage_mem_nhds {x : M} (ht : t ∈ 𝓝 x) : (extChartAt I x).symm ⁻¹' t ∈ 𝓝 ((extChartAt I x) x) := by apply (continuousAt_extChartAt_symm x).preimage_mem_nhds rwa [(extChartAt I x).left_inv (mem_extChartAt_source _)] /-- Technical lemma to rewrite suitably the preimage of an intersection under an extended chart, to bring it into a convenient form to apply derivative lemmas. -/ theorem extChartAt_preimage_inter_eq (x : M) : (extChartAt I x).symm ⁻¹' (s ∩ t) ∩ range I = (extChartAt I x).symm ⁻¹' s ∩ range I ∩ (extChartAt I x).symm ⁻¹' t := by mfld_set_tac theorem ContinuousWithinAt.nhdsWithin_extChartAt_symm_preimage_inter_range {f : M → M'} {x : M} (hc : ContinuousWithinAt f s x) : 𝓝[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x x) = 𝓝[(extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' (f x)).source)] (extChartAt I x x) := by rw [← (extChartAt I x).image_source_inter_eq', ← map_extChartAt_nhdsWithin_eq_image, ← map_extChartAt_nhdsWithin, nhdsWithin_inter_of_mem'] exact hc (extChartAt_source_mem_nhds _) theorem ContinuousWithinAt.extChartAt_symm_preimage_inter_range_eventuallyEq {f : M → M'} {x : M} (hc : ContinuousWithinAt f s x) : ((extChartAt I x).symm ⁻¹' s ∩ range I : Set E) =ᶠ[𝓝 (extChartAt I x x)] ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' (f x)).source) : Set E) := by rw [← nhdsWithin_eq_iff_eventuallyEq] exact hc.nhdsWithin_extChartAt_symm_preimage_inter_range /-! We use the name `ext_coord_change` for `(extChartAt I x').symm ≫ extChartAt I x`. -/ theorem ext_coord_change_source (x x' : M) : ((extChartAt I x').symm ≫ extChartAt I x).source = I '' ((chartAt H x').symm ≫ₕ chartAt H x).source := extend_coord_change_source _ _ open IsManifold theorem contDiffOn_ext_coord_change [IsManifold I n M] (x x' : M) : ContDiffOn 𝕜 n (extChartAt I x ∘ (extChartAt I x').symm) ((extChartAt I x').symm ≫ extChartAt I x).source := contDiffOn_extend_coord_change (chart_mem_maximalAtlas x) (chart_mem_maximalAtlas x') theorem contDiffWithinAt_ext_coord_change [IsManifold I n M] (x x' : M) {y : E} (hy : y ∈ ((extChartAt I x').symm ≫ extChartAt I x).source) : ContDiffWithinAt 𝕜 n (extChartAt I x ∘ (extChartAt I x').symm) (range I) y := contDiffWithinAt_extend_coord_change (chart_mem_maximalAtlas x) (chart_mem_maximalAtlas x') hy variable (I I') in /-- Conjugating a function to write it in the preferred charts around `x`. The manifold derivative of `f` will just be the derivative of this conjugated function. -/ @[simp, mfld_simps] def writtenInExtChartAt (x : M) (f : M → M') : E → E' := extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm theorem writtenInExtChartAt_chartAt {x : M} {y : E} (h : y ∈ (extChartAt I x).target) : writtenInExtChartAt I I x (chartAt H x) y = y := by simp_all only [mfld_simps] theorem writtenInExtChartAt_chartAt_symm {x : M} {y : E} (h : y ∈ (extChartAt I x).target) : writtenInExtChartAt I I (chartAt H x x) (chartAt H x).symm y = y := by simp_all only [mfld_simps] theorem writtenInExtChartAt_extChartAt {x : M} {y : E} (h : y ∈ (extChartAt I x).target) : writtenInExtChartAt I 𝓘(𝕜, E) x (extChartAt I x) y = y := by simp_all only [mfld_simps] theorem writtenInExtChartAt_extChartAt_symm {x : M} {y : E} (h : y ∈ (extChartAt I x).target) : writtenInExtChartAt 𝓘(𝕜, E) I (extChartAt I x x) (extChartAt I x).symm y = y := by simp_all only [mfld_simps] section variable {G G' F F' N N' : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] [NormedAddCommGroup F'] [NormedSpace 𝕜 F'] [TopologicalSpace G] [TopologicalSpace N] [TopologicalSpace G'] [TopologicalSpace N'] {J : ModelWithCorners 𝕜 F G} {J' : ModelWithCorners 𝕜 F' G'} [ChartedSpace G N] [ChartedSpace G' N'] lemma writtenInExtChart_prod {f : M → N} {g : M' → N'} {x : M} {x' : M'} : (writtenInExtChartAt (I.prod I') (J.prod J') (x, x') (Prod.map f g)) = Prod.map (writtenInExtChartAt I J x f) (writtenInExtChartAt I' J' x' g) := by ext p <;> simp [writtenInExtChartAt, I.toPartialEquiv.prod_symm, (chartAt H x).toPartialEquiv.prod_symm] end variable (𝕜) theorem extChartAt_self_eq {x : H} : ⇑(extChartAt I x) = I := rfl theorem extChartAt_self_apply {x y : H} : extChartAt I x y = I y := rfl /-- In the case of the manifold structure on a vector space, the extended charts are just the identity. -/ theorem extChartAt_model_space_eq_id (x : E) : extChartAt 𝓘(𝕜, E) x = PartialEquiv.refl E := by simp only [mfld_simps] theorem ext_chart_model_space_apply {x y : E} : extChartAt 𝓘(𝕜, E) x y = y := rfl variable {𝕜} theorem extChartAt_prod (x : M × M') : extChartAt (I.prod I') x = (extChartAt I x.1).prod (extChartAt I' x.2) := by simp only [mfld_simps] rw [PartialEquiv.prod_trans] theorem extChartAt_comp [ChartedSpace H H'] (x : M') : (letI := ChartedSpace.comp H H' M'; extChartAt I x) = (chartAt H' x).toPartialEquiv ≫ extChartAt I (chartAt H' x x) := PartialEquiv.trans_assoc .. theorem writtenInExtChartAt_chartAt_comp [ChartedSpace H H'] (x : M') {y} (hy : y ∈ letI := ChartedSpace.comp H H' M'; (extChartAt I x).target) : (letI := ChartedSpace.comp H H' M'; writtenInExtChartAt I I x (chartAt H' x) y) = y := by letI := ChartedSpace.comp H H' M' simp_all only [mfld_simps, chartAt_comp] theorem writtenInExtChartAt_chartAt_symm_comp [ChartedSpace H H'] (x : M') {y} (hy : y ∈ letI := ChartedSpace.comp H H' M'; (extChartAt I x).target) : ( letI := ChartedSpace.comp H H' M' writtenInExtChartAt I I (chartAt H' x x) (chartAt H' x).symm y) = y := by letI := ChartedSpace.comp H H' M' simp_all only [mfld_simps, chartAt_comp] end ExtendedCharts section Topology -- Let `M` be a topological manifold over the field 𝕜. variable {E : Type*} {𝕜 : Type*} [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {M : Type*} [TopologicalSpace M] [ChartedSpace H M] /-- A finite-dimensional manifold modelled on a locally compact field (such as ℝ, ℂ or the `p`-adic numbers) is locally compact. -/ lemma Manifold.locallyCompact_of_finiteDimensional (I : ModelWithCorners 𝕜 E H) [LocallyCompactSpace 𝕜] [FiniteDimensional 𝕜 E] : LocallyCompactSpace M := by have : ProperSpace E := FiniteDimensional.proper 𝕜 E have : LocallyCompactSpace H := I.locallyCompactSpace exact ChartedSpace.locallyCompactSpace H M variable (M) /-- A locally compact manifold must be modelled on a locally compact space. -/ lemma LocallyCompactSpace.of_locallyCompact_manifold (I : ModelWithCorners 𝕜 E H) [h : Nonempty M] [LocallyCompactSpace M] : LocallyCompactSpace E := by rcases h with ⟨x⟩ obtain ⟨y, hy⟩ := interior_extChartAt_target_nonempty I x have h'y : y ∈ (extChartAt I x).target := interior_subset hy obtain ⟨s, hmem, hss, hcom⟩ := LocallyCompactSpace.local_compact_nhds ((extChartAt I x).symm y) (extChartAt I x).source ((isOpen_extChartAt_source x).mem_nhds ((extChartAt I x).map_target h'y)) have : IsCompact <| (extChartAt I x) '' s := hcom.image_of_continuousOn <| (continuousOn_extChartAt x).mono hss apply this.locallyCompactSpace_of_mem_nhds_of_addGroup (x := y) rw [← (extChartAt I x).right_inv h'y] apply extChartAt_image_nhds_mem_nhds_of_mem_interior_range (PartialEquiv.map_target (extChartAt I x) h'y) _ hmem simp only [(extChartAt I x).right_inv h'y] exact interior_mono (extChartAt_target_subset_range x) hy /-- Riesz's theorem applied to manifolds: a locally compact manifolds must be modelled on a finite-dimensional space. This is the converse to `Manifold.locallyCompact_of_finiteDimensional`. -/ theorem FiniteDimensional.of_locallyCompact_manifold [CompleteSpace 𝕜] (I : ModelWithCorners 𝕜 E H) [Nonempty M] [LocallyCompactSpace M] : FiniteDimensional 𝕜 E := by have := LocallyCompactSpace.of_locallyCompact_manifold M I exact FiniteDimensional.of_locallyCompactSpace 𝕜 end Topology
.lake/packages/mathlib/Mathlib/Geometry/Manifold/IsManifold/Basic.lean
import Mathlib.Analysis.Calculus.ContDiff.Operations import Mathlib.Analysis.Normed.Module.Convex import Mathlib.Analysis.RCLike.TangentCone import Mathlib.Data.Bundle import Mathlib.Geometry.Manifold.ChartedSpace /-! # `C^n` manifolds (possibly with boundary or corners) A `C^n` manifold is a manifold modelled on a normed vector space, or a subset like a half-space (to get manifolds with boundaries) for which the changes of coordinates are `C^n` maps. We define a model with corners as a map `I : H → E` embedding nicely the topological space `H` in the vector space `E` (or more precisely as a structure containing all the relevant properties). Given such a model with corners `I` on `(E, H)`, we define the groupoid of local homeomorphisms of `H` which are `C^n` when read in `E` (for any regularity `n : WithTop ℕ∞`). With this groupoid at hand and the general machinery of charted spaces, we thus get the notion of `C^n` manifold with respect to any model with corners `I` on `(E, H)`. Some texts assume manifolds to be Hausdorff and second countable. We (in mathlib) assume neither, but add these assumptions later as needed. (Quite a few results still do not require them.) ## Main definitions * `ModelWithCorners 𝕜 E H` : a structure containing information on the way a space `H` embeds in a model vector space E over the field `𝕜`. This is all that is needed to define a `C^n` manifold with model space `H`, and model vector space `E`. * `modelWithCornersSelf 𝕜 E` : trivial model with corners structure on the space `E` embedded in itself by the identity. * `contDiffGroupoid n I` : when `I` is a model with corners on `(𝕜, E, H)`, this is the groupoid of partial homeos of `H` which are of class `C^n` over the normed field `𝕜`, when read in `E`. * `IsManifold I n M` : a type class saying that the charted space `M`, modelled on the space `H`, has `C^n` changes of coordinates with respect to the model with corners `I` on `(𝕜, E, H)`. This type class is just a shortcut for `HasGroupoid M (contDiffGroupoid n I)`. We define a few constructions of smooth manifolds: * every empty type is a smooth manifold * `IsManifold.of_discreteTopology`: a discrete space is a smooth manifold (over the trivial model with corners on the trivial space) * the product of two smooth manifolds * the disjoint union of two manifolds (over the same charted space) As specific examples of models with corners, we define (in `Geometry.Manifold.Instances.Real`) * `modelWithCornersSelf n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanSpace n)` for the model space used to define `n`-dimensional real manifolds without boundary (with notation `𝓡 n` in the scope `Manifold`) * `modelWithCornersEuclideanHalfSpace n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanHalfSpace n)` for the model space used to define `n`-dimensional real manifolds with boundary (with notation `𝓡∂ n` in the locale `Manifold`) * `modelWithCornersEuclideanQuadrant n : ModelWithCorners ℝ (EuclideanSpace ℝ (Fin n)) (EuclideanQuadrant n)` for the model space used to define `n`-dimensional real manifolds with corners With these definitions at hand, to invoke an `n`-dimensional `C^∞` real manifold without boundary, one could use `variable {n : ℕ} {M : Type*} [TopologicalSpace M] [ChartedSpace (EuclideanSpace ℝ (Fin n)) M] [IsManifold (𝓡 n) ∞ M]`. However, this is not the recommended way: a theorem proved using this assumption would not apply for instance to the tangent space of such a manifold, which is modelled on `(EuclideanSpace ℝ (Fin n)) × (EuclideanSpace ℝ (Fin n))` and not on `EuclideanSpace ℝ (Fin (2 * n))`! In the same way, it would not apply to product manifolds, modelled on `(EuclideanSpace ℝ (Fin n)) × (EuclideanSpace ℝ (Fin m))`. The right invocation does not focus on one specific construction, but on all constructions sharing the right properties, like `variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [FiniteDimensional ℝ E] {I : ModelWithCorners ℝ E E} [I.Boundaryless] {M : Type*} [TopologicalSpace M] [ChartedSpace E M] [IsManifold I ∞ M]` Here, `I.Boundaryless` is a typeclass property ensuring that there is no boundary (this is for instance the case for `modelWithCornersSelf`, or products of these). Note that one could consider as a natural assumption to only use the trivial model with corners `modelWithCornersSelf ℝ E`, but again in product manifolds the natural model with corners will not be this one but the product one (and they are not defeq as `(fun p : E × F ↦ (p.1, p.2))` is not defeq to the identity). So, it is important to use the above incantation to maximize the applicability of theorems. Even better, if the result should apply in a parallel way to smooth manifolds and to analytic manifolds, the last typeclass should be replaced with `[IsManifold I n M]` for `n : WithTop ℕ∞`. We also define `TangentSpace I (x : M)` as a type synonym of `E`, and `TangentBundle I M` as a type synonym for `Π (x : M), TangentSpace I x` (in the form of an abbrev of `Bundle.TotalSpace E (TangentSpace I : M → Type _)`). Apart from basic typeclasses on `TangentSpace I x`, nothing is proved about them in this file, but it is useful to have them available as definitions early on to get a clean import structure below. The smooth bundle structure is defined in `VectorBundle.Tangent`, while the definition is used to talk about manifold derivatives in `MFDeriv.Basic`, and neither file needs import the other. ## Implementation notes We want to talk about manifolds modelled on a vector space, but also on manifolds with boundary, modelled on a half space (or even manifolds with corners). For the latter examples, we still want to define smooth functions, tangent bundles, and so on. As smooth functions are well defined on vector spaces or subsets of these, one could take for model space a subtype of a vector space. With the drawback that the whole vector space itself (which is the most basic example) is not directly a subtype of itself: the inclusion of `univ : Set E` in `Set E` would show up in the definition, instead of `id`. A good abstraction covering both cases it to have a vector space `E` (with basic example the Euclidean space), a model space `H` (with basic example the upper half space), and an embedding of `H` into `E` (which can be the identity for `H = E`, or `Subtype.val` for manifolds with corners). We say that the pair `(E, H)` with their embedding is a model with corners, and we encompass all the relevant properties (in particular the fact that the image of `H` in `E` should have unique differentials) in the definition of `ModelWithCorners`. I have considered using the model with corners `I` as a typeclass argument, possibly `outParam`, to get lighter notations later on, but it did not turn out right, as on `E × F` there are two natural model with corners, the trivial (identity) one, and the product one, and they are not defeq and one needs to indicate to Lean which one we want to use. This means that when talking on objects on manifolds one will most often need to specify the model with corners one is using. For instance, the tangent bundle will be `TangentBundle I M` and the derivative will be `mfderiv I I' f`, instead of the more natural notations `TangentBundle 𝕜 M` and `mfderiv 𝕜 f` (the field has to be explicit anyway, as some manifolds could be considered both as real and complex manifolds). -/ open Topology noncomputable section universe u v w u' v' w' namespace PartialEquiv /- This lemma is here in this file, because in `PartialEquiv.basic` it would have required to import some topology, and it did not look right. -/ @[fun_prop] lemma Continuous.invFun {α β : Type*} [TopologicalSpace α] [TopologicalSpace β] (e : PartialEquiv α β) (he : Continuous e.symm) : Continuous e.invFun := he end PartialEquiv open Set Filter Function PartialEquiv open scoped Manifold Topology ContDiff /-! ### Models with corners. -/ open scoped Classical in /-- A structure containing information on the way a space `H` embeds in a model vector space `E` over the field `𝕜`. This is all what is needed to define a `C^n` manifold with model space `H`, and model vector space `E`. We require that, when the field is `ℝ` or `ℂ`, the range is `ℝ`-convex, as this is what is needed to do calculus and covers the standard examples of manifolds with boundary. Over other fields, we require that the range is `univ`, as there is no relevant notion of manifold with boundary there. -/ @[ext] structure ModelWithCorners (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] (H : Type*) [TopologicalSpace H] extends PartialEquiv H E where source_eq : source = univ /-- To check this condition when the space already has a real normed space structure, use `Convex.convex_isRCLikeNormedField` which eliminates the `letI`s below, or the constructor `ModelWithCorners.of_convex_range` -/ convex_range' : if h : IsRCLikeNormedField 𝕜 then letI := h.rclike 𝕜 letI : NormedSpace ℝ E := NormedSpace.restrictScalars ℝ 𝕜 E Convex ℝ (range toPartialEquiv) else range toPartialEquiv = univ nonempty_interior' : (interior (range toPartialEquiv)).Nonempty continuous_toFun : Continuous toFun := by fun_prop continuous_invFun : Continuous invFun := by fun_prop lemma ModelWithCorners.range_eq_target {𝕜 E H : Type*} [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) : range I.toPartialEquiv = I.target := by rw [← I.image_source_eq_target, I.source_eq, image_univ.symm] /-- If a model with corners has full range, the `convex_range'` condition is satisfied. -/ def ModelWithCorners.of_target_univ (𝕜 : Type*) [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (φ : PartialEquiv H E) (hsource : φ.source = univ) (htarget : φ.target = univ) (hcont : Continuous φ) (hcont_inv : Continuous φ.symm) : ModelWithCorners 𝕜 E H where toPartialEquiv := φ source_eq := hsource convex_range' := by have : range φ = φ.target := by rw [← φ.image_source_eq_target, hsource, image_univ.symm] simp only [this, htarget, dite_else_true] intro h letI := h.rclike 𝕜 letI := NormedSpace.restrictScalars ℝ 𝕜 E exact convex_univ nonempty_interior' := by have : range φ = φ.target := by rw [← φ.image_source_eq_target, hsource, image_univ.symm] simp [this, htarget] attribute [simp, mfld_simps] ModelWithCorners.source_eq /-- A vector space is a model with corners, denoted as `𝓘(𝕜, E)` within the `Manifold` namespace. -/ def modelWithCornersSelf (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] : ModelWithCorners 𝕜 E E := ModelWithCorners.of_target_univ 𝕜 (PartialEquiv.refl E) rfl rfl continuous_id continuous_id @[inherit_doc] scoped[Manifold] notation "𝓘(" 𝕜 ", " E ")" => modelWithCornersSelf 𝕜 E /-- A normed field is a model with corners. -/ scoped[Manifold] notation "𝓘(" 𝕜 ")" => modelWithCornersSelf 𝕜 𝕜 section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) namespace ModelWithCorners /-- Coercion of a model with corners to a function. We don't use `e.toFun` because it is actually `e.toPartialEquiv.toFun`, so `simp` will apply lemmas about `toPartialEquiv`. While we may want to switch to this behavior later, doing it mid-port will break a lot of proofs. -/ @[coe] def toFun' (e : ModelWithCorners 𝕜 E H) : H → E := e.toFun instance : CoeFun (ModelWithCorners 𝕜 E H) fun _ => H → E := ⟨toFun'⟩ /-- The inverse to a model with corners, only registered as a `PartialEquiv`. -/ protected def symm : PartialEquiv E H := I.toPartialEquiv.symm /-- See Note [custom simps projection]. We need to specify this projection explicitly in this case, because it is a composition of multiple projections. -/ def Simps.apply (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] (H : Type*) [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) : H → E := I /-- See Note [custom simps projection] -/ def Simps.symm_apply (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] (H : Type*) [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) : E → H := I.symm initialize_simps_projections ModelWithCorners (toFun → apply, invFun → symm_apply) -- Register a few lemmas to make sure that `simp` puts expressions in normal form @[simp, mfld_simps] theorem toPartialEquiv_coe : (I.toPartialEquiv : H → E) = I := rfl @[simp, mfld_simps] theorem mk_coe (e : PartialEquiv H E) (a b c d d') : ((ModelWithCorners.mk e a b c d d' : ModelWithCorners 𝕜 E H) : H → E) = (e : H → E) := rfl @[simp, mfld_simps] theorem toPartialEquiv_coe_symm : (I.toPartialEquiv.symm : E → H) = I.symm := rfl @[simp, mfld_simps] theorem mk_symm (e : PartialEquiv H E) (a b c d d') : (ModelWithCorners.mk e a b c d d' : ModelWithCorners 𝕜 E H).symm = e.symm := rfl @[fun_prop] protected theorem continuous : Continuous I := I.continuous_toFun protected theorem continuousAt {x} : ContinuousAt I x := I.continuous.continuousAt protected theorem continuousWithinAt {s x} : ContinuousWithinAt I s x := I.continuousAt.continuousWithinAt @[fun_prop] theorem continuous_symm : Continuous I.symm := I.continuous_invFun theorem continuousAt_symm {x} : ContinuousAt I.symm x := I.continuous_symm.continuousAt theorem continuousWithinAt_symm {s x} : ContinuousWithinAt I.symm s x := I.continuous_symm.continuousWithinAt theorem continuousOn_symm {s} : ContinuousOn I.symm s := I.continuous_symm.continuousOn @[simp, mfld_simps] theorem target_eq : I.target = range (I : H → E) := by rw [← image_univ, ← I.source_eq] exact I.image_source_eq_target.symm theorem nonempty_interior : (interior (range I)).Nonempty := I.nonempty_interior' theorem range_eq_univ_of_not_isRCLikeNormedField (h : ¬ IsRCLikeNormedField 𝕜) : range I = univ := by simpa [h] using I.convex_range' /-- If a set is `ℝ`-convex for some normed space structure, then it is `ℝ`-convex for the normed space structure coming from an `IsRCLikeNormedField 𝕜`. Useful when constructing model spaces to avoid diamond issues when populating the field `convex_range'`. -/ lemma _root_.Convex.convex_isRCLikeNormedField [NormedSpace ℝ E] [h : IsRCLikeNormedField 𝕜] {s : Set E} (hs : Convex ℝ s) : letI := h.rclike letI := NormedSpace.restrictScalars ℝ 𝕜 E Convex ℝ s := by letI := h.rclike letI := NormedSpace.restrictScalars ℝ 𝕜 E simp only [Convex, StarConvex] at hs ⊢ intro u hu v hv a b ha hb hab convert hs hu hv ha hb hab using 2 · rw [← @algebraMap_smul (R := ℝ) (A := 𝕜), ← @algebraMap_smul (R := ℝ) (A := 𝕜)] · rw [← @algebraMap_smul (R := ℝ) (A := 𝕜), ← @algebraMap_smul (R := ℝ) (A := 𝕜)] /-- Construct a model with corners over `ℝ` from a continuous partial equiv with convex range. -/ def of_convex_range {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type*} [TopologicalSpace H] (φ : PartialEquiv H E) (hsource : φ.source = univ) (htarget : Convex ℝ φ.target) (hcont : Continuous φ) (hcont_inv : Continuous φ.symm) (hint : (interior φ.target).Nonempty) : ModelWithCorners ℝ E H where toPartialEquiv := φ source_eq := hsource convex_range' := by have : range φ = φ.target := by rw [← φ.image_source_eq_target, hsource, image_univ.symm] simp only [instIsRCLikeNormedField, ↓reduceDIte, this] exact htarget.convex_isRCLikeNormedField nonempty_interior' := by have : range φ = φ.target := by rw [← φ.image_source_eq_target, hsource, image_univ.symm] simp [this, hint] theorem convex_range [NormedSpace ℝ E] : Convex ℝ (range I) := by by_cases h : IsRCLikeNormedField 𝕜 · letI : RCLike 𝕜 := h.rclike have W := I.convex_range' simp only [h, ↓reduceDIte, toPartialEquiv_coe] at W simp only [Convex, StarConvex] at W ⊢ intro u hu v hv a b ha hb hab convert W hu hv ha hb hab using 2 · rw [← @algebraMap_smul (R := ℝ) (A := 𝕜)] rfl · rw [← @algebraMap_smul (R := ℝ) (A := 𝕜)] rfl · simp [range_eq_univ_of_not_isRCLikeNormedField I h, convex_univ] protected theorem uniqueDiffOn : UniqueDiffOn 𝕜 (range I) := by by_cases h : IsRCLikeNormedField 𝕜 · letI := h.rclike 𝕜 letI := NormedSpace.restrictScalars ℝ 𝕜 E apply uniqueDiffOn_convex_of_isRCLikeNormedField _ I.nonempty_interior simpa [h] using I.convex_range · simp [range_eq_univ_of_not_isRCLikeNormedField I h, uniqueDiffOn_univ] theorem range_subset_closure_interior : range I ⊆ closure (interior (range I)) := by by_cases h : IsRCLikeNormedField 𝕜 · letI := h.rclike 𝕜 letI := NormedSpace.restrictScalars ℝ 𝕜 E rw [Convex.closure_interior_eq_closure_of_nonempty_interior (𝕜 := ℝ)] · apply subset_closure · apply I.convex_range · apply I.nonempty_interior · simp [range_eq_univ_of_not_isRCLikeNormedField I h] @[simp, mfld_simps] protected theorem left_inv (x : H) : I.symm (I x) = x := by refine I.left_inv' ?_; simp protected theorem leftInverse : LeftInverse I.symm I := I.left_inv theorem injective : Injective I := I.leftInverse.injective @[simp, mfld_simps] theorem symm_comp_self : I.symm ∘ I = id := I.leftInverse.comp_eq_id protected theorem rightInvOn : RightInvOn I.symm I (range I) := I.leftInverse.rightInvOn_range @[simp, mfld_simps] protected theorem right_inv {x : E} (hx : x ∈ range I) : I (I.symm x) = x := I.rightInvOn hx theorem preimage_image (s : Set H) : I ⁻¹' (I '' s) = s := I.injective.preimage_image s protected theorem image_eq (s : Set H) : I '' s = I.symm ⁻¹' s ∩ range I := by refine (I.toPartialEquiv.image_eq_target_inter_inv_preimage ?_).trans ?_ · rw [I.source_eq]; exact subset_univ _ · rw [inter_comm, I.target_eq, I.toPartialEquiv_coe_symm] theorem isClosedEmbedding : IsClosedEmbedding I := I.leftInverse.isClosedEmbedding I.continuous_symm I.continuous theorem isClosed_range : IsClosed (range I) := I.isClosedEmbedding.isClosed_range theorem range_eq_closure_interior : range I = closure (interior (range I)) := Subset.antisymm I.range_subset_closure_interior I.isClosed_range.closure_interior_subset theorem map_nhds_eq (x : H) : map I (𝓝 x) = 𝓝[range I] I x := I.isClosedEmbedding.isEmbedding.map_nhds_eq x theorem map_nhdsWithin_eq (s : Set H) (x : H) : map I (𝓝[s] x) = 𝓝[I '' s] I x := I.isClosedEmbedding.isEmbedding.map_nhdsWithin_eq s x theorem image_mem_nhdsWithin {x : H} {s : Set H} (hs : s ∈ 𝓝 x) : I '' s ∈ 𝓝[range I] I x := I.map_nhds_eq x ▸ image_mem_map hs theorem symm_map_nhdsWithin_image {x : H} {s : Set H} : map I.symm (𝓝[I '' s] I x) = 𝓝[s] x := by rw [← I.map_nhdsWithin_eq, map_map, I.symm_comp_self, map_id] theorem symm_map_nhdsWithin_range (x : H) : map I.symm (𝓝[range I] I x) = 𝓝 x := by rw [← I.map_nhds_eq, map_map, I.symm_comp_self, map_id] theorem uniqueDiffOn_preimage {s : Set H} (hs : IsOpen s) : UniqueDiffOn 𝕜 (I.symm ⁻¹' s ∩ range I) := by rw [inter_comm] exact I.uniqueDiffOn.inter (hs.preimage I.continuous_invFun) theorem uniqueDiffOn_preimage_source {β : Type*} [TopologicalSpace β] {e : OpenPartialHomeomorph H β} : UniqueDiffOn 𝕜 (I.symm ⁻¹' e.source ∩ range I) := I.uniqueDiffOn_preimage e.open_source theorem uniqueDiffWithinAt_image {x : H} : UniqueDiffWithinAt 𝕜 (range I) (I x) := I.uniqueDiffOn _ (mem_range_self _) theorem symm_continuousWithinAt_comp_right_iff {X} [TopologicalSpace X] {f : H → X} {s : Set H} {x : H} : ContinuousWithinAt (f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) (I x) ↔ ContinuousWithinAt f s x := by refine ⟨fun h => ?_, fun h => ?_⟩ · have := h.comp I.continuousWithinAt (mapsTo_preimage _ _) simp_rw [preimage_inter, preimage_preimage, I.left_inv, preimage_id', preimage_range, inter_univ] at this rwa [Function.comp_assoc, I.symm_comp_self] at this · rw [← I.left_inv x] at h; exact h.comp I.continuousWithinAt_symm inter_subset_left protected theorem locallyCompactSpace [LocallyCompactSpace E] (I : ModelWithCorners 𝕜 E H) : LocallyCompactSpace H := by have : ∀ x : H, (𝓝 x).HasBasis (fun s => s ∈ 𝓝 (I x) ∧ IsCompact s) fun s => I.symm '' (s ∩ range I) := fun x ↦ by rw [← I.symm_map_nhdsWithin_range] exact ((compact_basis_nhds (I x)).inf_principal _).map _ refine .of_hasBasis this ?_ rintro x s ⟨-, hsc⟩ exact (hsc.inter_right I.isClosed_range).image I.continuous_symm open TopologicalSpace protected theorem secondCountableTopology [SecondCountableTopology E] (I : ModelWithCorners 𝕜 E H) : SecondCountableTopology H := I.isClosedEmbedding.isEmbedding.secondCountableTopology include I in /-- Every manifold is a Fréchet space (T1 space) -- regardless of whether it is Hausdorff. -/ protected theorem t1Space (M : Type*) [TopologicalSpace M] [ChartedSpace H M] : T1Space M := by have : T2Space H := I.isClosedEmbedding.toIsEmbedding.t2Space exact ChartedSpace.t1Space H M end ModelWithCorners section variable (𝕜 E) /-- In the trivial model with corners, the associated `PartialEquiv` is the identity. -/ @[simp, mfld_simps] theorem modelWithCornersSelf_partialEquiv : 𝓘(𝕜, E).toPartialEquiv = PartialEquiv.refl E := rfl @[simp, mfld_simps] theorem modelWithCornersSelf_coe : (𝓘(𝕜, E) : E → E) = id := rfl @[simp, mfld_simps] theorem modelWithCornersSelf_coe_symm : (𝓘(𝕜, E).symm : E → E) = id := rfl end end section ModelWithCornersProd /-- Given two model_with_corners `I` on `(E, H)` and `I'` on `(E', H')`, we define the model with corners `I.prod I'` on `(E × E', ModelProd H H')`. This appears in particular for the manifold structure on the tangent bundle to a manifold modelled on `(E, H)`: it will be modelled on `(E × E, H × E)`. See note [Manifold type tags] for explanation about `ModelProd H H'` vs `H × H'`. -/ @[simps -isSimp] def ModelWithCorners.prod {𝕜 : Type u} [NontriviallyNormedField 𝕜] {E : Type v} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type w} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {E' : Type v'} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type w'} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 E' H') : ModelWithCorners 𝕜 (E × E') (ModelProd H H') := { I.toPartialEquiv.prod I'.toPartialEquiv with toFun := fun x => (I x.1, I' x.2) invFun := fun x => (I.symm x.1, I'.symm x.2) source := { x | x.1 ∈ I.source ∧ x.2 ∈ I'.source } source_eq := by simp only [setOf_true, mfld_simps] convex_range' := by have : range (fun (x : ModelProd H H') ↦ (I x.1, I' x.2)) = range (Prod.map I I') := rfl rw [this, Set.range_prodMap] split_ifs with h · letI := h.rclike letI := NormedSpace.restrictScalars ℝ 𝕜 E; letI := NormedSpace.restrictScalars ℝ 𝕜 E' exact I.convex_range.prod I'.convex_range · simp [range_eq_univ_of_not_isRCLikeNormedField, h] nonempty_interior' := by have : range (fun (x : ModelProd H H') ↦ (I x.1, I' x.2)) = range (Prod.map I I') := rfl simp [this, interior_prod_eq, nonempty_interior] continuous_toFun := I.continuous_toFun.prodMap I'.continuous_toFun continuous_invFun := I.continuous_invFun.prodMap I'.continuous_invFun } /-- Given a finite family of `ModelWithCorners` `I i` on `(E i, H i)`, we define the model with corners `pi I` on `(Π i, E i, ModelPi H)`. See note [Manifold type tags] for explanation about `ModelPi H`. -/ def ModelWithCorners.pi {𝕜 : Type u} [NontriviallyNormedField 𝕜] {ι : Type v} [Fintype ι] {E : ι → Type w} [∀ i, NormedAddCommGroup (E i)] [∀ i, NormedSpace 𝕜 (E i)] {H : ι → Type u'} [∀ i, TopologicalSpace (H i)] (I : ∀ i, ModelWithCorners 𝕜 (E i) (H i)) : ModelWithCorners 𝕜 (∀ i, E i) (ModelPi H) where toPartialEquiv := PartialEquiv.pi fun i => (I i).toPartialEquiv source_eq := by simp only [pi_univ, mfld_simps] convex_range' := by rw [PartialEquiv.pi_apply, Set.range_piMap] split_ifs with h · letI := h.rclike letI := fun i ↦ NormedSpace.restrictScalars ℝ 𝕜 (E i) exact convex_pi fun i _hi ↦ (I i).convex_range · simp [range_eq_univ_of_not_isRCLikeNormedField, h] nonempty_interior' := by rw [PartialEquiv.pi_apply, Set.range_piMap] simp [interior_pi_set finite_univ, univ_pi_nonempty_iff, nonempty_interior] continuous_toFun := continuous_pi fun i => (I i).continuous.comp (continuous_apply i) continuous_invFun := continuous_pi fun i => (I i).continuous_symm.comp (continuous_apply i) /-- Special case of product model with corners, which is trivial on the second factor. This shows up as the model to tangent bundles. -/ abbrev ModelWithCorners.tangent {𝕜 : Type u} [NontriviallyNormedField 𝕜] {E : Type v} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type w} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) : ModelWithCorners 𝕜 (E × E) (ModelProd H E) := I.prod 𝓘(𝕜, E) variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] {H : Type*} [TopologicalSpace H] {H' : Type*} [TopologicalSpace H'] {G : Type*} [TopologicalSpace G] {I : ModelWithCorners 𝕜 E H} {J : ModelWithCorners 𝕜 F G} @[simp, mfld_simps] theorem modelWithCorners_prod_toPartialEquiv : (I.prod J).toPartialEquiv = I.toPartialEquiv.prod J.toPartialEquiv := rfl @[simp, mfld_simps] theorem modelWithCorners_prod_coe (I : ModelWithCorners 𝕜 E H) (I' : ModelWithCorners 𝕜 E' H') : (I.prod I' : _ × _ → _ × _) = Prod.map I I' := rfl @[simp, mfld_simps] theorem modelWithCorners_prod_coe_symm (I : ModelWithCorners 𝕜 E H) (I' : ModelWithCorners 𝕜 E' H') : ((I.prod I').symm : _ × _ → _ × _) = Prod.map I.symm I'.symm := rfl /-- This lemma should be erased, or at least burn in hell, as it uses bad defeq: the left model with corners is for `E times F`, the right one for `ModelProd E F`, and there's a good reason we are distinguishing them. -/ theorem modelWithCornersSelf_prod : 𝓘(𝕜, E × F) = 𝓘(𝕜, E).prod 𝓘(𝕜, F) := by ext1 <;> simp theorem ModelWithCorners.range_prod : range (I.prod J) = range I ×ˢ range J := by simp_rw [← ModelWithCorners.target_eq]; rfl end ModelWithCornersProd section Boundaryless /-- Property ensuring that the model with corners `I` defines manifolds without boundary. This differs from the more general `BoundarylessManifold`, which requires every point on the manifold to be an interior point. -/ class ModelWithCorners.Boundaryless {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) : Prop where range_eq_univ : range I = univ theorem ModelWithCorners.range_eq_univ {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) [I.Boundaryless] : range I = univ := ModelWithCorners.Boundaryless.range_eq_univ /-- If `I` is a `ModelWithCorners.Boundaryless` model, then it is a homeomorphism. -/ @[simps +simpRhs] def ModelWithCorners.toHomeomorph {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) [I.Boundaryless] : H ≃ₜ E where __ := I left_inv := I.left_inv right_inv _ := I.right_inv <| I.range_eq_univ.symm ▸ mem_univ _ /-- The trivial model with corners has no boundary -/ instance modelWithCornersSelf_boundaryless (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] : (modelWithCornersSelf 𝕜 E).Boundaryless := ⟨by simp⟩ /-- If two model with corners are boundaryless, their product also is -/ instance ModelWithCorners.range_eq_univ_prod {𝕜 : Type u} [NontriviallyNormedField 𝕜] {E : Type v} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type w} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) [I.Boundaryless] {E' : Type v'} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type w'} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 E' H') [I'.Boundaryless] : (I.prod I').Boundaryless := by constructor dsimp [ModelWithCorners.prod, ModelProd] rw [← prod_range_range_eq, ModelWithCorners.Boundaryless.range_eq_univ, ModelWithCorners.Boundaryless.range_eq_univ, univ_prod_univ] end Boundaryless section contDiffGroupoid /-! ### `C^n` functions on models with corners -/ variable {m n : WithTop ℕ∞} {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {M : Type*} [TopologicalSpace M] variable (n I) in /-- Given a model with corners `(E, H)`, we define the pregroupoid of `C^n` transformations of `H` as the maps that are `C^n` when read in `E` through `I`. -/ def contDiffPregroupoid : Pregroupoid H where property f s := ContDiffOn 𝕜 n (I ∘ f ∘ I.symm) (I.symm ⁻¹' s ∩ range I) comp {f g u v} hf hg _ _ _ := by have : I ∘ (g ∘ f) ∘ I.symm = (I ∘ g ∘ I.symm) ∘ I ∘ f ∘ I.symm := by ext x; simp simp only [this] refine hg.comp (hf.mono fun x ⟨hx1, hx2⟩ ↦ ⟨hx1.1, hx2⟩) ?_ rintro x ⟨hx1, _⟩ simp only [mfld_simps] at hx1 ⊢ exact hx1.2 id_mem := by apply ContDiffOn.congr contDiff_id.contDiffOn rintro x ⟨_, hx2⟩ rcases mem_range.1 hx2 with ⟨y, hy⟩ rw [← hy] simp only [mfld_simps] locality {f u} _ H := by apply contDiffOn_of_locally_contDiffOn rintro y ⟨hy1, hy2⟩ rcases mem_range.1 hy2 with ⟨x, hx⟩ rw [← hx] at hy1 ⊢ simp only [mfld_simps] at hy1 ⊢ rcases H x hy1 with ⟨v, v_open, xv, hv⟩ have : I.symm ⁻¹' (u ∩ v) ∩ range I = I.symm ⁻¹' u ∩ range I ∩ I.symm ⁻¹' v := by rw [preimage_inter, inter_assoc, inter_assoc] congr 1 rw [inter_comm] rw [this] at hv exact ⟨I.symm ⁻¹' v, v_open.preimage I.continuous_symm, by simpa, hv⟩ congr {f g u} _ fg hf := by apply hf.congr rintro y ⟨hy1, hy2⟩ rcases mem_range.1 hy2 with ⟨x, hx⟩ rw [← hx] at hy1 ⊢ simp only [mfld_simps] at hy1 ⊢ rw [fg _ hy1] variable (n I) in /-- Given a model with corners `(E, H)`, we define the groupoid of invertible `C^n` transformations of `H` as the invertible maps that are `C^n` when read in `E` through `I`. -/ def contDiffGroupoid : StructureGroupoid H := Pregroupoid.groupoid (contDiffPregroupoid n I) /-- Inclusion of the groupoid of `C^n` local diffeos in the groupoid of `C^m` local diffeos when `m ≤ n` -/ theorem contDiffGroupoid_le (h : m ≤ n) : contDiffGroupoid n I ≤ contDiffGroupoid m I := by rw [contDiffGroupoid, contDiffGroupoid] apply groupoid_of_pregroupoid_le intro f s hfs exact ContDiffOn.of_le hfs h /-- The groupoid of `0`-times continuously differentiable maps is just the groupoid of all open partial homeomorphisms -/ theorem contDiffGroupoid_zero_eq : contDiffGroupoid 0 I = continuousGroupoid H := by apply le_antisymm le_top intro u _ -- we have to check that every open partial homeomorphism belongs to `contDiffGroupoid 0 I`, -- by unfolding its definition change u ∈ contDiffGroupoid 0 I rw [contDiffGroupoid, mem_groupoid_of_pregroupoid, contDiffPregroupoid] simp only [contDiffOn_zero] constructor · refine I.continuous.comp_continuousOn (u.continuousOn.comp I.continuousOn_symm ?_) exact (mapsTo_preimage _ _).mono_left inter_subset_left · refine I.continuous.comp_continuousOn (u.symm.continuousOn.comp I.continuousOn_symm ?_) exact (mapsTo_preimage _ _).mono_left inter_subset_left -- FIXME: does this generalise to other groupoids? The argument is not specific -- to C^n functions, but uses something about the groupoid's property that is not easy to abstract. /-- Any change of coordinates with empty source belongs to `contDiffGroupoid`. -/ lemma ContDiffGroupoid.mem_of_source_eq_empty (f : OpenPartialHomeomorph H H) (hf : f.source = ∅) : f ∈ contDiffGroupoid n I := by constructor · intro x ⟨hx, _⟩ rw [mem_preimage] at hx simp_all only [mem_empty_iff_false] · intro x ⟨hx, _⟩ have : f.target = ∅ := by simp [← f.image_source_eq_target, hf] simp_all include I in /-- Any change of coordinates with empty source belongs to `continuousGroupoid`. -/ lemma ContinuousGroupoid.mem_of_source_eq_empty (f : OpenPartialHomeomorph H H) (hf : f.source = ∅) : f ∈ continuousGroupoid H := by rw [← contDiffGroupoid_zero_eq (I := I)] exact ContDiffGroupoid.mem_of_source_eq_empty f hf /-- An identity open partial homeomorphism belongs to the `C^n` groupoid. -/ theorem ofSet_mem_contDiffGroupoid {s : Set H} (hs : IsOpen s) : OpenPartialHomeomorph.ofSet s hs ∈ contDiffGroupoid n I := by rw [contDiffGroupoid, mem_groupoid_of_pregroupoid] suffices h : ContDiffOn 𝕜 n (I ∘ I.symm) (I.symm ⁻¹' s ∩ range I) by simp [h, contDiffPregroupoid] have : ContDiffOn 𝕜 n id (univ : Set E) := contDiff_id.contDiffOn exact this.congr_mono (fun x hx => I.right_inv hx.2) (subset_univ _) /-- The composition of an open partial homeomorphism from `H` to `M` and its inverse belongs to the `C^n` groupoid. -/ theorem symm_trans_mem_contDiffGroupoid (e : OpenPartialHomeomorph M H) : e.symm.trans e ∈ contDiffGroupoid n I := haveI : e.symm.trans e ≈ OpenPartialHomeomorph.ofSet e.target e.open_target := OpenPartialHomeomorph.symm_trans_self _ StructureGroupoid.mem_of_eqOnSource _ (ofSet_mem_contDiffGroupoid e.open_target) this variable {E' H' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] [TopologicalSpace H'] /-- The product of two `C^n` open partial homeomorphisms is `C^n`. -/ theorem contDiffGroupoid_prod {I : ModelWithCorners 𝕜 E H} {I' : ModelWithCorners 𝕜 E' H'} {e : OpenPartialHomeomorph H H} {e' : OpenPartialHomeomorph H' H'} (he : e ∈ contDiffGroupoid n I) (he' : e' ∈ contDiffGroupoid n I') : e.prod e' ∈ contDiffGroupoid n (I.prod I') := by obtain ⟨he, he_symm⟩ := he obtain ⟨he', he'_symm⟩ := he' constructor <;> simp only [PartialEquiv.prod_source, OpenPartialHomeomorph.prod_toPartialEquiv, contDiffPregroupoid] · have h3 := ContDiffOn.prodMap he he' rw [← I.image_eq, ← I'.image_eq, prod_image_image_eq] at h3 rw [← (I.prod I').image_eq] exact h3 · have h3 := ContDiffOn.prodMap he_symm he'_symm rw [← I.image_eq, ← I'.image_eq, prod_image_image_eq] at h3 rw [← (I.prod I').image_eq] exact h3 /-- The `C^n` groupoid is closed under restriction. -/ instance : ClosedUnderRestriction (contDiffGroupoid n I) := (closedUnderRestriction_iff_id_le _).mpr (by rw [StructureGroupoid.le_iff] rintro e ⟨s, hs, hes⟩ apply (contDiffGroupoid n I).mem_of_eqOnSource' _ _ _ hes exact ofSet_mem_contDiffGroupoid hs) end contDiffGroupoid section IsManifold /-! ### `C^n` manifolds (possibly with boundary or corners) -/ /-- Typeclass defining manifolds with respect to a model with corners, over a field `𝕜`. This definition includes the model with corners `I` (which might allow boundary, corners, or not, so this class covers both manifolds with boundary and manifolds without boundary), and a smoothness parameter `n : WithTop ℕ∞` (where `n = 0` means topological manifold, `n = ∞` means smooth manifold and `n = ω` means analytic manifold). -/ class IsManifold {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (M : Type*) [TopologicalSpace M] [ChartedSpace H M] : Prop extends HasGroupoid M (contDiffGroupoid n I) /-- Building a `C^n` manifold from a `HasGroupoid` assumption. -/ theorem IsManifold.mk' {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (M : Type*) [TopologicalSpace M] [ChartedSpace H M] [gr : HasGroupoid M (contDiffGroupoid n I)] : IsManifold I n M := { gr with } theorem isManifold_of_contDiffOn {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (M : Type*) [TopologicalSpace M] [ChartedSpace H M] (h : ∀ e e' : OpenPartialHomeomorph M H, e ∈ atlas H M → e' ∈ atlas H M → ContDiffOn 𝕜 n (I ∘ e.symm ≫ₕ e' ∘ I.symm) (I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I)) : IsManifold I n M where compatible := by haveI : HasGroupoid M (contDiffGroupoid n I) := hasGroupoid_of_pregroupoid _ (h _ _) apply StructureGroupoid.compatible /-- For any model with corners, the model space is a `C^n` manifold -/ instance instIsManifoldModelSpace {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} : IsManifold I n H := { hasGroupoid_model_space _ _ with } @[deprecated (since := "2025-04-22")] alias intIsManifoldModelSpace := instIsManifoldModelSpace end IsManifold namespace IsManifold /- We restate in the namespace `IsManifold` some lemmas that hold for general charted space with a structure groupoid, avoiding the need to specify the groupoid `contDiffGroupoid n I` explicitly. -/ variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] protected theorem of_le {m n : WithTop ℕ∞} (hmn : m ≤ n) [IsManifold I n M] : IsManifold I m M := by have : HasGroupoid M (contDiffGroupoid m I) := hasGroupoid_of_le (G₁ := contDiffGroupoid n I) (by infer_instance) (contDiffGroupoid_le hmn) exact mk' I m M /-- A typeclass registering that a smoothness exponent is smaller than `∞`. Used to deduce that some manifolds are `C^n` when they are `C^∞`. -/ class _root_.ENat.LEInfty (m : WithTop ℕ∞) where out : m ≤ ∞ open ENat instance (n : ℕ) : LEInfty (n : WithTop ℕ∞) := ⟨mod_cast le_top⟩ instance (n : ℕ) [n.AtLeastTwo] : LEInfty (no_index (OfNat.ofNat n) : WithTop ℕ∞) := inferInstanceAs (LEInfty (n : WithTop ℕ∞)) instance : LEInfty (1 : WithTop ℕ∞) := inferInstanceAs (LEInfty ((1 : ℕ) : WithTop ℕ∞)) instance : LEInfty (0 : WithTop ℕ∞) := inferInstanceAs (LEInfty ((0 : ℕ) : WithTop ℕ∞)) instance {a : WithTop ℕ∞} [IsManifold I ∞ M] [h : LEInfty a] : IsManifold I a M := IsManifold.of_le h.out instance {a : WithTop ℕ∞} [IsManifold I ω M] : IsManifold I a M := IsManifold.of_le le_top instance : IsManifold I 0 M := by suffices HasGroupoid M (contDiffGroupoid 0 I) from mk' I 0 M constructor intro e e' he he' rw [contDiffGroupoid_zero_eq] trivial instance [IsManifold I 2 M] : IsManifold I 1 M := IsManifold.of_le one_le_two instance [IsManifold I 3 M] : IsManifold I 2 M := IsManifold.of_le (n := 3) (by norm_cast) variable (I n M) in /-- The maximal atlas of `M` for the `C^n` manifold with corners structure corresponding to the model with corners `I`. -/ def maximalAtlas := (contDiffGroupoid n I).maximalAtlas M theorem subset_maximalAtlas [IsManifold I n M] : atlas H M ⊆ maximalAtlas I n M := StructureGroupoid.subset_maximalAtlas _ theorem chart_mem_maximalAtlas [IsManifold I n M] (x : M) : chartAt H x ∈ maximalAtlas I n M := StructureGroupoid.chart_mem_maximalAtlas _ x theorem compatible_of_mem_maximalAtlas {e e' : OpenPartialHomeomorph M H} (he : e ∈ maximalAtlas I n M) (he' : e' ∈ maximalAtlas I n M) : e.symm.trans e' ∈ contDiffGroupoid n I := StructureGroupoid.compatible_of_mem_maximalAtlas he he' lemma maximalAtlas_subset_of_le {m n : WithTop ℕ∞} (h : m ≤ n) : maximalAtlas I n M ⊆ maximalAtlas I m M := StructureGroupoid.maximalAtlas_mono (contDiffGroupoid_le h) variable (n) in /-- The empty set is a `C^n` manifold w.r.t. any charted space and model. -/ instance empty [IsEmpty M] : IsManifold I n M := by apply isManifold_of_contDiffOn intro e e' _ _ x hx set t := I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I -- Since `M` is empty, the condition about compatibility of transition maps is vacuous. have : (e.symm ≫ₕ e').source = ∅ := calc (e.symm ≫ₕ e').source _ = (e.symm.source) ∩ e.symm ⁻¹' e'.source := by rw [← OpenPartialHomeomorph.trans_source] _ = (e.symm.source) ∩ e.symm ⁻¹' ∅ := by rw [eq_empty_of_isEmpty (e'.source)] _ = (e.symm.source) ∩ ∅ := by rw [preimage_empty] _ = ∅ := inter_empty e.symm.source have : t = ∅ := calc t _ = I.symm ⁻¹' (e.symm ≫ₕ e').source ∩ range I := by rw [← Subtype.preimage_val_eq_preimage_val_iff] _ = ∅ ∩ range I := by rw [this, preimage_empty] _ = ∅ := empty_inter (range I) apply (this ▸ hx).elim attribute [local instance] ChartedSpace.of_discreteTopology in variable (n) in /-- A discrete space `M` is a smooth manifold over the trivial model on a trivial normed space. -/ theorem of_discreteTopology [DiscreteTopology M] [Unique E] : IsManifold (modelWithCornersSelf 𝕜 E) n M := by apply isManifold_of_contDiffOn _ _ _ (fun _ _ _ _ ↦ contDiff_of_subsingleton.contDiffOn) attribute [local instance] ChartedSpace.of_discreteTopology in example [Unique E] : IsManifold (𝓘(𝕜, E)) n (Fin 2) := of_discreteTopology _ /-- The product of two `C^n` manifolds is naturally a `C^n` manifold. -/ instance prod {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} (M : Type*) [TopologicalSpace M] [ChartedSpace H M] [IsManifold I n M] (M' : Type*) [TopologicalSpace M'] [ChartedSpace H' M'] [IsManifold I' n M'] : IsManifold (I.prod I') n (M × M') where compatible := by rintro f g ⟨f1, hf1, f2, hf2, rfl⟩ ⟨g1, hg1, g2, hg2, rfl⟩ rw [OpenPartialHomeomorph.prod_symm, OpenPartialHomeomorph.prod_trans] have h1 := (contDiffGroupoid n I).compatible hf1 hg1 have h2 := (contDiffGroupoid n I').compatible hf2 hg2 exact contDiffGroupoid_prod h1 h2 section DisjointUnion variable {M' : Type*} [TopologicalSpace M'] [ChartedSpace H M'] [hM : IsManifold I n M] [hM' : IsManifold I n M'] /-- The disjoint union of two `C^n` manifolds modelled on `(E, H)` is a `C^n` manifold modeled on `(E, H)`. -/ instance disjointUnion : IsManifold I n (M ⊕ M') where compatible {e} e' he he' := by obtain (h | h) := isEmpty_or_nonempty H · exact ContDiffGroupoid.mem_of_source_eq_empty _ (eq_empty_of_isEmpty _) obtain (⟨f, hf, hef⟩ | ⟨f, hf, hef⟩) := ChartedSpace.mem_atlas_sum he · obtain (⟨f', hf', he'f'⟩ | ⟨f', hf', he'f'⟩) := ChartedSpace.mem_atlas_sum he' · rw [hef, he'f', f.lift_openEmbedding_trans f' IsOpenEmbedding.inl] exact hM.compatible hf hf' · rw [hef, he'f'] apply ContDiffGroupoid.mem_of_source_eq_empty ext x exact ⟨fun ⟨hx₁, hx₂⟩ ↦ by simp_all, fun hx ↦ hx.elim⟩ · -- Analogous argument to the first case: is there a way to deduplicate? obtain (⟨f', hf', he'f'⟩ | ⟨f', hf', he'f'⟩) := ChartedSpace.mem_atlas_sum he' · rw [hef, he'f'] apply ContDiffGroupoid.mem_of_source_eq_empty ext x exact ⟨fun ⟨hx₁, hx₂⟩ ↦ by simp_all, fun hx ↦ hx.elim⟩ · rw [hef, he'f', f.lift_openEmbedding_trans f' IsOpenEmbedding.inr] exact hM'.compatible hf hf' end DisjointUnion end IsManifold theorem OpenPartialHomeomorph.isManifold_singleton {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {M : Type*} [TopologicalSpace M] (e : OpenPartialHomeomorph M H) (h : e.source = Set.univ) : @IsManifold 𝕜 _ E _ _ H _ I n M _ (e.singletonChartedSpace h) := @IsManifold.mk' _ _ _ _ _ _ _ _ _ _ _ (id _) <| e.singleton_hasGroupoid h (contDiffGroupoid n I) theorem Topology.IsOpenEmbedding.isManifold_singleton {𝕜 E H : Type*} [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {M : Type*} [TopologicalSpace M] [Nonempty M] {f : M → H} (h : IsOpenEmbedding f) : @IsManifold 𝕜 _ E _ _ H _ I n M _ h.singletonChartedSpace := (h.toOpenPartialHomeomorph f).isManifold_singleton (by simp) namespace TopologicalSpace.Opens open TopologicalSpace variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] [IsManifold I n M] (s : Opens M) instance : IsManifold I n s := { s.instHasGroupoid (contDiffGroupoid n I) with } end TopologicalSpace.Opens section TangentSpace /- We define the tangent space to `M` modelled on `I : ModelWithCorners 𝕜 E H` as a type synonym for `E`. This is enough to define linear maps between tangent spaces, for instance derivatives, but the interesting part is to define a manifold structure on the whole tangent bundle, which requires that `M` is a `C^n` manifold. The definition is put here to avoid importing all the smooth bundle structure when defining manifold derivatives. -/ set_option linter.unusedVariables false in /-- The tangent space at a point of the manifold `M`. It is just `E`. We could use instead `(tangentBundleCore I M).toFiberBundleCore.fiber x`, but we use `E` to help the kernel. The definition of `TangentSpace` is not reducible so that type class inference does not pick wrong instances. -/ @[nolint unusedArguments] def TangentSpace {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type u} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {M : Type*} [TopologicalSpace M] [ChartedSpace H M] (_x : M) : Type u := E deriving TopologicalSpace, AddCommGroup, IsTopologicalAddGroup, Module 𝕜, ContinuousSMul 𝕜, -- the following instance derives from the previous one, but through an instance with priority 100 -- which takes a long time to be found. We register a shortcut instance instead ContinuousConstSMul 𝕜 variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) {M : Type*} [TopologicalSpace M] [ChartedSpace H M] {x : M} instance : Inhabited (TangentSpace I x) := ⟨0⟩ variable (M) in -- is empty if the base manifold is empty /-- The tangent bundle to a manifold, as a Sigma type. Defined in terms of `Bundle.TotalSpace` to be able to put a suitable topology on it. -/ abbrev TangentBundle := Bundle.TotalSpace E (TangentSpace I : M → Type _) end TangentSpace section Real variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners ℝ E H} {M : Type*} [TopologicalSpace M] [ChartedSpace H M] {x : M} deriving instance PathConnectedSpace for TangentSpace I x end Real
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Algebra/Monoid.lean
import Mathlib.Geometry.Manifold.ContMDiffMap import Mathlib.Geometry.Manifold.MFDeriv.Basic /-! # `C^n` monoid A `C^n` monoid is a monoid that is also a `C^n` manifold, in which multiplication is a `C^n` map of the product manifold `G` × `G` into `G`. In this file we define the basic structures to talk about `C^n` monoids: `ContMDiffMul` and its additive counterpart `ContMDiffAdd`. These structures are general enough to also talk about `C^n` semigroups. -/ open scoped Manifold ContDiff library_note2 «Design choices about smooth algebraic structures» /-- 1. All `C^n` algebraic structures on `G` are `Prop`-valued classes that extend `IsManifold I n G`. This way we save users from adding both `[IsManifold I n G]` and `[ContMDiffMul I n G]` to the assumptions. While many API lemmas hold true without the `IsManifold I n G` assumption, we're not aware of a mathematically interesting monoid on a topological manifold such that (a) the space is not a `IsManifold`; (b) the multiplication is `C^n` at `(a, b)` in the charts `extChartAt I a`, `extChartAt I b`, `extChartAt I (a * b)`. 2. Because of `ModelProd` we can't assume, e.g., that a `LieGroup` is modelled on `𝓘(𝕜, E)`. So, we formulate the definitions and lemmas for any model. 3. While smoothness of an operation implies its continuity, lemmas like `continuousMul_of_contMDiffMul` can't be instances because otherwise Lean would have to search for `ContMDiffMul I n G` with unknown `𝕜`, `E`, `H`, and `I : ModelWithCorners 𝕜 E H`. If users needs `[ContinuousMul G]` in a proof about a `C^n` monoid, then they need to either add `[ContinuousMul G]` as an assumption (worse) or use `haveI` in the proof (better). -/ -- See note [Design choices about smooth algebraic structures] /-- Basic hypothesis to talk about a `C^n` (Lie) additive monoid or a `C^n` additive semigroup. A `C^n` additive monoid over `G`, for example, is obtained by requiring both the instances `AddMonoid G` and `ContMDiffAdd I n G`. -/ class ContMDiffAdd {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (G : Type*) [Add G] [TopologicalSpace G] [ChartedSpace H G] : Prop extends IsManifold I n G where contMDiff_add : ContMDiff (I.prod I) I n fun p : G × G => p.1 + p.2 -- See note [Design choices about smooth algebraic structures] /-- Basic hypothesis to talk about a `C^n` (Lie) monoid or a `C^n` semigroup. A `C^n` monoid over `G`, for example, is obtained by requiring both the instances `Monoid G` and `ContMDiffMul I n G`. -/ @[to_additive] class ContMDiffMul {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (G : Type*) [Mul G] [TopologicalSpace G] [ChartedSpace H G] : Prop extends IsManifold I n G where contMDiff_mul : ContMDiff (I.prod I) I n fun p : G × G => p.1 * p.2 section ContMDiffMul variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {G : Type*} [Mul G] [TopologicalSpace G] [ChartedSpace H G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] @[to_additive] protected theorem ContMDiffMul.of_le {m n : WithTop ℕ∞} (hmn : m ≤ n) [h : ContMDiffMul I n G] : ContMDiffMul I m G := by have : IsManifold I m G := IsManifold.of_le hmn exact ⟨h.contMDiff_mul.of_le hmn⟩ @[to_additive] instance {a : WithTop ℕ∞} [ContMDiffMul I ∞ G] [h : ENat.LEInfty a] : ContMDiffMul I a G := ContMDiffMul.of_le h.out @[to_additive] instance {a : WithTop ℕ∞} [ContMDiffMul I ω G] : ContMDiffMul I a G := ContMDiffMul.of_le le_top @[to_additive] instance [ContinuousMul G] : ContMDiffMul I 0 G := by constructor rw [contMDiff_zero_iff] exact continuous_mul @[to_additive] instance [ContMDiffMul I 2 G] : ContMDiffMul I 1 G := ContMDiffMul.of_le one_le_two section variable (I n) @[to_additive] theorem contMDiff_mul [ContMDiffMul I n G] : ContMDiff (I.prod I) I n fun p : G × G => p.1 * p.2 := ContMDiffMul.contMDiff_mul include I n in /-- If the multiplication is `C^n`, then it is continuous. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ @[to_additive /-- If the addition is `C^n`, then it is continuous. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/] theorem continuousMul_of_contMDiffMul [ContMDiffMul I n G] : ContinuousMul G := ⟨(contMDiff_mul I n).continuous⟩ end section variable [ContMDiffMul I n G] {f g : M → G} {s : Set M} {x : M} @[to_additive] theorem ContMDiffWithinAt.mul (hf : ContMDiffWithinAt I' I n f s x) (hg : ContMDiffWithinAt I' I n g s x) : ContMDiffWithinAt I' I n (f * g) s x := (contMDiff_mul I n).contMDiffAt.comp_contMDiffWithinAt x (hf.prodMk hg) @[to_additive] nonrec theorem ContMDiffAt.mul (hf : ContMDiffAt I' I n f x) (hg : ContMDiffAt I' I n g x) : ContMDiffAt I' I n (f * g) x := hf.mul hg @[to_additive] theorem ContMDiffOn.mul (hf : ContMDiffOn I' I n f s) (hg : ContMDiffOn I' I n g s) : ContMDiffOn I' I n (f * g) s := fun x hx => (hf x hx).mul (hg x hx) @[to_additive] theorem ContMDiff.mul (hf : ContMDiff I' I n f) (hg : ContMDiff I' I n g) : ContMDiff I' I n (f * g) := fun x => (hf x).mul (hg x) @[to_additive] theorem contMDiff_mul_left {a : G} : ContMDiff I I n (a * ·) := contMDiff_const.mul contMDiff_id @[to_additive] theorem contMDiffAt_mul_left {a b : G} : ContMDiffAt I I n (a * ·) b := contMDiff_mul_left.contMDiffAt @[to_additive] theorem contMDiff_mul_right {a : G} : ContMDiff I I n (· * a) := contMDiff_id.mul contMDiff_const @[to_additive] theorem contMDiffAt_mul_right {a b : G} : ContMDiffAt I I n (· * a) b := contMDiff_mul_right.contMDiffAt end section variable [ContMDiffMul I 1 G] @[to_additive] theorem mdifferentiable_mul_left {a : G} : MDifferentiable I I (a * ·) := contMDiff_mul_left.mdifferentiable le_rfl @[to_additive] theorem mdifferentiableAt_mul_left {a b : G} : MDifferentiableAt I I (a * ·) b := contMDiffAt_mul_left.mdifferentiableAt le_rfl @[to_additive] theorem mdifferentiable_mul_right {a : G} : MDifferentiable I I (· * a) := contMDiff_mul_right.mdifferentiable le_rfl @[to_additive] theorem mdifferentiableAt_mul_right {a b : G} : MDifferentiableAt I I (· * a) b := contMDiffAt_mul_right.mdifferentiableAt le_rfl end variable (I) (g h : G) variable [ContMDiffMul I ∞ G] /-- Left multiplication by `g`. It is meant to mimic the usual notation in Lie groups. Used mostly through the notation `𝑳`. Lemmas involving `smoothLeftMul` with the notation `𝑳` usually use `L` instead of `𝑳` in the names. -/ def smoothLeftMul : C^∞⟮I, G; I, G⟯ := ⟨(g * ·), contMDiff_mul_left⟩ /-- Right multiplication by `g`. It is meant to mimic the usual notation in Lie groups. Used mostly through the notation `𝑹`. Lemmas involving `smoothRightMul` with the notation `𝑹` usually use `R` instead of `𝑹` in the names. -/ def smoothRightMul : C^∞⟮I, G; I, G⟯ := ⟨(· * g), contMDiff_mul_right⟩ -- Left multiplication. The abbreviation is `MIL`. @[inherit_doc] scoped[LieGroup] notation "𝑳" => smoothLeftMul -- Right multiplication. The abbreviation is `MIR`. @[inherit_doc] scoped[LieGroup] notation "𝑹" => smoothRightMul open scoped LieGroup @[simp] theorem L_apply : (𝑳 I g) h = g * h := rfl @[simp] theorem R_apply : (𝑹 I g) h = h * g := rfl @[simp] theorem L_mul {G : Type*} [Semigroup G] [TopologicalSpace G] [ChartedSpace H G] [ContMDiffMul I ∞ G] (g h : G) : 𝑳 I (g * h) = (𝑳 I g).comp (𝑳 I h) := by ext simp only [ContMDiffMap.comp_apply, L_apply, mul_assoc] @[simp] theorem R_mul {G : Type*} [Semigroup G] [TopologicalSpace G] [ChartedSpace H G] [ContMDiffMul I ∞ G] (g h : G) : 𝑹 I (g * h) = (𝑹 I h).comp (𝑹 I g) := by ext simp only [ContMDiffMap.comp_apply, R_apply, mul_assoc] section variable {G' : Type*} [Monoid G'] [TopologicalSpace G'] [ChartedSpace H G'] [ContMDiffMul I ∞ G'] (g' : G') theorem smoothLeftMul_one : (𝑳 I g') 1 = g' := mul_one g' theorem smoothRightMul_one : (𝑹 I g') 1 = g' := one_mul g' end -- Instance of product @[to_additive prod] instance ContMDiffMul.prod {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (G : Type*) [TopologicalSpace G] [ChartedSpace H G] [Mul G] [ContMDiffMul I n G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] (I' : ModelWithCorners 𝕜 E' H') (G' : Type*) [TopologicalSpace G'] [ChartedSpace H' G'] [Mul G'] [ContMDiffMul I' n G'] : ContMDiffMul (I.prod I') n (G × G') := { IsManifold.prod G G' with contMDiff_mul := ((contMDiff_fst.comp contMDiff_fst).mul (contMDiff_fst.comp contMDiff_snd)).prodMk ((contMDiff_snd.comp contMDiff_fst).mul (contMDiff_snd.comp contMDiff_snd)) } end ContMDiffMul section Monoid variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [Monoid G] [TopologicalSpace G] [ChartedSpace H G] [ContMDiffMul I n G] {H' : Type*} [TopologicalSpace H'] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {I' : ModelWithCorners 𝕜 E' H'} {G' : Type*} [Monoid G'] [TopologicalSpace G'] [ChartedSpace H' G'] [ContMDiffMul I' n G'] @[to_additive] theorem contMDiff_pow : ∀ i : ℕ, ContMDiff I I n fun a : G => a ^ i | 0 => by simp only [pow_zero, contMDiff_const] | k + 1 => by simpa [pow_succ] using (contMDiff_pow _).mul contMDiff_id /-- Morphism of additive `C^n` monoids. -/ structure ContMDiffAddMonoidMorphism (I : ModelWithCorners 𝕜 E H) (I' : ModelWithCorners 𝕜 E' H') (n : WithTop ℕ∞) (G : Type*) [TopologicalSpace G] [ChartedSpace H G] [AddMonoid G] (G' : Type*) [TopologicalSpace G'] [ChartedSpace H' G'] [AddMonoid G'] extends G →+ G' where contMDiff_toFun : ContMDiff I I' n toFun /-- Morphism of `C^n` monoids. -/ @[to_additive] structure ContMDiffMonoidMorphism (I : ModelWithCorners 𝕜 E H) (I' : ModelWithCorners 𝕜 E' H') (n : WithTop ℕ∞) (G : Type*) [TopologicalSpace G] [ChartedSpace H G] [Monoid G] (G' : Type*) [TopologicalSpace G'] [ChartedSpace H' G'] [Monoid G'] extends G →* G' where contMDiff_toFun : ContMDiff I I' n toFun @[to_additive] instance : One (ContMDiffMonoidMorphism I I' n G G') := ⟨{ contMDiff_toFun := contMDiff_const toMonoidHom := 1 }⟩ @[to_additive] instance : Inhabited (ContMDiffMonoidMorphism I I' n G G') := ⟨1⟩ @[to_additive] instance : FunLike (ContMDiffMonoidMorphism I I' n G G') G G' where coe a := a.toFun coe_injective' f g h := by cases f; cases g; congr; exact DFunLike.ext' h @[to_additive] instance : MonoidHomClass (ContMDiffMonoidMorphism I I' n G G') G G' where map_one f := f.map_one map_mul f := f.map_mul @[to_additive] instance : ContinuousMapClass (ContMDiffMonoidMorphism I I' n G G') G G' where map_continuous f := f.contMDiff_toFun.continuous end Monoid /-! ### Differentiability of finite point-wise sums and products, and powers Finite point-wise products (resp. sums), and powers, of `C^n` functions `M → G` (at `x`/on `s`) into a commutative monoid `G` are `C^n` at `x`/on `s`. -/ section CommMonoid open Function variable {ι 𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [CommMonoid G] [TopologicalSpace G] [ChartedSpace H G] [ContMDiffMul I n G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] {s : Set M} {x x₀ : M} {t : Finset ι} {f : ι → M → G} {p : ι → Prop} @[to_additive] theorem ContMDiffWithinAt.prod (h : ∀ i ∈ t, ContMDiffWithinAt I' I n (f i) s x₀) : ContMDiffWithinAt I' I n (fun x ↦ ∏ i ∈ t, f i x) s x₀ := by classical induction t using Finset.induction_on with | empty => simp [contMDiffWithinAt_const] | insert i K iK IH => simp only [iK, Finset.prod_insert, not_false_iff] exact (h _ (Finset.mem_insert_self i K)).mul (IH fun j hj ↦ h _ <| Finset.mem_insert_of_mem hj) @[to_additive] theorem contMDiffWithinAt_finprod (lf : LocallyFinite fun i ↦ mulSupport <| f i) {x₀ : M} (h : ∀ i, ContMDiffWithinAt I' I n (f i) s x₀) : ContMDiffWithinAt I' I n (fun x ↦ ∏ᶠ i, f i x) s x₀ := let ⟨_I, hI⟩ := finprod_eventually_eq_prod lf x₀ (ContMDiffWithinAt.prod fun i _hi ↦ h i).congr_of_eventuallyEq (eventually_nhdsWithin_of_eventually_nhds hI) hI.self_of_nhds @[to_additive] theorem contMDiffWithinAt_finset_prod' (h : ∀ i ∈ t, ContMDiffWithinAt I' I n (f i) s x) : ContMDiffWithinAt I' I n (∏ i ∈ t, f i) s x := Finset.prod_induction f (fun f => ContMDiffWithinAt I' I n f s x) (fun _ _ hf hg => hf.mul hg) (contMDiffWithinAt_const (c := 1)) h @[to_additive] theorem contMDiffWithinAt_finset_prod (h : ∀ i ∈ t, ContMDiffWithinAt I' I n (f i) s x) : ContMDiffWithinAt I' I n (fun x => ∏ i ∈ t, f i x) s x := by simp only [← Finset.prod_apply] exact contMDiffWithinAt_finset_prod' h @[to_additive] theorem ContMDiffAt.prod (h : ∀ i ∈ t, ContMDiffAt I' I n (f i) x₀) : ContMDiffAt I' I n (fun x ↦ ∏ i ∈ t, f i x) x₀ := by simp only [← contMDiffWithinAt_univ] at * exact ContMDiffWithinAt.prod h @[to_additive] theorem contMDiffAt_finprod (lf : LocallyFinite fun i ↦ mulSupport <| f i) (h : ∀ i, ContMDiffAt I' I n (f i) x₀) : ContMDiffAt I' I n (fun x ↦ ∏ᶠ i, f i x) x₀ := contMDiffWithinAt_finprod lf h @[to_additive] theorem contMDiffAt_finset_prod' (h : ∀ i ∈ t, ContMDiffAt I' I n (f i) x) : ContMDiffAt I' I n (∏ i ∈ t, f i) x := contMDiffWithinAt_finset_prod' h @[to_additive] theorem contMDiffAt_finset_prod (h : ∀ i ∈ t, ContMDiffAt I' I n (f i) x) : ContMDiffAt I' I n (fun x => ∏ i ∈ t, f i x) x := contMDiffWithinAt_finset_prod h @[to_additive] theorem contMDiffOn_finprod (lf : LocallyFinite fun i ↦ Function.mulSupport <| f i) (h : ∀ i, ContMDiffOn I' I n (f i) s) : ContMDiffOn I' I n (fun x ↦ ∏ᶠ i, f i x) s := fun x hx ↦ contMDiffWithinAt_finprod lf fun i ↦ h i x hx @[to_additive] theorem contMDiffOn_finset_prod' (h : ∀ i ∈ t, ContMDiffOn I' I n (f i) s) : ContMDiffOn I' I n (∏ i ∈ t, f i) s := fun x hx => contMDiffWithinAt_finset_prod' fun i hi => h i hi x hx @[to_additive] theorem contMDiffOn_finset_prod (h : ∀ i ∈ t, ContMDiffOn I' I n (f i) s) : ContMDiffOn I' I n (fun x => ∏ i ∈ t, f i x) s := fun x hx => contMDiffWithinAt_finset_prod fun i hi => h i hi x hx @[to_additive] theorem ContMDiff.prod (h : ∀ i ∈ t, ContMDiff I' I n (f i)) : ContMDiff I' I n fun x ↦ ∏ i ∈ t, f i x := fun x ↦ ContMDiffAt.prod fun j hj ↦ h j hj x @[to_additive] theorem contMDiff_finset_prod' (h : ∀ i ∈ t, ContMDiff I' I n (f i)) : ContMDiff I' I n (∏ i ∈ t, f i) := fun x => contMDiffAt_finset_prod' fun i hi => h i hi x @[to_additive] theorem contMDiff_finset_prod (h : ∀ i ∈ t, ContMDiff I' I n (f i)) : ContMDiff I' I n fun x => ∏ i ∈ t, f i x := fun x => contMDiffAt_finset_prod fun i hi => h i hi x @[to_additive] theorem contMDiff_finprod (h : ∀ i, ContMDiff I' I n (f i)) (hfin : LocallyFinite fun i => mulSupport (f i)) : ContMDiff I' I n fun x => ∏ᶠ i, f i x := fun x ↦ contMDiffAt_finprod hfin fun i ↦ h i x @[to_additive] theorem contMDiff_finprod_cond (hc : ∀ i, p i → ContMDiff I' I n (f i)) (hf : LocallyFinite fun i => mulSupport (f i)) : ContMDiff I' I n fun x => ∏ᶠ (i) (_ : p i), f i x := by simp only [← finprod_subtype_eq_finprod_cond] exact contMDiff_finprod (fun i => hc i i.2) (hf.comp_injective Subtype.coe_injective) variable {g : M → G} @[to_additive] theorem ContMDiffWithinAt.pow (hg : ContMDiffWithinAt I' I n g s x) (m : ℕ) : ContMDiffWithinAt I' I n (fun x ↦ g x ^ m) s x := (contMDiff_pow m).contMDiffAt.comp_contMDiffWithinAt x hg @[to_additive] nonrec theorem ContMDiffAt.pow (hg : ContMDiffAt I' I n g x) (m : ℕ) : ContMDiffAt I' I n (fun x ↦ g x ^ m) x := hg.pow m @[to_additive] theorem ContMDiffOn.pow (hg : ContMDiffOn I' I n g s) (m : ℕ) : ContMDiffOn I' I n (fun x ↦ g x ^ m) s := fun x hx ↦ (hg x hx).pow m @[to_additive] theorem ContMDiff.pow (hg : ContMDiff I' I n g) (m : ℕ) : ContMDiff I' I n (fun x ↦ g x ^ m) := fun x ↦ (hg x).pow m end CommMonoid section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {n : WithTop ℕ∞} instance instContMDiffAddSelf : ContMDiffAdd 𝓘(𝕜, E) n E := by constructor rw [← modelWithCornersSelf_prod, chartedSpaceSelf_prod] exact contDiff_add.contMDiff end section DivConst variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [DivInvMonoid G] [TopologicalSpace G] [ChartedSpace H G] [ContMDiffMul I n G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] variable {f : M → G} {s : Set M} {x : M} (c : G) @[to_additive] theorem ContMDiffWithinAt.div_const (hf : ContMDiffWithinAt I' I n f s x) : ContMDiffWithinAt I' I n (fun x ↦ f x / c) s x := by simpa only [div_eq_mul_inv] using hf.mul contMDiffWithinAt_const @[to_additive] nonrec theorem ContMDiffAt.div_const (hf : ContMDiffAt I' I n f x) : ContMDiffAt I' I n (fun x ↦ f x / c) x := hf.div_const c @[to_additive] theorem ContMDiffOn.div_const (hf : ContMDiffOn I' I n f s) : ContMDiffOn I' I n (fun x ↦ f x / c) s := fun x hx => (hf x hx).div_const c @[to_additive] theorem ContMDiff.div_const (hf : ContMDiff I' I n f) : ContMDiff I' I n (fun x ↦ f x / c) := fun x => (hf x).div_const c end DivConst
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Algebra/SmoothFunctions.lean
import Mathlib.Geometry.Manifold.Algebra.Structures /-! # Algebraic structures over `C^n` functions In this file, we define instances of algebraic structures over `C^n` functions. -/ noncomputable section open scoped Manifold ContDiff open TopologicalSpace variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H : Type*} [TopologicalSpace H] {I : ModelWithCorners 𝕜 E H} {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {N : Type*} [TopologicalSpace N] [ChartedSpace H N] {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace 𝕜 E''] {H'' : Type*} [TopologicalSpace H''] {I'' : ModelWithCorners 𝕜 E'' H''} {N' : Type*} [TopologicalSpace N'] [ChartedSpace H'' N'] {n : WithTop ℕ∞} namespace ContMDiffMap @[to_additive] protected instance instMul {G : Type*} [Mul G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : Mul C^n⟮I, N; I', G⟯ := ⟨fun f g => ⟨f * g, f.contMDiff.mul g.contMDiff⟩⟩ @[to_additive (attr := simp)] theorem coe_mul {G : Type*} [Mul G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] (f g : C^n⟮I, N; I', G⟯) : ⇑(f * g) = f * g := rfl @[to_additive (attr := simp)] theorem mul_comp {G : Type*} [Mul G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] (f g : C^n⟮I'', N'; I', G⟯) (h : C^n⟮I, N; I'', N'⟯) : (f * g).comp h = f.comp h * g.comp h := rfl @[to_additive] protected instance instOne {G : Type*} [One G] [TopologicalSpace G] [ChartedSpace H' G] : One C^n⟮I, N; I', G⟯ := ⟨ContMDiffMap.const (1 : G)⟩ @[to_additive (attr := simp)] theorem coe_one {G : Type*} [One G] [TopologicalSpace G] [ChartedSpace H' G] : ⇑(1 : C^n⟮I, N; I', G⟯) = 1 := rfl instance instNSMul {G : Type*} [AddMonoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffAdd I' n G] : SMul ℕ C^n⟮I, N; I', G⟯ where smul n f := ⟨n • (f : N → G), (contMDiff_nsmul n).comp f.contMDiff⟩ @[to_additive existing] instance instPow {G : Type*} [Monoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : Pow C^n⟮I, N; I', G⟯ ℕ where pow f n := ⟨(f : N → G) ^ n, (contMDiff_pow n).comp f.contMDiff⟩ @[to_additive (attr := simp)] theorem coe_pow {G : Type*} [Monoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] (f : C^n⟮I, N; I', G⟯) (n : ℕ) : ⇑(f ^ n) = (f : N → G) ^ n := rfl section GroupStructure /-! ### Group structure In this section we show that `C^n` functions valued in a Lie group inherit a group structure under pointwise multiplication. -/ @[to_additive] instance semigroup {G : Type*} [Semigroup G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : Semigroup C^n⟮I, N; I', G⟯ := DFunLike.coe_injective.semigroup _ coe_mul @[to_additive] instance monoid {G : Type*} [Monoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : Monoid C^n⟮I, N; I', G⟯ := DFunLike.coe_injective.monoid _ coe_one coe_mul coe_pow /-- Coercion to a function as a `MonoidHom`. Similar to `MonoidHom.coeFn`. -/ @[to_additive (attr := simps) /-- Coercion to a function as an `AddMonoidHom`. Similar to `AddMonoidHom.coeFn`. -/] def coeFnMonoidHom {G : Type*} [Monoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : C^n⟮I, N; I', G⟯ →* N → G where toFun := DFunLike.coe map_one' := coe_one map_mul' := coe_mul variable (I N) /-- For a manifold `N` and a `C^n` homomorphism `φ` between Lie groups `G'`, `G''`, the 'left-composition-by-`φ`' group homomorphism from `C^n⟮I, N; I', G'⟯` to `C^n⟮I, N; I'', G''⟯`. -/ @[to_additive /-- For a manifold `N` and a `C^n` homomorphism `φ` between additive Lie groups `G'`, `G''`, the 'left-composition-by-`φ`' group homomorphism from `C^n⟮I, N; I', G'⟯` to `C^n⟮I, N; I'', G''⟯`. -/] def compLeftMonoidHom {G' : Type*} [Monoid G'] [TopologicalSpace G'] [ChartedSpace H' G'] [ContMDiffMul I' n G'] {G'' : Type*} [Monoid G''] [TopologicalSpace G''] [ChartedSpace H'' G''] [ContMDiffMul I'' n G''] (φ : G' →* G'') (hφ : ContMDiff I' I'' n φ) : C^n⟮I, N; I', G'⟯ →* C^n⟮I, N; I'', G''⟯ where toFun f := ⟨φ ∘ f, hφ.comp f.contMDiff⟩ map_one' := by ext; change φ 1 = 1; simp map_mul' f g := by ext x; change φ (f x * g x) = φ (f x) * φ (g x); simp variable (I') {N} -- TODO: generalize to any `C^n` map instead of `Set.inclusion` /-- For a Lie group `G` and open sets `U ⊆ V` in `N`, the 'restriction' group homomorphism from `C^n⟮I, V; I', G⟯` to `C^n⟮I, U; I', G⟯`. -/ @[to_additive /-- For an additive Lie group `G` and open sets `U ⊆ V` in `N`, the 'restriction' group homomorphism from `C^n⟮I, V; I', G⟯` to `C^n⟮I, U; I', G⟯`. -/] def restrictMonoidHom (G : Type*) [Monoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] {U V : Opens N} (h : U ≤ V) : C^n⟮I, V; I', G⟯ →* C^n⟮I, U; I', G⟯ where toFun f := ⟨f ∘ Set.inclusion h, f.contMDiff.comp (contMDiff_inclusion h)⟩ map_one' := rfl map_mul' _ _ := rfl variable {I I'} @[to_additive] instance commMonoid {G : Type*} [CommMonoid G] [TopologicalSpace G] [ChartedSpace H' G] [ContMDiffMul I' n G] : CommMonoid C^n⟮I, N; I', G⟯ := DFunLike.coe_injective.commMonoid _ coe_one coe_mul coe_pow @[to_additive] instance group {G : Type*} [Group G] [TopologicalSpace G] [ChartedSpace H' G] [LieGroup I' n G] : Group C^n⟮I, N; I', G⟯ := { ContMDiffMap.monoid with inv := fun f => ⟨fun x => (f x)⁻¹, f.contMDiff.inv⟩ inv_mul_cancel := fun a => by ext; exact inv_mul_cancel _ div := fun f g => ⟨f / g, f.contMDiff.div g.contMDiff⟩ div_eq_mul_inv := fun f g => by ext; exact div_eq_mul_inv _ _ } @[to_additive (attr := simp)] theorem coe_inv {G : Type*} [Group G] [TopologicalSpace G] [ChartedSpace H' G] [LieGroup I' n G] (f : C^n⟮I, N; I', G⟯) : ⇑f⁻¹ = (⇑f)⁻¹ := rfl @[to_additive (attr := simp)] theorem coe_div {G : Type*} [Group G] [TopologicalSpace G] [ChartedSpace H' G] [LieGroup I' n G] (f g : C^n⟮I, N; I', G⟯) : ⇑(f / g) = f / g := rfl @[to_additive] instance commGroup {G : Type*} [CommGroup G] [TopologicalSpace G] [ChartedSpace H' G] [LieGroup I' n G] : CommGroup C^n⟮I, N; I', G⟯ := { ContMDiffMap.group, ContMDiffMap.commMonoid with } end GroupStructure section RingStructure /-! ### Ring structure In this section we show that `C^n` functions valued in a `C^n` ring `R` inherit a ring structure under pointwise multiplication. -/ instance semiring {R : Type*} [Semiring R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] : Semiring C^n⟮I, N; I', R⟯ := { ContMDiffMap.addCommMonoid, ContMDiffMap.monoid with left_distrib := fun a b c => by ext; exact left_distrib _ _ _ right_distrib := fun a b c => by ext; exact right_distrib _ _ _ zero_mul := fun a => by ext; exact zero_mul _ mul_zero := fun a => by ext; exact mul_zero _ } instance ring {R : Type*} [Ring R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] : Ring C^n⟮I, N; I', R⟯ := { ContMDiffMap.semiring, ContMDiffMap.addCommGroup with } instance commRing {R : Type*} [CommRing R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] : CommRing C^n⟮I, N; I', R⟯ := { ContMDiffMap.semiring, ContMDiffMap.addCommGroup, ContMDiffMap.commMonoid with } variable (I N) /-- For a manifold `N` and a `C^n` homomorphism `φ` between `C^n` rings `R'`, `R''`, the 'left-composition-by-`φ`' ring homomorphism from `C^n⟮I, N; I', R'⟯` to `C^n⟮I, N; I'', R''⟯`. -/ def compLeftRingHom {R' : Type*} [Ring R'] [TopologicalSpace R'] [ChartedSpace H' R'] [ContMDiffRing I' n R'] {R'' : Type*} [Ring R''] [TopologicalSpace R''] [ChartedSpace H'' R''] [ContMDiffRing I'' n R''] (φ : R' →+* R'') (hφ : ContMDiff I' I'' n φ) : C^n⟮I, N; I', R'⟯ →+* C^n⟮I, N; I'', R''⟯ := { ContMDiffMap.compLeftMonoidHom I N φ.toMonoidHom hφ, ContMDiffMap.compLeftAddMonoidHom I N φ.toAddMonoidHom hφ with toFun := fun f => ⟨φ ∘ f, hφ.comp f.contMDiff⟩ } variable (I') {N} /-- For a "`C^n` ring" `R` and open sets `U ⊆ V` in `N`, the "restriction" ring homomorphism from `C^n⟮I, V; I', R⟯` to `C^n⟮I, U; I', R⟯`. -/ def restrictRingHom (R : Type*) [Ring R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] {U V : Opens N} (h : U ≤ V) : C^n⟮I, V; I', R⟯ →+* C^n⟮I, U; I', R⟯ := { ContMDiffMap.restrictMonoidHom I I' R h, ContMDiffMap.restrictAddMonoidHom I I' R h with toFun := fun f => ⟨f ∘ Set.inclusion h, f.contMDiff.comp (contMDiff_inclusion h)⟩ } variable {I I'} /-- Coercion to a function as a `RingHom`. -/ @[simps] def coeFnRingHom {R : Type*} [CommRing R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] : C^n⟮I, N; I', R⟯ →+* N → R := { (coeFnMonoidHom : C^n⟮I, N; I', R⟯ →* _), (coeFnAddMonoidHom : C^n⟮I, N; I', R⟯ →+ _) with toFun := (↑) } /-- `Function.eval` as a `RingHom` on the ring of `C^n` functions. -/ def evalRingHom {R : Type*} [CommRing R] [TopologicalSpace R] [ChartedSpace H' R] [ContMDiffRing I' n R] (m : N) : C^n⟮I, N; I', R⟯ →+* R := (Pi.evalRingHom _ m : (N → R) →+* R).comp ContMDiffMap.coeFnRingHom end RingStructure section ModuleStructure /-! ### Semimodule structure In this section we show that `C^n` functions valued in a vector space `M` over a normed field `𝕜` inherit a vector space structure. -/ instance instSMul {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] : SMul 𝕜 C^n⟮I, N; 𝓘(𝕜, V), V⟯ := ⟨fun r f => ⟨r • ⇑f, contMDiff_const.smul f.contMDiff⟩⟩ @[simp] theorem coe_smul {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] (r : 𝕜) (f : C^n⟮I, N; 𝓘(𝕜, V), V⟯) : ⇑(r • f) = r • ⇑f := rfl @[simp] theorem smul_comp {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] (r : 𝕜) (g : C^n⟮I'', N'; 𝓘(𝕜, V), V⟯) (h : C^n⟮I, N; I'', N'⟯) : (r • g).comp h = r • g.comp h := rfl instance module {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] : Module 𝕜 C^n⟮I, N; 𝓘(𝕜, V), V⟯ := Function.Injective.module 𝕜 coeFnAddMonoidHom ContMDiffMap.coe_injective coe_smul /-- Coercion to a function as a `LinearMap`. -/ @[simps] def coeFnLinearMap {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] : C^n⟮I, N; 𝓘(𝕜, V), V⟯ →ₗ[𝕜] N → V := { (coeFnAddMonoidHom : C^n⟮I, N; 𝓘(𝕜, V), V⟯ →+ _) with toFun := (↑) map_smul' := coe_smul } end ModuleStructure section AlgebraStructure /-! ### Algebra structure In this section we show that `C^n` functions valued in a normed algebra `A` over a normed field `𝕜` inherit an algebra structure. -/ variable {A : Type*} [NormedRing A] [NormedAlgebra 𝕜 A] [ContMDiffRing 𝓘(𝕜, A) n A] /-- `C^n` constant functions as a `RingHom`. -/ def C : 𝕜 →+* C^n⟮I, N; 𝓘(𝕜, A), A⟯ where toFun := fun c : 𝕜 => ⟨fun _ => (algebraMap 𝕜 A) c, contMDiff_const⟩ map_one' := by ext; exact (algebraMap 𝕜 A).map_one map_mul' c₁ c₂ := by ext; exact (algebraMap 𝕜 A).map_mul _ _ map_zero' := by ext; exact (algebraMap 𝕜 A).map_zero map_add' c₁ c₂ := by ext; exact (algebraMap 𝕜 A).map_add _ _ instance algebra : Algebra 𝕜 C^n⟮I, N; 𝓘(𝕜, A), A⟯ where smul := fun r f => ⟨r • f, contMDiff_const.smul f.contMDiff⟩ algebraMap := ContMDiffMap.C commutes' := fun c f => by ext x; exact Algebra.commutes' _ _ smul_def' := fun c f => by ext x; exact Algebra.smul_def' _ _ /-- Coercion to a function as an `AlgHom`. -/ @[simps] def coeFnAlgHom : C^n⟮I, N; 𝓘(𝕜, A), A⟯ →ₐ[𝕜] N → A where toFun := (↑) commutes' _ := rfl -- `(ContMDiffMap.coeFnRingHom : C^n⟮I, N; 𝓘(𝕜, A), A⟯ →+* _) with` times out for some reason map_zero' := ContMDiffMap.coe_zero map_one' := ContMDiffMap.coe_one map_add' := ContMDiffMap.coe_add map_mul' := ContMDiffMap.coe_mul end AlgebraStructure section ModuleOverContinuousFunctions /-! ### Structure as module over scalar functions If `V` is a module over `𝕜`, then we show that the space of `C^n` functions from `N` to `V` is naturally a vector space over the ring of `C^n` functions from `N` to `𝕜`. -/ /-- `C^n` scalar-valued functions act by left-multiplication on `C^n` functions. -/ instance instSMul' {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] : SMul C^n⟮I, N; 𝕜⟯ C^n⟮I, N; 𝓘(𝕜, V), V⟯ := ⟨fun f g => ⟨fun x => f x • g x, ContMDiff.smul f.2 g.2⟩⟩ /-- The left multiplication with a `C^n` scalar function commutes with composition. -/ @[simp] theorem smul_comp' {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] (f : C^n⟮I'', N'; 𝕜⟯) (g : C^n⟮I'', N'; 𝓘(𝕜, V), V⟯) (h : C^n⟮I, N; I'', N'⟯) : (f • g).comp h = f.comp h • g.comp h := rfl /-- The space of `C^n` functions with values in a space `V` is a module over the space of `C^n` functions with values in `𝕜`. -/ instance module' {V : Type*} [NormedAddCommGroup V] [NormedSpace 𝕜 V] : Module C^n⟮I, N; 𝓘(𝕜), 𝕜⟯ C^n⟮I, N; 𝓘(𝕜, V), V⟯ where smul_add c f g := by ext x; exact smul_add (c x) (f x) (g x) add_smul c₁ c₂ f := by ext x; exact add_smul (c₁ x) (c₂ x) (f x) mul_smul c₁ c₂ f := by ext x; exact mul_smul (c₁ x) (c₂ x) (f x) one_smul f := by ext x; exact one_smul 𝕜 (f x) zero_smul f := by ext x; exact zero_smul _ _ smul_zero r := by ext x; exact smul_zero _ end ModuleOverContinuousFunctions end ContMDiffMap
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Algebra/LeftInvariantDerivation.lean
import Mathlib.RingTheory.Derivation.Lie import Mathlib.Geometry.Manifold.DerivationBundle /-! # Left invariant derivations In this file we define the concept of left invariant derivation for a Lie group. The concept is analogous to the more classical concept of left invariant vector fields, and it holds that the derivation associated to a vector field is left invariant iff the field is. Moreover we prove that `LeftInvariantDerivation I G` has the structure of a Lie algebra, hence implementing one of the possible definitions of the Lie algebra attached to a Lie group. Note that one can also define a Lie algebra on the space of left-invariant vector fields (see `instLieAlgebraGroupLieAlgebra`). For finite-dimensional `C^∞` real manifolds, the space of derivations can be canonically identified with the tangent space, and we recover the same Lie algebra structure (TODO: prove this). In other smoothness classes or on other fields, this identification is not always true, though, so the derivations point of view does not work in these settings. The left-invariant vector fields should therefore be favored to construct a theory of Lie groups in suitable generality. -/ noncomputable section open scoped LieGroup Manifold Derivation ContDiff variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {H : Type*} [TopologicalSpace H] (I : ModelWithCorners 𝕜 E H) (G : Type*) [TopologicalSpace G] [ChartedSpace H G] [Monoid G] [ContMDiffMul I ∞ G] (g h : G) /-- Left-invariant global derivations. A global derivation is left-invariant if it is equal to its pullback along left multiplication by an arbitrary element of `G`. -/ structure LeftInvariantDerivation extends Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯ where left_invariant'' : ∀ g, 𝒅ₕ (smoothLeftMul_one I g) (Derivation.evalAt 1 toDerivation) = Derivation.evalAt g toDerivation variable {I G} namespace LeftInvariantDerivation instance : Coe (LeftInvariantDerivation I G) (Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) := ⟨toDerivation⟩ attribute [coe] toDerivation theorem toDerivation_injective : Function.Injective (toDerivation : LeftInvariantDerivation I G → _) := fun X Y h => by cases X; cases Y; congr instance : FunLike (LeftInvariantDerivation I G) C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯ where coe f := f.toDerivation coe_injective' _ _ h := toDerivation_injective <| DFunLike.ext' h instance : LinearMapClass (LeftInvariantDerivation I G) 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯ where map_add f := map_add f.1 map_smulₛₗ f := map_smul f.1.1 variable {r : 𝕜} {X Y : LeftInvariantDerivation I G} {f f' : C^∞⟮I, G; 𝕜⟯} theorem toFun_eq_coe : X.toFun = ⇑X := rfl theorem coe_injective : @Function.Injective (LeftInvariantDerivation I G) (_ → C^∞⟮I, G; 𝕜⟯) DFunLike.coe := DFunLike.coe_injective @[ext] theorem ext (h : ∀ f, X f = Y f) : X = Y := DFunLike.ext _ _ h variable (X Y f) theorem coe_derivation : ⇑(X : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = (X : C^∞⟮I, G; 𝕜⟯ → C^∞⟮I, G; 𝕜⟯) := rfl /-- Premature version of the lemma. Prefer using `left_invariant` instead. -/ theorem left_invariant' : 𝒅ₕ (smoothLeftMul_one I g) (Derivation.evalAt (1 : G) ↑X) = Derivation.evalAt g ↑X := left_invariant'' X g protected theorem map_add : X (f + f') = X f + X f' := by simp protected theorem map_zero : X 0 = 0 := by simp protected theorem map_neg : X (-f) = -X f := by simp protected theorem map_sub : X (f - f') = X f - X f' := by simp protected theorem map_smul : X (r • f) = r • X f := by simp @[simp] theorem leibniz : X (f * f') = f • X f' + f' • X f := X.leibniz' _ _ instance : Zero (LeftInvariantDerivation I G) := ⟨⟨0, fun g => by simp only [map_zero]⟩⟩ instance : Inhabited (LeftInvariantDerivation I G) := ⟨0⟩ instance : Add (LeftInvariantDerivation I G) where add X Y := ⟨X + Y, fun g => by simp only [map_add, left_invariant']⟩ instance : Neg (LeftInvariantDerivation I G) where neg X := ⟨-X, fun g => by simp [left_invariant']⟩ instance : Sub (LeftInvariantDerivation I G) where sub X Y := ⟨X - Y, fun g => by simp [left_invariant']⟩ @[simp] theorem coe_add : ⇑(X + Y) = X + Y := rfl @[simp] theorem coe_zero : ⇑(0 : LeftInvariantDerivation I G) = 0 := rfl @[simp] theorem coe_neg : ⇑(-X) = -X := rfl @[simp] theorem coe_sub : ⇑(X - Y) = X - Y := rfl @[simp, norm_cast] theorem lift_add : (↑(X + Y) : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = X + Y := rfl @[simp, norm_cast] theorem lift_zero : (↑(0 : LeftInvariantDerivation I G) : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) = 0 := rfl instance hasNatScalar : SMul ℕ (LeftInvariantDerivation I G) where smul r X := ⟨r • X.1, fun g => by simp_rw [LinearMap.map_smul_of_tower _ r, left_invariant']⟩ instance hasIntScalar : SMul ℤ (LeftInvariantDerivation I G) where smul r X := ⟨r • X.1, fun g => by simp_rw [LinearMap.map_smul_of_tower _ r, left_invariant']⟩ instance : AddCommGroup (LeftInvariantDerivation I G) := coe_injective.addCommGroup _ coe_zero coe_add coe_neg coe_sub (fun _ _ => rfl) fun _ _ => rfl instance : SMul 𝕜 (LeftInvariantDerivation I G) where smul r X := ⟨r • X.1, fun g => by simp_rw [LinearMap.map_smul, left_invariant']⟩ variable (r) @[simp] theorem coe_smul : ⇑(r • X) = r • ⇑X := rfl @[simp] theorem lift_smul (k : 𝕜) : (k • X).1 = k • X.1 := rfl variable (I G) /-- The coercion to function is a monoid homomorphism. -/ @[simps] def coeFnAddMonoidHom : LeftInvariantDerivation I G →+ C^∞⟮I, G; 𝕜⟯ → C^∞⟮I, G; 𝕜⟯ := ⟨⟨DFunLike.coe, coe_zero⟩, coe_add⟩ variable {I G} instance : Module 𝕜 (LeftInvariantDerivation I G) := coe_injective.module _ (coeFnAddMonoidHom I G) coe_smul /-- Evaluation at a point for left invariant derivation. Same thing as for generic global derivations (`Derivation.evalAt`). -/ def evalAt : LeftInvariantDerivation I G →ₗ[𝕜] PointDerivation I g where toFun X := Derivation.evalAt g X.1 map_add' _ _ := rfl map_smul' _ _ := rfl theorem evalAt_apply : evalAt g X f = (X f) g := rfl @[simp] theorem evalAt_coe : Derivation.evalAt g ↑X = evalAt g X := rfl theorem left_invariant : 𝒅ₕ (smoothLeftMul_one I g) (evalAt (1 : G) X) = evalAt g X := X.left_invariant'' g theorem evalAt_mul : evalAt (g * h) X = 𝒅ₕ (L_apply I g h) (evalAt h X) := by ext f rw [← left_invariant, hfdifferential_apply, hfdifferential_apply, L_mul, fdifferential_comp, fdifferential_apply] simp only [ContMDiffMap.comp_apply, LinearMap.comp_apply] rw [fdifferential_apply, ← hfdifferential_apply (smoothLeftMul_one I h), left_invariant] theorem comp_L : (X f).comp (𝑳 I g) = X (f.comp (𝑳 I g)) := by ext h rw [ContMDiffMap.comp_apply, L_apply, ← evalAt_apply, evalAt_mul, hfdifferential_apply, fdifferential_apply, evalAt_apply] instance : Bracket (LeftInvariantDerivation I G) (LeftInvariantDerivation I G) where bracket X Y := ⟨⁅(X : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯), Y⁆, fun g => by ext f have hX := Derivation.congr_fun (left_invariant' g X) (Y f) have hY := Derivation.congr_fun (left_invariant' g Y) (X f) rw [hfdifferential_apply, fdifferential_apply, Derivation.evalAt_apply] at hX hY ⊢ rw [comp_L] at hX hY rw [Derivation.commutator_apply, ContMDiffMap.coe_sub, Pi.sub_apply, coe_derivation] rw [coe_derivation] at hX hY ⊢ rw [hX, hY] rfl⟩ @[simp] theorem commutator_coe_derivation : ⇑⁅X, Y⁆ = (⁅(X : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯), Y⁆ : Derivation 𝕜 C^∞⟮I, G; 𝕜⟯ C^∞⟮I, G; 𝕜⟯) := rfl theorem commutator_apply : ⁅X, Y⁆ f = X (Y f) - Y (X f) := rfl instance : LieRing (LeftInvariantDerivation I G) where add_lie X Y Z := by ext1 simp only [commutator_apply, coe_add, Pi.add_apply, map_add] ring lie_add X Y Z := by ext1 simp only [commutator_apply, coe_add, Pi.add_apply, map_add] ring lie_self X := by ext1; simp only [commutator_apply, sub_self]; rfl leibniz_lie X Y Z := by ext1 simp only [commutator_apply, coe_add, map_sub, Pi.add_apply] ring instance : LieAlgebra 𝕜 (LeftInvariantDerivation I G) where lie_smul r Y Z := by ext1 simp only [commutator_apply, map_smul, smul_sub, coe_smul, Pi.smul_apply] end LeftInvariantDerivation
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Algebra/LieGroup.lean
import Mathlib.Geometry.Manifold.Algebra.Monoid /-! # Lie groups A Lie group is a group that is also a `C^n` manifold, in which the group operations of multiplication and inversion are `C^n` maps. Regularity of the group multiplication means that multiplication is a `C^n` mapping of the product manifold `G` × `G` into `G`. Note that, since a manifold here is not second-countable and Hausdorff a Lie group here is not guaranteed to be second-countable (even though it can be proved it is Hausdorff). Note also that Lie groups here are not necessarily finite dimensional. ## Main definitions * `LieAddGroup I G` : a Lie additive group where `G` is a manifold on the model with corners `I`. * `LieGroup I G` : a Lie multiplicative group where `G` is a manifold on the model with corners `I`. * `ContMDiffInv₀`: typeclass for `C^n` manifolds with `0` and `Inv` such that inversion is `C^n` map at each non-zero point. This includes complete normed fields and (multiplicative) Lie groups. ## Main results * `ContMDiff.inv`, `ContMDiff.div` and variants: point-wise inversion and division of maps `M → G` is `C^n`. * `ContMDiff.inv₀` and variants: if `ContMDiffInv₀ I n N`, point-wise inversion of `C^n` maps `f : M → N` is `C^n` at all points at which `f` doesn't vanish. * `ContMDiff.div₀` and variants: if also `ContMDiffMul I n N` (i.e., `N` is a Lie group except possibly for smoothness of inversion at `0`), similar results hold for point-wise division. * `instNormedSpaceLieAddGroup` : a normed vector space over a nontrivially normed field is an additive Lie group. * `Instances/UnitsOfNormedAlgebra` shows that the group of units of a complete normed `𝕜`-algebra is a multiplicative Lie group. ## Implementation notes A priori, a Lie group here is a manifold with corners. The definition of Lie group cannot require `I : ModelWithCorners 𝕜 E E` with the same space as the model space and as the model vector space, as one might hope, because in the product situation, the model space is `ModelProd E E'` and the model vector space is `E × E'`, which are not the same, so the definition does not apply. Hence the definition should be more general, allowing `I : ModelWithCorners 𝕜 E H`. -/ noncomputable section open scoped Manifold ContDiff -- See note [Design choices about smooth algebraic structures] /-- An additive Lie group is a group and a `C^n` manifold at the same time in which the addition and negation operations are `C^n`. -/ class LieAddGroup {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (G : Type*) [AddGroup G] [TopologicalSpace G] [ChartedSpace H G] : Prop extends ContMDiffAdd I n G where /-- Negation is smooth in an additive Lie group. -/ contMDiff_neg : ContMDiff I I n fun a : G => -a -- See note [Design choices about smooth algebraic structures] /-- A (multiplicative) Lie group is a group and a `C^n` manifold at the same time in which the multiplication and inverse operations are `C^n`. -/ @[to_additive] class LieGroup {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (G : Type*) [Group G] [TopologicalSpace G] [ChartedSpace H G] : Prop extends ContMDiffMul I n G where /-- Inversion is smooth in a Lie group. -/ contMDiff_inv : ContMDiff I I n fun a : G => a⁻¹ /-! ### Smoothness of inversion, negation, division and subtraction Let `f : M → G` be a `C^n` function into a Lie group, then `f` is point-wise invertible with smooth inverse `f`. If `f` and `g` are two such functions, the quotient `f / g` (i.e., the point-wise product of `f` and the point-wise inverse of `g`) is also `C^n`. -/ section PointwiseDivision variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {n : WithTop ℕ∞} {G : Type*} [TopologicalSpace G] [ChartedSpace H G] [Group G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] @[to_additive] protected theorem LieGroup.of_le {m n : WithTop ℕ∞} (hmn : m ≤ n) [h : LieGroup I n G] : LieGroup I m G := by have : ContMDiffMul I m G := ContMDiffMul.of_le hmn exact ⟨h.contMDiff_inv.of_le hmn⟩ @[to_additive] instance {a : WithTop ℕ∞} [LieGroup I ∞ G] [h : ENat.LEInfty a] : LieGroup I a G := LieGroup.of_le h.out @[to_additive] instance {a : WithTop ℕ∞} [LieGroup I ω G] : LieGroup I a G := LieGroup.of_le le_top @[to_additive] instance [IsTopologicalGroup G] : LieGroup I 0 G := by constructor rw [contMDiff_zero_iff] exact continuous_inv @[to_additive] instance [LieGroup I 2 G] : LieGroup I 1 G := LieGroup.of_le one_le_two variable [LieGroup I n G] section variable (I n) /-- In a Lie group, inversion is `C^n`. -/ @[to_additive /-- In an additive Lie group, inversion is a smooth map. -/] theorem contMDiff_inv : ContMDiff I I n fun x : G => x⁻¹ := LieGroup.contMDiff_inv include I n in /-- A Lie group is a topological group. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ @[to_additive /-- An additive Lie group is an additive topological group. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/] theorem topologicalGroup_of_lieGroup : IsTopologicalGroup G := { continuousMul_of_contMDiffMul I n with continuous_inv := (contMDiff_inv I n).continuous } end @[to_additive] theorem ContMDiffWithinAt.inv {f : M → G} {s : Set M} {x₀ : M} (hf : ContMDiffWithinAt I' I n f s x₀) : ContMDiffWithinAt I' I n (fun x => (f x)⁻¹) s x₀ := (contMDiff_inv I n).contMDiffAt.contMDiffWithinAt.comp x₀ hf <| Set.mapsTo_univ _ _ @[to_additive] theorem ContMDiffAt.inv {f : M → G} {x₀ : M} (hf : ContMDiffAt I' I n f x₀) : ContMDiffAt I' I n (fun x => (f x)⁻¹) x₀ := (contMDiff_inv I n).contMDiffAt.comp x₀ hf @[to_additive] theorem ContMDiffOn.inv {f : M → G} {s : Set M} (hf : ContMDiffOn I' I n f s) : ContMDiffOn I' I n (fun x => (f x)⁻¹) s := fun x hx => (hf x hx).inv @[to_additive] theorem ContMDiff.inv {f : M → G} (hf : ContMDiff I' I n f) : ContMDiff I' I n fun x => (f x)⁻¹ := fun x => (hf x).inv @[to_additive] theorem ContMDiffWithinAt.div {f g : M → G} {s : Set M} {x₀ : M} (hf : ContMDiffWithinAt I' I n f s x₀) (hg : ContMDiffWithinAt I' I n g s x₀) : ContMDiffWithinAt I' I n (fun x => f x / g x) s x₀ := by simp_rw [div_eq_mul_inv]; exact hf.mul hg.inv @[to_additive] theorem ContMDiffAt.div {f g : M → G} {x₀ : M} (hf : ContMDiffAt I' I n f x₀) (hg : ContMDiffAt I' I n g x₀) : ContMDiffAt I' I n (fun x => f x / g x) x₀ := by simp_rw [div_eq_mul_inv]; exact hf.mul hg.inv @[to_additive] theorem ContMDiffOn.div {f g : M → G} {s : Set M} (hf : ContMDiffOn I' I n f s) (hg : ContMDiffOn I' I n g s) : ContMDiffOn I' I n (fun x => f x / g x) s := by simp_rw [div_eq_mul_inv]; exact hf.mul hg.inv @[to_additive] theorem ContMDiff.div {f g : M → G} (hf : ContMDiff I' I n f) (hg : ContMDiff I' I n g) : ContMDiff I' I n fun x => f x / g x := by simp_rw [div_eq_mul_inv]; exact hf.mul hg.inv end PointwiseDivision /-! Binary product of Lie groups -/ section Product -- Instance of product group @[to_additive] instance Prod.instLieGroup {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [TopologicalSpace G] [ChartedSpace H G] [Group G] [LieGroup I n G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {G' : Type*} [TopologicalSpace G'] [ChartedSpace H' G'] [Group G'] [LieGroup I' n G'] : LieGroup (I.prod I') n (G × G') := { ContMDiffMul.prod _ _ _ _ with contMDiff_inv := contMDiff_fst.inv.prodMk contMDiff_snd.inv } end Product /-! ### Normed spaces are Lie groups -/ instance instNormedSpaceLieAddGroup {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] : LieAddGroup 𝓘(𝕜, E) n E where contMDiff_neg := contDiff_neg.contMDiff /-! ## `C^n` manifolds with `C^n` inversion away from zero Typeclass for `C^n` manifolds with `0` and `Inv` such that inversion is `C^n` at all non-zero points. (This includes multiplicative Lie groups, but also complete normed semifields.) Point-wise inversion is `C^n` when the function/denominator is non-zero. -/ section ContMDiffInv₀ -- See note [Design choices about smooth algebraic structures] /-- A `C^n` manifold with `0` and `Inv` such that `fun x ↦ x⁻¹` is `C^n` at all nonzero points. Any complete normed (semi)field has this property. -/ class ContMDiffInv₀ {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (G : Type*) [Inv G] [Zero G] [TopologicalSpace G] [ChartedSpace H G] : Prop where /-- Inversion is `C^n` away from `0`. -/ contMDiffAt_inv₀ : ∀ ⦃x : G⦄, x ≠ 0 → ContMDiffAt I I n (fun y ↦ y⁻¹) x instance {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} : ContMDiffInv₀ 𝓘(𝕜) n 𝕜 where contMDiffAt_inv₀ x hx := by change ContMDiffAt 𝓘(𝕜) 𝓘(𝕜) n Inv.inv x rw [contMDiffAt_iff_contDiffAt] exact contDiffAt_inv 𝕜 hx variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [TopologicalSpace G] [ChartedSpace H G] [Inv G] [Zero G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] {f : M → G} protected theorem ContMDiffInv₀.of_le {m n : WithTop ℕ∞} (hmn : m ≤ n) [h : ContMDiffInv₀ I n G] : ContMDiffInv₀ I m G := by exact ⟨fun x hx ↦ (h.contMDiffAt_inv₀ hx).of_le hmn⟩ instance {a : WithTop ℕ∞} [ContMDiffInv₀ I ∞ G] [h : ENat.LEInfty a] : ContMDiffInv₀ I a G := ContMDiffInv₀.of_le h.out instance {a : WithTop ℕ∞} [ContMDiffInv₀ I ω G] : ContMDiffInv₀ I a G := ContMDiffInv₀.of_le le_top instance [ContinuousInv₀ G] : ContMDiffInv₀ I 0 G := by have : T1Space G := I.t1Space G constructor have A : ContMDiffOn I I 0 (fun (x : G) ↦ x⁻¹) {0}ᶜ := by rw [contMDiffOn_zero_iff] exact continuousOn_inv₀ intro x hx have : ContMDiffWithinAt I I 0 (fun (x : G) ↦ x⁻¹) {0}ᶜ x := A x hx apply ContMDiffWithinAt.contMDiffAt this exact IsOpen.mem_nhds isOpen_compl_singleton hx instance [ContMDiffInv₀ I 2 G] : ContMDiffInv₀ I 1 G := ContMDiffInv₀.of_le one_le_two variable [ContMDiffInv₀ I n G] theorem contMDiffAt_inv₀ {x : G} (hx : x ≠ 0) : ContMDiffAt I I n (fun y ↦ y⁻¹) x := ContMDiffInv₀.contMDiffAt_inv₀ hx include I n in /-- In a manifold with `C^n` inverse away from `0`, the inverse is continuous away from `0`. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ theorem continuousInv₀_of_contMDiffInv₀ : ContinuousInv₀ G := { continuousAt_inv₀ := fun _ hx ↦ (contMDiffAt_inv₀ (I := I) (n := n) hx).continuousAt } @[deprecated (since := "2025-09-01")] alias hasContinuousInv₀_of_hasContMDiffInv₀ := continuousInv₀_of_contMDiffInv₀ theorem contMDiffOn_inv₀ : ContMDiffOn I I n (Inv.inv : G → G) {0}ᶜ := fun _x hx => (contMDiffAt_inv₀ hx).contMDiffWithinAt variable {s : Set M} {a : M} theorem ContMDiffWithinAt.inv₀ (hf : ContMDiffWithinAt I' I n f s a) (ha : f a ≠ 0) : ContMDiffWithinAt I' I n (fun x => (f x)⁻¹) s a := (contMDiffAt_inv₀ ha).comp_contMDiffWithinAt a hf theorem ContMDiffAt.inv₀ (hf : ContMDiffAt I' I n f a) (ha : f a ≠ 0) : ContMDiffAt I' I n (fun x ↦ (f x)⁻¹) a := (contMDiffAt_inv₀ ha).comp a hf theorem ContMDiff.inv₀ (hf : ContMDiff I' I n f) (h0 : ∀ x, f x ≠ 0) : ContMDiff I' I n (fun x ↦ (f x)⁻¹) := fun x ↦ ContMDiffAt.inv₀ (hf x) (h0 x) theorem ContMDiffOn.inv₀ (hf : ContMDiffOn I' I n f s) (h0 : ∀ x ∈ s, f x ≠ 0) : ContMDiffOn I' I n (fun x => (f x)⁻¹) s := fun x hx ↦ ContMDiffWithinAt.inv₀ (hf x hx) (h0 x hx) end ContMDiffInv₀ /-! ### Point-wise division of `C^n` functions If `[ContMDiffMul I n N]` and `[ContMDiffInv₀ I n N]`, point-wise division of `C^n` functions `f : M → N` is `C^n` whenever the denominator is non-zero. (This includes `N` being a completely normed field.) -/ section Div variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {I : ModelWithCorners 𝕜 E H} {G : Type*} [TopologicalSpace G] [ChartedSpace H G] [GroupWithZero G] [ContMDiffInv₀ I n G] [ContMDiffMul I n G] {E' : Type*} [NormedAddCommGroup E'] [NormedSpace 𝕜 E'] {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners 𝕜 E' H'} {M : Type*} [TopologicalSpace M] [ChartedSpace H' M] {f g : M → G} {s : Set M} {a : M} theorem ContMDiffWithinAt.div₀ (hf : ContMDiffWithinAt I' I n f s a) (hg : ContMDiffWithinAt I' I n g s a) (h₀ : g a ≠ 0) : ContMDiffWithinAt I' I n (f / g) s a := by simpa [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) theorem ContMDiffOn.div₀ (hf : ContMDiffOn I' I n f s) (hg : ContMDiffOn I' I n g s) (h₀ : ∀ x ∈ s, g x ≠ 0) : ContMDiffOn I' I n (f / g) s := by simpa [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) theorem ContMDiffAt.div₀ (hf : ContMDiffAt I' I n f a) (hg : ContMDiffAt I' I n g a) (h₀ : g a ≠ 0) : ContMDiffAt I' I n (f / g) a := by simpa [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) theorem ContMDiff.div₀ (hf : ContMDiff I' I n f) (hg : ContMDiff I' I n g) (h₀ : ∀ x, g x ≠ 0) : ContMDiff I' I n (f / g) := by simpa only [div_eq_mul_inv] using hf.mul (hg.inv₀ h₀) end Div
.lake/packages/mathlib/Mathlib/Geometry/Manifold/Algebra/Structures.lean
import Mathlib.Geometry.Manifold.Algebra.LieGroup /-! # `C^n` structures In this file we define `C^n` structures that build on Lie groups. We prefer using the term `ContMDiffRing` instead of Lie mainly because Lie ring has currently another use in mathematics. -/ open scoped Manifold ContDiff section ContMDiffRing variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {H : Type*} [TopologicalSpace H] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {n : WithTop ℕ∞} -- See note [Design choices about smooth algebraic structures] /-- A `C^n` (semi)ring is a (semi)ring `R` where addition and multiplication are `C^n`. If `R` is a ring, then negation is automatically `C^n`, as it is multiplication with `-1`. -/ class ContMDiffRing (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) (R : Type*) [Semiring R] [TopologicalSpace R] [ChartedSpace H R] : Prop extends ContMDiffAdd I n R where contMDiff_mul : ContMDiff (I.prod I) I n fun p : R × R => p.1 * p.2 -- see Note [lower instance priority] instance (priority := 100) ContMDiffRing.toContMDiffMul (I : ModelWithCorners 𝕜 E H) (R : Type*) [Semiring R] [TopologicalSpace R] [ChartedSpace H R] [h : ContMDiffRing I n R] : ContMDiffMul I n R := { h with } -- see Note [lower instance priority] instance (priority := 100) ContMDiffRing.toLieAddGroup (I : ModelWithCorners 𝕜 E H) (R : Type*) [Ring R] [TopologicalSpace R] [ChartedSpace H R] [ContMDiffRing I n R] : LieAddGroup I n R where compatible := StructureGroupoid.compatible (contDiffGroupoid n I) contMDiff_add := contMDiff_add I n contMDiff_neg := by simpa only [neg_one_mul] using contMDiff_mul_left (G := R) (a := -1) end ContMDiffRing -- see Note [lower instance priority] instance (priority := 100) instFieldContMDiffRing {𝕜 : Type*} [NontriviallyNormedField 𝕜] {n : WithTop ℕ∞} : ContMDiffRing 𝓘(𝕜) n 𝕜 := { instNormedSpaceLieAddGroup with contMDiff_mul := by rw [contMDiff_iff] refine ⟨continuous_mul, fun x y => ?_⟩ simp only [mfld_simps] rw [contDiffOn_univ] exact contDiff_mul } variable {𝕜 R E H : Type*} [TopologicalSpace R] [TopologicalSpace H] [NontriviallyNormedField 𝕜] [NormedAddCommGroup E] [NormedSpace 𝕜 E] [ChartedSpace H R] (I : ModelWithCorners 𝕜 E H) (n : WithTop ℕ∞) /-- A `C^n` (semi)ring is a topological (semi)ring. This is not an instance for technical reasons, see note [Design choices about smooth algebraic structures]. -/ theorem topologicalSemiring_of_contMDiffRing [Semiring R] [ContMDiffRing I n R] : IsTopologicalSemiring R := { continuousMul_of_contMDiffMul I n, continuousAdd_of_contMDiffAdd I n with }
.lake/packages/mathlib/Mathlib/InformationTheory/Hamming.lean
import Mathlib.Analysis.Normed.Group.Basic /-! # Hamming spaces The Hamming metric counts the number of places two members of a (finite) Pi type differ. The Hamming norm is the same as the Hamming metric over additive groups, and counts the number of places a member of a (finite) Pi type differs from zero. This is a useful notion in various applications, but in particular it is relevant in coding theory, in which it is fundamental for defining the minimum distance of a code. ## Main definitions * `hammingDist x y`: the Hamming distance between `x` and `y`, the number of entries which differ. * `hammingNorm x`: the Hamming norm of `x`, the number of non-zero entries. * `Hamming β`: a type synonym for `Π i, β i` with `dist` and `norm` provided by the above. * `Hamming.toHamming`, `Hamming.ofHamming`: functions for casting between `Hamming β` and `Π i, β i`. * the Hamming norm forms a normed group on `Hamming β`. -/ section HammingDistNorm open Finset Function variable {α ι : Type*} {β : ι → Type*} [Fintype ι] [∀ i, DecidableEq (β i)] variable {γ : ι → Type*} [∀ i, DecidableEq (γ i)] /-- The Hamming distance function to the naturals. -/ def hammingDist (x y : ∀ i, β i) : ℕ := #{i | x i ≠ y i} /-- Corresponds to `dist_self`. -/ @[simp] theorem hammingDist_self (x : ∀ i, β i) : hammingDist x x = 0 := by rw [hammingDist, card_eq_zero, filter_eq_empty_iff] exact fun _ _ H => H rfl /-- Corresponds to `dist_nonneg`. -/ theorem hammingDist_nonneg {x y : ∀ i, β i} : 0 ≤ hammingDist x y := zero_le _ /-- Corresponds to `dist_comm`. -/ theorem hammingDist_comm (x y : ∀ i, β i) : hammingDist x y = hammingDist y x := by simp_rw [hammingDist, ne_comm] /-- Corresponds to `dist_triangle`. -/ theorem hammingDist_triangle (x y z : ∀ i, β i) : hammingDist x z ≤ hammingDist x y + hammingDist y z := by classical unfold hammingDist refine le_trans (card_mono ?_) (card_union_le _ _) rw [← filter_or] exact monotone_filter_right _ fun i _ h ↦ (h.ne_or_ne _).imp_right Ne.symm /-- Corresponds to `dist_triangle_left`. -/ theorem hammingDist_triangle_left (x y z : ∀ i, β i) : hammingDist x y ≤ hammingDist z x + hammingDist z y := by rw [hammingDist_comm z] exact hammingDist_triangle _ _ _ /-- Corresponds to `dist_triangle_right`. -/ theorem hammingDist_triangle_right (x y z : ∀ i, β i) : hammingDist x y ≤ hammingDist x z + hammingDist y z := by rw [hammingDist_comm y] exact hammingDist_triangle _ _ _ /-- Corresponds to `swap_dist`. -/ theorem swap_hammingDist : swap (@hammingDist _ β _ _) = hammingDist := by funext x y exact hammingDist_comm _ _ /-- Corresponds to `eq_of_dist_eq_zero`. -/ theorem eq_of_hammingDist_eq_zero {x y : ∀ i, β i} : hammingDist x y = 0 → x = y := by simp_rw [hammingDist, card_eq_zero, filter_eq_empty_iff, Classical.not_not, funext_iff, mem_univ, forall_true_left, imp_self] /-- Corresponds to `dist_eq_zero`. -/ @[simp] theorem hammingDist_eq_zero {x y : ∀ i, β i} : hammingDist x y = 0 ↔ x = y := ⟨eq_of_hammingDist_eq_zero, fun H => by rw [H] exact hammingDist_self _⟩ /-- Corresponds to `zero_eq_dist`. -/ @[simp] theorem hamming_zero_eq_dist {x y : ∀ i, β i} : 0 = hammingDist x y ↔ x = y := by rw [eq_comm, hammingDist_eq_zero] /-- Corresponds to `dist_ne_zero`. -/ theorem hammingDist_ne_zero {x y : ∀ i, β i} : hammingDist x y ≠ 0 ↔ x ≠ y := hammingDist_eq_zero.not /-- Corresponds to `dist_pos`. -/ @[simp] theorem hammingDist_pos {x y : ∀ i, β i} : 0 < hammingDist x y ↔ x ≠ y := by rw [← hammingDist_ne_zero, iff_not_comm, not_lt, Nat.le_zero] theorem hammingDist_lt_one {x y : ∀ i, β i} : hammingDist x y < 1 ↔ x = y := by rw [Nat.lt_one_iff, hammingDist_eq_zero] theorem hammingDist_le_card_fintype {x y : ∀ i, β i} : hammingDist x y ≤ Fintype.card ι := card_le_univ _ theorem hammingDist_comp_le_hammingDist (f : ∀ i, γ i → β i) {x y : ∀ i, γ i} : hammingDist (fun i => f i (x i)) (fun i => f i (y i)) ≤ hammingDist x y := by dsimp [hammingDist]; gcongr; simp +contextual theorem hammingDist_comp (f : ∀ i, γ i → β i) {x y : ∀ i, γ i} (hf : ∀ i, Injective (f i)) : hammingDist (fun i => f i (x i)) (fun i => f i (y i)) = hammingDist x y := le_antisymm (hammingDist_comp_le_hammingDist _) <| by dsimp [hammingDist]; gcongr; exact @hf _ _ _ theorem hammingDist_smul_le_hammingDist [∀ i, SMul α (β i)] {k : α} {x y : ∀ i, β i} : hammingDist (k • x) (k • y) ≤ hammingDist x y := hammingDist_comp_le_hammingDist fun i => (k • · : β i → β i) /-- Corresponds to `dist_smul` with the discrete norm on `α`. -/ theorem hammingDist_smul [∀ i, SMul α (β i)] {k : α} {x y : ∀ i, β i} (hk : ∀ i, IsSMulRegular (β i) k) : hammingDist (k • x) (k • y) = hammingDist x y := hammingDist_comp (fun i => (k • · : β i → β i)) hk section Zero variable [∀ i, Zero (β i)] [∀ i, Zero (γ i)] /-- The Hamming weight function to the naturals. -/ def hammingNorm (x : ∀ i, β i) : ℕ := #{i | x i ≠ 0} /-- Corresponds to `dist_zero_right`. -/ @[simp] theorem hammingDist_zero_right (x : ∀ i, β i) : hammingDist x 0 = hammingNorm x := rfl /-- Corresponds to `dist_zero_left`. -/ @[simp] theorem hammingDist_zero_left : hammingDist (0 : ∀ i, β i) = hammingNorm := funext fun x => by rw [hammingDist_comm, hammingDist_zero_right] /-- Corresponds to `norm_nonneg`. -/ theorem hammingNorm_nonneg {x : ∀ i, β i} : 0 ≤ hammingNorm x := zero_le _ /-- Corresponds to `norm_zero`. -/ @[simp] theorem hammingNorm_zero : hammingNorm (0 : ∀ i, β i) = 0 := hammingDist_self _ /-- Corresponds to `norm_eq_zero`. -/ @[simp] theorem hammingNorm_eq_zero {x : ∀ i, β i} : hammingNorm x = 0 ↔ x = 0 := hammingDist_eq_zero /-- Corresponds to `norm_ne_zero_iff`. -/ theorem hammingNorm_ne_zero_iff {x : ∀ i, β i} : hammingNorm x ≠ 0 ↔ x ≠ 0 := hammingNorm_eq_zero.not /-- Corresponds to `norm_pos_iff`. -/ @[simp] theorem hammingNorm_pos_iff {x : ∀ i, β i} : 0 < hammingNorm x ↔ x ≠ 0 := hammingDist_pos theorem hammingNorm_lt_one {x : ∀ i, β i} : hammingNorm x < 1 ↔ x = 0 := hammingDist_lt_one theorem hammingNorm_le_card_fintype {x : ∀ i, β i} : hammingNorm x ≤ Fintype.card ι := hammingDist_le_card_fintype theorem hammingNorm_comp_le_hammingNorm (f : ∀ i, γ i → β i) {x : ∀ i, γ i} (hf : ∀ i, f i 0 = 0) : (hammingNorm fun i => f i (x i)) ≤ hammingNorm x := by simpa only [← hammingDist_zero_right, hf] using hammingDist_comp_le_hammingDist f (y := fun _ ↦ 0) theorem hammingNorm_comp (f : ∀ i, γ i → β i) {x : ∀ i, γ i} (hf₁ : ∀ i, Injective (f i)) (hf₂ : ∀ i, f i 0 = 0) : (hammingNorm fun i => f i (x i)) = hammingNorm x := by simpa only [← hammingDist_zero_right, hf₂] using hammingDist_comp f hf₁ (y := fun _ ↦ 0) theorem hammingNorm_smul_le_hammingNorm [Zero α] [∀ i, SMulWithZero α (β i)] {k : α} {x : ∀ i, β i} : hammingNorm (k • x) ≤ hammingNorm x := hammingNorm_comp_le_hammingNorm (fun i (c : β i) => k • c) fun i => by simp_rw [smul_zero] theorem hammingNorm_smul [Zero α] [∀ i, SMulWithZero α (β i)] {k : α} (hk : ∀ i, IsSMulRegular (β i) k) (x : ∀ i, β i) : hammingNorm (k • x) = hammingNorm x := hammingNorm_comp (fun i (c : β i) => k • c) hk fun i => by simp_rw [smul_zero] end Zero /-- Corresponds to `dist_eq_norm`. -/ theorem hammingDist_eq_hammingNorm [∀ i, AddGroup (β i)] (x y : ∀ i, β i) : hammingDist x y = hammingNorm (x - y) := by simp_rw [hammingNorm, hammingDist, Pi.sub_apply, sub_ne_zero] end HammingDistNorm /-! ### The `Hamming` type synonym -/ /-- Type synonym for a Pi type which inherits the usual algebraic instances, but is equipped with the Hamming metric and norm, instead of `Pi.normedAddCommGroup` which uses the sup norm. -/ def Hamming {ι : Type*} (β : ι → Type*) : Type _ := ∀ i, β i namespace Hamming variable {α ι : Type*} {β : ι → Type*} /-! Instances inherited from normal Pi types. -/ instance [∀ i, Inhabited (β i)] : Inhabited (Hamming β) := ⟨fun _ => default⟩ instance [DecidableEq ι] [Fintype ι] [∀ i, Fintype (β i)] : Fintype (Hamming β) := Pi.instFintype instance [Inhabited ι] [∀ i, Nonempty (β i)] [Nontrivial (β default)] : Nontrivial (Hamming β) := Pi.nontrivial instance [Fintype ι] [∀ i, DecidableEq (β i)] : DecidableEq (Hamming β) := Fintype.decidablePiFintype instance [∀ i, Zero (β i)] : Zero (Hamming β) := Pi.instZero instance [∀ i, Neg (β i)] : Neg (Hamming β) := Pi.instNeg instance [∀ i, Add (β i)] : Add (Hamming β) := Pi.instAdd instance [∀ i, Sub (β i)] : Sub (Hamming β) := Pi.instSub instance [∀ i, SMul α (β i)] : SMul α (Hamming β) := Pi.instSMul instance [Zero α] [∀ i, Zero (β i)] [∀ i, SMulWithZero α (β i)] : SMulWithZero α (Hamming β) := Pi.smulWithZero _ instance [∀ i, AddMonoid (β i)] : AddMonoid (Hamming β) := Pi.addMonoid instance [∀ i, AddGroup (β i)] : AddGroup (Hamming β) := Pi.addGroup instance [∀ i, AddCommMonoid (β i)] : AddCommMonoid (Hamming β) := Pi.addCommMonoid instance [∀ i, AddCommGroup (β i)] : AddCommGroup (Hamming β) := Pi.addCommGroup instance (α) [Semiring α] (β : ι → Type*) [∀ i, AddCommMonoid (β i)] [∀ i, Module α (β i)] : Module α (Hamming β) := Pi.module _ _ _ /-! API to/from the type synonym. -/ /-- `Hamming.toHamming` is the identity function to the `Hamming` of a type. -/ @[match_pattern] def toHamming : (∀ i, β i) ≃ Hamming β := Equiv.refl _ /-- `Hamming.ofHamming` is the identity function from the `Hamming` of a type. -/ @[match_pattern] def ofHamming : Hamming β ≃ ∀ i, β i := Equiv.refl _ @[simp] theorem toHamming_symm_eq : (@toHamming _ β).symm = ofHamming := rfl @[simp] theorem ofHamming_symm_eq : (@ofHamming _ β).symm = toHamming := rfl @[simp] theorem toHamming_ofHamming (x : Hamming β) : toHamming (ofHamming x) = x := rfl @[simp] theorem ofHamming_toHamming (x : ∀ i, β i) : ofHamming (toHamming x) = x := rfl theorem toHamming_inj {x y : ∀ i, β i} : toHamming x = toHamming y ↔ x = y := Iff.rfl theorem ofHamming_inj {x y : Hamming β} : ofHamming x = ofHamming y ↔ x = y := Iff.rfl @[simp] theorem toHamming_zero [∀ i, Zero (β i)] : toHamming (0 : ∀ i, β i) = 0 := rfl @[simp] theorem ofHamming_zero [∀ i, Zero (β i)] : ofHamming (0 : Hamming β) = 0 := rfl @[simp] theorem toHamming_neg [∀ i, Neg (β i)] {x : ∀ i, β i} : toHamming (-x) = -toHamming x := rfl @[simp] theorem ofHamming_neg [∀ i, Neg (β i)] {x : Hamming β} : ofHamming (-x) = -ofHamming x := rfl @[simp] theorem toHamming_add [∀ i, Add (β i)] {x y : ∀ i, β i} : toHamming (x + y) = toHamming x + toHamming y := rfl @[simp] theorem ofHamming_add [∀ i, Add (β i)] {x y : Hamming β} : ofHamming (x + y) = ofHamming x + ofHamming y := rfl @[simp] theorem toHamming_sub [∀ i, Sub (β i)] {x y : ∀ i, β i} : toHamming (x - y) = toHamming x - toHamming y := rfl @[simp] theorem ofHamming_sub [∀ i, Sub (β i)] {x y : Hamming β} : ofHamming (x - y) = ofHamming x - ofHamming y := rfl @[simp] theorem toHamming_smul [∀ i, SMul α (β i)] {r : α} {x : ∀ i, β i} : toHamming (r • x) = r • toHamming x := rfl @[simp] theorem ofHamming_smul [∀ i, SMul α (β i)] {r : α} {x : Hamming β} : ofHamming (r • x) = r • ofHamming x := rfl section /-! Instances equipping `Hamming` with `hammingNorm` and `hammingDist`. -/ variable [Fintype ι] [∀ i, DecidableEq (β i)] instance : Dist (Hamming β) := ⟨fun x y => hammingDist (ofHamming x) (ofHamming y)⟩ @[simp, push_cast] theorem dist_eq_hammingDist (x y : Hamming β) : dist x y = hammingDist (ofHamming x) (ofHamming y) := rfl instance : PseudoMetricSpace (Hamming β) where dist_self := by push_cast exact mod_cast hammingDist_self dist_comm := by push_cast exact mod_cast hammingDist_comm dist_triangle := by push_cast exact mod_cast hammingDist_triangle toUniformSpace := ⊥ uniformity_dist := uniformity_dist_of_mem_uniformity _ _ fun s => by push_cast constructor · refine fun hs ↦ ⟨1, zero_lt_one, fun hab ↦ hs <| by simpa using hab⟩ · rintro ⟨_, hε, hs⟩ ⟨_, _⟩ rfl refine hs (lt_of_eq_of_lt ?_ hε) exact mod_cast hammingDist_self _ toBornology := ⟨⊥, bot_le⟩ cobounded_sets := by ext push_cast refine iff_of_true (Filter.mem_sets.mpr Filter.mem_bot) ⟨Fintype.card ι, fun _ _ _ _ => ?_⟩ exact mod_cast hammingDist_le_card_fintype @[simp, push_cast] theorem nndist_eq_hammingDist (x y : Hamming β) : nndist x y = hammingDist (ofHamming x) (ofHamming y) := rfl instance : DiscreteTopology (Hamming β) := ⟨rfl⟩ instance : MetricSpace (Hamming β) := .ofT0PseudoMetricSpace _ instance [∀ i, Zero (β i)] : Norm (Hamming β) := ⟨fun x => hammingNorm (ofHamming x)⟩ @[simp, push_cast] theorem norm_eq_hammingNorm [∀ i, Zero (β i)] (x : Hamming β) : ‖x‖ = hammingNorm (ofHamming x) := rfl instance [∀ i, AddGroup (β i)] : NormedAddGroup (Hamming β) where dist_eq := by push_cast; exact mod_cast hammingDist_eq_hammingNorm instance [∀ i, AddCommGroup (β i)] : NormedAddCommGroup (Hamming β) where dist_eq := fun x y => NormedAddGroup.dist_eq x y @[simp, push_cast] theorem nnnorm_eq_hammingNorm [∀ i, AddGroup (β i)] (x : Hamming β) : ‖x‖₊ = hammingNorm (ofHamming x) := rfl end end Hamming
.lake/packages/mathlib/Mathlib/InformationTheory/KullbackLeibler/Basic.lean
import Mathlib.InformationTheory.KullbackLeibler.KLFun import Mathlib.MeasureTheory.Measure.Decomposition.IntegralRNDeriv /-! # Kullback-Leibler divergence The Kullback-Leibler divergence is a measure of the difference between two measures. ## Main definitions * `klDiv μ ν`: Kullback-Leibler divergence between two measures, with value in `ℝ≥0∞`, defined as `∞` if `μ` is not absolutely continuous with respect to `ν` or if the log-likelihood ratio `llr μ ν` is not integrable with respect to `μ`, and by `ENNReal.ofReal (∫ x, llr μ ν x ∂μ + ν.real - μ.real univ)` otherwise. Note that our Kullback-Leibler divergence is nonnegative by definition (it takes value in `ℝ≥0∞`). However `∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ` is nonnegative for all finite measures `μ ≪ ν`, as proved in the lemma `integral_llr_add_sub_measure_univ_nonneg`. That lemma is our version of Gibbs' inequality ("the Kullback-Leibler divergence is nonnegative"). ## Main statements * `klDiv_eq_zero_iff` : the Kullback-Leibler divergence between two finite measures is zero if and only if the two measures are equal. ## Implementation details The Kullback-Leibler divergence on probability measures is `∫ x, llr μ ν x ∂μ` if `μ ≪ ν` (and the log-likelihood ratio is integrable) and `∞` otherwise. The definition we use extends this to finite measures by introducing a correction term `ν.real univ - μ.real univ`. The definition of the divergence thus uses the formula `∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ`, which is nonnegative for all finite measures `μ ≪ ν`. This also makes `klDiv μ ν` equal to an f-divergence: it equals the integral `∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν`, in which `klFun x = x * log x + 1 - x`. -/ open Real MeasureTheory Set open scoped ENNReal namespace InformationTheory variable {α : Type*} {mα : MeasurableSpace α} {μ ν : Measure α} open Classical in /-- Kullback-Leibler divergence between two measures. -/ noncomputable irreducible_def klDiv (μ ν : Measure α) : ℝ≥0∞ := if μ ≪ ν ∧ Integrable (llr μ ν) μ then ENNReal.ofReal (∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ) else ∞ lemma klDiv_of_ac_of_integrable (h1 : μ ≪ ν) (h2 : Integrable (llr μ ν) μ) : klDiv μ ν = ENNReal.ofReal (∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ) := by rw [klDiv_def] exact if_pos ⟨h1, h2⟩ @[simp] lemma klDiv_of_not_ac (h : ¬ μ ≪ ν) : klDiv μ ν = ∞ := by rw [klDiv_def] exact if_neg (not_and_of_not_left _ h) @[simp] lemma klDiv_of_not_integrable (h : ¬ Integrable (llr μ ν) μ) : klDiv μ ν = ∞ := by rw [klDiv_def] exact if_neg (not_and_of_not_right _ h) @[simp] lemma klDiv_self (μ : Measure α) [SigmaFinite μ] : klDiv μ μ = 0 := by have h := llr_self μ rw [klDiv_def, if_pos] · simp [integral_congr_ae h] · rw [integrable_congr h] exact ⟨Measure.AbsolutelyContinuous.rfl, integrable_zero _ _ μ⟩ @[simp] lemma klDiv_zero_left [IsFiniteMeasure ν] : klDiv 0 ν = ν univ := by convert klDiv_of_ac_of_integrable (Measure.AbsolutelyContinuous.zero _) integrable_zero_measure simp @[simp] lemma klDiv_zero_right [NeZero μ] : klDiv μ 0 = ∞ := klDiv_of_not_ac (Measure.absolutelyContinuous_zero_iff.mp.mt (NeZero.ne _)) lemma klDiv_eq_top_iff : klDiv μ ν = ∞ ↔ μ ≪ ν → ¬ Integrable (llr μ ν) μ := by constructor <;> intro h · contrapose! h simp [klDiv_of_ac_of_integrable h.1 h.2] · rcases or_not_of_imp h with (h | h) <;> simp [h] lemma klDiv_ne_top_iff : klDiv μ ν ≠ ∞ ↔ μ ≪ ν ∧ Integrable (llr μ ν) μ := by simp [ne_eq, klDiv_eq_top_iff] section AlternativeFormulas variable [IsFiniteMeasure μ] [IsFiniteMeasure ν] open Classical in lemma klDiv_eq_integral_klFun : klDiv μ ν = if μ ≪ ν ∧ Integrable (llr μ ν) μ then ENNReal.ofReal (∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν) else ∞ := by rw [klDiv_def] exact if_ctx_congr Iff.rfl (fun h ↦ by rw [integral_klFun_rnDeriv h.1 h.2]) fun _ ↦ rfl open Classical in lemma klDiv_eq_lintegral_klFun : klDiv μ ν = if μ ≪ ν then ∫⁻ x, ENNReal.ofReal (klFun (μ.rnDeriv ν x).toReal) ∂ν else ∞ := by rw [klDiv_eq_integral_klFun] by_cases hμν : μ ≪ ν swap; · simp [hμν] have h_int_iff := lintegral_ofReal_ne_top_iff_integrable (f := fun x ↦ klFun (μ.rnDeriv ν x).toReal) (μ := ν) ?_ ?_ rotate_left · exact Measurable.aestronglyMeasurable (by fun_prop) · exact ae_of_all _ fun _ ↦ klFun_nonneg ENNReal.toReal_nonneg by_cases h_int : Integrable (llr μ ν) μ · simp only [hμν, h_int, and_self, ↓reduceIte] rw [ofReal_integral_eq_lintegral_ofReal] · rwa [integrable_klFun_rnDeriv_iff hμν] · exact ae_of_all _ fun _ ↦ klFun_nonneg ENNReal.toReal_nonneg · rw [← not_iff_not, ne_eq, Decidable.not_not] at h_int_iff symm simp [hμν, h_int, h_int_iff, integrable_klFun_rnDeriv_iff hμν] end AlternativeFormulas section Real variable [IsFiniteMeasure μ] [IsFiniteMeasure ν] /-- **Gibbs' inequality**: the Kullback-Leibler divergence is nonnegative. Note that since `klDiv` takes value in `ℝ≥0∞` (defined when it is finite as `ENNReal.ofReal (...)`), it is nonnegative by definition. This lemma proves that the argument of `ENNReal.ofReal` is also nonnegative. -/ lemma integral_llr_add_sub_measure_univ_nonneg (hμν : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : 0 ≤ ∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ := by rw [← integral_klFun_rnDeriv hμν h_int] exact integral_nonneg fun x ↦ klFun_nonneg ENNReal.toReal_nonneg lemma toReal_klDiv (h : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : (klDiv μ ν).toReal = ∫ a, llr μ ν a ∂μ + ν.real univ - μ.real univ := by rw [klDiv_of_ac_of_integrable h h_int, ENNReal.toReal_ofReal] exact integral_llr_add_sub_measure_univ_nonneg h h_int /-- If `μ ≪ ν` and `μ univ = ν univ`, then `toReal` of the Kullback-Leibler divergence is equal to an integral, without any integrability condition. -/ lemma toReal_klDiv_of_measure_eq (h : μ ≪ ν) (h_eq : μ univ = ν univ) : (klDiv μ ν).toReal = ∫ a, llr μ ν a ∂μ := by by_cases h_int : Integrable (llr μ ν) μ · simp [toReal_klDiv h h_int, h_eq, measureReal_def] · rw [klDiv_of_not_integrable h_int, integral_undef h_int, ENNReal.toReal_top] lemma toReal_klDiv_eq_integral_klFun (h : μ ≪ ν) : (klDiv μ ν).toReal = ∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν := by by_cases h_int : Integrable (llr μ ν) μ · rw [klDiv_eq_integral_klFun, if_pos ⟨h, h_int⟩, ENNReal.toReal_ofReal] exact integral_nonneg fun _ ↦ klFun_nonneg ENNReal.toReal_nonneg · rw [integral_undef] · rw [klDiv_of_not_integrable h_int, ENNReal.toReal_top] · rwa [integrable_klFun_rnDeriv_iff h] end Real section Inequalities variable [IsFiniteMeasure μ] [IsFiniteMeasure ν] lemma integral_llr_add_mul_log_nonneg (hμν : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : 0 ≤ ∫ x, llr μ ν x ∂μ + μ.real univ * log (ν.real univ) + 1 - μ.real univ := by by_cases hμ : μ = 0 · simp [hμ] by_cases hν : ν = 0 · refine absurd ?_ hμ rw [hν] at hμν exact Measure.absolutelyContinuous_zero_iff.mp hμν have : NeZero ν := ⟨hν⟩ let ν' := (ν univ)⁻¹ • ν have hμν' : μ ≪ ν' := hμν.trans (Measure.absolutelyContinuous_smul (by simp)) have h := integral_llr_add_sub_measure_univ_nonneg hμν' ?_ swap · rw [integrable_congr (llr_smul_right hμν (ν univ)⁻¹ (by simp) (by simp [hν]))] exact h_int.sub (integrable_const _) rw [integral_congr_ae (llr_smul_right hμν (ν univ)⁻¹ (by simp) (by simp [hν])), integral_sub h_int (integrable_const _), integral_const, smul_eq_mul] at h simpa using h lemma mul_klFun_le_toReal_klDiv (hμν : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : ν.real univ * klFun (μ.real univ / ν.real univ) ≤ (klDiv μ ν).toReal := by calc ν.real univ * klFun (μ.real univ / ν.real univ) _ ≤ ∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν := by refine mul_le_integral_rnDeriv_of_ac convexOn_klFun continuous_klFun.continuousWithinAt ?_ hμν rwa [integrable_klFun_rnDeriv_iff hμν] _ = (klDiv μ ν).toReal := by rw [toReal_klDiv_eq_integral_klFun hμν] lemma mul_log_le_toReal_klDiv (hμν : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : μ.real univ * log (μ.real univ / ν.real univ) + ν.real univ - μ.real univ ≤ (klDiv μ ν).toReal := by by_cases hμ : μ = 0 · simp [hμ, measureReal_def] by_cases hν : ν = 0 · refine absurd ?_ hμ rw [hν] at hμν exact Measure.absolutelyContinuous_zero_iff.mp hμν refine (le_of_eq ?_).trans (mul_klFun_le_toReal_klDiv hμν h_int) have : ν.real univ * (μ.real univ / ν.real univ) = μ.real univ := by rw [mul_div_cancel₀]; simp [ENNReal.toReal_eq_zero_iff, hν, measureReal_def] rw [klFun, mul_sub, mul_add, mul_one, ← mul_assoc, this] lemma mul_log_le_klDiv (μ ν : Measure α) [IsFiniteMeasure μ] [IsFiniteMeasure ν] : ENNReal.ofReal (μ.real univ * log (μ.real univ / ν.real univ) + ν.real univ - μ.real univ) ≤ klDiv μ ν := by by_cases hμν : μ ≪ ν swap; · simp [hμν] by_cases h_int : Integrable (llr μ ν) μ swap; · simp [h_int] rw [← ENNReal.ofReal_toReal (a := klDiv μ ν)] · exact ENNReal.ofReal_le_ofReal (mul_log_le_toReal_klDiv hμν h_int) · rw [klDiv_ne_top_iff] exact ⟨hμν, h_int⟩ end Inequalities /-- **Converse Gibbs' inequality**: the Kullback-Leibler divergence between two finite measures is zero if and only if the two measures are equal. -/ lemma klDiv_eq_zero_iff [IsFiniteMeasure μ] [IsFiniteMeasure ν] : klDiv μ ν = 0 ↔ μ = ν := by refine ⟨fun h ↦ ?_, fun h ↦ h ▸ klDiv_self _⟩ have h_ne : klDiv μ ν ≠ ⊤ := by simp [h] rw [klDiv_ne_top_iff] at h_ne rw [klDiv_eq_lintegral_klFun, if_pos h_ne.1, lintegral_eq_zero_iff (by fun_prop)] at h refine (Measure.rnDeriv_eq_one_iff_eq h_ne.1).mp ?_ filter_upwards [h] with x hx simp only [Pi.zero_apply, ENNReal.ofReal_eq_zero] at hx have hx' : klFun (μ.rnDeriv ν x).toReal = 0 := le_antisymm hx (klFun_nonneg ENNReal.toReal_nonneg) rwa [klFun_eq_zero_iff ENNReal.toReal_nonneg, ENNReal.toReal_eq_one_iff] at hx' end InformationTheory
.lake/packages/mathlib/Mathlib/InformationTheory/KullbackLeibler/KLFun.lean
import Mathlib.Analysis.SpecialFunctions.Log.NegMulLog import Mathlib.MeasureTheory.Measure.Decomposition.IntegralRNDeriv import Mathlib.MeasureTheory.Measure.LogLikelihoodRatio /-! # The real function `fun x ↦ x * log x + 1 - x` We define `klFun x = x * log x + 1 - x`. That function is notable because the Kullback-Leibler divergence is an f-divergence for `klFun`. That is, the Kullback-Leibler divergence is an integral of `klFun` composed with a Radon-Nikodym derivative. For probability measures, any function `f` that differs from `klFun` by an affine function of the form `x ↦ a * (x - 1)` would give the same value for the integral `∫ x, f (μ.rnDeriv ν x).toReal ∂ν`. However, `klFun` is the particular choice among those that satisfies `klFun 1 = 0` and `deriv klFun 1 = 0`, which ensures that desirable properties of the Kullback-Leibler divergence extend to other finite measures: it is nonnegative and zero iff the two measures are equal. ## Main definitions * `klFun`: the function `fun x : ℝ ↦ x * log x + 1 - x`. This is a continuous nonnegative, strictly convex function on [0,∞), with minimum value 0 at 1. ## Main statements * `integrable_klFun_rnDeriv_iff`: For two finite measures `μ ≪ ν`, the function `x ↦ klFun (μ.rnDeriv ν x).toReal` is integrable with respect to `ν` iff the log-likelihood ratio `llr μ ν` is integrable with respect to `μ`. * `integral_klFun_rnDeriv`: For two finite measures `μ ≪ ν` such that `llr μ ν` is integrable with respect to `μ`, `∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν = ∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ`. -/ open Real MeasureTheory Filter Set namespace InformationTheory variable {α : Type*} {mα : MeasurableSpace α} {μ ν : Measure α} {x : ℝ} /-- The function `x : ℝ ↦ x * log x + 1 - x`. The Kullback-Leibler divergence is an f-divergence for this function. -/ noncomputable def klFun (x : ℝ) : ℝ := x * log x + 1 - x lemma klFun_apply (x : ℝ) : klFun x = x * log x + 1 - x := rfl lemma klFun_zero : klFun 0 = 1 := by simp [klFun] lemma klFun_one : klFun 1 = 0 := by simp [klFun] /-- `klFun` is strictly convex on [0,∞). -/ lemma strictConvexOn_klFun : StrictConvexOn ℝ (Ici 0) klFun := (strictConvexOn_mul_log.add_convexOn (convexOn_const _ (convex_Ici _))).sub_concaveOn (concaveOn_id (convex_Ici _)) /-- `klFun` is convex on [0,∞). -/ lemma convexOn_klFun : ConvexOn ℝ (Ici 0) klFun := strictConvexOn_klFun.convexOn /-- `klFun` is convex on (0,∞). This is an often useful consequence of `convexOn_klFun`, which states convexity on [0, ∞). -/ lemma convexOn_Ioi_klFun : ConvexOn ℝ (Ioi 0) klFun := convexOn_klFun.subset (Ioi_subset_Ici le_rfl) (convex_Ioi _) /-- `klFun` is continuous. -/ @[continuity, fun_prop] lemma continuous_klFun : Continuous klFun := by unfold klFun; fun_prop /-- `klFun` is measurable. -/ @[measurability, fun_prop] lemma measurable_klFun : Measurable klFun := continuous_klFun.measurable /-- `klFun` is strongly measurable. -/ @[measurability] lemma stronglyMeasurable_klFun : StronglyMeasurable klFun := measurable_klFun.stronglyMeasurable section Derivatives /-- The derivative of `klFun` at `x ≠ 0` is `log x`. -/ lemma hasDerivAt_klFun (hx : x ≠ 0) : HasDerivAt klFun (log x) x := by convert ((hasDerivAt_mul_log hx).add (hasDerivAt_const x 1)).sub (hasDerivAt_id x) using 1 ring lemma not_differentiableAt_klFun_zero : ¬ DifferentiableAt ℝ klFun 0 := by unfold klFun; simpa using not_DifferentiableAt_log_mul_zero /-- The derivative of `klFun` is `log x`. This also holds at `x = 0` although `klFun` is not differentiable there since the default value of `deriv` in that case is 0. -/ @[simp] lemma deriv_klFun : deriv klFun = log := by ext x by_cases h0 : x = 0 · simp only [h0, log_zero] exact deriv_zero_of_not_differentiableAt not_differentiableAt_klFun_zero · exact (hasDerivAt_klFun h0).deriv lemma not_differentiableWithinAt_klFun_Ioi_zero : ¬ DifferentiableWithinAt ℝ klFun (Ioi 0) 0 := by refine not_differentiableWithinAt_of_deriv_tendsto_atBot_Ioi _ ?_ rw [deriv_klFun] exact tendsto_log_nhdsGT_zero lemma not_differentiableWithinAt_klFun_Iio_zero : ¬ DifferentiableWithinAt ℝ klFun (Iio 0) 0 := by refine not_differentiableWithinAt_of_deriv_tendsto_atBot_Iio _ ?_ rw [deriv_klFun] exact tendsto_log_nhdsLT_zero /-- The right derivative of `klFun` is `log x`. This also holds at `x = 0` although `klFun` is not differentiable there since the default value of `derivWithin` in that case is 0. -/ @[simp] lemma rightDeriv_klFun : derivWithin klFun (Ioi x) x = log x := by by_cases h0 : x = 0 · simp only [h0, log_zero] exact derivWithin_zero_of_not_differentiableWithinAt not_differentiableWithinAt_klFun_Ioi_zero · exact (hasDerivAt_klFun h0).hasDerivWithinAt.derivWithin (uniqueDiffWithinAt_Ioi x) /-- The left derivative of `klFun` is `log x`. This also holds at `x = 0` although `klFun` is not differentiable there since the default value of `derivWithin` in that case is 0. -/ @[simp] lemma leftDeriv_klFun : derivWithin klFun (Iio x) x = log x := by by_cases h0 : x = 0 · simp only [h0, log_zero] exact derivWithin_zero_of_not_differentiableWithinAt not_differentiableWithinAt_klFun_Iio_zero · exact (hasDerivAt_klFun h0).hasDerivWithinAt.derivWithin (uniqueDiffWithinAt_Iio x) lemma rightDeriv_klFun_one : derivWithin klFun (Ioi 1) 1 = 0 := by simp lemma leftDeriv_klFun_one : derivWithin klFun (Iio 1) 1 = 0 := by simp lemma tendsto_rightDeriv_klFun_atTop : Tendsto (fun x ↦ derivWithin klFun (Ioi x) x) atTop atTop := by simp only [rightDeriv_klFun] exact tendsto_log_atTop end Derivatives lemma isMinOn_klFun : IsMinOn klFun (Ici 0) 1 := convexOn_klFun.isMinOn_of_rightDeriv_eq_zero (by simp) (by simp) /-- The function `klFun` is nonnegative on `[0,∞)`. -/ lemma klFun_nonneg (hx : 0 ≤ x) : 0 ≤ klFun x := klFun_one ▸ isMinOn_klFun hx lemma klFun_eq_zero_iff (hx : 0 ≤ x) : klFun x = 0 ↔ x = 1 := by refine ⟨fun h ↦ ?_, fun h ↦ by simp [klFun_apply, h]⟩ exact strictConvexOn_klFun.eq_of_isMinOn (isMinOn_iff.mpr fun y hy ↦ h ▸ klFun_nonneg hy) isMinOn_klFun hx (zero_le_one' ℝ) lemma tendsto_klFun_atTop : Tendsto klFun atTop atTop := by have : klFun = (fun x ↦ x * (log x - 1) + 1) := by unfold klFun; ext; ring rw [this] refine Tendsto.atTop_add ?_ tendsto_const_nhds refine tendsto_id.atTop_mul_atTop₀ ?_ exact tendsto_log_atTop.atTop_add tendsto_const_nhds section Integral variable [IsFiniteMeasure μ] [IsFiniteMeasure ν] /-- For two finite measures `μ ≪ ν`, the function `x ↦ klFun (μ.rnDeriv ν x).toReal` is integrable with respect to `ν` iff `llr μ ν` is integrable with respect to `μ`. -/ lemma integrable_klFun_rnDeriv_iff (hμν : μ ≪ ν) : Integrable (fun x ↦ klFun (μ.rnDeriv ν x).toReal) ν ↔ Integrable (llr μ ν) μ := by suffices Integrable (fun x ↦ (μ.rnDeriv ν x).toReal * log (μ.rnDeriv ν x).toReal + (1 - (μ.rnDeriv ν x).toReal)) ν ↔ Integrable (llr μ ν) μ by convert this using 3 with x rw [klFun, add_sub_assoc] rw [integrable_add_iff_integrable_left', integrable_rnDeriv_mul_log_iff hμν] fun_prop lemma integral_klFun_rnDeriv (hμν : μ ≪ ν) (h_int : Integrable (llr μ ν) μ) : ∫ x, klFun (μ.rnDeriv ν x).toReal ∂ν = ∫ x, llr μ ν x ∂μ + ν.real univ - μ.real univ := by unfold klFun rw [integral_sub, integral_add, integral_const, Measure.integral_toReal_rnDeriv hμν, smul_eq_mul, mul_one] · congr 2 exact integral_rnDeriv_smul hμν · rwa [integrable_rnDeriv_mul_log_iff hμν] · fun_prop · refine Integrable.add ?_ (integrable_const _) rwa [integrable_rnDeriv_mul_log_iff hμν] · fun_prop end Integral end InformationTheory
.lake/packages/mathlib/Mathlib/SetTheory/Lists.lean
import Mathlib.Algebra.Order.Group.Nat import Mathlib.Algebra.Order.Monoid.NatCast import Mathlib.Algebra.Ring.Nat import Mathlib.Data.Sigma.Basic /-! # A computable model of ZFA without infinity In this file we define finite hereditary lists. This is useful for calculations in naive set theory. We distinguish two kinds of ZFA lists: * Atoms. Directly correspond to an element of the original type. * Proper ZFA lists. Can be thought of (but are not implemented) as a list of ZFA lists (not necessarily proper). For example, `Lists ℕ` contains stuff like `23`, `[]`, `[37]`, `[1, [[2], 3], 4]`. ## Implementation note As we want to be able to append both atoms and proper ZFA lists to proper ZFA lists, it's handy that atoms and proper ZFA lists belong to the same type, even though atoms of `α` could be modelled as `α` directly. But we don't want to be able to append anything to atoms. This calls for a two-step definition of ZFA lists: * First, define ZFA prelists as atoms and proper ZFA prelists. Those proper ZFA prelists are defined by inductive appending of (not necessarily proper) ZFA lists. * Second, define ZFA lists by rubbing out the distinction between atoms and proper lists. ## Main declarations * `Lists' α false`: Atoms as ZFA prelists. Basically a copy of `α`. * `Lists' α true`: Proper ZFA prelists. Defined inductively from the empty ZFA prelist (`Lists'.nil`) and from appending a ZFA prelist to a proper ZFA prelist (`Lists'.cons a l`). * `Lists α`: ZFA lists. Sum of the atoms and proper ZFA prelists. * `Finsets α`: ZFA sets. Defined as `Lists` quotiented by `Lists.Equiv`, the extensional equivalence. -/ variable {α : Type*} /-- Prelists, helper type to define `Lists`. `Lists' α false` are the "atoms", a copy of `α`. `Lists' α true` are the "proper" ZFA prelists, inductively defined from the empty ZFA prelist and from appending a ZFA prelist to a proper ZFA prelist. It is made so that you can't append anything to an atom while having only one appending function for appending both atoms and proper ZFA prelists to a proper ZFA prelist. -/ inductive Lists'.{u} (α : Type u) : Bool → Type u | atom : α → Lists' α false | nil : Lists' α true | cons' {b} : Lists' α b → Lists' α true → Lists' α true deriving DecidableEq compile_inductive% Lists' /-- Hereditarily finite list, aka ZFA list. A ZFA list is either an "atom" (`b = false`), corresponding to an element of `α`, or a "proper" ZFA list, inductively defined from the empty ZFA list and from appending a ZFA list to a proper ZFA list. -/ def Lists (α : Type*) := Σ b, Lists' α b namespace Lists' instance [Inhabited α] : ∀ b, Inhabited (Lists' α b) | true => ⟨nil⟩ | false => ⟨atom default⟩ /-- Appending a ZFA list to a proper ZFA prelist. -/ def cons : Lists α → Lists' α true → Lists' α true | ⟨_, a⟩, l => cons' a l /-- Converts a ZFA prelist to a `List` of ZFA lists. Atoms are sent to `[]`. -/ @[simp] def toList : ∀ {b}, Lists' α b → List (Lists α) | _, atom _ => [] | _, nil => [] | _, cons' a l => ⟨_, a⟩ :: l.toList @[simp] theorem toList_cons (a : Lists α) (l) : toList (cons a l) = a :: l.toList := rfl /-- Converts a `List` of ZFA lists to a proper ZFA prelist. -/ @[simp] def ofList : List (Lists α) → Lists' α true | [] => nil | a :: l => cons a (ofList l) @[simp] theorem to_ofList (l : List (Lists α)) : toList (ofList l) = l := by induction l <;> simp [*] @[simp] theorem of_toList : ∀ l : Lists' α true, ofList (toList l) = l := suffices ∀ (b) (h : true = b) (l : Lists' α b), let l' : Lists' α true := by rw [h]; exact l ofList (toList l') = l' from this _ rfl fun b h l => by induction l with | atom => cases h | nil => simp | cons' b a _ IH => simpa [cons] using IH rfl /-- Recursion/induction principle for `Lists'.ofList`. -/ @[elab_as_elim] def recOfList {motive : Lists' α true → Sort*} (ofList : ∀ l, motive (ofList l)) : ∀ l, motive l := fun l ↦ cast (by simp) <| ofList (l.toList) end Lists' mutual /-- Equivalence of ZFA lists. Defined inductively. -/ inductive Lists.Equiv : Lists α → Lists α → Prop | refl (l) : Lists.Equiv l l | antisymm {l₁ l₂ : Lists' α true} : Lists'.Subset l₁ l₂ → Lists'.Subset l₂ l₁ → Lists.Equiv ⟨_, l₁⟩ ⟨_, l₂⟩ /-- Subset relation for ZFA lists. Defined inductively. -/ inductive Lists'.Subset : Lists' α true → Lists' α true → Prop | nil {l} : Lists'.Subset Lists'.nil l | cons {a a' l l'} : Lists.Equiv a a' → a' ∈ Lists'.toList l' → Lists'.Subset l l' → Lists'.Subset (Lists'.cons a l) l' end local infixl:50 " ~ " => Lists.Equiv namespace Lists' instance : HasSubset (Lists' α true) := ⟨Lists'.Subset⟩ /-- ZFA prelist membership. A ZFA list is in a ZFA prelist if some element of this ZFA prelist is equivalent as a ZFA list to this ZFA list. -/ instance {b} : Membership (Lists α) (Lists' α b) := ⟨fun l a => ∃ a' ∈ l.toList, a ~ a'⟩ theorem mem_def {b a} {l : Lists' α b} : a ∈ l ↔ ∃ a' ∈ l.toList, a ~ a' := Iff.rfl @[simp] theorem mem_cons {a y l} : a ∈ @cons α y l ↔ a ~ y ∨ a ∈ l := by simp [mem_def, or_and_right, exists_or] theorem cons_subset {a} {l₁ l₂ : Lists' α true} : Lists'.cons a l₁ ⊆ l₂ ↔ a ∈ l₂ ∧ l₁ ⊆ l₂ := by refine ⟨fun h => ?_, fun ⟨⟨a', m, e⟩, s⟩ => Subset.cons e m s⟩ generalize h' : Lists'.cons a l₁ = l₁' at h obtain - | @⟨a', _, _, _, e, m, s⟩ := h · cases a cases h' cases a; cases a'; cases h'; exact ⟨⟨_, m, e⟩, s⟩ theorem ofList_subset {l₁ l₂ : List (Lists α)} (h : l₁ ⊆ l₂) : Lists'.ofList l₁ ⊆ Lists'.ofList l₂ := by induction l₁ with | nil => exact Subset.nil | cons _ _ l₁_ih => refine Subset.cons (Lists.Equiv.refl _) ?_ (l₁_ih (List.subset_of_cons_subset h)) simp only [List.cons_subset] at h; simp [h] @[refl] theorem Subset.refl {l : Lists' α true} : l ⊆ l := by rw [← Lists'.of_toList l]; exact ofList_subset (List.Subset.refl _) theorem subset_nil {l : Lists' α true} : l ⊆ Lists'.nil → l = Lists'.nil := by rw [← of_toList l] induction toList l <;> intro h · rfl · rcases cons_subset.1 h with ⟨⟨_, ⟨⟩, _⟩, _⟩ theorem mem_of_subset' {a} : ∀ {l₁ l₂ : Lists' α true} (_ : l₁ ⊆ l₂) (_ : a ∈ l₁.toList), a ∈ l₂ | nil, _, Lists'.Subset.nil, h => by cases h | cons' a0 l0, l₂, s, h => by obtain - | ⟨e, m, s⟩ := s simp only [toList, Sigma.eta, List.mem_cons] at h rcases h with (rfl | h) · exact ⟨_, m, e⟩ · exact mem_of_subset' s h theorem subset_def {l₁ l₂ : Lists' α true} : l₁ ⊆ l₂ ↔ ∀ a ∈ l₁.toList, a ∈ l₂ := ⟨fun H _ => mem_of_subset' H, fun H => by induction l₁ using recOfList with | _ l₁ induction l₁ with | nil => exact Subset.nil | cons h t t_ih => simp only [to_ofList, ofList, toList_cons, List.mem_cons, forall_eq_or_imp] at * exact cons_subset.2 ⟨H.1, t_ih H.2⟩⟩ end Lists' namespace Lists /-- Sends `a : α` to the corresponding atom in `Lists α`. -/ @[match_pattern] def atom (a : α) : Lists α := ⟨_, Lists'.atom a⟩ /-- Converts a proper ZFA prelist to a ZFA list. -/ @[match_pattern] def of' (l : Lists' α true) : Lists α := ⟨_, l⟩ /-- Converts a ZFA list to a `List` of ZFA lists. Atoms are sent to `[]`. -/ @[simp] def toList : Lists α → List (Lists α) | ⟨_, l⟩ => l.toList /-- Predicate stating that a ZFA list is proper. -/ def IsList (l : Lists α) : Prop := l.1 /-- Converts a `List` of ZFA lists to a ZFA list. -/ def ofList (l : List (Lists α)) : Lists α := of' (Lists'.ofList l) theorem isList_toList (l : List (Lists α)) : IsList (ofList l) := Eq.refl _ theorem to_ofList (l : List (Lists α)) : toList (ofList l) = l := by simp [ofList, of'] theorem of_toList : ∀ {l : Lists α}, IsList l → ofList (toList l) = l | ⟨true, l⟩, _ => by simp_all [ofList, of'] instance : Inhabited (Lists α) := ⟨of' Lists'.nil⟩ instance [DecidableEq α] : DecidableEq (Lists α) := by unfold Lists; infer_instance instance [SizeOf α] : SizeOf (Lists α) := by unfold Lists; infer_instance /-- A recursion principle for pairs of ZFA lists and proper ZFA prelists. -/ def inductionMut (C : Lists α → Sort*) (D : Lists' α true → Sort*) (C0 : ∀ a, C (atom a)) (C1 : ∀ l, D l → C (of' l)) (D0 : D Lists'.nil) (D1 : ∀ a l, C a → D l → D (Lists'.cons a l)) : PProd (∀ l, C l) (∀ l, D l) := by suffices ∀ {b} (l : Lists' α b), PProd (C ⟨_, l⟩) (match b, l with | true, l => D l | false, _ => PUnit) by exact ⟨fun ⟨b, l⟩ => (this _).1, fun l => (this l).2⟩ intro b l induction l with | atom => exact ⟨C0 _, ⟨⟩⟩ | nil => exact ⟨C1 _ D0, D0⟩ | cons' a l IH₁ IH => have : D (Lists'.cons' a l) := D1 ⟨_, _⟩ _ IH₁.1 IH.2 exact ⟨C1 _ this, this⟩ /-- Membership of ZFA list. A ZFA list belongs to a proper ZFA list if it belongs to the latter as a proper ZFA prelist. An atom has no members. -/ def mem (a : Lists α) : Lists α → Prop | ⟨false, _⟩ => False | ⟨_, l⟩ => a ∈ l instance : Membership (Lists α) (Lists α) where mem ls l := mem l ls theorem isList_of_mem {a : Lists α} : ∀ {l : Lists α}, a ∈ l → IsList l | ⟨_, Lists'.nil⟩, _ => rfl | ⟨_, Lists'.cons' _ _⟩, _ => rfl theorem Equiv.antisymm_iff {l₁ l₂ : Lists' α true} : of' l₁ ~ of' l₂ ↔ l₁ ⊆ l₂ ∧ l₂ ⊆ l₁ := by refine ⟨fun h => ?_, fun ⟨h₁, h₂⟩ => Equiv.antisymm h₁ h₂⟩ obtain - | ⟨h₁, h₂⟩ := h · simp [Lists'.Subset.refl] · exact ⟨h₁, h₂⟩ attribute [refl] Equiv.refl theorem equiv_atom {a} {l : Lists α} : atom a ~ l ↔ atom a = l := ⟨fun h => by cases h; rfl, fun h => h ▸ Equiv.refl _⟩ @[symm] theorem Equiv.symm {l₁ l₂ : Lists α} (h : l₁ ~ l₂) : l₂ ~ l₁ := by obtain - | ⟨h₁, h₂⟩ := h <;> [rfl; exact Equiv.antisymm h₂ h₁] theorem Equiv.trans : ∀ {l₁ l₂ l₃ : Lists α}, l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃ := by let trans := fun l₁ : Lists α => ∀ ⦃l₂ l₃⦄, l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃ suffices PProd (∀ l₁, trans l₁) (∀ (l : Lists' α true), ∀ l' ∈ l.toList, trans l') by exact this.1 apply inductionMut · intro a l₂ l₃ h₁ h₂ rwa [← equiv_atom.1 h₁] at h₂ · intro l₁ IH l₂ l₃ h₁ h₂ obtain - | l₂ := id h₁ · exact h₂ obtain - | l₃ := id h₂ · exact h₁ obtain ⟨hl₁, hr₁⟩ := Equiv.antisymm_iff.1 h₁ obtain ⟨hl₂, hr₂⟩ := Equiv.antisymm_iff.1 h₂ apply Equiv.antisymm_iff.2; constructor <;> apply Lists'.subset_def.2 · intro a₁ m₁ rcases Lists'.mem_of_subset' hl₁ m₁ with ⟨a₂, m₂, e₁₂⟩ rcases Lists'.mem_of_subset' hl₂ m₂ with ⟨a₃, m₃, e₂₃⟩ exact ⟨a₃, m₃, IH _ m₁ e₁₂ e₂₃⟩ · intro a₃ m₃ rcases Lists'.mem_of_subset' hr₂ m₃ with ⟨a₂, m₂, e₃₂⟩ rcases Lists'.mem_of_subset' hr₁ m₂ with ⟨a₁, m₁, e₂₁⟩ exact ⟨a₁, m₁, (IH _ m₁ e₂₁.symm e₃₂.symm).symm⟩ · rintro _ ⟨⟩ · intro a l IH₁ IH₂ simpa using ⟨IH₁, IH₂⟩ instance instSetoidLists : Setoid (Lists α) := ⟨(· ~ ·), Equiv.refl, @Equiv.symm _, @Equiv.trans _⟩ section Decidable theorem sizeof_pos {b} (l : Lists' α b) : 0 < SizeOf.sizeOf l := by cases l <;> simp only [Lists'.atom.sizeOf_spec, Lists'.nil.sizeOf_spec, Lists'.cons'.sizeOf_spec, true_or, add_pos_iff, zero_lt_one] theorem lt_sizeof_cons' {b} (a : Lists' α b) (l) : SizeOf.sizeOf (⟨b, a⟩ : Lists α) < SizeOf.sizeOf (Lists'.cons' a l) := by simp only [Sigma.mk.sizeOf_spec, Lists'.cons'.sizeOf_spec, lt_add_iff_pos_right] apply sizeof_pos variable [DecidableEq α] mutual instance Equiv.decidable : ∀ l₁ l₂ : Lists α, Decidable (l₁ ~ l₂) | ⟨false, l₁⟩, ⟨false, l₂⟩ => decidable_of_iff' (l₁ = l₂) <| by cases l₁ apply equiv_atom.trans simp only [atom] constructor <;> (rintro ⟨rfl⟩; rfl) | ⟨false, l₁⟩, ⟨true, l₂⟩ => isFalse <| by rintro ⟨⟩ | ⟨true, l₁⟩, ⟨false, l₂⟩ => isFalse <| by rintro ⟨⟩ | ⟨true, l₁⟩, ⟨true, l₂⟩ => by haveI : Decidable (l₁ ⊆ l₂) := have : SizeOf.sizeOf l₁ + SizeOf.sizeOf l₂ < SizeOf.sizeOf (⟨true, l₁⟩ : Lists α) + SizeOf.sizeOf (⟨true, l₂⟩ : Lists α) := by decreasing_tactic Subset.decidable l₁ l₂ haveI : Decidable (l₂ ⊆ l₁) := have : SizeOf.sizeOf l₂ + SizeOf.sizeOf l₁ < SizeOf.sizeOf (⟨true, l₁⟩ : Lists α) + SizeOf.sizeOf (⟨true, l₂⟩ : Lists α) := by decreasing_tactic Subset.decidable l₂ l₁ exact decidable_of_iff' _ Equiv.antisymm_iff termination_by x y => sizeOf x + sizeOf y instance Subset.decidable : ∀ l₁ l₂ : Lists' α true, Decidable (l₁ ⊆ l₂) | Lists'.nil, _ => isTrue Lists'.Subset.nil | @Lists'.cons' _ b a l₁, l₂ => by haveI := have : sizeOf (⟨b, a⟩ : Lists α) < 1 + 1 + sizeOf a + sizeOf l₁ := by simp [sizeof_pos] mem.decidable ⟨b, a⟩ l₂ haveI := have : SizeOf.sizeOf l₁ + SizeOf.sizeOf l₂ < SizeOf.sizeOf (Lists'.cons' a l₁) + SizeOf.sizeOf l₂ := by decreasing_tactic Subset.decidable l₁ l₂ exact decidable_of_iff' _ (@Lists'.cons_subset _ ⟨_, _⟩ _ _) termination_by x y => sizeOf x + sizeOf y instance mem.decidable : ∀ (a : Lists α) (l : Lists' α true), Decidable (a ∈ l) | a, Lists'.nil => isFalse <| by rintro ⟨_, ⟨⟩, _⟩ | a, Lists'.cons' b l₂ => by haveI := have : sizeOf (⟨_, b⟩ : Lists α) < 1 + 1 + sizeOf b + sizeOf l₂ := by simp [sizeof_pos] Equiv.decidable a ⟨_, b⟩ haveI := have : SizeOf.sizeOf a + SizeOf.sizeOf l₂ < SizeOf.sizeOf a + SizeOf.sizeOf (Lists'.cons' b l₂) := by decreasing_tactic mem.decidable a l₂ refine decidable_of_iff' (a ~ ⟨_, b⟩ ∨ a ∈ l₂) ?_ rw [← Lists'.mem_cons]; rfl termination_by x y => sizeOf x + sizeOf y end /-- Copy over the decidability to the `Setoid` instance. -/ instance : DecidableRel ((· ≈ ·) : Lists α → Lists α → Prop) := Lists.Equiv.decidable -- This is an autogenerated declaration, so there's nothing we can do about it. attribute [nolint nonClassInstance] Lists.Equiv.decidable._mutual end Decidable end Lists namespace Lists' theorem mem_equiv_left {l : Lists' α true} : ∀ {a a'}, a ~ a' → (a ∈ l ↔ a' ∈ l) := suffices ∀ {a a'}, a ~ a' → a ∈ l → a' ∈ l from fun e => ⟨this e, this e.symm⟩ fun e₁ ⟨_, m₃, e₂⟩ => ⟨_, m₃, e₁.symm.trans e₂⟩ theorem mem_of_subset {a} {l₁ l₂ : Lists' α true} (s : l₁ ⊆ l₂) : a ∈ l₁ → a ∈ l₂ | ⟨_, m, e⟩ => (mem_equiv_left e).2 (mem_of_subset' s m) theorem Subset.trans {l₁ l₂ l₃ : Lists' α true} (h₁ : l₁ ⊆ l₂) (h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := subset_def.2 fun _ m₁ => mem_of_subset h₂ <| mem_of_subset' h₁ m₁ end Lists' /-- `Finsets` are defined via equivalence classes of `Lists` -/ def Finsets (α : Type*) := Quotient (@Lists.instSetoidLists α) namespace Finsets instance : EmptyCollection (Finsets α) := ⟨⟦Lists.of' Lists'.nil⟧⟩ instance : Inhabited (Finsets α) := ⟨∅⟩ instance [DecidableEq α] : DecidableEq (Finsets α) := inferInstanceAs <| DecidableEq (Quotient Lists.instSetoidLists) end Finsets
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/Class.lean
import Mathlib.SetTheory.ZFC.Ordinal /-! # ZFC classes Classes in set theory are usually defined as collections of elements satisfying some property. Here, however, we define `Class` as `Set ZFSet` to derive many instances automatically, most of them being the lifting of set operations to classes. The usual definition is then definitionally equal to ours. ## Main definitions * `Class`: Defined as `Set ZFSet`. * `Class.iota`: Definite description operator. * `ZFSet.isOrdinal_notMem_univ`: The Burali-Forti paradox. Ordinals form a proper class. -/ universe u /-- The collection of all classes. We define `Class` as `Set ZFSet`, as this allows us to get many instances automatically. However, in practice, we treat it as (the definitionally equal) `ZFSet → Prop`. This means, the preferred way to state that `x : ZFSet` belongs to `A : Class` is to write `A x`. -/ @[pp_with_univ] def Class := Set ZFSet deriving HasSubset, EmptyCollection, Nonempty, Union, Inter, HasCompl, SDiff instance : Insert ZFSet Class := ⟨Set.insert⟩ namespace Class -- Porting note: this used to be a `deriving HasSep Set` instance, -- it should probably be turned into notation. /-- `{x ∈ A | p x}` is the class of elements in `A` satisfying `p` -/ protected def sep (p : ZFSet → Prop) (A : Class) : Class := {y | A y ∧ p y} @[ext] theorem ext {x y : Class.{u}} : (∀ z : ZFSet.{u}, x z ↔ y z) → x = y := Set.ext /-- Coerce a ZFC set into a class -/ @[coe] def ofSet (x : ZFSet.{u}) : Class.{u} := { y | y ∈ x } instance : Coe ZFSet Class := ⟨ofSet⟩ /-- The universal class -/ def univ : Class := Set.univ /-- Assert that `A` is a ZFC set satisfying `B` -/ def ToSet (B : Class.{u}) (A : Class.{u}) : Prop := ∃ x : ZFSet, ↑x = A ∧ B x /-- `A ∈ B` if `A` is a ZFC set which satisfies `B` -/ protected def Mem (B A : Class.{u}) : Prop := ToSet.{u} B A instance : Membership Class Class := ⟨Class.Mem⟩ theorem mem_def (A B : Class.{u}) : A ∈ B ↔ ∃ x : ZFSet, ↑x = A ∧ B x := Iff.rfl @[simp] theorem notMem_empty (x : Class.{u}) : x ∉ (∅ : Class.{u}) := fun ⟨_, _, h⟩ => h @[deprecated (since := "2025-05-23")] alias not_mem_empty := notMem_empty @[simp] theorem not_empty_hom (x : ZFSet.{u}) : ¬(∅ : Class.{u}) x := id @[simp] theorem mem_univ {A : Class.{u}} : A ∈ univ.{u} ↔ ∃ x : ZFSet.{u}, ↑x = A := exists_congr fun _ => iff_of_eq (and_true _) @[simp] theorem mem_univ_hom (x : ZFSet.{u}) : univ.{u} x := trivial theorem eq_univ_iff_forall {A : Class.{u}} : A = univ ↔ ∀ x : ZFSet, A x := Set.eq_univ_iff_forall theorem eq_univ_of_forall {A : Class.{u}} : (∀ x : ZFSet, A x) → A = univ := Set.eq_univ_of_forall theorem mem_wf : @WellFounded Class.{u} (· ∈ ·) := ⟨by have H : ∀ x : ZFSet.{u}, @Acc Class.{u} (· ∈ ·) ↑x := by refine fun a => ZFSet.inductionOn a fun x IH => ⟨_, ?_⟩ rintro A ⟨z, rfl, hz⟩ exact IH z hz refine fun A => ⟨A, ?_⟩ rintro B ⟨x, rfl, _⟩ exact H x⟩ instance : IsWellFounded Class (· ∈ ·) := ⟨mem_wf⟩ instance : WellFoundedRelation Class := ⟨_, mem_wf⟩ theorem mem_asymm {x y : Class} : x ∈ y → y ∉ x := asymm_of (· ∈ ·) theorem mem_irrefl (x : Class) : x ∉ x := irrefl_of (· ∈ ·) x /-- **There is no universal set.** This is stated as `univ ∉ univ`, meaning that `univ` (the class of all sets) is proper (does not belong to the class of all sets). -/ theorem univ_notMem_univ : univ ∉ univ := mem_irrefl _ @[deprecated (since := "2025-05-23")] alias univ_not_mem_univ := univ_notMem_univ /-- Convert a conglomerate (a collection of classes) into a class -/ def congToClass (x : Set Class.{u}) : Class.{u} := { y | ↑y ∈ x } @[simp] theorem congToClass_empty : congToClass ∅ = ∅ := by rfl /-- Convert a class into a conglomerate (a collection of classes) -/ def classToCong (x : Class.{u}) : Set Class.{u} := { y | y ∈ x } @[simp] theorem classToCong_empty : classToCong ∅ = ∅ := by simp [classToCong] /-- The power class of a class is the class of all subclasses that are ZFC sets -/ def powerset (x : Class) : Class := congToClass (Set.powerset x) /-- The union of a class is the class of all members of ZFC sets in the class. Uses `⋃₀` notation, scoped under the `Class` namespace. -/ def sUnion (x : Class) : Class := ⋃₀ classToCong x @[inherit_doc] scoped prefix:110 "⋃₀ " => Class.sUnion /-- The intersection of a class is the class of all members of ZFC sets in the class . Uses `⋂₀` notation, scoped under the `Class` namespace. -/ def sInter (x : Class) : Class := ⋂₀ classToCong x @[inherit_doc] scoped prefix:110 "⋂₀ " => Class.sInter theorem ofSet.inj {x y : ZFSet.{u}} (h : (x : Class.{u}) = y) : x = y := ZFSet.ext fun z => by change (x : Class.{u}) z ↔ (y : Class.{u}) z rw [h] @[simp] theorem toSet_of_ZFSet (A : Class.{u}) (x : ZFSet.{u}) : ToSet A x ↔ A x := ⟨fun ⟨y, yx, py⟩ => by rwa [ofSet.inj yx] at py, fun px => ⟨x, rfl, px⟩⟩ @[simp, norm_cast] theorem coe_mem {x : ZFSet.{u}} {A : Class.{u}} : ↑x ∈ A ↔ A x := toSet_of_ZFSet _ _ @[simp] theorem coe_apply {x y : ZFSet.{u}} : (y : Class.{u}) x ↔ x ∈ y := Iff.rfl @[simp, norm_cast] theorem coe_subset (x y : ZFSet.{u}) : (x : Class.{u}) ⊆ y ↔ x ⊆ y := Iff.rfl @[simp, norm_cast] theorem coe_sep (p : Class.{u}) (x : ZFSet.{u}) : (ZFSet.sep p x : Class) = { y ∈ x | p y } := ext fun _ => ZFSet.mem_sep @[simp, norm_cast] theorem coe_empty : ↑(∅ : ZFSet.{u}) = (∅ : Class.{u}) := ext fun y => iff_false _ ▸ ZFSet.notMem_empty y @[simp, norm_cast] theorem coe_insert (x y : ZFSet.{u}) : ↑(insert x y) = @insert ZFSet.{u} Class.{u} _ x y := ext fun _ => ZFSet.mem_insert_iff @[simp, norm_cast] theorem coe_union (x y : ZFSet.{u}) : ↑(x ∪ y) = (x : Class.{u}) ∪ y := ext fun _ => ZFSet.mem_union @[simp, norm_cast] theorem coe_inter (x y : ZFSet.{u}) : ↑(x ∩ y) = (x : Class.{u}) ∩ y := ext fun _ => ZFSet.mem_inter @[simp, norm_cast] theorem coe_diff (x y : ZFSet.{u}) : ↑(x \ y) = (x : Class.{u}) \ y := ext fun _ => ZFSet.mem_diff @[simp, norm_cast] theorem coe_powerset (x : ZFSet.{u}) : ↑x.powerset = powerset.{u} x := ext fun _ => ZFSet.mem_powerset @[simp] theorem powerset_apply {A : Class.{u}} {x : ZFSet.{u}} : powerset A x ↔ ↑x ⊆ A := Iff.rfl @[simp] theorem sUnion_apply {x : Class} {y : ZFSet} : (⋃₀ x) y ↔ ∃ z : ZFSet, x z ∧ y ∈ z := by constructor · rintro ⟨-, ⟨z, rfl, hxz⟩, hyz⟩ exact ⟨z, hxz, hyz⟩ · exact fun ⟨z, hxz, hyz⟩ => ⟨_, coe_mem.2 hxz, hyz⟩ open scoped ZFSet in @[simp, norm_cast] theorem coe_sUnion (x : ZFSet.{u}) : ↑(⋃₀ x : ZFSet) = ⋃₀ (x : Class.{u}) := ext fun y => ZFSet.mem_sUnion.trans (sUnion_apply.trans <| by rfl).symm @[simp] theorem mem_sUnion {x y : Class.{u}} : y ∈ ⋃₀ x ↔ ∃ z, z ∈ x ∧ y ∈ z := by constructor · rintro ⟨w, rfl, z, hzx, hwz⟩ exact ⟨z, hzx, coe_mem.2 hwz⟩ · rintro ⟨w, hwx, z, rfl, hwz⟩ exact ⟨z, rfl, w, hwx, hwz⟩ theorem sInter_apply {x : Class.{u}} {y : ZFSet.{u}} : (⋂₀ x) y ↔ ∀ z : ZFSet.{u}, x z → y ∈ z := by refine ⟨fun hxy z hxz => hxy _ ⟨z, rfl, hxz⟩, ?_⟩ rintro H - ⟨z, rfl, hxz⟩ exact H _ hxz open scoped ZFSet in @[simp, norm_cast] theorem coe_sInter {x : ZFSet.{u}} (h : x.Nonempty) : ↑(⋂₀ x : ZFSet) = ⋂₀ (x : Class.{u}) := Set.ext fun _ => (ZFSet.mem_sInter h).trans sInter_apply.symm theorem mem_of_mem_sInter {x y z : Class} (hy : y ∈ ⋂₀ x) (hz : z ∈ x) : y ∈ z := by obtain ⟨w, rfl, hw⟩ := hy exact coe_mem.2 (hw z hz) theorem mem_sInter {x y : Class.{u}} (h : x.Nonempty) : y ∈ ⋂₀ x ↔ ∀ z, z ∈ x → y ∈ z := by refine ⟨fun hy z => mem_of_mem_sInter hy, fun H => ?_⟩ simp_rw [mem_def, sInter_apply] obtain ⟨z, hz⟩ := h obtain ⟨y, rfl, _⟩ := H z (coe_mem.2 hz) refine ⟨y, rfl, fun w hxw => ?_⟩ simpa only [coe_mem, coe_apply] using H w (coe_mem.2 hxw) @[simp] theorem sUnion_empty : ⋃₀ (∅ : Class.{u}) = (∅ : Class.{u}) := by ext simp @[simp] theorem sInter_empty : ⋂₀ (∅ : Class.{u}) = univ := by rw [sInter, classToCong_empty, Set.sInter_empty, univ] /-- An induction principle for sets. If every subset of a class is a member, then the class is universal. -/ theorem eq_univ_of_powerset_subset {A : Class} (hA : powerset A ⊆ A) : A = univ := eq_univ_of_forall (by by_contra! hnA exact WellFounded.min_mem ZFSet.mem_wf _ hnA (hA fun x hx => Classical.not_not.1 fun hB => WellFounded.not_lt_min ZFSet.mem_wf _ hnA hB <| coe_apply.1 hx)) /-- The definite description operator, which is `{x}` if `{y | A y} = {x}` and `∅` otherwise. -/ def iota (A : Class) : Class := ⋃₀ ({ x | ∀ y, A y ↔ y = x } : Class) theorem iota_val (A : Class) (x : ZFSet) (H : ∀ y, A y ↔ y = x) : iota A = ↑x := ext fun y => ⟨fun ⟨_, ⟨x', rfl, h⟩, yx'⟩ => by rwa [← (H x').1 <| (h x').2 rfl], fun yx => ⟨_, ⟨x, rfl, H⟩, yx⟩⟩ /-- Unlike the other set constructors, the `iota` definite descriptor is a set for any set input, but not constructively so, so there is no associated `Class → Set` function. -/ theorem iota_ex (A) : iota.{u} A ∈ univ.{u} := mem_univ.2 <| Or.elim (Classical.em <| ∃ x, ∀ y, A y ↔ y = x) (fun ⟨x, h⟩ => ⟨x, Eq.symm <| iota_val A x h⟩) fun hn => ⟨∅, ext fun _ => coe_empty.symm ▸ ⟨False.rec, fun ⟨_, ⟨x, rfl, H⟩, _⟩ => hn ⟨x, H⟩⟩⟩ /-- Function value -/ def fval (F A : Class.{u}) : Class.{u} := iota fun y => ToSet (fun x => F (ZFSet.pair x y)) A @[inherit_doc] infixl:100 " ′ " => fval theorem fval_ex (F A : Class.{u}) : F ′ A ∈ univ.{u} := iota_ex _ end Class namespace ZFSet @[simp] theorem map_fval {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x y : ZFSet.{u}} (h : y ∈ x) : (ZFSet.map f x ′ y : Class.{u}) = f y := Class.iota_val _ _ fun z => by rw [Class.toSet_of_ZFSet, Class.coe_apply, mem_map] exact ⟨fun ⟨w, _, pr⟩ => by let ⟨wy, fw⟩ := ZFSet.pair_injective pr rw [← fw, wy], fun e => by subst e exact ⟨_, h, rfl⟩⟩ variable (x : ZFSet.{u}) /-- A choice function on the class of nonempty ZFC sets. -/ noncomputable def choice : ZFSet := @map (fun y => Classical.epsilon fun z => z ∈ y) (Classical.allZFSetDefinable _) x theorem choice_mem_aux (h : ∅ ∉ x) (y : ZFSet.{u}) (yx : y ∈ x) : (Classical.epsilon fun z : ZFSet.{u} => z ∈ y) ∈ y := (@Classical.epsilon_spec _ fun z : ZFSet.{u} => z ∈ y) <| by_contradiction fun n => h <| by rwa [← (eq_empty y).2 fun z zx => n ⟨z, zx⟩] theorem choice_isFunc (h : ∅ ∉ x) : IsFunc x (⋃₀ x) (choice x) := (@map_isFunc _ (Classical.allZFSetDefinable _) _ _).2 fun y yx => mem_sUnion.2 ⟨y, yx, choice_mem_aux x h y yx⟩ theorem choice_mem (h : ∅ ∉ x) (y : ZFSet.{u}) (yx : y ∈ x) : (choice x ′ y : Class.{u}) ∈ (y : Class.{u}) := by delta choice rw [@map_fval _ (Classical.allZFSetDefinable _) x y yx, Class.coe_mem, Class.coe_apply] exact choice_mem_aux x h y yx private lemma toSet_equiv_aux {s : Set ZFSet.{u}} (hs : Small.{u} s) : (mk <| PSet.mk (Shrink s) fun x ↦ ((equivShrink s).symm x).1.out).toSet = s := by ext x rw [mem_toSet, ← mk_out x, mk_mem_iff, mk_out] refine ⟨?_, fun xs ↦ ⟨equivShrink s (Subtype.mk x xs), ?_⟩⟩ · rintro ⟨b, h2⟩ rw [← ZFSet.eq, ZFSet.mk_out] at h2 simp [h2] · simp [PSet.Equiv.refl] /-- `ZFSet.toSet` as an equivalence. -/ @[simps apply_coe] noncomputable def toSet_equiv : ZFSet.{u} ≃ {s : Set ZFSet.{u} // Small.{u, u+1} s} where toFun x := ⟨x.toSet, x.small_toSet⟩ invFun := fun ⟨s, _⟩ ↦ mk <| PSet.mk (Shrink s) fun x ↦ ((equivShrink.{u, u + 1} s).symm x).1.out left_inv := Function.rightInverse_of_injective_of_leftInverse (by intro _ _; simp) fun s ↦ Subtype.coe_injective <| toSet_equiv_aux s.2 right_inv s := Subtype.coe_injective <| toSet_equiv_aux s.2 /-- The **Burali-Forti paradox**: ordinals form a proper class. -/ theorem isOrdinal_notMem_univ : IsOrdinal ∉ Class.univ.{u} := by rintro ⟨x, hx, -⟩ suffices IsOrdinal x by apply Class.mem_irrefl x rwa [Class.coe_mem, hx] refine ⟨fun y hy z hz ↦ ?_, fun hyz hzw hwx ↦ ?_⟩ <;> rw [← Class.coe_apply, hx] at * exacts [hy.mem hz, hwx.mem_trans hyz hzw] @[deprecated (since := "2025-05-23")] alias isOrdinal_not_mem_univ := isOrdinal_notMem_univ end ZFSet
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/Rank.lean
import Mathlib.Logic.UnivLE import Mathlib.SetTheory.Ordinal.Rank import Mathlib.SetTheory.ZFC.Basic /-! # Ordinal ranks of PSet and ZFSet In this file, we define the ordinal ranks of `PSet` and `ZFSet`. These ranks are the same as `IsWellFounded.rank` over `∈`, but are defined in a way that the universe levels of ranks are the same as the indexing types. ## Definitions * `PSet.rank`: Ordinal rank of a pre-set. * `ZFSet.rank`: Ordinal rank of a ZFC set. -/ universe u v open Ordinal Order /-! ### PSet rank -/ namespace PSet /-- The ordinal rank of a pre-set -/ noncomputable def rank : PSet.{u} → Ordinal.{u} | ⟨_, A⟩ => ⨆ a, succ (rank (A a)) theorem rank_congr : ∀ {x y : PSet}, Equiv x y → rank x = rank y | ⟨_, _⟩, ⟨_, _⟩, ⟨αβ, βα⟩ => by apply congr_arg sSup ext constructor <;> simp only [Set.mem_range, forall_exists_index] <;> intro a h · obtain ⟨b, h'⟩ := αβ a exists b rw [← h, rank_congr h'] · obtain ⟨b, h'⟩ := βα a exists b rw [← h, rank_congr h'] theorem rank_lt_of_mem : ∀ {x y : PSet}, y ∈ x → rank y < rank x | ⟨_, _⟩, _, ⟨_, h⟩ => by rw [rank_congr h, ← succ_le_iff] apply Ordinal.le_iSup theorem rank_le_iff {o : Ordinal} : ∀ {x : PSet}, rank x ≤ o ↔ ∀ ⦃y⦄, y ∈ x → rank y < o | ⟨_, A⟩ => by refine ⟨fun h _ h' => (rank_lt_of_mem h').trans_le h, fun h ↦ Ordinal.iSup_le fun a ↦ ?_⟩ rw [succ_le_iff] exact h (Mem.mk A a) theorem lt_rank_iff {o : Ordinal} {x : PSet} : o < rank x ↔ ∃ y ∈ x, o ≤ rank y := by rw [← not_iff_not, not_lt, rank_le_iff] simp variable {x y : PSet.{u}} @[gcongr] theorem rank_mono (h : x ⊆ y) : rank x ≤ rank y := rank_le_iff.2 fun _ h₁ => rank_lt_of_mem (mem_of_subset h h₁) @[simp] theorem rank_empty : rank ∅ = 0 := by simp [empty_def, rank] @[simp] theorem rank_insert (x y : PSet) : rank (insert x y) = max (succ (rank x)) (rank y) := by apply le_antisymm · simp_rw [rank_le_iff, mem_insert_iff] rintro _ (h | h) · simp [rank_congr h] · simp [rank_lt_of_mem h] · apply max_le · exact (rank_lt_of_mem (mem_insert x y)).succ_le · exact rank_mono (subset_iff.2 fun z => mem_insert_of_mem x) @[simp] theorem rank_singleton (x : PSet) : rank {x} = succ (rank x) := (rank_insert _ _).trans (by simp) theorem rank_pair (x y : PSet) : rank {x, y} = max (succ (rank x)) (succ (rank y)) := by simp @[simp] theorem rank_powerset (x : PSet) : rank (powerset x) = succ (rank x) := by apply le_antisymm · simp_rw [rank_le_iff, mem_powerset, lt_succ_iff] intro exact rank_mono · rw [succ_le_iff] apply rank_lt_of_mem simp /-- For the rank of `⋃₀ x`, we only have `rank (⋃₀ x) ≤ rank x ≤ rank (⋃₀ x) + 1`. This inequality is split into `rank_sUnion_le` and `le_succ_rank_sUnion`. -/ theorem rank_sUnion_le (x : PSet) : rank (⋃₀ x) ≤ rank x := by simp_rw [rank_le_iff, mem_sUnion] intro _ ⟨_, _, _⟩ trans <;> apply rank_lt_of_mem <;> assumption theorem le_succ_rank_sUnion (x : PSet) : rank x ≤ succ (rank (⋃₀ x)) := by rw [← rank_powerset] apply rank_mono rw [subset_iff] intro z _ rw [mem_powerset, subset_iff] intro _ _ rw [mem_sUnion] exists z /-- `PSet.rank` is equal to the `IsWellFounded.rank` over `∈`. -/ theorem rank_eq_wfRank : lift.{u + 1, u} (rank x) = IsWellFounded.rank (α := PSet) (· ∈ ·) x := by induction x using mem_wf.induction with | _ x ih rw [IsWellFounded.rank_eq] simp_rw [← fun y : { y // y ∈ x } => ih y y.2] apply (le_of_forall_lt _).antisymm (Ordinal.iSup_le _) <;> intro h · rw [lt_lift_iff] rintro ⟨o, h, rfl⟩ simpa [Ordinal.lt_iSup_iff] using lt_rank_iff.1 h · simpa using rank_lt_of_mem h.2 end PSet /-! ### ZFSet rank -/ namespace ZFSet variable {x y : ZFSet.{u}} /-- The ordinal rank of a ZFC set -/ noncomputable def rank : ZFSet.{u} → Ordinal.{u} := Quotient.lift _ fun _ _ => PSet.rank_congr theorem rank_lt_of_mem : y ∈ x → rank y < rank x := Quotient.inductionOn₂ x y fun _ _ => PSet.rank_lt_of_mem theorem rank_le_iff {o : Ordinal} : rank x ≤ o ↔ ∀ ⦃y⦄, y ∈ x → rank y < o := ⟨fun h _ h' => (rank_lt_of_mem h').trans_le h, Quotient.inductionOn x fun _ h => PSet.rank_le_iff.2 fun y h' => @h ⟦y⟧ h'⟩ theorem lt_rank_iff {o : Ordinal} : o < rank x ↔ ∃ y ∈ x, o ≤ rank y := by rw [← not_iff_not, not_lt, rank_le_iff] simp @[gcongr] theorem rank_mono (h : x ⊆ y) : rank x ≤ rank y := rank_le_iff.2 fun _ h₁ => rank_lt_of_mem (h h₁) @[simp] theorem rank_empty : rank ∅ = 0 := PSet.rank_empty @[simp] theorem rank_insert (x y : ZFSet) : rank (insert x y) = max (succ (rank x)) (rank y) := Quotient.inductionOn₂ x y PSet.rank_insert @[simp] theorem rank_singleton (x : ZFSet) : rank {x} = succ (rank x) := (rank_insert _ _).trans (by simp) theorem rank_pair (x y : ZFSet) : rank {x, y} = max (succ (rank x)) (succ (rank y)) := by simp @[simp] theorem rank_union (x y : ZFSet) : rank (x ∪ y) = max (rank x) (rank y) := by apply le_antisymm · simp_rw [rank_le_iff, mem_union, lt_max_iff] intro apply Or.imp <;> apply rank_lt_of_mem · apply max_le <;> apply rank_mono <;> intro _ h <;> simp [h] @[simp] theorem rank_powerset (x : ZFSet) : rank (powerset x) = succ (rank x) := Quotient.inductionOn x PSet.rank_powerset /-- For the rank of `⋃₀ x`, we only have `rank (⋃₀ x) ≤ rank x ≤ rank (⋃₀ x) + 1`. This inequality is split into `rank_sUnion_le` and `le_succ_rank_sUnion`. -/ theorem rank_sUnion_le (x : ZFSet) : rank (⋃₀ x) ≤ rank x := by simp_rw [rank_le_iff, mem_sUnion] intro _ ⟨_, _, _⟩ trans <;> apply rank_lt_of_mem <;> assumption theorem le_succ_rank_sUnion (x : ZFSet) : rank x ≤ succ (rank (⋃₀ x)) := by rw [← rank_powerset] apply rank_mono intro z _ rw [mem_powerset] intro _ _ rw [mem_sUnion] exists z @[simp] theorem rank_range {α : Type*} [Small.{u} α] (f : α → ZFSet.{u}) : rank (range f) = ⨆ i, succ (rank (f i)) := by apply (Ordinal.iSup_le _).antisymm' · simpa [rank_le_iff, ← succ_le_iff] using Ordinal.le_iSup _ · simp [rank_lt_of_mem] @[simp] theorem rank_iUnion {α : Type*} [Small.{u} α] (f : α → ZFSet.{u}) : rank (⋃ i, f i) = ⨆ i, rank (f i) := by apply le_antisymm · simp_rw [rank_le_iff, mem_iUnion] intro y ⟨i, hy⟩ exact (rank_lt_of_mem hy).trans_le (Ordinal.le_iSup _ _) · exact Ordinal.iSup_le fun i => rank_mono (subset_iUnion f i) /-- `ZFSet.rank` is equal to the `IsWellFounded.rank` over `∈`. -/ theorem rank_eq_wfRank : lift.{u + 1, u} (rank x) = IsWellFounded.rank (α := ZFSet) (· ∈ ·) x := by induction x using inductionOn with | _ x ih rw [IsWellFounded.rank_eq] simp_rw [← fun y : { y // y ∈ x } => ih y y.2] apply (le_of_forall_lt _).antisymm (Ordinal.iSup_le _) <;> intro h · rw [lt_lift_iff] rintro ⟨o, h, rfl⟩ simpa [Ordinal.lt_iSup_iff] using lt_rank_iff.1 h · simpa using rank_lt_of_mem h.2 end ZFSet
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/Basic.lean
import Mathlib.Data.Fin.VecNotation import Mathlib.Data.SetLike.Basic import Mathlib.Logic.Small.Basic import Mathlib.SetTheory.ZFC.PSet /-! # A model of ZFC In this file, we model Zermelo-Fraenkel set theory (+ choice) using Lean's underlying type theory, building on the pre-sets defined in `Mathlib/SetTheory/ZFC/PSet.lean`. The theory of classes is developed in `Mathlib/SetTheory/ZFC/Class.lean`. ## Main definitions * `ZFSet`: ZFC set. Defined as `PSet` quotiented by `PSet.Equiv`, the extensional equivalence. * `ZFSet.choice`: Axiom of choice. Proved from Lean's axiom of choice. * `ZFSet.omega`: The von Neumann ordinal `ω` as a `Set`. * `Classical.allZFSetDefinable`: All functions are classically definable. * `ZFSet.IsFunc` : Predicate that a ZFC set is a subset of `x × y` that can be considered as a ZFC function `x → y`. That is, each member of `x` is related by the ZFC set to exactly one member of `y`. * `ZFSet.funs`: ZFC set of ZFC functions `x → y`. * `ZFSet.Hereditarily p x`: Predicate that every set in the transitive closure of `x` has property `p`. ## Notes To avoid confusion between the Lean `Set` and the ZFC `Set`, docstrings in this file refer to them respectively as "`Set`" and "ZFC set". -/ universe u /-- The ZFC universe of sets consists of the type of pre-sets, quotiented by extensional equivalence. -/ @[pp_with_univ] def ZFSet : Type (u + 1) := Quotient PSet.setoid.{u} namespace ZFSet /-- Turns a pre-set into a ZFC set. -/ def mk : PSet → ZFSet := Quotient.mk'' @[simp] theorem mk_eq (x : PSet) : @Eq ZFSet ⟦x⟧ (mk x) := rfl @[simp] theorem mk_out : ∀ x : ZFSet, mk x.out = x := Quotient.out_eq /-- A set function is "definable" if it is the image of some n-ary `PSet` function. This isn't exactly definability, but is useful as a sufficient condition for functions that have a computable image. -/ class Definable (n) (f : (Fin n → ZFSet.{u}) → ZFSet.{u}) where /-- Turns a definable function into an n-ary `PSet` function. -/ out : (Fin n → PSet.{u}) → PSet.{u} /-- A set function `f` is the image of `Definable.out f`. -/ mk_out xs : mk (out xs) = f (mk <| xs ·) := by simp attribute [simp] Definable.mk_out /-- An abbrev of `ZFSet.Definable` for unary functions. -/ abbrev Definable₁ (f : ZFSet.{u} → ZFSet.{u}) := Definable 1 (fun s ↦ f (s 0)) /-- A simpler constructor for `ZFSet.Definable₁`. -/ abbrev Definable₁.mk {f : ZFSet.{u} → ZFSet.{u}} (out : PSet.{u} → PSet.{u}) (mk_out : ∀ x, ⟦out x⟧ = f ⟦x⟧) : Definable₁ f where out xs := out (xs 0) mk_out xs := mk_out (xs 0) /-- Turns a unary definable function into a unary `PSet` function. -/ abbrev Definable₁.out (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] : PSet.{u} → PSet.{u} := fun x ↦ Definable.out (fun s ↦ f (s 0)) ![x] lemma Definable₁.mk_out {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x : PSet} : .mk (out f x) = f (.mk x) := Definable.mk_out ![x] /-- An abbrev of `ZFSet.Definable` for binary functions. -/ abbrev Definable₂ (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) := Definable 2 (fun s ↦ f (s 0) (s 1)) /-- A simpler constructor for `ZFSet.Definable₂`. -/ abbrev Definable₂.mk {f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}} (out : PSet.{u} → PSet.{u} → PSet.{u}) (mk_out : ∀ x y, ⟦out x y⟧ = f ⟦x⟧ ⟦y⟧) : Definable₂ f where out xs := out (xs 0) (xs 1) mk_out xs := mk_out (xs 0) (xs 1) /-- Turns a binary definable function into a binary `PSet` function. -/ abbrev Definable₂.out (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) [Definable₂ f] : PSet.{u} → PSet.{u} → PSet.{u} := fun x y ↦ Definable.out (fun s ↦ f (s 0) (s 1)) ![x, y] lemma Definable₂.mk_out {f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}} [Definable₂ f] {x y : PSet} : .mk (out f x y) = f (.mk x) (.mk y) := Definable.mk_out ![x, y] instance (f) [Definable₁ f] (n g) [Definable n g] : Definable n (fun s ↦ f (g s)) where out xs := Definable₁.out f (Definable.out g xs) instance (f) [Definable₂ f] (n g₁ g₂) [Definable n g₁] [Definable n g₂] : Definable n (fun s ↦ f (g₁ s) (g₂ s)) where out xs := Definable₂.out f (Definable.out g₁ xs) (Definable.out g₂ xs) instance (n) (i) : Definable n (fun s ↦ s i) where out s := s i lemma Definable.out_equiv {n} (f : (Fin n → ZFSet.{u}) → ZFSet.{u}) [Definable n f] {xs ys : Fin n → PSet} (h : ∀ i, xs i ≈ ys i) : out f xs ≈ out f ys := by rw [← Quotient.eq_iff_equiv, mk_eq, mk_eq, mk_out, mk_out] exact congrArg _ (funext fun i ↦ Quotient.sound (h i)) lemma Definable₁.out_equiv (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] {x y : PSet} (h : x ≈ y) : out f x ≈ out f y := Definable.out_equiv _ (by simp [h]) lemma Definable₂.out_equiv (f : ZFSet.{u} → ZFSet.{u} → ZFSet.{u}) [Definable₂ f] {x₁ y₁ x₂ y₂ : PSet} (h₁ : x₁ ≈ y₁) (h₂ : x₂ ≈ y₂) : out f x₁ x₂ ≈ out f y₁ y₂ := Definable.out_equiv _ (by simp [Fin.forall_fin_succ, h₁, h₂]) end ZFSet namespace Classical open PSet ZFSet /-- All functions are classically definable. -/ noncomputable def allZFSetDefinable {n} (F : (Fin n → ZFSet.{u}) → ZFSet.{u}) : Definable n F where out xs := (F (mk <| xs ·)).out end Classical namespace ZFSet open PSet theorem eq {x y : PSet} : mk x = mk y ↔ Equiv x y := Quotient.eq theorem sound {x y : PSet} (h : PSet.Equiv x y) : mk x = mk y := Quotient.sound h theorem exact {x y : PSet} : mk x = mk y → PSet.Equiv x y := Quotient.exact /-- The membership relation for ZFC sets is inherited from the membership relation for pre-sets. -/ protected def Mem : ZFSet → ZFSet → Prop := Quotient.lift₂ (· ∈ ·) fun _ _ _ _ hx hy => propext ((Mem.congr_left hx).trans (Mem.congr_right hy)) instance : Membership ZFSet ZFSet where mem t s := ZFSet.Mem s t @[simp] theorem mk_mem_iff {x y : PSet} : mk x ∈ mk y ↔ x ∈ y := Iff.rfl /-- Convert a ZFC set into a `Set` of ZFC sets -/ def toSet (u : ZFSet.{u}) : Set ZFSet.{u} := { x | x ∈ u } @[simp] theorem mem_toSet (a u : ZFSet.{u}) : a ∈ u.toSet ↔ a ∈ u := Iff.rfl instance small_toSet (x : ZFSet.{u}) : Small.{u} x.toSet := Quotient.inductionOn x fun a => by let f : a.Type → (mk a).toSet := fun i => ⟨mk <| a.Func i, func_mem a i⟩ suffices Function.Surjective f by exact small_of_surjective this rintro ⟨y, hb⟩ induction y using Quotient.inductionOn obtain ⟨i, h⟩ := hb exact ⟨i, Subtype.coe_injective (Quotient.sound h.symm)⟩ /-- A nonempty set is one that contains some element. -/ protected def Nonempty (u : ZFSet) : Prop := u.toSet.Nonempty theorem nonempty_def (u : ZFSet) : u.Nonempty ↔ ∃ x, x ∈ u := Iff.rfl theorem nonempty_of_mem {x u : ZFSet} (h : x ∈ u) : u.Nonempty := ⟨x, h⟩ @[simp] theorem nonempty_toSet_iff {u : ZFSet} : u.toSet.Nonempty ↔ u.Nonempty := Iff.rfl /-- `x ⊆ y` as ZFC sets means that all members of `x` are members of `y`. -/ protected def Subset (x y : ZFSet.{u}) := ∀ ⦃z⦄, z ∈ x → z ∈ y instance hasSubset : HasSubset ZFSet := ⟨ZFSet.Subset⟩ theorem subset_def {x y : ZFSet.{u}} : x ⊆ y ↔ ∀ ⦃z⦄, z ∈ x → z ∈ y := Iff.rfl instance : IsRefl ZFSet (· ⊆ ·) := ⟨fun _ _ => id⟩ instance : IsTrans ZFSet (· ⊆ ·) := ⟨fun _ _ _ hxy hyz _ ha => hyz (hxy ha)⟩ @[simp] theorem subset_iff : ∀ {x y : PSet}, mk x ⊆ mk y ↔ x ⊆ y | ⟨_, A⟩, ⟨_, _⟩ => ⟨fun h a => @h ⟦A a⟧ (Mem.mk A a), fun h z => Quotient.inductionOn z fun _ ⟨a, za⟩ => let ⟨b, ab⟩ := h a ⟨b, za.trans ab⟩⟩ @[simp] theorem toSet_subset_iff {x y : ZFSet} : x.toSet ⊆ y.toSet ↔ x ⊆ y := by simp [subset_def, Set.subset_def] @[ext] theorem ext {x y : ZFSet.{u}} : (∀ z : ZFSet.{u}, z ∈ x ↔ z ∈ y) → x = y := Quotient.inductionOn₂ x y fun _ _ h => Quotient.sound (Mem.ext fun w => h ⟦w⟧) theorem toSet_injective : Function.Injective toSet := fun _ _ h => ext <| Set.ext_iff.1 h @[simp] theorem toSet_inj {x y : ZFSet} : x.toSet = y.toSet ↔ x = y := toSet_injective.eq_iff instance : SetLike ZFSet ZFSet where coe := toSet coe_injective' := toSet_injective instance : HasSSubset ZFSet := ⟨(· < ·)⟩ @[simp] theorem le_def (x y : ZFSet) : x ≤ y ↔ x ⊆ y := Iff.rfl @[simp] theorem lt_def (x y : ZFSet) : x < y ↔ x ⊂ y := Iff.rfl instance : IsAntisymm ZFSet (· ⊆ ·) := ⟨@le_antisymm ZFSet _⟩ instance : IsNonstrictStrictOrder ZFSet (· ⊆ ·) (· ⊂ ·) := ⟨fun _ _ ↦ Iff.rfl⟩ /-- The empty ZFC set -/ protected def empty : ZFSet := mk ∅ instance : EmptyCollection ZFSet := ⟨ZFSet.empty⟩ instance : Inhabited ZFSet := ⟨∅⟩ @[simp] theorem notMem_empty (x) : x ∉ (∅ : ZFSet.{u}) := Quotient.inductionOn x PSet.notMem_empty @[deprecated (since := "2025-05-23")] alias not_mem_empty := notMem_empty @[simp] theorem toSet_empty : toSet ∅ = ∅ := by simp [toSet] @[simp] theorem empty_subset (x : ZFSet.{u}) : (∅ : ZFSet) ⊆ x := Quotient.inductionOn x fun y => subset_iff.2 <| PSet.empty_subset y @[simp] theorem not_nonempty_empty : ¬ZFSet.Nonempty ∅ := by simp [ZFSet.Nonempty] @[simp] theorem nonempty_mk_iff {x : PSet} : (mk x).Nonempty ↔ x.Nonempty := by refine ⟨?_, fun ⟨a, h⟩ => ⟨mk a, h⟩⟩ rintro ⟨a, h⟩ induction a using Quotient.inductionOn exact ⟨_, h⟩ theorem eq_empty (x : ZFSet.{u}) : x = ∅ ↔ ∀ y : ZFSet.{u}, y ∉ x := by simp [ZFSet.ext_iff] theorem eq_empty_or_nonempty (u : ZFSet) : u = ∅ ∨ u.Nonempty := by rw [eq_empty, ← not_exists] apply em' /-- `Insert x y` is the set `{x} ∪ y` -/ protected def Insert : ZFSet → ZFSet → ZFSet := Quotient.map₂ PSet.insert fun _ _ uv ⟨_, _⟩ ⟨_, _⟩ ⟨αβ, βα⟩ => ⟨fun o => match o with | some a => let ⟨b, hb⟩ := αβ a ⟨some b, hb⟩ | none => ⟨none, uv⟩, fun o => match o with | some b => let ⟨a, ha⟩ := βα b ⟨some a, ha⟩ | none => ⟨none, uv⟩⟩ instance : Insert ZFSet ZFSet := ⟨ZFSet.Insert⟩ instance : Singleton ZFSet ZFSet := ⟨fun x => insert x ∅⟩ instance : LawfulSingleton ZFSet ZFSet := ⟨fun _ => rfl⟩ @[simp] theorem mem_insert_iff {x y z : ZFSet.{u}} : x ∈ insert y z ↔ x = y ∨ x ∈ z := Quotient.inductionOn₃ x y z fun _ _ _ => PSet.mem_insert_iff.trans (or_congr_left eq.symm) theorem mem_insert (x y : ZFSet) : x ∈ insert x y := mem_insert_iff.2 <| Or.inl rfl theorem mem_insert_of_mem {y z : ZFSet} (x) (h : z ∈ y) : z ∈ insert x y := mem_insert_iff.2 <| Or.inr h @[simp] theorem toSet_insert (x y : ZFSet) : (insert x y).toSet = insert x y.toSet := by ext simp @[simp] theorem mem_singleton {x y : ZFSet.{u}} : x ∈ @singleton ZFSet.{u} ZFSet.{u} _ y ↔ x = y := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_singleton.trans eq.symm @[simp] theorem toSet_singleton (x : ZFSet) : ({x} : ZFSet).toSet = {x} := by ext simp theorem insert_nonempty (u v : ZFSet) : (insert u v).Nonempty := ⟨u, mem_insert u v⟩ theorem singleton_nonempty (u : ZFSet) : ZFSet.Nonempty {u} := insert_nonempty u ∅ theorem mem_pair {x y z : ZFSet.{u}} : x ∈ ({y, z} : ZFSet) ↔ x = y ∨ x = z := by simp @[simp] theorem pair_eq_singleton (x : ZFSet) : {x, x} = ({x} : ZFSet) := by ext simp @[simp] theorem pair_eq_singleton_iff {x y z : ZFSet} : ({x, y} : ZFSet) = {z} ↔ x = z ∧ y = z := by refine ⟨fun h ↦ ?_, ?_⟩ · rw [← mem_singleton, ← mem_singleton] simp [← h] · rintro ⟨rfl, rfl⟩ exact pair_eq_singleton y @[simp] theorem singleton_eq_pair_iff {x y z : ZFSet} : ({x} : ZFSet) = {y, z} ↔ x = y ∧ x = z := by rw [eq_comm, pair_eq_singleton_iff] simp_rw [eq_comm] /-- `omega` is the first infinite von Neumann ordinal -/ def omega : ZFSet := mk PSet.omega @[simp] theorem omega_zero : ∅ ∈ omega := ⟨⟨0⟩, Equiv.rfl⟩ @[simp] theorem omega_succ {n} : n ∈ omega.{u} → insert n n ∈ omega.{u} := Quotient.inductionOn n fun x ⟨⟨n⟩, h⟩ => ⟨⟨n + 1⟩, ZFSet.exact <| show insert (mk x) (mk x) = insert (mk <| ofNat n) (mk <| ofNat n) by rw [ZFSet.sound h] rfl⟩ /-- `{x ∈ a | p x}` is the set of elements in `a` satisfying `p` -/ protected def sep (p : ZFSet → Prop) : ZFSet → ZFSet := Quotient.map (PSet.sep fun y => p (mk y)) fun ⟨α, A⟩ ⟨β, B⟩ ⟨αβ, βα⟩ => ⟨fun ⟨a, pa⟩ => let ⟨b, hb⟩ := αβ a ⟨⟨b, by simpa only [mk_func, ← ZFSet.sound hb]⟩, hb⟩, fun ⟨b, pb⟩ => let ⟨a, ha⟩ := βα b ⟨⟨a, by simpa only [mk_func, ZFSet.sound ha]⟩, ha⟩⟩ -- Porting note: the { x | p x } notation appears to be disabled in Lean 4. instance : Sep ZFSet ZFSet := ⟨ZFSet.sep⟩ @[simp] theorem mem_sep {p : ZFSet.{u} → Prop} {x y : ZFSet.{u}} : y ∈ ZFSet.sep p x ↔ y ∈ x ∧ p y := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_sep (p := p ∘ mk) fun _ _ h => (Quotient.sound h).subst @[simp] theorem sep_empty (p : ZFSet → Prop) : (∅ : ZFSet).sep p = ∅ := (eq_empty _).mpr fun _ h ↦ notMem_empty _ (mem_sep.mp h).1 @[simp] theorem toSet_sep (a : ZFSet) (p : ZFSet → Prop) : (ZFSet.sep p a).toSet = { x ∈ a.toSet | p x } := by ext simp /-- The powerset operation, the collection of subsets of a ZFC set -/ def powerset : ZFSet → ZFSet := Quotient.map PSet.powerset fun ⟨_, A⟩ ⟨_, B⟩ ⟨αβ, βα⟩ => ⟨fun p => ⟨{ b | ∃ a, p a ∧ Equiv (A a) (B b) }, fun ⟨a, pa⟩ => let ⟨b, ab⟩ := αβ a ⟨⟨b, a, pa, ab⟩, ab⟩, fun ⟨_, a, pa, ab⟩ => ⟨⟨a, pa⟩, ab⟩⟩, fun q => ⟨{ a | ∃ b, q b ∧ Equiv (A a) (B b) }, fun ⟨_, b, qb, ab⟩ => ⟨⟨b, qb⟩, ab⟩, fun ⟨b, qb⟩ => let ⟨a, ab⟩ := βα b ⟨⟨a, b, qb, ab⟩, ab⟩⟩⟩ @[simp] theorem mem_powerset {x y : ZFSet.{u}} : y ∈ powerset x ↔ y ⊆ x := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_powerset.trans subset_iff.symm theorem sUnion_lem {α β : Type u} (A : α → PSet) (B : β → PSet) (αβ : ∀ a, ∃ b, Equiv (A a) (B b)) : ∀ a, ∃ b, Equiv ((sUnion ⟨α, A⟩).Func a) ((sUnion ⟨β, B⟩).Func b) | ⟨a, c⟩ => by let ⟨b, hb⟩ := αβ a induction ea : A a with | _ γ Γ induction eb : B b with | _ δ Δ rw [ea, eb] at hb obtain ⟨γδ, δγ⟩ := hb let c : (A a).Type := c let ⟨d, hd⟩ := γδ (by rwa [ea] at c) use ⟨b, Eq.ndrec d (Eq.symm eb)⟩ change PSet.Equiv ((A a).Func c) ((B b).Func (Eq.ndrec d eb.symm)) match A a, B b, ea, eb, c, d, hd with | _, _, rfl, rfl, _, _, hd => exact hd /-- The union operator, the collection of elements of elements of a ZFC set. Uses `⋃₀` notation, scoped under the `ZFSet` namespace. -/ def sUnion : ZFSet → ZFSet := Quotient.map PSet.sUnion fun ⟨_, A⟩ ⟨_, B⟩ ⟨αβ, βα⟩ => ⟨sUnion_lem A B αβ, fun a => Exists.elim (sUnion_lem B A (fun b => Exists.elim (βα b) fun c hc => ⟨c, PSet.Equiv.symm hc⟩) a) fun b hb => ⟨b, PSet.Equiv.symm hb⟩⟩ @[inherit_doc] scoped prefix:110 "⋃₀ " => ZFSet.sUnion /-- The intersection operator, the collection of elements in all of the elements of a ZFC set. We define `⋂₀ ∅ = ∅`. Uses `⋂₀` notation, scoped under the `ZFSet` namespace. -/ def sInter (x : ZFSet) : ZFSet := (⋃₀ x).sep (fun y => ∀ z ∈ x, y ∈ z) @[inherit_doc] scoped prefix:110 "⋂₀ " => ZFSet.sInter @[simp] theorem mem_sUnion {x y : ZFSet.{u}} : y ∈ ⋃₀ x ↔ ∃ z ∈ x, y ∈ z := Quotient.inductionOn₂ x y fun _ _ => PSet.mem_sUnion.trans ⟨fun ⟨z, h⟩ => ⟨⟦z⟧, h⟩, fun ⟨z, h⟩ => Quotient.inductionOn z (fun z h => ⟨z, h⟩) h⟩ theorem mem_sInter {x y : ZFSet} (h : x.Nonempty) : y ∈ ⋂₀ x ↔ ∀ z ∈ x, y ∈ z := by unfold sInter simp only [and_iff_right_iff_imp, mem_sep] intro mem apply mem_sUnion.mpr replace ⟨s, h⟩ := h exact ⟨_, h, mem _ h⟩ @[simp] theorem sUnion_empty : ⋃₀ (∅ : ZFSet.{u}) = ∅ := by ext simp @[simp] theorem sInter_empty : ⋂₀ (∅ : ZFSet) = ∅ := by simp [sInter] theorem mem_of_mem_sInter {x y z : ZFSet} (hy : y ∈ ⋂₀ x) (hz : z ∈ x) : y ∈ z := by rcases eq_empty_or_nonempty x with (rfl | hx) · exact (notMem_empty z hz).elim · exact (mem_sInter hx).1 hy z hz theorem mem_sUnion_of_mem {x y z : ZFSet} (hy : y ∈ z) (hz : z ∈ x) : y ∈ ⋃₀ x := mem_sUnion.2 ⟨z, hz, hy⟩ theorem notMem_sInter_of_notMem {x y z : ZFSet} (hy : y ∉ z) (hz : z ∈ x) : y ∉ ⋂₀ x := fun hx => hy <| mem_of_mem_sInter hx hz @[deprecated (since := "2025-05-23")] alias not_mem_sInter_of_not_mem := notMem_sInter_of_notMem @[simp] theorem sUnion_singleton {x : ZFSet.{u}} : ⋃₀ ({x} : ZFSet) = x := ext fun y => by simp_rw [mem_sUnion, mem_singleton, exists_eq_left] @[simp] theorem sInter_singleton {x : ZFSet.{u}} : ⋂₀ ({x} : ZFSet) = x := ext fun y => by simp_rw [mem_sInter (singleton_nonempty x), mem_singleton, forall_eq] @[simp] theorem toSet_sUnion (x : ZFSet.{u}) : (⋃₀ x).toSet = ⋃₀ (toSet '' x.toSet) := by ext simp theorem toSet_sInter {x : ZFSet.{u}} (h : x.Nonempty) : (⋂₀ x).toSet = ⋂₀ (toSet '' x.toSet) := by ext simp [mem_sInter h] theorem singleton_injective : Function.Injective (@singleton ZFSet ZFSet _) := fun x y H => by let this := congr_arg sUnion H rwa [sUnion_singleton, sUnion_singleton] at this @[simp] theorem singleton_inj {x y : ZFSet} : ({x} : ZFSet) = {y} ↔ x = y := singleton_injective.eq_iff /-- The binary union operation -/ protected def union (x y : ZFSet.{u}) : ZFSet.{u} := ⋃₀ {x, y} /-- The binary intersection operation -/ protected def inter (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (fun z => z ∈ y) x -- { z ∈ x | z ∈ y } /-- The set difference operation -/ protected def diff (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (fun z => z ∉ y) x -- { z ∈ x | z ∉ y } instance : Union ZFSet := ⟨ZFSet.union⟩ instance : Inter ZFSet := ⟨ZFSet.inter⟩ instance : SDiff ZFSet := ⟨ZFSet.diff⟩ @[simp] theorem toSet_union (x y : ZFSet.{u}) : (x ∪ y).toSet = x.toSet ∪ y.toSet := by change (⋃₀ {x, y}).toSet = _ simp @[simp] theorem toSet_inter (x y : ZFSet.{u}) : (x ∩ y).toSet = x.toSet ∩ y.toSet := by change (ZFSet.sep (fun z => z ∈ y) x).toSet = _ ext simp @[simp] theorem toSet_sdiff (x y : ZFSet.{u}) : (x \ y).toSet = x.toSet \ y.toSet := by change (ZFSet.sep (fun z => z ∉ y) x).toSet = _ ext simp @[simp] theorem mem_union {x y z : ZFSet.{u}} : z ∈ x ∪ y ↔ z ∈ x ∨ z ∈ y := by rw [← mem_toSet] simp @[simp] theorem mem_inter {x y z : ZFSet.{u}} : z ∈ x ∩ y ↔ z ∈ x ∧ z ∈ y := @mem_sep (fun z : ZFSet.{u} => z ∈ y) x z @[simp] theorem mem_diff {x y z : ZFSet.{u}} : z ∈ x \ y ↔ z ∈ x ∧ z ∉ y := @mem_sep (fun z : ZFSet.{u} => z ∉ y) x z @[simp] theorem sUnion_pair {x y : ZFSet.{u}} : ⋃₀ ({x, y} : ZFSet.{u}) = x ∪ y := rfl theorem mem_wf : @WellFounded ZFSet (· ∈ ·) := (wellFounded_lift₂_iff (H := fun a b c d hx hy => propext ((@Mem.congr_left a c hx).trans (@Mem.congr_right b d hy _)))).mpr PSet.mem_wf /-- Induction on the `∈` relation. -/ @[elab_as_elim] theorem inductionOn {p : ZFSet → Prop} (x) (h : ∀ x, (∀ y ∈ x, p y) → p x) : p x := mem_wf.induction x h instance : IsWellFounded ZFSet (· ∈ ·) := ⟨mem_wf⟩ instance : WellFoundedRelation ZFSet := ⟨_, mem_wf⟩ theorem mem_asymm {x y : ZFSet} : x ∈ y → y ∉ x := asymm_of (· ∈ ·) theorem mem_irrefl (x : ZFSet) : x ∉ x := irrefl_of (· ∈ ·) x theorem not_subset_of_mem {x y : ZFSet} (h : x ∈ y) : ¬ y ⊆ x := fun h' ↦ mem_irrefl _ (h' h) theorem notMem_of_subset {x y : ZFSet} (h : x ⊆ y) : y ∉ x := imp_not_comm.2 not_subset_of_mem h @[deprecated (since := "2025-05-23")] alias not_mem_of_subset := notMem_of_subset theorem regularity (x : ZFSet.{u}) (h : x ≠ ∅) : ∃ y ∈ x, x ∩ y = ∅ := by_contradiction fun ne => h <| (eq_empty x).2 fun y => @inductionOn (fun z => z ∉ x) y fun z IH zx => ne ⟨z, zx, (eq_empty _).2 fun w wxz => let ⟨wx, wz⟩ := mem_inter.1 wxz IH w wz wx⟩ /-- The image of a (definable) ZFC set function -/ def image (f : ZFSet → ZFSet) [Definable₁ f] : ZFSet → ZFSet := let r := Definable₁.out f Quotient.map (PSet.image r) fun _ _ e => Mem.ext fun _ => (mem_image (fun _ _ ↦ Definable₁.out_equiv _)).trans <| Iff.trans ⟨fun ⟨w, h1, h2⟩ => ⟨w, (Mem.congr_right e).1 h1, h2⟩, fun ⟨w, h1, h2⟩ => ⟨w, (Mem.congr_right e).2 h1, h2⟩⟩ <| (mem_image (fun _ _ ↦ Definable₁.out_equiv _)).symm theorem image.mk (f : ZFSet.{u} → ZFSet.{u}) [Definable₁ f] (x) {y} : y ∈ x → f y ∈ image f x := Quotient.inductionOn₂ x y fun ⟨_, _⟩ _ ⟨a, ya⟩ => by simp only [mk_eq, ← Definable₁.mk_out (f := f)] exact ⟨a, Definable₁.out_equiv f ya⟩ @[simp] theorem mem_image {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x y : ZFSet.{u}} : y ∈ image f x ↔ ∃ z ∈ x, f z = y := Quotient.inductionOn₂ x y fun ⟨_, A⟩ _ => ⟨fun ⟨a, ya⟩ => ⟨⟦A a⟧, Mem.mk A a, ((Quotient.sound ya).trans Definable₁.mk_out).symm⟩, fun ⟨_, hz, e⟩ => e ▸ image.mk _ _ hz⟩ @[simp] theorem toSet_image (f : ZFSet → ZFSet) [Definable₁ f] (x : ZFSet) : (image f x).toSet = f '' x.toSet := by ext simp section Small variable {α : Type*} [Small.{u} α] /-- The range of a type-indexed family of sets. -/ noncomputable def range (f : α → ZFSet.{u}) : ZFSet.{u} := ⟦⟨_, Quotient.out ∘ f ∘ (equivShrink α).symm⟩⟧ @[simp] theorem mem_range {f : α → ZFSet.{u}} {x : ZFSet.{u}} : x ∈ range f ↔ x ∈ Set.range f := Quotient.inductionOn x fun y => by constructor · rintro ⟨z, hz⟩ exact ⟨(equivShrink α).symm z, Quotient.eq_mk_iff_out.2 hz.symm⟩ · rintro ⟨z, hz⟩ use equivShrink α z simpa [hz] using PSet.Equiv.symm (Quotient.mk_out y) @[simp] theorem toSet_range (f : α → ZFSet.{u}) : (range f).toSet = Set.range f := by ext simp theorem mem_range_self {f : α → ZFSet.{u}} (a : α) : f a ∈ range f := by simp /-- Indexed union of a family of ZFC sets. Uses `⋃` notation, scoped under the `ZFSet` namespace. -/ noncomputable def iUnion (f : α → ZFSet.{u}) : ZFSet.{u} := sUnion (range f) @[inherit_doc iUnion] scoped notation3 "⋃ " (...)", " r:60:(scoped f => iUnion f) => r @[simp] theorem mem_iUnion {f : α → ZFSet.{u}} {x : ZFSet.{u}} : x ∈ ⋃ i, f i ↔ ∃ i, x ∈ f i := by simp [iUnion] @[simp] theorem toSet_iUnion (f : α → ZFSet.{u}) : (⋃ i, f i).toSet = ⋃ i, (f i).toSet := by ext simp theorem subset_iUnion (f : α → ZFSet.{u}) (i : α) : f i ⊆ ⋃ i, f i := by intro x hx simpa using ⟨i, hx⟩ end Small /-- Kuratowski ordered pair -/ def pair (x y : ZFSet.{u}) : ZFSet.{u} := {{x}, {x, y}} @[simp] theorem toSet_pair (x y : ZFSet.{u}) : (pair x y).toSet = {{x}, {x, y}} := by simp [pair] /-- A subset of pairs `{(a, b) ∈ x × y | p a b}` -/ def pairSep (p : ZFSet.{u} → ZFSet.{u} → Prop) (x y : ZFSet.{u}) : ZFSet.{u} := (powerset (powerset (x ∪ y))).sep fun z => ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b @[simp] theorem mem_pairSep {p} {x y z : ZFSet.{u}} : z ∈ pairSep p x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b ∧ p a b := by refine mem_sep.trans ⟨And.right, fun e => ⟨?_, e⟩⟩ grind [mem_pair, mem_powerset, mem_singleton, mem_union, pair, subset_def] theorem pair_injective : Function.Injective2 pair := by intro x x' y y' H simp_rw [ZFSet.ext_iff, pair, mem_pair] at H obtain rfl : x = x' := And.left <| by simpa [or_and_left] using (H {x}).1 (Or.inl rfl) have he : y = x → y = y' := by rintro rfl simpa [eq_comm] using H {y, y'} have hx := H {x, y} simp_rw [pair_eq_singleton_iff, true_and, or_true, true_iff] at hx refine ⟨rfl, hx.elim he fun hy ↦ Or.elim ?_ he id⟩ simpa using ZFSet.ext_iff.1 hy y @[simp] theorem pair_inj {x y x' y' : ZFSet} : pair x y = pair x' y' ↔ x = x' ∧ y = y' := pair_injective.eq_iff /-- The Cartesian product, `{(a, b) | a ∈ x, b ∈ y}` -/ def prod : ZFSet.{u} → ZFSet.{u} → ZFSet.{u} := pairSep fun _ _ => True @[simp] theorem mem_prod {x y z : ZFSet.{u}} : z ∈ prod x y ↔ ∃ a ∈ x, ∃ b ∈ y, z = pair a b := by simp [prod] theorem pair_mem_prod {x y a b : ZFSet.{u}} : pair a b ∈ prod x y ↔ a ∈ x ∧ b ∈ y := by simp /-- `isFunc x y f` is the assertion that `f` is a subset of `x × y` which relates to each element of `x` a unique element of `y`, so that we can consider `f` as a ZFC function `x → y`. -/ def IsFunc (x y f : ZFSet.{u}) : Prop := f ⊆ prod x y ∧ ∀ z : ZFSet.{u}, z ∈ x → ∃! w, pair z w ∈ f /-- `funs x y` is `y ^ x`, the set of all set functions `x → y` -/ def funs (x y : ZFSet.{u}) : ZFSet.{u} := ZFSet.sep (IsFunc x y) (powerset (prod x y)) @[simp] theorem mem_funs {x y f : ZFSet.{u}} : f ∈ funs x y ↔ IsFunc x y f := by simp [funs, IsFunc] instance : Definable₁ ({·}) := .mk ({·}) (fun _ ↦ rfl) instance : Definable₂ insert := .mk insert (fun _ _ ↦ rfl) instance : Definable₂ pair := by unfold pair; infer_instance /-- Graph of a function: `map f x` is the ZFC function which maps `a ∈ x` to `f a` -/ def map (f : ZFSet → ZFSet) [Definable₁ f] : ZFSet → ZFSet := image fun y => pair y (f y) @[simp] theorem mem_map {f : ZFSet → ZFSet} [Definable₁ f] {x y : ZFSet} : y ∈ map f x ↔ ∃ z ∈ x, pair z (f z) = y := mem_image theorem map_unique {f : ZFSet.{u} → ZFSet.{u}} [Definable₁ f] {x z : ZFSet.{u}} (zx : z ∈ x) : ∃! w, pair z w ∈ map f x := ⟨f z, image.mk _ _ zx, fun y yx => by let ⟨w, _, we⟩ := mem_image.1 yx let ⟨wz, fy⟩ := pair_injective we rw [← fy, wz]⟩ @[simp] theorem map_isFunc {f : ZFSet → ZFSet} [Definable₁ f] {x y : ZFSet} : IsFunc x y (map f x) ↔ ∀ z ∈ x, f z ∈ y := ⟨fun ⟨ss, h⟩ z zx => let ⟨_, t1, t2⟩ := h z zx (t2 (f z) (image.mk _ _ zx)).symm ▸ (pair_mem_prod.1 (ss t1)).right, fun h => ⟨fun _ yx => let ⟨z, zx, ze⟩ := mem_image.1 yx ze ▸ pair_mem_prod.2 ⟨zx, h z zx⟩, fun _ => map_unique⟩⟩ /-- Given a predicate `p` on ZFC sets. `Hereditarily p x` means that `x` has property `p` and the members of `x` are all `Hereditarily p`. -/ def Hereditarily (p : ZFSet → Prop) (x : ZFSet) : Prop := p x ∧ ∀ y ∈ x, Hereditarily p y termination_by x section Hereditarily variable {p : ZFSet.{u} → Prop} {x y : ZFSet.{u}} theorem hereditarily_iff : Hereditarily p x ↔ p x ∧ ∀ y ∈ x, Hereditarily p y := by rw [← Hereditarily] alias ⟨Hereditarily.def, _⟩ := hereditarily_iff theorem Hereditarily.self (h : x.Hereditarily p) : p x := h.def.1 theorem Hereditarily.mem (h : x.Hereditarily p) (hy : y ∈ x) : y.Hereditarily p := h.def.2 _ hy theorem Hereditarily.empty : Hereditarily p x → p ∅ := by apply @ZFSet.inductionOn _ x intro y IH h rcases ZFSet.eq_empty_or_nonempty y with (rfl | ⟨a, ha⟩) · exact h.self · exact IH a ha (h.mem ha) end Hereditarily end ZFSet
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/VonNeumann.lean
import Mathlib.SetTheory.ZFC.Class import Mathlib.SetTheory.ZFC.Ordinal import Mathlib.SetTheory.ZFC.Rank /-! # Von Neumann hierarchy This file defines the von Neumann hierarchy of sets `V_ o` for ordinal `o`, which is recursively defined so that `V_ a = ⋃ b < a, powerset (V_ b)`. This stratifies the universal class, in the sense that `⋃ o, V_ o = univ`. ## Notation - `V_ o` is notation for `vonNeumann o`. It is scoped in the `ZFSet` namespace. -/ universe u open Order namespace ZFSet /-- The von Neumann hierarchy is defined so that `V_ o` is the union of the powersets of all `V_ a` for `a < o`. It satisfies the following properties: - `vonNeumann_zero`: `V_ 0 = ∅` - `vonNeumann_succ`: `V_ (succ a) = powerset (V_ a)` - `vonNeumann_of_isSuccPrelimit`: `IsSuccPrelimit a → V_ a = ⋃ b < a, V_ b` -/ noncomputable def vonNeumann (o : Ordinal.{u}) : ZFSet.{u} := ⋃ a : Set.Iio o, powerset (vonNeumann a) termination_by o decreasing_by exact a.2 @[inherit_doc] scoped notation "V_ " => vonNeumann variable {a b o : Ordinal.{u}} {x : ZFSet.{u}} lemma mem_vonNeumann' : x ∈ V_ o ↔ ∃ a < o, x ⊆ V_ a := by rw [vonNeumann]; simp theorem isTransitive_vonNeumann (o : Ordinal) : IsTransitive (V_ o) := by rw [vonNeumann] exact .iUnion fun ⟨a, _⟩ => (isTransitive_vonNeumann a).powerset termination_by o @[gcongr] theorem vonNeumann_mem_of_lt (h : a < b) : V_ a ∈ V_ b := by rw [vonNeumann]; aesop @[gcongr] theorem vonNeumann_subset_of_le (h : a ≤ b) : V_ a ⊆ V_ b := h.eq_or_lt.rec (by simp_all) fun h ↦ isTransitive_vonNeumann _ _ <| vonNeumann_mem_of_lt h theorem subset_vonNeumann {o : Ordinal} {x : ZFSet} : x ⊆ V_ o ↔ rank x ≤ o := by rw [rank_le_iff] constructor <;> intro hx y hy · apply (rank_lt_of_mem (hx hy)).trans_le simp_rw [rank_le_iff, mem_vonNeumann'] rintro z ⟨a, ha, hz⟩ exact (subset_vonNeumann.1 hz).trans_lt ha · rw [mem_vonNeumann'] have := hx hy exact ⟨_, this, subset_vonNeumann.2 le_rfl⟩ termination_by o theorem subset_vonNeumann_self (x : ZFSet) : x ⊆ V_ (rank x) := by simp [subset_vonNeumann] theorem mem_vonNeumann : x ∈ V_ o ↔ rank x < o := by simp_rw [mem_vonNeumann', subset_vonNeumann] exact ⟨fun ⟨a, h₁, h₂⟩ ↦ h₂.trans_lt h₁, by aesop⟩ theorem mem_vonNeumann_succ (x : ZFSet) : x ∈ V_ (succ (rank x)) := by simp [mem_vonNeumann] /-- Every set is in some element of the von Neumann hierarchy. -/ theorem exists_mem_vonNeumann (x : ZFSet) : ∃ o, x ∈ V_ o := ⟨_, mem_vonNeumann_succ x⟩ @[simp] theorem rank_vonNeumann (o : Ordinal) : rank (V_ o) = o := le_antisymm (by rw [← subset_vonNeumann]) <| le_of_forall_lt fun a ha ↦ rank_vonNeumann a ▸ rank_lt_of_mem (vonNeumann_mem_of_lt ha) termination_by o @[simp] theorem vonNeumann_mem_vonNeumann_iff : V_ a ∈ V_ b ↔ a < b := by simp [mem_vonNeumann] @[simp] theorem vonNeumann_subset_vonNeumann_iff : V_ a ⊆ V_ b ↔ a ≤ b := by simp [subset_vonNeumann] theorem mem_vonNeumann_of_subset {y : ZFSet} (h : x ⊆ y) (hy : y ∈ V_ o) : x ∈ V_ o := by rw [mem_vonNeumann] at * exact (rank_mono h).trans_lt hy theorem vonNeumann_strictMono : StrictMono vonNeumann := strictMono_of_le_iff_le (by simp) theorem vonNeumann_injective : Function.Injective vonNeumann := vonNeumann_strictMono.injective @[simp] theorem vonNeumann_inj : V_ a = V_ b ↔ a = b := vonNeumann_injective.eq_iff @[gcongr] alias ⟨_, _root_.GCongr.ZFSet.vonNeumann_inj⟩ := vonNeumann_inj @[simp] theorem vonNeumann_zero : V_ 0 = ∅ := (eq_empty _).2 (by simp [mem_vonNeumann]) @[simp] theorem vonNeumann_succ (o : Ordinal) : V_ (succ o) = powerset (V_ o) := ext fun z ↦ by rw [mem_vonNeumann, mem_powerset, subset_vonNeumann, lt_succ_iff] theorem vonNeumann_of_isSuccPrelimit (h : IsSuccPrelimit o) : V_ o = ⋃ a : Set.Iio o, vonNeumann a := ext fun z ↦ by simpa [mem_vonNeumann] using h.lt_iff_exists_lt theorem iUnion_vonNeumann : ⋃ o, (V_ o : Class) = Class.univ := Class.eq_univ_of_forall fun x ↦ Set.mem_iUnion.2 <| exists_mem_vonNeumann x end ZFSet
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/Ordinal.lean
import Mathlib.Order.GameAdd import Mathlib.Order.RelIso.Set import Mathlib.SetTheory.ZFC.Basic /-! # Von Neumann ordinals This file works towards the development of von Neumann ordinals, i.e. transitive sets, well-ordered under `∈`. ## Definitions - `ZFSet.IsTransitive` means that every element of a set is a subset. - `ZFSet.IsOrdinal` means that the set is transitive and well-ordered under `∈`. We show multiple equivalences to this definition. ## TODO - Build correspondences between these set notions and those of the standard `Ordinal` type. -/ universe u variable {x y z w : ZFSet.{u}} namespace ZFSet /-! ### Transitive sets -/ /-- A transitive set is one where every element is a subset. This is equivalent to being an infinite-open interval in the transitive closure of membership. -/ def IsTransitive (x : ZFSet) : Prop := ∀ y ∈ x, y ⊆ x @[simp] theorem isTransitive_empty : IsTransitive ∅ := fun y hy => (notMem_empty y hy).elim theorem IsTransitive.subset_of_mem (h : x.IsTransitive) : y ∈ x → y ⊆ x := h y theorem isTransitive_iff_mem_trans : z.IsTransitive ↔ ∀ {x y : ZFSet}, x ∈ y → y ∈ z → x ∈ z := ⟨fun h _ _ hx hy => h.subset_of_mem hy hx, fun H _ hx _ hy => H hy hx⟩ alias ⟨IsTransitive.mem_trans, _⟩ := isTransitive_iff_mem_trans protected theorem IsTransitive.inter (hx : x.IsTransitive) (hy : y.IsTransitive) : (x ∩ y).IsTransitive := fun z hz w hw => by rw [mem_inter] at hz ⊢ exact ⟨hx.mem_trans hw hz.1, hy.mem_trans hw hz.2⟩ /-- The union of a transitive set is transitive. -/ protected theorem IsTransitive.sUnion (h : x.IsTransitive) : (⋃₀ x : ZFSet).IsTransitive := fun y hy z hz => by rcases mem_sUnion.1 hy with ⟨w, hw, hw'⟩ exact mem_sUnion_of_mem hz (h.mem_trans hw' hw) /-- The union of transitive sets is transitive. -/ theorem IsTransitive.sUnion' (H : ∀ y ∈ x, IsTransitive y) : (⋃₀ x : ZFSet).IsTransitive := fun y hy z hz => by rcases mem_sUnion.1 hy with ⟨w, hw, hw'⟩ exact mem_sUnion_of_mem ((H w hw).mem_trans hz hw') hw protected theorem IsTransitive.iUnion {α : Type*} [Small.{u} α] {f : α → ZFSet.{u}} (hf : ∀ i, (f i).IsTransitive) : (⋃ i, f i).IsTransitive := sUnion' (by simpa) protected theorem IsTransitive.union (hx : x.IsTransitive) (hy : y.IsTransitive) : (x ∪ y).IsTransitive := by rw [← sUnion_pair] apply IsTransitive.sUnion' intro rw [mem_pair] rintro (rfl | rfl) assumption' protected theorem IsTransitive.powerset (h : x.IsTransitive) : (powerset x).IsTransitive := fun y hy z hz => by rw [mem_powerset] at hy ⊢ exact h.subset_of_mem (hy hz) theorem isTransitive_iff_sUnion_subset : x.IsTransitive ↔ (⋃₀ x : ZFSet) ⊆ x := by constructor <;> intro h y hy · obtain ⟨z, hz, hz'⟩ := mem_sUnion.1 hy exact h.mem_trans hz' hz · exact fun z hz ↦ h <| mem_sUnion_of_mem hz hy alias ⟨IsTransitive.sUnion_subset, _⟩ := isTransitive_iff_sUnion_subset theorem isTransitive_iff_subset_powerset : x.IsTransitive ↔ x ⊆ powerset x := ⟨fun h _ hy => mem_powerset.2 <| h.subset_of_mem hy, fun H _ hy _ hz => mem_powerset.1 (H hy) hz⟩ alias ⟨IsTransitive.subset_powerset, _⟩ := isTransitive_iff_subset_powerset /-! ### Ordinals -/ /-- A set `x` is a von Neumann ordinal when it's a transitive set, that's transitive under `∈`. We prove that this further implies that `x` is well-ordered under `∈` in `isOrdinal_iff_isWellOrder`. The transitivity condition `a ∈ b → b ∈ c → a ∈ c` can be written without assuming `a ∈ x` and `b ∈ x`. The lemma `isOrdinal_iff_isTrans` shows this condition is equivalent to the usual one. -/ structure IsOrdinal (x : ZFSet) : Prop where /-- An ordinal is a transitive set. -/ isTransitive : x.IsTransitive /-- The membership operation within an ordinal is transitive. -/ mem_trans' {y z w : ZFSet} : y ∈ z → z ∈ w → w ∈ x → y ∈ w namespace IsOrdinal theorem subset_of_mem (h : x.IsOrdinal) : y ∈ x → y ⊆ x := h.isTransitive.subset_of_mem theorem mem_trans (h : z.IsOrdinal) : x ∈ y → y ∈ z → x ∈ z := h.isTransitive.mem_trans protected theorem isTrans (h : x.IsOrdinal) : IsTrans _ (Subrel (· ∈ ·) (· ∈ x)) := ⟨fun _ _ c hab hbc => h.mem_trans' hab hbc c.2⟩ /-- The simplified form of transitivity used within `IsOrdinal` yields an equivalent definition to the standard one. -/ theorem _root_.ZFSet.isOrdinal_iff_isTrans : x.IsOrdinal ↔ x.IsTransitive ∧ IsTrans _ (Subrel (· ∈ ·) (· ∈ x)) where mp h := ⟨h.isTransitive, h.isTrans⟩ mpr := by rintro ⟨h₁, ⟨h₂⟩⟩ refine ⟨h₁, fun {y z w} hyz hzw hwx ↦ ?_⟩ have hzx := h₁.mem_trans hzw hwx exact h₂ ⟨y, h₁.mem_trans hyz hzx⟩ ⟨z, hzx⟩ ⟨w, hwx⟩ hyz hzw protected theorem mem (hx : x.IsOrdinal) (hy : y ∈ x) : y.IsOrdinal := have := hx.isTrans let f : _ ↪r Subrel (· ∈ ·) (· ∈ x) := Subrel.inclusionEmbedding (· ∈ ·) (hx.subset_of_mem hy) isOrdinal_iff_isTrans.2 ⟨fun _ hz _ ha ↦ hx.mem_trans' ha hz hy, f.isTrans⟩ /-- An ordinal is a transitive set of transitive sets. -/ theorem _root_.ZFSet.isOrdinal_iff_forall_mem_isTransitive : x.IsOrdinal ↔ x.IsTransitive ∧ ∀ y ∈ x, y.IsTransitive where mp h := ⟨h.isTransitive, fun _ hy ↦ (h.mem hy).isTransitive⟩ mpr := fun ⟨h₁, h₂⟩ ↦ ⟨h₁, fun hyz hzw hwx ↦ (h₂ _ hwx).mem_trans hyz hzw⟩ /-- An ordinal is a transitive set of ordinals. -/ theorem _root_.ZFSet.isOrdinal_iff_forall_mem_isOrdinal : x.IsOrdinal ↔ x.IsTransitive ∧ ∀ y ∈ x, y.IsOrdinal where mp h := ⟨h.isTransitive, fun _ ↦ h.mem⟩ mpr := fun ⟨h₁, h₂⟩ ↦ isOrdinal_iff_forall_mem_isTransitive.2 ⟨h₁, fun y hy ↦ (h₂ y hy).isTransitive⟩ theorem subset_iff_eq_or_mem (hx : x.IsOrdinal) (hy : y.IsOrdinal) : x ⊆ y ↔ x = y ∨ x ∈ y := by constructor · revert hx hy apply Sym2.GameAdd.induction mem_wf _ x y intro x y IH hx hy hxy by_cases hyx : y ⊆ x · exact Or.inl (subset_antisymm hxy hyx) · obtain ⟨m, hm, hm'⟩ := mem_wf.has_min (y.toSet \ x.toSet) (Set.diff_nonempty.2 hyx) have hmy : m ∈ y := show m ∈ y.toSet from Set.mem_of_mem_diff hm have hmx : m ⊆ x := by intro z hzm by_contra hzx exact hm' _ ⟨hy.mem_trans hzm hmy, hzx⟩ hzm obtain rfl | H := IH m x (Sym2.GameAdd.fst_snd hmy) (hy.mem hmy) hx hmx · exact Or.inr hmy · cases Set.notMem_of_mem_diff hm H · rintro (rfl | h) · rfl · exact hy.subset_of_mem h alias ⟨eq_or_mem_of_subset, _⟩ := subset_iff_eq_or_mem theorem mem_of_subset_of_mem (h : x.IsOrdinal) (hz : z.IsOrdinal) (hx : x ⊆ y) (hy : y ∈ z) : x ∈ z := by obtain rfl | hx := h.eq_or_mem_of_subset (hz.mem hy) hx · exact hy · exact hz.mem_trans hx hy theorem notMem_iff_subset (hx : x.IsOrdinal) (hy : y.IsOrdinal) : x ∉ y ↔ y ⊆ x := by refine ⟨?_, fun hxy hyx ↦ mem_irrefl _ (hxy hyx)⟩ revert hx hy apply Sym2.GameAdd.induction mem_wf _ x y intro x y IH hx hy hyx z hzy by_contra hzx exact hyx (mem_of_subset_of_mem hx hy (IH z x (Sym2.GameAdd.fst_snd hzy) (hy.mem hzy) hx hzx) hzy) @[deprecated (since := "2025-05-23")] alias not_mem_iff_subset := notMem_iff_subset theorem not_subset_iff_mem (hx : x.IsOrdinal) (hy : y.IsOrdinal) : ¬ x ⊆ y ↔ y ∈ x := by rw [not_iff_comm, notMem_iff_subset hy hx] theorem mem_or_subset (hx : x.IsOrdinal) (hy : y.IsOrdinal) : x ∈ y ∨ y ⊆ x := by rw [or_iff_not_imp_left, notMem_iff_subset hx hy] exact id theorem subset_total (hx : x.IsOrdinal) (hy : y.IsOrdinal) : x ⊆ y ∨ y ⊆ x := by obtain h | h := mem_or_subset hx hy · exact Or.inl (hy.subset_of_mem h) · exact Or.inr h theorem mem_trichotomous (hx : x.IsOrdinal) (hy : y.IsOrdinal) : x ∈ y ∨ x = y ∨ y ∈ x := by rw [eq_comm, ← subset_iff_eq_or_mem hy hx] exact mem_or_subset hx hy protected theorem isTrichotomous (h : x.IsOrdinal) : IsTrichotomous _ (Subrel (· ∈ ·) (· ∈ x)) := ⟨fun ⟨a, ha⟩ ⟨b, hb⟩ ↦ by simpa using mem_trichotomous (h.mem ha) (h.mem hb)⟩ /-- An ordinal is a transitive set, trichotomous under membership. -/ theorem _root_.ZFSet.isOrdinal_iff_isTrichotomous : x.IsOrdinal ↔ x.IsTransitive ∧ IsTrichotomous _ (Subrel (· ∈ ·) (· ∈ x)) where mp h := ⟨h.isTransitive, h.isTrichotomous⟩ mpr := by rintro ⟨h₁, h₂⟩ rw [isOrdinal_iff_isTrans] refine ⟨h₁, ⟨@fun y z w hyz hzw ↦ ?_⟩⟩ obtain hyw | rfl | hwy := trichotomous_of (Subrel (· ∈ ·) (· ∈ x)) y w · exact hyw · cases asymm hyz hzw · cases mem_wf.asymmetric₃ _ _ _ hyz hzw hwy protected theorem isWellOrder (h : x.IsOrdinal) : IsWellOrder _ (Subrel (· ∈ ·) (· ∈ x)) where wf := (Subrel.relEmbedding _ _).wellFounded mem_wf trans := h.isTrans.1 trichotomous := h.isTrichotomous.1 /-- An ordinal is a transitive set, well-ordered under membership. -/ theorem _root_.ZFSet.isOrdinal_iff_isWellOrder : x.IsOrdinal ↔ x.IsTransitive ∧ IsWellOrder _ (Subrel (· ∈ ·) (· ∈ x)) := by use fun h ↦ ⟨h.isTransitive, h.isWellOrder⟩ rintro ⟨h₁, h₂⟩ refine isOrdinal_iff_isTrans.2 ⟨h₁, ?_⟩ infer_instance end IsOrdinal @[simp] theorem isOrdinal_empty : IsOrdinal ∅ := ⟨isTransitive_empty, fun _ _ H ↦ (notMem_empty _ H).elim⟩ end ZFSet
.lake/packages/mathlib/Mathlib/SetTheory/ZFC/PSet.lean
import Mathlib.Data.Set.Lattice /-! # Pre-sets A pre-set is inductively defined by its indexing type and its members, which are themselves pre-sets. After defining pre-sets we define extensional equality over them, also inductively. We construct a `Setoid` instance from it, and in `Mathlib/SetTheory/ZFC/Basic.lean` we define ZFC sets as the quotient of pre-sets by extensional equality. ## Main definitions * `PSet`: Pre-set. * `PSet.Type`: Underlying type of a pre-set. * `PSet.Func`: Underlying family of pre-sets of a pre-set. * `PSet.Equiv`: Extensional equivalence of pre-sets. Defined inductively. * `PSet.omega`: The von Neumann ordinal `ω` as a `PSet`. -/ universe u v /-- The type of pre-sets in universe `u`. A pre-set is a family of pre-sets indexed by a type in `Type u`. The ZFC universe is defined as a quotient of this to ensure extensionality. -/ @[pp_with_univ] inductive PSet : Type (u + 1) | mk (α : Type u) (A : α → PSet) : PSet namespace PSet /-- The underlying type of a pre-set -/ def «Type» : PSet → Type u | ⟨α, _⟩ => α /-- The underlying pre-set family of a pre-set -/ def Func : ∀ x : PSet, x.Type → PSet | ⟨_, A⟩ => A @[simp] theorem mk_type (α A) : «Type» ⟨α, A⟩ = α := rfl @[simp] theorem mk_func (α A) : Func ⟨α, A⟩ = A := rfl @[simp] theorem eta : ∀ x : PSet, mk x.Type x.Func = x | ⟨_, _⟩ => rfl /-- Two pre-sets are extensionally equivalent if every element of the first family is extensionally equivalent to some element of the second family and vice-versa. -/ def Equiv : PSet → PSet → Prop | ⟨_, A⟩, ⟨_, B⟩ => (∀ a, ∃ b, Equiv (A a) (B b)) ∧ (∀ b, ∃ a, Equiv (A a) (B b)) theorem equiv_iff : ∀ {x y : PSet}, Equiv x y ↔ (∀ i, ∃ j, Equiv (x.Func i) (y.Func j)) ∧ ∀ j, ∃ i, Equiv (x.Func i) (y.Func j) | ⟨_, _⟩, ⟨_, _⟩ => Iff.rfl theorem Equiv.exists_left {x y : PSet} (h : Equiv x y) : ∀ i, ∃ j, Equiv (x.Func i) (y.Func j) := (equiv_iff.1 h).1 theorem Equiv.exists_right {x y : PSet} (h : Equiv x y) : ∀ j, ∃ i, Equiv (x.Func i) (y.Func j) := (equiv_iff.1 h).2 @[refl] protected theorem Equiv.refl : ∀ x, Equiv x x | ⟨_, _⟩ => ⟨fun a => ⟨a, Equiv.refl _⟩, fun a => ⟨a, Equiv.refl _⟩⟩ protected theorem Equiv.rfl {x} : Equiv x x := Equiv.refl x protected theorem Equiv.euc : ∀ {x y z}, Equiv x y → Equiv z y → Equiv x z | ⟨_, _⟩, ⟨_, _⟩, ⟨_, _⟩, ⟨αβ, βα⟩, ⟨γβ, βγ⟩ => ⟨ fun a => let ⟨b, ab⟩ := αβ a let ⟨c, bc⟩ := βγ b ⟨c, Equiv.euc ab bc⟩, fun c => let ⟨b, cb⟩ := γβ c let ⟨a, ba⟩ := βα b ⟨a, Equiv.euc ba cb⟩ ⟩ @[symm] protected theorem Equiv.symm {x y} : Equiv x y → Equiv y x := (Equiv.refl y).euc protected theorem Equiv.comm {x y} : Equiv x y ↔ Equiv y x := ⟨Equiv.symm, Equiv.symm⟩ @[trans] protected theorem Equiv.trans {x y z} (h1 : Equiv x y) (h2 : Equiv y z) : Equiv x z := h1.euc h2.symm protected theorem equiv_of_isEmpty (x y : PSet) [IsEmpty x.Type] [IsEmpty y.Type] : Equiv x y := equiv_iff.2 <| by simp instance setoid : Setoid PSet := ⟨PSet.Equiv, Equiv.refl, Equiv.symm, Equiv.trans⟩ /-- A pre-set is a subset of another pre-set if every element of the first family is extensionally equivalent to some element of the second family. -/ protected def Subset (x y : PSet) : Prop := ∀ a, ∃ b, Equiv (x.Func a) (y.Func b) instance : HasSubset PSet := ⟨PSet.Subset⟩ instance : IsRefl PSet (· ⊆ ·) := ⟨fun _ a => ⟨a, Equiv.refl _⟩⟩ instance : IsTrans PSet (· ⊆ ·) := ⟨fun x y z hxy hyz a => by obtain ⟨b, hb⟩ := hxy a obtain ⟨c, hc⟩ := hyz b exact ⟨c, hb.trans hc⟩⟩ theorem Equiv.ext : ∀ x y : PSet, Equiv x y ↔ x ⊆ y ∧ y ⊆ x | ⟨_, _⟩, ⟨_, _⟩ => ⟨fun ⟨αβ, βα⟩ => ⟨αβ, fun b => let ⟨a, h⟩ := βα b ⟨a, Equiv.symm h⟩⟩, fun ⟨αβ, βα⟩ => ⟨αβ, fun b => let ⟨a, h⟩ := βα b ⟨a, Equiv.symm h⟩⟩⟩ theorem Subset.congr_left : ∀ {x y z : PSet}, Equiv x y → (x ⊆ z ↔ y ⊆ z) | ⟨_, _⟩, ⟨_, _⟩, ⟨_, _⟩, ⟨αβ, βα⟩ => ⟨fun αγ b => let ⟨a, ba⟩ := βα b let ⟨c, ac⟩ := αγ a ⟨c, (Equiv.symm ba).trans ac⟩, fun βγ a => let ⟨b, ab⟩ := αβ a let ⟨c, bc⟩ := βγ b ⟨c, Equiv.trans ab bc⟩⟩ theorem Subset.congr_right : ∀ {x y z : PSet}, Equiv x y → (z ⊆ x ↔ z ⊆ y) | ⟨_, _⟩, ⟨_, _⟩, ⟨_, _⟩, ⟨αβ, βα⟩ => ⟨fun γα c => let ⟨a, ca⟩ := γα c let ⟨b, ab⟩ := αβ a ⟨b, ca.trans ab⟩, fun γβ c => let ⟨b, cb⟩ := γβ c let ⟨a, ab⟩ := βα b ⟨a, cb.trans (Equiv.symm ab)⟩⟩ instance : Preorder PSet where le := (· ⊆ ·) le_refl := refl_of (· ⊆ ·) le_trans _ _ _ := trans_of (· ⊆ ·) instance : HasSSubset PSet := ⟨(· < ·)⟩ @[simp] theorem le_def (x y : PSet) : x ≤ y ↔ x ⊆ y := Iff.rfl @[simp] theorem lt_def (x y : PSet) : x < y ↔ x ⊂ y := Iff.rfl instance : IsNonstrictStrictOrder PSet (· ⊆ ·) (· ⊂ ·) := ⟨fun _ _ ↦ Iff.rfl⟩ /-- `x ∈ y` as pre-sets if `x` is extensionally equivalent to a member of the family `y`. -/ protected def Mem (y x : PSet.{u}) : Prop := ∃ b, Equiv x (y.Func b) instance : Membership PSet PSet := ⟨PSet.Mem⟩ theorem Mem.mk {α : Type u} (A : α → PSet) (a : α) : A a ∈ mk α A := ⟨a, Equiv.refl (A a)⟩ theorem func_mem (x : PSet) (i : x.Type) : x.Func i ∈ x := by cases x apply Mem.mk theorem Mem.ext : ∀ {x y : PSet.{u}}, (∀ w : PSet.{u}, w ∈ x ↔ w ∈ y) → Equiv x y | ⟨_, A⟩, ⟨_, B⟩, h => ⟨fun a => (h (A a)).1 (Mem.mk A a), fun b => let ⟨a, ha⟩ := (h (B b)).2 (Mem.mk B b) ⟨a, ha.symm⟩⟩ theorem Mem.congr_right : ∀ {x y : PSet.{u}}, Equiv x y → ∀ {w : PSet.{u}}, w ∈ x ↔ w ∈ y | ⟨_, _⟩, ⟨_, _⟩, ⟨αβ, βα⟩, _ => ⟨fun ⟨a, ha⟩ => let ⟨b, hb⟩ := αβ a ⟨b, ha.trans hb⟩, fun ⟨b, hb⟩ => let ⟨a, ha⟩ := βα b ⟨a, hb.euc ha⟩⟩ theorem equiv_iff_mem {x y : PSet.{u}} : Equiv x y ↔ ∀ {w : PSet.{u}}, w ∈ x ↔ w ∈ y := ⟨Mem.congr_right, match x, y with | ⟨_, A⟩, ⟨_, B⟩ => fun h => ⟨fun a => h.1 (Mem.mk A a), fun b => let ⟨a, h⟩ := h.2 (Mem.mk B b) ⟨a, h.symm⟩⟩⟩ theorem Mem.congr_left : ∀ {x y : PSet.{u}}, Equiv x y → ∀ {w : PSet.{u}}, x ∈ w ↔ y ∈ w | _, _, h, ⟨_, _⟩ => ⟨fun ⟨a, ha⟩ => ⟨a, h.symm.trans ha⟩, fun ⟨a, ha⟩ => ⟨a, h.trans ha⟩⟩ theorem mem_of_subset {x y z : PSet} : x ⊆ y → z ∈ x → z ∈ y | h₁, ⟨a, h₂⟩ => (h₁ a).elim fun b h₃ => ⟨b, h₂.trans h₃⟩ theorem subset_iff {x y : PSet} : x ⊆ y ↔ ∀ ⦃z⦄, z ∈ x → z ∈ y := ⟨fun h _ => mem_of_subset h, fun h a => h (Mem.mk _ a)⟩ private theorem mem_wf_aux : ∀ {x y : PSet.{u}}, Equiv x y → Acc (· ∈ ·) y | ⟨α, A⟩, ⟨β, B⟩, H => ⟨_, by rintro ⟨γ, C⟩ ⟨b, hc⟩ obtain ⟨a, ha⟩ := H.exists_right b have H := ha.trans hc.symm rw [mk_func] at H exact mem_wf_aux H⟩ theorem mem_wf : @WellFounded PSet (· ∈ ·) := ⟨fun x => mem_wf_aux <| Equiv.refl x⟩ instance : IsWellFounded PSet (· ∈ ·) := ⟨mem_wf⟩ instance : WellFoundedRelation PSet := ⟨_, mem_wf⟩ theorem mem_asymm {x y : PSet} : x ∈ y → y ∉ x := asymm_of (· ∈ ·) theorem mem_irrefl (x : PSet) : x ∉ x := irrefl_of (· ∈ ·) x theorem not_subset_of_mem {x y : PSet} (h : x ∈ y) : ¬ y ⊆ x := fun h' ↦ mem_irrefl _ <| mem_of_subset h' h theorem notMem_of_subset {x y : PSet} (h : x ⊆ y) : y ∉ x := imp_not_comm.2 not_subset_of_mem h @[deprecated (since := "2025-05-23")] alias not_mem_of_subset := notMem_of_subset /-- Convert a pre-set to a `Set` of pre-sets. -/ def toSet (u : PSet.{u}) : Set PSet.{u} := { x | x ∈ u } @[simp] theorem mem_toSet (a u : PSet.{u}) : a ∈ u.toSet ↔ a ∈ u := Iff.rfl /-- A nonempty set is one that contains some element. -/ protected def Nonempty (u : PSet) : Prop := u.toSet.Nonempty theorem nonempty_def (u : PSet) : u.Nonempty ↔ ∃ x, x ∈ u := Iff.rfl theorem nonempty_of_mem {x u : PSet} (h : x ∈ u) : u.Nonempty := ⟨x, h⟩ @[simp] theorem nonempty_toSet_iff {u : PSet} : u.toSet.Nonempty ↔ u.Nonempty := Iff.rfl theorem nonempty_type_iff_nonempty {x : PSet} : Nonempty x.Type ↔ PSet.Nonempty x := ⟨fun ⟨i⟩ => ⟨_, func_mem _ i⟩, fun ⟨_, j, _⟩ => ⟨j⟩⟩ theorem nonempty_of_nonempty_type (x : PSet) [h : Nonempty x.Type] : PSet.Nonempty x := nonempty_type_iff_nonempty.1 h /-- Two pre-sets are equivalent iff they have the same members. -/ theorem Equiv.eq {x y : PSet} : Equiv x y ↔ toSet x = toSet y := equiv_iff_mem.trans Set.ext_iff.symm instance : Coe PSet (Set PSet) := ⟨toSet⟩ /-- The empty pre-set -/ protected def empty : PSet := ⟨_, PEmpty.elim⟩ instance : EmptyCollection PSet := ⟨PSet.empty⟩ instance : Inhabited PSet := ⟨∅⟩ instance : IsEmpty («Type» ∅) := ⟨PEmpty.elim⟩ theorem empty_def : (∅ : PSet) = ⟨_, PEmpty.elim⟩ := by simp [EmptyCollection.emptyCollection, PSet.empty] @[simp] theorem notMem_empty (x : PSet.{u}) : x ∉ (∅ : PSet.{u}) := IsEmpty.exists_iff.1 @[deprecated (since := "2025-05-23")] alias not_mem_empty := notMem_empty @[simp] theorem toSet_empty : toSet ∅ = ∅ := by simp [toSet] @[simp] theorem empty_subset (x : PSet.{u}) : (∅ : PSet) ⊆ x := fun x => x.elim @[simp] theorem not_nonempty_empty : ¬PSet.Nonempty ∅ := by simp [PSet.Nonempty] protected theorem equiv_empty (x : PSet) [IsEmpty x.Type] : Equiv x ∅ := PSet.equiv_of_isEmpty x _ /-- Insert an element into a pre-set -/ protected def insert (x y : PSet) : PSet := ⟨Option y.Type, fun o => Option.casesOn o x y.Func⟩ instance : Insert PSet PSet := ⟨PSet.insert⟩ instance : Singleton PSet PSet := ⟨fun s => insert s ∅⟩ instance : LawfulSingleton PSet PSet := ⟨fun _ => rfl⟩ instance (x y : PSet) : Inhabited (insert x y).Type := inferInstanceAs (Inhabited <| Option y.Type) @[simp] theorem mem_insert_iff : ∀ {x y z : PSet.{u}}, x ∈ insert y z ↔ Equiv x y ∨ x ∈ z | x, y, ⟨α, A⟩ => show (x ∈ PSet.mk (Option α) fun o => Option.rec y A o) ↔ Equiv x y ∨ x ∈ PSet.mk α A from ⟨fun m => match m with | ⟨some a, ha⟩ => Or.inr ⟨a, ha⟩ | ⟨none, h⟩ => Or.inl h, fun m => match m with | Or.inr ⟨a, ha⟩ => ⟨some a, ha⟩ | Or.inl h => ⟨none, h⟩⟩ theorem mem_insert (x y : PSet) : x ∈ insert x y := mem_insert_iff.2 <| Or.inl Equiv.rfl theorem mem_insert_of_mem {y z : PSet} (x) (h : z ∈ y) : z ∈ insert x y := mem_insert_iff.2 <| Or.inr h @[simp] theorem mem_singleton {x y : PSet} : x ∈ ({y} : PSet) ↔ Equiv x y := mem_insert_iff.trans ⟨fun o => Or.rec id (fun n => absurd n (notMem_empty _)) o, Or.inl⟩ theorem mem_pair {x y z : PSet} : x ∈ ({y, z} : PSet) ↔ Equiv x y ∨ Equiv x z := by simp /-- The n-th von Neumann ordinal -/ def ofNat : ℕ → PSet | 0 => ∅ | n + 1 => insert (ofNat n) (ofNat n) /-- The von Neumann ordinal ω -/ def omega : PSet := ⟨ULift ℕ, fun n => ofNat n.down⟩ /-- The pre-set separation operation `{x ∈ a | p x}` -/ protected def sep (p : PSet → Prop) (x : PSet) : PSet := ⟨{ a // p (x.Func a) }, fun y => x.Func y.1⟩ instance : Sep PSet PSet := ⟨PSet.sep⟩ theorem mem_sep {p : PSet → Prop} (H : ∀ x y, Equiv x y → p x → p y) : ∀ {x y : PSet}, y ∈ PSet.sep p x ↔ y ∈ x ∧ p y | ⟨_, _⟩, _ => ⟨fun ⟨⟨a, pa⟩, h⟩ => ⟨⟨a, h⟩, H _ _ h.symm pa⟩, fun ⟨⟨a, h⟩, pa⟩ => ⟨⟨a, H _ _ h pa⟩, h⟩⟩ /-- The pre-set powerset operator -/ def powerset (x : PSet) : PSet := ⟨Set x.Type, fun p => ⟨{ a // p a }, fun y => x.Func y.1⟩⟩ @[simp] theorem mem_powerset : ∀ {x y : PSet}, y ∈ powerset x ↔ y ⊆ x | ⟨_, A⟩, ⟨_, B⟩ => ⟨fun ⟨_, e⟩ => (Subset.congr_left e).2 fun ⟨a, _⟩ => ⟨a, Equiv.refl (A a)⟩, fun βα => ⟨{ a | ∃ b, Equiv (B b) (A a) }, fun b => let ⟨a, ba⟩ := βα b ⟨⟨a, b, ba⟩, ba⟩, fun ⟨_, b, ba⟩ => ⟨b, ba⟩⟩⟩ /-- The pre-set union operator -/ def sUnion (a : PSet) : PSet := ⟨Σ x, (a.Func x).Type, fun ⟨x, y⟩ => (a.Func x).Func y⟩ @[inherit_doc] prefix:110 "⋃₀ " => sUnion @[simp] theorem mem_sUnion : ∀ {x y : PSet.{u}}, y ∈ ⋃₀ x ↔ ∃ z ∈ x, y ∈ z | ⟨α, A⟩, y => ⟨fun ⟨⟨a, c⟩, (e : Equiv y ((A a).Func c))⟩ => have : Func (A a) c ∈ mk (A a).Type (A a).Func := Mem.mk (A a).Func c ⟨_, Mem.mk _ _, (Mem.congr_left e).2 (by rwa [eta] at this)⟩, fun ⟨⟨β, B⟩, ⟨a, (e : Equiv (mk β B) (A a))⟩, ⟨b, yb⟩⟩ => by rw [← eta (A a)] at e exact let ⟨βt, _⟩ := e let ⟨c, bc⟩ := βt b ⟨⟨a, c⟩, yb.trans bc⟩⟩ @[simp] theorem toSet_sUnion (x : PSet.{u}) : (⋃₀ x).toSet = ⋃₀ (toSet '' x.toSet) := by ext simp /-- The image of a function from pre-sets to pre-sets. -/ def image (f : PSet.{u} → PSet.{u}) (x : PSet.{u}) : PSet := ⟨x.Type, f ∘ x.Func⟩ theorem mem_image {f : PSet.{u} → PSet.{u}} (H : ∀ x y, Equiv x y → Equiv (f x) (f y)) : ∀ {x y : PSet.{u}}, y ∈ image f x ↔ ∃ z ∈ x, Equiv y (f z) | ⟨_, A⟩, _ => ⟨fun ⟨a, ya⟩ => ⟨A a, Mem.mk A a, ya⟩, fun ⟨_, ⟨a, za⟩, yz⟩ => ⟨a, yz.trans <| H _ _ za⟩⟩ /-- Universe lift operation -/ protected def Lift : PSet.{u} → PSet.{max u v} | ⟨α, A⟩ => ⟨ULift.{v, u} α, fun ⟨x⟩ => PSet.Lift (A x)⟩ -- intended to be used with explicit universe parameters /-- Embedding of one universe in another -/ @[nolint checkUnivs] def embed : PSet.{max (u + 1) v} := ⟨ULift.{v, u + 1} PSet, fun ⟨x⟩ => PSet.Lift.{u, max (u + 1) v} x⟩ theorem lift_mem_embed : ∀ x : PSet.{u}, PSet.Lift.{u, max (u + 1) v} x ∈ embed.{u, v} := fun x => ⟨⟨x⟩, Equiv.rfl⟩ end PSet
.lake/packages/mathlib/Mathlib/SetTheory/Surreal/Dyadic.lean
import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Order.Group.Basic import Mathlib.Algebra.Ring.Regular import Mathlib.GroupTheory.MonoidLocalization.Away import Mathlib.RingTheory.Localization.Defs import Mathlib.SetTheory.Game.Birthday import Mathlib.SetTheory.Surreal.Multiplication import Mathlib.Tactic.Linarith import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Surreal.Dyadic` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Dyadic numbers Dyadic numbers are obtained by localizing ℤ away from 2. They are the initial object in the category of rings with no 2-torsion. ## Dyadic surreal numbers We construct dyadic surreal numbers using the canonical map from ℤ[2 ^ {-1}] to surreals. As we currently do not have a ring structure on `Surreal` we construct this map explicitly. Once we have the ring structure, this map can be constructed directly by sending `2 ^ {-1}` to `half`. ## Embeddings The above construction gives us an abelian group embedding of ℤ into `Surreal`. The goal is to extend this to an embedding of dyadic rationals into `Surreal` and use Cauchy sequences of dyadic rational numbers to construct an ordered field embedding of ℝ into `Surreal`. -/ universe u namespace SetTheory namespace PGame /-- For a natural number `n`, the pre-game `powHalf (n + 1)` is recursively defined as `{0 | powHalf n}`. These are the explicit expressions of powers of `1 / 2`. By definition, we have `powHalf 0 = 1` and `powHalf 1 ≈ 1 / 2` and we prove later on that `powHalf (n + 1) + powHalf (n + 1) ≈ powHalf n`. -/ def powHalf : ℕ → PGame | 0 => 1 | n + 1 => ⟨PUnit, PUnit, 0, fun _ => powHalf n⟩ @[simp] theorem powHalf_zero : powHalf 0 = 1 := rfl theorem powHalf_leftMoves (n) : (powHalf n).LeftMoves = PUnit := by cases n <;> rfl theorem powHalf_zero_rightMoves : (powHalf 0).RightMoves = PEmpty := rfl theorem powHalf_succ_rightMoves (n) : (powHalf (n + 1)).RightMoves = PUnit := rfl @[simp] theorem powHalf_moveLeft (n i) : (powHalf n).moveLeft i = 0 := by cases n <;> cases i <;> rfl @[simp] theorem powHalf_succ_moveRight (n i) : (powHalf (n + 1)).moveRight i = powHalf n := rfl instance uniquePowHalfLeftMoves (n) : Unique (powHalf n).LeftMoves := by cases n <;> exact PUnit.instUnique instance isEmpty_powHalf_zero_rightMoves : IsEmpty (powHalf 0).RightMoves := inferInstanceAs (IsEmpty PEmpty) instance uniquePowHalfSuccRightMoves (n) : Unique (powHalf (n + 1)).RightMoves := PUnit.instUnique @[simp] theorem birthday_half : birthday (powHalf 1) = 2 := by rw [birthday_def]; simp /-- For all natural numbers `n`, the pre-games `powHalf n` are numeric. -/ theorem numeric_powHalf (n) : (powHalf n).Numeric := by induction n with | zero => exact numeric_one | succ n hn => constructor · simpa using hn.moveLeft_lt default · exact ⟨fun _ => numeric_zero, fun _ => hn⟩ theorem powHalf_succ_lt_powHalf (n : ℕ) : powHalf (n + 1) < powHalf n := (numeric_powHalf (n + 1)).lt_moveRight default theorem powHalf_succ_le_powHalf (n : ℕ) : powHalf (n + 1) ≤ powHalf n := (powHalf_succ_lt_powHalf n).le theorem powHalf_le_one (n : ℕ) : powHalf n ≤ 1 := by induction n with | zero => exact le_rfl | succ n hn => exact (powHalf_succ_le_powHalf n).trans hn theorem powHalf_succ_lt_one (n : ℕ) : powHalf (n + 1) < 1 := (powHalf_succ_lt_powHalf n).trans_le <| powHalf_le_one n theorem powHalf_pos (n : ℕ) : 0 < powHalf n := by rw [← lf_iff_lt numeric_zero (numeric_powHalf n), zero_lf_le]; simp theorem zero_le_powHalf (n : ℕ) : 0 ≤ powHalf n := (powHalf_pos n).le theorem add_powHalf_succ_self_eq_powHalf (n) : powHalf (n + 1) + powHalf (n + 1) ≈ powHalf n := by induction n using Nat.strong_induction_on with | _ n hn constructor <;> rw [le_iff_forall_lf] <;> constructor · rintro (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> apply lf_of_lt · calc 0 + powHalf n.succ ≈ powHalf n.succ := zero_add_equiv _ _ < powHalf n := powHalf_succ_lt_powHalf n · calc powHalf n.succ + 0 ≈ powHalf n.succ := add_zero_equiv _ _ < powHalf n := powHalf_succ_lt_powHalf n · rcases n with - | n · rintro ⟨⟩ rintro ⟨⟩ apply lf_of_moveRight_le swap · exact Sum.inl default calc powHalf n.succ + powHalf (n.succ + 1) ≤ powHalf n.succ + powHalf n.succ := by grw [powHalf_succ_le_powHalf (n + 1)] _ ≈ powHalf n := hn _ (Nat.lt_succ_self n) · simp only [powHalf_moveLeft, forall_const] apply lf_of_lt calc 0 ≈ 0 + 0 := Equiv.symm (add_zero_equiv 0) _ ≤ powHalf n.succ + 0 := by grw [← zero_le_powHalf] _ < powHalf n.succ + powHalf n.succ := by gcongr; exact powHalf_pos _ · rintro (⟨⟨⟩⟩ | ⟨⟨⟩⟩) <;> apply lf_of_lt · calc powHalf n ≈ powHalf n + 0 := Equiv.symm (add_zero_equiv _) _ < powHalf n + powHalf n.succ := by gcongr; exact powHalf_pos _ · calc powHalf n ≈ 0 + powHalf n := Equiv.symm (zero_add_equiv _) _ < powHalf n.succ + powHalf n := by gcongr; exact powHalf_pos _ theorem half_add_half_equiv_one : powHalf 1 + powHalf 1 ≈ 1 := add_powHalf_succ_self_eq_powHalf 0 end PGame end SetTheory namespace Surreal open SetTheory PGame /-- Powers of the surreal number `half`. -/ def powHalf (n : ℕ) : Surreal := ⟦⟨PGame.powHalf n, PGame.numeric_powHalf n⟩⟧ @[simp] theorem powHalf_zero : powHalf 0 = 1 := rfl @[simp] theorem double_powHalf_succ_eq_powHalf (n : ℕ) : 2 * powHalf (n + 1) = powHalf n := by rw [two_mul]; exact Quotient.sound (PGame.add_powHalf_succ_self_eq_powHalf n) @[simp] theorem nsmul_pow_two_powHalf (n : ℕ) : 2 ^ n * powHalf n = 1 := by induction n with | zero => simp only [pow_zero, powHalf_zero, mul_one] | succ n hn => rw [← hn, ← double_powHalf_succ_eq_powHalf n, ← mul_assoc (2 ^ n) 2 (powHalf (n + 1)), pow_succ', mul_comm 2 (2 ^ n)] @[simp] theorem nsmul_pow_two_powHalf' (n k : ℕ) : 2 ^ n * powHalf (n + k) = powHalf k := by induction k with | zero => simp only [add_zero, Surreal.nsmul_pow_two_powHalf, Surreal.powHalf_zero] | succ k hk => rw [← double_powHalf_succ_eq_powHalf (n + k), ← double_powHalf_succ_eq_powHalf k, ← mul_assoc, mul_comm (2 ^ n) 2, mul_assoc] at hk rw [← zsmul_eq_zsmul_iff' two_ne_zero] simpa only [zsmul_eq_mul, Int.cast_ofNat] theorem zsmul_pow_two_powHalf (m : ℤ) (n k : ℕ) : (m * 2 ^ n) * powHalf (n + k) = m * powHalf k := by rw [mul_assoc] congr exact nsmul_pow_two_powHalf' n k theorem dyadic_aux {m₁ m₂ : ℤ} {y₁ y₂ : ℕ} (h₂ : m₁ * 2 ^ y₁ = m₂ * 2 ^ y₂) : m₁ * powHalf y₂ = m₂ * powHalf y₁ := by revert m₁ m₂ wlog h : y₁ ≤ y₂ · intro m₁ m₂ aux; exact (this (le_of_not_ge h) aux.symm).symm intro m₁ m₂ h₂ obtain ⟨c, rfl⟩ := le_iff_exists_add.mp h rw [add_comm, pow_add, ← mul_assoc, mul_eq_mul_right_iff] at h₂ rcases h₂ with h₂ | h₂ · rw [h₂, add_comm] simp_rw [Int.cast_mul, Int.cast_pow, Int.cast_ofNat, zsmul_pow_two_powHalf m₂ c y₁] · have := Nat.one_le_pow y₁ 2 Nat.succ_pos' norm_cast at h₂; cutsat /-- The additive monoid morphism `dyadicMap` sends ⟦⟨m, 2^n⟩⟧ to m • half ^ n. -/ noncomputable def dyadicMap : Localization.Away (2 : ℤ) →+ Surreal where toFun x := (Localization.liftOn x fun x y => x * powHalf (Submonoid.log y)) <| by intro m₁ m₂ n₁ n₂ h₁ obtain ⟨⟨n₃, y₃, hn₃⟩, h₂⟩ := Localization.r_iff_exists.mp h₁ simp only [mul_eq_mul_left_iff] at h₂ cases h₂ · obtain ⟨a₁, ha₁⟩ := n₁.prop obtain ⟨a₂, ha₂⟩ := n₂.prop simp only at ha₁ ha₂ ⊢ have hn₁ : n₁ = Submonoid.pow 2 a₁ := Subtype.ext ha₁.symm have hn₂ : n₂ = Submonoid.pow 2 a₂ := Subtype.ext ha₂.symm have h₂ : 1 < (2 : ℤ).natAbs := one_lt_two rw [hn₁, hn₂, Submonoid.log_pow_int_eq_self h₂, Submonoid.log_pow_int_eq_self h₂] apply dyadic_aux rwa [ha₁, ha₂, mul_comm, mul_comm m₂] · have : (1 : ℤ) ≤ 2 ^ y₃ := mod_cast Nat.one_le_pow y₃ 2 Nat.succ_pos' linarith map_zero' := by simp_rw [Localization.liftOn_zero _ _, Int.cast_zero, zero_mul] map_add' x y := Localization.induction_on₂ x y <| by rintro ⟨a, ⟨b, ⟨b', rfl⟩⟩⟩ ⟨c, ⟨d, ⟨d', rfl⟩⟩⟩ have h₂ : 1 < (2 : ℤ).natAbs := one_lt_two have hpow₂ := Submonoid.log_pow_int_eq_self h₂ simp_rw [Submonoid.pow_apply] at hpow₂ simp_rw [Localization.add_mk, Localization.liftOn_mk, Submonoid.log_mul (Int.pow_right_injective h₂), hpow₂] simp only [Int.cast_add, Int.cast_mul, Int.cast_pow, Int.cast_ofNat] calc (2 ^ b' * c + 2 ^ d' * a) * powHalf (b' + d') = (c * 2 ^ b') * powHalf (b' + d') + (a * 2 ^ d') * powHalf (d' + b') := by simp only [right_distrib, mul_comm, add_comm] _ = c * powHalf d' + a * powHalf b' := by simp only [zsmul_pow_two_powHalf] _ = a * powHalf b' + c * powHalf d' := add_comm _ _ @[simp] theorem dyadicMap_apply (m : ℤ) (p : Submonoid.powers (2 : ℤ)) : dyadicMap (IsLocalization.mk' (Localization (Submonoid.powers 2)) m p) = m * powHalf (Submonoid.log p) := by rw [← Localization.mk_eq_mk']; rfl theorem dyadicMap_apply_pow (m : ℤ) (n : ℕ) : dyadicMap (IsLocalization.mk' (Localization (Submonoid.powers 2)) m (Submonoid.pow 2 n)) = m • powHalf n := by rw [dyadicMap_apply, @Submonoid.log_pow_int_eq_self 2 one_lt_two] simp only [zsmul_eq_mul] @[simp] theorem dyadicMap_apply_pow' (m : ℤ) (n : ℕ) : m * Surreal.powHalf (Submonoid.log (Submonoid.pow (2 : ℤ) n)) = m * powHalf n := by rw [@Submonoid.log_pow_int_eq_self 2 one_lt_two] /-- We define dyadic surreals as the range of the map `dyadicMap`. -/ def dyadic : Set Surreal := Set.range dyadicMap -- We conclude with some ideas for further work on surreals; these would make fun projects. -- TODO show that the map from dyadic rationals to surreals is injective -- TODO map the reals into the surreals, using dyadic Dedekind cuts -- TODO show this is a group homomorphism, and injective -- TODO show the maps from the dyadic rationals and from the reals -- into the surreals are multiplicative end Surreal
.lake/packages/mathlib/Mathlib/SetTheory/Surreal/Multiplication.lean
import Mathlib.Logic.Hydra import Mathlib.SetTheory.Surreal.Basic import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Surreal.Multiplication` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! ### Surreal multiplication In this file, we show that multiplication of surreal numbers is well-defined, and thus the surreal numbers form a linear ordered commutative ring. An inductive argument proves the following three main theorems: * P1: being numeric is closed under multiplication, * P2: multiplying a numeric pregame by equivalent numeric pregames results in equivalent pregames, * P3: the product of two positive numeric pregames is positive (`mul_pos`). This is Theorem 8 in [Conway2001], or Theorem 3.8 in [SchleicherStoll]. P1 allows us to define multiplication as an operation on numeric pregames, P2 says that this is well-defined as an operation on the quotient by `PGame.Equiv`, namely the surreal numbers, and P3 is an axiom that needs to be satisfied for the surreals to be a `OrderedRing`. We follow the proof in [SchleicherStoll], except that we use the well-foundedness of the hydra relation `CutExpand` on `Multiset PGame` instead of the argument based on a depth function in the paper. In the argument, P3 is stated with four variables `x₁`, `x₂`, `y₁`, `y₂` satisfying `x₁ < x₂` and `y₁ < y₂`, and says that `x₁ * y₂ + x₂ * x₁ < x₁ * y₁ + x₂ * y₂`, which is equivalent to `0 < x₂ - x₁ → 0 < y₂ - y₁ → 0 < (x₂ - x₁) * (y₂ - y₁)`, i.e. `@mul_pos PGame _ (x₂ - x₁) (y₂ - y₁)`. It has to be stated in this form and not in terms of `mul_pos` because we need to show P1, P2 and (a specialized form of) P3 simultaneously, and for example `P1 x y` will be deduced from P3 with variables taking values simpler than `x` or `y` (among other induction hypotheses), but if you subtract two pregames simpler than `x` or `y`, the result may no longer be simpler. The specialized version of P3 is called P4, which takes only three arguments `x₁`, `x₂`, `y` and requires that `y₂ = y` or `-y` and that `y₁` is a left option of `y₂`. After P1, P2 and P4 are shown, a further inductive argument (this time using the `GameAdd` relation) proves P3 in full. Implementation strategy of the inductive argument: we * extract specialized versions (`IH1`, `IH2`, `IH3`, `IH4` and `IH24`) of the induction hypothesis that are easier to apply (taking `IsOption` arguments directly), and * show they are invariant under certain symmetries (permutation and negation of arguments) and that the induction hypothesis indeed implies the specialized versions. * utilize the symmetries to minimize calculation. The whole proof features a clear separation into lemmas of different roles: * verification of symmetry properties of P and IH (`P3_comm`, `ih1_neg_left`, etc.), * calculations that connect P1, P2, P3, and inequalities between the product of two surreals and its options (`mulOption_lt_iff_P1`, etc.), * specializations of the induction hypothesis (`numeric_option_mul`, `ih1`, `ih1_swap`, `ih₁₂`, `ih4`, etc.), * application of specialized induction hypothesis (`P1_of_ih`, `mul_right_le_of_equiv`, `P3_of_lt`, etc.). ## References * [Conway, *On numbers and games*][Conway2001] * [Schleicher, Stoll, *An introduction to Conway's games and numbers*][SchleicherStoll] -/ universe u open SetTheory Game PGame WellFounded namespace Surreal.Multiplication /-- The nontrivial part of P1 in [SchleicherStoll] says that the left options of `x * y` are less than the right options, and this is the general form of these statements. -/ def P1 (x₁ x₂ x₃ y₁ y₂ y₃ : PGame) := ⟦x₁ * y₁⟧ + ⟦x₂ * y₂⟧ - ⟦x₁ * y₂⟧ < ⟦x₃ * y₁⟧ + ⟦x₂ * y₃⟧ - (⟦x₃ * y₃⟧ : Game) /-- The proposition P2, without numericity assumptions. -/ def P2 (x₁ x₂ y : PGame) := x₁ ≈ x₂ → ⟦x₁ * y⟧ = (⟦x₂ * y⟧ : Game) /-- The proposition P3, without the `x₁ < x₂` and `y₁ < y₂` assumptions. -/ def P3 (x₁ x₂ y₁ y₂ : PGame) := ⟦x₁ * y₂⟧ + ⟦x₂ * y₁⟧ < ⟦x₁ * y₁⟧ + (⟦x₂ * y₂⟧ : Game) /-- The proposition P4, without numericity assumptions. In the references, the second part of the conjunction is stated as `∀ j, P3 x₁ x₂ y (y.moveRight j)`, which is equivalent to our statement by `P3_comm` and `P3_neg`. We choose to state everything in terms of left options for uniform treatment. -/ def P4 (x₁ x₂ y : PGame) := x₁ < x₂ → (∀ i, P3 x₁ x₂ (y.moveLeft i) y) ∧ ∀ j, P3 x₁ x₂ ((-y).moveLeft j) (-y) /-- The conjunction of P2 and P4. -/ def P24 (x₁ x₂ y : PGame) : Prop := P2 x₁ x₂ y ∧ P4 x₁ x₂ y variable {x x₁ x₂ x₃ x' y y₁ y₂ y₃ y' : PGame.{u}} /-! #### Symmetry properties of P1, P2, P3, and P4 -/ lemma P3_comm : P3 x₁ x₂ y₁ y₂ ↔ P3 y₁ y₂ x₁ x₂ := by rw [P3, P3, add_comm] congr! 2 <;> rw [quot_mul_comm] lemma P3.trans (h₁ : P3 x₁ x₂ y₁ y₂) (h₂ : P3 x₂ x₃ y₁ y₂) : P3 x₁ x₃ y₁ y₂ := by rw [P3] at h₁ h₂ rw [P3, ← add_lt_add_iff_left (⟦x₂ * y₁⟧ + ⟦x₂ * y₂⟧)] convert add_lt_add h₁ h₂ using 1 <;> abel lemma P3_neg : P3 x₁ x₂ y₁ y₂ ↔ P3 (-x₂) (-x₁) y₁ y₂ := by simp_rw [P3, quot_neg_mul] rw [← _root_.neg_lt_neg_iff] abel_nf lemma P2_neg_left : P2 x₁ x₂ y ↔ P2 (-x₂) (-x₁) y := by rw [P2, P2] constructor · rw [quot_neg_mul, quot_neg_mul, eq_comm, neg_inj, neg_equiv_neg_iff, PGame.equiv_comm] exact (· ·) · rw [PGame.equiv_comm, neg_equiv_neg_iff, quot_neg_mul, quot_neg_mul, neg_inj, eq_comm] exact (· ·) lemma P2_neg_right : P2 x₁ x₂ y ↔ P2 x₁ x₂ (-y) := by rw [P2, P2, quot_mul_neg, quot_mul_neg, neg_inj] lemma P4_neg_left : P4 x₁ x₂ y ↔ P4 (-x₂) (-x₁) y := by simp_rw [P4, PGame.neg_lt_neg_iff, moveLeft_neg, ← P3_neg] lemma P4_neg_right : P4 x₁ x₂ y ↔ P4 x₁ x₂ (-y) := by rw [P4, P4, neg_neg, and_comm] lemma P24_neg_left : P24 x₁ x₂ y ↔ P24 (-x₂) (-x₁) y := by rw [P24, P24, P2_neg_left, P4_neg_left] lemma P24_neg_right : P24 x₁ x₂ y ↔ P24 x₁ x₂ (-y) := by rw [P24, P24, P2_neg_right, P4_neg_right] /-! #### Explicit calculations necessary for the main proof -/ lemma mulOption_lt_iff_P1 {i j k l} : (⟦mulOption x y i k⟧ : Game) < -⟦mulOption x (-y) j l⟧ ↔ P1 (x.moveLeft i) x (x.moveLeft j) y (y.moveLeft k) (-(-y).moveLeft l) := by dsimp only [P1, mulOption, quot_sub, quot_add] simp_rw [neg_sub', neg_add, quot_mul_neg, neg_neg] lemma mulOption_lt_mul_iff_P3 {i j} : ⟦mulOption x y i j⟧ < (⟦x * y⟧ : Game) ↔ P3 (x.moveLeft i) x (y.moveLeft j) y := by dsimp only [mulOption, quot_sub, quot_add] exact sub_lt_iff_lt_add' lemma P1_of_eq (he : x₁ ≈ x₃) (h₁ : P2 x₁ x₃ y₁) (h₃ : P2 x₁ x₃ y₃) (h3 : P3 x₁ x₂ y₂ y₃) : P1 x₁ x₂ x₃ y₁ y₂ y₃ := by rw [P1, ← h₁ he, ← h₃ he, sub_lt_sub_iff] convert add_lt_add_left h3 ⟦x₁ * y₁⟧ using 1 <;> abel lemma P1_of_lt (h₁ : P3 x₃ x₂ y₂ y₃) (h₂ : P3 x₁ x₃ y₂ y₁) : P1 x₁ x₂ x₃ y₁ y₂ y₃ := by rw [P1, sub_lt_sub_iff, ← add_lt_add_iff_left ⟦x₃ * y₂⟧] convert add_lt_add h₁ h₂ using 1 <;> abel /-- The type of lists of arguments for P1, P2, and P4. -/ inductive Args : Type (u + 1) | P1 (x y : PGame.{u}) : Args | P24 (x₁ x₂ y : PGame.{u}) : Args /-- The multiset associated to a list of arguments. -/ def Args.toMultiset : Args → Multiset PGame | (Args.P1 x y) => {x, y} | (Args.P24 x₁ x₂ y) => {x₁, x₂, y} /-- A list of arguments is numeric if all the arguments are. -/ def Args.Numeric (a : Args) := ∀ x ∈ a.toMultiset, SetTheory.PGame.Numeric x lemma Args.numeric_P1 {x y} : (Args.P1 x y).Numeric ↔ x.Numeric ∧ y.Numeric := by simp [Args.Numeric, Args.toMultiset] lemma Args.numeric_P24 {x₁ x₂ y} : (Args.P24 x₁ x₂ y).Numeric ↔ x₁.Numeric ∧ x₂.Numeric ∧ y.Numeric := by simp [Args.Numeric, Args.toMultiset] open Relation /-- The relation specifying when a list of (pregame) arguments is considered simpler than another: `ArgsRel a₁ a₂` is true if `a₁`, considered as a multiset, can be obtained from `a₂` by repeatedly removing a pregame from `a₂` and adding back one or two options of the pregame. -/ def ArgsRel := InvImage (TransGen <| CutExpand IsOption) Args.toMultiset /-- `ArgsRel` is well-founded. -/ theorem argsRel_wf : WellFounded ArgsRel := InvImage.wf _ wf_isOption.cutExpand.transGen /-- The statement that we will show by induction using the well-founded relation `ArgsRel`. -/ def P124 : Args → Prop | (Args.P1 x y) => Numeric (x * y) | (Args.P24 x₁ x₂ y) => P24 x₁ x₂ y /-- The property that all arguments are numeric is leftward-closed under `ArgsRel`. -/ lemma ArgsRel.numeric_closed {a' a} : ArgsRel a' a → a.Numeric → a'.Numeric := TransGen.closed' <| @cutExpand_closed _ IsOption ⟨wf_isOption.isIrrefl.1⟩ _ Numeric.isOption /-- A specialized induction hypothesis used to prove P1. -/ def IH1 (x y : PGame) : Prop := ∀ ⦃x₁ x₂ y'⦄, IsOption x₁ x → IsOption x₂ x → (y' = y ∨ IsOption y' y) → P24 x₁ x₂ y' /-! #### Symmetry properties of `IH1` -/ lemma ih1_neg_left : IH1 x y → IH1 (-x) y := fun h x₁ x₂ y' h₁ h₂ hy ↦ by rw [isOption_neg] at h₁ h₂ exact P24_neg_left.2 (h h₂ h₁ hy) lemma ih1_neg_right : IH1 x y → IH1 x (-y) := fun h x₁ x₂ y' ↦ by rw [← neg_eq_iff_eq_neg, isOption_neg, P24_neg_right] apply h /-! #### Specialize `ih` to obtain specialized induction hypotheses for P1 -/ lemma numeric_option_mul (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (h : IsOption x' x) : (x' * y).Numeric := ih (Args.P1 x' y) (TransGen.single <| cutExpand_pair_left h) lemma numeric_mul_option (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (h : IsOption y' y) : (x * y').Numeric := ih (Args.P1 x y') (TransGen.single <| cutExpand_pair_right h) lemma numeric_option_mul_option (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (hx : IsOption x' x) (hy : IsOption y' y) : (x' * y').Numeric := ih (Args.P1 x' y') ((TransGen.single <| cutExpand_pair_right hy).tail <| cutExpand_pair_left hx) lemma ih1 (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) : IH1 x y := by rintro x₁ x₂ y' h₁ h₂ (rfl | hy) <;> apply ih (Args.P24 _ _ _) on_goal 2 => refine TransGen.tail ?_ (cutExpand_pair_right hy) all_goals exact TransGen.single (cutExpand_double_left h₁ h₂) lemma ih1_swap (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) : IH1 y x := ih1 <| by simp_rw [ArgsRel, InvImage, Args.toMultiset, Multiset.pair_comm] at ih ⊢ exact ih lemma P3_of_ih (hy : Numeric y) (ihyx : IH1 y x) (i k l) : P3 (x.moveLeft i) x (y.moveLeft k) (-(-y).moveLeft l) := P3_comm.2 <| ((ihyx (IsOption.moveLeft k) (isOption_neg.1 <| .moveLeft l) <| Or.inl rfl).2 (by rw [moveLeft_neg, neg_neg]; apply hy.left_lt_right)).1 i lemma P24_of_ih (ihxy : IH1 x y) (i j) : P24 (x.moveLeft i) (x.moveLeft j) y := ihxy (IsOption.moveLeft i) (IsOption.moveLeft j) (Or.inl rfl) section lemma mulOption_lt_of_lt (hy : y.Numeric) (ihxy : IH1 x y) (ihyx : IH1 y x) (i j k l) (h : x.moveLeft i < x.moveLeft j) : (⟦mulOption x y i k⟧ : Game) < -⟦mulOption x (-y) j l⟧ := mulOption_lt_iff_P1.2 <| P1_of_lt (P3_of_ih hy ihyx j k l) <| ((P24_of_ih ihxy i j).2 h).1 k lemma mulOption_lt (hx : x.Numeric) (hy : y.Numeric) (ihxy : IH1 x y) (ihyx : IH1 y x) (i j k l) : (⟦mulOption x y i k⟧ : Game) < -⟦mulOption x (-y) j l⟧ := by obtain (h | h | h) := lt_or_equiv_or_gt (hx.moveLeft i) (hx.moveLeft j) · exact mulOption_lt_of_lt hy ihxy ihyx i j k l h · have ml := @IsOption.moveLeft exact mulOption_lt_iff_P1.2 (P1_of_eq h (P24_of_ih ihxy i j).1 (ihxy (ml i) (ml j) <| Or.inr <| isOption_neg.1 <| ml l).1 <| P3_of_ih hy ihyx i k l) · rw [mulOption_neg_neg, lt_neg] exact mulOption_lt_of_lt hy.neg (ih1_neg_right ihxy) (ih1_neg_left ihyx) j i l _ h end /-- P1 follows from the induction hypothesis. -/ theorem P1_of_ih (ih : ∀ a, ArgsRel a (Args.P1 x y) → P124 a) (hx : x.Numeric) (hy : y.Numeric) : (x * y).Numeric := by have ihxy := ih1 ih have ihyx := ih1_swap ih have ihxyn := ih1_neg_left (ih1_neg_right ihxy) have ihyxn := ih1_neg_left (ih1_neg_right ihyx) refine numeric_def.mpr ⟨?_, ?_, ?_⟩ · simp_rw [lt_iff_game_lt] intro i rw [rightMoves_mul_iff] constructor <;> (intro j l; revert i; rw [leftMoves_mul_iff (_ > ·)]; constructor <;> intro i k) · apply mulOption_lt hx hy ihxy ihyx · simp_rw [← mulOption_symm (-y), mulOption_neg_neg x] apply mulOption_lt hy.neg hx.neg ihyxn ihxyn · simp only [← mulOption_symm y] apply mulOption_lt hy hx ihyx ihxy · rw [mulOption_neg_neg y] apply mulOption_lt hx.neg hy.neg ihxyn ihyxn all_goals cases x; cases y rintro (⟨i, j⟩ | ⟨i, j⟩) <;> refine ((numeric_option_mul ih ?_).add <| numeric_mul_option ih ?_).sub (numeric_option_mul_option ih ?_ ?_) <;> solve_by_elim [IsOption.mk_left, IsOption.mk_right] /-- A specialized induction hypothesis used to prove P2 and P4. -/ def IH24 (x₁ x₂ y : PGame) : Prop := ∀ ⦃z⦄, (IsOption z x₁ → P24 z x₂ y) ∧ (IsOption z x₂ → P24 x₁ z y) ∧ (IsOption z y → P24 x₁ x₂ z) /-- A specialized induction hypothesis used to prove P4. -/ def IH4 (x₁ x₂ y : PGame) : Prop := ∀ ⦃z w⦄, IsOption w y → (IsOption z x₁ → P2 z x₂ w) ∧ (IsOption z x₂ → P2 x₁ z w) /-! #### Specialize `ih'` to obtain specialized induction hypotheses for P2 and P4 -/ lemma ih₁₂ (ih' : ∀ a, ArgsRel a (Args.P24 x₁ x₂ y) → P124 a) : IH24 x₁ x₂ y := by rw [IH24] refine fun z ↦ ⟨?_, ?_, ?_⟩ <;> refine fun h ↦ ih' (Args.P24 _ _ _) (TransGen.single ?_) · exact (cutExpand_add_right {y}).2 (cutExpand_pair_left h) · exact (cutExpand_add_left {x₁}).2 (cutExpand_pair_left h) · exact (cutExpand_add_left {x₁}).2 (cutExpand_pair_right h) lemma ih₂₁ (ih' : ∀ a, ArgsRel a (Args.P24 x₁ x₂ y) → P124 a) : IH24 x₂ x₁ y := ih₁₂ <| by simp_rw [ArgsRel, InvImage, Args.toMultiset, Multiset.pair_comm] at ih' ⊢ suffices {x₁, y, x₂} = {x₂, y, x₁} by rwa [← this] dsimp only [Multiset.insert_eq_cons, ← Multiset.singleton_add] at ih' ⊢ abel lemma ih4 (ih' : ∀ a, ArgsRel a (Args.P24 x₁ x₂ y) → P124 a) : IH4 x₁ x₂ y := by refine fun z w h ↦ ⟨?_, ?_⟩ all_goals intro h' apply (ih' (Args.P24 _ _ _) <| (TransGen.single _).tail <| (cutExpand_add_left {x₁}).2 <| cutExpand_pair_right h).1 try exact (cutExpand_add_right {w}).2 <| cutExpand_pair_left h' try exact (cutExpand_add_right {w}).2 <| cutExpand_pair_right h' lemma numeric_of_ih (ih' : ∀ a, ArgsRel a (Args.P24 x₁ x₂ y) → P124 a) : (x₁ * y).Numeric ∧ (x₂ * y).Numeric := by constructor <;> refine ih' (Args.P1 _ _) (TransGen.single ?_) · exact (cutExpand_add_right {y}).2 <| (cutExpand_add_left {x₁}).2 cutExpand_zero · exact (cutExpand_add_right {x₂, y}).2 cutExpand_zero /-- Symmetry properties of `IH24`. -/ lemma ih24_neg : IH24 x₁ x₂ y → IH24 (-x₂) (-x₁) y ∧ IH24 x₁ x₂ (-y) := by simp_rw [IH24, ← P24_neg_right, isOption_neg] refine fun h ↦ ⟨fun z ↦ ⟨?_, ?_, ?_⟩, fun z ↦ ⟨(@h z).1, (@h z).2.1, P24_neg_right.2 ∘ (@h <| -z).2.2⟩⟩ all_goals rw [P24_neg_left] simp only [neg_neg] first | exact (@h <| -z).2.1 | exact (@h <| -z).1 | exact (@h z).2.2 /-- Symmetry properties of `IH4`. -/ lemma ih4_neg : IH4 x₁ x₂ y → IH4 (-x₂) (-x₁) y ∧ IH4 x₁ x₂ (-y) := by simp_rw [IH4, isOption_neg] refine fun h ↦ ⟨fun z w h' ↦ ?_, fun z w h' ↦ ?_⟩ · convert (h h').symm using 2 <;> rw [P2_neg_left, neg_neg] · convert h h' using 2 <;> rw [P2_neg_right] lemma mulOption_lt_mul_of_equiv (hn : x₁.Numeric) (h : IH24 x₁ x₂ y) (he : x₁ ≈ x₂) (i j) : ⟦mulOption x₁ y i j⟧ < (⟦x₂ * y⟧ : Game) := by convert sub_lt_iff_lt_add'.2 ((((@h _).1 <| IsOption.moveLeft i).2 _).1 j) using 1 · rw [← ((@h _).2.2 <| IsOption.moveLeft j).1 he] rfl · rw [← lt_congr_right he] apply hn.moveLeft_lt /-- P2 follows from specialized induction hypotheses (one half of the equality). -/ theorem mul_right_le_of_equiv (h₁ : x₁.Numeric) (h₂ : x₂.Numeric) (h₁₂ : IH24 x₁ x₂ y) (h₂₁ : IH24 x₂ x₁ y) (he : x₁ ≈ x₂) : x₁ * y ≤ x₂ * y := by have he' := neg_equiv_neg_iff.2 he apply PGame.le_of_forall_lt <;> simp_rw [lt_iff_game_lt] · rw [leftMoves_mul_iff (_ > ·)] refine ⟨mulOption_lt_mul_of_equiv h₁ h₁₂ he, ?_⟩ rw [← quot_neg_mul_neg] exact mulOption_lt_mul_of_equiv h₁.neg (ih24_neg <| (ih24_neg h₂₁).1).2 he' · rw [rightMoves_mul_iff] constructor <;> intros <;> rw [lt_neg] · rw [← quot_mul_neg] apply mulOption_lt_mul_of_equiv h₂ (ih24_neg h₂₁).2 (symm he) · rw [← quot_neg_mul] apply mulOption_lt_mul_of_equiv h₂.neg (ih24_neg h₁₂).1 (symm he') /-- The statement that all left options of `x * y` of the first kind are less than itself. -/ def MulOptionsLTMul (x y : PGame) : Prop := ∀ ⦃i j⦄, ⟦mulOption x y i j⟧ < (⟦x * y⟧ : Game) /-- That the left options of `x * y` are less than itself and the right options are greater, which is part of the condition that `x * y` is numeric, is equivalent to the conjunction of various `MulOptionsLTMul` statements for `x`, `y` and their negations. We only show the forward direction. -/ lemma mulOptionsLTMul_of_numeric (hn : (x * y).Numeric) : (MulOptionsLTMul x y ∧ MulOptionsLTMul (-x) (-y)) ∧ (MulOptionsLTMul x (-y) ∧ MulOptionsLTMul (-x) y) := by constructor · have h := hn.moveLeft_lt simp_rw [lt_iff_game_lt] at h convert (leftMoves_mul_iff <| GT.gt _).1 h rw [← quot_neg_mul_neg] rfl · have h := hn.lt_moveRight simp_rw [lt_iff_game_lt, rightMoves_mul_iff] at h refine h.imp ?_ ?_ <;> refine forall₂_imp fun a b ↦ ?_ all_goals rw [lt_neg] first | rw [quot_mul_neg] | rw [quot_neg_mul] exact id /-- A condition just enough to deduce P3, which will always be used with `x'` being a left option of `x₂`. When `y₁` is a left option of `y₂`, it can be deduced from induction hypotheses `IH24 x₁ x₂ y₂`, `IH4 x₁ x₂ y₂`, and `(x₂ * y₂).Numeric` (`ih3_of_ih`); when `y₁` is not necessarily an option of `y₂`, it follows from the induction hypothesis for P3 (with `x₂` replaced by a left option `x'`) after the `main` theorem (P124) is established, and is used to prove P3 in full (`P3_of_lt_of_lt`). -/ def IH3 (x₁ x' x₂ y₁ y₂ : PGame) : Prop := P2 x₁ x' y₁ ∧ P2 x₁ x' y₂ ∧ P3 x' x₂ y₁ y₂ ∧ (x₁ < x' → P3 x₁ x' y₁ y₂) lemma ih3_of_ih (h24 : IH24 x₁ x₂ y) (h4 : IH4 x₁ x₂ y) (hl : MulOptionsLTMul x₂ y) (i j) : IH3 x₁ (x₂.moveLeft i) x₂ (y.moveLeft j) y := have ml := @IsOption.moveLeft have h24 := (@h24 _).2.1 (ml i) ⟨(h4 <| ml j).2 (ml i), h24.1, mulOption_lt_mul_iff_P3.1 (@hl i j), fun l ↦ (h24.2 l).1 _⟩ lemma P3_of_le_left {y₁ y₂} (i) (h : IH3 x₁ (x₂.moveLeft i) x₂ y₁ y₂) (hl : x₁ ≤ x₂.moveLeft i) : P3 x₁ x₂ y₁ y₂ := by obtain (hl | he) := lt_or_equiv_of_le hl · exact (h.2.2.2 hl).trans h.2.2.1 · rw [P3, h.1 he, h.2.1 he] exact h.2.2.1 /-- P3 follows from `IH3` (so P4 (with `y₁` a left option of `y₂`) follows from the induction hypothesis). -/ theorem P3_of_lt {y₁ y₂} (h : ∀ i, IH3 x₁ (x₂.moveLeft i) x₂ y₁ y₂) (hs : ∀ i, IH3 (-x₂) ((-x₁).moveLeft i) (-x₁) y₁ y₂) (hl : x₁ < x₂) : P3 x₁ x₂ y₁ y₂ := by obtain (⟨i, hi⟩ | ⟨i, hi⟩) := lf_iff_exists_le.1 (lf_of_lt hl) · exact P3_of_le_left i (h i) hi · apply P3_neg.2 <| P3_of_le_left _ (hs (toLeftMovesNeg i)) _ simpa /-- The main chunk of Theorem 8 in [Conway2001] / Theorem 3.8 in [SchleicherStoll]. -/ theorem main (a : Args) : a.Numeric → P124 a := by apply argsRel_wf.induction a intro a ih ha replace ih : ∀ a', ArgsRel a' a → P124 a' := fun a' hr ↦ ih a' hr (hr.numeric_closed ha) cases a with /- P1 -/ | P1 x y => rw [Args.numeric_P1] at ha exact P1_of_ih ih ha.1 ha.2 | P24 x₁ x₂ y => have h₁₂ := ih₁₂ ih have h₂₁ := ih₂₁ ih have h4 := ih4 ih obtain ⟨h₁₂x, h₁₂y⟩ := ih24_neg h₁₂ obtain ⟨h4x, h4y⟩ := ih4_neg h4 refine ⟨fun he ↦ Quotient.sound ?_, fun hl ↦ ?_⟩ · /- P2 -/ rw [Args.numeric_P24] at ha exact ⟨mul_right_le_of_equiv ha.1 ha.2.1 h₁₂ h₂₁ he, mul_right_le_of_equiv ha.2.1 ha.1 h₂₁ h₁₂ (symm he)⟩ · /- P4 -/ obtain ⟨hn₁, hn₂⟩ := numeric_of_ih ih obtain ⟨⟨h₁, -⟩, h₂, -⟩ := mulOptionsLTMul_of_numeric hn₂ obtain ⟨⟨-, h₃⟩, -, h₄⟩ := mulOptionsLTMul_of_numeric hn₁ constructor <;> intro <;> refine P3_of_lt ?_ ?_ hl <;> intro <;> apply ih3_of_ih any_goals assumption exacts [(ih24_neg h₁₂y).1, (ih4_neg h4y).1] end Surreal.Multiplication namespace SetTheory.PGame open Surreal.Multiplication variable {x x₁ x₂ y y₁ y₂ : PGame.{u}} theorem Numeric.mul (hx : x.Numeric) (hy : y.Numeric) : Numeric (x * y) := main _ <| Args.numeric_P1.mpr ⟨hx, hy⟩ theorem P24 (hx₁ : x₁.Numeric) (hx₂ : x₂.Numeric) (hy : y.Numeric) : P24 x₁ x₂ y := main _ <| Args.numeric_P24.mpr ⟨hx₁, hx₂, hy⟩ theorem Equiv.mul_congr_left (hx₁ : x₁.Numeric) (hx₂ : x₂.Numeric) (hy : y.Numeric) (he : x₁ ≈ x₂) : x₁ * y ≈ x₂ * y := equiv_iff_game_eq.2 <| (P24 hx₁ hx₂ hy).1 he theorem Equiv.mul_congr_right (hx : x.Numeric) (hy₁ : y₁.Numeric) (hy₂ : y₂.Numeric) (he : y₁ ≈ y₂) : x * y₁ ≈ x * y₂ := .trans (mul_comm_equiv _ _) <| .trans (mul_congr_left hy₁ hy₂ hx he) (mul_comm_equiv _ _) theorem Equiv.mul_congr (hx₁ : x₁.Numeric) (hx₂ : x₂.Numeric) (hy₁ : y₁.Numeric) (hy₂ : y₂.Numeric) (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ * y₁ ≈ x₂ * y₂ := .trans (mul_congr_left hx₁ hx₂ hy₁ hx) (mul_congr_right hx₂ hy₁ hy₂ hy) open Prod.GameAdd /-- One additional inductive argument that supplies the last missing part of Theorem 8. -/ theorem P3_of_lt_of_lt (hx₁ : x₁.Numeric) (hx₂ : x₂.Numeric) (hy₁ : y₁.Numeric) (hy₂ : y₂.Numeric) (hx : x₁ < x₂) (hy : y₁ < y₂) : P3 x₁ x₂ y₁ y₂ := by revert x₁ x₂ rw [← Prod.forall'] refine (wf_isOption.prod_gameAdd wf_isOption).fix ?_ rintro ⟨x₁, x₂⟩ ih hx₁ hx₂ hx refine P3_of_lt ?_ ?_ hx <;> intro i · have hi := hx₂.moveLeft i exact ⟨(P24 hx₁ hi hy₁).1, (P24 hx₁ hi hy₂).1, P3_comm.2 <| ((P24 hy₁ hy₂ hx₂).2 hy).1 _, ih _ (snd <| IsOption.moveLeft i) hx₁ hi⟩ · have hi := hx₁.neg.moveLeft i exact ⟨(P24 hx₂.neg hi hy₁).1, (P24 hx₂.neg hi hy₂).1, P3_comm.2 <| ((P24 hy₁ hy₂ hx₁).2 hy).2 _, by rw [moveLeft_neg, ← P3_neg, neg_lt_neg_iff] exact ih _ (fst <| IsOption.moveRight _) (hx₁.moveRight _) hx₂⟩ theorem Numeric.mul_pos (hx₁ : x₁.Numeric) (hx₂ : x₂.Numeric) (hp₁ : 0 < x₁) (hp₂ : 0 < x₂) : 0 < x₁ * x₂ := by rw [lt_iff_game_lt] have := P3_of_lt_of_lt numeric_zero hx₁ numeric_zero hx₂ hp₁ hp₂ simp_rw [P3, quot_zero_mul, quot_mul_zero, add_lt_add_iff_left] at this exact this end SetTheory.PGame namespace Surreal open SetTheory.PGame.Equiv instance : CommRing Surreal where __ := Surreal.addCommGroup mul := Surreal.lift₂ (fun x y ox oy ↦ ⟦⟨x * y, ox.mul oy⟩⟧) (fun ox₁ oy₁ ox₂ oy₂ hx hy ↦ Quotient.sound <| mul_congr ox₁ ox₂ oy₁ oy₂ hx hy) mul_assoc := by rintro ⟨_⟩ ⟨_⟩ ⟨_⟩; exact Quotient.sound (mul_assoc_equiv _ _ _) one := mk 1 numeric_one one_mul := by rintro ⟨_⟩; exact Quotient.sound (one_mul_equiv _) mul_one := by rintro ⟨_⟩; exact Quotient.sound (mul_one_equiv _) left_distrib := by rintro ⟨_⟩ ⟨_⟩ ⟨_⟩; exact Quotient.sound (left_distrib_equiv _ _ _) right_distrib := by rintro ⟨_⟩ ⟨_⟩ ⟨_⟩; exact Quotient.sound (right_distrib_equiv _ _ _) mul_comm := by rintro ⟨_⟩ ⟨_⟩; exact Quotient.sound (mul_comm_equiv _ _) zero_mul := by rintro ⟨_⟩; exact Quotient.sound (zero_mul_equiv _) mul_zero := by rintro ⟨_⟩; exact Quotient.sound (mul_zero_equiv _) instance : ZeroLEOneClass Surreal where zero_le_one := PGame.zero_lt_one.le instance : Nontrivial Surreal where exists_pair_ne := ⟨0, 1, ne_of_lt PGame.zero_lt_one⟩ instance : IsStrictOrderedRing Surreal := .of_mul_pos <| by rintro ⟨x⟩ ⟨y⟩; exact x.2.mul_pos y.2 end Surreal
.lake/packages/mathlib/Mathlib/SetTheory/Surreal/Basic.lean
import Mathlib.Algebra.Order.Hom.Monoid import Mathlib.SetTheory.Game.Ordinal import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Surreal.Basic` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Surreal numbers The basic theory of surreal numbers, built on top of the theory of combinatorial (pre-)games. A pregame is `Numeric` if all the Left options are strictly smaller than all the Right options, and all those options are themselves numeric. In terms of combinatorial games, the numeric games have "frozen"; you can only make your position worse by playing, and Left is some definite "number" of moves ahead (or behind) Right. A surreal number is an equivalence class of numeric pregames. In fact, the surreals form a complete ordered field, containing a copy of the reals (and much else besides!) but we do not yet have a complete development. ## Order properties Surreal numbers inherit the relations `≤` and `<` from games (`Surreal.instLE` and `Surreal.instLT`), and these relations satisfy the axioms of a partial order. ## Algebraic operations In this file, we show that the surreals form a linear ordered commutative group. In `Mathlib/SetTheory/Surreal/Multiplication.lean`, we define multiplication and show that the surreals form a linear ordered commutative ring. One can also map all the ordinals into the surreals! ## TODO - Define the field structure on the surreals. ## References * [Conway, *On numbers and games*][Conway2001] * [Schleicher, Stoll, *An introduction to Conway's games and numbers*][SchleicherStoll] -/ universe u namespace SetTheory open scoped PGame namespace PGame /-- A pre-game is numeric if everything in the L set is less than everything in the R set, and all the elements of L and R are also numeric. -/ def Numeric : PGame → Prop | ⟨_, _, L, R⟩ => (∀ i j, L i < R j) ∧ (∀ i, Numeric (L i)) ∧ ∀ j, Numeric (R j) theorem numeric_def {x : PGame} : Numeric x ↔ (∀ i j, x.moveLeft i < x.moveRight j) ∧ (∀ i, Numeric (x.moveLeft i)) ∧ ∀ j, Numeric (x.moveRight j) := by cases x; rfl namespace Numeric theorem mk {x : PGame} (h₁ : ∀ i j, x.moveLeft i < x.moveRight j) (h₂ : ∀ i, Numeric (x.moveLeft i)) (h₃ : ∀ j, Numeric (x.moveRight j)) : Numeric x := numeric_def.2 ⟨h₁, h₂, h₃⟩ theorem left_lt_right {x : PGame} (o : Numeric x) (i : x.LeftMoves) (j : x.RightMoves) : x.moveLeft i < x.moveRight j := by cases x; exact o.1 i j theorem moveLeft {x : PGame} (o : Numeric x) (i : x.LeftMoves) : Numeric (x.moveLeft i) := by cases x; exact o.2.1 i theorem moveRight {x : PGame} (o : Numeric x) (j : x.RightMoves) : Numeric (x.moveRight j) := by cases x; exact o.2.2 j lemma isOption {x' x} (h : IsOption x' x) (hx : Numeric x) : Numeric x' := by cases h · apply hx.moveLeft · apply hx.moveRight end Numeric @[elab_as_elim] theorem numeric_rec {C : PGame → Prop} (H : ∀ (l r) (L : l → PGame) (R : r → PGame), (∀ i j, L i < R j) → (∀ i, Numeric (L i)) → (∀ i, Numeric (R i)) → (∀ i, C (L i)) → (∀ i, C (R i)) → C ⟨l, r, L, R⟩) : ∀ x, Numeric x → C x | ⟨_, _, _, _⟩, ⟨h, hl, hr⟩ => H _ _ _ _ h hl hr (fun i => numeric_rec H _ (hl i)) fun i => numeric_rec H _ (hr i) theorem Relabelling.numeric_imp {x y : PGame} (r : x ≡r y) (ox : Numeric x) : Numeric y := by induction x using PGame.moveRecOn generalizing y with | _ x IHl IHr apply Numeric.mk (fun i j => ?_) (fun i => ?_) fun j => ?_ · rw [← lt_congr (r.moveLeftSymm i).equiv (r.moveRightSymm j).equiv] apply ox.left_lt_right · exact IHl _ (r.moveLeftSymm i) (ox.moveLeft _) · exact IHr _ (r.moveRightSymm j) (ox.moveRight _) /-- Relabellings preserve being numeric. -/ theorem Relabelling.numeric_congr {x y : PGame} (r : x ≡r y) : Numeric x ↔ Numeric y := ⟨r.numeric_imp, r.symm.numeric_imp⟩ theorem lf_asymm {x y : PGame} (ox : Numeric x) (oy : Numeric y) : x ⧏ y → ¬y ⧏ x := by refine numeric_rec (C := fun x => ∀ z (_oz : Numeric z), x ⧏ z → ¬z ⧏ x) (fun xl xr xL xR hx _oxl _oxr IHxl IHxr => ?_) x ox y oy refine numeric_rec fun yl yr yL yR hy oyl oyr _IHyl _IHyr => ?_ rw [mk_lf_mk, mk_lf_mk]; rintro (⟨i, h₁⟩ | ⟨j, h₁⟩) (⟨i, h₂⟩ | ⟨j, h₂⟩) · exact IHxl _ _ (oyl _) (h₁.moveLeft_lf _) (h₂.moveLeft_lf _) · exact (le_trans h₂ h₁).not_gf (lf_of_lt (hy _ _)) · exact (le_trans h₁ h₂).not_gf (lf_of_lt (hx _ _)) · exact IHxr _ _ (oyr _) (h₁.lf_moveRight _) (h₂.lf_moveRight _) theorem le_of_lf {x y : PGame} (h : x ⧏ y) (ox : Numeric x) (oy : Numeric y) : x ≤ y := not_lf.1 (lf_asymm ox oy h) alias LF.le := le_of_lf theorem lt_of_lf {x y : PGame} (h : x ⧏ y) (ox : Numeric x) (oy : Numeric y) : x < y := (lt_or_fuzzy_of_lf h).resolve_right (not_fuzzy_of_le (h.le ox oy)) alias LF.lt := lt_of_lf theorem lf_iff_lt {x y : PGame} (ox : Numeric x) (oy : Numeric y) : x ⧏ y ↔ x < y := ⟨fun h => h.lt ox oy, lf_of_lt⟩ /-- Definition of `x ≤ y` on numeric pre-games, in terms of `<` -/ theorem le_iff_forall_lt {x y : PGame} (ox : x.Numeric) (oy : y.Numeric) : x ≤ y ↔ (∀ i, x.moveLeft i < y) ∧ ∀ j, x < y.moveRight j := by refine le_iff_forall_lf.trans (and_congr ?_ ?_) <;> refine forall_congr' fun i => lf_iff_lt ?_ ?_ <;> apply_rules [Numeric.moveLeft, Numeric.moveRight] /-- Definition of `x < y` on numeric pre-games, in terms of `≤` -/ theorem lt_iff_exists_le {x y : PGame} (ox : x.Numeric) (oy : y.Numeric) : x < y ↔ (∃ i, x ≤ y.moveLeft i) ∨ ∃ j, x.moveRight j ≤ y := by rw [← lf_iff_lt ox oy, lf_iff_exists_le] theorem lt_of_exists_le {x y : PGame} (ox : x.Numeric) (oy : y.Numeric) : ((∃ i, x ≤ y.moveLeft i) ∨ ∃ j, x.moveRight j ≤ y) → x < y := (lt_iff_exists_le ox oy).2 /-- The definition of `x < y` on numeric pre-games, in terms of `<` two moves later. -/ theorem lt_def {x y : PGame} (ox : x.Numeric) (oy : y.Numeric) : x < y ↔ (∃ i, (∀ i', x.moveLeft i' < y.moveLeft i) ∧ ∀ j, x < (y.moveLeft i).moveRight j) ∨ ∃ j, (∀ i, (x.moveRight j).moveLeft i < y) ∧ ∀ j', x.moveRight j < y.moveRight j' := by rw [← lf_iff_lt ox oy, lf_def] refine or_congr ?_ ?_ <;> refine exists_congr fun x_1 => ?_ <;> refine and_congr ?_ ?_ <;> refine forall_congr' fun i => lf_iff_lt ?_ ?_ <;> apply_rules [Numeric.moveLeft, Numeric.moveRight] theorem not_fuzzy {x y : PGame} (ox : Numeric x) (oy : Numeric y) : ¬Fuzzy x y := fun h => not_lf.2 ((lf_of_fuzzy h).le ox oy) h.2 theorem lt_or_equiv_or_gt {x y : PGame} (ox : Numeric x) (oy : Numeric y) : x < y ∨ (x ≈ y) ∨ y < x := ((lf_or_equiv_or_gf x y).imp fun h => h.lt ox oy) <| Or.imp_right fun h => h.lt oy ox theorem numeric_of_isEmpty (x : PGame) [IsEmpty x.LeftMoves] [IsEmpty x.RightMoves] : Numeric x := Numeric.mk isEmptyElim isEmptyElim isEmptyElim theorem numeric_of_isEmpty_leftMoves (x : PGame) [IsEmpty x.LeftMoves] : (∀ j, Numeric (x.moveRight j)) → Numeric x := Numeric.mk isEmptyElim isEmptyElim theorem numeric_of_isEmpty_rightMoves (x : PGame) [IsEmpty x.RightMoves] (H : ∀ i, Numeric (x.moveLeft i)) : Numeric x := Numeric.mk (fun _ => isEmptyElim) H isEmptyElim theorem numeric_zero : Numeric 0 := numeric_of_isEmpty 0 theorem numeric_one : Numeric 1 := numeric_of_isEmpty_rightMoves 1 fun _ => numeric_zero theorem Numeric.neg : ∀ {x : PGame} (_ : Numeric x), Numeric (-x) | ⟨_, _, _, _⟩, o => ⟨fun j i => neg_lt_neg_iff.2 (o.1 i j), fun j => (o.2.2 j).neg, fun i => (o.2.1 i).neg⟩ /-- Inserting a smaller numeric left option into a numeric game results in a numeric game. -/ theorem insertLeft_numeric {x x' : PGame} (x_num : x.Numeric) (x'_num : x'.Numeric) (h : x' ≤ x) : (insertLeft x x').Numeric := by rw [le_iff_forall_lt x'_num x_num] at h unfold Numeric at x_num ⊢ rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertLeft, Sum.forall, forall_const, Sum.elim_inl, Sum.elim_inr] at x_num ⊢ constructor · simp only [x_num.1, implies_true, true_and] simp only [rightMoves_mk, moveRight_mk] at h exact h.2 · simp only [x_num, implies_true, x'_num, and_self] /-- Inserting a larger numeric right option into a numeric game results in a numeric game. -/ theorem insertRight_numeric {x x' : PGame} (x_num : x.Numeric) (x'_num : x'.Numeric) (h : x ≤ x') : (insertRight x x').Numeric := by rw [← neg_neg (x.insertRight x'), ← neg_insertLeft_neg] apply Numeric.neg exact insertLeft_numeric (Numeric.neg x_num) (Numeric.neg x'_num) (neg_le_neg_iff.mpr h) namespace Numeric theorem moveLeft_lt {x : PGame} (o : Numeric x) (i) : x.moveLeft i < x := (moveLeft_lf i).lt (o.moveLeft i) o theorem moveLeft_le {x : PGame} (o : Numeric x) (i) : x.moveLeft i ≤ x := (o.moveLeft_lt i).le theorem lt_moveRight {x : PGame} (o : Numeric x) (j) : x < x.moveRight j := (lf_moveRight j).lt o (o.moveRight j) theorem le_moveRight {x : PGame} (o : Numeric x) (j) : x ≤ x.moveRight j := (o.lt_moveRight j).le theorem add : ∀ {x y : PGame} (_ : Numeric x) (_ : Numeric y), Numeric (x + y) | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩, ox, oy => ⟨by rintro (ix | iy) (jx | jy) · exact add_lt_add_right (ox.1 ix jx) _ · exact (add_lf_add_of_lf_of_le (lf_mk _ _ ix) (oy.le_moveRight jy)).lt ((ox.moveLeft ix).add oy) (ox.add (oy.moveRight jy)) · exact (add_lf_add_of_lf_of_le (mk_lf _ _ jx) (oy.moveLeft_le iy)).lt (ox.add (oy.moveLeft iy)) ((ox.moveRight jx).add oy) · exact add_lt_add_left (oy.1 iy jy) ⟨xl, xr, xL, xR⟩, by constructor · rintro (ix | iy) · exact (ox.moveLeft ix).add oy · exact ox.add (oy.moveLeft iy) · rintro (jx | jy) · apply (ox.moveRight jx).add oy · apply ox.add (oy.moveRight jy)⟩ termination_by x y => (x, y) -- Porting note: Added `termination_by` theorem sub {x y : PGame} (ox : Numeric x) (oy : Numeric y) : Numeric (x - y) := ox.add oy.neg end Numeric /-- Pre-games defined by natural numbers are numeric. -/ theorem numeric_nat : ∀ n : ℕ, Numeric n | 0 => numeric_zero | n + 1 => (numeric_nat n).add numeric_one /-- Ordinal games are numeric. -/ theorem numeric_toPGame (o : Ordinal) : o.toPGame.Numeric := by induction o using Ordinal.induction with | _ o IH apply numeric_of_isEmpty_rightMoves simpa using fun i => IH _ (Ordinal.toLeftMovesToPGame_symm_lt i) end PGame end SetTheory open SetTheory PGame /-- The type of surreal numbers. These are the numeric pre-games quotiented by the equivalence relation `x ≈ y ↔ x ≤ y ∧ y ≤ x`. In the quotient, the order becomes a total order. -/ def Surreal := Quotient (inferInstanceAs <| Setoid (Subtype Numeric)) namespace Surreal /-- Construct a surreal number from a numeric pre-game. -/ def mk (x : PGame) (h : x.Numeric) : Surreal := ⟦⟨x, h⟩⟧ instance : Zero Surreal := ⟨mk 0 numeric_zero⟩ instance : One Surreal := ⟨mk 1 numeric_one⟩ instance : Inhabited Surreal := ⟨0⟩ lemma mk_eq_mk {x y : PGame.{u}} {hx hy} : mk x hx = mk y hy ↔ x ≈ y := Quotient.eq lemma mk_eq_zero {x : PGame.{u}} {hx} : mk x hx = 0 ↔ x ≈ 0 := Quotient.eq /-- Lift an equivalence-respecting function on pre-games to surreals. -/ def lift {α} (f : ∀ x, Numeric x → α) (H : ∀ {x y} (hx : Numeric x) (hy : Numeric y), x.Equiv y → f x hx = f y hy) : Surreal → α := Quotient.lift (fun x : { x // Numeric x } => f x.1 x.2) fun x y => H x.2 y.2 /-- Lift a binary equivalence-respecting function on pre-games to surreals. -/ def lift₂ {α} (f : ∀ x y, Numeric x → Numeric y → α) (H : ∀ {x₁ y₁ x₂ y₂} (ox₁ : Numeric x₁) (oy₁ : Numeric y₁) (ox₂ : Numeric x₂) (oy₂ : Numeric y₂), x₁.Equiv x₂ → y₁.Equiv y₂ → f x₁ y₁ ox₁ oy₁ = f x₂ y₂ ox₂ oy₂) : Surreal → Surreal → α := lift (fun x ox => lift (fun y oy => f x y ox oy) fun _ _ => H _ _ _ _ equiv_rfl) fun _ _ h => funext <| Quotient.ind fun _ => H _ _ _ _ h equiv_rfl instance instLE : LE Surreal := ⟨lift₂ (fun x y _ _ => x ≤ y) fun _ _ _ _ hx hy => propext (le_congr hx hy)⟩ @[simp] lemma mk_le_mk {x y : PGame.{u}} {hx hy} : mk x hx ≤ mk y hy ↔ x ≤ y := Iff.rfl lemma zero_le_mk {x : PGame.{u}} {hx} : 0 ≤ mk x hx ↔ 0 ≤ x := Iff.rfl instance instLT : LT Surreal := ⟨lift₂ (fun x y _ _ => x < y) fun _ _ _ _ hx hy => propext (lt_congr hx hy)⟩ lemma mk_lt_mk {x y : PGame.{u}} {hx hy} : mk x hx < mk y hy ↔ x < y := Iff.rfl lemma zero_lt_mk {x : PGame.{u}} {hx} : 0 < mk x hx ↔ 0 < x := Iff.rfl /-- Same as `moveLeft_lt`, but for `Surreal` instead of `PGame` -/ theorem mk_moveLeft_lt_mk {x : PGame} (o : Numeric x) (i) : Surreal.mk (x.moveLeft i) (Numeric.moveLeft o i) < Surreal.mk x o := Numeric.moveLeft_lt o i /-- Same as `lt_moveRight`, but for `Surreal` instead of `PGame` -/ theorem mk_lt_mk_moveRight {x : PGame} (o : Numeric x) (j) : Surreal.mk x o < Surreal.mk (x.moveRight j) (Numeric.moveRight o j) := Numeric.lt_moveRight o j /-- Addition on surreals is inherited from pre-game addition: the sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ instance : Add Surreal := ⟨Surreal.lift₂ (fun (x y : PGame) ox oy => ⟦⟨x + y, ox.add oy⟩⟧) fun _ _ _ _ hx hy => Quotient.sound (add_congr hx hy)⟩ /-- Negation for surreal numbers is inherited from pre-game negation: the negation of `{L | R}` is `{-R | -L}`. -/ instance : Neg Surreal := ⟨Surreal.lift (fun x ox => ⟦⟨-x, ox.neg⟩⟧) fun _ _ a => Quotient.sound (neg_equiv_neg_iff.2 a)⟩ instance addCommGroup : AddCommGroup Surreal where add_assoc := by rintro ⟨_⟩ ⟨_⟩ ⟨_⟩; exact Quotient.sound add_assoc_equiv zero_add := by rintro ⟨a⟩; exact Quotient.sound (zero_add_equiv a) add_zero := by rintro ⟨a⟩; exact Quotient.sound (add_zero_equiv a) neg_add_cancel := by rintro ⟨a⟩; exact Quotient.sound (neg_add_cancel_equiv a) add_comm := by rintro ⟨_⟩ ⟨_⟩; exact Quotient.sound add_comm_equiv nsmul := nsmulRec zsmul := zsmulRec instance partialOrder : PartialOrder Surreal where le_refl := by rintro ⟨_⟩; apply @le_rfl PGame le_trans := by rintro ⟨_⟩ ⟨_⟩ ⟨_⟩; apply @le_trans PGame lt_iff_le_not_ge := by rintro ⟨_, ox⟩ ⟨_, oy⟩; apply @lt_iff_le_not_ge PGame le_antisymm := by rintro ⟨_⟩ ⟨_⟩ h₁ h₂; exact Quotient.sound ⟨h₁, h₂⟩ instance isOrderedAddMonoid : IsOrderedAddMonoid Surreal where add_le_add_left := by rintro ⟨_⟩ ⟨_⟩ hx ⟨_⟩; exact @add_le_add_left PGame _ _ _ _ _ hx _ lemma mk_add {x y : PGame} (hx : x.Numeric) (hy : y.Numeric) : Surreal.mk (x + y) (hx.add hy) = Surreal.mk x hx + Surreal.mk y hy := by rfl lemma mk_sub {x y : PGame} (hx : x.Numeric) (hy : y.Numeric) : Surreal.mk (x - y) (hx.sub hy) = Surreal.mk x hx - Surreal.mk y hy := by rfl lemma zero_def : 0 = mk 0 numeric_zero := by rfl noncomputable instance : LinearOrder Surreal := { Surreal.partialOrder with le_total := by rintro ⟨⟨x, ox⟩⟩ ⟨⟨y, oy⟩⟩ exact or_iff_not_imp_left.2 fun h => (PGame.not_le.1 h).le oy ox toDecidableLE := Classical.decRel _ } instance : AddMonoidWithOne Surreal := AddMonoidWithOne.unary /-- Casts a `Surreal` number into a `Game`. -/ def toGame : Surreal →+o Game where toFun := lift (fun x _ => ⟦x⟧) fun _ _ => Quot.sound map_zero' := rfl map_add' := by rintro ⟨_, _⟩ ⟨_, _⟩; rfl monotone' := by rintro ⟨_, _⟩ ⟨_, _⟩; exact id @[simp] theorem zero_toGame : toGame 0 = 0 := rfl @[simp] theorem one_toGame : toGame 1 = 1 := rfl @[simp] theorem nat_toGame : ∀ n : ℕ, toGame n = n := map_natCast' _ one_toGame /-- A small family of surreals is bounded above. -/ lemma bddAbove_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → Surreal.{u}) : BddAbove (Set.range f) := by induction f using Quotient.induction_on_pi with | _ f let g : ι → PGame.{u} := Subtype.val ∘ f have hg (i) : (g i).Numeric := Subtype.prop _ conv in (⟦f _⟧) => change mk (g i) (hg i) clear_value g clear f let x : PGame.{u} := ⟨Σ i, (g <| (equivShrink.{u} ι).symm i).LeftMoves, PEmpty, fun x ↦ moveLeft _ x.2, PEmpty.elim⟩ refine ⟨mk x (.mk (by simp [x]) (fun _ ↦ (hg _).moveLeft _) (by simp [x])), Set.forall_mem_range.2 fun i ↦ ?_⟩ rw [mk_le_mk, ← (equivShrink ι).symm_apply_apply i, le_iff_forall_lf] simpa [x] using fun j ↦ @moveLeft_lf x ⟨equivShrink ι i, j⟩ /-- A small set of surreals is bounded above. -/ lemma bddAbove_of_small (s : Set Surreal.{u}) [Small.{u} s] : BddAbove s := by simpa using bddAbove_range_of_small (Subtype.val : s → Surreal.{u}) /-- A small family of surreals is bounded below. -/ lemma bddBelow_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → Surreal.{u}) : BddBelow (Set.range f) := by induction f using Quotient.induction_on_pi with | _ f let g : ι → PGame.{u} := Subtype.val ∘ f have hg (i) : (g i).Numeric := Subtype.prop _ conv in (⟦f _⟧) => change mk (g i) (hg i) clear_value g clear f let x : PGame.{u} := ⟨PEmpty, Σ i, (g <| (equivShrink.{u} ι).symm i).RightMoves, PEmpty.elim, fun x ↦ moveRight _ x.2⟩ refine ⟨mk x (.mk (by simp [x]) (by simp [x]) (fun _ ↦ (hg _).moveRight _) ), Set.forall_mem_range.2 fun i ↦ ?_⟩ rw [mk_le_mk, ← (equivShrink ι).symm_apply_apply i, le_iff_forall_lf] simpa [x] using fun j ↦ @lf_moveRight x ⟨equivShrink ι i, j⟩ /-- A small set of surreals is bounded below. -/ lemma bddBelow_of_small (s : Set Surreal.{u}) [Small.{u} s] : BddBelow s := by simpa using bddBelow_range_of_small (Subtype.val : s → Surreal.{u}) end Surreal open Surreal namespace Ordinal /-- Converts an ordinal into the corresponding surreal. -/ noncomputable def toSurreal : Ordinal ↪o Surreal where toFun o := mk _ (numeric_toPGame o) inj' a b h := toPGame_equiv_iff.1 (by apply Quotient.exact h) -- Porting note: Added `by apply` map_rel_iff' := @toPGame_le_iff end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/FixedPointApproximants.lean
import Mathlib.SetTheory.Ordinal.Arithmetic /-! # Ordinal Approximants for the Fixed points on complete lattices This file sets up the ordinal-indexed approximation theory of fixed points of a monotone function in a complete lattice [Cousot1979]. The proof follows loosely the one from [Echenique2005]. However, the proof given here is not constructive as we use the non-constructive axiomatization of ordinals from mathlib. It still allows an approximation scheme indexed over the ordinals. ## Main definitions * `OrdinalApprox.lfpApprox`: The ordinal-indexed approximation of the least fixed point greater or equal than an initial value of a bundled monotone function. * `OrdinalApprox.gfpApprox`: The ordinal-indexed approximation of the greatest fixed point less or equal than an initial value of a bundled monotone function. ## Main theorems * `OrdinalApprox.lfp_mem_range_lfpApprox`: The ordinal-indexed approximation of the least fixed point eventually reaches the least fixed point * `OrdinalApprox.gfp_mem_range_gfpApprox`: The ordinal-indexed approximation of the greatest fixed point eventually reaches the greatest fixed point ## References * [F. Echenique, *A short and constructive proof of Tarski’s fixed-point theorem*][Echenique2005] * [P. Cousot & R. Cousot, *Constructive Versions of Tarski's Fixed Point Theorems*][Cousot1979] ## Tags fixed point, complete lattice, monotone function, ordinals, approximation -/ namespace Cardinal universe u variable {α : Type u} variable (g : Ordinal → α) open Cardinal Ordinal SuccOrder Function Set theorem not_injective_limitation_set : ¬ InjOn g (Iio (ord <| succ #α)) := by intro h_inj have h := lift_mk_le_lift_mk_of_injective <| injOn_iff_injective.1 h_inj have mk_initialSeg_subtype : #(Iio (ord <| succ #α)) = lift.{u + 1} (succ #α) := by simpa only [coe_setOf, card_typein, card_ord] using mk_Iio_ordinal (ord <| succ #α) rw [mk_initialSeg_subtype, lift_lift, lift_le] at h exact not_le_of_gt (Order.lt_succ #α) h end Cardinal namespace OrdinalApprox universe u variable {α : Type u} variable [CompleteLattice α] (f : α →o α) (x : α) open Function fixedPoints Cardinal Order OrderHom set_option linter.unusedVariables false in /-- The ordinal-indexed sequence approximating the least fixed point greater than an initial value `x`. It is defined in such a way that we have `lfpApprox 0 x = x` and `lfpApprox a x = ⨆ b < a, f (lfpApprox b x)`. -/ def lfpApprox (a : Ordinal.{u}) : α := sSup ({ f (lfpApprox b) | (b : Ordinal) (h : b < a) } ∪ {x}) termination_by a decreasing_by exact h theorem lfpApprox_monotone : Monotone (lfpApprox f x) := by intro a b h rw [lfpApprox, lfpApprox] gcongr sSup (?_ ∪ {x}) simp only [exists_prop, Set.setOf_subset_setOf, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂] intro a' h' use a' exact ⟨lt_of_lt_of_le h' h, rfl⟩ theorem le_lfpApprox {a : Ordinal} : x ≤ lfpApprox f x a := by rw [lfpApprox] apply le_sSup simp only [exists_prop, Set.union_singleton, Set.mem_insert_iff, Set.mem_setOf_eq, true_or] theorem lfpApprox_add_one (h : x ≤ f x) (a : Ordinal) : lfpApprox f x (a + 1) = f (lfpApprox f x a) := by apply le_antisymm · conv => left; rw [lfpApprox] apply sSup_le simp only [Ordinal.add_one_eq_succ, lt_succ_iff, exists_prop, Set.union_singleton, Set.mem_insert_iff, Set.mem_setOf_eq, forall_eq_or_imp, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂] apply And.intro · apply le_trans h apply Monotone.imp f.monotone exact le_lfpApprox f x · intro a' h apply f.2; apply lfpApprox_monotone; exact h · conv => right; rw [lfpApprox] apply le_sSup simp only [Ordinal.add_one_eq_succ, lt_succ_iff, exists_prop] rw [Set.mem_union] apply Or.inl simp only [Set.mem_setOf_eq] use a theorem lfpApprox_mono_left : Monotone (lfpApprox : (α →o α) → _) := by intro f g h x a induction a using Ordinal.induction with | h i ih => rw [lfpApprox, lfpApprox] apply sSup_le simp only [exists_prop, Set.union_singleton, Set.mem_insert_iff, Set.mem_setOf_eq, sSup_insert, forall_eq_or_imp, le_sup_left, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂, true_and] intro i' h_lt apply le_sup_of_le_right apply le_sSup_of_le · use i' · apply le_trans (h _) simp only [OrderHom.toFun_eq_coe] exact g.monotone (ih i' h_lt) theorem lfpApprox_mono_mid : Monotone (lfpApprox f) := by intro x₁ x₂ h a induction a using Ordinal.induction with | h i ih => rw [lfpApprox, lfpApprox] apply sSup_le simp only [exists_prop, Set.union_singleton, Set.mem_insert_iff, Set.mem_setOf_eq, sSup_insert, forall_eq_or_imp, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂] constructor · exact le_sup_of_le_left h · intro i' h_i' apply le_sup_of_le_right apply le_sSup_of_le · use i' · exact f.monotone (ih i' h_i') /-- The approximations of the least fixed point stabilize at a fixed point of `f` -/ theorem lfpApprox_eq_of_mem_fixedPoints {a b : Ordinal} (h_init : x ≤ f x) (h_ab : a ≤ b) (h : lfpApprox f x a ∈ fixedPoints f) : lfpApprox f x b = lfpApprox f x a := by rw [mem_fixedPoints_iff] at h induction b using Ordinal.induction with | h b IH => apply le_antisymm · conv => left; rw [lfpApprox] apply sSup_le simp only [exists_prop, Set.union_singleton, Set.mem_insert_iff, Set.mem_setOf_eq, forall_eq_or_imp, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂] apply And.intro (le_lfpApprox f x) intro a' ha'b by_cases! haa : a' < a · rw [← lfpApprox_add_one f x h_init] apply lfpApprox_monotone simp only [Ordinal.add_one_eq_succ, succ_le_iff] exact haa · rw [IH a' ha'b haa, h] · exact lfpApprox_monotone f x h_ab /-- There are distinct indices smaller than the successor of the domain's cardinality yielding the same value -/ theorem exists_lfpApprox_eq_lfpApprox : ∃ a < ord <| succ #α, ∃ b < ord <| succ #α, a ≠ b ∧ lfpApprox f x a = lfpApprox f x b := by have h_ninj := not_injective_limitation_set <| lfpApprox f x rw [Set.injOn_iff_injective, Function.not_injective_iff] at h_ninj let ⟨a, b, h_fab, h_nab⟩ := h_ninj use a.val; apply And.intro a.prop use b.val; apply And.intro b.prop apply And.intro · intro h_eq; rw [Subtype.coe_inj] at h_eq; exact h_nab h_eq · exact h_fab /-- If the sequence of ordinal-indexed approximations takes a value twice, then it actually stabilised at that value. -/ lemma lfpApprox_mem_fixedPoints_of_eq {a b c : Ordinal} (h_init : x ≤ f x) (h_ab : a < b) (h_ac : a ≤ c) (h_fab : lfpApprox f x a = lfpApprox f x b) : lfpApprox f x c ∈ fixedPoints f := by have lfpApprox_mem_fixedPoint : lfpApprox f x a ∈ fixedPoints f := by rw [mem_fixedPoints_iff, ← lfpApprox_add_one f x h_init] exact Monotone.eq_of_ge_of_le (lfpApprox_monotone f x) h_fab (SuccOrder.le_succ a) (SuccOrder.succ_le_of_lt h_ab) rw [lfpApprox_eq_of_mem_fixedPoints f x h_init] · exact lfpApprox_mem_fixedPoint · exact h_ac · exact lfpApprox_mem_fixedPoint /-- The approximation at the index of the successor of the domain's cardinality is a fixed point -/ theorem lfpApprox_ord_mem_fixedPoint (h_init : x ≤ f x) : lfpApprox f x (ord <| succ #α) ∈ fixedPoints f := by let ⟨a, h_a, b, h_b, h_nab, h_fab⟩ := exists_lfpApprox_eq_lfpApprox f x cases le_total a b with | inl h_ab => exact lfpApprox_mem_fixedPoints_of_eq f x h_init (h_nab.lt_of_le h_ab) (le_of_lt h_a) h_fab | inr h_ba => exact lfpApprox_mem_fixedPoints_of_eq f x h_init (h_nab.symm.lt_of_le h_ba) (le_of_lt h_b) (h_fab.symm) /-- Every value of the approximation is less or equal than every fixed point of `f` greater or equal than the initial value -/ theorem lfpApprox_le_of_mem_fixedPoints {a : α} (h_a : a ∈ fixedPoints f) (h_le_init : x ≤ a) (i : Ordinal) : lfpApprox f x i ≤ a := by induction i using Ordinal.induction with | h i IH => rw [lfpApprox] apply sSup_le simp only [exists_prop] intro y h_y simp only [Set.mem_union, Set.mem_setOf_eq, Set.mem_singleton_iff] at h_y cases h_y with | inl h_y => let ⟨j, h_j_lt, h_j⟩ := h_y rw [← h_j, ← h_a] exact f.monotone' (IH j h_j_lt) | inr h_y => rw [h_y] exact h_le_init /-- The approximation sequence converges at the successor of the domain's cardinality to the least fixed point if starting from `⊥` -/ theorem lfpApprox_ord_eq_lfp : lfpApprox f ⊥ (ord <| succ #α) = f.lfp := by apply le_antisymm · have h_lfp : ∃ y : fixedPoints f, f.lfp = y := by use ⊥; exact rfl let ⟨y, h_y⟩ := h_lfp; rw [h_y] exact lfpApprox_le_of_mem_fixedPoints f ⊥ y.2 bot_le (ord <| succ #α) · have h_fix : ∃ y : fixedPoints f, lfpApprox f ⊥ (ord <| succ #α) = y := by simpa only [Subtype.exists, mem_fixedPoints, exists_prop, exists_eq_right'] using lfpApprox_ord_mem_fixedPoint f ⊥ bot_le let ⟨x, h_x⟩ := h_fix; rw [h_x] exact lfp_le_fixed f x.prop /-- Some approximation of the least fixed point starting from `⊥` is the least fixed point. -/ theorem lfp_mem_range_lfpApprox : f.lfp ∈ Set.range (lfpApprox f ⊥) := by use ord <| succ #α exact lfpApprox_ord_eq_lfp f set_option linter.unusedVariables false in /-- The ordinal-indexed sequence approximating the greatest fixed point greater than an initial value `x`. It is defined in such a way that we have `gfpApprox 0 x = x` and `gfpApprox a x = ⨅ b < a, f (lfpApprox b x)`. -/ def gfpApprox (a : Ordinal.{u}) : α := sInf ({ f (gfpApprox b) | (b : Ordinal) (h : b < a) } ∪ {x}) termination_by a decreasing_by exact h -- By unsealing these recursive definitions we can relate them -- by definitional equality unseal gfpApprox lfpApprox theorem gfpApprox_antitone : Antitone (gfpApprox f x) := lfpApprox_monotone f.dual x theorem gfpApprox_le {a : Ordinal} : gfpApprox f x a ≤ x := le_lfpApprox f.dual x theorem gfpApprox_add_one (h : f x ≤ x) (a : Ordinal) : gfpApprox f x (a + 1) = f (gfpApprox f x a) := lfpApprox_add_one f.dual x h a theorem gfpApprox_mono_left : Monotone (gfpApprox : (α →o α) → _) := by intro f g h have : g.dual ≤ f.dual := h exact lfpApprox_mono_left this theorem gfpApprox_mono_mid : Monotone (gfpApprox f) := fun _ _ h => lfpApprox_mono_mid f.dual h /-- The approximations of the greatest fixed point stabilize at a fixed point of `f` -/ theorem gfpApprox_eq_of_mem_fixedPoints {a b : Ordinal} (h_init : f x ≤ x) (h_ab : a ≤ b) (h : gfpApprox f x a ∈ fixedPoints f) : gfpApprox f x b = gfpApprox f x a := lfpApprox_eq_of_mem_fixedPoints f.dual x h_init h_ab h /-- There are distinct indices smaller than the successor of the domain's cardinality yielding the same value -/ theorem exists_gfpApprox_eq_gfpApprox : ∃ a < ord <| succ #α, ∃ b < ord <| succ #α, a ≠ b ∧ gfpApprox f x a = gfpApprox f x b := exists_lfpApprox_eq_lfpApprox f.dual x /-- The approximation at the index of the successor of the domain's cardinality is a fixed point -/ lemma gfpApprox_ord_mem_fixedPoint (h_init : f x ≤ x) : gfpApprox f x (ord <| succ #α) ∈ fixedPoints f := lfpApprox_ord_mem_fixedPoint f.dual x h_init /-- Every value of the approximation is greater or equal than every fixed point of `f` less or equal than the initial value -/ lemma le_gfpApprox_of_mem_fixedPoints {a : α} (h_a : a ∈ fixedPoints f) (h_le_init : a ≤ x) (i : Ordinal) : a ≤ gfpApprox f x i := lfpApprox_le_of_mem_fixedPoints f.dual x h_a h_le_init i /-- The approximation sequence converges at the successor of the domain's cardinality to the greatest fixed point if starting from `⊥` -/ theorem gfpApprox_ord_eq_gfp : gfpApprox f ⊤ (ord <| succ #α) = f.gfp := lfpApprox_ord_eq_lfp f.dual /-- Some approximation of the least fixed point starting from `⊤` is the greatest fixed point. -/ theorem gfp_mem_range_gfpApprox : f.gfp ∈ Set.range (gfpApprox f ⊤) := lfp_mem_range_lfpApprox f.dual end OrdinalApprox
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Family.lean
import Mathlib.SetTheory.Ordinal.Arithmetic /-! # Arithmetic on families of ordinals ## Main definitions and results * `sup`, `lsub`: the supremum / least strict upper bound of an indexed family of ordinals in `Type u`, as an ordinal in `Type u`. * `bsup`, `blsub`: the supremum / least strict upper bound of a set of ordinals indexed by ordinals less than a given ordinal `o`. Various other basic arithmetic results are given in `Principal.lean` instead. -/ assert_not_exists Field Module noncomputable section open Function Cardinal Set Equiv Order open scoped Ordinal universe u v w namespace Ordinal variable {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Families of ordinals There are two kinds of indexed families that naturally arise when dealing with ordinals: those indexed by some type in the appropriate universe, and those indexed by ordinals less than another. The following API allows one to convert from one kind of family to the other. In many cases, this makes it easy to prove claims about one kind of family via the corresponding claim on the other. -/ /-- Converts a family indexed by a `Type u` to one indexed by an `Ordinal.{u}` using a specified well-ordering. -/ def bfamilyOfFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → α) : ∀ a < type r, α := fun a ha => f (enum r ⟨a, ha⟩) /-- Converts a family indexed by a `Type u` to one indexed by an `Ordinal.{u}` using a well-ordering given by the axiom of choice. -/ def bfamilyOfFamily {ι : Type u} : (ι → α) → ∀ a < type (@WellOrderingRel ι), α := bfamilyOfFamily' WellOrderingRel /-- Converts a family indexed by an `Ordinal.{u}` to one indexed by a `Type u` using a specified well-ordering. -/ def familyOfBFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] {o} (ho : type r = o) (f : ∀ a < o, α) : ι → α := fun i => f (typein r i) (by rw [← ho] exact typein_lt_type r i) /-- Converts a family indexed by an `Ordinal.{u}` to one indexed by a `Type u` using a well-ordering given by the axiom of choice. -/ def familyOfBFamily (o : Ordinal) (f : ∀ a < o, α) : o.toType → α := familyOfBFamily' (· < ·) (type_toType o) f @[simp] theorem bfamilyOfFamily'_typein {ι} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → α) (i) : bfamilyOfFamily' r f (typein r i) (typein_lt_type r i) = f i := by simp only [bfamilyOfFamily', enum_typein] @[simp] theorem bfamilyOfFamily_typein {ι} (f : ι → α) (i) : bfamilyOfFamily f (typein _ i) (typein_lt_type _ i) = f i := bfamilyOfFamily'_typein _ f i theorem familyOfBFamily'_enum {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] {o} (ho : type r = o) (f : ∀ a < o, α) (i hi) : familyOfBFamily' r ho f (enum r ⟨i, by rwa [ho]⟩) = f i hi := by simp only [familyOfBFamily', typein_enum] theorem familyOfBFamily_enum (o : Ordinal) (f : ∀ a < o, α) (i hi) : familyOfBFamily o f (enum (α := o.toType) (· < ·) ⟨i, hi.trans_eq (type_toType _).symm⟩) = f i hi := familyOfBFamily'_enum _ (type_toType o) f _ _ /-- The range of a family indexed by ordinals. -/ def brange (o : Ordinal) (f : ∀ a < o, α) : Set α := { a | ∃ i hi, f i hi = a } theorem mem_brange {o : Ordinal} {f : ∀ a < o, α} {a} : a ∈ brange o f ↔ ∃ i hi, f i hi = a := Iff.rfl theorem mem_brange_self {o} (f : ∀ a < o, α) (i hi) : f i hi ∈ brange o f := ⟨i, hi, rfl⟩ @[simp] theorem range_familyOfBFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] {o} (ho : type r = o) (f : ∀ a < o, α) : range (familyOfBFamily' r ho f) = brange o f := by refine Set.ext fun a => ⟨?_, ?_⟩ · rintro ⟨b, rfl⟩ apply mem_brange_self · rintro ⟨i, hi, rfl⟩ exact ⟨_, familyOfBFamily'_enum _ _ _ _ _⟩ @[simp] theorem range_familyOfBFamily {o} (f : ∀ a < o, α) : range (familyOfBFamily o f) = brange o f := range_familyOfBFamily' _ _ f @[simp] theorem brange_bfamilyOfFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → α) : brange _ (bfamilyOfFamily' r f) = range f := by refine Set.ext fun a => ⟨?_, ?_⟩ · rintro ⟨i, hi, rfl⟩ apply mem_range_self · rintro ⟨b, rfl⟩ exact ⟨_, _, bfamilyOfFamily'_typein _ _ _⟩ @[simp] theorem brange_bfamilyOfFamily {ι : Type u} (f : ι → α) : brange _ (bfamilyOfFamily f) = range f := brange_bfamilyOfFamily' _ _ @[simp] theorem brange_const {o : Ordinal} (ho : o ≠ 0) {c : α} : (brange o fun _ _ => c) = {c} := by rw [← range_familyOfBFamily] exact @Set.range_const _ o.toType (toType_nonempty_iff_ne_zero.2 ho) c theorem comp_bfamilyOfFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → α) (g : α → β) : (fun i hi => g (bfamilyOfFamily' r f i hi)) = bfamilyOfFamily' r (g ∘ f) := rfl theorem comp_bfamilyOfFamily {ι : Type u} (f : ι → α) (g : α → β) : (fun i hi => g (bfamilyOfFamily f i hi)) = bfamilyOfFamily (g ∘ f) := rfl theorem comp_familyOfBFamily' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] {o} (ho : type r = o) (f : ∀ a < o, α) (g : α → β) : g ∘ familyOfBFamily' r ho f = familyOfBFamily' r ho fun i hi => g (f i hi) := rfl theorem comp_familyOfBFamily {o} (f : ∀ a < o, α) (g : α → β) : g ∘ familyOfBFamily o f = familyOfBFamily o fun i hi => g (f i hi) := rfl /-! ### Supremum of a family of ordinals -/ /-- The range of an indexed ordinal function, whose outputs live in a higher universe than the inputs, is always bounded above. See `Ordinal.lsub` for an explicit bound. -/ theorem bddAbove_range {ι : Type u} (f : ι → Ordinal.{max u v}) : BddAbove (Set.range f) := ⟨(iSup (succ ∘ card ∘ f)).ord, by rintro a ⟨i, rfl⟩ exact le_of_lt (Cardinal.lt_ord.2 ((lt_succ _).trans_le (le_ciSup (Cardinal.bddAbove_range _) _)))⟩ theorem bddAbove_of_small (s : Set Ordinal.{u}) [h : Small.{u} s] : BddAbove s := by obtain ⟨a, ha⟩ := bddAbove_range (fun x => ((@equivShrink s h).symm x).val) use a intro b hb simpa using ha (mem_range_self (equivShrink s ⟨b, hb⟩)) theorem bddAbove_iff_small {s : Set Ordinal.{u}} : BddAbove s ↔ Small.{u} s := ⟨fun ⟨a, h⟩ => small_subset <| show s ⊆ Iic a from fun _ hx => h hx, fun _ => bddAbove_of_small _⟩ theorem bddAbove_image {s : Set Ordinal.{u}} (hf : BddAbove s) (f : Ordinal.{u} → Ordinal.{max u v}) : BddAbove (f '' s) := by rw [bddAbove_iff_small] at hf ⊢ exact small_lift _ theorem bddAbove_range_comp {ι : Type u} {f : ι → Ordinal.{v}} (hf : BddAbove (range f)) (g : Ordinal.{v} → Ordinal.{max v w}) : BddAbove (range (g ∘ f)) := by rw [range_comp] exact bddAbove_image hf g /-- `le_ciSup` whenever the input type is small in the output universe. This lemma sometimes fails to infer `f` in simple cases and needs it to be given explicitly. -/ protected theorem le_iSup {ι} (f : ι → Ordinal.{u}) [Small.{u} ι] : ∀ i, f i ≤ iSup f := le_ciSup (bddAbove_of_small _) /-- `ciSup_le_iff'` whenever the input type is small in the output universe. -/ protected theorem iSup_le_iff {ι} {f : ι → Ordinal.{u}} {a : Ordinal.{u}} [Small.{u} ι] : iSup f ≤ a ↔ ∀ i, f i ≤ a := ciSup_le_iff' (bddAbove_of_small _) /-- An alias of `ciSup_le'` for discoverability. -/ protected theorem iSup_le {ι} {f : ι → Ordinal} {a} : (∀ i, f i ≤ a) → iSup f ≤ a := ciSup_le' /-- `lt_ciSup_iff'` whenever the input type is small in the output universe. -/ protected theorem lt_iSup_iff {ι} {f : ι → Ordinal.{u}} {a : Ordinal.{u}} [Small.{u} ι] : a < iSup f ↔ ∃ i, a < f i := lt_ciSup_iff' (bddAbove_of_small _) -- TODO: state in terms of `IsSuccLimit`. theorem succ_lt_iSup_of_ne_iSup {ι} {f : ι → Ordinal.{u}} [Small.{u} ι] (hf : ∀ i, f i ≠ iSup f) {a} (hao : a < iSup f) : succ a < iSup f := by by_contra! hoa exact hao.not_ge (Ordinal.iSup_le fun i ↦ le_of_lt_succ <| ((Ordinal.le_iSup _ _).lt_of_ne (hf i)).trans_le hoa) -- TODO: generalize to conditionally complete lattices. theorem iSup_eq_zero_iff {ι} {f : ι → Ordinal.{u}} [Small.{u} ι] : iSup f = 0 ↔ ∀ i, f i = 0 := by refine ⟨fun h i => ?_, fun h => le_antisymm (Ordinal.iSup_le fun i => Ordinal.le_zero.2 (h i)) (Ordinal.zero_le _)⟩ rw [← Ordinal.le_zero, ← h] exact Ordinal.le_iSup f i -- TODO: generalize or remove theorem iSup_eq_of_range_eq {ι ι'} {f : ι → Ordinal} {g : ι' → Ordinal} (h : Set.range f = Set.range g) : iSup f = iSup g := congr_arg _ h @[deprecated (since := "2025-10-08")] alias iSup_succ := _root_.iSup_succ -- TODO: generalize to conditionally complete lattices theorem iSup_sum {α β} (f : α ⊕ β → Ordinal.{u}) [Small.{u} α] [Small.{u} β] : iSup f = max (⨆ a, f (Sum.inl a)) (⨆ b, f (Sum.inr b)) := by apply (Ordinal.iSup_le _).antisymm (max_le _ _) · rintro (i | i) · exact le_max_of_le_left (Ordinal.le_iSup (fun x ↦ f (Sum.inl x)) i) · exact le_max_of_le_right (Ordinal.le_iSup (fun x ↦ f (Sum.inr x)) i) all_goals apply csSup_le_csSup' (bddAbove_of_small _) rintro i ⟨a, rfl⟩ apply mem_range_self theorem unbounded_range_of_le_iSup {α β : Type u} (r : α → α → Prop) [IsWellOrder α r] (f : β → α) (h : type r ≤ ⨆ i, typein r (f i)) : Unbounded r (range f) := (not_bounded_iff _).1 fun ⟨x, hx⟩ => h.not_gt <| lt_of_le_of_lt (Ordinal.iSup_le fun y => ((typein_lt_typein r).2 <| hx _ <| mem_range_self y).le) (typein_lt_type r x) theorem IsNormal.map_iSup_of_bddAbove {f : Ordinal.{u} → Ordinal.{v}} (H : IsNormal f) {ι : Type*} (g : ι → Ordinal.{u}) (hg : BddAbove (range g)) [Nonempty ι] : f (⨆ i, g i) = ⨆ i, f (g i) := Order.IsNormal.map_iSup H hg theorem IsNormal.map_iSup {f : Ordinal.{u} → Ordinal.{v}} (H : IsNormal f) {ι : Type w} (g : ι → Ordinal.{u}) [Small.{u} ι] [Nonempty ι] : f (⨆ i, g i) = ⨆ i, f (g i) := H.map_iSup_of_bddAbove g (bddAbove_of_small _) theorem IsNormal.map_sSup_of_bddAbove {f : Ordinal.{u} → Ordinal.{v}} (H : IsNormal f) {s : Set Ordinal.{u}} (hs : BddAbove s) (hn : s.Nonempty) : f (sSup s) = sSup (f '' s) := by have := hn.to_subtype rw [sSup_eq_iSup', sSup_image', H.map_iSup_of_bddAbove] rwa [Subtype.range_coe_subtype, setOf_mem_eq] theorem IsNormal.map_sSup {f : Ordinal.{u} → Ordinal.{v}} (H : IsNormal f) {s : Set Ordinal.{u}} (hn : s.Nonempty) [Small.{u} s] : f (sSup s) = sSup (f '' s) := H.map_sSup_of_bddAbove (bddAbove_of_small s) hn theorem IsNormal.apply_of_isSuccLimit {f : Ordinal.{u} → Ordinal.{v}} (H : IsNormal f) {o : Ordinal} (ho : IsSuccLimit o) : f o = ⨆ a : Iio o, f a := by have : Nonempty (Iio o) := ⟨0, ho.bot_lt⟩ rw [← H.map_iSup, ho.iSup_Iio] @[deprecated (since := "2025-07-08")] alias IsNormal.apply_of_isLimit := IsNormal.apply_of_isSuccLimit theorem sSup_ord (s : Set Cardinal) : (sSup s).ord = sSup (ord '' s) := by obtain rfl | hn := s.eq_empty_or_nonempty · simp · by_cases hs : BddAbove s · exact isNormal_ord.map_sSup hn hs · rw [csSup_of_not_bddAbove hs, csSup_of_not_bddAbove (bddAbove_ord_image_iff.not.2 hs)] simp theorem iSup_ord {ι} (f : ι → Cardinal) : (⨆ i, f i).ord = ⨆ i, (f i).ord := by rw [iSup, iSup, sSup_ord, range_comp'] theorem lift_card_sInf_compl_le (s : Set Ordinal.{u}) : Cardinal.lift.{u + 1} (sInf sᶜ).card ≤ #s := by rw [← mk_Iio_ordinal] refine mk_le_mk_of_subset fun x (hx : x < _) ↦ ?_ rw [← not_notMem] exact notMem_of_lt_csInf' hx theorem card_sInf_range_compl_le_lift {ι : Type u} (f : ι → Ordinal.{max u v}) : (sInf (range f)ᶜ).card ≤ Cardinal.lift.{v} #ι := by rw [← Cardinal.lift_le.{max u v + 1}, Cardinal.lift_lift] apply (lift_card_sInf_compl_le _).trans rw [← Cardinal.lift_id'.{u, max u v + 1} #(range _)] exact mk_range_le_lift theorem card_sInf_range_compl_le {ι : Type u} (f : ι → Ordinal.{u}) : (sInf (range f)ᶜ).card ≤ #ι := Cardinal.lift_id #ι ▸ card_sInf_range_compl_le_lift f theorem sInf_compl_lt_lift_ord_succ {ι : Type u} (f : ι → Ordinal.{max u v}) : sInf (range f)ᶜ < lift.{v} (succ #ι).ord := by rw [lift_ord, Cardinal.lift_succ, ← card_le_iff] exact card_sInf_range_compl_le_lift f theorem sInf_compl_lt_ord_succ {ι : Type u} (f : ι → Ordinal.{u}) : sInf (range f)ᶜ < (succ #ι).ord := lift_id (succ #ι).ord ▸ sInf_compl_lt_lift_ord_succ f -- TODO: remove `bsup` in favor of `iSup` in a future refactor. section bsup theorem iSup_eq_iSup {ι ι' : Type u} (r : ι → ι → Prop) (r' : ι' → ι' → Prop) [IsWellOrder ι r] [IsWellOrder ι' r'] {o : Ordinal} (ho : type r = o) (ho' : type r' = o) (f : ∀ a < o, Ordinal) : iSup (familyOfBFamily' r ho f) = iSup (familyOfBFamily' r' ho' f) := iSup_eq_of_range_eq (by simp) /-- The supremum of a family of ordinals indexed by the set of ordinals less than some `o : Ordinal.{u}`. This is a special case of `iSup` over the family provided by `familyOfBFamily`. -/ def bsup (o : Ordinal.{u}) (f : ∀ a < o, Ordinal.{max u v}) : Ordinal.{max u v} := iSup (familyOfBFamily o f) @[simp] theorem iSup_eq_bsup {o : Ordinal} (f : ∀ a < o, Ordinal) : iSup (familyOfBFamily o f) = bsup o f := rfl theorem iSup'_eq_bsup {o : Ordinal} {ι} (r : ι → ι → Prop) [IsWellOrder ι r] (ho : type r = o) (f : ∀ a < o, Ordinal) : iSup (familyOfBFamily' r ho f) = bsup o f := iSup_eq_iSup r _ ho _ f theorem sSup_eq_bsup {o : Ordinal} (f : ∀ a < o, Ordinal) : sSup (brange o f) = bsup o f := by congr rw [range_familyOfBFamily] @[simp] theorem bsup'_eq_iSup {ι} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → Ordinal) : bsup _ (bfamilyOfFamily' r f) = iSup f := by simp +unfoldPartialApp only [← iSup'_eq_bsup r, enum_typein, familyOfBFamily', bfamilyOfFamily'] @[simp] theorem bsup_eq_iSup {ι} (f : ι → Ordinal) : bsup _ (bfamilyOfFamily f) = iSup f := bsup'_eq_iSup _ f theorem bsup_eq_bsup {ι : Type u} (r r' : ι → ι → Prop) [IsWellOrder ι r] [IsWellOrder ι r'] (f : ι → Ordinal.{max u v}) : bsup.{_, v} _ (bfamilyOfFamily' r f) = bsup.{_, v} _ (bfamilyOfFamily' r' f) := by rw [bsup'_eq_iSup, bsup'_eq_iSup] @[congr] theorem bsup_congr {o₁ o₂ : Ordinal.{u}} (f : ∀ a < o₁, Ordinal.{max u v}) (ho : o₁ = o₂) : bsup.{_, v} o₁ f = bsup.{_, v} o₂ fun a h => f a (h.trans_eq ho.symm) := by subst ho rfl theorem bsup_le_iff {o f a} : bsup.{u, v} o f ≤ a ↔ ∀ i h, f i h ≤ a := Ordinal.iSup_le_iff.trans ⟨fun h i hi => by rw [← familyOfBFamily_enum o f] exact h _, fun h _ => h _ _⟩ theorem bsup_le {o : Ordinal} {f : ∀ b < o, Ordinal} {a} : (∀ i h, f i h ≤ a) → bsup.{u, v} o f ≤ a := bsup_le_iff.2 theorem le_bsup {o} (f : ∀ a < o, Ordinal) (i h) : f i h ≤ bsup o f := bsup_le_iff.1 le_rfl _ _ theorem lt_bsup {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) {a} : a < bsup.{_, v} o f ↔ ∃ i hi, a < f i hi := by simpa only [not_forall, not_le] using not_congr (@bsup_le_iff.{_, v} _ f a) theorem IsNormal.bsup {f : Ordinal → Ordinal} (H : IsNormal f) {o : Ordinal} : ∀ (g : ∀ a < o, Ordinal), o ≠ 0 → f (bsup o g) = bsup o fun a h => f (g a h) := inductionOn o fun α r _ g h => by haveI := type_ne_zero_iff_nonempty.1 h rw [← iSup'_eq_bsup r, IsNormal.map_iSup H, ← iSup'_eq_bsup r] <;> rfl theorem lt_bsup_of_ne_bsup {o : Ordinal.{u}} {f : ∀ a < o, Ordinal.{max u v}} : (∀ i h, f i h ≠ bsup.{_, v} o f) ↔ ∀ i h, f i h < bsup.{_, v} o f := ⟨fun hf _ _ => lt_of_le_of_ne (le_bsup _ _ _) (hf _ _), fun hf _ _ => ne_of_lt (hf _ _)⟩ theorem bsup_not_succ_of_ne_bsup {o : Ordinal.{u}} {f : ∀ a < o, Ordinal.{max u v}} (hf : ∀ {i : Ordinal} (h : i < o), f i h ≠ bsup.{_, v} o f) (a) : a < bsup.{_, v} o f → succ a < bsup.{_, v} o f := by rw [← iSup_eq_bsup] at * exact succ_lt_iSup_of_ne_iSup fun i => hf _ @[simp] theorem bsup_eq_zero_iff {o} {f : ∀ a < o, Ordinal} : bsup o f = 0 ↔ ∀ i hi, f i hi = 0 := by refine ⟨fun h i hi => ?_, fun h => le_antisymm (bsup_le fun i hi => Ordinal.le_zero.2 (h i hi)) (Ordinal.zero_le _)⟩ rw [← Ordinal.le_zero, ← h] exact le_bsup f i hi theorem lt_bsup_of_limit {o : Ordinal} {f : ∀ a < o, Ordinal} (hf : ∀ {a a'} (ha : a < o) (ha' : a' < o), a < a' → f a ha < f a' ha') (ho : ∀ a < o, succ a < o) (i h) : f i h < bsup o f := (hf _ _ <| lt_succ i).trans_le (le_bsup f (succ i) <| ho _ h) theorem bsup_succ_of_mono {o : Ordinal} {f : ∀ a < succ o, Ordinal} (hf : ∀ {i j} (hi hj), i ≤ j → f i hi ≤ f j hj) : bsup _ f = f o (lt_succ o) := le_antisymm (bsup_le fun _i hi => hf _ _ <| le_of_lt_succ hi) (le_bsup _ _ _) @[simp] theorem bsup_zero (f : ∀ a < (0 : Ordinal), Ordinal) : bsup 0 f = 0 := bsup_eq_zero_iff.2 fun i hi => (Ordinal.not_lt_zero i hi).elim theorem bsup_const {o : Ordinal.{u}} (ho : o ≠ 0) (a : Ordinal.{max u v}) : (bsup.{_, v} o fun _ _ => a) = a := le_antisymm (bsup_le fun _ _ => le_rfl) (le_bsup _ 0 (Ordinal.pos_iff_ne_zero.2 ho)) @[simp] theorem bsup_one (f : ∀ a < (1 : Ordinal), Ordinal) : bsup 1 f = f 0 zero_lt_one := by simp_rw [← iSup_eq_bsup, ciSup_unique, familyOfBFamily, familyOfBFamily', typein_one_toType] theorem bsup_le_of_brange_subset {o o'} {f : ∀ a < o, Ordinal} {g : ∀ a < o', Ordinal} (h : brange o f ⊆ brange o' g) : bsup.{u, max v w} o f ≤ bsup.{v, max u w} o' g := bsup_le fun i hi => by obtain ⟨j, hj, hj'⟩ := h ⟨i, hi, rfl⟩ rw [← hj'] apply le_bsup theorem bsup_eq_of_brange_eq {o o'} {f : ∀ a < o, Ordinal} {g : ∀ a < o', Ordinal} (h : brange o f = brange o' g) : bsup.{u, max v w} o f = bsup.{v, max u w} o' g := (bsup_le_of_brange_subset.{u, v, w} h.le).antisymm (bsup_le_of_brange_subset.{v, u, w} h.ge) theorem iSup_Iio_eq_bsup {o} {f : ∀ a < o, Ordinal} : ⨆ a : Iio o, f a.1 a.2 = bsup o f := by simp_rw [Iio, bsup, iSup, range_familyOfBFamily, brange, range, Subtype.exists, mem_setOf] @[deprecated (since := "2025-10-01")] alias sup_eq_sup := iSup_eq_iSup @[deprecated (since := "2025-10-01")] alias sup_eq_bsup' := iSup'_eq_bsup @[deprecated (since := "2025-10-01")] alias sup_eq_bsup := iSup_eq_bsup @[deprecated (since := "2025-10-01")] alias bsup_eq_sup' := bsup'_eq_iSup @[deprecated (since := "2025-10-01")] alias bsup_eq_sup := bsup_eq_iSup end bsup -- TODO: bring the lsub API in line with the sSup / iSup API, or deprecate it altogether. section lsub /-- The least strict upper bound of a family of ordinals. -/ def lsub {ι : Type u} (f : ι → Ordinal.{max u v}) : Ordinal := iSup (succ ∘ f) @[simp] theorem iSup_eq_lsub {ι} (f : ι → Ordinal) : iSup (succ ∘ f) = lsub f := rfl theorem lsub_le_iff {ι} {f : ι → Ordinal} {a} : lsub f ≤ a ↔ ∀ i, f i < a := by simpa using Ordinal.iSup_le_iff (f := succ ∘ f) theorem lsub_le {ι} {f : ι → Ordinal} {a} : (∀ i, f i < a) → lsub f ≤ a := lsub_le_iff.2 theorem lt_lsub {ι} (f : ι → Ordinal) (i) : f i < lsub f := succ_le_iff.1 (Ordinal.le_iSup _ i) theorem lt_lsub_iff {ι} {f : ι → Ordinal} {a} : a < lsub f ↔ ∃ i, a ≤ f i := by simpa only [not_forall, not_lt, not_le] using not_congr lsub_le_iff theorem iSup_le_lsub {ι} (f : ι → Ordinal) : iSup f ≤ lsub f := Ordinal.iSup_le fun i => (lt_lsub f i).le theorem lsub_le_succ_iSup {ι} (f : ι → Ordinal) : lsub f ≤ succ (iSup f) := lsub_le fun i => lt_succ_iff.2 (Ordinal.le_iSup f i) theorem iSup_eq_lsub_or_succ_iSup_eq_lsub {ι} (f : ι → Ordinal) : iSup f = lsub f ∨ succ (iSup f) = lsub f := by rcases eq_or_lt_of_le (iSup_le_lsub f) with h | h · exact Or.inl h · exact Or.inr ((succ_le_of_lt h).antisymm (lsub_le_succ_iSup f)) theorem succ_iSup_le_lsub_iff {ι} (f : ι → Ordinal) : succ (iSup f) ≤ lsub f ↔ ∃ i, f i = iSup f := by refine ⟨fun h => ?_, ?_⟩ · by_contra! hf have := forall_congr' fun i ↦ (Ordinal.le_iSup f i).lt_iff_ne.symm exact (succ_le_iff.1 h).ne ((iSup_le_lsub f).antisymm (lsub_le (this.1 hf))) rintro ⟨_, hf⟩ rw [succ_le_iff, ← hf] exact lt_lsub _ _ theorem succ_iSup_eq_lsub_iff {ι} (f : ι → Ordinal) : succ (iSup f) = lsub f ↔ ∃ i, f i = iSup f := (lsub_le_succ_iSup f).ge_iff_eq'.symm.trans (succ_iSup_le_lsub_iff f) theorem iSup_eq_lsub_iff {ι} (f : ι → Ordinal) : iSup f = lsub f ↔ ∀ a < lsub f, succ a < lsub f := by refine ⟨fun h => ?_, fun hf => le_antisymm (iSup_le_lsub f) (lsub_le fun i => ?_)⟩ · rw [← h] exact fun a => succ_lt_iSup_of_ne_iSup fun i => (lsub_le_iff.1 (le_of_eq h.symm) i).ne by_contra! hle have heq := (succ_iSup_eq_lsub_iff f).2 ⟨i, le_antisymm (Ordinal.le_iSup _ _) hle⟩ have := hf _ (by rw [← heq] exact lt_succ (iSup f)) rw [heq] at this exact this.false theorem iSup_eq_lsub_iff_lt_iSup {ι} (f : ι → Ordinal) : iSup f = lsub f ↔ ∀ i, f i < iSup f := ⟨fun h i => by rw [h] apply lt_lsub, fun h => le_antisymm (iSup_le_lsub f) (lsub_le h)⟩ @[simp] theorem lsub_empty {ι} [h : IsEmpty ι] (f : ι → Ordinal) : lsub f = 0 := by rw [← Ordinal.le_zero, lsub_le_iff] exact h.elim theorem lsub_pos {ι} [h : Nonempty ι] (f : ι → Ordinal) : 0 < lsub f := h.elim fun i => (Ordinal.zero_le _).trans_lt (lt_lsub f i) @[simp] theorem lsub_eq_zero_iff {ι} (f : ι → Ordinal) : lsub.{_, v} f = 0 ↔ IsEmpty ι := by refine ⟨fun h => ⟨fun i => ?_⟩, fun h => @lsub_empty _ h _⟩ have := @lsub_pos.{_, v} _ ⟨i⟩ f rw [h] at this exact this.false @[simp] theorem lsub_const {ι} [Nonempty ι] (o : Ordinal) : (lsub fun _ : ι => o) = succ o := ciSup_const @[simp] theorem lsub_unique {ι} [Unique ι] (f : ι → Ordinal) : lsub f = succ (f default) := ciSup_unique theorem lsub_le_of_range_subset {ι ι'} {f : ι → Ordinal} {g : ι' → Ordinal} (h : Set.range f ⊆ Set.range g) : lsub.{u, max v w} f ≤ lsub.{v, max u w} g := csSup_le_csSup' (bddAbove_range.{v, max u w} _) (by convert Set.image_mono h <;> apply Set.range_comp) theorem lsub_eq_of_range_eq {ι ι'} {f : ι → Ordinal} {g : ι' → Ordinal} (h : Set.range f = Set.range g) : lsub.{u, max v w} f = lsub.{v, max u w} g := (lsub_le_of_range_subset.{u, v, w} h.le).antisymm (lsub_le_of_range_subset.{v, u, w} h.ge) @[simp] theorem lsub_sum {α : Type u} {β : Type v} (f : α ⊕ β → Ordinal) : lsub.{max u v, w} f = max (lsub.{u, max v w} fun a => f (Sum.inl a)) (lsub.{v, max u w} fun b => f (Sum.inr b)) := iSup_sum _ theorem lsub_notMem_range {ι} (f : ι → Ordinal) : lsub f ∉ Set.range f := fun ⟨i, h⟩ => h.not_lt (lt_lsub f i) @[deprecated (since := "2025-05-23")] alias lsub_not_mem_range := lsub_notMem_range theorem nonempty_compl_range {ι : Type u} (f : ι → Ordinal.{max u v}) : (Set.range f)ᶜ.Nonempty := ⟨_, lsub_notMem_range f⟩ @[simp] theorem lsub_typein (o : Ordinal) : lsub.{u, u} (typein (α := o.toType) (· < ·)) = o := (lsub_le.{u, u} typein_lt_self).antisymm (by by_contra! h have h := h.trans_eq (type_toType o).symm simpa [typein_enum] using lt_lsub.{u, u} (typein (· < ·)) (enum (· < ·) ⟨_, h⟩)) theorem iSup_typein_limit {o : Ordinal.{u}} (ho : ∀ a, a < o → succ a < o) : iSup (typein ((· < ·) : o.toType → o.toType → Prop)) = o := by rw [(iSup_eq_lsub_iff.{u, u} (typein (· < ·))).2] <;> rw [lsub_typein o] assumption @[simp] theorem iSup_typein_succ {o : Ordinal} : iSup (typein ((· < ·) : (succ o).toType → (succ o).toType → Prop)) = o := by rcases iSup_eq_lsub_or_succ_iSup_eq_lsub (typein ((· < ·) : (succ o).toType → (succ o).toType → Prop)) with h | h · rw [iSup_eq_lsub_iff] at h simp only [lsub_typein] at h exact (h o (lt_succ o)).false.elim rw [← succ_eq_succ_iff, h] apply lsub_typein @[deprecated (since := "2025-10-01")] alias sup_eq_lsub := iSup_eq_lsub @[deprecated (since := "2025-10-01")] alias sup_le_lsub := iSup_le_lsub @[deprecated (since := "2025-10-01")] alias lsub_le_sup_succ := lsub_le_succ_iSup @[deprecated (since := "2025-10-01")] alias sup_eq_lsub_or_sup_succ_eq_lsub := iSup_eq_lsub_or_succ_iSup_eq_lsub @[deprecated (since := "2025-10-01")] alias sup_succ_le_lsub := succ_iSup_le_lsub_iff @[deprecated (since := "2025-10-01")] alias sup_succ_eq_lsub := succ_iSup_eq_lsub_iff @[deprecated (since := "2025-10-01")] alias sup_eq_lsub_iff_succ := iSup_eq_lsub_iff @[deprecated (since := "2025-10-01")] alias sup_eq_lsub_iff_lt_sup := iSup_eq_lsub_iff_lt_iSup @[deprecated (since := "2025-10-01")] alias sup_typein_limit := iSup_typein_limit @[deprecated (since := "2025-10-01")] alias sup_typein_succ := iSup_typein_succ end lsub -- TODO: either deprecate this in favor of `lsub` when its universes are generalized, or deprecate -- both of them at once. section blsub /-- The least strict upper bound of a family of ordinals indexed by the set of ordinals less than some `o : Ordinal.{u}`. This is to `lsub` as `bsup` is to `sup`. -/ def blsub (o : Ordinal.{u}) (f : ∀ a < o, Ordinal.{max u v}) : Ordinal.{max u v} := bsup.{_, v} o fun a ha => succ (f a ha) @[simp] theorem bsup_eq_blsub (o : Ordinal.{u}) (f : ∀ a < o, Ordinal.{max u v}) : (bsup.{_, v} o fun a ha => succ (f a ha)) = blsub.{_, v} o f := rfl theorem lsub_eq_blsub' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] {o} (ho : type r = o) (f : ∀ a < o, Ordinal) : lsub (familyOfBFamily' r ho f) = blsub o f := iSup'_eq_bsup r ho fun a ha => succ (f a ha) theorem lsub_eq_lsub {ι ι' : Type u} (r : ι → ι → Prop) (r' : ι' → ι' → Prop) [IsWellOrder ι r] [IsWellOrder ι' r'] {o} (ho : type r = o) (ho' : type r' = o) (f : ∀ a < o, Ordinal.{max u v}) : lsub.{_, v} (familyOfBFamily' r ho f) = lsub.{_, v} (familyOfBFamily' r' ho' f) := by rw [lsub_eq_blsub', lsub_eq_blsub'] @[simp] theorem lsub_eq_blsub {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : lsub.{_, v} (familyOfBFamily o f) = blsub.{_, v} o f := lsub_eq_blsub' _ _ _ @[simp] theorem blsub_eq_lsub' {ι : Type u} (r : ι → ι → Prop) [IsWellOrder ι r] (f : ι → Ordinal.{max u v}) : blsub.{_, v} _ (bfamilyOfFamily' r f) = lsub.{_, v} f := bsup'_eq_iSup r (succ ∘ f) theorem blsub_eq_blsub {ι : Type u} (r r' : ι → ι → Prop) [IsWellOrder ι r] [IsWellOrder ι r'] (f : ι → Ordinal.{max u v}) : blsub.{_, v} _ (bfamilyOfFamily' r f) = blsub.{_, v} _ (bfamilyOfFamily' r' f) := by rw [blsub_eq_lsub', blsub_eq_lsub'] @[simp] theorem blsub_eq_lsub {ι : Type u} (f : ι → Ordinal.{max u v}) : blsub.{_, v} _ (bfamilyOfFamily f) = lsub.{_, v} f := blsub_eq_lsub' _ _ @[congr] theorem blsub_congr {o₁ o₂ : Ordinal.{u}} (f : ∀ a < o₁, Ordinal.{max u v}) (ho : o₁ = o₂) : blsub.{_, v} o₁ f = blsub.{_, v} o₂ fun a h => f a (h.trans_eq ho.symm) := by subst ho rfl theorem blsub_le_iff {o : Ordinal.{u}} {f : ∀ a < o, Ordinal.{max u v}} {a} : blsub.{_, v} o f ≤ a ↔ ∀ i h, f i h < a := by convert bsup_le_iff.{_, v} (f := fun a ha => succ (f a ha)) (a := a) using 2 simp_rw [succ_le_iff] theorem blsub_le {o : Ordinal} {f : ∀ b < o, Ordinal} {a} : (∀ i h, f i h < a) → blsub o f ≤ a := blsub_le_iff.2 theorem lt_blsub {o} (f : ∀ a < o, Ordinal) (i h) : f i h < blsub o f := blsub_le_iff.1 le_rfl _ _ theorem lt_blsub_iff {o : Ordinal.{u}} {f : ∀ b < o, Ordinal.{max u v}} {a} : a < blsub.{_, v} o f ↔ ∃ i hi, a ≤ f i hi := by simpa only [not_forall, not_lt, not_le] using not_congr (@blsub_le_iff.{_, v} _ f a) theorem bsup_le_blsub {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : bsup.{_, v} o f ≤ blsub.{_, v} o f := bsup_le fun i h => (lt_blsub f i h).le theorem blsub_le_bsup_succ {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : blsub.{_, v} o f ≤ succ (bsup.{_, v} o f) := blsub_le fun i h => lt_succ_iff.2 (le_bsup f i h) theorem bsup_eq_blsub_or_succ_bsup_eq_blsub {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : bsup.{_, v} o f = blsub.{_, v} o f ∨ succ (bsup.{_, v} o f) = blsub.{_, v} o f := by rw [← iSup_eq_bsup, ← lsub_eq_blsub] exact iSup_eq_lsub_or_succ_iSup_eq_lsub _ theorem bsup_succ_le_blsub {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : succ (bsup.{_, v} o f) ≤ blsub.{_, v} o f ↔ ∃ i hi, f i hi = bsup.{_, v} o f := by refine ⟨fun h => ?_, ?_⟩ · by_contra! hf exact ne_of_lt (succ_le_iff.1 h) (le_antisymm (bsup_le_blsub f) (blsub_le (lt_bsup_of_ne_bsup.1 hf))) rintro ⟨_, _, hf⟩ rw [succ_le_iff, ← hf] exact lt_blsub _ _ _ theorem bsup_succ_eq_blsub {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : succ (bsup.{_, v} o f) = blsub.{_, v} o f ↔ ∃ i hi, f i hi = bsup.{_, v} o f := (blsub_le_bsup_succ f).ge_iff_eq'.symm.trans (bsup_succ_le_blsub f) theorem bsup_eq_blsub_iff_succ {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : bsup.{_, v} o f = blsub.{_, v} o f ↔ ∀ a < blsub.{_, v} o f, succ a < blsub.{_, v} o f := by rw [← iSup_eq_bsup, ← lsub_eq_blsub] apply iSup_eq_lsub_iff theorem bsup_eq_blsub_iff_lt_bsup {o : Ordinal.{u}} (f : ∀ a < o, Ordinal.{max u v}) : bsup.{_, v} o f = blsub.{_, v} o f ↔ ∀ i hi, f i hi < bsup.{_, v} o f := ⟨fun h i => by rw [h] apply lt_blsub, fun h => le_antisymm (bsup_le_blsub f) (blsub_le h)⟩ theorem bsup_eq_blsub_of_lt_succ_limit {o : Ordinal.{u}} (ho : IsSuccLimit o) {f : ∀ a < o, Ordinal.{max u v}} (hf : ∀ a ha, f a ha < f (succ a) (ho.succ_lt ha)) : bsup.{_, v} o f = blsub.{_, v} o f := by rw [bsup_eq_blsub_iff_lt_bsup] exact fun i hi => (hf i hi).trans_le (le_bsup f _ _) theorem blsub_succ_of_mono {o : Ordinal.{u}} {f : ∀ a < succ o, Ordinal.{max u v}} (hf : ∀ {i j} (hi hj), i ≤ j → f i hi ≤ f j hj) : blsub.{_, v} _ f = succ (f o (lt_succ o)) := bsup_succ_of_mono fun {_ _} hi hj h => succ_le_succ (hf hi hj h) @[simp] theorem blsub_eq_zero_iff {o} {f : ∀ a < o, Ordinal} : blsub o f = 0 ↔ o = 0 := by rw [← lsub_eq_blsub, lsub_eq_zero_iff] exact toType_empty_iff_eq_zero @[simp] theorem blsub_zero (f : ∀ a < (0 : Ordinal), Ordinal) : blsub 0 f = 0 := by rw [blsub_eq_zero_iff] theorem blsub_pos {o : Ordinal} (ho : 0 < o) (f : ∀ a < o, Ordinal) : 0 < blsub o f := (Ordinal.zero_le _).trans_lt (lt_blsub f 0 ho) theorem blsub_type {α : Type u} (r : α → α → Prop) [IsWellOrder α r] (f : ∀ a < type r, Ordinal.{max u v}) : blsub.{_, v} (type r) f = lsub.{_, v} fun a => f (typein r a) (typein_lt_type _ _) := eq_of_forall_ge_iff fun o => by rw [blsub_le_iff, lsub_le_iff] exact ⟨fun H b => H _ _, fun H i h => by simpa only [typein_enum] using H (enum r ⟨i, h⟩)⟩ theorem blsub_const {o : Ordinal} (ho : o ≠ 0) (a : Ordinal) : (blsub.{u, v} o fun _ _ => a) = succ a := bsup_const.{u, v} ho (succ a) @[simp] theorem blsub_one (f : ∀ a < (1 : Ordinal), Ordinal) : blsub 1 f = succ (f 0 zero_lt_one) := bsup_one _ @[simp] theorem blsub_id : ∀ o, (blsub.{u, u} o fun x _ => x) = o := lsub_typein theorem bsup_id_limit {o : Ordinal} : (∀ a < o, succ a < o) → (bsup.{u, u} o fun x _ => x) = o := iSup_typein_limit @[simp] theorem bsup_id_succ (o) : (bsup.{u, u} (succ o) fun x _ => x) = o := iSup_typein_succ theorem blsub_le_of_brange_subset {o o'} {f : ∀ a < o, Ordinal} {g : ∀ a < o', Ordinal} (h : brange o f ⊆ brange o' g) : blsub.{u, max v w} o f ≤ blsub.{v, max u w} o' g := bsup_le_of_brange_subset.{u, v, w} fun a ⟨b, hb, hb'⟩ => by obtain ⟨c, hc, hc'⟩ := h ⟨b, hb, rfl⟩ simp_rw [← hc'] at hb' exact ⟨c, hc, hb'⟩ theorem blsub_eq_of_brange_eq {o o'} {f : ∀ a < o, Ordinal} {g : ∀ a < o', Ordinal} (h : { o | ∃ i hi, f i hi = o } = { o | ∃ i hi, g i hi = o }) : blsub.{u, max v w} o f = blsub.{v, max u w} o' g := (blsub_le_of_brange_subset.{u, v, w} h.le).antisymm (blsub_le_of_brange_subset.{v, u, w} h.ge) theorem bsup_comp {o o' : Ordinal.{max u v}} {f : ∀ a < o, Ordinal.{max u v w}} (hf : ∀ {i j} (hi) (hj), i ≤ j → f i hi ≤ f j hj) {g : ∀ a < o', Ordinal.{max u v}} (hg : blsub.{_, u} o' g = o) : (bsup.{_, w} o' fun a ha => f (g a ha) (by rw [← hg]; apply lt_blsub)) = bsup.{_, w} o f := by apply le_antisymm <;> refine bsup_le fun i hi => ?_ · apply le_bsup · rw [← hg, lt_blsub_iff] at hi rcases hi with ⟨j, hj, hj'⟩ exact (hf _ _ hj').trans (le_bsup _ _ _) theorem blsub_comp {o o' : Ordinal.{max u v}} {f : ∀ a < o, Ordinal.{max u v w}} (hf : ∀ {i j} (hi) (hj), i ≤ j → f i hi ≤ f j hj) {g : ∀ a < o', Ordinal.{max u v}} (hg : blsub.{_, u} o' g = o) : (blsub.{_, w} o' fun a ha => f (g a ha) (by rw [← hg]; apply lt_blsub)) = blsub.{_, w} o f := @bsup_comp.{u, v, w} o _ (fun a ha => succ (f a ha)) (fun {_ _} _ _ h => succ_le_succ_iff.2 (hf _ _ h)) g hg theorem IsNormal.bsup_eq {f : Ordinal.{u} → Ordinal.{max u v}} (H : IsNormal f) {o : Ordinal.{u}} (h : IsSuccLimit o) : (Ordinal.bsup.{_, v} o fun x _ => f x) = f o := by rw [← IsNormal.bsup.{u, u, v} H (fun x _ => x) h.ne_bot, bsup_id_limit fun _ ↦ h.succ_lt] theorem IsNormal.blsub_eq {f : Ordinal.{u} → Ordinal.{max u v}} (H : IsNormal f) {o : Ordinal.{u}} (h : IsSuccLimit o) : (blsub.{_, v} o fun x _ => f x) = f o := by rw [← IsNormal.bsup_eq.{u, v} H h, bsup_eq_blsub_of_lt_succ_limit h] exact fun a _ => H.strictMono (lt_succ a) theorem isNormal_iff_lt_succ_and_bsup_eq {f : Ordinal.{u} → Ordinal.{max u v}} : IsNormal f ↔ (∀ a, f a < f (succ a)) ∧ ∀ o, IsSuccLimit o → (bsup.{_, v} o fun x _ => f x) = f o := ⟨fun h => ⟨fun a ↦ h.strictMono (lt_succ a), @IsNormal.bsup_eq f h⟩, fun ⟨h₁, h₂⟩ => .of_succ_lt h₁ fun ho ↦ by rw [← h₂ _ ho] simpa [IsLUB, upperBounds, lowerBounds, IsLeast, bsup_le_iff] using le_bsup _⟩ theorem isNormal_iff_lt_succ_and_blsub_eq {f : Ordinal.{u} → Ordinal.{max u v}} : IsNormal f ↔ (∀ a, f a < f (succ a)) ∧ ∀ o, IsSuccLimit o → (blsub.{_, v} o fun x _ => f x) = f o := by rw [isNormal_iff_lt_succ_and_bsup_eq.{u, v}, and_congr_right_iff] intro h constructor <;> intro H o ho <;> have := H o ho <;> rwa [← bsup_eq_blsub_of_lt_succ_limit ho fun a _ => h a] at * theorem IsNormal.eq_iff_zero_and_succ {f g : Ordinal.{u} → Ordinal.{u}} (hf : IsNormal f) (hg : IsNormal g) : f = g ↔ f 0 = g 0 ∧ ∀ a, f a = g a → f (succ a) = g (succ a) := Order.IsNormal.ext hf hg end blsub end Ordinal /-! ### Results about injectivity and surjectivity -/ theorem not_surjective_of_ordinal {α : Type*} [Small.{u} α] (f : α → Ordinal.{u}) : ¬ Surjective f := by intro h obtain ⟨a, ha⟩ := h (⨆ i, succ (f i)) apply ha.not_lt rw [Ordinal.lt_iSup_iff] exact ⟨a, Order.lt_succ _⟩ theorem not_injective_of_ordinal {α : Type*} [Small.{u} α] (f : Ordinal.{u} → α) : ¬ Injective f := fun h ↦ not_surjective_of_ordinal _ (invFun_surjective h) @[deprecated (since := "2025-08-21")] alias not_surjective_of_ordinal_of_small := not_surjective_of_ordinal @[deprecated (since := "2025-08-21")] alias not_injective_of_ordinal_of_small := not_injective_of_ordinal /-- The type of ordinals in universe `u` is not `Small.{u}`. This is the type-theoretic analog of the Burali-Forti paradox. -/ theorem not_small_ordinal : ¬Small.{u} Ordinal.{max u v} := fun h => @not_injective_of_ordinal _ h _ fun _a _b => Ordinal.lift_inj.{v, u}.1 instance Ordinal.uncountable : Uncountable Ordinal.{u} := Uncountable.of_not_small not_small_ordinal.{u} theorem Ordinal.not_bddAbove_compl_of_small (s : Set Ordinal.{u}) [hs : Small.{u} s] : ¬BddAbove sᶜ := by rw [bddAbove_iff_small] intro h have := small_union s sᶜ rw [union_compl_self, small_univ_iff] at this exact not_small_ordinal this namespace Ordinal /-! ### Casting naturals into ordinals, compatibility with operations -/ @[simp] theorem iSup_natCast : iSup Nat.cast = ω := (Ordinal.iSup_le fun n => (nat_lt_omega0 n).le).antisymm <| omega0_le.2 <| Ordinal.le_iSup _ theorem IsNormal.apply_omega0 {f : Ordinal.{u} → Ordinal.{v}} (hf : IsNormal f) : ⨆ n : ℕ, f n = f ω := by rw [← iSup_natCast, hf.map_iSup] @[simp] theorem iSup_add_nat (o : Ordinal) : ⨆ n : ℕ, o + n = o + ω := (isNormal_add_right o).apply_omega0 @[simp] theorem iSup_mul_nat (o : Ordinal) : ⨆ n : ℕ, o * n = o * ω := by rcases eq_zero_or_pos o with (rfl | ho) · rw [zero_mul] exact iSup_eq_zero_iff.2 fun n => zero_mul (n : Ordinal) · exact (isNormal_mul_right ho).apply_omega0 end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Veblen.lean
import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Veblen hierarchy We define the two-arguments Veblen function, which satisfies `veblen 0 a = ω ^ a` and that for `o ≠ 0`, `veblen o` enumerates the common fixed points of `veblen o'` for `o' < o`. We use this to define two important functions on ordinals: the epsilon function `ε_ o = veblen 1 o`, and the gamma function `Γ_ o` enumerating the fixed points of `veblen · 0`. ## Main definitions * `veblenWith`: The Veblen hierarchy with a specified initial function. * `veblen`: The Veblen hierarchy starting with `ω ^ ·`. ## Notation The following notation is scoped to the `Ordinal` namespace. - `ε_ o` is notation for `veblen 1 o`. `ε₀` is notation for `ε_ 0`. - `Γ_ o` is notation for `gamma o`. `Γ₀` is notation for `Γ_ 0`. ## TODO - Prove that `ε₀` and `Γ₀` are countable. - Prove that the exponential principal ordinals are the epsilon ordinals (and 0, 1, 2, ω). - Prove that the ordinals principal under `veblen` are the gamma ordinals (and 0). ## References * [Larry W. Miller, Normal functions and constructive ordinal notations][Miller_1976] -/ noncomputable section open Order Set universe u namespace Ordinal variable {f : Ordinal.{u} → Ordinal.{u}} {o o₁ o₂ a b x : Ordinal.{u}} /-! ### Veblen function with a given starting function -/ section veblenWith /-- `veblenWith f o` is the `o`-th function in the Veblen hierarchy starting with `f`. This is defined so that - `veblenWith f 0 = f`. - `veblenWith f o` for `o ≠ 0` enumerates the common fixed points of `veblenWith f o'` over all `o' < o`. -/ @[pp_nodot] def veblenWith (f : Ordinal.{u} → Ordinal.{u}) (o : Ordinal.{u}) : Ordinal.{u} → Ordinal.{u} := if o = 0 then f else derivFamily fun (⟨x, _⟩ : Iio o) ↦ veblenWith f x termination_by o @[simp] theorem veblenWith_zero (f : Ordinal → Ordinal) : veblenWith f 0 = f := by rw [veblenWith, if_pos rfl] theorem veblenWith_of_ne_zero (f : Ordinal → Ordinal) (h : o ≠ 0) : veblenWith f o = derivFamily fun x : Iio o ↦ veblenWith f x.1 := by rw [veblenWith, if_neg h] /-- `veblenWith f o` is always normal for `o ≠ 0`. See `isNormal_veblenWith` for a version which assumes `IsNormal f`. -/ theorem isNormal_veblenWith' (f : Ordinal → Ordinal) (h : o ≠ 0) : IsNormal (veblenWith f o) := by rw [veblenWith_of_ne_zero f h] exact isNormal_derivFamily _ variable (hf : IsNormal f) include hf /-- `veblenWith f o` is always normal whenever `f` is. See `isNormal_veblenWith'` for a version which does not assume `IsNormal f`. -/ theorem isNormal_veblenWith (o : Ordinal) : IsNormal (veblenWith f o) := by obtain rfl | h := eq_or_ne o 0 · rwa [veblenWith_zero] · exact isNormal_veblenWith' f h protected alias IsNormal.veblenWith := isNormal_veblenWith theorem mem_range_veblenWith (h : o ≠ 0) : a ∈ range (veblenWith f o) ↔ ∀ b < o, veblenWith f b a = a := by rw [veblenWith_of_ne_zero f h, mem_range_derivFamily (fun _ ↦ isNormal_veblenWith hf _)] exact Subtype.forall theorem veblenWith_veblenWith_of_lt (h : o₁ < o₂) (a : Ordinal) : veblenWith f o₁ (veblenWith f o₂ a) = veblenWith f o₂ a := by apply (mem_range_veblenWith hf h.ne_bot).1 _ _ h simp theorem veblenWith_eq_self_of_le (h : o₁ ≤ o₂) (h' : veblenWith f o₂ a = a) : veblenWith f o₁ a = a := by obtain rfl | h := h.eq_or_lt · assumption · rw [← h', veblenWith_veblenWith_of_lt hf h] theorem veblenWith_mem_range : veblenWith f o a ∈ range f := by obtain rfl | h := eq_zero_or_pos o · simp · rw [← veblenWith_veblenWith_of_lt hf h] simp theorem veblenWith_succ (o : Ordinal) : veblenWith f (succ o) = deriv (veblenWith f o) := by rw [deriv_eq_enumOrd (hf.veblenWith o), veblenWith_of_ne_zero f (succ_ne_zero _), derivFamily_eq_enumOrd] · apply congr_arg ext a rw [mem_iInter] use fun ha ↦ ha ⟨o, lt_succ o⟩ rintro (ha : _ = _) ⟨b, hb : b < _⟩ obtain rfl | hb := lt_succ_iff_eq_or_lt.1 hb · rw [Function.mem_fixedPoints_iff, ha] · rw [← ha] exact veblenWith_veblenWith_of_lt hf hb _ · exact fun o ↦ hf.veblenWith o.1 theorem veblenWith_right_strictMono (o : Ordinal) : StrictMono (veblenWith f o) := (hf.veblenWith o).strictMono @[simp] theorem veblenWith_lt_veblenWith_iff_right : veblenWith f o a < veblenWith f o b ↔ a < b := (veblenWith_right_strictMono hf o).lt_iff_lt @[simp] theorem veblenWith_le_veblenWith_iff_right : veblenWith f o a ≤ veblenWith f o b ↔ a ≤ b := (veblenWith_right_strictMono hf o).le_iff_le theorem veblenWith_injective (o : Ordinal) : Function.Injective (veblenWith f o) := (veblenWith_right_strictMono hf o).injective @[simp] theorem veblenWith_inj : veblenWith f o a = veblenWith f o b ↔ a = b := (veblenWith_injective hf o).eq_iff theorem right_le_veblenWith (o a : Ordinal) : a ≤ veblenWith f o a := (veblenWith_right_strictMono hf o).le_apply theorem veblenWith_left_monotone (a : Ordinal) : Monotone (veblenWith f · a) := by rw [monotone_iff_forall_lt] intro o₁ o₂ h rw [← veblenWith_veblenWith_of_lt hf h] exact (veblenWith_right_strictMono hf o₁).monotone (right_le_veblenWith hf o₂ a) theorem veblenWith_pos (hp : 0 < f 0) : 0 < veblenWith f o a := by have H (b) : 0 < veblenWith f 0 b := by rw [veblenWith_zero] exact hp.trans_le (hf.monotone (Ordinal.zero_le _)) obtain rfl | h := eq_zero_or_pos o · exact H a · rw [← veblenWith_veblenWith_of_lt hf h] exact H _ theorem veblenWith_zero_strictMono (hp : 0 < f 0) : StrictMono (veblenWith f · 0) := by intro o₁ o₂ h dsimp only rw [← veblenWith_veblenWith_of_lt hf h, veblenWith_lt_veblenWith_iff_right hf] exact veblenWith_pos hf hp theorem veblenWith_zero_lt_veblenWith_zero (hp : 0 < f 0) : veblenWith f o₁ 0 < veblenWith f o₂ 0 ↔ o₁ < o₂ := (veblenWith_zero_strictMono hf hp).lt_iff_lt theorem veblenWith_zero_le_veblenWith_zero (hp : 0 < f 0) : veblenWith f o₁ 0 ≤ veblenWith f o₂ 0 ↔ o₁ ≤ o₂ := (veblenWith_zero_strictMono hf hp).le_iff_le theorem veblenWith_zero_inj (hp : 0 < f 0) : veblenWith f o₁ 0 = veblenWith f o₂ 0 ↔ o₁ = o₂ := (veblenWith_zero_strictMono hf hp).injective.eq_iff theorem left_le_veblenWith (hp : 0 < f 0) (o a : Ordinal) : o ≤ veblenWith f o a := (veblenWith_zero_strictMono hf hp).le_apply.trans <| (veblenWith_right_strictMono hf _).monotone (Ordinal.zero_le _) theorem IsNormal.veblenWith_zero (hp : 0 < f 0) : IsNormal (veblenWith f · 0) := by rw [isNormal_iff_strictMono_limit] refine ⟨veblenWith_zero_strictMono hf hp, fun o ho a IH ↦ ?_⟩ rw [veblenWith_of_ne_zero f ho.ne_bot, derivFamily_zero] apply nfpFamily_le fun l ↦ ?_ suffices ∃ b < o, List.foldr _ 0 l ≤ veblenWith f b 0 by obtain ⟨b, hb, hb'⟩ := this exact hb'.trans (IH b hb) induction l with | nil => use 0; simpa using ho.bot_lt | cons a l IH => obtain ⟨b, hb, hb'⟩ := IH refine ⟨_, ho.succ_lt (max_lt a.2 hb), ((veblenWith_right_strictMono hf _).monotone <| hb'.trans <| veblenWith_left_monotone hf _ <| (le_max_right a.1 b).trans (le_succ _)).trans ?_⟩ rw [veblenWith_veblenWith_of_lt hf] rw [lt_succ_iff] exact le_max_left _ b theorem veblenWith_veblenWith_eq_veblenWith_iff (h : o₂ ≤ o₁) : veblenWith f o₁ (veblenWith f o₂ a) = veblenWith f o₂ a ↔ veblenWith f o₁ a = a := by grind [veblenWith_inj, → veblenWith_eq_self_of_le] theorem veblenWith_lt_veblenWith_veblenWith_iff (h : o₂ ≤ o₁) : veblenWith f o₂ a < veblenWith f o₁ (veblenWith f o₂ a) ↔ a < veblenWith f o₁ a := by simp_rw [(right_le_veblenWith hf ..).lt_iff_ne', ne_eq, veblenWith_veblenWith_eq_veblenWith_iff hf h] theorem veblenWith_apply_eq_apply_iff : veblenWith f o (f a) = f a ↔ veblenWith f o a = a := by simpa using veblenWith_veblenWith_eq_veblenWith_iff hf (zero_le o) theorem apply_lt_veblenWith_apply_iff : f a < veblenWith f o (f a) ↔ a < veblenWith f o a := by simpa using veblenWith_lt_veblenWith_veblenWith_iff hf (zero_le o) theorem cmp_veblenWith : cmp (veblenWith f o₁ a) (veblenWith f o₂ b) = match cmp o₁ o₂ with | .eq => cmp a b | .lt => cmp a (veblenWith f o₂ b) | .gt => cmp (veblenWith f o₁ a) b := by obtain h | rfl | h := lt_trichotomy o₁ o₂ on_goal 2 => simp [(veblenWith_right_strictMono hf _).cmp_map_eq] all_goals conv_lhs => rw [← veblenWith_veblenWith_of_lt hf h] simp [h.cmp_eq_lt, h.cmp_eq_gt, (veblenWith_right_strictMono hf _).cmp_map_eq] /-- `veblenWith f o₁ a < veblenWith f o₂ b` iff one of the following holds: * `o₁ = o₂` and `a < b` * `o₁ < o₂` and `a < veblenWith f o₂ b` * `o₁ > o₂` and `veblenWith f o₁ a < b` -/ theorem veblenWith_lt_veblenWith_iff : veblenWith f o₁ a < veblenWith f o₂ b ↔ o₁ = o₂ ∧ a < b ∨ o₁ < o₂ ∧ a < veblenWith f o₂ b ∨ o₂ < o₁ ∧ veblenWith f o₁ a < b := by rw [← cmp_eq_lt_iff, cmp_veblenWith hf] aesop (add simp lt_asymm) /-- `veblenWith f o₁ a ≤ veblenWith f o₂ b` iff one of the following holds: * `o₁ = o₂` and `a ≤ b` * `o₁ < o₂` and `a ≤ veblenWith f o₂ b` * `o₁ > o₂` and `veblenWith f o₁ a ≤ b` -/ theorem veblenWith_le_veblenWith_iff : veblenWith f o₁ a ≤ veblenWith f o₂ b ↔ o₁ = o₂ ∧ a ≤ b ∨ o₁ < o₂ ∧ a ≤ veblenWith f o₂ b ∨ o₂ < o₁ ∧ veblenWith f o₁ a ≤ b := by rw [← not_lt, ← cmp_eq_gt_iff, cmp_veblenWith hf] aesop (add simp [not_lt_of_ge, lt_asymm]) /-- `veblenWith f o₁ a = veblenWith f o₂ b` iff one of the following holds: * `o₁ = o₂` and `a = b` * `o₁ < o₂` and `a = veblenWith f o₂ b` * `o₁ > o₂` and `veblenWith f o₁ a = b` -/ theorem veblenWith_eq_veblenWith_iff : veblenWith f o₁ a = veblenWith f o₂ b ↔ o₁ = o₂ ∧ a = b ∨ o₁ < o₂ ∧ a = veblenWith f o₂ b ∨ o₂ < o₁ ∧ veblenWith f o₁ a = b := by rw [← cmp_eq_eq_iff, cmp_veblenWith hf] aesop (add simp lt_asymm) end veblenWith /-! ### Veblen function -/ section veblen /-- `veblen o` is the `o`-th function in the Veblen hierarchy starting with `ω ^ ·`. That is: - `veblen 0 a = ω ^ a`. - `veblen o` for `o ≠ 0` enumerates the fixed points of `veblen o'` for `o' < o`. -/ @[pp_nodot] def veblen : Ordinal.{u} → Ordinal.{u} → Ordinal.{u} := veblenWith (ω ^ ·) @[simp] theorem veblen_zero : veblen 0 = fun a ↦ ω ^ a := by rw [veblen, veblenWith_zero] theorem veblen_zero_apply (a : Ordinal) : veblen 0 a = ω ^ a := by rw [veblen_zero] theorem veblen_of_ne_zero (h : o ≠ 0) : veblen o = derivFamily fun x : Iio o ↦ veblen x.1 := veblenWith_of_ne_zero _ h theorem isNormal_veblen (o : Ordinal) : IsNormal (veblen o) := (isNormal_opow one_lt_omega0).veblenWith o theorem mem_range_veblen (h : o ≠ 0) : a ∈ range (veblen o) ↔ ∀ b < o, veblen b a = a := mem_range_veblenWith (isNormal_opow one_lt_omega0) h theorem veblen_veblen_of_lt (h : o₁ < o₂) (a : Ordinal) : veblen o₁ (veblen o₂ a) = veblen o₂ a := veblenWith_veblenWith_of_lt (isNormal_opow one_lt_omega0) h a theorem veblen_eq_self_of_le (h : o₁ ≤ o₂) (h' : veblen o₂ a = a) : veblen o₁ a = a := veblenWith_eq_self_of_le (isNormal_opow one_lt_omega0) h h' theorem veblen_mem_range_opow (o a : Ordinal) : veblen o a ∈ range (ω ^ · : Ordinal → Ordinal) := veblenWith_mem_range (isNormal_opow one_lt_omega0) theorem veblen_succ (o : Ordinal) : veblen (succ o) = deriv (veblen o) := veblenWith_succ (isNormal_opow one_lt_omega0) o theorem veblen_right_strictMono (o : Ordinal) : StrictMono (veblen o) := veblenWith_right_strictMono (isNormal_opow one_lt_omega0) o @[simp] theorem veblen_lt_veblen_iff_right : veblen o a < veblen o b ↔ a < b := veblenWith_lt_veblenWith_iff_right (isNormal_opow one_lt_omega0) @[simp] theorem veblen_le_veblen_iff_right : veblen o a ≤ veblen o b ↔ a ≤ b := veblenWith_le_veblenWith_iff_right (isNormal_opow one_lt_omega0) theorem veblen_injective (o : Ordinal) : Function.Injective (veblen o) := veblenWith_injective (isNormal_opow one_lt_omega0) o @[simp] theorem veblen_inj : veblen o a = veblen o b ↔ a = b := (veblen_injective o).eq_iff theorem right_le_veblen (o a : Ordinal) : a ≤ veblen o a := right_le_veblenWith (isNormal_opow one_lt_omega0) o a theorem veblen_left_monotone (o : Ordinal) : Monotone (veblen · o) := veblenWith_left_monotone (isNormal_opow one_lt_omega0) o @[simp] theorem veblen_pos : 0 < veblen o a := veblenWith_pos (isNormal_opow one_lt_omega0) (by simp) theorem veblen_zero_strictMono : StrictMono (veblen · 0) := veblenWith_zero_strictMono (isNormal_opow one_lt_omega0) (by simp) @[simp] theorem veblen_zero_lt_veblen_zero : veblen o₁ 0 < veblen o₂ 0 ↔ o₁ < o₂ := veblen_zero_strictMono.lt_iff_lt @[simp] theorem veblen_zero_le_veblen_zero : veblen o₁ 0 ≤ veblen o₂ 0 ↔ o₁ ≤ o₂ := veblen_zero_strictMono.le_iff_le @[simp] theorem veblen_zero_inj : veblen o₁ 0 = veblen o₂ 0 ↔ o₁ = o₂ := veblen_zero_strictMono.injective.eq_iff theorem left_le_veblen (o a : Ordinal) : o ≤ veblen o a := left_le_veblenWith (isNormal_opow one_lt_omega0) (by simp) o a theorem isNormal_veblen_zero : IsNormal (veblen · 0) := (isNormal_opow one_lt_omega0).veblenWith_zero (by simp) theorem veblen_veblen_eq_veblen_iff (h : o₂ ≤ o₁) : veblen o₁ (veblen o₂ a) = veblen o₂ a ↔ veblen o₁ a = a := veblenWith_veblenWith_eq_veblenWith_iff (isNormal_opow one_lt_omega0) h theorem veblen_lt_veblen_veblen_iff (h : o₂ ≤ o₁) : veblen o₂ a < veblen o₁ (veblen o₂ a) ↔ a < veblen o₁ a := veblenWith_lt_veblenWith_veblenWith_iff (isNormal_opow one_lt_omega0) h theorem veblen_opow_eq_opow_iff : veblen o (ω ^ a) = ω ^ a ↔ veblen o a = a := veblenWith_apply_eq_apply_iff (isNormal_opow one_lt_omega0) theorem opow_lt_veblen_opow_iff : ω ^ a < veblen o (ω ^ a) ↔ a < veblen o a := apply_lt_veblenWith_apply_iff (isNormal_opow one_lt_omega0) theorem lt_veblen (a : Ordinal) : a < veblen a a := by obtain rfl | h := eq_zero_or_pos a · simp · apply (left_le_veblen a 0).trans_lt simpa theorem cmp_veblen : cmp (veblen o₁ a) (veblen o₂ b) = match cmp o₁ o₂ with | .eq => cmp a b | .lt => cmp a (veblen o₂ b) | .gt => cmp (veblen o₁ a) b := cmp_veblenWith (isNormal_opow one_lt_omega0) /-- `veblen o₁ a < veblen o₂ b` iff one of the following holds: * `o₁ = o₂` and `a < b` * `o₁ < o₂` and `a < veblen o₂ b` * `o₁ > o₂` and `veblen o₁ a < b` -/ theorem veblen_lt_veblen_iff : veblen o₁ a < veblen o₂ b ↔ o₁ = o₂ ∧ a < b ∨ o₁ < o₂ ∧ a < veblen o₂ b ∨ o₂ < o₁ ∧ veblen o₁ a < b := veblenWith_lt_veblenWith_iff (isNormal_opow one_lt_omega0) /-- `veblen o₁ a ≤ veblen o₂ b` iff one of the following holds: * `o₁ = o₂` and `a ≤ b` * `o₁ < o₂` and `a ≤ veblen o₂ b` * `o₁ > o₂` and `veblen o₁ a ≤ b` -/ theorem veblen_le_veblen_iff : veblen o₁ a ≤ veblen o₂ b ↔ o₁ = o₂ ∧ a ≤ b ∨ o₁ < o₂ ∧ a ≤ veblen o₂ b ∨ o₂ < o₁ ∧ veblen o₁ a ≤ b := veblenWith_le_veblenWith_iff (isNormal_opow one_lt_omega0) /-- `veblen o₁ a ≤ veblen o₂ b` iff one of the following holds: * `o₁ = o₂` and `a = b` * `o₁ < o₂` and `a = veblen o₂ b` * `o₁ > o₂` and `veblen o₁ a = b` -/ theorem veblen_eq_veblen_iff : veblen o₁ a = veblen o₂ b ↔ o₁ = o₂ ∧ a = b ∨ o₁ < o₂ ∧ a = veblen o₂ b ∨ o₂ < o₁ ∧ veblen o₁ a = b := veblenWith_eq_veblenWith_iff (isNormal_opow one_lt_omega0) end veblen /-! ### Inverse Veblen function -/ /-- For any given `x`, there exists a unique pair `(o, a)` such that `ω ^ x = veblen o a` and `a < ω ^ x`. `invVeblen₁ x` and `invVeblen₂ x` return the first and second entries of this pair, respectively. See `veblen_eq_opow_iff` for a proof. Composing this function with `Ordinal.CNF` yields a predicative ordinal notation up to `Γ₀`. -/ def invVeblen₁ (x : Ordinal) : Ordinal := sInf {y | veblen y x ≠ x} theorem veblen_eq_of_lt_invVeblen₁ (h : o < invVeblen₁ x) : veblen o x = x := by simpa using notMem_of_lt_csInf' h theorem invVeblen₁_le (x : Ordinal) : invVeblen₁ x ≤ x := csInf_le' (lt_veblen x).ne' theorem lt_veblen_invVeblen₁ (x : Ordinal) : x < veblen (invVeblen₁ x) x := (right_le_veblen ..).lt_of_ne' (csInf_mem (s := {y | veblen y x ≠ x}) ⟨x, (lt_veblen x).ne'⟩) theorem lt_veblen_iff_invVeblen₁_le : a < veblen o a ↔ invVeblen₁ a ≤ o := by obtain h | h := lt_or_ge o (invVeblen₁ a) · rw [veblen_eq_of_lt_invVeblen₁ h] simpa · simpa [(lt_veblen_invVeblen₁ a).trans_le (veblen_left_monotone _ h)] theorem mem_range_veblen_iff_le_invVeblen₁ : ω ^ x ∈ range (veblen o) ↔ o ≤ invVeblen₁ x := by obtain h | rfl | h := lt_trichotomy o (invVeblen₁ x) · exact iff_of_true ⟨_, veblen_opow_eq_opow_iff.2 <| veblen_eq_of_lt_invVeblen₁ h⟩ h.le · apply iff_of_true _ le_rfl by_cases h : invVeblen₁ x = 0 · simp [h] · simp_rw [mem_range_veblen h, veblen_opow_eq_opow_iff] exact fun o ↦ veblen_eq_of_lt_invVeblen₁ · apply iff_of_false _ h.not_ge rintro ⟨z, hz⟩ have hz' := hz rw [← veblen_veblen_of_lt h, hz', veblen_opow_eq_opow_iff] at hz exact (lt_veblen_invVeblen₁ x).ne' hz theorem invVeblen₁_veblen (h : a < veblen o a) : invVeblen₁ (veblen o a) = o := by apply le_antisymm · rwa [← lt_veblen_iff_invVeblen₁_le, veblen_lt_veblen_iff_right] · rw [← mem_range_veblen_iff_le_invVeblen₁] obtain rfl | ho := eq_zero_or_pos o · simp · rw [← veblen_zero_apply, veblen_veblen_of_lt ho] simp theorem invVeblen₁_of_lt_opow (h : a < ω ^ a) : invVeblen₁ a = 0 := by rwa [← Ordinal.le_zero, ← lt_veblen_iff_invVeblen₁_le, veblen_zero] @[simp] theorem invVeblen₁_zero : invVeblen₁ 0 = 0 := invVeblen₁_of_lt_opow <| by simp @[inherit_doc invVeblen₁] def invVeblen₂ (x : Ordinal) : Ordinal := Classical.choose ((mem_range_veblen_iff_le_invVeblen₁ (x := x)).2 le_rfl) @[simp] theorem veblen_invVeblen₁_invVeblen₂ (x : Ordinal) : veblen (invVeblen₁ x) (invVeblen₂ x) = ω ^ x := Classical.choose_spec (mem_range_veblen_iff_le_invVeblen₁.2 le_rfl) theorem invVeblen₂_eq_iff : invVeblen₂ x = a ↔ ω ^ x = veblen (invVeblen₁ x) a := by rw [← veblen_inj (o := x.invVeblen₁), veblen_invVeblen₁_invVeblen₂] theorem invVeblen₂_lt_iff : invVeblen₂ x < a ↔ ω ^ x < veblen (invVeblen₁ x) a := by rw [← veblen_lt_veblen_iff_right (o := x.invVeblen₁), veblen_invVeblen₁_invVeblen₂] theorem invVeblen₂_le_iff : invVeblen₂ x ≤ a ↔ ω ^ x ≤ veblen (invVeblen₁ x) a := by rw [← veblen_le_veblen_iff_right (o := x.invVeblen₁), veblen_invVeblen₁_invVeblen₂] theorem lt_invVeblen₂_iff : a < invVeblen₂ x ↔ veblen (invVeblen₁ x) a < ω ^ x := by rw [← veblen_lt_veblen_iff_right (o := x.invVeblen₁), veblen_invVeblen₁_invVeblen₂] theorem le_invVeblen₂_iff : a ≤ invVeblen₂ x ↔ veblen (invVeblen₁ x) a ≤ ω ^ x := by rw [← veblen_le_veblen_iff_right (o := x.invVeblen₁), veblen_invVeblen₁_invVeblen₂] theorem invVeblen₂_lt (x : Ordinal) : invVeblen₂ x < ω ^ x := by rw [invVeblen₂_lt_iff, opow_lt_veblen_opow_iff] exact lt_veblen_invVeblen₁ x theorem invVeblen₂_le (x : Ordinal) : invVeblen₂ x ≤ x := by obtain h | h := eq_zero_or_pos (invVeblen₁ x) · rw [invVeblen₂_le_iff, h, veblen_zero] · convert (invVeblen₂_lt x).le rw [← veblen_zero_apply, veblen_eq_of_lt_invVeblen₁ h] theorem invVeblen₂_of_lt_opow (h : a < ω ^ a) : invVeblen₂ a = a := by rw [invVeblen₂_eq_iff, invVeblen₁_of_lt_opow h, veblen_zero_apply] @[simp] theorem invVeblen₂_zero : invVeblen₂ 0 = 0 := by apply invVeblen₂_of_lt_opow simp theorem invVeblen₂_veblen (ho : o ≠ 0) (h : a < veblen o a) : invVeblen₂ (veblen o a) = a := by rw [invVeblen₂_eq_iff, invVeblen₁_veblen h, ← veblen_zero_apply, veblen_veblen_of_lt] exact ho.bot_lt theorem veblen_eq_opow_iff (h : a < veblen o a) : veblen o a = ω ^ x ↔ invVeblen₁ x = o ∧ invVeblen₂ x = a := by refine ⟨?_, fun ⟨hx, ha⟩ ↦ ?_⟩ · obtain rfl | ho := eq_zero_or_pos o · rw [veblen_zero] at h have := invVeblen₁_of_lt_opow h have := invVeblen₂_of_lt_opow h aesop · rw [← veblen_veblen_of_lt ho, veblen_zero_apply, opow_right_inj one_lt_omega0] rintro rfl simp [invVeblen₁_veblen h, invVeblen₂_veblen ho.ne' h] · convert ← veblen_invVeblen₁_invVeblen₂ x /-! ### Epsilon function -/ /-- The epsilon function enumerates the fixed points of `ω ^ ⬝`. This is an abbreviation for `veblen 1`. -/ abbrev epsilon := veblen 1 @[inherit_doc] scoped notation "ε_ " => epsilon /-- `ε₀` is the first fixed point of `ω ^ ⬝`, i.e. the supremum of `ω`, `ω ^ ω`, `ω ^ ω ^ ω`, … -/ scoped notation "ε₀" => ε_ 0 theorem epsilon_eq_deriv (o : Ordinal) : ε_ o = deriv (fun a ↦ ω ^ a) o := by rw [epsilon, ← succ_zero, veblen_succ, veblen_zero] theorem epsilon0_eq_nfp : ε₀ = nfp (fun a ↦ ω ^ a) 0 := by rw [epsilon_eq_deriv, deriv_zero_right] theorem epsilon_succ_eq_nfp (o : Ordinal) : ε_ (succ o) = nfp (fun a ↦ ω ^ a) (succ (ε_ o)) := by rw [epsilon_eq_deriv, epsilon_eq_deriv, deriv_succ] theorem epsilon0_le_of_omega0_opow_le (h : ω ^ o ≤ o) : ε₀ ≤ o := by rw [epsilon0_eq_nfp] exact nfp_le_fp (fun _ _ ↦ (opow_le_opow_iff_right one_lt_omega0).2) (Ordinal.zero_le o) h @[simp] theorem omega0_opow_epsilon (o : Ordinal) : ω ^ ε_ o = ε_ o := by rw [epsilon_eq_deriv, (isNormal_opow one_lt_omega0).deriv_fp] /-- `ε₀` is the limit of `0`, `ω ^ 0`, `ω ^ ω ^ 0`, … -/ theorem lt_epsilon0 : o < ε₀ ↔ ∃ n : ℕ, o < (fun a ↦ ω ^ a)^[n] 0 := by rw [epsilon0_eq_nfp, lt_nfp_iff] /-- `ω ^ ω ^ … ^ 0 < ε₀` -/ theorem iterate_omega0_opow_lt_epsilon0 (n : ℕ) : (fun a ↦ ω ^ a)^[n] 0 < ε₀ := by rw [epsilon0_eq_nfp] apply iterate_lt_nfp (isNormal_opow one_lt_omega0).strictMono simp theorem omega0_lt_epsilon (o : Ordinal) : ω < ε_ o := by apply lt_of_lt_of_le _ <| (veblen_right_strictMono _).monotone (Ordinal.zero_le o) simpa using iterate_omega0_opow_lt_epsilon0 2 theorem natCast_lt_epsilon (n : ℕ) (o : Ordinal) : n < ε_ o := (nat_lt_omega0 n).trans <| omega0_lt_epsilon o theorem epsilon_pos (o : Ordinal) : 0 < ε_ o := veblen_pos theorem invVeblen₁_epsilon (h : o < ε_ o) : invVeblen₁ (ε_ o) = 1 := invVeblen₁_veblen h theorem invVeblen₂_epsilon (h : o < ε_ o) : invVeblen₂ (ε_ o) = o := invVeblen₂_veblen one_ne_zero h /-! ### Gamma function -/ /-- The gamma function enumerates the fixed points of `veblen · 0`. Of particular importance is `Γ₀ = gamma 0`, the Feferman-Schütte ordinal. -/ def gamma : Ordinal → Ordinal := deriv (veblen · 0) @[inherit_doc] scoped notation "Γ_ " => gamma /-- The Feferman-Schütte ordinal `Γ₀` is the smallest fixed point of `veblen · 0`, i.e. the supremum of `veblen ε₀ 0`, `veblen (veblen ε₀ 0) 0`, etc. -/ scoped notation "Γ₀" => Γ_ 0 theorem isNormal_gamma : IsNormal gamma := isNormal_deriv _ theorem mem_range_gamma : o ∈ range Γ_ ↔ veblen o 0 = o := isNormal_veblen_zero.mem_range_deriv theorem strictMono_gamma : StrictMono gamma := isNormal_gamma.strictMono theorem monotone_gamma : Monotone gamma := isNormal_gamma.monotone @[simp] theorem gamma_lt_gamma : Γ_ a < Γ_ b ↔ a < b := strictMono_gamma.lt_iff_lt @[simp] theorem gamma_le_gamma : Γ_ a ≤ Γ_ b ↔ a ≤ b := strictMono_gamma.le_iff_le @[simp] theorem gamma_inj : Γ_ a = Γ_ b ↔ a = b := strictMono_gamma.injective.eq_iff @[simp] theorem veblen_gamma_zero (o : Ordinal) : veblen (Γ_ o) 0 = Γ_ o := isNormal_veblen_zero.deriv_fp o theorem gamma0_eq_nfp : Γ₀ = nfp (veblen · 0) 0 := deriv_zero_right _ theorem gamma_succ_eq_nfp (o : Ordinal) : Γ_ (succ o) = nfp (veblen · 0) (succ (Γ_ o)) := deriv_succ _ _ theorem gamma0_le_of_veblen_le (h : veblen o 0 ≤ o) : Γ₀ ≤ o := by rw [gamma0_eq_nfp] exact nfp_le_fp (veblen_left_monotone 0) (Ordinal.zero_le o) h /-- `Γ₀` is the limit of `0`, `veblen 0 0`, `veblen (veblen 0 0) 0`, … -/ theorem lt_gamma0 : o < Γ₀ ↔ ∃ n : ℕ, o < (fun a ↦ veblen a 0)^[n] 0 := by rw [gamma0_eq_nfp, lt_nfp_iff] /-- `veblen (veblen … (veblen 0 0) … 0) 0 < Γ₀` -/ theorem iterate_veblen_lt_gamma0 (n : ℕ) : (fun a ↦ veblen a 0)^[n] 0 < Γ₀ := by rw [gamma0_eq_nfp] apply iterate_lt_nfp veblen_zero_strictMono simp theorem epsilon0_lt_gamma (o : Ordinal) : ε₀ < Γ_ o := by apply lt_of_lt_of_le _ <| (gamma_le_gamma.2 (Ordinal.zero_le _)) simpa using iterate_veblen_lt_gamma0 2 theorem omega0_lt_gamma (o : Ordinal) : ω < Γ_ o := (omega0_lt_epsilon 0).trans (epsilon0_lt_gamma o) theorem natCast_lt_gamma (n : ℕ) : n < Γ_ o := (nat_lt_omega0 n).trans (omega0_lt_gamma o) @[simp] theorem gamma_pos : 0 < Γ_ o := natCast_lt_gamma 0 @[simp] theorem gamma_ne_zero : Γ_ o ≠ 0 := gamma_pos.ne' @[simp] theorem invVeblen₁_gamma (o : Ordinal) : invVeblen₁ (Γ_ o) = Γ_ o := by rw [← veblen_gamma_zero, invVeblen₁_veblen veblen_pos, veblen_gamma_zero] @[simp] theorem invVeblen₂_gamma (o : Ordinal) : invVeblen₂ (Γ_ o) = 0 := by rw [← veblen_gamma_zero, invVeblen₂_veblen gamma_ne_zero veblen_pos] theorem invVeblen₁_eq_iff : invVeblen₁ o = o ↔ o = 0 ∨ o ∈ range Γ_ := by constructor · rw [mem_range_gamma, or_iff_not_imp_left] refine fun h ho ↦ (left_le_veblen ..).antisymm' ?_ conv_rhs => rw [← veblen_eq_of_lt_invVeblen₁ (h.trans_ne ho).bot_lt, bot_eq_zero, veblen_zero_apply, ← veblen_invVeblen₁_invVeblen₂, h] simp · aesop theorem invVeblen₁_lt_iff : invVeblen₁ o < o ↔ o ≠ 0 ∧ o ∉ range Γ_ := by rw [(invVeblen₁_le o).lt_iff_ne, ne_eq, invVeblen₁_eq_iff, not_or] end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/NaturalOps.lean
import Mathlib.SetTheory.Ordinal.Family import Mathlib.Tactic.Abel import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.NatOrdinal` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Natural operations on ordinals The goal of this file is to define natural addition and multiplication on ordinals, also known as the Hessenberg sum and product, and provide a basic API. The natural addition of two ordinals `a ♯ b` is recursively defined as the least ordinal greater than `a' ♯ b` and `a ♯ b'` for `a' < a` and `b' < b`. The natural multiplication `a ⨳ b` is likewise recursively defined as the least ordinal such that `a ⨳ b ♯ a' ⨳ b'` is greater than `a' ⨳ b ♯ a ⨳ b'` for any `a' < a` and `b' < b`. These operations form a rich algebraic structure: they're commutative, associative, preserve order, have the usual `0` and `1` from ordinals, and distribute over one another. Moreover, these operations are the addition and multiplication of ordinals when viewed as combinatorial `Game`s. This makes them particularly useful for game theory. Finally, both operations admit simple, intuitive descriptions in terms of the Cantor normal form. The natural addition of two ordinals corresponds to adding their Cantor normal forms as if they were polynomials in `ω`. Likewise, their natural multiplication corresponds to multiplying the Cantor normal forms as polynomials. ## Implementation notes Given the rich algebraic structure of these two operations, we choose to create a type synonym `NatOrdinal`, where we provide the appropriate instances. However, to avoid casting back and forth between both types, we attempt to prove and state most results on `Ordinal`. ## Todo - Prove the characterizations of natural addition and multiplication in terms of the Cantor normal form. -/ universe u v open Function Order Set noncomputable section /-! ### Basic casts between `Ordinal` and `NatOrdinal` -/ /-- A type synonym for ordinals with natural addition and multiplication. -/ def NatOrdinal : Type _ := Ordinal deriving Zero, Inhabited, One, WellFoundedRelation, Uncountable, LinearOrder, SuccOrder, OrderBot, NoMaxOrder, ZeroLEOneClass instance NatOrdinal.instNeZeroOne : NeZero (1 : NatOrdinal) := Ordinal.instNeZeroOne /-- The identity function between `Ordinal` and `NatOrdinal`. -/ @[match_pattern] def Ordinal.toNatOrdinal : Ordinal ≃o NatOrdinal := OrderIso.refl _ /-- The identity function between `NatOrdinal` and `Ordinal`. -/ @[match_pattern] def NatOrdinal.toOrdinal : NatOrdinal ≃o Ordinal := OrderIso.refl _ namespace NatOrdinal open Ordinal @[simp] theorem toOrdinal_symm_eq : NatOrdinal.toOrdinal.symm = Ordinal.toNatOrdinal := rfl @[simp] theorem toOrdinal_toNatOrdinal (a : NatOrdinal) : a.toOrdinal.toNatOrdinal = a := rfl theorem lt_wf : @WellFounded NatOrdinal (· < ·) := Ordinal.lt_wf instance : WellFoundedLT NatOrdinal := Ordinal.wellFoundedLT instance : ConditionallyCompleteLinearOrderBot NatOrdinal := WellFoundedLT.conditionallyCompleteLinearOrderBot _ @[simp] theorem bot_eq_zero : (⊥ : NatOrdinal) = 0 := rfl @[simp] theorem toOrdinal_zero : toOrdinal 0 = 0 := rfl @[simp] theorem toOrdinal_one : toOrdinal 1 = 1 := rfl @[simp] theorem toOrdinal_eq_zero {a} : toOrdinal a = 0 ↔ a = 0 := Iff.rfl @[simp] theorem toOrdinal_eq_one {a} : toOrdinal a = 1 ↔ a = 1 := Iff.rfl @[simp] theorem toOrdinal_max (a b : NatOrdinal) : toOrdinal (max a b) = max (toOrdinal a) (toOrdinal b) := rfl @[simp] theorem toOrdinal_min (a b : NatOrdinal) : toOrdinal (min a b) = min (toOrdinal a) (toOrdinal b) := rfl theorem succ_def (a : NatOrdinal) : succ a = toNatOrdinal (toOrdinal a + 1) := rfl @[simp] theorem zero_le (o : NatOrdinal) : 0 ≤ o := Ordinal.zero_le o theorem not_lt_zero (o : NatOrdinal) : ¬ o < 0 := Ordinal.not_lt_zero o @[simp] theorem lt_one_iff_zero {o : NatOrdinal} : o < 1 ↔ o = 0 := Ordinal.lt_one_iff_zero /-- A recursor for `NatOrdinal`. Use as `induction x`. -/ @[elab_as_elim, cases_eliminator, induction_eliminator] protected def rec {β : NatOrdinal → Sort*} (h : ∀ a, β (toNatOrdinal a)) : ∀ a, β a := fun a => h (toOrdinal a) /-- `Ordinal.induction` but for `NatOrdinal`. -/ theorem induction {p : NatOrdinal → Prop} : ∀ (i) (_ : ∀ j, (∀ k, k < j → p k) → p j), p i := Ordinal.induction instance small_Iio (a : NatOrdinal.{u}) : Small.{u} (Set.Iio a) := Ordinal.small_Iio a instance small_Iic (a : NatOrdinal.{u}) : Small.{u} (Set.Iic a) := Ordinal.small_Iic a instance small_Ico (a b : NatOrdinal.{u}) : Small.{u} (Set.Ico a b) := Ordinal.small_Ico a b instance small_Icc (a b : NatOrdinal.{u}) : Small.{u} (Set.Icc a b) := Ordinal.small_Icc a b instance small_Ioo (a b : NatOrdinal.{u}) : Small.{u} (Set.Ioo a b) := Ordinal.small_Ioo a b instance small_Ioc (a b : NatOrdinal.{u}) : Small.{u} (Set.Ioc a b) := Ordinal.small_Ioc a b end NatOrdinal namespace Ordinal variable {a b c : Ordinal.{u}} @[simp] theorem toNatOrdinal_symm_eq : toNatOrdinal.symm = NatOrdinal.toOrdinal := rfl @[simp] theorem toNatOrdinal_toOrdinal (a : Ordinal) : a.toNatOrdinal.toOrdinal = a := rfl @[simp] theorem toNatOrdinal_zero : toNatOrdinal 0 = 0 := rfl @[simp] theorem toNatOrdinal_one : toNatOrdinal 1 = 1 := rfl @[simp] theorem toNatOrdinal_eq_zero (a) : toNatOrdinal a = 0 ↔ a = 0 := Iff.rfl @[simp] theorem toNatOrdinal_eq_one (a) : toNatOrdinal a = 1 ↔ a = 1 := Iff.rfl @[simp] theorem toNatOrdinal_max (a b : Ordinal) : toNatOrdinal (max a b) = max (toNatOrdinal a) (toNatOrdinal b) := rfl @[simp] theorem toNatOrdinal_min (a b : Ordinal) : toNatOrdinal (min a b) = min (toNatOrdinal a) (toNatOrdinal b) := rfl /-! We place the definitions of `nadd` and `nmul` before actually developing their API, as this guarantees we only need to open the `NaturalOps` scope once. -/ /-- Natural addition on ordinals `a ♯ b`, also known as the Hessenberg sum, is recursively defined as the least ordinal greater than `a' ♯ b` and `a ♯ b'` for all `a' < a` and `b' < b`. In contrast to normal ordinal addition, it is commutative. Natural addition can equivalently be characterized as the ordinal resulting from adding up corresponding coefficients in the Cantor normal forms of `a` and `b`. -/ noncomputable def nadd (a b : Ordinal.{u}) : Ordinal.{u} := max (⨆ x : Iio a, succ (nadd x.1 b)) (⨆ x : Iio b, succ (nadd a x.1)) termination_by (a, b) decreasing_by all_goals cases x; decreasing_tactic @[inherit_doc] scoped[NaturalOps] infixl:65 " ♯ " => Ordinal.nadd open NaturalOps /-- Natural multiplication on ordinals `a ⨳ b`, also known as the Hessenberg product, is recursively defined as the least ordinal such that `a ⨳ b ♯ a' ⨳ b'` is greater than `a' ⨳ b ♯ a ⨳ b'` for all `a' < a` and `b < b'`. In contrast to normal ordinal multiplication, it is commutative and distributive (over natural addition). Natural multiplication can equivalently be characterized as the ordinal resulting from multiplying the Cantor normal forms of `a` and `b` as if they were polynomials in `ω`. Addition of exponents is done via natural addition. -/ noncomputable def nmul (a b : Ordinal.{u}) : Ordinal.{u} := sInf {c | ∀ a' < a, ∀ b' < b, nmul a' b ♯ nmul a b' < c ♯ nmul a' b'} termination_by (a, b) @[inherit_doc] scoped[NaturalOps] infixl:70 " ⨳ " => Ordinal.nmul /-! ### Natural addition -/ theorem lt_nadd_iff : a < b ♯ c ↔ (∃ b' < b, a ≤ b' ♯ c) ∨ ∃ c' < c, a ≤ b ♯ c' := by rw [nadd] simp [Ordinal.lt_iSup_iff] theorem nadd_le_iff : b ♯ c ≤ a ↔ (∀ b' < b, b' ♯ c < a) ∧ ∀ c' < c, b ♯ c' < a := by rw [← not_lt, lt_nadd_iff] simp theorem nadd_lt_nadd_left (h : b < c) (a) : a ♯ b < a ♯ c := lt_nadd_iff.2 (Or.inr ⟨b, h, le_rfl⟩) theorem nadd_lt_nadd_right (h : b < c) (a) : b ♯ a < c ♯ a := lt_nadd_iff.2 (Or.inl ⟨b, h, le_rfl⟩) theorem nadd_le_nadd_left (h : b ≤ c) (a) : a ♯ b ≤ a ♯ c := by rcases lt_or_eq_of_le h with (h | rfl) · exact (nadd_lt_nadd_left h a).le · exact le_rfl theorem nadd_le_nadd_right (h : b ≤ c) (a) : b ♯ a ≤ c ♯ a := by rcases lt_or_eq_of_le h with (h | rfl) · exact (nadd_lt_nadd_right h a).le · exact le_rfl variable (a b) theorem nadd_comm (a b) : a ♯ b = b ♯ a := by rw [nadd, nadd, max_comm] congr <;> ext x <;> cases x <;> apply congr_arg _ (nadd_comm _ _) termination_by (a, b) private theorem iSup_nadd_of_monotone {a b} (f : Ordinal.{u} → Ordinal.{u}) (h : Monotone f) : ⨆ x : Iio (a ♯ b), f x = max (⨆ a' : Iio a, f (a'.1 ♯ b)) (⨆ b' : Iio b, f (a ♯ b'.1)) := by apply (max_le _ _).antisymm' · rw [Ordinal.iSup_le_iff] rintro ⟨i, hi⟩ obtain ⟨x, hx, hi⟩ | ⟨x, hx, hi⟩ := lt_nadd_iff.1 hi · exact le_max_of_le_left ((h hi).trans <| Ordinal.le_iSup (fun x : Iio a ↦ _) ⟨x, hx⟩) · exact le_max_of_le_right ((h hi).trans <| Ordinal.le_iSup (fun x : Iio b ↦ _) ⟨x, hx⟩) all_goals apply csSup_le_csSup' (bddAbove_of_small _) rintro _ ⟨⟨c, hc⟩, rfl⟩ refine mem_range_self (⟨_, ?_⟩ : Iio _) apply_rules [nadd_lt_nadd_left, nadd_lt_nadd_right] theorem nadd_assoc (a b c) : a ♯ b ♯ c = a ♯ (b ♯ c) := by unfold nadd rw [iSup_nadd_of_monotone fun a' ↦ succ (a' ♯ c), iSup_nadd_of_monotone fun b' ↦ succ (a ♯ b'), max_assoc] · congr <;> ext x <;> cases x <;> apply congr_arg _ (nadd_assoc _ _ _) · exact succ_mono.comp fun x y h ↦ nadd_le_nadd_left h _ · exact succ_mono.comp fun x y h ↦ nadd_le_nadd_right h _ termination_by (a, b, c) @[simp] theorem nadd_zero (a : Ordinal) : a ♯ 0 = a := by rw [nadd, ciSup_of_empty fun _ : Iio 0 ↦ _, sup_bot_eq] convert _root_.iSup_succ a rename_i x cases x exact nadd_zero _ termination_by a @[simp] theorem zero_nadd : 0 ♯ a = a := by rw [nadd_comm, nadd_zero] @[simp] theorem nadd_one (a : Ordinal) : a ♯ 1 = succ a := by rw [nadd, ciSup_unique (s := fun _ : Iio 1 ↦ _), Iio_one_default_eq, nadd_zero, max_eq_right_iff, Ordinal.iSup_le_iff] rintro ⟨i, hi⟩ rwa [nadd_one, succ_le_succ_iff, succ_le_iff] termination_by a @[simp] theorem one_nadd : 1 ♯ a = succ a := by rw [nadd_comm, nadd_one] theorem nadd_succ : a ♯ succ b = succ (a ♯ b) := by rw [← nadd_one (a ♯ b), nadd_assoc, nadd_one] theorem succ_nadd : succ a ♯ b = succ (a ♯ b) := by rw [← one_nadd (a ♯ b), ← nadd_assoc, one_nadd] @[simp] theorem nadd_nat (n : ℕ) : a ♯ n = a + n := by induction n with | zero => simp | succ n hn => rw [Nat.cast_succ, add_one_eq_succ, nadd_succ, add_succ, hn] @[simp] theorem nat_nadd (n : ℕ) : ↑n ♯ a = a + n := by rw [nadd_comm, nadd_nat] theorem add_le_nadd : a + b ≤ a ♯ b := by induction b using limitRecOn with | zero => simp | succ c h => rwa [add_succ, nadd_succ, succ_le_succ_iff] | limit c hc H => rw [(isNormal_add_right a).apply_of_isSuccLimit hc, Ordinal.iSup_le_iff] rintro ⟨i, hi⟩ exact (H i hi).trans (nadd_le_nadd_left hi.le a) end Ordinal namespace NatOrdinal open Ordinal NaturalOps instance : Add NatOrdinal := ⟨nadd⟩ instance : SuccAddOrder NatOrdinal := ⟨fun x => (nadd_one x).symm⟩ theorem lt_add_iff {a b c : NatOrdinal} : a < b + c ↔ (∃ b' < b, a ≤ b' + c) ∨ ∃ c' < c, a ≤ b + c' := Ordinal.lt_nadd_iff theorem add_le_iff {a b c : NatOrdinal} : b + c ≤ a ↔ (∀ b' < b, b' + c < a) ∧ ∀ c' < c, b + c' < a := Ordinal.nadd_le_iff instance : AddLeftStrictMono NatOrdinal.{u} := ⟨fun a _ _ h => nadd_lt_nadd_left h a⟩ instance : AddLeftMono NatOrdinal.{u} := ⟨fun a _ _ h => nadd_le_nadd_left h a⟩ instance : AddLeftReflectLE NatOrdinal.{u} := ⟨fun a b c h => by by_contra! h'; exact h.not_gt (by gcongr)⟩ instance : AddCommMonoid NatOrdinal := { add_assoc := nadd_assoc zero_add := zero_nadd add_zero := nadd_zero add_comm := nadd_comm nsmul := nsmulRec } instance : IsOrderedCancelAddMonoid NatOrdinal := { add_le_add_left := fun _ _ => add_le_add_left le_of_add_le_add_left := fun _ _ _ => le_of_add_le_add_left } instance : AddMonoidWithOne NatOrdinal := AddMonoidWithOne.unary @[simp] theorem toOrdinal_natCast (n : ℕ) : toOrdinal n = n := by induction n with | zero => rfl | succ n hn => change (toOrdinal n) ♯ 1 = n + 1 rw [hn]; exact nadd_one n instance : CharZero NatOrdinal where cast_injective m n h := by apply_fun toOrdinal at h simpa using h end NatOrdinal open NatOrdinal open NaturalOps namespace Ordinal theorem nadd_eq_add (a b : Ordinal) : a ♯ b = toOrdinal (toNatOrdinal a + toNatOrdinal b) := rfl @[simp] theorem toNatOrdinal_natCast (n : ℕ) : toNatOrdinal n = n := by rw [← toOrdinal_natCast n] rfl theorem lt_of_nadd_lt_nadd_left : ∀ {a b c}, a ♯ b < a ♯ c → b < c := @lt_of_add_lt_add_left NatOrdinal _ _ _ theorem lt_of_nadd_lt_nadd_right : ∀ {a b c}, b ♯ a < c ♯ a → b < c := @lt_of_add_lt_add_right NatOrdinal _ _ _ theorem le_of_nadd_le_nadd_left : ∀ {a b c}, a ♯ b ≤ a ♯ c → b ≤ c := @le_of_add_le_add_left NatOrdinal _ _ _ theorem le_of_nadd_le_nadd_right : ∀ {a b c}, b ♯ a ≤ c ♯ a → b ≤ c := @le_of_add_le_add_right NatOrdinal _ _ _ @[simp] theorem nadd_lt_nadd_iff_left : ∀ (a) {b c}, a ♯ b < a ♯ c ↔ b < c := @add_lt_add_iff_left NatOrdinal _ _ _ _ @[simp] theorem nadd_lt_nadd_iff_right : ∀ (a) {b c}, b ♯ a < c ♯ a ↔ b < c := @add_lt_add_iff_right NatOrdinal _ _ _ _ @[simp] theorem nadd_le_nadd_iff_left : ∀ (a) {b c}, a ♯ b ≤ a ♯ c ↔ b ≤ c := @add_le_add_iff_left NatOrdinal _ _ _ _ @[simp] theorem nadd_le_nadd_iff_right : ∀ (a) {b c}, b ♯ a ≤ c ♯ a ↔ b ≤ c := @_root_.add_le_add_iff_right NatOrdinal _ _ _ _ theorem nadd_le_nadd : ∀ {a b c d}, a ≤ b → c ≤ d → a ♯ c ≤ b ♯ d := @add_le_add NatOrdinal _ _ _ _ theorem nadd_lt_nadd : ∀ {a b c d}, a < b → c < d → a ♯ c < b ♯ d := @add_lt_add NatOrdinal _ _ _ _ theorem nadd_lt_nadd_of_lt_of_le : ∀ {a b c d}, a < b → c ≤ d → a ♯ c < b ♯ d := @add_lt_add_of_lt_of_le NatOrdinal _ _ _ _ theorem nadd_lt_nadd_of_le_of_lt : ∀ {a b c d}, a ≤ b → c < d → a ♯ c < b ♯ d := @add_lt_add_of_le_of_lt NatOrdinal _ _ _ _ theorem nadd_left_cancel : ∀ {a b c}, a ♯ b = a ♯ c → b = c := @_root_.add_left_cancel NatOrdinal _ _ theorem nadd_right_cancel : ∀ {a b c}, a ♯ b = c ♯ b → a = c := @_root_.add_right_cancel NatOrdinal _ _ @[simp] theorem nadd_left_cancel_iff : ∀ {a b c}, a ♯ b = a ♯ c ↔ b = c := @add_left_cancel_iff NatOrdinal _ _ @[simp] theorem nadd_right_cancel_iff : ∀ {a b c}, b ♯ a = c ♯ a ↔ b = c := @add_right_cancel_iff NatOrdinal _ _ theorem le_nadd_self {a b} : a ≤ b ♯ a := by simpa using nadd_le_nadd_right (Ordinal.zero_le b) a theorem le_nadd_left {a b c} (h : a ≤ c) : a ≤ b ♯ c := le_nadd_self.trans (nadd_le_nadd_left h b) theorem le_self_nadd {a b} : a ≤ a ♯ b := by simpa using nadd_le_nadd_left (Ordinal.zero_le b) a theorem le_nadd_right {a b c} (h : a ≤ b) : a ≤ b ♯ c := le_self_nadd.trans (nadd_le_nadd_right h c) theorem nadd_left_comm : ∀ a b c, a ♯ (b ♯ c) = b ♯ (a ♯ c) := @add_left_comm NatOrdinal _ theorem nadd_right_comm : ∀ a b c, a ♯ b ♯ c = a ♯ c ♯ b := @add_right_comm NatOrdinal _ /-! ### Natural multiplication -/ variable {a b c d : Ordinal.{u}} /-- The set in the definition of `nmul` is nonempty. -/ private theorem nmul_nonempty (a b : Ordinal.{u}) : {c : Ordinal.{u} | ∀ a' < a, ∀ b' < b, a' ⨳ b ♯ a ⨳ b' < c ♯ a' ⨳ b'}.Nonempty := by obtain ⟨c, hc⟩ : BddAbove ((fun x ↦ x.1 ⨳ b ♯ a ⨳ x.2) '' Set.Iio a ×ˢ Set.Iio b) := bddAbove_of_small _ exact ⟨_, fun x hx y hy ↦ (lt_succ_of_le <| hc <| Set.mem_image_of_mem _ <| Set.mk_mem_prod hx hy).trans_le le_self_nadd⟩ theorem nmul_nadd_lt {a' b' : Ordinal} (ha : a' < a) (hb : b' < b) : a' ⨳ b ♯ a ⨳ b' < a ⨳ b ♯ a' ⨳ b' := by conv_rhs => rw [nmul] exact csInf_mem (nmul_nonempty a b) a' ha b' hb theorem nmul_nadd_le {a' b' : Ordinal} (ha : a' ≤ a) (hb : b' ≤ b) : a' ⨳ b ♯ a ⨳ b' ≤ a ⨳ b ♯ a' ⨳ b' := by rcases lt_or_eq_of_le ha with (ha | rfl) · rcases lt_or_eq_of_le hb with (hb | rfl) · exact (nmul_nadd_lt ha hb).le · rw [nadd_comm] · exact le_rfl theorem lt_nmul_iff : c < a ⨳ b ↔ ∃ a' < a, ∃ b' < b, c ♯ a' ⨳ b' ≤ a' ⨳ b ♯ a ⨳ b' := by refine ⟨fun h => ?_, ?_⟩ · rw [nmul] at h simpa using notMem_of_lt_csInf h ⟨0, fun _ _ => bot_le⟩ · rintro ⟨a', ha, b', hb, h⟩ have := h.trans_lt (nmul_nadd_lt ha hb) rwa [nadd_lt_nadd_iff_right] at this theorem nmul_le_iff : a ⨳ b ≤ c ↔ ∀ a' < a, ∀ b' < b, a' ⨳ b ♯ a ⨳ b' < c ♯ a' ⨳ b' := by rw [← not_iff_not]; simp [lt_nmul_iff] theorem nmul_comm (a b) : a ⨳ b = b ⨳ a := by rw [nmul, nmul] congr; ext x; constructor <;> intro H c hc d hd · rw [nadd_comm, ← nmul_comm, ← nmul_comm a, ← nmul_comm d] exact H _ hd _ hc · rw [nadd_comm, nmul_comm, nmul_comm c, nmul_comm c] exact H _ hd _ hc termination_by (a, b) @[simp] theorem nmul_zero (a) : a ⨳ 0 = 0 := by rw [← Ordinal.le_zero, nmul_le_iff] exact fun _ _ a ha => (Ordinal.not_lt_zero a ha).elim @[simp] theorem zero_nmul (a) : 0 ⨳ a = 0 := by rw [nmul_comm, nmul_zero] @[simp] theorem nmul_one (a : Ordinal) : a ⨳ 1 = a := by rw [nmul] convert csInf_Ici ext b refine ⟨fun H ↦ le_of_forall_lt (a := a) fun c hc ↦ ?_, fun ha c hc ↦ ?_⟩ · simpa [nmul_one c] using H c hc · simpa [nmul_one c] using hc.trans_le ha termination_by a @[simp] theorem one_nmul (a) : 1 ⨳ a = a := by rw [nmul_comm, nmul_one] theorem nmul_lt_nmul_of_pos_left (h₁ : a < b) (h₂ : 0 < c) : c ⨳ a < c ⨳ b := lt_nmul_iff.2 ⟨0, h₂, a, h₁, by simp⟩ theorem nmul_lt_nmul_of_pos_right (h₁ : a < b) (h₂ : 0 < c) : a ⨳ c < b ⨳ c := lt_nmul_iff.2 ⟨a, h₁, 0, h₂, by simp⟩ @[gcongr] theorem nmul_le_nmul_left (h : a ≤ b) (c) : c ⨳ a ≤ c ⨳ b := by rcases lt_or_eq_of_le h with (h₁ | rfl) <;> rcases (eq_zero_or_pos c).symm with (h₂ | rfl) · exact (nmul_lt_nmul_of_pos_left h₁ h₂).le all_goals simp @[gcongr] theorem nmul_le_nmul_right (h : a ≤ b) (c) : a ⨳ c ≤ b ⨳ c := by rw [nmul_comm, nmul_comm b] exact nmul_le_nmul_left h c theorem nmul_nadd (a b c : Ordinal) : a ⨳ (b ♯ c) = a ⨳ b ♯ a ⨳ c := by refine le_antisymm (nmul_le_iff.2 fun a' ha d hd => ?_) (nadd_le_iff.2 ⟨fun d hd => ?_, fun d hd => ?_⟩) · rw [nmul_nadd] rcases lt_nadd_iff.1 hd with (⟨b', hb, hd⟩ | ⟨c', hc, hd⟩) · have := nadd_lt_nadd_of_lt_of_le (nmul_nadd_lt ha hb) (nmul_nadd_le ha.le hd) rw [nmul_nadd, nmul_nadd] at this simp only [nadd_assoc] at this rwa [nadd_left_comm, nadd_left_comm _ (a ⨳ b'), nadd_left_comm (a ⨳ b), nadd_lt_nadd_iff_left, nadd_left_comm (a' ⨳ b), nadd_left_comm (a ⨳ b), nadd_lt_nadd_iff_left, ← nadd_assoc, ← nadd_assoc] at this · have := nadd_lt_nadd_of_le_of_lt (nmul_nadd_le ha.le hd) (nmul_nadd_lt ha hc) rw [nmul_nadd, nmul_nadd] at this simp only [nadd_assoc] at this rwa [nadd_left_comm, nadd_comm (a ⨳ c), nadd_left_comm (a' ⨳ d), nadd_left_comm (a ⨳ c'), nadd_left_comm (a ⨳ b), nadd_lt_nadd_iff_left, nadd_comm (a' ⨳ c), nadd_left_comm (a ⨳ d), nadd_left_comm (a' ⨳ b), nadd_left_comm (a ⨳ b), nadd_lt_nadd_iff_left, nadd_comm (a ⨳ d), nadd_comm (a' ⨳ d), ← nadd_assoc, ← nadd_assoc] at this · rcases lt_nmul_iff.1 hd with ⟨a', ha, b', hb, hd⟩ have := nadd_lt_nadd_of_le_of_lt hd (nmul_nadd_lt ha (nadd_lt_nadd_right hb c)) rw [nmul_nadd, nmul_nadd, nmul_nadd a'] at this simp only [nadd_assoc] at this rwa [nadd_left_comm (a' ⨳ b'), nadd_left_comm, nadd_lt_nadd_iff_left, nadd_left_comm, nadd_left_comm _ (a' ⨳ b'), nadd_left_comm (a ⨳ b'), nadd_lt_nadd_iff_left, nadd_left_comm (a' ⨳ c), nadd_left_comm, nadd_lt_nadd_iff_left, nadd_left_comm, nadd_comm _ (a' ⨳ c), nadd_lt_nadd_iff_left] at this · rcases lt_nmul_iff.1 hd with ⟨a', ha, c', hc, hd⟩ have := nadd_lt_nadd_of_lt_of_le (nmul_nadd_lt ha (nadd_lt_nadd_left hc b)) hd rw [nmul_nadd, nmul_nadd, nmul_nadd a'] at this simp only [nadd_assoc] at this rwa [nadd_left_comm _ (a' ⨳ b), nadd_lt_nadd_iff_left, nadd_left_comm (a' ⨳ c'), nadd_left_comm _ (a' ⨳ c), nadd_lt_nadd_iff_left, nadd_left_comm, nadd_comm (a' ⨳ c'), nadd_left_comm _ (a ⨳ c'), nadd_lt_nadd_iff_left, nadd_comm _ (a' ⨳ c'), nadd_comm _ (a' ⨳ c'), nadd_left_comm, nadd_lt_nadd_iff_left] at this termination_by (a, b, c) theorem nadd_nmul (a b c) : (a ♯ b) ⨳ c = a ⨳ c ♯ b ⨳ c := by rw [nmul_comm, nmul_nadd, nmul_comm, nmul_comm c] theorem nmul_nadd_lt₃ {a' b' c' : Ordinal} (ha : a' < a) (hb : b' < b) (hc : c' < c) : a' ⨳ b ⨳ c ♯ a ⨳ b' ⨳ c ♯ a ⨳ b ⨳ c' ♯ a' ⨳ b' ⨳ c' < a ⨳ b ⨳ c ♯ a' ⨳ b' ⨳ c ♯ a' ⨳ b ⨳ c' ♯ a ⨳ b' ⨳ c' := by simpa only [nadd_nmul, ← nadd_assoc] using nmul_nadd_lt (nmul_nadd_lt ha hb) hc theorem nmul_nadd_le₃ {a' b' c' : Ordinal} (ha : a' ≤ a) (hb : b' ≤ b) (hc : c' ≤ c) : a' ⨳ b ⨳ c ♯ a ⨳ b' ⨳ c ♯ a ⨳ b ⨳ c' ♯ a' ⨳ b' ⨳ c' ≤ a ⨳ b ⨳ c ♯ a' ⨳ b' ⨳ c ♯ a' ⨳ b ⨳ c' ♯ a ⨳ b' ⨳ c' := by simpa only [nadd_nmul, ← nadd_assoc] using nmul_nadd_le (nmul_nadd_le ha hb) hc private theorem nmul_nadd_lt₃' {a' b' c' : Ordinal} (ha : a' < a) (hb : b' < b) (hc : c' < c) : a' ⨳ (b ⨳ c) ♯ a ⨳ (b' ⨳ c) ♯ a ⨳ (b ⨳ c') ♯ a' ⨳ (b' ⨳ c') < a ⨳ (b ⨳ c) ♯ a' ⨳ (b' ⨳ c) ♯ a' ⨳ (b ⨳ c') ♯ a ⨳ (b' ⨳ c') := by simp only [nmul_comm _ (_ ⨳ _)] convert nmul_nadd_lt₃ hb hc ha using 1 <;> (simp only [nadd_eq_add, NatOrdinal.toOrdinal_toNatOrdinal]; abel_nf) theorem lt_nmul_iff₃ : d < a ⨳ b ⨳ c ↔ ∃ a' < a, ∃ b' < b, ∃ c' < c, d ♯ a' ⨳ b' ⨳ c ♯ a' ⨳ b ⨳ c' ♯ a ⨳ b' ⨳ c' ≤ a' ⨳ b ⨳ c ♯ a ⨳ b' ⨳ c ♯ a ⨳ b ⨳ c' ♯ a' ⨳ b' ⨳ c' := by refine ⟨fun h ↦ ?_, fun ⟨a', ha, b', hb, c', hc, h⟩ ↦ ?_⟩ · rcases lt_nmul_iff.1 h with ⟨e, he, c', hc, H₁⟩ rcases lt_nmul_iff.1 he with ⟨a', ha, b', hb, H₂⟩ refine ⟨a', ha, b', hb, c', hc, ?_⟩ have := nadd_le_nadd H₁ (nmul_nadd_le H₂ hc.le) simp only [nadd_nmul, nadd_assoc] at this rw [nadd_left_comm, nadd_left_comm d, nadd_left_comm, nadd_le_nadd_iff_left, nadd_left_comm (a ⨳ b' ⨳ c), nadd_left_comm (a' ⨳ b ⨳ c), nadd_left_comm (a ⨳ b ⨳ c'), nadd_le_nadd_iff_left, nadd_left_comm (a ⨳ b ⨳ c'), nadd_left_comm (a ⨳ b ⨳ c')] at this simpa only [nadd_assoc] · have := h.trans_lt (nmul_nadd_lt₃ ha hb hc) repeat rw [nadd_lt_nadd_iff_right] at this assumption theorem nmul_le_iff₃ : a ⨳ b ⨳ c ≤ d ↔ ∀ a' < a, ∀ b' < b, ∀ c' < c, a' ⨳ b ⨳ c ♯ a ⨳ b' ⨳ c ♯ a ⨳ b ⨳ c' ♯ a' ⨳ b' ⨳ c' < d ♯ a' ⨳ b' ⨳ c ♯ a' ⨳ b ⨳ c' ♯ a ⨳ b' ⨳ c' := by simpa using lt_nmul_iff₃.not private theorem nmul_le_iff₃' : a ⨳ (b ⨳ c) ≤ d ↔ ∀ a' < a, ∀ b' < b, ∀ c' < c, a' ⨳ (b ⨳ c) ♯ a ⨳ (b' ⨳ c) ♯ a ⨳ (b ⨳ c') ♯ a' ⨳ (b' ⨳ c') < d ♯ a' ⨳ (b' ⨳ c) ♯ a' ⨳ (b ⨳ c') ♯ a ⨳ (b' ⨳ c') := by simp only [nmul_comm _ (_ ⨳ _), nmul_le_iff₃, nadd_eq_add, toOrdinal_toNatOrdinal] constructor <;> intro h a' ha b' hb c' hc · convert h b' hb c' hc a' ha using 1 <;> abel_nf · convert h c' hc a' ha b' hb using 1 <;> abel_nf theorem nmul_assoc (a b c : Ordinal) : a ⨳ b ⨳ c = a ⨳ (b ⨳ c) := by apply le_antisymm · rw [nmul_le_iff₃] intro a' ha b' hb c' hc repeat rw [nmul_assoc] exact nmul_nadd_lt₃' ha hb hc · rw [nmul_le_iff₃'] intro a' ha b' hb c' hc repeat rw [← nmul_assoc] exact nmul_nadd_lt₃ ha hb hc termination_by (a, b, c) end Ordinal namespace NatOrdinal open Ordinal instance : Mul NatOrdinal := ⟨nmul⟩ theorem lt_mul_iff {a b c : NatOrdinal} : c < a * b ↔ ∃ a' < a, ∃ b' < b, c + a' * b' ≤ a' * b + a * b' := Ordinal.lt_nmul_iff theorem mul_le_iff {a b c : NatOrdinal} : a * b ≤ c ↔ ∀ a' < a, ∀ b' < b, a' * b + a * b' < c + a' * b' := Ordinal.nmul_le_iff theorem mul_add_lt {a b a' b' : NatOrdinal} (ha : a' < a) (hb : b' < b) : a' * b + a * b' < a * b + a' * b' := Ordinal.nmul_nadd_lt ha hb theorem nmul_nadd_le {a b a' b' : NatOrdinal} (ha : a' ≤ a) (hb : b' ≤ b) : a' * b + a * b' ≤ a * b + a' * b' := Ordinal.nmul_nadd_le ha hb instance : CommSemiring NatOrdinal := { NatOrdinal.instAddCommMonoid with left_distrib := nmul_nadd right_distrib := nadd_nmul zero_mul := zero_nmul mul_zero := nmul_zero mul_assoc := nmul_assoc one_mul := one_nmul mul_one := nmul_one mul_comm := nmul_comm } instance : IsOrderedMonoid NatOrdinal where mul_le_mul_left _a _b hab _c := nmul_le_nmul_left hab _ mul_le_mul_right _a _b hab _c := nmul_le_nmul_right hab _ instance : IsOrderedRing NatOrdinal where end NatOrdinal namespace Ordinal theorem nmul_eq_mul (a b) : a ⨳ b = toOrdinal (toNatOrdinal a * toNatOrdinal b) := rfl theorem nmul_nadd_one : ∀ a b, a ⨳ (b ♯ 1) = a ⨳ b ♯ a := @mul_add_one NatOrdinal _ _ _ theorem nadd_one_nmul : ∀ a b, (a ♯ 1) ⨳ b = a ⨳ b ♯ b := @add_one_mul NatOrdinal _ _ _ theorem nmul_succ (a b) : a ⨳ succ b = a ⨳ b ♯ a := by rw [← nadd_one, nmul_nadd_one] theorem succ_nmul (a b) : succ a ⨳ b = a ⨳ b ♯ b := by rw [← nadd_one, nadd_one_nmul] theorem nmul_add_one : ∀ a b, a ⨳ (b + 1) = a ⨳ b ♯ a := nmul_succ theorem add_one_nmul : ∀ a b, (a + 1) ⨳ b = a ⨳ b ♯ b := succ_nmul theorem mul_le_nmul (a b : Ordinal.{u}) : a * b ≤ a ⨳ b := by refine b.limitRecOn ?_ ?_ ?_ · simp · intro c h rw [mul_succ, nmul_succ] exact (add_le_nadd _ a).trans (nadd_le_nadd_right h a) · intro c hc H rcases eq_zero_or_pos a with (rfl | ha) · simp · rw [(isNormal_mul_right ha).apply_of_isSuccLimit hc, Ordinal.iSup_le_iff] rintro ⟨i, (hi : i < c)⟩ grw [H i hi, hi] end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Rank.lean
import Mathlib.SetTheory.Ordinal.Family /-! # Rank in a well-founded relation For `r` a well-founded relation, `IsWellFounded.rank r a` is recursively defined as the least ordinal greater than the ranks of all elements below `a`. -/ universe u variable {α : Type u} {a b : α} /-! ### Rank of an accessible value -/ namespace Acc variable {r : α → α → Prop} /-- The rank of an element `a` accessible under a relation `r` is defined recursively as the smallest ordinal greater than the ranks of all elements below it (i.e. elements `b` such that `r b a`). -/ noncomputable def rank (h : Acc r a) : Ordinal.{u} := Acc.recOn h fun a _h ih => ⨆ b : { b // r b a }, Order.succ (ih b b.2) theorem rank_eq (h : Acc r a) : h.rank = ⨆ b : { b // r b a }, Order.succ (h.inv b.2).rank := by change (Acc.intro a fun _ => h.inv).rank = _ rfl /-- if `r a b` then the rank of `a` is less than the rank of `b`. -/ theorem rank_lt_of_rel (hb : Acc r b) (h : r a b) : (hb.inv h).rank < hb.rank := (Order.lt_succ _).trans_le <| by rw [hb.rank_eq] exact Ordinal.le_iSup _ (⟨a, h⟩ : {a // r a b}) theorem mem_range_rank_of_le {o : Ordinal} (ha : Acc r a) (ho : o ≤ ha.rank) : ∃ (b : α) (hb : Acc r b), hb.rank = o := by obtain rfl | ho := ho.eq_or_lt · exact ⟨a, ha, rfl⟩ · revert ho refine ha.recOn fun a ha IH ho ↦ ?_ rw [rank_eq, Ordinal.lt_iSup_iff] at ho obtain ⟨⟨b, hb⟩, ho⟩ := ho rw [Order.lt_succ_iff] at ho obtain rfl | ho := ho.eq_or_lt exacts [⟨b, ha b hb, rfl⟩, IH _ hb ho] end Acc /-! ### Rank in a well-founded relation -/ namespace IsWellFounded variable (r : α → α → Prop) [hwf : IsWellFounded α r] /-- The rank of an element `a` under a well-founded relation `r` is defined recursively as the smallest ordinal greater than the ranks of all elements below it (i.e. elements `b` such that `r b a`). -/ noncomputable def rank (a : α) : Ordinal.{u} := (hwf.apply r a).rank theorem rank_eq (a : α) : rank r a = ⨆ b : { b // r b a }, Order.succ (rank r b) := (hwf.apply r a).rank_eq variable {r : α → α → Prop} [hwf : IsWellFounded α r] theorem rank_lt_of_rel (h : r a b) : rank r a < rank r b := Acc.rank_lt_of_rel _ h theorem mem_range_rank_of_le {o : Ordinal} (h : o ≤ rank r a) : o ∈ Set.range (rank r) := by obtain ⟨b, hb, rfl⟩ := Acc.mem_range_rank_of_le (hwf.apply r a) h exact ⟨b, rfl⟩ end IsWellFounded theorem WellFoundedLT.rank_strictMono [Preorder α] [WellFoundedLT α] : StrictMono (IsWellFounded.rank (α := α) (· < ·)) := fun _ _ => IsWellFounded.rank_lt_of_rel theorem WellFoundedGT.rank_strictAnti [Preorder α] [WellFoundedGT α] : StrictAnti (IsWellFounded.rank (α := α) (· > ·)) := fun _ _ a => IsWellFounded.rank_lt_of_rel a @[simp] theorem IsWellFounded.rank_eq_typein (r) [IsWellOrder α r] : rank r = Ordinal.typein r := by classical letI := linearOrderOfSTO r ext a exact InitialSeg.eq (⟨(OrderEmbedding.ofStrictMono _ WellFoundedLT.rank_strictMono).ltEmbedding, fun a b h ↦ mem_range_rank_of_le h.le⟩) (Ordinal.typein r) a
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Topology.lean
import Mathlib.SetTheory.Ordinal.Enum import Mathlib.Tactic.TFAE import Mathlib.Topology.Order.IsNormal import Mathlib.Topology.Order.Monotone /-! ### Topology of ordinals We prove some miscellaneous results involving the order topology of ordinals. ### Main results * `Ordinal.isClosed_iff_iSup` / `Ordinal.isClosed_iff_bsup`: A set of ordinals is closed iff it's closed under suprema. * `Ordinal.isNormal_iff_strictMono_and_continuous`: A characterization of normal ordinal functions. * `Ordinal.enumOrd_isNormal_iff_isClosed`: The function enumerating the ordinals of a set is normal iff the set is closed. -/ noncomputable section universe u v open Cardinal Order Topology namespace Ordinal variable {s : Set Ordinal.{u}} {a : Ordinal.{u}} instance : TopologicalSpace Ordinal.{u} := Preorder.topology Ordinal.{u} instance : OrderTopology Ordinal.{u} := ⟨rfl⟩ -- todo: generalize to other well-orders theorem isOpen_singleton_iff : IsOpen ({a} : Set Ordinal) ↔ ¬ IsSuccLimit a := by refine ⟨fun h ha => ?_, fun ha => ?_⟩ · obtain ⟨b, c, hbc, hbc'⟩ := (mem_nhds_iff_exists_Ioo_subset' ⟨0, ha.bot_lt⟩ ⟨_, lt_succ a⟩).1 (h.mem_nhds rfl) have hba := ha.succ_lt hbc.1 exact hba.ne (hbc' ⟨lt_succ b, hba.trans hbc.2⟩) · rcases zero_or_succ_or_isSuccLimit a with (rfl | ⟨b, rfl⟩ | ha') · rw [← bot_eq_zero, ← Set.Iic_bot, ← Iio_succ] exact isOpen_Iio · rw [← Set.Icc_self, Icc_succ_left, ← Ioo_succ_right] exact isOpen_Ioo · exact (ha ha').elim -- todo: generalize to a `SuccOrder` theorem nhds_eq_pure : 𝓝 a = pure a ↔ ¬ IsSuccLimit a := (isOpen_singleton_iff_nhds_eq_pure _).symm.trans isOpen_singleton_iff -- todo: generalize this lemma to a `SuccOrder` theorem isOpen_iff : IsOpen s ↔ ∀ o ∈ s, IsSuccLimit o → ∃ a < o, Set.Ioo a o ⊆ s := by refine isOpen_iff_mem_nhds.trans <| forall₂_congr fun o ho => ?_ by_cases ho' : IsSuccLimit o · simp only [(SuccOrder.hasBasis_nhds_Ioc_of_exists_lt ⟨0, ho'.bot_lt⟩).mem_iff, ho', true_implies] refine exists_congr fun a => and_congr_right fun ha => ?_ simp only [← Set.Ioo_insert_right ha, Set.insert_subset_iff, ho, true_and] · simp [nhds_eq_pure.2 ho', ho, ho'] open List Set in theorem mem_closure_tfae (a : Ordinal.{u}) (s : Set Ordinal) : TFAE [a ∈ closure s, a ∈ closure (s ∩ Iic a), (s ∩ Iic a).Nonempty ∧ sSup (s ∩ Iic a) = a, ∃ t, t ⊆ s ∧ t.Nonempty ∧ BddAbove t ∧ sSup t = a, ∃ (o : Ordinal.{u}), o ≠ 0 ∧ ∃ (f : ∀ x < o, Ordinal), (∀ x hx, f x hx ∈ s) ∧ bsup.{u, u} o f = a, ∃ (ι : Type u), Nonempty ι ∧ ∃ f : ι → Ordinal, (∀ i, f i ∈ s) ∧ ⨆ i, f i = a] := by tfae_have 1 → 2 := by simpa only [mem_closure_iff_nhdsWithin_neBot, inter_comm s, nhdsWithin_inter', SuccOrder.nhdsLE_eq_nhds] using id tfae_have 2 → 3 | h => by rcases (s ∩ Iic a).eq_empty_or_nonempty with he | hne · simp [he] at h · refine ⟨hne, (isLUB_of_mem_closure ?_ h).csSup_eq hne⟩ exact fun x hx => hx.2 tfae_have 3 → 4 | h => ⟨_, inter_subset_left, h.1, bddAbove_Iic.mono inter_subset_right, h.2⟩ tfae_have 4 → 5 := by rintro ⟨t, hts, hne, hbdd, rfl⟩ have hlub : IsLUB t (sSup t) := isLUB_csSup hne hbdd let ⟨y, hyt⟩ := hne classical refine ⟨succ (sSup t), succ_ne_zero _, fun x _ => if x ∈ t then x else y, fun x _ => ?_, ?_⟩ · simp only split_ifs with h <;> exact hts ‹_› · refine le_antisymm (bsup_le fun x _ => ?_) (csSup_le hne fun x hx => ?_) · split_ifs <;> exact hlub.1 ‹_› · refine (if_pos hx).symm.trans_le (le_bsup _ _ <| (hlub.1 hx).trans_lt (lt_succ _)) tfae_have 5 → 6 := by rintro ⟨o, h₀, f, hfs, rfl⟩ exact ⟨_, toType_nonempty_iff_ne_zero.2 h₀, familyOfBFamily o f, fun _ => hfs _ _, rfl⟩ tfae_have 6 → 1 := by rintro ⟨ι, hne, f, hfs, rfl⟩ exact closure_mono (range_subset_iff.2 hfs) <| csSup_mem_closure (range_nonempty f) (bddAbove_range.{u, u} f) tfae_finish theorem mem_closure_iff_iSup : a ∈ closure s ↔ ∃ (ι : Type u) (_ : Nonempty ι) (f : ι → Ordinal), (∀ i, f i ∈ s) ∧ ⨆ i, f i = a := by apply ((mem_closure_tfae a s).out 0 5).trans simp_rw [exists_prop] theorem mem_iff_iSup_of_isClosed (hs : IsClosed s) : a ∈ s ↔ ∃ (ι : Type u) (_hι : Nonempty ι) (f : ι → Ordinal), (∀ i, f i ∈ s) ∧ ⨆ i, f i = a := by rw [← mem_closure_iff_iSup, hs.closure_eq] theorem mem_closure_iff_bsup : a ∈ closure s ↔ ∃ (o : Ordinal) (_ho : o ≠ 0) (f : ∀ a < o, Ordinal), (∀ i hi, f i hi ∈ s) ∧ bsup.{u, u} o f = a := by apply ((mem_closure_tfae a s).out 0 4).trans simp_rw [exists_prop] theorem mem_closed_iff_bsup (hs : IsClosed s) : a ∈ s ↔ ∃ (o : Ordinal) (_ho : o ≠ 0) (f : ∀ a < o, Ordinal), (∀ i hi, f i hi ∈ s) ∧ bsup.{u, u} o f = a := by rw [← mem_closure_iff_bsup, hs.closure_eq] theorem isClosed_iff_iSup : IsClosed s ↔ ∀ {ι : Type u}, Nonempty ι → ∀ f : ι → Ordinal, (∀ i, f i ∈ s) → ⨆ i, f i ∈ s := by use fun hs ι hι f hf => (mem_iff_iSup_of_isClosed hs).2 ⟨ι, hι, f, hf, rfl⟩ rw [← closure_subset_iff_isClosed] intro h x hx rcases mem_closure_iff_iSup.1 hx with ⟨ι, hι, f, hf, rfl⟩ exact h hι f hf theorem isClosed_iff_bsup : IsClosed s ↔ ∀ {o : Ordinal}, o ≠ 0 → ∀ f : ∀ a < o, Ordinal, (∀ i hi, f i hi ∈ s) → bsup.{u, u} o f ∈ s := by rw [isClosed_iff_iSup] refine ⟨fun H o ho f hf => H (toType_nonempty_iff_ne_zero.2 ho) _ ?_, fun H ι hι f hf => ?_⟩ · exact fun i => hf _ _ · rw [← bsup_eq_iSup] apply H (type_ne_zero_iff_nonempty.2 hι) exact fun i hi => hf _ -- todo: generalize to other well-orders theorem isSuccLimit_of_mem_frontier (ha : a ∈ frontier s) : IsSuccLimit a := by simp only [frontier_eq_closure_inter_closure, Set.mem_inter_iff, mem_closure_iff] at ha by_contra h rw [← isOpen_singleton_iff] at h rcases ha.1 _ h rfl with ⟨b, hb, hb'⟩ rcases ha.2 _ h rfl with ⟨c, hc, hc'⟩ rw [Set.mem_singleton_iff] at * subst hb; subst hc exact hc' hb' @[deprecated (since := "2025-07-08")] alias isLimit_of_mem_frontier := isSuccLimit_of_mem_frontier @[deprecated Order.isNormal_iff_strictMono_and_continuous (since := "2025-08-21")] theorem isNormal_iff_strictMono_and_continuous (f : Ordinal.{u} → Ordinal.{u}) : IsNormal f ↔ StrictMono f ∧ Continuous f := Order.isNormal_iff_strictMono_and_continuous theorem enumOrd_isNormal_iff_isClosed (hs : ¬ BddAbove s) : IsNormal (enumOrd s) ↔ IsClosed s := by have Hs := enumOrd_strictMono hs refine ⟨fun h => isClosed_iff_iSup.2 fun {ι} hι f hf => ?_, fun h => (isNormal_iff_strictMono_limit _).2 ⟨Hs, fun a ha o H => ?_⟩⟩ · let g : ι → Ordinal.{u} := fun i => (enumOrdOrderIso s hs).symm ⟨_, hf i⟩ suffices enumOrd s (⨆ i, g i) = ⨆ i, f i by rw [← this] exact enumOrd_mem hs _ rw [IsNormal.map_iSup h g] congr ext x change (enumOrdOrderIso s hs _).val = f x rw [OrderIso.apply_symm_apply] · rw [isClosed_iff_bsup] at h suffices enumOrd s a ≤ bsup.{u, u} a fun b (_ : b < a) => enumOrd s b from this.trans (bsup_le H) obtain ⟨b, hb⟩ := enumOrd_surjective hs (h ha.ne_bot (fun b _ => enumOrd s b) fun b _ => enumOrd_mem hs b) rw [← hb] apply Hs.monotone by_contra! hba apply (Hs (lt_succ b)).not_ge rw [hb] exact le_bsup.{u, u} _ _ (ha.succ_lt hba) open Set Filter Set.Notation /-- An ordinal is an accumulation point of a set of ordinals if it is positive and there are elements in the set arbitrarily close to the ordinal from below. -/ def IsAcc (o : Ordinal) (S : Set Ordinal) : Prop := AccPt o (𝓟 S) /-- A set of ordinals is closed below an ordinal if it contains all of its accumulation points below the ordinal. -/ def IsClosedBelow (S : Set Ordinal) (o : Ordinal) : Prop := IsClosed (Iio o ↓∩ S) theorem isAcc_iff (o : Ordinal) (S : Set Ordinal) : o.IsAcc S ↔ o ≠ 0 ∧ ∀ p < o, (S ∩ Ioo p o).Nonempty := by dsimp [IsAcc] constructor · rw [accPt_iff_nhds] intro h constructor · rintro rfl obtain ⟨x, hx⟩ := h (Iio 1) (Iio_mem_nhds zero_lt_one) exact hx.2 <| lt_one_iff_zero.mp hx.1.1 · intro p plt obtain ⟨x, hx⟩ := h (Ioo p (o + 1)) <| Ioo_mem_nhds plt (lt_succ o) use x refine ⟨hx.1.2, ⟨hx.1.1.1, lt_of_le_of_ne ?_ hx.2⟩⟩ have := hx.1.1.2 rwa [← succ_eq_add_one, lt_succ_iff] at this · rw [accPt_iff_nhds] intro h u umem obtain ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds umem ⟨0, Ordinal.pos_iff_ne_zero.mpr h.1⟩ obtain ⟨x, hx⟩ := h.2 l hl.1 use x exact ⟨⟨hl.2 ⟨hx.2.1, hx.2.2.le⟩, hx.1⟩, hx.2.2.ne⟩ theorem IsAcc.forall_lt {o : Ordinal} {S : Set Ordinal} (h : o.IsAcc S) : ∀ p < o, (S ∩ Ioo p o).Nonempty := ((isAcc_iff _ _).mp h).2 theorem IsAcc.pos {o : Ordinal} {S : Set Ordinal} (h : o.IsAcc S) : 0 < o := Ordinal.pos_iff_ne_zero.mpr ((isAcc_iff _ _).mp h).1 theorem IsAcc.isSuccLimit {o : Ordinal} {S : Set Ordinal} (h : o.IsAcc S) : IsSuccLimit o := by rw [isAcc_iff] at h rw [isSuccLimit_iff] refine ⟨h.1, isSuccPrelimit_of_succ_ne fun x hx ↦ ?_⟩ rcases h.2 x (lt_of_lt_of_le (lt_succ x) hx.le) with ⟨p, hp⟩ exact (hx.symm ▸ (succ_le_iff.mpr hp.2.1)).not_gt hp.2.2 @[deprecated IsAcc.isSuccLimit (since := "2025-07-08")] alias IsAcc.isLimit := IsAcc.isSuccLimit theorem IsAcc.mono {o : Ordinal} {S T : Set Ordinal} (h : S ⊆ T) (ho : o.IsAcc S) : o.IsAcc T := by rw [isAcc_iff] at * exact ⟨ho.1, fun p plto ↦ (ho.2 p plto).casesOn fun s hs ↦ ⟨s, h hs.1, hs.2⟩⟩ theorem IsAcc.inter_Ioo_nonempty {o : Ordinal} {S : Set Ordinal} (hS : o.IsAcc S) {p : Ordinal} (hp : p < o) : (S ∩ Ioo p o).Nonempty := hS.forall_lt p hp -- todo: prove this for a general linear `SuccOrder`. theorem accPt_subtype {p o : Ordinal} (S : Set Ordinal) (hpo : p < o) : AccPt p (𝓟 S) ↔ AccPt ⟨p, hpo⟩ (𝓟 (Iio o ↓∩ S)) := by constructor · intro h have plim := IsAcc.isSuccLimit h rw [accPt_iff_nhds] at * intro u hu obtain ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds hu ⟨⟨0, plim.bot_lt.trans hpo⟩, plim.bot_lt⟩ obtain ⟨x, hx⟩ := h (Ioo l (p + 1)) (Ioo_mem_nhds hl.1 (lt_add_one _)) use ⟨x, lt_of_le_of_lt (lt_succ_iff.mp hx.1.1.2) hpo⟩ refine ⟨?_, Subtype.coe_ne_coe.mp hx.2⟩ exact ⟨hl.2 ⟨hx.1.1.1, by exact_mod_cast lt_succ_iff.mp hx.1.1.2⟩, hx.1.2⟩ · intro h rw [accPt_iff_nhds] at * intro u hu by_cases ho : p + 1 < o · have ppos : p ≠ 0 := by rintro rfl rw [zero_add] at ho specialize h (Iio ⟨1, ho⟩) (Iio_mem_nhds (Subtype.mk_lt_mk.mpr zero_lt_one)) obtain ⟨_, h⟩ := h exact h.2 <| Subtype.mk_eq_mk.mpr (lt_one_iff_zero.mp h.1.1) have plim : IsSuccLimit p := by contrapose! h obtain ⟨q, hq⟩ := ((zero_or_succ_or_isSuccLimit p).resolve_left ppos).resolve_right h use (Ioo ⟨q, ((hq ▸ lt_succ q).trans hpo)⟩ ⟨p + 1, ho⟩) constructor · exact Ioo_mem_nhds (hq ▸ lt_succ q) (lt_succ p) · intro _ mem have aux1 := Subtype.mk_lt_mk.mp mem.1.1 have aux2 := Subtype.mk_lt_mk.mp mem.1.2 rw [Subtype.mk_eq_mk] subst hq exact ((succ_le_iff.mpr aux1).antisymm (le_of_lt_succ aux2)).symm obtain ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds hu ⟨0, plim.bot_lt⟩ obtain ⟨x, hx⟩ := h (Ioo ⟨l, hl.1.trans hpo⟩ ⟨p + 1, ho⟩) (Ioo_mem_nhds hl.1 (lt_add_one p)) use x exact ⟨⟨hl.2 ⟨hx.1.1.1, lt_succ_iff.mp hx.1.1.2⟩, hx.1.2⟩, fun h ↦ hx.2 (SetCoe.ext h)⟩ have hp : o = p + 1 := (le_succ_iff_eq_or_le.mp (le_of_not_gt ho)).resolve_right (not_le_of_gt hpo) have ppos : p ≠ 0 := by rintro rfl obtain ⟨x, hx⟩ := h Set.univ univ_mem have : ↑x < o := x.2 simp_rw [hp, zero_add, lt_one_iff_zero] at this exact hx.2 (SetCoe.ext this) obtain ⟨l, hl⟩ := exists_Ioc_subset_of_mem_nhds hu ⟨0, Ordinal.pos_iff_ne_zero.mpr ppos⟩ obtain ⟨x, hx⟩ := h (Ioi ⟨l, hl.1.trans hpo⟩) (Ioi_mem_nhds hl.1) use x refine ⟨⟨hl.2 ⟨hx.1.1, ?_⟩, hx.1.2⟩, fun h ↦ hx.2 (SetCoe.ext h)⟩ rw [← lt_add_one_iff, ← hp] exact x.2 theorem isClosedBelow_iff {S : Set Ordinal} {o : Ordinal} : IsClosedBelow S o ↔ ∀ p < o, IsAcc p S → p ∈ S := by dsimp [IsClosedBelow] constructor · intro h p plto hp have : AccPt ⟨p, plto⟩ (𝓟 (Iio o ↓∩ S)) := (accPt_subtype _ _).mp hp rw [isClosed_iff_clusterPt] at h exact h ⟨p, plto⟩ this.clusterPt · intro h rw [isClosed_iff_clusterPt] intro r hr match clusterPt_principal.mp hr with | .inl h => exact h | .inr h' => exact h r.1 r.2 <| (accPt_subtype _ _).mpr h' alias ⟨IsClosedBelow.forall_lt, _⟩ := isClosedBelow_iff theorem IsClosedBelow.sInter {o : Ordinal} {S : Set (Set Ordinal)} (h : ∀ C ∈ S, IsClosedBelow C o) : IsClosedBelow (⋂₀ S) o := by rw [isClosedBelow_iff] intro p plto pAcc C CmemS exact (h C CmemS).forall_lt p plto (pAcc.mono (sInter_subset_of_mem CmemS)) theorem IsClosedBelow.iInter {ι : Type u} {f : ι → Set Ordinal} {o : Ordinal} (h : ∀ i, IsClosedBelow (f i) o) : IsClosedBelow (⋂ i, f i) o := IsClosedBelow.sInter fun _ ⟨i, hi⟩ ↦ hi ▸ (h i) end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Exponential.lean
import Mathlib.SetTheory.Ordinal.Family /-! # Ordinal exponential In this file we define the power function and the logarithm function on ordinals. The two are related by the lemma `Ordinal.opow_le_iff_le_log : b ^ c ≤ x ↔ c ≤ log b x` for nontrivial inputs `b`, `c`. -/ noncomputable section open Function Set Equiv Order open scoped Cardinal Ordinal universe u v w namespace Ordinal /-- The ordinal exponential, defined by transfinite recursion. We call this `opow` in theorems in order to disambiguate from other exponentials. -/ instance instPow : Pow Ordinal Ordinal := ⟨fun a b ↦ if a = 0 then 1 - b else limitRecOn b 1 (fun _ x ↦ x * a) fun o _ f ↦ ⨆ x : Iio o, f x.1 x.2⟩ private theorem opow_of_ne_zero {a b : Ordinal} (h : a ≠ 0) : a ^ b = limitRecOn b 1 (fun _ x ↦ x * a) fun o _ f ↦ ⨆ x : Iio o, f x.1 x.2 := if_neg h /-- `0 ^ a = 1` if `a = 0` and `0 ^ a = 0` otherwise. -/ theorem zero_opow' (a : Ordinal) : 0 ^ a = 1 - a := if_pos rfl theorem zero_opow_le (a : Ordinal) : (0 : Ordinal) ^ a ≤ 1 := by rw [zero_opow'] exact sub_le_self 1 a @[simp] theorem zero_opow {a : Ordinal} (a0 : a ≠ 0) : (0 : Ordinal) ^ a = 0 := by rwa [zero_opow', Ordinal.sub_eq_zero_iff_le, one_le_iff_ne_zero] @[simp] theorem opow_zero (a : Ordinal) : a ^ (0 : Ordinal) = 1 := by obtain rfl | h := eq_or_ne a 0 · rw [zero_opow', Ordinal.sub_zero] · rw [opow_of_ne_zero h, limitRecOn_zero] @[simp] theorem opow_succ (a b : Ordinal) : a ^ succ b = a ^ b * a := by obtain rfl | h := eq_or_ne a 0 · rw [zero_opow (succ_ne_zero b), mul_zero] · rw [opow_of_ne_zero h, opow_of_ne_zero h, limitRecOn_succ] theorem opow_limit {a b : Ordinal} (ha : a ≠ 0) (hb : IsSuccLimit b) : a ^ b = ⨆ x : Iio b, a ^ x.1 := by simp_rw [opow_of_ne_zero ha, limitRecOn_limit _ _ _ _ hb] theorem opow_le_of_isSuccLimit {a b c : Ordinal} (a0 : a ≠ 0) (h : IsSuccLimit b) : a ^ b ≤ c ↔ ∀ b' < b, a ^ b' ≤ c := by rw [opow_limit a0 h, Ordinal.iSup_le_iff, Subtype.forall] rfl @[deprecated (since := "2025-07-08")] alias opow_le_of_limit := opow_le_of_isSuccLimit theorem lt_opow_of_isSuccLimit {a b c : Ordinal} (b0 : b ≠ 0) (h : IsSuccLimit c) : a < b ^ c ↔ ∃ c' < c, a < b ^ c' := by simpa using (opow_le_of_isSuccLimit b0 h).not @[deprecated (since := "2025-07-08")] alias lt_opow_of_limit := lt_opow_of_isSuccLimit @[simp] theorem opow_one (a : Ordinal) : a ^ (1 : Ordinal) = a := by rw [← succ_zero, opow_succ] simp only [opow_zero, one_mul] @[simp] theorem one_opow (a : Ordinal) : (1 : Ordinal) ^ a = 1 := by induction a using limitRecOn with | zero => simp only [opow_zero] | succ _ ih => simp only [opow_succ, ih, mul_one] | limit b l IH => refine eq_of_forall_ge_iff fun c => ?_ rw [opow_le_of_isSuccLimit Ordinal.one_ne_zero l] exact ⟨fun H => by simpa only [opow_zero] using H 0 l.bot_lt, fun H b' h => by rwa [IH _ h]⟩ theorem opow_pos {a : Ordinal} (b : Ordinal) (a0 : 0 < a) : 0 < a ^ b := by have h0 : 0 < a ^ (0 : Ordinal) := by simp only [opow_zero, zero_lt_one] induction b using limitRecOn with | zero => exact h0 | succ b IH => rw [opow_succ] exact mul_pos IH a0 | limit b l _ => exact (lt_opow_of_isSuccLimit (Ordinal.pos_iff_ne_zero.1 a0) l).2 ⟨0, l.bot_lt, h0⟩ theorem opow_ne_zero {a : Ordinal} (b : Ordinal) (a0 : a ≠ 0) : a ^ b ≠ 0 := Ordinal.pos_iff_ne_zero.1 <| opow_pos b <| Ordinal.pos_iff_ne_zero.2 a0 @[simp] theorem opow_eq_zero {a b : Ordinal} : a ^ b = 0 ↔ a = 0 ∧ b ≠ 0 := by by_cases a = 0 <;> by_cases b = 0 <;> simp_all [opow_ne_zero] @[simp, norm_cast] theorem opow_natCast (a : Ordinal) (n : ℕ) : a ^ (n : Ordinal) = a ^ n := by induction n with | zero => rw [Nat.cast_zero, opow_zero, pow_zero] | succ n IH => rw [Nat.cast_succ, add_one_eq_succ, opow_succ, pow_succ, IH] theorem isNormal_opow {a : Ordinal} (h : 1 < a) : IsNormal (a ^ ·) := by have ha : 0 < a := zero_lt_one.trans h refine IsNormal.of_succ_lt ?_ fun hl ↦ ?_ · simpa only [mul_one, opow_succ] using fun b ↦ mul_lt_mul_of_pos_left h (opow_pos b ha) · simp [IsLUB, IsLeast, upperBounds, lowerBounds, ← opow_le_of_isSuccLimit ha.ne' hl] @[simp] theorem opow_lt_opow_iff_right {a b c : Ordinal} (a1 : 1 < a) : a ^ b < a ^ c ↔ b < c := (isNormal_opow a1).lt_iff @[simp] theorem opow_le_opow_iff_right {a b c : Ordinal} (a1 : 1 < a) : a ^ b ≤ a ^ c ↔ b ≤ c := (isNormal_opow a1).le_iff @[simp] theorem opow_right_inj {a b c : Ordinal} (a1 : 1 < a) : a ^ b = a ^ c ↔ b = c := (isNormal_opow a1).inj theorem isSuccLimit_opow {a b : Ordinal} (a1 : 1 < a) : IsSuccLimit b → IsSuccLimit (a ^ b) := (isNormal_opow a1).isSuccLimit @[deprecated (since := "2025-07-08")] alias isLimit_opow := isSuccLimit_opow theorem isSuccLimit_opow_left {a b : Ordinal} (l : IsSuccLimit a) (hb : b ≠ 0) : IsSuccLimit (a ^ b) := by rcases zero_or_succ_or_isSuccLimit b with (e | ⟨b, rfl⟩ | l') · exact absurd e hb · rw [opow_succ] exact isSuccLimit_mul (opow_pos _ l.bot_lt) l · exact isSuccLimit_opow (one_lt_of_isSuccLimit l) l' @[deprecated (since := "2025-07-08")] alias isLimit_opow_left := isSuccLimit_opow_left theorem opow_le_opow_right {a b c : Ordinal} (h₁ : 0 < a) (h₂ : b ≤ c) : a ^ b ≤ a ^ c := by rcases (one_le_iff_pos.2 h₁).eq_or_lt' with h₁ | h₁ · simp_all · exact (opow_le_opow_iff_right h₁).2 h₂ @[gcongr] theorem opow_le_opow_left {a b : Ordinal} (c : Ordinal) (ab : a ≤ b) : a ^ c ≤ b ^ c := by by_cases ha : a = 0 · by_cases c = 0 <;> simp_all · induction c using limitRecOn with | zero => simp | succ c IH => simpa using mul_le_mul' IH ab | limit c l IH => exact (opow_le_of_isSuccLimit ha l).2 fun b' h ↦ (IH _ h).trans (opow_le_opow_right ((Ordinal.pos_iff_ne_zero.2 ha).trans_le ab) h.le) @[gcongr] theorem opow_le_opow {a b c d : Ordinal} (hac : a ≤ c) (hbd : b ≤ d) (hc : 0 < c) : a ^ b ≤ c ^ d := (opow_le_opow_left b hac).trans (opow_le_opow_right hc hbd) theorem left_le_opow (a : Ordinal) {b : Ordinal} (b1 : 0 < b) : a ≤ a ^ b := by nth_rw 1 [← opow_one a] rcases le_or_gt a 1 with a1 | a1 · rcases lt_or_eq_of_le a1 with a0 | a1 · rw [lt_one_iff_zero] at a0 rw [a0, zero_opow Ordinal.one_ne_zero] exact Ordinal.zero_le _ rw [a1, one_opow, one_opow] rwa [opow_le_opow_iff_right a1, one_le_iff_pos] theorem left_lt_opow {a b : Ordinal} (ha : 1 < a) (hb : 1 < b) : a < a ^ b := by conv_lhs => rw [← opow_one a] rwa [opow_lt_opow_iff_right ha] theorem right_le_opow {a : Ordinal} (b : Ordinal) (a1 : 1 < a) : b ≤ a ^ b := (isNormal_opow a1).le_apply theorem opow_lt_opow_left_of_succ {a b c : Ordinal} (ab : a < b) : a ^ succ c < b ^ succ c := by rw [opow_succ, opow_succ] exact mul_lt_mul_of_le_of_lt_of_nonneg_of_pos (by gcongr) ab a.zero_le (opow_pos _ ab.bot_lt) theorem opow_add (a b c : Ordinal) : a ^ (b + c) = a ^ b * a ^ c := by rcases eq_or_ne a 0 with (rfl | a0) · rcases eq_or_ne c 0 with (rfl | c0) · simp have : b + c ≠ 0 := ((Ordinal.pos_iff_ne_zero.2 c0).trans_le (le_add_left _ _)).ne' simp only [zero_opow c0, zero_opow this, mul_zero] rcases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with (rfl | a1) · simp only [one_opow, mul_one] induction c using limitRecOn with | zero => simp | succ c IH => rw [add_succ, opow_succ, IH, opow_succ, mul_assoc] | limit c l IH => refine eq_of_forall_ge_iff fun d => (((isNormal_opow a1).trans (isNormal_add_right b)).limit_le l).trans ?_ dsimp only [Function.comp_def] simp +contextual only [IH] exact (((isNormal_mul_right <| opow_pos b (Ordinal.pos_iff_ne_zero.2 a0)).trans (isNormal_opow a1)).limit_le l).symm theorem opow_one_add (a b : Ordinal) : a ^ (1 + b) = a * a ^ b := by rw [opow_add, opow_one] theorem opow_dvd_opow (a : Ordinal) {b c : Ordinal} (h : b ≤ c) : a ^ b ∣ a ^ c := ⟨a ^ (c - b), by rw [← opow_add, Ordinal.add_sub_cancel_of_le h]⟩ theorem opow_dvd_opow_iff {a b c : Ordinal} (a1 : 1 < a) : a ^ b ∣ a ^ c ↔ b ≤ c := ⟨fun h => le_of_not_gt fun hn => not_le_of_gt ((opow_lt_opow_iff_right a1).2 hn) <| le_of_dvd (opow_ne_zero _ <| one_le_iff_ne_zero.1 <| a1.le) h, opow_dvd_opow _⟩ theorem opow_mul (a b c : Ordinal) : a ^ (b * c) = (a ^ b) ^ c := by by_cases b0 : b = 0; · simp only [b0, zero_mul, opow_zero, one_opow] by_cases a0 : a = 0 · subst a by_cases c0 : c = 0 · simp only [c0, mul_zero, opow_zero] simp only [zero_opow b0, zero_opow c0, zero_opow (mul_ne_zero b0 c0)] rcases eq_or_lt_of_le (one_le_iff_ne_zero.2 a0) with a1 | a1 · subst a1 simp only [one_opow] induction c using limitRecOn with | zero => simp only [mul_zero, opow_zero] | succ c IH => rw [mul_succ, opow_add, IH, opow_succ] | limit c l IH => refine eq_of_forall_ge_iff fun d => (((isNormal_opow a1).trans (isNormal_mul_right (Ordinal.pos_iff_ne_zero.2 b0))).limit_le l).trans ?_ dsimp only [Function.comp_def] simp +contextual only [IH] exact (opow_le_of_isSuccLimit (opow_ne_zero _ a0) l).symm theorem opow_mul_add_pos {b v : Ordinal} (hb : b ≠ 0) (u : Ordinal) (hv : v ≠ 0) (w : Ordinal) : 0 < b ^ u * v + w := (opow_pos u <| Ordinal.pos_iff_ne_zero.2 hb).trans_le <| (le_mul_left _ <| Ordinal.pos_iff_ne_zero.2 hv).trans <| le_add_right _ _ theorem opow_mul_add_lt_opow_mul {b u w x : Ordinal} {v : Ordinal} (hw : w < b ^ u) (hv : v < x) : b ^ u * v + w < b ^ u * x := by apply lt_of_lt_of_le (b := b ^ u * (v + 1)) · rwa [mul_add_one, add_lt_add_iff_left] · grw [add_one_le_of_lt hv] @[deprecated opow_mul_add_lt_opow_mul (since := "2025-08-27")] theorem opow_mul_add_lt_opow_mul_succ {b u w : Ordinal} (v : Ordinal) (hw : w < b ^ u) : b ^ u * v + w < b ^ u * succ v := opow_mul_add_lt_opow_mul hw (lt_succ v) theorem opow_mul_add_lt_opow {b u v w x : Ordinal} (hv : v < b) (hw : w < b ^ u) (hu : u < x) : b ^ u * v + w < b ^ x := by apply (opow_mul_add_lt_opow_mul hw hv).trans_le rw [← opow_succ] exact opow_le_opow_right hv.pos (succ_le_of_lt hu) @[deprecated opow_mul_add_lt_opow_succ (since := "2025-08-27")] theorem opow_mul_add_lt_opow_succ {b u v w : Ordinal} (hvb : v < b) (hw : w < b ^ u) : b ^ u * v + w < b ^ succ u := opow_mul_add_lt_opow hvb hw (lt_succ u) /-! ### Ordinal logarithm -/ /-- The ordinal logarithm is the solution `u` to the equation `x = b ^ u * v + w` where `v < b` and `w < b ^ u`. -/ @[pp_nodot] def log (b : Ordinal) (x : Ordinal) : Ordinal := if 1 < b then pred (sInf { o | x < b ^ o }) else 0 /-- The set in the definition of `log` is nonempty. -/ private theorem log_nonempty {b x : Ordinal} (h : 1 < b) : { o : Ordinal | x < b ^ o }.Nonempty := ⟨_, succ_le_iff.1 (right_le_opow _ h)⟩ theorem log_def {b : Ordinal} (h : 1 < b) (x : Ordinal) : log b x = pred (sInf { o | x < b ^ o }) := if_pos h theorem log_of_left_le_one {b : Ordinal} (h : b ≤ 1) (x : Ordinal) : log b x = 0 := if_neg h.not_gt @[simp] theorem log_zero_left : ∀ b, log 0 b = 0 := log_of_left_le_one zero_le_one @[simp] theorem log_zero_right (b : Ordinal) : log b 0 = 0 := by obtain hb | hb := lt_or_ge 1 b · rw [log_def hb, ← Ordinal.le_zero, pred_le_iff_le_succ, succ_zero] apply csInf_le' rw [mem_setOf, opow_one] exact bot_lt_of_lt hb · rw [log_of_left_le_one hb] @[simp] theorem log_one_left : ∀ b, log 1 b = 0 := log_of_left_le_one le_rfl theorem succ_log_def {b x : Ordinal} (hb : 1 < b) (hx : x ≠ 0) : succ (log b x) = sInf { o : Ordinal | x < b ^ o } := by let t := sInf { o : Ordinal | x < b ^ o } have : x < b ^ t := csInf_mem (log_nonempty hb) rcases zero_or_succ_or_isSuccLimit t with (h | h | h) · refine ((one_le_iff_ne_zero.2 hx).not_gt ?_).elim simpa only [h, opow_zero] using this · rw [log_def hb x, succ_pred_eq_iff_not_isSuccPrelimit, not_isSuccPrelimit_iff'] simpa [eq_comm] using h · rcases (lt_opow_of_isSuccLimit (zero_lt_one.trans hb).ne' h).1 this with ⟨a, h₁, h₂⟩ exact h₁.not_ge.elim ((le_csInf_iff'' (log_nonempty hb)).1 le_rfl a h₂) theorem lt_opow_succ_log_self {b : Ordinal} (hb : 1 < b) (x : Ordinal) : x < b ^ succ (log b x) := by rcases eq_or_ne x 0 with (rfl | hx) · apply opow_pos _ (zero_lt_one.trans hb) · rw [succ_log_def hb hx] exact csInf_mem (log_nonempty hb) theorem opow_log_le_self (b : Ordinal) {x : Ordinal} (hx : x ≠ 0) : b ^ log b x ≤ x := by rcases eq_or_ne b 0 with (rfl | b0) · exact (zero_opow_le _).trans (one_le_iff_ne_zero.2 hx) rcases lt_or_eq_of_le (one_le_iff_ne_zero.2 b0) with (hb | rfl) · refine le_of_not_gt fun h => (lt_succ (log b x)).not_ge ?_ have := @csInf_le' _ _ { o | x < b ^ o } _ h rwa [← succ_log_def hb hx] at this · rwa [one_opow, one_le_iff_ne_zero] /-- `opow b` and `log b` (almost) form a Galois connection. See `opow_le_iff_le_log'` for a variant assuming `c ≠ 0` rather than `x ≠ 0`. See also `le_log_of_opow_le` and `opow_le_of_le_log`, which are both separate implications under weaker assumptions. -/ theorem opow_le_iff_le_log {b x c : Ordinal} (hb : 1 < b) (hx : x ≠ 0) : b ^ c ≤ x ↔ c ≤ log b x := by constructor <;> intro h · apply le_of_not_gt intro hn apply (lt_opow_succ_log_self hb x).not_ge <| ((opow_le_opow_iff_right hb).2 <| succ_le_of_lt hn).trans h · exact ((opow_le_opow_iff_right hb).2 h).trans <| opow_log_le_self b hx /-- `opow b` and `log b` (almost) form a Galois connection. See `opow_le_iff_le_log` for a variant assuming `x ≠ 0` rather than `c ≠ 0`. See also `le_log_of_opow_le` and `opow_le_of_le_log`, which are both separate implications under weaker assumptions. -/ theorem opow_le_iff_le_log' {b x c : Ordinal} (hb : 1 < b) (hc : c ≠ 0) : b ^ c ≤ x ↔ c ≤ log b x := by obtain rfl | hx := eq_or_ne x 0 · simp [hc, (zero_lt_one.trans hb).ne'] · exact opow_le_iff_le_log hb hx theorem le_log_of_opow_le {b x c : Ordinal} (hb : 1 < b) (h : b ^ c ≤ x) : c ≤ log b x := by obtain rfl | hx := eq_or_ne x 0 · rw [Ordinal.le_zero, opow_eq_zero] at h exact (zero_lt_one.asymm <| h.1 ▸ hb).elim · exact (opow_le_iff_le_log hb hx).1 h theorem opow_le_of_le_log {b x c : Ordinal} (hc : c ≠ 0) (h : c ≤ log b x) : b ^ c ≤ x := by obtain hb | hb := le_or_gt b 1 · rw [log_of_left_le_one hb] at h exact (h.not_gt (Ordinal.pos_iff_ne_zero.2 hc)).elim · rwa [opow_le_iff_le_log' hb hc] /-- `opow b` and `log b` (almost) form a Galois connection. See `lt_opow_iff_log_lt'` for a variant assuming `c ≠ 0` rather than `x ≠ 0`. See also `lt_opow_of_log_lt` and `lt_log_of_lt_opow`, which are both separate implications under weaker assumptions. -/ theorem lt_opow_iff_log_lt {b x c : Ordinal} (hb : 1 < b) (hx : x ≠ 0) : x < b ^ c ↔ log b x < c := lt_iff_lt_of_le_iff_le (opow_le_iff_le_log hb hx) /-- `opow b` and `log b` (almost) form a Galois connection. See `lt_opow_iff_log_lt` for a variant assuming `x ≠ 0` rather than `c ≠ 0`. See also `lt_opow_of_log_lt` and `lt_log_of_lt_opow`, which are both separate implications under weaker assumptions. -/ theorem lt_opow_iff_log_lt' {b x c : Ordinal} (hb : 1 < b) (hc : c ≠ 0) : x < b ^ c ↔ log b x < c := lt_iff_lt_of_le_iff_le (opow_le_iff_le_log' hb hc) theorem lt_opow_of_log_lt {b x c : Ordinal} (hb : 1 < b) : log b x < c → x < b ^ c := lt_imp_lt_of_le_imp_le <| le_log_of_opow_le hb theorem lt_log_of_lt_opow {b x c : Ordinal} (hc : c ≠ 0) : x < b ^ c → log b x < c := lt_imp_lt_of_le_imp_le <| opow_le_of_le_log hc theorem log_pos {b o : Ordinal} (hb : 1 < b) (ho : o ≠ 0) (hbo : b ≤ o) : 0 < log b o := by rwa [← succ_le_iff, succ_zero, ← opow_le_iff_le_log hb ho, opow_one] theorem log_eq_zero {b o : Ordinal} (hbo : o < b) : log b o = 0 := by rcases eq_or_ne o 0 with (rfl | ho) · exact log_zero_right b rcases le_or_gt b 1 with hb | hb · rcases le_one_iff.1 hb with (rfl | rfl) · exact log_zero_left o · exact log_one_left o · rwa [← Ordinal.le_zero, ← lt_succ_iff, succ_zero, ← lt_opow_iff_log_lt hb ho, opow_one] @[gcongr, mono] theorem log_mono_right (b : Ordinal) {x y : Ordinal} (xy : x ≤ y) : log b x ≤ log b y := by obtain rfl | hx := eq_or_ne x 0 · simp_rw [log_zero_right, Ordinal.zero_le] · obtain hb | hb := lt_or_ge 1 b · exact (opow_le_iff_le_log hb (hx.bot_lt.trans_le xy).ne').1 <| (opow_log_le_self _ hx).trans xy · rw [log_of_left_le_one hb, log_of_left_le_one hb] theorem log_le_self (b x : Ordinal) : log b x ≤ x := by obtain rfl | hx := eq_or_ne x 0 · rw [log_zero_right] · obtain hb | hb := lt_or_ge 1 b · exact (right_le_opow _ hb).trans (opow_log_le_self b hx) · simp_rw [log_of_left_le_one hb, Ordinal.zero_le] @[simp] theorem log_one_right (b : Ordinal) : log b 1 = 0 := by obtain hb | hb := lt_or_ge 1 b · exact log_eq_zero hb · exact log_of_left_le_one hb 1 theorem mod_opow_log_lt_self (b : Ordinal) {o : Ordinal} (ho : o ≠ 0) : o % (b ^ log b o) < o := by rcases eq_or_ne b 0 with (rfl | hb) · simpa using Ordinal.pos_iff_ne_zero.2 ho · exact (mod_lt _ <| opow_ne_zero _ hb).trans_le (opow_log_le_self _ ho) theorem log_mod_opow_log_lt_log_self {b o : Ordinal} (hb : 1 < b) (hbo : b ≤ o) : log b (o % (b ^ log b o)) < log b o := by rcases eq_or_ne (o % (b ^ log b o)) 0 with h | h · rw [h, log_zero_right] exact log_pos hb (one_le_iff_ne_zero.1 (hb.le.trans hbo)) hbo · rw [← succ_le_iff, succ_log_def hb h] apply csInf_le' apply mod_lt rw [← Ordinal.pos_iff_ne_zero] exact opow_pos _ (zero_lt_one.trans hb) theorem log_eq_iff {b x : Ordinal} (hb : 1 < b) (hx : x ≠ 0) (y : Ordinal) : log b x = y ↔ b ^ y ≤ x ∧ x < b ^ succ y := by constructor · rintro rfl use opow_log_le_self b hx, lt_opow_succ_log_self hb x · rintro ⟨hx₁, hx₂⟩ apply le_antisymm · rwa [← lt_succ_iff, ← lt_opow_iff_log_lt hb hx] · rwa [← opow_le_iff_le_log hb hx] theorem log_opow_mul_add {b u v w : Ordinal} (hb : 1 < b) (hv : v ≠ 0) (hw : w < b ^ u) : log b (b ^ u * v + w) = u + log b v := by rw [log_eq_iff hb] · constructor · grw [opow_add, opow_log_le_self b hv, ← le_add_right] · apply (add_lt_add_left hw _).trans_le rw [← mul_succ, ← add_succ, opow_add] gcongr rw [succ_le_iff] exact lt_opow_succ_log_self hb _ · exact fun h ↦ mul_ne_zero (opow_ne_zero u (bot_lt_of_lt hb).ne') hv <| left_eq_zero_of_add_eq_zero h theorem log_opow_mul {b v : Ordinal} (hb : 1 < b) (u : Ordinal) (hv : v ≠ 0) : log b (b ^ u * v) = u + log b v := by simpa using log_opow_mul_add hb hv (opow_pos u (bot_lt_of_lt hb)) theorem log_opow {b : Ordinal} (hb : 1 < b) (x : Ordinal) : log b (b ^ x) = x := by convert log_opow_mul hb x zero_ne_one.symm using 1 · rw [mul_one] · rw [log_one_right, add_zero] theorem div_opow_log_pos (b : Ordinal) {o : Ordinal} (ho : o ≠ 0) : 0 < o / (b ^ log b o) := by rcases eq_zero_or_pos b with (rfl | hb) · simpa using Ordinal.pos_iff_ne_zero.2 ho · rw [div_pos (opow_ne_zero _ hb.ne')] exact opow_log_le_self b ho theorem div_opow_log_lt {b : Ordinal} (o : Ordinal) (hb : 1 < b) : o / (b ^ log b o) < b := by rw [div_lt (opow_pos _ (zero_lt_one.trans hb)).ne', ← opow_succ] exact lt_opow_succ_log_self hb o theorem add_log_le_log_mul {x y : Ordinal} (b : Ordinal) (hx : x ≠ 0) (hy : y ≠ 0) : log b x + log b y ≤ log b (x * y) := by obtain hb | hb := lt_or_ge 1 b · rw [← opow_le_iff_le_log hb (mul_ne_zero hx hy), opow_add] exact mul_le_mul' (opow_log_le_self b hx) (opow_log_le_self b hy) · simpa only [log_of_left_le_one hb, zero_add] using le_rfl theorem omega0_opow_mul_nat_lt {a b : Ordinal} (h : a < b) (n : ℕ) : ω ^ a * n < ω ^ b := by apply lt_of_lt_of_le _ (opow_le_opow_right omega0_pos (succ_le_of_lt h)) rw [opow_succ] gcongr exacts [opow_pos a omega0_pos, nat_lt_omega0 n] theorem lt_omega0_opow {a b : Ordinal} (hb : b ≠ 0) : a < ω ^ b ↔ ∃ c < b, ∃ n : ℕ, a < ω ^ c * n := by refine ⟨fun ha ↦ ⟨_, lt_log_of_lt_opow hb ha, ?_⟩, fun ⟨c, hc, n, hn⟩ ↦ hn.trans (omega0_opow_mul_nat_lt hc n)⟩ obtain ⟨n, hn⟩ := lt_omega0.1 (div_opow_log_lt a one_lt_omega0) use n.succ rw [natCast_succ, ← hn] exact lt_mul_succ_div a (opow_ne_zero _ omega0_ne_zero) theorem lt_omega0_opow_succ {a b : Ordinal} : a < ω ^ succ b ↔ ∃ n : ℕ, a < ω ^ b * n := by refine ⟨fun ha ↦ ?_, fun ⟨n, hn⟩ ↦ hn.trans (omega0_opow_mul_nat_lt (lt_succ b) n)⟩ obtain ⟨c, hc, n, hn⟩ := (lt_omega0_opow (succ_ne_zero b)).1 ha refine ⟨n, hn.trans_le ?_⟩ grw [lt_succ_iff.1 hc] exact omega0_pos /-! ### Interaction with `Nat.cast` -/ @[simp, norm_cast] theorem natCast_opow (m : ℕ) : ∀ n : ℕ, ↑(m ^ n : ℕ) = (m : Ordinal) ^ (n : Ordinal) | 0 => by simp | n + 1 => by rw [pow_succ, natCast_mul, natCast_opow m n, Nat.cast_succ, add_one_eq_succ, opow_succ] theorem iSup_pow {o : Ordinal} (ho : 0 < o) : ⨆ n : ℕ, o ^ n = o ^ ω := by simp_rw [← opow_natCast] rcases (one_le_iff_pos.2 ho).lt_or_eq with ho₁ | rfl · exact (isNormal_opow ho₁).apply_omega0 · rw [one_opow] refine le_antisymm (Ordinal.iSup_le fun n => by rw [one_opow]) ?_ exact_mod_cast Ordinal.le_iSup _ 0 end Ordinal -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11215): TODO: Port this meta code. -- namespace Tactic -- open Ordinal Mathlib.Meta.Positivity -- /-- Extension for the `positivity` tactic: `ordinal.opow` takes positive values on positive -- inputs. -/ -- @[positivity] -- unsafe def positivity_opow : expr → tactic strictness -- | q(@Pow.pow _ _ $(inst) $(a) $(b)) => do -- let strictness_a ← core a -- match strictness_a with -- | positive p => positive <$> mk_app `` opow_pos [b, p] -- | _ => failed -- |-- We already know that `0 ≤ x` for all `x : Ordinal` -- _ => -- failed -- end Tactic
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Arithmetic.lean
import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Data.Nat.SuccPred import Mathlib.Order.SuccPred.InitialSeg import Mathlib.SetTheory.Ordinal.Basic /-! # Ordinal arithmetic Ordinals have an addition (corresponding to disjoint union) that turns them into an additive monoid, and a multiplication (corresponding to the lexicographic order on the product) that turns them into a monoid. One can also define correspondingly a subtraction, a division, a successor function, a power function and a logarithm function. We also define limit ordinals and prove the basic induction principle on ordinals separating successor ordinals and limit ordinals, in `limitRecOn`. ## Main definitions and results * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. * `o₁ - o₂` is the unique ordinal `o` such that `o₂ + o = o₁`, when `o₂ ≤ o₁`. * `o₁ * o₂` is the lexicographic order on `o₂ × o₁`. * `o₁ / o₂` is the ordinal `o` such that `o₁ = o₂ * o + o'` with `o' < o₂`. We also define the divisibility predicate, and a modulo operation. * `Order.succ o = o + 1` is the successor of `o`. * `pred o` if the predecessor of `o`. If `o` is not a successor, we set `pred o = o`. We discuss the properties of casts of natural numbers of and of `ω` with respect to these operations. Some properties of the operations are also used to discuss general tools on ordinals: * `Order.IsSuccLimit o`: an ordinal is a limit ordinal if it is neither `0` nor a successor. * `limitRecOn` is the main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. * `IsNormal`: a function `f : Ordinal → Ordinal` satisfies `IsNormal` if it is strictly increasing and order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. Various other basic arithmetic results are given in `Principal.lean` instead. -/ assert_not_exists Field Module noncomputable section open Function Cardinal Set Equiv Order open scoped Ordinal universe u v w namespace Ordinal variable {α β γ : Type*} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Further properties of addition on ordinals -/ @[simp] theorem lift_add (a b : Ordinal.{v}) : lift.{u} (a + b) = lift.{u} a + lift.{u} b := Quotient.inductionOn₂ a b fun ⟨_α, _r, _⟩ ⟨_β, _s, _⟩ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift _).trans (RelIso.sumLexCongr (RelIso.preimage Equiv.ulift _) (RelIso.preimage Equiv.ulift _)).symm⟩ @[simp] theorem lift_succ (a : Ordinal.{v}) : lift.{u} (succ a) = succ (lift.{u} a) := by rw [← add_one_eq_succ, lift_add, lift_one] rfl instance instAddLeftReflectLE : AddLeftReflectLE Ordinal.{u} where elim c a b := by refine inductionOn₃ a b c fun α r _ β s _ γ t _ ⟨f⟩ ↦ ?_ have H₁ a : f (Sum.inl a) = Sum.inl a := by simpa using ((InitialSeg.leAdd t r).trans f).eq (InitialSeg.leAdd t s) a have H₂ a : ∃ b, f (Sum.inr a) = Sum.inr b := by generalize hx : f (Sum.inr a) = x obtain x | x := x · rw [← H₁, f.inj] at hx contradiction · exact ⟨x, rfl⟩ choose g hg using H₂ refine (RelEmbedding.ofMonotone g fun _ _ h ↦ ?_).ordinal_type_le rwa [← @Sum.lex_inr_inr _ t _ s, ← hg, ← hg, f.map_rel_iff, Sum.lex_inr_inr] instance : IsLeftCancelAdd Ordinal where add_left_cancel a b c h := by simpa only [le_antisymm_iff, add_le_add_iff_left] using h private theorem add_lt_add_iff_left' (a) {b c : Ordinal} : a + b < a + c ↔ b < c := by rw [← not_le, ← not_le, add_le_add_iff_left] instance instAddLeftStrictMono : AddLeftStrictMono Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).2⟩ instance instAddLeftReflectLT : AddLeftReflectLT Ordinal.{u} := ⟨fun a _b _c ↦ (add_lt_add_iff_left' a).1⟩ instance instAddRightReflectLT : AddRightReflectLT Ordinal.{u} := ⟨fun _a _b _c ↦ lt_imp_lt_of_le_imp_le fun h => add_le_add_right h _⟩ theorem add_le_add_iff_right {a b : Ordinal} : ∀ n : ℕ, a + n ≤ b + n ↔ a ≤ b | 0 => by simp | n + 1 => by simp only [natCast_succ, add_succ, add_succ, succ_le_succ_iff, add_le_add_iff_right] theorem add_right_cancel {a b : Ordinal} (n : ℕ) : a + n = b + n ↔ a = b := by simp only [le_antisymm_iff, add_le_add_iff_right] theorem add_eq_zero_iff {a b : Ordinal} : a + b = 0 ↔ a = 0 ∧ b = 0 := inductionOn₂ a b fun α r _ β s _ => by simp_rw [← type_sum_lex, type_eq_zero_iff_isEmpty] exact isEmpty_sum theorem left_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : a = 0 := (add_eq_zero_iff.1 h).1 theorem right_eq_zero_of_add_eq_zero {a b : Ordinal} (h : a + b = 0) : b = 0 := (add_eq_zero_iff.1 h).2 /-! ### Limit ordinals -/ theorem isSuccLimit_iff {o : Ordinal} : IsSuccLimit o ↔ o ≠ 0 ∧ IsSuccPrelimit o := by simp [IsSuccLimit] @[deprecated (since := "2025-07-09")] alias isLimit_iff := isSuccLimit_iff @[deprecated (since := "2025-07-09")] alias IsLimit.isSuccPrelimit := IsSuccLimit.isSuccPrelimit @[deprecated (since := "2025-07-09")] alias IsLimit.succ_lt := IsSuccLimit.succ_lt @[simp] theorem isSuccPrelimit_zero : IsSuccPrelimit (0 : Ordinal) := isSuccPrelimit_bot @[simp] theorem not_isSuccLimit_zero : ¬ IsSuccLimit (0 : Ordinal) := not_isSuccLimit_bot @[deprecated (since := "2025-07-09")] alias not_zero_isLimit := not_isSuccLimit_zero @[deprecated (since := "2025-07-09")] alias not_succ_isLimit := not_isSuccLimit_succ @[deprecated (since := "2025-07-09")] alias succ_lt_of_isLimit := IsSuccLimit.succ_lt_iff @[deprecated (since := "2025-07-09")] alias le_succ_of_isLimit := IsSuccLimit.le_succ_iff @[deprecated (since := "2025-07-09")] alias limit_le := IsSuccLimit.le_iff_forall_le @[deprecated (since := "2025-07-09")] alias lt_limit := IsSuccLimit.lt_iff_exists_lt @[simp] theorem isSuccPrelimit_lift {o : Ordinal} : IsSuccPrelimit (lift.{u, v} o) ↔ IsSuccPrelimit o := liftInitialSeg.isSuccPrelimit_apply_iff @[simp] theorem isSuccLimit_lift {o : Ordinal} : IsSuccLimit (lift.{u, v} o) ↔ IsSuccLimit o := liftInitialSeg.isSuccLimit_apply_iff @[deprecated (since := "2025-07-09")] alias lift_isLimit := isSuccLimit_lift theorem natCast_lt_of_isSuccLimit {o : Ordinal} (h : IsSuccLimit o) (n : ℕ) : n < o := by simpa using h.add_natCast_lt h.bot_lt n @[deprecated (since := "2025-07-09")] alias IsLimit.nat_lt := natCast_lt_of_isSuccLimit theorem one_lt_of_isSuccLimit {o : Ordinal} (h : IsSuccLimit o) : 1 < o := mod_cast natCast_lt_of_isSuccLimit h 1 @[deprecated (since := "2025-07-09")] alias IsLimit.one_lt := one_lt_of_isSuccLimit theorem zero_or_succ_or_isSuccLimit (o : Ordinal) : o = 0 ∨ o ∈ range succ ∨ IsSuccLimit o := by simpa using isMin_or_mem_range_succ_or_isSuccLimit o @[deprecated (since := "2025-07-09")] alias IsLimit.sSup_Iio := IsSuccLimit.sSup_Iio @[deprecated (since := "2025-07-09")] alias IsLimit.iSup_Iio := IsSuccLimit.iSup_Iio /-- Main induction principle of ordinals: if one can prove a property by induction at successor ordinals and at limit ordinals, then it holds for all ordinals. -/ @[elab_as_elim] def limitRecOn {motive : Ordinal → Sort*} (o : Ordinal) (zero : motive 0) (succ : ∀ o, motive o → motive (succ o)) (limit : ∀ o, IsSuccLimit o → (∀ o' < o, motive o') → motive o) : motive o := SuccOrder.limitRecOn o (fun _a ha ↦ ha.eq_bot ▸ zero) (fun a _ ↦ succ a) limit @[simp] theorem limitRecOn_zero {motive} (H₁ H₂ H₃) : @limitRecOn motive 0 H₁ H₂ H₃ = H₁ := SuccOrder.limitRecOn_isMin _ _ _ isMin_bot @[simp] theorem limitRecOn_succ {motive} (o H₁ H₂ H₃) : @limitRecOn motive (succ o) H₁ H₂ H₃ = H₂ o (@limitRecOn motive o H₁ H₂ H₃) := SuccOrder.limitRecOn_succ .. @[simp] theorem limitRecOn_limit {motive} (o H₁ H₂ H₃ h) : @limitRecOn motive o H₁ H₂ H₃ = H₃ o h fun x _h => @limitRecOn motive x H₁ H₂ H₃ := SuccOrder.limitRecOn_of_isSuccLimit .. /-- Bounded recursion on ordinals. Similar to `limitRecOn`, with the assumption `o < l` added to all cases. The final term's domain is the ordinals below `l`. -/ @[elab_as_elim] def boundedLimitRecOn {l : Ordinal} (lLim : IsSuccLimit l) {motive : Iio l → Sort*} (o : Iio l) (zero : motive ⟨0, lLim.bot_lt⟩) (succ : (o : Iio l) → motive o → motive ⟨succ o, lLim.succ_lt o.2⟩) (limit : (o : Iio l) → IsSuccLimit o.1 → (Π o' < o, motive o') → motive o) : motive o := limitRecOn (motive := fun p ↦ (h : p < l) → motive ⟨p, h⟩) o.1 (fun _ ↦ zero) (fun o ih h ↦ succ ⟨o, _⟩ <| ih <| (lt_succ o).trans h) (fun _o ho ih _ ↦ limit _ ho fun _o' h ↦ ih _ h _) o.2 @[simp] theorem boundedLimitRec_zero {l} (lLim : IsSuccLimit l) {motive} (H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨0, lLim.bot_lt⟩ H₁ H₂ H₃ = H₁ := by rw [boundedLimitRecOn, limitRecOn_zero] @[simp] theorem boundedLimitRec_succ {l} (lLim : IsSuccLimit l) {motive} (o H₁ H₂ H₃) : @boundedLimitRecOn l lLim motive ⟨succ o.1, lLim.succ_lt o.2⟩ H₁ H₂ H₃ = H₂ o (@boundedLimitRecOn l lLim motive o H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_succ] rfl theorem boundedLimitRec_limit {l} (lLim : IsSuccLimit l) {motive} (o H₁ H₂ H₃ oLim) : @boundedLimitRecOn l lLim motive o H₁ H₂ H₃ = H₃ o oLim (fun x _ ↦ @boundedLimitRecOn l lLim motive x H₁ H₂ H₃) := by rw [boundedLimitRecOn, limitRecOn_limit] rfl instance orderTopToTypeSucc (o : Ordinal) : OrderTop (succ o).toType := @OrderTop.mk _ _ (Top.mk _) le_enum_succ theorem enum_succ_eq_top {o : Ordinal} : enum (α := (succ o).toType) (· < ·) ⟨o, type_toType _ ▸ lt_succ o⟩ = ⊤ := rfl theorem has_succ_of_type_succ_lt {α} {r : α → α → Prop} [wo : IsWellOrder α r] (h : ∀ a < type r, succ a < type r) (x : α) : ∃ y, r x y := by use enum r ⟨succ (typein r x), h _ (typein_lt_type r x)⟩ convert enum_lt_enum.mpr _ · rw [enum_typein] · rw [Subtype.mk_lt_mk, lt_succ_iff] theorem toType_noMax_of_succ_lt {o : Ordinal} (ho : ∀ a < o, succ a < o) : NoMaxOrder o.toType := ⟨has_succ_of_type_succ_lt (type_toType _ ▸ ho)⟩ theorem bounded_singleton {r : α → α → Prop} [IsWellOrder α r] (hr : IsSuccLimit (type r)) (x) : Bounded r {x} := by refine ⟨enum r ⟨succ (typein r x), hr.succ_lt (typein_lt_type r x)⟩, ?_⟩ intro b hb rw [mem_singleton_iff.1 hb] nth_rw 1 [← enum_typein r x] rw [@enum_lt_enum _ r, Subtype.mk_lt_mk] apply lt_succ @[simp] theorem typein_ordinal (o : Ordinal.{u}) : @typein Ordinal (· < ·) _ o = Ordinal.lift.{u + 1} o := by refine Quotient.inductionOn o ?_ rintro ⟨α, r, wo⟩; apply Quotient.sound constructor; refine ((RelIso.preimage Equiv.ulift r).trans (enum r).symm).symm theorem mk_Iio_ordinal (o : Ordinal.{u}) : #(Iio o) = Cardinal.lift.{u + 1} o.card := by rw [lift_card, ← typein_ordinal] rfl /-! ### The predecessor of an ordinal -/ /-- The ordinal predecessor of `o` is `o'` if `o = succ o'`, and `o` otherwise. -/ def pred (o : Ordinal) : Ordinal := isSuccPrelimitRecOn o (fun a _ ↦ a) (fun a _ ↦ a) @[simp] theorem pred_succ (o) : pred (succ o) = o := isSuccPrelimitRecOn_succ .. theorem pred_eq_of_isSuccPrelimit {o} : IsSuccPrelimit o → pred o = o := isSuccPrelimitRecOn_of_isSuccPrelimit _ _ alias _root_.Order.IsSuccPrelimit.ordinalPred_eq := pred_eq_of_isSuccPrelimit theorem _root_.Order.IsSuccLimit.ordinalPred_eq {o} (ho : IsSuccLimit o) : pred o = o := ho.isSuccPrelimit.ordinalPred_eq @[simp] theorem pred_zero : pred 0 = 0 := isSuccPrelimit_zero.ordinalPred_eq @[simp] theorem pred_le_iff_le_succ {a b} : pred a ≤ b ↔ a ≤ succ b := by obtain ⟨a, rfl⟩ | ha := mem_range_succ_or_isSuccPrelimit a · simp · rw [ha.ordinalPred_eq, ha.le_succ_iff] @[simp] theorem lt_pred_iff_succ_lt {a b} : a < pred b ↔ succ a < b := le_iff_le_iff_lt_iff_lt.1 pred_le_iff_le_succ theorem pred_le_self (o) : pred o ≤ o := by simpa using le_succ o /-- `Ordinal.pred` and `Order.succ` form a Galois insertion. -/ def pred_succ_gi : GaloisInsertion pred succ := GaloisConnection.toGaloisInsertion @pred_le_iff_le_succ (by simp) theorem pred_surjective : Function.Surjective pred := pred_succ_gi.l_surjective theorem self_le_succ_pred (o) : o ≤ succ (pred o) := pred_succ_gi.gc.le_u_l o theorem pred_eq_iff_isSuccPrelimit {o} : pred o = o ↔ IsSuccPrelimit o := by obtain ⟨a, rfl⟩ | ho := mem_range_succ_or_isSuccPrelimit o · simpa using (lt_succ a).ne · simp_rw [ho.ordinalPred_eq, ho] theorem pred_lt_iff_not_isSuccPrelimit {o} : pred o < o ↔ ¬ IsSuccPrelimit o := by rw [(pred_le_self o).lt_iff_ne] exact pred_eq_iff_isSuccPrelimit.not theorem succ_pred_eq_iff_not_isSuccPrelimit {o} : succ (pred o) = o ↔ ¬ IsSuccPrelimit o := by rw [← (self_le_succ_pred o).ge_iff_eq', succ_le_iff, pred_lt_iff_not_isSuccPrelimit] @[simp] theorem lift_pred (o : Ordinal.{v}) : lift.{u} (pred o) = pred (lift.{u} o) := by obtain ⟨a, rfl⟩ | ho := mem_range_succ_or_isSuccPrelimit o · simp · rwa [ho.ordinalPred_eq, eq_comm, pred_eq_iff_isSuccPrelimit, isSuccPrelimit_lift] /-! ### Normal ordinal functions -/ /-- A normal ordinal function is a strictly increasing function which is order-continuous, i.e., the image `f o` of a limit ordinal `o` is the sup of `f a` for `a < o`. Todo: deprecate this in favor of `Order.IsNormal`. -/ def IsNormal (f : Ordinal → Ordinal) : Prop := Order.IsNormal f theorem IsNormal.limit_le {f} (H : IsNormal f) : ∀ {o}, IsSuccLimit o → ∀ {a}, f o ≤ a ↔ ∀ b < o, f b ≤ a := H.le_iff_forall_le theorem IsNormal.limit_lt {f} (H : IsNormal f) {o} (h : IsSuccLimit o) {a} : a < f o ↔ ∃ b < o, a < f b := H.lt_iff_exists_lt h theorem IsNormal.strictMono {f} (H : IsNormal f) : StrictMono f := Order.IsNormal.strictMono H theorem IsNormal.monotone {f} (H : IsNormal f) : Monotone f := H.strictMono.monotone theorem isNormal_iff_strictMono_limit (f : Ordinal → Ordinal) : IsNormal f ↔ StrictMono f ∧ ∀ o, IsSuccLimit o → ∀ a, (∀ b < o, f b ≤ a) → f o ≤ a := isNormal_iff theorem IsNormal.lt_iff {f} (H : IsNormal f) {a b} : f a < f b ↔ a < b := StrictMono.lt_iff_lt <| H.strictMono theorem IsNormal.le_iff {f} (H : IsNormal f) {a b} : f a ≤ f b ↔ a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.lt_iff theorem IsNormal.inj {f} (H : IsNormal f) {a b} : f a = f b ↔ a = b := by simp only [le_antisymm_iff, H.le_iff] theorem IsNormal.id_le {f} (H : IsNormal f) : id ≤ f := H.strictMono.id_le theorem IsNormal.le_apply {f} (H : IsNormal f) {a} : a ≤ f a := H.strictMono.le_apply theorem IsNormal.le_iff_eq {f} (H : IsNormal f) {a} : f a ≤ a ↔ f a = a := H.le_apply.ge_iff_eq' theorem IsNormal.le_set {f o} (H : IsNormal f) (p : Set Ordinal) (p0 : p.Nonempty) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f a ≤ o := ⟨fun h _ pa => (H.le_iff.2 ((H₂ _).1 le_rfl _ pa)).trans h, fun h => by induction b using limitRecOn with | zero => obtain ⟨x, px⟩ := p0 have := Ordinal.le_zero.1 ((H₂ _).1 (Ordinal.zero_le _) _ px) rw [this] at px exact h _ px | succ S _ => rcases not_forall₂.1 (mt (H₂ S).2 <| (lt_succ S).not_ge) with ⟨a, h₁, h₂⟩ exact (H.le_iff.2 <| succ_le_of_lt <| not_le.1 h₂).trans (h _ h₁) | limit S L _ => refine (H.le_iff_forall_le L).2 fun a h' => ?_ rcases not_forall₂.1 (mt (H₂ a).2 h'.not_ge) with ⟨b, h₁, h₂⟩ exact (H.le_iff.2 <| (not_le.1 h₂).le).trans (h _ h₁)⟩ theorem IsNormal.le_set' {f o} (H : IsNormal f) (p : Set α) (p0 : p.Nonempty) (g : α → Ordinal) (b) (H₂ : ∀ o, b ≤ o ↔ ∀ a ∈ p, g a ≤ o) : f b ≤ o ↔ ∀ a ∈ p, f (g a) ≤ o := by simpa [H₂] using H.le_set (g '' p) (p0.image g) b theorem IsNormal.refl : IsNormal id := .id theorem IsNormal.trans {f g} (H₁ : IsNormal f) (H₂ : IsNormal g) : IsNormal (f ∘ g) := H₁.comp H₂ theorem IsNormal.isSuccLimit {f} (H : IsNormal f) {o} (ho : IsSuccLimit o) : IsSuccLimit (f o) := H.map_isSuccLimit ho /-! ### Subtraction on ordinals -/ instance existsAddOfLE : ExistsAddOfLE Ordinal where exists_add_of_le {a b} := by refine inductionOn₂ a b fun α r _ β s _ ⟨f⟩ ↦ ?_ obtain ⟨γ, t, _, ⟨g⟩⟩ := f.exists_sum_relIso exact ⟨type t, g.ordinal_type_eq.symm⟩ -- TODO: This gives us `zero_le` as an immediate consequence. -- Private/protect the old theorem, golf proofs. instance canonicallyOrderedAdd : CanonicallyOrderedAdd Ordinal where le_add_self := le_add_left le_self_add := le_add_right /-- `a - b` is the unique ordinal satisfying `b + (a - b) = a` when `b ≤ a`. -/ instance sub : Sub Ordinal where sub a b := if h : b ≤ a then Classical.choose (exists_add_of_le h) else 0 private theorem sub_eq_zero_of_lt {a b : Ordinal} (h : a < b) : a - b = 0 := dif_neg h.not_ge protected theorem add_sub_cancel_of_le {a b : Ordinal} (h : b ≤ a) : b + (a - b) = a := by change b + dite _ _ _ = a rw [dif_pos h] exact (Classical.choose_spec (exists_add_of_le h)).symm @[simp] theorem add_sub_cancel (a b : Ordinal) : a + b - a = b := by simpa using Ordinal.add_sub_cancel_of_le (le_add_right a b) theorem le_add_sub (a b : Ordinal) : a ≤ b + (a - b) := by obtain h | h := le_or_gt b a · exact (Ordinal.add_sub_cancel_of_le h).ge · simpa [sub_eq_zero_of_lt h] using h.le theorem sub_le {a b c : Ordinal} : a - b ≤ c ↔ a ≤ b + c := by refine ⟨fun h ↦ (le_add_sub a b).trans (by gcongr), fun h ↦ ?_⟩ obtain h' | h' := le_or_gt b a · rwa [← add_le_add_iff_left b, Ordinal.add_sub_cancel_of_le h'] · simp [sub_eq_zero_of_lt h'] theorem lt_sub {a b c : Ordinal} : a < b - c ↔ c + a < b := lt_iff_lt_of_le_iff_le sub_le theorem sub_eq_of_add_eq {a b c : Ordinal} (h : a + b = c) : c - a = b := h ▸ add_sub_cancel _ _ theorem sub_le_self (a b : Ordinal) : a - b ≤ a := sub_le.2 <| le_add_left _ _ theorem le_sub_of_le {a b c : Ordinal} (h : b ≤ a) : c ≤ a - b ↔ b + c ≤ a := by rw [← add_le_add_iff_left b, Ordinal.add_sub_cancel_of_le h] theorem sub_lt_of_le {a b c : Ordinal} (h : b ≤ a) : a - b < c ↔ a < b + c := lt_iff_lt_of_le_iff_le (le_sub_of_le h) @[simp] theorem sub_zero (a : Ordinal) : a - 0 = a := by simpa only [zero_add] using add_sub_cancel 0 a @[simp] theorem zero_sub (a : Ordinal) : 0 - a = 0 := by rw [← Ordinal.le_zero]; apply sub_le_self @[simp] theorem sub_self (a : Ordinal) : a - a = 0 := by simpa only [add_zero] using add_sub_cancel a 0 protected theorem sub_eq_zero_iff_le {a b : Ordinal} : a - b = 0 ↔ a ≤ b := ⟨fun h => by simpa only [h, add_zero] using le_add_sub a b, fun h => by rwa [← Ordinal.le_zero, sub_le, add_zero]⟩ protected theorem sub_ne_zero_iff_lt {a b : Ordinal} : a - b ≠ 0 ↔ b < a := by simpa using Ordinal.sub_eq_zero_iff_le.not theorem sub_sub (a b c : Ordinal) : a - b - c = a - (b + c) := eq_of_forall_ge_iff fun d => by rw [sub_le, sub_le, sub_le, add_assoc] @[simp] theorem add_sub_add_cancel (a b c : Ordinal) : a + b - (a + c) = b - c := by rw [← sub_sub, add_sub_cancel] theorem le_sub_of_add_le {a b c : Ordinal} (h : b + c ≤ a) : c ≤ a - b := by rw [← add_le_add_iff_left b] exact h.trans (le_add_sub a b) theorem sub_lt_of_lt_add {a b c : Ordinal} (h : a < b + c) (hc : 0 < c) : a - b < c := by obtain hab | hba := lt_or_ge a b · rwa [Ordinal.sub_eq_zero_iff_le.2 hab.le] · rwa [sub_lt_of_le hba] theorem lt_add_iff {a b c : Ordinal} (hc : c ≠ 0) : a < b + c ↔ ∃ d < c, a ≤ b + d := by use fun h ↦ ⟨_, sub_lt_of_lt_add h hc.bot_lt, le_add_sub a b⟩ rintro ⟨d, hd, ha⟩ exact ha.trans_lt (by gcongr) theorem add_le_iff {a b c : Ordinal} (hb : b ≠ 0) : a + b ≤ c ↔ ∀ d < b, a + d < c := by simpa using (lt_add_iff hb).not theorem lt_add_iff_of_isSuccLimit {a b c : Ordinal} (hc : IsSuccLimit c) : a < b + c ↔ ∃ d < c, a < b + d := by rw [lt_add_iff hc.ne_bot] constructor <;> rintro ⟨d, hd, ha⟩ · refine ⟨_, hc.succ_lt hd, ?_⟩ rwa [add_succ, lt_succ_iff] · exact ⟨d, hd, ha.le⟩ @[deprecated (since := "2025-07-08")] alias lt_add_of_limit := lt_add_iff_of_isSuccLimit theorem add_le_iff_of_isSuccLimit {a b c : Ordinal} (hb : IsSuccLimit b) : a + b ≤ c ↔ ∀ d < b, a + d ≤ c := by simpa using (lt_add_iff_of_isSuccLimit hb).not @[deprecated (since := "2025-07-08")] alias add_le_of_limit := add_le_iff_of_isSuccLimit theorem isNormal_add_right (a : Ordinal) : IsNormal (a + ·) := by rw [isNormal_iff_strictMono_limit] exact ⟨add_right_strictMono, fun _ l _ ↦ (add_le_iff_of_isSuccLimit l).2⟩ theorem isSuccLimit_add (a : Ordinal) {b : Ordinal} : IsSuccLimit b → IsSuccLimit (a + b) := (isNormal_add_right a).isSuccLimit @[deprecated (since := "2025-07-09")] alias isLimit_add := isSuccLimit_add @[deprecated (since := "2025-07-09")] alias IsLimit.add := isSuccLimit_add theorem isSuccLimit_sub {a b : Ordinal} (ha : IsSuccLimit a) (h : b < a) : IsSuccLimit (a - b) := by rw [isSuccLimit_iff, Ordinal.sub_ne_zero_iff_lt, isSuccPrelimit_iff_succ_lt] refine ⟨h, fun c hc ↦ ?_⟩ rw [lt_sub] at hc ⊢ rw [add_succ] exact ha.succ_lt hc @[deprecated (since := "2025-07-09")] alias isLimit_sub := isSuccLimit_sub /-! ### Multiplication of ordinals -/ /-- The multiplication of ordinals `o₁` and `o₂` is the (well-founded) lexicographic order on `o₂ × o₁`. -/ instance monoid : Monoid Ordinal.{u} where mul a b := Quotient.liftOn₂ a b (fun ⟨α, r, _⟩ ⟨β, s, _⟩ => ⟦⟨β × α, Prod.Lex s r, inferInstance⟩⟧ : WellOrder → WellOrder → Ordinal) fun ⟨_, _, _⟩ _ _ _ ⟨f⟩ ⟨g⟩ => Quot.sound ⟨RelIso.prodLexCongr g f⟩ mul_assoc a b c := Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ => Eq.symm <| Quotient.sound ⟨⟨prodAssoc _ _ _, @fun a b => by rcases a with ⟨⟨a₁, a₂⟩, a₃⟩ rcases b with ⟨⟨b₁, b₂⟩, b₃⟩ simp [Prod.lex_def, and_or_left, or_assoc, and_assoc]⟩⟩ mul_one a := inductionOn a fun α r _ => Quotient.sound ⟨⟨punitProd _, @fun a b => by rcases a with ⟨⟨⟨⟩⟩, a⟩; rcases b with ⟨⟨⟨⟩⟩, b⟩ simp only [Prod.lex_def, EmptyRelation, false_or] simp only [true_and] rfl⟩⟩ one_mul a := inductionOn a fun α r _ => Quotient.sound ⟨⟨prodPUnit _, @fun a b => by rcases a with ⟨a, ⟨⟨⟩⟩⟩; rcases b with ⟨b, ⟨⟨⟩⟩⟩ simp only [Prod.lex_def, EmptyRelation, and_false, or_false] rfl⟩⟩ @[simp] theorem type_prod_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [IsWellOrder α r] [IsWellOrder β s] : type (Prod.Lex s r) = type r * type s := rfl private theorem mul_eq_zero' {a b : Ordinal} : a * b = 0 ↔ a = 0 ∨ b = 0 := inductionOn a fun α _ _ => inductionOn b fun β _ _ => by simp_rw [← type_prod_lex, type_eq_zero_iff_isEmpty] rw [or_comm] exact isEmpty_prod instance monoidWithZero : MonoidWithZero Ordinal := { Ordinal.monoid with mul_zero := fun _a => mul_eq_zero'.2 <| Or.inr rfl zero_mul := fun _a => mul_eq_zero'.2 <| Or.inl rfl } instance noZeroDivisors : NoZeroDivisors Ordinal := ⟨fun {_ _} => mul_eq_zero'.1⟩ @[simp] theorem lift_mul (a b : Ordinal.{v}) : lift.{u} (a * b) = lift.{u} a * lift.{u} b := Quotient.inductionOn₂ a b fun ⟨_α, _r, _⟩ ⟨_β, _s, _⟩ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift _).trans (RelIso.prodLexCongr (RelIso.preimage Equiv.ulift _) (RelIso.preimage Equiv.ulift _)).symm⟩ @[simp] theorem card_mul (a b) : card (a * b) = card a * card b := Quotient.inductionOn₂ a b fun ⟨α, _r, _⟩ ⟨β, _s, _⟩ => mul_comm #β #α instance leftDistribClass : LeftDistribClass Ordinal.{u} := ⟨fun a b c => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ => Quotient.sound ⟨⟨sumProdDistrib _ _ _, by rintro ⟨a₁ | a₁, a₂⟩ ⟨b₁ | b₁, b₂⟩ <;> simp only [Prod.lex_def, Sum.lex_inl_inl, Sum.Lex.sep, Sum.lex_inr_inl, Sum.lex_inr_inr, sumProdDistrib_apply_left, sumProdDistrib_apply_right, reduceCtorEq] <;> simp⟩⟩⟩ theorem mul_succ (a b : Ordinal) : a * succ b = a * b + a := mul_add_one a b instance mulLeftMono : MulLeftMono Ordinal.{u} := ⟨fun c a b => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ => by refine (RelEmbedding.ofMonotone (fun a : α × γ => (f a.1, a.2)) fun a b h => ?_).ordinal_type_le obtain ⟨-, -, h'⟩ | ⟨-, h'⟩ := h · exact Prod.Lex.left _ _ (f.toRelEmbedding.map_rel_iff.2 h') · exact Prod.Lex.right _ h'⟩ instance mulRightMono : MulRightMono Ordinal.{u} := ⟨fun c a b => Quotient.inductionOn₃ a b c fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ ⟨f⟩ => by refine (RelEmbedding.ofMonotone (fun a : γ × α => (a.1, f a.2)) fun a b h => ?_).ordinal_type_le obtain ⟨-, -, h'⟩ | ⟨-, h'⟩ := h · exact Prod.Lex.left _ _ h' · exact Prod.Lex.right _ (f.toRelEmbedding.map_rel_iff.2 h')⟩ theorem le_mul_left (a : Ordinal) {b : Ordinal} (hb : 0 < b) : a ≤ a * b := by convert mul_le_mul_left' (one_le_iff_pos.2 hb) a rw [mul_one a] theorem le_mul_right (a : Ordinal) {b : Ordinal} (hb : 0 < b) : a ≤ b * a := by convert mul_le_mul_right' (one_le_iff_pos.2 hb) a rw [one_mul a] private theorem mul_le_of_limit_aux {α β r s} [IsWellOrder α r] [IsWellOrder β s] {c} (h : IsSuccLimit (type s)) (H : ∀ b' < type s, type r * b' ≤ c) (l : c < type r * type s) : False := by suffices ∀ a b, Prod.Lex s r (b, a) (enum _ ⟨_, l⟩) by obtain ⟨b, a⟩ := enum _ ⟨_, l⟩ exact irrefl _ (this _ _) intro a b rw [← typein_lt_typein (Prod.Lex s r), typein_enum] have := H _ (h.succ_lt (typein_lt_type s b)) rw [mul_succ] at this have := ((add_lt_add_iff_left _).2 (typein_lt_type _ a)).trans_le this refine (RelEmbedding.ofMonotone (fun a => ?_) fun a b => ?_).ordinal_type_le.trans_lt this · rcases a with ⟨⟨b', a'⟩, h⟩ by_cases e : b = b' · refine Sum.inr ⟨a', ?_⟩ subst e obtain ⟨-, -, h⟩ | ⟨-, h⟩ := h · exact (irrefl _ h).elim · exact h · refine Sum.inl (⟨b', ?_⟩, a') obtain ⟨-, -, h⟩ | ⟨e, h⟩ := h · exact h · exact (e rfl).elim · rcases a with ⟨⟨b₁, a₁⟩, h₁⟩ rcases b with ⟨⟨b₂, a₂⟩, h₂⟩ intro h by_cases e₁ : b = b₁ <;> by_cases e₂ : b = b₂ · substs b₁ b₂ simpa only [subrel_val, Prod.lex_def, @irrefl _ s _ b, true_and, false_or, eq_self_iff_true, dif_pos, Sum.lex_inr_inr] using h · subst b₁ simp only [subrel_val, Prod.lex_def, e₂, Prod.lex_def, dif_pos, subrel_val, or_false, dif_neg, not_false_iff, Sum.lex_inr_inl, false_and] at h ⊢ obtain ⟨-, -, h₂_h⟩ | e₂ := h₂ <;> [exact asymm h h₂_h; exact e₂ rfl] · simp [e₂, show b₂ ≠ b₁ from e₂ ▸ e₁] · simpa only [dif_neg e₁, dif_neg e₂, Prod.lex_def, subrel_val, Subtype.mk_eq_mk, Sum.lex_inl_inl] using h theorem mul_le_iff_of_isSuccLimit {a b c : Ordinal} (h : IsSuccLimit b) : a * b ≤ c ↔ ∀ b' < b, a * b' ≤ c := ⟨fun h _ l => (mul_le_mul_left' l.le _).trans h, fun H => -- We use the `induction` tactic in order to change `h`/`H` too. le_of_not_gt <| by induction a using inductionOn with | H α r => induction b using inductionOn with | H β s => exact mul_le_of_limit_aux h H⟩ @[deprecated (since := "2025-07-09")] alias mul_le_of_limit := mul_le_iff_of_isSuccLimit theorem isNormal_mul_right {a : Ordinal} (h : 0 < a) : IsNormal (a * ·) := by refine IsNormal.of_succ_lt (fun b ↦ ?_) fun hb ↦ ?_ · simpa [mul_succ] using (add_lt_add_iff_left (a * b)).2 h · simpa [IsLUB, IsLeast, upperBounds, lowerBounds, mul_le_iff_of_isSuccLimit hb] using fun c hc ↦ mul_le_mul_left' hc.le a theorem lt_mul_iff_of_isSuccLimit {a b c : Ordinal} (h : IsSuccLimit c) : a < b * c ↔ ∃ c' < c, a < b * c' := by simpa using (mul_le_iff_of_isSuccLimit h).not @[deprecated (since := "2025-07-09")] alias lt_mul_of_limit := lt_mul_iff_of_isSuccLimit instance : PosMulStrictMono Ordinal where mul_lt_mul_of_pos_left _a ha := (isNormal_mul_right ha).strictMono @[deprecated mul_lt_mul_iff_right₀ (since := "2025-08-26")] theorem mul_lt_mul_iff_left {a b c : Ordinal} (a0 : 0 < a) : a * b < a * c ↔ b < c := mul_lt_mul_iff_right₀ a0 @[deprecated mul_le_mul_left (since := "2025-08-26")] theorem mul_le_mul_iff_left {a b c : Ordinal} (a0 : 0 < a) : a * b ≤ a * c ↔ b ≤ c := mul_le_mul_iff_right₀ a0 @[deprecated mul_lt_mul_of_pos_left (since := "2025-08-26")] protected lemma mul_lt_mul_of_pos_left {a b c : Ordinal} (h : a < b) (c0 : 0 < c) : c * a < c * b := mul_lt_mul_of_pos_left h c0 @[deprecated mul_pos (since := "2025-08-26")] protected theorem mul_pos {a b : Ordinal} (h₁ : 0 < a) (h₂ : 0 < b) : 0 < a * b := mul_pos h₁ h₂ @[deprecated mul_ne_zero (since := "2025-08-26")] protected theorem mul_ne_zero {a b : Ordinal} (ha : a ≠ 0) (hb : b ≠ 0) : a * b ≠ 0 := mul_ne_zero ha hb @[deprecated mul_le_mul_left (since := "2025-08-26")] theorem le_of_mul_le_mul_left {a b c : Ordinal} (h : c * a ≤ c * b) (h0 : 0 < c) : a ≤ b := (mul_le_mul_iff_right₀ h0).mp h @[deprecated mul_left_cancel_iff_of_pos (since := "2025-08-26")] theorem mul_right_inj {a b c : Ordinal} (a0 : 0 < a) : a * b = a * c ↔ b = c := mul_left_cancel_iff_of_pos a0 theorem isSuccLimit_mul {a b : Ordinal} (a0 : 0 < a) : IsSuccLimit b → IsSuccLimit (a * b) := (isNormal_mul_right a0).isSuccLimit @[deprecated (since := "2025-07-09")] alias isLimit_mul := isSuccLimit_mul theorem isSuccLimit_mul_left {a b : Ordinal} (l : IsSuccLimit a) (b0 : 0 < b) : IsSuccLimit (a * b) := by rcases zero_or_succ_or_isSuccLimit b with (rfl | ⟨b, rfl⟩ | lb) · exact b0.false.elim · rw [mul_succ] exact isSuccLimit_add _ l · exact isSuccLimit_mul l.bot_lt lb @[deprecated (since := "2025-07-09")] alias isLimit_mul_left := isSuccLimit_mul_left theorem smul_eq_mul : ∀ (n : ℕ) (a : Ordinal), n • a = a * n | 0, a => by rw [zero_nsmul, Nat.cast_zero, mul_zero] | n + 1, a => by rw [succ_nsmul, Nat.cast_add, mul_add, Nat.cast_one, mul_one, smul_eq_mul n] private theorem add_mul_limit_aux {a b c : Ordinal} (ba : b + a = a) (l : IsSuccLimit c) (IH : ∀ c' < c, (a + b) * succ c' = a * succ c' + b) : (a + b) * c = a * c := le_antisymm ((mul_le_iff_of_isSuccLimit l).2 fun c' h => by grw [le_succ c', IH _ h, le_add_right b a, ba, ← mul_succ, succ_le_of_lt <| l.succ_lt h]) (by grw [← le_add_right]) theorem add_mul_succ {a b : Ordinal} (c) (ba : b + a = a) : (a + b) * succ c = a * succ c + b := by induction c using limitRecOn with | zero => simp only [succ_zero, mul_one] | succ c IH => rw [mul_succ, IH, ← add_assoc, add_assoc _ b, ba, ← mul_succ] | limit c l IH => rw [mul_succ, add_mul_limit_aux ba l IH, mul_succ, add_assoc] theorem add_mul_of_isSuccLimit {a b c : Ordinal} (ba : b + a = a) (l : IsSuccLimit c) : (a + b) * c = a * c := add_mul_limit_aux ba l fun c' _ => add_mul_succ c' ba @[deprecated (since := "2025-07-09")] alias add_mul_limit := add_mul_of_isSuccLimit /-! ### Division on ordinals -/ /-- The set in the definition of division is nonempty. -/ private theorem div_nonempty {a b : Ordinal} (h : b ≠ 0) : { o | a < b * succ o }.Nonempty := ⟨a, (succ_le_iff (a := a) (b := b * succ a)).1 <| by simpa only [succ_zero, one_mul] using mul_le_mul_right' (succ_le_of_lt (Ordinal.pos_iff_ne_zero.2 h)) (succ a)⟩ /-- `a / b` is the unique ordinal `o` satisfying `a = b * o + o'` with `o' < b`. -/ instance div : Div Ordinal := ⟨fun a b => if b = 0 then 0 else sInf { o | a < b * succ o }⟩ @[simp] theorem div_zero (a : Ordinal) : a / 0 = 0 := dif_pos rfl private theorem div_def (a) {b : Ordinal} (h : b ≠ 0) : a / b = sInf { o | a < b * succ o } := dif_neg h theorem lt_mul_succ_div (a) {b : Ordinal} (h : b ≠ 0) : a < b * succ (a / b) := by rw [div_def a h]; exact csInf_mem (div_nonempty h) theorem lt_mul_div_add (a) {b : Ordinal} (h : b ≠ 0) : a < b * (a / b) + b := by simpa only [mul_succ] using lt_mul_succ_div a h theorem div_le {a b c : Ordinal} (b0 : b ≠ 0) : a / b ≤ c ↔ a < b * succ c := ⟨fun h => (lt_mul_succ_div a b0).trans_le (mul_le_mul_left' (succ_le_succ_iff.2 h) _), fun h => by rw [div_def a b0]; exact csInf_le' h⟩ theorem lt_div {a b c : Ordinal} (h : c ≠ 0) : a < b / c ↔ c * succ a ≤ b := by rw [← not_le, div_le h, not_lt] theorem div_pos {b c : Ordinal} (h : c ≠ 0) : 0 < b / c ↔ c ≤ b := by simp [lt_div h] theorem le_div {a b c : Ordinal} (c0 : c ≠ 0) : a ≤ b / c ↔ c * a ≤ b := by induction a using limitRecOn with | zero => simp only [mul_zero, Ordinal.zero_le] | succ _ _ => rw [succ_le_iff, lt_div c0] | limit _ h₁ h₂ => revert h₁ h₂ simp +contextual only [mul_le_iff_of_isSuccLimit, IsSuccLimit.le_iff_forall_le, forall_true_iff] theorem div_lt {a b c : Ordinal} (b0 : b ≠ 0) : a / b < c ↔ a < b * c := lt_iff_lt_of_le_iff_le <| le_div b0 theorem div_le_of_le_mul {a b c : Ordinal} (h : a ≤ b * c) : a / b ≤ c := if b0 : b = 0 then by simp only [b0, div_zero, Ordinal.zero_le] else (div_le b0).2 <| h.trans_lt <| mul_lt_mul_of_pos_left (lt_succ c) (Ordinal.pos_iff_ne_zero.2 b0) theorem mul_lt_of_lt_div {a b c : Ordinal} : a < b / c → c * a < b := lt_imp_lt_of_le_imp_le div_le_of_le_mul @[simp] theorem zero_div (a : Ordinal) : 0 / a = 0 := Ordinal.le_zero.1 <| div_le_of_le_mul <| Ordinal.zero_le _ theorem mul_div_le (a b : Ordinal) : b * (a / b) ≤ a := if b0 : b = 0 then by simp only [b0, zero_mul, Ordinal.zero_le] else (le_div b0).1 le_rfl theorem div_le_left {a b : Ordinal} (h : a ≤ b) (c : Ordinal) : a / c ≤ b / c := by obtain rfl | hc := eq_or_ne c 0 · rw [div_zero, div_zero] · rw [le_div hc] exact (mul_div_le a c).trans h theorem mul_add_div (a) {b : Ordinal} (b0 : b ≠ 0) (c) : (b * a + c) / b = a + c / b := by apply le_antisymm · apply (div_le b0).2 rw [mul_succ, mul_add, add_assoc, add_lt_add_iff_left] apply lt_mul_div_add _ b0 · rw [le_div b0, mul_add, add_le_add_iff_left] apply mul_div_le theorem div_eq_zero_of_lt {a b : Ordinal} (h : a < b) : a / b = 0 := by rw [← Ordinal.le_zero, div_le <| Ordinal.pos_iff_ne_zero.1 <| (Ordinal.zero_le _).trans_lt h] simpa only [succ_zero, mul_one] using h @[simp] theorem mul_div_cancel (a) {b : Ordinal} (b0 : b ≠ 0) : b * a / b = a := by simpa only [add_zero, zero_div] using mul_add_div a b0 0 theorem mul_add_div_mul {a c : Ordinal} (hc : c < a) (b d : Ordinal) : (a * b + c) / (a * d) = b / d := by have ha : a ≠ 0 := ((Ordinal.zero_le c).trans_lt hc).ne' obtain rfl | hd := eq_or_ne d 0 · rw [mul_zero, div_zero, div_zero] · have H := mul_ne_zero ha hd apply le_antisymm · rw [← lt_succ_iff, div_lt H, mul_assoc] · apply (add_lt_add_left hc _).trans_le rw [← mul_succ] gcongr rw [succ_le_iff] exact lt_mul_succ_div b hd · grw [le_div H, mul_assoc, mul_div_le b d, ← le_add_right] theorem mul_div_mul_cancel {a : Ordinal} (ha : a ≠ 0) (b c) : a * b / (a * c) = b / c := by convert mul_add_div_mul (Ordinal.pos_iff_ne_zero.2 ha) b c using 1 rw [add_zero] @[simp] theorem div_one (a : Ordinal) : a / 1 = a := by simpa only [one_mul] using mul_div_cancel a Ordinal.one_ne_zero @[simp] theorem div_self {a : Ordinal} (h : a ≠ 0) : a / a = 1 := by simpa only [mul_one] using mul_div_cancel 1 h theorem mul_sub (a b c : Ordinal) : a * (b - c) = a * b - a * c := if a0 : a = 0 then by simp only [a0, zero_mul, sub_self] else eq_of_forall_ge_iff fun d => by rw [sub_le, ← le_div a0, sub_le, ← le_div a0, mul_add_div _ a0] theorem isSuccLimit_add_iff {a b : Ordinal} : IsSuccLimit (a + b) ↔ IsSuccLimit b ∨ b = 0 ∧ IsSuccLimit a := by constructor <;> intro h · by_cases h' : b = 0 · rw [h', add_zero] at h right exact ⟨h', h⟩ left rw [← add_sub_cancel a b] apply isSuccLimit_sub h suffices a + 0 < a + b by simpa only [add_zero] using this rwa [add_lt_add_iff_left, Ordinal.pos_iff_ne_zero] rcases h with (h | ⟨rfl, h⟩) · exact isSuccLimit_add a h · simpa only [add_zero] theorem isSuccLimit_add_iff_of_isSuccLimit {a b : Ordinal} (h : IsSuccLimit a) : IsSuccLimit (a + b) ↔ IsSuccPrelimit b := by rw [isSuccLimit_add_iff] obtain rfl | hb := eq_or_ne b 0 · simpa · simp [hb, isSuccLimit_iff] @[deprecated (since := "2025-07-09")] alias isLimit_add_iff := isSuccLimit_add_iff theorem dvd_add_iff : ∀ {a b c : Ordinal}, a ∣ b → (a ∣ b + c ↔ a ∣ c) | a, _, c, ⟨b, rfl⟩ => ⟨fun ⟨d, e⟩ => ⟨d - b, by rw [mul_sub, ← e, add_sub_cancel]⟩, fun ⟨d, e⟩ => by rw [e, ← mul_add] apply dvd_mul_right⟩ theorem div_mul_cancel : ∀ {a b : Ordinal}, a ≠ 0 → a ∣ b → a * (b / a) = b | a, _, a0, ⟨b, rfl⟩ => by rw [mul_div_cancel _ a0] theorem le_of_dvd : ∀ {a b : Ordinal}, b ≠ 0 → a ∣ b → a ≤ b | a, _, b0, ⟨b, e⟩ => by subst e simpa only [mul_one] using mul_le_mul_left' (one_le_iff_ne_zero.2 fun h : b = 0 => by simp [h] at b0) a theorem dvd_antisymm {a b : Ordinal} (h₁ : a ∣ b) (h₂ : b ∣ a) : a = b := if a0 : a = 0 then by subst a; exact (eq_zero_of_zero_dvd h₁).symm else if b0 : b = 0 then by subst b; exact eq_zero_of_zero_dvd h₂ else (le_of_dvd b0 h₁).antisymm (le_of_dvd a0 h₂) instance isAntisymm : IsAntisymm Ordinal (· ∣ ·) := ⟨@dvd_antisymm⟩ /-- `a % b` is the unique ordinal `o'` satisfying `a = b * o + o'` with `o' < b`. -/ instance mod : Mod Ordinal := ⟨fun a b => a - b * (a / b)⟩ theorem mod_def (a b : Ordinal) : a % b = a - b * (a / b) := rfl theorem mod_le (a b : Ordinal) : a % b ≤ a := sub_le_self a _ @[simp] theorem mod_zero (a : Ordinal) : a % 0 = a := by simp only [mod_def, div_zero, zero_mul, sub_zero] theorem mod_eq_of_lt {a b : Ordinal} (h : a < b) : a % b = a := by simp only [mod_def, div_eq_zero_of_lt h, mul_zero, sub_zero] @[simp] theorem zero_mod (b : Ordinal) : 0 % b = 0 := by simp only [mod_def, zero_div, mul_zero, sub_self] theorem div_add_mod (a b : Ordinal) : b * (a / b) + a % b = a := Ordinal.add_sub_cancel_of_le <| mul_div_le _ _ theorem mod_lt (a) {b : Ordinal} (h : b ≠ 0) : a % b < b := (add_lt_add_iff_left (b * (a / b))).1 <| by rw [div_add_mod]; exact lt_mul_div_add a h @[simp] theorem mod_self (a : Ordinal) : a % a = 0 := if a0 : a = 0 then by simp only [a0, zero_mod] else by simp only [mod_def, div_self a0, mul_one, sub_self] @[simp] theorem mod_one (a : Ordinal) : a % 1 = 0 := by simp only [mod_def, div_one, one_mul, sub_self] theorem dvd_of_mod_eq_zero {a b : Ordinal} (H : a % b = 0) : b ∣ a := ⟨a / b, by simpa [H] using (div_add_mod a b).symm⟩ theorem mod_eq_zero_of_dvd {a b : Ordinal} (H : b ∣ a) : a % b = 0 := by rcases H with ⟨c, rfl⟩ rcases eq_or_ne b 0 with (rfl | hb) · simp · simp [mod_def, hb] theorem dvd_iff_mod_eq_zero {a b : Ordinal} : b ∣ a ↔ a % b = 0 := ⟨mod_eq_zero_of_dvd, dvd_of_mod_eq_zero⟩ @[simp] theorem mul_add_mod_self (x y z : Ordinal) : (x * y + z) % x = z % x := by rcases eq_or_ne x 0 with rfl | hx · simp · rwa [mod_def, mul_add_div, mul_add, ← sub_sub, add_sub_cancel, mod_def] @[simp] theorem mul_mod (x y : Ordinal) : x * y % x = 0 := by simpa using mul_add_mod_self x y 0 theorem mul_add_mod_mul {w x : Ordinal} (hw : w < x) (y z : Ordinal) : (x * y + w) % (x * z) = x * (y % z) + w := by rw [mod_def, mul_add_div_mul hw] apply sub_eq_of_add_eq rw [← add_assoc, mul_assoc, ← mul_add, div_add_mod] theorem mul_mod_mul (x y z : Ordinal) : (x * y) % (x * z) = x * (y % z) := by obtain rfl | hx := Ordinal.eq_zero_or_pos x · simp · convert mul_add_mod_mul hx y z using 1 <;> rw [add_zero] theorem mod_mod_of_dvd (a : Ordinal) {b c : Ordinal} (h : c ∣ b) : a % b % c = a % c := by nth_rw 2 [← div_add_mod a b] rcases h with ⟨d, rfl⟩ rw [mul_assoc, mul_add_mod_self] @[simp] theorem mod_mod (a b : Ordinal) : a % b % b = a % b := mod_mod_of_dvd a dvd_rfl /-! ### Casting naturals into ordinals, compatibility with operations -/ instance instCharZero : CharZero Ordinal := by refine ⟨fun a b h ↦ ?_⟩ rwa [← Cardinal.ord_nat, ← Cardinal.ord_nat, Cardinal.ord_inj, Nat.cast_inj] at h @[simp] theorem one_add_natCast (m : ℕ) : 1 + (m : Ordinal) = succ m := by rw [← Nat.cast_one, ← Nat.cast_add, add_comm] rfl @[simp] theorem one_add_ofNat (m : ℕ) [m.AtLeastTwo] : 1 + (ofNat(m) : Ordinal) = Order.succ (OfNat.ofNat m : Ordinal) := one_add_natCast m @[simp, norm_cast] theorem natCast_mul (m : ℕ) : ∀ n : ℕ, ((m * n : ℕ) : Ordinal) = m * n | 0 => by simp | n + 1 => by rw [Nat.mul_succ, Nat.cast_add, natCast_mul m n, Nat.cast_succ, mul_add_one] @[simp, norm_cast] theorem natCast_sub (m n : ℕ) : ((m - n : ℕ) : Ordinal) = m - n := by rcases le_total m n with h | h · rw [tsub_eq_zero_iff_le.2 h, Ordinal.sub_eq_zero_iff_le.2 (Nat.cast_le.2 h), Nat.cast_zero] · rw [← add_left_cancel_iff (a := ↑n), ← Nat.cast_add, add_tsub_cancel_of_le h, Ordinal.add_sub_cancel_of_le (Nat.cast_le.2 h)] @[simp, norm_cast] theorem natCast_div (m n : ℕ) : ((m / n : ℕ) : Ordinal) = m / n := by rcases eq_or_ne n 0 with (rfl | hn) · simp · have hn' : (n : Ordinal) ≠ 0 := Nat.cast_ne_zero.2 hn apply le_antisymm · rw [le_div hn', ← natCast_mul, Nat.cast_le, mul_comm] apply Nat.div_mul_le_self · rw [div_le hn', ← add_one_eq_succ, ← Nat.cast_succ, ← natCast_mul, Nat.cast_lt, mul_comm, ← Nat.div_lt_iff_lt_mul (Nat.pos_of_ne_zero hn)] apply Nat.lt_succ_self @[simp, norm_cast] theorem natCast_mod (m n : ℕ) : ((m % n : ℕ) : Ordinal) = m % n := by rw [← add_left_cancel_iff, div_add_mod, ← natCast_div, ← natCast_mul, ← Nat.cast_add, Nat.div_add_mod] @[simp] theorem lift_natCast : ∀ n : ℕ, lift.{u, v} n = n | 0 => by simp | n + 1 => by simp [lift_natCast n] @[simp] theorem lift_ofNat (n : ℕ) [n.AtLeastTwo] : lift.{u, v} ofNat(n) = OfNat.ofNat n := lift_natCast n /-! ### Properties of `ω` -/ theorem lt_omega0 {o : Ordinal} : o < ω ↔ ∃ n : ℕ, o = n := by simp_rw [← Cardinal.ord_aleph0, Cardinal.lt_ord, lt_aleph0, card_eq_nat] theorem nat_lt_omega0 (n : ℕ) : ↑n < ω := lt_omega0.2 ⟨_, rfl⟩ theorem eq_nat_or_omega0_le (o : Ordinal) : (∃ n : ℕ, o = n) ∨ ω ≤ o := by obtain ho | ho := lt_or_ge o ω · exact Or.inl <| lt_omega0.1 ho · exact Or.inr ho @[simp] theorem omega0_pos : 0 < ω := nat_lt_omega0 0 @[simp] theorem omega0_ne_zero : ω ≠ 0 := omega0_pos.ne' @[simp] theorem one_lt_omega0 : 1 < ω := by simpa only [Nat.cast_one] using nat_lt_omega0 1 theorem isSuccLimit_omega0 : IsSuccLimit ω := by rw [isSuccLimit_iff, isSuccPrelimit_iff_succ_lt] refine ⟨omega0_ne_zero, fun o h => ?_⟩ obtain ⟨n, rfl⟩ := lt_omega0.1 h exact nat_lt_omega0 (n + 1) @[deprecated (since := "2025-07-09")] alias isLimit_omega0 := isSuccLimit_omega0 @[deprecated (since := "2025-07-09")] alias nat_lt_limit := natCast_lt_of_isSuccLimit theorem omega0_le {o : Ordinal} : ω ≤ o ↔ ∀ n : ℕ, ↑n ≤ o := ⟨fun h n => (nat_lt_omega0 _).le.trans h, fun H => le_of_forall_lt fun a h => by let ⟨n, e⟩ := lt_omega0.1 h rw [e, ← succ_le_iff]; exact H (n + 1)⟩ theorem omega0_le_of_isSuccLimit {o} (h : IsSuccLimit o) : ω ≤ o := omega0_le.2 fun n => le_of_lt <| natCast_lt_of_isSuccLimit h n @[deprecated (since := "2025-07-09")] alias omega0_le_of_isLimit := omega0_le_of_isSuccLimit theorem natCast_add_omega0 (n : ℕ) : n + ω = ω := by refine le_antisymm (le_of_forall_lt fun a ha ↦ ?_) (le_add_left _ _) obtain ⟨b, hb', hb⟩ := (lt_add_iff omega0_ne_zero).1 ha obtain ⟨m, rfl⟩ := lt_omega0.1 hb' apply hb.trans_lt exact_mod_cast nat_lt_omega0 (n + m) theorem one_add_omega0 : 1 + ω = ω := mod_cast natCast_add_omega0 1 theorem add_omega0 {a : Ordinal} (h : a < ω) : a + ω = ω := by obtain ⟨n, rfl⟩ := lt_omega0.1 h exact natCast_add_omega0 n @[simp] theorem natCast_add_of_omega0_le {o} (h : ω ≤ o) (n : ℕ) : n + o = o := by rw [← Ordinal.add_sub_cancel_of_le h, ← add_assoc, natCast_add_omega0] @[simp] theorem one_add_of_omega0_le {o} (h : ω ≤ o) : 1 + o = o := mod_cast natCast_add_of_omega0_le h 1 open Ordinal -- TODO: prove `IsSuccPrelimit a ↔ ω ∣ a`. theorem isSuccLimit_iff_omega0_dvd {a : Ordinal} : IsSuccLimit a ↔ a ≠ 0 ∧ ω ∣ a := by refine ⟨fun l => ⟨l.ne_bot, ⟨a / ω, le_antisymm ?_ (mul_div_le _ _)⟩⟩, fun h => ?_⟩ · refine l.le_iff_forall_le.2 fun x hx => le_of_lt ?_ rw [← div_lt omega0_ne_zero, ← succ_le_iff, le_div omega0_ne_zero, mul_succ, add_le_iff_of_isSuccLimit isSuccLimit_omega0] intro b hb rcases lt_omega0.1 hb with ⟨n, rfl⟩ grw [mul_div_le] exact (lt_sub.1 <| natCast_lt_of_isSuccLimit (isSuccLimit_sub l hx) _).le · rcases h with ⟨a0, b, rfl⟩ refine isSuccLimit_mul_left isSuccLimit_omega0 (Ordinal.pos_iff_ne_zero.2 <| mt ?_ a0) intro e simp only [e, mul_zero] @[deprecated (since := "2025-07-09")] alias isLimit_iff_omega0_dvd := isSuccLimit_iff_omega0_dvd @[simp] theorem natCast_mod_omega0 (n : ℕ) : n % ω = n := mod_eq_of_lt (nat_lt_omega0 n) end Ordinal namespace Cardinal open Ordinal @[simp] theorem add_one_of_aleph0_le {c} (h : ℵ₀ ≤ c) : c + 1 = c := by rw [add_comm, ← card_ord c, ← card_one, ← card_add, one_add_of_omega0_le] rwa [← ord_aleph0, ord_le_ord] theorem isSuccLimit_ord {c} (co : ℵ₀ ≤ c) : IsSuccLimit (ord c) := by rw [isSuccLimit_iff, isSuccPrelimit_iff_succ_lt] refine ⟨fun h => aleph0_ne_zero ?_, fun a => lt_imp_lt_of_le_imp_le fun h => ?_⟩ · rw [← Ordinal.le_zero, ord_le] at h simpa only [card_zero, nonpos_iff_eq_zero] using co.trans h · rw [ord_le] at h ⊢ rwa [← @add_one_of_aleph0_le (card a), ← card_succ] rw [← ord_le, ← IsSuccLimit.le_succ_iff, ord_le] · exact co.trans h · rw [ord_aleph0] exact Ordinal.isSuccLimit_omega0 @[deprecated (since := "2025-07-09")] alias isLimit_ord := isSuccLimit_ord theorem noMaxOrder {c} (h : ℵ₀ ≤ c) : NoMaxOrder c.ord.toType := toType_noMax_of_succ_lt fun _ ↦ (isSuccLimit_ord h).succ_lt end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Basic.lean
import Mathlib.Algebra.Order.SuccPred import Mathlib.Data.Sum.Order import Mathlib.Order.IsNormal import Mathlib.SetTheory.Cardinal.Basic import Mathlib.Tactic.PPWithUniv /-! # Ordinals Ordinals are defined as equivalences of well-ordered sets under order isomorphism. They are endowed with a total order, where an ordinal is smaller than another one if it embeds into it as an initial segment (or, equivalently, in any way). This total order is well founded. ## Main definitions * `Ordinal`: the type of ordinals (in a given universe) * `Ordinal.type r`: given a well-founded order `r`, this is the corresponding ordinal * `Ordinal.typein r a`: given a well-founded order `r` on a type `α`, and `a : α`, the ordinal corresponding to all elements smaller than `a`. * `enum r ⟨o, h⟩`: given a well-order `r` on a type `α`, and an ordinal `o` strictly smaller than the ordinal corresponding to `r` (this is the assumption `h`), returns the `o`-th element of `α`. In other words, the elements of `α` can be enumerated using ordinals up to `type r`. * `Ordinal.card o`: the cardinality of an ordinal `o`. * `Ordinal.lift` lifts an ordinal in universe `u` to an ordinal in universe `max u v`. For a version registering additionally that this is an initial segment embedding, see `Ordinal.liftInitialSeg`. For a version registering that it is a principal segment embedding if `u < v`, see `Ordinal.liftPrincipalSeg`. * `Ordinal.omega0` or `ω` is the order type of `ℕ`. It is called this to match `Cardinal.aleph0` and so that the omega function can be named `Ordinal.omega`. This definition is universe polymorphic: `Ordinal.omega0.{u} : Ordinal.{u}` (contrast with `ℕ : Type`, which lives in a specific universe). In some cases the universe level has to be given explicitly. * `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. The main properties of addition (and the other operations on ordinals) are stated and proved in `Mathlib/SetTheory/Ordinal/Arithmetic.lean`. Here, we only introduce it and prove its basic properties to deduce the fact that the order on ordinals is total (and well founded). * `succ o` is the successor of the ordinal `o`. * `Cardinal.ord c`: when `c` is a cardinal, `ord c` is the smallest ordinal with this cardinality. It is the canonical way to represent a cardinal with an ordinal. A conditionally complete linear order with bot structure is registered on ordinals, where `⊥` is `0`, the ordinal corresponding to the empty type, and `Inf` is the minimum for nonempty sets and `0` for the empty set by convention. ## Notation * `ω` is a notation for the first infinite ordinal in the scope `Ordinal`. -/ assert_not_exists Module Field noncomputable section open Function Cardinal Set Equiv Order open scoped Cardinal InitialSeg universe u v w variable {α : Type u} {β : Type v} {γ : Type w} {r : α → α → Prop} {s : β → β → Prop} {t : γ → γ → Prop} /-! ### Definition of ordinals -/ /-- Bundled structure registering a well order on a type. Ordinals will be defined as a quotient of this type. -/ structure WellOrder : Type (u + 1) where /-- The underlying type of the order. -/ α : Type u /-- The underlying relation of the order. -/ r : α → α → Prop /-- The proposition that `r` is a well-ordering for `α`. -/ wo : IsWellOrder α r attribute [instance] WellOrder.wo namespace WellOrder instance inhabited : Inhabited WellOrder := ⟨⟨PEmpty, _, inferInstanceAs (IsWellOrder PEmpty EmptyRelation)⟩⟩ end WellOrder /-- Equivalence relation on well orders on arbitrary types in universe `u`, given by order isomorphism. -/ instance Ordinal.isEquivalent : Setoid WellOrder where r := fun ⟨_, r, _⟩ ⟨_, s, _⟩ => Nonempty (r ≃r s) iseqv := ⟨fun _ => ⟨RelIso.refl _⟩, fun ⟨e⟩ => ⟨e.symm⟩, fun ⟨e₁⟩ ⟨e₂⟩ => ⟨e₁.trans e₂⟩⟩ /-- `Ordinal.{u}` is the type of well orders in `Type u`, up to order isomorphism. -/ @[pp_with_univ] def Ordinal : Type (u + 1) := Quotient Ordinal.isEquivalent /-- A "canonical" type order-isomorphic to the ordinal `o`, living in the same universe. This is defined through the axiom of choice. Use this over `Iio o` only when it is paramount to have a `Type u` rather than a `Type (u + 1)`. -/ def Ordinal.toType (o : Ordinal.{u}) : Type u := o.out.α instance hasWellFounded_toType (o : Ordinal) : WellFoundedRelation o.toType := ⟨o.out.r, o.out.wo.wf⟩ instance linearOrder_toType (o : Ordinal) : LinearOrder o.toType := @IsWellOrder.linearOrder _ o.out.r o.out.wo instance wellFoundedLT_toType_lt (o : Ordinal) : WellFoundedLT o.toType := o.out.wo.toIsWellFounded namespace Ordinal noncomputable instance (o : Ordinal) : SuccOrder o.toType := SuccOrder.ofLinearWellFoundedLT o.toType /-! ### Basic properties of the order type -/ /-- The order type of a well order is an ordinal. -/ def type (r : α → α → Prop) [wo : IsWellOrder α r] : Ordinal := ⟦⟨α, r, wo⟩⟧ /-- `typeLT α` is an abbreviation for the order type of the `<` relation of `α`. -/ scoped notation "typeLT " α:70 => @Ordinal.type α (· < ·) inferInstance instance zero : Zero Ordinal := ⟨type <| @EmptyRelation PEmpty⟩ instance inhabited : Inhabited Ordinal := ⟨0⟩ instance one : One Ordinal := ⟨type <| @EmptyRelation PUnit⟩ @[simp] theorem type_toType (o : Ordinal) : typeLT o.toType = o := o.out_eq theorem type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] : type r = type s ↔ Nonempty (r ≃r s) := Quotient.eq' theorem _root_.RelIso.ordinal_type_eq {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (h : r ≃r s) : type r = type s := type_eq.2 ⟨h⟩ theorem type_eq_zero_of_empty (r) [IsWellOrder α r] [IsEmpty α] : type r = 0 := (RelIso.relIsoOfIsEmpty r _).ordinal_type_eq @[simp] theorem type_eq_zero_iff_isEmpty [IsWellOrder α r] : type r = 0 ↔ IsEmpty α := ⟨fun h => let ⟨s⟩ := type_eq.1 h s.toEquiv.isEmpty, @type_eq_zero_of_empty α r _⟩ theorem type_ne_zero_iff_nonempty [IsWellOrder α r] : type r ≠ 0 ↔ Nonempty α := by simp theorem type_ne_zero_of_nonempty (r) [IsWellOrder α r] [h : Nonempty α] : type r ≠ 0 := type_ne_zero_iff_nonempty.2 h theorem type_pEmpty : type (@EmptyRelation PEmpty) = 0 := rfl theorem type_empty : type (@EmptyRelation Empty) = 0 := type_eq_zero_of_empty _ theorem type_eq_one_of_unique (r) [IsWellOrder α r] [Nonempty α] [Subsingleton α] : type r = 1 := by cases nonempty_unique α exact (RelIso.ofUniqueOfIrrefl r _).ordinal_type_eq @[simp] theorem type_eq_one_iff_unique [IsWellOrder α r] : type r = 1 ↔ Nonempty (Unique α) := ⟨fun h ↦ let ⟨s⟩ := type_eq.1 h; ⟨s.toEquiv.unique⟩, fun ⟨_⟩ ↦ type_eq_one_of_unique r⟩ theorem type_pUnit : type (@EmptyRelation PUnit) = 1 := rfl theorem type_unit : type (@EmptyRelation Unit) = 1 := rfl @[simp] theorem toType_empty_iff_eq_zero {o : Ordinal} : IsEmpty o.toType ↔ o = 0 := by rw [← @type_eq_zero_iff_isEmpty o.toType (· < ·), type_toType] instance isEmpty_toType_zero : IsEmpty (toType 0) := toType_empty_iff_eq_zero.2 rfl @[simp] theorem toType_nonempty_iff_ne_zero {o : Ordinal} : Nonempty o.toType ↔ o ≠ 0 := by rw [← @type_ne_zero_iff_nonempty o.toType (· < ·), type_toType] protected theorem one_ne_zero : (1 : Ordinal) ≠ 0 := type_ne_zero_of_nonempty _ instance nontrivial : Nontrivial Ordinal.{u} := ⟨⟨1, 0, Ordinal.one_ne_zero⟩⟩ /-- `Quotient.inductionOn` specialized to ordinals. Not to be confused with well-founded recursion `Ordinal.induction`. -/ @[elab_as_elim] theorem inductionOn {C : Ordinal → Prop} (o : Ordinal) (H : ∀ (α r) [IsWellOrder α r], C (type r)) : C o := Quot.inductionOn o fun ⟨α, r, wo⟩ => @H α r wo /-- `Quotient.inductionOn₂` specialized to ordinals. Not to be confused with well-founded recursion `Ordinal.induction`. -/ @[elab_as_elim] theorem inductionOn₂ {C : Ordinal → Ordinal → Prop} (o₁ o₂ : Ordinal) (H : ∀ (α r) [IsWellOrder α r] (β s) [IsWellOrder β s], C (type r) (type s)) : C o₁ o₂ := Quotient.inductionOn₂ o₁ o₂ fun ⟨α, r, wo₁⟩ ⟨β, s, wo₂⟩ => @H α r wo₁ β s wo₂ /-- `Quotient.inductionOn₃` specialized to ordinals. Not to be confused with well-founded recursion `Ordinal.induction`. -/ @[elab_as_elim] theorem inductionOn₃ {C : Ordinal → Ordinal → Ordinal → Prop} (o₁ o₂ o₃ : Ordinal) (H : ∀ (α r) [IsWellOrder α r] (β s) [IsWellOrder β s] (γ t) [IsWellOrder γ t], C (type r) (type s) (type t)) : C o₁ o₂ o₃ := Quotient.inductionOn₃ o₁ o₂ o₃ fun ⟨α, r, wo₁⟩ ⟨β, s, wo₂⟩ ⟨γ, t, wo₃⟩ => @H α r wo₁ β s wo₂ γ t wo₃ open Classical in /-- To prove a result on ordinals, it suffices to prove it for order types of well-orders. -/ @[elab_as_elim] theorem inductionOnWellOrder {C : Ordinal → Prop} (o : Ordinal) (H : ∀ (α) [LinearOrder α] [WellFoundedLT α], C (typeLT α)) : C o := inductionOn o fun α r wo ↦ @H α (linearOrderOfSTO r) wo.toIsWellFounded open Classical in /-- To define a function on ordinals, it suffices to define them on order types of well-orders. Since `LinearOrder` is data-carrying, `liftOnWellOrder_type` is not a definitional equality, unlike `Quotient.liftOn_mk` which is always def-eq. -/ def liftOnWellOrder {δ : Sort v} (o : Ordinal) (f : ∀ (α) [LinearOrder α] [WellFoundedLT α], δ) (c : ∀ (α) [LinearOrder α] [WellFoundedLT α] (β) [LinearOrder β] [WellFoundedLT β], typeLT α = typeLT β → f α = f β) : δ := Quotient.liftOn o (fun w ↦ @f w.α (linearOrderOfSTO w.r) w.wo.toIsWellFounded) fun w₁ w₂ h ↦ @c w₁.α (linearOrderOfSTO w₁.r) w₁.wo.toIsWellFounded w₂.α (linearOrderOfSTO w₂.r) w₂.wo.toIsWellFounded (Quotient.sound h) @[simp] theorem liftOnWellOrder_type {δ : Sort v} (f : ∀ (α) [LinearOrder α] [WellFoundedLT α], δ) (c : ∀ (α) [LinearOrder α] [WellFoundedLT α] (β) [LinearOrder β] [WellFoundedLT β], typeLT α = typeLT β → f α = f β) {γ} [LinearOrder γ] [WellFoundedLT γ] : liftOnWellOrder (typeLT γ) f c = f γ := by change Quotient.liftOn' ⟦_⟧ _ _ = _ rw [Quotient.liftOn'_mk] congr exact LinearOrder.ext_lt fun _ _ ↦ Iff.rfl /-! ### The order on ordinals -/ /-- For `Ordinal`: * less-equal is defined such that well orders `r` and `s` satisfy `type r ≤ type s` if there exists a function embedding `r` as an *initial* segment of `s`. * less-than is defined such that well orders `r` and `s` satisfy `type r < type s` if there exists a function embedding `r` as a *principal* segment of `s`. Note that most of the relevant results on initial and principal segments are proved in the `Order.InitialSeg` file. -/ instance partialOrder : PartialOrder Ordinal where le a b := Quotient.liftOn₂ a b (fun ⟨_, r, _⟩ ⟨_, s, _⟩ => Nonempty (r ≼i s)) fun _ _ _ _ ⟨f⟩ ⟨g⟩ => propext ⟨fun ⟨h⟩ => ⟨f.symm.toInitialSeg.trans <| h.trans g.toInitialSeg⟩, fun ⟨h⟩ => ⟨f.toInitialSeg.trans <| h.trans g.symm.toInitialSeg⟩⟩ lt a b := Quotient.liftOn₂ a b (fun ⟨_, r, _⟩ ⟨_, s, _⟩ => Nonempty (r ≺i s)) fun _ _ _ _ ⟨f⟩ ⟨g⟩ => propext ⟨fun ⟨h⟩ => ⟨PrincipalSeg.relIsoTrans f.symm <| h.transRelIso g⟩, fun ⟨h⟩ => ⟨PrincipalSeg.relIsoTrans f <| h.transRelIso g.symm⟩⟩ le_refl := Quot.ind fun ⟨_, _, _⟩ => ⟨InitialSeg.refl _⟩ le_trans a b c := Quotient.inductionOn₃ a b c fun _ _ _ ⟨f⟩ ⟨g⟩ => ⟨f.trans g⟩ lt_iff_le_not_ge a b := Quotient.inductionOn₂ a b fun _ _ => ⟨fun ⟨f⟩ => ⟨⟨f⟩, fun ⟨g⟩ => (f.transInitial g).irrefl⟩, fun ⟨⟨f⟩, h⟩ => f.principalSumRelIso.recOn (fun g => ⟨g⟩) fun g => (h ⟨g.symm.toInitialSeg⟩).elim⟩ le_antisymm a b := Quotient.inductionOn₂ a b fun _ _ ⟨h₁⟩ ⟨h₂⟩ => Quot.sound ⟨InitialSeg.antisymm h₁ h₂⟩ instance : LinearOrder Ordinal := {inferInstanceAs (PartialOrder Ordinal) with le_total := fun a b => Quotient.inductionOn₂ a b fun ⟨_, r, _⟩ ⟨_, s, _⟩ => (InitialSeg.total r s).recOn (fun f => Or.inl ⟨f⟩) fun f => Or.inr ⟨f⟩ toDecidableLE := Classical.decRel _ } theorem _root_.InitialSeg.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (h : r ≼i s) : type r ≤ type s := ⟨h⟩ theorem _root_.RelEmbedding.ordinal_type_le {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (h : r ↪r s) : type r ≤ type s := ⟨h.collapse⟩ theorem _root_.PrincipalSeg.ordinal_type_lt {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (h : r ≺i s) : type r < type s := ⟨h⟩ protected theorem zero_le (o : Ordinal) : 0 ≤ o := inductionOn o fun _ r _ => (InitialSeg.ofIsEmpty _ r).ordinal_type_le instance : OrderBot Ordinal where bot := 0 bot_le := Ordinal.zero_le @[simp] theorem bot_eq_zero : (⊥ : Ordinal) = 0 := rfl instance instIsEmptyIioZero : IsEmpty (Iio (0 : Ordinal)) := by simp [← bot_eq_zero] protected theorem le_zero {o : Ordinal} : o ≤ 0 ↔ o = 0 := le_bot_iff protected theorem pos_iff_ne_zero {o : Ordinal} : 0 < o ↔ o ≠ 0 := bot_lt_iff_ne_bot @[simp] protected theorem not_lt_zero (o : Ordinal) : ¬o < 0 := not_lt_bot theorem eq_zero_or_pos : ∀ a : Ordinal, a = 0 ∨ 0 < a := eq_bot_or_bot_lt instance : ZeroLEOneClass Ordinal := ⟨Ordinal.zero_le _⟩ instance instNeZeroOne : NeZero (1 : Ordinal) := ⟨Ordinal.one_ne_zero⟩ theorem type_le_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] : type r ≤ type s ↔ Nonempty (r ≼i s) := Iff.rfl theorem type_le_iff' {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] : type r ≤ type s ↔ Nonempty (r ↪r s) := ⟨fun ⟨f⟩ => ⟨f⟩, fun ⟨f⟩ => ⟨f.collapse⟩⟩ theorem type_lt_iff {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] : type r < type s ↔ Nonempty (r ≺i s) := Iff.rfl /-- Given two ordinals `α ≤ β`, then `initialSegToType α β` is the initial segment embedding of `α.toType` into `β.toType`. -/ def initialSegToType {α β : Ordinal} (h : α ≤ β) : α.toType ≤i β.toType := by apply Classical.choice (type_le_iff.mp _) rwa [type_toType, type_toType] /-- Given two ordinals `α < β`, then `principalSegToType α β` is the principal segment embedding of `α.toType` into `β.toType`. -/ def principalSegToType {α β : Ordinal} (h : α < β) : α.toType <i β.toType := by apply Classical.choice (type_lt_iff.mp _) rwa [type_toType, type_toType] /-! ### Enumerating elements in a well-order with ordinals -/ /-- The order type of an element inside a well order. This is registered as a principal segment embedding into the ordinals, with top `type r`. -/ def typein (r : α → α → Prop) [IsWellOrder α r] : @PrincipalSeg α Ordinal.{u} r (· < ·) := by refine ⟨RelEmbedding.ofMonotone _ fun a b ha ↦ ((PrincipalSeg.ofElement r a).codRestrict _ ?_ ?_).ordinal_type_lt, type r, fun a ↦ ⟨?_, ?_⟩⟩ · rintro ⟨c, hc⟩ exact trans hc ha · exact ha · rintro ⟨b, rfl⟩ exact (PrincipalSeg.ofElement _ _).ordinal_type_lt · refine inductionOn a ?_ rintro β s wo ⟨g⟩ exact ⟨_, g.subrelIso.ordinal_type_eq⟩ @[simp] theorem type_subrel (r : α → α → Prop) [IsWellOrder α r] (a : α) : type (Subrel r (r · a)) = typein r a := rfl @[simp] theorem top_typein (r : α → α → Prop) [IsWellOrder α r] : (typein r).top = type r := rfl theorem typein_lt_type (r : α → α → Prop) [IsWellOrder α r] (a : α) : typein r a < type r := (typein r).lt_top a theorem typein_lt_self {o : Ordinal} (i : o.toType) : typein (α := o.toType) (· < ·) i < o := by simp_rw [← type_toType o] apply typein_lt_type @[simp] theorem typein_top {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≺i s) : typein s f.top = type r := f.subrelIso.ordinal_type_eq @[simp] theorem typein_lt_typein (r : α → α → Prop) [IsWellOrder α r] {a b : α} : typein r a < typein r b ↔ r a b := (typein r).map_rel_iff @[simp] theorem typein_le_typein (r : α → α → Prop) [IsWellOrder α r] {a b : α} : typein r a ≤ typein r b ↔ ¬r b a := by rw [← not_lt, typein_lt_typein] theorem typein_injective (r : α → α → Prop) [IsWellOrder α r] : Injective (typein r) := (typein r).injective theorem typein_inj (r : α → α → Prop) [IsWellOrder α r] {a b} : typein r a = typein r b ↔ a = b := (typein_injective r).eq_iff theorem mem_range_typein_iff (r : α → α → Prop) [IsWellOrder α r] {o} : o ∈ Set.range (typein r) ↔ o < type r := (typein r).mem_range_iff_rel theorem typein_surj (r : α → α → Prop) [IsWellOrder α r] {o} (h : o < type r) : o ∈ Set.range (typein r) := (typein r).mem_range_of_rel_top h theorem typein_surjOn (r : α → α → Prop) [IsWellOrder α r] : Set.SurjOn (typein r) Set.univ (Set.Iio (type r)) := (typein r).surjOn /-- A well order `r` is order-isomorphic to the set of ordinals smaller than `type r`. `enum r ⟨o, h⟩` is the `o`-th element of `α` ordered by `r`. That is, `enum` maps an initial segment of the ordinals, those less than the order type of `r`, to the elements of `α`. -/ @[simps! symm_apply_coe] def enum (r : α → α → Prop) [IsWellOrder α r] : (· < · : Iio (type r) → Iio (type r) → Prop) ≃r r := (typein r).subrelIso @[simp] theorem typein_enum (r : α → α → Prop) [IsWellOrder α r] {o} (h : o < type r) : typein r (enum r ⟨o, h⟩) = o := (typein r).apply_subrelIso _ theorem enum_type {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : s ≺i r) {h : type s < type r} : enum r ⟨type s, h⟩ = f.top := (typein r).injective <| (typein_enum _ _).trans (typein_top _).symm @[simp] theorem enum_typein (r : α → α → Prop) [IsWellOrder α r] (a : α) : enum r ⟨typein r a, typein_lt_type r a⟩ = a := enum_type (PrincipalSeg.ofElement r a) theorem enum_lt_enum {r : α → α → Prop} [IsWellOrder α r] {o₁ o₂ : Iio (type r)} : r (enum r o₁) (enum r o₂) ↔ o₁ < o₂ := (enum _).map_rel_iff theorem enum_le_enum (r : α → α → Prop) [IsWellOrder α r] {o₁ o₂ : Iio (type r)} : ¬r (enum r o₁) (enum r o₂) ↔ o₂ ≤ o₁ := by rw [enum_lt_enum (r := r), not_lt] -- TODO: generalize to other well-orders @[simp] theorem enum_le_enum' (a : Ordinal) {o₁ o₂ : Iio (type (· < ·))} : enum (· < ·) o₁ ≤ enum (α := a.toType) (· < ·) o₂ ↔ o₁ ≤ o₂ := by rw [← enum_le_enum, not_lt] theorem enum_inj {r : α → α → Prop} [IsWellOrder α r] {o₁ o₂ : Iio (type r)} : enum r o₁ = enum r o₂ ↔ o₁ = o₂ := EmbeddingLike.apply_eq_iff_eq _ theorem enum_zero_le {r : α → α → Prop} [IsWellOrder α r] (h0 : 0 < type r) (a : α) : ¬r a (enum r ⟨0, h0⟩) := by rw [← enum_typein r a, enum_le_enum r] apply Ordinal.zero_le theorem enum_zero_le' {o : Ordinal} (h0 : 0 < o) (a : o.toType) : enum (α := o.toType) (· < ·) ⟨0, type_toType _ ▸ h0⟩ ≤ a := by rw [← not_lt] apply enum_zero_le theorem relIso_enum' {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≃r s) (o : Ordinal) : ∀ (hr : o < type r) (hs : o < type s), f (enum r ⟨o, hr⟩) = enum s ⟨o, hs⟩ := by refine inductionOn o ?_; rintro γ t wo ⟨g⟩ ⟨h⟩ rw [enum_type g, enum_type (g.transRelIso f)]; rfl theorem relIso_enum {α β : Type u} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≃r s) (o : Ordinal) (hr : o < type r) : f (enum r ⟨o, hr⟩) = enum s ⟨o, hr.trans_eq (Quotient.sound ⟨f⟩)⟩ := relIso_enum' _ _ _ _ /-- The order isomorphism between ordinals less than `o` and `o.toType`. -/ @[simps! -isSimp] noncomputable def enumIsoToType (o : Ordinal) : Set.Iio o ≃o o.toType where toFun x := enum (α := o.toType) (· < ·) ⟨x.1, type_toType _ ▸ x.2⟩ invFun x := ⟨typein (α := o.toType) (· < ·) x, typein_lt_self x⟩ left_inv _ := Subtype.ext (typein_enum _ _) right_inv _ := enum_typein _ _ map_rel_iff' := enum_le_enum' _ instance small_Iio (o : Ordinal.{u}) : Small.{u} (Iio o) := ⟨_, ⟨(enumIsoToType _).toEquiv⟩⟩ instance small_Iic (o : Ordinal.{u}) : Small.{u} (Iic o) := by rw [← Iio_union_right] infer_instance instance small_Ico (a b : Ordinal.{u}) : Small.{u} (Ico a b) := small_subset Ico_subset_Iio_self instance small_Icc (a b : Ordinal.{u}) : Small.{u} (Icc a b) := small_subset Icc_subset_Iic_self instance small_Ioo (a b : Ordinal.{u}) : Small.{u} (Ioo a b) := small_subset Ioo_subset_Iio_self instance small_Ioc (a b : Ordinal.{u}) : Small.{u} (Ioc a b) := small_subset Ioc_subset_Iic_self /-- `o.toType` is an `OrderBot` whenever `o ≠ 0`. -/ def toTypeOrderBot {o : Ordinal} (ho : o ≠ 0) : OrderBot o.toType where bot := (enum (· < ·)) ⟨0, _⟩ bot_le := enum_zero_le' (by rwa [Ordinal.pos_iff_ne_zero]) theorem enum_zero_eq_bot {o : Ordinal} (ho : 0 < o) : enum (α := o.toType) (· < ·) ⟨0, by rwa [type_toType]⟩ = have H := toTypeOrderBot (o := o) (by rintro rfl; simp at ho) (⊥ : o.toType) := rfl theorem lt_wf : @WellFounded Ordinal (· < ·) := wellFounded_iff_wellFounded_subrel.mpr (·.induction_on fun ⟨_, _, wo⟩ ↦ RelHomClass.wellFounded (enum _) wo.wf) instance wellFoundedRelation : WellFoundedRelation Ordinal := ⟨(· < ·), lt_wf⟩ instance wellFoundedLT : WellFoundedLT Ordinal := ⟨lt_wf⟩ instance : ConditionallyCompleteLinearOrderBot Ordinal := WellFoundedLT.conditionallyCompleteLinearOrderBot _ /-- Reformulation of well-founded induction on ordinals as a lemma that works with the `induction` tactic, as in `induction i using Ordinal.induction with | h i IH => ?_`. -/ theorem induction {p : Ordinal.{u} → Prop} (i : Ordinal.{u}) (h : ∀ j, (∀ k, k < j → p k) → p j) : p i := lt_wf.induction i h theorem typein_apply {α β} {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≼i s) (a : α) : typein s (f a) = typein r a := by rw [← f.transPrincipal_apply _ a, (f.transPrincipal _).eq] /-! ### Cardinality of ordinals -/ /-- The cardinal of an ordinal is the cardinality of any type on which a relation with that order type is defined. -/ def card : Ordinal → Cardinal := Quotient.map WellOrder.α fun _ _ ⟨e⟩ => ⟨e.toEquiv⟩ @[simp] theorem card_type (r : α → α → Prop) [IsWellOrder α r] : card (type r) = #α := rfl @[simp] theorem card_typein {r : α → α → Prop} [IsWellOrder α r] (x : α) : #{ y // r y x } = (typein r x).card := rfl @[gcongr] theorem card_le_card {o₁ o₂ : Ordinal} : o₁ ≤ o₂ → card o₁ ≤ card o₂ := inductionOn o₁ fun _ _ _ => inductionOn o₂ fun _ _ _ ⟨⟨⟨f, _⟩, _⟩⟩ => ⟨f⟩ @[simp] theorem card_zero : card 0 = 0 := mk_eq_zero _ @[simp] theorem card_one : card 1 = 1 := mk_eq_one _ /-! ### Lifting ordinals to a higher universe -/ /-- The universe lift operation for ordinals, which embeds `Ordinal.{u}` as a proper initial segment of `Ordinal.{v}` for `v > u`. For the initial segment version, see `liftInitialSeg`. -/ @[pp_with_univ] def lift (o : Ordinal.{v}) : Ordinal.{max v u} := Quotient.liftOn o (fun w => type <| ULift.down ⁻¹'o w.r) fun ⟨_, r, _⟩ ⟨_, s, _⟩ ⟨f⟩ => Quot.sound ⟨(RelIso.preimage Equiv.ulift r).trans <| f.trans (RelIso.preimage Equiv.ulift s).symm⟩ @[simp] theorem type_uLift (r : α → α → Prop) [IsWellOrder α r] : type (ULift.down ⁻¹'o r) = lift.{v} (type r) := rfl theorem _root_.RelIso.ordinal_lift_type_eq {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≃r s) : lift.{v} (type r) = lift.{u} (type s) := ((RelIso.preimage Equiv.ulift r).trans <| f.trans (RelIso.preimage Equiv.ulift s).symm).ordinal_type_eq @[simp] theorem type_preimage {α β : Type u} (r : α → α → Prop) [IsWellOrder α r] (f : β ≃ α) : type (f ⁻¹'o r) = type r := (RelIso.preimage f r).ordinal_type_eq @[simp] theorem type_lift_preimage (r : α → α → Prop) [IsWellOrder α r] (f : β ≃ α) : lift.{u} (type (f ⁻¹'o r)) = lift.{v} (type r) := (RelIso.preimage f r).ordinal_lift_type_eq /-- `lift.{max u v, u}` equals `lift.{v, u}`. Unfortunately, the simp lemma doesn't seem to work. -/ theorem lift_umax : lift.{max u v, u} = lift.{v, u} := funext fun a => inductionOn a fun _ r _ => Quotient.sound ⟨(RelIso.preimage Equiv.ulift r).trans (RelIso.preimage Equiv.ulift r).symm⟩ /-- An ordinal lifted to a lower or equal universe equals itself. Unfortunately, the simp lemma doesn't work. -/ theorem lift_id' (a : Ordinal) : lift a = a := inductionOn a fun _ r _ => Quotient.sound ⟨RelIso.preimage Equiv.ulift r⟩ /-- An ordinal lifted to the same universe equals itself. -/ @[simp] theorem lift_id : ∀ a, lift.{u, u} a = a := lift_id'.{u, u} /-- An ordinal lifted to the zero universe equals itself. -/ @[simp] theorem lift_uzero (a : Ordinal.{u}) : lift.{0} a = a := lift_id' a theorem lift_type_le {α : Type u} {β : Type v} {r s} [IsWellOrder α r] [IsWellOrder β s] : lift.{max v w} (type r) ≤ lift.{max u w} (type s) ↔ Nonempty (r ≼i s) := by constructor <;> refine fun ⟨f⟩ ↦ ⟨?_⟩ · exact (RelIso.preimage Equiv.ulift r).symm.toInitialSeg.trans (f.trans (RelIso.preimage Equiv.ulift s).toInitialSeg) · exact (RelIso.preimage Equiv.ulift r).toInitialSeg.trans (f.trans (RelIso.preimage Equiv.ulift s).symm.toInitialSeg) theorem lift_type_eq {α : Type u} {β : Type v} {r s} [IsWellOrder α r] [IsWellOrder β s] : lift.{max v w} (type r) = lift.{max u w} (type s) ↔ Nonempty (r ≃r s) := by refine Quotient.eq'.trans ⟨?_, ?_⟩ <;> refine fun ⟨f⟩ ↦ ⟨?_⟩ · exact (RelIso.preimage Equiv.ulift r).symm.trans <| f.trans (RelIso.preimage Equiv.ulift s) · exact (RelIso.preimage Equiv.ulift r).trans <| f.trans (RelIso.preimage Equiv.ulift s).symm theorem lift_type_lt {α : Type u} {β : Type v} {r s} [IsWellOrder α r] [IsWellOrder β s] : lift.{max v w} (type r) < lift.{max u w} (type s) ↔ Nonempty (r ≺i s) := by constructor <;> refine fun ⟨f⟩ ↦ ⟨?_⟩ · exact (f.relIsoTrans (RelIso.preimage Equiv.ulift r).symm).transInitial (RelIso.preimage Equiv.ulift s).toInitialSeg · exact (f.relIsoTrans (RelIso.preimage Equiv.ulift r)).transInitial (RelIso.preimage Equiv.ulift s).symm.toInitialSeg @[simp] theorem lift_le {a b : Ordinal} : lift.{u, v} a ≤ lift.{u, v} b ↔ a ≤ b := inductionOn₂ a b fun α r _ β s _ => by rw [← lift_umax] exact lift_type_le.{_,_,u} @[simp] theorem lift_inj {a b : Ordinal} : lift.{u, v} a = lift.{u, v} b ↔ a = b := by simp_rw [le_antisymm_iff, lift_le] @[simp] theorem lift_lt {a b : Ordinal} : lift.{u, v} a < lift.{u, v} b ↔ a < b := by simp_rw [lt_iff_le_not_ge, lift_le] @[simp] theorem lift_typein_top {r : α → α → Prop} {s : β → β → Prop} [IsWellOrder α r] [IsWellOrder β s] (f : r ≺i s) : lift.{u} (typein s f.top) = lift (type r) := f.subrelIso.ordinal_lift_type_eq /-- Initial segment version of the lift operation on ordinals, embedding `Ordinal.{u}` in `Ordinal.{v}` as an initial segment when `u ≤ v`. -/ def liftInitialSeg : Ordinal.{v} ≤i Ordinal.{max u v} := by refine ⟨RelEmbedding.ofMonotone lift.{u} (by simp), fun a b ↦ Ordinal.inductionOn₂ a b fun α r _ β s _ h ↦ ?_⟩ rw [RelEmbedding.ofMonotone_coe, ← lift_id'.{max u v} (type s), ← lift_umax.{v, u}, lift_type_lt] at h obtain ⟨f⟩ := h use typein r f.top rw [RelEmbedding.ofMonotone_coe, ← lift_umax, lift_typein_top, lift_id'] @[simp] theorem liftInitialSeg_coe : (liftInitialSeg.{v, u} : Ordinal → Ordinal) = lift.{v, u} := rfl @[simp] theorem lift_lift (a : Ordinal.{u}) : lift.{w} (lift.{v} a) = lift.{max v w} a := (liftInitialSeg.trans liftInitialSeg).eq liftInitialSeg a @[simp] theorem lift_zero : lift 0 = 0 := type_eq_zero_of_empty _ @[simp] theorem lift_one : lift 1 = 1 := type_eq_one_of_unique _ @[simp] theorem lift_card (a) : Cardinal.lift.{u, v} (card a) = card (lift.{u} a) := inductionOn a fun _ _ _ => rfl theorem mem_range_lift_of_le {a : Ordinal.{u}} {b : Ordinal.{max u v}} (h : b ≤ lift.{v} a) : b ∈ Set.range lift.{v} := liftInitialSeg.mem_range_of_le h theorem le_lift_iff {a : Ordinal.{u}} {b : Ordinal.{max u v}} : b ≤ lift.{v} a ↔ ∃ a' ≤ a, lift.{v} a' = b := liftInitialSeg.le_apply_iff theorem lt_lift_iff {a : Ordinal.{u}} {b : Ordinal.{max u v}} : b < lift.{v} a ↔ ∃ a' < a, lift.{v} a' = b := liftInitialSeg.lt_apply_iff /-! ### The first infinite ordinal ω -/ /-- `ω` is the first infinite ordinal, defined as the order type of `ℕ`. -/ def omega0 : Ordinal.{u} := lift (typeLT ℕ) @[inherit_doc] scoped notation "ω" => Ordinal.omega0 /-- Note that the presence of this lemma makes `simp [omega0]` form a loop. -/ @[simp] theorem type_nat_lt : typeLT ℕ = ω := (lift_id _).symm @[simp] theorem card_omega0 : card ω = ℵ₀ := rfl @[simp] theorem lift_omega0 : lift ω = ω := lift_lift _ /-! ### Definition and first properties of addition on ordinals In this paragraph, we introduce the addition on ordinals, and prove just enough properties to deduce that the order on ordinals is total (and therefore well-founded). Further properties of the addition, together with properties of the other operations, are proved in `Mathlib/SetTheory/Ordinal/Arithmetic.lean`. -/ /-- `o₁ + o₂` is the order on the disjoint union of `o₁` and `o₂` obtained by declaring that every element of `o₁` is smaller than every element of `o₂`. -/ instance add : Add Ordinal.{u} := ⟨fun o₁ o₂ => Quotient.liftOn₂ o₁ o₂ (fun ⟨_, r, _⟩ ⟨_, s, _⟩ => type (Sum.Lex r s)) fun _ _ _ _ ⟨f⟩ ⟨g⟩ => (RelIso.sumLexCongr f g).ordinal_type_eq⟩ instance addMonoidWithOne : AddMonoidWithOne Ordinal.{u} where zero_add o := inductionOn o fun α _ _ => (RelIso.emptySumLex _ _).ordinal_type_eq add_zero o := inductionOn o fun α _ _ => (RelIso.sumLexEmpty _ _).ordinal_type_eq add_assoc o₁ o₂ o₃ := Quotient.inductionOn₃ o₁ o₂ o₃ fun ⟨α, r, _⟩ ⟨β, s, _⟩ ⟨γ, t, _⟩ => Quot.sound ⟨⟨sumAssoc _ _ _, by intro a b rcases a with (⟨a | a⟩ | a) <;> rcases b with (⟨b | b⟩ | b) <;> simp only [sumAssoc_apply_inl_inl, sumAssoc_apply_inl_inr, sumAssoc_apply_inr, Sum.lex_inl_inl, Sum.lex_inr_inr, Sum.Lex.sep, Sum.lex_inr_inl]⟩⟩ nsmul := nsmulRec @[simp] theorem card_add (o₁ o₂ : Ordinal) : card (o₁ + o₂) = card o₁ + card o₂ := inductionOn o₁ fun _ __ => inductionOn o₂ fun _ _ _ => rfl @[simp] theorem type_sum_lex {α β : Type u} (r : α → α → Prop) (s : β → β → Prop) [IsWellOrder α r] [IsWellOrder β s] : type (Sum.Lex r s) = type r + type s := rfl @[simp] theorem card_nat (n : ℕ) : card.{u} n = n := by induction n <;> [simp; simp only [card_add, card_one, Nat.cast_succ, *]] @[simp] theorem card_ofNat (n : ℕ) [n.AtLeastTwo] : card.{u} ofNat(n) = OfNat.ofNat n := card_nat n instance instAddLeftMono : AddLeftMono Ordinal.{u} where elim c a b := by refine inductionOn₃ a b c fun α r _ β s _ γ t _ ⟨f⟩ ↦ (RelEmbedding.ofMonotone (Sum.recOn · Sum.inl (Sum.inr ∘ f)) ?_).ordinal_type_le simp [f.map_rel_iff] instance instAddRightMono : AddRightMono Ordinal.{u} where elim c a b := by refine inductionOn₃ a b c fun α r _ β s _ γ t _ ⟨f⟩ ↦ (RelEmbedding.ofMonotone (Sum.recOn · (Sum.inl ∘ f) Sum.inr) ?_).ordinal_type_le simp [f.map_rel_iff] theorem le_add_right (a b : Ordinal) : a ≤ a + b := by simpa only [add_zero] using add_le_add_left (Ordinal.zero_le b) a theorem le_add_left (a b : Ordinal) : a ≤ b + a := by simpa only [zero_add] using add_le_add_right (Ordinal.zero_le b) a theorem max_zero_left : ∀ a : Ordinal, max 0 a = a := max_bot_left theorem max_zero_right : ∀ a : Ordinal, max a 0 = a := max_bot_right @[simp] theorem max_eq_zero {a b : Ordinal} : max a b = 0 ↔ a = 0 ∧ b = 0 := max_eq_bot @[simp] theorem sInf_empty : sInf (∅ : Set Ordinal) = 0 := dif_neg Set.not_nonempty_empty /-! ### Successor order properties -/ private theorem succ_le_iff' {a b : Ordinal} : a + 1 ≤ b ↔ a < b := by refine inductionOn₂ a b fun α r _ β s _ ↦ ⟨?_, ?_⟩ <;> rintro ⟨f⟩ · refine ⟨((InitialSeg.leAdd _ _).trans f).toPrincipalSeg fun h ↦ ?_⟩ simpa using h (f (Sum.inr PUnit.unit)) · apply (RelEmbedding.ofMonotone (Sum.recOn · f fun _ ↦ f.top) ?_).ordinal_type_le simpa [f.map_rel_iff] using f.lt_top instance : NoMaxOrder Ordinal := ⟨fun _ => ⟨_, succ_le_iff'.1 le_rfl⟩⟩ instance : SuccOrder Ordinal.{u} := SuccOrder.ofSuccLeIff (fun o => o + 1) succ_le_iff' instance : SuccAddOrder Ordinal := ⟨fun _ => rfl⟩ @[simp] theorem add_one_eq_succ (o : Ordinal) : o + 1 = succ o := rfl @[simp] theorem succ_zero : succ (0 : Ordinal) = 1 := zero_add 1 @[simp] theorem succ_one : succ (1 : Ordinal) = 2 := by congr; simp only [Nat.unaryCast, zero_add] theorem add_succ (o₁ o₂ : Ordinal) : o₁ + succ o₂ = succ (o₁ + o₂) := (add_assoc _ _ _).symm theorem one_le_iff_ne_zero {o : Ordinal} : 1 ≤ o ↔ o ≠ 0 := by rw [Order.one_le_iff_pos, Ordinal.pos_iff_ne_zero] theorem succ_pos (o : Ordinal) : 0 < succ o := bot_lt_succ o theorem succ_ne_zero (o : Ordinal) : succ o ≠ 0 := ne_of_gt <| succ_pos o @[simp] theorem lt_one_iff_zero {a : Ordinal} : a < 1 ↔ a = 0 := by simpa using @lt_succ_bot_iff _ _ _ a _ _ theorem le_one_iff {a : Ordinal} : a ≤ 1 ↔ a = 0 ∨ a = 1 := by simpa using @le_succ_bot_iff _ _ _ a _ @[simp] theorem card_succ (o : Ordinal) : card (succ o) = card o + 1 := by simp only [← add_one_eq_succ, card_add, card_one] theorem natCast_succ (n : ℕ) : ↑n.succ = succ (n : Ordinal) := rfl instance uniqueIioOne : Unique (Iio (1 : Ordinal)) where default := ⟨0, zero_lt_one' Ordinal⟩ uniq a := Subtype.ext <| lt_one_iff_zero.1 a.2 @[simp] theorem Iio_one_default_eq : (default : Iio (1 : Ordinal)) = ⟨0, zero_lt_one' Ordinal⟩ := rfl instance uniqueToTypeOne : Unique (toType 1) where default := enum (α := toType 1) (· < ·) ⟨0, by simp⟩ uniq a := by rw [← enum_typein (α := toType 1) (· < ·) a] congr rw [← lt_one_iff_zero] apply typein_lt_self theorem one_toType_eq (x : toType 1) : x = enum (· < ·) ⟨0, by simp⟩ := Unique.eq_default x /-! ### Extra properties of typein and enum -/ -- TODO: use `enumIsoToType` for lemmas on `toType` rather than `enum` and `typein`. @[simp] theorem typein_one_toType (x : toType 1) : typein (α := toType 1) (· < ·) x = 0 := by rw [one_toType_eq x, typein_enum] theorem typein_le_typein' (o : Ordinal) {x y : o.toType} : typein (α := o.toType) (· < ·) x ≤ typein (α := o.toType) (· < ·) y ↔ x ≤ y := by simp theorem le_enum_succ {o : Ordinal} (a : (succ o).toType) : a ≤ enum (α := (succ o).toType) (· < ·) ⟨o, (type_toType _ ▸ lt_succ o)⟩ := by rw [← enum_typein (α := (succ o).toType) (· < ·) a, enum_le_enum', Subtype.mk_le_mk, ← lt_succ_iff] apply typein_lt_self /-! ### Universal ordinal -/ -- intended to be used with explicit universe parameters /-- `univ.{u v}` is the order type of the ordinals of `Type u` as a member of `Ordinal.{v}` (when `u < v`). It is an inaccessible cardinal. -/ @[pp_with_univ, nolint checkUnivs] def univ : Ordinal.{max (u + 1) v} := lift.{v, u + 1} (typeLT Ordinal) theorem univ_id : univ.{u, u + 1} = typeLT Ordinal := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u, v} = univ.{u, max v w} := lift_lift _ theorem univ_umax : univ.{u, max (u + 1) v} = univ.{u, v} := congr_fun lift_umax _ /-- Principal segment version of the lift operation on ordinals, embedding `Ordinal.{u}` in `Ordinal.{v}` as a principal segment when `u < v`. -/ def liftPrincipalSeg : Ordinal.{u} <i Ordinal.{max (u + 1) v} := ⟨↑liftInitialSeg.{max (u + 1) v, u}, univ.{u, v}, by refine fun b => inductionOn b ?_; intro β s _ rw [univ, ← lift_umax]; constructor <;> intro h · obtain ⟨a, e⟩ := h rw [← e] refine inductionOn a ?_ intro α r _ exact lift_type_lt.{u, u + 1, max (u + 1) v}.2 ⟨typein r⟩ · rw [← lift_id (type s)] at h ⊢ obtain ⟨f⟩ := lift_type_lt.{_,_,v}.1 h obtain ⟨f, a, hf⟩ := f exists a induction a using inductionOn with | H a r => refine lift_type_eq.{u, max (u + 1) v, max (u + 1) v}.2 ⟨(RelIso.ofSurjective (RelEmbedding.ofMonotone ?_ ?_) ?_).symm⟩ · exact fun b => enum r ⟨f b, (hf _).1 ⟨_, rfl⟩⟩ · refine fun a b h => (typein_lt_typein r).1 ?_ rw [typein_enum, typein_enum] exact f.map_rel_iff.2 h · intro a' obtain ⟨b, e⟩ := (hf _).2 (typein_lt_type _ a') exists b simp only [RelEmbedding.ofMonotone_coe] simp [e]⟩ @[simp] theorem liftPrincipalSeg_coe : (liftPrincipalSeg.{u, v} : Ordinal → Ordinal) = lift.{max (u + 1) v} := rfl @[simp] theorem liftPrincipalSeg_top : (liftPrincipalSeg.{u, v}).top = univ.{u, v} := rfl theorem liftPrincipalSeg_top' : liftPrincipalSeg.{u, u + 1}.top = typeLT Ordinal := by simp only [liftPrincipalSeg_top, univ_id] end Ordinal /-! ### Representing a cardinal with an ordinal -/ namespace Cardinal open Ordinal @[simp] theorem mk_toType (o : Ordinal) : #o.toType = o.card := (Ordinal.card_type _).symm.trans <| by rw [Ordinal.type_toType] /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. For the order-embedding version, see `ord.order_embedding`. -/ def ord (c : Cardinal) : Ordinal := Quot.liftOn c (fun α : Type u => ⨅ r : { r // IsWellOrder α r }, @type α r.1 r.2) <| by rintro α β ⟨f⟩ refine congr_arg sInf <| ext fun o ↦ ⟨?_, ?_⟩ <;> rintro ⟨⟨r, hr⟩, rfl⟩ <;> refine ⟨⟨_, RelIso.IsWellOrder.preimage r ?_⟩, type_preimage _ _⟩ exacts [f.symm, f] theorem ord_eq_Inf (α : Type u) : ord #α = ⨅ r : { r // IsWellOrder α r }, @type α r.1 r.2 := rfl /-- There exists a well-order on `α` whose order type is exactly `ord #α`. -/ theorem ord_eq (α) : ∃ (r : α → α → Prop) (wo : IsWellOrder α r), ord #α = @type α r wo := let ⟨r, wo⟩ := ciInf_mem fun r : { r // IsWellOrder α r } => @type α r.1 r.2 ⟨r.1, r.2, wo.symm⟩ theorem ord_le_type (r : α → α → Prop) [h : IsWellOrder α r] : ord #α ≤ type r := ciInf_le' _ (Subtype.mk r h) theorem ord_le {c o} : ord c ≤ o ↔ c ≤ o.card := by refine c.inductionOn fun α ↦ o.inductionOn fun β s _ ↦ ?_ let ⟨r, _, e⟩ := ord_eq α constructor <;> intro h · rw [e] at h exact card_le_card h · obtain ⟨f⟩ := h have g := RelEmbedding.preimage f s have := RelEmbedding.isWellOrder g exact (ord_le_type _).trans g.ordinal_type_le theorem gc_ord_card : GaloisConnection ord card := fun _ _ => ord_le theorem lt_ord {c o} : o < ord c ↔ o.card < c := gc_ord_card.lt_iff_lt @[simp] theorem card_ord (c) : (ord c).card = c := c.inductionOn fun α ↦ let ⟨r, _, e⟩ := ord_eq α; e ▸ card_type r theorem card_surjective : Function.Surjective card := fun c ↦ ⟨_, card_ord c⟩ theorem bddAbove_ord_image_iff {s : Set Cardinal} : BddAbove (ord '' s) ↔ BddAbove s := gc_ord_card.bddAbove_l_image /-- Galois coinsertion between `Cardinal.ord` and `Ordinal.card`. -/ def gciOrdCard : GaloisCoinsertion ord card := gc_ord_card.toGaloisCoinsertion fun c => c.card_ord.le theorem ord_card_le (o : Ordinal) : o.card.ord ≤ o := gc_ord_card.l_u_le _ theorem lt_ord_succ_card (o : Ordinal) : o < (succ o.card).ord := lt_ord.2 <| lt_succ _ theorem card_le_iff {o : Ordinal} {c : Cardinal} : o.card ≤ c ↔ o < (succ c).ord := by rw [lt_ord, lt_succ_iff] /-- A variation on `Cardinal.lt_ord` using `≤`: If `o` is no greater than the initial ordinal of cardinality `c`, then its cardinal is no greater than `c`. The converse, however, is false (for instance, `o = ω+1` and `c = ℵ₀`). -/ lemma card_le_of_le_ord {o : Ordinal} {c : Cardinal} (ho : o ≤ c.ord) : o.card ≤ c := by rw [← card_ord c]; exact Ordinal.card_le_card ho @[mono] theorem ord_strictMono : StrictMono ord := gciOrdCard.strictMono_l @[mono] theorem ord_mono : Monotone ord := gc_ord_card.monotone_l @[simp] theorem ord_le_ord {c₁ c₂} : ord c₁ ≤ ord c₂ ↔ c₁ ≤ c₂ := gciOrdCard.l_le_l_iff @[simp] theorem ord_lt_ord {c₁ c₂} : ord c₁ < ord c₂ ↔ c₁ < c₂ := ord_strictMono.lt_iff_lt @[simp] theorem ord_zero : ord 0 = 0 := gc_ord_card.l_bot @[simp] theorem ord_nat (n : ℕ) : ord n = n := by apply (ord_le.2 (card_nat n).ge).antisymm induction n with | zero => exact Ordinal.zero_le _ | succ n IH => exact (IH.trans_lt <| by simpa using Nat.cast_lt.2 n.lt_succ_self).succ_le @[simp] theorem ord_ofNat (n : ℕ) [n.AtLeastTwo] : ord ofNat(n) = OfNat.ofNat n := ord_nat n @[simp] theorem ord_one : ord 1 = 1 := by simpa using ord_nat 1 theorem isNormal_ord : Order.IsNormal ord where strictMono := ord_strictMono mem_lowerBounds_upperBounds_of_isSuccLimit := by intro a ha simp_rw [lowerBounds, upperBounds, mem_setOf, forall_mem_image, ord_le] refine fun b H ↦ le_of_forall_lt fun c hc ↦ ?_ simpa using H (ha.succ_lt hc) @[simp] theorem ord_aleph0 : ord.{u} ℵ₀ = ω := by refine le_antisymm (ord_le.2 le_rfl) <| le_of_forall_lt fun o h ↦ ?_ rcases Ordinal.lt_lift_iff.1 h with ⟨o, ho, rfl⟩ rw [lt_ord, ← lift_card, lift_lt_aleph0, ← typein_enum _ ho] exact lt_aleph0_iff_fintype.2 ⟨Set.fintypeLTNat _⟩ @[simp] theorem lift_ord (c) : Ordinal.lift.{u,v} (ord c) = ord (lift.{u,v} c) := by refine le_antisymm (le_of_forall_lt fun a ha => ?_) ?_ · rcases Ordinal.lt_lift_iff.1 ha with ⟨a, _, rfl⟩ rwa [lt_ord, ← lift_card, lift_lt, ← lt_ord, ← Ordinal.lift_lt] · rw [ord_le, ← lift_card, card_ord] theorem mk_ord_toType (c : Cardinal) : #c.ord.toType = c := by simp theorem card_typein_lt (r : α → α → Prop) [IsWellOrder α r] (x : α) (h : ord #α = type r) : card (typein r x) < #α := by rw [← lt_ord, h] apply typein_lt_type theorem card_typein_toType_lt (c : Cardinal) (x : c.ord.toType) : card (typein (α := c.ord.toType) (· < ·) x) < c := by rw [← lt_ord] apply typein_lt_self theorem mk_Iio_ord_toType {c : Cardinal} (i : c.ord.toType) : #(Iio i) < c := card_typein_toType_lt c i theorem ord_injective : Injective ord := by intro c c' h rw [← card_ord c, ← card_ord c', h] @[simp] theorem ord_inj {a b : Cardinal} : a.ord = b.ord ↔ a = b := ord_injective.eq_iff @[simp] theorem ord_eq_zero {a : Cardinal} : a.ord = 0 ↔ a = 0 := ord_injective.eq_iff' ord_zero @[simp] theorem ord_eq_one {a : Cardinal} : a.ord = 1 ↔ a = 1 := ord_injective.eq_iff' ord_one @[simp] theorem omega0_le_ord {a : Cardinal} : ω ≤ a.ord ↔ ℵ₀ ≤ a := by rw [← ord_aleph0, ord_le_ord] @[simp] theorem ord_le_omega0 {a : Cardinal} : a.ord ≤ ω ↔ a ≤ ℵ₀ := by rw [← ord_aleph0, ord_le_ord] @[simp] theorem ord_lt_omega0 {a : Cardinal} : a.ord < ω ↔ a < ℵ₀ := le_iff_le_iff_lt_iff_lt.1 omega0_le_ord @[simp] theorem omega0_lt_ord {a : Cardinal} : ω < a.ord ↔ ℵ₀ < a := le_iff_le_iff_lt_iff_lt.1 ord_le_omega0 @[simp] theorem ord_eq_omega0 {a : Cardinal} : a.ord = ω ↔ a = ℵ₀ := ord_injective.eq_iff' ord_aleph0 /-- The ordinal corresponding to a cardinal `c` is the least ordinal whose cardinal is `c`. This is the order-embedding version. For the regular function, see `ord`. -/ def ord.orderEmbedding : Cardinal ↪o Ordinal := RelEmbedding.orderEmbeddingOfLTEmbedding (RelEmbedding.ofMonotone Cardinal.ord fun _ _ => Cardinal.ord_lt_ord.2) @[simp] theorem ord.orderEmbedding_coe : (ord.orderEmbedding : Cardinal → Ordinal) = ord := rfl -- intended to be used with explicit universe parameters /-- The cardinal `univ` is the cardinality of ordinal `univ`, or equivalently the cardinal of `Ordinal.{u}`, or `Cardinal.{u}`, as an element of `Cardinal.{v}` (when `u < v`). -/ @[pp_with_univ, nolint checkUnivs] def univ := lift.{v, u + 1} #Ordinal theorem univ_id : univ.{u, u + 1} = #Ordinal := lift_id _ @[simp] theorem lift_univ : lift.{w} univ.{u, v} = univ.{u, max v w} := lift_lift _ theorem univ_umax : univ.{u, max (u + 1) v} = univ.{u, v} := congr_fun lift_umax _ theorem lift_lt_univ (c : Cardinal) : lift.{u + 1, u} c < univ.{u, u + 1} := by simpa only [liftPrincipalSeg_coe, lift_ord, lift_succ, ord_le, succ_le_iff] using le_of_lt (liftPrincipalSeg.{u, u + 1}.lt_top (succ c).ord) theorem lift_lt_univ' (c : Cardinal) : lift.{max (u + 1) v, u} c < univ.{u, v} := by have := lift_lt.{_, max (u+1) v}.2 (lift_lt_univ c) rw [lift_lift, lift_univ, univ_umax.{u,v}] at this exact this theorem aleph0_lt_univ : ℵ₀ < univ.{u, v} := by simpa using lift_lt_univ' ℵ₀ theorem nat_lt_univ (n : ℕ) : n < univ.{u, v} := (nat_lt_aleph0 n).trans aleph0_lt_univ theorem univ_pos : 0 < univ.{u, v} := aleph0_pos.trans aleph0_lt_univ theorem univ_ne_zero : univ.{u, v} ≠ 0 := univ_pos.ne' @[simp] theorem ord_univ : ord univ.{u, v} = Ordinal.univ.{u, v} := by refine le_antisymm (ord_card_le _) <| le_of_forall_lt fun o h => lt_ord.2 ?_ have := liftPrincipalSeg.mem_range_of_rel_top (by simpa only [liftPrincipalSeg_coe] using h) rcases this with ⟨o, h'⟩ rw [← h', liftPrincipalSeg_coe, ← lift_card] apply lift_lt_univ' theorem lt_univ {c} : c < univ.{u, u + 1} ↔ ∃ c', c = lift.{u + 1, u} c' := ⟨fun h => by have := ord_lt_ord.2 h rw [ord_univ] at this obtain ⟨o, e⟩ := liftPrincipalSeg.mem_range_of_rel_top (by simpa only [liftPrincipalSeg_top]) have := card_ord c rw [← e, liftPrincipalSeg_coe, ← lift_card] at this exact ⟨_, this.symm⟩, fun ⟨_, e⟩ => e.symm ▸ lift_lt_univ _⟩ theorem lt_univ' {c} : c < univ.{u, v} ↔ ∃ c', c = lift.{max (u + 1) v, u} c' := ⟨fun h => by let ⟨a, h', e⟩ := lt_lift_iff.1 h rw [← univ_id] at h' rcases lt_univ.{u}.1 h' with ⟨c', rfl⟩ exact ⟨c', by simp only [e.symm, lift_lift]⟩, fun ⟨_, e⟩ => e.symm ▸ lift_lt_univ' _⟩ theorem IsStrongLimit.univ : IsStrongLimit univ.{u, v} := ⟨univ_ne_zero, fun c h ↦ let ⟨w, h⟩ := lt_univ'.1 h; lt_univ'.2 ⟨2 ^ w, by simp [h]⟩⟩ theorem small_iff_lift_mk_lt_univ {α : Type u} : Small.{v} α ↔ Cardinal.lift.{v+1,_} #α < univ.{v, max u (v + 1)} := by rw [lt_univ'] constructor · rintro ⟨β, e⟩ exact ⟨#β, lift_mk_eq.{u, _, v + 1}.2 e⟩ · rintro ⟨c, hc⟩ exact ⟨⟨c.out, lift_mk_eq.{u, _, v + 1}.1 (hc.trans (congr rfl c.mk_out.symm))⟩⟩ /-- If a cardinal `c` is nonzero, then `c.ord.toType` has a least element. -/ noncomputable def toTypeOrderBot {c : Cardinal} (hc : c ≠ 0) : OrderBot c.ord.toType := Ordinal.toTypeOrderBot (fun h ↦ hc (ord_injective (by simpa using h))) end Cardinal namespace Ordinal @[simp] theorem card_univ : card univ.{u,v} = Cardinal.univ.{u,v} := rfl @[simp] theorem nat_le_card {o} {n : ℕ} : (n : Cardinal) ≤ card o ↔ (n : Ordinal) ≤ o := by rw [← Cardinal.ord_le, Cardinal.ord_nat] @[simp] theorem one_le_card {o} : 1 ≤ card o ↔ 1 ≤ o := by simpa using nat_le_card (n := 1) @[simp] theorem ofNat_le_card {o} {n : ℕ} [n.AtLeastTwo] : (ofNat(n) : Cardinal) ≤ card o ↔ (OfNat.ofNat n : Ordinal) ≤ o := nat_le_card @[simp] theorem aleph0_le_card {o} : ℵ₀ ≤ card o ↔ ω ≤ o := by rw [← ord_le, ord_aleph0] @[simp] theorem card_lt_aleph0 {o} : card o < ℵ₀ ↔ o < ω := le_iff_le_iff_lt_iff_lt.1 aleph0_le_card @[simp] theorem nat_lt_card {o} {n : ℕ} : (n : Cardinal) < card o ↔ (n : Ordinal) < o := by rw [← succ_le_iff, ← succ_le_iff, ← nat_succ, nat_le_card] rfl @[simp] theorem zero_lt_card {o} : 0 < card o ↔ 0 < o := by simpa using nat_lt_card (n := 0) @[simp] theorem one_lt_card {o} : 1 < card o ↔ 1 < o := by simpa using nat_lt_card (n := 1) @[simp] theorem ofNat_lt_card {o} {n : ℕ} [n.AtLeastTwo] : (ofNat(n) : Cardinal) < card o ↔ (OfNat.ofNat n : Ordinal) < o := nat_lt_card @[simp] theorem card_lt_nat {o} {n : ℕ} : card o < n ↔ o < n := lt_iff_lt_of_le_iff_le nat_le_card @[simp] theorem card_lt_ofNat {o} {n : ℕ} [n.AtLeastTwo] : card o < ofNat(n) ↔ o < OfNat.ofNat n := card_lt_nat @[simp] theorem card_le_nat {o} {n : ℕ} : card o ≤ n ↔ o ≤ n := le_iff_le_iff_lt_iff_lt.2 nat_lt_card @[simp] theorem card_le_one {o} : card o ≤ 1 ↔ o ≤ 1 := by simpa using card_le_nat (n := 1) @[simp] theorem card_le_ofNat {o} {n : ℕ} [n.AtLeastTwo] : card o ≤ ofNat(n) ↔ o ≤ OfNat.ofNat n := card_le_nat @[simp] theorem card_eq_nat {o} {n : ℕ} : card o = n ↔ o = n := by simp only [le_antisymm_iff, card_le_nat, nat_le_card] @[simp] theorem card_eq_zero {o} : card o = 0 ↔ o = 0 := by simpa using card_eq_nat (n := 0) @[simp] theorem card_eq_one {o} : card o = 1 ↔ o = 1 := by simpa using card_eq_nat (n := 1) theorem mem_range_lift_of_card_le {a : Cardinal.{u}} {b : Ordinal.{max u v}} (h : card b ≤ Cardinal.lift.{v, u} a) : b ∈ Set.range lift.{v, u} := by rw [card_le_iff, ← lift_succ, ← lift_ord] at h exact mem_range_lift_of_le h.le @[simp] theorem card_eq_ofNat {o} {n : ℕ} [n.AtLeastTwo] : card o = ofNat(n) ↔ o = OfNat.ofNat n := card_eq_nat @[simp] theorem type_fintype (r : α → α → Prop) [IsWellOrder α r] [Fintype α] : type r = Fintype.card α := by rw [← card_eq_nat, card_type, mk_fintype] theorem type_fin (n : ℕ) : typeLT (Fin n) = n := by simp end Ordinal /-! ### Sorted lists -/ theorem List.Sorted.lt_ord_of_lt [LinearOrder α] [WellFoundedLT α] {l m : List α} {o : Ordinal} (hl : l.Sorted (· > ·)) (hm : m.Sorted (· > ·)) (hmltl : m < l) (hlt : ∀ i ∈ l, Ordinal.typein (α := α) (· < ·) i < o) : ∀ i ∈ m, Ordinal.typein (α := α) (· < ·) i < o := by replace hmltl : List.Lex (· < ·) m l := hmltl cases l with | nil => simp at hmltl | cons a as => cases m with | nil => intro i hi; simp at hi | cons b bs => intro i hi suffices h : i ≤ a by refine lt_of_le_of_lt ?_ (hlt a mem_cons_self); simpa cases hi with | head as => exact List.head_le_of_lt hmltl | tail b hi => exact le_of_lt (lt_of_lt_of_le (List.rel_of_sorted_cons hm _ hi) (List.head_le_of_lt hmltl))
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Notation.lean
import Mathlib.Algebra.Ring.Divisibility.Basic import Mathlib.Data.Ordering.Lemmas import Mathlib.Data.PNat.Basic import Mathlib.SetTheory.Ordinal.Principal import Mathlib.Tactic.NormNum /-! # Ordinal notation Constructive ordinal arithmetic for ordinals below `ε₀`. We define a type `ONote`, with constructors `0 : ONote` and `ONote.oadd e n a` representing `ω ^ e * n + a`. We say that `o` is in Cantor normal form - `ONote.NF o` - if either `o = 0` or `o = ω ^ e * n + a` with `a < ω ^ e` and `a` in Cantor normal form. The type `NONote` is the type of ordinals below `ε₀` in Cantor normal form. Various operations (addition, subtraction, multiplication, exponentiation) are defined on `ONote` and `NONote`. -/ open Ordinal Order -- The generated theorem `ONote.zero.sizeOf_spec` is flagged by `simpNF`, -- and we don't otherwise need it. set_option genSizeOfSpec false in /-- Recursive definition of an ordinal notation. `zero` denotes the ordinal 0, and `oadd e n a` is intended to refer to `ω ^ e * n + a`. For this to be a valid Cantor normal form, we must have the exponents decrease to the right, but we can't state this condition until we've defined `repr`, so we make it a separate definition `NF`. -/ inductive ONote : Type | zero : ONote | oadd : ONote → ℕ+ → ONote → ONote deriving DecidableEq compile_inductive% ONote namespace ONote /-- Notation for 0 -/ instance : Zero ONote := ⟨zero⟩ @[simp] theorem zero_def : zero = 0 := rfl instance : Inhabited ONote := ⟨0⟩ /-- Notation for 1 -/ instance : One ONote := ⟨oadd 0 1 0⟩ /-- Notation for ω -/ def omega : ONote := oadd 1 1 0 /-- The ordinal denoted by a notation -/ noncomputable def repr : ONote → Ordinal.{0} | 0 => 0 | oadd e n a => ω ^ repr e * n + repr a @[simp] theorem repr_zero : repr 0 = 0 := rfl attribute [simp] repr.eq_1 repr.eq_2 /-- Print `ω^s*n`, omitting `s` if `e = 0` or `e = 1`, and omitting `n` if `n = 1` -/ private def toString_aux (e : ONote) (n : ℕ) (s : String) : String := if e = 0 then toString n else (if e = 1 then "ω" else "ω^(" ++ s ++ ")") ++ if n = 1 then "" else "*" ++ toString n /-- Print an ordinal notation -/ def toString : ONote → String | zero => "0" | oadd e n 0 => toString_aux e n (toString e) | oadd e n a => toString_aux e n (toString e) ++ " + " ++ toString a open Lean in /-- Print an ordinal notation -/ def repr' (prec : ℕ) : ONote → Format | zero => "0" | oadd e n a => Repr.addAppParen ("oadd " ++ (repr' max_prec e) ++ " " ++ Nat.repr (n : ℕ) ++ " " ++ (repr' max_prec a)) prec instance : ToString ONote := ⟨toString⟩ instance : Repr ONote where reprPrec o prec := repr' prec o instance : Preorder ONote where le x y := repr x ≤ repr y lt x y := repr x < repr y le_refl _ := @le_refl Ordinal _ _ le_trans _ _ _ := @le_trans Ordinal _ _ _ _ lt_iff_le_not_ge _ _ := @lt_iff_le_not_ge Ordinal _ _ _ theorem lt_def {x y : ONote} : x < y ↔ repr x < repr y := Iff.rfl theorem le_def {x y : ONote} : x ≤ y ↔ repr x ≤ repr y := Iff.rfl @[gcongr] alias ⟨repr_le_repr, _⟩ := le_def @[gcongr] alias ⟨repr_lt_repr, _⟩ := lt_def instance : WellFoundedRelation ONote := ⟨(· < ·), InvImage.wf repr Ordinal.lt_wf⟩ /-- Convert a `Nat` into an ordinal -/ @[coe] def ofNat : ℕ → ONote | 0 => 0 | Nat.succ n => oadd 0 n.succPNat 0 @[simp] theorem ofNat_zero : ofNat 0 = 0 := rfl @[simp] theorem ofNat_succ (n) : ofNat (Nat.succ n) = oadd 0 n.succPNat 0 := rfl instance (priority := low) nat (n : ℕ) : OfNat ONote n where ofNat := ofNat n @[simp 1200] theorem ofNat_one : ofNat 1 = 1 := rfl @[simp] theorem repr_ofNat (n : ℕ) : repr (ofNat n) = n := by cases n <;> simp @[simp] theorem repr_one : repr 1 = (1 : ℕ) := repr_ofNat 1 theorem omega0_le_oadd (e n a) : ω ^ repr e ≤ repr (oadd e n a) := by refine le_trans ?_ (le_add_right _ _) simpa using (mul_le_mul_iff_right₀ <| opow_pos (repr e) omega0_pos).2 (Nat.cast_le.2 n.2) theorem oadd_pos (e n a) : 0 < oadd e n a := @lt_of_lt_of_le _ _ _ (ω ^ repr e) _ (opow_pos (repr e) omega0_pos) (omega0_le_oadd e n a) /-- Comparison of ordinal notations: `ω ^ e₁ * n₁ + a₁` is less than `ω ^ e₂ * n₂ + a₂` when either `e₁ < e₂`, or `e₁ = e₂` and `n₁ < n₂`, or `e₁ = e₂`, `n₁ = n₂`, and `a₁ < a₂`. -/ def cmp : ONote → ONote → Ordering | 0, 0 => Ordering.eq | _, 0 => Ordering.gt | 0, _ => Ordering.lt | _o₁@(oadd e₁ n₁ a₁), _o₂@(oadd e₂ n₂ a₂) => (cmp e₁ e₂).then <| (_root_.cmp (n₁ : ℕ) n₂).then (cmp a₁ a₂) theorem eq_of_cmp_eq : ∀ {o₁ o₂}, cmp o₁ o₂ = Ordering.eq → o₁ = o₂ | 0, 0, _ => rfl | oadd e n a, 0, h => by injection h | 0, oadd e n a, h => by injection h | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h => by revert h; simp only [cmp] cases h₁ : cmp e₁ e₂ <;> intro h <;> try cases h obtain rfl := eq_of_cmp_eq h₁ revert h; cases h₂ : _root_.cmp (n₁ : ℕ) n₂ <;> intro h <;> try cases h obtain rfl := eq_of_cmp_eq h rw [_root_.cmp, cmpUsing_eq_eq, not_lt, not_lt, ← le_antisymm_iff] at h₂ obtain rfl := Subtype.eq h₂ simp protected theorem zero_lt_one : (0 : ONote) < 1 := by simp only [lt_def, repr_zero, repr_one, Nat.cast_one, zero_lt_one] /-- `NFBelow o b` says that `o` is a normal form ordinal notation satisfying `repr o < ω ^ b`. -/ inductive NFBelow : ONote → Ordinal.{0} → Prop | zero {b} : NFBelow 0 b | oadd' {e n a eb b} : NFBelow e eb → NFBelow a (repr e) → repr e < b → NFBelow (oadd e n a) b /-- A normal form ordinal notation has the form `ω ^ a₁ * n₁ + ω ^ a₂ * n₂ + ⋯ + ω ^ aₖ * nₖ` where `a₁ > a₂ > ⋯ > aₖ` and all the `aᵢ` are also in normal form. We will essentially only be interested in normal form ordinal notations, but to avoid complicating the algorithms, we define everything over general ordinal notations and only prove correctness with normal form as an invariant. -/ class NF (o : ONote) : Prop where out : Exists (NFBelow o) instance NF.zero : NF 0 := ⟨⟨0, NFBelow.zero⟩⟩ theorem NFBelow.oadd {e n a b} : NF e → NFBelow a (repr e) → repr e < b → NFBelow (oadd e n a) b | ⟨⟨_, h⟩⟩ => NFBelow.oadd' h theorem NFBelow.fst {e n a b} (h : NFBelow (ONote.oadd e n a) b) : NF e := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact ⟨⟨_, h₁⟩⟩ theorem NF.fst {e n a} : NF (oadd e n a) → NF e | ⟨⟨_, h⟩⟩ => h.fst theorem NFBelow.snd {e n a b} (h : NFBelow (ONote.oadd e n a) b) : NFBelow a (repr e) := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact h₂ theorem NF.snd' {e n a} : NF (oadd e n a) → NFBelow a (repr e) | ⟨⟨_, h⟩⟩ => h.snd theorem NF.snd {e n a} (h : NF (oadd e n a)) : NF a := ⟨⟨_, h.snd'⟩⟩ theorem NF.oadd {e a} (h₁ : NF e) (n) (h₂ : NFBelow a (repr e)) : NF (oadd e n a) := ⟨⟨_, NFBelow.oadd h₁ h₂ (lt_succ _)⟩⟩ instance NF.oadd_zero (e n) [h : NF e] : NF (ONote.oadd e n 0) := h.oadd _ NFBelow.zero theorem NFBelow.lt {e n a b} (h : NFBelow (ONote.oadd e n a) b) : repr e < b := by obtain - | ⟨h₁, h₂, h₃⟩ := h; exact h₃ theorem NFBelow_zero : ∀ {o}, NFBelow o 0 ↔ o = 0 | 0 => ⟨fun _ => rfl, fun _ => NFBelow.zero⟩ | oadd _ _ _ => ⟨fun h => (not_le_of_gt h.lt).elim (Ordinal.zero_le _), fun e => e.symm ▸ NFBelow.zero⟩ theorem NF.zero_of_zero {e n a} (h : NF (ONote.oadd e n a)) (e0 : e = 0) : a = 0 := by simpa [e0, NFBelow_zero] using h.snd' theorem NFBelow.repr_lt {o b} (h : NFBelow o b) : repr o < ω ^ b := by induction h with | zero => exact opow_pos _ omega0_pos | oadd' _ _ h₃ _ IH => rw [repr] apply (add_lt_add_left IH _).trans_le grw [← mul_succ, succ_le_of_lt (nat_lt_omega0 _), ← opow_succ, succ_le_of_lt h₃] exact omega0_pos theorem NFBelow.mono {o b₁ b₂} (bb : b₁ ≤ b₂) (h : NFBelow o b₁) : NFBelow o b₂ := by induction h with | zero => exact zero | oadd' h₁ h₂ h₃ _ _ => constructor; exacts [h₁, h₂, lt_of_lt_of_le h₃ bb] theorem NF.below_of_lt {e n a b} (H : repr e < b) : NF (ONote.oadd e n a) → NFBelow (ONote.oadd e n a) b | ⟨⟨b', h⟩⟩ => by (obtain - | ⟨h₁, h₂, h₃⟩ := h; exact NFBelow.oadd' h₁ h₂ H) theorem NF.below_of_lt' : ∀ {o b}, repr o < ω ^ b → NF o → NFBelow o b | 0, _, _, _ => NFBelow.zero | ONote.oadd _ _ _, _, H, h => h.below_of_lt <| (opow_lt_opow_iff_right one_lt_omega0).1 <| lt_of_le_of_lt (omega0_le_oadd _ _ _) H theorem nfBelow_ofNat : ∀ n, NFBelow (ofNat n) 1 | 0 => NFBelow.zero | Nat.succ _ => NFBelow.oadd NF.zero NFBelow.zero zero_lt_one instance nf_ofNat (n) : NF (ofNat n) := ⟨⟨_, nfBelow_ofNat n⟩⟩ instance nf_one : NF 1 := by rw [← ofNat_one]; infer_instance theorem oadd_lt_oadd_1 {e₁ n₁ o₁ e₂ n₂ o₂} (h₁ : NF (oadd e₁ n₁ o₁)) (h : e₁ < e₂) : oadd e₁ n₁ o₁ < oadd e₂ n₂ o₂ := @lt_of_lt_of_le _ _ (repr (oadd e₁ n₁ o₁)) _ _ (NF.below_of_lt h h₁).repr_lt (omega0_le_oadd e₂ n₂ o₂) theorem oadd_lt_oadd_2 {e o₁ o₂ : ONote} {n₁ n₂ : ℕ+} (h₁ : NF (oadd e n₁ o₁)) (h : (n₁ : ℕ) < n₂) : oadd e n₁ o₁ < oadd e n₂ o₂ := by simp only [lt_def, repr] refine lt_of_lt_of_le ((add_lt_add_iff_left _).2 h₁.snd'.repr_lt) (le_trans ?_ (le_add_right _ _)) rwa [← mul_succ, mul_le_mul_iff_right₀ (opow_pos _ omega0_pos), succ_le_iff, Nat.cast_lt] theorem oadd_lt_oadd_3 {e n a₁ a₂} (h : a₁ < a₂) : oadd e n a₁ < oadd e n a₂ := by rw [lt_def]; unfold repr; gcongr theorem cmp_compares : ∀ (a b : ONote) [NF a] [NF b], (cmp a b).Compares a b | 0, 0, _, _ => rfl | oadd _ _ _, 0, _, _ => oadd_pos _ _ _ | 0, oadd _ _ _, _, _ => oadd_pos _ _ _ | o₁@(oadd e₁ n₁ a₁), o₂@(oadd e₂ n₂ a₂), h₁, h₂ => by -- TODO: golf rw [cmp] have IHe := @cmp_compares _ _ h₁.fst h₂.fst simp only [Ordering.Compares, gt_iff_lt] at IHe; revert IHe cases cmp e₁ e₂ case lt => intro IHe; exact oadd_lt_oadd_1 h₁ IHe case gt => intro IHe; exact oadd_lt_oadd_1 h₂ IHe case eq => intro IHe; dsimp at IHe; subst IHe unfold _root_.cmp; cases nh : cmpUsing (· < ·) (n₁ : ℕ) n₂ <;> rw [cmpUsing, ite_eq_iff, not_lt] at nh case lt => rcases nh with nh | nh · exact oadd_lt_oadd_2 h₁ nh.left · rw [ite_eq_iff] at nh; rcases nh.right with nh | nh <;> cases nh <;> contradiction case gt => rcases nh with nh | nh · cases nh; contradiction · obtain ⟨_, nh⟩ := nh rw [ite_eq_iff] at nh; rcases nh with nh | nh · exact oadd_lt_oadd_2 h₂ nh.left · cases nh; contradiction rcases nh with nh | nh · cases nh; contradiction obtain ⟨nhl, nhr⟩ := nh rw [ite_eq_iff] at nhr rcases nhr with nhr | nhr · cases nhr; contradiction obtain rfl := Subtype.eq (nhl.eq_of_not_lt nhr.1) have IHa := @cmp_compares _ _ h₁.snd h₂.snd revert IHa; cases cmp a₁ a₂ <;> intro IHa <;> dsimp at IHa case lt => exact oadd_lt_oadd_3 IHa case gt => exact oadd_lt_oadd_3 IHa subst IHa; exact rfl theorem repr_inj {a b} [NF a] [NF b] : repr a = repr b ↔ a = b := ⟨fun e => match cmp a b, cmp_compares a b with | Ordering.lt, (h : repr a < repr b) => (ne_of_lt h e).elim | Ordering.gt, (h : repr a > repr b)=> (ne_of_gt h e).elim | Ordering.eq, h => h, congr_arg _⟩ theorem NF.of_dvd_omega0_opow {b e n a} (h : NF (ONote.oadd e n a)) (d : ω ^ b ∣ repr (ONote.oadd e n a)) : b ≤ repr e ∧ ω ^ b ∣ repr a := by have := mt repr_inj.1 (fun h => by injection h : ONote.oadd e n a ≠ 0) have L := le_of_not_gt fun l => not_le_of_gt (h.below_of_lt l).repr_lt (le_of_dvd this d) simp only [repr] at d exact ⟨L, (dvd_add_iff <| (opow_dvd_opow _ L).mul_right _).1 d⟩ theorem NF.of_dvd_omega0 {e n a} (h : NF (ONote.oadd e n a)) : ω ∣ repr (ONote.oadd e n a) → repr e ≠ 0 ∧ ω ∣ repr a := by (rw [← opow_one ω, ← one_le_iff_ne_zero]; exact h.of_dvd_omega0_opow) /-- `TopBelow b o` asserts that the largest exponent in `o`, if it exists, is less than `b`. This is an auxiliary definition for decidability of `NF`. -/ def TopBelow (b : ONote) : ONote → Prop | 0 => True | oadd e _ _ => cmp e b = Ordering.lt instance decidableTopBelow : DecidableRel TopBelow := by intro b o cases o <;> delta TopBelow <;> infer_instance theorem nfBelow_iff_topBelow {b} [NF b] : ∀ {o}, NFBelow o (repr b) ↔ NF o ∧ TopBelow b o | 0 => ⟨fun h => ⟨⟨⟨_, h⟩⟩, trivial⟩, fun _ => NFBelow.zero⟩ | oadd _ _ _ => ⟨fun h => ⟨⟨⟨_, h⟩⟩, (@cmp_compares _ b h.fst _).eq_lt.2 h.lt⟩, fun ⟨h₁, h₂⟩ => h₁.below_of_lt <| (@cmp_compares _ b h₁.fst _).eq_lt.1 h₂⟩ instance decidableNF : DecidablePred NF | 0 => isTrue NF.zero | oadd e n a => by have := decidableNF e have := decidableNF a apply decidable_of_iff (NF e ∧ NF a ∧ TopBelow e a) rw [← and_congr_right fun h => @nfBelow_iff_topBelow _ h _] exact ⟨fun ⟨h₁, h₂⟩ => NF.oadd h₁ n h₂, fun h => ⟨h.fst, h.snd'⟩⟩ /-- Auxiliary definition for `add` -/ def addAux (e : ONote) (n : ℕ+) (o : ONote) : ONote := match o with | 0 => oadd e n 0 | o'@(oadd e' n' a') => match cmp e e' with | Ordering.lt => o' | Ordering.eq => oadd e (n + n') a' | Ordering.gt => oadd e n o' /-- Addition of ordinal notations (correct only for normal input) -/ def add : ONote → ONote → ONote | 0, o => o | oadd e n a, o => addAux e n (add a o) instance : Add ONote := ⟨add⟩ @[simp] theorem zero_add (o : ONote) : 0 + o = o := rfl theorem oadd_add (e n a o) : oadd e n a + o = addAux e n (a + o) := rfl /-- Subtraction of ordinal notations (correct only for normal input) -/ def sub : ONote → ONote → ONote | 0, _ => 0 | o, 0 => o | o₁@(oadd e₁ n₁ a₁), oadd e₂ n₂ a₂ => match cmp e₁ e₂ with | Ordering.lt => 0 | Ordering.gt => o₁ | Ordering.eq => match (n₁ : ℕ) - n₂ with | 0 => if n₁ = n₂ then sub a₁ a₂ else 0 | Nat.succ k => oadd e₁ k.succPNat a₁ instance : Sub ONote := ⟨sub⟩ theorem add_nfBelow {b} : ∀ {o₁ o₂}, NFBelow o₁ b → NFBelow o₂ b → NFBelow (o₁ + o₂) b | 0, _, _, h₂ => h₂ | oadd e n a, o, h₁, h₂ => by have h' := add_nfBelow (h₁.snd.mono <| le_of_lt h₁.lt) h₂ simp only [oadd_add]; revert h'; obtain - | ⟨e', n', a'⟩ := a + o <;> intro h' · exact NFBelow.oadd h₁.fst NFBelow.zero h₁.lt have : ((e.cmp e').Compares e e') := @cmp_compares _ _ h₁.fst h'.fst cases h : cmp e e' <;> dsimp [addAux] <;> simp only [h] · exact h' · simp only [h] at this subst e' exact NFBelow.oadd h'.fst h'.snd h'.lt · simp only [h] at this exact NFBelow.oadd h₁.fst (NF.below_of_lt this ⟨⟨_, h'⟩⟩) h₁.lt instance add_nf (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ + o₂) | ⟨⟨b₁, h₁⟩⟩, ⟨⟨b₂, h₂⟩⟩ => ⟨(le_total b₁ b₂).elim (fun h => ⟨b₂, add_nfBelow (h₁.mono h) h₂⟩) fun h => ⟨b₁, add_nfBelow h₁ (h₂.mono h)⟩⟩ @[simp] theorem repr_add : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ + o₂) = repr o₁ + repr o₂ | 0, o, _, _ => by simp | oadd e n a, o, h₁, h₂ => by haveI := h₁.snd; have h' := repr_add a o conv_lhs at h' => simp [HAdd.hAdd, Add.add] have nf := ONote.add_nf a o conv at nf => simp [HAdd.hAdd, Add.add] conv in _ + o => simp [HAdd.hAdd, Add.add] rcases h : add a o with - | ⟨e', n', a'⟩ <;> simp only [add, addAux, h'.symm, h, add_assoc, repr] at nf h₁ ⊢ have := h₁.fst; haveI := nf.fst; have ee := cmp_compares e e' cases he : cmp e e' <;> simp only [he, Ordering.compares_gt, Ordering.compares_lt, Ordering.compares_eq, repr, gt_iff_lt, PNat.add_coe, Nat.cast_add] at ee ⊢ · rw [← add_assoc, @add_absorp _ (repr e') (ω ^ repr e' * (n' : ℕ))] · have := (h₁.below_of_lt ee).repr_lt unfold repr at this cases he' : e' <;> simp only [he', zero_def, opow_zero, repr, gt_iff_lt] at this ⊢ <;> exact lt_of_le_of_lt (le_add_right _ _) this · simpa using (mul_le_mul_iff_right₀ <| opow_pos (repr e') omega0_pos).2 (Nat.cast_le.2 n'.pos) · rw [ee, ← add_assoc, ← mul_add] theorem sub_nfBelow : ∀ {o₁ o₂ b}, NFBelow o₁ b → NF o₂ → NFBelow (o₁ - o₂) b | 0, o, b, _, h₂ => by cases o <;> exact NFBelow.zero | oadd _ _ _, 0, _, h₁, _ => h₁ | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, b, h₁, h₂ => by have h' := sub_nfBelow h₁.snd h₂.snd simp only [HSub.hSub, Sub.sub, sub] at h' ⊢ have := @cmp_compares _ _ h₁.fst h₂.fst cases h : cmp e₁ e₂ · apply NFBelow.zero · rw [Nat.sub_eq] simp only [h, Ordering.compares_eq] at this subst e₂ cases (n₁ : ℕ) - n₂ · by_cases en : n₁ = n₂ <;> simp only [en, ↓reduceIte] · exact h'.mono (le_of_lt h₁.lt) · exact NFBelow.zero · exact NFBelow.oadd h₁.fst h₁.snd h₁.lt · exact h₁ instance sub_nf (o₁ o₂) : ∀ [NF o₁] [NF o₂], NF (o₁ - o₂) | ⟨⟨b₁, h₁⟩⟩, h₂ => ⟨⟨b₁, sub_nfBelow h₁ h₂⟩⟩ @[simp] theorem repr_sub : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ - o₂) = repr o₁ - repr o₂ | 0, o, _, h₂ => by cases o <;> exact (Ordinal.zero_sub _).symm | oadd _ _ _, 0, _, _ => (Ordinal.sub_zero _).symm | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h₁, h₂ => by haveI := h₁.snd; haveI := h₂.snd; have h' := repr_sub a₁ a₂ conv_lhs at h' => dsimp [HSub.hSub, Sub.sub, sub] conv_lhs => dsimp only [HSub.hSub, Sub.sub]; dsimp only [sub] have ee := @cmp_compares _ _ h₁.fst h₂.fst cases h : cmp e₁ e₂ <;> simp only [h] at ee · rw [Ordinal.sub_eq_zero_iff_le.2] · rfl exact le_of_lt (oadd_lt_oadd_1 h₁ ee) · change e₁ = e₂ at ee subst e₂ dsimp only cases mn : (n₁ : ℕ) - n₂ <;> dsimp only · by_cases en : n₁ = n₂ · simpa [en] · simp only [en, ite_false] exact (Ordinal.sub_eq_zero_iff_le.2 <| le_of_lt <| oadd_lt_oadd_2 h₁ <| lt_of_le_of_ne (tsub_eq_zero_iff_le.1 mn) (mt PNat.eq en)).symm · simp only [Nat.succPNat, Nat.succ_eq_add_one, repr, PNat.mk_coe, Nat.cast_add, Nat.cast_one, add_one_eq_succ] rw [(tsub_eq_iff_eq_add_of_le <| le_of_lt <| Nat.lt_of_sub_eq_succ mn).1 mn, add_comm, Nat.cast_add, mul_add, add_assoc, add_sub_add_cancel] refine (Ordinal.sub_eq_of_add_eq <| add_absorp h₂.snd'.repr_lt <| le_trans ?_ (le_add_right _ _)).symm exact Ordinal.le_mul_left _ (Nat.cast_lt.2 <| Nat.succ_pos _) · exact (Ordinal.sub_eq_of_add_eq <| add_absorp (h₂.below_of_lt ee).repr_lt <| omega0_le_oadd _ _ _).symm /-- Multiplication of ordinal notations (correct only for normal input) -/ def mul : ONote → ONote → ONote | 0, _ => 0 | _, 0 => 0 | o₁@(oadd e₁ n₁ a₁), oadd e₂ n₂ a₂ => if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else oadd (e₁ + e₂) n₂ (mul o₁ a₂) instance : Mul ONote := ⟨mul⟩ instance : MulZeroClass ONote where zero_mul o := by cases o <;> rfl mul_zero o := by cases o <;> rfl theorem oadd_mul (e₁ n₁ a₁ e₂ n₂ a₂) : oadd e₁ n₁ a₁ * oadd e₂ n₂ a₂ = if e₂ = 0 then oadd e₁ (n₁ * n₂) a₁ else oadd (e₁ + e₂) n₂ (oadd e₁ n₁ a₁ * a₂) := rfl theorem oadd_mul_nfBelow {e₁ n₁ a₁ b₁} (h₁ : NFBelow (oadd e₁ n₁ a₁) b₁) : ∀ {o₂ b₂}, NFBelow o₂ b₂ → NFBelow (oadd e₁ n₁ a₁ * o₂) (repr e₁ + b₂) | 0, _, _ => NFBelow.zero | oadd e₂ n₂ a₂, b₂, h₂ => by have IH := oadd_mul_nfBelow h₁ h₂.snd by_cases e0 : e₂ = 0 <;> simp only [e0, oadd_mul, ↓reduceIte] · apply NFBelow.oadd h₁.fst h₁.snd simpa using (add_lt_add_iff_left (repr e₁)).2 (lt_of_le_of_lt (Ordinal.zero_le _) h₂.lt) · haveI := h₁.fst haveI := h₂.fst apply NFBelow.oadd · infer_instance · rwa [repr_add] · rw [repr_add, add_lt_add_iff_left] exact h₂.lt instance mul_nf : ∀ (o₁ o₂) [NF o₁] [NF o₂], NF (o₁ * o₂) | 0, o, _, h₂ => by cases o <;> exact NF.zero | oadd _ _ _, _, ⟨⟨_, hb₁⟩⟩, ⟨⟨_, hb₂⟩⟩ => ⟨⟨_, oadd_mul_nfBelow hb₁ hb₂⟩⟩ @[simp] theorem repr_mul : ∀ (o₁ o₂) [NF o₁] [NF o₂], repr (o₁ * o₂) = repr o₁ * repr o₂ | 0, o, _, h₂ => by cases o <;> exact (zero_mul _).symm | oadd _ _ _, 0, _, _ => (mul_zero _).symm | oadd e₁ n₁ a₁, oadd e₂ n₂ a₂, h₁, h₂ => by have IH : repr (mul _ _) = _ := @repr_mul _ _ h₁ h₂.snd conv => lhs simp [(· * ·)] have ao : repr a₁ + ω ^ repr e₁ * (n₁ : ℕ) = ω ^ repr e₁ * (n₁ : ℕ) := by apply add_absorp h₁.snd'.repr_lt simpa using (mul_le_mul_iff_right₀ <| opow_pos _ omega0_pos).2 (Nat.cast_le.2 n₁.2) by_cases e0 : e₂ = 0 · obtain ⟨x, xe⟩ := Nat.exists_eq_succ_of_ne_zero n₂.ne_zero simp only [Mul.mul, mul, e0, ↓reduceIte, repr, PNat.mul_coe, natCast_mul, opow_zero, one_mul] simp only [xe, h₂.zero_of_zero e0, repr, add_zero] rw [natCast_succ x, add_mul_succ _ ao, mul_assoc] · simp only [repr] haveI := h₁.fst haveI := h₂.fst simp only [Mul.mul, mul, e0, ite_false, repr.eq_2, repr_add, opow_add, IH, repr, mul_add] rw [← mul_assoc] congr 2 have := mt repr_inj.1 e0 rw [add_mul_of_isSuccLimit ao (isSuccLimit_opow_left isSuccLimit_omega0 this), mul_assoc, mul_omega0_dvd (Nat.cast_pos'.2 n₁.pos) (nat_lt_omega0 _)] simpa using opow_dvd_opow ω (one_le_iff_ne_zero.2 this) /-- Calculate division and remainder of `o` mod `ω`: `split' o = (a, n)` means `o = ω * a + n`. -/ def split' : ONote → ONote × ℕ | 0 => (0, 0) | oadd e n a => if e = 0 then (0, n) else let (a', m) := split' a (oadd (e - 1) n a', m) /-- Calculate division and remainder of `o` mod `ω`: `split o = (a, n)` means `o = a + n`, where `ω ∣ a`. -/ def split : ONote → ONote × ℕ | 0 => (0, 0) | oadd e n a => if e = 0 then (0, n) else let (a', m) := split a (oadd e n a', m) /-- `scale x o` is the ordinal notation for `ω ^ x * o`. -/ def scale (x : ONote) : ONote → ONote | 0 => 0 | oadd e n a => oadd (x + e) n (scale x a) /-- `mulNat o n` is the ordinal notation for `o * n`. -/ def mulNat : ONote → ℕ → ONote | 0, _ => 0 | _, 0 => 0 | oadd e n a, m + 1 => oadd e (n * m.succPNat) a /-- Auxiliary definition to compute the ordinal notation for the ordinal exponentiation in `opow` -/ def opowAux (e a0 a : ONote) : ℕ → ℕ → ONote | _, 0 => 0 | 0, m + 1 => oadd e m.succPNat 0 | k + 1, m => scale (e + mulNat a0 k) a + (opowAux e a0 a k m) /-- Auxiliary definition to compute the ordinal notation for the ordinal exponentiation in `opow` -/ def opowAux2 (o₂ : ONote) (o₁ : ONote × ℕ) : ONote := match o₁ with | (0, 0) => if o₂ = 0 then 1 else 0 | (0, 1) => 1 | (0, m + 1) => let (b', k) := split' o₂ oadd b' (m.succPNat ^ k) 0 | (a@(oadd a0 _ _), m) => match split o₂ with | (b, 0) => oadd (a0 * b) 1 0 | (b, k + 1) => let eb := a0 * b scale (eb + mulNat a0 k) a + opowAux eb a0 (mulNat a m) k m /-- `opow o₁ o₂` calculates the ordinal notation for the ordinal exponential `o₁ ^ o₂`. -/ def opow (o₁ o₂ : ONote) : ONote := opowAux2 o₂ (split o₁) instance : Pow ONote ONote := ⟨opow⟩ theorem opow_def (o₁ o₂ : ONote) : o₁ ^ o₂ = opowAux2 o₂ (split o₁) := rfl theorem split_eq_scale_split' : ∀ {o o' m} [NF o], split' o = (o', m) → split o = (scale 1 o', m) | 0, o', m, _, p => by injection p; substs o' m; rfl | oadd e n a, o', m, h, p => by by_cases e0 : e = 0 <;> simp only [split', e0, ↓reduceIte, Prod.mk.injEq, split] at p ⊢ · rcases p with ⟨rfl, rfl⟩ exact ⟨rfl, rfl⟩ · revert p rcases h' : split' a with ⟨a', m'⟩ haveI := h.fst haveI := h.snd simp only [split_eq_scale_split' h', and_imp] have : 1 + (e - 1) = e := by refine repr_inj.1 ?_ simp only [repr_add, repr_one, Nat.cast_one, repr_sub] have := mt repr_inj.1 e0 exact Ordinal.add_sub_cancel_of_le <| one_le_iff_ne_zero.2 this intros substs o' m simp [scale, this] theorem nf_repr_split' : ∀ {o o' m} [NF o], split' o = (o', m) → NF o' ∧ repr o = ω * repr o' + m | 0, o', m, _, p => by injection p; substs o' m; simp [NF.zero] | oadd e n a, o', m, h, p => by by_cases e0 : e = 0 <;> simp only [split', e0, ↓reduceIte, Prod.mk.injEq, repr, repr_zero, opow_zero, one_mul] at p ⊢ · rcases p with ⟨rfl, rfl⟩ simp [h.zero_of_zero e0, NF.zero] · revert p rcases h' : split' a with ⟨a', m'⟩ haveI := h.fst haveI := h.snd obtain ⟨IH₁, IH₂⟩ := nf_repr_split' h' simp only [IH₂, and_imp] intros substs o' m have : (ω : Ordinal.{0}) ^ repr e = ω ^ (1 : Ordinal.{0}) * ω ^ (repr e - 1) := by have := mt repr_inj.1 e0 rw [← opow_add, Ordinal.add_sub_cancel_of_le (one_le_iff_ne_zero.2 this)] refine ⟨NF.oadd (by infer_instance) _ ?_, ?_⟩ · simp only [opow_one, repr_sub, repr_one, Nat.cast_one] at this ⊢ refine IH₁.below_of_lt' <| (mul_lt_mul_iff_right₀ omega0_pos).1 <| lt_of_le_of_lt (le_add_right _ m') ?_ rw [← this, ← IH₂] exact h.snd'.repr_lt · rw [this] simp [mul_add, mul_assoc, add_assoc] theorem scale_eq_mul (x) [NF x] : ∀ (o) [NF o], scale x o = oadd x 1 0 * o | 0, _ => rfl | oadd e n a, h => by simp only [HMul.hMul]; simp only [scale] haveI := h.snd by_cases e0 : e = 0 · simp_rw [scale_eq_mul] simp [Mul.mul, mul, e0, h.zero_of_zero, show x + 0 = x from repr_inj.1 (by simp)] · simp [e0, Mul.mul, mul, scale_eq_mul, (· * ·)] instance nf_scale (x) [NF x] (o) [NF o] : NF (scale x o) := by rw [scale_eq_mul] infer_instance @[simp] theorem repr_scale (x) [NF x] (o) [NF o] : repr (scale x o) = ω ^ repr x * repr o := by simp only [scale_eq_mul, repr_mul, repr, PNat.one_coe, Nat.cast_one, mul_one, add_zero] theorem nf_repr_split {o o' m} [NF o] (h : split o = (o', m)) : NF o' ∧ repr o = repr o' + m := by rcases e : split' o with ⟨a, n⟩ obtain ⟨s₁, s₂⟩ := nf_repr_split' e rw [split_eq_scale_split' e] at h injection h; substs o' n simp only [repr_scale, repr_one, Nat.cast_one, opow_one, ← s₂, and_true] infer_instance theorem split_dvd {o o' m} [NF o] (h : split o = (o', m)) : ω ∣ repr o' := by rcases e : split' o with ⟨a, n⟩ rw [split_eq_scale_split' e] at h injection h; subst o' cases nf_repr_split' e; simp theorem split_add_lt {o e n a m} [NF o] (h : split o = (oadd e n a, m)) : repr a + m < ω ^ repr e := by obtain ⟨h₁, h₂⟩ := nf_repr_split h obtain ⟨e0, d⟩ := h₁.of_dvd_omega0 (split_dvd h) apply principal_add_omega0_opow _ h₁.snd'.repr_lt (lt_of_lt_of_le (nat_lt_omega0 _) _) simpa using opow_le_opow_right omega0_pos (one_le_iff_ne_zero.2 e0) @[simp] theorem mulNat_eq_mul (n o) : mulNat o n = o * ofNat n := by cases o <;> cases n <;> rfl instance nf_mulNat (o) [NF o] (n) : NF (mulNat o n) := by simpa using ONote.mul_nf o (ofNat n) instance nf_opowAux (e a0 a) [NF e] [NF a0] [NF a] : ∀ k m, NF (opowAux e a0 a k m) := by intro k m unfold opowAux cases m with | zero => cases k <;> exact NF.zero | succ m => cases k with | zero => exact NF.oadd_zero _ _ | succ k => haveI := nf_opowAux e a0 a k simp only [mulNat_eq_mul]; infer_instance instance nf_opow (o₁ o₂) [NF o₁] [NF o₂] : NF (o₁ ^ o₂) := by rcases e₁ : split o₁ with ⟨a, m⟩ have na := (nf_repr_split e₁).1 rcases e₂ : split' o₂ with ⟨b', k⟩ haveI := (nf_repr_split' e₂).1 obtain - | ⟨a0, n, a'⟩ := a · rcases m with - | m · by_cases o₂ = 0 <;> simp only [(· ^ ·), Pow.pow, opow, opowAux2, *] <;> decide · by_cases m = 0 · simp only [(· ^ ·), Pow.pow, opow, opowAux2, *, zero_def] decide · simp only [(· ^ ·), Pow.pow, opow, opowAux2, *] infer_instance · simp only [(· ^ ·), Pow.pow, opow, opowAux2, e₁, split_eq_scale_split' e₂, mulNat_eq_mul] have := na.fst rcases k with - | k · infer_instance · cases k <;> cases m <;> infer_instance theorem scale_opowAux (e a0 a : ONote) [NF e] [NF a0] [NF a] : ∀ k m, repr (opowAux e a0 a k m) = ω ^ repr e * repr (opowAux 0 a0 a k m) | 0, m => by cases m <;> simp [opowAux] | k + 1, m => by by_cases h : m = 0 <;> simp only [h, opowAux, mulNat_eq_mul, repr_add, repr_scale, repr_mul, repr_ofNat, zero_add, mul_add, repr_zero, mul_zero, scale_opowAux e, opow_add, mul_assoc] theorem repr_opow_aux₁ {e a} [Ne : NF e] [Na : NF a] {a' : Ordinal} (e0 : repr e ≠ 0) (h : a' < (ω : Ordinal.{0}) ^ repr e) (aa : repr a = a') (n : ℕ+) : ((ω : Ordinal.{0}) ^ repr e * (n : ℕ) + a') ^ (ω : Ordinal.{0}) = (ω ^ repr e) ^ (ω : Ordinal.{0}) := by subst aa have No := Ne.oadd n (Na.below_of_lt' h) have := omega0_le_oadd e n a rw [repr] at this refine le_antisymm ?_ (opow_le_opow_left _ this) apply (opow_le_of_isSuccLimit ((opow_pos _ omega0_pos).trans_le this).ne' isSuccLimit_omega0).2 intro b l have := (No.below_of_lt (lt_succ _)).repr_lt rw [repr] at this apply (opow_le_opow_left b <| this.le).trans rw [← opow_mul, ← opow_mul] rcases le_or_gt ω (repr e) with h | h · grw [le_succ b, ← add_one_eq_succ, add_mul_succ _ (one_add_of_omega0_le h), add_one_eq_succ] · gcongr · exact omega0_pos · exact succ_le_iff.2 <| by gcongr; exact isSuccLimit_omega0.succ_lt l · exact omega0_pos · grw [show _ * _ < _ from principal_mul_omega0 (isSuccLimit_omega0.succ_lt h) l] · simpa using mul_le_mul_right' (one_le_iff_ne_zero.2 e0) ω · exact omega0_pos section theorem repr_opow_aux₂ {a0 a'} [N0 : NF a0] [Na' : NF a'] (m : ℕ) (d : ω ∣ repr a') (e0 : repr a0 ≠ 0) (h : repr a' + m < (ω ^ repr a0)) (n : ℕ+) (k : ℕ) : let R := repr (opowAux 0 a0 (oadd a0 n a' * ofNat m) k m) (k ≠ 0 → R < ((ω ^ repr a0) ^ succ (k : Ordinal))) ∧ ((ω ^ repr a0) ^ (k : Ordinal)) * ((ω ^ repr a0) * (n : ℕ) + repr a') + R = ((ω ^ repr a0) * (n : ℕ) + repr a' + m) ^ succ (k : Ordinal) := by intro R' haveI No : NF (oadd a0 n a') := N0.oadd n (Na'.below_of_lt' <| lt_of_le_of_lt (le_add_right _ _) h) induction k with | zero => cases m <;> simp [R', opowAux] | succ k IH => -- rename R => R' let R := repr (opowAux 0 a0 (oadd a0 n a' * ofNat m) k m) let ω0 := ω ^ repr a0 let α' := ω0 * n + repr a' change (k ≠ 0 → R < (ω0 ^ succ (k : Ordinal))) ∧ (ω0 ^ (k : Ordinal)) * α' + R = (α' + m) ^ (succ ↑k : Ordinal) at IH have RR : R' = ω0 ^ (k : Ordinal) * (α' * m) + R := by by_cases h : m = 0 · simp only [R, R', h, ONote.ofNat, Nat.cast_zero, ONote.repr, mul_zero, ONote.opowAux, add_zero] · simp only [α', ω0, R, R', ONote.repr_scale, ONote.repr, ONote.mulNat_eq_mul, ONote.opowAux, ONote.repr_ofNat, ONote.repr_mul, ONote.repr_add, Ordinal.opow_mul, ONote.zero_add] have α0 : 0 < α' := by simpa [lt_def, repr] using oadd_pos a0 n a' have ω00 : 0 < ω0 ^ (k : Ordinal) := opow_pos _ (opow_pos _ omega0_pos) have Rl : R < ω ^ (repr a0 * succ ↑k) := by by_cases k0 : k = 0 · simp only [k0, Nat.cast_zero, succ_zero, mul_one, R] refine lt_of_lt_of_le ?_ (opow_le_opow_right omega0_pos (one_le_iff_ne_zero.2 e0)) rcases m with - | m <;> simp [opowAux, omega0_pos] rw [← add_one_eq_succ, ← Nat.cast_succ] apply nat_lt_omega0 · rw [opow_mul] exact IH.1 k0 refine ⟨fun _ => ?_, ?_⟩ · rw [RR, ← opow_mul _ _ (succ k.succ)] have e0 := Ordinal.pos_iff_ne_zero.2 e0 have rr0 : 0 < repr a0 + repr a0 := lt_of_lt_of_le e0 (le_add_left _ _) apply principal_add_omega0_opow · simp only [Nat.succ_eq_add_one, Nat.cast_add, Nat.cast_one, add_one_eq_succ, opow_mul, opow_succ, mul_assoc] gcongr ?_ * ?_ rw [← Ordinal.opow_add] have : _ < ω ^ (repr a0 + repr a0) := (No.below_of_lt ?_).repr_lt · exact mul_lt_omega0_opow rr0 this (nat_lt_omega0 _) · simpa using (add_lt_add_iff_left (repr a0)).2 e0 · exact lt_of_lt_of_le Rl (opow_le_opow_right omega0_pos <| mul_le_mul_left' (succ_le_succ_iff.2 (Nat.cast_le.2 (le_of_lt k.lt_succ_self))) _) calc (ω0 ^ (k.succ : Ordinal)) * α' + R' _ = (ω0 ^ succ (k : Ordinal)) * α' + ((ω0 ^ (k : Ordinal)) * α' * m + R) := by rw [natCast_succ, RR, ← mul_assoc] _ = ((ω0 ^ (k : Ordinal)) * α' + R) * α' + ((ω0 ^ (k : Ordinal)) * α' + R) * m := ?_ _ = (α' + m) ^ succ (k.succ : Ordinal) := by rw [← mul_add, natCast_succ, opow_succ, IH.2] congr 1 · have αd : ω ∣ α' := dvd_add (dvd_mul_of_dvd_left (by simpa using opow_dvd_opow ω (one_le_iff_ne_zero.2 e0)) _) d rw [mul_add (ω0 ^ (k : Ordinal)), add_assoc, ← mul_assoc, ← opow_succ, add_mul_of_isSuccLimit _ (isSuccLimit_iff_omega0_dvd.2 ⟨ne_of_gt α0, αd⟩), mul_assoc, @mul_omega0_dvd n (Nat.cast_pos'.2 n.pos) (nat_lt_omega0 _) _ αd] apply @add_absorp _ (repr a0 * succ ↑k) · refine principal_add_omega0_opow _ ?_ Rl rw [opow_mul, opow_succ] gcongr exact No.snd'.repr_lt · have := mul_le_mul_left' (one_le_iff_pos.2 <| Nat.cast_pos'.2 n.pos) (ω0 ^ succ (k : Ordinal)) rw [opow_mul] simpa [-opow_succ] · cases m · have : R = 0 := by cases k <;> simp [R, opowAux] simp [this] · rw [natCast_succ, add_mul_succ] apply add_absorp Rl rw [opow_mul, opow_succ] gcongr simpa [repr] using omega0_le_oadd a0 n a' end theorem repr_opow (o₁ o₂) [NF o₁] [NF o₂] : repr (o₁ ^ o₂) = repr o₁ ^ repr o₂ := by rcases e₁ : split o₁ with ⟨a, m⟩ obtain ⟨N₁, r₁⟩ := nf_repr_split e₁ obtain - | ⟨a0, n, a'⟩ := a · rcases m with - | m · by_cases h : o₂ = 0 <;> simp [opow_def, opowAux2, e₁, h, r₁] have := mt repr_inj.1 h rw [zero_opow this] · rcases e₂ : split' o₂ with ⟨b', k⟩ obtain ⟨_, r₂⟩ := nf_repr_split' e₂ by_cases h : m = 0 · simp [opowAux2, opow_def, e₁, h, r₁, r₂] simp only [opow_def, opowAux2, e₁, r₁, e₂, r₂, repr, Nat.cast_succ, _root_.zero_add, add_zero] rw [opow_add, opow_mul, opow_omega0, add_one_eq_succ] · simp · simpa [Nat.one_le_iff_ne_zero] · rw [← Nat.cast_succ, lt_omega0] exact ⟨_, rfl⟩ · haveI := N₁.fst haveI := N₁.snd obtain ⟨a00, ad⟩ := N₁.of_dvd_omega0 (split_dvd e₁) have al := split_add_lt e₁ have aa : repr (a' + ofNat m) = repr a' + m := by simp only [ONote.repr_ofNat, ONote.repr_add] rcases e₂ : split' o₂ with ⟨b', k⟩ obtain ⟨_, r₂⟩ := nf_repr_split' e₂ simp only [opow_def, e₁, r₁, split_eq_scale_split' e₂, opowAux2, repr] rcases k with - | k · simp [r₂, opow_mul, repr_opow_aux₁ a00 al aa, add_assoc] · simp [r₂, opow_add, opow_mul, mul_assoc, add_assoc] rw [repr_opow_aux₁ a00 al aa, scale_opowAux] simp only [repr_mul, repr_scale, repr, opow_zero, PNat.val_ofNat, Nat.cast_one, mul_one, add_zero, opow_one, opow_mul] rw [← mul_add, ← add_assoc ((ω : Ordinal.{0}) ^ repr a0 * (n : ℕ))] congr 1 rw [← pow_succ, ← opow_natCast, ← opow_natCast] exact (repr_opow_aux₂ _ ad a00 al _ _).2 /-- Given an ordinal, returns: * `inl none` for `0` * `inl (some a)` for `a + 1` * `inr f` for a limit ordinal `a`, where `f i` is a sequence converging to `a` -/ def fundamentalSequence : ONote → (Option ONote) ⊕ (ℕ → ONote) | zero => Sum.inl none | oadd a m b => match fundamentalSequence b with | Sum.inr f => Sum.inr fun i => oadd a m (f i) | Sum.inl (some b') => Sum.inl (some (oadd a m b')) | Sum.inl none => match fundamentalSequence a, m.natPred with | Sum.inl none, 0 => Sum.inl (some zero) | Sum.inl none, m + 1 => Sum.inl (some (oadd zero m.succPNat zero)) | Sum.inl (some a'), 0 => Sum.inr fun i => oadd a' i.succPNat zero | Sum.inl (some a'), m + 1 => Sum.inr fun i => oadd a m.succPNat (oadd a' i.succPNat zero) | Sum.inr f, 0 => Sum.inr fun i => oadd (f i) 1 zero | Sum.inr f, m + 1 => Sum.inr fun i => oadd a m.succPNat (oadd (f i) 1 zero) private theorem exists_lt_add {α} [hα : Nonempty α] {o : Ordinal} {f : α → Ordinal} (H : ∀ ⦃a⦄, a < o → ∃ i, a < f i) {b : Ordinal} ⦃a⦄ (h : a < b + o) : ∃ i, a < b + f i := by rcases lt_or_ge a b with h | h' · obtain ⟨i⟩ := id hα exact ⟨i, h.trans_le (le_add_right _ _)⟩ · rw [← Ordinal.add_sub_cancel_of_le h', add_lt_add_iff_left] at h refine (H h).imp fun i H => ?_ rwa [← Ordinal.add_sub_cancel_of_le h', add_lt_add_iff_left] private theorem exists_lt_mul_omega0' {o : Ordinal} ⦃a⦄ (h : a < o * ω) : ∃ i : ℕ, a < o * ↑i + o := by obtain ⟨i, hi, h'⟩ := (lt_mul_iff_of_isSuccLimit isSuccLimit_omega0).1 h obtain ⟨i, rfl⟩ := lt_omega0.1 hi exact ⟨i, h'.trans_le (le_add_right _ _)⟩ private theorem exists_lt_omega0_opow' {α} {o b : Ordinal} (hb : 1 < b) (ho : IsSuccLimit o) {f : α → Ordinal} (H : ∀ ⦃a⦄, a < o → ∃ i, a < f i) ⦃a⦄ (h : a < b ^ o) : ∃ i, a < b ^ f i := by obtain ⟨d, hd, h'⟩ := (lt_opow_of_isSuccLimit (zero_lt_one.trans hb).ne' ho).1 h exact (H hd).imp fun i hi => h'.trans <| (opow_lt_opow_iff_right hb).2 hi /-- The property satisfied by `fundamentalSequence o`: * `inl none` means `o = 0` * `inl (some a)` means `o = succ a` * `inr f` means `o` is a limit ordinal and `f` is a strictly increasing sequence which converges to `o` -/ def FundamentalSequenceProp (o : ONote) : (Option ONote) ⊕ (ℕ → ONote) → Prop | Sum.inl none => o = 0 | Sum.inl (some a) => o.repr = succ a.repr ∧ (o.NF → a.NF) | Sum.inr f => IsSuccLimit o.repr ∧ (∀ i, f i < f (i + 1) ∧ f i < o ∧ (o.NF → (f i).NF)) ∧ ∀ a, a < o.repr → ∃ i, a < (f i).repr theorem fundamentalSequenceProp_inl_none (o) : FundamentalSequenceProp o (Sum.inl none) ↔ o = 0 := Iff.rfl theorem fundamentalSequenceProp_inl_some (o a) : FundamentalSequenceProp o (Sum.inl (some a)) ↔ o.repr = succ a.repr ∧ (o.NF → a.NF) := Iff.rfl theorem fundamentalSequenceProp_inr (o f) : FundamentalSequenceProp o (Sum.inr f) ↔ IsSuccLimit o.repr ∧ (∀ i, f i < f (i + 1) ∧ f i < o ∧ (o.NF → (f i).NF)) ∧ ∀ a, a < o.repr → ∃ i, a < (f i).repr := Iff.rfl theorem fundamentalSequence_has_prop (o) : FundamentalSequenceProp o (fundamentalSequence o) := by induction o with | zero => exact rfl | oadd a m b iha ihb rw [fundamentalSequence] rcases e : b.fundamentalSequence with (⟨_ | b'⟩ | f) <;> simp only [FundamentalSequenceProp] <;> rw [e, FundamentalSequenceProp] at ihb · rcases e : a.fundamentalSequence with (⟨_ | a'⟩ | f) <;> rcases e' : m.natPred with - | m' <;> simp only <;> rw [e, FundamentalSequenceProp] at iha <;> (try rw [show m = 1 by have := PNat.natPred_add_one m; rw [e'] at this; exact PNat.coe_inj.1 this.symm]) <;> (try rw [show m = (m' + 1).succPNat by rw [← e', ← PNat.coe_inj, Nat.succPNat_coe, ← Nat.add_one, PNat.natPred_add_one]]) <;> simp only [repr, iha, ihb, opow_lt_opow_iff_right one_lt_omega0, add_lt_add_iff_left, add_zero, lt_add_iff_pos_right, lt_def, mul_one, Nat.cast_zero, Nat.cast_succ, Nat.succPNat_coe, opow_succ, opow_zero, mul_add_one, PNat.one_coe, succ_zero, _root_.zero_add, zero_def] · decide · exact ⟨rfl, inferInstance⟩ · have := opow_pos (repr a') omega0_pos refine ⟨isSuccLimit_mul this isSuccLimit_omega0, fun i => ⟨this, ?_, fun H => @NF.oadd_zero _ _ (iha.2 H.fst)⟩, exists_lt_mul_omega0'⟩ rw [← mul_succ, ← natCast_succ] gcongr apply nat_lt_omega0 · have := opow_pos (repr a') omega0_pos refine ⟨isSuccLimit_add _ (isSuccLimit_mul this isSuccLimit_omega0), fun i => ⟨this, ?_, ?_⟩, exists_lt_add exists_lt_mul_omega0'⟩ · rw [← mul_succ, ← natCast_succ] gcongr apply nat_lt_omega0 · refine fun H => H.fst.oadd _ (NF.below_of_lt' ?_ (@NF.oadd_zero _ _ (iha.2 H.fst))) rw [repr, ← zero_def, repr, add_zero, iha.1, opow_succ] gcongr apply nat_lt_omega0 · rcases iha with ⟨h1, h2, h3⟩ refine ⟨isSuccLimit_opow one_lt_omega0 h1, fun i => ?_, exists_lt_omega0_opow' one_lt_omega0 h1 h3⟩ obtain ⟨h4, h5, h6⟩ := h2 i exact ⟨h4, h5, fun H => @NF.oadd_zero _ _ (h6 H.fst)⟩ · rcases iha with ⟨h1, h2, h3⟩ refine ⟨isSuccLimit_add _ (isSuccLimit_opow one_lt_omega0 h1), fun i => ?_, exists_lt_add (exists_lt_omega0_opow' one_lt_omega0 h1 h3)⟩ obtain ⟨h4, h5, h6⟩ := h2 i refine ⟨h4, h5, fun H => H.fst.oadd _ (NF.below_of_lt' ?_ (@NF.oadd_zero _ _ (h6 H.fst)))⟩ rwa [repr, ← zero_def, repr, add_zero, PNat.one_coe, Nat.cast_one, mul_one, opow_lt_opow_iff_right one_lt_omega0] · refine ⟨by rw [repr, ihb.1, add_succ, repr], fun H => H.fst.oadd _ (NF.below_of_lt' ?_ (ihb.2 H.snd))⟩ have := H.snd'.repr_lt rw [ihb.1] at this exact (lt_succ _).trans this · rcases ihb with ⟨h1, h2, h3⟩ simp only [repr] exact ⟨isSuccLimit_add _ h1, fun i => ⟨oadd_lt_oadd_3 (h2 i).1, oadd_lt_oadd_3 (h2 i).2.1, fun H => H.fst.oadd _ (NF.below_of_lt' (lt_trans (h2 i).2.1 H.snd'.repr_lt) ((h2 i).2.2 H.snd))⟩, exists_lt_add h3⟩ /-- The fast growing hierarchy for ordinal notations `< ε₀`. This is a sequence of functions `ℕ → ℕ` indexed by ordinals, with the definition: * `f_0(n) = n + 1` * `f_(α + 1)(n) = f_α^[n](n)` * `f_α(n) = f_(α[n])(n)` where `α` is a limit ordinal and `α[i]` is the fundamental sequence converging to `α` -/ def fastGrowing : ONote → ℕ → ℕ | o => match fundamentalSequence o, fundamentalSequence_has_prop o with | Sum.inl none, _ => Nat.succ | Sum.inl (some a), h => have : a < o := by rw [lt_def, h.1]; apply lt_succ fun i => (fastGrowing a)^[i] i | Sum.inr f, h => fun i => have : f i < o := (h.2.1 i).2.1 fastGrowing (f i) i termination_by o => o theorem fastGrowing_def {o : ONote} {x} (e : fundamentalSequence o = x) : fastGrowing o = match (motive := (x : Option ONote ⊕ (ℕ → ONote)) → FundamentalSequenceProp o x → ℕ → ℕ) x, e ▸ fundamentalSequence_has_prop o with | Sum.inl none, _ => Nat.succ | Sum.inl (some a), _ => fun i => (fastGrowing a)^[i] i | Sum.inr f, _ => fun i => fastGrowing (f i) i := by subst x rw [fastGrowing] theorem fastGrowing_zero' (o : ONote) (h : fundamentalSequence o = Sum.inl none) : fastGrowing o = Nat.succ := by rw [fastGrowing_def h] theorem fastGrowing_succ (o) {a} (h : fundamentalSequence o = Sum.inl (some a)) : fastGrowing o = fun i => (fastGrowing a)^[i] i := by rw [fastGrowing_def h] theorem fastGrowing_limit (o) {f} (h : fundamentalSequence o = Sum.inr f) : fastGrowing o = fun i => fastGrowing (f i) i := by rw [fastGrowing_def h] @[simp] theorem fastGrowing_zero : fastGrowing 0 = Nat.succ := fastGrowing_zero' _ rfl @[simp] theorem fastGrowing_one : fastGrowing 1 = fun n => 2 * n := by rw [@fastGrowing_succ 1 0 rfl]; funext i; rw [two_mul, fastGrowing_zero] suffices ∀ a b, Nat.succ^[a] b = b + a from this _ _ intro a b; induction a <;> simp [*, Function.iterate_succ', Nat.add_assoc, -Function.iterate_succ] @[simp] theorem fastGrowing_two : fastGrowing 2 = fun n => (2 ^ n) * n := by rw [@fastGrowing_succ 2 1 rfl] simp /-- We can extend the fast growing hierarchy one more step to `ε₀` itself, using `ω ^ (ω ^ (⋯ ^ ω))` as the fundamental sequence converging to `ε₀` (which is not an `ONote`). Extending the fast growing hierarchy beyond this requires a definition of fundamental sequence for larger ordinals. -/ def fastGrowingε₀ (i : ℕ) : ℕ := fastGrowing ((fun a => a.oadd 1 0)^[i] 0) i theorem fastGrowingε₀_zero : fastGrowingε₀ 0 = 1 := by simp [fastGrowingε₀] theorem fastGrowingε₀_one : fastGrowingε₀ 1 = 2 := by simp [fastGrowingε₀, show oadd 0 1 0 = 1 from rfl] theorem fastGrowingε₀_two : fastGrowingε₀ 2 = 2048 := by norm_num [fastGrowingε₀, show oadd 0 1 0 = 1 from rfl, @fastGrowing_limit (oadd 1 1 0) _ rfl, show oadd 0 (2 : Nat).succPNat 0 = 3 from rfl, @fastGrowing_succ 3 2 rfl] end ONote /-- The type of normal ordinal notations. It would have been nicer to define this right in the inductive type, but `NF o` requires `repr` which requires `ONote`, so all these things would have to be defined at once, which messes up the VM representation. -/ def NONote := { o : ONote // o.NF } instance : DecidableEq NONote := by unfold NONote; infer_instance namespace NONote open ONote instance NF (o : NONote) : NF o.1 := o.2 /-- Construct a `NONote` from an ordinal notation (and infer normality) -/ def mk (o : ONote) [h : ONote.NF o] : NONote := ⟨o, h⟩ /-- The ordinal represented by an ordinal notation. This function is noncomputable because ordinal arithmetic is noncomputable. In computational applications `NONote` can be used exclusively without reference to `Ordinal`, but this function allows for correctness results to be stated. -/ noncomputable def repr (o : NONote) : Ordinal := o.1.repr instance : ToString NONote := ⟨fun x => x.1.toString⟩ instance : Repr NONote := ⟨fun x prec => x.1.repr' prec⟩ instance : Preorder NONote where le x y := repr x ≤ repr y lt x y := repr x < repr y le_refl _ := @le_refl Ordinal _ _ le_trans _ _ _ := @le_trans Ordinal _ _ _ _ lt_iff_le_not_ge _ _ := @lt_iff_le_not_ge Ordinal _ _ _ instance : Zero NONote := ⟨⟨0, NF.zero⟩⟩ instance : Inhabited NONote := ⟨0⟩ theorem lt_wf : @WellFounded NONote (· < ·) := InvImage.wf repr Ordinal.lt_wf instance : WellFoundedLT NONote := ⟨lt_wf⟩ instance : WellFoundedRelation NONote := ⟨(· < ·), lt_wf⟩ /-- Convert a natural number to an ordinal notation -/ def ofNat (n : ℕ) : NONote := ⟨ONote.ofNat n, ⟨⟨_, nfBelow_ofNat _⟩⟩⟩ /-- Compare ordinal notations -/ def cmp (a b : NONote) : Ordering := ONote.cmp a.1 b.1 theorem cmp_compares : ∀ a b : NONote, (cmp a b).Compares a b | ⟨a, ha⟩, ⟨b, hb⟩ => by dsimp [cmp] have := ONote.cmp_compares a b cases h : ONote.cmp a b <;> simp only [h] at this <;> try exact this exact Subtype.mk_eq_mk.2 this instance : LinearOrder NONote := linearOrderOfCompares cmp cmp_compares /-- Asserts that `repr a < ω ^ repr b`. Used in `NONote.recOn`. -/ def below (a b : NONote) : Prop := NFBelow a.1 (repr b) /-- The `oadd` pseudo-constructor for `NONote` -/ def oadd (e : NONote) (n : ℕ+) (a : NONote) (h : below a e) : NONote := ⟨_, NF.oadd e.2 n h⟩ /-- This is a recursor-like theorem for `NONote` suggesting an inductive definition, which can't actually be defined this way due to conflicting dependencies. -/ @[elab_as_elim] def recOn {C : NONote → Sort*} (o : NONote) (H0 : C 0) (H1 : ∀ e n a h, C e → C a → C (oadd e n a h)) : C o := by obtain ⟨o, h⟩ := o; induction o with | zero => exact H0 | oadd e n a IHe IHa => exact H1 ⟨e, h.fst⟩ n ⟨a, h.snd⟩ h.snd' (IHe _) (IHa _) /-- Addition of ordinal notations -/ instance : Add NONote := ⟨fun x y => mk (x.1 + y.1)⟩ theorem repr_add (a b) : repr (a + b) = repr a + repr b := ONote.repr_add a.1 b.1 /-- Subtraction of ordinal notations -/ instance : Sub NONote := ⟨fun x y => mk (x.1 - y.1)⟩ theorem repr_sub (a b) : repr (a - b) = repr a - repr b := ONote.repr_sub a.1 b.1 /-- Multiplication of ordinal notations -/ instance : Mul NONote := ⟨fun x y => mk (x.1 * y.1)⟩ theorem repr_mul (a b) : repr (a * b) = repr a * repr b := ONote.repr_mul a.1 b.1 /-- Exponentiation of ordinal notations -/ def opow (x y : NONote) := mk (x.1 ^ y.1) theorem repr_opow (a b) : repr (opow a b) = repr a ^ repr b := ONote.repr_opow a.1 b.1 end NONote
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/CantorNormalForm.lean
import Mathlib.Data.Finsupp.AList import Mathlib.SetTheory.Ordinal.Exponential import Mathlib.SetTheory.Ordinal.Family /-! # Cantor Normal Form The Cantor normal form of an ordinal is generally defined as its base `ω` expansion, with its non-zero exponents in decreasing order. Here, we more generally define a base `b` expansion `Ordinal.CNF` in this manner, which is well-behaved for any `b ≥ 2`. ## Implementation notes We implement `Ordinal.CNF` as an association list, where keys are exponents and values are coefficients. This is because this structure intrinsically reflects two key properties of the Cantor normal form: - It is ordered. - It has finitely many entries. ## Todo - Prove the basic results relating the CNF to the arithmetic operations on ordinals. -/ noncomputable section universe u open List namespace Ordinal.CNF /-! ### Cantor normal form as a list -/ /-- Inducts on the base `b` expansion of an ordinal. -/ @[elab_as_elim] protected def rec (b : Ordinal) {C : Ordinal → Sort*} (H0 : C 0) (H : ∀ o, o ≠ 0 → C (o % b ^ log b o) → C o) (o : Ordinal) : C o := if h : o = 0 then h ▸ H0 else H o h (CNF.rec b H0 H (o % b ^ log b o)) termination_by o decreasing_by exact mod_opow_log_lt_self b h @[deprecated (since := "2025-08-18")] noncomputable alias _root_.Ordinal.CNFRec := CNF.rec @[simp] theorem rec_zero {C : Ordinal → Sort*} (b : Ordinal) (H0 : C 0) (H : ∀ o, o ≠ 0 → C (o % b ^ log b o) → C o) : CNF.rec b H0 H 0 = H0 := by rw [CNF.rec, dif_pos rfl] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNFRec_zero := rec_zero theorem rec_pos (b : Ordinal) {o : Ordinal} {C : Ordinal → Sort*} (ho : o ≠ 0) (H0 : C 0) (H : ∀ o, o ≠ 0 → C (o % b ^ log b o) → C o) : CNF.rec b H0 H o = H o ho (@CNF.rec b C H0 H _) := by rw [CNF.rec, dif_neg] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNFRec_pos := rec_pos /-- The Cantor normal form of an ordinal `o` is the list of coefficients and exponents in the base-`b` expansion of `o`. We special-case `CNF 0 o = CNF 1 o = [(0, o)]` for `o ≠ 0`. `CNF b (b ^ u₁ * v₁ + b ^ u₂ * v₂) = [(u₁, v₁), (u₂, v₂)]` -/ @[pp_nodot] def _root_.Ordinal.CNF (b o : Ordinal) : List (Ordinal × Ordinal) := CNF.rec b [] (fun o _ IH ↦ (log b o, o / b ^ log b o)::IH) o @[simp] theorem zero_right (b : Ordinal) : CNF b 0 = [] := rec_zero b _ _ @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_zero := zero_right /-- Recursive definition for the Cantor normal form. -/ protected theorem ne_zero {b o : Ordinal} (ho : o ≠ 0) : CNF b o = (log b o, o / b ^ log b o)::CNF b (o % b ^ log b o) := rec_pos b ho _ _ @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_ne_zero := CNF.ne_zero protected theorem zero_left {o : Ordinal} (ho : o ≠ 0) : CNF 0 o = [(0, o)] := by simp [CNF.ne_zero ho] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.zero_CNF := CNF.zero_left protected theorem one_left {o : Ordinal} (ho : o ≠ 0) : CNF 1 o = [(0, o)] := by simp [CNF.ne_zero ho] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.one_CNF := CNF.one_left protected theorem of_le_one {b o : Ordinal} (hb : b ≤ 1) (ho : o ≠ 0) : CNF b o = [(0, o)] := by rcases le_one_iff.1 hb with (rfl | rfl) exacts [CNF.zero_left ho, CNF.one_left ho] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_of_le_one := CNF.of_le_one protected theorem of_lt {b o : Ordinal} (ho : o ≠ 0) (hb : o < b) : CNF b o = [(0, o)] := by rw [CNF.ne_zero ho, log_eq_zero hb, opow_zero, div_one, mod_one, zero_right] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_of_lt := CNF.of_lt /-- Evaluating the Cantor normal form of an ordinal returns the ordinal. -/ protected theorem foldr (b o : Ordinal) : (CNF b o).foldr (fun p r ↦ b ^ p.1 * p.2 + r) 0 = o := by refine CNF.rec b ?_ ?_ o · rw [zero_right, foldr_nil] · intro o ho IH rw [CNF.ne_zero ho, foldr_cons, IH, div_add_mod] @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_foldr := CNF.foldr /-- Every exponent in the Cantor normal form `CNF b o` is less or equal to `log b o`. -/ theorem fst_le_log {b o : Ordinal.{u}} {x : Ordinal × Ordinal} : x ∈ CNF b o → x.1 ≤ log b o := by refine CNF.rec b ?_ (fun o ho H ↦ ?_) o · simp · rw [CNF.ne_zero ho, mem_cons] rintro (rfl | h) · rfl · exact (H h).trans (log_mono_right _ (mod_opow_log_lt_self b ho).le) @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_fst_le_log := fst_le_log /-- Every coefficient in a Cantor normal form is positive. -/ theorem lt_snd {b o : Ordinal.{u}} {x : Ordinal × Ordinal} : x ∈ CNF b o → 0 < x.2 := by refine CNF.rec b (by simp) (fun o ho IH ↦ ?_) o rw [CNF.ne_zero ho] rintro (h | ⟨_, h⟩) · exact div_opow_log_pos b ho · exact IH h @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_lt_snd := lt_snd /-- Every coefficient in the Cantor normal form `CNF b o` is less than `b`. -/ theorem snd_lt {b o : Ordinal.{u}} (hb : 1 < b) {x : Ordinal × Ordinal} : x ∈ CNF b o → x.2 < b := by refine CNF.rec b ?_ (fun o ho IH ↦ ?_) o · simp · rw [CNF.ne_zero ho] intro h obtain rfl | h := mem_cons.mp h · exact div_opow_log_lt o hb · exact IH h @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_snd_lt := snd_lt /-- The exponents of the Cantor normal form are decreasing. -/ protected theorem sorted (b o : Ordinal) : ((CNF b o).map Prod.fst).Sorted (· > ·) := by refine CNF.rec b ?_ (fun o ho IH ↦ ?_) o · rw [zero_right] exact sorted_nil · rcases le_or_gt b 1 with hb | hb · rw [CNF.of_le_one hb ho] exact sorted_singleton _ · obtain hob | hbo := lt_or_ge o b · rw [CNF.of_lt ho hob] exact sorted_singleton _ · rw [CNF.ne_zero ho, map_cons, sorted_cons] refine ⟨fun a H ↦ ?_, IH⟩ rw [mem_map] at H rcases H with ⟨⟨a, a'⟩, H, rfl⟩ exact (fst_le_log H).trans_lt (log_mod_opow_log_lt_log_self hb hbo) @[deprecated (since := "2025-08-18")] alias _root_.Ordinal.CNF_sorted := CNF.sorted private theorem nodupKeys (b o : Ordinal) : (map Prod.toSigma (CNF b o)).NodupKeys := by rw [NodupKeys, List.keys, map_map, Prod.fst_comp_toSigma] exact (CNF.sorted ..).nodup /-! ### Cantor normal form as a finsupp -/ open AList Finsupp /-- `CNF.coeff b o` is the finitely supported function returning the coefficient of `b ^ e` in the Cantor Normal Form (`CNF`) of `o`, for each `e`. -/ @[pp_nodot] def coeff (b o : Ordinal) : Ordinal →₀ Ordinal := lookupFinsupp ⟨_, nodupKeys b o⟩ theorem coeff_of_mem_CNF {b o e c : Ordinal} (h : ⟨e, c⟩ ∈ CNF b o) : coeff b o e = c := by rw [coeff, lookupFinsupp_apply, mem_lookup_iff.2, Option.getD_some] simpa theorem coeff_of_not_mem_CNF {b o e : Ordinal} (h : e ∉ (CNF b o).map Prod.fst) : coeff b o e = 0 := by rw [coeff, lookupFinsupp_apply, lookup_eq_none.2, Option.getD_none] simp_all [List.keys] theorem coeff_zero_apply (b e : Ordinal) : coeff b 0 e = 0 := by apply coeff_of_not_mem_CNF simp @[simp] theorem coeff_zero_right (b : Ordinal) : coeff b 0 = 0 := by ext e exact coeff_zero_apply b e theorem coeff_of_le_one {b : Ordinal} (hb : b ≤ 1) (o : Ordinal) : coeff b o = single 0 o := by ext a obtain rfl | ho := eq_or_ne o 0 · simp · obtain rfl | ha := eq_or_ne a 0 · apply coeff_of_mem_CNF rw [CNF.of_le_one hb ho] simp · rw [single_eq_of_ne ha] apply coeff_of_not_mem_CNF rw [CNF.of_le_one hb ho] simpa using ha @[simp] theorem coeff_zero_left (o : Ordinal) : coeff 0 o = single 0 o := coeff_of_le_one zero_le_one o @[simp] theorem coeff_one_left (o : Ordinal) : coeff 1 o = single 0 o := coeff_of_le_one le_rfl o end Ordinal.CNF
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Principal.lean
import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Principal ordinals We define principal or indecomposable ordinals, and we prove the standard properties about them. ## Main definitions and results * `Principal`: A principal or indecomposable ordinal under some binary operation. We include 0 and any other typically excluded edge cases for simplicity. * `not_bddAbove_principal`: Principal ordinals (under any operation) are unbounded. * `principal_add_iff_zero_or_omega0_opow`: The main characterization theorem for additive principal ordinals. * `principal_mul_iff_le_two_or_omega0_opow_opow`: The main characterization theorem for multiplicative principal ordinals. ## TODO * Prove that exponential principal ordinals are 0, 1, 2, ω, or epsilon numbers, i.e. fixed points of `fun x ↦ ω ^ x`. -/ universe u open Order namespace Ordinal variable {a b c o : Ordinal.{u}} section Arbitrary variable {op : Ordinal → Ordinal → Ordinal} /-! ### Principal ordinals -/ /-- An ordinal `o` is said to be principal or indecomposable under an operation when the set of ordinals less than it is closed under that operation. In standard mathematical usage, this term is almost exclusively used for additive and multiplicative principal ordinals. For simplicity, we break usual convention and regard `0` as principal. -/ def Principal (op : Ordinal → Ordinal → Ordinal) (o : Ordinal) : Prop := ∀ ⦃a b⦄, a < o → b < o → op a b < o theorem principal_swap_iff : Principal (Function.swap op) o ↔ Principal op o := by constructor <;> exact fun h a b ha hb => h hb ha theorem not_principal_iff : ¬ Principal op o ↔ ∃ a < o, ∃ b < o, o ≤ op a b := by simp [Principal] theorem principal_iff_of_monotone (h₁ : ∀ a, Monotone (op a)) (h₂ : ∀ a, Monotone (Function.swap op a)) : Principal op o ↔ ∀ a < o, op a a < o := by use fun h a ha => h ha ha intro H a b ha hb obtain hab | hba := le_or_gt a b · exact (h₂ b hab).trans_lt <| H b hb · exact (h₁ a hba.le).trans_lt <| H a ha theorem not_principal_iff_of_monotone (h₁ : ∀ a, Monotone (op a)) (h₂ : ∀ a, Monotone (Function.swap op a)) : ¬ Principal op o ↔ ∃ a < o, o ≤ op a a := by simp [principal_iff_of_monotone h₁ h₂] @[simp] theorem principal_zero : Principal op 0 := fun a _ h => (Ordinal.not_lt_zero a h).elim @[simp] theorem principal_one_iff : Principal op 1 ↔ op 0 0 = 0 := by refine ⟨fun h => ?_, fun h a b ha hb => ?_⟩ · rw [← lt_one_iff_zero] exact h zero_lt_one zero_lt_one · rwa [lt_one_iff_zero, ha, hb] at * theorem Principal.iterate_lt (hao : a < o) (ho : Principal op o) (n : ℕ) : (op a)^[n] a < o := by induction n with | zero => rwa [Function.iterate_zero] | succ n hn => rw [Function.iterate_succ'] exact ho hao hn theorem op_eq_self_of_principal (hao : a < o) (H : IsNormal (op a)) (ho : Principal op o) (ho' : IsSuccLimit o) : op a o = o := by apply H.le_apply.antisymm' rw [H.apply_of_isSuccLimit ho', Ordinal.iSup_le_iff] exact fun ⟨b, hbo⟩ ↦ (ho hao hbo).le theorem nfp_le_of_principal (hao : a < o) (ho : Principal op o) : nfp (op a) a ≤ o := nfp_le fun n => (ho.iterate_lt hao n).le protected theorem Principal.sSup {s : Set Ordinal} (H : ∀ x ∈ s, Principal op x) : Principal op (sSup s) := by have : Principal op (sSup ∅) := by simp by_cases hs : BddAbove s · obtain rfl | hs' := s.eq_empty_or_nonempty · assumption simp only [Principal, lt_csSup_iff hs hs', forall_exists_index, and_imp] intro x y a has ha b hbs hb have h : max a b ∈ s := max_rec' _ has hbs exact ⟨_, h, H (max a b) h (lt_max_of_lt_left ha) (lt_max_of_lt_right hb)⟩ · rwa [csSup_of_not_bddAbove hs] protected theorem Principal.iSup {ι} {f : ι → Ordinal} (H : ∀ i, Principal op (f i)) : Principal op (⨆ i, f i) := Principal.sSup (by simpa) end Arbitrary /-! ### Principal ordinals are unbounded -/ /-- We give an explicit construction for a principal ordinal larger or equal than `o`. -/ private theorem principal_nfp_iSup (op : Ordinal → Ordinal → Ordinal) (o : Ordinal) : Principal op (nfp (fun x ↦ ⨆ y : Set.Iio x ×ˢ Set.Iio x, succ (op y.1.1 y.1.2)) o) := by intro a b ha hb rw [lt_nfp_iff] at * obtain ⟨m, ha⟩ := ha obtain ⟨n, hb⟩ := hb obtain h | h := le_total ((fun x ↦ ⨆ y : Set.Iio x ×ˢ Set.Iio x, succ (op y.1.1 y.1.2))^[m] o) ((fun x ↦ ⨆ y : Set.Iio x ×ˢ Set.Iio x, succ (op y.1.1 y.1.2))^[n] o) · use n + 1 rw [Function.iterate_succ'] apply (lt_succ _).trans_le exact Ordinal.le_iSup (fun y : Set.Iio _ ×ˢ Set.Iio _ ↦ succ (op y.1.1 y.1.2)) ⟨_, Set.mk_mem_prod (ha.trans_le h) hb⟩ · use m + 1 rw [Function.iterate_succ'] apply (lt_succ _).trans_le exact Ordinal.le_iSup (fun y : Set.Iio _ ×ˢ Set.Iio _ ↦ succ (op y.1.1 y.1.2)) ⟨_, Set.mk_mem_prod ha (hb.trans_le h)⟩ /-- Principal ordinals under any operation are unbounded. -/ theorem not_bddAbove_principal (op : Ordinal → Ordinal → Ordinal) : ¬ BddAbove { o | Principal op o } := by rintro ⟨a, ha⟩ exact ((le_nfp _ _).trans (ha (principal_nfp_iSup op (succ a)))).not_gt (lt_succ a) /-! #### Additive principal ordinals -/ theorem principal_add_one : Principal (· + ·) 1 := principal_one_iff.2 <| zero_add 0 theorem principal_add_of_le_one (ho : o ≤ 1) : Principal (· + ·) o := by rcases le_one_iff.1 ho with (rfl | rfl) · exact principal_zero · exact principal_add_one theorem isSuccLimit_of_principal_add (ho₁ : 1 < o) (ho : Principal (· + ·) o) : IsSuccLimit o := by rw [isSuccLimit_iff, isSuccPrelimit_iff_succ_lt] exact ⟨ho₁.ne_bot, fun _ ha ↦ ho ha ho₁⟩ @[deprecated (since := "2025-07-08")] alias isLimit_of_principal_add := isSuccLimit_of_principal_add theorem principal_add_iff_add_left_eq_self : Principal (· + ·) o ↔ ∀ a < o, a + o = o := by refine ⟨fun ho a hao => ?_, fun h a b hao hbo => ?_⟩ · rcases lt_or_ge 1 o with ho₁ | ho₁ · exact op_eq_self_of_principal hao (isNormal_add_right a) ho (isSuccLimit_of_principal_add ho₁ ho) · rcases le_one_iff.1 ho₁ with (rfl | rfl) · exact (Ordinal.not_lt_zero a hao).elim · rw [lt_one_iff_zero] at hao rw [hao, zero_add] · rw [← h a hao] exact (isNormal_add_right a).strictMono hbo theorem exists_lt_add_of_not_principal_add (ha : ¬ Principal (· + ·) a) : ∃ b < a, ∃ c < a, b + c = a := by rw [not_principal_iff] at ha rcases ha with ⟨b, hb, c, hc, H⟩ refine ⟨b, hb, _, lt_of_le_of_ne (sub_le_self a b) fun hab => ?_, Ordinal.add_sub_cancel_of_le hb.le⟩ rw [← sub_le, hab] at H exact H.not_gt hc theorem principal_add_iff_add_lt_ne_self : Principal (· + ·) a ↔ ∀ b < a, ∀ c < a, b + c ≠ a := ⟨fun ha _ hb _ hc => (ha hb hc).ne, fun H => by by_contra! ha rcases exists_lt_add_of_not_principal_add ha with ⟨b, hb, c, hc, rfl⟩ exact (H b hb c hc).irrefl⟩ theorem principal_add_omega0 : Principal (· + ·) ω := principal_add_iff_add_left_eq_self.2 fun _ => add_omega0 theorem add_omega0_opow (h : a < ω ^ b) : a + ω ^ b = ω ^ b := by refine le_antisymm ?_ (le_add_left _ a) induction b using limitRecOn with | zero => rw [opow_zero, ← succ_zero, lt_succ_iff, Ordinal.le_zero] at h rw [h, zero_add] | succ => rw [opow_succ] at h rcases (lt_mul_iff_of_isSuccLimit isSuccLimit_omega0).1 h with ⟨x, xo, ax⟩ grw [ax, opow_succ, ← mul_add, add_omega0 xo] | limit b l IH => rcases (lt_opow_of_isSuccLimit omega0_ne_zero l).1 h with ⟨x, xb, ax⟩ apply (((isNormal_add_right a).trans <| isNormal_opow one_lt_omega0).limit_le l).2 intro y yb calc a + ω ^ y ≤ a + ω ^ max x y := by gcongr; exacts [omega0_pos, le_max_right ..] _ ≤ ω ^ max x y := IH _ (max_lt xb yb) <| ax.trans_le <| opow_le_opow_right omega0_pos <| le_max_left x y _ ≤ ω ^ b := opow_le_opow_right omega0_pos <| (max_lt xb yb).le theorem principal_add_omega0_opow (o : Ordinal) : Principal (· + ·) (ω ^ o) := principal_add_iff_add_left_eq_self.2 fun _ => add_omega0_opow /-- The main characterization theorem for additive principal ordinals. -/ theorem principal_add_iff_zero_or_omega0_opow : Principal (· + ·) o ↔ o = 0 ∨ o ∈ Set.range (ω ^ · : Ordinal → Ordinal) := by rcases eq_or_ne o 0 with (rfl | ho) · simp only [principal_zero, Or.inl] · rw [principal_add_iff_add_left_eq_self] simp only [ho, false_or] refine ⟨fun H => ⟨_, ((lt_or_eq_of_le (opow_log_le_self _ ho)).resolve_left fun h => ?_)⟩, fun ⟨b, e⟩ => e.symm ▸ fun a => add_omega0_opow⟩ have := H _ h have := lt_opow_succ_log_self one_lt_omega0 o rw [opow_succ, lt_mul_iff_of_isSuccLimit isSuccLimit_omega0] at this rcases this with ⟨a, ao, h'⟩ rcases lt_omega0.1 ao with ⟨n, rfl⟩ clear ao revert h' apply not_lt_of_ge suffices e : ω ^ log ω o * n + o = o by simpa only [e] using le_add_right (ω ^ log ω o * ↑n) o induction n with | zero => simp [Nat.cast_zero, mul_zero, zero_add] | succ n IH => simp only [Nat.cast_succ, mul_add_one, add_assoc, this, IH] theorem principal_add_opow_of_principal_add {a} (ha : Principal (· + ·) a) (b : Ordinal) : Principal (· + ·) (a ^ b) := by rcases principal_add_iff_zero_or_omega0_opow.1 ha with (rfl | ⟨c, rfl⟩) · rcases eq_or_ne b 0 with (rfl | hb) · rw [opow_zero] exact principal_add_one · rwa [zero_opow hb] · rw [← opow_mul] exact principal_add_omega0_opow _ theorem add_absorp (h₁ : a < ω ^ b) (h₂ : ω ^ b ≤ c) : a + c = c := by rw [← Ordinal.add_sub_cancel_of_le h₂, ← add_assoc, add_omega0_opow h₁] theorem principal_add_mul_of_principal_add (a : Ordinal.{u}) {b : Ordinal.{u}} (hb₁ : b ≠ 1) (hb : Principal (· + ·) b) : Principal (· + ·) (a * b) := by rcases eq_zero_or_pos a with (rfl | _) · rw [zero_mul] exact principal_zero · rcases eq_zero_or_pos b with (rfl | hb₁') · rw [mul_zero] exact principal_zero · rw [← succ_le_iff, succ_zero] at hb₁' intro c d hc hd rw [lt_mul_iff_of_isSuccLimit (isSuccLimit_of_principal_add (lt_of_le_of_ne hb₁' hb₁.symm) hb)] at * rcases hc with ⟨x, hx, hx'⟩ rcases hd with ⟨y, hy, hy'⟩ use x + y, hb hx hy rw [mul_add] exact Left.add_lt_add hx' hy' /-! #### Multiplicative principal ordinals -/ theorem principal_mul_one : Principal (· * ·) 1 := by rw [principal_one_iff] exact zero_mul _ theorem principal_mul_two : Principal (· * ·) 2 := by intro a b ha hb rw [← succ_one, lt_succ_iff] at * convert mul_le_mul' ha hb exact (mul_one 1).symm theorem principal_mul_of_le_two (ho : o ≤ 2) : Principal (· * ·) o := by rcases lt_or_eq_of_le ho with (ho | rfl) · rw [← succ_one, lt_succ_iff] at ho rcases lt_or_eq_of_le ho with (ho | rfl) · rw [lt_one_iff_zero.1 ho] exact principal_zero · exact principal_mul_one · exact principal_mul_two theorem principal_add_of_principal_mul (ho : Principal (· * ·) o) (ho₂ : o ≠ 2) : Principal (· + ·) o := by rcases lt_or_gt_of_ne ho₂ with ho₁ | ho₂ · replace ho₁ : o < succ 1 := by rwa [succ_one] rw [lt_succ_iff] at ho₁ exact principal_add_of_le_one ho₁ · refine fun a b hao hbo => lt_of_le_of_lt ?_ (ho (max_lt hao hbo) ho₂) dsimp only rw [← one_add_one_eq_two, mul_add, mul_one] exact add_le_add (le_max_left a b) (le_max_right a b) theorem isSuccLimit_of_principal_mul (ho₂ : 2 < o) (ho : Principal (· * ·) o) : IsSuccLimit o := isSuccLimit_of_principal_add ((lt_succ 1).trans (succ_one ▸ ho₂)) (principal_add_of_principal_mul ho (ne_of_gt ho₂)) @[deprecated (since := "2025-07-08")] alias isLimit_of_principal_mul := isSuccLimit_of_principal_mul theorem principal_mul_iff_mul_left_eq : Principal (· * ·) o ↔ ∀ a, 0 < a → a < o → a * o = o := by refine ⟨fun h a ha₀ hao => ?_, fun h a b hao hbo => ?_⟩ · rcases le_or_gt o 2 with ho | ho · convert one_mul o apply le_antisymm · rw [← lt_succ_iff, succ_one] exact hao.trans_le ho · rwa [← succ_le_iff, succ_zero] at ha₀ · exact op_eq_self_of_principal hao (isNormal_mul_right ha₀) h (isSuccLimit_of_principal_mul ho h) · rcases eq_or_ne a 0 with (rfl | ha) · dsimp only; rwa [zero_mul] rw [← Ordinal.pos_iff_ne_zero] at ha rw [← h a ha hao] exact (isNormal_mul_right ha).strictMono hbo theorem principal_mul_omega0 : Principal (· * ·) ω := fun a b ha hb => match a, b, lt_omega0.1 ha, lt_omega0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by dsimp only; rw [← natCast_mul] apply nat_lt_omega0 theorem mul_omega0 (a0 : 0 < a) (ha : a < ω) : a * ω = ω := principal_mul_iff_mul_left_eq.1 principal_mul_omega0 a a0 ha theorem natCast_mul_omega0 {n : ℕ} (hn : 0 < n) : n * ω = ω := mul_omega0 (mod_cast hn) (nat_lt_omega0 n) theorem mul_lt_omega0_opow (c0 : 0 < c) (ha : a < ω ^ c) (hb : b < ω) : a * b < ω ^ c := by rcases zero_or_succ_or_isSuccLimit c with (rfl | ⟨c, rfl⟩ | l) · exact (lt_irrefl _).elim c0 · rw [opow_succ] at ha obtain ⟨n, hn, an⟩ := ((isNormal_mul_right <| opow_pos _ omega0_pos).limit_lt isSuccLimit_omega0).1 ha grw [an, opow_succ, mul_assoc] gcongr exacts [opow_pos _ omega0_pos, principal_mul_omega0 hn hb] · rcases ((isNormal_opow one_lt_omega0).limit_lt l).1 ha with ⟨x, hx, ax⟩ refine (mul_le_mul' (le_of_lt ax) (le_of_lt hb)).trans_lt ?_ rw [← opow_succ, opow_lt_opow_iff_right one_lt_omega0] exact l.succ_lt hx theorem mul_omega0_opow_opow (a0 : 0 < a) (h : a < ω ^ ω ^ b) : a * ω ^ ω ^ b = ω ^ ω ^ b := by obtain rfl | b0 := eq_or_ne b 0 · rw [opow_zero, opow_one] at h ⊢ exact mul_omega0 a0 h · apply le_antisymm · obtain ⟨x, xb, ax⟩ := (lt_opow_of_isSuccLimit omega0_ne_zero (isSuccLimit_opow_left isSuccLimit_omega0 b0)).1 h grw [ax, ← opow_add, add_omega0_opow xb] · conv_lhs => rw [← one_mul (ω ^ _)] grw [one_le_iff_pos.2 a0] theorem principal_mul_omega0_opow_opow (o : Ordinal) : Principal (· * ·) (ω ^ ω ^ o) := principal_mul_iff_mul_left_eq.2 fun _ => mul_omega0_opow_opow theorem principal_add_of_principal_mul_opow (hb : 1 < b) (ho : Principal (· * ·) (b ^ o)) : Principal (· + ·) o := by intro x y hx hy have := ho ((opow_lt_opow_iff_right hb).2 hx) ((opow_lt_opow_iff_right hb).2 hy) dsimp only at * rwa [← opow_add, opow_lt_opow_iff_right hb] at this /-- The main characterization theorem for multiplicative principal ordinals. -/ theorem principal_mul_iff_le_two_or_omega0_opow_opow : Principal (· * ·) o ↔ o ≤ 2 ∨ o ∈ Set.range (ω ^ ω ^ · : Ordinal → Ordinal) := by refine ⟨fun ho => ?_, ?_⟩ · rcases le_or_gt o 2 with ho₂ | ho₂ · exact Or.inl ho₂ · rcases principal_add_iff_zero_or_omega0_opow.1 (principal_add_of_principal_mul ho ho₂.ne') with (rfl | ⟨a, rfl⟩) · exact (Ordinal.not_lt_zero 2 ho₂).elim · rcases principal_add_iff_zero_or_omega0_opow.1 (principal_add_of_principal_mul_opow one_lt_omega0 ho) with (rfl | ⟨b, rfl⟩) · simp · exact Or.inr ⟨b, rfl⟩ · rintro (ho₂ | ⟨a, rfl⟩) · exact principal_mul_of_le_two ho₂ · exact principal_mul_omega0_opow_opow a theorem mul_omega0_dvd (a0 : 0 < a) (ha : a < ω) : ∀ {b}, ω ∣ b → a * b = b | _, ⟨b, rfl⟩ => by rw [← mul_assoc, mul_omega0 a0 ha] theorem mul_eq_opow_log_succ (ha : a ≠ 0) (hb : Principal (· * ·) b) (hb₂ : 2 < b) : a * b = b ^ succ (log b a) := by apply le_antisymm · have hbl := isSuccLimit_of_principal_mul hb₂ hb rw [(isNormal_mul_right (Ordinal.pos_iff_ne_zero.2 ha)).apply_of_isSuccLimit hbl, Ordinal.iSup_le_iff] intro ⟨c, hcb⟩ have hb₁ : 1 < b := one_lt_two.trans hb₂ have hbo₀ : b ^ log b a ≠ 0 := Ordinal.pos_iff_ne_zero.1 (opow_pos _ (zero_lt_one.trans hb₁)) apply (mul_le_mul_right' (le_of_lt (lt_mul_succ_div a hbo₀)) c).trans rw [mul_assoc, opow_succ] gcongr refine (hb (hbl.succ_lt ?_) hcb).le rw [div_lt hbo₀, ← opow_succ] exact lt_opow_succ_log_self hb₁ _ · grw [opow_succ, opow_log_le_self b ha] /-! #### Exponential principal ordinals -/ theorem principal_opow_omega0 : Principal (· ^ ·) ω := fun a b ha hb => match a, b, lt_omega0.1 ha, lt_omega0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by simp_rw [← natCast_opow] apply nat_lt_omega0 theorem opow_omega0 (a1 : 1 < a) (h : a < ω) : a ^ ω = ω := ((opow_le_of_isSuccLimit (one_le_iff_ne_zero.1 <| le_of_lt a1) isSuccLimit_omega0).2 fun _ hb => (principal_opow_omega0 h hb).le).antisymm (right_le_opow _ a1) theorem natCast_opow_omega0 {n : ℕ} (hn : 1 < n) : n ^ ω = ω := opow_omega0 (mod_cast hn) (nat_lt_omega0 n) end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/Enum.lean
import Mathlib.SetTheory.Ordinal.Family /-! # Enumerating sets of ordinals by ordinals The ordinals have the peculiar property that every subset bounded above is a small type, while themselves not being small. As a consequence of this, every unbounded subset of `Ordinal` is order isomorphic to `Ordinal`. We define this correspondence as `enumOrd`, and use it to then define an order isomorphism `enumOrdOrderIso`. This can be thought of as an ordinal analog of `Nat.nth`. -/ universe u open Order Set namespace Ordinal variable {o a b : Ordinal.{u}} /-- Enumerator function for an unbounded set of ordinals. -/ noncomputable def enumOrd (s : Set Ordinal.{u}) (o : Ordinal.{u}) : Ordinal.{u} := sInf (s ∩ { b | ∀ c, c < o → enumOrd s c < b }) termination_by o variable {s : Set Ordinal.{u}} theorem enumOrd_le_of_forall_lt (ha : a ∈ s) (H : ∀ b < o, enumOrd s b < a) : enumOrd s o ≤ a := by rw [enumOrd] exact csInf_le' ⟨ha, H⟩ /-- The set in the definition of `enumOrd` is nonempty. -/ private theorem enumOrd_nonempty (hs : ¬ BddAbove s) (o : Ordinal) : (s ∩ { b | ∀ c, c < o → enumOrd s c < b }).Nonempty := by rw [not_bddAbove_iff] at hs obtain ⟨a, ha⟩ := bddAbove_of_small (enumOrd s '' Iio o) obtain ⟨b, hb, hba⟩ := hs a exact ⟨b, hb, fun c hc ↦ (ha (mem_image_of_mem _ hc)).trans_lt hba⟩ private theorem enumOrd_mem_aux (hs : ¬ BddAbove s) (o : Ordinal) : enumOrd s o ∈ s ∩ { b | ∀ c, c < o → enumOrd s c < b } := by rw [enumOrd] exact csInf_mem (enumOrd_nonempty hs o) theorem enumOrd_mem (hs : ¬ BddAbove s) (o : Ordinal) : enumOrd s o ∈ s := (enumOrd_mem_aux hs o).1 theorem enumOrd_strictMono (hs : ¬ BddAbove s) : StrictMono (enumOrd s) := fun a b ↦ (enumOrd_mem_aux hs b).2 a theorem enumOrd_injective (hs : ¬ BddAbove s) : Function.Injective (enumOrd s) := (enumOrd_strictMono hs).injective theorem enumOrd_inj (hs : ¬ BddAbove s) {a b : Ordinal} : enumOrd s a = enumOrd s b ↔ a = b := (enumOrd_injective hs).eq_iff theorem enumOrd_le_enumOrd (hs : ¬ BddAbove s) {a b : Ordinal} : enumOrd s a ≤ enumOrd s b ↔ a ≤ b := (enumOrd_strictMono hs).le_iff_le theorem enumOrd_lt_enumOrd (hs : ¬ BddAbove s) {a b : Ordinal} : enumOrd s a < enumOrd s b ↔ a < b := (enumOrd_strictMono hs).lt_iff_lt theorem id_le_enumOrd (hs : ¬ BddAbove s) : id ≤ enumOrd s := (enumOrd_strictMono hs).id_le theorem le_enumOrd_self (hs : ¬ BddAbove s) {a} : a ≤ enumOrd s a := (enumOrd_strictMono hs).le_apply theorem enumOrd_succ_le (hs : ¬ BddAbove s) (ha : a ∈ s) (hb : enumOrd s b < a) : enumOrd s (succ b) ≤ a := by apply enumOrd_le_of_forall_lt ha intro c hc rw [lt_succ_iff] at hc exact ((enumOrd_strictMono hs).monotone hc).trans_lt hb theorem range_enumOrd (hs : ¬ BddAbove s) : range (enumOrd s) = s := by ext a let t := { b | a ≤ enumOrd s b } constructor · rintro ⟨b, rfl⟩ exact enumOrd_mem hs b · intro ha refine ⟨sInf t, (enumOrd_le_of_forall_lt ha ?_).antisymm ?_⟩ · intro b hb by_contra! hb' exact hb.not_ge (csInf_le' hb') · exact csInf_mem (s := t) ⟨a, (enumOrd_strictMono hs).id_le a⟩ theorem enumOrd_surjective (hs : ¬ BddAbove s) {b : Ordinal} (hb : b ∈ s) : ∃ a, enumOrd s a = b := by rwa [← range_enumOrd hs] at hb theorem enumOrd_le_of_subset {t : Set Ordinal} (hs : ¬ BddAbove s) (hst : s ⊆ t) : enumOrd t ≤ enumOrd s := by intro a rw [enumOrd, enumOrd] gcongr with b c exacts [enumOrd_nonempty hs a, enumOrd_le_of_subset hs hst c] termination_by a => a /-- A characterization of `enumOrd`: it is the unique strict monotonic function with range `s`. -/ theorem eq_enumOrd (f : Ordinal → Ordinal) (hs : ¬ BddAbove s) : enumOrd s = f ↔ StrictMono f ∧ range f = s := by constructor · rintro rfl exact ⟨enumOrd_strictMono hs, range_enumOrd hs⟩ · rintro ⟨h₁, h₂⟩ rwa [← (enumOrd_strictMono hs).range_inj h₁, range_enumOrd hs, eq_comm] theorem enumOrd_range {f : Ordinal → Ordinal} (hf : StrictMono f) : enumOrd (range f) = f := (eq_enumOrd _ hf.not_bddAbove_range_of_wellFoundedLT).2 ⟨hf, rfl⟩ /-- If `s` is closed under nonempty suprema, then its enumerator function is normal. See also `enumOrd_isNormal_iff_isClosed`. -/ theorem isNormal_enumOrd (H : ∀ t ⊆ s, t.Nonempty → BddAbove t → sSup t ∈ s) (hs : ¬ BddAbove s) : IsNormal (enumOrd s) := by refine (isNormal_iff_strictMono_limit _).2 ⟨enumOrd_strictMono hs, fun o ho a ha ↦ ?_⟩ trans ⨆ b : Iio o, enumOrd s b · refine enumOrd_le_of_forall_lt ?_ (fun b hb ↦ (enumOrd_strictMono hs (lt_succ b)).trans_le ?_) · have : Nonempty (Iio o) := ⟨0, ho.bot_lt⟩ apply H _ _ (range_nonempty _) (bddAbove_of_small _) rintro _ ⟨c, rfl⟩ exact enumOrd_mem hs c · exact Ordinal.le_iSup _ (⟨_, ho.succ_lt hb⟩ : Iio o) · exact Ordinal.iSup_le fun x ↦ ha _ x.2 @[simp] theorem enumOrd_univ : enumOrd Set.univ = id := by rw [← range_id] exact enumOrd_range strictMono_id @[simp] theorem enumOrd_zero : enumOrd s 0 = sInf s := by rw [enumOrd] simp [Ordinal.not_lt_zero] /-- An order isomorphism between an unbounded set of ordinals and the ordinals. -/ noncomputable def enumOrdOrderIso (s : Set Ordinal) (hs : ¬ BddAbove s) : Ordinal ≃o s := StrictMono.orderIsoOfSurjective (fun o => ⟨_, enumOrd_mem hs o⟩) (enumOrd_strictMono hs) fun s => let ⟨a, ha⟩ := enumOrd_surjective hs s.prop ⟨a, Subtype.eq ha⟩ end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Ordinal/FixedPoint.lean
import Mathlib.Logic.Small.List import Mathlib.SetTheory.Ordinal.Enum import Mathlib.SetTheory.Ordinal.Exponential /-! # Fixed points of normal functions We prove various statements about the fixed points of normal ordinal functions. We state them in three forms: as statements about type-indexed families of normal functions, as statements about ordinal-indexed families of normal functions, and as statements about a single normal function. For the most part, the first case encompasses the others. Moreover, we prove some lemmas about the fixed points of specific normal functions. ## Main definitions and results * `nfpFamily`, `nfp`: the next fixed point of a (family of) normal function(s). * `not_bddAbove_fp_family`, `not_bddAbove_fp`: the (common) fixed points of a (family of) normal function(s) are unbounded in the ordinals. * `deriv_add_eq_mul_omega0_add`: a characterization of the derivative of addition. * `deriv_mul_eq_opow_omega0_mul`: a characterization of the derivative of multiplication. -/ noncomputable section universe u v open Function Order namespace Ordinal /-! ### Fixed points of type-indexed families of ordinals -/ section variable {ι : Type*} {f : ι → Ordinal.{u} → Ordinal.{u}} /-- The next common fixed point, at least `a`, for a family of normal functions. This is defined for any family of functions, as the supremum of all values reachable by applying finitely many functions in the family to `a`. `Ordinal.nfpFamily_fp` shows this is a fixed point, `Ordinal.le_nfpFamily` shows it's at least `a`, and `Ordinal.nfpFamily_le_fp` shows this is the least ordinal with these properties. -/ def nfpFamily (f : ι → Ordinal.{u} → Ordinal.{u}) (a : Ordinal.{u}) : Ordinal := ⨆ i, List.foldr f a i theorem foldr_le_nfpFamily [Small.{u} ι] (f : ι → Ordinal.{u} → Ordinal.{u}) (a l) : List.foldr f a l ≤ nfpFamily f a := Ordinal.le_iSup _ _ theorem le_nfpFamily [Small.{u} ι] (f : ι → Ordinal.{u} → Ordinal.{u}) (a) : a ≤ nfpFamily f a := foldr_le_nfpFamily f a [] theorem lt_nfpFamily_iff [Small.{u} ι] {a b} : a < nfpFamily f b ↔ ∃ l, a < List.foldr f b l := Ordinal.lt_iSup_iff theorem nfpFamily_le_iff [Small.{u} ι] {a b} : nfpFamily f a ≤ b ↔ ∀ l, List.foldr f a l ≤ b := Ordinal.iSup_le_iff theorem nfpFamily_le {a b} : (∀ l, List.foldr f a l ≤ b) → nfpFamily f a ≤ b := Ordinal.iSup_le theorem nfpFamily_monotone [Small.{u} ι] (hf : ∀ i, Monotone (f i)) : Monotone (nfpFamily f) := fun _ _ h ↦ nfpFamily_le <| fun l ↦ (List.foldr_monotone hf l h).trans (foldr_le_nfpFamily _ _ l) theorem apply_lt_nfpFamily [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a b} (hb : b < nfpFamily f a) (i) : f i b < nfpFamily f a := let ⟨l, hl⟩ := lt_nfpFamily_iff.1 hb lt_nfpFamily_iff.2 ⟨i::l, (H i).strictMono hl⟩ theorem apply_lt_nfpFamily_iff [Nonempty ι] [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a b} : (∀ i, f i b < nfpFamily f a) ↔ b < nfpFamily f a := by refine ⟨fun h ↦ ?_, apply_lt_nfpFamily H⟩ let ⟨l, hl⟩ := lt_nfpFamily_iff.1 (h (Classical.arbitrary ι)) exact lt_nfpFamily_iff.2 <| ⟨l, (H _).le_apply.trans_lt hl⟩ theorem nfpFamily_le_apply [Nonempty ι] [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a b} : (∃ i, nfpFamily f a ≤ f i b) ↔ nfpFamily f a ≤ b := by rw [← not_iff_not] push_neg exact apply_lt_nfpFamily_iff H theorem nfpFamily_le_fp (H : ∀ i, Monotone (f i)) {a b} (ab : a ≤ b) (h : ∀ i, f i b ≤ b) : nfpFamily f a ≤ b := by apply Ordinal.iSup_le fun l ↦ ?_ induction l generalizing a with | nil => exact ab | cons i l IH => exact (H i (IH ab)).trans (h i) theorem nfpFamily_fp [Small.{u} ι] {i} (H : IsNormal (f i)) (a) : f i (nfpFamily f a) = nfpFamily f a := by rw [nfpFamily, H.map_iSup] apply le_antisymm <;> refine Ordinal.iSup_le fun l => ?_ · exact Ordinal.le_iSup _ (i::l) · exact H.le_apply.trans (Ordinal.le_iSup _ _) theorem apply_le_nfpFamily [Small.{u} ι] [hι : Nonempty ι] (H : ∀ i, IsNormal (f i)) {a b} : (∀ i, f i b ≤ nfpFamily f a) ↔ b ≤ nfpFamily f a := by refine ⟨fun h => ?_, fun h i => ?_⟩ · obtain ⟨i⟩ := hι exact (H i).le_apply.trans (h i) · rw [← nfpFamily_fp (H i)] exact (H i).monotone h theorem nfpFamily_eq_self [Small.{u} ι] {a} (h : ∀ i, f i a = a) : nfpFamily f a = a := by apply (Ordinal.iSup_le ?_).antisymm (le_nfpFamily f a) intro l rw [List.foldr_fixed' h l] -- Todo: This is actually a special case of the fact the intersection of club sets is a club set. /-- A generalization of the fixed point lemma for normal functions: any family of normal functions has an unbounded set of common fixed points. -/ theorem not_bddAbove_fp_family [Small.{u} ι] (H : ∀ i, IsNormal (f i)) : ¬ BddAbove (⋂ i, Function.fixedPoints (f i)) := by rw [not_bddAbove_iff] refine fun a ↦ ⟨nfpFamily f (succ a), ?_, (lt_succ a).trans_le (le_nfpFamily f _)⟩ rintro _ ⟨i, rfl⟩ exact nfpFamily_fp (H i) _ /-- The derivative of a family of normal functions is the sequence of their common fixed points. This is defined for all functions such that `Ordinal.derivFamily_zero`, `Ordinal.derivFamily_succ`, and `Ordinal.derivFamily_limit` are satisfied. -/ def derivFamily (f : ι → Ordinal.{u} → Ordinal.{u}) (o : Ordinal.{u}) : Ordinal.{u} := limitRecOn o (nfpFamily f 0) (fun _ IH => nfpFamily f (succ IH)) fun a _ g => ⨆ b : Set.Iio a, g _ b.2 @[simp] theorem derivFamily_zero (f : ι → Ordinal → Ordinal) : derivFamily f 0 = nfpFamily f 0 := limitRecOn_zero .. @[simp] theorem derivFamily_succ (f : ι → Ordinal → Ordinal) (o) : derivFamily f (succ o) = nfpFamily f (succ (derivFamily f o)) := limitRecOn_succ .. theorem derivFamily_limit (f : ι → Ordinal → Ordinal) {o} : IsSuccLimit o → derivFamily f o = ⨆ b : Set.Iio o, derivFamily f b := limitRecOn_limit _ _ _ _ theorem isNormal_derivFamily [Small.{u} ι] (f : ι → Ordinal.{u} → Ordinal.{u}) : IsNormal (derivFamily f) := by refine IsNormal.of_succ_lt (fun o ↦ ?_) @fun o h ↦ ?_ · rw [derivFamily_succ, ← succ_le_iff] exact le_nfpFamily _ _ · rw [derivFamily_limit _ h, Set.image_eq_range] have := h.nonempty_Iio.to_subtype exact isLUB_ciSup (bddAbove_of_small _) theorem derivFamily_strictMono [Small.{u} ι] (f : ι → Ordinal.{u} → Ordinal.{u}) : StrictMono (derivFamily f) := (isNormal_derivFamily f).strictMono theorem derivFamily_fp [Small.{u} ι] {i} (H : IsNormal (f i)) (o : Ordinal) : f i (derivFamily f o) = derivFamily f o := by induction o using limitRecOn with | zero => rw [derivFamily_zero] exact nfpFamily_fp H 0 | succ => rw [derivFamily_succ] exact nfpFamily_fp H _ | limit o l IH => have := l.nonempty_Iio.to_subtype rw [derivFamily_limit _ l, H.map_iSup] refine eq_of_forall_ge_iff fun c => ?_ rw [Ordinal.iSup_le_iff, Ordinal.iSup_le_iff] refine forall_congr' fun a ↦ ?_ rw [IH _ a.2] theorem le_iff_derivFamily [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a} : (∀ i, f i a ≤ a) ↔ ∃ o, derivFamily f o = a := ⟨fun ha => by suffices ∀ (o), a ≤ derivFamily f o → ∃ o, derivFamily f o = a from this a (isNormal_derivFamily _).le_apply intro o induction o using limitRecOn with | zero => intro h₁ refine ⟨0, le_antisymm ?_ h₁⟩ rw [derivFamily_zero] exact nfpFamily_le_fp (fun i => (H i).monotone) (Ordinal.zero_le _) ha | succ o IH => intro h₁ rcases le_or_gt a (derivFamily f o) with h | h · exact IH h refine ⟨succ o, le_antisymm ?_ h₁⟩ rw [derivFamily_succ] exact nfpFamily_le_fp (fun i => (H i).monotone) (succ_le_of_lt h) ha | limit o l IH => intro h₁ rcases eq_or_lt_of_le h₁ with h | h · exact ⟨_, h.symm⟩ rw [derivFamily_limit _ l, ← not_le, Ordinal.iSup_le_iff, not_forall] at h obtain ⟨o', h⟩ := h exact IH o' o'.2 (le_of_not_ge h), fun ⟨_, e⟩ i => e ▸ (derivFamily_fp (H i) _).le⟩ theorem fp_iff_derivFamily [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a} : (∀ i, f i a = a) ↔ ∃ o, derivFamily f o = a := Iff.trans ⟨fun h i => le_of_eq (h i), fun h i => (H i).le_iff_eq.1 (h i)⟩ (le_iff_derivFamily H) theorem mem_range_derivFamily [Small.{u} ι] (H : ∀ i, IsNormal (f i)) {a} : a ∈ Set.range (derivFamily f) ↔ ∀ i, f i a = a := (fp_iff_derivFamily H).symm /-- For a family of normal functions, `Ordinal.derivFamily` enumerates the common fixed points. -/ theorem derivFamily_eq_enumOrd [Small.{u} ι] (H : ∀ i, IsNormal (f i)) : derivFamily f = enumOrd (⋂ i, Function.fixedPoints (f i)) := by rw [eq_comm, eq_enumOrd _ (not_bddAbove_fp_family H)] use (isNormal_derivFamily f).strictMono rw [Set.range_eq_iff] refine ⟨?_, fun a ha => ?_⟩ · rintro a S ⟨i, hi⟩ rw [← hi] exact derivFamily_fp (H i) a rw [Set.mem_iInter] at ha rwa [← fp_iff_derivFamily H] end /-! ### Fixed points of a single function -/ section variable {f : Ordinal.{u} → Ordinal.{u}} /-- The next fixed point function, the least fixed point of the normal function `f`, at least `a`. This is defined as `nfpFamily` applied to a family consisting only of `f`. -/ def nfp (f : Ordinal → Ordinal) : Ordinal → Ordinal := nfpFamily fun _ : Unit => f theorem nfp_eq_nfpFamily (f : Ordinal → Ordinal) : nfp f = nfpFamily fun _ : Unit => f := rfl theorem iSup_iterate_eq_nfp (f : Ordinal.{u} → Ordinal.{u}) (a : Ordinal.{u}) : ⨆ n : ℕ, f^[n] a = nfp f a := by apply le_antisymm · rw [Ordinal.iSup_le_iff] intro n rw [← List.length_replicate (n := n) (a := Unit.unit), ← List.foldr_const f a] exact Ordinal.le_iSup _ _ · apply Ordinal.iSup_le intro l rw [List.foldr_const f a l] exact Ordinal.le_iSup _ _ theorem iterate_le_nfp (f a n) : f^[n] a ≤ nfp f a := by rw [← iSup_iterate_eq_nfp] exact Ordinal.le_iSup (fun n ↦ f^[n] a) n theorem le_nfp (f a) : a ≤ nfp f a := iterate_le_nfp f a 0 theorem lt_nfp_iff {a b} : a < nfp f b ↔ ∃ n, a < f^[n] b := by rw [← iSup_iterate_eq_nfp] exact Ordinal.lt_iSup_iff theorem nfp_le_iff {a b} : nfp f a ≤ b ↔ ∀ n, f^[n] a ≤ b := by rw [← iSup_iterate_eq_nfp] exact Ordinal.iSup_le_iff theorem nfp_le {a b} : (∀ n, f^[n] a ≤ b) → nfp f a ≤ b := nfp_le_iff.2 @[simp] theorem nfp_id : nfp id = id := by ext simp_rw [← iSup_iterate_eq_nfp, iterate_id] exact ciSup_const theorem nfp_monotone (hf : Monotone f) : Monotone (nfp f) := nfpFamily_monotone fun _ => hf theorem iterate_lt_nfp (hf : StrictMono f) {a} (h : a < f a) (n : ℕ) : f^[n] a < nfp f a := by apply (hf.iterate n h).trans_le rw [← iterate_succ_apply] exact iterate_le_nfp .. theorem IsNormal.apply_lt_nfp (H : IsNormal f) {a b} : f b < nfp f a ↔ b < nfp f a := by unfold nfp rw [← @apply_lt_nfpFamily_iff Unit (fun _ => f) _ _ (fun _ => H) a b] exact ⟨fun h _ => h, fun h => h Unit.unit⟩ theorem IsNormal.nfp_le_apply (H : IsNormal f) {a b} : nfp f a ≤ f b ↔ nfp f a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.apply_lt_nfp theorem nfp_le_fp (H : Monotone f) {a b} (ab : a ≤ b) (h : f b ≤ b) : nfp f a ≤ b := nfpFamily_le_fp (fun _ => H) ab fun _ => h theorem IsNormal.nfp_fp (H : IsNormal f) : ∀ a, f (nfp f a) = nfp f a := @nfpFamily_fp Unit (fun _ => f) _ () H theorem IsNormal.apply_le_nfp (H : IsNormal f) {a b} : f b ≤ nfp f a ↔ b ≤ nfp f a := ⟨H.le_apply.trans, fun h => by simpa only [H.nfp_fp] using H.le_iff.2 h⟩ theorem nfp_eq_self {a} (h : f a = a) : nfp f a = a := nfpFamily_eq_self fun _ => h /-- The fixed point lemma for normal functions: any normal function has an unbounded set of fixed points. -/ theorem not_bddAbove_fp (H : IsNormal f) : ¬ BddAbove (Function.fixedPoints f) := by convert not_bddAbove_fp_family fun _ : Unit => H exact (Set.iInter_const _).symm /-- The derivative of a normal function `f` is the sequence of fixed points of `f`. This is defined as `Ordinal.derivFamily` applied to a trivial family consisting only of `f`. -/ def deriv (f : Ordinal → Ordinal) : Ordinal → Ordinal := derivFamily fun _ : Unit => f theorem deriv_eq_derivFamily (f : Ordinal → Ordinal) : deriv f = derivFamily fun _ : Unit => f := rfl @[simp] theorem deriv_zero_right (f) : deriv f 0 = nfp f 0 := derivFamily_zero _ @[simp] theorem deriv_succ (f o) : deriv f (succ o) = nfp f (succ (deriv f o)) := derivFamily_succ _ _ theorem deriv_limit (f) {o} : IsSuccLimit o → deriv f o = ⨆ a : {a // a < o}, deriv f a := derivFamily_limit _ theorem isNormal_deriv (f) : IsNormal (deriv f) := isNormal_derivFamily _ theorem deriv_strictMono (f) : StrictMono (deriv f) := derivFamily_strictMono _ theorem deriv_eq_id_of_nfp_eq_id (h : nfp f = id) : deriv f = id := ((isNormal_deriv _).eq_iff_zero_and_succ IsNormal.refl).2 (by simp [h]) @[deprecated (since := "2025-10-25")] alias deriv_id_of_nfp_id := deriv_eq_id_of_nfp_eq_id theorem IsNormal.deriv_fp (H : IsNormal f) : ∀ o, f (deriv f o) = deriv f o := derivFamily_fp (i := ⟨⟩) H theorem IsNormal.le_iff_deriv (H : IsNormal f) {a} : f a ≤ a ↔ ∃ o, deriv f o = a := by unfold deriv rw [← le_iff_derivFamily fun _ : Unit => H] exact ⟨fun h _ => h, fun h => h Unit.unit⟩ theorem IsNormal.fp_iff_deriv (H : IsNormal f) {a} : f a = a ↔ ∃ o, deriv f o = a := by rw [← H.le_iff_eq, H.le_iff_deriv] theorem IsNormal.mem_range_deriv (H : IsNormal f) {a} : a ∈ Set.range (deriv f) ↔ f a = a := H.fp_iff_deriv.symm /-- `Ordinal.deriv` enumerates the fixed points of a normal function. -/ theorem deriv_eq_enumOrd (H : IsNormal f) : deriv f = enumOrd (Function.fixedPoints f) := by convert derivFamily_eq_enumOrd fun _ : Unit => H exact (Set.iInter_const _).symm theorem nfp_zero_left (a) : nfp 0 a = a := by rw [← iSup_iterate_eq_nfp] apply (Ordinal.iSup_le ?_).antisymm (Ordinal.le_iSup _ 0) intro n cases n · rfl · rw [Function.iterate_succ'] simp @[simp] theorem nfp_zero : nfp 0 = id := by ext exact nfp_zero_left _ @[simp] theorem deriv_zero : deriv 0 = id := deriv_eq_id_of_nfp_eq_id nfp_zero theorem deriv_zero_left (a) : deriv 0 a = a := by rw [deriv_zero, id_eq] end /-! ### Fixed points of addition -/ @[simp] theorem nfp_add_zero (a) : nfp (a + ·) 0 = a * ω := by simp_rw [← iSup_iterate_eq_nfp, ← iSup_mul_nat] congr; funext n induction n with | zero => rw [Nat.cast_zero, mul_zero, iterate_zero_apply] | succ n hn => rw [iterate_succ_apply', Nat.add_comm, Nat.cast_add, Nat.cast_one, mul_one_add, hn] theorem nfp_add_eq_mul_omega0 {a b} (hba : b ≤ a * ω) : nfp (a + ·) b = a * ω := by apply le_antisymm (nfp_le_fp (isNormal_add_right a).monotone hba _) · rw [← nfp_add_zero] exact nfp_monotone (isNormal_add_right a).monotone (Ordinal.zero_le b) · dsimp; rw [← mul_one_add, one_add_omega0] theorem add_eq_right_iff_mul_omega0_le {a b : Ordinal} : a + b = b ↔ a * ω ≤ b := by refine ⟨fun h => ?_, fun h => ?_⟩ · rw [← nfp_add_zero a, ← deriv_zero_right] obtain ⟨c, hc⟩ := (isNormal_add_right a).fp_iff_deriv.1 h rw [← hc] exact (isNormal_deriv _).monotone (Ordinal.zero_le _) · have := Ordinal.add_sub_cancel_of_le h nth_rw 1 [← this] rwa [← add_assoc, ← mul_one_add, one_add_omega0] theorem add_le_right_iff_mul_omega0_le {a b : Ordinal} : a + b ≤ b ↔ a * ω ≤ b := by rw [← add_eq_right_iff_mul_omega0_le] exact (isNormal_add_right a).le_iff_eq theorem deriv_add_eq_mul_omega0_add (a b : Ordinal.{u}) : deriv (a + ·) b = a * ω + b := by revert b rw [← funext_iff, IsNormal.eq_iff_zero_and_succ (isNormal_deriv _) (isNormal_add_right _)] refine ⟨?_, fun a h => ?_⟩ · rw [deriv_zero_right, add_zero] exact nfp_add_zero a · rw [deriv_succ, h, add_succ] exact nfp_eq_self (add_eq_right_iff_mul_omega0_le.2 ((le_add_right _ _).trans (le_succ _))) /-! ### Fixed points of multiplication -/ @[simp] theorem nfp_mul_one {a : Ordinal} (ha : 0 < a) : nfp (a * ·) 1 = a ^ ω := by rw [← iSup_iterate_eq_nfp, ← iSup_pow ha] congr funext n induction n with | zero => rw [pow_zero, iterate_zero_apply] | succ n hn => rw [iterate_succ_apply', Nat.add_comm, pow_add, pow_one, hn] @[simp] theorem nfp_mul_zero (a : Ordinal) : nfp (a * ·) 0 = 0 := by rw [← Ordinal.le_zero, nfp_le_iff] intro n induction n with | zero => rfl | succ n hn => dsimp only; rwa [iterate_succ_apply, mul_zero] theorem nfp_mul_eq_opow_omega0 {a b : Ordinal} (hb : 0 < b) (hba : b ≤ a ^ ω) : nfp (a * ·) b = a ^ ω := by rcases eq_zero_or_pos a with ha | ha · rw [ha, zero_opow omega0_ne_zero] at hba ⊢ simp_rw [Ordinal.le_zero.1 hba, zero_mul] exact nfp_zero_left 0 apply le_antisymm · apply nfp_le_fp (isNormal_mul_right ha).monotone hba rw [← opow_one_add, one_add_omega0] rw [← nfp_mul_one ha] exact nfp_monotone (isNormal_mul_right ha).monotone (one_le_iff_pos.2 hb) theorem eq_zero_or_opow_omega0_le_of_mul_eq_right {a b : Ordinal} (hab : a * b = b) : b = 0 ∨ a ^ ω ≤ b := by rcases eq_zero_or_pos a with ha | ha · rw [ha, zero_opow omega0_ne_zero] exact Or.inr (Ordinal.zero_le b) rw [or_iff_not_imp_left] intro hb rw [← nfp_mul_one ha] rw [← Ne, ← one_le_iff_ne_zero] at hb exact nfp_le_fp (isNormal_mul_right ha).monotone hb (le_of_eq hab) theorem mul_eq_right_iff_opow_omega0_dvd {a b : Ordinal} : a * b = b ↔ a ^ ω ∣ b := by rcases eq_zero_or_pos a with ha | ha · rw [ha, zero_mul, zero_opow omega0_ne_zero, zero_dvd_iff] exact eq_comm refine ⟨fun hab => ?_, fun h => ?_⟩ · rw [dvd_iff_mod_eq_zero] rw [← div_add_mod b (a ^ ω), mul_add, ← mul_assoc, ← opow_one_add, one_add_omega0, add_left_cancel_iff] at hab rcases eq_zero_or_opow_omega0_le_of_mul_eq_right hab with hab | hab · exact hab refine (not_lt_of_ge hab (mod_lt b (opow_ne_zero ω ?_))).elim rwa [← Ordinal.pos_iff_ne_zero] obtain ⟨c, hc⟩ := h rw [hc, ← mul_assoc, ← opow_one_add, one_add_omega0] theorem mul_le_right_iff_opow_omega0_dvd {a b : Ordinal} (ha : 0 < a) : a * b ≤ b ↔ (a ^ ω) ∣ b := by rw [← mul_eq_right_iff_opow_omega0_dvd] exact (isNormal_mul_right ha).le_iff_eq theorem nfp_mul_opow_omega0_add {a c : Ordinal} (b) (ha : 0 < a) (hc : 0 < c) (hca : c ≤ a ^ ω) : nfp (a * ·) (a ^ ω * b + c) = a ^ ω * succ b := by apply le_antisymm · apply nfp_le_fp (isNormal_mul_right ha).monotone · rw [mul_succ] gcongr · dsimp only; rw [← mul_assoc, ← opow_one_add, one_add_omega0] · obtain ⟨d, hd⟩ := mul_eq_right_iff_opow_omega0_dvd.1 ((isNormal_mul_right ha).nfp_fp ((a ^ ω) * b + c)) rw [hd] apply mul_le_mul_left' have := le_nfp (a * ·) (a ^ ω * b + c) rw [hd] at this have := (add_lt_add_left hc (a ^ ω * b)).trans_le this rw [add_zero, mul_lt_mul_iff_right₀ (opow_pos ω ha)] at this rwa [succ_le_iff] theorem deriv_mul_eq_opow_omega0_mul {a : Ordinal.{u}} (ha : 0 < a) (b) : deriv (a * ·) b = a ^ ω * b := by revert b rw [← funext_iff, IsNormal.eq_iff_zero_and_succ (isNormal_deriv _) (isNormal_mul_right (opow_pos ω ha))] refine ⟨?_, fun c h => ?_⟩ · dsimp only; rw [deriv_zero_right, nfp_mul_zero, mul_zero] · rw [deriv_succ, h] exact nfp_mul_opow_omega0_add c ha zero_lt_one (one_le_iff_pos.2 (opow_pos _ ha)) end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Finite.lean
import Mathlib.Data.ENat.Pow import Mathlib.Data.ULift import Mathlib.Data.ZMod.Defs import Mathlib.SetTheory.Cardinal.ToNat import Mathlib.SetTheory.Cardinal.ENat /-! # Finite Cardinality Functions ## Main Definitions * `Nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `Nat.card α = 0`. * `ENat.card α` is the cardinality of `α` as an extended natural number. If `α` is infinite, `ENat.card α = ⊤`. -/ assert_not_exists Field open Cardinal Function noncomputable section variable {α β : Type*} universe u v namespace Nat /-- `Nat.card α` is the cardinality of `α` as a natural number. If `α` is infinite, `Nat.card α = 0`. -/ protected def card (α : Type*) : ℕ := toNat (mk α) @[simp] theorem card_eq_fintype_card [Fintype α] : Nat.card α = Fintype.card α := mk_toNat_eq_card /-- Because this theorem takes `Fintype α` as a non-instance argument, it can be used in particular when `Fintype.card` ends up with different instance than the one found by inference -/ theorem _root_.Fintype.card_eq_nat_card {_ : Fintype α} : Fintype.card α = Nat.card α := mk_toNat_eq_card.symm lemma card_eq_finsetCard (s : Finset α) : Nat.card s = s.card := by simp only [Nat.card_eq_fintype_card, Fintype.card_coe] lemma card_eq_card_toFinset (s : Set α) [Fintype s] : Nat.card s = s.toFinset.card := by simp only [← Nat.card_eq_finsetCard, s.mem_toFinset] lemma card_eq_card_finite_toFinset {s : Set α} (hs : s.Finite) : Nat.card s = hs.toFinset.card := by simp only [← Nat.card_eq_finsetCard, hs.mem_toFinset] theorem subtype_card {p : α → Prop} (s : Finset α) (H : ∀ x : α, x ∈ s ↔ p x) : Nat.card { x // p x } = Finset.card s := by rw [← Fintype.subtype_card s H, Fintype.card_eq_nat_card] @[simp] theorem card_of_isEmpty [IsEmpty α] : Nat.card α = 0 := by simp [Nat.card] @[simp] lemma card_eq_zero_of_infinite [Infinite α] : Nat.card α = 0 := mk_toNat_of_infinite lemma cast_card [Finite α] : (Nat.card α : Cardinal) = Cardinal.mk α := by rw [Nat.card, Cardinal.cast_toNat_of_lt_aleph0] exact Cardinal.lt_aleph0_of_finite _ lemma _root_.Set.Infinite.card_eq_zero {s : Set α} (hs : s.Infinite) : Nat.card s = 0 := @card_eq_zero_of_infinite _ hs.to_subtype lemma card_eq_zero : Nat.card α = 0 ↔ IsEmpty α ∨ Infinite α := by simp [Nat.card, mk_eq_zero_iff, aleph0_le_mk_iff] lemma card_ne_zero : Nat.card α ≠ 0 ↔ Nonempty α ∧ Finite α := by simp [card_eq_zero, not_or] lemma card_pos_iff : 0 < Nat.card α ↔ Nonempty α ∧ Finite α := by simp [Nat.card, mk_eq_zero_iff, mk_lt_aleph0_iff] @[simp] lemma card_pos [Nonempty α] [Finite α] : 0 < Nat.card α := card_pos_iff.2 ⟨‹_›, ‹_›⟩ theorem finite_of_card_ne_zero (h : Nat.card α ≠ 0) : Finite α := (card_ne_zero.1 h).2 theorem card_congr (f : α ≃ β) : Nat.card α = Nat.card β := Cardinal.toNat_congr f lemma card_le_card_of_injective {α : Type u} {β : Type v} [Finite β] (f : α → β) (hf : Injective f) : Nat.card α ≤ Nat.card β := by simpa using toNat_le_toNat (lift_mk_le_lift_mk_of_injective hf) (by simp) lemma card_le_card_of_surjective {α : Type u} {β : Type v} [Finite α] (f : α → β) (hf : Surjective f) : Nat.card β ≤ Nat.card α := by have : lift.{u} #β ≤ lift.{v} #α := mk_le_of_surjective (ULift.map_surjective.2 hf) simpa using toNat_le_toNat this (by simp) theorem card_eq_of_bijective (f : α → β) (hf : Function.Bijective f) : Nat.card α = Nat.card β := card_congr (Equiv.ofBijective f hf) protected theorem bijective_iff_injective_and_card [Finite β] (f : α → β) : Bijective f ↔ Injective f ∧ Nat.card α = Nat.card β := by rw [Bijective, and_congr_right_iff] intro h have := Fintype.ofFinite β have := Fintype.ofInjective f h revert h rw [← and_congr_right_iff, ← Bijective, card_eq_fintype_card, card_eq_fintype_card, Fintype.bijective_iff_injective_and_card] protected theorem bijective_iff_surjective_and_card [Finite α] (f : α → β) : Bijective f ↔ Surjective f ∧ Nat.card α = Nat.card β := by classical rw [_root_.and_comm, Bijective, and_congr_left_iff] intro h have := Fintype.ofFinite α have := Fintype.ofSurjective f h revert h rw [← and_congr_left_iff, ← Bijective, ← and_comm, card_eq_fintype_card, card_eq_fintype_card, Fintype.bijective_iff_surjective_and_card] theorem _root_.Function.Injective.bijective_of_nat_card_le [Finite β] {f : α → β} (inj : Injective f) (hc : Nat.card β ≤ Nat.card α) : Bijective f := (Nat.bijective_iff_injective_and_card f).mpr ⟨inj, hc.antisymm (card_le_card_of_injective f inj) |>.symm⟩ theorem _root_.Function.Surjective.bijective_of_nat_card_le [Finite α] {f : α → β} (surj : Surjective f) (hc : Nat.card α ≤ Nat.card β) : Bijective f := (Nat.bijective_iff_surjective_and_card f).mpr ⟨surj, hc.antisymm (card_le_card_of_surjective f surj)⟩ theorem card_eq_of_equiv_fin {α : Type*} {n : ℕ} (f : α ≃ Fin n) : Nat.card α = n := by simpa only [card_eq_fintype_card, Fintype.card_fin] using card_congr f lemma card_fin (n : ℕ) : Nat.card (Fin n) = n := by rw [Nat.card_eq_fintype_card, Fintype.card_fin] section Set open Set variable {s t : Set α} lemma card_mono (ht : t.Finite) (h : s ⊆ t) : Nat.card s ≤ Nat.card t := toNat_le_toNat (mk_le_mk_of_subset h) ht.lt_aleph0 lemma card_image_le {f : α → β} (hs : s.Finite) : Nat.card (f '' s) ≤ Nat.card s := have := hs.to_subtype card_le_card_of_surjective (imageFactorization f s) imageFactorization_surjective lemma card_image_of_injOn {f : α → β} (hf : s.InjOn f) : Nat.card (f '' s) = Nat.card s := by classical obtain hs | hs := s.finite_or_infinite · have := hs.fintype have := fintypeImage s f simp_rw [Nat.card_eq_fintype_card, Set.card_image_of_inj_on hf] · have := hs.to_subtype have := (hs.image hf).to_subtype simp [Nat.card_eq_zero_of_infinite] lemma card_image_of_injective {f : α → β} (hf : Injective f) (s : Set α) : Nat.card (f '' s) = Nat.card s := card_image_of_injOn hf.injOn lemma card_image_equiv (e : α ≃ β) : Nat.card (e '' s) = Nat.card s := Nat.card_congr (e.image s).symm lemma card_preimage_of_injOn {f : α → β} {s : Set β} (hf : (f ⁻¹' s).InjOn f) (hsf : s ⊆ range f) : Nat.card (f ⁻¹' s) = Nat.card s := by rw [← Nat.card_image_of_injOn hf, image_preimage_eq_iff.2 hsf] lemma card_preimage_of_injective {f : α → β} {s : Set β} (hf : Injective f) (hsf : s ⊆ range f) : Nat.card (f ⁻¹' s) = Nat.card s := card_preimage_of_injOn hf.injOn hsf lemma card_univ : Nat.card (univ : Set α) = Nat.card α := card_congr (Equiv.Set.univ α) lemma card_range_of_injective {f : α → β} (hf : Injective f) : Nat.card (range f) = Nat.card α := by rw [← Nat.card_preimage_of_injective hf le_rfl] simp [Nat.card_univ] end Set /-- If the cardinality is positive, that means it is a finite type, so there is an equivalence between `α` and `Fin (Nat.card α)`. See also `Finite.equivFin`. -/ def equivFinOfCardPos {α : Type*} (h : Nat.card α ≠ 0) : α ≃ Fin (Nat.card α) := by cases fintypeOrInfinite α · simpa only [card_eq_fintype_card] using Fintype.equivFin α · simp only [card_eq_zero_of_infinite, ne_eq, not_true_eq_false] at h theorem card_of_subsingleton (a : α) [Subsingleton α] : Nat.card α = 1 := by letI := Fintype.ofSubsingleton a rw [card_eq_fintype_card, Fintype.card_ofSubsingleton a] theorem card_eq_one_iff_unique : Nat.card α = 1 ↔ Subsingleton α ∧ Nonempty α := Cardinal.toNat_eq_one_iff_unique @[simp] theorem card_unique [Nonempty α] [Subsingleton α] : Nat.card α = 1 := by simp [card_eq_one_iff_unique, *] theorem card_eq_one_iff_exists : Nat.card α = 1 ↔ ∃ x : α, ∀ y : α, y = x := by rw [card_eq_one_iff_unique] exact ⟨fun ⟨s, ⟨a⟩⟩ ↦ ⟨a, fun x ↦ s.elim x a⟩, fun ⟨x, h⟩ ↦ ⟨subsingleton_of_forall_eq x h, ⟨x⟩⟩⟩ theorem card_eq_two_iff : Nat.card α = 2 ↔ ∃ x y : α, x ≠ y ∧ {x, y} = @Set.univ α := toNat_eq_ofNat.trans mk_eq_two_iff theorem card_eq_two_iff' (x : α) : Nat.card α = 2 ↔ ∃! y, y ≠ x := toNat_eq_ofNat.trans (mk_eq_two_iff' x) @[simp] theorem card_subtype_true : Nat.card {_a : α // True} = Nat.card α := card_congr <| Equiv.subtypeUnivEquiv fun _ => trivial @[simp] theorem card_sum [Finite α] [Finite β] : Nat.card (α ⊕ β) = Nat.card α + Nat.card β := by have := Fintype.ofFinite α have := Fintype.ofFinite β simp_rw [Nat.card_eq_fintype_card, Fintype.card_sum] @[simp] theorem card_prod (α β : Type*) : Nat.card (α × β) = Nat.card α * Nat.card β := by simp only [Nat.card, mk_prod, toNat_mul, toNat_lift] @[simp] theorem card_ulift (α : Type*) : Nat.card (ULift α) = Nat.card α := card_congr Equiv.ulift @[simp] theorem card_plift (α : Type*) : Nat.card (PLift α) = Nat.card α := card_congr Equiv.plift theorem card_sigma {β : α → Type*} [Fintype α] [∀ a, Finite (β a)] : Nat.card (Sigma β) = ∑ a, Nat.card (β a) := by letI _ (a : α) : Fintype (β a) := Fintype.ofFinite (β a) simp_rw [Nat.card_eq_fintype_card, Fintype.card_sigma] theorem card_pi {β : α → Type*} [Fintype α] : Nat.card (∀ a, β a) = ∏ a, Nat.card (β a) := by simp_rw [Nat.card, mk_pi, prod_eq_of_fintype, toNat_lift, map_prod] theorem card_fun [Finite α] : Nat.card (α → β) = Nat.card β ^ Nat.card α := by haveI := Fintype.ofFinite α rw [Nat.card_pi, Finset.prod_const, Finset.card_univ, ← Nat.card_eq_fintype_card] @[simp] theorem card_zmod (n : ℕ) : Nat.card (ZMod n) = n := by cases n · exact @Nat.card_eq_zero_of_infinite _ Int.infinite · rw [Nat.card_eq_fintype_card, ZMod.card] end Nat namespace Set variable {s : Set α} lemma card_singleton_prod (a : α) (t : Set β) : Nat.card ({a} ×ˢ t) = Nat.card t := by rw [singleton_prod, Nat.card_image_of_injective (Prod.mk_right_injective a)] lemma card_prod_singleton (s : Set α) (b : β) : Nat.card (s ×ˢ {b}) = Nat.card s := by rw [prod_singleton, Nat.card_image_of_injective (Prod.mk_left_injective b)] theorem natCard_pos (hs : s.Finite) : 0 < Nat.card s ↔ s.Nonempty := by simp [pos_iff_ne_zero, Nat.card_eq_zero, hs.to_subtype, nonempty_iff_ne_empty] protected alias ⟨_, Nonempty.natCard_pos⟩ := natCard_pos lemma natCard_graphOn (s : Set α) (f : α → β) : Nat.card (s.graphOn f) = Nat.card s := by rw [← Nat.card_image_of_injOn fst_injOn_graph, image_fst_graphOn] end Set namespace ENat /-- `ENat.card α` is the cardinality of `α` as an extended natural number. If `α` is infinite, `ENat.card α = ⊤`. -/ def card (α : Type*) : ℕ∞ := toENat (mk α) @[simp] theorem card_eq_coe_fintype_card [Fintype α] : card α = Fintype.card α := by simp [card] @[simp high] theorem card_eq_top_of_infinite [Infinite α] : card α = ⊤ := by simp only [card, toENat_eq_top, aleph0_le_mk] @[simp] lemma card_eq_top : card α = ⊤ ↔ Infinite α := by simp [card, aleph0_le_mk_iff] @[simp high] theorem card_lt_top_of_finite [Finite α] : card α < ⊤ := by simp [card] @[simp] theorem card_lt_top : card α < ⊤ ↔ Finite α := by simp [card, lt_aleph0_iff_finite] @[simp] theorem card_sum (α β : Type*) : card (α ⊕ β) = card α + card β := by simp only [card, mk_sum, map_add, toENat_lift] theorem card_congr {α β : Type*} (f : α ≃ β) : card α = card β := Cardinal.toENat_congr f @[simp] lemma card_ulift (α : Type*) : card (ULift α) = card α := card_congr Equiv.ulift @[simp] lemma card_plift (α : Type*) : card (PLift α) = card α := card_congr Equiv.plift theorem card_image_of_injOn {α β : Type*} {f : α → β} {s : Set α} (h : Set.InjOn f s) : card (f '' s) = card s := card_congr (Equiv.Set.imageOfInjOn f s h).symm theorem card_image_of_injective {α β : Type*} (f : α → β) (s : Set α) (h : Function.Injective f) : card (f '' s) = card s := card_image_of_injOn h.injOn lemma card_le_card_of_injective {α β : Type*} {f : α → β} (hf : Injective f) : card α ≤ card β := by rw [← card_ulift α, ← card_ulift β] exact Cardinal.gciENat.gc.monotone_u <| Cardinal.lift_mk_le_lift_mk_of_injective hf @[simp] theorem _root_.Cardinal.natCast_le_toENat_iff {n : ℕ} {c : Cardinal} : ↑n ≤ toENat c ↔ ↑n ≤ c := by rw [← toENat_nat n, toENat_le_iff_of_le_aleph0 (le_of_lt (nat_lt_aleph0 n))] theorem _root_.Cardinal.toENat_le_natCast_iff {c : Cardinal} {n : ℕ} : toENat c ≤ n ↔ c ≤ n := by simp @[simp] theorem _root_.Cardinal.natCast_eq_toENat_iff {n : ℕ} {c : Cardinal} : ↑n = toENat c ↔ ↑n = c := by rw [le_antisymm_iff, le_antisymm_iff, Cardinal.toENat_le_natCast_iff, Cardinal.natCast_le_toENat_iff] theorem _root_.Cardinal.toENat_eq_natCast_iff {c : Cardinal} {n : ℕ} : Cardinal.toENat c = n ↔ c = n := by simp @[simp] theorem _root_.Cardinal.natCast_lt_toENat_iff {n : ℕ} {c : Cardinal} : ↑n < toENat c ↔ ↑n < c := by simp only [← not_le, Cardinal.toENat_le_natCast_iff] @[simp] theorem _root_.Cardinal.toENat_lt_natCast_iff {n : ℕ} {c : Cardinal} : toENat c < ↑n ↔ c < ↑n := by simp only [← not_le, Cardinal.natCast_le_toENat_iff] theorem card_eq_zero_iff_empty (α : Type*) : card α = 0 ↔ IsEmpty α := by rw [← Cardinal.mk_eq_zero_iff] simp [card] theorem card_ne_zero_iff_nonempty (α : Type*) : card α ≠ 0 ↔ Nonempty α := by simp [card_eq_zero_iff_empty] theorem one_le_card_iff_nonempty (α : Type*) : 1 ≤ card α ↔ Nonempty α := by simp [one_le_iff_ne_zero, card_eq_zero_iff_empty] @[simp] lemma card_pos [Nonempty α] : 0 < card α := by simpa [pos_iff_ne_zero, card_ne_zero_iff_nonempty] theorem card_le_one_iff_subsingleton (α : Type*) : card α ≤ 1 ↔ Subsingleton α := by rw [← le_one_iff_subsingleton] simp [card] @[simp] lemma card_le_one [Subsingleton α] : card α ≤ 1 := by simpa [card_le_one_iff_subsingleton] lemma card_eq_one_iff_unique {α : Type*} : card α = 1 ↔ Nonempty (Unique α) := by rw [unique_iff_subsingleton_and_nonempty α, le_antisymm_iff] exact and_congr (card_le_one_iff_subsingleton α) (one_le_card_iff_nonempty α) theorem one_lt_card_iff_nontrivial (α : Type*) : 1 < card α ↔ Nontrivial α := by rw [← Cardinal.one_lt_iff_nontrivial] conv_rhs => rw [← Nat.cast_one] rw [← natCast_lt_toENat_iff] simp only [ENat.card, Nat.cast_one] @[simp] lemma one_lt_card [Nontrivial α] : 1 < card α := by simpa [one_lt_card_iff_nontrivial] @[simp] theorem card_prod (α β : Type*) : card (α × β) = card α * card β := by simp [ENat.card] @[simp] lemma card_fun {α β : Type*} : card (α → β) = (card β) ^ card α := by classical rcases isEmpty_or_nonempty α with α_emp | α_emp · simp [(card_eq_zero_iff_empty α).2 α_emp] rcases finite_or_infinite α · rcases finite_or_infinite β · letI := Fintype.ofFinite α letI := Fintype.ofFinite β simp · simp only [card_eq_top_of_infinite] exact (top_epow (one_le_iff_ne_zero.1 ((one_le_card_iff_nonempty α).2 α_emp))).symm · rw [card_eq_top_of_infinite (α := α)] rcases lt_trichotomy (card β) 1 with b_0 | b_1 | b_2 · rw [lt_one_iff_eq_zero, card_eq_zero_iff_empty] at b_0 rw [(card_eq_zero_iff_empty β).2 b_0, zero_epow_top, card_eq_zero_iff_empty] simp [b_0] · rw [b_1, one_epow] apply le_antisymm · letI := (card_le_one_iff_subsingleton β).1 b_1.le exact (card_le_one_iff_subsingleton (α → β)).2 Pi.instSubsingleton · letI := (one_le_card_iff_nonempty β).1 b_1.ge exact (one_le_card_iff_nonempty (α → β)).2 Pi.instNonempty · rw [epow_top b_2, card_eq_top] rw [one_lt_card_iff_nontrivial β] at b_2 exact Pi.infinite_of_left end ENat
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Regular.lean
import Mathlib.SetTheory.Cardinal.Cofinality import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Regular cardinals This file defines regular and inaccessible cardinals. ## Main definitions * `Cardinal.IsRegular c` means that `c` is a regular cardinal: `ℵ₀ ≤ c ∧ c.ord.cof = c`. * `Cardinal.IsInaccessible c` means that `c` is strongly inaccessible: `ℵ₀ < c ∧ IsRegular c ∧ IsStrongLimit c`. ## TODO * Generalize the universes in the lemmas about `iSup`, by taking a `Small` assumption when necessary instead. * Prove more theorems on inaccessible cardinals. * Define singular cardinals. -/ universe u v open Function Cardinal Set Order Ordinal namespace Cardinal /-! ### Regular cardinals -/ /-- A cardinal is regular if it is infinite and it equals its own cofinality. -/ def IsRegular (c : Cardinal) : Prop := ℵ₀ ≤ c ∧ c ≤ c.ord.cof theorem IsRegular.aleph0_le {c : Cardinal} (H : c.IsRegular) : ℵ₀ ≤ c := H.1 theorem IsRegular.cof_eq {c : Cardinal} (H : c.IsRegular) : c.ord.cof = c := (cof_ord_le c).antisymm H.2 theorem IsRegular.cof_omega_eq {o : Ordinal} (H : (ℵ_ o).IsRegular) : (ω_ o).cof = ℵ_ o := by rw [← ord_aleph, H.cof_eq] theorem IsRegular.pos {c : Cardinal} (H : c.IsRegular) : 0 < c := aleph0_pos.trans_le H.1 theorem IsRegular.nat_lt {c : Cardinal} (H : c.IsRegular) (n : ℕ) : n < c := lt_of_lt_of_le (nat_lt_aleph0 n) H.aleph0_le theorem IsRegular.ord_pos {c : Cardinal} (H : c.IsRegular) : 0 < c.ord := by rw [Cardinal.lt_ord, card_zero] exact H.pos theorem isRegular_cof {o : Ordinal} (h : IsSuccLimit o) : IsRegular o.cof := ⟨aleph0_le_cof.2 h, (cof_cof o).ge⟩ /-- If `c` is a regular cardinal, then `c.ord.toType` has a least element. -/ lemma IsRegular.ne_zero {c : Cardinal} (H : c.IsRegular) : c ≠ 0 := H.pos.ne' theorem isRegular_aleph0 : IsRegular ℵ₀ := ⟨le_rfl, by simp⟩ lemma fact_isRegular_aleph0 : Fact (IsRegular ℵ₀) where out := isRegular_aleph0 theorem isRegular_succ {c : Cardinal.{u}} (h : ℵ₀ ≤ c) : IsRegular (succ c) := ⟨h.trans (le_succ c), succ_le_of_lt (by have αe := Cardinal.mk_out (succ c) set α := (succ c).out rcases ord_eq α with ⟨r, wo, re⟩ have := isSuccLimit_ord (h.trans (le_succ _)) rw [← αe, re] at this ⊢ rcases cof_eq' r this with ⟨S, H, Se⟩ rw [← Se] apply lt_imp_lt_of_le_imp_le fun h => mul_le_mul_right' h c rw [mul_eq_self h, ← succ_le_iff, ← αe, ← sum_const'] refine le_trans ?_ (sum_le_sum (fun (x : S) => card (typein r (x : α))) _ fun i => ?_) · simp only [← card_typein, ← mk_sigma] exact ⟨Embedding.ofSurjective (fun x => x.2.1) fun a => let ⟨b, h, ab⟩ := H a ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩⟩ · rw [← lt_succ_iff, ← lt_ord, ← αe, re] apply typein_lt_type)⟩ theorem isRegular_aleph_one : IsRegular ℵ₁ := by rw [← succ_aleph0] exact isRegular_succ le_rfl theorem isRegular_preAleph_succ {o : Ordinal} (h : ω ≤ o) : IsRegular (preAleph (succ o)) := by rw [preAleph_succ] exact isRegular_succ (aleph0_le_preAleph.2 h) theorem isRegular_aleph_succ (o : Ordinal) : IsRegular (ℵ_ (succ o)) := by rw [aleph_succ] exact isRegular_succ (aleph0_le_aleph o) lemma IsRegular.lift {κ : Cardinal.{v}} (h : κ.IsRegular) : (Cardinal.lift.{u} κ).IsRegular := by obtain ⟨h₁, h₂⟩ := h constructor · simpa · rwa [← Cardinal.lift_ord, ← Ordinal.lift_cof, lift_le] @[simp] lemma isRegular_lift_iff {κ : Cardinal.{v}} : (Cardinal.lift.{u} κ).IsRegular ↔ κ.IsRegular := ⟨fun ⟨h₁, h₂⟩ ↦ ⟨by simpa using h₁, by simpa [← lift_le.{u, v}]⟩, fun h ↦ h.lift⟩ theorem lsub_lt_ord_lift_of_isRegular {ι} {f : ι → Ordinal} {c} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} #ι < c) : (∀ i, f i < c.ord) → Ordinal.lsub.{u, v} f < c.ord := lsub_lt_ord_lift (by rwa [hc.cof_eq]) theorem lsub_lt_ord_of_isRegular {ι} {f : ι → Ordinal} {c} (hc : IsRegular c) (hι : #ι < c) : (∀ i, f i < c.ord) → Ordinal.lsub f < c.ord := lsub_lt_ord (by rwa [hc.cof_eq]) theorem iSup_lt_ord_lift_of_isRegular {ι} {f : ι → Ordinal} {c} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} #ι < c) : (∀ i, f i < c.ord) → iSup f < c.ord := iSup_lt_ord_lift (by rwa [hc.cof_eq]) theorem iSup_lt_ord_of_isRegular {ι} {f : ι → Ordinal} {c} (hc : IsRegular c) (hι : #ι < c) : (∀ i, f i < c.ord) → iSup f < c.ord := iSup_lt_ord (by rwa [hc.cof_eq]) theorem blsub_lt_ord_lift_of_isRegular {o : Ordinal} {f : ∀ a < o, Ordinal} {c} (hc : IsRegular c) (ho : Cardinal.lift.{v, u} o.card < c) : (∀ i hi, f i hi < c.ord) → Ordinal.blsub.{u, v} o f < c.ord := blsub_lt_ord_lift (by rwa [hc.cof_eq]) theorem blsub_lt_ord_of_isRegular {o : Ordinal} {f : ∀ a < o, Ordinal} {c} (hc : IsRegular c) (ho : o.card < c) : (∀ i hi, f i hi < c.ord) → Ordinal.blsub o f < c.ord := blsub_lt_ord (by rwa [hc.cof_eq]) theorem bsup_lt_ord_lift_of_isRegular {o : Ordinal} {f : ∀ a < o, Ordinal} {c} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} o.card < c) : (∀ i hi, f i hi < c.ord) → Ordinal.bsup.{u, v} o f < c.ord := bsup_lt_ord_lift (by rwa [hc.cof_eq]) theorem bsup_lt_ord_of_isRegular {o : Ordinal} {f : ∀ a < o, Ordinal} {c} (hc : IsRegular c) (hι : o.card < c) : (∀ i hi, f i hi < c.ord) → Ordinal.bsup o f < c.ord := bsup_lt_ord (by rwa [hc.cof_eq]) theorem iSup_lt_lift_of_isRegular {ι} {f : ι → Cardinal} {c} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} #ι < c) : (∀ i, f i < c) → iSup.{max u v + 1, u + 1} f < c := iSup_lt_lift.{u, v} (by rwa [hc.cof_eq]) theorem iSup_lt_of_isRegular {ι} {f : ι → Cardinal} {c} (hc : IsRegular c) (hι : #ι < c) : (∀ i, f i < c) → iSup f < c := iSup_lt (by rwa [hc.cof_eq]) theorem sum_lt_lift_of_isRegular {ι : Type u} {f : ι → Cardinal} {c : Cardinal} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} #ι < c) (hf : ∀ i, f i < c) : sum f < c := (sum_le_lift_mk_mul_iSup _).trans_lt <| mul_lt_of_lt hc.1 hι (iSup_lt_lift_of_isRegular hc hι hf) theorem sum_lt_of_isRegular {ι : Type u} {f : ι → Cardinal} {c : Cardinal} (hc : IsRegular c) (hι : #ι < c) : (∀ i, f i < c) → sum f < c := sum_lt_lift_of_isRegular.{u, u} hc (by rwa [lift_id]) @[simp] theorem card_lt_of_card_iUnion_lt {ι : Type u} {α : Type u} {t : ι → Set α} {c : Cardinal} (h : #(⋃ i, t i) < c) (i : ι) : #(t i) < c := lt_of_le_of_lt (Cardinal.mk_le_mk_of_subset <| subset_iUnion _ _) h @[simp] theorem card_iUnion_lt_iff_forall_of_isRegular {ι : Type u} {α : Type u} {t : ι → Set α} {c : Cardinal} (hc : c.IsRegular) (hι : #ι < c) : #(⋃ i, t i) < c ↔ ∀ i, #(t i) < c := by refine ⟨card_lt_of_card_iUnion_lt, fun h ↦ ?_⟩ apply lt_of_le_of_lt (Cardinal.mk_sUnion_le _) apply Cardinal.mul_lt_of_lt hc.aleph0_le (lt_of_le_of_lt Cardinal.mk_range_le hι) apply Cardinal.iSup_lt_of_isRegular hc (lt_of_le_of_lt Cardinal.mk_range_le hι) simpa theorem card_lt_of_card_biUnion_lt {α β : Type u} {s : Set α} {t : ∀ a ∈ s, Set β} {c : Cardinal} (h : #(⋃ a ∈ s, t a ‹_›) < c) (a : α) (ha : a ∈ s) : # (t a ha) < c := by rw [biUnion_eq_iUnion] at h have := card_lt_of_card_iUnion_lt h simp_all only [iUnion_coe_set, Subtype.forall] theorem card_biUnion_lt_iff_forall_of_isRegular {α β : Type u} {s : Set α} {t : ∀ a ∈ s, Set β} {c : Cardinal} (hc : c.IsRegular) (hs : #s < c) : #(⋃ a ∈ s, t a ‹_›) < c ↔ ∀ a (ha : a ∈ s), # (t a ha) < c := by rw [biUnion_eq_iUnion, card_iUnion_lt_iff_forall_of_isRegular hc hs, SetCoe.forall'] theorem nfpFamily_lt_ord_lift_of_isRegular {ι} {f : ι → Ordinal → Ordinal} {c} (hc : IsRegular c) (hι : Cardinal.lift.{v, u} #ι < c) (hc' : c ≠ ℵ₀) (hf : ∀ (i), ∀ b < c.ord, f i b < c.ord) {a} (ha : a < c.ord) : nfpFamily f a < c.ord := by apply nfpFamily_lt_ord_lift _ _ hf ha <;> rw [hc.cof_eq] · exact lt_of_le_of_ne hc.1 hc'.symm · exact hι theorem nfpFamily_lt_ord_of_isRegular {ι} {f : ι → Ordinal → Ordinal} {c} (hc : IsRegular c) (hι : #ι < c) (hc' : c ≠ ℵ₀) {a} (hf : ∀ (i), ∀ b < c.ord, f i b < c.ord) : a < c.ord → nfpFamily.{u, u} f a < c.ord := nfpFamily_lt_ord_lift_of_isRegular hc (by rwa [lift_id]) hc' hf theorem nfp_lt_ord_of_isRegular {f : Ordinal → Ordinal} {c} (hc : IsRegular c) (hc' : c ≠ ℵ₀) (hf : ∀ i < c.ord, f i < c.ord) {a} : a < c.ord → nfp f a < c.ord := nfp_lt_ord (by rw [hc.cof_eq]; exact lt_of_le_of_ne hc.1 hc'.symm) hf theorem derivFamily_lt_ord_lift {ι : Type u} {f : ι → Ordinal → Ordinal} {c} (hc : IsRegular c) (hι : lift.{v} #ι < c) (hc' : c ≠ ℵ₀) (hf : ∀ i, ∀ b < c.ord, f i b < c.ord) {a} : a < c.ord → derivFamily f a < c.ord := by have hω : ℵ₀ < c.ord.cof := by rw [hc.cof_eq] exact lt_of_le_of_ne hc.1 hc'.symm induction a using limitRecOn with | zero => rw [derivFamily_zero] exact nfpFamily_lt_ord_lift hω (by rwa [hc.cof_eq]) hf | succ b hb => intro hb' rw [derivFamily_succ] exact nfpFamily_lt_ord_lift hω (by rwa [hc.cof_eq]) hf ((isSuccLimit_ord hc.1).succ_lt (hb ((lt_succ b).trans hb'))) | limit b hb H => intro hb' -- TODO: generalize the universes of the lemmas in this file so we don't have to rely on bsup have : ⨆ a : Iio b, _ = _ := iSup_Iio_eq_bsup (f := fun x (_ : x < b) ↦ derivFamily f x) rw [derivFamily_limit f hb, this] exact bsup_lt_ord_of_isRegular.{u, v} hc (ord_lt_ord.1 ((ord_card_le b).trans_lt hb')) fun o' ho' => H o' ho' (ho'.trans hb') theorem derivFamily_lt_ord {ι} {f : ι → Ordinal → Ordinal} {c} (hc : IsRegular c) (hι : #ι < c) (hc' : c ≠ ℵ₀) (hf : ∀ (i), ∀ b < c.ord, f i b < c.ord) {a} : a < c.ord → derivFamily.{u, u} f a < c.ord := derivFamily_lt_ord_lift hc (by rwa [lift_id]) hc' hf theorem deriv_lt_ord {f : Ordinal.{u} → Ordinal} {c} (hc : IsRegular c) (hc' : c ≠ ℵ₀) (hf : ∀ i < c.ord, f i < c.ord) {a} : a < c.ord → deriv f a < c.ord := derivFamily_lt_ord_lift hc (by simpa using Cardinal.one_lt_aleph0.trans (lt_of_le_of_ne hc.1 hc'.symm)) hc' fun _ => hf /-! ### Inaccessible cardinals -/ /-- A cardinal is inaccessible if it is an uncountable regular strong limit cardinal. -/ def IsInaccessible (c : Cardinal) : Prop := ℵ₀ < c ∧ c ≤ c.ord.cof ∧ ∀ x < c, 2 ^ x < c @[deprecated "use the default constructor for `IsInaccessible`" (since := "2025-07-01")] theorem IsInaccessible.mk {c} (h₁ : ℵ₀ < c) (h₂ : c ≤ c.ord.cof) (h₃ : ∀ x < c, (2 ^ x) < c) : IsInaccessible c := ⟨h₁, h₂, h₃⟩ theorem IsInaccessible.aleph0_lt {c : Cardinal} (h : IsInaccessible c) : ℵ₀ < c := h.1 theorem IsInaccessible.nat_lt {c : Cardinal} (h : IsInaccessible c) (n : ℕ) : n < c := (nat_lt_aleph0 n).trans h.1 theorem IsInaccessible.pos {c : Cardinal} (h : IsInaccessible c) : 0 < c := aleph0_pos.trans h.1 theorem IsInaccessible.ne_zero {c : Cardinal} (h : IsInaccessible c) : c ≠ 0 := h.pos.ne' theorem IsInaccessible.isRegular {c : Cardinal} (h : IsInaccessible c) : IsRegular c := ⟨h.aleph0_lt.le, h.2.1⟩ theorem IsInaccessible.isStrongLimit {c : Cardinal} (h : IsInaccessible c) : IsStrongLimit c := ⟨h.ne_zero, h.2.2⟩ theorem isInaccessible_def {c : Cardinal} : IsInaccessible c ↔ ℵ₀ < c ∧ IsRegular c ∧ IsStrongLimit c where mp h := ⟨h.aleph0_lt, h.isRegular, h.isStrongLimit⟩ mpr := fun ⟨h₁, h₂, h₃⟩ ↦ ⟨h₁, h₂.2, h₃.two_power_lt⟩ @[deprecated (since := "2025-08-20")] alias isInaccesible_def := isInaccessible_def -- Lean's foundations prove the existence of ℵ₀ many inaccessible cardinals theorem IsInaccessible.univ : IsInaccessible univ.{u, v} := ⟨aleph0_lt_univ, by simp, IsStrongLimit.univ.two_power_lt⟩ @[deprecated IsInaccessible.univ (since := "2025-07-01")] alias univ_inaccessible := IsInaccessible.univ -- TODO: prove that `IsInaccessible o.card` implies `IsInaccessible (ℵ_ o)` and -- `IsInaccessible (ℶ_ o)` end Cardinal section Omega1 namespace Ordinal open Cardinal open scoped Ordinal -- TODO: generalize universes, and use ω₁. lemma iSup_sequence_lt_omega1 {α : Type u} [Countable α] (o : α → Ordinal.{max u v}) (ho : ∀ n, o n < (aleph 1).ord) : iSup o < (aleph 1).ord := by apply iSup_lt_ord_lift _ ho rw [Cardinal.isRegular_aleph_one.cof_eq] exact lt_of_le_of_lt mk_le_aleph0 aleph0_lt_aleph_one end Ordinal end Omega1
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Aleph.lean
import Mathlib.SetTheory.Cardinal.ToNat import Mathlib.SetTheory.Cardinal.ENat import Mathlib.SetTheory.Ordinal.Enum /-! # Omega, aleph, and beth functions This file defines the `ω`, `ℵ`, and `ℶ` functions which enumerate certain kinds of ordinals and cardinals. Each is provided in two variants: the standard versions which only take infinite values, and "preliminary" versions which include finite values and are sometimes more convenient. * The function `Ordinal.preOmega` enumerates the initial ordinals, i.e. the smallest ordinals with any given cardinality. Thus `preOmega n = n`, `preOmega ω = ω`, `preOmega (ω + 1) = ω₁`, etc. `Ordinal.omega` is the more standard function which skips over finite ordinals. * The function `Cardinal.preAleph` is an order isomorphism between ordinals and cardinals. Thus `preAleph n = n`, `preAleph ω = ℵ₀`, `preAleph (ω + 1) = ℵ₁`, etc. `Cardinal.aleph` is the more standard function which skips over finite cardinals. * The function `Cardinal.preBeth` is the unique normal function with `beth 0 = 0` and `beth (succ o) = 2 ^ beth o`. `Cardinal.beth` is the more standard function which skips over finite cardinals. ## Notation The following notations are scoped to the `Ordinal` namespace. - `ω_ o` is notation for `Ordinal.omega o`. `ω₁` is notation for `ω_ 1`. The following notations are scoped to the `Cardinal` namespace. - `ℵ_ o` is notation for `aleph o`. `ℵ₁` is notation for `ℵ_ 1`. - `ℶ_ o` is notation for `beth o`. The value `ℶ_ 1` equals the continuum `𝔠`, which is defined in `Mathlib/SetTheory/Cardinal/Continuum.lean`. -/ assert_not_exists Field Finsupp Module Cardinal.mul_eq_self noncomputable section open Function Set Cardinal Equiv Order Ordinal universe u v w /-! ### Omega ordinals -/ namespace Ordinal /-- An ordinal is initial when it is the first ordinal with a given cardinality. This is written as `o.card.ord = o`, i.e. `o` is the smallest ordinal with cardinality `o.card`. -/ def IsInitial (o : Ordinal) : Prop := o.card.ord = o theorem IsInitial.ord_card {o : Ordinal} (h : IsInitial o) : o.card.ord = o := h theorem IsInitial.card_le_card {a b : Ordinal} (ha : IsInitial a) : a.card ≤ b.card ↔ a ≤ b := by refine ⟨fun h ↦ ?_, Ordinal.card_le_card⟩ rw [← ord_le_ord, ha.ord_card] at h exact h.trans (ord_card_le b) theorem IsInitial.card_lt_card {a b : Ordinal} (hb : IsInitial b) : a.card < b.card ↔ a < b := lt_iff_lt_of_le_iff_le hb.card_le_card theorem isInitial_ord (c : Cardinal) : IsInitial c.ord := by rw [IsInitial, card_ord] @[simp] theorem isInitial_natCast (n : ℕ) : IsInitial n := by rw [IsInitial, card_nat, ord_nat] theorem isInitial_zero : IsInitial 0 := by exact_mod_cast isInitial_natCast 0 theorem isInitial_one : IsInitial 1 := by exact_mod_cast isInitial_natCast 1 theorem isInitial_omega0 : IsInitial ω := by rw [IsInitial, card_omega0, ord_aleph0] theorem isInitial_succ {o : Ordinal} : IsInitial (succ o) ↔ o < ω := ⟨Function.mtr fun hwo ↦ ne_of_lt <| by simp_all [ord_card_le], fun how ↦ (Ordinal.lt_omega0.1 how).rec fun n h ↦ h ▸ isInitial_natCast (n + 1)⟩ theorem not_bddAbove_isInitial : ¬ BddAbove {x | IsInitial x} := by rintro ⟨a, ha⟩ have := ha (isInitial_ord (succ a.card)) rw [ord_le] at this exact (lt_succ _).not_ge this /-- Initial ordinals are order-isomorphic to the cardinals. -/ @[simps!] def isInitialIso : {x // IsInitial x} ≃o Cardinal where toFun x := x.1.card invFun x := ⟨x.ord, isInitial_ord _⟩ left_inv x := Subtype.ext x.2.ord_card right_inv x := card_ord x map_rel_iff' {a _} := a.2.card_le_card /-- The "pre-omega" function gives the initial ordinals listed by their ordinal index. `preOmega n = n`, `preOmega ω = ω`, `preOmega (ω + 1) = ω₁`, etc. For the more common omega function skipping over finite ordinals, see `Ordinal.omega`. -/ def preOmega : Ordinal.{u} ↪o Ordinal.{u} where toFun := enumOrd {x | IsInitial x} inj' _ _ h := enumOrd_injective not_bddAbove_isInitial h map_rel_iff' := enumOrd_le_enumOrd not_bddAbove_isInitial theorem coe_preOmega : preOmega = enumOrd {x | IsInitial x} := rfl theorem preOmega_strictMono : StrictMono preOmega := preOmega.strictMono theorem preOmega_lt_preOmega {o₁ o₂ : Ordinal} : preOmega o₁ < preOmega o₂ ↔ o₁ < o₂ := preOmega.lt_iff_lt theorem preOmega_le_preOmega {o₁ o₂ : Ordinal} : preOmega o₁ ≤ preOmega o₂ ↔ o₁ ≤ o₂ := preOmega.le_iff_le theorem preOmega_max (o₁ o₂ : Ordinal) : preOmega (max o₁ o₂) = max (preOmega o₁) (preOmega o₂) := preOmega.monotone.map_max theorem isInitial_preOmega (o : Ordinal) : IsInitial (preOmega o) := enumOrd_mem not_bddAbove_isInitial o theorem le_preOmega_self (o : Ordinal) : o ≤ preOmega o := preOmega_strictMono.le_apply @[simp] theorem preOmega_zero : preOmega 0 = 0 := by rw [coe_preOmega, enumOrd_zero] exact csInf_eq_bot_of_bot_mem isInitial_zero @[simp] theorem preOmega_natCast (n : ℕ) : preOmega n = n := by induction n with | zero => exact preOmega_zero | succ n IH => apply (le_preOmega_self _).antisymm' apply enumOrd_succ_le not_bddAbove_isInitial (isInitial_natCast _) (IH.trans_lt _) rw [Nat.cast_lt] exact lt_succ n @[simp] theorem preOmega_ofNat (n : ℕ) [n.AtLeastTwo] : preOmega ofNat(n) = n := preOmega_natCast n theorem preOmega_le_of_forall_lt {o a : Ordinal} (ha : IsInitial a) (H : ∀ b < o, preOmega b < a) : preOmega o ≤ a := enumOrd_le_of_forall_lt ha H theorem isNormal_preOmega : IsNormal preOmega := by rw [isNormal_iff_strictMono_limit] refine ⟨preOmega_strictMono, fun o ho a ha ↦ (preOmega_le_of_forall_lt (isInitial_ord _) fun b hb ↦ ?_).trans (ord_card_le a)⟩ rw [← (isInitial_ord _).card_lt_card, card_ord] apply lt_of_lt_of_le _ (card_le_card <| ha _ (ho.succ_lt hb)) rw [(isInitial_preOmega _).card_lt_card, preOmega_lt_preOmega] exact lt_succ b @[simp] theorem range_preOmega : range preOmega = {x | IsInitial x} := range_enumOrd not_bddAbove_isInitial theorem mem_range_preOmega_iff {x : Ordinal} : x ∈ range preOmega ↔ IsInitial x := by rw [range_preOmega, mem_setOf] alias ⟨_, IsInitial.mem_range_preOmega⟩ := mem_range_preOmega_iff @[simp] theorem preOmega_omega0 : preOmega ω = ω := by simp_rw [← isNormal_preOmega.apply_omega0, preOmega_natCast, iSup_natCast] @[simp] theorem omega0_le_preOmega_iff {x : Ordinal} : ω ≤ preOmega x ↔ ω ≤ x := by conv_lhs => rw [← preOmega_omega0, preOmega_le_preOmega] @[simp] theorem omega0_lt_preOmega_iff {x : Ordinal} : ω < preOmega x ↔ ω < x := by conv_lhs => rw [← preOmega_omega0, preOmega_lt_preOmega] /-- The `omega` function gives the infinite initial ordinals listed by their ordinal index. `omega 0 = ω`, `omega 1 = ω₁` is the first uncountable ordinal, and so on. This is not to be confused with the first infinite ordinal `Ordinal.omega0`. For a version including finite ordinals, see `Ordinal.preOmega`. -/ def omega : Ordinal ↪o Ordinal := (OrderEmbedding.addLeft ω).trans preOmega @[inherit_doc] scoped notation "ω_ " => omega /-- `ω₁` is the first uncountable ordinal. -/ scoped notation "ω₁" => ω_ 1 theorem omega_eq_preOmega (o : Ordinal) : ω_ o = preOmega (ω + o) := rfl theorem omega_strictMono : StrictMono omega := omega.strictMono theorem omega_lt_omega {o₁ o₂ : Ordinal} : ω_ o₁ < ω_ o₂ ↔ o₁ < o₂ := omega.lt_iff_lt theorem omega_le_omega {o₁ o₂ : Ordinal} : ω_ o₁ ≤ ω_ o₂ ↔ o₁ ≤ o₂ := omega.le_iff_le theorem omega_max (o₁ o₂ : Ordinal) : ω_ (max o₁ o₂) = max (ω_ o₁) (ω_ o₂) := omega.monotone.map_max theorem preOmega_le_omega (o : Ordinal) : preOmega o ≤ ω_ o := preOmega_le_preOmega.2 (Ordinal.le_add_left _ _) theorem isInitial_omega (o : Ordinal) : IsInitial (omega o) := isInitial_preOmega _ theorem le_omega_self (o : Ordinal) : o ≤ omega o := omega_strictMono.le_apply @[simp] theorem omega_zero : ω_ 0 = ω := by rw [omega_eq_preOmega, add_zero, preOmega_omega0] theorem omega0_le_omega (o : Ordinal) : ω ≤ ω_ o := by rw [← omega_zero, omega_le_omega] exact Ordinal.zero_le o /-- For the theorem `0 < ω`, see `omega0_pos`. -/ theorem omega_pos (o : Ordinal) : 0 < ω_ o := omega0_pos.trans_le (omega0_le_omega o) theorem omega0_lt_omega1 : ω < ω₁ := by rw [← omega_zero, omega_lt_omega] exact zero_lt_one theorem isNormal_omega : IsNormal omega := isNormal_preOmega.trans (isNormal_add_right _) @[simp] theorem range_omega : range omega = {x | ω ≤ x ∧ IsInitial x} := by ext x constructor · rintro ⟨a, rfl⟩ exact ⟨omega0_le_omega a, isInitial_omega a⟩ · rintro ⟨ha', ha⟩ obtain ⟨a, rfl⟩ := ha.mem_range_preOmega use a - ω rw [omega0_le_preOmega_iff] at ha' rw [omega_eq_preOmega, Ordinal.add_sub_cancel_of_le ha'] theorem mem_range_omega_iff {x : Ordinal} : x ∈ range omega ↔ ω ≤ x ∧ IsInitial x := by rw [range_omega, mem_setOf] end Ordinal /-! ### Aleph cardinals -/ namespace Cardinal /-- The "pre-aleph" function gives the cardinals listed by their ordinal index. `preAleph n = n`, `preAleph ω = ℵ₀`, `preAleph (ω + 1) = succ ℵ₀`, etc. For the more common aleph function skipping over finite cardinals, see `Cardinal.aleph`. -/ def preAleph : Ordinal.{u} ≃o Cardinal.{u} := (enumOrdOrderIso _ not_bddAbove_isInitial).trans isInitialIso @[simp] theorem _root_.Ordinal.card_preOmega (o : Ordinal) : (preOmega o).card = preAleph o := rfl @[simp] theorem ord_preAleph (o : Ordinal) : (preAleph o).ord = preOmega o := by rw [← o.card_preOmega, (isInitial_preOmega o).ord_card] @[simp] theorem type_cardinal : typeLT Cardinal = Ordinal.univ.{u, u + 1} := by rw [Ordinal.univ_id] exact Quotient.sound ⟨preAleph.symm.toRelIsoLT⟩ @[simp] theorem mk_cardinal : #Cardinal = univ.{u, u + 1} := by simpa only [card_type, card_univ] using congr_arg card type_cardinal theorem preAleph_lt_preAleph {o₁ o₂ : Ordinal} : preAleph o₁ < preAleph o₂ ↔ o₁ < o₂ := preAleph.lt_iff_lt theorem preAleph_le_preAleph {o₁ o₂ : Ordinal} : preAleph o₁ ≤ preAleph o₂ ↔ o₁ ≤ o₂ := preAleph.le_iff_le theorem preAleph_max (o₁ o₂ : Ordinal) : preAleph (max o₁ o₂) = max (preAleph o₁) (preAleph o₂) := preAleph.monotone.map_max @[simp] theorem preAleph_zero : preAleph 0 = 0 := preAleph.map_bot @[simp] theorem preAleph_succ (o : Ordinal) : preAleph (succ o) = succ (preAleph o) := preAleph.map_succ o @[simp] theorem preAleph_nat (n : ℕ) : preAleph n = n := by rw [← card_preOmega, preOmega_natCast, card_nat] @[simp] theorem preAleph_omega0 : preAleph ω = ℵ₀ := by rw [← card_preOmega, preOmega_omega0, card_omega0] @[simp] theorem preAleph_pos {o : Ordinal} : 0 < preAleph o ↔ 0 < o := by rw [← preAleph_zero, preAleph_lt_preAleph] @[simp] theorem aleph0_le_preAleph {o : Ordinal} : ℵ₀ ≤ preAleph o ↔ ω ≤ o := by rw [← preAleph_omega0, preAleph_le_preAleph] @[simp] theorem lift_preAleph (o : Ordinal.{u}) : lift.{v} (preAleph o) = preAleph (Ordinal.lift.{v} o) := (preAleph.toInitialSeg.trans liftInitialSeg).eq (Ordinal.liftInitialSeg.trans preAleph.toInitialSeg) o @[simp] theorem _root_.Ordinal.lift_preOmega (o : Ordinal.{u}) : Ordinal.lift.{v} (preOmega o) = preOmega (Ordinal.lift.{v} o) := by rw [← ord_preAleph, lift_ord, lift_preAleph, ord_preAleph] theorem isNormal_preAleph : Order.IsNormal preAleph := OrderIso.isNormal _ theorem preAleph_le_of_isSuccPrelimit {o : Ordinal} (l : IsSuccPrelimit o) {c} : preAleph o ≤ c ↔ ∀ o' < o, preAleph o' ≤ c := by obtain rfl | ho := eq_or_ne o 0 · simp · exact isNormal_preAleph.le_iff_forall_le ⟨by simpa, l⟩ @[deprecated (since := "2025-07-08")] alias preAleph_le_of_isLimit := preAleph_le_of_isSuccPrelimit theorem preAleph_limit {o : Ordinal} (ho : IsSuccPrelimit o) : preAleph o = ⨆ a : Iio o, preAleph a := by obtain rfl | h := eq_or_ne o 0 · simp · exact isNormal_preAleph.apply_of_isSuccLimit ⟨by simpa, ho⟩ theorem preAleph_le_of_strictMono {f : Ordinal → Cardinal} (hf : StrictMono f) (o : Ordinal) : preAleph o ≤ f o := by simpa using (hf.comp preAleph.symm.strictMono).id_le (preAleph o) /-- The `aleph` function gives the infinite cardinals listed by their ordinal index. `aleph 0 = ℵ₀`, `aleph 1 = succ ℵ₀` is the first uncountable cardinal, and so on. For a version including finite cardinals, see `Cardinal.preAleph`. -/ def aleph : Ordinal ↪o Cardinal := (OrderEmbedding.addLeft ω).trans preAleph.toOrderEmbedding @[inherit_doc] scoped notation "ℵ_ " => aleph /-- `ℵ₁` is the first uncountable cardinal. -/ scoped notation "ℵ₁" => ℵ_ 1 theorem aleph_eq_preAleph (o : Ordinal) : ℵ_ o = preAleph (ω + o) := rfl @[simp] theorem _root_.Ordinal.card_omega (o : Ordinal) : (ω_ o).card = ℵ_ o := rfl @[simp] theorem ord_aleph (o : Ordinal) : (ℵ_ o).ord = ω_ o := ord_preAleph _ theorem aleph_lt_aleph {o₁ o₂ : Ordinal} : ℵ_ o₁ < ℵ_ o₂ ↔ o₁ < o₂ := aleph.lt_iff_lt theorem aleph_le_aleph {o₁ o₂ : Ordinal} : ℵ_ o₁ ≤ ℵ_ o₂ ↔ o₁ ≤ o₂ := aleph.le_iff_le theorem aleph_max (o₁ o₂ : Ordinal) : ℵ_ (max o₁ o₂) = max (ℵ_ o₁) (ℵ_ o₂) := aleph.monotone.map_max theorem preAleph_le_aleph (o : Ordinal) : preAleph o ≤ ℵ_ o := preAleph_le_preAleph.2 (Ordinal.le_add_left _ _) @[simp] theorem aleph_succ (o : Ordinal) : ℵ_ (succ o) = succ (ℵ_ o) := by rw [aleph_eq_preAleph, add_succ, preAleph_succ, aleph_eq_preAleph] @[simp] theorem aleph_zero : ℵ_ 0 = ℵ₀ := by rw [aleph_eq_preAleph, add_zero, preAleph_omega0] @[simp] theorem lift_aleph (o : Ordinal.{u}) : lift.{v} (aleph o) = aleph (Ordinal.lift.{v} o) := by simp [aleph_eq_preAleph] /-- For the theorem `lift ω = ω`, see `lift_omega0`. -/ @[simp] theorem _root_.Ordinal.lift_omega (o : Ordinal.{u}) : Ordinal.lift.{v} (ω_ o) = ω_ (Ordinal.lift.{v} o) := by simp [omega_eq_preOmega] theorem isNormal_aleph : Order.IsNormal aleph := isNormal_preAleph.comp (isNormal_add_right _) theorem aleph_limit {o : Ordinal} (ho : IsSuccLimit o) : ℵ_ o = ⨆ a : Iio o, ℵ_ a := isNormal_aleph.apply_of_isSuccLimit ho theorem aleph0_le_aleph (o : Ordinal) : ℵ₀ ≤ ℵ_ o := by rw [aleph_eq_preAleph, aleph0_le_preAleph] apply Ordinal.le_add_right theorem aleph_pos (o : Ordinal) : 0 < ℵ_ o := aleph0_pos.trans_le (aleph0_le_aleph o) @[simp] theorem aleph_toNat (o : Ordinal) : toNat (ℵ_ o) = 0 := toNat_apply_of_aleph0_le <| aleph0_le_aleph o @[simp] theorem aleph_toENat (o : Ordinal) : toENat (ℵ_ o) = ⊤ := (toENat_eq_top.2 (aleph0_le_aleph o)) theorem isSuccLimit_omega (o : Ordinal) : IsSuccLimit (ω_ o) := by rw [← ord_aleph] exact isSuccLimit_ord (aleph0_le_aleph _) @[deprecated (since := "2025-07-08")] alias isLimit_omega := isSuccLimit_omega @[simp] theorem range_aleph : range aleph = Set.Ici ℵ₀ := by ext c refine ⟨fun ⟨o, e⟩ => e ▸ aleph0_le_aleph _, fun hc ↦ ⟨preAleph.symm c - ω, ?_⟩⟩ rw [aleph_eq_preAleph, Ordinal.add_sub_cancel_of_le, preAleph.apply_symm_apply] rwa [← aleph0_le_preAleph, preAleph.apply_symm_apply] theorem mem_range_aleph_iff {c : Cardinal} : c ∈ range aleph ↔ ℵ₀ ≤ c := by rw [range_aleph, mem_Ici] @[simp] theorem succ_aleph0 : succ ℵ₀ = ℵ₁ := by rw [← aleph_zero, ← aleph_succ, Ordinal.succ_zero] theorem aleph0_lt_aleph_one : ℵ₀ < ℵ₁ := by rw [← succ_aleph0] apply lt_succ theorem countable_iff_lt_aleph_one {α : Type*} (s : Set α) : s.Countable ↔ #s < ℵ₁ := by rw [← succ_aleph0, lt_succ_iff, le_aleph0_iff_set_countable] @[simp] theorem aleph1_le_lift {c : Cardinal.{u}} : ℵ₁ ≤ lift.{v} c ↔ ℵ₁ ≤ c := by simpa using lift_le (a := ℵ₁) @[simp] theorem lift_le_aleph1 {c : Cardinal.{u}} : lift.{v} c ≤ ℵ₁ ↔ c ≤ ℵ₁ := by simpa using lift_le (b := ℵ₁) @[simp] theorem aleph1_lt_lift {c : Cardinal.{u}} : ℵ₁ < lift.{v} c ↔ ℵ₁ < c := by simpa using lift_lt (a := ℵ₁) @[simp] theorem lift_lt_aleph1 {c : Cardinal.{u}} : lift.{v} c < ℵ₁ ↔ c < ℵ₁ := by simpa using lift_lt (b := ℵ₁) @[simp] theorem aleph1_eq_lift {c : Cardinal.{u}} : ℵ₁ = lift.{v} c ↔ ℵ₁ = c := by simpa using lift_inj (a := ℵ₁) @[simp] theorem lift_eq_aleph1 {c : Cardinal.{u}} : lift.{v} c = ℵ₁ ↔ c = ℵ₁ := by simpa using lift_inj (b := ℵ₁) theorem lt_omega_iff_card_lt {x o : Ordinal} : x < ω_ o ↔ x.card < ℵ_ o := by rw [← (isInitial_omega o).card_lt_card, card_omega] /-! ### Beth cardinals -/ /-- The "pre-beth" function is defined so that `preBeth o` is the supremum of `2 ^ preBeth a` for `a < o`. This implies `beth 0 = 0`, `beth (succ o) = 2 ^ beth o`, and that for a limit ordinal `o`, `beth o` is the supremum of `beth a` for `a < o`. For the usual function starting at `ℵ₀`, see `Cardinal.beth`. -/ def preBeth (o : Ordinal.{u}) : Cardinal.{u} := ⨆ a : Iio o, 2 ^ preBeth a termination_by o decreasing_by exact a.2 theorem preBeth_strictMono : StrictMono preBeth := by intro a b h conv_rhs => rw [preBeth] rw [lt_ciSup_iff' (bddAbove_of_small _)] exact ⟨⟨a, h⟩, cantor _⟩ theorem preBeth_mono : Monotone preBeth := preBeth_strictMono.monotone theorem preAleph_le_preBeth (o : Ordinal) : preAleph o ≤ preBeth o := preAleph_le_of_strictMono preBeth_strictMono o @[simp] theorem preBeth_lt_preBeth {o₁ o₂ : Ordinal} : preBeth o₁ < preBeth o₂ ↔ o₁ < o₂ := preBeth_strictMono.lt_iff_lt @[simp] theorem preBeth_le_preBeth {o₁ o₂ : Ordinal} : preBeth o₁ ≤ preBeth o₂ ↔ o₁ ≤ o₂ := preBeth_strictMono.le_iff_le @[simp] theorem preBeth_inj {o₁ o₂ : Ordinal} : preBeth o₁ = preBeth o₂ ↔ o₁ = o₂ := preBeth_strictMono.injective.eq_iff @[simp] theorem preBeth_zero : preBeth 0 = 0 := by rw [preBeth] simp @[simp] theorem preBeth_succ (o : Ordinal) : preBeth (succ o) = 2 ^ preBeth o := by rw [preBeth, Iio_succ] exact ciSup_Iic o fun x y h ↦ power_le_power_left two_ne_zero (preBeth_mono h) theorem preBeth_limit {o : Ordinal} (ho : IsSuccPrelimit o) : preBeth o = ⨆ a : Iio o, preBeth a := by rw [preBeth] apply (ciSup_mono (bddAbove_of_small _) fun _ ↦ (cantor _).le).antisymm' rw [ciSup_le_iff' (bddAbove_of_small _)] intro a rw [← preBeth_succ] exact le_ciSup (bddAbove_of_small _) (⟨_, ho.succ_lt a.2⟩ : Iio o) theorem isNormal_preBeth : Order.IsNormal preBeth := by rw [isNormal_iff] refine ⟨preBeth_strictMono, fun o ho ↦ ?_⟩ simp [preBeth_limit ho.isSuccPrelimit, ciSup_le_iff' (bddAbove_of_small _)] theorem preBeth_nat : ∀ n : ℕ, preBeth n = (2 ^ ·)^[n] (0 : ℕ) | 0 => by simp | n + 1 => by rw [natCast_succ, preBeth_succ, Function.iterate_succ_apply', preBeth_nat] simp @[simp] theorem preBeth_one : preBeth 1 = 1 := by simpa using preBeth_nat 1 @[simp] theorem preBeth_omega : preBeth ω = ℵ₀ := by apply le_antisymm · rw [preBeth_limit isSuccLimit_omega0.isSuccPrelimit, ciSup_le_iff' (bddAbove_of_small _)] rintro ⟨a, ha⟩ obtain ⟨n, rfl⟩ := lt_omega0.1 ha rw [preBeth_nat] exact (nat_lt_aleph0 _).le · simpa using preAleph_le_preBeth ω @[simp] theorem preBeth_pos {o : Ordinal} : 0 < preBeth o ↔ 0 < o := by simpa using preBeth_lt_preBeth (o₁ := 0) @[simp] theorem preBeth_eq_zero {o : Ordinal} : preBeth o = 0 ↔ o = 0 := by simpa using preBeth_inj (o₂ := 0) theorem isStrongLimit_preBeth {o : Ordinal} : IsStrongLimit (preBeth o) ↔ IsSuccLimit o := by by_cases H : IsSuccLimit o · refine iff_of_true ⟨by simpa using H.ne_bot, fun a ha ↦ ?_⟩ H rw [preBeth_limit H.isSuccPrelimit] at ha rcases exists_lt_of_lt_ciSup' ha with ⟨⟨i, hi⟩, ha⟩ have := power_le_power_left two_ne_zero ha.le rw [← preBeth_succ] at this exact this.trans_lt (preBeth_strictMono (H.succ_lt hi)) · apply iff_of_false _ H rw [not_isSuccLimit_iff, not_isSuccPrelimit_iff'] at H obtain ho | ⟨a, rfl⟩ := H · simp [ho.eq_bot] · intro h simpa using h.two_power_lt (preBeth_strictMono (lt_succ a)) /-- The Beth function is defined so that `beth 0 = ℵ₀'`, `beth (succ o) = 2 ^ beth o`, and that for a limit ordinal `o`, `beth o` is the supremum of `beth a` for `a < o`. Assuming the generalized continuum hypothesis, which is undecidable in ZFC, we have `ℶ_ o = ℵ_ o` for all ordinals. For a version which starts at zero, see `Cardinal.preBeth`. -/ def beth (o : Ordinal.{u}) : Cardinal.{u} := preBeth (ω + o) @[inherit_doc] scoped notation "ℶ_ " => beth theorem beth_eq_preBeth (o : Ordinal) : beth o = preBeth (ω + o) := rfl theorem preBeth_le_beth (o : Ordinal) : preBeth o ≤ ℶ_ o := preBeth_le_preBeth.2 (Ordinal.le_add_left _ _) theorem beth_strictMono : StrictMono beth := preBeth_strictMono.comp fun _ _ h ↦ by gcongr theorem beth_mono : Monotone beth := beth_strictMono.monotone @[simp] theorem beth_lt_beth {o₁ o₂ : Ordinal} : ℶ_ o₁ < ℶ_ o₂ ↔ o₁ < o₂ := beth_strictMono.lt_iff_lt @[simp] theorem beth_le_beth {o₁ o₂ : Ordinal} : ℶ_ o₁ ≤ ℶ_ o₂ ↔ o₁ ≤ o₂ := beth_strictMono.le_iff_le @[simp] theorem beth_zero : ℶ_ 0 = ℵ₀ := by simp [beth] @[simp] theorem beth_succ (o : Ordinal) : ℶ_ (succ o) = 2 ^ ℶ_ o := by simp [beth, add_succ] theorem isNormal_beth : Order.IsNormal beth := isNormal_preBeth.comp (isNormal_add_right _) theorem beth_limit {o : Ordinal} (ho : IsSuccLimit o) : ℶ_ o = ⨆ a : Iio o, ℶ_ a := isNormal_beth.apply_of_isSuccLimit ho theorem aleph_le_beth (o : Ordinal) : ℵ_ o ≤ ℶ_ o := preAleph_le_preBeth _ theorem aleph0_le_beth (o : Ordinal) : ℵ₀ ≤ ℶ_ o := (aleph0_le_aleph o).trans <| aleph_le_beth o theorem beth_pos (o : Ordinal) : 0 < ℶ_ o := aleph0_pos.trans_le <| aleph0_le_beth o theorem beth_ne_zero (o : Ordinal) : ℶ_ o ≠ 0 := (beth_pos o).ne' theorem isStrongLimit_beth {o : Ordinal} : IsStrongLimit (ℶ_ o) ↔ IsSuccPrelimit o := by rw [beth_eq_preBeth, isStrongLimit_preBeth, isSuccLimit_add_iff_of_isSuccLimit isSuccLimit_omega0] end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/SchroederBernstein.lean
import Mathlib.Data.Set.Piecewise import Mathlib.Order.FixedPoints import Mathlib.Order.Zorn /-! # Schröder-Bernstein theorem, well-ordering of cardinals This file proves the Schröder-Bernstein theorem (see `schroeder_bernstein`), the well-ordering of cardinals (see `min_injective`) and the totality of their order (see `total`). ## Notes Cardinals are naturally ordered by `α ≤ β ↔ ∃ f : a → β, Injective f`: * `schroeder_bernstein` states that, given injections `α → β` and `β → α`, one can get a bijection `α → β`. This corresponds to the antisymmetry of the order. * The order is also well-founded: any nonempty set of cardinals has a minimal element. `min_injective` states that by saying that there exists an element of the set that injects into all others. Cardinals are defined and further developed in the folder `SetTheory.Cardinal`. -/ open Set Function universe u v namespace Function namespace Embedding section antisymm variable {α : Type u} {β : Type v} /-- **The Schröder-Bernstein Theorem**: Given injections `α → β` and `β → α` that satisfy a pointwise property `R`, we can get a bijection `α → β` that satisfies that same pointwise property. -/ theorem schroeder_bernstein_of_rel {f : α → β} {g : β → α} (hf : Function.Injective f) (hg : Function.Injective g) (R : α → β → Prop) (hp₁ : ∀ a : α, R a (f a)) (hp₂ : ∀ b : β, R (g b) b) : ∃ h : α → β, Bijective h ∧ ∀ a : α, R a (h a) := by classical rcases isEmpty_or_nonempty β with hβ | hβ · have : IsEmpty α := Function.isEmpty f exact ⟨_, ((Equiv.equivEmpty α).trans (Equiv.equivEmpty β).symm).bijective, by simp⟩ set F : Set α →o Set α := { toFun := fun s => (g '' (f '' s)ᶜ)ᶜ monotone' := fun s t hst => by dsimp at hst ⊢; gcongr } set s : Set α := F.lfp have hs : (g '' (f '' s)ᶜ)ᶜ = s := F.map_lfp have hns : g '' (f '' s)ᶜ = sᶜ := compl_injective (by simp [hs]) set g' := invFun g have g'g : LeftInverse g' g := leftInverse_invFun hg have hg'ns : g' '' sᶜ = (f '' s)ᶜ := by rw [← hns, g'g.image_image] set h : α → β := s.piecewise f g' have : Surjective h := by rw [← range_eq_univ, range_piecewise, hg'ns, union_compl_self] have : Injective h := by refine (injective_piecewise_iff _).2 ⟨hf.injOn, ?_, ?_⟩ · intro x hx y hy hxy obtain ⟨x', _, rfl⟩ : x ∈ g '' (f '' s)ᶜ := by rwa [hns] obtain ⟨y', _, rfl⟩ : y ∈ g '' (f '' s)ᶜ := by rwa [hns] rw [g'g _, g'g _] at hxy rw [hxy] · intro x hx y hy hxy obtain ⟨y', hy', rfl⟩ : y ∈ g '' (f '' s)ᶜ := by rwa [hns] rw [g'g _] at hxy exact hy' ⟨x, hx, hxy⟩ refine ⟨h, ⟨‹Injective h›, ‹Surjective h›⟩, fun a ↦ ?_⟩ simp only [h, Set.piecewise, g'] split · exact hp₁ a · have : g (invFun g a) = a := by have : a ∈ g '' (f '' s)ᶜ := by grind obtain ⟨x, _, hx⟩ := mem_image _ _ _ |>.mp this exact Function.invFun_eq ⟨x, hx⟩ grind /-- **The Schröder-Bernstein Theorem**: Given injections `α → β` and `β → α`, we can get a bijection `α → β`. -/ theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : Function.Injective f) (hg : Function.Injective g) : ∃ h : α → β, Bijective h := by obtain ⟨f, hf, _⟩ := schroeder_bernstein_of_rel hf hg (fun x y ↦ True) (by simp) (by simp) exact ⟨f, hf⟩ /-- **The Schröder-Bernstein Theorem**: Given embeddings `α ↪ β` and `β ↪ α`, there exists an equivalence `α ≃ β`. -/ theorem antisymm : (α ↪ β) → (β ↪ α) → Nonempty (α ≃ β) | ⟨_, h₁⟩, ⟨_, h₂⟩ => let ⟨f, hf⟩ := schroeder_bernstein h₁ h₂ ⟨Equiv.ofBijective f hf⟩ end antisymm section Wo variable {ι : Type u} (β : ι → Type v) /-- `sets β` -/ private abbrev sets := { s : Set (∀ i, β i) | ∀ i : ι, s.InjOn fun x => x i } /-- The cardinals are well-ordered. We express it here by the fact that in any set of cardinals there is an element that injects into the others. See `Cardinal.conditionallyCompleteLinearOrderBot` for (one of) the lattice instances. -/ theorem min_injective [I : Nonempty ι] : ∃ i, Nonempty (∀ j, β i ↪ β j) := let ⟨s, hs⟩ := show ∃ s, Maximal (· ∈ sets β) s by refine zorn_subset _ fun c hc hcc ↦ ⟨⋃₀ c, fun i x ⟨p, hpc, hxp⟩ y ⟨q, hqc, hyq⟩ hi ↦ ?_, fun _ ↦ subset_sUnion_of_mem⟩ exact (hcc.total hpc hqc).elim (fun h ↦ hc hqc i (h hxp) hyq hi) fun h ↦ hc hpc i hxp (h hyq) hi let ⟨i, e⟩ := show ∃ i, Surjective fun x : s => x.val i from Classical.by_contradiction fun h => have h : ∀ i, ∃ y, ∀ x ∈ s, (x : ∀ i, β i) i ≠ y := by simpa [Surjective] using h let ⟨f, hf⟩ := Classical.axiom_of_choice h have : f ∈ s := have : insert f s ∈ sets β := fun i x hx y hy => by rcases hx with hx | hx <;> rcases hy with hy | hy; · simp [hx, hy] · subst x exact fun e => (hf i y hy e.symm).elim · subst y exact fun e => (hf i x hx e).elim · exact hs.prop i hx hy hs.eq_of_subset this (subset_insert _ _) ▸ mem_insert .. let ⟨i⟩ := I hf i f this rfl ⟨i, ⟨fun j => ⟨s.restrict (fun x => x j) ∘ surjInv e, ((hs.1 j).injective).comp (injective_surjInv _)⟩⟩⟩ end Wo /-- The cardinals are totally ordered. See `Cardinal.conditionallyCompleteLinearOrderBot` for (one of) the lattice instance. -/ theorem total (α : Type u) (β : Type v) : Nonempty (α ↪ β) ∨ Nonempty (β ↪ α) := match @min_injective Bool (fun b => cond b (ULift α) (ULift.{max u v, v} β)) ⟨true⟩ with | ⟨true, ⟨h⟩⟩ => let ⟨f, hf⟩ := h false Or.inl ⟨Embedding.congr Equiv.ulift Equiv.ulift ⟨f, hf⟩⟩ | ⟨false, ⟨h⟩⟩ => let ⟨f, hf⟩ := h true Or.inr ⟨Embedding.congr Equiv.ulift Equiv.ulift ⟨f, hf⟩⟩ end Embedding end Function
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Subfield.lean
import Mathlib.Algebra.Field.Subfield.Basic import Mathlib.Data.W.Cardinal import Mathlib.Tactic.FinCases /-! # Cardinality of the division ring generated by a set `Subfield.cardinalMk_closure_le_max`: the cardinality of the (sub-)division ring generated by a set is bounded by the cardinality of the set unless it is finite. The method used to prove this (via `WType`) can be easily generalized to other algebraic structures, but those cardinalities can usually be obtained by other means, using some explicit universal objects. -/ universe u variable {α : Type u} (s : Set α) namespace Subfield private abbrev Operands : Fin 6 ⊕ s → Type | .inl 0 => Bool -- add | .inl 1 => Bool -- mul | .inl 2 => Unit -- neg | .inl 3 => Unit -- inv | .inl 4 => Empty -- zero | .inl 5 => Empty -- one | .inr _ => Empty -- s variable [DivisionRing α] private def operate : (Σ n, Operands s n → closure s) → closure s | ⟨.inl 0, f⟩ => f false + f true | ⟨.inl 1, f⟩ => f false * f true | ⟨.inl 2, f⟩ => - f () | ⟨.inl 3, f⟩ => (f ())⁻¹ | ⟨.inl 4, _⟩ => 0 | ⟨.inl 5, _⟩ => 1 | ⟨.inr a, _⟩ => ⟨a, subset_closure a.prop⟩ private def rangeOfWType : Subfield (closure s) where carrier := Set.range (WType.elim _ <| operate s) add_mem' := by rintro _ _ ⟨x, rfl⟩ ⟨y, rfl⟩; exact ⟨WType.mk (.inl 0) (Bool.rec x y), by rfl⟩ mul_mem' := by rintro _ _ ⟨x, rfl⟩ ⟨y, rfl⟩; exact ⟨WType.mk (.inl 1) (Bool.rec x y), by rfl⟩ neg_mem' := by rintro _ ⟨x, rfl⟩; exact ⟨WType.mk (.inl 2) fun _ ↦ x, rfl⟩ inv_mem' := by rintro _ ⟨x, rfl⟩; exact ⟨WType.mk (.inl 3) fun _ ↦ x, rfl⟩ zero_mem' := ⟨WType.mk (.inl 4) Empty.rec, rfl⟩ one_mem' := ⟨WType.mk (.inl 5) Empty.rec, rfl⟩ private lemma rangeOfWType_eq_top : rangeOfWType s = ⊤ := top_le_iff.mp fun a _ ↦ by rw [← SetLike.mem_coe, ← Subtype.val_injective.mem_set_image] change ↑a ∈ map (closure s).subtype _ refine closure_le.mpr (fun a ha ↦ ?_) a.prop exact ⟨⟨a, subset_closure ha⟩, ⟨WType.mk (.inr ⟨a, ha⟩) Empty.rec, rfl⟩, rfl⟩ private lemma surjective_ofWType : Function.Surjective (WType.elim _ <| operate s) := by rw [← Set.range_eq_univ] exact SetLike.coe_set_eq.mpr (rangeOfWType_eq_top s) open Cardinal lemma cardinalMk_closure_le_max : #(closure s) ≤ max #s ℵ₀ := (Cardinal.mk_le_of_surjective <| surjective_ofWType s).trans <| by convert WType.cardinalMk_le_max_aleph0_of_finite' using 1 · rw [lift_uzero, mk_sum, lift_uzero] have : lift.{u,0} #(Fin 6) < ℵ₀ := lift_lt_aleph0.mpr (lt_aleph0_of_finite _) obtain h|h := lt_or_ge #s ℵ₀ · rw [max_eq_right h.le, max_eq_right] exact (add_lt_aleph0 this h).le · rw [max_eq_left h, add_eq_right h (this.le.trans h), max_eq_left h] rintro (n|_) · fin_cases n <;> (dsimp only [id_eq]; infer_instance) infer_instance lemma cardinalMk_closure [Infinite s] : #(closure s) = #s := ((cardinalMk_closure_le_max s).trans_eq <| max_eq_left <| aleph0_le_mk s).antisymm (mk_le_mk_of_subset subset_closure) end Subfield
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Continuum.lean
import Mathlib.SetTheory.Cardinal.Arithmetic /-! # Cardinality of continuum In this file we define `Cardinal.continuum` (notation: `𝔠`, localized in `Cardinal`) to be `2 ^ ℵ₀`. We also prove some `simp` lemmas about cardinal arithmetic involving `𝔠`. ## Notation - `𝔠` : notation for `Cardinal.continuum` in scope `Cardinal`. -/ namespace Cardinal universe u v open Cardinal /-- Cardinality of the continuum. -/ def continuum : Cardinal.{u} := 2 ^ ℵ₀ @[inherit_doc] scoped notation "𝔠" => Cardinal.continuum @[simp] theorem two_power_aleph0 : 2 ^ ℵ₀ = 𝔠 := rfl @[simp] theorem lift_continuum : lift.{v} 𝔠 = 𝔠 := by rw [← two_power_aleph0, lift_two_power, lift_aleph0, two_power_aleph0] @[simp] theorem continuum_le_lift {c : Cardinal.{u}} : 𝔠 ≤ lift.{v} c ↔ 𝔠 ≤ c := by rw [← lift_continuum.{v, u}, lift_le] @[simp] theorem lift_le_continuum {c : Cardinal.{u}} : lift.{v} c ≤ 𝔠 ↔ c ≤ 𝔠 := by rw [← lift_continuum.{v, u}, lift_le] @[simp] theorem continuum_lt_lift {c : Cardinal.{u}} : 𝔠 < lift.{v} c ↔ 𝔠 < c := by rw [← lift_continuum.{v, u}, lift_lt] @[simp] theorem lift_lt_continuum {c : Cardinal.{u}} : lift.{v} c < 𝔠 ↔ c < 𝔠 := by rw [← lift_continuum.{v, u}, lift_lt] /-! ### Inequalities -/ theorem aleph0_lt_continuum : ℵ₀ < 𝔠 := cantor ℵ₀ theorem aleph0_le_continuum : ℵ₀ ≤ 𝔠 := aleph0_lt_continuum.le @[simp] theorem beth_one : ℶ_ 1 = 𝔠 := by simpa using beth_succ 0 theorem nat_lt_continuum (n : ℕ) : ↑n < 𝔠 := (nat_lt_aleph0 n).trans aleph0_lt_continuum theorem mk_set_nat : #(Set ℕ) = 𝔠 := by simp theorem continuum_pos : 0 < 𝔠 := nat_lt_continuum 0 theorem continuum_ne_zero : 𝔠 ≠ 0 := continuum_pos.ne' theorem aleph_one_le_continuum : ℵ₁ ≤ 𝔠 := by rw [← succ_aleph0] exact Order.succ_le_of_lt aleph0_lt_continuum @[simp] theorem continuum_toNat : toNat continuum = 0 := toNat_apply_of_aleph0_le aleph0_le_continuum @[simp] theorem continuum_toENat : toENat continuum = ⊤ := (toENat_eq_top.2 aleph0_le_continuum) /-! ### Addition -/ @[simp] theorem aleph0_add_continuum : ℵ₀ + 𝔠 = 𝔠 := add_eq_right aleph0_le_continuum aleph0_le_continuum @[simp] theorem continuum_add_aleph0 : 𝔠 + ℵ₀ = 𝔠 := (add_comm _ _).trans aleph0_add_continuum @[simp] theorem continuum_add_self : 𝔠 + 𝔠 = 𝔠 := add_eq_self aleph0_le_continuum @[simp] theorem nat_add_continuum (n : ℕ) : ↑n + 𝔠 = 𝔠 := nat_add_eq n aleph0_le_continuum @[simp] theorem continuum_add_nat (n : ℕ) : 𝔠 + n = 𝔠 := (add_comm _ _).trans (nat_add_continuum n) @[simp] theorem ofNat_add_continuum {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) + 𝔠 = 𝔠 := nat_add_continuum n @[simp] theorem continuum_add_ofNat {n : ℕ} [Nat.AtLeastTwo n] : 𝔠 + ofNat(n) = 𝔠 := continuum_add_nat n /-! ### Multiplication -/ @[simp] theorem continuum_mul_self : 𝔠 * 𝔠 = 𝔠 := mul_eq_left aleph0_le_continuum le_rfl continuum_ne_zero @[simp] theorem continuum_mul_aleph0 : 𝔠 * ℵ₀ = 𝔠 := mul_eq_left aleph0_le_continuum aleph0_le_continuum aleph0_ne_zero @[simp] theorem aleph0_mul_continuum : ℵ₀ * 𝔠 = 𝔠 := (mul_comm _ _).trans continuum_mul_aleph0 @[simp] theorem nat_mul_continuum {n : ℕ} (hn : n ≠ 0) : ↑n * 𝔠 = 𝔠 := mul_eq_right aleph0_le_continuum (nat_lt_continuum n).le (Nat.cast_ne_zero.2 hn) @[simp] theorem continuum_mul_nat {n : ℕ} (hn : n ≠ 0) : 𝔠 * n = 𝔠 := (mul_comm _ _).trans (nat_mul_continuum hn) @[simp] theorem ofNat_mul_continuum {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) * 𝔠 = 𝔠 := nat_mul_continuum (OfNat.ofNat_ne_zero n) @[simp] theorem continuum_mul_ofNat {n : ℕ} [Nat.AtLeastTwo n] : 𝔠 * ofNat(n) = 𝔠 := continuum_mul_nat (OfNat.ofNat_ne_zero n) /-! ### Power -/ @[simp] theorem aleph0_power_aleph0 : ℵ₀ ^ ℵ₀ = 𝔠 := power_self_eq le_rfl @[simp] theorem nat_power_aleph0 {n : ℕ} (hn : 2 ≤ n) : n ^ ℵ₀ = 𝔠 := nat_power_eq le_rfl hn @[simp] theorem continuum_power_aleph0 : 𝔠 ^ ℵ₀ = 𝔠 := by rw [← two_power_aleph0, ← power_mul, mul_eq_left le_rfl le_rfl aleph0_ne_zero] theorem power_aleph0_of_le_continuum {x : Cardinal} (h₁ : 2 ≤ x) (h₂ : x ≤ 𝔠) : x ^ ℵ₀ = 𝔠 := by apply le_antisymm · rw [← continuum_power_aleph0] exact power_le_power_right h₂ · rw [← two_power_aleph0] exact power_le_power_right h₁ end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/UnivLE.lean
import Mathlib.Logic.UnivLE import Mathlib.SetTheory.Ordinal.Basic /-! # UnivLE and cardinals -/ noncomputable section universe u v open Cardinal theorem univLE_iff_cardinal_le : UnivLE.{u, v} ↔ univ.{u, v + 1} ≤ univ.{v, u + 1} := by rw [← not_iff_not, univLE_iff]; simp_rw [small_iff_lift_mk_lt_univ]; push_neg -- strange: simp_rw [univ_umax.{v,u}] doesn't work refine ⟨fun ⟨α, le⟩ ↦ ?_, fun h ↦ ?_⟩ · rw [univ_umax.{v, u}, ← lift_le.{u + 1}, lift_univ, lift_lift] at le exact le.trans_lt (lift_lt_univ'.{u, v + 1} #α) · obtain ⟨⟨α⟩, h⟩ := lt_univ'.mp h; use α rw [univ_umax.{v, u}, ← lift_le.{u + 1}, lift_univ, lift_lift] exact h.le theorem univLE_iff_exists_embedding : UnivLE.{u, v} ↔ Nonempty (Ordinal.{u} ↪ Ordinal.{v}) := by rw [univLE_iff_cardinal_le] exact lift_mk_le' theorem Ordinal.univLE_of_injective {f : Ordinal.{u} → Ordinal.{v}} (h : f.Injective) : UnivLE.{u, v} := univLE_iff_exists_embedding.2 ⟨f, h⟩ /-- Together with transitivity, this shows `UnivLE` is a total preorder. -/ theorem univLE_total : UnivLE.{u, v} ∨ UnivLE.{v, u} := by simp_rw [univLE_iff_cardinal_le]; apply le_total
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Order.lean
import Mathlib.Algebra.Order.GroupWithZero.Canonical import Mathlib.Algebra.Order.Ring.Canonical import Mathlib.Data.Fintype.Option import Mathlib.Order.InitialSeg import Mathlib.Order.Nat import Mathlib.Order.SuccPred.CompleteLinearOrder import Mathlib.SetTheory.Cardinal.Defs import Mathlib.SetTheory.Cardinal.SchroederBernstein /-! # Order on cardinal numbers We define the order on cardinal numbers and show its basic properties, including the ordered semiring structure. ## Main definitions * The order `c₁ ≤ c₂` is defined by `Cardinal.le_def α β : #α ≤ #β ↔ Nonempty (α ↪ β)`. * `Order.IsSuccLimit c` means that `c` is a (weak) limit cardinal: `c ≠ 0 ∧ ∀ x < c, succ x < c`. * `Cardinal.IsStrongLimit c` means that `c` is a strong limit cardinal: `c ≠ 0 ∧ ∀ x < c, 2 ^ x < c`. ## Main instances * Cardinals form a `CanonicallyOrderedAdd` `OrderedCommSemiring` with the aforementioned sum and product. * Cardinals form a `SuccOrder`. Use `Order.succ c` for the smallest cardinal greater than `c`. * The less-than relation on cardinals forms a well-order. * Cardinals form a `ConditionallyCompleteLinearOrderBot`. Bounded sets for cardinals in universe `u` are precisely the sets indexed by some type in universe `u`, see `Cardinal.bddAbove_iff_small`. One can use `sSup` for the cardinal supremum, and `sInf` for the minimum of a set of cardinals. ## Main statements * Cantor's theorem: `Cardinal.cantor c : c < 2 ^ c`. * König's theorem: `Cardinal.sum_lt_prod` ## Implementation notes The current setup interweaves the order structure and the algebraic structure on `Cardinal` tightly. For example, we need to know what a ring is in order to show that `0` is the smallest cardinality. That is reflected in this file containing both the order and algebra structure. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, aleph, Cantor's theorem, König's theorem, Konig's theorem -/ assert_not_exists Field open List Function Order Set noncomputable section universe u v w v' w' variable {α β : Type u} namespace Cardinal /-! ### Order on cardinals -/ /-- We define the order on cardinal numbers by `#α ≤ #β` if and only if there exists an embedding (injective function) from α to β. -/ instance : LE Cardinal.{u} := ⟨fun q₁ q₂ => Quotient.liftOn₂ q₁ q₂ (fun α β => Nonempty <| α ↪ β) fun _ _ _ _ ⟨e₁⟩ ⟨e₂⟩ => propext ⟨fun ⟨e⟩ => ⟨e.congr e₁ e₂⟩, fun ⟨e⟩ => ⟨e.congr e₁.symm e₂.symm⟩⟩⟩ instance partialOrder : PartialOrder Cardinal.{u} where le_refl := by rintro ⟨α⟩ exact ⟨Embedding.refl _⟩ le_trans := by rintro ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨e₁⟩ ⟨e₂⟩ exact ⟨e₁.trans e₂⟩ le_antisymm := by rintro ⟨α⟩ ⟨β⟩ ⟨e₁⟩ ⟨e₂⟩ exact Quotient.sound (e₁.antisymm e₂) instance linearOrder : LinearOrder Cardinal.{u} := { Cardinal.partialOrder with le_total := by rintro ⟨α⟩ ⟨β⟩ apply Embedding.total toDecidableLE := Classical.decRel _ } theorem le_def (α β : Type u) : #α ≤ #β ↔ Nonempty (α ↪ β) := Iff.rfl theorem mk_le_of_injective {α β : Type u} {f : α → β} (hf : Injective f) : #α ≤ #β := ⟨⟨f, hf⟩⟩ theorem _root_.Function.Embedding.cardinal_le {α β : Type u} (f : α ↪ β) : #α ≤ #β := ⟨f⟩ theorem mk_le_of_surjective {α β : Type u} {f : α → β} (hf : Surjective f) : #β ≤ #α := ⟨Embedding.ofSurjective f hf⟩ theorem le_mk_iff_exists_set {c : Cardinal} {α : Type u} : c ≤ #α ↔ ∃ p : Set α, #p = c := ⟨inductionOn c fun _ ⟨⟨f, hf⟩⟩ => ⟨Set.range f, (Equiv.ofInjective f hf).cardinal_eq.symm⟩, fun ⟨_, e⟩ => e ▸ ⟨⟨Subtype.val, fun _ _ => Subtype.eq⟩⟩⟩ theorem mk_subtype_le {α : Type u} (p : α → Prop) : #(Subtype p) ≤ #α := ⟨Embedding.subtype p⟩ theorem mk_set_le (s : Set α) : #s ≤ #α := mk_subtype_le s theorem out_embedding {c c' : Cardinal} : c ≤ c' ↔ Nonempty (c.out ↪ c'.out) := by conv_lhs => rw [← Cardinal.mk_out c, ← Cardinal.mk_out c', le_def] theorem lift_mk_le {α : Type v} {β : Type w} : lift.{max u w} #α ≤ lift.{max u v} #β ↔ Nonempty (α ↪ β) := ⟨fun ⟨f⟩ => ⟨Embedding.congr Equiv.ulift Equiv.ulift f⟩, fun ⟨f⟩ => ⟨Embedding.congr Equiv.ulift.symm Equiv.ulift.symm f⟩⟩ /-- A variant of `Cardinal.lift_mk_le` with specialized universes. Because Lean often cannot realize it should use this specialization itself, we provide this statement separately so you don't have to solve the specialization problem either. -/ theorem lift_mk_le' {α : Type u} {β : Type v} : lift.{v} #α ≤ lift.{u} #β ↔ Nonempty (α ↪ β) := lift_mk_le.{0} /-! ### `lift` sends `Cardinal.{u}` to an initial segment of `Cardinal.{max u v}`. -/ /-- `Cardinal.lift` as an `InitialSeg`. -/ @[simps!] def liftInitialSeg : Cardinal.{u} ≤i Cardinal.{max u v} := by refine ⟨(OrderEmbedding.ofMapLEIff lift ?_).ltEmbedding, ?_⟩ <;> intro a b · refine inductionOn₂ a b fun _ _ ↦ ?_ rw [← lift_umax, lift_mk_le.{v, u, u}, le_def] · refine inductionOn₂ a b fun α β h ↦ ?_ obtain ⟨e⟩ := h.le replace e := e.congr (Equiv.refl β) Equiv.ulift refine ⟨#(range e), mk_congr (Equiv.ulift.trans <| Equiv.symm ?_)⟩ apply (e.codRestrict _ mem_range_self).equivOfSurjective rintro ⟨a, ⟨b, rfl⟩⟩ exact ⟨b, rfl⟩ theorem mem_range_lift_of_le {a : Cardinal.{u}} {b : Cardinal.{max u v}} : b ≤ lift.{v, u} a → b ∈ Set.range lift.{v, u} := liftInitialSeg.mem_range_of_le theorem lift_injective : Injective lift.{u, v} := liftInitialSeg.injective @[simp] theorem lift_inj {a b : Cardinal.{u}} : lift.{v, u} a = lift.{v, u} b ↔ a = b := lift_injective.eq_iff @[simp] theorem lift_le {a b : Cardinal.{v}} : lift.{u} a ≤ lift.{u} b ↔ a ≤ b := liftInitialSeg.le_iff_le @[simp] theorem lift_lt {a b : Cardinal.{u}} : lift.{v, u} a < lift.{v, u} b ↔ a < b := liftInitialSeg.lt_iff_lt theorem lift_strictMono : StrictMono lift := fun _ _ => lift_lt.2 theorem lift_monotone : Monotone lift := lift_strictMono.monotone @[simp] theorem lift_min {a b : Cardinal} : lift.{u, v} (min a b) = min (lift.{u, v} a) (lift.{u, v} b) := lift_monotone.map_min @[simp] theorem lift_max {a b : Cardinal} : lift.{u, v} (max a b) = max (lift.{u, v} a) (lift.{u, v} b) := lift_monotone.map_max -- This cannot be a `@[simp]` lemma because `simp` can't figure out the universes. theorem lift_umax_eq {a : Cardinal.{u}} {b : Cardinal.{v}} : lift.{max v w} a = lift.{max u w} b ↔ lift.{v} a = lift.{u} b := by rw [← lift_lift.{v, w, u}, ← lift_lift.{u, w, v}, lift_inj] theorem le_lift_iff {a : Cardinal.{u}} {b : Cardinal.{max u v}} : b ≤ lift.{v, u} a ↔ ∃ a' ≤ a, lift.{v, u} a' = b := liftInitialSeg.le_apply_iff theorem lt_lift_iff {a : Cardinal.{u}} {b : Cardinal.{max u v}} : b < lift.{v, u} a ↔ ∃ a' < a, lift.{v, u} a' = b := liftInitialSeg.lt_apply_iff /-! ### Basic cardinals -/ @[simp] theorem lift_eq_zero {a : Cardinal.{v}} : lift.{u} a = 0 ↔ a = 0 := lift_injective.eq_iff' lift_zero @[simp] theorem mk_fintype (α : Type u) [h : Fintype α] : #α = Fintype.card α := mk_congr (Fintype.equivOfCardEq (by simp)) private theorem cast_succ (n : ℕ) : ((n + 1 : ℕ) : Cardinal.{u}) = n + 1 := by change #(ULift.{u} _) = #(ULift.{u} _) + 1 rw [← mk_option] simp instance commSemiring : CommSemiring Cardinal.{u} where zero_add a := inductionOn a fun α => mk_congr <| Equiv.emptySum _ α add_zero a := inductionOn a fun α => mk_congr <| Equiv.sumEmpty α _ add_assoc a b c := inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.sumAssoc α β γ add_comm a b := inductionOn₂ a b fun α β => mk_congr <| Equiv.sumComm α β zero_mul a := inductionOn a fun _ => mk_eq_zero _ mul_zero a := inductionOn a fun _ => mk_eq_zero _ one_mul a := inductionOn a fun α => mk_congr <| Equiv.uniqueProd α _ mul_one a := inductionOn a fun α => mk_congr <| Equiv.prodUnique α _ mul_assoc a b c := inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.prodAssoc α β γ mul_comm a b := inductionOn₂ a b fun α β => mk_congr <| Equiv.prodComm α β left_distrib a b c := inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.prodSumDistrib α β γ right_distrib a b c := inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.sumProdDistrib α β γ nsmul := nsmulRec npow n c := c ^ (n : Cardinal) npow_zero := power_zero npow_succ n c := by rw [cast_succ, power_add, power_one] natCast n := lift #(Fin n) natCast_zero := rfl natCast_succ n := cast_succ n theorem mk_bool : #Bool = 2 := by simp theorem mk_Prop : #Prop = 2 := by simp theorem power_mul {a b c : Cardinal} : a ^ (b * c) = (a ^ b) ^ c := by rw [mul_comm b c] exact inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.curry γ β α @[simp, norm_cast] theorem power_natCast (a : Cardinal.{u}) (n : ℕ) : a ^ (↑n : Cardinal.{u}) = a ^ n := rfl @[simp] theorem lift_eq_one {a : Cardinal.{v}} : lift.{u} a = 1 ↔ a = 1 := lift_injective.eq_iff' lift_one @[simp] theorem lift_mul (a b : Cardinal.{u}) : lift.{v} (a * b) = lift.{v} a * lift.{v} b := inductionOn₂ a b fun _ _ => mk_congr <| Equiv.ulift.trans (Equiv.prodCongr Equiv.ulift Equiv.ulift).symm theorem lift_two : lift.{u, v} 2 = 2 := by simp [← one_add_one_eq_two] @[simp] theorem mk_set {α : Type u} : #(Set α) = 2 ^ #α := by simp [← one_add_one_eq_two, Set] /-- A variant of `Cardinal.mk_set` expressed in terms of a `Set` instead of a `Type`. -/ @[simp] theorem mk_powerset {α : Type u} (s : Set α) : #(↥(𝒫 s)) = 2 ^ #(↥s) := (mk_congr (Equiv.Set.powerset s)).trans mk_set theorem lift_two_power (a : Cardinal) : lift.{v} (2 ^ a) = 2 ^ lift.{v} a := by simp [← one_add_one_eq_two] /-! ### Order properties -/ protected theorem zero_le : ∀ a : Cardinal, 0 ≤ a := by rintro ⟨α⟩ exact ⟨Embedding.ofIsEmpty⟩ private theorem add_le_add' : ∀ {a b c d : Cardinal}, a ≤ b → c ≤ d → a + c ≤ b + d := by rintro ⟨α⟩ ⟨β⟩ ⟨γ⟩ ⟨δ⟩ ⟨e₁⟩ ⟨e₂⟩; exact ⟨e₁.sumMap e₂⟩ instance addLeftMono : AddLeftMono Cardinal := ⟨fun _ _ _ => add_le_add' le_rfl⟩ instance addRightMono : AddRightMono Cardinal := ⟨fun _ _ _ h => add_le_add' h le_rfl⟩ instance canonicallyOrderedAdd : CanonicallyOrderedAdd Cardinal.{u} where exists_add_of_le {a b} := inductionOn₂ a b fun α β ⟨⟨f, hf⟩⟩ => have : α ⊕ ((range f)ᶜ : Set β) ≃ β := by classical exact (Equiv.sumCongr (Equiv.ofInjective f hf) (Equiv.refl _)).trans <| Equiv.Set.sumCompl (range f) ⟨#(↥(range f)ᶜ), mk_congr this.symm⟩ le_self_add a b := (add_zero a).ge.trans <| by grw [Cardinal.zero_le b] le_add_self a _ := (zero_add a).ge.trans <| by grw [Cardinal.zero_le] instance isOrderedRing : IsOrderedRing Cardinal.{u} := CanonicallyOrderedAdd.toIsOrderedRing instance orderBot : OrderBot Cardinal.{u} where bot := 0 bot_le := zero_le instance noZeroDivisors : NoZeroDivisors Cardinal.{u} where eq_zero_or_eq_zero_of_mul_eq_zero := fun {a b} => inductionOn₂ a b fun α β => by simpa only [mul_def, mk_eq_zero_iff, isEmpty_prod] using id instance : LinearOrderedCommMonoidWithZero Cardinal.{u} := { Cardinal.commSemiring, Cardinal.linearOrder with bot_le _ := bot_le mul_le_mul_left := @mul_le_mul_left' _ _ _ _ zero_le_one := zero_le _ } -- Computable instance to prevent a non-computable one being found via the one above instance : CommMonoidWithZero Cardinal.{u} := { Cardinal.commSemiring with } -- Computable instance to prevent a non-computable one being found via the one above instance : CommMonoid Cardinal.{u} := { Cardinal.commSemiring with } theorem zero_power_le (c : Cardinal.{u}) : (0 : Cardinal.{u}) ^ c ≤ 1 := by by_cases h : c = 0 · rw [h, power_zero] · rw [zero_power h] apply zero_le theorem power_le_power_left : ∀ {a b c : Cardinal}, a ≠ 0 → b ≤ c → a ^ b ≤ a ^ c := by rintro ⟨α⟩ ⟨β⟩ ⟨γ⟩ hα ⟨e⟩ let ⟨a⟩ := mk_ne_zero_iff.1 hα exact ⟨@Function.Embedding.arrowCongrLeft _ _ _ ⟨a⟩ e⟩ theorem self_le_power (a : Cardinal) {b : Cardinal} (hb : 1 ≤ b) : a ≤ a ^ b := by rcases eq_or_ne a 0 with (rfl | ha) · exact zero_le _ · convert power_le_power_left ha hb exact (power_one a).symm /-- **Cantor's theorem** -/ theorem cantor (a : Cardinal.{u}) : a < 2 ^ a := by induction a using Cardinal.inductionOn with | _ α rw [← mk_set] refine ⟨⟨⟨singleton, fun a b => singleton_eq_singleton_iff.1⟩⟩, ?_⟩ rintro ⟨⟨f, hf⟩⟩ exact cantor_injective f hf instance : NoMaxOrder Cardinal.{u} where exists_gt a := ⟨_, cantor a⟩ -- short-circuit type class inference instance : DistribLattice Cardinal.{u} := inferInstance theorem power_le_max_power_one {a b c : Cardinal} (h : b ≤ c) : a ^ b ≤ max (a ^ c) 1 := by by_cases ha : a = 0 · simp [ha, zero_power_le] · exact (power_le_power_left ha h).trans (le_max_left _ _) theorem power_le_power_right {a b c : Cardinal} : a ≤ b → a ^ c ≤ b ^ c := inductionOn₃ a b c fun _ _ _ ⟨e⟩ => ⟨Embedding.arrowCongrRight e⟩ theorem power_pos {a : Cardinal} (b : Cardinal) (ha : 0 < a) : 0 < a ^ b := (power_ne_zero _ ha.ne').bot_lt protected theorem lt_wf : @WellFounded Cardinal.{u} (· < ·) := ⟨fun a => by_contradiction fun h => by let ι := { c : Cardinal // ¬Acc (· < ·) c } let f : ι → Cardinal := Subtype.val haveI hι : Nonempty ι := ⟨⟨_, h⟩⟩ obtain ⟨⟨c : Cardinal, hc : ¬Acc (· < ·) c⟩, ⟨h_1 : ∀ j, (f ⟨c, hc⟩).out ↪ (f j).out⟩⟩ := Embedding.min_injective fun i => (f i).out refine hc (Acc.intro _ fun j h' => by_contradiction fun hj => h'.2 ?_) have : #_ ≤ #_ := ⟨h_1 ⟨j, hj⟩⟩ simpa only [mk_out] using this⟩ instance : WellFoundedRelation Cardinal.{u} := ⟨(· < ·), Cardinal.lt_wf⟩ instance : WellFoundedLT Cardinal.{u} := ⟨Cardinal.lt_wf⟩ instance : ConditionallyCompleteLinearOrderBot Cardinal := WellFoundedLT.conditionallyCompleteLinearOrderBot _ @[simp] theorem sInf_empty : sInf (∅ : Set Cardinal.{u}) = 0 := dif_neg Set.not_nonempty_empty /-- Note that the successor of `c` is not the same as `c + 1` except in the case of finite `c`. -/ instance : SuccOrder Cardinal := ConditionallyCompleteLinearOrder.toSuccOrder theorem succ_def (c : Cardinal) : succ c = sInf { c' | c < c' } := dif_neg <| not_isMax c theorem succ_pos : ∀ c : Cardinal, 0 < succ c := bot_lt_succ theorem succ_ne_zero (c : Cardinal) : succ c ≠ 0 := (succ_pos _).ne' theorem add_one_le_succ (c : Cardinal.{u}) : c + 1 ≤ succ c := by have : Set.Nonempty { c' | c < c' } := exists_gt c simp_rw [succ_def, le_csInf_iff'' this, mem_setOf] intro b hlt rcases b, c with ⟨⟨β⟩, ⟨γ⟩⟩ obtain ⟨f⟩ := le_of_lt hlt have : ¬Surjective f := fun hn => (not_le_of_gt hlt) (mk_le_of_surjective hn) simp only [Surjective, not_forall] at this rcases this with ⟨b, hb⟩ calc #γ + 1 = #(Option γ) := mk_option.symm _ ≤ #β := (f.optionElim b hb).cardinal_le @[simp] theorem lift_succ (a) : lift.{v, u} (succ a) = succ (lift.{v, u} a) := le_antisymm (le_of_not_gt fun h => by rcases lt_lift_iff.1 h with ⟨b, h, e⟩ rw [lt_succ_iff, ← lift_le, e] at h exact h.not_gt (lt_succ _)) (succ_le_of_lt <| lift_lt.2 <| lt_succ a) /-! ### Limit cardinals -/ theorem ne_zero_of_isSuccLimit {c} (h : IsSuccLimit c) : c ≠ 0 := h.ne_bot theorem isSuccPrelimit_zero : IsSuccPrelimit (0 : Cardinal) := isSuccPrelimit_bot protected theorem isSuccLimit_iff {c : Cardinal} : IsSuccLimit c ↔ c ≠ 0 ∧ IsSuccPrelimit c := isSuccLimit_iff @[simp] protected theorem not_isSuccLimit_zero : ¬ IsSuccLimit (0 : Cardinal) := not_isSuccLimit_bot /-- A cardinal is a strong limit if it is not zero and it is closed under powersets. Note that `ℵ₀` is a strong limit by this definition. -/ structure IsStrongLimit (c : Cardinal) : Prop where ne_zero : c ≠ 0 two_power_lt ⦃x⦄ : x < c → 2 ^ x < c protected theorem IsStrongLimit.isSuccLimit {c} (H : IsStrongLimit c) : IsSuccLimit c := by rw [Cardinal.isSuccLimit_iff] exact ⟨H.ne_zero, isSuccPrelimit_of_succ_lt fun x h ↦ (succ_le_of_lt <| cantor x).trans_lt (H.two_power_lt h)⟩ protected theorem IsStrongLimit.isSuccPrelimit {c} (H : IsStrongLimit c) : IsSuccPrelimit c := H.isSuccLimit.isSuccPrelimit @[simp] theorem not_isStrongLimit_zero : ¬ IsStrongLimit (0 : Cardinal) := fun h ↦ h.ne_zero rfl /-! ### Indexed cardinal `sum` -/ theorem lift_le_sum {ι : Type u} (f : ι → Cardinal.{v}) (i) : lift.{u, v} (f i) ≤ sum f := by rw [← Quotient.out_eq (f i)] exact ⟨⟨fun a => ⟨i, a.down⟩, fun a b h => by simpa using h⟩⟩ theorem le_sum {ι : Type u} (f : ι → Cardinal.{max u v}) (i) : f i ≤ sum f := by simpa [← lift_umax] using lift_le_sum f i theorem iSup_le_sum {ι} (f : ι → Cardinal) : iSup f ≤ sum f := ciSup_le' <| le_sum _ @[simp] theorem sum_add_distrib {ι} (f g : ι → Cardinal) : sum (f + g) = sum f + sum g := by have := mk_congr (Equiv.sigmaSumDistrib (Quotient.out ∘ f) (Quotient.out ∘ g)) simp only [comp_apply, mk_sigma, mk_sum, mk_out, lift_id] at this exact this @[simp] theorem sum_add_distrib' {ι} (f g : ι → Cardinal) : (Cardinal.sum fun i => f i + g i) = sum f + sum g := sum_add_distrib f g @[gcongr] theorem sum_le_sum {ι} (f g : ι → Cardinal) (H : ∀ i, f i ≤ g i) : sum f ≤ sum g := ⟨(Embedding.refl _).sigmaMap fun i => Classical.choice <| by have := H i; rwa [← Quot.out_eq (f i), ← Quot.out_eq (g i)] at this⟩ theorem mk_le_mk_mul_of_mk_preimage_le {c : Cardinal} (f : α → β) (hf : ∀ b : β, #(f ⁻¹' {b}) ≤ c) : #α ≤ #β * c := by simpa only [← mk_congr (@Equiv.sigmaFiberEquiv α β f), mk_sigma, ← sum_const'] using sum_le_sum _ _ hf theorem lift_mk_le_lift_mk_mul_of_lift_mk_preimage_le {α : Type u} {β : Type v} {c : Cardinal} (f : α → β) (hf : ∀ b : β, lift.{v} #(f ⁻¹' {b}) ≤ c) : lift.{v} #α ≤ lift.{u} #β * c := (mk_le_mk_mul_of_mk_preimage_le fun x : ULift.{v} α => ULift.up.{u} (f x.1)) <| ULift.forall.2 fun b => (mk_congr <| (Equiv.ulift.image _).trans (Equiv.trans (by rw [Equiv.image_eq_preimage_symm] simp only [preimage, mem_singleton_iff, ULift.up_inj, mem_setOf_eq, coe_setOf] exact Equiv.refl _) Equiv.ulift.symm)).trans_le (hf b) end Cardinal /-! ### Well-ordering theorem -/ open Cardinal in theorem nonempty_embedding_to_cardinal : Nonempty (α ↪ Cardinal.{u}) := (Embedding.total _ _).resolve_left fun ⟨⟨f, hf⟩⟩ => let g : α → Cardinal.{u} := invFun f let ⟨x, (hx : g x = 2 ^ sum g)⟩ := invFun_surjective hf (2 ^ sum g) have : g x ≤ sum g := le_sum.{u, u} g x not_le_of_gt (by rw [hx]; exact cantor _) this /-- An embedding of any type to the set of cardinals in its universe. -/ def embeddingToCardinal : α ↪ Cardinal.{u} := Classical.choice nonempty_embedding_to_cardinal /-- Any type can be endowed with a well order, obtained by pulling back the well order over cardinals by some embedding. -/ def WellOrderingRel : α → α → Prop := embeddingToCardinal ⁻¹'o (· < ·) instance WellOrderingRel.isWellOrder : IsWellOrder α WellOrderingRel := (RelEmbedding.preimage _ _).isWellOrder instance IsWellOrder.subtype_nonempty : Nonempty { r // IsWellOrder α r } := ⟨⟨WellOrderingRel, inferInstance⟩⟩ variable (α) in /-- The **well-ordering theorem** (or **Zermelo's theorem**): every type has a well-order -/ theorem exists_wellOrder : ∃ (_ : LinearOrder α), WellFoundedLT α := by classical exact ⟨linearOrderOfSTO WellOrderingRel, WellOrderingRel.isWellOrder.toIsWellFounded⟩ namespace Cardinal /-! ### Bounds on suprema -/ lemma exists_eq_of_iSup_eq_of_not_isSuccPrelimit {ι : Type u} (f : ι → Cardinal.{v}) (ω : Cardinal.{v}) (hω : ¬ IsSuccPrelimit ω) (h : ⨆ i : ι, f i = ω) : ∃ i, f i = ω := by subst h suffices BddAbove (range f) from (isLUB_csSup' this).mem_of_not_isSuccPrelimit hω contrapose! hω with hf rw [iSup, csSup_of_not_bddAbove hf, csSup_empty] exact isSuccPrelimit_bot lemma exists_eq_of_iSup_eq_of_not_isSuccLimit {ι : Type u} [hι : Nonempty ι] (f : ι → Cardinal.{v}) (hf : BddAbove (range f)) {c : Cardinal.{v}} (hc : ¬ IsSuccLimit c) (h : ⨆ i, f i = c) : ∃ i, f i = c := by rw [Cardinal.isSuccLimit_iff] at hc refine (not_and_or.mp hc).elim (fun e ↦ ⟨hι.some, ?_⟩) (Cardinal.exists_eq_of_iSup_eq_of_not_isSuccPrelimit.{u, v} f c · h) cases not_not.mp e rw [← le_zero_iff] at h ⊢ exact (le_ciSup hf _).trans h /-! ### Indexed cardinal `prod` -/ /-- **König's theorem** -/ theorem sum_lt_prod {ι} (f g : ι → Cardinal) (H : ∀ i, f i < g i) : sum f < prod g := lt_of_not_ge fun ⟨F⟩ => by have : Inhabited (∀ i : ι, (g i).out) := by refine ⟨fun i => Classical.choice <| mk_ne_zero_iff.1 ?_⟩ rw [mk_out] exact (H i).ne_bot let G := invFun F have sG : Surjective G := invFun_surjective F.2 choose C hc using show ∀ i, ∃ b, ∀ a, G ⟨i, a⟩ i ≠ b by intro i simp only [not_exists.symm, not_forall.symm] refine fun h => (H i).not_ge ?_ rw [← mk_out (f i), ← mk_out (g i)] exact ⟨Embedding.ofSurjective _ h⟩ let ⟨⟨i, a⟩, h⟩ := sG C exact hc i a (congr_fun h _) theorem prod_le_prod {ι} (f g : ι → Cardinal) (H : ∀ i, f i ≤ g i) : prod f ≤ prod g := ⟨Embedding.piCongrRight fun i => Classical.choice <| by have := H i; rwa [← mk_out (f i), ← mk_out (g i)] at this⟩ /-! ### The first infinite cardinal `aleph0` -/ theorem aleph0_pos : 0 < ℵ₀ := pos_iff_ne_zero.2 aleph0_ne_zero @[simp] theorem aleph0_le_lift {c : Cardinal.{u}} : ℵ₀ ≤ lift.{v} c ↔ ℵ₀ ≤ c := by simpa using lift_le (a := ℵ₀) @[simp] theorem lift_le_aleph0 {c : Cardinal.{u}} : lift.{v} c ≤ ℵ₀ ↔ c ≤ ℵ₀ := by simpa using lift_le (b := ℵ₀) @[simp] theorem aleph0_lt_lift {c : Cardinal.{u}} : ℵ₀ < lift.{v} c ↔ ℵ₀ < c := by simpa using lift_lt (a := ℵ₀) @[simp] theorem lift_lt_aleph0 {c : Cardinal.{u}} : lift.{v} c < ℵ₀ ↔ c < ℵ₀ := by simpa using lift_lt (b := ℵ₀) @[simp] theorem aleph0_eq_lift {c : Cardinal.{u}} : ℵ₀ = lift.{v} c ↔ ℵ₀ = c := by simpa using lift_inj (a := ℵ₀) @[simp] theorem lift_eq_aleph0 {c : Cardinal.{u}} : lift.{v} c = ℵ₀ ↔ c = ℵ₀ := by simpa using lift_inj (b := ℵ₀) /-! ### Properties about the cast from `ℕ` -/ theorem mk_fin (n : ℕ) : #(Fin n) = n := by simp @[simp] theorem lift_natCast (n : ℕ) : lift.{u} (n : Cardinal.{v}) = n := by induction n <;> simp [*] @[simp] theorem lift_ofNat (n : ℕ) [n.AtLeastTwo] : lift.{u} (ofNat(n) : Cardinal.{v}) = OfNat.ofNat n := lift_natCast n @[simp] theorem lift_eq_nat_iff {a : Cardinal.{u}} {n : ℕ} : lift.{v} a = n ↔ a = n := lift_injective.eq_iff' (lift_natCast n) @[simp] theorem lift_eq_ofNat_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : lift.{v} a = ofNat(n) ↔ a = OfNat.ofNat n := lift_eq_nat_iff @[simp] theorem nat_eq_lift_iff {n : ℕ} {a : Cardinal.{u}} : (n : Cardinal) = lift.{v} a ↔ (n : Cardinal) = a := by rw [← lift_natCast.{v,u} n, lift_inj] @[simp] theorem zero_eq_lift_iff {a : Cardinal.{u}} : (0 : Cardinal) = lift.{v} a ↔ 0 = a := by simpa using nat_eq_lift_iff (n := 0) @[simp] theorem one_eq_lift_iff {a : Cardinal.{u}} : (1 : Cardinal) = lift.{v} a ↔ 1 = a := by simpa using nat_eq_lift_iff (n := 1) @[simp] theorem ofNat_eq_lift_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : (ofNat(n) : Cardinal) = lift.{v} a ↔ (OfNat.ofNat n : Cardinal) = a := nat_eq_lift_iff @[simp] theorem lift_le_nat_iff {a : Cardinal.{u}} {n : ℕ} : lift.{v} a ≤ n ↔ a ≤ n := by rw [← lift_natCast.{v,u}, lift_le] @[simp] theorem lift_le_one_iff {a : Cardinal.{u}} : lift.{v} a ≤ 1 ↔ a ≤ 1 := by simpa using lift_le_nat_iff (n := 1) @[simp] theorem lift_le_ofNat_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : lift.{v} a ≤ ofNat(n) ↔ a ≤ OfNat.ofNat n := lift_le_nat_iff @[simp] theorem nat_le_lift_iff {n : ℕ} {a : Cardinal.{u}} : n ≤ lift.{v} a ↔ n ≤ a := by rw [← lift_natCast.{v,u}, lift_le] @[simp] theorem one_le_lift_iff {a : Cardinal.{u}} : (1 : Cardinal) ≤ lift.{v} a ↔ 1 ≤ a := by simpa using nat_le_lift_iff (n := 1) @[simp] theorem ofNat_le_lift_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : (ofNat(n) : Cardinal) ≤ lift.{v} a ↔ (OfNat.ofNat n : Cardinal) ≤ a := nat_le_lift_iff @[simp] theorem lift_lt_nat_iff {a : Cardinal.{u}} {n : ℕ} : lift.{v} a < n ↔ a < n := by rw [← lift_natCast.{v,u}, lift_lt] @[simp] theorem lift_lt_ofNat_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : lift.{v} a < ofNat(n) ↔ a < OfNat.ofNat n := lift_lt_nat_iff @[simp] theorem nat_lt_lift_iff {n : ℕ} {a : Cardinal.{u}} : n < lift.{v} a ↔ n < a := by rw [← lift_natCast.{v,u}, lift_lt] @[simp] theorem zero_lt_lift_iff {a : Cardinal.{u}} : (0 : Cardinal) < lift.{v} a ↔ 0 < a := by simpa using nat_lt_lift_iff (n := 0) @[simp] theorem one_lt_lift_iff {a : Cardinal.{u}} : (1 : Cardinal) < lift.{v} a ↔ 1 < a := by simpa using nat_lt_lift_iff (n := 1) @[simp] theorem ofNat_lt_lift_iff {a : Cardinal.{u}} {n : ℕ} [n.AtLeastTwo] : (ofNat(n) : Cardinal) < lift.{v} a ↔ (OfNat.ofNat n : Cardinal) < a := nat_lt_lift_iff theorem mk_coe_finset {α : Type u} {s : Finset α} : #s = ↑(Finset.card s) := by simp theorem card_le_of_finset {α} (s : Finset α) : (s.card : Cardinal) ≤ #α := @mk_coe_finset _ s ▸ mk_set_le _ instance : CharZero Cardinal := by refine ⟨fun a b h ↦ ?_⟩ rwa [← lift_mk_fin, ← lift_mk_fin, lift_inj, Cardinal.eq, ← Fintype.card_eq, Fintype.card_fin, Fintype.card_fin] at h end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Arithmetic.lean
import Mathlib.SetTheory.Cardinal.Aleph /-! # Cardinal arithmetic Arithmetic operations on cardinals are defined in `SetTheory/Cardinal/Order.lean`. However, proving the important theorem `c * c = c` for infinite cardinals and its corollaries requires the use of ordinal numbers. This is done within this file. ## Main statements * `Cardinal.mul_eq_max` and `Cardinal.add_eq_max` state that the product (resp. sum) of two infinite cardinals is just their maximum. Several variations around this fact are also given. * `Cardinal.mk_list_eq_mk`: when `α` is infinite, `α` and `List α` have the same cardinality. ## Tags cardinal arithmetic (for infinite cardinals) -/ assert_not_exists Module Finsupp Ordinal.log noncomputable section open Function Set Cardinal Equiv Order Ordinal universe u v w namespace Cardinal /-! ### Properties of `mul` -/ section mul /-- If `α` is an infinite type, then `α × α` and `α` have the same cardinality. -/ theorem mul_eq_self {c : Cardinal} (h : ℵ₀ ≤ c) : c * c = c := by refine le_antisymm ?_ (by simpa only [mul_one] using mul_le_mul_left' (one_le_aleph0.trans h) c) -- the only nontrivial part is `c * c ≤ c`. We prove it inductively. refine Acc.recOn (Cardinal.lt_wf.apply c) (fun c _ => Cardinal.inductionOn c fun α IH ol => ?_) h -- consider the minimal well-order `r` on `α` (a type with cardinality `c`). rcases ord_eq α with ⟨r, wo, e⟩ classical letI := linearOrderOfSTO r haveI : IsWellOrder α (· < ·) := wo -- Define an order `s` on `α × α` by writing `(a, b) < (c, d)` if `max a b < max c d`, or -- the max are equal and `a < c`, or the max are equal and `a = c` and `b < d`. let g : α × α → α := fun p => max p.1 p.2 let f : α × α ↪ Ordinal × α × α := ⟨fun p : α × α => (typein (· < ·) (g p), p), fun p q => congr_arg Prod.snd⟩ let s := f ⁻¹'o Prod.Lex (· < ·) (Prod.Lex (· < ·) (· < ·)) -- this is a well order on `α × α`. haveI : IsWellOrder _ s := (RelEmbedding.preimage _ _).isWellOrder /- it suffices to show that this well order is smaller than `r` if it were larger, then `r` would be a strict prefix of `s`. It would be contained in `β × β` for some `β` of cardinality `< c`. By the inductive assumption, this set has the same cardinality as `β` (or it is finite if `β` is finite), so it is `< c`, which is a contradiction. -/ suffices type s ≤ type r by exact card_le_card this refine le_of_forall_lt fun o h => ?_ rcases typein_surj s h with ⟨p, rfl⟩ rw [← e, lt_ord] refine lt_of_le_of_lt (?_ : _ ≤ card (succ (typein (· < ·) (g p))) * card (succ (typein (· < ·) (g p)))) ?_ · have : { q | s q p } ⊆ insert (g p) { x | x < g p } ×ˢ insert (g p) { x | x < g p } := by intro q h simp only [s, f, Preimage, Embedding.coeFn_mk, Prod.lex_def, typein_lt_typein, typein_inj, mem_setOf_eq] at h exact max_le_iff.1 (le_iff_lt_or_eq.2 <| h.imp_right And.left) suffices H : (insert (g p) { x | r x (g p) } : Set α) ≃ { x | r x (g p) } ⊕ PUnit from ⟨(Set.embeddingOfSubset _ _ this).trans ((Equiv.Set.prod _ _).trans (H.prodCongr H)).toEmbedding⟩ refine (Equiv.Set.insert ?_).trans ((Equiv.refl _).sumCongr punitEquivPUnit) apply @irrefl _ r rcases lt_or_ge (card (succ (typein (· < ·) (g p)))) ℵ₀ with qo | qo · exact (mul_lt_aleph0 qo qo).trans_le ol · suffices (succ (typein LT.lt (g p))).card < #α from (IH _ this qo).trans_lt this rw [← lt_ord] apply (isSuccLimit_ord ol).succ_lt rw [e] apply typein_lt_type /-- If `α` and `β` are infinite types, then the cardinality of `α × β` is the maximum of the cardinalities of `α` and `β`. -/ theorem mul_eq_max {a b : Cardinal} (ha : ℵ₀ ≤ a) (hb : ℵ₀ ≤ b) : a * b = max a b := le_antisymm (mul_eq_self (ha.trans (le_max_left a b)) ▸ mul_le_mul' (le_max_left _ _) (le_max_right _ _)) <| max_le (by simpa only [mul_one] using mul_le_mul_left' (one_le_aleph0.trans hb) a) (by simpa only [one_mul] using mul_le_mul_right' (one_le_aleph0.trans ha) b) @[simp] theorem mul_mk_eq_max {α β : Type u} [Infinite α] [Infinite β] : #α * #β = max #α #β := mul_eq_max (aleph0_le_mk α) (aleph0_le_mk β) @[simp] theorem aleph_mul_aleph (o₁ o₂ : Ordinal) : ℵ_ o₁ * ℵ_ o₂ = ℵ_ (max o₁ o₂) := by rw [Cardinal.mul_eq_max (aleph0_le_aleph o₁) (aleph0_le_aleph o₂), aleph_max] @[simp] theorem aleph0_mul_eq {a : Cardinal} (ha : ℵ₀ ≤ a) : ℵ₀ * a = a := (mul_eq_max le_rfl ha).trans (max_eq_right ha) @[simp] theorem mul_aleph0_eq {a : Cardinal} (ha : ℵ₀ ≤ a) : a * ℵ₀ = a := (mul_eq_max ha le_rfl).trans (max_eq_left ha) theorem aleph0_mul_mk_eq {α : Type*} [Infinite α] : ℵ₀ * #α = #α := aleph0_mul_eq (aleph0_le_mk α) theorem mk_mul_aleph0_eq {α : Type*} [Infinite α] : #α * ℵ₀ = #α := mul_aleph0_eq (aleph0_le_mk α) @[simp] theorem aleph0_mul_aleph (o : Ordinal) : ℵ₀ * ℵ_ o = ℵ_ o := aleph0_mul_eq (aleph0_le_aleph o) @[simp] theorem aleph_mul_aleph0 (o : Ordinal) : ℵ_ o * ℵ₀ = ℵ_ o := mul_aleph0_eq (aleph0_le_aleph o) theorem mul_lt_of_lt {a b c : Cardinal} (hc : ℵ₀ ≤ c) (h1 : a < c) (h2 : b < c) : a * b < c := (mul_le_mul' (le_max_left a b) (le_max_right a b)).trans_lt <| (lt_or_ge (max a b) ℵ₀).elim (fun h => (mul_lt_aleph0 h h).trans_le hc) fun h => by rw [mul_eq_self h] exact max_lt h1 h2 theorem mul_le_max_of_aleph0_le_left {a b : Cardinal} (h : ℵ₀ ≤ a) : a * b ≤ max a b := by convert mul_le_mul' (le_max_left a b) (le_max_right a b) using 1 rw [mul_eq_self] exact h.trans (le_max_left a b) theorem mul_eq_max_of_aleph0_le_left {a b : Cardinal} (h : ℵ₀ ≤ a) (h' : b ≠ 0) : a * b = max a b := by rcases le_or_gt ℵ₀ b with hb | hb · exact mul_eq_max h hb refine (mul_le_max_of_aleph0_le_left h).antisymm ?_ have : b ≤ a := hb.le.trans h rw [max_eq_left this] convert mul_le_mul_left' (one_le_iff_ne_zero.mpr h') a rw [mul_one] theorem mul_le_max_of_aleph0_le_right {a b : Cardinal} (h : ℵ₀ ≤ b) : a * b ≤ max a b := by simpa only [mul_comm b, max_comm b] using mul_le_max_of_aleph0_le_left h theorem mul_eq_max_of_aleph0_le_right {a b : Cardinal} (h' : a ≠ 0) (h : ℵ₀ ≤ b) : a * b = max a b := by rw [mul_comm, max_comm] exact mul_eq_max_of_aleph0_le_left h h' theorem mul_eq_max' {a b : Cardinal} (h : ℵ₀ ≤ a * b) : a * b = max a b := by rcases aleph0_le_mul_iff.mp h with ⟨ha, hb, ha' | hb'⟩ · exact mul_eq_max_of_aleph0_le_left ha' hb · exact mul_eq_max_of_aleph0_le_right ha hb' theorem mul_le_max (a b : Cardinal) : a * b ≤ max (max a b) ℵ₀ := by rcases eq_or_ne a 0 with (rfl | ha0); · simp rcases eq_or_ne b 0 with (rfl | hb0); · simp rcases le_or_gt ℵ₀ a with ha | ha · rw [mul_eq_max_of_aleph0_le_left ha hb0] exact le_max_left _ _ · rcases le_or_gt ℵ₀ b with hb | hb · rw [mul_comm, mul_eq_max_of_aleph0_le_left hb ha0, max_comm] exact le_max_left _ _ · exact le_max_of_le_right (mul_lt_aleph0 ha hb).le theorem mul_eq_left {a b : Cardinal} (ha : ℵ₀ ≤ a) (hb : b ≤ a) (hb' : b ≠ 0) : a * b = a := by rw [mul_eq_max_of_aleph0_le_left ha hb', max_eq_left hb] theorem mul_eq_right {a b : Cardinal} (hb : ℵ₀ ≤ b) (ha : a ≤ b) (ha' : a ≠ 0) : a * b = b := by rw [mul_comm, mul_eq_left hb ha ha'] theorem le_mul_left {a b : Cardinal} (h : b ≠ 0) : a ≤ b * a := by convert mul_le_mul_right' (one_le_iff_ne_zero.mpr h) a rw [one_mul] theorem le_mul_right {a b : Cardinal} (h : b ≠ 0) : a ≤ a * b := by rw [mul_comm] exact le_mul_left h theorem mul_eq_left_iff {a b : Cardinal} : a * b = a ↔ max ℵ₀ b ≤ a ∧ b ≠ 0 ∨ b = 1 ∨ a = 0 := by rw [max_le_iff] refine ⟨fun h => ?_, ?_⟩ · rcases le_or_gt ℵ₀ a with ha | ha · have : a ≠ 0 := by rintro rfl exact ha.not_gt aleph0_pos left rw [and_assoc] use ha constructor · rw [← not_lt] exact fun hb => ne_of_gt (hb.trans_le (le_mul_left this)) h · rintro rfl apply this rw [mul_zero] at h exact h.symm right by_cases h2a : a = 0 · exact Or.inr h2a have hb : b ≠ 0 := by rintro rfl apply h2a rw [mul_zero] at h exact h.symm left rw [← h, mul_lt_aleph0_iff, lt_aleph0, lt_aleph0] at ha rcases ha with (rfl | rfl | ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩) · contradiction · contradiction rw [← Ne] at h2a rw [← one_le_iff_ne_zero] at h2a hb norm_cast at h2a hb h ⊢ apply le_antisymm _ hb rw [← not_lt] apply fun h2b => ne_of_gt _ h conv_rhs => left; rw [← mul_one n] rw [Nat.mul_lt_mul_left] · exact id apply Nat.lt_of_succ_le h2a · rintro (⟨⟨ha, hab⟩, hb⟩ | rfl | rfl) · rw [mul_eq_max_of_aleph0_le_left ha hb, max_eq_left hab] all_goals simp end mul /-! ### Properties of `add` -/ section add /-- If `α` is an infinite type, then `α ⊕ α` and `α` have the same cardinality. -/ theorem add_eq_self {c : Cardinal} (h : ℵ₀ ≤ c) : c + c = c := le_antisymm (by convert mul_le_mul_right' ((nat_lt_aleph0 2).le.trans h) c using 1 <;> simp [two_mul, mul_eq_self h]) (self_le_add_left c c) /-- If `α` is an infinite type, then the cardinality of `α ⊕ β` is the maximum of the cardinalities of `α` and `β`. -/ theorem add_eq_max {a b : Cardinal} (ha : ℵ₀ ≤ a) : a + b = max a b := le_antisymm (add_eq_self (ha.trans (le_max_left a b)) ▸ add_le_add (le_max_left _ _) (le_max_right _ _)) <| max_le (self_le_add_right _ _) (self_le_add_left _ _) theorem add_eq_max' {a b : Cardinal} (ha : ℵ₀ ≤ b) : a + b = max a b := by rw [add_comm, max_comm, add_eq_max ha] @[simp] theorem add_mk_eq_max {α β : Type u} [Infinite α] : #α + #β = max #α #β := add_eq_max (aleph0_le_mk α) @[simp] theorem add_mk_eq_max' {α β : Type u} [Infinite β] : #α + #β = max #α #β := add_eq_max' (aleph0_le_mk β) theorem add_mk_eq_self {α : Type*} [Infinite α] : #α + #α = #α := by simp theorem add_le_max (a b : Cardinal) : a + b ≤ max (max a b) ℵ₀ := by rcases le_or_gt ℵ₀ a with ha | ha · rw [add_eq_max ha] exact le_max_left _ _ · rcases le_or_gt ℵ₀ b with hb | hb · rw [add_comm, add_eq_max hb, max_comm] exact le_max_left _ _ · exact le_max_of_le_right (add_lt_aleph0 ha hb).le theorem add_le_of_le {a b c : Cardinal} (hc : ℵ₀ ≤ c) (h1 : a ≤ c) (h2 : b ≤ c) : a + b ≤ c := (add_le_add h1 h2).trans <| le_of_eq <| add_eq_self hc theorem add_lt_of_lt {a b c : Cardinal} (hc : ℵ₀ ≤ c) (h1 : a < c) (h2 : b < c) : a + b < c := (add_le_add (le_max_left a b) (le_max_right a b)).trans_lt <| (lt_or_ge (max a b) ℵ₀).elim (fun h => (add_lt_aleph0 h h).trans_le hc) fun h => by rw [add_eq_self h]; exact max_lt h1 h2 theorem eq_of_add_eq_of_aleph0_le {a b c : Cardinal} (h : a + b = c) (ha : a < c) (hc : ℵ₀ ≤ c) : b = c := by apply le_antisymm · rw [← h] apply self_le_add_left rw [← not_lt]; intro hb have : a + b < c := add_lt_of_lt hc ha hb simp [h] at this theorem add_eq_left {a b : Cardinal} (ha : ℵ₀ ≤ a) (hb : b ≤ a) : a + b = a := by rw [add_eq_max ha, max_eq_left hb] theorem add_eq_right {a b : Cardinal} (hb : ℵ₀ ≤ b) (ha : a ≤ b) : a + b = b := by rw [add_comm, add_eq_left hb ha] theorem add_eq_left_iff {a b : Cardinal} : a + b = a ↔ max ℵ₀ b ≤ a ∨ b = 0 := by rw [max_le_iff] refine ⟨fun h => ?_, ?_⟩ · rcases le_or_gt ℵ₀ a with ha | ha · left use ha rw [← not_lt] apply fun hb => ne_of_gt _ h intro hb exact hb.trans_le (self_le_add_left b a) right rw [← h, add_lt_aleph0_iff, lt_aleph0, lt_aleph0] at ha rcases ha with ⟨⟨n, rfl⟩, ⟨m, rfl⟩⟩ norm_cast at h ⊢ rw [← add_right_inj, h, add_zero] · rintro (⟨h1, h2⟩ | h3) · rw [add_eq_max h1, max_eq_left h2] · rw [h3, add_zero] theorem add_eq_right_iff {a b : Cardinal} : a + b = b ↔ max ℵ₀ a ≤ b ∨ a = 0 := by rw [add_comm, add_eq_left_iff] theorem add_nat_eq {a : Cardinal} (n : ℕ) (ha : ℵ₀ ≤ a) : a + n = a := add_eq_left ha ((nat_lt_aleph0 _).le.trans ha) theorem nat_add_eq {a : Cardinal} (n : ℕ) (ha : ℵ₀ ≤ a) : n + a = a := by rw [add_comm, add_nat_eq n ha] theorem add_one_eq {a : Cardinal} (ha : ℵ₀ ≤ a) : a + 1 = a := add_one_of_aleph0_le ha theorem mk_add_one_eq {α : Type*} [Infinite α] : #α + 1 = #α := add_one_eq (aleph0_le_mk α) protected theorem eq_of_add_eq_add_left {a b c : Cardinal} (h : a + b = a + c) (ha : a < ℵ₀) : b = c := by rcases le_or_gt ℵ₀ b with hb | hb · have : a < b := ha.trans_le hb rw [add_eq_right hb this.le, eq_comm] at h rw [eq_of_add_eq_of_aleph0_le h this hb] · have hc : c < ℵ₀ := by rw [← not_le] intro hc apply lt_irrefl ℵ₀ apply (hc.trans (self_le_add_left _ a)).trans_lt rw [← h] apply add_lt_aleph0 ha hb rw [lt_aleph0] at * rcases ha with ⟨n, rfl⟩ rcases hb with ⟨m, rfl⟩ rcases hc with ⟨k, rfl⟩ norm_cast at h ⊢ apply add_left_cancel h protected theorem eq_of_add_eq_add_right {a b c : Cardinal} (h : a + b = c + b) (hb : b < ℵ₀) : a = c := by rw [add_comm a b, add_comm c b] at h exact Cardinal.eq_of_add_eq_add_left h hb end add /-! ### Properties of `ciSup` -/ section ciSup variable {ι : Type u} {ι' : Type w} (f : ι → Cardinal.{v}) section add variable [Nonempty ι] [Nonempty ι'] protected theorem ciSup_add (hf : BddAbove (range f)) (c : Cardinal.{v}) : (⨆ i, f i) + c = ⨆ i, f i + c := by have (i : ι) : f i + c ≤ (⨆ i, f i) + c := by grw [le_ciSup hf i] refine le_antisymm ?_ (ciSup_le' this) have bdd : BddAbove (range (f · + c)) := ⟨_, forall_mem_range.mpr this⟩ obtain hs | hs := lt_or_ge (⨆ i, f i) ℵ₀ · obtain ⟨i, hi⟩ := exists_eq_of_iSup_eq_of_not_isSuccLimit f hf (not_isSuccLimit_of_lt_aleph0 hs) rfl exact hi ▸ le_ciSup bdd i rw [add_eq_max hs, max_le_iff] exact ⟨ciSup_mono bdd fun i ↦ self_le_add_right _ c, (self_le_add_left _ _).trans (le_ciSup bdd <| Classical.arbitrary ι)⟩ protected theorem add_ciSup (hf : BddAbove (range f)) (c : Cardinal.{v}) : c + (⨆ i, f i) = ⨆ i, c + f i := by rw [add_comm, Cardinal.ciSup_add f hf]; simp_rw [add_comm] protected theorem ciSup_add_ciSup (hf : BddAbove (range f)) (g : ι' → Cardinal.{v}) (hg : BddAbove (range g)) : (⨆ i, f i) + (⨆ j, g j) = ⨆ (i) (j), f i + g j := by simp_rw [Cardinal.ciSup_add f hf, Cardinal.add_ciSup g hg] end add protected theorem ciSup_mul (c : Cardinal.{v}) : (⨆ i, f i) * c = ⨆ i, f i * c := by cases isEmpty_or_nonempty ι; · simp obtain rfl | h0 := eq_or_ne c 0; · simp by_cases hf : BddAbove (range f); swap · have hfc : ¬ BddAbove (range (f · * c)) := fun bdd ↦ hf ⟨⨆ i, f i * c, forall_mem_range.mpr fun i ↦ (le_mul_right h0).trans (le_ciSup bdd i)⟩ simp [iSup, csSup_of_not_bddAbove, hf, hfc] have (i : ι) : f i * c ≤ (⨆ i, f i) * c := by grw [← le_ciSup hf i] refine le_antisymm ?_ (ciSup_le' this) have bdd : BddAbove (range (f · * c)) := ⟨_, forall_mem_range.mpr this⟩ obtain hs | hs := lt_or_ge (⨆ i, f i) ℵ₀ · obtain ⟨i, hi⟩ := exists_eq_of_iSup_eq_of_not_isSuccLimit f hf (not_isSuccLimit_of_lt_aleph0 hs) rfl exact hi ▸ le_ciSup bdd i rw [mul_eq_max_of_aleph0_le_left hs h0, max_le_iff] obtain ⟨i, hi⟩ := exists_lt_of_lt_ciSup' (one_lt_aleph0.trans_le hs) exact ⟨ciSup_mono bdd fun i ↦ le_mul_right h0, (le_mul_left (zero_lt_one.trans hi).ne').trans (le_ciSup bdd i)⟩ protected theorem mul_ciSup (c : Cardinal.{v}) : c * (⨆ i, f i) = ⨆ i, c * f i := by rw [mul_comm, Cardinal.ciSup_mul f]; simp_rw [mul_comm] protected theorem ciSup_mul_ciSup (g : ι' → Cardinal.{v}) : (⨆ i, f i) * (⨆ j, g j) = ⨆ (i) (j), f i * g j := by simp_rw [Cardinal.ciSup_mul f, Cardinal.mul_ciSup g] theorem sum_eq_lift_iSup_of_lift_mk_le_lift_iSup [Small.{v} ι] {f : ι → Cardinal.{v}} (hι : ℵ₀ ≤ #ι) (h : lift.{v} #ι ≤ lift.{u} (⨆ i, f i)) : sum f = lift (⨆ i, f i) := by rw [lift_iSup (bddAbove_of_small _)] at h apply (lift_iSup_le_sum f).antisymm' convert sum_le_lift_mk_mul_iSup_lift f rw [mul_eq_max (aleph0_le_lift.mpr hι) ((aleph0_le_lift.mpr hι).trans h), max_eq_right h, lift_iSup (bddAbove_of_small _)] theorem sum_eq_iSup_of_lift_mk_le_iSup {f : ι → Cardinal.{max u v}} (hι : ℵ₀ ≤ #ι) (h : lift.{v} #ι ≤ ⨆ i, f i) : sum f = ⨆ i, f i := by rw [← lift_id'.{u, v} (iSup _)] apply sum_eq_lift_iSup_of_lift_mk_le_lift_iSup hι rwa [lift_umax, lift_id'.{u, v}] theorem sum_eq_iSup_of_mk_le_iSup {f : ι → Cardinal.{u}} (hι : ℵ₀ ≤ #ι) (h : #ι ≤ iSup f) : sum f = ⨆ i, f i := sum_eq_iSup_of_lift_mk_le_iSup hι ((lift_id #ι).symm ▸ h) @[deprecated (since := "2025-09-06")] alias sum_eq_iSup_lift := sum_eq_iSup_of_lift_mk_le_iSup @[deprecated (since := "2025-09-06")] alias sum_eq_iSup := sum_eq_iSup_of_mk_le_iSup end ciSup /-! ### Properties of `aleph` -/ section aleph @[simp] theorem aleph_add_aleph (o₁ o₂ : Ordinal) : ℵ_ o₁ + ℵ_ o₂ = ℵ_ (max o₁ o₂) := by rw [Cardinal.add_eq_max (aleph0_le_aleph o₁), aleph_max] theorem add_right_inj_of_lt_aleph0 {α β γ : Cardinal} (γ₀ : γ < aleph0) : α + γ = β + γ ↔ α = β := ⟨fun h => Cardinal.eq_of_add_eq_add_right h γ₀, fun h => congr_arg (· + γ) h⟩ @[simp] theorem add_nat_inj {α β : Cardinal} (n : ℕ) : α + n = β + n ↔ α = β := add_right_inj_of_lt_aleph0 (nat_lt_aleph0 _) @[simp] theorem add_one_inj {α β : Cardinal} : α + 1 = β + 1 ↔ α = β := add_right_inj_of_lt_aleph0 one_lt_aleph0 theorem add_le_add_iff_of_lt_aleph0 {α β γ : Cardinal} (γ₀ : γ < ℵ₀) : α + γ ≤ β + γ ↔ α ≤ β := by refine ⟨fun h => ?_, fun h => by gcongr⟩ contrapose h rw [not_le, lt_iff_le_and_ne, Ne] at h ⊢ exact ⟨by grw [h.1], mt (add_right_inj_of_lt_aleph0 γ₀).1 h.2⟩ @[simp] theorem add_nat_le_add_nat_iff {α β : Cardinal} (n : ℕ) : α + n ≤ β + n ↔ α ≤ β := add_le_add_iff_of_lt_aleph0 (nat_lt_aleph0 n) @[simp] theorem add_one_le_add_one_iff {α β : Cardinal} : α + 1 ≤ β + 1 ↔ α ≤ β := add_le_add_iff_of_lt_aleph0 one_lt_aleph0 end aleph /-! ### Properties about `power` -/ section power theorem pow_le {κ μ : Cardinal.{u}} (H1 : ℵ₀ ≤ κ) (H2 : μ < ℵ₀) : κ ^ μ ≤ κ := let ⟨n, H3⟩ := lt_aleph0.1 H2 H3.symm ▸ Quotient.inductionOn κ (fun α H1 => Nat.recOn n (by grw [Nat.cast_zero, power_zero, one_lt_aleph0, H1]) fun n ih => by grw [Nat.cast_succ, power_add, power_one, ih, mul_eq_self H1]) H1 theorem pow_eq {κ μ : Cardinal.{u}} (H1 : ℵ₀ ≤ κ) (H2 : 1 ≤ μ) (H3 : μ < ℵ₀) : κ ^ μ = κ := (pow_le H1 H3).antisymm <| self_le_power κ H2 theorem power_self_eq {c : Cardinal} (h : ℵ₀ ≤ c) : c ^ c = 2 ^ c := by apply ((power_le_power_right <| (cantor c).le).trans _).antisymm · exact power_le_power_right ((nat_lt_aleph0 2).le.trans h) · rw [← power_mul, mul_eq_self h] theorem prod_eq_two_power {ι : Type u} [Infinite ι] {c : ι → Cardinal.{v}} (h₁ : ∀ i, 2 ≤ c i) (h₂ : ∀ i, lift.{u} (c i) ≤ lift.{v} #ι) : prod c = 2 ^ lift.{v} #ι := by rw [← lift_id'.{u, v} (prod.{u, v} c), lift_prod, ← lift_two_power] apply le_antisymm · refine (prod_le_prod _ _ h₂).trans_eq ?_ rw [prod_const, lift_lift, ← lift_power, power_self_eq (aleph0_le_mk ι), lift_umax.{u, v}] · rw [← prod_const', lift_prod] refine prod_le_prod _ _ fun i => ?_ rw [lift_two, ← lift_two.{u, v}, lift_le] exact h₁ i theorem power_eq_two_power {c₁ c₂ : Cardinal} (h₁ : ℵ₀ ≤ c₁) (h₂ : 2 ≤ c₂) (h₂' : c₂ ≤ c₁) : c₂ ^ c₁ = 2 ^ c₁ := le_antisymm (power_self_eq h₁ ▸ power_le_power_right h₂') (power_le_power_right h₂) theorem nat_power_eq {c : Cardinal.{u}} (h : ℵ₀ ≤ c) {n : ℕ} (hn : 2 ≤ n) : (n : Cardinal.{u}) ^ c = 2 ^ c := power_eq_two_power h (by assumption_mod_cast) ((nat_lt_aleph0 n).le.trans h) theorem power_nat_le {c : Cardinal.{u}} {n : ℕ} (h : ℵ₀ ≤ c) : c ^ n ≤ c := pow_le h (nat_lt_aleph0 n) theorem power_nat_eq {c : Cardinal.{u}} {n : ℕ} (h1 : ℵ₀ ≤ c) (h2 : 1 ≤ n) : c ^ n = c := pow_eq h1 (mod_cast h2) (nat_lt_aleph0 n) theorem power_nat_le_max {c : Cardinal.{u}} {n : ℕ} : c ^ (n : Cardinal.{u}) ≤ max c ℵ₀ := by rcases le_or_gt ℵ₀ c with hc | hc · exact le_max_of_le_left (power_nat_le hc) · exact le_max_of_le_right (power_lt_aleph0 hc (nat_lt_aleph0 _)).le lemma power_le_aleph0 {a b : Cardinal.{u}} (ha : a ≤ ℵ₀) (hb : b < ℵ₀) : a ^ b ≤ ℵ₀ := by lift b to ℕ using hb; simpa [ha] using power_nat_le_max (c := a) theorem powerlt_aleph0 {c : Cardinal} (h : ℵ₀ ≤ c) : c ^< ℵ₀ = c := by apply le_antisymm · rw [powerlt_le] intro c' rw [lt_aleph0] rintro ⟨n, rfl⟩ apply power_nat_le h convert le_powerlt c one_lt_aleph0; rw [power_one] theorem powerlt_aleph0_le (c : Cardinal) : c ^< ℵ₀ ≤ max c ℵ₀ := by rcases le_or_gt ℵ₀ c with h | h · rw [powerlt_aleph0 h] apply le_max_left rw [powerlt_le] exact fun c' hc' => (power_lt_aleph0 h hc').le.trans (le_max_right _ _) end power /-! ### Computing cardinality of various types -/ section computing section Function variable {α β : Type u} {β' : Type v} theorem mk_equiv_eq_zero_iff_lift_ne : #(α ≃ β') = 0 ↔ lift.{v} #α ≠ lift.{u} #β' := by rw [mk_eq_zero_iff, ← not_nonempty_iff, ← lift_mk_eq'] theorem mk_equiv_eq_zero_iff_ne : #(α ≃ β) = 0 ↔ #α ≠ #β := by rw [mk_equiv_eq_zero_iff_lift_ne, lift_id, lift_id] /-- This lemma makes lemmas assuming `Infinite α` applicable to the situation where we have `Infinite β` instead. -/ theorem mk_equiv_comm : #(α ≃ β') = #(β' ≃ α) := (ofBijective _ symm_bijective).cardinal_eq theorem mk_embedding_eq_zero_iff_lift_lt : #(α ↪ β') = 0 ↔ lift.{u} #β' < lift.{v} #α := by rw [mk_eq_zero_iff, ← not_nonempty_iff, ← lift_mk_le', not_le] theorem mk_embedding_eq_zero_iff_lt : #(α ↪ β) = 0 ↔ #β < #α := by rw [mk_embedding_eq_zero_iff_lift_lt, lift_lt] theorem mk_arrow_eq_zero_iff : #(α → β') = 0 ↔ #α ≠ 0 ∧ #β' = 0 := by simp_rw [mk_eq_zero_iff, mk_ne_zero_iff, isEmpty_fun] theorem mk_surjective_eq_zero_iff_lift : #{f : α → β' | Surjective f} = 0 ↔ lift.{v} #α < lift.{u} #β' ∨ (#α ≠ 0 ∧ #β' = 0) := by rw [← not_iff_not, not_or, not_lt, lift_mk_le', ← Ne, not_and_or, not_ne_iff, and_comm] simp_rw [mk_ne_zero_iff, mk_eq_zero_iff, nonempty_coe_sort, Set.Nonempty, mem_setOf, exists_surjective_iff, nonempty_fun] theorem mk_surjective_eq_zero_iff : #{f : α → β | Surjective f} = 0 ↔ #α < #β ∨ (#α ≠ 0 ∧ #β = 0) := by rw [mk_surjective_eq_zero_iff_lift, lift_lt] variable (α β') theorem mk_equiv_le_embedding : #(α ≃ β') ≤ #(α ↪ β') := ⟨⟨_, Equiv.toEmbedding_injective⟩⟩ theorem mk_embedding_le_arrow : #(α ↪ β') ≤ #(α → β') := ⟨⟨_, DFunLike.coe_injective⟩⟩ variable [Infinite α] {α β'} theorem mk_perm_eq_self_power : #(Equiv.Perm α) = #α ^ #α := ((mk_equiv_le_embedding α α).trans (mk_embedding_le_arrow α α)).antisymm <| by suffices Nonempty ((α → Bool) ↪ Equiv.Perm (α × Bool)) by obtain ⟨e⟩ : Nonempty (α ≃ α × Bool) := by erw [← Cardinal.eq, mk_prod, lift_uzero, mk_bool, lift_natCast, mul_two, add_eq_self (aleph0_le_mk α)] erw [← le_def, mk_arrow, lift_uzero, mk_bool, lift_natCast 2] at this rwa [← power_def, power_self_eq (aleph0_le_mk α), e.permCongr.cardinal_eq] refine ⟨⟨fun f ↦ Involutive.toPerm (fun x ↦ ⟨x.1, xor (f x.1) x.2⟩) fun x ↦ ?_, fun f g h ↦ ?_⟩⟩ · simp_rw [← Bool.xor_assoc, Bool.xor_self, Bool.false_xor] · ext a; rw [← (f a).xor_false, ← (g a).xor_false]; exact congr(($h ⟨a, false⟩).2) theorem mk_perm_eq_two_power : #(Equiv.Perm α) = 2 ^ #α := by rw [mk_perm_eq_self_power, power_self_eq (aleph0_le_mk α)] theorem mk_equiv_eq_arrow_of_lift_eq (leq : lift.{v} #α = lift.{u} #β') : #(α ≃ β') = #(α → β') := by obtain ⟨e⟩ := lift_mk_eq'.mp leq have e₁ := lift_mk_eq'.mpr ⟨.equivCongr (.refl α) e⟩ have e₂ := lift_mk_eq'.mpr ⟨.arrowCongr (.refl α) e⟩ rw [lift_id'.{u,v}] at e₁ e₂ rw [← e₁, ← e₂, lift_inj, mk_perm_eq_self_power, power_def] theorem mk_equiv_eq_arrow_of_eq (eq : #α = #β) : #(α ≃ β) = #(α → β) := mk_equiv_eq_arrow_of_lift_eq congr(lift $eq) theorem mk_equiv_of_lift_eq (leq : lift.{v} #α = lift.{u} #β') : #(α ≃ β') = 2 ^ lift.{v} #α := by erw [← (lift_mk_eq'.2 ⟨.equivCongr (.refl α) (lift_mk_eq'.1 leq).some⟩).trans (lift_id'.{u,v} _), lift_umax.{u,v}, mk_perm_eq_two_power, lift_power, lift_natCast]; rfl theorem mk_equiv_of_eq (eq : #α = #β) : #(α ≃ β) = 2 ^ #α := by rw [mk_equiv_of_lift_eq (lift_inj.mpr eq), lift_id] theorem mk_embedding_eq_arrow_of_lift_le (lle : lift.{u} #β' ≤ lift.{v} #α) : #(β' ↪ α) = #(β' → α) := (mk_embedding_le_arrow _ _).antisymm <| by conv_rhs => rw [← (Equiv.embeddingCongr (.refl _) (Cardinal.eq.mp <| mul_eq_self <| aleph0_le_mk α).some).cardinal_eq] obtain ⟨e⟩ := lift_mk_le'.mp lle exact ⟨⟨fun f ↦ ⟨fun b ↦ ⟨e b, f b⟩, fun _ _ h ↦ e.injective congr(Prod.fst $h)⟩, fun f g h ↦ funext fun b ↦ congr(Prod.snd <| $h b)⟩⟩ theorem mk_embedding_eq_arrow_of_le (le : #β ≤ #α) : #(β ↪ α) = #(β → α) := mk_embedding_eq_arrow_of_lift_le (lift_le.mpr le) theorem mk_surjective_eq_arrow_of_lift_le (lle : lift.{u} #β' ≤ lift.{v} #α) : #{f : α → β' | Surjective f} = #(α → β') := (mk_set_le _).antisymm <| have ⟨e⟩ : Nonempty (α ≃ α ⊕ β') := by simp_rw [← lift_mk_eq', mk_sum, lift_add, lift_lift]; rw [lift_umax.{u,v}, eq_comm] exact add_eq_left (aleph0_le_lift.mpr <| aleph0_le_mk α) lle ⟨⟨fun f ↦ ⟨fun a ↦ (e a).elim f id, fun b ↦ ⟨e.symm (.inr b), congr_arg _ (e.right_inv _)⟩⟩, fun f g h ↦ funext fun a ↦ by simpa only [e.apply_symm_apply] using congr_fun (Subtype.ext_iff.mp h) (e.symm <| .inl a)⟩⟩ theorem mk_surjective_eq_arrow_of_le (le : #β ≤ #α) : #{f : α → β | Surjective f} = #(α → β) := mk_surjective_eq_arrow_of_lift_le (lift_le.mpr le) end Function @[simp] theorem mk_list_eq_mk (α : Type u) [Infinite α] : #(List α) = #α := have H1 : ℵ₀ ≤ #α := aleph0_le_mk α Eq.symm <| le_antisymm ((le_def _ _).2 ⟨⟨fun a => [a], fun _ => by simp⟩⟩) <| calc #(List α) = sum fun n : ℕ => #α ^ (n : Cardinal.{u}) := mk_list_eq_sum_pow α _ ≤ sum fun _ : ℕ => #α := sum_le_sum _ _ fun n => pow_le H1 <| nat_lt_aleph0 n _ = #α := by simp [H1] theorem mk_list_eq_aleph0 (α : Type u) [Countable α] [Nonempty α] : #(List α) = ℵ₀ := mk_le_aleph0.antisymm (aleph0_le_mk _) theorem mk_list_eq_max_mk_aleph0 (α : Type u) [Nonempty α] : #(List α) = max #α ℵ₀ := by cases finite_or_infinite α · rw [mk_list_eq_aleph0, eq_comm, max_eq_right] exact mk_le_aleph0 · rw [mk_list_eq_mk, eq_comm, max_eq_left] exact aleph0_le_mk α theorem mk_list_le_max (α : Type u) : #(List α) ≤ max ℵ₀ #α := by cases finite_or_infinite α · exact mk_le_aleph0.trans (le_max_left _ _) · rw [mk_list_eq_mk] apply le_max_right @[simp] theorem mk_finset_of_infinite (α : Type u) [Infinite α] : #(Finset α) = #α := by classical exact Eq.symm <| le_antisymm (mk_le_of_injective fun _ _ => Finset.singleton_inj.1) <| calc #(Finset α) ≤ #(List α) := mk_le_of_surjective List.toFinset_surjective _ = #α := mk_list_eq_mk α theorem mk_bounded_set_le_of_infinite (α : Type u) [Infinite α] (c : Cardinal) : #{ t : Set α // #t ≤ c } ≤ #α ^ c := by rw [← add_one_eq (aleph0_le_mk α)] induction c using Cardinal.inductionOn with | _ β fapply mk_le_of_surjective · intro f use Sum.inl ⁻¹' range f refine le_trans (mk_preimage_of_injective _ _ fun x y => Sum.inl.inj) ?_ apply mk_range_le rintro ⟨s, ⟨g⟩⟩ classical use fun y => if h : ∃ x : s, g x = y then Sum.inl (Classical.choose h).val else Sum.inr (ULift.up 0) apply Subtype.eq; ext x constructor · rintro ⟨y, h⟩ dsimp only at h by_cases h' : ∃ z : s, g z = y · rw [dif_pos h'] at h cases Sum.inl.inj h exact (Classical.choose h').2 · rw [dif_neg h'] at h cases h · intro h have : ∃ z : s, g z = g ⟨x, h⟩ := ⟨⟨x, h⟩, rfl⟩ use g ⟨x, h⟩ dsimp only rw [dif_pos this] congr suffices Classical.choose this = ⟨x, h⟩ from congr_arg Subtype.val this apply g.2 exact Classical.choose_spec this theorem mk_bounded_set_le (α : Type u) (c : Cardinal) : #{ t : Set α // #t ≤ c } ≤ max #α ℵ₀ ^ c := by trans #{ t : Set ((ULift.{u} ℕ) ⊕ α) // #t ≤ c } · refine ⟨Embedding.subtypeMap ?_ ?_⟩ · apply Embedding.image use Sum.inr apply Sum.inr.inj intro s hs exact mk_image_le.trans hs apply (mk_bounded_set_le_of_infinite ((ULift.{u} ℕ) ⊕ α) c).trans rw [max_comm, ← add_eq_max] <;> rfl theorem mk_bounded_subset_le {α : Type u} (s : Set α) (c : Cardinal.{u}) : #{ t : Set α // t ⊆ s ∧ #t ≤ c } ≤ max #s ℵ₀ ^ c := by refine le_trans ?_ (mk_bounded_set_le s c) refine ⟨Embedding.codRestrict _ ?_ ?_⟩ · use fun t => (↑) ⁻¹' t.1 rintro ⟨t, ht1, ht2⟩ ⟨t', h1t', h2t'⟩ h apply Subtype.eq dsimp only at h ⊢ refine (preimage_eq_preimage' ?_ ?_).1 h <;> rw [Subtype.range_coe] <;> assumption rintro ⟨t, _, h2t⟩; exact (mk_preimage_of_injective _ _ Subtype.val_injective).trans h2t end computing /-! ### Properties of `compl` -/ section compl theorem mk_compl_of_infinite {α : Type*} [Infinite α] (s : Set α) (h2 : #s < #α) : #(sᶜ : Set α) = #α := by refine eq_of_add_eq_of_aleph0_le ?_ h2 (aleph0_le_mk α) exact mk_sum_compl s theorem mk_compl_finset_of_infinite {α : Type*} [Infinite α] (s : Finset α) : #((↑s)ᶜ : Set α) = #α := by apply mk_compl_of_infinite exact (finset_card_lt_aleph0 s).trans_le (aleph0_le_mk α) theorem mk_compl_eq_mk_compl_infinite {α : Type*} [Infinite α] {s t : Set α} (hs : #s < #α) (ht : #t < #α) : #(sᶜ : Set α) = #(tᶜ : Set α) := by rw [mk_compl_of_infinite s hs, mk_compl_of_infinite t ht] theorem mk_compl_eq_mk_compl_finite_lift {α : Type u} {β : Type v} [Finite α] {s : Set α} {t : Set β} (h1 : (lift.{max v w, u} #α) = (lift.{max u w, v} #β)) (h2 : lift.{max v w, u} #s = lift.{max u w, v} #t) : lift.{max v w} #(sᶜ : Set α) = lift.{max u w} #(tᶜ : Set β) := by cases nonempty_fintype α rcases lift_mk_eq.{u, v, w}.1 h1 with ⟨e⟩; letI : Fintype β := Fintype.ofEquiv α e replace h1 : Fintype.card α = Fintype.card β := (Fintype.ofEquiv_card _).symm classical lift s to Finset α using s.toFinite lift t to Finset β using t.toFinite simp only [Finset.coe_sort_coe, mk_fintype, Fintype.card_coe, lift_natCast, Nat.cast_inj] at h2 simp only [← Finset.coe_compl, Finset.coe_sort_coe, mk_coe_finset, Finset.card_compl, lift_natCast, h1, h2] theorem mk_compl_eq_mk_compl_finite {α β : Type u} [Finite α] {s : Set α} {t : Set β} (h1 : #α = #β) (h : #s = #t) : #(sᶜ : Set α) = #(tᶜ : Set β) := by rw [← lift_inj.{u, u}] apply mk_compl_eq_mk_compl_finite_lift.{u, u, u} <;> rwa [lift_inj] theorem mk_compl_eq_mk_compl_finite_same {α : Type u} [Finite α] {s t : Set α} (h : #s = #t) : #(sᶜ : Set α) = #(tᶜ : Set α) := mk_compl_eq_mk_compl_finite.{u} rfl h end compl /-! ### Extending an injection to an equiv -/ section extend theorem extend_function {α β : Type*} {s : Set α} (f : s ↪ β) (h : Nonempty ((sᶜ : Set α) ≃ ((range f)ᶜ : Set β))) : ∃ g : α ≃ β, ∀ x : s, g x = f x := by classical have := h; obtain ⟨g⟩ := this let h : α ≃ β := (Set.sumCompl (s : Set α)).symm.trans ((sumCongr (Equiv.ofInjective f f.2) g).trans (Set.sumCompl (range f))) refine ⟨h, ?_⟩; rintro ⟨x, hx⟩; simp [h, Set.sumCompl_symm_apply_of_mem, hx] theorem extend_function_finite {α : Type u} {β : Type v} [Finite α] {s : Set α} (f : s ↪ β) (h : Nonempty (α ≃ β)) : ∃ g : α ≃ β, ∀ x : s, g x = f x := by apply extend_function.{u, v} f obtain ⟨g⟩ := id h rw [← lift_mk_eq.{u, v, max u v}] at h rw [← lift_mk_eq.{u, v, max u v}, mk_compl_eq_mk_compl_finite_lift.{u, v, max u v} h] rw [mk_range_eq_lift.{u, v, max u v}]; exact f.2 theorem extend_function_of_lt {α β : Type*} {s : Set α} (f : s ↪ β) (hs : #s < #α) (h : Nonempty (α ≃ β)) : ∃ g : α ≃ β, ∀ x : s, g x = f x := by cases fintypeOrInfinite α · exact extend_function_finite f h · apply extend_function f obtain ⟨g⟩ := id h haveI := Infinite.of_injective _ g.injective rw [← lift_mk_eq'] at h ⊢ rwa [mk_compl_of_infinite s hs, mk_compl_of_infinite] rwa [← lift_lt, mk_range_eq_of_injective f.injective, ← h, lift_lt] end extend end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Basic.lean
import Mathlib.Data.Countable.Small import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Fintype.Powerset import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Data.Set.Countable import Mathlib.Logic.Small.Set import Mathlib.Logic.UnivLE import Mathlib.SetTheory.Cardinal.Order /-! # Basic results on cardinal numbers We provide a collection of basic results on cardinal numbers, in particular focusing on finite/countable/small types and sets. ## Main definitions * `Cardinal.powerlt a b` or `a ^< b` is defined as the supremum of `a ^ c` for `c < b`. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, aleph, Cantor's theorem, König's theorem, Konig's theorem -/ assert_not_exists Field open List (Vector) open Function Order Set noncomputable section universe u v w v' w' variable {α β : Type u} namespace Cardinal /-! ### Lifting cardinals to a higher universe -/ @[simp] lemma mk_preimage_down {s : Set α} : #(ULift.down.{v} ⁻¹' s) = lift.{v} (#s) := by rw [← mk_uLift, Cardinal.eq] constructor let f : ULift.down ⁻¹' s → ULift s := fun x ↦ ULift.up (restrictPreimage s ULift.down x) have : Function.Bijective f := ULift.up_bijective.comp (restrictPreimage_bijective _ (ULift.down_bijective)) exact Equiv.ofBijective f this -- `simp` can't figure out universe levels: normal form is `lift_mk_shrink'`. theorem lift_mk_shrink (α : Type u) [Small.{v} α] : Cardinal.lift.{max u w} #(Shrink.{v} α) = Cardinal.lift.{max v w} #α := lift_mk_eq.2 ⟨(equivShrink α).symm⟩ @[simp] theorem lift_mk_shrink' (α : Type u) [Small.{v} α] : Cardinal.lift.{u} #(Shrink.{v} α) = Cardinal.lift.{v} #α := lift_mk_shrink.{u, v, 0} α @[simp] theorem lift_mk_shrink'' (α : Type max u v) [Small.{v} α] : Cardinal.lift.{u} #(Shrink.{v} α) = #α := by rw [← lift_umax, lift_mk_shrink.{max u v, v, 0} α, ← lift_umax, lift_id] theorem prod_eq_of_fintype {α : Type u} [h : Fintype α] (f : α → Cardinal.{v}) : prod f = Cardinal.lift.{u} (∏ i, f i) := by revert f refine Fintype.induction_empty_option ?_ ?_ ?_ α (h_fintype := h) · intro α β hβ e h f letI := Fintype.ofEquiv β e.symm rw [← e.prod_comp f, ← h] exact mk_congr (e.piCongrLeft _).symm · intro f rw [Fintype.univ_pempty, Finset.prod_empty, lift_one, Cardinal.prod, mk_eq_one] · intro α hα h f rw [Cardinal.prod, mk_congr Equiv.piOptionEquivProd, mk_prod, lift_umax.{v, u}, mk_out, ← Cardinal.prod, lift_prod, Fintype.prod_option, lift_mul, ← h fun a => f (some a)] simp only [lift_id] /-! ### Basic cardinals -/ theorem le_one_iff_subsingleton {α : Type u} : #α ≤ 1 ↔ Subsingleton α := ⟨fun ⟨f⟩ => ⟨fun _ _ => f.injective (Subsingleton.elim _ _)⟩, fun ⟨h⟩ => ⟨fun _ => ULift.up 0, fun _ _ _ => h _ _⟩⟩ @[simp] theorem mk_le_one_iff_set_subsingleton {s : Set α} : #s ≤ 1 ↔ s.Subsingleton := le_one_iff_subsingleton.trans s.subsingleton_coe alias ⟨_, _root_.Set.Subsingleton.cardinalMk_le_one⟩ := mk_le_one_iff_set_subsingleton /-! ### Order properties -/ theorem one_lt_iff_nontrivial {α : Type u} : 1 < #α ↔ Nontrivial α := by rw [← not_le, le_one_iff_subsingleton, ← not_nontrivial_iff_subsingleton, Classical.not_not] lemma sInf_eq_zero_iff {s : Set Cardinal} : sInf s = 0 ↔ s = ∅ ∨ ∃ a ∈ s, a = 0 := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rcases s.eq_empty_or_nonempty with rfl | hne · exact Or.inl rfl · exact Or.inr ⟨sInf s, csInf_mem hne, h⟩ · rcases h with rfl | ⟨a, ha, rfl⟩ · exact Cardinal.sInf_empty · exact eq_bot_iff.2 (csInf_le' ha) lemma iInf_eq_zero_iff {ι : Sort*} {f : ι → Cardinal} : (⨅ i, f i) = 0 ↔ IsEmpty ι ∨ ∃ i, f i = 0 := by simp [iInf, sInf_eq_zero_iff] /-- A variant of `ciSup_of_empty` but with `0` on the RHS for convenience -/ protected theorem iSup_of_empty {ι} (f : ι → Cardinal) [IsEmpty ι] : iSup f = 0 := ciSup_of_empty f @[simp] theorem lift_sInf (s : Set Cardinal) : lift.{u, v} (sInf s) = sInf (lift.{u, v} '' s) := by rcases eq_empty_or_nonempty s with (rfl | hs) · simp · exact lift_monotone.map_csInf hs @[simp] theorem lift_iInf {ι} (f : ι → Cardinal) : lift.{u, v} (iInf f) = ⨅ i, lift.{u, v} (f i) := by unfold iInf convert lift_sInf (range f) simp_rw [← comp_apply (f := lift), range_comp] end Cardinal /-! ### Small sets of cardinals -/ namespace Cardinal instance small_Iic (a : Cardinal.{u}) : Small.{u} (Iic a) := by rw [← mk_out a] apply @small_of_surjective (Set a.out) (Iic #a.out) _ fun x => ⟨#x, mk_set_le x⟩ rintro ⟨x, hx⟩ simpa using le_mk_iff_exists_set.1 hx instance small_Iio (a : Cardinal.{u}) : Small.{u} (Iio a) := small_subset Iio_subset_Iic_self instance small_Icc (a b : Cardinal.{u}) : Small.{u} (Icc a b) := small_subset Icc_subset_Iic_self instance small_Ico (a b : Cardinal.{u}) : Small.{u} (Ico a b) := small_subset Ico_subset_Iio_self instance small_Ioc (a b : Cardinal.{u}) : Small.{u} (Ioc a b) := small_subset Ioc_subset_Iic_self instance small_Ioo (a b : Cardinal.{u}) : Small.{u} (Ioo a b) := small_subset Ioo_subset_Iio_self /-- A set of cardinals is bounded above iff it's small, i.e. it corresponds to a usual ZFC set. -/ theorem bddAbove_iff_small {s : Set Cardinal.{u}} : BddAbove s ↔ Small.{u} s := ⟨fun ⟨a, ha⟩ => @small_subset _ (Iic a) s (fun _ h => ha h) _, by rintro ⟨ι, ⟨e⟩⟩ use sum.{u, u} fun x ↦ e.symm x intro a ha simpa using le_sum (fun x ↦ e.symm x) (e ⟨a, ha⟩)⟩ theorem bddAbove_of_small (s : Set Cardinal.{u}) [h : Small.{u} s] : BddAbove s := bddAbove_iff_small.2 h theorem bddAbove_range {ι : Type*} [Small.{u} ι] (f : ι → Cardinal.{u}) : BddAbove (Set.range f) := bddAbove_of_small _ theorem bddAbove_image (f : Cardinal.{u} → Cardinal.{max u v}) {s : Set Cardinal.{u}} (hs : BddAbove s) : BddAbove (f '' s) := by rw [bddAbove_iff_small] at hs ⊢ exact small_lift _ theorem bddAbove_range_comp {ι : Type u} {f : ι → Cardinal.{v}} (hf : BddAbove (range f)) (g : Cardinal.{v} → Cardinal.{max v w}) : BddAbove (range (g ∘ f)) := by rw [range_comp] exact bddAbove_image g hf /-- The type of cardinals in universe `u` is not `Small.{u}`. This is a version of the Burali-Forti paradox. -/ theorem _root_.not_small_cardinal : ¬ Small.{u} Cardinal.{max u v} := by intro h have := small_lift.{_, v} Cardinal.{max u v} rw [← small_univ_iff, ← bddAbove_iff_small] at this exact not_bddAbove_univ this instance uncountable : Uncountable Cardinal.{u} := Uncountable.of_not_small not_small_cardinal.{u} /-! ### Bounds on suprema -/ theorem sum_le_lift_mk_mul_iSup_lift {ι : Type u} (f : ι → Cardinal.{v}) : sum f ≤ lift #ι * ⨆ i, lift (f i) := by rw [← (sum f).lift_id, lift_sum, ← lift_umax.{u, v}, ← (⨆ i, lift (f i)).lift_id, lift_umax.{max v u, u}, ← sum_const] refine sum_le_sum _ _ fun i => ?_ rw [lift_umax.{v, u}] exact le_ciSup (bddAbove_of_small _) i theorem sum_le_lift_mk_mul_iSup {ι : Type u} (f : ι → Cardinal.{max u v}) : sum f ≤ lift #ι * ⨆ i, f i := by simpa [← lift_umax] using sum_le_lift_mk_mul_iSup_lift f theorem sum_le_mk_mul_iSup {ι : Type u} (f : ι → Cardinal.{u}) : sum f ≤ #ι * ⨆ i, f i := by simpa using sum_le_lift_mk_mul_iSup_lift f @[deprecated (since := "2025-09-04")] alias sum_le_iSup_lift := sum_le_lift_mk_mul_iSup @[deprecated (since := "2025-09-04")] alias sum_le_iSup := sum_le_mk_mul_iSup /-- The lift of a supremum is the supremum of the lifts. -/ theorem lift_sSup {s : Set Cardinal} (hs : BddAbove s) : lift.{u} (sSup s) = sSup (lift.{u} '' s) := by apply ((le_csSup_iff' (bddAbove_image.{_,u} _ hs)).2 fun c hc => _).antisymm (csSup_le' _) · intro c hc by_contra h obtain ⟨d, rfl⟩ := Cardinal.mem_range_lift_of_le (not_le.1 h).le simp_rw [lift_le] at h hc rw [csSup_le_iff' hs] at h exact h fun a ha => lift_le.1 <| hc (mem_image_of_mem _ ha) · rintro i ⟨j, hj, rfl⟩ exact lift_le.2 (le_csSup hs hj) /-- The lift of a supremum is the supremum of the lifts. -/ theorem lift_iSup {ι : Type v} {f : ι → Cardinal.{w}} (hf : BddAbove (range f)) : lift.{u} (iSup f) = ⨆ i, lift.{u} (f i) := by rw [iSup, iSup, lift_sSup hf, ← range_comp] simp [Function.comp_def] /-- To prove that the lift of a supremum is bounded by some cardinal `t`, it suffices to show that the lift of each cardinal is bounded by `t`. -/ theorem lift_iSup_le {ι : Type v} {f : ι → Cardinal.{w}} {t : Cardinal} (hf : BddAbove (range f)) (w : ∀ i, lift.{u} (f i) ≤ t) : lift.{u} (iSup f) ≤ t := by rw [lift_iSup hf] exact ciSup_le' w @[simp] theorem lift_iSup_le_iff {ι : Type v} {f : ι → Cardinal.{w}} (hf : BddAbove (range f)) {t : Cardinal} : lift.{u} (iSup f) ≤ t ↔ ∀ i, lift.{u} (f i) ≤ t := by rw [lift_iSup hf] exact ciSup_le_iff' (bddAbove_range_comp.{_,_,u} hf _) /-- To prove an inequality between the lifts to a common universe of two different supremums, it suffices to show that the lift of each cardinal from the smaller supremum if bounded by the lift of some cardinal from the larger supremum. -/ theorem lift_iSup_le_lift_iSup {ι : Type v} {ι' : Type v'} {f : ι → Cardinal.{w}} {f' : ι' → Cardinal.{w'}} (hf : BddAbove (range f)) (hf' : BddAbove (range f')) {g : ι → ι'} (h : ∀ i, lift.{w'} (f i) ≤ lift.{w} (f' (g i))) : lift.{w'} (iSup f) ≤ lift.{w} (iSup f') := by rw [lift_iSup hf, lift_iSup hf'] exact ciSup_mono' (bddAbove_range_comp.{_,_,w} hf' _) fun i => ⟨_, h i⟩ /-- A variant of `lift_iSup_le_lift_iSup` with universes specialized via `w = v` and `w' = v'`. This is sometimes necessary to avoid universe unification issues. -/ theorem lift_iSup_le_lift_iSup' {ι : Type v} {ι' : Type v'} {f : ι → Cardinal.{v}} {f' : ι' → Cardinal.{v'}} (hf : BddAbove (range f)) (hf' : BddAbove (range f')) (g : ι → ι') (h : ∀ i, lift.{v'} (f i) ≤ lift.{v} (f' (g i))) : lift.{v'} (iSup f) ≤ lift.{v} (iSup f') := lift_iSup_le_lift_iSup hf hf' h theorem lift_iSup_le_sum {ι : Type u} [Small.{v} ι] (f : ι → Cardinal.{v}) : lift (⨆ i, f i) ≤ sum f := by rw [lift_iSup (bddAbove_of_small _)] exact ciSup_le' fun i => lift_le_sum f i /-! ### Properties about the cast from `ℕ` -/ theorem mk_finset_of_fintype [Fintype α] : #(Finset α) = 2 ^ Fintype.card α := by simp @[norm_cast] theorem nat_succ (n : ℕ) : (n.succ : Cardinal) = succ ↑n := by rw [Nat.cast_succ] refine (add_one_le_succ _).antisymm (succ_le_of_lt ?_) rw [← Nat.cast_succ] exact Nat.cast_lt.2 (Nat.lt_succ_self _) lemma succ_natCast (n : ℕ) : Order.succ (n : Cardinal) = n + 1 := by rw [← Cardinal.nat_succ] norm_cast lemma natCast_add_one_le_iff {n : ℕ} {c : Cardinal} : n + 1 ≤ c ↔ n < c := by rw [← Order.succ_le_iff, Cardinal.succ_natCast] lemma two_le_iff_one_lt {c : Cardinal} : 2 ≤ c ↔ 1 < c := by convert natCast_add_one_le_iff norm_cast @[simp] theorem succ_zero : succ (0 : Cardinal) = 1 := by norm_cast -- This works generally to prove inequalities between numeric cardinals. theorem one_lt_two : (1 : Cardinal) < 2 := by norm_cast theorem exists_finset_le_card (α : Type*) (n : ℕ) (h : n ≤ #α) : ∃ s : Finset α, n ≤ s.card := by obtain hα|hα := finite_or_infinite α · let hα := Fintype.ofFinite α use Finset.univ simpa only [mk_fintype, Nat.cast_le] using h · obtain ⟨s, hs⟩ := Infinite.exists_subset_card_eq α n exact ⟨s, hs.ge⟩ theorem card_le_of {α : Type u} {n : ℕ} (H : ∀ s : Finset α, s.card ≤ n) : #α ≤ n := by contrapose! H apply exists_finset_le_card α (n + 1) simpa only [nat_succ, succ_le_iff] using H theorem cantor' (a) {b : Cardinal} (hb : 1 < b) : a < b ^ a := by rw [← succ_le_iff, (by norm_cast : succ (1 : Cardinal) = 2)] at hb exact (cantor a).trans_le (power_le_power_right hb) theorem one_le_iff_pos {c : Cardinal} : 1 ≤ c ↔ 0 < c := by rw [← succ_zero, succ_le_iff] theorem one_le_iff_ne_zero {c : Cardinal} : 1 ≤ c ↔ c ≠ 0 := by rw [one_le_iff_pos, pos_iff_ne_zero] @[simp] theorem lt_one_iff_zero {c : Cardinal} : c < 1 ↔ c = 0 := by simpa using lt_succ_bot_iff (a := c) /-! ### Properties about `aleph0` -/ theorem nat_lt_aleph0 (n : ℕ) : (n : Cardinal.{u}) < ℵ₀ := succ_le_iff.1 (by rw [← nat_succ, ← lift_mk_fin, aleph0, lift_mk_le.{u}] exact ⟨⟨(↑), fun a b => Fin.ext⟩⟩) @[simp] theorem one_lt_aleph0 : 1 < ℵ₀ := by simpa using nat_lt_aleph0 1 @[simp] theorem one_le_aleph0 : 1 ≤ ℵ₀ := one_lt_aleph0.le theorem lt_aleph0 {c : Cardinal} : c < ℵ₀ ↔ ∃ n : ℕ, c = n := ⟨fun h => by rcases lt_lift_iff.1 h with ⟨c, h', rfl⟩ rcases le_mk_iff_exists_set.1 h'.1 with ⟨S, rfl⟩ suffices S.Finite by lift S to Finset ℕ using this simp contrapose! h' haveI := Infinite.to_subtype h' exact ⟨Infinite.natEmbedding S⟩, fun ⟨_, e⟩ => e.symm ▸ nat_lt_aleph0 _⟩ lemma succ_eq_of_lt_aleph0 {c : Cardinal} (h : c < ℵ₀) : Order.succ c = c + 1 := by obtain ⟨n, hn⟩ := Cardinal.lt_aleph0.mp h rw [hn, succ_natCast] theorem aleph0_le {c : Cardinal} : ℵ₀ ≤ c ↔ ∀ n : ℕ, ↑n ≤ c := ⟨fun h _ => (nat_lt_aleph0 _).le.trans h, fun h => le_of_not_gt fun hn => by rcases lt_aleph0.1 hn with ⟨n, rfl⟩ exact (Nat.lt_succ_self _).not_ge (Nat.cast_le.1 (h (n + 1)))⟩ theorem isSuccPrelimit_aleph0 : IsSuccPrelimit ℵ₀ := isSuccPrelimit_of_succ_lt fun a ha => by rcases lt_aleph0.1 ha with ⟨n, rfl⟩ rw [← nat_succ] apply nat_lt_aleph0 theorem isSuccLimit_aleph0 : IsSuccLimit ℵ₀ := by rw [Cardinal.isSuccLimit_iff] exact ⟨aleph0_ne_zero, isSuccPrelimit_aleph0⟩ lemma not_isSuccLimit_natCast : (n : ℕ) → ¬ IsSuccLimit (n : Cardinal.{u}) | 0, e => e.1 isMin_bot | Nat.succ n, e => Order.not_isSuccPrelimit_succ _ (nat_succ n ▸ e.2) theorem not_isSuccLimit_of_lt_aleph0 {c : Cardinal} (h : c < ℵ₀) : ¬ IsSuccLimit c := by obtain ⟨n, rfl⟩ := lt_aleph0.1 h exact not_isSuccLimit_natCast n theorem aleph0_le_of_isSuccLimit {c : Cardinal} (h : IsSuccLimit c) : ℵ₀ ≤ c := by contrapose! h exact not_isSuccLimit_of_lt_aleph0 h theorem isStrongLimit_aleph0 : IsStrongLimit ℵ₀ := by refine ⟨aleph0_ne_zero, fun x hx ↦ ?_⟩ obtain ⟨n, rfl⟩ := lt_aleph0.1 hx exact_mod_cast nat_lt_aleph0 _ theorem IsStrongLimit.aleph0_le {c} (H : IsStrongLimit c) : ℵ₀ ≤ c := aleph0_le_of_isSuccLimit H.isSuccLimit lemma exists_eq_natCast_of_iSup_eq {ι : Type u} [Nonempty ι] (f : ι → Cardinal.{v}) (hf : BddAbove (range f)) (n : ℕ) (h : ⨆ i, f i = n) : ∃ i, f i = n := exists_eq_of_iSup_eq_of_not_isSuccLimit.{u, v} f hf (not_isSuccLimit_natCast n) h @[simp] theorem range_natCast : range ((↑) : ℕ → Cardinal) = Iio ℵ₀ := ext fun x => by simp only [mem_Iio, mem_range, eq_comm, lt_aleph0] theorem mk_eq_nat_iff {α : Type u} {n : ℕ} : #α = n ↔ Nonempty (α ≃ Fin n) := by rw [← lift_mk_fin, ← lift_uzero #α, lift_mk_eq'] theorem lt_aleph0_iff_finite {α : Type u} : #α < ℵ₀ ↔ Finite α := by simp only [lt_aleph0, mk_eq_nat_iff, finite_iff_exists_equiv_fin] theorem lt_aleph0_iff_fintype {α : Type u} : #α < ℵ₀ ↔ Nonempty (Fintype α) := lt_aleph0_iff_finite.trans (finite_iff_nonempty_fintype _) theorem lt_aleph0_of_finite (α : Type u) [Finite α] : #α < ℵ₀ := lt_aleph0_iff_finite.2 ‹_› theorem lt_aleph0_iff_set_finite {S : Set α} : #S < ℵ₀ ↔ S.Finite := lt_aleph0_iff_finite.trans finite_coe_iff alias ⟨_, _root_.Set.Finite.lt_aleph0⟩ := lt_aleph0_iff_set_finite @[simp] theorem lt_aleph0_iff_subtype_finite {p : α → Prop} : #{ x // p x } < ℵ₀ ↔ { x | p x }.Finite := lt_aleph0_iff_set_finite theorem mk_le_aleph0_iff : #α ≤ ℵ₀ ↔ Countable α := by rw [countable_iff_nonempty_embedding, aleph0, ← lift_uzero #α, lift_mk_le'] @[simp] theorem mk_le_aleph0 [Countable α] : #α ≤ ℵ₀ := mk_le_aleph0_iff.mpr ‹_› theorem le_aleph0_iff_set_countable {s : Set α} : #s ≤ ℵ₀ ↔ s.Countable := mk_le_aleph0_iff alias ⟨_, _root_.Set.Countable.le_aleph0⟩ := le_aleph0_iff_set_countable @[simp] theorem le_aleph0_iff_subtype_countable {p : α → Prop} : #{ x // p x } ≤ ℵ₀ ↔ { x | p x }.Countable := le_aleph0_iff_set_countable theorem aleph0_lt_mk_iff : ℵ₀ < #α ↔ Uncountable α := by rw [← not_le, ← not_countable_iff, not_iff_not, mk_le_aleph0_iff] @[simp] theorem aleph0_lt_mk [Uncountable α] : ℵ₀ < #α := aleph0_lt_mk_iff.mpr ‹_› instance canLiftCardinalNat : CanLift Cardinal ℕ (↑) fun x => x < ℵ₀ := ⟨fun _ hx => let ⟨n, hn⟩ := lt_aleph0.mp hx ⟨n, hn.symm⟩⟩ theorem add_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a + b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [← Nat.cast_add]; apply nat_lt_aleph0 theorem add_lt_aleph0_iff {a b : Cardinal} : a + b < ℵ₀ ↔ a < ℵ₀ ∧ b < ℵ₀ := ⟨fun h => ⟨(self_le_add_right _ _).trans_lt h, (self_le_add_left _ _).trans_lt h⟩, fun ⟨h1, h2⟩ => add_lt_aleph0 h1 h2⟩ theorem aleph0_le_add_iff {a b : Cardinal} : ℵ₀ ≤ a + b ↔ ℵ₀ ≤ a ∨ ℵ₀ ≤ b := by simp only [← not_lt, add_lt_aleph0_iff, not_and_or] /-- See also `Cardinal.nsmul_lt_aleph0_iff_of_ne_zero` if you already have `n ≠ 0`. -/ theorem nsmul_lt_aleph0_iff {n : ℕ} {a : Cardinal} : n • a < ℵ₀ ↔ n = 0 ∨ a < ℵ₀ := by cases n with | zero => simpa using nat_lt_aleph0 0 | succ n => simp only [Nat.succ_ne_zero, false_or] induction n with | zero => simp | succ n ih => rw [succ_nsmul, add_lt_aleph0_iff, ih, and_self_iff] /-- See also `Cardinal.nsmul_lt_aleph0_iff` for a hypothesis-free version. -/ theorem nsmul_lt_aleph0_iff_of_ne_zero {n : ℕ} {a : Cardinal} (h : n ≠ 0) : n • a < ℵ₀ ↔ a < ℵ₀ := nsmul_lt_aleph0_iff.trans <| or_iff_right h theorem mul_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a * b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [← Nat.cast_mul]; apply nat_lt_aleph0 theorem mul_lt_aleph0_iff {a b : Cardinal} : a * b < ℵ₀ ↔ a = 0 ∨ b = 0 ∨ a < ℵ₀ ∧ b < ℵ₀ := by refine ⟨fun h => ?_, ?_⟩ · by_cases ha : a = 0 · exact Or.inl ha right by_cases hb : b = 0 · exact Or.inl hb right rw [← Ne, ← one_le_iff_ne_zero] at ha hb constructor · rw [← mul_one a] exact (mul_le_mul' le_rfl hb).trans_lt h · rw [← one_mul b] exact (mul_le_mul' ha le_rfl).trans_lt h rintro (rfl | rfl | ⟨ha, hb⟩) <;> simp only [*, mul_lt_aleph0, aleph0_pos, zero_mul, mul_zero] /-- See also `Cardinal.aleph0_le_mul_iff`. -/ theorem aleph0_le_mul_iff {a b : Cardinal} : ℵ₀ ≤ a * b ↔ a ≠ 0 ∧ b ≠ 0 ∧ (ℵ₀ ≤ a ∨ ℵ₀ ≤ b) := by let h := (@mul_lt_aleph0_iff a b).not rwa [not_lt, not_or, not_or, not_and_or, not_lt, not_lt] at h /-- See also `Cardinal.aleph0_le_mul_iff'`. -/ theorem aleph0_le_mul_iff' {a b : Cardinal.{u}} : ℵ₀ ≤ a * b ↔ a ≠ 0 ∧ ℵ₀ ≤ b ∨ ℵ₀ ≤ a ∧ b ≠ 0 := by have : ∀ {a : Cardinal.{u}}, ℵ₀ ≤ a → a ≠ 0 := fun a => ne_bot_of_le_ne_bot aleph0_ne_zero a simp only [aleph0_le_mul_iff, and_or_left, and_iff_right_of_imp this, @and_left_comm (a ≠ 0)] simp only [and_comm, or_comm] theorem mul_lt_aleph0_iff_of_ne_zero {a b : Cardinal} (ha : a ≠ 0) (hb : b ≠ 0) : a * b < ℵ₀ ↔ a < ℵ₀ ∧ b < ℵ₀ := by simp [mul_lt_aleph0_iff, ha, hb] theorem power_lt_aleph0 {a b : Cardinal} (ha : a < ℵ₀) (hb : b < ℵ₀) : a ^ b < ℵ₀ := match a, b, lt_aleph0.1 ha, lt_aleph0.1 hb with | _, _, ⟨m, rfl⟩, ⟨n, rfl⟩ => by rw [power_natCast, ← Nat.cast_pow]; apply nat_lt_aleph0 theorem eq_one_iff_unique {α : Type*} : #α = 1 ↔ Subsingleton α ∧ Nonempty α := calc #α = 1 ↔ #α ≤ 1 ∧ 1 ≤ #α := le_antisymm_iff _ ↔ Subsingleton α ∧ Nonempty α := le_one_iff_subsingleton.and (one_le_iff_ne_zero.trans mk_ne_zero_iff) theorem infinite_iff {α : Type u} : Infinite α ↔ ℵ₀ ≤ #α := by rw [← not_lt, lt_aleph0_iff_finite, not_finite_iff_infinite] lemma aleph0_le_mk_iff : ℵ₀ ≤ #α ↔ Infinite α := infinite_iff.symm lemma mk_lt_aleph0_iff : #α < ℵ₀ ↔ Finite α := by simp [← not_le, aleph0_le_mk_iff] @[simp] lemma mk_lt_aleph0 [Finite α] : #α < ℵ₀ := mk_lt_aleph0_iff.2 ‹_› @[simp] theorem aleph0_le_mk (α : Type u) [Infinite α] : ℵ₀ ≤ #α := infinite_iff.1 ‹_› @[simp] theorem mk_eq_aleph0 (α : Type*) [Countable α] [Infinite α] : #α = ℵ₀ := mk_le_aleph0.antisymm <| aleph0_le_mk _ theorem denumerable_iff {α : Type u} : Nonempty (Denumerable α) ↔ #α = ℵ₀ := ⟨fun ⟨h⟩ => mk_congr ((@Denumerable.eqv α h).trans Equiv.ulift.symm), fun h => by obtain ⟨f⟩ := Quotient.exact h exact ⟨Denumerable.mk' <| f.trans Equiv.ulift⟩⟩ theorem mk_denumerable (α : Type u) [Denumerable α] : #α = ℵ₀ := denumerable_iff.1 ⟨‹_›⟩ theorem _root_.Set.countable_infinite_iff_nonempty_denumerable {α : Type*} {s : Set α} : s.Countable ∧ s.Infinite ↔ Nonempty (Denumerable s) := by rw [nonempty_denumerable_iff, ← Set.infinite_coe_iff, countable_coe_iff] @[simp] theorem aleph0_add_aleph0 : ℵ₀ + ℵ₀ = ℵ₀ := mk_denumerable _ theorem aleph0_mul_aleph0 : ℵ₀ * ℵ₀ = ℵ₀ := mk_denumerable _ @[simp] theorem nat_mul_aleph0 {n : ℕ} (hn : n ≠ 0) : ↑n * ℵ₀ = ℵ₀ := le_antisymm (lift_mk_fin n ▸ mk_le_aleph0) <| le_mul_of_one_le_left (zero_le _) <| by rwa [← Nat.cast_one, Nat.cast_le, Nat.one_le_iff_ne_zero] @[simp] theorem aleph0_mul_nat {n : ℕ} (hn : n ≠ 0) : ℵ₀ * n = ℵ₀ := by rw [mul_comm, nat_mul_aleph0 hn] @[simp] theorem ofNat_mul_aleph0 {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) * ℵ₀ = ℵ₀ := nat_mul_aleph0 (NeZero.ne n) @[simp] theorem aleph0_mul_ofNat {n : ℕ} [Nat.AtLeastTwo n] : ℵ₀ * ofNat(n) = ℵ₀ := aleph0_mul_nat (NeZero.ne n) @[simp] theorem add_le_aleph0 {c₁ c₂ : Cardinal} : c₁ + c₂ ≤ ℵ₀ ↔ c₁ ≤ ℵ₀ ∧ c₂ ≤ ℵ₀ := ⟨fun h => ⟨le_self_add.trans h, le_add_self.trans h⟩, fun h => aleph0_add_aleph0 ▸ add_le_add h.1 h.2⟩ @[simp] theorem aleph0_add_nat (n : ℕ) : ℵ₀ + n = ℵ₀ := (add_le_aleph0.2 ⟨le_rfl, (nat_lt_aleph0 n).le⟩).antisymm le_self_add @[simp] theorem nat_add_aleph0 (n : ℕ) : ↑n + ℵ₀ = ℵ₀ := by rw [add_comm, aleph0_add_nat] @[simp] theorem ofNat_add_aleph0 {n : ℕ} [Nat.AtLeastTwo n] : ofNat(n) + ℵ₀ = ℵ₀ := nat_add_aleph0 n @[simp] theorem aleph0_add_ofNat {n : ℕ} [Nat.AtLeastTwo n] : ℵ₀ + ofNat(n) = ℵ₀ := aleph0_add_nat n theorem exists_nat_eq_of_le_nat {c : Cardinal} {n : ℕ} (h : c ≤ n) : ∃ m, m ≤ n ∧ c = m := by lift c to ℕ using h.trans_lt (nat_lt_aleph0 _) exact ⟨c, mod_cast h, rfl⟩ theorem mk_int : #ℤ = ℵ₀ := mk_denumerable ℤ theorem mk_pnat : #ℕ+ = ℵ₀ := mk_denumerable ℕ+ @[deprecated (since := "2025-04-27")] alias mk_pNat := mk_pnat /-! ### Cardinalities of basic sets and types -/ @[simp] theorem mk_additive : #(Additive α) = #α := rfl @[simp] theorem mk_multiplicative : #(Multiplicative α) = #α := rfl @[to_additive (attr := simp)] theorem mk_mulOpposite : #(MulOpposite α) = #α := mk_congr MulOpposite.opEquiv.symm theorem mk_singleton {α : Type u} (x : α) : #({x} : Set α) = 1 := mk_eq_one _ @[simp] theorem mk_vector (α : Type u) (n : ℕ) : #(List.Vector α n) = #α ^ n := (mk_congr (Equiv.vectorEquivFin α n)).trans <| by simp theorem mk_list_eq_sum_pow (α : Type u) : #(List α) = sum fun n : ℕ => #α ^ n := calc #(List α) = #(Σ n, List.Vector α n) := mk_congr (Equiv.sigmaFiberEquiv List.length).symm _ = sum fun n : ℕ => #α ^ n := by simp theorem mk_quot_le {α : Type u} {r : α → α → Prop} : #(Quot r) ≤ #α := mk_le_of_surjective Quot.exists_rep theorem mk_quotient_le {α : Type u} {s : Setoid α} : #(Quotient s) ≤ #α := mk_quot_le theorem mk_subtype_le_of_subset {α : Type u} {p q : α → Prop} (h : ∀ ⦃x⦄, p x → q x) : #(Subtype p) ≤ #(Subtype q) := ⟨Embedding.subtypeMap (Embedding.refl α) h⟩ theorem mk_emptyCollection (α : Type u) : #(∅ : Set α) = 0 := mk_eq_zero _ theorem mk_emptyCollection_iff {α : Type u} {s : Set α} : #s = 0 ↔ s = ∅ := by rw [mk_eq_zero_iff, isEmpty_coe_sort] lemma mk_set_ne_zero_iff {α : Type u} (s : Set α) : #s ≠ 0 ↔ s.Nonempty := by rw [mk_ne_zero_iff, nonempty_coe_sort] @[simp] theorem mk_univ {α : Type u} : #(@univ α) = #α := mk_congr (Equiv.Set.univ α) @[simp] lemma mk_setProd {α β : Type u} (s : Set α) (t : Set β) : #(s ×ˢ t) = #s * #t := by rw [mul_def, mk_congr (Equiv.Set.prod ..)] theorem mk_image_le {α β : Type u} {f : α → β} {s : Set α} : #(f '' s) ≤ #s := mk_le_of_surjective imageFactorization_surjective lemma mk_image2_le {α β γ : Type u} {f : α → β → γ} {s : Set α} {t : Set β} : #(image2 f s t) ≤ #s * #t := by rw [← image_uncurry_prod, ← mk_setProd] exact mk_image_le theorem mk_image_le_lift {α : Type u} {β : Type v} {f : α → β} {s : Set α} : lift.{u} #(f '' s) ≤ lift.{v} #s := lift_mk_le.{0}.mpr ⟨Embedding.ofSurjective _ imageFactorization_surjective⟩ theorem mk_range_le {α β : Type u} {f : α → β} : #(range f) ≤ #α := mk_le_of_surjective rangeFactorization_surjective theorem mk_range_le_lift {α : Type u} {β : Type v} {f : α → β} : lift.{u} #(range f) ≤ lift.{v} #α := lift_mk_le.{0}.mpr ⟨Embedding.ofSurjective _ rangeFactorization_surjective⟩ theorem mk_range_eq (f : α → β) (h : Injective f) : #(range f) = #α := mk_congr (Equiv.ofInjective f h).symm theorem mk_range_eq_lift {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : lift.{max u w} #(range f) = lift.{max v w} #α := lift_mk_eq.{v,u,w}.mpr ⟨(Equiv.ofInjective f hf).symm⟩ theorem mk_range_eq_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : lift.{u} #(range f) = lift.{v} #α := lift_mk_eq'.mpr ⟨(Equiv.ofInjective f hf).symm⟩ lemma lift_mk_le_lift_mk_of_injective {α : Type u} {β : Type v} {f : α → β} (hf : Injective f) : Cardinal.lift.{v} (#α) ≤ Cardinal.lift.{u} (#β) := by rw [← Cardinal.mk_range_eq_of_injective hf] exact Cardinal.lift_le.2 (Cardinal.mk_set_le _) lemma lift_mk_le_lift_mk_of_surjective {α : Type u} {β : Type v} {f : α → β} (hf : Surjective f) : Cardinal.lift.{u} (#β) ≤ Cardinal.lift.{v} (#α) := lift_mk_le_lift_mk_of_injective (injective_surjInv hf) theorem mk_image_eq_of_injOn {α β : Type u} (f : α → β) (s : Set α) (h : InjOn f s) : #(f '' s) = #s := mk_congr (Equiv.Set.imageOfInjOn f s h).symm theorem mk_image_eq_of_injOn_lift {α : Type u} {β : Type v} (f : α → β) (s : Set α) (h : InjOn f s) : lift.{u} #(f '' s) = lift.{v} #s := lift_mk_eq.{v, u, 0}.mpr ⟨(Equiv.Set.imageOfInjOn f s h).symm⟩ theorem mk_image_eq {α β : Type u} {f : α → β} {s : Set α} (hf : Injective f) : #(f '' s) = #s := mk_image_eq_of_injOn _ _ hf.injOn theorem mk_image_eq_lift {α : Type u} {β : Type v} (f : α → β) (s : Set α) (h : Injective f) : lift.{u} #(f '' s) = lift.{v} #s := mk_image_eq_of_injOn_lift _ _ h.injOn @[simp] theorem mk_image_embedding_lift {β : Type v} (f : α ↪ β) (s : Set α) : lift.{u} #(f '' s) = lift.{v} #s := mk_image_eq_lift _ _ f.injective @[simp] theorem mk_image_embedding (f : α ↪ β) (s : Set α) : #(f '' s) = #s := by simpa using mk_image_embedding_lift f s theorem mk_iUnion_le_sum_mk {α ι : Type u} {f : ι → Set α} : #(⋃ i, f i) ≤ sum fun i => #(f i) := calc #(⋃ i, f i) ≤ #(Σ i, f i) := mk_le_of_surjective (Set.sigmaToiUnion_surjective f) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_le_sum_mk_lift {α : Type u} {ι : Type v} {f : ι → Set α} : lift.{v} #(⋃ i, f i) ≤ sum fun i => #(f i) := calc lift.{v} #(⋃ i, f i) ≤ #(Σ i, f i) := mk_le_of_surjective <| ULift.up_surjective.comp (Set.sigmaToiUnion_surjective f) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_eq_sum_mk {α ι : Type u} {f : ι → Set α} (h : Pairwise (Disjoint on f)) : #(⋃ i, f i) = sum fun i => #(f i) := calc #(⋃ i, f i) = #(Σ i, f i) := mk_congr (Set.unionEqSigmaOfDisjoint h) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_eq_sum_mk_lift {α : Type u} {ι : Type v} {f : ι → Set α} (h : Pairwise (Disjoint on f)) : lift.{v} #(⋃ i, f i) = sum fun i => #(f i) := calc lift.{v} #(⋃ i, f i) = #(Σ i, f i) := mk_congr <| .trans Equiv.ulift (Set.unionEqSigmaOfDisjoint h) _ = sum fun i => #(f i) := mk_sigma _ theorem mk_iUnion_le {α ι : Type u} (f : ι → Set α) : #(⋃ i, f i) ≤ #ι * ⨆ i, #(f i) := mk_iUnion_le_sum_mk.trans (sum_le_mk_mul_iSup _) theorem mk_iUnion_le_lift {α : Type u} {ι : Type v} (f : ι → Set α) : lift.{v} #(⋃ i, f i) ≤ lift.{u} #ι * ⨆ i, lift.{v} #(f i) := by refine mk_iUnion_le_sum_mk_lift.trans <| Eq.trans_le ?_ (sum_le_lift_mk_mul_iSup _) rw [← lift_sum, lift_id'.{_,u}] theorem mk_sUnion_le {α : Type u} (A : Set (Set α)) : #(⋃₀ A) ≤ #A * ⨆ s : A, #s := by rw [sUnion_eq_iUnion] apply mk_iUnion_le theorem mk_biUnion_le {ι α : Type u} (A : ι → Set α) (s : Set ι) : #(⋃ x ∈ s, A x) ≤ #s * ⨆ x : s, #(A x.1) := by rw [biUnion_eq_iUnion] apply mk_iUnion_le theorem mk_biUnion_le_lift {α : Type u} {ι : Type v} (A : ι → Set α) (s : Set ι) : lift.{v} #(⋃ x ∈ s, A x) ≤ lift.{u} #s * ⨆ x : s, lift.{v} #(A x.1) := by rw [biUnion_eq_iUnion] apply mk_iUnion_le_lift theorem finset_card_lt_aleph0 (s : Finset α) : #(↑s : Set α) < ℵ₀ := lt_aleph0_of_finite _ theorem mk_set_eq_nat_iff_finset {α} {s : Set α} {n : ℕ} : #s = n ↔ ∃ t : Finset α, (t : Set α) = s ∧ t.card = n := by constructor · intro h lift s to Finset α using lt_aleph0_iff_set_finite.1 (h.symm ▸ nat_lt_aleph0 n) simpa using h · rintro ⟨t, rfl, rfl⟩ exact mk_coe_finset theorem mk_eq_nat_iff_finset {n : ℕ} : #α = n ↔ ∃ t : Finset α, (t : Set α) = univ ∧ t.card = n := by rw [← mk_univ, mk_set_eq_nat_iff_finset] theorem mk_eq_nat_iff_fintype {n : ℕ} : #α = n ↔ ∃ h : Fintype α, @Fintype.card α h = n := by rw [mk_eq_nat_iff_finset] constructor · rintro ⟨t, ht, hn⟩ exact ⟨⟨t, eq_univ_iff_forall.1 ht⟩, hn⟩ · rintro ⟨⟨t, ht⟩, hn⟩ exact ⟨t, eq_univ_iff_forall.2 ht, hn⟩ theorem mk_union_add_mk_inter {α : Type u} {S T : Set α} : #(S ∪ T : Set α) + #(S ∩ T : Set α) = #S + #T := by classical exact Quot.sound ⟨Equiv.Set.unionSumInter S T⟩ /-- The cardinality of a union is at most the sum of the cardinalities of the two sets. -/ theorem mk_union_le {α : Type u} (S T : Set α) : #(S ∪ T : Set α) ≤ #S + #T := @mk_union_add_mk_inter α S T ▸ self_le_add_right #(S ∪ T : Set α) #(S ∩ T : Set α) theorem mk_union_of_disjoint {α : Type u} {S T : Set α} (H : Disjoint S T) : #(S ∪ T : Set α) = #S + #T := by classical exact Quot.sound ⟨Equiv.Set.union H⟩ theorem mk_insert {α : Type u} {s : Set α} {a : α} (h : a ∉ s) : #(insert a s : Set α) = #s + 1 := by rw [← union_singleton, mk_union_of_disjoint, mk_singleton] simpa theorem mk_insert_le {α : Type u} {s : Set α} {a : α} : #(insert a s : Set α) ≤ #s + 1 := by by_cases h : a ∈ s · simp only [insert_eq_of_mem h, self_le_add_right] · rw [mk_insert h] theorem mk_sum_compl {α} (s : Set α) : #s + #(sᶜ : Set α) = #α := by classical exact mk_congr (Equiv.Set.sumCompl s) theorem mk_le_mk_of_subset {α} {s t : Set α} (h : s ⊆ t) : #s ≤ #t := ⟨Set.embeddingOfSubset s t h⟩ theorem mk_le_iff_forall_finset_subset_card_le {α : Type u} {n : ℕ} {t : Set α} : #t ≤ n ↔ ∀ s : Finset α, (s : Set α) ⊆ t → s.card ≤ n := by refine ⟨fun H s hs ↦ by simpa using (mk_le_mk_of_subset hs).trans H, fun H ↦ ?_⟩ apply card_le_of (fun s ↦ ?_) classical let u : Finset α := s.image Subtype.val have : u.card = s.card := Finset.card_image_of_injOn Subtype.coe_injective.injOn rw [← this] apply H simp only [u, Finset.coe_image, image_subset_iff, Subtype.coe_preimage_self, subset_univ] theorem mk_subtype_mono {p q : α → Prop} (h : ∀ x, p x → q x) : #{ x // p x } ≤ #{ x // q x } := ⟨embeddingOfSubset _ _ h⟩ theorem le_mk_diff_add_mk (S T : Set α) : #S ≤ #(S \ T : Set α) + #T := (mk_le_mk_of_subset <| subset_diff_union _ _).trans <| mk_union_le _ _ theorem mk_diff_add_mk {S T : Set α} (h : T ⊆ S) : #(S \ T : Set α) + #T = #S := by refine (mk_union_of_disjoint <| ?_).symm.trans <| by rw [diff_union_of_subset h] exact disjoint_sdiff_self_left lemma diff_nonempty_of_mk_lt_mk {S T : Set α} (h : #S < #T) : (T \ S).Nonempty := by rw [← mk_set_ne_zero_iff] intro h' exact h.not_ge ((le_mk_diff_add_mk T S).trans (by simp [h'])) lemma compl_nonempty_of_mk_lt_mk {S : Set α} (h : #S < #α) : Sᶜ.Nonempty := by rw [← mk_univ (α := α)] at h simpa [Set.compl_eq_univ_diff] using diff_nonempty_of_mk_lt_mk h theorem mk_union_le_aleph0 {α} {P Q : Set α} : #(P ∪ Q : Set α) ≤ ℵ₀ ↔ #P ≤ ℵ₀ ∧ #Q ≤ ℵ₀ := by simp only [le_aleph0_iff_subtype_countable, setOf_mem_eq, Set.union_def, ← countable_union] theorem mk_sep (s : Set α) (t : α → Prop) : #({ x ∈ s | t x } : Set α) = #{ x : s | t x.1 } := mk_congr (Equiv.Set.sep s t) theorem mk_preimage_of_injective_lift {α : Type u} {β : Type v} (f : α → β) (s : Set β) (h : Injective f) : lift.{v} #(f ⁻¹' s) ≤ lift.{u} #s := by rw [lift_mk_le.{0}] use Subtype.coind (fun x => f x.1) fun x => mem_preimage.mp x.2 apply Subtype.coind_injective; exact h.comp Subtype.val_injective theorem mk_preimage_of_subset_range_lift {α : Type u} {β : Type v} (f : α → β) (s : Set β) (h : s ⊆ range f) : lift.{u} #s ≤ lift.{v} #(f ⁻¹' s) := by rw [← image_preimage_eq_iff] at h nth_rewrite 1 [← h] apply mk_image_le_lift theorem mk_preimage_of_injective_of_subset_range_lift {β : Type v} (f : α → β) (s : Set β) (h : Injective f) (h2 : s ⊆ range f) : lift.{v} #(f ⁻¹' s) = lift.{u} #s := le_antisymm (mk_preimage_of_injective_lift f s h) (mk_preimage_of_subset_range_lift f s h2) theorem mk_preimage_of_injective_of_subset_range (f : α → β) (s : Set β) (h : Injective f) (h2 : s ⊆ range f) : #(f ⁻¹' s) = #s := by convert mk_preimage_of_injective_of_subset_range_lift.{u, u} f s h h2 using 1 <;> rw [lift_id] @[simp] theorem mk_preimage_equiv_lift {β : Type v} (f : α ≃ β) (s : Set β) : lift.{v} #(f ⁻¹' s) = lift.{u} #s := by apply mk_preimage_of_injective_of_subset_range_lift _ _ f.injective rw [f.range_eq_univ] exact fun _ _ ↦ ⟨⟩ @[simp] theorem mk_preimage_equiv (f : α ≃ β) (s : Set β) : #(f ⁻¹' s) = #s := by simpa using mk_preimage_equiv_lift f s theorem mk_preimage_of_injective (f : α → β) (s : Set β) (h : Injective f) : #(f ⁻¹' s) ≤ #s := by rw [← lift_id #(↑(f ⁻¹' s)), ← lift_id #(↑s)] exact mk_preimage_of_injective_lift f s h theorem mk_preimage_of_subset_range (f : α → β) (s : Set β) (h : s ⊆ range f) : #s ≤ #(f ⁻¹' s) := by rw [← lift_id #(↑(f ⁻¹' s)), ← lift_id #(↑s)] exact mk_preimage_of_subset_range_lift f s h theorem mk_subset_ge_of_subset_image_lift {α : Type u} {β : Type v} (f : α → β) {s : Set α} {t : Set β} (h : t ⊆ f '' s) : lift.{u} #t ≤ lift.{v} #({ x ∈ s | f x ∈ t } : Set α) := by rw [image_eq_range] at h convert mk_preimage_of_subset_range_lift _ _ h using 1 rw [mk_sep] rfl theorem mk_subset_ge_of_subset_image (f : α → β) {s : Set α} {t : Set β} (h : t ⊆ f '' s) : #t ≤ #({ x ∈ s | f x ∈ t } : Set α) := by rw [image_eq_range] at h convert mk_preimage_of_subset_range _ _ h using 1 rw [mk_sep] rfl theorem le_mk_iff_exists_subset {c : Cardinal} {α : Type u} {s : Set α} : c ≤ #s ↔ ∃ p : Set α, p ⊆ s ∧ #p = c := by rw [le_mk_iff_exists_set, ← Subtype.exists_set_subtype] apply exists_congr; intro t; rw [mk_image_eq]; apply Subtype.val_injective @[simp] theorem mk_range_inl {α : Type u} {β : Type v} : #(range (@Sum.inl α β)) = lift.{v} #α := by rw [← lift_id'.{u, v} #_, (Equiv.Set.rangeInl α β).lift_cardinal_eq, lift_umax.{u, v}] @[simp] theorem mk_range_inr {α : Type u} {β : Type v} : #(range (@Sum.inr α β)) = lift.{u} #β := by rw [← lift_id'.{v, u} #_, (Equiv.Set.rangeInr α β).lift_cardinal_eq, lift_umax.{v, u}] theorem two_le_iff : (2 : Cardinal) ≤ #α ↔ ∃ x y : α, x ≠ y := by rw [← Nat.cast_two, nat_succ, succ_le_iff, Nat.cast_one, one_lt_iff_nontrivial, nontrivial_iff] theorem two_le_iff' (x : α) : (2 : Cardinal) ≤ #α ↔ ∃ y : α, y ≠ x := by rw [two_le_iff, ← nontrivial_iff, nontrivial_iff_exists_ne x] theorem mk_eq_two_iff : #α = 2 ↔ ∃ x y : α, x ≠ y ∧ ({x, y} : Set α) = univ := by classical simp only [← @Nat.cast_two Cardinal, mk_eq_nat_iff_finset, Finset.card_eq_two] constructor · rintro ⟨t, ht, x, y, hne, rfl⟩ exact ⟨x, y, hne, by simpa using ht⟩ · rintro ⟨x, y, hne, h⟩ exact ⟨{x, y}, by simpa using h, x, y, hne, rfl⟩ theorem mk_eq_two_iff' (x : α) : #α = 2 ↔ ∃! y, y ≠ x := by rw [mk_eq_two_iff]; constructor · rintro ⟨a, b, hne, h⟩ simp only [eq_univ_iff_forall, mem_insert_iff, mem_singleton_iff] at h rcases h x with (rfl | rfl) exacts [⟨b, hne.symm, fun z => (h z).resolve_left⟩, ⟨a, hne, fun z => (h z).resolve_right⟩] · rintro ⟨y, hne, hy⟩ exact ⟨x, y, hne.symm, eq_univ_of_forall fun z => or_iff_not_imp_left.2 (hy z)⟩ theorem exists_notMem_of_length_lt {α : Type*} (l : List α) (h : ↑l.length < #α) : ∃ z : α, z ∉ l := by classical contrapose! h calc #α = #(Set.univ : Set α) := mk_univ.symm _ ≤ #l.toFinset := mk_le_mk_of_subset fun x _ => List.mem_toFinset.mpr (h x) _ = l.toFinset.card := Cardinal.mk_coe_finset _ ≤ l.length := Nat.cast_le.mpr (List.toFinset_card_le l) @[deprecated (since := "2025-05-23")] alias exists_not_mem_of_length_lt := exists_notMem_of_length_lt theorem three_le {α : Type*} (h : 3 ≤ #α) (x : α) (y : α) : ∃ z : α, z ≠ x ∧ z ≠ y := by have : ↑(3 : ℕ) ≤ #α := by simpa using h have : ↑(2 : ℕ) < #α := by rwa [← succ_le_iff, ← Cardinal.nat_succ] have := exists_notMem_of_length_lt [x, y] this simpa [not_or] using this /-! ### `powerlt` operation -/ /-- The function `a ^< b`, defined as the supremum of `a ^ c` for `c < b`. -/ def powerlt (a b : Cardinal.{u}) : Cardinal.{u} := ⨆ c : Iio b, a ^ (c : Cardinal) @[inherit_doc] infixl:80 " ^< " => powerlt theorem le_powerlt {b c : Cardinal.{u}} (a) (h : c < b) : (a^c) ≤ a ^< b := by refine le_ciSup (f := fun y : Iio b => a ^ (y : Cardinal)) ?_ ⟨c, h⟩ rw [← image_eq_range] exact bddAbove_image.{u, u} _ bddAbove_Iio theorem powerlt_le {a b c : Cardinal.{u}} : a ^< b ≤ c ↔ ∀ x < b, a ^ x ≤ c := by rw [powerlt, ciSup_le_iff'] · simp · rw [← image_eq_range] exact bddAbove_image.{u, u} _ bddAbove_Iio theorem powerlt_le_powerlt_left {a b c : Cardinal} (h : b ≤ c) : a ^< b ≤ a ^< c := powerlt_le.2 fun _ hx => le_powerlt a <| hx.trans_le h theorem powerlt_mono_left (a) : Monotone fun c => a ^< c := fun _ _ => powerlt_le_powerlt_left theorem powerlt_succ {a b : Cardinal} (h : a ≠ 0) : a ^< succ b = a ^ b := (powerlt_le.2 fun _ h' => power_le_power_left h <| le_of_lt_succ h').antisymm <| le_powerlt a (lt_succ b) theorem powerlt_min {a b c : Cardinal} : a ^< min b c = min (a ^< b) (a ^< c) := (powerlt_mono_left a).map_min theorem powerlt_max {a b c : Cardinal} : a ^< max b c = max (a ^< b) (a ^< c) := (powerlt_mono_left a).map_max theorem zero_powerlt {a : Cardinal} (h : a ≠ 0) : 0 ^< a = 1 := by apply (powerlt_le.2 fun c _ => zero_power_le _).antisymm rw [← power_zero] exact le_powerlt 0 (pos_iff_ne_zero.2 h) @[simp] theorem powerlt_zero {a : Cardinal} : a ^< 0 = 0 := by convert Cardinal.iSup_of_empty _ exact Subtype.isEmpty_of_false fun x => mem_Iio.not.mpr (Cardinal.zero_le x).not_gt end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/ENat.lean
import Mathlib.Algebra.Order.Hom.Ring import Mathlib.Data.ENat.Basic import Mathlib.SetTheory.Cardinal.Basic /-! # Conversion between `Cardinal` and `ℕ∞` In this file we define a coercion `Cardinal.ofENat : ℕ∞ → Cardinal` and a projection `Cardinal.toENat : Cardinal →+*o ℕ∞`. We also prove basic theorems about these definitions. ## Implementation notes We define `Cardinal.ofENat` as a function instead of a bundled homomorphism so that we can use it as a coercion and delaborate its application to `↑n`. We define `Cardinal.toENat` as a bundled homomorphism so that we can use all the theorems about homomorphisms without specializing them to this function. Since it is not registered as a coercion, the argument about delaboration does not apply. ## Keywords set theory, cardinals, extended natural numbers -/ assert_not_exists Field open Function Set universe u v namespace Cardinal /-- Coercion `ℕ∞ → Cardinal`. It sends natural numbers to natural numbers and `⊤` to `ℵ₀`. See also `Cardinal.ofENatHom` for a bundled homomorphism version. -/ @[coe] def ofENat : ℕ∞ → Cardinal | (n : ℕ) => n | ⊤ => ℵ₀ instance : Coe ENat Cardinal := ⟨Cardinal.ofENat⟩ @[simp, norm_cast] lemma ofENat_top : ofENat ⊤ = ℵ₀ := rfl @[simp, norm_cast] lemma ofENat_nat (n : ℕ) : ofENat n = n := rfl @[simp, norm_cast] lemma ofENat_zero : ofENat 0 = 0 := rfl @[simp, norm_cast] lemma ofENat_one : ofENat 1 = 1 := rfl @[simp, norm_cast] lemma ofENat_ofNat (n : ℕ) [n.AtLeastTwo] : ((ofNat(n) : ℕ∞) : Cardinal) = OfNat.ofNat n := rfl lemma ofENat_strictMono : StrictMono ofENat := WithTop.strictMono_iff.2 ⟨Nat.strictMono_cast, nat_lt_aleph0⟩ @[simp, norm_cast] lemma ofENat_lt_ofENat {m n : ℕ∞} : (m : Cardinal) < n ↔ m < n := ofENat_strictMono.lt_iff_lt @[gcongr, mono] alias ⟨_, ofENat_lt_ofENat_of_lt⟩ := ofENat_lt_ofENat @[simp, norm_cast] lemma ofENat_lt_aleph0 {m : ℕ∞} : (m : Cardinal) < ℵ₀ ↔ m < ⊤ := ofENat_lt_ofENat (n := ⊤) @[simp] lemma ofENat_lt_nat {m : ℕ∞} {n : ℕ} : ofENat m < n ↔ m < n := by norm_cast @[simp] lemma ofENat_lt_ofNat {m : ℕ∞} {n : ℕ} [n.AtLeastTwo] : ofENat m < ofNat(n) ↔ m < OfNat.ofNat n := ofENat_lt_nat @[simp] lemma nat_lt_ofENat {m : ℕ} {n : ℕ∞} : (m : Cardinal) < n ↔ m < n := by norm_cast @[simp] lemma ofENat_pos {m : ℕ∞} : 0 < (m : Cardinal) ↔ 0 < m := by norm_cast @[simp] lemma one_lt_ofENat {m : ℕ∞} : 1 < (m : Cardinal) ↔ 1 < m := by norm_cast @[simp, norm_cast] lemma ofNat_lt_ofENat {m : ℕ} [m.AtLeastTwo] {n : ℕ∞} : (ofNat(m) : Cardinal) < n ↔ OfNat.ofNat m < n := nat_lt_ofENat lemma ofENat_mono : Monotone ofENat := ofENat_strictMono.monotone @[simp, norm_cast] lemma ofENat_le_ofENat {m n : ℕ∞} : (m : Cardinal) ≤ n ↔ m ≤ n := ofENat_strictMono.le_iff_le @[gcongr, mono] alias ⟨_, ofENat_le_ofENat_of_le⟩ := ofENat_le_ofENat @[simp] lemma ofENat_le_aleph0 (n : ℕ∞) : ↑n ≤ ℵ₀ := ofENat_le_ofENat.2 le_top @[simp] lemma ofENat_le_nat {m : ℕ∞} {n : ℕ} : ofENat m ≤ n ↔ m ≤ n := by norm_cast @[simp] lemma ofENat_le_one {m : ℕ∞} : ofENat m ≤ 1 ↔ m ≤ 1 := by norm_cast @[simp] lemma ofENat_le_ofNat {m : ℕ∞} {n : ℕ} [n.AtLeastTwo] : ofENat m ≤ ofNat(n) ↔ m ≤ OfNat.ofNat n := ofENat_le_nat @[simp] lemma nat_le_ofENat {m : ℕ} {n : ℕ∞} : (m : Cardinal) ≤ n ↔ m ≤ n := by norm_cast @[simp] lemma one_le_ofENat {n : ℕ∞} : 1 ≤ (n : Cardinal) ↔ 1 ≤ n := by norm_cast @[simp] lemma ofNat_le_ofENat {m : ℕ} [m.AtLeastTwo] {n : ℕ∞} : (ofNat(m) : Cardinal) ≤ n ↔ OfNat.ofNat m ≤ n := nat_le_ofENat lemma ofENat_injective : Injective ofENat := ofENat_strictMono.injective @[simp, norm_cast] lemma ofENat_inj {m n : ℕ∞} : (m : Cardinal) = n ↔ m = n := ofENat_injective.eq_iff @[simp] lemma ofENat_eq_nat {m : ℕ∞} {n : ℕ} : (m : Cardinal) = n ↔ m = n := by norm_cast @[simp] lemma nat_eq_ofENat {m : ℕ} {n : ℕ∞} : (m : Cardinal) = n ↔ m = n := by norm_cast @[simp] lemma ofENat_eq_zero {m : ℕ∞} : (m : Cardinal) = 0 ↔ m = 0 := by norm_cast @[simp] lemma zero_eq_ofENat {m : ℕ∞} : 0 = (m : Cardinal) ↔ m = 0 := by norm_cast; apply eq_comm @[simp] lemma ofENat_eq_one {m : ℕ∞} : (m : Cardinal) = 1 ↔ m = 1 := by norm_cast @[simp] lemma one_eq_ofENat {m : ℕ∞} : 1 = (m : Cardinal) ↔ m = 1 := by norm_cast; apply eq_comm @[simp] lemma ofENat_eq_ofNat {m : ℕ∞} {n : ℕ} [n.AtLeastTwo] : (m : Cardinal) = ofNat(n) ↔ m = OfNat.ofNat n := ofENat_eq_nat @[simp] lemma ofNat_eq_ofENat {m : ℕ} {n : ℕ∞} [m.AtLeastTwo] : ofNat(m) = (n : Cardinal) ↔ OfNat.ofNat m = n := nat_eq_ofENat @[simp, norm_cast] lemma lift_ofENat : ∀ m : ℕ∞, lift.{u, v} m = m | (m : ℕ) => lift_natCast m | ⊤ => lift_aleph0 @[simp] lemma lift_lt_ofENat {x : Cardinal.{v}} {m : ℕ∞} : lift.{u} x < m ↔ x < m := by rw [← lift_ofENat.{u, v}, lift_lt] @[simp] lemma lift_le_ofENat {x : Cardinal.{v}} {m : ℕ∞} : lift.{u} x ≤ m ↔ x ≤ m := by rw [← lift_ofENat.{u, v}, lift_le] @[simp] lemma lift_eq_ofENat {x : Cardinal.{v}} {m : ℕ∞} : lift.{u} x = m ↔ x = m := by rw [← lift_ofENat.{u, v}, lift_inj] @[simp] lemma ofENat_lt_lift {x : Cardinal.{v}} {m : ℕ∞} : m < lift.{u} x ↔ m < x := by rw [← lift_ofENat.{u, v}, lift_lt] @[simp] lemma ofENat_le_lift {x : Cardinal.{v}} {m : ℕ∞} : m ≤ lift.{u} x ↔ m ≤ x := by rw [← lift_ofENat.{u, v}, lift_le] @[simp] lemma ofENat_eq_lift {x : Cardinal.{v}} {m : ℕ∞} : m = lift.{u} x ↔ m = x := by rw [← lift_ofENat.{u, v}, lift_inj] @[simp] lemma range_ofENat : range ofENat = Iic ℵ₀ := by refine (range_subset_iff.2 ofENat_le_aleph0).antisymm fun x (hx : x ≤ ℵ₀) ↦ ?_ rcases hx.lt_or_eq with hlt | rfl · lift x to ℕ using hlt exact mem_range_self (x : ℕ∞) · exact mem_range_self (⊤ : ℕ∞) instance : CanLift Cardinal ℕ∞ (↑) (· ≤ ℵ₀) where prf x := (Set.ext_iff.1 range_ofENat x).2 /-- Unbundled version of `Cardinal.toENat`. -/ noncomputable def toENatAux : Cardinal.{u} → ℕ∞ := extend Nat.cast Nat.cast fun _ ↦ ⊤ lemma toENatAux_nat (n : ℕ) : toENatAux n = n := Nat.cast_injective.extend_apply .. lemma toENatAux_zero : toENatAux 0 = 0 := toENatAux_nat 0 lemma toENatAux_eq_top {a : Cardinal} (ha : ℵ₀ ≤ a) : toENatAux a = ⊤ := extend_apply' _ _ _ fun ⟨n, hn⟩ ↦ ha.not_gt <| hn ▸ nat_lt_aleph0 n lemma toENatAux_ofENat : ∀ n : ℕ∞, toENatAux n = n | (n : ℕ) => toENatAux_nat n | ⊤ => toENatAux_eq_top le_rfl attribute [local simp] toENatAux_nat toENatAux_zero toENatAux_ofENat lemma toENatAux_gc : GaloisConnection (↑) toENatAux := fun n x ↦ by cases lt_or_ge x ℵ₀ with | inl hx => lift x to ℕ using hx; simp | inr hx => simp [toENatAux_eq_top hx, (ofENat_le_aleph0 n).trans hx] theorem toENatAux_le_nat {x : Cardinal} {n : ℕ} : toENatAux x ≤ n ↔ x ≤ n := by cases lt_or_ge x ℵ₀ with | inl hx => lift x to ℕ using hx; simp | inr hx => simp [toENatAux_eq_top hx, (nat_lt_aleph0 n).trans_le hx] lemma toENatAux_eq_nat {x : Cardinal} {n : ℕ} : toENatAux x = n ↔ x = n := by simp only [le_antisymm_iff, toENatAux_le_nat, ← toENatAux_gc _, ofENat_nat] lemma toENatAux_eq_zero {x : Cardinal} : toENatAux x = 0 ↔ x = 0 := toENatAux_eq_nat /-- Projection from cardinals to `ℕ∞`. Sends all infinite cardinals to `⊤`. We define this function as a bundled monotone ring homomorphism. -/ noncomputable def toENat : Cardinal.{u} →+*o ℕ∞ where toFun := toENatAux map_one' := toENatAux_nat 1 map_mul' x y := by wlog hle : x ≤ y; · rw [mul_comm, this y x (le_of_not_ge hle), mul_comm] cases lt_or_ge y ℵ₀ with | inl hy => lift x to ℕ using hle.trans_lt hy; lift y to ℕ using hy simp only [← Nat.cast_mul, toENatAux_nat] | inr hy => rcases eq_or_ne x 0 with rfl | hx · simp · simp only [toENatAux_eq_top hy] rw [toENatAux_eq_top, ENat.mul_top] · rwa [Ne, toENatAux_eq_zero] · exact le_mul_of_one_le_of_le (one_le_iff_ne_zero.2 hx) hy map_add' x y := by wlog hle : x ≤ y; · rw [add_comm, this y x (le_of_not_ge hle), add_comm] cases lt_or_ge y ℵ₀ with | inl hy => lift x to ℕ using hle.trans_lt hy; lift y to ℕ using hy simp only [← Nat.cast_add, toENatAux_nat] | inr hy => simp only [toENatAux_eq_top hy, add_top] exact toENatAux_eq_top <| le_add_left hy map_zero' := toENatAux_zero monotone' := toENatAux_gc.monotone_u /-- The coercion `Cardinal.ofENat` and the projection `Cardinal.toENat` form a Galois connection. See also `Cardinal.gciENat`. -/ lemma enat_gc : GaloisConnection (↑) toENat := toENatAux_gc @[simp] lemma toENat_ofENat (n : ℕ∞) : toENat n = n := toENatAux_ofENat n @[simp] lemma toENat_comp_ofENat : toENat ∘ (↑) = id := funext toENat_ofENat /-- The coercion `Cardinal.ofENat` and the projection `Cardinal.toENat` form a Galois coinsertion. -/ noncomputable def gciENat : GaloisCoinsertion (↑) toENat := enat_gc.toGaloisCoinsertion fun n ↦ (toENat_ofENat n).le lemma toENat_strictMonoOn : StrictMonoOn toENat (Iic ℵ₀) := by simp only [← range_ofENat, StrictMonoOn, forall_mem_range, toENat_ofENat, ofENat_lt_ofENat] exact fun _ _ ↦ id lemma toENat_injOn : InjOn toENat (Iic ℵ₀) := toENat_strictMonoOn.injOn lemma ofENat_toENat_le (a : Cardinal) : ↑(toENat a) ≤ a := enat_gc.l_u_le _ @[simp] lemma ofENat_toENat_eq_self {a : Cardinal} : toENat a = a ↔ a ≤ ℵ₀ := by rw [eq_comm, ← enat_gc.exists_eq_l] simpa only [mem_range, eq_comm] using Set.ext_iff.1 range_ofENat a @[simp] alias ⟨_, ofENat_toENat⟩ := ofENat_toENat_eq_self lemma toENat_nat (n : ℕ) : toENat n = n := map_natCast _ n @[simp] lemma toENat_le_nat {a : Cardinal} {n : ℕ} : toENat a ≤ n ↔ a ≤ n := toENatAux_le_nat @[simp] lemma toENat_eq_nat {a : Cardinal} {n : ℕ} : toENat a = n ↔ a = n := toENatAux_eq_nat @[simp] lemma toENat_eq_zero {a : Cardinal} : toENat a = 0 ↔ a = 0 := toENatAux_eq_zero @[simp] lemma toENat_le_one {a : Cardinal} : toENat a ≤ 1 ↔ a ≤ 1 := toENat_le_nat @[simp] lemma toENat_eq_one {a : Cardinal} : toENat a = 1 ↔ a = 1 := toENat_eq_nat @[simp] lemma toENat_le_ofNat {a : Cardinal} {n : ℕ} [n.AtLeastTwo] : toENat a ≤ ofNat(n) ↔ a ≤ OfNat.ofNat n := toENat_le_nat @[simp] lemma toENat_eq_ofNat {a : Cardinal} {n : ℕ} [n.AtLeastTwo] : toENat a = ofNat(n) ↔ a = OfNat.ofNat n := toENat_eq_nat @[simp] lemma toENat_eq_top {a : Cardinal} : toENat a = ⊤ ↔ ℵ₀ ≤ a := enat_gc.u_eq_top lemma toENat_ne_top {a : Cardinal} : toENat a ≠ ⊤ ↔ a < ℵ₀ := by simp @[simp] lemma toENat_lt_top {a : Cardinal} : toENat a < ⊤ ↔ a < ℵ₀ := by simp [lt_top_iff_ne_top] @[simp] theorem toENat_lift {a : Cardinal.{v}} : toENat (lift.{u} a) = toENat a := by cases le_total a ℵ₀ with | inl ha => lift a to ℕ∞ using ha; simp | inr ha => simp [toENat_eq_top.2, ha] theorem toENat_congr {α : Type u} {β : Type v} (e : α ≃ β) : toENat #α = toENat #β := by rw [← toENat_lift, lift_mk_eq.{_, _,v}.mpr ⟨e⟩, toENat_lift] lemma toENat_le_iff_of_le_aleph0 {c c' : Cardinal} (h : c ≤ ℵ₀) : toENat c ≤ toENat c' ↔ c ≤ c' := by lift c to ℕ∞ using h simp_rw [toENat_ofENat, enat_gc _] lemma toENat_le_iff_of_lt_aleph0 {c c' : Cardinal} (hc' : c' < ℵ₀) : toENat c ≤ toENat c' ↔ c ≤ c' := by lift c' to ℕ using hc' simp_rw [toENat_nat, ← toENat_le_nat] lemma toENat_eq_iff_of_le_aleph0 {c c' : Cardinal} (hc : c ≤ ℵ₀) (hc' : c' ≤ ℵ₀) : toENat c = toENat c' ↔ c = c' := toENat_strictMonoOn.injOn.eq_iff hc hc' @[simp, norm_cast] lemma ofENat_add (m n : ℕ∞) : ofENat (m + n) = m + n := by apply toENat_injOn <;> simp @[simp] lemma aleph0_add_ofENat (m : ℕ∞) : ℵ₀ + m = ℵ₀ := (ofENat_add ⊤ m).symm @[simp] lemma ofENat_add_aleph0 (m : ℕ∞) : m + ℵ₀ = ℵ₀ := by rw [add_comm, aleph0_add_ofENat] @[simp] lemma ofENat_mul_aleph0 {m : ℕ∞} (hm : m ≠ 0) : ↑m * ℵ₀ = ℵ₀ := by induction m with | top => exact aleph0_mul_aleph0 | coe m => rw [ofENat_nat, nat_mul_aleph0 (mod_cast hm)] @[simp] lemma aleph0_mul_ofENat {m : ℕ∞} (hm : m ≠ 0) : ℵ₀ * m = ℵ₀ := by rw [mul_comm, ofENat_mul_aleph0 hm] @[simp] lemma ofENat_mul (m n : ℕ∞) : ofENat (m * n) = m * n := toENat_injOn (by simp) (aleph0_mul_aleph0 ▸ mul_le_mul' (ofENat_le_aleph0 _) (ofENat_le_aleph0 _)) (by simp) /-- The coercion `Cardinal.ofENat` as a bundled homomorphism. -/ def ofENatHom : ℕ∞ →+*o Cardinal where toFun := (↑) map_one' := ofENat_one map_mul' := ofENat_mul map_zero' := ofENat_zero map_add' := ofENat_add monotone' := ofENat_mono end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/CountableCover.lean
import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.Order.Filter.Finite import Mathlib.Order.Filter.Map /-! # Cardinality of a set with a countable cover Assume that a set `t` is eventually covered by a countable family of sets, all with cardinality `≤ a`. Then `t` itself has cardinality at most `a`. This is proved in `Cardinal.mk_subtype_le_of_countable_eventually_mem`. Versions are also given when `t = univ`, and with `= a` instead of `≤ a`. -/ open Set Order Filter open scoped Cardinal namespace Cardinal universe u v /-- If a set `t` is eventually covered by a countable family of sets, all with cardinality at most `a`, then the cardinality of `t` is also bounded by `a`. Superseded by `mk_le_of_countable_eventually_mem` which does not assume that the indexing set lives in the same universe. -/ lemma mk_subtype_le_of_countable_eventually_mem_aux {α ι : Type u} {a : Cardinal} [Countable ι] {f : ι → Set α} {l : Filter ι} [NeBot l] {t : Set α} (ht : ∀ x ∈ t, ∀ᶠ i in l, x ∈ f i) (h'f : ∀ i, #(f i) ≤ a) : #t ≤ a := by rcases lt_or_ge a ℵ₀ with ha|ha /- case `a` finite. In this case, it suffices to show that any finite subset `s` of `t` has cardinality at most `a`. For this, we pick `i` such that `f i` contains all the points in `s`, and apply the assumption that the cardinality of `f i` is at most `a`. -/ · obtain ⟨n, rfl⟩ : ∃ (n : ℕ), a = n := lt_aleph0.1 ha apply mk_le_iff_forall_finset_subset_card_le.2 (fun s hs ↦ ?_) have A : ∀ x ∈ s, ∀ᶠ i in l, x ∈ f i := fun x hx ↦ ht x (hs hx) have B : ∀ᶠ i in l, ∀ x ∈ s, x ∈ f i := (s.eventually_all).2 A rcases B.exists with ⟨i, hi⟩ have : ∀ i, Fintype (f i) := fun i ↦ (lt_aleph0_iff_fintype.1 ((h'f i).trans_lt ha)).some let u : Finset α := (f i).toFinset have I1 : s.card ≤ u.card := by have : s ⊆ u := fun x hx ↦ by simpa only [u, Set.mem_toFinset] using hi x hx exact Finset.card_le_card this have I2 : (u.card : Cardinal) ≤ n := by convert h'f i; simp only [u, Set.toFinset_card, mk_fintype] exact I1.trans (Nat.cast_le.1 I2) -- case `a` infinite: · have : t ⊆ ⋃ i, f i := by intro x hx obtain ⟨i, hi⟩ : ∃ i, x ∈ f i := (ht x hx).exists exact mem_iUnion_of_mem i hi calc #t ≤ #(⋃ i, f i) := mk_le_mk_of_subset this _ ≤ sum (fun i ↦ #(f i)) := mk_iUnion_le_sum_mk _ ≤ sum (fun _ ↦ a) := sum_le_sum _ _ h'f _ = #ι * a := by simp _ ≤ ℵ₀ * a := by grw [mk_le_aleph0] _ = a := aleph0_mul_eq ha /-- If a set `t` is eventually covered by a countable family of sets, all with cardinality at most `a`, then the cardinality of `t` is also bounded by `a`. -/ lemma mk_subtype_le_of_countable_eventually_mem {α : Type u} {ι : Type v} {a : Cardinal} [Countable ι] {f : ι → Set α} {l : Filter ι} [NeBot l] {t : Set α} (ht : ∀ x ∈ t, ∀ᶠ i in l, x ∈ f i) (h'f : ∀ i, #(f i) ≤ a) : #t ≤ a := by let g : ULift.{u, v} ι → Set (ULift.{v, u} α) := (ULift.down ⁻¹' ·) ∘ f ∘ ULift.down suffices #(ULift.down.{v} ⁻¹' t) ≤ Cardinal.lift.{v, u} a by simpa let l' : Filter (ULift.{u} ι) := Filter.map ULift.up l have : NeBot l' := map_neBot apply mk_subtype_le_of_countable_eventually_mem_aux (ι := ULift.{u} ι) (l := l') (f := g) · intro x hx simpa only [Function.comp_apply, mem_preimage, eventually_map] using ht _ hx · intro i simpa [g] using h'f i.down /-- If a space is eventually covered by a countable family of sets, all with cardinality at most `a`, then the cardinality of the space is also bounded by `a`. -/ lemma mk_le_of_countable_eventually_mem {α : Type u} {ι : Type v} {a : Cardinal} [Countable ι] {f : ι → Set α} {l : Filter ι} [NeBot l] (ht : ∀ x, ∀ᶠ i in l, x ∈ f i) (h'f : ∀ i, #(f i) ≤ a) : #α ≤ a := by rw [← mk_univ] exact mk_subtype_le_of_countable_eventually_mem (l := l) (fun x _ ↦ ht x) h'f /-- If a space is eventually covered by a countable family of sets, all with cardinality `a`, then the cardinality of the space is also `a`. -/ lemma mk_of_countable_eventually_mem {α : Type u} {ι : Type v} {a : Cardinal} [Countable ι] {f : ι → Set α} {l : Filter ι} [NeBot l] (ht : ∀ x, ∀ᶠ i in l, x ∈ f i) (h'f : ∀ i, #(f i) = a) : #α = a := by apply le_antisymm · apply mk_le_of_countable_eventually_mem ht (fun i ↦ (h'f i).le) · obtain ⟨i⟩ : Nonempty ι := nonempty_of_neBot l rw [← (h'f i)] exact mk_set_le (f i) end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Cofinality.lean
import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.SetTheory.Ordinal.FixedPoint /-! # Cofinality This file contains the definition of cofinality of an order and an ordinal number. ## Main Definitions * `Order.cof r` is the cofinality of a reflexive order. This is the smallest cardinality of a subset `s` that is *cofinal*, i.e. `∀ x, ∃ y ∈ s, r x y`. * `Ordinal.cof o` is the cofinality of the ordinal `o` when viewed as a linear order. ## Main Statements * `Cardinal.lt_power_cof`: A consequence of König's theorem stating that `c < c ^ c.ord.cof` for `c ≥ ℵ₀`. ## Implementation Notes * The cofinality is defined for ordinals. If `c` is a cardinal number, its cofinality is `c.ord.cof`. -/ noncomputable section open Function Cardinal Set Order open scoped Ordinal universe u v w variable {α : Type u} {β : Type v} {r : α → α → Prop} {s : β → β → Prop} /-! ### Cofinality of orders -/ attribute [local instance] IsRefl.swap namespace Order /-- Cofinality of a reflexive order `≼`. This is the smallest cardinality of a subset `S : Set α` such that `∀ a, ∃ b ∈ S, a ≼ b`. -/ def cof (r : α → α → Prop) : Cardinal := sInf { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c } /-- The set in the definition of `Order.cof` is nonempty. -/ private theorem cof_nonempty (r : α → α → Prop) [IsRefl α r] : { c | ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = c }.Nonempty := ⟨_, Set.univ, fun a => ⟨a, ⟨⟩, refl _⟩, rfl⟩ theorem cof_le (r : α → α → Prop) {S : Set α} (h : ∀ a, ∃ b ∈ S, r a b) : cof r ≤ #S := csInf_le' ⟨S, h, rfl⟩ theorem le_cof [IsRefl α r] (c : Cardinal) : c ≤ cof r ↔ ∀ {S : Set α}, (∀ a, ∃ b ∈ S, r a b) → c ≤ #S := by rw [cof, le_csInf_iff'' (cof_nonempty r)] use fun H S h => H _ ⟨S, h, rfl⟩ rintro H d ⟨S, h, rfl⟩ exact H h end Order namespace RelIso private theorem cof_le_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) ≤ Cardinal.lift.{u} (Order.cof s) := by rw [Order.cof, Order.cof, lift_sInf, lift_sInf, le_csInf_iff'' ((Order.cof_nonempty s).image _)] rintro - ⟨-, ⟨u, H, rfl⟩, rfl⟩ apply csInf_le' refine ⟨_, ⟨f.symm '' u, fun a => ?_, rfl⟩, lift_mk_eq'.2 ⟨(f.symm.toEquiv.image u).symm⟩⟩ rcases H (f a) with ⟨b, hb, hb'⟩ refine ⟨f.symm b, mem_image_of_mem _ hb, f.map_rel_iff.1 ?_⟩ rwa [RelIso.apply_symm_apply] theorem cof_eq_lift [IsRefl β s] (f : r ≃r s) : Cardinal.lift.{v} (Order.cof r) = Cardinal.lift.{u} (Order.cof s) := have := f.toRelEmbedding.isRefl (f.cof_le_lift).antisymm (f.symm.cof_le_lift) theorem cof_eq {α β : Type u} {r : α → α → Prop} {s} [IsRefl β s] (f : r ≃r s) : Order.cof r = Order.cof s := lift_inj.1 (f.cof_eq_lift) end RelIso /-! ### Cofinality of ordinals -/ namespace Ordinal /-- Cofinality of an ordinal. This is the smallest cardinal of a subset `S` of the ordinal which is unbounded, in the sense `∀ a, ∃ b ∈ S, a ≤ b`. In particular, `cof 0 = 0` and `cof (succ o) = 1`. -/ def cof (o : Ordinal.{u}) : Cardinal.{u} := o.liftOn (fun a ↦ Order.cof (swap a.rᶜ)) fun _ _ ⟨f⟩ ↦ f.compl.swap.cof_eq theorem cof_type (r : α → α → Prop) [IsWellOrder α r] : (type r).cof = Order.cof (swap rᶜ) := rfl theorem cof_type_lt [LinearOrder α] [IsWellOrder α (· < ·)] : (@type α (· < ·) _).cof = @Order.cof α (· ≤ ·) := by rw [cof_type, compl_lt, swap_ge] theorem cof_eq_cof_toType (o : Ordinal) : o.cof = @Order.cof o.toType (· ≤ ·) := by conv_lhs => rw [← type_toType o, cof_type_lt] theorem le_cof_type [IsWellOrder α r] {c} : c ≤ cof (type r) ↔ ∀ S, Unbounded r S → c ≤ #S := (le_csInf_iff'' (Order.cof_nonempty _)).trans ⟨fun H S h => H _ ⟨S, h, rfl⟩, by rintro H d ⟨S, h, rfl⟩ exact H _ h⟩ theorem cof_type_le [IsWellOrder α r] {S : Set α} (h : Unbounded r S) : cof (type r) ≤ #S := le_cof_type.1 le_rfl S h theorem lt_cof_type [IsWellOrder α r] {S : Set α} : #S < cof (type r) → Bounded r S := by simpa using not_imp_not.2 cof_type_le theorem cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ #S = cof (type r) := csInf_mem (Order.cof_nonempty (swap rᶜ)) theorem ord_cof_eq (r : α → α → Prop) [IsWellOrder α r] : ∃ S, Unbounded r S ∧ type (Subrel r (· ∈ S)) = (cof (type r)).ord := by let ⟨S, hS, e⟩ := cof_eq r let ⟨s, _, e'⟩ := Cardinal.ord_eq S let T : Set α := { a | ∃ aS : a ∈ S, ∀ b : S, s b ⟨_, aS⟩ → r b a } suffices Unbounded r T by refine ⟨T, this, le_antisymm ?_ (Cardinal.ord_le.2 <| cof_type_le this)⟩ rw [← e, e'] refine (RelEmbedding.ofMonotone (fun a : T => (⟨a, let ⟨aS, _⟩ := a.2 aS⟩ : S)) fun a b h => ?_).ordinal_type_le rcases a with ⟨a, aS, ha⟩ rcases b with ⟨b, bS, hb⟩ change s ⟨a, _⟩ ⟨b, _⟩ refine ((trichotomous_of s _ _).resolve_left fun hn => ?_).resolve_left ?_ · exact asymm h (ha _ hn) · intro e injection e with e subst b exact irrefl _ h intro a have : { b : S | ¬r b a }.Nonempty := let ⟨b, bS, ba⟩ := hS a ⟨⟨b, bS⟩, ba⟩ let b := (IsWellFounded.wf : WellFounded s).min _ this have ba : ¬r b a := IsWellFounded.wf.min_mem _ this refine ⟨b, ⟨b.2, fun c => not_imp_not.1 fun h => ?_⟩, ba⟩ rw [show ∀ b : S, (⟨b, b.2⟩ : S) = b by intro b; cases b; rfl] exact IsWellFounded.wf.not_lt_min _ this (IsOrderConnected.neg_trans h ba) /-! ### Cofinality of suprema and least strict upper bounds -/ private theorem card_mem_cof {o} : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = o.card := ⟨_, _, lsub_typein o, mk_toType o⟩ /-- The set in the `lsub` characterization of `cof` is nonempty. -/ theorem cof_lsub_def_nonempty (o) : { a : Cardinal | ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a }.Nonempty := ⟨_, card_mem_cof⟩ theorem cof_eq_sInf_lsub (o : Ordinal.{u}) : cof o = sInf { a : Cardinal | ∃ (ι : Type u) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = a } := by refine le_antisymm (le_csInf (cof_lsub_def_nonempty o) ?_) (csInf_le' ?_) · rintro a ⟨ι, f, hf, rfl⟩ rw [← type_toType o] refine (cof_type_le fun a => ?_).trans (@mk_le_of_injective _ _ (fun s : typein ((· < ·) : o.toType → o.toType → Prop) ⁻¹' Set.range f => Classical.choose s.prop) fun s t hst => by let H := congr_arg f hst rwa [Classical.choose_spec s.prop, Classical.choose_spec t.prop, typein_inj, Subtype.coe_inj] at H) have := typein_lt_self a simp_rw [← hf, lt_lsub_iff] at this obtain ⟨i, hi⟩ := this refine ⟨enum (α := o.toType) (· < ·) ⟨f i, ?_⟩, ?_, ?_⟩ · rw [type_toType, ← hf] apply lt_lsub · rw [mem_preimage, typein_enum] exact mem_range_self i · rwa [← typein_le_typein, typein_enum] · rcases cof_eq (α := o.toType) (· < ·) with ⟨S, hS, hS'⟩ let f : S → Ordinal := fun s => typein LT.lt s.val refine ⟨S, f, le_antisymm (lsub_le fun i => typein_lt_self (o := o) i) (le_of_forall_lt fun a ha => ?_), by rwa [type_toType o] at hS'⟩ rw [← type_toType o] at ha rcases hS (enum (· < ·) ⟨a, ha⟩) with ⟨b, hb, hb'⟩ rw [← typein_le_typein, typein_enum] at hb' exact hb'.trans_lt (lt_lsub.{u, u} f ⟨b, hb⟩) @[simp] theorem lift_cof (o) : Cardinal.lift.{u, v} (cof o) = cof (Ordinal.lift.{u, v} o) := by refine inductionOn o fun α r _ ↦ ?_ rw [← type_uLift, cof_type, cof_type, ← Cardinal.lift_id'.{v, u} (Order.cof _), ← Cardinal.lift_umax] apply RelIso.cof_eq_lift ⟨Equiv.ulift.symm, _⟩ simp [swap] theorem cof_le_card (o) : cof o ≤ card o := by rw [cof_eq_sInf_lsub] exact csInf_le' card_mem_cof theorem cof_ord_le (c : Cardinal) : c.ord.cof ≤ c := by simpa using cof_le_card c.ord theorem ord_cof_le (o : Ordinal.{u}) : o.cof.ord ≤ o := (ord_le_ord.2 (cof_le_card o)).trans (ord_card_le o) theorem exists_lsub_cof (o : Ordinal) : ∃ (ι : _) (f : ι → Ordinal), lsub.{u, u} f = o ∧ #ι = cof o := by rw [cof_eq_sInf_lsub] exact csInf_mem (cof_lsub_def_nonempty o) theorem cof_lsub_le {ι} (f : ι → Ordinal) : cof (lsub.{u, u} f) ≤ #ι := by rw [cof_eq_sInf_lsub] exact csInf_le' ⟨ι, f, rfl, rfl⟩ theorem cof_lsub_le_lift {ι} (f : ι → Ordinal) : cof (lsub.{u, v} f) ≤ Cardinal.lift.{v, u} #ι := by rw [← mk_uLift.{u, v}] convert cof_lsub_le.{max u v} fun i : ULift.{v, u} ι => f i.down exact lsub_eq_of_range_eq.{u, max u v, max u v} (Set.ext fun x => ⟨fun ⟨i, hi⟩ => ⟨ULift.up.{v, u} i, hi⟩, fun ⟨i, hi⟩ => ⟨_, hi⟩⟩) theorem le_cof_iff_lsub {o : Ordinal} {a : Cardinal} : a ≤ cof o ↔ ∀ {ι} (f : ι → Ordinal), lsub.{u, u} f = o → a ≤ #ι := by rw [cof_eq_sInf_lsub] exact (le_csInf_iff'' (cof_lsub_def_nonempty o)).trans ⟨fun H ι f hf => H _ ⟨ι, f, hf, rfl⟩, fun H b ⟨ι, f, hf, hb⟩ => by rw [← hb] exact H _ hf⟩ theorem lsub_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : lsub.{u, v} f < c := lt_of_le_of_ne (lsub_le hf) fun h => by subst h exact (cof_lsub_le_lift.{u, v} f).not_gt hι theorem lsub_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → lsub.{u, u} f < c := lsub_lt_ord_lift (by rwa [(#ι).lift_id]) theorem cof_iSup_le_lift {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ Cardinal.lift.{v, u} #ι := by rw [← iSup_eq_lsub_iff_lt_iSup] at H rw [H] exact cof_lsub_le_lift f theorem cof_iSup_le {ι} {f : ι → Ordinal} (H : ∀ i, f i < iSup f) : cof (iSup f) ≤ #ι := by rw [← (#ι).lift_id] exact cof_iSup_le_lift H theorem iSup_lt_ord_lift {ι} {f : ι → Ordinal} {c : Ordinal} (hι : Cardinal.lift.{v, u} #ι < c.cof) (hf : ∀ i, f i < c) : iSup f < c := (iSup_le_lsub f).trans_lt (lsub_lt_ord_lift hι hf) theorem iSup_lt_ord {ι} {f : ι → Ordinal} {c : Ordinal} (hι : #ι < c.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_ord_lift (by rwa [(#ι).lift_id]) theorem iSup_lt_lift {ι} {f : ι → Cardinal} {c : Cardinal} (hι : Cardinal.lift.{v, u} #ι < c.ord.cof) (hf : ∀ i, f i < c) : iSup f < c := by rw [← ord_lt_ord, iSup_ord] refine iSup_lt_ord_lift hι fun i => ?_ rw [ord_lt_ord] apply hf theorem iSup_lt {ι} {f : ι → Cardinal} {c : Cardinal} (hι : #ι < c.ord.cof) : (∀ i, f i < c) → iSup f < c := iSup_lt_lift (by rwa [(#ι).lift_id]) theorem nfpFamily_lt_ord_lift {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : Cardinal.lift.{v, u} #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} (ha : a < c) : nfpFamily f a < c := by refine iSup_lt_ord_lift ((Cardinal.lift_le.2 (mk_list_le_max ι)).trans_lt ?_) fun l => ?_ · rw [lift_max] apply max_lt _ hc' rwa [Cardinal.lift_aleph0] · induction l with | nil => exact ha | cons i l H => exact hf _ _ H theorem nfpFamily_lt_ord {ι} {f : ι → Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hc' : #ι < cof c) (hf : ∀ (i), ∀ b < c, f i b < c) {a} : a < c → nfpFamily.{u, u} f a < c := nfpFamily_lt_ord_lift hc (by rwa [(#ι).lift_id]) hf theorem nfp_lt_ord {f : Ordinal → Ordinal} {c} (hc : ℵ₀ < cof c) (hf : ∀ i < c, f i < c) {a} : a < c → nfp f a < c := nfpFamily_lt_ord_lift hc (by simpa using Cardinal.one_lt_aleph0.trans hc) fun _ => hf theorem exists_blsub_cof (o : Ordinal) : ∃ f : ∀ a < (cof o).ord, Ordinal, blsub.{u, u} _ f = o := by rcases exists_lsub_cof o with ⟨ι, f, hf, hι⟩ rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf rw [← hι, hι'] exact ⟨_, hf⟩ theorem le_cof_iff_blsub {b : Ordinal} {a : Cardinal} : a ≤ cof b ↔ ∀ {o} (f : ∀ a < o, Ordinal), blsub.{u, u} o f = b → a ≤ o.card := le_cof_iff_lsub.trans ⟨fun H o f hf => by simpa using H _ hf, fun H ι f hf => by rcases Cardinal.ord_eq ι with ⟨r, hr, hι'⟩ rw [← @blsub_eq_lsub' ι r hr] at hf simpa using H _ hf⟩ theorem cof_blsub_le_lift {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← mk_toType o] exact cof_lsub_le_lift _ theorem cof_blsub_le {o} (f : ∀ a < o, Ordinal) : cof (blsub.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_blsub_le_lift f theorem blsub_lt_ord_lift {o : Ordinal.{u}} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, v} o f < c := lt_of_le_of_ne (blsub_le hf) fun h => ho.not_ge (by simpa [← iSup_ord, hf, h] using cof_blsub_le_lift.{u, v} f) theorem blsub_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) (hf : ∀ i hi, f i hi < c) : blsub.{u, u} o f < c := blsub_lt_ord_lift (by rwa [o.card.lift_id]) hf theorem cof_bsup_le_lift {o : Ordinal} {f : ∀ a < o, Ordinal} (H : ∀ i h, f i h < bsup.{u, v} o f) : cof (bsup.{u, v} o f) ≤ Cardinal.lift.{v, u} o.card := by rw [← bsup_eq_blsub_iff_lt_bsup.{u, v}] at H rw [H] exact cof_blsub_le_lift.{u, v} f theorem cof_bsup_le {o : Ordinal} {f : ∀ a < o, Ordinal} : (∀ i h, f i h < bsup.{u, u} o f) → cof (bsup.{u, u} o f) ≤ o.card := by rw [← o.card.lift_id] exact cof_bsup_le_lift theorem bsup_lt_ord_lift {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : Cardinal.lift.{v, u} o.card < c.cof) (hf : ∀ i hi, f i hi < c) : bsup.{u, v} o f < c := (bsup_le_blsub f).trans_lt (blsub_lt_ord_lift ho hf) theorem bsup_lt_ord {o : Ordinal} {f : ∀ a < o, Ordinal} {c : Ordinal} (ho : o.card < c.cof) : (∀ i hi, f i hi < c) → bsup.{u, u} o f < c := bsup_lt_ord_lift (by rwa [o.card.lift_id]) /-! ### Basic results -/ @[simp] theorem cof_zero : cof 0 = 0 := by refine LE.le.antisymm ?_ (Cardinal.zero_le _) rw [← card_zero] exact cof_le_card 0 @[simp] theorem cof_eq_zero {o} : cof o = 0 ↔ o = 0 := ⟨inductionOn o fun _ r _ z => let ⟨_, hl, e⟩ := cof_eq r type_eq_zero_iff_isEmpty.2 <| ⟨fun a => let ⟨_, h, _⟩ := hl a (mk_eq_zero_iff.1 (e.trans z)).elim' ⟨_, h⟩⟩, fun e => by simp [e]⟩ theorem cof_ne_zero {o} : cof o ≠ 0 ↔ o ≠ 0 := cof_eq_zero.not @[simp] theorem cof_succ (o) : cof (succ o) = 1 := by apply le_antisymm · refine inductionOn o fun α r _ => ?_ change cof (type _) ≤ _ rw [← (_ : #_ = 1)] · apply cof_type_le refine fun a => ⟨Sum.inr PUnit.unit, Set.mem_singleton _, ?_⟩ rcases a with (a | ⟨⟨⟨⟩⟩⟩) <;> simp [EmptyRelation] · simp · rw [← Cardinal.succ_zero, succ_le_iff] simpa [lt_iff_le_and_ne, Cardinal.zero_le] using fun h => succ_ne_zero o (cof_eq_zero.1 (Eq.symm h)) @[simp] theorem cof_eq_one_iff_is_succ {o} : cof.{u} o = 1 ↔ ∃ a, o = succ a := ⟨inductionOn o fun α r _ z => by rcases cof_eq r with ⟨S, hl, e⟩; rw [z] at e obtain ⟨a⟩ := mk_ne_zero_iff.1 (by rw [e]; exact one_ne_zero) refine ⟨typein r a, Eq.symm <| Quotient.sound ⟨RelIso.ofSurjective (RelEmbedding.ofMonotone ?_ fun x y => ?_) fun x => ?_⟩⟩ · apply Sum.rec <;> [exact Subtype.val; exact fun _ => a] · rcases x with (x | ⟨⟨⟨⟩⟩⟩) <;> rcases y with (y | ⟨⟨⟨⟩⟩⟩) <;> simp [Subrel, Order.Preimage, EmptyRelation] exact x.2 · suffices r x a ∨ ∃ _ : PUnit.{u}, ↑a = x by convert this dsimp [RelEmbedding.ofMonotone]; simp rcases trichotomous_of r x a with (h | h | h) · exact Or.inl h · exact Or.inr ⟨PUnit.unit, h.symm⟩ · rcases hl x with ⟨a', aS, hn⟩ refine absurd h ?_ convert hn change (a : α) = ↑(⟨a', aS⟩ : S) have := le_one_iff_subsingleton.1 (le_of_eq e) congr!, fun ⟨a, e⟩ => by simp [e]⟩ /-! ### Fundamental sequences -/ -- TODO: move stuff about fundamental sequences to their own file. /-- A fundamental sequence for `a` is an increasing sequence of length `o = cof a` that converges at `a`. We provide `o` explicitly in order to avoid type rewrites. -/ def IsFundamentalSequence (a o : Ordinal.{u}) (f : ∀ b < o, Ordinal.{u}) : Prop := o ≤ a.cof.ord ∧ (∀ {i j} (hi hj), i < j → f i hi < f j hj) ∧ blsub.{u, u} o f = a namespace IsFundamentalSequence variable {a o : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} protected theorem cof_eq (hf : IsFundamentalSequence a o f) : a.cof.ord = o := hf.1.antisymm' <| by rw [← hf.2.2] exact (ord_le_ord.2 (cof_blsub_le f)).trans (ord_card_le o) protected theorem strict_mono (hf : IsFundamentalSequence a o f) {i j} : ∀ hi hj, i < j → f i hi < f j hj := hf.2.1 theorem blsub_eq (hf : IsFundamentalSequence a o f) : blsub.{u, u} o f = a := hf.2.2 theorem ord_cof (hf : IsFundamentalSequence a o f) : IsFundamentalSequence a a.cof.ord fun i hi => f i (hi.trans_le (by rw [hf.cof_eq])) := by have H := hf.cof_eq subst H exact hf theorem id_of_le_cof (h : o ≤ o.cof.ord) : IsFundamentalSequence o o fun a _ => a := ⟨h, @fun _ _ _ _ => id, blsub_id o⟩ protected theorem zero {f : ∀ b < (0 : Ordinal), Ordinal} : IsFundamentalSequence 0 0 f := ⟨by rw [cof_zero, ord_zero], @fun i _ hi => (Ordinal.not_lt_zero i hi).elim, blsub_zero f⟩ protected theorem succ : IsFundamentalSequence (succ o) 1 fun _ _ => o := by refine ⟨?_, @fun i j hi hj h => ?_, blsub_const Ordinal.one_ne_zero o⟩ · rw [cof_succ, ord_one] · rw [lt_one_iff_zero] at hi hj rw [hi, hj] at h exact h.false.elim protected theorem monotone (hf : IsFundamentalSequence a o f) {i j : Ordinal} (hi : i < o) (hj : j < o) (hij : i ≤ j) : f i hi ≤ f j hj := by rcases lt_or_eq_of_le hij with (hij | rfl) · exact (hf.2.1 hi hj hij).le · rfl theorem trans {a o o' : Ordinal.{u}} {f : ∀ b < o, Ordinal.{u}} (hf : IsFundamentalSequence a o f) {g : ∀ b < o', Ordinal.{u}} (hg : IsFundamentalSequence o o' g) : IsFundamentalSequence a o' fun i hi => f (g i hi) (by rw [← hg.2.2]; apply lt_blsub) := by refine ⟨?_, @fun i j _ _ h => hf.2.1 _ _ (hg.2.1 _ _ h), ?_⟩ · rw [hf.cof_eq] exact hg.1.trans (ord_cof_le o) · rw [@blsub_comp.{u, u, u} o _ f (@IsFundamentalSequence.monotone _ _ f hf)] · exact hf.2.2 · exact hg.2.2 protected theorem lt {a o : Ordinal} {s : Π p < o, Ordinal} (h : IsFundamentalSequence a o s) {p : Ordinal} (hp : p < o) : s p hp < a := h.blsub_eq ▸ lt_blsub s p hp end IsFundamentalSequence /-- Every ordinal has a fundamental sequence. -/ theorem exists_fundamental_sequence (a : Ordinal.{u}) : ∃ f, IsFundamentalSequence a a.cof.ord f := by suffices h : ∃ o f, IsFundamentalSequence a o f by rcases h with ⟨o, f, hf⟩ exact ⟨_, hf.ord_cof⟩ rcases exists_lsub_cof a with ⟨ι, f, hf, hι⟩ rcases ord_eq ι with ⟨r, wo, hr⟩ let r' := Subrel r fun i ↦ ∀ j, r j i → f j < f i let hrr' : r' ↪r r := Subrel.relEmbedding _ _ haveI := hrr'.isWellOrder refine ⟨_, _, hrr'.ordinal_type_le.trans ?_, @fun i j _ h _ => (enum r' ⟨j, h⟩).prop _ ?_, le_antisymm (blsub_le fun i hi => lsub_le_iff.1 hf.le _) ?_⟩ · rw [← hι, hr] · change r (hrr'.1 _) (hrr'.1 _) rwa [hrr'.2, @enum_lt_enum _ r'] · rw [← hf, lsub_le_iff] intro i suffices h : ∃ i' hi', f i ≤ bfamilyOfFamily' r' (fun i => f i) i' hi' by rcases h with ⟨i', hi', hfg⟩ exact hfg.trans_lt (lt_blsub _ _ _) by_cases! h : ∀ j, r j i → f j < f i · refine ⟨typein r' ⟨i, h⟩, typein_lt_type _ _, ?_⟩ rw [bfamilyOfFamily'_typein] · obtain ⟨hji, hij⟩ := wo.wf.min_mem _ h refine ⟨typein r' ⟨_, fun k hkj => lt_of_lt_of_le ?_ hij⟩, typein_lt_type _ _, ?_⟩ · by_contra! H exact (wo.wf.not_lt_min _ h ⟨IsTrans.trans _ _ _ hkj hji, H⟩) hkj · rwa [bfamilyOfFamily'_typein] @[simp] theorem cof_cof (a : Ordinal.{u}) : cof (cof a).ord = cof a := by obtain ⟨f, hf⟩ := exists_fundamental_sequence a obtain ⟨g, hg⟩ := exists_fundamental_sequence a.cof.ord exact ord_injective (hf.trans hg).cof_eq.symm protected theorem IsNormal.isFundamentalSequence {f : Ordinal.{u} → Ordinal.{u}} (hf : IsNormal f) {a o} (ha : IsSuccLimit a) {g} (hg : IsFundamentalSequence a o g) : IsFundamentalSequence (f a) o fun b hb => f (g b hb) := by refine ⟨?_, @fun i j _ _ h => hf.strictMono (hg.2.1 _ _ h), ?_⟩ · rcases exists_lsub_cof (f a) with ⟨ι, f', hf', hι⟩ rw [← hg.cof_eq, ord_le_ord, ← hι] suffices (lsub.{u, u} fun i => sInf { b : Ordinal | f' i ≤ f b }) = a by rw [← this] apply cof_lsub_le have H : ∀ i, ∃ b < a, f' i ≤ f b := fun i => by have := lt_lsub.{u, u} f' i rw [hf', ← IsNormal.blsub_eq.{u, u} hf ha, lt_blsub_iff] at this simpa using this refine (lsub_le fun i => ?_).antisymm (le_of_forall_lt fun b hb => ?_) · rcases H i with ⟨b, hb, hb'⟩ exact lt_of_le_of_lt (csInf_le' hb') hb · have := hf.strictMono hb rw [← hf', lt_lsub_iff] at this obtain ⟨i, hi⟩ := this rcases H i with ⟨b, _, hb⟩ exact ((le_csInf_iff'' ⟨b, by exact hb⟩).2 fun c hc => hf.strictMono.le_iff_le.1 (hi.trans hc)).trans_lt (lt_lsub _ i) · rw [@blsub_comp.{u, u, u} a _ (fun b _ => f b) (@fun i j _ _ h => hf.strictMono.monotone h) g hg.2.2] exact IsNormal.blsub_eq.{u, u} hf ha theorem IsNormal.cof_eq {f} (hf : IsNormal f) {a} (ha : IsSuccLimit a) : cof (f a) = cof a := let ⟨_, hg⟩ := exists_fundamental_sequence a ord_injective (hf.isFundamentalSequence ha hg).cof_eq theorem IsNormal.cof_le {f} (hf : IsNormal f) (a) : cof a ≤ cof (f a) := by rcases zero_or_succ_or_isSuccLimit a with (rfl | ⟨b, rfl⟩ | ha) · rw [cof_zero] exact zero_le _ · rw [cof_succ, Cardinal.one_le_iff_ne_zero, cof_ne_zero, ← Ordinal.pos_iff_ne_zero] exact (Ordinal.zero_le (f b)).trans_lt (hf.strictMono (lt_succ b)) · rw [hf.cof_eq ha] @[simp] theorem cof_add (a b : Ordinal) : b ≠ 0 → cof (a + b) = cof b := fun h => by rcases zero_or_succ_or_isSuccLimit b with (rfl | ⟨c, rfl⟩ | hb) · contradiction · rw [add_succ, cof_succ, cof_succ] · exact (isNormal_add_right a).cof_eq hb theorem aleph0_le_cof {o} : ℵ₀ ≤ cof o ↔ IsSuccLimit o := by rcases zero_or_succ_or_isSuccLimit o with (rfl | ⟨o, rfl⟩ | l) · simp [Cardinal.aleph0_ne_zero] · simp [Cardinal.one_lt_aleph0] · simp only [l, iff_true] refine le_of_not_gt fun h => ?_ obtain ⟨n, e⟩ := Cardinal.lt_aleph0.1 h have := cof_cof o rw [e, ord_nat] at this cases n · apply l.ne_bot simpa using e · rw [natCast_succ, cof_succ] at this rw [← this, cof_eq_one_iff_is_succ] at e rcases e with ⟨a, rfl⟩ exact not_isSuccLimit_succ _ l @[simp] theorem cof_preOmega {o : Ordinal} (ho : IsSuccPrelimit o) : (preOmega o).cof = o.cof := by by_cases h : IsMin o · simp [h.eq_bot] · exact isNormal_preOmega.cof_eq ⟨h, ho⟩ @[simp] theorem cof_omega {o : Ordinal} (ho : IsSuccLimit o) : (ω_ o).cof = o.cof := isNormal_omega.cof_eq ho @[simp] theorem cof_omega0 : cof ω = ℵ₀ := (aleph0_le_cof.2 isSuccLimit_omega0).antisymm' <| by rw [← card_omega0] apply cof_le_card theorem cof_eq' (r : α → α → Prop) [IsWellOrder α r] (h : IsSuccLimit (type r)) : ∃ S : Set α, (∀ a, ∃ b ∈ S, r a b) ∧ #S = cof (type r) := let ⟨S, H, e⟩ := cof_eq r ⟨S, fun a => let a' := enum r ⟨_, h.succ_lt (typein_lt_type r a)⟩ let ⟨b, h, ab⟩ := H a' ⟨b, h, (IsOrderConnected.conn a b a' <| (typein_lt_typein r).1 (by rw [typein_enum] exact lt_succ (typein _ _))).resolve_right ab⟩, e⟩ @[simp] theorem cof_univ : cof univ.{u, v} = Cardinal.univ.{u, v} := le_antisymm (cof_le_card _) (by refine le_of_forall_lt fun c h => ?_ rcases lt_univ'.1 h with ⟨c, rfl⟩ rcases @cof_eq Ordinal.{u} (· < ·) _ with ⟨S, H, Se⟩ rw [univ, ← lift_cof, ← Cardinal.lift_lift.{u+1, v, u}, Cardinal.lift_lt, ← Se] refine lt_of_not_ge fun h => ?_ obtain ⟨a, e⟩ := Cardinal.mem_range_lift_of_le h refine Quotient.inductionOn a (fun α e => ?_) e obtain ⟨f⟩ := Quotient.exact e have f := Equiv.ulift.symm.trans f let g a := (f a).1 let o := succ (iSup g) rcases H o with ⟨b, h, l⟩ refine l (lt_succ_iff.2 ?_) rw [← show g (f.symm ⟨b, h⟩) = b by simp [g]] apply Ordinal.le_iSup) end Ordinal namespace Cardinal open Ordinal /-! ### Results on sets -/ theorem mk_bounded_subset {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) {r : α → α → Prop} [IsWellOrder α r] (hr : (#α).ord = type r) : #{ s : Set α // Bounded r s } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · rw [ha] haveI := mk_eq_zero_iff.1 ha rw [mk_eq_zero_iff] constructor rintro ⟨s, hs⟩ exact (not_unbounded_iff s).2 hs (unbounded_of_isEmpty s) have h' : IsStrongLimit #α := ⟨ha, @h⟩ have ha := h'.aleph0_le apply le_antisymm · have : { s : Set α | Bounded r s } = ⋃ i, 𝒫{ j | r j i } := setOf_exists _ rw [← coe_setOf, this] refine mk_iUnion_le_sum_mk.trans ((sum_le_mk_mul_iSup (fun i => #(𝒫{ j | r j i }))).trans ((mul_le_max_of_aleph0_le_left ha).trans ?_)) rw [max_eq_left] apply ciSup_le' _ intro i rw [mk_powerset] apply (h'.two_power_lt _).le rw [coe_setOf, card_typein, ← lt_ord, hr] apply typein_lt_type · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · apply bounded_singleton rw [← hr] apply isSuccLimit_ord ha · intro a b hab simpa [singleton_eq_singleton_iff] using hab theorem mk_subset_mk_lt_cof {α : Type*} (h : ∀ x < #α, 2 ^ x < #α) : #{ s : Set α // #s < cof (#α).ord } = #α := by rcases eq_or_ne #α 0 with (ha | ha) · simp [ha] have h' : IsStrongLimit #α := ⟨ha, @h⟩ rcases ord_eq α with ⟨r, wo, hr⟩ apply le_antisymm · conv_rhs => rw [← mk_bounded_subset h hr] apply mk_le_mk_of_subset intro s hs rw [hr] at hs exact lt_cof_type hs · refine @mk_le_of_injective α _ (fun x => Subtype.mk {x} ?_) ?_ · rw [mk_singleton] exact one_lt_aleph0.trans_le (aleph0_le_cof.2 (isSuccLimit_ord h'.aleph0_le)) · intro a b hab simpa [singleton_eq_singleton_iff] using hab /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_sUnion (r : α → α → Prop) [wo : IsWellOrder α r] {s : Set (Set α)} (h₁ : Unbounded r <| ⋃₀ s) (h₂ : #s < Order.cof (swap rᶜ)) : ∃ x ∈ s, Unbounded r x := by by_contra! h simp_rw [not_unbounded_iff] at h let f : s → α := fun x : s => wo.wf.sup x (h x.1 x.2) refine h₂.not_ge (le_trans (csInf_le' ⟨range f, fun x => ?_, rfl⟩) mk_range_le) rcases h₁ x with ⟨y, ⟨c, hc, hy⟩, hxy⟩ exact ⟨f ⟨c, hc⟩, mem_range_self _, fun hxz => hxy (Trans.trans (wo.wf.lt_sup _ hy) hxz)⟩ /-- If the union of s is unbounded and s is smaller than the cofinality, then s has an unbounded member -/ theorem unbounded_of_unbounded_iUnion {α β : Type u} (r : α → α → Prop) [wo : IsWellOrder α r] (s : β → Set α) (h₁ : Unbounded r <| ⋃ x, s x) (h₂ : #β < Order.cof (swap rᶜ)) : ∃ x : β, Unbounded r (s x) := by rw [← sUnion_range] at h₁ rcases unbounded_of_unbounded_sUnion r h₁ (mk_range_le.trans_lt h₂) with ⟨_, ⟨x, rfl⟩, u⟩ exact ⟨x, u⟩ /-! ### Consequences of König's lemma -/ theorem lt_power_cof {c : Cardinal.{u}} : ℵ₀ ≤ c → c < c ^ c.ord.cof := Cardinal.inductionOn c fun α h => by rcases ord_eq α with ⟨r, wo, re⟩ have := isSuccLimit_ord h rw [re] at this ⊢ rcases cof_eq' r this with ⟨S, H, Se⟩ have := sum_lt_prod (fun a : S => #{ x // r x a }) (fun _ => #α) fun i => ?_ · simp only [Cardinal.prod_const, Cardinal.lift_id, ← Se, ← mk_sigma, power_def] at this ⊢ refine lt_of_le_of_lt ?_ this refine ⟨Embedding.ofSurjective ?_ ?_⟩ · exact fun x => x.2.1 · exact fun a => let ⟨b, h, ab⟩ := H a ⟨⟨⟨_, h⟩, _, ab⟩, rfl⟩ · have := typein_lt_type r i rwa [← re, lt_ord] at this theorem lt_cof_power {a b : Cardinal} (ha : ℵ₀ ≤ a) (b1 : 1 < b) : a < (b ^ a).ord.cof := by have b0 : b ≠ 0 := (zero_lt_one.trans b1).ne' apply lt_imp_lt_of_le_imp_le (power_le_power_left <| power_ne_zero a b0) rw [← power_mul, mul_eq_self ha] exact lt_power_cof (ha.trans <| (cantor' _ b1).le) end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/HasCardinalLT.lean
import Mathlib.SetTheory.Cardinal.Regular /-! # The property of being of cardinality less than a cardinal Given `X : Type u` and `κ : Cardinal.{v}`, we introduce a predicate `HasCardinalLT X κ` expressing that `Cardinal.lift.{v} (Cardinal.mk X) < Cardinal.lift κ`. -/ universe w v u u' /-- The property that the cardinal of a type `X : Type u` is less than `κ : Cardinal.{v}`. -/ def HasCardinalLT (X : Type u) (κ : Cardinal.{v}) : Prop := Cardinal.lift.{v} (Cardinal.mk X) < Cardinal.lift κ lemma hasCardinalLT_iff_cardinal_mk_lt (X : Type u) (κ : Cardinal.{u}) : HasCardinalLT X κ ↔ Cardinal.mk X < κ := by simp [HasCardinalLT] namespace HasCardinalLT section variable {X : Type u} {κ : Cardinal.{v}} (h : HasCardinalLT X κ) include h lemma small : Small.{v} X := by dsimp [HasCardinalLT] at h rw [← Cardinal.lift_lt.{_, v + 1}, Cardinal.lift_lift, Cardinal.lift_lift] at h simpa only [Cardinal.small_iff_lift_mk_lt_univ] using h.trans (Cardinal.lift_lt_univ' κ) lemma of_le {κ' : Cardinal.{v}} (hκ' : κ ≤ κ') : HasCardinalLT X κ' := lt_of_lt_of_le h (by simpa only [Cardinal.lift_le] using hκ') variable {Y : Type u'} lemma of_injective (f : Y → X) (hf : Function.Injective f) : HasCardinalLT Y κ := by dsimp [HasCardinalLT] at h ⊢ rw [← Cardinal.lift_lt.{_, u}, Cardinal.lift_lift, Cardinal.lift_lift] rw [← Cardinal.lift_lt.{_, u'}, Cardinal.lift_lift, Cardinal.lift_lift] at h exact lt_of_le_of_lt (Cardinal.mk_le_of_injective (Function.Injective.comp ULift.up_injective (Function.Injective.comp hf ULift.down_injective))) h lemma of_surjective (f : X → Y) (hf : Function.Surjective f) : HasCardinalLT Y κ := by dsimp [HasCardinalLT] at h ⊢ rw [← Cardinal.lift_lt.{_, u}, Cardinal.lift_lift, Cardinal.lift_lift] rw [← Cardinal.lift_lt.{_, u'}, Cardinal.lift_lift, Cardinal.lift_lift] at h exact lt_of_le_of_lt (Cardinal.mk_le_of_surjective (Function.Surjective.comp ULift.up_surjective (Function.Surjective.comp hf ULift.down_surjective))) h end end HasCardinalLT lemma hasCardinalLT_iff_of_equiv {X : Type u} {Y : Type u'} (e : X ≃ Y) (κ : Cardinal.{v}) : HasCardinalLT X κ ↔ HasCardinalLT Y κ := ⟨fun h ↦ h.of_injective _ e.symm.injective, fun h ↦ h.of_injective _ e.injective⟩ @[simp] lemma hasCardinalLT_aleph0_iff (X : Type u) : HasCardinalLT X Cardinal.aleph0.{v} ↔ Finite X := by simpa [HasCardinalLT] using Cardinal.mk_lt_aleph0_iff lemma hasCardinalLT_of_finite (X : Type*) [Finite X] (κ : Cardinal) (hκ : Cardinal.aleph0 ≤ κ) : HasCardinalLT X κ := .of_le (by rwa [hasCardinalLT_aleph0_iff]) hκ @[simp] lemma hasCardinalLT_lift_iff (X : Type v) (κ : Cardinal.{w}) : HasCardinalLT X (Cardinal.lift.{u} κ) ↔ HasCardinalLT X κ := by simp [HasCardinalLT, ← (Cardinal.lift_strictMono.{max v w, max u}).lt_iff_lt] @[simp] lemma hasCardinalLT_ulift_iff (X : Type v) (κ : Cardinal.{w}) : HasCardinalLT (ULift.{u} X) κ ↔ HasCardinalLT X κ := hasCardinalLT_iff_of_equiv Equiv.ulift κ lemma hasCardinalLT_sum_iff (X : Type u) (Y : Type u') (κ : Cardinal.{w}) (hκ : Cardinal.aleph0 ≤ κ) : HasCardinalLT (X ⊕ Y) κ ↔ HasCardinalLT X κ ∧ HasCardinalLT Y κ := by constructor · intro h exact ⟨h.of_injective _ Sum.inl_injective, h.of_injective _ Sum.inr_injective⟩ · rintro ⟨hX, hY⟩ dsimp [HasCardinalLT] at hX hY ⊢ rw [← Cardinal.lift_lt.{_, u'}, Cardinal.lift_lift, Cardinal.lift_lift] at hX rw [← Cardinal.lift_lt.{_, u}, Cardinal.lift_lift, Cardinal.lift_lift] at hY simp only [Cardinal.mk_sum, Cardinal.lift_add, Cardinal.lift_lift] exact Cardinal.add_lt_of_lt (by simpa using hκ) hX hY lemma hasCardinalLT_option_iff (X : Type u) (κ : Cardinal.{w}) (hκ : Cardinal.aleph0 ≤ κ) : HasCardinalLT (Option X) κ ↔ HasCardinalLT X κ := by rw [hasCardinalLT_iff_of_equiv (Equiv.optionEquivSumPUnit.{0} X), hasCardinalLT_sum_iff _ _ _ hκ, and_iff_left_iff_imp] refine fun _ ↦ HasCardinalLT.of_le ?_ hκ rw [hasCardinalLT_aleph0_iff] infer_instance lemma hasCardinalLT_subtype_max {X : Type*} {P₁ P₂ : X → Prop} {κ : Cardinal} (hκ : Cardinal.aleph0 ≤ κ) (h₁ : HasCardinalLT (Subtype P₁) κ) (h₂ : HasCardinalLT (Subtype P₂) κ) : HasCardinalLT (Subtype (P₁ ⊔ P₂)) κ := by have : HasCardinalLT (Subtype P₁ ⊕ Subtype P₂) κ := by rw [hasCardinalLT_sum_iff _ _ _ hκ] exact ⟨h₁, h₂⟩ refine this.of_surjective (Sum.elim (fun x ↦ ⟨x.1, Or.inl x.2⟩) (fun x ↦ ⟨x.1, Or.inr x.2⟩)) ?_ rintro ⟨x, hx | hx⟩ · exact ⟨Sum.inl ⟨x, hx⟩, rfl⟩ · exact ⟨Sum.inr ⟨x, hx⟩, rfl⟩ lemma hasCardinalLT_union {X : Type*} {S₁ S₂ : Set X} {κ : Cardinal} (hκ : Cardinal.aleph0 ≤ κ) (h₁ : HasCardinalLT S₁ κ) (h₂ : HasCardinalLT S₂ κ) : HasCardinalLT (S₁ ∪ S₂ : Set _) κ := hasCardinalLT_subtype_max hκ h₁ h₂ /-- The particular case of `hasCardinatLT_sigma` when all the inputs are in the same universe `w`. It is used to prove the general case. -/ lemma hasCardinalLT_sigma' {ι : Type w} (α : ι → Type w) (κ : Cardinal.{w}) [Fact κ.IsRegular] (hι : HasCardinalLT ι κ) (hα : ∀ i, HasCardinalLT (α i) κ) : HasCardinalLT (Σ i, α i) κ := by simp only [hasCardinalLT_iff_cardinal_mk_lt] at hι hα ⊢ rw [Cardinal.mk_sigma] exact Cardinal.sum_lt_lift_of_isRegular.{w, w} Fact.out (by simpa) hα lemma hasCardinalLT_sigma {ι : Type u} (α : ι → Type v) (κ : Cardinal.{w}) [Fact κ.IsRegular] (hι : HasCardinalLT ι κ) (hα : ∀ i, HasCardinalLT (α i) κ) : HasCardinalLT (Σ i, α i) κ := by have : Fact (Cardinal.lift.{max u v} κ).IsRegular := ⟨Cardinal.IsRegular.lift Fact.out⟩ have := hasCardinalLT_sigma' (fun (i : ULift.{max v w} ι) ↦ ULift.{max u w} (α (ULift.down i))) (Cardinal.lift.{max u v} κ) (by simpa) (fun i ↦ by simpa using hα (ULift.down i)) rw [hasCardinalLT_lift_iff] at this exact this.of_surjective (fun ⟨i, a⟩ ↦ ⟨ULift.down i, ULift.down a⟩) (fun ⟨i, a⟩ ↦ ⟨⟨ULift.up i, ULift.up a⟩, rfl⟩) lemma hasCardinalLT_subtype_iSup {ι : Type*} {X : Type*} (P : ι → X → Prop) {κ : Cardinal} [Fact κ.IsRegular] (hι : HasCardinalLT ι κ) (hP : ∀ i, HasCardinalLT (Subtype (P i)) κ) : HasCardinalLT (Subtype (⨆ i, P i)) κ := (hasCardinalLT_sigma (fun i ↦ Subtype (P i)) κ hι hP).of_surjective (fun ⟨i, x, hx⟩ ↦ ⟨x, by simp only [iSup_apply, iSup_Prop_eq]; exact ⟨i, hx⟩⟩) (by rintro ⟨_, h⟩ simp only [iSup_apply, iSup_Prop_eq] at h obtain ⟨i, hi⟩ := h exact ⟨⟨i, _, hi⟩, rfl⟩) lemma hasCardinalLT_iUnion {ι : Type*} {X : Type*} (S : ι → Set X) {κ : Cardinal} [Fact κ.IsRegular] (hι : HasCardinalLT ι κ) (hS : ∀ i, HasCardinalLT (S i) κ) : HasCardinalLT (⋃ i, S i) κ := by convert show HasCardinalLT (setOf ((⨆ i, S i))) κ from hasCardinalLT_subtype_iSup S hι hS aesop /-- The particular case of `hasCardinatLT_prod` when all the inputs are in the same universe `w`. It is used to prove the general case. -/ lemma hasCardinalLT_prod' {T₁ T₂ : Type w} {κ : Cardinal.{w}} (hκ : Cardinal.aleph0 ≤ κ) (h₁ : HasCardinalLT T₁ κ) (h₂ : HasCardinalLT T₂ κ) : HasCardinalLT (T₁ × T₂) κ := by rw [hasCardinalLT_iff_cardinal_mk_lt] at h₁ h₂ ⊢ simpa using Cardinal.mul_lt_of_lt hκ h₁ h₂ lemma hasCardinalLT_prod {T₁ : Type u} {T₂ : Type u'} {κ : Cardinal.{w}} (hκ : Cardinal.aleph0 ≤ κ) (h₁ : HasCardinalLT T₁ κ) (h₂ : HasCardinalLT T₂ κ) : HasCardinalLT (T₁ × T₂) κ := by have := hasCardinalLT_prod' (T₁ := ULift.{max u' w} T₁) (T₂ := ULift.{max u w} T₂) (κ := Cardinal.lift.{max u u'} κ) (by simpa) (by simpa) (by simpa) simp only [hasCardinalLT_lift_iff] at this exact this.of_surjective (fun ⟨x₁, x₂⟩ ↦ ⟨ULift.down x₁, ULift.down x₂⟩) (fun ⟨x₁, x₂⟩ ↦ ⟨⟨ULift.up x₁, ULift.up x₂⟩, rfl⟩) namespace HasCardinalLT /-- For any `w`-small type `X`, there exists a regular cardinal `κ : Cardinal.{w}` such that `HasCardinalLT X κ`. -/ lemma exists_regular_cardinal (X : Type u) [Small.{w} X] : ∃ (κ : Cardinal.{w}), κ.IsRegular ∧ HasCardinalLT X κ := ⟨Order.succ (max (Cardinal.mk (Shrink.{w} X)) .aleph0), Cardinal.isRegular_succ (le_max_right _ _), by simp [hasCardinalLT_iff_of_equiv (equivShrink.{w} X), hasCardinalLT_iff_cardinal_mk_lt]⟩ /-- For any `w`-small family `X : ι → Type u` of `w`-small types, there exists a regular cardinal `κ : Cardinal.{w}` such that `HasCardinalLT (X i) κ` for all `i : ι`. -/ lemma exists_regular_cardinal_forall {ι : Type v} (X : ι → Type u) [Small.{w} ι] [∀ i, Small.{w} (X i)] : ∃ (κ : Cardinal.{w}), κ.IsRegular ∧ ∀ (i : ι), HasCardinalLT (X i) κ := by obtain ⟨κ, hκ, h⟩ := exists_regular_cardinal.{w} (Sigma X) exact ⟨κ, hκ, fun i ↦ h.of_injective _ sigma_mk_injective⟩ end HasCardinalLT
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Pigeonhole.lean
import Mathlib.Data.Set.Finite.Lattice import Mathlib.SetTheory.Cardinal.Regular /-! # Infinite pigeonhole principle This file proves variants of the infinite pigeonhole principle. ## TODO Generalize universes of results. -/ open Order Ordinal Set universe u namespace Cardinal /-- The infinite pigeonhole principle -/ theorem infinite_pigeonhole {β α : Type u} (f : β → α) (h₁ : ℵ₀ ≤ #β) (h₂ : #α < (#β).ord.cof) : ∃ a : α, #(f ⁻¹' {a}) = #β := by have : ∃ a, #β ≤ #(f ⁻¹' {a}) := by by_contra! h apply mk_univ.not_lt rw [← preimage_univ, ← iUnion_of_singleton, preimage_iUnion] exact mk_iUnion_le_sum_mk.trans_lt <| (sum_le_mk_mul_iSup _).trans_lt <| mul_lt_of_lt h₁ (h₂.trans_le <| cof_ord_le _) (iSup_lt h₂ h) obtain ⟨x, h⟩ := this refine ⟨x, h.antisymm' ?_⟩ rw [le_mk_iff_exists_set] exact ⟨_, rfl⟩ /-- Pigeonhole principle for a cardinality below the cardinality of the domain -/ theorem infinite_pigeonhole_card {β α : Type u} (f : β → α) (θ : Cardinal) (hθ : θ ≤ #β) (h₁ : ℵ₀ ≤ θ) (h₂ : #α < θ.ord.cof) : ∃ a : α, θ ≤ #(f ⁻¹' {a}) := by rcases le_mk_iff_exists_set.1 hθ with ⟨s, rfl⟩ obtain ⟨a, ha⟩ := infinite_pigeonhole (f ∘ Subtype.val : s → α) h₁ h₂ use a; rw [← ha, @preimage_comp _ _ _ Subtype.val f] exact mk_preimage_of_injective _ _ Subtype.val_injective theorem infinite_pigeonhole_set {β α : Type u} {s : Set β} (f : s → α) (θ : Cardinal) (hθ : θ ≤ #s) (h₁ : ℵ₀ ≤ θ) (h₂ : #α < θ.ord.cof) : ∃ (a : α) (t : Set β) (h : t ⊆ s), θ ≤ #t ∧ ∀ ⦃x⦄ (hx : x ∈ t), f ⟨x, h hx⟩ = a := by obtain ⟨a, ha⟩ := infinite_pigeonhole_card f θ hθ h₁ h₂ refine ⟨a, { x | ∃ h, f ⟨x, h⟩ = a }, ?_, ?_, ?_⟩ · rintro x ⟨hx, _⟩ exact hx · refine ha.trans (ge_of_eq <| Quotient.sound ⟨Equiv.trans ?_ (Equiv.subtypeSubtypeEquivSubtypeExists _ _).symm⟩) simp only [coe_eq_subtype, mem_singleton_iff, mem_preimage, mem_setOf_eq] rfl rintro x ⟨_, hx'⟩; exact hx' /-- A function whose codomain's cardinality is infinite but strictly smaller than its domain's has a fiber with cardinality strictly great than the codomain. -/ theorem infinite_pigeonhole_card_lt {β α : Type u} (f : β → α) (w : #α < #β) (w' : ℵ₀ ≤ #α) : ∃ a : α, #α < #(f ⁻¹' {a}) := by simp_rw [← succ_le_iff] exact infinite_pigeonhole_card f (succ #α) (succ_le_of_lt w) (w'.trans (lt_succ _).le) ((lt_succ _).trans_le (isRegular_succ w').2.ge) /-- A function whose codomain's cardinality is infinite but strictly smaller than its domain's has an infinite fiber. -/ theorem exists_infinite_fiber {β α : Type u} (f : β → α) (w : #α < #β) (w' : Infinite α) : ∃ a : α, Infinite (f ⁻¹' {a}) := by simp_rw [Cardinal.infinite_iff] at w' ⊢ obtain ⟨a, ha⟩ := infinite_pigeonhole_card_lt f w w' exact ⟨a, w'.trans ha.le⟩ /-- If an infinite type `β` can be expressed as a union of finite sets, then the cardinality of the collection of those finite sets must be at least the cardinality of `β`. -/ -- TODO: write `Set.univ` instead of `⊤` and rename the theorem accordingly. theorem le_range_of_union_finset_eq_top {α β : Type*} [Infinite β] (f : α → Finset β) (w : ⋃ a, (f a : Set β) = ⊤) : #β ≤ #(range f) := by have k : _root_.Infinite (range f) := by rw [infinite_coe_iff] apply mt (union_finset_finite_of_range_finite f) rw [w] exact infinite_univ by_contra h simp only [not_le] at h let u : ∀ b, ∃ a, b ∈ f a := fun b => by simpa using (w.ge :) (Set.mem_univ b) let u' : β → range f := fun b => ⟨f (u b).choose, by simp⟩ have v' : ∀ a, u' ⁻¹' {⟨f a, by simp⟩} ≤ f a := by rintro a p m have m : f (u p).choose = f a := by simpa [u'] using m rw [← m] apply fun b => (u b).choose_spec obtain ⟨⟨-, ⟨a, rfl⟩⟩, p⟩ := exists_infinite_fiber u' h k exact (@Infinite.of_injective _ _ p (inclusion (v' a)) (inclusion_injective _)).false end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/NatCount.lean
import Mathlib.Data.Nat.Count import Mathlib.Data.Set.Card /-! # Counting on ℕ This file provides lemmas about the relation of `Nat.count` with cardinality functions. -/ namespace Nat open Nat Count variable {p : ℕ → Prop} [DecidablePred p] (n : ℕ) theorem count_le_cardinal : (count p n : Cardinal) ≤ Cardinal.mk { k | p k } := by rw [count_eq_card_fintype, ← Cardinal.mk_fintype] exact Cardinal.mk_subtype_mono fun x hx ↦ hx.2 theorem count_le_setENCard : count p n ≤ Set.encard { k | p k } := by simp only [Set.encard, ENat.card, Set.coe_setOf, Cardinal.natCast_le_toENat_iff] exact Nat.count_le_cardinal n theorem count_le_setNCard (h : { k | p k }.Finite) : count p n ≤ Set.ncard { k | p k } := by rw [Set.ncard_def, ← ENat.coe_le_coe, ENat.coe_toNat (by simpa)] exact count_le_setENCard n end Nat
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Defs.lean
import Mathlib.Data.ULift import Mathlib.Util.Delaborators import Mathlib.Util.AssertExists /-! # Cardinal Numbers We define cardinal numbers as a quotient of types under the equivalence relation of equinumerosity (i.e., existence of a bijection). ## Main definitions * `Cardinal` is the type of cardinal numbers (in a given universe). * `Cardinal.mk α` or `#α` is the cardinality of `α`. The notation `#` lives in the locale `Cardinal`. * Addition `c₁ + c₂` is defined by `Cardinal.add_def α β : #α + #β = #(α ⊕ β)`. * Multiplication `c₁ * c₂` is defined by `Cardinal.mul_def : #α * #β = #(α × β)`. * Exponentiation `c₁ ^ c₂` is defined by `Cardinal.power_def α β : #α ^ #β = #(β → α)`. * `Cardinal.sum` is the sum of an indexed family of cardinals, i.e. the cardinality of the corresponding sigma type. * `Cardinal.prod` is the product of an indexed family of cardinals, i.e. the cardinality of the corresponding pi type. * `Cardinal.aleph0` or `ℵ₀` is the cardinality of `ℕ`. This definition is universe polymorphic: `Cardinal.aleph0.{u} : Cardinal.{u}` (contrast with `ℕ : Type`, which lives in a specific universe). In some cases the universe level has to be given explicitly. ## Implementation notes * There is a type of cardinal numbers in every universe level: `Cardinal.{u} : Type (u + 1)` is the quotient of types in `Type u`. The operation `Cardinal.lift` lifts cardinal numbers to a higher level. * Cardinal arithmetic specifically for infinite cardinals (like `κ * κ = κ`) is in the file `Mathlib/SetTheory/Cardinal/Ordinal.lean`. ## References * <https://en.wikipedia.org/wiki/Cardinal_number> ## Tags cardinal number, cardinal arithmetic, cardinal exponentiation, aleph, Cantor's theorem, König's theorem, Konig's theorem -/ assert_not_exists Monoid open List Function Set noncomputable section universe u v w v' w' variable {α β : Type u} /-! ### Definition of cardinals -/ /-- The equivalence relation on types given by equivalence (bijective correspondence) of types. Quotienting by this equivalence relation gives the cardinal numbers. -/ instance Cardinal.isEquivalent : Setoid (Type u) where r α β := Nonempty (α ≃ β) iseqv := ⟨ fun α => ⟨Equiv.refl α⟩, fun ⟨e⟩ => ⟨e.symm⟩, fun ⟨e₁⟩ ⟨e₂⟩ => ⟨e₁.trans e₂⟩⟩ /-- `Cardinal.{u}` is the type of cardinal numbers in `Type u`, defined as the quotient of `Type u` by existence of an equivalence (a bijection with explicit inverse). -/ @[pp_with_univ] def Cardinal : Type (u + 1) := Quotient Cardinal.isEquivalent namespace Cardinal /-- The cardinal number of a type -/ def mk : Type u → Cardinal := Quotient.mk' @[inherit_doc] scoped prefix:max "#" => Cardinal.mk instance canLiftCardinalType : CanLift Cardinal.{u} (Type u) mk fun _ => True := ⟨fun c _ => Quot.inductionOn c fun α => ⟨α, rfl⟩⟩ @[elab_as_elim] theorem inductionOn {p : Cardinal → Prop} (c : Cardinal) (h : ∀ α, p #α) : p c := Quotient.inductionOn c h @[elab_as_elim] theorem inductionOn₂ {p : Cardinal → Cardinal → Prop} (c₁ : Cardinal) (c₂ : Cardinal) (h : ∀ α β, p #α #β) : p c₁ c₂ := Quotient.inductionOn₂ c₁ c₂ h @[elab_as_elim] theorem inductionOn₃ {p : Cardinal → Cardinal → Cardinal → Prop} (c₁ : Cardinal) (c₂ : Cardinal) (c₃ : Cardinal) (h : ∀ α β γ, p #α #β #γ) : p c₁ c₂ c₃ := Quotient.inductionOn₃ c₁ c₂ c₃ h theorem induction_on_pi {ι : Type u} {p : (ι → Cardinal.{v}) → Prop} (f : ι → Cardinal.{v}) (h : ∀ f : ι → Type v, p fun i ↦ #(f i)) : p f := Quotient.induction_on_pi f h protected theorem eq : #α = #β ↔ Nonempty (α ≃ β) := Quotient.eq' @[simp] theorem mk_out (c : Cardinal) : #c.out = c := Quotient.out_eq _ /-- The representative of the cardinal of a type is equivalent to the original type. -/ def outMkEquiv {α : Type v} : (#α).out ≃ α := Nonempty.some <| Cardinal.eq.mp (by simp) theorem mk_congr (e : α ≃ β) : #α = #β := Quot.sound ⟨e⟩ alias _root_.Equiv.cardinal_eq := mk_congr /-- Lift a function between `Type*`s to a function between `Cardinal`s. -/ def map (f : Type u → Type v) (hf : ∀ α β, α ≃ β → f α ≃ f β) : Cardinal.{u} → Cardinal.{v} := Quotient.map f fun α β ⟨e⟩ => ⟨hf α β e⟩ @[simp] theorem map_mk (f : Type u → Type v) (hf : ∀ α β, α ≃ β → f α ≃ f β) (α : Type u) : map f hf #α = #(f α) := rfl /-- Lift a binary operation `Type* → Type* → Type*` to a binary operation on `Cardinal`s. -/ def map₂ (f : Type u → Type v → Type w) (hf : ∀ α β γ δ, α ≃ β → γ ≃ δ → f α γ ≃ f β δ) : Cardinal.{u} → Cardinal.{v} → Cardinal.{w} := Quotient.map₂ f fun α β ⟨e₁⟩ γ δ ⟨e₂⟩ => ⟨hf α β γ δ e₁ e₂⟩ /-! ### Lifting cardinals to a higher universe -/ /-- The universe lift operation on cardinals. You can specify the universes explicitly with `lift.{u v} : Cardinal.{v} → Cardinal.{max v u}` -/ @[pp_with_univ] def lift (c : Cardinal.{v}) : Cardinal.{max v u} := map ULift.{u, v} (fun _ _ e => Equiv.ulift.trans <| e.trans Equiv.ulift.symm) c @[simp] theorem mk_uLift (α) : #(ULift.{v, u} α) = lift.{v} #α := rfl /-- `lift.{max u v, u}` equals `lift.{v, u}`. Unfortunately, the simp lemma doesn't work. -/ theorem lift_umax : lift.{max u v, u} = lift.{v, u} := funext fun a => inductionOn a fun _ => (Equiv.ulift.trans Equiv.ulift.symm).cardinal_eq /-- A cardinal lifted to a lower or equal universe equals itself. Unfortunately, the simp lemma doesn't work. -/ theorem lift_id' (a : Cardinal.{max u v}) : lift.{u} a = a := inductionOn a fun _ => mk_congr Equiv.ulift /-- A cardinal lifted to the same universe equals itself. -/ @[simp] theorem lift_id (a : Cardinal) : lift.{u, u} a = a := lift_id'.{u, u} a /-- A cardinal lifted to the zero universe equals itself. -/ @[simp] theorem lift_uzero (a : Cardinal.{u}) : lift.{0} a = a := lift_id'.{0, u} a @[simp] theorem lift_lift.{u_1} (a : Cardinal.{u_1}) : lift.{w} (lift.{v} a) = lift.{max v w} a := inductionOn a fun _ => (Equiv.ulift.trans <| Equiv.ulift.trans Equiv.ulift.symm).cardinal_eq theorem out_lift_equiv (a : Cardinal.{u}) : Nonempty ((lift.{v} a).out ≃ a.out) := by rw [← mk_out a, ← mk_uLift, mk_out] exact ⟨outMkEquiv.trans Equiv.ulift⟩ theorem lift_mk_eq {α : Type u} {β : Type v} : lift.{max v w} #α = lift.{max u w} #β ↔ Nonempty (α ≃ β) := Quotient.eq'.trans ⟨fun ⟨f⟩ => ⟨Equiv.ulift.symm.trans <| f.trans Equiv.ulift⟩, fun ⟨f⟩ => ⟨Equiv.ulift.trans <| f.trans Equiv.ulift.symm⟩⟩ /-- A variant of `Cardinal.lift_mk_eq` with specialized universes. Because Lean often cannot realize it should use this specialization itself, we provide this statement separately so you don't have to solve the specialization problem either. -/ theorem lift_mk_eq' {α : Type u} {β : Type v} : lift.{v} #α = lift.{u} #β ↔ Nonempty (α ≃ β) := lift_mk_eq.{u, v, 0} theorem mk_congr_lift {α : Type u} {β : Type v} (e : α ≃ β) : lift.{v} #α = lift.{u} #β := lift_mk_eq'.2 ⟨e⟩ alias _root_.Equiv.lift_cardinal_eq := mk_congr_lift /-! ### Basic cardinals -/ instance : Zero Cardinal.{u} := -- `PEmpty` might be more canonical, but this is convenient for defeq with natCast ⟨lift #(Fin 0)⟩ instance : Inhabited Cardinal.{u} := ⟨0⟩ @[simp] theorem mk_eq_zero (α : Type u) [IsEmpty α] : #α = 0 := (Equiv.equivOfIsEmpty α (ULift (Fin 0))).cardinal_eq @[simp] theorem lift_zero : lift 0 = 0 := mk_eq_zero _ theorem mk_eq_zero_iff {α : Type u} : #α = 0 ↔ IsEmpty α := ⟨fun e => let ⟨h⟩ := Quotient.exact e h.isEmpty, @mk_eq_zero α⟩ theorem mk_ne_zero_iff {α : Type u} : #α ≠ 0 ↔ Nonempty α := (not_iff_not.2 mk_eq_zero_iff).trans not_isEmpty_iff @[simp] theorem mk_ne_zero (α : Type u) [Nonempty α] : #α ≠ 0 := mk_ne_zero_iff.2 ‹_› instance : One Cardinal.{u} := -- `PUnit` might be more canonical, but this is convenient for defeq with natCast ⟨lift #(Fin 1)⟩ instance : Nontrivial Cardinal.{u} := ⟨⟨1, 0, mk_ne_zero _⟩⟩ theorem mk_eq_one (α : Type u) [Subsingleton α] [Nonempty α] : #α = 1 := let ⟨_⟩ := nonempty_unique α; (Equiv.ofUnique α (ULift (Fin 1))).cardinal_eq instance : Add Cardinal.{u} := ⟨map₂ Sum fun _ _ _ _ => Equiv.sumCongr⟩ theorem add_def (α β : Type u) : #α + #β = #(α ⊕ β) := rfl instance : NatCast Cardinal.{u} := ⟨fun n => lift #(Fin n)⟩ @[simp] theorem mk_sum (α : Type u) (β : Type v) : #(α ⊕ β) = lift.{v, u} #α + lift.{u, v} #β := mk_congr (Equiv.ulift.symm.sumCongr Equiv.ulift.symm) @[simp] theorem mk_option {α : Type u} : #(Option α) = #α + 1 := by rw [(Equiv.optionEquivSumPUnit.{u, u} α).cardinal_eq, mk_sum, mk_eq_one PUnit, lift_id, lift_id] @[simp] theorem mk_psum (α : Type u) (β : Type v) : #(α ⊕' β) = lift.{v} #α + lift.{u} #β := (mk_congr (Equiv.psumEquivSum α β)).trans (mk_sum α β) instance : Mul Cardinal.{u} := ⟨map₂ Prod fun _ _ _ _ => Equiv.prodCongr⟩ theorem mul_def (α β : Type u) : #α * #β = #(α × β) := rfl @[simp] theorem mk_prod (α : Type u) (β : Type v) : #(α × β) = lift.{v, u} #α * lift.{u, v} #β := mk_congr (Equiv.ulift.symm.prodCongr Equiv.ulift.symm) /-- The cardinal exponential. `#α ^ #β` is the cardinal of `β → α`. -/ instance instPowCardinal : Pow Cardinal.{u} Cardinal.{u} := ⟨map₂ (fun α β => β → α) fun _ _ _ _ e₁ e₂ => e₂.arrowCongr e₁⟩ theorem power_def (α β : Type u) : #α ^ #β = #(β → α) := rfl theorem mk_arrow (α : Type u) (β : Type v) : #(α → β) = (lift.{u} #β ^ lift.{v} #α) := mk_congr (Equiv.ulift.symm.arrowCongr Equiv.ulift.symm) @[simp] theorem lift_power (a b : Cardinal.{u}) : lift.{v} (a ^ b) = lift.{v} a ^ lift.{v} b := inductionOn₂ a b fun _ _ => mk_congr <| Equiv.ulift.trans (Equiv.ulift.arrowCongr Equiv.ulift).symm @[simp] theorem power_zero (a : Cardinal) : a ^ (0 : Cardinal) = 1 := inductionOn a fun _ => mk_eq_one _ @[simp] theorem power_one (a : Cardinal.{u}) : a ^ (1 : Cardinal) = a := inductionOn a fun α => mk_congr (Equiv.funUnique (ULift.{u} (Fin 1)) α) theorem power_add (a b c : Cardinal) : a ^ (b + c) = a ^ b * a ^ c := inductionOn₃ a b c fun α β γ => mk_congr <| Equiv.sumArrowEquivProdArrow β γ α @[simp] theorem one_power {a : Cardinal} : (1 : Cardinal) ^ a = 1 := inductionOn a fun _ => mk_eq_one _ @[simp] theorem zero_power {a : Cardinal} : a ≠ 0 → (0 : Cardinal) ^ a = 0 := inductionOn a fun _ heq => mk_eq_zero_iff.2 <| isEmpty_pi.2 <| let ⟨a⟩ := mk_ne_zero_iff.1 heq ⟨a, inferInstance⟩ theorem power_ne_zero {a : Cardinal} (b : Cardinal) : a ≠ 0 → a ^ b ≠ 0 := inductionOn₂ a b fun _ _ h => let ⟨a⟩ := mk_ne_zero_iff.1 h mk_ne_zero_iff.2 ⟨fun _ => a⟩ theorem mul_power {a b c : Cardinal} : (a * b) ^ c = a ^ c * b ^ c := inductionOn₃ a b c fun _ _ γ => mk_congr <| Equiv.arrowProdEquivProdArrow γ _ _ @[simp] theorem lift_one : lift 1 = 1 := mk_eq_one _ @[simp] theorem lift_add (a b : Cardinal.{u}) : lift.{v} (a + b) = lift.{v} a + lift.{v} b := inductionOn₂ a b fun _ _ => mk_congr <| Equiv.ulift.trans (Equiv.sumCongr Equiv.ulift Equiv.ulift).symm /-! ### Indexed cardinal `sum` -/ /-- The indexed sum of cardinals is the cardinality of the indexed disjoint union, i.e. sigma type. -/ def sum {ι} (f : ι → Cardinal) : Cardinal := mk (Σ i, (f i).out) @[simp] theorem mk_sigma {ι} (f : ι → Type*) : #(Σ i, f i) = sum fun i => #(f i) := mk_congr <| Equiv.sigmaCongrRight fun _ => outMkEquiv.symm theorem mk_sigma_congr_lift {ι : Type v} {ι' : Type v'} {f : ι → Type w} {g : ι' → Type w'} (e : ι ≃ ι') (h : ∀ i, lift.{w'} #(f i) = lift.{w} #(g (e i))) : lift.{max v' w'} #(Σ i, f i) = lift.{max v w} #(Σ i, g i) := Cardinal.lift_mk_eq'.2 ⟨.sigmaCongr e fun i ↦ Classical.choice <| Cardinal.lift_mk_eq'.1 (h i)⟩ theorem mk_sigma_congr {ι ι' : Type u} {f : ι → Type v} {g : ι' → Type v} (e : ι ≃ ι') (h : ∀ i, #(f i) = #(g (e i))) : #(Σ i, f i) = #(Σ i, g i) := mk_congr <| Equiv.sigmaCongr e fun i ↦ Classical.choice <| Cardinal.eq.mp (h i) /-- Similar to `mk_sigma_congr` with indexing types in different universes. This is not a strict generalization. -/ theorem mk_sigma_congr' {ι : Type u} {ι' : Type v} {f : ι → Type max w (max u v)} {g : ι' → Type max w (max u v)} (e : ι ≃ ι') (h : ∀ i, #(f i) = #(g (e i))) : #(Σ i, f i) = #(Σ i, g i) := mk_congr <| Equiv.sigmaCongr e fun i ↦ Classical.choice <| Cardinal.eq.mp (h i) theorem mk_sigma_congrRight {ι : Type u} {f g : ι → Type v} (h : ∀ i, #(f i) = #(g i)) : #(Σ i, f i) = #(Σ i, g i) := mk_sigma_congr (Equiv.refl ι) h theorem mk_psigma_congrRight {ι : Type u} {f g : ι → Type v} (h : ∀ i, #(f i) = #(g i)) : #(Σ' i, f i) = #(Σ' i, g i) := mk_congr <| .psigmaCongrRight fun i => Classical.choice <| Cardinal.eq.mp (h i) theorem mk_psigma_congrRight_prop {ι : Prop} {f g : ι → Type v} (h : ∀ i, #(f i) = #(g i)) : #(Σ' i, f i) = #(Σ' i, g i) := mk_congr <| .psigmaCongrRight fun i => Classical.choice <| Cardinal.eq.mp (h i) theorem mk_sigma_arrow {ι} (α : Type*) (f : ι → Type*) : #(Sigma f → α) = #(Π i, f i → α) := mk_congr <| Equiv.piCurry fun _ _ ↦ α @[simp] theorem sum_const (ι : Type u) (a : Cardinal.{v}) : (sum fun _ : ι => a) = lift.{v} #ι * lift.{u} a := inductionOn a fun α => mk_congr <| calc (Σ _ : ι, Quotient.out #α) ≃ ι × Quotient.out #α := Equiv.sigmaEquivProd _ _ _ ≃ ULift ι × ULift α := Equiv.ulift.symm.prodCongr (outMkEquiv.trans Equiv.ulift.symm) theorem sum_const' (ι : Type u) (a : Cardinal.{u}) : (sum fun _ : ι => a) = #ι * a := by simp @[simp] theorem lift_sum {ι : Type u} (f : ι → Cardinal.{v}) : Cardinal.lift.{w} (Cardinal.sum f) = Cardinal.sum fun i => Cardinal.lift.{w} (f i) := Equiv.cardinal_eq <| Equiv.ulift.trans <| Equiv.sigmaCongrRight fun a => -- Porting note: Inserted universe hint .{_,_,v} below Nonempty.some <| by rw [← lift_mk_eq.{_, _, v}, mk_out, mk_out, lift_lift] theorem sum_nat_eq_add_sum_succ (f : ℕ → Cardinal.{u}) : Cardinal.sum f = f 0 + Cardinal.sum fun i => f (i + 1) := by refine (Equiv.sigmaNatSucc fun i => Quotient.out (f i)).cardinal_eq.trans ?_ simp only [mk_sum, mk_out, lift_id, mk_sigma] /-! ### Indexed cardinal `prod` -/ /-- The indexed product of cardinals is the cardinality of the Pi type (dependent product). -/ def prod {ι : Type u} (f : ι → Cardinal) : Cardinal := #(Π i, (f i).out) @[simp] theorem mk_pi {ι : Type u} (α : ι → Type v) : #(Π i, α i) = prod fun i => #(α i) := mk_congr <| Equiv.piCongrRight fun _ => outMkEquiv.symm theorem mk_pi_congr_lift {ι : Type v} {ι' : Type v'} {f : ι → Type w} {g : ι' → Type w'} (e : ι ≃ ι') (h : ∀ i, lift.{w'} #(f i) = lift.{w} #(g (e i))) : lift.{max v' w'} #(Π i, f i) = lift.{max v w} #(Π i, g i) := Cardinal.lift_mk_eq'.2 ⟨.piCongr e fun i ↦ Classical.choice <| Cardinal.lift_mk_eq'.1 (h i)⟩ theorem mk_pi_congr {ι ι' : Type u} {f : ι → Type v} {g : ι' → Type v} (e : ι ≃ ι') (h : ∀ i, #(f i) = #(g (e i))) : #(Π i, f i) = #(Π i, g i) := mk_congr <| Equiv.piCongr e fun i ↦ Classical.choice <| Cardinal.eq.mp (h i) theorem mk_pi_congr_prop {ι ι' : Prop} {f : ι → Type v} {g : ι' → Type v} (e : ι ↔ ι') (h : ∀ i, #(f i) = #(g (e.mp i))) : #(Π i, f i) = #(Π i, g i) := mk_congr <| Equiv.piCongr (.ofIff e) fun i ↦ Classical.choice <| Cardinal.eq.mp (h i) /-- Similar to `mk_pi_congr` with indexing types in different universes. This is not a strict generalization. -/ theorem mk_pi_congr' {ι : Type u} {ι' : Type v} {f : ι → Type max w (max u v)} {g : ι' → Type max w (max u v)} (e : ι ≃ ι') (h : ∀ i, #(f i) = #(g (e i))) : #(Π i, f i) = #(Π i, g i) := mk_congr <| Equiv.piCongr e fun i ↦ Classical.choice <| Cardinal.eq.mp (h i) theorem mk_pi_congrRight {ι : Type u} {f g : ι → Type v} (h : ∀ i, #(f i) = #(g i)) : #(Π i, f i) = #(Π i, g i) := mk_pi_congr (Equiv.refl ι) h theorem mk_pi_congrRight_prop {ι : Prop} {f g : ι → Type v} (h : ∀ i, #(f i) = #(g i)) : #(Π i, f i) = #(Π i, g i) := mk_pi_congr_prop Iff.rfl h @[simp] theorem prod_const (ι : Type u) (a : Cardinal.{v}) : (prod fun _ : ι => a) = lift.{u} a ^ lift.{v} #ι := inductionOn a fun _ => mk_congr <| Equiv.piCongr Equiv.ulift.symm fun _ => outMkEquiv.trans Equiv.ulift.symm theorem prod_const' (ι : Type u) (a : Cardinal.{u}) : (prod fun _ : ι => a) = a ^ #ι := inductionOn a fun _ => (mk_pi _).symm @[simp] theorem prod_eq_zero {ι} (f : ι → Cardinal.{u}) : prod f = 0 ↔ ∃ i, f i = 0 := by lift f to ι → Type u using fun _ => trivial simp only [mk_eq_zero_iff, ← mk_pi, isEmpty_pi] theorem prod_ne_zero {ι} (f : ι → Cardinal) : prod f ≠ 0 ↔ ∀ i, f i ≠ 0 := by simp [prod_eq_zero] theorem lift_power_sum {ι : Type u} (a : Cardinal.{v}) (f : ι → Cardinal.{v}) : lift.{u, v} a ^ sum f = prod fun i ↦ a ^ f i := by induction a using Cardinal.inductionOn with | _ α => induction f using induction_on_pi with | _ f => simp_rw [← mk_uLift, prod, sum, power_def] apply mk_congr refine (Equiv.piCurry fun _ _ => ULift α).trans ?_ refine Equiv.piCongrRight fun b => ?_ refine (Equiv.arrowCongr outMkEquiv Equiv.ulift).trans ?_ exact outMkEquiv.symm theorem power_sum {ι : Type u} (a : Cardinal.{max u v}) (f : ι → Cardinal.{max u v}) : a ^ sum f = prod fun i ↦ a ^ f i := by simpa [← lift_umax] using lift_power_sum a f @[simp] theorem lift_prod {ι : Type u} (c : ι → Cardinal.{v}) : lift.{w} (prod c) = prod fun i => lift.{w} (c i) := by lift c to ι → Type v using fun _ => trivial simp only [← mk_pi, ← mk_uLift] exact mk_congr (Equiv.ulift.trans <| Equiv.piCongrRight fun i => Equiv.ulift.symm) /-! ### The first infinite cardinal `aleph0` -/ /-- `ℵ₀` is the smallest infinite cardinal. -/ def aleph0 : Cardinal.{u} := lift #ℕ @[inherit_doc] scoped notation "ℵ₀" => Cardinal.aleph0 theorem mk_nat : #ℕ = ℵ₀ := (lift_id _).symm theorem aleph0_ne_zero : ℵ₀ ≠ 0 := mk_ne_zero _ @[simp] theorem lift_aleph0 : lift ℵ₀ = ℵ₀ := lift_lift _ theorem lift_mk_fin (n : ℕ) : lift #(Fin n) = n := rfl /-! ### Cardinalities of basic sets and types -/ theorem mk_empty : #Empty = 0 := mk_eq_zero _ theorem mk_pempty : #PEmpty = 0 := mk_eq_zero _ theorem mk_punit : #PUnit = 1 := mk_eq_one PUnit theorem mk_unit : #Unit = 1 := mk_punit theorem mk_plift_true : #(PLift True) = 1 := mk_eq_one _ theorem mk_plift_false : #(PLift False) = 0 := mk_eq_zero _ theorem mk_subtype_of_equiv {α β : Type u} (p : β → Prop) (e : α ≃ β) : #{ a : α // p (e a) } = #{ b : β // p b } := mk_congr (Equiv.subtypeEquivOfSubtype e) end Cardinal -- namespace Tactic -- open Cardinal Positivity -- Porting note: Meta code, do not port directly -- /-- Extension for the `positivity` tactic: The cardinal power of a positive cardinal is -- positive. -/ -- @[positivity] -- unsafe def positivity_cardinal_pow : expr → tactic strictness -- | q(@Pow.pow _ _ $(inst) $(a) $(b)) => do -- let strictness_a ← core a -- match strictness_a with -- | positive p => positive <$> mk_app `` power_pos [b, p] -- | _ => failed -- |-- We already know that `0 ≤ x` for all `x : Cardinal` -- _ => -- failed -- end Tactic
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Finsupp.lean
import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.Data.Finsupp.Basic import Mathlib.Data.Finsupp.Multiset /-! # Results on the cardinality of finitely supported functions and multisets. -/ universe u v namespace Cardinal @[simp] theorem mk_finsupp_lift_of_fintype (α : Type u) (β : Type v) [Fintype α] [Zero β] : #(α →₀ β) = lift.{u} #β ^ Fintype.card α := by simpa using (@Finsupp.equivFunOnFinite α β _ _).cardinal_eq theorem mk_finsupp_of_fintype (α β : Type u) [Fintype α] [Zero β] : #(α →₀ β) = #β ^ Fintype.card α := by simp @[simp] theorem mk_finsupp_lift_of_infinite (α : Type u) (β : Type v) [Infinite α] [Zero β] [Nontrivial β] : #(α →₀ β) = max (lift.{v} #α) (lift.{u} #β) := by apply le_antisymm · calc #(α →₀ β) ≤ #(Finset (α × β)) := mk_le_of_injective (Finsupp.graph_injective α β) _ = #(α × β) := mk_finset_of_infinite _ _ = max (lift.{v} #α) (lift.{u} #β) := by rw [mk_prod, mul_eq_max_of_aleph0_le_left] <;> simp · apply max_le <;> rw [← lift_id #(α →₀ β), ← lift_umax] · obtain ⟨b, hb⟩ := exists_ne (0 : β) exact lift_mk_le.{v}.2 ⟨⟨_, Finsupp.single_left_injective hb⟩⟩ · inhabit α exact lift_mk_le.{u}.2 ⟨⟨_, Finsupp.single_injective default⟩⟩ theorem mk_finsupp_of_infinite (α β : Type u) [Infinite α] [Zero β] [Nontrivial β] : #(α →₀ β) = max #α #β := by simp @[simp] theorem mk_finsupp_lift_of_infinite' (α : Type u) (β : Type v) [Nonempty α] [Zero β] [Infinite β] : #(α →₀ β) = max (lift.{v} #α) (lift.{u} #β) := by cases fintypeOrInfinite α · rw [mk_finsupp_lift_of_fintype] have : ℵ₀ ≤ (#β).lift := aleph0_le_lift.2 (aleph0_le_mk β) rw [max_eq_right (le_trans _ this), power_nat_eq this] exacts [Fintype.card_pos, lift_le_aleph0.2 (lt_aleph0_of_finite _).le] · apply mk_finsupp_lift_of_infinite theorem mk_finsupp_of_infinite' (α β : Type u) [Nonempty α] [Zero β] [Infinite β] : #(α →₀ β) = max #α #β := by simp theorem mk_finsupp_nat (α : Type u) [Nonempty α] : #(α →₀ ℕ) = max #α ℵ₀ := by simp theorem mk_multiset_of_isEmpty (α : Type u) [IsEmpty α] : #(Multiset α) = 1 := Multiset.toFinsupp.toEquiv.cardinal_eq.trans (by simp) @[simp] theorem mk_multiset_of_nonempty (α : Type u) [Nonempty α] : #(Multiset α) = max #α ℵ₀ := by classical exact Multiset.toFinsupp.toEquiv.cardinal_eq.trans (mk_finsupp_nat α) theorem mk_multiset_of_infinite (α : Type u) [Infinite α] : #(Multiset α) = #α := by simp @[deprecated (since := "2025-10-06")] alias mk_multiset_of_countable := mk_eq_aleph0 end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/ToNat.lean
import Mathlib.SetTheory.Cardinal.ENat /-! # Projection from cardinal numbers to natural numbers In this file we define `Cardinal.toNat` to be the natural projection `Cardinal → ℕ`, sending all infinite cardinals to zero. We also prove basic lemmas about this definition. -/ assert_not_exists Field universe u v open Function Set namespace Cardinal variable {α : Type u} {c d : Cardinal.{u}} /-- This function sends finite cardinals to the corresponding natural, and infinite cardinals to 0. -/ noncomputable def toNat : Cardinal →*₀ ℕ := ENat.toNatHom.comp toENat @[simp] lemma toNat_toENat (a : Cardinal) : ENat.toNat (toENat a) = toNat a := rfl @[simp] theorem toNat_ofENat (n : ℕ∞) : toNat n = ENat.toNat n := congr_arg ENat.toNat <| toENat_ofENat n @[simp, norm_cast] theorem toNat_natCast (n : ℕ) : toNat n = n := toNat_ofENat n @[simp] lemma toNat_eq_zero : toNat c = 0 ↔ c = 0 ∨ ℵ₀ ≤ c := by rw [← toNat_toENat, ENat.toNat_eq_zero, toENat_eq_zero, toENat_eq_top] lemma toNat_ne_zero : toNat c ≠ 0 ↔ c ≠ 0 ∧ c < ℵ₀ := by simp [not_or] @[simp] lemma toNat_pos : 0 < toNat c ↔ c ≠ 0 ∧ c < ℵ₀ := pos_iff_ne_zero.trans toNat_ne_zero theorem cast_toNat_of_lt_aleph0 {c : Cardinal} (h : c < ℵ₀) : ↑(toNat c) = c := by lift c to ℕ using h rw [toNat_natCast] theorem toNat_apply_of_lt_aleph0 {c : Cardinal.{u}} (h : c < ℵ₀) : toNat c = Classical.choose (lt_aleph0.1 h) := Nat.cast_injective (R := Cardinal.{u}) <| by rw [cast_toNat_of_lt_aleph0 h, ← Classical.choose_spec (lt_aleph0.1 h)] theorem toNat_apply_of_aleph0_le {c : Cardinal} (h : ℵ₀ ≤ c) : toNat c = 0 := by simp [h] theorem cast_toNat_of_aleph0_le {c : Cardinal} (h : ℵ₀ ≤ c) : ↑(toNat c) = (0 : Cardinal) := by rw [toNat_apply_of_aleph0_le h, Nat.cast_zero] theorem cast_toNat_eq_iff_lt_aleph0 {c : Cardinal} : (toNat c) = c ↔ c < ℵ₀ := by constructor · intro h; by_contra h'; rw [not_lt] at h' rw [toNat_apply_of_aleph0_le h'] at h; rw [← h] at h' absurd h'; rw [not_le]; exact nat_lt_aleph0 0 · exact fun h ↦ (Cardinal.cast_toNat_of_lt_aleph0 h) theorem toNat_strictMonoOn : StrictMonoOn toNat (Iio ℵ₀) := by simp only [← range_natCast, StrictMonoOn, forall_mem_range, toNat_natCast, Nat.cast_lt] exact fun _ _ ↦ id theorem toNat_monotoneOn : MonotoneOn toNat (Iio ℵ₀) := toNat_strictMonoOn.monotoneOn theorem toNat_injOn : InjOn toNat (Iio ℵ₀) := toNat_strictMonoOn.injOn /-- Two finite cardinals are equal iff they are equal their `Cardinal.toNat` projections are equal. -/ theorem toNat_inj_of_lt_aleph0 (hc : c < ℵ₀) (hd : d < ℵ₀) : toNat c = toNat d ↔ c = d := toNat_injOn.eq_iff hc hd theorem toNat_le_iff_le_of_lt_aleph0 (hc : c < ℵ₀) (hd : d < ℵ₀) : toNat c ≤ toNat d ↔ c ≤ d := toNat_strictMonoOn.le_iff_le hc hd theorem toNat_lt_iff_lt_of_lt_aleph0 (hc : c < ℵ₀) (hd : d < ℵ₀) : toNat c < toNat d ↔ c < d := toNat_strictMonoOn.lt_iff_lt hc hd @[gcongr] theorem toNat_le_toNat (hcd : c ≤ d) (hd : d < ℵ₀) : toNat c ≤ toNat d := toNat_monotoneOn (hcd.trans_lt hd) hd hcd theorem toNat_lt_toNat (hcd : c < d) (hd : d < ℵ₀) : toNat c < toNat d := toNat_strictMonoOn (hcd.trans hd) hd hcd @[simp] theorem toNat_ofNat (n : ℕ) [n.AtLeastTwo] : Cardinal.toNat ofNat(n) = OfNat.ofNat n := toNat_natCast n /-- `toNat` has a right-inverse: coercion. -/ theorem toNat_rightInverse : Function.RightInverse ((↑) : ℕ → Cardinal) toNat := toNat_natCast theorem toNat_surjective : Surjective toNat := toNat_rightInverse.surjective @[simp] theorem mk_toNat_of_infinite [h : Infinite α] : toNat #α = 0 := by simp @[simp] theorem aleph0_toNat : toNat ℵ₀ = 0 := toNat_apply_of_aleph0_le le_rfl theorem mk_toNat_eq_card [Fintype α] : toNat #α = Fintype.card α := by simp @[simp] theorem zero_toNat : toNat 0 = 0 := map_zero _ theorem one_toNat : toNat 1 = 1 := map_one _ theorem toNat_eq_iff {n : ℕ} (hn : n ≠ 0) : toNat c = n ↔ c = n := by rw [← toNat_toENat, ENat.toNat_eq_iff hn, toENat_eq_nat] /-- A version of `toNat_eq_iff` for literals -/ theorem toNat_eq_ofNat {n : ℕ} [Nat.AtLeastTwo n] : toNat c = OfNat.ofNat n ↔ c = OfNat.ofNat n := toNat_eq_iff <| OfNat.ofNat_ne_zero n @[simp] theorem toNat_eq_one : toNat c = 1 ↔ c = 1 := by rw [toNat_eq_iff one_ne_zero, Nat.cast_one] theorem toNat_eq_one_iff_unique : toNat #α = 1 ↔ Subsingleton α ∧ Nonempty α := toNat_eq_one.trans eq_one_iff_unique @[simp] theorem toNat_lift (c : Cardinal.{v}) : toNat (lift.{u, v} c) = toNat c := by simp only [← toNat_toENat, toENat_lift] theorem toNat_congr {β : Type v} (e : α ≃ β) : toNat #α = toNat #β := by -- Porting note: Inserted universe hint below rw [← toNat_lift, (lift_mk_eq.{_,_,v}).mpr ⟨e⟩, toNat_lift] theorem toNat_mul (x y : Cardinal) : toNat (x * y) = toNat x * toNat y := map_mul toNat x y @[simp] theorem toNat_add (hc : c < ℵ₀) (hd : d < ℵ₀) : toNat (c + d) = toNat c + toNat d := by lift c to ℕ using hc lift d to ℕ using hd norm_cast theorem toNat_lift_add_lift {a : Cardinal.{u}} {b : Cardinal.{v}} (ha : a < ℵ₀) (hb : b < ℵ₀) : toNat (lift.{v} a + lift.{u} b) = toNat a + toNat b := by simp [*] @[simp] lemma natCast_toNat_le (a : Cardinal) : (toNat a : Cardinal) ≤ a := by obtain h | h := lt_or_ge a ℵ₀ · simp [cast_toNat_of_lt_aleph0 h] · simp [Cardinal.toNat_apply_of_aleph0_le h] end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Ordinal.lean
import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.SetTheory.Ordinal.Principal /-! # Ordinal arithmetic with cardinals This file collects results about the cardinality of different ordinal operations. -/ universe u v open Cardinal Ordinal Set /-! ### Cardinal operations with ordinal indices -/ namespace Cardinal /-- Bounds the cardinal of an ordinal-indexed union of sets. -/ lemma mk_iUnion_Ordinal_lift_le_of_le {β : Type v} {o : Ordinal.{u}} {c : Cardinal.{v}} (ho : lift.{v} o.card ≤ lift.{u} c) (hc : ℵ₀ ≤ c) (A : Ordinal → Set β) (hA : ∀ j < o, #(A j) ≤ c) : #(⋃ j < o, A j) ≤ c := by simp_rw [← mem_Iio, biUnion_eq_iUnion, iUnion, iSup, ← o.enumIsoToType.symm.surjective.range_comp] rw [← lift_le.{u}] apply ((mk_iUnion_le_lift _).trans _).trans_eq (mul_eq_self (aleph0_le_lift.2 hc)) rw [mk_toType] refine mul_le_mul' ho (ciSup_le' ?_) intro i simpa using hA _ (o.enumIsoToType.symm i).2 lemma mk_iUnion_Ordinal_le_of_le {β : Type*} {o : Ordinal} {c : Cardinal} (ho : o.card ≤ c) (hc : ℵ₀ ≤ c) (A : Ordinal → Set β) (hA : ∀ j < o, #(A j) ≤ c) : #(⋃ j < o, A j) ≤ c := by apply mk_iUnion_Ordinal_lift_le_of_le _ hc A hA rwa [Cardinal.lift_le] end Cardinal /-! ### Cardinality of ordinals -/ namespace Ordinal theorem lift_card_iSup_le_sum_card {ι : Type u} [Small.{v} ι] (f : ι → Ordinal.{v}) : Cardinal.lift.{u} (⨆ i, f i).card ≤ Cardinal.sum fun i ↦ (f i).card := by simp_rw [← mk_toType] rw [← mk_sigma, ← Cardinal.lift_id'.{v} #(Σ _, _), ← Cardinal.lift_umax.{v, u}] apply lift_mk_le_lift_mk_of_surjective (f := enumIsoToType _ ∘ (⟨(enumIsoToType _).symm ·.2, (mem_Iio.mp ((enumIsoToType _).symm _).2).trans_le (Ordinal.le_iSup _ _)⟩)) rw [EquivLike.comp_surjective] rintro ⟨x, hx⟩ obtain ⟨i, hi⟩ := Ordinal.lt_iSup_iff.mp hx exact ⟨⟨i, enumIsoToType _ ⟨x, hi⟩⟩, by simp⟩ theorem card_iSup_le_sum_card {ι : Type u} (f : ι → Ordinal.{max u v}) : (⨆ i, f i).card ≤ Cardinal.sum (fun i ↦ (f i).card) := by have := lift_card_iSup_le_sum_card f rwa [Cardinal.lift_id'] at this theorem card_iSup_Iio_le_sum_card {o : Ordinal.{u}} (f : Iio o → Ordinal.{max u v}) : (⨆ a : Iio o, f a).card ≤ Cardinal.sum fun i ↦ (f ((enumIsoToType o).symm i)).card := by apply le_of_eq_of_le (congr_arg _ _).symm (card_iSup_le_sum_card _) simpa using (enumIsoToType o).symm.iSup_comp (g := fun x ↦ f x) theorem card_iSup_Iio_le_card_mul_iSup {o : Ordinal.{u}} (f : Iio o → Ordinal.{max u v}) : (⨆ a : Iio o, f a).card ≤ Cardinal.lift.{v} o.card * ⨆ a : Iio o, (f a).card := by apply (card_iSup_Iio_le_sum_card f).trans convert ← sum_le_lift_mk_mul_iSup _ · exact mk_toType o · exact (enumIsoToType o).symm.iSup_comp (g := fun x ↦ (f x).card) theorem card_opow_le_of_omega0_le_left {a : Ordinal} (ha : ω ≤ a) (b : Ordinal) : (a ^ b).card ≤ max a.card b.card := by refine limitRecOn b ?_ ?_ ?_ · simpa using one_lt_omega0.le.trans ha · intro b IH rw [opow_succ, card_mul, card_succ, Cardinal.mul_eq_max_of_aleph0_le_right, max_comm] · grw [IH] rw [← max_assoc, max_self] grw [← le_self_add] · rw [ne_eq, card_eq_zero, opow_eq_zero] rintro ⟨rfl, -⟩ cases omega0_pos.not_ge ha · rwa [aleph0_le_card] · intro b hb IH rw [(isNormal_opow (one_lt_omega0.trans_le ha)).apply_of_isSuccLimit hb] apply (card_iSup_Iio_le_card_mul_iSup _).trans rw [Cardinal.lift_id, Cardinal.mul_eq_max_of_aleph0_le_right, max_comm] · apply max_le _ (le_max_right _ _) apply ciSup_le' rintro ⟨c, (hcb : c < b)⟩ grw [IH c hcb, hcb] · simpa using hb.ne_bot · refine le_ciSup_of_le ?_ ⟨1, one_lt_omega0.trans_le <| omega0_le_of_isSuccLimit hb⟩ ?_ · exact Cardinal.bddAbove_of_small _ · simpa theorem card_opow_le_of_omega0_le_right (a : Ordinal) {b : Ordinal} (hb : ω ≤ b) : (a ^ b).card ≤ max a.card b.card := by obtain ⟨n, rfl⟩ | ha := eq_nat_or_omega0_le a · apply (card_le_card <| opow_le_opow_left b (nat_lt_omega0 n).le).trans apply (card_opow_le_of_omega0_le_left le_rfl _).trans simp [hb] · exact card_opow_le_of_omega0_le_left ha b theorem card_opow_le (a b : Ordinal) : (a ^ b).card ≤ max ℵ₀ (max a.card b.card) := by obtain ⟨n, rfl⟩ | ha := eq_nat_or_omega0_le a · obtain ⟨m, rfl⟩ | hb := eq_nat_or_omega0_le b · rw [← natCast_opow, card_nat] exact le_max_of_le_left (nat_lt_aleph0 _).le · exact (card_opow_le_of_omega0_le_right _ hb).trans (le_max_right _ _) · exact (card_opow_le_of_omega0_le_left ha _).trans (le_max_right _ _) theorem card_opow_eq_of_omega0_le_left {a b : Ordinal} (ha : ω ≤ a) (hb : 0 < b) : (a ^ b).card = max a.card b.card := by apply (card_opow_le_of_omega0_le_left ha b).antisymm (max_le _ _) <;> apply card_le_card · exact left_le_opow a hb · exact right_le_opow b (one_lt_omega0.trans_le ha) theorem card_opow_eq_of_omega0_le_right {a b : Ordinal} (ha : 1 < a) (hb : ω ≤ b) : (a ^ b).card = max a.card b.card := by apply (card_opow_le_of_omega0_le_right a hb).antisymm (max_le _ _) <;> apply card_le_card · exact left_le_opow a (omega0_pos.trans_le hb) · exact right_le_opow b ha theorem card_omega0_opow {a : Ordinal} (h : a ≠ 0) : card (ω ^ a) = max ℵ₀ a.card := by rw [card_opow_eq_of_omega0_le_left le_rfl h.bot_lt, card_omega0] theorem card_opow_omega0 {a : Ordinal} (h : 1 < a) : card (a ^ ω) = max ℵ₀ a.card := by rw [card_opow_eq_of_omega0_le_right h le_rfl, card_omega0, max_comm] theorem principal_opow_omega (o : Ordinal) : Principal (· ^ ·) (ω_ o) := by obtain rfl | ho := Ordinal.eq_zero_or_pos o · rw [omega_zero] exact principal_opow_omega0 · intro a b ha hb rw [lt_omega_iff_card_lt] at ha hb ⊢ apply (card_opow_le a b).trans_lt (max_lt _ (max_lt ha hb)) rwa [← aleph_zero, aleph_lt_aleph] theorem IsInitial.principal_opow {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· ^ ·) o := by obtain ⟨a, rfl⟩ := mem_range_omega_iff.2 ⟨ho, h⟩ exact principal_opow_omega a theorem principal_opow_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· ^ ·) c.ord := by apply (isInitial_ord c).principal_opow rwa [omega0_le_ord] /-! ### Initial ordinals are principal -/ theorem principal_add_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· + ·) c.ord := by intro a b ha hb rw [lt_ord, card_add] at * exact add_lt_of_lt hc ha hb theorem IsInitial.principal_add {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· + ·) o := by rw [← h.ord_card] apply principal_add_ord rwa [aleph0_le_card] theorem principal_add_omega (o : Ordinal) : Principal (· + ·) (ω_ o) := (isInitial_omega o).principal_add (omega0_le_omega o) theorem principal_mul_ord {c : Cardinal} (hc : ℵ₀ ≤ c) : Principal (· * ·) c.ord := by intro a b ha hb rw [lt_ord, card_mul] at * exact mul_lt_of_lt hc ha hb theorem IsInitial.principal_mul {o : Ordinal} (h : IsInitial o) (ho : ω ≤ o) : Principal (· * ·) o := by rw [← h.ord_card] apply principal_mul_ord rwa [aleph0_le_card] theorem principal_mul_omega (o : Ordinal) : Principal (· * ·) (ω_ o) := (isInitial_omega o).principal_mul (omega0_le_omega o) end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Embedding.lean
import Mathlib.Data.ENat.Lattice import Mathlib.Data.Fin.Tuple.Embedding import Mathlib.Data.Finite.Card import Mathlib.Data.Set.Card /-! # Existence of embeddings from finite types Let `s : Set α` be a finite set. * `Fin.Embedding.exists_embedding_disjoint_range_of_add_le_ENat_card` If `s.ncard + n ≤ ENat.card α`, then there exists an embedding `Fin n ↪ α` whose range is disjoint from `s`. * `Fin.Embedding.exists_embedding_disjoint_range_of_add_le_Nat_card` If `α` is finite and `s.ncard + n ≤ Nat.card α`, then there exists an embedding `Fin n ↪ α` whose range is disjoint from `s`. * `Fin.Embedding.restrictSurjective_of_add_le_ENatCard` If `m + n ≤ ENat.card α`, then the restriction map from `Fin (m + n) ↪ α` to `Fin m ↪ α` is surjective. * `Fin.Embedding.restrictSurjective_of_add_le_natCard` If `α` is finite and `m + n ≤ Nat.card α`, then the restriction map from `Fin (m + n) ↪ α` to `Fin m ↪ α` is surjective. -/ open Set Fin Function Function.Embedding namespace Fin.Embedding variable {α : Type*} {m n : ℕ} {s : Set α} theorem exists_embedding_disjoint_range_of_add_le_ENat_card [Finite s] (hs : s.ncard + n ≤ ENat.card α) : ∃ y : Fin n ↪ α, Disjoint s (range y) := by rsuffices ⟨y⟩ : Nonempty (Fin n ↪ (sᶜ : Set α)) · use y.trans (subtype _) rw [Set.disjoint_right] rintro _ ⟨i, rfl⟩ simpa only [← mem_compl_iff] using Subtype.coe_prop (y i) rcases finite_or_infinite α with hα | hα · let _ : Fintype α := Fintype.ofFinite α classical apply nonempty_of_card_le rwa [Fintype.card_fin, ← add_le_add_iff_left s.ncard, ← Nat.card_eq_fintype_card, Nat.card_coe_set_eq, ncard_add_ncard_compl, ← ENat.coe_le_coe, ← ENat.card_eq_coe_natCard, ENat.coe_add] · exact ⟨valEmbedding.trans s.toFinite.infinite_compl.to_subtype.natEmbedding⟩ theorem exists_embedding_disjoint_range_of_add_le_Nat_card [Finite α] (hs : s.ncard + n ≤ Nat.card α) : ∃ y : Fin n ↪ α, Disjoint s (range y) := by apply exists_embedding_disjoint_range_of_add_le_ENat_card rwa [← ENat.coe_add, ENat.card_eq_coe_natCard, ENat.coe_le_coe] theorem restrictSurjective_of_add_le_ENatCard (hn : m + n ≤ ENat.card α) : Surjective (fun (x : Fin (m + n) ↪ α) ↦ (Fin.castAddEmb n).trans x) := by intro x obtain ⟨y, hxy⟩ := exists_embedding_disjoint_range_of_add_le_ENat_card (s := range x) (by simpa [← Nat.card_coe_set_eq, Nat.card_range_of_injective x.injective]) use append hxy ext i simp [trans_apply, coe_castAddEmb, append] theorem restrictSurjective_of_le_ENatCard (hmn : m ≤ n) (hn : n ≤ ENat.card α) : Function.Surjective (fun x : Fin n ↪ α ↦ (castLEEmb hmn).trans x) := by obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn exact Fin.Embedding.restrictSurjective_of_add_le_ENatCard hn theorem restrictSurjective_of_add_le_natCard [Finite α] (hn : m + n ≤ Nat.card α) : Surjective (fun x : Fin (m + n) ↪ α ↦ (castAddEmb n).trans x) := by apply restrictSurjective_of_add_le_ENatCard rwa [← ENat.coe_add, ENat.card_eq_coe_natCard, ENat.coe_le_coe] theorem restrictSurjective_of_le_natCard [Finite α] (hmn : m ≤ n) (hn : n ≤ Nat.card α) : Function.Surjective (fun x : Fin n ↪ α ↦ (castLEEmb hmn).trans x) := by obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_le hmn exact Fin.Embedding.restrictSurjective_of_add_le_natCard hn end Fin.Embedding
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Divisibility.lean
import Mathlib.Algebra.IsPrimePow import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.Tactic.WLOG /-! # Cardinal Divisibility We show basic results about divisibility in the cardinal numbers. This relation can be characterised in the following simple way: if `a` and `b` are both less than `ℵ₀`, then `a ∣ b` iff they are divisible as natural numbers. If `b` is greater than `ℵ₀`, then `a ∣ b` iff `a ≤ b`. This furthermore shows that all infinite cardinals are prime; recall that `a * b = max a b` if `ℵ₀ ≤ a * b`; therefore `a ∣ b * c = a ∣ max b c` and therefore clearly either `a ∣ b` or `a ∣ c`. Note furthermore that no infinite cardinal is irreducible (`Cardinal.not_irreducible_of_aleph0_le`), showing that the cardinal numbers do not form a `CancelCommMonoidWithZero`. ## Main results * `Cardinal.prime_of_aleph0_le`: a `Cardinal` is prime if it is infinite. * `Cardinal.is_prime_iff`: a `Cardinal` is prime iff it is infinite or a prime natural number. * `Cardinal.isPrimePow_iff`: a `Cardinal` is a prime power iff it is infinite or a natural number which is itself a prime power. -/ namespace Cardinal universe u variable {a b : Cardinal.{u}} {n m : ℕ} /-- Alias of `isUnit_iff_eq_one` for discoverability. -/ theorem isUnit_iff : IsUnit a ↔ a = 1 := by refine ⟨fun h => ?_, by rintro rfl exact isUnit_one⟩ rcases eq_or_ne a 0 with (rfl | ha) · exact (not_isUnit_zero h).elim rw [isUnit_iff_forall_dvd] at h obtain ⟨t, ht⟩ := h 1 rw [eq_comm, mul_eq_one_iff_of_one_le] at ht · exact ht.1 · exact one_le_iff_ne_zero.mpr ha · apply one_le_iff_ne_zero.mpr intro h rw [h, mul_zero] at ht exact zero_ne_one ht instance : Unique Cardinal.{u}ˣ where default := 1 uniq a := Units.val_eq_one.mp <| isUnit_iff.mp a.isUnit theorem le_of_dvd : ∀ {a b : Cardinal}, b ≠ 0 → a ∣ b → a ≤ b | a, x, b0, ⟨b, hab⟩ => by simpa only [hab, mul_one] using mul_le_mul_left' (one_le_iff_ne_zero.2 fun h : b = 0 => b0 (by rwa [h, mul_zero] at hab)) a theorem dvd_of_le_of_aleph0_le (ha : a ≠ 0) (h : a ≤ b) (hb : ℵ₀ ≤ b) : a ∣ b := ⟨b, (mul_eq_right hb h ha).symm⟩ @[simp] theorem prime_of_aleph0_le (ha : ℵ₀ ≤ a) : Prime a := by refine ⟨(aleph0_pos.trans_le ha).ne', ?_, fun b c hbc => ?_⟩ · rw [isUnit_iff] exact (one_lt_aleph0.trans_le ha).ne' rcases eq_or_ne (b * c) 0 with hz | hz · rcases mul_eq_zero.mp hz with (rfl | rfl) <;> simp wlog h : c ≤ b · cases le_total c b <;> [solve_by_elim; rw [or_comm]] apply_assumption assumption' all_goals rwa [mul_comm] left have habc := le_of_dvd hz hbc rwa [mul_eq_max' <| ha.trans <| habc, max_def', if_pos h] at hbc theorem not_irreducible_of_aleph0_le (ha : ℵ₀ ≤ a) : ¬Irreducible a := by rw [irreducible_iff, not_and_or] refine Or.inr fun h => ?_ simpa [mul_aleph0_eq ha, isUnit_iff, (one_lt_aleph0.trans_le ha).ne', one_lt_aleph0.ne'] using @h a ℵ₀ @[simp, norm_cast] theorem nat_coe_dvd_iff : (n : Cardinal) ∣ m ↔ n ∣ m := by refine ⟨?_, fun ⟨h, ht⟩ => ⟨h, mod_cast ht⟩⟩ rintro ⟨k, hk⟩ have : ↑m < ℵ₀ := nat_lt_aleph0 m rw [hk, mul_lt_aleph0_iff] at this rcases this with (h | h | ⟨-, hk'⟩) iterate 2 simp only [h, mul_zero, zero_mul, Nat.cast_eq_zero] at hk; simp [hk] lift k to ℕ using hk' exact ⟨k, mod_cast hk⟩ @[simp] theorem nat_is_prime_iff : Prime (n : Cardinal) ↔ n.Prime := by simp only [Prime, Nat.prime_iff] refine and_congr (by simp) (and_congr ?_ ⟨fun h b c hbc => ?_, fun h b c hbc => ?_⟩) · simp only [isUnit_iff, Nat.isUnit_iff] exact mod_cast Iff.rfl · exact mod_cast h b c (mod_cast hbc) rcases lt_or_ge (b * c) ℵ₀ with h' | h' · rcases mul_lt_aleph0_iff.mp h' with (rfl | rfl | ⟨hb, hc⟩) · simp · simp lift b to ℕ using hb lift c to ℕ using hc exact mod_cast h b c (mod_cast hbc) rcases aleph0_le_mul_iff.mp h' with ⟨hb, hc, hℵ₀⟩ have hn : (n : Cardinal) ≠ 0 := by intro h rw [h, zero_dvd_iff, mul_eq_zero] at hbc cases hbc <;> contradiction wlog hℵ₀b : ℵ₀ ≤ b apply (this h c b _ _ hc hb hℵ₀.symm hn (hℵ₀.resolve_left hℵ₀b)).symm <;> try assumption · rwa [mul_comm] at hbc · rwa [mul_comm] at h' · exact Or.inl (dvd_of_le_of_aleph0_le hn ((nat_lt_aleph0 n).le.trans hℵ₀b) hℵ₀b) theorem is_prime_iff {a : Cardinal} : Prime a ↔ ℵ₀ ≤ a ∨ ∃ p : ℕ, a = p ∧ p.Prime := by rcases le_or_gt ℵ₀ a with h | h · simp [h] lift a to ℕ using id h simp [not_le.mpr h] theorem isPrimePow_iff {a : Cardinal} : IsPrimePow a ↔ ℵ₀ ≤ a ∨ ∃ n : ℕ, a = n ∧ IsPrimePow n := by by_cases h : ℵ₀ ≤ a · simp [h, (prime_of_aleph0_le h).isPrimePow] simp only [h, false_or, isPrimePow_nat_iff] lift a to ℕ using not_le.mp h rw [isPrimePow_def] refine ⟨?_, fun ⟨n, han, p, k, hp, hk, h⟩ => ⟨p, k, nat_is_prime_iff.2 hp, hk, by rw [han]; exact mod_cast h⟩⟩ rintro ⟨p, k, hp, hk, hpk⟩ have key : p ^ (1 : Cardinal) ≤ ↑a := by rw [← hpk]; apply power_le_power_left hp.ne_zero; exact mod_cast hk rw [power_one] at key lift p to ℕ using key.trans_lt (nat_lt_aleph0 a) exact ⟨a, rfl, p, k, nat_is_prime_iff.mp hp, hk, mod_cast hpk⟩ end Cardinal
.lake/packages/mathlib/Mathlib/SetTheory/Cardinal/Free.lean
import Mathlib.Algebra.FreeAbelianGroup.Finsupp import Mathlib.Algebra.Ring.TransferInstance import Mathlib.Data.Finsupp.Fintype import Mathlib.Data.ZMod.Defs import Mathlib.GroupTheory.FreeGroup.Reduce import Mathlib.RingTheory.FreeCommRing import Mathlib.SetTheory.Cardinal.Arithmetic import Mathlib.SetTheory.Cardinal.Finsupp /-! # Cardinalities of free constructions This file shows that all the free constructions over `α` have cardinality `max #α ℵ₀`, and are thus infinite, and specifically countable over countable generators. Combined with the ring `Fin n` for the finite cases, this lets us show that there is a `CommRing` of any cardinality. -/ universe u variable (α : Type u) section Infinite @[to_additive] instance [Nonempty α] : Infinite (FreeMonoid α) := inferInstanceAs <| Infinite (List α) @[to_additive] instance [Nonempty α] : Infinite (FreeGroup α) := by classical exact Infinite.of_surjective FreeGroup.norm FreeGroup.norm_surjective instance [Nonempty α] : Infinite (FreeAbelianGroup α) := (FreeAbelianGroup.equivFinsupp α).toEquiv.infinite_iff.2 inferInstance instance : Infinite (FreeRing α) := by unfold FreeRing; infer_instance instance : Infinite (FreeCommRing α) := by unfold FreeCommRing; infer_instance end Infinite section Countable variable [Countable α] @[to_additive] instance : Countable (FreeMonoid α) := by unfold FreeMonoid; infer_instance @[to_additive] instance : Countable (FreeGroup α) := Quotient.countable instance : Countable (FreeAbelianGroup α) := Quotient.countable instance : Countable (FreeRing α) := Quotient.countable instance : Countable (FreeCommRing α) := by unfold FreeCommRing Multiplicative infer_instance end Countable namespace Cardinal theorem mk_abelianization_le (G : Type u) [Group G] : #(Abelianization G) ≤ #G := Cardinal.mk_le_of_surjective Quotient.mk_surjective @[to_additive (attr := simp)] theorem mk_freeMonoid [Nonempty α] : #(FreeMonoid α) = max #α ℵ₀ := Cardinal.mk_list_eq_max_mk_aleph0 _ @[to_additive (attr := simp)] theorem mk_freeGroup [Nonempty α] : #(FreeGroup α) = max #α ℵ₀ := by classical apply le_antisymm · apply (mk_le_of_injective (FreeGroup.toWord_injective (α := α))).trans_eq simp [Cardinal.mk_list_eq_max_mk_aleph0] obtain hα | hα := lt_or_ge #α ℵ₀ · simp only [hα.le, max_eq_right, max_eq_right_iff] exact (mul_lt_aleph0 hα (nat_lt_aleph0 2)).le · rw [max_eq_left hα, max_eq_left (hα.trans <| Cardinal.le_mul_right two_ne_zero), Cardinal.mul_eq_left hα _ (by simp)] exact (nat_lt_aleph0 2).le.trans hα · apply max_le · exact mk_le_of_injective FreeGroup.of_injective · simp @[simp] theorem mk_freeAbelianGroup [Nonempty α] : #(FreeAbelianGroup α) = max #α ℵ₀ := by rw [Cardinal.mk_congr (FreeAbelianGroup.equivFinsupp α).toEquiv] simp @[simp] theorem mk_freeRing : #(FreeRing α) = max #α ℵ₀ := by cases isEmpty_or_nonempty α <;> simp [FreeRing] @[simp] theorem mk_freeCommRing : #(FreeCommRing α) = max #α ℵ₀ := by cases isEmpty_or_nonempty α <;> simp [FreeCommRing] end Cardinal section Nonempty /-- A commutative ring can be constructed on any non-empty type. See also `Infinite.nonempty_field`. -/ instance nonempty_commRing [Nonempty α] : Nonempty (CommRing α) := by obtain hR | hR := finite_or_infinite α · obtain ⟨x⟩ := nonempty_fintype α have : NeZero (Fintype.card α) := ⟨by simp⟩ classical obtain ⟨e⟩ := Fintype.truncEquivFin α exact ⟨open scoped Fin.CommRing in e.commRing⟩ · have ⟨e⟩ : Nonempty (α ≃ FreeCommRing α) := by simp [← Cardinal.eq] exact ⟨e.commRing⟩ @[simp] theorem nonempty_commRing_iff : Nonempty (CommRing α) ↔ Nonempty α := ⟨Nonempty.map (·.zero), fun _ => nonempty_commRing _⟩ @[simp] theorem nonempty_ring_iff : Nonempty (Ring α) ↔ Nonempty α := ⟨Nonempty.map (·.zero), fun _ => (nonempty_commRing _).map (·.toRing)⟩ @[simp] theorem nonempty_commSemiring_iff : Nonempty (CommSemiring α) ↔ Nonempty α := ⟨Nonempty.map (·.zero), fun _ => (nonempty_commRing _).map (·.toCommSemiring)⟩ @[simp] theorem nonempty_semiring_iff : Nonempty (Semiring α) ↔ Nonempty α := ⟨Nonempty.map (·.zero), fun _ => (nonempty_commRing _).map (·.toSemiring)⟩ end Nonempty
.lake/packages/mathlib/Mathlib/SetTheory/Nimber/Basic.lean
import Mathlib.Data.Nat.Bitwise import Mathlib.SetTheory.Ordinal.Family import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Nimber.Basic` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Nimbers The goal of this file is to define the nimbers, constructed as ordinals endowed with new arithmetical operations. The nim sum `a + b` is recursively defined as the least ordinal not equal to any `a' + b` or `a + b'` for `a' < a` and `b' < b`. There is also a nim product, defined in the `Mathlib/SetTheory/Nimber/Field.lean` file. Nim addition arises within the context of impartial games. By the Sprague-Grundy theorem, each impartial game is equivalent to some game of nim. If `x ≈ nim o₁` and `y ≈ nim o₂`, then `x + y ≈ nim (o₁ + o₂)`, where the ordinals are summed together as nimbers. Unfortunately, the nim product admits no such characterization. ## Notation Following [On Numbers And Games][conway2001] (p. 121), we define notation `∗o` for the cast from `Ordinal` to `Nimber`. Note that for general `n : ℕ`, `∗n` is **not** the same as `↑n`. For instance, `∗2 ≠ 0`, whereas `↑2 = ↑1 + ↑1 = 0`. ## Implementation notes The nimbers inherit the order from the ordinals - this makes working with minimum excluded values much more convenient. However, the fact that nimbers are of characteristic 2 prevents the order from interacting with the arithmetic in any nice way. To reduce API duplication, we opt not to implement operations on `Nimber` on `Ordinal`. The order isomorphisms `Ordinal.toNimber` and `Nimber.toOrdinal` allow us to cast between them whenever needed. -/ universe u v open Function Order noncomputable section /-! ### Basic casts between `Ordinal` and `Nimber` -/ /-- A type synonym for ordinals with nimber addition and multiplication. -/ def Nimber : Type _ := Ordinal deriving Zero, Inhabited, One, Nontrivial, WellFoundedRelation instance Nimber.instLinearOrder : LinearOrder Nimber := Ordinal.instLinearOrder instance Nimber.instSuccOrder : SuccOrder Nimber := Ordinal.instSuccOrder instance Nimber.instOrderBot : OrderBot Nimber := Ordinal.instOrderBot instance Nimber.instNoMaxOrder : NoMaxOrder Nimber := Ordinal.instNoMaxOrder instance Nimber.instZeroLEOneClass : ZeroLEOneClass Nimber := Ordinal.instZeroLEOneClass instance Nimber.instNeZeroOne : NeZero (1 : Nimber) := Ordinal.instNeZeroOne /-- The identity function between `Ordinal` and `Nimber`. -/ @[match_pattern] def Ordinal.toNimber : Ordinal ≃o Nimber := OrderIso.refl _ /-- The identity function between `Nimber` and `Ordinal`. -/ @[match_pattern] def Nimber.toOrdinal : Nimber ≃o Ordinal := OrderIso.refl _ @[inherit_doc] scoped[Nimber] prefix:75 "∗" => Ordinal.toNimber namespace Nimber open Ordinal @[simp] theorem toOrdinal_symm_eq : Nimber.toOrdinal.symm = Ordinal.toNimber := rfl @[simp] theorem toOrdinal_toNimber (a : Nimber) : ∗(Nimber.toOrdinal a) = a := rfl theorem lt_wf : @WellFounded Nimber (· < ·) := Ordinal.lt_wf instance : WellFoundedLT Nimber := Ordinal.wellFoundedLT instance : ConditionallyCompleteLinearOrderBot Nimber := WellFoundedLT.conditionallyCompleteLinearOrderBot _ @[simp] theorem bot_eq_zero : ⊥ = 0 := rfl @[simp] theorem toOrdinal_zero : toOrdinal 0 = 0 := rfl @[simp] theorem toOrdinal_one : toOrdinal 1 = 1 := rfl @[simp] theorem toOrdinal_eq_zero {a} : toOrdinal a = 0 ↔ a = 0 := Iff.rfl @[simp] theorem toOrdinal_eq_one {a} : toOrdinal a = 1 ↔ a = 1 := Iff.rfl @[simp] theorem toOrdinal_max (a b : Nimber) : toOrdinal (max a b) = max (toOrdinal a) (toOrdinal b) := rfl @[simp] theorem toOrdinal_min (a b : Nimber) : toOrdinal (min a b) = min (toOrdinal a) (toOrdinal b) := rfl theorem succ_def (a : Nimber) : succ a = ∗(toOrdinal a + 1) := rfl /-- A recursor for `Nimber`. Use as `induction x`. -/ @[elab_as_elim, cases_eliminator, induction_eliminator] protected def rec {β : Nimber → Sort*} (h : ∀ a, β (∗a)) : ∀ a, β a := fun a => h (toOrdinal a) /-- `Ordinal.induction` but for `Nimber`. -/ theorem induction {p : Nimber → Prop} : ∀ (i) (_ : ∀ j, (∀ k, k < j → p k) → p j), p i := Ordinal.induction protected theorem le_zero {a : Nimber} : a ≤ 0 ↔ a = 0 := Ordinal.le_zero protected theorem not_lt_zero (a : Nimber) : ¬ a < 0 := Ordinal.not_lt_zero a protected theorem pos_iff_ne_zero {a : Nimber} : 0 < a ↔ a ≠ 0 := Ordinal.pos_iff_ne_zero theorem lt_one_iff_zero {a : Nimber} : a < 1 ↔ a = 0 := Ordinal.lt_one_iff_zero theorem one_le_iff_ne_zero {a : Nimber} : 1 ≤ a ↔ a ≠ 0 := Ordinal.one_le_iff_ne_zero theorem eq_nat_of_le_nat {a : Nimber} {b : ℕ} (h : a ≤ ∗b) : ∃ c : ℕ, a = ∗c := Ordinal.lt_omega0.1 (h.trans_lt (nat_lt_omega0 b)) instance small_Iio (a : Nimber.{u}) : Small.{u} (Set.Iio a) := Ordinal.small_Iio a instance small_Iic (a : Nimber.{u}) : Small.{u} (Set.Iic a) := Ordinal.small_Iic a instance small_Ico (a b : Nimber.{u}) : Small.{u} (Set.Ico a b) := Ordinal.small_Ico a b instance small_Icc (a b : Nimber.{u}) : Small.{u} (Set.Icc a b) := Ordinal.small_Icc a b instance small_Ioo (a b : Nimber.{u}) : Small.{u} (Set.Ioo a b) := Ordinal.small_Ioo a b instance small_Ioc (a b : Nimber.{u}) : Small.{u} (Set.Ioc a b) := Ordinal.small_Ioc a b theorem not_bddAbove_compl_of_small (s : Set Nimber.{u}) [Small.{u} s] : ¬ BddAbove sᶜ := Ordinal.not_bddAbove_compl_of_small s end Nimber theorem not_small_nimber : ¬ Small.{u} Nimber.{max u v} := not_small_ordinal instance Nimber.uncountable : Uncountable Nimber := Ordinal.uncountable open Nimber namespace Ordinal @[simp] theorem toNimber_symm_eq : toNimber.symm = Nimber.toOrdinal := rfl @[simp] theorem toNimber_toOrdinal (a : Ordinal) : Nimber.toOrdinal (∗a) = a := rfl @[simp] theorem toNimber_zero : ∗0 = 0 := rfl @[simp] theorem toNimber_one : ∗1 = 1 := rfl @[simp] theorem toNimber_eq_zero {a} : ∗a = 0 ↔ a = 0 := Iff.rfl @[simp] theorem toNimber_eq_one {a} : ∗a = 1 ↔ a = 1 := Iff.rfl @[simp] theorem toNimber_max (a b : Ordinal) : ∗(max a b) = max (∗a) (∗b) := rfl @[simp] theorem toNimber_min (a b : Ordinal) : ∗(min a b) = min (∗a) (∗b) := rfl end Ordinal /-! ### Nimber addition -/ namespace Nimber variable {a b c : Nimber.{u}} /-- Nimber addition is recursively defined so that `a + b` is the smallest nimber not equal to `a' + b` or `a + b'` for `a' < a` and `b' < b`. -/ -- We write the binders like this so that the termination checker works. protected def add (a b : Nimber.{u}) : Nimber.{u} := sInf {x | (∃ a', ∃ (_ : a' < a), Nimber.add a' b = x) ∨ ∃ b', ∃ (_ : b' < b), Nimber.add a b' = x}ᶜ termination_by (a, b) instance : Add Nimber := ⟨Nimber.add⟩ theorem add_def (a b : Nimber) : a + b = sInf {x | (∃ a' < a, a' + b = x) ∨ ∃ b' < b, a + b' = x}ᶜ := by change Nimber.add a b = _ rw [Nimber.add] simp_rw [exists_prop] rfl /-- The set in the definition of `Nimber.add` is nonempty. -/ private theorem add_nonempty (a b : Nimber.{u}) : {x | (∃ a' < a, a' + b = x) ∨ ∃ b' < b, a + b' = x}ᶜ.Nonempty := nonempty_of_not_bddAbove <| not_bddAbove_compl_of_small ((· + b) '' Set.Iio a ∪ (a + ·) '' Set.Iio b) theorem exists_of_lt_add (h : c < a + b) : (∃ a' < a, a' + b = c) ∨ ∃ b' < b, a + b' = c := by rw [add_def] at h have := notMem_of_lt_csInf' h rwa [Set.mem_compl_iff, not_not] at this theorem add_le_of_forall_ne (h₁ : ∀ a' < a, a' + b ≠ c) (h₂ : ∀ b' < b, a + b' ≠ c) : a + b ≤ c := by by_contra! h have := exists_of_lt_add h tauto private theorem add_ne_of_lt (a b : Nimber) : (∀ a' < a, a' + b ≠ a + b) ∧ ∀ b' < b, a + b' ≠ a + b := by have H := csInf_mem (add_nonempty a b) rw [← add_def] at H simpa using H instance : IsLeftCancelAdd Nimber := by constructor intro a b c h apply le_antisymm <;> apply le_of_not_gt · exact fun hc => (add_ne_of_lt a b).2 c hc h.symm · exact fun hb => (add_ne_of_lt a c).2 b hb h instance : IsRightCancelAdd Nimber := by constructor intro a b c h apply le_antisymm <;> apply le_of_not_gt · exact fun hc => (add_ne_of_lt b a).1 c hc h.symm · exact fun ha => (add_ne_of_lt c a).1 b ha h protected theorem add_comm (a b : Nimber) : a + b = b + a := by rw [add_def, add_def] simp_rw [or_comm] congr! 7 <;> (rw [and_congr_right_iff]; intro; rw [Nimber.add_comm]) termination_by (a, b) theorem add_eq_zero {a b : Nimber} : a + b = 0 ↔ a = b := by constructor <;> intro hab · obtain h | rfl | h := lt_trichotomy a b · have ha : a + a = 0 := add_eq_zero.2 rfl rwa [← ha, add_right_inj, eq_comm] at hab · rfl · have hb : b + b = 0 := add_eq_zero.2 rfl rwa [← hb, add_left_inj] at hab · rw [← Nimber.le_zero] apply add_le_of_forall_ne <;> simp_rw [ne_eq] <;> intro x hx · rw [add_eq_zero, ← hab] exact hx.ne · rw [add_eq_zero, hab] exact hx.ne' termination_by (a, b) theorem add_ne_zero_iff : a + b ≠ 0 ↔ a ≠ b := add_eq_zero.not @[simp] theorem add_self (a : Nimber) : a + a = 0 := add_eq_zero.2 rfl protected theorem add_assoc (a b c : Nimber) : a + b + c = a + (b + c) := by apply le_antisymm <;> apply add_le_of_forall_ne <;> intro x hx <;> try obtain ⟨y, hy, rfl⟩ | ⟨y, hy, rfl⟩ := exists_of_lt_add hx on_goal 1 => rw [Nimber.add_assoc y, add_ne_add_left] on_goal 2 => rw [Nimber.add_assoc _ y, add_ne_add_right, add_ne_add_left] on_goal 3 => rw [Nimber.add_assoc _ _ x, add_ne_add_right, add_ne_add_right] on_goal 4 => rw [← Nimber.add_assoc x, add_ne_add_left, add_ne_add_left] on_goal 5 => rw [← Nimber.add_assoc _ y, add_ne_add_left, add_ne_add_right] on_goal 6 => rw [← Nimber.add_assoc _ _ y, add_ne_add_right] all_goals apply ne_of_lt; assumption termination_by (a, b, c) protected theorem add_zero (a : Nimber) : a + 0 = a := by apply le_antisymm · apply add_le_of_forall_ne · intro a' ha rw [Nimber.add_zero] exact ha.ne · intro _ h exact (Nimber.not_lt_zero _ h).elim · by_contra! h replace h := h -- needed to remind `termination_by` have := Nimber.add_zero (a + 0) rw [add_left_inj] at this exact this.not_lt h termination_by a protected theorem zero_add (a : Nimber) : 0 + a = a := by rw [Nimber.add_comm, Nimber.add_zero] instance : Neg Nimber := ⟨id⟩ @[simp] protected theorem neg_eq (a : Nimber) : -a = a := rfl instance : AddCommGroupWithOne Nimber where add_assoc := Nimber.add_assoc add_zero := Nimber.add_zero zero_add := Nimber.zero_add nsmul := nsmulRec zsmul := zsmulRec neg_add_cancel := add_self add_comm := Nimber.add_comm @[simp] theorem add_cancel_right (a b : Nimber) : a + b + b = a := by rw [add_assoc, add_self, add_zero] @[simp] theorem add_cancel_left (a b : Nimber) : a + (a + b) = b := by rw [← add_assoc, add_self, zero_add] theorem add_trichotomy {a b c : Nimber} (h : a + b + c ≠ 0) : b + c < a ∨ c + a < b ∨ a + b < c := by rw [← Nimber.pos_iff_ne_zero] at h obtain ⟨x, hx, hx'⟩ | ⟨x, hx, hx'⟩ := exists_of_lt_add h <;> rw [add_eq_zero] at hx' · obtain ⟨x, hx, hx'⟩ | ⟨x, hx, hx'⟩ := exists_of_lt_add (hx' ▸ hx) · rw [← hx', add_comm, add_cancel_right] exact Or.inl hx · rw [← hx', add_comm a, add_cancel_right] exact Or.inr <| Or.inl hx · rw [← hx'] at hx exact Or.inr <| Or.inr hx theorem lt_add_cases {a b c : Nimber} (h : a < b + c) : a + c < b ∨ a + b < c := by obtain ha | hb | hc := add_trichotomy <| add_assoc a b c ▸ add_ne_zero_iff.2 h.ne exacts [(h.asymm ha).elim, Or.inl <| add_comm c a ▸ hb, Or.inr hc] /-- Nimber addition of naturals corresponds to the bitwise XOR operation. -/ theorem add_nat (a b : ℕ) : ∗a + ∗b = ∗(a ^^^ b) := by apply le_antisymm · apply add_le_of_forall_ne all_goals intro c hc obtain ⟨c, rfl⟩ := eq_nat_of_le_nat hc.le rw [OrderIso.lt_iff_lt] at hc replace hc := Nat.cast_lt.1 hc rw [add_nat] simpa using hc.ne · apply le_of_not_gt intro hc obtain ⟨c, hc'⟩ := eq_nat_of_le_nat hc.le rw [hc', OrderIso.lt_iff_lt, Nat.cast_lt] at hc obtain h | h := Nat.lt_xor_cases hc · apply h.ne simpa [Nat.xor_comm, Nat.xor_xor_cancel_left, ← hc'] using add_nat (c ^^^ b) b · apply h.ne simpa [Nat.xor_comm, Nat.xor_xor_cancel_left, ← hc'] using add_nat a (c ^^^ a) end Nimber
.lake/packages/mathlib/Mathlib/SetTheory/Nimber/Field.lean
import Mathlib.Algebra.CharP.Two import Mathlib.SetTheory.Nimber.Basic import Mathlib.Tactic.Abel import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Nimber.Field` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Nimber multiplication and division The nim product `a * b` is recursively defined as the least nimber not equal to any `a' * b + a * b' + a' * b'` for `a' < a` and `b' < b`. When endowed with this operation, the nimbers form a field. It's possible to show the existence of the nimber inverse implicitly via the simplicity theorem. Instead, we employ the explicit formula given in [On Numbers And Games][conway2001] (p. 56), which uses mutual induction and mimics the definition for the surreal inverse. This definition `invAux` "accidentally" gives the inverse of `0` as `1`, which the real inverse corrects. ## Todo - Show the nimbers are algebraically closed. -/ universe u v open Function Order noncomputable section namespace Nimber /-! ### Nimber multiplication -/ variable {a b c : Nimber.{u}} instance : CharP Nimber 2 := by apply CharTwo.of_one_ne_zero_of_two_eq_zero one_ne_zero rw [← one_add_one_eq_two, add_self] private theorem two_zsmul (x : Nimber) : (2 : ℤ) • x = 0 := by rw [_root_.two_zsmul] exact add_self x private theorem add_eq_iff_eq_add : a + b = c ↔ a = c + b := sub_eq_iff_eq_add /-- Nimber multiplication is recursively defined so that `a * b` is the smallest nimber not equal to `a' * b + a * b' + a' * b'` for `a' < a` and `b' < b`. -/ -- We write the binders like this so that the termination checker works. protected def mul (a b : Nimber.{u}) : Nimber.{u} := sInf {x | ∃ a', ∃ (_ : a' < a), ∃ b', ∃ (_ : b' < b), Nimber.mul a' b + Nimber.mul a b' + Nimber.mul a' b' = x}ᶜ termination_by (a, b) instance : Mul Nimber := ⟨Nimber.mul⟩ theorem mul_def (a b : Nimber) : a * b = sInf {x | ∃ a' < a, ∃ b' < b, a' * b + a * b' + a' * b' = x}ᶜ := by change Nimber.mul a b = _ rw [Nimber.mul] simp_rw [exists_prop] rfl /-- The set in the definition of `Nimber.mul` is nonempty. -/ private theorem mul_nonempty (a b : Nimber.{u}) : {x | ∃ a' < a, ∃ b' < b, a' * b + a * b' + a' * b' = x}ᶜ.Nonempty := by convert nonempty_of_not_bddAbove <| not_bddAbove_compl_of_small ((fun x ↦ x.1 * b + a * x.2 + x.1 * x.2) '' Set.Iio a ×ˢ Set.Iio b) ext simp_rw [Set.mem_setOf_eq, Set.mem_image, Set.mem_prod, Set.mem_Iio, Prod.exists] tauto theorem exists_of_lt_mul (h : c < a * b) : ∃ a' < a, ∃ b' < b, a' * b + a * b' + a' * b' = c := by rw [mul_def] at h have := notMem_of_lt_csInf' h rwa [Set.notMem_compl_iff] at this theorem mul_le_of_forall_ne (h : ∀ a' < a, ∀ b' < b, a' * b + a * b' + a' * b' ≠ c) : a * b ≤ c := by by_contra! h' have := exists_of_lt_mul h' tauto instance : MulZeroClass Nimber where mul_zero a := by rw [← Nimber.le_zero] exact mul_le_of_forall_ne fun _ _ _ h ↦ (Nimber.not_lt_zero _ h).elim zero_mul a := by rw [← Nimber.le_zero] exact mul_le_of_forall_ne fun _ h ↦ (Nimber.not_lt_zero _ h).elim private theorem mul_ne_of_lt : ∀ a' < a, ∀ b' < b, a' * b + a * b' + a' * b' ≠ a * b := by have H := csInf_mem (mul_nonempty a b) rw [← mul_def] at H simpa using H instance : NoZeroDivisors Nimber where eq_zero_or_eq_zero_of_mul_eq_zero {a b} h := by by_contra! hab iterate 2 rw [← Nimber.pos_iff_ne_zero] at hab apply (mul_ne_of_lt _ hab.1 _ hab.2).symm simpa only [zero_add, mul_zero, zero_mul] protected theorem mul_comm (a b : Nimber) : a * b = b * a := by apply le_antisymm <;> refine mul_le_of_forall_ne fun x hx y hy ↦ ?_ on_goal 1 => rw [add_comm (x * _), Nimber.mul_comm a, Nimber.mul_comm x, Nimber.mul_comm x] on_goal 2 => rw [add_comm (x * _), ← Nimber.mul_comm y, ← Nimber.mul_comm a, ← Nimber.mul_comm y] all_goals exact mul_ne_of_lt y hy x hx termination_by (a, b) protected theorem mul_add (a b c : Nimber) : a * (b + c) = a * b + a * c := by apply le_antisymm · refine mul_le_of_forall_ne fun a' ha x hx ↦ ?_ obtain (⟨b', h, rfl⟩ | ⟨c', h, rfl⟩) := exists_of_lt_add hx <;> rw [Nimber.mul_add a', Nimber.mul_add a, Nimber.mul_add a'] on_goal 1 => rw [← add_ne_add_left (a * c)] on_goal 2 => rw [← add_ne_add_left (a * b)] all_goals abel_nf simp only [two_zsmul, zero_add] rw [← add_assoc] exact mul_ne_of_lt _ ha _ h · apply add_le_of_forall_ne <;> (intro x' hx'; obtain ⟨x, hx, y, hy, rfl⟩ := exists_of_lt_mul hx') · obtain h | h | h := lt_trichotomy (y + c) (b + c) · have H := mul_ne_of_lt _ hx _ h rw [Nimber.mul_add x, Nimber.mul_add a, Nimber.mul_add x] at H abel_nf at H ⊢ simpa only [two_zsmul, zero_add] using H · exact (hy.ne <| add_left_injective _ h).elim · obtain ⟨z, hz, hz'⟩ | ⟨c', hc, hc'⟩ := exists_of_lt_add h · exact ((hz.trans hy).ne <| add_left_injective _ hz').elim · have := add_eq_iff_eq_add.1 hc' have H := mul_ne_of_lt _ hx _ hc rw [← hc', Nimber.mul_add a y c', ← add_ne_add_left (a * y), ← add_ne_add_left (a * c), ← add_ne_add_left (a * c'), ← add_eq_iff_eq_add.2 hc', Nimber.mul_add x, Nimber.mul_add x] abel_nf at H ⊢ simpa only [two_zsmul, add_zero, zero_add] using H · obtain h | h | h := lt_trichotomy (b + y) (b + c) · have H := mul_ne_of_lt _ hx _ h rw [Nimber.mul_add x, Nimber.mul_add a, Nimber.mul_add x] at H abel_nf at H ⊢ simpa only [two_zsmul, zero_add] using H · exact (hy.ne <| add_right_injective _ h).elim · obtain ⟨b', hb, hb'⟩ | ⟨z, hz, hz'⟩ := exists_of_lt_add h · have H := mul_ne_of_lt _ hx _ hb have hb'' := add_eq_iff_eq_add.2 (add_comm b c ▸ hb') rw [← hb', Nimber.mul_add a b', ← add_ne_add_left (a * y), ← add_ne_add_left (a * b), ← add_ne_add_left (a * b'), ← hb'', Nimber.mul_add x, Nimber.mul_add x] abel_nf at H ⊢ simpa only [two_zsmul, add_zero, zero_add] using H · exact ((hz.trans hy).ne <| add_right_injective _ hz').elim termination_by (a, b, c) protected theorem add_mul (a b c : Nimber) : (a + b) * c = a * c + b * c := by rw [Nimber.mul_comm, Nimber.mul_add, Nimber.mul_comm, Nimber.mul_comm b] private theorem add_ne_zero_of_lt {a b : Nimber} (h : b < a) : a + b ≠ 0 := by rw [add_ne_zero_iff] exact h.ne' protected theorem mul_assoc (a b c : Nimber) : a * b * c = a * (b * c) := by apply le_antisymm <;> refine mul_le_of_forall_ne fun x hx y hy ↦ ?_ · obtain ⟨a', ha, b', hb, rfl⟩ := exists_of_lt_mul hx have H : (a + a') * ((b + b') * (c + y)) ≠ 0 := by apply mul_ne_zero _ (mul_ne_zero _ _) <;> apply add_ne_zero_of_lt assumption' simp only [Nimber.add_mul, Nimber.mul_add] at H ⊢ iterate 7 rw [Nimber.mul_assoc] rw [← add_ne_add_left (a * (b * c))] abel_nf at H ⊢ simpa only [two_zsmul, zero_add] using H · obtain ⟨b', hb, c', hc, rfl⟩ := exists_of_lt_mul hy have H : (a + x) * (b + b') * (c + c') ≠ 0 := by apply mul_ne_zero (mul_ne_zero _ _) <;> apply add_ne_zero_of_lt assumption' simp only [Nimber.add_mul, Nimber.mul_add] at H ⊢ iterate 7 rw [← Nimber.mul_assoc] rw [← add_ne_add_left (a * b * c)] abel_nf at H ⊢ simpa only [two_zsmul, zero_add] using H termination_by (a, b, c) instance : IsCancelMulZero Nimber where mul_left_cancel_of_ne_zero ha _ _ h := by rw [← add_eq_zero, ← Nimber.mul_add, mul_eq_zero] at h exact add_eq_zero.1 (h.resolve_left ha) mul_right_cancel_of_ne_zero ha _ _ h := by rw [← add_eq_zero, ← Nimber.add_mul, mul_eq_zero] at h exact add_eq_zero.1 (h.resolve_right ha) protected theorem one_mul (a : Nimber) : 1 * a = a := by apply le_antisymm · refine mul_le_of_forall_ne fun x hx y hy ↦ ?_ rw [Nimber.lt_one_iff_zero] at hx rw [hx, Nimber.one_mul, zero_mul, zero_mul, add_zero, zero_add] exact hy.ne · by_contra! h replace h := h -- needed to remind `termination_by` exact (mul_left_cancel₀ one_ne_zero <| Nimber.one_mul _).not_lt h termination_by a protected theorem mul_one (a : Nimber) : a * 1 = a := by rw [Nimber.mul_comm, Nimber.one_mul] instance : CommRing Nimber where left_distrib := Nimber.mul_add right_distrib := Nimber.add_mul zero_mul := zero_mul mul_zero := mul_zero mul_assoc := Nimber.mul_assoc mul_comm := Nimber.mul_comm one_mul := Nimber.one_mul mul_one := Nimber.mul_one __ : AddCommGroupWithOne Nimber := inferInstance instance : IsDomain Nimber where instance : CancelMonoidWithZero Nimber where /-! ### Nimber division -/ mutual /-- The nimber inverse `a⁻¹` is mutually recursively defined as the smallest nimber not in the set `s = invSet a`, which itself is defined as the smallest set with `0 ∈ s` and `(1 + (a + a') * b) / a' ∈ s` for `0 < a' < a` and `b ∈ s`. This preliminary definition "accidentally" satisfies `invAux 0 = 1`, which the real inverse corrects. The lemma `inv_eq_invAux` can be used to transfer between the two. -/ def invAux (a : Nimber) : Nimber := sInf (invSet a)ᶜ termination_by (a, 1) /-- The set in the definition of `invAux a`. -/ def invSet (a : Nimber) : Set Nimber := ⋂₀ {s | 0 ∈ s ∧ ∀ a' < a, a' ≠ 0 → ∀ b ∈ s, invAux a' * (1 + (a + a') * b) ∈ s} termination_by (a, 0) end theorem zero_mem_invSet (a : Nimber) : 0 ∈ invSet a := by rw [invSet] exact Set.mem_sInter.2 fun _ hs => hs.1 /-- "cons" is our operation `(1 + (a + a') * b) / a'` in the definition of the inverse. -/ theorem cons_mem_invSet {a' : Nimber} (ha₀ : a' ≠ 0) (ha : a' < a) (hb : b ∈ invSet a) : invAux a' * (1 + (a + a') * b) ∈ invSet a := by rw [invSet] at hb ⊢ exact Set.mem_sInter.2 fun _ hs => hs.2 _ ha ha₀ _ (Set.mem_sInter.1 hb _ hs) /-- A recursion principle for `invSet`. -/ @[elab_as_elim] theorem invSet_recOn {p : Nimber → Prop} (a : Nimber) (h0 : p 0) (hi : ∀ a' < a, a' ≠ 0 → ∀ b, p b → p (invAux a' * (1 + (a + a') * b))) (x : Nimber) (hx : x ∈ invSet a) : p x := by revert x rw [invSet] exact Set.sInter_subset_of_mem ⟨h0, hi⟩ /-- An enumeration of elements in `invSet` by a type in the same universe. -/ private def List.toNimber {a : Nimber} : List a.toOrdinal.toType → Nimber | [] => 0 | x :: l => let a' := ∗((Ordinal.enumIsoToType a.toOrdinal).symm x) invAux a' * (1 + (a + a') * toNimber l) instance (a : Nimber.{u}) : Small.{u} (invSet a) := by refine @small_subset.{u, u + 1} _ _ _ ?_ (small_range (@List.toNimber a)) refine fun x hx ↦ invSet_recOn a ⟨[], rfl⟩ ?_ x hx rintro a' ha _ _ ⟨l, rfl⟩ use Ordinal.enumIsoToType _ ⟨toOrdinal a', ha⟩ :: l rw [List.toNimber] simp /-- The complement of `invSet a` is nonempty. -/ private theorem invSet_nonempty (a : Nimber) : (invSet a)ᶜ.Nonempty := have := instSmallElemInvSet a -- why is this needed? nonempty_of_not_bddAbove (Ordinal.not_bddAbove_compl_of_small _) theorem invAux_ne_zero (a : Nimber) : invAux a ≠ 0 := by rw [invAux] exact fun h ↦ h ▸ csInf_mem (invSet_nonempty a) <| zero_mem_invSet a theorem mem_invSet_of_lt_invAux (h : b < invAux a) : b ∈ invSet a := by rw [invAux] at h have := notMem_of_lt_csInf h ⟨_, bot_mem_lowerBounds _⟩ rwa [Set.notMem_compl_iff] at this theorem invAux_notMem_invSet (a : Nimber) : invAux a ∉ invSet a := by rw [invAux] exact csInf_mem (invSet_nonempty a) @[deprecated (since := "2025-05-23")] alias invAux_not_mem_invSet := invAux_notMem_invSet theorem invAux_mem_invSet_of_lt (ha : a ≠ 0) (hb : a < b) : invAux a ∈ invSet b := by have H := cons_mem_invSet ha hb (zero_mem_invSet b) rwa [mul_zero, add_zero, mul_one] at H /-- We set up a simultaneous induction to prove that `invAux a` is the inverse of `a`, and no element in its defining set `invSet a` is. -/ private theorem mul_inv_cancel_aux (a : Nimber) : (∀ b ∈ invSet a, a * b ≠ 1) ∧ (a ≠ 0 → a * invAux a = 1) := by have H₁ : ∀ b ∈ invSet a, a * b ≠ 1 := by refine fun b hb ↦ invSet_recOn a ?_ ?_ b hb · rw [mul_zero] exact zero_ne_one · intro a' ha ha' b hb rw [ne_eq, mul_comm, ← mul_right_inj' ha', ← mul_assoc, ← mul_assoc, (mul_inv_cancel_aux a').2 ha', one_mul, mul_one, add_mul, one_mul, ← add_left_inj a', add_self, mul_assoc, mul_comm b, add_assoc, add_comm _ a', ← add_assoc, ← mul_one_add, ← ne_eq, mul_ne_zero_iff, add_ne_zero_iff, add_ne_zero_iff] exact ⟨ha.ne', hb.symm⟩ refine ⟨H₁, fun ha₀ => le_antisymm ?_ ?_⟩ · apply mul_le_of_forall_ne fun a' ha b hb H ↦ ?_ replace hb := mem_invSet_of_lt_invAux hb rw [add_assoc, ← add_mul, ← CharTwo.eq_add_iff_add_eq] at H obtain rfl | ha' := eq_or_ne a' 0 · rw [zero_mul, add_zero, ← add_eq_iff_eq_add, zero_add] at H exact H₁ _ hb H · rw [← mul_right_inj' (invAux_ne_zero a'), ← mul_assoc, mul_comm _ a', (mul_inv_cancel_aux a').2 ha', one_mul] at H exact invAux_notMem_invSet a (H ▸ cons_mem_invSet ha' ha hb) · rw [one_le_iff_ne_zero, mul_ne_zero_iff] exact ⟨ha₀, invAux_ne_zero a⟩ termination_by a theorem mul_invAux_cancel (h : a ≠ 0) : a * invAux a = 1 := (mul_inv_cancel_aux a).2 h instance : Inv Nimber where inv a := if a = 0 then 0 else invAux a theorem inv_eq_invAux (ha : a ≠ 0) : a⁻¹ = invAux a := dif_neg ha instance : Field Nimber where mul_inv_cancel a ha := by rw [inv_eq_invAux ha, mul_invAux_cancel ha] inv_zero := dif_pos rfl nnqsmul := _ qsmul := _ end Nimber
.lake/packages/mathlib/Mathlib/SetTheory/Game/Short.lean
import Mathlib.Data.Fintype.Basic import Mathlib.SetTheory.Cardinal.Regular import Mathlib.SetTheory.Game.Birthday import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Short` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Short games A combinatorial game is `Short` [Conway, ch.9][conway2001] if it has only finitely many positions. In particular, this means there is a finite set of moves at every point. We prove that the order relations `≤` and `<`, and the equivalence relation `≈`, are decidable on short games, although unfortunately in practice `decide` doesn't seem to be able to prove anything using these instances. -/ -- Porting note: The local instances `moveLeftShort'` and `fintypeLeft` (and resp. `Right`) -- trigger this error. set_option synthInstance.checkSynthOrder false universe u namespace SetTheory open scoped PGame namespace PGame /-- A short game is a game with a finite set of moves at every turn. -/ inductive Short : PGame.{u} → Type (u + 1) | mk : ∀ {α β : Type u} {L : α → PGame.{u}} {R : β → PGame.{u}} (_ : ∀ i : α, Short (L i)) (_ : ∀ j : β, Short (R j)) [Fintype α] [Fintype β], Short ⟨α, β, L, R⟩ instance subsingleton_short (x : PGame) : Subsingleton (Short x) := by induction x with | mk xl xr xL xR => constructor intro a b cases a; cases b congr! -- Porting note: We use `induction` to prove `subsingleton_short` instead of recursion. -- A proof using recursion generates a harder `decreasing_by` goal than in Lean 3 for some reason: attribute [-instance] subsingleton_short in theorem subsingleton_short_example : ∀ x : PGame, Subsingleton (Short x) | mk xl xr xL xR => ⟨fun a b => by cases a; cases b congr! · funext x apply @Subsingleton.elim _ (subsingleton_short_example (xL x)) -- Decreasing goal in Lean 4 is `Subsequent (xL x) (mk α β L R)` -- where `α`, `β`, `L`, and `R` are fresh hypotheses only propositionally -- equal to `xl`, `xr`, `xL`, and `xR`. -- (In Lean 3 it was `(mk xl xr xL xR)` instead.) · funext x apply @Subsingleton.elim _ (subsingleton_short_example (xR x))⟩ termination_by x => x -- We need to unify a bunch of hypotheses before `pgame_wf_tac` can work. decreasing_by all_goals { subst_vars simp only [mk.injEq, heq_eq_eq, true_and] at * casesm* _ ∧ _ subst_vars pgame_wf_tac } /-- A synonym for `Short.mk` that specifies the pgame in an implicit argument. -/ def Short.mk' {x : PGame} [Fintype x.LeftMoves] [Fintype x.RightMoves] (sL : ∀ i : x.LeftMoves, Short (x.moveLeft i)) (sR : ∀ j : x.RightMoves, Short (x.moveRight j)) : Short x := by -- Porting note: Old proof relied on `unfreezingI`, which doesn't exist in Lean 4. convert Short.mk sL sR cases x dsimp attribute [class] Short /-- Extracting the `Fintype` instance for the indexing type for Left's moves in a short game. This is an unindexed typeclass, so it can't be made a global instance. -/ def fintypeLeft {α β : Type u} {L : α → PGame.{u}} {R : β → PGame.{u}} [S : Short ⟨α, β, L, R⟩] : Fintype α := by cases S; assumption attribute [local instance] fintypeLeft instance fintypeLeftMoves (x : PGame) [S : Short x] : Fintype x.LeftMoves := by cases S; assumption /-- Extracting the `Fintype` instance for the indexing type for Right's moves in a short game. This is an unindexed typeclass, so it can't be made a global instance. -/ def fintypeRight {α β : Type u} {L : α → PGame.{u}} {R : β → PGame.{u}} [S : Short ⟨α, β, L, R⟩] : Fintype β := by cases S; assumption attribute [local instance] fintypeRight instance fintypeRightMoves (x : PGame) [S : Short x] : Fintype x.RightMoves := by cases S; assumption instance moveLeftShort (x : PGame) [S : Short x] (i : x.LeftMoves) : Short (x.moveLeft i) := by obtain ⟨L, _⟩ := S; apply L /-- Extracting the `Short` instance for a move by Left. This would be a dangerous instance potentially introducing new metavariables in typeclass search, so we only make it an instance locally. -/ def moveLeftShort' {xl xr} (xL xR) [S : Short (mk xl xr xL xR)] (i : xl) : Short (xL i) := by obtain ⟨L, _⟩ := S; apply L attribute [local instance] moveLeftShort' instance moveRightShort (x : PGame) [S : Short x] (j : x.RightMoves) : Short (x.moveRight j) := by obtain ⟨_, R⟩ := S; apply R /-- Extracting the `Short` instance for a move by Right. This would be a dangerous instance potentially introducing new metavariables in typeclass search, so we only make it an instance locally. -/ def moveRightShort' {xl xr} (xL xR) [S : Short (mk xl xr xL xR)] (j : xr) : Short (xR j) := by obtain ⟨_, R⟩ := S; apply R attribute [local instance] moveRightShort' theorem short_birthday (x : PGame.{u}) : [Short x] → x.birthday < Ordinal.omega0 := by -- Porting note: Again `induction` is used instead of `pgame_wf_tac` induction x with | mk xl xr xL xR ihl ihr => intro hs rcases hs with ⟨sL, sR⟩ rw [birthday, max_lt_iff] constructor all_goals rw [← Cardinal.ord_aleph0] refine Cardinal.lsub_lt_ord_of_isRegular.{u, u} Cardinal.isRegular_aleph0 (Cardinal.lt_aleph0_of_finite _) fun i => ?_ rw [Cardinal.ord_aleph0] · apply ihl · apply ihr /-- This leads to infinite loops if made into an instance. -/ def Short.ofIsEmpty {l r xL xR} [IsEmpty l] [IsEmpty r] : Short (PGame.mk l r xL xR) := by have : Fintype l := Fintype.ofIsEmpty have : Fintype r := Fintype.ofIsEmpty exact Short.mk isEmptyElim isEmptyElim instance short0 : Short 0 := Short.ofIsEmpty instance short1 : Short 1 := Short.mk (fun i => by cases i; infer_instance) fun j => by cases j /-- Evidence that every `PGame` in a list is `Short`. -/ class inductive ListShort : List PGame.{u} → Type (u + 1) | nil : ListShort [] -- Porting note: We introduce `cons` as a separate instance because attempting to use -- `[ListShort tl]` as a constructor argument errors saying that `ListShort tl` is not a class. -- Is this a bug in `class inductive`? | cons' {hd : PGame.{u}} {tl : List PGame.{u}} : Short hd → ListShort tl → ListShort (hd::tl) attribute [instance] ListShort.nil instance ListShort.cons (hd : PGame.{u}) [short_hd : Short hd] (tl : List PGame.{u}) [short_tl : ListShort tl] : ListShort (hd::tl) := cons' short_hd short_tl instance listShortGet : ∀ (L : List PGame.{u}) [ListShort L] (i : Nat) (h : i < List.length L), Short L[i] | _::_, ListShort.cons' S _, 0, _ => S | _::tl, ListShort.cons' _ S, n + 1, h => @listShortGet tl S n ((add_lt_add_iff_right 1).mp h) instance shortOfLists : ∀ (L R : List PGame) [ListShort L] [ListShort R], Short (PGame.ofLists L R) | L, R, _, _ => by exact Short.mk (fun i ↦ inferInstance) fun j ↦ listShortGet R (↑j.down) (Fin.is_lt j.down) /-- If `x` is a short game, and `y` is a relabelling of `x`, then `y` is also short. -/ def shortOfRelabelling : ∀ {x y : PGame.{u}}, Relabelling x y → Short x → Short y | x, y, ⟨L, R, rL, rR⟩, S => by haveI := Fintype.ofEquiv _ L haveI := Fintype.ofEquiv _ R exact Short.mk' (fun i => by rw [← L.right_inv i]; apply shortOfRelabelling (rL (L.symm i)) inferInstance) fun j => by simpa using shortOfRelabelling (rR (R.symm j)) inferInstance instance shortNeg : ∀ (x : PGame.{u}) [Short x], Short (-x) | mk xl xr xL xR, _ => by exact Short.mk (fun i => shortNeg _) fun i => shortNeg _ instance shortAdd : ∀ (x y : PGame.{u}) [Short x] [Short y], Short (x + y) | mk xl xr xL xR, mk yl yr yL yR, _, _ => by apply Short.mk all_goals rintro ⟨i⟩ · apply shortAdd · change Short (mk xl xr xL xR + _); apply shortAdd termination_by x y => (x, y) instance shortNat : ∀ n : ℕ, Short n | 0 => PGame.short0 | n + 1 => @PGame.shortAdd _ _ (shortNat n) PGame.short1 instance shortOfNat (n : ℕ) [Nat.AtLeastTwo n] : Short ofNat(n) := shortNat n instance shortBit0 (x : PGame.{u}) [Short x] : Short (x + x) := by infer_instance instance shortBit1 (x : PGame.{u}) [Short x] : Short ((x + x) + 1) := shortAdd _ _ /-- Auxiliary construction of decidability instances. We build `Decidable (x ≤ y)` and `Decidable (x ⧏ y)` in a simultaneous induction. Instances for the two projections separately are provided below. -/ def leLFDecidable : ∀ (x y : PGame.{u}) [Short x] [Short y], Decidable (x ≤ y) × Decidable (x ⧏ y) | mk xl xr xL xR, mk yl yr yL yR, shortx, shorty => by constructor · refine @decidable_of_iff' _ _ mk_le_mk (id ?_) have : Decidable (∀ (i : xl), xL i ⧏ mk yl yr yL yR) := by apply @Fintype.decidableForallFintype xl _ ?_ _ intro i apply (leLFDecidable _ _).2 have : Decidable (∀ (j : yr), mk xl xr xL xR ⧏ yR j) := by apply @Fintype.decidableForallFintype yr _ ?_ _ intro i apply (leLFDecidable _ _).2 exact inferInstanceAs (Decidable (_ ∧ _)) · refine @decidable_of_iff' _ _ mk_lf_mk (id ?_) have : Decidable (∃ i, mk xl xr xL xR ≤ yL i) := by apply @Fintype.decidableExistsFintype yl _ ?_ _ intro i apply (leLFDecidable _ _).1 have : Decidable (∃ j, xR j ≤ mk yl yr yL yR) := by apply @Fintype.decidableExistsFintype xr _ ?_ _ intro i apply (leLFDecidable _ _).1 exact inferInstanceAs (Decidable (_ ∨ _)) termination_by x y => (x, y) instance leDecidable (x y : PGame.{u}) [Short x] [Short y] : Decidable (x ≤ y) := (leLFDecidable x y).1 instance lfDecidable (x y : PGame.{u}) [Short x] [Short y] : Decidable (x ⧏ y) := (leLFDecidable x y).2 instance ltDecidable (x y : PGame.{u}) [Short x] [Short y] : Decidable (x < y) := inferInstanceAs (Decidable (_ ∧ _)) instance equivDecidable (x y : PGame.{u}) [Short x] [Short y] : Decidable (x ≈ y) := inferInstanceAs (Decidable (_ ∧ _)) example : Short 0 := by infer_instance example : Short 1 := by infer_instance example : Short 2 := by infer_instance example : Short (-2) := by infer_instance example : Short (ofLists [0] [1]) := by infer_instance example : Short (ofLists [-2, -1] [1]) := by infer_instance example : Short (0 + 0) := by infer_instance example : Decidable ((1 : PGame) ≤ 1) := by infer_instance -- No longer works since definitional reduction of well-founded definitions has been restricted. -- example : (0 : PGame.{u}) ≤ 0 := by decide -- example : (1 : PGame.{u}) ≤ 1 := by decide end PGame end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Impartial.lean
import Mathlib.SetTheory.Game.Basic import Mathlib.Tactic.NthRewrite import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Impartial.Basic` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Basic definitions about impartial (pre-)games We will define an impartial game, one in which left and right can make exactly the same moves. Our definition differs slightly by saying that the game is always equivalent to its negative, no matter what moves are played. This allows for games such as poker-nim to be classified as impartial. -/ universe u namespace SetTheory open scoped PGame namespace PGame private def ImpartialAux (G : PGame) : Prop := (G ≈ -G) ∧ (∀ i, ImpartialAux (G.moveLeft i)) ∧ ∀ j, ImpartialAux (G.moveRight j) termination_by G /-- An impartial game is one that's equivalent to its negative, such that each left and right move is also impartial. Note that this is a slightly more general definition than the one that's usually in the literature, as we don't require `G ≡ -G`. Despite this, the Sprague-Grundy theorem still holds: see `SetTheory.PGame.equiv_nim_grundyValue`. In such a game, both players have the same payoffs at any subposition. -/ class Impartial (G : PGame) : Prop where out : ImpartialAux G private theorem impartial_iff_aux {G : PGame} : G.Impartial ↔ G.ImpartialAux := ⟨fun h => h.1, fun h => ⟨h⟩⟩ theorem impartial_def {G : PGame} : G.Impartial ↔ G ≈ -G ∧ (∀ i, Impartial (G.moveLeft i)) ∧ ∀ j, Impartial (G.moveRight j) := by simp_rw [impartial_iff_aux] rw [ImpartialAux] namespace Impartial instance impartial_zero : Impartial 0 := by rw [impartial_def] simp instance impartial_star : Impartial star := by rw [impartial_def] simpa using Impartial.impartial_zero theorem neg_equiv_self (G : PGame) [h : G.Impartial] : G ≈ -G := (impartial_def.1 h).1 @[simp] theorem mk'_neg_equiv_self (G : PGame) [G.Impartial] : -(⟦G⟧ : Game) = ⟦G⟧ := game_eq (Equiv.symm (neg_equiv_self G)) instance moveLeft_impartial {G : PGame} [h : G.Impartial] (i : G.LeftMoves) : (G.moveLeft i).Impartial := (impartial_def.1 h).2.1 i instance moveRight_impartial {G : PGame} [h : G.Impartial] (j : G.RightMoves) : (G.moveRight j).Impartial := (impartial_def.1 h).2.2 j theorem impartial_congr {G H : PGame} (e : G ≡r H) [G.Impartial] : H.Impartial := impartial_def.2 ⟨Equiv.trans e.symm.equiv (Equiv.trans (neg_equiv_self G) (neg_equiv_neg_iff.2 e.equiv)), fun i => impartial_congr (e.moveLeftSymm i), fun j => impartial_congr (e.moveRightSymm j)⟩ termination_by G instance impartial_add (G H : PGame) [G.Impartial] [H.Impartial] : (G + H).Impartial := by rw [impartial_def] refine ⟨Equiv.trans (add_congr (neg_equiv_self G) (neg_equiv_self _)) (Equiv.symm (negAddRelabelling _ _).equiv), fun k => ?_, fun k => ?_⟩ · apply leftMoves_add_cases k all_goals intro i; simp only [add_moveLeft_inl, add_moveLeft_inr] apply impartial_add · apply rightMoves_add_cases k all_goals intro i; simp only [add_moveRight_inl, add_moveRight_inr] apply impartial_add termination_by (G, H) instance impartial_neg (G : PGame) [G.Impartial] : (-G).Impartial := by rw [impartial_def] refine ⟨?_, fun i => ?_, fun i => ?_⟩ · rw [neg_neg] exact Equiv.symm (neg_equiv_self G) · rw [moveLeft_neg] exact impartial_neg _ · rw [moveRight_neg] exact impartial_neg _ termination_by G variable (G : PGame) [Impartial G] theorem nonpos : ¬0 < G := by apply (lt_asymm · ?_) rwa [← neg_lt_neg_iff, neg_zero, ← lt_congr_right (neg_equiv_self G)] theorem nonneg : ¬G < 0 := by simpa using nonpos (-G) /-- In an impartial game, either the first player always wins, or the second player always wins. -/ theorem equiv_or_fuzzy_zero : G ≈ 0 ∨ G ‖ 0 := by rcases lt_or_equiv_or_gt_or_fuzzy G 0 with (h | h | h | h) · exact ((nonneg G) h).elim · exact Or.inl h · exact ((nonpos G) h).elim · exact Or.inr h @[simp] theorem not_equiv_zero_iff : ¬ G ≈ 0 ↔ G ‖ 0 := ⟨(equiv_or_fuzzy_zero G).resolve_left, Fuzzy.not_equiv⟩ @[simp] theorem not_fuzzy_zero_iff : ¬ G ‖ 0 ↔ G ≈ 0 := ⟨(equiv_or_fuzzy_zero G).resolve_right, Equiv.not_fuzzy⟩ theorem add_self : G + G ≈ 0 := Equiv.trans (add_congr_left (neg_equiv_self G)) (neg_add_cancel_equiv G) @[simp] theorem mk'_add_self : (⟦G⟧ : Game) + ⟦G⟧ = 0 := game_eq (add_self G) /-- This lemma doesn't require `H` to be impartial. -/ theorem equiv_iff_add_equiv_zero (H : PGame) : H ≈ G ↔ H + G ≈ 0 := by rw [equiv_iff_game_eq, ← add_right_cancel_iff (a := ⟦G⟧), mk'_add_self, ← quot_add, equiv_iff_game_eq, quot_zero] /-- This lemma doesn't require `H` to be impartial. -/ theorem equiv_iff_add_equiv_zero' (H : PGame) : G ≈ H ↔ G + H ≈ 0 := by rw [equiv_iff_game_eq, ← add_left_cancel_iff, mk'_add_self, ← quot_add, equiv_iff_game_eq, Eq.comm, quot_zero] theorem le_zero_iff {G : PGame} [G.Impartial] : G ≤ 0 ↔ 0 ≤ G := by rw [← zero_le_neg_iff, le_congr_right (neg_equiv_self G)] theorem lf_zero_iff {G : PGame} [G.Impartial] : G ⧏ 0 ↔ 0 ⧏ G := by rw [← zero_lf_neg_iff, lf_congr_right (neg_equiv_self G)] theorem equiv_zero_iff_le : (G ≈ 0) ↔ G ≤ 0 := ⟨And.left, fun h => ⟨h, le_zero_iff.1 h⟩⟩ theorem fuzzy_zero_iff_lf : G ‖ 0 ↔ G ⧏ 0 := ⟨And.left, fun h => ⟨h, lf_zero_iff.1 h⟩⟩ theorem equiv_zero_iff_ge : (G ≈ 0) ↔ 0 ≤ G := ⟨And.right, fun h => ⟨le_zero_iff.2 h, h⟩⟩ theorem fuzzy_zero_iff_gf : G ‖ 0 ↔ 0 ⧏ G := ⟨And.right, fun h => ⟨lf_zero_iff.2 h, h⟩⟩ theorem forall_leftMoves_fuzzy_iff_equiv_zero : (∀ i, G.moveLeft i ‖ 0) ↔ G ≈ 0 := by refine ⟨fun hb => ?_, fun hp i => ?_⟩ · rw [equiv_zero_iff_le G, le_zero_lf] exact fun i => (hb i).1 · rw [fuzzy_zero_iff_lf] exact hp.1.moveLeft_lf i theorem forall_rightMoves_fuzzy_iff_equiv_zero : (∀ j, G.moveRight j ‖ 0) ↔ G ≈ 0 := by refine ⟨fun hb => ?_, fun hp i => ?_⟩ · rw [equiv_zero_iff_ge G, zero_le_lf] exact fun i => (hb i).2 · rw [fuzzy_zero_iff_gf] exact hp.2.lf_moveRight i theorem exists_left_move_equiv_iff_fuzzy_zero : (∃ i, G.moveLeft i ≈ 0) ↔ G ‖ 0 := by refine ⟨fun ⟨i, hi⟩ => (fuzzy_zero_iff_gf G).2 (lf_of_le_moveLeft hi.2), fun hn => ?_⟩ rw [fuzzy_zero_iff_gf G, zero_lf_le] at hn obtain ⟨i, hi⟩ := hn exact ⟨i, (equiv_zero_iff_ge _).2 hi⟩ theorem exists_right_move_equiv_iff_fuzzy_zero : (∃ j, G.moveRight j ≈ 0) ↔ G ‖ 0 := by refine ⟨fun ⟨i, hi⟩ => (fuzzy_zero_iff_lf G).2 (lf_of_moveRight_le hi.1), fun hn => ?_⟩ rw [fuzzy_zero_iff_lf G, lf_zero_le] at hn obtain ⟨i, hi⟩ := hn exact ⟨i, (equiv_zero_iff_le _).2 hi⟩ end Impartial end PGame end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Domineering.lean
import Mathlib.SetTheory.Game.State import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Specific.Domineering` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Domineering as a combinatorial game. We define the game of Domineering, played on a chessboard of arbitrary shape (possibly even disconnected). Left moves by placing a domino vertically, while Right moves by placing a domino horizontally. This is only a fragment of a full development; in order to successfully analyse positions we would need some more theorems. Most importantly, we need a general statement that allows us to discard irrelevant moves. Specifically to domineering, we need the fact that disjoint parts of the chessboard give sums of games. -/ namespace SetTheory namespace PGame namespace Domineering open Function /-- The equivalence `(x, y) ↦ (x, y+1)`. -/ @[simps!] def shiftUp : ℤ × ℤ ≃ ℤ × ℤ := (Equiv.refl ℤ).prodCongr (Equiv.addRight (1 : ℤ)) /-- The equivalence `(x, y) ↦ (x+1, y)`. -/ @[simps!] def shiftRight : ℤ × ℤ ≃ ℤ × ℤ := (Equiv.addRight (1 : ℤ)).prodCongr (Equiv.refl ℤ) /-- A Domineering board is an arbitrary finite subset of `ℤ × ℤ`. -/ -- Porting note: reducibility cannot be `local`. For now there are no dependents of this file so -- being globally reducible is fine. abbrev Board := Finset (ℤ × ℤ) /-- Left can play anywhere that a square and the square below it are open. -/ def left (b : Board) : Finset (ℤ × ℤ) := b ∩ b.map shiftUp /-- Right can play anywhere that a square and the square to the left are open. -/ def right (b : Board) : Finset (ℤ × ℤ) := b ∩ b.map shiftRight theorem mem_left {b : Board} (x : ℤ × ℤ) : x ∈ left b ↔ x ∈ b ∧ (x.1, x.2 - 1) ∈ b := Finset.mem_inter.trans (and_congr Iff.rfl Finset.mem_map_equiv) theorem mem_right {b : Board} (x : ℤ × ℤ) : x ∈ right b ↔ x ∈ b ∧ (x.1 - 1, x.2) ∈ b := Finset.mem_inter.trans (and_congr Iff.rfl Finset.mem_map_equiv) /-- After Left moves, two vertically adjacent squares are removed from the board. -/ def moveLeft (b : Board) (m : ℤ × ℤ) : Board := (b.erase m).erase (m.1, m.2 - 1) /-- After Left moves, two horizontally adjacent squares are removed from the board. -/ def moveRight (b : Board) (m : ℤ × ℤ) : Board := (b.erase m).erase (m.1 - 1, m.2) theorem fst_pred_mem_erase_of_mem_right {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : (m.1 - 1, m.2) ∈ b.erase m := by rw [mem_right] at h apply Finset.mem_erase_of_ne_of_mem _ h.2 exact ne_of_apply_ne Prod.fst (pred_ne_self m.1) theorem snd_pred_mem_erase_of_mem_left {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : (m.1, m.2 - 1) ∈ b.erase m := by rw [mem_left] at h apply Finset.mem_erase_of_ne_of_mem _ h.2 exact ne_of_apply_ne Prod.snd (pred_ne_self m.2) theorem card_of_mem_left {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : 2 ≤ Finset.card b := by have w₁ : m ∈ b := (Finset.mem_inter.1 h).1 have w₂ : (m.1, m.2 - 1) ∈ b.erase m := snd_pred_mem_erase_of_mem_left h have i₁ := Finset.card_erase_lt_of_mem w₁ have i₂ := Nat.lt_of_le_of_lt (Nat.zero_le _) (Finset.card_erase_lt_of_mem w₂) exact Nat.lt_of_le_of_lt i₂ i₁ theorem card_of_mem_right {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : 2 ≤ Finset.card b := by have w₁ : m ∈ b := (Finset.mem_inter.1 h).1 have w₂ := fst_pred_mem_erase_of_mem_right h have i₁ := Finset.card_erase_lt_of_mem w₁ have i₂ := Nat.lt_of_le_of_lt (Nat.zero_le _) (Finset.card_erase_lt_of_mem w₂) exact Nat.lt_of_le_of_lt i₂ i₁ theorem moveLeft_card {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : Finset.card (moveLeft b m) + 2 = Finset.card b := by dsimp only [moveLeft] rw [Finset.card_erase_of_mem (snd_pred_mem_erase_of_mem_left h)] rw [Finset.card_erase_of_mem (Finset.mem_of_mem_inter_left h)] exact tsub_add_cancel_of_le (card_of_mem_left h) theorem moveRight_card {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : Finset.card (moveRight b m) + 2 = Finset.card b := by dsimp only [moveRight] rw [Finset.card_erase_of_mem (fst_pred_mem_erase_of_mem_right h)] rw [Finset.card_erase_of_mem (Finset.mem_of_mem_inter_left h)] exact tsub_add_cancel_of_le (card_of_mem_right h) theorem moveLeft_smaller {b : Board} {m : ℤ × ℤ} (h : m ∈ left b) : Finset.card (moveLeft b m) / 2 < Finset.card b / 2 := by simp [← moveLeft_card h] theorem moveRight_smaller {b : Board} {m : ℤ × ℤ} (h : m ∈ right b) : Finset.card (moveRight b m) / 2 < Finset.card b / 2 := by simp [← moveRight_card h] /-- The instance describing allowed moves on a Domineering board. -/ instance state : State Board where turnBound s := s.card / 2 l s := (left s).image (moveLeft s) r s := (right s).image (moveRight s) left_bound m := by simp only [Finset.mem_image, Prod.exists] at m rcases m with ⟨_, _, ⟨h, rfl⟩⟩ exact moveLeft_smaller h right_bound m := by simp only [Finset.mem_image, Prod.exists] at m rcases m with ⟨_, _, ⟨h, rfl⟩⟩ exact moveRight_smaller h end Domineering /-- Construct a pre-game from a Domineering board. -/ def domineering (b : Domineering.Board) : PGame := PGame.ofState b /-- All games of Domineering are short, because each move removes two squares. -/ instance shortDomineering (b : Domineering.Board) : Short (domineering b) := by dsimp only [domineering] infer_instance /-- The Domineering board with two squares arranged vertically, in which Left has the only move. -/ def domineering.one := domineering [(0, 0), (0, 1)].toFinset /-- The `L` shaped Domineering board, in which Left is exactly half a move ahead. -/ def domineering.L := domineering [(0, 2), (0, 1), (0, 0), (1, 0)].toFinset instance shortOne : Short domineering.one := by dsimp [domineering.one]; infer_instance instance shortL : Short domineering.L := by dsimp [domineering.L]; infer_instance -- The VM can play small games successfully: -- #eval decide (domineering.one ≈ 1) -- #eval decide (domineering.L + domineering.L ≈ 1) -- The following no longer works since Lean 3.29, since definitions by well-founded -- recursion no longer reduce definitionally. -- We can check that `Decidable` instances reduce as expected, -- and so our implementation of domineering is computable. -- run_cmd tactic.whnf `(by apply_instance : Decidable (domineering.one ≤ 1)) >>= tactic.trace -- dec_trivial can handle most of the dictionary of small games described in [conway2001] -- example : domineering.one ≈ 1 := by decide -- example : domineering.L + domineering.L ≈ 1 := by decide -- example : domineering.L ≈ PGame.ofLists [0] [1] := by decide -- example : (domineering ([(0,0), (0,1), (0,2), (0,3)].toFinset) ≈ 2) := by decide -- example : (domineering ([(0,0), (0,1), (1,0), (1,1)].toFinset) ≈ PGame.ofLists [1] [-1]) := -- by decide -- The 3x3 grid is doable, but takes a minute... -- example : -- (domineering ([(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)].toFinset) ≈ -- PGame.ofLists [1] [-1]) := by decide -- The 5x5 grid is actually 0, but brute-forcing this is too challenging even for the VM. -- #eval decide (domineering ([ -- (0,0), (0,1), (0,2), (0,3), (0,4), -- (1,0), (1,1), (1,2), (1,3), (1,4), -- (2,0), (2,1), (2,2), (2,3), (2,4), -- (3,0), (3,1), (3,2), (3,3), (3,4), -- (4,0), (4,1), (4,2), (4,3), (4,4) -- ].toFinset) ≈ 0) end PGame end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Basic.lean
import Mathlib.Algebra.Order.Monoid.Defs import Mathlib.SetTheory.PGame.Algebra import Mathlib.Tactic.Abel import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Basic` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Combinatorial games. In this file we construct an instance `OrderedAddCommGroup SetTheory.Game`. ## Multiplication on pre-games We define the operations of multiplication and inverse on pre-games, and prove a few basic theorems about them. Multiplication is not well-behaved under equivalence of pre-games i.e. `x ≈ y` does not imply `x * z ≈ y * z`. Hence, multiplication is not a well-defined operation on games. Nevertheless, the abelian group structure on games allows us to simplify many proofs for pre-games. -/ -- Porting note: many definitions here are noncomputable as the compiler does not support PGame.rec noncomputable section namespace SetTheory open Function PGame universe u -- Porting note: moved the setoid instance to PGame.lean /-- The type of combinatorial games. In ZFC, a combinatorial game is constructed from two sets of combinatorial games that have been constructed at an earlier stage. To do this in type theory, we say that a combinatorial pre-game is built inductively from two families of combinatorial games indexed over any type in Type u. The resulting type `PGame.{u}` lives in `Type (u+1)`, reflecting that it is a proper class in ZFC. A combinatorial game is then constructed by quotienting by the equivalence `x ≈ y ↔ x ≤ y ∧ y ≤ x`. -/ abbrev Game := Quotient PGame.setoid namespace Game -- Porting note (https://github.com/leanprover-community/mathlib4/issues/11445): added this definition /-- Negation of games. -/ instance : Neg Game where neg := Quot.map Neg.neg <| fun _ _ => (neg_equiv_neg_iff).2 instance : Zero Game where zero := ⟦0⟧ instance : Add Game where add := Quotient.map₂ HAdd.hAdd <| fun _ _ hx _ _ hy => PGame.add_congr hx hy instance instAddCommGroupWithOneGame : AddCommGroupWithOne Game where zero := ⟦0⟧ one := ⟦1⟧ add_zero := by rintro ⟨x⟩ exact Quot.sound (add_zero_equiv x) zero_add := by rintro ⟨x⟩ exact Quot.sound (zero_add_equiv x) add_assoc := by rintro ⟨x⟩ ⟨y⟩ ⟨z⟩ exact Quot.sound add_assoc_equiv neg_add_cancel := Quotient.ind <| fun x => Quot.sound (neg_add_cancel_equiv x) add_comm := by rintro ⟨x⟩ ⟨y⟩ exact Quot.sound add_comm_equiv nsmul := nsmulRec zsmul := zsmulRec instance : Inhabited Game := ⟨0⟩ theorem zero_def : (0 : Game) = ⟦0⟧ := rfl instance instPartialOrderGame : PartialOrder Game where le := Quotient.lift₂ (· ≤ ·) fun _ _ _ _ hx hy => propext (le_congr hx hy) le_refl := by rintro ⟨x⟩ exact le_refl x le_trans := by rintro ⟨x⟩ ⟨y⟩ ⟨z⟩ exact @le_trans _ _ x y z le_antisymm := by rintro ⟨x⟩ ⟨y⟩ h₁ h₂ apply Quot.sound exact ⟨h₁, h₂⟩ lt := Quotient.lift₂ (· < ·) fun _ _ _ _ hx hy => propext (lt_congr hx hy) lt_iff_le_not_ge := by rintro ⟨x⟩ ⟨y⟩ exact @lt_iff_le_not_ge _ _ x y /-- The less or fuzzy relation on games. If `0 ⧏ x` (less or fuzzy with), then Left can win `x` as the first player. -/ def LF : Game → Game → Prop := Quotient.lift₂ PGame.LF fun _ _ _ _ hx hy => propext (lf_congr hx hy) /-- On `Game`, simp-normal inequalities should use as few negations as possible. -/ @[simp] theorem not_le : ∀ {x y : Game}, ¬x ≤ y ↔ Game.LF y x := by rintro ⟨x⟩ ⟨y⟩ exact PGame.not_le /-- On `Game`, simp-normal inequalities should use as few negations as possible. -/ @[simp] theorem not_lf : ∀ {x y : Game}, ¬Game.LF x y ↔ y ≤ x := by rintro ⟨x⟩ ⟨y⟩ exact PGame.not_lf /-- The fuzzy, confused, or incomparable relation on games. If `x ‖ 0`, then the first player can always win `x`. -/ def Fuzzy : Game → Game → Prop := Quotient.lift₂ PGame.Fuzzy fun _ _ _ _ hx hy => propext (fuzzy_congr hx hy) -- Porting note: had to replace ⧏ with LF, otherwise cannot differentiate with the operator on PGame instance : IsTrichotomous Game LF := ⟨by rintro ⟨x⟩ ⟨y⟩ change _ ∨ ⟦x⟧ = ⟦y⟧ ∨ _ rw [Quotient.eq] apply lf_or_equiv_or_gf⟩ /-! It can be useful to use these lemmas to turn `PGame` inequalities into `Game` inequalities, as the `AddCommGroup` structure on `Game` often simplifies many proofs. -/ end Game namespace PGame -- Porting note: In a lot of places, I had to add explicitly that the quotient element was a Game. -- In Lean4, quotients don't have the setoid as an instance argument, -- but as an explicit argument, see https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/confusion.20between.20equivalence.20and.20instance.20setoid/near/360822354 theorem le_iff_game_le {x y : PGame} : x ≤ y ↔ (⟦x⟧ : Game) ≤ ⟦y⟧ := Iff.rfl theorem lf_iff_game_lf {x y : PGame} : x ⧏ y ↔ Game.LF ⟦x⟧ ⟦y⟧ := Iff.rfl theorem lt_iff_game_lt {x y : PGame} : x < y ↔ (⟦x⟧ : Game) < ⟦y⟧ := Iff.rfl theorem equiv_iff_game_eq {x y : PGame} : x ≈ y ↔ (⟦x⟧ : Game) = ⟦y⟧ := (@Quotient.eq' _ _ x y).symm alias ⟨game_eq, _⟩ := equiv_iff_game_eq theorem fuzzy_iff_game_fuzzy {x y : PGame} : x ‖ y ↔ Game.Fuzzy ⟦x⟧ ⟦y⟧ := Iff.rfl end PGame namespace Game local infixl:50 " ⧏ " => LF local infixl:50 " ‖ " => Fuzzy instance addLeftMono : AddLeftMono Game := ⟨by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ h exact @add_le_add_left _ _ _ _ b c h a⟩ instance addRightMono : AddRightMono Game := ⟨by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ h exact @add_le_add_right _ _ _ _ b c h a⟩ instance addLeftStrictMono : AddLeftStrictMono Game := ⟨by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ h exact @add_lt_add_left _ _ _ _ b c h a⟩ instance addRightStrictMono : AddRightStrictMono Game := ⟨by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ h exact @add_lt_add_right _ _ _ _ b c h a⟩ theorem add_lf_add_right : ∀ {b c : Game} (_ : b ⧏ c) (a), (b + a : Game) ⧏ c + a := by rintro ⟨b⟩ ⟨c⟩ h ⟨a⟩ apply PGame.add_lf_add_right h theorem add_lf_add_left : ∀ {b c : Game} (_ : b ⧏ c) (a), (a + b : Game) ⧏ a + c := by rintro ⟨b⟩ ⟨c⟩ h ⟨a⟩ apply PGame.add_lf_add_left h instance isOrderedAddMonoid : IsOrderedAddMonoid Game := { add_le_add_left := @add_le_add_left _ _ _ Game.addLeftMono } /-- A small family of games is bounded above. -/ lemma bddAbove_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → Game.{u}) : BddAbove (Set.range f) := by obtain ⟨x, hx⟩ := PGame.bddAbove_range_of_small (Quotient.out ∘ f) refine ⟨⟦x⟧, Set.forall_mem_range.2 fun i ↦ ?_⟩ simpa [PGame.le_iff_game_le] using hx <| Set.mem_range_self i /-- A small set of games is bounded above. -/ lemma bddAbove_of_small (s : Set Game.{u}) [Small.{u} s] : BddAbove s := by simpa using bddAbove_range_of_small (Subtype.val : s → Game.{u}) /-- A small family of games is bounded below. -/ lemma bddBelow_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → Game.{u}) : BddBelow (Set.range f) := by obtain ⟨x, hx⟩ := PGame.bddBelow_range_of_small (Quotient.out ∘ f) refine ⟨⟦x⟧, Set.forall_mem_range.2 fun i ↦ ?_⟩ simpa [PGame.le_iff_game_le] using hx <| Set.mem_range_self i /-- A small set of games is bounded below. -/ lemma bddBelow_of_small (s : Set Game.{u}) [Small.{u} s] : BddBelow s := by simpa using bddBelow_range_of_small (Subtype.val : s → Game.{u}) end Game namespace PGame @[simp] theorem quot_zero : (⟦0⟧ : Game) = 0 := rfl @[simp] theorem quot_one : (⟦1⟧ : Game) = 1 := rfl @[simp] theorem quot_neg (a : PGame) : (⟦-a⟧ : Game) = -⟦a⟧ := rfl @[simp] theorem quot_add (a b : PGame) : ⟦a + b⟧ = (⟦a⟧ : Game) + ⟦b⟧ := rfl @[simp] theorem quot_sub (a b : PGame) : ⟦a - b⟧ = (⟦a⟧ : Game) - ⟦b⟧ := rfl @[simp] theorem quot_natCast : ∀ n : ℕ, ⟦(n : PGame)⟧ = (n : Game) | 0 => rfl | n + 1 => by rw [PGame.nat_succ, quot_add, Nat.cast_add, Nat.cast_one, quot_natCast] rfl theorem quot_eq_of_mk'_quot_eq {x y : PGame} (L : x.LeftMoves ≃ y.LeftMoves) (R : x.RightMoves ≃ y.RightMoves) (hl : ∀ i, (⟦x.moveLeft i⟧ : Game) = ⟦y.moveLeft (L i)⟧) (hr : ∀ j, (⟦x.moveRight j⟧ : Game) = ⟦y.moveRight (R j)⟧) : (⟦x⟧ : Game) = ⟦y⟧ := game_eq (.of_equiv L R (fun _ => equiv_iff_game_eq.2 (hl _)) (fun _ => equiv_iff_game_eq.2 (hr _))) /-! Multiplicative operations can be defined at the level of pre-games, but to prove their properties we need to use the abelian group structure of games. Hence we define them here. -/ /-- The product of `x = {xL | xR}` and `y = {yL | yR}` is `{xL*y + x*yL - xL*yL, xR*y + x*yR - xR*yR | xL*y + x*yR - xL*yR, xR*y + x*yL - xR*yL}`. -/ instance : Mul PGame.{u} := ⟨fun x y => by induction x generalizing y with | mk xl xr _ _ IHxl IHxr => _ induction y with | mk yl yr yL yR IHyl IHyr => _ have y := mk yl yr yL yR refine ⟨(xl × yl) ⊕ (xr × yr), (xl × yr) ⊕ (xr × yl), ?_, ?_⟩ <;> rintro (⟨i, j⟩ | ⟨i, j⟩) · exact IHxl i y + IHyl j - IHxl i (yL j) · exact IHxr i y + IHyr j - IHxr i (yR j) · exact IHxl i y + IHyr j - IHxl i (yR j) · exact IHxr i y + IHyl j - IHxr i (yL j)⟩ theorem leftMoves_mul : ∀ x y : PGame.{u}, (x * y).LeftMoves = (x.LeftMoves × y.LeftMoves ⊕ x.RightMoves × y.RightMoves) | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩ => rfl theorem rightMoves_mul : ∀ x y : PGame.{u}, (x * y).RightMoves = (x.LeftMoves × y.RightMoves ⊕ x.RightMoves × y.LeftMoves) | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩ => rfl /-- Turns two left or right moves for `x` and `y` into a left move for `x * y` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toLeftMovesMul {x y : PGame} : (x.LeftMoves × y.LeftMoves) ⊕ (x.RightMoves × y.RightMoves) ≃ (x * y).LeftMoves := Equiv.cast (leftMoves_mul x y).symm /-- Turns a left and a right move for `x` and `y` into a right move for `x * y` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toRightMovesMul {x y : PGame} : (x.LeftMoves × y.RightMoves) ⊕ (x.RightMoves × y.LeftMoves) ≃ (x * y).RightMoves := Equiv.cast (rightMoves_mul x y).symm @[simp] theorem mk_mul_moveLeft_inl {xl xr yl yr} {xL xR yL yR} {i j} : (mk xl xr xL xR * mk yl yr yL yR).moveLeft (Sum.inl (i, j)) = xL i * mk yl yr yL yR + mk xl xr xL xR * yL j - xL i * yL j := rfl @[simp] theorem mul_moveLeft_inl {x y : PGame} {i j} : (x * y).moveLeft (toLeftMovesMul (Sum.inl (i, j))) = x.moveLeft i * y + x * y.moveLeft j - x.moveLeft i * y.moveLeft j := by cases x cases y rfl @[simp] theorem mk_mul_moveLeft_inr {xl xr yl yr} {xL xR yL yR} {i j} : (mk xl xr xL xR * mk yl yr yL yR).moveLeft (Sum.inr (i, j)) = xR i * mk yl yr yL yR + mk xl xr xL xR * yR j - xR i * yR j := rfl @[simp] theorem mul_moveLeft_inr {x y : PGame} {i j} : (x * y).moveLeft (toLeftMovesMul (Sum.inr (i, j))) = x.moveRight i * y + x * y.moveRight j - x.moveRight i * y.moveRight j := by cases x cases y rfl @[simp] theorem mk_mul_moveRight_inl {xl xr yl yr} {xL xR yL yR} {i j} : (mk xl xr xL xR * mk yl yr yL yR).moveRight (Sum.inl (i, j)) = xL i * mk yl yr yL yR + mk xl xr xL xR * yR j - xL i * yR j := rfl @[simp] theorem mul_moveRight_inl {x y : PGame} {i j} : (x * y).moveRight (toRightMovesMul (Sum.inl (i, j))) = x.moveLeft i * y + x * y.moveRight j - x.moveLeft i * y.moveRight j := by cases x cases y rfl @[simp] theorem mk_mul_moveRight_inr {xl xr yl yr} {xL xR yL yR} {i j} : (mk xl xr xL xR * mk yl yr yL yR).moveRight (Sum.inr (i, j)) = xR i * mk yl yr yL yR + mk xl xr xL xR * yL j - xR i * yL j := rfl @[simp] theorem mul_moveRight_inr {x y : PGame} {i j} : (x * y).moveRight (toRightMovesMul (Sum.inr (i, j))) = x.moveRight i * y + x * y.moveLeft j - x.moveRight i * y.moveLeft j := by cases x cases y rfl @[simp] theorem neg_mk_mul_moveLeft_inl {xl xr yl yr} {xL xR yL yR} {i j} : (-(mk xl xr xL xR * mk yl yr yL yR)).moveLeft (Sum.inl (i, j)) = -(xL i * mk yl yr yL yR + mk xl xr xL xR * yR j - xL i * yR j) := rfl @[simp] theorem neg_mk_mul_moveLeft_inr {xl xr yl yr} {xL xR yL yR} {i j} : (-(mk xl xr xL xR * mk yl yr yL yR)).moveLeft (Sum.inr (i, j)) = -(xR i * mk yl yr yL yR + mk xl xr xL xR * yL j - xR i * yL j) := rfl @[simp] theorem neg_mk_mul_moveRight_inl {xl xr yl yr} {xL xR yL yR} {i j} : (-(mk xl xr xL xR * mk yl yr yL yR)).moveRight (Sum.inl (i, j)) = -(xL i * mk yl yr yL yR + mk xl xr xL xR * yL j - xL i * yL j) := rfl @[simp] theorem neg_mk_mul_moveRight_inr {xl xr yl yr} {xL xR yL yR} {i j} : (-(mk xl xr xL xR * mk yl yr yL yR)).moveRight (Sum.inr (i, j)) = -(xR i * mk yl yr yL yR + mk xl xr xL xR * yR j - xR i * yR j) := rfl theorem leftMoves_mul_cases {x y : PGame} (k) {P : (x * y).LeftMoves → Prop} (hl : ∀ ix iy, P <| toLeftMovesMul (Sum.inl ⟨ix, iy⟩)) (hr : ∀ jx jy, P <| toLeftMovesMul (Sum.inr ⟨jx, jy⟩)) : P k := by rw [← toLeftMovesMul.apply_symm_apply k] rcases toLeftMovesMul.symm k with (⟨ix, iy⟩ | ⟨jx, jy⟩) · apply hl · apply hr theorem rightMoves_mul_cases {x y : PGame} (k) {P : (x * y).RightMoves → Prop} (hl : ∀ ix jy, P <| toRightMovesMul (Sum.inl ⟨ix, jy⟩)) (hr : ∀ jx iy, P <| toRightMovesMul (Sum.inr ⟨jx, iy⟩)) : P k := by rw [← toRightMovesMul.apply_symm_apply k] rcases toRightMovesMul.symm k with (⟨ix, iy⟩ | ⟨jx, jy⟩) · apply hl · apply hr /-- `x * y` and `y * x` have the same moves. -/ protected lemma mul_comm (x y : PGame) : x * y ≡ y * x := match x, y with | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩ => by refine Identical.of_equiv ((Equiv.prodComm _ _).sumCongr (Equiv.prodComm _ _)) ((Equiv.sumComm _ _).trans ((Equiv.prodComm _ _).sumCongr (Equiv.prodComm _ _))) ?_ ?_ <;> · rintro (⟨_, _⟩ | ⟨_, _⟩) <;> exact ((((PGame.mul_comm _ (mk _ _ _ _)).add (PGame.mul_comm (mk _ _ _ _) _)).trans (PGame.add_comm _ _)).sub (PGame.mul_comm _ _)) termination_by (x, y) /-- `x * y` and `y * x` have the same moves. -/ def mulCommRelabelling (x y : PGame.{u}) : x * y ≡r y * x := match x, y with | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩ => by refine ⟨Equiv.sumCongr (Equiv.prodComm _ _) (Equiv.prodComm _ _), (Equiv.sumComm _ _).trans (Equiv.sumCongr (Equiv.prodComm _ _) (Equiv.prodComm _ _)), ?_, ?_⟩ <;> rintro (⟨i, j⟩ | ⟨i, j⟩) <;> { dsimp exact ((addCommRelabelling _ _).trans <| (mulCommRelabelling _ _).addCongr (mulCommRelabelling _ _)).subCongr (mulCommRelabelling _ _) } termination_by (x, y) theorem quot_mul_comm (x y : PGame.{u}) : (⟦x * y⟧ : Game) = ⟦y * x⟧ := game_eq (x.mul_comm y).equiv /-- `x * y` is equivalent to `y * x`. -/ theorem mul_comm_equiv (x y : PGame) : x * y ≈ y * x := Quotient.exact <| quot_mul_comm _ _ instance isEmpty_leftMoves_mul (x y : PGame.{u}) [IsEmpty (x.LeftMoves × y.LeftMoves ⊕ x.RightMoves × y.RightMoves)] : IsEmpty (x * y).LeftMoves := by cases x cases y assumption instance isEmpty_rightMoves_mul (x y : PGame.{u}) [IsEmpty (x.LeftMoves × y.RightMoves ⊕ x.RightMoves × y.LeftMoves)] : IsEmpty (x * y).RightMoves := by cases x cases y assumption /-- `x * 0` has exactly the same moves as `0`. -/ protected lemma mul_zero (x : PGame) : x * 0 ≡ 0 := identical_zero _ /-- `x * 0` has exactly the same moves as `0`. -/ def mulZeroRelabelling (x : PGame) : x * 0 ≡r 0 := Relabelling.isEmpty _ /-- `x * 0` is equivalent to `0`. -/ theorem mul_zero_equiv (x : PGame) : x * 0 ≈ 0 := x.mul_zero.equiv @[simp] theorem quot_mul_zero (x : PGame) : (⟦x * 0⟧ : Game) = 0 := game_eq x.mul_zero_equiv /-- `0 * x` has exactly the same moves as `0`. -/ protected lemma zero_mul (x : PGame) : 0 * x ≡ 0 := identical_zero _ /-- `0 * x` has exactly the same moves as `0`. -/ def zeroMulRelabelling (x : PGame) : 0 * x ≡r 0 := Relabelling.isEmpty _ /-- `0 * x` is equivalent to `0`. -/ theorem zero_mul_equiv (x : PGame) : 0 * x ≈ 0 := x.zero_mul.equiv @[simp] theorem quot_zero_mul (x : PGame) : (⟦0 * x⟧ : Game) = 0 := game_eq x.zero_mul_equiv /-- `-x * y` and `-(x * y)` have the same moves. -/ def negMulRelabelling (x y : PGame.{u}) : -x * y ≡r -(x * y) := match x, y with | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩ => by refine ⟨Equiv.sumComm _ _, Equiv.sumComm _ _, ?_, ?_⟩ <;> rintro (⟨i, j⟩ | ⟨i, j⟩) <;> · dsimp apply ((negAddRelabelling _ _).trans _).symm apply ((negAddRelabelling _ _).trans (Relabelling.addCongr _ _)).subCongr -- Porting note: we used to just do `<;> exact (negMulRelabelling _ _).symm` from here. · exact (negMulRelabelling _ _).symm · exact (negMulRelabelling _ _).symm -- Porting note: not sure what has gone wrong here. -- The goal is hideous here, and the `exact` doesn't work, -- but if we just `change` it to look like the mathlib3 goal then we're fine!? change -(mk xl xr xL xR * _) ≡r _ exact (negMulRelabelling _ _).symm termination_by (x, y) /-- `x * -y` and `-(x * y)` have the same moves. -/ @[simp] lemma mul_neg (x y : PGame) : x * -y = -(x * y) := match x, y with | mk xl xr xL xR, mk yl yr yL yR => by refine ext rfl rfl ?_ ?_ <;> rintro (⟨i, j⟩ | ⟨i, j⟩) _ ⟨rfl⟩ all_goals dsimp rw [PGame.neg_sub', PGame.neg_add] congr exacts [mul_neg _ (mk ..), mul_neg .., mul_neg ..] termination_by (x, y) /-- `-x * y` and `-(x * y)` have the same moves. -/ lemma neg_mul (x y : PGame) : -x * y ≡ -(x * y) := ((PGame.mul_comm _ _).trans (of_eq (mul_neg _ _))).trans (PGame.mul_comm _ _).neg @[simp] theorem quot_neg_mul (x y : PGame) : (⟦-x * y⟧ : Game) = -⟦x * y⟧ := game_eq (x.neg_mul y).equiv /-- `x * -y` and `-(x * y)` have the same moves. -/ def mulNegRelabelling (x y : PGame) : x * -y ≡r -(x * y) := (mulCommRelabelling x _).trans <| (negMulRelabelling _ x).trans (mulCommRelabelling y x).negCongr theorem quot_mul_neg (x y : PGame) : ⟦x * -y⟧ = (-⟦x * y⟧ : Game) := game_eq (by rw [mul_neg]) theorem quot_neg_mul_neg (x y : PGame) : ⟦-x * -y⟧ = (⟦x * y⟧ : Game) := by simp @[simp] theorem quot_left_distrib (x y z : PGame) : (⟦x * (y + z)⟧ : Game) = ⟦x * y⟧ + ⟦x * z⟧ := match x, y, z with | mk xl xr xL xR, mk yl yr yL yR, mk zl zr zL zR => by let x := mk xl xr xL xR let y := mk yl yr yL yR let z := mk zl zr zL zR refine quot_eq_of_mk'_quot_eq ?_ ?_ ?_ ?_ · fconstructor · rintro (⟨_, _ | _⟩ | ⟨_, _ | _⟩) <;> -- Porting note: we've increased `maxDepth` here from `5` to `6`. -- Likely this sort of off-by-one error is just a change in the implementation -- of `solve_by_elim`. solve_by_elim (maxDepth := 6) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, _⟩ | ⟨_, _⟩) <;> solve_by_elim (maxDepth := 6) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨_, _ | _⟩ | ⟨_, _ | _⟩) <;> rfl · rintro (⟨⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, _⟩ | ⟨_, _⟩) <;> rfl · fconstructor · rintro (⟨_, _ | _⟩ | ⟨_, _ | _⟩) <;> solve_by_elim (maxDepth := 6) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, _⟩ | ⟨_, _⟩) <;> solve_by_elim (maxDepth := 6) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨_, _ | _⟩ | ⟨_, _ | _⟩) <;> rfl · rintro (⟨⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, _⟩ | ⟨_, _⟩) <;> rfl -- Porting note: explicitly wrote out arguments to each recursive -- quot_left_distrib reference below, because otherwise the decreasing_by block -- failed. Previously, each branch ended with: `simp [quot_left_distrib]; abel` -- See https://github.com/leanprover/lean4/issues/2288 · rintro (⟨i, j | k⟩ | ⟨i, j | k⟩) · change ⟦xL i * (y + z) + x * (yL j + z) - xL i * (yL j + z)⟧ = ⟦xL i * y + x * yL j - xL i * yL j + x * z⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_left_distrib (xL i) (yL j) (mk zl zr zL zR)] abel · change ⟦xL i * (y + z) + x * (y + zL k) - xL i * (y + zL k)⟧ = ⟦x * y + (xL i * z + x * zL k - xL i * zL k)⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (zL k)] abel · change ⟦xR i * (y + z) + x * (yR j + z) - xR i * (yR j + z)⟧ = ⟦xR i * y + x * yR j - xR i * yR j + x * z⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_left_distrib (xR i) (yR j) (mk zl zr zL zR)] abel · change ⟦xR i * (y + z) + x * (y + zR k) - xR i * (y + zR k)⟧ = ⟦x * y + (xR i * z + x * zR k - xR i * zR k)⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (zR k)] abel · rintro (⟨i, j | k⟩ | ⟨i, j | k⟩) · change ⟦xL i * (y + z) + x * (yR j + z) - xL i * (yR j + z)⟧ = ⟦xL i * y + x * yR j - xL i * yR j + x * z⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_left_distrib (xL i) (yR j) (mk zl zr zL zR)] abel · change ⟦xL i * (y + z) + x * (y + zR k) - xL i * (y + zR k)⟧ = ⟦x * y + (xL i * z + x * zR k - xL i * zR k)⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_left_distrib (xL i) (mk yl yr yL yR) (zR k)] abel · change ⟦xR i * (y + z) + x * (yL j + z) - xR i * (yL j + z)⟧ = ⟦xR i * y + x * yL j - xR i * yL j + x * z⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_left_distrib (xR i) (yL j) (mk zl zr zL zR)] abel · change ⟦xR i * (y + z) + x * (y + zL k) - xR i * (y + zL k)⟧ = ⟦x * y + (xR i * z + x * zL k - xR i * zL k)⟧ simp only [quot_sub, quot_add] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_left_distrib (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_left_distrib (xR i) (mk yl yr yL yR) (zL k)] abel termination_by (x, y, z) /-- `x * (y + z)` is equivalent to `x * y + x * z`. -/ theorem left_distrib_equiv (x y z : PGame) : x * (y + z) ≈ x * y + x * z := Quotient.exact <| quot_left_distrib _ _ _ @[simp] theorem quot_left_distrib_sub (x y z : PGame) : (⟦x * (y - z)⟧ : Game) = ⟦x * y⟧ - ⟦x * z⟧ := by change (⟦x * (y + -z)⟧ : Game) = ⟦x * y⟧ + -⟦x * z⟧ rw [quot_left_distrib, quot_mul_neg] @[simp] theorem quot_right_distrib (x y z : PGame) : (⟦(x + y) * z⟧ : Game) = ⟦x * z⟧ + ⟦y * z⟧ := by simp only [quot_mul_comm, quot_left_distrib] /-- `(x + y) * z` is equivalent to `x * z + y * z`. -/ theorem right_distrib_equiv (x y z : PGame) : (x + y) * z ≈ x * z + y * z := Quotient.exact <| quot_right_distrib _ _ _ @[simp] theorem quot_right_distrib_sub (x y z : PGame) : (⟦(y - z) * x⟧ : Game) = ⟦y * x⟧ - ⟦z * x⟧ := by change (⟦(y + -z) * x⟧ : Game) = ⟦y * x⟧ + -⟦z * x⟧ rw [quot_right_distrib, quot_neg_mul] /-- `x * 1` has the same moves as `x`. -/ def mulOneRelabelling : ∀ x : PGame.{u}, x * 1 ≡r x | ⟨xl, xr, xL, xR⟩ => by -- Porting note: the next four lines were just `unfold has_one.one,` change _ * One.one ≡r _ unfold One.one unfold instOnePGame change mk _ _ _ _ * mk _ _ _ _ ≡r _ refine ⟨(Equiv.sumEmpty _ _).trans (Equiv.prodPUnit _), (Equiv.emptySum _ _).trans (Equiv.prodPUnit _), ?_, ?_⟩ <;> (try rintro (⟨i, ⟨⟩⟩ | ⟨i, ⟨⟩⟩)) <;> { dsimp apply (Relabelling.subCongr (Relabelling.refl _) (mulZeroRelabelling _)).trans rw [sub_zero_eq_add_zero] exact (addZeroRelabelling _).trans <| (((mulOneRelabelling _).addCongr (mulZeroRelabelling _)).trans <| addZeroRelabelling _) } /-- `1 * x` has the same moves as `x`. -/ protected lemma one_mul : ∀ (x : PGame), 1 * x ≡ x | ⟨xl, xr, xL, xR⟩ => by refine Identical.of_equiv ((Equiv.sumEmpty _ _).trans (Equiv.punitProd _)) ((Equiv.sumEmpty _ _).trans (Equiv.punitProd _)) ?_ ?_ <;> · rintro (⟨⟨⟩, _⟩ | ⟨⟨⟩, _⟩) exact ((((PGame.zero_mul (mk _ _ _ _)).add (PGame.one_mul _)).trans (PGame.zero_add _)).sub (PGame.zero_mul _)).trans (PGame.sub_zero _) /-- `x * 1` has the same moves as `x`. -/ protected lemma mul_one (x : PGame) : x * 1 ≡ x := (x.mul_comm _).trans x.one_mul @[simp] theorem quot_mul_one (x : PGame) : (⟦x * 1⟧ : Game) = ⟦x⟧ := game_eq x.mul_one.equiv /-- `x * 1` is equivalent to `x`. -/ theorem mul_one_equiv (x : PGame) : x * 1 ≈ x := Quotient.exact <| quot_mul_one x /-- `1 * x` has the same moves as `x`. -/ def oneMulRelabelling (x : PGame) : 1 * x ≡r x := (mulCommRelabelling 1 x).trans <| mulOneRelabelling x @[simp] theorem quot_one_mul (x : PGame) : (⟦1 * x⟧ : Game) = ⟦x⟧ := game_eq x.one_mul.equiv /-- `1 * x` is equivalent to `x`. -/ theorem one_mul_equiv (x : PGame) : 1 * x ≈ x := Quotient.exact <| quot_one_mul x theorem quot_mul_assoc (x y z : PGame) : (⟦x * y * z⟧ : Game) = ⟦x * (y * z)⟧ := match x, y, z with | mk xl xr xL xR, mk yl yr yL yR, mk zl zr zL zR => by let x := mk xl xr xL xR let y := mk yl yr yL yR let z := mk zl zr zL zR refine quot_eq_of_mk'_quot_eq ?_ ?_ ?_ ?_ · fconstructor · rintro (⟨⟨_, _⟩ | ⟨_, _⟩, _⟩ | ⟨⟨_, _⟩ | ⟨_, _⟩, _⟩) <;> -- Porting note: as above, increased the `maxDepth` here by 1. solve_by_elim (maxDepth := 8) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩) <;> solve_by_elim (maxDepth := 8) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨⟨_, _⟩ | ⟨_, _⟩, _⟩ | ⟨⟨_, _⟩ | ⟨_, _⟩, _⟩) <;> rfl · rintro (⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩) <;> rfl · fconstructor · rintro (⟨⟨_, _⟩ | ⟨_, _⟩, _⟩ | ⟨⟨_, _⟩ | ⟨_, _⟩, _⟩) <;> solve_by_elim (maxDepth := 8) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩) <;> solve_by_elim (maxDepth := 8) [Sum.inl, Sum.inr, Prod.mk] · rintro (⟨⟨_, _⟩ | ⟨_, _⟩, _⟩ | ⟨⟨_, _⟩ | ⟨_, _⟩, _⟩) <;> rfl · rintro (⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩ | ⟨_, ⟨_, _⟩ | ⟨_, _⟩⟩) <;> rfl -- Porting note: explicitly wrote out arguments to each recursive -- quot_mul_assoc reference below, because otherwise the decreasing_by block -- failed. Each branch previously ended with: `simp [quot_mul_assoc]; abel` -- See https://github.com/leanprover/lean4/issues/2288 · rintro (⟨⟨i, j⟩ | ⟨i, j⟩, k⟩ | ⟨⟨i, j⟩ | ⟨i, j⟩, k⟩) · change ⟦(xL i * y + x * yL j - xL i * yL j) * z + x * y * zL k - (xL i * y + x * yL j - xL i * yL j) * zL k⟧ = ⟦xL i * (y * z) + x * (yL j * z + y * zL k - yL j * zL k) - xL i * (yL j * z + y * zL k - yL j * zL k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (xL i) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (zL k)] rw [quot_mul_assoc (xL i) (yL j) (zL k)] abel · change ⟦(xR i * y + x * yR j - xR i * yR j) * z + x * y * zL k - (xR i * y + x * yR j - xR i * yR j) * zL k⟧ = ⟦xR i * (y * z) + x * (yR j * z + y * zL k - yR j * zL k) - xR i * (yR j * z + y * zL k - yR j * zL k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (xR i) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (zL k)] rw [quot_mul_assoc (xR i) (yR j) (zL k)] abel · change ⟦(xL i * y + x * yR j - xL i * yR j) * z + x * y * zR k - (xL i * y + x * yR j - xL i * yR j) * zR k⟧ = ⟦xL i * (y * z) + x * (yR j * z + y * zR k - yR j * zR k) - xL i * (yR j * z + y * zR k - yR j * zR k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (xL i) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (zR k)] rw [quot_mul_assoc (xL i) (yR j) (zR k)] abel · change ⟦(xR i * y + x * yL j - xR i * yL j) * z + x * y * zR k - (xR i * y + x * yL j - xR i * yL j) * zR k⟧ = ⟦xR i * (y * z) + x * (yL j * z + y * zR k - yL j * zR k) - xR i * (yL j * z + y * zR k - yL j * zR k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (xR i) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (zR k)] rw [quot_mul_assoc (xR i) (yL j) (zR k)] abel · rintro (⟨⟨i, j⟩ | ⟨i, j⟩, k⟩ | ⟨⟨i, j⟩ | ⟨i, j⟩, k⟩) · change ⟦(xL i * y + x * yL j - xL i * yL j) * z + x * y * zR k - (xL i * y + x * yL j - xL i * yL j) * zR k⟧ = ⟦xL i * (y * z) + x * (yL j * z + y * zR k - yL j * zR k) - xL i * (yL j * z + y * zR k - yL j * zR k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (xL i) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (zR k)] rw [quot_mul_assoc (xL i) (yL j) (zR k)] abel · change ⟦(xR i * y + x * yR j - xR i * yR j) * z + x * y * zR k - (xR i * y + x * yR j - xR i * yR j) * zR k⟧ = ⟦xR i * (y * z) + x * (yR j * z + y * zR k - yR j * zR k) - xR i * (yR j * z + y * zR k - yR j * zR k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (xR i) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (zR k)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (zR k)] rw [quot_mul_assoc (xR i) (yR j) (zR k)] abel · change ⟦(xL i * y + x * yR j - xL i * yR j) * z + x * y * zL k - (xL i * y + x * yR j - xL i * yR j) * zL k⟧ = ⟦xL i * (y * z) + x * (yR j * z + y * zL k - yR j * zL k) - xL i * (yR j * z + y * zL k - yR j * zL k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (xL i) (yR j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (xL i) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (mk xl xr xL xR) (yR j) (zL k)] rw [quot_mul_assoc (xL i) (yR j) (zL k)] abel · change ⟦(xR i * y + x * yL j - xR i * yL j) * z + x * y * zL k - (xR i * y + x * yL j - xR i * yL j) * zL k⟧ = ⟦xR i * (y * z) + x * (yL j * z + y * zL k - yL j * zL k) - xR i * (yL j * z + y * zL k - yL j * zL k)⟧ simp only [quot_sub, quot_add, quot_right_distrib_sub, quot_right_distrib, quot_left_distrib_sub, quot_left_distrib] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (xR i) (yL j) (mk zl zr zL zR)] rw [quot_mul_assoc (mk xl xr xL xR) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (xR i) (mk yl yr yL yR) (zL k)] rw [quot_mul_assoc (mk xl xr xL xR) (yL j) (zL k)] rw [quot_mul_assoc (xR i) (yL j) (zL k)] abel termination_by (x, y, z) /-- `x * y * z` is equivalent to `x * (y * z)`. -/ theorem mul_assoc_equiv (x y z : PGame) : x * y * z ≈ x * (y * z) := Quotient.exact <| quot_mul_assoc _ _ _ /-- The left options of `x * y` of the first kind, i.e. of the form `xL * y + x * yL - xL * yL`. -/ def mulOption (x y : PGame) (i : LeftMoves x) (j : LeftMoves y) : PGame := x.moveLeft i * y + x * y.moveLeft j - x.moveLeft i * y.moveLeft j /-- Any left option of `x * y` of the first kind is also a left option of `x * -(-y)` of the first kind. -/ lemma mulOption_neg_neg {x} (y) {i j} : mulOption x y i j = mulOption x (-(-y)) i (toLeftMovesNeg <| toRightMovesNeg j) := by simp [mulOption] /-- The left options of `x * y` agree with that of `y * x` up to equivalence. -/ lemma mulOption_symm (x y) {i j} : ⟦mulOption x y i j⟧ = (⟦mulOption y x j i⟧ : Game) := by dsimp only [mulOption, quot_sub, quot_add] rw [add_comm] congr 1 on_goal 1 => congr 1 all_goals rw [quot_mul_comm] /-- The left options of `x * y` of the second kind are the left options of `(-x) * (-y)` of the first kind, up to equivalence. -/ lemma leftMoves_mul_iff {x y : PGame} (P : Game → Prop) : (∀ k, P ⟦(x * y).moveLeft k⟧) ↔ (∀ i j, P ⟦mulOption x y i j⟧) ∧ (∀ i j, P ⟦mulOption (-x) (-y) i j⟧) := by cases x; cases y constructor <;> intro h on_goal 1 => constructor <;> intro i j · exact h (Sum.inl (i, j)) convert h (Sum.inr (i, j)) using 1 on_goal 2 => rintro (⟨i, j⟩ | ⟨i, j⟩) · exact h.1 i j convert h.2 i j using 1 all_goals dsimp only [mk_mul_moveLeft_inr, quot_sub, quot_add, neg_def, mulOption, moveLeft_mk] rw [← neg_def, ← neg_def] congr 1 on_goal 1 => congr 1 all_goals rw [quot_neg_mul_neg] /-- The right options of `x * y` are the left options of `x * (-y)` and of `(-x) * y` of the first kind, up to equivalence. -/ lemma rightMoves_mul_iff {x y : PGame} (P : Game → Prop) : (∀ k, P ⟦(x * y).moveRight k⟧) ↔ (∀ i j, P (-⟦mulOption x (-y) i j⟧)) ∧ (∀ i j, P (-⟦mulOption (-x) y i j⟧)) := by cases x; cases y constructor <;> intro h on_goal 1 => constructor <;> intro i j on_goal 1 => convert h (Sum.inl (i, j)) on_goal 2 => convert h (Sum.inr (i, j)) on_goal 3 => rintro (⟨i, j⟩ | ⟨i, j⟩) on_goal 1 => convert h.1 i j using 1 on_goal 2 => convert h.2 i j using 1 all_goals dsimp [mulOption] rw [neg_sub', neg_add, ← neg_def] congr 1 on_goal 1 => congr 1 any_goals rw [quot_neg_mul, neg_neg] iterate 6 rw [quot_mul_neg, neg_neg] /-- Because the two halves of the definition of `inv` produce more elements on each side, we have to define the two families inductively. This is the indexing set for the function, and `invVal` is the function part. -/ inductive InvTy (l r : Type u) : Bool → Type u | zero : InvTy l r false | left₁ : r → InvTy l r false → InvTy l r false | left₂ : l → InvTy l r true → InvTy l r false | right₁ : l → InvTy l r false → InvTy l r true | right₂ : r → InvTy l r true → InvTy l r true instance (l r : Type u) [IsEmpty l] [IsEmpty r] : IsEmpty (InvTy l r true) := ⟨by rintro (_ | _ | _ | a | a) <;> exact isEmptyElim a⟩ instance InvTy.instInhabited (l r : Type u) : Inhabited (InvTy l r false) := ⟨InvTy.zero⟩ instance uniqueInvTy (l r : Type u) [IsEmpty l] [IsEmpty r] : Unique (InvTy l r false) := { InvTy.instInhabited l r with uniq := by rintro (a | a | a) · rfl all_goals exact isEmptyElim a } /-- Because the two halves of the definition of `inv` produce more elements of each side, we have to define the two families inductively. This is the function part, defined by recursion on `InvTy`. -/ def invVal {l r} (L : l → PGame) (R : r → PGame) (IHl : l → PGame) (IHr : r → PGame) (x : PGame) : ∀ {b}, InvTy l r b → PGame | _, InvTy.zero => 0 | _, InvTy.left₁ i j => (1 + (R i - x) * invVal L R IHl IHr x j) * IHr i | _, InvTy.left₂ i j => (1 + (L i - x) * invVal L R IHl IHr x j) * IHl i | _, InvTy.right₁ i j => (1 + (L i - x) * invVal L R IHl IHr x j) * IHl i | _, InvTy.right₂ i j => (1 + (R i - x) * invVal L R IHl IHr x j) * IHr i @[simp] theorem invVal_isEmpty {l r : Type u} {b} (L R IHl IHr) (i : InvTy l r b) (x) [IsEmpty l] [IsEmpty r] : invVal L R IHl IHr x i = 0 := by obtain - | a | a | a | a := i · rfl all_goals exact isEmptyElim a /-- The inverse of a positive surreal number `x = {L | R}` is given by `x⁻¹ = {0, (1 + (R - x) * x⁻¹L) * R, (1 + (L - x) * x⁻¹R) * L | (1 + (L - x) * x⁻¹L) * L, (1 + (R - x) * x⁻¹R) * R}`. Because the two halves `x⁻¹L, x⁻¹R` of `x⁻¹` are used in their own definition, the sets and elements are inductively generated. -/ def inv' : PGame → PGame | ⟨l, r, L, R⟩ => let l' := { i // 0 < L i } let L' : l' → PGame := fun i => L i.1 let IHl' : l' → PGame := fun i => inv' (L i.1) let IHr i := inv' (R i) let x := mk l r L R ⟨InvTy l' r false, InvTy l' r true, invVal L' R IHl' IHr x, invVal L' R IHl' IHr x⟩ theorem zero_lf_inv' : ∀ x : PGame, 0 ⧏ inv' x | ⟨xl, xr, xL, xR⟩ => by convert lf_mk _ _ InvTy.zero rfl /-- `inv' 0` has exactly the same moves as `1`. -/ def inv'Zero : inv' 0 ≡r 1 := by change mk _ _ _ _ ≡r 1 refine ⟨?_, ?_, fun i => ?_, IsEmpty.elim ?_⟩ · apply Equiv.equivPUnit (InvTy _ _ _) · apply Equiv.equivPEmpty (InvTy _ _ _) · -- Porting note: we added `rfl` after the `simp` -- (because `simp` now uses `rfl` only at reducible transparency) -- Can we improve the simp set so it is not needed? simp; rfl · dsimp infer_instance theorem inv'_zero_equiv : inv' 0 ≈ 1 := inv'Zero.equiv /-- `inv' 1` has exactly the same moves as `1`. -/ lemma inv'_one : inv' 1 ≡ 1 := by rw [Identical.ext_iff] constructor · simp [memₗ_def, inv', isEmpty_subtype] · simp [memᵣ_def, inv', isEmpty_subtype] /-- `inv' 1` has exactly the same moves as `1`. -/ def inv'One : inv' 1 ≡r (1 : PGame.{u}) := by change Relabelling (mk _ _ _ _) 1 have : IsEmpty { _i : PUnit.{u + 1} // (0 : PGame.{u}) < 0 } := by rw [lt_self_iff_false] infer_instance refine ⟨?_, ?_, fun i => ?_, IsEmpty.elim ?_⟩ <;> dsimp · apply Equiv.equivPUnit · apply Equiv.equivOfIsEmpty · -- Porting note: had to add `rfl`, because `simp` only uses the built-in `rfl`. simp; rfl · infer_instance theorem inv'_one_equiv : inv' 1 ≈ 1 := inv'_one.equiv /-- The inverse of a pre-game in terms of the inverse on positive pre-games. -/ noncomputable instance : Inv PGame := ⟨by classical exact fun x => if x ≈ 0 then 0 else if 0 < x then inv' x else -inv' (-x)⟩ noncomputable instance : Div PGame := ⟨fun x y => x * y⁻¹⟩ theorem inv_eq_of_equiv_zero {x : PGame} (h : x ≈ 0) : x⁻¹ = 0 := by classical exact if_pos h @[simp] theorem inv_zero : (0 : PGame)⁻¹ = 0 := inv_eq_of_equiv_zero (equiv_refl _) theorem inv_eq_of_pos {x : PGame} (h : 0 < x) : x⁻¹ = inv' x := by classical exact (if_neg h.lf.not_equiv').trans (if_pos h) theorem inv_eq_of_lf_zero {x : PGame} (h : x ⧏ 0) : x⁻¹ = -inv' (-x) := by classical exact (if_neg h.not_equiv).trans (if_neg h.not_gt) /-- `1⁻¹` has exactly the same moves as `1`. -/ lemma inv_one : 1⁻¹ ≡ 1 := by rw [inv_eq_of_pos PGame.zero_lt_one] exact inv'_one /-- `1⁻¹` has exactly the same moves as `1`. -/ def invOne : 1⁻¹ ≡r 1 := by rw [inv_eq_of_pos PGame.zero_lt_one] exact inv'One theorem inv_one_equiv : (1⁻¹ : PGame) ≈ 1 := inv_one.equiv end PGame end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Nim.lean
import Mathlib.SetTheory.Game.Birthday import Mathlib.SetTheory.Game.Impartial import Mathlib.SetTheory.Nimber.Basic import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Specific.Nim` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Nim and the Sprague-Grundy theorem This file contains the definition for nim for any ordinal `o`. In the game of `nim o₁` both players may move to `nim o₂` for any `o₂ < o₁`. We also define a Grundy value for an impartial game `G` and prove the Sprague-Grundy theorem, that `G` is equivalent to `nim (grundyValue G)`. Finally, we prove that the grundy value of a sum `G + H` corresponds to the nimber sum of the individual grundy values. ## Implementation details The pen-and-paper definition of nim defines the possible moves of `nim o` to be `Set.Iio o`. However, this definition does not work for us because it would make the type of nim `Ordinal.{u} → SetTheory.PGame.{u + 1}`, which would make it impossible for us to state the Sprague-Grundy theorem, since that requires the type of `nim` to be `Ordinal.{u} → SetTheory.PGame.{u}`. For this reason, we instead use `o.toType` for the possible moves. We expose `toLeftMovesNim` and `toRightMovesNim` to conveniently convert an ordinal less than `o` into a left or right move of `nim o`, and vice versa. -/ noncomputable section universe u namespace SetTheory open scoped PGame open Ordinal Nimber namespace PGame /-- The definition of single-heap nim, which can be viewed as a pile of stones where each player can take a positive number of stones from it on their turn. -/ noncomputable def nim (o : Ordinal.{u}) : PGame.{u} := ⟨o.toType, o.toType, fun x => nim ((enumIsoToType o).symm x).val, fun x => nim ((enumIsoToType o).symm x).val⟩ termination_by o decreasing_by all_goals exact ((enumIsoToType o).symm x).prop theorem leftMoves_nim (o : Ordinal) : (nim o).LeftMoves = o.toType := by rw [nim]; rfl theorem rightMoves_nim (o : Ordinal) : (nim o).RightMoves = o.toType := by rw [nim]; rfl theorem moveLeft_nim_heq (o : Ordinal) : (nim o).moveLeft ≍ fun i : o.toType => nim ((enumIsoToType o).symm i) := by rw [nim]; rfl @[deprecated (since := "2025-07-05")] alias moveLeft_nim_hEq := moveLeft_nim_heq theorem moveRight_nim_heq (o : Ordinal) : (nim o).moveRight ≍ fun i : o.toType => nim ((enumIsoToType o).symm i) := by rw [nim]; rfl @[deprecated (since := "2025-07-05")] alias moveRight_nim_hEq := moveRight_nim_heq /-- Turns an ordinal less than `o` into a left move for `nim o` and vice versa. -/ noncomputable def toLeftMovesNim {o : Ordinal} : Set.Iio o ≃ (nim o).LeftMoves := (enumIsoToType o).toEquiv.trans (Equiv.cast (leftMoves_nim o).symm) /-- Turns an ordinal less than `o` into a right move for `nim o` and vice versa. -/ noncomputable def toRightMovesNim {o : Ordinal} : Set.Iio o ≃ (nim o).RightMoves := (enumIsoToType o).toEquiv.trans (Equiv.cast (rightMoves_nim o).symm) @[simp] theorem toLeftMovesNim_symm_lt {o : Ordinal} (i : (nim o).LeftMoves) : toLeftMovesNim.symm i < o := (toLeftMovesNim.symm i).prop @[simp] theorem toRightMovesNim_symm_lt {o : Ordinal} (i : (nim o).RightMoves) : toRightMovesNim.symm i < o := (toRightMovesNim.symm i).prop @[simp] theorem moveLeft_nim {o : Ordinal} (i) : (nim o).moveLeft i = nim (toLeftMovesNim.symm i).val := (congr_heq (moveLeft_nim_heq o).symm (cast_heq _ i)).symm theorem moveLeft_toLeftMovesNim {o : Ordinal} (i) : (nim o).moveLeft (toLeftMovesNim i) = nim i := by simp @[simp] theorem moveRight_nim {o : Ordinal} (i) : (nim o).moveRight i = nim (toRightMovesNim.symm i).val := (congr_heq (moveRight_nim_heq o).symm (cast_heq _ i)).symm theorem moveRight_toRightMovesNim {o : Ordinal} (i) : (nim o).moveRight (toRightMovesNim i) = nim i := by simp /-- A recursion principle for left moves of a nim game. -/ @[elab_as_elim] def leftMovesNimRecOn {o : Ordinal} {P : (nim o).LeftMoves → Sort*} (i : (nim o).LeftMoves) (H : ∀ a (H : a < o), P <| toLeftMovesNim ⟨a, H⟩) : P i := by rw [← toLeftMovesNim.apply_symm_apply i]; apply H /-- A recursion principle for right moves of a nim game. -/ @[elab_as_elim] def rightMovesNimRecOn {o : Ordinal} {P : (nim o).RightMoves → Sort*} (i : (nim o).RightMoves) (H : ∀ a (H : a < o), P <| toRightMovesNim ⟨a, H⟩) : P i := by rw [← toRightMovesNim.apply_symm_apply i]; apply H instance isEmpty_nim_zero_leftMoves : IsEmpty (nim 0).LeftMoves := by rw [nim] exact isEmpty_toType_zero instance isEmpty_nim_zero_rightMoves : IsEmpty (nim 0).RightMoves := by rw [nim] exact isEmpty_toType_zero /-- `nim 0` has exactly the same moves as `0`. -/ def nimZeroRelabelling : nim 0 ≡r 0 := Relabelling.isEmpty _ theorem nim_zero_equiv : nim 0 ≈ 0 := Equiv.isEmpty _ noncomputable instance uniqueNimOneLeftMoves : Unique (nim 1).LeftMoves := (Equiv.cast <| leftMoves_nim 1).unique noncomputable instance uniqueNimOneRightMoves : Unique (nim 1).RightMoves := (Equiv.cast <| rightMoves_nim 1).unique @[simp] theorem default_nim_one_leftMoves_eq : (default : (nim 1).LeftMoves) = @toLeftMovesNim 1 ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := rfl @[simp] theorem default_nim_one_rightMoves_eq : (default : (nim 1).RightMoves) = @toRightMovesNim 1 ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := rfl @[simp] theorem toLeftMovesNim_one_symm (i) : (@toLeftMovesNim 1).symm i = ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := by simp [eq_iff_true_of_subsingleton] @[simp] theorem toRightMovesNim_one_symm (i) : (@toRightMovesNim 1).symm i = ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := by simp [eq_iff_true_of_subsingleton] theorem nim_one_moveLeft (x) : (nim 1).moveLeft x = nim 0 := by simp theorem nim_one_moveRight (x) : (nim 1).moveRight x = nim 0 := by simp /-- `nim 1` has exactly the same moves as `star`. -/ def nimOneRelabelling : nim 1 ≡r star := by rw [nim] refine ⟨?_, ?_, fun i => ?_, fun j => ?_⟩ any_goals dsimp; apply Equiv.ofUnique all_goals simpa [enumIsoToType] using nimZeroRelabelling theorem nim_one_equiv : nim 1 ≈ star := nimOneRelabelling.equiv @[simp] theorem nim_birthday (o : Ordinal) : (nim o).birthday = o := by induction o using Ordinal.induction with | _ o IH rw [nim, birthday_def] dsimp rw [max_eq_right le_rfl] convert lsub_typein o with i exact IH _ (typein_lt_self i) @[simp] theorem neg_nim (o : Ordinal) : -nim o = nim o := by induction o using Ordinal.induction with | _ o IH rw [nim]; dsimp; congr <;> funext i <;> exact IH _ (Ordinal.typein_lt_self i) instance impartial_nim (o : Ordinal) : Impartial (nim o) := by induction o using Ordinal.induction with | _ o IH rw [impartial_def, neg_nim] refine ⟨equiv_rfl, fun i => ?_, fun i => ?_⟩ <;> simpa using IH _ (typein_lt_self _) theorem nim_fuzzy_zero_of_ne_zero {o : Ordinal} (ho : o ≠ 0) : nim o ‖ 0 := by rw [Impartial.fuzzy_zero_iff_lf, lf_zero_le] use toRightMovesNim ⟨0, Ordinal.pos_iff_ne_zero.2 ho⟩ simp @[simp] theorem nim_add_equiv_zero_iff (o₁ o₂ : Ordinal) : (nim o₁ + nim o₂ ≈ 0) ↔ o₁ = o₂ := by constructor · refine not_imp_not.1 fun hne : _ ≠ _ => (Impartial.not_equiv_zero_iff (nim o₁ + nim o₂)).2 ?_ wlog h : o₁ < o₂ · exact (fuzzy_congr_left add_comm_equiv).1 (this _ _ hne.symm (hne.lt_or_gt.resolve_left h)) rw [Impartial.fuzzy_zero_iff_gf, zero_lf_le] use toLeftMovesAdd (Sum.inr <| toLeftMovesNim ⟨_, h⟩) · simpa using (Impartial.add_self (nim o₁)).2 · rintro rfl exact Impartial.add_self (nim o₁) @[simp] theorem nim_add_fuzzy_zero_iff {o₁ o₂ : Ordinal} : nim o₁ + nim o₂ ‖ 0 ↔ o₁ ≠ o₂ := by rw [iff_not_comm, Impartial.not_fuzzy_zero_iff, nim_add_equiv_zero_iff] @[simp] theorem nim_equiv_iff_eq {o₁ o₂ : Ordinal} : (nim o₁ ≈ nim o₂) ↔ o₁ = o₂ := by rw [Impartial.equiv_iff_add_equiv_zero, nim_add_equiv_zero_iff] /-- The Grundy value of an impartial game is recursively defined as the minimum excluded value (the infimum of the complement) of the Grundy values of either its left or right options. This is the ordinal which corresponds to the game of nim that the game is equivalent to. This function takes a value in `Nimber`. This is a type synonym for the ordinals which has the same ordering, but addition in `Nimber` is such that it corresponds to the grundy value of the addition of games. See that file for more information on nimbers and their arithmetic. -/ noncomputable def grundyValue (G : PGame.{u}) : Nimber.{u} := sInf (Set.range fun i => grundyValue (G.moveLeft i))ᶜ termination_by G theorem grundyValue_eq_sInf_moveLeft (G : PGame) : grundyValue G = sInf (Set.range (grundyValue ∘ G.moveLeft))ᶜ := by rw [grundyValue]; rfl theorem grundyValue_ne_moveLeft {G : PGame} (i : G.LeftMoves) : grundyValue (G.moveLeft i) ≠ grundyValue G := by conv_rhs => rw [grundyValue_eq_sInf_moveLeft] have := csInf_mem (nonempty_of_not_bddAbove <| Nimber.not_bddAbove_compl_of_small (Set.range fun i => grundyValue (G.moveLeft i))) rw [Set.mem_compl_iff, Set.mem_range, not_exists] at this exact this _ theorem le_grundyValue_of_Iio_subset_moveLeft {G : PGame} {o : Nimber} (h : Set.Iio o ⊆ Set.range (grundyValue ∘ G.moveLeft)) : o ≤ grundyValue G := by by_contra! ho obtain ⟨i, hi⟩ := h ho exact grundyValue_ne_moveLeft i hi theorem exists_grundyValue_moveLeft_of_lt {G : PGame} {o : Nimber} (h : o < grundyValue G) : ∃ i, grundyValue (G.moveLeft i) = o := by rw [grundyValue_eq_sInf_moveLeft] at h by_contra ha exact h.not_ge (csInf_le' ha) theorem grundyValue_le_of_forall_moveLeft {G : PGame} {o : Nimber} (h : ∀ i, grundyValue (G.moveLeft i) ≠ o) : G.grundyValue ≤ o := by contrapose! h exact exists_grundyValue_moveLeft_of_lt h /-- The **Sprague-Grundy theorem** states that every impartial game is equivalent to a game of nim, namely the game of nim corresponding to the game's Grundy value. -/ theorem equiv_nim_grundyValue (G : PGame.{u}) [G.Impartial] : G ≈ nim (toOrdinal (grundyValue G)) := by rw [Impartial.equiv_iff_add_equiv_zero, ← Impartial.forall_leftMoves_fuzzy_iff_equiv_zero] intro x apply leftMoves_add_cases x <;> intro i · rw [add_moveLeft_inl, ← fuzzy_congr_left (add_congr_left (Equiv.symm (equiv_nim_grundyValue _))), nim_add_fuzzy_zero_iff] exact grundyValue_ne_moveLeft i · rw [add_moveLeft_inr, ← Impartial.exists_left_move_equiv_iff_fuzzy_zero] obtain ⟨j, hj⟩ := exists_grundyValue_moveLeft_of_lt <| toLeftMovesNim_symm_lt i use toLeftMovesAdd (Sum.inl j) rw [add_moveLeft_inl, moveLeft_nim] exact Equiv.trans (add_congr_left (equiv_nim_grundyValue _)) (hj ▸ Impartial.add_self _) termination_by G theorem grundyValue_eq_iff_equiv_nim {G : PGame} [G.Impartial] {o : Nimber} : grundyValue G = o ↔ G ≈ nim (toOrdinal o) := ⟨by rintro rfl; exact equiv_nim_grundyValue G, by intro h; rw [← nim_equiv_iff_eq]; exact Equiv.trans (Equiv.symm (equiv_nim_grundyValue G)) h⟩ @[simp] theorem nim_grundyValue (o : Ordinal.{u}) : grundyValue (nim o) = ∗o := grundyValue_eq_iff_equiv_nim.2 PGame.equiv_rfl theorem grundyValue_eq_iff_equiv (G H : PGame) [G.Impartial] [H.Impartial] : grundyValue G = grundyValue H ↔ (G ≈ H) := grundyValue_eq_iff_equiv_nim.trans (equiv_congr_left.1 (equiv_nim_grundyValue H) _).symm @[simp] theorem grundyValue_zero : grundyValue 0 = 0 := grundyValue_eq_iff_equiv_nim.2 (Equiv.symm nim_zero_equiv) theorem grundyValue_iff_equiv_zero (G : PGame) [G.Impartial] : grundyValue G = 0 ↔ G ≈ 0 := by rw [← grundyValue_eq_iff_equiv, grundyValue_zero] @[simp] theorem grundyValue_star : grundyValue star = 1 := grundyValue_eq_iff_equiv_nim.2 (Equiv.symm nim_one_equiv) @[simp] theorem grundyValue_neg (G : PGame) [G.Impartial] : grundyValue (-G) = grundyValue G := by rw [grundyValue_eq_iff_equiv_nim, neg_equiv_iff, neg_nim, ← grundyValue_eq_iff_equiv_nim] theorem grundyValue_eq_sInf_moveRight (G : PGame) [G.Impartial] : grundyValue G = sInf (Set.range (grundyValue ∘ G.moveRight))ᶜ := by obtain ⟨l, r, L, R⟩ := G rw [← grundyValue_neg, grundyValue_eq_sInf_moveLeft] iterate 3 apply congr_arg ext i exact @grundyValue_neg _ (@Impartial.moveRight_impartial ⟨l, r, L, R⟩ _ _) theorem grundyValue_ne_moveRight {G : PGame} [G.Impartial] (i : G.RightMoves) : grundyValue (G.moveRight i) ≠ grundyValue G := by convert grundyValue_ne_moveLeft (toLeftMovesNeg i) using 1 <;> simp theorem le_grundyValue_of_Iio_subset_moveRight {G : PGame} [G.Impartial] {o : Nimber} (h : Set.Iio o ⊆ Set.range (grundyValue ∘ G.moveRight)) : o ≤ grundyValue G := by by_contra! ho obtain ⟨i, hi⟩ := h ho exact grundyValue_ne_moveRight i hi theorem exists_grundyValue_moveRight_of_lt {G : PGame} [G.Impartial] {o : Nimber} (h : o < grundyValue G) : ∃ i, grundyValue (G.moveRight i) = o := by rw [← grundyValue_neg] at h obtain ⟨i, hi⟩ := exists_grundyValue_moveLeft_of_lt h use toLeftMovesNeg.symm i rwa [← grundyValue_neg, ← moveLeft_neg] theorem grundyValue_le_of_forall_moveRight {G : PGame} [G.Impartial] {o : Nimber} (h : ∀ i, grundyValue (G.moveRight i) ≠ o) : G.grundyValue ≤ o := by contrapose! h exact exists_grundyValue_moveRight_of_lt h /-- The Grundy value of the sum of two nim games equals their nimber addition. -/ theorem grundyValue_nim_add_nim (x y : Ordinal) : grundyValue (nim x + nim y) = ∗x + ∗y := by apply (grundyValue_le_of_forall_moveLeft _).antisymm (le_grundyValue_of_Iio_subset_moveLeft _) · intro i apply leftMoves_add_cases i <;> intro j <;> have := (toLeftMovesNim_symm_lt j).ne · simpa [grundyValue_nim_add_nim (toLeftMovesNim.symm j) y] · simpa [grundyValue_nim_add_nim x (toLeftMovesNim.symm j)] · intro k hk obtain h | h := Nimber.lt_add_cases hk · let a := toOrdinal (k + ∗y) use toLeftMovesAdd (Sum.inl (toLeftMovesNim ⟨a, h⟩)) simp [a, grundyValue_nim_add_nim a y] · let a := toOrdinal (k + ∗x) use toLeftMovesAdd (Sum.inr (toLeftMovesNim ⟨a, h⟩)) simp [a, grundyValue_nim_add_nim x a, add_comm (∗x)] termination_by (x, y) theorem nim_add_nim_equiv (x y : Ordinal) : nim x + nim y ≈ nim (toOrdinal (∗x + ∗y)) := by rw [← grundyValue_eq_iff_equiv_nim, grundyValue_nim_add_nim] @[simp] theorem grundyValue_add (G H : PGame) [G.Impartial] [H.Impartial] : grundyValue (G + H) = grundyValue G + grundyValue H := by rw [← (grundyValue G).toOrdinal_toNimber, ← (grundyValue H).toOrdinal_toNimber, ← grundyValue_nim_add_nim, grundyValue_eq_iff_equiv] exact add_congr (equiv_nim_grundyValue G) (equiv_nim_grundyValue H) end PGame end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Ordinal.lean
import Mathlib.SetTheory.Game.Basic import Mathlib.SetTheory.Ordinal.NaturalOps import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Ordinal` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Ordinals as games We define the canonical map `Ordinal → SetTheory.PGame`, where every ordinal is mapped to the game whose left set consists of all previous ordinals. The map to surreals is defined in `Ordinal.toSurreal`. ## Main declarations - `Ordinal.toPGame`: The canonical map between ordinals and pre-games. - `Ordinal.toPGameEmbedding`: The order embedding version of the previous map. -/ universe u open SetTheory PGame open scoped NaturalOps PGame namespace Ordinal /-- Converts an ordinal into the corresponding pre-game. -/ noncomputable def toPGame (o : Ordinal.{u}) : PGame.{u} := ⟨o.toType, PEmpty, fun x => ((enumIsoToType o).symm x).val.toPGame, PEmpty.elim⟩ termination_by o decreasing_by exact ((enumIsoToType o).symm x).prop @[simp] theorem toPGame_leftMoves (o : Ordinal) : o.toPGame.LeftMoves = o.toType := by rw [toPGame, LeftMoves] @[simp] theorem toPGame_rightMoves (o : Ordinal) : o.toPGame.RightMoves = PEmpty := by rw [toPGame, RightMoves] instance isEmpty_zero_toPGame_leftMoves : IsEmpty (toPGame 0).LeftMoves := by rw [toPGame_leftMoves]; infer_instance instance isEmpty_toPGame_rightMoves (o : Ordinal) : IsEmpty o.toPGame.RightMoves := by rw [toPGame_rightMoves]; infer_instance /-- Converts an ordinal less than `o` into a move for the `PGame` corresponding to `o`, and vice versa. -/ noncomputable def toLeftMovesToPGame {o : Ordinal} : Set.Iio o ≃ o.toPGame.LeftMoves := (enumIsoToType o).toEquiv.trans (Equiv.cast (toPGame_leftMoves o).symm) @[simp] theorem toLeftMovesToPGame_symm_lt {o : Ordinal} (i : o.toPGame.LeftMoves) : ↑(toLeftMovesToPGame.symm i) < o := (toLeftMovesToPGame.symm i).prop @[nolint unusedHavesSuffices] theorem toPGame_moveLeft_hEq {o : Ordinal} : o.toPGame.moveLeft ≍ fun x : o.toType => ((enumIsoToType o).symm x).val.toPGame := by rw [toPGame] rfl @[simp] theorem toPGame_moveLeft' {o : Ordinal} (i) : o.toPGame.moveLeft i = (toLeftMovesToPGame.symm i).val.toPGame := (congr_heq toPGame_moveLeft_hEq.symm (cast_heq _ i)).symm theorem toPGame_moveLeft {o : Ordinal} (i) : o.toPGame.moveLeft (toLeftMovesToPGame i) = i.val.toPGame := by simp /-- `0.toPGame` has the same moves as `0`. -/ noncomputable def zeroToPGameRelabelling : toPGame 0 ≡r 0 := Relabelling.isEmpty _ theorem toPGame_zero : toPGame 0 ≈ 0 := zeroToPGameRelabelling.equiv noncomputable instance uniqueOneToPGameLeftMoves : Unique (toPGame 1).LeftMoves := (Equiv.cast <| toPGame_leftMoves 1).unique @[simp] theorem one_toPGame_leftMoves_default_eq : (default : (toPGame 1).LeftMoves) = @toLeftMovesToPGame 1 ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := rfl @[simp] theorem to_leftMoves_one_toPGame_symm (i) : (@toLeftMovesToPGame 1).symm i = ⟨0, Set.mem_Iio.mpr zero_lt_one⟩ := by simp [eq_iff_true_of_subsingleton] theorem one_toPGame_moveLeft (x) : (toPGame 1).moveLeft x = toPGame 0 := by simp /-- `1.toPGame` has the same moves as `1`. -/ noncomputable def oneToPGameRelabelling : toPGame 1 ≡r 1 := ⟨Equiv.ofUnique _ _, Equiv.equivOfIsEmpty _ _, fun i => by simpa using zeroToPGameRelabelling, isEmptyElim⟩ theorem toPGame_one : toPGame 1 ≈ 1 := oneToPGameRelabelling.equiv theorem toPGame_lf {a b : Ordinal} (h : a < b) : a.toPGame ⧏ b.toPGame := by convert moveLeft_lf (toLeftMovesToPGame ⟨a, h⟩); rw [toPGame_moveLeft] theorem toPGame_le {a b : Ordinal} (h : a ≤ b) : a.toPGame ≤ b.toPGame := by refine le_iff_forall_lf.2 ⟨fun i => ?_, isEmptyElim⟩ rw [toPGame_moveLeft'] exact toPGame_lf ((toLeftMovesToPGame_symm_lt i).trans_le h) theorem toPGame_lt {a b : Ordinal} (h : a < b) : a.toPGame < b.toPGame := ⟨toPGame_le h.le, toPGame_lf h⟩ theorem toPGame_nonneg (a : Ordinal) : 0 ≤ a.toPGame := zeroToPGameRelabelling.ge.trans <| toPGame_le <| Ordinal.zero_le a @[simp] theorem toPGame_lf_iff {a b : Ordinal} : a.toPGame ⧏ b.toPGame ↔ a < b := ⟨by contrapose; rw [not_lt, not_lf]; exact toPGame_le, toPGame_lf⟩ @[simp] theorem toPGame_le_iff {a b : Ordinal} : a.toPGame ≤ b.toPGame ↔ a ≤ b := ⟨by contrapose; rw [not_le, PGame.not_le]; exact toPGame_lf, toPGame_le⟩ @[simp] theorem toPGame_lt_iff {a b : Ordinal} : a.toPGame < b.toPGame ↔ a < b := ⟨by contrapose; rw [not_lt]; exact fun h => not_lt_of_ge (toPGame_le h), toPGame_lt⟩ @[simp] theorem toPGame_equiv_iff {a b : Ordinal} : (a.toPGame ≈ b.toPGame) ↔ a = b := by -- Porting note: was `rw [PGame.Equiv]` change _ ≤ _ ∧ _ ≤ _ ↔ _ rw [le_antisymm_iff, toPGame_le_iff, toPGame_le_iff] theorem toPGame_injective : Function.Injective Ordinal.toPGame := fun _ _ h => toPGame_equiv_iff.1 <| equiv_of_eq h @[simp] theorem toPGame_inj {a b : Ordinal} : a.toPGame = b.toPGame ↔ a = b := toPGame_injective.eq_iff /-- The order embedding version of `toPGame`. -/ @[simps] noncomputable def toPGameEmbedding : Ordinal.{u} ↪o PGame.{u} where toFun := Ordinal.toPGame inj' := toPGame_injective map_rel_iff' := @toPGame_le_iff /-- Converts an ordinal into the corresponding game. -/ noncomputable def toGame : Ordinal.{u} ↪o Game.{u} where toFun o := ⟦o.toPGame⟧ inj' a b := by simpa [AntisymmRel] using le_antisymm map_rel_iff' := toPGame_le_iff @[simp] theorem mk_toPGame (o : Ordinal) : ⟦o.toPGame⟧ = o.toGame := rfl @[simp] theorem toGame_zero : toGame 0 = 0 := game_eq toPGame_zero @[simp] theorem toGame_one : toGame 1 = 1 := game_eq toPGame_one theorem toGame_injective : Function.Injective toGame := toGame.injective @[simp] theorem toGame_lf_iff {a b : Ordinal} : Game.LF a.toGame b.toGame ↔ a < b := toPGame_lf_iff theorem toGame_le_iff {a b : Ordinal} : a.toGame ≤ b.toGame ↔ a ≤ b := toPGame_le_iff theorem toGame_lt_iff {a b : Ordinal} : a.toGame < b.toGame ↔ a < b := toPGame_lt_iff theorem toGame_inj {a b : Ordinal} : a.toGame = b.toGame ↔ a = b := toGame.inj /-- The natural addition of ordinals corresponds to their sum as games. -/ theorem toPGame_nadd (a b : Ordinal) : (a ♯ b).toPGame ≈ a.toPGame + b.toPGame := by refine ⟨le_of_forall_lf (fun i => ?_) isEmptyElim, le_of_forall_lf (fun i => ?_) isEmptyElim⟩ · rw [toPGame_moveLeft'] rcases lt_nadd_iff.1 (toLeftMovesToPGame_symm_lt i) with (⟨c, hc, hc'⟩ | ⟨c, hc, hc'⟩) <;> rw [← toPGame_le_iff, le_congr_right (toPGame_nadd _ _)] at hc' <;> apply lf_of_le_of_lf hc' · apply add_lf_add_right rwa [toPGame_lf_iff] · apply add_lf_add_left rwa [toPGame_lf_iff] · apply leftMoves_add_cases i <;> intro i <;> let wf := toLeftMovesToPGame_symm_lt i <;> (try rw [add_moveLeft_inl]) <;> (try rw [add_moveLeft_inr]) <;> rw [toPGame_moveLeft', ← lf_congr_left (toPGame_nadd _ _), toPGame_lf_iff] · exact nadd_lt_nadd_right wf _ · exact nadd_lt_nadd_left wf _ termination_by (a, b) theorem toGame_nadd (a b : Ordinal) : (a ♯ b).toGame = a.toGame + b.toGame := game_eq (toPGame_nadd a b) /-- The natural multiplication of ordinals corresponds to their product as pre-games. -/ theorem toPGame_nmul (a b : Ordinal) : (a ⨳ b).toPGame ≈ a.toPGame * b.toPGame := by refine ⟨le_of_forall_lf (fun i => ?_) isEmptyElim, le_of_forall_lf (fun i => ?_) isEmptyElim⟩ · rw [toPGame_moveLeft'] rcases lt_nmul_iff.1 (toLeftMovesToPGame_symm_lt i) with ⟨c, hc, d, hd, h⟩ rw [← toPGame_le_iff, le_iff_game_le, mk_toPGame, mk_toPGame, toGame_nadd _ _, toGame_nadd _ _, ← le_sub_iff_add_le] at h refine lf_of_le_of_lf h <| (lf_congr_left ?_).1 <| moveLeft_lf <| toLeftMovesMul <| Sum.inl ⟨toLeftMovesToPGame ⟨c, hc⟩, toLeftMovesToPGame ⟨d, hd⟩⟩ simp only [mul_moveLeft_inl, toPGame_moveLeft', Equiv.symm_apply_apply, equiv_iff_game_eq, quot_sub, quot_add] repeat rw [← game_eq (toPGame_nmul _ _)] rfl · apply leftMoves_mul_cases i _ isEmptyElim intro i j rw [mul_moveLeft_inl, toPGame_moveLeft', toPGame_moveLeft', lf_iff_game_lf, quot_sub, quot_add, ← Game.not_le, le_sub_iff_add_le] repeat rw [← game_eq (toPGame_nmul _ _)] simp_rw [mk_toPGame, ← toGame_nadd] apply toPGame_lf (nmul_nadd_lt _ _) <;> exact toLeftMovesToPGame_symm_lt _ termination_by (a, b) theorem toGame_nmul (a b : Ordinal) : (a ⨳ b).toGame = ⟦a.toPGame * b.toPGame⟧ := game_eq (toPGame_nmul a b) @[simp] -- used to be a norm_cast lemma theorem toGame_natCast : ∀ n : ℕ, toGame n = n | 0 => Quot.sound (zeroToPGameRelabelling).equiv | n + 1 => by have : toGame 1 = 1 := Quot.sound oneToPGameRelabelling.equiv rw [Nat.cast_add, ← nadd_nat, toGame_nadd, toGame_natCast, Nat.cast_one, this] rfl theorem toPGame_natCast (n : ℕ) : toPGame n ≈ n := by rw [PGame.equiv_iff_game_eq, mk_toPGame, toGame_natCast, quot_natCast] end Ordinal
.lake/packages/mathlib/Mathlib/SetTheory/Game/State.lean
import Mathlib.SetTheory.Game.Short import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.ConcreteGame` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Games described via "the state of the board". We provide a simple mechanism for constructing combinatorial (pre-)games, by describing "the state of the board", and providing an upper bound on the number of turns remaining. ## Implementation notes We're very careful to produce a computable definition, so small games can be evaluated using `decide`. To achieve this, I've had to rely solely on induction on natural numbers: relying on general well-foundedness seems to be poisonous to computation? See `SetTheory/Game/Domineering` for an example using this construction. -/ universe u namespace SetTheory namespace PGame /-- `SetTheory.PGame.State S` describes how to interpret `s : S` as a state of a combinatorial game. Use `SetTheory.PGame.ofState s` or `SetTheory.Game.ofState s` to construct the game. `SetTheory.PGame.State.l : S → Finset S` and `SetTheory.PGame.State.r : S → Finset S` describe the states reachable by a move by Left or Right. `SetTheory.PGame.State.turnBound : S → ℕ` gives an upper bound on the number of possible turns remaining from this state. -/ class State (S : Type u) where /-- Upper bound on the number of possible turns remaining from this state -/ turnBound : S → ℕ /-- States reachable by a Left move -/ l : S → Finset S /-- States reachable by a Right move -/ r : S → Finset S left_bound : ∀ {s t : S}, t ∈ l s → turnBound t < turnBound s right_bound : ∀ {s t : S}, t ∈ r s → turnBound t < turnBound s open State variable {S : Type u} [State S] theorem turnBound_ne_zero_of_left_move {s t : S} (m : t ∈ l s) : turnBound s ≠ 0 := by intro h have t := left_bound m rw [h] at t exact Nat.not_succ_le_zero _ t theorem turnBound_ne_zero_of_right_move {s t : S} (m : t ∈ r s) : turnBound s ≠ 0 := by intro h have t := right_bound m cutsat theorem turnBound_of_left {s t : S} (m : t ∈ l s) (n : ℕ) (h : turnBound s ≤ n + 1) : turnBound t ≤ n := Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (left_bound m) h) theorem turnBound_of_right {s t : S} (m : t ∈ r s) (n : ℕ) (h : turnBound s ≤ n + 1) : turnBound t ≤ n := Nat.le_of_lt_succ (Nat.lt_of_lt_of_le (right_bound m) h) /-- Construct a `PGame` from a state and a (not necessarily optimal) bound on the number of turns remaining. -/ def ofStateAux : ∀ (n : ℕ) (s : S), turnBound s ≤ n → PGame | 0, s, h => PGame.mk { t // t ∈ l s } { t // t ∈ r s } (fun t => by exfalso; exact turnBound_ne_zero_of_left_move t.2 (nonpos_iff_eq_zero.mp h)) fun t => by exfalso; exact turnBound_ne_zero_of_right_move t.2 (nonpos_iff_eq_zero.mp h) | n + 1, s, h => PGame.mk { t // t ∈ l s } { t // t ∈ r s } (fun t => ofStateAux n t (turnBound_of_left t.2 n h)) fun t => ofStateAux n t (turnBound_of_right t.2 n h) /-- Two different (valid) turn bounds give equivalent games. -/ def ofStateAuxRelabelling : ∀ (s : S) (n m : ℕ) (hn : turnBound s ≤ n) (hm : turnBound s ≤ m), Relabelling (ofStateAux n s hn) (ofStateAux m s hm) | s, 0, 0, hn, hm => by dsimp [PGame.ofStateAux] fconstructor · rfl · rfl · intro i; dsimp at i; exfalso exact turnBound_ne_zero_of_left_move i.2 (nonpos_iff_eq_zero.mp hn) · intro j; dsimp at j; exfalso exact turnBound_ne_zero_of_right_move j.2 (nonpos_iff_eq_zero.mp hm) | s, 0, m + 1, hn, hm => by dsimp [PGame.ofStateAux] fconstructor · rfl · rfl · intro i; dsimp at i; exfalso exact turnBound_ne_zero_of_left_move i.2 (nonpos_iff_eq_zero.mp hn) · intro j; dsimp at j; exfalso exact turnBound_ne_zero_of_right_move j.2 (nonpos_iff_eq_zero.mp hn) | s, n + 1, 0, hn, hm => by dsimp [PGame.ofStateAux] fconstructor · rfl · rfl · intro i; dsimp at i; exfalso exact turnBound_ne_zero_of_left_move i.2 (nonpos_iff_eq_zero.mp hm) · intro j; dsimp at j; exfalso exact turnBound_ne_zero_of_right_move j.2 (nonpos_iff_eq_zero.mp hm) | s, n + 1, m + 1, hn, hm => by dsimp [PGame.ofStateAux] fconstructor · rfl · rfl · intro i apply ofStateAuxRelabelling · intro j apply ofStateAuxRelabelling /-- Construct a combinatorial `PGame` from a state. -/ def ofState (s : S) : PGame := ofStateAux (turnBound s) s (refl _) /-- The equivalence between `leftMoves` for a `PGame` constructed using `ofStateAux _ s _`, and `L s`. -/ def leftMovesOfStateAux (n : ℕ) {s : S} (h : turnBound s ≤ n) : LeftMoves (ofStateAux n s h) ≃ { t // t ∈ l s } := by induction n <;> rfl /-- The equivalence between `leftMoves` for a `PGame` constructed using `ofState s`, and `l s`. -/ def leftMovesOfState (s : S) : LeftMoves (ofState s) ≃ { t // t ∈ l s } := leftMovesOfStateAux _ _ /-- The equivalence between `rightMoves` for a `PGame` constructed using `ofStateAux _ s _`, and `R s`. -/ def rightMovesOfStateAux (n : ℕ) {s : S} (h : turnBound s ≤ n) : RightMoves (ofStateAux n s h) ≃ { t // t ∈ r s } := by induction n <;> rfl /-- The equivalence between `rightMoves` for a `PGame` constructed using `ofState s`, and `R s`. -/ def rightMovesOfState (s : S) : RightMoves (ofState s) ≃ { t // t ∈ r s } := rightMovesOfStateAux _ _ /-- The relabelling showing `moveLeft` applied to a game constructed using `ofStateAux` has itself been constructed using `ofStateAux`. -/ def relabellingMoveLeftAux (n : ℕ) {s : S} (h : turnBound s ≤ n) (t : LeftMoves (ofStateAux n s h)) : Relabelling (moveLeft (ofStateAux n s h) t) (ofStateAux (n - 1) ((leftMovesOfStateAux n h) t : S) (turnBound_of_left ((leftMovesOfStateAux n h) t).2 (n - 1) (Nat.le_trans h le_tsub_add))) := by induction n · have t' := (leftMovesOfStateAux 0 h) t exfalso; exact turnBound_ne_zero_of_left_move t'.2 (nonpos_iff_eq_zero.mp h) · rfl /-- The relabelling showing `moveLeft` applied to a game constructed using `of` has itself been constructed using `of`. -/ def relabellingMoveLeft (s : S) (t : LeftMoves (ofState s)) : Relabelling (moveLeft (ofState s) t) (ofState ((leftMovesOfState s).toFun t : S)) := by trans · apply relabellingMoveLeftAux · apply ofStateAuxRelabelling /-- The relabelling showing `moveRight` applied to a game constructed using `ofStateAux` has itself been constructed using `ofStateAux`. -/ def relabellingMoveRightAux (n : ℕ) {s : S} (h : turnBound s ≤ n) (t : RightMoves (ofStateAux n s h)) : Relabelling (moveRight (ofStateAux n s h) t) (ofStateAux (n - 1) ((rightMovesOfStateAux n h) t : S) (turnBound_of_right ((rightMovesOfStateAux n h) t).2 (n - 1) (Nat.le_trans h le_tsub_add))) := by induction n · have t' := (rightMovesOfStateAux 0 h) t exfalso; exact turnBound_ne_zero_of_right_move t'.2 (nonpos_iff_eq_zero.mp h) · rfl /-- The relabelling showing `moveRight` applied to a game constructed using `of` has itself been constructed using `of`. -/ def relabellingMoveRight (s : S) (t : RightMoves (ofState s)) : Relabelling (moveRight (ofState s) t) (ofState ((rightMovesOfState s).toFun t : S)) := by trans · apply relabellingMoveRightAux · apply ofStateAuxRelabelling instance fintypeLeftMovesOfStateAux (n : ℕ) (s : S) (h : turnBound s ≤ n) : Fintype (LeftMoves (ofStateAux n s h)) := by apply Fintype.ofEquiv _ (leftMovesOfStateAux _ _).symm instance fintypeRightMovesOfStateAux (n : ℕ) (s : S) (h : turnBound s ≤ n) : Fintype (RightMoves (ofStateAux n s h)) := by apply Fintype.ofEquiv _ (rightMovesOfStateAux _ _).symm instance shortOfStateAux : ∀ (n : ℕ) {s : S} (h : turnBound s ≤ n), Short (ofStateAux n s h) | 0, s, h => Short.mk' (fun i => by have i := (leftMovesOfStateAux _ _).toFun i exfalso exact turnBound_ne_zero_of_left_move i.2 (nonpos_iff_eq_zero.mp h)) fun j => by have j := (rightMovesOfStateAux _ _).toFun j exfalso exact turnBound_ne_zero_of_right_move j.2 (nonpos_iff_eq_zero.mp h) | n + 1, _, h => Short.mk' (fun i => shortOfRelabelling (relabellingMoveLeftAux (n + 1) h i).symm (shortOfStateAux n _)) fun j => shortOfRelabelling (relabellingMoveRightAux (n + 1) h j).symm (shortOfStateAux n _) instance shortOfState (s : S) : Short (ofState s) := by dsimp [PGame.ofState] infer_instance end PGame namespace Game /-- Construct a combinatorial `Game` from a state. -/ def ofState {S : Type u} [PGame.State S] (s : S) : Game := ⟦PGame.ofState s⟧ end Game end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Game/Birthday.lean
import Mathlib.Algebra.Order.Group.OrderIso import Mathlib.SetTheory.Game.Ordinal import Mathlib.SetTheory.Ordinal.NaturalOps import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.Birthday` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Birthdays of games There are two related but distinct notions of a birthday within combinatorial game theory. One is the birthday of a pre-game, which represents the "step" at which it is constructed. We define it recursively as the least ordinal larger than the birthdays of its left and right options. On the other hand, the birthday of a game is the smallest birthday among all pre-games that quotient to it. The birthday of a pre-game can be understood as representing the depth of its game tree. On the other hand, the birthday of a game more closely matches Conway's original description. The lemma `SetTheory.Game.birthday_eq_pGameBirthday` links both definitions together. ## Main declarations - `SetTheory.PGame.birthday`: The birthday of a pre-game. - `SetTheory.Game.birthday`: The birthday of a game. ## Todo - Characterize the birthdays of other basic arithmetical operations. -/ universe u open Ordinal namespace SetTheory open scoped NaturalOps PGame namespace PGame /-- The birthday of a pre-game is inductively defined as the least strict upper bound of the birthdays of its left and right games. It may be thought as the "step" in which a certain game is constructed. -/ noncomputable def birthday : PGame.{u} → Ordinal.{u} | ⟨_, _, xL, xR⟩ => max (lsub.{u, u} fun i => birthday (xL i)) (lsub.{u, u} fun i => birthday (xR i)) theorem birthday_def (x : PGame) : birthday x = max (lsub.{u, u} fun i => birthday (x.moveLeft i)) (lsub.{u, u} fun i => birthday (x.moveRight i)) := by cases x; rw [birthday]; rfl theorem birthday_moveLeft_lt {x : PGame} (i : x.LeftMoves) : (x.moveLeft i).birthday < x.birthday := by cases x; rw [birthday]; exact lt_max_of_lt_left (lt_lsub _ i) theorem birthday_moveRight_lt {x : PGame} (i : x.RightMoves) : (x.moveRight i).birthday < x.birthday := by cases x; rw [birthday]; exact lt_max_of_lt_right (lt_lsub _ i) theorem lt_birthday_iff {x : PGame} {o : Ordinal} : o < x.birthday ↔ (∃ i : x.LeftMoves, o ≤ (x.moveLeft i).birthday) ∨ ∃ i : x.RightMoves, o ≤ (x.moveRight i).birthday := by constructor · rw [birthday_def] intro h rcases lt_max_iff.1 h with h' | h' · left rwa [lt_lsub_iff] at h' · right rwa [lt_lsub_iff] at h' · rintro (⟨i, hi⟩ | ⟨i, hi⟩) · exact hi.trans_lt (birthday_moveLeft_lt i) · exact hi.trans_lt (birthday_moveRight_lt i) theorem Relabelling.birthday_congr : ∀ {x y : PGame.{u}}, x ≡r y → birthday x = birthday y | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩, r => by unfold birthday congr 1 all_goals apply lsub_eq_of_range_eq.{u, u, u} ext i; constructor all_goals rintro ⟨j, rfl⟩ · exact ⟨_, (r.moveLeft j).birthday_congr.symm⟩ · exact ⟨_, (r.moveLeftSymm j).birthday_congr⟩ · exact ⟨_, (r.moveRight j).birthday_congr.symm⟩ · exact ⟨_, (r.moveRightSymm j).birthday_congr⟩ @[simp] theorem birthday_eq_zero {x : PGame} : birthday x = 0 ↔ IsEmpty x.LeftMoves ∧ IsEmpty x.RightMoves := by rw [birthday_def, max_eq_zero, lsub_eq_zero_iff, lsub_eq_zero_iff] @[simp] theorem birthday_zero : birthday 0 = 0 := by simp [inferInstanceAs (IsEmpty PEmpty)] @[simp] theorem birthday_one : birthday 1 = 1 := by rw [birthday_def]; simp @[simp] theorem birthday_star : birthday star = 1 := by rw [birthday_def]; simp @[simp] theorem birthday_neg : ∀ x : PGame, (-x).birthday = x.birthday | ⟨xl, xr, xL, xR⟩ => by rw [birthday_def, birthday_def, max_comm] congr <;> funext <;> apply birthday_neg @[simp] theorem birthday_ordinalToPGame (o : Ordinal) : o.toPGame.birthday = o := by induction o using Ordinal.induction with | _ o IH rw [toPGame, PGame.birthday] simp only [lsub_empty, max_zero_right] conv_rhs => rw [← lsub_typein o] congr with x exact IH _ (typein_lt_self x) theorem le_birthday : ∀ x : PGame, x ≤ x.birthday.toPGame | ⟨xl, _, xL, _⟩ => le_def.2 ⟨fun i => Or.inl ⟨toLeftMovesToPGame ⟨_, birthday_moveLeft_lt i⟩, by simp [le_birthday (xL i)]⟩, isEmptyElim⟩ variable (x : PGame.{u}) theorem neg_birthday_le : -x.birthday.toPGame ≤ x := by simpa only [birthday_neg, ← neg_le_iff] using le_birthday (-x) @[simp] theorem birthday_add : ∀ x y : PGame.{u}, (x + y).birthday = x.birthday ♯ y.birthday | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩ => by rw [birthday_def, nadd, lsub_sum, lsub_sum] simp only [mk_add_moveLeft_inl, mk_add_moveLeft_inr, mk_add_moveRight_inl, mk_add_moveRight_inr, moveLeft_mk, moveRight_mk] conv_lhs => left; left; right; intro a; rw [birthday_add (xL a) ⟨yl, yr, yL, yR⟩] conv_lhs => left; right; right; intro b; rw [birthday_add ⟨xl, xr, xL, xR⟩ (yL b)] conv_lhs => right; left; right; intro a; rw [birthday_add (xR a) ⟨yl, yr, yL, yR⟩] conv_lhs => right; right; right; intro b; rw [birthday_add ⟨xl, xr, xL, xR⟩ (yR b)] rw [max_max_max_comm] congr <;> apply le_antisymm any_goals refine max_le_iff.2 ⟨?_, ?_⟩ all_goals refine lsub_le_iff.2 fun i ↦ ?_ rw [← Order.succ_le_iff] refine Ordinal.le_iSup (fun _ : Set.Iio _ ↦ _) ⟨_, ?_⟩ apply_rules [birthday_moveLeft_lt, birthday_moveRight_lt] all_goals rw [Ordinal.iSup_le_iff] rintro ⟨i, hi⟩ obtain ⟨j, hj⟩ | ⟨j, hj⟩ := lt_birthday_iff.1 hi <;> rw [Order.succ_le_iff] · exact lt_max_of_lt_left ((nadd_le_nadd_right hj _).trans_lt (lt_lsub _ _)) · exact lt_max_of_lt_right ((nadd_le_nadd_right hj _).trans_lt (lt_lsub _ _)) · exact lt_max_of_lt_left ((nadd_le_nadd_left hj _).trans_lt (lt_lsub _ _)) · exact lt_max_of_lt_right ((nadd_le_nadd_left hj _).trans_lt (lt_lsub _ _)) termination_by a b => (a, b) @[simp] theorem birthday_sub (x y : PGame) : (x - y).birthday = x.birthday ♯ y.birthday := by apply (birthday_add x _).trans rw [birthday_neg] @[simp] theorem birthday_natCast : ∀ n : ℕ, birthday n = n | 0 => birthday_zero | n + 1 => by simp [birthday_natCast] end PGame namespace Game /-- The birthday of a game is defined as the least birthday among all pre-games that define it. -/ noncomputable def birthday (x : Game.{u}) : Ordinal.{u} := sInf (PGame.birthday '' (Quotient.mk' ⁻¹' {x})) theorem birthday_eq_pGameBirthday (x : Game) : ∃ y : PGame.{u}, ⟦y⟧ = x ∧ y.birthday = birthday x := by refine csInf_mem (Set.image_nonempty.2 ?_) exact ⟨_, x.out_eq⟩ theorem birthday_quot_le_pGameBirthday (x : PGame) : birthday ⟦x⟧ ≤ x.birthday := csInf_le' ⟨x, rfl, rfl⟩ @[simp] theorem birthday_zero : birthday 0 = 0 := by rw [← Ordinal.le_zero, ← PGame.birthday_zero] exact birthday_quot_le_pGameBirthday _ @[simp] theorem birthday_eq_zero {x : Game} : birthday x = 0 ↔ x = 0 := by constructor · intro h let ⟨y, hy₁, hy₂⟩ := birthday_eq_pGameBirthday x rw [← hy₁] rw [h, PGame.birthday_eq_zero] at hy₂ exact PGame.game_eq (@PGame.Equiv.isEmpty _ hy₂.1 hy₂.2) · rintro rfl exact birthday_zero @[simp] theorem birthday_ordinalToGame (o : Ordinal) : birthday o.toGame = o := by apply le_antisymm · conv_rhs => rw [← PGame.birthday_ordinalToPGame o] apply birthday_quot_le_pGameBirthday · let ⟨x, hx₁, hx₂⟩ := birthday_eq_pGameBirthday o.toGame rw [← hx₂, ← toPGame_le_iff] rw [← mk_toPGame, ← PGame.equiv_iff_game_eq] at hx₁ exact hx₁.2.trans (PGame.le_birthday x) @[simp, norm_cast] theorem birthday_natCast (n : ℕ) : birthday n = n := by rw [← toGame_natCast] exact birthday_ordinalToGame _ @[simp] theorem birthday_ofNat (n : ℕ) [n.AtLeastTwo] : birthday ofNat(n) = OfNat.ofNat n := birthday_natCast n @[simp] theorem birthday_one : birthday 1 = 1 := by rw [← Nat.cast_one, birthday_natCast, Nat.cast_one] @[simp] theorem birthday_star : birthday ⟦PGame.star⟧ = 1 := by apply le_antisymm · rw [← PGame.birthday_star] exact birthday_quot_le_pGameBirthday _ · rw [Ordinal.one_le_iff_ne_zero, ne_eq, birthday_eq_zero, Game.zero_def, ← PGame.equiv_iff_game_eq] exact PGame.star_fuzzy_zero.not_equiv private theorem birthday_neg' (x : Game) : (-x).birthday ≤ x.birthday := by let ⟨y, hy₁, hy₂⟩ := birthday_eq_pGameBirthday x rw [← hy₂, ← PGame.birthday_neg y] conv_lhs => rw [← hy₁] apply birthday_quot_le_pGameBirthday @[simp] theorem birthday_neg (x : Game) : (-x).birthday = x.birthday := by apply le_antisymm (birthday_neg' x) conv_lhs => rw [← neg_neg x] exact birthday_neg' _ theorem le_birthday (x : Game) : x ≤ x.birthday.toGame := by let ⟨y, hy₁, hy₂⟩ := birthday_eq_pGameBirthday x rw [← hy₁] apply (y.le_birthday).trans rw [toPGame_le_iff, hy₁, hy₂] theorem neg_birthday_le (x : Game) : -x.birthday.toGame ≤ x := by rw [neg_le, ← birthday_neg] exact le_birthday _ theorem birthday_add_le (x y : Game) : (x + y).birthday ≤ x.birthday ♯ y.birthday := by let ⟨a, ha₁, ha₂⟩ := birthday_eq_pGameBirthday x let ⟨b, hb₁, hb₂⟩ := birthday_eq_pGameBirthday y rw [← ha₂, ← hb₂, ← ha₁, ← hb₁, ← PGame.birthday_add] exact birthday_quot_le_pGameBirthday _ theorem birthday_sub_le (x y : Game) : (x - y).birthday ≤ x.birthday ♯ y.birthday := by apply (birthday_add_le x _).trans_eq rw [birthday_neg] /- The bound `(x * y).birthday ≤ x.birthday ⨳ y.birthday` is currently an open problem. See https://mathoverflow.net/a/476829/147705. -/ /-- Games with bounded birthday are a small set. -/ theorem small_setOf_birthday_lt (o : Ordinal) : Small.{u} {x : Game.{u} // birthday x < o} := by induction o using Ordinal.induction with | h o IH => let S := ⋃ a ∈ Set.Iio o, {x : Game.{u} | birthday x < a} let H : Small.{u} S := @small_biUnion _ _ _ _ _ IH obtain rfl | ⟨a, rfl⟩ | ho := zero_or_succ_or_isSuccLimit o · simp_rw [Ordinal.not_lt_zero] exact small_empty · simp_rw [Order.lt_succ_iff, le_iff_lt_or_eq] convert small_union.{u} {x | birthday x < a} {x | birthday x = a} · exact IH _ (Order.lt_succ a) · let f (g : Set S × Set S) : Game := ⟦PGame.mk _ _ (fun x ↦ ((equivShrink g.1).symm x).1.1.out) (fun x ↦ ((equivShrink g.2).symm x).1.1.out)⟧ suffices {x | x.birthday = a} ⊆ Set.range f from small_subset this rintro x rfl obtain ⟨y, rfl, hy'⟩ := birthday_eq_pGameBirthday x refine ⟨⟨{z | ∃ i, ⟦y.moveLeft i⟧ = z.1}, {z | ∃ i, ⟦y.moveRight i⟧ = z.1}⟩, ?_⟩ apply PGame.game_eq <| PGame.Equiv.of_exists _ _ _ _ <;> intro i · obtain ⟨j, hj⟩ := ((equivShrink _).symm i).2 exact ⟨j, by simp [PGame.equiv_iff_game_eq, hj]⟩ · obtain ⟨j, hj⟩ := ((equivShrink _).symm i).2 exact ⟨j, by simp [PGame.equiv_iff_game_eq, hj]⟩ · refine ⟨equivShrink _ ⟨⟨⟦y.moveLeft i⟧, ?_⟩, i, rfl⟩, by simpa using Quotient.mk_out _⟩ suffices ∃ b ≤ y.birthday, birthday ⟦y.moveLeft i⟧ < b by simpa [S, hy'] using this refine ⟨_, le_rfl, ?_⟩ exact (birthday_quot_le_pGameBirthday _).trans_lt (PGame.birthday_moveLeft_lt i) · refine ⟨equivShrink _ ⟨⟨⟦y.moveRight i⟧, ?_⟩, i, rfl⟩, by simpa using Quotient.mk_out _⟩ suffices ∃ b ≤ y.birthday, birthday ⟦y.moveRight i⟧ < b by simpa [S, hy'] using this refine ⟨_, le_rfl, ?_⟩ exact (birthday_quot_le_pGameBirthday _).trans_lt (PGame.birthday_moveRight_lt i) · convert H change birthday _ < o ↔ ∃ a, _ simpa using ho.lt_iff_exists_lt end Game end SetTheory
.lake/packages/mathlib/Mathlib/SetTheory/Descriptive/Tree.lean
import Mathlib.Order.CompleteLattice.SetLike /-! # Trees in the sense of descriptive set theory This file defines trees of depth `ω` in the sense of descriptive set theory as sets of finite sequences that are stable under taking prefixes. ## Main declarations * `tree A`: a (possibly infinite) tree of depth at most `ω` with nodes in `A` -/ namespace Descriptive /-- A tree is a set of finite sequences, implemented as `List A`, that is stable under taking prefixes. For the definition we use the equivalent property `x ++ [a] ∈ T → x ∈ T`, which is more convenient to check. We define `tree A` as a complete sublattice of `Set (List A)`, which coerces to the type of trees on `A`. -/ def tree (A : Type*) : CompleteSublattice (Set (List A)) := CompleteSublattice.mk' {T | ∀ ⦃x : List A⦄ ⦃a : A⦄, x ++ [a] ∈ T → x ∈ T} (by rintro S hS x a ⟨t, ht, hx⟩; use t, ht, hS ht hx) (by rintro S hS x a h T hT; exact hS hT <| h T hT) @[simps!] instance (A : Type*) : SetLike (tree A) (List A) := SetLike.instSubtypeSet namespace Tree variable {A : Type*} {S T : tree A} lemma mem_of_append {x y : List A} (h : x ++ y ∈ T) : x ∈ T := by induction y generalizing x with | nil => simpa using h | cons y ys ih => exact T.prop (ih (by simpa)) lemma mem_of_prefix {x y : List A} (h' : x <+: y) (h : y ∈ T) : x ∈ T := by obtain ⟨_, rfl⟩ := h'; exact mem_of_append h instance : Trans List.IsPrefix (fun x (T : tree A) ↦ x ∈ T) (fun x T ↦ x ∈ T) where trans := mem_of_prefix lemma singleton_mem (T : tree A) {a : A} {x : List A} (h : a :: x ∈ T) : [a] ∈ T := mem_of_prefix ⟨x, rfl⟩ h @[simp] lemma tree_eq_bot : T = ⊥ ↔ [] ∉ T where mp := by rintro rfl; simp mpr h := by ext x; simpa using fun h' ↦ h <| mem_of_prefix x.nil_prefix h' lemma take_mem {n : ℕ} (x : T) : x.val.take n ∈ T := mem_of_prefix (x.val.take_prefix n) x.prop /-- A variant of `List.take` internally to a tree -/ @[simps] def take (n : ℕ) (x : T) : T := ⟨x.val.take n, take_mem x⟩ @[simp] lemma take_take (m n : ℕ) (x : T) : take m (take n x) = take (m ⊓ n) x := by simp [Subtype.ext_iff, List.take_take] @[simp] lemma take_eq_take {x : T} {m n : ℕ} : take m x = take n x ↔ m ⊓ x.val.length = n ⊓ x.val.length := by simp [Subtype.ext_iff] -- ### `subAt` variable (T) (x y : List A) /-- The residual tree obtained by regarding the node x as new root -/ def subAt : tree A := ⟨(x ++ ·)⁻¹' T, fun _ _ _ ↦ mem_of_append (by rwa [List.append_assoc])⟩ @[simp] lemma mem_subAt : y ∈ subAt T x ↔ x ++ y ∈ T := Iff.rfl @[simp] lemma subAt_nil : subAt T [] = T := rfl @[simp] lemma subAt_append : subAt (subAt T x) y = subAt T (x ++ y) := by ext; simp @[gcongr] lemma subAt_mono (h : S ≤ T) : subAt S x ≤ subAt T x := Set.preimage_mono h /-- A variant of `List.drop` that takes values in `subAt` -/ @[simps] def drop (n : ℕ) (x : T) : subAt T (Tree.take n x).val := ⟨x.val.drop n, by simp⟩ -- ### `pullSub` /-- Adjoint of `subAt`, given by pasting x before the root of T. Explicitly, elements are prefixes of x or x with an element of T appended -/ def pullSub : tree A where val := { y | y.take x.length <+: x ∧ y.drop x.length ∈ T } property := fun y a ⟨h1, h2⟩ ↦ ⟨((y.prefix_append [a]).take x.length).trans h1, mem_of_prefix ((y.prefix_append [a]).drop x.length) h2⟩ variable {T x y} lemma mem_pullSub_short (hl : y.length ≤ x.length) : y ∈ pullSub T x ↔ y <+: x ∧ [] ∈ T := by simp [pullSub, List.take_of_length_le hl, List.drop_eq_nil_iff.mpr hl] lemma mem_pullSub_long (hl : x.length ≤ y.length) : y ∈ pullSub T x ↔ ∃ z ∈ T, y = x ++ z where mp := by intro ⟨h1, h2⟩; use y.drop x.length, h2 nth_rw 1 [← List.take_append_drop x.length y] simpa [-List.take_append_drop, List.prefix_iff_eq_take, hl] using h1 mpr := by simp +contextual [pullSub] @[simp] lemma mem_pullSub_append : x ++ y ∈ pullSub T x ↔ y ∈ T := by simp [mem_pullSub_long] @[simp] lemma mem_pullSub_self : x ∈ pullSub T x ↔ [] ∈ T := by simpa using mem_pullSub_append (y := []) variable (T x y) lemma pullSub_subAt : pullSub (subAt T x) x ≤ T := by intro y (h : y ∈ pullSub _ x); rcases le_total y.length x.length with h' | h' · rw [mem_pullSub_short h'] at h; exact mem_of_prefix h.1 (by simpa using h.2) · rw [mem_pullSub_long h'] at h; obtain ⟨_, h, rfl⟩ := h; exact h @[simp] lemma subAt_pullSub : subAt (pullSub T x) x = T := by ext y; simp @[gcongr] lemma pullSub_mono (h : S ≤ T) x : pullSub S x ≤ pullSub T x := fun _ ⟨h1, h2⟩ ↦ ⟨h1, h h2⟩ lemma pullSub_adjunction (S T : tree A) (x : List A) : pullSub S x ≤ T ↔ S ≤ subAt T x where mp _ := by rw [← subAt_pullSub S x]; gcongr mpr _ := le_trans (by gcongr) (pullSub_subAt T x) @[simp] lemma pullSub_nil : pullSub T [] = T := by simp [pullSub] @[simp] lemma pullSub_append : pullSub (pullSub T y) x = pullSub T (x ++ y) := by ext z; rcases le_total x.length z.length with hl | hl · by_cases hp : x <+: z · obtain ⟨z, rfl⟩ := hp simp [pullSub, List.take_add] · constructor <;> intro ⟨h, _⟩ <;> [skip; replace h := by simpa [List.take_take] using h.take x.length] <;> cases hp <| List.prefix_iff_eq_take.mpr (h.eq_of_length (by simpa)).symm · rw [mem_pullSub_short hl, mem_pullSub_short (by simp), mem_pullSub_short (by simp; cutsat)] simpa using fun _ ↦ (z.isPrefix_append_of_length hl).symm end Descriptive.Tree
.lake/packages/mathlib/Mathlib/SetTheory/PGame/Order.lean
import Mathlib.Logic.Small.Defs import Mathlib.Order.GameAdd import Mathlib.SetTheory.PGame.Basic import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.IGame` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Order properties of pregames Pregames have both a `≤` and a `<` relation, satisfying the usual properties of a `Preorder`. The relation `0 < x` means that `x` can always be won by Left, while `0 ≤ x` means that `x` can be won by Left as the second player. It turns out to be quite convenient to define various relations on top of these. We define the "less or fuzzy" relation `x ⧏ y` as `¬ y ≤ x`, the equivalence relation `x ≈ y` as `x ≤ y ∧ y ≤ x`, and the fuzzy relation `x ‖ y` as `x ⧏ y ∧ y ⧏ x`. If `0 ⧏ x`, then `x` can be won by Left as the first player. If `x ≈ 0`, then `x` can be won by the second player. If `x ‖ 0`, then `x` can be won by the first player. Statements like `zero_le_lf`, `zero_lf_le`, etc. unfold these definitions. The theorems `le_def` and `lf_def` give a recursive characterisation of each relation in terms of themselves two moves later. The theorems `zero_le`, `zero_lf`, etc. also take into account that `0` has no moves. Later, games will be defined as the quotient by the `≈` relation; that is to say, the `Antisymmetrization` of `SetTheory.PGame`. -/ namespace SetTheory.PGame open Function Relation universe u /-- The less or equal relation on pre-games. If `0 ≤ x`, then Left can win `x` as the second player. `x ≤ y` means that `0 ≤ y - x`. See `PGame.le_iff_sub_nonneg`. -/ instance le : LE PGame := ⟨Sym2.GameAdd.fix wf_isOption fun x y le => (∀ i, ¬le y (x.moveLeft i) (Sym2.GameAdd.snd_fst <| IsOption.moveLeft i)) ∧ ∀ j, ¬le (y.moveRight j) x (Sym2.GameAdd.fst_snd <| IsOption.moveRight j)⟩ /-- The less or fuzzy relation on pre-games. `x ⧏ y` is defined as `¬ y ≤ x`. If `0 ⧏ x`, then Left can win `x` as the first player. `x ⧏ y` means that `0 ⧏ y - x`. See `PGame.lf_iff_sub_zero_lf`. -/ def LF (x y : PGame) : Prop := ¬y ≤ x @[inherit_doc] scoped infixl:50 " ⧏ " => PGame.LF @[simp] protected theorem not_le {x y : PGame} : ¬x ≤ y ↔ y ⧏ x := Iff.rfl @[simp] theorem not_lf {x y : PGame} : ¬x ⧏ y ↔ y ≤ x := Classical.not_not theorem _root_.LE.le.not_gf {x y : PGame} : x ≤ y → ¬y ⧏ x := not_lf.2 theorem LF.not_ge {x y : PGame} : x ⧏ y → ¬y ≤ x := id /-- Definition of `x ≤ y` on pre-games, in terms of `⧏`. The ordering here is chosen so that `And.left` refer to moves by Left, and `And.right` refer to moves by Right. -/ theorem le_iff_forall_lf {x y : PGame} : x ≤ y ↔ (∀ i, x.moveLeft i ⧏ y) ∧ ∀ j, x ⧏ y.moveRight j := by unfold LE.le le simp only rw [Sym2.GameAdd.fix_eq] rfl /-- Definition of `x ≤ y` on pre-games built using the constructor. -/ @[simp] theorem mk_le_mk {xl xr xL xR yl yr yL yR} : mk xl xr xL xR ≤ mk yl yr yL yR ↔ (∀ i, xL i ⧏ mk yl yr yL yR) ∧ ∀ j, mk xl xr xL xR ⧏ yR j := le_iff_forall_lf theorem le_of_forall_lf {x y : PGame} (h₁ : ∀ i, x.moveLeft i ⧏ y) (h₂ : ∀ j, x ⧏ y.moveRight j) : x ≤ y := le_iff_forall_lf.2 ⟨h₁, h₂⟩ /-- Definition of `x ⧏ y` on pre-games, in terms of `≤`. The ordering here is chosen so that `or.inl` refer to moves by Left, and `or.inr` refer to moves by Right. -/ theorem lf_iff_exists_le {x y : PGame} : x ⧏ y ↔ (∃ i, x ≤ y.moveLeft i) ∨ ∃ j, x.moveRight j ≤ y := by rw [LF, le_iff_forall_lf, not_and_or] simp /-- Definition of `x ⧏ y` on pre-games built using the constructor. -/ @[simp] theorem mk_lf_mk {xl xr xL xR yl yr yL yR} : mk xl xr xL xR ⧏ mk yl yr yL yR ↔ (∃ i, mk xl xr xL xR ≤ yL i) ∨ ∃ j, xR j ≤ mk yl yr yL yR := lf_iff_exists_le theorem le_or_gf (x y : PGame) : x ≤ y ∨ y ⧏ x := by rw [← PGame.not_le] apply em theorem moveLeft_lf_of_le {x y : PGame} (h : x ≤ y) (i) : x.moveLeft i ⧏ y := (le_iff_forall_lf.1 h).1 i alias _root_.LE.le.moveLeft_lf := moveLeft_lf_of_le theorem lf_moveRight_of_le {x y : PGame} (h : x ≤ y) (j) : x ⧏ y.moveRight j := (le_iff_forall_lf.1 h).2 j alias _root_.LE.le.lf_moveRight := lf_moveRight_of_le theorem lf_of_moveRight_le {x y : PGame} {j} (h : x.moveRight j ≤ y) : x ⧏ y := lf_iff_exists_le.2 <| Or.inr ⟨j, h⟩ theorem lf_of_le_moveLeft {x y : PGame} {i} (h : x ≤ y.moveLeft i) : x ⧏ y := lf_iff_exists_le.2 <| Or.inl ⟨i, h⟩ theorem lf_of_le_mk {xl xr xL xR y} : mk xl xr xL xR ≤ y → ∀ i, xL i ⧏ y := moveLeft_lf_of_le theorem lf_of_mk_le {x yl yr yL yR} : x ≤ mk yl yr yL yR → ∀ j, x ⧏ yR j := lf_moveRight_of_le theorem mk_lf_of_le {xl xr y j} (xL) {xR : xr → PGame} : xR j ≤ y → mk xl xr xL xR ⧏ y := @lf_of_moveRight_le (mk _ _ _ _) y j theorem lf_mk_of_le {x yl yr} {yL : yl → PGame} (yR) {i} : x ≤ yL i → x ⧏ mk yl yr yL yR := @lf_of_le_moveLeft x (mk _ _ _ _) i /- We prove that `x ≤ y → y ≤ z → x ≤ z` inductively, by also simultaneously proving its cyclic reorderings. This auxiliary lemma is used during said induction. -/ private theorem le_trans_aux {x y z : PGame} (h₁ : ∀ {i}, y ≤ z → z ≤ x.moveLeft i → y ≤ x.moveLeft i) (h₂ : ∀ {j}, z.moveRight j ≤ x → x ≤ y → z.moveRight j ≤ y) (hxy : x ≤ y) (hyz : y ≤ z) : x ≤ z := le_of_forall_lf (fun i => PGame.not_le.1 fun h => (h₁ hyz h).not_gf <| hxy.moveLeft_lf i) fun j => PGame.not_le.1 fun h => (h₂ h hxy).not_gf <| hyz.lf_moveRight j instance : Preorder PGame := { PGame.le with le_refl := fun x => by induction x with | mk _ _ _ _ IHl IHr => _ exact le_of_forall_lf (fun i => lf_of_le_moveLeft (IHl i)) fun i => lf_of_moveRight_le (IHr i) le_trans := by suffices ∀ {x y z : PGame}, (x ≤ y → y ≤ z → x ≤ z) ∧ (y ≤ z → z ≤ x → y ≤ x) ∧ (z ≤ x → x ≤ y → z ≤ y) from fun x y z => this.1 intro x y z induction x generalizing y z with | _ xl xr xL xR IHxl IHxr induction y generalizing z with | _ yl yr yL yR IHyl IHyr induction z with | _ zl zr zL zR IHzl IHzr exact ⟨le_trans_aux (fun {i} => (IHxl i).2.1) fun {j} => (IHzr j).2.2, le_trans_aux (fun {i} => (IHyl i).2.2) fun {j} => (IHxr j).1, le_trans_aux (fun {i} => (IHzl i).1) fun {j} => (IHyr j).2.1⟩ lt := fun x y => x ≤ y ∧ x ⧏ y } lemma Identical.le : ∀ {x y}, x ≡ y → x ≤ y | mk _ _ _ _, mk _ _ _ _, ⟨hL, hR⟩ => le_of_forall_lf (fun i ↦ let ⟨_, hj⟩ := hL.1 i; lf_of_le_moveLeft hj.le) (fun i ↦ let ⟨_, hj⟩ := hR.2 i; lf_of_moveRight_le hj.le) lemma Identical.ge {x y} (h : x ≡ y) : y ≤ x := h.symm.le theorem lt_iff_le_and_lf {x y : PGame} : x < y ↔ x ≤ y ∧ x ⧏ y := Iff.rfl theorem lt_of_le_of_lf {x y : PGame} (h₁ : x ≤ y) (h₂ : x ⧏ y) : x < y := ⟨h₁, h₂⟩ theorem lf_of_lt {x y : PGame} (h : x < y) : x ⧏ y := h.2 alias _root_.LT.lt.lf := lf_of_lt theorem lf_irrefl (x : PGame) : ¬x ⧏ x := le_rfl.not_gf instance : IsIrrefl _ (· ⧏ ·) := ⟨lf_irrefl⟩ protected theorem not_lt {x y : PGame} : ¬ x < y ↔ y ⧏ x ∨ y ≤ x := not_lt_iff_not_le_or_ge @[trans] theorem lf_of_le_of_lf {x y z : PGame} (h₁ : x ≤ y) (h₂ : y ⧏ z) : x ⧏ z := by rw [← PGame.not_le] at h₂ ⊢ exact fun h₃ => h₂ (h₃.trans h₁) instance : Trans (· ≤ ·) (· ⧏ ·) (· ⧏ ·) := ⟨lf_of_le_of_lf⟩ @[trans] theorem lf_of_lf_of_le {x y z : PGame} (h₁ : x ⧏ y) (h₂ : y ≤ z) : x ⧏ z := by rw [← PGame.not_le] at h₁ ⊢ exact fun h₃ => h₁ (h₂.trans h₃) instance : Trans (· ⧏ ·) (· ≤ ·) (· ⧏ ·) := ⟨lf_of_lf_of_le⟩ alias _root_.LE.le.trans_lf := lf_of_le_of_lf alias LF.trans_le := lf_of_lf_of_le @[trans] theorem lf_of_lt_of_lf {x y z : PGame} (h₁ : x < y) (h₂ : y ⧏ z) : x ⧏ z := h₁.le.trans_lf h₂ @[trans] theorem lf_of_lf_of_lt {x y z : PGame} (h₁ : x ⧏ y) (h₂ : y < z) : x ⧏ z := h₁.trans_le h₂.le alias _root_.LT.lt.trans_lf := lf_of_lt_of_lf alias LF.trans_lt := lf_of_lf_of_lt theorem moveLeft_lf {x : PGame} : ∀ i, x.moveLeft i ⧏ x := le_rfl.moveLeft_lf theorem lf_moveRight {x : PGame} : ∀ j, x ⧏ x.moveRight j := le_rfl.lf_moveRight theorem lf_mk {xl xr} (xL : xl → PGame) (xR : xr → PGame) (i) : xL i ⧏ mk xl xr xL xR := @moveLeft_lf (mk _ _ _ _) i theorem mk_lf {xl xr} (xL : xl → PGame) (xR : xr → PGame) (j) : mk xl xr xL xR ⧏ xR j := @lf_moveRight (mk _ _ _ _) j /-- This special case of `PGame.le_of_forall_lf` is useful when dealing with surreals, where `<` is preferred over `⧏`. -/ theorem le_of_forall_lt {x y : PGame} (h₁ : ∀ i, x.moveLeft i < y) (h₂ : ∀ j, x < y.moveRight j) : x ≤ y := le_of_forall_lf (fun i => (h₁ i).lf) fun i => (h₂ i).lf /-- The definition of `x ≤ y` on pre-games, in terms of `≤` two moves later. Note that it's often more convenient to use `le_iff_forall_lf`, which only unfolds the definition by one step. -/ theorem le_def {x y : PGame} : x ≤ y ↔ (∀ i, (∃ i', x.moveLeft i ≤ y.moveLeft i') ∨ ∃ j, (x.moveLeft i).moveRight j ≤ y) ∧ ∀ j, (∃ i, x ≤ (y.moveRight j).moveLeft i) ∨ ∃ j', x.moveRight j' ≤ y.moveRight j := by rw [le_iff_forall_lf] conv => lhs simp only [lf_iff_exists_le] /-- The definition of `x ⧏ y` on pre-games, in terms of `⧏` two moves later. Note that it's often more convenient to use `lf_iff_exists_le`, which only unfolds the definition by one step. -/ theorem lf_def {x y : PGame} : x ⧏ y ↔ (∃ i, (∀ i', x.moveLeft i' ⧏ y.moveLeft i) ∧ ∀ j, x ⧏ (y.moveLeft i).moveRight j) ∨ ∃ j, (∀ i, (x.moveRight j).moveLeft i ⧏ y) ∧ ∀ j', x.moveRight j ⧏ y.moveRight j' := by rw [lf_iff_exists_le] conv => lhs simp only [le_iff_forall_lf] /-- The definition of `0 ≤ x` on pre-games, in terms of `0 ⧏`. -/ theorem zero_le_lf {x : PGame} : 0 ≤ x ↔ ∀ j, 0 ⧏ x.moveRight j := by rw [le_iff_forall_lf] simp /-- The definition of `x ≤ 0` on pre-games, in terms of `⧏ 0`. -/ theorem le_zero_lf {x : PGame} : x ≤ 0 ↔ ∀ i, x.moveLeft i ⧏ 0 := by rw [le_iff_forall_lf] simp /-- The definition of `0 ⧏ x` on pre-games, in terms of `0 ≤`. -/ theorem zero_lf_le {x : PGame} : 0 ⧏ x ↔ ∃ i, 0 ≤ x.moveLeft i := by rw [lf_iff_exists_le] simp /-- The definition of `x ⧏ 0` on pre-games, in terms of `≤ 0`. -/ theorem lf_zero_le {x : PGame} : x ⧏ 0 ↔ ∃ j, x.moveRight j ≤ 0 := by rw [lf_iff_exists_le] simp /-- The definition of `0 ≤ x` on pre-games, in terms of `0 ≤` two moves later. -/ theorem zero_le {x : PGame} : 0 ≤ x ↔ ∀ j, ∃ i, 0 ≤ (x.moveRight j).moveLeft i := by rw [le_def] simp /-- The definition of `x ≤ 0` on pre-games, in terms of `≤ 0` two moves later. -/ theorem le_zero {x : PGame} : x ≤ 0 ↔ ∀ i, ∃ j, (x.moveLeft i).moveRight j ≤ 0 := by rw [le_def] simp /-- The definition of `0 ⧏ x` on pre-games, in terms of `0 ⧏` two moves later. -/ theorem zero_lf {x : PGame} : 0 ⧏ x ↔ ∃ i, ∀ j, 0 ⧏ (x.moveLeft i).moveRight j := by rw [lf_def] simp /-- The definition of `x ⧏ 0` on pre-games, in terms of `⧏ 0` two moves later. -/ theorem lf_zero {x : PGame} : x ⧏ 0 ↔ ∃ j, ∀ i, (x.moveRight j).moveLeft i ⧏ 0 := by rw [lf_def] simp @[simp] theorem zero_le_of_isEmpty_rightMoves (x : PGame) [IsEmpty x.RightMoves] : 0 ≤ x := zero_le.2 isEmptyElim @[simp] theorem le_zero_of_isEmpty_leftMoves (x : PGame) [IsEmpty x.LeftMoves] : x ≤ 0 := le_zero.2 isEmptyElim /-- Given a game won by the right player when they play second, provide a response to any move by left. -/ noncomputable def rightResponse {x : PGame} (h : x ≤ 0) (i : x.LeftMoves) : (x.moveLeft i).RightMoves := Classical.choose <| le_zero.1 h i /-- Show that the response for right provided by `rightResponse` preserves the right-player-wins condition. -/ theorem rightResponse_spec {x : PGame} (h : x ≤ 0) (i : x.LeftMoves) : (x.moveLeft i).moveRight (rightResponse h i) ≤ 0 := Classical.choose_spec <| le_zero.1 h i /-- Given a game won by the left player when they play second, provide a response to any move by right. -/ noncomputable def leftResponse {x : PGame} (h : 0 ≤ x) (j : x.RightMoves) : (x.moveRight j).LeftMoves := Classical.choose <| zero_le.1 h j /-- Show that the response for left provided by `leftResponse` preserves the left-player-wins condition. -/ theorem leftResponse_spec {x : PGame} (h : 0 ≤ x) (j : x.RightMoves) : 0 ≤ (x.moveRight j).moveLeft (leftResponse h j) := Classical.choose_spec <| zero_le.1 h j /-- A small family of pre-games is bounded above. -/ lemma bddAbove_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → PGame.{u}) : BddAbove (Set.range f) := by let x : PGame.{u} := ⟨Σ i, (f <| (equivShrink.{u} ι).symm i).LeftMoves, PEmpty, fun x ↦ moveLeft _ x.2, PEmpty.elim⟩ refine ⟨x, Set.forall_mem_range.2 fun i ↦ ?_⟩ rw [← (equivShrink ι).symm_apply_apply i, le_iff_forall_lf] simpa [x] using fun j ↦ @moveLeft_lf x ⟨equivShrink ι i, j⟩ /-- A small set of pre-games is bounded above. -/ lemma bddAbove_of_small (s : Set PGame.{u}) [Small.{u} s] : BddAbove s := by simpa using bddAbove_range_of_small (Subtype.val : s → PGame.{u}) /-- A small family of pre-games is bounded below. -/ lemma bddBelow_range_of_small {ι : Type*} [Small.{u} ι] (f : ι → PGame.{u}) : BddBelow (Set.range f) := by let x : PGame.{u} := ⟨PEmpty, Σ i, (f <| (equivShrink.{u} ι).symm i).RightMoves, PEmpty.elim, fun x ↦ moveRight _ x.2⟩ refine ⟨x, Set.forall_mem_range.2 fun i ↦ ?_⟩ rw [← (equivShrink ι).symm_apply_apply i, le_iff_forall_lf] simpa [x] using fun j ↦ @lf_moveRight x ⟨equivShrink ι i, j⟩ /-- A small set of pre-games is bounded below. -/ lemma bddBelow_of_small (s : Set PGame.{u}) [Small.{u} s] : BddBelow s := by simpa using bddBelow_range_of_small (Subtype.val : s → PGame.{u}) /-- The equivalence relation on pre-games. Two pre-games `x`, `y` are equivalent if `x ≤ y` and `y ≤ x`. If `x ≈ 0`, then the second player can always win `x`. -/ def Equiv (x y : PGame) : Prop := x ≤ y ∧ y ≤ x instance : IsEquiv _ PGame.Equiv where refl _ := ⟨le_rfl, le_rfl⟩ trans := fun _ _ _ ⟨xy, yx⟩ ⟨yz, zy⟩ => ⟨xy.trans yz, zy.trans yx⟩ symm _ _ := And.symm instance setoid : Setoid PGame := ⟨Equiv, refl, symm, Trans.trans⟩ theorem equiv_def {x y : PGame} : x ≈ y ↔ x ≤ y ∧ y ≤ x := Iff.rfl theorem Equiv.le {x y : PGame} (h : x ≈ y) : x ≤ y := h.1 theorem Equiv.ge {x y : PGame} (h : x ≈ y) : y ≤ x := h.2 theorem equiv_rfl {x : PGame} : x ≈ x := refl x theorem equiv_refl (x : PGame) : x ≈ x := refl x @[symm] protected theorem Equiv.symm {x y : PGame} : (x ≈ y) → (y ≈ x) := symm @[trans] protected theorem Equiv.trans {x y z : PGame} : (x ≈ y) → (y ≈ z) → (x ≈ z) := _root_.trans protected theorem equiv_comm {x y : PGame} : (x ≈ y) ↔ (y ≈ x) := comm theorem equiv_of_eq {x y : PGame} (h : x = y) : x ≈ y := by subst h; rfl lemma Identical.equiv {x y} (h : x ≡ y) : x ≈ y := ⟨h.le, h.ge⟩ @[trans] theorem le_of_le_of_equiv {x y z : PGame} (h₁ : x ≤ y) (h₂ : y ≈ z) : x ≤ z := h₁.trans h₂.1 instance : Trans ((· ≤ ·) : PGame → PGame → Prop) ((· ≈ ·) : PGame → PGame → Prop) ((· ≤ ·) : PGame → PGame → Prop) where trans := le_of_le_of_equiv @[trans] theorem le_of_equiv_of_le {x y z : PGame} (h₁ : x ≈ y) : y ≤ z → x ≤ z := h₁.1.trans instance : Trans ((· ≈ ·) : PGame → PGame → Prop) ((· ≤ ·) : PGame → PGame → Prop) ((· ≤ ·) : PGame → PGame → Prop) where trans := le_of_equiv_of_le theorem LF.not_equiv {x y : PGame} (h : x ⧏ y) : ¬(x ≈ y) := fun h' => h.not_ge h'.2 theorem LF.not_equiv' {x y : PGame} (h : x ⧏ y) : ¬(y ≈ x) := fun h' => h.not_ge h'.1 theorem LF.not_gt {x y : PGame} (h : x ⧏ y) : ¬y < x := fun h' => h.not_ge h'.le theorem le_congr_imp {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) (h : x₁ ≤ y₁) : x₂ ≤ y₂ := hx.2.trans (h.trans hy.1) theorem le_congr {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ ≤ y₁ ↔ x₂ ≤ y₂ := ⟨le_congr_imp hx hy, le_congr_imp (Equiv.symm hx) (Equiv.symm hy)⟩ theorem le_congr_left {x₁ x₂ y : PGame} (hx : x₁ ≈ x₂) : x₁ ≤ y ↔ x₂ ≤ y := le_congr hx equiv_rfl theorem le_congr_right {x y₁ y₂ : PGame} (hy : y₁ ≈ y₂) : x ≤ y₁ ↔ x ≤ y₂ := le_congr equiv_rfl hy theorem lf_congr {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ ⧏ y₁ ↔ x₂ ⧏ y₂ := PGame.not_le.symm.trans <| (not_congr (le_congr hy hx)).trans PGame.not_le theorem lf_congr_imp {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ ⧏ y₁ → x₂ ⧏ y₂ := (lf_congr hx hy).1 theorem lf_congr_left {x₁ x₂ y : PGame} (hx : x₁ ≈ x₂) : x₁ ⧏ y ↔ x₂ ⧏ y := lf_congr hx equiv_rfl theorem lf_congr_right {x y₁ y₂ : PGame} (hy : y₁ ≈ y₂) : x ⧏ y₁ ↔ x ⧏ y₂ := lf_congr equiv_rfl hy @[trans] theorem lf_of_lf_of_equiv {x y z : PGame} (h₁ : x ⧏ y) (h₂ : y ≈ z) : x ⧏ z := lf_congr_imp equiv_rfl h₂ h₁ instance : Trans (· ⧏ ·) (· ≈ ·) (· ⧏ ·) := ⟨lf_of_lf_of_equiv⟩ @[trans] theorem lf_of_equiv_of_lf {x y z : PGame} (h₁ : x ≈ y) : y ⧏ z → x ⧏ z := lf_congr_imp (Equiv.symm h₁) equiv_rfl instance : Trans (· ≈ ·) (· ⧏ ·) (· ⧏ ·) := ⟨lf_of_equiv_of_lf⟩ @[trans] theorem lt_of_lt_of_equiv {x y z : PGame} (h₁ : x < y) (h₂ : y ≈ z) : x < z := h₁.trans_le h₂.1 instance : Trans ((· < ·) : PGame → PGame → Prop) ((· ≈ ·) : PGame → PGame → Prop) ((· < ·) : PGame → PGame → Prop) where trans := lt_of_lt_of_equiv @[trans] theorem lt_of_equiv_of_lt {x y z : PGame} (h₁ : x ≈ y) : y < z → x < z := h₁.1.trans_lt instance : Trans ((· ≈ ·) : PGame → PGame → Prop) ((· < ·) : PGame → PGame → Prop) ((· < ·) : PGame → PGame → Prop) where trans := lt_of_equiv_of_lt theorem lt_congr_imp {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) (h : x₁ < y₁) : x₂ < y₂ := hx.2.trans_lt (h.trans_le hy.1) theorem lt_congr {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ < y₁ ↔ x₂ < y₂ := ⟨lt_congr_imp hx hy, lt_congr_imp (Equiv.symm hx) (Equiv.symm hy)⟩ theorem lt_congr_left {x₁ x₂ y : PGame} (hx : x₁ ≈ x₂) : x₁ < y ↔ x₂ < y := lt_congr hx equiv_rfl theorem lt_congr_right {x y₁ y₂ : PGame} (hy : y₁ ≈ y₂) : x < y₁ ↔ x < y₂ := lt_congr equiv_rfl hy theorem lt_or_equiv_of_le {x y : PGame} (h : x ≤ y) : x < y ∨ (x ≈ y) := and_or_left.mp ⟨h, (em <| y ≤ x).symm.imp_left PGame.not_le.1⟩ theorem lf_or_equiv_or_gf (x y : PGame) : x ⧏ y ∨ (x ≈ y) ∨ y ⧏ x := by rw [or_iff_not_imp_left] intro h rcases lt_or_equiv_of_le (PGame.not_lf.1 h) with h' | h' · exact Or.inr h'.lf · exact Or.inl (Equiv.symm h') theorem equiv_congr_left {y₁ y₂ : PGame} : (y₁ ≈ y₂) ↔ ∀ x₁, (x₁ ≈ y₁) ↔ (x₁ ≈ y₂) := ⟨fun h _ => ⟨fun h' => Equiv.trans h' h, fun h' => Equiv.trans h' (Equiv.symm h)⟩, fun h => (h y₁).1 <| equiv_rfl⟩ theorem equiv_congr_right {x₁ x₂ : PGame} : (x₁ ≈ x₂) ↔ ∀ y₁, (x₁ ≈ y₁) ↔ (x₂ ≈ y₁) := ⟨fun h _ => ⟨fun h' => Equiv.trans (Equiv.symm h) h', fun h' => Equiv.trans h h'⟩, fun h => (h x₂).2 <| equiv_rfl⟩ theorem Equiv.of_exists {x y : PGame} (hl₁ : ∀ i, ∃ j, x.moveLeft i ≈ y.moveLeft j) (hr₁ : ∀ i, ∃ j, x.moveRight i ≈ y.moveRight j) (hl₂ : ∀ j, ∃ i, x.moveLeft i ≈ y.moveLeft j) (hr₂ : ∀ j, ∃ i, x.moveRight i ≈ y.moveRight j) : x ≈ y := by constructor <;> refine le_def.2 ⟨?_, ?_⟩ <;> intro i · obtain ⟨j, hj⟩ := hl₁ i exact Or.inl ⟨j, Equiv.le hj⟩ · obtain ⟨j, hj⟩ := hr₂ i exact Or.inr ⟨j, Equiv.le hj⟩ · obtain ⟨j, hj⟩ := hl₂ i exact Or.inl ⟨j, Equiv.ge hj⟩ · obtain ⟨j, hj⟩ := hr₁ i exact Or.inr ⟨j, Equiv.ge hj⟩ theorem Equiv.of_equiv {x y : PGame} (L : x.LeftMoves ≃ y.LeftMoves) (R : x.RightMoves ≃ y.RightMoves) (hl : ∀ i, x.moveLeft i ≈ y.moveLeft (L i)) (hr : ∀ j, x.moveRight j ≈ y.moveRight (R j)) : x ≈ y := by apply Equiv.of_exists <;> intro i exacts [⟨_, hl i⟩, ⟨_, hr i⟩, ⟨_, by simpa using hl (L.symm i)⟩, ⟨_, by simpa using hr (R.symm i)⟩] /-- The fuzzy, confused, or incomparable relation on pre-games. If `x ‖ 0`, then the first player can always win `x`. -/ def Fuzzy (x y : PGame) : Prop := x ⧏ y ∧ y ⧏ x @[inherit_doc] scoped infixl:50 " ‖ " => PGame.Fuzzy @[symm] theorem Fuzzy.swap {x y : PGame} : x ‖ y → y ‖ x := And.symm instance : IsSymm _ (· ‖ ·) := ⟨fun _ _ => Fuzzy.swap⟩ theorem Fuzzy.swap_iff {x y : PGame} : x ‖ y ↔ y ‖ x := ⟨Fuzzy.swap, Fuzzy.swap⟩ theorem fuzzy_irrefl (x : PGame) : ¬x ‖ x := fun h => lf_irrefl x h.1 instance : IsIrrefl _ (· ‖ ·) := ⟨fuzzy_irrefl⟩ theorem lf_iff_lt_or_fuzzy {x y : PGame} : x ⧏ y ↔ x < y ∨ x ‖ y := by simp only [lt_iff_le_and_lf, Fuzzy, ← PGame.not_le] tauto theorem lf_of_fuzzy {x y : PGame} (h : x ‖ y) : x ⧏ y := lf_iff_lt_or_fuzzy.2 (Or.inr h) alias Fuzzy.lf := lf_of_fuzzy theorem lt_or_fuzzy_of_lf {x y : PGame} : x ⧏ y → x < y ∨ x ‖ y := lf_iff_lt_or_fuzzy.1 theorem Fuzzy.not_equiv {x y : PGame} (h : x ‖ y) : ¬(x ≈ y) := fun h' => h'.1.not_gf h.2 theorem Fuzzy.not_equiv' {x y : PGame} (h : x ‖ y) : ¬(y ≈ x) := fun h' => h'.2.not_gf h.2 theorem not_fuzzy_of_le {x y : PGame} (h : x ≤ y) : ¬x ‖ y := fun h' => h'.2.not_ge h theorem not_fuzzy_of_ge {x y : PGame} (h : y ≤ x) : ¬x ‖ y := fun h' => h'.1.not_ge h theorem Equiv.not_fuzzy {x y : PGame} (h : x ≈ y) : ¬x ‖ y := not_fuzzy_of_le h.1 theorem Equiv.not_fuzzy' {x y : PGame} (h : x ≈ y) : ¬y ‖ x := not_fuzzy_of_le h.2 theorem fuzzy_congr {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ ‖ y₁ ↔ x₂ ‖ y₂ := show _ ∧ _ ↔ _ ∧ _ by rw [lf_congr hx hy, lf_congr hy hx] theorem fuzzy_congr_imp {x₁ y₁ x₂ y₂ : PGame} (hx : x₁ ≈ x₂) (hy : y₁ ≈ y₂) : x₁ ‖ y₁ → x₂ ‖ y₂ := (fuzzy_congr hx hy).1 theorem fuzzy_congr_left {x₁ x₂ y : PGame} (hx : x₁ ≈ x₂) : x₁ ‖ y ↔ x₂ ‖ y := fuzzy_congr hx equiv_rfl theorem fuzzy_congr_right {x y₁ y₂ : PGame} (hy : y₁ ≈ y₂) : x ‖ y₁ ↔ x ‖ y₂ := fuzzy_congr equiv_rfl hy @[trans] theorem fuzzy_of_fuzzy_of_equiv {x y z : PGame} (h₁ : x ‖ y) (h₂ : y ≈ z) : x ‖ z := (fuzzy_congr_right h₂).1 h₁ @[trans] theorem fuzzy_of_equiv_of_fuzzy {x y z : PGame} (h₁ : x ≈ y) (h₂ : y ‖ z) : x ‖ z := (fuzzy_congr_left h₁).2 h₂ /-- Exactly one of the following is true (although we don't prove this here). -/ theorem lt_or_equiv_or_gt_or_fuzzy (x y : PGame) : x < y ∨ (x ≈ y) ∨ y < x ∨ x ‖ y := by rcases le_or_gf x y with h₁ | h₁ <;> rcases le_or_gf y x with h₂ | h₂ · right left exact ⟨h₁, h₂⟩ · left exact ⟨h₁, h₂⟩ · right right left exact ⟨h₂, h₁⟩ · right right right exact ⟨h₂, h₁⟩ theorem lt_or_equiv_or_gf (x y : PGame) : x < y ∨ (x ≈ y) ∨ y ⧏ x := by rw [lf_iff_lt_or_fuzzy, Fuzzy.swap_iff] exact lt_or_equiv_or_gt_or_fuzzy x y /-! ### Interaction of relabelling with order -/ theorem Relabelling.le {x y : PGame} (r : x ≡r y) : x ≤ y := le_def.2 ⟨fun i => Or.inl ⟨_, (r.moveLeft i).le⟩, fun j => Or.inr ⟨_, (r.moveRightSymm j).le⟩⟩ termination_by x theorem Relabelling.ge {x y : PGame} (r : x ≡r y) : y ≤ x := r.symm.le /-- A relabelling lets us prove equivalence of games. -/ theorem Relabelling.equiv {x y : PGame} (r : x ≡r y) : x ≈ y := ⟨r.le, r.ge⟩ theorem Equiv.isEmpty (x : PGame) [IsEmpty x.LeftMoves] [IsEmpty x.RightMoves] : x ≈ 0 := (Relabelling.isEmpty x).equiv instance {x y : PGame} : Coe (x ≡r y) (x ≈ y) := ⟨Relabelling.equiv⟩ /-! ### Interaction of option insertion with order -/ /-- A new left option cannot hurt Left. -/ lemma le_insertLeft (x x' : PGame) : x ≤ insertLeft x x' := by rw [le_def] constructor · intro i left rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertLeft, leftMoves_mk, moveLeft_mk, Sum.exists, Sum.elim_inl] left use i · intro j right rcases x with ⟨xl, xr, xL, xR⟩ simp only [rightMoves_mk, moveRight_mk, insertLeft] use j /-- A new right option cannot hurt Right. -/ lemma insertRight_le (x x' : PGame) : insertRight x x' ≤ x := by rw [le_def] constructor · intro j left rcases x with ⟨xl, xr, xL, xR⟩ simp only [leftMoves_mk, moveLeft_mk, insertRight] use j · intro i right rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertRight, rightMoves_mk, moveRight_mk, Sum.exists, Sum.elim_inl] left use i /-- Adding a gift horse left option does not change the value of `x`. A gift horse left option is a game `x'` with `x' ⧏ x`. It is called "gift horse" because it seems like Left has gotten the "gift" of a new option, but actually the value of the game did not change. -/ lemma insertLeft_equiv_of_lf {x x' : PGame} (h : x' ⧏ x) : insertLeft x x' ≈ x := by rw [equiv_def] constructor · rw [le_def] constructor · intro i rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertLeft, moveLeft_mk] at i ⊢ rcases i with i | _ · rw [Sum.elim_inl] left use i · rw [Sum.elim_inr] simpa only [lf_iff_exists_le] using h · intro j right rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertLeft, moveRight_mk] use j · apply le_insertLeft /-- Adding a gift horse right option does not change the value of `x`. A gift horse right option is a game `x'` with `x ⧏ x'`. It is called "gift horse" because it seems like Right has gotten the "gift" of a new option, but actually the value of the game did not change. -/ lemma insertRight_equiv_of_lf {x x' : PGame} (h : x ⧏ x') : insertRight x x' ≈ x := by rw [equiv_def] constructor · apply insertRight_le · rw [le_def] constructor · intro j left rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertRight, moveLeft_mk] use j · intro i rcases x with ⟨xl, xr, xL, xR⟩ simp only [insertRight, moveRight_mk] at i ⊢ rcases i with i | _ · rw [Sum.elim_inl] right use i · rw [Sum.elim_inr] simpa only [lf_iff_exists_le] using h end SetTheory.PGame
.lake/packages/mathlib/Mathlib/SetTheory/PGame/Basic.lean
import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.Convert import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.IGame` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Combinatorial pregames This is the first in a series of files developing the basic theory of combinatorial games, following Conway's book "On Numbers and Games": * this file defines _pregames_ and elementary operations on them (relabelling, option insertion) * `Mathlib/SetTheory/PGame/Order.lean` defines an ordering on pregames * `Mathlib/SetTheory/PGame/Algebra.lean` defines an `AddCommGroup` structure on pregames * `Mathlib/SetTheory/Game/Basic.lean` defines games as the quotient of pregames by the equivalence relation `p ≈ q ↔ p ≤ q ∧ q ≤ p`. The surreal numbers will be built as a quotient of a subtype of pregames. A pregame (`SetTheory.PGame` below) is axiomatised via an inductive type, whose sole constructor takes two types (thought of as indexing the possible moves for the players Left and Right), and a pair of functions out of these types to `SetTheory.PGame` (thought of as describing the resulting game after making a move). We may denote a game as $\{L | R\}$, where $L$ and $R$ stand for the collections of left and right moves. This notation is not currently used in Mathlib. ## Conway induction By construction, the induction principle for pregames is exactly "Conway induction". That is, to prove some predicate `SetTheory.PGame → Prop` holds for all pregames, it suffices to prove that for every pregame `g`, if the predicate holds for every game resulting from making a move, then it also holds for `g`. While it is often convenient to work "by induction" on pregames, in some situations this becomes awkward, so we also define accessor functions `SetTheory.PGame.LeftMoves`, `SetTheory.PGame.RightMoves`, `SetTheory.PGame.moveLeft` and `SetTheory.PGame.moveRight`. There is a relation `PGame.Subsequent p q`, saying that `p` can be reached by playing some non-empty sequence of moves starting from `q`, an instance `WellFounded Subsequent`, and a local tactic `pgame_wf_tac` which is helpful for discharging proof obligations in inductive proofs relying on this relation. ## Future work * The theory of dominated and reversible positions, and unique normal form for short games. * Analysis of basic domineering positions. * Hex. * Temperature. * The development of surreal numbers, based on this development of combinatorial games, is still quite incomplete. ## References The material here is all drawn from * [Conway, *On numbers and games*][conway2001] An interested reader may like to formalise some of the material from * [Andreas Blass, *A game semantics for linear logic*][MR1167694] * [André Joyal, *Remarques sur la théorie des jeux à deux personnes*][joyal1977] -/ namespace SetTheory open Function Relation /-! ### Pre-game moves -/ universe u /-- The type of pre-games, before we have quotiented by equivalence (`PGame.setoid`). In ZFC, a combinatorial game is constructed from two sets of combinatorial games that have been constructed at an earlier stage. To do this in type theory, we say that a pre-game is built inductively from two families of pre-games indexed over any type in Type u. The resulting type `PGame.{u}` lives in `Type (u+1)`, reflecting that it is a proper class in ZFC. -/ inductive PGame : Type (u + 1) | mk : ∀ α β : Type u, (α → PGame) → (β → PGame) → PGame compile_inductive% PGame namespace PGame /-- The indexing type for allowable moves by Left. -/ def LeftMoves : PGame → Type u | mk l _ _ _ => l /-- The indexing type for allowable moves by Right. -/ def RightMoves : PGame → Type u | mk _ r _ _ => r /-- The new game after Left makes an allowed move. -/ def moveLeft : ∀ g : PGame, LeftMoves g → PGame | mk _l _ L _ => L /-- The new game after Right makes an allowed move. -/ def moveRight : ∀ g : PGame, RightMoves g → PGame | mk _ _r _ R => R @[simp] theorem leftMoves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : PGame).LeftMoves = xl := rfl @[simp] theorem moveLeft_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : PGame).moveLeft = xL := rfl @[simp] theorem rightMoves_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : PGame).RightMoves = xr := rfl @[simp] theorem moveRight_mk {xl xr xL xR} : (⟨xl, xr, xL, xR⟩ : PGame).moveRight = xR := rfl lemma ext {x y : PGame} (hl : x.LeftMoves = y.LeftMoves) (hr : x.RightMoves = y.RightMoves) (hL : ∀ i j, i ≍ j → x.moveLeft i = y.moveLeft j) (hR : ∀ i j, i ≍ j → x.moveRight i = y.moveRight j) : x = y := by cases x cases y subst hl hr simp only [leftMoves_mk, rightMoves_mk, heq_eq_eq, forall_eq', mk.injEq, true_and] at * exact ⟨funext hL, funext hR⟩ -- TODO define this at the level of games, as well, and perhaps also for finsets of games. /-- Construct a pre-game from list of pre-games describing the available moves for Left and Right. -/ def ofLists (L R : List PGame.{u}) : PGame.{u} := mk (ULift (Fin L.length)) (ULift (Fin R.length)) (fun i => L[i.down.1]) fun j ↦ R[j.down.1] theorem leftMoves_ofLists (L R : List PGame) : (ofLists L R).LeftMoves = ULift (Fin L.length) := rfl theorem rightMoves_ofLists (L R : List PGame) : (ofLists L R).RightMoves = ULift (Fin R.length) := rfl /-- Converts a number into a left move for `ofLists`. This is just an abbreviation for `Equiv.ulift.symm` -/ abbrev toOfListsLeftMoves {L R : List PGame} : Fin L.length ≃ (ofLists L R).LeftMoves := Equiv.ulift.symm /-- Converts a number into a right move for `ofLists`. This is just an abbreviation for `Equiv.ulift.symm` -/ abbrev toOfListsRightMoves {L R : List PGame} : Fin R.length ≃ (ofLists L R).RightMoves := Equiv.ulift.symm @[simp] theorem ofLists_moveLeft' {L R : List PGame} (i : (ofLists L R).LeftMoves) : (ofLists L R).moveLeft i = L[i.down.val] := rfl theorem ofLists_moveLeft {L R : List PGame} (i : Fin L.length) : (ofLists L R).moveLeft (ULift.up i) = L[i] := rfl @[simp] theorem ofLists_moveRight' {L R : List PGame} (i : (ofLists L R).RightMoves) : (ofLists L R).moveRight i = R[i.down.val] := rfl theorem ofLists_moveRight {L R : List PGame} (i : Fin R.length) : (ofLists L R).moveRight (ULift.up i) = R[i] := rfl /-- A variant of `PGame.recOn` expressed in terms of `PGame.moveLeft` and `PGame.moveRight`. Both this and `PGame.recOn` describe Conway induction on games. -/ @[elab_as_elim] def moveRecOn {C : PGame → Sort*} (x : PGame) (IH : ∀ y : PGame, (∀ i, C (y.moveLeft i)) → (∀ j, C (y.moveRight j)) → C y) : C x := x.recOn fun yl yr yL yR => IH (mk yl yr yL yR) /-- `IsOption x y` means that `x` is either a left or right option for `y`. -/ @[mk_iff] inductive IsOption : PGame → PGame → Prop | moveLeft {x : PGame} (i : x.LeftMoves) : IsOption (x.moveLeft i) x | moveRight {x : PGame} (i : x.RightMoves) : IsOption (x.moveRight i) x theorem IsOption.mk_left {xl xr : Type u} (xL : xl → PGame) (xR : xr → PGame) (i : xl) : (xL i).IsOption (mk xl xr xL xR) := @IsOption.moveLeft (mk _ _ _ _) i theorem IsOption.mk_right {xl xr : Type u} (xL : xl → PGame) (xR : xr → PGame) (i : xr) : (xR i).IsOption (mk xl xr xL xR) := @IsOption.moveRight (mk _ _ _ _) i theorem wf_isOption : WellFounded IsOption := ⟨fun x => moveRecOn x fun x IHl IHr => Acc.intro x fun y h => by induction h with | moveLeft i => exact IHl i | moveRight j => exact IHr j⟩ /-- `Subsequent x y` says that `x` can be obtained by playing some nonempty sequence of moves from `y`. It is the transitive closure of `IsOption`. -/ def Subsequent : PGame → PGame → Prop := TransGen IsOption instance : IsTrans _ Subsequent := inferInstanceAs <| IsTrans _ (TransGen _) @[trans] theorem Subsequent.trans {x y z} : Subsequent x y → Subsequent y z → Subsequent x z := TransGen.trans theorem wf_subsequent : WellFounded Subsequent := wf_isOption.transGen instance : WellFoundedRelation PGame := ⟨_, wf_subsequent⟩ @[simp] theorem Subsequent.moveLeft {x : PGame} (i : x.LeftMoves) : Subsequent (x.moveLeft i) x := TransGen.single (IsOption.moveLeft i) @[simp] theorem Subsequent.moveRight {x : PGame} (j : x.RightMoves) : Subsequent (x.moveRight j) x := TransGen.single (IsOption.moveRight j) @[simp] theorem Subsequent.mk_left {xl xr} (xL : xl → PGame) (xR : xr → PGame) (i : xl) : Subsequent (xL i) (mk xl xr xL xR) := @Subsequent.moveLeft (mk _ _ _ _) i @[simp] theorem Subsequent.mk_right {xl xr} (xL : xl → PGame) (xR : xr → PGame) (j : xr) : Subsequent (xR j) (mk xl xr xL xR) := @Subsequent.moveRight (mk _ _ _ _) j /-- Discharges proof obligations of the form `⊢ Subsequent ..` arising in termination proofs of definitions using well-founded recursion on `PGame`. -/ macro "pgame_wf_tac" : tactic => `(tactic| solve_by_elim (config := { maxDepth := 8 }) [Prod.Lex.left, Prod.Lex.right, PSigma.Lex.left, PSigma.Lex.right, Subsequent.moveLeft, Subsequent.moveRight, Subsequent.mk_left, Subsequent.mk_right, Subsequent.trans]) -- Register some consequences of pgame_wf_tac as simp-lemmas for convenience -- (which are applied by default for WF goals) variable {xl xr : Type u} -- This is different from mk_right from the POV of the simplifier, -- because the unifier can't solve `xr =?= RightMoves (mk xl xr xL xR)` at reducible transparency. @[simp] theorem Subsequent.mk_right' (xL : xl → PGame) (xR : xr → PGame) (j : RightMoves (mk xl xr xL xR)) : Subsequent (xR j) (mk xl xr xL xR) := by pgame_wf_tac @[simp] theorem Subsequent.moveRight_mk_left {xR : xr → PGame} {i : xl} (xL : xl → PGame) (j) : Subsequent ((xL i).moveRight j) (mk xl xr xL xR) := by pgame_wf_tac @[simp] theorem Subsequent.moveRight_mk_right {xL : xl → PGame} {i : xr} (xR : xr → PGame) (j) : Subsequent ((xR i).moveRight j) (mk xl xr xL xR) := by pgame_wf_tac @[simp] theorem Subsequent.moveLeft_mk_left {xR : xr → PGame} {i : xl} (xL : xl → PGame) (j) : Subsequent ((xL i).moveLeft j) (mk xl xr xL xR) := by pgame_wf_tac @[simp] theorem Subsequent.moveLeft_mk_right {xL : xl → PGame} {i : xr} (xR : xr → PGame) (j) : Subsequent ((xR i).moveLeft j) (mk xl xr xL xR) := by pgame_wf_tac /-! ### Basic pre-games -/ /-- The pre-game `Zero` is defined by `0 = { | }`. -/ instance : Zero PGame := ⟨⟨PEmpty, PEmpty, PEmpty.elim, PEmpty.elim⟩⟩ @[simp] theorem zero_leftMoves : LeftMoves 0 = PEmpty := rfl @[simp] theorem zero_rightMoves : RightMoves 0 = PEmpty := rfl instance isEmpty_zero_leftMoves : IsEmpty (LeftMoves 0) := PEmpty.instIsEmpty instance isEmpty_zero_rightMoves : IsEmpty (RightMoves 0) := PEmpty.instIsEmpty instance : Inhabited PGame := ⟨0⟩ /-- The pre-game `One` is defined by `1 = { 0 | }`. -/ instance instOnePGame : One PGame := ⟨⟨PUnit, PEmpty, fun _ => 0, PEmpty.elim⟩⟩ @[simp] theorem one_leftMoves : LeftMoves 1 = PUnit := rfl @[simp] theorem one_moveLeft (x) : moveLeft 1 x = 0 := rfl @[simp] theorem one_rightMoves : RightMoves 1 = PEmpty := rfl instance uniqueOneLeftMoves : Unique (LeftMoves 1) := PUnit.instUnique instance isEmpty_one_rightMoves : IsEmpty (RightMoves 1) := PEmpty.instIsEmpty /-! ### Identity -/ /-- Two pre-games are identical if their left and right sets are identical. That is, `Identical x y` if every left move of `x` is identical to some left move of `y`, every right move of `x` is identical to some right move of `y`, and vice versa. -/ def Identical : PGame.{u} → PGame.{u} → Prop | mk _ _ xL xR, mk _ _ yL yR => Relator.BiTotal (fun i j ↦ Identical (xL i) (yL j)) ∧ Relator.BiTotal (fun i j ↦ Identical (xR i) (yR j)) @[inherit_doc] scoped infix:50 " ≡ " => PGame.Identical theorem identical_iff : ∀ {x y : PGame}, x ≡ y ↔ Relator.BiTotal (x.moveLeft · ≡ y.moveLeft ·) ∧ Relator.BiTotal (x.moveRight · ≡ y.moveRight ·) | mk _ _ _ _, mk _ _ _ _ => Iff.rfl @[refl, simp] protected theorem Identical.refl (x) : x ≡ x := PGame.recOn x fun _ _ _ _ IHL IHR ↦ ⟨Relator.BiTotal.refl IHL, Relator.BiTotal.refl IHR⟩ protected theorem Identical.rfl {x} : x ≡ x := Identical.refl x @[symm] protected theorem Identical.symm : ∀ {x y}, x ≡ y → y ≡ x | mk _ _ _ _, mk _ _ _ _, ⟨hL, hR⟩ => ⟨hL.symm fun _ _ h ↦ h.symm, hR.symm fun _ _ h ↦ h.symm⟩ theorem identical_comm {x y} : x ≡ y ↔ y ≡ x := ⟨.symm, .symm⟩ @[trans] protected theorem Identical.trans : ∀ {x y z}, x ≡ y → y ≡ z → x ≡ z | mk _ _ _ _, mk _ _ _ _, mk _ _ _ _, ⟨hL₁, hR₁⟩, ⟨hL₂, hR₂⟩ => ⟨hL₁.trans (fun _ _ _ h₁ h₂ ↦ h₁.trans h₂) hL₂, hR₁.trans (fun _ _ _ h₁ h₂ ↦ h₁.trans h₂) hR₂⟩ /-- `x ∈ₗ y` if `x` is identical to some left move of `y`. -/ def memₗ (x y : PGame.{u}) : Prop := ∃ b, x ≡ y.moveLeft b /-- `x ∈ᵣ y` if `x` is identical to some right move of `y`. -/ def memᵣ (x y : PGame.{u}) : Prop := ∃ b, x ≡ y.moveRight b @[inherit_doc] scoped infix:50 " ∈ₗ " => PGame.memₗ @[inherit_doc] scoped infix:50 " ∈ᵣ " => PGame.memᵣ @[inherit_doc PGame.memₗ] binder_predicate x " ∈ₗ " y:term => `($x ∈ₗ $y) @[inherit_doc PGame.memᵣ] binder_predicate x " ∈ᵣ " y:term => `($x ∈ᵣ $y) theorem memₗ_def {x y : PGame} : x ∈ₗ y ↔ ∃ b, x ≡ y.moveLeft b := .rfl theorem memᵣ_def {x y : PGame} : x ∈ᵣ y ↔ ∃ b, x ≡ y.moveRight b := .rfl theorem moveLeft_memₗ (x : PGame) (b) : x.moveLeft b ∈ₗ x := ⟨_, .rfl⟩ theorem moveRight_memᵣ (x : PGame) (b) : x.moveRight b ∈ᵣ x := ⟨_, .rfl⟩ theorem identical_of_isEmpty (x y : PGame) [IsEmpty x.LeftMoves] [IsEmpty x.RightMoves] [IsEmpty y.LeftMoves] [IsEmpty y.RightMoves] : x ≡ y := identical_iff.2 (by simp [biTotal_empty]) /-- `Identical` as a `Setoid`. -/ def identicalSetoid : Setoid PGame := ⟨Identical, Identical.refl, Identical.symm, Identical.trans⟩ instance : IsRefl PGame (· ≡ ·) := ⟨Identical.refl⟩ instance : IsSymm PGame (· ≡ ·) := ⟨fun _ _ ↦ Identical.symm⟩ instance : IsTrans PGame (· ≡ ·) := ⟨fun _ _ _ ↦ Identical.trans⟩ instance : IsEquiv PGame (· ≡ ·) := { } /-- If `x` and `y` are identical, then a left move of `x` is identical to some left move of `y`. -/ lemma Identical.moveLeft : ∀ {x y}, x ≡ y → ∀ i, ∃ j, x.moveLeft i ≡ y.moveLeft j | mk _ _ _ _, mk _ _ _ _, ⟨hl, _⟩, i => hl.1 i /-- If `x` and `y` are identical, then a right move of `x` is identical to some right move of `y`. -/ lemma Identical.moveRight : ∀ {x y}, x ≡ y → ∀ i, ∃ j, x.moveRight i ≡ y.moveRight j | mk _ _ _ _, mk _ _ _ _, ⟨_, hr⟩, i => hr.1 i theorem identical_of_eq {x y : PGame} (h : x = y) : x ≡ y := by subst h; rfl /-- Uses `∈ₗ` and `∈ᵣ` instead of `≡`. -/ theorem identical_iff' : ∀ {x y : PGame}, x ≡ y ↔ ((∀ i, x.moveLeft i ∈ₗ y) ∧ (∀ j, y.moveLeft j ∈ₗ x)) ∧ ((∀ i, x.moveRight i ∈ᵣ y) ∧ (∀ j, y.moveRight j ∈ᵣ x)) | mk xl xr xL xR, mk yl yr yL yR => by convert identical_iff <;> dsimp [Relator.BiTotal, Relator.LeftTotal, Relator.RightTotal] <;> congr! <;> exact exists_congr <| fun _ ↦ identical_comm theorem memₗ.congr_right : ∀ {x y : PGame}, x ≡ y → (∀ {w : PGame}, w ∈ₗ x ↔ w ∈ₗ y) | mk _ _ _ _, mk _ _ _ _, ⟨⟨h₁, h₂⟩, _⟩, _w => ⟨fun ⟨i, hi⟩ ↦ (h₁ i).imp (fun _ ↦ hi.trans), fun ⟨j, hj⟩ ↦ (h₂ j).imp (fun _ hi ↦ hj.trans hi.symm)⟩ theorem memᵣ.congr_right : ∀ {x y : PGame}, x ≡ y → (∀ {w : PGame}, w ∈ᵣ x ↔ w ∈ᵣ y) | mk _ _ _ _, mk _ _ _ _, ⟨_, ⟨h₁, h₂⟩⟩, _w => ⟨fun ⟨i, hi⟩ ↦ (h₁ i).imp (fun _ ↦ hi.trans), fun ⟨j, hj⟩ ↦ (h₂ j).imp (fun _ hi ↦ hj.trans hi.symm)⟩ theorem memₗ.congr_left : ∀ {x y : PGame}, x ≡ y → (∀ {w : PGame}, x ∈ₗ w ↔ y ∈ₗ w) | _, _, h, mk _ _ _ _ => ⟨fun ⟨i, hi⟩ ↦ ⟨i, h.symm.trans hi⟩, fun ⟨i, hi⟩ ↦ ⟨i, h.trans hi⟩⟩ theorem memᵣ.congr_left : ∀ {x y : PGame}, x ≡ y → (∀ {w : PGame}, x ∈ᵣ w ↔ y ∈ᵣ w) | _, _, h, mk _ _ _ _ => ⟨fun ⟨i, hi⟩ ↦ ⟨i, h.symm.trans hi⟩, fun ⟨i, hi⟩ ↦ ⟨i, h.trans hi⟩⟩ lemma Identical.ext : ∀ {x y}, (∀ z, z ∈ₗ x ↔ z ∈ₗ y) → (∀ z, z ∈ᵣ x ↔ z ∈ᵣ y) → x ≡ y | mk _ _ _ _, mk _ _ _ _, hl, hr => identical_iff'.mpr ⟨⟨fun i ↦ (hl _).mp ⟨i, refl _⟩, fun j ↦ (hl _).mpr ⟨j, refl _⟩⟩, ⟨fun i ↦ (hr _).mp ⟨i, refl _⟩, fun j ↦ (hr _).mpr ⟨j, refl _⟩⟩⟩ lemma Identical.ext_iff {x y} : x ≡ y ↔ (∀ z, z ∈ₗ x ↔ z ∈ₗ y) ∧ (∀ z, z ∈ᵣ x ↔ z ∈ᵣ y) := ⟨fun h ↦ ⟨@memₗ.congr_right _ _ h, @memᵣ.congr_right _ _ h⟩, fun h ↦ h.elim Identical.ext⟩ lemma Identical.congr_right {x y z} (h : x ≡ y) : z ≡ x ↔ z ≡ y := ⟨fun hz ↦ hz.trans h, fun hz ↦ hz.trans h.symm⟩ lemma Identical.congr_left {x y z} (h : x ≡ y) : x ≡ z ↔ y ≡ z := ⟨fun hz ↦ h.symm.trans hz, fun hz ↦ h.trans hz⟩ /-- Show `x ≡ y` by giving an explicit correspondence between the moves of `x` and `y`. -/ lemma Identical.of_fn {x y : PGame} (l : x.LeftMoves → y.LeftMoves) (il : y.LeftMoves → x.LeftMoves) (r : x.RightMoves → y.RightMoves) (ir : y.RightMoves → x.RightMoves) (hl : ∀ i, x.moveLeft i ≡ y.moveLeft (l i)) (hil : ∀ i, x.moveLeft (il i) ≡ y.moveLeft i) (hr : ∀ i, x.moveRight i ≡ y.moveRight (r i)) (hir : ∀ i, x.moveRight (ir i) ≡ y.moveRight i) : x ≡ y := identical_iff.mpr ⟨⟨fun i ↦ ⟨l i, hl i⟩, fun i ↦ ⟨il i, hil i⟩⟩, ⟨fun i ↦ ⟨r i, hr i⟩, fun i ↦ ⟨ir i, hir i⟩⟩⟩ lemma Identical.of_equiv {x y : PGame} (l : x.LeftMoves ≃ y.LeftMoves) (r : x.RightMoves ≃ y.RightMoves) (hl : ∀ i, x.moveLeft i ≡ y.moveLeft (l i)) (hr : ∀ i, x.moveRight i ≡ y.moveRight (r i)) : x ≡ y := .of_fn l l.symm r r.symm hl (by simpa using hl <| l.symm ·) hr (by simpa using hr <| r.symm ·) /-! ### Relabellings -/ /-- `Relabelling x y` says that `x` and `y` are really the same game, just dressed up differently. Specifically, there is a bijection between the moves for Left in `x` and in `y`, and similarly for Right, and under these bijections we inductively have `Relabelling`s for the consequent games. -/ inductive Relabelling : PGame.{u} → PGame.{u} → Type (u + 1) | mk : ∀ {x y : PGame} (L : x.LeftMoves ≃ y.LeftMoves) (R : x.RightMoves ≃ y.RightMoves), (∀ i, Relabelling (x.moveLeft i) (y.moveLeft (L i))) → (∀ j, Relabelling (x.moveRight j) (y.moveRight (R j))) → Relabelling x y @[inherit_doc] scoped infixl:50 " ≡r " => PGame.Relabelling namespace Relabelling variable {x y : PGame.{u}} /-- A constructor for relabellings swapping the equivalences. -/ def mk' (L : y.LeftMoves ≃ x.LeftMoves) (R : y.RightMoves ≃ x.RightMoves) (hL : ∀ i, x.moveLeft (L i) ≡r y.moveLeft i) (hR : ∀ j, x.moveRight (R j) ≡r y.moveRight j) : x ≡r y := ⟨L.symm, R.symm, fun i => by simpa using hL (L.symm i), fun j => by simpa using hR (R.symm j)⟩ /-- The equivalence between left moves of `x` and `y` given by the relabelling. -/ def leftMovesEquiv : x ≡r y → x.LeftMoves ≃ y.LeftMoves | ⟨L,_, _,_⟩ => L @[simp] theorem mk_leftMovesEquiv {x y L R hL hR} : (@Relabelling.mk x y L R hL hR).leftMovesEquiv = L := rfl @[simp] theorem mk'_leftMovesEquiv {x y L R hL hR} : (@Relabelling.mk' x y L R hL hR).leftMovesEquiv = L.symm := rfl /-- The equivalence between right moves of `x` and `y` given by the relabelling. -/ def rightMovesEquiv : x ≡r y → x.RightMoves ≃ y.RightMoves | ⟨_, R, _, _⟩ => R @[simp] theorem mk_rightMovesEquiv {x y L R hL hR} : (@Relabelling.mk x y L R hL hR).rightMovesEquiv = R := rfl @[simp] theorem mk'_rightMovesEquiv {x y L R hL hR} : (@Relabelling.mk' x y L R hL hR).rightMovesEquiv = R.symm := rfl /-- A left move of `x` is a relabelling of a left move of `y`. -/ def moveLeft : ∀ (r : x ≡r y) (i : x.LeftMoves), x.moveLeft i ≡r y.moveLeft (r.leftMovesEquiv i) | ⟨_, _, hL, _⟩ => hL /-- A left move of `y` is a relabelling of a left move of `x`. -/ def moveLeftSymm : ∀ (r : x ≡r y) (i : y.LeftMoves), x.moveLeft (r.leftMovesEquiv.symm i) ≡r y.moveLeft i | ⟨L, R, hL, hR⟩, i => by simpa using hL (L.symm i) /-- A right move of `x` is a relabelling of a right move of `y`. -/ def moveRight : ∀ (r : x ≡r y) (i : x.RightMoves), x.moveRight i ≡r y.moveRight (r.rightMovesEquiv i) | ⟨_, _, _, hR⟩ => hR /-- A right move of `y` is a relabelling of a right move of `x`. -/ def moveRightSymm : ∀ (r : x ≡r y) (i : y.RightMoves), x.moveRight (r.rightMovesEquiv.symm i) ≡r y.moveRight i | ⟨L, R, hL, hR⟩, i => by simpa using hR (R.symm i) /-- The identity relabelling. -/ @[refl] def refl (x : PGame) : x ≡r x := ⟨Equiv.refl _, Equiv.refl _, fun _ => refl _, fun _ => refl _⟩ termination_by x instance (x : PGame) : Inhabited (x ≡r x) := ⟨refl _⟩ /-- Flip a relabelling. -/ @[symm] def symm : ∀ {x y : PGame}, x ≡r y → y ≡r x | _, _, ⟨L, R, hL, hR⟩ => mk' L R (fun i => (hL i).symm) fun j => (hR j).symm /-- Transitivity of relabelling. -/ @[trans] def trans : ∀ {x y z : PGame}, x ≡r y → y ≡r z → x ≡r z | _, _, _, ⟨L₁, R₁, hL₁, hR₁⟩, ⟨L₂, R₂, hL₂, hR₂⟩ => ⟨L₁.trans L₂, R₁.trans R₂, fun i => (hL₁ i).trans (hL₂ _), fun j => (hR₁ j).trans (hR₂ _)⟩ /-- Any game without left or right moves is a relabelling of 0. -/ def isEmpty (x : PGame) [IsEmpty x.LeftMoves] [IsEmpty x.RightMoves] : x ≡r 0 := ⟨Equiv.equivPEmpty _, Equiv.equivOfIsEmpty _ _, isEmptyElim, isEmptyElim⟩ end Relabelling /-- Replace the types indexing the next moves for Left and Right by equivalent types. -/ def relabel {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) : PGame := ⟨xl', xr', x.moveLeft ∘ el, x.moveRight ∘ er⟩ @[simp] theorem relabel_moveLeft' {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) (i : xl') : moveLeft (relabel el er) i = x.moveLeft (el i) := rfl theorem relabel_moveLeft {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) (i : x.LeftMoves) : moveLeft (relabel el er) (el.symm i) = x.moveLeft i := by simp @[simp] theorem relabel_moveRight' {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) (j : xr') : moveRight (relabel el er) j = x.moveRight (er j) := rfl theorem relabel_moveRight {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) (j : x.RightMoves) : moveRight (relabel el er) (er.symm j) = x.moveRight j := by simp /-- The game obtained by relabelling the next moves is a relabelling of the original game. -/ def relabelRelabelling {x : PGame} {xl' xr'} (el : xl' ≃ x.LeftMoves) (er : xr' ≃ x.RightMoves) : x ≡r relabel el er := -- Porting note: needed to add `rfl` Relabelling.mk' el er (fun i => by simp; rfl) (fun j => by simp; rfl) /-! ### Inserting an option -/ /-- The pregame constructed by inserting `x'` as a new left option into x. -/ def insertLeft (x x' : PGame.{u}) : PGame := match x with | mk xl xr xL xR => mk (xl ⊕ PUnit) xr (Sum.elim xL fun _ => x') xR /-- The pregame constructed by inserting `x'` as a new right option into x. -/ def insertRight (x x' : PGame.{u}) : PGame := match x with | mk xl xr xL xR => mk xl (xr ⊕ PUnit) xL (Sum.elim xR fun _ => x') /-- Inserting on the left and right commutes. -/ theorem insertRight_insertLeft {x x' x'' : PGame} : insertRight (insertLeft x x') x'' = insertLeft (insertRight x x'') x' := by cases x; cases x'; cases x'' dsimp [insertLeft, insertRight] end SetTheory.PGame
.lake/packages/mathlib/Mathlib/SetTheory/PGame/Algebra.lean
import Mathlib.Algebra.Order.ZeroLEOne import Mathlib.SetTheory.PGame.Order import Mathlib.Data.Nat.Cast.Defs import Mathlib.Tactic.Linter.DeprecatedModule deprecated_module "This module is now at `CombinatorialGames.Game.IGame` in the CGT repo <https://github.com/vihdzp/combinatorial-games>" (since := "2025-08-06") /-! # Algebraic structure on pregames This file defines the operations necessary to make games into an additive commutative group. Addition is defined for $x = \{xL | xR\}$ and $y = \{yL | yR\}$ by $x + y = \{xL + y, x + yL | xR + y, x + yR\}$. Negation is defined by $\{xL | xR\} = \{-xR | -xL\}$. The order structures interact in the expected way with addition, so we have ``` theorem le_iff_sub_nonneg {x y : PGame} : x ≤ y ↔ 0 ≤ y - x := sorry theorem lt_iff_sub_pos {x y : PGame} : x < y ↔ 0 < y - x := sorry ``` We show that these operations respect the equivalence relation, and hence descend to games. At the level of games, these operations satisfy all the laws of a commutative group. To prove the necessary equivalence relations at the level of pregames, the notion of a `Relabelling` of a game is used (defined in `Mathlib/SetTheory/PGame/Basic.lean`); for example, there is a relabelling between `x + (y + z)` and `(x + y) + z`. -/ namespace SetTheory.PGame open Function Relation universe u /-! ### Negation -/ /-- The negation of `{L | R}` is `{-R | -L}`. -/ def neg : PGame → PGame | ⟨l, r, L, R⟩ => ⟨r, l, fun i => neg (R i), fun i => neg (L i)⟩ instance : Neg PGame := ⟨neg⟩ @[simp] theorem neg_def {xl xr xL xR} : -mk xl xr xL xR = mk xr xl (-xR ·) (-xL ·) := rfl instance : InvolutiveNeg PGame := { inferInstanceAs (Neg PGame) with neg_neg := fun x => by induction x with | mk xl xr xL xR ihL ihR => simp_rw [neg_def, ihL, ihR] } instance : NegZeroClass PGame := { inferInstanceAs (Zero PGame), inferInstanceAs (Neg PGame) with neg_zero := by dsimp [Zero.zero, Neg.neg, neg] congr <;> funext i <;> cases i } @[simp] theorem neg_ofLists (L R : List PGame) : -ofLists L R = ofLists (R.map fun x => -x) (L.map fun x => -x) := by simp only [ofLists, neg_def, List.getElem_map, mk.injEq, List.length_map, true_and] constructor all_goals apply hfunext · simp · rintro ⟨⟨a, ha⟩⟩ ⟨⟨b, hb⟩⟩ h have : ∀ {m n} (_ : m = n) {b : ULift (Fin m)} {c : ULift (Fin n)} (_ : b ≍ c), (b.down : ℕ) = ↑c.down := by rintro m n rfl b c simp only [heq_eq_eq] rintro rfl rfl simp only [heq_eq_eq] congr 5 exact this (List.length_map _).symm h theorem isOption_neg {x y : PGame} : IsOption x (-y) ↔ IsOption (-x) y := by rw [isOption_iff, isOption_iff, or_comm] cases y apply or_congr <;> · apply exists_congr intro rw [neg_eq_iff_eq_neg] simp only [neg_def, moveRight_mk, moveLeft_mk] @[simp] theorem isOption_neg_neg {x y : PGame} : IsOption (-x) (-y) ↔ IsOption x y := by rw [isOption_neg, neg_neg] /-- Use `toLeftMovesNeg` to cast between these two types. -/ theorem leftMoves_neg : ∀ x : PGame, (-x).LeftMoves = x.RightMoves | ⟨_, _, _, _⟩ => rfl /-- Use `toRightMovesNeg` to cast between these two types. -/ theorem rightMoves_neg : ∀ x : PGame, (-x).RightMoves = x.LeftMoves | ⟨_, _, _, _⟩ => rfl /-- Turns a right move for `x` into a left move for `-x` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toLeftMovesNeg {x : PGame} : x.RightMoves ≃ (-x).LeftMoves := Equiv.cast (leftMoves_neg x).symm /-- Turns a left move for `x` into a right move for `-x` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toRightMovesNeg {x : PGame} : x.LeftMoves ≃ (-x).RightMoves := Equiv.cast (rightMoves_neg x).symm @[simp] theorem moveLeft_neg {x : PGame} (i) : (-x).moveLeft i = -x.moveRight (toLeftMovesNeg.symm i) := by cases x rfl theorem moveLeft_neg_toLeftMovesNeg {x : PGame} (i) : (-x).moveLeft (toLeftMovesNeg i) = -x.moveRight i := by simp @[simp] theorem moveRight_neg {x : PGame} (i) : (-x).moveRight i = -x.moveLeft (toRightMovesNeg.symm i) := by cases x rfl theorem moveRight_neg_toRightMovesNeg {x : PGame} (i) : (-x).moveRight (toRightMovesNeg i) = -x.moveLeft i := by simp @[simp] theorem forall_leftMoves_neg {x : PGame} {p : (-x).LeftMoves → Prop} : (∀ i : (-x).LeftMoves, p i) ↔ (∀ i : x.RightMoves, p (toLeftMovesNeg i)) := toLeftMovesNeg.forall_congr_right.symm @[simp] theorem exists_leftMoves_neg {x : PGame} {p : (-x).LeftMoves → Prop} : (∃ i : (-x).LeftMoves, p i) ↔ (∃ i : x.RightMoves, p (toLeftMovesNeg i)) := toLeftMovesNeg.exists_congr_right.symm @[simp] theorem forall_rightMoves_neg {x : PGame} {p : (-x).RightMoves → Prop} : (∀ i : (-x).RightMoves, p i) ↔ (∀ i : x.LeftMoves, p (toRightMovesNeg i)) := toRightMovesNeg.forall_congr_right.symm @[simp] theorem exists_rightMoves_neg {x : PGame} {p : (-x).RightMoves → Prop} : (∃ i : (-x).RightMoves, p i) ↔ (∃ i : x.LeftMoves, p (toRightMovesNeg i)) := toRightMovesNeg.exists_congr_right.symm theorem leftMoves_neg_cases {x : PGame} (k) {P : (-x).LeftMoves → Prop} (h : ∀ i, P <| toLeftMovesNeg i) : P k := by rw [← toLeftMovesNeg.apply_symm_apply k] exact h _ theorem rightMoves_neg_cases {x : PGame} (k) {P : (-x).RightMoves → Prop} (h : ∀ i, P <| toRightMovesNeg i) : P k := by rw [← toRightMovesNeg.apply_symm_apply k] exact h _ /-- If `x` has the same moves as `y`, then `-x` has the sames moves as `-y`. -/ lemma Identical.neg : ∀ {x₁ x₂ : PGame}, x₁ ≡ x₂ → -x₁ ≡ -x₂ | mk _ _ _ _, mk _ _ _ _, ⟨⟨hL₁, hL₂⟩, ⟨hR₁, hR₂⟩⟩ => ⟨⟨fun i ↦ (hR₁ i).imp (fun _ ↦ Identical.neg), fun j ↦ (hR₂ j).imp (fun _ ↦ Identical.neg)⟩, ⟨fun i ↦ (hL₁ i).imp (fun _ ↦ Identical.neg), fun j ↦ (hL₂ j).imp (fun _ ↦ Identical.neg)⟩⟩ /-- If `-x` has the same moves as `-y`, then `x` has the sames moves as `y`. -/ lemma Identical.of_neg : ∀ {x₁ x₂ : PGame}, -x₁ ≡ -x₂ → x₁ ≡ x₂ | mk x₁l x₁r x₁L x₁R, mk x₂l x₂r x₂L x₂R => by simpa using Identical.neg (x₁ := mk _ _ (-x₁R ·) (-x₁L ·)) (x₂ := mk _ _ (-x₂R ·) (-x₂L ·)) lemma memₗ_neg_iff : ∀ {x y : PGame}, x ∈ₗ -y ↔ ∃ z ∈ᵣ y, x ≡ -z | mk _ _ _ _, mk _ _ _ _ => ⟨fun ⟨_i, hi⟩ ↦ ⟨_, ⟨_, refl _⟩, hi⟩, fun ⟨_, ⟨i, hi⟩, h⟩ ↦ ⟨i, h.trans hi.neg⟩⟩ lemma memᵣ_neg_iff : ∀ {x y : PGame}, x ∈ᵣ -y ↔ ∃ z ∈ₗ y, x ≡ -z | mk _ _ _ _, mk _ _ _ _ => ⟨fun ⟨_i, hi⟩ ↦ ⟨_, ⟨_, refl _⟩, hi⟩, fun ⟨_, ⟨i, hi⟩, h⟩ ↦ ⟨i, h.trans hi.neg⟩⟩ /-- If `x` has the same moves as `y`, then `-x` has the same moves as `-y`. -/ def Relabelling.negCongr : ∀ {x y : PGame}, x ≡r y → -x ≡r -y | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩, ⟨L, R, hL, hR⟩ => ⟨R, L, fun j => (hR j).negCongr, fun i => (hL i).negCongr⟩ private theorem neg_le_lf_neg_iff : ∀ {x y : PGame.{u}}, (-y ≤ -x ↔ x ≤ y) ∧ (-y ⧏ -x ↔ x ⧏ y) | mk xl xr xL xR, mk yl yr yL yR => by simp_rw [neg_def, mk_le_mk, mk_lf_mk, ← neg_def] constructor · rw [and_comm] apply and_congr <;> exact forall_congr' fun _ => neg_le_lf_neg_iff.2 · rw [or_comm] apply or_congr <;> exact exists_congr fun _ => neg_le_lf_neg_iff.1 termination_by x y => (x, y) @[simp] theorem neg_le_neg_iff {x y : PGame} : -y ≤ -x ↔ x ≤ y := neg_le_lf_neg_iff.1 @[simp] theorem neg_lf_neg_iff {x y : PGame} : -y ⧏ -x ↔ x ⧏ y := neg_le_lf_neg_iff.2 @[simp] theorem neg_lt_neg_iff {x y : PGame} : -y < -x ↔ x < y := by rw [lt_iff_le_and_lf, lt_iff_le_and_lf, neg_le_neg_iff, neg_lf_neg_iff] @[simp] theorem neg_identical_neg {x y : PGame} : -x ≡ -y ↔ x ≡ y := ⟨Identical.of_neg, Identical.neg⟩ @[simp] theorem neg_equiv_neg_iff {x y : PGame} : -x ≈ -y ↔ x ≈ y := by change Equiv (-x) (-y) ↔ Equiv x y rw [Equiv, Equiv, neg_le_neg_iff, neg_le_neg_iff, and_comm] @[simp] theorem neg_fuzzy_neg_iff {x y : PGame} : -x ‖ -y ↔ x ‖ y := by rw [Fuzzy, Fuzzy, neg_lf_neg_iff, neg_lf_neg_iff, and_comm] theorem neg_le_iff {x y : PGame} : -y ≤ x ↔ -x ≤ y := by rw [← neg_neg x, neg_le_neg_iff, neg_neg] theorem neg_lf_iff {x y : PGame} : -y ⧏ x ↔ -x ⧏ y := by rw [← neg_neg x, neg_lf_neg_iff, neg_neg] theorem neg_lt_iff {x y : PGame} : -y < x ↔ -x < y := by rw [← neg_neg x, neg_lt_neg_iff, neg_neg] theorem neg_equiv_iff {x y : PGame} : (-x ≈ y) ↔ (x ≈ -y) := by rw [← neg_neg y, neg_equiv_neg_iff, neg_neg] theorem neg_fuzzy_iff {x y : PGame} : -x ‖ y ↔ x ‖ -y := by rw [← neg_neg y, neg_fuzzy_neg_iff, neg_neg] theorem le_neg_iff {x y : PGame} : y ≤ -x ↔ x ≤ -y := by rw [← neg_neg x, neg_le_neg_iff, neg_neg] theorem lf_neg_iff {x y : PGame} : y ⧏ -x ↔ x ⧏ -y := by rw [← neg_neg x, neg_lf_neg_iff, neg_neg] theorem lt_neg_iff {x y : PGame} : y < -x ↔ x < -y := by rw [← neg_neg x, neg_lt_neg_iff, neg_neg] @[simp] theorem neg_le_zero_iff {x : PGame} : -x ≤ 0 ↔ 0 ≤ x := by rw [neg_le_iff, neg_zero] @[simp] theorem zero_le_neg_iff {x : PGame} : 0 ≤ -x ↔ x ≤ 0 := by rw [le_neg_iff, neg_zero] @[simp] theorem neg_lf_zero_iff {x : PGame} : -x ⧏ 0 ↔ 0 ⧏ x := by rw [neg_lf_iff, neg_zero] @[simp] theorem zero_lf_neg_iff {x : PGame} : 0 ⧏ -x ↔ x ⧏ 0 := by rw [lf_neg_iff, neg_zero] @[simp] theorem neg_lt_zero_iff {x : PGame} : -x < 0 ↔ 0 < x := by rw [neg_lt_iff, neg_zero] @[simp] theorem zero_lt_neg_iff {x : PGame} : 0 < -x ↔ x < 0 := by rw [lt_neg_iff, neg_zero] @[simp] theorem neg_equiv_zero_iff {x : PGame} : (-x ≈ 0) ↔ (x ≈ 0) := by rw [neg_equiv_iff, neg_zero] @[simp] theorem neg_fuzzy_zero_iff {x : PGame} : -x ‖ 0 ↔ x ‖ 0 := by rw [neg_fuzzy_iff, neg_zero] @[simp] theorem zero_equiv_neg_iff {x : PGame} : (0 ≈ -x) ↔ (0 ≈ x) := by rw [← neg_equiv_iff, neg_zero] @[simp] theorem zero_fuzzy_neg_iff {x : PGame} : 0 ‖ -x ↔ 0 ‖ x := by rw [← neg_fuzzy_iff, neg_zero] /-! ### Addition and subtraction -/ /-- The sum of `x = {xL | xR}` and `y = {yL | yR}` is `{xL + y, x + yL | xR + y, x + yR}`. -/ instance : Add PGame.{u} := ⟨fun x y => by induction x generalizing y with | mk xl xr _ _ IHxl IHxr => _ induction y with | mk yl yr yL yR IHyl IHyr => _ have y := mk yl yr yL yR refine ⟨xl ⊕ yl, xr ⊕ yr, Sum.rec ?_ ?_, Sum.rec ?_ ?_⟩ · exact fun i => IHxl i y · exact IHyl · exact fun i => IHxr i y · exact IHyr⟩ theorem mk_add_moveLeft {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveLeft i = i.rec (xL · + mk yl yr yL yR) (mk xl xr xL xR + yL ·) := rfl theorem mk_add_moveRight {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveRight i = i.rec (xR · + mk yl yr yL yR) (mk xl xr xL xR + yR ·) := rfl /-- The pre-game `((0 + 1) + ⋯) + 1`. Note that this is **not** the usual recursive definition `n = {0, 1, … | }`. For instance, `2 = 0 + 1 + 1 = {0 + 0 + 1, 0 + 1 + 0 | }` does not contain any left option equivalent to `0`. For an implementation of said definition, see `Ordinal.toPGame`. For the proof that these games are equivalent, see `Ordinal.toPGame_natCast`. -/ instance : NatCast PGame := ⟨Nat.unaryCast⟩ @[simp] protected theorem nat_succ (n : ℕ) : ((n + 1 : ℕ) : PGame) = n + 1 := rfl instance isEmpty_leftMoves_add (x y : PGame.{u}) [IsEmpty x.LeftMoves] [IsEmpty y.LeftMoves] : IsEmpty (x + y).LeftMoves := by cases x cases y apply isEmpty_sum.2 ⟨_, _⟩ assumption' instance isEmpty_rightMoves_add (x y : PGame.{u}) [IsEmpty x.RightMoves] [IsEmpty y.RightMoves] : IsEmpty (x + y).RightMoves := by cases x cases y apply isEmpty_sum.2 ⟨_, _⟩ assumption' /-- `x + 0` has exactly the same moves as `x`. -/ def addZeroRelabelling : ∀ x : PGame.{u}, x + 0 ≡r x | ⟨xl, xr, xL, xR⟩ => by refine ⟨Equiv.sumEmpty xl PEmpty, Equiv.sumEmpty xr PEmpty, ?_, ?_⟩ <;> rintro (⟨i⟩ | ⟨⟨⟩⟩) <;> apply addZeroRelabelling termination_by x => x /-- `x + 0` is equivalent to `x`. -/ theorem add_zero_equiv (x : PGame.{u}) : x + 0 ≈ x := (addZeroRelabelling x).equiv /-- `0 + x` has exactly the same moves as `x`. -/ def zeroAddRelabelling : ∀ x : PGame.{u}, 0 + x ≡r x | ⟨xl, xr, xL, xR⟩ => by refine ⟨Equiv.emptySum PEmpty xl, Equiv.emptySum PEmpty xr, ?_, ?_⟩ <;> rintro (⟨⟨⟩⟩ | ⟨i⟩) <;> apply zeroAddRelabelling /-- `0 + x` is equivalent to `x`. -/ theorem zero_add_equiv (x : PGame.{u}) : 0 + x ≈ x := (zeroAddRelabelling x).equiv /-- Use `toLeftMovesAdd` to cast between these two types. -/ theorem leftMoves_add : ∀ x y : PGame.{u}, (x + y).LeftMoves = (x.LeftMoves ⊕ y.LeftMoves) | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩ => rfl /-- Use `toRightMovesAdd` to cast between these two types. -/ theorem rightMoves_add : ∀ x y : PGame.{u}, (x + y).RightMoves = (x.RightMoves ⊕ y.RightMoves) | ⟨_, _, _, _⟩, ⟨_, _, _, _⟩ => rfl /-- Converts a left move for `x` or `y` into a left move for `x + y` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toLeftMovesAdd {x y : PGame} : x.LeftMoves ⊕ y.LeftMoves ≃ (x + y).LeftMoves := Equiv.cast (leftMoves_add x y).symm /-- Converts a right move for `x` or `y` into a right move for `x + y` and vice versa. Even though these types are the same (not definitionally so), this is the preferred way to convert between them. -/ def toRightMovesAdd {x y : PGame} : x.RightMoves ⊕ y.RightMoves ≃ (x + y).RightMoves := Equiv.cast (rightMoves_add x y).symm @[simp] theorem mk_add_moveLeft_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveLeft (Sum.inl i) = (mk xl xr xL xR).moveLeft i + mk yl yr yL yR := rfl @[simp] theorem add_moveLeft_inl {x : PGame} (y : PGame) (i) : (x + y).moveLeft (toLeftMovesAdd (Sum.inl i)) = x.moveLeft i + y := by cases x cases y rfl @[simp] theorem mk_add_moveRight_inl {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveRight (Sum.inl i) = (mk xl xr xL xR).moveRight i + mk yl yr yL yR := rfl @[simp] theorem add_moveRight_inl {x : PGame} (y : PGame) (i) : (x + y).moveRight (toRightMovesAdd (Sum.inl i)) = x.moveRight i + y := by cases x cases y rfl @[simp] theorem mk_add_moveLeft_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveLeft (Sum.inr i) = mk xl xr xL xR + (mk yl yr yL yR).moveLeft i := rfl @[simp] theorem add_moveLeft_inr (x : PGame) {y : PGame} (i) : (x + y).moveLeft (toLeftMovesAdd (Sum.inr i)) = x + y.moveLeft i := by cases x cases y rfl @[simp] theorem mk_add_moveRight_inr {xl xr yl yr} {xL xR yL yR} {i} : (mk xl xr xL xR + mk yl yr yL yR).moveRight (Sum.inr i) = mk xl xr xL xR + (mk yl yr yL yR).moveRight i := rfl @[simp] theorem add_moveRight_inr (x : PGame) {y : PGame} (i) : (x + y).moveRight (toRightMovesAdd (Sum.inr i)) = x + y.moveRight i := by cases x cases y rfl /-- Case on possible left moves of `x + y`. -/ theorem leftMoves_add_cases {x y : PGame} (k) {P : (x + y).LeftMoves → Prop} (hl : ∀ i, P <| toLeftMovesAdd (Sum.inl i)) (hr : ∀ i, P <| toLeftMovesAdd (Sum.inr i)) : P k := by rw [← toLeftMovesAdd.apply_symm_apply k] rcases toLeftMovesAdd.symm k with i | i · exact hl i · exact hr i /-- Case on possible right moves of `x + y`. -/ theorem rightMoves_add_cases {x y : PGame} (k) {P : (x + y).RightMoves → Prop} (hl : ∀ j, P <| toRightMovesAdd (Sum.inl j)) (hr : ∀ j, P <| toRightMovesAdd (Sum.inr j)) : P k := by rw [← toRightMovesAdd.apply_symm_apply k] rcases toRightMovesAdd.symm k with i | i · exact hl i · exact hr i instance isEmpty_nat_rightMoves : ∀ n : ℕ, IsEmpty (RightMoves n) | 0 => inferInstanceAs (IsEmpty PEmpty) | n + 1 => by haveI := isEmpty_nat_rightMoves n rw [PGame.nat_succ, rightMoves_add] infer_instance /-- `x + y` has exactly the same moves as `y + x`. -/ protected lemma add_comm (x y : PGame) : x + y ≡ y + x := match x, y with | mk xl xr xL xR, mk yl yr yL yR => by refine Identical.of_equiv (Equiv.sumComm _ _) (Equiv.sumComm _ _) ?_ ?_ <;> · rintro (_ | _) <;> · dsimp; exact PGame.add_comm _ _ termination_by (x, y) /-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/ protected lemma add_assoc (x y z : PGame) : x + y + z ≡ x + (y + z) := match x, y, z with | mk xl xr xL xR, mk yl yr yL yR, mk zl zr zL zR => by refine Identical.of_equiv (Equiv.sumAssoc _ _ _) (Equiv.sumAssoc _ _ _) ?_ ?_ <;> · rintro ((_ | _) | _) · exact PGame.add_assoc _ _ _ · exact PGame.add_assoc (mk _ _ _ _) _ _ · exact PGame.add_assoc (mk _ _ _ _) (mk _ _ _ _) _ termination_by (x, y, z) /-- `x + 0` has exactly the same moves as `x`. -/ protected lemma add_zero : ∀ (x : PGame), x + 0 ≡ x | mk xl xr xL xR => by refine Identical.of_equiv (Equiv.sumEmpty _ _) (Equiv.sumEmpty _ _) ?_ ?_ <;> · rintro (_ | ⟨⟨⟩⟩) exact PGame.add_zero _ /-- `0 + x` has exactly the same moves as `x`. -/ protected lemma zero_add (x : PGame) : 0 + x ≡ x := (PGame.add_comm _ _).trans x.add_zero /-- `-(x + y)` has exactly the same moves as `-x + -y`. -/ protected lemma neg_add (x y : PGame) : -(x + y) = -x + -y := match x, y with | mk xl xr xL xR, mk yl yr yL yR => by refine ext rfl rfl ?_ ?_ <;> · rintro (i | i) _ ⟨rfl⟩ · exact PGame.neg_add _ _ · simpa [Equiv.refl, mk_add_moveLeft, mk_add_moveRight] using PGame.neg_add _ _ termination_by (x, y) /-- `-(x + y)` has exactly the same moves as `-y + -x`. -/ protected lemma neg_add_rev (x y : PGame) : -(x + y) ≡ -y + -x := Identical.trans (of_eq (x.neg_add y)) (PGame.add_comm _ _) lemma identical_zero_iff : ∀ (x : PGame), x ≡ 0 ↔ IsEmpty x.LeftMoves ∧ IsEmpty x.RightMoves | mk xl xr xL xR => by constructor · rintro ⟨h₁, h₂⟩ dsimp [Relator.BiTotal, Relator.LeftTotal, Relator.RightTotal] at h₁ h₂ simp_rw [IsEmpty.forall_iff, and_true, IsEmpty.exists_iff] at h₁ h₂ exact ⟨⟨h₁⟩, ⟨h₂⟩⟩ · rintro ⟨h₁, h₂⟩ exact identical_of_isEmpty _ _ /-- Any game without left or right moves is identical to 0. -/ lemma identical_zero (x : PGame) [IsEmpty x.LeftMoves] [IsEmpty x.RightMoves] : x ≡ 0 := x.identical_zero_iff.mpr ⟨by infer_instance, by infer_instance⟩ protected lemma add_eq_zero : ∀ {x y : PGame}, x + y ≡ 0 ↔ x ≡ 0 ∧ y ≡ 0 | mk xl xr xL xR, mk yl yr yL yR => by simp_rw [identical_zero_iff, leftMoves_add, rightMoves_add, isEmpty_sum] tauto lemma Identical.add_right {x₁ x₂ y} : x₁ ≡ x₂ → x₁ + y ≡ x₂ + y := match x₁, x₂, y with | mk x₁l x₁r x₁L x₁R, mk x₂l x₂r x₂L x₂R, mk yl yr yL yR => by intro h refine ⟨⟨?_, ?_⟩, ⟨?_, ?_⟩⟩ <;> rintro (_ | _) <;> try exact ⟨.inr _, h.add_right⟩ · exact (h.1.1 _).elim (⟨.inl ·, ·.add_right⟩) · exact (h.1.2 _).elim (⟨.inl ·, ·.add_right⟩) · exact (h.2.1 _).elim (⟨.inl ·, ·.add_right⟩) · exact (h.2.2 _).elim (⟨.inl ·, ·.add_right⟩) termination_by (x₁, x₂, y) lemma Identical.add_left {x y₁ y₂} (hy : y₁ ≡ y₂) : x + y₁ ≡ x + y₂ := (x.add_comm y₁).trans (hy.add_right.trans (y₂.add_comm x)) /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w + y` has the same moves as `x + z`. -/ lemma Identical.add {x₁ x₂ y₁ y₂ : PGame.{u}} (hx : x₁ ≡ x₂) (hy : y₁ ≡ y₂) : x₁ + y₁ ≡ x₂ + y₂ := hx.add_right.trans hy.add_left lemma memₗ_add_iff {x y₁ y₂ : PGame} : x ∈ₗ y₁ + y₂ ↔ (∃ z ∈ₗ y₁, x ≡ z + y₂) ∨ (∃ z ∈ₗ y₂, x ≡ y₁ + z) := by obtain ⟨y₁l, y₁r, y₁L, y₁R⟩ := y₁ obtain ⟨y₂l, y₂r, y₂L, y₂R⟩ := y₂ constructor · rintro ⟨(i | i), hi⟩ exacts [.inl ⟨y₁L i, moveLeft_memₗ _ _, hi⟩, .inr ⟨y₂L i, moveLeft_memₗ _ _, hi⟩] · rintro (⟨_, ⟨i, hi⟩, h⟩ | ⟨_, ⟨i, hi⟩, h⟩) exacts [⟨.inl i, h.trans hi.add_right⟩, ⟨.inr i, h.trans hi.add_left⟩] lemma memᵣ_add_iff {x y₁ y₂ : PGame} : x ∈ᵣ y₁ + y₂ ↔ (∃ z ∈ᵣ y₁, x ≡ z + y₂) ∨ (∃ z ∈ᵣ y₂, x ≡ y₁ + z) := by obtain ⟨y₁l, y₁r, y₁L, y₁R⟩ := y₁ obtain ⟨y₂l, y₂r, y₂L, y₂R⟩ := y₂ constructor · rintro ⟨(i | i), hi⟩ exacts [.inl ⟨y₁R i, moveRight_memᵣ _ _, hi⟩, .inr ⟨y₂R i, moveRight_memᵣ _ _, hi⟩] · rintro (⟨_, ⟨i, hi⟩, h⟩ | ⟨_, ⟨i, hi⟩, h⟩) exacts [⟨.inl i, h.trans hi.add_right⟩, ⟨.inr i, h.trans hi.add_left⟩] /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w + y` has the same moves as `x + z`. -/ def Relabelling.addCongr : ∀ {w x y z : PGame.{u}}, w ≡r x → y ≡r z → w + y ≡r x + z | ⟨wl, wr, wL, wR⟩, ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩, ⟨zl, zr, zL, zR⟩, ⟨L₁, R₁, hL₁, hR₁⟩, ⟨L₂, R₂, hL₂, hR₂⟩ => by let Hwx : ⟨wl, wr, wL, wR⟩ ≡r ⟨xl, xr, xL, xR⟩ := ⟨L₁, R₁, hL₁, hR₁⟩ let Hyz : ⟨yl, yr, yL, yR⟩ ≡r ⟨zl, zr, zL, zR⟩ := ⟨L₂, R₂, hL₂, hR₂⟩ refine ⟨Equiv.sumCongr L₁ L₂, Equiv.sumCongr R₁ R₂, ?_, ?_⟩ <;> rintro (i | j) · exact (hL₁ i).addCongr Hyz · exact Hwx.addCongr (hL₂ j) · exact (hR₁ i).addCongr Hyz · exact Hwx.addCongr (hR₂ j) termination_by _ x _ z => (x, z) instance : Sub PGame := ⟨fun x y => x + -y⟩ @[simp] theorem sub_zero_eq_add_zero (x : PGame) : x - 0 = x + 0 := show x + -0 = x + 0 by rw [neg_zero] protected lemma sub_zero (x : PGame) : x - 0 ≡ x := _root_.trans (of_eq x.sub_zero_eq_add_zero) x.add_zero /-- This lemma is named to match `neg_sub'`. -/ protected lemma neg_sub' (x y : PGame) : -(x - y) = -x - -y := PGame.neg_add _ _ /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w - y` has the same moves as `x - z`. -/ lemma Identical.sub {x₁ x₂ y₁ y₂ : PGame.{u}} (hx : x₁ ≡ x₂) (hy : y₁ ≡ y₂) : x₁ - y₁ ≡ x₂ - y₂ := hx.add hy.neg /-- If `w` has the same moves as `x` and `y` has the same moves as `z`, then `w - y` has the same moves as `x - z`. -/ def Relabelling.subCongr {w x y z : PGame} (h₁ : w ≡r x) (h₂ : y ≡r z) : w - y ≡r x - z := h₁.addCongr h₂.negCongr /-- `-(x + y)` has exactly the same moves as `-x + -y`. -/ def negAddRelabelling : ∀ x y : PGame, -(x + y) ≡r -x + -y | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩ => by refine ⟨Equiv.refl _, Equiv.refl _, ?_, ?_⟩ all_goals exact fun j => Sum.casesOn j (fun j => negAddRelabelling _ _) fun j => negAddRelabelling ⟨xl, xr, xL, xR⟩ _ termination_by x y => (x, y) theorem neg_add_le {x y : PGame} : -(x + y) ≤ -x + -y := (x.neg_add y).le /-- `x + y` has exactly the same moves as `y + x`. -/ def addCommRelabelling : ∀ x y : PGame.{u}, x + y ≡r y + x | mk xl xr xL xR, mk yl yr yL yR => by refine ⟨Equiv.sumComm _ _, Equiv.sumComm _ _, ?_, ?_⟩ <;> rintro (_ | _) <;> · dsimp apply addCommRelabelling termination_by x y => (x, y) theorem add_comm_le {x y : PGame} : x + y ≤ y + x := (x.add_comm y).le theorem add_comm_equiv {x y : PGame} : x + y ≈ y + x := (x.add_comm y).equiv /-- `(x + y) + z` has exactly the same moves as `x + (y + z)`. -/ def addAssocRelabelling : ∀ x y z : PGame.{u}, x + y + z ≡r x + (y + z) | ⟨xl, xr, xL, xR⟩, ⟨yl, yr, yL, yR⟩, ⟨zl, zr, zL, zR⟩ => by refine ⟨Equiv.sumAssoc _ _ _, Equiv.sumAssoc _ _ _, ?_, ?_⟩ · rintro (⟨i | i⟩ | i) · apply addAssocRelabelling · apply addAssocRelabelling ⟨xl, xr, xL, xR⟩ (yL i) · apply addAssocRelabelling ⟨xl, xr, xL, xR⟩ ⟨yl, yr, yL, yR⟩ (zL i) · rintro (⟨i | i⟩ | i) · apply addAssocRelabelling · apply addAssocRelabelling ⟨xl, xr, xL, xR⟩ (yR i) · apply addAssocRelabelling ⟨xl, xr, xL, xR⟩ ⟨yl, yr, yL, yR⟩ (zR i) termination_by x y z => (x, y, z) theorem add_assoc_equiv {x y z : PGame} : x + y + z ≈ x + (y + z) := (x.add_assoc y z).equiv theorem neg_add_cancel_le_zero : ∀ x : PGame, -x + x ≤ 0 | ⟨xl, xr, xL, xR⟩ => le_zero.2 fun i => by obtain i | i := i · -- If Left played in -x, Right responds with the same move in x. refine ⟨@toRightMovesAdd _ ⟨_, _, _, _⟩ (Sum.inr i), ?_⟩ convert @neg_add_cancel_le_zero (xR i) apply add_moveRight_inr · -- If Left in x, Right responds with the same move in -x. dsimp refine ⟨@toRightMovesAdd ⟨_, _, _, _⟩ _ (Sum.inl i), ?_⟩ convert @neg_add_cancel_le_zero (xL i) apply add_moveRight_inl theorem zero_le_neg_add_cancel (x : PGame) : 0 ≤ -x + x := by rw [← neg_le_neg_iff, neg_zero] exact neg_add_le.trans (neg_add_cancel_le_zero _) theorem neg_add_cancel_equiv (x : PGame) : -x + x ≈ 0 := ⟨neg_add_cancel_le_zero x, zero_le_neg_add_cancel x⟩ theorem add_neg_cancel_le_zero (x : PGame) : x + -x ≤ 0 := add_comm_le.trans (neg_add_cancel_le_zero x) theorem zero_le_add_neg_cancel (x : PGame) : 0 ≤ x + -x := (zero_le_neg_add_cancel x).trans add_comm_le theorem add_neg_cancel_equiv (x : PGame) : x + -x ≈ 0 := ⟨add_neg_cancel_le_zero x, zero_le_add_neg_cancel x⟩ theorem sub_self_equiv : ∀ (x : PGame), x - x ≈ 0 := add_neg_cancel_equiv private theorem add_le_add_right' : ∀ {x y z : PGame}, x ≤ y → x + z ≤ y + z | mk xl xr xL xR, mk yl yr yL yR, mk zl zr zL zR => fun h => by refine le_def.2 ⟨fun i => ?_, fun i => ?_⟩ <;> obtain i | i := i · rw [le_def] at h obtain ⟨h_left, h_right⟩ := h rcases h_left i with (⟨i', ih⟩ | ⟨j, jh⟩) · exact Or.inl ⟨toLeftMovesAdd (Sum.inl i'), add_le_add_right' ih⟩ · refine Or.inr ⟨toRightMovesAdd (Sum.inl j), ?_⟩ convert add_le_add_right' jh apply add_moveRight_inl · exact Or.inl ⟨@toLeftMovesAdd _ ⟨_, _, _, _⟩ (Sum.inr i), add_le_add_right' h⟩ · rw [le_def] at h rcases h.right i with (⟨i, ih⟩ | ⟨j', jh⟩) · refine Or.inl ⟨toLeftMovesAdd (Sum.inl i), ?_⟩ convert add_le_add_right' ih apply add_moveLeft_inl · exact Or.inr ⟨toRightMovesAdd (Sum.inl j'), add_le_add_right' jh⟩ · exact Or.inr ⟨@toRightMovesAdd _ ⟨_, _, _, _⟩ (Sum.inr i), add_le_add_right' h⟩ termination_by x y z => (x, y, z) instance addRightMono : AddRightMono PGame := ⟨fun _ _ _ => add_le_add_right'⟩ instance addLeftMono : AddLeftMono PGame := ⟨fun x _ _ h => (add_comm_le.trans (by gcongr)).trans add_comm_le⟩ theorem add_lf_add_right {y z : PGame} (h : y ⧏ z) (x) : y + x ⧏ z + x := suffices z + x ≤ y + x → z ≤ y by rw [← PGame.not_le] at h ⊢ exact mt this h fun w => calc z ≤ z + 0 := (PGame.add_zero _).symm.le _ ≤ z + (x + -x) := by grw [zero_le_add_neg_cancel] _ ≤ z + x + -x := (PGame.add_assoc _ _ _).symm.le _ ≤ y + x + -x := by gcongr _ ≤ y + (x + -x) := (PGame.add_assoc _ _ _).le _ ≤ y + 0 := by grw [add_neg_cancel_le_zero] _ ≤ y := (PGame.add_zero _).le theorem add_lf_add_left {y z : PGame} (h : y ⧏ z) (x) : x + y ⧏ x + z := by rw [lf_congr add_comm_equiv add_comm_equiv] apply add_lf_add_right h instance addRightStrictMono : AddRightStrictMono PGame := ⟨fun x _ _ h => ⟨add_le_add_right h.1 x, add_lf_add_right h.2 x⟩⟩ instance addLeftStrictMono : AddLeftStrictMono PGame := ⟨fun x _ _ h => ⟨add_le_add_left h.1 x, add_lf_add_left h.2 x⟩⟩ theorem add_lf_add_of_lf_of_le {w x y z : PGame} (hwx : w ⧏ x) (hyz : y ≤ z) : w + y ⧏ x + z := lf_of_lf_of_le (add_lf_add_right hwx y) (add_le_add_left hyz x) theorem add_lf_add_of_le_of_lf {w x y z : PGame} (hwx : w ≤ x) (hyz : y ⧏ z) : w + y ⧏ x + z := lf_of_le_of_lf (add_le_add_right hwx y) (add_lf_add_left hyz x) theorem add_congr {w x y z : PGame} (h₁ : w ≈ x) (h₂ : y ≈ z) : w + y ≈ x + z := ⟨add_le_add h₁.1 h₂.1, add_le_add h₁.2 h₂.2⟩ theorem add_congr_left {x y z : PGame} (h : x ≈ y) : x + z ≈ y + z := add_congr h equiv_rfl theorem add_congr_right {x y z : PGame} : (y ≈ z) → (x + y ≈ x + z) := add_congr equiv_rfl theorem sub_congr {w x y z : PGame} (h₁ : w ≈ x) (h₂ : y ≈ z) : w - y ≈ x - z := add_congr h₁ (neg_equiv_neg_iff.2 h₂) theorem sub_congr_left {x y z : PGame} (h : x ≈ y) : x - z ≈ y - z := sub_congr h equiv_rfl theorem sub_congr_right {x y z : PGame} : (y ≈ z) → (x - y ≈ x - z) := sub_congr equiv_rfl theorem le_iff_sub_nonneg {x y : PGame} : x ≤ y ↔ 0 ≤ y - x := ⟨fun h => (zero_le_add_neg_cancel x).trans (add_le_add_right h _), fun h => calc x ≤ 0 + x := (PGame.zero_add x).symm.le _ ≤ y - x + x := by gcongr _ ≤ y + (-x + x) := (PGame.add_assoc _ _ _).le _ ≤ y + 0 := add_le_add_left (neg_add_cancel_le_zero x) _ _ ≤ y := (PGame.add_zero y).le ⟩ theorem lf_iff_sub_zero_lf {x y : PGame} : x ⧏ y ↔ 0 ⧏ y - x := ⟨fun h => (zero_le_add_neg_cancel x).trans_lf (add_lf_add_right h _), fun h => calc x ≤ 0 + x := (PGame.zero_add x).symm.le _ ⧏ y - x + x := add_lf_add_right h _ _ ≤ y + (-x + x) := (PGame.add_assoc _ _ _).le _ ≤ y + 0 := add_le_add_left (neg_add_cancel_le_zero x) _ _ ≤ y := (PGame.add_zero y).le ⟩ theorem lt_iff_sub_pos {x y : PGame} : x < y ↔ 0 < y - x := ⟨fun h => lt_of_le_of_lt (zero_le_add_neg_cancel x) (add_lt_add_right h _), fun h => calc x ≤ 0 + x := (PGame.zero_add x).symm.le _ < y - x + x := add_lt_add_right h _ _ ≤ y + (-x + x) := (PGame.add_assoc _ _ _).le _ ≤ y + 0 := by gcongr; exact neg_add_cancel_le_zero x _ ≤ y := (PGame.add_zero y).le ⟩ /-! ### Interaction of option insertion with negation -/ theorem neg_insertRight_neg (x x' : PGame.{u}) : (-x).insertRight (-x') = -x.insertLeft x' := by cases x cases x' dsimp [insertRight, insertLeft] congr! with (i | j) theorem neg_insertLeft_neg (x x' : PGame.{u}) : (-x).insertLeft (-x') = -x.insertRight x' := by rw [← neg_eq_iff_eq_neg, ← neg_insertRight_neg, neg_neg, neg_neg] /-! ### Special pre-games -/ /-- The pre-game `star`, which is fuzzy with zero. -/ def star : PGame.{u} := ⟨PUnit, PUnit, fun _ => 0, fun _ => 0⟩ @[simp] theorem star_leftMoves : star.LeftMoves = PUnit := rfl @[simp] theorem star_rightMoves : star.RightMoves = PUnit := rfl @[simp] theorem star_moveLeft (x) : star.moveLeft x = 0 := rfl @[simp] theorem star_moveRight (x) : star.moveRight x = 0 := rfl instance uniqueStarLeftMoves : Unique star.LeftMoves := PUnit.instUnique instance uniqueStarRightMoves : Unique star.RightMoves := PUnit.instUnique theorem zero_lf_star : 0 ⧏ star := by rw [zero_lf] use default rintro ⟨⟩ theorem star_lf_zero : star ⧏ 0 := by rw [lf_zero] use default rintro ⟨⟩ theorem star_fuzzy_zero : star ‖ 0 := ⟨star_lf_zero, zero_lf_star⟩ @[simp] theorem neg_star : -star = star := by simp [star] @[simp] protected theorem zero_lt_one : (0 : PGame) < 1 := lt_of_le_of_lf (zero_le_of_isEmpty_rightMoves 1) (zero_lf_le.2 ⟨default, le_rfl⟩) /-- The pre-game `up` -/ def up : PGame.{u} := ⟨PUnit, PUnit, fun _ => 0, fun _ => star⟩ @[simp] theorem up_leftMoves : up.LeftMoves = PUnit := rfl @[simp] theorem up_rightMoves : up.RightMoves = PUnit := rfl @[simp] theorem up_moveLeft (x) : up.moveLeft x = 0 := rfl @[simp] theorem up_moveRight (x) : up.moveRight x = star := rfl @[simp] theorem up_neg : 0 < up := by rw [lt_iff_le_and_lf, zero_lf] simp [zero_le_lf, zero_lf_star] theorem star_fuzzy_up : star ‖ up := by unfold Fuzzy simp only [← PGame.not_le] simp [le_iff_forall_lf] /-- The pre-game `down` -/ def down : PGame.{u} := ⟨PUnit, PUnit, fun _ => star, fun _ => 0⟩ @[simp] theorem down_leftMoves : down.LeftMoves = PUnit := rfl @[simp] theorem down_rightMoves : down.RightMoves = PUnit := rfl @[simp] theorem down_moveLeft (x) : down.moveLeft x = star := rfl @[simp] theorem down_moveRight (x) : down.moveRight x = 0 := rfl @[simp] theorem down_neg : down < 0 := by rw [lt_iff_le_and_lf, lf_zero] simp [le_zero_lf, star_lf_zero] @[simp] theorem neg_down : -down = up := by simp [up, down] @[simp] theorem neg_up : -up = down := by simp [up, down] theorem star_fuzzy_down : star ‖ down := by rw [← neg_fuzzy_neg_iff, neg_down, neg_star] exact star_fuzzy_up instance : ZeroLEOneClass PGame := ⟨PGame.zero_lt_one.le⟩ @[simp] theorem zero_lf_one : (0 : PGame) ⧏ 1 := PGame.zero_lt_one.lf end SetTheory.PGame
.lake/packages/mathlib/Mathlib/Data/Subtype.lean
import Mathlib.Logic.Function.Basic import Mathlib.Tactic.AdaptationNote import Mathlib.Tactic.Simps.Basic /-! # Subtypes This file provides basic API for subtypes, which are defined in core. A subtype is a type made from restricting another type, say `α`, to its elements that satisfy some predicate, say `p : α → Prop`. Specifically, it is the type of pairs `⟨val, property⟩` where `val : α` and `property : p val`. It is denoted `Subtype p` and notation `{val : α // p val}` is available. A subtype has a natural coercion to the parent type, by coercing `⟨val, property⟩` to `val`. As such, subtypes can be thought of as bundled sets, the difference being that elements of a set are still of type `α` while elements of a subtype aren't. -/ open Function namespace Subtype variable {α β γ : Sort*} {p q : α → Prop} attribute [coe] Subtype.val initialize_simps_projections Subtype (val → coe) /-- A version of `x.property` or `x.2` where `p` is syntactically applied to the coercion of `x` instead of `x.1`. A similar result is `Subtype.mem` in `Mathlib/Data/Set/Basic.lean`. -/ -- This is a leftover from Lean 3: it is identical to `Subtype.property`, and should be deprecated. theorem prop (x : Subtype p) : p x := x.2 /-- An alternative version of `Subtype.forall`. This one is useful if Lean cannot figure out `q` when using `Subtype.forall` from right to left. -/ protected theorem forall' {q : ∀ x, p x → Prop} : (∀ x h, q x h) ↔ ∀ x : { a // p a }, q x x.2 := (@Subtype.forall _ _ fun x ↦ q x.1 x.2).symm /-- An alternative version of `Subtype.exists`. This one is useful if Lean cannot figure out `q` when using `Subtype.exists` from right to left. -/ protected theorem exists' {q : ∀ x, p x → Prop} : (∃ x h, q x h) ↔ ∃ x : { a // p a }, q x x.2 := (@Subtype.exists _ _ fun x ↦ q x.1 x.2).symm theorem heq_iff_coe_eq (h : ∀ x, p x ↔ q x) {a1 : { x // p x }} {a2 : { x // q x }} : a1 ≍ a2 ↔ (a1 : α) = (a2 : α) := Eq.rec (motive := fun (pp : (α → Prop)) _ ↦ ∀ a2' : {x // pp x}, a1 ≍ a2' ↔ (a1 : α) = (a2' : α)) (by grind) (funext <| fun x ↦ propext (h x)) a2 lemma heq_iff_coe_heq {α β : Sort _} {p : α → Prop} {q : β → Prop} {a : {x // p x}} {b : {y // q y}} (h : α = β) (h' : p ≍ q) : a ≍ b ↔ (a : α) ≍ (b : β) := by subst h subst h' grind @[deprecated Subtype.ext (since := "2025-09-10")] theorem ext_val {a1 a2 : { x // p x }} : a1.1 = a2.1 → a1 = a2 := Subtype.ext @[deprecated Subtype.ext_iff (since := "2025-09-10")] theorem ext_iff_val {a1 a2 : { x // p x }} : a1 = a2 ↔ a1.1 = a2.1 := Subtype.ext_iff @[simp] theorem coe_eta (a : { a // p a }) (h : p a) : mk (↑a) h = a := Subtype.ext rfl theorem coe_mk (a h) : (@mk α p a h : α) = a := rfl /-- Restatement of `Subtype.mk.injEq` as an iff. -/ theorem mk_eq_mk {a h a' h'} : @mk α p a h = @mk α p a' h' ↔ a = a' := by simp theorem coe_eq_of_eq_mk {a : { a // p a }} {b : α} (h : ↑a = b) : a = ⟨b, h ▸ a.2⟩ := Subtype.ext h theorem coe_eq_iff {a : { a // p a }} {b : α} : ↑a = b ↔ ∃ h, a = ⟨b, h⟩ := by grind theorem coe_injective : Injective (fun (a : Subtype p) ↦ (a : α)) := fun _ _ ↦ Subtype.ext @[simp] theorem val_injective : Injective (@val _ p) := coe_injective theorem coe_inj {a b : Subtype p} : (a : α) = b ↔ a = b := coe_injective.eq_iff theorem val_inj {a b : Subtype p} : a.val = b.val ↔ a = b := coe_inj lemma coe_ne_coe {a b : Subtype p} : (a : α) ≠ b ↔ a ≠ b := coe_injective.ne_iff @[simp] theorem _root_.exists_eq_subtype_mk_iff {a : Subtype p} {b : α} : (∃ h : p b, a = Subtype.mk b h) ↔ ↑a = b := coe_eq_iff.symm @[simp] theorem _root_.exists_subtype_mk_eq_iff {a : Subtype p} {b : α} : (∃ h : p b, Subtype.mk b h = a) ↔ b = a := by grind theorem _root_.Function.extend_val_apply {p : β → Prop} {g : {x // p x} → γ} {j : β → γ} {b : β} (hb : p b) : val.extend g j b = g ⟨b, hb⟩ := val_injective.extend_apply g j ⟨b, hb⟩ theorem _root_.Function.extend_val_apply' {p : β → Prop} {g : {x // p x} → γ} {j : β → γ} {b : β} (hb : ¬p b) : val.extend g j b = j b := by grind [Function.extend] /-- Restrict a (dependent) function to a subtype -/ def restrict {α} {β : α → Type*} (p : α → Prop) (f : ∀ x, β x) (x : Subtype p) : β x.1 := f x @[simp, grind =] theorem restrict_apply {α} {β : α → Type*} (f : ∀ x, β x) (p : α → Prop) (x : Subtype p) : restrict p f x = f x.1 := by rfl theorem restrict_def {α β} (f : α → β) (p : α → Prop) : restrict p f = f ∘ (fun (a : Subtype p) ↦ a) := rfl theorem restrict_injective {α β} {f : α → β} (p : α → Prop) (h : Injective f) : Injective (restrict p f) := h.comp coe_injective theorem surjective_restrict {α} {β : α → Type*} [ne : ∀ a, Nonempty (β a)] (p : α → Prop) : Surjective fun f : ∀ x, β x ↦ restrict p f := by classical exact fun f ↦ ⟨fun x ↦ if h : p x then f ⟨x, h⟩ else Nonempty.some (ne x), by grind⟩ /-- Defining a map into a subtype, this can be seen as a "coinduction principle" of `Subtype` -/ @[simps] def coind {α β} (f : α → β) {p : β → Prop} (h : ∀ a, p (f a)) : α → Subtype p := fun a ↦ ⟨f a, h a⟩ theorem coind_injective {α β} {f : α → β} {p : β → Prop} (h : ∀ a, p (f a)) (hf : Injective f) : Injective (coind f h) := fun x y hxy ↦ hf <| by apply congr_arg Subtype.val hxy /-- Restriction of a function to a function on subtypes. -/ @[simps] def map {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀ a, p a → q (f a)) : Subtype p → Subtype q := fun x ↦ ⟨f x, h x x.prop⟩ theorem map_def {p : α → Prop} {q : β → Prop} (f : α → β) (h : ∀ a, p a → q (f a)) : map f h = fun x ↦ ⟨f x, h x x.prop⟩ := rfl theorem map_comp {p : α → Prop} {q : β → Prop} {r : γ → Prop} {x : Subtype p} (f : α → β) (h : ∀ a, p a → q (f a)) (g : β → γ) (l : ∀ a, q a → r (g a)) : map g l (map f h x) = map (g ∘ f) (fun a ha ↦ l (f a) <| h a ha) x := rfl theorem map_id {p : α → Prop} {h : ∀ a, p a → p (id a)} : map (@id α) h = id := funext fun _ ↦ rfl theorem map_injective {p : α → Prop} {q : β → Prop} {f : α → β} (h : ∀ a, p a → q (f a)) (hf : Injective f) : Injective (map f h) := coind_injective _ <| hf.comp coe_injective theorem map_involutive {p : α → Prop} {f : α → α} (h : ∀ a, p a → p (f a)) (hf : Involutive f) : Involutive (map f h) := fun x ↦ Subtype.ext (hf x) theorem map_eq {p : α → Prop} {q : β → Prop} {f g : α → β} (h₁ : ∀ a : α, p a → q (f a)) (h₂ : ∀ a : α, p a → q (g a)) {x y : Subtype p} : map f h₁ x = map g h₂ y ↔ f x = g y := Subtype.ext_iff theorem map_ne {p : α → Prop} {q : β → Prop} {f g : α → β} (h₁ : ∀ a : α, p a → q (f a)) (h₂ : ∀ a : α, p a → q (g a)) {x y : Subtype p} : map f h₁ x ≠ map g h₂ y ↔ f x ≠ g y := map_eq h₁ h₂ |>.not instance [HasEquiv α] (p : α → Prop) : HasEquiv (Subtype p) := ⟨fun s t ↦ (s : α) ≈ (t : α)⟩ theorem equiv_iff [HasEquiv α] {p : α → Prop} {s t : Subtype p} : s ≈ t ↔ (s : α) ≈ (t : α) := Iff.rfl variable [Setoid α] protected theorem refl (s : Subtype p) : s ≈ s := Setoid.refl _ protected theorem symm {s t : Subtype p} (h : s ≈ t) : t ≈ s := Setoid.symm h protected theorem trans {s t u : Subtype p} (h₁ : s ≈ t) (h₂ : t ≈ u) : s ≈ u := Setoid.trans h₁ h₂ theorem equivalence (p : α → Prop) : Equivalence (@HasEquiv.Equiv (Subtype p) _) := .mk (Subtype.refl) (@Subtype.symm _ p _) (@Subtype.trans _ p _) instance (p : α → Prop) : Setoid (Subtype p) := Setoid.mk (· ≈ ·) (equivalence p) end Subtype namespace Subtype /-! Some facts about sets, which require that `α` is a type. -/ variable {α : Type*} @[simp] theorem coe_prop {S : Set α} (a : { a // a ∈ S }) : ↑a ∈ S := a.prop theorem val_prop {S : Set α} (a : { a // a ∈ S }) : a.val ∈ S := a.prop end Subtype
.lake/packages/mathlib/Mathlib/Data/Ineq.lean
import Mathlib.Lean.Expr.Basic /-! # `Ineq` datatype This file contains an enum `Ineq` (whose constructors are `eq`, `le`, `lt`), and operations involving it. The type `Ineq` is one of the fundamental objects manipulated by the `linarith` and `linear_combination` tactics. -/ open Lean Meta namespace Mathlib /-! ### Inequalities -/ /-- The three-element type `Ineq` is used to represent the strength of a comparison between terms. -/ inductive Ineq : Type | eq | le | lt deriving DecidableEq, Inhabited, Repr namespace Ineq /-- `max R1 R2` computes the strength of the sum of two inequalities. If `t1 R1 0` and `t2 R2 0`, then `t1 + t2 (max R1 R2) 0`. -/ def max : Ineq → Ineq → Ineq | lt, _ => lt | _, lt => lt | le, _ => le | _, le => le | eq, eq => eq /-- `Ineq` is ordered `eq < le < lt`. -/ def cmp : Ineq → Ineq → Ordering | eq, eq => Ordering.eq | eq, _ => Ordering.lt | le, le => Ordering.eq | le, lt => Ordering.lt | lt, lt => Ordering.eq | _, _ => Ordering.gt /-- Prints an `Ineq` as the corresponding infix symbol. -/ def toString : Ineq → String | eq => "=" | le => "≤" | lt => "<" instance : ToString Ineq := ⟨toString⟩ instance : ToFormat Ineq := ⟨fun i => Ineq.toString i⟩ end Mathlib.Ineq /-! ### Parsing inequalities -/ namespace Lean.Expr open Mathlib /-- Given an expression `e`, parse it as a `=`, `≤` or `<`, and return this relation (as a `Linarith.Ineq`) together with the type in which the (in)equality occurs and the two sides of the (in)equality. This function is more naturally in the `Option` monad, but it is convenient to put in `MetaM` for compositionality. -/ def ineq? (e : Expr) : MetaM (Ineq × Expr × Expr × Expr) := do let e ← whnfR (← instantiateMVars e) match e.eq? with | some p => return (Ineq.eq, p) | none => match e.le? with | some p => return (Ineq.le, p) | none => match e.lt? with | some p => return (Ineq.lt, p) | none => throwError "Not a comparison: {e}" /-- Given an expression `e`, parse it as a `=`, `≤` or `<`, or the negation of such, and return this relation (as a `Linarith.Ineq`) together with the type in which the (in)equality occurs, the two sides of the (in)equality, and a Boolean flag indicating the presence or absence of the `¬`. This function is more naturally in the `Option` monad, but it is convenient to put in `MetaM` for compositionality. -/ def ineqOrNotIneq? (e : Expr) : MetaM (Bool × Ineq × Expr × Expr × Expr) := do try return (true, ← e.ineq?) catch _ => let some e' := e.not? | throwError "Not a comparison: {e}" return (false, ← e'.ineq?) end Lean.Expr
.lake/packages/mathlib/Mathlib/Data/Quot.lean
import Mathlib.Logic.Relation import Mathlib.Logic.Unique import Mathlib.Util.Notation3 /-! # Quotient types This module extends the core library's treatment of quotient types (`Init.Core`). ## Tags quotient -/ variable {α : Sort*} {β : Sort*} namespace Setoid -- Pretty print `@Setoid.r _ s a b` as `s a b`. run_cmd Lean.Elab.Command.liftTermElabM do Lean.Meta.registerCoercion ``Setoid.r (some { numArgs := 2, coercee := 1, type := .coeFun }) /-- When writing a lemma about `someSetoid x y` (which uses this instance), call it `someSetoid_apply` not `someSetoid_r`. -/ instance : CoeFun (Setoid α) (fun _ ↦ α → α → Prop) where coe := @Setoid.r _ theorem ext {α : Sort*} : ∀ {s t : Setoid α}, (∀ a b, s a b ↔ t a b) → s = t | ⟨r, _⟩, ⟨p, _⟩, Eq => by have : r = p := funext fun a ↦ funext fun b ↦ propext <| Eq a b subst this rfl end Setoid namespace Quot variable {ra : α → α → Prop} {rb : β → β → Prop} {φ : Quot ra → Quot rb → Sort*} @[inherit_doc Quot.mk] local notation3:arg "⟦" a "⟧" => Quot.mk _ a @[elab_as_elim] protected theorem induction_on {α : Sort*} {r : α → α → Prop} {β : Quot r → Prop} (q : Quot r) (h : ∀ a, β (Quot.mk r a)) : β q := ind h q instance (r : α → α → Prop) [Inhabited α] : Inhabited (Quot r) := ⟨⟦default⟧⟩ protected instance Subsingleton [Subsingleton α] : Subsingleton (Quot ra) := ⟨fun x ↦ Quot.induction_on x fun _ ↦ Quot.ind fun _ ↦ congr_arg _ (Subsingleton.elim _ _)⟩ instance [Unique α] : Unique (Quot ra) := Unique.mk' _ /-- Recursion on two `Quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrecOn₂ (qa : Quot ra) (qb : Quot rb) (f : ∀ a b, φ ⟦a⟧ ⟦b⟧) (ca : ∀ {b a₁ a₂}, ra a₁ a₂ → f a₁ b ≍ f a₂ b) (cb : ∀ {a b₁ b₂}, rb b₁ b₂ → f a b₁ ≍ f a b₂) : φ qa qb := Quot.hrecOn (motive := fun qa ↦ φ qa qb) qa (fun a ↦ Quot.hrecOn qb (f a) (fun _ _ pb ↦ cb pb)) fun a₁ a₂ pa ↦ Quot.induction_on qb fun b ↦ have h₁ : @Quot.hrecOn _ _ (φ _) ⟦b⟧ (f a₁) (@cb _) ≍ f a₁ b := by simp have h₂ : f a₂ b ≍ @Quot.hrecOn _ _ (φ _) ⟦b⟧ (f a₂) (@cb _) := by simp (h₁.trans (ca pa)).trans h₂ /-- Map a function `f : α → β` such that `ra x y` implies `rb (f x) (f y)` to a map `Quot ra → Quot rb`. -/ protected def map (f : α → β) (h : ∀ ⦃a b : α⦄, ra a b → rb (f a) (f b)) : Quot ra → Quot rb := Quot.lift (fun x => Quot.mk rb (f x)) fun _ _ hra ↦ Quot.sound <| h hra /-- If `ra` is a subrelation of `ra'`, then we have a natural map `Quot ra → Quot ra'`. -/ protected def mapRight {ra' : α → α → Prop} (h : ∀ a₁ a₂, ra a₁ a₂ → ra' a₁ a₂) : Quot ra → Quot ra' := Quot.map id h /-- Weaken the relation of a quotient. This is the same as `Quot.map id`. -/ def factor {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) : Quot r → Quot s := Quot.lift (Quot.mk s) fun x y rxy ↦ Quot.sound (h x y rxy) theorem factor_mk_eq {α : Type*} (r s : α → α → Prop) (h : ∀ x y, r x y → s x y) : factor r s h ∘ Quot.mk _ = Quot.mk _ := rfl variable {γ : Sort*} {r : α → α → Prop} {s : β → β → Prop} theorem lift_mk (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) (a : α) : Quot.lift f h (Quot.mk r a) = f a := rfl theorem liftOn_mk (a : α) (f : α → γ) (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) : Quot.liftOn (Quot.mk r a) f h = f a := rfl @[simp] theorem surjective_lift {f : α → γ} (h : ∀ a₁ a₂, r a₁ a₂ → f a₁ = f a₂) : Function.Surjective (lift f h) ↔ Function.Surjective f := ⟨fun hf => hf.comp Quot.exists_rep, fun hf y => let ⟨x, hx⟩ := hf y; ⟨Quot.mk _ x, hx⟩⟩ /-- Descends a function `f : α → β → γ` to quotients of `α` and `β`. -/ protected def lift₂ (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) (q₁ : Quot r) (q₂ : Quot s) : γ := Quot.lift (fun a ↦ Quot.lift (f a) (hr a)) (fun a₁ a₂ ha ↦ funext fun q ↦ Quot.induction_on q fun b ↦ hs a₁ a₂ b ha) q₁ q₂ @[simp] theorem lift₂_mk (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) (a : α) (b : β) : Quot.lift₂ f hr hs (Quot.mk r a) (Quot.mk s b) = f a b := rfl /-- Descends a function `f : α → β → γ` to quotients of `α` and `β` and applies it. -/ protected def liftOn₂ (p : Quot r) (q : Quot s) (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : γ := Quot.lift₂ f hr hs p q @[simp] theorem liftOn₂_mk (a : α) (b : β) (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hs : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) : Quot.liftOn₂ (Quot.mk r a) (Quot.mk s b) f hr hs = f a b := rfl variable {t : γ → γ → Prop} /-- Descends a function `f : α → β → γ` to quotients of `α` and `β` with values in a quotient of `γ`. -/ protected def map₂ (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂)) (hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b)) (q₁ : Quot r) (q₂ : Quot s) : Quot t := Quot.lift₂ (fun a b ↦ Quot.mk t <| f a b) (fun a b₁ b₂ hb ↦ Quot.sound (hr a b₁ b₂ hb)) (fun a₁ a₂ b ha ↦ Quot.sound (hs a₁ a₂ b ha)) q₁ q₂ @[simp] theorem map₂_mk (f : α → β → γ) (hr : ∀ a b₁ b₂, s b₁ b₂ → t (f a b₁) (f a b₂)) (hs : ∀ a₁ a₂ b, r a₁ a₂ → t (f a₁ b) (f a₂ b)) (a : α) (b : β) : Quot.map₂ f hr hs (Quot.mk r a) (Quot.mk s b) = Quot.mk t (f a b) := rfl /-- A binary version of `Quot.recOnSubsingleton`. -/ @[elab_as_elim] protected def recOnSubsingleton₂ {φ : Quot r → Quot s → Sort*} [h : ∀ a b, Subsingleton (φ ⟦a⟧ ⟦b⟧)] (q₁ : Quot r) (q₂ : Quot s) (f : ∀ a b, φ ⟦a⟧ ⟦b⟧) : φ q₁ q₂ := @Quot.recOnSubsingleton _ r (fun q ↦ φ q q₂) (fun a ↦ Quot.ind (β := fun b ↦ Subsingleton (φ (mk r a) b)) (h a) q₂) q₁ fun a ↦ Quot.recOnSubsingleton q₂ fun b ↦ f a b @[elab_as_elim] protected theorem induction_on₂ {δ : Quot r → Quot s → Prop} (q₁ : Quot r) (q₂ : Quot s) (h : ∀ a b, δ (Quot.mk r a) (Quot.mk s b)) : δ q₁ q₂ := Quot.ind (β := fun a ↦ δ a q₂) (fun a₁ ↦ Quot.ind (fun a₂ ↦ h a₁ a₂) q₂) q₁ @[elab_as_elim] protected theorem induction_on₃ {δ : Quot r → Quot s → Quot t → Prop} (q₁ : Quot r) (q₂ : Quot s) (q₃ : Quot t) (h : ∀ a b c, δ (Quot.mk r a) (Quot.mk s b) (Quot.mk t c)) : δ q₁ q₂ q₃ := Quot.ind (β := fun a ↦ δ a q₂ q₃) (fun a₁ ↦ Quot.ind (β := fun b ↦ δ _ b q₃) (fun a₂ ↦ Quot.ind (fun a₃ ↦ h a₁ a₂ a₃) q₃) q₂) q₁ instance lift.decidablePred (r : α → α → Prop) (f : α → Prop) (h : ∀ a b, r a b → f a = f b) [hf : DecidablePred f] : DecidablePred (Quot.lift f h) := fun q ↦ Quot.recOnSubsingleton (motive := fun _ ↦ Decidable _) q hf /-- Note that this provides `DecidableRel (Quot.Lift₂ f ha hb)` when `α = β`. -/ instance lift₂.decidablePred (r : α → α → Prop) (s : β → β → Prop) (f : α → β → Prop) (ha : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hb : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) [hf : ∀ a, DecidablePred (f a)] (q₁ : Quot r) : DecidablePred (Quot.lift₂ f ha hb q₁) := fun q₂ ↦ Quot.recOnSubsingleton₂ q₁ q₂ hf instance (r : α → α → Prop) (q : Quot r) (f : α → Prop) (h : ∀ a b, r a b → f a = f b) [DecidablePred f] : Decidable (Quot.liftOn q f h) := Quot.lift.decidablePred _ _ _ _ instance (r : α → α → Prop) (s : β → β → Prop) (q₁ : Quot r) (q₂ : Quot s) (f : α → β → Prop) (ha : ∀ a b₁ b₂, s b₁ b₂ → f a b₁ = f a b₂) (hb : ∀ a₁ a₂ b, r a₁ a₂ → f a₁ b = f a₂ b) [∀ a, DecidablePred (f a)] : Decidable (Quot.liftOn₂ q₁ q₂ f ha hb) := Quot.lift₂.decidablePred _ _ _ _ _ _ _ end Quot namespace Quotient variable {sa : Setoid α} {sb : Setoid β} variable {φ : Quotient sa → Quotient sb → Sort*} -- TODO: in mathlib3 this notation took the Setoid as an instance-implicit argument, -- now it's explicit but left as a metavariable. -- We have not yet decided which one works best, since the setoid instance can't always be -- reliably found but it can't always be inferred from the expected type either. -- See also: https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/confusion.20between.20equivalence.20and.20instance.20setoid/near/360822354 @[inherit_doc Quotient.mk] notation3:arg "⟦" a "⟧" => Quotient.mk _ a instance instInhabitedQuotient (s : Setoid α) [Inhabited α] : Inhabited (Quotient s) := ⟨⟦default⟧⟩ instance instSubsingletonQuotient (s : Setoid α) [Subsingleton α] : Subsingleton (Quotient s) := Quot.Subsingleton instance instUniqueQuotient (s : Setoid α) [Unique α] : Unique (Quotient s) := Unique.mk' _ instance {α : Type*} [Setoid α] : IsEquiv α (· ≈ ·) where refl := Setoid.refl symm _ _ := Setoid.symm trans _ _ _ := Setoid.trans /-- Induction on two `Quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrecOn₂ (qa : Quotient sa) (qb : Quotient sb) (f : ∀ a b, φ ⟦a⟧ ⟦b⟧) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ ≍ f a₂ b₂) : φ qa qb := Quot.hrecOn₂ qa qb f (fun p ↦ c _ _ _ _ p (Setoid.refl _)) fun p ↦ c _ _ _ _ (Setoid.refl _) p /-- Map a function `f : α → β` that sends equivalent elements to equivalent elements to a function `Quotient sa → Quotient sb`. Useful to define unary operations on quotients. -/ protected def map (f : α → β) (h : ∀ ⦃a b : α⦄, a ≈ b → f a ≈ f b) : Quotient sa → Quotient sb := Quot.map f h @[simp] theorem map_mk (f : α → β) (h) (x : α) : Quotient.map f h (⟦x⟧ : Quotient sa) = (⟦f x⟧ : Quotient sb) := rfl variable {γ : Sort*} {sc : Setoid γ} /-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements to a function `f : Quotient sa → Quotient sb → Quotient sc`. Useful to define binary operations on quotients. -/ protected def map₂ (f : α → β → γ) (h : ∀ ⦃a₁ a₂⦄, a₁ ≈ a₂ → ∀ ⦃b₁ b₂⦄, b₁ ≈ b₂ → f a₁ b₁ ≈ f a₂ b₂) : Quotient sa → Quotient sb → Quotient sc := Quotient.lift₂ (fun x y ↦ ⟦f x y⟧) fun _ _ _ _ h₁ h₂ ↦ Quot.sound <| h h₁ h₂ @[simp] theorem map₂_mk (f : α → β → γ) (h) (x : α) (y : β) : Quotient.map₂ f h (⟦x⟧ : Quotient sa) (⟦y⟧ : Quotient sb) = (⟦f x y⟧ : Quotient sc) := rfl instance lift.decidablePred (f : α → Prop) (h : ∀ a b, a ≈ b → f a = f b) [DecidablePred f] : DecidablePred (Quotient.lift f h) := Quot.lift.decidablePred _ _ _ /-- Note that this provides `DecidableRel (Quotient.lift₂ f h)` when `α = β`. -/ instance lift₂.decidablePred (f : α → β → Prop) (h : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ = f a₂ b₂) [hf : ∀ a, DecidablePred (f a)] (q₁ : Quotient sa) : DecidablePred (Quotient.lift₂ f h q₁) := fun q₂ ↦ Quotient.recOnSubsingleton₂ q₁ q₂ hf instance (q : Quotient sa) (f : α → Prop) (h : ∀ a b, a ≈ b → f a = f b) [DecidablePred f] : Decidable (Quotient.liftOn q f h) := Quotient.lift.decidablePred _ _ _ instance (q₁ : Quotient sa) (q₂ : Quotient sb) (f : α → β → Prop) (h : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ = f a₂ b₂) [∀ a, DecidablePred (f a)] : Decidable (Quotient.liftOn₂ q₁ q₂ f h) := Quotient.lift₂.decidablePred _ _ _ _ end Quotient theorem Quot.eq {α : Type*} {r : α → α → Prop} {x y : α} : Quot.mk r x = Quot.mk r y ↔ Relation.EqvGen r x y := ⟨Quot.eqvGen_exact, Quot.eqvGen_sound⟩ @[simp] theorem Quotient.eq {r : Setoid α} {x y : α} : Quotient.mk r x = ⟦y⟧ ↔ r x y := ⟨Quotient.exact, Quotient.sound⟩ theorem Quotient.eq_iff_equiv {r : Setoid α} {x y : α} : Quotient.mk r x = ⟦y⟧ ↔ x ≈ y := Quotient.eq theorem Quotient.forall {α : Sort*} {s : Setoid α} {p : Quotient s → Prop} : (∀ a, p a) ↔ ∀ a : α, p ⟦a⟧ := ⟨fun h _ ↦ h _, fun h a ↦ a.ind h⟩ theorem Quotient.exists {α : Sort*} {s : Setoid α} {p : Quotient s → Prop} : (∃ a, p a) ↔ ∃ a : α, p ⟦a⟧ := ⟨fun ⟨q, hq⟩ ↦ q.ind (motive := (p · → _)) .intro hq, fun ⟨a, ha⟩ ↦ ⟨⟦a⟧, ha⟩⟩ @[simp] theorem Quotient.lift_mk {s : Setoid α} (f : α → β) (h : ∀ a b : α, a ≈ b → f a = f b) (x : α) : Quotient.lift f h (Quotient.mk s x) = f x := rfl @[simp] theorem Quotient.lift_comp_mk {_ : Setoid α} (f : α → β) (h : ∀ a b : α, a ≈ b → f a = f b) : Quotient.lift f h ∘ Quotient.mk _ = f := rfl @[simp] theorem Quotient.lift_surjective_iff {α β : Sort*} {s : Setoid α} (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) : Function.Surjective (Quotient.lift f h : Quotient s → β) ↔ Function.Surjective f := Quot.surjective_lift h theorem Quotient.lift_surjective {α β : Sort*} {s : Setoid α} (f : α → β) (h : ∀ (a b : α), a ≈ b → f a = f b) (hf : Function.Surjective f) : Function.Surjective (Quotient.lift f h : Quotient s → β) := (Quot.surjective_lift h).mpr hf @[simp] theorem Quotient.lift₂_mk {α : Sort*} {β : Sort*} {γ : Sort*} {_ : Setoid α} {_ : Setoid β} (f : α → β → γ) (h : ∀ (a₁ : α) (a₂ : β) (b₁ : α) (b₂ : β), a₁ ≈ b₁ → a₂ ≈ b₂ → f a₁ a₂ = f b₁ b₂) (a : α) (b : β) : Quotient.lift₂ f h (Quotient.mk _ a) (Quotient.mk _ b) = f a b := rfl theorem Quotient.liftOn_mk {s : Setoid α} (f : α → β) (h : ∀ a b : α, a ≈ b → f a = f b) (x : α) : Quotient.liftOn (Quotient.mk s x) f h = f x := rfl @[simp] theorem Quotient.liftOn₂_mk {α : Sort*} {β : Sort*} {γ : Sort*} {_ : Setoid α} {_ : Setoid β} (f : α → β → γ) (h : ∀ (a₁ : α) (b₁ : β) (a₂ : α) (b₂ : β), a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ = f a₂ b₂) (x : α) (y : β) : Quotient.liftOn₂ (Quotient.mk _ x) (Quotient.mk _ y) f h = f x y := rfl /-- `Quot.mk r` is a surjective function. -/ theorem Quot.mk_surjective {r : α → α → Prop} : Function.Surjective (Quot.mk r) := Quot.exists_rep /-- `Quotient.mk` is a surjective function. -/ theorem Quotient.mk_surjective {s : Setoid α} : Function.Surjective (Quotient.mk s) := Quot.exists_rep /-- `Quotient.mk'` is a surjective function. -/ theorem Quotient.mk'_surjective [s : Setoid α] : Function.Surjective (Quotient.mk' : α → Quotient s) := Quot.exists_rep /-- Choose an element of the equivalence class using the axiom of choice. Sound but noncomputable. -/ noncomputable def Quot.out {r : α → α → Prop} (q : Quot r) : α := Classical.choose (Quot.exists_rep q) /-- Unwrap the VM representation of a quotient to obtain an element of the equivalence class. Computable but unsound. -/ unsafe def Quot.unquot {r : α → α → Prop} : Quot r → α := cast lcProof @[simp] theorem Quot.out_eq {r : α → α → Prop} (q : Quot r) : Quot.mk r q.out = q := Classical.choose_spec (Quot.exists_rep q) /-- Choose an element of the equivalence class using the axiom of choice. Sound but noncomputable. -/ noncomputable def Quotient.out {s : Setoid α} : Quotient s → α := Quot.out @[simp] theorem Quotient.out_eq {s : Setoid α} (q : Quotient s) : ⟦q.out⟧ = q := Quot.out_eq q theorem Quotient.mk_out {s : Setoid α} (a : α) : s (⟦a⟧ : Quotient s).out a := Quotient.exact (Quotient.out_eq _) theorem Quotient.mk_eq_iff_out {s : Setoid α} {x : α} {y : Quotient s} : ⟦x⟧ = y ↔ x ≈ Quotient.out y := by refine Iff.trans ?_ Quotient.eq rw [Quotient.out_eq y] theorem Quotient.eq_mk_iff_out {s : Setoid α} {x : Quotient s} {y : α} : x = ⟦y⟧ ↔ Quotient.out x ≈ y := by refine Iff.trans ?_ Quotient.eq rw [Quotient.out_eq x] @[simp] theorem Quotient.out_equiv_out {s : Setoid α} {x y : Quotient s} : x.out ≈ y.out ↔ x = y := by rw [← Quotient.eq_mk_iff_out, Quotient.out_eq] theorem Quotient.out_injective {s : Setoid α} : Function.Injective (@Quotient.out α s) := fun _ _ h ↦ Quotient.out_equiv_out.1 <| h ▸ Setoid.refl _ @[simp] theorem Quotient.out_inj {s : Setoid α} {x y : Quotient s} : x.out = y.out ↔ x = y := ⟨fun h ↦ Quotient.out_injective h, fun h ↦ h ▸ rfl⟩ section Pi instance piSetoid {ι : Sort*} {α : ι → Sort*} [∀ i, Setoid (α i)] : Setoid (∀ i, α i) where r a b := ∀ i, a i ≈ b i iseqv := ⟨fun _ _ ↦ Setoid.refl _, fun h _ ↦ Setoid.symm (h _), fun h₁ h₂ _ ↦ Setoid.trans (h₁ _) (h₂ _)⟩ /-- Given a class of functions `q : @Quotient (∀ i, α i) _`, returns the class of `i`-th projection `Quotient (S i)`. -/ def Quotient.eval {ι : Type*} {α : ι → Sort*} {S : ∀ i, Setoid (α i)} (q : @Quotient (∀ i, α i) (by infer_instance)) (i : ι) : Quotient (S i) := q.map (· i) fun _ _ h ↦ by exact h i @[simp] theorem Quotient.eval_mk {ι : Type*} {α : ι → Type*} {S : ∀ i, Setoid (α i)} (f : ∀ i, α i) : Quotient.eval (S := S) ⟦f⟧ = fun i ↦ ⟦f i⟧ := rfl /-- Given a function `f : Π i, Quotient (S i)`, returns the class of functions `Π i, α i` sending each `i` to an element of the class `f i`. -/ noncomputable def Quotient.choice {ι : Type*} {α : ι → Type*} {S : ∀ i, Setoid (α i)} (f : ∀ i, Quotient (S i)) : @Quotient (∀ i, α i) (by infer_instance) := ⟦fun i ↦ (f i).out⟧ @[simp] theorem Quotient.choice_eq {ι : Type*} {α : ι → Type*} {S : ∀ i, Setoid (α i)} (f : ∀ i, α i) : (Quotient.choice (S := S) fun i ↦ ⟦f i⟧) = ⟦f⟧ := Quotient.sound fun _ ↦ Quotient.mk_out _ @[elab_as_elim] theorem Quotient.induction_on_pi {ι : Type*} {α : ι → Sort*} {s : ∀ i, Setoid (α i)} {p : (∀ i, Quotient (s i)) → Prop} (f : ∀ i, Quotient (s i)) (h : ∀ a : ∀ i, α i, p fun i ↦ ⟦a i⟧) : p f := by rw [← (funext fun i ↦ Quotient.out_eq (f i) : (fun i ↦ ⟦(f i).out⟧) = f)] apply h end Pi theorem nonempty_quotient_iff (s : Setoid α) : Nonempty (Quotient s) ↔ Nonempty α := ⟨fun ⟨a⟩ ↦ Quotient.inductionOn a Nonempty.intro, fun ⟨a⟩ ↦ ⟨⟦a⟧⟩⟩ /-! ### Truncation -/ theorem true_equivalence : @Equivalence α fun _ _ ↦ True := ⟨fun _ ↦ trivial, fun _ ↦ trivial, fun _ _ ↦ trivial⟩ /-- Always-true relation as a `Setoid`. Note that in later files the preferred spelling is `⊤ : Setoid α`. -/ def trueSetoid : Setoid α := ⟨_, true_equivalence⟩ /-- `Trunc α` is the quotient of `α` by the always-true relation. This is related to the propositional truncation in HoTT, and is similar in effect to `Nonempty α`, but unlike `Nonempty α`, `Trunc α` is data, so the VM representation is the same as `α`, and so this can be used to maintain computability. -/ def Trunc.{u} (α : Sort u) : Sort u := @Quotient α trueSetoid namespace Trunc /-- Constructor for `Trunc α` -/ def mk (a : α) : Trunc α := Quot.mk _ a instance [Inhabited α] : Inhabited (Trunc α) := ⟨mk default⟩ /-- Any constant function lifts to a function out of the truncation -/ def lift (f : α → β) (c : ∀ a b : α, f a = f b) : Trunc α → β := Quot.lift f fun a b _ ↦ c a b theorem ind {β : Trunc α → Prop} : (∀ a : α, β (mk a)) → ∀ q : Trunc α, β q := Quot.ind protected theorem lift_mk (f : α → β) (c) (a : α) : lift f c (mk a) = f a := rfl /-- Lift a constant function on `q : Trunc α`. -/ protected def liftOn (q : Trunc α) (f : α → β) (c : ∀ a b : α, f a = f b) : β := lift f c q @[elab_as_elim] protected theorem induction_on {β : Trunc α → Prop} (q : Trunc α) (h : ∀ a, β (mk a)) : β q := ind h q theorem exists_rep (q : Trunc α) : ∃ a : α, mk a = q := Quot.exists_rep q @[elab_as_elim] protected theorem induction_on₂ {C : Trunc α → Trunc β → Prop} (q₁ : Trunc α) (q₂ : Trunc β) (h : ∀ a b, C (mk a) (mk b)) : C q₁ q₂ := Trunc.induction_on q₁ fun a₁ ↦ Trunc.induction_on q₂ (h a₁) protected theorem eq (a b : Trunc α) : a = b := Trunc.induction_on₂ a b fun _ _ ↦ Quot.sound trivial instance instSubsingletonTrunc : Subsingleton (Trunc α) := ⟨Trunc.eq⟩ /-- The `bind` operator for the `Trunc` monad. -/ def bind (q : Trunc α) (f : α → Trunc β) : Trunc β := Trunc.liftOn q f fun _ _ ↦ Trunc.eq _ _ /-- A function `f : α → β` defines a function `map f : Trunc α → Trunc β`. -/ def map (f : α → β) (q : Trunc α) : Trunc β := bind q (Trunc.mk ∘ f) instance : Monad Trunc where pure := @Trunc.mk bind := @Trunc.bind instance : LawfulMonad Trunc where id_map _ := Trunc.eq _ _ pure_bind _ _ := rfl bind_assoc _ _ _ := Trunc.eq _ _ map_const := rfl seqLeft_eq _ _ := Trunc.eq _ _ seqRight_eq _ _ := Trunc.eq _ _ pure_seq _ _ := rfl bind_pure_comp _ _ := rfl bind_map _ _ := rfl variable {C : Trunc α → Sort*} /-- Recursion/induction principle for `Trunc`. -/ @[elab_as_elim] protected def rec (f : ∀ a, C (mk a)) (h : ∀ a b : α, (Eq.ndrec (f a) (Trunc.eq (mk a) (mk b)) : C (mk b)) = f b) (q : Trunc α) : C q := Quot.rec f (fun a b _ ↦ h a b) q /-- A version of `Trunc.rec` taking `q : Trunc α` as the first argument. -/ @[elab_as_elim] protected def recOn (q : Trunc α) (f : ∀ a, C (mk a)) (h : ∀ a b : α, (Eq.ndrec (f a) (Trunc.eq (mk a) (mk b)) : C (mk b)) = f b) : C q := Trunc.rec f h q /-- A version of `Trunc.recOn` assuming the codomain is a `Subsingleton`. -/ @[elab_as_elim] protected def recOnSubsingleton [∀ a, Subsingleton (C (mk a))] (q : Trunc α) (f : ∀ a, C (mk a)) : C q := Trunc.rec f (fun _ b ↦ Subsingleton.elim _ (f b)) q /-- Noncomputably extract a representative of `Trunc α` (using the axiom of choice). -/ noncomputable def out : Trunc α → α := Quot.out @[simp] theorem out_eq (q : Trunc α) : mk q.out = q := Trunc.eq _ _ protected theorem nonempty (q : Trunc α) : Nonempty α := q.exists_rep.nonempty end Trunc /-! ### `Quotient` with implicit `Setoid` -/ namespace Quotient variable {γ : Sort*} {φ : Sort*} {s₁ : Setoid α} {s₂ : Setoid β} {s₃ : Setoid γ} /-! Versions of quotient definitions and lemmas ending in `'` use unification instead of typeclass inference for inferring the `Setoid` argument. This is useful when there are several different quotient relations on a type, for example quotient groups, rings and modules. -/ -- TODO: this whole section can probably be replaced `Quotient.mk`, with explicit parameter /-- A version of `Quotient.mk` taking `{s : Setoid α}` as an implicit argument instead of an instance argument. -/ protected abbrev mk'' (a : α) : Quotient s₁ := ⟦a⟧ /-- `Quotient.mk''` is a surjective function. -/ theorem mk''_surjective : Function.Surjective (Quotient.mk'' : α → Quotient s₁) := Quot.exists_rep /-- A version of `Quotient.liftOn` taking `{s : Setoid α}` as an implicit argument instead of an instance argument. -/ protected def liftOn' (q : Quotient s₁) (f : α → φ) (h : ∀ a b, s₁ a b → f a = f b) : φ := Quotient.liftOn q f h @[simp] protected theorem liftOn'_mk'' (f : α → φ) (h) (x : α) : Quotient.liftOn' (@Quotient.mk'' _ s₁ x) f h = f x := rfl @[simp] lemma surjective_liftOn' {f : α → φ} (h) : Function.Surjective (fun x : Quotient s₁ ↦ x.liftOn' f h) ↔ Function.Surjective f := Quot.surjective_lift _ /-- A version of `Quotient.liftOn₂` taking `{s₁ : Setoid α} {s₂ : Setoid β}` as implicit arguments instead of instance arguments. -/ protected def liftOn₂' (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : α → β → γ) (h : ∀ a₁ a₂ b₁ b₂, s₁ a₁ b₁ → s₂ a₂ b₂ → f a₁ a₂ = f b₁ b₂) : γ := Quotient.liftOn₂ q₁ q₂ f h @[simp] protected theorem liftOn₂'_mk'' (f : α → β → γ) (h) (a : α) (b : β) : Quotient.liftOn₂' (@Quotient.mk'' _ s₁ a) (@Quotient.mk'' _ s₂ b) f h = f a b := rfl /-- A version of `Quotient.ind` taking `{s : Setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_elim] protected theorem ind' {p : Quotient s₁ → Prop} (h : ∀ a, p (Quotient.mk'' a)) (q : Quotient s₁) : p q := Quotient.ind h q /-- A version of `Quotient.ind₂` taking `{s₁ : Setoid α} {s₂ : Setoid β}` as implicit arguments instead of instance arguments. -/ @[elab_as_elim] protected theorem ind₂' {p : Quotient s₁ → Quotient s₂ → Prop} (h : ∀ a₁ a₂, p (Quotient.mk'' a₁) (Quotient.mk'' a₂)) (q₁ : Quotient s₁) (q₂ : Quotient s₂) : p q₁ q₂ := Quotient.ind₂ h q₁ q₂ /-- A version of `Quotient.inductionOn` taking `{s : Setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_elim] protected theorem inductionOn' {p : Quotient s₁ → Prop} (q : Quotient s₁) (h : ∀ a, p (Quotient.mk'' a)) : p q := Quotient.inductionOn q h /-- A version of `Quotient.inductionOn₂` taking `{s₁ : Setoid α} {s₂ : Setoid β}` as implicit arguments instead of instance arguments. -/ @[elab_as_elim] protected theorem inductionOn₂' {p : Quotient s₁ → Quotient s₂ → Prop} (q₁ : Quotient s₁) (q₂ : Quotient s₂) (h : ∀ a₁ a₂, p (Quotient.mk'' a₁) (Quotient.mk'' a₂)) : p q₁ q₂ := Quotient.inductionOn₂ q₁ q₂ h /-- A version of `Quotient.inductionOn₃` taking `{s₁ : Setoid α} {s₂ : Setoid β} {s₃ : Setoid γ}` as implicit arguments instead of instance arguments. -/ @[elab_as_elim] protected theorem inductionOn₃' {p : Quotient s₁ → Quotient s₂ → Quotient s₃ → Prop} (q₁ : Quotient s₁) (q₂ : Quotient s₂) (q₃ : Quotient s₃) (h : ∀ a₁ a₂ a₃, p (Quotient.mk'' a₁) (Quotient.mk'' a₂) (Quotient.mk'' a₃)) : p q₁ q₂ q₃ := Quotient.inductionOn₃ q₁ q₂ q₃ h /-- A version of `Quotient.recOnSubsingleton` taking `{s₁ : Setoid α}` as an implicit argument instead of an instance argument. -/ @[elab_as_elim] protected def recOnSubsingleton' {φ : Quotient s₁ → Sort*} [∀ a, Subsingleton (φ ⟦a⟧)] (q : Quotient s₁) (f : ∀ a, φ (Quotient.mk'' a)) : φ q := Quotient.recOnSubsingleton q f /-- A version of `Quotient.recOnSubsingleton₂` taking `{s₁ : Setoid α} {s₂ : Setoid α}` as implicit arguments instead of instance arguments. -/ @[elab_as_elim] protected def recOnSubsingleton₂' {φ : Quotient s₁ → Quotient s₂ → Sort*} [∀ a b, Subsingleton (φ ⟦a⟧ ⟦b⟧)] (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : ∀ a₁ a₂, φ (Quotient.mk'' a₁) (Quotient.mk'' a₂)) : φ q₁ q₂ := Quotient.recOnSubsingleton₂ q₁ q₂ f /-- Recursion on a `Quotient` argument `a`, result type depends on `⟦a⟧`. -/ protected def hrecOn' {φ : Quotient s₁ → Sort*} (qa : Quotient s₁) (f : ∀ a, φ (Quotient.mk'' a)) (c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ ≍ f a₂) : φ qa := Quot.hrecOn qa f c @[simp] theorem hrecOn'_mk'' {φ : Quotient s₁ → Sort*} (f : ∀ a, φ (Quotient.mk'' a)) (c : ∀ a₁ a₂, a₁ ≈ a₂ → f a₁ ≍ f a₂) (x : α) : (Quotient.mk'' x).hrecOn' f c = f x := rfl /-- Recursion on two `Quotient` arguments `a` and `b`, result type depends on `⟦a⟧` and `⟦b⟧`. -/ protected def hrecOn₂' {φ : Quotient s₁ → Quotient s₂ → Sort*} (qa : Quotient s₁) (qb : Quotient s₂) (f : ∀ a b, φ (Quotient.mk'' a) (Quotient.mk'' b)) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ ≍ f a₂ b₂) : φ qa qb := Quotient.hrecOn₂ qa qb f c @[simp] theorem hrecOn₂'_mk'' {φ : Quotient s₁ → Quotient s₂ → Sort*} (f : ∀ a b, φ (Quotient.mk'' a) (Quotient.mk'' b)) (c : ∀ a₁ b₁ a₂ b₂, a₁ ≈ a₂ → b₁ ≈ b₂ → f a₁ b₁ ≍ f a₂ b₂) (x : α) (qb : Quotient s₂) : (Quotient.mk'' x).hrecOn₂' qb f c = qb.hrecOn' (f x) fun _ _ ↦ c _ _ _ _ (Setoid.refl _) := rfl /-- Map a function `f : α → β` that sends equivalent elements to equivalent elements to a function `Quotient sa → Quotient sb`. Useful to define unary operations on quotients. This is a version of `Quotient.map` using `Setoid.r` instead of `≈`. -/ protected def map' (f : α → β) (h : ∀ a b, s₁.r a b → s₂.r (f a) (f b)) : Quotient s₁ → Quotient s₂ := Quot.map f h @[simp] theorem map'_mk'' (f : α → β) (h) (x : α) : (Quotient.mk'' x : Quotient s₁).map' f h = (Quotient.mk'' (f x) : Quotient s₂) := rfl /-- Map a function `f : α → β → γ` that sends equivalent elements to equivalent elements to a function `f : Quotient sa → Quotient sb → Quotient sc`. Useful to define binary operations on quotients. This is a version of `Quotient.map₂` using `Setoid.r` instead of `≈`. -/ protected def map₂' (f : α → β → γ) (h : ∀ ⦃a₁ a₂ : α⦄, s₁.r a₁ a₂ → ∀ ⦃b₁ b₂ : β⦄, s₂.r b₁ b₂ → s₃.r (f a₁ b₁) (f a₂ b₂)) : Quotient s₁ → Quotient s₂ → Quotient s₃ := Quotient.map₂ f h @[simp] theorem map₂'_mk'' (f : α → β → γ) (h) (x : α) : (Quotient.mk'' x : Quotient s₁).map₂' f h = (Quotient.map' (f x) (h (Setoid.refl x)) : Quotient s₂ → Quotient s₃) := rfl theorem exact' {a b : α} : (Quotient.mk'' a : Quotient s₁) = Quotient.mk'' b → s₁ a b := Quotient.exact theorem sound' {a b : α} : s₁ a b → @Quotient.mk'' α s₁ a = Quotient.mk'' b := Quotient.sound @[simp] protected theorem eq' {s₁ : Setoid α} {a b : α} : @Quotient.mk' α s₁ a = @Quotient.mk' α s₁ b ↔ s₁ a b := Quotient.eq protected theorem eq'' {a b : α} : @Quotient.mk'' α s₁ a = Quotient.mk'' b ↔ s₁ a b := Quotient.eq theorem out_eq' (q : Quotient s₁) : Quotient.mk'' q.out = q := q.out_eq theorem mk_out' (a : α) : s₁ (Quotient.mk'' a : Quotient s₁).out a := Quotient.exact (Quotient.out_eq _) section variable {s : Setoid α} protected theorem mk''_eq_mk : Quotient.mk'' = Quotient.mk s := rfl @[simp] protected theorem liftOn'_mk (x : α) (f : α → β) (h) : (Quotient.mk s x).liftOn' f h = f x := rfl @[simp] protected theorem liftOn₂'_mk {t : Setoid β} (f : α → β → γ) (h) (a : α) (b : β) : Quotient.liftOn₂' (Quotient.mk s a) (Quotient.mk t b) f h = f a b := rfl theorem map'_mk {t : Setoid β} (f : α → β) (h) (x : α) : (Quotient.mk s x).map' f h = (Quotient.mk t (f x)) := rfl end instance (q : Quotient s₁) (f : α → Prop) (h : ∀ a b, s₁ a b → f a = f b) [DecidablePred f] : Decidable (Quotient.liftOn' q f h) := Quotient.lift.decidablePred _ _ q instance (q₁ : Quotient s₁) (q₂ : Quotient s₂) (f : α → β → Prop) (h : ∀ a₁ b₁ a₂ b₂, s₁ a₁ a₂ → s₂ b₁ b₂ → f a₁ b₁ = f a₂ b₂) [∀ a, DecidablePred (f a)] : Decidable (Quotient.liftOn₂' q₁ q₂ f h) := Quotient.lift₂.decidablePred _ h _ _ end Quotient @[simp] lemma Equivalence.quot_mk_eq_iff {α : Type*} {r : α → α → Prop} (h : Equivalence r) (x y : α) : Quot.mk r x = Quot.mk r y ↔ r x y := by constructor · rw [Quot.eq] intro hxy induction hxy with | rel _ _ H => exact H | refl _ => exact h.refl _ | symm _ _ _ H => exact h.symm H | trans _ _ _ _ _ h₁₂ h₂₃ => exact h.trans h₁₂ h₂₃ · exact Quot.sound
.lake/packages/mathlib/Mathlib/Data/Part.lean
import Mathlib.Algebra.Notation.Defs import Mathlib.Data.Set.Subsingleton import Mathlib.Logic.Equiv.Defs /-! # Partial values of a type This file defines `Part α`, the partial values of a type. `o : Part α` carries a proposition `o.Dom`, its domain, along with a function `get : o.Dom → α`, its value. The rule is then that every partial value has a value but, to access it, you need to provide a proof of the domain. `Part α` behaves the same as `Option α` except that `o : Option α` is decidably `none` or `some a` for some `a : α`, while the domain of `o : Part α` doesn't have to be decidable. That means you can translate back and forth between a partial value with a decidable domain and an option, and `Option α` and `Part α` are classically equivalent. In general, `Part α` is bigger than `Option α`. ## Main declarations `Option`-like declarations: * `Part.none`: The partial value whose domain is `False`. * `Part.some a`: The partial value whose domain is `True` and whose value is `a`. * `Part.ofOption`: Converts an `Option α` to a `Part α` by sending `none` to `none` and `some a` to `some a`. * `Part.toOption`: Converts a `Part α` with a decidable domain to an `Option α`. * `Part.equivOption`: Classical equivalence between `Part α` and `Option α`. Monadic structure: * `Part.bind`: `o.bind f` has value `(f (o.get _)).get _` (`f o` morally) and is defined when `o` and `f (o.get _)` are defined. * `Part.map`: Maps the value and keeps the same domain. Other: * `Part.restrict`: `Part.restrict p o` replaces the domain of `o : Part α` by `p : Prop` so long as `p → o.Dom`. * `Part.assert`: `assert p f` appends `p` to the domains of the values of a partial function. * `Part.unwrap`: Gets the value of a partial value regardless of its domain. Unsound. ## Notation For `a : α`, `o : Part α`, `a ∈ o` means that `o` is defined and equal to `a`. Formally, it means `o.Dom` and `o.get _ = a`. -/ assert_not_exists RelIso open Function /-- `Part α` is the type of "partial values" of type `α`. It is similar to `Option α` except the domain condition can be an arbitrary proposition, not necessarily decidable. -/ structure Part.{u} (α : Type u) : Type u where /-- The domain of a partial value -/ Dom : Prop /-- Extract a value from a partial value given a proof of `Dom` -/ get : Dom → α namespace Part variable {α : Type*} {β : Type*} {γ : Type*} /-- Convert a `Part α` with a decidable domain to an option -/ def toOption (o : Part α) [Decidable o.Dom] : Option α := if h : Dom o then some (o.get h) else none @[simp] lemma toOption_isSome (o : Part α) [Decidable o.Dom] : o.toOption.isSome ↔ o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] @[simp] lemma toOption_eq_none (o : Part α) [Decidable o.Dom] : o.toOption = none ↔ ¬o.Dom := by by_cases h : o.Dom <;> simp [h, toOption] /-- `Part` extensionality -/ theorem ext' : ∀ {o p : Part α}, (o.Dom ↔ p.Dom) → (∀ h₁ h₂, o.get h₁ = p.get h₂) → o = p | ⟨od, o⟩, ⟨pd, p⟩, H1, H2 => by have t : od = pd := propext H1 cases t; rw [show o = p from funext fun p => H2 p p] /-- `Part` eta expansion -/ @[simp] theorem eta : ∀ o : Part α, (⟨o.Dom, fun h => o.get h⟩ : Part α) = o | ⟨_, _⟩ => rfl /-- `a ∈ o` means that `o` is defined and equal to `a` -/ protected def Mem (o : Part α) (a : α) : Prop := ∃ h, o.get h = a instance : Membership α (Part α) := ⟨Part.Mem⟩ theorem mem_eq (a : α) (o : Part α) : (a ∈ o) = ∃ h, o.get h = a := rfl theorem dom_iff_mem : ∀ {o : Part α}, o.Dom ↔ ∃ y, y ∈ o | ⟨_, f⟩ => ⟨fun h => ⟨f h, h, rfl⟩, fun ⟨_, h, rfl⟩ => h⟩ theorem get_mem {o : Part α} (h) : get o h ∈ o := ⟨_, rfl⟩ @[simp] theorem mem_mk_iff {p : Prop} {o : p → α} {a : α} : a ∈ Part.mk p o ↔ ∃ h, o h = a := Iff.rfl /-- `Part` extensionality -/ @[ext] theorem ext {o p : Part α} (H : ∀ a, a ∈ o ↔ a ∈ p) : o = p := (ext' ⟨fun h => ((H _).1 ⟨h, rfl⟩).fst, fun h => ((H _).2 ⟨h, rfl⟩).fst⟩) fun _ _ => ((H _).2 ⟨_, rfl⟩).snd /-- The `none` value in `Part` has a `False` domain and an empty function. -/ def none : Part α := ⟨False, False.rec⟩ instance : Inhabited (Part α) := ⟨none⟩ @[simp] theorem notMem_none (a : α) : a ∉ @none α := fun h => h.fst @[deprecated (since := "2025-05-23")] alias not_mem_none := notMem_none /-- The `some a` value in `Part` has a `True` domain and the function returns `a`. -/ def some (a : α) : Part α := ⟨True, fun _ => a⟩ @[simp] theorem some_dom (a : α) : (some a).Dom := trivial theorem mem_unique : ∀ {a b : α} {o : Part α}, a ∈ o → b ∈ o → a = b | _, _, ⟨_, _⟩, ⟨_, rfl⟩, ⟨_, rfl⟩ => rfl theorem mem_right_unique : ∀ {a : α} {o p : Part α}, a ∈ o → a ∈ p → o = p | _, _, _, ⟨ho, _⟩, ⟨hp, _⟩ => ext' (iff_of_true ho hp) (by simp [*]) theorem Mem.left_unique : Relator.LeftUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_unique theorem Mem.right_unique : Relator.RightUnique ((· ∈ ·) : α → Part α → Prop) := fun _ _ _ => mem_right_unique theorem get_eq_of_mem {o : Part α} {a} (h : a ∈ o) (h') : get o h' = a := mem_unique ⟨_, rfl⟩ h protected theorem subsingleton (o : Part α) : Set.Subsingleton { a | a ∈ o } := fun _ ha _ hb => mem_unique ha hb @[simp] theorem get_some {a : α} (ha : (some a).Dom) : get (some a) ha = a := rfl theorem mem_some (a : α) : a ∈ some a := ⟨trivial, rfl⟩ @[simp] theorem mem_some_iff {a b} : b ∈ (some a : Part α) ↔ b = a := ⟨fun ⟨_, e⟩ => e.symm, fun e => ⟨trivial, e.symm⟩⟩ theorem eq_some_iff {a : α} {o : Part α} : o = some a ↔ a ∈ o := ⟨fun e => e.symm ▸ mem_some _, fun ⟨h, e⟩ => e ▸ ext' (iff_true_intro h) fun _ _ => rfl⟩ theorem eq_none_iff {o : Part α} : o = none ↔ ∀ a, a ∉ o := ⟨fun e => e.symm ▸ notMem_none, fun h => ext (by simpa)⟩ theorem eq_none_iff' {o : Part α} : o = none ↔ ¬o.Dom := ⟨fun e => e.symm ▸ id, fun h => eq_none_iff.2 fun _ h' => h h'.fst⟩ @[simp] theorem not_none_dom : ¬(none : Part α).Dom := id @[simp] theorem some_ne_none (x : α) : some x ≠ none := by intro h exact true_ne_false (congr_arg Dom h) @[simp] theorem none_ne_some (x : α) : none ≠ some x := (some_ne_none x).symm theorem ne_none_iff {o : Part α} : o ≠ none ↔ ∃ x, o = some x := by constructor · rw [Ne, eq_none_iff', not_not] exact fun h => ⟨o.get h, eq_some_iff.2 (get_mem h)⟩ · rintro ⟨x, rfl⟩ apply some_ne_none theorem eq_none_or_eq_some (o : Part α) : o = none ∨ ∃ x, o = some x := or_iff_not_imp_left.2 ne_none_iff.1 theorem some_injective : Injective (@Part.some α) := fun _ _ h => congr_fun (eq_of_heq (Part.mk.inj h).2) trivial @[simp] theorem some_inj {a b : α} : Part.some a = some b ↔ a = b := some_injective.eq_iff @[simp] theorem some_get {a : Part α} (ha : a.Dom) : Part.some (Part.get a ha) = a := Eq.symm (eq_some_iff.2 ⟨ha, rfl⟩) theorem get_eq_iff_eq_some {a : Part α} {ha : a.Dom} {b : α} : a.get ha = b ↔ a = some b := ⟨fun h => by simp [h.symm], fun h => by simp [h]⟩ theorem get_eq_get_of_eq (a : Part α) (ha : a.Dom) {b : Part α} (h : a = b) : a.get ha = b.get (h ▸ ha) := by congr theorem get_eq_iff_mem {o : Part α} {a : α} (h : o.Dom) : o.get h = a ↔ a ∈ o := ⟨fun H => ⟨h, H⟩, fun ⟨_, H⟩ => H⟩ theorem eq_get_iff_mem {o : Part α} {a : α} (h : o.Dom) : a = o.get h ↔ a ∈ o := eq_comm.trans (get_eq_iff_mem h) theorem eq_of_get_eq_get {a b : Part α} (ha : a.Dom) (hb : b.Dom) (hab : a.get ha = b.get hb) : a = b := ext' (iff_of_true ha hb) fun _ _ => hab theorem eq_iff_of_dom {a b : Part α} (ha : a.Dom) (hb : b.Dom) : a.get ha = b.get hb ↔ a = b := ⟨eq_of_get_eq_get ha hb, get_eq_get_of_eq a ha⟩ theorem eq_of_mem {a b : Part α} (ha : a.Dom) (hb : a.get ha ∈ b) : a = b := by have hb' : b.Dom := Part.dom_iff_mem.mpr ⟨a.get ha, hb⟩ rwa [← eq_get_iff_mem hb', eq_iff_of_dom ha hb'] at hb @[simp] theorem none_toOption [Decidable (@none α).Dom] : (none : Part α).toOption = Option.none := dif_neg id @[simp] theorem some_toOption (a : α) [Decidable (some a).Dom] : (some a).toOption = Option.some a := dif_pos trivial instance noneDecidable : Decidable (@none α).Dom := instDecidableFalse instance someDecidable (a : α) : Decidable (some a).Dom := instDecidableTrue /-- Retrieves the value of `a : Part α` if it exists, and return the provided default value otherwise. -/ def getOrElse (a : Part α) [Decidable a.Dom] (d : α) := if ha : a.Dom then a.get ha else d theorem getOrElse_of_dom (a : Part α) (h : a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = a.get h := dif_pos h theorem getOrElse_of_not_dom (a : Part α) (h : ¬a.Dom) [Decidable a.Dom] (d : α) : getOrElse a d = d := dif_neg h @[simp] theorem getOrElse_none (d : α) [Decidable (none : Part α).Dom] : getOrElse none d = d := none.getOrElse_of_not_dom not_none_dom d @[simp] theorem getOrElse_some (a : α) (d : α) [Decidable (some a).Dom] : getOrElse (some a) d = a := (some a).getOrElse_of_dom (some_dom a) d -- `simp`-normal form is `toOption_eq_some_iff`. theorem mem_toOption {o : Part α} [Decidable o.Dom] {a : α} : a ∈ toOption o ↔ a ∈ o := by unfold toOption by_cases h : o.Dom · simpa [h] using ⟨fun h => ⟨_, h⟩, fun ⟨_, h⟩ => h⟩ · simp only [h, ↓reduceDIte, Option.mem_def, reduceCtorEq, false_iff] exact mt Exists.fst h @[simp] theorem toOption_eq_some_iff {o : Part α} [Decidable o.Dom] {a : α} : toOption o = Option.some a ↔ a ∈ o := by rw [← Option.mem_def, mem_toOption] protected theorem Dom.toOption {o : Part α} [Decidable o.Dom] (h : o.Dom) : o.toOption = o.get h := dif_pos h theorem toOption_eq_none_iff {a : Part α} [Decidable a.Dom] : a.toOption = Option.none ↔ ¬a.Dom := Ne.dite_eq_right_iff fun _ => Option.some_ne_none _ @[simp] theorem elim_toOption {α β : Type*} (a : Part α) [Decidable a.Dom] (b : β) (f : α → β) : a.toOption.elim b f = if h : a.Dom then f (a.get h) else b := by split_ifs with h · rw [h.toOption] rfl · rw [Part.toOption_eq_none_iff.2 h] rfl /-- Converts an `Option α` into a `Part α`. -/ @[coe] def ofOption : Option α → Part α | Option.none => none | Option.some a => some a @[simp] theorem mem_ofOption {a : α} : ∀ {o : Option α}, a ∈ ofOption o ↔ a ∈ o | Option.none => ⟨fun h => h.fst.elim, fun h => Option.noConfusion h⟩ | Option.some _ => ⟨fun h => congr_arg Option.some h.snd, fun h => ⟨trivial, Option.some.inj h⟩⟩ @[simp] theorem ofOption_dom {α} : ∀ o : Option α, (ofOption o).Dom ↔ o.isSome | Option.none => by simp [ofOption, none] | Option.some a => by simp [ofOption] theorem ofOption_eq_get {α} (o : Option α) : ofOption o = ⟨_, @Option.get _ o⟩ := Part.ext' (ofOption_dom o) fun h₁ h₂ => by cases o · simp at h₂ · rfl instance : Coe (Option α) (Part α) := ⟨ofOption⟩ theorem mem_coe {a : α} {o : Option α} : a ∈ (o : Part α) ↔ a ∈ o := mem_ofOption @[simp] theorem coe_none : (@Option.none α : Part α) = none := rfl @[simp] theorem coe_some (a : α) : (Option.some a : Part α) = some a := rfl @[elab_as_elim] protected theorem induction_on {P : Part α → Prop} (a : Part α) (hnone : P none) (hsome : ∀ a : α, P (some a)) : P a := (Classical.em a.Dom).elim (fun h => Part.some_get h ▸ hsome _) fun h => (eq_none_iff'.2 h).symm ▸ hnone instance ofOptionDecidable : ∀ o : Option α, Decidable (ofOption o).Dom | Option.none => Part.noneDecidable | Option.some a => Part.someDecidable a @[simp] theorem to_ofOption (o : Option α) : toOption (ofOption o) = o := by cases o <;> rfl @[simp] theorem of_toOption (o : Part α) [Decidable o.Dom] : ofOption (toOption o) = o := ext fun _ => mem_ofOption.trans mem_toOption /-- `Part α` is (classically) equivalent to `Option α`. -/ noncomputable def equivOption : Part α ≃ Option α := haveI := Classical.dec ⟨fun o => toOption o, ofOption, fun o => of_toOption o, fun o => Eq.trans (by dsimp; congr) (to_ofOption o)⟩ /-- We give `Part α` the order where everything is greater than `none`. -/ instance : PartialOrder (Part α) where le x y := ∀ i, i ∈ x → i ∈ y le_refl _ _ := id le_trans _ _ _ f g _ := g _ ∘ f _ le_antisymm _ _ f g := Part.ext fun _ => ⟨f _, g _⟩ instance : OrderBot (Part α) where bot := none bot_le := by rintro x _ ⟨⟨_⟩, _⟩ theorem le_total_of_le_of_le {x y : Part α} (z : Part α) (hx : x ≤ z) (hy : y ≤ z) : x ≤ y ∨ y ≤ x := by rcases Part.eq_none_or_eq_some x with (h | ⟨b, h₀⟩) · rw [h] left apply OrderBot.bot_le _ right; intro b' h₁ rw [Part.eq_some_iff] at h₀ have hx := hx _ h₀; have hy := hy _ h₁ have hx := Part.mem_unique hx hy; subst hx exact h₀ /-- `assert p f` is a bind-like operation which appends an additional condition `p` to the domain and uses `f` to produce the value. -/ def assert (p : Prop) (f : p → Part α) : Part α := ⟨∃ h : p, (f h).Dom, fun ha => (f ha.fst).get ha.snd⟩ /-- The bind operation has value `g (f.get)`, and is defined when all the parts are defined. -/ protected def bind (f : Part α) (g : α → Part β) : Part β := assert (Dom f) fun b => g (f.get b) /-- The map operation for `Part` just maps the value and maintains the same domain. -/ @[simps] def map (f : α → β) (o : Part α) : Part β := ⟨o.Dom, f ∘ o.get⟩ theorem mem_map (f : α → β) {o : Part α} : ∀ {a}, a ∈ o → f a ∈ map f o | _, ⟨_, rfl⟩ => ⟨_, rfl⟩ @[simp] theorem mem_map_iff (f : α → β) {o : Part α} {b} : b ∈ map f o ↔ ∃ a ∈ o, f a = b := ⟨fun hb => match b, hb with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩, rfl⟩, fun ⟨_, h₁, h₂⟩ => h₂ ▸ mem_map f h₁⟩ @[simp] theorem map_none (f : α → β) : map f none = none := eq_none_iff.2 fun a => by simp @[simp] theorem map_some (f : α → β) (a : α) : map f (some a) = some (f a) := eq_some_iff.2 <| mem_map f <| mem_some _ theorem mem_assert {p : Prop} {f : p → Part α} : ∀ {a} (h : p), a ∈ f h → a ∈ assert p f | _, x, ⟨h, rfl⟩ => ⟨⟨x, h⟩, rfl⟩ @[simp] theorem mem_assert_iff {p : Prop} {f : p → Part α} {a} : a ∈ assert p f ↔ ∃ h : p, a ∈ f h := ⟨fun ha => match a, ha with | _, ⟨_, rfl⟩ => ⟨_, ⟨_, rfl⟩⟩, fun ⟨_, h⟩ => mem_assert _ h⟩ theorem assert_pos {p : Prop} {f : p → Part α} (h : p) : assert p f = f h := by dsimp [assert] cases h' : f h simp only [h', mk.injEq, h, exists_prop_of_true, true_and] apply Function.hfunext · simp only [h, h', exists_prop_of_true] · simp theorem assert_neg {p : Prop} {f : p → Part α} (h : ¬p) : assert p f = none := by dsimp [assert, none]; congr · simp only [h, not_false_iff, exists_prop_of_false] · apply Function.hfunext · simp only [h, not_false_iff, exists_prop_of_false] simp at * theorem mem_bind {f : Part α} {g : α → Part β} : ∀ {a b}, a ∈ f → b ∈ g a → b ∈ f.bind g | _, _, ⟨h, rfl⟩, ⟨h₂, rfl⟩ => ⟨⟨h, h₂⟩, rfl⟩ @[simp] theorem mem_bind_iff {f : Part α} {g : α → Part β} {b} : b ∈ f.bind g ↔ ∃ a ∈ f, b ∈ g a := ⟨fun hb => match b, hb with | _, ⟨⟨_, _⟩, rfl⟩ => ⟨_, ⟨_, rfl⟩, ⟨_, rfl⟩⟩, fun ⟨_, h₁, h₂⟩ => mem_bind h₁ h₂⟩ protected theorem Dom.bind {o : Part α} (h : o.Dom) (f : α → Part β) : o.bind f = f (o.get h) := by ext b simp only [Part.mem_bind_iff] refine ⟨?_, fun hb => ⟨o.get h, Part.get_mem _, hb⟩⟩ rintro ⟨a, ha, hb⟩ rwa [Part.get_eq_of_mem ha] theorem Dom.of_bind {f : α → Part β} {a : Part α} (h : (a.bind f).Dom) : a.Dom := h.1 @[simp] theorem bind_none (f : α → Part β) : none.bind f = none := eq_none_iff.2 fun a => by simp @[simp] theorem bind_some (a : α) (f : α → Part β) : (some a).bind f = f a := ext <| by simp theorem bind_of_mem {o : Part α} {a : α} (h : a ∈ o) (f : α → Part β) : o.bind f = f a := by rw [eq_some_iff.2 h, bind_some] theorem bind_some_eq_map (f : α → β) (x : Part α) : x.bind (fun y => some (f y)) = map f x := ext <| by simp [eq_comm] theorem bind_toOption (f : α → Part β) (o : Part α) [Decidable o.Dom] [∀ a, Decidable (f a).Dom] [Decidable (o.bind f).Dom] : (o.bind f).toOption = o.toOption.elim Option.none fun a => (f a).toOption := by by_cases h : o.Dom · simp_rw [h.toOption, h.bind] rfl · rw [Part.toOption_eq_none_iff.2 h] exact Part.toOption_eq_none_iff.2 fun ho => h ho.of_bind theorem bind_assoc {γ} (f : Part α) (g : α → Part β) (k : β → Part γ) : (f.bind g).bind k = f.bind fun x => (g x).bind k := ext fun a => by simp only [mem_bind_iff] exact ⟨fun ⟨_, ⟨_, h₁, h₂⟩, h₃⟩ => ⟨_, h₁, _, h₂, h₃⟩, fun ⟨_, h₁, _, h₂, h₃⟩ => ⟨_, ⟨_, h₁, h₂⟩, h₃⟩⟩ @[simp] theorem bind_map {γ} (f : α → β) (x) (g : β → Part γ) : (map f x).bind g = x.bind fun y => g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp @[simp] theorem map_bind {γ} (f : α → Part β) (x : Part α) (g : β → γ) : map g (x.bind f) = x.bind fun y => map g (f y) := by rw [← bind_some_eq_map, bind_assoc]; simp [bind_some_eq_map] theorem map_map (g : β → γ) (f : α → β) (o : Part α) : map g (map f o) = map (g ∘ f) o := by simp [map, Function.comp_assoc] instance : Monad Part where pure := @some map := @map bind := @Part.bind instance : LawfulMonad Part where bind_pure_comp := @bind_some_eq_map id_map f := by cases f; rfl pure_bind := @bind_some bind_assoc := @bind_assoc map_const := by simp [Functor.mapConst, Functor.map] --Porting TODO : In Lean3 these were automatic by a tactic seqLeft_eq x y := ext' (by simp [SeqLeft.seqLeft, Part.bind, assert, Seq.seq, const, (· <$> ·), and_comm]) (fun _ _ => rfl) seqRight_eq x y := ext' (by simp [SeqRight.seqRight, Part.bind, assert, Seq.seq, const, (· <$> ·)]) (fun _ _ => rfl) pure_seq x y := ext' (by simp [Seq.seq, Part.bind, assert, (· <$> ·), pure]) (fun _ _ => rfl) bind_map x y := ext' (by simp [(· >>= ·), Part.bind, assert, Seq.seq, (· <$> ·)] ) (fun _ _ => rfl) theorem map_id' {f : α → α} (H : ∀ x : α, f x = x) (o) : map f o = o := by rw [show f = id from funext H]; exact id_map o @[simp] theorem bind_some_right (x : Part α) : x.bind some = x := by rw [bind_some_eq_map] simp [map_id'] @[simp] theorem pure_eq_some (a : α) : pure a = some a := rfl @[simp] theorem ret_eq_some (a : α) : (return a : Part α) = some a := rfl @[simp] theorem map_eq_map {α β} (f : α → β) (o : Part α) : f <$> o = map f o := rfl @[simp] theorem bind_eq_bind {α β} (f : Part α) (g : α → Part β) : f >>= g = f.bind g := rfl theorem bind_le {α} (x : Part α) (f : α → Part β) (y : Part β) : x >>= f ≤ y ↔ ∀ a, a ∈ x → f a ≤ y := by constructor <;> intro h · intro a h' b have h := h b simp only [and_imp, bind_eq_bind, mem_bind_iff, exists_imp] at h apply h _ h' · intro b h' simp only [bind_eq_bind, mem_bind_iff] at h' rcases h' with ⟨a, h₀, h₁⟩ apply h _ h₀ _ h₁ -- TODO: if `MonadFail` is defined, define the below instance. -- instance : MonadFail Part := -- { Part.monad with fail := fun _ _ => none } /-- `restrict p o h` replaces the domain of `o` with `p`, and is well defined when `p` implies `o` is defined. -/ def restrict (p : Prop) (o : Part α) (H : p → o.Dom) : Part α := ⟨p, fun h => o.get (H h)⟩ @[simp] theorem mem_restrict (p : Prop) (o : Part α) (h : p → o.Dom) (a : α) : a ∈ restrict p o h ↔ p ∧ a ∈ o := by dsimp [restrict, mem_eq]; constructor · rintro ⟨h₀, h₁⟩ exact ⟨h₀, ⟨_, h₁⟩⟩ rintro ⟨h₀, _, h₂⟩; exact ⟨h₀, h₂⟩ /-- `unwrap o` gets the value at `o`, ignoring the condition. This function is unsound. -/ unsafe def unwrap (o : Part α) : α := o.get lcProof theorem assert_defined {p : Prop} {f : p → Part α} : ∀ h : p, (f h).Dom → (assert p f).Dom := Exists.intro theorem bind_defined {f : Part α} {g : α → Part β} : ∀ h : f.Dom, (g (f.get h)).Dom → (f.bind g).Dom := assert_defined @[simp] theorem bind_dom {f : Part α} {g : α → Part β} : (f.bind g).Dom ↔ ∃ h : f.Dom, (g (f.get h)).Dom := Iff.rfl section Instances /-! We define several instances for constants and operations on `Part α` inherited from `α`. This section could be moved to a separate file to avoid the import of `Mathlib/Algebra/Notation/Defs.lean`. -/ @[to_additive] instance [One α] : One (Part α) where one := pure 1 @[to_additive] instance [Mul α] : Mul (Part α) where mul a b := (· * ·) <$> a <*> b @[to_additive] instance [Inv α] : Inv (Part α) where inv := map Inv.inv @[to_additive] instance [Div α] : Div (Part α) where div a b := (· / ·) <$> a <*> b instance [Mod α] : Mod (Part α) where mod a b := (· % ·) <$> a <*> b instance [Append α] : Append (Part α) where append a b := (· ++ ·) <$> a <*> b instance [Inter α] : Inter (Part α) where inter a b := (· ∩ ·) <$> a <*> b instance [Union α] : Union (Part α) where union a b := (· ∪ ·) <$> a <*> b instance [SDiff α] : SDiff (Part α) where sdiff a b := (· \ ·) <$> a <*> b section @[to_additive] theorem mul_def [Mul α] (a b : Part α) : a * b = bind a fun y ↦ map (y * ·) b := rfl @[to_additive] theorem one_def [One α] : (1 : Part α) = some 1 := rfl @[to_additive] theorem inv_def [Inv α] (a : Part α) : a⁻¹ = Part.map (·⁻¹) a := rfl @[to_additive] theorem div_def [Div α] (a b : Part α) : a / b = bind a fun y => map (y / ·) b := rfl theorem mod_def [Mod α] (a b : Part α) : a % b = bind a fun y => map (y % ·) b := rfl theorem append_def [Append α] (a b : Part α) : a ++ b = bind a fun y => map (y ++ ·) b := rfl theorem inter_def [Inter α] (a b : Part α) : a ∩ b = bind a fun y => map (y ∩ ·) b := rfl theorem union_def [Union α] (a b : Part α) : a ∪ b = bind a fun y => map (y ∪ ·) b := rfl theorem sdiff_def [SDiff α] (a b : Part α) : a \ b = bind a fun y => map (y \ ·) b := rfl end @[to_additive] theorem one_mem_one [One α] : (1 : α) ∈ (1 : Part α) := ⟨trivial, rfl⟩ @[to_additive] theorem mul_mem_mul [Mul α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma * mb ∈ a * b := ⟨⟨ha.1, hb.1⟩, by simp only [← ha.2, ← hb.2]; rfl⟩ @[to_additive] theorem left_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_mul_dom [Mul α] {a b : Part α} (hab : Dom (a * b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem mul_get_eq [Mul α] (a b : Part α) (hab : Dom (a * b)) : (a * b).get hab = a.get (left_dom_of_mul_dom hab) * b.get (right_dom_of_mul_dom hab) := rfl @[to_additive] theorem some_mul_some [Mul α] (a b : α) : some a * some b = some (a * b) := by simp [mul_def] @[to_additive] theorem inv_mem_inv [Inv α] (a : Part α) (ma : α) (ha : ma ∈ a) : ma⁻¹ ∈ a⁻¹ := by simp [inv_def]; aesop @[to_additive] theorem inv_some [Inv α] (a : α) : (some a)⁻¹ = some a⁻¹ := rfl @[to_additive] theorem div_mem_div [Div α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma / mb ∈ a / b := by simp [div_def]; aesop @[to_additive] theorem left_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : a.Dom := hab.1 @[to_additive] theorem right_dom_of_div_dom [Div α] {a b : Part α} (hab : Dom (a / b)) : b.Dom := hab.2 @[to_additive (attr := simp)] theorem div_get_eq [Div α] (a b : Part α) (hab : Dom (a / b)) : (a / b).get hab = a.get (left_dom_of_div_dom hab) / b.get (right_dom_of_div_dom hab) := by simp [div_def]; aesop @[to_additive] theorem some_div_some [Div α] (a b : α) : some a / some b = some (a / b) := by simp [div_def] theorem mod_mem_mod [Mod α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma % mb ∈ a % b := by simp [mod_def]; aesop theorem left_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : a.Dom := hab.1 theorem right_dom_of_mod_dom [Mod α] {a b : Part α} (hab : Dom (a % b)) : b.Dom := hab.2 @[simp] theorem mod_get_eq [Mod α] (a b : Part α) (hab : Dom (a % b)) : (a % b).get hab = a.get (left_dom_of_mod_dom hab) % b.get (right_dom_of_mod_dom hab) := by simp [mod_def]; aesop theorem some_mod_some [Mod α] (a b : α) : some a % some b = some (a % b) := by simp [mod_def] theorem append_mem_append [Append α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ++ mb ∈ a ++ b := by simp [append_def]; aesop theorem left_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : a.Dom := hab.1 theorem right_dom_of_append_dom [Append α] {a b : Part α} (hab : Dom (a ++ b)) : b.Dom := hab.2 @[simp] theorem append_get_eq [Append α] (a b : Part α) (hab : Dom (a ++ b)) : (a ++ b).get hab = a.get (left_dom_of_append_dom hab) ++ b.get (right_dom_of_append_dom hab) := by simp [append_def]; aesop theorem some_append_some [Append α] (a b : α) : some a ++ some b = some (a ++ b) := by simp [append_def] theorem inter_mem_inter [Inter α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∩ mb ∈ a ∩ b := by simp [inter_def]; aesop theorem left_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : a.Dom := hab.1 theorem right_dom_of_inter_dom [Inter α] {a b : Part α} (hab : Dom (a ∩ b)) : b.Dom := hab.2 @[simp] theorem inter_get_eq [Inter α] (a b : Part α) (hab : Dom (a ∩ b)) : (a ∩ b).get hab = a.get (left_dom_of_inter_dom hab) ∩ b.get (right_dom_of_inter_dom hab) := by simp [inter_def]; aesop theorem some_inter_some [Inter α] (a b : α) : some a ∩ some b = some (a ∩ b) := by simp [inter_def] theorem union_mem_union [Union α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma ∪ mb ∈ a ∪ b := by simp [union_def]; aesop theorem left_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : a.Dom := hab.1 theorem right_dom_of_union_dom [Union α] {a b : Part α} (hab : Dom (a ∪ b)) : b.Dom := hab.2 @[simp] theorem union_get_eq [Union α] (a b : Part α) (hab : Dom (a ∪ b)) : (a ∪ b).get hab = a.get (left_dom_of_union_dom hab) ∪ b.get (right_dom_of_union_dom hab) := by simp [union_def]; aesop theorem some_union_some [Union α] (a b : α) : some a ∪ some b = some (a ∪ b) := by simp [union_def] theorem sdiff_mem_sdiff [SDiff α] (a b : Part α) (ma mb : α) (ha : ma ∈ a) (hb : mb ∈ b) : ma \ mb ∈ a \ b := by simp [sdiff_def]; aesop theorem left_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : a.Dom := hab.1 theorem right_dom_of_sdiff_dom [SDiff α] {a b : Part α} (hab : Dom (a \ b)) : b.Dom := hab.2 @[simp] theorem sdiff_get_eq [SDiff α] (a b : Part α) (hab : Dom (a \ b)) : (a \ b).get hab = a.get (left_dom_of_sdiff_dom hab) \ b.get (right_dom_of_sdiff_dom hab) := by simp [sdiff_def]; aesop theorem some_sdiff_some [SDiff α] (a b : α) : some a \ some b = some (a \ b) := by simp [sdiff_def] end Instances end Part
.lake/packages/mathlib/Mathlib/Data/Opposite.lean
import Mathlib.Logic.Equiv.Defs import Mathlib.Logic.Small.Defs /-! # Opposites In this file we define a structure `Opposite α` containing a single field of type `α` and two bijections `op : α → αᵒᵖ` and `unop : αᵒᵖ → α`. If `α` is a category, then `αᵒᵖ` is the opposite category, with all arrows reversed. -/ universe v u -- morphism levels before object levels. See note [category theory universes]. variable (α : Sort u) /-- The type of objects of the opposite of `α`; used to define the opposite category. Now that Lean 4 supports definitional eta equality for records, both `unop (op X) = X` and `op (unop X) = X` are definitional equalities. -/ structure Opposite where /-- The canonical map `α → αᵒᵖ`. -/ op :: /-- The canonical map `αᵒᵖ → α`. -/ unop : α attribute [pp_nodot] Opposite.unop /-- Make sure that `Opposite.op a` is pretty-printed as `op a` instead of `{ unop := a }` or `⟨a⟩`. -/ @[app_unexpander Opposite.op] protected def Opposite.unexpander_op : Lean.PrettyPrinter.Unexpander | s => pure s @[inherit_doc] notation:max -- Use a high right binding power (like that of postfix ⁻¹) so that, for example, -- `Presheaf Cᵒᵖ` parses as `Presheaf (Cᵒᵖ)` and not `(Presheaf C)ᵒᵖ`. α "ᵒᵖ" => Opposite α namespace Opposite variable {α} theorem op_injective : Function.Injective (op : α → αᵒᵖ) := fun _ _ => congr_arg Opposite.unop theorem unop_injective : Function.Injective (unop : αᵒᵖ → α) := fun ⟨_⟩⟨_⟩ => by simp @[simp] theorem op_unop (x : αᵒᵖ) : op (unop x) = x := rfl theorem unop_op (x : α) : unop (op x) = x := rfl -- We could prove these by `Iff.rfl`, but that would make these eligible for `dsimp`. That would be -- a bad idea because `Opposite` is irreducible. theorem op_inj_iff (x y : α) : op x = op y ↔ x = y := op_injective.eq_iff @[simp] theorem unop_inj_iff (x y : αᵒᵖ) : unop x = unop y ↔ x = y := unop_injective.eq_iff /-- The type-level equivalence between a type and its opposite. -/ def equivToOpposite : α ≃ αᵒᵖ where toFun := op invFun := unop left_inv := unop_op right_inv := op_unop theorem op_surjective : Function.Surjective (op : α → αᵒᵖ) := equivToOpposite.surjective theorem unop_surjective : Function.Surjective (unop : αᵒᵖ → α) := equivToOpposite.symm.surjective @[simp] theorem equivToOpposite_coe : (equivToOpposite : α → αᵒᵖ) = op := rfl @[simp] theorem equivToOpposite_symm_coe : (equivToOpposite.symm : αᵒᵖ → α) = unop := rfl theorem op_eq_iff_eq_unop {x : α} {y} : op x = y ↔ x = unop y := equivToOpposite.apply_eq_iff_eq_symm_apply theorem unop_eq_iff_eq_op {x} {y : α} : unop x = y ↔ x = op y := equivToOpposite.symm.apply_eq_iff_eq_symm_apply instance [Inhabited α] : Inhabited αᵒᵖ := ⟨op default⟩ instance [Nonempty α] : Nonempty αᵒᵖ := Nonempty.map op ‹_› instance [Subsingleton α] : Subsingleton αᵒᵖ := unop_injective.subsingleton /-- If `X` is `u`-small, also `Xᵒᵖ` is `u`-small. -/ instance small {X : Type v} [Small.{u} X] : Small.{u} Xᵒᵖ := by obtain ⟨S, ⟨e⟩⟩ := Small.equiv_small (α := X) exact ⟨S, ⟨equivToOpposite.symm.trans e⟩⟩ end Opposite
.lake/packages/mathlib/Mathlib/Data/Sign.lean
import Mathlib.Data.Sign.Defs deprecated_module (since := "2025-05-11")
.lake/packages/mathlib/Mathlib/Data/Rel.lean
import Mathlib.Data.Set.Prod import Mathlib.Order.RelIso.Basic import Mathlib.Order.SetNotation /-! # Relations as sets of pairs This file provides API to regard relations between `α` and `β` as sets of pairs `Set (α × β)`. This is in particular useful in the study of uniform spaces, which are topological spaces equipped with a *uniformity*, namely a filter of pairs `α × α` whose elements can be viewed as "proximity" relations. ## Main declarations * `SetRel α β`: Type of relations between `α` and `β`. * `SetRel.inv`: Turn `R : SetRel α β` into `R.inv : SetRel β α` by swapping the arguments. * `SetRel.dom`: Domain of a relation. `a ∈ R.dom` iff there exists `b` such that `a ~[R] b`. * `SetRel.cod`: Codomain of a relation. `b ∈ R.cod` iff there exists `a` such that `a ~[R] b`. * `SetRel.id`: The identity relation `SetRel α α`. * `SetRel.comp`: SetRelation composition. Note that the arguments order follows the category theory convention, namely `(R ○ S) a c ↔ ∃ b, a ~[R] b ∧ b ~[S] z`. * `SetRel.image`: Image of a set under a relation. `b ∈ image R s` iff there exists `a ∈ s` such that `a ~[R] b`. If `R` is the graph of `f` (`a ~[R] b ↔ f a = b`), then `R.image = Set.image f`. * `SetRel.preimage`: Preimage of a set under a relation. `a ∈ preimage R t` iff there exists `b ∈ t` such that `a ~[R] b`. If `R` is the graph of `f` (`a ~[R] b ↔ f a = b`), then `R.preimage = Set.preimage f`. * `SetRel.core`: Core of a set. For `t : Set β`, `a ∈ R.core t` iff all `b` related to `a` are in `t`. * `SetRel.restrictDomain`: Domain-restriction of a relation to a subtype. * `Function.graph`: Graph of a function as a relation. ## Implementation notes There is tension throughout the library between considering relations between `α` and `β` simply as `α → β → Prop`, or as a bundled object `SetRel α β` with dedicated operations and API. The former approach is used almost everywhere as it is very lightweight and has arguably native support from core Lean features, but it cracks at the seams whenever one starts talking about operations on relations. For example: * composition of relations `R : α → β → Prop`, `S : β → γ → Prop` is `SetRelation.Comp R S := fun a c ↦ ∃ b, R a b ∧ S b c` * map of a relation `R : α → β → Prop` under `f : α → γ`, `g : β → δ` is `SetRelation.map R f g := fun c d ↦ ∃ a b, r a b ∧ f a = c ∧ g b = d`. The latter approach is embodied by `SetRel α β`, with dedicated notation like `○` for composition. Previously, `SetRel` suffered from the leakage of its definition as ``` def SetRel (α β : Type*) := α → β → Prop ``` The fact that `SetRel` wasn't an `abbrev` confuses automation. But simply making it an `abbrev` would have killed the point of having a separate less see-through type to perform relation operations on, so we instead redefined ``` def SetRel (α β : Type*) := Set (α × β) → Prop ``` This extra level of indirection guides automation correctly and prevents (some kinds of) leakage. Simultaneously, uniform spaces need a theory of relations on a type `α` as elements of `Set (α × α)`, and the new definition of `SetRel` fulfills this role quite well. -/ variable {α β γ δ : Type*} {ι : Sort*} /-- A relation on `α` and `β`, aka a set-valued function, aka a partial multifunction. We represent them as sets due to how relations are used in the context of uniform spaces. -/ abbrev SetRel (α β : Type*) := Set (α × β) namespace SetRel variable {R R₁ R₂ : SetRel α β} {S : SetRel β γ} {s s₁ s₂ : Set α} {t t₁ t₂ : Set β} {u : Set γ} {a a₁ a₂ : α} {b : β} {c : γ} /-- Notation for apply a relation `R : SetRel α β` to `a : α`, `b : β`, scoped to the `SetRel` namespace. Since `SetRel α β := Set (α × β)`, `a ~[R] b` is simply notation for `(a, b) ∈ R`, but this should be considered an implementation detail. -/ scoped notation:50 a:50 " ~[" R "] " b:50 => (a, b) ∈ R variable (R) in /-- The inverse relation : `R.inv x y ↔ R y x`. Note that this is *not* a groupoid inverse. -/ def inv (R : SetRel α β) : SetRel β α := Prod.swap ⁻¹' R @[simp] lemma mem_inv : b ~[R.inv] a ↔ a ~[R] b := .rfl @[deprecated (since := "2025-07-06")] alias inv_def := mem_inv @[simp] lemma inv_inv : R.inv.inv = R := rfl @[gcongr] lemma inv_mono (h : R₁ ⊆ R₂) : R₁.inv ⊆ R₂.inv := fun (_a, _b) hab ↦ h hab @[simp] lemma inv_empty : (∅ : SetRel α β).inv = ∅ := rfl @[simp] lemma inv_univ : inv (.univ : SetRel α β) = .univ := rfl @[deprecated (since := "2025-07-06")] alias inv_bot := inv_empty variable (R) in /-- Domain of a relation. -/ def dom : Set α := {a | ∃ b, a ~[R] b} variable (R) in /-- Codomain of a relation, aka range. -/ def cod : Set β := {b | ∃ a, a ~[R] b} @[deprecated (since := "2025-07-06")] alias codom := cod @[simp] lemma mem_dom : a ∈ R.dom ↔ ∃ b, a ~[R] b := .rfl @[simp] lemma mem_cod : b ∈ R.cod ↔ ∃ a, a ~[R] b := .rfl @[gcongr] lemma dom_mono (h : R₁ ≤ R₂) : R₁.dom ⊆ R₂.dom := fun _a ⟨b, hab⟩ ↦ ⟨b, h hab⟩ @[gcongr] lemma cod_mono (h : R₁ ≤ R₂) : R₁.cod ⊆ R₂.cod := fun _b ⟨a, hab⟩ ↦ ⟨a, h hab⟩ @[simp] lemma dom_empty : (∅ : SetRel α β).dom = ∅ := by aesop @[simp] lemma cod_empty : (∅ : SetRel α β).cod = ∅ := by aesop @[simp] lemma dom_univ [Nonempty β] : dom (.univ : SetRel α β) = .univ := by aesop @[simp] lemma cod_univ [Nonempty α] : cod (.univ : SetRel α β) = .univ := by aesop @[simp] lemma cod_inv : R.inv.cod = R.dom := rfl @[simp] lemma dom_inv : R.inv.dom = R.cod := rfl @[deprecated (since := "2025-07-06")] alias codom_inv := cod_inv /-- The identity relation. -/ protected def id : SetRel α α := {(a₁, a₂) | a₁ = a₂} @[simp] lemma mem_id : a₁ ~[SetRel.id] a₂ ↔ a₁ = a₂ := .rfl -- Not simp because `SetRel.inv_eq_self` already proves it lemma inv_id : (.id : SetRel α α).inv = .id := by aesop /-- Composition of relation. Note that this follows the `CategoryTheory` order of arguments. -/ def comp (R : SetRel α β) (S : SetRel β γ) : SetRel α γ := {(a, c) | ∃ b, a ~[R] b ∧ b ~[S] c} @[inherit_doc] scoped infixl:62 " ○ " => comp @[simp] lemma mem_comp : a ~[R ○ S] c ↔ ∃ b, a ~[R] b ∧ b ~[S] c := .rfl lemma prodMk_mem_comp (hab : a ~[R] b) (hbc : b ~[S] c) : a ~[R ○ S] c := ⟨b, hab, hbc⟩ lemma comp_assoc (R : SetRel α β) (S : SetRel β γ) (t : SetRel γ δ) : (R ○ S) ○ t = R ○ (S ○ t) := by aesop @[simp] lemma comp_id (R : SetRel α β) : R ○ .id = R := by aesop @[simp] lemma id_comp (R : SetRel α β) : .id ○ R = R := by aesop @[simp] lemma inv_comp (R : SetRel α β) (S : SetRel β γ) : (R ○ S).inv = S.inv ○ R.inv := by aesop @[simp] lemma comp_empty (R : SetRel α β) : R ○ (∅ : SetRel β γ) = ∅ := by aesop @[simp] lemma empty_comp (S : SetRel β γ) : (∅ : SetRel α β) ○ S = ∅ := by aesop @[simp] lemma comp_univ (R : SetRel α β) : R ○ (.univ : SetRel β γ) = {(a, _c) : α × γ | a ∈ R.dom} := by aesop @[simp] lemma univ_comp (S : SetRel β γ) : (.univ : SetRel α β) ○ S = {(_b, c) : α × γ | c ∈ S.cod} := by aesop lemma comp_iUnion (R : SetRel α β) (S : ι → SetRel β γ) : R ○ ⋃ i, S i = ⋃ i, R ○ S i := by aesop lemma iUnion_comp (R : ι → SetRel α β) (S : SetRel β γ) : (⋃ i, R i) ○ S = ⋃ i, R i ○ S := by aesop lemma comp_sUnion (R : SetRel α β) (𝒮 : Set (SetRel β γ)) : R ○ ⋃₀ 𝒮 = ⋃ S ∈ 𝒮, R ○ S := by aesop lemma sUnion_comp (ℛ : Set (SetRel α β)) (S : SetRel β γ) : ⋃₀ ℛ ○ S = ⋃ R ∈ ℛ, R ○ S := by aesop @[gcongr] lemma comp_subset_comp {S₁ S₂ : SetRel β γ} (hR : R₁ ⊆ R₂) (hS : S₁ ⊆ S₂) : R₁ ○ S₁ ⊆ R₂ ○ S₂ := fun _ ↦ .imp fun _ ↦ .imp (@hR _) (@hS _) @[gcongr] lemma comp_subset_comp_left {S : SetRel β γ} (hR : R₁ ⊆ R₂) : R₁ ○ S ⊆ R₂ ○ S := comp_subset_comp hR .rfl @[gcongr] lemma comp_subset_comp_right {S₁ S₂ : SetRel β γ} (hS : S₁ ⊆ S₂) : R ○ S₁ ⊆ R ○ S₂ := comp_subset_comp .rfl hS protected lemma _root_.Monotone.relComp {ι : Type*} [Preorder ι] {f : ι → SetRel α β} {g : ι → SetRel β γ} (hf : Monotone f) (hg : Monotone g) : Monotone fun x ↦ f x ○ g x := fun _i _j hij ⟨_a, _c⟩ ⟨b, hab, hbc⟩ ↦ ⟨b, hf hij hab, hg hij hbc⟩ lemma prod_comp_prod_of_inter_nonempty (ht : (t₁ ∩ t₂).Nonempty) (s : Set α) (u : Set γ) : s ×ˢ t₁ ○ t₂ ×ˢ u = s ×ˢ u := by aesop lemma prod_comp_prod_of_disjoint (ht : Disjoint t₁ t₂) (s : Set α) (u : Set γ) : s ×ˢ t₁ ○ t₂ ×ˢ u = ∅ := Set.eq_empty_of_forall_notMem fun _ ⟨_z, ⟨_, hzs⟩, hzu, _⟩ ↦ Set.disjoint_left.1 ht hzs hzu lemma prod_comp_prod (s : Set α) (t₁ t₂ : Set β) (u : Set γ) [Decidable (Disjoint t₁ t₂)] : s ×ˢ t₁ ○ t₂ ×ˢ u = if Disjoint t₁ t₂ then ∅ else s ×ˢ u := by split_ifs with hst · exact prod_comp_prod_of_disjoint hst .. · rw [prod_comp_prod_of_inter_nonempty <| Set.not_disjoint_iff_nonempty_inter.1 hst] @[deprecated (since := "2025-07-06")] alias comp_right_top := comp_univ @[deprecated (since := "2025-07-06")] alias comp_left_top := univ_comp variable (R s) in /-- Image of a set under a relation. -/ def image : Set β := {b | ∃ a ∈ s, a ~[R] b} variable (R t) in /-- Preimage of a set `t` under a relation `R`. Same as the image of `t` under `R.inv`. -/ def preimage : Set α := {a | ∃ b ∈ t, a ~[R] b} @[simp] lemma mem_image : b ∈ image R s ↔ ∃ a ∈ s, a ~[R] b := .rfl @[simp] lemma mem_preimage : a ∈ preimage R t ↔ ∃ b ∈ t, a ~[R] b := .rfl @[gcongr] lemma image_subset_image (hs : s₁ ⊆ s₂) : image R s₁ ⊆ image R s₂ := fun _ ⟨a, ha, hab⟩ ↦ ⟨a, hs ha, hab⟩ @[gcongr] lemma preimage_subset_preimage (ht : t₁ ⊆ t₂) : preimage R t₁ ⊆ preimage R t₂ := fun _ ⟨a, ha, hab⟩ ↦ ⟨a, ht ha, hab⟩ variable (R t) in @[simp] lemma image_inv : R.inv.image t = preimage R t := rfl variable (R s) in @[simp] lemma preimage_inv : R.inv.preimage s = image R s := rfl lemma image_mono : Monotone R.image := fun _ _ ↦ image_subset_image lemma preimage_mono : Monotone R.preimage := fun _ _ ↦ preimage_subset_preimage @[simp] lemma image_empty_right : image R ∅ = ∅ := by aesop @[simp] lemma preimage_empty_right : preimage R ∅ = ∅ := by aesop @[simp] lemma image_univ_right : image R .univ = R.cod := by aesop @[simp] lemma preimage_univ_right : preimage R .univ = R.dom := by aesop variable (R) in lemma image_inter_subset : image R (s₁ ∩ s₂) ⊆ image R s₁ ∩ image R s₂ := image_mono.map_inf_le .. @[deprecated (since := "2025-07-06")] alias preimage_top := image_inter_subset variable (R) in lemma preimage_inter_subset : preimage R (t₁ ∩ t₂) ⊆ preimage R t₁ ∩ preimage R t₂ := preimage_mono.map_inf_le .. @[deprecated (since := "2025-07-06")] alias image_eq_dom_of_codomain_subset := preimage_inter_subset variable (R s₁ s₂) in lemma image_union : image R (s₁ ∪ s₂) = image R s₁ ∪ image R s₂ := by aesop @[deprecated (since := "2025-07-06")] alias preimage_eq_codom_of_domain_subset := image_union variable (R t₁ t₂) in lemma preimage_union : preimage R (t₁ ∪ t₂) = preimage R t₁ ∪ preimage R t₂ := by aesop variable (s) in @[simp] lemma image_id : image .id s = s := by aesop variable (s) in @[simp] lemma preimage_id : preimage .id s = s := by aesop variable (R S s) in lemma image_comp : image (R ○ S) s = image S (image R s) := by aesop variable (R S u) in lemma preimage_comp : preimage (R ○ S) u = preimage R (preimage S u) := by aesop variable (s) in @[simp] lemma image_empty_left : image (∅ : SetRel α β) s = ∅ := by aesop variable (t) in @[simp] lemma preimage_empty_left : preimage (∅ : SetRel α β) t = ∅ := by aesop @[deprecated (since := "2025-07-06")] alias preimage_bot := preimage_empty_left @[simp] lemma image_univ_left (hs : s.Nonempty) : image (.univ : SetRel α β) s = .univ := by aesop @[simp] lemma preimage_univ_left (ht : t.Nonempty) : preimage (.univ : SetRel α β) t = .univ := by aesop lemma image_eq_cod_of_dom_subset (h : R.cod ⊆ t) : R.preimage t = R.dom := by aesop lemma preimage_eq_dom_of_cod_subset (h : R.cod ⊆ t) : R.preimage t = R.dom := by aesop variable (R s) in @[simp] lemma image_inter_dom : image R (s ∩ R.dom) = image R s := by aesop variable (R t) in @[simp] lemma preimage_inter_cod : preimage R (t ∩ R.cod) = preimage R t := by aesop @[deprecated (since := "2025-07-06")] alias preimage_inter_codom_eq := preimage_inter_cod lemma inter_dom_subset_preimage_image : s ∩ R.dom ⊆ R.preimage (image R s) := by aesop (add simp [Set.subset_def]) lemma inter_cod_subset_image_preimage : t ∩ R.cod ⊆ image R (R.preimage t) := by aesop (add simp [Set.subset_def]) @[deprecated (since := "2025-07-06")] alias image_preimage_subset_inter_codom := inter_cod_subset_image_preimage variable (R t) in /-- Core of a set `S : Set β` w.R.t `R : SetRel α β` is the set of `x : α` that are related *only* to elements of `S`. Other generalization of `Function.preimage`. -/ def core : Set α := {a | ∀ ⦃b⦄, a ~[R] b → b ∈ t} @[simp] lemma mem_core : a ∈ R.core t ↔ ∀ ⦃b⦄, a ~[R] b → b ∈ t := .rfl @[gcongr] lemma core_subset_core (ht : t₁ ⊆ t₂) : R.core t₁ ⊆ R.core t₂ := fun _a ha _b hab ↦ ht <| ha hab lemma core_mono : Monotone R.core := fun _ _ ↦ core_subset_core variable (R t₁ t₂) in lemma core_inter : R.core (t₁ ∩ t₂) = R.core t₁ ∩ R.core t₂ := by aesop lemma core_union_subset : R.core t₁ ∪ R.core t₂ ⊆ R.core (t₁ ∪ t₂) := core_mono.le_map_sup .. @[simp] lemma core_univ : R.core Set.univ = Set.univ := by aesop variable (t) in @[simp] lemma core_id : core .id t = t := by aesop variable (R S u) in lemma core_comp : core (R ○ S) u = core R (core S u) := by aesop lemma image_subset_iff : image R s ⊆ t ↔ s ⊆ core R t := by aesop (add simp [Set.subset_def]) lemma image_core_gc : GaloisConnection R.image R.core := fun _ _ ↦ image_subset_iff variable (R s) in /-- Restrict the domain of a relation to a subtype. -/ def restrictDomain : SetRel s β := {(a, b) | ↑a ~[R] b} variable {R R₁ R₂ : SetRel α α} {S : SetRel β β} {a b c : α} /-! ### Reflexive relations -/ variable (R) in /-- A relation `R` is reflexive if `a ~[R] a`. -/ protected abbrev IsRefl : Prop := IsRefl α (· ~[R] ·) variable (R) in protected lemma refl [R.IsRefl] (a : α) : a ~[R] a := refl_of (· ~[R] ·) a variable (R) in protected lemma rfl [R.IsRefl] : a ~[R] a := R.refl a lemma id_subset [R.IsRefl] : .id ⊆ R := by rintro ⟨_, _⟩ rfl; exact R.rfl lemma id_subset_iff : .id ⊆ R ↔ R.IsRefl where mp h := ⟨fun _ ↦ h rfl⟩ mpr _ := id_subset instance isRefl_univ : SetRel.IsRefl (.univ : SetRel α α) where refl _ := trivial instance isRefl_inter [R₁.IsRefl] [R₂.IsRefl] : (R₁ ∩ R₂).IsRefl where refl _ := ⟨R₁.rfl, R₂.rfl⟩ protected lemma IsRefl.sInter {ℛ : Set <| SetRel α α} (hℛ : ∀ R ∈ ℛ, R.IsRefl) : SetRel.IsRefl (⋂₀ ℛ) where refl _a R hR := (hℛ R hR).refl _ instance isRefl_iInter {R : ι → SetRel α α} [∀ i, (R i).IsRefl] : SetRel.IsRefl (⋂ i, R i) := .sInter <| by simpa instance isRefl_preimage {f : β → α} [R.IsRefl] : SetRel.IsRefl (Prod.map f f ⁻¹' R) where refl _ := R.rfl lemma isRefl_mono [R₁.IsRefl] (hR : R₁ ⊆ R₂) : R₂.IsRefl where refl _ := hR R₁.rfl lemma left_subset_comp {R : SetRel α β} [S.IsRefl] : R ⊆ R ○ S := by simpa using comp_subset_comp_right id_subset lemma right_subset_comp [R.IsRefl] {S : SetRel α β} : S ⊆ R ○ S := by simpa using comp_subset_comp_left id_subset lemma subset_iterate_comp [R.IsRefl] {S : SetRel α β} : ∀ {n}, S ⊆ (R ○ ·)^[n] S | 0 => .rfl | _n + 1 => right_subset_comp.trans subset_iterate_comp lemma exists_eq_singleton_of_prod_subset_id {s t : Set α} (hs : s.Nonempty) (ht : t.Nonempty) (hst : s ×ˢ t ⊆ SetRel.id) : ∃ x, s = {x} ∧ t = {x} := by obtain ⟨a, ha⟩ := hs obtain ⟨b, hb⟩ := ht simp only [Set.prod_subset_iff, mem_id] at hst obtain rfl := hst _ ha _ hb simp only [Set.eq_singleton_iff_unique_mem, and_assoc] exact ⟨a, ha, (hst · · _ hb), hb, (hst _ ha · · |>.symm)⟩ /-! ### Symmetric relations -/ variable (R) in /-- A relation `R` is symmetric if `a ~[R] b ↔ b ~[R] a`. -/ protected abbrev IsSymm : Prop := IsSymm α (· ~[R] ·) variable (R) in protected lemma symm [R.IsSymm] (hab : a ~[R] b) : b ~[R] a := symm_of (· ~[R] ·) hab variable (R) in protected lemma comm [R.IsSymm] : a ~[R] b ↔ b ~[R] a := comm_of (· ~[R] ·) variable (R) in @[simp] lemma inv_eq_self [R.IsSymm] : R.inv = R := by ext; exact R.comm lemma inv_eq_self_iff : R.inv = R ↔ R.IsSymm where mp hR := ⟨fun a b hab ↦ by rwa [← hR]⟩ mpr _ := inv_eq_self _ instance [R.IsSymm] : R.inv.IsSymm := by simpa instance isSymm_empty : (∅ : SetRel α α).IsSymm where symm _ _ := by simp instance isSymm_univ : SetRel.IsSymm (Set.univ : SetRel α α) where symm _ _ := by simp instance isSymm_inter [R₁.IsSymm] [R₂.IsSymm] : (R₁ ∩ R₂).IsSymm where symm _ _ := .imp R₁.symm R₂.symm protected lemma IsSymm.sInter {ℛ : Set <| SetRel α α} (hℛ : ∀ R ∈ ℛ, R.IsSymm) : SetRel.IsSymm (⋂₀ ℛ) where symm _a _b hab R hR := (hℛ R hR).symm _ _ <| hab R hR instance isSymm_iInter {R : ι → SetRel α α} [∀ i, (R i).IsSymm] : SetRel.IsSymm (⋂ i, R i) := .sInter <| by simpa instance isSymm_id : (SetRel.id : SetRel α α).IsSymm where symm _ _ := .symm instance isSymm_preimage {f : β → α} [R.IsSymm] : SetRel.IsSymm (Prod.map f f ⁻¹' R) where symm _ _ := R.symm instance isSymm_image {f : α → β} [R.IsSymm] : SetRel.IsSymm (Prod.map f f '' R) where symm := by simp only [Set.mem_image, Prod.exists, Prod.map_apply, Prod.mk.injEq, forall_exists_index, and_imp] rintro _ _ a₁ a₂ ha rfl rfl exact ⟨_, _, R.symm ha, rfl, rfl⟩ instance isSymm_comp_inv : (R ○ R.inv).IsSymm where symm a c := by rintro ⟨b, hab, hbc⟩; exact ⟨b, hbc, hab⟩ instance isSymm_inv_comp : (R.inv ○ R).IsSymm := isSymm_comp_inv instance isSymm_comp_self [R.IsSymm] : (R ○ R).IsSymm := by simpa using R.isSymm_comp_inv lemma prod_subset_comm [R.IsSymm] : s₁ ×ˢ s₂ ⊆ R ↔ s₂ ×ˢ s₁ ⊆ R := by rw [← R.inv_eq_self, SetRel.inv, ← Set.image_subset_iff, Set.image_swap_prod, ← SetRel.inv, R.inv_eq_self] variable (R) in /-- The maximal symmetric relation contained in a given relation. -/ def symmetrize : SetRel α α := R ∩ R.inv instance isSymm_symmetrize : R.symmetrize.IsSymm where symm _ _ := .symm lemma symmetrize_subset_self : R.symmetrize ⊆ R := Set.inter_subset_left lemma symmetrize_subset_inv : R.symmetrize ⊆ R.inv := Set.inter_subset_right lemma subset_symmetrize {S : SetRel α α} : S ⊆ R.symmetrize ↔ S ⊆ R ∧ S ⊆ R.inv := Set.subset_inter_iff @[gcongr] lemma symmetrize_mono (h : R₁ ⊆ R₂) : R₁.symmetrize ⊆ R₂.symmetrize := Set.inter_subset_inter h <| Set.preimage_mono h /-! ### Transitive relations -/ variable (R) in /-- A relation `R` is transitive if `a ~[R] b` and `b ~[R] c` together imply `a ~[R] c`. -/ protected abbrev IsTrans : Prop := IsTrans α (· ~[R] ·) variable (R) in protected lemma trans [R.IsTrans] (hab : a ~[R] b) (hbc : b ~[R] c) : a ~[R] c := trans_of (· ~[R] ·) hab hbc instance {R : α → α → Prop} [IsTrans α R] : SetRel.IsTrans {(a, b) | R a b} := ‹_› lemma comp_subset_self [R.IsTrans] : R ○ R ⊆ R := fun ⟨_, _⟩ ⟨_, hab, hbc⟩ ↦ R.trans hab hbc lemma comp_eq_self [R.IsRefl] [R.IsTrans] : R ○ R = R := subset_antisymm comp_subset_self left_subset_comp lemma isTrans_iff_comp_subset_self : R.IsTrans ↔ R ○ R ⊆ R where mp _ := comp_subset_self mpr h := ⟨fun _ _ _ hx hy ↦ h ⟨_, hx, hy⟩⟩ instance isTrans_empty : (∅ : SetRel α α).IsTrans where trans _ _ _ := by simp instance isTrans_univ : SetRel.IsTrans (Set.univ : SetRel α α) where trans _ _ _ := by simp instance isTrans_singleton (x : α × α) : SetRel.IsTrans {x} where trans _ _ _ := by aesop instance isTrans_inter [R₁.IsTrans] [R₂.IsTrans] : (R₁ ∩ R₂).IsTrans where trans _a _b _c hab hbc := ⟨R₁.trans hab.1 hbc.1, R₂.trans hab.2 hbc.2⟩ protected lemma IsTrans.sInter {ℛ : Set <| SetRel α α} (hℛ : ∀ R ∈ ℛ, R.IsTrans) : SetRel.IsTrans (⋂₀ ℛ) where trans _a _b _c hab hbc R hR := (hℛ R hR).trans _ _ _ (hab R hR) <| hbc R hR instance isTrans_iInter {R : ι → SetRel α α} [∀ i, (R i).IsTrans] : SetRel.IsTrans (⋂ i, R i) := .sInter <| by simpa instance isTrans_id : (.id : SetRel α α).IsTrans where trans _ _ _ := .trans instance isTrans_preimage {f : β → α} [R.IsTrans] : SetRel.IsTrans (Prod.map f f ⁻¹' R) where trans _ _ _ := R.trans instance isTrans_symmetrize [R.IsTrans] : R.symmetrize.IsTrans where trans _a _b _c hab hbc := ⟨R.trans hab.1 hbc.1, R.trans hbc.2 hab.2⟩ variable (R) in /-- A relation `R` is irreflexive if `¬ a ~[R] a`. -/ protected abbrev IsIrrefl : Prop := IsIrrefl α (· ~[R] ·) variable (R a) in protected lemma irrefl [R.IsIrrefl] : ¬ a ~[R] a := irrefl_of (· ~[R] ·) _ instance {R : α → α → Prop} [IsIrrefl α R] : SetRel.IsIrrefl {(a, b) | R a b} := ‹_› variable (R) in /-- A relation `R` on a type `α` is well-founded if all elements of `α` are accessible within `R`. -/ abbrev IsWellFounded : Prop := WellFounded (· ~[R] ·) variable (R S) in /-- A relation homomorphism with respect to a given pair of relations `R` and `S` s is a function `f : α → β` such that `a ~[R] b → f a ~[s] f b`. -/ abbrev Hom := (· ~[R] ·) →r (· ~[S] ·) end SetRel open Set open scoped SetRel namespace Function variable {f : α → β} {a : α} {b : β} /-- The graph of a function as a relation. -/ def graph (f : α → β) : SetRel α β := {(a, b) | f a = b} @[simp] lemma mem_graph : a ~[f.graph] b ↔ f a = b := .rfl @[deprecated (since := "2025-07-06")] alias graph_def := mem_graph theorem graph_injective : Injective (graph : (α → β) → SetRel α β) := by aesop (add simp [Injective, Set.ext_iff]) @[simp] lemma graph_inj {f g : α → β} : f.graph = g.graph ↔ f = g := graph_injective.eq_iff @[simp] lemma graph_id : graph (id : α → α) = .id := by aesop theorem graph_comp (f : β → γ) (g : α → β) : graph (f ∘ g) = graph g ○ graph f := by aesop end Function theorem Equiv.graph_inv (f : α ≃ β) : (f.symm : β → α).graph = SetRel.inv (f : α → β).graph := by aesop lemma SetRel.exists_graph_eq_iff (R : SetRel α β) : (∃! f, Function.graph f = R) ↔ ∀ a, ∃! b, a ~[R] b := by constructor · rintro ⟨f, rfl, _⟩ x simp intro h choose f hf using fun x ↦ (h x).exists refine ⟨f, ?_, by aesop⟩ ext ⟨a, b⟩ constructor · aesop · exact (h _).unique (hf _) @[deprecated (since := "2025-07-06")] alias SetRelation.is_graph_iff := SetRel.exists_graph_eq_iff namespace Set theorem image_eq (f : α → β) (s : Set α) : f '' s = (Function.graph f).image s := by rfl theorem preimage_eq (f : α → β) (s : Set β) : f ⁻¹' s = (Function.graph f).preimage s := by simp [Set.preimage, SetRel.preimage] theorem preimage_eq_core (f : α → β) (s : Set β) : f ⁻¹' s = (Function.graph f).core s := by simp [Set.preimage, SetRel.core] end Set /-- A shorthand for `α → β → Prop`. Consider using `SetRel` instead if you want extra API for relations. -/ abbrev Rel (α β : Type*) : Type _ := α → β → Prop
.lake/packages/mathlib/Mathlib/Data/Erased.lean
import Mathlib.Logic.Equiv.Defs /-! # A type for VM-erased data This file defines a type `Erased α` which is classically isomorphic to `α`, but erased in the VM. That is, at runtime every value of `Erased α` is represented as `0`, just like types and proofs. -/ universe u /-- `Erased α` is the same as `α`, except that the elements of `Erased α` are erased in the VM in the same way as types and proofs. This can be used to track data without storing it literally. -/ def Erased (α : Sort u) : Sort max 1 u := { s : α → Prop // ∃ a, (a = ·) = s } namespace Erased /-- Erase a value. -/ @[inline] def mk {α} (a : α) : Erased α := ⟨fun b => a = b, a, rfl⟩ /-- Extracts the erased value, noncomputably. -/ noncomputable def out {α} : Erased α → α | ⟨_, h⟩ => Classical.choose h /-- Extracts the erased value, if it is a type. Note: `(mk a).OutType` is not definitionally equal to `a`. -/ abbrev OutType (a : Erased (Sort u)) : Sort u := out a /-- Extracts the erased value, if it is a proof. -/ theorem out_proof {p : Prop} (a : Erased p) : p := out a @[simp] theorem out_mk {α} (a : α) : (mk a).out = a := by let h := (mk a).2; change Classical.choose h = a have := Classical.choose_spec h exact cast (congr_fun this a).symm rfl @[simp] theorem mk_out {α} : ∀ a : Erased α, mk (out a) = a | ⟨s, h⟩ => by simp only [mk]; congr; exact Classical.choose_spec h @[ext] theorem out_inj {α} (a b : Erased α) (h : a.out = b.out) : a = b := by simpa using congr_arg mk h /-- Equivalence between `Erased α` and `α`. -/ noncomputable def equiv (α) : Erased α ≃ α := ⟨out, mk, mk_out, out_mk⟩ instance (α : Type u) : Repr (Erased α) := ⟨fun _ _ => "Erased"⟩ instance (α : Type u) : ToString (Erased α) := ⟨fun _ => "Erased"⟩ /-- Computably produce an erased value from a proof of nonemptiness. -/ def choice {α} (h : Nonempty α) : Erased α := mk (Classical.choice h) @[simp] theorem nonempty_iff {α} : Nonempty (Erased α) ↔ Nonempty α := ⟨fun ⟨a⟩ => ⟨a.out⟩, fun ⟨a⟩ => ⟨mk a⟩⟩ instance {α} [h : Nonempty α] : Inhabited (Erased α) := ⟨choice h⟩ /-- `(>>=)` operation on `Erased`. This is a separate definition because `α` and `β` can live in different universes (the universe is fixed in `Monad`). -/ def bind {α β} (a : Erased α) (f : α → Erased β) : Erased β := ⟨fun b => (f a.out).1 b, (f a.out).2⟩ @[simp] theorem bind_eq_out {α β} (a f) : @bind α β a f = f a.out := rfl /-- Collapses two levels of erasure. -/ def join {α} (a : Erased (Erased α)) : Erased α := bind a id @[simp] theorem join_eq_out {α} (a) : @join α a = a.out := rfl /-- `(<$>)` operation on `Erased`. This is a separate definition because `α` and `β` can live in different universes (the universe is fixed in `Functor`). -/ def map {α β} (f : α → β) (a : Erased α) : Erased β := bind a (mk ∘ f) @[simp] theorem map_out {α β} {f : α → β} (a : Erased α) : (a.map f).out = f a.out := by simp [map] protected instance Monad : Monad Erased where pure := @mk bind := @bind map := @map @[simp] theorem pure_def {α} : (pure : α → Erased α) = @mk _ := rfl @[simp] theorem bind_def {α β} : ((· >>= ·) : Erased α → (α → Erased β) → Erased β) = @bind _ _ := rfl @[simp] theorem map_def {α β} : ((· <$> ·) : (α → β) → Erased α → Erased β) = @map _ _ := rfl protected instance instLawfulMonad : LawfulMonad Erased := { id_map := by intros; ext; simp map_const := by intros; ext; simp [Functor.mapConst] pure_bind := by intros; ext; simp bind_assoc := by intros; ext; simp bind_pure_comp := by intros; ext; simp bind_map := by intros; ext; simp [Seq.seq] seqLeft_eq := by intros; ext; simp [Seq.seq, SeqLeft.seqLeft] seqRight_eq := by intros; ext; simp [Seq.seq, SeqRight.seqRight] pure_seq := by intros; ext; simp [Seq.seq] } end Erased
.lake/packages/mathlib/Mathlib/Data/PFun.lean
import Batteries.WF import Batteries.Tactic.GeneralizeProofs import Mathlib.Data.Part import Mathlib.Data.Rel /-! # Partial functions This file defines partial functions. Partial functions are like functions, except they can also be "undefined" on some inputs. We define them as functions `α → Part β`. ## Definitions * `PFun α β`: Type of partial functions from `α` to `β`. Defined as `α → Part β` and denoted `α →. β`. * `PFun.Dom`: Domain of a partial function. Set of values on which it is defined. Not to be confused with the domain of a function `α → β`, which is a type (`α` presently). * `PFun.fn`: Evaluation of a partial function. Takes in an element and a proof it belongs to the partial function's `Dom`. * `PFun.asSubtype`: Returns a partial function as a function from its `Dom`. * `PFun.toSubtype`: Restricts the codomain of a function to a subtype. * `PFun.evalOpt`: Returns a partial function with a decidable `Dom` as a function `a → Option β`. * `PFun.lift`: Turns a function into a partial function. * `PFun.id`: The identity as a partial function. * `PFun.comp`: Composition of partial functions. * `PFun.restrict`: Restriction of a partial function to a smaller `Dom`. * `PFun.res`: Turns a function into a partial function with a prescribed domain. * `PFun.fix` : First return map of a partial function `f : α →. β ⊕ α`. * `PFun.fix_induction`: A recursion principle for `PFun.fix`. ### Partial functions as relations Partial functions can be considered as relations, so we specialize some `Rel` definitions to `PFun`: * `PFun.image`: Image of a set under a partial function. * `PFun.ran`: Range of a partial function. * `PFun.preimage`: Preimage of a set under a partial function. * `PFun.core`: Core of a set under a partial function. * `PFun.graph`: Graph of a partial function `a →. β`as a `Set (α × β)`. * `PFun.graph'`: Graph of a partial function `a →. β`as a `Rel α β`. ### `PFun α` as a monad Monad operations: * `PFun.pure`: The monad `pure` function, the constant `x` function. * `PFun.bind`: The monad `bind` function, pointwise `Part.bind` * `PFun.map`: The monad `map` function, pointwise `Part.map`. -/ open Function /-- `PFun α β`, or `α →. β`, is the type of partial functions from `α` to `β`. It is defined as `α → Part β`. -/ def PFun (α β : Type*) := α → Part β /-- `α →. β` is notation for the type `PFun α β` of partial functions from `α` to `β`. -/ infixr:25 " →. " => PFun namespace PFun variable {α β γ δ ε ι : Type*} instance inhabited : Inhabited (α →. β) := ⟨fun _ => Part.none⟩ /-- The domain of a partial function -/ def Dom (f : α →. β) : Set α := { a | (f a).Dom } @[simp] theorem mem_dom (f : α →. β) (x : α) : x ∈ Dom f ↔ ∃ y, y ∈ f x := by simp [Dom, Part.dom_iff_mem] @[simp] theorem dom_mk (p : α → Prop) (f : ∀ a, p a → β) : (PFun.Dom fun x => ⟨p x, f x⟩) = { x | p x } := rfl theorem dom_eq (f : α →. β) : Dom f = { x | ∃ y, y ∈ f x } := Set.ext (mem_dom f) /-- Evaluate a partial function -/ def fn (f : α →. β) (a : α) : Dom f a → β := (f a).get @[simp] theorem fn_apply (f : α →. β) (a : α) : f.fn a = (f a).get := rfl /-- Evaluate a partial function to return an `Option` -/ def evalOpt (f : α →. β) [D : DecidablePred (· ∈ Dom f)] (x : α) : Option β := @Part.toOption _ _ (D x) /-- Partial function extensionality -/ theorem ext' {f g : α →. β} (H1 : ∀ a, a ∈ Dom f ↔ a ∈ Dom g) (H2 : ∀ a p q, f.fn a p = g.fn a q) : f = g := funext fun a => Part.ext' (H1 a) (H2 a) @[ext] theorem ext {f g : α →. β} (H : ∀ a b, b ∈ f a ↔ b ∈ g a) : f = g := funext fun a => Part.ext (H a) /-- Turns a partial function into a function out of its domain. -/ def asSubtype (f : α →. β) (s : f.Dom) : β := f.fn s s.2 /-- The type of partial functions `α →. β` is equivalent to the type of pairs `(p : α → Prop, f : Subtype p → β)`. -/ def equivSubtype : (α →. β) ≃ Σ p : α → Prop, Subtype p → β := ⟨fun f => ⟨fun a => (f a).Dom, asSubtype f⟩, fun f x => ⟨f.1 x, fun h => f.2 ⟨x, h⟩⟩, fun _ => funext fun _ => Part.eta _, fun ⟨p, f⟩ => by dsimp; congr⟩ theorem asSubtype_eq_of_mem {f : α →. β} {x : α} {y : β} (fxy : y ∈ f x) (domx : x ∈ f.Dom) : f.asSubtype ⟨x, domx⟩ = y := Part.mem_unique (Part.get_mem _) fxy /-- Turn a total function into a partial function. -/ @[coe] protected def lift (f : α → β) : α →. β := fun a => Part.some (f a) instance coe : Coe (α → β) (α →. β) := ⟨PFun.lift⟩ @[simp] theorem coe_val (f : α → β) (a : α) : (f : α →. β) a = Part.some (f a) := rfl @[simp] theorem dom_coe (f : α → β) : (f : α →. β).Dom = Set.univ := rfl theorem lift_injective : Injective (PFun.lift : (α → β) → α →. β) := fun _ _ h => funext fun a => Part.some_injective <| congr_fun h a /-- Graph of a partial function `f` as the set of pairs `(x, f x)` where `x` is in the domain of `f`. -/ def graph (f : α →. β) : Set (α × β) := { p | p.2 ∈ f p.1 } /-- Graph of a partial function as a relation. `x` and `y` are related iff `f x` is defined and "equals" `y`. -/ def graph' (f : α →. β) : SetRel α β := {(x, y) : α × β | y ∈ f x} /-- The range of a partial function is the set of values `f x` where `x` is in the domain of `f`. -/ def ran (f : α →. β) : Set β := { b | ∃ a, b ∈ f a } /-- Restrict a partial function to a smaller domain. -/ def restrict (f : α →. β) {p : Set α} (H : p ⊆ f.Dom) : α →. β := fun x => (f x).restrict (x ∈ p) (@H x) @[simp] theorem mem_restrict {f : α →. β} {s : Set α} (h : s ⊆ f.Dom) (a : α) (b : β) : b ∈ f.restrict h a ↔ a ∈ s ∧ b ∈ f a := by simp [restrict] /-- Turns a function into a partial function with a prescribed domain. -/ def res (f : α → β) (s : Set α) : α →. β := (PFun.lift f).restrict s.subset_univ theorem mem_res (f : α → β) (s : Set α) (a : α) (b : β) : b ∈ res f s a ↔ a ∈ s ∧ f a = b := by simp [res, @eq_comm _ b] theorem res_univ (f : α → β) : PFun.res f Set.univ = f := rfl theorem dom_iff_graph (f : α →. β) (x : α) : x ∈ f.Dom ↔ ∃ y, (x, y) ∈ f.graph := Part.dom_iff_mem theorem lift_graph {f : α → β} {a b} : (a, b) ∈ (f : α →. β).graph ↔ f a = b := show (∃ _ : True, f a = b) ↔ f a = b by simp /-- The monad `pure` function, the total constant `x` function -/ protected def pure (x : β) : α →. β := fun _ => Part.some x /-- The monad `bind` function, pointwise `Part.bind` -/ def bind (f : α →. β) (g : β → α →. γ) : α →. γ := fun a => (f a).bind fun b => g b a @[simp] theorem bind_apply (f : α →. β) (g : β → α →. γ) (a : α) : f.bind g a = (f a).bind fun b => g b a := rfl /-- The monad `map` function, pointwise `Part.map` -/ def map (f : β → γ) (g : α →. β) : α →. γ := fun a => (g a).map f instance monad : Monad (PFun α) where pure := PFun.pure bind := PFun.bind map := PFun.map instance lawfulMonad : LawfulMonad (PFun α) := LawfulMonad.mk' (bind_pure_comp := fun _ _ => funext fun _ => Part.bind_some_eq_map _ _) (id_map := fun f => by funext a; dsimp [Functor.map, PFun.map]; cases f a; rfl) (pure_bind := fun x f => funext fun _ => Part.bind_some _ (f x)) (bind_assoc := fun f g k => funext fun a => (f a).bind_assoc (fun b => g b a) fun b => k b a) theorem pure_defined (p : Set α) (x : β) : p ⊆ (@PFun.pure α _ x).Dom := p.subset_univ theorem bind_defined {α β γ} (p : Set α) {f : α →. β} {g : β → α →. γ} (H1 : p ⊆ f.Dom) (H2 : ∀ x, p ⊆ (g x).Dom) : p ⊆ (f >>= g).Dom := fun a ha => (⟨H1 ha, H2 _ ha⟩ : (f >>= g).Dom a) /-- First return map. Transforms a partial function `f : α →. β ⊕ α` into the partial function `α →. β` which sends `a : α` to the first value in `β` it hits by iterating `f`, if such a value exists. By abusing notation to illustrate, either `f a` is in the `β` part of `β ⊕ α` (in which case `f.fix a` returns `f a`), or it is undefined (in which case `f.fix a` is undefined as well), or it is in the `α` part of `β ⊕ α` (in which case we repeat the procedure, so `f.fix a` will return `f.fix (f a)`). -/ def fix (f : α →. β ⊕ α) : α →. β := fun a => Part.assert (Acc (fun x y => Sum.inr x ∈ f y) a) fun h => WellFounded.fixF (fun a IH => Part.assert (f a).Dom fun hf => match e : (f a).get hf with | Sum.inl b => Part.some b | Sum.inr a' => IH a' ⟨hf, e⟩) a h theorem dom_of_mem_fix {f : α →. β ⊕ α} {a : α} {b : β} (h : b ∈ f.fix a) : (f a).Dom := by let ⟨h₁, h₂⟩ := Part.mem_assert_iff.1 h rw [WellFounded.fixF_eq] at h₂; exact h₂.fst.fst theorem mem_fix_iff {f : α →. β ⊕ α} {a : α} {b : β} : b ∈ f.fix a ↔ Sum.inl b ∈ f a ∨ ∃ a', Sum.inr a' ∈ f a ∧ b ∈ f.fix a' := ⟨fun h => by let ⟨h₁, h₂⟩ := Part.mem_assert_iff.1 h rw [WellFounded.fixF_eq] at h₂ simp only [Part.mem_assert_iff] at h₂ obtain ⟨h₂, h₃⟩ := h₂ split at h₃ next e => simp only [Part.mem_some_iff] at h₃; subst b; exact Or.inl ⟨h₂, e⟩ next e => exact Or.inr ⟨_, ⟨_, e⟩, Part.mem_assert _ h₃⟩, fun h => by simp only [fix, Part.mem_assert_iff] rcases h with (⟨h₁, h₂⟩ | ⟨a', h, h₃⟩) · refine ⟨⟨_, fun y h' => ?_⟩, ?_⟩ · injection Part.mem_unique ⟨h₁, h₂⟩ h' · rw [WellFounded.fixF_eq] -- Porting note: used to be simp [h₁, h₂] apply Part.mem_assert h₁ split next e => injection h₂.symm.trans e with h; simp [h] next e => injection h₂.symm.trans e · simp only [fix, Part.mem_assert_iff] at h₃ obtain ⟨h₃, h₄⟩ := h₃ refine ⟨⟨_, fun y h' => ?_⟩, ?_⟩ · injection Part.mem_unique h h' with e exact e ▸ h₃ · obtain ⟨h₁, h₂⟩ := h rw [WellFounded.fixF_eq] -- Porting note: used to be simp [h₁, h₂, h₄] apply Part.mem_assert h₁ split next e => injection h₂.symm.trans e next e => injection h₂.symm.trans e; subst a'; exact h₄⟩ /-- If advancing one step from `a` leads to `b : β`, then `f.fix a = b` -/ theorem fix_stop {f : α →. β ⊕ α} {b : β} {a : α} (hb : Sum.inl b ∈ f a) : b ∈ f.fix a := by rw [PFun.mem_fix_iff] exact Or.inl hb /-- If advancing one step from `a` on `f` leads to `a' : α`, then `f.fix a = f.fix a'` -/ theorem fix_fwd_eq {f : α →. β ⊕ α} {a a' : α} (ha' : Sum.inr a' ∈ f a) : f.fix a = f.fix a' := by ext b; constructor · intro h obtain h' | ⟨a, h', e'⟩ := mem_fix_iff.1 h <;> cases Part.mem_unique ha' h' exact e' · intro h rw [PFun.mem_fix_iff] exact Or.inr ⟨a', ha', h⟩ theorem fix_fwd {f : α →. β ⊕ α} {b : β} {a a' : α} (hb : b ∈ f.fix a) (ha' : Sum.inr a' ∈ f a) : b ∈ f.fix a' := by rwa [← fix_fwd_eq ha'] /-- A recursion principle for `PFun.fix`. -/ @[elab_as_elim] def fixInduction {C : α → Sort*} {f : α →. β ⊕ α} {b : β} {a : α} (h : b ∈ f.fix a) (H : ∀ a', b ∈ f.fix a' → (∀ a'', Sum.inr a'' ∈ f a' → C a'') → C a') : C a := by have h₂ := (Part.mem_assert_iff.1 h).snd generalize_proofs at h₂ clear h induction ‹Acc _ _› with | intro a ha IH => _ have h : b ∈ f.fix a := Part.mem_assert_iff.2 ⟨⟨a, ha⟩, h₂⟩ exact H a h fun a' fa' => IH a' fa' (Part.mem_assert_iff.1 (fix_fwd h fa')).snd theorem fixInduction_spec {C : α → Sort*} {f : α →. β ⊕ α} {b : β} {a : α} (h : b ∈ f.fix a) (H : ∀ a', b ∈ f.fix a' → (∀ a'', Sum.inr a'' ∈ f a' → C a'') → C a') : @fixInduction _ _ C _ _ _ h H = H a h fun _ h' => fixInduction (fix_fwd h h') H := by unfold fixInduction generalize_proofs induction ‹Acc _ _› rfl /-- Another induction lemma for `b ∈ f.fix a` which allows one to prove a predicate `P` holds for `a` given that `f a` inherits `P` from `a` and `P` holds for preimages of `b`. -/ @[elab_as_elim] def fixInduction' {C : α → Sort*} {f : α →. β ⊕ α} {b : β} {a : α} (h : b ∈ f.fix a) (hbase : ∀ a_final : α, Sum.inl b ∈ f a_final → C a_final) (hind : ∀ a₀ a₁ : α, b ∈ f.fix a₁ → Sum.inr a₁ ∈ f a₀ → C a₁ → C a₀) : C a := by refine fixInduction h fun a' h ih => ?_ rcases e : (f a').get (dom_of_mem_fix h) with b' | a'' <;> replace e : _ ∈ f a' := ⟨_, e⟩ · apply hbase convert e exact Part.mem_unique h (fix_stop e) · exact hind _ _ (fix_fwd h e) e (ih _ e) theorem fixInduction'_stop {C : α → Sort*} {f : α →. β ⊕ α} {b : β} {a : α} (h : b ∈ f.fix a) (fa : Sum.inl b ∈ f a) (hbase : ∀ a_final : α, Sum.inl b ∈ f a_final → C a_final) (hind : ∀ a₀ a₁ : α, b ∈ f.fix a₁ → Sum.inr a₁ ∈ f a₀ → C a₁ → C a₀) : @fixInduction' _ _ C _ _ _ h hbase hind = hbase a fa := by unfold fixInduction' rw [fixInduction_spec] -- Porting note: the explicit motive required because `simp` does not apply `Part.get_eq_of_mem` refine Eq.rec (motive := fun x e ↦ Sum.casesOn x ?_ ?_ (Eq.trans (Part.get_eq_of_mem fa (dom_of_mem_fix h)) e) = hbase a fa) ?_ (Part.get_eq_of_mem fa (dom_of_mem_fix h)).symm simp theorem fixInduction'_fwd {C : α → Sort*} {f : α →. β ⊕ α} {b : β} {a a' : α} (h : b ∈ f.fix a) (h' : b ∈ f.fix a') (fa : Sum.inr a' ∈ f a) (hbase : ∀ a_final : α, Sum.inl b ∈ f a_final → C a_final) (hind : ∀ a₀ a₁ : α, b ∈ f.fix a₁ → Sum.inr a₁ ∈ f a₀ → C a₁ → C a₀) : @fixInduction' _ _ C _ _ _ h hbase hind = hind a a' h' fa (fixInduction' h' hbase hind) := by unfold fixInduction' rw [fixInduction_spec] -- Porting note: the explicit motive required because `simp` does not apply `Part.get_eq_of_mem` refine Eq.rec (motive := fun x e => Sum.casesOn (motive := fun y => (f a).get (dom_of_mem_fix h) = y → C a) x ?_ ?_ (Eq.trans (Part.get_eq_of_mem fa (dom_of_mem_fix h)) e) = _) ?_ (Part.get_eq_of_mem fa (dom_of_mem_fix h)).symm simp variable (f : α →. β) /-- Image of a set under a partial function. -/ def image (s : Set α) : Set β := f.graph'.image s theorem image_def (s : Set α) : f.image s = { y | ∃ x ∈ s, y ∈ f x } := rfl theorem mem_image (y : β) (s : Set α) : y ∈ f.image s ↔ ∃ x ∈ s, y ∈ f x := Iff.rfl theorem image_mono {s t : Set α} (h : s ⊆ t) : f.image s ⊆ f.image t := SetRel.image_mono h theorem image_inter (s t : Set α) : f.image (s ∩ t) ⊆ f.image s ∩ f.image t := SetRel.image_inter_subset _ theorem image_union (s t : Set α) : f.image (s ∪ t) = f.image s ∪ f.image t := SetRel.image_union _ s t /-- Preimage of a set under a partial function. -/ def preimage (s : Set β) : Set α := f.graph'.preimage s theorem Preimage_def (s : Set β) : f.preimage s = { x | ∃ y ∈ s, y ∈ f x } := rfl @[simp] theorem mem_preimage (s : Set β) (x : α) : x ∈ f.preimage s ↔ ∃ y ∈ s, y ∈ f x := Iff.rfl theorem preimage_subset_dom (s : Set β) : f.preimage s ⊆ f.Dom := fun _ ⟨y, _, fxy⟩ => Part.dom_iff_mem.mpr ⟨y, fxy⟩ theorem preimage_mono {s t : Set β} (h : s ⊆ t) : f.preimage s ⊆ f.preimage t := SetRel.preimage_mono h theorem preimage_inter (s t : Set β) : f.preimage (s ∩ t) ⊆ f.preimage s ∩ f.preimage t := SetRel.preimage_inter_subset _ theorem preimage_union (s t : Set β) : f.preimage (s ∪ t) = f.preimage s ∪ f.preimage t := SetRel.preimage_union _ s t theorem preimage_univ : f.preimage Set.univ = f.Dom := by ext; simp [mem_preimage, mem_dom] theorem coe_preimage (f : α → β) (s : Set β) : (f : α →. β).preimage s = f ⁻¹' s := by ext; simp /-- Core of a set `s : Set β` with respect to a partial function `f : α →. β`. Set of all `a : α` such that `f a ∈ s`, if `f a` is defined. -/ def core (s : Set β) : Set α := f.graph'.core s theorem core_def (s : Set β) : f.core s = { x | ∀ y, y ∈ f x → y ∈ s } := rfl @[simp] theorem mem_core (x : α) (s : Set β) : x ∈ f.core s ↔ ∀ y, y ∈ f x → y ∈ s := Iff.rfl theorem compl_dom_subset_core (s : Set β) : f.Domᶜ ⊆ f.core s := fun x hx y fxy => absurd ((mem_dom f x).mpr ⟨y, fxy⟩) hx theorem core_mono {s t : Set β} (h : s ⊆ t) : f.core s ⊆ f.core t := SetRel.core_mono h theorem core_inter (s t : Set β) : f.core (s ∩ t) = f.core s ∩ f.core t := SetRel.core_inter _ s t theorem mem_core_res (f : α → β) (s : Set α) (t : Set β) (x : α) : x ∈ (res f s).core t ↔ x ∈ s → f x ∈ t := by simp [mem_core, mem_res] theorem core_res (f : α → β) (s : Set α) (t : Set β) : (res f s).core t = sᶜ ∪ f ⁻¹' t := by ext x rw [mem_core_res] by_cases h : x ∈ s <;> simp [h] theorem core_restrict (f : α → β) (s : Set β) : (f : α →. β).core s = s.preimage f := by ext x; simp [core_def] theorem preimage_subset_core (f : α →. β) (s : Set β) : f.preimage s ⊆ f.core s := fun _ ⟨y, ys, fxy⟩ y' fxy' => have : y = y' := Part.mem_unique fxy fxy' this ▸ ys theorem preimage_eq (f : α →. β) (s : Set β) : f.preimage s = f.core s ∩ f.Dom := Set.eq_of_subset_of_subset (Set.subset_inter (f.preimage_subset_core s) (f.preimage_subset_dom s)) fun x ⟨xcore, xdom⟩ => let y := (f x).get xdom have ys : y ∈ s := xcore (Part.get_mem _) show x ∈ f.preimage s from ⟨(f x).get xdom, ys, Part.get_mem _⟩ theorem core_eq (f : α →. β) (s : Set β) : f.core s = f.preimage s ∪ f.Domᶜ := by rw [preimage_eq, Set.inter_union_distrib_right, Set.union_comm (Dom f), Set.compl_union_self, Set.inter_univ, Set.union_eq_self_of_subset_right (f.compl_dom_subset_core s)] theorem preimage_asSubtype (f : α →. β) (s : Set β) : f.asSubtype ⁻¹' s = Subtype.val ⁻¹' f.preimage s := by ext x simp only [Set.mem_preimage, PFun.asSubtype, PFun.mem_preimage] show f.fn x.val _ ∈ s ↔ ∃ y ∈ s, y ∈ f x.val exact Iff.intro (fun h => ⟨_, h, Part.get_mem _⟩) fun ⟨y, ys, fxy⟩ => have : f.fn x.val x.property ∈ f x.val := Part.get_mem _ Part.mem_unique fxy this ▸ ys /-- Turns a function into a partial function to a subtype. -/ def toSubtype (p : β → Prop) (f : α → β) : α →. Subtype p := fun a => ⟨p (f a), Subtype.mk _⟩ @[simp] theorem dom_toSubtype (p : β → Prop) (f : α → β) : (toSubtype p f).Dom = { a | p (f a) } := rfl @[simp] theorem toSubtype_apply (p : β → Prop) (f : α → β) (a : α) : toSubtype p f a = ⟨p (f a), Subtype.mk _⟩ := rfl theorem dom_toSubtype_apply_iff {p : β → Prop} {f : α → β} {a : α} : (toSubtype p f a).Dom ↔ p (f a) := Iff.rfl theorem mem_toSubtype_iff {p : β → Prop} {f : α → β} {a : α} {b : Subtype p} : b ∈ toSubtype p f a ↔ ↑b = f a := by rw [toSubtype_apply, Part.mem_mk_iff, exists_subtype_mk_eq_iff, eq_comm] /-- The identity as a partial function -/ protected def id (α : Type*) : α →. α := Part.some @[simp, norm_cast] theorem coe_id (α : Type*) : ((id : α → α) : α →. α) = PFun.id α := rfl @[simp] theorem id_apply (a : α) : PFun.id α a = Part.some a := rfl /-- Composition of partial functions as a partial function. -/ def comp (f : β →. γ) (g : α →. β) : α →. γ := fun a => (g a).bind f @[simp] theorem comp_apply (f : β →. γ) (g : α →. β) (a : α) : f.comp g a = (g a).bind f := rfl @[simp] theorem id_comp (f : α →. β) : (PFun.id β).comp f = f := ext fun _ _ => by simp @[simp] theorem comp_id (f : α →. β) : f.comp (PFun.id α) = f := ext fun _ _ => by simp @[simp] theorem dom_comp (f : β →. γ) (g : α →. β) : (f.comp g).Dom = g.preimage f.Dom := by ext simp_rw [mem_preimage, mem_dom, comp_apply, Part.mem_bind_iff, ← exists_and_right] rw [exists_comm] simp_rw [and_comm] @[simp] theorem preimage_comp (f : β →. γ) (g : α →. β) (s : Set γ) : (f.comp g).preimage s = g.preimage (f.preimage s) := by ext simp_rw [mem_preimage, comp_apply, Part.mem_bind_iff, ← exists_and_right, ← exists_and_left] rw [exists_comm] simp_rw [and_assoc, and_comm] @[simp] theorem Part.bind_comp (f : β →. γ) (g : α →. β) (a : Part α) : a.bind (f.comp g) = (a.bind g).bind f := by ext c simp_rw [Part.mem_bind_iff, comp_apply, Part.mem_bind_iff, ← exists_and_right, ← exists_and_left] rw [exists_comm] simp_rw [and_assoc] @[simp] theorem comp_assoc (f : γ →. δ) (g : β →. γ) (h : α →. β) : (f.comp g).comp h = f.comp (g.comp h) := ext fun _ _ => by simp only [comp_apply, Part.bind_comp] -- This can't be `simp` theorem coe_comp (g : β → γ) (f : α → β) : ((g ∘ f : α → γ) : α →. γ) = (g : β →. γ).comp f := ext fun _ _ => by simp only [coe_val, comp_apply, Function.comp, Part.bind_some] /-- Product of partial functions. -/ def prodLift (f : α →. β) (g : α →. γ) : α →. β × γ := fun x => ⟨(f x).Dom ∧ (g x).Dom, fun h => ((f x).get h.1, (g x).get h.2)⟩ @[simp] theorem dom_prodLift (f : α →. β) (g : α →. γ) : (f.prodLift g).Dom = { x | (f x).Dom ∧ (g x).Dom } := rfl theorem get_prodLift (f : α →. β) (g : α →. γ) (x : α) (h) : (f.prodLift g x).get h = ((f x).get h.1, (g x).get h.2) := rfl @[simp] theorem prodLift_apply (f : α →. β) (g : α →. γ) (x : α) : f.prodLift g x = ⟨(f x).Dom ∧ (g x).Dom, fun h => ((f x).get h.1, (g x).get h.2)⟩ := rfl theorem mem_prodLift {f : α →. β} {g : α →. γ} {x : α} {y : β × γ} : y ∈ f.prodLift g x ↔ y.1 ∈ f x ∧ y.2 ∈ g x := by trans ∃ hp hq, (f x).get hp = y.1 ∧ (g x).get hq = y.2 · simp only [prodLift, Part.mem_mk_iff, And.exists, Prod.ext_iff] · simp only [exists_and_left, exists_and_right, Membership.mem, Part.Mem] /-- Product of partial functions. -/ def prodMap (f : α →. γ) (g : β →. δ) : α × β →. γ × δ := fun x => ⟨(f x.1).Dom ∧ (g x.2).Dom, fun h => ((f x.1).get h.1, (g x.2).get h.2)⟩ @[simp] theorem dom_prodMap (f : α →. γ) (g : β →. δ) : (f.prodMap g).Dom = { x | (f x.1).Dom ∧ (g x.2).Dom } := rfl theorem get_prodMap (f : α →. γ) (g : β →. δ) (x : α × β) (h) : (f.prodMap g x).get h = ((f x.1).get h.1, (g x.2).get h.2) := rfl @[simp] theorem prodMap_apply (f : α →. γ) (g : β →. δ) (x : α × β) : f.prodMap g x = ⟨(f x.1).Dom ∧ (g x.2).Dom, fun h => ((f x.1).get h.1, (g x.2).get h.2)⟩ := rfl theorem mem_prodMap {f : α →. γ} {g : β →. δ} {x : α × β} {y : γ × δ} : y ∈ f.prodMap g x ↔ y.1 ∈ f x.1 ∧ y.2 ∈ g x.2 := by trans ∃ hp hq, (f x.1).get hp = y.1 ∧ (g x.2).get hq = y.2 · simp only [prodMap, Part.mem_mk_iff, And.exists, Prod.ext_iff] · simp only [exists_and_left, exists_and_right, Membership.mem, Part.Mem] @[simp] theorem prodLift_fst_comp_snd_comp (f : α →. γ) (g : β →. δ) : prodLift (f.comp ((Prod.fst : α × β → α) : α × β →. α)) (g.comp ((Prod.snd : α × β → β) : α × β →. β)) = prodMap f g := by aesop @[simp] theorem prodMap_id_id : (PFun.id α).prodMap (PFun.id β) = PFun.id _ := by aesop @[simp] theorem prodMap_comp_comp (f₁ : α →. β) (f₂ : β →. γ) (g₁ : δ →. ε) (g₂ : ε →. ι) : (f₂.comp f₁).prodMap (g₂.comp g₁) = (f₂.prodMap g₂).comp (f₁.prodMap g₁) := -- `aesop` can prove this but takes over a second, so we do it manually ext <| fun ⟨_, _⟩ ⟨_, _⟩ ↦ ⟨fun ⟨⟨⟨h1l1, h1l2⟩, ⟨h1r1, h1r2⟩⟩, h2⟩ ↦ ⟨⟨⟨h1l1, h1r1⟩, ⟨h1l2, h1r2⟩⟩, h2⟩, fun ⟨⟨⟨h1l1, h1r1⟩, ⟨h1l2, h1r2⟩⟩, h2⟩ ↦ ⟨⟨⟨h1l1, h1l2⟩, ⟨h1r1, h1r2⟩⟩, h2⟩⟩ end PFun
.lake/packages/mathlib/Mathlib/Data/TwoPointing.lean
import Mathlib.Logic.Nontrivial.Defs import Mathlib.Logic.Nonempty import Mathlib.Tactic.Simps.Basic import Batteries.Logic /-! # Two-pointings This file defines `TwoPointing α`, the type of two pointings of `α`. A two-pointing is the data of two distinct terms. This is morally a Type-valued `Nontrivial`. Another type which is quite close in essence is `Sym2`. Categorically speaking, `prod` is a cospan in the category of types. This forms the category of bipointed types. Two-pointed types form a full subcategory of those. ## References * [nLab, *Coalgebra of the real interval*] (https://ncatlab.org/nlab/show/coalgebra+of+the+real+interval) -/ open Function variable {α β : Type*} /-- Two-pointing of a type. This is a Type-valued termed `Nontrivial`. -/ @[ext] structure TwoPointing (α : Type*) extends α × α where /-- `fst` and `snd` are distinct terms -/ fst_ne_snd : fst ≠ snd deriving DecidableEq initialize_simps_projections TwoPointing (+toProd, -fst, -snd) namespace TwoPointing variable (p : TwoPointing α) (q : TwoPointing β) theorem snd_ne_fst : p.snd ≠ p.fst := p.fst_ne_snd.symm /-- Swaps the two pointed elements. -/ @[simps] def swap : TwoPointing α := ⟨(p.snd, p.fst), p.snd_ne_fst⟩ theorem swap_fst : p.swap.fst = p.snd := rfl theorem swap_snd : p.swap.snd = p.fst := rfl @[simp] theorem swap_swap : p.swap.swap = p := rfl include p in theorem to_nontrivial : Nontrivial α := ⟨⟨p.fst, p.snd, p.fst_ne_snd⟩⟩ instance [Nontrivial α] : Nonempty (TwoPointing α) := let ⟨a, b, h⟩ := exists_pair_ne α ⟨⟨(a, b), h⟩⟩ @[simp] theorem nonempty_two_pointing_iff : Nonempty (TwoPointing α) ↔ Nontrivial α := ⟨fun ⟨p⟩ ↦ p.to_nontrivial, fun _ => inferInstance⟩ section Pi variable (α) [Nonempty α] /-- The two-pointing of constant functions. -/ def pi : TwoPointing (α → β) where fst _ := q.fst snd _ := q.snd fst_ne_snd h := q.fst_ne_snd (congr_fun h (Classical.arbitrary α)) @[simp] theorem pi_fst : (q.pi α).fst = const α q.fst := rfl @[simp] theorem pi_snd : (q.pi α).snd = const α q.snd := rfl end Pi /-- The product of two two-pointings. -/ def prod : TwoPointing (α × β) where fst := (p.fst, q.fst) snd := (p.snd, q.snd) fst_ne_snd h := p.fst_ne_snd (congr_arg Prod.fst h) @[simp] theorem prod_fst : (p.prod q).fst = (p.fst, q.fst) := rfl @[simp] theorem prod_snd : (p.prod q).snd = (p.snd, q.snd) := rfl /-- The sum of two pointings. Keeps the first point from the left and the second point from the right. -/ protected def sum : TwoPointing (α ⊕ β) := ⟨(Sum.inl p.fst, Sum.inr q.snd), Sum.inl_ne_inr⟩ @[simp] theorem sum_fst : (p.sum q).fst = Sum.inl p.fst := rfl @[simp] theorem sum_snd : (p.sum q).snd = Sum.inr q.snd := rfl /-- The `false`, `true` two-pointing of `Bool`. -/ protected def bool : TwoPointing Bool := ⟨(false, true), Bool.false_ne_true⟩ @[simp] theorem bool_fst : TwoPointing.bool.fst = false := rfl @[simp] theorem bool_snd : TwoPointing.bool.snd = true := rfl instance : Inhabited (TwoPointing Bool) := ⟨TwoPointing.bool⟩ /-- The `False`, `True` two-pointing of `Prop`. -/ protected def prop : TwoPointing Prop := ⟨(False, True), false_ne_true⟩ @[simp] theorem prop_fst : TwoPointing.prop.fst = False := rfl @[simp] theorem prop_snd : TwoPointing.prop.snd = True := rfl end TwoPointing
.lake/packages/mathlib/Mathlib/Data/BitVec.lean
import Mathlib.Algebra.Ring.InjSurj import Mathlib.Algebra.Ring.Equiv import Mathlib.Data.ZMod.Defs import Mathlib.Data.Int.Cast.Lemmas /-! # Basic Theorems About Bitvectors This file contains theorems about bitvectors which can only be stated in Mathlib or downstream because they refer to other notions defined in Mathlib. Please do not extend this file further: material about BitVec needed in downstream projects can either be PR'd to Lean, or kept downstream if it also relies on Mathlib. -/ namespace BitVec variable {w : Nat} -- TODO: move to the Lean4 repository. open Fin.CommRing in theorem ofFin_intCast (z : ℤ) : ofFin (z : Fin (2 ^ w)) = ↑z := by cases w case zero => simp only [eq_nil] case succ w => apply BitVec.eq_of_toInt_eq rw [toInt_ofFin, Fin.val_intCast, Int.natCast_pow, Nat.cast_ofNat, Int.ofNat_toNat, toInt_intCast] rw [Int.max_eq_left] · have h : (2 ^ (w + 1) : Int) = (2 ^ (w + 1) : Nat) := by simp rw [h, Int.emod_bmod] · omega open Fin.CommRing in @[simp] theorem toFin_intCast (z : ℤ) : (z : BitVec w).toFin = ↑z := by rw [← ofFin_intCast] /-! ## Injectivity -/ theorem toNat_injective {n : Nat} : Function.Injective (BitVec.toNat : BitVec n → _) | ⟨_, _⟩, ⟨_, _⟩, rfl => rfl theorem toFin_injective {n : Nat} : Function.Injective (toFin : BitVec n → _) | ⟨_, _⟩, ⟨_, _⟩, rfl => rfl /-! ## Scalar Multiplication and Powers -/ open Fin.NatCast lemma toFin_nsmul (n : ℕ) (x : BitVec w) : toFin (n • x) = n • x.toFin := toFin_mul _ _ |>.trans <| by open scoped Fin.CommRing in simp only [natCast_eq_ofNat, toFin_ofNat, Fin.ofNat_eq_cast, nsmul_eq_mul] lemma toFin_zsmul (z : ℤ) (x : BitVec w) : toFin (z • x) = z • x.toFin := toFin_mul _ _ |>.trans <| by open scoped Fin.CommRing in simp only [zsmul_eq_mul, toFin_intCast] lemma toFin_pow (x : BitVec w) (n : ℕ) : toFin (x ^ n) = x.toFin ^ n := by induction n with | zero => simp | succ n ih => simp [ih, BitVec.pow_succ, pow_succ] /-! ## Ring -/ -- Verify that the `HPow` instance from Lean agrees definitionally with the instance via `Monoid`. example : @instHPow (Fin (2 ^ w)) ℕ Monoid.toNatPow = Lean.Grind.Fin.instHPowFinNatOfNeZero := rfl instance : CommSemiring (BitVec w) := open Fin.CommRing in toFin_injective.commSemiring _ toFin_zero toFin_one toFin_add toFin_mul toFin_nsmul toFin_pow toFin_natCast -- The statement in the new API would be: `n#(k.succ) = ((n / 2)#k).concat (n % 2 != 0)` instance : CommRing (BitVec w) := open Fin.CommRing in toFin_injective.commRing _ toFin_zero toFin_one toFin_add toFin_mul toFin_neg toFin_sub toFin_nsmul toFin_zsmul toFin_pow toFin_natCast toFin_intCast /-- The ring `BitVec m` is isomorphic to `Fin (2 ^ m)`. -/ @[simps] def equivFin {m : ℕ} : BitVec m ≃+* Fin (2 ^ m) where toFun a := a.toFin invFun a := ofFin a map_mul' := toFin_mul map_add' := toFin_add end BitVec
.lake/packages/mathlib/Mathlib/Data/ULift.lean
import Mathlib.Control.ULift import Mathlib.Logic.Equiv.Basic /-! # Extra lemmas about `ULift` and `PLift` In this file we provide `Subsingleton`, `Unique`, `DecidableEq`, and `isEmpty` instances for `ULift α` and `PLift α`. We also prove `ULift.forall`, `ULift.exists`, `PLift.forall`, and `PLift.exists`. -/ universe u v u' v' open Function namespace PLift variable {α : Sort u} {β : Sort v} {f : α → β} instance [Subsingleton α] : Subsingleton (PLift α) := Equiv.plift.subsingleton instance [Nonempty α] : Nonempty (PLift α) := Equiv.plift.nonempty instance [Unique α] : Unique (PLift α) := Equiv.plift.unique instance [DecidableEq α] : DecidableEq (PLift α) := Equiv.plift.decidableEq instance [IsEmpty α] : IsEmpty (PLift α) := Equiv.plift.isEmpty theorem up_injective : Injective (@up α) := Equiv.plift.symm.injective theorem up_surjective : Surjective (@up α) := Equiv.plift.symm.surjective theorem up_bijective : Bijective (@up α) := Equiv.plift.symm.bijective theorem up_inj {x y : α} : up x = up y ↔ x = y := by simp theorem down_surjective : Surjective (@down α) := Equiv.plift.surjective theorem down_bijective : Bijective (@down α) := Equiv.plift.bijective -- This is not a good simp lemma, as its discrimination tree key is just an arrow. theorem «forall» {p : PLift α → Prop} : (∀ x, p x) ↔ ∀ x : α, p (PLift.up x) := up_surjective.forall @[simp] theorem «exists» {p : PLift α → Prop} : (∃ x, p x) ↔ ∃ x : α, p (PLift.up x) := up_surjective.exists @[simp] lemma map_injective : Injective (PLift.map f) ↔ Injective f := (Injective.of_comp_iff' _ down_bijective).trans <| up_injective.of_comp_iff _ @[simp] lemma map_surjective : Surjective (PLift.map f) ↔ Surjective f := (down_surjective.of_comp_iff _).trans <| Surjective.of_comp_iff' up_bijective _ @[simp] lemma map_bijective : Bijective (PLift.map f) ↔ Bijective f := (down_bijective.of_comp_iff _).trans <| Bijective.of_comp_iff' up_bijective _ end PLift namespace ULift variable {α : Type u} {β : Type v} {f : α → β} instance [Subsingleton α] : Subsingleton (ULift α) := Equiv.ulift.subsingleton instance [Nonempty α] : Nonempty (ULift α) := Equiv.ulift.nonempty instance [Unique α] : Unique (ULift α) := Equiv.ulift.unique instance [DecidableEq α] : DecidableEq (ULift α) := Equiv.ulift.decidableEq instance [IsEmpty α] : IsEmpty (ULift α) := Equiv.ulift.isEmpty theorem up_injective : Injective (@up α) := Equiv.ulift.symm.injective theorem up_surjective : Surjective (@up α) := Equiv.ulift.symm.surjective theorem up_bijective : Bijective (@up α) := Equiv.ulift.symm.bijective theorem up_inj {x y : α} : up x = up y ↔ x = y := by simp theorem down_surjective : Surjective (@down α) := Equiv.ulift.surjective theorem down_bijective : Bijective (@down α) := Equiv.ulift.bijective @[simp] theorem «forall» {p : ULift α → Prop} : (∀ x, p x) ↔ ∀ x : α, p (ULift.up x) := up_surjective.forall @[simp] theorem «exists» {p : ULift α → Prop} : (∃ x, p x) ↔ ∃ x : α, p (ULift.up x) := up_surjective.exists @[simp] lemma map_injective : Injective (ULift.map f : ULift.{u'} α → ULift.{v'} β) ↔ Injective f := (Injective.of_comp_iff' _ down_bijective).trans <| up_injective.of_comp_iff _ @[simp] lemma map_surjective : Surjective (ULift.map f : ULift.{u'} α → ULift.{v'} β) ↔ Surjective f := (down_surjective.of_comp_iff _).trans <| Surjective.of_comp_iff' up_bijective _ @[simp] lemma map_bijective : Bijective (ULift.map f : ULift.{u'} α → ULift.{v'} β) ↔ Bijective f := (down_bijective.of_comp_iff _).trans <| Bijective.of_comp_iff' up_bijective _ @[ext] theorem ext (x y : ULift α) (h : x.down = y.down) : x = y := congrArg up h @[simp] lemma rec_update {β : ULift α → Type*} [DecidableEq α] (f : ∀ a, β (.up a)) (a : α) (x : β (.up a)) : ULift.rec (update f a x) = update (ULift.rec f) (.up a) x := Function.rec_update up_injective (ULift.rec ·) (fun _ _ => rfl) (fun | _, _, .up _, h => (h _ rfl).elim) _ _ _ end ULift
.lake/packages/mathlib/Mathlib/Data/Finmap.lean
import Mathlib.Data.List.AList import Mathlib.Data.Finset.Sigma import Mathlib.Data.Part /-! # Finite maps over `Multiset` -/ universe u v w open List variable {α : Type u} {β : α → Type v} /-! ### Multisets of sigma types -/ namespace Multiset /-- Multiset of keys of an association multiset. -/ def keys (s : Multiset (Sigma β)) : Multiset α := s.map Sigma.fst @[simp] theorem coe_keys {l : List (Sigma β)} : keys (l : Multiset (Sigma β)) = (l.keys : Multiset α) := rfl @[simp] theorem keys_zero : keys (0 : Multiset (Sigma β)) = 0 := rfl @[simp] theorem keys_cons {a : α} {b : β a} {s : Multiset (Sigma β)} : keys (⟨a, b⟩ ::ₘ s) = a ::ₘ keys s := by simp [keys] @[simp] theorem keys_singleton {a : α} {b : β a} : keys ({⟨a, b⟩} : Multiset (Sigma β)) = {a} := rfl /-- `NodupKeys s` means that `s` has no duplicate keys. -/ def NodupKeys (s : Multiset (Sigma β)) : Prop := Quot.liftOn s List.NodupKeys fun _ _ p => propext <| perm_nodupKeys p @[simp] theorem coe_nodupKeys {l : List (Sigma β)} : @NodupKeys α β l ↔ l.NodupKeys := Iff.rfl lemma nodup_keys {m : Multiset (Σ a, β a)} : m.keys.Nodup ↔ m.NodupKeys := by rcases m with ⟨l⟩; rfl alias ⟨_, NodupKeys.nodup_keys⟩ := nodup_keys protected lemma NodupKeys.nodup {m : Multiset (Σ a, β a)} (h : m.NodupKeys) : m.Nodup := h.nodup_keys.of_map _ end Multiset /-! ### Finmap -/ /-- `Finmap β` is the type of finite maps over a multiset. It is effectively a quotient of `AList β` by permutation of the underlying list. -/ structure Finmap (β : α → Type v) : Type max u v where /-- The underlying `Multiset` of a `Finmap` -/ entries : Multiset (Sigma β) /-- There are no duplicate keys in `entries` -/ nodupKeys : entries.NodupKeys /-- The quotient map from `AList` to `Finmap`. -/ def AList.toFinmap (s : AList β) : Finmap β := ⟨s.entries, s.nodupKeys⟩ -- Setting `priority := high` means that Lean will prefer this notation to the identical one -- for `Quotient.mk` local notation:arg "⟦" a "⟧" => AList.toFinmap a theorem AList.toFinmap_eq {s₁ s₂ : AList β} : toFinmap s₁ = toFinmap s₂ ↔ s₁.entries ~ s₂.entries := by cases s₁ cases s₂ simp [AList.toFinmap] @[simp] theorem AList.toFinmap_entries (s : AList β) : ⟦s⟧.entries = s.entries := rfl /-- Given `l : List (Sigma β)`, create a term of type `Finmap β` by removing entries with duplicate keys. -/ def List.toFinmap [DecidableEq α] (s : List (Sigma β)) : Finmap β := s.toAList.toFinmap namespace Finmap open AList lemma nodup_entries (f : Finmap β) : f.entries.Nodup := f.nodupKeys.nodup /-! ### Lifting from AList -/ /-- Lift a permutation-respecting function on `AList` to `Finmap`. -/ def liftOn {γ} (s : Finmap β) (f : AList β → γ) (H : ∀ a b : AList β, a.entries ~ b.entries → f a = f b) : γ := by refine (Quotient.liftOn s.entries (fun (l : List (Sigma β)) => (⟨_, fun nd => f ⟨l, nd⟩⟩ : Part γ)) (fun l₁ l₂ p => Part.ext' (perm_nodupKeys p) ?_) : Part γ).get ?_ · exact fun h1 h2 => H _ _ p · have := s.nodupKeys revert this rcases s.entries with ⟨l⟩ exact id @[simp] theorem liftOn_toFinmap {γ} (s : AList β) (f : AList β → γ) (H) : liftOn ⟦s⟧ f H = f s := by cases s rfl /-- Lift a permutation-respecting function on 2 `AList`s to 2 `Finmap`s. -/ def liftOn₂ {γ} (s₁ s₂ : Finmap β) (f : AList β → AList β → γ) (H : ∀ a₁ b₁ a₂ b₂ : AList β, a₁.entries ~ a₂.entries → b₁.entries ~ b₂.entries → f a₁ b₁ = f a₂ b₂) : γ := liftOn s₁ (fun l₁ => liftOn s₂ (f l₁) fun _ _ p => H _ _ _ _ (Perm.refl _) p) fun a₁ a₂ p => by have H' : f a₁ = f a₂ := funext fun _ => H _ _ _ _ p (Perm.refl _) simp only [H'] @[simp] theorem liftOn₂_toFinmap {γ} (s₁ s₂ : AList β) (f : AList β → AList β → γ) (H) : liftOn₂ ⟦s₁⟧ ⟦s₂⟧ f H = f s₁ s₂ := rfl /-! ### Induction -/ @[elab_as_elim] theorem induction_on {C : Finmap β → Prop} (s : Finmap β) (H : ∀ a : AList β, C ⟦a⟧) : C s := by rcases s with ⟨⟨a⟩, h⟩; exact H ⟨a, h⟩ @[elab_as_elim] theorem induction_on₂ {C : Finmap β → Finmap β → Prop} (s₁ s₂ : Finmap β) (H : ∀ a₁ a₂ : AList β, C ⟦a₁⟧ ⟦a₂⟧) : C s₁ s₂ := induction_on s₁ fun l₁ => induction_on s₂ fun l₂ => H l₁ l₂ @[elab_as_elim] theorem induction_on₃ {C : Finmap β → Finmap β → Finmap β → Prop} (s₁ s₂ s₃ : Finmap β) (H : ∀ a₁ a₂ a₃ : AList β, C ⟦a₁⟧ ⟦a₂⟧ ⟦a₃⟧) : C s₁ s₂ s₃ := induction_on₂ s₁ s₂ fun l₁ l₂ => induction_on s₃ fun l₃ => H l₁ l₂ l₃ /-! ### extensionality -/ @[ext] theorem ext : ∀ {s t : Finmap β}, s.entries = t.entries → s = t | ⟨l₁, h₁⟩, ⟨l₂, _⟩, H => by congr @[simp] theorem ext_iff' {s t : Finmap β} : s.entries = t.entries ↔ s = t := Finmap.ext_iff.symm /-! ### mem -/ /-- The predicate `a ∈ s` means that `s` has a value associated to the key `a`. -/ instance : Membership α (Finmap β) := ⟨fun s a => a ∈ s.entries.keys⟩ theorem mem_def {a : α} {s : Finmap β} : a ∈ s ↔ a ∈ s.entries.keys := Iff.rfl @[simp] theorem mem_toFinmap {a : α} {s : AList β} : a ∈ toFinmap s ↔ a ∈ s := Iff.rfl /-! ### keys -/ /-- The set of keys of a finite map. -/ def keys (s : Finmap β) : Finset α := ⟨s.entries.keys, s.nodupKeys.nodup_keys⟩ @[simp] theorem keys_val (s : AList β) : (keys ⟦s⟧).val = s.keys := rfl @[simp] theorem keys_ext {s₁ s₂ : AList β} : keys ⟦s₁⟧ = keys ⟦s₂⟧ ↔ s₁.keys ~ s₂.keys := by simp [keys, AList.keys] theorem mem_keys {a : α} {s : Finmap β} : a ∈ s.keys ↔ a ∈ s := induction_on s fun _ => AList.mem_keys /-! ### empty -/ /-- The empty map. -/ instance : EmptyCollection (Finmap β) := ⟨⟨0, nodupKeys_nil⟩⟩ instance : Inhabited (Finmap β) := ⟨∅⟩ @[simp] theorem empty_toFinmap : (⟦∅⟧ : Finmap β) = ∅ := rfl @[simp] theorem toFinmap_nil [DecidableEq α] : ([].toFinmap : Finmap β) = ∅ := rfl theorem notMem_empty {a : α} : a ∉ (∅ : Finmap β) := Multiset.notMem_zero a @[deprecated (since := "2025-05-23")] alias not_mem_empty := notMem_empty @[simp] theorem keys_empty : (∅ : Finmap β).keys = ∅ := rfl /-! ### singleton -/ /-- The singleton map. -/ def singleton (a : α) (b : β a) : Finmap β := ⟦AList.singleton a b⟧ @[simp] theorem keys_singleton (a : α) (b : β a) : (singleton a b).keys = {a} := rfl @[simp] theorem mem_singleton (x y : α) (b : β y) : x ∈ singleton y b ↔ x = y := by simp [singleton, mem_def] section variable [DecidableEq α] instance decidableEq [∀ a, DecidableEq (β a)] : DecidableEq (Finmap β) | _, _ => decidable_of_iff _ Finmap.ext_iff.symm /-! ### lookup -/ /-- Look up the value associated to a key in a map. -/ def lookup (a : α) (s : Finmap β) : Option (β a) := liftOn s (AList.lookup a) fun _ _ => perm_lookup @[simp] theorem lookup_toFinmap (a : α) (s : AList β) : lookup a ⟦s⟧ = s.lookup a := rfl @[simp] theorem dlookup_list_toFinmap (a : α) (s : List (Sigma β)) : lookup a s.toFinmap = s.dlookup a := by rw [List.toFinmap, lookup_toFinmap, lookup_to_alist] @[simp] theorem lookup_empty (a) : lookup a (∅ : Finmap β) = none := rfl theorem lookup_isSome {a : α} {s : Finmap β} : (s.lookup a).isSome ↔ a ∈ s := induction_on s fun _ => AList.lookup_isSome theorem lookup_eq_none {a} {s : Finmap β} : lookup a s = none ↔ a ∉ s := induction_on s fun _ => AList.lookup_eq_none lemma mem_lookup_iff {s : Finmap β} {a : α} {b : β a} : b ∈ s.lookup a ↔ Sigma.mk a b ∈ s.entries := by rcases s with ⟨⟨l⟩, hl⟩; exact List.mem_dlookup_iff hl lemma lookup_eq_some_iff {s : Finmap β} {a : α} {b : β a} : s.lookup a = b ↔ Sigma.mk a b ∈ s.entries := mem_lookup_iff @[simp] lemma sigma_keys_lookup (s : Finmap β) : s.keys.sigma (fun i => (s.lookup i).toFinset) = ⟨s.entries, s.nodup_entries⟩ := by ext x have : x ∈ s.entries → x.1 ∈ s.keys := Multiset.mem_map_of_mem _ simpa [lookup_eq_some_iff] @[simp] theorem lookup_singleton_eq {a : α} {b : β a} : (singleton a b).lookup a = some b := by rw [singleton, lookup_toFinmap, AList.singleton, AList.lookup, dlookup_cons_eq] instance (a : α) (s : Finmap β) : Decidable (a ∈ s) := decidable_of_iff _ lookup_isSome theorem mem_iff {a : α} {s : Finmap β} : a ∈ s ↔ ∃ b, s.lookup a = some b := induction_on s fun s => Iff.trans List.mem_keys <| exists_congr fun _ => (mem_dlookup_iff s.nodupKeys).symm theorem mem_of_lookup_eq_some {a : α} {b : β a} {s : Finmap β} (h : s.lookup a = some b) : a ∈ s := mem_iff.mpr ⟨_, h⟩ theorem ext_lookup {s₁ s₂ : Finmap β} : (∀ x, s₁.lookup x = s₂.lookup x) → s₁ = s₂ := induction_on₂ s₁ s₂ fun s₁ s₂ h => by simp only [AList.lookup, lookup_toFinmap] at h rw [AList.toFinmap_eq] apply lookup_ext s₁.nodupKeys s₂.nodupKeys intro x y rw [h] /-- An equivalence between `Finmap β` and pairs `(keys : Finset α, lookup : ∀ a, Option (β a))` such that `(lookup a).isSome ↔ a ∈ keys`. -/ @[simps apply_coe_fst apply_coe_snd] def keysLookupEquiv : Finmap β ≃ { f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1 } where toFun s := ⟨(s.keys, fun i => s.lookup i), fun _ => lookup_isSome⟩ invFun f := mk (f.1.1.sigma fun i => (f.1.2 i).toFinset).val <| by refine Multiset.nodup_keys.1 ((Finset.nodup _).map_on ?_) simp only [Finset.mem_val, Finset.mem_sigma, Option.mem_toFinset, Option.mem_def] rintro ⟨i, x⟩ ⟨_, hx⟩ ⟨j, y⟩ ⟨_, hy⟩ (rfl : i = j) simpa using hx.symm.trans hy left_inv f := ext <| by simp right_inv := fun ⟨(s, f), hf⟩ => by dsimp only at hf ext · simp [keys, Multiset.keys, ← hf, Option.isSome_iff_exists] · simp +contextual [lookup_eq_some_iff, ← hf] @[simp] lemma keysLookupEquiv_symm_apply_keys : ∀ f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}, (keysLookupEquiv.symm f).keys = f.1.1 := keysLookupEquiv.surjective.forall.2 fun _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_fst] @[simp] lemma keysLookupEquiv_symm_apply_lookup : ∀ (f : {f : Finset α × (∀ a, Option (β a)) // ∀ i, (f.2 i).isSome ↔ i ∈ f.1}) a, (keysLookupEquiv.symm f).lookup a = f.1.2 a := keysLookupEquiv.surjective.forall.2 fun _ _ => by simp only [Equiv.symm_apply_apply, keysLookupEquiv_apply_coe_snd] /-! ### replace -/ /-- Replace a key with a given value in a finite map. If the key is not present it does nothing. -/ def replace (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.replace a b t)) fun _ _ p => toFinmap_eq.2 <| perm_replace p @[simp] theorem replace_toFinmap (a : α) (b : β a) (s : AList β) : replace a b ⟦s⟧ = (⟦s.replace a b⟧ : Finmap β) := by simp [replace] @[simp] theorem keys_replace (a : α) (b : β a) (s : Finmap β) : (replace a b s).keys = s.keys := induction_on s fun s => by simp @[simp] theorem mem_replace {a a' : α} {b : β a} {s : Finmap β} : a' ∈ replace a b s ↔ a' ∈ s := induction_on s fun s => by simp end /-! ### foldl -/ /-- Fold a commutative function over the key-value pairs in the map -/ def foldl {δ : Type w} (f : δ → ∀ a, β a → δ) (H : ∀ d a₁ b₁ a₂ b₂, f (f d a₁ b₁) a₂ b₂ = f (f d a₂ b₂) a₁ b₁) (d : δ) (m : Finmap β) : δ := letI : RightCommutative fun d (s : Sigma β) ↦ f d s.1 s.2 := ⟨fun _ _ _ ↦ H _ _ _ _ _⟩ m.entries.foldl (fun d s => f d s.1 s.2) d /-- `any f s` returns `true` iff there exists a value `v` in `s` such that `f v = true`. -/ def any (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x || f y z) (fun _ _ _ _ => by simp_rw [Bool.or_assoc, Bool.or_comm, imp_true_iff]) false /-- `all f s` returns `true` iff `f v = true` for all values `v` in `s`. -/ def all (f : ∀ x, β x → Bool) (s : Finmap β) : Bool := s.foldl (fun x y z => x && f y z) (fun _ _ _ _ => by simp_rw [Bool.and_assoc, Bool.and_comm, imp_true_iff]) true /-! ### erase -/ section variable [DecidableEq α] /-- Erase a key from the map. If the key is not present it does nothing. -/ def erase (a : α) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.erase a t)) fun _ _ p => toFinmap_eq.2 <| perm_erase p @[simp] theorem erase_toFinmap (a : α) (s : AList β) : erase a ⟦s⟧ = AList.toFinmap (s.erase a) := by simp [erase] @[simp] theorem keys_erase_toFinset (a : α) (s : AList β) : keys ⟦s.erase a⟧ = (keys ⟦s⟧).erase a := by simp [Finset.erase, keys, AList.erase, keys_kerase] @[simp] theorem keys_erase (a : α) (s : Finmap β) : (erase a s).keys = s.keys.erase a := induction_on s fun s => by simp @[simp] theorem mem_erase {a a' : α} {s : Finmap β} : a' ∈ erase a s ↔ a' ≠ a ∧ a' ∈ s := induction_on s fun s => by simp theorem notMem_erase_self {a : α} {s : Finmap β} : a ∉ erase a s := by rw [mem_erase, not_and_or, not_not] left rfl @[deprecated (since := "2025-05-23")] alias not_mem_erase_self := notMem_erase_self @[simp] theorem lookup_erase (a) (s : Finmap β) : lookup a (erase a s) = none := induction_on s <| AList.lookup_erase a @[simp] theorem lookup_erase_ne {a a'} {s : Finmap β} (h : a ≠ a') : lookup a (erase a' s) = lookup a s := induction_on s fun _ => AList.lookup_erase_ne h theorem erase_erase {a a' : α} {s : Finmap β} : erase a (erase a' s) = erase a' (erase a s) := induction_on s fun s => ext (by simp only [AList.erase_erase, erase_toFinmap]) /-! ### sdiff -/ /-- `sdiff s s'` consists of all key-value pairs from `s` and `s'` where the keys are in `s` or `s'` but not both. -/ def sdiff (s s' : Finmap β) : Finmap β := s'.foldl (fun s x _ => s.erase x) (fun _ _ _ _ _ => erase_erase) s instance : SDiff (Finmap β) := ⟨sdiff⟩ /-! ### insert -/ /-- Insert a key-value pair into a finite map, replacing any existing pair with the same key. -/ def insert (a : α) (b : β a) (s : Finmap β) : Finmap β := (liftOn s fun t => AList.toFinmap (AList.insert a b t)) fun _ _ p => toFinmap_eq.2 <| perm_insert p @[simp] theorem insert_toFinmap (a : α) (b : β a) (s : AList β) : insert a b (AList.toFinmap s) = AList.toFinmap (s.insert a b) := by simp [insert] theorem entries_insert_of_notMem {a : α} {b : β a} {s : Finmap β} : a ∉ s → (insert a b s).entries = ⟨a, b⟩ ::ₘ s.entries := induction_on s fun s h => by simp [AList.entries_insert_of_notMem (mt mem_toFinmap.1 h), -entries_insert] @[deprecated (since := "2025-05-23")] alias entries_insert_of_not_mem := entries_insert_of_notMem @[simp] theorem mem_insert {a a' : α} {b' : β a'} {s : Finmap β} : a ∈ insert a' b' s ↔ a = a' ∨ a ∈ s := induction_on s AList.mem_insert @[simp] theorem lookup_insert {a} {b : β a} (s : Finmap β) : lookup a (insert a b s) = some b := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, AList.lookup_insert] @[simp] theorem lookup_insert_of_ne {a a'} {b : β a} (s : Finmap β) (h : a' ≠ a) : lookup a' (insert a b s) = lookup a' s := induction_on s fun s => by simp only [insert_toFinmap, lookup_toFinmap, lookup_insert_ne h] @[simp] theorem insert_insert {a} {b b' : β a} (s : Finmap β) : (s.insert a b).insert a b' = s.insert a b' := induction_on s fun s => by simp only [insert_toFinmap, AList.insert_insert] theorem insert_insert_of_ne {a a'} {b : β a} {b' : β a'} (s : Finmap β) (h : a ≠ a') : (s.insert a b).insert a' b' = (s.insert a' b').insert a b := induction_on s fun s => by simp only [insert_toFinmap, AList.toFinmap_eq, AList.insert_insert_of_ne _ h] theorem toFinmap_cons (a : α) (b : β a) (xs : List (Sigma β)) : List.toFinmap (⟨a, b⟩ :: xs) = insert a b xs.toFinmap := rfl theorem mem_list_toFinmap (a : α) (xs : List (Sigma β)) : a ∈ xs.toFinmap ↔ ∃ b : β a, Sigma.mk a b ∈ xs := by induction xs with | nil => simp only [toFinmap_nil, notMem_empty, not_mem_nil, exists_false] | cons x xs => obtain ⟨fst_i, snd_i⟩ := x simp only [toFinmap_cons, *, exists_or, mem_cons, mem_insert, exists_and_left, Sigma.mk.inj_iff] refine (or_congr_left <| and_iff_left_of_imp ?_).symm rintro rfl simp only [exists_eq, heq_iff_eq] @[simp] theorem insert_singleton_eq {a : α} {b b' : β a} : insert a b (singleton a b') = singleton a b := by simp only [singleton, Finmap.insert_toFinmap, AList.insert_singleton_eq] /-! ### extract -/ /-- Erase a key from the map, and return the corresponding value, if found. -/ def extract (a : α) (s : Finmap β) : Option (β a) × Finmap β := (liftOn s fun t => Prod.map id AList.toFinmap (AList.extract a t)) fun s₁ s₂ p => by simp [perm_lookup p, toFinmap_eq, perm_erase p] @[simp] theorem extract_eq_lookup_erase (a : α) (s : Finmap β) : extract a s = (lookup a s, erase a s) := induction_on s fun s => by simp [extract] /-! ### union -/ /-- `s₁ ∪ s₂` is the key-based union of two finite maps. It is left-biased: if there exists an `a ∈ s₁`, `lookup a (s₁ ∪ s₂) = lookup a s₁`. -/ def union (s₁ s₂ : Finmap β) : Finmap β := (liftOn₂ s₁ s₂ fun s₁ s₂ => (AList.toFinmap (s₁ ∪ s₂))) fun _ _ _ _ p₁₃ p₂₄ => toFinmap_eq.mpr <| perm_union p₁₃ p₂₄ instance : Union (Finmap β) := ⟨union⟩ @[simp] theorem mem_union {a} {s₁ s₂ : Finmap β} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_union @[simp] theorem union_toFinmap (s₁ s₂ : AList β) : (toFinmap s₁) ∪ (toFinmap s₂) = toFinmap (s₁ ∪ s₂) := by simp [(· ∪ ·), union] theorem keys_union {s₁ s₂ : Finmap β} : (s₁ ∪ s₂).keys = s₁.keys ∪ s₂.keys := induction_on₂ s₁ s₂ fun s₁ s₂ => Finset.ext <| by simp [keys] @[simp] theorem lookup_union_left {a} {s₁ s₂ : Finmap β} : a ∈ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₁ := induction_on₂ s₁ s₂ fun _ _ => AList.lookup_union_left @[simp] theorem lookup_union_right {a} {s₁ s₂ : Finmap β} : a ∉ s₁ → lookup a (s₁ ∪ s₂) = lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.lookup_union_right theorem lookup_union_left_of_not_in {a} {s₁ s₂ : Finmap β} (h : a ∉ s₂) : lookup a (s₁ ∪ s₂) = lookup a s₁ := by by_cases h' : a ∈ s₁ · rw [lookup_union_left h'] · rw [lookup_union_right h', lookup_eq_none.mpr h, lookup_eq_none.mpr h'] /-- `simp`-normal form of `mem_lookup_union` -/ @[simp] theorem mem_lookup_union' {a} {b : β a} {s₁ s₂ : Finmap β} : lookup a (s₁ ∪ s₂) = some b ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_lookup_union theorem mem_lookup_union {a} {b : β a} {s₁ s₂ : Finmap β} : b ∈ lookup a (s₁ ∪ s₂) ↔ b ∈ lookup a s₁ ∨ a ∉ s₁ ∧ b ∈ lookup a s₂ := induction_on₂ s₁ s₂ fun _ _ => AList.mem_lookup_union theorem mem_lookup_union_middle {a} {b : β a} {s₁ s₂ s₃ : Finmap β} : b ∈ lookup a (s₁ ∪ s₃) → a ∉ s₂ → b ∈ lookup a (s₁ ∪ s₂ ∪ s₃) := induction_on₃ s₁ s₂ s₃ fun _ _ _ => AList.mem_lookup_union_middle theorem insert_union {a} {b : β a} {s₁ s₂ : Finmap β} : insert a b (s₁ ∪ s₂) = insert a b s₁ ∪ s₂ := induction_on₂ s₁ s₂ fun a₁ a₂ => by simp [AList.insert_union] theorem union_assoc {s₁ s₂ s₃ : Finmap β} : s₁ ∪ s₂ ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := induction_on₃ s₁ s₂ s₃ fun s₁ s₂ s₃ => by simp only [AList.toFinmap_eq, union_toFinmap, AList.union_assoc] @[simp] theorem empty_union {s₁ : Finmap β} : ∅ ∪ s₁ = s₁ := induction_on s₁ fun s₁ => by rw [← empty_toFinmap] simp [-empty_toFinmap, union_toFinmap] @[simp] theorem union_empty {s₁ : Finmap β} : s₁ ∪ ∅ = s₁ := induction_on s₁ fun s₁ => by rw [← empty_toFinmap] simp [-empty_toFinmap, union_toFinmap] theorem erase_union_singleton (a : α) (b : β a) (s : Finmap β) (h : s.lookup a = some b) : s.erase a ∪ singleton a b = s := ext_lookup fun x => by by_cases h' : x = a · subst a rw [lookup_union_right notMem_erase_self, lookup_singleton_eq, h] · have : x ∉ singleton a b := by rwa [mem_singleton] rw [lookup_union_left_of_not_in this, lookup_erase_ne h'] end /-! ### Disjoint -/ /-- `Disjoint s₁ s₂` holds if `s₁` and `s₂` have no keys in common. -/ def Disjoint (s₁ s₂ : Finmap β) : Prop := ∀ x ∈ s₁, x ∉ s₂ theorem disjoint_empty (x : Finmap β) : Disjoint ∅ x := nofun @[symm] theorem Disjoint.symm (x y : Finmap β) (h : Disjoint x y) : Disjoint y x := fun p hy hx => h p hx hy theorem Disjoint.symm_iff (x y : Finmap β) : Disjoint x y ↔ Disjoint y x := ⟨Disjoint.symm x y, Disjoint.symm y x⟩ section variable [DecidableEq α] instance : DecidableRel (@Disjoint α β) := fun x y => by dsimp only [Disjoint]; infer_instance theorem disjoint_union_left (x y z : Finmap β) : Disjoint (x ∪ y) z ↔ Disjoint x z ∧ Disjoint y z := by simp [Disjoint, Finmap.mem_union, or_imp, forall_and] theorem disjoint_union_right (x y z : Finmap β) : Disjoint x (y ∪ z) ↔ Disjoint x y ∧ Disjoint x z := by rw [Disjoint.symm_iff, disjoint_union_left, Disjoint.symm_iff _ x, Disjoint.symm_iff _ x] theorem union_comm_of_disjoint {s₁ s₂ : Finmap β} : Disjoint s₁ s₂ → s₁ ∪ s₂ = s₂ ∪ s₁ := induction_on₂ s₁ s₂ fun s₁ s₂ => by intro h simp only [AList.toFinmap_eq, union_toFinmap, AList.union_comm_of_disjoint h] theorem union_cancel {s₁ s₂ s₃ : Finmap β} (h : Disjoint s₁ s₃) (h' : Disjoint s₂ s₃) : s₁ ∪ s₃ = s₂ ∪ s₃ ↔ s₁ = s₂ := ⟨fun h'' => by apply ext_lookup intro x have : (s₁ ∪ s₃).lookup x = (s₂ ∪ s₃).lookup x := h'' ▸ rfl by_cases hs₁ : x ∈ s₁ · rwa [lookup_union_left hs₁, lookup_union_left_of_not_in (h _ hs₁)] at this · by_cases hs₂ : x ∈ s₂ · rwa [lookup_union_left_of_not_in (h' _ hs₂), lookup_union_left hs₂] at this · rw [lookup_eq_none.mpr hs₁, lookup_eq_none.mpr hs₂], fun h => h ▸ rfl⟩ end end Finmap
.lake/packages/mathlib/Mathlib/Data/FinEnum.lean
import Mathlib.Data.Fintype.Basic import Mathlib.Data.Fintype.EquivFin import Mathlib.Data.List.ProdSigma import Mathlib.Data.List.Pi /-! Type class for finitely enumerable types. The property is stronger than `Fintype` in that it assigns each element a rank in a finite enumeration. -/ universe u v open Finset /-- `FinEnum α` means that `α` is finite and can be enumerated in some order, i.e. `α` has an explicit bijection with `Fin n` for some n. -/ class FinEnum (α : Sort*) where /-- `FinEnum.card` is the cardinality of the `FinEnum` -/ card : ℕ /-- `FinEnum.Equiv` states that type `α` is in bijection with `Fin card`, the size of the `FinEnum` -/ equiv : α ≃ Fin card [decEq : DecidableEq α] attribute [instance 100] FinEnum.decEq namespace FinEnum variable {α : Type u} {β : α → Type v} /-- transport a `FinEnum` instance across an equivalence -/ def ofEquiv (α) {β} [FinEnum α] (h : β ≃ α) : FinEnum β where card := card α equiv := h.trans (equiv) decEq := (h.trans (equiv)).decidableEq /-- create a `FinEnum` instance from an exhaustive list without duplicates -/ def ofNodupList [DecidableEq α] (xs : List α) (h : ∀ x : α, x ∈ xs) (h' : List.Nodup xs) : FinEnum α where card := xs.length equiv := ⟨fun x => ⟨xs.idxOf x, by rw [List.idxOf_lt_length_iff]; apply h⟩, xs.get, fun x => by simp, fun i => by ext; simp [List.idxOf_getElem h']⟩ /-- create a `FinEnum` instance from an exhaustive list; duplicates are removed -/ def ofList [DecidableEq α] (xs : List α) (h : ∀ x : α, x ∈ xs) : FinEnum α := ofNodupList xs.dedup (by simp [*]) (List.nodup_dedup _) /-- create an exhaustive list of the values of a given type -/ def toList (α) [FinEnum α] : List α := (List.finRange (card α)).map (equiv).symm open Function @[simp] theorem mem_toList [FinEnum α] (x : α) : x ∈ toList α := by simp only [toList, List.mem_map, List.mem_finRange, true_and]; exists equiv x; simp @[simp] theorem nodup_toList [FinEnum α] : List.Nodup (toList α) := by simp only [toList]; apply List.Nodup.map <;> [apply Equiv.injective; apply List.nodup_finRange] /-- create a `FinEnum` instance using a surjection -/ def ofSurjective {β} (f : β → α) [DecidableEq α] [FinEnum β] (h : Surjective f) : FinEnum α := ofList ((toList β).map f) (by intro; simpa using h _) /-- create a `FinEnum` instance using an injection -/ noncomputable def ofInjective {α β} (f : α → β) [DecidableEq α] [FinEnum β] (h : Injective f) : FinEnum α := ofList ((toList β).filterMap (partialInv f)) (by intro x simp only [mem_toList, true_and, List.mem_filterMap] use f x simp only [h, Function.partialInv_left]) instance _root_.ULift.instFinEnum [FinEnum α] : FinEnum (ULift α) := ⟨card α, Equiv.ulift.trans equiv⟩ @[simp] theorem card_ulift [FinEnum (ULift α)] [FinEnum α] : card (ULift α) = card α := Fin.equiv_iff_eq.mp ⟨equiv.symm.trans Equiv.ulift |>.trans equiv⟩ section ULift variable [FinEnum α] (a : α) (a' : ULift α) (i : Fin (card α)) @[simp] lemma equiv_up : equiv (ULift.up a) = equiv a := rfl @[simp] lemma equiv_down : equiv a'.down = equiv a' := rfl @[simp] lemma up_equiv_symm : ULift.up (equiv.symm i) = (equiv (α := ULift α)).symm i := rfl @[simp] lemma down_equiv_symm : ((equiv (α := ULift α)).symm i).down = equiv.symm i := rfl end ULift instance pempty : FinEnum PEmpty := ofList [] fun x => PEmpty.elim x instance empty : FinEnum Empty := ofList [] fun x => Empty.elim x instance punit : FinEnum PUnit := ofList [PUnit.unit] fun x => by simp instance prod {β} [FinEnum α] [FinEnum β] : FinEnum (α × β) := ofList (toList α ×ˢ toList β) fun x => by cases x; simp instance sum {β} [FinEnum α] [FinEnum β] : FinEnum (α ⊕ β) := ofList ((toList α).map Sum.inl ++ (toList β).map Sum.inr) fun x => by cases x <;> simp instance fin {n} : FinEnum (Fin n) := ofList (List.finRange _) (by simp) @[simp] theorem card_fin {n} [FinEnum (Fin n)] : card (Fin n) = n := Fin.equiv_iff_eq.mp ⟨equiv.symm⟩ instance Quotient.enum [FinEnum α] (s : Setoid α) [DecidableRel ((· ≈ ·) : α → α → Prop)] : FinEnum (Quotient s) := FinEnum.ofSurjective Quotient.mk'' fun x => Quotient.inductionOn x fun x => ⟨x, rfl⟩ /-- enumerate all finite sets of a given type -/ def Finset.enum [DecidableEq α] : List α → List (Finset α) | [] => [∅] | x :: xs => do let r ← Finset.enum xs [r, insert x r] @[simp, grind =] theorem Finset.mem_enum [DecidableEq α] (s : Finset α) (xs : List α) : s ∈ Finset.enum xs ↔ ∀ x ∈ s, x ∈ xs := by induction xs generalizing s with | nil => simp [enum, eq_empty_iff_forall_notMem] | cons x xs ih => simp only [enum, List.bind_eq_flatMap, List.mem_flatMap, List.mem_cons, List.not_mem_nil, or_false, ih] refine ⟨by aesop, fun hs => ⟨s.erase x, ?_⟩⟩ simp only [or_iff_not_imp_left] at hs simp +contextual [eq_comm (a := s), or_iff_not_imp_left, hs] instance Finset.finEnum [FinEnum α] : FinEnum (Finset α) := ofList (Finset.enum (toList α)) (by simp) instance Subtype.finEnum [FinEnum α] (p : α → Prop) [DecidablePred p] : FinEnum { x // p x } := ofList ((toList α).filterMap fun x => if h : p x then some ⟨_, h⟩ else none) (by rintro ⟨x, h⟩; simpa) instance (β : α → Type v) [FinEnum α] [∀ a, FinEnum (β a)] : FinEnum (Sigma β) := ofList ((toList α).flatMap fun a => (toList (β a)).map <| Sigma.mk a) (by intro x; cases x; simp) instance PSigma.finEnum [FinEnum α] [∀ a, FinEnum (β a)] : FinEnum (Σ' a, β a) := FinEnum.ofEquiv _ (Equiv.psigmaEquivSigma _) instance PSigma.finEnumPropLeft {α : Prop} {β : α → Type v} [∀ a, FinEnum (β a)] [Decidable α] : FinEnum (Σ' a, β a) := if h : α then ofList ((toList (β h)).map <| PSigma.mk h) fun ⟨a, Ba⟩ => by simp else ofList [] fun ⟨a, _⟩ => (h a).elim instance PSigma.finEnumPropRight {β : α → Prop} [FinEnum α] [∀ a, Decidable (β a)] : FinEnum (Σ' a, β a) := FinEnum.ofEquiv { a // β a } ⟨fun ⟨x, y⟩ => ⟨x, y⟩, fun ⟨x, y⟩ => ⟨x, y⟩, fun ⟨_, _⟩ => rfl, fun ⟨_, _⟩ => rfl⟩ instance PSigma.finEnumPropProp {α : Prop} {β : α → Prop} [Decidable α] [∀ a, Decidable (β a)] : FinEnum (Σ' a, β a) := if h : ∃ a, β a then ofList [⟨h.fst, h.snd⟩] (by simp) else ofList [] fun a => (h ⟨a.fst, a.snd⟩).elim instance [DecidableEq α] (xs : List α) : FinEnum { x : α // x ∈ xs } := ofList xs.attach (by simp) instance (priority := 100) [FinEnum α] : Fintype α where elems := univ.map (equiv).symm.toEmbedding complete := by intros; simp /-- The enumeration merely adds an ordering, leaving the cardinality as is. -/ theorem card_eq_fintypeCard {α : Type u} [FinEnum α] [Fintype α] : card α = Fintype.card α := Fintype.truncEquivFin α |>.inductionOn (fun h ↦ Fin.equiv_iff_eq.mp ⟨equiv.symm.trans h⟩) /-- Any two enumerations of the same type have the same length. -/ theorem card_unique {α : Type u} (e₁ e₂ : FinEnum α) : e₁.card = e₂.card := calc _ _ = _ := @card_eq_fintypeCard _ e₁ inferInstance _ = _ := Fintype.card_congr' rfl _ = _ := @card_eq_fintypeCard _ e₂ inferInstance |>.symm /-- A type indexable by `Fin 0` is empty and vice versa. -/ theorem card_eq_zero_iff {α : Type u} [FinEnum α] : card α = 0 ↔ IsEmpty α := Eq.congr_left card_eq_fintypeCard |>.trans Fintype.card_eq_zero_iff /-- Any enumeration of an empty type has length 0. -/ theorem card_eq_zero {α : Type u} [FinEnum α] [IsEmpty α] : card α = 0 := card_eq_zero_iff.mpr ‹_› /-- A type indexable by `Fin n` with positive `n` is inhabited and vice versa. -/ theorem card_pos_iff {α : Type u} [FinEnum α] : 0 < card α ↔ Nonempty α := card_eq_fintypeCard (α := α) ▸ Fintype.card_pos_iff /-- Any non-empty enumeration has more than one element. -/ lemma card_pos {α : Type*} [FinEnum α] [Nonempty α] : 0 < card α := card_pos_iff.mpr ‹_› /-- No non-empty enumeration has 0 elements. -/ lemma card_ne_zero {α : Type*} [FinEnum α] [Nonempty α] : card α ≠ 0 := card_pos.ne' /-- Any enumeration of a type with unique inhabitant has length 1. -/ theorem card_eq_one (α : Type u) [FinEnum α] [Unique α] : card α = 1 := card_eq_fintypeCard.trans <| Fintype.card_eq_one_iff_nonempty_unique.mpr ⟨‹_›⟩ instance [IsEmpty α] : Unique (FinEnum α) where default := ⟨0, Equiv.equivOfIsEmpty α (Fin 0)⟩ uniq e := by change FinEnum.mk e.1 e.2 = _ congr 1 · exact card_eq_zero · refine heq_of_cast_eq ?_ (Subsingleton.allEq _ _) exact congrArg (α ≃ Fin ·) <| card_eq_zero · funext x exact ‹IsEmpty α›.elim x /-- An empty type has a trivial enumeration. Not registered as an instance, to make sure that there aren't two definitionally differing instances around. -/ def ofIsEmpty [IsEmpty α] : FinEnum α := default instance [Unique α] : Unique (FinEnum α) where default := ⟨1, Equiv.ofUnique α (Fin 1)⟩ uniq e := by change FinEnum.mk e.1 e.2 = _ congr 1 · exact card_eq_one α · refine heq_of_cast_eq ?_ (Subsingleton.allEq _ _) exact congrArg (α ≃ Fin ·) <| card_eq_one α · subsingleton /-- A type with unique inhabitant has a trivial enumeration. Not registered as an instance, to make sure that there aren't two definitionally differing instances around. -/ def ofUnique [Unique α] : FinEnum α := default end FinEnum namespace List variable {α : Type*} [FinEnum α] {β : α → Type*} [∀ a, FinEnum (β a)] open FinEnum theorem mem_pi_toList (xs : List α) (f : ∀ a, a ∈ xs → β a) : f ∈ pi xs fun x => toList (β x) := (mem_pi _ _).mpr fun _ _ ↦ mem_toList _ /-- enumerate all functions whose domain and range are finitely enumerable -/ def Pi.enum (β : α → Type*) [∀ a, FinEnum (β a)] : List (∀ a, β a) := (pi (toList α) fun x => toList (β x)).map (fun f x => f x (mem_toList _)) theorem Pi.mem_enum (f : ∀ a, β a) : f ∈ Pi.enum β := by simpa [Pi.enum] using ⟨fun a _ => f a, mem_pi_toList _ _, rfl⟩ instance Pi.finEnum : FinEnum (∀ a, β a) := ofList (Pi.enum _) fun _ => Pi.mem_enum _ instance pfunFinEnum (p : Prop) [Decidable p] (α : p → Type) [∀ hp, FinEnum (α hp)] : FinEnum (∀ hp : p, α hp) := if hp : p then ofList ((toList (α hp)).map fun x _ => x) (by intro x; simpa using ⟨x hp, rfl⟩) else ofList [fun hp' => (hp hp').elim] (by simp [funext_iff, hp]) end List
.lake/packages/mathlib/Mathlib/Data/Holor.lean
import Mathlib.Data.Nat.Find import Mathlib.Algebra.Module.Pi import Mathlib.Algebra.BigOperators.Group.Finset.Basic /-! # Basic properties of holors Holors are indexed collections of tensor coefficients. Confusingly, they are often called tensors in physics and in the neural network community. A holor is simply a multidimensional array of values. The size of a holor is specified by a `List ℕ`, whose length is called the dimension of the holor. The tensor product of `x₁ : Holor α ds₁` and `x₂ : Holor α ds₂` is the holor given by `(x₁ ⊗ x₂) (i₁ ++ i₂) = x₁ i₁ * x₂ i₂`. A holor is "of rank at most 1" if it is a tensor product of one-dimensional holors. The CP rank of a holor `x` is the smallest N such that `x` is the sum of N holors of rank at most 1. Based on the tensor library found in <https://www.isa-afp.org/entries/Deep_Learning.html> ## References * <https://en.wikipedia.org/wiki/Tensor_rank_decomposition> -/ universe u open List /-- `HolorIndex ds` is the type of valid index tuples used to identify an entry of a holor of dimensions `ds`. -/ def HolorIndex (ds : List ℕ) : Type := { is : List ℕ // Forall₂ (· < ·) is ds } namespace HolorIndex variable {ds₁ ds₂ ds₃ : List ℕ} /-- Take the first elements of a `HolorIndex`. -/ def take : ∀ {ds₁ : List ℕ}, HolorIndex (ds₁ ++ ds₂) → HolorIndex ds₁ | ds, is => ⟨List.take (length ds) is.1, forall₂_take_append is.1 ds ds₂ is.2⟩ /-- Drop the first elements of a `HolorIndex`. -/ def drop : ∀ {ds₁ : List ℕ}, HolorIndex (ds₁ ++ ds₂) → HolorIndex ds₂ | ds, is => ⟨List.drop (length ds) is.1, forall₂_drop_append is.1 ds ds₂ is.2⟩ theorem cast_type (is : List ℕ) (eq : ds₁ = ds₂) (h : Forall₂ (· < ·) is ds₁) : (cast (congr_arg HolorIndex eq) ⟨is, h⟩).val = is := by subst eq; rfl /-- Right associator for `HolorIndex` -/ def assocRight : HolorIndex (ds₁ ++ ds₂ ++ ds₃) → HolorIndex (ds₁ ++ (ds₂ ++ ds₃)) := cast (congr_arg HolorIndex (append_assoc ds₁ ds₂ ds₃)) /-- Left associator for `HolorIndex` -/ def assocLeft : HolorIndex (ds₁ ++ (ds₂ ++ ds₃)) → HolorIndex (ds₁ ++ ds₂ ++ ds₃) := cast (congr_arg HolorIndex (append_assoc ds₁ ds₂ ds₃).symm) theorem take_take : ∀ t : HolorIndex (ds₁ ++ ds₂ ++ ds₃), t.assocRight.take = t.take.take | ⟨is, h⟩ => Subtype.eq <| by simp [assocRight, take, cast_type, List.take_take, Nat.le_add_right] theorem drop_take : ∀ t : HolorIndex (ds₁ ++ ds₂ ++ ds₃), t.assocRight.drop.take = t.take.drop | ⟨is, h⟩ => Subtype.eq (by simp [assocRight, take, drop, cast_type, List.drop_take]) theorem drop_drop : ∀ t : HolorIndex (ds₁ ++ ds₂ ++ ds₃), t.assocRight.drop.drop = t.drop | ⟨is, h⟩ => Subtype.eq (by simp [assocRight, drop, cast_type, List.drop_drop]) end HolorIndex /-- Holor (indexed collections of tensor coefficients) -/ def Holor (α : Type u) (ds : List ℕ) := HolorIndex ds → α namespace Holor variable {α : Type} {d : ℕ} {ds : List ℕ} {ds₁ : List ℕ} {ds₂ : List ℕ} {ds₃ : List ℕ} instance [Inhabited α] : Inhabited (Holor α ds) := ⟨fun _ => default⟩ instance [Zero α] : Zero (Holor α ds) := ⟨fun _ => 0⟩ instance [Add α] : Add (Holor α ds) := ⟨fun x y t => x t + y t⟩ instance [Neg α] : Neg (Holor α ds) := ⟨fun a t => -a t⟩ instance [AddSemigroup α] : AddSemigroup (Holor α ds) := Pi.addSemigroup instance [AddCommSemigroup α] : AddCommSemigroup (Holor α ds) := Pi.addCommSemigroup instance [AddMonoid α] : AddMonoid (Holor α ds) := Pi.addMonoid instance [AddCommMonoid α] : AddCommMonoid (Holor α ds) := Pi.addCommMonoid instance [AddGroup α] : AddGroup (Holor α ds) := Pi.addGroup instance [AddCommGroup α] : AddCommGroup (Holor α ds) := Pi.addCommGroup -- scalar product instance [Mul α] : SMul α (Holor α ds) := ⟨fun a x => fun t => a * x t⟩ instance [Semiring α] : Module α (Holor α ds) := Pi.module _ _ _ /-- The tensor product of two holors. -/ def mul [Mul α] (x : Holor α ds₁) (y : Holor α ds₂) : Holor α (ds₁ ++ ds₂) := fun t => x t.take * y t.drop local infixl:70 " ⊗ " => mul theorem cast_type (eq : ds₁ = ds₂) (a : Holor α ds₁) : cast (congr_arg (Holor α) eq) a = fun t => a (cast (congr_arg HolorIndex eq.symm) t) := by subst eq; rfl /-- Right associator for `Holor` -/ def assocRight : Holor α (ds₁ ++ ds₂ ++ ds₃) → Holor α (ds₁ ++ (ds₂ ++ ds₃)) := cast (congr_arg (Holor α) (append_assoc ds₁ ds₂ ds₃)) /-- Left associator for `Holor` -/ def assocLeft : Holor α (ds₁ ++ (ds₂ ++ ds₃)) → Holor α (ds₁ ++ ds₂ ++ ds₃) := cast (congr_arg (Holor α) (append_assoc ds₁ ds₂ ds₃).symm) theorem mul_assoc0 [Semigroup α] (x : Holor α ds₁) (y : Holor α ds₂) (z : Holor α ds₃) : x ⊗ y ⊗ z = (x ⊗ (y ⊗ z)).assocLeft := funext fun t : HolorIndex (ds₁ ++ ds₂ ++ ds₃) => by rw [assocLeft] unfold mul rw [mul_assoc, ← HolorIndex.take_take, ← HolorIndex.drop_take, ← HolorIndex.drop_drop, cast_type] · rfl rw [append_assoc] theorem mul_assoc [Semigroup α] (x : Holor α ds₁) (y : Holor α ds₂) (z : Holor α ds₃) : mul (mul x y) z ≍ mul x (mul y z) := by simp [cast_heq, mul_assoc0, assocLeft] theorem mul_left_distrib [Distrib α] (x : Holor α ds₁) (y : Holor α ds₂) (z : Holor α ds₂) : x ⊗ (y + z) = x ⊗ y + x ⊗ z := funext fun t => left_distrib (x t.take) (y t.drop) (z t.drop) theorem mul_right_distrib [Distrib α] (x : Holor α ds₁) (y : Holor α ds₁) (z : Holor α ds₂) : (x + y) ⊗ z = x ⊗ z + y ⊗ z := funext fun t => add_mul (x t.take) (y t.take) (z t.drop) @[simp] nonrec theorem zero_mul {α : Type} [MulZeroClass α] (x : Holor α ds₂) : (0 : Holor α ds₁) ⊗ x = 0 := funext fun t => zero_mul (x (HolorIndex.drop t)) @[simp] nonrec theorem mul_zero {α : Type} [MulZeroClass α] (x : Holor α ds₁) : x ⊗ (0 : Holor α ds₂) = 0 := funext fun t => mul_zero (x (HolorIndex.take t)) theorem mul_scalar_mul [Mul α] (x : Holor α []) (y : Holor α ds) : x ⊗ y = x ⟨[], Forall₂.nil⟩ • y := by simp +unfoldPartialApp [mul, SMul.smul, HolorIndex.take, HolorIndex.drop, HSMul.hSMul] -- holor slices /-- A slice is a subholor consisting of all entries with initial index i. -/ def slice (x : Holor α (d :: ds)) (i : ℕ) (h : i < d) : Holor α ds := fun is : HolorIndex ds => x ⟨i :: is.1, Forall₂.cons h is.2⟩ /-- The 1-dimensional "unit" holor with 1 in the `j`th position. -/ def unitVec [Monoid α] [AddMonoid α] (d : ℕ) (j : ℕ) : Holor α [d] := fun ti => if ti.1 = [j] then 1 else 0 theorem holor_index_cons_decomp (p : HolorIndex (d :: ds) → Prop) : ∀ t : HolorIndex (d :: ds), (∀ i is, ∀ h : t.1 = i :: is, p ⟨i :: is, by rw [← h]; exact t.2⟩) → p t | ⟨[], hforall₂⟩, _ => absurd (forall₂_nil_left_iff.1 hforall₂) (cons_ne_nil d ds) | ⟨i :: is, _⟩, hp => hp i is rfl /-- Two holors are equal if all their slices are equal. -/ theorem slice_eq (x : Holor α (d :: ds)) (y : Holor α (d :: ds)) (h : slice x = slice y) : x = y := funext fun t : HolorIndex (d :: ds) => holor_index_cons_decomp (fun t => x t = y t) t fun i is hiis => have hiisdds : Forall₂ (· < ·) (i :: is) (d :: ds) := by rw [← hiis]; exact t.2 have hid : i < d := (forall₂_cons.1 hiisdds).1 have hisds : Forall₂ (· < ·) is ds := (forall₂_cons.1 hiisdds).2 calc x ⟨i :: is, _⟩ = slice x i hid ⟨is, hisds⟩ := congr_arg x (Subtype.eq rfl) _ = slice y i hid ⟨is, hisds⟩ := by rw [h] _ = y ⟨i :: is, _⟩ := congr_arg y (Subtype.eq rfl) theorem slice_unitVec_mul [Semiring α] {i : ℕ} {j : ℕ} (hid : i < d) (x : Holor α ds) : slice (unitVec d j ⊗ x) i hid = if i = j then x else 0 := funext fun t : HolorIndex ds => if h : i = j then by simp [slice, mul, HolorIndex.take, unitVec, HolorIndex.drop, h] else by simp [slice, mul, HolorIndex.take, unitVec, HolorIndex.drop, h]; rfl theorem slice_add [Add α] (i : ℕ) (hid : i < d) (x : Holor α (d :: ds)) (y : Holor α (d :: ds)) : slice x i hid + slice y i hid = slice (x + y) i hid := funext fun t => by simp [slice, (· + ·), Add.add] theorem slice_zero [Zero α] (i : ℕ) (hid : i < d) : slice (0 : Holor α (d :: ds)) i hid = 0 := rfl theorem slice_sum [AddCommMonoid α] {β : Type} (i : ℕ) (hid : i < d) (s : Finset β) (f : β → Holor α (d :: ds)) : (∑ x ∈ s, slice (f x) i hid) = slice (∑ x ∈ s, f x) i hid := by letI := Classical.decEq β refine Finset.induction_on s ?_ ?_ · simp [slice_zero] · intro _ _ h_not_in ih rw [Finset.sum_insert h_not_in, ih, slice_add, Finset.sum_insert h_not_in] /-- The original holor can be recovered from its slices by multiplying with unit vectors and summing up. -/ @[simp] theorem sum_unitVec_mul_slice [Semiring α] (x : Holor α (d :: ds)) : (∑ i ∈ (Finset.range d).attach, unitVec d i ⊗ slice x i (Nat.succ_le_of_lt (Finset.mem_range.1 i.prop))) = x := by apply slice_eq _ _ _ ext i hid rw [← slice_sum] simp only [slice_unitVec_mul hid] rw [Finset.sum_eq_single (Subtype.mk i <| Finset.mem_range.2 hid)] · simp · intro (b : { x // x ∈ Finset.range d }) (_ : b ∈ (Finset.range d).attach) (hbi : b ≠ ⟨i, _⟩) have hbi' : i ≠ b := by simpa only [Ne, Subtype.ext_iff, Subtype.coe_mk] using hbi.symm simp [hbi'] · intro (hid' : Subtype.mk i _ ∉ Finset.attach (Finset.range d)) exfalso exact absurd (Finset.mem_attach _ _) hid' -- CP rank /-- `CPRankMax1 x` means `x` has CP rank at most 1, that is, it is the tensor product of 1-dimensional holors. -/ inductive CPRankMax1 [Mul α] : ∀ {ds}, Holor α ds → Prop | nil (x : Holor α []) : CPRankMax1 x | cons {d} {ds} (x : Holor α [d]) (y : Holor α ds) : CPRankMax1 y → CPRankMax1 (x ⊗ y) /-- `CPRankMax N x` means `x` has CP rank at most `N`, that is, it can be written as the sum of N holors of rank at most 1. -/ inductive CPRankMax [Mul α] [AddMonoid α] : ℕ → ∀ {ds}, Holor α ds → Prop | zero {ds} : CPRankMax 0 (0 : Holor α ds) | succ (n) {ds} (x : Holor α ds) (y : Holor α ds) : CPRankMax1 x → CPRankMax n y → CPRankMax (n + 1) (x + y) theorem cprankMax_nil [Mul α] [AddMonoid α] (x : Holor α nil) : CPRankMax 1 x := by have h := CPRankMax.succ 0 x 0 (CPRankMax1.nil x) CPRankMax.zero rwa [add_zero x, zero_add] at h theorem cprankMax_1 [Mul α] [AddMonoid α] {x : Holor α ds} (h : CPRankMax1 x) : CPRankMax 1 x := by have h' := CPRankMax.succ 0 x 0 h CPRankMax.zero rwa [zero_add, add_zero] at h' theorem cprankMax_add [Mul α] [AddMonoid α] : ∀ {m : ℕ} {n : ℕ} {x : Holor α ds} {y : Holor α ds}, CPRankMax m x → CPRankMax n y → CPRankMax (m + n) (x + y) | 0, n, x, y, hx, hy => by match hx with | CPRankMax.zero => simp only [zero_add, hy] | m + 1, n, _, y, CPRankMax.succ _ x₁ x₂ hx₁ hx₂, hy => by suffices CPRankMax (m + n + 1) (x₁ + (x₂ + y)) by simpa only [add_comm, add_assoc, add_left_comm] using this apply CPRankMax.succ · assumption · exact cprankMax_add hx₂ hy theorem cprankMax_mul [NonUnitalNonAssocSemiring α] : ∀ (n : ℕ) (x : Holor α [d]) (y : Holor α ds), CPRankMax n y → CPRankMax n (x ⊗ y) | 0, x, _, CPRankMax.zero => by simp [mul_zero x, CPRankMax.zero] | n + 1, x, _, CPRankMax.succ _ y₁ y₂ hy₁ hy₂ => by rw [mul_left_distrib] rw [Nat.add_comm] apply cprankMax_add · exact cprankMax_1 (CPRankMax1.cons _ _ hy₁) · exact cprankMax_mul _ x y₂ hy₂ theorem cprankMax_sum [NonUnitalNonAssocSemiring α] {β} {n : ℕ} (s : Finset β) (f : β → Holor α ds) : (∀ x ∈ s, CPRankMax n (f x)) → CPRankMax (s.card * n) (∑ x ∈ s, f x) := letI := Classical.decEq β Finset.induction_on s (by simp [CPRankMax.zero]) (by intro x s (h_x_notin_s : x ∉ s) ih h_cprank simp only [Finset.sum_insert h_x_notin_s, Finset.card_insert_of_notMem h_x_notin_s] rw [Nat.right_distrib] simp only [Nat.one_mul, Nat.add_comm] have ih' : CPRankMax (Finset.card s * n) (∑ x ∈ s, f x) := by apply ih intro (x : β) (h_x_in_s : x ∈ s) simp only [h_cprank, Finset.mem_insert_of_mem, h_x_in_s] exact cprankMax_add (h_cprank x (Finset.mem_insert_self x s)) ih') theorem cprankMax_upper_bound [Semiring α] : ∀ {ds}, ∀ x : Holor α ds, CPRankMax ds.prod x | [], x => cprankMax_nil x | d :: ds, x => by have h_summands : ∀ i : { x // x ∈ Finset.range d }, CPRankMax ds.prod (unitVec d i.1 ⊗ slice x i.1 (mem_range.1 i.2)) := fun i => cprankMax_mul _ _ _ (cprankMax_upper_bound (slice x i.1 (mem_range.1 i.2))) have h_dds_prod : (List.cons d ds).prod = Finset.card (Finset.range d) * prod ds := by simp [Finset.card_range] have : CPRankMax (Finset.card (Finset.attach (Finset.range d)) * prod ds) (∑ i ∈ Finset.attach (Finset.range d), unitVec d i.val ⊗ slice x i.val (mem_range.1 i.2)) := cprankMax_sum (Finset.range d).attach _ fun i _ => h_summands i have h_cprankMax_sum : CPRankMax (Finset.card (Finset.range d) * prod ds) (∑ i ∈ Finset.attach (Finset.range d), unitVec d i.val ⊗ slice x i.val (mem_range.1 i.2)) := by rwa [Finset.card_attach] at this rw [← sum_unitVec_mul_slice x] rw [h_dds_prod] exact h_cprankMax_sum /-- The CP rank of a holor `x`: the smallest N such that `x` can be written as the sum of N holors of rank at most 1. -/ noncomputable def cprank [Ring α] (x : Holor α ds) : Nat := @Nat.find (fun n => CPRankMax n x) (Classical.decPred _) ⟨ds.prod, cprankMax_upper_bound x⟩ theorem cprank_upper_bound [Ring α] : ∀ {ds}, ∀ x : Holor α ds, cprank x ≤ ds.prod := fun {ds} x => letI := Classical.decPred fun n : ℕ => CPRankMax n x Nat.find_min' ⟨ds.prod, show (fun n => CPRankMax n x) ds.prod from cprankMax_upper_bound x⟩ (cprankMax_upper_bound x) end Holor
.lake/packages/mathlib/Mathlib/Data/SProd.lean
import Mathlib.Tactic.FBinop /-! # Set Product Notation This file provides notation for a product of sets, and other similar types. ## Main Definitions * `SProd α β γ` for a binary operation `(· ×ˢ ·) : α → β → γ`. ## Notation We introduce the notation `x ×ˢ y` for the `sprod` of any `SProd` structure. Ideally, `x × y` notation is desirable but this notation is defined in core for `Prod` so replacing `x ×ˢ y` with `x × y` seems difficult. -/ universe u v w /-- Notation type class for the set product `×ˢ`. -/ class SProd (α : Type u) (β : Type v) (γ : outParam (Type w)) where /-- The Cartesian product `s ×ˢ t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ sprod : α → β → γ -- This notation binds more strongly than (pre)images, unions and intersections. @[inherit_doc SProd.sprod] infixr:82 " ×ˢ " => SProd.sprod macro_rules | `($x ×ˢ $y) => `(fbinop% SProd.sprod $x $y)