source
stringlengths
17
118
lean4
stringlengths
0
335k
mathematics_in_lean/html/_sources/index.rst.txt
Mathematics in Lean =================== .. toctree:: :numbered: :maxdepth: 2 C01_Introduction C02_Basics C03_Logic C04_Sets_and_Functions C05_Elementary_Number_Theory C06_Discrete_Mathematics C07_Structures C08_Hierarchies C09_Groups_and_Rings C10_Linear_Algebra C11_Topology C12_Differential_Calculus C13_Integration_and_Measure_Theory .. toctree:: :hidden: genindex
mathematics_in_lean/html/_sources/C09_Groups_and_Rings.rst.txt
.. _groups_and_ring: Groups and Rings ================ We saw in :numref:`proving_identities_in_algebraic_structures` how to reason about operations in groups and rings. Later, in :numref:`section_algebraic_structures`, we saw how to define abstract algebraic structures, such as group structures, as well as concrete instances such as the ring structure on the Gaussian integers. :numref:`Chapter %s <hierarchies>` explained how hierarchies of abstract structures are handled in Mathlib. In this chapter we work with groups and rings in more detail. We won't be able to cover every aspect of the treatment of these topics in Mathlib, especially since Mathlib is constantly growing. But we will provide entry points to the library and show how the essential concepts are used. There is some overlap with the discussion of :numref:`Chapter %s <hierarchies>`, but here we will focus on how to use Mathlib instead of the design decisions behind the way the topics are treated. So making sense of some of the examples may require reviewing the background from :numref:`Chapter %s <hierarchies>`. .. include:: C09_Groups_and_Rings/S01_Groups.inc .. include:: C09_Groups_and_Rings/S02_Rings.inc
mathematics_in_lean/html/_sources/C01_Introduction.rst.txt
.. _introduction: Introduction ============ .. include:: C01_Introduction/S01_Getting_Started.inc .. include:: C01_Introduction/S02_Overview.inc
mathematics_in_lean/MIL/Common.lean
import Mathlib.Tactic import Mathlib.Util.Delaborators set_option warningAsError false
mathematics_in_lean/MIL/C04_Sets_and_Functions/S02_Functions.lean
import MIL.Common import Mathlib.Data.Set.Lattice import Mathlib.Data.Set.Function import Mathlib.Analysis.SpecialFunctions.Log.Basic section variable {α β : Type*} variable (f : α → β) variable (s t : Set α) variable (u v : Set β) open Function open Set example : f ⁻¹' (u ∩ v) = f ⁻¹' u ∩ f ⁻¹' v := by ext rfl example : f '' (s ∪ t) = f '' s ∪ f '' t := by ext y; constructor · rintro ⟨x, xs | xt, rfl⟩ · left use x, xs right use x, xt rintro (⟨x, xs, rfl⟩ | ⟨x, xt, rfl⟩) · use x, Or.inl xs use x, Or.inr xt example : s ⊆ f ⁻¹' (f '' s) := by intro x xs show f x ∈ f '' s use x, xs example : f '' s ⊆ v ↔ s ⊆ f ⁻¹' v := by sorry example (h : Injective f) : f ⁻¹' (f '' s) ⊆ s := by sorry example : f '' (f ⁻¹' u) ⊆ u := by sorry example (h : Surjective f) : u ⊆ f '' (f ⁻¹' u) := by sorry example (h : s ⊆ t) : f '' s ⊆ f '' t := by sorry example (h : u ⊆ v) : f ⁻¹' u ⊆ f ⁻¹' v := by sorry example : f ⁻¹' (u ∪ v) = f ⁻¹' u ∪ f ⁻¹' v := by sorry example : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := by sorry example (h : Injective f) : f '' s ∩ f '' t ⊆ f '' (s ∩ t) := by sorry example : f '' s \ f '' t ⊆ f '' (s \ t) := by sorry example : f ⁻¹' u \ f ⁻¹' v ⊆ f ⁻¹' (u \ v) := by sorry example : f '' s ∩ v = f '' (s ∩ f ⁻¹' v) := by sorry example : f '' (s ∩ f ⁻¹' u) ⊆ f '' s ∩ u := by sorry example : s ∩ f ⁻¹' u ⊆ f ⁻¹' (f '' s ∩ u) := by sorry example : s ∪ f ⁻¹' u ⊆ f ⁻¹' (f '' s ∪ u) := by sorry variable {I : Type*} (A : I → Set α) (B : I → Set β) example : (f '' ⋃ i, A i) = ⋃ i, f '' A i := by sorry example : (f '' ⋂ i, A i) ⊆ ⋂ i, f '' A i := by sorry example (i : I) (injf : Injective f) : (⋂ i, f '' A i) ⊆ f '' ⋂ i, A i := by sorry example : (f ⁻¹' ⋃ i, B i) = ⋃ i, f ⁻¹' B i := by sorry example : (f ⁻¹' ⋂ i, B i) = ⋂ i, f ⁻¹' B i := by sorry example : InjOn f s ↔ ∀ x₁ ∈ s, ∀ x₂ ∈ s, f x₁ = f x₂ → x₁ = x₂ := Iff.refl _ end section open Set Real example : InjOn log { x | x > 0 } := by intro x xpos y ypos intro e -- log x = log y calc x = exp (log x) := by rw [exp_log xpos] _ = exp (log y) := by rw [e] _ = y := by rw [exp_log ypos] example : range exp = { y | y > 0 } := by ext y; constructor · rintro ⟨x, rfl⟩ apply exp_pos intro ypos use log y rw [exp_log ypos] example : InjOn sqrt { x | x ≥ 0 } := by sorry example : InjOn (fun x ↦ x ^ 2) { x : ℝ | x ≥ 0 } := by sorry example : sqrt '' { x | x ≥ 0 } = { y | y ≥ 0 } := by sorry example : (range fun x ↦ x ^ 2) = { y : ℝ | y ≥ 0 } := by sorry end section variable {α β : Type*} [Inhabited α] #check (default : α) variable (P : α → Prop) (h : ∃ x, P x) #check Classical.choose h example : P (Classical.choose h) := Classical.choose_spec h noncomputable section open Classical def inverse (f : α → β) : β → α := fun y : β ↦ if h : ∃ x, f x = y then Classical.choose h else default theorem inverse_spec {f : α → β} (y : β) (h : ∃ x, f x = y) : f (inverse f y) = y := by rw [inverse, dif_pos h] exact Classical.choose_spec h variable (f : α → β) open Function example : Injective f ↔ LeftInverse (inverse f) f := sorry example : Surjective f ↔ RightInverse (inverse f) f := sorry end section variable {α : Type*} open Function theorem Cantor : ∀ f : α → Set α, ¬Surjective f := by intro f surjf let S := { i | i ∉ f i } rcases surjf S with ⟨j, h⟩ have h₁ : j ∉ f j := by intro h' have : j ∉ f j := by rwa [h] at h' contradiction have h₂ : j ∈ S sorry have h₃ : j ∉ S sorry contradiction -- COMMENTS: TODO: improve this end
mathematics_in_lean/MIL/C04_Sets_and_Functions/S03_The_Schroeder_Bernstein_Theorem.lean
import Mathlib.Data.Set.Lattice import Mathlib.Data.Set.Function import MIL.Common open Set open Function noncomputable section open Classical variable {α β : Type*} [Nonempty β] section variable (f : α → β) (g : β → α) def sbAux : ℕ → Set α | 0 => univ \ g '' univ | n + 1 => g '' (f '' sbAux n) def sbSet := ⋃ n, sbAux f g n def sbFun (x : α) : β := if x ∈ sbSet f g then f x else invFun g x theorem sb_right_inv {x : α} (hx : x ∉ sbSet f g) : g (invFun g x) = x := by have : x ∈ g '' univ := by contrapose! hx rw [sbSet, mem_iUnion] use 0 rw [sbAux, mem_diff] sorry have : ∃ y, g y = x := by sorry sorry theorem sb_injective (hf : Injective f) : Injective (sbFun f g) := by set A := sbSet f g with A_def set h := sbFun f g with h_def intro x₁ x₂ intro (hxeq : h x₁ = h x₂) show x₁ = x₂ simp only [h_def, sbFun, ← A_def] at hxeq by_cases xA : x₁ ∈ A ∨ x₂ ∈ A · wlog x₁A : x₁ ∈ A generalizing x₁ x₂ hxeq xA · symm apply this hxeq.symm xA.symm (xA.resolve_left x₁A) have x₂A : x₂ ∈ A := by apply _root_.not_imp_self.mp intro (x₂nA : x₂ ∉ A) rw [if_pos x₁A, if_neg x₂nA] at hxeq rw [A_def, sbSet, mem_iUnion] at x₁A have x₂eq : x₂ = g (f x₁) := by sorry rcases x₁A with ⟨n, hn⟩ rw [A_def, sbSet, mem_iUnion] use n + 1 simp [sbAux] exact ⟨x₁, hn, x₂eq.symm⟩ sorry push_neg at xA sorry theorem sb_surjective (hg : Injective g) : Surjective (sbFun f g) := by set A := sbSet f g with A_def set h := sbFun f g with h_def intro y by_cases gyA : g y ∈ A · rw [A_def, sbSet, mem_iUnion] at gyA rcases gyA with ⟨n, hn⟩ rcases n with _ | n · simp [sbAux] at hn simp [sbAux] at hn rcases hn with ⟨x, xmem, hx⟩ use x have : x ∈ A := by rw [A_def, sbSet, mem_iUnion] exact ⟨n, xmem⟩ rw [h_def, sbFun, if_pos this] apply hg hx sorry end theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : Injective f) (hg : Injective g) : ∃ h : α → β, Bijective h := ⟨sbFun f g, sb_injective f g hf, sb_surjective f g hg⟩ -- Auxiliary information section variable (g : β → α) (x : α) #check (invFun g : α → β) #check (leftInverse_invFun : Injective g → LeftInverse (invFun g) g) #check (leftInverse_invFun : Injective g → ∀ y, invFun g (g y) = y) #check (invFun_eq : (∃ y, g y = x) → g (invFun g x) = x) end
mathematics_in_lean/MIL/C04_Sets_and_Functions/S01_Sets.lean
import Mathlib.Data.Set.Lattice import Mathlib.Data.Nat.Prime.Basic import MIL.Common section variable {α : Type*} variable (s t u : Set α) open Set example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := by rw [subset_def, inter_def, inter_def] rw [subset_def] at h simp only [mem_setOf] rintro x ⟨xs, xu⟩ exact ⟨h _ xs, xu⟩ example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := by simp only [subset_def, mem_inter_iff] at * rintro x ⟨xs, xu⟩ exact ⟨h _ xs, xu⟩ example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := by intro x xsu exact ⟨h xsu.1, xsu.2⟩ example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := fun x ⟨xs, xu⟩ ↦ ⟨h xs, xu⟩ example : s ∩ (t ∪ u) ⊆ s ∩ t ∪ s ∩ u := by intro x hx have xs : x ∈ s := hx.1 have xtu : x ∈ t ∪ u := hx.2 rcases xtu with xt | xu · left show x ∈ s ∩ t exact ⟨xs, xt⟩ · right show x ∈ s ∩ u exact ⟨xs, xu⟩ example : s ∩ (t ∪ u) ⊆ s ∩ t ∪ s ∩ u := by rintro x ⟨xs, xt | xu⟩ · left; exact ⟨xs, xt⟩ · right; exact ⟨xs, xu⟩ example : s ∩ t ∪ s ∩ u ⊆ s ∩ (t ∪ u) := by sorry example : (s \ t) \ u ⊆ s \ (t ∪ u) := by intro x xstu have xs : x ∈ s := xstu.1.1 have xnt : x ∉ t := xstu.1.2 have xnu : x ∉ u := xstu.2 constructor · exact xs intro xtu -- x ∈ t ∨ x ∈ u rcases xtu with xt | xu · show False; exact xnt xt · show False; exact xnu xu example : (s \ t) \ u ⊆ s \ (t ∪ u) := by rintro x ⟨⟨xs, xnt⟩, xnu⟩ use xs rintro (xt | xu) <;> contradiction example : s \ (t ∪ u) ⊆ (s \ t) \ u := by sorry example : s ∩ t = t ∩ s := by ext x simp only [mem_inter_iff] constructor · rintro ⟨xs, xt⟩; exact ⟨xt, xs⟩ · rintro ⟨xt, xs⟩; exact ⟨xs, xt⟩ example : s ∩ t = t ∩ s := Set.ext fun x ↦ ⟨fun ⟨xs, xt⟩ ↦ ⟨xt, xs⟩, fun ⟨xt, xs⟩ ↦ ⟨xs, xt⟩⟩ example : s ∩ t = t ∩ s := by ext x; simp [and_comm] example : s ∩ t = t ∩ s := by apply Subset.antisymm · rintro x ⟨xs, xt⟩; exact ⟨xt, xs⟩ · rintro x ⟨xt, xs⟩; exact ⟨xs, xt⟩ example : s ∩ t = t ∩ s := Subset.antisymm sorry sorry example : s ∩ (s ∪ t) = s := by sorry example : s ∪ s ∩ t = s := by sorry example : s \ t ∪ t = s ∪ t := by sorry example : s \ t ∪ t \ s = (s ∪ t) \ (s ∩ t) := by sorry def evens : Set ℕ := { n | Even n } def odds : Set ℕ := { n | ¬Even n } example : evens ∪ odds = univ := by rw [evens, odds] ext n simp [-Nat.not_even_iff_odd] apply Classical.em example (x : ℕ) (h : x ∈ (∅ : Set ℕ)) : False := h example (x : ℕ) : x ∈ (univ : Set ℕ) := trivial example : { n | Nat.Prime n } ∩ { n | n > 2 } ⊆ { n | ¬Even n } := by sorry #print Prime #print Nat.Prime example (n : ℕ) : Prime n ↔ Nat.Prime n := Nat.prime_iff.symm example (n : ℕ) (h : Prime n) : Nat.Prime n := by rw [Nat.prime_iff] exact h example (n : ℕ) (h : Prime n) : Nat.Prime n := by rwa [Nat.prime_iff] end section variable (s t : Set ℕ) example (h₀ : ∀ x ∈ s, ¬Even x) (h₁ : ∀ x ∈ s, Prime x) : ∀ x ∈ s, ¬Even x ∧ Prime x := by intro x xs constructor · apply h₀ x xs apply h₁ x xs example (h : ∃ x ∈ s, ¬Even x ∧ Prime x) : ∃ x ∈ s, Prime x := by rcases h with ⟨x, xs, _, prime_x⟩ use x, xs section variable (ssubt : s ⊆ t) example (h₀ : ∀ x ∈ t, ¬Even x) (h₁ : ∀ x ∈ t, Prime x) : ∀ x ∈ s, ¬Even x ∧ Prime x := by sorry example (h : ∃ x ∈ s, ¬Even x ∧ Prime x) : ∃ x ∈ t, Prime x := by sorry end end section variable {α I : Type*} variable (A B : I → Set α) variable (s : Set α) open Set example : (s ∩ ⋃ i, A i) = ⋃ i, A i ∩ s := by ext x simp only [mem_inter_iff, mem_iUnion] constructor · rintro ⟨xs, ⟨i, xAi⟩⟩ exact ⟨i, xAi, xs⟩ rintro ⟨i, xAi, xs⟩ exact ⟨xs, ⟨i, xAi⟩⟩ example : (⋂ i, A i ∩ B i) = (⋂ i, A i) ∩ ⋂ i, B i := by ext x simp only [mem_inter_iff, mem_iInter] constructor · intro h constructor · intro i exact (h i).1 intro i exact (h i).2 rintro ⟨h1, h2⟩ i constructor · exact h1 i exact h2 i example : (s ∪ ⋂ i, A i) = ⋂ i, A i ∪ s := by sorry def primes : Set ℕ := { x | Nat.Prime x } example : (⋃ p ∈ primes, { x | p ^ 2 ∣ x }) = { x | ∃ p ∈ primes, p ^ 2 ∣ x } :=by ext rw [mem_iUnion₂] simp example : (⋃ p ∈ primes, { x | p ^ 2 ∣ x }) = { x | ∃ p ∈ primes, p ^ 2 ∣ x } := by ext simp example : (⋂ p ∈ primes, { x | ¬p ∣ x }) ⊆ { x | x = 1 } := by intro x contrapose! simp apply Nat.exists_prime_and_dvd example : (⋃ p ∈ primes, { x | x ≤ p }) = univ := by sorry end section open Set variable {α : Type*} (s : Set (Set α)) example : ⋃₀ s = ⋃ t ∈ s, t := by ext x rw [mem_iUnion₂] simp example : ⋂₀ s = ⋂ t ∈ s, t := by ext x rw [mem_iInter₂] rfl end
mathematics_in_lean/MIL/C04_Sets_and_Functions/solutions/Solutions_S01_Sets.lean
import Mathlib.Data.Set.Lattice import Mathlib.Data.Nat.Prime.Basic import MIL.Common section variable {α : Type*} variable (s t u : Set α) open Set example : s ∩ t ∪ s ∩ u ⊆ s ∩ (t ∪ u) := by rintro x (⟨xs, xt⟩ | ⟨xs, xu⟩) · use xs; left; exact xt · use xs; right; exact xu example : s \ (t ∪ u) ⊆ (s \ t) \ u := by rintro x ⟨xs, xntu⟩ constructor use xs · intro xt exact xntu (Or.inl xt) intro xu apply xntu (Or.inr xu) example : s ∩ t = t ∩ s := Subset.antisymm (fun x ⟨xs, xt⟩ ↦ ⟨xt, xs⟩) fun x ⟨xt, xs⟩ ↦ ⟨xs, xt⟩ example : s ∩ (s ∪ t) = s := by ext x; constructor · rintro ⟨xs, _⟩ exact xs · intro xs use xs; left; exact xs example : s ∪ s ∩ t = s := by ext x; constructor · rintro (xs | ⟨xs, xt⟩) <;> exact xs · intro xs; left; exact xs example : s \ t ∪ t = s ∪ t := by ext x; constructor · rintro (⟨xs, nxt⟩ | xt) · left exact xs · right exact xt by_cases h : x ∈ t · intro right exact h rintro (xs | xt) · left use xs right; exact xt example : s \ t ∪ t \ s = (s ∪ t) \ (s ∩ t) := by ext x; constructor · rintro (⟨xs, xnt⟩ | ⟨xt, xns⟩) · constructor left exact xs rintro ⟨_, xt⟩ contradiction · constructor right exact xt rintro ⟨xs, _⟩ contradiction rintro ⟨xs | xt, nxst⟩ · left use xs intro xt apply nxst constructor <;> assumption · right; use xt; intro xs apply nxst constructor <;> assumption example : { n | Nat.Prime n } ∩ { n | n > 2 } ⊆ { n | ¬Even n } := by intro n simp intro nprime n_gt rcases Nat.Prime.eq_two_or_odd nprime with h | h · rw [h] linarith · rw [Nat.odd_iff, h] end section variable (s t : Set ℕ) section variable (ssubt : s ⊆ t) example (h₀ : ∀ x ∈ t, ¬Even x) (h₁ : ∀ x ∈ t, Prime x) : ∀ x ∈ s, ¬Even x ∧ Prime x := by intro x xs constructor · apply h₀ x (ssubt xs) apply h₁ x (ssubt xs) example (h : ∃ x ∈ s, ¬Even x ∧ Prime x) : ∃ x ∈ t, Prime x := by rcases h with ⟨x, xs, _, px⟩ use x, ssubt xs end end section variable {α I : Type*} variable (A B : I → Set α) variable (s : Set α) open Set example : (s ∪ ⋂ i, A i) = ⋂ i, A i ∪ s := by ext x simp only [mem_union, mem_iInter] constructor · rintro (xs | xI) · intro i right exact xs intro i left exact xI i intro h by_cases xs : x ∈ s · left exact xs right intro i cases h i · assumption contradiction def primes : Set ℕ := { x | Nat.Prime x } example : (⋃ p ∈ primes, { x | x ≤ p }) = univ := by apply eq_univ_of_forall intro x simp rcases Nat.exists_infinite_primes x with ⟨p, pge, primep⟩ use p, primep end
mathematics_in_lean/MIL/C04_Sets_and_Functions/solutions/Solutions_S03_The_Schroeder_Bernstein_Theorem.lean
import Mathlib.Data.Set.Lattice import Mathlib.Data.Set.Function import MIL.Common open Set open Function noncomputable section open Classical variable {α β : Type*} [Nonempty β] section variable (f : α → β) (g : β → α) def sbAux : ℕ → Set α | 0 => univ \ g '' univ | n + 1 => g '' (f '' sbAux n) def sbSet := ⋃ n, sbAux f g n def sbFun (x : α) : β := if x ∈ sbSet f g then f x else invFun g x theorem sb_right_inv {x : α} (hx : x ∉ sbSet f g) : g (invFun g x) = x := by have : x ∈ g '' univ := by contrapose! hx rw [sbSet, mem_iUnion] use 0 rw [sbAux, mem_diff] exact ⟨mem_univ _, hx⟩ have : ∃ y, g y = x := by simp at this assumption exact invFun_eq this theorem sb_injective (hf : Injective f) : Injective (sbFun f g) := by set A := sbSet f g with A_def set h := sbFun f g with h_def intro x₁ x₂ intro (hxeq : h x₁ = h x₂) show x₁ = x₂ simp only [h_def, sbFun, ← A_def] at hxeq by_cases xA : x₁ ∈ A ∨ x₂ ∈ A · wlog x₁A : x₁ ∈ A generalizing x₁ x₂ hxeq xA · symm apply this hxeq.symm xA.symm (xA.resolve_left x₁A) have x₂A : x₂ ∈ A := by apply _root_.not_imp_self.mp intro (x₂nA : x₂ ∉ A) rw [if_pos x₁A, if_neg x₂nA] at hxeq rw [A_def, sbSet, mem_iUnion] at x₁A have x₂eq : x₂ = g (f x₁) := by rw [hxeq, sb_right_inv f g x₂nA] rcases x₁A with ⟨n, hn⟩ rw [A_def, sbSet, mem_iUnion] use n + 1 simp [sbAux] exact ⟨x₁, hn, x₂eq.symm⟩ rw [if_pos x₁A, if_pos x₂A] at hxeq exact hf hxeq push_neg at xA rw [if_neg xA.1, if_neg xA.2] at hxeq rw [← sb_right_inv f g xA.1, hxeq, sb_right_inv f g xA.2] theorem sb_surjective (hg : Injective g) : Surjective (sbFun f g) := by set A := sbSet f g with A_def set h := sbFun f g with h_def intro y by_cases gyA : g y ∈ A · rw [A_def, sbSet, mem_iUnion] at gyA rcases gyA with ⟨n, hn⟩ rcases n with _ | n · simp [sbAux] at hn simp [sbAux] at hn rcases hn with ⟨x, xmem, hx⟩ use x have : x ∈ A := by rw [A_def, sbSet, mem_iUnion] exact ⟨n, xmem⟩ rw [h_def, sbFun, if_pos this] apply hg hx use g y rw [h_def, sbFun, if_neg gyA] apply leftInverse_invFun hg end
mathematics_in_lean/MIL/C04_Sets_and_Functions/solutions/Solutions_S02_Functions.lean
import MIL.Common import Mathlib.Data.Set.Lattice import Mathlib.Data.Set.Function import Mathlib.Analysis.SpecialFunctions.Log.Basic section variable {α β : Type*} variable (f : α → β) variable (s t : Set α) variable (u v : Set β) open Function open Set example : f '' s ⊆ v ↔ s ⊆ f ⁻¹' v := by constructor · intro h x xs have : f x ∈ f '' s := mem_image_of_mem _ xs exact h this intro h y ymem rcases ymem with ⟨x, xs, fxeq⟩ rw [← fxeq] apply h xs example (h : Injective f) : f ⁻¹' (f '' s) ⊆ s := by rintro x ⟨y, ys, fxeq⟩ rw [← h fxeq] exact ys example : f '' (f ⁻¹' u) ⊆ u := by rintro y ⟨x, xmem, rfl⟩ exact xmem example (h : Surjective f) : u ⊆ f '' (f ⁻¹' u) := by intro y yu rcases h y with ⟨x, fxeq⟩ use x constructor · show f x ∈ u rw [fxeq] exact yu exact fxeq example (h : s ⊆ t) : f '' s ⊆ f '' t := by rintro y ⟨x, xs, fxeq⟩ use x, h xs example (h : u ⊆ v) : f ⁻¹' u ⊆ f ⁻¹' v := by intro x; apply h example : f ⁻¹' (u ∪ v) = f ⁻¹' u ∪ f ⁻¹' v := by ext x; rfl example : f '' (s ∩ t) ⊆ f '' s ∩ f '' t := by rintro y ⟨x, ⟨xs, xt⟩, rfl⟩ constructor · use x, xs · use x, xt example (h : Injective f) : f '' s ∩ f '' t ⊆ f '' (s ∩ t) := by rintro y ⟨⟨x₁, x₁s, rfl⟩, ⟨x₂, x₂t, fx₂eq⟩⟩ use x₁ constructor · use x₁s rw [← h fx₂eq] exact x₂t · rfl example : f '' s \ f '' t ⊆ f '' (s \ t) := by rintro y ⟨⟨x₁, x₁s, rfl⟩, h⟩ use x₁ constructor · constructor · exact x₁s · intro h' apply h use x₁, h' · rfl example : f ⁻¹' u \ f ⁻¹' v ⊆ f ⁻¹' (u \ v) := fun x ↦ id example : f '' s ∩ v = f '' (s ∩ f ⁻¹' v) := by ext y; constructor · rintro ⟨⟨x, xs, rfl⟩, fxv⟩ use x, ⟨xs, fxv⟩ rintro ⟨x, ⟨⟨xs, fxv⟩, rfl⟩⟩ exact ⟨⟨x, xs, rfl⟩, fxv⟩ example : f '' (s ∩ f ⁻¹' u) ⊆ f '' s ∩ u := by rintro y ⟨x, ⟨xs, fxu⟩, rfl⟩ exact ⟨⟨x, xs, rfl⟩, fxu⟩ example : s ∩ f ⁻¹' u ⊆ f ⁻¹' (f '' s ∩ u) := by rintro x ⟨xs, fxu⟩ exact ⟨⟨x, xs, rfl⟩, fxu⟩ example : s ∪ f ⁻¹' u ⊆ f ⁻¹' (f '' s ∪ u) := by rintro x (xs | fxu) · left exact ⟨x, xs, rfl⟩ right; exact fxu variable {I : Type*} (A : I → Set α) (B : I → Set β) example : (f '' ⋃ i, A i) = ⋃ i, f '' A i := by ext y; simp constructor · rintro ⟨x, ⟨i, xAi⟩, fxeq⟩ use i, x rintro ⟨i, x, xAi, fxeq⟩ exact ⟨x, ⟨i, xAi⟩, fxeq⟩ example : (f '' ⋂ i, A i) ⊆ ⋂ i, f '' A i := by intro y; simp intro x h fxeq i use x exact ⟨h i, fxeq⟩ example (i : I) (injf : Injective f) : (⋂ i, f '' A i) ⊆ f '' ⋂ i, A i := by intro y; simp intro h rcases h i with ⟨x, xAi, fxeq⟩ use x; constructor · intro i' rcases h i' with ⟨x', x'Ai, fx'eq⟩ have : f x = f x' := by rw [fxeq, fx'eq] have : x = x' := injf this rw [this] exact x'Ai exact fxeq example : (f ⁻¹' ⋃ i, B i) = ⋃ i, f ⁻¹' B i := by ext x simp example : (f ⁻¹' ⋂ i, B i) = ⋂ i, f ⁻¹' B i := by ext x simp end section open Set Real example : InjOn sqrt { x | x ≥ 0 } := by intro x xnonneg y ynonneg intro e calc x = sqrt x ^ 2 := by rw [sq_sqrt xnonneg] _ = sqrt y ^ 2 := by rw [e] _ = y := by rw [sq_sqrt ynonneg] example : InjOn (fun x ↦ x ^ 2) { x : ℝ | x ≥ 0 } := by intro x xnonneg y ynonneg intro e dsimp at * calc x = sqrt (x ^ 2) := by rw [sqrt_sq xnonneg] _ = sqrt (y ^ 2) := by rw [e] _ = y := by rw [sqrt_sq ynonneg] example : sqrt '' { x | x ≥ 0 } = { y | y ≥ 0 } := by ext y; constructor · rintro ⟨x, ⟨xnonneg, rfl⟩⟩ apply sqrt_nonneg intro ynonneg use y ^ 2 dsimp at * constructor apply pow_nonneg ynonneg apply sqrt_sq assumption example : (range fun x ↦ x ^ 2) = { y : ℝ | y ≥ 0 } := by ext y constructor · rintro ⟨x, rfl⟩ dsimp at * apply pow_two_nonneg intro ynonneg use sqrt y exact sq_sqrt ynonneg end section variable {α β : Type*} [Inhabited α] noncomputable section open Classical def inverse (f : α → β) : β → α := fun y : β ↦ if h : ∃ x, f x = y then Classical.choose h else default theorem inverse_spec {f : α → β} (y : β) (h : ∃ x, f x = y) : f (inverse f y) = y := by rw [inverse, dif_pos h] exact Classical.choose_spec h variable (f : α → β) open Function example : Injective f ↔ LeftInverse (inverse f) f := by constructor · intro h y apply h apply inverse_spec use y intro h x1 x2 e rw [← h x1, ← h x2, e] example : Injective f ↔ LeftInverse (inverse f) f := ⟨fun h y ↦ h (inverse_spec _ ⟨y, rfl⟩), fun h x1 x2 e ↦ by rw [← h x1, ← h x2, e]⟩ example : Surjective f ↔ RightInverse (inverse f) f := by constructor · intro h y apply inverse_spec apply h intro h y use inverse f y apply h example : Surjective f ↔ RightInverse (inverse f) f := ⟨fun h y ↦ inverse_spec _ (h _), fun h y ↦ ⟨inverse f y, h _⟩⟩ end section variable {α : Type*} open Function theorem Cantor : ∀ f : α → Set α, ¬Surjective f := by intro f surjf let S := { i | i ∉ f i } rcases surjf S with ⟨j, h⟩ have h₁ : j ∉ f j := by intro h' have : j ∉ f j := by rwa [h] at h' contradiction have h₂ : j ∈ S := h₁ have h₃ : j ∉ S := by rwa [h] at h₁ contradiction end
mathematics_in_lean/MIL/C07_Structures/S03_Building_the_Gaussian_Integers.lean
import Mathlib.Algebra.EuclideanDomain.Basic import Mathlib.RingTheory.PrincipalIdealDomain import MIL.Common @[ext] structure GaussInt where re : ℤ im : ℤ namespace GaussInt instance : Zero GaussInt := ⟨⟨0, 0⟩⟩ instance : One GaussInt := ⟨⟨1, 0⟩⟩ instance : Add GaussInt := ⟨fun x y ↦ ⟨x.re + y.re, x.im + y.im⟩⟩ instance : Neg GaussInt := ⟨fun x ↦ ⟨-x.re, -x.im⟩⟩ instance : Mul GaussInt := ⟨fun x y ↦ ⟨x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re⟩⟩ theorem zero_def : (0 : GaussInt) = ⟨0, 0⟩ := rfl theorem one_def : (1 : GaussInt) = ⟨1, 0⟩ := rfl theorem add_def (x y : GaussInt) : x + y = ⟨x.re + y.re, x.im + y.im⟩ := rfl theorem neg_def (x : GaussInt) : -x = ⟨-x.re, -x.im⟩ := rfl theorem mul_def (x y : GaussInt) : x * y = ⟨x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re⟩ := rfl @[simp] theorem zero_re : (0 : GaussInt).re = 0 := rfl @[simp] theorem zero_im : (0 : GaussInt).im = 0 := rfl @[simp] theorem one_re : (1 : GaussInt).re = 1 := rfl @[simp] theorem one_im : (1 : GaussInt).im = 0 := rfl @[simp] theorem add_re (x y : GaussInt) : (x + y).re = x.re + y.re := rfl @[simp] theorem add_im (x y : GaussInt) : (x + y).im = x.im + y.im := rfl @[simp] theorem neg_re (x : GaussInt) : (-x).re = -x.re := rfl @[simp] theorem neg_im (x : GaussInt) : (-x).im = -x.im := rfl @[simp] theorem mul_re (x y : GaussInt) : (x * y).re = x.re * y.re - x.im * y.im := rfl @[simp] theorem mul_im (x y : GaussInt) : (x * y).im = x.re * y.im + x.im * y.re := rfl instance instCommRing : CommRing GaussInt where zero := 0 one := 1 add := (· + ·) neg x := -x mul := (· * ·) nsmul := nsmulRec zsmul := zsmulRec add_assoc := by intros ext <;> simp <;> ring zero_add := by intro ext <;> simp add_zero := by intro ext <;> simp neg_add_cancel := by intro ext <;> simp add_comm := by intros ext <;> simp <;> ring mul_assoc := by intros ext <;> simp <;> ring one_mul := by intro ext <;> simp mul_one := by intro ext <;> simp left_distrib := by intros ext <;> simp <;> ring right_distrib := by intros ext <;> simp <;> ring mul_comm := by intros ext <;> simp <;> ring zero_mul := by intros ext <;> simp mul_zero := by intros ext <;> simp @[simp] theorem sub_re (x y : GaussInt) : (x - y).re = x.re - y.re := rfl @[simp] theorem sub_im (x y : GaussInt) : (x - y).im = x.im - y.im := rfl instance : Nontrivial GaussInt := by use 0, 1 rw [Ne, GaussInt.ext_iff] simp end GaussInt example (a b : ℤ) : a = b * (a / b) + a % b := Eq.symm (Int.ediv_add_emod a b) example (a b : ℤ) : b ≠ 0 → 0 ≤ a % b := Int.emod_nonneg a example (a b : ℤ) : b ≠ 0 → a % b < |b| := Int.emod_lt_abs a namespace Int def div' (a b : ℤ) := (a + b / 2) / b def mod' (a b : ℤ) := (a + b / 2) % b - b / 2 theorem div'_add_mod' (a b : ℤ) : b * div' a b + mod' a b = a := by rw [div', mod'] linarith [Int.ediv_add_emod (a + b / 2) b] theorem abs_mod'_le (a b : ℤ) (h : 0 < b) : |mod' a b| ≤ b / 2 := by rw [mod', abs_le] constructor · linarith [Int.emod_nonneg (a + b / 2) h.ne'] have := Int.emod_lt_of_pos (a + b / 2) h have := Int.ediv_add_emod b 2 have := Int.emod_lt_of_pos b zero_lt_two linarith theorem mod'_eq (a b : ℤ) : mod' a b = a - b * div' a b := by linarith [div'_add_mod' a b] end Int theorem sq_add_sq_eq_zero {α : Type*} [Ring α] [LinearOrder α] [IsStrictOrderedRing α] (x y : α) : x ^ 2 + y ^ 2 = 0 ↔ x = 0 ∧ y = 0 := by sorry namespace GaussInt def norm (x : GaussInt) := x.re ^ 2 + x.im ^ 2 @[simp] theorem norm_nonneg (x : GaussInt) : 0 ≤ norm x := by sorry theorem norm_eq_zero (x : GaussInt) : norm x = 0 ↔ x = 0 := by sorry theorem norm_pos (x : GaussInt) : 0 < norm x ↔ x ≠ 0 := by sorry theorem norm_mul (x y : GaussInt) : norm (x * y) = norm x * norm y := by sorry def conj (x : GaussInt) : GaussInt := ⟨x.re, -x.im⟩ @[simp] theorem conj_re (x : GaussInt) : (conj x).re = x.re := rfl @[simp] theorem conj_im (x : GaussInt) : (conj x).im = -x.im := rfl theorem norm_conj (x : GaussInt) : norm (conj x) = norm x := by simp [norm] instance : Div GaussInt := ⟨fun x y ↦ ⟨Int.div' (x * conj y).re (norm y), Int.div' (x * conj y).im (norm y)⟩⟩ instance : Mod GaussInt := ⟨fun x y ↦ x - y * (x / y)⟩ theorem div_def (x y : GaussInt) : x / y = ⟨Int.div' (x * conj y).re (norm y), Int.div' (x * conj y).im (norm y)⟩ := rfl theorem mod_def (x y : GaussInt) : x % y = x - y * (x / y) := rfl theorem norm_mod_lt (x : GaussInt) {y : GaussInt} (hy : y ≠ 0) : (x % y).norm < y.norm := by have norm_y_pos : 0 < norm y := by rwa [norm_pos] have H1 : x % y * conj y = ⟨Int.mod' (x * conj y).re (norm y), Int.mod' (x * conj y).im (norm y)⟩ · ext <;> simp [Int.mod'_eq, mod_def, div_def, norm] <;> ring have H2 : norm (x % y) * norm y ≤ norm y / 2 * norm y · calc norm (x % y) * norm y = norm (x % y * conj y) := by simp only [norm_mul, norm_conj] _ = |Int.mod' (x.re * y.re + x.im * y.im) (norm y)| ^ 2 + |Int.mod' (-(x.re * y.im) + x.im * y.re) (norm y)| ^ 2 := by simp [H1, norm, sq_abs] _ ≤ (y.norm / 2) ^ 2 + (y.norm / 2) ^ 2 := by gcongr <;> apply Int.abs_mod'_le _ _ norm_y_pos _ = norm y / 2 * (norm y / 2 * 2) := by ring _ ≤ norm y / 2 * norm y := by gcongr; apply Int.ediv_mul_le; norm_num calc norm (x % y) ≤ norm y / 2 := le_of_mul_le_mul_right H2 norm_y_pos _ < norm y := by apply Int.ediv_lt_of_lt_mul · norm_num · linarith theorem coe_natAbs_norm (x : GaussInt) : (x.norm.natAbs : ℤ) = x.norm := Int.natAbs_of_nonneg (norm_nonneg _) theorem natAbs_norm_mod_lt (x y : GaussInt) (hy : y ≠ 0) : (x % y).norm.natAbs < y.norm.natAbs := by apply Int.ofNat_lt.1 simp only [Int.natCast_natAbs, abs_of_nonneg, norm_nonneg] exact norm_mod_lt x hy theorem not_norm_mul_left_lt_norm (x : GaussInt) {y : GaussInt} (hy : y ≠ 0) : ¬(norm (x * y)).natAbs < (norm x).natAbs := by apply not_lt_of_ge rw [norm_mul, Int.natAbs_mul] apply le_mul_of_one_le_right (Nat.zero_le _) apply Int.ofNat_le.1 rw [coe_natAbs_norm] exact Int.add_one_le_of_lt ((norm_pos _).mpr hy) instance : EuclideanDomain GaussInt := { GaussInt.instCommRing with quotient := (· / ·) remainder := (· % ·) quotient_mul_add_remainder_eq := fun x y ↦ by rw [mod_def, add_comm] ; ring quotient_zero := fun x ↦ by simp [div_def, norm, Int.div'] rfl r := (measure (Int.natAbs ∘ norm)).1 r_wellFounded := (measure (Int.natAbs ∘ norm)).2 remainder_lt := natAbs_norm_mod_lt mul_left_not_lt := not_norm_mul_left_lt_norm } example (x : GaussInt) : Irreducible x ↔ Prime x := irreducible_iff_prime end GaussInt
mathematics_in_lean/MIL/C07_Structures/S02_Algebraic_Structures.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C06S02 structure Group₁ (α : Type*) where mul : α → α → α one : α inv : α → α mul_assoc : ∀ x y z : α, mul (mul x y) z = mul x (mul y z) mul_one : ∀ x : α, mul x one = x one_mul : ∀ x : α, mul one x = x inv_mul_cancel : ∀ x : α, mul (inv x) x = one structure Grp₁ where α : Type* str : Group₁ α section variable (α β γ : Type*) variable (f : α ≃ β) (g : β ≃ γ) #check Equiv α β #check (f.toFun : α → β) #check (f.invFun : β → α) #check (f.right_inv : ∀ x : β, f (f.invFun x) = x) #check (f.left_inv : ∀ x : α, f.invFun (f x) = x) #check (Equiv.refl α : α ≃ α) #check (f.symm : β ≃ α) #check (f.trans g : α ≃ γ) example (x : α) : (f.trans g).toFun x = g.toFun (f.toFun x) := rfl example (x : α) : (f.trans g) x = g (f x) := rfl example : (f.trans g : α → γ) = g ∘ f := rfl end example (α : Type*) : Equiv.Perm α = (α ≃ α) := rfl def permGroup {α : Type*} : Group₁ (Equiv.Perm α) where mul f g := Equiv.trans g f one := Equiv.refl α inv := Equiv.symm mul_assoc f g h := (Equiv.trans_assoc _ _ _).symm one_mul := Equiv.trans_refl mul_one := Equiv.refl_trans inv_mul_cancel := Equiv.self_trans_symm structure AddGroup₁ (α : Type*) where (add : α → α → α) -- fill in the rest @[ext] structure Point where x : ℝ y : ℝ z : ℝ namespace Point def add (a b : Point) : Point := ⟨a.x + b.x, a.y + b.y, a.z + b.z⟩ def neg (a : Point) : Point := sorry def zero : Point := sorry def addGroupPoint : AddGroup₁ Point := sorry end Point section variable {α : Type*} (f g : Equiv.Perm α) (n : ℕ) #check f * g #check mul_assoc f g g⁻¹ -- group power, defined for any group #check g ^ n example : f * g * g⁻¹ = f := by rw [mul_assoc, mul_inv_cancel, mul_one] example : f * g * g⁻¹ = f := mul_inv_cancel_right f g example {α : Type*} (f g : Equiv.Perm α) : g.symm.trans (g.trans f) = f := mul_inv_cancel_right f g end class Group₂ (α : Type*) where mul : α → α → α one : α inv : α → α mul_assoc : ∀ x y z : α, mul (mul x y) z = mul x (mul y z) mul_one : ∀ x : α, mul x one = x one_mul : ∀ x : α, mul one x = x inv_mul_cancel : ∀ x : α, mul (inv x) x = one instance {α : Type*} : Group₂ (Equiv.Perm α) where mul f g := Equiv.trans g f one := Equiv.refl α inv := Equiv.symm mul_assoc f g h := (Equiv.trans_assoc _ _ _).symm one_mul := Equiv.trans_refl mul_one := Equiv.refl_trans inv_mul_cancel := Equiv.self_trans_symm #check Group₂.mul def mySquare {α : Type*} [Group₂ α] (x : α) := Group₂.mul x x #check mySquare section variable {β : Type*} (f g : Equiv.Perm β) example : Group₂.mul f g = g.trans f := rfl example : mySquare f = f.trans f := rfl end instance : Inhabited Point where default := ⟨0, 0, 0⟩ #check (default : Point) example : ([] : List Point).headI = default := rfl instance : Add Point where add := Point.add section variable (x y : Point) #check x + y example : x + y = Point.add x y := rfl end instance {α : Type*} [Group₂ α] : Mul α := ⟨Group₂.mul⟩ instance {α : Type*} [Group₂ α] : One α := ⟨Group₂.one⟩ instance {α : Type*} [Group₂ α] : Inv α := ⟨Group₂.inv⟩ section variable {α : Type*} (f g : Equiv.Perm α) #check f * 1 * g⁻¹ def foo : f * 1 * g⁻¹ = g.symm.trans ((Equiv.refl α).trans f) := rfl end class AddGroup₂ (α : Type*) where add : α → α → α -- fill in the rest
mathematics_in_lean/MIL/C07_Structures/S01_Structures.lean
import MIL.Common import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Real.Basic namespace C06S01 noncomputable section @[ext] structure Point where x : ℝ y : ℝ z : ℝ #check Point.ext example (a b : Point) (hx : a.x = b.x) (hy : a.y = b.y) (hz : a.z = b.z) : a = b := by ext repeat' assumption def myPoint1 : Point where x := 2 y := -1 z := 4 def myPoint2 : Point := ⟨2, -1, 4⟩ def myPoint3 := Point.mk 2 (-1) 4 structure Point' where build :: x : ℝ y : ℝ z : ℝ #check Point'.build 2 (-1) 4 namespace Point def add (a b : Point) : Point := ⟨a.x + b.x, a.y + b.y, a.z + b.z⟩ def add' (a b : Point) : Point where x := a.x + b.x y := a.y + b.y z := a.z + b.z #check add myPoint1 myPoint2 #check myPoint1.add myPoint2 end Point #check Point.add myPoint1 myPoint2 #check myPoint1.add myPoint2 namespace Point protected theorem add_comm (a b : Point) : add a b = add b a := by rw [add, add] ext <;> dsimp repeat' apply add_comm example (a b : Point) : add a b = add b a := by simp [add, add_comm] theorem add_x (a b : Point) : (a.add b).x = a.x + b.x := rfl def addAlt : Point → Point → Point | Point.mk x₁ y₁ z₁, Point.mk x₂ y₂ z₂ => ⟨x₁ + x₂, y₁ + y₂, z₁ + z₂⟩ def addAlt' : Point → Point → Point | ⟨x₁, y₁, z₁⟩, ⟨x₂, y₂, z₂⟩ => ⟨x₁ + x₂, y₁ + y₂, z₁ + z₂⟩ theorem addAlt_x (a b : Point) : (a.addAlt b).x = a.x + b.x := by rfl theorem addAlt_comm (a b : Point) : addAlt a b = addAlt b a := by rw [addAlt, addAlt] -- the same proof still works, but the goal view here is harder to read ext <;> dsimp repeat' apply add_comm protected theorem add_assoc (a b c : Point) : (a.add b).add c = a.add (b.add c) := by sorry def smul (r : ℝ) (a : Point) : Point := sorry theorem smul_distrib (r : ℝ) (a b : Point) : (smul r a).add (smul r b) = smul r (a.add b) := by sorry end Point structure StandardTwoSimplex where x : ℝ y : ℝ z : ℝ x_nonneg : 0 ≤ x y_nonneg : 0 ≤ y z_nonneg : 0 ≤ z sum_eq : x + y + z = 1 namespace StandardTwoSimplex def swapXy (a : StandardTwoSimplex) : StandardTwoSimplex where x := a.y y := a.x z := a.z x_nonneg := a.y_nonneg y_nonneg := a.x_nonneg z_nonneg := a.z_nonneg sum_eq := by rw [add_comm a.y a.x, a.sum_eq] noncomputable section def midpoint (a b : StandardTwoSimplex) : StandardTwoSimplex where x := (a.x + b.x) / 2 y := (a.y + b.y) / 2 z := (a.z + b.z) / 2 x_nonneg := div_nonneg (add_nonneg a.x_nonneg b.x_nonneg) (by norm_num) y_nonneg := div_nonneg (add_nonneg a.y_nonneg b.y_nonneg) (by norm_num) z_nonneg := div_nonneg (add_nonneg a.z_nonneg b.z_nonneg) (by norm_num) sum_eq := by field_simp; linarith [a.sum_eq, b.sum_eq] def weightedAverage (lambda : Real) (lambda_nonneg : 0 ≤ lambda) (lambda_le : lambda ≤ 1) (a b : StandardTwoSimplex) : StandardTwoSimplex := sorry end end StandardTwoSimplex open BigOperators structure StandardSimplex (n : ℕ) where V : Fin n → ℝ NonNeg : ∀ i : Fin n, 0 ≤ V i sum_eq_one : (∑ i, V i) = 1 namespace StandardSimplex def midpoint (n : ℕ) (a b : StandardSimplex n) : StandardSimplex n where V i := (a.V i + b.V i) / 2 NonNeg := by intro i apply div_nonneg · linarith [a.NonNeg i, b.NonNeg i] norm_num sum_eq_one := by simp [div_eq_mul_inv, ← Finset.sum_mul, Finset.sum_add_distrib, a.sum_eq_one, b.sum_eq_one] field_simp end StandardSimplex structure IsLinear (f : ℝ → ℝ) where is_additive : ∀ x y, f (x + y) = f x + f y preserves_mul : ∀ x c, f (c * x) = c * f x section variable (f : ℝ → ℝ) (linf : IsLinear f) #check linf.is_additive #check linf.preserves_mul end def Point'' := ℝ × ℝ × ℝ def IsLinear' (f : ℝ → ℝ) := (∀ x y, f (x + y) = f x + f y) ∧ ∀ x c, f (c * x) = c * f x def PReal := { y : ℝ // 0 < y } section variable (x : PReal) #check x.val #check x.property #check x.1 #check x.2 end def StandardTwoSimplex' := { p : ℝ × ℝ × ℝ // 0 ≤ p.1 ∧ 0 ≤ p.2.1 ∧ 0 ≤ p.2.2 ∧ p.1 + p.2.1 + p.2.2 = 1 } def StandardSimplex' (n : ℕ) := { v : Fin n → ℝ // (∀ i : Fin n, 0 ≤ v i) ∧ (∑ i, v i) = 1 } def StdSimplex := Σ n : ℕ, StandardSimplex n section variable (s : StdSimplex) #check s.fst #check s.snd #check s.1 #check s.2 end
mathematics_in_lean/MIL/C07_Structures/solutions/Solutions_S03_Building_the_Gaussian_Integers.lean
import Mathlib.Algebra.EuclideanDomain.Basic import Mathlib.RingTheory.PrincipalIdealDomain import MIL.Common @[ext] structure GaussInt where re : ℤ im : ℤ namespace GaussInt instance : Zero GaussInt := ⟨⟨0, 0⟩⟩ instance : One GaussInt := ⟨⟨1, 0⟩⟩ instance : Add GaussInt := ⟨fun x y ↦ ⟨x.re + y.re, x.im + y.im⟩⟩ instance : Neg GaussInt := ⟨fun x ↦ ⟨-x.re, -x.im⟩⟩ instance : Mul GaussInt := ⟨fun x y ↦ ⟨x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re⟩⟩ theorem zero_def : (0 : GaussInt) = ⟨0, 0⟩ := rfl theorem one_def : (1 : GaussInt) = ⟨1, 0⟩ := rfl theorem add_def (x y : GaussInt) : x + y = ⟨x.re + y.re, x.im + y.im⟩ := rfl theorem neg_def (x : GaussInt) : -x = ⟨-x.re, -x.im⟩ := rfl theorem mul_def (x y : GaussInt) : x * y = ⟨x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re⟩ := rfl @[simp] theorem zero_re : (0 : GaussInt).re = 0 := rfl @[simp] theorem zero_im : (0 : GaussInt).im = 0 := rfl @[simp] theorem one_re : (1 : GaussInt).re = 1 := rfl @[simp] theorem one_im : (1 : GaussInt).im = 0 := rfl @[simp] theorem add_re (x y : GaussInt) : (x + y).re = x.re + y.re := rfl @[simp] theorem add_im (x y : GaussInt) : (x + y).im = x.im + y.im := rfl @[simp] theorem neg_re (x : GaussInt) : (-x).re = -x.re := rfl @[simp] theorem neg_im (x : GaussInt) : (-x).im = -x.im := rfl @[simp] theorem mul_re (x y : GaussInt) : (x * y).re = x.re * y.re - x.im * y.im := rfl @[simp] theorem mul_im (x y : GaussInt) : (x * y).im = x.re * y.im + x.im * y.re := rfl instance instCommRing : CommRing GaussInt where zero := 0 one := 1 add := (· + ·) neg x := -x mul := (· * ·) nsmul := nsmulRec zsmul := zsmulRec add_assoc := by intros ext <;> simp <;> ring zero_add := by intro ext <;> simp add_zero := by intro ext <;> simp neg_add_cancel := by intro ext <;> simp add_comm := by intros ext <;> simp <;> ring mul_assoc := by intros ext <;> simp <;> ring one_mul := by intro ext <;> simp mul_one := by intro ext <;> simp left_distrib := by intros ext <;> simp <;> ring right_distrib := by intros ext <;> simp <;> ring mul_comm := by intros ext <;> simp <;> ring zero_mul := by intros ext <;> simp mul_zero := by intros ext <;> simp @[simp] theorem sub_re (x y : GaussInt) : (x - y).re = x.re - y.re := rfl @[simp] theorem sub_im (x y : GaussInt) : (x - y).im = x.im - y.im := rfl instance : Nontrivial GaussInt := by use 0, 1 rw [Ne, GaussInt.ext_iff] simp end GaussInt namespace Int def div' (a b : ℤ) := (a + b / 2) / b def mod' (a b : ℤ) := (a + b / 2) % b - b / 2 theorem div'_add_mod' (a b : ℤ) : b * div' a b + mod' a b = a := by rw [div', mod'] linarith [Int.ediv_add_emod (a + b / 2) b] theorem abs_mod'_le (a b : ℤ) (h : 0 < b) : |mod' a b| ≤ b / 2 := by rw [mod', abs_le] constructor · linarith [Int.emod_nonneg (a + b / 2) h.ne'] have := Int.emod_lt_of_pos (a + b / 2) h have := Int.ediv_add_emod b 2 have := Int.emod_lt_of_pos b zero_lt_two linarith theorem mod'_eq (a b : ℤ) : mod' a b = a - b * div' a b := by linarith [div'_add_mod' a b] end Int private theorem aux {α : Type*} [Ring α] [LinearOrder α] [IsStrictOrderedRing α] {x y : α} (h : x ^ 2 + y ^ 2 = 0) : x = 0 := haveI h' : x ^ 2 = 0 := by apply le_antisymm _ (sq_nonneg x) rw [← h] apply le_add_of_nonneg_right (sq_nonneg y) pow_eq_zero h' theorem sq_add_sq_eq_zero {α : Type*} [Ring α] [LinearOrder α] [IsStrictOrderedRing α] (x y : α) : x ^ 2 + y ^ 2 = 0 ↔ x = 0 ∧ y = 0 := by constructor · intro h constructor · exact aux h rw [add_comm] at h exact aux h rintro ⟨rfl, rfl⟩ norm_num namespace GaussInt def norm (x : GaussInt) := x.re ^ 2 + x.im ^ 2 @[simp] theorem norm_nonneg (x : GaussInt) : 0 ≤ norm x := by apply add_nonneg <;> apply sq_nonneg theorem norm_eq_zero (x : GaussInt) : norm x = 0 ↔ x = 0 := by rw [norm, sq_add_sq_eq_zero, GaussInt.ext_iff] rfl theorem norm_pos (x : GaussInt) : 0 < norm x ↔ x ≠ 0 := by rw [lt_iff_le_and_ne, ne_comm, Ne, norm_eq_zero] simp [norm_nonneg] theorem norm_mul (x y : GaussInt) : norm (x * y) = norm x * norm y := by simp [norm] ring def conj (x : GaussInt) : GaussInt := ⟨x.re, -x.im⟩ @[simp] theorem conj_re (x : GaussInt) : (conj x).re = x.re := rfl @[simp] theorem conj_im (x : GaussInt) : (conj x).im = -x.im := rfl theorem norm_conj (x : GaussInt) : norm (conj x) = norm x := by simp [norm] instance : Div GaussInt := ⟨fun x y ↦ ⟨Int.div' (x * conj y).re (norm y), Int.div' (x * conj y).im (norm y)⟩⟩ instance : Mod GaussInt := ⟨fun x y ↦ x - y * (x / y)⟩ theorem div_def (x y : GaussInt) : x / y = ⟨Int.div' (x * conj y).re (norm y), Int.div' (x * conj y).im (norm y)⟩ := rfl theorem mod_def (x y : GaussInt) : x % y = x - y * (x / y) := rfl theorem norm_mod_lt (x : GaussInt) {y : GaussInt} (hy : y ≠ 0) : (x % y).norm < y.norm := by have norm_y_pos : 0 < norm y := by rwa [norm_pos] have H1 : x % y * conj y = ⟨Int.mod' (x * conj y).re (norm y), Int.mod' (x * conj y).im (norm y)⟩ · ext <;> simp [Int.mod'_eq, mod_def, div_def, norm] <;> ring have H2 : norm (x % y) * norm y ≤ norm y / 2 * norm y · calc norm (x % y) * norm y = norm (x % y * conj y) := by simp only [norm_mul, norm_conj] _ = |Int.mod' (x.re * y.re + x.im * y.im) (norm y)| ^ 2 + |Int.mod' (-(x.re * y.im) + x.im * y.re) (norm y)| ^ 2 := by simp [H1, norm, sq_abs] _ ≤ (y.norm / 2) ^ 2 + (y.norm / 2) ^ 2 := by gcongr <;> apply Int.abs_mod'_le _ _ norm_y_pos _ = norm y / 2 * (norm y / 2 * 2) := by ring _ ≤ norm y / 2 * norm y := by gcongr; apply Int.ediv_mul_le; norm_num calc norm (x % y) ≤ norm y / 2 := le_of_mul_le_mul_right H2 norm_y_pos _ < norm y := by apply Int.ediv_lt_of_lt_mul · norm_num · linarith theorem coe_natAbs_norm (x : GaussInt) : (x.norm.natAbs : ℤ) = x.norm := Int.natAbs_of_nonneg (norm_nonneg _) theorem natAbs_norm_mod_lt (x y : GaussInt) (hy : y ≠ 0) : (x % y).norm.natAbs < y.norm.natAbs := by apply Int.ofNat_lt.1 simp only [Int.natCast_natAbs, abs_of_nonneg, norm_nonneg] exact norm_mod_lt x hy theorem not_norm_mul_left_lt_norm (x : GaussInt) {y : GaussInt} (hy : y ≠ 0) : ¬(norm (x * y)).natAbs < (norm x).natAbs := by apply not_lt_of_ge rw [norm_mul, Int.natAbs_mul] apply le_mul_of_one_le_right (Nat.zero_le _) apply Int.ofNat_le.1 rw [coe_natAbs_norm] exact Int.add_one_le_of_lt ((norm_pos _).mpr hy) instance : EuclideanDomain GaussInt := { GaussInt.instCommRing with quotient := (· / ·) remainder := (· % ·) quotient_mul_add_remainder_eq := fun x y ↦ by rw [mod_def, add_comm] ; ring quotient_zero := fun x ↦ by simp [div_def, norm, Int.div'] rfl r := (measure (Int.natAbs ∘ norm)).1 r_wellFounded := (measure (Int.natAbs ∘ norm)).2 remainder_lt := natAbs_norm_mod_lt mul_left_not_lt := not_norm_mul_left_lt_norm } example (x : GaussInt) : Irreducible x ↔ Prime x := irreducible_iff_prime end GaussInt
mathematics_in_lean/MIL/C07_Structures/solutions/Solutions_S01_Structures.lean
import MIL.Common import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Real.Basic namespace C06S01 noncomputable section @[ext] structure Point where x : ℝ y : ℝ z : ℝ namespace Point def add (a b : Point) : Point := ⟨a.x + b.x, a.y + b.y, a.z + b.z⟩ protected theorem add_assoc (a b c : Point) : (a.add b).add c = a.add (b.add c) := by simp [add, add_assoc] def smul (r : ℝ) (a : Point) : Point := ⟨r * a.x, r * a.y, r * a.z⟩ theorem smul_distrib (r : ℝ) (a b : Point) : (smul r a).add (smul r b) = smul r (a.add b) := by simp [add, smul, mul_add] end Point structure StandardTwoSimplex where x : ℝ y : ℝ z : ℝ x_nonneg : 0 ≤ x y_nonneg : 0 ≤ y z_nonneg : 0 ≤ z sum_eq : x + y + z = 1 namespace StandardTwoSimplex noncomputable section def weightedAverage (lambda : Real) (lambda_nonneg : 0 ≤ lambda) (lambda_le : lambda ≤ 1) (a b : StandardTwoSimplex) : StandardTwoSimplex where x := lambda * a.x + (1 - lambda) * b.x y := lambda * a.y + (1 - lambda) * b.y z := lambda * a.z + (1 - lambda) * b.z x_nonneg := add_nonneg (mul_nonneg lambda_nonneg a.x_nonneg) (mul_nonneg (by linarith) b.x_nonneg) y_nonneg := add_nonneg (mul_nonneg lambda_nonneg a.y_nonneg) (mul_nonneg (by linarith) b.y_nonneg) z_nonneg := add_nonneg (mul_nonneg lambda_nonneg a.z_nonneg) (mul_nonneg (by linarith) b.z_nonneg) sum_eq := by trans (a.x + a.y + a.z) * lambda + (b.x + b.y + b.z) * (1 - lambda) · ring simp [a.sum_eq, b.sum_eq] end end StandardTwoSimplex open BigOperators structure StandardSimplex (n : ℕ) where V : Fin n → ℝ NonNeg : ∀ i : Fin n, 0 ≤ V i sum_eq_one : (∑ i, V i) = 1 namespace StandardSimplex def midpoint (n : ℕ) (a b : StandardSimplex n) : StandardSimplex n where V i := (a.V i + b.V i) / 2 NonNeg := by intro i apply div_nonneg · linarith [a.NonNeg i, b.NonNeg i] norm_num sum_eq_one := by simp [div_eq_mul_inv, ← Finset.sum_mul, Finset.sum_add_distrib, a.sum_eq_one, b.sum_eq_one] field_simp end StandardSimplex namespace StandardSimplex def weightedAverage {n : ℕ} (lambda : Real) (lambda_nonneg : 0 ≤ lambda) (lambda_le : lambda ≤ 1) (a b : StandardSimplex n) : StandardSimplex n where V i := lambda * a.V i + (1 - lambda) * b.V i NonNeg i := add_nonneg (mul_nonneg lambda_nonneg (a.NonNeg i)) (mul_nonneg (by linarith) (b.NonNeg i)) sum_eq_one := by trans (lambda * ∑ i, a.V i) + (1 - lambda) * ∑ i, b.V i · rw [Finset.sum_add_distrib, Finset.mul_sum, Finset.mul_sum] simp [a.sum_eq_one, b.sum_eq_one] end StandardSimplex
mathematics_in_lean/MIL/C07_Structures/solutions/Solutions_S02_Algebraic_Structures.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C06S02 structure AddGroup₁ (α : Type*) where add : α → α → α zero : α neg : α → α add_assoc : ∀ x y z : α, add (add x y) z = add x (add y z) add_zero : ∀ x : α, add x zero = x zero_add : ∀ x : α, add x zero = x neg_add_cancel : ∀ x : α, add (neg x) x = zero @[ext] structure Point where x : ℝ y : ℝ z : ℝ namespace Point def add (a b : Point) : Point := ⟨a.x + b.x, a.y + b.y, a.z + b.z⟩ def neg (a : Point) : Point := ⟨-a.x, -a.y, -a.z⟩ def zero : Point := ⟨0, 0, 0⟩ def addGroupPoint : AddGroup₁ Point where add := Point.add zero := Point.zero neg := Point.neg add_assoc := by simp [Point.add, add_assoc] add_zero := by simp [Point.add, Point.zero] zero_add := by simp [Point.add, Point.zero] neg_add_cancel := by simp [Point.add, Point.neg, Point.zero] end Point class AddGroup₂ (α : Type*) where add : α → α → α zero : α neg : α → α add_assoc : ∀ x y z : α, add (add x y) z = add x (add y z) add_zero : ∀ x : α, add x zero = x zero_add : ∀ x : α, add x zero = x neg_add_cancel : ∀ x : α, add (neg x) x = zero instance hasAddAddGroup₂ {α : Type*} [AddGroup₂ α] : Add α := ⟨AddGroup₂.add⟩ instance hasZeroAddGroup₂ {α : Type*} [AddGroup₂ α] : Zero α := ⟨AddGroup₂.zero⟩ instance hasNegAddGroup₂ {α : Type*} [AddGroup₂ α] : Neg α := ⟨AddGroup₂.neg⟩ instance : AddGroup₂ Point where add := Point.add zero := Point.zero neg := Point.neg add_assoc := by simp [Point.add, add_assoc] add_zero := by simp [Point.add, Point.zero] zero_add := by simp [Point.add, Point.zero] neg_add_cancel := by simp [Point.add, Point.neg, Point.zero] section variable (x y : Point) #check x + -y + 0 end
mathematics_in_lean/MIL/C09_Groups_and_Rings/S01_Groups.lean
import Mathlib.GroupTheory.Sylow import Mathlib.GroupTheory.Perm.Cycle.Concrete import Mathlib.GroupTheory.Perm.Subgroup import Mathlib.GroupTheory.PresentedGroup import MIL.Common example {M : Type*} [Monoid M] (x : M) : x * 1 = x := mul_one x example {M : Type*} [AddCommMonoid M] (x y : M) : x + y = y + x := add_comm x y example {M N : Type*} [Monoid M] [Monoid N] (x y : M) (f : M →* N) : f (x * y) = f x * f y := f.map_mul x y example {M N : Type*} [AddMonoid M] [AddMonoid N] (f : M →+ N) : f 0 = 0 := f.map_zero example {M N P : Type*} [AddMonoid M] [AddMonoid N] [AddMonoid P] (f : M →+ N) (g : N →+ P) : M →+ P := g.comp f example {G : Type*} [Group G] (x : G) : x * x⁻¹ = 1 := mul_inv_cancel x example {G : Type*} [Group G] (x y z : G) : x * (y * z) * (x * z)⁻¹ * (x * y * x⁻¹)⁻¹ = 1 := by group example {G : Type*} [AddCommGroup G] (x y z : G) : z + x + (y - z - x) = y := by abel example {G H : Type*} [Group G] [Group H] (x y : G) (f : G →* H) : f (x * y) = f x * f y := f.map_mul x y example {G H : Type*} [Group G] [Group H] (x : G) (f : G →* H) : f (x⁻¹) = (f x)⁻¹ := f.map_inv x example {G H : Type*} [Group G] [Group H] (f : G → H) (h : ∀ x y, f (x * y) = f x * f y) : G →* H := MonoidHom.mk' f h example {G H : Type*} [Group G] [Group H] (f : G ≃* H) : f.trans f.symm = MulEquiv.refl G := f.self_trans_symm noncomputable example {G H : Type*} [Group G] [Group H] (f : G →* H) (h : Function.Bijective f) : G ≃* H := MulEquiv.ofBijective f h example {G : Type*} [Group G] (H : Subgroup G) {x y : G} (hx : x ∈ H) (hy : y ∈ H) : x * y ∈ H := H.mul_mem hx hy example {G : Type*} [Group G] (H : Subgroup G) {x : G} (hx : x ∈ H) : x⁻¹ ∈ H := H.inv_mem hx example : AddSubgroup ℚ where carrier := Set.range ((↑) : ℤ → ℚ) add_mem' := by rintro _ _ ⟨n, rfl⟩ ⟨m, rfl⟩ use n + m simp zero_mem' := by use 0 simp neg_mem' := by rintro _ ⟨n, rfl⟩ use -n simp example {G : Type*} [Group G] (H : Subgroup G) : Group H := inferInstance example {G : Type*} [Group G] (H : Subgroup G) : Group {x : G // x ∈ H} := inferInstance example {G : Type*} [Group G] (H H' : Subgroup G) : ((H ⊓ H' : Subgroup G) : Set G) = (H : Set G) ∩ (H' : Set G) := rfl example {G : Type*} [Group G] (H H' : Subgroup G) : ((H ⊔ H' : Subgroup G) : Set G) = Subgroup.closure ((H : Set G) ∪ (H' : Set G)) := by rw [Subgroup.sup_eq_closure] example {G : Type*} [Group G] (x : G) : x ∈ (⊤ : Subgroup G) := trivial example {G : Type*} [Group G] (x : G) : x ∈ (⊥ : Subgroup G) ↔ x = 1 := Subgroup.mem_bot def conjugate {G : Type*} [Group G] (x : G) (H : Subgroup G) : Subgroup G where carrier := {a : G | ∃ h, h ∈ H ∧ a = x * h * x⁻¹} one_mem' := by dsimp sorry inv_mem' := by dsimp sorry mul_mem' := by dsimp sorry example {G H : Type*} [Group G] [Group H] (G' : Subgroup G) (f : G →* H) : Subgroup H := Subgroup.map f G' example {G H : Type*} [Group G] [Group H] (H' : Subgroup H) (f : G →* H) : Subgroup G := Subgroup.comap f H' #check Subgroup.mem_map #check Subgroup.mem_comap example {G H : Type*} [Group G] [Group H] (f : G →* H) (g : G) : g ∈ MonoidHom.ker f ↔ f g = 1 := f.mem_ker example {G H : Type*} [Group G] [Group H] (f : G →* H) (h : H) : h ∈ MonoidHom.range f ↔ ∃ g : G, f g = h := f.mem_range section exercises variable {G H : Type*} [Group G] [Group H] open Subgroup example (φ : G →* H) (S T : Subgroup H) (hST : S ≤ T) : comap φ S ≤ comap φ T := by sorry example (φ : G →* H) (S T : Subgroup G) (hST : S ≤ T) : map φ S ≤ map φ T := by sorry variable {K : Type*} [Group K] -- Remember you can use the `ext` tactic to prove an equality of subgroups. example (φ : G →* H) (ψ : H →* K) (U : Subgroup K) : comap (ψ.comp φ) U = comap φ (comap ψ U) := by sorry -- Pushing a subgroup along one homomorphism and then another is equal to -- pushing it forward along the composite of the homomorphisms. example (φ : G →* H) (ψ : H →* K) (S : Subgroup G) : map (ψ.comp φ) S = map ψ (S.map φ) := by sorry end exercises open scoped Classical example {G : Type*} [Group G] (G' : Subgroup G) : Nat.card G' ∣ Nat.card G := ⟨G'.index, mul_comm G'.index _ ▸ G'.index_mul_card.symm⟩ open Subgroup example {G : Type*} [Group G] [Finite G] (p : ℕ) {n : ℕ} [Fact p.Prime] (hdvd : p ^ n ∣ Nat.card G) : ∃ K : Subgroup G, Nat.card K = p ^ n := Sylow.exists_subgroup_card_pow_prime p hdvd lemma eq_bot_iff_card {G : Type*} [Group G] {H : Subgroup G} : H = ⊥ ↔ Nat.card H = 1 := by suffices (∀ x ∈ H, x = 1) ↔ ∃ x ∈ H, ∀ a ∈ H, a = x by simpa [eq_bot_iff_forall, Nat.card_eq_one_iff_exists] sorry #check card_dvd_of_le lemma inf_bot_of_coprime {G : Type*} [Group G] (H K : Subgroup G) (h : (Nat.card H).Coprime (Nat.card K)) : H ⊓ K = ⊥ := by sorry open Equiv example {X : Type*} [Finite X] : Subgroup.closure {σ : Perm X | Perm.IsCycle σ} = ⊤ := Perm.closure_isCycle #simp [mul_assoc] c[1, 2, 3] * c[2, 3, 4] section FreeGroup inductive S | a | b | c open S def myElement : FreeGroup S := (.of a) * (.of b)⁻¹ def myMorphism : FreeGroup S →* Perm (Fin 5) := FreeGroup.lift fun | .a => c[1, 2, 3] | .b => c[2, 3, 1] | .c => c[2, 3] def myGroup := PresentedGroup {.of () ^ 3} deriving Group def myMap : Unit → Perm (Fin 5) | () => c[1, 2, 3] lemma compat_myMap : ∀ r ∈ ({.of () ^ 3} : Set (FreeGroup Unit)), FreeGroup.lift myMap r = 1 := by rintro _ rfl simp decide def myNewMorphism : myGroup →* Perm (Fin 5) := PresentedGroup.toGroup compat_myMap end FreeGroup noncomputable section GroupActions example {G X : Type*} [Group G] [MulAction G X] (g g': G) (x : X) : g • (g' • x) = (g * g') • x := (mul_smul g g' x).symm example {G X : Type*} [AddGroup G] [AddAction G X] (g g' : G) (x : X) : g +ᵥ (g' +ᵥ x) = (g + g') +ᵥ x := (add_vadd g g' x).symm open MulAction example {G X : Type*} [Group G] [MulAction G X] : G →* Equiv.Perm X := toPermHom G X def CayleyIsoMorphism (G : Type*) [Group G] : G ≃* (toPermHom G G).range := Equiv.Perm.subgroupOfMulAction G G example {G X : Type*} [Group G] [MulAction G X] : X ≃ (ω : orbitRel.Quotient G X) × (orbit G (Quotient.out ω)) := MulAction.selfEquivSigmaOrbits G X example {G X : Type*} [Group G] [MulAction G X] (x : X) : orbit G x ≃ G ⧸ stabilizer G x := MulAction.orbitEquivQuotientStabilizer G x example {G : Type*} [Group G] (H : Subgroup G) : G ≃ (G ⧸ H) × H := groupEquivQuotientProdSubgroup variable {G : Type*} [Group G] lemma conjugate_one (H : Subgroup G) : conjugate 1 H = H := by sorry instance : MulAction G (Subgroup G) where smul := conjugate one_smul := by sorry mul_smul := by sorry end GroupActions noncomputable section QuotientGroup example {G : Type*} [Group G] (H : Subgroup G) [H.Normal] : Group (G ⧸ H) := inferInstance example {G : Type*} [Group G] (H : Subgroup G) [H.Normal] : G →* G ⧸ H := QuotientGroup.mk' H example {G : Type*} [Group G] (N : Subgroup G) [N.Normal] {M : Type*} [Group M] (φ : G →* M) (h : N ≤ MonoidHom.ker φ) : G ⧸ N →* M := QuotientGroup.lift N φ h example {G : Type*} [Group G] {M : Type*} [Group M] (φ : G →* M) : G ⧸ MonoidHom.ker φ →* MonoidHom.range φ := QuotientGroup.quotientKerEquivRange φ example {G G': Type*} [Group G] [Group G'] {N : Subgroup G} [N.Normal] {N' : Subgroup G'} [N'.Normal] {φ : G →* G'} (h : N ≤ Subgroup.comap φ N') : G ⧸ N →* G' ⧸ N':= QuotientGroup.map N N' φ h example {G : Type*} [Group G] {M N : Subgroup G} [M.Normal] [N.Normal] (h : M = N) : G ⧸ M ≃* G ⧸ N := QuotientGroup.quotientMulEquivOfEq h section variable {G : Type*} [Group G] {H K : Subgroup G} open MonoidHom #check Nat.card_pos -- The nonempty argument will be automatically inferred for subgroups #check Subgroup.index_eq_card #check Subgroup.index_mul_card #check Nat.eq_of_mul_eq_mul_right lemma aux_card_eq [Finite G] (h' : Nat.card G = Nat.card H * Nat.card K) : Nat.card (G ⧸ H) = Nat.card K := by sorry variable [H.Normal] [K.Normal] [Fintype G] (h : Disjoint H K) (h' : Nat.card G = Nat.card H * Nat.card K) #check Nat.bijective_iff_injective_and_card #check ker_eq_bot_iff #check restrict #check ker_restrict def iso₁ : K ≃* G ⧸ H := by sorry def iso₂ : G ≃* (G ⧸ K) × (G ⧸ H) := by sorry #check MulEquiv.prodCongr def finalIso : G ≃* H × K := sorry
mathematics_in_lean/MIL/C09_Groups_and_Rings/S02_Rings.lean
import Mathlib.RingTheory.Ideal.Quotient.Operations import Mathlib.RingTheory.Localization.Basic import Mathlib.RingTheory.DedekindDomain.Ideal import Mathlib.Analysis.Complex.Polynomial.Basic import Mathlib.Data.ZMod.QuotientRing import MIL.Common noncomputable section example {R : Type*} [CommRing R] (x y : R) : (x + y) ^ 2 = x ^ 2 + y ^ 2 + 2 * x * y := by ring example (x y : ℕ) : (x + y) ^ 2 = x ^ 2 + y ^ 2 + 2 * x * y := by ring example (x : ℤˣ) : x = 1 ∨ x = -1 := Int.units_eq_one_or x example {M : Type*} [Monoid M] (x : Mˣ) : (x : M) * x⁻¹ = 1 := Units.mul_inv x example {M : Type*} [Monoid M] : Group Mˣ := inferInstance example {R S : Type*} [Ring R] [Ring S] (f : R →+* S) (x y : R) : f (x + y) = f x + f y := f.map_add x y example {R S : Type*} [Ring R] [Ring S] (f : R →+* S) : Rˣ →* Sˣ := Units.map f example {R : Type*} [Ring R] (S : Subring R) : Ring S := inferInstance example {R : Type*} [CommRing R] (I : Ideal R) : R →+* R ⧸ I := Ideal.Quotient.mk I example {R : Type*} [CommRing R] {a : R} {I : Ideal R} : Ideal.Quotient.mk I a = 0 ↔ a ∈ I := Ideal.Quotient.eq_zero_iff_mem example {R S : Type*} [CommRing R] [CommRing S] (I : Ideal R) (f : R →+* S) (H : I ≤ RingHom.ker f) : R ⧸ I →+* S := Ideal.Quotient.lift I f H example {R S : Type*} [CommRing R] [CommRing S](f : R →+* S) : R ⧸ RingHom.ker f ≃+* f.range := RingHom.quotientKerEquivRange f section variable {R : Type*} [CommRing R] {I J : Ideal R} example : I + J = I ⊔ J := rfl example {x : R} : x ∈ I + J ↔ ∃ a ∈ I, ∃ b ∈ J, a + b = x := by simp [Submodule.mem_sup] example : I * J ≤ J := Ideal.mul_le_left example : I * J ≤ I := Ideal.mul_le_right example : I * J ≤ I ⊓ J := Ideal.mul_le_inf end example {R S : Type*} [CommRing R] [CommRing S] (I : Ideal R) (J : Ideal S) (f : R →+* S) (H : I ≤ Ideal.comap f J) : R ⧸ I →+* S ⧸ J := Ideal.quotientMap J f H example {R : Type*} [CommRing R] {I J : Ideal R} (h : I = J) : R ⧸ I ≃+* R ⧸ J := Ideal.quotEquivOfEq h example {R : Type*} [CommRing R] {ι : Type*} [Fintype ι] (f : ι → Ideal R) (hf : ∀ i j, i ≠ j → IsCoprime (f i) (f j)) : (R ⧸ ⨅ i, f i) ≃+* Π i, R ⧸ f i := Ideal.quotientInfRingEquivPiQuotient f hf open BigOperators PiNotation example {ι : Type*} [Fintype ι] (a : ι → ℕ) (coprime : ∀ i j, i ≠ j → (a i).Coprime (a j)) : ZMod (∏ i, a i) ≃+* Π i, ZMod (a i) := ZMod.prodEquivPi a coprime section variable {ι R : Type*} [CommRing R] open Ideal Quotient Function #check Pi.ringHom #check ker_Pi_Quotient_mk /-- The homomorphism from ``R ⧸ ⨅ i, I i`` to ``Π i, R ⧸ I i`` featured in the Chinese Remainder Theorem. -/ def chineseMap (I : ι → Ideal R) : (R ⧸ ⨅ i, I i) →+* Π i, R ⧸ I i := sorry lemma chineseMap_mk (I : ι → Ideal R) (x : R) : chineseMap I (Quotient.mk _ x) = fun i : ι ↦ Ideal.Quotient.mk (I i) x := sorry lemma chineseMap_mk' (I : ι → Ideal R) (x : R) (i : ι) : chineseMap I (mk _ x) i = mk (I i) x := sorry #check injective_lift_iff lemma chineseMap_inj (I : ι → Ideal R) : Injective (chineseMap I) := by sorry #check IsCoprime #check isCoprime_iff_add #check isCoprime_iff_exists #check isCoprime_iff_sup_eq #check isCoprime_iff_codisjoint #check Finset.mem_insert_of_mem #check Finset.mem_insert_self theorem isCoprime_Inf {I : Ideal R} {J : ι → Ideal R} {s : Finset ι} (hf : ∀ j ∈ s, IsCoprime I (J j)) : IsCoprime I (⨅ j ∈ s, J j) := by classical simp_rw [isCoprime_iff_add] at * induction s using Finset.induction with | empty => simp | @insert i s _ hs => rw [Finset.iInf_insert, inf_comm, one_eq_top, eq_top_iff, ← one_eq_top] set K := ⨅ j ∈ s, J j calc 1 = I + K := sorry _ = I + K * (I + J i) := sorry _ = (1 + K) * I + K * J i := sorry _ ≤ I + K ⊓ J i := sorry lemma chineseMap_surj [Fintype ι] {I : ι → Ideal R} (hI : ∀ i j, i ≠ j → IsCoprime (I i) (I j)) : Surjective (chineseMap I) := by classical intro g choose f hf using fun i ↦ Ideal.Quotient.mk_surjective (g i) have key : ∀ i, ∃ e : R, mk (I i) e = 1 ∧ ∀ j, j ≠ i → mk (I j) e = 0 := by intro i have hI' : ∀ j ∈ ({i} : Finset ι)ᶜ, IsCoprime (I i) (I j) := by sorry sorry choose e he using key use mk _ (∑ i, f i * e i) sorry noncomputable def chineseIso [Fintype ι] (f : ι → Ideal R) (hf : ∀ i j, i ≠ j → IsCoprime (f i) (f j)) : (R ⧸ ⨅ i, f i) ≃+* Π i, R ⧸ f i := { Equiv.ofBijective _ ⟨chineseMap_inj f, chineseMap_surj hf⟩, chineseMap f with } end example {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (r r' : R) (a : A) : (r + r') • a = r • a + r' • a := add_smul r r' a example {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (r r' : R) (a : A) : (r * r') • a = r • r' • a := mul_smul r r' a section Polynomials open Polynomial example {R : Type*} [CommRing R] : R[X] := X example {R : Type*} [CommRing R] (r : R) := X - C r example {R : Type*} [CommRing R] (r : R) : (X + C r) * (X - C r) = X ^ 2 - C (r ^ 2) := by rw [C.map_pow] ring example {R : Type*} [CommRing R] (r:R) : (C r).coeff 0 = r := by simp example {R : Type*} [CommRing R] : (X ^ 2 + 2 * X + C 3 : R[X]).coeff 1 = 2 := by simp example {R : Type*} [Semiring R] [NoZeroDivisors R] {p q : R[X]} : degree (p * q) = degree p + degree q := Polynomial.degree_mul example {R : Type*} [Semiring R] [NoZeroDivisors R] {p q : R[X]} (hp : p ≠ 0) (hq : q ≠ 0) : natDegree (p * q) = natDegree p + natDegree q := Polynomial.natDegree_mul hp hq example {R : Type*} [Semiring R] [NoZeroDivisors R] {p q : R[X]} : natDegree (comp p q) = natDegree p * natDegree q := Polynomial.natDegree_comp example {R : Type*} [CommRing R] (P: R[X]) (x : R) := P.eval x example {R : Type*} [CommRing R] (r : R) : (X - C r).eval r = 0 := by simp example {R : Type*} [CommRing R] (P : R[X]) (r : R) : IsRoot P r ↔ P.eval r = 0 := Iff.rfl example {R : Type*} [CommRing R] [IsDomain R] (r : R) : (X - C r).roots = {r} := roots_X_sub_C r example {R : Type*} [CommRing R] [IsDomain R] (r : R) (n : ℕ): ((X - C r) ^ n).roots = n • {r} := by simp example : aeval Complex.I (X ^ 2 + 1 : ℝ[X]) = 0 := by simp open Complex Polynomial example : aroots (X ^ 2 + 1 : ℝ[X]) ℂ = {Complex.I, -I} := by suffices roots (X ^ 2 + 1 : ℂ[X]) = {I, -I} by simpa [aroots_def] have factored : (X ^ 2 + 1 : ℂ[X]) = (X - C I) * (X - C (-I)) := by have key : (C I * C I : ℂ[X]) = -1 := by simp [← C_mul] rw [C_neg] linear_combination key have p_ne_zero : (X - C I) * (X - C (-I)) ≠ 0 := by intro H apply_fun eval 0 at H simp [eval] at H simp only [factored, roots_mul p_ne_zero, roots_X_sub_C] rfl -- Mathlib knows about D'Alembert-Gauss theorem: ``ℂ`` is algebraically closed. example : IsAlgClosed ℂ := inferInstance #check (Complex.ofRealHom : ℝ →+* ℂ) example : (X ^ 2 + 1 : ℝ[X]).eval₂ Complex.ofRealHom Complex.I = 0 := by simp open MvPolynomial def circleEquation : MvPolynomial (Fin 2) ℝ := X 0 ^ 2 + X 1 ^ 2 - 1 example : MvPolynomial.eval ![1, 0] circleEquation = 0 := by simp [circleEquation] end Polynomials
mathematics_in_lean/MIL/C09_Groups_and_Rings/solutions/Solutions_S01_Groups.lean
import Mathlib.GroupTheory.Sylow import Mathlib.GroupTheory.Perm.Cycle.Concrete import Mathlib.GroupTheory.Perm.Subgroup import Mathlib.GroupTheory.PresentedGroup import MIL.Common def conjugate {G : Type*} [Group G] (x : G) (H : Subgroup G) : Subgroup G where carrier := {a : G | ∃ h, h ∈ H ∧ a = x * h * x⁻¹} one_mem' := by dsimp use 1 constructor exact H.one_mem group inv_mem' := by dsimp rintro - ⟨h, h_in, rfl⟩ use h⁻¹, H.inv_mem h_in group mul_mem' := by dsimp rintro - - ⟨h, h_in, rfl⟩ ⟨k, k_in, rfl⟩ use h*k, H.mul_mem h_in k_in group section exercises variable {G H : Type*} [Group G] [Group H] open Subgroup example (φ : G →* H) (S T : Subgroup H) (hST : S ≤ T) : comap φ S ≤ comap φ T := by intro x hx rw [mem_comap] at * -- Lean does not need this line exact hST hx example (φ : G →* H) (S T : Subgroup G) (hST : S ≤ T) : map φ S ≤ map φ T := by intro x hx rw [mem_map] at * -- Lean does not need this line rcases hx with ⟨y, hy, rfl⟩ use y, hST hy variable {K : Type*} [Group K] -- Remember you can use the `ext` tactic to prove an equality of subgroups. example (φ : G →* H) (ψ : H →* K) (U : Subgroup K) : comap (ψ.comp φ) U = comap φ (comap ψ U) := by -- The whole proof could be ``rfl``, but let's decompose it a bit. ext x simp only [mem_comap] rfl -- Pushing a subgroup along one homomorphism and then another is equal to -- pushing it forward along the composite of the homomorphisms. example (φ : G →* H) (ψ : H →* K) (S : Subgroup G) : map (ψ.comp φ) S = map ψ (S.map φ) := by ext x simp only [mem_map] constructor · rintro ⟨y, y_in, hy⟩ exact ⟨φ y, ⟨y, y_in, rfl⟩, hy⟩ · rintro ⟨y, ⟨z, z_in, hz⟩, hy⟩ use z, z_in calc ψ.comp φ z = ψ (φ z) := rfl _ = ψ y := by congr _ = x := hy end exercises open scoped Classical open Subgroup lemma eq_bot_iff_card {G : Type*} [Group G] {H : Subgroup G} : H = ⊥ ↔ Nat.card H = 1 := by suffices (∀ x ∈ H, x = 1) ↔ ∃ x ∈ H, ∀ a ∈ H, a = x by simpa [eq_bot_iff_forall, Nat.card_eq_one_iff_exists] constructor · intro h use 1, H.one_mem · rintro ⟨y, -, hy'⟩ x hx calc x = y := hy' x hx _ = 1 := (hy' 1 H.one_mem).symm lemma inf_bot_of_coprime {G : Type*} [Group G] (H K : Subgroup G) (h : (Nat.card H).Coprime (Nat.card K)) : H ⊓ K = ⊥ := by have D₁ : Nat.card (H ⊓ K : Subgroup G) ∣ Nat.card H := card_dvd_of_le inf_le_left have D₂ : Nat.card (H ⊓ K : Subgroup G) ∣ Nat.card K := card_dvd_of_le inf_le_right exact eq_bot_iff_card.2 (Nat.eq_one_of_dvd_coprimes h D₁ D₂) noncomputable section GroupActions variable {G : Type*} [Group G] lemma conjugate_one (H : Subgroup G) : conjugate 1 H = H := by ext x simp [conjugate] instance : MulAction G (Subgroup G) where smul := conjugate one_smul := by exact conjugate_one mul_smul := by intro x y H ext z constructor · rintro ⟨h, h_in, rfl⟩ use y*h*y⁻¹ constructor · use h · group · rintro ⟨-, ⟨h, h_in, rfl⟩, rfl⟩ use h, h_in group end GroupActions noncomputable section QuotientGroup section variable {G : Type*} [Group G] {H K : Subgroup G} open MonoidHom #check Nat.card_pos -- The nonempty argument will be automatically inferred for subgroups #check Subgroup.index_eq_card #check Subgroup.index_mul_card #check Nat.eq_of_mul_eq_mul_right lemma aux_card_eq [Finite G] (h' : Nat.card G = Nat.card H * Nat.card K) : Nat.card (G ⧸ H) = Nat.card K := by have := calc Nat.card (G ⧸ H) * Nat.card H = Nat.card G := by rw [← H.index_eq_card, H.index_mul_card] _ = Nat.card K * Nat.card H := by rw [h', mul_comm] exact Nat.eq_of_mul_eq_mul_right Nat.card_pos this variable [H.Normal] [K.Normal] [Fintype G] (h : Disjoint H K) (h' : Nat.card G = Nat.card H * Nat.card K) #check Nat.bijective_iff_injective_and_card #check ker_eq_bot_iff #check restrict #check ker_restrict def iso₁ : K ≃* G ⧸ H := by apply MulEquiv.ofBijective ((QuotientGroup.mk' H).restrict K) rw [Nat.bijective_iff_injective_and_card] constructor · rw [← ker_eq_bot_iff, (QuotientGroup.mk' H).ker_restrict K] simp [h] · symm exact aux_card_eq h' def iso₂ : G ≃* (G ⧸ K) × (G ⧸ H) := by apply MulEquiv.ofBijective <| (QuotientGroup.mk' K).prod (QuotientGroup.mk' H) rw [Nat.bijective_iff_injective_and_card] constructor · rw [← ker_eq_bot_iff, ker_prod] simp [h.symm.eq_bot] · rw [Nat.card_prod] rw [aux_card_eq h', aux_card_eq (mul_comm (Nat.card H) _▸ h'), h'] def finalIso : G ≃* H × K := (iso₂ h h').trans ((iso₁ h.symm (mul_comm (Nat.card H) _ ▸ h')).prodCongr (iso₁ h h')).symm end end QuotientGroup
mathematics_in_lean/MIL/C09_Groups_and_Rings/solutions/Solutions_S02_Rings.lean
import Mathlib.RingTheory.Ideal.Quotient.Operations import Mathlib.RingTheory.Localization.Basic import Mathlib.RingTheory.DedekindDomain.Ideal import Mathlib.Analysis.Complex.Polynomial.Basic import Mathlib.Data.ZMod.QuotientRing import MIL.Common noncomputable section open BigOperators PiNotation section variable {ι R : Type*} [CommRing R] open Ideal Quotient Function #check Pi.ringHom #check ker_Pi_Quotient_mk /-- The homomorphism from ``R ⧸ ⨅ i, I i`` to ``Π i, R ⧸ I i`` featured in the Chinese Remainder Theorem. -/ def chineseMap (I : ι → Ideal R) : (R ⧸ ⨅ i, I i) →+* Π i, R ⧸ I i := Ideal.Quotient.lift (⨅ i, I i) (Pi.ringHom fun i : ι ↦ Ideal.Quotient.mk (I i)) (by simp [← RingHom.mem_ker, ker_Pi_Quotient_mk]) lemma chineseMap_mk (I : ι → Ideal R) (x : R) : chineseMap I (Quotient.mk _ x) = fun i : ι ↦ Ideal.Quotient.mk (I i) x := rfl lemma chineseMap_mk' (I : ι → Ideal R) (x : R) (i : ι) : chineseMap I (mk _ x) i = mk (I i) x := rfl lemma chineseMap_inj (I : ι → Ideal R) : Injective (chineseMap I) := by rw [chineseMap, injective_lift_iff, ker_Pi_Quotient_mk] theorem isCoprime_Inf {I : Ideal R} {J : ι → Ideal R} {s : Finset ι} (hf : ∀ j ∈ s, IsCoprime I (J j)) : IsCoprime I (⨅ j ∈ s, J j) := by classical simp_rw [isCoprime_iff_add] at * induction s using Finset.induction with | empty => simp | @insert i s _ hs => rw [Finset.iInf_insert, inf_comm, one_eq_top, eq_top_iff, ← one_eq_top] set K := ⨅ j ∈ s, J j calc 1 = I + K := (hs fun j hj ↦ hf j (Finset.mem_insert_of_mem hj)).symm _ = I + K * (I + J i) := by rw [hf i (Finset.mem_insert_self i s), mul_one] _ = (1 + K) * I + K * J i := by ring _ ≤ I + K ⊓ J i := by gcongr ; apply mul_le_left ; apply mul_le_inf lemma chineseMap_surj [Fintype ι] {I : ι → Ideal R} (hI : ∀ i j, i ≠ j → IsCoprime (I i) (I j)) : Surjective (chineseMap I) := by classical intro g choose f hf using fun i ↦ Ideal.Quotient.mk_surjective (g i) have key : ∀ i, ∃ e : R, mk (I i) e = 1 ∧ ∀ j, j ≠ i → mk (I j) e = 0 := by intro i have hI' : ∀ j ∈ ({i} : Finset ι)ᶜ, IsCoprime (I i) (I j) := by intros j hj exact hI _ _ (by simpa [ne_comm, isCoprime_iff_add] using hj) rcases isCoprime_iff_exists.mp (isCoprime_Inf hI') with ⟨u, hu, e, he, hue⟩ replace he : ∀ j, j ≠ i → e ∈ I j := by simpa using he refine ⟨e, ?_, ?_⟩ · simp [eq_sub_of_add_eq' hue, map_sub, eq_zero_iff_mem.mpr hu] · exact fun j hj ↦ eq_zero_iff_mem.mpr (he j hj) choose e he using key use mk _ (∑ i, f i * e i) ext i rw [chineseMap_mk', map_sum, Fintype.sum_eq_single i] · simp [(he i).1, hf] · intros j hj simp [(he j).2 i hj.symm] noncomputable def chineseIso [Fintype ι] (f : ι → Ideal R) (hf : ∀ i j, i ≠ j → IsCoprime (f i) (f j)) : (R ⧸ ⨅ i, f i) ≃+* Π i, R ⧸ f i := { Equiv.ofBijective _ ⟨chineseMap_inj f, chineseMap_surj hf⟩, chineseMap f with } end
mathematics_in_lean/MIL/C02_Basics/S04_More_on_Order_and_Divisibility.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C02S04 section variable (a b c d : ℝ) #check (min_le_left a b : min a b ≤ a) #check (min_le_right a b : min a b ≤ b) #check (le_min : c ≤ a → c ≤ b → c ≤ min a b) example : min a b = min b a := by apply le_antisymm · show min a b ≤ min b a apply le_min · apply min_le_right apply min_le_left · show min b a ≤ min a b apply le_min · apply min_le_right apply min_le_left example : min a b = min b a := by have h : ∀ x y : ℝ, min x y ≤ min y x := by intro x y apply le_min apply min_le_right apply min_le_left apply le_antisymm apply h apply h example : min a b = min b a := by apply le_antisymm repeat apply le_min apply min_le_right apply min_le_left example : max a b = max b a := by sorry example : min (min a b) c = min a (min b c) := by sorry theorem aux : min a b + c ≤ min (a + c) (b + c) := by sorry example : min a b + c = min (a + c) (b + c) := by sorry #check (abs_add : ∀ a b : ℝ, |a + b| ≤ |a| + |b|) example : |a| - |b| ≤ |a - b| := sorry end section variable (w x y z : ℕ) example (h₀ : x ∣ y) (h₁ : y ∣ z) : x ∣ z := dvd_trans h₀ h₁ example : x ∣ y * x * z := by apply dvd_mul_of_dvd_left apply dvd_mul_left example : x ∣ x ^ 2 := by apply dvd_mul_left example (h : x ∣ w) : x ∣ y * (x * z) + x ^ 2 + w ^ 2 := by sorry end section variable (m n : ℕ) #check (Nat.gcd_zero_right n : Nat.gcd n 0 = n) #check (Nat.gcd_zero_left n : Nat.gcd 0 n = n) #check (Nat.lcm_zero_right n : Nat.lcm n 0 = 0) #check (Nat.lcm_zero_left n : Nat.lcm 0 n = 0) example : Nat.gcd m n = Nat.gcd n m := by sorry end
mathematics_in_lean/MIL/C02_Basics/S01_Calculating.lean
import MIL.Common import Mathlib.Data.Real.Basic -- An example. example (a b c : ℝ) : a * b * c = b * (a * c) := by rw [mul_comm a b] rw [mul_assoc b a c] -- Try these. example (a b c : ℝ) : c * b * a = b * (a * c) := by sorry example (a b c : ℝ) : a * (b * c) = b * (a * c) := by sorry -- An example. example (a b c : ℝ) : a * b * c = b * c * a := by rw [mul_assoc] rw [mul_comm] /- Try doing the first of these without providing any arguments at all, and the second with only one argument. -/ example (a b c : ℝ) : a * (b * c) = b * (c * a) := by sorry example (a b c : ℝ) : a * (b * c) = b * (a * c) := by sorry -- Using facts from the local context. example (a b c d e f : ℝ) (h : a * b = c * d) (h' : e = f) : a * (b * e) = c * (d * f) := by rw [h'] rw [← mul_assoc] rw [h] rw [mul_assoc] example (a b c d e f : ℝ) (h : b * c = e * f) : a * b * c * d = a * e * f * d := by sorry example (a b c d : ℝ) (hyp : c = b * a - d) (hyp' : d = a * b) : c = 0 := by sorry example (a b c d e f : ℝ) (h : a * b = c * d) (h' : e = f) : a * (b * e) = c * (d * f) := by rw [h', ← mul_assoc, h, mul_assoc] section variable (a b c d e f : ℝ) example (h : a * b = c * d) (h' : e = f) : a * (b * e) = c * (d * f) := by rw [h', ← mul_assoc, h, mul_assoc] end section variable (a b c : ℝ) #check a #check a + b #check (a : ℝ) #check mul_comm a b #check (mul_comm a b : a * b = b * a) #check mul_assoc c a b #check mul_comm a #check mul_comm end section variable (a b : ℝ) example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := by rw [mul_add, add_mul, add_mul] rw [← add_assoc, add_assoc (a * a)] rw [mul_comm b a, ← two_mul] example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := calc (a + b) * (a + b) = a * a + b * a + (a * b + b * b) := by rw [mul_add, add_mul, add_mul] _ = a * a + (b * a + a * b) + b * b := by rw [← add_assoc, add_assoc (a * a)] _ = a * a + 2 * (a * b) + b * b := by rw [mul_comm b a, ← two_mul] example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := calc (a + b) * (a + b) = a * a + b * a + (a * b + b * b) := by sorry _ = a * a + (b * a + a * b) + b * b := by sorry _ = a * a + 2 * (a * b) + b * b := by sorry end -- Try these. For the second, use the theorems listed underneath. section variable (a b c d : ℝ) example : (a + b) * (c + d) = a * c + a * d + b * c + b * d := by sorry example (a b : ℝ) : (a + b) * (a - b) = a ^ 2 - b ^ 2 := by sorry #check pow_two a #check mul_sub a b c #check add_mul a b c #check add_sub a b c #check sub_sub a b c #check add_zero a end -- Examples. section variable (a b c d : ℝ) example (a b c d : ℝ) (hyp : c = d * a + b) (hyp' : b = a * d) : c = 2 * a * d := by rw [hyp'] at hyp rw [mul_comm d a] at hyp rw [← two_mul (a * d)] at hyp rw [← mul_assoc 2 a d] at hyp exact hyp example : c * b * a = b * (a * c) := by ring example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := by ring example : (a + b) * (a - b) = a ^ 2 - b ^ 2 := by ring example (hyp : c = d * a + b) (hyp' : b = a * d) : c = 2 * a * d := by rw [hyp, hyp'] ring end example (a b c : ℕ) (h : a + b = c) : (a + b) * (a + b) = a * c + b * c := by nth_rw 2 [h] rw [add_mul]
mathematics_in_lean/MIL/C02_Basics/S03_Using_Theorems_and_Lemmas.lean
import Mathlib.Analysis.SpecialFunctions.Log.Basic import MIL.Common variable (a b c d e : ℝ) open Real #check (le_refl : ∀ a : ℝ, a ≤ a) #check (le_trans : a ≤ b → b ≤ c → a ≤ c) section variable (h : a ≤ b) (h' : b ≤ c) #check (le_refl : ∀ a : Real, a ≤ a) #check (le_refl a : a ≤ a) #check (le_trans : a ≤ b → b ≤ c → a ≤ c) #check (le_trans h : b ≤ c → a ≤ c) #check (le_trans h h' : a ≤ c) end example (x y z : ℝ) (h₀ : x ≤ y) (h₁ : y ≤ z) : x ≤ z := by apply le_trans · apply h₀ · apply h₁ example (x y z : ℝ) (h₀ : x ≤ y) (h₁ : y ≤ z) : x ≤ z := by apply le_trans h₀ apply h₁ example (x y z : ℝ) (h₀ : x ≤ y) (h₁ : y ≤ z) : x ≤ z := le_trans h₀ h₁ example (x : ℝ) : x ≤ x := by apply le_refl example (x : ℝ) : x ≤ x := le_refl x #check (le_refl : ∀ a, a ≤ a) #check (le_trans : a ≤ b → b ≤ c → a ≤ c) #check (lt_of_le_of_lt : a ≤ b → b < c → a < c) #check (lt_of_lt_of_le : a < b → b ≤ c → a < c) #check (lt_trans : a < b → b < c → a < c) -- Try this. example (h₀ : a ≤ b) (h₁ : b < c) (h₂ : c ≤ d) (h₃ : d < e) : a < e := by sorry example (h₀ : a ≤ b) (h₁ : b < c) (h₂ : c ≤ d) (h₃ : d < e) : a < e := by linarith section example (h : 2 * a ≤ 3 * b) (h' : 1 ≤ a) (h'' : d = 2) : d + a ≤ 5 * b := by linarith end example (h : 1 ≤ a) (h' : b ≤ c) : 2 + a + exp b ≤ 3 * a + exp c := by linarith [exp_le_exp.mpr h'] #check (exp_le_exp : exp a ≤ exp b ↔ a ≤ b) #check (exp_lt_exp : exp a < exp b ↔ a < b) #check (log_le_log : 0 < a → a ≤ b → log a ≤ log b) #check (log_lt_log : 0 < a → a < b → log a < log b) #check (add_le_add : a ≤ b → c ≤ d → a + c ≤ b + d) #check (add_le_add_left : a ≤ b → ∀ c, c + a ≤ c + b) #check (add_le_add_right : a ≤ b → ∀ c, a + c ≤ b + c) #check (add_lt_add_of_le_of_lt : a ≤ b → c < d → a + c < b + d) #check (add_lt_add_of_lt_of_le : a < b → c ≤ d → a + c < b + d) #check (add_lt_add_left : a < b → ∀ c, c + a < c + b) #check (add_lt_add_right : a < b → ∀ c, a + c < b + c) #check (add_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a + b) #check (add_pos : 0 < a → 0 < b → 0 < a + b) #check (add_pos_of_pos_of_nonneg : 0 < a → 0 ≤ b → 0 < a + b) #check (exp_pos : ∀ a, 0 < exp a) #check add_le_add_left example (h : a ≤ b) : exp a ≤ exp b := by rw [exp_le_exp] exact h example (h₀ : a ≤ b) (h₁ : c < d) : a + exp c + e < b + exp d + e := by apply add_lt_add_of_lt_of_le · apply add_lt_add_of_le_of_lt h₀ apply exp_lt_exp.mpr h₁ apply le_refl example (h₀ : d ≤ e) : c + exp (a + d) ≤ c + exp (a + e) := by sorry example : (0 : ℝ) < 1 := by norm_num example (h : a ≤ b) : log (1 + exp a) ≤ log (1 + exp b) := by have h₀ : 0 < 1 + exp a := by sorry apply log_le_log h₀ sorry example : 0 ≤ a ^ 2 := by -- apply? exact sq_nonneg a example (h : a ≤ b) : c - exp b ≤ c - exp a := by sorry example : 2*a*b ≤ a^2 + b^2 := by have h : 0 ≤ a^2 - 2*a*b + b^2 calc a^2 - 2*a*b + b^2 = (a - b)^2 := by ring _ ≥ 0 := by apply pow_two_nonneg calc 2*a*b = 2*a*b + 0 := by ring _ ≤ 2*a*b + (a^2 - 2*a*b + b^2) := add_le_add (le_refl _) h _ = a^2 + b^2 := by ring example : 2*a*b ≤ a^2 + b^2 := by have h : 0 ≤ a^2 - 2*a*b + b^2 calc a^2 - 2*a*b + b^2 = (a - b)^2 := by ring _ ≥ 0 := by apply pow_two_nonneg linarith example : |a*b| ≤ (a^2 + b^2)/2 := by sorry #check abs_le'.mpr
mathematics_in_lean/MIL/C02_Basics/S02_Proving_Identities_in_Algebraic_Structures.lean
import Mathlib.Algebra.Ring.Defs import Mathlib.Data.Real.Basic import MIL.Common section variable (R : Type*) [Ring R] #check (add_assoc : ∀ a b c : R, a + b + c = a + (b + c)) #check (add_comm : ∀ a b : R, a + b = b + a) #check (zero_add : ∀ a : R, 0 + a = a) #check (neg_add_cancel : ∀ a : R, -a + a = 0) #check (mul_assoc : ∀ a b c : R, a * b * c = a * (b * c)) #check (mul_one : ∀ a : R, a * 1 = a) #check (one_mul : ∀ a : R, 1 * a = a) #check (mul_add : ∀ a b c : R, a * (b + c) = a * b + a * c) #check (add_mul : ∀ a b c : R, (a + b) * c = a * c + b * c) end section variable (R : Type*) [CommRing R] variable (a b c d : R) example : c * b * a = b * (a * c) := by ring example : (a + b) * (a + b) = a * a + 2 * (a * b) + b * b := by ring example : (a + b) * (a - b) = a ^ 2 - b ^ 2 := by ring example (hyp : c = d * a + b) (hyp' : b = a * d) : c = 2 * a * d := by rw [hyp, hyp'] ring end namespace MyRing variable {R : Type*} [Ring R] theorem add_zero (a : R) : a + 0 = a := by rw [add_comm, zero_add] theorem add_neg_cancel (a : R) : a + -a = 0 := by rw [add_comm, neg_add_cancel] #check MyRing.add_zero #check add_zero end MyRing namespace MyRing variable {R : Type*} [Ring R] theorem neg_add_cancel_left (a b : R) : -a + (a + b) = b := by rw [← add_assoc, neg_add_cancel, zero_add] -- Prove these: theorem add_neg_cancel_right (a b : R) : a + b + -b = a := by sorry theorem add_left_cancel {a b c : R} (h : a + b = a + c) : b = c := by sorry theorem add_right_cancel {a b c : R} (h : a + b = c + b) : a = c := by sorry theorem mul_zero (a : R) : a * 0 = 0 := by have h : a * 0 + a * 0 = a * 0 + 0 := by rw [← mul_add, add_zero, add_zero] rw [add_left_cancel h] theorem zero_mul (a : R) : 0 * a = 0 := by sorry theorem neg_eq_of_add_eq_zero {a b : R} (h : a + b = 0) : -a = b := by sorry theorem eq_neg_of_add_eq_zero {a b : R} (h : a + b = 0) : a = -b := by sorry theorem neg_zero : (-0 : R) = 0 := by apply neg_eq_of_add_eq_zero rw [add_zero] theorem neg_neg (a : R) : - -a = a := by sorry end MyRing -- Examples. section variable {R : Type*} [Ring R] example (a b : R) : a - b = a + -b := sub_eq_add_neg a b end example (a b : ℝ) : a - b = a + -b := rfl example (a b : ℝ) : a - b = a + -b := by rfl namespace MyRing variable {R : Type*} [Ring R] theorem self_sub (a : R) : a - a = 0 := by sorry theorem one_add_one_eq_two : 1 + 1 = (2 : R) := by norm_num theorem two_mul (a : R) : 2 * a = a + a := by sorry end MyRing section variable (A : Type*) [AddGroup A] #check (add_assoc : ∀ a b c : A, a + b + c = a + (b + c)) #check (zero_add : ∀ a : A, 0 + a = a) #check (neg_add_cancel : ∀ a : A, -a + a = 0) end section variable {G : Type*} [Group G] #check (mul_assoc : ∀ a b c : G, a * b * c = a * (b * c)) #check (one_mul : ∀ a : G, 1 * a = a) #check (inv_mul_cancel : ∀ a : G, a⁻¹ * a = 1) namespace MyGroup theorem mul_inv_cancel (a : G) : a * a⁻¹ = 1 := by sorry theorem mul_one (a : G) : a * 1 = a := by sorry theorem mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by sorry end MyGroup end
mathematics_in_lean/MIL/C02_Basics/S05_Proving_Facts_about_Algebraic_Structures.lean
import MIL.Common import Mathlib.Topology.MetricSpace.Basic section variable {α : Type*} [PartialOrder α] variable (x y z : α) #check x ≤ y #check (le_refl x : x ≤ x) #check (le_trans : x ≤ y → y ≤ z → x ≤ z) #check (le_antisymm : x ≤ y → y ≤ x → x = y) #check x < y #check (lt_irrefl x : ¬ (x < x)) #check (lt_trans : x < y → y < z → x < z) #check (lt_of_le_of_lt : x ≤ y → y < z → x < z) #check (lt_of_lt_of_le : x < y → y ≤ z → x < z) example : x < y ↔ x ≤ y ∧ x ≠ y := lt_iff_le_and_ne end section variable {α : Type*} [Lattice α] variable (x y z : α) #check x ⊓ y #check (inf_le_left : x ⊓ y ≤ x) #check (inf_le_right : x ⊓ y ≤ y) #check (le_inf : z ≤ x → z ≤ y → z ≤ x ⊓ y) #check x ⊔ y #check (le_sup_left : x ≤ x ⊔ y) #check (le_sup_right : y ≤ x ⊔ y) #check (sup_le : x ≤ z → y ≤ z → x ⊔ y ≤ z) example : x ⊓ y = y ⊓ x := by sorry example : x ⊓ y ⊓ z = x ⊓ (y ⊓ z) := by sorry example : x ⊔ y = y ⊔ x := by sorry example : x ⊔ y ⊔ z = x ⊔ (y ⊔ z) := by sorry theorem absorb1 : x ⊓ (x ⊔ y) = x := by sorry theorem absorb2 : x ⊔ x ⊓ y = x := by sorry end section variable {α : Type*} [DistribLattice α] variable (x y z : α) #check (inf_sup_left x y z : x ⊓ (y ⊔ z) = x ⊓ y ⊔ x ⊓ z) #check (inf_sup_right x y z : (x ⊔ y) ⊓ z = x ⊓ z ⊔ y ⊓ z) #check (sup_inf_left x y z : x ⊔ y ⊓ z = (x ⊔ y) ⊓ (x ⊔ z)) #check (sup_inf_right x y z : x ⊓ y ⊔ z = (x ⊔ z) ⊓ (y ⊔ z)) end section variable {α : Type*} [Lattice α] variable (a b c : α) example (h : ∀ x y z : α, x ⊓ (y ⊔ z) = x ⊓ y ⊔ x ⊓ z) : a ⊔ b ⊓ c = (a ⊔ b) ⊓ (a ⊔ c) := by sorry example (h : ∀ x y z : α, x ⊔ y ⊓ z = (x ⊔ y) ⊓ (x ⊔ z)) : a ⊓ (b ⊔ c) = a ⊓ b ⊔ a ⊓ c := by sorry end section variable {R : Type*} [Ring R] [PartialOrder R] [IsStrictOrderedRing R] variable (a b c : R) #check (add_le_add_left : a ≤ b → ∀ c, c + a ≤ c + b) #check (mul_pos : 0 < a → 0 < b → 0 < a * b) #check (mul_nonneg : 0 ≤ a → 0 ≤ b → 0 ≤ a * b) example (h : a ≤ b) : 0 ≤ b - a := by sorry example (h: 0 ≤ b - a) : a ≤ b := by sorry example (h : a ≤ b) (h' : 0 ≤ c) : a * c ≤ b * c := by sorry end section variable {X : Type*} [MetricSpace X] variable (x y z : X) #check (dist_self x : dist x x = 0) #check (dist_comm x y : dist x y = dist y x) #check (dist_triangle x y z : dist x z ≤ dist x y + dist y z) example (x y : X) : 0 ≤ dist x y := by sorry end
mathematics_in_lean/MIL/C02_Basics/solutions/Solutions_S03_Using_Theorems_and_Lemmas.lean
import Mathlib.Analysis.SpecialFunctions.Log.Basic import MIL.Common variable (a b c d e : ℝ) open Real example (h₀ : a ≤ b) (h₁ : b < c) (h₂ : c ≤ d) (h₃ : d < e) : a < e := by apply lt_of_le_of_lt h₀ apply lt_trans h₁ exact lt_of_le_of_lt h₂ h₃ example (h₀ : d ≤ e) : c + exp (a + d) ≤ c + exp (a + e) := by apply add_le_add_left rw [exp_le_exp] apply add_le_add_left h₀ -- an alternative using `linarith`. example (h₀ : d ≤ e) : c + exp (a + d) ≤ c + exp (a + e) := by have : exp (a + d) ≤ exp (a + e) := by rw [exp_le_exp] linarith linarith [this] example (h : a ≤ b) : log (1 + exp a) ≤ log (1 + exp b) := by have h₀ : 0 < 1 + exp a := by linarith [exp_pos a] apply log_le_log h₀ apply add_le_add_left (exp_le_exp.mpr h) -- SOLUTION. example (h : a ≤ b) : c - exp b ≤ c - exp a := by apply sub_le_sub_left exact exp_le_exp.mpr h -- alternatively: example (h : a ≤ b) : c - exp b ≤ c - exp a := by linarith [exp_le_exp.mpr h] theorem fact1 : a*b*2 ≤ a^2 + b^2 := by have h : 0 ≤ a^2 - 2*a*b + b^2 calc a^2 - 2*a*b + b^2 = (a - b)^2 := by ring _ ≥ 0 := by apply pow_two_nonneg linarith theorem fact2 : -(a*b)*2 ≤ a^2 + b^2 := by have h : 0 ≤ a^2 + 2*a*b + b^2 calc a^2 + 2*a*b + b^2 = (a + b)^2 := by ring _ ≥ 0 := by apply pow_two_nonneg linarith example : |a*b| ≤ (a^2 + b^2)/2 := by have h : (0 : ℝ) < 2 := by norm_num apply abs_le'.mpr constructor · rw [le_div_iff₀ h] apply fact1 rw [le_div_iff₀ h] apply fact2
mathematics_in_lean/MIL/C02_Basics/solutions/Solutions_S02_Proving_Identities_in_Algebraic_Structures.lean
import Mathlib.Algebra.Ring.Defs import Mathlib.Data.Real.Basic import MIL.Common namespace MyRing variable {R : Type*} [Ring R] theorem add_neg_cancel_right (a b : R) : a + b + -b = a := by rw [add_assoc, add_neg_cancel, add_zero] theorem add_left_cancel {a b c : R} (h : a + b = a + c) : b = c := by rw [← neg_add_cancel_left a b, h, neg_add_cancel_left] theorem add_right_cancel {a b c : R} (h : a + b = c + b) : a = c := by rw [← add_neg_cancel_right a b, h, add_neg_cancel_right] theorem zero_mul (a : R) : 0 * a = 0 := by have h : 0 * a + 0 * a = 0 * a + 0 := by rw [← add_mul, add_zero, add_zero] rw [add_left_cancel h] theorem neg_eq_of_add_eq_zero {a b : R} (h : a + b = 0) : -a = b := by rw [← neg_add_cancel_left a b, h, add_zero] theorem eq_neg_of_add_eq_zero {a b : R} (h : a + b = 0) : a = -b := by symm apply neg_eq_of_add_eq_zero rw [add_comm, h] theorem neg_zero : (-0 : R) = 0 := by apply neg_eq_of_add_eq_zero rw [add_zero] theorem neg_neg (a : R) : - -a = a := by apply neg_eq_of_add_eq_zero rw [neg_add_cancel] end MyRing namespace MyRing variable {R : Type*} [Ring R] theorem self_sub (a : R) : a - a = 0 := by rw [sub_eq_add_neg, add_neg_cancel] theorem one_add_one_eq_two : 1 + 1 = (2 : R) := by norm_num theorem two_mul (a : R) : 2 * a = a + a := by rw [← one_add_one_eq_two, add_mul, one_mul] end MyRing section variable {G : Type*} [Group G] namespace MyGroup theorem mul_inv_cancel (a : G) : a * a⁻¹ = 1 := by have h : (a * a⁻¹)⁻¹ * (a * a⁻¹ * (a * a⁻¹)) = 1 := by rw [mul_assoc, ← mul_assoc a⁻¹ a, inv_mul_cancel, one_mul, inv_mul_cancel] rw [← h, ← mul_assoc, inv_mul_cancel, one_mul] theorem mul_one (a : G) : a * 1 = a := by rw [← inv_mul_cancel a, ← mul_assoc, mul_inv_cancel, one_mul] theorem mul_inv_rev (a b : G) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by rw [← one_mul (b⁻¹ * a⁻¹), ← inv_mul_cancel (a * b), mul_assoc, mul_assoc, ← mul_assoc b b⁻¹, mul_inv_cancel, one_mul, mul_inv_cancel, mul_one] end MyGroup end
mathematics_in_lean/MIL/C02_Basics/solutions/Solutions_S04_More_on_Order_and_Divisibility.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C02S04 section variable (a b c d : ℝ) #check (min_le_left a b : min a b ≤ a) #check (min_le_right a b : min a b ≤ b) #check (le_min : c ≤ a → c ≤ b → c ≤ min a b) example : max a b = max b a := by apply le_antisymm repeat apply max_le apply le_max_right apply le_max_left example : min (min a b) c = min a (min b c) := by apply le_antisymm · apply le_min · apply le_trans apply min_le_left apply min_le_left apply le_min · apply le_trans apply min_le_left apply min_le_right apply min_le_right apply le_min · apply le_min · apply min_le_left apply le_trans apply min_le_right apply min_le_left apply le_trans apply min_le_right apply min_le_right theorem aux : min a b + c ≤ min (a + c) (b + c) := by apply le_min · apply add_le_add_right apply min_le_left apply add_le_add_right apply min_le_right example : min a b + c = min (a + c) (b + c) := by apply le_antisymm · apply aux have h : min (a + c) (b + c) = min (a + c) (b + c) - c + c := by rw [sub_add_cancel] rw [h] apply add_le_add_right rw [sub_eq_add_neg] apply le_trans apply aux rw [add_neg_cancel_right, add_neg_cancel_right] example : |a| - |b| ≤ |a - b| := calc |a| - |b| = |a - b + b| - |b| := by rw [sub_add_cancel] _ ≤ |a - b| + |b| - |b| := by apply sub_le_sub_right apply abs_add _ ≤ |a - b| := by rw [add_sub_cancel_right] -- alternatively example : |a| - |b| ≤ |a - b| := by have h := abs_add (a - b) b rw [sub_add_cancel] at h linarith end section variable (w x y z : ℕ) example (h₀ : x ∣ y) (h₁ : y ∣ z) : x ∣ z := dvd_trans h₀ h₁ example : x ∣ y * x * z := by apply dvd_mul_of_dvd_left apply dvd_mul_left example : x ∣ x ^ 2 := by apply dvd_mul_left example (h : x ∣ w) : x ∣ y * (x * z) + x ^ 2 + w ^ 2 := by apply dvd_add · apply dvd_add · apply dvd_mul_of_dvd_right apply dvd_mul_right apply dvd_mul_left rw [pow_two] apply dvd_mul_of_dvd_right exact h end section variable (m n : ℕ) #check (Nat.gcd_zero_right n : Nat.gcd n 0 = n) #check (Nat.gcd_zero_left n : Nat.gcd 0 n = n) #check (Nat.lcm_zero_right n : Nat.lcm n 0 = 0) #check (Nat.lcm_zero_left n : Nat.lcm 0 n = 0) example : Nat.gcd m n = Nat.gcd n m := by apply Nat.dvd_antisymm repeat apply Nat.dvd_gcd apply Nat.gcd_dvd_right apply Nat.gcd_dvd_left end
mathematics_in_lean/MIL/C02_Basics/solutions/Solutions_S05_Proving_Facts_about_Algebraic_Structures.lean
import MIL.Common import Mathlib.Topology.MetricSpace.Basic section variable {α : Type*} [Lattice α] variable (x y z : α) example : x ⊓ y = y ⊓ x := by apply le_antisymm repeat apply le_inf · apply inf_le_right apply inf_le_left example : x ⊓ y ⊓ z = x ⊓ (y ⊓ z) := by apply le_antisymm · apply le_inf · trans x ⊓ y apply inf_le_left apply inf_le_left apply le_inf · trans x ⊓ y apply inf_le_left apply inf_le_right apply inf_le_right apply le_inf · apply le_inf · apply inf_le_left trans y ⊓ z apply inf_le_right apply inf_le_left trans y ⊓ z apply inf_le_right apply inf_le_right example : x ⊔ y = y ⊔ x := by apply le_antisymm repeat apply sup_le · apply le_sup_right apply le_sup_left example : x ⊔ y ⊔ z = x ⊔ (y ⊔ z) := by apply le_antisymm · apply sup_le · apply sup_le apply le_sup_left · trans y ⊔ z apply le_sup_left apply le_sup_right trans y ⊔ z apply le_sup_right apply le_sup_right apply sup_le · trans x ⊔ y apply le_sup_left apply le_sup_left apply sup_le · trans x ⊔ y apply le_sup_right apply le_sup_left apply le_sup_right theorem absorb1 : x ⊓ (x ⊔ y) = x := by apply le_antisymm · apply inf_le_left apply le_inf · apply le_refl apply le_sup_left theorem absorb2 : x ⊔ x ⊓ y = x := by apply le_antisymm · apply sup_le · apply le_refl apply inf_le_left apply le_sup_left end section variable {α : Type*} [DistribLattice α] variable (x y z : α) #check (inf_sup_left x y z : x ⊓ (y ⊔ z) = x ⊓ y ⊔ x ⊓ z) #check (inf_sup_right x y z : (x ⊔ y) ⊓ z = x ⊓ z ⊔ y ⊓ z) #check (sup_inf_left x y z : x ⊔ y ⊓ z = (x ⊔ y) ⊓ (x ⊔ z)) #check (sup_inf_right x y z : x ⊓ y ⊔ z = (x ⊔ z) ⊓ (y ⊔ z)) end section variable {α : Type*} [Lattice α] variable (a b c : α) example (h : ∀ x y z : α, x ⊓ (y ⊔ z) = x ⊓ y ⊔ x ⊓ z) : a ⊔ b ⊓ c = (a ⊔ b) ⊓ (a ⊔ c) := by rw [h, @inf_comm _ _ (a ⊔ b), absorb1, @inf_comm _ _ (a ⊔ b), h, ← sup_assoc, @inf_comm _ _ c a, absorb2, inf_comm] example (h : ∀ x y z : α, x ⊔ y ⊓ z = (x ⊔ y) ⊓ (x ⊔ z)) : a ⊓ (b ⊔ c) = a ⊓ b ⊔ a ⊓ c := by rw [h, @sup_comm _ _ (a ⊓ b), absorb2, @sup_comm _ _ (a ⊓ b), h, ← inf_assoc, @sup_comm _ _ c a, absorb1, sup_comm] end section variable {R : Type*} [Ring R] [PartialOrder R] [IsStrictOrderedRing R] variable (a b c : R) theorem aux1 (h : a ≤ b) : 0 ≤ b - a := by rw [← sub_self a, sub_eq_add_neg, sub_eq_add_neg, add_comm, add_comm b] apply add_le_add_left h theorem aux2 (h : 0 ≤ b - a) : a ≤ b := by rw [← add_zero a, ← sub_add_cancel b a, add_comm (b - a)] apply add_le_add_left h example (h : a ≤ b) (h' : 0 ≤ c) : a * c ≤ b * c := by have h1 : 0 ≤ (b - a) * c := mul_nonneg (aux1 _ _ h) h' rw [sub_mul] at h1 exact aux2 _ _ h1 end section variable {X : Type*} [MetricSpace X] variable (x y z : X) example (x y : X) : 0 ≤ dist x y :=by have : 0 ≤ dist x y + dist y x := by rw [← dist_self x] apply dist_triangle linarith [dist_comm x y] end
mathematics_in_lean/MIL/C02_Basics/solutions/Solutions_S01_Calculating.lean
import MIL.Common import Mathlib.Data.Real.Basic example (a b c : ℝ) : c * b * a = b * (a * c) := by rw [mul_comm c b] rw [mul_assoc b c a] rw [mul_comm c a] example (a b c : ℝ) : a * (b * c) = b * (a * c) := by rw [← mul_assoc a b c] rw [mul_comm a b] rw [mul_assoc b a c] example (a b c : ℝ) : a * (b * c) = b * (c * a) := by rw [mul_comm] rw [mul_assoc] example (a b c : ℝ) : a * (b * c) = b * (a * c) := by rw [← mul_assoc] rw [mul_comm a] rw [mul_assoc] example (a b c d e f : ℝ) (h : b * c = e * f) : a * b * c * d = a * e * f * d := by rw [mul_assoc a] rw [h] rw [← mul_assoc] example (a b c d : ℝ) (hyp : c = b * a - d) (hyp' : d = a * b) : c = 0 := by rw [hyp] rw [hyp'] rw [mul_comm] rw [sub_self]
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/S02_Induction_and_Recursion.lean
import Mathlib.Data.Nat.GCD.Basic import MIL.Common example (n : Nat) : n.succ ≠ Nat.zero := Nat.succ_ne_zero n example (m n : Nat) (h : m.succ = n.succ) : m = n := Nat.succ.inj h def fac : ℕ → ℕ | 0 => 1 | n + 1 => (n + 1) * fac n example : fac 0 = 1 := rfl example : fac 0 = 1 := by rw [fac] example : fac 0 = 1 := by simp [fac] example (n : ℕ) : fac (n + 1) = (n + 1) * fac n := rfl example (n : ℕ) : fac (n + 1) = (n + 1) * fac n := by rw [fac] example (n : ℕ) : fac (n + 1) = (n + 1) * fac n := by simp [fac] theorem fac_pos (n : ℕ) : 0 < fac n := by induction' n with n ih · rw [fac] exact zero_lt_one rw [fac] exact mul_pos n.succ_pos ih theorem dvd_fac {i n : ℕ} (ipos : 0 < i) (ile : i ≤ n) : i ∣ fac n := by induction' n with n ih · exact absurd ipos (not_lt_of_ge ile) rw [fac] rcases Nat.of_le_succ ile with h | h · apply dvd_mul_of_dvd_right (ih h) rw [h] apply dvd_mul_right theorem pow_two_le_fac (n : ℕ) : 2 ^ (n - 1) ≤ fac n := by rcases n with _ | n · simp [fac] sorry section variable {α : Type*} (s : Finset ℕ) (f : ℕ → ℕ) (n : ℕ) #check Finset.sum s f #check Finset.prod s f open BigOperators open Finset example : s.sum f = ∑ x ∈ s, f x := rfl example : s.prod f = ∏ x ∈ s, f x := rfl example : (range n).sum f = ∑ x ∈ range n, f x := rfl example : (range n).prod f = ∏ x ∈ range n, f x := rfl example (f : ℕ → ℕ) : ∑ x ∈ range 0, f x = 0 := Finset.sum_range_zero f example (f : ℕ → ℕ) (n : ℕ) : ∑ x ∈ range n.succ, f x = ∑ x ∈ range n, f x + f n := Finset.sum_range_succ f n example (f : ℕ → ℕ) : ∏ x ∈ range 0, f x = 1 := Finset.prod_range_zero f example (f : ℕ → ℕ) (n : ℕ) : ∏ x ∈ range n.succ, f x = (∏ x ∈ range n, f x) * f n := Finset.prod_range_succ f n example (n : ℕ) : fac n = ∏ i ∈ range n, (i + 1) := by induction' n with n ih · simp [fac, prod_range_zero] simp [fac, ih, prod_range_succ, mul_comm] example (a b c d e f : ℕ) : a * (b * c * f * (d * e)) = d * (a * f * e) * (c * b) := by simp [mul_assoc, mul_comm, mul_left_comm] theorem sum_id (n : ℕ) : ∑ i ∈ range (n + 1), i = n * (n + 1) / 2 := by symm; apply Nat.div_eq_of_eq_mul_right (by norm_num : 0 < 2) induction' n with n ih · simp rw [Finset.sum_range_succ, mul_add 2, ← ih] ring theorem sum_sqr (n : ℕ) : ∑ i ∈ range (n + 1), i ^ 2 = n * (n + 1) * (2 * n + 1) / 6 := by sorry end inductive MyNat where | zero : MyNat | succ : MyNat → MyNat namespace MyNat def add : MyNat → MyNat → MyNat | x, zero => x | x, succ y => succ (add x y) def mul : MyNat → MyNat → MyNat | x, zero => zero | x, succ y => add (mul x y) x theorem zero_add (n : MyNat) : add zero n = n := by induction' n with n ih · rfl rw [add, ih] theorem succ_add (m n : MyNat) : add (succ m) n = succ (add m n) := by induction' n with n ih · rfl rw [add, ih] rfl theorem add_comm (m n : MyNat) : add m n = add n m := by induction' n with n ih · rw [zero_add] rfl rw [add, succ_add, ih] theorem add_assoc (m n k : MyNat) : add (add m n) k = add m (add n k) := by sorry theorem mul_add (m n k : MyNat) : mul m (add n k) = add (mul m n) (mul m k) := by sorry theorem zero_mul (n : MyNat) : mul zero n = zero := by sorry theorem succ_mul (m n : MyNat) : mul (succ m) n = add (mul m n) n := by sorry theorem mul_comm (m n : MyNat) : mul m n = mul n m := by sorry end MyNat
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/S01_Irrational_Roots.lean
import MIL.Common import Mathlib.Data.Nat.Factorization.Basic import Mathlib.Data.Nat.Prime.Basic #print Nat.Coprime example (m n : Nat) (h : m.Coprime n) : m.gcd n = 1 := h example (m n : Nat) (h : m.Coprime n) : m.gcd n = 1 := by rw [Nat.Coprime] at h exact h example : Nat.Coprime 12 7 := by norm_num example : Nat.gcd 12 8 = 4 := by norm_num #check Nat.prime_def_lt example (p : ℕ) (prime_p : Nat.Prime p) : 2 ≤ p ∧ ∀ m : ℕ, m < p → m ∣ p → m = 1 := by rwa [Nat.prime_def_lt] at prime_p #check Nat.Prime.eq_one_or_self_of_dvd example (p : ℕ) (prime_p : Nat.Prime p) : ∀ m : ℕ, m ∣ p → m = 1 ∨ m = p := prime_p.eq_one_or_self_of_dvd example : Nat.Prime 17 := by norm_num -- commonly used example : Nat.Prime 2 := Nat.prime_two example : Nat.Prime 3 := Nat.prime_three #check Nat.Prime.dvd_mul #check Nat.Prime.dvd_mul Nat.prime_two #check Nat.prime_two.dvd_mul theorem even_of_even_sqr {m : ℕ} (h : 2 ∣ m ^ 2) : 2 ∣ m := by rw [pow_two, Nat.prime_two.dvd_mul] at h cases h <;> assumption example {m : ℕ} (h : 2 ∣ m ^ 2) : 2 ∣ m := Nat.Prime.dvd_of_dvd_pow Nat.prime_two h example (a b c : Nat) (h : a * b = a * c) (h' : a ≠ 0) : b = c := -- apply? suggests the following: (mul_right_inj' h').mp h example {m n : ℕ} (coprime_mn : m.Coprime n) : m ^ 2 ≠ 2 * n ^ 2 := by intro sqr_eq have : 2 ∣ m := by sorry obtain ⟨k, meq⟩ := dvd_iff_exists_eq_mul_left.mp this have : 2 * (2 * k ^ 2) = 2 * n ^ 2 := by rw [← sqr_eq, meq] ring have : 2 * k ^ 2 = n ^ 2 := sorry have : 2 ∣ n := by sorry have : 2 ∣ m.gcd n := by sorry have : 2 ∣ 1 := by sorry norm_num at this example {m n p : ℕ} (coprime_mn : m.Coprime n) (prime_p : p.Prime) : m ^ 2 ≠ p * n ^ 2 := by sorry #check Nat.primeFactorsList #check Nat.prime_of_mem_primeFactorsList #check Nat.prod_primeFactorsList #check Nat.primeFactorsList_unique theorem factorization_mul' {m n : ℕ} (mnez : m ≠ 0) (nnez : n ≠ 0) (p : ℕ) : (m * n).factorization p = m.factorization p + n.factorization p := by rw [Nat.factorization_mul mnez nnez] rfl theorem factorization_pow' (n k p : ℕ) : (n ^ k).factorization p = k * n.factorization p := by rw [Nat.factorization_pow] rfl theorem Nat.Prime.factorization' {p : ℕ} (prime_p : p.Prime) : p.factorization p = 1 := by rw [prime_p.factorization] simp example {m n p : ℕ} (nnz : n ≠ 0) (prime_p : p.Prime) : m ^ 2 ≠ p * n ^ 2 := by intro sqr_eq have nsqr_nez : n ^ 2 ≠ 0 := by simpa have eq1 : Nat.factorization (m ^ 2) p = 2 * m.factorization p := by sorry have eq2 : (p * n ^ 2).factorization p = 2 * n.factorization p + 1 := by sorry have : 2 * m.factorization p % 2 = (2 * n.factorization p + 1) % 2 := by rw [← eq1, sqr_eq, eq2] rw [add_comm, Nat.add_mul_mod_self_left, Nat.mul_mod_right] at this norm_num at this example {m n k r : ℕ} (nnz : n ≠ 0) (pow_eq : m ^ k = r * n ^ k) {p : ℕ} : k ∣ r.factorization p := by rcases r with _ | r · simp have npow_nz : n ^ k ≠ 0 := fun npowz ↦ nnz (pow_eq_zero npowz) have eq1 : (m ^ k).factorization p = k * m.factorization p := by sorry have eq2 : ((r + 1) * n ^ k).factorization p = k * n.factorization p + (r + 1).factorization p := by sorry have : r.succ.factorization p = k * m.factorization p - k * n.factorization p := by rw [← eq1, pow_eq, eq2, add_comm, Nat.add_sub_cancel] rw [this] sorry #check multiplicity
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/S04_More_Induction.lean
import MIL.Common import Mathlib.Analysis.Calculus.Taylor import Mathlib.Data.Nat.GCD.Basic namespace more_induction def fac : ℕ → ℕ | 0 => 1 | n + 1 => (n + 1) * fac n theorem fac_pos (n : ℕ) : 0 < fac n := by induction' n with n ih · rw [fac] exact zero_lt_one rw [fac] exact mul_pos n.succ_pos ih example (n : ℕ) : 0 < fac n := by induction n case zero => rw [fac] exact zero_lt_one case succ n ih => rw [fac] exact mul_pos n.succ_pos ih example (n : ℕ) : 0 < fac n := by induction n with | zero => rw [fac] exact zero_lt_one | succ n ih => rw [fac] exact mul_pos n.succ_pos ih theorem fac_pos' : ∀ n, 0 < fac n | 0 => by rw [fac] exact zero_lt_one | n + 1 => by rw [fac] exact mul_pos n.succ_pos (fac_pos' n) @[simp] def fib : ℕ → ℕ | 0 => 0 | 1 => 1 | n + 2 => fib n + fib (n + 1) theorem fib_add_two (n : ℕ) : fib (n + 2) = fib n + fib (n + 1) := rfl example (n : ℕ) : fib (n + 2) = fib n + fib (n + 1) := by rw [fib] noncomputable section def phi : ℝ := (1 + √5) / 2 def phi' : ℝ := (1 - √5) / 2 theorem phi_sq : phi^2 = phi + 1 := by field_simp [phi, add_sq]; ring theorem phi'_sq : phi'^2 = phi' + 1 := by field_simp [phi', sub_sq]; ring theorem fib_eq : ∀ n, fib n = (phi^n - phi'^n) / √5 | 0 => by simp | 1 => by field_simp [phi, phi'] | n+2 => by field_simp [fib_eq, pow_add, phi_sq, phi'_sq]; ring end theorem fib_coprime_fib_succ (n : ℕ) : Nat.Coprime (fib n) (fib (n + 1)) := by induction n with | zero => simp | succ n ih => simp only [fib, Nat.coprime_add_self_right] exact ih.symm #eval fib 6 #eval List.range 20 |>.map fib def fib' (n : Nat) : Nat := aux n 0 1 where aux | 0, x, _ => x | n+1, x, y => aux n y (x + y) theorem fib'.aux_eq (m n : ℕ) : fib'.aux n (fib m) (fib (m + 1)) = fib (n + m) := by induction n generalizing m with | zero => simp [fib'.aux] | succ n ih => rw [fib'.aux, ←fib_add_two, ih, add_assoc, add_comm 1] theorem fib'_eq_fib : fib' = fib := by ext n erw [fib', fib'.aux_eq 0 n]; rfl #eval fib' 10000 theorem fib_add (m n : ℕ) : fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) := by induction n generalizing m with | zero => simp | succ n ih => specialize ih (m + 1) rw [add_assoc m 1 n, add_comm 1 n] at ih simp only [fib_add_two, Nat.succ_eq_add_one, ih] ring theorem fib_add' : ∀ m n, fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) | _, 0 => by simp | m, n + 1 => by have := fib_add' (m + 1) n rw [add_assoc m 1 n, add_comm 1 n] at this simp only [fib_add_two, Nat.succ_eq_add_one, this] ring example (n : ℕ): (fib n) ^ 2 + (fib (n + 1)) ^ 2 = fib (2 * n + 1) := by sorry example (n : ℕ): (fib n) ^ 2 + (fib (n + 1)) ^ 2 = fib (2 * n + 1) := by rw [two_mul, fib_add, pow_two, pow_two] #check (@Nat.not_prime_iff_exists_dvd_lt : ∀ {n : ℕ}, 2 ≤ n → (¬Nat.Prime n ↔ ∃ m, m ∣ n ∧ 2 ≤ m ∧ m < n)) theorem ne_one_iff_exists_prime_dvd : ∀ {n}, n ≠ 1 ↔ ∃ p : ℕ, p.Prime ∧ p ∣ n | 0 => by simpa using Exists.intro 2 Nat.prime_two | 1 => by simp [Nat.not_prime_one] | n + 2 => by have hn : n+2 ≠ 1 := by omega simp only [Ne, not_false_iff, true_iff, hn] by_cases h : Nat.Prime (n + 2) · use n+2, h · have : 2 ≤ n + 2 := by omega rw [Nat.not_prime_iff_exists_dvd_lt this] at h rcases h with ⟨m, mdvdn, mge2, -⟩ have : m ≠ 1 := by omega rw [ne_one_iff_exists_prime_dvd] at this rcases this with ⟨p, primep, pdvdm⟩ use p, primep exact pdvdm.trans mdvdn theorem zero_lt_of_mul_eq_one (m n : ℕ) : n * m = 1 → 0 < n ∧ 0 < m := by cases n <;> cases m <;> simp example (m n : ℕ) : n*m = 1 → 0 < n ∧ 0 < m := by rcases m with (_ | m); simp rcases n with (_ | n) <;> simp
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/S03_Infinitely_Many_Primes.lean
import Mathlib.Data.Nat.Prime.Basic import MIL.Common open BigOperators namespace C05S03 theorem two_le {m : ℕ} (h0 : m ≠ 0) (h1 : m ≠ 1) : 2 ≤ m := by cases m; contradiction case succ m => cases m; contradiction repeat apply Nat.succ_le_succ apply zero_le example {m : ℕ} (h0 : m ≠ 0) (h1 : m ≠ 1) : 2 ≤ m := by by_contra h push_neg at h interval_cases m <;> contradiction example {m : ℕ} (h0 : m ≠ 0) (h1 : m ≠ 1) : 2 ≤ m := by by_contra h push_neg at h revert h0 h1 revert h m decide theorem exists_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ p : Nat, p.Prime ∧ p ∣ n := by by_cases np : n.Prime · use n, np induction' n using Nat.strong_induction_on with n ih rw [Nat.prime_def_lt] at np push_neg at np rcases np h with ⟨m, mltn, mdvdn, mne1⟩ have : m ≠ 0 := by intro mz rw [mz, zero_dvd_iff] at mdvdn linarith have mgt2 : 2 ≤ m := two_le this mne1 by_cases mp : m.Prime · use m, mp · rcases ih m mltn mgt2 mp with ⟨p, pp, pdvd⟩ use p, pp apply pdvd.trans mdvdn theorem primes_infinite : ∀ n, ∃ p > n, Nat.Prime p := by intro n have : 2 ≤ Nat.factorial n + 1 := by sorry rcases exists_prime_factor this with ⟨p, pp, pdvd⟩ refine ⟨p, ?_, pp⟩ show p > n by_contra ple push_neg at ple have : p ∣ Nat.factorial n := by sorry have : p ∣ 1 := by sorry show False sorry open Finset section variable {α : Type*} [DecidableEq α] (r s t : Finset α) example : r ∩ (s ∪ t) ⊆ r ∩ s ∪ r ∩ t := by rw [subset_iff] intro x rw [mem_inter, mem_union, mem_union, mem_inter, mem_inter] tauto example : r ∩ (s ∪ t) ⊆ r ∩ s ∪ r ∩ t := by simp [subset_iff] intro x tauto example : r ∩ s ∪ r ∩ t ⊆ r ∩ (s ∪ t) := by simp [subset_iff] intro x tauto example : r ∩ s ∪ r ∩ t = r ∩ (s ∪ t) := by ext x simp tauto end section variable {α : Type*} [DecidableEq α] (r s t : Finset α) example : (r ∪ s) ∩ (r ∪ t) = r ∪ s ∩ t := by sorry example : (r \ s) \ t = r \ (s ∪ t) := by sorry end example (s : Finset ℕ) (n : ℕ) (h : n ∈ s) : n ∣ ∏ i ∈ s, i := Finset.dvd_prod_of_mem _ h theorem _root_.Nat.Prime.eq_of_dvd_of_prime {p q : ℕ} (prime_p : Nat.Prime p) (prime_q : Nat.Prime q) (h : p ∣ q) : p = q := by sorry theorem mem_of_dvd_prod_primes {s : Finset ℕ} {p : ℕ} (prime_p : p.Prime) : (∀ n ∈ s, Nat.Prime n) → (p ∣ ∏ n ∈ s, n) → p ∈ s := by intro h₀ h₁ induction' s using Finset.induction_on with a s ans ih · simp at h₁ linarith [prime_p.two_le] simp [Finset.prod_insert ans, prime_p.dvd_mul] at h₀ h₁ rw [mem_insert] sorry example (s : Finset ℕ) (x : ℕ) : x ∈ s.filter Nat.Prime ↔ x ∈ s ∧ x.Prime := mem_filter theorem primes_infinite' : ∀ s : Finset Nat, ∃ p, Nat.Prime p ∧ p ∉ s := by intro s by_contra h push_neg at h set s' := s.filter Nat.Prime with s'_def have mem_s' : ∀ {n : ℕ}, n ∈ s' ↔ n.Prime := by intro n simp [s'_def] apply h have : 2 ≤ (∏ i ∈ s', i) + 1 := by sorry rcases exists_prime_factor this with ⟨p, pp, pdvd⟩ have : p ∣ ∏ i ∈ s', i := by sorry have : p ∣ 1 := by convert Nat.dvd_sub pdvd this simp show False sorry theorem bounded_of_ex_finset (Q : ℕ → Prop) : (∃ s : Finset ℕ, ∀ k, Q k → k ∈ s) → ∃ n, ∀ k, Q k → k < n := by rintro ⟨s, hs⟩ use s.sup id + 1 intro k Qk apply Nat.lt_succ_of_le show id k ≤ s.sup id apply le_sup (hs k Qk) theorem ex_finset_of_bounded (Q : ℕ → Prop) [DecidablePred Q] : (∃ n, ∀ k, Q k → k ≤ n) → ∃ s : Finset ℕ, ∀ k, Q k ↔ k ∈ s := by rintro ⟨n, hn⟩ use (range (n + 1)).filter Q intro k simp [Nat.lt_succ_iff] exact hn k example : 27 % 4 = 3 := by norm_num example (n : ℕ) : (4 * n + 3) % 4 = 3 := by rw [add_comm, Nat.add_mul_mod_self_left] theorem mod_4_eq_3_or_mod_4_eq_3 {m n : ℕ} (h : m * n % 4 = 3) : m % 4 = 3 ∨ n % 4 = 3 := by revert h rw [Nat.mul_mod] have : m % 4 < 4 := Nat.mod_lt m (by norm_num) interval_cases m % 4 <;> simp [-Nat.mul_mod_mod] have : n % 4 < 4 := Nat.mod_lt n (by norm_num) interval_cases n % 4 <;> simp theorem two_le_of_mod_4_eq_3 {n : ℕ} (h : n % 4 = 3) : 2 ≤ n := by apply two_le <;> · intro neq rw [neq] at h norm_num at h theorem aux {m n : ℕ} (h₀ : m ∣ n) (h₁ : 2 ≤ m) (h₂ : m < n) : n / m ∣ n ∧ n / m < n := by sorry theorem exists_prime_factor_mod_4_eq_3 {n : Nat} (h : n % 4 = 3) : ∃ p : Nat, p.Prime ∧ p ∣ n ∧ p % 4 = 3 := by by_cases np : n.Prime · use n induction' n using Nat.strong_induction_on with n ih rw [Nat.prime_def_lt] at np push_neg at np rcases np (two_le_of_mod_4_eq_3 h) with ⟨m, mltn, mdvdn, mne1⟩ have mge2 : 2 ≤ m := by apply two_le _ mne1 intro mz rw [mz, zero_dvd_iff] at mdvdn linarith have neq : m * (n / m) = n := Nat.mul_div_cancel' mdvdn have : m % 4 = 3 ∨ n / m % 4 = 3 := by apply mod_4_eq_3_or_mod_4_eq_3 rw [neq, h] rcases this with h1 | h1 . sorry . sorry example (m n : ℕ) (s : Finset ℕ) (h : m ∈ erase s n) : m ≠ n ∧ m ∈ s := by rwa [mem_erase] at h example (m n : ℕ) (s : Finset ℕ) (h : m ∈ erase s n) : m ≠ n ∧ m ∈ s := by simp at h assumption theorem primes_mod_4_eq_3_infinite : ∀ n, ∃ p > n, Nat.Prime p ∧ p % 4 = 3 := by by_contra h push_neg at h rcases h with ⟨n, hn⟩ have : ∃ s : Finset Nat, ∀ p : ℕ, p.Prime ∧ p % 4 = 3 ↔ p ∈ s := by apply ex_finset_of_bounded use n contrapose! hn rcases hn with ⟨p, ⟨pp, p4⟩, pltn⟩ exact ⟨p, pltn, pp, p4⟩ rcases this with ⟨s, hs⟩ have h₁ : ((4 * ∏ i ∈ erase s 3, i) + 3) % 4 = 3 := by sorry rcases exists_prime_factor_mod_4_eq_3 h₁ with ⟨p, pp, pdvd, p4eq⟩ have ps : p ∈ s := by sorry have pne3 : p ≠ 3 := by sorry have : p ∣ 4 * ∏ i ∈ erase s 3, i := by sorry have : p ∣ 3 := by sorry have : p = 3 := by sorry contradiction
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/solutions/Solutions_S03_Infinitely_Many_Primes.lean
import Mathlib.Data.Nat.Prime.Basic import MIL.Common open BigOperators namespace C05S03 theorem two_le {m : ℕ} (h0 : m ≠ 0) (h1 : m ≠ 1) : 2 ≤ m := by cases m; contradiction case succ m => cases m; contradiction repeat apply Nat.succ_le_succ apply zero_le theorem exists_prime_factor {n : Nat} (h : 2 ≤ n) : ∃ p : Nat, p.Prime ∧ p ∣ n := by by_cases np : n.Prime · use n, np induction' n using Nat.strong_induction_on with n ih rw [Nat.prime_def_lt] at np push_neg at np rcases np h with ⟨m, mltn, mdvdn, mne1⟩ have : m ≠ 0 := by intro mz rw [mz, zero_dvd_iff] at mdvdn linarith have mgt2 : 2 ≤ m := two_le this mne1 by_cases mp : m.Prime · use m, mp · rcases ih m mltn mgt2 mp with ⟨p, pp, pdvd⟩ use p, pp apply pdvd.trans mdvdn theorem primes_infinite : ∀ n, ∃ p > n, Nat.Prime p := by intro n have : 2 ≤ Nat.factorial n + 1 := by apply Nat.succ_le_succ exact Nat.succ_le_of_lt (Nat.factorial_pos _) rcases exists_prime_factor this with ⟨p, pp, pdvd⟩ refine ⟨p, ?_, pp⟩ show p > n by_contra ple push_neg at ple have : p ∣ Nat.factorial n := by apply Nat.dvd_factorial apply pp.pos linarith have : p ∣ 1 := by convert Nat.dvd_sub pdvd this simp show False have := Nat.le_of_dvd zero_lt_one this linarith [pp.two_le] open Finset section variable {α : Type*} [DecidableEq α] (r s t : Finset α) example : (r ∪ s) ∩ (r ∪ t) = r ∪ s ∩ t := by ext x rw [mem_inter, mem_union, mem_union, mem_union, mem_inter] tauto example : (r ∪ s) ∩ (r ∪ t) = r ∪ s ∩ t := by ext x simp tauto example : (r \ s) \ t = r \ (s ∪ t) := by ext x rw [mem_sdiff, mem_sdiff, mem_sdiff, mem_union] tauto example : (r \ s) \ t = r \ (s ∪ t) := by ext x simp tauto end theorem _root_.Nat.Prime.eq_of_dvd_of_prime {p q : ℕ} (prime_p : Nat.Prime p) (prime_q : Nat.Prime q) (h : p ∣ q) : p = q := by cases prime_q.eq_one_or_self_of_dvd _ h · linarith [prime_p.two_le] assumption theorem mem_of_dvd_prod_primes {s : Finset ℕ} {p : ℕ} (prime_p : p.Prime) : (∀ n ∈ s, Nat.Prime n) → (p ∣ ∏ n ∈ s, n) → p ∈ s := by intro h₀ h₁ induction' s using Finset.induction_on with a s ans ih · simp at h₁ linarith [prime_p.two_le] simp [Finset.prod_insert ans, prime_p.dvd_mul] at h₀ h₁ rw [mem_insert] rcases h₁ with h₁ | h₁ · left exact prime_p.eq_of_dvd_of_prime h₀.1 h₁ right exact ih h₀.2 h₁ theorem primes_infinite' : ∀ s : Finset Nat, ∃ p, Nat.Prime p ∧ p ∉ s := by intro s by_contra h push_neg at h set s' := s.filter Nat.Prime with s'_def have mem_s' : ∀ {n : ℕ}, n ∈ s' ↔ n.Prime := by intro n simp [s'_def] apply h have : 2 ≤ (∏ i ∈ s', i) + 1 := by apply Nat.succ_le_succ apply Nat.succ_le_of_lt apply Finset.prod_pos intro n ns' apply (mem_s'.mp ns').pos rcases exists_prime_factor this with ⟨p, pp, pdvd⟩ have : p ∣ ∏ i ∈ s', i := by apply dvd_prod_of_mem rw [mem_s'] apply pp have : p ∣ 1 := by convert Nat.dvd_sub pdvd this simp show False have := Nat.le_of_dvd zero_lt_one this linarith [pp.two_le] theorem bounded_of_ex_finset (Q : ℕ → Prop) : (∃ s : Finset ℕ, ∀ k, Q k → k ∈ s) → ∃ n, ∀ k, Q k → k < n := by rintro ⟨s, hs⟩ use s.sup id + 1 intro k Qk apply Nat.lt_succ_of_le show id k ≤ s.sup id apply le_sup (hs k Qk) theorem ex_finset_of_bounded (Q : ℕ → Prop) [DecidablePred Q] : (∃ n, ∀ k, Q k → k ≤ n) → ∃ s : Finset ℕ, ∀ k, Q k ↔ k ∈ s := by rintro ⟨n, hn⟩ use (range (n + 1)).filter Q intro k simp [Nat.lt_succ_iff] exact hn k theorem mod_4_eq_3_or_mod_4_eq_3 {m n : ℕ} (h : m * n % 4 = 3) : m % 4 = 3 ∨ n % 4 = 3 := by revert h rw [Nat.mul_mod] have : m % 4 < 4 := Nat.mod_lt m (by norm_num) interval_cases m % 4 <;> simp [-Nat.mul_mod_mod] have : n % 4 < 4 := Nat.mod_lt n (by norm_num) interval_cases n % 4 <;> simp theorem two_le_of_mod_4_eq_3 {n : ℕ} (h : n % 4 = 3) : 2 ≤ n := by apply two_le <;> · intro neq rw [neq] at h norm_num at h theorem aux {m n : ℕ} (h₀ : m ∣ n) (h₁ : 2 ≤ m) (h₂ : m < n) : n / m ∣ n ∧ n / m < n := by constructor · exact Nat.div_dvd_of_dvd h₀ exact Nat.div_lt_self (lt_of_le_of_lt (zero_le _) h₂) h₁ theorem exists_prime_factor_mod_4_eq_3 {n : Nat} (h : n % 4 = 3) : ∃ p : Nat, p.Prime ∧ p ∣ n ∧ p % 4 = 3 := by by_cases np : n.Prime · use n induction' n using Nat.strong_induction_on with n ih rw [Nat.prime_def_lt] at np push_neg at np rcases np (two_le_of_mod_4_eq_3 h) with ⟨m, mltn, mdvdn, mne1⟩ have mge2 : 2 ≤ m := by apply two_le _ mne1 intro mz rw [mz, zero_dvd_iff] at mdvdn linarith have neq : m * (n / m) = n := Nat.mul_div_cancel' mdvdn have : m % 4 = 3 ∨ n / m % 4 = 3 := by apply mod_4_eq_3_or_mod_4_eq_3 rw [neq, h] rcases this with h1 | h1 · by_cases mp : m.Prime · use m rcases ih m mltn h1 mp with ⟨p, pp, pdvd, p4eq⟩ use p exact ⟨pp, pdvd.trans mdvdn, p4eq⟩ obtain ⟨nmdvdn, nmltn⟩ := aux mdvdn mge2 mltn by_cases nmp : (n / m).Prime · use n / m rcases ih (n / m) nmltn h1 nmp with ⟨p, pp, pdvd, p4eq⟩ use p exact ⟨pp, pdvd.trans nmdvdn, p4eq⟩ theorem primes_mod_4_eq_3_infinite : ∀ n, ∃ p > n, Nat.Prime p ∧ p % 4 = 3 := by by_contra h push_neg at h rcases h with ⟨n, hn⟩ have : ∃ s : Finset Nat, ∀ p : ℕ, p.Prime ∧ p % 4 = 3 ↔ p ∈ s := by apply ex_finset_of_bounded use n contrapose! hn rcases hn with ⟨p, ⟨pp, p4⟩, pltn⟩ exact ⟨p, pltn, pp, p4⟩ rcases this with ⟨s, hs⟩ have h₁ : ((4 * ∏ i ∈ erase s 3, i) + 3) % 4 = 3 := by rw [add_comm, Nat.add_mul_mod_self_left] rcases exists_prime_factor_mod_4_eq_3 h₁ with ⟨p, pp, pdvd, p4eq⟩ have ps : p ∈ s := by rw [← hs p] exact ⟨pp, p4eq⟩ have pne3 : p ≠ 3 := by intro peq rw [peq, ← Nat.dvd_add_iff_left (dvd_refl 3)] at pdvd rw [Nat.prime_three.dvd_mul] at pdvd norm_num at pdvd have : 3 ∈ s.erase 3 := by apply mem_of_dvd_prod_primes Nat.prime_three _ pdvd intro n simp [← hs n] tauto simp at this have : p ∣ 4 * ∏ i ∈ erase s 3, i := by apply dvd_trans _ (dvd_mul_left _ _) apply dvd_prod_of_mem simp constructor <;> assumption have : p ∣ 3 := by convert Nat.dvd_sub pdvd this simp have : p = 3 := by apply pp.eq_of_dvd_of_prime Nat.prime_three this contradiction
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/solutions/Solutions_S02_Induction_and_Recursion.lean
import Mathlib.Data.Nat.GCD.Basic import MIL.Common def fac : ℕ → ℕ | 0 => 1 | n + 1 => (n + 1) * fac n theorem pow_two_le_fac (n : ℕ) : 2 ^ (n - 1) ≤ fac n := by rcases n with _ | n · simp [fac] induction' n with n ih · simp [fac] simp at * rw [pow_succ', fac] apply Nat.mul_le_mul _ ih repeat' apply Nat.succ_le_succ apply zero_le section variable {α : Type*} (s : Finset ℕ) (f : ℕ → ℕ) (n : ℕ) open BigOperators open Finset theorem sum_sqr (n : ℕ) : ∑ i ∈ range (n + 1), i ^ 2 = n * (n + 1) * (2 * n + 1) / 6 := by symm; apply Nat.div_eq_of_eq_mul_right (by norm_num : 0 < 6) induction' n with n ih · simp rw [Finset.sum_range_succ, mul_add 6, ← ih] ring end inductive MyNat where | zero : MyNat | succ : MyNat → MyNat namespace MyNat def add : MyNat → MyNat → MyNat | x, zero => x | x, succ y => succ (add x y) def mul : MyNat → MyNat → MyNat | x, zero => zero | x, succ y => add (mul x y) x theorem zero_add (n : MyNat) : add zero n = n := by induction' n with n ih · rfl rw [add, ih] theorem succ_add (m n : MyNat) : add (succ m) n = succ (add m n) := by induction' n with n ih · rfl rw [add, ih] rfl theorem add_comm (m n : MyNat) : add m n = add n m := by induction' n with n ih · rw [zero_add] rfl rw [add, succ_add, ih] theorem add_assoc (m n k : MyNat) : add (add m n) k = add m (add n k) := by induction' k with k ih · rfl rw [add, ih] rfl theorem mul_add (m n k : MyNat) : mul m (add n k) = add (mul m n) (mul m k) := by induction' k with k ih · rfl rw [add, mul, mul, ih, add_assoc] theorem zero_mul (n : MyNat) : mul zero n = zero := by induction' n with n ih · rfl rw [mul, ih] rfl theorem succ_mul (m n : MyNat) : mul (succ m) n = add (mul m n) n := by induction' n with n ih · rfl rw [mul, mul, ih, add_assoc, add_assoc, add_comm n, succ_add] rfl theorem mul_comm (m n : MyNat) : mul m n = mul n m := by induction' n with n ih · rw [zero_mul] rfl rw [mul, ih, succ_mul] end MyNat
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/solutions/Solutions_S04_More_Induction.lean
import MIL.Common import Mathlib.Analysis.Calculus.Taylor import Mathlib.Data.Nat.GCD.Basic namespace more_induction @[simp] def fib : ℕ → ℕ | 0 => 0 | 1 => 1 | n + 2 => fib n + fib (n + 1) theorem fib_add_two (n : ℕ) : fib (n + 2) = fib n + fib (n + 1) := rfl theorem fib_add (m n : ℕ) : fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) := by induction n generalizing m with | zero => simp | succ n ih => specialize ih (m + 1) rw [add_assoc m 1 n, add_comm 1 n] at ih simp only [fib_add_two, Nat.succ_eq_add_one, ih] ring example (n : ℕ): (fib n) ^ 2 + (fib (n + 1)) ^ 2 = fib (2 * n + 1) := by rw [two_mul, fib_add, pow_two, pow_two] example (n : ℕ): (fib n) ^ 2 + (fib (n + 1)) ^ 2 = fib (2 * n + 1) := by rw [two_mul, fib_add, pow_two, pow_two]
mathematics_in_lean/MIL/C05_Elementary_Number_Theory/solutions/Solutions_S01_Irrational_Roots.lean
import MIL.Common import Mathlib.Data.Nat.Factorization.Basic import Mathlib.Data.Nat.Prime.Basic theorem even_of_even_sqr {m : ℕ} (h : 2 ∣ m ^ 2) : 2 ∣ m := by rw [pow_two, Nat.prime_two.dvd_mul] at h cases h <;> assumption example {m n : ℕ} (coprime_mn : m.Coprime n) : m ^ 2 ≠ 2 * n ^ 2 := by intro sqr_eq have : 2 ∣ m := by apply even_of_even_sqr rw [sqr_eq] apply dvd_mul_right obtain ⟨k, meq⟩ := dvd_iff_exists_eq_mul_left.mp this have : 2 * (2 * k ^ 2) = 2 * n ^ 2 := by rw [← sqr_eq, meq] ring have : 2 * k ^ 2 = n ^ 2 := (mul_right_inj' (by norm_num)).mp this have : 2 ∣ n := by apply even_of_even_sqr rw [← this] apply dvd_mul_right have : 2 ∣ m.gcd n := by apply Nat.dvd_gcd <;> assumption have : 2 ∣ 1 := by convert this symm exact coprime_mn norm_num at this example {m n p : ℕ} (coprime_mn : m.Coprime n) (prime_p : p.Prime) : m ^ 2 ≠ p * n ^ 2 := by intro sqr_eq have : p ∣ m := by apply prime_p.dvd_of_dvd_pow rw [sqr_eq] apply dvd_mul_right obtain ⟨k, meq⟩ := dvd_iff_exists_eq_mul_left.mp this have : p * (p * k ^ 2) = p * n ^ 2 := by rw [← sqr_eq, meq] ring have : p * k ^ 2 = n ^ 2 := by apply (mul_right_inj' _).mp this exact prime_p.ne_zero have : p ∣ n := by apply prime_p.dvd_of_dvd_pow rw [← this] apply dvd_mul_right have : p ∣ Nat.gcd m n := by apply Nat.dvd_gcd <;> assumption have : p ∣ 1 := by convert this symm exact coprime_mn have : 2 ≤ 1 := by apply prime_p.two_le.trans exact Nat.le_of_dvd zero_lt_one this norm_num at this theorem factorization_mul' {m n : ℕ} (mnez : m ≠ 0) (nnez : n ≠ 0) (p : ℕ) : (m * n).factorization p = m.factorization p + n.factorization p := by rw [Nat.factorization_mul mnez nnez] rfl theorem factorization_pow' (n k p : ℕ) : (n ^ k).factorization p = k * n.factorization p := by rw [Nat.factorization_pow] rfl theorem Nat.Prime.factorization' {p : ℕ} (prime_p : p.Prime) : p.factorization p = 1 := by rw [prime_p.factorization] simp example {m n p : ℕ} (nnz : n ≠ 0) (prime_p : p.Prime) : m ^ 2 ≠ p * n ^ 2 := by intro sqr_eq have nsqr_nez : n ^ 2 ≠ 0 := by simpa have eq1 : Nat.factorization (m ^ 2) p = 2 * m.factorization p := by rw [factorization_pow'] have eq2 : (p * n ^ 2).factorization p = 2 * n.factorization p + 1 := by rw [factorization_mul' prime_p.ne_zero nsqr_nez, prime_p.factorization', factorization_pow', add_comm] have : 2 * m.factorization p % 2 = (2 * n.factorization p + 1) % 2 := by rw [← eq1, sqr_eq, eq2] rw [add_comm, Nat.add_mul_mod_self_left, Nat.mul_mod_right] at this norm_num at this example {m n k r : ℕ} (nnz : n ≠ 0) (pow_eq : m ^ k = r * n ^ k) {p : ℕ} : k ∣ r.factorization p := by rcases r with _ | r · simp have npow_nz : n ^ k ≠ 0 := fun npowz ↦ nnz (pow_eq_zero npowz) have eq1 : (m ^ k).factorization p = k * m.factorization p := by rw [factorization_pow'] have eq2 : ((r + 1) * n ^ k).factorization p = k * n.factorization p + (r + 1).factorization p := by rw [factorization_mul' r.succ_ne_zero npow_nz, factorization_pow', add_comm] have : r.succ.factorization p = k * m.factorization p - k * n.factorization p := by rw [← eq1, pow_eq, eq2, add_comm, Nat.add_sub_cancel] rw [this] apply Nat.dvd_sub <;> apply Nat.dvd_mul_right
mathematics_in_lean/MIL/C06_Discrete_Mathematics/S02_Counting_Arguments.lean
import Mathlib.Data.Fintype.BigOperators import Mathlib.Combinatorics.Pigeonhole import Mathlib.Tactic open Finset variable {α β : Type*} [DecidableEq α] [DecidableEq β] (s t : Finset α) (f : α → β) example : #(s ×ˢ t) = #s * #t := by rw [card_product] example : #(s ×ˢ t) = #s * #t := by simp example : #(s ∪ t) = #s + #t - #(s ∩ t) := by rw [card_union] example (h : Disjoint s t) : #(s ∪ t) = #s + #t := by rw [card_union_of_disjoint h] example (h : Disjoint s t) : #(s ∪ t) = #s + #t := by simp [h] example (h : Function.Injective f) : #(s.image f) = #s := by rw [card_image_of_injective _ h] example (h : Set.InjOn f s) : #(s.image f) = #s := by rw [card_image_of_injOn h] section open Fintype variable {α β : Type*} [Fintype α] [Fintype β] example : card (α × β) = card α * card β := by simp example : card (α ⊕ β) = card α + card β := by simp example (n : ℕ) : card (Fin n → α) = (card α)^n := by simp variable {n : ℕ} {γ : Fin n → Type*} [∀ i, Fintype (γ i)] example : card ((i : Fin n) → γ i) = ∏ i, card (γ i) := by simp example : card (Σ i, γ i) = ∑ i, card (γ i) := by simp end #check Disjoint example (m n : ℕ) (h : m ≥ n) : card (range n ∪ (range n).image (fun i ↦ m + i)) = 2 * n := by rw [card_union_of_disjoint, card_range, card_image_of_injective, card_range]; omega . apply add_right_injective . simp [disjoint_iff_ne]; omega def triangle (n : ℕ) : Finset (ℕ × ℕ) := {p ∈ range (n+1) ×ˢ range (n+1) | p.1 < p.2} example (n : ℕ) : #(triangle n) = (n + 1) * n / 2 := by have : triangle n = (range (n+1)).biUnion (fun j ↦ (range j).image (., j)) := by ext p simp only [triangle, mem_filter, mem_product, mem_range, mem_biUnion, mem_image] constructor . rintro ⟨⟨hp1, hp2⟩, hp3⟩ use p.2, hp2, p.1, hp3 . rintro ⟨p1, hp1, p2, hp2, rfl⟩ omega rw [this, card_biUnion]; swap · -- take care of disjointness first intro x _ y _ xney simp [disjoint_iff_ne, xney] -- continue the calculation transitivity (∑ i ∈ range (n + 1), i) · congr; ext i rw [card_image_of_injective, card_range] intros i1 i2; simp rw [sum_range_id]; rfl example (n : ℕ) : #(triangle n) = (n + 1) * n / 2 := by have : triangle n ≃ Σ i : Fin (n + 1), Fin i.val := { toFun := by rintro ⟨⟨i, j⟩, hp⟩ simp [triangle] at hp exact ⟨⟨j, hp.1.2⟩, ⟨i, hp.2⟩⟩ invFun := by rintro ⟨i, j⟩ use ⟨j, i⟩ simp [triangle] exact j.isLt.trans i.isLt left_inv := by intro i; rfl right_inv := by intro i; rfl } rw [←Fintype.card_coe] trans; apply (Fintype.card_congr this) rw [Fintype.card_sigma, sum_fin_eq_sum_range] convert Finset.sum_range_id (n + 1) simp_all example (n : ℕ) : #(triangle n) = (n + 1) * n / 2 := by apply Nat.eq_div_of_mul_eq_right (by norm_num) let turn (p : ℕ × ℕ) : ℕ × ℕ := (n - 1 - p.1, n - p.2) calc 2 * #(triangle n) = #(triangle n) + #(triangle n) := by sorry _ = #(triangle n) + #(triangle n |>.image turn) := by sorry _ = #(range n ×ˢ range (n + 1)) := by sorry _ = (n + 1) * n := by sorry def triangle' (n : ℕ) : Finset (ℕ × ℕ) := {p ∈ range n ×ˢ range n | p.1 ≤ p.2} example (n : ℕ) : #(triangle' n) = #(triangle n) := by sorry section open Classical variable (s t : Finset Nat) (a b : Nat) theorem doubleCounting {α β : Type*} (s : Finset α) (t : Finset β) (r : α → β → Prop) (h_left : ∀ a ∈ s, 3 ≤ #{b ∈ t | r a b}) (h_right : ∀ b ∈ t, #{a ∈ s | r a b} ≤ 1) : 3 * #(s) ≤ #(t) := by calc 3 * #(s) = ∑ a ∈ s, 3 := by simp [sum_const_nat, mul_comm] _ ≤ ∑ a ∈ s, #({b ∈ t | r a b}) := sum_le_sum h_left _ = ∑ a ∈ s, ∑ b ∈ t, if r a b then 1 else 0 := by simp _ = ∑ b ∈ t, ∑ a ∈ s, if r a b then 1 else 0 := sum_comm _ = ∑ b ∈ t, #({a ∈ s | r a b}) := by simp _ ≤ ∑ b ∈ t, 1 := sum_le_sum h_right _ ≤ #(t) := by simp example (m k : ℕ) (h : m ≠ k) (h' : m / 2 = k / 2) : m = k + 1 ∨ k = m + 1 := by omega example {n : ℕ} (A : Finset ℕ) (hA : #(A) = n + 1) (hA' : A ⊆ range (2 * n)) : ∃ m ∈ A, ∃ k ∈ A, Nat.Coprime m k := by have : ∃ t ∈ range n, 1 < #({u ∈ A | u / 2 = t}) := by apply exists_lt_card_fiber_of_mul_lt_card_of_maps_to · sorry · sorry rcases this with ⟨t, ht, ht'⟩ simp only [one_lt_card, mem_filter] at ht' sorry
mathematics_in_lean/MIL/C06_Discrete_Mathematics/S03_Inductive_Structures.lean
import Mathlib.Tactic namespace MyListSpace inductive List (α : Type*) where | nil : List α | cons : α → List α → List α end MyListSpace namespace MyListSpace2 def append {α : Type*} : List α → List α → List α | [], bs => bs | a :: as, bs => a :: (append as bs) def map {α β : Type*} (f : α → β) : List α → List β | [] => [] | a :: as => f a :: map f as #eval append [1, 2, 3] [4, 5, 6] #eval map (fun n => n^2) [1, 2, 3, 4, 5] theorem nil_append {α : Type*} (as : List α) : append [] as = as := rfl theorem cons_append {α : Type*} (a : α) (as : List α) (bs : List α) : append (a :: as) bs = a :: (append as bs) := rfl theorem map_nil {α β : Type*} (f : α → β) : map f [] = [] := rfl theorem map_cons {α β : Type*} (f : α → β) (a : α) (as : List α) : map f (a :: as) = f a :: map f as := rfl end MyListSpace2 namespace MyListSpace3 variable {α β γ : Type*} variable (as bs cs : List α) variable (a b c : α) open List theorem append_nil : ∀ as : List α, as ++ [] = as | [] => rfl | a :: as => by rw [cons_append, append_nil as] theorem map_map (f : α → β) (g : β → γ) : ∀ as : List α, map g (map f as) = map (g ∘ f) as | [] => rfl | a :: as => by rw [map_cons, map_cons, map_cons, map_map f g as]; rfl theorem append_nil' : as ++ [] = as := by induction' as with a as ih . rfl . rw [cons_append, ih] theorem map_map' (f : α → β) (g : β → γ) (as : List α) : map g (map f as) = map (g ∘ f) as := by induction' as with a as ih . rfl . simp [map, ih] def reverse : List α → List α := sorry theorem reverse_append (as bs : List α) : reverse (as ++ bs) = reverse bs ++ reverse as := by sorry theorem reverse_reverse (as : List α) : reverse (reverse as) = as := by sorry end MyListSpace3 inductive BinTree where | empty : BinTree | node : BinTree → BinTree → BinTree namespace BinTree def size : BinTree → ℕ | empty => 0 | node l r => size l + size r + 1 def depth : BinTree → ℕ | empty => 0 | node l r => max (depth l) (depth r) + 1 theorem size_le : ∀ t : BinTree, size t ≤ 2^depth t - 1 | empty => Nat.zero_le _ | node l r => by simp only [depth, size] calc l.size + r.size + 1 ≤ (2^l.depth - 1) + (2^r.depth - 1) + 1 := by gcongr <;> apply size_le _ ≤ (2 ^ max l.depth r.depth - 1) + (2 ^ max l.depth r.depth - 1) + 1 := by gcongr <;> simp _ ≤ 2 ^ (max l.depth r.depth + 1) - 1 := by have : 0 < 2 ^ max l.depth r.depth := by simp omega theorem depth_le_size : ∀ t : BinTree, depth t ≤ size t := by sorry def flip : BinTree → BinTree := sorry example: flip (node (node empty (node empty empty)) (node empty empty)) = node (node empty empty) (node (node empty empty) empty) := sorry theorem size_flip : ∀ t, size (flip t) = size t := by sorry end BinTree inductive PropForm : Type where | var (n : ℕ) : PropForm | fls : PropForm | conj (A B : PropForm) : PropForm | disj (A B : PropForm) : PropForm | impl (A B : PropForm) : PropForm namespace PropForm def eval : PropForm → (ℕ → Bool) → Bool | var n, v => v n | fls, _ => false | conj A B, v => A.eval v && B.eval v | disj A B, v => A.eval v || B.eval v | impl A B, v => ! A.eval v || B.eval v def vars : PropForm → Finset ℕ | var n => {n} | fls => ∅ | conj A B => A.vars ∪ B.vars | disj A B => A.vars ∪ B.vars | impl A B => A.vars ∪ B.vars theorem eval_eq_eval : ∀ (A : PropForm) (v1 v2 : ℕ → Bool), (∀ n ∈ A.vars, v1 n = v2 n) → A.eval v1 = A.eval v2 | var n, v1, v2, h => by simp_all [vars, eval, h] | fls, v1, v2, h => by simp_all [eval] | conj A B, v1, v2, h => by simp_all [vars, eval, eval_eq_eval A v1 v2, eval_eq_eval B v1 v2] | disj A B, v1, v2, h => by simp_all [vars, eval, eval_eq_eval A v1 v2, eval_eq_eval B v1 v2] | impl A B, v1, v2, h => by simp_all [vars, eval, eval_eq_eval A v1 v2, eval_eq_eval B v1 v2] theorem eval_eq_eval' (A : PropForm) (v1 v2 : ℕ → Bool) (h : ∀ n ∈ A.vars, v1 n = v2 n) : A.eval v1 = A.eval v2 := by cases A <;> simp_all [eval, vars, fun A => eval_eq_eval' A v1 v2] def subst : PropForm → ℕ → PropForm → PropForm | var n, m, C => if n = m then C else var n | fls, _, _ => fls | conj A B, m, C => conj (A.subst m C) (B.subst m C) | disj A B, m, C => disj (A.subst m C) (B.subst m C) | impl A B, m, C => impl (A.subst m C) (B.subst m C) theorem subst_eq_of_not_mem_vars : ∀ (A : PropForm) (n : ℕ) (C : PropForm), n ∉ A.vars → A.subst n C = A := sorry theorem subst_eval_eq : ∀ (A : PropForm) (n : ℕ) (C : PropForm) (v : ℕ → Bool), (A.subst n C).eval v = A.eval (fun m => if m = n then C.eval v else v m) := sorry end PropForm
mathematics_in_lean/MIL/C06_Discrete_Mathematics/S01_Finsets_and_Fintypes.lean
import Mathlib.Data.Finset.Powerset import Mathlib.Data.Finset.Max import Mathlib.Data.Nat.Prime.Basic import Mathlib.Data.Fintype.BigOperators section variable {α : Type*} [DecidableEq α] (a : α) (s t : Finset α) #check a ∈ s #check s ∩ t end open Finset variable (a b c : Finset ℕ) variable (n : ℕ) #check a ∩ b #check a ∪ b #check a \ b #check (∅ : Finset ℕ) example : a ∩ (b ∪ c) = (a ∩ b) ∪ (a ∩ c) := by ext x; simp only [mem_inter, mem_union]; tauto example : a ∩ (b ∪ c) = (a ∩ b) ∪ (a ∩ c) := by rw [inter_union_distrib_left] #check ({0, 2, 5} : Finset Nat) def example1 : Finset ℕ := {0, 1, 2} example : ({0, 1, 2} : Finset ℕ) = {1, 2, 0} := by decide example : ({0, 1, 2} : Finset ℕ) = {0, 1, 1, 2} := by decide example : ({0, 1} : Finset ℕ) = {1, 0} := by rw [Finset.pair_comm] example (x : Nat) : ({x, x} : Finset ℕ) = {x} := by simp example (x y z : Nat) : ({x, y, z, y, z, x} : Finset ℕ) = {x, y, z} := by ext i; simp [or_comm, or_assoc, or_left_comm] example (x y z : Nat) : ({x, y, z, y, z, x} : Finset ℕ) = {x, y, z} := by ext i; simp; tauto example (s : Finset ℕ) (a : ℕ) (h : a ∉ s) : (insert a s |>.erase a) = s := Finset.erase_insert h example (s : Finset ℕ) (a : ℕ) (h : a ∈ s) : insert a (s.erase a) = s := Finset.insert_erase h set_option pp.notation false in #check ({0, 1, 2} : Finset ℕ) example : {m ∈ range n | Even m} = (range n).filter Even := rfl example : {m ∈ range n | Even m ∧ m ≠ 3} = (range n).filter (fun m ↦ Even m ∧ m ≠ 3) := rfl example : {m ∈ range 10 | Even m} = {0, 2, 4, 6, 8} := by decide #check (range 5).image (fun x ↦ x * 2) example : (range 5).image (fun x ↦ x * 2) = {x ∈ range 10 | Even x} := by decide section variable (s t : Finset Nat) #check s ×ˢ t #check s.powerset end namespace finsets_and_fintypes #check Finset.fold def f (n : ℕ) : Int := (↑n)^2 #check (range 5).fold (fun x y : Int ↦ x + y) 0 f #eval (range 5).fold (fun x y : Int ↦ x + y) 0 f #check ∑ i ∈ range 5, i^2 #check ∏ i ∈ range 5, i + 1 variable (g : Nat → Finset Int) #check (range 5).biUnion g end finsets_and_fintypes #check Finset.induction example {α : Type*} [DecidableEq α] (f : α → ℕ) (s : Finset α) (h : ∀ x ∈ s, f x ≠ 0) : ∏ x ∈ s, f x ≠ 0 := by induction s using Finset.induction_on with | empty => simp | @insert a s anins ih => rw [prod_insert anins] apply mul_ne_zero · apply h; apply mem_insert_self apply ih intros x xs exact h x (mem_insert_of_mem xs) noncomputable example (s : Finset ℕ) (h : s.Nonempty) : ℕ := Classical.choose h example (s : Finset ℕ) (h : s.Nonempty) : Classical.choose h ∈ s := Classical.choose_spec h noncomputable example (s : Finset ℕ) : List ℕ := s.toList example (s : Finset ℕ) (a : ℕ) : a ∈ s.toList ↔ a ∈ s := mem_toList #check Finset.min #check Finset.min' #check Finset.max #check Finset.max' #check Finset.inf #check Finset.inf' #check Finset.sup #check Finset.sup' example : Finset.Nonempty {2, 6, 7} := ⟨6, by trivial⟩ example : Finset.min' {2, 6, 7} ⟨6, by trivial⟩ = 2 := by trivial #check Finset.card #eval (range 5).card example (s : Finset ℕ) : s.card = #s := by rfl example (s : Finset ℕ) : s.card = ∑ i ∈ s, 1 := by rw [card_eq_sum_ones] example (s : Finset ℕ) : s.card = ∑ i ∈ s, 1 := by simp section variable {α : Type*} [Fintype α] example : ∀ x : α, x ∈ Finset.univ := by intro x; exact mem_univ x example : Fintype.card α = (Finset.univ : Finset α).card := rfl end example : Fintype.card (Fin 5) = 5 := by simp example : Fintype.card ((Fin 5) × (Fin 3)) = 15 := by simp section variable (s : Finset ℕ) example : (↑s : Type) = {x : ℕ // x ∈ s} := rfl example : Fintype.card ↑s = s.card := by simp end
mathematics_in_lean/MIL/C06_Discrete_Mathematics/solutions/Solutions_S02_Counting_Arguments.lean
import Mathlib.Data.Fintype.BigOperators import Mathlib.Combinatorics.Pigeonhole import Mathlib.Tactic open Finset def triangle (n : ℕ) : Finset (ℕ × ℕ) := {p ∈ range (n+1) ×ˢ range (n+1) | p.1 < p.2} example (n : ℕ) : #(triangle n) = (n + 1) * n / 2 := by apply Nat.eq_div_of_mul_eq_right (by norm_num) let turn (p : ℕ × ℕ) : ℕ × ℕ := (n - 1 - p.1, n - p.2) calc 2 * #(triangle n) = #(triangle n) + #(triangle n) := by ring _ = #(triangle n) + #(triangle n |>.image turn) := by rw [Finset.card_image_of_injOn] rintro ⟨p1, p2⟩ hp ⟨q1, q2⟩ hq; simp [turn] simp_all [triangle]; omega _ = #(range n ×ˢ range (n + 1)) := by rw [←Finset.card_union_of_disjoint]; swap . rw [Finset.disjoint_iff_ne] rintro ⟨p1, p2⟩ hp ⟨q1, q2⟩ hq; simp [turn] at * simp_all [triangle]; omega congr; ext p; rcases p with ⟨p1, p2⟩ simp [triangle, turn] constructor . rintro (h | h) <;> omega rcases Nat.lt_or_ge p1 p2 with h | h . omega . intro h' right use n - 1 - p1, n - p2 omega _ = (n + 1) * n := by simp [mul_comm] def triangle' (n : ℕ) : Finset (ℕ × ℕ) := {p ∈ range n ×ˢ range n | p.1 ≤ p.2} example (n : ℕ) : #(triangle' n) = #(triangle n) := by let f (p : ℕ × ℕ) : ℕ × ℕ := (p.1, p.2 + 1) have : triangle n = (triangle' n |>.image f) := by ext p; rcases p with ⟨p1, p2⟩ simp [triangle, triangle', f] constructor . intro h use p1, p2 - 1 omega . simp; omega rw [this, card_image_of_injOn] rintro ⟨p1, p2⟩ hp ⟨q1, q2⟩ hq; simp [f] at * example {n : ℕ} (A : Finset ℕ) (hA : #(A) = n + 1) (hA' : A ⊆ range (2 * n)) : ∃ m ∈ A, ∃ k ∈ A, Nat.Coprime m k := by have : ∃ t ∈ range n, 1 < #({u ∈ A | u / 2 = t}) := by apply exists_lt_card_fiber_of_mul_lt_card_of_maps_to · intro u hu specialize hA' hu simp only [mem_range] at * exact Nat.div_lt_of_lt_mul hA' · simp [hA] rcases this with ⟨t, ht, ht'⟩ simp only [one_lt_card, mem_filter] at ht' rcases ht' with ⟨m, ⟨hm, hm'⟩, k, ⟨hk, hk'⟩, hmk⟩ have : m = k + 1 ∨ k = m + 1 := by omega rcases this with rfl | rfl . use k, hk, k+1, hm; simp . use m, hm, m+1, hk; simp
mathematics_in_lean/MIL/C06_Discrete_Mathematics/solutions/Solutions_S03_Inductive_Structures.lean
import Mathlib.Tactic namespace MyListSpace3 variable {α β γ : Type*} variable (as bs cs : List α) variable (a b c : α) open List def reverse : List α → List α | [] => [] | a :: as => reverse as ++ [a] theorem reverse_append (as bs : List α) : reverse (as ++ bs) = reverse bs ++ reverse as := by induction' as with a as ih . rw [nil_append, reverse, append_nil] rw [cons_append, reverse, ih, reverse, append_assoc] theorem reverse_reverse (as : List α) : reverse (reverse as) = as := by induction' as with a as ih . rfl rw [reverse, reverse_append, ih, reverse, reverse, nil_append, cons_append, nil_append] end MyListSpace3 inductive BinTree where | empty : BinTree | node : BinTree → BinTree → BinTree namespace BinTree def size : BinTree → ℕ | empty => 0 | node l r => size l + size r + 1 def depth : BinTree → ℕ | empty => 0 | node l r => max (depth l) (depth r) + 1 theorem depth_le_size : ∀ t : BinTree, depth t ≤ size t | BinTree.empty => Nat.zero_le _ | BinTree.node l r => by simp only [depth, size, add_le_add_iff_right, max_le_iff] constructor . apply le_add_right apply depth_le_size . apply le_add_left apply depth_le_size def flip : BinTree → BinTree | empty => empty | node l r => node (flip r) (flip l) example: flip (node (node empty (node empty empty)) (node empty empty)) = node (node empty empty) (node (node empty empty) empty) := rfl theorem size_flip : ∀ t, size (flip t) = size t | empty => rfl | node l r => by dsimp [size, flip] rw [size_flip l, size_flip r]; omega end BinTree inductive PropForm : Type where | var (n : ℕ) : PropForm | fls : PropForm | conj (A B : PropForm) : PropForm | disj (A B : PropForm) : PropForm | impl (A B : PropForm) : PropForm namespace PropForm def eval : PropForm → (ℕ → Bool) → Bool | var n, v => v n | fls, _ => false | conj A B, v => A.eval v && B.eval v | disj A B, v => A.eval v || B.eval v | impl A B, v => ! A.eval v || B.eval v def vars : PropForm → Finset ℕ | var n => {n} | fls => ∅ | conj A B => A.vars ∪ B.vars | disj A B => A.vars ∪ B.vars | impl A B => A.vars ∪ B.vars def subst : PropForm → ℕ → PropForm → PropForm | var n, m, C => if n = m then C else var n | fls, _, _ => fls | conj A B, m, C => conj (A.subst m C) (B.subst m C) | disj A B, m, C => disj (A.subst m C) (B.subst m C) | impl A B, m, C => impl (A.subst m C) (B.subst m C) theorem subst_eq_of_not_mem_vars : ∀ (A : PropForm) (n : ℕ) (C : PropForm), n ∉ A.vars → A.subst n C = A | var m, n, C, h => by simp_all [subst, vars]; tauto | fls, n, C, _ => by rw [subst] | conj A B, n, C, h => by simp_all [subst, vars, subst_eq_of_not_mem_vars A, subst_eq_of_not_mem_vars B] | disj A B, n, C, h => by simp_all [subst, vars, subst_eq_of_not_mem_vars A, subst_eq_of_not_mem_vars B] | impl A B, n, C, h => by simp_all [subst, vars, subst_eq_of_not_mem_vars A, subst_eq_of_not_mem_vars B] -- alternative proof: theorem subst_eq_of_not_mem_vars' (A : PropForm) (n : ℕ) (C : PropForm): n ∉ A.vars → A.subst n C = A := by cases A <;> simp_all [subst, vars, subst_eq_of_not_mem_vars']; tauto theorem subst_eval_eq : ∀ (A : PropForm) (n : ℕ) (C : PropForm) (v : ℕ → Bool), (A.subst n C).eval v = A.eval (fun m => if m = n then C.eval v else v m) | var m, n, C, v => by simp [subst, eval] split <;> simp [eval] | fls, n, C, v => by simp [subst, eval] | conj A B, n, C, v => by simp [subst, eval, subst_eval_eq A n C v, subst_eval_eq B n C v] | disj A B, n, C, v => by simp [subst, eval, subst_eval_eq A n C v, subst_eval_eq B n C v] | impl A B, n, C, v => by simp [subst, eval, subst_eval_eq A n C v, subst_eval_eq B n C v] -- alternative proof: theorem subst_eval_eq' (A : PropForm) (n : ℕ) (C : PropForm) (v : ℕ → Bool) : (A.subst n C).eval v = A.eval (fun m => if m = n then C.eval v else v m) := by cases A <;> simp [subst, eval, subst_eval_eq']; split <;> simp_all [eval] end PropForm
mathematics_in_lean/MIL/C06_Discrete_Mathematics/solutions/Solutions_S01_Finsets_and_Fintypes.lean
import Mathlib.Data.Finset.Powerset import Mathlib.Data.Finset.Max import Mathlib.Data.Nat.Prime.Basic import Mathlib.Data.Fintype.BigOperators
mathematics_in_lean/MIL/C11_Topology/S03_Topological_Spaces.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas import Mathlib.Analysis.Normed.Operator.BanachSteinhaus open Set Filter Topology section variable {X : Type*} [TopologicalSpace X] example : IsOpen (univ : Set X) := isOpen_univ example : IsOpen (∅ : Set X) := isOpen_empty example {ι : Type*} {s : ι → Set X} (hs : ∀ i, IsOpen (s i)) : IsOpen (⋃ i, s i) := isOpen_iUnion hs example {ι : Type*} [Fintype ι] {s : ι → Set X} (hs : ∀ i, IsOpen (s i)) : IsOpen (⋂ i, s i) := isOpen_iInter_of_finite hs variable {Y : Type*} [TopologicalSpace Y] example {f : X → Y} : Continuous f ↔ ∀ s, IsOpen s → IsOpen (f ⁻¹' s) := continuous_def example {f : X → Y} {x : X} : ContinuousAt f x ↔ map f (𝓝 x) ≤ 𝓝 (f x) := Iff.rfl example {f : X → Y} {x : X} : ContinuousAt f x ↔ ∀ U ∈ 𝓝 (f x), ∀ᶠ x in 𝓝 x, f x ∈ U := Iff.rfl example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ t, t ⊆ s ∧ IsOpen t ∧ x ∈ t := mem_nhds_iff example (x : X) : pure x ≤ 𝓝 x := pure_le_nhds x example (x : X) (P : X → Prop) (h : ∀ᶠ y in 𝓝 x, P y) : P x := h.self_of_nhds example {P : X → Prop} {x : X} (h : ∀ᶠ y in 𝓝 x, P y) : ∀ᶠ y in 𝓝 x, ∀ᶠ z in 𝓝 y, P z := eventually_eventually_nhds.mpr h #check TopologicalSpace.mkOfNhds #check TopologicalSpace.nhds_mkOfNhds example {α : Type*} (n : α → Filter α) (H₀ : ∀ a, pure a ≤ n a) (H : ∀ a : α, ∀ p : α → Prop, (∀ᶠ x in n a, p x) → ∀ᶠ y in n a, ∀ᶠ x in n y, p x) : ∀ a, ∀ s ∈ n a, ∃ t ∈ n a, t ⊆ s ∧ ∀ a' ∈ t, s ∈ n a' := by sorry end variable {X Y : Type*} example (f : X → Y) : TopologicalSpace X → TopologicalSpace Y := TopologicalSpace.coinduced f example (f : X → Y) : TopologicalSpace Y → TopologicalSpace X := TopologicalSpace.induced f example (f : X → Y) (T_X : TopologicalSpace X) (T_Y : TopologicalSpace Y) : TopologicalSpace.coinduced f T_X ≤ T_Y ↔ T_X ≤ TopologicalSpace.induced f T_Y := coinduced_le_iff_le_induced #check coinduced_compose #check induced_compose example {T T' : TopologicalSpace X} : T ≤ T' ↔ ∀ s, T'.IsOpen s → T.IsOpen s := Iff.rfl example (T_X : TopologicalSpace X) (T_Y : TopologicalSpace Y) (f : X → Y) : Continuous f ↔ TopologicalSpace.coinduced f T_X ≤ T_Y := continuous_iff_coinduced_le example {Z : Type*} (f : X → Y) (T_X : TopologicalSpace X) (T_Z : TopologicalSpace Z) (g : Y → Z) : @Continuous Y Z (TopologicalSpace.coinduced f T_X) T_Z g ↔ @Continuous X Z T_X T_Z (g ∘ f) := by rw [continuous_iff_coinduced_le, coinduced_compose, continuous_iff_coinduced_le] example (ι : Type*) (X : ι → Type*) (T_X : ∀ i, TopologicalSpace (X i)) : (Pi.topologicalSpace : TopologicalSpace (∀ i, X i)) = ⨅ i, TopologicalSpace.induced (fun x ↦ x i) (T_X i) := rfl example [TopologicalSpace X] [T2Space X] {u : ℕ → X} {a b : X} (ha : Tendsto u atTop (𝓝 a)) (hb : Tendsto u atTop (𝓝 b)) : a = b := tendsto_nhds_unique ha hb example [TopologicalSpace X] [RegularSpace X] (a : X) : (𝓝 a).HasBasis (fun s : Set X ↦ s ∈ 𝓝 a ∧ IsClosed s) id := closed_nhds_basis a example [TopologicalSpace X] {x : X} : (𝓝 x).HasBasis (fun t : Set X ↦ t ∈ 𝓝 x ∧ IsOpen t) id := nhds_basis_opens' x theorem aux {X Y A : Type*} [TopologicalSpace X] {c : A → X} {f : A → Y} {x : X} {F : Filter Y} (h : Tendsto f (comap c (𝓝 x)) F) {V' : Set Y} (V'_in : V' ∈ F) : ∃ V ∈ 𝓝 x, IsOpen V ∧ c ⁻¹' V ⊆ f ⁻¹' V' := by sorry example [TopologicalSpace X] [TopologicalSpace Y] [T3Space Y] {A : Set X} (hA : ∀ x, x ∈ closure A) {f : A → Y} (f_cont : Continuous f) (hf : ∀ x : X, ∃ c : Y, Tendsto f (comap (↑) (𝓝 x)) (𝓝 c)) : ∃ φ : X → Y, Continuous φ ∧ ∀ a : A, φ a = f a := by sorry #check HasBasis.tendsto_right_iff example [TopologicalSpace X] [FirstCountableTopology X] {s : Set X} {a : X} : a ∈ closure s ↔ ∃ u : ℕ → X, (∀ n, u n ∈ s) ∧ Tendsto u atTop (𝓝 a) := mem_closure_iff_seq_limit variable [TopologicalSpace X] example {F : Filter X} {x : X} : ClusterPt x F ↔ NeBot (𝓝 x ⊓ F) := Iff.rfl example {s : Set X} : IsCompact s ↔ ∀ (F : Filter X) [NeBot F], F ≤ 𝓟 s → ∃ a ∈ s, ClusterPt a F := Iff.rfl example [FirstCountableTopology X] {s : Set X} {u : ℕ → X} (hs : IsCompact s) (hu : ∀ n, u n ∈ s) : ∃ a ∈ s, ∃ φ : ℕ → ℕ, StrictMono φ ∧ Tendsto (u ∘ φ) atTop (𝓝 a) := hs.tendsto_subseq hu variable [TopologicalSpace Y] example {x : X} {F : Filter X} {G : Filter Y} (H : ClusterPt x F) {f : X → Y} (hfx : ContinuousAt f x) (hf : Tendsto f F G) : ClusterPt (f x) G := ClusterPt.map H hfx hf example [TopologicalSpace Y] {f : X → Y} (hf : Continuous f) {s : Set X} (hs : IsCompact s) : IsCompact (f '' s) := by intro F F_ne F_le have map_eq : map f (𝓟 s ⊓ comap f F) = 𝓟 (f '' s) ⊓ F := by sorry have Hne : (𝓟 s ⊓ comap f F).NeBot := by sorry have Hle : 𝓟 s ⊓ comap f F ≤ 𝓟 s := inf_le_left sorry example {ι : Type*} {s : Set X} (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_finite_subcover U hUo hsU
mathematics_in_lean/MIL/C11_Topology/S01_Filters.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas open Set Filter Topology def principal {α : Type*} (s : Set α) : Filter α where sets := { t | s ⊆ t } univ_sets := sorry sets_of_superset := sorry inter_sets := sorry example : Filter ℕ := { sets := { s | ∃ a, ∀ b, a ≤ b → b ∈ s } univ_sets := sorry sets_of_superset := sorry inter_sets := sorry } def Tendsto₁ {X Y : Type*} (f : X → Y) (F : Filter X) (G : Filter Y) := ∀ V ∈ G, f ⁻¹' V ∈ F def Tendsto₂ {X Y : Type*} (f : X → Y) (F : Filter X) (G : Filter Y) := map f F ≤ G example {X Y : Type*} (f : X → Y) (F : Filter X) (G : Filter Y) : Tendsto₂ f F G ↔ Tendsto₁ f F G := Iff.rfl #check (@Filter.map_mono : ∀ {α β} {m : α → β}, Monotone (map m)) #check (@Filter.map_map : ∀ {α β γ} {f : Filter α} {m : α → β} {m' : β → γ}, map m' (map m f) = map (m' ∘ m) f) example {X Y Z : Type*} {F : Filter X} {G : Filter Y} {H : Filter Z} {f : X → Y} {g : Y → Z} (hf : Tendsto₁ f F G) (hg : Tendsto₁ g G H) : Tendsto₁ (g ∘ f) F H := sorry variable (f : ℝ → ℝ) (x₀ y₀ : ℝ) #check comap ((↑) : ℚ → ℝ) (𝓝 x₀) #check Tendsto (f ∘ (↑)) (comap ((↑) : ℚ → ℝ) (𝓝 x₀)) (𝓝 y₀) section variable {α β γ : Type*} (F : Filter α) {m : γ → β} {n : β → α} #check (comap_comap : comap m (comap n F) = comap (n ∘ m) F) end example : 𝓝 (x₀, y₀) = 𝓝 x₀ ×ˢ 𝓝 y₀ := nhds_prod_eq #check le_inf_iff example (f : ℕ → ℝ × ℝ) (x₀ y₀ : ℝ) : Tendsto f atTop (𝓝 (x₀, y₀)) ↔ Tendsto (Prod.fst ∘ f) atTop (𝓝 x₀) ∧ Tendsto (Prod.snd ∘ f) atTop (𝓝 y₀) := sorry example (x₀ : ℝ) : HasBasis (𝓝 x₀) (fun ε : ℝ ↦ 0 < ε) fun ε ↦ Ioo (x₀ - ε) (x₀ + ε) := nhds_basis_Ioo_pos x₀ example (u : ℕ → ℝ) (x₀ : ℝ) : Tendsto u atTop (𝓝 x₀) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, u n ∈ Ioo (x₀ - ε) (x₀ + ε) := by have : atTop.HasBasis (fun _ : ℕ ↦ True) Ici := atTop_basis rw [this.tendsto_iff (nhds_basis_Ioo_pos x₀)] simp example (P Q : ℕ → Prop) (hP : ∀ᶠ n in atTop, P n) (hQ : ∀ᶠ n in atTop, Q n) : ∀ᶠ n in atTop, P n ∧ Q n := hP.and hQ example (u v : ℕ → ℝ) (h : ∀ᶠ n in atTop, u n = v n) (x₀ : ℝ) : Tendsto u atTop (𝓝 x₀) ↔ Tendsto v atTop (𝓝 x₀) := tendsto_congr' h example (u v : ℕ → ℝ) (h : u =ᶠ[atTop] v) (x₀ : ℝ) : Tendsto u atTop (𝓝 x₀) ↔ Tendsto v atTop (𝓝 x₀) := tendsto_congr' h #check Eventually.of_forall #check Eventually.mono #check Eventually.and example (P Q R : ℕ → Prop) (hP : ∀ᶠ n in atTop, P n) (hQ : ∀ᶠ n in atTop, Q n) (hR : ∀ᶠ n in atTop, P n ∧ Q n → R n) : ∀ᶠ n in atTop, R n := by apply (hP.and (hQ.and hR)).mono rintro n ⟨h, h', h''⟩ exact h'' ⟨h, h'⟩ example (P Q R : ℕ → Prop) (hP : ∀ᶠ n in atTop, P n) (hQ : ∀ᶠ n in atTop, Q n) (hR : ∀ᶠ n in atTop, P n ∧ Q n → R n) : ∀ᶠ n in atTop, R n := by filter_upwards [hP, hQ, hR] with n h h' h'' exact h'' ⟨h, h'⟩ #check mem_closure_iff_clusterPt #check le_principal_iff #check neBot_of_le example (u : ℕ → ℝ) (M : Set ℝ) (x : ℝ) (hux : Tendsto u atTop (𝓝 x)) (huM : ∀ᶠ n in atTop, u n ∈ M) : x ∈ closure M := sorry
mathematics_in_lean/MIL/C11_Topology/S02_Metric_Spaces.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas import Mathlib.Analysis.Normed.Operator.BanachSteinhaus open Set Filter open Topology Filter variable {X : Type*} [MetricSpace X] (a b c : X) #check (dist a b : ℝ) #check (dist_nonneg : 0 ≤ dist a b) #check (dist_eq_zero : dist a b = 0 ↔ a = b) #check (dist_comm a b : dist a b = dist b a) #check (dist_triangle a b c : dist a c ≤ dist a b + dist b c) -- Note the next three lines are not quoted, their purpose is to make sure those things don't get renamed while we're looking elsewhere. #check EMetricSpace #check PseudoMetricSpace #check PseudoEMetricSpace example {u : ℕ → X} {a : X} : Tendsto u atTop (𝓝 a) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, dist (u n) a < ε := Metric.tendsto_atTop example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} : Continuous f ↔ ∀ x : X, ∀ ε > 0, ∃ δ > 0, ∀ x', dist x' x < δ → dist (f x') (f x) < ε := Metric.continuous_iff example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := by continuity example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := continuous_dist.comp ((hf.comp continuous_fst).prodMk (hf.comp continuous_snd)) example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := by apply Continuous.dist exact hf.comp continuous_fst exact hf.comp continuous_snd example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := (hf.comp continuous_fst).dist (hf.comp continuous_snd) example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := hf.fst'.dist hf.snd' example {f : ℝ → X} (hf : Continuous f) : Continuous fun x : ℝ ↦ f (x ^ 2 + x) := sorry example {X Y : Type*} [MetricSpace X] [MetricSpace Y] (f : X → Y) (a : X) : ContinuousAt f a ↔ ∀ ε > 0, ∃ δ > 0, ∀ {x}, dist x a < δ → dist (f x) (f a) < ε := Metric.continuousAt_iff variable (r : ℝ) example : Metric.ball a r = { b | dist b a < r } := rfl example : Metric.closedBall a r = { b | dist b a ≤ r } := rfl example (hr : 0 < r) : a ∈ Metric.ball a r := Metric.mem_ball_self hr example (hr : 0 ≤ r) : a ∈ Metric.closedBall a r := Metric.mem_closedBall_self hr example (s : Set X) : IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, Metric.ball x ε ⊆ s := Metric.isOpen_iff example {s : Set X} : IsClosed s ↔ IsOpen (sᶜ) := isOpen_compl_iff.symm example {s : Set X} (hs : IsClosed s) {u : ℕ → X} (hu : Tendsto u atTop (𝓝 a)) (hus : ∀ n, u n ∈ s) : a ∈ s := hs.mem_of_tendsto hu (Eventually.of_forall hus) example {s : Set X} : a ∈ closure s ↔ ∀ ε > 0, ∃ b ∈ s, a ∈ Metric.ball b ε := Metric.mem_closure_iff example {u : ℕ → X} (hu : Tendsto u atTop (𝓝 a)) {s : Set X} (hs : ∀ n, u n ∈ s) : a ∈ closure s := by sorry example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ ε > 0, Metric.ball x ε ⊆ s := Metric.nhds_basis_ball.mem_iff example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ ε > 0, Metric.closedBall x ε ⊆ s := Metric.nhds_basis_closedBall.mem_iff example : IsCompact (Set.Icc 0 1 : Set ℝ) := isCompact_Icc example {s : Set X} (hs : IsCompact s) {u : ℕ → X} (hu : ∀ n, u n ∈ s) : ∃ a ∈ s, ∃ φ : ℕ → ℕ, StrictMono φ ∧ Tendsto (u ∘ φ) atTop (𝓝 a) := hs.tendsto_subseq hu example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X → ℝ} (hfs : ContinuousOn f s) : ∃ x ∈ s, ∀ y ∈ s, f x ≤ f y := hs.exists_isMinOn hs' hfs example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X → ℝ} (hfs : ContinuousOn f s) : ∃ x ∈ s, ∀ y ∈ s, f y ≤ f x := hs.exists_isMaxOn hs' hfs example {s : Set X} (hs : IsCompact s) : IsClosed s := hs.isClosed example {X : Type*} [MetricSpace X] [CompactSpace X] : IsCompact (univ : Set X) := isCompact_univ #check IsCompact.isClosed example {X : Type*} [MetricSpace X] {Y : Type*} [MetricSpace Y] {f : X → Y} : UniformContinuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀ {a b : X}, dist a b < δ → dist (f a) (f b) < ε := Metric.uniformContinuous_iff example {X : Type*} [MetricSpace X] [CompactSpace X] {Y : Type*} [MetricSpace Y] {f : X → Y} (hf : Continuous f) : UniformContinuous f := by sorry example (u : ℕ → X) : CauchySeq u ↔ ∀ ε > 0, ∃ N : ℕ, ∀ m ≥ N, ∀ n ≥ N, dist (u m) (u n) < ε := Metric.cauchySeq_iff example (u : ℕ → X) : CauchySeq u ↔ ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, dist (u n) (u N) < ε := Metric.cauchySeq_iff' example [CompleteSpace X] (u : ℕ → X) (hu : CauchySeq u) : ∃ x, Tendsto u atTop (𝓝 x) := cauchySeq_tendsto_of_complete hu open BigOperators open Finset theorem cauchySeq_of_le_geometric_two' {u : ℕ → X} (hu : ∀ n : ℕ, dist (u n) (u (n + 1)) ≤ (1 / 2) ^ n) : CauchySeq u := by rw [Metric.cauchySeq_iff'] intro ε ε_pos obtain ⟨N, hN⟩ : ∃ N : ℕ, 1 / 2 ^ N * 2 < ε := by sorry use N intro n hn obtain ⟨k, rfl : n = N + k⟩ := le_iff_exists_add.mp hn calc dist (u (N + k)) (u N) = dist (u (N + 0)) (u (N + k)) := sorry _ ≤ ∑ i ∈ range k, dist (u (N + i)) (u (N + (i + 1))) := sorry _ ≤ ∑ i ∈ range k, (1 / 2 : ℝ) ^ (N + i) := sorry _ = 1 / 2 ^ N * ∑ i ∈ range k, (1 / 2 : ℝ) ^ i := sorry _ ≤ 1 / 2 ^ N * 2 := sorry _ < ε := sorry open Metric example [CompleteSpace X] (f : ℕ → Set X) (ho : ∀ n, IsOpen (f n)) (hd : ∀ n, Dense (f n)) : Dense (⋂ n, f n) := by let B : ℕ → ℝ := fun n ↦ (1 / 2) ^ n have Bpos : ∀ n, 0 < B n sorry /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closedBall center radius` is included both in `f n` and in `closedBall x δ`. We can also require `radius ≤ (1/2)^(n+1)`, to ensure we get a Cauchy sequence later. -/ have : ∀ (n : ℕ) (x : X), ∀ δ > 0, ∃ y : X, ∃ r > 0, r ≤ B (n + 1) ∧ closedBall y r ⊆ closedBall x δ ∩ f n := by sorry choose! center radius Hpos HB Hball using this intro x rw [mem_closure_iff_nhds_basis nhds_basis_closedBall] intro ε εpos /- `ε` is positive. We have to find a point in the ball of radius `ε` around `x` belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closedBall (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → X × ℝ := fun n ↦ Nat.recOn n (Prod.mk x (min ε (B 0))) fun n p ↦ Prod.mk (center n p.1 p.2) (radius n p.1 p.2) let c : ℕ → X := fun n ↦ (F n).1 let r : ℕ → ℝ := fun n ↦ (F n).2 have rpos : ∀ n, 0 < r n := by sorry have rB : ∀ n, r n ≤ B n := by sorry have incl : ∀ n, closedBall (c (n + 1)) (r (n + 1)) ⊆ closedBall (c n) (r n) ∩ f n := by sorry have cdist : ∀ n, dist (c n) (c (n + 1)) ≤ B n := by sorry have : CauchySeq c := cauchySeq_of_le_geometric_two' cdist -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchySeq_tendsto_of_complete this with ⟨y, ylim⟩ -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y have I : ∀ n, ∀ m ≥ n, closedBall (c m) (r m) ⊆ closedBall (c n) (r n) := by sorry have yball : ∀ n, y ∈ closedBall (c n) (r n) := by sorry sorry
mathematics_in_lean/MIL/C11_Topology/solutions/Solutions_S03_Topological_Spaces.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas import Mathlib.Analysis.Normed.Operator.BanachSteinhaus open Set Filter Topology section variable {X : Type*} [TopologicalSpace X] example : IsOpen (univ : Set X) := isOpen_univ example : IsOpen (∅ : Set X) := isOpen_empty example {ι : Type*} {s : ι → Set X} (hs : ∀ i, IsOpen (s i)) : IsOpen (⋃ i, s i) := isOpen_iUnion hs example {ι : Type*} [Fintype ι] {s : ι → Set X} (hs : ∀ i, IsOpen (s i)) : IsOpen (⋂ i, s i) := isOpen_iInter_of_finite hs variable {Y : Type*} [TopologicalSpace Y] example {f : X → Y} : Continuous f ↔ ∀ s, IsOpen s → IsOpen (f ⁻¹' s) := continuous_def example {f : X → Y} {x : X} : ContinuousAt f x ↔ map f (𝓝 x) ≤ 𝓝 (f x) := Iff.rfl example {f : X → Y} {x : X} : ContinuousAt f x ↔ ∀ U ∈ 𝓝 (f x), ∀ᶠ x in 𝓝 x, f x ∈ U := Iff.rfl example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ t, t ⊆ s ∧ IsOpen t ∧ x ∈ t := mem_nhds_iff example (x : X) : pure x ≤ 𝓝 x := pure_le_nhds x example (x : X) (P : X → Prop) (h : ∀ᶠ y in 𝓝 x, P y) : P x := h.self_of_nhds example {P : X → Prop} {x : X} (h : ∀ᶠ y in 𝓝 x, P y) : ∀ᶠ y in 𝓝 x, ∀ᶠ z in 𝓝 y, P z := eventually_eventually_nhds.mpr h #check TopologicalSpace.mkOfNhds #check TopologicalSpace.nhds_mkOfNhds example {α : Type*} (n : α → Filter α) (H₀ : ∀ a, pure a ≤ n a) (H : ∀ a : α, ∀ p : α → Prop, (∀ᶠ x in n a, p x) → ∀ᶠ y in n a, ∀ᶠ x in n y, p x) : ∀ a, ∀ s ∈ n a, ∃ t ∈ n a, t ⊆ s ∧ ∀ a' ∈ t, s ∈ n a' := by intro a s s_in refine ⟨{ y | s ∈ n y }, H a (fun x ↦ x ∈ s) s_in, ?_, by tauto⟩ rintro y (hy : s ∈ n y) exact H₀ y hy end variable {X Y : Type*} example (f : X → Y) : TopologicalSpace X → TopologicalSpace Y := TopologicalSpace.coinduced f example (f : X → Y) : TopologicalSpace Y → TopologicalSpace X := TopologicalSpace.induced f example (f : X → Y) (T_X : TopologicalSpace X) (T_Y : TopologicalSpace Y) : TopologicalSpace.coinduced f T_X ≤ T_Y ↔ T_X ≤ TopologicalSpace.induced f T_Y := coinduced_le_iff_le_induced #check coinduced_compose #check induced_compose example {T T' : TopologicalSpace X} : T ≤ T' ↔ ∀ s, T'.IsOpen s → T.IsOpen s := Iff.rfl example (T_X : TopologicalSpace X) (T_Y : TopologicalSpace Y) (f : X → Y) : Continuous f ↔ TopologicalSpace.coinduced f T_X ≤ T_Y := continuous_iff_coinduced_le example {Z : Type*} (f : X → Y) (T_X : TopologicalSpace X) (T_Z : TopologicalSpace Z) (g : Y → Z) : @Continuous Y Z (TopologicalSpace.coinduced f T_X) T_Z g ↔ @Continuous X Z T_X T_Z (g ∘ f) := by rw [continuous_iff_coinduced_le, coinduced_compose, continuous_iff_coinduced_le] example (ι : Type*) (X : ι → Type*) (T_X : ∀ i, TopologicalSpace (X i)) : (Pi.topologicalSpace : TopologicalSpace (∀ i, X i)) = ⨅ i, TopologicalSpace.induced (fun x ↦ x i) (T_X i) := rfl example [TopologicalSpace X] [T2Space X] {u : ℕ → X} {a b : X} (ha : Tendsto u atTop (𝓝 a)) (hb : Tendsto u atTop (𝓝 b)) : a = b := tendsto_nhds_unique ha hb example [TopologicalSpace X] [RegularSpace X] (a : X) : (𝓝 a).HasBasis (fun s : Set X ↦ s ∈ 𝓝 a ∧ IsClosed s) id := closed_nhds_basis a example [TopologicalSpace X] {x : X} : (𝓝 x).HasBasis (fun t : Set X ↦ t ∈ 𝓝 x ∧ IsOpen t) id := nhds_basis_opens' x theorem aux {X Y A : Type*} [TopologicalSpace X] {c : A → X} {f : A → Y} {x : X} {F : Filter Y} (h : Tendsto f (comap c (𝓝 x)) F) {V' : Set Y} (V'_in : V' ∈ F) : ∃ V ∈ 𝓝 x, IsOpen V ∧ c ⁻¹' V ⊆ f ⁻¹' V' := by simpa [and_assoc] using ((nhds_basis_opens' x).comap c).tendsto_left_iff.mp h V' V'_in example [TopologicalSpace X] [TopologicalSpace Y] [T3Space Y] {A : Set X} (hA : ∀ x, x ∈ closure A) {f : A → Y} (f_cont : Continuous f) (hf : ∀ x : X, ∃ c : Y, Tendsto f (comap (↑) (𝓝 x)) (𝓝 c)) : ∃ φ : X → Y, Continuous φ ∧ ∀ a : A, φ a = f a := by choose φ hφ using hf use φ constructor · rw [continuous_iff_continuousAt] intro x suffices ∀ V' ∈ 𝓝 (φ x), IsClosed V' → φ ⁻¹' V' ∈ 𝓝 x by simpa [ContinuousAt, (closed_nhds_basis (φ x)).tendsto_right_iff] intro V' V'_in V'_closed obtain ⟨V, V_in, V_op, hV⟩ : ∃ V ∈ 𝓝 x, IsOpen V ∧ (↑) ⁻¹' V ⊆ f ⁻¹' V' := aux (hφ x) V'_in suffices : ∀ y ∈ V, φ y ∈ V' exact mem_of_superset V_in this intro y y_in have hVx : V ∈ 𝓝 y := V_op.mem_nhds y_in haveI : (comap ((↑) : A → X) (𝓝 y)).NeBot := by simpa [mem_closure_iff_comap_neBot] using hA y apply V'_closed.mem_of_tendsto (hφ y) exact mem_of_superset (preimage_mem_comap hVx) hV · intro a have lim : Tendsto f (𝓝 a) (𝓝 (φ a)) := by simpa [nhds_induced] using hφ a exact tendsto_nhds_unique lim f_cont.continuousAt example [TopologicalSpace X] [FirstCountableTopology X] {s : Set X} {a : X} : a ∈ closure s ↔ ∃ u : ℕ → X, (∀ n, u n ∈ s) ∧ Tendsto u atTop (𝓝 a) := mem_closure_iff_seq_limit variable [TopologicalSpace X] example {F : Filter X} {x : X} : ClusterPt x F ↔ NeBot (𝓝 x ⊓ F) := Iff.rfl example {s : Set X} : IsCompact s ↔ ∀ (F : Filter X) [NeBot F], F ≤ 𝓟 s → ∃ a ∈ s, ClusterPt a F := Iff.rfl example [FirstCountableTopology X] {s : Set X} {u : ℕ → X} (hs : IsCompact s) (hu : ∀ n, u n ∈ s) : ∃ a ∈ s, ∃ φ : ℕ → ℕ, StrictMono φ ∧ Tendsto (u ∘ φ) atTop (𝓝 a) := hs.tendsto_subseq hu variable [TopologicalSpace Y] example {x : X} {F : Filter X} {G : Filter Y} (H : ClusterPt x F) {f : X → Y} (hfx : ContinuousAt f x) (hf : Tendsto f F G) : ClusterPt (f x) G := ClusterPt.map H hfx hf example [TopologicalSpace Y] {f : X → Y} (hf : Continuous f) {s : Set X} (hs : IsCompact s) : IsCompact (f '' s) := by intro F F_ne F_le have map_eq : map f (𝓟 s ⊓ comap f F) = 𝓟 (f '' s) ⊓ F := by rw [Filter.push_pull, map_principal] have Hne : (𝓟 s ⊓ comap f F).NeBot := by apply NeBot.of_map rwa [map_eq, inf_of_le_right F_le] have Hle : 𝓟 s ⊓ comap f F ≤ 𝓟 s := inf_le_left rcases hs Hle with ⟨x, x_in, hx⟩ refine ⟨f x, mem_image_of_mem f x_in, ?_⟩ apply hx.map hf.continuousAt rw [Tendsto, map_eq] exact inf_le_right example {ι : Type*} {s : Set X} (hs : IsCompact s) (U : ι → Set X) (hUo : ∀ i, IsOpen (U i)) (hsU : s ⊆ ⋃ i, U i) : ∃ t : Finset ι, s ⊆ ⋃ i ∈ t, U i := hs.elim_finite_subcover U hUo hsU
mathematics_in_lean/MIL/C11_Topology/solutions/Solutions_S01_Filters.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas open Set Filter Topology -- In the next example we could use `tauto` in each proof instead of knowing the lemmas example {α : Type*} (s : Set α) : Filter α := { sets := { t | s ⊆ t } univ_sets := subset_univ s sets_of_superset := fun hU hUV ↦ Subset.trans hU hUV inter_sets := fun hU hV ↦ subset_inter hU hV } example : Filter ℕ := { sets := { s | ∃ a, ∀ b, a ≤ b → b ∈ s } univ_sets := by use 42 simp sets_of_superset := by rintro U V ⟨N, hN⟩ hUV use N tauto inter_sets := by rintro U V ⟨N, hN⟩ ⟨N', hN'⟩ use max N N' intro b hb rw [max_le_iff] at hb constructor <;> tauto } def Tendsto₁ {X Y : Type*} (f : X → Y) (F : Filter X) (G : Filter Y) := ∀ V ∈ G, f ⁻¹' V ∈ F example {X Y Z : Type*} {F : Filter X} {G : Filter Y} {H : Filter Z} {f : X → Y} {g : Y → Z} (hf : Tendsto₁ f F G) (hg : Tendsto₁ g G H) : Tendsto₁ (g ∘ f) F H := calc map (g ∘ f) F = map g (map f F) := by rw [map_map] _ ≤ map g G := (map_mono hf) _ ≤ H := hg example {X Y Z : Type*} {F : Filter X} {G : Filter Y} {H : Filter Z} {f : X → Y} {g : Y → Z} (hf : Tendsto₁ f F G) (hg : Tendsto₁ g G H) : Tendsto₁ (g ∘ f) F H := by intro V hV rw [preimage_comp] apply hf apply hg exact hV example (f : ℕ → ℝ × ℝ) (x₀ y₀ : ℝ) : Tendsto f atTop (𝓝 (x₀, y₀)) ↔ Tendsto (Prod.fst ∘ f) atTop (𝓝 x₀) ∧ Tendsto (Prod.snd ∘ f) atTop (𝓝 y₀) := calc Tendsto f atTop (𝓝 (x₀, y₀)) ↔ map f atTop ≤ 𝓝 (x₀, y₀) := Iff.rfl _ ↔ map f atTop ≤ 𝓝 x₀ ×ˢ 𝓝 y₀ := by rw [nhds_prod_eq] _ ↔ map f atTop ≤ comap Prod.fst (𝓝 x₀) ⊓ comap Prod.snd (𝓝 y₀) := Iff.rfl _ ↔ map f atTop ≤ comap Prod.fst (𝓝 x₀) ∧ map f atTop ≤ comap Prod.snd (𝓝 y₀) := le_inf_iff _ ↔ map Prod.fst (map f atTop) ≤ 𝓝 x₀ ∧ map Prod.snd (map f atTop) ≤ 𝓝 y₀ := by rw [← map_le_iff_le_comap, ← map_le_iff_le_comap] _ ↔ map (Prod.fst ∘ f) atTop ≤ 𝓝 x₀ ∧ map (Prod.snd ∘ f) atTop ≤ 𝓝 y₀ := by rw [map_map, map_map] -- an alternative solution example (f : ℕ → ℝ × ℝ) (x₀ y₀ : ℝ) : Tendsto f atTop (𝓝 (x₀, y₀)) ↔ Tendsto (Prod.fst ∘ f) atTop (𝓝 x₀) ∧ Tendsto (Prod.snd ∘ f) atTop (𝓝 y₀) := by rw [nhds_prod_eq] unfold Tendsto SProd.sprod Filter.instSProd rw [le_inf_iff, ← map_le_iff_le_comap, map_map, ← map_le_iff_le_comap, map_map] example (u : ℕ → ℝ) (M : Set ℝ) (x : ℝ) (hux : Tendsto u atTop (𝓝 x)) (huM : ∀ᶠ n in atTop, u n ∈ M) : x ∈ closure M := mem_closure_iff_clusterPt.mpr (neBot_of_le <| le_inf hux <| le_principal_iff.mpr huM)
mathematics_in_lean/MIL/C11_Topology/solutions/Solutions_S02_Metric_Spaces.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas import Mathlib.Analysis.Normed.Operator.BanachSteinhaus open Set Filter open Topology Filter variable {X : Type*} [MetricSpace X] (a b c : X) #check (dist a b : ℝ) #check (dist_nonneg : 0 ≤ dist a b) #check (dist_eq_zero : dist a b = 0 ↔ a = b) #check (dist_comm a b : dist a b = dist b a) #check (dist_triangle a b c : dist a c ≤ dist a b + dist b c) -- Note the next three lines are not quoted, their purpose is to make sure those things don't get renamed while we're looking elsewhere. #check EMetricSpace #check PseudoMetricSpace #check PseudoEMetricSpace example {u : ℕ → X} {a : X} : Tendsto u atTop (𝓝 a) ↔ ∀ ε > 0, ∃ N, ∀ n ≥ N, dist (u n) a < ε := Metric.tendsto_atTop example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} : Continuous f ↔ ∀ x : X, ∀ ε > 0, ∃ δ > 0, ∀ x', dist x' x < δ → dist (f x') (f x) < ε := Metric.continuous_iff example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := by continuity example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := continuous_dist.comp ((hf.comp continuous_fst).prodMk (hf.comp continuous_snd)) example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := by apply Continuous.dist exact hf.comp continuous_fst exact hf.comp continuous_snd example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := (hf.comp continuous_fst).dist (hf.comp continuous_snd) example {X Y : Type*} [MetricSpace X] [MetricSpace Y] {f : X → Y} (hf : Continuous f) : Continuous fun p : X × X ↦ dist (f p.1) (f p.2) := hf.fst'.dist hf.snd' example {f : ℝ → X} (hf : Continuous f) : Continuous fun x : ℝ ↦ f (x ^ 2 + x) := hf.comp <| (continuous_pow 2).add continuous_id example {X Y : Type*} [MetricSpace X] [MetricSpace Y] (f : X → Y) (a : X) : ContinuousAt f a ↔ ∀ ε > 0, ∃ δ > 0, ∀ {x}, dist x a < δ → dist (f x) (f a) < ε := Metric.continuousAt_iff variable (r : ℝ) example : Metric.ball a r = { b | dist b a < r } := rfl example : Metric.closedBall a r = { b | dist b a ≤ r } := rfl example (hr : 0 < r) : a ∈ Metric.ball a r := Metric.mem_ball_self hr example (hr : 0 ≤ r) : a ∈ Metric.closedBall a r := Metric.mem_closedBall_self hr example (s : Set X) : IsOpen s ↔ ∀ x ∈ s, ∃ ε > 0, Metric.ball x ε ⊆ s := Metric.isOpen_iff example {s : Set X} : IsClosed s ↔ IsOpen (sᶜ) := isOpen_compl_iff.symm example {s : Set X} (hs : IsClosed s) {u : ℕ → X} (hu : Tendsto u atTop (𝓝 a)) (hus : ∀ n, u n ∈ s) : a ∈ s := hs.mem_of_tendsto hu (Eventually.of_forall hus) example {s : Set X} : a ∈ closure s ↔ ∀ ε > 0, ∃ b ∈ s, a ∈ Metric.ball b ε := Metric.mem_closure_iff example {u : ℕ → X} (hu : Tendsto u atTop (𝓝 a)) {s : Set X} (hs : ∀ n, u n ∈ s) : a ∈ closure s := by rw [Metric.tendsto_atTop] at hu rw [Metric.mem_closure_iff] intro ε ε_pos rcases hu ε ε_pos with ⟨N, hN⟩ refine ⟨u N, hs _, ?_⟩ rw [dist_comm] exact hN N le_rfl example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ ε > 0, Metric.ball x ε ⊆ s := Metric.nhds_basis_ball.mem_iff example {x : X} {s : Set X} : s ∈ 𝓝 x ↔ ∃ ε > 0, Metric.closedBall x ε ⊆ s := Metric.nhds_basis_closedBall.mem_iff example : IsCompact (Set.Icc 0 1 : Set ℝ) := isCompact_Icc example {s : Set X} (hs : IsCompact s) {u : ℕ → X} (hu : ∀ n, u n ∈ s) : ∃ a ∈ s, ∃ φ : ℕ → ℕ, StrictMono φ ∧ Tendsto (u ∘ φ) atTop (𝓝 a) := hs.tendsto_subseq hu example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X → ℝ} (hfs : ContinuousOn f s) : ∃ x ∈ s, ∀ y ∈ s, f x ≤ f y := hs.exists_isMinOn hs' hfs example {s : Set X} (hs : IsCompact s) (hs' : s.Nonempty) {f : X → ℝ} (hfs : ContinuousOn f s) : ∃ x ∈ s, ∀ y ∈ s, f y ≤ f x := hs.exists_isMaxOn hs' hfs example {s : Set X} (hs : IsCompact s) : IsClosed s := hs.isClosed example {X : Type*} [MetricSpace X] [CompactSpace X] : IsCompact (univ : Set X) := isCompact_univ #check IsCompact.isClosed example {X : Type*} [MetricSpace X] {Y : Type*} [MetricSpace Y] {f : X → Y} : UniformContinuous f ↔ ∀ ε > 0, ∃ δ > 0, ∀ {a b : X}, dist a b < δ → dist (f a) (f b) < ε := Metric.uniformContinuous_iff example {X : Type*} [MetricSpace X] [CompactSpace X] {Y : Type*} [MetricSpace Y] {f : X → Y} (hf : Continuous f) : UniformContinuous f := by rw [Metric.uniformContinuous_iff] intro ε ε_pos let φ : X × X → ℝ := fun p ↦ dist (f p.1) (f p.2) have φ_cont : Continuous φ := hf.fst'.dist hf.snd' let K := { p : X × X | ε ≤ φ p } have K_closed : IsClosed K := isClosed_le continuous_const φ_cont have K_cpct : IsCompact K := K_closed.isCompact rcases eq_empty_or_nonempty K with hK | hK · use 1, by norm_num intro x y _ have : (x, y) ∉ K := by simp [hK] simpa [K] using this · rcases K_cpct.exists_isMinOn hK continuous_dist.continuousOn with ⟨⟨x₀, x₁⟩, xx_in, H⟩ use dist x₀ x₁ constructor · change _ < _ rw [dist_pos] intro h have : ε ≤ 0 := by simpa [K, φ, *] using xx_in linarith · intro x x' contrapose! intro (hxx' : (x, x') ∈ K) exact H hxx' example (u : ℕ → X) : CauchySeq u ↔ ∀ ε > 0, ∃ N : ℕ, ∀ m ≥ N, ∀ n ≥ N, dist (u m) (u n) < ε := Metric.cauchySeq_iff example (u : ℕ → X) : CauchySeq u ↔ ∀ ε > 0, ∃ N : ℕ, ∀ n ≥ N, dist (u n) (u N) < ε := Metric.cauchySeq_iff' example [CompleteSpace X] (u : ℕ → X) (hu : CauchySeq u) : ∃ x, Tendsto u atTop (𝓝 x) := cauchySeq_tendsto_of_complete hu open BigOperators open Finset theorem cauchySeq_of_le_geometric_two' {u : ℕ → X} (hu : ∀ n : ℕ, dist (u n) (u (n + 1)) ≤ (1 / 2) ^ n) : CauchySeq u := by rw [Metric.cauchySeq_iff'] intro ε ε_pos obtain ⟨N, hN⟩ : ∃ N : ℕ, 1 / 2 ^ N * 2 < ε := by have : Tendsto (fun N : ℕ ↦ (1 / 2 ^ N * 2 : ℝ)) atTop (𝓝 0) := by rw [← zero_mul (2 : ℝ)] apply Tendsto.mul simp_rw [← one_div_pow (2 : ℝ)] apply tendsto_pow_atTop_nhds_zero_of_lt_one <;> linarith exact tendsto_const_nhds rcases(atTop_basis.tendsto_iff (nhds_basis_Ioo_pos (0 : ℝ))).mp this ε ε_pos with ⟨N, _, hN⟩ exact ⟨N, by simpa using (hN N left_mem_Ici).2⟩ use N intro n hn obtain ⟨k, rfl : n = N + k⟩ := le_iff_exists_add.mp hn calc dist (u (N + k)) (u N) = dist (u (N + 0)) (u (N + k)) := by rw [dist_comm, add_zero] _ ≤ ∑ i ∈ range k, dist (u (N + i)) (u (N + (i + 1))) := (dist_le_range_sum_dist (fun i ↦ u (N + i)) k) _ ≤ ∑ i ∈ range k, (1 / 2 : ℝ) ^ (N + i) := (sum_le_sum fun i _ ↦ hu <| N + i) _ = 1 / 2 ^ N * ∑ i ∈ range k, (1 / 2 : ℝ) ^ i := by simp_rw [← one_div_pow, pow_add, ← mul_sum] _ ≤ 1 / 2 ^ N * 2 := (mul_le_mul_of_nonneg_left (sum_geometric_two_le _) (one_div_nonneg.mpr (pow_nonneg (zero_le_two : (0 : ℝ) ≤ 2) _))) _ < ε := hN open Metric example [CompleteSpace X] (f : ℕ → Set X) (ho : ∀ n, IsOpen (f n)) (hd : ∀ n, Dense (f n)) : Dense (⋂ n, f n) := by let B : ℕ → ℝ := fun n ↦ (1 / 2) ^ n have Bpos : ∀ n, 0 < B n := fun n ↦ pow_pos (by linarith) n /- Translate the density assumption into two functions `center` and `radius` associating to any n, x, δ, δpos a center and a positive radius such that `closedBall center radius` is included both in `f n` and in `closedBall x δ`. We can also require `radius ≤ (1/2)^(n+1)`, to ensure we get a Cauchy sequence later. -/ have : ∀ (n : ℕ) (x : X), ∀ δ > 0, ∃ y : X, ∃ r > 0, r ≤ B (n + 1) ∧ closedBall y r ⊆ closedBall x δ ∩ f n := by intro n x δ δpos have : x ∈ closure (f n) := hd n x rcases Metric.mem_closure_iff.1 this (δ / 2) (half_pos δpos) with ⟨y, ys, xy⟩ rw [dist_comm] at xy obtain ⟨r, rpos, hr⟩ : ∃ r > 0, closedBall y r ⊆ f n := nhds_basis_closedBall.mem_iff.1 (isOpen_iff_mem_nhds.1 (ho n) y ys) refine ⟨y, min (min (δ / 2) r) (B (n + 1)), ?_, ?_, fun z hz ↦ ⟨?_, ?_⟩⟩ show 0 < min (min (δ / 2) r) (B (n + 1)) exact lt_min (lt_min (half_pos δpos) rpos) (Bpos (n + 1)) show min (min (δ / 2) r) (B (n + 1)) ≤ B (n + 1) exact min_le_right _ _ show z ∈ closedBall x δ exact calc dist z x ≤ dist z y + dist y x := dist_triangle _ _ _ _ ≤ min (min (δ / 2) r) (B (n + 1)) + δ / 2 := (add_le_add hz xy.le) _ ≤ δ / 2 + δ / 2 := (add_le_add_right ((min_le_left _ _).trans (min_le_left _ _)) _) _ = δ := add_halves δ show z ∈ f n exact hr (calc dist z y ≤ min (min (δ / 2) r) (B (n + 1)) := hz _ ≤ r := (min_le_left _ _).trans (min_le_right _ _) ) choose! center radius Hpos HB Hball using this refine fun x ↦ (mem_closure_iff_nhds_basis nhds_basis_closedBall).2 fun ε εpos ↦ ?_ /- `ε` is positive. We have to find a point in the ball of radius `ε` around `x` belonging to all `f n`. For this, we construct inductively a sequence `F n = (c n, r n)` such that the closed ball `closedBall (c n) (r n)` is included in the previous ball and in `f n`, and such that `r n` is small enough to ensure that `c n` is a Cauchy sequence. Then `c n` converges to a limit which belongs to all the `f n`. -/ let F : ℕ → X × ℝ := fun n ↦ Nat.recOn n (Prod.mk x (min ε (B 0))) fun n p ↦ Prod.mk (center n p.1 p.2) (radius n p.1 p.2) let c : ℕ → X := fun n ↦ (F n).1 let r : ℕ → ℝ := fun n ↦ (F n).2 have rpos : ∀ n, 0 < r n := by intro n induction' n with n hn exact lt_min εpos (Bpos 0) exact Hpos n (c n) (r n) hn have rB : ∀ n, r n ≤ B n := by intro n induction' n with n hn exact min_le_right _ _ exact HB n (c n) (r n) (rpos n) have incl : ∀ n, closedBall (c (n + 1)) (r (n + 1)) ⊆ closedBall (c n) (r n) ∩ f n := fun n ↦ Hball n (c n) (r n) (rpos n) have cdist : ∀ n, dist (c n) (c (n + 1)) ≤ B n := by intro n rw [dist_comm] have A : c (n + 1) ∈ closedBall (c (n + 1)) (r (n + 1)) := mem_closedBall_self (rpos <| n + 1).le have I := calc closedBall (c (n + 1)) (r (n + 1)) ⊆ closedBall (c n) (r n) := (incl n).trans Set.inter_subset_left _ ⊆ closedBall (c n) (B n) := closedBall_subset_closedBall (rB n) exact I A have : CauchySeq c := cauchySeq_of_le_geometric_two' cdist -- as the sequence `c n` is Cauchy in a complete space, it converges to a limit `y`. rcases cauchySeq_tendsto_of_complete this with ⟨y, ylim⟩ -- this point `y` will be the desired point. We will check that it belongs to all -- `f n` and to `ball x ε`. use y have I : ∀ n, ∀ m ≥ n, closedBall (c m) (r m) ⊆ closedBall (c n) (r n) := by intro n refine Nat.le_induction ?_ fun m hnm h ↦ ?_ · exact Subset.rfl · exact (incl m).trans (Set.inter_subset_left.trans h) have yball : ∀ n, y ∈ closedBall (c n) (r n) := by intro n refine isClosed_closedBall.mem_of_tendsto ylim ?_ refine (Filter.eventually_ge_atTop n).mono fun m hm ↦ ?_ exact I n m hm (mem_closedBall_self (rpos _).le) constructor · suffices ∀ n, y ∈ f n by rwa [Set.mem_iInter] intro n have : closedBall (c (n + 1)) (r (n + 1)) ⊆ f n := Subset.trans (incl n) Set.inter_subset_right exact this (yball (n + 1)) calc dist y x ≤ r 0 := yball 0 _ ≤ ε := min_le_left _ _
mathematics_in_lean/MIL/C12_Differential_Calculus/S02_Differential_Calculus_in_Normed_Spaces.lean
import MIL.Common import Mathlib.Analysis.Normed.Operator.BanachSteinhaus import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Calculus.InverseFunctionTheorem.FDeriv import Mathlib.Analysis.Calculus.ContDiff.RCLike import Mathlib.Analysis.Calculus.FDeriv.Prod open Set Filter open Topology Filter noncomputable section section variable {E : Type*} [NormedAddCommGroup E] example (x : E) : 0 ≤ ‖x‖ := norm_nonneg x example {x : E} : ‖x‖ = 0 ↔ x = 0 := norm_eq_zero example (x y : E) : ‖x + y‖ ≤ ‖x‖ + ‖y‖ := norm_add_le x y example : MetricSpace E := by infer_instance example {X : Type*} [TopologicalSpace X] {f : X → E} (hf : Continuous f) : Continuous fun x ↦ ‖f x‖ := hf.norm variable [NormedSpace ℝ E] example (a : ℝ) (x : E) : ‖a • x‖ = |a| * ‖x‖ := norm_smul a x example [FiniteDimensional ℝ E] : CompleteSpace E := by infer_instance example (𝕜 : Type*) [NontriviallyNormedField 𝕜] (x y : 𝕜) : ‖x * y‖ = ‖x‖ * ‖y‖ := norm_mul x y example (𝕜 : Type*) [NontriviallyNormedField 𝕜] : ∃ x : 𝕜, 1 < ‖x‖ := NormedField.exists_one_lt_norm 𝕜 example (𝕜 : Type*) [NontriviallyNormedField 𝕜] (E : Type*) [NormedAddCommGroup E] [NormedSpace 𝕜 E] [CompleteSpace 𝕜] [FiniteDimensional 𝕜 E] : CompleteSpace E := FiniteDimensional.complete 𝕜 E end section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] example : E →L[𝕜] E := ContinuousLinearMap.id 𝕜 E example (f : E →L[𝕜] F) : E → F := f example (f : E →L[𝕜] F) : Continuous f := f.cont example (f : E →L[𝕜] F) (x y : E) : f (x + y) = f x + f y := f.map_add x y example (f : E →L[𝕜] F) (a : 𝕜) (x : E) : f (a • x) = a • f x := f.map_smul a x variable (f : E →L[𝕜] F) example (x : E) : ‖f x‖ ≤ ‖f‖ * ‖x‖ := f.le_opNorm x example {M : ℝ} (hMp : 0 ≤ M) (hM : ∀ x, ‖f x‖ ≤ M * ‖x‖) : ‖f‖ ≤ M := f.opNorm_le_bound hMp hM end section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] open Metric example {ι : Type*} [CompleteSpace E] {g : ι → E →L[𝕜] F} (h : ∀ x, ∃ C, ∀ i, ‖g i x‖ ≤ C) : ∃ C', ∀ i, ‖g i‖ ≤ C' := by -- sequence of subsets consisting of those `x : E` with norms `‖g i x‖` bounded by `n` let e : ℕ → Set E := fun n ↦ ⋂ i : ι, { x : E | ‖g i x‖ ≤ n } -- each of these sets is closed have hc : ∀ n : ℕ, IsClosed (e n) sorry -- the union is the entire space; this is where we use `h` have hU : (⋃ n : ℕ, e n) = univ sorry /- apply the Baire category theorem to conclude that for some `m : ℕ`, `e m` contains some `x` -/ obtain ⟨m, x, hx⟩ : ∃ m, ∃ x, x ∈ interior (e m) := sorry obtain ⟨ε, ε_pos, hε⟩ : ∃ ε > 0, ball x ε ⊆ interior (e m) := sorry obtain ⟨k, hk⟩ : ∃ k : 𝕜, 1 < ‖k‖ := sorry -- show all elements in the ball have norm bounded by `m` after applying any `g i` have real_norm_le : ∀ z ∈ ball x ε, ∀ (i : ι), ‖g i z‖ ≤ m sorry have εk_pos : 0 < ε / ‖k‖ := sorry refine ⟨(m + m : ℕ) / (ε / ‖k‖), fun i ↦ ContinuousLinearMap.opNorm_le_of_shell ε_pos ?_ hk ?_⟩ sorry sorry end open Asymptotics example {α : Type*} {E : Type*} [NormedGroup E] {F : Type*} [NormedGroup F] (c : ℝ) (l : Filter α) (f : α → E) (g : α → F) : IsBigOWith c l f g ↔ ∀ᶠ x in l, ‖f x‖ ≤ c * ‖g x‖ := isBigOWith_iff example {α : Type*} {E : Type*} [NormedGroup E] {F : Type*} [NormedGroup F] (l : Filter α) (f : α → E) (g : α → F) : f =O[l] g ↔ ∃ C, IsBigOWith C l f g := isBigO_iff_isBigOWith example {α : Type*} {E : Type*} [NormedGroup E] {F : Type*} [NormedGroup F] (l : Filter α) (f : α → E) (g : α → F) : f =o[l] g ↔ ∀ C > 0, IsBigOWith C l f g := isLittleO_iff_forall_isBigOWith example {α : Type*} {E : Type*} [NormedAddCommGroup E] (l : Filter α) (f g : α → E) : f ~[l] g ↔ (f - g) =o[l] g := Iff.rfl section open Topology variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] example (f : E → F) (f' : E →L[𝕜] F) (x₀ : E) : HasFDerivAt f f' x₀ ↔ (fun x ↦ f x - f x₀ - f' (x - x₀)) =o[𝓝 x₀] fun x ↦ x - x₀ := hasFDerivAtFilter_iff_isLittleO .. example (f : E → F) (f' : E →L[𝕜] F) (x₀ : E) (hff' : HasFDerivAt f f' x₀) : fderiv 𝕜 f x₀ = f' := hff'.fderiv example (n : ℕ) (f : E → F) : E → E[×n]→L[𝕜] F := iteratedFDeriv 𝕜 n f example (n : ℕ∞) {f : E → F} : ContDiff 𝕜 n f ↔ (∀ m : ℕ, (m : WithTop ℕ) ≤ n → Continuous fun x ↦ iteratedFDeriv 𝕜 m f x) ∧ ∀ m : ℕ, (m : WithTop ℕ) < n → Differentiable 𝕜 fun x ↦ iteratedFDeriv 𝕜 m f x := contDiff_iff_continuous_differentiable example {𝕂 : Type*} [RCLike 𝕂] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕂 E] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕂 F] {f : E → F} {x : E} {n : WithTop ℕ∞} (hf : ContDiffAt 𝕂 n f x) (hn : 1 ≤ n) : HasStrictFDerivAt f (fderiv 𝕂 f x) x := hf.hasStrictFDerivAt hn section LocalInverse variable [CompleteSpace E] {f : E → F} {f' : E ≃L[𝕜] F} {a : E} example (hf : HasStrictFDerivAt f (f' : E →L[𝕜] F) a) : F → E := HasStrictFDerivAt.localInverse f f' a hf example (hf : HasStrictFDerivAt f (f' : E →L[𝕜] F) a) : ∀ᶠ x in 𝓝 a, hf.localInverse f f' a (f x) = x := hf.eventually_left_inverse example (hf : HasStrictFDerivAt f (f' : E →L[𝕜] F) a) : ∀ᶠ x in 𝓝 (f a), f (hf.localInverse f f' a x) = x := hf.eventually_right_inverse example {f : E → F} {f' : E ≃L[𝕜] F} {a : E} (hf : HasStrictFDerivAt f (f' : E →L[𝕜] F) a) : HasStrictFDerivAt (HasStrictFDerivAt.localInverse f f' a hf) (f'.symm : F →L[𝕜] E) (f a) := HasStrictFDerivAt.to_localInverse hf end LocalInverse #check HasFDerivWithinAt #check HasFDerivAtFilter end
mathematics_in_lean/MIL/C12_Differential_Calculus/S01_Elementary_Differential_Calculus.lean
import MIL.Common import Mathlib.Analysis.SpecialFunctions.Trigonometric.Deriv import Mathlib.Analysis.Calculus.Deriv.Pow import Mathlib.Analysis.Calculus.MeanValue open Set Filter open Topology Filter Classical Real noncomputable section open Real /-- The sin function has derivative 1 at 0. -/ example : HasDerivAt sin 1 0 := by simpa using hasDerivAt_sin 0 example (x : ℝ) : DifferentiableAt ℝ sin x := (hasDerivAt_sin x).differentiableAt example {f : ℝ → ℝ} {x a : ℝ} (h : HasDerivAt f a x) : deriv f x = a := h.deriv example {f : ℝ → ℝ} {x : ℝ} (h : ¬DifferentiableAt ℝ f x) : deriv f x = 0 := deriv_zero_of_not_differentiableAt h example {f g : ℝ → ℝ} {x : ℝ} (hf : DifferentiableAt ℝ f x) (hg : DifferentiableAt ℝ g x) : deriv (f + g) x = deriv f x + deriv g x := deriv_add hf hg example {f : ℝ → ℝ} {a : ℝ} (h : IsLocalMin f a) : deriv f a = 0 := h.deriv_eq_zero open Set example {f : ℝ → ℝ} {a b : ℝ} (hab : a < b) (hfc : ContinuousOn f (Icc a b)) (hfI : f a = f b) : ∃ c ∈ Ioo a b, deriv f c = 0 := exists_deriv_eq_zero hab hfc hfI example (f : ℝ → ℝ) {a b : ℝ} (hab : a < b) (hf : ContinuousOn f (Icc a b)) (hf' : DifferentiableOn ℝ f (Ioo a b)) : ∃ c ∈ Ioo a b, deriv f c = (f b - f a) / (b - a) := exists_deriv_eq_slope f hab hf hf' example : deriv (fun x : ℝ ↦ x ^ 5) 6 = 5 * 6 ^ 4 := by simp example : deriv sin π = -1 := by simp
mathematics_in_lean/MIL/C12_Differential_Calculus/solutions/Solutions_S02_Differential_Calculus_in_Normed_Spaces.lean
import MIL.Common import Mathlib.Analysis.Normed.Operator.BanachSteinhaus import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Calculus.InverseFunctionTheorem.FDeriv import Mathlib.Analysis.Calculus.ContDiff.RCLike import Mathlib.Analysis.Calculus.FDeriv.Prod open Set Filter open Topology Filter noncomputable section section variable {𝕜 : Type*} [NontriviallyNormedField 𝕜] {E : Type*} [NormedAddCommGroup E] [NormedSpace 𝕜 E] {F : Type*} [NormedAddCommGroup F] [NormedSpace 𝕜 F] open Metric example {ι : Type*} [CompleteSpace E] {g : ι → E →L[𝕜] F} (h : ∀ x, ∃ C, ∀ i, ‖g i x‖ ≤ C) : ∃ C', ∀ i, ‖g i‖ ≤ C' := by -- sequence of subsets consisting of those `x : E` with norms `‖g i x‖` bounded by `n` let e : ℕ → Set E := fun n ↦ ⋂ i : ι, { x : E | ‖g i x‖ ≤ n } -- each of these sets is closed have hc : ∀ n : ℕ, IsClosed (e n) := fun i ↦ isClosed_iInter fun i ↦ isClosed_le (g i).cont.norm continuous_const -- the union is the entire space; this is where we use `h` have hU : (⋃ n : ℕ, e n) = univ := by refine eq_univ_of_forall fun x ↦ ?_ rcases h x with ⟨C, hC⟩ obtain ⟨m, hm⟩ := exists_nat_ge C exact ⟨e m, mem_range_self m, mem_iInter.mpr fun i ↦ le_trans (hC i) hm⟩ /- apply the Baire category theorem to conclude that for some `m : ℕ`, `e m` contains some `x` -/ obtain ⟨m : ℕ, x : E, hx : x ∈ interior (e m)⟩ := nonempty_interior_of_iUnion_of_closed hc hU obtain ⟨ε, ε_pos, hε : ball x ε ⊆ interior (e m)⟩ := isOpen_iff.mp isOpen_interior x hx obtain ⟨k : 𝕜, hk : 1 < ‖k‖⟩ := NormedField.exists_one_lt_norm 𝕜 -- show all elements in the ball have norm bounded by `m` after applying any `g i` have real_norm_le : ∀ z ∈ ball x ε, ∀ (i : ι), ‖g i z‖ ≤ m := by intro z hz i replace hz := mem_iInter.mp (interior_iInter_subset _ (hε hz)) i apply interior_subset hz have εk_pos : 0 < ε / ‖k‖ := div_pos ε_pos (zero_lt_one.trans hk) refine ⟨(m + m : ℕ) / (ε / ‖k‖), fun i ↦ ContinuousLinearMap.opNorm_le_of_shell ε_pos ?_ hk ?_⟩ · exact div_nonneg (Nat.cast_nonneg _) εk_pos.le intro y le_y y_lt calc ‖g i y‖ = ‖g i (y + x) - g i x‖ := by rw [(g i).map_add, add_sub_cancel_right] _ ≤ ‖g i (y + x)‖ + ‖g i x‖ := (norm_sub_le _ _) _ ≤ m + m := (add_le_add (real_norm_le (y + x) (by rwa [add_comm, add_mem_ball_iff_norm]) i) (real_norm_le x (mem_ball_self ε_pos) i)) _ = (m + m : ℕ) := by norm_cast _ ≤ (m + m : ℕ) * (‖y‖ / (ε / ‖k‖)) := (le_mul_of_one_le_right (Nat.cast_nonneg _) ((one_le_div <| div_pos ε_pos (zero_lt_one.trans hk)).2 le_y)) _ = (m + m : ℕ) / (ε / ‖k‖) * ‖y‖ := (mul_comm_div _ _ _).symm end
mathematics_in_lean/MIL/C12_Differential_Calculus/solutions/Solutions_S01_Elementary_Differential_Calculus.lean
import MIL.Common import Mathlib.Analysis.SpecialFunctions.Trigonometric.Deriv import Mathlib.Analysis.Calculus.Deriv.Pow import Mathlib.Analysis.Calculus.MeanValue open Set Filter open Topology Filter Classical Real noncomputable section
mathematics_in_lean/MIL/C01_Introduction/S02_Overview.lean
import MIL.Common open Nat -- These are pieces of data. #check 2 + 2 def f (x : ℕ) := x + 3 #check f -- These are propositions, of type `Prop`. #check 2 + 2 = 4 def FermatLastTheorem := ∀ x y z n : ℕ, n > 2 ∧ x * y * z ≠ 0 → x ^ n + y ^ n ≠ z ^ n #check FermatLastTheorem -- These are proofs of propositions. theorem easy : 2 + 2 = 4 := rfl #check easy theorem hard : FermatLastTheorem := sorry #check hard -- Here are some proofs. example : ∀ m n : Nat, Even n → Even (m * n) := fun m n ⟨k, (hk : n = k + k)⟩ ↦ have hmn : m * n = m * k + m * k := by rw [hk, mul_add] show ∃ l, m * n = l + l from ⟨_, hmn⟩ example : ∀ m n : Nat, Even n → Even (m * n) := fun m n ⟨k, hk⟩ ↦ ⟨m * k, by rw [hk, mul_add]⟩ example : ∀ m n : Nat, Even n → Even (m * n) := by -- Say `m` and `n` are natural numbers, and assume `n = 2 * k`. rintro m n ⟨k, hk⟩ -- We need to prove `m * n` is twice a natural number. Let's show it's twice `m * k`. use m * k -- Substitute for `n`, rw [hk] -- and now it's obvious. ring example : ∀ m n : Nat, Even n → Even (m * n) := by rintro m n ⟨k, hk⟩; use m * k; rw [hk]; ring example : ∀ m n : Nat, Even n → Even (m * n) := by intros; simp [*, parity_simps]
mathematics_in_lean/MIL/C01_Introduction/S01_Getting_Started.lean
#eval "Hello, World!"
mathematics_in_lean/MIL/C01_Introduction/solutions/Solutions_S02_Overview.lean
import MIL.Common open Nat -- There are no exercises in this section.
mathematics_in_lean/MIL/C01_Introduction/solutions/Solutions_S01_Getting_Started.lean
mathematics_in_lean/MIL/C10_Linear_Algebra/S01_Vector_Spaces.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] example (a : K) (u v : V) : a • (u + v) = a • u + a • v := smul_add a u v example (a b : K) (u : V) : (a + b) • u = a • u + b • u := add_smul a b u example (a b : K) (u : V) : a • b • u = b • a • u := smul_comm a b u example {R M : Type*} [CommSemiring R] [AddCommMonoid M] [Module R M] : Module (Ideal R) (Submodule R M) := inferInstance variable {W : Type*} [AddCommGroup W] [Module K W] variable (φ : V →ₗ[K] W) example (a : K) (v : V) : φ (a • v) = a • φ v := map_smul φ a v example (v w : V) : φ (v + w) = φ v + φ w := map_add φ v w variable (ψ : V →ₗ[K] W) #check (2 • φ + ψ : V →ₗ[K] W) variable (θ : W →ₗ[K] V) #check (φ.comp θ : W →ₗ[K] W) #check (φ ∘ₗ θ : W →ₗ[K] W) example : V →ₗ[K] V where toFun v := 3 • v map_add' _ _ := smul_add .. map_smul' _ _ := smul_comm .. #check (φ.map_add' : ∀ x y : V, φ.toFun (x + y) = φ.toFun x + φ.toFun y) #check (φ.map_add : ∀ x y : V, φ (x + y) = φ x + φ y) #check (map_add φ : ∀ x y : V, φ (x + y) = φ x + φ y) #check (LinearMap.lsmul K V 3 : V →ₗ[K] V) #check (LinearMap.lsmul K V : K →ₗ[K] V →ₗ[K] V) example (f : V ≃ₗ[K] W) : f ≪≫ₗ f.symm = LinearEquiv.refl K V := f.self_trans_symm noncomputable example (f : V →ₗ[K] W) (h : Function.Bijective f) : V ≃ₗ[K] W := .ofBijective f h section binary_product variable {W : Type*} [AddCommGroup W] [Module K W] variable {U : Type*} [AddCommGroup U] [Module K U] variable {T : Type*} [AddCommGroup T] [Module K T] -- First projection map example : V × W →ₗ[K] V := LinearMap.fst K V W -- Second projection map example : V × W →ₗ[K] W := LinearMap.snd K V W -- Universal property of the product example (φ : U →ₗ[K] V) (ψ : U →ₗ[K] W) : U →ₗ[K] V × W := LinearMap.prod φ ψ -- The product map does the expected thing, first component example (φ : U →ₗ[K] V) (ψ : U →ₗ[K] W) : LinearMap.fst K V W ∘ₗ LinearMap.prod φ ψ = φ := rfl -- The product map does the expected thing, second component example (φ : U →ₗ[K] V) (ψ : U →ₗ[K] W) : LinearMap.snd K V W ∘ₗ LinearMap.prod φ ψ = ψ := rfl -- We can also combine maps in parallel example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] T) : (V × W) →ₗ[K] (U × T) := φ.prodMap ψ -- This is simply done by combining the projections with the universal property example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] T) : φ.prodMap ψ = (φ ∘ₗ .fst K V W).prod (ψ ∘ₗ .snd K V W) := rfl -- First inclusion map example : V →ₗ[K] V × W := LinearMap.inl K V W -- Second inclusion map example : W →ₗ[K] V × W := LinearMap.inr K V W -- Universal property of the sum (aka coproduct) example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] U) : V × W →ₗ[K] U := φ.coprod ψ -- The coproduct map does the expected thing, first component example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] U) : φ.coprod ψ ∘ₗ LinearMap.inl K V W = φ := LinearMap.coprod_inl φ ψ -- The coproduct map does the expected thing, second component example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] U) : φ.coprod ψ ∘ₗ LinearMap.inr K V W = ψ := LinearMap.coprod_inr φ ψ -- The coproduct map is defined in the expected way example (φ : V →ₗ[K] U) (ψ : W →ₗ[K] U) (v : V) (w : W) : φ.coprod ψ (v, w) = φ v + ψ w := rfl end binary_product section families open DirectSum variable {ι : Type*} [DecidableEq ι] (V : ι → Type*) [∀ i, AddCommGroup (V i)] [∀ i, Module K (V i)] -- The universal property of the direct sum assembles maps from the summands to build -- a map from the direct sum example (φ : Π i, (V i →ₗ[K] W)) : (⨁ i, V i) →ₗ[K] W := DirectSum.toModule K ι W φ -- The universal property of the direct product assembles maps into the factors -- to build a map into the direct product example (φ : Π i, (W →ₗ[K] V i)) : W →ₗ[K] (Π i, V i) := LinearMap.pi φ -- The projection maps from the product example (i : ι) : (Π j, V j) →ₗ[K] V i := LinearMap.proj i -- The inclusion maps into the sum example (i : ι) : V i →ₗ[K] (⨁ i, V i) := DirectSum.lof K ι V i -- The inclusion maps into the product example (i : ι) : V i →ₗ[K] (Π i, V i) := LinearMap.single K V i -- In case `ι` is a finite type, there is an isomorphism between the sum and product. example [Fintype ι] : (⨁ i, V i) ≃ₗ[K] (Π i, V i) := linearEquivFunOnFintype K ι V end families
mathematics_in_lean/MIL/C10_Linear_Algebra/S02_Subspaces.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] example (U : Submodule K V) {x y : V} (hx : x ∈ U) (hy : y ∈ U) : x + y ∈ U := U.add_mem hx hy example (U : Submodule K V) {x : V} (hx : x ∈ U) (a : K) : a • x ∈ U := U.smul_mem a hx noncomputable example : Submodule ℝ ℂ where carrier := Set.range ((↑) : ℝ → ℂ) add_mem' := by rintro _ _ ⟨n, rfl⟩ ⟨m, rfl⟩ use n + m simp zero_mem' := by use 0 simp smul_mem' := by rintro c - ⟨a, rfl⟩ use c*a simp def preimage {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) (H : Submodule K W) : Submodule K V where carrier := φ ⁻¹' H zero_mem' := by sorry add_mem' := by sorry smul_mem' := by sorry example (U : Submodule K V) : Module K U := inferInstance example (U : Submodule K V) : Module K {x : V // x ∈ U} := inferInstance example (H H' : Submodule K V) : ((H ⊓ H' : Submodule K V) : Set V) = (H : Set V) ∩ (H' : Set V) := rfl example (H H' : Submodule K V) : ((H ⊔ H' : Submodule K V) : Set V) = Submodule.span K ((H : Set V) ∪ (H' : Set V)) := by simp [Submodule.span_union] example (x : V) : x ∈ (⊤ : Submodule K V) := trivial example (x : V) : x ∈ (⊥ : Submodule K V) ↔ x = 0 := Submodule.mem_bot K -- If two subspaces are in direct sum then they span the whole space. example (U V : Submodule K V) (h : IsCompl U V) : U ⊔ V = ⊤ := h.sup_eq_top -- If two subspaces are in direct sum then they intersect only at zero. example (U V : Submodule K V) (h : IsCompl U V) : U ⊓ V = ⊥ := h.inf_eq_bot section open DirectSum variable {ι : Type*} [DecidableEq ι] -- If subspaces are in direct sum then they span the whole space. example (U : ι → Submodule K V) (h : DirectSum.IsInternal U) : ⨆ i, U i = ⊤ := h.submodule_iSup_eq_top -- If subspaces are in direct sum then they pairwise intersect only at zero. example {ι : Type*} [DecidableEq ι] (U : ι → Submodule K V) (h : DirectSum.IsInternal U) {i j : ι} (hij : i ≠ j) : U i ⊓ U j = ⊥ := (h.submodule_iSupIndep.pairwiseDisjoint hij).eq_bot -- Those conditions characterize direct sums. #check DirectSum.isInternal_submodule_iff_independent_and_iSup_eq_top -- The relation with external direct sums: if a family of subspaces is -- in internal direct sum then the map from their external direct sum into `V` -- is a linear isomorphism. noncomputable example {ι : Type*} [DecidableEq ι] (U : ι → Submodule K V) (h : DirectSum.IsInternal U) : (⨁ i, U i) ≃ₗ[K] V := LinearEquiv.ofBijective (coeLinearMap U) h end example {s : Set V} (E : Submodule K V) : Submodule.span K s ≤ E ↔ s ⊆ E := Submodule.span_le example : GaloisInsertion (Submodule.span K) ((↑) : Submodule K V → Set V) := Submodule.gi K V example {S T : Submodule K V} {x : V} (h : x ∈ S ⊔ T) : ∃ s ∈ S, ∃ t ∈ T, x = s + t := by rw [← S.span_eq, ← T.span_eq, ← Submodule.span_union] at h induction h using Submodule.span_induction with | mem y h => sorry | zero => sorry | add x y hx hy hx' hy' => sorry | smul a x hx hx' => sorry section variable {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) variable (E : Submodule K V) in #check (Submodule.map φ E : Submodule K W) variable (F : Submodule K W) in #check (Submodule.comap φ F : Submodule K V) example : LinearMap.range φ = .map φ ⊤ := LinearMap.range_eq_map φ example : LinearMap.ker φ = .comap φ ⊥ := Submodule.comap_bot φ -- or `rfl` open Function LinearMap example : Injective φ ↔ ker φ = ⊥ := ker_eq_bot.symm example : Surjective φ ↔ range φ = ⊤ := range_eq_top.symm #check Submodule.mem_map_of_mem #check Submodule.mem_map #check Submodule.mem_comap example (E : Submodule K V) (F : Submodule K W) : Submodule.map φ E ≤ F ↔ E ≤ Submodule.comap φ F := by sorry variable (E : Submodule K V) example : Module K (V ⧸ E) := inferInstance example : V →ₗ[K] V ⧸ E := E.mkQ example : ker E.mkQ = E := E.ker_mkQ example : range E.mkQ = ⊤ := E.range_mkQ example (hφ : E ≤ ker φ) : V ⧸ E →ₗ[K] W := E.liftQ φ hφ example (F : Submodule K W) (hφ : E ≤ .comap φ F) : V ⧸ E →ₗ[K] W ⧸ F := E.mapQ F φ hφ noncomputable example : (V ⧸ LinearMap.ker φ) ≃ₗ[K] range φ := φ.quotKerEquivRange open Submodule #check Submodule.map_comap_eq #check Submodule.comap_map_eq example : Submodule K (V ⧸ E) ≃ { F : Submodule K V // E ≤ F } where toFun := sorry invFun := sorry left_inv := sorry right_inv := sorry
mathematics_in_lean/MIL/C10_Linear_Algebra/S04_Bases.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import Mathlib.Data.Complex.FiniteDimensional import MIL.Common section matrices -- Adding vectors #eval ![1, 2] + ![3, 4] -- ![4, 6] -- Adding matrices #eval !![1, 2; 3, 4] + !![3, 4; 5, 6] -- !![4, 6; 8, 10] -- Multiplying matrices #eval !![1, 2; 3, 4] * !![3, 4; 5, 6] -- !![13, 16; 29, 36] open Matrix -- matrices acting on vectors on the left #eval !![1, 2; 3, 4] *ᵥ ![1, 1] -- ![3, 7] -- matrices acting on vectors on the left, resulting in a size one matrix #eval !![1, 2] *ᵥ ![1, 1] -- ![3] -- matrices acting on vectors on the right #eval ![1, 1, 1] ᵥ* !![1, 2; 3, 4; 5, 6] -- ![9, 12] #eval replicateRow (Fin 1) ![1, 2] -- !![1, 2] #eval replicateCol (Fin 1) ![1, 2] -- !![1; 2] -- vector dot product #eval ![1, 2] ⬝ᵥ ![3, 4] -- `11` -- matrix transpose #eval !![1, 2; 3, 4]ᵀ -- `!![1, 3; 2, 4]` -- determinant #eval !![(1 : ℤ), 2; 3, 4].det -- `-2` -- trace #eval !![(1 : ℤ), 2; 3, 4].trace -- `5` #simp !![(1 : ℝ), 2; 3, 4].det -- `4 - 2*3` #norm_num !![(1 : ℝ), 2; 3, 4].det -- `-2` #norm_num !![(1 : ℝ), 2; 3, 4].trace -- `5` variable (a b c d : ℝ) in #simp !![a, b; c, d].det -- `a * d – b * c` #norm_num [Matrix.inv_def] !![(1 : ℝ), 2; 3, 4]⁻¹ -- !![-2, 1; 3 / 2, -(1 / 2)] example : !![(1 : ℝ), 2; 3, 4]⁻¹ * !![(1 : ℝ), 2; 3, 4] = 1 := by have : Invertible !![(1 : ℝ), 2; 3, 4] := by apply Matrix.invertibleOfIsUnitDet norm_num simp example : !![(1 : ℝ), 2; 3, 4]⁻¹ * !![(1 : ℝ), 2; 3, 4] = 1 := by norm_num [Matrix.inv_def] exact one_fin_two.symm section example : (fun _ ↦ 1 : Fin 2 → Fin 2 → ℤ) = !![1, 1; 1, 1] := by ext i j fin_cases i <;> fin_cases j <;> rfl example : (fun _ ↦ 1 : Fin 2 → Fin 2 → ℤ) * (fun _ ↦ 1 : Fin 2 → Fin 2 → ℤ) = !![1, 1; 1, 1] := by ext i j fin_cases i <;> fin_cases j <;> rfl example : !![1, 1; 1, 1] * !![1, 1; 1, 1] = !![2, 2; 2, 2] := by norm_num example {n : ℕ} (v : Fin n → ℝ) : Matrix.vandermonde v = Matrix.of (fun i j : Fin n ↦ v i ^ (j : ℕ)) := rfl end end matrices variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] section variable {ι : Type*} (B : Basis ι K V) (v : V) (i : ι) -- The basis vector with index ``i`` #check (B i : V) -- the linear isomorphism with the model space given by ``B`` #check (B.repr : V ≃ₗ[K] ι →₀ K) -- the component function of ``v`` #check (B.repr v : ι →₀ K) -- the component of ``v`` with index ``i`` #check (B.repr v i : K) noncomputable example (b : ι → V) (b_indep : LinearIndependent K b) (b_spans : ∀ v, v ∈ Submodule.span K (Set.range b)) : Basis ι K V := Basis.mk b_indep (fun v _ ↦ b_spans v) -- The family of vectors underlying the above basis is indeed ``b``. example (b : ι → V) (b_indep : LinearIndependent K b) (b_spans : ∀ v, v ∈ Submodule.span K (Set.range b)) (i : ι) : Basis.mk b_indep (fun v _ ↦ b_spans v) i = b i := Basis.mk_apply b_indep (fun v _ ↦ b_spans v) i variable [DecidableEq ι] example : Finsupp.basisSingleOne.repr = LinearEquiv.refl K (ι →₀ K) := rfl example (i : ι) : Finsupp.basisSingleOne i = Finsupp.single i 1 := rfl example [Finite ι] (x : ι → K) (i : ι) : (Pi.basisFun K ι).repr x i = x i := by simp example [Fintype ι] : ∑ i : ι, B.repr v i • (B i) = v := B.sum_repr v example (c : ι →₀ K) (f : ι → V) (s : Finset ι) (h : c.support ⊆ s) : Finsupp.linearCombination K f c = ∑ i ∈ s, c i • f i := Finsupp.linearCombination_apply_of_mem_supported K h example : Finsupp.linearCombination K B (B.repr v) = v := B.linearCombination_repr v variable (f : ι → V) in #check (Finsupp.linearCombination K f : (ι →₀ K) →ₗ[K] V) section variable {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) (u : ι → W) #check (B.constr K : (ι → W) ≃ₗ[K] (V →ₗ[K] W)) #check (B.constr K u : V →ₗ[K] W) example (i : ι) : B.constr K u (B i) = u i := B.constr_basis K u i example (φ ψ : V →ₗ[K] W) (h : ∀ i, φ (B i) = ψ (B i)) : φ = ψ := B.ext h variable {ι' : Type*} (B' : Basis ι' K W) [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] open LinearMap #check (toMatrix B B' : (V →ₗ[K] W) ≃ₗ[K] Matrix ι' ι K) open Matrix -- get access to the ``*ᵥ`` notation for multiplication between matrices and vectors. example (φ : V →ₗ[K] W) (v : V) : (toMatrix B B' φ) *ᵥ (B.repr v) = B'.repr (φ v) := toMatrix_mulVec_repr B B' φ v variable {ι'' : Type*} (B'' : Basis ι'' K W) [Fintype ι''] [DecidableEq ι''] example (φ : V →ₗ[K] W) : (toMatrix B B'' φ) = (toMatrix B' B'' .id) * (toMatrix B B' φ) := by simp end open Module LinearMap Matrix -- Some lemmas coming from the fact that `LinearMap.toMatrix` is an algebra morphism. #check toMatrix_comp #check id_comp #check comp_id #check toMatrix_id -- Some lemmas coming from the fact that ``Matrix.det`` is a multiplicative monoid morphism. #check Matrix.det_mul #check Matrix.det_one example [Fintype ι] (B' : Basis ι K V) (φ : End K V) : (toMatrix B B φ).det = (toMatrix B' B' φ).det := by set M := toMatrix B B φ set M' := toMatrix B' B' φ set P := (toMatrix B B') LinearMap.id set P' := (toMatrix B' B) LinearMap.id sorry end section #check (Module.finrank K V : ℕ) -- `Fin n → K` is the archetypical space with dimension `n` over `K`. example (n : ℕ) : Module.finrank K (Fin n → K) = n := Module.finrank_fin_fun K -- Seen as a vector space over itself, `ℂ` has dimension one. example : Module.finrank ℂ ℂ = 1 := Module.finrank_self ℂ -- But as a real vector space it has dimension two. example : Module.finrank ℝ ℂ = 2 := Complex.finrank_real_complex example [FiniteDimensional K V] : 0 < Module.finrank K V ↔ Nontrivial V := Module.finrank_pos_iff example [FiniteDimensional K V] (h : 0 < Module.finrank K V) : Nontrivial V := by apply (Module.finrank_pos_iff (R := K)).1 exact h variable {ι : Type*} (B : Basis ι K V) example [Finite ι] : FiniteDimensional K V := FiniteDimensional.of_fintype_basis B example [FiniteDimensional K V] : Finite ι := (FiniteDimensional.fintypeBasisIndex B).finite end section variable (E F : Submodule K V) [FiniteDimensional K V] open Module example : finrank K (E ⊔ F : Submodule K V) + finrank K (E ⊓ F : Submodule K V) = finrank K E + finrank K F := Submodule.finrank_sup_add_finrank_inf_eq E F example : finrank K E ≤ finrank K V := Submodule.finrank_le E example (h : finrank K V < finrank K E + finrank K F) : Nontrivial (E ⊓ F : Submodule K V) := by sorry end #check V -- Type u_2 #check Module.rank K V -- Cardinal.{u_2} universe u v -- `u` and `v` will denote universe levels variable {ι : Type u} (B : Basis ι K V) {ι' : Type v} (B' : Basis ι' K V) example : Cardinal.lift.{v, u} (.mk ι) = Cardinal.lift.{u, v} (.mk ι') := mk_eq_mk_of_basis B B' example [FiniteDimensional K V] : (Module.finrank K V : Cardinal) = Module.rank K V := Module.finrank_eq_rank K V
mathematics_in_lean/MIL/C10_Linear_Algebra/S03_Endomorphisms.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] variable {W : Type*} [AddCommGroup W] [Module K W] open Polynomial Module LinearMap End example (φ ψ : End K V) : φ * ψ = φ ∘ₗ ψ := End.mul_eq_comp φ ψ -- `rfl` would also work -- evaluating `P` on `φ` example (P : K[X]) (φ : End K V) : V →ₗ[K] V := aeval φ P -- evaluating `X` on `φ` gives back `φ` example (φ : End K V) : aeval φ (X : K[X]) = φ := aeval_X φ #check Submodule.eq_bot_iff #check Submodule.mem_inf #check LinearMap.mem_ker example (P Q : K[X]) (h : IsCoprime P Q) (φ : End K V) : ker (aeval φ P) ⊓ ker (aeval φ Q) = ⊥ := by sorry #check Submodule.add_mem_sup #check map_mul #check End.mul_apply #check LinearMap.ker_le_ker_comp example (P Q : K[X]) (h : IsCoprime P Q) (φ : End K V) : ker (aeval φ P) ⊔ ker (aeval φ Q) = ker (aeval φ (P*Q)) := by sorry example (φ : End K V) (a : K) : φ.eigenspace a = LinearMap.ker (φ - a • 1) := End.eigenspace_def example (φ : End K V) (a : K) : φ.HasEigenvalue a ↔ φ.eigenspace a ≠ ⊥ := Iff.rfl example (φ : End K V) (a : K) : φ.HasEigenvalue a ↔ ∃ v, φ.HasEigenvector a v := ⟨End.HasEigenvalue.exists_hasEigenvector, fun ⟨_, hv⟩ ↦ φ.hasEigenvalue_of_hasEigenvector hv⟩ example (φ : End K V) : φ.Eigenvalues = {a // φ.HasEigenvalue a} := rfl -- Eigenvalue are roots of the minimal polynomial example (φ : End K V) (a : K) : φ.HasEigenvalue a → (minpoly K φ).IsRoot a := φ.isRoot_of_hasEigenvalue -- In finite dimension, the converse is also true (we will discuss dimension below) example [FiniteDimensional K V] (φ : End K V) (a : K) : φ.HasEigenvalue a ↔ (minpoly K φ).IsRoot a := φ.hasEigenvalue_iff_isRoot -- Cayley-Hamilton example [FiniteDimensional K V] (φ : End K V) : aeval φ φ.charpoly = 0 := φ.aeval_self_charpoly
mathematics_in_lean/MIL/C10_Linear_Algebra/solutions/Solutions_S03_Endomorphisms.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] variable {W : Type*} [AddCommGroup W] [Module K W] open Polynomial Module LinearMap End example (φ ψ : End K V) : φ * ψ = φ ∘ₗ ψ := End.mul_eq_comp φ ψ -- `rfl` would also work -- evaluating `P` on `φ` example (P : K[X]) (φ : End K V) : V →ₗ[K] V := aeval φ P -- evaluating `X` on `φ` gives back `φ` example (φ : End K V) : aeval φ (X : K[X]) = φ := aeval_X φ #check Submodule.eq_bot_iff #check Submodule.mem_inf #check LinearMap.mem_ker example (P Q : K[X]) (h : IsCoprime P Q) (φ : End K V) : ker (aeval φ P) ⊓ ker (aeval φ Q) = ⊥ := by rw [Submodule.eq_bot_iff] rintro x hx rw [Submodule.mem_inf, mem_ker, mem_ker] at hx rcases h with ⟨U, V, hUV⟩ have := congr((aeval φ) $hUV.symm x) simpa [hx] #check Submodule.add_mem_sup #check map_mul #check End.mul_apply #check LinearMap.ker_le_ker_comp example (P Q : K[X]) (h : IsCoprime P Q) (φ : End K V) : ker (aeval φ P) ⊔ ker (aeval φ Q) = ker (aeval φ (P*Q)) := by apply le_antisymm · apply sup_le · rw [mul_comm, map_mul] apply ker_le_ker_comp -- or alternative below: -- intro x hx -- rw [mul_comm, mem_ker] at * -- simp [hx] · rw [map_mul] apply ker_le_ker_comp -- or alternative as above · intro x hx rcases h with ⟨U, V, hUV⟩ have key : x = aeval φ (U*P) x + aeval φ (V*Q) x := by simpa using congr((aeval φ) $hUV.symm x) rw [key, add_comm] apply Submodule.add_mem_sup <;> rw [mem_ker] at * · rw [← mul_apply, ← map_mul, show P*(V*Q) = V*(P*Q) by ring, map_mul, mul_apply, hx, map_zero] · rw [← mul_apply, ← map_mul, show Q*(U*P) = U*(P*Q) by ring, map_mul, mul_apply, hx, map_zero]
mathematics_in_lean/MIL/C10_Linear_Algebra/solutions/Solutions_S04_Bases.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import Mathlib.Data.Complex.FiniteDimensional import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] section variable {ι : Type*} (B : Basis ι K V) (v : V) (i : ι) -- The basis vector with index ``i`` #check (B i : V) -- the linear isomorphism with the model space given by ``B`` #check (B.repr : V ≃ₗ[K] ι →₀ K) -- the component function of ``v`` #check (B.repr v : ι →₀ K) -- the component of ``v`` with index ``i`` #check (B.repr v i : K) variable [DecidableEq ι] section variable {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) (u : ι → W) #check (B.constr K : (ι → W) ≃ₗ[K] (V →ₗ[K] W)) #check (B.constr K u : V →ₗ[K] W) example (i : ι) : B.constr K u (B i) = u i := B.constr_basis K u i variable {ι' : Type*} (B' : Basis ι' K W) [Fintype ι] [DecidableEq ι] [Fintype ι'] [DecidableEq ι'] open LinearMap #check (toMatrix B B' : (V →ₗ[K] W) ≃ₗ[K] Matrix ι' ι K) open Matrix -- get access to the ``*ᵥ`` notation for multiplication between matrices and vectors. example (φ : V →ₗ[K] W) (v : V) : (toMatrix B B' φ) *ᵥ (B.repr v) = B'.repr (φ v) := toMatrix_mulVec_repr B B' φ v variable {ι'' : Type*} (B'' : Basis ι'' K W) [Fintype ι''] [DecidableEq ι''] example (φ : V →ₗ[K] W) : (toMatrix B B'' φ) = (toMatrix B' B'' .id) * (toMatrix B B' φ) := by simp end open Module LinearMap Matrix -- Some lemmas coming from the fact that `LinearMap.toMatrix` is an algebra morphism. #check toMatrix_comp #check id_comp #check comp_id #check toMatrix_id -- Some lemmas coming from the fact that ``Matrix.det`` is a multiplicative monoid morphism. #check Matrix.det_mul #check Matrix.det_one example [Fintype ι] (B' : Basis ι K V) (φ : End K V) : (toMatrix B B φ).det = (toMatrix B' B' φ).det := by set M := toMatrix B B φ set M' := toMatrix B' B' φ set P := (toMatrix B B') LinearMap.id set P' := (toMatrix B' B) LinearMap.id have F : M = P' * M' * P := by rw [← toMatrix_comp, ← toMatrix_comp, id_comp, comp_id] have F' : P' * P = 1 := by rw [← toMatrix_comp, id_comp, toMatrix_id] rw [F, Matrix.det_mul, Matrix.det_mul, show P'.det * M'.det * P.det = P'.det * P.det * M'.det by ring, ← Matrix.det_mul, F', Matrix.det_one, one_mul] end section variable (E F : Submodule K V) [FiniteDimensional K V] open Module example : finrank K (E ⊔ F : Submodule K V) + finrank K (E ⊓ F : Submodule K V) = finrank K E + finrank K F := Submodule.finrank_sup_add_finrank_inf_eq E F example : finrank K E ≤ finrank K V := Submodule.finrank_le E example (h : finrank K V < finrank K E + finrank K F) : Nontrivial (E ⊓ F : Submodule K V) := by rw [← finrank_pos_iff (R := K)] have := Submodule.finrank_sup_add_finrank_inf_eq E F have := Submodule.finrank_le E have := Submodule.finrank_le F have := Submodule.finrank_le (E ⊔ F) linarith end
mathematics_in_lean/MIL/C10_Linear_Algebra/solutions/Solutions_S01_Vector_Spaces.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common
mathematics_in_lean/MIL/C10_Linear_Algebra/solutions/Solutions_S02_Subspaces.lean
import Mathlib.LinearAlgebra.Matrix.Determinant.Basic import Mathlib.LinearAlgebra.Eigenspace.Minpoly import Mathlib.LinearAlgebra.Charpoly.Basic import MIL.Common variable {K : Type*} [Field K] {V : Type*} [AddCommGroup V] [Module K V] example (U : Submodule K V) {x y : V} (hx : x ∈ U) (hy : y ∈ U) : x + y ∈ U := U.add_mem hx hy example (U : Submodule K V) {x : V} (hx : x ∈ U) (a : K) : a • x ∈ U := U.smul_mem a hx def preimage {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) (H : Submodule K W) : Submodule K V where carrier := φ ⁻¹' H zero_mem' := by rw [Set.mem_preimage, map_zero] exact H.zero_mem add_mem' := by rintro a b ha hb rw [Set.mem_preimage, map_add] apply H.add_mem <;> assumption smul_mem' := by rintro a v hv rw [Set.mem_preimage, map_smul] exact H.smul_mem a hv example {S T : Submodule K V} {x : V} (h : x ∈ S ⊔ T) : ∃ s ∈ S, ∃ t ∈ T, x = s + t := by rw [← S.span_eq, ← T.span_eq, ← Submodule.span_union] at h induction h using Submodule.span_induction with | mem x h => rcases h with (hx|hx) · use x, hx, 0, T.zero_mem module · use 0, S.zero_mem, x, hx module | zero => use 0, S.zero_mem, 0, T.zero_mem module | add x y hx hy hx' hy' => rcases hx' with ⟨s, hs, t, ht, rfl⟩ rcases hy' with ⟨s', hs', t', ht', rfl⟩ use s + s', S.add_mem hs hs', t + t', T.add_mem ht ht' module | smul a x hx hx' => rcases hx' with ⟨s, hs, t, ht, rfl⟩ use a • s, S.smul_mem a hs, a • t, T.smul_mem a ht module section variable {W : Type*} [AddCommGroup W] [Module K W] (φ : V →ₗ[K] W) variable (E : Submodule K V) in #check (Submodule.map φ E : Submodule K W) variable (F : Submodule K W) in #check (Submodule.comap φ F : Submodule K V) open Function LinearMap example : Injective φ ↔ ker φ = ⊥ := ker_eq_bot.symm example : Surjective φ ↔ range φ = ⊤ := range_eq_top.symm #check Submodule.mem_map_of_mem #check Submodule.mem_map #check Submodule.mem_comap example (E : Submodule K V) (F : Submodule K W) : Submodule.map φ E ≤ F ↔ E ≤ Submodule.comap φ F := by constructor · intro h x hx exact h ⟨x, hx, rfl⟩ · rintro h - ⟨x, hx, rfl⟩ exact h hx variable (E : Submodule K V) example : Module K (V ⧸ E) := inferInstance example : V →ₗ[K] V ⧸ E := E.mkQ example : ker E.mkQ = E := E.ker_mkQ example : range E.mkQ = ⊤ := E.range_mkQ example (hφ : E ≤ ker φ) : V ⧸ E →ₗ[K] W := E.liftQ φ hφ example (F : Submodule K W) (hφ : E ≤ .comap φ F) : V ⧸ E →ₗ[K] W ⧸ F := E.mapQ F φ hφ noncomputable example : (V ⧸ LinearMap.ker φ) ≃ₗ[K] range φ := φ.quotKerEquivRange open Submodule #check Submodule.map_comap_eq #check Submodule.comap_map_eq example : Submodule K (V ⧸ E) ≃ { F : Submodule K V // E ≤ F } where toFun F := ⟨comap E.mkQ F, by conv_lhs => rw [← E.ker_mkQ, ← comap_bot] gcongr apply bot_le⟩ invFun P := map E.mkQ P left_inv P := by dsimp rw [Submodule.map_comap_eq, E.range_mkQ] exact top_inf_eq P right_inv := by intro P ext x dsimp only rw [Submodule.comap_map_eq, E.ker_mkQ, sup_of_le_left] exact P.2
mathematics_in_lean/MIL/C08_Hierarchies/S01_Basics.lean
import MIL.Common import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Real.Basic set_option autoImplicit true class One₁ (α : Type) where /-- The element one -/ one : α #check One₁.one -- One₁.one {α : Type} [self : One₁ α] : α @[class] structure One₂ (α : Type) where /-- The element one -/ one : α #check One₂.one example (α : Type) [One₁ α] : α := One₁.one example (α : Type) [One₁ α] := (One₁.one : α) @[inherit_doc] notation "𝟙" => One₁.one example {α : Type} [One₁ α] : α := 𝟙 example {α : Type} [One₁ α] : (𝟙 : α) = 𝟙 := rfl class Dia₁ (α : Type) where dia : α → α → α infixl:70 " ⋄ " => Dia₁.dia class Semigroup₀ (α : Type) where toDia₁ : Dia₁ α /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) attribute [instance] Semigroup₀.toDia₁ example {α : Type} [Semigroup₀ α] (a b : α) : α := a ⋄ b class Semigroup₁ (α : Type) extends toDia₁ : Dia₁ α where /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) example {α : Type} [Semigroup₁ α] (a b : α) : α := a ⋄ b class Semigroup₂ (α : Type) extends Dia₁ α where /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) class DiaOneClass₁ (α : Type) extends One₁ α, Dia₁ α where /-- One is a left neutral element for diamond. -/ one_dia : ∀ a : α, 𝟙 ⋄ a = a /-- One is a right neutral element for diamond -/ dia_one : ∀ a : α, a ⋄ 𝟙 = a set_option trace.Meta.synthInstance true in example {α : Type} [DiaOneClass₁ α] (a b : α) : Prop := a ⋄ b = 𝟙 class Monoid₁ (α : Type) extends Semigroup₁ α, DiaOneClass₁ α class Monoid₂ (α : Type) where toSemigroup₁ : Semigroup₁ α toDiaOneClass₁ : DiaOneClass₁ α example {α : Type} [Monoid₁ α] : (Monoid₁.toSemigroup₁.toDia₁.dia : α → α → α) = Monoid₁.toDiaOneClass₁.toDia₁.dia := rfl /- Monoid₂.mk {α : Type} (toSemigroup₁ : Semigroup₁ α) (toDiaOneClass₁ : DiaOneClass₁ α) : Monoid₂ α -/ #check Monoid₂.mk /- Monoid₁.mk {α : Type} [toSemigroup₁ : Semigroup₁ α] [toOne₁ : One₁ α] (one_dia : ∀ (a : α), 𝟙 ⋄ a = a) (dia_one : ∀ (a : α), a ⋄ 𝟙 = a) : Monoid₁ α -/ #check Monoid₁.mk #check Monoid₁.toSemigroup₁ #check Monoid₁.toDiaOneClass₁ class Inv₁ (α : Type) where /-- The inversion function -/ inv : α → α @[inherit_doc] postfix:max "⁻¹" => Inv₁.inv class Group₁ (G : Type) extends Monoid₁ G, Inv₁ G where inv_dia : ∀ a : G, a⁻¹ ⋄ a = 𝟙 lemma left_inv_eq_right_inv₁ {M : Type} [Monoid₁ M] {a b c : M} (hba : b ⋄ a = 𝟙) (hac : a ⋄ c = 𝟙) : b = c := by rw [← DiaOneClass₁.one_dia c, ← hba, Semigroup₁.dia_assoc, hac, DiaOneClass₁.dia_one b] export DiaOneClass₁ (one_dia dia_one) export Semigroup₁ (dia_assoc) export Group₁ (inv_dia) example {M : Type} [Monoid₁ M] {a b c : M} (hba : b ⋄ a = 𝟙) (hac : a ⋄ c = 𝟙) : b = c := by rw [← one_dia c, ← hba, dia_assoc, hac, dia_one b] lemma inv_eq_of_dia [Group₁ G] {a b : G} (h : a ⋄ b = 𝟙) : a⁻¹ = b := sorry lemma dia_inv [Group₁ G] (a : G) : a ⋄ a⁻¹ = 𝟙 := sorry class AddSemigroup₃ (α : Type) extends Add α where /-- Addition is associative -/ add_assoc₃ : ∀ a b c : α, a + b + c = a + (b + c) @[to_additive AddSemigroup₃] class Semigroup₃ (α : Type) extends Mul α where /-- Multiplication is associative -/ mul_assoc₃ : ∀ a b c : α, a * b * c = a * (b * c) class AddMonoid₃ (α : Type) extends AddSemigroup₃ α, AddZeroClass α @[to_additive AddMonoid₃] class Monoid₃ (α : Type) extends Semigroup₃ α, MulOneClass α export Semigroup₃ (mul_assoc₃) export AddSemigroup₃ (add_assoc₃) whatsnew in @[to_additive] lemma left_inv_eq_right_inv' {M : Type} [Monoid₃ M] {a b c : M} (hba : b * a = 1) (hac : a * c = 1) : b = c := by rw [← one_mul c, ← hba, mul_assoc₃, hac, mul_one b] #check left_neg_eq_right_neg' class AddCommSemigroup₃ (α : Type) extends AddSemigroup₃ α where add_comm : ∀ a b : α, a + b = b + a @[to_additive AddCommSemigroup₃] class CommSemigroup₃ (α : Type) extends Semigroup₃ α where mul_comm : ∀ a b : α, a * b = b * a class AddCommMonoid₃ (α : Type) extends AddMonoid₃ α, AddCommSemigroup₃ α @[to_additive AddCommMonoid₃] class CommMonoid₃ (α : Type) extends Monoid₃ α, CommSemigroup₃ α class AddGroup₃ (G : Type) extends AddMonoid₃ G, Neg G where neg_add : ∀ a : G, -a + a = 0 @[to_additive AddGroup₃] class Group₃ (G : Type) extends Monoid₃ G, Inv G where inv_mul : ∀ a : G, a⁻¹ * a = 1 attribute [simp] Group₃.inv_mul AddGroup₃.neg_add @[to_additive] lemma inv_eq_of_mul [Group₃ G] {a b : G} (h : a * b = 1) : a⁻¹ = b := sorry @[to_additive (attr := simp)] lemma Group₃.mul_inv {G : Type} [Group₃ G] {a : G} : a * a⁻¹ = 1 := by sorry @[to_additive] lemma mul_left_cancel₃ {G : Type} [Group₃ G] {a b c : G} (h : a * b = a * c) : b = c := by sorry @[to_additive] lemma mul_right_cancel₃ {G : Type} [Group₃ G] {a b c : G} (h : b*a = c*a) : b = c := by sorry class AddCommGroup₃ (G : Type) extends AddGroup₃ G, AddCommMonoid₃ G @[to_additive AddCommGroup₃] class CommGroup₃ (G : Type) extends Group₃ G, CommMonoid₃ G class Ring₃ (R : Type) extends AddGroup₃ R, Monoid₃ R, MulZeroClass R where /-- Multiplication is left distributive over addition -/ left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c /-- Multiplication is right distributive over addition -/ right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c instance {R : Type} [Ring₃ R] : AddCommGroup₃ R := { add_comm := by sorry } instance : Ring₃ ℤ where add := (· + ·) add_assoc₃ := add_assoc zero := 0 zero_add := by simp add_zero := by simp neg := (- ·) neg_add := by simp mul := (· * ·) mul_assoc₃ := mul_assoc one := 1 one_mul := by simp mul_one := by simp zero_mul := by simp mul_zero := by simp left_distrib := Int.mul_add right_distrib := Int.add_mul class LE₁ (α : Type) where /-- The Less-or-Equal relation. -/ le : α → α → Prop @[inherit_doc] infix:50 " ≤₁ " => LE₁.le class Preorder₁ (α : Type) class PartialOrder₁ (α : Type) class OrderedCommMonoid₁ (α : Type) instance : OrderedCommMonoid₁ ℕ where class SMul₃ (α : Type) (β : Type) where /-- Scalar multiplication -/ smul : α → β → β infixr:73 " • " => SMul₃.smul class Module₁ (R : Type) [Ring₃ R] (M : Type) [AddCommGroup₃ M] extends SMul₃ R M where zero_smul : ∀ m : M, (0 : R) • m = 0 one_smul : ∀ m : M, (1 : R) • m = m mul_smul : ∀ (a b : R) (m : M), (a * b) • m = a • b • m add_smul : ∀ (a b : R) (m : M), (a + b) • m = a • m + b • m smul_add : ∀ (a : R) (m n : M), a • (m + n) = a • m + a • n instance selfModule (R : Type) [Ring₃ R] : Module₁ R R where smul := fun r s ↦ r*s zero_smul := zero_mul one_smul := one_mul mul_smul := mul_assoc₃ add_smul := Ring₃.right_distrib smul_add := Ring₃.left_distrib def nsmul₁ {M : Type*} [Zero M] [Add M] : ℕ → M → M | 0, _ => 0 | n + 1, a => a + nsmul₁ n a def zsmul₁ {M : Type*} [Zero M] [Add M] [Neg M] : ℤ → M → M | Int.ofNat n, a => nsmul₁ n a | Int.negSucc n, a => -nsmul₁ n.succ a instance abGrpModule (A : Type) [AddCommGroup₃ A] : Module₁ ℤ A where smul := zsmul₁ zero_smul := sorry one_smul := sorry mul_smul := sorry add_smul := sorry smul_add := sorry #synth Module₁ ℤ ℤ -- abGrpModule ℤ class AddMonoid₄ (M : Type) extends AddSemigroup₃ M, AddZeroClass M where /-- Multiplication by a natural number. -/ nsmul : ℕ → M → M := nsmul₁ /-- Multiplication by `(0 : ℕ)` gives `0`. -/ nsmul_zero : ∀ x, nsmul 0 x = 0 := by intros; rfl /-- Multiplication by `(n + 1 : ℕ)` behaves as expected. -/ nsmul_succ : ∀ (n : ℕ) (x), nsmul (n + 1) x = x + nsmul n x := by intros; rfl instance mySMul {M : Type} [AddMonoid₄ M] : SMul ℕ M := ⟨AddMonoid₄.nsmul⟩ instance (M N : Type) [AddMonoid₄ M] [AddMonoid₄ N] : AddMonoid₄ (M × N) where add := fun p q ↦ (p.1 + q.1, p.2 + q.2) add_assoc₃ := fun a b c ↦ by ext <;> apply add_assoc₃ zero := (0, 0) zero_add := fun a ↦ by ext <;> apply zero_add add_zero := fun a ↦ by ext <;> apply add_zero instance : AddMonoid₄ ℤ where add := (· + ·) add_assoc₃ := Int.add_assoc zero := 0 zero_add := Int.zero_add add_zero := Int.add_zero nsmul := fun n m ↦ (n : ℤ) * m nsmul_zero := Int.zero_mul nsmul_succ := fun n m ↦ show (n + 1 : ℤ) * m = m + n * m by rw [Int.add_mul, Int.add_comm, Int.one_mul] example (n : ℕ) (m : ℤ) : SMul.smul (self := mySMul) n m = n * m := rfl
mathematics_in_lean/MIL/C08_Hierarchies/S03_Subobjects.lean
import MIL.Common import Mathlib.GroupTheory.QuotientGroup.Basic set_option autoImplicit true @[ext] structure Submonoid₁ (M : Type) [Monoid M] where /-- The carrier of a submonoid. -/ carrier : Set M /-- The product of two elements of a submonoid belongs to the submonoid. -/ mul_mem {a b} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier /-- The unit element belongs to the submonoid. -/ one_mem : 1 ∈ carrier /-- Submonoids in `M` can be seen as sets in `M`. -/ instance [Monoid M] : SetLike (Submonoid₁ M) M where coe := Submonoid₁.carrier coe_injective' _ _ := Submonoid₁.ext example [Monoid M] (N : Submonoid₁ M) : 1 ∈ N := N.one_mem example [Monoid M] (N : Submonoid₁ M) (α : Type) (f : M → α) := f '' N example [Monoid M] (N : Submonoid₁ M) (x : N) : (x : M) ∈ N := x.property instance SubMonoid₁Monoid [Monoid M] (N : Submonoid₁ M) : Monoid N where mul := fun x y ↦ ⟨x*y, N.mul_mem x.property y.property⟩ mul_assoc := fun x y z ↦ SetCoe.ext (mul_assoc (x : M) y z) one := ⟨1, N.one_mem⟩ one_mul := fun x ↦ SetCoe.ext (one_mul (x : M)) mul_one := fun x ↦ SetCoe.ext (mul_one (x : M)) example [Monoid M] (N : Submonoid₁ M) : Monoid N where mul := fun ⟨x, hx⟩ ⟨y, hy⟩ ↦ ⟨x*y, N.mul_mem hx hy⟩ mul_assoc := fun ⟨x, _⟩ ⟨y, _⟩ ⟨z, _⟩ ↦ SetCoe.ext (mul_assoc x y z) one := ⟨1, N.one_mem⟩ one_mul := fun ⟨x, _⟩ ↦ SetCoe.ext (one_mul x) mul_one := fun ⟨x, _⟩ ↦ SetCoe.ext (mul_one x) class SubmonoidClass₁ (S : Type) (M : Type) [Monoid M] [SetLike S M] : Prop where mul_mem : ∀ (s : S) {a b : M}, a ∈ s → b ∈ s → a * b ∈ s one_mem : ∀ s : S, 1 ∈ s instance [Monoid M] : SubmonoidClass₁ (Submonoid₁ M) M where mul_mem := Submonoid₁.mul_mem one_mem := Submonoid₁.one_mem instance [Monoid M] : Min (Submonoid₁ M) := ⟨fun S₁ S₂ ↦ { carrier := S₁ ∩ S₂ one_mem := ⟨S₁.one_mem, S₂.one_mem⟩ mul_mem := fun ⟨hx, hx'⟩ ⟨hy, hy'⟩ ↦ ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩ example [Monoid M] (N P : Submonoid₁ M) : Submonoid₁ M := N ⊓ P def Submonoid.Setoid [CommMonoid M] (N : Submonoid M) : Setoid M where r := fun x y ↦ ∃ w ∈ N, ∃ z ∈ N, x*w = y*z iseqv := { refl := fun x ↦ ⟨1, N.one_mem, 1, N.one_mem, rfl⟩ symm := fun ⟨w, hw, z, hz, h⟩ ↦ ⟨z, hz, w, hw, h.symm⟩ trans := by sorry } instance [CommMonoid M] : HasQuotient M (Submonoid M) where quotient' := fun N ↦ Quotient N.Setoid def QuotientMonoid.mk [CommMonoid M] (N : Submonoid M) : M → M ⧸ N := Quotient.mk N.Setoid instance [CommMonoid M] (N : Submonoid M) : Monoid (M ⧸ N) where mul := Quotient.map₂ (· * ·) (by sorry ) mul_assoc := by sorry one := QuotientMonoid.mk N 1 one_mul := by sorry mul_one := by sorry
mathematics_in_lean/MIL/C08_Hierarchies/S02_Morphisms.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas set_option autoImplicit true def isMonoidHom₁ [Monoid G] [Monoid H] (f : G → H) : Prop := f 1 = 1 ∧ ∀ g g', f (g * g') = f g * f g' structure isMonoidHom₂ [Monoid G] [Monoid H] (f : G → H) : Prop where map_one : f 1 = 1 map_mul : ∀ g g', f (g * g') = f g * f g' example : Continuous (id : ℝ → ℝ) := continuous_id @[ext] structure MonoidHom₁ (G H : Type) [Monoid G] [Monoid H] where toFun : G → H map_one : toFun 1 = 1 map_mul : ∀ g g', toFun (g * g') = toFun g * toFun g' instance [Monoid G] [Monoid H] : CoeFun (MonoidHom₁ G H) (fun _ ↦ G → H) where coe := MonoidHom₁.toFun attribute [coe] MonoidHom₁.toFun example [Monoid G] [Monoid H] (f : MonoidHom₁ G H) : f 1 = 1 := f.map_one @[ext] structure AddMonoidHom₁ (G H : Type) [AddMonoid G] [AddMonoid H] where toFun : G → H map_zero : toFun 0 = 0 map_add : ∀ g g', toFun (g + g') = toFun g + toFun g' instance [AddMonoid G] [AddMonoid H] : CoeFun (AddMonoidHom₁ G H) (fun _ ↦ G → H) where coe := AddMonoidHom₁.toFun attribute [coe] AddMonoidHom₁.toFun @[ext] structure RingHom₁ (R S : Type) [Ring R] [Ring S] extends MonoidHom₁ R S, AddMonoidHom₁ R S class MonoidHomClass₁ (F : Type) (M N : Type) [Monoid M] [Monoid N] where toFun : F → M → N map_one : ∀ f : F, toFun f 1 = 1 map_mul : ∀ f g g', toFun f (g * g') = toFun f g * toFun f g' def badInst [Monoid M] [Monoid N] [MonoidHomClass₁ F M N] : CoeFun F (fun _ ↦ M → N) where coe := MonoidHomClass₁.toFun class MonoidHomClass₂ (F : Type) (M N : outParam Type) [Monoid M] [Monoid N] where toFun : F → M → N map_one : ∀ f : F, toFun f 1 = 1 map_mul : ∀ f g g', toFun f (g * g') = toFun f g * toFun f g' instance [Monoid M] [Monoid N] [MonoidHomClass₂ F M N] : CoeFun F (fun _ ↦ M → N) where coe := MonoidHomClass₂.toFun attribute [coe] MonoidHomClass₂.toFun instance (M N : Type) [Monoid M] [Monoid N] : MonoidHomClass₂ (MonoidHom₁ M N) M N where toFun := MonoidHom₁.toFun map_one := fun f ↦ f.map_one map_mul := fun f ↦ f.map_mul instance (R S : Type) [Ring R] [Ring S] : MonoidHomClass₂ (RingHom₁ R S) R S where toFun := fun f ↦ f.toMonoidHom₁.toFun map_one := fun f ↦ f.toMonoidHom₁.map_one map_mul := fun f ↦ f.toMonoidHom₁.map_mul lemma map_inv_of_inv [Monoid M] [Monoid N] [MonoidHomClass₂ F M N] (f : F) {m m' : M} (h : m*m' = 1) : f m * f m' = 1 := by rw [← MonoidHomClass₂.map_mul, h, MonoidHomClass₂.map_one] example [Monoid M] [Monoid N] (f : MonoidHom₁ M N) {m m' : M} (h : m*m' = 1) : f m * f m' = 1 := map_inv_of_inv f h example [Ring R] [Ring S] (f : RingHom₁ R S) {r r' : R} (h : r*r' = 1) : f r * f r' = 1 := map_inv_of_inv f h class MonoidHomClass₃ (F : Type) (M N : outParam Type) [Monoid M] [Monoid N] extends DFunLike F M (fun _ ↦ N) where map_one : ∀ f : F, f 1 = 1 map_mul : ∀ (f : F) g g', f (g * g') = f g * f g' instance (M N : Type) [Monoid M] [Monoid N] : MonoidHomClass₃ (MonoidHom₁ M N) M N where coe := MonoidHom₁.toFun coe_injective' _ _ := MonoidHom₁.ext map_one := MonoidHom₁.map_one map_mul := MonoidHom₁.map_mul @[ext] structure OrderPresHom (α β : Type) [LE α] [LE β] where toFun : α → β le_of_le : ∀ a a', a ≤ a' → toFun a ≤ toFun a' @[ext] structure OrderPresMonoidHom (M N : Type) [Monoid M] [LE M] [Monoid N] [LE N] extends MonoidHom₁ M N, OrderPresHom M N class OrderPresHomClass (F : Type) (α β : outParam Type) [LE α] [LE β] instance (α β : Type) [LE α] [LE β] : OrderPresHomClass (OrderPresHom α β) α β where instance (α β : Type) [LE α] [Monoid α] [LE β] [Monoid β] : OrderPresHomClass (OrderPresMonoidHom α β) α β where instance (α β : Type) [LE α] [Monoid α] [LE β] [Monoid β] : MonoidHomClass₃ (OrderPresMonoidHom α β) α β := sorry
mathematics_in_lean/MIL/C08_Hierarchies/solutions/Solutions_S01_Basics.lean
import MIL.Common import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Real.Basic set_option autoImplicit true class One₁ (α : Type) where /-- The element one -/ one : α #check One₁.one -- One₁.one {α : Type} [self : One₁ α] : α @[class] structure One₂ (α : Type) where /-- The element one -/ one : α #check One₂.one example (α : Type) [One₁ α] : α := One₁.one example (α : Type) [One₁ α] := (One₁.one : α) @[inherit_doc] notation "𝟙" => One₁.one example {α : Type} [One₁ α] : α := 𝟙 example {α : Type} [One₁ α] : (𝟙 : α) = 𝟙 := rfl class Dia₁ (α : Type) where dia : α → α → α infixl:70 " ⋄ " => Dia₁.dia class Semigroup₀ (α : Type) where toDia₁ : Dia₁ α /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) attribute [instance] Semigroup₀.toDia₁ example {α : Type} [Semigroup₀ α] (a b : α) : α := a ⋄ b class Semigroup₁ (α : Type) extends toDia₁ : Dia₁ α where /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) example {α : Type} [Semigroup₁ α] (a b : α) : α := a ⋄ b class Semigroup₂ (α : Type) extends Dia₁ α where /-- Diamond is associative -/ dia_assoc : ∀ a b c : α, a ⋄ b ⋄ c = a ⋄ (b ⋄ c) class DiaOneClass₁ (α : Type) extends One₁ α, Dia₁ α where /-- One is a left neutral element for diamond. -/ one_dia : ∀ a : α, 𝟙 ⋄ a = a /-- One is a right neutral element for diamond -/ dia_one : ∀ a : α, a ⋄ 𝟙 = a set_option trace.Meta.synthInstance true in example {α : Type} [DiaOneClass₁ α] (a b : α) : Prop := a ⋄ b = 𝟙 class Monoid₁ (α : Type) extends Semigroup₁ α, DiaOneClass₁ α class Monoid₂ (α : Type) where toSemigroup₁ : Semigroup₁ α toDiaOneClass₁ : DiaOneClass₁ α example {α : Type} [Monoid₁ α] : (Monoid₁.toSemigroup₁.toDia₁.dia : α → α → α) = Monoid₁.toDiaOneClass₁.toDia₁.dia := rfl /- Monoid₂.mk {α : Type} (toSemigroup₁ : Semigroup₁ α) (toDiaOneClass₁ : DiaOneClass₁ α) : Monoid₂ α -/ #check Monoid₂.mk /- Monoid₁.mk {α : Type} [toSemigroup₁ : Semigroup₁ α] [toOne₁ : One₁ α] (one_dia : ∀ (a : α), 𝟙 ⋄ a = a) (dia_one : ∀ (a : α), a ⋄ 𝟙 = a) : Monoid₁ α -/ #check Monoid₁.mk #check Monoid₁.toSemigroup₁ #check Monoid₁.toDiaOneClass₁ class Inv₁ (α : Type) where /-- The inversion function -/ inv : α → α @[inherit_doc] postfix:max "⁻¹" => Inv₁.inv class Group₁ (G : Type) extends Monoid₁ G, Inv₁ G where inv_dia : ∀ a : G, a⁻¹ ⋄ a = 𝟙 lemma left_inv_eq_right_inv₁ {M : Type} [Monoid₁ M] {a b c : M} (hba : b ⋄ a = 𝟙) (hac : a ⋄ c = 𝟙) : b = c := by rw [← DiaOneClass₁.one_dia c, ← hba, Semigroup₁.dia_assoc, hac, DiaOneClass₁.dia_one b] export DiaOneClass₁ (one_dia dia_one) export Semigroup₁ (dia_assoc) export Group₁ (inv_dia) example {M : Type} [Monoid₁ M] {a b c : M} (hba : b ⋄ a = 𝟙) (hac : a ⋄ c = 𝟙) : b = c := by rw [← one_dia c, ← hba, dia_assoc, hac, dia_one b] lemma inv_eq_of_dia [Group₁ G] {a b : G} (h : a ⋄ b = 𝟙) : a⁻¹ = b := left_inv_eq_right_inv₁ (inv_dia a) h lemma dia_inv [Group₁ G] (a : G) : a ⋄ a⁻¹ = 𝟙 := by rw [← inv_dia a⁻¹, inv_eq_of_dia (inv_dia a)] class AddSemigroup₃ (α : Type) extends Add α where /-- Addition is associative -/ add_assoc₃ : ∀ a b c : α, a + b + c = a + (b + c) @[to_additive AddSemigroup₃] class Semigroup₃ (α : Type) extends Mul α where /-- Multiplication is associative -/ mul_assoc₃ : ∀ a b c : α, a * b * c = a * (b * c) class AddMonoid₃ (α : Type) extends AddSemigroup₃ α, AddZeroClass α @[to_additive AddMonoid₃] class Monoid₃ (α : Type) extends Semigroup₃ α, MulOneClass α export Semigroup₃ (mul_assoc₃) export AddSemigroup₃ (add_assoc₃) whatsnew in @[to_additive] lemma left_inv_eq_right_inv' {M : Type} [Monoid₃ M] {a b c : M} (hba : b * a = 1) (hac : a * c = 1) : b = c := by rw [← one_mul c, ← hba, mul_assoc₃, hac, mul_one b] #check left_neg_eq_right_neg' class AddCommSemigroup₃ (α : Type) extends AddSemigroup₃ α where add_comm : ∀ a b : α, a + b = b + a @[to_additive AddCommSemigroup₃] class CommSemigroup₃ (α : Type) extends Semigroup₃ α where mul_comm : ∀ a b : α, a * b = b * a class AddCommMonoid₃ (α : Type) extends AddMonoid₃ α, AddCommSemigroup₃ α @[to_additive AddCommMonoid₃] class CommMonoid₃ (α : Type) extends Monoid₃ α, CommSemigroup₃ α class AddGroup₃ (G : Type) extends AddMonoid₃ G, Neg G where neg_add : ∀ a : G, -a + a = 0 @[to_additive AddGroup₃] class Group₃ (G : Type) extends Monoid₃ G, Inv G where inv_mul : ∀ a : G, a⁻¹ * a = 1 attribute [simp] Group₃.inv_mul AddGroup₃.neg_add @[to_additive] lemma inv_eq_of_mul [Group₃ G] {a b : G} (h : a * b = 1) : a⁻¹ = b := left_inv_eq_right_inv' (Group₃.inv_mul a) h @[to_additive (attr := simp)] lemma Group₃.mul_inv {G : Type} [Group₃ G] {a : G} : a * a⁻¹ = 1 := by rw [← inv_mul a⁻¹, inv_eq_of_mul (inv_mul a)] @[to_additive] lemma mul_left_cancel₃ {G : Type} [Group₃ G] {a b c : G} (h : a * b = a * c) : b = c := by simpa [← mul_assoc₃] using congr_arg (a⁻¹ * ·) h @[to_additive] lemma mul_right_cancel₃ {G : Type} [Group₃ G] {a b c : G} (h : b*a = c*a) : b = c := by simpa [mul_assoc₃] using congr_arg (· * a⁻¹) h class AddCommGroup₃ (G : Type) extends AddGroup₃ G, AddCommMonoid₃ G @[to_additive AddCommGroup₃] class CommGroup₃ (G : Type) extends Group₃ G, CommMonoid₃ G class Ring₃ (R : Type) extends AddGroup₃ R, Monoid₃ R, MulZeroClass R where /-- Multiplication is left distributive over addition -/ left_distrib : ∀ a b c : R, a * (b + c) = a * b + a * c /-- Multiplication is right distributive over addition -/ right_distrib : ∀ a b c : R, (a + b) * c = a * c + b * c instance {R : Type} [Ring₃ R] : AddCommGroup₃ R := { add_comm := by intro a b have : a + (a + b + b) = a + (b + a + b) := calc a + (a + b + b) = (a + a) + (b + b) := by simp [add_assoc₃, add_assoc₃] _ = (1 * a + 1 * a) + (1 * b + 1 * b) := by simp _ = (1 + 1) * a + (1 + 1) * b := by simp [Ring₃.right_distrib] _ = (1 + 1) * (a + b) := by simp [Ring₃.left_distrib] _ = 1 * (a + b) + 1 * (a + b) := by simp [Ring₃.right_distrib] _ = (a + b) + (a + b) := by simp _ = a + (b + a + b) := by simp [add_assoc₃] exact add_right_cancel₃ (add_left_cancel₃ this) } instance : Ring₃ ℤ where add := (· + ·) add_assoc₃ := add_assoc zero := 0 zero_add := by simp add_zero := by simp neg := (- ·) neg_add := by simp mul := (· * ·) mul_assoc₃ := mul_assoc one := 1 one_mul := by simp mul_one := by simp zero_mul := by simp mul_zero := by simp left_distrib := Int.mul_add right_distrib := Int.add_mul class LE₁ (α : Type) where /-- The Less-or-Equal relation. -/ le : α → α → Prop @[inherit_doc] infix:50 " ≤₁ " => LE₁.le class Preorder₁ (α : Type) extends LE₁ α where le_refl : ∀ a : α, a ≤₁ a le_trans : ∀ a b c : α, a ≤₁ b → b ≤₁ c → a ≤₁ c class PartialOrder₁ (α : Type) extends Preorder₁ α where le_antisymm : ∀ a b : α, a ≤₁ b → b ≤₁ a → a = b class OrderedCommMonoid₁ (α : Type) extends PartialOrder₁ α, CommMonoid₃ α where mul_of_le : ∀ a b : α, a ≤₁ b → ∀ c : α, c * a ≤₁ c * b instance : OrderedCommMonoid₁ ℕ where le := (· ≤ ·) le_refl := fun _ ↦ le_rfl le_trans := fun _ _ _ ↦ le_trans le_antisymm := fun _ _ ↦ le_antisymm mul := (· * ·) mul_assoc₃ := mul_assoc one := 1 one_mul := one_mul mul_one := mul_one mul_comm := mul_comm mul_of_le := fun _ _ h c ↦ Nat.mul_le_mul_left c h class SMul₃ (α : Type) (β : Type) where /-- Scalar multiplication -/ smul : α → β → β infixr:73 " • " => SMul₃.smul class Module₁ (R : Type) [Ring₃ R] (M : Type) [AddCommGroup₃ M] extends SMul₃ R M where zero_smul : ∀ m : M, (0 : R) • m = 0 one_smul : ∀ m : M, (1 : R) • m = m mul_smul : ∀ (a b : R) (m : M), (a * b) • m = a • b • m add_smul : ∀ (a b : R) (m : M), (a + b) • m = a • m + b • m smul_add : ∀ (a : R) (m n : M), a • (m + n) = a • m + a • n instance selfModule (R : Type) [Ring₃ R] : Module₁ R R where smul := fun r s ↦ r*s zero_smul := zero_mul one_smul := one_mul mul_smul := mul_assoc₃ add_smul := Ring₃.right_distrib smul_add := Ring₃.left_distrib def nsmul₁ {M : Type*} [Zero M] [Add M] : ℕ → M → M | 0, _ => 0 | n + 1, a => a + nsmul₁ n a def zsmul₁ {M : Type*} [Zero M] [Add M] [Neg M] : ℤ → M → M | Int.ofNat n, a => nsmul₁ n a | Int.negSucc n, a => -nsmul₁ n.succ a instance abGrpModule (A : Type) [AddCommGroup₃ A] : Module₁ ℤ A where smul := zsmul₁ zero_smul := sorry one_smul := sorry mul_smul := sorry add_smul := sorry smul_add := sorry #synth Module₁ ℤ ℤ -- abGrpModule ℤ class AddMonoid₄ (M : Type) extends AddSemigroup₃ M, AddZeroClass M where /-- Multiplication by a natural number. -/ nsmul : ℕ → M → M := nsmul₁ /-- Multiplication by `(0 : ℕ)` gives `0`. -/ nsmul_zero : ∀ x, nsmul 0 x = 0 := by intros; rfl /-- Multiplication by `(n + 1 : ℕ)` behaves as expected. -/ nsmul_succ : ∀ (n : ℕ) (x), nsmul (n + 1) x = x + nsmul n x := by intros; rfl instance mySMul {M : Type} [AddMonoid₄ M] : SMul ℕ M := ⟨AddMonoid₄.nsmul⟩ instance (M N : Type) [AddMonoid₄ M] [AddMonoid₄ N] : AddMonoid₄ (M × N) where add := fun p q ↦ (p.1 + q.1, p.2 + q.2) add_assoc₃ := fun a b c ↦ by ext <;> apply add_assoc₃ zero := (0, 0) zero_add := fun a ↦ by ext <;> apply zero_add add_zero := fun a ↦ by ext <;> apply add_zero instance : AddMonoid₄ ℤ where add := (· + ·) add_assoc₃ := Int.add_assoc zero := 0 zero_add := Int.zero_add add_zero := Int.add_zero nsmul := fun n m ↦ (n : ℤ) * m nsmul_zero := Int.zero_mul nsmul_succ := fun n m ↦ show (n + 1 : ℤ) * m = m + n * m by rw [Int.add_mul, Int.add_comm, Int.one_mul] example (n : ℕ) (m : ℤ) : SMul.smul (self := mySMul) n m = n * m := rfl class LT₁ (α : Type) where /-- The Less-Than relation -/ lt : α → α → Prop @[inherit_doc] infix:50 " <₁ " => LT₁.lt class PreOrder₂ (α : Type) extends LE₁ α, LT₁ α where le_refl : ∀ a : α, a ≤₁ a le_trans : ∀ a b c : α, a ≤₁ b → b ≤₁ c → a ≤₁ c lt := fun a b ↦ a ≤₁ b ∧ ¬b ≤₁ a lt_iff_le_not_le : ∀ a b : α, a <₁ b ↔ a ≤₁ b ∧ ¬b ≤₁ a := by intros; rfl
mathematics_in_lean/MIL/C08_Hierarchies/solutions/Solutions_S02_Morphisms.lean
import MIL.Common import Mathlib.Topology.Instances.Real.Lemmas set_option autoImplicit true def isMonoidHom₁ [Monoid G] [Monoid H] (f : G → H) : Prop := f 1 = 1 ∧ ∀ g g', f (g * g') = f g * f g' structure isMonoidHom₂ [Monoid G] [Monoid H] (f : G → H) : Prop where map_one : f 1 = 1 map_mul : ∀ g g', f (g * g') = f g * f g' example : Continuous (id : ℝ → ℝ) := continuous_id @[ext] structure MonoidHom₁ (G H : Type) [Monoid G] [Monoid H] where toFun : G → H map_one : toFun 1 = 1 map_mul : ∀ g g', toFun (g * g') = toFun g * toFun g' instance [Monoid G] [Monoid H] : CoeFun (MonoidHom₁ G H) (fun _ ↦ G → H) where coe := MonoidHom₁.toFun attribute [coe] MonoidHom₁.toFun example [Monoid G] [Monoid H] (f : MonoidHom₁ G H) : f 1 = 1 := f.map_one @[ext] structure AddMonoidHom₁ (G H : Type) [AddMonoid G] [AddMonoid H] where toFun : G → H map_zero : toFun 0 = 0 map_add : ∀ g g', toFun (g + g') = toFun g + toFun g' instance [AddMonoid G] [AddMonoid H] : CoeFun (AddMonoidHom₁ G H) (fun _ ↦ G → H) where coe := AddMonoidHom₁.toFun attribute [coe] AddMonoidHom₁.toFun @[ext] structure RingHom₁ (R S : Type) [Ring R] [Ring S] extends MonoidHom₁ R S, AddMonoidHom₁ R S class MonoidHomClass₁ (F : Type) (M N : Type) [Monoid M] [Monoid N] where toFun : F → M → N map_one : ∀ f : F, toFun f 1 = 1 map_mul : ∀ f g g', toFun f (g * g') = toFun f g * toFun f g' def badInst [Monoid M] [Monoid N] [MonoidHomClass₁ F M N] : CoeFun F (fun _ ↦ M → N) where coe := MonoidHomClass₁.toFun class MonoidHomClass₂ (F : Type) (M N : outParam Type) [Monoid M] [Monoid N] where toFun : F → M → N map_one : ∀ f : F, toFun f 1 = 1 map_mul : ∀ f g g', toFun f (g * g') = toFun f g * toFun f g' instance [Monoid M] [Monoid N] [MonoidHomClass₂ F M N] : CoeFun F (fun _ ↦ M → N) where coe := MonoidHomClass₂.toFun attribute [coe] MonoidHomClass₂.toFun instance (M N : Type) [Monoid M] [Monoid N] : MonoidHomClass₂ (MonoidHom₁ M N) M N where toFun := MonoidHom₁.toFun map_one := fun f ↦ f.map_one map_mul := fun f ↦ f.map_mul instance (R S : Type) [Ring R] [Ring S] : MonoidHomClass₂ (RingHom₁ R S) R S where toFun := fun f ↦ f.toMonoidHom₁.toFun map_one := fun f ↦ f.toMonoidHom₁.map_one map_mul := fun f ↦ f.toMonoidHom₁.map_mul lemma map_inv_of_inv [Monoid M] [Monoid N] [MonoidHomClass₂ F M N] (f : F) {m m' : M} (h : m*m' = 1) : f m * f m' = 1 := by rw [← MonoidHomClass₂.map_mul, h, MonoidHomClass₂.map_one] example [Monoid M] [Monoid N] (f : MonoidHom₁ M N) {m m' : M} (h : m*m' = 1) : f m * f m' = 1 := map_inv_of_inv f h example [Ring R] [Ring S] (f : RingHom₁ R S) {r r' : R} (h : r*r' = 1) : f r * f r' = 1 := map_inv_of_inv f h class MonoidHomClass₃ (F : Type) (M N : outParam Type) [Monoid M] [Monoid N] extends DFunLike F M (fun _ ↦ N) where map_one : ∀ f : F, f 1 = 1 map_mul : ∀ (f : F) g g', f (g * g') = f g * f g' instance (M N : Type) [Monoid M] [Monoid N] : MonoidHomClass₃ (MonoidHom₁ M N) M N where coe := MonoidHom₁.toFun coe_injective' _ _ := MonoidHom₁.ext map_one := MonoidHom₁.map_one map_mul := MonoidHom₁.map_mul @[ext] structure OrderPresHom (α β : Type) [LE α] [LE β] where toFun : α → β le_of_le : ∀ a a', a ≤ a' → toFun a ≤ toFun a' @[ext] structure OrderPresMonoidHom (M N : Type) [Monoid M] [LE M] [Monoid N] [LE N] extends MonoidHom₁ M N, OrderPresHom M N class OrderPresHomClass (F : Type) (α β : outParam Type) [LE α] [LE β] extends DFunLike F α (fun _ ↦ β) where le_of_le : ∀ (f : F) a a', a ≤ a' → f a ≤ f a' instance (α β : Type) [LE α] [LE β] : OrderPresHomClass (OrderPresHom α β) α β where coe := OrderPresHom.toFun coe_injective' _ _ := OrderPresHom.ext le_of_le := OrderPresHom.le_of_le instance (α β : Type) [LE α] [Monoid α] [LE β] [Monoid β] : OrderPresHomClass (OrderPresMonoidHom α β) α β where coe := fun f ↦ f.toOrderPresHom.toFun coe_injective' _ _ := OrderPresMonoidHom.ext le_of_le := fun f ↦ f.toOrderPresHom.le_of_le instance (α β : Type) [LE α] [Monoid α] [LE β] [Monoid β] : MonoidHomClass₃ (OrderPresMonoidHom α β) α β where coe := fun f ↦ f.toOrderPresHom.toFun coe_injective' _ _ := OrderPresMonoidHom.ext map_one := fun f ↦ f.toMonoidHom₁.map_one map_mul := fun f ↦ f.toMonoidHom₁.map_mul
mathematics_in_lean/MIL/C08_Hierarchies/solutions/Solutions_S03_Subobjects.lean
import MIL.Common import Mathlib.GroupTheory.QuotientGroup.Basic set_option autoImplicit true @[ext] structure Submonoid₁ (M : Type) [Monoid M] where /-- The carrier of a submonoid. -/ carrier : Set M /-- The product of two elements of a submonoid belongs to the submonoid. -/ mul_mem {a b} : a ∈ carrier → b ∈ carrier → a * b ∈ carrier /-- The unit element belongs to the submonoid. -/ one_mem : 1 ∈ carrier /-- Submonoids in `M` can be seen as sets in `M`. -/ instance [Monoid M] : SetLike (Submonoid₁ M) M where coe := Submonoid₁.carrier coe_injective' _ _ := Submonoid₁.ext example [Monoid M] (N : Submonoid₁ M) : 1 ∈ N := N.one_mem example [Monoid M] (N : Submonoid₁ M) (α : Type) (f : M → α) := f '' N example [Monoid M] (N : Submonoid₁ M) (x : N) : (x : M) ∈ N := x.property instance SubMonoid₁Monoid [Monoid M] (N : Submonoid₁ M) : Monoid N where mul := fun x y ↦ ⟨x*y, N.mul_mem x.property y.property⟩ mul_assoc := fun x y z ↦ SetCoe.ext (mul_assoc (x : M) y z) one := ⟨1, N.one_mem⟩ one_mul := fun x ↦ SetCoe.ext (one_mul (x : M)) mul_one := fun x ↦ SetCoe.ext (mul_one (x : M)) example [Monoid M] (N : Submonoid₁ M) : Monoid N where mul := fun ⟨x, hx⟩ ⟨y, hy⟩ ↦ ⟨x*y, N.mul_mem hx hy⟩ mul_assoc := fun ⟨x, _⟩ ⟨y, _⟩ ⟨z, _⟩ ↦ SetCoe.ext (mul_assoc x y z) one := ⟨1, N.one_mem⟩ one_mul := fun ⟨x, _⟩ ↦ SetCoe.ext (one_mul x) mul_one := fun ⟨x, _⟩ ↦ SetCoe.ext (mul_one x) class SubmonoidClass₁ (S : Type) (M : Type) [Monoid M] [SetLike S M] : Prop where mul_mem : ∀ (s : S) {a b : M}, a ∈ s → b ∈ s → a * b ∈ s one_mem : ∀ s : S, 1 ∈ s instance [Monoid M] : SubmonoidClass₁ (Submonoid₁ M) M where mul_mem := Submonoid₁.mul_mem one_mem := Submonoid₁.one_mem @[ext] structure Subgroup₁ (G : Type) [Group G] extends Submonoid₁ G where /-- The inverse of an element of a subgroup belongs to the subgroup. -/ inv_mem {a} : a ∈ carrier → a⁻¹ ∈ carrier /-- Subgroups in `M` can be seen as sets in `M`. -/ instance [Group G] : SetLike (Subgroup₁ G) G where coe := fun H ↦ H.toSubmonoid₁.carrier coe_injective' _ _ := Subgroup₁.ext instance [Group G] (H : Subgroup₁ G) : Group H := { SubMonoid₁Monoid H.toSubmonoid₁ with inv := fun x ↦ ⟨x⁻¹, H.inv_mem x.property⟩ inv_mul_cancel := fun x ↦ SetCoe.ext (inv_mul_cancel (x : G)) } class SubgroupClass₁ (S : Type) (G : Type) [Group G] [SetLike S G] : Prop extends SubmonoidClass₁ S G where inv_mem : ∀ (s : S) {a : G}, a ∈ s → a⁻¹ ∈ s instance [Group G] : SubmonoidClass₁ (Subgroup₁ G) G where mul_mem := fun H ↦ H.toSubmonoid₁.mul_mem one_mem := fun H ↦ H.toSubmonoid₁.one_mem instance [Group G] : SubgroupClass₁ (Subgroup₁ G) G := { (inferInstance : SubmonoidClass₁ (Subgroup₁ G) G) with inv_mem := Subgroup₁.inv_mem } instance [Monoid M] : Min (Submonoid₁ M) := ⟨fun S₁ S₂ ↦ { carrier := S₁ ∩ S₂ one_mem := ⟨S₁.one_mem, S₂.one_mem⟩ mul_mem := fun ⟨hx, hx'⟩ ⟨hy, hy'⟩ ↦ ⟨S₁.mul_mem hx hy, S₂.mul_mem hx' hy'⟩ }⟩ example [Monoid M] (N P : Submonoid₁ M) : Submonoid₁ M := N ⊓ P def Submonoid.Setoid [CommMonoid M] (N : Submonoid M) : Setoid M where r := fun x y ↦ ∃ w ∈ N, ∃ z ∈ N, x*w = y*z iseqv := { refl := fun x ↦ ⟨1, N.one_mem, 1, N.one_mem, rfl⟩ symm := fun ⟨w, hw, z, hz, h⟩ ↦ ⟨z, hz, w, hw, h.symm⟩ trans := by rintro a b c ⟨w, hw, z, hz, h⟩ ⟨w', hw', z', hz', h'⟩ refine ⟨w*w', N.mul_mem hw hw', z*z', N.mul_mem hz hz', ?_⟩ rw [← mul_assoc, h, mul_comm b, mul_assoc, h', ← mul_assoc, mul_comm z, mul_assoc] } instance [CommMonoid M] : HasQuotient M (Submonoid M) where quotient' := fun N ↦ Quotient N.Setoid def QuotientMonoid.mk [CommMonoid M] (N : Submonoid M) : M → M ⧸ N := Quotient.mk N.Setoid instance [CommMonoid M] (N : Submonoid M) : Monoid (M ⧸ N) where mul := Quotient.map₂ (· * ·) (by rintro a₁ b₁ ⟨w, hw, z, hz, ha⟩ a₂ b₂ ⟨w', hw', z', hz', hb⟩ refine ⟨w*w', N.mul_mem hw hw', z*z', N.mul_mem hz hz', ?_⟩ rw [mul_comm w, ← mul_assoc, mul_assoc a₁, hb, mul_comm, ← mul_assoc, mul_comm w, ha, mul_assoc, mul_comm z, mul_assoc b₂, mul_comm z', mul_assoc] ) mul_assoc := by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ apply Quotient.sound dsimp only rw [mul_assoc] apply @Setoid.refl M N.Setoid one := QuotientMonoid.mk N 1 one_mul := by rintro ⟨a⟩ ; apply Quotient.sound ; dsimp only ; rw [one_mul] ; apply @Setoid.refl M N.Setoid mul_one := by rintro ⟨a⟩ ; apply Quotient.sound ; dsimp only ; rw [mul_one] ; apply @Setoid.refl M N.Setoid
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/S03_Integration.lean
import MIL.Common import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Convolution import Mathlib.MeasureTheory.Function.Jacobian import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.MeasureTheory.Measure.Lebesgue.Basic open Set Filter open Topology Filter ENNReal open MeasureTheory noncomputable section variable {α : Type*} [MeasurableSpace α] variable {μ : Measure α} section variable {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E] {f : α → E} example {f g : α → E} (hf : Integrable f μ) (hg : Integrable g μ) : ∫ a, f a + g a ∂μ = ∫ a, f a ∂μ + ∫ a, g a ∂μ := integral_add hf hg example {s : Set α} (c : E) : ∫ x in s, c ∂μ = (μ s).toReal • c := setIntegral_const c open Filter example {F : ℕ → α → E} {f : α → E} (bound : α → ℝ) (hmeas : ∀ n, AEStronglyMeasurable (F n) μ) (hint : Integrable bound μ) (hbound : ∀ n, ∀ᵐ a ∂μ, ‖F n a‖ ≤ bound a) (hlim : ∀ᵐ a ∂μ, Tendsto (fun n : ℕ ↦ F n a) atTop (𝓝 (f a))) : Tendsto (fun n ↦ ∫ a, F n a ∂μ) atTop (𝓝 (∫ a, f a ∂μ)) := tendsto_integral_of_dominated_convergence bound hmeas hint hbound hlim example {α : Type*} [MeasurableSpace α] {μ : Measure α} [SigmaFinite μ] {β : Type*} [MeasurableSpace β] {ν : Measure β} [SigmaFinite ν] (f : α × β → E) (hf : Integrable f (μ.prod ν)) : ∫ z, f z ∂ μ.prod ν = ∫ x, ∫ y, f (x, y) ∂ν ∂μ := integral_prod f hf end section open Convolution variable {𝕜 : Type*} {G : Type*} {E : Type*} {E' : Type*} {F : Type*} [NormedAddCommGroup E] [NormedAddCommGroup E'] [NormedAddCommGroup F] [NontriviallyNormedField 𝕜] [NormedSpace 𝕜 E] [NormedSpace 𝕜 E'] [NormedSpace 𝕜 F] [MeasurableSpace G] [NormedSpace ℝ F] [CompleteSpace F] [Sub G] example (f : G → E) (g : G → E') (L : E →L[𝕜] E' →L[𝕜] F) (μ : Measure G) : f ⋆[L, μ] g = fun x ↦ ∫ t, L (f t) (g (x - t)) ∂μ := rfl end example {E : Type*} [NormedAddCommGroup E] [NormedSpace ℝ E] [FiniteDimensional ℝ E] [MeasurableSpace E] [BorelSpace E] (μ : Measure E) [μ.IsAddHaarMeasure] {F : Type*} [NormedAddCommGroup F] [NormedSpace ℝ F] [CompleteSpace F] {s : Set E} {f : E → E} {f' : E → E →L[ℝ] E} (hs : MeasurableSet s) (hf : ∀ x : E, x ∈ s → HasFDerivWithinAt f (f' x) s x) (h_inj : InjOn f s) (g : E → F) : ∫ x in f '' s, g x ∂μ = ∫ x in s, |(f' x).det| • g (f x) ∂μ := integral_image_eq_integral_abs_det_fderiv_smul μ hs hf h_inj g
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/S02_Measure_Theory.lean
import MIL.Common import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Convolution import Mathlib.MeasureTheory.Function.Jacobian import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.MeasureTheory.Measure.Lebesgue.Basic open Set Filter noncomputable section variable {α : Type*} [MeasurableSpace α] example : MeasurableSet (∅ : Set α) := MeasurableSet.empty example : MeasurableSet (univ : Set α) := MeasurableSet.univ example {s : Set α} (hs : MeasurableSet s) : MeasurableSet (sᶜ) := hs.compl example : Encodable ℕ := by infer_instance example (n : ℕ) : Encodable (Fin n) := by infer_instance variable {ι : Type*} [Encodable ι] example {f : ι → Set α} (h : ∀ b, MeasurableSet (f b)) : MeasurableSet (⋃ b, f b) := MeasurableSet.iUnion h example {f : ι → Set α} (h : ∀ b, MeasurableSet (f b)) : MeasurableSet (⋂ b, f b) := MeasurableSet.iInter h open MeasureTheory Function variable {μ : Measure α} example (s : Set α) : μ s = ⨅ (t : Set α) (_ : s ⊆ t) (_ : MeasurableSet t), μ t := measure_eq_iInf s example (s : ι → Set α) : μ (⋃ i, s i) ≤ ∑' i, μ (s i) := measure_iUnion_le s example {f : ℕ → Set α} (hmeas : ∀ i, MeasurableSet (f i)) (hdis : Pairwise (Disjoint on f)) : μ (⋃ i, f i) = ∑' i, μ (f i) := μ.m_iUnion hmeas hdis example {P : α → Prop} : (∀ᵐ x ∂μ, P x) ↔ ∀ᶠ x in ae μ, P x := Iff.rfl
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/S01_Elementary_Integration.lean
import MIL.Common import Mathlib.Analysis.SpecialFunctions.Integrals.Basic import Mathlib.Analysis.Convolution open Set Filter open Topology Filter noncomputable section open MeasureTheory intervalIntegral open Interval -- this introduces the notation `[[a, b]]` for the segment from `min a b` to `max a b` example (a b : ℝ) : (∫ x in a..b, x) = (b ^ 2 - a ^ 2) / 2 := integral_id example {a b : ℝ} (h : (0 : ℝ) ∉ [[a, b]]) : (∫ x in a..b, 1 / x) = Real.log (b / a) := integral_one_div h example (f : ℝ → ℝ) (hf : Continuous f) (a b : ℝ) : deriv (fun u ↦ ∫ x : ℝ in a..u, f x) b = f b := (integral_hasStrictDerivAt_right (hf.intervalIntegrable _ _) (hf.stronglyMeasurableAtFilter _ _) hf.continuousAt).hasDerivAt.deriv example {f : ℝ → ℝ} {a b : ℝ} {f' : ℝ → ℝ} (h : ∀ x ∈ [[a, b]], HasDerivAt f (f' x) x) (h' : IntervalIntegrable f' volume a b) : (∫ y in a..b, f' y) = f b - f a := integral_eq_sub_of_hasDerivAt h h' open Convolution example (f : ℝ → ℝ) (g : ℝ → ℝ) : f ⋆ g = fun x ↦ ∫ t, f t * g (x - t) := rfl
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/solutions/Solutions_S01_Elementary_Integration.lean
import MIL.Common import Mathlib.Analysis.SpecialFunctions.Integrals.Basic import Mathlib.Analysis.Convolution open Set Filter open Topology Filter noncomputable section
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/solutions/Solutions_S03_Integration.lean
import MIL.Common import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Convolution import Mathlib.MeasureTheory.Function.Jacobian import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.MeasureTheory.Measure.Lebesgue.Basic open Set Filter open Topology Filter ENNReal open MeasureTheory noncomputable section variable {α : Type*} [MeasurableSpace α] variable {μ : Measure α}
mathematics_in_lean/MIL/C13_Integration_and_Measure_Theory/solutions/Solutions_S02_Measure_Theory.lean
import MIL.Common import Mathlib.Analysis.Normed.Module.FiniteDimension import Mathlib.Analysis.Convolution import Mathlib.MeasureTheory.Function.Jacobian import Mathlib.MeasureTheory.Integral.Bochner.Basic import Mathlib.MeasureTheory.Measure.Lebesgue.Basic open Set Filter noncomputable section variable {α : Type*} [MeasurableSpace α] variable {ι : Type*} [Encodable ι] open MeasureTheory Function variable {μ : Measure α}
mathematics_in_lean/MIL/C03_Logic/S05_Disjunction.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S05 section variable {x y : ℝ} example (h : y > x ^ 2) : y > 0 ∨ y < -1 := by left linarith [pow_two_nonneg x] example (h : -y > x ^ 2 + 1) : y > 0 ∨ y < -1 := by right linarith [pow_two_nonneg x] example (h : y > 0) : y > 0 ∨ y < -1 := Or.inl h example (h : y < -1) : y > 0 ∨ y < -1 := Or.inr h example : x < |y| → x < y ∨ x < -y := by rcases le_or_gt 0 y with h | h · rw [abs_of_nonneg h] intro h; left; exact h · rw [abs_of_neg h] intro h; right; exact h example : x < |y| → x < y ∨ x < -y := by cases le_or_gt 0 y case inl h => rw [abs_of_nonneg h] intro h; left; exact h case inr h => rw [abs_of_neg h] intro h; right; exact h example : x < |y| → x < y ∨ x < -y := by cases le_or_gt 0 y next h => rw [abs_of_nonneg h] intro h; left; exact h next h => rw [abs_of_neg h] intro h; right; exact h example : x < |y| → x < y ∨ x < -y := by match le_or_gt 0 y with | Or.inl h => rw [abs_of_nonneg h] intro h; left; exact h | Or.inr h => rw [abs_of_neg h] intro h; right; exact h namespace MyAbs theorem le_abs_self (x : ℝ) : x ≤ |x| := by sorry theorem neg_le_abs_self (x : ℝ) : -x ≤ |x| := by sorry theorem abs_add (x y : ℝ) : |x + y| ≤ |x| + |y| := by sorry theorem lt_abs : x < |y| ↔ x < y ∨ x < -y := by sorry theorem abs_lt : |x| < y ↔ -y < x ∧ x < y := by sorry end MyAbs end example {x : ℝ} (h : x ≠ 0) : x < 0 ∨ x > 0 := by rcases lt_trichotomy x 0 with xlt | xeq | xgt · left exact xlt · contradiction · right; exact xgt example {m n k : ℕ} (h : m ∣ n ∨ m ∣ k) : m ∣ n * k := by rcases h with ⟨a, rfl⟩ | ⟨b, rfl⟩ · rw [mul_assoc] apply dvd_mul_right · rw [mul_comm, mul_assoc] apply dvd_mul_right example {z : ℝ} (h : ∃ x y, z = x ^ 2 + y ^ 2 ∨ z = x ^ 2 + y ^ 2 + 1) : z ≥ 0 := by sorry example {x : ℝ} (h : x ^ 2 = 1) : x = 1 ∨ x = -1 := by sorry example {x y : ℝ} (h : x ^ 2 = y ^ 2) : x = y ∨ x = -y := by sorry section variable {R : Type*} [CommRing R] [IsDomain R] variable (x y : R) example (h : x ^ 2 = 1) : x = 1 ∨ x = -1 := by sorry example (h : x ^ 2 = y ^ 2) : x = y ∨ x = -y := by sorry end example (P : Prop) : ¬¬P → P := by intro h cases em P · assumption · contradiction example (P : Prop) : ¬¬P → P := by intro h by_cases h' : P · assumption contradiction example (P Q : Prop) : P → Q ↔ ¬P ∨ Q := by sorry
mathematics_in_lean/MIL/C03_Logic/S04_Conjunction_and_Iff.lean
import MIL.Common import Mathlib.Data.Real.Basic import Mathlib.Data.Nat.Prime.Basic namespace C03S04 example {x y : ℝ} (h₀ : x ≤ y) (h₁ : ¬y ≤ x) : x ≤ y ∧ x ≠ y := by constructor · assumption intro h apply h₁ rw [h] example {x y : ℝ} (h₀ : x ≤ y) (h₁ : ¬y ≤ x) : x ≤ y ∧ x ≠ y := ⟨h₀, fun h ↦ h₁ (by rw [h])⟩ example {x y : ℝ} (h₀ : x ≤ y) (h₁ : ¬y ≤ x) : x ≤ y ∧ x ≠ y := have h : x ≠ y := by contrapose! h₁ rw [h₁] ⟨h₀, h⟩ example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by rcases h with ⟨h₀, h₁⟩ contrapose! h₁ exact le_antisymm h₀ h₁ example {x y : ℝ} : x ≤ y ∧ x ≠ y → ¬y ≤ x := by rintro ⟨h₀, h₁⟩ h' exact h₁ (le_antisymm h₀ h') example {x y : ℝ} : x ≤ y ∧ x ≠ y → ¬y ≤ x := fun ⟨h₀, h₁⟩ h' ↦ h₁ (le_antisymm h₀ h') example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by have ⟨h₀, h₁⟩ := h contrapose! h₁ exact le_antisymm h₀ h₁ example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by cases h case intro h₀ h₁ => contrapose! h₁ exact le_antisymm h₀ h₁ example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by cases h next h₀ h₁ => contrapose! h₁ exact le_antisymm h₀ h₁ example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by match h with | ⟨h₀, h₁⟩ => contrapose! h₁ exact le_antisymm h₀ h₁ example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := by intro h' apply h.right exact le_antisymm h.left h' example {x y : ℝ} (h : x ≤ y ∧ x ≠ y) : ¬y ≤ x := fun h' ↦ h.right (le_antisymm h.left h') example {m n : ℕ} (h : m ∣ n ∧ m ≠ n) : m ∣ n ∧ ¬n ∣ m := sorry example : ∃ x : ℝ, 2 < x ∧ x < 4 := ⟨5 / 2, by norm_num, by norm_num⟩ example (x y : ℝ) : (∃ z : ℝ, x < z ∧ z < y) → x < y := by rintro ⟨z, xltz, zlty⟩ exact lt_trans xltz zlty example (x y : ℝ) : (∃ z : ℝ, x < z ∧ z < y) → x < y := fun ⟨z, xltz, zlty⟩ ↦ lt_trans xltz zlty example : ∃ x : ℝ, 2 < x ∧ x < 4 := by use 5 / 2 constructor <;> norm_num example : ∃ m n : ℕ, 4 < m ∧ m < n ∧ n < 10 ∧ Nat.Prime m ∧ Nat.Prime n := by use 5 use 7 norm_num example {x y : ℝ} : x ≤ y ∧ x ≠ y → x ≤ y ∧ ¬y ≤ x := by rintro ⟨h₀, h₁⟩ use h₀ exact fun h' ↦ h₁ (le_antisymm h₀ h') example {x y : ℝ} (h : x ≤ y) : ¬y ≤ x ↔ x ≠ y := by constructor · contrapose! rintro rfl rfl contrapose! exact le_antisymm h example {x y : ℝ} (h : x ≤ y) : ¬y ≤ x ↔ x ≠ y := ⟨fun h₀ h₁ ↦ h₀ (by rw [h₁]), fun h₀ h₁ ↦ h₀ (le_antisymm h h₁)⟩ example {x y : ℝ} : x ≤ y ∧ ¬y ≤ x ↔ x ≤ y ∧ x ≠ y := sorry theorem aux {x y : ℝ} (h : x ^ 2 + y ^ 2 = 0) : x = 0 := have h' : x ^ 2 = 0 := by sorry pow_eq_zero h' example (x y : ℝ) : x ^ 2 + y ^ 2 = 0 ↔ x = 0 ∧ y = 0 := sorry section example (x : ℝ) : |x + 3| < 5 → -8 < x ∧ x < 2 := by rw [abs_lt] intro h constructor <;> linarith example : 3 ∣ Nat.gcd 6 15 := by rw [Nat.dvd_gcd_iff] constructor <;> norm_num end theorem not_monotone_iff {f : ℝ → ℝ} : ¬Monotone f ↔ ∃ x y, x ≤ y ∧ f x > f y := by rw [Monotone] push_neg rfl example : ¬Monotone fun x : ℝ ↦ -x := by sorry section variable {α : Type*} [PartialOrder α] variable (a b : α) example : a < b ↔ a ≤ b ∧ a ≠ b := by rw [lt_iff_le_not_ge] sorry end section variable {α : Type*} [Preorder α] variable (a b c : α) example : ¬a < a := by rw [lt_iff_le_not_ge] sorry example : a < b → b < c → a < c := by simp only [lt_iff_le_not_ge] sorry end
mathematics_in_lean/MIL/C03_Logic/S01_Implication_and_the_Universal_Quantifier.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S01 #check ∀ x : ℝ, 0 ≤ x → |x| = x #check ∀ x y ε : ℝ, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε theorem my_lemma : ∀ x y ε : ℝ, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε := sorry section variable (a b δ : ℝ) variable (h₀ : 0 < δ) (h₁ : δ ≤ 1) variable (ha : |a| < δ) (hb : |b| < δ) #check my_lemma a b δ #check my_lemma a b δ h₀ h₁ #check my_lemma a b δ h₀ h₁ ha hb end theorem my_lemma2 : ∀ {x y ε : ℝ}, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε := sorry section variable (a b δ : ℝ) variable (h₀ : 0 < δ) (h₁ : δ ≤ 1) variable (ha : |a| < δ) (hb : |b| < δ) #check my_lemma2 h₀ h₁ ha hb end theorem my_lemma3 : ∀ {x y ε : ℝ}, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε := by intro x y ε epos ele1 xlt ylt sorry theorem my_lemma4 : ∀ {x y ε : ℝ}, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε := by intro x y ε epos ele1 xlt ylt calc |x * y| = |x| * |y| := sorry _ ≤ |x| * ε := sorry _ < 1 * ε := sorry _ = ε := sorry def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x section variable (f g : ℝ → ℝ) (a b : ℝ) example (hfa : FnUb f a) (hgb : FnUb g b) : FnUb (fun x ↦ f x + g x) (a + b) := by intro x dsimp apply add_le_add apply hfa apply hgb example (hfa : FnLb f a) (hgb : FnLb g b) : FnLb (fun x ↦ f x + g x) (a + b) := sorry example (nnf : FnLb f 0) (nng : FnLb g 0) : FnLb (fun x ↦ f x * g x) 0 := sorry example (hfa : FnUb f a) (hgb : FnUb g b) (nng : FnLb g 0) (nna : 0 ≤ a) : FnUb (fun x ↦ f x * g x) (a * b) := sorry end section variable {α : Type*} {R : Type*} [AddCommMonoid R] [PartialOrder R] [IsOrderedCancelAddMonoid R] #check add_le_add def FnUb' (f : α → R) (a : R) : Prop := ∀ x, f x ≤ a theorem fnUb_add {f g : α → R} {a b : R} (hfa : FnUb' f a) (hgb : FnUb' g b) : FnUb' (fun x ↦ f x + g x) (a + b) := fun x ↦ add_le_add (hfa x) (hgb x) end example (f : ℝ → ℝ) (h : Monotone f) : ∀ {a b}, a ≤ b → f a ≤ f b := @h section variable (f g : ℝ → ℝ) example (mf : Monotone f) (mg : Monotone g) : Monotone fun x ↦ f x + g x := by intro a b aleb apply add_le_add apply mf aleb apply mg aleb example (mf : Monotone f) (mg : Monotone g) : Monotone fun x ↦ f x + g x := fun a b aleb ↦ add_le_add (mf aleb) (mg aleb) example {c : ℝ} (mf : Monotone f) (nnc : 0 ≤ c) : Monotone fun x ↦ c * f x := sorry example (mf : Monotone f) (mg : Monotone g) : Monotone fun x ↦ f (g x) := sorry def FnEven (f : ℝ → ℝ) : Prop := ∀ x, f x = f (-x) def FnOdd (f : ℝ → ℝ) : Prop := ∀ x, f x = -f (-x) example (ef : FnEven f) (eg : FnEven g) : FnEven fun x ↦ f x + g x := by intro x calc (fun x ↦ f x + g x) x = f x + g x := rfl _ = f (-x) + g (-x) := by rw [ef, eg] example (of : FnOdd f) (og : FnOdd g) : FnEven fun x ↦ f x * g x := by sorry example (ef : FnEven f) (og : FnOdd g) : FnOdd fun x ↦ f x * g x := by sorry example (ef : FnEven f) (og : FnOdd g) : FnEven fun x ↦ f (g x) := by sorry end section variable {α : Type*} (r s t : Set α) example : s ⊆ s := by intro x xs exact xs theorem Subset.refl : s ⊆ s := fun x xs ↦ xs theorem Subset.trans : r ⊆ s → s ⊆ t → r ⊆ t := by sorry end section variable {α : Type*} [PartialOrder α] variable (s : Set α) (a b : α) def SetUb (s : Set α) (a : α) := ∀ x, x ∈ s → x ≤ a example (h : SetUb s a) (h' : a ≤ b) : SetUb s b := sorry end section open Function example (c : ℝ) : Injective fun x ↦ x + c := by intro x₁ x₂ h' exact (add_left_inj c).mp h' example {c : ℝ} (h : c ≠ 0) : Injective fun x ↦ c * x := by sorry variable {α : Type*} {β : Type*} {γ : Type*} variable {g : β → γ} {f : α → β} example (injg : Injective g) (injf : Injective f) : Injective fun x ↦ g (f x) := by sorry end
mathematics_in_lean/MIL/C03_Logic/S03_Negation.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S03 section variable (a b : ℝ) example (h : a < b) : ¬b < a := by intro h' have : a < a := lt_trans h h' apply lt_irrefl a this def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x def FnHasUb (f : ℝ → ℝ) := ∃ a, FnUb f a def FnHasLb (f : ℝ → ℝ) := ∃ a, FnLb f a variable (f : ℝ → ℝ) example (h : ∀ a, ∃ x, f x > a) : ¬FnHasUb f := by intro fnub rcases fnub with ⟨a, fnuba⟩ rcases h a with ⟨x, hx⟩ have : f x ≤ a := fnuba x linarith example (h : ∀ a, ∃ x, f x < a) : ¬FnHasLb f := sorry example : ¬FnHasUb fun x ↦ x := sorry #check (not_le_of_gt : a > b → ¬a ≤ b) #check (not_lt_of_ge : a ≥ b → ¬a < b) #check (lt_of_not_ge : ¬a ≥ b → a < b) #check (le_of_not_gt : ¬a > b → a ≤ b) example (h : Monotone f) (h' : f a < f b) : a < b := by sorry example (h : a ≤ b) (h' : f b < f a) : ¬Monotone f := by sorry example : ¬∀ {f : ℝ → ℝ}, Monotone f → ∀ {a b}, f a ≤ f b → a ≤ b := by intro h let f := fun x : ℝ ↦ (0 : ℝ) have monof : Monotone f := by sorry have h' : f 1 ≤ f 0 := le_refl _ sorry example (x : ℝ) (h : ∀ ε > 0, x < ε) : x ≤ 0 := by sorry end section variable {α : Type*} (P : α → Prop) (Q : Prop) example (h : ¬∃ x, P x) : ∀ x, ¬P x := by sorry example (h : ∀ x, ¬P x) : ¬∃ x, P x := by sorry example (h : ¬∀ x, P x) : ∃ x, ¬P x := by sorry example (h : ∃ x, ¬P x) : ¬∀ x, P x := by sorry example (h : ¬∀ x, P x) : ∃ x, ¬P x := by by_contra h' apply h intro x show P x by_contra h'' exact h' ⟨x, h''⟩ example (h : ¬¬Q) : Q := by sorry example (h : Q) : ¬¬Q := by sorry end section variable (f : ℝ → ℝ) example (h : ¬FnHasUb f) : ∀ a, ∃ x, f x > a := by sorry example (h : ¬∀ a, ∃ x, f x > a) : FnHasUb f := by push_neg at h exact h example (h : ¬FnHasUb f) : ∀ a, ∃ x, f x > a := by dsimp only [FnHasUb, FnUb] at h push_neg at h exact h example (h : ¬Monotone f) : ∃ x y, x ≤ y ∧ f y < f x := by sorry example (h : ¬FnHasUb f) : ∀ a, ∃ x, f x > a := by contrapose! h exact h example (x : ℝ) (h : ∀ ε > 0, x ≤ ε) : x ≤ 0 := by contrapose! h use x / 2 constructor <;> linarith end section variable (a : ℕ) example (h : 0 < 0) : a > 37 := by exfalso apply lt_irrefl 0 h example (h : 0 < 0) : a > 37 := absurd h (lt_irrefl 0) example (h : 0 < 0) : a > 37 := by have h' : ¬0 < 0 := lt_irrefl 0 contradiction end
mathematics_in_lean/MIL/C03_Logic/S02_The_Existential_Quantifier.lean
import MIL.Common import Mathlib.Data.Real.Basic set_option autoImplicit true namespace C03S02 example : ∃ x : ℝ, 2 < x ∧ x < 3 := by use 5 / 2 norm_num example : ∃ x : ℝ, 2 < x ∧ x < 3 := by have h1 : 2 < (5 : ℝ) / 2 := by norm_num have h2 : (5 : ℝ) / 2 < 3 := by norm_num use 5 / 2, h1, h2 example : ∃ x : ℝ, 2 < x ∧ x < 3 := by have h : 2 < (5 : ℝ) / 2 ∧ (5 : ℝ) / 2 < 3 := by norm_num use 5 / 2 example : ∃ x : ℝ, 2 < x ∧ x < 3 := have h : 2 < (5 : ℝ) / 2 ∧ (5 : ℝ) / 2 < 3 := by norm_num ⟨5 / 2, h⟩ example : ∃ x : ℝ, 2 < x ∧ x < 3 := ⟨5 / 2, by norm_num⟩ def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x def FnHasUb (f : ℝ → ℝ) := ∃ a, FnUb f a def FnHasLb (f : ℝ → ℝ) := ∃ a, FnLb f a theorem fnUb_add {f g : ℝ → ℝ} {a b : ℝ} (hfa : FnUb f a) (hgb : FnUb g b) : FnUb (fun x ↦ f x + g x) (a + b) := fun x ↦ add_le_add (hfa x) (hgb x) section variable {f g : ℝ → ℝ} example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := by rcases ubf with ⟨a, ubfa⟩ rcases ubg with ⟨b, ubgb⟩ use a + b apply fnUb_add ubfa ubgb example (lbf : FnHasLb f) (lbg : FnHasLb g) : FnHasLb fun x ↦ f x + g x := by sorry example {c : ℝ} (ubf : FnHasUb f) (h : c ≥ 0) : FnHasUb fun x ↦ c * f x := by sorry example : FnHasUb f → FnHasUb g → FnHasUb fun x ↦ f x + g x := by rintro ⟨a, ubfa⟩ ⟨b, ubgb⟩ exact ⟨a + b, fnUb_add ubfa ubgb⟩ example : FnHasUb f → FnHasUb g → FnHasUb fun x ↦ f x + g x := fun ⟨a, ubfa⟩ ⟨b, ubgb⟩ ↦ ⟨a + b, fnUb_add ubfa ubgb⟩ end example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := by obtain ⟨a, ubfa⟩ := ubf obtain ⟨b, ubgb⟩ := ubg exact ⟨a + b, fnUb_add ubfa ubgb⟩ example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := by cases ubf case intro a ubfa => cases ubg case intro b ubgb => exact ⟨a + b, fnUb_add ubfa ubgb⟩ example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := by cases ubf next a ubfa => cases ubg next b ubgb => exact ⟨a + b, fnUb_add ubfa ubgb⟩ example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := by match ubf, ubg with | ⟨a, ubfa⟩, ⟨b, ubgb⟩ => exact ⟨a + b, fnUb_add ubfa ubgb⟩ example (ubf : FnHasUb f) (ubg : FnHasUb g) : FnHasUb fun x ↦ f x + g x := match ubf, ubg with | ⟨a, ubfa⟩, ⟨b, ubgb⟩ => ⟨a + b, fnUb_add ubfa ubgb⟩ section variable {α : Type*} [CommRing α] def SumOfSquares (x : α) := ∃ a b, x = a ^ 2 + b ^ 2 theorem sumOfSquares_mul {x y : α} (sosx : SumOfSquares x) (sosy : SumOfSquares y) : SumOfSquares (x * y) := by rcases sosx with ⟨a, b, xeq⟩ rcases sosy with ⟨c, d, yeq⟩ rw [xeq, yeq] use a * c - b * d, a * d + b * c ring theorem sumOfSquares_mul' {x y : α} (sosx : SumOfSquares x) (sosy : SumOfSquares y) : SumOfSquares (x * y) := by rcases sosx with ⟨a, b, rfl⟩ rcases sosy with ⟨c, d, rfl⟩ use a * c - b * d, a * d + b * c ring end section variable {a b c : ℕ} example (divab : a ∣ b) (divbc : b ∣ c) : a ∣ c := by rcases divab with ⟨d, beq⟩ rcases divbc with ⟨e, ceq⟩ rw [ceq, beq] use d * e; ring example (divab : a ∣ b) (divac : a ∣ c) : a ∣ b + c := by sorry end section open Function example {c : ℝ} : Surjective fun x ↦ x + c := by intro x use x - c dsimp; ring example {c : ℝ} (h : c ≠ 0) : Surjective fun x ↦ c * x := by sorry example (x y : ℝ) (h : x - y ≠ 0) : (x ^ 2 - y ^ 2) / (x - y) = x + y := by field_simp [h] ring example {f : ℝ → ℝ} (h : Surjective f) : ∃ x, f x ^ 2 = 4 := by rcases h 2 with ⟨x, hx⟩ use x rw [hx] norm_num end section open Function variable {α : Type*} {β : Type*} {γ : Type*} variable {g : β → γ} {f : α → β} example (surjg : Surjective g) (surjf : Surjective f) : Surjective fun x ↦ g (f x) := by sorry end
mathematics_in_lean/MIL/C03_Logic/S06_Sequences_and_Convergence.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S06 def ConvergesTo (s : ℕ → ℝ) (a : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |s n - a| < ε example : (fun x y : ℝ ↦ (x + y) ^ 2) = fun x y : ℝ ↦ x ^ 2 + 2 * x * y + y ^ 2 := by ext ring example (a b : ℝ) : |a| = |a - b + b| := by congr ring example {a : ℝ} (h : 1 < a) : a < a * a := by convert (mul_lt_mul_right _).2 h · rw [one_mul] exact lt_trans zero_lt_one h theorem convergesTo_const (a : ℝ) : ConvergesTo (fun x : ℕ ↦ a) a := by intro ε εpos use 0 intro n nge rw [sub_self, abs_zero] apply εpos theorem convergesTo_add {s t : ℕ → ℝ} {a b : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t b) : ConvergesTo (fun n ↦ s n + t n) (a + b) := by intro ε εpos dsimp -- this line is not needed but cleans up the goal a bit. have ε2pos : 0 < ε / 2 := by linarith rcases cs (ε / 2) ε2pos with ⟨Ns, hs⟩ rcases ct (ε / 2) ε2pos with ⟨Nt, ht⟩ use max Ns Nt sorry theorem convergesTo_mul_const {s : ℕ → ℝ} {a : ℝ} (c : ℝ) (cs : ConvergesTo s a) : ConvergesTo (fun n ↦ c * s n) (c * a) := by by_cases h : c = 0 · convert convergesTo_const 0 · rw [h] ring rw [h] ring have acpos : 0 < |c| := abs_pos.mpr h sorry theorem exists_abs_le_of_convergesTo {s : ℕ → ℝ} {a : ℝ} (cs : ConvergesTo s a) : ∃ N b, ∀ n, N ≤ n → |s n| < b := by rcases cs 1 zero_lt_one with ⟨N, h⟩ use N, |a| + 1 sorry theorem aux {s t : ℕ → ℝ} {a : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t 0) : ConvergesTo (fun n ↦ s n * t n) 0 := by intro ε εpos dsimp rcases exists_abs_le_of_convergesTo cs with ⟨N₀, B, h₀⟩ have Bpos : 0 < B := lt_of_le_of_lt (abs_nonneg _) (h₀ N₀ (le_refl _)) have pos₀ : ε / B > 0 := div_pos εpos Bpos rcases ct _ pos₀ with ⟨N₁, h₁⟩ sorry theorem convergesTo_mul {s t : ℕ → ℝ} {a b : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t b) : ConvergesTo (fun n ↦ s n * t n) (a * b) := by have h₁ : ConvergesTo (fun n ↦ s n * (t n + -b)) 0 := by apply aux cs convert convergesTo_add ct (convergesTo_const (-b)) ring have := convergesTo_add h₁ (convergesTo_mul_const b cs) convert convergesTo_add h₁ (convergesTo_mul_const b cs) using 1 · ext; ring ring theorem convergesTo_unique {s : ℕ → ℝ} {a b : ℝ} (sa : ConvergesTo s a) (sb : ConvergesTo s b) : a = b := by by_contra abne have : |a - b| > 0 := by sorry let ε := |a - b| / 2 have εpos : ε > 0 := by change |a - b| / 2 > 0 linarith rcases sa ε εpos with ⟨Na, hNa⟩ rcases sb ε εpos with ⟨Nb, hNb⟩ let N := max Na Nb have absa : |s N - a| < ε := by sorry have absb : |s N - b| < ε := by sorry have : |a - b| < |a - b| := by sorry exact lt_irrefl _ this section variable {α : Type*} [LinearOrder α] def ConvergesTo' (s : α → ℝ) (a : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |s n - a| < ε end
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S06_Sequences_and_Convergence.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S06 def ConvergesTo (s : ℕ → ℝ) (a : ℝ) := ∀ ε > 0, ∃ N, ∀ n ≥ N, |s n - a| < ε theorem convergesTo_const (a : ℝ) : ConvergesTo (fun x : ℕ ↦ a) a := by intro ε εpos use 0 intro n nge rw [sub_self, abs_zero] apply εpos theorem convergesTo_add {s t : ℕ → ℝ} {a b : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t b) : ConvergesTo (fun n ↦ s n + t n) (a + b) := by intro ε εpos dsimp have ε2pos : 0 < ε / 2 := by linarith rcases cs (ε / 2) ε2pos with ⟨Ns, hs⟩ rcases ct (ε / 2) ε2pos with ⟨Nt, ht⟩ use max Ns Nt intro n hn have ngeNs : n ≥ Ns := le_of_max_le_left hn have ngeNt : n ≥ Nt := le_of_max_le_right hn calc |s n + t n - (a + b)| = |s n - a + (t n - b)| := by congr ring _ ≤ |s n - a| + |t n - b| := (abs_add _ _) _ < ε / 2 + ε / 2 := (add_lt_add (hs n ngeNs) (ht n ngeNt)) _ = ε := by norm_num theorem convergesTo_mul_const {s : ℕ → ℝ} {a : ℝ} (c : ℝ) (cs : ConvergesTo s a) : ConvergesTo (fun n ↦ c * s n) (c * a) := by by_cases h : c = 0 · convert convergesTo_const 0 · rw [h] ring rw [h] ring have acpos : 0 < |c| := abs_pos.mpr h intro ε εpos dsimp have εcpos : 0 < ε / |c| := by apply div_pos εpos acpos rcases cs (ε / |c|) εcpos with ⟨Ns, hs⟩ use Ns intro n ngt calc |c * s n - c * a| = |c| * |s n - a| := by rw [← abs_mul, mul_sub] _ < |c| * (ε / |c|) := (mul_lt_mul_of_pos_left (hs n ngt) acpos) _ = ε := mul_div_cancel₀ _ (ne_of_lt acpos).symm theorem exists_abs_le_of_convergesTo {s : ℕ → ℝ} {a : ℝ} (cs : ConvergesTo s a) : ∃ N b, ∀ n, N ≤ n → |s n| < b := by rcases cs 1 zero_lt_one with ⟨N, h⟩ use N, |a| + 1 intro n ngt calc |s n| = |s n - a + a| := by congr abel _ ≤ |s n - a| + |a| := (abs_add _ _) _ < |a| + 1 := by linarith [h n ngt] theorem aux {s t : ℕ → ℝ} {a : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t 0) : ConvergesTo (fun n ↦ s n * t n) 0 := by intro ε εpos dsimp rcases exists_abs_le_of_convergesTo cs with ⟨N₀, B, h₀⟩ have Bpos : 0 < B := lt_of_le_of_lt (abs_nonneg _) (h₀ N₀ (le_refl _)) have pos₀ : ε / B > 0 := div_pos εpos Bpos rcases ct _ pos₀ with ⟨N₁, h₁⟩ use max N₀ N₁ intro n ngt have ngeN₀ : n ≥ N₀ := le_of_max_le_left ngt have ngeN₁ : n ≥ N₁ := le_of_max_le_right ngt calc |s n * t n - 0| = |s n| * |t n - 0| := by rw [sub_zero, abs_mul, sub_zero] _ < B * (ε / B) := (mul_lt_mul'' (h₀ n ngeN₀) (h₁ n ngeN₁) (abs_nonneg _) (abs_nonneg _)) _ = ε := mul_div_cancel₀ _ (ne_of_lt Bpos).symm theorem convergesTo_mul {s t : ℕ → ℝ} {a b : ℝ} (cs : ConvergesTo s a) (ct : ConvergesTo t b) : ConvergesTo (fun n ↦ s n * t n) (a * b) := by have h₁ : ConvergesTo (fun n ↦ s n * (t n + -b)) 0 := by apply aux cs convert convergesTo_add ct (convergesTo_const (-b)) ring have := convergesTo_add h₁ (convergesTo_mul_const b cs) convert convergesTo_add h₁ (convergesTo_mul_const b cs) using 1 · ext; ring ring theorem convergesTo_unique {s : ℕ → ℝ} {a b : ℝ} (sa : ConvergesTo s a) (sb : ConvergesTo s b) : a = b := by by_contra abne have : |a - b| > 0 := by apply lt_of_le_of_ne · apply abs_nonneg intro h'' apply abne apply eq_of_abs_sub_eq_zero h''.symm let ε := |a - b| / 2 have εpos : ε > 0 := by change |a - b| / 2 > 0 linarith rcases sa ε εpos with ⟨Na, hNa⟩ rcases sb ε εpos with ⟨Nb, hNb⟩ let N := max Na Nb have absa : |s N - a| < ε := by apply hNa apply le_max_left have absb : |s N - b| < ε := by apply hNb apply le_max_right have : |a - b| < |a - b| calc |a - b| = |(-(s N - a)) + (s N - b)| := by congr ring _ ≤ |(-(s N - a))| + |s N - b| := (abs_add _ _) _ = |s N - a| + |s N - b| := by rw [abs_neg] _ < ε + ε := (add_lt_add absa absb) _ = |a - b| := by norm_num [ε] exact lt_irrefl _ this
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S03_Negation.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S03 section variable (a b : ℝ) def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x def FnHasUb (f : ℝ → ℝ) := ∃ a, FnUb f a def FnHasLb (f : ℝ → ℝ) := ∃ a, FnLb f a variable (f : ℝ → ℝ) example (h : ∀ a, ∃ x, f x < a) : ¬FnHasLb f := by rintro ⟨a, ha⟩ rcases h a with ⟨x, hx⟩ have := ha x linarith example : ¬FnHasUb fun x ↦ x := by rintro ⟨a, ha⟩ have : a + 1 ≤ a := ha (a + 1) linarith example (h : Monotone f) (h' : f a < f b) : a < b := by apply lt_of_not_ge intro h'' apply absurd h' apply not_lt_of_ge (h h'') example (h : a ≤ b) (h' : f b < f a) : ¬Monotone f := by intro h'' apply absurd h' apply not_lt_of_ge apply h'' h example : ¬∀ {f : ℝ → ℝ}, Monotone f → ∀ {a b}, f a ≤ f b → a ≤ b := by intro h let f := fun x : ℝ ↦ (0 : ℝ) have monof : Monotone f := by intro a b leab rfl have h' : f 1 ≤ f 0 := le_refl _ have : (1 : ℝ) ≤ 0 := h monof h' linarith example (x : ℝ) (h : ∀ ε > 0, x < ε) : x ≤ 0 := by apply le_of_not_gt intro h' linarith [h _ h'] end section variable {α : Type*} (P : α → Prop) (Q : Prop) example (h : ¬∃ x, P x) : ∀ x, ¬P x := by intro x Px apply h use x example (h : ∀ x, ¬P x) : ¬∃ x, P x := by rintro ⟨x, Px⟩ exact h x Px example (h : ∃ x, ¬P x) : ¬∀ x, P x := by intro h' rcases h with ⟨x, nPx⟩ apply nPx apply h' example (h : ¬¬Q) : Q := by by_contra h' exact h h' example (h : Q) : ¬¬Q := by intro h' exact h' h end section variable (f : ℝ → ℝ) example (h : ¬FnHasUb f) : ∀ a, ∃ x, f x > a := by intro a by_contra h' apply h use a intro x apply le_of_not_gt intro h'' apply h' use x example (h : ¬Monotone f) : ∃ x y, x ≤ y ∧ f y < f x := by rw [Monotone] at h push_neg at h exact h end
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S01_Implication_and_the_Universal_Quantifier.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S01 theorem my_lemma4 : ∀ {x y ε : ℝ}, 0 < ε → ε ≤ 1 → |x| < ε → |y| < ε → |x * y| < ε := by intro x y ε epos ele1 xlt ylt calc |x * y| = |x| * |y| := by apply abs_mul _ ≤ |x| * ε := by apply mul_le_mul; linarith; linarith; apply abs_nonneg; apply abs_nonneg; _ < 1 * ε := by rw [mul_lt_mul_right epos]; linarith _ = ε := by apply one_mul def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x section variable (f g : ℝ → ℝ) (a b : ℝ) example (hfa : FnLb f a) (hgb : FnLb g b) : FnLb (fun x ↦ f x + g x) (a + b) := by intro x apply add_le_add apply hfa apply hgb example (nnf : FnLb f 0) (nng : FnLb g 0) : FnLb (fun x ↦ f x * g x) 0 := by intro x apply mul_nonneg apply nnf apply nng example (hfa : FnUb f a) (hgb : FnUb g b) (nng : FnLb g 0) (nna : 0 ≤ a) : FnUb (fun x ↦ f x * g x) (a * b) := by intro x apply mul_le_mul apply hfa apply hgb apply nng apply nna end section variable (f g : ℝ → ℝ) example {c : ℝ} (mf : Monotone f) (nnc : 0 ≤ c) : Monotone fun x ↦ c * f x := by intro a b aleb apply mul_le_mul_of_nonneg_left _ nnc apply mf aleb example {c : ℝ} (mf : Monotone f) (nnc : 0 ≤ c) : Monotone fun x ↦ c * f x := fun a b aleb ↦ mul_le_mul_of_nonneg_left (mf aleb) nnc example (mf : Monotone f) (mg : Monotone g) : Monotone fun x ↦ f (g x) := by intro a b aleb apply mf apply mg apply aleb example (mf : Monotone f) (mg : Monotone g) : Monotone fun x ↦ f (g x) := fun a b aleb ↦ mf (mg aleb) def FnEven (f : ℝ → ℝ) : Prop := ∀ x, f x = f (-x) def FnOdd (f : ℝ → ℝ) : Prop := ∀ x, f x = -f (-x) example (of : FnOdd f) (og : FnOdd g) : FnEven fun x ↦ f x * g x := by intro x calc (fun x ↦ f x * g x) x = f x * g x := rfl _ = f (-x) * g (-x) := by rw [of, og, neg_mul_neg] example (ef : FnEven f) (og : FnOdd g) : FnOdd fun x ↦ f x * g x := by intro x dsimp rw [ef, og, neg_mul_eq_mul_neg] example (ef : FnEven f) (og : FnOdd g) : FnEven fun x ↦ f (g x) := by intro x dsimp rw [og, ← ef] end section variable {α : Type*} (r s t : Set α) example : r ⊆ s → s ⊆ t → r ⊆ t := by intro rsubs ssubt x xr apply ssubt apply rsubs apply xr theorem Subset.trans : r ⊆ s → s ⊆ t → r ⊆ t := fun rsubs ssubt x xr ↦ ssubt (rsubs xr) end section variable {α : Type*} [PartialOrder α] variable (s : Set α) (a b : α) def SetUb (s : Set α) (a : α) := ∀ x, x ∈ s → x ≤ a example (h : SetUb s a) (h' : a ≤ b) : SetUb s b := by intro x xs apply le_trans (h x xs) h' example (h : SetUb s a) (h' : a ≤ b) : SetUb s b := fun x xs ↦ le_trans (h x xs) h' end section open Function example {c : ℝ} (h : c ≠ 0) : Injective fun x ↦ c * x := by intro x₁ x₂ h' apply (mul_right_inj' h).mp h' variable {α : Type*} {β : Type*} {γ : Type*} variable {g : β → γ} {f : α → β} example (injg : Injective g) (injf : Injective f) : Injective fun x ↦ g (f x) := by intro x₁ x₂ h apply injf apply injg apply h end
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S04_Conjunction_and_Iff.lean
import MIL.Common import Mathlib.Data.Real.Basic import Mathlib.Data.Nat.Prime.Basic namespace C03S04 example {m n : ℕ} (h : m ∣ n ∧ m ≠ n) : m ∣ n ∧ ¬n ∣ m := by rcases h with ⟨h0, h1⟩ constructor · exact h0 intro h2 apply h1 apply Nat.dvd_antisymm h0 h2 example {x y : ℝ} : x ≤ y ∧ ¬y ≤ x ↔ x ≤ y ∧ x ≠ y := by constructor · rintro ⟨h0, h1⟩ constructor · exact h0 intro h2 apply h1 rw [h2] rintro ⟨h0, h1⟩ constructor · exact h0 intro h2 apply h1 apply le_antisymm h0 h2 theorem aux {x y : ℝ} (h : x ^ 2 + y ^ 2 = 0) : x = 0 := have h' : x ^ 2 = 0 := by linarith [pow_two_nonneg x, pow_two_nonneg y] pow_eq_zero h' example (x y : ℝ) : x ^ 2 + y ^ 2 = 0 ↔ x = 0 ∧ y = 0 := by constructor · intro h constructor · exact aux h rw [add_comm] at h exact aux h rintro ⟨rfl, rfl⟩ norm_num theorem not_monotone_iff {f : ℝ → ℝ} : ¬Monotone f ↔ ∃ x y, x ≤ y ∧ f x > f y := by rw [Monotone] push_neg rfl example : ¬Monotone fun x : ℝ ↦ -x := by rw [not_monotone_iff] use 0, 1 norm_num section variable {α : Type*} [PartialOrder α] variable (a b : α) example : a < b ↔ a ≤ b ∧ a ≠ b := by rw [lt_iff_le_not_ge] constructor · rintro ⟨h0, h1⟩ constructor · exact h0 intro h2 apply h1 rw [h2] rintro ⟨h0, h1⟩ constructor · exact h0 intro h2 apply h1 apply le_antisymm h0 h2 end section variable {α : Type*} [Preorder α] variable (a b c : α) example : ¬a < a := by rw [lt_iff_le_not_ge] rintro ⟨h0, h1⟩ exact h1 h0 example : a < b → b < c → a < c := by simp only [lt_iff_le_not_ge] rintro ⟨h0, h1⟩ ⟨h2, h3⟩ constructor · apply le_trans h0 h2 intro h4 apply h1 apply le_trans h2 h4 end
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S05_Disjunction.lean
import MIL.Common import Mathlib.Data.Real.Basic namespace C03S05 section variable {x y : ℝ} namespace MyAbs theorem le_abs_self (x : ℝ) : x ≤ |x| := by rcases le_or_gt 0 x with h | h · rw [abs_of_nonneg h] · rw [abs_of_neg h] linarith theorem neg_le_abs_self (x : ℝ) : -x ≤ |x| := by rcases le_or_gt 0 x with h | h · rw [abs_of_nonneg h] linarith · rw [abs_of_neg h] theorem abs_add (x y : ℝ) : |x + y| ≤ |x| + |y| := by rcases le_or_gt 0 (x + y) with h | h · rw [abs_of_nonneg h] linarith [le_abs_self x, le_abs_self y] · rw [abs_of_neg h] linarith [neg_le_abs_self x, neg_le_abs_self y] theorem lt_abs : x < |y| ↔ x < y ∨ x < -y := by rcases le_or_gt 0 y with h | h · rw [abs_of_nonneg h] constructor · intro h' left exact h' · intro h' rcases h' with h' | h' · exact h' · linarith rw [abs_of_neg h] constructor · intro h' right exact h' · intro h' rcases h' with h' | h' · linarith · exact h' theorem abs_lt : |x| < y ↔ -y < x ∧ x < y := by rcases le_or_gt 0 x with h | h · rw [abs_of_nonneg h] constructor · intro h' constructor · linarith exact h' · intro h' rcases h' with ⟨h1, h2⟩ exact h2 · rw [abs_of_neg h] constructor · intro h' constructor · linarith · linarith · intro h' linarith end MyAbs end example {z : ℝ} (h : ∃ x y, z = x ^ 2 + y ^ 2 ∨ z = x ^ 2 + y ^ 2 + 1) : z ≥ 0 := by rcases h with ⟨x, y, rfl | rfl⟩ <;> linarith [sq_nonneg x, sq_nonneg y] example {x : ℝ} (h : x ^ 2 = 1) : x = 1 ∨ x = -1 := by have h' : x ^ 2 - 1 = 0 := by rw [h, sub_self] have h'' : (x + 1) * (x - 1) = 0 := by rw [← h'] ring rcases eq_zero_or_eq_zero_of_mul_eq_zero h'' with h1 | h1 · right exact eq_neg_iff_add_eq_zero.mpr h1 · left exact eq_of_sub_eq_zero h1 example {x y : ℝ} (h : x ^ 2 = y ^ 2) : x = y ∨ x = -y := by have h' : x ^ 2 - y ^ 2 = 0 := by rw [h, sub_self] have h'' : (x + y) * (x - y) = 0 := by rw [← h'] ring rcases eq_zero_or_eq_zero_of_mul_eq_zero h'' with h1 | h1 · right exact eq_neg_iff_add_eq_zero.mpr h1 · left exact eq_of_sub_eq_zero h1 section variable {R : Type*} [CommRing R] [IsDomain R] variable (x y : R) example (h : x ^ 2 = 1) : x = 1 ∨ x = -1 := by have h' : x ^ 2 - 1 = 0 := by rw [h, sub_self] have h'' : (x + 1) * (x - 1) = 0 := by rw [← h'] ring rcases eq_zero_or_eq_zero_of_mul_eq_zero h'' with h1 | h1 · right exact eq_neg_iff_add_eq_zero.mpr h1 · left exact eq_of_sub_eq_zero h1 example (h : x ^ 2 = y ^ 2) : x = y ∨ x = -y := by have h' : x ^ 2 - y ^ 2 = 0 := by rw [h, sub_self] have h'' : (x + y) * (x - y) = 0 := by rw [← h'] ring rcases eq_zero_or_eq_zero_of_mul_eq_zero h'' with h1 | h1 · right exact eq_neg_iff_add_eq_zero.mpr h1 · left exact eq_of_sub_eq_zero h1 end example (P Q : Prop) : P → Q ↔ ¬P ∨ Q := by constructor · intro h by_cases h' : P · right exact h h' · left exact h' rintro (h | h) · intro h' exact absurd h' h · intro exact h
mathematics_in_lean/MIL/C03_Logic/solutions/Solutions_S02_The_Existential_Quantifier.lean
import MIL.Common import Mathlib.Data.Real.Basic set_option autoImplicit true namespace C03S02 def FnUb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a def FnLb (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, a ≤ f x def FnHasUb (f : ℝ → ℝ) := ∃ a, FnUb f a def FnHasLb (f : ℝ → ℝ) := ∃ a, FnLb f a theorem fnUb_add {f g : ℝ → ℝ} {a b : ℝ} (hfa : FnUb f a) (hgb : FnUb g b) : FnUb (fun x ↦ f x + g x) (a + b) := fun x ↦ add_le_add (hfa x) (hgb x) section variable {f g : ℝ → ℝ} example (lbf : FnHasLb f) (lbg : FnHasLb g) : FnHasLb fun x ↦ f x + g x := by rcases lbf with ⟨a, lbfa⟩ rcases lbg with ⟨b, lbgb⟩ use a + b intro x exact add_le_add (lbfa x) (lbgb x) example {c : ℝ} (ubf : FnHasUb f) (h : c ≥ 0) : FnHasUb fun x ↦ c * f x := by rcases ubf with ⟨a, ubfa⟩ use c * a intro x exact mul_le_mul_of_nonneg_left (ubfa x) h end section variable {a b c : ℕ} example (divab : a ∣ b) (divbc : b ∣ c) : a ∣ c := by rcases divab with ⟨d, rfl⟩ rcases divbc with ⟨e, rfl⟩ use d * e; ring example (divab : a ∣ b) (divac : a ∣ c) : a ∣ b + c := by rcases divab with ⟨d, rfl⟩ rcases divac with ⟨e, rfl⟩ use d + e; ring end section open Function example {c : ℝ} (h : c ≠ 0) : Surjective fun x ↦ c * x := by intro x use x / c dsimp; rw [mul_div_cancel₀ _ h] example {c : ℝ} (h : c ≠ 0) : Surjective fun x ↦ c * x := by intro x use x / c field_simp end section open Function variable {α : Type*} {β : Type*} {γ : Type*} variable {g : β → γ} {f : α → β} example (surjg : Surjective g) (surjf : Surjective f) : Surjective fun x ↦ g (f x) := by intro z rcases surjg z with ⟨y, rfl⟩ rcases surjf y with ⟨x, rfl⟩ use x end
mathematics_in_lean/.github/pull_request_template.md
WARNING: Please do not open a pull request in this repository. This is not the relevant repository to contribute to Mathematics in Lean. This repository is cloned by people who want to study the book. It is automatically created from the source repository which can be found at https://github.com/avigad/mathematics_in_lean_source and where you can open a pull-request.
fp-lean/README.md
# Functional Programming in Lean This repository contains the source code of the book Functional Programming in Lean by David Thrane Christiansen. The original version of the book was released by Microsoft Corporation in 2023 under a Creative Commons Attribution 4.0 International License. The current version has been modified by the author from the original version to account for changes in newer versions of Lean and to use Verso; these changes are copyright 2023-2025 Lean FRO, LLC. A detailed account of the changes can be found in the book's [source code repository](https://github.com/leanprover/fp-lean/). The book's build has been tested with: 1. Lean 4 (see the version in lean-toolchain in examples/) 2. expect (tested with v5.45.4 but any version from the last decade should work) To build the book, change to the "book" directory and run "lake exe fp-lean". After this, "book/out/html-multi" contains a multi-page Web version of the book.
fp-lean/CONTRIBUTING.md
# Contribution Guidelines This project is a single-author text. Generally speaking, I'm not interested in pull requests that modify the text of the online book, because I want to maintain a single coherent authorial voice and style for readers. Additionally, the book is now considered to be complete. It may receive updates to keep it up to date with changes in Lean 4 over time, or to fix errors, but I do not expect to add significant amounts of new content. ## Mistakes If you find a mistake in the book, please open an issue! I'd like this book to be a correct and reliable resource, and to stay that way over time. Please don't open issues for questions about Lean or solving exercises. These discussions are better suited to the [Lean Zulip](https://leanprover.zulipchat.com/). ## Book Infrastructure The infrastructure of the book, like the Lean metaprograms in `Examples.Support` and the Python scripts that extract examples into the text, is currently "good enough". This means that the code is not as elegant as it could be, but it's sufficient to accomplish its task. If you re-use this code in your own book project and find bugs, then I'd like to fix them here too, but I'm not generally interested in stylistic improvements. The code exists to create the rendered HTML version of the book, primarily when running on GitHub Actions and secondarily on Unix-like systems such as WSL, Linux, or macOS. I'm not interested in generalizations to other platforms or contexts because I don't have the time and expertise to maintain them. ## Pull Requests Generally speaking, pull requests are not welcome without prior agreement. Should you wish to contribute a pull request, you'll need to [sign a CLA](https://cla.opensource.microsoft.com/) with Microsoft. Please contact Sarah Smith (smithsarah@microsoft.com) or Gabriel Ebner (gabrielebner@microsoft.com) if you have any questions about this.
fp-lean/examples/ForMIO.lean
-- ANCHOR: LinesOf structure LinesOf where stream : IO.FS.Stream partial def LinesOf.forM (readFrom : LinesOf) (action : String → IO Unit) : IO Unit := do let line ← readFrom.stream.getLine if line == "" then return () action line forM readFrom action instance : ForM IO LinesOf String where forM := LinesOf.forM -- ANCHOR_END: LinesOf -- ANCHOR: main def main (argv : List String) : IO UInt32 := do if argv != [] then IO.eprintln "Unexpected arguments" return 1 forM (LinesOf.mk (← IO.getStdin)) fun line => do if line.any (·.isAlpha) then IO.print line return 0 -- ANCHOR_END: main
fp-lean/examples/lakefile.lean
import Lake open Lake DSL require subverso from git "https://github.com/leanprover/subverso.git"@"main" package examples { -- add configuration options here } @[default_target] lean_lib SimpleHello where srcDir := "simple-hello" roots := #[`Hello] @[default_target] lean_lib HelloName where srcDir := "hello-name" roots := #[`HelloName] @[default_target] lean_lib ExampleSupport @[default_target] lean_lib Examples @[default_target] lean_lib FelineLib { srcDir := "feline/2" roots := #[`FelineLib] } @[default_target] lean_lib EarlyReturn where srcDir := "early-return" @[default_target] lean_exe ForMIO where @[default_target] lean_exe feline { srcDir := "feline/2" root := `Main } @[default_target] lean_exe examples { root := `Main } @[default_target] lean_exe countdown { root := `Examples.HelloWorld } @[default_target] lean_lib DougLib where srcDir := "douglib" roots := #[`DirTree] @[default_target] lean_exe doug { root := `Examples.Doug1 } @[default_target] lean_exe tco { root := `Examples.ProgramsProofs.TCOTest } @[default_target] lean_exe sort { root := `Examples.ProgramsProofs.InstrumentedInsertionSort }
fp-lean/examples/Examples.lean
import Examples.Intro import Examples.HelloWorld import Examples.Cat import Examples.Props import Examples.Classes import Examples.Classes.Even import Examples.Induction import Examples.Monads import Examples.Monads.Class import Examples.Monads.Many import Examples.Monads.Do import Examples.Monads.IO import Examples.Monads.Conveniences import Examples.FunctorApplicativeMonad import Examples.Universes import Examples.FunctorApplicativeMonad.ActualDefs import Examples.MonadTransformers import Examples.MonadTransformers.Defs import Examples.MonadTransformers.Conveniences import Examples.DependentTypes import Examples.DependentTypes.DB import Examples.DependentTypes.IndicesParameters import Examples.DependentTypes.Pitfalls import Examples.ProgramsProofs.TCO import Examples.ProgramsProofs.Arrays import Examples.ProgramsProofs.Fin import Examples.ProgramsProofs.InsertionSort import Examples.ProgramsProofs.Inequalities def whatIsThis : String := "This is a place for examples for the book."
fp-lean/examples/Main.lean
import Examples def main : IO Unit := do IO.println "The code here is for the book"
fp-lean/examples/ExampleSupport.lean
import Lean.Message import Lean.Data.PersistentArray import SubVerso.Examples open SubVerso.Examples Messages open Lean Elab Command syntax withPosition("book" "declaration" "{{{" ws ident ws "}}}" command+ "stop" "book" "declaration") : command open Lean in def mkExampleStx (kind : Name) (name : Ident) (lastHeaderToken : Syntax) (commands : Array Command) : MacroM Command := do if h : commands.size ≥ 1 then `(%example (kind := $(mkIdent <| `FPLean ++ kind)) $(mkIdentFrom lastHeaderToken name.getId true):ident $(commands[0]) $(commands.extract 1 commands.size)* %end) else Macro.throwError "Expected at least one command" open Lean in def wrapExampleStx (kind : Name) (name : Ident) (lastHeaderToken : Syntax) (commands : MacroM Command) : MacroM Command := do let cmd ← commands `(%example +embeddedOnly (kind := $(mkIdent <| `FPLean ++ kind)) $(mkIdentFrom lastHeaderToken name.getId true):ident $cmd %end) macro_rules | `(book declaration {{{ $name:ident }}}%$tok $decls* stop book declaration) => mkExampleStx `decl name tok decls book declaration {{{ foo }}} def twentyFive : Nat := 25 stop book declaration /-- info: twentyFive : Nat -/ #guard_msgs in #check twentyFive book declaration {{{ foo' }}} namespace MyNamespaceIsGreat def twentyFive : Nat := 25 end MyNamespaceIsGreat stop book declaration /-- info: MyNamespaceIsGreat.twentyFive : Nat -/ #guard_msgs in #check MyNamespaceIsGreat.twentyFive syntax withPosition("bookExample" "{{{" ws ident ws "}}}" colGt term:10 colGt "===>" colGt term:10 "end" "bookExample") : command macro_rules | `(bookExample {{{ $name:ident }}}%$tok $x:term ===> $y:term end bookExample) => wrapExampleStx `inputOutput name tok `(#guard_msgs (drop info) in #check (rfl : %ex{$(Lean.mkIdent `in)}{$x} = %ex{$(Lean.mkIdent `out)}{$y})) syntax withPosition("bookExample" ":" term "{{{" ws ident ws "}}}" colGt term:10 colGt "===>" colGt term:10 "end" "bookExample") : command macro_rules | `(bookExample : $type:term {{{ $name:ident }}}%$tok $x:term ===> $y:term end bookExample) => wrapExampleStx `inputOutput name tok `(#guard_msgs (drop info) in #check (rfl : (%ex{$(Lean.mkIdent `in)}{$x} : $type) = %ex{$(Lean.mkIdent `out)}{$y})) bookExample {{{ one }}} 1 ===> 1 end bookExample bookExample {{{ two }}} 1 + 1 ===> 2 end bookExample syntax withPosition("bookExample" "type" "{{{" ws ident ws "}}}" colGt term:1 colGt "===>" colGt term:1 "end" "bookExample") : command macro_rules | `(bookExample type {{{ $name:ident }}}%$tok $x:term ===> $y:term end bookExample) => wrapExampleStx `inputOutput name tok `(noncomputable example : %ex{$(Lean.mkIdent `out)}{$y} := %ex{$(Lean.mkIdent `in)}{$x}) bookExample type {{{ three }}} 2 + 1 ===> Nat end bookExample bookExample type {{{ listT }}} List ===> Type → Type end bookExample syntax withPosition("bookExample" "type" "{{{" ws ident ws "}}}" colGt term:10 colGt "<===" colGt term:0 "end" "bookExample") : command macro_rules | `(bookExample type {{{ $name:ident }}}%$tok $x:term <=== $y:term end bookExample) => wrapExampleStx `inputOutput name tok `(noncomputable example : %ex{$(Lean.mkIdent `out)}{$y} := %ex{$(Lean.mkIdent `in)}{$x}) def nats : (min : Nat) -> (howMany : Nat) -> List Nat | n, Nat.zero => [n] | n, Nat.succ k => n :: nats (Nat.succ n) k syntax withPosition("expect" "error" "{{{" ws ident ws "}}}" colGt command "message" str "end" "expect") : command syntax withPosition("expect" "error" "{{{" ws ident ws "}}}" colGt term "message" str "end" "expect") : command def List.containsBy (xs : List α) (pred : α → Bool) : Bool := xs.find? pred |>.isSome -- Locally modify a state for the scope of an action, undoing the modification later def withSavedState [Monad m] [MonadStateOf σ m] [MonadFinally m] (f : σ → σ) (act : m α) := do let saved ← get set (f saved) try act finally set saved def forgetMessages (st : Lean.Elab.Command.State) : Lean.Elab.Command.State := {st with messages := Lean.MessageLog.empty} def withEmptyMessageLog [Monad m] [MonadStateOf Lean.Elab.Command.State m] [MonadFinally m] : m α → m α := withSavedState forgetMessages elab_rules : command | `(expect error {{{ $name:ident }}} $expr:term message $msg:str end expect) => open Lean.Elab.Command in open Lean in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredError := msg.getString let cmd ← `(command|%show_term (kind := FPLean.forMessage) $name:ident := $expr) elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newErrors := newMessages.filter (·.severity == MessageSeverity.error) let errStrings <- newErrors.mapM (·.data.toString) unless errStrings.containsBy (messagesMatch desiredError) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired error {desiredError} was not found in:\n{desired}" pure <| highlighted.getState (← getEnv) modifyEnv (highlighted.setState · hls) elab_rules : command | `(expect error {{{ $name:ident }}}%$tok $cmd:command message $msg:str end expect) => open Lean.Elab.Command in open Lean in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredError := msg.getString let cmd ← Lean.Elab.liftMacroM <| mkExampleStx `forMessage name tok #[cmd] elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newErrors := newMessages.filter (·.severity == MessageSeverity.error) let errStrings <- newErrors.mapM (·.data.toString) unless errStrings.containsBy (messagesMatch desiredError) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired error {desiredError} was not found in:\n{desired}" pure <| highlighted.getState (← getEnv) modifyEnv (highlighted.setState · hls) expect error {{{ errorEx1 }}} def x : Nat := "I am not a Nat" message "Type mismatch \"I am not a Nat\" has type String but is expected to have type Nat" end expect syntax withPosition("expect" "info" "{{{" ws ident ws "}}}" colGt command "message" str "end" "expect") : command elab_rules : command | `(expect info {{{ $name:ident }}}%$tok $cmd:command message $msg:str end expect) => open Lean.Elab.Command in open Lean in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredInfo := msg.getString let cmd ← Lean.Elab.liftMacroM <| mkExampleStx `forMessage name tok #[cmd] elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newInfos := newMessages.filter fun m => m.severity == MessageSeverity.information let errStrings <- newInfos.mapM fun err => err.data.toString unless errStrings.containsBy (messagesMatch desiredInfo) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired info {repr desiredInfo} was not found in\n{desired}" pure <| highlighted.getState afterState.env modifyEnv (highlighted.setState · hls) expect info {{{ infoEx1 }}} #check 1 + 2 message "1 + 2 : Nat" end expect syntax withPosition("expect" "warning" "{{{" ws ident ws "}}}" colGt command "message" str "end" "expect") : command syntax withPosition("expect" "warning" "{{{" ws ident ws "}}}" colGt term "message" str "end" "expect") : command elab_rules : command | `(expect warning {{{ $name }}}%$tok $cmd:command message $msg:str end expect) => open Lean.Elab Command in open Lean in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredWarning := msg.getString let cmd ← liftMacroM <| mkExampleStx `forMessage name tok #[cmd] elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newWarnings := newMessages.filter (·.severity == MessageSeverity.warning) let errStrings <- newWarnings.mapM (·.data.toString) unless errStrings.containsBy (messagesMatch desiredWarning) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired warning {desiredWarning} was not found in\n{desired}" pure <| highlighted.getState afterState.env modifyEnv (highlighted.setState · hls) elab_rules : command | `(expect warning {{{ $name }}} $expr:term message $msg:str end expect) => open Lean.Elab.Command in open Lean in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredWarning := msg.getString let cmd ← `(%show_term (kind := FPLean.forMessage) $name:ident := $expr) elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newWarnings := newMessages.filter (·.severity == MessageSeverity.warning) let errStrings <- newWarnings.mapM (·.data.toString) unless errStrings.containsBy (messagesMatch desiredWarning) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired warning {desiredWarning} was not found in\n{desired}" pure <| highlighted.getState afterState.env modifyEnv (highlighted.setState · hls) /-- A version of `#guard_msgs` that leaves the messages in the log for extraction. The passthrough parts of the spec are ignored. -/ syntax (name := checkMsgsCmd) (docComment)? "#check_msgs" (ppSpace guardMsgsSpec)? " in" ppLine command : command /-- Gives a string representation of a message without source position information. Ensures the message ends with a '\n'. -/ private def messageToStringWithoutPos (msg : Message) : BaseIO String := do let mut str ← msg.data.toString unless msg.caption == "" do str := msg.caption ++ ":\n" ++ str if !("\n".isPrefixOf str) then str := " " ++ str match msg.severity with | MessageSeverity.information => str := "info:" ++ str | MessageSeverity.warning => str := "warning:" ++ str | MessageSeverity.error => str := "error:" ++ str if str.isEmpty || str.back != '\n' then str := str ++ "\n" return str open Tactic.GuardMsgs in @[command_elab checkMsgsCmd] def elabCheckMsgs : CommandElab | `(command| $[$dc?:docComment]? #check_msgs%$tk $(spec?)? in $cmd) => do let expected : String := (← dc?.mapM (getDocStringText ·)).getD "" |>.trim |> removeTrailingWhitespaceMarker let {whitespace, ordering, filterFn, reportPositions := _} ← parseGuardMsgsSpec spec? let initMsgs ← modifyGet fun st => (st.messages, { st with messages := {} }) -- do not forward snapshot as we don't want messages assigned to it to leak outside withReader ({ · with snap? := none }) do -- The `#guard_msgs` command is special-cased in `elabCommandTopLevel` to ensure linters only run once. elabCommandTopLevel cmd -- collect sync and async messages let msgs := (← get).messages ++ (← get).snapshotTasks.foldl (· ++ ·.get.getAll.foldl (· ++ ·.diagnostics.msgLog) {}) {} -- clear async messages as we don't want them to leak outside modify ({ · with snapshotTasks := #[] }) let mut toCheck : MessageLog := .empty let mut toPassthrough : MessageLog := .empty for msg in msgs.toList do match filterFn msg with | .check => toCheck := toCheck.add msg | .drop => pure () | .pass => toPassthrough := toPassthrough.add msg let strings ← toCheck.toList.mapM (messageToStringWithoutPos ·) let strings := ordering.apply strings let res := "---\n".intercalate strings |>.trim if messagesMatch (whitespace.apply expected) (whitespace.apply res) then -- Passed. Put messages back on the log, downgrading errors to warnings while recording their original status modify fun st => { st with messages := initMsgs ++ SubVerso.Highlighting.Messages.errorsToWarnings msgs } else -- Failed. Put all the messages back on the message log and add an error modify fun st => { st with messages := initMsgs ++ msgs } let feedback := let diff := Diff.diff (expected.splitToList (· == '\n')).toArray (res.splitToList (· == '\n')).toArray Diff.linesToString diff logErrorAt tk m!"❌️ Docstring on `#check_msgs` does not match generated message:\n\n{feedback}" pushInfoLeaf (.ofCustomInfo { stx := ← getRef, value := Dynamic.mk (GuardMsgFailure.mk res) }) | _ => throwUnsupportedSyntax attribute [command_code_action checkMsgsCmd] Tactic.GuardMsgs.guardMsgsCodeAction /-- info: 5 -/ #check_msgs in #eval 5 syntax withPosition("expect" "eval" "info" "{{{" ws ident ws "}}}" colGt term "message" str "end" "expect") : command elab_rules : command | `(expect eval info {{{ $name:ident }}}%$tok $expr:term message $msg:str end expect) => open Lean Elab Command in open Lean.Meta in do let hls ← withEmptyMessageLog do let desiredInfo := msg.getString let cmd ← liftMacroM <| wrapExampleStx `evalInfo name tok `(#eval %ex{$(mkIdent `in)}{$expr}) elabCommand cmd let afterState <- get let newMessages := afterState.messages.toList let newInfos := newMessages.filter fun m => m.severity == MessageSeverity.information let errStrings <- newInfos.mapM fun err => err.data.toString unless errStrings.containsBy (messagesMatch desiredInfo) do let desired := errStrings.map (· |> repr |>.pretty |>.replace "\\n" "\n") let desired := Std.Format.joinSep desired .line throwErrorAt msg "The desired info {repr desiredInfo} was not found in\n{desired}" pure <| highlighted.getState afterState.env modifyEnv (highlighted.setState · hls) expect eval info {{{ foo'' }}} IO.println "hej" >>= fun _ => IO.println "med dig" message "hej med dig" end expect syntax withPosition("evaluation" "steps" "{{{" ws ident ws "}}}" sepBy1(colGe term, "===>") "end" "evaluation" "steps"): command elab_rules : command | `(evaluation steps {{{ $name }}}%$tok $[ $exprs ]===>* end evaluation steps) => open Lean Elab Command Term in open Lean.Meta in do let mut current : Option Syntax := none for item in exprs do if let some v := current then runTermElabM fun _ => withDeclName name.raw.getId do let x <- elabTerm item.raw none let y <- elabTerm v none synthesizeSyntheticMVarsNoPostponing unless (← isDefEq x y) do throwError "Example reduction step {y} ===> {x} is incorrect" current := some item.raw let named : Array Term ← exprs.mapIdxM fun i e => `(%ex{$(mkIdent s!"step{i}".toName)}{$e}) let cmd ← liftMacroM <| wrapExampleStx `evalSteps name tok `(noncomputable example := [$named,*]) elabCommand cmd evaluation steps {{{ fooSteps }}} 1 + 1 + 2 ===> 2 + 2 ===> 4 end evaluation steps syntax withPosition("evaluation" "steps" "-" noWs &"check" "{{{" ws ident ws "}}}" sepBy1(colGe term, "===>") "end" "evaluation" "steps"): command elab_rules : command | `(evaluation steps -check {{{ $name }}}%$tok $[ $exprs ]===>* end evaluation steps) => open Lean Elab Command Term in open Lean.Meta in do let mut current : Option Syntax := none for item in exprs do if let some v := current then runTermElabM fun _ => withDeclName name.raw.getId do let _ <- elabTerm item.raw none let _ <- elabTerm v none synthesizeSyntheticMVarsNoPostponing current := some item.raw let named : Array Term ← exprs.mapIdxM fun i e => `(%ex{$(mkIdent s!"step{i}".toName)}{$e}) let cmd ← liftMacroM <| wrapExampleStx `evalSteps name tok `(noncomputable example := [$named,*]) elabCommand cmd syntax withPosition("evaluation" "steps" ":" term "{{{" ws ident ws "}}}" sepBy1(colGe term, "===>") "end" "evaluation" "steps"): command elab_rules : command | `(evaluation steps : $ty {{{ $name }}}%$tok $[ $exprs ]===>* end evaluation steps) => open Lean Elab Command Term in open Lean.Meta in do let cmd ← runTermElabM fun _ => do let expected <- withDeclName name.raw.getId <| elabType ty <* synthesizeSyntheticMVarsNoPostponing let mut current : Option Syntax := none for item in exprs do if let some v := current then withDeclName name.raw.getId do let x <- elabTerm item.raw (some expected) let y <- elabTerm v (some expected) synthesizeSyntheticMVarsNoPostponing unless (← isDefEq x y) do throwError "Example reduction step {y} ===> {x} is incorrect\n----------\n\t {(← whnf y)}\n ≠\n\t {(← whnf x)}\n----------\n\t {(← reduceAll y)}\n ≠\n\t {(← reduceAll x)}" current := some item.raw let named : Array Term ← exprs.mapIdxM fun i e => `((%ex{$(mkIdent s!"step{i}".toName)}{$e} : $ty)) liftMacroM <| wrapExampleStx `evalSteps name tok `(noncomputable example := [$named,*]) elabCommand cmd evaluation steps : IO Unit {{{ thingy }}} let x := 5; IO.println s!"{x}" ===> IO.println "5" end evaluation steps declare_syntax_cat eqSteps syntax term : eqSteps syntax term "={" docComment "}=" eqSteps : eqSteps syntax term "={" docComment term "}=" eqSteps : eqSteps syntax withPosition("equational" "steps" "{{{" ws ident ws "}}}" (colGe eqSteps) "stop" "equational" "steps") : command syntax withPosition("equational" "steps" ":" term "{{{" ws ident ws "}}}" (colGe eqSteps) "stop" "equational" "steps") : command open Lean Elab Parser Command in inductive Steps where | done : Term → Steps | cons : Term → TSyntax ``docComment → Option Term → Steps → Steps deriving Inhabited open Lean Elab Parser Command in def Steps.forM [Monad m] (todo : Steps) (forStep : Term × TSyntax ``docComment × Option Term × Term → m Unit) : m Unit := match todo with | .done _ => pure () | .cons e1 txt why (.done e2) => forStep (e1, txt, why, e2) | .cons e1 txt why (.cons e2 txt2 why2 more) => do forStep (e1, txt, why, e2) forM (.cons e2 txt2 why2 more) forStep open Lean Elab Parser Command in instance : ForM m Steps (Term × TSyntax ``docComment × Option Term × Term) where forM := Steps.forM open Lean Elab Parser Command in instance : ForIn m Steps (Term × TSyntax ``docComment × Option Term × Term) where forIn := ForM.forIn partial def getSteps [Monad m] [MonadError m] [MonadQuotation m] : Lean.Syntax → m Steps | `(eqSteps|$t:term) => pure (.done t) | `(eqSteps|$t:term ={ $c }= $more:eqSteps) => do return (.cons t c none (← getSteps more)) | `(eqSteps|$t:term ={ $c $why:term }= $more:eqSteps) => do return (.cons t c (some why) (← getSteps more)) | other => throwError "Invalid equational steps {other}" open Internals in open Lean Elab Command in def Steps.asCmd [Monad m] [MonadQuotation m] (ty? : Option Term) (ss : Steps) : m Command := do let tms ← go 0 ss if let some ty := ty? then `(noncomputable example : List $ty := [$tms,*]) else `(noncomputable example := [$tms,*]) where go (n : Nat) : Steps → m (Array Term) | .cons e txt _ ss => do let more ← go (n + 1) ss let me ← `(let _ : Unit := %ex{$(mkIdent s!"why{n}".toName)}{$txt:docComment ()}; %ex{$(mkIdent s!"step{n}".toName)}{$e}) pure <| more.push me | .done e => do let me ← `(%ex{$(mkIdent s!"step{n}".toName)}{$e}) pure #[me] open Lean in def elabEquationalSteps (name : TSyntax `ident) (tok : Syntax) (stepStx : TSyntax `eqSteps) (ty : Option (TSyntax `term)) := open Lean Elab Command Term in open Lean.Meta in do let cmd ← runTermElabM fun _ => do let expected ← match ty with | none => pure none | some t => some <$> withDeclName name.raw.getId (elabType t) let exprs ← getSteps stepStx if let .done e := exprs then withDeclName name.raw.getId do let _ ← elabTerm e none for (e1, txt, why, e2) in exprs do withDeclName name.raw.getId do let x ← elabTermEnsuringType e1 expected let y ← elabTermEnsuringType e2 expected synthesizeSyntheticMVarsNoPostponing match why with | none => unless (← isDefEq x y) do throwErrorAt txt "Example equational step {y} ===> {x} is incorrect\n----------\n\t {(← whnf y)}\n ≠\n\t {(← whnf x)}\n----------\n\t {(← reduceAll y)}\n ≠\n\t {(← reduceAll x)}" | some p => let e1' : TSyntax `term := ⟨e1⟩ let e2' : TSyntax `term := ⟨e2⟩ let stepTypeSyntax ← match ty with | none => `($e1' = $e2') | some t => `(($e1' : $t) = ($e2' : $t)) let eq ← elabTermEnsuringType stepTypeSyntax (some (mkSort Level.zero)) let _ ← elabTermEnsuringType p (some eq) synthesizeSyntheticMVarsNoPostponing liftMacroM <| wrapExampleStx `eqSteps name tok (exprs.asCmd ty) elabCommand cmd elab_rules : command | `(equational steps {{{ $name }}}%$tok $stepStx:eqSteps stop equational steps) => elabEquationalSteps name tok stepStx none | `(equational steps : $ty {{{ $name }}}%$tok $stepStx:eqSteps stop equational steps) => elabEquationalSteps name tok stepStx (some ty) equational steps {{{ fooStepsEqs }}} 1 + 1 ={ /-- Compute forwards -/ by rfl }= 2 ={ /-- Compute backwards -/ }= 0 + 2 stop equational steps def zipSameLength : List α → List β → Option (List (α × β)) | [], [] => some [] | x :: xs, y :: ys => do pure ((x, y) :: (← zipSameLength xs ys)) | _, _ => none def Lean.Name.last : Lean.Name -> Option String | Lean.Name.str _ s => some s | _ => none syntax "similar " "datatypes" ident ident : command elab_rules : command | `(similar datatypes $C1:ident $C2:ident) => open Lean.Elab.Command in open Lean.Environment in open Lean in do let e := (<- get).env let t1 ← runTermElabM fun _ => realizeGlobalConstNoOverloadWithInfo C1 let t2 ← runTermElabM fun _ => realizeGlobalConstNoOverloadWithInfo C2 let i1 <- match (e.find? t1).get! with | ConstantInfo.inductInfo i => pure i | _ => throwErrorAt C1 "Not an inductive type: {t1}" let i2 <- match (e.find? t2).get! with | ConstantInfo.inductInfo i => pure i | _ => throwErrorAt C2 "Not an inductive type: {t2}" if i1.numParams != i2.numParams then throwError "Param count mismatch" if i1.numIndices != i2.numIndices then throwError "Index count mismatch" if i1.isRec != i1.isRec then throwError "Recursiveness mismatch" let ctors <- match zipSameLength i1.ctors i2.ctors with | some v => pure v | none => throwError "Different number of constructors" for (c1, c2) in ctors do let (n1, n2) := (c1.last.get!, c2.last.get!) if n1 != n2 then throwError "Constructor name mismatch: {n1} vs {n2}" let ctor1 <- match (e.find? c1).get! with | ConstantInfo.ctorInfo i => pure i | _ => throwError "Not a constructor {c1}" let ctor2 <- match (e.find? c2).get! with | ConstantInfo.ctorInfo i => pure i | _ => throwError "Not a constructor {c2}" if ctor1.numFields != ctor2.numFields then throwError "Constructor field count mismatch for {n1}" syntax withPosition("discarding" (colGe command)* "stop " "discarding") : command open Lean Elab Command in elab_rules : command | `(discarding $cmds* stop discarding) => do let hls ← withoutModifyingEnv do for c in cmds do elabCommand c pure <| highlighted.getState (← getEnv) modifyEnv (highlighted.setState · hls) namespace Foo inductive List (α : Type) : Type where | nil : List α | cons : α -> List α -> List α end Foo similar datatypes List Foo.List
fp-lean/examples/second-lake/greeting/Aux.lean
example : Nat → Float := Nat.toFloat open Nat (toFloat) example : Nat → Float := toFloat
fp-lean/examples/second-lake/greeting/lakefile.lean
import Lake open Lake DSL require subverso from git "https://github.com/leanprover/subverso.git"@"main" package greeting { -- add package configuration options here } @[default_target] lean_lib Greeting @[default_target] lean_lib Aux @[default_target] lean_exe greeting { root := `Main }