path
stringlengths
11
71
content
stringlengths
75
124k
Tactic.lean
import Mathlib.Tactic.Abel import Mathlib.Tactic.AdaptationNote import Mathlib.Tactic.ApplyAt import Mathlib.Tactic.ApplyCongr import Mathlib.Tactic.ApplyFun import Mathlib.Tactic.ApplyWith import Mathlib.Tactic.ArithMult import Mathlib.Tactic.ArithMult.Init import Mathlib.Tactic.Attr.Core import Mathlib.Tactic.Attr.Register import Mathlib.Tactic.Basic import Mathlib.Tactic.Bound import Mathlib.Tactic.Bound.Attribute import Mathlib.Tactic.Bound.Init import Mathlib.Tactic.ByContra import Mathlib.Tactic.CC import Mathlib.Tactic.CC.Addition import Mathlib.Tactic.CC.Datatypes import Mathlib.Tactic.CC.Lemmas import Mathlib.Tactic.CancelDenoms import Mathlib.Tactic.CancelDenoms.Core import Mathlib.Tactic.Cases import Mathlib.Tactic.CasesM import Mathlib.Tactic.CategoryTheory.BicategoricalComp import Mathlib.Tactic.CategoryTheory.BicategoryCoherence import Mathlib.Tactic.CategoryTheory.Coherence import Mathlib.Tactic.CategoryTheory.Elementwise import Mathlib.Tactic.CategoryTheory.Monoidal import Mathlib.Tactic.CategoryTheory.MonoidalComp import Mathlib.Tactic.CategoryTheory.Reassoc import Mathlib.Tactic.CategoryTheory.Slice import Mathlib.Tactic.Change import Mathlib.Tactic.Check import Mathlib.Tactic.Choose import Mathlib.Tactic.Clean import Mathlib.Tactic.ClearExcept import Mathlib.Tactic.ClearExclamation import Mathlib.Tactic.Clear_ import Mathlib.Tactic.Coe import Mathlib.Tactic.Common import Mathlib.Tactic.ComputeDegree import Mathlib.Tactic.CongrExclamation import Mathlib.Tactic.CongrM import Mathlib.Tactic.Constructor import Mathlib.Tactic.Continuity import Mathlib.Tactic.Continuity.Init import Mathlib.Tactic.ContinuousFunctionalCalculus import Mathlib.Tactic.Contrapose import Mathlib.Tactic.Conv import Mathlib.Tactic.Convert import Mathlib.Tactic.Core import Mathlib.Tactic.DefEqTransformations import Mathlib.Tactic.DeprecateMe import Mathlib.Tactic.DeriveFintype import Mathlib.Tactic.DeriveToExpr import Mathlib.Tactic.DeriveTraversable import Mathlib.Tactic.Eqns import Mathlib.Tactic.Eval import Mathlib.Tactic.ExistsI import Mathlib.Tactic.Explode import Mathlib.Tactic.Explode.Datatypes import Mathlib.Tactic.Explode.Pretty import Mathlib.Tactic.ExtendDoc import Mathlib.Tactic.ExtractGoal import Mathlib.Tactic.ExtractLets import Mathlib.Tactic.FBinop import Mathlib.Tactic.FailIfNoProgress import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.FinCases import Mathlib.Tactic.Find import Mathlib.Tactic.FunProp import Mathlib.Tactic.FunProp.AEMeasurable import Mathlib.Tactic.FunProp.Attr import Mathlib.Tactic.FunProp.ContDiff import Mathlib.Tactic.FunProp.Core import Mathlib.Tactic.FunProp.Decl import Mathlib.Tactic.FunProp.Differentiable import Mathlib.Tactic.FunProp.Elab import Mathlib.Tactic.FunProp.FunctionData import Mathlib.Tactic.FunProp.Mor import Mathlib.Tactic.FunProp.RefinedDiscrTree import Mathlib.Tactic.FunProp.StateList import Mathlib.Tactic.FunProp.Theorems import Mathlib.Tactic.FunProp.ToBatteries import Mathlib.Tactic.FunProp.Types import Mathlib.Tactic.GCongr import Mathlib.Tactic.GCongr.Core import Mathlib.Tactic.GCongr.ForwardAttr import Mathlib.Tactic.Generalize import Mathlib.Tactic.GeneralizeProofs import Mathlib.Tactic.Group import Mathlib.Tactic.GuardGoalNums import Mathlib.Tactic.GuardHypNums import Mathlib.Tactic.Have import Mathlib.Tactic.HaveI import Mathlib.Tactic.HelpCmd import Mathlib.Tactic.HigherOrder import Mathlib.Tactic.Hint import Mathlib.Tactic.ITauto import Mathlib.Tactic.InferParam import Mathlib.Tactic.Inhabit import Mathlib.Tactic.IntervalCases import Mathlib.Tactic.IrreducibleDef import Mathlib.Tactic.Lemma import Mathlib.Tactic.Lift import Mathlib.Tactic.LiftLets import Mathlib.Tactic.Linarith import Mathlib.Tactic.Linarith.Datatypes import Mathlib.Tactic.Linarith.Frontend import Mathlib.Tactic.Linarith.Lemmas import Mathlib.Tactic.Linarith.Oracle.FourierMotzkin import Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm import Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm.Datatypes import Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm.Gauss import Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm.PositiveVector import Mathlib.Tactic.Linarith.Oracle.SimplexAlgorithm.SimplexAlgorithm import Mathlib.Tactic.Linarith.Parsing import Mathlib.Tactic.Linarith.Preprocessing import Mathlib.Tactic.Linarith.Verification import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Linter import Mathlib.Tactic.Linter.GlobalAttributeIn import Mathlib.Tactic.Linter.HashCommandLinter import Mathlib.Tactic.Linter.HaveLetLinter import Mathlib.Tactic.Linter.Lint import Mathlib.Tactic.Linter.MinImports import Mathlib.Tactic.Linter.OldObtain import Mathlib.Tactic.Linter.RefineLinter import Mathlib.Tactic.Linter.Style import Mathlib.Tactic.Linter.TextBased import Mathlib.Tactic.Linter.UnusedTactic import Mathlib.Tactic.Measurability import Mathlib.Tactic.Measurability.Init import Mathlib.Tactic.MinImports import Mathlib.Tactic.MkIffOfInductiveProp import Mathlib.Tactic.ModCases import Mathlib.Tactic.Monotonicity import Mathlib.Tactic.Monotonicity.Attr import Mathlib.Tactic.Monotonicity.Basic import Mathlib.Tactic.Monotonicity.Lemmas import Mathlib.Tactic.MoveAdd import Mathlib.Tactic.NoncommRing import Mathlib.Tactic.Nontriviality import Mathlib.Tactic.Nontriviality.Core import Mathlib.Tactic.NormNum import Mathlib.Tactic.NormNum.Basic import Mathlib.Tactic.NormNum.BigOperators import Mathlib.Tactic.NormNum.Core import Mathlib.Tactic.NormNum.DivMod import Mathlib.Tactic.NormNum.Eq import Mathlib.Tactic.NormNum.GCD import Mathlib.Tactic.NormNum.Ineq import Mathlib.Tactic.NormNum.Inv import Mathlib.Tactic.NormNum.IsCoprime import Mathlib.Tactic.NormNum.LegendreSymbol import Mathlib.Tactic.NormNum.NatFib import Mathlib.Tactic.NormNum.NatSqrt import Mathlib.Tactic.NormNum.OfScientific import Mathlib.Tactic.NormNum.Pow import Mathlib.Tactic.NormNum.Prime import Mathlib.Tactic.NormNum.Result import Mathlib.Tactic.NthRewrite import Mathlib.Tactic.Observe import Mathlib.Tactic.PPWithUniv import Mathlib.Tactic.Peel import Mathlib.Tactic.Polyrith import Mathlib.Tactic.Positivity import Mathlib.Tactic.Positivity.Basic import Mathlib.Tactic.Positivity.Core import Mathlib.Tactic.Positivity.Finset import Mathlib.Tactic.ProdAssoc import Mathlib.Tactic.ProjectionNotation import Mathlib.Tactic.Propose import Mathlib.Tactic.ProxyType import Mathlib.Tactic.PushNeg import Mathlib.Tactic.Qify import Mathlib.Tactic.RSuffices import Mathlib.Tactic.Recall import Mathlib.Tactic.Recover import Mathlib.Tactic.ReduceModChar import Mathlib.Tactic.ReduceModChar.Ext import Mathlib.Tactic.Relation.Rfl import Mathlib.Tactic.Relation.Symm import Mathlib.Tactic.Relation.Trans import Mathlib.Tactic.Rename import Mathlib.Tactic.RenameBVar import Mathlib.Tactic.Replace import Mathlib.Tactic.RewriteSearch import Mathlib.Tactic.Rify import Mathlib.Tactic.Ring import Mathlib.Tactic.Ring.Basic import Mathlib.Tactic.Ring.PNat import Mathlib.Tactic.Ring.RingNF import Mathlib.Tactic.Sat.FromLRAT import Mathlib.Tactic.Says import Mathlib.Tactic.ScopedNS import Mathlib.Tactic.Set import Mathlib.Tactic.SetLike import Mathlib.Tactic.SimpIntro import Mathlib.Tactic.SimpRw import Mathlib.Tactic.Simps.Basic import Mathlib.Tactic.Simps.NotationClass import Mathlib.Tactic.SlimCheck import Mathlib.Tactic.SplitIfs import Mathlib.Tactic.Spread import Mathlib.Tactic.Subsingleton import Mathlib.Tactic.Substs import Mathlib.Tactic.SuccessIfFailWithMsg import Mathlib.Tactic.SudoSetOption import Mathlib.Tactic.SuppressCompilation import Mathlib.Tactic.SwapVar import Mathlib.Tactic.TFAE import Mathlib.Tactic.Tauto import Mathlib.Tactic.TermCongr import Mathlib.Tactic.ToAdditive import Mathlib.Tactic.ToAdditive.Frontend import Mathlib.Tactic.ToExpr import Mathlib.Tactic.ToLevel import Mathlib.Tactic.Trace import Mathlib.Tactic.TryThis import Mathlib.Tactic.TypeCheck import Mathlib.Tactic.TypeStar import Mathlib.Tactic.UnsetOption import Mathlib.Tactic.Use import Mathlib.Tactic.Variable import Mathlib.Tactic.WLOG import Mathlib.Tactic.Widget.Calc import Mathlib.Tactic.Widget.CommDiag import Mathlib.Tactic.Widget.CongrM import Mathlib.Tactic.Widget.Conv import Mathlib.Tactic.Widget.GCongr import Mathlib.Tactic.Widget.InteractiveUnfold import Mathlib.Tactic.Widget.SelectInsertParamsClass import Mathlib.Tactic.Widget.SelectPanelUtils import Mathlib.Tactic.Widget.StringDiagram import Mathlib.Tactic.Zify
Algebra\AddTorsor.lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov -/ import Mathlib.Data.Set.Pointwise.SMul /-! # Torsors of additive group actions This file defines torsors of additive group actions. ## Notations The group elements are referred to as acting on points. This file defines the notation `+ᵥ` for adding a group element to a point and `-ᵥ` for subtracting two points to produce a group element. ## Implementation notes Affine spaces are the motivating example of torsors of additive group actions. It may be appropriate to refactor in terms of the general definition of group actions, via `to_additive`, when there is a use for multiplicative torsors (currently mathlib only develops the theory of group actions for multiplicative group actions). ## Notations * `v +ᵥ p` is a notation for `VAdd.vadd`, the left action of an additive monoid; * `p₁ -ᵥ p₂` is a notation for `VSub.vsub`, difference between two points in an additive torsor as an element of the corresponding additive group; ## References * https://en.wikipedia.org/wiki/Principal_homogeneous_space * https://en.wikipedia.org/wiki/Affine_space -/ /-- An `AddTorsor G P` gives a structure to the nonempty type `P`, acted on by an `AddGroup G` with a transitive and free action given by the `+ᵥ` operation and a corresponding subtraction given by the `-ᵥ` operation. In the case of a vector space, it is an affine space. -/ class AddTorsor (G : outParam Type*) (P : Type*) [AddGroup G] extends AddAction G P, VSub G P where [nonempty : Nonempty P] /-- Torsor subtraction and addition with the same element cancels out. -/ vsub_vadd' : ∀ p₁ p₂ : P, (p₁ -ᵥ p₂ : G) +ᵥ p₂ = p₁ /-- Torsor addition and subtraction with the same element cancels out. -/ vadd_vsub' : ∀ (g : G) (p : P), g +ᵥ p -ᵥ p = g -- Porting note(#12096): removed `nolint instance_priority`; lint not ported yet attribute [instance 100] AddTorsor.nonempty -- Porting note(#12094): removed nolint; dangerous_instance linter not ported yet --attribute [nolint dangerous_instance] AddTorsor.toVSub /-- An `AddGroup G` is a torsor for itself. -/ -- Porting note(#12096): linter not ported yet --@[nolint instance_priority] instance addGroupIsAddTorsor (G : Type*) [AddGroup G] : AddTorsor G G where vsub := Sub.sub vsub_vadd' := sub_add_cancel vadd_vsub' := add_sub_cancel_right /-- Simplify subtraction for a torsor for an `AddGroup G` over itself. -/ @[simp] theorem vsub_eq_sub {G : Type*} [AddGroup G] (g₁ g₂ : G) : g₁ -ᵥ g₂ = g₁ - g₂ := rfl section General variable {G : Type*} {P : Type*} [AddGroup G] [T : AddTorsor G P] /-- Adding the result of subtracting from another point produces that point. -/ @[simp] theorem vsub_vadd (p₁ p₂ : P) : p₁ -ᵥ p₂ +ᵥ p₂ = p₁ := AddTorsor.vsub_vadd' p₁ p₂ /-- Adding a group element then subtracting the original point produces that group element. -/ @[simp] theorem vadd_vsub (g : G) (p : P) : g +ᵥ p -ᵥ p = g := AddTorsor.vadd_vsub' g p /-- If the same point added to two group elements produces equal results, those group elements are equal. -/ theorem vadd_right_cancel {g₁ g₂ : G} (p : P) (h : g₁ +ᵥ p = g₂ +ᵥ p) : g₁ = g₂ := by -- Porting note: vadd_vsub g₁ → vadd_vsub g₁ p rw [← vadd_vsub g₁ p, h, vadd_vsub] @[simp] theorem vadd_right_cancel_iff {g₁ g₂ : G} (p : P) : g₁ +ᵥ p = g₂ +ᵥ p ↔ g₁ = g₂ := ⟨vadd_right_cancel p, fun h => h ▸ rfl⟩ /-- Adding a group element to the point `p` is an injective function. -/ theorem vadd_right_injective (p : P) : Function.Injective ((· +ᵥ p) : G → P) := fun _ _ => vadd_right_cancel p /-- Adding a group element to a point, then subtracting another point, produces the same result as subtracting the points then adding the group element. -/ theorem vadd_vsub_assoc (g : G) (p₁ p₂ : P) : g +ᵥ p₁ -ᵥ p₂ = g + (p₁ -ᵥ p₂) := by apply vadd_right_cancel p₂ rw [vsub_vadd, add_vadd, vsub_vadd] /-- Subtracting a point from itself produces 0. -/ @[simp] theorem vsub_self (p : P) : p -ᵥ p = (0 : G) := by rw [← zero_add (p -ᵥ p), ← vadd_vsub_assoc, vadd_vsub] /-- If subtracting two points produces 0, they are equal. -/ theorem eq_of_vsub_eq_zero {p₁ p₂ : P} (h : p₁ -ᵥ p₂ = (0 : G)) : p₁ = p₂ := by rw [← vsub_vadd p₁ p₂, h, zero_vadd] /-- Subtracting two points produces 0 if and only if they are equal. -/ @[simp] theorem vsub_eq_zero_iff_eq {p₁ p₂ : P} : p₁ -ᵥ p₂ = (0 : G) ↔ p₁ = p₂ := Iff.intro eq_of_vsub_eq_zero fun h => h ▸ vsub_self _ theorem vsub_ne_zero {p q : P} : p -ᵥ q ≠ (0 : G) ↔ p ≠ q := not_congr vsub_eq_zero_iff_eq /-- Cancellation adding the results of two subtractions. -/ @[simp] theorem vsub_add_vsub_cancel (p₁ p₂ p₃ : P) : p₁ -ᵥ p₂ + (p₂ -ᵥ p₃) = p₁ -ᵥ p₃ := by apply vadd_right_cancel p₃ rw [add_vadd, vsub_vadd, vsub_vadd, vsub_vadd] /-- Subtracting two points in the reverse order produces the negation of subtracting them. -/ @[simp] theorem neg_vsub_eq_vsub_rev (p₁ p₂ : P) : -(p₁ -ᵥ p₂) = p₂ -ᵥ p₁ := by refine neg_eq_of_add_eq_zero_right (vadd_right_cancel p₁ ?_) rw [vsub_add_vsub_cancel, vsub_self] theorem vadd_vsub_eq_sub_vsub (g : G) (p q : P) : g +ᵥ p -ᵥ q = g - (q -ᵥ p) := by rw [vadd_vsub_assoc, sub_eq_add_neg, neg_vsub_eq_vsub_rev] /-- Subtracting the result of adding a group element produces the same result as subtracting the points and subtracting that group element. -/ theorem vsub_vadd_eq_vsub_sub (p₁ p₂ : P) (g : G) : p₁ -ᵥ (g +ᵥ p₂) = p₁ -ᵥ p₂ - g := by rw [← add_right_inj (p₂ -ᵥ p₁ : G), vsub_add_vsub_cancel, ← neg_vsub_eq_vsub_rev, vadd_vsub, ← add_sub_assoc, ← neg_vsub_eq_vsub_rev, neg_add_self, zero_sub] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] theorem vsub_sub_vsub_cancel_right (p₁ p₂ p₃ : P) : p₁ -ᵥ p₃ - (p₂ -ᵥ p₃) = p₁ -ᵥ p₂ := by rw [← vsub_vadd_eq_vsub_sub, vsub_vadd] /-- Convert between an equality with adding a group element to a point and an equality of a subtraction of two points with a group element. -/ theorem eq_vadd_iff_vsub_eq (p₁ : P) (g : G) (p₂ : P) : p₁ = g +ᵥ p₂ ↔ p₁ -ᵥ p₂ = g := ⟨fun h => h.symm ▸ vadd_vsub _ _, fun h => h ▸ (vsub_vadd _ _).symm⟩ theorem vadd_eq_vadd_iff_neg_add_eq_vsub {v₁ v₂ : G} {p₁ p₂ : P} : v₁ +ᵥ p₁ = v₂ +ᵥ p₂ ↔ -v₁ + v₂ = p₁ -ᵥ p₂ := by rw [eq_vadd_iff_vsub_eq, vadd_vsub_assoc, ← add_right_inj (-v₁), neg_add_cancel_left, eq_comm] namespace Set open Pointwise -- porting note (#10618): simp can prove this --@[simp] theorem singleton_vsub_self (p : P) : ({p} : Set P) -ᵥ {p} = {(0 : G)} := by rw [Set.singleton_vsub_singleton, vsub_self] end Set @[simp] theorem vadd_vsub_vadd_cancel_right (v₁ v₂ : G) (p : P) : v₁ +ᵥ p -ᵥ (v₂ +ᵥ p) = v₁ - v₂ := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, vsub_self, add_zero] /-- If the same point subtracted from two points produces equal results, those points are equal. -/ theorem vsub_left_cancel {p₁ p₂ p : P} (h : p₁ -ᵥ p = p₂ -ᵥ p) : p₁ = p₂ := by rwa [← sub_eq_zero, vsub_sub_vsub_cancel_right, vsub_eq_zero_iff_eq] at h /-- The same point subtracted from two points produces equal results if and only if those points are equal. -/ @[simp] theorem vsub_left_cancel_iff {p₁ p₂ p : P} : p₁ -ᵥ p = p₂ -ᵥ p ↔ p₁ = p₂ := ⟨vsub_left_cancel, fun h => h ▸ rfl⟩ /-- Subtracting the point `p` is an injective function. -/ theorem vsub_left_injective (p : P) : Function.Injective ((· -ᵥ p) : P → G) := fun _ _ => vsub_left_cancel /-- If subtracting two points from the same point produces equal results, those points are equal. -/ theorem vsub_right_cancel {p₁ p₂ p : P} (h : p -ᵥ p₁ = p -ᵥ p₂) : p₁ = p₂ := by refine vadd_left_cancel (p -ᵥ p₂) ?_ rw [vsub_vadd, ← h, vsub_vadd] /-- Subtracting two points from the same point produces equal results if and only if those points are equal. -/ @[simp] theorem vsub_right_cancel_iff {p₁ p₂ p : P} : p -ᵥ p₁ = p -ᵥ p₂ ↔ p₁ = p₂ := ⟨vsub_right_cancel, fun h => h ▸ rfl⟩ /-- Subtracting a point from the point `p` is an injective function. -/ theorem vsub_right_injective (p : P) : Function.Injective ((p -ᵥ ·) : P → G) := fun _ _ => vsub_right_cancel end General section comm variable {G : Type*} {P : Type*} [AddCommGroup G] [AddTorsor G P] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] theorem vsub_sub_vsub_cancel_left (p₁ p₂ p₃ : P) : p₃ -ᵥ p₂ - (p₃ -ᵥ p₁) = p₁ -ᵥ p₂ := by rw [sub_eq_add_neg, neg_vsub_eq_vsub_rev, add_comm, vsub_add_vsub_cancel] @[simp] theorem vadd_vsub_vadd_cancel_left (v : G) (p₁ p₂ : P) : v +ᵥ p₁ -ᵥ (v +ᵥ p₂) = p₁ -ᵥ p₂ := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub_cancel_left] theorem vsub_vadd_comm (p₁ p₂ p₃ : P) : (p₁ -ᵥ p₂ : G) +ᵥ p₃ = p₃ -ᵥ p₂ +ᵥ p₁ := by rw [← @vsub_eq_zero_iff_eq G, vadd_vsub_assoc, vsub_vadd_eq_vsub_sub] simp theorem vadd_eq_vadd_iff_sub_eq_vsub {v₁ v₂ : G} {p₁ p₂ : P} : v₁ +ᵥ p₁ = v₂ +ᵥ p₂ ↔ v₂ - v₁ = p₁ -ᵥ p₂ := by rw [vadd_eq_vadd_iff_neg_add_eq_vsub, neg_add_eq_sub] theorem vsub_sub_vsub_comm (p₁ p₂ p₃ p₄ : P) : p₁ -ᵥ p₂ - (p₃ -ᵥ p₄) = p₁ -ᵥ p₃ - (p₂ -ᵥ p₄) := by rw [← vsub_vadd_eq_vsub_sub, vsub_vadd_comm, vsub_vadd_eq_vsub_sub] end comm namespace Prod variable {G G' P P' : Type*} [AddGroup G] [AddGroup G'] [AddTorsor G P] [AddTorsor G' P'] instance instAddTorsor : AddTorsor (G × G') (P × P') where vadd v p := (v.1 +ᵥ p.1, v.2 +ᵥ p.2) zero_vadd _ := Prod.ext (zero_vadd _ _) (zero_vadd _ _) add_vadd _ _ _ := Prod.ext (add_vadd _ _ _) (add_vadd _ _ _) vsub p₁ p₂ := (p₁.1 -ᵥ p₂.1, p₁.2 -ᵥ p₂.2) nonempty := Prod.instNonempty vsub_vadd' _ _ := Prod.ext (vsub_vadd _ _) (vsub_vadd _ _) vadd_vsub' _ _ := Prod.ext (vadd_vsub _ _) (vadd_vsub _ _) -- Porting note: The proofs above used to be shorter: -- zero_vadd p := by simp ⊢ 0 +ᵥ p = p -- add_vadd := by simp [add_vadd] ⊢ ∀ (a : G) (b : G') (a_1 : G) (b_1 : G') (a_2 : P) (b_2 : P'), -- (a + a_1, b + b_1) +ᵥ (a_2, b_2) = (a, b) +ᵥ ((a_1, b_1) +ᵥ (a_2, b_2)) -- vsub_vadd' p₁ p₂ := show (p₁.1 -ᵥ p₂.1 +ᵥ p₂.1, _) = p₁ by simp -- ⊢ (p₁.fst -ᵥ p₂.fst +ᵥ p₂.fst, ((p₁.fst -ᵥ p₂.fst, p₁.snd -ᵥ p₂.snd) +ᵥ p₂).snd) = p₁ -- vadd_vsub' v p := show (v.1 +ᵥ p.1 -ᵥ p.1, v.2 +ᵥ p.2 -ᵥ p.2) = v by simp -- ⊢ (v.fst +ᵥ p.fst -ᵥ p.fst, v.snd) = v @[simp] theorem fst_vadd (v : G × G') (p : P × P') : (v +ᵥ p).1 = v.1 +ᵥ p.1 := rfl @[simp] theorem snd_vadd (v : G × G') (p : P × P') : (v +ᵥ p).2 = v.2 +ᵥ p.2 := rfl @[simp] theorem mk_vadd_mk (v : G) (v' : G') (p : P) (p' : P') : (v, v') +ᵥ (p, p') = (v +ᵥ p, v' +ᵥ p') := rfl @[simp] theorem fst_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').1 = p₁.1 -ᵥ p₂.1 := rfl @[simp] theorem snd_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').2 = p₁.2 -ᵥ p₂.2 := rfl @[simp] theorem mk_vsub_mk (p₁ p₂ : P) (p₁' p₂' : P') : ((p₁, p₁') -ᵥ (p₂, p₂') : G × G') = (p₁ -ᵥ p₂, p₁' -ᵥ p₂') := rfl end Prod namespace Pi universe u v w variable {I : Type u} {fg : I → Type v} [∀ i, AddGroup (fg i)] {fp : I → Type w} open AddAction AddTorsor /-- A product of `AddTorsor`s is an `AddTorsor`. -/ instance instAddTorsor [∀ i, AddTorsor (fg i) (fp i)] : AddTorsor (∀ i, fg i) (∀ i, fp i) where vadd g p i := g i +ᵥ p i zero_vadd p := funext fun i => zero_vadd (fg i) (p i) add_vadd g₁ g₂ p := funext fun i => add_vadd (g₁ i) (g₂ i) (p i) vsub p₁ p₂ i := p₁ i -ᵥ p₂ i vsub_vadd' p₁ p₂ := funext fun i => vsub_vadd (p₁ i) (p₂ i) vadd_vsub' g p := funext fun i => vadd_vsub (g i) (p i) end Pi namespace Equiv variable {G : Type*} {P : Type*} [AddGroup G] [AddTorsor G P] /-- `v ↦ v +ᵥ p` as an equivalence. -/ def vaddConst (p : P) : G ≃ P where toFun v := v +ᵥ p invFun p' := p' -ᵥ p left_inv _ := vadd_vsub _ _ right_inv _ := vsub_vadd _ _ @[simp] theorem coe_vaddConst (p : P) : ⇑(vaddConst p) = fun v => v +ᵥ p := rfl @[simp] theorem coe_vaddConst_symm (p : P) : ⇑(vaddConst p).symm = fun p' => p' -ᵥ p := rfl /-- `p' ↦ p -ᵥ p'` as an equivalence. -/ def constVSub (p : P) : P ≃ G where toFun := (p -ᵥ ·) invFun := (-· +ᵥ p) left_inv p' := by simp right_inv v := by simp [vsub_vadd_eq_vsub_sub] @[simp] lemma coe_constVSub (p : P) : ⇑(constVSub p) = (p -ᵥ ·) := rfl @[simp] theorem coe_constVSub_symm (p : P) : ⇑(constVSub p).symm = fun (v : G) => -v +ᵥ p := rfl variable (P) /-- The permutation given by `p ↦ v +ᵥ p`. -/ def constVAdd (v : G) : Equiv.Perm P where toFun := (v +ᵥ ·) invFun := (-v +ᵥ ·) left_inv p := by simp [vadd_vadd] right_inv p := by simp [vadd_vadd] @[simp] lemma coe_constVAdd (v : G) : ⇑(constVAdd P v) = (v +ᵥ ·) := rfl variable (G) @[simp] theorem constVAdd_zero : constVAdd P (0 : G) = 1 := ext <| zero_vadd G variable {G} @[simp] theorem constVAdd_add (v₁ v₂ : G) : constVAdd P (v₁ + v₂) = constVAdd P v₁ * constVAdd P v₂ := ext <| add_vadd v₁ v₂ /-- `Equiv.constVAdd` as a homomorphism from `Multiplicative G` to `Equiv.perm P` -/ def constVAddHom : Multiplicative G →* Equiv.Perm P where toFun v := constVAdd P (Multiplicative.toAdd v) map_one' := constVAdd_zero G P map_mul' := constVAdd_add P variable {P} -- Porting note: Previous code was: -- open _Root_.Function open Function /-- Point reflection in `x` as a permutation. -/ def pointReflection (x : P) : Perm P := (constVSub x).trans (vaddConst x) theorem pointReflection_apply (x y : P) : pointReflection x y = x -ᵥ y +ᵥ x := rfl @[simp] theorem pointReflection_vsub_left (x y : P) : pointReflection x y -ᵥ x = x -ᵥ y := vadd_vsub .. @[simp] theorem left_vsub_pointReflection (x y : P) : x -ᵥ pointReflection x y = y -ᵥ x := neg_injective <| by simp @[simp] theorem pointReflection_vsub_right (x y : P) : pointReflection x y -ᵥ y = 2 • (x -ᵥ y) := by simp [pointReflection, two_nsmul, vadd_vsub_assoc] @[simp] theorem right_vsub_pointReflection (x y : P) : y -ᵥ pointReflection x y = 2 • (y -ᵥ x) := neg_injective <| by simp [← neg_nsmul] @[simp] theorem pointReflection_symm (x : P) : (pointReflection x).symm = pointReflection x := ext <| by simp [pointReflection] @[simp] theorem pointReflection_self (x : P) : pointReflection x x = x := vsub_vadd _ _ theorem pointReflection_involutive (x : P) : Involutive (pointReflection x : P → P) := fun y => (Equiv.apply_eq_iff_eq_symm_apply _).2 <| by rw [pointReflection_symm] /-- `x` is the only fixed point of `pointReflection x`. This lemma requires `x + x = y + y ↔ x = y`. There is no typeclass to use here, so we add it as an explicit argument. -/ theorem pointReflection_fixed_iff_of_injective_bit0 {x y : P} (h : Injective (2 • · : G → G)) : pointReflection x y = y ↔ y = x := by rw [pointReflection_apply, eq_comm, eq_vadd_iff_vsub_eq, ← neg_vsub_eq_vsub_rev, neg_eq_iff_add_eq_zero, ← two_nsmul, ← nsmul_zero 2, h.eq_iff, vsub_eq_zero_iff_eq, eq_comm] -- Porting note: need this to calm down CI theorem injective_pointReflection_left_of_injective_bit0 {G P : Type*} [AddCommGroup G] [AddTorsor G P] (h : Injective (2 • · : G → G)) (y : P) : Injective fun x : P => pointReflection x y := fun x₁ x₂ (hy : pointReflection x₁ y = pointReflection x₂ y) => by rwa [pointReflection_apply, pointReflection_apply, vadd_eq_vadd_iff_sub_eq_vsub, vsub_sub_vsub_cancel_right, ← neg_vsub_eq_vsub_rev, neg_eq_iff_add_eq_zero, ← two_nsmul, ← nsmul_zero 2, h.eq_iff, vsub_eq_zero_iff_eq] at hy end Equiv theorem AddTorsor.subsingleton_iff (G P : Type*) [AddGroup G] [AddTorsor G P] : Subsingleton G ↔ Subsingleton P := by inhabit P exact (Equiv.vaddConst default).subsingleton_congr
Algebra\AlgebraicCard.lean
/- Copyright (c) 2022 Violeta Hernández Palacios. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta Hernández Palacios -/ import Mathlib.Algebra.Polynomial.Cardinal import Mathlib.RingTheory.Algebraic /-! ### Cardinality of algebraic numbers In this file, we prove variants of the following result: the cardinality of algebraic numbers under an R-algebra is at most `# R[X] * ℵ₀`. Although this can be used to prove that real or complex transcendental numbers exist, a more direct proof is given by `Liouville.is_transcendental`. -/ universe u v open Cardinal Polynomial Set open Cardinal Polynomial namespace Algebraic theorem infinite_of_charZero (R A : Type*) [CommRing R] [IsDomain R] [Ring A] [Algebra R A] [CharZero A] : { x : A | IsAlgebraic R x }.Infinite := infinite_of_injective_forall_mem Nat.cast_injective isAlgebraic_nat theorem aleph0_le_cardinal_mk_of_charZero (R A : Type*) [CommRing R] [IsDomain R] [Ring A] [Algebra R A] [CharZero A] : ℵ₀ ≤ #{ x : A // IsAlgebraic R x } := infinite_iff.1 (Set.infinite_coe_iff.2 <| infinite_of_charZero R A) section lift variable (R : Type u) (A : Type v) [CommRing R] [CommRing A] [IsDomain A] [Algebra R A] [NoZeroSMulDivisors R A] theorem cardinal_mk_lift_le_mul : Cardinal.lift.{u} #{ x : A // IsAlgebraic R x } ≤ Cardinal.lift.{v} #R[X] * ℵ₀ := by rw [← mk_uLift, ← mk_uLift] choose g hg₁ hg₂ using fun x : { x : A | IsAlgebraic R x } => x.coe_prop refine lift_mk_le_lift_mk_mul_of_lift_mk_preimage_le g fun f => ?_ rw [lift_le_aleph0, le_aleph0_iff_set_countable] suffices MapsTo (↑) (g ⁻¹' {f}) (f.rootSet A) from this.countable_of_injOn Subtype.coe_injective.injOn (f.rootSet_finite A).countable rintro x (rfl : g x = f) exact mem_rootSet.2 ⟨hg₁ x, hg₂ x⟩ theorem cardinal_mk_lift_le_max : Cardinal.lift.{u} #{ x : A // IsAlgebraic R x } ≤ max (Cardinal.lift.{v} #R) ℵ₀ := (cardinal_mk_lift_le_mul R A).trans <| (mul_le_mul_right' (lift_le.2 cardinal_mk_le_max) _).trans <| by simp @[simp] theorem cardinal_mk_lift_of_infinite [Infinite R] : Cardinal.lift.{u} #{ x : A // IsAlgebraic R x } = Cardinal.lift.{v} #R := ((cardinal_mk_lift_le_max R A).trans_eq (max_eq_left <| aleph0_le_mk _)).antisymm <| lift_mk_le'.2 ⟨⟨fun x => ⟨algebraMap R A x, isAlgebraic_algebraMap _⟩, fun _ _ h => NoZeroSMulDivisors.algebraMap_injective R A (Subtype.ext_iff.1 h)⟩⟩ variable [Countable R] @[simp] protected theorem countable : Set.Countable { x : A | IsAlgebraic R x } := by rw [← le_aleph0_iff_set_countable, ← lift_le_aleph0] apply (cardinal_mk_lift_le_max R A).trans simp @[simp] theorem cardinal_mk_of_countable_of_charZero [CharZero A] [IsDomain R] : #{ x : A // IsAlgebraic R x } = ℵ₀ := (Algebraic.countable R A).le_aleph0.antisymm (aleph0_le_cardinal_mk_of_charZero R A) end lift section NonLift variable (R A : Type u) [CommRing R] [CommRing A] [IsDomain A] [Algebra R A] [NoZeroSMulDivisors R A] theorem cardinal_mk_le_mul : #{ x : A // IsAlgebraic R x } ≤ #R[X] * ℵ₀ := by rw [← lift_id #_, ← lift_id #R[X]] exact cardinal_mk_lift_le_mul R A theorem cardinal_mk_le_max : #{ x : A // IsAlgebraic R x } ≤ max #R ℵ₀ := by rw [← lift_id #_, ← lift_id #R] exact cardinal_mk_lift_le_max R A @[simp] theorem cardinal_mk_of_infinite [Infinite R] : #{ x : A // IsAlgebraic R x } = #R := lift_inj.1 <| cardinal_mk_lift_of_infinite R A end NonLift end Algebraic
Algebra\Bounds.lean
/- Copyright (c) 2021 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov -/ import Mathlib.Algebra.Order.Group.OrderIso import Mathlib.Data.Set.Pointwise.Basic import Mathlib.Order.Bounds.OrderIso import Mathlib.Order.ConditionallyCompleteLattice.Basic import Mathlib.Algebra.Order.Monoid.Unbundled.OrderDual /-! # Upper/lower bounds in ordered monoids and groups In this file we prove a few facts like “`-s` is bounded above iff `s` is bounded below” (`bddAbove_neg`). -/ open Function Set open Pointwise section InvNeg variable {G : Type*} [Group G] [Preorder G] [CovariantClass G G (· * ·) (· ≤ ·)] [CovariantClass G G (swap (· * ·)) (· ≤ ·)] {s : Set G} {a : G} @[to_additive (attr := simp)] theorem bddAbove_inv : BddAbove s⁻¹ ↔ BddBelow s := (OrderIso.inv G).bddAbove_preimage @[to_additive (attr := simp)] theorem bddBelow_inv : BddBelow s⁻¹ ↔ BddAbove s := (OrderIso.inv G).bddBelow_preimage @[to_additive] theorem BddAbove.inv (h : BddAbove s) : BddBelow s⁻¹ := bddBelow_inv.2 h @[to_additive] theorem BddBelow.inv (h : BddBelow s) : BddAbove s⁻¹ := bddAbove_inv.2 h @[to_additive (attr := simp)] theorem isLUB_inv : IsLUB s⁻¹ a ↔ IsGLB s a⁻¹ := (OrderIso.inv G).isLUB_preimage @[to_additive] theorem isLUB_inv' : IsLUB s⁻¹ a⁻¹ ↔ IsGLB s a := (OrderIso.inv G).isLUB_preimage' @[to_additive] theorem IsGLB.inv (h : IsGLB s a) : IsLUB s⁻¹ a⁻¹ := isLUB_inv'.2 h @[to_additive (attr := simp)] theorem isGLB_inv : IsGLB s⁻¹ a ↔ IsLUB s a⁻¹ := (OrderIso.inv G).isGLB_preimage @[to_additive] theorem isGLB_inv' : IsGLB s⁻¹ a⁻¹ ↔ IsLUB s a := (OrderIso.inv G).isGLB_preimage' @[to_additive] theorem IsLUB.inv (h : IsLUB s a) : IsGLB s⁻¹ a⁻¹ := isGLB_inv'.2 h @[to_additive] lemma BddBelow.range_inv {α : Type*} {f : α → G} (hf : BddBelow (range f)) : BddAbove (range (fun x => (f x)⁻¹)) := hf.range_comp (OrderIso.inv G).monotone @[to_additive] lemma BddAbove.range_inv {α : Type*} {f : α → G} (hf : BddAbove (range f)) : BddBelow (range (fun x => (f x)⁻¹)) := BddBelow.range_inv (G := Gᵒᵈ) hf end InvNeg section mul_add variable {M : Type*} [Mul M] [Preorder M] [CovariantClass M M (· * ·) (· ≤ ·)] [CovariantClass M M (swap (· * ·)) (· ≤ ·)] @[to_additive] theorem mul_mem_upperBounds_mul {s t : Set M} {a b : M} (ha : a ∈ upperBounds s) (hb : b ∈ upperBounds t) : a * b ∈ upperBounds (s * t) := forall_image2_iff.2 fun _ hx _ hy => mul_le_mul' (ha hx) (hb hy) @[to_additive] theorem subset_upperBounds_mul (s t : Set M) : upperBounds s * upperBounds t ⊆ upperBounds (s * t) := image2_subset_iff.2 fun _ hx _ hy => mul_mem_upperBounds_mul hx hy @[to_additive] theorem mul_mem_lowerBounds_mul {s t : Set M} {a b : M} (ha : a ∈ lowerBounds s) (hb : b ∈ lowerBounds t) : a * b ∈ lowerBounds (s * t) := mul_mem_upperBounds_mul (M := Mᵒᵈ) ha hb @[to_additive] theorem subset_lowerBounds_mul (s t : Set M) : lowerBounds s * lowerBounds t ⊆ lowerBounds (s * t) := subset_upperBounds_mul (M := Mᵒᵈ) _ _ @[to_additive] theorem BddAbove.mul {s t : Set M} (hs : BddAbove s) (ht : BddAbove t) : BddAbove (s * t) := (Nonempty.mul hs ht).mono (subset_upperBounds_mul s t) @[to_additive] theorem BddBelow.mul {s t : Set M} (hs : BddBelow s) (ht : BddBelow t) : BddBelow (s * t) := (Nonempty.mul hs ht).mono (subset_lowerBounds_mul s t) @[to_additive] lemma BddAbove.range_mul {α : Type*} {f g : α → M} (hf : BddAbove (range f)) (hg : BddAbove (range g)) : BddAbove (range (fun x => f x * g x)) := BddAbove.range_comp (f := fun x => (⟨f x, g x⟩ : M × M)) (bddAbove_range_prod.mpr ⟨hf, hg⟩) (Monotone.mul' monotone_fst monotone_snd) @[to_additive] lemma BddBelow.range_mul {α : Type*} {f g : α → M} (hf : BddBelow (range f)) (hg : BddBelow (range g)) : BddBelow (range (fun x => f x * g x)) := BddAbove.range_mul (M := Mᵒᵈ) hf hg end mul_add section ConditionallyCompleteLattice section Right variable {ι G : Type*} [Group G] [ConditionallyCompleteLattice G] [CovariantClass G G (Function.swap (· * ·)) (· ≤ ·)] [Nonempty ι] {f : ι → G} @[to_additive] theorem ciSup_mul (hf : BddAbove (range f)) (a : G) : (⨆ i, f i) * a = ⨆ i, f i * a := (OrderIso.mulRight a).map_ciSup hf @[to_additive] theorem ciSup_div (hf : BddAbove (range f)) (a : G) : (⨆ i, f i) / a = ⨆ i, f i / a := by simp only [div_eq_mul_inv, ciSup_mul hf] @[to_additive] theorem ciInf_mul (hf : BddBelow (range f)) (a : G) : (⨅ i, f i) * a = ⨅ i, f i * a := (OrderIso.mulRight a).map_ciInf hf @[to_additive] theorem ciInf_div (hf : BddBelow (range f)) (a : G) : (⨅ i, f i) / a = ⨅ i, f i / a := by simp only [div_eq_mul_inv, ciInf_mul hf] end Right section Left variable {ι : Sort*} {G : Type*} [Group G] [ConditionallyCompleteLattice G] [CovariantClass G G (· * ·) (· ≤ ·)] [Nonempty ι] {f : ι → G} @[to_additive] theorem mul_ciSup (hf : BddAbove (range f)) (a : G) : (a * ⨆ i, f i) = ⨆ i, a * f i := (OrderIso.mulLeft a).map_ciSup hf @[to_additive] theorem mul_ciInf (hf : BddBelow (range f)) (a : G) : (a * ⨅ i, f i) = ⨅ i, a * f i := (OrderIso.mulLeft a).map_ciInf hf end Left end ConditionallyCompleteLattice
Algebra\CubicDiscriminant.lean
/- Copyright (c) 2022 David Kurniadi Angdinata. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Kurniadi Angdinata -/ import Mathlib.Algebra.Polynomial.Splits /-! # Cubics and discriminants This file defines cubic polynomials over a semiring and their discriminants over a splitting field. ## Main definitions * `Cubic`: the structure representing a cubic polynomial. * `Cubic.disc`: the discriminant of a cubic polynomial. ## Main statements * `Cubic.disc_ne_zero_iff_roots_nodup`: the cubic discriminant is not equal to zero if and only if the cubic has no duplicate roots. ## References * https://en.wikipedia.org/wiki/Cubic_equation * https://en.wikipedia.org/wiki/Discriminant ## Tags cubic, discriminant, polynomial, root -/ noncomputable section /-- The structure representing a cubic polynomial. -/ @[ext] structure Cubic (R : Type*) where (a b c d : R) namespace Cubic open Cubic Polynomial open Polynomial variable {R S F K : Type*} instance [Inhabited R] : Inhabited (Cubic R) := ⟨⟨default, default, default, default⟩⟩ instance [Zero R] : Zero (Cubic R) := ⟨⟨0, 0, 0, 0⟩⟩ section Basic variable {P Q : Cubic R} {a b c d a' b' c' d' : R} [Semiring R] /-- Convert a cubic polynomial to a polynomial. -/ def toPoly (P : Cubic R) : R[X] := C P.a * X ^ 3 + C P.b * X ^ 2 + C P.c * X + C P.d theorem C_mul_prod_X_sub_C_eq [CommRing S] {w x y z : S} : C w * (X - C x) * (X - C y) * (X - C z) = toPoly ⟨w, w * -(x + y + z), w * (x * y + x * z + y * z), w * -(x * y * z)⟩ := by simp only [toPoly, C_neg, C_add, C_mul] ring1 theorem prod_X_sub_C_eq [CommRing S] {x y z : S} : (X - C x) * (X - C y) * (X - C z) = toPoly ⟨1, -(x + y + z), x * y + x * z + y * z, -(x * y * z)⟩ := by rw [← one_mul <| X - C x, ← C_1, C_mul_prod_X_sub_C_eq, one_mul, one_mul, one_mul] /-! ### Coefficients -/ section Coeff private theorem coeffs : (∀ n > 3, P.toPoly.coeff n = 0) ∧ P.toPoly.coeff 3 = P.a ∧ P.toPoly.coeff 2 = P.b ∧ P.toPoly.coeff 1 = P.c ∧ P.toPoly.coeff 0 = P.d := by simp only [toPoly, coeff_add, coeff_C, coeff_C_mul_X, coeff_C_mul_X_pow] norm_num intro n hn repeat' rw [if_neg] any_goals linarith only [hn] repeat' rw [zero_add] @[simp] theorem coeff_eq_zero {n : ℕ} (hn : 3 < n) : P.toPoly.coeff n = 0 := coeffs.1 n hn @[simp] theorem coeff_eq_a : P.toPoly.coeff 3 = P.a := coeffs.2.1 @[simp] theorem coeff_eq_b : P.toPoly.coeff 2 = P.b := coeffs.2.2.1 @[simp] theorem coeff_eq_c : P.toPoly.coeff 1 = P.c := coeffs.2.2.2.1 @[simp] theorem coeff_eq_d : P.toPoly.coeff 0 = P.d := coeffs.2.2.2.2 theorem a_of_eq (h : P.toPoly = Q.toPoly) : P.a = Q.a := by rw [← coeff_eq_a, h, coeff_eq_a] theorem b_of_eq (h : P.toPoly = Q.toPoly) : P.b = Q.b := by rw [← coeff_eq_b, h, coeff_eq_b] theorem c_of_eq (h : P.toPoly = Q.toPoly) : P.c = Q.c := by rw [← coeff_eq_c, h, coeff_eq_c] theorem d_of_eq (h : P.toPoly = Q.toPoly) : P.d = Q.d := by rw [← coeff_eq_d, h, coeff_eq_d] theorem toPoly_injective (P Q : Cubic R) : P.toPoly = Q.toPoly ↔ P = Q := ⟨fun h ↦ Cubic.ext (a_of_eq h) (b_of_eq h) (c_of_eq h) (d_of_eq h), congr_arg toPoly⟩ theorem of_a_eq_zero (ha : P.a = 0) : P.toPoly = C P.b * X ^ 2 + C P.c * X + C P.d := by rw [toPoly, ha, C_0, zero_mul, zero_add] theorem of_a_eq_zero' : toPoly ⟨0, b, c, d⟩ = C b * X ^ 2 + C c * X + C d := of_a_eq_zero rfl theorem of_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) : P.toPoly = C P.c * X + C P.d := by rw [of_a_eq_zero ha, hb, C_0, zero_mul, zero_add] theorem of_b_eq_zero' : toPoly ⟨0, 0, c, d⟩ = C c * X + C d := of_b_eq_zero rfl rfl theorem of_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.toPoly = C P.d := by rw [of_b_eq_zero ha hb, hc, C_0, zero_mul, zero_add] theorem of_c_eq_zero' : toPoly ⟨0, 0, 0, d⟩ = C d := of_c_eq_zero rfl rfl rfl theorem of_d_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d = 0) : P.toPoly = 0 := by rw [of_c_eq_zero ha hb hc, hd, C_0] theorem of_d_eq_zero' : (⟨0, 0, 0, 0⟩ : Cubic R).toPoly = 0 := of_d_eq_zero rfl rfl rfl rfl theorem zero : (0 : Cubic R).toPoly = 0 := of_d_eq_zero' theorem toPoly_eq_zero_iff (P : Cubic R) : P.toPoly = 0 ↔ P = 0 := by rw [← zero, toPoly_injective] private theorem ne_zero (h0 : P.a ≠ 0 ∨ P.b ≠ 0 ∨ P.c ≠ 0 ∨ P.d ≠ 0) : P.toPoly ≠ 0 := by contrapose! h0 rw [(toPoly_eq_zero_iff P).mp h0] exact ⟨rfl, rfl, rfl, rfl⟩ theorem ne_zero_of_a_ne_zero (ha : P.a ≠ 0) : P.toPoly ≠ 0 := (or_imp.mp ne_zero).1 ha theorem ne_zero_of_b_ne_zero (hb : P.b ≠ 0) : P.toPoly ≠ 0 := (or_imp.mp (or_imp.mp ne_zero).2).1 hb theorem ne_zero_of_c_ne_zero (hc : P.c ≠ 0) : P.toPoly ≠ 0 := (or_imp.mp (or_imp.mp (or_imp.mp ne_zero).2).2).1 hc theorem ne_zero_of_d_ne_zero (hd : P.d ≠ 0) : P.toPoly ≠ 0 := (or_imp.mp (or_imp.mp (or_imp.mp ne_zero).2).2).2 hd @[simp] theorem leadingCoeff_of_a_ne_zero (ha : P.a ≠ 0) : P.toPoly.leadingCoeff = P.a := leadingCoeff_cubic ha @[simp] theorem leadingCoeff_of_a_ne_zero' (ha : a ≠ 0) : (toPoly ⟨a, b, c, d⟩).leadingCoeff = a := leadingCoeff_of_a_ne_zero ha @[simp] theorem leadingCoeff_of_b_ne_zero (ha : P.a = 0) (hb : P.b ≠ 0) : P.toPoly.leadingCoeff = P.b := by rw [of_a_eq_zero ha, leadingCoeff_quadratic hb] @[simp] theorem leadingCoeff_of_b_ne_zero' (hb : b ≠ 0) : (toPoly ⟨0, b, c, d⟩).leadingCoeff = b := leadingCoeff_of_b_ne_zero rfl hb @[simp] theorem leadingCoeff_of_c_ne_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c ≠ 0) : P.toPoly.leadingCoeff = P.c := by rw [of_b_eq_zero ha hb, leadingCoeff_linear hc] @[simp] theorem leadingCoeff_of_c_ne_zero' (hc : c ≠ 0) : (toPoly ⟨0, 0, c, d⟩).leadingCoeff = c := leadingCoeff_of_c_ne_zero rfl rfl hc @[simp] theorem leadingCoeff_of_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.toPoly.leadingCoeff = P.d := by rw [of_c_eq_zero ha hb hc, leadingCoeff_C] -- @[simp] -- porting note (#10618): simp can prove this theorem leadingCoeff_of_c_eq_zero' : (toPoly ⟨0, 0, 0, d⟩).leadingCoeff = d := leadingCoeff_of_c_eq_zero rfl rfl rfl theorem monic_of_a_eq_one (ha : P.a = 1) : P.toPoly.Monic := by nontriviality R rw [Monic, leadingCoeff_of_a_ne_zero (ha ▸ one_ne_zero), ha] theorem monic_of_a_eq_one' : (toPoly ⟨1, b, c, d⟩).Monic := monic_of_a_eq_one rfl theorem monic_of_b_eq_one (ha : P.a = 0) (hb : P.b = 1) : P.toPoly.Monic := by nontriviality R rw [Monic, leadingCoeff_of_b_ne_zero ha (hb ▸ one_ne_zero), hb] theorem monic_of_b_eq_one' : (toPoly ⟨0, 1, c, d⟩).Monic := monic_of_b_eq_one rfl rfl theorem monic_of_c_eq_one (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 1) : P.toPoly.Monic := by nontriviality R rw [Monic, leadingCoeff_of_c_ne_zero ha hb (hc ▸ one_ne_zero), hc] theorem monic_of_c_eq_one' : (toPoly ⟨0, 0, 1, d⟩).Monic := monic_of_c_eq_one rfl rfl rfl theorem monic_of_d_eq_one (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d = 1) : P.toPoly.Monic := by rw [Monic, leadingCoeff_of_c_eq_zero ha hb hc, hd] theorem monic_of_d_eq_one' : (toPoly ⟨0, 0, 0, 1⟩).Monic := monic_of_d_eq_one rfl rfl rfl rfl end Coeff /-! ### Degrees -/ section Degree /-- The equivalence between cubic polynomials and polynomials of degree at most three. -/ @[simps] def equiv : Cubic R ≃ { p : R[X] // p.degree ≤ 3 } where toFun P := ⟨P.toPoly, degree_cubic_le⟩ invFun f := ⟨coeff f 3, coeff f 2, coeff f 1, coeff f 0⟩ left_inv P := by ext <;> simp only [Subtype.coe_mk, coeffs] right_inv f := by -- Porting note: Added `simp only [Nat.zero_eq, Nat.succ_eq_add_one] <;> ring_nf` -- There's probably a better way to do this. ext (_ | _ | _ | _ | n) <;> simp only [Nat.zero_eq, Nat.succ_eq_add_one] <;> ring_nf <;> try simp only [coeffs] have h3 : 3 < 4 + n := by linarith only rw [coeff_eq_zero h3, (degree_le_iff_coeff_zero (f : R[X]) 3).mp f.2 _ <| WithBot.coe_lt_coe.mpr (by exact h3)] @[simp] theorem degree_of_a_ne_zero (ha : P.a ≠ 0) : P.toPoly.degree = 3 := degree_cubic ha @[simp] theorem degree_of_a_ne_zero' (ha : a ≠ 0) : (toPoly ⟨a, b, c, d⟩).degree = 3 := degree_of_a_ne_zero ha theorem degree_of_a_eq_zero (ha : P.a = 0) : P.toPoly.degree ≤ 2 := by simpa only [of_a_eq_zero ha] using degree_quadratic_le theorem degree_of_a_eq_zero' : (toPoly ⟨0, b, c, d⟩).degree ≤ 2 := degree_of_a_eq_zero rfl @[simp] theorem degree_of_b_ne_zero (ha : P.a = 0) (hb : P.b ≠ 0) : P.toPoly.degree = 2 := by rw [of_a_eq_zero ha, degree_quadratic hb] @[simp] theorem degree_of_b_ne_zero' (hb : b ≠ 0) : (toPoly ⟨0, b, c, d⟩).degree = 2 := degree_of_b_ne_zero rfl hb theorem degree_of_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) : P.toPoly.degree ≤ 1 := by simpa only [of_b_eq_zero ha hb] using degree_linear_le theorem degree_of_b_eq_zero' : (toPoly ⟨0, 0, c, d⟩).degree ≤ 1 := degree_of_b_eq_zero rfl rfl @[simp] theorem degree_of_c_ne_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c ≠ 0) : P.toPoly.degree = 1 := by rw [of_b_eq_zero ha hb, degree_linear hc] @[simp] theorem degree_of_c_ne_zero' (hc : c ≠ 0) : (toPoly ⟨0, 0, c, d⟩).degree = 1 := degree_of_c_ne_zero rfl rfl hc theorem degree_of_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.toPoly.degree ≤ 0 := by simpa only [of_c_eq_zero ha hb hc] using degree_C_le theorem degree_of_c_eq_zero' : (toPoly ⟨0, 0, 0, d⟩).degree ≤ 0 := degree_of_c_eq_zero rfl rfl rfl @[simp] theorem degree_of_d_ne_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d ≠ 0) : P.toPoly.degree = 0 := by rw [of_c_eq_zero ha hb hc, degree_C hd] @[simp] theorem degree_of_d_ne_zero' (hd : d ≠ 0) : (toPoly ⟨0, 0, 0, d⟩).degree = 0 := degree_of_d_ne_zero rfl rfl rfl hd @[simp] theorem degree_of_d_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) (hd : P.d = 0) : P.toPoly.degree = ⊥ := by rw [of_d_eq_zero ha hb hc hd, degree_zero] -- @[simp] -- porting note (#10618): simp can prove this theorem degree_of_d_eq_zero' : (⟨0, 0, 0, 0⟩ : Cubic R).toPoly.degree = ⊥ := degree_of_d_eq_zero rfl rfl rfl rfl @[simp] theorem degree_of_zero : (0 : Cubic R).toPoly.degree = ⊥ := degree_of_d_eq_zero' @[simp] theorem natDegree_of_a_ne_zero (ha : P.a ≠ 0) : P.toPoly.natDegree = 3 := natDegree_cubic ha @[simp] theorem natDegree_of_a_ne_zero' (ha : a ≠ 0) : (toPoly ⟨a, b, c, d⟩).natDegree = 3 := natDegree_of_a_ne_zero ha theorem natDegree_of_a_eq_zero (ha : P.a = 0) : P.toPoly.natDegree ≤ 2 := by simpa only [of_a_eq_zero ha] using natDegree_quadratic_le theorem natDegree_of_a_eq_zero' : (toPoly ⟨0, b, c, d⟩).natDegree ≤ 2 := natDegree_of_a_eq_zero rfl @[simp] theorem natDegree_of_b_ne_zero (ha : P.a = 0) (hb : P.b ≠ 0) : P.toPoly.natDegree = 2 := by rw [of_a_eq_zero ha, natDegree_quadratic hb] @[simp] theorem natDegree_of_b_ne_zero' (hb : b ≠ 0) : (toPoly ⟨0, b, c, d⟩).natDegree = 2 := natDegree_of_b_ne_zero rfl hb theorem natDegree_of_b_eq_zero (ha : P.a = 0) (hb : P.b = 0) : P.toPoly.natDegree ≤ 1 := by simpa only [of_b_eq_zero ha hb] using natDegree_linear_le theorem natDegree_of_b_eq_zero' : (toPoly ⟨0, 0, c, d⟩).natDegree ≤ 1 := natDegree_of_b_eq_zero rfl rfl @[simp] theorem natDegree_of_c_ne_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c ≠ 0) : P.toPoly.natDegree = 1 := by rw [of_b_eq_zero ha hb, natDegree_linear hc] @[simp] theorem natDegree_of_c_ne_zero' (hc : c ≠ 0) : (toPoly ⟨0, 0, c, d⟩).natDegree = 1 := natDegree_of_c_ne_zero rfl rfl hc @[simp] theorem natDegree_of_c_eq_zero (ha : P.a = 0) (hb : P.b = 0) (hc : P.c = 0) : P.toPoly.natDegree = 0 := by rw [of_c_eq_zero ha hb hc, natDegree_C] -- @[simp] -- porting note (#10618): simp can prove this theorem natDegree_of_c_eq_zero' : (toPoly ⟨0, 0, 0, d⟩).natDegree = 0 := natDegree_of_c_eq_zero rfl rfl rfl @[simp] theorem natDegree_of_zero : (0 : Cubic R).toPoly.natDegree = 0 := natDegree_of_c_eq_zero' end Degree /-! ### Map across a homomorphism -/ section Map variable [Semiring S] {φ : R →+* S} /-- Map a cubic polynomial across a semiring homomorphism. -/ def map (φ : R →+* S) (P : Cubic R) : Cubic S := ⟨φ P.a, φ P.b, φ P.c, φ P.d⟩ theorem map_toPoly : (map φ P).toPoly = Polynomial.map φ P.toPoly := by simp only [map, toPoly, map_C, map_X, Polynomial.map_add, Polynomial.map_mul, Polynomial.map_pow] end Map end Basic section Roots open Multiset /-! ### Roots over an extension -/ section Extension variable {P : Cubic R} [CommRing R] [CommRing S] {φ : R →+* S} /-- The roots of a cubic polynomial. -/ def roots [IsDomain R] (P : Cubic R) : Multiset R := P.toPoly.roots theorem map_roots [IsDomain S] : (map φ P).roots = (Polynomial.map φ P.toPoly).roots := by rw [roots, map_toPoly] theorem mem_roots_iff [IsDomain R] (h0 : P.toPoly ≠ 0) (x : R) : x ∈ P.roots ↔ P.a * x ^ 3 + P.b * x ^ 2 + P.c * x + P.d = 0 := by rw [roots, mem_roots h0, IsRoot, toPoly] simp only [eval_C, eval_X, eval_add, eval_mul, eval_pow] theorem card_roots_le [IsDomain R] [DecidableEq R] : P.roots.toFinset.card ≤ 3 := by apply (toFinset_card_le P.toPoly.roots).trans by_cases hP : P.toPoly = 0 · exact (card_roots' P.toPoly).trans (by rw [hP, natDegree_zero]; exact zero_le 3) · exact WithBot.coe_le_coe.1 ((card_roots hP).trans degree_cubic_le) end Extension variable {P : Cubic F} [Field F] [Field K] {φ : F →+* K} {x y z : K} /-! ### Roots over a splitting field -/ section Split theorem splits_iff_card_roots (ha : P.a ≠ 0) : Splits φ P.toPoly ↔ Multiset.card (map φ P).roots = 3 := by replace ha : (map φ P).a ≠ 0 := (_root_.map_ne_zero φ).mpr ha nth_rw 1 [← RingHom.id_comp φ] rw [roots, ← splits_map_iff, ← map_toPoly, Polynomial.splits_iff_card_roots, ← ((degree_eq_iff_natDegree_eq <| ne_zero_of_a_ne_zero ha).1 <| degree_of_a_ne_zero ha : _ = 3)] theorem splits_iff_roots_eq_three (ha : P.a ≠ 0) : Splits φ P.toPoly ↔ ∃ x y z : K, (map φ P).roots = {x, y, z} := by rw [splits_iff_card_roots ha, card_eq_three] theorem eq_prod_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : (map φ P).toPoly = C (φ P.a) * (X - C x) * (X - C y) * (X - C z) := by rw [map_toPoly, eq_prod_roots_of_splits <| (splits_iff_roots_eq_three ha).mpr <| Exists.intro x <| Exists.intro y <| Exists.intro z h3, leadingCoeff_of_a_ne_zero ha, ← map_roots, h3] change C (φ P.a) * ((X - C x) ::ₘ (X - C y) ::ₘ {X - C z}).prod = _ rw [prod_cons, prod_cons, prod_singleton, mul_assoc, mul_assoc] theorem eq_sum_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : map φ P = ⟨φ P.a, φ P.a * -(x + y + z), φ P.a * (x * y + x * z + y * z), φ P.a * -(x * y * z)⟩ := by apply_fun @toPoly _ _ · rw [eq_prod_three_roots ha h3, C_mul_prod_X_sub_C_eq] · exact fun P Q ↦ (toPoly_injective P Q).mp theorem b_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.b = φ P.a * -(x + y + z) := by injection eq_sum_three_roots ha h3 theorem c_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.c = φ P.a * (x * y + x * z + y * z) := by injection eq_sum_three_roots ha h3 theorem d_eq_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.d = φ P.a * -(x * y * z) := by injection eq_sum_three_roots ha h3 end Split /-! ### Discriminant over a splitting field -/ section Discriminant /-- The discriminant of a cubic polynomial. -/ def disc {R : Type*} [Ring R] (P : Cubic R) : R := P.b ^ 2 * P.c ^ 2 - 4 * P.a * P.c ^ 3 - 4 * P.b ^ 3 * P.d - 27 * P.a ^ 2 * P.d ^ 2 + 18 * P.a * P.b * P.c * P.d theorem disc_eq_prod_three_roots (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : φ P.disc = (φ P.a * φ P.a * (x - y) * (x - z) * (y - z)) ^ 2 := by simp only [disc, RingHom.map_add, RingHom.map_sub, RingHom.map_mul, map_pow] -- Porting note: Replaced `simp only [RingHom.map_one, map_bit0, map_bit1]` with f4, f18, f27 have f4 : φ 4 = 4 := map_natCast φ 4 have f18 : φ 18 = 18 := map_natCast φ 18 have f27 : φ 27 = 27 := map_natCast φ 27 rw [f4, f18, f27, b_eq_three_roots ha h3, c_eq_three_roots ha h3, d_eq_three_roots ha h3] ring1 theorem disc_ne_zero_iff_roots_ne (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : P.disc ≠ 0 ↔ x ≠ y ∧ x ≠ z ∧ y ≠ z := by rw [← _root_.map_ne_zero φ, disc_eq_prod_three_roots ha h3, pow_two] simp_rw [mul_ne_zero_iff, sub_ne_zero, _root_.map_ne_zero, and_self_iff, and_iff_right ha, and_assoc] theorem disc_ne_zero_iff_roots_nodup (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) : P.disc ≠ 0 ↔ (map φ P).roots.Nodup := by rw [disc_ne_zero_iff_roots_ne ha h3, h3] change _ ↔ (x ::ₘ y ::ₘ {z}).Nodup rw [nodup_cons, nodup_cons, mem_cons, mem_singleton, mem_singleton] simp only [nodup_singleton] tauto theorem card_roots_of_disc_ne_zero [DecidableEq K] (ha : P.a ≠ 0) (h3 : (map φ P).roots = {x, y, z}) (hd : P.disc ≠ 0) : (map φ P).roots.toFinset.card = 3 := by rw [toFinset_card_of_nodup <| (disc_ne_zero_iff_roots_nodup ha h3).mp hd, ← splits_iff_card_roots ha, splits_iff_roots_eq_three ha] exact ⟨x, ⟨y, ⟨z, h3⟩⟩⟩ end Discriminant end Roots end Cubic
Algebra\DirectLimit.lean
/- Copyright (c) 2019 Kenny Lau, Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Chris Hughes, Jujian Zhang -/ import Mathlib.Data.Finset.Order import Mathlib.Algebra.DirectSum.Module import Mathlib.RingTheory.FreeCommRing import Mathlib.RingTheory.Ideal.Maps import Mathlib.RingTheory.Ideal.Quotient import Mathlib.Tactic.SuppressCompilation /-! # Direct limit of modules, abelian groups, rings, and fields. See Atiyah-Macdonald PP.32-33, Matsumura PP.269-270 Generalizes the notion of "union", or "gluing", of incomparable modules over the same ring, or incomparable abelian groups, or rings, or fields. It is constructed as a quotient of the free module (for the module case) or quotient of the free commutative ring (for the ring case) instead of a quotient of the disjoint union so as to make the operations (addition etc.) "computable". ## Main definitions * `DirectedSystem f` * `Module.DirectLimit G f` * `AddCommGroup.DirectLimit G f` * `Ring.DirectLimit G f` -/ suppress_compilation universe u v v' v'' w u₁ open Submodule variable {R : Type u} [Ring R] variable {ι : Type v} variable [Preorder ι] variable (G : ι → Type w) /-- A directed system is a functor from a category (directed poset) to another category. -/ class DirectedSystem (f : ∀ i j, i ≤ j → G i → G j) : Prop where map_self' : ∀ i x h, f i i h x = x map_map' : ∀ {i j k} (hij hjk x), f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x section variable {G} variable (f : ∀ i j, i ≤ j → G i → G j) [DirectedSystem G fun i j h => f i j h] theorem DirectedSystem.map_self i x h : f i i h x = x := DirectedSystem.map_self' i x h theorem DirectedSystem.map_map {i j k} (hij hjk x) : f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x := DirectedSystem.map_map' hij hjk x end namespace Module variable [∀ i, AddCommGroup (G i)] [∀ i, Module R (G i)] variable {G} variable (f : ∀ i j, i ≤ j → G i →ₗ[R] G j) /-- A copy of `DirectedSystem.map_self` specialized to linear maps, as otherwise the `fun i j h ↦ f i j h` can confuse the simplifier. -/ nonrec theorem DirectedSystem.map_self [DirectedSystem G fun i j h => f i j h] (i x h) : f i i h x = x := DirectedSystem.map_self (fun i j h => f i j h) i x h /-- A copy of `DirectedSystem.map_map` specialized to linear maps, as otherwise the `fun i j h ↦ f i j h` can confuse the simplifier. -/ nonrec theorem DirectedSystem.map_map [DirectedSystem G fun i j h => f i j h] {i j k} (hij hjk x) : f j k hjk (f i j hij x) = f i k (le_trans hij hjk) x := DirectedSystem.map_map (fun i j h => f i j h) hij hjk x variable (G) /-- The direct limit of a directed system is the modules glued together along the maps. -/ def DirectLimit [DecidableEq ι] : Type max v w := DirectSum ι G ⧸ (span R <| { a | ∃ (i j : _) (H : i ≤ j) (x : _), DirectSum.lof R ι G i x - DirectSum.lof R ι G j (f i j H x) = a }) namespace DirectLimit section Basic variable [DecidableEq ι] instance addCommGroup : AddCommGroup (DirectLimit G f) := Quotient.addCommGroup _ instance module : Module R (DirectLimit G f) := Quotient.module _ instance inhabited : Inhabited (DirectLimit G f) := ⟨0⟩ instance unique [IsEmpty ι] : Unique (DirectLimit G f) := inferInstanceAs <| Unique (Quotient _) variable (R ι) /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →ₗ[R] DirectLimit G f := (mkQ _).comp <| DirectSum.lof R ι G i variable {R ι G f} @[simp] theorem of_f {i j hij x} : of R ι G f j (f i j hij x) = of R ι G f i x := Eq.symm <| (Submodule.Quotient.eq _).2 <| subset_span ⟨i, j, hij, x, rfl⟩ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of [Nonempty ι] [IsDirected ι (· ≤ ·)] (z : DirectLimit G f) : ∃ i x, of R ι G f i x = z := Nonempty.elim (by infer_instance) fun ind : ι => Quotient.inductionOn' z fun z => DirectSum.induction_on z ⟨ind, 0, LinearMap.map_zero _⟩ (fun i x => ⟨i, x, rfl⟩) fun p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩ => let ⟨k, hik, hjk⟩ := exists_ge_ge i j ⟨k, f i k hik x + f j k hjk y, by rw [LinearMap.map_add, of_f, of_f, ihx, ihy] rfl ⟩ @[elab_as_elim] protected theorem induction_on [Nonempty ι] [IsDirected ι (· ≤ ·)] {C : DirectLimit G f → Prop} (z : DirectLimit G f) (ih : ∀ i x, C (of R ι G f i x)) : C z := let ⟨i, x, h⟩ := exists_of z h ▸ ih i x variable {P : Type u₁} [AddCommGroup P] [Module R P] (g : ∀ i, G i →ₗ[R] P) variable (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) variable (R ι G f) /-- The universal property of the direct limit: maps from the components to another module that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : DirectLimit G f →ₗ[R] P := liftQ _ (DirectSum.toModule R ι P g) (span_le.2 fun a ⟨i, j, hij, x, hx⟩ => by rw [← hx, SetLike.mem_coe, LinearMap.sub_mem_ker_iff, DirectSum.toModule_lof, DirectSum.toModule_lof, Hg]) variable {R ι G f} theorem lift_of {i} (x) : lift R ι G f g Hg (of R ι G f i x) = g i x := DirectSum.toModule_lof R _ _ theorem lift_unique [IsDirected ι (· ≤ ·)] (F : DirectLimit G f →ₗ[R] P) (x) : F x = lift R ι G f (fun i => F.comp <| of R ι G f i) (fun i j hij x => by rw [LinearMap.comp_apply, of_f]; rfl) x := by cases isEmpty_or_nonempty ι · simp_rw [Subsingleton.elim x 0, _root_.map_zero] · exact DirectLimit.induction_on x fun i x => by rw [lift_of]; rfl lemma lift_injective [IsDirected ι (· ≤ ·)] (injective : ∀ i, Function.Injective <| g i) : Function.Injective (lift R ι G f g Hg) := by cases isEmpty_or_nonempty ι · apply Function.injective_of_subsingleton simp_rw [injective_iff_map_eq_zero] at injective ⊢ intros z hz induction' z using DirectLimit.induction_on with _ g rw [lift_of] at hz rw [injective _ g hz, _root_.map_zero] section functorial variable {G' : ι → Type v'} [∀ i, AddCommGroup (G' i)] [∀ i, Module R (G' i)] variable {f' : ∀ i j, i ≤ j → G' i →ₗ[R] G' j} variable {G'' : ι → Type v''} [∀ i, AddCommGroup (G'' i)] [∀ i, Module R (G'' i)] variable {f'' : ∀ i j, i ≤ j → G'' i →ₗ[R] G'' j} /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of linear maps `gᵢ : Gᵢ ⟶ G'ᵢ` such that `g ∘ f = f' ∘ g` induces a linear map `lim G ⟶ lim G'`. -/ def map (g : (i : ι) → G i →ₗ[R] G' i) (hg : ∀ i j h, g j ∘ₗ f i j h = f' i j h ∘ₗ g i) : DirectLimit G f →ₗ[R] DirectLimit G' f' := lift _ _ _ _ (fun i ↦ of _ _ _ _ _ ∘ₗ g i) fun i j h g ↦ by cases isEmpty_or_nonempty ι · subsingleton · have eq1 := LinearMap.congr_fun (hg i j h) g simp only [LinearMap.coe_comp, Function.comp_apply] at eq1 ⊢ rw [eq1, of_f] @[simp] lemma map_apply_of (g : (i : ι) → G i →ₗ[R] G' i) (hg : ∀ i j h, g j ∘ₗ f i j h = f' i j h ∘ₗ g i) {i : ι} (x : G i) : map g hg (of _ _ G f _ x) = of R ι G' f' i (g i x) := lift_of _ _ _ @[simp] lemma map_id [IsDirected ι (· ≤ ·)] : map (fun i ↦ LinearMap.id) (fun _ _ _ ↦ rfl) = LinearMap.id (R := R) (M := DirectLimit G f) := DFunLike.ext _ _ fun x ↦ (isEmpty_or_nonempty ι).elim (by subsingleton) fun _ ↦ x.induction_on fun i g ↦ by simp lemma map_comp [IsDirected ι (· ≤ ·)] (g₁ : (i : ι) → G i →ₗ[R] G' i) (g₂ : (i : ι) → G' i →ₗ[R] G'' i) (hg₁ : ∀ i j h, g₁ j ∘ₗ f i j h = f' i j h ∘ₗ g₁ i) (hg₂ : ∀ i j h, g₂ j ∘ₗ f' i j h = f'' i j h ∘ₗ g₂ i) : (map g₂ hg₂ ∘ₗ map g₁ hg₁ : DirectLimit G f →ₗ[R] DirectLimit G'' f'') = (map (fun i ↦ g₂ i ∘ₗ g₁ i) fun i j h ↦ by rw [LinearMap.comp_assoc, hg₁ i, ← LinearMap.comp_assoc, hg₂ i, LinearMap.comp_assoc] : DirectLimit G f →ₗ[R] DirectLimit G'' f'') := DFunLike.ext _ _ fun x ↦ (isEmpty_or_nonempty ι).elim (by subsingleton) fun _ ↦ x.induction_on fun i g ↦ by simp open LinearEquiv LinearMap in /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of equivalences `eᵢ : Gᵢ ≅ G'ᵢ` such that `e ∘ f = f' ∘ e` induces an equivalence `lim G ≅ lim G'`. -/ def congr [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃ₗ[R] G' i) (he : ∀ i j h, e j ∘ₗ f i j h = f' i j h ∘ₗ e i) : DirectLimit G f ≃ₗ[R] DirectLimit G' f' := LinearEquiv.ofLinear (map (e ·) he) (map (fun i ↦ (e i).symm) fun i j h ↦ by rw [toLinearMap_symm_comp_eq, ← comp_assoc, he i, comp_assoc, comp_coe, symm_trans_self, refl_toLinearMap, comp_id]) (by simp [map_comp]) (by simp [map_comp]) lemma congr_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃ₗ[R] G' i) (he : ∀ i j h, e j ∘ₗ f i j h = f' i j h ∘ₗ e i) {i : ι} (g : G i) : congr e he (of _ _ G f i g) = of _ _ G' f' i (e i g) := map_apply_of _ he _ open LinearEquiv LinearMap in lemma congr_symm_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃ₗ[R] G' i) (he : ∀ i j h, e j ∘ₗ f i j h = f' i j h ∘ₗ e i) {i : ι} (g : G' i) : (congr e he).symm (of _ _ G' f' i g) = of _ _ G f i ((e i).symm g) := map_apply_of _ (fun i j h ↦ by rw [toLinearMap_symm_comp_eq, ← comp_assoc, he i, comp_assoc, comp_coe, symm_trans_self, refl_toLinearMap, comp_id]) _ end functorial end Basic section Totalize open Classical in /-- `totalize G f i j` is a linear map from `G i` to `G j`, for *every* `i` and `j`. If `i ≤ j`, then it is the map `f i j` that comes with the directed system `G`, and otherwise it is the zero map. -/ noncomputable def totalize (i j) : G i →ₗ[R] G j := if h : i ≤ j then f i j h else 0 variable {G f} theorem totalize_of_le {i j} (h : i ≤ j) : totalize G f i j = f i j h := dif_pos h theorem totalize_of_not_le {i j} (h : ¬i ≤ j) : totalize G f i j = 0 := dif_neg h end Totalize variable [DecidableEq ι] [DirectedSystem G fun i j h => f i j h] variable {G f} theorem toModule_totalize_of_le [∀ i (k : G i), Decidable (k ≠ 0)] {x : DirectSum ι G} {i j : ι} (hij : i ≤ j) (hx : ∀ k ∈ x.support, k ≤ i) : DirectSum.toModule R ι (G j) (fun k => totalize G f k j) x = f i j hij (DirectSum.toModule R ι (G i) (fun k => totalize G f k i) x) := by rw [← @DFinsupp.sum_single ι G _ _ _ x] unfold DFinsupp.sum simp only [map_sum] refine Finset.sum_congr rfl fun k hk => ?_ rw [DirectSum.single_eq_lof R k (x k), DirectSum.toModule_lof, DirectSum.toModule_lof, totalize_of_le (hx k hk), totalize_of_le (le_trans (hx k hk) hij), DirectedSystem.map_map] theorem of.zero_exact_aux [∀ i (k : G i), Decidable (k ≠ 0)] [Nonempty ι] [IsDirected ι (· ≤ ·)] {x : DirectSum ι G} (H : (Submodule.Quotient.mk x : DirectLimit G f) = (0 : DirectLimit G f)) : ∃ j, (∀ k ∈ x.support, k ≤ j) ∧ DirectSum.toModule R ι (G j) (fun i => totalize G f i j) x = (0 : G j) := Nonempty.elim (by infer_instance) fun ind : ι => span_induction ((Quotient.mk_eq_zero _).1 H) (fun x ⟨i, j, hij, y, hxy⟩ => let ⟨k, hik, hjk⟩ := exists_ge_ge i j ⟨k, by subst hxy constructor · intro i0 hi0 rw [DFinsupp.mem_support_iff, DirectSum.sub_apply, ← DirectSum.single_eq_lof, ← DirectSum.single_eq_lof, DFinsupp.single_apply, DFinsupp.single_apply] at hi0 split_ifs at hi0 with hi hj hj · rwa [hi] at hik · rwa [hi] at hik · rwa [hj] at hjk exfalso apply hi0 rw [sub_zero] simp [LinearMap.map_sub, totalize_of_le, hik, hjk, DirectedSystem.map_map, DirectSum.apply_eq_component, DirectSum.component.of]⟩) ⟨ind, fun _ h => (Finset.not_mem_empty _ h).elim, LinearMap.map_zero _⟩ (fun x y ⟨i, hi, hxi⟩ ⟨j, hj, hyj⟩ => let ⟨k, hik, hjk⟩ := exists_ge_ge i j ⟨k, fun l hl => (Finset.mem_union.1 (DFinsupp.support_add hl)).elim (fun hl => le_trans (hi _ hl) hik) fun hl => le_trans (hj _ hl) hjk, by -- Porting note: this had been -- simp [LinearMap.map_add, hxi, hyj, toModule_totalize_of_le hik hi, -- toModule_totalize_of_le hjk hj] simp only [map_add] rw [toModule_totalize_of_le hik hi, toModule_totalize_of_le hjk hj] simp [hxi, hyj]⟩) fun a x ⟨i, hi, hxi⟩ => ⟨i, fun k hk => hi k (DirectSum.support_smul _ _ hk), by simp [LinearMap.map_smul, hxi]⟩ open Classical in /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact [IsDirected ι (· ≤ ·)] {i x} (H : of R ι G f i x = 0) : ∃ j hij, f i j hij x = (0 : G j) := haveI : Nonempty ι := ⟨i⟩ let ⟨j, hj, hxj⟩ := of.zero_exact_aux H if hx0 : x = 0 then ⟨i, le_rfl, by simp [hx0]⟩ else have hij : i ≤ j := hj _ <| by simp [DirectSum.apply_eq_component, hx0] ⟨j, hij, by -- Porting note: this had been -- simpa [totalize_of_le hij] using hxj simp only [DirectSum.toModule_lof] at hxj rwa [totalize_of_le hij] at hxj⟩ end DirectLimit end Module namespace AddCommGroup variable [∀ i, AddCommGroup (G i)] /-- The direct limit of a directed system is the abelian groups glued together along the maps. -/ def DirectLimit [DecidableEq ι] (f : ∀ i j, i ≤ j → G i →+ G j) : Type _ := @Module.DirectLimit ℤ _ ι _ G _ _ (fun i j hij => (f i j hij).toIntLinearMap) _ namespace DirectLimit variable (f : ∀ i j, i ≤ j → G i →+ G j) protected theorem directedSystem [h : DirectedSystem G fun i j h => f i j h] : DirectedSystem G fun i j hij => (f i j hij).toIntLinearMap := h attribute [local instance] DirectLimit.directedSystem variable [DecidableEq ι] instance : AddCommGroup (DirectLimit G f) := Module.DirectLimit.addCommGroup G fun i j hij => (f i j hij).toIntLinearMap instance : Inhabited (DirectLimit G f) := ⟨0⟩ instance [IsEmpty ι] : Unique (DirectLimit G f) := Module.DirectLimit.unique _ _ /-- The canonical map from a component to the direct limit. -/ def of (i) : G i →+ DirectLimit G f := (Module.DirectLimit.of ℤ ι G (fun i j hij => (f i j hij).toIntLinearMap) i).toAddMonoidHom variable {G f} @[simp] theorem of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := Module.DirectLimit.of_f @[elab_as_elim] protected theorem induction_on [Nonempty ι] [IsDirected ι (· ≤ ·)] {C : DirectLimit G f → Prop} (z : DirectLimit G f) (ih : ∀ i x, C (of G f i x)) : C z := Module.DirectLimit.induction_on z ih /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact [IsDirected ι (· ≤ ·)] [DirectedSystem G fun i j h => f i j h] (i x) (h : of G f i x = 0) : ∃ j hij, f i j hij x = 0 := Module.DirectLimit.of.zero_exact h variable (P : Type u₁) [AddCommGroup P] variable (g : ∀ i, G i →+ P) variable (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) variable (G f) /-- The universal property of the direct limit: maps from the components to another abelian group that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : DirectLimit G f →+ P := (Module.DirectLimit.lift ℤ ι G (fun i j hij => (f i j hij).toIntLinearMap) (fun i => (g i).toIntLinearMap) Hg).toAddMonoidHom variable {G f} @[simp] theorem lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := Module.DirectLimit.lift_of -- Note: had to make these arguments explicit #8386 (f := (fun i j hij => (f i j hij).toIntLinearMap)) (fun i => (g i).toIntLinearMap) Hg x theorem lift_unique [IsDirected ι (· ≤ ·)] (F : DirectLimit G f →+ P) (x) : F x = lift G f P (fun i => F.comp (of G f i)) (fun i j hij x => by simp) x := by cases isEmpty_or_nonempty ι · simp_rw [Subsingleton.elim x 0, _root_.map_zero] · exact DirectLimit.induction_on x fun i x => by simp lemma lift_injective [IsDirected ι (· ≤ ·)] (injective : ∀ i, Function.Injective <| g i) : Function.Injective (lift G f P g Hg) := by cases isEmpty_or_nonempty ι · apply Function.injective_of_subsingleton simp_rw [injective_iff_map_eq_zero] at injective ⊢ intros z hz induction' z using DirectLimit.induction_on with _ g rw [lift_of] at hz rw [injective _ g hz, _root_.map_zero] section functorial variable {G' : ι → Type v'} [∀ i, AddCommGroup (G' i)] variable {f' : ∀ i j, i ≤ j → G' i →+ G' j} variable {G'' : ι → Type v''} [∀ i, AddCommGroup (G'' i)] variable {f'' : ∀ i j, i ≤ j → G'' i →+ G'' j} /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of group homomorphisms `gᵢ : Gᵢ ⟶ G'ᵢ` such that `g ∘ f = f' ∘ g` induces a group homomorphism `lim G ⟶ lim G'`. -/ def map (g : (i : ι) → G i →+ G' i) (hg : ∀ i j h, (g j).comp (f i j h) = (f' i j h).comp (g i)) : DirectLimit G f →+ DirectLimit G' f' := lift _ _ _ (fun i ↦ (of _ _ _).comp (g i)) fun i j h g ↦ by cases isEmpty_or_nonempty ι · subsingleton · have eq1 := DFunLike.congr_fun (hg i j h) g simp only [AddMonoidHom.coe_comp, Function.comp_apply] at eq1 ⊢ rw [eq1, of_f] @[simp] lemma map_apply_of (g : (i : ι) → G i →+ G' i) (hg : ∀ i j h, (g j).comp (f i j h) = (f' i j h).comp (g i)) {i : ι} (x : G i) : map g hg (of G f _ x) = of G' f' i (g i x) := lift_of _ _ _ _ _ @[simp] lemma map_id [IsDirected ι (· ≤ ·)] : map (fun i ↦ AddMonoidHom.id _) (fun _ _ _ ↦ rfl) = AddMonoidHom.id (DirectLimit G f) := DFunLike.ext _ _ fun x ↦ (isEmpty_or_nonempty ι).elim (by subsingleton) fun _ ↦ x.induction_on fun i g ↦ by simp lemma map_comp [IsDirected ι (· ≤ ·)] (g₁ : (i : ι) → G i →+ G' i) (g₂ : (i : ι) → G' i →+ G'' i) (hg₁ : ∀ i j h, (g₁ j).comp (f i j h) = (f' i j h).comp (g₁ i)) (hg₂ : ∀ i j h, (g₂ j).comp (f' i j h) = (f'' i j h).comp (g₂ i)) : ((map g₂ hg₂).comp (map g₁ hg₁) : DirectLimit G f →+ DirectLimit G'' f'') = (map (fun i ↦ (g₂ i).comp (g₁ i)) fun i j h ↦ by rw [AddMonoidHom.comp_assoc, hg₁ i, ← AddMonoidHom.comp_assoc, hg₂ i, AddMonoidHom.comp_assoc] : DirectLimit G f →+ DirectLimit G'' f'') := DFunLike.ext _ _ fun x ↦ (isEmpty_or_nonempty ι).elim (by subsingleton) fun _ ↦ x.induction_on fun i g ↦ by simp /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of equivalences `eᵢ : Gᵢ ≅ G'ᵢ` such that `e ∘ f = f' ∘ e` induces an equivalence `lim G ⟶ lim G'`. -/ def congr [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+ G' i) (he : ∀ i j h, (e j).toAddMonoidHom.comp (f i j h) = (f' i j h).comp (e i)) : DirectLimit G f ≃+ DirectLimit G' f' := AddMonoidHom.toAddEquiv (map (e ·) he) (map (fun i ↦ (e i).symm) fun i j h ↦ DFunLike.ext _ _ fun x ↦ by have eq1 := DFunLike.congr_fun (he i j h) ((e i).symm x) simp only [AddMonoidHom.coe_comp, AddEquiv.coe_toAddMonoidHom, Function.comp_apply, AddMonoidHom.coe_coe, AddEquiv.apply_symm_apply] at eq1 ⊢ simp [← eq1, of_f]) (by rw [map_comp]; convert map_id <;> aesop) (by rw [map_comp]; convert map_id <;> aesop) lemma congr_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+ G' i) (he : ∀ i j h, (e j).toAddMonoidHom.comp (f i j h) = (f' i j h).comp (e i)) {i : ι} (g : G i) : congr e he (of G f i g) = of G' f' i (e i g) := map_apply_of _ he _ lemma congr_symm_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+ G' i) (he : ∀ i j h, (e j).toAddMonoidHom.comp (f i j h) = (f' i j h).comp (e i)) {i : ι} (g : G' i) : (congr e he).symm (of G' f' i g) = of G f i ((e i).symm g) := by simp only [congr, AddMonoidHom.toAddEquiv_symm_apply, map_apply_of, AddMonoidHom.coe_coe] end functorial end DirectLimit end AddCommGroup namespace Ring variable [∀ i, CommRing (G i)] section variable (f : ∀ i j, i ≤ j → G i → G j) open FreeCommRing /-- The direct limit of a directed system is the rings glued together along the maps. -/ def DirectLimit : Type max v w := FreeCommRing (Σi, G i) ⧸ Ideal.span { a | (∃ i j H x, of (⟨j, f i j H x⟩ : Σi, G i) - of ⟨i, x⟩ = a) ∨ (∃ i, of (⟨i, 1⟩ : Σi, G i) - 1 = a) ∨ (∃ i x y, of (⟨i, x + y⟩ : Σi, G i) - (of ⟨i, x⟩ + of ⟨i, y⟩) = a) ∨ ∃ i x y, of (⟨i, x * y⟩ : Σi, G i) - of ⟨i, x⟩ * of ⟨i, y⟩ = a } namespace DirectLimit instance commRing : CommRing (DirectLimit G f) := Ideal.Quotient.commRing _ instance ring : Ring (DirectLimit G f) := CommRing.toRing -- Porting note: Added a `Zero` instance to get rid of `0` errors. instance zero : Zero (DirectLimit G f) := by unfold DirectLimit exact ⟨0⟩ instance : Inhabited (DirectLimit G f) := ⟨0⟩ /-- The canonical map from a component to the direct limit. -/ nonrec def of (i) : G i →+* DirectLimit G f := RingHom.mk' { toFun := fun x => Ideal.Quotient.mk _ (of (⟨i, x⟩ : Σi, G i)) map_one' := Ideal.Quotient.eq.2 <| subset_span <| Or.inr <| Or.inl ⟨i, rfl⟩ map_mul' := fun x y => Ideal.Quotient.eq.2 <| subset_span <| Or.inr <| Or.inr <| Or.inr ⟨i, x, y, rfl⟩ } fun x y => Ideal.Quotient.eq.2 <| subset_span <| Or.inr <| Or.inr <| Or.inl ⟨i, x, y, rfl⟩ variable {G f} -- Porting note: the @[simp] attribute would trigger a `simpNF` linter error: -- failed to synthesize CommMonoidWithZero (Ring.DirectLimit G f) theorem of_f {i j} (hij) (x) : of G f j (f i j hij x) = of G f i x := Ideal.Quotient.eq.2 <| subset_span <| Or.inl ⟨i, j, hij, x, rfl⟩ /-- Every element of the direct limit corresponds to some element in some component of the directed system. -/ theorem exists_of [Nonempty ι] [IsDirected ι (· ≤ ·)] (z : DirectLimit G f) : ∃ i x, of G f i x = z := Nonempty.elim (by infer_instance) fun ind : ι => Quotient.inductionOn' z fun x => FreeAbelianGroup.induction_on x ⟨ind, 0, (of _ _ ind).map_zero⟩ (fun s => Multiset.induction_on s ⟨ind, 1, (of _ _ ind).map_one⟩ fun a s ih => let ⟨i, x⟩ := a let ⟨j, y, hs⟩ := ih let ⟨k, hik, hjk⟩ := exists_ge_ge i j ⟨k, f i k hik x * f j k hjk y, by rw [(of G f k).map_mul, of_f, of_f, hs] /- porting note: In Lean3, from here, this was `by refl`. I have added the lemma `FreeCommRing.of_cons` to fix this proof. -/ apply congr_arg Quotient.mk'' symm apply FreeCommRing.of_cons⟩) (fun s ⟨i, x, ih⟩ => ⟨i, -x, by rw [(of G _ _).map_neg, ih] rfl⟩) fun p q ⟨i, x, ihx⟩ ⟨j, y, ihy⟩ => let ⟨k, hik, hjk⟩ := exists_ge_ge i j ⟨k, f i k hik x + f j k hjk y, by rw [(of _ _ _).map_add, of_f, of_f, ihx, ihy]; rfl⟩ section open Polynomial variable {f' : ∀ i j, i ≤ j → G i →+* G j} nonrec theorem Polynomial.exists_of [Nonempty ι] [IsDirected ι (· ≤ ·)] (q : Polynomial (DirectLimit G fun i j h => f' i j h)) : ∃ i p, Polynomial.map (of G (fun i j h => f' i j h) i) p = q := Polynomial.induction_on q (fun z => let ⟨i, x, h⟩ := exists_of z ⟨i, C x, by rw [map_C, h]⟩) (fun q₁ q₂ ⟨i₁, p₁, ih₁⟩ ⟨i₂, p₂, ih₂⟩ => let ⟨i, h1, h2⟩ := exists_ge_ge i₁ i₂ ⟨i, p₁.map (f' i₁ i h1) + p₂.map (f' i₂ i h2), by rw [Polynomial.map_add, map_map, map_map, ← ih₁, ← ih₂] congr 2 <;> ext x <;> simp_rw [RingHom.comp_apply, of_f]⟩) fun n z _ => let ⟨i, x, h⟩ := exists_of z ⟨i, C x * X ^ (n + 1), by rw [Polynomial.map_mul, map_C, h, Polynomial.map_pow, map_X]⟩ end @[elab_as_elim] theorem induction_on [Nonempty ι] [IsDirected ι (· ≤ ·)] {C : DirectLimit G f → Prop} (z : DirectLimit G f) (ih : ∀ i x, C (of G f i x)) : C z := let ⟨i, x, hx⟩ := exists_of z hx ▸ ih i x section OfZeroExact variable (f' : ∀ i j, i ≤ j → G i →+* G j) variable [DirectedSystem G fun i j h => f' i j h] variable (G f) theorem of.zero_exact_aux2 {x : FreeCommRing (Σi, G i)} {s t} [DecidablePred (· ∈ s)] [DecidablePred (· ∈ t)] (hxs : IsSupported x s) {j k} (hj : ∀ z : Σi, G i, z ∈ s → z.1 ≤ j) (hk : ∀ z : Σi, G i, z ∈ t → z.1 ≤ k) (hjk : j ≤ k) (hst : s ⊆ t) : f' j k hjk (lift (fun ix : s => f' ix.1.1 j (hj ix ix.2) ix.1.2) (restriction s x)) = lift (fun ix : t => f' ix.1.1 k (hk ix ix.2) ix.1.2) (restriction t x) := by refine Subring.InClosure.recOn hxs ?_ ?_ ?_ ?_ · rw [(restriction _).map_one, (FreeCommRing.lift _).map_one, (f' j k hjk).map_one, (restriction _).map_one, (FreeCommRing.lift _).map_one] · -- Porting note: Lean 3 had `(FreeCommRing.lift _).map_neg` but I needed to replace it with -- `RingHom.map_neg` to get the rewrite to compile rw [(restriction _).map_neg, (restriction _).map_one, RingHom.map_neg, (FreeCommRing.lift _).map_one, (f' j k hjk).map_neg, (f' j k hjk).map_one, -- Porting note: similarly here I give strictly less information (restriction _).map_neg, (restriction _).map_one, RingHom.map_neg, (FreeCommRing.lift _).map_one] · rintro _ ⟨p, hps, rfl⟩ n ih rw [(restriction _).map_mul, (FreeCommRing.lift _).map_mul, (f' j k hjk).map_mul, ih, (restriction _).map_mul, (FreeCommRing.lift _).map_mul, restriction_of, dif_pos hps, lift_of, restriction_of, dif_pos (hst hps), lift_of] dsimp only -- Porting note: Lean 3 could get away with far fewer hints for inputs in the line below have := DirectedSystem.map_map (fun i j h => f' i j h) (hj p hps) hjk rw [this] · rintro x y ihx ihy rw [(restriction _).map_add, (FreeCommRing.lift _).map_add, (f' j k hjk).map_add, ihx, ihy, (restriction _).map_add, (FreeCommRing.lift _).map_add] variable {G f f'} theorem of.zero_exact_aux [Nonempty ι] [IsDirected ι (· ≤ ·)] {x : FreeCommRing (Σi, G i)} (H : (Ideal.Quotient.mk _ x : DirectLimit G fun i j h => f' i j h) = (0 : DirectLimit G fun i j h => f' i j h)) : ∃ j s, ∃ H : ∀ k : Σ i, G i, k ∈ s → k.1 ≤ j, IsSupported x s ∧ ∀ [DecidablePred (· ∈ s)], lift (fun ix : s => f' ix.1.1 j (H ix ix.2) ix.1.2) (restriction s x) = (0 : G j) := by have := Classical.decEq refine span_induction (Ideal.Quotient.eq_zero_iff_mem.1 H) ?_ ?_ ?_ ?_ · rintro x (⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩) · refine ⟨j, {⟨i, x⟩, ⟨j, f' i j hij x⟩}, ?_, isSupported_sub (isSupported_of.2 <| Or.inr (Set.mem_singleton _)) (isSupported_of.2 <| Or.inl rfl), fun [_] => ?_⟩ · rintro k (rfl | ⟨rfl | _⟩) · exact hij · rfl · rw [(restriction _).map_sub, RingHom.map_sub, restriction_of, dif_pos, restriction_of, dif_pos, lift_of, lift_of] on_goal 1 => dsimp only have := DirectedSystem.map_map (fun i j h => f' i j h) hij (le_refl j : j ≤ j) rw [this] · exact sub_self _ exacts [Or.inl rfl, Or.inr rfl] · refine ⟨i, {⟨i, 1⟩}, ?_, isSupported_sub (isSupported_of.2 (Set.mem_singleton _)) isSupported_one, fun [_] => ?_⟩ · rintro k (rfl | h) rfl -- Porting note: the Lean3 proof contained `rw [restriction_of]`, but this -- lemma does not seem to work here · rw [RingHom.map_sub, RingHom.map_sub] erw [lift_of, dif_pos rfl, RingHom.map_one, lift_of, RingHom.map_one, sub_self] · refine ⟨i, {⟨i, x + y⟩, ⟨i, x⟩, ⟨i, y⟩}, ?_, isSupported_sub (isSupported_of.2 <| Or.inl rfl) (isSupported_add (isSupported_of.2 <| Or.inr <| Or.inl rfl) (isSupported_of.2 <| Or.inr <| Or.inr (Set.mem_singleton _))), fun [_] => ?_⟩ · rintro k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩) <;> rfl · rw [(restriction _).map_sub, (restriction _).map_add, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, RingHom.map_sub, (FreeCommRing.lift _).map_add, lift_of, lift_of, lift_of] on_goal 1 => dsimp only rw [(f' i i _).map_add] · exact sub_self _ all_goals tauto · refine ⟨i, {⟨i, x * y⟩, ⟨i, x⟩, ⟨i, y⟩}, ?_, isSupported_sub (isSupported_of.2 <| Or.inl rfl) (isSupported_mul (isSupported_of.2 <| Or.inr <| Or.inl rfl) (isSupported_of.2 <| Or.inr <| Or.inr (Set.mem_singleton _))), fun [_] => ?_⟩ · rintro k (rfl | ⟨rfl | ⟨rfl | hk⟩⟩) <;> rfl · rw [(restriction _).map_sub, (restriction _).map_mul, restriction_of, restriction_of, restriction_of, dif_pos, dif_pos, dif_pos, RingHom.map_sub, (FreeCommRing.lift _).map_mul, lift_of, lift_of, lift_of] on_goal 1 => dsimp only rw [(f' i i _).map_mul] · exact sub_self _ all_goals tauto -- Porting note: was --exacts [sub_self _, Or.inl rfl, Or.inr (Or.inr rfl), Or.inr (Or.inl rfl)] · refine Nonempty.elim (by infer_instance) fun ind : ι => ?_ refine ⟨ind, ∅, fun _ => False.elim, isSupported_zero, fun [_] => ?_⟩ -- Porting note: `RingHom.map_zero` was `(restriction _).map_zero` rw [RingHom.map_zero, (FreeCommRing.lift _).map_zero] · intro x y ⟨i, s, hi, hxs, ihs⟩ ⟨j, t, hj, hyt, iht⟩ obtain ⟨k, hik, hjk⟩ := exists_ge_ge i j have : ∀ z : Σi, G i, z ∈ s ∪ t → z.1 ≤ k := by rintro z (hz | hz) · exact le_trans (hi z hz) hik · exact le_trans (hj z hz) hjk refine ⟨k, s ∪ t, this, isSupported_add (isSupported_upwards hxs Set.subset_union_left) (isSupported_upwards hyt Set.subset_union_right), fun [_] => ?_⟩ -- Porting note: was `(restriction _).map_add` classical rw [RingHom.map_add, (FreeCommRing.lift _).map_add, ← of.zero_exact_aux2 G f' hxs hi this hik Set.subset_union_left, ← of.zero_exact_aux2 G f' hyt hj this hjk Set.subset_union_right, ihs, (f' i k hik).map_zero, iht, (f' j k hjk).map_zero, zero_add] · rintro x y ⟨j, t, hj, hyt, iht⟩ rw [smul_eq_mul] rcases exists_finset_support x with ⟨s, hxs⟩ rcases (s.image Sigma.fst).exists_le with ⟨i, hi⟩ obtain ⟨k, hik, hjk⟩ := exists_ge_ge i j have : ∀ z : Σi, G i, z ∈ ↑s ∪ t → z.1 ≤ k := by rintro z (hz | hz) exacts [(hi z.1 <| Finset.mem_image.2 ⟨z, hz, rfl⟩).trans hik, (hj z hz).trans hjk] refine ⟨k, ↑s ∪ t, this, isSupported_mul (isSupported_upwards hxs Set.subset_union_left) (isSupported_upwards hyt Set.subset_union_right), fun [_] => ?_⟩ -- Porting note: RingHom.map_mul was `(restriction _).map_mul` classical rw [RingHom.map_mul, (FreeCommRing.lift _).map_mul, ← of.zero_exact_aux2 G f' hyt hj this hjk Set.subset_union_right, iht, (f' j k hjk).map_zero, mul_zero] /-- A component that corresponds to zero in the direct limit is already zero in some bigger module in the directed system. -/ theorem of.zero_exact [IsDirected ι (· ≤ ·)] {i x} (hix : of G (fun i j h => f' i j h) i x = 0) : ∃ (j : _) (hij : i ≤ j), f' i j hij x = 0 := by haveI : Nonempty ι := ⟨i⟩ let ⟨j, s, H, hxs, hx⟩ := of.zero_exact_aux hix have hixs : (⟨i, x⟩ : Σi, G i) ∈ s := isSupported_of.1 hxs classical specialize @hx _ exact ⟨j, H ⟨i, x⟩ hixs, by classical rw [restriction_of, dif_pos hixs, lift_of] at hx; exact hx⟩ end OfZeroExact variable (f' : ∀ i j, i ≤ j → G i →+* G j) /-- If the maps in the directed system are injective, then the canonical maps from the components to the direct limits are injective. -/ theorem of_injective [IsDirected ι (· ≤ ·)] [DirectedSystem G fun i j h => f' i j h] (hf : ∀ i j hij, Function.Injective (f' i j hij)) (i) : Function.Injective (of G (fun i j h => f' i j h) i) := by suffices ∀ x, of G (fun i j h => f' i j h) i x = 0 → x = 0 by intro x y hxy rw [← sub_eq_zero] apply this rw [(of G _ i).map_sub, hxy, sub_self] intro x hx rcases of.zero_exact hx with ⟨j, hij, hfx⟩ apply hf i j hij rw [hfx, (f' i j hij).map_zero] variable (P : Type u₁) [CommRing P] variable (g : ∀ i, G i →+* P) variable (Hg : ∀ i j hij x, g j (f i j hij x) = g i x) open FreeCommRing variable (G f) /-- The universal property of the direct limit: maps from the components to another ring that respect the directed system structure (i.e. make some diagram commute) give rise to a unique map out of the direct limit. -/ def lift : DirectLimit G f →+* P := Ideal.Quotient.lift _ (FreeCommRing.lift fun x : Σi, G i => g x.1 x.2) (by suffices Ideal.span _ ≤ Ideal.comap (FreeCommRing.lift fun x : Σi : ι, G i => g x.fst x.snd) ⊥ by intro x hx exact (mem_bot P).1 (this hx) rw [Ideal.span_le] intro x hx rw [SetLike.mem_coe, Ideal.mem_comap, mem_bot] rcases hx with (⟨i, j, hij, x, rfl⟩ | ⟨i, rfl⟩ | ⟨i, x, y, rfl⟩ | ⟨i, x, y, rfl⟩) <;> simp only [RingHom.map_sub, lift_of, Hg, RingHom.map_one, RingHom.map_add, RingHom.map_mul, (g i).map_one, (g i).map_add, (g i).map_mul, sub_self]) variable {G f} -- Porting note: the @[simp] attribute would trigger a `simpNF` linter error: -- failed to synthesize CommMonoidWithZero (Ring.DirectLimit G f) theorem lift_of (i x) : lift G f P g Hg (of G f i x) = g i x := FreeCommRing.lift_of _ _ theorem lift_unique [IsDirected ι (· ≤ ·)] (F : DirectLimit G f →+* P) (x) : F x = lift G f P (fun i => F.comp <| of G f i) (fun i j hij x => by simp [of_f]) x := by cases isEmpty_or_nonempty ι · apply DFunLike.congr_fun apply Ideal.Quotient.ringHom_ext refine FreeCommRing.hom_ext fun ⟨i, _⟩ ↦ ?_ exact IsEmpty.elim' inferInstance i · exact DirectLimit.induction_on x fun i x => by simp [lift_of] lemma lift_injective [Nonempty ι] [IsDirected ι (· ≤ ·)] (injective : ∀ i, Function.Injective <| g i) : Function.Injective (lift G f P g Hg) := by simp_rw [injective_iff_map_eq_zero] at injective ⊢ intros z hz induction' z using DirectLimit.induction_on with _ g rw [lift_of] at hz rw [injective _ g hz, _root_.map_zero] section functorial variable {f : ∀ i j, i ≤ j → G i →+* G j} variable {G' : ι → Type v'} [∀ i, CommRing (G' i)] variable {f' : ∀ i j, i ≤ j → G' i →+* G' j} variable {G'' : ι → Type v''} [∀ i, CommRing (G'' i)] variable {f'' : ∀ i j, i ≤ j → G'' i →+* G'' j} /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of ring homomorphisms `gᵢ : Gᵢ ⟶ G'ᵢ` such that `g ∘ f = f' ∘ g` induces a ring homomorphism `lim G ⟶ lim G'`. -/ def map (g : (i : ι) → G i →+* G' i) (hg : ∀ i j h, (g j).comp (f i j h) = (f' i j h).comp (g i)) : DirectLimit G (fun _ _ h ↦ f _ _ h) →+* DirectLimit G' fun _ _ h ↦ f' _ _ h := lift _ _ _ (fun i ↦ (of _ _ _).comp (g i)) fun i j h g ↦ by have eq1 := DFunLike.congr_fun (hg i j h) g simp only [RingHom.coe_comp, Function.comp_apply] at eq1 ⊢ rw [eq1, of_f] @[simp] lemma map_apply_of (g : (i : ι) → G i →+* G' i) (hg : ∀ i j h, (g j).comp (f i j h) = (f' i j h).comp (g i)) {i : ι} (x : G i) : map g hg (of G _ _ x) = of G' (fun _ _ h ↦ f' _ _ h) i (g i x) := lift_of _ _ _ _ _ variable [Nonempty ι] @[simp] lemma map_id [IsDirected ι (· ≤ ·)] : map (fun i ↦ RingHom.id _) (fun _ _ _ ↦ rfl) = RingHom.id (DirectLimit G fun _ _ h ↦ f _ _ h) := DFunLike.ext _ _ fun x ↦ x.induction_on fun i g ↦ by simp lemma map_comp [IsDirected ι (· ≤ ·)] (g₁ : (i : ι) → G i →+* G' i) (g₂ : (i : ι) → G' i →+* G'' i) (hg₁ : ∀ i j h, (g₁ j).comp (f i j h) = (f' i j h).comp (g₁ i)) (hg₂ : ∀ i j h, (g₂ j).comp (f' i j h) = (f'' i j h).comp (g₂ i)) : ((map g₂ hg₂).comp (map g₁ hg₁) : DirectLimit G (fun _ _ h ↦ f _ _ h) →+* DirectLimit G'' fun _ _ h ↦ f'' _ _ h) = (map (fun i ↦ (g₂ i).comp (g₁ i)) fun i j h ↦ by rw [RingHom.comp_assoc, hg₁ i, ← RingHom.comp_assoc, hg₂ i, RingHom.comp_assoc] : DirectLimit G (fun _ _ h ↦ f _ _ h) →+* DirectLimit G'' fun _ _ h ↦ f'' _ _ h) := DFunLike.ext _ _ fun x ↦ x.induction_on fun i g ↦ by simp /-- Consider direct limits `lim G` and `lim G'` with direct system `f` and `f'` respectively, any family of equivalences `eᵢ : Gᵢ ≅ G'ᵢ` such that `e ∘ f = f' ∘ e` induces an equivalence `lim G ⟶ lim G'`. -/ def congr [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+* G' i) (he : ∀ i j h, (e j).toRingHom.comp (f i j h) = (f' i j h).comp (e i)) : DirectLimit G (fun _ _ h ↦ f _ _ h) ≃+* DirectLimit G' fun _ _ h ↦ f' _ _ h := RingEquiv.ofHomInv (map (e ·) he) (map (fun i ↦ (e i).symm) fun i j h ↦ DFunLike.ext _ _ fun x ↦ by have eq1 := DFunLike.congr_fun (he i j h) ((e i).symm x) simp only [RingEquiv.toRingHom_eq_coe, RingHom.coe_comp, RingHom.coe_coe, Function.comp_apply, RingEquiv.apply_symm_apply] at eq1 ⊢ simp [← eq1, of_f]) (DFunLike.ext _ _ fun x ↦ x.induction_on <| by simp) (DFunLike.ext _ _ fun x ↦ x.induction_on <| by simp) lemma congr_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+* G' i) (he : ∀ i j h, (e j).toRingHom.comp (f i j h) = (f' i j h).comp (e i)) {i : ι} (g : G i) : congr e he (of G _ i g) = of G' (fun _ _ h ↦ f' _ _ h) i (e i g) := map_apply_of _ he _ lemma congr_symm_apply_of [IsDirected ι (· ≤ ·)] (e : (i : ι) → G i ≃+* G' i) (he : ∀ i j h, (e j).toRingHom.comp (f i j h) = (f' i j h).comp (e i)) {i : ι} (g : G' i) : (congr e he).symm (of G' _ i g) = of G (fun _ _ h ↦ f _ _ h) i ((e i).symm g) := by simp only [congr, RingEquiv.ofHomInv_symm_apply, map_apply_of, RingHom.coe_coe] end functorial end DirectLimit end end Ring namespace Field variable [Nonempty ι] [IsDirected ι (· ≤ ·)] [∀ i, Field (G i)] variable (f : ∀ i j, i ≤ j → G i → G j) variable (f' : ∀ i j, i ≤ j → G i →+* G j) namespace DirectLimit instance nontrivial [DirectedSystem G fun i j h => f' i j h] : Nontrivial (Ring.DirectLimit G fun i j h => f' i j h) := ⟨⟨0, 1, Nonempty.elim (by infer_instance) fun i : ι => by change (0 : Ring.DirectLimit G fun i j h => f' i j h) ≠ 1 rw [← (Ring.DirectLimit.of _ _ _).map_one] · intro H; rcases Ring.DirectLimit.of.zero_exact H.symm with ⟨j, hij, hf⟩ rw [(f' i j hij).map_one] at hf exact one_ne_zero hf⟩⟩ theorem exists_inv {p : Ring.DirectLimit G f} : p ≠ 0 → ∃ y, p * y = 1 := Ring.DirectLimit.induction_on p fun i x H => ⟨Ring.DirectLimit.of G f i x⁻¹, by erw [← (Ring.DirectLimit.of _ _ _).map_mul, mul_inv_cancel fun h : x = 0 => H <| by rw [h, (Ring.DirectLimit.of _ _ _).map_zero], (Ring.DirectLimit.of _ _ _).map_one]⟩ section open Classical in /-- Noncomputable multiplicative inverse in a direct limit of fields. -/ noncomputable def inv (p : Ring.DirectLimit G f) : Ring.DirectLimit G f := if H : p = 0 then 0 else Classical.choose (DirectLimit.exists_inv G f H) protected theorem mul_inv_cancel {p : Ring.DirectLimit G f} (hp : p ≠ 0) : p * inv G f p = 1 := by rw [inv, dif_neg hp, Classical.choose_spec (DirectLimit.exists_inv G f hp)] protected theorem inv_mul_cancel {p : Ring.DirectLimit G f} (hp : p ≠ 0) : inv G f p * p = 1 := by rw [_root_.mul_comm, DirectLimit.mul_inv_cancel G f hp] /-- Noncomputable field structure on the direct limit of fields. See note [reducible non-instances]. -/ protected noncomputable abbrev field [DirectedSystem G fun i j h => f' i j h] : Field (Ring.DirectLimit G fun i j h => f' i j h) where -- This used to include the parent CommRing and Nontrivial instances, -- but leaving them implicit avoids a very expensive (2-3 minutes!) eta expansion. inv := inv G fun i j h => f' i j h mul_inv_cancel := fun p => DirectLimit.mul_inv_cancel G fun i j h => f' i j h inv_zero := dif_pos rfl nnqsmul := _ qsmul := _ end end DirectLimit end Field
Algebra\DualNumber.lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.TrivSqZeroExt /-! # Dual numbers The dual numbers over `R` are of the form `a + bε`, where `a` and `b` are typically elements of a commutative ring `R`, and `ε` is a symbol satisfying `ε^2 = 0` that commutes with every other element. They are a special case of `TrivSqZeroExt R M` with `M = R`. ## Notation In the `DualNumber` locale: * `R[ε]` is a shorthand for `DualNumber R` * `ε` is a shorthand for `DualNumber.eps` ## Main definitions * `DualNumber` * `DualNumber.eps` * `DualNumber.lift` ## Implementation notes Rather than duplicating the API of `TrivSqZeroExt`, this file reuses the functions there. ## References * https://en.wikipedia.org/wiki/Dual_number -/ variable {R A B : Type*} /-- The type of dual numbers, numbers of the form $a + bε$ where $ε^2 = 0$. `R[ε]` is notation for `DualNumber R`. -/ abbrev DualNumber (R : Type*) : Type _ := TrivSqZeroExt R R /-- The unit element $ε$ that squares to zero, with notation `ε`. -/ def DualNumber.eps [Zero R] [One R] : DualNumber R := TrivSqZeroExt.inr 1 @[inherit_doc] scoped[DualNumber] notation "ε" => DualNumber.eps @[inherit_doc] scoped[DualNumber] postfix:1024 "[ε]" => DualNumber open DualNumber namespace DualNumber open TrivSqZeroExt @[simp] theorem fst_eps [Zero R] [One R] : fst ε = (0 : R) := fst_inr _ _ @[simp] theorem snd_eps [Zero R] [One R] : snd ε = (1 : R) := snd_inr _ _ /-- A version of `TrivSqZeroExt.snd_mul` with `*` instead of `•`. -/ @[simp] theorem snd_mul [Semiring R] (x y : R[ε]) : snd (x * y) = fst x * snd y + snd x * fst y := TrivSqZeroExt.snd_mul _ _ @[simp] theorem eps_mul_eps [Semiring R] : (ε * ε : R[ε]) = 0 := inr_mul_inr _ _ _ @[simp] theorem inv_eps [DivisionRing R] : (ε : R[ε])⁻¹ = 0 := TrivSqZeroExt.inv_inr 1 @[simp] theorem inr_eq_smul_eps [MulZeroOneClass R] (r : R) : inr r = (r • ε : R[ε]) := ext (mul_zero r).symm (mul_one r).symm /-- `ε` commutes with every element of the algebra. -/ theorem commute_eps_left [Semiring R] (x : DualNumber R) : Commute ε x := by ext <;> simp /-- `ε` commutes with every element of the algebra. -/ theorem commute_eps_right [Semiring R] (x : DualNumber R) : Commute x ε := (commute_eps_left x).symm variable {A : Type*} [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] /-- For two `R`-algebra morphisms out of `A[ε]` to agree, it suffices for them to agree on the elements of `A` and the `A`-multiples of `ε`. -/ @[ext 1100] nonrec theorem algHom_ext' ⦃f g : A[ε] →ₐ[R] B⦄ (hinl : f.comp (inlAlgHom _ _ _) = g.comp (inlAlgHom _ _ _)) (hinr : f.toLinearMap ∘ₗ (LinearMap.toSpanSingleton A A[ε] ε).restrictScalars R = g.toLinearMap ∘ₗ (LinearMap.toSpanSingleton A A[ε] ε).restrictScalars R) : f = g := algHom_ext' hinl (by ext a show f (inr a) = g (inr a) simpa only [inr_eq_smul_eps] using DFunLike.congr_fun hinr a) /-- For two `R`-algebra morphisms out of `R[ε]` to agree, it suffices for them to agree on `ε`. -/ @[ext 1200] nonrec theorem algHom_ext ⦃f g : R[ε] →ₐ[R] A⦄ (hε : f ε = g ε) : f = g := by ext dsimp simp only [one_smul, hε] /-- A universal property of the dual numbers, providing a unique `A[ε] →ₐ[R] B` for every map `f : A →ₐ[R] B` and a choice of element `e : B` which squares to `0` and commutes with the range of `f`. This isomorphism is named to match the similar `Complex.lift`. Note that when `f : R →ₐ[R] B := Algebra.ofId R B`, the commutativity assumption is automatic, and we are free to choose any element `e : B`. -/ def lift : {fe : (A →ₐ[R] B) × B // fe.2 * fe.2 = 0 ∧ ∀ a, Commute fe.2 (fe.1 a)} ≃ (A[ε] →ₐ[R] B) := by refine Equiv.trans ?_ TrivSqZeroExt.liftEquiv exact { toFun := fun fe => ⟨ (fe.val.1, MulOpposite.op fe.val.2 • fe.val.1.toLinearMap), fun x y => show (fe.val.1 x * fe.val.2) * (fe.val.1 y * fe.val.2) = 0 by rw [(fe.prop.2 _).mul_mul_mul_comm, fe.prop.1, mul_zero], fun r x => show fe.val.1 (r * x) * fe.val.2 = fe.val.1 r * (fe.val.1 x * fe.val.2) by rw [map_mul, mul_assoc], fun r x => show fe.val.1 (x * r) * fe.val.2 = (fe.val.1 x * fe.val.2) * fe.val.1 r by rw [map_mul, (fe.prop.2 _).right_comm]⟩ invFun := fun fg => ⟨ (fg.val.1, fg.val.2 1), fg.prop.1 _ _, fun a => show fg.val.2 1 * fg.val.1 a = fg.val.1 a * fg.val.2 1 by rw [← fg.prop.2.1, ← fg.prop.2.2, smul_eq_mul, op_smul_eq_mul, mul_one, one_mul]⟩ left_inv := fun fe => Subtype.ext <| Prod.ext rfl <| show fe.val.1 1 * fe.val.2 = fe.val.2 by rw [map_one, one_mul] right_inv := fun fg => Subtype.ext <| Prod.ext rfl <| LinearMap.ext fun x => show fg.val.1 x * fg.val.2 1 = fg.val.2 x by rw [← fg.prop.2.1, smul_eq_mul, mul_one] } theorem lift_apply_apply (fe : {_fe : (A →ₐ[R] B) × B // _}) (a : A[ε]) : lift fe a = fe.val.1 a.fst + fe.val.1 a.snd * fe.val.2 := rfl @[simp] theorem coe_lift_symm_apply (F : A[ε] →ₐ[R] B) : (lift.symm F).val = (F.comp (inlAlgHom _ _ _), F ε) := rfl /-- When applied to `inl`, `DualNumber.lift` applies the map `f : A →ₐ[R] B`. -/ @[simp] theorem lift_apply_inl (fe : {fe : (A →ₐ[R] B) × B // _}) (a : A) : lift fe (inl a : A[ε]) = fe.val.1 a := by rw [lift_apply_apply, fst_inl, snd_inl, map_zero, zero_mul, add_zero] /-- Scaling on the left is sent by `DualNumber.lift` to multiplication on the left -/ @[simp] theorem lift_smul (fe : {fe : (A →ₐ[R] B) × B // _}) (a : A) (ad : A[ε]) : lift fe (a • ad) = fe.val.1 a * lift fe ad := by rw [← inl_mul_eq_smul, map_mul, lift_apply_inl] /-- Scaling on the right is sent by `DualNumber.lift` to multiplication on the right -/ @[simp] theorem lift_op_smul (fe : {fe : (A →ₐ[R] B) × B // _}) (a : A) (ad : A[ε]) : lift fe (MulOpposite.op a • ad) = lift fe ad * fe.val.1 a := by rw [← mul_inl_eq_op_smul, map_mul, lift_apply_inl] /-- When applied to `ε`, `DualNumber.lift` produces the element of `B` that squares to 0. -/ @[simp] theorem lift_apply_eps (fe : {fe : (A →ₐ[R] B) × B // fe.2 * fe.2 = 0 ∧ ∀ a, Commute fe.2 (fe.1 a)}) : lift fe (ε : A[ε]) = fe.val.2 := by simp only [lift_apply_apply, fst_eps, map_zero, snd_eps, map_one, one_mul, zero_add] /-- Lifting `DualNumber.eps` itself gives the identity. -/ @[simp] theorem lift_inlAlgHom_eps : lift ⟨(inlAlgHom _ _ _, ε), eps_mul_eps, fun _ => commute_eps_left _⟩ = AlgHom.id R A[ε] := lift.apply_symm_apply <| AlgHom.id R A[ε] /-- Show DualNumber with values x and y as an "x + y*ε" string -/ instance instRepr [Repr R] : Repr (DualNumber R) where reprPrec f p := (if p > 65 then (Std.Format.bracket "(" · ")") else (·)) <| reprPrec f.fst 65 ++ " + " ++ reprPrec f.snd 70 ++ "*ε" end DualNumber
Algebra\DualQuaternion.lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.DualNumber import Mathlib.Algebra.Quaternion /-! # Dual quaternions Similar to the way that rotations in 3D space can be represented by quaternions of unit length, rigid motions in 3D space can be represented by dual quaternions of unit length. ## Main results * `Quaternion.dualNumberEquiv`: quaternions over dual numbers or dual numbers over quaternions are equivalent constructions. ## References * <https://en.wikipedia.org/wiki/Dual_quaternion> -/ variable {R : Type*} [CommRing R] namespace Quaternion /-- The dual quaternions can be equivalently represented as a quaternion with dual coefficients, or as a dual number with quaternion coefficients. See also `Matrix.dualNumberEquiv` for a similar result. -/ def dualNumberEquiv : Quaternion (DualNumber R) ≃ₐ[R] DualNumber (Quaternion R) where toFun q := (⟨q.re.fst, q.imI.fst, q.imJ.fst, q.imK.fst⟩, ⟨q.re.snd, q.imI.snd, q.imJ.snd, q.imK.snd⟩) invFun d := ⟨(d.fst.re, d.snd.re), (d.fst.imI, d.snd.imI), (d.fst.imJ, d.snd.imJ), (d.fst.imK, d.snd.imK)⟩ left_inv := fun ⟨⟨r, rε⟩, ⟨i, iε⟩, ⟨j, jε⟩, ⟨k, kε⟩⟩ => rfl right_inv := fun ⟨⟨r, i, j, k⟩, ⟨rε, iε, jε, kε⟩⟩ => rfl map_mul' := by rintro ⟨⟨xr, xrε⟩, ⟨xi, xiε⟩, ⟨xj, xjε⟩, ⟨xk, xkε⟩⟩ rintro ⟨⟨yr, yrε⟩, ⟨yi, yiε⟩, ⟨yj, yjε⟩, ⟨yk, ykε⟩⟩ ext : 1 · rfl · dsimp congr 1 <;> simp <;> ring map_add' := by rintro ⟨⟨xr, xrε⟩, ⟨xi, xiε⟩, ⟨xj, xjε⟩, ⟨xk, xkε⟩⟩ rintro ⟨⟨yr, yrε⟩, ⟨yi, yiε⟩, ⟨yj, yjε⟩, ⟨yk, ykε⟩⟩ rfl commutes' r := rfl /-! Lemmas characterizing `Quaternion.dualNumberEquiv`. -/ -- `simps` can't work on `DualNumber` because it's not a structure @[simp] theorem re_fst_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).fst.re = q.re.fst := rfl @[simp] theorem imI_fst_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).fst.imI = q.imI.fst := rfl @[simp] theorem imJ_fst_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).fst.imJ = q.imJ.fst := rfl @[simp] theorem imK_fst_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).fst.imK = q.imK.fst := rfl @[simp] theorem re_snd_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).snd.re = q.re.snd := rfl @[simp] theorem imI_snd_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).snd.imI = q.imI.snd := rfl @[simp] theorem imJ_snd_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).snd.imJ = q.imJ.snd := rfl @[simp] theorem imK_snd_dualNumberEquiv (q : Quaternion (DualNumber R)) : (dualNumberEquiv q).snd.imK = q.imK.snd := rfl @[simp] theorem fst_re_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).re.fst = d.fst.re := rfl @[simp] theorem fst_imI_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imI.fst = d.fst.imI := rfl @[simp] theorem fst_imJ_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imJ.fst = d.fst.imJ := rfl @[simp] theorem fst_imK_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imK.fst = d.fst.imK := rfl @[simp] theorem snd_re_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).re.snd = d.snd.re := rfl @[simp] theorem snd_imI_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imI.snd = d.snd.imI := rfl @[simp] theorem snd_imJ_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imJ.snd = d.snd.imJ := rfl @[simp] theorem snd_imK_dualNumberEquiv_symm (d : DualNumber (Quaternion R)) : (dualNumberEquiv.symm d).imK.snd = d.snd.imK := rfl end Quaternion
Algebra\Exact.lean
/- Copyright (c) 2023 Antoine Chambert-Loir. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Antoine Chambert-Loir -/ import Mathlib.Algebra.Module.Submodule.Range import Mathlib.LinearAlgebra.Prod import Mathlib.LinearAlgebra.Quotient /-! # Exactness of a pair * For two maps `f : M → N` and `g : N → P`, with `Zero P`, `Function.Exact f g` says that `Set.range f = Set.preimage g {0}` * For additive maps `f : M →+ N` and `g : N →+ P`, `Exact f g` says that `range f = ker g` * For linear maps `f : M →ₗ[R] N` and `g : N →ₗ[R] P`, `Exact f g` says that `range f = ker g` ## TODO : * generalize to `SemilinearMap`, even `SemilinearMapClass` * add the multiplicative case (`Function.Exact` will become `Function.AddExact`?) -/ variable {R M M' N N' P P' : Type*} namespace Function variable (f : M → N) (g : N → P) (g' : P → P') /-- The maps `f` and `g` form an exact pair : `g y = 0` iff `y` belongs to the image of `f` -/ def Exact [Zero P] : Prop := ∀ y, g y = 0 ↔ y ∈ Set.range f variable {f g} namespace Exact lemma apply_apply_eq_zero [Zero P] (h : Exact f g) (x : M) : g (f x) = 0 := (h _).mpr <| Set.mem_range_self _ lemma comp_eq_zero [Zero P] (h : Exact f g) : g.comp f = 0 := funext h.apply_apply_eq_zero lemma of_comp_of_mem_range [Zero P] (h1 : g ∘ f = 0) (h2 : ∀ x, g x = 0 → x ∈ Set.range f) : Exact f g := fun y => Iff.intro (h2 y) <| Exists.rec ((forall_apply_eq_imp_iff (p := (g · = 0))).mpr (congrFun h1) y) lemma comp_injective [Zero P] [Zero P'] (exact : Exact f g) (inj : Function.Injective g') (h0 : g' 0 = 0) : Exact f (g' ∘ g) := by intro x refine ⟨fun H => exact x |>.mp <| inj <| h0 ▸ H, ?_⟩ intro H rw [Function.comp_apply, exact x |>.mpr H, h0] lemma of_comp_eq_zero_of_ker_in_range [Zero P] (hc : g.comp f = 0) (hr : ∀ y, g y = 0 → y ∈ Set.range f) : Exact f g := fun y ↦ ⟨hr y, fun ⟨x, hx⟩ ↦ hx ▸ congrFun hc x⟩ end Exact end Function section AddMonoidHom variable [AddGroup M] [AddGroup N] [AddGroup P] {f : M →+ N} {g : N →+ P} namespace AddMonoidHom open Function lemma exact_iff : Exact f g ↔ ker g = range f := Iff.symm SetLike.ext_iff lemma exact_of_comp_eq_zero_of_ker_le_range (h1 : g.comp f = 0) (h2 : ker g ≤ range f) : Exact f g := Exact.of_comp_of_mem_range (congrArg DFunLike.coe h1) h2 lemma exact_of_comp_of_mem_range (h1 : g.comp f = 0) (h2 : ∀ x, g x = 0 → x ∈ range f) : Exact f g := exact_of_comp_eq_zero_of_ker_le_range h1 h2 end AddMonoidHom namespace Function.Exact open AddMonoidHom lemma addMonoidHom_ker_eq (hfg : Exact f g) : ker g = range f := SetLike.ext hfg lemma addMonoidHom_comp_eq_zero (h : Exact f g) : g.comp f = 0 := DFunLike.coe_injective h.comp_eq_zero section variable {X₁ X₂ X₃ Y₁ Y₂ Y₃ : Type*} [AddCommMonoid X₁] [AddCommMonoid X₂] [AddCommMonoid X₃] [AddCommMonoid Y₁] [AddCommMonoid Y₂] [AddCommMonoid Y₃] (e₁ : X₁ ≃+ Y₁) (e₂ : X₂ ≃+ Y₂) (e₃ : X₃ ≃+ Y₃) {f₁₂ : X₁ →+ X₂} {f₂₃ : X₂ →+ X₃} {g₁₂ : Y₁ →+ Y₂} {g₂₃ : Y₂ →+ Y₃} lemma of_ladder_addEquiv_of_exact (comm₁₂ : g₁₂.comp e₁ = AddMonoidHom.comp e₂ f₁₂) (comm₂₃ : g₂₃.comp e₂ = AddMonoidHom.comp e₃ f₂₃) (H : Exact f₁₂ f₂₃) : Exact g₁₂ g₂₃ := by have h₁₂ := DFunLike.congr_fun comm₁₂ have h₂₃ := DFunLike.congr_fun comm₂₃ dsimp at h₁₂ h₂₃ apply of_comp_eq_zero_of_ker_in_range · ext y₁ obtain ⟨x₁, rfl⟩ := e₁.surjective y₁ dsimp rw [h₁₂, h₂₃, H.apply_apply_eq_zero, map_zero] · intro y₂ hx₂ obtain ⟨x₂, rfl⟩ := e₂.surjective y₂ obtain ⟨x₁, rfl⟩ := (H x₂).1 (e₃.injective (by rw [← h₂₃, hx₂, map_zero])) exact ⟨e₁ x₁, by rw [h₁₂]⟩ lemma of_ladder_addEquiv_of_exact' (comm₁₂ : g₁₂.comp e₁ = AddMonoidHom.comp e₂ f₁₂) (comm₂₃ : g₂₃.comp e₂ = AddMonoidHom.comp e₃ f₂₃) (H : Exact g₁₂ g₂₃) : Exact f₁₂ f₂₃ := by refine of_ladder_addEquiv_of_exact e₁.symm e₂.symm e₃.symm ?_ ?_ H · ext y₁ obtain ⟨x₁, rfl⟩ := e₁.surjective y₁ apply e₂.injective simpa using DFunLike.congr_fun comm₁₂.symm x₁ · ext y₂ obtain ⟨x₂, rfl⟩ := e₂.surjective y₂ apply e₃.injective simpa using DFunLike.congr_fun comm₂₃.symm x₂ lemma iff_of_ladder_addEquiv (comm₁₂ : g₁₂.comp e₁ = AddMonoidHom.comp e₂ f₁₂) (comm₂₃ : g₂₃.comp e₂ = AddMonoidHom.comp e₃ f₂₃) : Exact g₁₂ g₂₃ ↔ Exact f₁₂ f₂₃ := by constructor · exact of_ladder_addEquiv_of_exact' e₁ e₂ e₃ comm₁₂ comm₂₃ · exact of_ladder_addEquiv_of_exact e₁ e₂ e₃ comm₁₂ comm₂₃ end end Function.Exact end AddMonoidHom section LinearMap open Function variable [Semiring R] [AddCommMonoid M] [AddCommMonoid M'] [AddCommMonoid N] [AddCommMonoid N'] [AddCommMonoid P] [AddCommMonoid P'] [Module R M] [Module R M'] [Module R N] [Module R N'] [Module R P] [Module R P'] variable {f : M →ₗ[R] N} {g : N →ₗ[R] P} namespace LinearMap lemma exact_iff : Exact f g ↔ LinearMap.ker g = LinearMap.range f := Iff.symm SetLike.ext_iff lemma exact_of_comp_eq_zero_of_ker_le_range (h1 : g ∘ₗ f = 0) (h2 : ker g ≤ range f) : Exact f g := Exact.of_comp_of_mem_range (congrArg DFunLike.coe h1) h2 lemma exact_of_comp_of_mem_range (h1 : g ∘ₗ f = 0) (h2 : ∀ x, g x = 0 → x ∈ range f) : Exact f g := exact_of_comp_eq_zero_of_ker_le_range h1 h2 variable {R M N P : Type*} [CommRing R] [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [Module R M] [Module R N] [Module R P] lemma exact_subtype_mkQ (Q : Submodule R N) : Exact (Submodule.subtype Q) (Submodule.mkQ Q) := by rw [exact_iff, Submodule.ker_mkQ, Submodule.range_subtype Q] lemma exact_map_mkQ_range (f : M →ₗ[R] N) : Exact f (Submodule.mkQ (range f)) := exact_iff.mpr <| Submodule.ker_mkQ _ lemma exact_subtype_ker_map (g : N →ₗ[R] P) : Exact (Submodule.subtype (ker g)) g := exact_iff.mpr <| (Submodule.range_subtype _).symm end LinearMap variable (f g) in lemma LinearEquiv.conj_exact_iff_exact (e : N ≃ₗ[R] N') : Function.Exact (e ∘ₗ f) (g ∘ₗ (e.symm : N' →ₗ[R] N)) ↔ Exact f g := by simp_rw [LinearMap.exact_iff, LinearMap.ker_comp, ← e.map_eq_comap, LinearMap.range_comp] exact (Submodule.map_injective_of_injective e.injective).eq_iff namespace Function open LinearMap lemma Exact.linearMap_ker_eq (hfg : Exact f g) : ker g = range f := SetLike.ext hfg lemma Exact.linearMap_comp_eq_zero (h : Exact f g) : g.comp f = 0 := DFunLike.coe_injective h.comp_eq_zero lemma Surjective.comp_exact_iff_exact {p : M' →ₗ[R] M} (h : Surjective p) : Exact (f ∘ₗ p) g ↔ Exact f g := iff_of_eq <| forall_congr fun x => congrArg (g x = 0 ↔ x ∈ ·) (h.range_comp f) lemma Injective.comp_exact_iff_exact {i : P →ₗ[R] P'} (h : Injective i) : Exact f (i ∘ₗ g) ↔ Exact f g := forall_congr' fun _ => iff_congr (LinearMap.map_eq_zero_iff _ h) Iff.rfl variable {f₁₂ : M →ₗ[R] N} {f₂₃ : N →ₗ[R] P} {g₁₂ : M' →ₗ[R] N'} {g₂₃ : N' →ₗ[R] P'} {e₁ : M ≃ₗ[R] M'} {e₂ : N ≃ₗ[R] N'} {e₃ : P ≃ₗ[R] P'} lemma Exact.iff_of_ladder_linearEquiv (h₁₂ : g₁₂ ∘ₗ e₁ = e₂ ∘ₗ f₁₂) (h₂₃ : g₂₃ ∘ₗ e₂ = e₃ ∘ₗ f₂₃) : Exact g₁₂ g₂₃ ↔ Exact f₁₂ f₂₃ := iff_of_ladder_addEquiv e₁.toAddEquiv e₂.toAddEquiv e₃.toAddEquiv (f₁₂ := f₁₂) (f₂₃ := f₂₃) (g₁₂ := g₁₂) (g₂₃ := g₂₃) (congr_arg LinearMap.toAddMonoidHom h₁₂) (congr_arg LinearMap.toAddMonoidHom h₂₃) lemma Exact.of_ladder_linearEquiv_of_exact (h₁₂ : g₁₂ ∘ₗ e₁ = e₂ ∘ₗ f₁₂) (h₂₃ : g₂₃ ∘ₗ e₂ = e₃ ∘ₗ f₂₃) (H : Exact f₁₂ f₂₃) : Exact g₁₂ g₂₃ := by rwa [iff_of_ladder_linearEquiv h₁₂ h₂₃] end Function end LinearMap namespace Function section split variable [Semiring R] variable [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [Module R M] [Module R N] [Module R P] variable {f : M →ₗ[R] N} {g : N →ₗ[R] P} open LinearMap /-- Given an exact sequence `0 → M → N → P`, giving a section `P → N` is equivalent to giving a splitting `N ≃ M × P`. -/ noncomputable def Exact.splitSurjectiveEquiv (h : Function.Exact f g) (hf : Function.Injective f) : { l // g ∘ₗ l = .id } ≃ { e : N ≃ₗ[R] M × P // f = e.symm ∘ₗ inl R M P ∧ g = snd R M P ∘ₗ e } := by refine { toFun := fun l ↦ ⟨(LinearEquiv.ofBijective (f ∘ₗ fst R M P + l.1 ∘ₗ snd R M P) ?_).symm, ?_⟩ invFun := fun e ↦ ⟨e.1.symm ∘ₗ inr R M P, ?_⟩ left_inv := ?_ right_inv := ?_ } · have h₁ : ∀ x, g (l.1 x) = x := LinearMap.congr_fun l.2 have h₂ : ∀ x, g (f x) = 0 := congr_fun h.comp_eq_zero constructor · intros x y e simp only [add_apply, coe_comp, comp_apply, fst_apply, snd_apply] at e suffices x.2 = y.2 from Prod.ext (hf (by rwa [this, add_left_inj] at e)) this simpa [h₁, h₂] using DFunLike.congr_arg g e · intro x obtain ⟨y, hy⟩ := (h (x - l.1 (g x))).mp (by simp [h₁, g.map_sub]) exact ⟨⟨y, g x⟩, by simp [hy]⟩ · have h₁ : ∀ x, g (l.1 x) = x := LinearMap.congr_fun l.2 have h₂ : ∀ x, g (f x) = 0 := congr_fun h.comp_eq_zero constructor · ext; simp · rw [LinearEquiv.eq_comp_toLinearMap_symm] ext <;> simp [h₁, h₂] · rw [← LinearMap.comp_assoc, (LinearEquiv.eq_comp_toLinearMap_symm _ _).mp e.2.2]; rfl · intro; ext; simp · rintro ⟨e, rfl, rfl⟩ ext1 apply LinearEquiv.symm_bijective.injective ext apply e.injective ext <;> simp /-- Given an exact sequence `M → N → P → 0`, giving a retraction `N → M` is equivalent to giving a splitting `N ≃ M × P`. -/ noncomputable def Exact.splitInjectiveEquiv {R M N P} [Semiring R] [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [Module R M] [Module R N] [Module R P] {f : M →ₗ[R] N} {g : N →ₗ[R] P} (h : Function.Exact f g) (hg : Function.Surjective g) : { l // l ∘ₗ f = .id } ≃ { e : N ≃ₗ[R] M × P // f = e.symm ∘ₗ inl R M P ∧ g = snd R M P ∘ₗ e } := by refine { toFun := fun l ↦ ⟨(LinearEquiv.ofBijective (l.1.prod g) ?_), ?_⟩ invFun := fun e ↦ ⟨fst R M P ∘ₗ e.1, ?_⟩ left_inv := ?_ right_inv := ?_ } · have h₁ : ∀ x, l.1 (f x) = x := LinearMap.congr_fun l.2 have h₂ : ∀ x, g (f x) = 0 := congr_fun h.comp_eq_zero constructor · intros x y e simp only [prod_apply, Pi.prod, Prod.mk.injEq] at e obtain ⟨z, hz⟩ := (h (x - y)).mp (by simpa [sub_eq_zero] using e.2) suffices z = 0 by rw [← sub_eq_zero, ← hz, this, map_zero] rw [← h₁ z, hz, map_sub, e.1, sub_self] · rintro ⟨x, y⟩ obtain ⟨y, rfl⟩ := hg y refine ⟨f x + y - f (l.1 y), by ext <;> simp [h₁, h₂]⟩ · have h₁ : ∀ x, l.1 (f x) = x := LinearMap.congr_fun l.2 have h₂ : ∀ x, g (f x) = 0 := congr_fun h.comp_eq_zero constructor · rw [LinearEquiv.eq_toLinearMap_symm_comp] ext <;> simp [h₁, h₂] · ext; simp · rw [LinearMap.comp_assoc, (LinearEquiv.eq_toLinearMap_symm_comp _ _).mp e.2.1]; rfl · intro; ext; simp · rintro ⟨e, rfl, rfl⟩ ext x <;> simp theorem Exact.split_tfae' (h : Function.Exact f g) : List.TFAE [ Function.Injective f ∧ ∃ l, g ∘ₗ l = LinearMap.id, Function.Surjective g ∧ ∃ l, l ∘ₗ f = LinearMap.id, ∃ e : N ≃ₗ[R] M × P, f = e.symm ∘ₗ LinearMap.inl R M P ∧ g = LinearMap.snd R M P ∘ₗ e] := by tfae_have 1 → 3 · rintro ⟨hf, l, hl⟩ exact ⟨_, (h.splitSurjectiveEquiv hf ⟨l, hl⟩).2⟩ tfae_have 2 → 3 · rintro ⟨hg, l, hl⟩ exact ⟨_, (h.splitInjectiveEquiv hg ⟨l, hl⟩).2⟩ tfae_have 3 → 1 · rintro ⟨e, e₁, e₂⟩ have : Function.Injective f := e₁ ▸ e.symm.injective.comp LinearMap.inl_injective refine ⟨this, ⟨_, ((h.splitSurjectiveEquiv this).symm ⟨e, e₁, e₂⟩).2⟩⟩ tfae_have 3 → 2 · rintro ⟨e, e₁, e₂⟩ have : Function.Surjective g := e₂ ▸ Prod.snd_surjective.comp e.surjective refine ⟨this, ⟨_, ((h.splitInjectiveEquiv this).symm ⟨e, e₁, e₂⟩).2⟩⟩ tfae_finish /-- Equivalent characterizations of split exact sequences. Also known as the **Splitting lemma**. -/ theorem Exact.split_tfae {R M N P} [Semiring R] [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [Module R M] [Module R N] [Module R P] {f : M →ₗ[R] N} {g : N →ₗ[R] P} (h : Function.Exact f g) (hf : Function.Injective f) (hg : Function.Surjective g) : List.TFAE [ ∃ l, g ∘ₗ l = LinearMap.id, ∃ l, l ∘ₗ f = LinearMap.id, ∃ e : N ≃ₗ[R] M × P, f = e.symm ∘ₗ LinearMap.inl R M P ∧ g = LinearMap.snd R M P ∘ₗ e] := by tfae_have 1 ↔ 3 · simpa using (h.splitSurjectiveEquiv hf).nonempty_congr tfae_have 2 ↔ 3 · simpa using (h.splitInjectiveEquiv hg).nonempty_congr tfae_finish end split section Prod variable [Semiring R] [AddCommMonoid M] [AddCommMonoid N] [Module R M] [Module R N] lemma Exact.inr_fst : Function.Exact (LinearMap.inr R M N) (LinearMap.fst R M N) := by rintro ⟨x, y⟩ simp only [LinearMap.fst_apply, @eq_comm _ x, LinearMap.coe_inr, Set.mem_range, Prod.mk.injEq, exists_eq_right] lemma Exact.inl_snd : Function.Exact (LinearMap.inl R M N) (LinearMap.snd R M N) := by rintro ⟨x, y⟩ simp only [LinearMap.snd_apply, @eq_comm _ y, LinearMap.coe_inl, Set.mem_range, Prod.mk.injEq, exists_eq_left] end Prod section Ring open LinearMap Submodule variable [Ring R] [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [Module R M] [Module R N] [Module R P] /-- A necessary and sufficient condition for an exact sequence to descend to a quotient. -/ lemma Exact.exact_mapQ_iff {f : M →ₗ[R] N} {g : N →ₗ[R] P} (hfg : Exact f g) {p q r} (hpq : p ≤ comap f q) (hqr : q ≤ comap g r) : Exact (mapQ p q f hpq) (mapQ q r g hqr) ↔ range g ⊓ r ≤ map g q := by rw [exact_iff, ← (comap_injective_of_surjective (mkQ_surjective _)).eq_iff] dsimp only [mapQ] rw [← ker_comp, range_liftQ, liftQ_mkQ, ker_comp, range_comp, comap_map_eq, ker_mkQ, ker_mkQ, ← hfg.linearMap_ker_eq, sup_comm, ← LE.le.le_iff_eq (sup_le hqr (ker_le_comap g)), ← comap_map_eq, ← map_le_iff_le_comap, map_comap_eq] end Ring end Function
Algebra\Expr.lean
/- Copyright (c) 2022 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Group.ZeroOne import Qq /-! # Helpers to invoke functions involving algebra at tactic time This file provides instances on `x y : Q($α)` such that `x + y = q($x + $y)`. Porting note: This has been rewritten to use `Qq` instead of `Expr`. -/ open Qq /-- Produce a `One` instance for `Q($α)` such that `1 : Q($α)` is `q(1 : $α)`. -/ def Expr.instOne {u : Lean.Level} (α : Q(Type u)) (_ : Q(One $α)) : One Q($α) where one := q(1 : $α) /-- Produce a `Zero` instance for `Q($α)` such that `0 : Q($α)` is `q(0 : $α)`. -/ def Expr.instZero {u : Lean.Level} (α : Q(Type u)) (_ : Q(Zero $α)) : Zero Q($α) where zero := q(0 : $α) /-- Produce a `Mul` instance for `Q($α)` such that `x * y : Q($α)` is `q($x * $y)`. -/ def Expr.instMul {u : Lean.Level} (α : Q(Type u)) (_ : Q(Mul $α)) : Mul Q($α) where mul x y := q($x * $y) /-- Produce an `Add` instance for `Q($α)` such that `x + y : Q($α)` is `q($x + $y)`. -/ def Expr.instAdd {u : Lean.Level} (α : Q(Type u)) (_ : Q(Add $α)) : Add Q($α) where add x y := q($x + $y)
Algebra\Free.lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Group.Equiv.Basic import Mathlib.Control.Applicative import Mathlib.Control.Traversable.Basic import Mathlib.Data.List.Basic import Mathlib.Logic.Equiv.Defs import Mathlib.Tactic.AdaptationNote /-! # Free constructions ## Main definitions * `FreeMagma α`: free magma (structure with binary operation without any axioms) over alphabet `α`, defined inductively, with traversable instance and decidable equality. * `MagmaAssocQuotient α`: quotient of a magma `α` by the associativity equivalence relation. * `FreeSemigroup α`: free semigroup over alphabet `α`, defined as a structure with two fields `head : α` and `tail : List α` (i.e. nonempty lists), with traversable instance and decidable equality. * `FreeMagmaAssocQuotientEquiv α`: isomorphism between `MagmaAssocQuotient (FreeMagma α)` and `FreeSemigroup α`. * `FreeMagma.lift`: the universal property of the free magma, expressing its adjointness. -/ universe u v l /-- Free nonabelian additive magma over a given alphabet. -/ inductive FreeAddMagma (α : Type u) : Type u | of : α → FreeAddMagma α | add : FreeAddMagma α → FreeAddMagma α → FreeAddMagma α deriving DecidableEq compile_inductive% FreeAddMagma /-- Free magma over a given alphabet. -/ @[to_additive] inductive FreeMagma (α : Type u) : Type u | of : α → FreeMagma α | mul : FreeMagma α → FreeMagma α → FreeMagma α deriving DecidableEq compile_inductive% FreeMagma namespace FreeMagma variable {α : Type u} @[to_additive] instance [Inhabited α] : Inhabited (FreeMagma α) := ⟨of default⟩ @[to_additive] instance : Mul (FreeMagma α) := ⟨FreeMagma.mul⟩ -- Porting note: invalid attribute 'match_pattern', declaration is in an imported module -- attribute [match_pattern] Mul.mul @[to_additive (attr := simp)] theorem mul_eq (x y : FreeMagma α) : mul x y = x * y := rfl /- Porting note: these lemmas are autogenerated by the inductive definition and due to the existence of mul_eq not in simp normal form -/ attribute [nolint simpNF] FreeAddMagma.add.sizeOf_spec attribute [nolint simpNF] FreeMagma.mul.sizeOf_spec attribute [nolint simpNF] FreeAddMagma.add.injEq attribute [nolint simpNF] FreeMagma.mul.injEq /-- Recursor for `FreeMagma` using `x * y` instead of `FreeMagma.mul x y`. -/ @[to_additive (attr := elab_as_elim, induction_eliminator) "Recursor for `FreeAddMagma` using `x + y` instead of `FreeAddMagma.add x y`."] def recOnMul {C : FreeMagma α → Sort l} (x) (ih1 : ∀ x, C (of x)) (ih2 : ∀ x y, C x → C y → C (x * y)) : C x := FreeMagma.recOn x ih1 ih2 @[to_additive (attr := ext 1100)] theorem hom_ext {β : Type v} [Mul β] {f g : FreeMagma α →ₙ* β} (h : f ∘ of = g ∘ of) : f = g := (DFunLike.ext _ _) fun x ↦ recOnMul x (congr_fun h) <| by intros; simp only [map_mul, *] end FreeMagma #adaptation_note /-- around nightly-2024-02-25, we need to write `mul x y` in the second pattern, instead of `x * y`. --/ /-- Lifts a function `α → β` to a magma homomorphism `FreeMagma α → β` given a magma `β`. -/ def FreeMagma.liftAux {α : Type u} {β : Type v} [Mul β] (f : α → β) : FreeMagma α → β | FreeMagma.of x => f x | mul x y => liftAux f x * liftAux f y /-- Lifts a function `α → β` to an additive magma homomorphism `FreeAddMagma α → β` given an additive magma `β`. -/ def FreeAddMagma.liftAux {α : Type u} {β : Type v} [Add β] (f : α → β) : FreeAddMagma α → β | FreeAddMagma.of x => f x | x + y => liftAux f x + liftAux f y attribute [to_additive existing] FreeMagma.liftAux namespace FreeMagma section lift variable {α : Type u} {β : Type v} [Mul β] (f : α → β) /-- The universal property of the free magma expressing its adjointness. -/ @[to_additive (attr := simps symm_apply) "The universal property of the free additive magma expressing its adjointness."] def lift : (α → β) ≃ (FreeMagma α →ₙ* β) where toFun f := { toFun := liftAux f map_mul' := fun x y ↦ rfl } invFun F := F ∘ of left_inv f := rfl right_inv F := by ext; rfl @[to_additive (attr := simp)] theorem lift_of (x) : lift f (of x) = f x := rfl @[to_additive (attr := simp)] theorem lift_comp_of : lift f ∘ of = f := rfl @[to_additive (attr := simp)] theorem lift_comp_of' (f : FreeMagma α →ₙ* β) : lift (f ∘ of) = f := lift.apply_symm_apply f end lift section Map variable {α : Type u} {β : Type v} (f : α → β) /-- The unique magma homomorphism `FreeMagma α →ₙ* FreeMagma β` that sends each `of x` to `of (f x)`. -/ @[to_additive "The unique additive magma homomorphism `FreeAddMagma α → FreeAddMagma β` that sends each `of x` to `of (f x)`."] def map (f : α → β) : FreeMagma α →ₙ* FreeMagma β := lift (of ∘ f) @[to_additive (attr := simp)] theorem map_of (x) : map f (of x) = of (f x) := rfl end Map section Category variable {α β : Type u} @[to_additive] instance : Monad FreeMagma where pure := of bind x f := lift f x /-- Recursor on `FreeMagma` using `pure` instead of `of`. -/ @[to_additive (attr := elab_as_elim) "Recursor on `FreeAddMagma` using `pure` instead of `of`."] protected def recOnPure {C : FreeMagma α → Sort l} (x) (ih1 : ∀ x, C (pure x)) (ih2 : ∀ x y, C x → C y → C (x * y)) : C x := FreeMagma.recOnMul x ih1 ih2 @[to_additive (attr := simp)] theorem map_pure (f : α → β) (x) : (f <$> pure x : FreeMagma β) = pure (f x) := rfl @[to_additive (attr := simp)] theorem map_mul' (f : α → β) (x y : FreeMagma α) : f <$> (x * y) = f <$> x * f <$> y := rfl @[to_additive (attr := simp)] theorem pure_bind (f : α → FreeMagma β) (x) : pure x >>= f = f x := rfl @[to_additive (attr := simp)] theorem mul_bind (f : α → FreeMagma β) (x y : FreeMagma α) : x * y >>= f = (x >>= f) * (y >>= f) := rfl @[to_additive (attr := simp)] theorem pure_seq {α β : Type u} {f : α → β} {x : FreeMagma α} : pure f <*> x = f <$> x := rfl @[to_additive (attr := simp)] theorem mul_seq {α β : Type u} {f g : FreeMagma (α → β)} {x : FreeMagma α} : f * g <*> x = (f <*> x) * (g <*> x) := rfl @[to_additive] instance instLawfulMonad : LawfulMonad FreeMagma.{u} := LawfulMonad.mk' (pure_bind := fun f x ↦ rfl) (bind_assoc := fun x f g ↦ FreeMagma.recOnPure x (fun x ↦ rfl) fun x y ih1 ih2 ↦ by rw [mul_bind, mul_bind, mul_bind, ih1, ih2]) (id_map := fun x ↦ FreeMagma.recOnPure x (fun _ ↦ rfl) fun x y ih1 ih2 ↦ by rw [map_mul', ih1, ih2]) end Category end FreeMagma #adaptation_note /-- around nightly-2024-02-25, we need to write `mul x y` in the second pattern, instead of `x * y`. -/ /-- `FreeMagma` is traversable. -/ protected def FreeMagma.traverse {m : Type u → Type u} [Applicative m] {α β : Type u} (F : α → m β) : FreeMagma α → m (FreeMagma β) | FreeMagma.of x => FreeMagma.of <$> F x | mul x y => (· * ·) <$> x.traverse F <*> y.traverse F /-- `FreeAddMagma` is traversable. -/ protected def FreeAddMagma.traverse {m : Type u → Type u} [Applicative m] {α β : Type u} (F : α → m β) : FreeAddMagma α → m (FreeAddMagma β) | FreeAddMagma.of x => FreeAddMagma.of <$> F x | x + y => (· + ·) <$> x.traverse F <*> y.traverse F attribute [to_additive existing] FreeMagma.traverse namespace FreeMagma variable {α : Type u} section Category variable {β : Type u} @[to_additive] instance : Traversable FreeMagma := ⟨@FreeMagma.traverse⟩ variable {m : Type u → Type u} [Applicative m] (F : α → m β) @[to_additive (attr := simp)] theorem traverse_pure (x) : traverse F (pure x : FreeMagma α) = pure <$> F x := rfl @[to_additive (attr := simp)] theorem traverse_pure' : traverse F ∘ pure = fun x ↦ (pure <$> F x : m (FreeMagma β)) := rfl @[to_additive (attr := simp)] theorem traverse_mul (x y : FreeMagma α) : traverse F (x * y) = (· * ·) <$> traverse F x <*> traverse F y := rfl @[to_additive (attr := simp)] theorem traverse_mul' : Function.comp (traverse F) ∘ (HMul.hMul : FreeMagma α → FreeMagma α → FreeMagma α) = fun x y ↦ (· * ·) <$> traverse F x <*> traverse F y := rfl @[to_additive (attr := simp)] theorem traverse_eq (x) : FreeMagma.traverse F x = traverse F x := rfl -- Porting note (#10675): dsimp can not prove this @[to_additive (attr := simp, nolint simpNF)] theorem mul_map_seq (x y : FreeMagma α) : ((· * ·) <$> x <*> y : Id (FreeMagma α)) = (x * y : FreeMagma α) := rfl @[to_additive] instance : LawfulTraversable FreeMagma.{u} := { instLawfulMonad with id_traverse := fun x ↦ FreeMagma.recOnPure x (fun x ↦ rfl) fun x y ih1 ih2 ↦ by rw [traverse_mul, ih1, ih2, mul_map_seq] comp_traverse := fun f g x ↦ FreeMagma.recOnPure x (fun x ↦ by simp only [(· ∘ ·), traverse_pure, traverse_pure', functor_norm]) (fun x y ih1 ih2 ↦ by rw [traverse_mul, ih1, ih2, traverse_mul] simp [Functor.Comp.map_mk, Functor.map_map, (· ∘ ·), Comp.seq_mk, seq_map_assoc, map_seq, traverse_mul]) naturality := fun η α β f x ↦ FreeMagma.recOnPure x (fun x ↦ by simp only [traverse_pure, functor_norm, Function.comp_apply]) (fun x y ih1 ih2 ↦ by simp only [traverse_mul, functor_norm, ih1, ih2]) traverse_eq_map_id := fun f x ↦ FreeMagma.recOnPure x (fun _ ↦ rfl) fun x y ih1 ih2 ↦ by rw [traverse_mul, ih1, ih2, map_mul', mul_map_seq]; rfl } end Category end FreeMagma -- Porting note: changed String to Lean.Format #adaptation_note /-- around nightly-2024-02-25, we need to write `mul x y` in the second pattern, instead of `x * y`. -/ /-- Representation of an element of a free magma. -/ protected def FreeMagma.repr {α : Type u} [Repr α] : FreeMagma α → Lean.Format | FreeMagma.of x => repr x | mul x y => "( " ++ x.repr ++ " * " ++ y.repr ++ " )" /-- Representation of an element of a free additive magma. -/ protected def FreeAddMagma.repr {α : Type u} [Repr α] : FreeAddMagma α → Lean.Format | FreeAddMagma.of x => repr x | x + y => "( " ++ x.repr ++ " + " ++ y.repr ++ " )" attribute [to_additive existing] FreeMagma.repr @[to_additive] instance {α : Type u} [Repr α] : Repr (FreeMagma α) := ⟨fun o _ => FreeMagma.repr o⟩ #adaptation_note /-- around nightly-2024-02-25, we need to write `mul x y` in the second pattern, instead of `x * y`. -/ /-- Length of an element of a free magma. -/ def FreeMagma.length {α : Type u} : FreeMagma α → ℕ | FreeMagma.of _x => 1 | mul x y => x.length + y.length /-- Length of an element of a free additive magma. -/ def FreeAddMagma.length {α : Type u} : FreeAddMagma α → ℕ | FreeAddMagma.of _x => 1 | x + y => x.length + y.length attribute [to_additive existing (attr := simp)] FreeMagma.length /-- The length of an element of a free magma is positive. -/ @[to_additive "The length of an element of a free additive magma is positive."] lemma FreeMagma.length_pos {α : Type u} (x : FreeMagma α) : 0 < x.length := match x with | FreeMagma.of _ => Nat.succ_pos 0 | mul y z => Nat.add_pos_left (length_pos y) z.length /-- Associativity relations for an additive magma. -/ inductive AddMagma.AssocRel (α : Type u) [Add α] : α → α → Prop | intro : ∀ x y z, AddMagma.AssocRel α (x + y + z) (x + (y + z)) | left : ∀ w x y z, AddMagma.AssocRel α (w + (x + y + z)) (w + (x + (y + z))) /-- Associativity relations for a magma. -/ @[to_additive AddMagma.AssocRel "Associativity relations for an additive magma."] inductive Magma.AssocRel (α : Type u) [Mul α] : α → α → Prop | intro : ∀ x y z, Magma.AssocRel α (x * y * z) (x * (y * z)) | left : ∀ w x y z, Magma.AssocRel α (w * (x * y * z)) (w * (x * (y * z))) namespace Magma /-- Semigroup quotient of a magma. -/ @[to_additive AddMagma.FreeAddSemigroup "Additive semigroup quotient of an additive magma."] def AssocQuotient (α : Type u) [Mul α] : Type u := Quot <| AssocRel α namespace AssocQuotient variable {α : Type u} [Mul α] @[to_additive] theorem quot_mk_assoc (x y z : α) : Quot.mk (AssocRel α) (x * y * z) = Quot.mk _ (x * (y * z)) := Quot.sound (AssocRel.intro _ _ _) @[to_additive] theorem quot_mk_assoc_left (x y z w : α) : Quot.mk (AssocRel α) (x * (y * z * w)) = Quot.mk _ (x * (y * (z * w))) := Quot.sound (AssocRel.left _ _ _ _) @[to_additive] instance : Semigroup (AssocQuotient α) where mul x y := by refine Quot.liftOn₂ x y (fun x y ↦ Quot.mk _ (x * y)) ?_ ?_ · rintro a b₁ b₂ (⟨c, d, e⟩ | ⟨c, d, e, f⟩) <;> simp only · exact quot_mk_assoc_left _ _ _ _ · rw [← quot_mk_assoc, quot_mk_assoc_left, quot_mk_assoc] · rintro a₁ a₂ b (⟨c, d, e⟩ | ⟨c, d, e, f⟩) <;> simp only · simp only [quot_mk_assoc, quot_mk_assoc_left] · rw [quot_mk_assoc, quot_mk_assoc, quot_mk_assoc_left, quot_mk_assoc_left, quot_mk_assoc_left, ← quot_mk_assoc c d, ← quot_mk_assoc c d, quot_mk_assoc_left] mul_assoc x y z := Quot.induction_on₃ x y z fun a b c ↦ quot_mk_assoc a b c /-- Embedding from magma to its free semigroup. -/ @[to_additive "Embedding from additive magma to its free additive semigroup."] def of : α →ₙ* AssocQuotient α where toFun := Quot.mk _; map_mul' _x _y := rfl @[to_additive] instance [Inhabited α] : Inhabited (AssocQuotient α) := ⟨of default⟩ @[to_additive (attr := elab_as_elim, induction_eliminator)] protected theorem induction_on {C : AssocQuotient α → Prop} (x : AssocQuotient α) (ih : ∀ x, C (of x)) : C x := Quot.induction_on x ih section lift variable {β : Type v} [Semigroup β] (f : α →ₙ* β) @[to_additive (attr := ext 1100)] theorem hom_ext {f g : AssocQuotient α →ₙ* β} (h : f.comp of = g.comp of) : f = g := (DFunLike.ext _ _) fun x => AssocQuotient.induction_on x <| DFunLike.congr_fun h /-- Lifts a magma homomorphism `α → β` to a semigroup homomorphism `Magma.AssocQuotient α → β` given a semigroup `β`. -/ @[to_additive (attr := simps symm_apply) "Lifts an additive magma homomorphism `α → β` to an additive semigroup homomorphism `AddMagma.AssocQuotient α → β` given an additive semigroup `β`."] def lift : (α →ₙ* β) ≃ (AssocQuotient α →ₙ* β) where toFun f := { toFun := fun x ↦ Quot.liftOn x f <| by rintro a b (⟨c, d, e⟩ | ⟨c, d, e, f⟩) <;> simp only [map_mul, mul_assoc] map_mul' := fun x y ↦ Quot.induction_on₂ x y (map_mul f) } invFun f := f.comp of left_inv f := (DFunLike.ext _ _) fun x ↦ rfl right_inv f := hom_ext <| (DFunLike.ext _ _) fun x ↦ rfl @[to_additive (attr := simp)] theorem lift_of (x : α) : lift f (of x) = f x := rfl @[to_additive (attr := simp)] theorem lift_comp_of : (lift f).comp of = f := lift.symm_apply_apply f @[to_additive (attr := simp)] theorem lift_comp_of' (f : AssocQuotient α →ₙ* β) : lift (f.comp of) = f := lift.apply_symm_apply f end lift variable {β : Type v} [Mul β] (f : α →ₙ* β) /-- From a magma homomorphism `α →ₙ* β` to a semigroup homomorphism `Magma.AssocQuotient α →ₙ* Magma.AssocQuotient β`. -/ @[to_additive "From an additive magma homomorphism `α → β` to an additive semigroup homomorphism `AddMagma.AssocQuotient α → AddMagma.AssocQuotient β`."] def map : AssocQuotient α →ₙ* AssocQuotient β := lift (of.comp f) @[to_additive (attr := simp)] theorem map_of (x) : map f (of x) = of (f x) := rfl end AssocQuotient end Magma /-- Free additive semigroup over a given alphabet. -/ structure FreeAddSemigroup (α : Type u) where /-- The head of the element -/ head : α /-- The tail of the element -/ tail : List α compile_inductive% FreeAddSemigroup /-- Free semigroup over a given alphabet. -/ @[to_additive (attr := ext)] structure FreeSemigroup (α : Type u) where /-- The head of the element -/ head : α /-- The tail of the element -/ tail : List α compile_inductive% FreeSemigroup namespace FreeSemigroup variable {α : Type u} @[to_additive] instance : Semigroup (FreeSemigroup α) where mul L1 L2 := ⟨L1.1, L1.2 ++ L2.1 :: L2.2⟩ mul_assoc _L1 _L2 _L3 := FreeSemigroup.ext rfl <| List.append_assoc _ _ _ @[to_additive (attr := simp)] theorem head_mul (x y : FreeSemigroup α) : (x * y).1 = x.1 := rfl @[to_additive (attr := simp)] theorem tail_mul (x y : FreeSemigroup α) : (x * y).2 = x.2 ++ y.1 :: y.2 := rfl @[to_additive (attr := simp)] theorem mk_mul_mk (x y : α) (L1 L2 : List α) : mk x L1 * mk y L2 = mk x (L1 ++ y :: L2) := rfl /-- The embedding `α → FreeSemigroup α`. -/ @[to_additive (attr := simps) "The embedding `α → FreeAddSemigroup α`."] def of (x : α) : FreeSemigroup α := ⟨x, []⟩ /-- Length of an element of free semigroup. -/ @[to_additive "Length of an element of free additive semigroup"] def length (x : FreeSemigroup α) : ℕ := x.tail.length + 1 @[to_additive (attr := simp)] theorem length_mul (x y : FreeSemigroup α) : (x * y).length = x.length + y.length := by simp [length, Nat.add_right_comm, List.length, List.length_append] @[to_additive (attr := simp)] theorem length_of (x : α) : (of x).length = 1 := rfl @[to_additive] instance [Inhabited α] : Inhabited (FreeSemigroup α) := ⟨of default⟩ /-- Recursor for free semigroup using `of` and `*`. -/ @[to_additive (attr := elab_as_elim, induction_eliminator) "Recursor for free additive semigroup using `of` and `+`."] protected def recOnMul {C : FreeSemigroup α → Sort l} (x) (ih1 : ∀ x, C (of x)) (ih2 : ∀ x y, C (of x) → C y → C (of x * y)) : C x := FreeSemigroup.recOn x fun f s ↦ List.recOn s ih1 (fun hd tl ih f ↦ ih2 f ⟨hd, tl⟩ (ih1 f) (ih hd)) f @[to_additive (attr := ext 1100)] theorem hom_ext {β : Type v} [Mul β] {f g : FreeSemigroup α →ₙ* β} (h : f ∘ of = g ∘ of) : f = g := (DFunLike.ext _ _) fun x ↦ FreeSemigroup.recOnMul x (congr_fun h) fun x y hx hy ↦ by simp only [map_mul, *] section lift variable {β : Type v} [Semigroup β] (f : α → β) /-- Lifts a function `α → β` to a semigroup homomorphism `FreeSemigroup α → β` given a semigroup `β`. -/ @[to_additive (attr := simps symm_apply) "Lifts a function `α → β` to an additive semigroup homomorphism `FreeAddSemigroup α → β` given an additive semigroup `β`."] def lift : (α → β) ≃ (FreeSemigroup α →ₙ* β) where toFun f := { toFun := fun x ↦ x.2.foldl (fun a b ↦ a * f b) (f x.1) map_mul' := fun x y ↦ by simp [head_mul, tail_mul, ← List.foldl_map, List.foldl_append, List.foldl_cons, List.foldl_assoc] } invFun f := f ∘ of left_inv f := rfl right_inv f := hom_ext rfl @[to_additive (attr := simp)] theorem lift_of (x : α) : lift f (of x) = f x := rfl @[to_additive (attr := simp)] theorem lift_comp_of : lift f ∘ of = f := rfl @[to_additive (attr := simp)] theorem lift_comp_of' (f : FreeSemigroup α →ₙ* β) : lift (f ∘ of) = f := hom_ext rfl @[to_additive] theorem lift_of_mul (x y) : lift f (of x * y) = f x * lift f y := by rw [map_mul, lift_of] end lift section Map variable {β : Type v} (f : α → β) /-- The unique semigroup homomorphism that sends `of x` to `of (f x)`. -/ @[to_additive "The unique additive semigroup homomorphism that sends `of x` to `of (f x)`."] def map : FreeSemigroup α →ₙ* FreeSemigroup β := lift <| of ∘ f @[to_additive (attr := simp)] theorem map_of (x) : map f (of x) = of (f x) := rfl @[to_additive (attr := simp)] theorem length_map (x) : (map f x).length = x.length := FreeSemigroup.recOnMul x (fun x ↦ rfl) (fun x y hx hy ↦ by simp only [map_mul, length_mul, *]) end Map section Category variable {β : Type u} @[to_additive] instance : Monad FreeSemigroup where pure := of bind x f := lift f x /-- Recursor that uses `pure` instead of `of`. -/ @[to_additive (attr := elab_as_elim) "Recursor that uses `pure` instead of `of`."] def recOnPure {C : FreeSemigroup α → Sort l} (x) (ih1 : ∀ x, C (pure x)) (ih2 : ∀ x y, C (pure x) → C y → C (pure x * y)) : C x := FreeSemigroup.recOnMul x ih1 ih2 @[to_additive (attr := simp)] theorem map_pure (f : α → β) (x) : (f <$> pure x : FreeSemigroup β) = pure (f x) := rfl @[to_additive (attr := simp)] theorem map_mul' (f : α → β) (x y : FreeSemigroup α) : f <$> (x * y) = f <$> x * f <$> y := map_mul (map f) _ _ @[to_additive (attr := simp)] theorem pure_bind (f : α → FreeSemigroup β) (x) : pure x >>= f = f x := rfl @[to_additive (attr := simp)] theorem mul_bind (f : α → FreeSemigroup β) (x y : FreeSemigroup α) : x * y >>= f = (x >>= f) * (y >>= f) := map_mul (lift f) _ _ @[to_additive (attr := simp)] theorem pure_seq {f : α → β} {x : FreeSemigroup α} : pure f <*> x = f <$> x := rfl @[to_additive (attr := simp)] theorem mul_seq {f g : FreeSemigroup (α → β)} {x : FreeSemigroup α} : f * g <*> x = (f <*> x) * (g <*> x) := mul_bind _ _ _ @[to_additive] instance instLawfulMonad : LawfulMonad FreeSemigroup.{u} := LawfulMonad.mk' (pure_bind := fun _ _ ↦ rfl) (bind_assoc := fun x g f ↦ recOnPure x (fun x ↦ rfl) fun x y ih1 ih2 ↦ by rw [mul_bind, mul_bind, mul_bind, ih1, ih2]) (id_map := fun x ↦ recOnPure x (fun _ ↦ rfl) fun x y ih1 ih2 ↦ by rw [map_mul', ih1, ih2]) /-- `FreeSemigroup` is traversable. -/ @[to_additive "`FreeAddSemigroup` is traversable."] protected def traverse {m : Type u → Type u} [Applicative m] {α β : Type u} (F : α → m β) (x : FreeSemigroup α) : m (FreeSemigroup β) := recOnPure x (fun x ↦ pure <$> F x) fun _x _y ihx ihy ↦ (· * ·) <$> ihx <*> ihy @[to_additive] instance : Traversable FreeSemigroup := ⟨@FreeSemigroup.traverse⟩ variable {m : Type u → Type u} [Applicative m] (F : α → m β) @[to_additive (attr := simp)] theorem traverse_pure (x) : traverse F (pure x : FreeSemigroup α) = pure <$> F x := rfl @[to_additive (attr := simp)] theorem traverse_pure' : traverse F ∘ pure = fun x ↦ (pure <$> F x : m (FreeSemigroup β)) := rfl section variable [LawfulApplicative m] @[to_additive (attr := simp)] theorem traverse_mul (x y : FreeSemigroup α) : traverse F (x * y) = (· * ·) <$> traverse F x <*> traverse F y := let ⟨x, L1⟩ := x let ⟨y, L2⟩ := y List.recOn L1 (fun x ↦ rfl) (fun hd tl ih x ↦ show (· * ·) <$> pure <$> F x <*> traverse F (mk hd tl * mk y L2) = (· * ·) <$> ((· * ·) <$> pure <$> F x <*> traverse F (mk hd tl)) <*> traverse F (mk y L2) by rw [ih]; simp only [(· ∘ ·), (mul_assoc _ _ _).symm, functor_norm]) x @[to_additive (attr := simp)] theorem traverse_mul' : Function.comp (traverse F) ∘ (HMul.hMul : FreeSemigroup α → FreeSemigroup α → FreeSemigroup α) = fun x y ↦ (· * ·) <$> traverse F x <*> traverse F y := funext fun x ↦ funext fun y ↦ traverse_mul F x y end @[to_additive (attr := simp)] theorem traverse_eq (x) : FreeSemigroup.traverse F x = traverse F x := rfl -- Porting note (#10675): dsimp can not prove this @[to_additive (attr := simp, nolint simpNF)] theorem mul_map_seq (x y : FreeSemigroup α) : ((· * ·) <$> x <*> y : Id (FreeSemigroup α)) = (x * y : FreeSemigroup α) := rfl @[to_additive] instance : LawfulTraversable FreeSemigroup.{u} := { instLawfulMonad with id_traverse := fun x ↦ FreeSemigroup.recOnMul x (fun x ↦ rfl) fun x y ih1 ih2 ↦ by rw [traverse_mul, ih1, ih2, mul_map_seq] comp_traverse := fun f g x ↦ recOnPure x (fun x ↦ by simp only [traverse_pure, functor_norm, (· ∘ ·)]) fun x y ih1 ih2 ↦ by (rw [traverse_mul, ih1, ih2, traverse_mul, Functor.Comp.map_mk]; simp only [Function.comp, functor_norm, traverse_mul]) naturality := fun η α β f x ↦ recOnPure x (fun x ↦ by simp only [traverse_pure, functor_norm, Function.comp]) (fun x y ih1 ih2 ↦ by simp only [traverse_mul, functor_norm, ih1, ih2]) traverse_eq_map_id := fun f x ↦ FreeSemigroup.recOnMul x (fun _ ↦ rfl) fun x y ih1 ih2 ↦ by rw [traverse_mul, ih1, ih2, map_mul', mul_map_seq]; rfl } end Category @[to_additive] instance [DecidableEq α] : DecidableEq (FreeSemigroup α) := fun _ _ ↦ decidable_of_iff' _ FreeSemigroup.ext_iff end FreeSemigroup namespace FreeMagma variable {α : Type u} {β : Type v} /-- The canonical multiplicative morphism from `FreeMagma α` to `FreeSemigroup α`. -/ @[to_additive "The canonical additive morphism from `FreeAddMagma α` to `FreeAddSemigroup α`."] def toFreeSemigroup : FreeMagma α →ₙ* FreeSemigroup α := FreeMagma.lift FreeSemigroup.of @[to_additive (attr := simp)] theorem toFreeSemigroup_of (x : α) : toFreeSemigroup (of x) = FreeSemigroup.of x := rfl @[to_additive (attr := simp)] theorem toFreeSemigroup_comp_of : @toFreeSemigroup α ∘ of = FreeSemigroup.of := rfl @[to_additive] theorem toFreeSemigroup_comp_map (f : α → β) : toFreeSemigroup.comp (map f) = (FreeSemigroup.map f).comp toFreeSemigroup := by ext1; rfl @[to_additive] theorem toFreeSemigroup_map (f : α → β) (x : FreeMagma α) : toFreeSemigroup (map f x) = FreeSemigroup.map f (toFreeSemigroup x) := DFunLike.congr_fun (toFreeSemigroup_comp_map f) x @[to_additive (attr := simp)] theorem length_toFreeSemigroup (x : FreeMagma α) : (toFreeSemigroup x).length = x.length := FreeMagma.recOnMul x (fun x ↦ rfl) fun x y hx hy ↦ by rw [map_mul, FreeSemigroup.length_mul, hx, hy]; rfl end FreeMagma /-- Isomorphism between `Magma.AssocQuotient (FreeMagma α)` and `FreeSemigroup α`. -/ @[to_additive "Isomorphism between `AddMagma.AssocQuotient (FreeAddMagma α)` and `FreeAddSemigroup α`."] def FreeMagmaAssocQuotientEquiv (α : Type u) : Magma.AssocQuotient (FreeMagma α) ≃* FreeSemigroup α := (Magma.AssocQuotient.lift FreeMagma.toFreeSemigroup).toMulEquiv (FreeSemigroup.lift (Magma.AssocQuotient.of ∘ FreeMagma.of)) (by ext; rfl) (by ext1; rfl)
Algebra\FreeAlgebra.lean
/- Copyright (c) 2020 Adam Topaz. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Adam Topaz, Eric Wieser -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Algebra.Algebra.Tower import Mathlib.Algebra.MonoidAlgebra.NoZeroDivisors import Mathlib.RingTheory.Adjoin.Basic /-! # Free Algebras Given a commutative semiring `R`, and a type `X`, we construct the free unital, associative `R`-algebra on `X`. ## Notation 1. `FreeAlgebra R X` is the free algebra itself. It is endowed with an `R`-algebra structure. 2. `FreeAlgebra.ι R` is the function `X → FreeAlgebra R X`. 3. Given a function `f : X → A` to an R-algebra `A`, `lift R f` is the lift of `f` to an `R`-algebra morphism `FreeAlgebra R X → A`. ## Theorems 1. `ι_comp_lift` states that the composition `(lift R f) ∘ (ι R)` is identical to `f`. 2. `lift_unique` states that whenever an R-algebra morphism `g : FreeAlgebra R X → A` is given whose composition with `ι R` is `f`, then one has `g = lift R f`. 3. `hom_ext` is a variant of `lift_unique` in the form of an extensionality theorem. 4. `lift_comp_ι` is a combination of `ι_comp_lift` and `lift_unique`. It states that the lift of the composition of an algebra morphism with `ι` is the algebra morphism itself. 5. `equivMonoidAlgebraFreeMonoid : FreeAlgebra R X ≃ₐ[R] MonoidAlgebra R (FreeMonoid X)` 6. An inductive principle `induction`. ## Implementation details We construct the free algebra on `X` as a quotient of an inductive type `FreeAlgebra.Pre` by an inductively defined relation `FreeAlgebra.Rel`. Explicitly, the construction involves three steps: 1. We construct an inductive type `FreeAlgebra.Pre R X`, the terms of which should be thought of as representatives for the elements of `FreeAlgebra R X`. It is the free type with maps from `R` and `X`, and with two binary operations `add` and `mul`. 2. We construct an inductive relation `FreeAlgebra.Rel R X` on `FreeAlgebra.Pre R X`. This is the smallest relation for which the quotient is an `R`-algebra where addition resp. multiplication are induced by `add` resp. `mul` from 1., and for which the map from `R` is the structure map for the algebra. 3. The free algebra `FreeAlgebra R X` is the quotient of `FreeAlgebra.Pre R X` by the relation `FreeAlgebra.Rel R X`. -/ variable (R : Type*) [CommSemiring R] variable (X : Type*) namespace FreeAlgebra /-- This inductive type is used to express representatives of the free algebra. -/ inductive Pre | of : X → Pre | ofScalar : R → Pre | add : Pre → Pre → Pre | mul : Pre → Pre → Pre namespace Pre instance : Inhabited (Pre R X) := ⟨ofScalar 0⟩ -- Note: These instances are only used to simplify the notation. /-- Coercion from `X` to `Pre R X`. Note: Used for notation only. -/ def hasCoeGenerator : Coe X (Pre R X) := ⟨of⟩ /-- Coercion from `R` to `Pre R X`. Note: Used for notation only. -/ def hasCoeSemiring : Coe R (Pre R X) := ⟨ofScalar⟩ /-- Multiplication in `Pre R X` defined as `Pre.mul`. Note: Used for notation only. -/ def hasMul : Mul (Pre R X) := ⟨mul⟩ /-- Addition in `Pre R X` defined as `Pre.add`. Note: Used for notation only. -/ def hasAdd : Add (Pre R X) := ⟨add⟩ /-- Zero in `Pre R X` defined as the image of `0` from `R`. Note: Used for notation only. -/ def hasZero : Zero (Pre R X) := ⟨ofScalar 0⟩ /-- One in `Pre R X` defined as the image of `1` from `R`. Note: Used for notation only. -/ def hasOne : One (Pre R X) := ⟨ofScalar 1⟩ /-- Scalar multiplication defined as multiplication by the image of elements from `R`. Note: Used for notation only. -/ def hasSMul : SMul R (Pre R X) := ⟨fun r m ↦ mul (ofScalar r) m⟩ end Pre attribute [local instance] Pre.hasCoeGenerator Pre.hasCoeSemiring Pre.hasMul Pre.hasAdd Pre.hasZero Pre.hasOne Pre.hasSMul /-- Given a function from `X` to an `R`-algebra `A`, `lift_fun` provides a lift of `f` to a function from `Pre R X` to `A`. This is mainly used in the construction of `FreeAlgebra.lift`. -/ -- Porting note: recOn was replaced to preserve computability, see lean4#2049 def liftFun {A : Type*} [Semiring A] [Algebra R A] (f : X → A) : Pre R X → A | .of t => f t | .add a b => liftFun f a + liftFun f b | .mul a b => liftFun f a * liftFun f b | .ofScalar c => algebraMap _ _ c /-- An inductively defined relation on `Pre R X` used to force the initial algebra structure on the associated quotient. -/ inductive Rel : Pre R X → Pre R X → Prop -- force `ofScalar` to be a central semiring morphism | add_scalar {r s : R} : Rel (↑(r + s)) (↑r + ↑s) | mul_scalar {r s : R} : Rel (↑(r * s)) (↑r * ↑s) | central_scalar {r : R} {a : Pre R X} : Rel (r * a) (a * r) -- commutative additive semigroup | add_assoc {a b c : Pre R X} : Rel (a + b + c) (a + (b + c)) | add_comm {a b : Pre R X} : Rel (a + b) (b + a) | zero_add {a : Pre R X} : Rel (0 + a) a -- multiplicative monoid | mul_assoc {a b c : Pre R X} : Rel (a * b * c) (a * (b * c)) | one_mul {a : Pre R X} : Rel (1 * a) a | mul_one {a : Pre R X} : Rel (a * 1) a -- distributivity | left_distrib {a b c : Pre R X} : Rel (a * (b + c)) (a * b + a * c) | right_distrib {a b c : Pre R X} : Rel ((a + b) * c) (a * c + b * c) -- other relations needed for semiring | zero_mul {a : Pre R X} : Rel (0 * a) 0 | mul_zero {a : Pre R X} : Rel (a * 0) 0 -- compatibility | add_compat_left {a b c : Pre R X} : Rel a b → Rel (a + c) (b + c) | add_compat_right {a b c : Pre R X} : Rel a b → Rel (c + a) (c + b) | mul_compat_left {a b c : Pre R X} : Rel a b → Rel (a * c) (b * c) | mul_compat_right {a b c : Pre R X} : Rel a b → Rel (c * a) (c * b) end FreeAlgebra /-- The free algebra for the type `X` over the commutative semiring `R`. -/ def FreeAlgebra := Quot (FreeAlgebra.Rel R X) namespace FreeAlgebra attribute [local instance] Pre.hasCoeGenerator Pre.hasCoeSemiring Pre.hasMul Pre.hasAdd Pre.hasZero Pre.hasOne Pre.hasSMul /-! Define the basic operations-/ instance instSMul {A} [CommSemiring A] [Algebra R A] : SMul R (FreeAlgebra A X) where smul r := Quot.map (HMul.hMul (algebraMap R A r : Pre A X)) fun _ _ ↦ Rel.mul_compat_right instance instZero : Zero (FreeAlgebra R X) where zero := Quot.mk _ 0 instance instOne : One (FreeAlgebra R X) where one := Quot.mk _ 1 instance instAdd : Add (FreeAlgebra R X) where add := Quot.map₂ HAdd.hAdd (fun _ _ _ ↦ Rel.add_compat_right) fun _ _ _ ↦ Rel.add_compat_left instance instMul : Mul (FreeAlgebra R X) where mul := Quot.map₂ HMul.hMul (fun _ _ _ ↦ Rel.mul_compat_right) fun _ _ _ ↦ Rel.mul_compat_left -- `Quot.mk` is an implementation detail of `FreeAlgebra`, so this lemma is private private theorem mk_mul (x y : Pre R X) : Quot.mk (Rel R X) (x * y) = (HMul.hMul (self := instHMul (α := FreeAlgebra R X)) (Quot.mk (Rel R X) x) (Quot.mk (Rel R X) y)) := rfl /-! Build the semiring structure. We do this one piece at a time as this is convenient for proving the `nsmul` fields. -/ instance instMonoidWithZero : MonoidWithZero (FreeAlgebra R X) where mul_assoc := by rintro ⟨⟩ ⟨⟩ ⟨⟩ exact Quot.sound Rel.mul_assoc one := Quot.mk _ 1 one_mul := by rintro ⟨⟩ exact Quot.sound Rel.one_mul mul_one := by rintro ⟨⟩ exact Quot.sound Rel.mul_one zero_mul := by rintro ⟨⟩ exact Quot.sound Rel.zero_mul mul_zero := by rintro ⟨⟩ exact Quot.sound Rel.mul_zero instance instDistrib : Distrib (FreeAlgebra R X) where left_distrib := by rintro ⟨⟩ ⟨⟩ ⟨⟩ exact Quot.sound Rel.left_distrib right_distrib := by rintro ⟨⟩ ⟨⟩ ⟨⟩ exact Quot.sound Rel.right_distrib instance instAddCommMonoid : AddCommMonoid (FreeAlgebra R X) where add_assoc := by rintro ⟨⟩ ⟨⟩ ⟨⟩ exact Quot.sound Rel.add_assoc zero_add := by rintro ⟨⟩ exact Quot.sound Rel.zero_add add_zero := by rintro ⟨⟩ change Quot.mk _ _ = _ rw [Quot.sound Rel.add_comm, Quot.sound Rel.zero_add] add_comm := by rintro ⟨⟩ ⟨⟩ exact Quot.sound Rel.add_comm nsmul := (· • ·) nsmul_zero := by rintro ⟨⟩ change Quot.mk _ (_ * _) = _ rw [map_zero] exact Quot.sound Rel.zero_mul nsmul_succ n := by rintro ⟨a⟩ dsimp only [HSMul.hSMul, instSMul, Quot.map] rw [map_add, map_one, mk_mul, mk_mul, ← add_one_mul (_ : FreeAlgebra R X)] congr 1 exact Quot.sound Rel.add_scalar instance : Semiring (FreeAlgebra R X) where __ := instMonoidWithZero R X __ := instAddCommMonoid R X __ := instDistrib R X natCast n := Quot.mk _ (n : R) natCast_zero := by simp; rfl natCast_succ n := by simpa using Quot.sound Rel.add_scalar instance : Inhabited (FreeAlgebra R X) := ⟨0⟩ instance instAlgebra {A} [CommSemiring A] [Algebra R A] : Algebra R (FreeAlgebra A X) where toRingHom := ({ toFun := fun r => Quot.mk _ r map_one' := rfl map_mul' := fun _ _ => Quot.sound Rel.mul_scalar map_zero' := rfl map_add' := fun _ _ => Quot.sound Rel.add_scalar } : A →+* FreeAlgebra A X).comp (algebraMap R A) commutes' _ := by rintro ⟨⟩ exact Quot.sound Rel.central_scalar smul_def' _ _ := rfl -- verify there is no diamond at `default` transparency but we will need -- `reducible_and_instances` which currently fails #10906 variable (S : Type) [CommSemiring S] in example : (Semiring.toNatAlgebra : Algebra ℕ (FreeAlgebra S X)) = instAlgebra _ _ := rfl instance {R S A} [CommSemiring R] [CommSemiring S] [CommSemiring A] [SMul R S] [Algebra R A] [Algebra S A] [IsScalarTower R S A] : IsScalarTower R S (FreeAlgebra A X) where smul_assoc r s x := by change algebraMap S A (r • s) • x = algebraMap R A _ • (algebraMap S A _ • x) rw [← smul_assoc] congr simp only [Algebra.algebraMap_eq_smul_one, smul_eq_mul] rw [smul_assoc, ← smul_one_mul] instance {R S A} [CommSemiring R] [CommSemiring S] [CommSemiring A] [Algebra R A] [Algebra S A] : SMulCommClass R S (FreeAlgebra A X) where smul_comm r s x := smul_comm (algebraMap R A r) (algebraMap S A s) x instance {S : Type*} [CommRing S] : Ring (FreeAlgebra S X) := Algebra.semiringToRing S -- verify there is no diamond but we will need -- `reducible_and_instances` which currently fails #10906 variable (S : Type) [CommRing S] in example : (Ring.toIntAlgebra _ : Algebra ℤ (FreeAlgebra S X)) = instAlgebra _ _ := rfl variable {X} /-- The canonical function `X → FreeAlgebra R X`. -/ irreducible_def ι : X → FreeAlgebra R X := fun m ↦ Quot.mk _ m @[simp] theorem quot_mk_eq_ι (m : X) : Quot.mk (FreeAlgebra.Rel R X) m = ι R m := by rw [ι_def] variable {A : Type*} [Semiring A] [Algebra R A] /-- Internal definition used to define `lift` -/ private def liftAux (f : X → A) : FreeAlgebra R X →ₐ[R] A where toFun a := Quot.liftOn a (liftFun _ _ f) fun a b h ↦ by induction' h · exact (algebraMap R A).map_add _ _ · exact (algebraMap R A).map_mul _ _ · apply Algebra.commutes · change _ + _ + _ = _ + (_ + _) rw [add_assoc] · change _ + _ = _ + _ rw [add_comm] · change algebraMap _ _ _ + liftFun R X f _ = liftFun R X f _ simp · change _ * _ * _ = _ * (_ * _) rw [mul_assoc] · change algebraMap _ _ _ * liftFun R X f _ = liftFun R X f _ simp · change liftFun R X f _ * algebraMap _ _ _ = liftFun R X f _ simp · change _ * (_ + _) = _ * _ + _ * _ rw [left_distrib] · change (_ + _) * _ = _ * _ + _ * _ rw [right_distrib] · change algebraMap _ _ _ * _ = algebraMap _ _ _ simp · change _ * algebraMap _ _ _ = algebraMap _ _ _ simp repeat change liftFun R X f _ + liftFun R X f _ = _ simp only [*] rfl repeat change liftFun R X f _ * liftFun R X f _ = _ simp only [*] rfl map_one' := by change algebraMap _ _ _ = _ simp map_mul' := by rintro ⟨⟩ ⟨⟩ rfl map_zero' := by dsimp change algebraMap _ _ _ = _ simp map_add' := by rintro ⟨⟩ ⟨⟩ rfl commutes' := by tauto /-- Given a function `f : X → A` where `A` is an `R`-algebra, `lift R f` is the unique lift of `f` to a morphism of `R`-algebras `FreeAlgebra R X → A`. -/ @[irreducible] def lift : (X → A) ≃ (FreeAlgebra R X →ₐ[R] A) := { toFun := liftAux R invFun := fun F ↦ F ∘ ι R left_inv := fun f ↦ by ext simp only [Function.comp_apply, ι_def] rfl right_inv := fun F ↦ by ext t rcases t with ⟨x⟩ induction x with | of => change ((F : FreeAlgebra R X → A) ∘ ι R) _ = _ simp only [Function.comp_apply, ι_def] | ofScalar x => change algebraMap _ _ x = F (algebraMap _ _ x) rw [AlgHom.commutes F _] | add a b ha hb => -- Porting note: it is necessary to declare fa and fb explicitly otherwise Lean refuses -- to consider `Quot.mk (Rel R X) ·` as element of FreeAlgebra R X let fa : FreeAlgebra R X := Quot.mk (Rel R X) a let fb : FreeAlgebra R X := Quot.mk (Rel R X) b change liftAux R (F ∘ ι R) (fa + fb) = F (fa + fb) rw [map_add, map_add, ha, hb] | mul a b ha hb => let fa : FreeAlgebra R X := Quot.mk (Rel R X) a let fb : FreeAlgebra R X := Quot.mk (Rel R X) b change liftAux R (F ∘ ι R) (fa * fb) = F (fa * fb) rw [map_mul, map_mul, ha, hb] } @[simp] theorem liftAux_eq (f : X → A) : liftAux R f = lift R f := by rw [lift] rfl @[simp] theorem lift_symm_apply (F : FreeAlgebra R X →ₐ[R] A) : (lift R).symm F = F ∘ ι R := by rw [lift] rfl variable {R} @[simp] theorem ι_comp_lift (f : X → A) : (lift R f : FreeAlgebra R X → A) ∘ ι R = f := by ext rw [Function.comp_apply, ι_def, lift] rfl @[simp] theorem lift_ι_apply (f : X → A) (x) : lift R f (ι R x) = f x := by rw [ι_def, lift] rfl @[simp] theorem lift_unique (f : X → A) (g : FreeAlgebra R X →ₐ[R] A) : (g : FreeAlgebra R X → A) ∘ ι R = f ↔ g = lift R f := by rw [← (lift R).symm_apply_eq, lift] rfl /-! Since we have set the basic definitions as `@[Irreducible]`, from this point onwards one should only use the universal properties of the free algebra, and consider the actual implementation as a quotient of an inductive type as completely hidden. -/ -- Marking `FreeAlgebra` irreducible makes `Ring` instances inaccessible on quotients. -- https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/algebra.2Esemiring_to_ring.20breaks.20semimodule.20typeclass.20lookup/near/212580241 -- For now, we avoid this by not marking it irreducible. @[simp] theorem lift_comp_ι (g : FreeAlgebra R X →ₐ[R] A) : lift R ((g : FreeAlgebra R X → A) ∘ ι R) = g := by rw [← lift_symm_apply] exact (lift R).apply_symm_apply g /-- See note [partially-applied ext lemmas]. -/ @[ext high] theorem hom_ext {f g : FreeAlgebra R X →ₐ[R] A} (w : (f : FreeAlgebra R X → A) ∘ ι R = (g : FreeAlgebra R X → A) ∘ ι R) : f = g := by rw [← lift_symm_apply, ← lift_symm_apply] at w exact (lift R).symm.injective w /-- The free algebra on `X` is "just" the monoid algebra on the free monoid on `X`. This would be useful when constructing linear maps out of a free algebra, for example. -/ noncomputable def equivMonoidAlgebraFreeMonoid : FreeAlgebra R X ≃ₐ[R] MonoidAlgebra R (FreeMonoid X) := AlgEquiv.ofAlgHom (lift R fun x ↦ (MonoidAlgebra.of R (FreeMonoid X)) (FreeMonoid.of x)) ((MonoidAlgebra.lift R (FreeMonoid X) (FreeAlgebra R X)) (FreeMonoid.lift (ι R))) (by apply MonoidAlgebra.algHom_ext; intro x refine FreeMonoid.recOn x ?_ ?_ · simp rfl · intro x y ih simp at ih simp [ih]) (by ext simp) /-- `FreeAlgebra R X` is nontrivial when `R` is. -/ instance [Nontrivial R] : Nontrivial (FreeAlgebra R X) := equivMonoidAlgebraFreeMonoid.surjective.nontrivial /-- `FreeAlgebra R X` has no zero-divisors when `R` has no zero-divisors. -/ instance instNoZeroDivisors [NoZeroDivisors R] : NoZeroDivisors (FreeAlgebra R X) := equivMonoidAlgebraFreeMonoid.toMulEquiv.noZeroDivisors /-- `FreeAlgebra R X` is a domain when `R` is an integral domain. -/ instance instIsDomain {R X} [CommRing R] [IsDomain R] : IsDomain (FreeAlgebra R X) := NoZeroDivisors.to_isDomain _ section /-- The left-inverse of `algebraMap`. -/ def algebraMapInv : FreeAlgebra R X →ₐ[R] R := lift R (0 : X → R) theorem algebraMap_leftInverse : Function.LeftInverse algebraMapInv (algebraMap R <| FreeAlgebra R X) := fun x ↦ by simp [algebraMapInv] @[simp] theorem algebraMap_inj (x y : R) : algebraMap R (FreeAlgebra R X) x = algebraMap R (FreeAlgebra R X) y ↔ x = y := algebraMap_leftInverse.injective.eq_iff @[simp] theorem algebraMap_eq_zero_iff (x : R) : algebraMap R (FreeAlgebra R X) x = 0 ↔ x = 0 := map_eq_zero_iff (algebraMap _ _) algebraMap_leftInverse.injective @[simp] theorem algebraMap_eq_one_iff (x : R) : algebraMap R (FreeAlgebra R X) x = 1 ↔ x = 1 := map_eq_one_iff (algebraMap _ _) algebraMap_leftInverse.injective -- this proof is copied from the approach in `FreeAbelianGroup.of_injective` theorem ι_injective [Nontrivial R] : Function.Injective (ι R : X → FreeAlgebra R X) := fun x y hoxy ↦ by_contradiction <| by classical exact fun hxy : x ≠ y ↦ let f : FreeAlgebra R X →ₐ[R] R := lift R fun z ↦ if x = z then (1 : R) else 0 have hfx1 : f (ι R x) = 1 := (lift_ι_apply _ _).trans <| if_pos rfl have hfy1 : f (ι R y) = 1 := hoxy ▸ hfx1 have hfy0 : f (ι R y) = 0 := (lift_ι_apply _ _).trans <| if_neg hxy one_ne_zero <| hfy1.symm.trans hfy0 @[simp] theorem ι_inj [Nontrivial R] (x y : X) : ι R x = ι R y ↔ x = y := ι_injective.eq_iff @[simp] theorem ι_ne_algebraMap [Nontrivial R] (x : X) (r : R) : ι R x ≠ algebraMap R _ r := fun h ↦ by let f0 : FreeAlgebra R X →ₐ[R] R := lift R 0 let f1 : FreeAlgebra R X →ₐ[R] R := lift R 1 have hf0 : f0 (ι R x) = 0 := lift_ι_apply _ _ have hf1 : f1 (ι R x) = 1 := lift_ι_apply _ _ rw [h, f0.commutes, Algebra.id.map_eq_self] at hf0 rw [h, f1.commutes, Algebra.id.map_eq_self] at hf1 exact zero_ne_one (hf0.symm.trans hf1) @[simp] theorem ι_ne_zero [Nontrivial R] (x : X) : ι R x ≠ 0 := ι_ne_algebraMap x 0 @[simp] theorem ι_ne_one [Nontrivial R] (x : X) : ι R x ≠ 1 := ι_ne_algebraMap x 1 end end FreeAlgebra /- There is something weird in the above namespace that breaks the typeclass resolution of `CoeSort` below. Closing it and reopening it fixes it... -/ namespace FreeAlgebra /-- An induction principle for the free algebra. If `C` holds for the `algebraMap` of `r : R` into `FreeAlgebra R X`, the `ι` of `x : X`, and is preserved under addition and muliplication, then it holds for all of `FreeAlgebra R X`. -/ @[elab_as_elim, induction_eliminator] theorem induction {C : FreeAlgebra R X → Prop} (h_grade0 : ∀ r, C (algebraMap R (FreeAlgebra R X) r)) (h_grade1 : ∀ x, C (ι R x)) (h_mul : ∀ a b, C a → C b → C (a * b)) (h_add : ∀ a b, C a → C b → C (a + b)) (a : FreeAlgebra R X) : C a := by -- the arguments are enough to construct a subalgebra, and a mapping into it from X let s : Subalgebra R (FreeAlgebra R X) := { carrier := C mul_mem' := h_mul _ _ add_mem' := h_add _ _ algebraMap_mem' := h_grade0 } let of : X → s := Subtype.coind (ι R) h_grade1 -- the mapping through the subalgebra is the identity have of_id : AlgHom.id R (FreeAlgebra R X) = s.val.comp (lift R of) := by ext simp [of, Subtype.coind] -- finding a proof is finding an element of the subalgebra suffices a = lift R of a by rw [this] exact Subtype.prop (lift R of a) simp [AlgHom.ext_iff] at of_id exact of_id a @[simp] theorem adjoin_range_ι : Algebra.adjoin R (Set.range (ι R : X → FreeAlgebra R X)) = ⊤ := by set S := Algebra.adjoin R (Set.range (ι R : X → FreeAlgebra R X)) refine top_unique fun x hx => ?_; clear hx induction x with | h_grade0 => exact S.algebraMap_mem _ | h_add x y hx hy => exact S.add_mem hx hy | h_mul x y hx hy => exact S.mul_mem hx hy | h_grade1 x => exact Algebra.subset_adjoin (Set.mem_range_self _) variable {A : Type*} [Semiring A] [Algebra R A] /-- Noncommutative version of `Algebra.adjoin_range_eq_range_aeval`. -/ theorem _root_.Algebra.adjoin_range_eq_range_freeAlgebra_lift (f : X → A) : Algebra.adjoin R (Set.range f) = (FreeAlgebra.lift R f).range := by simp only [← Algebra.map_top, ← adjoin_range_ι, AlgHom.map_adjoin, ← Set.range_comp, (· ∘ ·), lift_ι_apply] /-- Noncommutative version of `Algebra.adjoin_range_eq_range`. -/ theorem _root_.Algebra.adjoin_eq_range_freeAlgebra_lift (s : Set A) : Algebra.adjoin R s = (FreeAlgebra.lift R ((↑) : s → A)).range := by rw [← Algebra.adjoin_range_eq_range_freeAlgebra_lift, Subtype.range_coe] end FreeAlgebra
Algebra\FreeNonUnitalNonAssocAlgebra.lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Algebra.Free import Mathlib.Algebra.MonoidAlgebra.Basic /-! # Free algebras Given a semiring `R` and a type `X`, we construct the free non-unital, non-associative algebra on `X` with coefficients in `R`, together with its universal property. The construction is valuable because it can be used to build free algebras with more structure, e.g., free Lie algebras. Note that elsewhere we have a construction of the free unital, associative algebra. This is called `FreeAlgebra`. ## Main definitions * `FreeNonUnitalNonAssocAlgebra` * `FreeNonUnitalNonAssocAlgebra.lift` * `FreeNonUnitalNonAssocAlgebra.of` ## Implementation details We construct the free algebra as the magma algebra, with coefficients in `R`, of the free magma on `X`. However we regard this as an implementation detail and thus deliberately omit the lemmas `of_apply` and `lift_apply`, and we mark `FreeNonUnitalNonAssocAlgebra` and `lift` as irreducible once we have established the universal property. ## Tags free algebra, non-unital, non-associative, free magma, magma algebra, universal property, forgetful functor, adjoint functor -/ universe u v w noncomputable section variable (R : Type u) (X : Type v) [Semiring R] /-- The free non-unital, non-associative algebra on the type `X` with coefficients in `R`. -/ abbrev FreeNonUnitalNonAssocAlgebra := MonoidAlgebra R (FreeMagma X) namespace FreeNonUnitalNonAssocAlgebra variable {X} /-- The embedding of `X` into the free algebra with coefficients in `R`. -/ def of : X → FreeNonUnitalNonAssocAlgebra R X := MonoidAlgebra.ofMagma R _ ∘ FreeMagma.of variable {A : Type w} [NonUnitalNonAssocSemiring A] variable [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] /-- The functor `X ↦ FreeNonUnitalNonAssocAlgebra R X` from the category of types to the category of non-unital, non-associative algebras over `R` is adjoint to the forgetful functor in the other direction. -/ def lift : (X → A) ≃ (FreeNonUnitalNonAssocAlgebra R X →ₙₐ[R] A) := FreeMagma.lift.trans (MonoidAlgebra.liftMagma R) @[simp] theorem lift_symm_apply (F : FreeNonUnitalNonAssocAlgebra R X →ₙₐ[R] A) : (lift R).symm F = F ∘ of R := rfl @[simp] theorem of_comp_lift (f : X → A) : lift R f ∘ of R = f := (lift R).left_inv f @[simp] theorem lift_unique (f : X → A) (F : FreeNonUnitalNonAssocAlgebra R X →ₙₐ[R] A) : F ∘ of R = f ↔ F = lift R f := (lift R).symm_apply_eq @[simp] theorem lift_of_apply (f : X → A) (x) : lift R f (of R x) = f x := congr_fun (of_comp_lift _ f) x @[simp] theorem lift_comp_of (F : FreeNonUnitalNonAssocAlgebra R X →ₙₐ[R] A) : lift R (F ∘ of R) = F := (lift R).apply_symm_apply F @[ext] theorem hom_ext {F₁ F₂ : FreeNonUnitalNonAssocAlgebra R X →ₙₐ[R] A} (h : ∀ x, F₁ (of R x) = F₂ (of R x)) : F₁ = F₂ := (lift R).symm.injective <| funext h end FreeNonUnitalNonAssocAlgebra
Algebra\GeomSum.lean
/- Copyright (c) 2019 Neil Strickland. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Neil Strickland -/ import Mathlib.Algebra.BigOperators.Intervals import Mathlib.Algebra.BigOperators.Ring import Mathlib.Algebra.Order.BigOperators.Ring.Finset import Mathlib.Algebra.Order.Field.Basic import Mathlib.Algebra.Order.Ring.Abs import Mathlib.Algebra.Ring.Opposite import Mathlib.Tactic.Abel import Mathlib.Algebra.Ring.Regular /-! # Partial sums of geometric series This file determines the values of the geometric series $\sum_{i=0}^{n-1} x^i$ and $\sum_{i=0}^{n-1} x^i y^{n-1-i}$ and variants thereof. We also provide some bounds on the "geometric" sum of `a/b^i` where `a b : ℕ`. ## Main statements * `geom_sum_Ico` proves that $\sum_{i=m}^{n-1} x^i=\frac{x^n-x^m}{x-1}$ in a division ring. * `geom_sum₂_Ico` proves that $\sum_{i=m}^{n-1} x^iy^{n - 1 - i}=\frac{x^n-y^{n-m}x^m}{x-y}$ in a field. Several variants are recorded, generalising in particular to the case of a noncommutative ring in which `x` and `y` commute. Even versions not using division or subtraction, valid in each semiring, are recorded. -/ -- Porting note: corrected type in the description of `geom_sum₂_Ico` (in the doc string only). universe u variable {α : Type u} open Finset MulOpposite section Semiring variable [Semiring α] theorem geom_sum_succ {x : α} {n : ℕ} : ∑ i ∈ range (n + 1), x ^ i = (x * ∑ i ∈ range n, x ^ i) + 1 := by simp only [mul_sum, ← pow_succ', sum_range_succ', pow_zero] theorem geom_sum_succ' {x : α} {n : ℕ} : ∑ i ∈ range (n + 1), x ^ i = x ^ n + ∑ i ∈ range n, x ^ i := (sum_range_succ _ _).trans (add_comm _ _) theorem geom_sum_zero (x : α) : ∑ i ∈ range 0, x ^ i = 0 := rfl theorem geom_sum_one (x : α) : ∑ i ∈ range 1, x ^ i = 1 := by simp [geom_sum_succ'] @[simp] theorem geom_sum_two {x : α} : ∑ i ∈ range 2, x ^ i = x + 1 := by simp [geom_sum_succ'] @[simp] theorem zero_geom_sum : ∀ {n}, ∑ i ∈ range n, (0 : α) ^ i = if n = 0 then 0 else 1 | 0 => by simp | 1 => by simp | n + 2 => by rw [geom_sum_succ'] simp [zero_geom_sum] theorem one_geom_sum (n : ℕ) : ∑ i ∈ range n, (1 : α) ^ i = n := by simp -- porting note (#10618): simp can prove this -- @[simp] theorem op_geom_sum (x : α) (n : ℕ) : op (∑ i ∈ range n, x ^ i) = ∑ i ∈ range n, op x ^ i := by simp -- Porting note: linter suggested to change left hand side @[simp] theorem op_geom_sum₂ (x y : α) (n : ℕ) : ∑ i ∈ range n, op y ^ (n - 1 - i) * op x ^ i = ∑ i ∈ range n, op y ^ i * op x ^ (n - 1 - i) := by rw [← sum_range_reflect] refine sum_congr rfl fun j j_in => ?_ rw [mem_range, Nat.lt_iff_add_one_le] at j_in congr apply tsub_tsub_cancel_of_le exact le_tsub_of_add_le_right j_in theorem geom_sum₂_with_one (x : α) (n : ℕ) : ∑ i ∈ range n, x ^ i * 1 ^ (n - 1 - i) = ∑ i ∈ range n, x ^ i := sum_congr rfl fun i _ => by rw [one_pow, mul_one] /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ protected theorem Commute.geom_sum₂_mul_add {x y : α} (h : Commute x y) (n : ℕ) : (∑ i ∈ range n, (x + y) ^ i * y ^ (n - 1 - i)) * x + y ^ n = (x + y) ^ n := by let f : ℕ → ℕ → α := fun m i : ℕ => (x + y) ^ i * y ^ (m - 1 - i) change (∑ i ∈ range n, (f n) i) * x + y ^ n = (x + y) ^ n induction n with | zero => rw [range_zero, sum_empty, zero_mul, zero_add, pow_zero, pow_zero] | succ n ih => have f_last : f (n + 1) n = (x + y) ^ n := by dsimp only [f] rw [← tsub_add_eq_tsub_tsub, Nat.add_comm, tsub_self, pow_zero, mul_one] have f_succ : ∀ i, i ∈ range n → f (n + 1) i = y * f n i := fun i hi => by dsimp only [f] have : Commute y ((x + y) ^ i) := (h.symm.add_right (Commute.refl y)).pow_right i rw [← mul_assoc, this.eq, mul_assoc, ← pow_succ' y (n - 1 - i), add_tsub_cancel_right, ← tsub_add_eq_tsub_tsub, add_comm 1 i] have : i + 1 + (n - (i + 1)) = n := add_tsub_cancel_of_le (mem_range.mp hi) rw [add_comm (i + 1)] at this rw [← this, add_tsub_cancel_right, add_comm i 1, ← add_assoc, add_tsub_cancel_right] rw [pow_succ' (x + y), add_mul, sum_range_succ_comm, add_mul, f_last, add_assoc, (((Commute.refl x).add_right h).pow_right n).eq, sum_congr rfl f_succ, ← mul_sum, pow_succ' y, mul_assoc, ← mul_add y, ih] end Semiring @[simp] theorem neg_one_geom_sum [Ring α] {n : ℕ} : ∑ i ∈ range n, (-1 : α) ^ i = if Even n then 0 else 1 := by induction n with | zero => simp | succ k hk => simp only [geom_sum_succ', Nat.even_add_one, hk] split_ifs with h · rw [h.neg_one_pow, add_zero] · rw [(Nat.odd_iff_not_even.2 h).neg_one_pow, neg_add_self] theorem geom_sum₂_self {α : Type*} [CommRing α] (x : α) (n : ℕ) : ∑ i ∈ range n, x ^ i * x ^ (n - 1 - i) = n * x ^ (n - 1) := calc ∑ i ∈ Finset.range n, x ^ i * x ^ (n - 1 - i) = ∑ i ∈ Finset.range n, x ^ (i + (n - 1 - i)) := by simp_rw [← pow_add] _ = ∑ _i ∈ Finset.range n, x ^ (n - 1) := Finset.sum_congr rfl fun i hi => congr_arg _ <| add_tsub_cancel_of_le <| Nat.le_sub_one_of_lt <| Finset.mem_range.1 hi _ = (Finset.range n).card • x ^ (n - 1) := Finset.sum_const _ _ = n * x ^ (n - 1) := by rw [Finset.card_range, nsmul_eq_mul] /-- $x^n-y^n = (x-y) \sum x^ky^{n-1-k}$ reformulated without `-` signs. -/ theorem geom_sum₂_mul_add [CommSemiring α] (x y : α) (n : ℕ) : (∑ i ∈ range n, (x + y) ^ i * y ^ (n - 1 - i)) * x + y ^ n = (x + y) ^ n := (Commute.all x y).geom_sum₂_mul_add n theorem geom_sum_mul_add [Semiring α] (x : α) (n : ℕ) : (∑ i ∈ range n, (x + 1) ^ i) * x + 1 = (x + 1) ^ n := by have := (Commute.one_right x).geom_sum₂_mul_add n rw [one_pow, geom_sum₂_with_one] at this exact this protected theorem Commute.geom_sum₂_mul [Ring α] {x y : α} (h : Commute x y) (n : ℕ) : (∑ i ∈ range n, x ^ i * y ^ (n - 1 - i)) * (x - y) = x ^ n - y ^ n := by have := (h.sub_left (Commute.refl y)).geom_sum₂_mul_add n rw [sub_add_cancel] at this rw [← this, add_sub_cancel_right] theorem Commute.mul_neg_geom_sum₂ [Ring α] {x y : α} (h : Commute x y) (n : ℕ) : ((y - x) * ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i)) = y ^ n - x ^ n := by apply op_injective simp only [op_mul, op_sub, op_geom_sum₂, op_pow] simp [(Commute.op h.symm).geom_sum₂_mul n] theorem Commute.mul_geom_sum₂ [Ring α] {x y : α} (h : Commute x y) (n : ℕ) : ((x - y) * ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i)) = x ^ n - y ^ n := by rw [← neg_sub (y ^ n), ← h.mul_neg_geom_sum₂, ← neg_mul, neg_sub] theorem geom_sum₂_mul [CommRing α] (x y : α) (n : ℕ) : (∑ i ∈ range n, x ^ i * y ^ (n - 1 - i)) * (x - y) = x ^ n - y ^ n := (Commute.all x y).geom_sum₂_mul n theorem Commute.sub_dvd_pow_sub_pow [Ring α] {x y : α} (h : Commute x y) (n : ℕ) : x - y ∣ x ^ n - y ^ n := Dvd.intro _ <| h.mul_geom_sum₂ _ theorem sub_dvd_pow_sub_pow [CommRing α] (x y : α) (n : ℕ) : x - y ∣ x ^ n - y ^ n := (Commute.all x y).sub_dvd_pow_sub_pow n theorem one_sub_dvd_one_sub_pow [Ring α] (x : α) (n : ℕ) : 1 - x ∣ 1 - x ^ n := by conv_rhs => rw [← one_pow n] exact (Commute.one_left x).sub_dvd_pow_sub_pow n theorem sub_one_dvd_pow_sub_one [Ring α] (x : α) (n : ℕ) : x - 1 ∣ x ^ n - 1 := by conv_rhs => rw [← one_pow n] exact (Commute.one_right x).sub_dvd_pow_sub_pow n theorem nat_sub_dvd_pow_sub_pow (x y n : ℕ) : x - y ∣ x ^ n - y ^ n := by rcases le_or_lt y x with h | h · have : y ^ n ≤ x ^ n := Nat.pow_le_pow_left h _ exact mod_cast sub_dvd_pow_sub_pow (x : ℤ) (↑y) n · have : x ^ n ≤ y ^ n := Nat.pow_le_pow_left h.le _ exact (Nat.sub_eq_zero_of_le this).symm ▸ dvd_zero (x - y) theorem Odd.add_dvd_pow_add_pow [CommRing α] (x y : α) {n : ℕ} (h : Odd n) : x + y ∣ x ^ n + y ^ n := by have h₁ := geom_sum₂_mul x (-y) n rw [Odd.neg_pow h y, sub_neg_eq_add, sub_neg_eq_add] at h₁ exact Dvd.intro_left _ h₁ theorem Odd.nat_add_dvd_pow_add_pow (x y : ℕ) {n : ℕ} (h : Odd n) : x + y ∣ x ^ n + y ^ n := mod_cast Odd.add_dvd_pow_add_pow (x : ℤ) (↑y) h theorem geom_sum_mul [Ring α] (x : α) (n : ℕ) : (∑ i ∈ range n, x ^ i) * (x - 1) = x ^ n - 1 := by have := (Commute.one_right x).geom_sum₂_mul n rw [one_pow, geom_sum₂_with_one] at this exact this theorem mul_geom_sum [Ring α] (x : α) (n : ℕ) : ((x - 1) * ∑ i ∈ range n, x ^ i) = x ^ n - 1 := op_injective <| by simpa using geom_sum_mul (op x) n theorem geom_sum_mul_neg [Ring α] (x : α) (n : ℕ) : (∑ i ∈ range n, x ^ i) * (1 - x) = 1 - x ^ n := by have := congr_arg Neg.neg (geom_sum_mul x n) rw [neg_sub, ← mul_neg, neg_sub] at this exact this theorem mul_neg_geom_sum [Ring α] (x : α) (n : ℕ) : ((1 - x) * ∑ i ∈ range n, x ^ i) = 1 - x ^ n := op_injective <| by simpa using geom_sum_mul_neg (op x) n protected theorem Commute.geom_sum₂_comm {α : Type u} [Semiring α] {x y : α} (n : ℕ) (h : Commute x y) : ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) = ∑ i ∈ range n, y ^ i * x ^ (n - 1 - i) := by cases n; · simp simp only [Nat.succ_eq_add_one, Nat.add_sub_cancel] rw [← Finset.sum_flip] refine Finset.sum_congr rfl fun i hi => ?_ simpa [Nat.sub_sub_self (Nat.succ_le_succ_iff.mp (Finset.mem_range.mp hi))] using h.pow_pow _ _ theorem geom_sum₂_comm {α : Type u} [CommSemiring α] (x y : α) (n : ℕ) : ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) = ∑ i ∈ range n, y ^ i * x ^ (n - 1 - i) := (Commute.all x y).geom_sum₂_comm n protected theorem Commute.geom_sum₂ [DivisionRing α] {x y : α} (h' : Commute x y) (h : x ≠ y) (n : ℕ) : ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) = (x ^ n - y ^ n) / (x - y) := by have : x - y ≠ 0 := by simp_all [sub_eq_iff_eq_add] rw [← h'.geom_sum₂_mul, mul_div_cancel_right₀ _ this] theorem geom₂_sum [Field α] {x y : α} (h : x ≠ y) (n : ℕ) : ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) = (x ^ n - y ^ n) / (x - y) := (Commute.all x y).geom_sum₂ h n theorem geom_sum_eq [DivisionRing α] {x : α} (h : x ≠ 1) (n : ℕ) : ∑ i ∈ range n, x ^ i = (x ^ n - 1) / (x - 1) := by have : x - 1 ≠ 0 := by simp_all [sub_eq_iff_eq_add] rw [← geom_sum_mul, mul_div_cancel_right₀ _ this] protected theorem Commute.mul_geom_sum₂_Ico [Ring α] {x y : α} (h : Commute x y) {m n : ℕ} (hmn : m ≤ n) : ((x - y) * ∑ i ∈ Finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = x ^ n - x ^ m * y ^ (n - m) := by rw [sum_Ico_eq_sub _ hmn] have : ∑ k ∈ range m, x ^ k * y ^ (n - 1 - k) = ∑ k ∈ range m, x ^ k * (y ^ (n - m) * y ^ (m - 1 - k)) := by refine sum_congr rfl fun j j_in => ?_ rw [← pow_add] congr rw [mem_range, Nat.lt_iff_add_one_le, add_comm] at j_in have h' : n - m + (m - (1 + j)) = n - (1 + j) := tsub_add_tsub_cancel hmn j_in rw [← tsub_add_eq_tsub_tsub m, h', ← tsub_add_eq_tsub_tsub] rw [this] simp_rw [pow_mul_comm y (n - m) _] simp_rw [← mul_assoc] rw [← sum_mul, mul_sub, h.mul_geom_sum₂, ← mul_assoc, h.mul_geom_sum₂, sub_mul, ← pow_add, add_tsub_cancel_of_le hmn, sub_sub_sub_cancel_right (x ^ n) (x ^ m * y ^ (n - m)) (y ^ n)] protected theorem Commute.geom_sum₂_succ_eq {α : Type u} [Ring α] {x y : α} (h : Commute x y) {n : ℕ} : ∑ i ∈ range (n + 1), x ^ i * y ^ (n - i) = x ^ n + y * ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) := by simp_rw [mul_sum, sum_range_succ_comm, tsub_self, pow_zero, mul_one, add_right_inj, ← mul_assoc, (h.symm.pow_right _).eq, mul_assoc, ← pow_succ'] refine sum_congr rfl fun i hi => ?_ suffices n - 1 - i + 1 = n - i by rw [this] cases' n with n · exact absurd (List.mem_range.mp hi) i.not_lt_zero · rw [tsub_add_eq_add_tsub (Nat.le_sub_one_of_lt (List.mem_range.mp hi)), tsub_add_cancel_of_le (Nat.succ_le_iff.mpr n.succ_pos)] theorem geom_sum₂_succ_eq {α : Type u} [CommRing α] (x y : α) {n : ℕ} : ∑ i ∈ range (n + 1), x ^ i * y ^ (n - i) = x ^ n + y * ∑ i ∈ range n, x ^ i * y ^ (n - 1 - i) := (Commute.all x y).geom_sum₂_succ_eq theorem mul_geom_sum₂_Ico [CommRing α] (x y : α) {m n : ℕ} (hmn : m ≤ n) : ((x - y) * ∑ i ∈ Finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = x ^ n - x ^ m * y ^ (n - m) := (Commute.all x y).mul_geom_sum₂_Ico hmn protected theorem Commute.geom_sum₂_Ico_mul [Ring α] {x y : α} (h : Commute x y) {m n : ℕ} (hmn : m ≤ n) : (∑ i ∈ Finset.Ico m n, x ^ i * y ^ (n - 1 - i)) * (x - y) = x ^ n - y ^ (n - m) * x ^ m := by apply op_injective simp only [op_sub, op_mul, op_pow, op_sum] have : (∑ k ∈ Ico m n, MulOpposite.op y ^ (n - 1 - k) * MulOpposite.op x ^ k) = ∑ k ∈ Ico m n, MulOpposite.op x ^ k * MulOpposite.op y ^ (n - 1 - k) := by refine sum_congr rfl fun k _ => ?_ have hp := Commute.pow_pow (Commute.op h.symm) (n - 1 - k) k simpa [Commute, SemiconjBy] using hp simp only [this] -- Porting note: gives deterministic timeout without this intermediate `have` convert (Commute.op h).mul_geom_sum₂_Ico hmn theorem geom_sum_Ico_mul [Ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : (∑ i ∈ Finset.Ico m n, x ^ i) * (x - 1) = x ^ n - x ^ m := by rw [sum_Ico_eq_sub _ hmn, sub_mul, geom_sum_mul, geom_sum_mul, sub_sub_sub_cancel_right] theorem geom_sum_Ico_mul_neg [Ring α] (x : α) {m n : ℕ} (hmn : m ≤ n) : (∑ i ∈ Finset.Ico m n, x ^ i) * (1 - x) = x ^ m - x ^ n := by rw [sum_Ico_eq_sub _ hmn, sub_mul, geom_sum_mul_neg, geom_sum_mul_neg, sub_sub_sub_cancel_left] protected theorem Commute.geom_sum₂_Ico [DivisionRing α] {x y : α} (h : Commute x y) (hxy : x ≠ y) {m n : ℕ} (hmn : m ≤ n) : (∑ i ∈ Finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = (x ^ n - y ^ (n - m) * x ^ m) / (x - y) := by have : x - y ≠ 0 := by simp_all [sub_eq_iff_eq_add] rw [← h.geom_sum₂_Ico_mul hmn, mul_div_cancel_right₀ _ this] theorem geom_sum₂_Ico [Field α] {x y : α} (hxy : x ≠ y) {m n : ℕ} (hmn : m ≤ n) : (∑ i ∈ Finset.Ico m n, x ^ i * y ^ (n - 1 - i)) = (x ^ n - y ^ (n - m) * x ^ m) / (x - y) := (Commute.all x y).geom_sum₂_Ico hxy hmn theorem geom_sum_Ico [DivisionRing α] {x : α} (hx : x ≠ 1) {m n : ℕ} (hmn : m ≤ n) : ∑ i ∈ Finset.Ico m n, x ^ i = (x ^ n - x ^ m) / (x - 1) := by simp only [sum_Ico_eq_sub _ hmn, geom_sum_eq hx, div_sub_div_same, sub_sub_sub_cancel_right] theorem geom_sum_Ico' [DivisionRing α] {x : α} (hx : x ≠ 1) {m n : ℕ} (hmn : m ≤ n) : ∑ i ∈ Finset.Ico m n, x ^ i = (x ^ m - x ^ n) / (1 - x) := by simp only [geom_sum_Ico hx hmn] convert neg_div_neg_eq (x ^ m - x ^ n) (1 - x) using 2 <;> abel theorem geom_sum_Ico_le_of_lt_one [LinearOrderedField α] {x : α} (hx : 0 ≤ x) (h'x : x < 1) {m n : ℕ} : ∑ i ∈ Ico m n, x ^ i ≤ x ^ m / (1 - x) := by rcases le_or_lt m n with (hmn | hmn) · rw [geom_sum_Ico' h'x.ne hmn] apply div_le_div (pow_nonneg hx _) _ (sub_pos.2 h'x) le_rfl simpa using pow_nonneg hx _ · rw [Ico_eq_empty, sum_empty] · apply div_nonneg (pow_nonneg hx _) simpa using h'x.le · simpa using hmn.le theorem geom_sum_inv [DivisionRing α] {x : α} (hx1 : x ≠ 1) (hx0 : x ≠ 0) (n : ℕ) : ∑ i ∈ range n, x⁻¹ ^ i = (x - 1)⁻¹ * (x - x⁻¹ ^ n * x) := by have h₁ : x⁻¹ ≠ 1 := by rwa [inv_eq_one_div, Ne, div_eq_iff_mul_eq hx0, one_mul] have h₂ : x⁻¹ - 1 ≠ 0 := mt sub_eq_zero.1 h₁ have h₃ : x - 1 ≠ 0 := mt sub_eq_zero.1 hx1 have h₄ : x * (x ^ n)⁻¹ = (x ^ n)⁻¹ * x := Nat.recOn n (by simp) fun n h => by rw [pow_succ', mul_inv_rev, ← mul_assoc, h, mul_assoc, mul_inv_cancel hx0, mul_assoc, inv_mul_cancel hx0] rw [geom_sum_eq h₁, div_eq_iff_mul_eq h₂, ← mul_right_inj' h₃, ← mul_assoc, ← mul_assoc, mul_inv_cancel h₃] simp [mul_add, add_mul, mul_inv_cancel hx0, mul_assoc, h₄, sub_eq_add_neg, add_comm, add_left_comm] rw [add_comm _ (-x), add_assoc, add_assoc _ _ 1] variable {β : Type*} -- TODO: for consistency, the next two lemmas should be moved to the root namespace theorem RingHom.map_geom_sum [Semiring α] [Semiring β] (x : α) (n : ℕ) (f : α →+* β) : f (∑ i ∈ range n, x ^ i) = ∑ i ∈ range n, f x ^ i := by simp [map_sum f] theorem RingHom.map_geom_sum₂ [Semiring α] [Semiring β] (x y : α) (n : ℕ) (f : α →+* β) : f (∑ i ∈ range n, x ^ i * y ^ (n - 1 - i)) = ∑ i ∈ range n, f x ^ i * f y ^ (n - 1 - i) := by simp [map_sum f] /-! ### Geometric sum with `ℕ`-division -/ theorem Nat.pred_mul_geom_sum_le (a b n : ℕ) : ((b - 1) * ∑ i ∈ range n.succ, a / b ^ i) ≤ a * b - a / b ^ n := calc ((b - 1) * ∑ i ∈ range n.succ, a / b ^ i) = (∑ i ∈ range n, a / b ^ (i + 1) * b) + a * b - ((∑ i ∈ range n, a / b ^ i) + a / b ^ n) := by rw [tsub_mul, mul_comm, sum_mul, one_mul, sum_range_succ', sum_range_succ, pow_zero, Nat.div_one] _ ≤ (∑ i ∈ range n, a / b ^ i) + a * b - ((∑ i ∈ range n, a / b ^ i) + a / b ^ n) := by refine tsub_le_tsub_right (add_le_add_right (sum_le_sum fun i _ => ?_) _) _ rw [pow_succ', mul_comm b] rw [← Nat.div_div_eq_div_mul] exact Nat.div_mul_le_self _ _ _ = a * b - a / b ^ n := add_tsub_add_eq_tsub_left _ _ _ theorem Nat.geom_sum_le {b : ℕ} (hb : 2 ≤ b) (a n : ℕ) : ∑ i ∈ range n, a / b ^ i ≤ a * b / (b - 1) := by refine (Nat.le_div_iff_mul_le <| tsub_pos_of_lt hb).2 ?_ cases' n with n · rw [sum_range_zero, zero_mul] exact Nat.zero_le _ rw [mul_comm] exact (Nat.pred_mul_geom_sum_le a b n).trans tsub_le_self theorem Nat.geom_sum_Ico_le {b : ℕ} (hb : 2 ≤ b) (a n : ℕ) : ∑ i ∈ Ico 1 n, a / b ^ i ≤ a / (b - 1) := by cases' n with n · rw [Ico_eq_empty_of_le (zero_le_one' ℕ), sum_empty] exact Nat.zero_le _ rw [← add_le_add_iff_left a] calc (a + ∑ i ∈ Ico 1 n.succ, a / b ^ i) = a / b ^ 0 + ∑ i ∈ Ico 1 n.succ, a / b ^ i := by rw [pow_zero, Nat.div_one] _ = ∑ i ∈ range n.succ, a / b ^ i := by rw [range_eq_Ico, ← Nat.Ico_insert_succ_left (Nat.succ_pos _), sum_insert] exact fun h => zero_lt_one.not_le (mem_Ico.1 h).1 _ ≤ a * b / (b - 1) := Nat.geom_sum_le hb a _ _ = (a * 1 + a * (b - 1)) / (b - 1) := by rw [← mul_add, add_tsub_cancel_of_le (one_le_two.trans hb)] _ = a + a / (b - 1) := by rw [mul_one, Nat.add_mul_div_right _ _ (tsub_pos_of_lt hb), add_comm] section Order variable {n : ℕ} {x : α} theorem geom_sum_pos [StrictOrderedSemiring α] (hx : 0 ≤ x) (hn : n ≠ 0) : 0 < ∑ i ∈ range n, x ^ i := sum_pos' (fun k _ => pow_nonneg hx _) ⟨0, mem_range.2 hn.bot_lt, by simp⟩ theorem geom_sum_pos_and_lt_one [StrictOrderedRing α] (hx : x < 0) (hx' : 0 < x + 1) (hn : 1 < n) : (0 < ∑ i ∈ range n, x ^ i) ∧ ∑ i ∈ range n, x ^ i < 1 := by refine Nat.le_induction ?_ ?_ n (show 2 ≤ n from hn) · rw [geom_sum_two] exact ⟨hx', (add_lt_iff_neg_right _).2 hx⟩ clear hn intro n _ ihn rw [geom_sum_succ, add_lt_iff_neg_right, ← neg_lt_iff_pos_add', neg_mul_eq_neg_mul] exact ⟨mul_lt_one_of_nonneg_of_lt_one_left (neg_nonneg.2 hx.le) (neg_lt_iff_pos_add'.2 hx') ihn.2.le, mul_neg_of_neg_of_pos hx ihn.1⟩ theorem geom_sum_alternating_of_le_neg_one [StrictOrderedRing α] (hx : x + 1 ≤ 0) (n : ℕ) : if Even n then (∑ i ∈ range n, x ^ i) ≤ 0 else 1 ≤ ∑ i ∈ range n, x ^ i := by have hx0 : x ≤ 0 := (le_add_of_nonneg_right zero_le_one).trans hx induction n with | zero => simp only [Nat.zero_eq, range_zero, sum_empty, le_refl, ite_true, even_zero] | succ n ih => simp only [Nat.even_add_one, geom_sum_succ] split_ifs at ih with h · rw [if_neg (not_not_intro h), le_add_iff_nonneg_left] exact mul_nonneg_of_nonpos_of_nonpos hx0 ih · rw [if_pos h] refine (add_le_add_right ?_ _).trans hx simpa only [mul_one] using mul_le_mul_of_nonpos_left ih hx0 theorem geom_sum_alternating_of_lt_neg_one [StrictOrderedRing α] (hx : x + 1 < 0) (hn : 1 < n) : if Even n then (∑ i ∈ range n, x ^ i) < 0 else 1 < ∑ i ∈ range n, x ^ i := by have hx0 : x < 0 := (le_add_of_nonneg_right zero_le_one).trans_lt hx refine Nat.le_induction ?_ ?_ n (show 2 ≤ n from hn) · simp only [geom_sum_two, lt_add_iff_pos_left, ite_true, gt_iff_lt, hx, even_two] clear hn intro n _ ihn simp only [Nat.even_add_one, geom_sum_succ] by_cases hn' : Even n · rw [if_pos hn'] at ihn rw [if_neg, lt_add_iff_pos_left] · exact mul_pos_of_neg_of_neg hx0 ihn · exact not_not_intro hn' · rw [if_neg hn'] at ihn rw [if_pos] swap · exact hn' have := add_lt_add_right (mul_lt_mul_of_neg_left ihn hx0) 1 rw [mul_one] at this exact this.trans hx theorem geom_sum_pos' [LinearOrderedRing α] (hx : 0 < x + 1) (hn : n ≠ 0) : 0 < ∑ i ∈ range n, x ^ i := by obtain _ | _ | n := n · cases hn rfl · simp only [zero_add, range_one, sum_singleton, pow_zero, zero_lt_one] obtain hx' | hx' := lt_or_le x 0 · exact (geom_sum_pos_and_lt_one hx' hx n.one_lt_succ_succ).1 · exact geom_sum_pos hx' (by simp only [Nat.succ_ne_zero, Ne, not_false_iff]) theorem Odd.geom_sum_pos [LinearOrderedRing α] (h : Odd n) : 0 < ∑ i ∈ range n, x ^ i := by rcases n with (_ | _ | k) · exact ((show ¬Odd 0 by decide) h).elim · simp only [zero_add, range_one, sum_singleton, pow_zero, zero_lt_one] rw [Nat.odd_iff_not_even] at h rcases lt_trichotomy (x + 1) 0 with (hx | hx | hx) · have := geom_sum_alternating_of_lt_neg_one hx k.one_lt_succ_succ simp only [h, if_false] at this exact zero_lt_one.trans this · simp only [eq_neg_of_add_eq_zero_left hx, h, neg_one_geom_sum, if_false, zero_lt_one] · exact geom_sum_pos' hx k.succ.succ_ne_zero theorem geom_sum_pos_iff [LinearOrderedRing α] (hn : n ≠ 0) : (0 < ∑ i ∈ range n, x ^ i) ↔ Odd n ∨ 0 < x + 1 := by refine ⟨fun h => ?_, ?_⟩ · rw [or_iff_not_imp_left, ← not_le, ← Nat.even_iff_not_odd] refine fun hn hx => h.not_le ?_ simpa [if_pos hn] using geom_sum_alternating_of_le_neg_one hx n · rintro (hn | hx') · exact hn.geom_sum_pos · exact geom_sum_pos' hx' hn theorem geom_sum_ne_zero [LinearOrderedRing α] (hx : x ≠ -1) (hn : n ≠ 0) : ∑ i ∈ range n, x ^ i ≠ 0 := by obtain _ | _ | n := n · cases hn rfl · simp only [zero_add, range_one, sum_singleton, pow_zero, ne_eq, one_ne_zero, not_false_eq_true] rw [Ne, eq_neg_iff_add_eq_zero, ← Ne] at hx obtain h | h := hx.lt_or_lt · have := geom_sum_alternating_of_lt_neg_one h n.one_lt_succ_succ split_ifs at this · exact this.ne · exact (zero_lt_one.trans this).ne' · exact (geom_sum_pos' h n.succ.succ_ne_zero).ne' theorem geom_sum_eq_zero_iff_neg_one [LinearOrderedRing α] (hn : n ≠ 0) : ∑ i ∈ range n, x ^ i = 0 ↔ x = -1 ∧ Even n := by refine ⟨fun h => ?_, @fun ⟨h, hn⟩ => by simp only [h, hn, neg_one_geom_sum, if_true]⟩ contrapose! h have hx := eq_or_ne x (-1) cases' hx with hx hx · rw [hx, neg_one_geom_sum] simp only [h hx, ite_false, ne_eq, one_ne_zero, not_false_eq_true] · exact geom_sum_ne_zero hx hn theorem geom_sum_neg_iff [LinearOrderedRing α] (hn : n ≠ 0) : ∑ i ∈ range n, x ^ i < 0 ↔ Even n ∧ x + 1 < 0 := by rw [← not_iff_not, not_lt, le_iff_lt_or_eq, eq_comm, or_congr (geom_sum_pos_iff hn) (geom_sum_eq_zero_iff_neg_one hn), Nat.odd_iff_not_even, ← add_eq_zero_iff_eq_neg, not_and, not_lt, le_iff_lt_or_eq, eq_comm, ← imp_iff_not_or, or_comm, and_comm, Decidable.and_or_imp, or_comm] end Order variable {m n : ℕ} {s : Finset ℕ} /-- Value of a geometric sum over the naturals. Note: see `geom_sum_mul_add` for a formulation that avoids division and subtraction. -/ lemma Nat.geomSum_eq (hm : 2 ≤ m) (n : ℕ) : ∑ k ∈ range n, m ^ k = (m ^ n - 1) / (m - 1) := by refine (Nat.div_eq_of_eq_mul_left (tsub_pos_iff_lt.2 hm) <| tsub_eq_of_eq_add ?_).symm simpa only [tsub_add_cancel_of_le (one_le_two.trans hm), eq_comm] using geom_sum_mul_add (m - 1) n /-- If all the elements of a finset of naturals are less than `n`, then the sum of their powers of `m ≥ 2` is less than `m ^ n`. -/ lemma Nat.geomSum_lt (hm : 2 ≤ m) (hs : ∀ k ∈ s, k < n) : ∑ k ∈ s, m ^ k < m ^ n := calc ∑ k ∈ s, m ^ k ≤ ∑ k ∈ range n, m ^ k := sum_le_sum_of_subset fun k hk ↦ mem_range.2 <| hs _ hk _ = (m ^ n - 1) / (m - 1) := Nat.geomSum_eq hm _ _ ≤ m ^ n - 1 := Nat.div_le_self _ _ _ < m ^ n := tsub_lt_self (by positivity) zero_lt_one
Algebra\GradedMonoid.lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.BigOperators.Group.List import Mathlib.Algebra.Group.InjSurj import Mathlib.Data.List.FinRange import Mathlib.Algebra.Group.Action.Defs import Mathlib.Data.SetLike.Basic import Mathlib.Algebra.Group.Submonoid.Operations import Mathlib.Data.Sigma.Basic import Lean.Elab.Tactic /-! # Additively-graded multiplicative structures This module provides a set of heterogeneous typeclasses for defining a multiplicative structure over the sigma type `GradedMonoid A` such that `(*) : A i → A j → A (i + j)`; that is to say, `A` forms an additively-graded monoid. The typeclasses are: * `GradedMonoid.GOne A` * `GradedMonoid.GMul A` * `GradedMonoid.GMonoid A` * `GradedMonoid.GCommMonoid A` These respectively imbue: * `One (GradedMonoid A)` * `Mul (GradedMonoid A)` * `Monoid (GradedMonoid A)` * `CommMonoid (GradedMonoid A)` the base type `A 0` with: * `GradedMonoid.GradeZero.One` * `GradedMonoid.GradeZero.Mul` * `GradedMonoid.GradeZero.Monoid` * `GradedMonoid.GradeZero.CommMonoid` and the `i`th grade `A i` with `A 0`-actions (`•`) defined as left-multiplication: * (nothing) * `GradedMonoid.GradeZero.SMul (A 0)` * `GradedMonoid.GradeZero.MulAction (A 0)` * (nothing) For now, these typeclasses are primarily used in the construction of `DirectSum.Ring` and the rest of that file. ## Dependent graded products This also introduces `List.dProd`, which takes the (possibly non-commutative) product of a list of graded elements of type `A i`. This definition primarily exist to allow `GradedMonoid.mk` and `DirectSum.of` to be pulled outside a product, such as in `GradedMonoid.mk_list_dProd` and `DirectSum.of_list_dProd`. ## Internally graded monoids In addition to the above typeclasses, in the most frequent case when `A` is an indexed collection of `SetLike` subobjects (such as `AddSubmonoid`s, `AddSubgroup`s, or `Submodule`s), this file provides the `Prop` typeclasses: * `SetLike.GradedOne A` (which provides the obvious `GradedMonoid.GOne A` instance) * `SetLike.GradedMul A` (which provides the obvious `GradedMonoid.GMul A` instance) * `SetLike.GradedMonoid A` (which provides the obvious `GradedMonoid.GMonoid A` and `GradedMonoid.GCommMonoid A` instances) which respectively provide the API lemmas * `SetLike.one_mem_graded` * `SetLike.mul_mem_graded` * `SetLike.pow_mem_graded`, `SetLike.list_prod_map_mem_graded` Strictly this last class is unnecessary as it has no fields not present in its parents, but it is included for convenience. Note that there is no need for `SetLike.GradedRing` or similar, as all the information it would contain is already supplied by `GradedMonoid` when `A` is a collection of objects satisfying `AddSubmonoidClass` such as `Submodule`s. These constructions are explored in `Algebra.DirectSum.Internal`. This file also defines: * `SetLike.isHomogeneous A` (which says that `a` is homogeneous iff `a ∈ A i` for some `i : ι`) * `SetLike.homogeneousSubmonoid A`, which is, as the name suggests, the submonoid consisting of all the homogeneous elements. ## Tags graded monoid -/ variable {ι : Type*} /-- A type alias of sigma types for graded monoids. -/ def GradedMonoid (A : ι → Type*) := Sigma A namespace GradedMonoid instance {A : ι → Type*} [Inhabited ι] [Inhabited (A default)] : Inhabited (GradedMonoid A) := inferInstanceAs <| Inhabited (Sigma _) /-- Construct an element of a graded monoid. -/ def mk {A : ι → Type*} : ∀ i, A i → GradedMonoid A := Sigma.mk /-! ### Actions -/ section actions variable {α β} {A : ι → Type*} /-- If `R` acts on each `A i`, then it acts on `GradedMonoid A` via the `.2` projection. -/ instance [∀ i, SMul α (A i)] : SMul α (GradedMonoid A) where smul r g := GradedMonoid.mk g.1 (r • g.2) @[simp] theorem fst_smul [∀ i, SMul α (A i)] (a : α) (x : GradedMonoid A) : (a • x).fst = x.fst := rfl @[simp] theorem snd_smul [∀ i, SMul α (A i)] (a : α) (x : GradedMonoid A) : (a • x).snd = a • x.snd := rfl theorem smul_mk [∀ i, SMul α (A i)] {i} (c : α) (a : A i) : c • mk i a = mk i (c • a) := rfl instance [∀ i, SMul α (A i)] [∀ i, SMul β (A i)] [∀ i, SMulCommClass α β (A i)] : SMulCommClass α β (GradedMonoid A) where smul_comm a b g := Sigma.ext rfl <| heq_of_eq <| smul_comm a b g.2 instance [SMul α β] [∀ i, SMul α (A i)] [∀ i, SMul β (A i)] [∀ i, IsScalarTower α β (A i)] : IsScalarTower α β (GradedMonoid A) where smul_assoc a b g := Sigma.ext rfl <| heq_of_eq <| smul_assoc a b g.2 instance [Monoid α] [∀ i, MulAction α (A i)] : MulAction α (GradedMonoid A) where one_smul g := Sigma.ext rfl <| heq_of_eq <| one_smul _ g.2 mul_smul r₁ r₂ g := Sigma.ext rfl <| heq_of_eq <| mul_smul r₁ r₂ g.2 end actions /-! ### Typeclasses -/ section Defs variable (A : ι → Type*) /-- A graded version of `One`, which must be of grade 0. -/ class GOne [Zero ι] where /-- The term `one` of grade 0 -/ one : A 0 /-- `GOne` implies `One (GradedMonoid A)` -/ instance GOne.toOne [Zero ι] [GOne A] : One (GradedMonoid A) := ⟨⟨_, GOne.one⟩⟩ @[simp] theorem fst_one [Zero ι] [GOne A] : (1 : GradedMonoid A).fst = 0 := rfl @[simp] theorem snd_one [Zero ι] [GOne A] : (1 : GradedMonoid A).snd = GOne.one := rfl /-- A graded version of `Mul`. Multiplication combines grades additively, like `AddMonoidAlgebra`. -/ class GMul [Add ι] where /-- The homogeneous multiplication map `mul` -/ mul {i j} : A i → A j → A (i + j) /-- `GMul` implies `Mul (GradedMonoid A)`. -/ instance GMul.toMul [Add ι] [GMul A] : Mul (GradedMonoid A) := ⟨fun x y : GradedMonoid A => ⟨_, GMul.mul x.snd y.snd⟩⟩ @[simp] theorem fst_mul [Add ι] [GMul A] (x y : GradedMonoid A) : (x * y).fst = x.fst + y.fst := rfl @[simp] theorem snd_mul [Add ι] [GMul A] (x y : GradedMonoid A) : (x * y).snd = GMul.mul x.snd y.snd := rfl theorem mk_mul_mk [Add ι] [GMul A] {i j} (a : A i) (b : A j) : mk i a * mk j b = mk (i + j) (GMul.mul a b) := rfl namespace GMonoid variable {A} variable [AddMonoid ι] [GMul A] [GOne A] /-- A default implementation of power on a graded monoid, like `npowRec`. `GMonoid.gnpow` should be used instead. -/ def gnpowRec : ∀ (n : ℕ) {i}, A i → A (n • i) | 0, i, _ => cast (congr_arg A (zero_nsmul i).symm) GOne.one | n + 1, i, a => cast (congr_arg A (succ_nsmul i n).symm) (GMul.mul (gnpowRec _ a) a) @[simp] theorem gnpowRec_zero (a : GradedMonoid A) : GradedMonoid.mk _ (gnpowRec 0 a.snd) = 1 := Sigma.ext (zero_nsmul _) (heq_of_cast_eq _ rfl).symm @[simp] theorem gnpowRec_succ (n : ℕ) (a : GradedMonoid A) : (GradedMonoid.mk _ <| gnpowRec n.succ a.snd) = ⟨_, gnpowRec n a.snd⟩ * a := Sigma.ext (succ_nsmul _ _) (heq_of_cast_eq _ rfl).symm end GMonoid /-- A tactic to for use as an optional value for `GMonoid.gnpow_zero'`. -/ macro "apply_gmonoid_gnpowRec_zero_tac" : tactic => `(tactic| apply GMonoid.gnpowRec_zero) /-- A tactic to for use as an optional value for `GMonoid.gnpow_succ'`. -/ macro "apply_gmonoid_gnpowRec_succ_tac" : tactic => `(tactic| apply GMonoid.gnpowRec_succ) /-- A graded version of `Monoid` Like `Monoid.npow`, this has an optional `GMonoid.gnpow` field to allow definitional control of natural powers of a graded monoid. -/ class GMonoid [AddMonoid ι] extends GMul A, GOne A where /-- Multiplication by `one` on the left is the identity -/ one_mul (a : GradedMonoid A) : 1 * a = a /-- Multiplication by `one` on the right is the identity -/ mul_one (a : GradedMonoid A) : a * 1 = a /-- Multiplication is associative -/ mul_assoc (a b c : GradedMonoid A) : a * b * c = a * (b * c) /-- Optional field to allow definitional control of natural powers -/ gnpow : ∀ (n : ℕ) {i}, A i → A (n • i) := GMonoid.gnpowRec /-- The zeroth power will yield 1 -/ gnpow_zero' : ∀ a : GradedMonoid A, GradedMonoid.mk _ (gnpow 0 a.snd) = 1 := by apply_gmonoid_gnpowRec_zero_tac /-- Successor powers behave as expected -/ gnpow_succ' : ∀ (n : ℕ) (a : GradedMonoid A), (GradedMonoid.mk _ <| gnpow n.succ a.snd) = ⟨_, gnpow n a.snd⟩ * a := by apply_gmonoid_gnpowRec_succ_tac /-- `GMonoid` implies a `Monoid (GradedMonoid A)`. -/ instance GMonoid.toMonoid [AddMonoid ι] [GMonoid A] : Monoid (GradedMonoid A) where one := 1 mul := (· * ·) npow n a := GradedMonoid.mk _ (GMonoid.gnpow n a.snd) npow_zero a := GMonoid.gnpow_zero' a npow_succ n a := GMonoid.gnpow_succ' n a one_mul := GMonoid.one_mul mul_one := GMonoid.mul_one mul_assoc := GMonoid.mul_assoc @[simp] theorem fst_pow [AddMonoid ι] [GMonoid A] (x : GradedMonoid A) (n : ℕ) : (x ^ n).fst = n • x.fst := rfl @[simp] theorem snd_pow [AddMonoid ι] [GMonoid A] (x : GradedMonoid A) (n : ℕ) : (x ^ n).snd = GMonoid.gnpow n x.snd := rfl theorem mk_pow [AddMonoid ι] [GMonoid A] {i} (a : A i) (n : ℕ) : mk i a ^ n = mk (n • i) (GMonoid.gnpow _ a) := rfl /-- A graded version of `CommMonoid`. -/ class GCommMonoid [AddCommMonoid ι] extends GMonoid A where /-- Multiplication is commutative -/ mul_comm (a : GradedMonoid A) (b : GradedMonoid A) : a * b = b * a /-- `GCommMonoid` implies a `CommMonoid (GradedMonoid A)`, although this is only used as an instance locally to define notation in `gmonoid` and similar typeclasses. -/ instance GCommMonoid.toCommMonoid [AddCommMonoid ι] [GCommMonoid A] : CommMonoid (GradedMonoid A) := { GMonoid.toMonoid A with mul_comm := GCommMonoid.mul_comm } end Defs /-! ### Instances for `A 0` The various `g*` instances are enough to promote the `AddCommMonoid (A 0)` structure to various types of multiplicative structure. -/ section GradeZero variable (A : ι → Type*) section One variable [Zero ι] [GOne A] /-- `1 : A 0` is the value provided in `GOne.one`. -/ @[nolint unusedArguments] instance GradeZero.one : One (A 0) := ⟨GOne.one⟩ end One section Mul variable [AddZeroClass ι] [GMul A] /-- `(•) : A 0 → A i → A i` is the value provided in `GradedMonoid.GMul.mul`, composed with an `Eq.rec` to turn `A (0 + i)` into `A i`. -/ instance GradeZero.smul (i : ι) : SMul (A 0) (A i) where smul x y := @Eq.rec ι (0+i) (fun a _ => A a) (GMul.mul x y) i (zero_add i) /-- `(*) : A 0 → A 0 → A 0` is the value provided in `GradedMonoid.GMul.mul`, composed with an `Eq.rec` to turn `A (0 + 0)` into `A 0`. -/ instance GradeZero.mul : Mul (A 0) where mul := (· • ·) variable {A} @[simp] theorem mk_zero_smul {i} (a : A 0) (b : A i) : mk _ (a • b) = mk _ a * mk _ b := Sigma.ext (zero_add _).symm <| eq_rec_heq _ _ @[simp] theorem GradeZero.smul_eq_mul (a b : A 0) : a • b = a * b := rfl end Mul section Monoid variable [AddMonoid ι] [GMonoid A] instance : NatPow (A 0) where pow x n := @Eq.rec ι (n • (0 : ι)) (fun a _ => A a) (GMonoid.gnpow n x) 0 (nsmul_zero n) variable {A} @[simp] theorem mk_zero_pow (a : A 0) (n : ℕ) : mk _ (a ^ n) = mk _ a ^ n := Sigma.ext (nsmul_zero n).symm <| eq_rec_heq _ _ variable (A) /-- The `Monoid` structure derived from `GMonoid A`. -/ instance GradeZero.monoid : Monoid (A 0) := Function.Injective.monoid (mk 0) sigma_mk_injective rfl mk_zero_smul mk_zero_pow end Monoid section Monoid variable [AddCommMonoid ι] [GCommMonoid A] /-- The `CommMonoid` structure derived from `GCommMonoid A`. -/ instance GradeZero.commMonoid : CommMonoid (A 0) := Function.Injective.commMonoid (mk 0) sigma_mk_injective rfl mk_zero_smul mk_zero_pow end Monoid section MulAction variable [AddMonoid ι] [GMonoid A] /-- `GradedMonoid.mk 0` is a `MonoidHom`, using the `GradedMonoid.GradeZero.monoid` structure. -/ def mkZeroMonoidHom : A 0 →* GradedMonoid A where toFun := mk 0 map_one' := rfl map_mul' := mk_zero_smul /-- Each grade `A i` derives an `A 0`-action structure from `GMonoid A`. -/ instance GradeZero.mulAction {i} : MulAction (A 0) (A i) := letI := MulAction.compHom (GradedMonoid A) (mkZeroMonoidHom A) Function.Injective.mulAction (mk i) sigma_mk_injective mk_zero_smul end MulAction end GradeZero end GradedMonoid /-! ### Dependent products of graded elements -/ section DProd variable {α : Type*} {A : ι → Type*} [AddMonoid ι] [GradedMonoid.GMonoid A] /-- The index used by `List.dProd`. Propositionally this is equal to `(l.map fι).Sum`, but definitionally it needs to have a different form to avoid introducing `Eq.rec`s in `List.dProd`. -/ def List.dProdIndex (l : List α) (fι : α → ι) : ι := l.foldr (fun i b => fι i + b) 0 @[simp] theorem List.dProdIndex_nil (fι : α → ι) : ([] : List α).dProdIndex fι = 0 := rfl @[simp] theorem List.dProdIndex_cons (a : α) (l : List α) (fι : α → ι) : (a :: l).dProdIndex fι = fι a + l.dProdIndex fι := rfl theorem List.dProdIndex_eq_map_sum (l : List α) (fι : α → ι) : l.dProdIndex fι = (l.map fι).sum := by match l with | [] => simp | head::tail => simp [List.dProdIndex_eq_map_sum tail fι] /-- A dependent product for graded monoids represented by the indexed family of types `A i`. This is a dependent version of `(l.map fA).prod`. For a list `l : List α`, this computes the product of `fA a` over `a`, where each `fA` is of type `A (fι a)`. -/ def List.dProd (l : List α) (fι : α → ι) (fA : ∀ a, A (fι a)) : A (l.dProdIndex fι) := l.foldrRecOn _ _ GradedMonoid.GOne.one fun _ x a _ => GradedMonoid.GMul.mul (fA a) x @[simp] theorem List.dProd_nil (fι : α → ι) (fA : ∀ a, A (fι a)) : (List.nil : List α).dProd fι fA = GradedMonoid.GOne.one := rfl -- the `( : _)` in this lemma statement results in the type on the RHS not being unfolded, which -- is nicer in the goal view. @[simp] theorem List.dProd_cons (fι : α → ι) (fA : ∀ a, A (fι a)) (a : α) (l : List α) : (a :: l).dProd fι fA = (GradedMonoid.GMul.mul (fA a) (l.dProd fι fA) : _) := rfl theorem GradedMonoid.mk_list_dProd (l : List α) (fι : α → ι) (fA : ∀ a, A (fι a)) : GradedMonoid.mk _ (l.dProd fι fA) = (l.map fun a => GradedMonoid.mk (fι a) (fA a)).prod := by match l with | [] => simp only [List.dProdIndex_nil, List.dProd_nil, List.map_nil, List.prod_nil]; rfl | head::tail => simp [← GradedMonoid.mk_list_dProd tail _ _, GradedMonoid.mk_mul_mk, List.prod_cons] /-- A variant of `GradedMonoid.mk_list_dProd` for rewriting in the other direction. -/ theorem GradedMonoid.list_prod_map_eq_dProd (l : List α) (f : α → GradedMonoid A) : (l.map f).prod = GradedMonoid.mk _ (l.dProd (fun i => (f i).1) fun i => (f i).2) := by rw [GradedMonoid.mk_list_dProd, GradedMonoid.mk] simp_rw [Sigma.eta] theorem GradedMonoid.list_prod_ofFn_eq_dProd {n : ℕ} (f : Fin n → GradedMonoid A) : (List.ofFn f).prod = GradedMonoid.mk _ ((List.finRange n).dProd (fun i => (f i).1) fun i => (f i).2) := by rw [List.ofFn_eq_map, GradedMonoid.list_prod_map_eq_dProd] end DProd /-! ### Concrete instances -/ section variable (ι) {R : Type*} @[simps one] instance One.gOne [Zero ι] [One R] : GradedMonoid.GOne fun _ : ι => R where one := 1 @[simps mul] instance Mul.gMul [Add ι] [Mul R] : GradedMonoid.GMul fun _ : ι => R where mul x y := x * y /-- If all grades are the same type and themselves form a monoid, then there is a trivial grading structure. -/ @[simps gnpow] instance Monoid.gMonoid [AddMonoid ι] [Monoid R] : GradedMonoid.GMonoid fun _ : ι => R := -- { Mul.gMul ι, One.gOne ι with { One.gOne ι with mul := fun x y => x * y one_mul := fun _ => Sigma.ext (zero_add _) (heq_of_eq (one_mul _)) mul_one := fun _ => Sigma.ext (add_zero _) (heq_of_eq (mul_one _)) mul_assoc := fun _ _ _ => Sigma.ext (add_assoc _ _ _) (heq_of_eq (mul_assoc _ _ _)) gnpow := fun n _ a => a ^ n gnpow_zero' := fun _ => Sigma.ext (zero_nsmul _) (heq_of_eq (Monoid.npow_zero _)) gnpow_succ' := fun _ ⟨_, _⟩ => Sigma.ext (succ_nsmul _ _) (heq_of_eq (Monoid.npow_succ _ _)) } /-- If all grades are the same type and themselves form a commutative monoid, then there is a trivial grading structure. -/ instance CommMonoid.gCommMonoid [AddCommMonoid ι] [CommMonoid R] : GradedMonoid.GCommMonoid fun _ : ι => R := { Monoid.gMonoid ι with mul_comm := fun _ _ => Sigma.ext (add_comm _ _) (heq_of_eq (mul_comm _ _)) } /-- When all the indexed types are the same, the dependent product is just the regular product. -/ @[simp] theorem List.dProd_monoid {α} [AddMonoid ι] [Monoid R] (l : List α) (fι : α → ι) (fA : α → R) : @List.dProd _ _ (fun _ : ι => R) _ _ l fι fA = (l.map fA).prod := by match l with | [] => rw [List.dProd_nil, List.map_nil, List.prod_nil] rfl | head::tail => rw [List.dProd_cons, List.map_cons, List.prod_cons, List.dProd_monoid tail _ _] rfl end /-! ### Shorthands for creating instance of the above typeclasses for collections of subobjects -/ section Subobjects variable {R : Type*} /-- A version of `GradedMonoid.GOne` for internally graded objects. -/ class SetLike.GradedOne {S : Type*} [SetLike S R] [One R] [Zero ι] (A : ι → S) : Prop where /-- One has grade zero -/ one_mem : (1 : R) ∈ A 0 theorem SetLike.one_mem_graded {S : Type*} [SetLike S R] [One R] [Zero ι] (A : ι → S) [SetLike.GradedOne A] : (1 : R) ∈ A 0 := SetLike.GradedOne.one_mem instance SetLike.gOne {S : Type*} [SetLike S R] [One R] [Zero ι] (A : ι → S) [SetLike.GradedOne A] : GradedMonoid.GOne fun i => A i where one := ⟨1, SetLike.one_mem_graded _⟩ @[simp] theorem SetLike.coe_gOne {S : Type*} [SetLike S R] [One R] [Zero ι] (A : ι → S) [SetLike.GradedOne A] : ↑(@GradedMonoid.GOne.one _ (fun i => A i) _ _) = (1 : R) := rfl /-- A version of `GradedMonoid.ghas_one` for internally graded objects. -/ class SetLike.GradedMul {S : Type*} [SetLike S R] [Mul R] [Add ι] (A : ι → S) : Prop where /-- Multiplication is homogeneous -/ mul_mem : ∀ ⦃i j⦄ {gi gj}, gi ∈ A i → gj ∈ A j → gi * gj ∈ A (i + j) theorem SetLike.mul_mem_graded {S : Type*} [SetLike S R] [Mul R] [Add ι] {A : ι → S} [SetLike.GradedMul A] ⦃i j⦄ {gi gj} (hi : gi ∈ A i) (hj : gj ∈ A j) : gi * gj ∈ A (i + j) := SetLike.GradedMul.mul_mem hi hj instance SetLike.gMul {S : Type*} [SetLike S R] [Mul R] [Add ι] (A : ι → S) [SetLike.GradedMul A] : GradedMonoid.GMul fun i => A i where mul := fun a b => ⟨(a * b : R), SetLike.mul_mem_graded a.prop b.prop⟩ /- Porting note: simpNF linter returns "Left-hand side does not simplify, when using the simp lemma on itself." However, simp does indeed solve the following. Possibly related std#71,std#78 example {S : Type*} [SetLike S R] [Mul R] [Add ι] (A : ι → S) [SetLike.GradedMul A] {i j : ι} (x : A i) (y : A j) : ↑(@GradedMonoid.GMul.mul _ (fun i => A i) _ _ _ _ x y) = (x * y : R) := by simp -/ @[simp,nolint simpNF] theorem SetLike.coe_gMul {S : Type*} [SetLike S R] [Mul R] [Add ι] (A : ι → S) [SetLike.GradedMul A] {i j : ι} (x : A i) (y : A j) : ↑(@GradedMonoid.GMul.mul _ (fun i => A i) _ _ _ _ x y) = (x * y : R) := rfl /-- A version of `GradedMonoid.GMonoid` for internally graded objects. -/ class SetLike.GradedMonoid {S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] (A : ι → S) extends SetLike.GradedOne A, SetLike.GradedMul A : Prop namespace SetLike variable {S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] variable {A : ι → S} [SetLike.GradedMonoid A] namespace GradeZero variable (A) in /-- The submonoid `A 0` of `R`. -/ @[simps] def submonoid : Submonoid R where carrier := A 0 mul_mem' ha hb := add_zero (0 : ι) ▸ SetLike.mul_mem_graded ha hb one_mem' := SetLike.one_mem_graded A -- TODO: it might be expensive to unify `A` in this instances in practice /-- The monoid `A 0` inherited from `R` in the presence of `SetLike.GradedMonoid A`. -/ instance instMonoid : Monoid (A 0) := inferInstanceAs <| Monoid (GradeZero.submonoid A) -- TODO: it might be expensive to unify `A` in this instances in practice /-- The commutative monoid `A 0` inherited from `R` in the presence of `SetLike.GradedMonoid A`. -/ instance instCommMonoid {R S : Type*} [SetLike S R] [CommMonoid R] {A : ι → S} [SetLike.GradedMonoid A] : CommMonoid (A 0) := inferInstanceAs <| CommMonoid (GradeZero.submonoid A) /-- The linter message "error: SetLike.GradeZero.coe_one.{u_3, u_2, u_1} Left-hand side does not simplify, when using the simp lemma on itself." is wrong. The LHS does simplify. -/ @[nolint simpNF, simp, norm_cast] theorem coe_one : ↑(1 : A 0) = (1 : R) := rfl /-- The linter message "error: SetLike.GradeZero.coe_mul.{u_3, u_2, u_1} Left-hand side does not simplify, when using the simp lemma on itself." is wrong. The LHS does simplify. -/ @[nolint simpNF, simp, norm_cast] theorem coe_mul (a b : A 0) : ↑(a * b) = (↑a * ↑b : R) := rfl /-- The linter message "error: SetLike.GradeZero.coe_pow.{u_3, u_2, u_1} Left-hand side does not simplify, when using the simp lemma on itself." is wrong. The LHS does simplify. -/ @[nolint simpNF, simp, norm_cast] theorem coe_pow (a : A 0) (n : ℕ) : ↑(a ^ n) = (↑a : R) ^ n := rfl end GradeZero theorem pow_mem_graded (n : ℕ) {r : R} {i : ι} (h : r ∈ A i) : r ^ n ∈ A (n • i) := by match n with | 0 => rw [pow_zero, zero_nsmul] exact one_mem_graded _ | n+1 => rw [pow_succ', succ_nsmul'] exact mul_mem_graded h (pow_mem_graded n h) theorem list_prod_map_mem_graded {ι'} (l : List ι') (i : ι' → ι) (r : ι' → R) (h : ∀ j ∈ l, r j ∈ A (i j)) : (l.map r).prod ∈ A (l.map i).sum := by match l with | [] => rw [List.map_nil, List.map_nil, List.prod_nil, List.sum_nil] exact one_mem_graded _ | head::tail => rw [List.map_cons, List.map_cons, List.prod_cons, List.sum_cons] exact mul_mem_graded (h _ <| List.mem_cons_self _ _) (list_prod_map_mem_graded tail _ _ fun j hj => h _ <| List.mem_cons_of_mem _ hj) theorem list_prod_ofFn_mem_graded {n} (i : Fin n → ι) (r : Fin n → R) (h : ∀ j, r j ∈ A (i j)) : (List.ofFn r).prod ∈ A (List.ofFn i).sum := by rw [List.ofFn_eq_map, List.ofFn_eq_map] exact list_prod_map_mem_graded _ _ _ fun _ _ => h _ end SetLike /-- Build a `GMonoid` instance for a collection of subobjects. -/ instance SetLike.gMonoid {S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] (A : ι → S) [SetLike.GradedMonoid A] : GradedMonoid.GMonoid fun i => A i := { SetLike.gOne A, SetLike.gMul A with one_mul := fun ⟨_, _, _⟩ => Sigma.subtype_ext (zero_add _) (one_mul _) mul_one := fun ⟨_, _, _⟩ => Sigma.subtype_ext (add_zero _) (mul_one _) mul_assoc := fun ⟨_, _, _⟩ ⟨_, _, _⟩ ⟨_, _, _⟩ => Sigma.subtype_ext (add_assoc _ _ _) (mul_assoc _ _ _) gnpow := fun n _ a => ⟨(a:R)^n, SetLike.pow_mem_graded n a.prop⟩ gnpow_zero' := fun _ => Sigma.subtype_ext (zero_nsmul _) (pow_zero _) gnpow_succ' := fun _ _ => Sigma.subtype_ext (succ_nsmul _ _) (pow_succ _ _) } /- Porting note: simpNF linter returns "Left-hand side does not simplify, when using the simp lemma on itself." However, simp does indeed solve the following. Possibly related std#71,std#78 example {S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] (A : ι → S) [SetLike.GradedMonoid A] {i : ι} (x : A i) (n : ℕ) : ↑(@GradedMonoid.GMonoid.gnpow _ (fun i => A i) _ _ n _ x) = (x:R)^n := by simp -/ @[simp,nolint simpNF] theorem SetLike.coe_gnpow {S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] (A : ι → S) [SetLike.GradedMonoid A] {i : ι} (x : A i) (n : ℕ) : ↑(@GradedMonoid.GMonoid.gnpow _ (fun i => A i) _ _ n _ x) = (x:R)^n := rfl /-- Build a `GCommMonoid` instance for a collection of subobjects. -/ instance SetLike.gCommMonoid {S : Type*} [SetLike S R] [CommMonoid R] [AddCommMonoid ι] (A : ι → S) [SetLike.GradedMonoid A] : GradedMonoid.GCommMonoid fun i => A i := { SetLike.gMonoid A with mul_comm := fun ⟨_, _, _⟩ ⟨_, _, _⟩ => Sigma.subtype_ext (add_comm _ _) (mul_comm _ _) } section DProd open SetLike SetLike.GradedMonoid variable {α S : Type*} [SetLike S R] [Monoid R] [AddMonoid ι] /- Porting note: simpNF linter returns "Left-hand side does not simplify, when using the simp lemma on itself." However, simp does indeed solve the following. Possibly related std#71,std#78 example (A : ι → S) [SetLike.GradedMonoid A] (fι : α → ι) (fA : ∀ a, A (fι a)) (l : List α) : ↑(@List.dProd _ _ (fun i => ↥(A i)) _ _ l fι fA) = (List.prod (l.map fun a => fA a) : R) := by simp -/ /-- Coercing a dependent product of subtypes is the same as taking the regular product of the coercions. -/ @[simp,nolint simpNF] theorem SetLike.coe_list_dProd (A : ι → S) [SetLike.GradedMonoid A] (fι : α → ι) (fA : ∀ a, A (fι a)) (l : List α) : ↑(@List.dProd _ _ (fun i => ↥(A i)) _ _ l fι fA) = (List.prod (l.map fun a => fA a) : R) := by match l with | [] => rw [List.dProd_nil, coe_gOne, List.map_nil, List.prod_nil] | head::tail => rw [List.dProd_cons, coe_gMul, List.map_cons, List.prod_cons, SetLike.coe_list_dProd _ _ _ tail] /-- A version of `List.coe_dProd_set_like` with `Subtype.mk`. -/ theorem SetLike.list_dProd_eq (A : ι → S) [SetLike.GradedMonoid A] (fι : α → ι) (fA : ∀ a, A (fι a)) (l : List α) : (@List.dProd _ _ (fun i => ↥(A i)) _ _ l fι fA) = ⟨List.prod (l.map fun a => fA a), (l.dProdIndex_eq_map_sum fι).symm ▸ list_prod_map_mem_graded l _ _ fun i _ => (fA i).prop⟩ := Subtype.ext <| SetLike.coe_list_dProd _ _ _ _ end DProd end Subobjects section HomogeneousElements variable {R S : Type*} [SetLike S R] /-- An element `a : R` is said to be homogeneous if there is some `i : ι` such that `a ∈ A i`. -/ def SetLike.Homogeneous (A : ι → S) (a : R) : Prop := ∃ i, a ∈ A i @[simp] theorem SetLike.homogeneous_coe {A : ι → S} {i} (x : A i) : SetLike.Homogeneous A (x : R) := ⟨i, x.prop⟩ theorem SetLike.homogeneous_one [Zero ι] [One R] (A : ι → S) [SetLike.GradedOne A] : SetLike.Homogeneous A (1 : R) := ⟨0, SetLike.one_mem_graded _⟩ theorem SetLike.homogeneous_mul [Add ι] [Mul R] {A : ι → S} [SetLike.GradedMul A] {a b : R} : SetLike.Homogeneous A a → SetLike.Homogeneous A b → SetLike.Homogeneous A (a * b) | ⟨i, hi⟩, ⟨j, hj⟩ => ⟨i + j, SetLike.mul_mem_graded hi hj⟩ /-- When `A` is a `SetLike.GradedMonoid A`, then the homogeneous elements forms a submonoid. -/ def SetLike.homogeneousSubmonoid [AddMonoid ι] [Monoid R] (A : ι → S) [SetLike.GradedMonoid A] : Submonoid R where carrier := { a | SetLike.Homogeneous A a } one_mem' := SetLike.homogeneous_one A mul_mem' a b := SetLike.homogeneous_mul a b end HomogeneousElements
Algebra\GradedMulAction.lean
/- Copyright (c) 2022 Jujian Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jujian Zhang, Eric Wieser -/ import Mathlib.Algebra.GradedMonoid /-! # Additively-graded multiplicative action structures This module provides a set of heterogeneous typeclasses for defining a multiplicative structure over the sigma type `GradedMonoid A` such that `(•) : A i → M j → M (i +ᵥ j)`; that is to say, `A` has an additively-graded multiplicative action on `M`. The typeclasses are: * `GradedMonoid.GSMul A M` * `GradedMonoid.GMulAction A M` With the `SigmaGraded` locale open, these respectively imbue: * `SMul (GradedMonoid A) (GradedMonoid M)` * `MulAction (GradedMonoid A) (GradedMonoid M)` For now, these typeclasses are primarily used in the construction of `DirectSum.GModule.Module` and the rest of that file. ## Internally graded multiplicative actions In addition to the above typeclasses, in the most frequent case when `A` is an indexed collection of `SetLike` subobjects (such as `AddSubmonoid`s, `AddSubgroup`s, or `Submodule`s), this file provides the `Prop` typeclasses: * `SetLike.GradedSMul A M` (which provides the obvious `GradedMonoid.GSMul A` instance) which provides the API lemma * `SetLike.graded_smul_mem_graded` Note that there is no need for `SetLike.graded_mul_action` or similar, as all the information it would contain is already supplied by `GradedSMul` when the objects within `A` and `M` have a `MulAction` instance. ## Tags graded action -/ variable {ιA ιB ιM : Type*} namespace GradedMonoid /-! ### Typeclasses -/ section Defs variable (A : ιA → Type*) (M : ιM → Type*) /-- A graded version of `SMul`. Scalar multiplication combines grades additively, i.e. if `a ∈ A i` and `m ∈ M j`, then `a • b` must be in `M (i + j)`-/ class GSMul [VAdd ιA ιM] where /-- The homogeneous multiplication map `smul` -/ smul {i j} : A i → M j → M (i +ᵥ j) /-- A graded version of `Mul.toSMul` -/ instance GMul.toGSMul [Add ιA] [GMul A] : GSMul A A where smul := GMul.mul instance GSMul.toSMul [VAdd ιA ιM] [GSMul A M] : SMul (GradedMonoid A) (GradedMonoid M) := ⟨fun x y ↦ ⟨_, GSMul.smul x.snd y.snd⟩⟩ theorem mk_smul_mk [VAdd ιA ιM] [GSMul A M] {i j} (a : A i) (b : M j) : mk i a • mk j b = mk (i +ᵥ j) (GSMul.smul a b) := rfl /-- A graded version of `MulAction`. -/ class GMulAction [AddMonoid ιA] [VAdd ιA ιM] [GMonoid A] extends GSMul A M where /-- One is the neutral element for `•` -/ one_smul (b : GradedMonoid M) : (1 : GradedMonoid A) • b = b /-- Associativity of `•` and `*` -/ mul_smul (a a' : GradedMonoid A) (b : GradedMonoid M) : (a * a') • b = a • a' • b /-- The graded version of `Monoid.toMulAction`. -/ instance GMonoid.toGMulAction [AddMonoid ιA] [GMonoid A] : GMulAction A A := { GMul.toGSMul _ with one_smul := GMonoid.one_mul mul_smul := GMonoid.mul_assoc } instance GMulAction.toMulAction [AddMonoid ιA] [GMonoid A] [VAdd ιA ιM] [GMulAction A M] : MulAction (GradedMonoid A) (GradedMonoid M) where one_smul := GMulAction.one_smul mul_smul := GMulAction.mul_smul end Defs end GradedMonoid /-! ### Shorthands for creating instance of the above typeclasses for collections of subobjects -/ section Subobjects variable {R : Type*} /-- A version of `GradedMonoid.GSMul` for internally graded objects. -/ class SetLike.GradedSMul {S R N M : Type*} [SetLike S R] [SetLike N M] [SMul R M] [VAdd ιA ιB] (A : ιA → S) (B : ιB → N) : Prop where /-- Multiplication is homogeneous -/ smul_mem : ∀ ⦃i : ιA⦄ ⦃j : ιB⦄ {ai bj}, ai ∈ A i → bj ∈ B j → ai • bj ∈ B (i +ᵥ j) instance SetLike.toGSMul {S R N M : Type*} [SetLike S R] [SetLike N M] [SMul R M] [VAdd ιA ιB] (A : ιA → S) (B : ιB → N) [SetLike.GradedSMul A B] : GradedMonoid.GSMul (fun i ↦ A i) fun i ↦ B i where smul a b := ⟨a.1 • b.1, SetLike.GradedSMul.smul_mem a.2 b.2⟩ /- Porting note: simpNF linter returns "Left-hand side does not simplify, when using the simp lemma on itself." However, simp does indeed solve the following. Possibly related std#71,std#78 example {S R N M : Type*} [SetLike S R] [SetLike N M] [SMul R M] [Add ι] (A : ι → S) (B : ι → N) [SetLike.GradedSMul A B] {i j : ι} (x : A i) (y : B j) : (@GradedMonoid.GSMul.smul ι (fun i ↦ A i) (fun i ↦ B i) _ _ i j x y : M) = x.1 • y.1 := by simp -/ @[simp,nolint simpNF] theorem SetLike.coe_GSMul {S R N M : Type*} [SetLike S R] [SetLike N M] [SMul R M] [VAdd ιA ιB] (A : ιA → S) (B : ιB → N) [SetLike.GradedSMul A B] {i : ιA} {j : ιB} (x : A i) (y : B j) : (@GradedMonoid.GSMul.smul ιA ιB (fun i ↦ A i) (fun i ↦ B i) _ _ i j x y : M) = x.1 • y.1 := rfl /-- Internally graded version of `Mul.toSMul`. -/ instance SetLike.GradedMul.toGradedSMul [AddMonoid ιA] [Monoid R] {S : Type*} [SetLike S R] (A : ιA → S) [SetLike.GradedMonoid A] : SetLike.GradedSMul A A where smul_mem _ _ _ _ hi hj := SetLike.GradedMonoid.toGradedMul.mul_mem hi hj end Subobjects section HomogeneousElements variable {S R N M : Type*} [SetLike S R] [SetLike N M] theorem SetLike.Homogeneous.graded_smul [VAdd ιA ιB] [SMul R M] {A : ιA → S} {B : ιB → N} [SetLike.GradedSMul A B] {a : R} {b : M} : SetLike.Homogeneous A a → SetLike.Homogeneous B b → SetLike.Homogeneous B (a • b) | ⟨i, hi⟩, ⟨j, hj⟩ => ⟨i +ᵥ j, SetLike.GradedSMul.smul_mem hi hj⟩ end HomogeneousElements
Algebra\HierarchyDesign.lean
/- Copyright (c) 2021 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Eric Wieser -/ import Batteries.Util.LibraryNote /-! # Documentation of the algebraic hierarchy A library note giving advice on modifying the algebraic hierarchy. (It is not intended as a "tour".) This is ported directly from the Lean3 version, so may refer to files/types that currently only exist in mathlib3. TODO: Add sections about interactions with topological typeclasses, and order typeclasses. -/ library_note "the algebraic hierarchy"/-- # The algebraic hierarchy In any theorem proving environment, there are difficult decisions surrounding the design of the "algebraic hierarchy". There is a danger of exponential explosion in the number of gadgets, especially once interactions between algebraic and order/topological/etc structures are considered. In mathlib, we try to avoid this by only introducing new algebraic typeclasses either 1. when there is "real mathematics" to be done with them, or 2. when there is a meaningful gain in simplicity by factoring out a common substructure. (As examples, at this point we don't have `Loop`, or `UnitalMagma`, but we do have `LieSubmodule` and `TopologicalField`! We also have `GroupWithZero`, as an exemplar of point 2.) Generally in mathlib we use the extension mechanism (so `CommRing` extends `Ring`) rather than mixins (e.g. with separate `Ring` and `CommMul` classes), in part because of the potential blow-up in term sizes described at https://www.ralfj.de/blog/2019/05/15/typeclasses-exponential-blowup.html However there is tension here, as it results in considerable duplication in the API, particularly in the interaction with order structures. This library note is not intended as a design document justifying and explaining the history of mathlib's algebraic hierarchy! Instead it is intended as a developer's guide, for contributors wanting to extend (either new leaves, or new intermediate classes) the algebraic hierarchy as it exists. (Ideally we would have both a tour guide to the existing hierarchy, and an account of the design choices. See https://arxiv.org/abs/1910.09336 for an overview of mathlib as a whole, with some attention to the algebraic hierarchy and https://leanprover-community.github.io/mathlib-overview.html for a summary of what is in mathlib today.) ## Instances When adding a new typeclass `Z` to the algebraic hierarchy one should attempt to add the following constructions and results, when applicable: * Instances transferred elementwise to products, like `Prod.Monoid`. See `Mathlib.Algebra.Group.Prod` for more examples. ``` instance Prod.Z [Z M] [Z N] : Z (M × N) := ... ``` * Instances transferred elementwise to pi types, like `Pi.Monoid`. See `Mathlib.Algebra.Group.Pi` for more examples. ``` instance Pi.Z [∀ i, Z <| f i] : Z (Π i : I, f i) := ... ``` * Instances transferred to `MulOpposite M`, like `MulOpposite.Monoid`. See `Mathlib.Algebra.Opposites` for more examples. ``` instance MulOpposite.Z [Z M] : Z (MulOpposite M) := ... ``` * Instances transferred to `ULift M`, like `ULift.Monoid`. See `Mathlib.Algebra.Group.ULift` for more examples. ``` instance ULift.Z [Z M] : Z (ULift M) := ... ``` * Definitions for transferring the proof fields of instances along injective or surjective functions that agree on the data fields, like `Function.Injective.monoid` and `Function.Surjective.monoid`. We make these definitions `abbrev`, see note [reducible non-instances]. See `Mathlib.Algebra.Group.InjSurj` for more examples. ``` abbrev Function.Injective.Z [Z M₂] (f : M₁ → M₂) (hf : f.Injective) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : Z M₁ := ... abbrev Function.Surjective.Z [Z M₁] (f : M₁ → M₂) (hf : f.Surjective) (one : f 1 = 1) (mul : ∀ x y, f (x * y) = f x * f y) : Z M₂ := ... ``` * Instances transferred elementwise to `Finsupp`s, like `Finsupp.semigroup`. See `Mathlib.Data.Finsupp.Pointwise` for more examples. ``` instance FinSupp.Z [Z β] : Z (α →₀ β) := ... ``` * Instances transferred elementwise to `Set`s, like `Set.monoid`. See `Mathlib.Algebra.Pointwise` for more examples. ``` instance Set.Z [Z α] : Z (Set α) := ... ``` * Definitions for transferring the entire structure across an equivalence, like `Equiv.monoid`. See `Mathlib.Data.Equiv.TransferInstance` for more examples. See also the `transport` tactic. ``` def Equiv.Z (e : α ≃ β) [Z β] : Z α := ... /-- When there is a new notion of `Z`-equiv: -/ def Equiv.ZEquiv (e : α ≃ β) [Z β] : by { letI := Equiv.Z e, exact α ≃Z β } := ... ``` ## Subobjects When a new typeclass `Z` adds new data fields, you should also create a new `SubZ` `structure` with a `carrier` field. This can be a lot of work; for now try to closely follow the existing examples (e.g. `Submonoid`, `Subring`, `Subalgebra`). We would very much like to provide some automation here, but a prerequisite will be making all the existing APIs more uniform. If `Z` extends `Y`, then `SubZ` should usually extend `SubY`. When `Z` adds only new proof fields to an existing structure `Y`, you should provide instances transferring `Z α` to `Z (SubY α)`, like `Submonoid.toCommMonoid`. Typically this is done using the `Function.Injective.Z` definition mentioned above. ``` instance SubY.toZ [Z α] : Z (SubY α) := coe_injective.Z coe ... ``` ## Morphisms and equivalences ## Category theory For many algebraic structures, particularly ones used in representation theory, algebraic geometry, etc., we also define "bundled" versions, which carry `category` instances. These bundled versions are usually named by appending `Cat`, so for example we have `AddCommGrp` as a bundled `AddCommGroup`, and `TopCommRingCat` (which bundles together `CommRing`, `TopologicalSpace`, and `TopologicalRing`). These bundled versions have many appealing features: * a uniform notation for morphisms `X ⟶ Y` * a uniform notation (and definition) for isomorphisms `X ≅ Y` * a uniform API for subobjects, via the partial order `Subobject X` * interoperability with unbundled structures, via coercions to `Type` (so if `G : AddCommGrp`, you can treat `G` as a type, and it automatically has an `AddCommGroup` instance) and lifting maps `AddCommGrp.of G`, when `G` is a type with an `AddCommGroup` instance. If, for example you do the work of proving that a typeclass `Z` has a good notion of tensor product, you are strongly encouraged to provide the corresponding `MonoidalCategory` instance on a bundled version. This ensures that the API for tensor products is complete, and enables use of general machinery. Similarly if you prove universal properties, or adjunctions, you are encouraged to state these using categorical language! One disadvantage of the bundled approach is that we can only speak of morphisms between objects living in the same type-theoretic universe. In practice this is rarely a problem. # Making a pull request With so many moving parts, how do you actually go about changing the algebraic hierarchy? We're still evolving how to handle this, but the current suggestion is: * If you're adding a new "leaf" class, the requirements are lower, and an initial PR can just add whatever is immediately needed. * A new "intermediate" class, especially low down in the hierarchy, needs to be careful about leaving gaps. In a perfect world, there would be a group of simultaneous PRs that basically cover everything! (Or at least an expectation that PRs may not be merged immediately while waiting on other PRs that fill out the API.) However "perfect is the enemy of good", and it would also be completely reasonable to add a TODO list in the main module doc-string for the new class, briefly listing the parts of the API which still need to be provided. Hopefully this document makes it easy to assemble this list. Another alternative to a TODO list in the doc-strings is adding Github issues. -/ library_note "reducible non-instances"/-- Some definitions that define objects of a class cannot be instances, because they have an explicit argument that does not occur in the conclusion. An example is `Preorder.lift` that has a function `f : α → β` as an explicit argument to lift a preorder on `β` to a preorder on `α`. If these definitions are used to define instances of this class *and* this class is an argument to some other type-class so that type-class inference will have to unfold these instances to check for definitional equality, then these definitions should be marked `@[reducible]`. For example, `Preorder.lift` is used to define `Units.Preorder` and `PartialOrder.lift` is used to define `Units.PartialOrder`. In some cases it is important that type-class inference can recognize that `Units.Preorder` and `Units.PartialOrder` give rise to the same `LE` instance. For example, you might have another class that takes `[LE α]` as an argument, and this argument sometimes comes from `Units.Preorder` and sometimes from `Units.PartialOrder`. Therefore, `Preorder.lift` and `PartialOrder.lift` are marked `@[reducible]`. -/ library_note "implicit instance arguments"/-- There are places where typeclass arguments are specified with implicit `{}` brackets instead of the usual `[]` brackets. This is done when the instances can be inferred because they are implicit arguments to the type of one of the other arguments. When they can be inferred from these other arguments, it is faster to use this method than to use type class inference. For example, when writing lemmas about `(f : α →+* β)`, it is faster to specify the fact that `α` and `β` are `Semiring`s as `{rα : Semiring α} {rβ : Semiring β}` rather than the usual `[Semiring α] [Semiring β]`. -/ library_note "lower instance priority"/-- Certain instances always apply during type-class resolution. For example, the instance `AddCommGroup.toAddGroup {α} [AddCommGroup α] : AddGroup α` applies to all type-class resolution problems of the form `AddGroup _`, and type-class inference will then do an exhaustive search to find a commutative group. These instances take a long time to fail. Other instances will only apply if the goal has a certain shape. For example `Int.instAddGroupInt : AddGroup ℤ` or `Prod.instAddGroup {α β} [AddGroup α] [AddGroup β] : AddGroup (α × β)`. Usually these instances will fail quickly, and when they apply, they are almost always the desired instance. For this reason, we want the instances of the second type (that only apply in specific cases) to always have higher priority than the instances of the first type (that always apply). See also [mathlib#1561](https://github.com/leanprover-community/mathlib/issues/1561). Therefore, if we create an instance that always applies, we set the priority of these instances to 100 (or something similar, which is below the default value of 1000). -/
Algebra\IsPrimePow.lean
/- Copyright (c) 2022 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import Mathlib.Algebra.Associated.Basic import Mathlib.NumberTheory.Divisors /-! # Prime powers This file deals with prime powers: numbers which are positive integer powers of a single prime. -/ variable {R : Type*} [CommMonoidWithZero R] (n p : R) (k : ℕ) /-- `n` is a prime power if there is a prime `p` and a positive natural `k` such that `n` can be written as `p^k`. -/ def IsPrimePow : Prop := ∃ (p : R) (k : ℕ), Prime p ∧ 0 < k ∧ p ^ k = n theorem isPrimePow_def : IsPrimePow n ↔ ∃ (p : R) (k : ℕ), Prime p ∧ 0 < k ∧ p ^ k = n := Iff.rfl /-- An equivalent definition for prime powers: `n` is a prime power iff there is a prime `p` and a natural `k` such that `n` can be written as `p^(k+1)`. -/ theorem isPrimePow_iff_pow_succ : IsPrimePow n ↔ ∃ (p : R) (k : ℕ), Prime p ∧ p ^ (k + 1) = n := (isPrimePow_def _).trans ⟨fun ⟨p, k, hp, hk, hn⟩ => ⟨p, k - 1, hp, by rwa [Nat.sub_add_cancel hk]⟩, fun ⟨p, k, hp, hn⟩ => ⟨_, _, hp, Nat.succ_pos', hn⟩⟩ theorem not_isPrimePow_zero [NoZeroDivisors R] : ¬IsPrimePow (0 : R) := by simp only [isPrimePow_def, not_exists, not_and', and_imp] intro x n _hn hx rw [pow_eq_zero hx] simp theorem IsPrimePow.not_unit {n : R} (h : IsPrimePow n) : ¬IsUnit n := let ⟨_p, _k, hp, hk, hn⟩ := h hn ▸ (isUnit_pow_iff hk.ne').not.mpr hp.not_unit theorem IsUnit.not_isPrimePow {n : R} (h : IsUnit n) : ¬IsPrimePow n := fun h' => h'.not_unit h theorem not_isPrimePow_one : ¬IsPrimePow (1 : R) := isUnit_one.not_isPrimePow theorem Prime.isPrimePow {p : R} (hp : Prime p) : IsPrimePow p := ⟨p, 1, hp, zero_lt_one, by simp⟩ theorem IsPrimePow.pow {n : R} (hn : IsPrimePow n) {k : ℕ} (hk : k ≠ 0) : IsPrimePow (n ^ k) := let ⟨p, k', hp, hk', hn⟩ := hn ⟨p, k * k', hp, mul_pos hk.bot_lt hk', by rw [pow_mul', hn]⟩ theorem IsPrimePow.ne_zero [NoZeroDivisors R] {n : R} (h : IsPrimePow n) : n ≠ 0 := fun t => not_isPrimePow_zero (t ▸ h) theorem IsPrimePow.ne_one {n : R} (h : IsPrimePow n) : n ≠ 1 := fun t => not_isPrimePow_one (t ▸ h) section Nat theorem isPrimePow_nat_iff (n : ℕ) : IsPrimePow n ↔ ∃ p k : ℕ, Nat.Prime p ∧ 0 < k ∧ p ^ k = n := by simp only [isPrimePow_def, Nat.prime_iff] theorem Nat.Prime.isPrimePow {p : ℕ} (hp : p.Prime) : IsPrimePow p := _root_.Prime.isPrimePow (prime_iff.mp hp) theorem isPrimePow_nat_iff_bounded (n : ℕ) : IsPrimePow n ↔ ∃ p : ℕ, p ≤ n ∧ ∃ k : ℕ, k ≤ n ∧ p.Prime ∧ 0 < k ∧ p ^ k = n := by rw [isPrimePow_nat_iff] refine Iff.symm ⟨fun ⟨p, _, k, _, hp, hk, hn⟩ => ⟨p, k, hp, hk, hn⟩, ?_⟩ rintro ⟨p, k, hp, hk, rfl⟩ refine ⟨p, ?_, k, (Nat.lt_pow_self hp.one_lt _).le, hp, hk, rfl⟩ conv => { lhs; rw [← (pow_one p)] } exact Nat.pow_le_pow_right hp.one_lt.le hk instance {n : ℕ} : Decidable (IsPrimePow n) := decidable_of_iff' _ (isPrimePow_nat_iff_bounded n) theorem IsPrimePow.dvd {n m : ℕ} (hn : IsPrimePow n) (hm : m ∣ n) (hm₁ : m ≠ 1) : IsPrimePow m := by rw [isPrimePow_nat_iff] at hn ⊢ rcases hn with ⟨p, k, hp, _hk, rfl⟩ obtain ⟨i, hik, rfl⟩ := (Nat.dvd_prime_pow hp).1 hm refine ⟨p, i, hp, ?_, rfl⟩ apply Nat.pos_of_ne_zero rintro rfl simp only [pow_zero, ne_eq, not_true_eq_false] at hm₁ theorem Nat.disjoint_divisors_filter_isPrimePow {a b : ℕ} (hab : a.Coprime b) : Disjoint (a.divisors.filter IsPrimePow) (b.divisors.filter IsPrimePow) := by simp only [Finset.disjoint_left, Finset.mem_filter, and_imp, Nat.mem_divisors, not_and] rintro n han _ha hn hbn _hb - exact hn.ne_one (Nat.eq_one_of_dvd_coprimes hab han hbn) theorem IsPrimePow.two_le : ∀ {n : ℕ}, IsPrimePow n → 2 ≤ n | 0, h => (not_isPrimePow_zero h).elim | 1, h => (not_isPrimePow_one h).elim | _n + 2, _ => le_add_self theorem IsPrimePow.pos {n : ℕ} (hn : IsPrimePow n) : 0 < n := pos_of_gt hn.two_le theorem IsPrimePow.one_lt {n : ℕ} (h : IsPrimePow n) : 1 < n := h.two_le end Nat
Algebra\LinearRecurrence.lean
/- Copyright (c) 2020 Anatole Dedecker. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anatole Dedecker -/ import Mathlib.Algebra.Polynomial.Eval import Mathlib.LinearAlgebra.Dimension.Constructions /-! # Linear recurrence Informally, a "linear recurrence" is an assertion of the form `∀ n : ℕ, u (n + d) = a 0 * u n + a 1 * u (n+1) + ... + a (d-1) * u (n+d-1)`, where `u` is a sequence, `d` is the *order* of the recurrence and the `a i` are its *coefficients*. In this file, we define the structure `LinearRecurrence` so that `LinearRecurrence.mk d a` represents the above relation, and we call a sequence `u` which verifies it a *solution* of the linear recurrence. We prove a few basic lemmas about this concept, such as : * the space of solutions is a submodule of `(ℕ → α)` (i.e a vector space if `α` is a field) * the function that maps a solution `u` to its first `d` terms builds a `LinearEquiv` between the solution space and `Fin d → α`, aka `α ^ d`. As a consequence, two solutions are equal if and only if their first `d` terms are equals. * a geometric sequence `q ^ n` is solution iff `q` is a root of a particular polynomial, which we call the *characteristic polynomial* of the recurrence Of course, although we can inductively generate solutions (cf `mkSol`), the interesting part would be to determinate closed-forms for the solutions. This is currently *not implemented*, as we are waiting for definition and properties of eigenvalues and eigenvectors. -/ noncomputable section open Finset open Polynomial /-- A "linear recurrence relation" over a commutative semiring is given by its order `n` and `n` coefficients. -/ structure LinearRecurrence (α : Type*) [CommSemiring α] where order : ℕ coeffs : Fin order → α instance (α : Type*) [CommSemiring α] : Inhabited (LinearRecurrence α) := ⟨⟨0, default⟩⟩ namespace LinearRecurrence section CommSemiring variable {α : Type*} [CommSemiring α] (E : LinearRecurrence α) /-- We say that a sequence `u` is solution of `LinearRecurrence order coeffs` when we have `u (n + order) = ∑ i : Fin order, coeffs i * u (n + i)` for any `n`. -/ def IsSolution (u : ℕ → α) := ∀ n, u (n + E.order) = ∑ i, E.coeffs i * u (n + i) /-- A solution of a `LinearRecurrence` which satisfies certain initial conditions. We will prove this is the only such solution. -/ def mkSol (init : Fin E.order → α) : ℕ → α | n => if h : n < E.order then init ⟨n, h⟩ else ∑ k : Fin E.order, have _ : n - E.order + k < n := by rw [add_comm, ← add_tsub_assoc_of_le (not_lt.mp h), tsub_lt_iff_left] · exact add_lt_add_right k.is_lt n · convert add_le_add (zero_le (k : ℕ)) (not_lt.mp h) simp only [zero_add] E.coeffs k * mkSol init (n - E.order + k) /-- `E.mkSol` indeed gives solutions to `E`. -/ theorem is_sol_mkSol (init : Fin E.order → α) : E.IsSolution (E.mkSol init) := by intro n rw [mkSol] simp /-- `E.mkSol init`'s first `E.order` terms are `init`. -/ theorem mkSol_eq_init (init : Fin E.order → α) : ∀ n : Fin E.order, E.mkSol init n = init n := by intro n rw [mkSol] simp only [n.is_lt, dif_pos, Fin.mk_val, Fin.eta] /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `∀ n, u n = E.mkSol init n`. -/ theorem eq_mk_of_is_sol_of_eq_init {u : ℕ → α} {init : Fin E.order → α} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : ∀ n, u n = E.mkSol init n := by intro n rw [mkSol] split_ifs with h' · exact mod_cast heq ⟨n, h'⟩ simp only rw [← tsub_add_cancel_of_le (le_of_not_lt h'), h (n - E.order)] congr with k have : n - E.order + k < n := by rw [add_comm, ← add_tsub_assoc_of_le (not_lt.mp h'), tsub_lt_iff_left] · exact add_lt_add_right k.is_lt n · convert add_le_add (zero_le (k : ℕ)) (not_lt.mp h') simp only [zero_add] rw [eq_mk_of_is_sol_of_eq_init h heq (n - E.order + k)] simp /-- If `u` is a solution to `E` and `init` designates its first `E.order` values, then `u = E.mkSol init`. This proves that `E.mkSol init` is the only solution of `E` whose first `E.order` values are given by `init`. -/ theorem eq_mk_of_is_sol_of_eq_init' {u : ℕ → α} {init : Fin E.order → α} (h : E.IsSolution u) (heq : ∀ n : Fin E.order, u n = init n) : u = E.mkSol init := funext (E.eq_mk_of_is_sol_of_eq_init h heq) /-- The space of solutions of `E`, as a `Submodule` over `α` of the module `ℕ → α`. -/ def solSpace : Submodule α (ℕ → α) where carrier := { u | E.IsSolution u } zero_mem' n := by simp add_mem' {u v} hu hv n := by simp [mul_add, sum_add_distrib, hu n, hv n] smul_mem' a u hu n := by simp [hu n, mul_sum]; congr; ext; ac_rfl /-- Defining property of the solution space : `u` is a solution iff it belongs to the solution space. -/ theorem is_sol_iff_mem_solSpace (u : ℕ → α) : E.IsSolution u ↔ u ∈ E.solSpace := Iff.rfl /-- The function that maps a solution `u` of `E` to its first `E.order` terms as a `LinearEquiv`. -/ def toInit : E.solSpace ≃ₗ[α] Fin E.order → α where toFun u x := (u : ℕ → α) x map_add' u v := by ext simp map_smul' a u := by ext simp invFun u := ⟨E.mkSol u, E.is_sol_mkSol u⟩ left_inv u := by ext n; symm; apply E.eq_mk_of_is_sol_of_eq_init u.2; intro k; rfl right_inv u := Function.funext_iff.mpr fun n ↦ E.mkSol_eq_init u n /-- Two solutions are equal iff they are equal on `range E.order`. -/ theorem sol_eq_of_eq_init (u v : ℕ → α) (hu : E.IsSolution u) (hv : E.IsSolution v) : u = v ↔ Set.EqOn u v ↑(range E.order) := by refine Iff.intro (fun h x _ ↦ h ▸ rfl) ?_ intro h set u' : ↥E.solSpace := ⟨u, hu⟩ set v' : ↥E.solSpace := ⟨v, hv⟩ change u'.val = v'.val suffices h' : u' = v' from h' ▸ rfl rw [← E.toInit.toEquiv.apply_eq_iff_eq, LinearEquiv.coe_toEquiv] ext x exact mod_cast h (mem_range.mpr x.2) /-! `E.tupleSucc` maps `![s₀, s₁, ..., sₙ]` to `![s₁, ..., sₙ, ∑ (E.coeffs i) * sᵢ]`, where `n := E.order`. This operation is quite useful for determining closed-form solutions of `E`. -/ /-- `E.tupleSucc` maps `![s₀, s₁, ..., sₙ]` to `![s₁, ..., sₙ, ∑ (E.coeffs i) * sᵢ]`, where `n := E.order`. -/ def tupleSucc : (Fin E.order → α) →ₗ[α] Fin E.order → α where toFun X i := if h : (i : ℕ) + 1 < E.order then X ⟨i + 1, h⟩ else ∑ i, E.coeffs i * X i map_add' x y := by ext i simp only split_ifs with h <;> simp [h, mul_add, sum_add_distrib] map_smul' x y := by ext i simp only split_ifs with h <;> simp [h, mul_sum] exact sum_congr rfl fun x _ ↦ by ac_rfl end CommSemiring section StrongRankCondition -- note: `StrongRankCondition` is the same as `Nontrivial` on `CommRing`s, but that result, -- `commRing_strongRankCondition`, is in a much later file. variable {α : Type*} [CommRing α] [StrongRankCondition α] (E : LinearRecurrence α) /-- The dimension of `E.solSpace` is `E.order`. -/ theorem solSpace_rank : Module.rank α E.solSpace = E.order := letI := nontrivial_of_invariantBasisNumber α @rank_fin_fun α _ _ E.order ▸ E.toInit.rank_eq end StrongRankCondition section CommRing variable {α : Type*} [CommRing α] (E : LinearRecurrence α) /-- The characteristic polynomial of `E` is `X ^ E.order - ∑ i : Fin E.order, (E.coeffs i) * X ^ i`. -/ def charPoly : α[X] := Polynomial.monomial E.order 1 - ∑ i : Fin E.order, Polynomial.monomial i (E.coeffs i) /-- The geometric sequence `q^n` is a solution of `E` iff `q` is a root of `E`'s characteristic polynomial. -/ theorem geom_sol_iff_root_charPoly (q : α) : (E.IsSolution fun n ↦ q ^ n) ↔ E.charPoly.IsRoot q := by rw [charPoly, Polynomial.IsRoot.def, Polynomial.eval] simp only [Polynomial.eval₂_finset_sum, one_mul, RingHom.id_apply, Polynomial.eval₂_monomial, Polynomial.eval₂_sub] constructor · intro h simpa [sub_eq_zero] using h 0 · intro h n simp only [pow_add, sub_eq_zero.mp h, mul_sum] exact sum_congr rfl fun _ _ ↦ by ring end CommRing end LinearRecurrence
Algebra\ModEq.lean
/- Copyright (c) 2023 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Data.Int.ModEq import Mathlib.GroupTheory.QuotientGroup /-! # Equality modulo an element This file defines equality modulo an element in a commutative group. ## Main definitions * `a ≡ b [PMOD p]`: `a` and `b` are congruent modulo `p`. ## See also `SModEq` is a generalisation to arbitrary submodules. ## TODO Delete `Int.ModEq` in favour of `AddCommGroup.ModEq`. Generalise `SModEq` to `AddSubgroup` and redefine `AddCommGroup.ModEq` using it. Once this is done, we can rename `AddCommGroup.ModEq` to `AddSubgroup.ModEq` and multiplicativise it. Longer term, we could generalise to submonoids and also unify with `Nat.ModEq`. -/ namespace AddCommGroup variable {α : Type*} section AddCommGroup variable [AddCommGroup α] {p a a₁ a₂ b b₁ b₂ c : α} {n : ℕ} {z : ℤ} /-- `a ≡ b [PMOD p]` means that `b` is congruent to `a` modulo `p`. Equivalently (as shown in `Algebra.Order.ToIntervalMod`), `b` does not lie in the open interval `(a, a + p)` modulo `p`, or `toIcoMod hp a` disagrees with `toIocMod hp a` at `b`, or `toIcoDiv hp a` disagrees with `toIocDiv hp a` at `b`. -/ def ModEq (p a b : α) : Prop := ∃ z : ℤ, b - a = z • p @[inherit_doc] notation:50 a " ≡ " b " [PMOD " p "]" => ModEq p a b @[refl, simp] theorem modEq_refl (a : α) : a ≡ a [PMOD p] := ⟨0, by simp⟩ theorem modEq_rfl : a ≡ a [PMOD p] := modEq_refl _ theorem modEq_comm : a ≡ b [PMOD p] ↔ b ≡ a [PMOD p] := (Equiv.neg _).exists_congr_left.trans <| by simp [ModEq, ← neg_eq_iff_eq_neg] alias ⟨ModEq.symm, _⟩ := modEq_comm attribute [symm] ModEq.symm @[trans] theorem ModEq.trans : a ≡ b [PMOD p] → b ≡ c [PMOD p] → a ≡ c [PMOD p] := fun ⟨m, hm⟩ ⟨n, hn⟩ => ⟨m + n, by simp [add_smul, ← hm, ← hn]⟩ instance : IsRefl _ (ModEq p) := ⟨modEq_refl⟩ @[simp] theorem neg_modEq_neg : -a ≡ -b [PMOD p] ↔ a ≡ b [PMOD p] := modEq_comm.trans <| by simp [ModEq, neg_add_eq_sub] alias ⟨ModEq.of_neg, ModEq.neg⟩ := neg_modEq_neg @[simp] theorem modEq_neg : a ≡ b [PMOD -p] ↔ a ≡ b [PMOD p] := modEq_comm.trans <| by simp [ModEq, ← neg_eq_iff_eq_neg] alias ⟨ModEq.of_neg', ModEq.neg'⟩ := modEq_neg theorem modEq_sub (a b : α) : a ≡ b [PMOD b - a] := ⟨1, (one_smul _ _).symm⟩ @[simp] theorem modEq_zero : a ≡ b [PMOD 0] ↔ a = b := by simp [ModEq, sub_eq_zero, eq_comm] @[simp] theorem self_modEq_zero : p ≡ 0 [PMOD p] := ⟨-1, by simp⟩ @[simp] theorem zsmul_modEq_zero (z : ℤ) : z • p ≡ 0 [PMOD p] := ⟨-z, by simp⟩ theorem add_zsmul_modEq (z : ℤ) : a + z • p ≡ a [PMOD p] := ⟨-z, by simp⟩ theorem zsmul_add_modEq (z : ℤ) : z • p + a ≡ a [PMOD p] := ⟨-z, by simp [← sub_sub]⟩ theorem add_nsmul_modEq (n : ℕ) : a + n • p ≡ a [PMOD p] := ⟨-n, by simp⟩ theorem nsmul_add_modEq (n : ℕ) : n • p + a ≡ a [PMOD p] := ⟨-n, by simp [← sub_sub]⟩ namespace ModEq protected theorem add_zsmul (z : ℤ) : a ≡ b [PMOD p] → a + z • p ≡ b [PMOD p] := (add_zsmul_modEq _).trans protected theorem zsmul_add (z : ℤ) : a ≡ b [PMOD p] → z • p + a ≡ b [PMOD p] := (zsmul_add_modEq _).trans protected theorem add_nsmul (n : ℕ) : a ≡ b [PMOD p] → a + n • p ≡ b [PMOD p] := (add_nsmul_modEq _).trans protected theorem nsmul_add (n : ℕ) : a ≡ b [PMOD p] → n • p + a ≡ b [PMOD p] := (nsmul_add_modEq _).trans protected theorem of_zsmul : a ≡ b [PMOD z • p] → a ≡ b [PMOD p] := fun ⟨m, hm⟩ => ⟨m * z, by rwa [mul_smul]⟩ protected theorem of_nsmul : a ≡ b [PMOD n • p] → a ≡ b [PMOD p] := fun ⟨m, hm⟩ => ⟨m * n, by rwa [mul_smul, natCast_zsmul]⟩ protected theorem zsmul : a ≡ b [PMOD p] → z • a ≡ z • b [PMOD z • p] := Exists.imp fun m hm => by rw [← smul_sub, hm, smul_comm] protected theorem nsmul : a ≡ b [PMOD p] → n • a ≡ n • b [PMOD n • p] := Exists.imp fun m hm => by rw [← smul_sub, hm, smul_comm] end ModEq @[simp] theorem zsmul_modEq_zsmul [NoZeroSMulDivisors ℤ α] (hn : z ≠ 0) : z • a ≡ z • b [PMOD z • p] ↔ a ≡ b [PMOD p] := exists_congr fun m => by rw [← smul_sub, smul_comm, smul_right_inj hn] @[simp] theorem nsmul_modEq_nsmul [NoZeroSMulDivisors ℕ α] (hn : n ≠ 0) : n • a ≡ n • b [PMOD n • p] ↔ a ≡ b [PMOD p] := exists_congr fun m => by rw [← smul_sub, smul_comm, smul_right_inj hn] alias ⟨ModEq.zsmul_cancel, _⟩ := zsmul_modEq_zsmul alias ⟨ModEq.nsmul_cancel, _⟩ := nsmul_modEq_nsmul namespace ModEq @[simp] protected theorem add_iff_left : a₁ ≡ b₁ [PMOD p] → (a₁ + a₂ ≡ b₁ + b₂ [PMOD p] ↔ a₂ ≡ b₂ [PMOD p]) := fun ⟨m, hm⟩ => (Equiv.addLeft m).symm.exists_congr_left.trans <| by simp [add_sub_add_comm, hm, add_smul, ModEq] @[simp] protected theorem add_iff_right : a₂ ≡ b₂ [PMOD p] → (a₁ + a₂ ≡ b₁ + b₂ [PMOD p] ↔ a₁ ≡ b₁ [PMOD p]) := fun ⟨m, hm⟩ => (Equiv.addRight m).symm.exists_congr_left.trans <| by simp [add_sub_add_comm, hm, add_smul, ModEq] @[simp] protected theorem sub_iff_left : a₁ ≡ b₁ [PMOD p] → (a₁ - a₂ ≡ b₁ - b₂ [PMOD p] ↔ a₂ ≡ b₂ [PMOD p]) := fun ⟨m, hm⟩ => (Equiv.subLeft m).symm.exists_congr_left.trans <| by simp [sub_sub_sub_comm, hm, sub_smul, ModEq] @[simp] protected theorem sub_iff_right : a₂ ≡ b₂ [PMOD p] → (a₁ - a₂ ≡ b₁ - b₂ [PMOD p] ↔ a₁ ≡ b₁ [PMOD p]) := fun ⟨m, hm⟩ => (Equiv.subRight m).symm.exists_congr_left.trans <| by simp [sub_sub_sub_comm, hm, sub_smul, ModEq] alias ⟨add_left_cancel, add⟩ := ModEq.add_iff_left alias ⟨add_right_cancel, _⟩ := ModEq.add_iff_right alias ⟨sub_left_cancel, sub⟩ := ModEq.sub_iff_left alias ⟨sub_right_cancel, _⟩ := ModEq.sub_iff_right -- Porting note: doesn't work -- attribute [protected] add_left_cancel add_right_cancel add sub_left_cancel sub_right_cancel sub protected theorem add_left (c : α) (h : a ≡ b [PMOD p]) : c + a ≡ c + b [PMOD p] := modEq_rfl.add h protected theorem sub_left (c : α) (h : a ≡ b [PMOD p]) : c - a ≡ c - b [PMOD p] := modEq_rfl.sub h protected theorem add_right (c : α) (h : a ≡ b [PMOD p]) : a + c ≡ b + c [PMOD p] := h.add modEq_rfl protected theorem sub_right (c : α) (h : a ≡ b [PMOD p]) : a - c ≡ b - c [PMOD p] := h.sub modEq_rfl protected theorem add_left_cancel' (c : α) : c + a ≡ c + b [PMOD p] → a ≡ b [PMOD p] := modEq_rfl.add_left_cancel protected theorem add_right_cancel' (c : α) : a + c ≡ b + c [PMOD p] → a ≡ b [PMOD p] := modEq_rfl.add_right_cancel protected theorem sub_left_cancel' (c : α) : c - a ≡ c - b [PMOD p] → a ≡ b [PMOD p] := modEq_rfl.sub_left_cancel protected theorem sub_right_cancel' (c : α) : a - c ≡ b - c [PMOD p] → a ≡ b [PMOD p] := modEq_rfl.sub_right_cancel end ModEq theorem modEq_sub_iff_add_modEq' : a ≡ b - c [PMOD p] ↔ c + a ≡ b [PMOD p] := by simp [ModEq, sub_sub] theorem modEq_sub_iff_add_modEq : a ≡ b - c [PMOD p] ↔ a + c ≡ b [PMOD p] := modEq_sub_iff_add_modEq'.trans <| by rw [add_comm] theorem sub_modEq_iff_modEq_add' : a - b ≡ c [PMOD p] ↔ a ≡ b + c [PMOD p] := modEq_comm.trans <| modEq_sub_iff_add_modEq'.trans modEq_comm theorem sub_modEq_iff_modEq_add : a - b ≡ c [PMOD p] ↔ a ≡ c + b [PMOD p] := modEq_comm.trans <| modEq_sub_iff_add_modEq.trans modEq_comm @[simp] theorem sub_modEq_zero : a - b ≡ 0 [PMOD p] ↔ a ≡ b [PMOD p] := by simp [sub_modEq_iff_modEq_add] @[simp] theorem add_modEq_left : a + b ≡ a [PMOD p] ↔ b ≡ 0 [PMOD p] := by simp [← modEq_sub_iff_add_modEq'] @[simp] theorem add_modEq_right : a + b ≡ b [PMOD p] ↔ a ≡ 0 [PMOD p] := by simp [← modEq_sub_iff_add_modEq] theorem modEq_iff_eq_add_zsmul : a ≡ b [PMOD p] ↔ ∃ z : ℤ, b = a + z • p := by simp_rw [ModEq, sub_eq_iff_eq_add'] theorem not_modEq_iff_ne_add_zsmul : ¬a ≡ b [PMOD p] ↔ ∀ z : ℤ, b ≠ a + z • p := by rw [modEq_iff_eq_add_zsmul, not_exists] theorem modEq_iff_eq_mod_zmultiples : a ≡ b [PMOD p] ↔ (b : α ⧸ AddSubgroup.zmultiples p) = a := by simp_rw [modEq_iff_eq_add_zsmul, QuotientAddGroup.eq_iff_sub_mem, AddSubgroup.mem_zmultiples_iff, eq_sub_iff_add_eq', eq_comm] theorem not_modEq_iff_ne_mod_zmultiples : ¬a ≡ b [PMOD p] ↔ (b : α ⧸ AddSubgroup.zmultiples p) ≠ a := modEq_iff_eq_mod_zmultiples.not end AddCommGroup @[simp] theorem modEq_iff_int_modEq {a b z : ℤ} : a ≡ b [PMOD z] ↔ a ≡ b [ZMOD z] := by simp [ModEq, dvd_iff_exists_eq_mul_left, Int.modEq_iff_dvd] section AddCommGroupWithOne variable [AddCommGroupWithOne α] [CharZero α] @[simp, norm_cast] theorem intCast_modEq_intCast {a b z : ℤ} : a ≡ b [PMOD (z : α)] ↔ a ≡ b [PMOD z] := by simp_rw [ModEq, ← Int.cast_mul_eq_zsmul_cast] norm_cast @[simp, norm_cast] lemma intCast_modEq_intCast' {a b : ℤ} {n : ℕ} : a ≡ b [PMOD (n : α)] ↔ a ≡ b [PMOD (n : ℤ)] := by simpa using intCast_modEq_intCast (α := α) (z := n) @[simp, norm_cast] theorem natCast_modEq_natCast {a b n : ℕ} : a ≡ b [PMOD (n : α)] ↔ a ≡ b [MOD n] := by simp_rw [← Int.natCast_modEq_iff, ← modEq_iff_int_modEq, ← @intCast_modEq_intCast α, Int.cast_natCast] alias ⟨ModEq.of_intCast, ModEq.intCast⟩ := intCast_modEq_intCast alias ⟨_root_.Nat.ModEq.of_natCast, ModEq.natCast⟩ := natCast_modEq_natCast end AddCommGroupWithOne section DivisionRing variable [DivisionRing α] {a b c p : α} @[simp] lemma div_modEq_div (hc : c ≠ 0) : a / c ≡ b / c [PMOD p] ↔ a ≡ b [PMOD (p * c)] := by simp [ModEq, ← sub_div, div_eq_iff hc, mul_assoc] end DivisionRing end AddCommGroup
Algebra\NeZero.lean
/- Copyright (c) 2021 Eric Rodriguez. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Rodriguez -/ import Mathlib.Logic.Basic import Mathlib.Algebra.Group.ZeroOne import Mathlib.Order.Defs /-! # `NeZero` typeclass We create a typeclass `NeZero n` which carries around the fact that `(n : R) ≠ 0`. ## Main declarations * `NeZero`: `n ≠ 0` as a typeclass. -/ variable {R : Type*} [Zero R] /-- A type-class version of `n ≠ 0`. -/ class NeZero (n : R) : Prop where /-- The proposition that `n` is not zero. -/ out : n ≠ 0 theorem NeZero.ne (n : R) [h : NeZero n] : n ≠ 0 := h.out theorem NeZero.ne' (n : R) [h : NeZero n] : 0 ≠ n := h.out.symm theorem neZero_iff {n : R} : NeZero n ↔ n ≠ 0 := ⟨fun h ↦ h.out, NeZero.mk⟩ @[simp] lemma neZero_zero_iff_false {α : Type*} [Zero α] : NeZero (0 : α) ↔ False := ⟨fun h ↦ h.ne rfl, fun h ↦ h.elim⟩ theorem not_neZero {n : R} : ¬NeZero n ↔ n = 0 := by simp [neZero_iff] theorem eq_zero_or_neZero (a : R) : a = 0 ∨ NeZero a := (eq_or_ne a 0).imp_right NeZero.mk section variable {α : Type*} [Zero α] @[simp] lemma zero_ne_one [One α] [NeZero (1 : α)] : (0 : α) ≠ 1 := NeZero.ne' (1 : α) @[simp] lemma one_ne_zero [One α] [NeZero (1 : α)] : (1 : α) ≠ 0 := NeZero.ne (1 : α) lemma ne_zero_of_eq_one [One α] [NeZero (1 : α)] {a : α} (h : a = 1) : a ≠ 0 := h ▸ one_ne_zero @[field_simps] lemma two_ne_zero [OfNat α 2] [NeZero (2 : α)] : (2 : α) ≠ 0 := NeZero.ne (2 : α) @[field_simps] lemma three_ne_zero [OfNat α 3] [NeZero (3 : α)] : (3 : α) ≠ 0 := NeZero.ne (3 : α) @[field_simps] lemma four_ne_zero [OfNat α 4] [NeZero (4 : α)] : (4 : α) ≠ 0 := NeZero.ne (4 : α) variable (α) lemma zero_ne_one' [One α] [NeZero (1 : α)] : (0 : α) ≠ 1 := zero_ne_one lemma one_ne_zero' [One α] [NeZero (1 : α)] : (1 : α) ≠ 0 := one_ne_zero lemma two_ne_zero' [OfNat α 2] [NeZero (2 : α)] : (2 : α) ≠ 0 := two_ne_zero lemma three_ne_zero' [OfNat α 3] [NeZero (3 : α)] : (3 : α) ≠ 0 := three_ne_zero lemma four_ne_zero' [OfNat α 4] [NeZero (4 : α)] : (4 : α) ≠ 0 := four_ne_zero end namespace NeZero variable {M : Type*} {x : M} instance succ {n : ℕ} : NeZero (n + 1) := ⟨n.succ_ne_zero⟩ theorem of_pos [Preorder M] [Zero M] (h : 0 < x) : NeZero x := ⟨ne_of_gt h⟩ end NeZero lemma Nat.pos_of_neZero (n : ℕ) [NeZero n] : 0 < n := Nat.pos_of_ne_zero (NeZero.ne _)
Algebra\Opposites.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Group.Defs import Mathlib.Logic.Equiv.Defs import Mathlib.Logic.Nontrivial.Basic import Mathlib.Logic.IsEmpty /-! # Multiplicative opposite and algebraic operations on it In this file we define `MulOpposite α = αᵐᵒᵖ` to be the multiplicative opposite of `α`. It inherits all additive algebraic structures on `α` (in other files), and reverses the order of multipliers in multiplicative structures, i.e., `op (x * y) = op y * op x`, where `MulOpposite.op` is the canonical map from `α` to `αᵐᵒᵖ`. We also define `AddOpposite α = αᵃᵒᵖ` to be the additive opposite of `α`. It inherits all multiplicative algebraic structures on `α` (in other files), and reverses the order of summands in additive structures, i.e. `op (x + y) = op y + op x`, where `AddOpposite.op` is the canonical map from `α` to `αᵃᵒᵖ`. ## Notation * `αᵐᵒᵖ = MulOpposite α` * `αᵃᵒᵖ = AddOpposite α` ## Implementation notes In mathlib3 `αᵐᵒᵖ` was just a type synonym for `α`, marked irreducible after the API was developed. In mathlib4 we use a structure with one field, because it is not possible to change the reducibility of a declaration after its definition, and because Lean 4 has definitional eta reduction for structures (Lean 3 does not). ## Tags multiplicative opposite, additive opposite -/ variable {α β : Type*} open Function /-- Auxiliary type to implement `MulOpposite` and `AddOpposite`. It turns out to be convenient to have `MulOpposite α = AddOpposite α` true by definition, in the same way that it is convenient to have `Additive α = α`; this means that we also get the defeq `AddOpposite (Additive α) = MulOpposite α`, which is convenient when working with quotients. This is a compromise between making `MulOpposite α = AddOpposite α = α` (what we had in Lean 3) and having no defeqs within those three types (which we had as of mathlib4#1036). -/ structure PreOpposite (α : Type*) : Type _ where /-- The element of `PreOpposite α` that represents `x : α`. -/ op' :: /-- The element of `α` represented by `x : PreOpposite α`. -/ unop' : α /-- Multiplicative opposite of a type. This type inherits all additive structures on `α` and reverses left and right in multiplication. -/ @[to_additive "Additive opposite of a type. This type inherits all multiplicative structures on `α` and reverses left and right in addition."] def MulOpposite (α : Type*) : Type _ := PreOpposite α /-- Multiplicative opposite of a type. -/ postfix:max "ᵐᵒᵖ" => MulOpposite /-- Additive opposite of a type. -/ postfix:max "ᵃᵒᵖ" => AddOpposite namespace MulOpposite /-- The element of `MulOpposite α` that represents `x : α`. -/ @[to_additive "The element of `αᵃᵒᵖ` that represents `x : α`."] def op : α → αᵐᵒᵖ := PreOpposite.op' /-- The element of `α` represented by `x : αᵐᵒᵖ`. -/ @[to_additive (attr := pp_nodot) "The element of `α` represented by `x : αᵃᵒᵖ`."] def unop : αᵐᵒᵖ → α := PreOpposite.unop' @[to_additive (attr := simp)] theorem unop_op (x : α) : unop (op x) = x := rfl @[to_additive (attr := simp)] theorem op_unop (x : αᵐᵒᵖ) : op (unop x) = x := rfl @[to_additive (attr := simp)] theorem op_comp_unop : (op : α → αᵐᵒᵖ) ∘ unop = id := rfl @[to_additive (attr := simp)] theorem unop_comp_op : (unop : αᵐᵒᵖ → α) ∘ op = id := rfl /-- A recursor for `MulOpposite`. Use as `induction x`. -/ @[to_additive (attr := simp, elab_as_elim, induction_eliminator, cases_eliminator) "A recursor for `AddOpposite`. Use as `induction x`."] protected def rec' {F : αᵐᵒᵖ → Sort*} (h : ∀ X, F (op X)) : ∀ X, F X := fun X ↦ h (unop X) /-- The canonical bijection between `α` and `αᵐᵒᵖ`. -/ @[to_additive (attr := simps (config := .asFn) apply symm_apply) "The canonical bijection between `α` and `αᵃᵒᵖ`."] def opEquiv : α ≃ αᵐᵒᵖ := ⟨op, unop, unop_op, op_unop⟩ @[to_additive] theorem op_bijective : Bijective (op : α → αᵐᵒᵖ) := opEquiv.bijective @[to_additive] theorem unop_bijective : Bijective (unop : αᵐᵒᵖ → α) := opEquiv.symm.bijective @[to_additive] theorem op_injective : Injective (op : α → αᵐᵒᵖ) := op_bijective.injective @[to_additive] theorem op_surjective : Surjective (op : α → αᵐᵒᵖ) := op_bijective.surjective @[to_additive] theorem unop_injective : Injective (unop : αᵐᵒᵖ → α) := unop_bijective.injective @[to_additive] theorem unop_surjective : Surjective (unop : αᵐᵒᵖ → α) := unop_bijective.surjective @[to_additive (attr := simp)] theorem op_inj {x y : α} : op x = op y ↔ x = y := iff_of_eq <| PreOpposite.op'.injEq _ _ @[to_additive (attr := simp, nolint simpComm)] theorem unop_inj {x y : αᵐᵒᵖ} : unop x = unop y ↔ x = y := unop_injective.eq_iff attribute [nolint simpComm] AddOpposite.unop_inj @[to_additive] instance instNontrivial [Nontrivial α] : Nontrivial αᵐᵒᵖ := op_injective.nontrivial @[to_additive] instance instInhabited [Inhabited α] : Inhabited αᵐᵒᵖ := ⟨op default⟩ @[to_additive] instance instSubsingleton [Subsingleton α] : Subsingleton αᵐᵒᵖ := unop_injective.subsingleton @[to_additive] instance instUnique [Unique α] : Unique αᵐᵒᵖ := Unique.mk' _ @[to_additive] instance instIsEmpty [IsEmpty α] : IsEmpty αᵐᵒᵖ := Function.isEmpty unop @[to_additive] instance instDecidableEq [DecidableEq α] : DecidableEq αᵐᵒᵖ := unop_injective.decidableEq instance instZero [Zero α] : Zero αᵐᵒᵖ where zero := op 0 @[to_additive] instance instOne [One α] : One αᵐᵒᵖ where one := op 1 instance instAdd [Add α] : Add αᵐᵒᵖ where add x y := op (unop x + unop y) instance instSub [Sub α] : Sub αᵐᵒᵖ where sub x y := op (unop x - unop y) instance instNeg [Neg α] : Neg αᵐᵒᵖ where neg x := op <| -unop x instance instInvolutiveNeg [InvolutiveNeg α] : InvolutiveNeg αᵐᵒᵖ where neg_neg _ := unop_injective <| neg_neg _ @[to_additive] instance instMul [Mul α] : Mul αᵐᵒᵖ where mul x y := op (unop y * unop x) @[to_additive] instance instInv [Inv α] : Inv αᵐᵒᵖ where inv x := op <| (unop x)⁻¹ @[to_additive] instance instInvolutiveInv [InvolutiveInv α] : InvolutiveInv αᵐᵒᵖ where inv_inv _ := unop_injective <| inv_inv _ @[to_additive] instance instSMul [SMul α β] : SMul α βᵐᵒᵖ where smul c x := op (c • unop x) @[simp] lemma op_zero [Zero α] : op (0 : α) = 0 := rfl @[simp] lemma unop_zero [Zero α] : unop (0 : αᵐᵒᵖ) = 0 := rfl @[to_additive (attr := simp)] lemma op_one [One α] : op (1 : α) = 1 := rfl @[to_additive (attr := simp)] lemma unop_one [One α] : unop (1 : αᵐᵒᵖ) = 1 := rfl @[simp] lemma op_add [Add α] (x y : α) : op (x + y) = op x + op y := rfl @[simp] lemma unop_add [Add α] (x y : αᵐᵒᵖ) : unop (x + y) = unop x + unop y := rfl @[simp] lemma op_neg [Neg α] (x : α) : op (-x) = -op x := rfl @[simp] lemma unop_neg [Neg α] (x : αᵐᵒᵖ) : unop (-x) = -unop x := rfl @[to_additive (attr := simp)] lemma op_mul [Mul α] (x y : α) : op (x * y) = op y * op x := rfl @[to_additive (attr := simp)] lemma unop_mul [Mul α] (x y : αᵐᵒᵖ) : unop (x * y) = unop y * unop x := rfl @[to_additive (attr := simp)] lemma op_inv [Inv α] (x : α) : op x⁻¹ = (op x)⁻¹ := rfl @[to_additive (attr := simp)] lemma unop_inv [Inv α] (x : αᵐᵒᵖ) : unop x⁻¹ = (unop x)⁻¹ := rfl @[simp] lemma op_sub [Sub α] (x y : α) : op (x - y) = op x - op y := rfl @[simp] lemma unop_sub [Sub α] (x y : αᵐᵒᵖ) : unop (x - y) = unop x - unop y := rfl @[to_additive (attr := simp)] lemma op_smul [SMul α β] (a : α) (b : β) : op (a • b) = a • op b := rfl @[to_additive (attr := simp)] lemma unop_smul [SMul α β] (a : α) (b : βᵐᵒᵖ) : unop (a • b) = a • unop b := rfl @[simp, nolint simpComm] theorem unop_eq_zero_iff [Zero α] (a : αᵐᵒᵖ) : a.unop = (0 : α) ↔ a = (0 : αᵐᵒᵖ) := unop_injective.eq_iff' rfl @[simp] theorem op_eq_zero_iff [Zero α] (a : α) : op a = (0 : αᵐᵒᵖ) ↔ a = (0 : α) := op_injective.eq_iff' rfl theorem unop_ne_zero_iff [Zero α] (a : αᵐᵒᵖ) : a.unop ≠ (0 : α) ↔ a ≠ (0 : αᵐᵒᵖ) := not_congr <| unop_eq_zero_iff a theorem op_ne_zero_iff [Zero α] (a : α) : op a ≠ (0 : αᵐᵒᵖ) ↔ a ≠ (0 : α) := not_congr <| op_eq_zero_iff a @[to_additive (attr := simp, nolint simpComm)] theorem unop_eq_one_iff [One α] (a : αᵐᵒᵖ) : a.unop = 1 ↔ a = 1 := unop_injective.eq_iff' rfl attribute [nolint simpComm] AddOpposite.unop_eq_zero_iff @[to_additive (attr := simp)] lemma op_eq_one_iff [One α] (a : α) : op a = 1 ↔ a = 1 := op_injective.eq_iff end MulOpposite namespace AddOpposite instance instOne [One α] : One αᵃᵒᵖ where one := op 1 @[simp] lemma op_one [One α] : op (1 : α) = 1 := rfl @[simp] lemma unop_one [One α] : unop 1 = (1 : α) := rfl @[simp] lemma op_eq_one_iff [One α] {a : α} : op a = 1 ↔ a = 1 := op_injective.eq_iff @[simp] lemma unop_eq_one_iff [One α] {a : αᵃᵒᵖ} : unop a = 1 ↔ a = 1 := unop_injective.eq_iff attribute [nolint simpComm] unop_eq_one_iff instance instMul [Mul α] : Mul αᵃᵒᵖ where mul a b := op (unop a * unop b) @[simp] lemma op_mul [Mul α] (a b : α) : op (a * b) = op a * op b := rfl @[simp] lemma unop_mul [Mul α] (a b : αᵃᵒᵖ) : unop (a * b) = unop a * unop b := rfl instance instInv [Inv α] : Inv αᵃᵒᵖ where inv a := op (unop a)⁻¹ instance instInvolutiveInv [InvolutiveInv α] : InvolutiveInv αᵃᵒᵖ where inv_inv _ := unop_injective <| inv_inv _ @[simp] lemma op_inv [Inv α] (a : α) : op a⁻¹ = (op a)⁻¹ := rfl @[simp] lemma unop_inv [Inv α] (a : αᵃᵒᵖ) : unop a⁻¹ = (unop a)⁻¹ := rfl instance instDiv [Div α] : Div αᵃᵒᵖ where div a b := op (unop a / unop b) @[simp] lemma op_div [Div α] (a b : α) : op (a / b) = op a / op b := rfl @[simp] lemma unop_div [Div α] (a b : αᵃᵒᵖ) : unop (a / b) = unop a / unop b := rfl end AddOpposite
Algebra\PEmptyInstances.lean
/- Copyright (c) 2021 Julian Kuelshammer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Julian Kuelshammer -/ import Mathlib.Algebra.Group.Defs import Mathlib.Tactic.ToAdditive /-! # Instances on pempty This file collects facts about algebraic structures on the (universe-polymorphic) empty type, e.g. that it is a semigroup. -/ universe u @[to_additive] instance SemigroupPEmpty : Semigroup PEmpty.{u + 1} where mul x _ := by cases x mul_assoc x y z := by cases x
Algebra\Periodic.lean
/- Copyright (c) 2021 Benjamin Davidson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Benjamin Davidson -/ import Mathlib.Algebra.Field.Opposite import Mathlib.Algebra.Group.Subgroup.ZPowers import Mathlib.Algebra.Group.Submonoid.Membership import Mathlib.Algebra.Ring.NegOnePow import Mathlib.Algebra.Order.Archimedean.Basic import Mathlib.GroupTheory.Coset /-! # Periodicity In this file we define and then prove facts about periodic and antiperiodic functions. ## Main definitions * `Function.Periodic`: A function `f` is *periodic* if `∀ x, f (x + c) = f x`. `f` is referred to as periodic with period `c` or `c`-periodic. * `Function.Antiperiodic`: A function `f` is *antiperiodic* if `∀ x, f (x + c) = -f x`. `f` is referred to as antiperiodic with antiperiod `c` or `c`-antiperiodic. Note that any `c`-antiperiodic function will necessarily also be `2 • c`-periodic. ## Tags period, periodic, periodicity, antiperiodic -/ variable {α β γ : Type*} {f g : α → β} {c c₁ c₂ x : α} open Set namespace Function /-! ### Periodicity -/ /-- A function `f` is said to be `Periodic` with period `c` if for all `x`, `f (x + c) = f x`. -/ @[simp] def Periodic [Add α] (f : α → β) (c : α) : Prop := ∀ x : α, f (x + c) = f x protected theorem Periodic.funext [Add α] (h : Periodic f c) : (fun x => f (x + c)) = f := funext h protected theorem Periodic.comp [Add α] (h : Periodic f c) (g : β → γ) : Periodic (g ∘ f) c := by simp_all theorem Periodic.comp_addHom [Add α] [Add γ] (h : Periodic f c) (g : AddHom γ α) (g_inv : α → γ) (hg : RightInverse g_inv g) : Periodic (f ∘ g) (g_inv c) := fun x => by simp only [hg c, h (g x), map_add, comp_apply] @[to_additive] protected theorem Periodic.mul [Add α] [Mul β] (hf : Periodic f c) (hg : Periodic g c) : Periodic (f * g) c := by simp_all @[to_additive] protected theorem Periodic.div [Add α] [Div β] (hf : Periodic f c) (hg : Periodic g c) : Periodic (f / g) c := by simp_all @[to_additive] theorem _root_.List.periodic_prod [Add α] [Monoid β] (l : List (α → β)) (hl : ∀ f ∈ l, Periodic f c) : Periodic l.prod c := by induction' l with g l ih hl · simp · rw [List.forall_mem_cons] at hl simpa only [List.prod_cons] using hl.1.mul (ih hl.2) @[to_additive] theorem _root_.Multiset.periodic_prod [Add α] [CommMonoid β] (s : Multiset (α → β)) (hs : ∀ f ∈ s, Periodic f c) : Periodic s.prod c := (s.prod_toList ▸ s.toList.periodic_prod) fun f hf => hs f <| Multiset.mem_toList.mp hf @[to_additive] theorem _root_.Finset.periodic_prod [Add α] [CommMonoid β] {ι : Type*} {f : ι → α → β} (s : Finset ι) (hs : ∀ i ∈ s, Periodic (f i) c) : Periodic (∏ i ∈ s, f i) c := s.prod_to_list f ▸ (s.toList.map f).periodic_prod (by simpa [-Periodic] ) @[to_additive] protected theorem Periodic.smul [Add α] [SMul γ β] (h : Periodic f c) (a : γ) : Periodic (a • f) c := by simp_all protected theorem Periodic.const_smul [AddMonoid α] [Group γ] [DistribMulAction γ α] (h : Periodic f c) (a : γ) : Periodic (fun x => f (a • x)) (a⁻¹ • c) := fun x => by simpa only [smul_add, smul_inv_smul] using h (a • x) protected theorem Periodic.const_smul₀ [AddCommMonoid α] [DivisionSemiring γ] [Module γ α] (h : Periodic f c) (a : γ) : Periodic (fun x => f (a • x)) (a⁻¹ • c) := fun x => by by_cases ha : a = 0 · simp only [ha, zero_smul] · simpa only [smul_add, smul_inv_smul₀ ha] using h (a • x) protected theorem Periodic.const_mul [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (a * x)) (a⁻¹ * c) := Periodic.const_smul₀ h a theorem Periodic.const_inv_smul [AddMonoid α] [Group γ] [DistribMulAction γ α] (h : Periodic f c) (a : γ) : Periodic (fun x => f (a⁻¹ • x)) (a • c) := by simpa only [inv_inv] using h.const_smul a⁻¹ theorem Periodic.const_inv_smul₀ [AddCommMonoid α] [DivisionSemiring γ] [Module γ α] (h : Periodic f c) (a : γ) : Periodic (fun x => f (a⁻¹ • x)) (a • c) := by simpa only [inv_inv] using h.const_smul₀ a⁻¹ theorem Periodic.const_inv_mul [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (a⁻¹ * x)) (a * c) := h.const_inv_smul₀ a theorem Periodic.mul_const [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x * a)) (c * a⁻¹) := h.const_smul₀ (MulOpposite.op a) theorem Periodic.mul_const' [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x * a)) (c / a) := by simpa only [div_eq_mul_inv] using h.mul_const a theorem Periodic.mul_const_inv [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x * a⁻¹)) (c * a) := h.const_inv_smul₀ (MulOpposite.op a) theorem Periodic.div_const [DivisionSemiring α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x / a)) (c * a) := by simpa only [div_eq_mul_inv] using h.mul_const_inv a theorem Periodic.add_period [AddSemigroup α] (h1 : Periodic f c₁) (h2 : Periodic f c₂) : Periodic f (c₁ + c₂) := by simp_all [← add_assoc] theorem Periodic.sub_eq [AddGroup α] (h : Periodic f c) (x : α) : f (x - c) = f x := by simpa only [sub_add_cancel] using (h (x - c)).symm theorem Periodic.sub_eq' [AddCommGroup α] (h : Periodic f c) : f (c - x) = f (-x) := by simpa only [sub_eq_neg_add] using h (-x) protected theorem Periodic.neg [AddGroup α] (h : Periodic f c) : Periodic f (-c) := by simpa only [sub_eq_add_neg, Periodic] using h.sub_eq theorem Periodic.sub_period [AddGroup α] (h1 : Periodic f c₁) (h2 : Periodic f c₂) : Periodic f (c₁ - c₂) := fun x => by rw [sub_eq_add_neg, ← add_assoc, h2.neg, h1] theorem Periodic.const_add [AddSemigroup α] (h : Periodic f c) (a : α) : Periodic (fun x => f (a + x)) c := fun x => by simpa [add_assoc] using h (a + x) theorem Periodic.add_const [AddCommSemigroup α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x + a)) c := fun x => by simpa only [add_right_comm] using h (x + a) theorem Periodic.const_sub [AddCommGroup α] (h : Periodic f c) (a : α) : Periodic (fun x => f (a - x)) c := fun x => by simp only [← sub_sub, h.sub_eq] theorem Periodic.sub_const [AddCommGroup α] (h : Periodic f c) (a : α) : Periodic (fun x => f (x - a)) c := by simpa only [sub_eq_add_neg] using h.add_const (-a) theorem Periodic.nsmul [AddMonoid α] (h : Periodic f c) (n : ℕ) : Periodic f (n • c) := by induction n <;> simp_all [add_nsmul, ← add_assoc, zero_nsmul] theorem Periodic.nat_mul [Semiring α] (h : Periodic f c) (n : ℕ) : Periodic f (n * c) := by simpa only [nsmul_eq_mul] using h.nsmul n theorem Periodic.neg_nsmul [AddGroup α] (h : Periodic f c) (n : ℕ) : Periodic f (-(n • c)) := (h.nsmul n).neg theorem Periodic.neg_nat_mul [Ring α] (h : Periodic f c) (n : ℕ) : Periodic f (-(n * c)) := (h.nat_mul n).neg theorem Periodic.sub_nsmul_eq [AddGroup α] (h : Periodic f c) (n : ℕ) : f (x - n • c) = f x := by simpa only [sub_eq_add_neg] using h.neg_nsmul n x theorem Periodic.sub_nat_mul_eq [Ring α] (h : Periodic f c) (n : ℕ) : f (x - n * c) = f x := by simpa only [nsmul_eq_mul] using h.sub_nsmul_eq n theorem Periodic.nsmul_sub_eq [AddCommGroup α] (h : Periodic f c) (n : ℕ) : f (n • c - x) = f (-x) := (h.nsmul n).sub_eq' theorem Periodic.nat_mul_sub_eq [Ring α] (h : Periodic f c) (n : ℕ) : f (n * c - x) = f (-x) := by simpa only [sub_eq_neg_add] using h.nat_mul n (-x) protected theorem Periodic.zsmul [AddGroup α] (h : Periodic f c) (n : ℤ) : Periodic f (n • c) := by cases' n with n n · simpa only [Int.ofNat_eq_coe, natCast_zsmul] using h.nsmul n · simpa only [negSucc_zsmul] using (h.nsmul (n + 1)).neg protected theorem Periodic.int_mul [Ring α] (h : Periodic f c) (n : ℤ) : Periodic f (n * c) := by simpa only [zsmul_eq_mul] using h.zsmul n theorem Periodic.sub_zsmul_eq [AddGroup α] (h : Periodic f c) (n : ℤ) : f (x - n • c) = f x := (h.zsmul n).sub_eq x theorem Periodic.sub_int_mul_eq [Ring α] (h : Periodic f c) (n : ℤ) : f (x - n * c) = f x := (h.int_mul n).sub_eq x theorem Periodic.zsmul_sub_eq [AddCommGroup α] (h : Periodic f c) (n : ℤ) : f (n • c - x) = f (-x) := (h.zsmul _).sub_eq' theorem Periodic.int_mul_sub_eq [Ring α] (h : Periodic f c) (n : ℤ) : f (n * c - x) = f (-x) := (h.int_mul _).sub_eq' protected theorem Periodic.eq [AddZeroClass α] (h : Periodic f c) : f c = f 0 := by simpa only [zero_add] using h 0 protected theorem Periodic.neg_eq [AddGroup α] (h : Periodic f c) : f (-c) = f 0 := h.neg.eq protected theorem Periodic.nsmul_eq [AddMonoid α] (h : Periodic f c) (n : ℕ) : f (n • c) = f 0 := (h.nsmul n).eq theorem Periodic.nat_mul_eq [Semiring α] (h : Periodic f c) (n : ℕ) : f (n * c) = f 0 := (h.nat_mul n).eq theorem Periodic.zsmul_eq [AddGroup α] (h : Periodic f c) (n : ℤ) : f (n • c) = f 0 := (h.zsmul n).eq theorem Periodic.int_mul_eq [Ring α] (h : Periodic f c) (n : ℤ) : f (n * c) = f 0 := (h.int_mul n).eq /-- If a function `f` is `Periodic` with positive period `c`, then for all `x` there exists some `y ∈ Ico 0 c` such that `f x = f y`. -/ theorem Periodic.exists_mem_Ico₀ [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : 0 < c) (x) : ∃ y ∈ Ico 0 c, f x = f y := let ⟨n, H, _⟩ := existsUnique_zsmul_near_of_pos' hc x ⟨x - n • c, H, (h.sub_zsmul_eq n).symm⟩ /-- If a function `f` is `Periodic` with positive period `c`, then for all `x` there exists some `y ∈ Ico a (a + c)` such that `f x = f y`. -/ theorem Periodic.exists_mem_Ico [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : 0 < c) (x a) : ∃ y ∈ Ico a (a + c), f x = f y := let ⟨n, H, _⟩ := existsUnique_add_zsmul_mem_Ico hc x a ⟨x + n • c, H, (h.zsmul n x).symm⟩ /-- If a function `f` is `Periodic` with positive period `c`, then for all `x` there exists some `y ∈ Ioc a (a + c)` such that `f x = f y`. -/ theorem Periodic.exists_mem_Ioc [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : 0 < c) (x a) : ∃ y ∈ Ioc a (a + c), f x = f y := let ⟨n, H, _⟩ := existsUnique_add_zsmul_mem_Ioc hc x a ⟨x + n • c, H, (h.zsmul n x).symm⟩ theorem Periodic.image_Ioc [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : 0 < c) (a : α) : f '' Ioc a (a + c) = range f := (image_subset_range _ _).antisymm <| range_subset_iff.2 fun x => let ⟨y, hy, hyx⟩ := h.exists_mem_Ioc hc x a ⟨y, hy, hyx.symm⟩ theorem Periodic.image_Icc [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : 0 < c) (a : α) : f '' Icc a (a + c) = range f := (image_subset_range _ _).antisymm <| h.image_Ioc hc a ▸ image_subset _ Ioc_subset_Icc_self theorem Periodic.image_uIcc [LinearOrderedAddCommGroup α] [Archimedean α] (h : Periodic f c) (hc : c ≠ 0) (a : α) : f '' uIcc a (a + c) = range f := by cases hc.lt_or_lt with | inl hc => rw [uIcc_of_ge (add_le_of_nonpos_right hc.le), ← h.neg.image_Icc (neg_pos.2 hc) (a + c), add_neg_cancel_right] | inr hc => rw [uIcc_of_le (le_add_of_nonneg_right hc.le), h.image_Icc hc] theorem periodic_with_period_zero [AddZeroClass α] (f : α → β) : Periodic f 0 := fun x => by rw [add_zero] theorem Periodic.map_vadd_zmultiples [AddCommGroup α] (hf : Periodic f c) (a : AddSubgroup.zmultiples c) (x : α) : f (a +ᵥ x) = f x := by rcases a with ⟨_, m, rfl⟩ simp [AddSubgroup.vadd_def, add_comm _ x, hf.zsmul m x] theorem Periodic.map_vadd_multiples [AddCommMonoid α] (hf : Periodic f c) (a : AddSubmonoid.multiples c) (x : α) : f (a +ᵥ x) = f x := by rcases a with ⟨_, m, rfl⟩ simp [AddSubmonoid.vadd_def, add_comm _ x, hf.nsmul m x] /-- Lift a periodic function to a function from the quotient group. -/ def Periodic.lift [AddGroup α] (h : Periodic f c) (x : α ⧸ AddSubgroup.zmultiples c) : β := Quotient.liftOn' x f fun a b h' => by rw [QuotientAddGroup.leftRel_apply] at h' obtain ⟨k, hk⟩ := h' exact (h.zsmul k _).symm.trans (congr_arg f (add_eq_of_eq_neg_add hk)) @[simp] theorem Periodic.lift_coe [AddGroup α] (h : Periodic f c) (a : α) : h.lift (a : α ⧸ AddSubgroup.zmultiples c) = f a := rfl /-- A periodic function `f : R → X` on a semiring (or, more generally, `AddZeroClass`) of non-zero period is not injective. -/ lemma Periodic.not_injective {R X : Type*} [AddZeroClass R] {f : R → X} {c : R} (hf : Periodic f c) (hc : c ≠ 0) : ¬ Injective f := fun h ↦ hc <| h hf.eq /-! ### Antiperiodicity -/ /-- A function `f` is said to be `antiperiodic` with antiperiod `c` if for all `x`, `f (x + c) = -f x`. -/ @[simp] def Antiperiodic [Add α] [Neg β] (f : α → β) (c : α) : Prop := ∀ x : α, f (x + c) = -f x protected theorem Antiperiodic.funext [Add α] [Neg β] (h : Antiperiodic f c) : (fun x => f (x + c)) = -f := funext h protected theorem Antiperiodic.funext' [Add α] [InvolutiveNeg β] (h : Antiperiodic f c) : (fun x => -f (x + c)) = f := neg_eq_iff_eq_neg.mpr h.funext /-- If a function is `antiperiodic` with antiperiod `c`, then it is also `Periodic` with period `2 • c`. -/ protected theorem Antiperiodic.periodic [AddMonoid α] [InvolutiveNeg β] (h : Antiperiodic f c) : Periodic f (2 • c) := by simp [two_nsmul, ← add_assoc, h _] /-- If a function is `antiperiodic` with antiperiod `c`, then it is also `Periodic` with period `2 * c`. -/ protected theorem Antiperiodic.periodic_two_mul [Semiring α] [InvolutiveNeg β] (h : Antiperiodic f c) : Periodic f (2 * c) := nsmul_eq_mul 2 c ▸ h.periodic protected theorem Antiperiodic.eq [AddZeroClass α] [Neg β] (h : Antiperiodic f c) : f c = -f 0 := by simpa only [zero_add] using h 0 theorem Antiperiodic.even_nsmul_periodic [AddMonoid α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℕ) : Periodic f ((2 * n) • c) := mul_nsmul c 2 n ▸ h.periodic.nsmul n theorem Antiperiodic.nat_even_mul_periodic [Semiring α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℕ) : Periodic f (n * (2 * c)) := h.periodic_two_mul.nat_mul n theorem Antiperiodic.odd_nsmul_antiperiodic [AddMonoid α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℕ) : Antiperiodic f ((2 * n + 1) • c) := fun x => by rw [add_nsmul, one_nsmul, ← add_assoc, h, h.even_nsmul_periodic] theorem Antiperiodic.nat_odd_mul_antiperiodic [Semiring α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℕ) : Antiperiodic f (n * (2 * c) + c) := fun x => by rw [← add_assoc, h, h.nat_even_mul_periodic] theorem Antiperiodic.even_zsmul_periodic [AddGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℤ) : Periodic f ((2 * n) • c) := by rw [mul_comm, mul_zsmul, two_zsmul, ← two_nsmul] exact h.periodic.zsmul n theorem Antiperiodic.int_even_mul_periodic [Ring α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℤ) : Periodic f (n * (2 * c)) := h.periodic_two_mul.int_mul n theorem Antiperiodic.odd_zsmul_antiperiodic [AddGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℤ) : Antiperiodic f ((2 * n + 1) • c) := by intro x rw [add_zsmul, one_zsmul, ← add_assoc, h, h.even_zsmul_periodic] theorem Antiperiodic.int_odd_mul_antiperiodic [Ring α] [InvolutiveNeg β] (h : Antiperiodic f c) (n : ℤ) : Antiperiodic f (n * (2 * c) + c) := fun x => by rw [← add_assoc, h, h.int_even_mul_periodic] theorem Antiperiodic.sub_eq [AddGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) (x : α) : f (x - c) = -f x := by simp only [← neg_eq_iff_eq_neg, ← h (x - c), sub_add_cancel] theorem Antiperiodic.sub_eq' [AddCommGroup α] [Neg β] (h : Antiperiodic f c) : f (c - x) = -f (-x) := by simpa only [sub_eq_neg_add] using h (-x) protected theorem Antiperiodic.neg [AddGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) : Antiperiodic f (-c) := by simpa only [sub_eq_add_neg, Antiperiodic] using h.sub_eq theorem Antiperiodic.neg_eq [AddGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) : f (-c) = -f 0 := by simpa only [zero_add] using h.neg 0 theorem Antiperiodic.nat_mul_eq_of_eq_zero [Semiring α] [NegZeroClass β] (h : Antiperiodic f c) (hi : f 0 = 0) : ∀ n : ℕ, f (n * c) = 0 | 0 => by rwa [Nat.cast_zero, zero_mul] | n + 1 => by simp [add_mul, h _, Antiperiodic.nat_mul_eq_of_eq_zero h hi n] theorem Antiperiodic.int_mul_eq_of_eq_zero [Ring α] [SubtractionMonoid β] (h : Antiperiodic f c) (hi : f 0 = 0) : ∀ n : ℤ, f (n * c) = 0 | (n : ℕ) => by rw [Int.cast_natCast, h.nat_mul_eq_of_eq_zero hi n] | .negSucc n => by rw [Int.cast_negSucc, neg_mul, ← mul_neg, h.neg.nat_mul_eq_of_eq_zero hi] theorem Antiperiodic.add_zsmul_eq [AddGroup α] [AddGroup β] (h : Antiperiodic f c) (n : ℤ) : f (x + n • c) = (n.negOnePow : ℤ) • f x := by rcases Int.even_or_odd' n with ⟨k, rfl | rfl⟩ · rw [h.even_zsmul_periodic, Int.negOnePow_two_mul, Units.val_one, one_zsmul] · rw [h.odd_zsmul_antiperiodic, Int.negOnePow_two_mul_add_one, Units.val_neg, Units.val_one, neg_zsmul, one_zsmul] theorem Antiperiodic.sub_zsmul_eq [AddGroup α] [AddGroup β] (h : Antiperiodic f c) (n : ℤ) : f (x - n • c) = (n.negOnePow : ℤ) • f x := by simpa only [sub_eq_add_neg, neg_zsmul, Int.negOnePow_neg] using h.add_zsmul_eq (-n) theorem Antiperiodic.zsmul_sub_eq [AddCommGroup α] [AddGroup β] (h : Antiperiodic f c) (n : ℤ) : f (n • c - x) = (n.negOnePow : ℤ) • f (-x) := by rw [sub_eq_add_neg, add_comm] exact h.add_zsmul_eq n theorem Antiperiodic.add_int_mul_eq [Ring α] [Ring β] (h : Antiperiodic f c) (n : ℤ) : f (x + n * c) = (n.negOnePow : ℤ) * f x := by simpa only [zsmul_eq_mul] using h.add_zsmul_eq n theorem Antiperiodic.sub_int_mul_eq [Ring α] [Ring β] (h : Antiperiodic f c) (n : ℤ) : f (x - n * c) = (n.negOnePow : ℤ) * f x := by simpa only [zsmul_eq_mul] using h.sub_zsmul_eq n theorem Antiperiodic.int_mul_sub_eq [Ring α] [Ring β] (h : Antiperiodic f c) (n : ℤ) : f (n * c - x) = (n.negOnePow : ℤ) * f (-x) := by simpa only [zsmul_eq_mul] using h.zsmul_sub_eq n theorem Antiperiodic.add_nsmul_eq [AddMonoid α] [AddGroup β] (h : Antiperiodic f c) (n : ℕ) : f (x + n • c) = (-1) ^ n • f x := by rcases Nat.even_or_odd' n with ⟨k, rfl | rfl⟩ · rw [h.even_nsmul_periodic, pow_mul, (by norm_num : (-1) ^ 2 = 1), one_pow, one_zsmul] · rw [h.odd_nsmul_antiperiodic, pow_add, pow_mul, (by norm_num : (-1) ^ 2 = 1), one_pow, pow_one, one_mul, neg_zsmul, one_zsmul] theorem Antiperiodic.sub_nsmul_eq [AddGroup α] [AddGroup β] (h : Antiperiodic f c) (n : ℕ) : f (x - n • c) = (-1) ^ n • f x := by simpa only [Int.reduceNeg, natCast_zsmul] using h.sub_zsmul_eq n theorem Antiperiodic.nsmul_sub_eq [AddCommGroup α] [AddGroup β] (h : Antiperiodic f c) (n : ℕ) : f (n • c - x) = (-1) ^ n • f (-x) := by simpa only [Int.reduceNeg, natCast_zsmul] using h.zsmul_sub_eq n theorem Antiperiodic.add_nat_mul_eq [Semiring α] [Ring β] (h : Antiperiodic f c) (n : ℕ) : f (x + n * c) = (-1) ^ n * f x := by simpa only [nsmul_eq_mul, zsmul_eq_mul, Int.cast_pow, Int.cast_neg, Int.cast_one] using h.add_nsmul_eq n theorem Antiperiodic.sub_nat_mul_eq [Ring α] [Ring β] (h : Antiperiodic f c) (n : ℕ) : f (x - n * c) = (-1) ^ n * f x := by simpa only [nsmul_eq_mul, zsmul_eq_mul, Int.cast_pow, Int.cast_neg, Int.cast_one] using h.sub_nsmul_eq n theorem Antiperiodic.nat_mul_sub_eq [Ring α] [Ring β] (h : Antiperiodic f c) (n : ℕ) : f (n * c - x) = (-1) ^ n * f (-x) := by simpa only [nsmul_eq_mul, zsmul_eq_mul, Int.cast_pow, Int.cast_neg, Int.cast_one] using h.nsmul_sub_eq n theorem Antiperiodic.const_add [AddSemigroup α] [Neg β] (h : Antiperiodic f c) (a : α) : Antiperiodic (fun x => f (a + x)) c := fun x => by simpa [add_assoc] using h (a + x) theorem Antiperiodic.add_const [AddCommSemigroup α] [Neg β] (h : Antiperiodic f c) (a : α) : Antiperiodic (fun x => f (x + a)) c := fun x => by simpa only [add_right_comm] using h (x + a) theorem Antiperiodic.const_sub [AddCommGroup α] [InvolutiveNeg β] (h : Antiperiodic f c) (a : α) : Antiperiodic (fun x => f (a - x)) c := fun x => by simp only [← sub_sub, h.sub_eq] theorem Antiperiodic.sub_const [AddCommGroup α] [Neg β] (h : Antiperiodic f c) (a : α) : Antiperiodic (fun x => f (x - a)) c := by simpa only [sub_eq_add_neg] using h.add_const (-a) theorem Antiperiodic.smul [Add α] [Monoid γ] [AddGroup β] [DistribMulAction γ β] (h : Antiperiodic f c) (a : γ) : Antiperiodic (a • f) c := by simp_all theorem Antiperiodic.const_smul [AddMonoid α] [Neg β] [Group γ] [DistribMulAction γ α] (h : Antiperiodic f c) (a : γ) : Antiperiodic (fun x => f (a • x)) (a⁻¹ • c) := fun x => by simpa only [smul_add, smul_inv_smul] using h (a • x) theorem Antiperiodic.const_smul₀ [AddCommMonoid α] [Neg β] [DivisionSemiring γ] [Module γ α] (h : Antiperiodic f c) {a : γ} (ha : a ≠ 0) : Antiperiodic (fun x => f (a • x)) (a⁻¹ • c) := fun x => by simpa only [smul_add, smul_inv_smul₀ ha] using h (a • x) theorem Antiperiodic.const_mul [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (a * x)) (a⁻¹ * c) := h.const_smul₀ ha theorem Antiperiodic.const_inv_smul [AddMonoid α] [Neg β] [Group γ] [DistribMulAction γ α] (h : Antiperiodic f c) (a : γ) : Antiperiodic (fun x => f (a⁻¹ • x)) (a • c) := by simpa only [inv_inv] using h.const_smul a⁻¹ theorem Antiperiodic.const_inv_smul₀ [AddCommMonoid α] [Neg β] [DivisionSemiring γ] [Module γ α] (h : Antiperiodic f c) {a : γ} (ha : a ≠ 0) : Antiperiodic (fun x => f (a⁻¹ • x)) (a • c) := by simpa only [inv_inv] using h.const_smul₀ (inv_ne_zero ha) theorem Antiperiodic.const_inv_mul [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (a⁻¹ * x)) (a * c) := h.const_inv_smul₀ ha theorem Antiperiodic.mul_const [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (x * a)) (c * a⁻¹) := h.const_smul₀ <| (MulOpposite.op_ne_zero_iff a).mpr ha theorem Antiperiodic.mul_const' [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (x * a)) (c / a) := by simpa only [div_eq_mul_inv] using h.mul_const ha theorem Antiperiodic.mul_const_inv [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (x * a⁻¹)) (c * a) := h.const_inv_smul₀ <| (MulOpposite.op_ne_zero_iff a).mpr ha theorem Antiperiodic.div_inv [DivisionSemiring α] [Neg β] (h : Antiperiodic f c) {a : α} (ha : a ≠ 0) : Antiperiodic (fun x => f (x / a)) (c * a) := by simpa only [div_eq_mul_inv] using h.mul_const_inv ha theorem Antiperiodic.add [AddGroup α] [InvolutiveNeg β] (h1 : Antiperiodic f c₁) (h2 : Antiperiodic f c₂) : Periodic f (c₁ + c₂) := by simp_all [← add_assoc] theorem Antiperiodic.sub [AddGroup α] [InvolutiveNeg β] (h1 : Antiperiodic f c₁) (h2 : Antiperiodic f c₂) : Periodic f (c₁ - c₂) := by simpa only [sub_eq_add_neg] using h1.add h2.neg theorem Periodic.add_antiperiod [AddGroup α] [Neg β] (h1 : Periodic f c₁) (h2 : Antiperiodic f c₂) : Antiperiodic f (c₁ + c₂) := by simp_all [← add_assoc] theorem Periodic.sub_antiperiod [AddGroup α] [InvolutiveNeg β] (h1 : Periodic f c₁) (h2 : Antiperiodic f c₂) : Antiperiodic f (c₁ - c₂) := by simpa only [sub_eq_add_neg] using h1.add_antiperiod h2.neg theorem Periodic.add_antiperiod_eq [AddGroup α] [Neg β] (h1 : Periodic f c₁) (h2 : Antiperiodic f c₂) : f (c₁ + c₂) = -f 0 := (h1.add_antiperiod h2).eq theorem Periodic.sub_antiperiod_eq [AddGroup α] [InvolutiveNeg β] (h1 : Periodic f c₁) (h2 : Antiperiodic f c₂) : f (c₁ - c₂) = -f 0 := (h1.sub_antiperiod h2).eq theorem Antiperiodic.mul [Add α] [Mul β] [HasDistribNeg β] (hf : Antiperiodic f c) (hg : Antiperiodic g c) : Periodic (f * g) c := by simp_all theorem Antiperiodic.div [Add α] [DivisionMonoid β] [HasDistribNeg β] (hf : Antiperiodic f c) (hg : Antiperiodic g c) : Periodic (f / g) c := by simp_all [neg_div_neg_eq] end Function theorem Int.fract_periodic (α) [LinearOrderedRing α] [FloorRing α] : Function.Periodic Int.fract (1 : α) := fun a => mod_cast Int.fract_add_int a 1
Algebra\QuadraticDiscriminant.lean
/- Copyright (c) 2019 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou -/ import Mathlib.Order.Filter.AtTopBot import Mathlib.Tactic.FieldSimp import Mathlib.Tactic.LinearCombination import Mathlib.Tactic.Linarith.Frontend /-! # Quadratic discriminants and roots of a quadratic This file defines the discriminant of a quadratic and gives the solution to a quadratic equation. ## Main definition - `discrim a b c`: the discriminant of a quadratic `a * x * x + b * x + c` is `b * b - 4 * a * c`. ## Main statements - `quadratic_eq_zero_iff`: roots of a quadratic can be written as `(-b + s) / (2 * a)` or `(-b - s) / (2 * a)`, where `s` is a square root of the discriminant. - `quadratic_ne_zero_of_discrim_ne_sq`: if the discriminant has no square root, then the corresponding quadratic has no root. - `discrim_le_zero`: if a quadratic is always non-negative, then its discriminant is non-positive. - `discrim_le_zero_of_nonpos`, `discrim_lt_zero`, `discrim_lt_zero_of_neg`: versions of this statement with other inequalities. ## Tags polynomial, quadratic, discriminant, root -/ open Filter section Ring variable {R : Type*} /-- Discriminant of a quadratic -/ def discrim [Ring R] (a b c : R) : R := b ^ 2 - 4 * a * c @[simp] lemma discrim_neg [Ring R] (a b c : R) : discrim (-a) (-b) (-c) = discrim a b c := by simp [discrim] variable [CommRing R] {a b c : R} lemma discrim_eq_sq_of_quadratic_eq_zero {x : R} (h : a * x * x + b * x + c = 0) : discrim a b c = (2 * a * x + b) ^ 2 := by rw [discrim] linear_combination -4 * a * h /-- A quadratic has roots if and only if its discriminant equals some square. -/ theorem quadratic_eq_zero_iff_discrim_eq_sq [NeZero (2 : R)] [NoZeroDivisors R] (ha : a ≠ 0) (x : R) : a * x * x + b * x + c = 0 ↔ discrim a b c = (2 * a * x + b) ^ 2 := by refine ⟨discrim_eq_sq_of_quadratic_eq_zero, fun h ↦ ?_⟩ rw [discrim] at h have ha : 2 * 2 * a ≠ 0 := mul_ne_zero (mul_ne_zero (NeZero.ne _) (NeZero.ne _)) ha apply mul_left_cancel₀ ha linear_combination -h /-- A quadratic has no root if its discriminant has no square root. -/ theorem quadratic_ne_zero_of_discrim_ne_sq (h : ∀ s : R, discrim a b c ≠ s^2) (x : R) : a * x * x + b * x + c ≠ 0 := mt discrim_eq_sq_of_quadratic_eq_zero (h _) end Ring section Field variable {K : Type*} [Field K] [NeZero (2 : K)] {a b c x : K} /-- Roots of a quadratic equation. -/ theorem quadratic_eq_zero_iff (ha : a ≠ 0) {s : K} (h : discrim a b c = s * s) (x : K) : a * x * x + b * x + c = 0 ↔ x = (-b + s) / (2 * a) ∨ x = (-b - s) / (2 * a) := by rw [quadratic_eq_zero_iff_discrim_eq_sq ha, h, sq, mul_self_eq_mul_self_iff] field_simp apply or_congr · constructor <;> intro h' <;> linear_combination -h' · constructor <;> intro h' <;> linear_combination h' /-- A quadratic has roots if its discriminant has square roots -/ theorem exists_quadratic_eq_zero (ha : a ≠ 0) (h : ∃ s, discrim a b c = s * s) : ∃ x, a * x * x + b * x + c = 0 := by rcases h with ⟨s, hs⟩ use (-b + s) / (2 * a) rw [quadratic_eq_zero_iff ha hs] simp /-- Root of a quadratic when its discriminant equals zero -/ theorem quadratic_eq_zero_iff_of_discrim_eq_zero (ha : a ≠ 0) (h : discrim a b c = 0) (x : K) : a * x * x + b * x + c = 0 ↔ x = -b / (2 * a) := by have : discrim a b c = 0 * 0 := by rw [h, mul_zero] rw [quadratic_eq_zero_iff ha this, add_zero, sub_zero, or_self_iff] end Field section LinearOrderedField variable {K : Type*} [LinearOrderedField K] {a b c : K} /-- If a polynomial of degree 2 is always nonnegative, then its discriminant is nonpositive -/ theorem discrim_le_zero (h : ∀ x : K, 0 ≤ a * x * x + b * x + c) : discrim a b c ≤ 0 := by rw [discrim, sq] obtain ha | rfl | ha : a < 0 ∨ a = 0 ∨ 0 < a := lt_trichotomy a 0 -- if a < 0 · have : Tendsto (fun x => (a * x + b) * x + c) atTop atBot := tendsto_atBot_add_const_right _ c ((tendsto_atBot_add_const_right _ b (tendsto_id.const_mul_atTop_of_neg ha)).atBot_mul_atTop tendsto_id) rcases (this.eventually (eventually_lt_atBot 0)).exists with ⟨x, hx⟩ exact False.elim ((h x).not_lt <| by rwa [← add_mul]) -- if a = 0 · rcases eq_or_ne b 0 with (rfl | hb) · simp · have := h ((-c - 1) / b) rw [mul_div_cancel₀ _ hb] at this linarith -- if a > 0 · have ha' : 0 ≤ 4 * a := mul_nonneg zero_le_four ha.le convert neg_nonpos.2 (mul_nonneg ha' (h (-b / (2 * a)))) using 1 field_simp ring lemma discrim_le_zero_of_nonpos (h : ∀ x : K, a * x * x + b * x + c ≤ 0) : discrim a b c ≤ 0 := discrim_neg a b c ▸ discrim_le_zero <| by simpa only [neg_mul, ← neg_add, neg_nonneg] /-- If a polynomial of degree 2 is always positive, then its discriminant is negative, at least when the coefficient of the quadratic term is nonzero. -/ theorem discrim_lt_zero (ha : a ≠ 0) (h : ∀ x : K, 0 < a * x * x + b * x + c) : discrim a b c < 0 := by have : ∀ x : K, 0 ≤ a * x * x + b * x + c := fun x => le_of_lt (h x) refine lt_of_le_of_ne (discrim_le_zero this) fun h' ↦ ?_ have := h (-b / (2 * a)) have : a * (-b / (2 * a)) * (-b / (2 * a)) + b * (-b / (2 * a)) + c = 0 := by rw [quadratic_eq_zero_iff_of_discrim_eq_zero ha h' (-b / (2 * a))] linarith lemma discrim_lt_zero_of_neg (ha : a ≠ 0) (h : ∀ x : K, a * x * x + b * x + c < 0) : discrim a b c < 0 := discrim_neg a b c ▸ discrim_lt_zero (neg_ne_zero.2 ha) <| by simpa only [neg_mul, ← neg_add, neg_pos] end LinearOrderedField
Algebra\Quandle.lean
/- Copyright (c) 2020 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import Mathlib.Algebra.Group.Equiv.Basic import Mathlib.Algebra.Group.Aut import Mathlib.Data.ZMod.Defs import Mathlib.Tactic.Ring /-! # Racks and Quandles This file defines racks and quandles, algebraic structures for sets that bijectively act on themselves with a self-distributivity property. If `R` is a rack and `act : R → (R ≃ R)` is the self-action, then the self-distributivity is, equivalently, that ``` act (act x y) = act x * act y * (act x)⁻¹ ``` where multiplication is composition in `R ≃ R` as a group. Quandles are racks such that `act x x = x` for all `x`. One example of a quandle (not yet in mathlib) is the action of a Lie algebra on itself, defined by `act x y = Ad (exp x) y`. Quandles and racks were independently developed by multiple mathematicians. David Joyce introduced quandles in his thesis [Joyce1982] to define an algebraic invariant of knot and link complements that is analogous to the fundamental group of the exterior, and he showed that the quandle associated to an oriented knot is invariant up to orientation-reversed mirror image. Racks were used by Fenn and Rourke for framed codimension-2 knots and links in [FennRourke1992]. Unital shelves are discussed in [crans2017]. The name "rack" came from wordplay by Conway and Wraith for the "wrack and ruin" of forgetting everything but the conjugation operation for a group. ## Main definitions * `Shelf` is a type with a self-distributive action * `UnitalShelf` is a shelf with a left and right unit * `Rack` is a shelf whose action for each element is invertible * `Quandle` is a rack whose action for an element fixes that element * `Quandle.conj` defines a quandle of a group acting on itself by conjugation. * `ShelfHom` is homomorphisms of shelves, racks, and quandles. * `Rack.EnvelGroup` gives the universal group the rack maps to as a conjugation quandle. * `Rack.oppositeRack` gives the rack with the action replaced by its inverse. ## Main statements * `Rack.EnvelGroup` is left adjoint to `Quandle.Conj` (`toEnvelGroup.map`). The universality statements are `toEnvelGroup.univ` and `toEnvelGroup.univ_uniq`. ## Implementation notes "Unital racks" are uninteresting (see `Rack.assoc_iff_id`, `UnitalShelf.assoc`), so we do not define them. ## Notation The following notation is localized in `quandles`: * `x ◃ y` is `Shelf.act x y` * `x ◃⁻¹ y` is `Rack.inv_act x y` * `S →◃ S'` is `ShelfHom S S'` Use `open quandles` to use these. ## TODO * If `g` is the Lie algebra of a Lie group `G`, then `(x ◃ y) = Ad (exp x) x` forms a quandle. * If `X` is a symmetric space, then each point has a corresponding involution that acts on `X`, forming a quandle. * Alexander quandle with `a ◃ b = t * b + (1 - t) * b`, with `a` and `b` elements of a module over `Z[t,t⁻¹]`. * If `G` is a group, `H` a subgroup, and `z` in `H`, then there is a quandle `(G/H;z)` defined by `yH ◃ xH = yzy⁻¹xH`. Every homogeneous quandle (i.e., a quandle `Q` whose automorphism group acts transitively on `Q` as a set) is isomorphic to such a quandle. There is a generalization to this arbitrary quandles in [Joyce's paper (Theorem 7.2)][Joyce1982]. ## Tags rack, quandle -/ open MulOpposite universe u v /-- A *Shelf* is a structure with a self-distributive binary operation. The binary operation is regarded as a left action of the type on itself. -/ class Shelf (α : Type u) where /-- The action of the `Shelf` over `α`-/ act : α → α → α /-- A verification that `act` is self-distributive-/ self_distrib : ∀ {x y z : α}, act x (act y z) = act (act x y) (act x z) /-- A *unital shelf* is a shelf equipped with an element `1` such that, for all elements `x`, we have both `x ◃ 1` and `1 ◃ x` equal `x`. -/ class UnitalShelf (α : Type u) extends Shelf α, One α := (one_act : ∀ a : α, act 1 a = a) (act_one : ∀ a : α, act a 1 = a) /-- The type of homomorphisms between shelves. This is also the notion of rack and quandle homomorphisms. -/ @[ext] structure ShelfHom (S₁ : Type*) (S₂ : Type*) [Shelf S₁] [Shelf S₂] where /-- The function under the Shelf Homomorphism -/ toFun : S₁ → S₂ /-- The homomorphism property of a Shelf Homomorphism-/ map_act' : ∀ {x y : S₁}, toFun (Shelf.act x y) = Shelf.act (toFun x) (toFun y) /-- A *rack* is an automorphic set (a set with an action on itself by bijections) that is self-distributive. It is a shelf such that each element's action is invertible. The notations `x ◃ y` and `x ◃⁻¹ y` denote the action and the inverse action, respectively, and they are right associative. -/ class Rack (α : Type u) extends Shelf α where /-- The inverse actions of the elements -/ invAct : α → α → α /-- Proof of left inverse -/ left_inv : ∀ x, Function.LeftInverse (invAct x) (act x) /-- Proof of right inverse -/ right_inv : ∀ x, Function.RightInverse (invAct x) (act x) /-- Action of a Shelf-/ scoped[Quandles] infixr:65 " ◃ " => Shelf.act /-- Inverse Action of a Rack-/ scoped[Quandles] infixr:65 " ◃⁻¹ " => Rack.invAct /-- Shelf Homomorphism-/ scoped[Quandles] infixr:25 " →◃ " => ShelfHom open Quandles namespace UnitalShelf open Shelf variable {S : Type*} [UnitalShelf S] /-- A monoid is *graphic* if, for all `x` and `y`, the *graphic identity* `(x * y) * x = x * y` holds. For a unital shelf, this graphic identity holds. -/ lemma act_act_self_eq (x y : S) : (x ◃ y) ◃ x = x ◃ y := by have h : (x ◃ y) ◃ x = (x ◃ y) ◃ (x ◃ 1) := by rw [act_one] rw [h, ← Shelf.self_distrib, act_one] lemma act_idem (x : S) : (x ◃ x) = x := by rw [← act_one x, ← Shelf.self_distrib, act_one] lemma act_self_act_eq (x y : S) : x ◃ (x ◃ y) = x ◃ y := by have h : x ◃ (x ◃ y) = (x ◃ 1) ◃ (x ◃ y) := by rw [act_one] rw [h, ← Shelf.self_distrib, one_act] /-- The associativity of a unital shelf comes for free. -/ lemma assoc (x y z : S) : (x ◃ y) ◃ z = x ◃ y ◃ z := by rw [self_distrib, self_distrib, act_act_self_eq, act_self_act_eq] end UnitalShelf namespace Rack variable {R : Type*} [Rack R] -- Porting note: No longer a need for `Rack.self_distrib` export Shelf (self_distrib) -- porting note, changed name to `act'` to not conflict with `Shelf.act` /-- A rack acts on itself by equivalences. -/ def act' (x : R) : R ≃ R where toFun := Shelf.act x invFun := invAct x left_inv := left_inv x right_inv := right_inv x @[simp] theorem act'_apply (x y : R) : act' x y = x ◃ y := rfl @[simp] theorem act'_symm_apply (x y : R) : (act' x).symm y = x ◃⁻¹ y := rfl @[simp] theorem invAct_apply (x y : R) : (act' x)⁻¹ y = x ◃⁻¹ y := rfl @[simp] theorem invAct_act_eq (x y : R) : x ◃⁻¹ x ◃ y = y := left_inv x y @[simp] theorem act_invAct_eq (x y : R) : x ◃ x ◃⁻¹ y = y := right_inv x y theorem left_cancel (x : R) {y y' : R} : x ◃ y = x ◃ y' ↔ y = y' := by constructor · apply (act' x).injective rintro rfl rfl theorem left_cancel_inv (x : R) {y y' : R} : x ◃⁻¹ y = x ◃⁻¹ y' ↔ y = y' := by constructor · apply (act' x).symm.injective rintro rfl rfl theorem self_distrib_inv {x y z : R} : x ◃⁻¹ y ◃⁻¹ z = (x ◃⁻¹ y) ◃⁻¹ x ◃⁻¹ z := by rw [← left_cancel (x ◃⁻¹ y), right_inv, ← left_cancel x, right_inv, self_distrib] repeat' rw [right_inv] /-- The *adjoint action* of a rack on itself is `op'`, and the adjoint action of `x ◃ y` is the conjugate of the action of `y` by the action of `x`. It is another way to understand the self-distributivity axiom. This is used in the natural rack homomorphism `toConj` from `R` to `Conj (R ≃ R)` defined by `op'`. -/ theorem ad_conj {R : Type*} [Rack R] (x y : R) : act' (x ◃ y) = act' x * act' y * (act' x)⁻¹ := by rw [eq_mul_inv_iff_mul_eq]; ext z apply self_distrib.symm /-- The opposite rack, swapping the roles of `◃` and `◃⁻¹`. -/ instance oppositeRack : Rack Rᵐᵒᵖ where act x y := op (invAct (unop x) (unop y)) self_distrib := by intro x y z induction x induction y induction z simp only [op_inj, unop_op, op_unop] rw [self_distrib_inv] invAct x y := op (Shelf.act (unop x) (unop y)) left_inv := MulOpposite.rec' fun x => MulOpposite.rec' fun y => by simp right_inv := MulOpposite.rec' fun x => MulOpposite.rec' fun y => by simp @[simp] theorem op_act_op_eq {x y : R} : op x ◃ op y = op (x ◃⁻¹ y) := rfl @[simp] theorem op_invAct_op_eq {x y : R} : op x ◃⁻¹ op y = op (x ◃ y) := rfl @[simp] theorem self_act_act_eq {x y : R} : (x ◃ x) ◃ y = x ◃ y := by rw [← right_inv x y, ← self_distrib] @[simp] theorem self_invAct_invAct_eq {x y : R} : (x ◃⁻¹ x) ◃⁻¹ y = x ◃⁻¹ y := by have h := @self_act_act_eq _ _ (op x) (op y) simpa using h @[simp] theorem self_act_invAct_eq {x y : R} : (x ◃ x) ◃⁻¹ y = x ◃⁻¹ y := by rw [← left_cancel (x ◃ x)] rw [right_inv] rw [self_act_act_eq] rw [right_inv] @[simp] theorem self_invAct_act_eq {x y : R} : (x ◃⁻¹ x) ◃ y = x ◃ y := by have h := @self_act_invAct_eq _ _ (op x) (op y) simpa using h theorem self_act_eq_iff_eq {x y : R} : x ◃ x = y ◃ y ↔ x = y := by constructor; swap · rintro rfl; rfl intro h trans (x ◃ x) ◃⁻¹ x ◃ x · rw [← left_cancel (x ◃ x), right_inv, self_act_act_eq] · rw [h, ← left_cancel (y ◃ y), right_inv, self_act_act_eq] theorem self_invAct_eq_iff_eq {x y : R} : x ◃⁻¹ x = y ◃⁻¹ y ↔ x = y := by have h := @self_act_eq_iff_eq _ _ (op x) (op y) simpa using h /-- The map `x ↦ x ◃ x` is a bijection. (This has applications for the regular isotopy version of the Reidemeister I move for knot diagrams.) -/ def selfApplyEquiv (R : Type*) [Rack R] : R ≃ R where toFun x := x ◃ x invFun x := x ◃⁻¹ x left_inv x := by simp right_inv x := by simp /-- An involutory rack is one for which `Rack.oppositeRack R x` is an involution for every x. -/ def IsInvolutory (R : Type*) [Rack R] : Prop := ∀ x : R, Function.Involutive (Shelf.act x) theorem involutory_invAct_eq_act {R : Type*} [Rack R] (h : IsInvolutory R) (x y : R) : x ◃⁻¹ y = x ◃ y := by rw [← left_cancel x, right_inv, h x] /-- An abelian rack is one for which the mediality axiom holds. -/ def IsAbelian (R : Type*) [Rack R] : Prop := ∀ x y z w : R, (x ◃ y) ◃ z ◃ w = (x ◃ z) ◃ y ◃ w /-- Associative racks are uninteresting. -/ theorem assoc_iff_id {R : Type*} [Rack R] {x y z : R} : x ◃ y ◃ z = (x ◃ y) ◃ z ↔ x ◃ z = z := by rw [self_distrib] rw [left_cancel] end Rack namespace ShelfHom variable {S₁ : Type*} {S₂ : Type*} {S₃ : Type*} [Shelf S₁] [Shelf S₂] [Shelf S₃] instance : FunLike (S₁ →◃ S₂) S₁ S₂ where coe := toFun coe_injective' | ⟨_, _⟩, ⟨_, _⟩, rfl => rfl @[simp] theorem toFun_eq_coe (f : S₁ →◃ S₂) : f.toFun = f := rfl @[simp] theorem map_act (f : S₁ →◃ S₂) {x y : S₁} : f (x ◃ y) = f x ◃ f y := map_act' f /-- The identity homomorphism -/ def id (S : Type*) [Shelf S] : S →◃ S where toFun := fun x => x map_act' := by simp instance inhabited (S : Type*) [Shelf S] : Inhabited (S →◃ S) := ⟨id S⟩ /-- The composition of shelf homomorphisms -/ def comp (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) : S₁ →◃ S₃ where toFun := g.toFun ∘ f.toFun map_act' := by simp @[simp] theorem comp_apply (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) (x : S₁) : (g.comp f) x = g (f x) := rfl end ShelfHom /-- A quandle is a rack such that each automorphism fixes its corresponding element. -/ class Quandle (α : Type*) extends Rack α where /-- The fixing property of a Quandle -/ fix : ∀ {x : α}, act x x = x namespace Quandle open Rack variable {Q : Type*} [Quandle Q] attribute [simp] fix @[simp] theorem fix_inv {x : Q} : x ◃⁻¹ x = x := by rw [← left_cancel x] simp instance oppositeQuandle : Quandle Qᵐᵒᵖ where fix := by intro x induction' x simp /-- The conjugation quandle of a group. Each element of the group acts by the corresponding inner automorphism. -/ -- Porting note: no need for `nolint` and added `reducible` abbrev Conj (G : Type*) := G instance Conj.quandle (G : Type*) [Group G] : Quandle (Conj G) where act x := @MulAut.conj G _ x self_distrib := by intro x y z dsimp only [MulAut.conj_apply] simp [mul_assoc] invAct x := (@MulAut.conj G _ x).symm left_inv x y := by simp [act', mul_assoc] right_inv x y := by simp [act', mul_assoc] fix := by simp @[simp] theorem conj_act_eq_conj {G : Type*} [Group G] (x y : Conj G) : x ◃ y = ((x : G) * (y : G) * (x : G)⁻¹ : G) := rfl theorem conj_swap {G : Type*} [Group G] (x y : Conj G) : x ◃ y = y ↔ y ◃ x = x := by dsimp [Conj] at *; constructor repeat' intro h; conv_rhs => rw [eq_mul_inv_of_mul_eq (eq_mul_inv_of_mul_eq h)]; simp /-- `Conj` is functorial -/ def Conj.map {G : Type*} {H : Type*} [Group G] [Group H] (f : G →* H) : Conj G →◃ Conj H where toFun := f map_act' := by simp -- Porting note: I don't think HasLift exists -- instance {G : Type*} {H : Type*} [Group G] [Group H] : HasLift (G →* H) (Conj G →◃ Conj H) -- where lift := Conj.map /-- The dihedral quandle. This is the conjugation quandle of the dihedral group restrict to flips. Used for Fox n-colorings of knots. -/ -- Porting note: Removed nolint def Dihedral (n : ℕ) := ZMod n /-- The operation for the dihedral quandle. It does not need to be an equivalence because it is an involution (see `dihedralAct.inv`). -/ def dihedralAct (n : ℕ) (a : ZMod n) : ZMod n → ZMod n := fun b => 2 * a - b theorem dihedralAct.inv (n : ℕ) (a : ZMod n) : Function.Involutive (dihedralAct n a) := by intro b dsimp only [dihedralAct] simp instance (n : ℕ) : Quandle (Dihedral n) where act := dihedralAct n self_distrib := by intro x y z simp only [dihedralAct] ring_nf invAct := dihedralAct n left_inv x := (dihedralAct.inv n x).leftInverse right_inv x := (dihedralAct.inv n x).rightInverse fix := by intro x simp only [dihedralAct] ring_nf end Quandle namespace Rack /-- This is the natural rack homomorphism to the conjugation quandle of the group `R ≃ R` that acts on the rack. -/ def toConj (R : Type*) [Rack R] : R →◃ Quandle.Conj (R ≃ R) where toFun := act' map_act' := by intro x y exact ad_conj x y section EnvelGroup /-! ### Universal enveloping group of a rack The universal enveloping group `EnvelGroup R` of a rack `R` is the universal group such that every rack homomorphism `R →◃ conj G` is induced by a unique group homomorphism `EnvelGroup R →* G`. For quandles, Joyce called this group `AdConj R`. The `EnvelGroup` functor is left adjoint to the `Conj` forgetful functor, and the way we construct the enveloping group is via a technique that should work for left adjoints of forgetful functors in general. It involves thinking a little about 2-categories, but the payoff is that the map `EnvelGroup R →* G` has a nice description. Let's think of a group as being a one-object category. The first step is to define `PreEnvelGroup`, which gives formal expressions for all the 1-morphisms and includes the unit element, elements of `R`, multiplication, and inverses. To introduce relations, the second step is to define `PreEnvelGroupRel'`, which gives formal expressions for all 2-morphisms between the 1-morphisms. The 2-morphisms include associativity, multiplication by the unit, multiplication by inverses, compatibility with multiplication and inverses (`congr_mul` and `congr_inv`), the axioms for an equivalence relation, and, importantly, the relationship between conjugation and the rack action (see `Rack.ad_conj`). None of this forms a 2-category yet, for example due to lack of associativity of `trans`. The `PreEnvelGroupRel` relation is a `Prop`-valued version of `PreEnvelGroupRel'`, and making it `Prop`-valued essentially introduces enough 3-isomorphisms so that every pair of compatible 2-morphisms is isomorphic. Now, while composition in `PreEnvelGroup` does not strictly satisfy the category axioms, `PreEnvelGroup` and `PreEnvelGroupRel'` do form a weak 2-category. Since we just want a 1-category, the last step is to quotient `PreEnvelGroup` by `PreEnvelGroupRel'`, and the result is the group `EnvelGroup`. For a homomorphism `f : R →◃ Conj G`, how does `EnvelGroup.map f : EnvelGroup R →* G` work? Let's think of `G` as being a 2-category with one object, a 1-morphism per element of `G`, and a single 2-morphism called `Eq.refl` for each 1-morphism. We define the map using a "higher `Quotient.lift`" -- not only do we evaluate elements of `PreEnvelGroup` as expressions in `G` (this is `toEnvelGroup.mapAux`), but we evaluate elements of `PreEnvelGroup'` as expressions of 2-morphisms of `G` (this is `toEnvelGroup.mapAux.well_def`). That is to say, `toEnvelGroup.mapAux.well_def` recursively evaluates formal expressions of 2-morphisms as equality proofs in `G`. Now that all morphisms are accounted for, the map descends to a homomorphism `EnvelGroup R →* G`. Note: `Type`-valued relations are not common. The fact it is `Type`-valued is what makes `toEnvelGroup.mapAux.well_def` have well-founded recursion. -/ /-- Free generators of the enveloping group. -/ inductive PreEnvelGroup (R : Type u) : Type u | unit : PreEnvelGroup R | incl (x : R) : PreEnvelGroup R | mul (a b : PreEnvelGroup R) : PreEnvelGroup R | inv (a : PreEnvelGroup R) : PreEnvelGroup R instance PreEnvelGroup.inhabited (R : Type u) : Inhabited (PreEnvelGroup R) := ⟨PreEnvelGroup.unit⟩ open PreEnvelGroup /-- Relations for the enveloping group. This is a type-valued relation because `toEnvelGroup.mapAux.well_def` inducts on it to show `toEnvelGroup.map` is well-defined. The relation `PreEnvelGroupRel` is the `Prop`-valued version, which is used to define `EnvelGroup` itself. -/ inductive PreEnvelGroupRel' (R : Type u) [Rack R] : PreEnvelGroup R → PreEnvelGroup R → Type u | refl {a : PreEnvelGroup R} : PreEnvelGroupRel' R a a | symm {a b : PreEnvelGroup R} (hab : PreEnvelGroupRel' R a b) : PreEnvelGroupRel' R b a | trans {a b c : PreEnvelGroup R} (hab : PreEnvelGroupRel' R a b) (hbc : PreEnvelGroupRel' R b c) : PreEnvelGroupRel' R a c | congr_mul {a b a' b' : PreEnvelGroup R} (ha : PreEnvelGroupRel' R a a') (hb : PreEnvelGroupRel' R b b') : PreEnvelGroupRel' R (mul a b) (mul a' b') | congr_inv {a a' : PreEnvelGroup R} (ha : PreEnvelGroupRel' R a a') : PreEnvelGroupRel' R (inv a) (inv a') | assoc (a b c : PreEnvelGroup R) : PreEnvelGroupRel' R (mul (mul a b) c) (mul a (mul b c)) | one_mul (a : PreEnvelGroup R) : PreEnvelGroupRel' R (mul unit a) a | mul_one (a : PreEnvelGroup R) : PreEnvelGroupRel' R (mul a unit) a | mul_left_inv (a : PreEnvelGroup R) : PreEnvelGroupRel' R (mul (inv a) a) unit | act_incl (x y : R) : PreEnvelGroupRel' R (mul (mul (incl x) (incl y)) (inv (incl x))) (incl (x ◃ y)) instance PreEnvelGroupRel'.inhabited (R : Type u) [Rack R] : Inhabited (PreEnvelGroupRel' R unit unit) := ⟨PreEnvelGroupRel'.refl⟩ /-- The `PreEnvelGroupRel` relation as a `Prop`. Used as the relation for `PreEnvelGroup.setoid`. -/ inductive PreEnvelGroupRel (R : Type u) [Rack R] : PreEnvelGroup R → PreEnvelGroup R → Prop | rel {a b : PreEnvelGroup R} (r : PreEnvelGroupRel' R a b) : PreEnvelGroupRel R a b /-- A quick way to convert a `PreEnvelGroupRel'` to a `PreEnvelGroupRel`. -/ theorem PreEnvelGroupRel'.rel {R : Type u} [Rack R] {a b : PreEnvelGroup R} : PreEnvelGroupRel' R a b → PreEnvelGroupRel R a b := PreEnvelGroupRel.rel @[refl] theorem PreEnvelGroupRel.refl {R : Type u} [Rack R] {a : PreEnvelGroup R} : PreEnvelGroupRel R a a := PreEnvelGroupRel.rel PreEnvelGroupRel'.refl @[symm] theorem PreEnvelGroupRel.symm {R : Type u} [Rack R] {a b : PreEnvelGroup R} : PreEnvelGroupRel R a b → PreEnvelGroupRel R b a | ⟨r⟩ => r.symm.rel @[trans] theorem PreEnvelGroupRel.trans {R : Type u} [Rack R] {a b c : PreEnvelGroup R} : PreEnvelGroupRel R a b → PreEnvelGroupRel R b c → PreEnvelGroupRel R a c | ⟨rab⟩, ⟨rbc⟩ => (rab.trans rbc).rel instance PreEnvelGroup.setoid (R : Type*) [Rack R] : Setoid (PreEnvelGroup R) where r := PreEnvelGroupRel R iseqv := by constructor · apply PreEnvelGroupRel.refl · apply PreEnvelGroupRel.symm · apply PreEnvelGroupRel.trans /-- The universal enveloping group for the rack R. -/ def EnvelGroup (R : Type*) [Rack R] := Quotient (PreEnvelGroup.setoid R) -- Define the `Group` instances in two steps so `inv` can be inferred correctly. -- TODO: is there a non-invasive way of defining the instance directly? instance (R : Type*) [Rack R] : DivInvMonoid (EnvelGroup R) where mul a b := Quotient.liftOn₂ a b (fun a b => ⟦PreEnvelGroup.mul a b⟧) fun a b a' b' ⟨ha⟩ ⟨hb⟩ => Quotient.sound (PreEnvelGroupRel'.congr_mul ha hb).rel one := ⟦unit⟧ inv a := Quotient.liftOn a (fun a => ⟦PreEnvelGroup.inv a⟧) fun a a' ⟨ha⟩ => Quotient.sound (PreEnvelGroupRel'.congr_inv ha).rel mul_assoc a b c := Quotient.inductionOn₃ a b c fun a b c => Quotient.sound (PreEnvelGroupRel'.assoc a b c).rel one_mul a := Quotient.inductionOn a fun a => Quotient.sound (PreEnvelGroupRel'.one_mul a).rel mul_one a := Quotient.inductionOn a fun a => Quotient.sound (PreEnvelGroupRel'.mul_one a).rel instance (R : Type*) [Rack R] : Group (EnvelGroup R) := { mul_left_inv := fun a => Quotient.inductionOn a fun a => Quotient.sound (PreEnvelGroupRel'.mul_left_inv a).rel } instance EnvelGroup.inhabited (R : Type*) [Rack R] : Inhabited (EnvelGroup R) := ⟨1⟩ /-- The canonical homomorphism from a rack to its enveloping group. Satisfies universal properties given by `toEnvelGroup.map` and `toEnvelGroup.univ`. -/ def toEnvelGroup (R : Type*) [Rack R] : R →◃ Quandle.Conj (EnvelGroup R) where toFun x := ⟦incl x⟧ map_act' := @fun x y => Quotient.sound (PreEnvelGroupRel'.act_incl x y).symm.rel /-- The preliminary definition of the induced map from the enveloping group. See `toEnvelGroup.map`. -/ def toEnvelGroup.mapAux {R : Type*} [Rack R] {G : Type*} [Group G] (f : R →◃ Quandle.Conj G) : PreEnvelGroup R → G | .unit => 1 | .incl x => f x | .mul a b => toEnvelGroup.mapAux f a * toEnvelGroup.mapAux f b | .inv a => (toEnvelGroup.mapAux f a)⁻¹ namespace toEnvelGroup.mapAux open PreEnvelGroupRel' /-- Show that `toEnvelGroup.mapAux` sends equivalent expressions to equal terms. -/ theorem well_def {R : Type*} [Rack R] {G : Type*} [Group G] (f : R →◃ Quandle.Conj G) : ∀ {a b : PreEnvelGroup R}, PreEnvelGroupRel' R a b → toEnvelGroup.mapAux f a = toEnvelGroup.mapAux f b | a, _, PreEnvelGroupRel'.refl => rfl | a, b, PreEnvelGroupRel'.symm h => (well_def f h).symm | a, b, PreEnvelGroupRel'.trans hac hcb => Eq.trans (well_def f hac) (well_def f hcb) | _, _, PreEnvelGroupRel'.congr_mul ha hb => by simp [toEnvelGroup.mapAux, well_def f ha, well_def f hb] | _, _, congr_inv ha => by simp [toEnvelGroup.mapAux, well_def f ha] | _, _, assoc a b c => by apply mul_assoc | _, _, PreEnvelGroupRel'.one_mul a => by simp [toEnvelGroup.mapAux] | _, _, PreEnvelGroupRel'.mul_one a => by simp [toEnvelGroup.mapAux] | _, _, PreEnvelGroupRel'.mul_left_inv a => by simp [toEnvelGroup.mapAux] | _, _, act_incl x y => by simp [toEnvelGroup.mapAux] end toEnvelGroup.mapAux /-- Given a map from a rack to a group, lift it to being a map from the enveloping group. More precisely, the `EnvelGroup` functor is left adjoint to `Quandle.Conj`. -/ def toEnvelGroup.map {R : Type*} [Rack R] {G : Type*} [Group G] : (R →◃ Quandle.Conj G) ≃ (EnvelGroup R →* G) where toFun f := { toFun := fun x => Quotient.liftOn x (toEnvelGroup.mapAux f) fun a b ⟨hab⟩ => toEnvelGroup.mapAux.well_def f hab map_one' := by change Quotient.liftOn ⟦Rack.PreEnvelGroup.unit⟧ (toEnvelGroup.mapAux f) _ = 1 simp only [Quotient.lift_mk, mapAux] map_mul' := fun x y => Quotient.inductionOn₂ x y fun x y => by simp only [toEnvelGroup.mapAux] change Quotient.liftOn ⟦mul x y⟧ (toEnvelGroup.mapAux f) _ = _ simp [toEnvelGroup.mapAux] } invFun F := (Quandle.Conj.map F).comp (toEnvelGroup R) left_inv f := by ext; rfl right_inv F := MonoidHom.ext fun x => Quotient.inductionOn x fun x => by induction' x with _ x y ih_x ih_y x ih_x · exact F.map_one.symm · rfl · have hm : ⟦x.mul y⟧ = @Mul.mul (EnvelGroup R) _ ⟦x⟧ ⟦y⟧ := rfl simp only [MonoidHom.coe_mk, OneHom.coe_mk, Quotient.lift_mk] suffices ∀ x y, F (Mul.mul x y) = F (x) * F (y) by simp_all only [MonoidHom.coe_mk, OneHom.coe_mk, Quotient.lift_mk, hm] rw [← ih_x, ← ih_y, mapAux] exact F.map_mul · have hm : ⟦x.inv⟧ = @Inv.inv (EnvelGroup R) _ ⟦x⟧ := rfl rw [hm, F.map_inv, MonoidHom.map_inv, ih_x] /-- Given a homomorphism from a rack to a group, it factors through the enveloping group. -/ theorem toEnvelGroup.univ (R : Type*) [Rack R] (G : Type*) [Group G] (f : R →◃ Quandle.Conj G) : (Quandle.Conj.map (toEnvelGroup.map f)).comp (toEnvelGroup R) = f := toEnvelGroup.map.symm_apply_apply f /-- The homomorphism `toEnvelGroup.map f` is the unique map that fits into the commutative triangle in `toEnvelGroup.univ`. -/ theorem toEnvelGroup.univ_uniq (R : Type*) [Rack R] (G : Type*) [Group G] (f : R →◃ Quandle.Conj G) (g : EnvelGroup R →* G) (h : f = (Quandle.Conj.map g).comp (toEnvelGroup R)) : g = toEnvelGroup.map f := h.symm ▸ (toEnvelGroup.map.apply_symm_apply g).symm /-- The induced group homomorphism from the enveloping group into bijections of the rack, using `Rack.toConj`. Satisfies the property `envelAction_prop`. This gives the rack `R` the structure of an augmented rack over `EnvelGroup R`. -/ def envelAction {R : Type*} [Rack R] : EnvelGroup R →* R ≃ R := toEnvelGroup.map (toConj R) @[simp] theorem envelAction_prop {R : Type*} [Rack R] (x y : R) : envelAction (toEnvelGroup R x) y = x ◃ y := rfl end EnvelGroup end Rack
Algebra\Quaternion.lean
/- Copyright (c) 2020 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Equiv import Mathlib.LinearAlgebra.Dimension.StrongRankCondition import Mathlib.LinearAlgebra.FreeModule.Basic import Mathlib.LinearAlgebra.FreeModule.Finite.Basic import Mathlib.SetTheory.Cardinal.Ordinal /-! # Quaternions In this file we define quaternions `ℍ[R]` over a commutative ring `R`, and define some algebraic structures on `ℍ[R]`. ## Main definitions * `QuaternionAlgebra R a b`, `ℍ[R, a, b]` : [quaternion algebra](https://en.wikipedia.org/wiki/Quaternion_algebra) with coefficients `a`, `b` * `Quaternion R`, `ℍ[R]` : the space of quaternions, a.k.a. `QuaternionAlgebra R (-1) (-1)`; * `Quaternion.normSq` : square of the norm of a quaternion; We also define the following algebraic structures on `ℍ[R]`: * `Ring ℍ[R, a, b]`, `StarRing ℍ[R, a, b]`, and `Algebra R ℍ[R, a, b]` : for any commutative ring `R`; * `Ring ℍ[R]`, `StarRing ℍ[R]`, and `Algebra R ℍ[R]` : for any commutative ring `R`; * `IsDomain ℍ[R]` : for a linear ordered commutative ring `R`; * `DivisionRing ℍ[R]` : for a linear ordered field `R`. ## Notation The following notation is available with `open Quaternion` or `open scoped Quaternion`. * `ℍ[R, c₁, c₂]` : `QuaternionAlgebra R c₁ c₂` * `ℍ[R]` : quaternions over `R`. ## Implementation notes We define quaternions over any ring `R`, not just `ℝ` to be able to deal with, e.g., integer or rational quaternions without using real numbers. In particular, all definitions in this file are computable. ## Tags quaternion -/ /-- Quaternion algebra over a type with fixed coefficients $a=i^2$ and $b=j^2$. Implemented as a structure with four fields: `re`, `imI`, `imJ`, and `imK`. -/ @[ext] structure QuaternionAlgebra (R : Type*) (a b : R) where /-- Real part of a quaternion. -/ re : R imI : R imJ : R imK : R @[inherit_doc] scoped[Quaternion] notation "ℍ[" R "," a "," b "]" => QuaternionAlgebra R a b open Quaternion namespace QuaternionAlgebra /-- The equivalence between a quaternion algebra over `R` and `R × R × R × R`. -/ @[simps] def equivProd {R : Type*} (c₁ c₂ : R) : ℍ[R,c₁,c₂] ≃ R × R × R × R where toFun a := ⟨a.1, a.2, a.3, a.4⟩ invFun a := ⟨a.1, a.2.1, a.2.2.1, a.2.2.2⟩ left_inv _ := rfl right_inv _ := rfl /-- The equivalence between a quaternion algebra over `R` and `Fin 4 → R`. -/ @[simps symm_apply] def equivTuple {R : Type*} (c₁ c₂ : R) : ℍ[R,c₁,c₂] ≃ (Fin 4 → R) where toFun a := ![a.1, a.2, a.3, a.4] invFun a := ⟨a 0, a 1, a 2, a 3⟩ left_inv _ := rfl right_inv f := by ext ⟨_, _ | _ | _ | _ | _ | ⟨⟩⟩ <;> rfl @[simp] theorem equivTuple_apply {R : Type*} (c₁ c₂ : R) (x : ℍ[R,c₁,c₂]) : equivTuple c₁ c₂ x = ![x.re, x.imI, x.imJ, x.imK] := rfl @[simp] theorem mk.eta {R : Type*} {c₁ c₂} (a : ℍ[R,c₁,c₂]) : mk a.1 a.2 a.3 a.4 = a := rfl variable {S T R : Type*} {c₁ c₂ : R} (r x y z : R) (a b c : ℍ[R,c₁,c₂]) instance [Subsingleton R] : Subsingleton ℍ[R, c₁, c₂] := (equivTuple c₁ c₂).subsingleton instance [Nontrivial R] : Nontrivial ℍ[R, c₁, c₂] := (equivTuple c₁ c₂).surjective.nontrivial section Zero variable [Zero R] /-- The imaginary part of a quaternion. -/ def im (x : ℍ[R,c₁,c₂]) : ℍ[R,c₁,c₂] := ⟨0, x.imI, x.imJ, x.imK⟩ @[simp] theorem im_re : a.im.re = 0 := rfl @[simp] theorem im_imI : a.im.imI = a.imI := rfl @[simp] theorem im_imJ : a.im.imJ = a.imJ := rfl @[simp] theorem im_imK : a.im.imK = a.imK := rfl @[simp] theorem im_idem : a.im.im = a.im := rfl /-- Coercion `R → ℍ[R,c₁,c₂]`. -/ @[coe] def coe (x : R) : ℍ[R,c₁,c₂] := ⟨x, 0, 0, 0⟩ instance : CoeTC R ℍ[R,c₁,c₂] := ⟨coe⟩ @[simp, norm_cast] theorem coe_re : (x : ℍ[R,c₁,c₂]).re = x := rfl @[simp, norm_cast] theorem coe_imI : (x : ℍ[R,c₁,c₂]).imI = 0 := rfl @[simp, norm_cast] theorem coe_imJ : (x : ℍ[R,c₁,c₂]).imJ = 0 := rfl @[simp, norm_cast] theorem coe_imK : (x : ℍ[R,c₁,c₂]).imK = 0 := rfl theorem coe_injective : Function.Injective (coe : R → ℍ[R,c₁,c₂]) := fun _ _ h => congr_arg re h @[simp] theorem coe_inj {x y : R} : (x : ℍ[R,c₁,c₂]) = y ↔ x = y := coe_injective.eq_iff -- Porting note: removed `simps`, added simp lemmas manually instance : Zero ℍ[R,c₁,c₂] := ⟨⟨0, 0, 0, 0⟩⟩ @[simp] theorem zero_re : (0 : ℍ[R,c₁,c₂]).re = 0 := rfl @[simp] theorem zero_imI : (0 : ℍ[R,c₁,c₂]).imI = 0 := rfl @[simp] theorem zero_imJ : (0 : ℍ[R,c₁,c₂]).imJ = 0 := rfl @[simp] theorem zero_imK : (0 : ℍ[R,c₁,c₂]).imK = 0 := rfl @[simp] theorem zero_im : (0 : ℍ[R,c₁,c₂]).im = 0 := rfl @[simp, norm_cast] theorem coe_zero : ((0 : R) : ℍ[R,c₁,c₂]) = 0 := rfl instance : Inhabited ℍ[R,c₁,c₂] := ⟨0⟩ section One variable [One R] -- Porting note: removed `simps`, added simp lemmas manually instance : One ℍ[R,c₁,c₂] := ⟨⟨1, 0, 0, 0⟩⟩ @[simp] theorem one_re : (1 : ℍ[R,c₁,c₂]).re = 1 := rfl @[simp] theorem one_imI : (1 : ℍ[R,c₁,c₂]).imI = 0 := rfl @[simp] theorem one_imJ : (1 : ℍ[R,c₁,c₂]).imJ = 0 := rfl @[simp] theorem one_imK : (1 : ℍ[R,c₁,c₂]).imK = 0 := rfl @[simp] theorem one_im : (1 : ℍ[R,c₁,c₂]).im = 0 := rfl @[simp, norm_cast] theorem coe_one : ((1 : R) : ℍ[R,c₁,c₂]) = 1 := rfl end One end Zero section Add variable [Add R] -- Porting note: removed `simps`, added simp lemmas manually instance : Add ℍ[R,c₁,c₂] := ⟨fun a b => ⟨a.1 + b.1, a.2 + b.2, a.3 + b.3, a.4 + b.4⟩⟩ @[simp] theorem add_re : (a + b).re = a.re + b.re := rfl @[simp] theorem add_imI : (a + b).imI = a.imI + b.imI := rfl @[simp] theorem add_imJ : (a + b).imJ = a.imJ + b.imJ := rfl @[simp] theorem add_imK : (a + b).imK = a.imK + b.imK := rfl @[simp] theorem mk_add_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) : (mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂]) + mk b₁ b₂ b₃ b₄ = mk (a₁ + b₁) (a₂ + b₂) (a₃ + b₃) (a₄ + b₄) := rfl end Add section AddZeroClass variable [AddZeroClass R] @[simp] theorem add_im : (a + b).im = a.im + b.im := QuaternionAlgebra.ext (zero_add _).symm rfl rfl rfl @[simp, norm_cast] theorem coe_add : ((x + y : R) : ℍ[R,c₁,c₂]) = x + y := by ext <;> simp end AddZeroClass section Neg variable [Neg R] -- Porting note: removed `simps`, added simp lemmas manually instance : Neg ℍ[R,c₁,c₂] := ⟨fun a => ⟨-a.1, -a.2, -a.3, -a.4⟩⟩ @[simp] theorem neg_re : (-a).re = -a.re := rfl @[simp] theorem neg_imI : (-a).imI = -a.imI := rfl @[simp] theorem neg_imJ : (-a).imJ = -a.imJ := rfl @[simp] theorem neg_imK : (-a).imK = -a.imK := rfl @[simp] theorem neg_mk (a₁ a₂ a₃ a₄ : R) : -(mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂]) = ⟨-a₁, -a₂, -a₃, -a₄⟩ := rfl end Neg section AddGroup variable [AddGroup R] @[simp] theorem neg_im : (-a).im = -a.im := QuaternionAlgebra.ext neg_zero.symm rfl rfl rfl @[simp, norm_cast] theorem coe_neg : ((-x : R) : ℍ[R,c₁,c₂]) = -x := by ext <;> simp instance : Sub ℍ[R,c₁,c₂] := ⟨fun a b => ⟨a.1 - b.1, a.2 - b.2, a.3 - b.3, a.4 - b.4⟩⟩ @[simp] theorem sub_re : (a - b).re = a.re - b.re := rfl @[simp] theorem sub_imI : (a - b).imI = a.imI - b.imI := rfl @[simp] theorem sub_imJ : (a - b).imJ = a.imJ - b.imJ := rfl @[simp] theorem sub_imK : (a - b).imK = a.imK - b.imK := rfl @[simp] theorem sub_im : (a - b).im = a.im - b.im := QuaternionAlgebra.ext (sub_zero _).symm rfl rfl rfl @[simp] theorem mk_sub_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) : (mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂]) - mk b₁ b₂ b₃ b₄ = mk (a₁ - b₁) (a₂ - b₂) (a₃ - b₃) (a₄ - b₄) := rfl @[simp, norm_cast] theorem coe_im : (x : ℍ[R,c₁,c₂]).im = 0 := rfl @[simp] theorem re_add_im : ↑a.re + a.im = a := QuaternionAlgebra.ext (add_zero _) (zero_add _) (zero_add _) (zero_add _) @[simp] theorem sub_self_im : a - a.im = a.re := QuaternionAlgebra.ext (sub_zero _) (sub_self _) (sub_self _) (sub_self _) @[simp] theorem sub_self_re : a - a.re = a.im := QuaternionAlgebra.ext (sub_self _) (sub_zero _) (sub_zero _) (sub_zero _) end AddGroup section Ring variable [Ring R] /-- Multiplication is given by * `1 * x = x * 1 = x`; * `i * i = c₁`; * `j * j = c₂`; * `i * j = k`, `j * i = -k`; * `k * k = -c₁ * c₂`; * `i * k = c₁ * j`, `k * i = -c₁ * j`; * `j * k = -c₂ * i`, `k * j = c₂ * i`. -/ instance : Mul ℍ[R,c₁,c₂] := ⟨fun a b => ⟨a.1 * b.1 + c₁ * a.2 * b.2 + c₂ * a.3 * b.3 - c₁ * c₂ * a.4 * b.4, a.1 * b.2 + a.2 * b.1 - c₂ * a.3 * b.4 + c₂ * a.4 * b.3, a.1 * b.3 + c₁ * a.2 * b.4 + a.3 * b.1 - c₁ * a.4 * b.2, a.1 * b.4 + a.2 * b.3 - a.3 * b.2 + a.4 * b.1⟩⟩ @[simp] theorem mul_re : (a * b).re = a.1 * b.1 + c₁ * a.2 * b.2 + c₂ * a.3 * b.3 - c₁ * c₂ * a.4 * b.4 := rfl @[simp] theorem mul_imI : (a * b).imI = a.1 * b.2 + a.2 * b.1 - c₂ * a.3 * b.4 + c₂ * a.4 * b.3 := rfl @[simp] theorem mul_imJ : (a * b).imJ = a.1 * b.3 + c₁ * a.2 * b.4 + a.3 * b.1 - c₁ * a.4 * b.2 := rfl @[simp] theorem mul_imK : (a * b).imK = a.1 * b.4 + a.2 * b.3 - a.3 * b.2 + a.4 * b.1 := rfl @[simp] theorem mk_mul_mk (a₁ a₂ a₃ a₄ b₁ b₂ b₃ b₄ : R) : (mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂]) * mk b₁ b₂ b₃ b₄ = ⟨a₁ * b₁ + c₁ * a₂ * b₂ + c₂ * a₃ * b₃ - c₁ * c₂ * a₄ * b₄, a₁ * b₂ + a₂ * b₁ - c₂ * a₃ * b₄ + c₂ * a₄ * b₃, a₁ * b₃ + c₁ * a₂ * b₄ + a₃ * b₁ - c₁ * a₄ * b₂, a₁ * b₄ + a₂ * b₃ - a₃ * b₂ + a₄ * b₁⟩ := rfl end Ring section SMul variable [SMul S R] [SMul T R] (s : S) instance : SMul S ℍ[R,c₁,c₂] where smul s a := ⟨s • a.1, s • a.2, s • a.3, s • a.4⟩ instance [SMul S T] [IsScalarTower S T R] : IsScalarTower S T ℍ[R,c₁,c₂] where smul_assoc s t x := by ext <;> exact smul_assoc _ _ _ instance [SMulCommClass S T R] : SMulCommClass S T ℍ[R,c₁,c₂] where smul_comm s t x := by ext <;> exact smul_comm _ _ _ @[simp] theorem smul_re : (s • a).re = s • a.re := rfl @[simp] theorem smul_imI : (s • a).imI = s • a.imI := rfl @[simp] theorem smul_imJ : (s • a).imJ = s • a.imJ := rfl @[simp] theorem smul_imK : (s • a).imK = s • a.imK := rfl @[simp] theorem smul_im {S} [CommRing R] [SMulZeroClass S R] (s : S) : (s • a).im = s • a.im := QuaternionAlgebra.ext (smul_zero s).symm rfl rfl rfl @[simp] theorem smul_mk (re im_i im_j im_k : R) : s • (⟨re, im_i, im_j, im_k⟩ : ℍ[R,c₁,c₂]) = ⟨s • re, s • im_i, s • im_j, s • im_k⟩ := rfl end SMul @[simp, norm_cast] theorem coe_smul [Zero R] [SMulZeroClass S R] (s : S) (r : R) : (↑(s • r) : ℍ[R,c₁,c₂]) = s • (r : ℍ[R,c₁,c₂]) := QuaternionAlgebra.ext rfl (smul_zero s).symm (smul_zero s).symm (smul_zero s).symm instance [AddCommGroup R] : AddCommGroup ℍ[R,c₁,c₂] := (equivProd c₁ c₂).injective.addCommGroup _ rfl (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) section AddCommGroupWithOne variable [AddCommGroupWithOne R] instance : AddCommGroupWithOne ℍ[R,c₁,c₂] where natCast n := ((n : R) : ℍ[R,c₁,c₂]) natCast_zero := by simp natCast_succ := by simp intCast n := ((n : R) : ℍ[R,c₁,c₂]) intCast_ofNat _ := congr_arg coe (Int.cast_natCast _) intCast_negSucc n := by change coe _ = -coe _ rw [Int.cast_negSucc, coe_neg] @[simp, norm_cast] theorem natCast_re (n : ℕ) : (n : ℍ[R,c₁,c₂]).re = n := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_re := natCast_re @[simp, norm_cast] theorem natCast_imI (n : ℕ) : (n : ℍ[R,c₁,c₂]).imI = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imI := natCast_imI @[simp, norm_cast] theorem natCast_imJ (n : ℕ) : (n : ℍ[R,c₁,c₂]).imJ = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imJ := natCast_imJ @[simp, norm_cast] theorem natCast_imK (n : ℕ) : (n : ℍ[R,c₁,c₂]).imK = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imK := natCast_imK @[simp, norm_cast] theorem natCast_im (n : ℕ) : (n : ℍ[R,c₁,c₂]).im = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_im := natCast_im @[norm_cast] theorem coe_natCast (n : ℕ) : ↑(n : R) = (n : ℍ[R,c₁,c₂]) := rfl @[deprecated (since := "2024-04-17")] alias coe_nat_cast := coe_natCast @[simp, norm_cast] theorem intCast_re (z : ℤ) : (z : ℍ[R,c₁,c₂]).re = z := rfl @[deprecated (since := "2024-04-17")] alias int_cast_re := intCast_re @[simp, norm_cast] theorem intCast_imI (z : ℤ) : (z : ℍ[R,c₁,c₂]).imI = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imI := intCast_imI @[simp, norm_cast] theorem intCast_imJ (z : ℤ) : (z : ℍ[R,c₁,c₂]).imJ = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imJ := intCast_imJ @[simp, norm_cast] theorem intCast_imK (z : ℤ) : (z : ℍ[R,c₁,c₂]).imK = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imK := intCast_imK @[simp, norm_cast] theorem intCast_im (z : ℤ) : (z : ℍ[R,c₁,c₂]).im = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_im := intCast_im @[norm_cast] theorem coe_intCast (z : ℤ) : ↑(z : R) = (z : ℍ[R,c₁,c₂]) := rfl @[deprecated (since := "2024-04-17")] alias coe_int_cast := coe_intCast end AddCommGroupWithOne -- For the remainder of the file we assume `CommRing R`. variable [CommRing R] instance instRing : Ring ℍ[R,c₁,c₂] where __ := inferInstanceAs (AddCommGroupWithOne ℍ[R,c₁,c₂]) left_distrib _ _ _ := by ext <;> simp <;> ring right_distrib _ _ _ := by ext <;> simp <;> ring zero_mul _ := by ext <;> simp mul_zero _ := by ext <;> simp mul_assoc _ _ _ := by ext <;> simp <;> ring one_mul _ := by ext <;> simp mul_one _ := by ext <;> simp @[norm_cast, simp] theorem coe_mul : ((x * y : R) : ℍ[R,c₁,c₂]) = x * y := by ext <;> simp -- TODO: add weaker `MulAction`, `DistribMulAction`, and `Module` instances (and repeat them -- for `ℍ[R]`) instance [CommSemiring S] [Algebra S R] : Algebra S ℍ[R,c₁,c₂] where smul := (· • ·) toFun s := coe (algebraMap S R s) map_one' := by simp only [map_one, coe_one] map_zero' := by simp only [map_zero, coe_zero] map_mul' x y := by simp only [map_mul, coe_mul] map_add' x y := by simp only [map_add, coe_add] smul_def' s x := by ext <;> simp [Algebra.smul_def] commutes' s x := by ext <;> simp [Algebra.commutes] theorem algebraMap_eq (r : R) : algebraMap R ℍ[R,c₁,c₂] r = ⟨r, 0, 0, 0⟩ := rfl theorem algebraMap_injective : (algebraMap R ℍ[R,c₁,c₂] : _ → _).Injective := fun _ _ ↦ by simp [algebraMap_eq] instance [NoZeroDivisors R] : NoZeroSMulDivisors R ℍ[R,c₁,c₂] := ⟨by rintro t ⟨a, b, c, d⟩ h rw [or_iff_not_imp_left] intro ht simpa [QuaternionAlgebra.ext_iff, ht] using h⟩ section variable (c₁ c₂) /-- `QuaternionAlgebra.re` as a `LinearMap`-/ @[simps] def reₗ : ℍ[R,c₁,c₂] →ₗ[R] R where toFun := re map_add' _ _ := rfl map_smul' _ _ := rfl /-- `QuaternionAlgebra.imI` as a `LinearMap`-/ @[simps] def imIₗ : ℍ[R,c₁,c₂] →ₗ[R] R where toFun := imI map_add' _ _ := rfl map_smul' _ _ := rfl /-- `QuaternionAlgebra.imJ` as a `LinearMap`-/ @[simps] def imJₗ : ℍ[R,c₁,c₂] →ₗ[R] R where toFun := imJ map_add' _ _ := rfl map_smul' _ _ := rfl /-- `QuaternionAlgebra.imK` as a `LinearMap`-/ @[simps] def imKₗ : ℍ[R,c₁,c₂] →ₗ[R] R where toFun := imK map_add' _ _ := rfl map_smul' _ _ := rfl /-- `QuaternionAlgebra.equivTuple` as a linear equivalence. -/ def linearEquivTuple : ℍ[R,c₁,c₂] ≃ₗ[R] Fin 4 → R := LinearEquiv.symm -- proofs are not `rfl` in the forward direction { (equivTuple c₁ c₂).symm with toFun := (equivTuple c₁ c₂).symm invFun := equivTuple c₁ c₂ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl } @[simp] theorem coe_linearEquivTuple : ⇑(linearEquivTuple c₁ c₂) = equivTuple c₁ c₂ := rfl @[simp] theorem coe_linearEquivTuple_symm : ⇑(linearEquivTuple c₁ c₂).symm = (equivTuple c₁ c₂).symm := rfl /-- `ℍ[R, c₁, c₂]` has a basis over `R` given by `1`, `i`, `j`, and `k`. -/ noncomputable def basisOneIJK : Basis (Fin 4) R ℍ[R,c₁,c₂] := .ofEquivFun <| linearEquivTuple c₁ c₂ @[simp] theorem coe_basisOneIJK_repr (q : ℍ[R,c₁,c₂]) : ⇑((basisOneIJK c₁ c₂).repr q) = ![q.re, q.imI, q.imJ, q.imK] := rfl instance : Module.Finite R ℍ[R,c₁,c₂] := .of_basis (basisOneIJK c₁ c₂) instance : Module.Free R ℍ[R,c₁,c₂] := .of_basis (basisOneIJK c₁ c₂) theorem rank_eq_four [StrongRankCondition R] : Module.rank R ℍ[R,c₁,c₂] = 4 := by rw [rank_eq_card_basis (basisOneIJK c₁ c₂), Fintype.card_fin] norm_num theorem finrank_eq_four [StrongRankCondition R] : FiniteDimensional.finrank R ℍ[R,c₁,c₂] = 4 := by rw [FiniteDimensional.finrank, rank_eq_four, Cardinal.toNat_ofNat] /-- There is a natural equivalence when swapping the coefficients of a quaternion algebra. -/ @[simps] def swapEquiv : ℍ[R,c₁,c₂] ≃ₐ[R] ℍ[R, c₂, c₁] where toFun t := ⟨t.1, t.3, t.2, -t.4⟩ invFun t := ⟨t.1, t.3, t.2, -t.4⟩ left_inv _ := by simp right_inv _ := by simp map_mul' _ _ := by ext <;> simp only [mul_re, mul_imJ, mul_imI, add_left_inj, mul_imK, neg_mul, neg_add_rev, neg_sub, mk_mul_mk, mul_neg, neg_neg, sub_neg_eq_add] <;> ring map_add' _ _ := by ext <;> simp [add_comm] commutes' _ := by simp [algebraMap_eq] end @[norm_cast, simp] theorem coe_sub : ((x - y : R) : ℍ[R,c₁,c₂]) = x - y := (algebraMap R ℍ[R,c₁,c₂]).map_sub x y @[norm_cast, simp] theorem coe_pow (n : ℕ) : (↑(x ^ n) : ℍ[R,c₁,c₂]) = (x : ℍ[R,c₁,c₂]) ^ n := (algebraMap R ℍ[R,c₁,c₂]).map_pow x n theorem coe_commutes : ↑r * a = a * r := Algebra.commutes r a theorem coe_commute : Commute (↑r) a := coe_commutes r a theorem coe_mul_eq_smul : ↑r * a = r • a := (Algebra.smul_def r a).symm theorem mul_coe_eq_smul : a * r = r • a := by rw [← coe_commutes, coe_mul_eq_smul] @[norm_cast, simp] theorem coe_algebraMap : ⇑(algebraMap R ℍ[R,c₁,c₂]) = coe := rfl theorem smul_coe : x • (y : ℍ[R,c₁,c₂]) = ↑(x * y) := by rw [coe_mul, coe_mul_eq_smul] /-- Quaternion conjugate. -/ instance instStarQuaternionAlgebra : Star ℍ[R,c₁,c₂] where star a := ⟨a.1, -a.2, -a.3, -a.4⟩ @[simp] theorem re_star : (star a).re = a.re := rfl @[simp] theorem imI_star : (star a).imI = -a.imI := rfl @[simp] theorem imJ_star : (star a).imJ = -a.imJ := rfl @[simp] theorem imK_star : (star a).imK = -a.imK := rfl @[simp] theorem im_star : (star a).im = -a.im := QuaternionAlgebra.ext neg_zero.symm rfl rfl rfl @[simp] theorem star_mk (a₁ a₂ a₃ a₄ : R) : star (mk a₁ a₂ a₃ a₄ : ℍ[R,c₁,c₂]) = ⟨a₁, -a₂, -a₃, -a₄⟩ := rfl instance instStarRing : StarRing ℍ[R,c₁,c₂] where star_involutive x := by simp [Star.star] star_add a b := by ext <;> simp [add_comm] star_mul a b := by ext <;> simp <;> ring theorem self_add_star' : a + star a = ↑(2 * a.re) := by ext <;> simp [two_mul] theorem self_add_star : a + star a = 2 * a.re := by simp only [self_add_star', two_mul, coe_add] theorem star_add_self' : star a + a = ↑(2 * a.re) := by rw [add_comm, self_add_star'] theorem star_add_self : star a + a = 2 * a.re := by rw [add_comm, self_add_star] theorem star_eq_two_re_sub : star a = ↑(2 * a.re) - a := eq_sub_iff_add_eq.2 a.star_add_self' instance : IsStarNormal a := ⟨by rw [a.star_eq_two_re_sub] exact (coe_commute (2 * a.re) a).sub_left (Commute.refl a)⟩ @[simp, norm_cast] theorem star_coe : star (x : ℍ[R,c₁,c₂]) = x := by ext <;> simp @[simp] theorem star_im : star a.im = -a.im := im_star _ @[simp] theorem star_smul [Monoid S] [DistribMulAction S R] (s : S) (a : ℍ[R,c₁,c₂]) : star (s • a) = s • star a := QuaternionAlgebra.ext rfl (smul_neg _ _).symm (smul_neg _ _).symm (smul_neg _ _).symm theorem eq_re_of_eq_coe {a : ℍ[R,c₁,c₂]} {x : R} (h : a = x) : a = a.re := by rw [h, coe_re] theorem eq_re_iff_mem_range_coe {a : ℍ[R,c₁,c₂]} : a = a.re ↔ a ∈ Set.range (coe : R → ℍ[R,c₁,c₂]) := ⟨fun h => ⟨a.re, h.symm⟩, fun ⟨_, h⟩ => eq_re_of_eq_coe h.symm⟩ section CharZero variable [NoZeroDivisors R] [CharZero R] @[simp] theorem star_eq_self {c₁ c₂ : R} {a : ℍ[R,c₁,c₂]} : star a = a ↔ a = a.re := by simp [QuaternionAlgebra.ext_iff, neg_eq_iff_add_eq_zero, add_self_eq_zero] theorem star_eq_neg {c₁ c₂ : R} {a : ℍ[R,c₁,c₂]} : star a = -a ↔ a.re = 0 := by simp [QuaternionAlgebra.ext_iff, eq_neg_iff_add_eq_zero] end CharZero -- Can't use `rw ← star_eq_self` in the proof without additional assumptions theorem star_mul_eq_coe : star a * a = (star a * a).re := by ext <;> simp <;> ring theorem mul_star_eq_coe : a * star a = (a * star a).re := by rw [← star_comm_self'] exact a.star_mul_eq_coe open MulOpposite /-- Quaternion conjugate as an `AlgEquiv` to the opposite ring. -/ def starAe : ℍ[R,c₁,c₂] ≃ₐ[R] ℍ[R,c₁,c₂]ᵐᵒᵖ := { starAddEquiv.trans opAddEquiv with toFun := op ∘ star invFun := star ∘ unop map_mul' := fun x y => by simp commutes' := fun r => by simp } @[simp] theorem coe_starAe : ⇑(starAe : ℍ[R,c₁,c₂] ≃ₐ[R] _) = op ∘ star := rfl end QuaternionAlgebra /-- Space of quaternions over a type. Implemented as a structure with four fields: `re`, `im_i`, `im_j`, and `im_k`. -/ def Quaternion (R : Type*) [One R] [Neg R] := QuaternionAlgebra R (-1) (-1) scoped[Quaternion] notation "ℍ[" R "]" => Quaternion R /-- The equivalence between the quaternions over `R` and `R × R × R × R`. -/ @[simps!] def Quaternion.equivProd (R : Type*) [One R] [Neg R] : ℍ[R] ≃ R × R × R × R := QuaternionAlgebra.equivProd _ _ /-- The equivalence between the quaternions over `R` and `Fin 4 → R`. -/ @[simps! symm_apply] def Quaternion.equivTuple (R : Type*) [One R] [Neg R] : ℍ[R] ≃ (Fin 4 → R) := QuaternionAlgebra.equivTuple _ _ @[simp] theorem Quaternion.equivTuple_apply (R : Type*) [One R] [Neg R] (x : ℍ[R]) : Quaternion.equivTuple R x = ![x.re, x.imI, x.imJ, x.imK] := rfl instance {R : Type*} [One R] [Neg R] [Subsingleton R] : Subsingleton ℍ[R] := inferInstanceAs (Subsingleton <| ℍ[R, -1, -1]) instance {R : Type*} [One R] [Neg R] [Nontrivial R] : Nontrivial ℍ[R] := inferInstanceAs (Nontrivial <| ℍ[R, -1, -1]) namespace Quaternion variable {S T R : Type*} [CommRing R] (r x y z : R) (a b c : ℍ[R]) export QuaternionAlgebra (re imI imJ imK) /-- Coercion `R → ℍ[R]`. -/ @[coe] def coe : R → ℍ[R] := QuaternionAlgebra.coe instance : CoeTC R ℍ[R] := ⟨coe⟩ instance instRing : Ring ℍ[R] := QuaternionAlgebra.instRing instance : Inhabited ℍ[R] := inferInstanceAs <| Inhabited ℍ[R,-1,-1] instance [SMul S R] : SMul S ℍ[R] := inferInstanceAs <| SMul S ℍ[R,-1,-1] instance [SMul S T] [SMul S R] [SMul T R] [IsScalarTower S T R] : IsScalarTower S T ℍ[R] := inferInstanceAs <| IsScalarTower S T ℍ[R,-1,-1] instance [SMul S R] [SMul T R] [SMulCommClass S T R] : SMulCommClass S T ℍ[R] := inferInstanceAs <| SMulCommClass S T ℍ[R,-1,-1] protected instance algebra [CommSemiring S] [Algebra S R] : Algebra S ℍ[R] := inferInstanceAs <| Algebra S ℍ[R,-1,-1] -- Porting note: added shortcut instance : Star ℍ[R] := QuaternionAlgebra.instStarQuaternionAlgebra instance : StarRing ℍ[R] := QuaternionAlgebra.instStarRing instance : IsStarNormal a := inferInstanceAs <| IsStarNormal (R := ℍ[R,-1,-1]) a @[ext] theorem ext : a.re = b.re → a.imI = b.imI → a.imJ = b.imJ → a.imK = b.imK → a = b := QuaternionAlgebra.ext /-- The imaginary part of a quaternion. -/ nonrec def im (x : ℍ[R]) : ℍ[R] := x.im @[simp] theorem im_re : a.im.re = 0 := rfl @[simp] theorem im_imI : a.im.imI = a.imI := rfl @[simp] theorem im_imJ : a.im.imJ = a.imJ := rfl @[simp] theorem im_imK : a.im.imK = a.imK := rfl @[simp] theorem im_idem : a.im.im = a.im := rfl @[simp] nonrec theorem re_add_im : ↑a.re + a.im = a := a.re_add_im @[simp] nonrec theorem sub_self_im : a - a.im = a.re := a.sub_self_im @[simp] nonrec theorem sub_self_re : a - ↑a.re = a.im := a.sub_self_re @[simp, norm_cast] theorem coe_re : (x : ℍ[R]).re = x := rfl @[simp, norm_cast] theorem coe_imI : (x : ℍ[R]).imI = 0 := rfl @[simp, norm_cast] theorem coe_imJ : (x : ℍ[R]).imJ = 0 := rfl @[simp, norm_cast] theorem coe_imK : (x : ℍ[R]).imK = 0 := rfl @[simp, norm_cast] theorem coe_im : (x : ℍ[R]).im = 0 := rfl @[simp] theorem zero_re : (0 : ℍ[R]).re = 0 := rfl @[simp] theorem zero_imI : (0 : ℍ[R]).imI = 0 := rfl @[simp] theorem zero_imJ : (0 : ℍ[R]).imJ = 0 := rfl @[simp] theorem zero_imK : (0 : ℍ[R]).imK = 0 := rfl @[simp] theorem zero_im : (0 : ℍ[R]).im = 0 := rfl @[simp, norm_cast] theorem coe_zero : ((0 : R) : ℍ[R]) = 0 := rfl @[simp] theorem one_re : (1 : ℍ[R]).re = 1 := rfl @[simp] theorem one_imI : (1 : ℍ[R]).imI = 0 := rfl @[simp] theorem one_imJ : (1 : ℍ[R]).imJ = 0 := rfl @[simp] theorem one_imK : (1 : ℍ[R]).imK = 0 := rfl @[simp] theorem one_im : (1 : ℍ[R]).im = 0 := rfl @[simp, norm_cast] theorem coe_one : ((1 : R) : ℍ[R]) = 1 := rfl @[simp] theorem add_re : (a + b).re = a.re + b.re := rfl @[simp] theorem add_imI : (a + b).imI = a.imI + b.imI := rfl @[simp] theorem add_imJ : (a + b).imJ = a.imJ + b.imJ := rfl @[simp] theorem add_imK : (a + b).imK = a.imK + b.imK := rfl @[simp] nonrec theorem add_im : (a + b).im = a.im + b.im := a.add_im b @[simp, norm_cast] theorem coe_add : ((x + y : R) : ℍ[R]) = x + y := QuaternionAlgebra.coe_add x y @[simp] theorem neg_re : (-a).re = -a.re := rfl @[simp] theorem neg_imI : (-a).imI = -a.imI := rfl @[simp] theorem neg_imJ : (-a).imJ = -a.imJ := rfl @[simp] theorem neg_imK : (-a).imK = -a.imK := rfl @[simp] nonrec theorem neg_im : (-a).im = -a.im := a.neg_im @[simp, norm_cast] theorem coe_neg : ((-x : R) : ℍ[R]) = -x := QuaternionAlgebra.coe_neg x @[simp] theorem sub_re : (a - b).re = a.re - b.re := rfl @[simp] theorem sub_imI : (a - b).imI = a.imI - b.imI := rfl @[simp] theorem sub_imJ : (a - b).imJ = a.imJ - b.imJ := rfl @[simp] theorem sub_imK : (a - b).imK = a.imK - b.imK := rfl @[simp] nonrec theorem sub_im : (a - b).im = a.im - b.im := a.sub_im b @[simp, norm_cast] theorem coe_sub : ((x - y : R) : ℍ[R]) = x - y := QuaternionAlgebra.coe_sub x y @[simp] theorem mul_re : (a * b).re = a.re * b.re - a.imI * b.imI - a.imJ * b.imJ - a.imK * b.imK := (QuaternionAlgebra.mul_re a b).trans <| by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg] @[simp] theorem mul_imI : (a * b).imI = a.re * b.imI + a.imI * b.re + a.imJ * b.imK - a.imK * b.imJ := (QuaternionAlgebra.mul_imI a b).trans <| by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg] @[simp] theorem mul_imJ : (a * b).imJ = a.re * b.imJ - a.imI * b.imK + a.imJ * b.re + a.imK * b.imI := (QuaternionAlgebra.mul_imJ a b).trans <| by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg] @[simp] theorem mul_imK : (a * b).imK = a.re * b.imK + a.imI * b.imJ - a.imJ * b.imI + a.imK * b.re := (QuaternionAlgebra.mul_imK a b).trans <| by simp only [one_mul, neg_mul, sub_eq_add_neg, neg_neg] @[simp, norm_cast] theorem coe_mul : ((x * y : R) : ℍ[R]) = x * y := QuaternionAlgebra.coe_mul x y @[norm_cast, simp] theorem coe_pow (n : ℕ) : (↑(x ^ n) : ℍ[R]) = (x : ℍ[R]) ^ n := QuaternionAlgebra.coe_pow x n @[simp, norm_cast] theorem natCast_re (n : ℕ) : (n : ℍ[R]).re = n := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_re := natCast_re @[simp, norm_cast] theorem natCast_imI (n : ℕ) : (n : ℍ[R]).imI = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imI := natCast_imI @[simp, norm_cast] theorem natCast_imJ (n : ℕ) : (n : ℍ[R]).imJ = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imJ := natCast_imJ @[simp, norm_cast] theorem natCast_imK (n : ℕ) : (n : ℍ[R]).imK = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_imK := natCast_imK @[simp, norm_cast] theorem natCast_im (n : ℕ) : (n : ℍ[R]).im = 0 := rfl @[deprecated (since := "2024-04-17")] alias nat_cast_im := natCast_im @[norm_cast] theorem coe_natCast (n : ℕ) : ↑(n : R) = (n : ℍ[R]) := rfl @[deprecated (since := "2024-04-17")] alias coe_nat_cast := coe_natCast @[simp, norm_cast] theorem intCast_re (z : ℤ) : (z : ℍ[R]).re = z := rfl @[deprecated (since := "2024-04-17")] alias int_cast_re := intCast_re @[simp, norm_cast] theorem intCast_imI (z : ℤ) : (z : ℍ[R]).imI = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imI := intCast_imI @[simp, norm_cast] theorem intCast_imJ (z : ℤ) : (z : ℍ[R]).imJ = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imJ := intCast_imJ @[simp, norm_cast] theorem intCast_imK (z : ℤ) : (z : ℍ[R]).imK = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_imK := intCast_imK @[simp, norm_cast] theorem intCast_im (z : ℤ) : (z : ℍ[R]).im = 0 := rfl @[deprecated (since := "2024-04-17")] alias int_cast_im := intCast_im @[norm_cast] theorem coe_intCast (z : ℤ) : ↑(z : R) = (z : ℍ[R]) := rfl @[deprecated (since := "2024-04-17")] alias coe_int_cast := coe_intCast theorem coe_injective : Function.Injective (coe : R → ℍ[R]) := QuaternionAlgebra.coe_injective @[simp] theorem coe_inj {x y : R} : (x : ℍ[R]) = y ↔ x = y := coe_injective.eq_iff @[simp] theorem smul_re [SMul S R] (s : S) : (s • a).re = s • a.re := rfl @[simp] theorem smul_imI [SMul S R] (s : S) : (s • a).imI = s • a.imI := rfl @[simp] theorem smul_imJ [SMul S R] (s : S) : (s • a).imJ = s • a.imJ := rfl @[simp] theorem smul_imK [SMul S R] (s : S) : (s • a).imK = s • a.imK := rfl @[simp] nonrec theorem smul_im [SMulZeroClass S R] (s : S) : (s • a).im = s • a.im := a.smul_im s @[simp, norm_cast] theorem coe_smul [SMulZeroClass S R] (s : S) (r : R) : (↑(s • r) : ℍ[R]) = s • (r : ℍ[R]) := QuaternionAlgebra.coe_smul _ _ theorem coe_commutes : ↑r * a = a * r := QuaternionAlgebra.coe_commutes r a theorem coe_commute : Commute (↑r) a := QuaternionAlgebra.coe_commute r a theorem coe_mul_eq_smul : ↑r * a = r • a := QuaternionAlgebra.coe_mul_eq_smul r a theorem mul_coe_eq_smul : a * r = r • a := QuaternionAlgebra.mul_coe_eq_smul r a @[simp] theorem algebraMap_def : ⇑(algebraMap R ℍ[R]) = coe := rfl theorem algebraMap_injective : (algebraMap R ℍ[R] : _ → _).Injective := QuaternionAlgebra.algebraMap_injective theorem smul_coe : x • (y : ℍ[R]) = ↑(x * y) := QuaternionAlgebra.smul_coe x y instance : Module.Finite R ℍ[R] := inferInstanceAs <| Module.Finite R ℍ[R,-1,-1] instance : Module.Free R ℍ[R] := inferInstanceAs <| Module.Free R ℍ[R,-1,-1] theorem rank_eq_four [StrongRankCondition R] : Module.rank R ℍ[R] = 4 := QuaternionAlgebra.rank_eq_four _ _ theorem finrank_eq_four [StrongRankCondition R] : FiniteDimensional.finrank R ℍ[R] = 4 := QuaternionAlgebra.finrank_eq_four _ _ @[simp] theorem star_re : (star a).re = a.re := rfl @[simp] theorem star_imI : (star a).imI = -a.imI := rfl @[simp] theorem star_imJ : (star a).imJ = -a.imJ := rfl @[simp] theorem star_imK : (star a).imK = -a.imK := rfl @[simp] theorem star_im : (star a).im = -a.im := a.im_star nonrec theorem self_add_star' : a + star a = ↑(2 * a.re) := a.self_add_star' nonrec theorem self_add_star : a + star a = 2 * a.re := a.self_add_star nonrec theorem star_add_self' : star a + a = ↑(2 * a.re) := a.star_add_self' nonrec theorem star_add_self : star a + a = 2 * a.re := a.star_add_self nonrec theorem star_eq_two_re_sub : star a = ↑(2 * a.re) - a := a.star_eq_two_re_sub @[simp, norm_cast] theorem star_coe : star (x : ℍ[R]) = x := QuaternionAlgebra.star_coe x @[simp] theorem im_star : star a.im = -a.im := QuaternionAlgebra.im_star _ @[simp] theorem star_smul [Monoid S] [DistribMulAction S R] (s : S) (a : ℍ[R]) : star (s • a) = s • star a := QuaternionAlgebra.star_smul _ _ theorem eq_re_of_eq_coe {a : ℍ[R]} {x : R} (h : a = x) : a = a.re := QuaternionAlgebra.eq_re_of_eq_coe h theorem eq_re_iff_mem_range_coe {a : ℍ[R]} : a = a.re ↔ a ∈ Set.range (coe : R → ℍ[R]) := QuaternionAlgebra.eq_re_iff_mem_range_coe section CharZero variable [NoZeroDivisors R] [CharZero R] @[simp] theorem star_eq_self {a : ℍ[R]} : star a = a ↔ a = a.re := QuaternionAlgebra.star_eq_self @[simp] theorem star_eq_neg {a : ℍ[R]} : star a = -a ↔ a.re = 0 := QuaternionAlgebra.star_eq_neg end CharZero nonrec theorem star_mul_eq_coe : star a * a = (star a * a).re := a.star_mul_eq_coe nonrec theorem mul_star_eq_coe : a * star a = (a * star a).re := a.mul_star_eq_coe open MulOpposite /-- Quaternion conjugate as an `AlgEquiv` to the opposite ring. -/ def starAe : ℍ[R] ≃ₐ[R] ℍ[R]ᵐᵒᵖ := QuaternionAlgebra.starAe @[simp] theorem coe_starAe : ⇑(starAe : ℍ[R] ≃ₐ[R] ℍ[R]ᵐᵒᵖ) = op ∘ star := rfl /-- Square of the norm. -/ def normSq : ℍ[R] →*₀ R where toFun a := (a * star a).re map_zero' := by simp only [star_zero, zero_mul, zero_re] map_one' := by simp only [star_one, one_mul, one_re] map_mul' x y := coe_injective <| by conv_lhs => rw [← mul_star_eq_coe, star_mul, mul_assoc, ← mul_assoc y, y.mul_star_eq_coe, coe_commutes, ← mul_assoc, x.mul_star_eq_coe, ← coe_mul] theorem normSq_def : normSq a = (a * star a).re := rfl theorem normSq_def' : normSq a = a.1 ^ 2 + a.2 ^ 2 + a.3 ^ 2 + a.4 ^ 2 := by simp only [normSq_def, sq, mul_neg, sub_neg_eq_add, mul_re, star_re, star_imI, star_imJ, star_imK] theorem normSq_coe : normSq (x : ℍ[R]) = x ^ 2 := by rw [normSq_def, star_coe, ← coe_mul, coe_re, sq] @[simp] theorem normSq_star : normSq (star a) = normSq a := by simp [normSq_def'] @[norm_cast] theorem normSq_natCast (n : ℕ) : normSq (n : ℍ[R]) = (n : R) ^ 2 := by rw [← coe_natCast, normSq_coe] @[deprecated (since := "2024-04-17")] alias normSq_nat_cast := normSq_natCast @[norm_cast] theorem normSq_intCast (z : ℤ) : normSq (z : ℍ[R]) = (z : R) ^ 2 := by rw [← coe_intCast, normSq_coe] @[deprecated (since := "2024-04-17")] alias normSq_int_cast := normSq_intCast @[simp] theorem normSq_neg : normSq (-a) = normSq a := by simp only [normSq_def, star_neg, neg_mul_neg] theorem self_mul_star : a * star a = normSq a := by rw [mul_star_eq_coe, normSq_def] theorem star_mul_self : star a * a = normSq a := by rw [star_comm_self, self_mul_star] theorem im_sq : a.im ^ 2 = -normSq a.im := by simp_rw [sq, ← star_mul_self, im_star, neg_mul, neg_neg] theorem coe_normSq_add : normSq (a + b) = normSq a + a * star b + b * star a + normSq b := by simp only [star_add, ← self_mul_star, mul_add, add_mul, add_assoc, add_left_comm] theorem normSq_smul (r : R) (q : ℍ[R]) : normSq (r • q) = r ^ 2 * normSq q := by simp only [normSq_def', smul_re, smul_imI, smul_imJ, smul_imK, mul_pow, mul_add, smul_eq_mul] theorem normSq_add (a b : ℍ[R]) : normSq (a + b) = normSq a + normSq b + 2 * (a * star b).re := calc normSq (a + b) = normSq a + (a * star b).re + ((b * star a).re + normSq b) := by simp_rw [normSq_def, star_add, add_mul, mul_add, add_re] _ = normSq a + normSq b + ((a * star b).re + (b * star a).re) := by abel _ = normSq a + normSq b + 2 * (a * star b).re := by rw [← add_re, ← star_mul_star a b, self_add_star', coe_re] end Quaternion namespace Quaternion variable {R : Type*} section LinearOrderedCommRing variable [LinearOrderedCommRing R] {a : ℍ[R]} @[simp] theorem normSq_eq_zero : normSq a = 0 ↔ a = 0 := by refine ⟨fun h => ?_, fun h => h.symm ▸ normSq.map_zero⟩ rw [normSq_def', add_eq_zero_iff', add_eq_zero_iff', add_eq_zero_iff'] at h · exact ext a 0 (pow_eq_zero h.1.1.1) (pow_eq_zero h.1.1.2) (pow_eq_zero h.1.2) (pow_eq_zero h.2) all_goals apply_rules [sq_nonneg, add_nonneg] theorem normSq_ne_zero : normSq a ≠ 0 ↔ a ≠ 0 := normSq_eq_zero.not @[simp] theorem normSq_nonneg : 0 ≤ normSq a := by rw [normSq_def'] apply_rules [sq_nonneg, add_nonneg] @[simp] theorem normSq_le_zero : normSq a ≤ 0 ↔ a = 0 := normSq_nonneg.le_iff_eq.trans normSq_eq_zero instance instNontrivial : Nontrivial ℍ[R] where exists_pair_ne := ⟨0, 1, mt (congr_arg re) zero_ne_one⟩ instance : NoZeroDivisors ℍ[R] where eq_zero_or_eq_zero_of_mul_eq_zero {a b} hab := have : normSq a * normSq b = 0 := by rwa [← map_mul, normSq_eq_zero] (eq_zero_or_eq_zero_of_mul_eq_zero this).imp normSq_eq_zero.1 normSq_eq_zero.1 instance : IsDomain ℍ[R] := NoZeroDivisors.to_isDomain _ theorem sq_eq_normSq : a ^ 2 = normSq a ↔ a = a.re := by rw [← star_eq_self, ← star_mul_self, sq, mul_eq_mul_right_iff, eq_comm] exact or_iff_left_of_imp fun ha ↦ ha.symm ▸ star_zero _ theorem sq_eq_neg_normSq : a ^ 2 = -normSq a ↔ a.re = 0 := by simp_rw [← star_eq_neg] obtain rfl | hq0 := eq_or_ne a 0 · simp · rw [← star_mul_self, ← mul_neg, ← neg_sq, sq, mul_left_inj' (neg_ne_zero.mpr hq0), eq_comm] end LinearOrderedCommRing section Field variable [LinearOrderedField R] (a b : ℍ[R]) @[simps (config := .lemmasOnly)] instance instInv : Inv ℍ[R] := ⟨fun a => (normSq a)⁻¹ • star a⟩ instance instGroupWithZero : GroupWithZero ℍ[R] := { Quaternion.instNontrivial, (by infer_instance : MonoidWithZero ℍ[R]) with inv := Inv.inv inv_zero := by rw [instInv_inv, star_zero, smul_zero] mul_inv_cancel := fun a ha => by -- Porting note: the aliased definition confuse TC search letI : Semiring ℍ[R] := inferInstanceAs (Semiring ℍ[R,-1,-1]) rw [instInv_inv, Algebra.mul_smul_comm (normSq a)⁻¹ a (star a), self_mul_star, smul_coe, inv_mul_cancel (normSq_ne_zero.2 ha), coe_one] } @[norm_cast, simp] theorem coe_inv (x : R) : ((x⁻¹ : R) : ℍ[R]) = (↑x)⁻¹ := map_inv₀ (algebraMap R ℍ[R]) _ @[norm_cast, simp] theorem coe_div (x y : R) : ((x / y : R) : ℍ[R]) = x / y := map_div₀ (algebraMap R ℍ[R]) x y @[norm_cast, simp] theorem coe_zpow (x : R) (z : ℤ) : ((x ^ z : R) : ℍ[R]) = (x : ℍ[R]) ^ z := map_zpow₀ (algebraMap R ℍ[R]) x z instance instNNRatCast : NNRatCast ℍ[R] where nnratCast q := (q : R) instance instRatCast : RatCast ℍ[R] where ratCast q := (q : R) @[simp, norm_cast] lemma re_nnratCast (q : ℚ≥0) : (q : ℍ[R]).re = q := rfl @[simp, norm_cast] lemma im_nnratCast (q : ℚ≥0) : (q : ℍ[R]).im = 0 := rfl @[simp, norm_cast] lemma imI_nnratCast (q : ℚ≥0) : (q : ℍ[R]).imI = 0 := rfl @[simp, norm_cast] lemma imJ_nnratCast (q : ℚ≥0) : (q : ℍ[R]).imJ = 0 := rfl @[simp, norm_cast] lemma imK_nnratCast (q : ℚ≥0) : (q : ℍ[R]).imK = 0 := rfl @[simp, norm_cast] lemma ratCast_re (q : ℚ) : (q : ℍ[R]).re = q := rfl @[simp, norm_cast] lemma ratCast_im (q : ℚ) : (q : ℍ[R]).im = 0 := rfl @[simp, norm_cast] lemma ratCast_imI (q : ℚ) : (q : ℍ[R]).imI = 0 := rfl @[simp, norm_cast] lemma ratCast_imJ (q : ℚ) : (q : ℍ[R]).imJ = 0 := rfl @[simp, norm_cast] lemma ratCast_imK (q : ℚ) : (q : ℍ[R]).imK = 0 := rfl @[deprecated (since := "2024-04-17")] alias rat_cast_imI := ratCast_imI @[deprecated (since := "2024-04-17")] alias rat_cast_imJ := ratCast_imJ @[deprecated (since := "2024-04-17")] alias rat_cast_imK := ratCast_imK @[norm_cast] lemma coe_nnratCast (q : ℚ≥0) : ↑(q : R) = (q : ℍ[R]) := rfl @[norm_cast] lemma coe_ratCast (q : ℚ) : ↑(q : R) = (q : ℍ[R]) := rfl @[deprecated (since := "2024-04-17")] alias coe_rat_cast := coe_ratCast instance instDivisionRing : DivisionRing ℍ[R] where __ := Quaternion.instGroupWithZero __ := Quaternion.instRing nnqsmul := (· • ·) qsmul := (· • ·) nnratCast_def q := by rw [← coe_nnratCast, NNRat.cast_def, coe_div, coe_natCast, coe_natCast] ratCast_def q := by rw [← coe_ratCast, Rat.cast_def, coe_div, coe_intCast, coe_natCast] nnqsmul_def q x := by rw [← coe_nnratCast, coe_mul_eq_smul]; ext <;> exact NNRat.smul_def _ _ qsmul_def q x := by rw [← coe_ratCast, coe_mul_eq_smul]; ext <;> exact Rat.smul_def _ _ --@[simp] Porting note (#10618): `simp` can prove it theorem normSq_inv : normSq a⁻¹ = (normSq a)⁻¹ := map_inv₀ normSq _ --@[simp] Porting note (#10618): `simp` can prove it theorem normSq_div : normSq (a / b) = normSq a / normSq b := map_div₀ normSq a b --@[simp] Porting note (#10618): `simp` can prove it theorem normSq_zpow (z : ℤ) : normSq (a ^ z) = normSq a ^ z := map_zpow₀ normSq a z @[norm_cast] theorem normSq_ratCast (q : ℚ) : normSq (q : ℍ[R]) = (q : ℍ[R]) ^ 2 := by rw [← coe_ratCast, normSq_coe, coe_pow] @[deprecated (since := "2024-04-17")] alias normSq_rat_cast := normSq_ratCast end Field end Quaternion namespace Cardinal open Quaternion section QuaternionAlgebra variable {R : Type*} (c₁ c₂ : R) private theorem pow_four [Infinite R] : #R ^ 4 = #R := power_nat_eq (aleph0_le_mk R) <| by decide /-- The cardinality of a quaternion algebra, as a type. -/ theorem mk_quaternionAlgebra : #(ℍ[R,c₁,c₂]) = #R ^ 4 := by rw [mk_congr (QuaternionAlgebra.equivProd c₁ c₂)] simp only [mk_prod, lift_id] ring @[simp] theorem mk_quaternionAlgebra_of_infinite [Infinite R] : #(ℍ[R,c₁,c₂]) = #R := by rw [mk_quaternionAlgebra, pow_four] /-- The cardinality of a quaternion algebra, as a set. -/ theorem mk_univ_quaternionAlgebra : #(Set.univ : Set ℍ[R,c₁,c₂]) = #R ^ 4 := by rw [mk_univ, mk_quaternionAlgebra] --@[simp] Porting note (#10618): `simp` can prove it theorem mk_univ_quaternionAlgebra_of_infinite [Infinite R] : #(Set.univ : Set ℍ[R,c₁,c₂]) = #R := by rw [mk_univ_quaternionAlgebra, pow_four] /-- Show the quaternion ⟨w, x, y, z⟩ as a string "{ re := w, imI := x, imJ := y, imK := z }". For the typical case of quaternions over ℝ, each component will show as a Cauchy sequence due to the way Real numbers are represented. -/ instance [Repr R] {a b : R} : Repr ℍ[R, a, b] where reprPrec q _ := s!"\{ re := {repr q.re}, imI := {repr q.imI}, imJ := {repr q.imJ}, imK := {repr q.imK} }" end QuaternionAlgebra section Quaternion variable (R : Type*) [One R] [Neg R] /-- The cardinality of the quaternions, as a type. -/ @[simp] theorem mk_quaternion : #(ℍ[R]) = #R ^ 4 := mk_quaternionAlgebra _ _ --@[simp] Porting note: LHS can be simplified to `#R^4` theorem mk_quaternion_of_infinite [Infinite R] : #(ℍ[R]) = #R := mk_quaternionAlgebra_of_infinite _ _ /-- The cardinality of the quaternions, as a set. -/ --@[simp] Porting note (#10618): `simp` can prove it theorem mk_univ_quaternion : #(Set.univ : Set ℍ[R]) = #R ^ 4 := mk_univ_quaternionAlgebra _ _ --@[simp] Porting note: LHS can be simplified to `#R^4` theorem mk_univ_quaternion_of_infinite [Infinite R] : #(Set.univ : Set ℍ[R]) = #R := mk_univ_quaternionAlgebra_of_infinite _ _ end Quaternion end Cardinal
Algebra\QuaternionBasis.lean
/- Copyright (c) 2021 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Quaternion import Mathlib.Tactic.Ring /-! # Basis on a quaternion-like algebra ## Main definitions * `QuaternionAlgebra.Basis A c₁ c₂`: a basis for a subspace of an `R`-algebra `A` that has the same algebra structure as `ℍ[R,c₁,c₂]`. * `QuaternionAlgebra.Basis.self R`: the canonical basis for `ℍ[R,c₁,c₂]`. * `QuaternionAlgebra.Basis.compHom b f`: transform a basis `b` by an AlgHom `f`. * `QuaternionAlgebra.lift`: Define an `AlgHom` out of `ℍ[R,c₁,c₂]` by its action on the basis elements `i`, `j`, and `k`. In essence, this is a universal property. Analogous to `Complex.lift`, but takes a bundled `QuaternionAlgebra.Basis` instead of just a `Subtype` as the amount of data / proves is non-negligible. -/ open Quaternion namespace QuaternionAlgebra /-- A quaternion basis contains the information both sufficient and necessary to construct an `R`-algebra homomorphism from `ℍ[R,c₁,c₂]` to `A`; or equivalently, a surjective `R`-algebra homomorphism from `ℍ[R,c₁,c₂]` to an `R`-subalgebra of `A`. Note that for definitional convenience, `k` is provided as a field even though `i_mul_j` fully determines it. -/ structure Basis {R : Type*} (A : Type*) [CommRing R] [Ring A] [Algebra R A] (c₁ c₂ : R) where (i j k : A) i_mul_i : i * i = c₁ • (1 : A) j_mul_j : j * j = c₂ • (1 : A) i_mul_j : i * j = k j_mul_i : j * i = -k variable {R : Type*} {A B : Type*} [CommRing R] [Ring A] [Ring B] [Algebra R A] [Algebra R B] variable {c₁ c₂ : R} namespace Basis /-- Since `k` is redundant, it is not necessary to show `q₁.k = q₂.k` when showing `q₁ = q₂`. -/ @[ext] protected theorem ext ⦃q₁ q₂ : Basis A c₁ c₂⦄ (hi : q₁.i = q₂.i) (hj : q₁.j = q₂.j) : q₁ = q₂ := by cases q₁; rename_i q₁_i_mul_j _ cases q₂; rename_i q₂_i_mul_j _ congr rw [← q₁_i_mul_j, ← q₂_i_mul_j] congr variable (R) /-- There is a natural quaternionic basis for the `QuaternionAlgebra`. -/ @[simps i j k] protected def self : Basis ℍ[R,c₁,c₂] c₁ c₂ where i := ⟨0, 1, 0, 0⟩ i_mul_i := by ext <;> simp j := ⟨0, 0, 1, 0⟩ j_mul_j := by ext <;> simp k := ⟨0, 0, 0, 1⟩ i_mul_j := by ext <;> simp j_mul_i := by ext <;> simp variable {R} instance : Inhabited (Basis ℍ[R,c₁,c₂] c₁ c₂) := ⟨Basis.self R⟩ variable (q : Basis A c₁ c₂) attribute [simp] i_mul_i j_mul_j i_mul_j j_mul_i @[simp] theorem i_mul_k : q.i * q.k = c₁ • q.j := by rw [← i_mul_j, ← mul_assoc, i_mul_i, smul_mul_assoc, one_mul] @[simp] theorem k_mul_i : q.k * q.i = -c₁ • q.j := by rw [← i_mul_j, mul_assoc, j_mul_i, mul_neg, i_mul_k, neg_smul] @[simp] theorem k_mul_j : q.k * q.j = c₂ • q.i := by rw [← i_mul_j, mul_assoc, j_mul_j, mul_smul_comm, mul_one] @[simp] theorem j_mul_k : q.j * q.k = -c₂ • q.i := by rw [← i_mul_j, ← mul_assoc, j_mul_i, neg_mul, k_mul_j, neg_smul] @[simp] theorem k_mul_k : q.k * q.k = -((c₁ * c₂) • (1 : A)) := by rw [← i_mul_j, mul_assoc, ← mul_assoc q.j _ _, j_mul_i, ← i_mul_j, ← mul_assoc, mul_neg, ← mul_assoc, i_mul_i, smul_mul_assoc, one_mul, neg_mul, smul_mul_assoc, j_mul_j, smul_smul] /-- Intermediate result used to define `QuaternionAlgebra.Basis.liftHom`. -/ def lift (x : ℍ[R,c₁,c₂]) : A := algebraMap R _ x.re + x.imI • q.i + x.imJ • q.j + x.imK • q.k theorem lift_zero : q.lift (0 : ℍ[R,c₁,c₂]) = 0 := by simp [lift] theorem lift_one : q.lift (1 : ℍ[R,c₁,c₂]) = 1 := by simp [lift] theorem lift_add (x y : ℍ[R,c₁,c₂]) : q.lift (x + y) = q.lift x + q.lift y := by simp only [lift, add_re, map_add, add_imI, add_smul, add_imJ, add_imK] abel theorem lift_mul (x y : ℍ[R,c₁,c₂]) : q.lift (x * y) = q.lift x * q.lift y := by simp only [lift, Algebra.algebraMap_eq_smul_one] simp_rw [add_mul, mul_add, smul_mul_assoc, mul_smul_comm, one_mul, mul_one, smul_smul] simp only [i_mul_i, j_mul_j, i_mul_j, j_mul_i, i_mul_k, k_mul_i, k_mul_j, j_mul_k, k_mul_k] simp only [smul_smul, smul_neg, sub_eq_add_neg, add_smul, ← add_assoc, mul_neg, neg_smul] simp only [mul_right_comm _ _ (c₁ * c₂), mul_comm _ (c₁ * c₂)] simp only [mul_comm _ c₁, mul_right_comm _ _ c₁] simp only [mul_comm _ c₂, mul_right_comm _ _ c₂] simp only [← mul_comm c₁ c₂, ← mul_assoc] simp only [mul_re, sub_eq_add_neg, add_smul, neg_smul, mul_imI, ← add_assoc, mul_imJ, mul_imK] abel theorem lift_smul (r : R) (x : ℍ[R,c₁,c₂]) : q.lift (r • x) = r • q.lift x := by simp [lift, mul_smul, ← Algebra.smul_def] /-- A `QuaternionAlgebra.Basis` implies an `AlgHom` from the quaternions. -/ @[simps!] def liftHom : ℍ[R,c₁,c₂] →ₐ[R] A := AlgHom.mk' { toFun := q.lift map_zero' := q.lift_zero map_one' := q.lift_one map_add' := q.lift_add map_mul' := q.lift_mul } q.lift_smul /-- Transform a `QuaternionAlgebra.Basis` through an `AlgHom`. -/ @[simps i j k] def compHom (F : A →ₐ[R] B) : Basis B c₁ c₂ where i := F q.i i_mul_i := by rw [← map_mul, q.i_mul_i, map_smul, map_one] j := F q.j j_mul_j := by rw [← map_mul, q.j_mul_j, map_smul, map_one] k := F q.k i_mul_j := by rw [← map_mul, q.i_mul_j] j_mul_i := by rw [← map_mul, q.j_mul_i, map_neg] end Basis /-- A quaternionic basis on `A` is equivalent to a map from the quaternion algebra to `A`. -/ @[simps] def lift : Basis A c₁ c₂ ≃ (ℍ[R,c₁,c₂] →ₐ[R] A) where toFun := Basis.liftHom invFun := (Basis.self R).compHom left_inv q := by ext <;> simp [Basis.lift] right_inv F := by ext dsimp [Basis.lift] rw [← F.commutes] simp only [← F.commutes, ← map_smul, ← map_add, mk_add_mk, smul_mk, smul_zero, algebraMap_eq] congr <;> simp /-- Two `R`-algebra morphisms from a quaternion algebra are equal if they agree on `i` and `j`. -/ @[ext] theorem hom_ext ⦃f g : ℍ[R,c₁,c₂] →ₐ[R] A⦄ (hi : f (Basis.self R).i = g (Basis.self R).i) (hj : f (Basis.self R).j = g (Basis.self R).j) : f = g := lift.symm.injective <| Basis.ext hi hj end QuaternionAlgebra namespace Quaternion variable {R A : Type*} [CommRing R] [Ring A] [Algebra R A] open QuaternionAlgebra (Basis) /-- Two `R`-algebra morphisms from the quaternions are equal if they agree on `i` and `j`. -/ @[ext] theorem hom_ext ⦃f g : ℍ[R] →ₐ[R] A⦄ (hi : f (Basis.self R).i = g (Basis.self R).i) (hj : f (Basis.self R).j = g (Basis.self R).j) : f = g := QuaternionAlgebra.hom_ext hi hj end Quaternion
Algebra\Quotient.lean
/- Copyright (c) 2021 Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Anne Baanen -/ import Mathlib.Tactic.Common /-! # Algebraic quotients This file defines notation for algebraic quotients, e.g. quotient groups `G ⧸ H`, quotient modules `M ⧸ N` and ideal quotients `R ⧸ I`. The actual quotient structures are defined in the following files: * quotient group: `Mathlib/GroupTheory/QuotientGroup.lean` * quotient module: `Mathlib/LinearAlgebra/Quotient.lean` * quotient ring: `Mathlib/RingTheory/Ideal/Quotient.lean` ## Notations The following notation is introduced: * `G ⧸ H` stands for the quotient of the type `G` by some term `H` (for example, `H` can be a normal subgroup of `G`). To implement this notation for other quotients, you should provide a `HasQuotient` instance. Note that since `G` can usually be inferred from `H`, `_ ⧸ H` can also be used, but this is less readable. ## Tags quotient, group quotient, quotient group, module quotient, quotient module, ring quotient, ideal quotient, quotient ring -/ universe u v /-- `HasQuotient A B` is a notation typeclass that allows us to write `A ⧸ b` for `b : B`. This allows the usual notation for quotients of algebraic structures, such as groups, modules and rings. `A` is a parameter, despite being unused in the definition below, so it appears in the notation. -/ class HasQuotient (A : outParam <| Type u) (B : Type v) where /-- auxiliary quotient function, the one used will have `A` explicit -/ quotient' : B → Type max u v -- Will be provided by e.g. `Ideal.Quotient.inhabited` /-- `HasQuotient.Quotient A b` (with notation `A ⧸ b`) is the quotient of the type `A` by `b`. This differs from `HasQuotient.quotient'` in that the `A` argument is explicit, which is necessary to make Lean show the notation in the goal state. -/ abbrev HasQuotient.Quotient (A : outParam <| Type u) {B : Type v} [HasQuotient A B] (b : B) : Type max u v := HasQuotient.quotient' b /-- Quotient notation based on the `HasQuotient` typeclass -/ notation:35 G " ⧸ " H:34 => HasQuotient.Quotient G H
Algebra\RingQuot.lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.Algebra.Algebra.Hom import Mathlib.RingTheory.Ideal.Quotient /-! # Quotients of non-commutative rings Unfortunately, ideals have only been developed in the commutative case as `Ideal`, and it's not immediately clear how one should formalise ideals in the non-commutative case. In this file, we directly define the quotient of a semiring by any relation, by building a bigger relation that represents the ideal generated by that relation. We prove the universal properties of the quotient, and recommend avoiding relying on the actual definition, which is made irreducible for this purpose. Since everything runs in parallel for quotients of `R`-algebras, we do that case at the same time. -/ assert_not_exists Star.star universe uR uS uT uA u₄ variable {R : Type uR} [Semiring R] variable {S : Type uS} [CommSemiring S] variable {T : Type uT} variable {A : Type uA} [Semiring A] [Algebra S A] namespace RingCon instance (c : RingCon A) : Algebra S c.Quotient where smul := (· • ·) toRingHom := c.mk'.comp (algebraMap S A) commutes' _ := Quotient.ind' fun _ ↦ congr_arg Quotient.mk'' <| Algebra.commutes _ _ smul_def' _ := Quotient.ind' fun _ ↦ congr_arg Quotient.mk'' <| Algebra.smul_def _ _ @[simp, norm_cast] theorem coe_algebraMap (c : RingCon A) (s : S) : (algebraMap S A s : c.Quotient) = algebraMap S _ s := rfl end RingCon namespace RingQuot /-- Given an arbitrary relation `r` on a ring, we strengthen it to a relation `Rel r`, such that the equivalence relation generated by `Rel r` has `x ~ y` if and only if `x - y` is in the ideal generated by elements `a - b` such that `r a b`. -/ inductive Rel (r : R → R → Prop) : R → R → Prop | of ⦃x y : R⦄ (h : r x y) : Rel r x y | add_left ⦃a b c⦄ : Rel r a b → Rel r (a + c) (b + c) | mul_left ⦃a b c⦄ : Rel r a b → Rel r (a * c) (b * c) | mul_right ⦃a b c⦄ : Rel r b c → Rel r (a * b) (a * c) theorem Rel.add_right {r : R → R → Prop} ⦃a b c : R⦄ (h : Rel r b c) : Rel r (a + b) (a + c) := by rw [add_comm a b, add_comm a c] exact Rel.add_left h theorem Rel.neg {R : Type uR} [Ring R] {r : R → R → Prop} ⦃a b : R⦄ (h : Rel r a b) : Rel r (-a) (-b) := by simp only [neg_eq_neg_one_mul a, neg_eq_neg_one_mul b, Rel.mul_right h] theorem Rel.sub_left {R : Type uR} [Ring R] {r : R → R → Prop} ⦃a b c : R⦄ (h : Rel r a b) : Rel r (a - c) (b - c) := by simp only [sub_eq_add_neg, h.add_left] theorem Rel.sub_right {R : Type uR} [Ring R] {r : R → R → Prop} ⦃a b c : R⦄ (h : Rel r b c) : Rel r (a - b) (a - c) := by simp only [sub_eq_add_neg, h.neg.add_right] theorem Rel.smul {r : A → A → Prop} (k : S) ⦃a b : A⦄ (h : Rel r a b) : Rel r (k • a) (k • b) := by simp only [Algebra.smul_def, Rel.mul_right h] /-- `EqvGen (RingQuot.Rel r)` is a ring congruence. -/ def ringCon (r : R → R → Prop) : RingCon R where r := EqvGen (Rel r) iseqv := EqvGen.is_equivalence _ add' {a b c d} hab hcd := by induction hab generalizing c d with | rel _ _ hab => refine (EqvGen.rel _ _ hab.add_left).trans _ _ _ ?_ induction hcd with | rel _ _ hcd => exact EqvGen.rel _ _ hcd.add_right | refl => exact EqvGen.refl _ | symm _ _ _ h => exact h.symm _ _ | trans _ _ _ _ _ h h' => exact h.trans _ _ _ h' | refl => induction hcd with | rel _ _ hcd => exact EqvGen.rel _ _ hcd.add_right | refl => exact EqvGen.refl _ | symm _ _ _ h => exact h.symm _ _ | trans _ _ _ _ _ h h' => exact h.trans _ _ _ h' | symm x y _ hxy => exact (hxy hcd.symm).symm | trans x y z _ _ h h' => exact (h hcd).trans _ _ _ (h' <| EqvGen.refl _) mul' {a b c d} hab hcd := by induction hab generalizing c d with | rel _ _ hab => refine (EqvGen.rel _ _ hab.mul_left).trans _ _ _ ?_ induction hcd with | rel _ _ hcd => exact EqvGen.rel _ _ hcd.mul_right | refl => exact EqvGen.refl _ | symm _ _ _ h => exact h.symm _ _ | trans _ _ _ _ _ h h' => exact h.trans _ _ _ h' | refl => induction hcd with | rel _ _ hcd => exact EqvGen.rel _ _ hcd.mul_right | refl => exact EqvGen.refl _ | symm _ _ _ h => exact h.symm _ _ | trans _ _ _ _ _ h h' => exact h.trans _ _ _ h' | symm x y _ hxy => exact (hxy hcd.symm).symm | trans x y z _ _ h h' => exact (h hcd).trans _ _ _ (h' <| EqvGen.refl _) theorem eqvGen_rel_eq (r : R → R → Prop) : EqvGen (Rel r) = RingConGen.Rel r := by ext x₁ x₂ constructor · intro h induction h with | rel _ _ h => induction h with | of => exact RingConGen.Rel.of _ _ ‹_› | add_left _ h => exact h.add (RingConGen.Rel.refl _) | mul_left _ h => exact h.mul (RingConGen.Rel.refl _) | mul_right _ h => exact (RingConGen.Rel.refl _).mul h | refl => exact RingConGen.Rel.refl _ | symm => exact RingConGen.Rel.symm ‹_› | trans => exact RingConGen.Rel.trans ‹_› ‹_› · intro h induction h with | of => exact EqvGen.rel _ _ (Rel.of ‹_›) | refl => exact (RingQuot.ringCon r).refl _ | symm => exact (RingQuot.ringCon r).symm ‹_› | trans => exact (RingQuot.ringCon r).trans ‹_› ‹_› | add => exact (RingQuot.ringCon r).add ‹_› ‹_› | mul => exact (RingQuot.ringCon r).mul ‹_› ‹_› end RingQuot /-- The quotient of a ring by an arbitrary relation. -/ structure RingQuot (r : R → R → Prop) where toQuot : Quot (RingQuot.Rel r) namespace RingQuot variable (r : R → R → Prop) -- can't be irreducible, causes diamonds in ℕ-algebras private def natCast (n : ℕ) : RingQuot r := ⟨Quot.mk _ n⟩ private irreducible_def zero : RingQuot r := ⟨Quot.mk _ 0⟩ private irreducible_def one : RingQuot r := ⟨Quot.mk _ 1⟩ private irreducible_def add : RingQuot r → RingQuot r → RingQuot r | ⟨a⟩, ⟨b⟩ => ⟨Quot.map₂ (· + ·) Rel.add_right Rel.add_left a b⟩ private irreducible_def mul : RingQuot r → RingQuot r → RingQuot r | ⟨a⟩, ⟨b⟩ => ⟨Quot.map₂ (· * ·) Rel.mul_right Rel.mul_left a b⟩ private irreducible_def neg {R : Type uR} [Ring R] (r : R → R → Prop) : RingQuot r → RingQuot r | ⟨a⟩ => ⟨Quot.map (fun a ↦ -a) Rel.neg a⟩ private irreducible_def sub {R : Type uR} [Ring R] (r : R → R → Prop) : RingQuot r → RingQuot r → RingQuot r | ⟨a⟩, ⟨b⟩ => ⟨Quot.map₂ Sub.sub Rel.sub_right Rel.sub_left a b⟩ private irreducible_def npow (n : ℕ) : RingQuot r → RingQuot r | ⟨a⟩ => ⟨Quot.lift (fun a ↦ Quot.mk (RingQuot.Rel r) (a ^ n)) (fun a b (h : Rel r a b) ↦ by -- note we can't define a `Rel.pow` as `Rel` isn't reflexive so `Rel r 1 1` isn't true dsimp only induction n with | zero => rw [pow_zero, pow_zero] | succ n ih => rw [pow_succ, pow_succ] -- Porting note: -- `simpa [mul_def] using congr_arg₂ (fun x y ↦ mul r ⟨x⟩ ⟨y⟩) (Quot.sound h) ih` -- mysteriously doesn't work have := congr_arg₂ (fun x y ↦ mul r ⟨x⟩ ⟨y⟩) ih (Quot.sound h) dsimp only at this simp? [mul_def] at this says simp only [mul_def, Quot.map₂_mk, mk.injEq] at this exact this) a⟩ -- note: this cannot be irreducible, as otherwise diamonds don't commute. private def smul [Algebra S R] (n : S) : RingQuot r → RingQuot r | ⟨a⟩ => ⟨Quot.map (fun a ↦ n • a) (Rel.smul n) a⟩ instance : NatCast (RingQuot r) := ⟨natCast r⟩ instance : Zero (RingQuot r) := ⟨zero r⟩ instance : One (RingQuot r) := ⟨one r⟩ instance : Add (RingQuot r) := ⟨add r⟩ instance : Mul (RingQuot r) := ⟨mul r⟩ instance : NatPow (RingQuot r) := ⟨fun x n ↦ npow r n x⟩ instance {R : Type uR} [Ring R] (r : R → R → Prop) : Neg (RingQuot r) := ⟨neg r⟩ instance {R : Type uR} [Ring R] (r : R → R → Prop) : Sub (RingQuot r) := ⟨sub r⟩ instance [Algebra S R] : SMul S (RingQuot r) := ⟨smul r⟩ theorem zero_quot : (⟨Quot.mk _ 0⟩ : RingQuot r) = 0 := show _ = zero r by rw [zero_def] theorem one_quot : (⟨Quot.mk _ 1⟩ : RingQuot r) = 1 := show _ = one r by rw [one_def] theorem add_quot {a b} : (⟨Quot.mk _ a⟩ + ⟨Quot.mk _ b⟩ : RingQuot r) = ⟨Quot.mk _ (a + b)⟩ := by show add r _ _ = _ rw [add_def] rfl theorem mul_quot {a b} : (⟨Quot.mk _ a⟩ * ⟨Quot.mk _ b⟩ : RingQuot r) = ⟨Quot.mk _ (a * b)⟩ := by show mul r _ _ = _ rw [mul_def] rfl theorem pow_quot {a} {n : ℕ} : (⟨Quot.mk _ a⟩ ^ n : RingQuot r) = ⟨Quot.mk _ (a ^ n)⟩ := by show npow r _ _ = _ rw [npow_def] theorem neg_quot {R : Type uR} [Ring R] (r : R → R → Prop) {a} : (-⟨Quot.mk _ a⟩ : RingQuot r) = ⟨Quot.mk _ (-a)⟩ := by show neg r _ = _ rw [neg_def] rfl theorem sub_quot {R : Type uR} [Ring R] (r : R → R → Prop) {a b} : (⟨Quot.mk _ a⟩ - ⟨Quot.mk _ b⟩ : RingQuot r) = ⟨Quot.mk _ (a - b)⟩ := by show sub r _ _ = _ rw [sub_def] rfl theorem smul_quot [Algebra S R] {n : S} {a : R} : (n • ⟨Quot.mk _ a⟩ : RingQuot r) = ⟨Quot.mk _ (n • a)⟩ := by show smul r _ _ = _ rw [smul] rfl instance instIsScalarTower [CommSemiring T] [SMul S T] [Algebra S R] [Algebra T R] [IsScalarTower S T R] : IsScalarTower S T (RingQuot r) := ⟨fun s t ⟨a⟩ => Quot.inductionOn a fun a' => by simp only [RingQuot.smul_quot, smul_assoc]⟩ instance instSMulCommClass [CommSemiring T] [Algebra S R] [Algebra T R] [SMulCommClass S T R] : SMulCommClass S T (RingQuot r) := ⟨fun s t ⟨a⟩ => Quot.inductionOn a fun a' => by simp only [RingQuot.smul_quot, smul_comm]⟩ instance instAddCommMonoid (r : R → R → Prop) : AddCommMonoid (RingQuot r) where add := (· + ·) zero := 0 add_assoc := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp only [add_quot, add_assoc] zero_add := by rintro ⟨⟨⟩⟩ simp [add_quot, ← zero_quot, zero_add] add_zero := by rintro ⟨⟨⟩⟩ simp only [add_quot, ← zero_quot, add_zero] add_comm := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp only [add_quot, add_comm] nsmul := (· • ·) nsmul_zero := by rintro ⟨⟨⟩⟩ simp only [smul_quot, zero_smul, zero_quot] nsmul_succ := by rintro n ⟨⟨⟩⟩ simp only [smul_quot, nsmul_eq_mul, Nat.cast_add, Nat.cast_one, add_mul, one_mul, add_comm, add_quot] instance instMonoidWithZero (r : R → R → Prop) : MonoidWithZero (RingQuot r) where mul_assoc := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp only [mul_quot, mul_assoc] one_mul := by rintro ⟨⟨⟩⟩ simp only [mul_quot, ← one_quot, one_mul] mul_one := by rintro ⟨⟨⟩⟩ simp only [mul_quot, ← one_quot, mul_one] zero_mul := by rintro ⟨⟨⟩⟩ simp only [mul_quot, ← zero_quot, zero_mul] mul_zero := by rintro ⟨⟨⟩⟩ simp only [mul_quot, ← zero_quot, mul_zero] npow n x := x ^ n npow_zero := by rintro ⟨⟨⟩⟩ simp only [pow_quot, ← one_quot, pow_zero] npow_succ := by rintro n ⟨⟨⟩⟩ simp only [pow_quot, mul_quot, pow_succ] instance instSemiring (r : R → R → Prop) : Semiring (RingQuot r) where natCast := natCast r natCast_zero := by simp [Nat.cast, natCast, ← zero_quot] natCast_succ := by simp [Nat.cast, natCast, ← one_quot, add_quot] left_distrib := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp only [mul_quot, add_quot, left_distrib] right_distrib := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp only [mul_quot, add_quot, right_distrib] nsmul := (· • ·) nsmul_zero := by rintro ⟨⟨⟩⟩ simp only [smul_quot, zero_smul, zero_quot] nsmul_succ := by rintro n ⟨⟨⟩⟩ simp only [smul_quot, nsmul_eq_mul, Nat.cast_add, Nat.cast_one, add_mul, one_mul, add_comm, add_quot] __ := instAddCommMonoid r __ := instMonoidWithZero r -- can't be irreducible, causes diamonds in ℤ-algebras private def intCast {R : Type uR} [Ring R] (r : R → R → Prop) (z : ℤ) : RingQuot r := ⟨Quot.mk _ z⟩ instance instRing {R : Type uR} [Ring R] (r : R → R → Prop) : Ring (RingQuot r) := { RingQuot.instSemiring r with neg := Neg.neg add_left_neg := by rintro ⟨⟨⟩⟩ simp [neg_quot, add_quot, ← zero_quot] sub := Sub.sub sub_eq_add_neg := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp [neg_quot, sub_quot, add_quot, sub_eq_add_neg] zsmul := (· • ·) zsmul_zero' := by rintro ⟨⟨⟩⟩ simp [smul_quot, ← zero_quot] zsmul_succ' := by rintro n ⟨⟨⟩⟩ simp [smul_quot, add_quot, add_mul, add_comm] zsmul_neg' := by rintro n ⟨⟨⟩⟩ simp [smul_quot, neg_quot, add_mul] intCast := intCast r intCast_ofNat := fun n => congrArg RingQuot.mk <| by exact congrArg (Quot.mk _) (Int.cast_natCast _) intCast_negSucc := fun n => congrArg RingQuot.mk <| by simp_rw [neg_def] exact congrArg (Quot.mk _) (Int.cast_negSucc n) } instance instCommSemiring {R : Type uR} [CommSemiring R] (r : R → R → Prop) : CommSemiring (RingQuot r) := { RingQuot.instSemiring r with mul_comm := by rintro ⟨⟨⟩⟩ ⟨⟨⟩⟩ simp [mul_quot, mul_comm] } instance {R : Type uR} [CommRing R] (r : R → R → Prop) : CommRing (RingQuot r) := { RingQuot.instCommSemiring r, RingQuot.instRing r with } instance instInhabited (r : R → R → Prop) : Inhabited (RingQuot r) := ⟨0⟩ instance instAlgebra [Algebra S R] (r : R → R → Prop) : Algebra S (RingQuot r) where smul := (· • ·) toFun r := ⟨Quot.mk _ (algebraMap S R r)⟩ map_one' := by simp [← one_quot] map_mul' := by simp [mul_quot] map_zero' := by simp [← zero_quot] map_add' := by simp [add_quot] commutes' r := by rintro ⟨⟨a⟩⟩ simp [Algebra.commutes, mul_quot] smul_def' r := by rintro ⟨⟨a⟩⟩ simp [smul_quot, Algebra.smul_def, mul_quot] /-- The quotient map from a ring to its quotient, as a homomorphism of rings. -/ irreducible_def mkRingHom (r : R → R → Prop) : R →+* RingQuot r := { toFun := fun x ↦ ⟨Quot.mk _ x⟩ map_one' := by simp [← one_quot] map_mul' := by simp [mul_quot] map_zero' := by simp [← zero_quot] map_add' := by simp [add_quot] } theorem mkRingHom_rel {r : R → R → Prop} {x y : R} (w : r x y) : mkRingHom r x = mkRingHom r y := by simp [mkRingHom_def, Quot.sound (Rel.of w)] theorem mkRingHom_surjective (r : R → R → Prop) : Function.Surjective (mkRingHom r) := by simp only [mkRingHom_def, RingHom.coe_mk, MonoidHom.coe_mk, OneHom.coe_mk] rintro ⟨⟨⟩⟩ simp @[ext 1100] theorem ringQuot_ext [Semiring T] {r : R → R → Prop} (f g : RingQuot r →+* T) (w : f.comp (mkRingHom r) = g.comp (mkRingHom r)) : f = g := by ext x rcases mkRingHom_surjective r x with ⟨x, rfl⟩ exact (RingHom.congr_fun w x : _) variable [Semiring T] irreducible_def preLift {r : R → R → Prop} {f : R →+* T} (h : ∀ ⦃x y⦄, r x y → f x = f y) : RingQuot r →+* T := { toFun := fun x ↦ Quot.lift f (by rintro _ _ r induction r with | of r => exact h r | add_left _ r' => rw [map_add, map_add, r'] | mul_left _ r' => rw [map_mul, map_mul, r'] | mul_right _ r' => rw [map_mul, map_mul, r']) x.toQuot map_zero' := by simp only [← zero_quot, f.map_zero] map_add' := by rintro ⟨⟨x⟩⟩ ⟨⟨y⟩⟩ simp only [add_quot, f.map_add x y] map_one' := by simp only [← one_quot, f.map_one] map_mul' := by rintro ⟨⟨x⟩⟩ ⟨⟨y⟩⟩ simp only [mul_quot, f.map_mul x y] } /-- Any ring homomorphism `f : R →+* T` which respects a relation `r : R → R → Prop` factors uniquely through a morphism `RingQuot r →+* T`. -/ irreducible_def lift {r : R → R → Prop} : { f : R →+* T // ∀ ⦃x y⦄, r x y → f x = f y } ≃ (RingQuot r →+* T) := { toFun := fun f ↦ preLift f.prop invFun := fun F ↦ ⟨F.comp (mkRingHom r), fun x y h ↦ congr_arg F (mkRingHom_rel h)⟩ left_inv := fun f ↦ by ext simp only [preLift_def, mkRingHom_def, RingHom.coe_comp, RingHom.coe_mk, MonoidHom.coe_mk, OneHom.coe_mk, Function.comp_apply] right_inv := fun F ↦ by simp only [preLift_def] ext simp only [mkRingHom_def, RingHom.coe_comp, RingHom.coe_mk, MonoidHom.coe_mk, OneHom.coe_mk, Function.comp_apply, forall_const] } @[simp] theorem lift_mkRingHom_apply (f : R →+* T) {r : R → R → Prop} (w : ∀ ⦃x y⦄, r x y → f x = f y) (x) : lift ⟨f, w⟩ (mkRingHom r x) = f x := by simp_rw [lift_def, preLift_def, mkRingHom_def] rfl -- note this is essentially `lift.symm_apply_eq.mp h` theorem lift_unique (f : R →+* T) {r : R → R → Prop} (w : ∀ ⦃x y⦄, r x y → f x = f y) (g : RingQuot r →+* T) (h : g.comp (mkRingHom r) = f) : g = lift ⟨f, w⟩ := by ext simp [h] theorem eq_lift_comp_mkRingHom {r : R → R → Prop} (f : RingQuot r →+* T) : f = lift ⟨f.comp (mkRingHom r), fun x y h ↦ congr_arg f (mkRingHom_rel h)⟩ := by conv_lhs => rw [← lift.apply_symm_apply f] rw [lift_def] rfl section CommRing /-! We now verify that in the case of a commutative ring, the `RingQuot` construction agrees with the quotient by the appropriate ideal. -/ variable {B : Type uR} [CommRing B] /-- The universal ring homomorphism from `RingQuot r` to `B ⧸ Ideal.ofRel r`. -/ def ringQuotToIdealQuotient (r : B → B → Prop) : RingQuot r →+* B ⧸ Ideal.ofRel r := lift ⟨Ideal.Quotient.mk (Ideal.ofRel r), fun x y h ↦ Ideal.Quotient.eq.2 <| Submodule.mem_sInf.mpr fun _ w ↦ w ⟨x, y, h, sub_add_cancel x y⟩⟩ @[simp] theorem ringQuotToIdealQuotient_apply (r : B → B → Prop) (x : B) : ringQuotToIdealQuotient r (mkRingHom r x) = Ideal.Quotient.mk (Ideal.ofRel r) x := by simp_rw [ringQuotToIdealQuotient, lift_def, preLift_def, mkRingHom_def] rfl /-- The universal ring homomorphism from `B ⧸ Ideal.ofRel r` to `RingQuot r`. -/ def idealQuotientToRingQuot (r : B → B → Prop) : B ⧸ Ideal.ofRel r →+* RingQuot r := Ideal.Quotient.lift (Ideal.ofRel r) (mkRingHom r) (by refine fun x h ↦ Submodule.span_induction h ?_ ?_ ?_ ?_ · rintro y ⟨a, b, h, su⟩ symm at su rw [← sub_eq_iff_eq_add] at su rw [← su, RingHom.map_sub, mkRingHom_rel h, sub_self] · simp · intro a b ha hb simp [ha, hb] · intro a x hx simp [hx]) @[simp] theorem idealQuotientToRingQuot_apply (r : B → B → Prop) (x : B) : idealQuotientToRingQuot r (Ideal.Quotient.mk _ x) = mkRingHom r x := rfl /-- The ring equivalence between `RingQuot r` and `(Ideal.ofRel r).quotient` -/ def ringQuotEquivIdealQuotient (r : B → B → Prop) : RingQuot r ≃+* B ⧸ Ideal.ofRel r := RingEquiv.ofHomInv (ringQuotToIdealQuotient r) (idealQuotientToRingQuot r) (by ext x simp_rw [ringQuotToIdealQuotient, lift_def, preLift_def, mkRingHom_def] change mkRingHom r x = _ rw [mkRingHom_def] rfl) (by ext x simp_rw [ringQuotToIdealQuotient, lift_def, preLift_def, mkRingHom_def] change Quot.lift _ _ ((mkRingHom r) x).toQuot = _ rw [mkRingHom_def] rfl) end CommRing section Algebra variable (S) /-- The quotient map from an `S`-algebra to its quotient, as a homomorphism of `S`-algebras. -/ irreducible_def mkAlgHom (s : A → A → Prop) : A →ₐ[S] RingQuot s := { mkRingHom s with commutes' := fun _ ↦ by simp [mkRingHom_def]; rfl } @[simp] theorem mkAlgHom_coe (s : A → A → Prop) : (mkAlgHom S s : A →+* RingQuot s) = mkRingHom s := by simp_rw [mkAlgHom_def, mkRingHom_def] rfl theorem mkAlgHom_rel {s : A → A → Prop} {x y : A} (w : s x y) : mkAlgHom S s x = mkAlgHom S s y := by simp [mkAlgHom_def, mkRingHom_def, Quot.sound (Rel.of w)] theorem mkAlgHom_surjective (s : A → A → Prop) : Function.Surjective (mkAlgHom S s) := by suffices Function.Surjective fun x ↦ (⟨.mk (Rel s) x⟩ : RingQuot s) by simpa [mkAlgHom_def, mkRingHom_def] rintro ⟨⟨a⟩⟩ use a variable {B : Type u₄} [Semiring B] [Algebra S B] @[ext 1100] theorem ringQuot_ext' {s : A → A → Prop} (f g : RingQuot s →ₐ[S] B) (w : f.comp (mkAlgHom S s) = g.comp (mkAlgHom S s)) : f = g := by ext x rcases mkAlgHom_surjective S s x with ⟨x, rfl⟩ exact AlgHom.congr_fun w x irreducible_def preLiftAlgHom {s : A → A → Prop} {f : A →ₐ[S] B} (h : ∀ ⦃x y⦄, s x y → f x = f y) : RingQuot s →ₐ[S] B := { toFun := fun x ↦ Quot.lift f (by rintro _ _ r induction r with | of r => exact h r | add_left _ r' => simp only [map_add, r'] | mul_left _ r' => simp only [map_mul, r'] | mul_right _ r' => simp only [map_mul, r']) x.toQuot map_zero' := by simp only [← zero_quot, map_zero] map_add' := by rintro ⟨⟨x⟩⟩ ⟨⟨y⟩⟩ simp only [add_quot, map_add _ x y] map_one' := by simp only [← one_quot, map_one] map_mul' := by rintro ⟨⟨x⟩⟩ ⟨⟨y⟩⟩ simp only [mul_quot, map_mul _ x y] commutes' := by rintro x simp [← one_quot, smul_quot, Algebra.algebraMap_eq_smul_one] } /-- Any `S`-algebra homomorphism `f : A →ₐ[S] B` which respects a relation `s : A → A → Prop` factors uniquely through a morphism `RingQuot s →ₐ[S] B`. -/ irreducible_def liftAlgHom {s : A → A → Prop} : { f : A →ₐ[S] B // ∀ ⦃x y⦄, s x y → f x = f y } ≃ (RingQuot s →ₐ[S] B) := { toFun := fun f' ↦ preLiftAlgHom _ f'.prop invFun := fun F ↦ ⟨F.comp (mkAlgHom S s), fun _ _ h ↦ congr_arg F (mkAlgHom_rel S h)⟩ left_inv := fun f ↦ by ext simp only [preLiftAlgHom_def, mkAlgHom_def, mkRingHom_def, RingHom.toMonoidHom_eq_coe, RingHom.coe_monoidHom_mk, AlgHom.coe_comp, AlgHom.coe_mk, RingHom.coe_mk, MonoidHom.coe_mk, OneHom.coe_mk, Function.comp_apply] right_inv := fun F ↦ by ext simp only [preLiftAlgHom_def, mkAlgHom_def, mkRingHom_def, RingHom.toMonoidHom_eq_coe, RingHom.coe_monoidHom_mk, AlgHom.coe_comp, AlgHom.coe_mk, RingHom.coe_mk, MonoidHom.coe_mk, OneHom.coe_mk, Function.comp_apply] } @[simp] theorem liftAlgHom_mkAlgHom_apply (f : A →ₐ[S] B) {s : A → A → Prop} (w : ∀ ⦃x y⦄, s x y → f x = f y) (x) : (liftAlgHom S ⟨f, w⟩) ((mkAlgHom S s) x) = f x := by simp_rw [liftAlgHom_def, preLiftAlgHom_def, mkAlgHom_def, mkRingHom_def] rfl -- note this is essentially `(liftAlgHom S).symm_apply_eq.mp h` theorem liftAlgHom_unique (f : A →ₐ[S] B) {s : A → A → Prop} (w : ∀ ⦃x y⦄, s x y → f x = f y) (g : RingQuot s →ₐ[S] B) (h : g.comp (mkAlgHom S s) = f) : g = liftAlgHom S ⟨f, w⟩ := by ext simp [h] theorem eq_liftAlgHom_comp_mkAlgHom {s : A → A → Prop} (f : RingQuot s →ₐ[S] B) : f = liftAlgHom S ⟨f.comp (mkAlgHom S s), fun x y h ↦ congr_arg f (mkAlgHom_rel S h)⟩ := by conv_lhs => rw [← (liftAlgHom S).apply_symm_apply f] rw [liftAlgHom] rfl end Algebra end RingQuot
Algebra\SMulWithZero.lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Group.Action.Opposite import Mathlib.Algebra.Group.Action.Prod import Mathlib.Algebra.GroupWithZero.Action.Defs import Mathlib.Algebra.GroupWithZero.Opposite import Mathlib.Algebra.GroupWithZero.Prod import Mathlib.Algebra.Ring.Defs /-! # Introduce `SMulWithZero` In analogy with the usual monoid action on a Type `M`, we introduce an action of a `MonoidWithZero` on a Type with `0`. In particular, for Types `R` and `M`, both containing `0`, we define `SMulWithZero R M` to be the typeclass where the products `r • 0` and `0 • m` vanish for all `r : R` and all `m : M`. Moreover, in the case in which `R` is a `MonoidWithZero`, we introduce the typeclass `MulActionWithZero R M`, mimicking group actions and having an absorbing `0` in `R`. Thus, the action is required to be compatible with * the unit of the monoid, acting as the identity; * the zero of the `MonoidWithZero`, acting as zero; * associativity of the monoid. We also add an `instance`: * any `MonoidWithZero` has a `MulActionWithZero R R` acting on itself. ## Main declarations * `smulMonoidWithZeroHom`: Scalar multiplication bundled as a morphism of monoids with zero. -/ variable {R R' M M' : Type*} section Zero variable (R M) /-- `SMulWithZero` is a class consisting of a Type `R` with `0 ∈ R` and a scalar multiplication of `R` on a Type `M` with `0`, such that the equality `r • m = 0` holds if at least one among `r` or `m` equals `0`. -/ class SMulWithZero [Zero R] [Zero M] extends SMulZeroClass R M where /-- Scalar multiplication by the scalar `0` is `0`. -/ zero_smul : ∀ m : M, (0 : R) • m = 0 instance MulZeroClass.toSMulWithZero [MulZeroClass R] : SMulWithZero R R where smul := (· * ·) smul_zero := mul_zero zero_smul := zero_mul /-- Like `MulZeroClass.toSMulWithZero`, but multiplies on the right. -/ instance MulZeroClass.toOppositeSMulWithZero [MulZeroClass R] : SMulWithZero Rᵐᵒᵖ R where smul := (· • ·) smul_zero _ := zero_mul _ zero_smul := mul_zero variable {M} [Zero R] [Zero M] [SMulWithZero R M] @[simp] theorem zero_smul (m : M) : (0 : R) • m = 0 := SMulWithZero.zero_smul m variable {R} {a : R} {b : M} lemma smul_eq_zero_of_left (h : a = 0) (b : M) : a • b = 0 := h.symm ▸ zero_smul _ b lemma left_ne_zero_of_smul : a • b ≠ 0 → a ≠ 0 := mt fun h ↦ smul_eq_zero_of_left h b variable [Zero R'] [Zero M'] [SMul R M'] /-- Pullback a `SMulWithZero` structure along an injective zero-preserving homomorphism. See note [reducible non-instances]. -/ protected abbrev Function.Injective.smulWithZero (f : ZeroHom M' M) (hf : Function.Injective f) (smul : ∀ (a : R) (b), f (a • b) = a • f b) : SMulWithZero R M' where smul := (· • ·) zero_smul a := hf <| by simp [smul] smul_zero a := hf <| by simp [smul] /-- Pushforward a `SMulWithZero` structure along a surjective zero-preserving homomorphism. See note [reducible non-instances]. -/ protected abbrev Function.Surjective.smulWithZero (f : ZeroHom M M') (hf : Function.Surjective f) (smul : ∀ (a : R) (b), f (a • b) = a • f b) : SMulWithZero R M' where smul := (· • ·) zero_smul m := by rcases hf m with ⟨x, rfl⟩ simp [← smul] smul_zero c := by rw [← f.map_zero, ← smul, smul_zero] variable (M) /-- Compose a `SMulWithZero` with a `ZeroHom`, with action `f r' • m` -/ def SMulWithZero.compHom (f : ZeroHom R' R) : SMulWithZero R' M where smul := (f · • ·) smul_zero m := smul_zero (f m) zero_smul m := by show (f 0) • m = 0; rw [map_zero, zero_smul] end Zero instance AddMonoid.natSMulWithZero [AddMonoid M] : SMulWithZero ℕ M where smul_zero := _root_.nsmul_zero zero_smul := zero_nsmul instance AddGroup.intSMulWithZero [AddGroup M] : SMulWithZero ℤ M where smul_zero := zsmul_zero zero_smul := zero_zsmul section MonoidWithZero variable [MonoidWithZero R] [MonoidWithZero R'] [Zero M] variable (R M) /-- An action of a monoid with zero `R` on a Type `M`, also with `0`, extends `MulAction` and is compatible with `0` (both in `R` and in `M`), with `1 ∈ R`, and with associativity of multiplication on the monoid `M`. -/ class MulActionWithZero extends MulAction R M where -- these fields are copied from `SMulWithZero`, as `extends` behaves poorly /-- Scalar multiplication by any element send `0` to `0`. -/ smul_zero : ∀ r : R, r • (0 : M) = 0 /-- Scalar multiplication by the scalar `0` is `0`. -/ zero_smul : ∀ m : M, (0 : R) • m = 0 -- see Note [lower instance priority] instance (priority := 100) MulActionWithZero.toSMulWithZero [m : MulActionWithZero R M] : SMulWithZero R M := { m with } /-- See also `Semiring.toModule` -/ instance MonoidWithZero.toMulActionWithZero : MulActionWithZero R R := { MulZeroClass.toSMulWithZero R, Monoid.toMulAction R with } /-- Like `MonoidWithZero.toMulActionWithZero`, but multiplies on the right. See also `Semiring.toOppositeModule` -/ instance MonoidWithZero.toOppositeMulActionWithZero : MulActionWithZero Rᵐᵒᵖ R := { MulZeroClass.toOppositeSMulWithZero R, Monoid.toOppositeMulAction with } protected lemma MulActionWithZero.subsingleton [MulActionWithZero R M] [Subsingleton R] : Subsingleton M := ⟨fun x y => by rw [← one_smul R x, ← one_smul R y, Subsingleton.elim (1 : R) 0, zero_smul, zero_smul]⟩ protected lemma MulActionWithZero.nontrivial [MulActionWithZero R M] [Nontrivial M] : Nontrivial R := (subsingleton_or_nontrivial R).resolve_left fun _ => not_subsingleton M <| MulActionWithZero.subsingleton R M variable {R M} variable [MulActionWithZero R M] [Zero M'] [SMul R M'] (p : Prop) [Decidable p] lemma ite_zero_smul (a : R) (b : M) : (if p then a else 0 : R) • b = if p then a • b else 0 := by rw [ite_smul, zero_smul] lemma boole_smul (a : M) : (if p then 1 else 0 : R) • a = if p then a else 0 := by simp lemma Pi.single_apply_smul {ι : Type*} [DecidableEq ι] (x : M) (i j : ι) : (Pi.single i 1 : ι → R) j • x = (Pi.single i x : ι → M) j := by rw [single_apply, ite_smul, one_smul, zero_smul, single_apply] /-- Pullback a `MulActionWithZero` structure along an injective zero-preserving homomorphism. See note [reducible non-instances]. -/ protected abbrev Function.Injective.mulActionWithZero (f : ZeroHom M' M) (hf : Function.Injective f) (smul : ∀ (a : R) (b), f (a • b) = a • f b) : MulActionWithZero R M' := { hf.mulAction f smul, hf.smulWithZero f smul with } /-- Pushforward a `MulActionWithZero` structure along a surjective zero-preserving homomorphism. See note [reducible non-instances]. -/ protected abbrev Function.Surjective.mulActionWithZero (f : ZeroHom M M') (hf : Function.Surjective f) (smul : ∀ (a : R) (b), f (a • b) = a • f b) : MulActionWithZero R M' := { hf.mulAction f smul, hf.smulWithZero f smul with } variable (M) /-- Compose a `MulActionWithZero` with a `MonoidWithZeroHom`, with action `f r' • m` -/ def MulActionWithZero.compHom (f : R' →*₀ R) : MulActionWithZero R' M := { SMulWithZero.compHom M f.toZeroHom with mul_smul := fun r s m => by show f (r * s) • m = (f r) • (f s) • m; simp [mul_smul] one_smul := fun m => by show (f 1) • m = m; simp } end MonoidWithZero section GroupWithZero variable {α β : Type*} [GroupWithZero α] [GroupWithZero β] [MulActionWithZero α β] theorem smul_inv₀ [SMulCommClass α β β] [IsScalarTower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by obtain rfl | hc := eq_or_ne c 0 · simp only [inv_zero, zero_smul] obtain rfl | hx := eq_or_ne x 0 · simp only [inv_zero, smul_zero] · refine inv_eq_of_mul_eq_one_left ?_ rw [smul_mul_smul, inv_mul_cancel hc, inv_mul_cancel hx, one_smul] end GroupWithZero /-- Scalar multiplication as a monoid homomorphism with zero. -/ @[simps] def smulMonoidWithZeroHom {α β : Type*} [MonoidWithZero α] [MulZeroOneClass β] [MulActionWithZero α β] [IsScalarTower α β β] [SMulCommClass α β β] : α × β →*₀ β := { smulMonoidHom with map_zero' := smul_zero _ } -- This instance seems a bit incongruous in this file, but `#find_home!` told me to put it here. instance NonUnitalNonAssocSemiring.toDistribSMul [NonUnitalNonAssocSemiring R] : DistribSMul R R where smul_add := mul_add
Algebra\Symmetrized.lean
/- Copyright (c) 2021 Christopher Hoskin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Christopher Hoskin -/ import Mathlib.Algebra.Jordan.Basic import Mathlib.Algebra.Module.Defs /-! # Symmetrized algebra A commutative multiplication on a real or complex space can be constructed from any multiplication by "symmetrization" i.e $$ a \circ b = \frac{1}{2}(ab + ba) $$ We provide the symmetrized version of a type `α` as `SymAlg α`, with notation `αˢʸᵐ`. ## Implementation notes The approach taken here is inspired by `Mathlib/Algebra/Opposites.lean`. We use Oxford Spellings (IETF en-GB-oxendict). ## References * [Hanche-Olsen and Størmer, Jordan Operator Algebras][hancheolsenstormer1984] -/ open Function /-- The symmetrized algebra has the same underlying space as the original algebra. -/ def SymAlg (α : Type*) : Type _ := α postfix:max "ˢʸᵐ" => SymAlg namespace SymAlg variable {α : Type*} /-- The element of `SymAlg α` that represents `a : α`. -/ @[match_pattern] def sym : α ≃ αˢʸᵐ := Equiv.refl _ /-- The element of `α` represented by `x : αˢʸᵐ`. -/ -- Porting note (kmill): `pp_nodot` has no affect here -- unless RFC lean4#1910 leads to dot notation for CoeFun @[pp_nodot] def unsym : αˢʸᵐ ≃ α := Equiv.refl _ @[simp] theorem unsym_sym (a : α) : unsym (sym a) = a := rfl @[simp] theorem sym_unsym (a : α) : sym (unsym a) = a := rfl @[simp] theorem sym_comp_unsym : (sym : α → αˢʸᵐ) ∘ unsym = id := rfl @[simp] theorem unsym_comp_sym : (unsym : αˢʸᵐ → α) ∘ sym = id := rfl @[simp] theorem sym_symm : (@sym α).symm = unsym := rfl @[simp] theorem unsym_symm : (@unsym α).symm = sym := rfl theorem sym_bijective : Bijective (sym : α → αˢʸᵐ) := sym.bijective theorem unsym_bijective : Bijective (unsym : αˢʸᵐ → α) := unsym.symm.bijective theorem sym_injective : Injective (sym : α → αˢʸᵐ) := sym.injective theorem sym_surjective : Surjective (sym : α → αˢʸᵐ) := sym.surjective theorem unsym_injective : Injective (unsym : αˢʸᵐ → α) := unsym.injective theorem unsym_surjective : Surjective (unsym : αˢʸᵐ → α) := unsym.surjective -- Porting note (#10618): @[simp] can prove this theorem sym_inj {a b : α} : sym a = sym b ↔ a = b := sym_injective.eq_iff -- Porting note (#10618): @[simp] can prove this theorem unsym_inj {a b : αˢʸᵐ} : unsym a = unsym b ↔ a = b := unsym_injective.eq_iff instance [Nontrivial α] : Nontrivial αˢʸᵐ := sym_injective.nontrivial instance [Inhabited α] : Inhabited αˢʸᵐ := ⟨sym default⟩ instance [Subsingleton α] : Subsingleton αˢʸᵐ := unsym_injective.subsingleton instance [Unique α] : Unique αˢʸᵐ := Unique.mk' _ instance [IsEmpty α] : IsEmpty αˢʸᵐ := Function.isEmpty unsym @[to_additive] instance [One α] : One αˢʸᵐ where one := sym 1 instance [Add α] : Add αˢʸᵐ where add a b := sym (unsym a + unsym b) instance [Sub α] : Sub αˢʸᵐ where sub a b := sym (unsym a - unsym b) instance [Neg α] : Neg αˢʸᵐ where neg a := sym (-unsym a) -- Introduce the symmetrized multiplication instance [Add α] [Mul α] [One α] [OfNat α 2] [Invertible (2 : α)] : Mul αˢʸᵐ where mul a b := sym (⅟ 2 * (unsym a * unsym b + unsym b * unsym a)) @[to_additive existing] instance [Inv α] : Inv αˢʸᵐ where inv a := sym <| (unsym a)⁻¹ instance (R : Type*) [SMul R α] : SMul R αˢʸᵐ where smul r a := sym (r • unsym a) @[to_additive (attr := simp)] theorem sym_one [One α] : sym (1 : α) = 1 := rfl @[to_additive (attr := simp)] theorem unsym_one [One α] : unsym (1 : αˢʸᵐ) = 1 := rfl @[simp] theorem sym_add [Add α] (a b : α) : sym (a + b) = sym a + sym b := rfl @[simp] theorem unsym_add [Add α] (a b : αˢʸᵐ) : unsym (a + b) = unsym a + unsym b := rfl @[simp] theorem sym_sub [Sub α] (a b : α) : sym (a - b) = sym a - sym b := rfl @[simp] theorem unsym_sub [Sub α] (a b : αˢʸᵐ) : unsym (a - b) = unsym a - unsym b := rfl @[simp] theorem sym_neg [Neg α] (a : α) : sym (-a) = -sym a := rfl @[simp] theorem unsym_neg [Neg α] (a : αˢʸᵐ) : unsym (-a) = -unsym a := rfl theorem mul_def [Add α] [Mul α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : a * b = sym (⅟ 2 * (unsym a * unsym b + unsym b * unsym a)) := rfl theorem unsym_mul [Mul α] [Add α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : unsym (a * b) = ⅟ 2 * (unsym a * unsym b + unsym b * unsym a) := rfl theorem sym_mul_sym [Mul α] [Add α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : α) : sym a * sym b = sym (⅟ 2 * (a * b + b * a)) := rfl set_option linter.existingAttributeWarning false in @[simp, to_additive existing] theorem sym_inv [Inv α] (a : α) : sym a⁻¹ = (sym a)⁻¹ := rfl set_option linter.existingAttributeWarning false in @[simp, to_additive existing] theorem unsym_inv [Inv α] (a : αˢʸᵐ) : unsym a⁻¹ = (unsym a)⁻¹ := rfl @[simp] theorem sym_smul {R : Type*} [SMul R α] (c : R) (a : α) : sym (c • a) = c • sym a := rfl @[simp] theorem unsym_smul {R : Type*} [SMul R α] (c : R) (a : αˢʸᵐ) : unsym (c • a) = c • unsym a := rfl @[to_additive (attr := simp)] theorem unsym_eq_one_iff [One α] (a : αˢʸᵐ) : unsym a = 1 ↔ a = 1 := unsym_injective.eq_iff' rfl @[to_additive (attr := simp)] theorem sym_eq_one_iff [One α] (a : α) : sym a = 1 ↔ a = 1 := sym_injective.eq_iff' rfl @[to_additive] theorem unsym_ne_one_iff [One α] (a : αˢʸᵐ) : unsym a ≠ (1 : α) ↔ a ≠ (1 : αˢʸᵐ) := not_congr <| unsym_eq_one_iff a @[to_additive] theorem sym_ne_one_iff [One α] (a : α) : sym a ≠ (1 : αˢʸᵐ) ↔ a ≠ (1 : α) := not_congr <| sym_eq_one_iff a instance addCommSemigroup [AddCommSemigroup α] : AddCommSemigroup αˢʸᵐ := unsym_injective.addCommSemigroup _ unsym_add instance addMonoid [AddMonoid α] : AddMonoid αˢʸᵐ := unsym_injective.addMonoid _ unsym_zero unsym_add fun _ _ => rfl instance addGroup [AddGroup α] : AddGroup αˢʸᵐ := unsym_injective.addGroup _ unsym_zero unsym_add unsym_neg unsym_sub (fun _ _ => rfl) fun _ _ => rfl instance addCommMonoid [AddCommMonoid α] : AddCommMonoid αˢʸᵐ := { SymAlg.addCommSemigroup, SymAlg.addMonoid with } instance addCommGroup [AddCommGroup α] : AddCommGroup αˢʸᵐ := { SymAlg.addCommMonoid, SymAlg.addGroup with } instance {R : Type*} [Semiring R] [AddCommMonoid α] [Module R α] : Module R αˢʸᵐ := Function.Injective.module R ⟨⟨unsym, unsym_zero⟩, unsym_add⟩ unsym_injective unsym_smul instance [Mul α] [AddMonoidWithOne α] [Invertible (2 : α)] (a : α) [Invertible a] : Invertible (sym a) where invOf := sym (⅟ a) invOf_mul_self := by rw [sym_mul_sym, mul_invOf_self, invOf_mul_self, one_add_one_eq_two, invOf_mul_self, sym_one] mul_invOf_self := by rw [sym_mul_sym, mul_invOf_self, invOf_mul_self, one_add_one_eq_two, invOf_mul_self, sym_one] @[simp] theorem invOf_sym [Mul α] [AddMonoidWithOne α] [Invertible (2 : α)] (a : α) [Invertible a] : ⅟ (sym a) = sym (⅟ a) := rfl instance nonAssocSemiring [Semiring α] [Invertible (2 : α)] : NonAssocSemiring αˢʸᵐ := { SymAlg.addCommMonoid with one := 1 mul := (· * ·) zero_mul := fun _ => by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero] mul_zero := fun _ => by rw [mul_def, unsym_zero, zero_mul, mul_zero, add_zero, mul_zero, sym_zero] mul_one := fun _ => by rw [mul_def, unsym_one, mul_one, one_mul, ← two_mul, invOf_mul_self_assoc, sym_unsym] one_mul := fun _ => by rw [mul_def, unsym_one, mul_one, one_mul, ← two_mul, invOf_mul_self_assoc, sym_unsym] left_distrib := fun a b c => by -- Porting note: rewrote previous proof which used `match` in a way that seems unsupported. rw [mul_def, mul_def, mul_def, ← sym_add, ← mul_add, unsym_add, add_mul] congr 2 rw [mul_add] abel right_distrib := fun a b c => by -- Porting note: rewrote previous proof which used `match` in a way that seems unsupported. rw [mul_def, mul_def, mul_def, ← sym_add, ← mul_add, unsym_add, add_mul] congr 2 rw [mul_add] abel } /-- The symmetrization of a real (unital, associative) algebra is a non-associative ring. -/ instance [Ring α] [Invertible (2 : α)] : NonAssocRing αˢʸᵐ := { SymAlg.nonAssocSemiring, SymAlg.addCommGroup with } /-! The squaring operation coincides for both multiplications -/ theorem unsym_mul_self [Semiring α] [Invertible (2 : α)] (a : αˢʸᵐ) : unsym (a * a) = unsym a * unsym a := by rw [mul_def, unsym_sym, ← two_mul, invOf_mul_self_assoc] theorem sym_mul_self [Semiring α] [Invertible (2 : α)] (a : α) : sym (a * a) = sym a * sym a := by rw [sym_mul_sym, ← two_mul, invOf_mul_self_assoc] theorem mul_comm [Mul α] [AddCommSemigroup α] [One α] [OfNat α 2] [Invertible (2 : α)] (a b : αˢʸᵐ) : a * b = b * a := by rw [mul_def, mul_def, add_comm] instance [Ring α] [Invertible (2 : α)] : CommMagma αˢʸᵐ where mul_comm := SymAlg.mul_comm instance [Ring α] [Invertible (2 : α)] : IsCommJordan αˢʸᵐ where lmul_comm_rmul_rmul a b := by have commute_half_left := fun a : α => by -- Porting note: mathlib3 used `bit0_left` have := (Commute.one_left a).add_left (Commute.one_left a) rw [one_add_one_eq_two] at this exact this.invOf_left.eq -- Porting note: introduced `calc` block to make more robust calc a * b * (a * a) _ = sym (⅟2 * ⅟2 * (unsym a * unsym b * unsym (a * a) + unsym b * unsym a * unsym (a * a) + unsym (a * a) * unsym a * unsym b + unsym (a * a) * unsym b * unsym a)) := ?_ _ = sym (⅟ 2 * (unsym a * unsym (sym (⅟ 2 * (unsym b * unsym (a * a) + unsym (a * a) * unsym b))) + unsym (sym (⅟ 2 * (unsym b * unsym (a * a) + unsym (a * a) * unsym b))) * unsym a)) := ?_ _ = a * (b * (a * a)) := ?_ -- Rearrange LHS · rw [mul_def, mul_def a b, unsym_sym, ← mul_assoc, ← commute_half_left (unsym (a * a)), mul_assoc, mul_assoc, ← mul_add, ← mul_assoc, add_mul, mul_add (unsym (a * a)), ← add_assoc, ← mul_assoc, ← mul_assoc] · rw [unsym_sym, sym_inj, ← mul_assoc, ← commute_half_left (unsym a), mul_assoc (⅟ 2) (unsym a), mul_assoc (⅟ 2) _ (unsym a), ← mul_add, ← mul_assoc] conv_rhs => rw [mul_add (unsym a)] rw [add_mul, ← add_assoc, ← mul_assoc, ← mul_assoc] rw [unsym_mul_self] rw [← mul_assoc, ← mul_assoc, ← mul_assoc, ← mul_assoc, ← sub_eq_zero, ← mul_sub] convert mul_zero (⅟ (2 : α) * ⅟ (2 : α)) rw [add_sub_add_right_eq_sub, add_assoc, add_assoc, add_sub_add_left_eq_sub, add_comm, add_sub_add_right_eq_sub, sub_eq_zero] -- Rearrange RHS · rw [← mul_def, ← mul_def] end SymAlg
Algebra\TrivSqZeroExt.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Eric Wieser -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.GroupTheory.GroupAction.BigOperators import Mathlib.LinearAlgebra.Prod /-! # Trivial Square-Zero Extension Given a ring `R` together with an `(R, R)`-bimodule `M`, the trivial square-zero extension of `M` over `R` is defined to be the `R`-algebra `R ⊕ M` with multiplication given by `(r₁ + m₁) * (r₂ + m₂) = r₁ r₂ + r₁ m₂ + m₁ r₂`. It is a square-zero extension because `M^2 = 0`. Note that expressing this requires bimodules; we write these in general for a not-necessarily-commutative `R` as: ```lean variable {R M : Type*} [Semiring R] [AddCommMonoid M] variable [Module R M] [Module Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] ``` If we instead work with a commutative `R'` acting symmetrically on `M`, we write ```lean variable {R' M : Type*} [CommSemiring R'] [AddCommMonoid M] variable [Module R' M] [Module R'ᵐᵒᵖ M] [IsCentralScalar R' M] ``` noting that in this context `IsCentralScalar R' M` implies `SMulCommClass R' R'ᵐᵒᵖ M`. Many of the later results in this file are only stated for the commutative `R'` for simplicity. ## Main definitions * `TrivSqZeroExt.inl`, `TrivSqZeroExt.inr`: the canonical inclusions into `TrivSqZeroExt R M`. * `TrivSqZeroExt.fst`, `TrivSqZeroExt.snd`: the canonical projections from `TrivSqZeroExt R M`. * `triv_sq_zero_ext.algebra`: the associated `R`-algebra structure. * `TrivSqZeroExt.lift`: the universal property of the trivial square-zero extension; algebra morphisms `TrivSqZeroExt R M →ₐ[S] A` are uniquely defined by an algebra morphism `f : R →ₐ[S] A` on `R` and a linear map `g : M →ₗ[S] A` on `M` such that: * `g x * g y = 0`: the elements of `M` continue to square to zero. * `g (r •> x) = f r * g x` and `g (x <• r) = g x * f r`: left and right actions are preserved by `g`. * `TrivSqZeroExt.lift`: the universal property of the trivial square-zero extension; algebra morphisms `TrivSqZeroExt R M →ₐ[R] A` are uniquely defined by linear maps `M →ₗ[R] A` for which the product of any two elements in the range is zero. -/ universe u v w /-- "Trivial Square-Zero Extension". Given a module `M` over a ring `R`, the trivial square-zero extension of `M` over `R` is defined to be the `R`-algebra `R × M` with multiplication given by `(r₁ + m₁) * (r₂ + m₂) = r₁ r₂ + r₁ m₂ + r₂ m₁`. It is a square-zero extension because `M^2 = 0`. -/ def TrivSqZeroExt (R : Type u) (M : Type v) := R × M local notation "tsze" => TrivSqZeroExt open scoped RightActions namespace TrivSqZeroExt open MulOpposite section Basic variable {R : Type u} {M : Type v} /-- The canonical inclusion `R → TrivSqZeroExt R M`. -/ def inl [Zero M] (r : R) : tsze R M := (r, 0) /-- The canonical inclusion `M → TrivSqZeroExt R M`. -/ def inr [Zero R] (m : M) : tsze R M := (0, m) /-- The canonical projection `TrivSqZeroExt R M → R`. -/ def fst (x : tsze R M) : R := x.1 /-- The canonical projection `TrivSqZeroExt R M → M`. -/ def snd (x : tsze R M) : M := x.2 @[simp] theorem fst_mk (r : R) (m : M) : fst (r, m) = r := rfl @[simp] theorem snd_mk (r : R) (m : M) : snd (r, m) = m := rfl @[ext] theorem ext {x y : tsze R M} (h1 : x.fst = y.fst) (h2 : x.snd = y.snd) : x = y := Prod.ext h1 h2 section variable (M) @[simp] theorem fst_inl [Zero M] (r : R) : (inl r : tsze R M).fst = r := rfl @[simp] theorem snd_inl [Zero M] (r : R) : (inl r : tsze R M).snd = 0 := rfl @[simp] theorem fst_comp_inl [Zero M] : fst ∘ (inl : R → tsze R M) = id := rfl @[simp] theorem snd_comp_inl [Zero M] : snd ∘ (inl : R → tsze R M) = 0 := rfl end section variable (R) @[simp] theorem fst_inr [Zero R] (m : M) : (inr m : tsze R M).fst = 0 := rfl @[simp] theorem snd_inr [Zero R] (m : M) : (inr m : tsze R M).snd = m := rfl @[simp] theorem fst_comp_inr [Zero R] : fst ∘ (inr : M → tsze R M) = 0 := rfl @[simp] theorem snd_comp_inr [Zero R] : snd ∘ (inr : M → tsze R M) = id := rfl end theorem inl_injective [Zero M] : Function.Injective (inl : R → tsze R M) := Function.LeftInverse.injective <| fst_inl _ theorem inr_injective [Zero R] : Function.Injective (inr : M → tsze R M) := Function.LeftInverse.injective <| snd_inr _ end Basic /-! ### Structures inherited from `Prod` Additive operators and scalar multiplication operate elementwise. -/ section Additive variable {T : Type*} {S : Type*} {R : Type u} {M : Type v} instance inhabited [Inhabited R] [Inhabited M] : Inhabited (tsze R M) := instInhabitedProd instance zero [Zero R] [Zero M] : Zero (tsze R M) := Prod.instZero instance add [Add R] [Add M] : Add (tsze R M) := Prod.instAdd instance sub [Sub R] [Sub M] : Sub (tsze R M) := Prod.instSub instance neg [Neg R] [Neg M] : Neg (tsze R M) := Prod.instNeg instance addSemigroup [AddSemigroup R] [AddSemigroup M] : AddSemigroup (tsze R M) := Prod.instAddSemigroup instance addZeroClass [AddZeroClass R] [AddZeroClass M] : AddZeroClass (tsze R M) := Prod.instAddZeroClass instance addMonoid [AddMonoid R] [AddMonoid M] : AddMonoid (tsze R M) := Prod.instAddMonoid instance addGroup [AddGroup R] [AddGroup M] : AddGroup (tsze R M) := Prod.instAddGroup instance addCommSemigroup [AddCommSemigroup R] [AddCommSemigroup M] : AddCommSemigroup (tsze R M) := Prod.instAddCommSemigroup instance addCommMonoid [AddCommMonoid R] [AddCommMonoid M] : AddCommMonoid (tsze R M) := Prod.instAddCommMonoid instance addCommGroup [AddCommGroup R] [AddCommGroup M] : AddCommGroup (tsze R M) := Prod.instAddCommGroup instance smul [SMul S R] [SMul S M] : SMul S (tsze R M) := Prod.smul instance isScalarTower [SMul T R] [SMul T M] [SMul S R] [SMul S M] [SMul T S] [IsScalarTower T S R] [IsScalarTower T S M] : IsScalarTower T S (tsze R M) := Prod.isScalarTower instance smulCommClass [SMul T R] [SMul T M] [SMul S R] [SMul S M] [SMulCommClass T S R] [SMulCommClass T S M] : SMulCommClass T S (tsze R M) := Prod.smulCommClass instance isCentralScalar [SMul S R] [SMul S M] [SMul Sᵐᵒᵖ R] [SMul Sᵐᵒᵖ M] [IsCentralScalar S R] [IsCentralScalar S M] : IsCentralScalar S (tsze R M) := Prod.isCentralScalar instance mulAction [Monoid S] [MulAction S R] [MulAction S M] : MulAction S (tsze R M) := Prod.mulAction instance distribMulAction [Monoid S] [AddMonoid R] [AddMonoid M] [DistribMulAction S R] [DistribMulAction S M] : DistribMulAction S (tsze R M) := Prod.distribMulAction instance module [Semiring S] [AddCommMonoid R] [AddCommMonoid M] [Module S R] [Module S M] : Module S (tsze R M) := Prod.instModule @[simp] theorem fst_zero [Zero R] [Zero M] : (0 : tsze R M).fst = 0 := rfl @[simp] theorem snd_zero [Zero R] [Zero M] : (0 : tsze R M).snd = 0 := rfl @[simp] theorem fst_add [Add R] [Add M] (x₁ x₂ : tsze R M) : (x₁ + x₂).fst = x₁.fst + x₂.fst := rfl @[simp] theorem snd_add [Add R] [Add M] (x₁ x₂ : tsze R M) : (x₁ + x₂).snd = x₁.snd + x₂.snd := rfl @[simp] theorem fst_neg [Neg R] [Neg M] (x : tsze R M) : (-x).fst = -x.fst := rfl @[simp] theorem snd_neg [Neg R] [Neg M] (x : tsze R M) : (-x).snd = -x.snd := rfl @[simp] theorem fst_sub [Sub R] [Sub M] (x₁ x₂ : tsze R M) : (x₁ - x₂).fst = x₁.fst - x₂.fst := rfl @[simp] theorem snd_sub [Sub R] [Sub M] (x₁ x₂ : tsze R M) : (x₁ - x₂).snd = x₁.snd - x₂.snd := rfl @[simp] theorem fst_smul [SMul S R] [SMul S M] (s : S) (x : tsze R M) : (s • x).fst = s • x.fst := rfl @[simp] theorem snd_smul [SMul S R] [SMul S M] (s : S) (x : tsze R M) : (s • x).snd = s • x.snd := rfl theorem fst_sum {ι} [AddCommMonoid R] [AddCommMonoid M] (s : Finset ι) (f : ι → tsze R M) : (∑ i ∈ s, f i).fst = ∑ i ∈ s, (f i).fst := Prod.fst_sum theorem snd_sum {ι} [AddCommMonoid R] [AddCommMonoid M] (s : Finset ι) (f : ι → tsze R M) : (∑ i ∈ s, f i).snd = ∑ i ∈ s, (f i).snd := Prod.snd_sum section variable (M) @[simp] theorem inl_zero [Zero R] [Zero M] : (inl 0 : tsze R M) = 0 := rfl @[simp] theorem inl_add [Add R] [AddZeroClass M] (r₁ r₂ : R) : (inl (r₁ + r₂) : tsze R M) = inl r₁ + inl r₂ := ext rfl (add_zero 0).symm @[simp] theorem inl_neg [Neg R] [SubNegZeroMonoid M] (r : R) : (inl (-r) : tsze R M) = -inl r := ext rfl neg_zero.symm @[simp] theorem inl_sub [Sub R] [SubNegZeroMonoid M] (r₁ r₂ : R) : (inl (r₁ - r₂) : tsze R M) = inl r₁ - inl r₂ := ext rfl (sub_zero _).symm @[simp] theorem inl_smul [Monoid S] [AddMonoid M] [SMul S R] [DistribMulAction S M] (s : S) (r : R) : (inl (s • r) : tsze R M) = s • inl r := ext rfl (smul_zero s).symm theorem inl_sum {ι} [AddCommMonoid R] [AddCommMonoid M] (s : Finset ι) (f : ι → R) : (inl (∑ i ∈ s, f i) : tsze R M) = ∑ i ∈ s, inl (f i) := map_sum (LinearMap.inl ℕ _ _) _ _ end section variable (R) @[simp] theorem inr_zero [Zero R] [Zero M] : (inr 0 : tsze R M) = 0 := rfl @[simp] theorem inr_add [AddZeroClass R] [AddZeroClass M] (m₁ m₂ : M) : (inr (m₁ + m₂) : tsze R M) = inr m₁ + inr m₂ := ext (add_zero 0).symm rfl @[simp] theorem inr_neg [SubNegZeroMonoid R] [Neg M] (m : M) : (inr (-m) : tsze R M) = -inr m := ext neg_zero.symm rfl @[simp] theorem inr_sub [SubNegZeroMonoid R] [Sub M] (m₁ m₂ : M) : (inr (m₁ - m₂) : tsze R M) = inr m₁ - inr m₂ := ext (sub_zero _).symm rfl @[simp] theorem inr_smul [Zero R] [Zero S] [SMulWithZero S R] [SMul S M] (r : S) (m : M) : (inr (r • m) : tsze R M) = r • inr m := ext (smul_zero _).symm rfl theorem inr_sum {ι} [AddCommMonoid R] [AddCommMonoid M] (s : Finset ι) (f : ι → M) : (inr (∑ i ∈ s, f i) : tsze R M) = ∑ i ∈ s, inr (f i) := map_sum (LinearMap.inr ℕ _ _) _ _ end theorem inl_fst_add_inr_snd_eq [AddZeroClass R] [AddZeroClass M] (x : tsze R M) : inl x.fst + inr x.snd = x := ext (add_zero x.1) (zero_add x.2) /-- To show a property hold on all `TrivSqZeroExt R M` it suffices to show it holds on terms of the form `inl r + inr m`. -/ @[elab_as_elim, induction_eliminator, cases_eliminator] theorem ind {R M} [AddZeroClass R] [AddZeroClass M] {P : TrivSqZeroExt R M → Prop} (inl_add_inr : ∀ r m, P (inl r + inr m)) (x) : P x := inl_fst_add_inr_snd_eq x ▸ inl_add_inr x.1 x.2 /-- This cannot be marked `@[ext]` as it ends up being used instead of `LinearMap.prod_ext` when working with `R × M`. -/ theorem linearMap_ext {N} [Semiring S] [AddCommMonoid R] [AddCommMonoid M] [AddCommMonoid N] [Module S R] [Module S M] [Module S N] ⦃f g : tsze R M →ₗ[S] N⦄ (hl : ∀ r, f (inl r) = g (inl r)) (hr : ∀ m, f (inr m) = g (inr m)) : f = g := LinearMap.prod_ext (LinearMap.ext hl) (LinearMap.ext hr) variable (R M) /-- The canonical `R`-linear inclusion `M → TrivSqZeroExt R M`. -/ @[simps apply] def inrHom [Semiring R] [AddCommMonoid M] [Module R M] : M →ₗ[R] tsze R M := { LinearMap.inr R R M with toFun := inr } /-- The canonical `R`-linear projection `TrivSqZeroExt R M → M`. -/ @[simps apply] def sndHom [Semiring R] [AddCommMonoid M] [Module R M] : tsze R M →ₗ[R] M := { LinearMap.snd _ _ _ with toFun := snd } end Additive /-! ### Multiplicative structure -/ section Mul variable {R : Type u} {M : Type v} instance one [One R] [Zero M] : One (tsze R M) := ⟨(1, 0)⟩ instance mul [Mul R] [Add M] [SMul R M] [SMul Rᵐᵒᵖ M] : Mul (tsze R M) := ⟨fun x y => (x.1 * y.1, x.1 •> y.2 + x.2 <• y.1)⟩ @[simp] theorem fst_one [One R] [Zero M] : (1 : tsze R M).fst = 1 := rfl @[simp] theorem snd_one [One R] [Zero M] : (1 : tsze R M).snd = 0 := rfl @[simp] theorem fst_mul [Mul R] [Add M] [SMul R M] [SMul Rᵐᵒᵖ M] (x₁ x₂ : tsze R M) : (x₁ * x₂).fst = x₁.fst * x₂.fst := rfl @[simp] theorem snd_mul [Mul R] [Add M] [SMul R M] [SMul Rᵐᵒᵖ M] (x₁ x₂ : tsze R M) : (x₁ * x₂).snd = x₁.fst •> x₂.snd + x₁.snd <• x₂.fst := rfl section variable (M) @[simp] theorem inl_one [One R] [Zero M] : (inl 1 : tsze R M) = 1 := rfl @[simp] theorem inl_mul [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] (r₁ r₂ : R) : (inl (r₁ * r₂) : tsze R M) = inl r₁ * inl r₂ := ext rfl <| show (0 : M) = r₁ •> (0 : M) + (0 : M) <• r₂ by rw [smul_zero, zero_add, smul_zero] theorem inl_mul_inl [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] (r₁ r₂ : R) : (inl r₁ * inl r₂ : tsze R M) = inl (r₁ * r₂) := (inl_mul M r₁ r₂).symm end section variable (R) @[simp] theorem inr_mul_inr [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] (m₁ m₂ : M) : (inr m₁ * inr m₂ : tsze R M) = 0 := ext (mul_zero _) <| show (0 : R) •> m₂ + m₁ <• (0 : R) = 0 by rw [zero_smul, zero_add, op_zero, zero_smul] end theorem inl_mul_inr [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] (r : R) (m : M) : (inl r * inr m : tsze R M) = inr (r • m) := ext (mul_zero r) <| show r • m + (0 : Rᵐᵒᵖ) • (0 : M) = r • m by rw [smul_zero, add_zero] theorem inr_mul_inl [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] (r : R) (m : M) : (inr m * inl r : tsze R M) = inr (m <• r) := ext (zero_mul r) <| show (0 : R) •> (0 : M) + m <• r = m <• r by rw [smul_zero, zero_add] theorem inl_mul_eq_smul [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] (r : R) (x : tsze R M) : inl r * x = r •> x := ext rfl (by dsimp; rw [smul_zero, add_zero]) theorem mul_inl_eq_op_smul [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] (x : tsze R M) (r : R) : x * inl r = x <• r := ext rfl (by dsimp; rw [smul_zero, zero_add]) instance mulOneClass [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] : MulOneClass (tsze R M) := { TrivSqZeroExt.one, TrivSqZeroExt.mul with one_mul := fun x => ext (one_mul x.1) <| show (1 : R) •> x.2 + (0 : M) <• x.1 = x.2 by rw [one_smul, smul_zero, add_zero] mul_one := fun x => ext (mul_one x.1) <| show x.1 • (0 : M) + x.2 <• (1 : R) = x.2 by rw [smul_zero, zero_add, op_one, one_smul] } instance addMonoidWithOne [AddMonoidWithOne R] [AddMonoid M] : AddMonoidWithOne (tsze R M) := { TrivSqZeroExt.addMonoid, TrivSqZeroExt.one with natCast := fun n => inl n natCast_zero := by simp [Nat.cast] natCast_succ := fun _ => by ext <;> simp [Nat.cast] } @[simp] theorem fst_natCast [AddMonoidWithOne R] [AddMonoid M] (n : ℕ) : (n : tsze R M).fst = n := rfl @[deprecated (since := "2024-04-17")] alias fst_nat_cast := fst_natCast @[simp] theorem snd_natCast [AddMonoidWithOne R] [AddMonoid M] (n : ℕ) : (n : tsze R M).snd = 0 := rfl @[deprecated (since := "2024-04-17")] alias snd_nat_cast := snd_natCast @[simp] theorem inl_natCast [AddMonoidWithOne R] [AddMonoid M] (n : ℕ) : (inl n : tsze R M) = n := rfl @[deprecated (since := "2024-04-17")] alias inl_nat_cast := inl_natCast instance addGroupWithOne [AddGroupWithOne R] [AddGroup M] : AddGroupWithOne (tsze R M) := { TrivSqZeroExt.addGroup, TrivSqZeroExt.addMonoidWithOne with intCast := fun z => inl z intCast_ofNat := fun _n => ext (Int.cast_natCast _) rfl intCast_negSucc := fun _n => ext (Int.cast_negSucc _) neg_zero.symm } @[simp] theorem fst_intCast [AddGroupWithOne R] [AddGroup M] (z : ℤ) : (z : tsze R M).fst = z := rfl @[deprecated (since := "2024-04-17")] alias fst_int_cast := fst_intCast @[simp] theorem snd_intCast [AddGroupWithOne R] [AddGroup M] (z : ℤ) : (z : tsze R M).snd = 0 := rfl @[deprecated (since := "2024-04-17")] alias snd_int_cast := snd_intCast @[simp] theorem inl_intCast [AddGroupWithOne R] [AddGroup M] (z : ℤ) : (inl z : tsze R M) = z := rfl @[deprecated (since := "2024-04-17")] alias inl_int_cast := inl_intCast instance nonAssocSemiring [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] : NonAssocSemiring (tsze R M) := { TrivSqZeroExt.addMonoidWithOne, TrivSqZeroExt.mulOneClass, TrivSqZeroExt.addCommMonoid with zero_mul := fun x => ext (zero_mul x.1) <| show (0 : R) •> x.2 + (0 : M) <• x.1 = 0 by rw [zero_smul, zero_add, smul_zero] mul_zero := fun x => ext (mul_zero x.1) <| show x.1 • (0 : M) + (0 : Rᵐᵒᵖ) • x.2 = 0 by rw [smul_zero, zero_add, zero_smul] left_distrib := fun x₁ x₂ x₃ => ext (mul_add x₁.1 x₂.1 x₃.1) <| show x₁.1 •> (x₂.2 + x₃.2) + x₁.2 <• (x₂.1 + x₃.1) = x₁.1 •> x₂.2 + x₁.2 <• x₂.1 + (x₁.1 •> x₃.2 + x₁.2 <• x₃.1) by simp_rw [smul_add, MulOpposite.op_add, add_smul, add_add_add_comm] right_distrib := fun x₁ x₂ x₃ => ext (add_mul x₁.1 x₂.1 x₃.1) <| show (x₁.1 + x₂.1) •> x₃.2 + (x₁.2 + x₂.2) <• x₃.1 = x₁.1 •> x₃.2 + x₁.2 <• x₃.1 + (x₂.1 •> x₃.2 + x₂.2 <• x₃.1) by simp_rw [add_smul, smul_add, add_add_add_comm] } instance nonAssocRing [Ring R] [AddCommGroup M] [Module R M] [Module Rᵐᵒᵖ M] : NonAssocRing (tsze R M) := { TrivSqZeroExt.addGroupWithOne, TrivSqZeroExt.nonAssocSemiring with } /-- In the general non-commutative case, the power operator is $$\begin{align} (r + m)^n &= r^n + r^{n-1}m + r^{n-2}mr + \cdots + rmr^{n-2} + mr^{n-1} \\ & =r^n + \sum_{i = 0}^{n - 1} r^{(n - 1) - i} m r^{i} \end{align}$$ In the commutative case this becomes the simpler $(r + m)^n = r^n + nr^{n-1}m$. -/ instance [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] : Pow (tsze R M) ℕ := ⟨fun x n => ⟨x.fst ^ n, ((List.range n).map fun i => x.fst ^ (n.pred - i) •> x.snd <• x.fst ^ i).sum⟩⟩ @[simp] theorem fst_pow [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] (x : tsze R M) (n : ℕ) : fst (x ^ n) = x.fst ^ n := rfl theorem snd_pow_eq_sum [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] (x : tsze R M) (n : ℕ) : snd (x ^ n) = ((List.range n).map fun i => x.fst ^ (n.pred - i) •> x.snd <• x.fst ^ i).sum := rfl theorem snd_pow_of_smul_comm [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] (x : tsze R M) (n : ℕ) (h : x.snd <• x.fst = x.fst •> x.snd) : snd (x ^ n) = n • x.fst ^ n.pred •> x.snd := by simp_rw [snd_pow_eq_sum, ← smul_comm (_ : R) (_ : Rᵐᵒᵖ), aux, smul_smul, ← pow_add] match n with | 0 => rw [Nat.pred_zero, pow_zero, List.range_zero, zero_smul, List.map_nil, List.sum_nil] | (Nat.succ n) => simp_rw [Nat.pred_succ] refine (List.sum_eq_card_nsmul _ (x.fst ^ n • x.snd) ?_).trans ?_ · rintro m hm simp_rw [List.mem_map, List.mem_range] at hm obtain ⟨i, hi, rfl⟩ := hm rw [Nat.sub_add_cancel (Nat.lt_succ_iff.mp hi)] · rw [List.length_map, List.length_range] where aux : ∀ n : ℕ, x.snd <• x.fst ^ n = x.fst ^ n •> x.snd := by intro n induction' n with n ih · simp · rw [pow_succ, op_mul, mul_smul, mul_smul, ← h, smul_comm (_ : R) (op x.fst) x.snd, ih] theorem snd_pow_of_smul_comm' [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] (x : tsze R M) (n : ℕ) (h : x.snd <• x.fst = x.fst •> x.snd) : snd (x ^ n) = n • (x.snd <• x.fst ^ n.pred) := by rw [snd_pow_of_smul_comm _ _ h, snd_pow_of_smul_comm.aux _ h] @[simp] theorem snd_pow [CommMonoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [IsCentralScalar R M] (x : tsze R M) (n : ℕ) : snd (x ^ n) = n • x.fst ^ n.pred • x.snd := snd_pow_of_smul_comm _ _ (op_smul_eq_smul _ _) @[simp] theorem inl_pow [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] (r : R) (n : ℕ) : (inl r ^ n : tsze R M) = inl (r ^ n) := ext rfl <| by simp [snd_pow_eq_sum, List.map_const'] instance monoid [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] : Monoid (tsze R M) := { TrivSqZeroExt.mulOneClass with mul_assoc := fun x y z => ext (mul_assoc x.1 y.1 z.1) <| show (x.1 * y.1) •> z.2 + (x.1 •> y.2 + x.2 <• y.1) <• z.1 = x.1 •> (y.1 •> z.2 + y.2 <• z.1) + x.2 <• (y.1 * z.1) by simp_rw [smul_add, ← mul_smul, add_assoc, smul_comm, op_mul] npow := fun n x => x ^ n npow_zero := fun x => ext (pow_zero x.fst) (by simp [snd_pow_eq_sum]) npow_succ := fun n x => ext (pow_succ _ _) (by simp_rw [snd_mul, snd_pow_eq_sum, Nat.pred_succ] cases n · simp [List.range_succ] rw [List.sum_range_succ'] simp only [pow_zero, op_one, Nat.sub_zero, one_smul, Nat.succ_sub_succ_eq_sub, fst_pow, Nat.pred_succ, List.smul_sum, List.map_map, Function.comp] simp_rw [← smul_comm (_ : R) (_ : Rᵐᵒᵖ), smul_smul, pow_succ] rfl) } theorem fst_list_prod [Monoid R] [AddMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] (l : List (tsze R M)) : l.prod.fst = (l.map fst).prod := map_list_prod ({ toFun := fst, map_one' := fst_one, map_mul' := fst_mul } : tsze R M →* R) _ instance semiring [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] : Semiring (tsze R M) := { TrivSqZeroExt.monoid, TrivSqZeroExt.nonAssocSemiring with } /-- The second element of a product $\prod_{i=0}^n (r_i + m_i)$ is a sum of terms of the form $r_0\cdots r_{i-1}m_ir_{i+1}\cdots r_n$. -/ theorem snd_list_prod [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] (l : List (tsze R M)) : l.prod.snd = (l.enum.map fun x : ℕ × tsze R M => ((l.map fst).take x.1).prod •> x.snd.snd <• ((l.map fst).drop x.1.succ).prod).sum := by induction' l with x xs ih · simp · rw [List.enum_cons, ← List.map_fst_add_enum_eq_enumFrom] simp_rw [List.map_cons, List.map_map, Function.comp, Prod.map_snd, Prod.map_fst, id, List.take_zero, List.take_cons, List.prod_nil, List.prod_cons, snd_mul, one_smul, List.drop, mul_smul, List.sum_cons, fst_list_prod, ih, List.smul_sum, List.map_map, ← smul_comm (_ : R) (_ : Rᵐᵒᵖ)] exact add_comm _ _ instance ring [Ring R] [AddCommGroup M] [Module R M] [Module Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] : Ring (tsze R M) := { TrivSqZeroExt.semiring, TrivSqZeroExt.nonAssocRing with } instance commMonoid [CommMonoid R] [AddCommMonoid M] [DistribMulAction R M] [DistribMulAction Rᵐᵒᵖ M] [IsCentralScalar R M] : CommMonoid (tsze R M) := { TrivSqZeroExt.monoid with mul_comm := fun x₁ x₂ => ext (mul_comm x₁.1 x₂.1) <| show x₁.1 •> x₂.2 + x₁.2 <• x₂.1 = x₂.1 •> x₁.2 + x₂.2 <• x₁.1 by rw [op_smul_eq_smul, op_smul_eq_smul, add_comm] } instance commSemiring [CommSemiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] [IsCentralScalar R M] : CommSemiring (tsze R M) := { TrivSqZeroExt.commMonoid, TrivSqZeroExt.nonAssocSemiring with } instance commRing [CommRing R] [AddCommGroup M] [Module R M] [Module Rᵐᵒᵖ M] [IsCentralScalar R M] : CommRing (tsze R M) := { TrivSqZeroExt.nonAssocRing, TrivSqZeroExt.commSemiring with } variable (R M) /-- The canonical inclusion of rings `R → TrivSqZeroExt R M`. -/ @[simps apply] def inlHom [Semiring R] [AddCommMonoid M] [Module R M] [Module Rᵐᵒᵖ M] : R →+* tsze R M where toFun := inl map_one' := inl_one M map_mul' := inl_mul M map_zero' := inl_zero M map_add' := inl_add M end Mul section Inv variable {R : Type u} {M : Type v} variable [Neg M] [Inv R] [SMul Rᵐᵒᵖ M] [SMul R M] /-- Inversion of the trivial-square-zero extension, sending $r + m$ to $r^{-1} - r^{-1}mr^{-1}$. -/ instance instInv : Inv (tsze R M) := ⟨fun b => (b.1⁻¹, -(b.1⁻¹ •> b.2 <• b.1⁻¹))⟩ @[simp] theorem fst_inv (x : tsze R M) : fst x⁻¹ = (fst x)⁻¹ := rfl @[simp] theorem snd_inv (x : tsze R M) : snd x⁻¹ = -((fst x)⁻¹ •> snd x <• (fst x)⁻¹) := rfl end Inv section DivisionSemiring variable {R : Type u} {M : Type v} variable [DivisionSemiring R] [AddCommGroup M] [Module Rᵐᵒᵖ M] [Module R M] protected theorem inv_inl (r : R) : (inl r)⁻¹ = (inl (r⁻¹ : R) : tsze R M) := by ext · rw [fst_inv, fst_inl, fst_inl] · rw [snd_inv, fst_inl, snd_inl, snd_inl, smul_zero, smul_zero, neg_zero] @[simp] theorem inv_inr (m : M) : (inr m)⁻¹ = (0 : tsze R M) := by ext · rw [fst_inv, fst_inr, fst_zero, inv_zero] · rw [snd_inv, snd_inr, fst_inr, inv_zero, op_zero, zero_smul, snd_zero, neg_zero] @[simp] protected theorem inv_zero : (0 : tsze R M)⁻¹ = (0 : tsze R M) := by rw [← inl_zero, TrivSqZeroExt.inv_inl, inv_zero] @[simp] protected theorem inv_one : (1 : tsze R M)⁻¹ = (1 : tsze R M) := by rw [← inl_one, TrivSqZeroExt.inv_inl, inv_one] protected theorem inv_mul_cancel {x : tsze R M} (hx : fst x ≠ 0) : x⁻¹ * x = 1 := by ext · rw [fst_mul, fst_inv, inv_mul_cancel hx, fst_one] · rw [snd_mul, snd_inv, snd_one, smul_neg, op_smul_op_smul, inv_mul_cancel hx, op_one, one_smul, fst_inv, add_right_neg] variable [SMulCommClass R Rᵐᵒᵖ M] protected theorem mul_inv_cancel {x : tsze R M} (hx : fst x ≠ 0) : x * x⁻¹ = 1 := by ext · rw [fst_mul, fst_inv, fst_one, mul_inv_cancel hx] · rw [snd_mul, snd_inv, snd_one, smul_neg, smul_comm, smul_smul, mul_inv_cancel hx, one_smul, fst_inv, add_left_neg] protected theorem mul_inv_rev (a b : tsze R M) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := by ext · rw [fst_inv, fst_mul, fst_mul, mul_inv_rev, fst_inv, fst_inv] · simp only [snd_inv, snd_mul, fst_mul, fst_inv] simp only [neg_smul, smul_neg, smul_add] simp_rw [mul_inv_rev, smul_comm (_ : R), op_smul_op_smul, smul_smul, add_comm, neg_add] obtain ha0 | ha := eq_or_ne (fst a) 0 · simp [ha0] obtain hb0 | hb := eq_or_ne (fst b) 0 · simp [hb0] rw [inv_mul_cancel_right₀ ha, mul_inv_cancel_left₀ hb] protected theorem inv_inv {x : tsze R M} (hx : fst x ≠ 0) : x⁻¹⁻¹ = x := -- adapted from `Matrix.nonsing_inv_nonsing_inv` calc x⁻¹⁻¹ = 1 * x⁻¹⁻¹ := by rw [one_mul] _ = x * x⁻¹ * x⁻¹⁻¹ := by rw [TrivSqZeroExt.mul_inv_cancel hx] _ = x := by rw [mul_assoc, TrivSqZeroExt.mul_inv_cancel, mul_one] rw [fst_inv] apply inv_ne_zero hx end DivisionSemiring section DivisionRing variable {R : Type u} {M : Type v} variable [DivisionRing R] [AddCommGroup M] [Module Rᵐᵒᵖ M] [Module R M] protected theorem inv_neg {x : tsze R M} : (-x)⁻¹ = -(x⁻¹) := by ext <;> simp [inv_neg] end DivisionRing section Algebra variable (S : Type*) (R R' : Type u) (M : Type v) variable [CommSemiring S] [Semiring R] [CommSemiring R'] [AddCommMonoid M] variable [Algebra S R] [Algebra S R'] [Module S M] variable [Module R M] [Module Rᵐᵒᵖ M] [SMulCommClass R Rᵐᵒᵖ M] variable [IsScalarTower S R M] [IsScalarTower S Rᵐᵒᵖ M] variable [Module R' M] [Module R'ᵐᵒᵖ M] [IsCentralScalar R' M] [IsScalarTower S R' M] instance algebra' : Algebra S (tsze R M) := { (TrivSqZeroExt.inlHom R M).comp (algebraMap S R) with smul := (· • ·) commutes' := fun s x => ext (Algebra.commutes _ _) <| show algebraMap S R s •> x.snd + (0 : M) <• x.fst = x.fst •> (0 : M) + x.snd <• algebraMap S R s by rw [smul_zero, smul_zero, add_zero, zero_add] rw [Algebra.algebraMap_eq_smul_one, MulOpposite.op_smul, op_one, smul_assoc, one_smul, smul_assoc, one_smul] smul_def' := fun s x => ext (Algebra.smul_def _ _) <| show s • x.snd = algebraMap S R s •> x.snd + (0 : M) <• x.fst by rw [smul_zero, add_zero, algebraMap_smul] } -- shortcut instance for the common case instance : Algebra R' (tsze R' M) := TrivSqZeroExt.algebra' _ _ _ theorem algebraMap_eq_inl : ⇑(algebraMap R' (tsze R' M)) = inl := rfl theorem algebraMap_eq_inlHom : algebraMap R' (tsze R' M) = inlHom R' M := rfl theorem algebraMap_eq_inl' (s : S) : algebraMap S (tsze R M) s = inl (algebraMap S R s) := rfl /-- The canonical `S`-algebra projection `TrivSqZeroExt R M → R`. -/ @[simps] def fstHom : tsze R M →ₐ[S] R where toFun := fst map_one' := fst_one map_mul' := fst_mul map_zero' := fst_zero (M := M) map_add' := fst_add commutes' _r := fst_inl M _ /-- The canonical `S`-algebra inclusion `R → TrivSqZeroExt R M`. -/ @[simps] def inlAlgHom : R →ₐ[S] tsze R M where toFun := inl map_one' := inl_one _ map_mul' := inl_mul _ map_zero' := inl_zero (M := M) map_add' := inl_add _ commutes' _r := (algebraMap_eq_inl' _ _ _ _).symm variable {R R' S M} theorem algHom_ext {A} [Semiring A] [Algebra R' A] ⦃f g : tsze R' M →ₐ[R'] A⦄ (h : ∀ m, f (inr m) = g (inr m)) : f = g := AlgHom.toLinearMap_injective <| linearMap_ext (fun _r => (f.commutes _).trans (g.commutes _).symm) h @[ext] theorem algHom_ext' {A} [Semiring A] [Algebra S A] ⦃f g : tsze R M →ₐ[S] A⦄ (hinl : f.comp (inlAlgHom S R M) = g.comp (inlAlgHom S R M)) (hinr : f.toLinearMap.comp (inrHom R M |>.restrictScalars S) = g.toLinearMap.comp (inrHom R M |>.restrictScalars S)) : f = g := AlgHom.toLinearMap_injective <| linearMap_ext (AlgHom.congr_fun hinl) (LinearMap.congr_fun hinr) variable {A : Type*} [Semiring A] [Algebra S A] [Algebra R' A] /-- Assemble an algebra morphism `TrivSqZeroExt R M →ₐ[S] A` from separate morphisms on `R` and `M`. Namely, we require that for an algebra morphism `f : R →ₐ[S] A` and a linear map `g : M →ₗ[S] A`, we have: * `g x * g y = 0`: the elements of `M` continue to square to zero. * `g (r •> x) = f r * g x` and `g (x <• r) = g x * f r`: scalar multiplication on the left and right is sent to left- and right- multiplication by the image under `f`. See `TrivSqZeroExt.liftEquiv` for this as an equiv; namely that any such algebra morphism can be factored in this way. When `R` is commutative, this can be invoked with `f = Algebra.ofId R A`, which satisfies `hfg` and `hgf`. This version is captured as an equiv by `TrivSqZeroExt.liftEquivOfComm`. -/ def lift (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r •> x) = f r * g x) (hgf : ∀ r x, g (x <• r) = g x * f r) : tsze R M →ₐ[S] A := AlgHom.ofLinearMap ((f.comp <| fstHom S R M).toLinearMap + g ∘ₗ (sndHom R M |>.restrictScalars S)) (show f 1 + g (0 : M) = 1 by rw [map_zero, map_one, add_zero]) (TrivSqZeroExt.ind fun r₁ m₁ => TrivSqZeroExt.ind fun r₂ m₂ => by dsimp simp only [add_zero, zero_add, add_mul, mul_add, smul_mul_smul, hg, smul_zero, op_smul_eq_smul] rw [← map_mul, LinearMap.map_add, add_comm (g _), add_assoc, hfg, hgf]) theorem lift_def (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r • x) = f r * g x) (hgf : ∀ r x, g (op r • x) = g x * f r) (x : tsze R M) : lift f g hg hfg hgf x = f x.fst + g x.snd := rfl @[simp] theorem lift_apply_inl (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r •> x) = f r * g x) (hgf : ∀ r x, g (x <• r) = g x * f r) (r : R) : lift f g hg hfg hgf (inl r) = f r := show f r + g 0 = f r by rw [map_zero, add_zero] @[simp] theorem lift_apply_inr (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r •> x) = f r * g x) (hgf : ∀ r x, g (x <• r) = g x * f r) (m : M) : lift f g hg hfg hgf (inr m) = g m := show f 0 + g m = g m by rw [map_zero, zero_add] @[simp] theorem lift_comp_inlHom (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r •> x) = f r * g x) (hgf : ∀ r x, g (x <• r) = g x * f r) : (lift f g hg hfg hgf).comp (inlAlgHom S R M) = f := AlgHom.ext <| lift_apply_inl f g hg hfg hgf @[simp] theorem lift_comp_inrHom (f : R →ₐ[S] A) (g : M →ₗ[S] A) (hg : ∀ x y, g x * g y = 0) (hfg : ∀ r x, g (r •> x) = f r * g x) (hgf : ∀ r x, g (x <• r) = g x * f r) : (lift f g hg hfg hgf).toLinearMap.comp (inrHom R M |>.restrictScalars S) = g := LinearMap.ext <| lift_apply_inr f g hg hfg hgf /-- When applied to `inr` and `inl` themselves, `lift` is the identity. -/ @[simp] theorem lift_inlAlgHom_inrHom : lift (inlAlgHom _ _ _) (inrHom R M |>.restrictScalars S) (inr_mul_inr R) (fun _ _ => (inl_mul_inr _ _).symm) (fun _ _ => (inr_mul_inl _ _).symm) = AlgHom.id S (tsze R M) := algHom_ext' (lift_comp_inlHom _ _ _ _ _) (lift_comp_inrHom _ _ _ _ _) /-- A universal property of the trivial square-zero extension, providing a unique `TrivSqZeroExt R M →ₐ[R] A` for every pair of maps `f : R →ₐ[S] A` and `g : M →ₗ[S] A`, where the range of `g` has no non-zero products, and scaling the input to `g` on the left or right amounts to a corresponding multiplication by `f` in the output. This isomorphism is named to match the very similar `Complex.lift`. -/ @[simps! apply symm_apply_coe] def liftEquiv : {fg : (R →ₐ[S] A) × (M →ₗ[S] A) // (∀ x y, fg.2 x * fg.2 y = 0) ∧ (∀ r x, fg.2 (r •> x) = fg.1 r * fg.2 x) ∧ (∀ r x, fg.2 (x <• r) = fg.2 x * fg.1 r)} ≃ (tsze R M →ₐ[S] A) where toFun fg := lift fg.val.1 fg.val.2 fg.prop.1 fg.prop.2.1 fg.prop.2.2 invFun F := ⟨(F.comp (inlAlgHom _ _ _), F.toLinearMap ∘ₗ (inrHom _ _ |>.restrictScalars _)), (fun _x _y => (map_mul F _ _).symm.trans <| (F.congr_arg <| inr_mul_inr _ _ _).trans (map_zero F)), (fun _r _x => (F.congr_arg (inl_mul_inr _ _).symm).trans (map_mul F _ _)), (fun _r _x => (F.congr_arg (inr_mul_inl _ _).symm).trans (map_mul F _ _))⟩ left_inv _f := Subtype.ext <| Prod.ext (lift_comp_inlHom _ _ _ _ _) (lift_comp_inrHom _ _ _ _ _) right_inv _F := algHom_ext' (lift_comp_inlHom _ _ _ _ _) (lift_comp_inrHom _ _ _ _ _) /-- A simplified version of `TrivSqZeroExt.liftEquiv` for the commutative case. -/ @[simps! apply symm_apply_coe] def liftEquivOfComm : { f : M →ₗ[R'] A // ∀ x y, f x * f y = 0 } ≃ (tsze R' M →ₐ[R'] A) := by refine Equiv.trans ?_ liftEquiv exact { toFun := fun f => ⟨(Algebra.ofId _ _, f.val), f.prop, fun r x => by simp [Algebra.smul_def, Algebra.ofId_apply], fun r x => by simp [Algebra.smul_def, Algebra.ofId_apply, Algebra.commutes]⟩ invFun := fun fg => ⟨fg.val.2, fg.prop.1⟩ left_inv := fun f => rfl right_inv := fun fg => Subtype.ext <| Prod.ext (AlgHom.toLinearMap_injective <| LinearMap.ext_ring <| by simp) rfl } section map variable {N P : Type*} [AddCommMonoid N] [Module R' N] [Module R'ᵐᵒᵖ N] [IsCentralScalar R' N] [AddCommMonoid P] [Module R' P] [Module R'ᵐᵒᵖ P] [IsCentralScalar R' P] /-- Functoriality of `TrivSqZeroExt` when the ring is commutative: a linear map `f : M →ₗ[R'] N` induces a morphism of `R'`-algebras from `TrivSqZeroExt R' M` to `TrivSqZeroExt R' N`. Note that we cannot neatly state the non-commutative case, as we do not have morphisms of bimodules. -/ def map (f : M →ₗ[R'] N) : TrivSqZeroExt R' M →ₐ[R'] TrivSqZeroExt R' N := liftEquivOfComm ⟨inrHom R' N ∘ₗ f, fun _ _ => inr_mul_inr _ _ _⟩ @[simp] theorem map_inl (f : M →ₗ[R'] N) (r : R') : map f (inl r) = inl r := by rw [map, liftEquivOfComm_apply, lift_apply_inl, Algebra.ofId_apply, algebraMap_eq_inl] @[simp] theorem map_inr (f : M →ₗ[R'] N) (x : M) : map f (inr x) = inr (f x) := by rw [map, liftEquivOfComm_apply, lift_apply_inr, LinearMap.comp_apply, inrHom_apply] @[simp] theorem fst_map (f : M →ₗ[R'] N) (x : TrivSqZeroExt R' M) : fst (map f x) = fst x := by simp [map, lift_def, Algebra.ofId_apply, algebraMap_eq_inl] @[simp] theorem snd_map (f : M →ₗ[R'] N) (x : TrivSqZeroExt R' M) : snd (map f x) = f (snd x) := by simp [map, lift_def, Algebra.ofId_apply, algebraMap_eq_inl] @[simp] theorem map_comp_inlAlgHom (f : M →ₗ[R'] N) : (map f).comp (inlAlgHom R' R' M) = inlAlgHom R' R' N := AlgHom.ext <| map_inl _ @[simp] theorem map_comp_inrHom (f : M →ₗ[R'] N) : (map f).toLinearMap ∘ₗ inrHom R' M = inrHom R' N ∘ₗ f := LinearMap.ext <| map_inr _ @[simp] theorem fstHom_comp_map (f : M →ₗ[R'] N) : (fstHom R' R' N).comp (map f) = fstHom R' R' M := AlgHom.ext <| fst_map _ @[simp] theorem sndHom_comp_map (f : M →ₗ[R'] N) : sndHom R' N ∘ₗ (map f).toLinearMap = f ∘ₗ sndHom R' M := LinearMap.ext <| snd_map _ @[simp] theorem map_id : map (LinearMap.id : M →ₗ[R'] M) = AlgHom.id R' _ := by apply algHom_ext simp only [map_inr, LinearMap.id_coe, id_eq, AlgHom.coe_id, forall_const] theorem map_comp_map (f : M →ₗ[R'] N) (g : N →ₗ[R'] P) : map (g.comp f) = (map g).comp (map f) := by apply algHom_ext simp only [map_inr, LinearMap.coe_comp, Function.comp_apply, AlgHom.coe_comp, forall_const] end map end Algebra end TrivSqZeroExt
Algebra\AddConstMap\Basic.lean
/- Copyright (c) 2024 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.Group.Action.Pi import Mathlib.Algebra.GroupPower.IterateHom import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Order.Archimedean.Basic import Mathlib.Algebra.Order.Group.Instances import Mathlib.Logic.Function.Iterate /-! # Maps (semi)conjugating a shift to a shift Denote by $S^1$ the unit circle `UnitAddCircle`. A common way to study a self-map $f\colon S^1\to S^1$ of degree `1` is to lift it to a map $\tilde f\colon \mathbb R\to \mathbb R$ such that $\tilde f(x + 1) = \tilde f(x)+1$ for all `x`. In this file we define a structure and a typeclass for bundled maps satisfying `f (x + a) = f x + b`. We use parameters `a` and `b` instead of `1` to accomodate for two use cases: - maps between circles of different lengths; - self-maps $f\colon S^1\to S^1$ of degree other than one, including orientation-reversing maps. -/ open Function Set /-- A bundled map `f : G → H` such that `f (x + a) = f x + b` for all `x`. One can think about `f` as a lift to `G` of a map between two `AddCircle`s. -/ structure AddConstMap (G H : Type*) [Add G] [Add H] (a : G) (b : H) where /-- The underlying function of an `AddConstMap`. Use automatic coercion to function instead. -/ protected toFun : G → H /-- An `AddConstMap` satisfies `f (x + a) = f x + b`. Use `map_add_const` instead. -/ map_add_const' (x : G) : toFun (x + a) = toFun x + b @[inherit_doc] scoped [AddConstMap] notation:25 G " →+c[" a ", " b "] " H => AddConstMap G H a b /-- Typeclass for maps satisfying `f (x + a) = f x + b`. Note that `a` and `b` are `outParam`s, so one should not add instances like `[AddConstMapClass F G H a b] : AddConstMapClass F G H (-a) (-b)`. -/ class AddConstMapClass (F : Type*) (G H : outParam Type*) [Add G] [Add H] (a : outParam G) (b : outParam H) [FunLike F G H] : Prop where /-- A map of `AddConstMapClass` class semiconjugates shift by `a` to the shift by `b`: `∀ x, f (x + a) = f x + b`. -/ map_add_const (f : F) (x : G) : f (x + a) = f x + b namespace AddConstMapClass /-! ### Properties of `AddConstMapClass` maps In this section we prove properties like `f (x + n • a) = f x + n • b`. -/ scoped [AddConstMapClass] attribute [simp] map_add_const variable {F G H : Type*} [FunLike F G H] {a : G} {b : H} protected theorem semiconj [Add G] [Add H] [AddConstMapClass F G H a b] (f : F) : Semiconj f (· + a) (· + b) := map_add_const f @[scoped simp] theorem map_add_nsmul [AddMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℕ) : f (x + n • a) = f x + n • b := by simpa using (AddConstMapClass.semiconj f).iterate_right n x @[scoped simp] theorem map_add_nat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) : f (x + n) = f x + n • b := by simp [← map_add_nsmul] theorem map_add_one [AddMonoidWithOne G] [Add H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (x + 1) = f x + b := map_add_const f x @[scoped simp] theorem map_add_ofNat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x + no_index (OfNat.ofNat n)) = f x + (OfNat.ofNat n : ℕ) • b := map_add_nat' f x n theorem map_add_nat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℕ) : f (x + n) = f x + n := by simp theorem map_add_ofNat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x + OfNat.ofNat n) = f x + OfNat.ofNat n := map_add_nat f x n @[scoped simp] theorem map_const [AddZeroClass G] [Add H] [AddConstMapClass F G H a b] (f : F) : f a = f 0 + b := by simpa using map_add_const f 0 theorem map_one [AddZeroClass G] [One G] [Add H] [AddConstMapClass F G H 1 b] (f : F) : f 1 = f 0 + b := map_const f @[scoped simp] theorem map_nsmul_const [AddMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (n : ℕ) : f (n • a) = f 0 + n • b := by simpa using map_add_nsmul f 0 n @[scoped simp] theorem map_nat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) : f n = f 0 + n • b := by simpa using map_add_nat' f 0 n theorem map_ofNat' [AddMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) [n.AtLeastTwo] : f (OfNat.ofNat n) = f 0 + (OfNat.ofNat n : ℕ) • b := map_nat' f n theorem map_nat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) : f n = f 0 + n := by simp theorem map_ofNat [AddMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) [n.AtLeastTwo] : f (OfNat.ofNat n) = f 0 + OfNat.ofNat n := map_nat f n @[scoped simp] theorem map_const_add [AddCommSemigroup G] [Add H] [AddConstMapClass F G H a b] (f : F) (x : G) : f (a + x) = f x + b := by rw [add_comm, map_add_const] theorem map_one_add [AddCommMonoidWithOne G] [Add H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (1 + x) = f x + b := map_const_add f x @[scoped simp] theorem map_nsmul_add [AddCommMonoid G] [AddMonoid H] [AddConstMapClass F G H a b] (f : F) (n : ℕ) (x : G) : f (n • a + x) = f x + n • b := by rw [add_comm, map_add_nsmul] @[scoped simp] theorem map_nat_add' [AddCommMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) (x : G) : f (↑n + x) = f x + n • b := by simpa using map_nsmul_add f n x theorem map_ofNat_add' [AddCommMonoidWithOne G] [AddMonoid H] [AddConstMapClass F G H 1 b] (f : F) (n : ℕ) [n.AtLeastTwo] (x : G) : f (OfNat.ofNat n + x) = f x + OfNat.ofNat n • b := map_nat_add' f n x theorem map_nat_add [AddCommMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) (x : G) : f (↑n + x) = f x + n := by simp theorem map_ofNat_add [AddCommMonoidWithOne G] [AddMonoidWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℕ) [n.AtLeastTwo] (x : G) : f (OfNat.ofNat n + x) = f x + OfNat.ofNat n := map_nat_add f n x @[scoped simp] theorem map_sub_nsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℕ) : f (x - n • a) = f x - n • b := by conv_rhs => rw [← sub_add_cancel x (n • a), map_add_nsmul, add_sub_cancel_right] @[scoped simp] theorem map_sub_const [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) : f (x - a) = f x - b := by simpa using map_sub_nsmul f x 1 theorem map_sub_one [AddGroup G] [One G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) : f (x - 1) = f x - b := map_sub_const f x @[scoped simp] theorem map_sub_nat' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) : f (x - n) = f x - n • b := by simpa using map_sub_nsmul f x n @[scoped simp] theorem map_sub_ofNat' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℕ) [n.AtLeastTwo] : f (x - no_index (OfNat.ofNat n)) = f x - OfNat.ofNat n • b := map_sub_nat' f x n @[scoped simp] theorem map_add_zsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) : ∀ n : ℤ, f (x + n • a) = f x + n • b | (n : ℕ) => by simp | .negSucc n => by simp [← sub_eq_add_neg] @[scoped simp] theorem map_zsmul_const [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (n : ℤ) : f (n • a) = f 0 + n • b := by simpa using map_add_zsmul f 0 n @[scoped simp] theorem map_add_int' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℤ) : f (x + n) = f x + n • b := by rw [← map_add_zsmul f x n, zsmul_one] theorem map_add_int [AddGroupWithOne G] [AddGroupWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℤ) : f (x + n) = f x + n := by simp @[scoped simp] theorem map_sub_zsmul [AddGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (x : G) (n : ℤ) : f (x - n • a) = f x - n • b := by simpa [sub_eq_add_neg] using map_add_zsmul f x (-n) @[scoped simp] theorem map_sub_int' [AddGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (x : G) (n : ℤ) : f (x - n) = f x - n • b := by rw [← map_sub_zsmul, zsmul_one] theorem map_sub_int [AddGroupWithOne G] [AddGroupWithOne H] [AddConstMapClass F G H 1 1] (f : F) (x : G) (n : ℤ) : f (x - n) = f x - n := by simp @[scoped simp] theorem map_zsmul_add [AddCommGroup G] [AddGroup H] [AddConstMapClass F G H a b] (f : F) (n : ℤ) (x : G) : f (n • a + x) = f x + n • b := by rw [add_comm, map_add_zsmul] @[scoped simp] theorem map_int_add' [AddCommGroupWithOne G] [AddGroup H] [AddConstMapClass F G H 1 b] (f : F) (n : ℤ) (x : G) : f (↑n + x) = f x + n • b := by rw [← map_zsmul_add, zsmul_one] theorem map_int_add [AddCommGroupWithOne G] [AddGroupWithOne H] [AddConstMapClass F G H 1 1] (f : F) (n : ℤ) (x : G) : f (↑n + x) = f x + n := by simp theorem map_fract {R : Type*} [LinearOrderedRing R] [FloorRing R] [AddGroup H] [FunLike F R H] [AddConstMapClass F R H 1 b] (f : F) (x : R) : f (Int.fract x) = f x - ⌊x⌋ • b := map_sub_int' .. /-- Auxiliary lemmas for the "monotonicity on a fundamental interval implies monotonicity" lemmas. We formulate it for any relation so that the proof works both for `Monotone` and `StrictMono`. -/ protected theorem rel_map_of_Icc [LinearOrderedAddCommGroup G] [Archimedean G] [AddGroup H] [AddConstMapClass F G H a b] {f : F} {R : H → H → Prop} [IsTrans H R] [hR : CovariantClass H H (fun x y ↦ y + x) R] (ha : 0 < a) {l : G} (hf : ∀ x ∈ Icc l (l + a), ∀ y ∈ Icc l (l + a), x < y → R (f x) (f y)) : ((· < ·) ⇒ R) f f := fun x y hxy ↦ by replace hR := hR.elim have ha' : 0 ≤ a := ha.le -- Shift both points by `m • a` so that `l ≤ x < l + a` wlog hx : x ∈ Ico l (l + a) generalizing x y · rcases existsUnique_sub_zsmul_mem_Ico ha x l with ⟨m, hm, -⟩ suffices R (f (x - m • a)) (f (y - m • a)) by simpa using hR (m • b) this exact this _ _ (by simpa) hm · -- Now find `n` such that `l + n • a < y ≤ l + (n + 1) • a` rcases existsUnique_sub_zsmul_mem_Ioc ha y l with ⟨n, hny, -⟩ rcases lt_trichotomy n 0 with hn | rfl | hn · -- Since `l ≤ x ≤ y`, the case `n < 0` is impossible refine absurd ?_ hxy.not_le calc y ≤ l + a + n • a := sub_le_iff_le_add.1 hny.2 _ = l + (n + 1) • a := by rw [add_comm n, add_smul, one_smul, add_assoc] _ ≤ l + 0 • a := add_le_add_left (zsmul_le_zsmul ha.le (by omega)) _ _ ≤ x := by simpa using hx.1 · -- If `n = 0`, then `l < y ≤ l + a`, hence we can apply the assumption exact hf x (Ico_subset_Icc_self hx) y (by simpa using Ioc_subset_Icc_self hny) hxy · -- In the remaining case `0 < n` we use transitivity. -- If `R = (· < ·)`, then the proof looks like -- `f x < f (l + a) ≤ f (l + n • a) < f y` trans f (l + (1 : ℤ) • a) · rw [one_zsmul] exact hf x (Ico_subset_Icc_self hx) (l + a) (by simpa) hx.2 have hy : R (f (l + n • a)) (f y) := by rw [← sub_add_cancel y (n • a), map_add_zsmul, map_add_zsmul] refine hR _ <| hf _ ?_ _ (Ioc_subset_Icc_self hny) hny.1; simpa rw [← Int.add_one_le_iff, zero_add] at hn rcases hn.eq_or_lt with rfl | hn; · assumption trans f (l + n • a) · refine Int.rel_of_forall_rel_succ_of_lt R (f := (f <| l + · • a)) (fun k ↦ ?_) hn simp_rw [add_comm k 1, add_zsmul, ← add_assoc, one_zsmul, map_add_zsmul] refine hR (k • b) (hf _ ?_ _ ?_ ?_) <;> simpa · assumption theorem monotone_iff_Icc [LinearOrderedAddCommGroup G] [Archimedean G] [OrderedAddCommGroup H] [AddConstMapClass F G H a b] {f : F} (ha : 0 < a) (l : G) : Monotone f ↔ MonotoneOn f (Icc l (l + a)) := ⟨(Monotone.monotoneOn · _), fun hf ↦ monotone_iff_forall_lt.2 <| AddConstMapClass.rel_map_of_Icc ha fun _x hx _y hy hxy ↦ hf hx hy hxy.le⟩ theorem antitone_iff_Icc [LinearOrderedAddCommGroup G] [Archimedean G] [OrderedAddCommGroup H] [AddConstMapClass F G H a b] {f : F} (ha : 0 < a) (l : G) : Antitone f ↔ AntitoneOn f (Icc l (l + a)) := monotone_iff_Icc (H := Hᵒᵈ) ha l theorem strictMono_iff_Icc [LinearOrderedAddCommGroup G] [Archimedean G] [OrderedAddCommGroup H] [AddConstMapClass F G H a b] {f : F} (ha : 0 < a) (l : G) : StrictMono f ↔ StrictMonoOn f (Icc l (l + a)) := ⟨(StrictMono.strictMonoOn · _), AddConstMapClass.rel_map_of_Icc ha⟩ theorem strictAnti_iff_Icc [LinearOrderedAddCommGroup G] [Archimedean G] [OrderedAddCommGroup H] [AddConstMapClass F G H a b] {f : F} (ha : 0 < a) (l : G) : StrictAnti f ↔ StrictAntiOn f (Icc l (l + a)) := strictMono_iff_Icc (H := Hᵒᵈ) ha l end AddConstMapClass open AddConstMapClass namespace AddConstMap section Add variable {G H : Type*} [Add G] [Add H] {a : G} {b : H} /-! ### Coercion to function -/ instance : FunLike (G →+c[a, b] H) G H where coe := AddConstMap.toFun coe_injective' | ⟨_, _⟩, ⟨_, _⟩, rfl => rfl @[simp, push_cast] theorem coe_mk (f : G → H) (hf) : ⇑(mk f hf : G →+c[a, b] H) = f := rfl @[simp] theorem mk_coe (f : G →+c[a, b] H) : mk f f.2 = f := rfl @[simp] theorem toFun_eq_coe (f : G →+c[a, b] H) : f.toFun = f := rfl instance : AddConstMapClass (G →+c[a, b] H) G H a b where map_add_const f := f.map_add_const' @[ext] protected theorem ext {f g : G →+c[a, b] H} (h : ∀ x, f x = g x) : f = g := DFunLike.ext _ _ h initialize_simps_projections AddConstMap (toFun → coe, as_prefix coe) /-! ### Constructions about `G →+c[a, b] H` -/ /-- The identity map as `G →+c[a, a] G`. -/ @[simps (config := .asFn)] protected def id : G →+c[a, a] G := ⟨id, fun _ ↦ rfl⟩ instance : Inhabited (G →+c[a, a] G) := ⟨.id⟩ /-- Composition of two `AddConstMap`s. -/ @[simps (config := .asFn)] def comp {K : Type*} [Add K] {c : K} (g : H →+c[b, c] K) (f : G →+c[a, b] H) : G →+c[a, c] K := ⟨g ∘ f, by simp⟩ @[simp] theorem comp_id (f : G →+c[a, b] H) : f.comp .id = f := rfl @[simp] theorem id_comp (f : G →+c[a, b] H) : .comp .id f = f := rfl /-- Change constants `a` and `b` in `(f : G →+c[a, b] H)` to improve definitional equalities. -/ @[simps (config := .asFn)] def replaceConsts (f : G →+c[a, b] H) (a' b') (ha : a = a') (hb : b = b') : G →+c[a', b'] H where toFun := f map_add_const' := ha ▸ hb ▸ f.map_add_const' /-! ### Additive action on `G →+c[a, b] H` -/ /-- If `f` is an `AddConstMap`, then so is `(c +ᵥ f ·)`. -/ instance {K : Type*} [VAdd K H] [VAddAssocClass K H H] : VAdd K (G →+c[a, b] H) := ⟨fun c f ↦ ⟨c +ᵥ ⇑f, fun x ↦ by simp [vadd_add_assoc]⟩⟩ @[simp, norm_cast] theorem coe_vadd {K : Type*} [VAdd K H] [VAddAssocClass K H H] (c : K) (f : G →+c[a, b] H) : ⇑(c +ᵥ f) = c +ᵥ ⇑f := rfl instance {K : Type*} [AddMonoid K] [AddAction K H] [VAddAssocClass K H H] : AddAction K (G →+c[a, b] H) := DFunLike.coe_injective.addAction _ coe_vadd /-! ### Monoid structure on endomorphisms `G →+c[a, a] G` -/ instance : Mul (G →+c[a, a] G) := ⟨comp⟩ instance : One (G →+c[a, a] G) := ⟨.id⟩ instance : Pow (G →+c[a, a] G) ℕ where pow f n := ⟨f^[n], Commute.iterate_left (AddConstMapClass.semiconj f) _⟩ instance : Monoid (G →+c[a, a] G) := DFunLike.coe_injective.monoid (M₂ := Function.End G) _ rfl (fun _ _ ↦ rfl) fun _ _ ↦ rfl theorem mul_def (f g : G →+c[a, a] G) : f * g = f.comp g := rfl @[simp, push_cast] theorem coe_mul (f g : G →+c[a, a] G) : ⇑(f * g) = f ∘ g := rfl theorem one_def : (1 : G →+c[a, a] G) = .id := rfl @[simp, push_cast] theorem coe_one : ⇑(1 : G →+c[a, a] G) = id := rfl @[simp, push_cast] theorem coe_pow (f : G →+c[a, a] G) (n : ℕ) : ⇑(f ^ n) = f^[n] := rfl theorem pow_apply (f : G →+c[a, a] G) (n : ℕ) (x : G) : (f ^ n) x = f^[n] x := rfl /-- Coercion to functions as a monoid homomorphism to `Function.End G`. -/ @[simps (config := .asFn)] def toEnd : (G →+c[a, a] G) →* Function.End G where toFun := DFunLike.coe map_mul' _ _ := rfl map_one' := rfl end Add section AddZeroClass variable {G H K : Type*} [Add G] [AddZeroClass H] {a : G} {b : H} /-! ### Multiplicative action on `(b : H) × (G →+c[a, b] H)` If `K` acts distributively on `H`, then for each `f : G →+c[a, b] H` we define `(AddConstMap.smul c f : G →+c[a, c • b] H)`. One can show that this defines a multiplicative action of `K` on `(b : H) × (G →+c[a, b] H)` but we don't do this at the moment because we don't need this. -/ /-- Pointwise scalar multiplication of `f : G →+c[a, b] H` as a map `G →+c[a, c • b] H`. -/ @[simps (config := .asFn)] def smul [DistribSMul K H] (c : K) (f : G →+c[a, b] H) : G →+c[a, c • b] H where toFun := c • ⇑f map_add_const' x := by simp [smul_add] end AddZeroClass section AddMonoid variable {G : Type*} [AddMonoid G] {a : G} /-- The map that sends `c` to a translation by `c` as a monoid homomorphism from `Multiplicative G` to `G →+c[a, a] G`. -/ @[simps! (config := .asFn)] def addLeftHom : Multiplicative G →* (G →+c[a, a] G) where toFun c := Multiplicative.toAdd c +ᵥ .id map_one' := by ext; apply zero_add map_mul' _ _ := by ext; apply add_assoc end AddMonoid section AddCommGroup variable {G H : Type*} [AddCommGroup G] [AddCommGroup H] {a : G} {b : H} /-- If `f : G → H` is an `AddConstMap`, then so is `fun x ↦ -f (-x)`. -/ @[simps! apply_coe] def conjNeg : (G →+c[a, b] H) ≃ (G →+c[a, b] H) := Involutive.toPerm (fun f ↦ ⟨fun x ↦ - f (-x), fun _ ↦ by simp [neg_add_eq_sub]⟩) fun _ ↦ AddConstMap.ext fun _ ↦ by simp @[simp] theorem conjNeg_symm : (conjNeg (a := a) (b := b)).symm = conjNeg := rfl end AddCommGroup section FloorRing variable {R G : Type*} [LinearOrderedRing R] [FloorRing R] [AddGroup G] (a : G) /-- A map `f : R →+c[1, a] G` is defined by its values on `Set.Ico 0 1`. -/ def mkFract : (Ico (0 : R) 1 → G) ≃ (R →+c[1, a] G) where toFun f := ⟨fun x ↦ f ⟨Int.fract x, Int.fract_nonneg _, Int.fract_lt_one _⟩ + ⌊x⌋ • a, fun x ↦ by simp [add_one_zsmul, add_assoc]⟩ invFun f x := f x left_inv _ := by ext x; simp [Int.fract_eq_self.2 x.2, Int.floor_eq_zero_iff.2 x.2] right_inv f := by ext x; simp [map_fract] end FloorRing end AddConstMap
Algebra\AddConstMap\Equiv.lean
/- Copyright (c) 2024 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.AddConstMap.Basic import Mathlib.GroupTheory.Perm.Basic /-! # Equivalences conjugating `(· + a)` to `(· + b)` In this file we define `AddConstEquiv G H a b` (notation: `G ≃+c[a, b] H`) to be the type of equivalences such that `∀ x, f (x + a) = f x + b`. We also define the corresponding typeclass and prove some basic properties. -/ open Function open scoped AddConstMap /-- An equivalence between `G` and `H` conjugating `(· + a)` to `(· + b)`. -/ structure AddConstEquiv (G H : Type*) [Add G] [Add H] (a : G) (b : H) extends G ≃ H, G →+c[a, b] H /-- Interpret an `AddConstEquiv` as an `Equiv`. -/ add_decl_doc AddConstEquiv.toEquiv /-- Interpret an `AddConstEquiv` as an `AddConstMap`. -/ add_decl_doc AddConstEquiv.toAddConstMap @[inherit_doc] scoped [AddConstMap] notation:25 G " ≃+c[" a ", " b "] " H => AddConstEquiv G H a b namespace AddConstEquiv variable {G H K : Type*} [Add G] [Add H] [Add K] {a : G} {b : H} {c : K} lemma toEquiv_injective : Injective (toEquiv : (G ≃+c[a, b] H) → G ≃ H) | ⟨_, _⟩, ⟨_, _⟩, rfl => rfl instance {G H : Type*} [Add G] [Add H] {a : G} {b : H} : EquivLike (G ≃+c[a, b] H) G H where coe f := f.toEquiv inv f := f.toEquiv.symm left_inv f := f.left_inv right_inv f := f.right_inv coe_injective' _ _ h _ := toEquiv_injective <| DFunLike.ext' h instance {G H : Type*} [Add G] [Add H] {a : G} {b : H} : AddConstMapClass (G ≃+c[a, b] H) G H a b where map_add_const f x := f.map_add_const' x @[ext] lemma ext {e₁ e₂ : G ≃+c[a, b] H} (h : ∀ x, e₁ x = e₂ x) : e₁ = e₂ := DFunLike.ext _ _ h @[simp] lemma toEquiv_inj {e₁ e₂ : G ≃+c[a, b] H} : e₁.toEquiv = e₂.toEquiv ↔ e₁ = e₂ := toEquiv_injective.eq_iff @[simp] lemma coe_toEquiv (e : G ≃+c[a, b] H) : ⇑e.toEquiv = e := rfl /-- Inverse map of an `AddConstEquiv`, as an `AddConstEquiv`. -/ def symm (e : G ≃+c[a, b] H) : H ≃+c[b, a] G where toEquiv := e.toEquiv.symm map_add_const' := (AddConstMapClass.semiconj e).inverse_left e.left_inv e.right_inv /-- A custom projection for `simps`. -/ def Simps.symm_apply (e : G ≃+c[a, b] H) : H → G := e.symm initialize_simps_projections AddConstEquiv (toFun → apply, invFun → symm_apply) @[simp] lemma symm_symm (e : G ≃+c[a, b] H) : e.symm.symm = e := rfl /-- The identity map as an `AddConstEquiv`. -/ @[simps! toEquiv apply] def refl (a : G) : G ≃+c[a, a] G where toEquiv := .refl G map_add_const' _ := rfl @[simp] lemma symm_refl (a : G) : (refl a).symm = refl a := rfl /-- Composition of `AddConstEquiv`s, as an `AddConstEquiv`. -/ @[simps! (config := { simpRhs := true }) toEquiv apply] def trans (e₁ : G ≃+c[a, b] H) (e₂ : H ≃+c[b, c] K) : G ≃+c[a, c] K where toEquiv := e₁.toEquiv.trans e₂.toEquiv map_add_const' := (AddConstMapClass.semiconj e₁).trans (AddConstMapClass.semiconj e₂) @[simp] lemma trans_refl (e : G ≃+c[a, b] H) : e.trans (.refl b) = e := rfl @[simp] lemma refl_trans (e : G ≃+c[a, b] H) : (refl a).trans e = e := rfl @[simp] lemma self_trans_symm (e : G ≃+c[a, b] H) : e.trans e.symm = .refl a := toEquiv_injective e.toEquiv.self_trans_symm @[simp] lemma symm_trans_self (e : G ≃+c[a, b] H) : e.symm.trans e = .refl b := toEquiv_injective e.toEquiv.symm_trans_self instance instOne : One (G ≃+c[a, a] G) := ⟨.refl _⟩ instance instMul : Mul (G ≃+c[a, a] G) := ⟨fun f g ↦ g.trans f⟩ instance instInv : Inv (G ≃+c[a, a] G) := ⟨.symm⟩ instance instDiv : Div (G ≃+c[a, a] G) := ⟨fun f g ↦ f * g⁻¹⟩ instance instPowNat : Pow (G ≃+c[a, a] G) ℕ where pow e n := ⟨e^n, (e.toAddConstMap^n).map_add_const'⟩ instance instPowInt : Pow (G ≃+c[a, a] G) ℤ where pow e n := ⟨e^n, match n with | .ofNat n => (e^n).map_add_const' | .negSucc n => (e.symm^(n + 1)).map_add_const'⟩ instance instGroup : Group (G ≃+c[a, a] G) := toEquiv_injective.group _ rfl (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) fun _ _ ↦ rfl /-- Projection from `G ≃+c[a, a] G` to permutations `G ≃ G`, as a monoid homomorphism. -/ @[simps! apply] def toPerm : (G ≃+c[a, a] G) →* Equiv.Perm G := .mk' toEquiv fun _ _ ↦ rfl /-- Projection from `G ≃+c[a, a] G` to `G →+c[a, a] G`, as a monoid homomorphism. -/ @[simps! apply] def toAddConstMapHom : (G ≃+c[a, a] G) →* (G →+c[a, a] G) where toFun := toAddConstMap map_mul' _ _ := rfl map_one' := rfl /-- Group equivalence between `G ≃+c[a, a] G` and the units of `G →+c[a, a] G`. -/ @[simps!] def equivUnits : (G ≃+c[a, a] G) ≃* (G →+c[a, a] G)ˣ where toFun := toAddConstMapHom.toHomUnits invFun u := { toEquiv := Equiv.Perm.equivUnitsEnd.symm <| Units.map AddConstMap.toEnd u map_add_const' := u.1.2 } left_inv _ := rfl right_inv _ := rfl map_mul' _ _ := rfl end AddConstEquiv
Algebra\Algebra\Basic.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.CharZero.Lemmas import Mathlib.Algebra.Module.Equiv.Basic import Mathlib.Algebra.Module.Submodule.Ker import Mathlib.Algebra.Module.Submodule.RestrictScalars import Mathlib.Algebra.Module.ULift import Mathlib.Algebra.Ring.Subring.Basic import Mathlib.Data.Nat.Cast.Order.Basic import Mathlib.Data.Int.CharZero /-! # Further basic results about `Algebra`. This file could usefully be split further. -/ universe u v w u₁ v₁ namespace Algebra variable {R : Type u} {S : Type v} {A : Type w} {B : Type*} section Semiring variable [CommSemiring R] [CommSemiring S] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] section PUnit instance _root_.PUnit.algebra : Algebra R PUnit.{v + 1} where toFun _ := PUnit.unit map_one' := rfl map_mul' _ _ := rfl map_zero' := rfl map_add' _ _ := rfl commutes' _ _ := rfl smul_def' _ _ := rfl @[simp] theorem algebraMap_pUnit (r : R) : algebraMap R PUnit r = PUnit.unit := rfl end PUnit section ULift instance _root_.ULift.algebra : Algebra R (ULift A) := { ULift.module', (ULift.ringEquiv : ULift A ≃+* A).symm.toRingHom.comp (algebraMap R A) with toFun := fun r => ULift.up (algebraMap R A r) commutes' := fun r x => ULift.down_injective <| Algebra.commutes r x.down smul_def' := fun r x => ULift.down_injective <| Algebra.smul_def' r x.down } theorem _root_.ULift.algebraMap_eq (r : R) : algebraMap R (ULift A) r = ULift.up (algebraMap R A r) := rfl @[simp] theorem _root_.ULift.down_algebraMap (r : R) : (algebraMap R (ULift A) r).down = algebraMap R A r := rfl end ULift /-- Algebra over a subsemiring. This builds upon `Subsemiring.module`. -/ instance ofSubsemiring (S : Subsemiring R) : Algebra S A where toRingHom := (algebraMap R A).comp S.subtype smul := (· • ·) commutes' r x := Algebra.commutes (r : R) x smul_def' r x := Algebra.smul_def (r : R) x theorem algebraMap_ofSubsemiring (S : Subsemiring R) : (algebraMap S R : S →+* R) = Subsemiring.subtype S := rfl theorem coe_algebraMap_ofSubsemiring (S : Subsemiring R) : (algebraMap S R : S → R) = Subtype.val := rfl theorem algebraMap_ofSubsemiring_apply (S : Subsemiring R) (x : S) : algebraMap S R x = x := rfl /-- Algebra over a subring. This builds upon `Subring.module`. -/ instance ofSubring {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (S : Subring R) : Algebra S A where -- Porting note: don't use `toSubsemiring` because of a timeout toRingHom := (algebraMap R A).comp S.subtype smul := (· • ·) commutes' r x := Algebra.commutes (r : R) x smul_def' r x := Algebra.smul_def (r : R) x theorem algebraMap_ofSubring {R : Type*} [CommRing R] (S : Subring R) : (algebraMap S R : S →+* R) = Subring.subtype S := rfl theorem coe_algebraMap_ofSubring {R : Type*} [CommRing R] (S : Subring R) : (algebraMap S R : S → R) = Subtype.val := rfl theorem algebraMap_ofSubring_apply {R : Type*} [CommRing R] (S : Subring R) (x : S) : algebraMap S R x = x := rfl /-- Explicit characterization of the submonoid map in the case of an algebra. `S` is made explicit to help with type inference -/ def algebraMapSubmonoid (S : Type*) [Semiring S] [Algebra R S] (M : Submonoid R) : Submonoid S := M.map (algebraMap R S) theorem mem_algebraMapSubmonoid_of_mem {S : Type*} [Semiring S] [Algebra R S] {M : Submonoid R} (x : M) : algebraMap R S x ∈ algebraMapSubmonoid S M := Set.mem_image_of_mem (algebraMap R S) x.2 end Semiring section CommSemiring variable [CommSemiring R] theorem mul_sub_algebraMap_commutes [Ring A] [Algebra R A] (x : A) (r : R) : x * (x - algebraMap R A r) = (x - algebraMap R A r) * x := by rw [mul_sub, ← commutes, sub_mul] theorem mul_sub_algebraMap_pow_commutes [Ring A] [Algebra R A] (x : A) (r : R) (n : ℕ) : x * (x - algebraMap R A r) ^ n = (x - algebraMap R A r) ^ n * x := by induction' n with n ih · simp · rw [pow_succ', ← mul_assoc, mul_sub_algebraMap_commutes, mul_assoc, ih, ← mul_assoc] end CommSemiring section Ring variable [CommRing R] variable (R) /-- A `Semiring` that is an `Algebra` over a commutative ring carries a natural `Ring` structure. See note [reducible non-instances]. -/ abbrev semiringToRing [Semiring A] [Algebra R A] : Ring A := { __ := (inferInstance : Semiring A) __ := Module.addCommMonoidToAddCommGroup R intCast := fun z => algebraMap R A z intCast_ofNat := fun z => by simp only [Int.cast_natCast, map_natCast] intCast_negSucc := fun z => by simp } end Ring end Algebra open scoped Algebra namespace Module variable (R : Type u) (S : Type v) (M : Type w) variable [CommSemiring R] [Semiring S] [AddCommMonoid M] [Module R M] [Module S M] variable [SMulCommClass S R M] [SMul R S] [IsScalarTower R S M] instance End.instAlgebra : Algebra R (Module.End S M) := Algebra.ofModule smul_mul_assoc fun r f g => (smul_comm r f g).symm -- to prove this is a special case of the above example : Algebra R (Module.End R M) := End.instAlgebra _ _ _ theorem algebraMap_end_eq_smul_id (a : R) : algebraMap R (End S M) a = a • LinearMap.id := rfl @[simp] theorem algebraMap_end_apply (a : R) (m : M) : algebraMap R (End S M) a m = a • m := rfl @[simp] theorem ker_algebraMap_end (K : Type u) (V : Type v) [Field K] [AddCommGroup V] [Module K V] (a : K) (ha : a ≠ 0) : LinearMap.ker ((algebraMap K (End K V)) a) = ⊥ := LinearMap.ker_smul _ _ ha section variable {R M} theorem End_algebraMap_isUnit_inv_apply_eq_iff {x : R} (h : IsUnit (algebraMap R (Module.End S M) x)) (m m' : M) : (↑(h.unit⁻¹) : Module.End S M) m = m' ↔ m = x • m' := { mp := fun H => ((congr_arg h.unit H).symm.trans (End_isUnit_apply_inv_apply_of_isUnit h _)).symm mpr := fun H => H.symm ▸ by apply_fun ⇑h.unit.val using ((Module.End_isUnit_iff _).mp h).injective erw [End_isUnit_apply_inv_apply_of_isUnit] rfl } theorem End_algebraMap_isUnit_inv_apply_eq_iff' {x : R} (h : IsUnit (algebraMap R (Module.End S M) x)) (m m' : M) : m' = (↑h.unit⁻¹ : Module.End S M) m ↔ m = x • m' := { mp := fun H => ((congr_arg h.unit H).trans (End_isUnit_apply_inv_apply_of_isUnit h _)).symm mpr := fun H => H.symm ▸ by apply_fun (↑h.unit : M → M) using ((Module.End_isUnit_iff _).mp h).injective erw [End_isUnit_apply_inv_apply_of_isUnit] rfl } end end Module namespace LinearMap variable {R : Type*} {A : Type*} {B : Type*} [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] /-- An alternate statement of `LinearMap.map_smul` for when `algebraMap` is more convenient to work with than `•`. -/ theorem map_algebraMap_mul (f : A →ₗ[R] B) (a : A) (r : R) : f (algebraMap R A r * a) = algebraMap R B r * f a := by rw [← Algebra.smul_def, ← Algebra.smul_def, map_smul] theorem map_mul_algebraMap (f : A →ₗ[R] B) (a : A) (r : R) : f (a * algebraMap R A r) = f a * algebraMap R B r := by rw [← Algebra.commutes, ← Algebra.commutes, map_algebraMap_mul] end LinearMap section Nat variable {R : Type*} [Semiring R] -- Lower the priority so that `Algebra.id` is picked most of the time when working with -- `ℕ`-algebras. -- TODO: is this still needed? /-- Semiring ⥤ ℕ-Alg -/ instance (priority := 99) Semiring.toNatAlgebra : Algebra ℕ R where commutes' := Nat.cast_commute smul_def' _ _ := nsmul_eq_mul _ _ toRingHom := Nat.castRingHom R instance nat_algebra_subsingleton : Subsingleton (Algebra ℕ R) := ⟨fun P Q => by ext; simp⟩ end Nat section Int variable (R : Type*) [Ring R] -- Lower the priority so that `Algebra.id` is picked most of the time when working with -- `ℤ`-algebras. -- TODO: is this still needed? /-- Ring ⥤ ℤ-Alg -/ instance (priority := 99) Ring.toIntAlgebra : Algebra ℤ R where commutes' := Int.cast_commute smul_def' _ _ := zsmul_eq_mul _ _ toRingHom := Int.castRingHom R /-- A special case of `eq_intCast'` that happens to be true definitionally -/ @[simp] theorem algebraMap_int_eq : algebraMap ℤ R = Int.castRingHom R := rfl variable {R} instance int_algebra_subsingleton : Subsingleton (Algebra ℤ R) := ⟨fun P Q => Algebra.algebra_ext P Q <| RingHom.congr_fun <| Subsingleton.elim _ _⟩ end Int namespace NoZeroSMulDivisors variable {R A : Type*} open Algebra /-- If `algebraMap R A` is injective and `A` has no zero divisors, `R`-multiples in `A` are zero only if one of the factors is zero. Cannot be an instance because there is no `Injective (algebraMap R A)` typeclass. -/ theorem of_algebraMap_injective [CommSemiring R] [Semiring A] [Algebra R A] [NoZeroDivisors A] (h : Function.Injective (algebraMap R A)) : NoZeroSMulDivisors R A := ⟨fun hcx => (mul_eq_zero.mp ((smul_def _ _).symm.trans hcx)).imp_left (map_eq_zero_iff (algebraMap R A) h).mp⟩ variable (R A) theorem algebraMap_injective [CommRing R] [Ring A] [Nontrivial A] [Algebra R A] [NoZeroSMulDivisors R A] : Function.Injective (algebraMap R A) := by simpa only [algebraMap_eq_smul_one'] using smul_left_injective R one_ne_zero theorem _root_.NeZero.of_noZeroSMulDivisors (n : ℕ) [CommRing R] [NeZero (n : R)] [Ring A] [Nontrivial A] [Algebra R A] [NoZeroSMulDivisors R A] : NeZero (n : A) := NeZero.nat_of_injective <| NoZeroSMulDivisors.algebraMap_injective R A variable {R A} theorem iff_algebraMap_injective [CommRing R] [Ring A] [IsDomain A] [Algebra R A] : NoZeroSMulDivisors R A ↔ Function.Injective (algebraMap R A) := ⟨@NoZeroSMulDivisors.algebraMap_injective R A _ _ _ _, NoZeroSMulDivisors.of_algebraMap_injective⟩ -- see note [lower instance priority] instance (priority := 100) CharZero.noZeroSMulDivisors_nat [Semiring R] [NoZeroDivisors R] [CharZero R] : NoZeroSMulDivisors ℕ R := NoZeroSMulDivisors.of_algebraMap_injective <| (algebraMap ℕ R).injective_nat -- see note [lower instance priority] instance (priority := 100) CharZero.noZeroSMulDivisors_int [Ring R] [NoZeroDivisors R] [CharZero R] : NoZeroSMulDivisors ℤ R := NoZeroSMulDivisors.of_algebraMap_injective <| (algebraMap ℤ R).injective_int section Field variable [Field R] [Semiring A] [Algebra R A] -- see note [lower instance priority] instance (priority := 100) Algebra.noZeroSMulDivisors [Nontrivial A] [NoZeroDivisors A] : NoZeroSMulDivisors R A := NoZeroSMulDivisors.of_algebraMap_injective (algebraMap R A).injective end Field end NoZeroSMulDivisors section IsScalarTower variable {R : Type*} [CommSemiring R] variable (A : Type*) [Semiring A] [Algebra R A] variable {M : Type*} [AddCommMonoid M] [Module A M] [Module R M] [IsScalarTower R A M] variable {N : Type*} [AddCommMonoid N] [Module A N] [Module R N] [IsScalarTower R A N] theorem algebra_compatible_smul (r : R) (m : M) : r • m = (algebraMap R A) r • m := by rw [← one_smul A m, ← smul_assoc, Algebra.smul_def, mul_one, one_smul] @[simp] theorem algebraMap_smul (r : R) (m : M) : (algebraMap R A) r • m = r • m := (algebra_compatible_smul A r m).symm theorem NoZeroSMulDivisors.trans (R A M : Type*) [CommRing R] [Ring A] [IsDomain A] [Algebra R A] [AddCommGroup M] [Module R M] [Module A M] [IsScalarTower R A M] [NoZeroSMulDivisors R A] [NoZeroSMulDivisors A M] : NoZeroSMulDivisors R M := by refine ⟨fun {r m} h => ?_⟩ rw [algebra_compatible_smul A r m] at h cases' smul_eq_zero.1 h with H H · have : Function.Injective (algebraMap R A) := NoZeroSMulDivisors.iff_algebraMap_injective.1 inferInstance left exact (injective_iff_map_eq_zero _).1 this _ H · right exact H variable {A} -- see Note [lower instance priority] -- priority manually adjusted in #11980, as it is a very common path instance (priority := 120) IsScalarTower.to_smulCommClass : SMulCommClass R A M := ⟨fun r a m => by rw [algebra_compatible_smul A r (a • m), smul_smul, Algebra.commutes, mul_smul, ← algebra_compatible_smul]⟩ -- see Note [lower instance priority] -- priority manually adjusted in #11980, as it is a very common path instance (priority := 110) IsScalarTower.to_smulCommClass' : SMulCommClass A R M := SMulCommClass.symm _ _ _ -- see Note [lower instance priority] instance (priority := 200) Algebra.to_smulCommClass {R A} [CommSemiring R] [Semiring A] [Algebra R A] : SMulCommClass R A A := IsScalarTower.to_smulCommClass theorem smul_algebra_smul_comm (r : R) (a : A) (m : M) : a • r • m = r • a • m := smul_comm _ _ _ namespace LinearMap variable (R) -- Porting note (#11215): TODO: generalize to `CompatibleSMul` /-- `A`-linearly coerce an `R`-linear map from `M` to `A` to a function, given an algebra `A` over a commutative semiring `R` and `M` a module over `R`. -/ def ltoFun (R : Type u) (M : Type v) (A : Type w) [CommSemiring R] [AddCommMonoid M] [Module R M] [CommSemiring A] [Algebra R A] : (M →ₗ[R] A) →ₗ[A] M → A where toFun f := f.toFun map_add' _ _ := rfl map_smul' _ _ := rfl end LinearMap end IsScalarTower /-! TODO: The following lemmas no longer involve `Algebra` at all, and could be moved closer to `Algebra/Module/Submodule.lean`. Currently this is tricky because `ker`, `range`, `⊤`, and `⊥` are all defined in `LinearAlgebra/Basic.lean`. -/ section Module variable (R : Type*) {S M N : Type*} [Semiring R] [Semiring S] [SMul R S] variable [AddCommMonoid M] [Module R M] [Module S M] [IsScalarTower R S M] variable [AddCommMonoid N] [Module R N] [Module S N] [IsScalarTower R S N] @[simp] theorem LinearMap.ker_restrictScalars (f : M →ₗ[S] N) : LinearMap.ker (f.restrictScalars R) = f.ker.restrictScalars R := rfl end Module example {R A} [CommSemiring R] [Semiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] : Algebra R A := Algebra.ofModule smul_mul_assoc mul_smul_comm section invertibility variable {R A B : Type*} variable [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] /-- If there is a linear map `f : A →ₗ[R] B` that preserves `1`, then `algebraMap R B r` is invertible when `algebraMap R A r` is. -/ abbrev Invertible.algebraMapOfInvertibleAlgebraMap (f : A →ₗ[R] B) (hf : f 1 = 1) {r : R} (h : Invertible (algebraMap R A r)) : Invertible (algebraMap R B r) where invOf := f ⅟(algebraMap R A r) invOf_mul_self := by rw [← Algebra.commutes, ← Algebra.smul_def, ← map_smul, Algebra.smul_def, mul_invOf_self, hf] mul_invOf_self := by rw [← Algebra.smul_def, ← map_smul, Algebra.smul_def, mul_invOf_self, hf] /-- If there is a linear map `f : A →ₗ[R] B` that preserves `1`, then `algebraMap R B r` is a unit when `algebraMap R A r` is. -/ lemma IsUnit.algebraMap_of_algebraMap (f : A →ₗ[R] B) (hf : f 1 = 1) {r : R} (h : IsUnit (algebraMap R A r)) : IsUnit (algebraMap R B r) := let ⟨i⟩ := nonempty_invertible h letI := Invertible.algebraMapOfInvertibleAlgebraMap f hf i isUnit_of_invertible _ end invertibility section algebraMap variable {F E : Type*} [CommSemiring F] [Semiring E] [Algebra F E] (b : F →ₗ[F] E) /-- If `E` is an `F`-algebra, and there exists an injective `F`-linear map from `F` to `E`, then the algebra map from `F` to `E` is also injective. -/ theorem injective_algebraMap_of_linearMap (hb : Function.Injective b) : Function.Injective (algebraMap F E) := fun x y e ↦ hb <| by rw [← mul_one x, ← mul_one y, ← smul_eq_mul, ← smul_eq_mul, map_smul, map_smul, Algebra.smul_def, Algebra.smul_def, e] /-- If `E` is an `F`-algebra, and there exists a surjective `F`-linear map from `F` to `E`, then the algebra map from `F` to `E` is also surjective. -/ theorem surjective_algebraMap_of_linearMap (hb : Function.Surjective b) : Function.Surjective (algebraMap F E) := fun x ↦ by obtain ⟨x, rfl⟩ := hb x obtain ⟨y, hy⟩ := hb (b 1 * b 1) refine ⟨x * y, ?_⟩ obtain ⟨z, hz⟩ := hb 1 apply_fun (x • z • ·) at hy rwa [← map_smul, smul_eq_mul, mul_comm, ← smul_mul_assoc, ← map_smul _ z, smul_eq_mul, mul_one, ← smul_eq_mul, map_smul, hz, one_mul, ← map_smul, smul_eq_mul, mul_one, smul_smul, ← Algebra.algebraMap_eq_smul_one] at hy /-- If `E` is an `F`-algebra, and there exists a bijective `F`-linear map from `F` to `E`, then the algebra map from `F` to `E` is also bijective. NOTE: The same result can also be obtained if there are two `F`-linear maps from `F` to `E`, one is injective, the other one is surjective. In this case, use `injective_algebraMap_of_linearMap` and `surjective_algebraMap_of_linearMap` separately. -/ theorem bijective_algebraMap_of_linearMap (hb : Function.Bijective b) : Function.Bijective (algebraMap F E) := ⟨injective_algebraMap_of_linearMap b hb.1, surjective_algebraMap_of_linearMap b hb.2⟩ /-- If `E` is an `F`-algebra, there exists an `F`-linear isomorphism from `F` to `E` (namely, `E` is a free `F`-module of rank one), then the algebra map from `F` to `E` is bijective. -/ theorem bijective_algebraMap_of_linearEquiv (b : F ≃ₗ[F] E) : Function.Bijective (algebraMap F E) := bijective_algebraMap_of_linearMap _ b.bijective end algebraMap section surjective variable {R S} [CommSemiring R] [Semiring S] [Algebra R S] variable {M N} [AddCommMonoid M] [AddCommMonoid N] [Module R M] [Module S M] [IsScalarTower R S M] variable [Module R N] [Module S N] [IsScalarTower R S N] /-- If `R →+* S` is surjective, then `S`-linear maps between modules are exactly `R`-linear maps. -/ def LinearMap.extendScalarsOfSurjectiveEquiv (h : Function.Surjective (algebraMap R S)) : (M →ₗ[R] N) ≃ₗ[R] (M →ₗ[S] N) where toFun f := { __ := f, map_smul' := fun r x ↦ by obtain ⟨r, rfl⟩ := h r; simp } map_add' _ _ := rfl map_smul' _ _ := rfl invFun f := f.restrictScalars S left_inv f := rfl right_inv f := rfl /-- If `R →+* S` is surjective, then `R`-linear maps are also `S`-linear. -/ abbrev LinearMap.extendScalarsOfSurjective (h : Function.Surjective (algebraMap R S)) (l : M →ₗ[R] N) : M →ₗ[S] N := extendScalarsOfSurjectiveEquiv h l /-- If `R →+* S` is surjective, then `R`-linear isomorphisms are also `S`-linear. -/ def LinearEquiv.extendScalarsOfSurjective (h : Function.Surjective (algebraMap R S)) (f : M ≃ₗ[R] N) : M ≃ₗ[S] N where __ := f map_smul' r x := by obtain ⟨r, rfl⟩ := h r; simp variable (h : Function.Surjective (algebraMap R S)) @[simp] lemma LinearMap.extendScalarsOfSurjective_apply (l : M →ₗ[R] N) (x) : l.extendScalarsOfSurjective h x = l x := rfl @[simp] lemma LinearEquiv.extendScalarsOfSurjective_apply (f : M ≃ₗ[R] N) (x) : f.extendScalarsOfSurjective h x = f x := rfl @[simp] lemma LinearEquiv.extendScalarsOfSurjective_symm (f : M ≃ₗ[R] N) : (f.extendScalarsOfSurjective h).symm = f.symm.extendScalarsOfSurjective h := rfl end surjective
Algebra\Algebra\Bilinear.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Algebra.NonUnitalHom import Mathlib.Algebra.GroupPower.IterateHom import Mathlib.LinearAlgebra.TensorProduct.Basic /-! # Facts about algebras involving bilinear maps and tensor products We move a few basic statements about algebras out of `Algebra.Algebra.Basic`, in order to avoid importing `LinearAlgebra.BilinearMap` and `LinearAlgebra.TensorProduct` unnecessarily. -/ open TensorProduct Module namespace LinearMap section NonUnitalNonAssoc variable (R A : Type*) [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] /-- The multiplication in a non-unital non-associative algebra is a bilinear map. A weaker version of this for semirings exists as `AddMonoidHom.mul`. -/ def mul : A →ₗ[R] A →ₗ[R] A := LinearMap.mk₂ R (· * ·) add_mul smul_mul_assoc mul_add mul_smul_comm /-- The multiplication map on a non-unital algebra, as an `R`-linear map from `A ⊗[R] A` to `A`. -/ noncomputable def mul' : A ⊗[R] A →ₗ[R] A := TensorProduct.lift (mul R A) variable {A} /-- The multiplication on the left in a non-unital algebra is a linear map. -/ def mulLeft (a : A) : A →ₗ[R] A := mul R A a /-- The multiplication on the right in an algebra is a linear map. -/ def mulRight (a : A) : A →ₗ[R] A := (mul R A).flip a /-- Simultaneous multiplication on the left and right is a linear map. -/ def mulLeftRight (ab : A × A) : A →ₗ[R] A := (mulRight R ab.snd).comp (mulLeft R ab.fst) @[simp] theorem mulLeft_toAddMonoidHom (a : A) : (mulLeft R a : A →+ A) = AddMonoidHom.mulLeft a := rfl @[simp] theorem mulRight_toAddMonoidHom (a : A) : (mulRight R a : A →+ A) = AddMonoidHom.mulRight a := rfl variable {R} @[simp] theorem mul_apply' (a b : A) : mul R A a b = a * b := rfl @[simp] theorem mulLeft_apply (a b : A) : mulLeft R a b = a * b := rfl @[simp] theorem mulRight_apply (a b : A) : mulRight R a b = b * a := rfl @[simp] theorem mulLeftRight_apply (a b x : A) : mulLeftRight R (a, b) x = a * x * b := rfl @[simp] theorem mul'_apply {a b : A} : mul' R A (a ⊗ₜ b) = a * b := rfl @[simp] theorem mulLeft_zero_eq_zero : mulLeft R (0 : A) = 0 := (mul R A).map_zero @[simp] theorem mulRight_zero_eq_zero : mulRight R (0 : A) = 0 := (mul R A).flip.map_zero end NonUnitalNonAssoc section NonUnital variable (R A : Type*) [CommSemiring R] [NonUnitalSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] /-- The multiplication in a non-unital algebra is a bilinear map. A weaker version of this for non-unital non-associative algebras exists as `LinearMap.mul`. -/ def _root_.NonUnitalAlgHom.lmul : A →ₙₐ[R] End R A := { mul R A with map_mul' := by intro a b ext c exact mul_assoc a b c map_zero' := by ext a exact zero_mul a } variable {R A} @[simp] theorem _root_.NonUnitalAlgHom.coe_lmul_eq_mul : ⇑(NonUnitalAlgHom.lmul R A) = mul R A := rfl theorem commute_mulLeft_right (a b : A) : Commute (mulLeft R a) (mulRight R b) := by ext c exact (mul_assoc a c b).symm @[simp] theorem mulLeft_mul (a b : A) : mulLeft R (a * b) = (mulLeft R a).comp (mulLeft R b) := by ext simp only [mulLeft_apply, comp_apply, mul_assoc] @[simp] theorem mulRight_mul (a b : A) : mulRight R (a * b) = (mulRight R b).comp (mulRight R a) := by ext simp only [mulRight_apply, comp_apply, mul_assoc] end NonUnital section Semiring variable (R A B : Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] variable {R A B} in /-- A `LinearMap` preserves multiplication if pre- and post- composition with `LinearMap.mul` are equivalent. By converting the statement into an equality of `LinearMap`s, this lemma allows various specialized `ext` lemmas about `→ₗ[R]` to then be applied. This is the `LinearMap` version of `AddMonoidHom.map_mul_iff`. -/ theorem map_mul_iff (f : A →ₗ[R] B) : (∀ x y, f (x * y) = f x * f y) ↔ (LinearMap.mul R A).compr₂ f = (LinearMap.mul R B ∘ₗ f).compl₂ f := Iff.symm LinearMap.ext_iff₂ /-- The multiplication in an algebra is an algebra homomorphism into the endomorphisms on the algebra. A weaker version of this for non-unital algebras exists as `NonUnitalAlgHom.mul`. -/ def _root_.Algebra.lmul : A →ₐ[R] End R A := { LinearMap.mul R A with map_one' := by ext a exact one_mul a map_mul' := by intro a b ext c exact mul_assoc a b c map_zero' := by ext a exact zero_mul a commutes' := by intro r ext a exact (Algebra.smul_def r a).symm } variable {R A} @[simp] theorem _root_.Algebra.coe_lmul_eq_mul : ⇑(Algebra.lmul R A) = mul R A := rfl theorem _root_.Algebra.lmul_injective : Function.Injective (Algebra.lmul R A) := fun a₁ a₂ h ↦ by simpa using DFunLike.congr_fun h 1 theorem _root_.Algebra.lmul_isUnit_iff {x : A} : IsUnit (Algebra.lmul R A x) ↔ IsUnit x := by rw [Module.End_isUnit_iff, Iff.comm] exact IsUnit.isUnit_iff_mulLeft_bijective @[simp] theorem mulLeft_eq_zero_iff (a : A) : mulLeft R a = 0 ↔ a = 0 := by constructor <;> intro h -- Porting note: had to supply `R` explicitly in `@mulLeft_apply` below · rw [← mul_one a, ← @mulLeft_apply R _ _ _ _ _ _ a 1, h, LinearMap.zero_apply] · rw [h] exact mulLeft_zero_eq_zero @[simp] theorem mulRight_eq_zero_iff (a : A) : mulRight R a = 0 ↔ a = 0 := by constructor <;> intro h -- Porting note: had to supply `R` explicitly in `@mulRight_apply` below · rw [← one_mul a, ← @mulRight_apply R _ _ _ _ _ _ a 1, h, LinearMap.zero_apply] · rw [h] exact mulRight_zero_eq_zero @[simp] theorem mulLeft_one : mulLeft R (1 : A) = LinearMap.id := by ext simp @[simp] theorem mulRight_one : mulRight R (1 : A) = LinearMap.id := by ext simp @[simp] theorem pow_mulLeft (a : A) (n : ℕ) : mulLeft R a ^ n = mulLeft R (a ^ n) := by simpa only [mulLeft, ← Algebra.coe_lmul_eq_mul] using (map_pow (Algebra.lmul R A) a n).symm @[simp] theorem pow_mulRight (a : A) (n : ℕ) : mulRight R a ^ n = mulRight R (a ^ n) := by simp only [mulRight, ← Algebra.coe_lmul_eq_mul] exact LinearMap.coe_injective (((mulRight R a).coe_pow n).symm ▸ mul_right_iterate a n) end Semiring section Ring variable {R A : Type*} [CommSemiring R] [Ring A] [Algebra R A] theorem mulLeft_injective [NoZeroDivisors A] {x : A} (hx : x ≠ 0) : Function.Injective (mulLeft R x) := by letI : Nontrivial A := ⟨⟨x, 0, hx⟩⟩ letI := NoZeroDivisors.to_isDomain A exact mul_right_injective₀ hx theorem mulRight_injective [NoZeroDivisors A] {x : A} (hx : x ≠ 0) : Function.Injective (mulRight R x) := by letI : Nontrivial A := ⟨⟨x, 0, hx⟩⟩ letI := NoZeroDivisors.to_isDomain A exact mul_left_injective₀ hx theorem mul_injective [NoZeroDivisors A] {x : A} (hx : x ≠ 0) : Function.Injective (mul R A x) := by letI : Nontrivial A := ⟨⟨x, 0, hx⟩⟩ letI := NoZeroDivisors.to_isDomain A exact mul_right_injective₀ hx end Ring end LinearMap
Algebra\Algebra\Defs.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Module.LinearMap.Defs /-! # Algebras over commutative semirings In this file we define associative unital `Algebra`s over commutative (semi)rings. * algebra homomorphisms `AlgHom` are defined in `Mathlib.Algebra.Algebra.Hom`; * algebra equivalences `AlgEquiv` are defined in `Mathlib.Algebra.Algebra.Equiv`; * `Subalgebra`s are defined in `Mathlib.Algebra.Algebra.Subalgebra`; * The category `AlgebraCat R` of `R`-algebras is defined in the file `Mathlib.Algebra.Category.Algebra.Basic`. See the implementation notes for remarks about non-associative and non-unital algebras. ## Main definitions: * `Algebra R A`: the algebra typeclass. * `algebraMap R A : R →+* A`: the canonical map from `R` to `A`, as a `RingHom`. This is the preferred spelling of this map, it is also available as: * `Algebra.linearMap R A : R →ₗ[R] A`, a `LinearMap`. * `Algebra.ofId R A : R →ₐ[R] A`, an `AlgHom` (defined in a later file). ## Implementation notes Given a commutative (semi)ring `R`, there are two ways to define an `R`-algebra structure on a (possibly noncommutative) (semi)ring `A`: * By endowing `A` with a morphism of rings `R →+* A` denoted `algebraMap R A` which lands in the center of `A`. * By requiring `A` be an `R`-module such that the action associates and commutes with multiplication as `r • (a₁ * a₂) = (r • a₁) * a₂ = a₁ * (r • a₂)`. We define `Algebra R A` in a way that subsumes both definitions, by extending `SMul R A` and requiring that this scalar action `r • x` must agree with left multiplication by the image of the structure morphism `algebraMap R A r * x`. As a result, there are two ways to talk about an `R`-algebra `A` when `A` is a semiring: 1. ```lean variable [CommSemiring R] [Semiring A] variable [Algebra R A] ``` 2. ```lean variable [CommSemiring R] [Semiring A] variable [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] ``` The first approach implies the second via typeclass search; so any lemma stated with the second set of arguments will automatically apply to the first set. Typeclass search does not know that the second approach implies the first, but this can be shown with: ```lean example {R A : Type*} [CommSemiring R] [Semiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] : Algebra R A := Algebra.ofModule smul_mul_assoc mul_smul_comm ``` The advantage of the first approach is that `algebraMap R A` is available, and `AlgHom R A B` and `Subalgebra R A` can be used. For concrete `R` and `A`, `algebraMap R A` is often definitionally convenient. The advantage of the second approach is that `CommSemiring R`, `Semiring A`, and `Module R A` can all be relaxed independently; for instance, this allows us to: * Replace `Semiring A` with `NonUnitalNonAssocSemiring A` in order to describe non-unital and/or non-associative algebras. * Replace `CommSemiring R` and `Module R A` with `CommGroup R'` and `DistribMulAction R' A`, which when `R' = Rˣ` lets us talk about the "algebra-like" action of `Rˣ` on an `R`-algebra `A`. While `AlgHom R A B` cannot be used in the second approach, `NonUnitalAlgHom R A B` still can. You should always use the first approach when working with associative unital algebras, and mimic the second approach only when you need to weaken a condition on either `R` or `A`. -/ assert_not_exists Field universe u v w u₁ v₁ section Prio -- We set this priority to 0 later in this file -- Porting note: unsupported set_option extends_priority 200 /- control priority of `instance [Algebra R A] : SMul R A` -/ /-- An associative unital `R`-algebra is a semiring `A` equipped with a map into its center `R → A`. See the implementation notes in this file for discussion of the details of this definition. -/ -- Porting note(#5171): unsupported @[nolint has_nonempty_instance] class Algebra (R : Type u) (A : Type v) [CommSemiring R] [Semiring A] extends SMul R A, R →+* A where commutes' : ∀ r x, toRingHom r * x = x * toRingHom r smul_def' : ∀ r x, r • x = toRingHom r * x end Prio /-- Embedding `R →+* A` given by `Algebra` structure. -/ def algebraMap (R : Type u) (A : Type v) [CommSemiring R] [Semiring A] [Algebra R A] : R →+* A := Algebra.toRingHom /-- Coercion from a commutative semiring to an algebra over this semiring. -/ @[coe, reducible] def Algebra.cast {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] : R → A := algebraMap R A namespace algebraMap scoped instance coeHTCT (R A : Type*) [CommSemiring R] [Semiring A] [Algebra R A] : CoeHTCT R A := ⟨Algebra.cast⟩ section CommSemiringSemiring variable {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] @[norm_cast] theorem coe_zero : (↑(0 : R) : A) = 0 := map_zero (algebraMap R A) @[norm_cast] theorem coe_one : (↑(1 : R) : A) = 1 := map_one (algebraMap R A) @[norm_cast] theorem coe_natCast (a : ℕ) : (↑(a : R) : A) = a := map_natCast (algebraMap R A) a @[norm_cast] theorem coe_add (a b : R) : (↑(a + b : R) : A) = ↑a + ↑b := map_add (algebraMap R A) a b @[norm_cast] theorem coe_mul (a b : R) : (↑(a * b : R) : A) = ↑a * ↑b := map_mul (algebraMap R A) a b @[norm_cast] theorem coe_pow (a : R) (n : ℕ) : (↑(a ^ n : R) : A) = (a : A) ^ n := map_pow (algebraMap R A) _ _ end CommSemiringSemiring section CommRingRing variable {R A : Type*} [CommRing R] [Ring A] [Algebra R A] @[norm_cast] theorem coe_neg (x : R) : (↑(-x : R) : A) = -↑x := map_neg (algebraMap R A) x @[norm_cast] theorem coe_sub (a b : R) : (↑(a - b : R) : A) = ↑a - ↑b := map_sub (algebraMap R A) a b end CommRingRing section CommSemiringCommSemiring variable {R A : Type*} [CommSemiring R] [CommSemiring A] [Algebra R A] -- direct to_additive fails because of some mix-up with polynomials @[norm_cast] theorem coe_prod {ι : Type*} {s : Finset ι} (a : ι → R) : (↑(∏ i ∈ s, a i : R) : A) = ∏ i ∈ s, (↑(a i) : A) := map_prod (algebraMap R A) a s -- to_additive fails for some reason @[norm_cast] theorem coe_sum {ι : Type*} {s : Finset ι} (a : ι → R) : ↑(∑ i ∈ s, a i) = ∑ i ∈ s, (↑(a i) : A) := map_sum (algebraMap R A) a s -- Porting note: removed attribute [to_additive] coe_prod; why should this be a `to_additive`? end CommSemiringCommSemiring end algebraMap /-- Creating an algebra from a morphism to the center of a semiring. -/ def RingHom.toAlgebra' {R S} [CommSemiring R] [Semiring S] (i : R →+* S) (h : ∀ c x, i c * x = x * i c) : Algebra R S where smul c x := i c * x commutes' := h smul_def' _ _ := rfl toRingHom := i /-- Creating an algebra from a morphism to a commutative semiring. -/ def RingHom.toAlgebra {R S} [CommSemiring R] [CommSemiring S] (i : R →+* S) : Algebra R S := i.toAlgebra' fun _ => mul_comm _ theorem RingHom.algebraMap_toAlgebra {R S} [CommSemiring R] [CommSemiring S] (i : R →+* S) : @algebraMap R S _ _ i.toAlgebra = i := rfl namespace Algebra variable {R : Type u} {S : Type v} {A : Type w} {B : Type*} /-- Let `R` be a commutative semiring, let `A` be a semiring with a `Module R` structure. If `(r • 1) * x = x * (r • 1) = r • x` for all `r : R` and `x : A`, then `A` is an `Algebra` over `R`. See note [reducible non-instances]. -/ abbrev ofModule' [CommSemiring R] [Semiring A] [Module R A] (h₁ : ∀ (r : R) (x : A), r • (1 : A) * x = r • x) (h₂ : ∀ (r : R) (x : A), x * r • (1 : A) = r • x) : Algebra R A where toFun r := r • (1 : A) map_one' := one_smul _ _ map_mul' r₁ r₂ := by simp only [h₁, mul_smul] map_zero' := zero_smul _ _ map_add' r₁ r₂ := add_smul r₁ r₂ 1 commutes' r x := by simp [h₁, h₂] smul_def' r x := by simp [h₁] /-- Let `R` be a commutative semiring, let `A` be a semiring with a `Module R` structure. If `(r • x) * y = x * (r • y) = r • (x * y)` for all `r : R` and `x y : A`, then `A` is an `Algebra` over `R`. See note [reducible non-instances]. -/ abbrev ofModule [CommSemiring R] [Semiring A] [Module R A] (h₁ : ∀ (r : R) (x y : A), r • x * y = r • (x * y)) (h₂ : ∀ (r : R) (x y : A), x * r • y = r • (x * y)) : Algebra R A := ofModule' (fun r x => by rw [h₁, one_mul]) fun r x => by rw [h₂, mul_one] section Semiring variable [CommSemiring R] [CommSemiring S] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] -- Porting note: deleted a private lemma -- We'll later use this to show `Algebra ℤ M` is a subsingleton. /-- To prove two algebra structures on a fixed `[CommSemiring R] [Semiring A]` agree, it suffices to check the `algebraMap`s agree. -/ @[ext] theorem algebra_ext {R : Type*} [CommSemiring R] {A : Type*} [Semiring A] (P Q : Algebra R A) (h : ∀ r : R, (haveI := P; algebraMap R A r) = haveI := Q; algebraMap R A r) : P = Q := by replace h : P.toRingHom = Q.toRingHom := DFunLike.ext _ _ h have h' : (haveI := P; (· • ·) : R → A → A) = (haveI := Q; (· • ·) : R → A → A) := by funext r a rw [P.smul_def', Q.smul_def', h] rcases P with @⟨⟨P⟩⟩ rcases Q with @⟨⟨Q⟩⟩ congr -- see Note [lower instance priority] instance (priority := 200) toModule : Module R A where one_smul _ := by simp [smul_def'] mul_smul := by simp [smul_def', mul_assoc] smul_add := by simp [smul_def', mul_add] smul_zero := by simp [smul_def'] add_smul := by simp [smul_def', add_mul] zero_smul := by simp [smul_def'] -- Porting note: this caused deterministic timeouts later in mathlib3 but not in mathlib 4. -- attribute [instance 0] Algebra.toSMul theorem smul_def (r : R) (x : A) : r • x = algebraMap R A r * x := Algebra.smul_def' r x theorem algebraMap_eq_smul_one (r : R) : algebraMap R A r = r • (1 : A) := calc algebraMap R A r = algebraMap R A r * 1 := (mul_one _).symm _ = r • (1 : A) := (Algebra.smul_def r 1).symm theorem algebraMap_eq_smul_one' : ⇑(algebraMap R A) = fun r => r • (1 : A) := funext algebraMap_eq_smul_one /-- `mul_comm` for `Algebra`s when one element is from the base ring. -/ theorem commutes (r : R) (x : A) : algebraMap R A r * x = x * algebraMap R A r := Algebra.commutes' r x lemma commute_algebraMap_left (r : R) (x : A) : Commute (algebraMap R A r) x := Algebra.commutes r x lemma commute_algebraMap_right (r : R) (x : A) : Commute x (algebraMap R A r) := (Algebra.commutes r x).symm /-- `mul_left_comm` for `Algebra`s when one element is from the base ring. -/ theorem left_comm (x : A) (r : R) (y : A) : x * (algebraMap R A r * y) = algebraMap R A r * (x * y) := by rw [← mul_assoc, ← commutes, mul_assoc] /-- `mul_right_comm` for `Algebra`s when one element is from the base ring. -/ theorem right_comm (x : A) (r : R) (y : A) : x * algebraMap R A r * y = x * y * algebraMap R A r := by rw [mul_assoc, commutes, ← mul_assoc] instance _root_.IsScalarTower.right : IsScalarTower R A A := ⟨fun x y z => by rw [smul_eq_mul, smul_eq_mul, smul_def, smul_def, mul_assoc]⟩ @[simp] theorem _root_.RingHom.smulOneHom_eq_algebraMap : RingHom.smulOneHom = algebraMap R A := RingHom.ext fun r => (algebraMap_eq_smul_one r).symm -- TODO: set up `IsScalarTower.smulCommClass` earlier so that we can actually prove this using -- `mul_smul_comm s x y`. /-- This is just a special case of the global `mul_smul_comm` lemma that requires less typeclass search (and was here first). -/ @[simp] protected theorem mul_smul_comm (s : R) (x y : A) : x * s • y = s • (x * y) := by rw [smul_def, smul_def, left_comm] /-- This is just a special case of the global `smul_mul_assoc` lemma that requires less typeclass search (and was here first). -/ @[simp] protected theorem smul_mul_assoc (r : R) (x y : A) : r • x * y = r • (x * y) := smul_mul_assoc r x y @[simp] theorem _root_.smul_algebraMap {α : Type*} [Monoid α] [MulDistribMulAction α A] [SMulCommClass α R A] (a : α) (r : R) : a • algebraMap R A r = algebraMap R A r := by rw [algebraMap_eq_smul_one, smul_comm a r (1 : A), smul_one] section end variable (R A) /-- The canonical ring homomorphism `algebraMap R A : R →+* A` for any `R`-algebra `A`, packaged as an `R`-linear map. -/ protected def linearMap : R →ₗ[R] A := { algebraMap R A with map_smul' := fun x y => by simp [Algebra.smul_def] } @[simp] theorem linearMap_apply (r : R) : Algebra.linearMap R A r = algebraMap R A r := rfl theorem coe_linearMap : ⇑(Algebra.linearMap R A) = algebraMap R A := rfl /-- The identity map inducing an `Algebra` structure. -/ instance (priority := 1100) id : Algebra R R where -- We override `toFun` and `toSMul` because `RingHom.id` is not reducible and cannot -- be made so without a significant performance hit. -- see library note [reducible non-instances]. toFun x := x toSMul := Mul.toSMul _ __ := (RingHom.id R).toAlgebra variable {R A} namespace id @[simp] theorem map_eq_id : algebraMap R R = RingHom.id _ := rfl theorem map_eq_self (x : R) : algebraMap R R x = x := rfl @[simp] theorem smul_eq_mul (x y : R) : x • y = x * y := rfl end id end Semiring end Algebra @[norm_cast] theorem algebraMap.coe_smul (A B C : Type*) [SMul A B] [CommSemiring B] [Semiring C] [Algebra B C] [SMul A C] [IsScalarTower A B C] (a : A) (b : B) : (a • b : B) = a • (b : C) := calc ((a • b : B) : C) = (a • b) • 1 := Algebra.algebraMap_eq_smul_one _ _ = a • (b • 1) := smul_assoc .. _ = a • (b : C) := congrArg _ (Algebra.algebraMap_eq_smul_one b).symm assert_not_exists Module.End
Algebra\Algebra\Equiv.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Hom import Mathlib.Algebra.Ring.Aut /-! # Isomorphisms of `R`-algebras This file defines bundled isomorphisms of `R`-algebras. ## Main definitions * `AlgEquiv R A B`: the type of `R`-algebra isomorphisms between `A` and `B`. ## Notations * `A ≃ₐ[R] B` : `R`-algebra equivalence from `A` to `B`. -/ universe u v w u₁ v₁ /-- An equivalence of algebras is an equivalence of rings commuting with the actions of scalars. -/ structure AlgEquiv (R : Type u) (A : Type v) (B : Type w) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] extends A ≃ B, A ≃* B, A ≃+ B, A ≃+* B where /-- An equivalence of algebras commutes with the action of scalars. -/ protected commutes' : ∀ r : R, toFun (algebraMap R A r) = algebraMap R B r attribute [nolint docBlame] AlgEquiv.toRingEquiv attribute [nolint docBlame] AlgEquiv.toEquiv attribute [nolint docBlame] AlgEquiv.toAddEquiv attribute [nolint docBlame] AlgEquiv.toMulEquiv @[inherit_doc] notation:50 A " ≃ₐ[" R "] " A' => AlgEquiv R A A' /-- `AlgEquivClass F R A B` states that `F` is a type of algebra structure preserving equivalences. You should extend this class when you extend `AlgEquiv`. -/ class AlgEquivClass (F : Type*) (R A B : outParam Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [EquivLike F A B] extends RingEquivClass F A B : Prop where /-- An equivalence of algebras commutes with the action of scalars. -/ commutes : ∀ (f : F) (r : R), f (algebraMap R A r) = algebraMap R B r -- Porting note: Removed nolint dangerousInstance from AlgEquivClass.toRingEquivClass namespace AlgEquivClass -- See note [lower instance priority] instance (priority := 100) toAlgHomClass (F R A B : Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [EquivLike F A B] [h : AlgEquivClass F R A B] : AlgHomClass F R A B := { h with } instance (priority := 100) toLinearEquivClass (F R A B : Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [EquivLike F A B] [h : AlgEquivClass F R A B] : LinearEquivClass F R A B := { h with map_smulₛₗ := fun f => map_smulₛₗ f } /-- Turn an element of a type `F` satisfying `AlgEquivClass F R A B` into an actual `AlgEquiv`. This is declared as the default coercion from `F` to `A ≃ₐ[R] B`. -/ @[coe] def toAlgEquiv {F R A B : Type*} [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [EquivLike F A B] [AlgEquivClass F R A B] (f : F) : A ≃ₐ[R] B := { (f : A ≃ B), (f : A ≃+* B) with commutes' := commutes f } instance (F R A B : Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [EquivLike F A B] [AlgEquivClass F R A B] : CoeTC F (A ≃ₐ[R] B) := ⟨toAlgEquiv⟩ end AlgEquivClass namespace AlgEquiv universe uR uA₁ uA₂ uA₃ uA₁' uA₂' uA₃' variable {R : Type uR} variable {A₁ : Type uA₁} {A₂ : Type uA₂} {A₃ : Type uA₃} variable {A₁' : Type uA₁'} {A₂' : Type uA₂'} {A₃' : Type uA₃'} section Semiring variable [CommSemiring R] [Semiring A₁] [Semiring A₂] [Semiring A₃] variable [Semiring A₁'] [Semiring A₂'] [Semiring A₃'] variable [Algebra R A₁] [Algebra R A₂] [Algebra R A₃] variable [Algebra R A₁'] [Algebra R A₂'] [Algebra R A₃'] variable (e : A₁ ≃ₐ[R] A₂) instance : EquivLike (A₁ ≃ₐ[R] A₂) A₁ A₂ where coe f := f.toFun inv f := f.invFun left_inv f := f.left_inv right_inv f := f.right_inv coe_injective' f g h₁ h₂ := by obtain ⟨⟨f,_⟩,_⟩ := f obtain ⟨⟨g,_⟩,_⟩ := g congr /-- Helper instance since the coercion is not always found. -/ instance : FunLike (A₁ ≃ₐ[R] A₂) A₁ A₂ where coe := DFunLike.coe coe_injective' := DFunLike.coe_injective' instance : AlgEquivClass (A₁ ≃ₐ[R] A₂) R A₁ A₂ where map_add f := f.map_add' map_mul f := f.map_mul' commutes f := f.commutes' -- Porting note: the default simps projection was `e.toEquiv.toFun`, it should be `FunLike.coe` /-- See Note [custom simps projection] -/ def Simps.apply (e : A₁ ≃ₐ[R] A₂) : A₁ → A₂ := e -- Porting note: the default simps projection was `e.toEquiv`, it should be `EquivLike.toEquiv` /-- See Note [custom simps projection] -/ def Simps.toEquiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ A₂ := e -- Porting note: `protected` used to be an attribute below @[simp] protected theorem coe_coe {F : Type*} [EquivLike F A₁ A₂] [AlgEquivClass F R A₁ A₂] (f : F) : ⇑(f : A₁ ≃ₐ[R] A₂) = f := rfl @[ext] theorem ext {f g : A₁ ≃ₐ[R] A₂} (h : ∀ a, f a = g a) : f = g := DFunLike.ext f g h protected theorem congr_arg {f : A₁ ≃ₐ[R] A₂} {x x' : A₁} : x = x' → f x = f x' := DFunLike.congr_arg f protected theorem congr_fun {f g : A₁ ≃ₐ[R] A₂} (h : f = g) (x : A₁) : f x = g x := DFunLike.congr_fun h x theorem coe_fun_injective : @Function.Injective (A₁ ≃ₐ[R] A₂) (A₁ → A₂) fun e => (e : A₁ → A₂) := DFunLike.coe_injective -- Porting note: Made to CoeOut instance from Coe, not dangerous anymore instance hasCoeToRingEquiv : CoeOut (A₁ ≃ₐ[R] A₂) (A₁ ≃+* A₂) := ⟨AlgEquiv.toRingEquiv⟩ @[simp] theorem coe_mk {toEquiv map_mul map_add commutes} : ⇑(⟨toEquiv, map_mul, map_add, commutes⟩ : A₁ ≃ₐ[R] A₂) = toEquiv := rfl @[simp] theorem mk_coe (e : A₁ ≃ₐ[R] A₂) (e' h₁ h₂ h₃ h₄ h₅) : (⟨⟨e, e', h₁, h₂⟩, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂) = e := ext fun _ => rfl -- Porting note: `toFun_eq_coe` no longer needed in Lean4 @[simp] theorem toEquiv_eq_coe : e.toEquiv = e := rfl @[simp] theorem toRingEquiv_eq_coe : e.toRingEquiv = e := rfl @[simp, norm_cast] lemma toRingEquiv_toRingHom : ((e : A₁ ≃+* A₂) : A₁ →+* A₂) = e := rfl @[simp, norm_cast] theorem coe_ringEquiv : ((e : A₁ ≃+* A₂) : A₁ → A₂) = e := rfl theorem coe_ringEquiv' : (e.toRingEquiv : A₁ → A₂) = e := rfl theorem coe_ringEquiv_injective : Function.Injective ((↑) : (A₁ ≃ₐ[R] A₂) → A₁ ≃+* A₂) := fun _ _ h => ext <| RingEquiv.congr_fun h @[deprecated map_add (since := "2024-06-20")] protected theorem map_add : ∀ x y, e (x + y) = e x + e y := map_add e @[deprecated map_zero (since := "2024-06-20")] protected theorem map_zero : e 0 = 0 := map_zero e @[deprecated map_mul (since := "2024-06-20")] protected theorem map_mul : ∀ x y, e (x * y) = e x * e y := map_mul e @[deprecated map_one (since := "2024-06-20")] protected theorem map_one : e 1 = 1 := map_one e @[simp] theorem commutes : ∀ r : R, e (algebraMap R A₁ r) = algebraMap R A₂ r := e.commutes' -- @[simp] -- Porting note (#10618): simp can prove this @[deprecated map_smul (since := "2024-06-20")] protected theorem map_smul (r : R) (x : A₁) : e (r • x) = r • e x := map_smul _ _ _ @[deprecated map_sum (since := "2023-12-26")] protected theorem map_sum {ι : Type*} (f : ι → A₁) (s : Finset ι) : e (∑ x ∈ s, f x) = ∑ x ∈ s, e (f x) := map_sum e f s @[deprecated map_finsupp_sum (since := "2024-06-20")] protected theorem map_finsupp_sum {α : Type*} [Zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.sum g) = f.sum fun i b => e (g i b) := map_finsupp_sum _ _ _ -- Porting note: Added [coe] attribute /-- Interpret an algebra equivalence as an algebra homomorphism. This definition is included for symmetry with the other `to*Hom` projections. The `simp` normal form is to use the coercion of the `AlgHomClass.coeTC` instance. -/ @[coe] def toAlgHom : A₁ →ₐ[R] A₂ := { e with map_one' := map_one e map_zero' := map_zero e } @[simp] theorem toAlgHom_eq_coe : e.toAlgHom = e := rfl @[simp, norm_cast] theorem coe_algHom : DFunLike.coe (e.toAlgHom) = DFunLike.coe e := rfl theorem coe_algHom_injective : Function.Injective ((↑) : (A₁ ≃ₐ[R] A₂) → A₁ →ₐ[R] A₂) := fun _ _ h => ext <| AlgHom.congr_fun h @[simp, norm_cast] lemma toAlgHom_toRingHom : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = e := rfl /-- The two paths coercion can take to a `RingHom` are equivalent -/ theorem coe_ringHom_commutes : ((e : A₁ →ₐ[R] A₂) : A₁ →+* A₂) = ((e : A₁ ≃+* A₂) : A₁ →+* A₂) := rfl @[deprecated map_pow (since := "2024-06-20")] protected theorem map_pow : ∀ (x : A₁) (n : ℕ), e (x ^ n) = e x ^ n := map_pow _ protected theorem injective : Function.Injective e := EquivLike.injective e protected theorem surjective : Function.Surjective e := EquivLike.surjective e protected theorem bijective : Function.Bijective e := EquivLike.bijective e /-- Algebra equivalences are reflexive. -/ @[refl] def refl : A₁ ≃ₐ[R] A₁ := { (1 : A₁ ≃+* A₁) with commutes' := fun _ => rfl } instance : Inhabited (A₁ ≃ₐ[R] A₁) := ⟨refl⟩ @[simp] theorem refl_toAlgHom : ↑(refl : A₁ ≃ₐ[R] A₁) = AlgHom.id R A₁ := rfl @[simp] theorem coe_refl : ⇑(refl : A₁ ≃ₐ[R] A₁) = id := rfl /-- Algebra equivalences are symmetric. -/ @[symm] def symm (e : A₁ ≃ₐ[R] A₂) : A₂ ≃ₐ[R] A₁ := { e.toRingEquiv.symm with commutes' := fun r => by rw [← e.toRingEquiv.symm_apply_apply (algebraMap R A₁ r)] congr change _ = e _ rw [e.commutes] } /-- See Note [custom simps projection] -/ def Simps.symm_apply (e : A₁ ≃ₐ[R] A₂) : A₂ → A₁ := e.symm initialize_simps_projections AlgEquiv (toFun → apply, invFun → symm_apply) --@[simp] -- Porting note (#10618): simp can prove this once symm_mk is introduced theorem coe_apply_coe_coe_symm_apply {F : Type*} [EquivLike F A₁ A₂] [AlgEquivClass F R A₁ A₂] (f : F) (x : A₂) : f ((f : A₁ ≃ₐ[R] A₂).symm x) = x := EquivLike.right_inv f x --@[simp] -- Porting note (#10618): simp can prove this once symm_mk is introduced theorem coe_coe_symm_apply_coe_apply {F : Type*} [EquivLike F A₁ A₂] [AlgEquivClass F R A₁ A₂] (f : F) (x : A₁) : (f : A₁ ≃ₐ[R] A₂).symm (f x) = x := EquivLike.left_inv f x -- Porting note: `simp` normal form of `invFun_eq_symm` @[simp] theorem symm_toEquiv_eq_symm {e : A₁ ≃ₐ[R] A₂} : (e : A₁ ≃ A₂).symm = e.symm := rfl theorem invFun_eq_symm {e : A₁ ≃ₐ[R] A₂} : e.invFun = e.symm := rfl @[simp] theorem symm_symm (e : A₁ ≃ₐ[R] A₂) : e.symm.symm = e := by ext rfl theorem symm_bijective : Function.Bijective (symm : (A₁ ≃ₐ[R] A₂) → A₂ ≃ₐ[R] A₁) := Function.bijective_iff_has_inverse.mpr ⟨_, symm_symm, symm_symm⟩ @[simp] theorem mk_coe' (e : A₁ ≃ₐ[R] A₂) (f h₁ h₂ h₃ h₄ h₅) : (⟨⟨f, e, h₁, h₂⟩, h₃, h₄, h₅⟩ : A₂ ≃ₐ[R] A₁) = e.symm := symm_bijective.injective <| ext fun _ => rfl /-- Auxilliary definition to avoid looping in `dsimp` with `AlgEquiv.symm_mk`. -/ protected def symm_mk.aux (f f') (h₁ h₂ h₃ h₄ h₅) := (⟨⟨f, f', h₁, h₂⟩, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm @[simp] theorem symm_mk (f f') (h₁ h₂ h₃ h₄ h₅) : (⟨⟨f, f', h₁, h₂⟩, h₃, h₄, h₅⟩ : A₁ ≃ₐ[R] A₂).symm = { symm_mk.aux f f' h₁ h₂ h₃ h₄ h₅ with toFun := f' invFun := f } := rfl @[simp] theorem refl_symm : (AlgEquiv.refl : A₁ ≃ₐ[R] A₁).symm = AlgEquiv.refl := rfl --this should be a simp lemma but causes a lint timeout theorem toRingEquiv_symm (f : A₁ ≃ₐ[R] A₁) : (f : A₁ ≃+* A₁).symm = f.symm := rfl @[simp] theorem symm_toRingEquiv : (e.symm : A₂ ≃+* A₁) = (e : A₁ ≃+* A₂).symm := rfl /-- Algebra equivalences are transitive. -/ @[trans] def trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : A₁ ≃ₐ[R] A₃ := { e₁.toRingEquiv.trans e₂.toRingEquiv with commutes' := fun r => show e₂.toFun (e₁.toFun _) = _ by rw [e₁.commutes', e₂.commutes'] } @[simp] theorem apply_symm_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e (e.symm x) = x := e.toEquiv.apply_symm_apply @[simp] theorem symm_apply_apply (e : A₁ ≃ₐ[R] A₂) : ∀ x, e.symm (e x) = x := e.toEquiv.symm_apply_apply @[simp] theorem symm_trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₃) : (e₁.trans e₂).symm x = e₁.symm (e₂.symm x) := rfl @[simp] theorem coe_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : ⇑(e₁.trans e₂) = e₂ ∘ e₁ := rfl @[simp] theorem trans_apply (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) (x : A₁) : (e₁.trans e₂) x = e₂ (e₁ x) := rfl @[simp] theorem comp_symm (e : A₁ ≃ₐ[R] A₂) : AlgHom.comp (e : A₁ →ₐ[R] A₂) ↑e.symm = AlgHom.id R A₂ := by ext simp @[simp] theorem symm_comp (e : A₁ ≃ₐ[R] A₂) : AlgHom.comp ↑e.symm (e : A₁ →ₐ[R] A₂) = AlgHom.id R A₁ := by ext simp theorem leftInverse_symm (e : A₁ ≃ₐ[R] A₂) : Function.LeftInverse e.symm e := e.left_inv theorem rightInverse_symm (e : A₁ ≃ₐ[R] A₂) : Function.RightInverse e.symm e := e.right_inv /-- If `A₁` is equivalent to `A₁'` and `A₂` is equivalent to `A₂'`, then the type of maps `A₁ →ₐ[R] A₂` is equivalent to the type of maps `A₁' →ₐ[R] A₂'`. -/ @[simps apply] def arrowCongr (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (A₁ →ₐ[R] A₂) ≃ (A₁' →ₐ[R] A₂') where toFun f := (e₂.toAlgHom.comp f).comp e₁.symm.toAlgHom invFun f := (e₂.symm.toAlgHom.comp f).comp e₁.toAlgHom left_inv f := by simp only [AlgHom.comp_assoc, toAlgHom_eq_coe, symm_comp] simp only [← AlgHom.comp_assoc, symm_comp, AlgHom.id_comp, AlgHom.comp_id] right_inv f := by simp only [AlgHom.comp_assoc, toAlgHom_eq_coe, comp_symm] simp only [← AlgHom.comp_assoc, comp_symm, AlgHom.id_comp, AlgHom.comp_id] theorem arrowCongr_comp (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') (e₃ : A₃ ≃ₐ[R] A₃') (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₃) : arrowCongr e₁ e₃ (g.comp f) = (arrowCongr e₂ e₃ g).comp (arrowCongr e₁ e₂ f) := by ext simp only [arrowCongr, Equiv.coe_fn_mk, AlgHom.comp_apply] congr exact (e₂.symm_apply_apply _).symm @[simp] theorem arrowCongr_refl : arrowCongr AlgEquiv.refl AlgEquiv.refl = Equiv.refl (A₁ →ₐ[R] A₂) := by ext rfl @[simp] theorem arrowCongr_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₁' : A₁' ≃ₐ[R] A₂') (e₂ : A₂ ≃ₐ[R] A₃) (e₂' : A₂' ≃ₐ[R] A₃') : arrowCongr (e₁.trans e₂) (e₁'.trans e₂') = (arrowCongr e₁ e₁').trans (arrowCongr e₂ e₂') := by ext rfl @[simp] theorem arrowCongr_symm (e₁ : A₁ ≃ₐ[R] A₁') (e₂ : A₂ ≃ₐ[R] A₂') : (arrowCongr e₁ e₂).symm = arrowCongr e₁.symm e₂.symm := by ext rfl /-- If `A₁` is equivalent to `A₂` and `A₁'` is equivalent to `A₂'`, then the type of maps `A₁ ≃ₐ[R] A₁'` is equivalent to the type of maps `A₂ ≃ ₐ[R] A₂'`. This is the `AlgEquiv` version of `AlgEquiv.arrowCongr`. -/ @[simps apply] def equivCongr (e : A₁ ≃ₐ[R] A₂) (e' : A₁' ≃ₐ[R] A₂') : (A₁ ≃ₐ[R] A₁') ≃ A₂ ≃ₐ[R] A₂' where toFun ψ := e.symm.trans (ψ.trans e') invFun ψ := e.trans (ψ.trans e'.symm) left_inv ψ := by ext simp_rw [trans_apply, symm_apply_apply] right_inv ψ := by ext simp_rw [trans_apply, apply_symm_apply] @[simp] theorem equivCongr_refl : equivCongr AlgEquiv.refl AlgEquiv.refl = Equiv.refl (A₁ ≃ₐ[R] A₁') := by ext rfl @[simp] theorem equivCongr_symm (e : A₁ ≃ₐ[R] A₂) (e' : A₁' ≃ₐ[R] A₂') : (equivCongr e e').symm = equivCongr e.symm e'.symm := rfl @[simp] theorem equivCongr_trans (e₁₂ : A₁ ≃ₐ[R] A₂) (e₁₂' : A₁' ≃ₐ[R] A₂') (e₂₃ : A₂ ≃ₐ[R] A₃) (e₂₃' : A₂' ≃ₐ[R] A₃') : (equivCongr e₁₂ e₁₂').trans (equivCongr e₂₃ e₂₃') = equivCongr (e₁₂.trans e₂₃) (e₁₂'.trans e₂₃') := rfl /-- If an algebra morphism has an inverse, it is an algebra isomorphism. -/ @[simps] def ofAlgHom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ : f.comp g = AlgHom.id R A₂) (h₂ : g.comp f = AlgHom.id R A₁) : A₁ ≃ₐ[R] A₂ := { f with toFun := f invFun := g left_inv := AlgHom.ext_iff.1 h₂ right_inv := AlgHom.ext_iff.1 h₁ } theorem coe_algHom_ofAlgHom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : ↑(ofAlgHom f g h₁ h₂) = f := AlgHom.ext fun _ => rfl @[simp] theorem ofAlgHom_coe_algHom (f : A₁ ≃ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : ofAlgHom (↑f) g h₁ h₂ = f := ext fun _ => rfl theorem ofAlgHom_symm (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : (ofAlgHom f g h₁ h₂).symm = ofAlgHom g f h₂ h₁ := rfl /-- Promotes a bijective algebra homomorphism to an algebra equivalence. -/ noncomputable def ofBijective (f : A₁ →ₐ[R] A₂) (hf : Function.Bijective f) : A₁ ≃ₐ[R] A₂ := { RingEquiv.ofBijective (f : A₁ →+* A₂) hf, f with } @[simp] theorem coe_ofBijective {f : A₁ →ₐ[R] A₂} {hf : Function.Bijective f} : (AlgEquiv.ofBijective f hf : A₁ → A₂) = f := rfl theorem ofBijective_apply {f : A₁ →ₐ[R] A₂} {hf : Function.Bijective f} (a : A₁) : (AlgEquiv.ofBijective f hf) a = f a := rfl /-- Forgetting the multiplicative structures, an equivalence of algebras is a linear equivalence. -/ @[simps apply] def toLinearEquiv (e : A₁ ≃ₐ[R] A₂) : A₁ ≃ₗ[R] A₂ := { e with toFun := e map_smul' := map_smul e invFun := e.symm } @[simp] theorem toLinearEquiv_refl : (AlgEquiv.refl : A₁ ≃ₐ[R] A₁).toLinearEquiv = LinearEquiv.refl R A₁ := rfl @[simp] theorem toLinearEquiv_symm (e : A₁ ≃ₐ[R] A₂) : e.toLinearEquiv.symm = e.symm.toLinearEquiv := rfl @[simp] theorem toLinearEquiv_trans (e₁ : A₁ ≃ₐ[R] A₂) (e₂ : A₂ ≃ₐ[R] A₃) : (e₁.trans e₂).toLinearEquiv = e₁.toLinearEquiv.trans e₂.toLinearEquiv := rfl theorem toLinearEquiv_injective : Function.Injective (toLinearEquiv : _ → A₁ ≃ₗ[R] A₂) := fun _ _ h => ext <| LinearEquiv.congr_fun h /-- Interpret an algebra equivalence as a linear map. -/ def toLinearMap : A₁ →ₗ[R] A₂ := e.toAlgHom.toLinearMap @[simp] theorem toAlgHom_toLinearMap : (e : A₁ →ₐ[R] A₂).toLinearMap = e.toLinearMap := rfl theorem toLinearMap_ofAlgHom (f : A₁ →ₐ[R] A₂) (g : A₂ →ₐ[R] A₁) (h₁ h₂) : (ofAlgHom f g h₁ h₂).toLinearMap = f.toLinearMap := LinearMap.ext fun _ => rfl @[simp] theorem toLinearEquiv_toLinearMap : e.toLinearEquiv.toLinearMap = e.toLinearMap := rfl @[simp] theorem toLinearMap_apply (x : A₁) : e.toLinearMap x = e x := rfl theorem toLinearMap_injective : Function.Injective (toLinearMap : _ → A₁ →ₗ[R] A₂) := fun _ _ h => ext <| LinearMap.congr_fun h @[simp] theorem trans_toLinearMap (f : A₁ ≃ₐ[R] A₂) (g : A₂ ≃ₐ[R] A₃) : (f.trans g).toLinearMap = g.toLinearMap.comp f.toLinearMap := rfl section OfLinearEquiv variable (l : A₁ ≃ₗ[R] A₂) (map_one : l 1 = 1) (map_mul : ∀ x y : A₁, l (x * y) = l x * l y) /-- Upgrade a linear equivalence to an algebra equivalence, given that it distributes over multiplication and the identity -/ @[simps apply] def ofLinearEquiv : A₁ ≃ₐ[R] A₂ := { l with toFun := l invFun := l.symm map_mul' := map_mul commutes' := (AlgHom.ofLinearMap l map_one map_mul : A₁ →ₐ[R] A₂).commutes } /-- Auxilliary definition to avoid looping in `dsimp` with `AlgEquiv.ofLinearEquiv_symm`. -/ protected def ofLinearEquiv_symm.aux := (ofLinearEquiv l map_one map_mul).symm @[simp] theorem ofLinearEquiv_symm : (ofLinearEquiv l map_one map_mul).symm = ofLinearEquiv l.symm (_root_.map_one <| ofLinearEquiv_symm.aux l map_one map_mul) (_root_.map_mul <| ofLinearEquiv_symm.aux l map_one map_mul) := rfl @[simp] theorem ofLinearEquiv_toLinearEquiv (map_mul) (map_one) : ofLinearEquiv e.toLinearEquiv map_mul map_one = e := by ext rfl @[simp] theorem toLinearEquiv_ofLinearEquiv : toLinearEquiv (ofLinearEquiv l map_one map_mul) = l := by ext rfl end OfLinearEquiv section OfRingEquiv /-- Promotes a linear `RingEquiv` to an `AlgEquiv`. -/ @[simps apply symm_apply toEquiv] -- Porting note: don't want redundant `toEquiv_symm_apply` simps def ofRingEquiv {f : A₁ ≃+* A₂} (hf : ∀ x, f (algebraMap R A₁ x) = algebraMap R A₂ x) : A₁ ≃ₐ[R] A₂ := { f with toFun := f invFun := f.symm commutes' := hf } end OfRingEquiv -- Porting note: projections mul & one not found, removed [simps] and added theorems manually -- @[simps (config := .lemmasOnly) one] instance aut : Group (A₁ ≃ₐ[R] A₁) where mul ϕ ψ := ψ.trans ϕ mul_assoc ϕ ψ χ := rfl one := refl one_mul ϕ := ext fun x => rfl mul_one ϕ := ext fun x => rfl inv := symm mul_left_inv ϕ := ext <| symm_apply_apply ϕ theorem aut_mul (ϕ ψ : A₁ ≃ₐ[R] A₁) : ϕ * ψ = ψ.trans ϕ := rfl theorem aut_one : 1 = AlgEquiv.refl (R := R) (A₁ := A₁) := rfl @[simp] theorem one_apply (x : A₁) : (1 : A₁ ≃ₐ[R] A₁) x = x := rfl @[simp] theorem mul_apply (e₁ e₂ : A₁ ≃ₐ[R] A₁) (x : A₁) : (e₁ * e₂) x = e₁ (e₂ x) := rfl /-- An algebra isomorphism induces a group isomorphism between automorphism groups. This is a more bundled version of `AlgEquiv.equivCongr`. -/ @[simps apply] def autCongr (ϕ : A₁ ≃ₐ[R] A₂) : (A₁ ≃ₐ[R] A₁) ≃* A₂ ≃ₐ[R] A₂ where __ := equivCongr ϕ ϕ toFun ψ := ϕ.symm.trans (ψ.trans ϕ) invFun ψ := ϕ.trans (ψ.trans ϕ.symm) map_mul' ψ χ := by ext simp only [mul_apply, trans_apply, symm_apply_apply] @[simp] theorem autCongr_refl : autCongr AlgEquiv.refl = MulEquiv.refl (A₁ ≃ₐ[R] A₁) := by ext rfl @[simp] theorem autCongr_symm (ϕ : A₁ ≃ₐ[R] A₂) : (autCongr ϕ).symm = autCongr ϕ.symm := rfl @[simp] theorem autCongr_trans (ϕ : A₁ ≃ₐ[R] A₂) (ψ : A₂ ≃ₐ[R] A₃) : (autCongr ϕ).trans (autCongr ψ) = autCongr (ϕ.trans ψ) := rfl /-- The tautological action by `A₁ ≃ₐ[R] A₁` on `A₁`. This generalizes `Function.End.applyMulAction`. -/ instance applyMulSemiringAction : MulSemiringAction (A₁ ≃ₐ[R] A₁) A₁ where smul := (· <| ·) smul_zero := map_zero smul_add := map_add smul_one := map_one smul_mul := map_mul one_smul _ := rfl mul_smul _ _ _ := rfl @[simp] protected theorem smul_def (f : A₁ ≃ₐ[R] A₁) (a : A₁) : f • a = f a := rfl instance apply_faithfulSMul : FaithfulSMul (A₁ ≃ₐ[R] A₁) A₁ := ⟨AlgEquiv.ext⟩ instance apply_smulCommClass {S} [SMul S R] [SMul S A₁] [IsScalarTower S R A₁] : SMulCommClass S (A₁ ≃ₐ[R] A₁) A₁ where smul_comm r e a := (e.toLinearEquiv.map_smul_of_tower r a).symm instance apply_smulCommClass' {S} [SMul S R] [SMul S A₁] [IsScalarTower S R A₁] : SMulCommClass (A₁ ≃ₐ[R] A₁) S A₁ := SMulCommClass.symm _ _ _ instance : MulDistribMulAction (A₁ ≃ₐ[R] A₁) A₁ˣ where smul := fun f => Units.map f one_smul := fun x => by ext; rfl mul_smul := fun x y z => by ext; rfl smul_mul := fun x y z => by ext; exact map_mul x _ _ smul_one := fun x => by ext; exact map_one x @[simp] theorem smul_units_def (f : A₁ ≃ₐ[R] A₁) (x : A₁ˣ) : f • x = Units.map f x := rfl @[simp] theorem algebraMap_eq_apply (e : A₁ ≃ₐ[R] A₂) {y : R} {x : A₁} : algebraMap R A₂ y = e x ↔ algebraMap R A₁ y = x := ⟨fun h => by simpa using e.symm.toAlgHom.algebraMap_eq_apply h, fun h => e.toAlgHom.algebraMap_eq_apply h⟩ /-- `AlgEquiv.toLinearMap` as a `MonoidHom`. -/ @[simps] def toLinearMapHom (R A) [CommSemiring R] [Semiring A] [Algebra R A] : (A ≃ₐ[R] A) →* A →ₗ[R] A where toFun := AlgEquiv.toLinearMap map_one' := rfl map_mul' := fun _ _ ↦ rfl lemma pow_toLinearMap (σ : A₁ ≃ₐ[R] A₁) (n : ℕ) : (σ ^ n).toLinearMap = σ.toLinearMap ^ n := (AlgEquiv.toLinearMapHom R A₁).map_pow σ n @[simp] lemma one_toLinearMap : (1 : A₁ ≃ₐ[R] A₁).toLinearMap = 1 := rfl /-- The units group of `S →ₐ[R] S` is `S ≃ₐ[R] S`. See `LinearMap.GeneralLinearGroup.generalLinearEquiv` for the linear map version. -/ @[simps] def algHomUnitsEquiv (R S : Type*) [CommSemiring R] [Semiring S] [Algebra R S] : (S →ₐ[R] S)ˣ ≃* (S ≃ₐ[R] S) where toFun := fun f ↦ { (f : S →ₐ[R] S) with invFun := ↑(f⁻¹) left_inv := (fun x ↦ show (↑(f⁻¹ * f) : S →ₐ[R] S) x = x by rw [inv_mul_self]; rfl) right_inv := (fun x ↦ show (↑(f * f⁻¹) : S →ₐ[R] S) x = x by rw [mul_inv_self]; rfl) } invFun := fun f ↦ ⟨f, f.symm, f.comp_symm, f.symm_comp⟩ left_inv := fun _ ↦ rfl right_inv := fun _ ↦ rfl map_mul' := fun _ _ ↦ rfl end Semiring section CommSemiring variable [CommSemiring R] [CommSemiring A₁] [CommSemiring A₂] variable [Algebra R A₁] [Algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[deprecated map_prod (since := "2024-06-20")] protected theorem map_prod {ι : Type*} (f : ι → A₁) (s : Finset ι) : e (∏ x ∈ s, f x) = ∏ x ∈ s, e (f x) := map_prod _ f s @[deprecated map_finsupp_prod (since := "2024-06-20")] protected theorem map_finsupp_prod {α : Type*} [Zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A₁) : e (f.prod g) = f.prod fun i a => e (g i a) := map_finsupp_prod _ f g end CommSemiring section Ring variable [CommSemiring R] [Ring A₁] [Ring A₂] variable [Algebra R A₁] [Algebra R A₂] (e : A₁ ≃ₐ[R] A₂) @[deprecated map_neg (since := "2024-06-20")] protected theorem map_neg (x) : e (-x) = -e x := map_neg e x @[deprecated map_sub (since := "2024-06-20")] protected theorem map_sub (x y) : e (x - y) = e x - e y := map_sub e x y end Ring end AlgEquiv namespace MulSemiringAction variable {M G : Type*} (R A : Type*) [CommSemiring R] [Semiring A] [Algebra R A] section variable [Group G] [MulSemiringAction G A] [SMulCommClass G R A] /-- Each element of the group defines an algebra equivalence. This is a stronger version of `MulSemiringAction.toRingEquiv` and `DistribMulAction.toLinearEquiv`. -/ @[simps! apply symm_apply toEquiv] -- Porting note: don't want redundant simps lemma `toEquiv_symm` def toAlgEquiv (g : G) : A ≃ₐ[R] A := { MulSemiringAction.toRingEquiv _ _ g, MulSemiringAction.toAlgHom R A g with } theorem toAlgEquiv_injective [FaithfulSMul G A] : Function.Injective (MulSemiringAction.toAlgEquiv R A : G → A ≃ₐ[R] A) := fun _ _ h => eq_of_smul_eq_smul fun r => AlgEquiv.ext_iff.1 h r end end MulSemiringAction
Algebra\Algebra\Field.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Data.Rat.Cast.Defs /-! # Facts about `algebraMap` when the coefficient ring is a field. -/ namespace algebraMap universe u v w u₁ v₁ section FieldNontrivial variable {R A : Type*} [Field R] [CommSemiring A] [Nontrivial A] [Algebra R A] @[norm_cast, simp] theorem coe_inj {a b : R} : (↑a : A) = ↑b ↔ a = b := (algebraMap R A).injective.eq_iff @[norm_cast] theorem lift_map_eq_zero_iff (a : R) : (↑a : A) = 0 ↔ a = 0 := map_eq_zero _ end FieldNontrivial section SemifieldSemidivisionRing variable {R : Type*} (A : Type*) [Semifield R] [DivisionSemiring A] [Algebra R A] @[norm_cast] theorem coe_inv (r : R) : ↑r⁻¹ = ((↑r)⁻¹ : A) := map_inv₀ (algebraMap R A) r @[norm_cast] theorem coe_div (r s : R) : ↑(r / s) = (↑r / ↑s : A) := map_div₀ (algebraMap R A) r s @[norm_cast] theorem coe_zpow (r : R) (z : ℤ) : ↑(r ^ z) = (r : A) ^ z := map_zpow₀ (algebraMap R A) r z end SemifieldSemidivisionRing section FieldDivisionRing variable (R A : Type*) [Field R] [DivisionRing A] [Algebra R A] @[norm_cast] theorem coe_ratCast (q : ℚ) : ↑(q : R) = (q : A) := map_ratCast (algebraMap R A) q end FieldDivisionRing end algebraMap
Algebra\Algebra\Hom.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Basic import Mathlib.Algebra.BigOperators.Finsupp /-! # Homomorphisms of `R`-algebras This file defines bundled homomorphisms of `R`-algebras. ## Main definitions * `AlgHom R A B`: the type of `R`-algebra morphisms from `A` to `B`. * `Algebra.ofId R A : R →ₐ[R] A`: the canonical map from `R` to `A`, as an `AlgHom`. ## Notations * `A →ₐ[R] B` : `R`-algebra homomorphism from `A` to `B`. -/ universe u v w u₁ v₁ /-- Defining the homomorphism in the category R-Alg. -/ -- @[nolint has_nonempty_instance] -- Porting note(#5171): linter not ported yet structure AlgHom (R : Type u) (A : Type v) (B : Type w) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] extends RingHom A B where commutes' : ∀ r : R, toFun (algebraMap R A r) = algebraMap R B r /-- Reinterpret an `AlgHom` as a `RingHom` -/ add_decl_doc AlgHom.toRingHom @[inherit_doc AlgHom] infixr:25 " →ₐ " => AlgHom _ @[inherit_doc] notation:25 A " →ₐ[" R "] " B => AlgHom R A B /-- `AlgHomClass F R A B` asserts `F` is a type of bundled algebra homomorphisms from `A` to `B`. -/ class AlgHomClass (F : Type*) (R A B : outParam Type*) [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [FunLike F A B] extends RingHomClass F A B : Prop where commutes : ∀ (f : F) (r : R), f (algebraMap R A r) = algebraMap R B r -- Porting note: `dangerousInstance` linter has become smarter about `outParam`s -- attribute [nolint dangerousInstance] AlgHomClass.toRingHomClass -- Porting note (#10618): simp can prove this -- attribute [simp] AlgHomClass.commutes namespace AlgHomClass variable {R A B F : Type*} [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] [FunLike F A B] -- see Note [lower instance priority] instance (priority := 100) linearMapClass [AlgHomClass F R A B] : LinearMapClass F R A B := { ‹AlgHomClass F R A B› with map_smulₛₗ := fun f r x => by simp only [Algebra.smul_def, map_mul, commutes, RingHom.id_apply] } -- Porting note (#11445): A new definition underlying a coercion `↑`. /-- Turn an element of a type `F` satisfying `AlgHomClass F α β` into an actual `AlgHom`. This is declared as the default coercion from `F` to `α →+* β`. -/ @[coe] def toAlgHom {F : Type*} [FunLike F A B] [AlgHomClass F R A B] (f : F) : A →ₐ[R] B where __ := (f : A →+* B) toFun := f commutes' := AlgHomClass.commutes f instance coeTC {F : Type*} [FunLike F A B] [AlgHomClass F R A B] : CoeTC F (A →ₐ[R] B) := ⟨AlgHomClass.toAlgHom⟩ end AlgHomClass namespace AlgHom variable {R : Type u} {A : Type v} {B : Type w} {C : Type u₁} {D : Type v₁} section Semiring variable [CommSemiring R] [Semiring A] [Semiring B] [Semiring C] [Semiring D] variable [Algebra R A] [Algebra R B] [Algebra R C] [Algebra R D] -- Porting note: we don't port specialized `CoeFun` instances if there is `DFunLike` instead instance funLike : FunLike (A →ₐ[R] B) A B where coe f := f.toFun coe_injective' f g h := by rcases f with ⟨⟨⟨⟨_, _⟩, _⟩, _, _⟩, _⟩ rcases g with ⟨⟨⟨⟨_, _⟩, _⟩, _, _⟩, _⟩ congr -- Porting note: This instance is moved. instance algHomClass : AlgHomClass (A →ₐ[R] B) R A B where map_add f := f.map_add' map_zero f := f.map_zero' map_mul f := f.map_mul' map_one f := f.map_one' commutes f := f.commutes' /-- See Note [custom simps projection] -/ def Simps.apply {R : Type u} {α : Type v} {β : Type w} [CommSemiring R] [Semiring α] [Semiring β] [Algebra R α] [Algebra R β] (f : α →ₐ[R] β) : α → β := f initialize_simps_projections AlgHom (toFun → apply) @[simp] protected theorem coe_coe {F : Type*} [FunLike F A B] [AlgHomClass F R A B] (f : F) : ⇑(f : A →ₐ[R] B) = f := rfl @[simp] theorem toFun_eq_coe (f : A →ₐ[R] B) : f.toFun = f := rfl -- Porting note (#11445): A new definition underlying a coercion `↑`. @[coe] def toMonoidHom' (f : A →ₐ[R] B) : A →* B := (f : A →+* B) instance coeOutMonoidHom : CoeOut (A →ₐ[R] B) (A →* B) := ⟨AlgHom.toMonoidHom'⟩ -- Porting note (#11445): A new definition underlying a coercion `↑`. @[coe] def toAddMonoidHom' (f : A →ₐ[R] B) : A →+ B := (f : A →+* B) instance coeOutAddMonoidHom : CoeOut (A →ₐ[R] B) (A →+ B) := ⟨AlgHom.toAddMonoidHom'⟩ -- Porting note: Lean 3: `@[simp, norm_cast] coe_mk` -- Lean 4: `@[simp] coe_mk` & `@[norm_cast] coe_mks` @[simp] theorem coe_mk {f : A →+* B} (h) : ((⟨f, h⟩ : A →ₐ[R] B) : A → B) = f := rfl @[norm_cast] theorem coe_mks {f : A → B} (h₁ h₂ h₃ h₄ h₅) : ⇑(⟨⟨⟨⟨f, h₁⟩, h₂⟩, h₃, h₄⟩, h₅⟩ : A →ₐ[R] B) = f := rfl -- Porting note: This theorem is new. @[simp, norm_cast] theorem coe_ringHom_mk {f : A →+* B} (h) : ((⟨f, h⟩ : A →ₐ[R] B) : A →+* B) = f := rfl -- make the coercion the simp-normal form @[simp] theorem toRingHom_eq_coe (f : A →ₐ[R] B) : f.toRingHom = f := rfl @[simp, norm_cast] theorem coe_toRingHom (f : A →ₐ[R] B) : ⇑(f : A →+* B) = f := rfl @[simp, norm_cast] theorem coe_toMonoidHom (f : A →ₐ[R] B) : ⇑(f : A →* B) = f := rfl @[simp, norm_cast] theorem coe_toAddMonoidHom (f : A →ₐ[R] B) : ⇑(f : A →+ B) = f := rfl variable (φ : A →ₐ[R] B) theorem coe_fn_injective : @Function.Injective (A →ₐ[R] B) (A → B) (↑) := DFunLike.coe_injective theorem coe_fn_inj {φ₁ φ₂ : A →ₐ[R] B} : (φ₁ : A → B) = φ₂ ↔ φ₁ = φ₂ := DFunLike.coe_fn_eq theorem coe_ringHom_injective : Function.Injective ((↑) : (A →ₐ[R] B) → A →+* B) := fun φ₁ φ₂ H => coe_fn_injective <| show ((φ₁ : A →+* B) : A → B) = ((φ₂ : A →+* B) : A → B) from congr_arg _ H theorem coe_monoidHom_injective : Function.Injective ((↑) : (A →ₐ[R] B) → A →* B) := RingHom.coe_monoidHom_injective.comp coe_ringHom_injective theorem coe_addMonoidHom_injective : Function.Injective ((↑) : (A →ₐ[R] B) → A →+ B) := RingHom.coe_addMonoidHom_injective.comp coe_ringHom_injective protected theorem congr_fun {φ₁ φ₂ : A →ₐ[R] B} (H : φ₁ = φ₂) (x : A) : φ₁ x = φ₂ x := DFunLike.congr_fun H x protected theorem congr_arg (φ : A →ₐ[R] B) {x y : A} (h : x = y) : φ x = φ y := DFunLike.congr_arg φ h @[ext] theorem ext {φ₁ φ₂ : A →ₐ[R] B} (H : ∀ x, φ₁ x = φ₂ x) : φ₁ = φ₂ := DFunLike.ext _ _ H @[simp] theorem mk_coe {f : A →ₐ[R] B} (h₁ h₂ h₃ h₄ h₅) : (⟨⟨⟨⟨f, h₁⟩, h₂⟩, h₃, h₄⟩, h₅⟩ : A →ₐ[R] B) = f := ext fun _ => rfl @[simp] theorem commutes (r : R) : φ (algebraMap R A r) = algebraMap R B r := φ.commutes' r theorem comp_algebraMap : (φ : A →+* B).comp (algebraMap R A) = algebraMap R B := RingHom.ext <| φ.commutes @[deprecated map_add (since := "2024-06-26")] protected theorem map_add (r s : A) : φ (r + s) = φ r + φ s := map_add _ _ _ @[deprecated map_zero (since := "2024-06-26")] protected theorem map_zero : φ 0 = 0 := map_zero _ @[deprecated map_mul (since := "2024-06-26")] protected theorem map_mul (x y) : φ (x * y) = φ x * φ y := map_mul _ _ _ @[deprecated map_one (since := "2024-06-26")] protected theorem map_one : φ 1 = 1 := map_one _ @[deprecated map_pow (since := "2024-06-26")] protected theorem map_pow (x : A) (n : ℕ) : φ (x ^ n) = φ x ^ n := map_pow _ _ _ -- @[simp] -- Porting note (#10618): simp can prove this @[deprecated map_smul (since := "2024-06-26")] protected theorem map_smul (r : R) (x : A) : φ (r • x) = r • φ x := map_smul _ _ _ @[deprecated map_sum (since := "2024-06-26")] protected theorem map_sum {ι : Type*} (f : ι → A) (s : Finset ι) : φ (∑ x ∈ s, f x) = ∑ x ∈ s, φ (f x) := map_sum _ _ _ @[deprecated map_finsupp_sum (since := "2024-06-26")] protected theorem map_finsupp_sum {α : Type*} [Zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.sum g) = f.sum fun i a => φ (g i a) := map_finsupp_sum _ _ _ /-- If a `RingHom` is `R`-linear, then it is an `AlgHom`. -/ def mk' (f : A →+* B) (h : ∀ (c : R) (x), f (c • x) = c • f x) : A →ₐ[R] B := { f with toFun := f commutes' := fun c => by simp only [Algebra.algebraMap_eq_smul_one, h, f.map_one] } @[simp] theorem coe_mk' (f : A →+* B) (h : ∀ (c : R) (x), f (c • x) = c • f x) : ⇑(mk' f h) = f := rfl section variable (R A) /-- Identity map as an `AlgHom`. -/ protected def id : A →ₐ[R] A := { RingHom.id A with commutes' := fun _ => rfl } @[simp] theorem coe_id : ⇑(AlgHom.id R A) = id := rfl @[simp] theorem id_toRingHom : (AlgHom.id R A : A →+* A) = RingHom.id _ := rfl end theorem id_apply (p : A) : AlgHom.id R A p = p := rfl /-- Composition of algebra homeomorphisms. -/ def comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : A →ₐ[R] C := { φ₁.toRingHom.comp ↑φ₂ with commutes' := fun r : R => by rw [← φ₁.commutes, ← φ₂.commutes]; rfl } @[simp] theorem coe_comp (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : ⇑(φ₁.comp φ₂) = φ₁ ∘ φ₂ := rfl theorem comp_apply (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) (p : A) : φ₁.comp φ₂ p = φ₁ (φ₂ p) := rfl theorem comp_toRingHom (φ₁ : B →ₐ[R] C) (φ₂ : A →ₐ[R] B) : (φ₁.comp φ₂ : A →+* C) = (φ₁ : B →+* C).comp ↑φ₂ := rfl @[simp] theorem comp_id : φ.comp (AlgHom.id R A) = φ := ext fun _x => rfl @[simp] theorem id_comp : (AlgHom.id R B).comp φ = φ := ext fun _x => rfl theorem comp_assoc (φ₁ : C →ₐ[R] D) (φ₂ : B →ₐ[R] C) (φ₃ : A →ₐ[R] B) : (φ₁.comp φ₂).comp φ₃ = φ₁.comp (φ₂.comp φ₃) := ext fun _x => rfl /-- R-Alg ⥤ R-Mod -/ def toLinearMap : A →ₗ[R] B where toFun := φ map_add' := map_add _ map_smul' := map_smul _ @[simp] theorem toLinearMap_apply (p : A) : φ.toLinearMap p = φ p := rfl theorem toLinearMap_injective : Function.Injective (toLinearMap : _ → A →ₗ[R] B) := fun _φ₁ _φ₂ h => ext <| LinearMap.congr_fun h @[simp] theorem comp_toLinearMap (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).toLinearMap = g.toLinearMap.comp f.toLinearMap := rfl @[simp] theorem toLinearMap_id : toLinearMap (AlgHom.id R A) = LinearMap.id := LinearMap.ext fun _ => rfl /-- Promote a `LinearMap` to an `AlgHom` by supplying proofs about the behavior on `1` and `*`. -/ @[simps] def ofLinearMap (f : A →ₗ[R] B) (map_one : f 1 = 1) (map_mul : ∀ x y, f (x * y) = f x * f y) : A →ₐ[R] B := { f.toAddMonoidHom with toFun := f map_one' := map_one map_mul' := map_mul commutes' := fun c => by simp only [Algebra.algebraMap_eq_smul_one, f.map_smul, map_one] } @[simp] theorem ofLinearMap_toLinearMap (map_one) (map_mul) : ofLinearMap φ.toLinearMap map_one map_mul = φ := by ext rfl @[simp] theorem toLinearMap_ofLinearMap (f : A →ₗ[R] B) (map_one) (map_mul) : toLinearMap (ofLinearMap f map_one map_mul) = f := by ext rfl @[simp] theorem ofLinearMap_id (map_one) (map_mul) : ofLinearMap LinearMap.id map_one map_mul = AlgHom.id R A := ext fun _ => rfl theorem map_smul_of_tower {R'} [SMul R' A] [SMul R' B] [LinearMap.CompatibleSMul A B R' R] (r : R') (x : A) : φ (r • x) = r • φ x := φ.toLinearMap.map_smul_of_tower r x @[deprecated map_list_prod (since := "2024-06-26")] protected theorem map_list_prod (s : List A) : φ s.prod = (s.map φ).prod := map_list_prod φ s @[simps (config := .lemmasOnly) toSemigroup_toMul_mul toOne_one] instance End : Monoid (A →ₐ[R] A) where mul := comp mul_assoc ϕ ψ χ := rfl one := AlgHom.id R A one_mul ϕ := ext fun x => rfl mul_one ϕ := ext fun x => rfl @[simp] theorem one_apply (x : A) : (1 : A →ₐ[R] A) x = x := rfl @[simp] theorem mul_apply (φ ψ : A →ₐ[R] A) (x : A) : (φ * ψ) x = φ (ψ x) := rfl theorem algebraMap_eq_apply (f : A →ₐ[R] B) {y : R} {x : A} (h : algebraMap R A y = x) : algebraMap R B y = f x := h ▸ (f.commutes _).symm end Semiring section CommSemiring variable [CommSemiring R] [CommSemiring A] [CommSemiring B] variable [Algebra R A] [Algebra R B] (φ : A →ₐ[R] B) @[deprecated map_multiset_prod (since := "2024-06-26")] protected theorem map_multiset_prod (s : Multiset A) : φ s.prod = (s.map φ).prod := map_multiset_prod _ _ @[deprecated map_prod (since := "2024-06-26")] protected theorem map_prod {ι : Type*} (f : ι → A) (s : Finset ι) : φ (∏ x ∈ s, f x) = ∏ x ∈ s, φ (f x) := map_prod _ _ _ @[deprecated map_finsupp_prod (since := "2024-06-26")] protected theorem map_finsupp_prod {α : Type*} [Zero α] {ι : Type*} (f : ι →₀ α) (g : ι → α → A) : φ (f.prod g) = f.prod fun i a => φ (g i a) := map_finsupp_prod _ _ _ end CommSemiring section Ring variable [CommSemiring R] [Ring A] [Ring B] variable [Algebra R A] [Algebra R B] (φ : A →ₐ[R] B) @[deprecated map_neg (since := "2024-06-26")] protected theorem map_neg (x) : φ (-x) = -φ x := map_neg _ _ @[deprecated map_sub (since := "2024-06-26")] protected theorem map_sub (x y) : φ (x - y) = φ x - φ y := map_sub _ _ _ end Ring end AlgHom namespace RingHom variable {R S : Type*} /-- Reinterpret a `RingHom` as an `ℕ`-algebra homomorphism. -/ def toNatAlgHom [Semiring R] [Semiring S] (f : R →+* S) : R →ₐ[ℕ] S := { f with toFun := f commutes' := fun n => by simp } /-- Reinterpret a `RingHom` as a `ℤ`-algebra homomorphism. -/ def toIntAlgHom [Ring R] [Ring S] [Algebra ℤ R] [Algebra ℤ S] (f : R →+* S) : R →ₐ[ℤ] S := { f with commutes' := fun n => by simp } lemma toIntAlgHom_injective [Ring R] [Ring S] [Algebra ℤ R] [Algebra ℤ S] : Function.Injective (RingHom.toIntAlgHom : (R →+* S) → _) := fun _ _ e ↦ DFunLike.ext _ _ (fun x ↦ DFunLike.congr_fun e x) end RingHom namespace Algebra variable (R : Type u) (A : Type v) variable [CommSemiring R] [Semiring A] [Algebra R A] /-- `AlgebraMap` as an `AlgHom`. -/ def ofId : R →ₐ[R] A := { algebraMap R A with commutes' := fun _ => rfl } variable {R} theorem ofId_apply (r) : ofId R A r = algebraMap R A r := rfl /-- This is a special case of a more general instance that we define in a later file. -/ instance subsingleton_id : Subsingleton (R →ₐ[R] A) := ⟨fun f g => AlgHom.ext fun _ => (f.commutes _).trans (g.commutes _).symm⟩ /-- This ext lemma closes trivial subgoals create when chaining heterobasic ext lemmas. -/ @[ext high] theorem ext_id (f g : R →ₐ[R] A) : f = g := Subsingleton.elim _ _ section MulDistribMulAction instance : MulDistribMulAction (A →ₐ[R] A) Aˣ where smul := fun f => Units.map f one_smul := fun x => by ext; rfl mul_smul := fun x y z => by ext; rfl smul_mul := fun x y z => by ext; exact map_mul _ _ _ smul_one := fun x => by ext; exact map_one _ @[simp] theorem smul_units_def (f : A →ₐ[R] A) (x : Aˣ) : f • x = Units.map (f : A →* A) x := rfl end MulDistribMulAction variable (M : Submonoid R) {B : Type w} [CommRing B] [Algebra R B] {A} lemma algebraMapSubmonoid_map_eq (f : A →ₐ[R] B) : (algebraMapSubmonoid A M).map f = algebraMapSubmonoid B M := by ext x constructor · rintro ⟨a, ⟨r, hr, rfl⟩, rfl⟩ simp only [AlgHom.commutes] use r · rintro ⟨r, hr, rfl⟩ simp only [Submonoid.mem_map] use (algebraMap R A r) simp only [AlgHom.commutes, and_true] use r lemma algebraMapSubmonoid_le_comap (f : A →ₐ[R] B) : algebraMapSubmonoid A M ≤ (algebraMapSubmonoid B M).comap f.toRingHom := by rw [← algebraMapSubmonoid_map_eq M f] exact Submonoid.le_comap_map (Algebra.algebraMapSubmonoid A M) end Algebra namespace MulSemiringAction variable {M G : Type*} (R A : Type*) [CommSemiring R] [Semiring A] [Algebra R A] variable [Monoid M] [MulSemiringAction M A] [SMulCommClass M R A] /-- Each element of the monoid defines an algebra homomorphism. This is a stronger version of `MulSemiringAction.toRingHom` and `DistribMulAction.toLinearMap`. -/ @[simps] def toAlgHom (m : M) : A →ₐ[R] A := { MulSemiringAction.toRingHom _ _ m with toFun := fun a => m • a commutes' := smul_algebraMap _ } theorem toAlgHom_injective [FaithfulSMul M A] : Function.Injective (MulSemiringAction.toAlgHom R A : M → A →ₐ[R] A) := fun _m₁ _m₂ h => eq_of_smul_eq_smul fun r => AlgHom.ext_iff.1 h r end MulSemiringAction
Algebra\Algebra\NonUnitalHom.lean
/- Copyright (c) 2021 Oliver Nash. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Oliver Nash -/ import Mathlib.Algebra.Algebra.Hom import Mathlib.Algebra.GroupWithZero.Action.Prod /-! # Morphisms of non-unital algebras This file defines morphisms between two types, each of which carries: * an addition, * an additive zero, * a multiplication, * a scalar action. The multiplications are not assumed to be associative or unital, or even to be compatible with the scalar actions. In a typical application, the operations will satisfy compatibility conditions making them into algebras (albeit possibly non-associative and/or non-unital) but such conditions are not required to make this definition. This notion of morphism should be useful for any category of non-unital algebras. The motivating application at the time it was introduced was to be able to state the adjunction property for magma algebras. These are non-unital, non-associative algebras obtained by applying the group-algebra construction except where we take a type carrying just `Mul` instead of `Group`. For a plausible future application, one could take the non-unital algebra of compactly-supported functions on a non-compact topological space. A proper map between a pair of such spaces (contravariantly) induces a morphism between their algebras of compactly-supported functions which will be a `NonUnitalAlgHom`. TODO: add `NonUnitalAlgEquiv` when needed. ## Main definitions * `NonUnitalAlgHom` * `AlgHom.toNonUnitalAlgHom` ## Tags non-unital, algebra, morphism -/ universe u u₁ v w w₁ w₂ w₃ variable {R : Type u} {S : Type u₁} /-- A morphism respecting addition, multiplication, and scalar multiplication. When these arise from algebra structures, this is the same as a not-necessarily-unital morphism of algebras. -/ structure NonUnitalAlgHom [Monoid R] [Monoid S] (φ : R →* S) (A : Type v) (B : Type w) [NonUnitalNonAssocSemiring A] [DistribMulAction R A] [NonUnitalNonAssocSemiring B] [DistribMulAction S B] extends A →ₑ+[φ] B, A →ₙ* B @[inherit_doc NonUnitalAlgHom] infixr:25 " →ₙₐ " => NonUnitalAlgHom _ @[inherit_doc] notation:25 A " →ₛₙₐ[" φ "] " B => NonUnitalAlgHom φ A B @[inherit_doc] notation:25 A " →ₙₐ[" R "] " B => NonUnitalAlgHom (MonoidHom.id R) A B attribute [nolint docBlame] NonUnitalAlgHom.toMulHom /-- `NonUnitalAlgSemiHomClass F φ A B` asserts `F` is a type of bundled algebra homomorphisms from `A` to `B` which are equivariant with respect to `φ`. -/ class NonUnitalAlgSemiHomClass (F : Type*) {R S : outParam Type*} [Monoid R] [Monoid S] (φ : outParam (R →* S)) (A B : outParam Type*) [NonUnitalNonAssocSemiring A] [NonUnitalNonAssocSemiring B] [DistribMulAction R A] [DistribMulAction S B] [FunLike F A B] extends DistribMulActionSemiHomClass F φ A B, MulHomClass F A B : Prop /-- `NonUnitalAlgHomClass F R A B` asserts `F` is a type of bundled algebra homomorphisms from `A` to `B` which are `R`-linear. This is an abbreviation to `NonUnitalAlgSemiHomClass F (MonoidHom.id R) A B` -/ abbrev NonUnitalAlgHomClass (F : Type*) (R A B : outParam Type*) [Monoid R] [NonUnitalNonAssocSemiring A] [NonUnitalNonAssocSemiring B] [DistribMulAction R A] [DistribMulAction R B] [FunLike F A B] := NonUnitalAlgSemiHomClass F (MonoidHom.id R) A B -- Porting note: commented out, not dangerous -- attribute [nolint dangerousInstance] NonUnitalAlgHomClass.toMulHomClass namespace NonUnitalAlgHomClass -- Porting note: Made following instance non-dangerous through [...] -> [...] replacement -- See note [lower instance priority] instance (priority := 100) toNonUnitalRingHomClass {F R S A B : Type*} {_ : Monoid R} {_ : Monoid S} {φ : outParam (R →* S)} {_ : NonUnitalNonAssocSemiring A} [DistribMulAction R A] {_ : NonUnitalNonAssocSemiring B} [DistribMulAction S B] [FunLike F A B] [NonUnitalAlgSemiHomClass F φ A B] : NonUnitalRingHomClass F A B := { ‹NonUnitalAlgSemiHomClass F φ A B› with } variable [Semiring R] [Semiring S] {φ : R →+* S} {A B : Type*} [NonUnitalNonAssocSemiring A] [Module R A] [NonUnitalNonAssocSemiring B] [Module S B] -- see Note [lower instance priority] instance (priority := 100) {F R S A B : Type*} {_ : Semiring R} {_ : Semiring S} {φ : R →+* S} {_ : NonUnitalSemiring A} {_ : NonUnitalSemiring B} [Module R A] [Module S B] [FunLike F A B] [NonUnitalAlgSemiHomClass (R := R) (S := S) F φ A B] : SemilinearMapClass F φ A B := { ‹NonUnitalAlgSemiHomClass F φ A B› with map_smulₛₗ := map_smulₛₗ } instance (priority := 100) {F : Type*} [FunLike F A B] [Module R B] [NonUnitalAlgHomClass F R A B] : LinearMapClass F R A B := { ‹NonUnitalAlgHomClass F R A B› with map_smulₛₗ := map_smulₛₗ } /-- Turn an element of a type `F` satisfying `NonUnitalAlgSemiHomClass F φ A B` into an actual `NonUnitalAlgHom`. This is declared as the default coercion from `F` to `A →ₛₙₐ[φ] B`. -/ @[coe] def toNonUnitalAlgSemiHom {F R S : Type*} [Monoid R] [Monoid S] {φ : R →* S} {A B : Type*} [NonUnitalNonAssocSemiring A] [DistribMulAction R A] [NonUnitalNonAssocSemiring B] [DistribMulAction S B] [FunLike F A B] [NonUnitalAlgSemiHomClass F φ A B] (f : F) : A →ₛₙₐ[φ] B := { (f : A →ₙ+* B) with toFun := f map_smul' := map_smulₛₗ f } instance {F R S A B : Type*} [Monoid R] [Monoid S] {φ : R →* S} [NonUnitalNonAssocSemiring A] [DistribMulAction R A] [NonUnitalNonAssocSemiring B] [DistribMulAction S B] [FunLike F A B] [NonUnitalAlgSemiHomClass F φ A B] : CoeTC F (A →ₛₙₐ[φ] B) := ⟨toNonUnitalAlgSemiHom⟩ /-- Turn an element of a type `F` satisfying `NonUnitalAlgHomClass F R A B` into an actual @[coe] `NonUnitalAlgHom`. This is declared as the default coercion from `F` to `A →ₛₙₐ[R] B`. -/ def toNonUnitalAlgHom {F R : Type*} [Monoid R] {A B : Type*} [NonUnitalNonAssocSemiring A] [DistribMulAction R A] [NonUnitalNonAssocSemiring B] [DistribMulAction R B] [FunLike F A B] [NonUnitalAlgHomClass F R A B] (f : F) : A →ₙₐ[R] B := { (f : A →ₙ+* B) with toFun := f map_smul' := map_smulₛₗ f } instance {F R : Type*} [Monoid R] {A B : Type*} [NonUnitalNonAssocSemiring A] [DistribMulAction R A] [NonUnitalNonAssocSemiring B] [DistribMulAction R B] [FunLike F A B] [NonUnitalAlgHomClass F R A B] : CoeTC F (A →ₙₐ[R] B) := ⟨toNonUnitalAlgHom⟩ end NonUnitalAlgHomClass namespace NonUnitalAlgHom variable {T : Type*} [Monoid R] [Monoid S] [Monoid T] (φ : R →* S) variable (A : Type v) (B : Type w) (C : Type w₁) variable [NonUnitalNonAssocSemiring A] [DistribMulAction R A] variable [NonUnitalNonAssocSemiring B] [DistribMulAction S B] variable [NonUnitalNonAssocSemiring C] [DistribMulAction T C] -- Porting note: Replaced with DFunLike instance -- /-- see Note [function coercion] -/ -- instance : CoeFun (A →ₙₐ[R] B) fun _ => A → B := -- ⟨toFun⟩ instance : DFunLike (A →ₛₙₐ[φ] B) A fun _ => B where coe f := f.toFun coe_injective' := by rintro ⟨⟨⟨f, _⟩, _⟩, _⟩ ⟨⟨⟨g, _⟩, _⟩, _⟩ h; congr @[simp] theorem toFun_eq_coe (f : A →ₛₙₐ[φ] B) : f.toFun = ⇑f := rfl /-- See Note [custom simps projection] -/ def Simps.apply (f : A →ₛₙₐ[φ] B) : A → B := f initialize_simps_projections NonUnitalAlgHom (toDistribMulActionHom_toMulActionHom_toFun → apply, -toDistribMulActionHom) variable {φ A B C} @[simp] protected theorem coe_coe {F : Type*} [FunLike F A B] [NonUnitalAlgSemiHomClass F φ A B] (f : F) : ⇑(f : A →ₛₙₐ[φ] B) = f := rfl theorem coe_injective : @Function.Injective (A →ₛₙₐ[φ] B) (A → B) (↑) := by rintro ⟨⟨⟨f, _⟩, _⟩, _⟩ ⟨⟨⟨g, _⟩, _⟩, _⟩ h; congr instance : FunLike (A →ₛₙₐ[φ] B) A B where coe f := f.toFun coe_injective' := coe_injective instance : NonUnitalAlgSemiHomClass (A →ₛₙₐ[φ] B) φ A B where map_add f := f.map_add' map_zero f := f.map_zero' map_mul f := f.map_mul' map_smulₛₗ f := f.map_smul' @[ext] theorem ext {f g : A →ₛₙₐ[φ] B} (h : ∀ x, f x = g x) : f = g := coe_injective <| funext h theorem congr_fun {f g : A →ₛₙₐ[φ] B} (h : f = g) (x : A) : f x = g x := h ▸ rfl @[simp] theorem coe_mk (f : A → B) (h₁ h₂ h₃ h₄) : ⇑(⟨⟨⟨f, h₁⟩, h₂, h₃⟩, h₄⟩ : A →ₛₙₐ[φ] B) = f := rfl @[simp] theorem mk_coe (f : A →ₛₙₐ[φ] B) (h₁ h₂ h₃ h₄) : (⟨⟨⟨f, h₁⟩, h₂, h₃⟩, h₄⟩ : A →ₛₙₐ[φ] B) = f := by rfl instance : CoeOut (A →ₛₙₐ[φ] B) (A →ₑ+[φ] B) := ⟨toDistribMulActionHom⟩ instance : CoeOut (A →ₛₙₐ[φ] B) (A →ₙ* B) := ⟨toMulHom⟩ @[simp] theorem toDistribMulActionHom_eq_coe (f : A →ₛₙₐ[φ] B) : f.toDistribMulActionHom = ↑f := rfl @[simp] theorem toMulHom_eq_coe (f : A →ₛₙₐ[φ] B) : f.toMulHom = ↑f := rfl @[simp, norm_cast] theorem coe_to_distribMulActionHom (f : A →ₛₙₐ[φ] B) : ⇑(f : A →ₑ+[φ] B) = f := rfl @[simp, norm_cast] theorem coe_to_mulHom (f : A →ₛₙₐ[φ] B) : ⇑(f : A →ₙ* B) = f := rfl theorem to_distribMulActionHom_injective {f g : A →ₛₙₐ[φ] B} (h : (f : A →ₑ+[φ] B) = (g : A →ₑ+[φ] B)) : f = g := by ext a exact DistribMulActionHom.congr_fun h a theorem to_mulHom_injective {f g : A →ₛₙₐ[φ] B} (h : (f : A →ₙ* B) = (g : A →ₙ* B)) : f = g := by ext a exact DFunLike.congr_fun h a @[norm_cast] theorem coe_distribMulActionHom_mk (f : A →ₛₙₐ[φ] B) (h₁ h₂ h₃ h₄) : ((⟨⟨⟨f, h₁⟩, h₂, h₃⟩, h₄⟩ : A →ₛₙₐ[φ] B) : A →ₑ+[φ] B) = ⟨⟨f, h₁⟩, h₂, h₃⟩ := by rfl @[norm_cast] theorem coe_mulHom_mk (f : A →ₛₙₐ[φ] B) (h₁ h₂ h₃ h₄) : ((⟨⟨⟨f, h₁⟩, h₂, h₃⟩, h₄⟩ : A →ₛₙₐ[φ] B) : A →ₙ* B) = ⟨f, h₄⟩ := by rfl -- @[simp] -- Porting note (#10618) : simp can prove this protected theorem map_smul (f : A →ₛₙₐ[φ] B) (c : R) (x : A) : f (c • x) = (φ c) • f x := map_smulₛₗ _ _ _ -- @[simp] -- Porting note (#10618) : simp can prove this protected theorem map_add (f : A →ₛₙₐ[φ] B) (x y : A) : f (x + y) = f x + f y := map_add _ _ _ -- @[simp] -- Porting note (#10618) : simp can prove this protected theorem map_mul (f : A →ₛₙₐ[φ] B) (x y : A) : f (x * y) = f x * f y := map_mul _ _ _ -- @[simp] -- Porting note (#10618) : simp can prove this protected theorem map_zero (f : A →ₛₙₐ[φ] B) : f 0 = 0 := map_zero _ /-- The identity map as a `NonUnitalAlgHom`. -/ protected def id (R A : Type*) [Monoid R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] : A →ₙₐ[R] A := { NonUnitalRingHom.id A with toFun := id map_smul' := fun _ _ => rfl } @[simp] theorem coe_id : ⇑(NonUnitalAlgHom.id R A) = id := rfl instance : Zero (A →ₛₙₐ[φ] B) := ⟨{ (0 : A →ₑ+[φ] B) with map_mul' := by simp }⟩ instance : One (A →ₙₐ[R] A) := ⟨NonUnitalAlgHom.id R A⟩ @[simp] theorem coe_zero : ⇑(0 : A →ₛₙₐ[φ] B) = 0 := rfl @[simp] theorem coe_one : ((1 : A →ₙₐ[R] A) : A → A) = id := rfl theorem zero_apply (a : A) : (0 : A →ₛₙₐ[φ] B) a = 0 := rfl theorem one_apply (a : A) : (1 : A →ₙₐ[R] A) a = a := rfl instance : Inhabited (A →ₛₙₐ[φ] B) := ⟨0⟩ variable {φ' : S →* R} {ψ : S →* T} {χ : R →* T} set_option linter.unusedVariables false in /-- The composition of morphisms is a morphism. -/ def comp (f : B →ₛₙₐ[ψ] C) (g : A →ₛₙₐ[φ] B) [κ : MonoidHom.CompTriple φ ψ χ] : A →ₛₙₐ[χ] C := { (f : B →ₙ* C).comp (g : A →ₙ* B), (f : B →ₑ+[ψ] C).comp (g : A →ₑ+[φ] B) with } @[simp, norm_cast] theorem coe_comp (f : B →ₛₙₐ[ψ] C) (g : A →ₛₙₐ[φ] B) [MonoidHom.CompTriple φ ψ χ] : ⇑(f.comp g) = (⇑f) ∘ (⇑g) := rfl theorem comp_apply (f : B →ₛₙₐ[ψ] C) (g : A →ₛₙₐ[φ] B) [MonoidHom.CompTriple φ ψ χ] (x : A) : f.comp g x = f (g x) := rfl variable {B₁ : Type*} [NonUnitalNonAssocSemiring B₁] [DistribMulAction R B₁] /-- The inverse of a bijective morphism is a morphism. -/ def inverse (f : A →ₙₐ[R] B₁) (g : B₁ → A) (h₁ : Function.LeftInverse g f) (h₂ : Function.RightInverse g f) : B₁ →ₙₐ[R] A := { (f : A →ₙ* B₁).inverse g h₁ h₂, (f : A →+[R] B₁).inverse g h₁ h₂ with } @[simp] theorem coe_inverse (f : A →ₙₐ[R] B₁) (g : B₁ → A) (h₁ : Function.LeftInverse g f) (h₂ : Function.RightInverse g f) : (inverse f g h₁ h₂ : B₁ → A) = g := rfl /-- The inverse of a bijective morphism is a morphism. -/ def inverse' (f : A →ₛₙₐ[φ] B) (g : B → A) (k : Function.RightInverse φ' φ) (h₁ : Function.LeftInverse g f) (h₂ : Function.RightInverse g f) : B →ₛₙₐ[φ'] A := { (f : A →ₙ* B).inverse g h₁ h₂, (f : A →ₑ+[φ] B).inverse' g k h₁ h₂ with map_zero' := by simp only [MulHom.toFun_eq_coe, MulHom.inverse_apply] rw [← f.map_zero, h₁] map_add' := fun x y ↦ by simp only [MulHom.toFun_eq_coe, MulHom.inverse_apply] rw [← h₂ x, ← h₂ y, ← map_add, h₁, h₂, h₂] } @[simp] theorem coe_inverse' (f : A →ₛₙₐ[φ] B) (g : B → A) (k : Function.RightInverse φ' φ) (h₁ : Function.LeftInverse g f) (h₂ : Function.RightInverse g f) : (inverse' f g k h₁ h₂ : B → A) = g := rfl /-! ### Operations on the product type Note that much of this is copied from [`LinearAlgebra/Prod`](../../LinearAlgebra/Prod). -/ section Prod variable (R A B) variable [DistribMulAction R B] /-- The first projection of a product is a non-unital alg_hom. -/ @[simps] def fst : A × B →ₙₐ[R] A where toFun := Prod.fst map_zero' := rfl map_add' _ _ := rfl map_smul' _ _ := rfl map_mul' _ _ := rfl /-- The second projection of a product is a non-unital alg_hom. -/ @[simps] def snd : A × B →ₙₐ[R] B where toFun := Prod.snd map_zero' := rfl map_add' _ _ := rfl map_smul' _ _ := rfl map_mul' _ _ := rfl variable {R A B} variable [DistribMulAction R C] /-- The prod of two morphisms is a morphism. -/ @[simps] def prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : A →ₙₐ[R] B × C where toFun := Pi.prod f g map_zero' := by simp only [Pi.prod, Prod.zero_eq_mk, map_zero] map_add' x y := by simp only [Pi.prod, Prod.mk_add_mk, map_add] map_mul' x y := by simp only [Pi.prod, Prod.mk_mul_mk, map_mul] map_smul' c x := by simp only [Pi.prod, map_smul, MonoidHom.id_apply, id_eq, Prod.smul_mk] theorem coe_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : ⇑(f.prod g) = Pi.prod f g := rfl @[simp] theorem fst_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : (fst R B C).comp (prod f g) = f := by rfl @[simp] theorem snd_prod (f : A →ₙₐ[R] B) (g : A →ₙₐ[R] C) : (snd R B C).comp (prod f g) = g := by rfl @[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 := coe_injective Pi.prod_fst_snd /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. -/ @[simps] def prodEquiv : (A →ₙₐ[R] B) × (A →ₙₐ[R] C) ≃ (A →ₙₐ[R] B × C) where toFun f := f.1.prod f.2 invFun f := ((fst _ _ _).comp f, (snd _ _ _).comp f) left_inv _ := rfl right_inv _ := rfl variable (R A B) /-- The left injection into a product is a non-unital algebra homomorphism. -/ def inl : A →ₙₐ[R] A × B := prod 1 0 /-- The right injection into a product is a non-unital algebra homomorphism. -/ def inr : B →ₙₐ[R] A × B := prod 0 1 variable {R A B} @[simp] theorem coe_inl : (inl R A B : A → A × B) = fun x => (x, 0) := rfl theorem inl_apply (x : A) : inl R A B x = (x, 0) := rfl @[simp] theorem coe_inr : (inr R A B : B → A × B) = Prod.mk 0 := rfl theorem inr_apply (x : B) : inr R A B x = (0, x) := rfl end Prod end NonUnitalAlgHom /-! ### Interaction with `AlgHom` -/ namespace AlgHom variable {F R : Type*} [CommSemiring R] variable {A B : Type*} [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] -- see Note [lower instance priority] instance (priority := 100) [FunLike F A B] [AlgHomClass F R A B] : NonUnitalAlgHomClass F R A B := { ‹AlgHomClass F R A B› with map_smulₛₗ := map_smul } /-- A unital morphism of algebras is a `NonUnitalAlgHom`. -/ @[coe] def toNonUnitalAlgHom (f : A →ₐ[R] B) : A →ₙₐ[R] B := { f with map_smul' := map_smul f } instance NonUnitalAlgHom.hasCoe : CoeOut (A →ₐ[R] B) (A →ₙₐ[R] B) := ⟨toNonUnitalAlgHom⟩ @[simp] theorem toNonUnitalAlgHom_eq_coe (f : A →ₐ[R] B) : f.toNonUnitalAlgHom = f := rfl -- Note (#6057) : tagging simpNF because linter complains @[simp, norm_cast, nolint simpNF] theorem coe_to_nonUnitalAlgHom (f : A →ₐ[R] B) : ⇑(f.toNonUnitalAlgHom) = ⇑f := rfl end AlgHom section RestrictScalars namespace NonUnitalAlgHom variable (R : Type*) {S A B : Type*} [Monoid R] [Monoid S] [NonUnitalNonAssocSemiring A] [NonUnitalNonAssocSemiring B] [MulAction R S] [DistribMulAction S A] [DistribMulAction S B] [DistribMulAction R A] [DistribMulAction R B] [IsScalarTower R S A] [IsScalarTower R S B] /-- If a monoid `R` acts on another monoid `S`, then a non-unital algebra homomorphism over `S` can be viewed as a non-unital algebra homomorphism over `R`. -/ def restrictScalars (f : A →ₙₐ[S] B) : A →ₙₐ[R] B := { (f : A →ₙ+* B) with map_smul' := fun r x ↦ by have := map_smul f (r • 1) x; simpa } @[simp] lemma restrictScalars_apply (f : A →ₙₐ[S] B) (x : A) : f.restrictScalars R x = f x := rfl lemma coe_restrictScalars (f : A →ₙₐ[S] B) : (f.restrictScalars R : A →ₙ+* B) = f := rfl lemma coe_restrictScalars' (f : A →ₙₐ[S] B) : (f.restrictScalars R : A → B) = f := rfl theorem restrictScalars_injective : Function.Injective (restrictScalars R : (A →ₙₐ[S] B) → A →ₙₐ[R] B) := fun _ _ h ↦ ext (congr_fun h : _) end NonUnitalAlgHom end RestrictScalars
Algebra\Algebra\NonUnitalSubalgebra.lean
/- Copyright (c) 2023 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Algebra.NonUnitalHom import Mathlib.Data.Set.UnionLift import Mathlib.LinearAlgebra.Span import Mathlib.RingTheory.NonUnitalSubring.Basic /-! # Non-unital Subalgebras over Commutative Semirings In this file we define `NonUnitalSubalgebra`s and the usual operations on them (`map`, `comap`). ## TODO * once we have scalar actions by semigroups (as opposed to monoids), implement the action of a non-unital subalgebra on the larger algebra. -/ universe u u' v v' w w' section NonUnitalSubalgebraClass variable {S R A : Type*} [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] variable [SetLike S A] [NonUnitalSubsemiringClass S A] [hSR : SMulMemClass S R A] (s : S) namespace NonUnitalSubalgebraClass /-- Embedding of a non-unital subalgebra into the non-unital algebra. -/ def subtype (s : S) : s →ₙₐ[R] A := { NonUnitalSubsemiringClass.subtype s, SMulMemClass.subtype s with toFun := (↑) } @[simp] theorem coeSubtype : (subtype s : s → A) = ((↑) : s → A) := rfl end NonUnitalSubalgebraClass end NonUnitalSubalgebraClass /-- A non-unital subalgebra is a sub(semi)ring that is also a submodule. -/ structure NonUnitalSubalgebra (R : Type u) (A : Type v) [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] extends NonUnitalSubsemiring A, Submodule R A : Type v /-- Reinterpret a `NonUnitalSubalgebra` as a `NonUnitalSubsemiring`. -/ add_decl_doc NonUnitalSubalgebra.toNonUnitalSubsemiring /-- Reinterpret a `NonUnitalSubalgebra` as a `Submodule`. -/ add_decl_doc NonUnitalSubalgebra.toSubmodule namespace NonUnitalSubalgebra variable {F : Type v'} {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} section NonUnitalNonAssocSemiring variable [CommSemiring R] variable [NonUnitalNonAssocSemiring A] [NonUnitalNonAssocSemiring B] [NonUnitalNonAssocSemiring C] variable [Module R A] [Module R B] [Module R C] instance : SetLike (NonUnitalSubalgebra R A) A where coe s := s.carrier coe_injective' p q h := by cases p; cases q; congr; exact SetLike.coe_injective h instance instNonUnitalSubsemiringClass : NonUnitalSubsemiringClass (NonUnitalSubalgebra R A) A where add_mem {s} := s.add_mem' mul_mem {s} := s.mul_mem' zero_mem {s} := s.zero_mem' instance instSMulMemClass : SMulMemClass (NonUnitalSubalgebra R A) R A where smul_mem := @fun s => s.smul_mem' theorem mem_carrier {s : NonUnitalSubalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := Iff.rfl @[ext] theorem ext {S T : NonUnitalSubalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := SetLike.ext h @[simp] theorem mem_toNonUnitalSubsemiring {S : NonUnitalSubalgebra R A} {x} : x ∈ S.toNonUnitalSubsemiring ↔ x ∈ S := Iff.rfl @[simp] theorem coe_toNonUnitalSubsemiring (S : NonUnitalSubalgebra R A) : (↑S.toNonUnitalSubsemiring : Set A) = S := rfl theorem toNonUnitalSubsemiring_injective : Function.Injective (toNonUnitalSubsemiring : NonUnitalSubalgebra R A → NonUnitalSubsemiring A) := fun S T h => ext fun x => by rw [← mem_toNonUnitalSubsemiring, ← mem_toNonUnitalSubsemiring, h] theorem toNonUnitalSubsemiring_inj {S U : NonUnitalSubalgebra R A} : S.toNonUnitalSubsemiring = U.toNonUnitalSubsemiring ↔ S = U := toNonUnitalSubsemiring_injective.eq_iff theorem mem_toSubmodule (S : NonUnitalSubalgebra R A) {x} : x ∈ S.toSubmodule ↔ x ∈ S := Iff.rfl @[simp] theorem coe_toSubmodule (S : NonUnitalSubalgebra R A) : (↑S.toSubmodule : Set A) = S := rfl theorem toSubmodule_injective : Function.Injective (toSubmodule : NonUnitalSubalgebra R A → Submodule R A) := fun S T h => ext fun x => by rw [← mem_toSubmodule, ← mem_toSubmodule, h] theorem toSubmodule_inj {S U : NonUnitalSubalgebra R A} : S.toSubmodule = U.toSubmodule ↔ S = U := toSubmodule_injective.eq_iff /-- Copy of a non-unital subalgebra with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : NonUnitalSubalgebra R A) (s : Set A) (hs : s = ↑S) : NonUnitalSubalgebra R A := { S.toNonUnitalSubsemiring.copy s hs with smul_mem' := fun r a (ha : a ∈ s) => by show r • a ∈ s rw [hs] at ha ⊢ exact S.smul_mem' r ha } @[simp] theorem coe_copy (S : NonUnitalSubalgebra R A) (s : Set A) (hs : s = ↑S) : (S.copy s hs : Set A) = s := rfl theorem copy_eq (S : NonUnitalSubalgebra R A) (s : Set A) (hs : s = ↑S) : S.copy s hs = S := SetLike.coe_injective hs instance (S : NonUnitalSubalgebra R A) : Inhabited S := ⟨(0 : S.toNonUnitalSubsemiring)⟩ end NonUnitalNonAssocSemiring section NonUnitalNonAssocRing variable [CommRing R] variable [NonUnitalNonAssocRing A] [NonUnitalNonAssocRing B] [NonUnitalNonAssocRing C] variable [Module R A] [Module R B] [Module R C] instance instNonUnitalSubringClass : NonUnitalSubringClass (NonUnitalSubalgebra R A) A := { NonUnitalSubalgebra.instNonUnitalSubsemiringClass with neg_mem := @fun _ x hx => neg_one_smul R x ▸ SMulMemClass.smul_mem _ hx } /-- A non-unital subalgebra over a ring is also a `Subring`. -/ def toNonUnitalSubring (S : NonUnitalSubalgebra R A) : NonUnitalSubring A where toNonUnitalSubsemiring := S.toNonUnitalSubsemiring neg_mem' := neg_mem (s := S) @[simp] theorem mem_toNonUnitalSubring {S : NonUnitalSubalgebra R A} {x} : x ∈ S.toNonUnitalSubring ↔ x ∈ S := Iff.rfl @[simp] theorem coe_toNonUnitalSubring (S : NonUnitalSubalgebra R A) : (↑S.toNonUnitalSubring : Set A) = S := rfl theorem toNonUnitalSubring_injective : Function.Injective (toNonUnitalSubring : NonUnitalSubalgebra R A → NonUnitalSubring A) := fun S T h => ext fun x => by rw [← mem_toNonUnitalSubring, ← mem_toNonUnitalSubring, h] theorem toNonUnitalSubring_inj {S U : NonUnitalSubalgebra R A} : S.toNonUnitalSubring = U.toNonUnitalSubring ↔ S = U := toNonUnitalSubring_injective.eq_iff end NonUnitalNonAssocRing section /-! `NonUnitalSubalgebra`s inherit structure from their `NonUnitalSubsemiring` / `Semiring` coercions. -/ instance toNonUnitalNonAssocSemiring [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalNonAssocSemiring S := inferInstance instance toNonUnitalSemiring [CommSemiring R] [NonUnitalSemiring A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalSemiring S := inferInstance instance toNonUnitalCommSemiring [CommSemiring R] [NonUnitalCommSemiring A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalCommSemiring S := inferInstance instance toNonUnitalNonAssocRing [CommRing R] [NonUnitalNonAssocRing A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalNonAssocRing S := inferInstance instance toNonUnitalRing [CommRing R] [NonUnitalRing A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalRing S := inferInstance instance toNonUnitalCommRing [CommRing R] [NonUnitalCommRing A] [Module R A] (S : NonUnitalSubalgebra R A) : NonUnitalCommRing S := inferInstance end /-- The forgetful map from `NonUnitalSubalgebra` to `Submodule` as an `OrderEmbedding` -/ def toSubmodule' [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] : NonUnitalSubalgebra R A ↪o Submodule R A where toEmbedding := { toFun := fun S => S.toSubmodule inj' := fun S T h => ext <| by apply SetLike.ext_iff.1 h } map_rel_iff' := SetLike.coe_subset_coe.symm.trans SetLike.coe_subset_coe /-- The forgetful map from `NonUnitalSubalgebra` to `NonUnitalSubsemiring` as an `OrderEmbedding` -/ def toNonUnitalSubsemiring' [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] : NonUnitalSubalgebra R A ↪o NonUnitalSubsemiring A where toEmbedding := { toFun := fun S => S.toNonUnitalSubsemiring inj' := fun S T h => ext <| by apply SetLike.ext_iff.1 h } map_rel_iff' := SetLike.coe_subset_coe.symm.trans SetLike.coe_subset_coe /-- The forgetful map from `NonUnitalSubalgebra` to `NonUnitalSubsemiring` as an `OrderEmbedding` -/ def toNonUnitalSubring' [CommRing R] [NonUnitalNonAssocRing A] [Module R A] : NonUnitalSubalgebra R A ↪o NonUnitalSubring A where toEmbedding := { toFun := fun S => S.toNonUnitalSubring inj' := fun S T h => ext <| by apply SetLike.ext_iff.1 h } map_rel_iff' := SetLike.coe_subset_coe.symm.trans SetLike.coe_subset_coe variable [CommSemiring R] variable [NonUnitalNonAssocSemiring A] [NonUnitalNonAssocSemiring B] [NonUnitalNonAssocSemiring C] variable [Module R A] [Module R B] [Module R C] variable {S : NonUnitalSubalgebra R A} section /-! ### `NonUnitalSubalgebra`s inherit structure from their `Submodule` coercions. -/ instance instModule' [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] : Module R' S := SMulMemClass.toModule' _ R' R A S instance instModule : Module R S := S.instModule' instance instIsScalarTower' [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] : IsScalarTower R' R S := S.toSubmodule.isScalarTower instance [IsScalarTower R A A] : IsScalarTower R S S where smul_assoc r x y := Subtype.ext <| smul_assoc r (x : A) (y : A) instance instSMulCommClass' [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] [SMulCommClass R' R A] : SMulCommClass R' R S where smul_comm r' r s := Subtype.ext <| smul_comm r' r (s : A) instance instSMulCommClass [SMulCommClass R A A] : SMulCommClass R S S where smul_comm r x y := Subtype.ext <| smul_comm r (x : A) (y : A) instance noZeroSMulDivisors_bot [NoZeroSMulDivisors R A] : NoZeroSMulDivisors R S := ⟨fun {c x} h => have : c = 0 ∨ (x : A) = 0 := eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg ((↑) : S → A) h) this.imp_right (@Subtype.ext_iff _ _ x 0).mpr⟩ end protected theorem coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl protected theorem coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl protected theorem coe_zero : ((0 : S) : A) = 0 := rfl protected theorem coe_neg {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S : NonUnitalSubalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl protected theorem coe_sub {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S : NonUnitalSubalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl @[simp, norm_cast] theorem coe_smul [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] (r : R') (x : S) : ↑(r • x) = r • (x : A) := rfl protected theorem coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := ZeroMemClass.coe_eq_zero @[simp] theorem toNonUnitalSubsemiring_subtype : NonUnitalSubsemiringClass.subtype S = NonUnitalSubalgebraClass.subtype (R := R) S := rfl @[simp] theorem toSubring_subtype {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (S : NonUnitalSubalgebra R A) : NonUnitalSubringClass.subtype S = NonUnitalSubalgebraClass.subtype (R := R) S := rfl /-- Linear equivalence between `S : Submodule R A` and `S`. Though these types are equal, we define it as a `LinearEquiv` to avoid type equalities. -/ def toSubmoduleEquiv (S : NonUnitalSubalgebra R A) : S.toSubmodule ≃ₗ[R] S := LinearEquiv.ofEq _ _ rfl variable [FunLike F A B] [NonUnitalAlgHomClass F R A B] /-- Transport a non-unital subalgebra via an algebra homomorphism. -/ def map (f : F) (S : NonUnitalSubalgebra R A) : NonUnitalSubalgebra R B := { S.toNonUnitalSubsemiring.map (f : A →ₙ+* B) with smul_mem' := fun r b hb => by rcases hb with ⟨a, ha, rfl⟩ exact map_smulₛₗ f r a ▸ Set.mem_image_of_mem f (S.smul_mem' r ha) } theorem map_mono {S₁ S₂ : NonUnitalSubalgebra R A} {f : F} : S₁ ≤ S₂ → (map f S₁ : NonUnitalSubalgebra R B) ≤ map f S₂ := Set.image_subset f theorem map_injective {f : F} (hf : Function.Injective f) : Function.Injective (map f : NonUnitalSubalgebra R A → NonUnitalSubalgebra R B) := fun _S₁ _S₂ ih => ext <| Set.ext_iff.1 <| Set.image_injective.2 hf <| Set.ext <| SetLike.ext_iff.mp ih @[simp] theorem map_id (S : NonUnitalSubalgebra R A) : map (NonUnitalAlgHom.id R A) S = S := SetLike.coe_injective <| Set.image_id _ theorem map_map (S : NonUnitalSubalgebra R A) (g : B →ₙₐ[R] C) (f : A →ₙₐ[R] B) : (S.map f).map g = S.map (g.comp f) := SetLike.coe_injective <| Set.image_image _ _ _ @[simp] theorem mem_map {S : NonUnitalSubalgebra R A} {f : F} {y : B} : y ∈ map f S ↔ ∃ x ∈ S, f x = y := NonUnitalSubsemiring.mem_map theorem map_toSubmodule {S : NonUnitalSubalgebra R A} {f : F} : -- TODO: introduce a better coercion from `NonUnitalAlgHomClass` to `LinearMap` (map f S).toSubmodule = Submodule.map (LinearMapClass.linearMap f) S.toSubmodule := SetLike.coe_injective rfl theorem map_toNonUnitalSubsemiring {S : NonUnitalSubalgebra R A} {f : F} : (map f S).toNonUnitalSubsemiring = S.toNonUnitalSubsemiring.map (f : A →ₙ+* B) := SetLike.coe_injective rfl @[simp] theorem coe_map (S : NonUnitalSubalgebra R A) (f : F) : (map f S : Set B) = f '' S := rfl /-- Preimage of a non-unital subalgebra under an algebra homomorphism. -/ def comap (f : F) (S : NonUnitalSubalgebra R B) : NonUnitalSubalgebra R A := { S.toNonUnitalSubsemiring.comap (f : A →ₙ+* B) with smul_mem' := fun r a (ha : f a ∈ S) => show f (r • a) ∈ S from (map_smulₛₗ f r a).symm ▸ SMulMemClass.smul_mem r ha } theorem map_le {S : NonUnitalSubalgebra R A} {f : F} {U : NonUnitalSubalgebra R B} : map f S ≤ U ↔ S ≤ comap f U := Set.image_subset_iff theorem gc_map_comap (f : F) : GaloisConnection (map f : NonUnitalSubalgebra R A → NonUnitalSubalgebra R B) (comap f) := fun _ _ => map_le @[simp] theorem mem_comap (S : NonUnitalSubalgebra R B) (f : F) (x : A) : x ∈ comap f S ↔ f x ∈ S := Iff.rfl @[simp, norm_cast] theorem coe_comap (S : NonUnitalSubalgebra R B) (f : F) : (comap f S : Set A) = f ⁻¹' (S : Set B) := rfl instance noZeroDivisors {R A : Type*} [CommSemiring R] [NonUnitalSemiring A] [NoZeroDivisors A] [Module R A] (S : NonUnitalSubalgebra R A) : NoZeroDivisors S := NonUnitalSubsemiringClass.noZeroDivisors S end NonUnitalSubalgebra namespace Submodule variable {R A : Type*} [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] /-- A submodule closed under multiplication is a non-unital subalgebra. -/ def toNonUnitalSubalgebra (p : Submodule R A) (h_mul : ∀ x y, x ∈ p → y ∈ p → x * y ∈ p) : NonUnitalSubalgebra R A := { p with mul_mem' := h_mul _ _ } @[simp] theorem mem_toNonUnitalSubalgebra {p : Submodule R A} {h_mul} {x} : x ∈ p.toNonUnitalSubalgebra h_mul ↔ x ∈ p := Iff.rfl @[simp] theorem coe_toNonUnitalSubalgebra (p : Submodule R A) (h_mul) : (p.toNonUnitalSubalgebra h_mul : Set A) = p := rfl theorem toNonUnitalSubalgebra_mk (p : Submodule R A) hmul : p.toNonUnitalSubalgebra hmul = NonUnitalSubalgebra.mk ⟨⟨⟨p, p.add_mem⟩, p.zero_mem⟩, hmul _ _⟩ p.smul_mem' := rfl @[simp] theorem toNonUnitalSubalgebra_toSubmodule (p : Submodule R A) (h_mul) : (p.toNonUnitalSubalgebra h_mul).toSubmodule = p := SetLike.coe_injective rfl @[simp] theorem _root_.NonUnitalSubalgebra.toSubmodule_toNonUnitalSubalgebra (S : NonUnitalSubalgebra R A) : (S.toSubmodule.toNonUnitalSubalgebra fun _ _ => mul_mem (s := S)) = S := SetLike.coe_injective rfl end Submodule namespace NonUnitalAlgHom variable {F : Type v'} {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} variable [CommSemiring R] variable [NonUnitalNonAssocSemiring A] [Module R A] [NonUnitalNonAssocSemiring B] [Module R B] variable [NonUnitalNonAssocSemiring C] [Module R C] [FunLike F A B] [NonUnitalAlgHomClass F R A B] /-- Range of an `NonUnitalAlgHom` as a non-unital subalgebra. -/ protected def range (φ : F) : NonUnitalSubalgebra R B where toNonUnitalSubsemiring := NonUnitalRingHom.srange (φ : A →ₙ+* B) smul_mem' := fun r a => by rintro ⟨a, rfl⟩; exact ⟨r • a, map_smul φ r a⟩ @[simp] theorem mem_range (φ : F) {y : B} : y ∈ (NonUnitalAlgHom.range φ : NonUnitalSubalgebra R B) ↔ ∃ x : A, φ x = y := NonUnitalRingHom.mem_srange theorem mem_range_self (φ : F) (x : A) : φ x ∈ (NonUnitalAlgHom.range φ : NonUnitalSubalgebra R B) := (NonUnitalAlgHom.mem_range φ).2 ⟨x, rfl⟩ @[simp] theorem coe_range (φ : F) : ((NonUnitalAlgHom.range φ : NonUnitalSubalgebra R B) : Set B) = Set.range (φ : A → B) := by ext rw [SetLike.mem_coe, mem_range] rfl theorem range_comp (f : A →ₙₐ[R] B) (g : B →ₙₐ[R] C) : NonUnitalAlgHom.range (g.comp f) = (NonUnitalAlgHom.range f).map g := SetLike.coe_injective (Set.range_comp g f) theorem range_comp_le_range (f : A →ₙₐ[R] B) (g : B →ₙₐ[R] C) : NonUnitalAlgHom.range (g.comp f) ≤ NonUnitalAlgHom.range g := SetLike.coe_mono (Set.range_comp_subset_range f g) /-- Restrict the codomain of a non-unital algebra homomorphism. -/ def codRestrict (f : F) (S : NonUnitalSubalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₙₐ[R] S := { NonUnitalRingHom.codRestrict (f : A →ₙ+* B) S.toNonUnitalSubsemiring hf with map_smul' := fun r a => Subtype.ext <| map_smul f r a } @[simp] theorem subtype_comp_codRestrict (f : F) (S : NonUnitalSubalgebra R B) (hf : ∀ x : A, f x ∈ S) : (NonUnitalSubalgebraClass.subtype S).comp (NonUnitalAlgHom.codRestrict f S hf) = f := NonUnitalAlgHom.ext fun _ => rfl @[simp] theorem coe_codRestrict (f : F) (S : NonUnitalSubalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) : ↑(NonUnitalAlgHom.codRestrict f S hf x) = f x := rfl theorem injective_codRestrict (f : F) (S : NonUnitalSubalgebra R B) (hf : ∀ x : A, f x ∈ S) : Function.Injective (NonUnitalAlgHom.codRestrict f S hf) ↔ Function.Injective f := ⟨fun H _x _y hxy => H <| Subtype.eq hxy, fun H _x _y hxy => H (congr_arg Subtype.val hxy : _)⟩ /-- Restrict the codomain of an `NonUnitalAlgHom` `f` to `f.range`. This is the bundled version of `Set.rangeFactorization`. -/ abbrev rangeRestrict (f : F) : A →ₙₐ[R] (NonUnitalAlgHom.range f : NonUnitalSubalgebra R B) := NonUnitalAlgHom.codRestrict f (NonUnitalAlgHom.range f) (NonUnitalAlgHom.mem_range_self f) /-- The equalizer of two non-unital `R`-algebra homomorphisms -/ def equalizer (ϕ ψ : F) : NonUnitalSubalgebra R A where carrier := {a | (ϕ a : B) = ψ a} zero_mem' := by rw [Set.mem_setOf_eq, map_zero, map_zero] add_mem' {x y} (hx : ϕ x = ψ x) (hy : ϕ y = ψ y) := by rw [Set.mem_setOf_eq, map_add, map_add, hx, hy] mul_mem' {x y} (hx : ϕ x = ψ x) (hy : ϕ y = ψ y) := by rw [Set.mem_setOf_eq, map_mul, map_mul, hx, hy] smul_mem' r x (hx : ϕ x = ψ x) := by rw [Set.mem_setOf_eq, map_smul, map_smul, hx] @[simp] theorem mem_equalizer (φ ψ : F) (x : A) : x ∈ NonUnitalAlgHom.equalizer φ ψ ↔ φ x = ψ x := Iff.rfl /-- The range of a morphism of algebras is a fintype, if the domain is a fintype. Note that this instance can cause a diamond with `Subtype.fintype` if `B` is also a fintype. -/ instance fintypeRange [Fintype A] [DecidableEq B] (φ : F) : Fintype (NonUnitalAlgHom.range φ) := Set.fintypeRange φ end NonUnitalAlgHom namespace NonUnitalAlgebra variable {F : Type*} (R : Type u) {A : Type v} {B : Type w} variable [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] @[simp] lemma span_eq_toSubmodule (s : NonUnitalSubalgebra R A) : Submodule.span R (s : Set A) = s.toSubmodule := by simp [SetLike.ext'_iff, Submodule.coe_span_eq_self] variable [NonUnitalNonAssocSemiring B] [Module R B] variable [FunLike F A B] [NonUnitalAlgHomClass F R A B] section IsScalarTower variable [IsScalarTower R A A] [SMulCommClass R A A] /-- The minimal non-unital subalgebra that includes `s`. -/ def adjoin (s : Set A) : NonUnitalSubalgebra R A := { Submodule.span R (NonUnitalSubsemiring.closure s : Set A) with mul_mem' := @fun a b (ha : a ∈ Submodule.span R (NonUnitalSubsemiring.closure s : Set A)) (hb : b ∈ Submodule.span R (NonUnitalSubsemiring.closure s : Set A)) => show a * b ∈ Submodule.span R (NonUnitalSubsemiring.closure s : Set A) by refine Submodule.span_induction ha ?_ ?_ ?_ ?_ · refine Submodule.span_induction hb ?_ ?_ ?_ ?_ · exact fun x (hx : x ∈ NonUnitalSubsemiring.closure s) y (hy : y ∈ NonUnitalSubsemiring.closure s) => Submodule.subset_span (mul_mem hy hx) · exact fun x _hx => (mul_zero x).symm ▸ Submodule.zero_mem _ · exact fun x y hx hy z hz => (mul_add z x y).symm ▸ add_mem (hx z hz) (hy z hz) · exact fun r x hx y hy => (mul_smul_comm r y x).symm ▸ SMulMemClass.smul_mem r (hx y hy) · exact (zero_mul b).symm ▸ Submodule.zero_mem _ · exact fun x y => (add_mul x y b).symm ▸ add_mem · exact fun r x hx => (smul_mul_assoc r x b).symm ▸ SMulMemClass.smul_mem r hx } theorem adjoin_toSubmodule (s : Set A) : (adjoin R s).toSubmodule = Submodule.span R (NonUnitalSubsemiring.closure s : Set A) := rfl @[aesop safe 20 apply (rule_sets := [SetLike])] theorem subset_adjoin {s : Set A} : s ⊆ adjoin R s := NonUnitalSubsemiring.subset_closure.trans Submodule.subset_span theorem self_mem_adjoin_singleton (x : A) : x ∈ adjoin R ({x} : Set A) := NonUnitalAlgebra.subset_adjoin R (Set.mem_singleton x) variable {R} /-- If some predicate holds for all `x ∈ (s : Set A)` and this predicate is closed under the `algebraMap`, addition, multiplication and star operations, then it holds for `a ∈ adjoin R s`. -/ @[elab_as_elim] theorem adjoin_induction {s : Set A} {p : A → Prop} {a : A} (h : a ∈ adjoin R s) (mem : ∀ x ∈ s, p x) (add : ∀ x y, p x → p y → p (x + y)) (zero : p 0) (mul : ∀ x y, p x → p y → p (x * y)) (smul : ∀ (r : R) x, p x → p (r • x)) : p a := Submodule.span_induction h (fun _a ha => NonUnitalSubsemiring.closure_induction ha mem zero add mul) zero add smul @[elab_as_elim] theorem adjoin_induction₂ {s : Set A} {p : A → A → Prop} {a b : A} (ha : a ∈ adjoin R s) (hb : b ∈ adjoin R s) (Hs : ∀ x ∈ s, ∀ y ∈ s, p x y) (H0_left : ∀ y, p 0 y) (H0_right : ∀ x, p x 0) (Hadd_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ + x₂) y) (Hadd_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ + y₂)) (Hmul_left : ∀ x₁ x₂ y, p x₁ y → p x₂ y → p (x₁ * x₂) y) (Hmul_right : ∀ x y₁ y₂, p x y₁ → p x y₂ → p x (y₁ * y₂)) (Hsmul_left : ∀ (r : R) x y, p x y → p (r • x) y) (Hsmul_right : ∀ (r : R) x y, p x y → p x (r • y)) : p a b := Submodule.span_induction₂ ha hb (fun _x hx _y hy => NonUnitalSubsemiring.closure_induction₂ hx hy Hs H0_left H0_right Hadd_left Hadd_right Hmul_left Hmul_right) H0_left H0_right Hadd_left Hadd_right Hsmul_left Hsmul_right /-- The difference with `NonUnitalAlgebra.adjoin_induction` is that this acts on the subtype. -/ @[elab_as_elim] lemma adjoin_induction_subtype {s : Set A} {p : adjoin R s → Prop} (a : adjoin R s) (mem : ∀ x (h : x ∈ s), p ⟨x, subset_adjoin R h⟩) (add : ∀ x y, p x → p y → p (x + y)) (zero : p 0) (mul : ∀ x y, p x → p y → p (x * y)) (smul : ∀ (r : R) x, p x → p (r • x)) : p a := Subtype.recOn a fun b hb => by refine Exists.elim ?_ (fun (hb : b ∈ adjoin R s) (hc : p ⟨b, hb⟩) => hc) refine adjoin_induction hb ?_ ?_ ?_ ?_ ?_ · exact fun x hx => ⟨subset_adjoin R hx, mem x hx⟩ · exact fun x y hx hy => Exists.elim hx fun hx' hx => Exists.elim hy fun hy' hy => ⟨add_mem hx' hy', add _ _ hx hy⟩ · exact ⟨_, zero⟩ · exact fun x y hx hy => Exists.elim hx fun hx' hx => Exists.elim hy fun hy' hy => ⟨mul_mem hx' hy', mul _ _ hx hy⟩ · exact fun r x hx => Exists.elim hx fun hx' hx => ⟨SMulMemClass.smul_mem r hx', smul r _ hx⟩ /-- A dependent version of `NonUnitalAlgebra.adjoin_induction`. -/ theorem adjoin_induction' {s : Set A} {p : ∀ x, x ∈ adjoin R s → Prop} (mem : ∀ (x) (h : x ∈ s), p x (subset_adjoin R h)) (add : ∀ x hx y hy, p x hx → p y hy → p (x + y) (add_mem ‹_› ‹_›)) (zero : p 0 (zero_mem _)) (mul : ∀ x hx y hy, p x hx → p y hy → p (x * y) (mul_mem ‹_› ‹_›)) (smul : ∀ (r : R) (x hx), p x hx → p (r • x) (SMulMemClass.smul_mem _ ‹_›)) {a} (ha : a ∈ adjoin R s) : p a ha := adjoin_induction_subtype ⟨a, ha⟩ (p := fun x ↦ p x.1 x.2) mem (fun x y ↦ add x.1 x.2 y.1 y.2) zero (fun x y ↦ mul x.1 x.2 y.1 y.2) (fun r x ↦ smul r x.1 x.2) protected theorem gc : GaloisConnection (adjoin R : Set A → NonUnitalSubalgebra R A) (↑) := fun s S => ⟨fun H => (NonUnitalSubsemiring.subset_closure.trans Submodule.subset_span).trans H, fun H => show Submodule.span R _ ≤ S.toSubmodule from Submodule.span_le.mpr <| show NonUnitalSubsemiring.closure s ≤ S.toNonUnitalSubsemiring from NonUnitalSubsemiring.closure_le.2 H⟩ /-- Galois insertion between `adjoin` and `Subtype.val`. -/ protected def gi : GaloisInsertion (adjoin R : Set A → NonUnitalSubalgebra R A) (↑) where choice s hs := (adjoin R s).copy s <| le_antisymm (NonUnitalAlgebra.gc.le_u_l s) hs gc := NonUnitalAlgebra.gc le_l_u S := (NonUnitalAlgebra.gc (S : Set A) (adjoin R S)).1 <| le_rfl choice_eq _ _ := NonUnitalSubalgebra.copy_eq _ _ _ instance : CompleteLattice (NonUnitalSubalgebra R A) := GaloisInsertion.liftCompleteLattice NonUnitalAlgebra.gi theorem adjoin_le {S : NonUnitalSubalgebra R A} {s : Set A} (hs : s ⊆ S) : adjoin R s ≤ S := NonUnitalAlgebra.gc.l_le hs theorem adjoin_le_iff {S : NonUnitalSubalgebra R A} {s : Set A} : adjoin R s ≤ S ↔ s ⊆ S := NonUnitalAlgebra.gc _ _ theorem adjoin_union (s t : Set A) : adjoin R (s ∪ t) = adjoin R s ⊔ adjoin R t := (NonUnitalAlgebra.gc : GaloisConnection _ ((↑) : NonUnitalSubalgebra R A → Set A)).l_sup lemma adjoin_eq (s : NonUnitalSubalgebra R A) : adjoin R (s : Set A) = s := le_antisymm (adjoin_le le_rfl) (subset_adjoin R) open Submodule in lemma adjoin_eq_span (s : Set A) : (adjoin R s).toSubmodule = span R (Subsemigroup.closure s) := by apply le_antisymm · intro x hx induction hx using adjoin_induction' with | mem x hx => exact subset_span <| Subsemigroup.subset_closure hx | add x _ y _ hpx hpy => exact add_mem hpx hpy | zero => exact zero_mem _ | mul x _ y _ hpx hpy => apply span_induction₂ hpx hpy ?Hs (by simp) (by simp) ?Hadd_l ?Hadd_r ?Hsmul_l ?Hsmul_r case Hs => exact fun x hx y hy ↦ subset_span <| mul_mem hx hy case Hadd_l => exact fun x y z hxz hyz ↦ by simpa [add_mul] using add_mem hxz hyz case Hadd_r => exact fun x y z hxz hyz ↦ by simpa [mul_add] using add_mem hxz hyz case Hsmul_l => exact fun r x y hxy ↦ by simpa [smul_mul_assoc] using smul_mem _ _ hxy case Hsmul_r => exact fun r x y hxy ↦ by simpa [mul_smul_comm] using smul_mem _ _ hxy | smul r x _ hpx => exact smul_mem _ _ hpx · apply span_le.2 _ show Subsemigroup.closure s ≤ (adjoin R s).toSubsemigroup exact Subsemigroup.closure_le.2 (subset_adjoin R) variable (R A) @[simp] theorem adjoin_empty : adjoin R (∅ : Set A) = ⊥ := show adjoin R ⊥ = ⊥ by apply GaloisConnection.l_bot; exact NonUnitalAlgebra.gc @[simp] theorem adjoin_univ : adjoin R (Set.univ : Set A) = ⊤ := eq_top_iff.2 fun _x hx => subset_adjoin R hx open NonUnitalSubalgebra in lemma _root_.NonUnitalAlgHom.map_adjoin [IsScalarTower R B B] [SMulCommClass R B B] (f : F) (s : Set A) : map f (adjoin R s) = adjoin R (f '' s) := Set.image_preimage.l_comm_of_u_comm (gc_map_comap f) NonUnitalAlgebra.gi.gc NonUnitalAlgebra.gi.gc fun _t => rfl open NonUnitalSubalgebra in @[simp] lemma _root_.NonUnitalAlgHom.map_adjoin_singleton [IsScalarTower R B B] [SMulCommClass R B B] (f : F) (x : A) : map f (adjoin R {x}) = adjoin R {f x} := by simp [NonUnitalAlgHom.map_adjoin] variable {R A} @[simp] theorem coe_top : (↑(⊤ : NonUnitalSubalgebra R A) : Set A) = Set.univ := rfl @[simp] theorem mem_top {x : A} : x ∈ (⊤ : NonUnitalSubalgebra R A) := Set.mem_univ x @[simp] theorem top_toSubmodule : (⊤ : NonUnitalSubalgebra R A).toSubmodule = ⊤ := rfl @[simp] theorem top_toNonUnitalSubsemiring : (⊤ : NonUnitalSubalgebra R A).toNonUnitalSubsemiring = ⊤ := rfl @[simp] theorem top_toSubring {R A : Type*} [CommRing R] [NonUnitalNonAssocRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : (⊤ : NonUnitalSubalgebra R A).toNonUnitalSubring = ⊤ := rfl @[simp] theorem toSubmodule_eq_top {S : NonUnitalSubalgebra R A} : S.toSubmodule = ⊤ ↔ S = ⊤ := NonUnitalSubalgebra.toSubmodule'.injective.eq_iff' top_toSubmodule @[simp] theorem toNonUnitalSubsemiring_eq_top {S : NonUnitalSubalgebra R A} : S.toNonUnitalSubsemiring = ⊤ ↔ S = ⊤ := NonUnitalSubalgebra.toNonUnitalSubsemiring_injective.eq_iff' top_toNonUnitalSubsemiring @[simp] theorem to_subring_eq_top {R A : Type*} [CommRing R] [Ring A] [Algebra R A] {S : NonUnitalSubalgebra R A} : S.toNonUnitalSubring = ⊤ ↔ S = ⊤ := NonUnitalSubalgebra.toNonUnitalSubring_injective.eq_iff' top_toSubring theorem mem_sup_left {S T : NonUnitalSubalgebra R A} : ∀ {x : A}, x ∈ S → x ∈ S ⊔ T := by rw [← SetLike.le_def] exact le_sup_left theorem mem_sup_right {S T : NonUnitalSubalgebra R A} : ∀ {x : A}, x ∈ T → x ∈ S ⊔ T := by rw [← SetLike.le_def] exact le_sup_right theorem mul_mem_sup {S T : NonUnitalSubalgebra R A} {x y : A} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := mul_mem (mem_sup_left hx) (mem_sup_right hy) theorem map_sup [IsScalarTower R B B] [SMulCommClass R B B] (f : F) (S T : NonUnitalSubalgebra R A) : ((S ⊔ T).map f : NonUnitalSubalgebra R B) = S.map f ⊔ T.map f := (NonUnitalSubalgebra.gc_map_comap f).l_sup @[simp, norm_cast] theorem coe_inf (S T : NonUnitalSubalgebra R A) : (↑(S ⊓ T) : Set A) = (S : Set A) ∩ T := rfl @[simp] theorem mem_inf {S T : NonUnitalSubalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := Iff.rfl @[simp] theorem inf_toSubmodule (S T : NonUnitalSubalgebra R A) : (S ⊓ T).toSubmodule = S.toSubmodule ⊓ T.toSubmodule := rfl @[simp] theorem inf_toNonUnitalSubsemiring (S T : NonUnitalSubalgebra R A) : (S ⊓ T).toNonUnitalSubsemiring = S.toNonUnitalSubsemiring ⊓ T.toNonUnitalSubsemiring := rfl @[simp, norm_cast] theorem coe_sInf (S : Set (NonUnitalSubalgebra R A)) : (↑(sInf S) : Set A) = ⋂ s ∈ S, ↑s := sInf_image theorem mem_sInf {S : Set (NonUnitalSubalgebra R A)} {x : A} : x ∈ sInf S ↔ ∀ p ∈ S, x ∈ p := by simp only [← SetLike.mem_coe, coe_sInf, Set.mem_iInter₂] @[simp] theorem sInf_toSubmodule (S : Set (NonUnitalSubalgebra R A)) : (sInf S).toSubmodule = sInf (NonUnitalSubalgebra.toSubmodule '' S) := SetLike.coe_injective <| by simp @[simp] theorem sInf_toNonUnitalSubsemiring (S : Set (NonUnitalSubalgebra R A)) : (sInf S).toNonUnitalSubsemiring = sInf (NonUnitalSubalgebra.toNonUnitalSubsemiring '' S) := SetLike.coe_injective <| by simp @[simp, norm_cast] theorem coe_iInf {ι : Sort*} {S : ι → NonUnitalSubalgebra R A} : (↑(⨅ i, S i) : Set A) = ⋂ i, S i := by simp [iInf] theorem mem_iInf {ι : Sort*} {S : ι → NonUnitalSubalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [iInf, mem_sInf, Set.forall_mem_range] @[simp] theorem iInf_toSubmodule {ι : Sort*} (S : ι → NonUnitalSubalgebra R A) : (⨅ i, S i).toSubmodule = ⨅ i, (S i).toSubmodule := SetLike.coe_injective <| by simp instance : Inhabited (NonUnitalSubalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : NonUnitalSubalgebra R A) ↔ x = 0 := show x ∈ Submodule.span R (NonUnitalSubsemiring.closure (∅ : Set A) : Set A) ↔ x = 0 by rw [NonUnitalSubsemiring.closure_empty, NonUnitalSubsemiring.coe_bot, Submodule.span_zero_singleton, Submodule.mem_bot] theorem toSubmodule_bot : (⊥ : NonUnitalSubalgebra R A).toSubmodule = ⊥ := by ext simp only [mem_bot, NonUnitalSubalgebra.mem_toSubmodule, Submodule.mem_bot] @[simp] theorem coe_bot : ((⊥ : NonUnitalSubalgebra R A) : Set A) = {0} := by simp [Set.ext_iff, NonUnitalAlgebra.mem_bot] theorem eq_top_iff {S : NonUnitalSubalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨fun h x => by rw [h]; exact mem_top, fun h => by ext x; exact ⟨fun _ => mem_top, fun _ => h x⟩⟩ @[simp] theorem range_id : NonUnitalAlgHom.range (NonUnitalAlgHom.id R A) = ⊤ := SetLike.coe_injective Set.range_id @[simp] theorem map_top (f : A →ₙₐ[R] B) : (⊤ : NonUnitalSubalgebra R A).map f = NonUnitalAlgHom.range f := SetLike.coe_injective Set.image_univ @[simp] theorem map_bot [IsScalarTower R B B] [SMulCommClass R B B] (f : A →ₙₐ[R] B) : (⊥ : NonUnitalSubalgebra R A).map f = ⊥ := SetLike.coe_injective <| by simp [NonUnitalAlgebra.coe_bot, NonUnitalSubalgebra.coe_map] @[simp] theorem comap_top [IsScalarTower R B B] [SMulCommClass R B B] (f : A →ₙₐ[R] B) : (⊤ : NonUnitalSubalgebra R B).comap f = ⊤ := eq_top_iff.2 fun _ => mem_top /-- `NonUnitalAlgHom` to `⊤ : NonUnitalSubalgebra R A`. -/ def toTop : A →ₙₐ[R] (⊤ : NonUnitalSubalgebra R A) := NonUnitalAlgHom.codRestrict (NonUnitalAlgHom.id R A) ⊤ fun _ => mem_top end IsScalarTower theorem range_top_iff_surjective [IsScalarTower R B B] [SMulCommClass R B B] (f : A →ₙₐ[R] B) : NonUnitalAlgHom.range f = (⊤ : NonUnitalSubalgebra R B) ↔ Function.Surjective f := NonUnitalAlgebra.eq_top_iff end NonUnitalAlgebra namespace NonUnitalSubalgebra open NonUnitalAlgebra section NonAssoc variable {R : Type u} {A : Type v} {B : Type w} variable [CommSemiring R] variable [NonUnitalNonAssocSemiring A] [Module R A] variable (S : NonUnitalSubalgebra R A) theorem range_val : NonUnitalAlgHom.range (NonUnitalSubalgebraClass.subtype S) = S := ext <| Set.ext_iff.1 <| (NonUnitalSubalgebraClass.subtype S).coe_range.trans Subtype.range_val instance subsingleton_of_subsingleton [Subsingleton A] : Subsingleton (NonUnitalSubalgebra R A) := ⟨fun B C => ext fun x => by simp only [Subsingleton.elim x 0, zero_mem B, zero_mem C]⟩ variable [NonUnitalNonAssocSemiring B] [Module R B] section Prod variable (S₁ : NonUnitalSubalgebra R B) /-- The product of two non-unital subalgebras is a non-unital subalgebra. -/ def prod : NonUnitalSubalgebra R (A × B) := { S.toNonUnitalSubsemiring.prod S₁.toNonUnitalSubsemiring with carrier := S ×ˢ S₁ smul_mem' := fun r _x hx => ⟨SMulMemClass.smul_mem r hx.1, SMulMemClass.smul_mem r hx.2⟩ } @[simp] theorem coe_prod : (prod S S₁ : Set (A × B)) = (S : Set A) ×ˢ S₁ := rfl theorem prod_toSubmodule : (S.prod S₁).toSubmodule = S.toSubmodule.prod S₁.toSubmodule := rfl @[simp] theorem mem_prod {S : NonUnitalSubalgebra R A} {S₁ : NonUnitalSubalgebra R B} {x : A × B} : x ∈ prod S S₁ ↔ x.1 ∈ S ∧ x.2 ∈ S₁ := Set.mem_prod variable [IsScalarTower R A A] [SMulCommClass R A A] [IsScalarTower R B B] [SMulCommClass R B B] @[simp] theorem prod_top : (prod ⊤ ⊤ : NonUnitalSubalgebra R (A × B)) = ⊤ := by ext; simp theorem prod_mono {S T : NonUnitalSubalgebra R A} {S₁ T₁ : NonUnitalSubalgebra R B} : S ≤ T → S₁ ≤ T₁ → prod S S₁ ≤ prod T T₁ := Set.prod_mono @[simp] theorem prod_inf_prod {S T : NonUnitalSubalgebra R A} {S₁ T₁ : NonUnitalSubalgebra R B} : S.prod S₁ ⊓ T.prod T₁ = (S ⊓ T).prod (S₁ ⊓ T₁) := SetLike.coe_injective Set.prod_inter_prod end Prod variable [IsScalarTower R A A] [SMulCommClass R A A] instance _root_.NonUnitalAlgHom.subsingleton [Subsingleton (NonUnitalSubalgebra R A)] : Subsingleton (A →ₙₐ[R] B) := ⟨fun f g => NonUnitalAlgHom.ext fun a => have : a ∈ (⊥ : NonUnitalSubalgebra R A) := Subsingleton.elim (⊤ : NonUnitalSubalgebra R A) ⊥ ▸ mem_top (mem_bot.mp this).symm ▸ (map_zero f).trans (map_zero g).symm⟩ /-- The map `S → T` when `S` is a non-unital subalgebra contained in the non-unital subalgebra `T`. This is the non-unital subalgebra version of `Submodule.inclusion`, or `Subring.inclusion` -/ def inclusion {S T : NonUnitalSubalgebra R A} (h : S ≤ T) : S →ₙₐ[R] T where toFun := Set.inclusion h map_add' _ _ := rfl map_mul' _ _ := rfl map_zero' := rfl map_smul' _ _ := rfl theorem inclusion_injective {S T : NonUnitalSubalgebra R A} (h : S ≤ T) : Function.Injective (inclusion h) := fun _ _ => Subtype.ext ∘ Subtype.mk.inj @[simp] theorem inclusion_self {S : NonUnitalSubalgebra R A} : inclusion (le_refl S) = NonUnitalAlgHom.id R S := NonUnitalAlgHom.ext fun _ => Subtype.ext rfl @[simp] theorem inclusion_mk {S T : NonUnitalSubalgebra R A} (h : S ≤ T) (x : A) (hx : x ∈ S) : inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl theorem inclusion_right {S T : NonUnitalSubalgebra R A} (h : S ≤ T) (x : T) (m : (x : A) ∈ S) : inclusion h ⟨x, m⟩ = x := Subtype.ext rfl @[simp] theorem inclusion_inclusion {S T U : NonUnitalSubalgebra R A} (hst : S ≤ T) (htu : T ≤ U) (x : S) : inclusion htu (inclusion hst x) = inclusion (le_trans hst htu) x := Subtype.ext rfl @[simp] theorem coe_inclusion {S T : NonUnitalSubalgebra R A} (h : S ≤ T) (s : S) : (inclusion h s : A) = s := rfl section SuprLift variable {ι : Type*} theorem coe_iSup_of_directed [Nonempty ι] {S : ι → NonUnitalSubalgebra R A} (dir : Directed (· ≤ ·) S) : ↑(iSup S) = ⋃ i, (S i : Set A) := let K : NonUnitalSubalgebra R A := { __ := NonUnitalSubsemiring.copy _ _ (NonUnitalSubsemiring.coe_iSup_of_directed dir).symm smul_mem' := fun r _x hx ↦ let ⟨i, hi⟩ := Set.mem_iUnion.1 hx Set.mem_iUnion.2 ⟨i, (S i).smul_mem' r hi⟩ } have : iSup S = K := le_antisymm (iSup_le fun i ↦ le_iSup (fun i ↦ (S i : Set A)) i) (Set.iUnion_subset fun _ ↦ le_iSup S _) this.symm ▸ rfl /-- Define an algebra homomorphism on a directed supremum of non-unital subalgebras by defining it on each non-unital subalgebra, and proving that it agrees on the intersection of non-unital subalgebras. -/ noncomputable def iSupLift [Nonempty ι] (K : ι → NonUnitalSubalgebra R A) (dir : Directed (· ≤ ·) K) (f : ∀ i, K i →ₙₐ[R] B) (hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)) (T : NonUnitalSubalgebra R A) (hT : T = iSup K) : ↥T →ₙₐ[R] B := by subst hT exact { toFun := Set.iUnionLift (fun i => ↑(K i)) (fun i x => f i x) (fun i j x hxi hxj => by let ⟨k, hik, hjk⟩ := dir i j simp only rw [hf i k hik, hf j k hjk] rfl) (↑(iSup K)) (by rw [coe_iSup_of_directed dir]) map_zero' := by dsimp exact Set.iUnionLift_const _ (fun i : ι => (0 : K i)) (fun _ => rfl) _ (by simp) map_mul' := by dsimp apply Set.iUnionLift_binary (coe_iSup_of_directed dir) dir _ (fun _ => (· * ·)) all_goals simp map_add' := by dsimp apply Set.iUnionLift_binary (coe_iSup_of_directed dir) dir _ (fun _ => (· + ·)) all_goals simp map_smul' := fun r => by dsimp apply Set.iUnionLift_unary (coe_iSup_of_directed dir) _ (fun _ x => r • x) (fun _ _ => rfl) all_goals simp } variable [Nonempty ι] {K : ι → NonUnitalSubalgebra R A} {dir : Directed (· ≤ ·) K} {f : ∀ i, K i →ₙₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : NonUnitalSubalgebra R A} {hT : T = iSup K} @[simp] theorem iSupLift_inclusion {i : ι} (x : K i) (h : K i ≤ T) : iSupLift K dir f hf T hT (inclusion h x) = f i x := by subst T dsimp [iSupLift] apply Set.iUnionLift_inclusion exact h @[simp] theorem iSupLift_comp_inclusion {i : ι} (h : K i ≤ T) : (iSupLift K dir f hf T hT).comp (inclusion h) = f i := by ext simp only [NonUnitalAlgHom.comp_apply, iSupLift_inclusion] @[simp] theorem iSupLift_mk {i : ι} (x : K i) (hx : (x : A) ∈ T) : iSupLift K dir f hf T hT ⟨x, hx⟩ = f i x := by subst hT dsimp [iSupLift] apply Set.iUnionLift_mk theorem iSupLift_of_mem {i : ι} (x : T) (hx : (x : A) ∈ K i) : iSupLift K dir f hf T hT x = f i ⟨x, hx⟩ := by subst hT dsimp [iSupLift] apply Set.iUnionLift_of_mem end SuprLift end NonAssoc section Center section NonUnitalNonAssocSemiring variable {R A : Type*} variable [CommSemiring R] [NonUnitalNonAssocSemiring A] [Module R A] variable [IsScalarTower R A A] [SMulCommClass R A A] theorem _root_.Set.smul_mem_center (r : R) {a : A} (ha : a ∈ Set.center A) : r • a ∈ Set.center A where comm b := by rw [mul_smul_comm, smul_mul_assoc, ha.comm] left_assoc b c := by rw [smul_mul_assoc, smul_mul_assoc, smul_mul_assoc, ha.left_assoc] mid_assoc b c := by rw [mul_smul_comm, smul_mul_assoc, smul_mul_assoc, mul_smul_comm, ha.mid_assoc] right_assoc b c := by rw [mul_smul_comm, mul_smul_comm, mul_smul_comm, ha.right_assoc] variable (R A) in /-- The center of a non-unital algebra is the set of elements which commute with every element. They form a non-unital subalgebra. -/ def center : NonUnitalSubalgebra R A := { NonUnitalSubsemiring.center A with smul_mem' := Set.smul_mem_center } theorem coe_center : (center R A : Set A) = Set.center A := rfl /-- The center of a non-unital algebra is commutative and associative -/ instance center.instNonUnitalCommSemiring : NonUnitalCommSemiring (center R A) := NonUnitalSubsemiring.center.instNonUnitalCommSemiring _ instance center.instNonUnitalCommRing {A : Type*} [NonUnitalNonAssocRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : NonUnitalCommRing (center R A) := NonUnitalSubring.center.instNonUnitalCommRing _ @[simp] theorem center_toNonUnitalSubsemiring : (center R A).toNonUnitalSubsemiring = NonUnitalSubsemiring.center A := rfl @[simp] lemma center_toNonUnitalSubring (R A : Type*) [CommRing R] [NonUnitalRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : (center R A).toNonUnitalSubring = NonUnitalSubring.center A := rfl end NonUnitalNonAssocSemiring variable (R A : Type*) [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] -- no instance diamond, as the `npow` field isn't present in the non-unital case. example : center.instNonUnitalCommSemiring.toNonUnitalSemiring = NonUnitalSubsemiringClass.toNonUnitalSemiring (center R A) := by with_reducible_and_instances rfl @[simp] theorem center_eq_top (A : Type*) [NonUnitalCommSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : center R A = ⊤ := SetLike.coe_injective (Set.center_eq_univ A) variable {R A} theorem mem_center_iff {a : A} : a ∈ center R A ↔ ∀ b : A, b * a = a * b := Subsemigroup.mem_center_iff end Center section Centralizer variable {R A : Type*} [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] @[simp] theorem _root_.Set.smul_mem_centralizer {s : Set A} (r : R) {a : A} (ha : a ∈ s.centralizer) : r • a ∈ s.centralizer := fun x hx => by rw [mul_smul_comm, smul_mul_assoc, ha x hx] variable (R) /-- The centralizer of a set as a non-unital subalgebra. -/ def centralizer (s : Set A) : NonUnitalSubalgebra R A where toNonUnitalSubsemiring := NonUnitalSubsemiring.centralizer s smul_mem' := Set.smul_mem_centralizer @[simp, norm_cast] theorem coe_centralizer (s : Set A) : (centralizer R s : Set A) = s.centralizer := rfl theorem mem_centralizer_iff {s : Set A} {z : A} : z ∈ centralizer R s ↔ ∀ g ∈ s, g * z = z * g := Iff.rfl theorem centralizer_le (s t : Set A) (h : s ⊆ t) : centralizer R t ≤ centralizer R s := Set.centralizer_subset h @[simp] theorem centralizer_univ : centralizer R Set.univ = center R A := SetLike.ext' (Set.centralizer_univ A) end Centralizer end NonUnitalSubalgebra section Nat variable {R : Type*} [NonUnitalNonAssocSemiring R] /-- A non-unital subsemiring is a non-unital `ℕ`-subalgebra. -/ def nonUnitalSubalgebraOfNonUnitalSubsemiring (S : NonUnitalSubsemiring R) : NonUnitalSubalgebra ℕ R where toNonUnitalSubsemiring := S smul_mem' n _x hx := nsmul_mem (S := S) hx n @[simp] theorem mem_nonUnitalSubalgebraOfNonUnitalSubsemiring {x : R} {S : NonUnitalSubsemiring R} : x ∈ nonUnitalSubalgebraOfNonUnitalSubsemiring S ↔ x ∈ S := Iff.rfl end Nat section Int variable {R : Type*} [NonUnitalNonAssocRing R] /-- A non-unital subring is a non-unital `ℤ`-subalgebra. -/ def nonUnitalSubalgebraOfNonUnitalSubring (S : NonUnitalSubring R) : NonUnitalSubalgebra ℤ R where toNonUnitalSubsemiring := S.toNonUnitalSubsemiring smul_mem' n _x hx := zsmul_mem (K := S) hx n @[simp] theorem mem_nonUnitalSubalgebraOfNonUnitalSubring {x : R} {S : NonUnitalSubring R} : x ∈ nonUnitalSubalgebraOfNonUnitalSubring S ↔ x ∈ S := Iff.rfl end Int
Algebra\Algebra\Operations.lean
/- Copyright (c) 2019 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.Algebra.Bilinear import Mathlib.Algebra.Algebra.Equiv import Mathlib.Algebra.Algebra.Opposite import Mathlib.Algebra.GroupWithZero.NonZeroDivisors import Mathlib.Algebra.Module.Opposites import Mathlib.Algebra.Module.Submodule.Bilinear import Mathlib.Algebra.Module.Submodule.Pointwise import Mathlib.Algebra.Order.Kleene import Mathlib.Data.Finset.Pointwise import Mathlib.Data.Set.Pointwise.BigOperators import Mathlib.Data.Set.Semiring import Mathlib.GroupTheory.GroupAction.SubMulAction.Pointwise /-! # Multiplication and division of submodules of an algebra. An interface for multiplication and division of sub-R-modules of an R-algebra A is developed. ## Main definitions Let `R` be a commutative ring (or semiring) and let `A` be an `R`-algebra. * `1 : Submodule R A` : the R-submodule R of the R-algebra A * `Mul (Submodule R A)` : multiplication of two sub-R-modules M and N of A is defined to be the smallest submodule containing all the products `m * n`. * `Div (Submodule R A)` : `I / J` is defined to be the submodule consisting of all `a : A` such that `a • J ⊆ I` It is proved that `Submodule R A` is a semiring, and also an algebra over `Set A`. Additionally, in the `Pointwise` locale we promote `Submodule.pointwiseDistribMulAction` to a `MulSemiringAction` as `Submodule.pointwiseMulSemiringAction`. ## Tags multiplication of submodules, division of submodules, submodule semiring -/ universe uι u v open Algebra Set MulOpposite open Pointwise namespace SubMulAction variable {R : Type u} {A : Type v} [CommSemiring R] [Semiring A] [Algebra R A] theorem algebraMap_mem (r : R) : algebraMap R A r ∈ (1 : SubMulAction R A) := ⟨r, (algebraMap_eq_smul_one r).symm⟩ theorem mem_one' {x : A} : x ∈ (1 : SubMulAction R A) ↔ ∃ y, algebraMap R A y = x := exists_congr fun r => by rw [algebraMap_eq_smul_one] end SubMulAction namespace Submodule variable {ι : Sort uι} variable {R : Type u} [CommSemiring R] section Ring variable {A : Type v} [Semiring A] [Algebra R A] variable (S T : Set A) {M N P Q : Submodule R A} {m n : A} /-- `1 : Submodule R A` is the submodule R of A. -/ instance one : One (Submodule R A) := -- Porting note: `f.range` notation doesn't work ⟨LinearMap.range (Algebra.linearMap R A)⟩ theorem one_eq_range : (1 : Submodule R A) = LinearMap.range (Algebra.linearMap R A) := rfl theorem le_one_toAddSubmonoid : 1 ≤ (1 : Submodule R A).toAddSubmonoid := by rintro x ⟨n, rfl⟩ exact ⟨n, map_natCast (algebraMap R A) n⟩ theorem algebraMap_mem (r : R) : algebraMap R A r ∈ (1 : Submodule R A) := LinearMap.mem_range_self (Algebra.linearMap R A) _ @[simp] theorem mem_one {x : A} : x ∈ (1 : Submodule R A) ↔ ∃ y, algebraMap R A y = x := Iff.rfl @[simp] theorem toSubMulAction_one : (1 : Submodule R A).toSubMulAction = 1 := SetLike.ext fun _ => mem_one.trans SubMulAction.mem_one'.symm theorem one_eq_span : (1 : Submodule R A) = R ∙ 1 := by apply Submodule.ext intro a simp only [mem_one, mem_span_singleton, Algebra.smul_def, mul_one] theorem one_eq_span_one_set : (1 : Submodule R A) = span R 1 := one_eq_span theorem one_le : (1 : Submodule R A) ≤ P ↔ (1 : A) ∈ P := by -- Porting note: simpa no longer closes refl goals, so added `SetLike.mem_coe` simp only [one_eq_span, span_le, Set.singleton_subset_iff, SetLike.mem_coe] protected theorem map_one {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : map f.toLinearMap (1 : Submodule R A) = 1 := by ext simp @[simp] theorem map_op_one : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (1 : Submodule R A) = 1 := by ext x induction x simp @[simp] theorem comap_op_one : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (1 : Submodule R Aᵐᵒᵖ) = 1 := by ext simp @[simp] theorem map_unop_one : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (1 : Submodule R Aᵐᵒᵖ) = 1 := by rw [← comap_equiv_eq_map_symm, comap_op_one] @[simp] theorem comap_unop_one : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (1 : Submodule R A) = 1 := by rw [← map_equiv_eq_comap_symm, map_op_one] /-- Multiplication of sub-R-modules of an R-algebra A. The submodule `M * N` is the smallest R-submodule of `A` containing the elements `m * n` for `m ∈ M` and `n ∈ N`. -/ instance mul : Mul (Submodule R A) := ⟨Submodule.map₂ <| LinearMap.mul R A⟩ theorem mul_mem_mul (hm : m ∈ M) (hn : n ∈ N) : m * n ∈ M * N := apply_mem_map₂ _ hm hn theorem mul_le : M * N ≤ P ↔ ∀ m ∈ M, ∀ n ∈ N, m * n ∈ P := map₂_le theorem mul_toAddSubmonoid (M N : Submodule R A) : (M * N).toAddSubmonoid = M.toAddSubmonoid * N.toAddSubmonoid := by dsimp [HMul.hMul, Mul.mul] -- Porting note: added `hMul` rw [map₂, iSup_toAddSubmonoid] rfl @[elab_as_elim] protected theorem mul_induction_on {C : A → Prop} {r : A} (hr : r ∈ M * N) (hm : ∀ m ∈ M, ∀ n ∈ N, C (m * n)) (ha : ∀ x y, C x → C y → C (x + y)) : C r := by rw [← mem_toAddSubmonoid, mul_toAddSubmonoid] at hr exact AddSubmonoid.mul_induction_on hr hm ha /-- A dependent version of `mul_induction_on`. -/ @[elab_as_elim] protected theorem mul_induction_on' {C : ∀ r, r ∈ M * N → Prop} (mem_mul_mem : ∀ m (hm : m ∈ M) n (hn : n ∈ N), C (m * n) (mul_mem_mul hm hn)) (add : ∀ x hx y hy, C x hx → C y hy → C (x + y) (add_mem hx hy)) {r : A} (hr : r ∈ M * N) : C r hr := by refine Exists.elim ?_ fun (hr : r ∈ M * N) (hc : C r hr) => hc exact Submodule.mul_induction_on hr (fun x hx y hy => ⟨_, mem_mul_mem _ hx _ hy⟩) fun x y ⟨_, hx⟩ ⟨_, hy⟩ => ⟨_, add _ _ _ _ hx hy⟩ variable (R) theorem span_mul_span : span R S * span R T = span R (S * T) := map₂_span_span _ _ _ _ variable {R} variable (M N P Q) @[simp] theorem mul_bot : M * ⊥ = ⊥ := map₂_bot_right _ _ @[simp] theorem bot_mul : ⊥ * M = ⊥ := map₂_bot_left _ _ -- @[simp] -- Porting note (#10618): simp can prove this once we have a monoid structure protected theorem one_mul : (1 : Submodule R A) * M = M := by conv_lhs => rw [one_eq_span, ← span_eq M] erw [span_mul_span, one_mul, span_eq] -- @[simp] -- Porting note (#10618): simp can prove this once we have a monoid structure protected theorem mul_one : M * 1 = M := by conv_lhs => rw [one_eq_span, ← span_eq M] erw [span_mul_span, mul_one, span_eq] variable {M N P Q} @[mono] theorem mul_le_mul (hmp : M ≤ P) (hnq : N ≤ Q) : M * N ≤ P * Q := map₂_le_map₂ hmp hnq theorem mul_le_mul_left (h : M ≤ N) : M * P ≤ N * P := map₂_le_map₂_left h theorem mul_le_mul_right (h : N ≤ P) : M * N ≤ M * P := map₂_le_map₂_right h variable (M N P) theorem mul_sup : M * (N ⊔ P) = M * N ⊔ M * P := map₂_sup_right _ _ _ _ theorem sup_mul : (M ⊔ N) * P = M * P ⊔ N * P := map₂_sup_left _ _ _ _ theorem mul_subset_mul : (↑M : Set A) * (↑N : Set A) ⊆ (↑(M * N) : Set A) := image2_subset_map₂ (Algebra.lmul R A).toLinearMap M N protected theorem map_mul {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : map f.toLinearMap (M * N) = map f.toLinearMap M * map f.toLinearMap N := calc map f.toLinearMap (M * N) = ⨆ i : M, (N.map (LinearMap.mul R A i)).map f.toLinearMap := map_iSup _ _ _ = map f.toLinearMap M * map f.toLinearMap N := by apply congr_arg sSup ext S constructor <;> rintro ⟨y, hy⟩ · use ⟨f y, mem_map.mpr ⟨y.1, y.2, rfl⟩⟩ -- Porting note: added `⟨⟩` refine Eq.trans ?_ hy ext simp · obtain ⟨y', hy', fy_eq⟩ := mem_map.mp y.2 use ⟨y', hy'⟩ -- Porting note: added `⟨⟩` refine Eq.trans ?_ hy rw [f.toLinearMap_apply] at fy_eq ext simp [fy_eq] theorem map_op_mul : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M * N) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) N * map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M := by apply le_antisymm · simp_rw [map_le_iff_le_comap] refine mul_le.2 fun m hm n hn => ?_ rw [mem_comap, map_equiv_eq_comap_symm, map_equiv_eq_comap_symm] show op n * op m ∈ _ exact mul_mem_mul hn hm · refine mul_le.2 (MulOpposite.rec' fun m hm => MulOpposite.rec' fun n hn => ?_) rw [Submodule.mem_map_equiv] at hm hn ⊢ exact mul_mem_mul hn hm theorem comap_unop_mul : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M * N) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) N * comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M := by simp_rw [← map_equiv_eq_comap_symm, map_op_mul] theorem map_unop_mul (M N : Submodule R Aᵐᵒᵖ) : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M * N) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) N * map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M := have : Function.Injective (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) := LinearEquiv.injective _ map_injective_of_injective this <| by rw [← map_comp, map_op_mul, ← map_comp, ← map_comp, LinearEquiv.comp_coe, LinearEquiv.symm_trans_self, LinearEquiv.refl_toLinearMap, map_id, map_id, map_id] theorem comap_op_mul (M N : Submodule R Aᵐᵒᵖ) : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M * N) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) N * comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M := by simp_rw [comap_equiv_eq_map_symm, map_unop_mul] lemma restrictScalars_mul {A B C} [CommSemiring A] [CommSemiring B] [Semiring C] [Algebra A B] [Algebra A C] [Algebra B C] [IsScalarTower A B C] {I J : Submodule B C} : (I * J).restrictScalars A = I.restrictScalars A * J.restrictScalars A := by apply le_antisymm · intro x (hx : x ∈ I * J) refine Submodule.mul_induction_on hx ?_ ?_ · exact fun m hm n hn ↦ mul_mem_mul hm hn · exact fun _ _ ↦ add_mem · exact mul_le.mpr (fun _ hm _ hn ↦ mul_mem_mul hm hn) section open Pointwise /-- `Submodule.pointwiseNeg` distributes over multiplication. This is available as an instance in the `Pointwise` locale. -/ protected def hasDistribPointwiseNeg {A} [Ring A] [Algebra R A] : HasDistribNeg (Submodule R A) := toAddSubmonoid_injective.hasDistribNeg _ neg_toAddSubmonoid mul_toAddSubmonoid scoped[Pointwise] attribute [instance] Submodule.hasDistribPointwiseNeg end section DecidableEq theorem mem_span_mul_finite_of_mem_span_mul {R A} [Semiring R] [AddCommMonoid A] [Mul A] [Module R A] {S : Set A} {S' : Set A} {x : A} (hx : x ∈ span R (S * S')) : ∃ T T' : Finset A, ↑T ⊆ S ∧ ↑T' ⊆ S' ∧ x ∈ span R (T * T' : Set A) := by classical obtain ⟨U, h, hU⟩ := mem_span_finite_of_mem_span hx obtain ⟨T, T', hS, hS', h⟩ := Finset.subset_mul h use T, T', hS, hS' have h' : (U : Set A) ⊆ T * T' := by assumption_mod_cast have h'' := span_mono h' hU assumption end DecidableEq theorem mul_eq_span_mul_set (s t : Submodule R A) : s * t = span R ((s : Set A) * (t : Set A)) := map₂_eq_span_image2 _ s t theorem iSup_mul (s : ι → Submodule R A) (t : Submodule R A) : (⨆ i, s i) * t = ⨆ i, s i * t := map₂_iSup_left _ s t theorem mul_iSup (t : Submodule R A) (s : ι → Submodule R A) : (t * ⨆ i, s i) = ⨆ i, t * s i := map₂_iSup_right _ t s theorem mem_span_mul_finite_of_mem_mul {P Q : Submodule R A} {x : A} (hx : x ∈ P * Q) : ∃ T T' : Finset A, (T : Set A) ⊆ P ∧ (T' : Set A) ⊆ Q ∧ x ∈ span R (T * T' : Set A) := Submodule.mem_span_mul_finite_of_mem_span_mul (by rwa [← Submodule.span_eq P, ← Submodule.span_eq Q, Submodule.span_mul_span] at hx) variable {M N P} theorem mem_span_singleton_mul {x y : A} : x ∈ span R {y} * P ↔ ∃ z ∈ P, y * z = x := by -- Porting note: need both `*` and `Mul.mul` simp_rw [(· * ·), Mul.mul, map₂_span_singleton_eq_map] rfl theorem mem_mul_span_singleton {x y : A} : x ∈ P * span R {y} ↔ ∃ z ∈ P, z * y = x := by -- Porting note: need both `*` and `Mul.mul` simp_rw [(· * ·), Mul.mul, map₂_span_singleton_eq_map_flip] rfl lemma span_singleton_mul {x : A} {p : Submodule R A} : Submodule.span R {x} * p = x • p := ext fun _ ↦ mem_span_singleton_mul lemma mem_smul_iff_inv_mul_mem {S} [Field S] [Algebra R S] {x : S} {p : Submodule R S} {y : S} (hx : x ≠ 0) : y ∈ x • p ↔ x⁻¹ * y ∈ p := by constructor · rintro ⟨a, ha : a ∈ p, rfl⟩; simpa [inv_mul_cancel_left₀ hx] · exact fun h ↦ ⟨_, h, by simp [mul_inv_cancel_left₀ hx]⟩ lemma mul_mem_smul_iff {S} [CommRing S] [Algebra R S] {x : S} {p : Submodule R S} {y : S} (hx : x ∈ nonZeroDivisors S) : x * y ∈ x • p ↔ y ∈ p := show Exists _ ↔ _ by simp [mul_cancel_left_mem_nonZeroDivisors hx] variable (M N) in theorem mul_smul_mul_eq_smul_mul_smul (x y : R) : (x * y) • (M * N) = (x • M) * (y • N) := by ext refine ⟨?_, fun hx ↦ Submodule.mul_induction_on hx ?_ fun _ _ hx hy ↦ Submodule.add_mem _ hx hy⟩ · rintro ⟨_, hx, rfl⟩ rw [DistribMulAction.toLinearMap_apply] refine Submodule.mul_induction_on hx (fun m hm n hn ↦ ?_) (fun _ _ hn hm ↦ ?_) · rw [← smul_mul_smul x y m n] exact mul_mem_mul (smul_mem_pointwise_smul m x M hm) (smul_mem_pointwise_smul n y N hn) · rw [smul_add] exact Submodule.add_mem _ hn hm · rintro _ ⟨m, hm, rfl⟩ _ ⟨n, hn, rfl⟩ erw [smul_mul_smul x y m n] exact smul_mem_pointwise_smul _ _ _ (mul_mem_mul hm hn) /-- Sub-R-modules of an R-algebra form an idempotent semiring. -/ instance idemSemiring : IdemSemiring (Submodule R A) := { toAddSubmonoid_injective.semigroup _ fun m n : Submodule R A => mul_toAddSubmonoid m n, AddMonoidWithOne.unary, Submodule.pointwiseAddCommMonoid, (by infer_instance : Lattice (Submodule R A)) with one_mul := Submodule.one_mul mul_one := Submodule.mul_one zero_mul := bot_mul mul_zero := mul_bot left_distrib := mul_sup right_distrib := sup_mul, -- Porting note: removed `(by infer_instance : OrderBot (Submodule R A))` bot_le := fun _ => bot_le } variable (M) theorem span_pow (s : Set A) : ∀ n : ℕ, span R s ^ n = span R (s ^ n) | 0 => by rw [pow_zero, pow_zero, one_eq_span_one_set] | n + 1 => by rw [pow_succ, pow_succ, span_pow s n, span_mul_span] theorem pow_eq_span_pow_set (n : ℕ) : M ^ n = span R ((M : Set A) ^ n) := by rw [← span_pow, span_eq] theorem pow_subset_pow {n : ℕ} : (↑M : Set A) ^ n ⊆ ↑(M ^ n : Submodule R A) := (pow_eq_span_pow_set M n).symm ▸ subset_span theorem pow_mem_pow {x : A} (hx : x ∈ M) (n : ℕ) : x ^ n ∈ M ^ n := pow_subset_pow _ <| Set.pow_mem_pow hx _ theorem pow_toAddSubmonoid {n : ℕ} (h : n ≠ 0) : (M ^ n).toAddSubmonoid = M.toAddSubmonoid ^ n := by induction' n with n ih · exact (h rfl).elim · rw [pow_succ, pow_succ, mul_toAddSubmonoid] cases n with | zero => rw [pow_zero, pow_zero, one_mul, ← mul_toAddSubmonoid, one_mul] | succ n => rw [ih n.succ_ne_zero] theorem le_pow_toAddSubmonoid {n : ℕ} : M.toAddSubmonoid ^ n ≤ (M ^ n).toAddSubmonoid := by obtain rfl | hn := Decidable.eq_or_ne n 0 · rw [pow_zero, pow_zero] exact le_one_toAddSubmonoid · exact (pow_toAddSubmonoid M hn).ge /-- Dependent version of `Submodule.pow_induction_on_left`. -/ @[elab_as_elim] protected theorem pow_induction_on_left' {C : ∀ (n : ℕ) (x), x ∈ M ^ n → Prop} (algebraMap : ∀ r : R, C 0 (algebraMap _ _ r) (algebraMap_mem r)) (add : ∀ x y i hx hy, C i x hx → C i y hy → C i (x + y) (add_mem ‹_› ‹_›)) (mem_mul : ∀ m (hm : m ∈ M), ∀ (i x hx), C i x hx → C i.succ (m * x) ((pow_succ' M i).symm ▸ (mul_mem_mul hm hx))) -- Porting note: swapped argument order to match order of `C` {n : ℕ} {x : A} (hx : x ∈ M ^ n) : C n x hx := by induction' n with n n_ih generalizing x · rw [pow_zero] at hx obtain ⟨r, rfl⟩ := hx exact algebraMap r revert hx simp_rw [pow_succ'] intro hx exact Submodule.mul_induction_on' (fun m hm x ih => mem_mul _ hm _ _ _ (n_ih ih)) (fun x hx y hy Cx Cy => add _ _ _ _ _ Cx Cy) hx /-- Dependent version of `Submodule.pow_induction_on_right`. -/ @[elab_as_elim] protected theorem pow_induction_on_right' {C : ∀ (n : ℕ) (x), x ∈ M ^ n → Prop} (algebraMap : ∀ r : R, C 0 (algebraMap _ _ r) (algebraMap_mem r)) (add : ∀ x y i hx hy, C i x hx → C i y hy → C i (x + y) (add_mem ‹_› ‹_›)) (mul_mem : ∀ i x hx, C i x hx → ∀ m (hm : m ∈ M), C i.succ (x * m) (mul_mem_mul hx hm)) -- Porting note: swapped argument order to match order of `C` {n : ℕ} {x : A} (hx : x ∈ M ^ n) : C n x hx := by induction' n with n n_ih generalizing x · rw [pow_zero] at hx obtain ⟨r, rfl⟩ := hx exact algebraMap r revert hx simp_rw [pow_succ] intro hx exact Submodule.mul_induction_on' (fun m hm x ih => mul_mem _ _ hm (n_ih _) _ ih) (fun x hx y hy Cx Cy => add _ _ _ _ _ Cx Cy) hx /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `m * x` where `m ∈ M` and it holds for `x` -/ @[elab_as_elim] protected theorem pow_induction_on_left {C : A → Prop} (hr : ∀ r : R, C (algebraMap _ _ r)) (hadd : ∀ x y, C x → C y → C (x + y)) (hmul : ∀ m ∈ M, ∀ (x), C x → C (m * x)) {x : A} {n : ℕ} (hx : x ∈ M ^ n) : C x := -- Porting note: `M` is explicit yet can't be passed positionally! Submodule.pow_induction_on_left' (M := M) (C := fun _ a _ => C a) hr (fun x y _i _hx _hy => hadd x y) (fun _m hm _i _x _hx => hmul _ hm _) hx /-- To show a property on elements of `M ^ n` holds, it suffices to show that it holds for scalars, is closed under addition, and holds for `x * m` where `m ∈ M` and it holds for `x` -/ @[elab_as_elim] protected theorem pow_induction_on_right {C : A → Prop} (hr : ∀ r : R, C (algebraMap _ _ r)) (hadd : ∀ x y, C x → C y → C (x + y)) (hmul : ∀ x, C x → ∀ m ∈ M, C (x * m)) {x : A} {n : ℕ} (hx : x ∈ M ^ n) : C x := Submodule.pow_induction_on_right' (M := M) (C := fun _ a _ => C a) hr (fun x y _i _hx _hy => hadd x y) (fun _i _x _hx => hmul _) hx /-- `Submonoid.map` as a `MonoidWithZeroHom`, when applied to `AlgHom`s. -/ @[simps] def mapHom {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') : Submodule R A →*₀ Submodule R A' where toFun := map f.toLinearMap map_zero' := Submodule.map_bot _ map_one' := Submodule.map_one _ map_mul' _ _ := Submodule.map_mul _ _ _ /-- The ring of submodules of the opposite algebra is isomorphic to the opposite ring of submodules. -/ @[simps apply symm_apply] def equivOpposite : Submodule R Aᵐᵒᵖ ≃+* (Submodule R A)ᵐᵒᵖ where toFun p := op <| p.comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) invFun p := p.unop.comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) left_inv p := SetLike.coe_injective <| rfl right_inv p := unop_injective <| SetLike.coe_injective rfl map_add' p q := by simp [comap_equiv_eq_map_symm, ← op_add] map_mul' p q := congr_arg op <| comap_op_mul _ _ protected theorem map_pow {A'} [Semiring A'] [Algebra R A'] (f : A →ₐ[R] A') (n : ℕ) : map f.toLinearMap (M ^ n) = map f.toLinearMap M ^ n := map_pow (mapHom f) M n theorem comap_unop_pow (n : ℕ) : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M ^ n) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M ^ n := (equivOpposite : Submodule R Aᵐᵒᵖ ≃+* _).symm.map_pow (op M) n theorem comap_op_pow (n : ℕ) (M : Submodule R Aᵐᵒᵖ) : comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M ^ n) = comap (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M ^ n := op_injective <| (equivOpposite : Submodule R Aᵐᵒᵖ ≃+* _).map_pow M n theorem map_op_pow (n : ℕ) : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) (M ^ n) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ) : A →ₗ[R] Aᵐᵒᵖ) M ^ n := by rw [map_equiv_eq_comap_symm, map_equiv_eq_comap_symm, comap_unop_pow] theorem map_unop_pow (n : ℕ) (M : Submodule R Aᵐᵒᵖ) : map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) (M ^ n) = map (↑(opLinearEquiv R : A ≃ₗ[R] Aᵐᵒᵖ).symm : Aᵐᵒᵖ →ₗ[R] A) M ^ n := by rw [← comap_equiv_eq_map_symm, ← comap_equiv_eq_map_symm, comap_op_pow] /-- `span` is a semiring homomorphism (recall multiplication is pointwise multiplication of subsets on either side). -/ @[simps] def span.ringHom : SetSemiring A →+* Submodule R A where -- Note: the hint `(α := A)` is new in #8386 toFun s := Submodule.span R (SetSemiring.down (α := A) s) map_zero' := span_empty map_one' := one_eq_span.symm map_add' := span_union map_mul' s t := by dsimp only -- Porting note: new, needed due to new-style structures rw [SetSemiring.down_mul, span_mul_span] section variable {α : Type*} [Monoid α] [MulSemiringAction α A] [SMulCommClass α R A] /-- The action on a submodule corresponding to applying the action to every element. This is available as an instance in the `Pointwise` locale. This is a stronger version of `Submodule.pointwiseDistribMulAction`. -/ protected def pointwiseMulSemiringAction : MulSemiringAction α (Submodule R A) := { Submodule.pointwiseDistribMulAction with smul_mul := fun r x y => Submodule.map_mul x y <| MulSemiringAction.toAlgHom R A r smul_one := fun r => Submodule.map_one <| MulSemiringAction.toAlgHom R A r } scoped[Pointwise] attribute [instance] Submodule.pointwiseMulSemiringAction end end Ring section CommRing variable {A : Type v} [CommSemiring A] [Algebra R A] variable {M N : Submodule R A} {m n : A} theorem mul_mem_mul_rev (hm : m ∈ M) (hn : n ∈ N) : n * m ∈ M * N := mul_comm m n ▸ mul_mem_mul hm hn variable (M N) protected theorem mul_comm : M * N = N * M := le_antisymm (mul_le.2 fun _r hrm _s hsn => mul_mem_mul_rev hsn hrm) (mul_le.2 fun _r hrn _s hsm => mul_mem_mul_rev hsm hrn) /-- Sub-R-modules of an R-algebra A form a semiring. -/ instance : IdemCommSemiring (Submodule R A) := { Submodule.idemSemiring with mul_comm := Submodule.mul_comm } theorem prod_span {ι : Type*} (s : Finset ι) (M : ι → Set A) : (∏ i ∈ s, Submodule.span R (M i)) = Submodule.span R (∏ i ∈ s, M i) := by letI := Classical.decEq ι refine Finset.induction_on s ?_ ?_ · simp [one_eq_span, Set.singleton_one] · intro _ _ H ih rw [Finset.prod_insert H, Finset.prod_insert H, ih, span_mul_span] theorem prod_span_singleton {ι : Type*} (s : Finset ι) (x : ι → A) : (∏ i ∈ s, span R ({x i} : Set A)) = span R {∏ i ∈ s, x i} := by rw [prod_span, Set.finset_prod_singleton] variable (R A) /-- R-submodules of the R-algebra A are a module over `Set A`. -/ instance moduleSet : Module (SetSemiring A) (Submodule R A) where -- Porting note: have to unfold both `HSMul.hSMul` and `SMul.smul` -- Note: the hint `(α := A)` is new in #8386 smul s P := span R (SetSemiring.down (α := A) s) * P smul_add _ _ _ := mul_add _ _ _ add_smul s t P := by simp_rw [HSMul.hSMul, SetSemiring.down_add, span_union, sup_mul, add_eq_sup] mul_smul s t P := by simp_rw [HSMul.hSMul, SetSemiring.down_mul, ← mul_assoc, span_mul_span] one_smul P := by simp_rw [HSMul.hSMul, SetSemiring.down_one, ← one_eq_span_one_set, one_mul] zero_smul P := by simp_rw [HSMul.hSMul, SetSemiring.down_zero, span_empty, bot_mul, bot_eq_zero] smul_zero _ := mul_bot _ variable {R A} theorem smul_def (s : SetSemiring A) (P : Submodule R A) : s • P = span R (SetSemiring.down (α := A) s) * P := rfl theorem smul_le_smul {s t : SetSemiring A} {M N : Submodule R A} (h₁ : SetSemiring.down (α := A) s ⊆ SetSemiring.down (α := A) t) (h₂ : M ≤ N) : s • M ≤ t • N := mul_le_mul (span_mono h₁) h₂ theorem singleton_smul (a : A) (M : Submodule R A) : Set.up ({a} : Set A) • M = M.map (LinearMap.mulLeft R a) := by conv_lhs => rw [← span_eq M] rw [smul_def, SetSemiring.down_up, span_mul_span, singleton_mul] exact (map (LinearMap.mulLeft R a) M).span_eq section Quotient /-- The elements of `I / J` are the `x` such that `x • J ⊆ I`. In fact, we define `x ∈ I / J` to be `∀ y ∈ J, x * y ∈ I` (see `mem_div_iff_forall_mul_mem`), which is equivalent to `x • J ⊆ I` (see `mem_div_iff_smul_subset`), but nicer to use in proofs. This is the general form of the ideal quotient, traditionally written $I : J$. -/ instance : Div (Submodule R A) := ⟨fun I J => { carrier := { x | ∀ y ∈ J, x * y ∈ I } zero_mem' := fun y _ => by rw [zero_mul] apply Submodule.zero_mem add_mem' := fun ha hb y hy => by rw [add_mul] exact Submodule.add_mem _ (ha _ hy) (hb _ hy) smul_mem' := fun r x hx y hy => by rw [Algebra.smul_mul_assoc] exact Submodule.smul_mem _ _ (hx _ hy) }⟩ theorem mem_div_iff_forall_mul_mem {x : A} {I J : Submodule R A} : x ∈ I / J ↔ ∀ y ∈ J, x * y ∈ I := Iff.refl _ theorem mem_div_iff_smul_subset {x : A} {I J : Submodule R A} : x ∈ I / J ↔ x • (J : Set A) ⊆ I := ⟨fun h y ⟨y', hy', xy'_eq_y⟩ => by rw [← xy'_eq_y] apply h assumption, fun h y hy => h (Set.smul_mem_smul_set hy)⟩ theorem le_div_iff {I J K : Submodule R A} : I ≤ J / K ↔ ∀ x ∈ I, ∀ z ∈ K, x * z ∈ J := Iff.refl _ theorem le_div_iff_mul_le {I J K : Submodule R A} : I ≤ J / K ↔ I * K ≤ J := by rw [le_div_iff, mul_le] @[simp] theorem one_le_one_div {I : Submodule R A} : 1 ≤ 1 / I ↔ I ≤ 1 := by constructor; all_goals intro hI · rwa [le_div_iff_mul_le, one_mul] at hI · rwa [le_div_iff_mul_le, one_mul] theorem le_self_mul_one_div {I : Submodule R A} (hI : I ≤ 1) : I ≤ I * (1 / I) := by refine (mul_one I).symm.trans_le ?_ -- Porting note: drop `rw {occs := _}` in favor of `refine` apply mul_le_mul_right (one_le_one_div.mpr hI) theorem mul_one_div_le_one {I : Submodule R A} : I * (1 / I) ≤ 1 := by rw [Submodule.mul_le] intro m hm n hn rw [Submodule.mem_div_iff_forall_mul_mem] at hn rw [mul_comm] exact hn m hm @[simp] protected theorem map_div {B : Type*} [CommSemiring B] [Algebra R B] (I J : Submodule R A) (h : A ≃ₐ[R] B) : (I / J).map h.toLinearMap = I.map h.toLinearMap / J.map h.toLinearMap := by ext x simp only [mem_map, mem_div_iff_forall_mul_mem] constructor · rintro ⟨x, hx, rfl⟩ _ ⟨y, hy, rfl⟩ exact ⟨x * y, hx _ hy, map_mul h x y⟩ · rintro hx refine ⟨h.symm x, fun z hz => ?_, h.apply_symm_apply x⟩ obtain ⟨xz, xz_mem, hxz⟩ := hx (h z) ⟨z, hz, rfl⟩ convert xz_mem apply h.injective erw [map_mul, h.apply_symm_apply, hxz] end Quotient end CommRing end Submodule
Algebra\Algebra\Opposite.lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Algebra.Equiv import Mathlib.Algebra.Module.Opposites import Mathlib.Algebra.Ring.Opposite /-! # Algebra structures on the multiplicative opposite ## Main definitions * `MulOpposite.instAlgebra`: the algebra on `Aᵐᵒᵖ` * `AlgHom.op`/`AlgHom.unop`: simultaneously convert the domain and codomain of a morphism to the opposite algebra. * `AlgHom.opComm`: swap which side of a morphism lies in the opposite algebra. * `AlgEquiv.op`/`AlgEquiv.unop`: simultaneously convert the source and target of an isomorphism to the opposite algebra. * `AlgEquiv.opOp`: any algebra is isomorphic to the opposite of its opposite. * `AlgEquiv.toOpposite`: in a commutative algebra, the opposite algebra is isomorphic to the original algebra. * `AlgEquiv.opComm`: swap which side of an isomorphism lies in the opposite algebra. -/ variable {R S A B : Type*} open MulOpposite section Semiring variable [CommSemiring R] [CommSemiring S] [Semiring A] [Semiring B] variable [Algebra R S] [Algebra R A] [Algebra R B] [Algebra S A] [SMulCommClass R S A] variable [IsScalarTower R S A] namespace MulOpposite instance instAlgebra : Algebra R Aᵐᵒᵖ where toRingHom := (algebraMap R A).toOpposite fun x y => Algebra.commutes _ _ smul_def' c x := unop_injective <| by simp only [unop_smul, RingHom.toOpposite_apply, Function.comp_apply, unop_mul, op_mul, Algebra.smul_def, Algebra.commutes, op_unop, unop_op] commutes' r := MulOpposite.rec' fun x => by simp only [RingHom.toOpposite_apply, Function.comp_apply, ← op_mul, Algebra.commutes] @[simp] theorem algebraMap_apply (c : R) : algebraMap R Aᵐᵒᵖ c = op (algebraMap R A c) := rfl end MulOpposite namespace AlgEquiv variable (R A) /-- An algebra is isomorphic to the opposite of its opposite. -/ @[simps!] def opOp : A ≃ₐ[R] Aᵐᵒᵖᵐᵒᵖ where __ := RingEquiv.opOp A commutes' _ := rfl @[simp] theorem toRingEquiv_opOp : (opOp R A : A ≃+* Aᵐᵒᵖᵐᵒᵖ) = RingEquiv.opOp A := rfl end AlgEquiv namespace AlgHom /-- An algebra homomorphism `f : A →ₐ[R] B` such that `f x` commutes with `f y` for all `x, y` defines an algebra homomorphism from `Aᵐᵒᵖ`. -/ @[simps (config := .asFn)] def fromOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : Aᵐᵒᵖ →ₐ[R] B := { f.toRingHom.fromOpposite hf with toFun := f ∘ unop commutes' := fun r => f.commutes r } @[simp] theorem toLinearMap_fromOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : (f.fromOpposite hf).toLinearMap = f.toLinearMap ∘ₗ (opLinearEquiv R (M := A)).symm := rfl @[simp] theorem toRingHom_fromOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : (f.fromOpposite hf : Aᵐᵒᵖ →+* B) = (f : A →+* B).fromOpposite hf := rfl /-- An algebra homomorphism `f : A →ₐ[R] B` such that `f x` commutes with `f y` for all `x, y` defines an algebra homomorphism to `Bᵐᵒᵖ`. -/ @[simps (config := .asFn)] def toOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : A →ₐ[R] Bᵐᵒᵖ := { f.toRingHom.toOpposite hf with toFun := op ∘ f commutes' := fun r => unop_injective <| f.commutes r } @[simp] theorem toLinearMap_toOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : (f.toOpposite hf).toLinearMap = (opLinearEquiv R : B ≃ₗ[R] Bᵐᵒᵖ) ∘ₗ f.toLinearMap := rfl @[simp] theorem toRingHom_toOpposite (f : A →ₐ[R] B) (hf : ∀ x y, Commute (f x) (f y)) : (f.toOpposite hf : A →+* Bᵐᵒᵖ) = (f : A →+* B).toOpposite hf := rfl /-- An algebra hom `A →ₐ[R] B` can equivalently be viewed as an algebra hom `Aᵐᵒᵖ →ₐ[R] Bᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on morphisms. -/ @[simps!] protected def op : (A →ₐ[R] B) ≃ (Aᵐᵒᵖ →ₐ[R] Bᵐᵒᵖ) where toFun f := { RingHom.op f.toRingHom with commutes' := fun r => unop_injective <| f.commutes r } invFun f := { RingHom.unop f.toRingHom with commutes' := fun r => op_injective <| f.commutes r } left_inv _f := AlgHom.ext fun _a => rfl right_inv _f := AlgHom.ext fun _a => rfl theorem toRingHom_op (f : A →ₐ[R] B) : f.op.toRingHom = RingHom.op f.toRingHom := rfl /-- The 'unopposite' of an algebra hom `Aᵐᵒᵖ →ₐ[R] Bᵐᵒᵖ`. Inverse to `RingHom.op`. -/ abbrev unop : (Aᵐᵒᵖ →ₐ[R] Bᵐᵒᵖ) ≃ (A →ₐ[R] B) := AlgHom.op.symm theorem toRingHom_unop (f : Aᵐᵒᵖ →ₐ[R] Bᵐᵒᵖ) : f.unop.toRingHom = RingHom.unop f.toRingHom := rfl /-- Swap the `ᵐᵒᵖ` on an algebra hom to the opposite side. -/ @[simps!] def opComm : (A →ₐ[R] Bᵐᵒᵖ) ≃ (Aᵐᵒᵖ →ₐ[R] B) := AlgHom.op.trans <| AlgEquiv.refl.arrowCongr (AlgEquiv.opOp R B).symm end AlgHom namespace AlgEquiv /-- An algebra iso `A ≃ₐ[R] B` can equivalently be viewed as an algebra iso `Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ`. This is the action of the (fully faithful) `ᵐᵒᵖ`-functor on morphisms. -/ @[simps!] def op : (A ≃ₐ[R] B) ≃ Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ where toFun f := { RingEquiv.op f.toRingEquiv with commutes' := fun r => MulOpposite.unop_injective <| f.commutes r } invFun f := { RingEquiv.unop f.toRingEquiv with commutes' := fun r => MulOpposite.op_injective <| f.commutes r } left_inv _f := AlgEquiv.ext fun _a => rfl right_inv _f := AlgEquiv.ext fun _a => rfl theorem toAlgHom_op (f : A ≃ₐ[R] B) : (AlgEquiv.op f).toAlgHom = AlgHom.op f.toAlgHom := rfl theorem toRingEquiv_op (f : A ≃ₐ[R] B) : (AlgEquiv.op f).toRingEquiv = RingEquiv.op f.toRingEquiv := rfl /-- The 'unopposite' of an algebra iso `Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ`. Inverse to `AlgEquiv.op`. -/ abbrev unop : (Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ) ≃ A ≃ₐ[R] B := AlgEquiv.op.symm theorem toAlgHom_unop (f : Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ) : f.unop.toAlgHom = AlgHom.unop f.toAlgHom := rfl theorem toRingEquiv_unop (f : Aᵐᵒᵖ ≃ₐ[R] Bᵐᵒᵖ) : (AlgEquiv.unop f).toRingEquiv = RingEquiv.unop f.toRingEquiv := rfl /-- Swap the `ᵐᵒᵖ` on an algebra isomorphism to the opposite side. -/ @[simps!] def opComm : (A ≃ₐ[R] Bᵐᵒᵖ) ≃ (Aᵐᵒᵖ ≃ₐ[R] B) := AlgEquiv.op.trans <| AlgEquiv.refl.equivCongr (opOp R B).symm end AlgEquiv end Semiring section CommSemiring variable (R A) [CommSemiring R] [CommSemiring A] [Algebra R A] namespace AlgEquiv /-- A commutative algebra is isomorphic to its opposite. -/ @[simps!] def toOpposite : A ≃ₐ[R] Aᵐᵒᵖ where __ := RingEquiv.toOpposite A commutes' _r := rfl @[simp] lemma toRingEquiv_toOpposite : (toOpposite R A : A ≃+* Aᵐᵒᵖ) = RingEquiv.toOpposite A := rfl @[simp] lemma toLinearEquiv_toOpposite : toLinearEquiv (toOpposite R A) = opLinearEquiv R := rfl end AlgEquiv end CommSemiring
Algebra\Algebra\Pi.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Equiv /-! # The R-algebra structure on families of R-algebras The R-algebra structure on `∀ i : I, A i` when each `A i` is an R-algebra. ## Main definitions * `Pi.algebra` * `Pi.evalAlgHom` * `Pi.constAlgHom` -/ universe u v w namespace Pi -- The indexing type variable {I : Type u} -- The scalar type variable {R : Type*} -- The family of types already equipped with instances variable {f : I → Type v} variable (x y : ∀ i, f i) (i : I) variable (I f) instance algebra {r : CommSemiring R} [s : ∀ i, Semiring (f i)] [∀ i, Algebra R (f i)] : Algebra R (∀ i : I, f i) := { (Pi.ringHom fun i => algebraMap R (f i) : R →+* ∀ i : I, f i) with commutes' := fun a f => by ext; simp [Algebra.commutes] smul_def' := fun a f => by ext; simp [Algebra.smul_def] } theorem algebraMap_def {_ : CommSemiring R} [_s : ∀ i, Semiring (f i)] [∀ i, Algebra R (f i)] (a : R) : algebraMap R (∀ i, f i) a = fun i => algebraMap R (f i) a := rfl @[simp] theorem algebraMap_apply {_ : CommSemiring R} [_s : ∀ i, Semiring (f i)] [∀ i, Algebra R (f i)] (a : R) (i : I) : algebraMap R (∀ i, f i) a i = algebraMap R (f i) a := rfl -- One could also build a `∀ i, R i`-algebra structure on `∀ i, A i`, -- when each `A i` is an `R i`-algebra, although I'm not sure that it's useful. variable {I} (R) /-- `Function.eval` as an `AlgHom`. The name matches `Pi.evalRingHom`, `Pi.evalMonoidHom`, etc. -/ @[simps] def evalAlgHom {_ : CommSemiring R} [∀ i, Semiring (f i)] [∀ i, Algebra R (f i)] (i : I) : (∀ i, f i) →ₐ[R] f i := { Pi.evalRingHom f i with toFun := fun f => f i commutes' := fun _ => rfl } variable (A B : Type*) [CommSemiring R] [Semiring B] [Algebra R B] /-- `Function.const` as an `AlgHom`. The name matches `Pi.constRingHom`, `Pi.constMonoidHom`, etc. -/ @[simps] def constAlgHom : B →ₐ[R] A → B := { Pi.constRingHom A B with toFun := Function.const _ commutes' := fun _ => rfl } /-- When `R` is commutative and permits an `algebraMap`, `Pi.constRingHom` is equal to that map. -/ @[simp] theorem constRingHom_eq_algebraMap : constRingHom A R = algebraMap R (A → R) := rfl @[simp] theorem constAlgHom_eq_algebra_ofId : constAlgHom R A R = Algebra.ofId R (A → R) := rfl end Pi /-- A special case of `Pi.algebra` for non-dependent types. Lean struggles to elaborate definitions elsewhere in the library without this, -/ instance Function.algebra {R : Type*} (I : Type*) (A : Type*) [CommSemiring R] [Semiring A] [Algebra R A] : Algebra R (I → A) := Pi.algebra _ _ namespace AlgHom variable {R : Type u} {A : Type v} {B : Type w} {I : Type*} variable [CommSemiring R] [Semiring A] [Semiring B] variable [Algebra R A] [Algebra R B] /-- `R`-algebra homomorphism between the function spaces `I → A` and `I → B`, induced by an `R`-algebra homomorphism `f` between `A` and `B`. -/ @[simps] protected def compLeft (f : A →ₐ[R] B) (I : Type*) : (I → A) →ₐ[R] I → B := { f.toRingHom.compLeft I with toFun := fun h => f ∘ h commutes' := fun c => by ext exact f.commutes' c } end AlgHom namespace AlgEquiv /-- A family of algebra equivalences `∀ i, (A₁ i ≃ₐ A₂ i)` generates a multiplicative equivalence between `∀ i, A₁ i` and `∀ i, A₂ i`. This is the `AlgEquiv` version of `Equiv.piCongrRight`, and the dependent version of `AlgEquiv.arrowCongr`. -/ @[simps apply] def piCongrRight {R ι : Type*} {A₁ A₂ : ι → Type*} [CommSemiring R] [∀ i, Semiring (A₁ i)] [∀ i, Semiring (A₂ i)] [∀ i, Algebra R (A₁ i)] [∀ i, Algebra R (A₂ i)] (e : ∀ i, A₁ i ≃ₐ[R] A₂ i) : (∀ i, A₁ i) ≃ₐ[R] ∀ i, A₂ i := { @RingEquiv.piCongrRight ι A₁ A₂ _ _ fun i => (e i).toRingEquiv with toFun := fun x j => e j (x j) invFun := fun x j => (e j).symm (x j) commutes' := fun r => by ext i simp } @[simp] theorem piCongrRight_refl {R ι : Type*} {A : ι → Type*} [CommSemiring R] [∀ i, Semiring (A i)] [∀ i, Algebra R (A i)] : (piCongrRight fun i => (AlgEquiv.refl : A i ≃ₐ[R] A i)) = AlgEquiv.refl := rfl @[simp] theorem piCongrRight_symm {R ι : Type*} {A₁ A₂ : ι → Type*} [CommSemiring R] [∀ i, Semiring (A₁ i)] [∀ i, Semiring (A₂ i)] [∀ i, Algebra R (A₁ i)] [∀ i, Algebra R (A₂ i)] (e : ∀ i, A₁ i ≃ₐ[R] A₂ i) : (piCongrRight e).symm = piCongrRight fun i => (e i).symm := rfl @[simp] theorem piCongrRight_trans {R ι : Type*} {A₁ A₂ A₃ : ι → Type*} [CommSemiring R] [∀ i, Semiring (A₁ i)] [∀ i, Semiring (A₂ i)] [∀ i, Semiring (A₃ i)] [∀ i, Algebra R (A₁ i)] [∀ i, Algebra R (A₂ i)] [∀ i, Algebra R (A₃ i)] (e₁ : ∀ i, A₁ i ≃ₐ[R] A₂ i) (e₂ : ∀ i, A₂ i ≃ₐ[R] A₃ i) : (piCongrRight e₁).trans (piCongrRight e₂) = piCongrRight fun i => (e₁ i).trans (e₂ i) := rfl end AlgEquiv
Algebra\Algebra\Prod.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Hom import Mathlib.Algebra.Module.Prod /-! # The R-algebra structure on products of R-algebras The R-algebra structure on `(i : I) → A i` when each `A i` is an R-algebra. ## Main definitions * `Prod.algebra` * `AlgHom.fst` * `AlgHom.snd` * `AlgHom.prod` -/ variable {R A B C : Type*} variable [CommSemiring R] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] namespace Prod variable (R A B) open Algebra instance algebra : Algebra R (A × B) := { Prod.instModule, RingHom.prod (algebraMap R A) (algebraMap R B) with commutes' := by rintro r ⟨a, b⟩ dsimp rw [commutes r a, commutes r b] smul_def' := by rintro r ⟨a, b⟩ dsimp rw [Algebra.smul_def r a, Algebra.smul_def r b] } variable {R A B} @[simp] theorem algebraMap_apply (r : R) : algebraMap R (A × B) r = (algebraMap R A r, algebraMap R B r) := rfl end Prod namespace AlgHom variable (R A B) /-- First projection as `AlgHom`. -/ def fst : A × B →ₐ[R] A := { RingHom.fst A B with commutes' := fun _r => rfl } /-- Second projection as `AlgHom`. -/ def snd : A × B →ₐ[R] B := { RingHom.snd A B with commutes' := fun _r => rfl } variable {R A B} /-- The `Pi.prod` of two morphisms is a morphism. -/ @[simps!] def prod (f : A →ₐ[R] B) (g : A →ₐ[R] C) : A →ₐ[R] B × C := { f.toRingHom.prod g.toRingHom with commutes' := fun r => by simp only [toRingHom_eq_coe, RingHom.toFun_eq_coe, RingHom.prod_apply, coe_toRingHom, commutes, Prod.algebraMap_apply] } theorem coe_prod (f : A →ₐ[R] B) (g : A →ₐ[R] C) : ⇑(f.prod g) = Pi.prod f g := rfl @[simp] theorem fst_prod (f : A →ₐ[R] B) (g : A →ₐ[R] C) : (fst R B C).comp (prod f g) = f := by ext; rfl @[simp] theorem snd_prod (f : A →ₐ[R] B) (g : A →ₐ[R] C) : (snd R B C).comp (prod f g) = g := by ext; rfl @[simp] theorem prod_fst_snd : prod (fst R A B) (snd R A B) = 1 := DFunLike.coe_injective Pi.prod_fst_snd /-- Taking the product of two maps with the same domain is equivalent to taking the product of their codomains. -/ @[simps] def prodEquiv : (A →ₐ[R] B) × (A →ₐ[R] C) ≃ (A →ₐ[R] B × C) where toFun f := f.1.prod f.2 invFun f := ((fst _ _ _).comp f, (snd _ _ _).comp f) left_inv f := by ext <;> rfl right_inv f := by ext <;> rfl /-- `Prod.map` of two algebra homomorphisms. -/ def prodMap {D : Type*} [Semiring D] [Algebra R D] (f : A →ₐ[R] B) (g : C →ₐ[R] D) : A × C →ₐ[R] B × D := { toRingHom := f.toRingHom.prodMap g.toRingHom commutes' := fun r => by simp [commutes] } end AlgHom
Algebra\Algebra\Quasispectrum.lean
/- Copyright (c) 2024 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Algebra.Unitization import Mathlib.Algebra.Algebra.Spectrum /-! # Quasiregularity and quasispectrum For a non-unital ring `R`, an element `r : R` is *quasiregular* if it is invertible in the monoid `(R, ∘)` where `x ∘ y := y + x + x * y` with identity `0 : R`. We implement this both as a type synonym `PreQuasiregular` which has an associated `Monoid` instance (note: *not* an `AddMonoid` instance despite the fact that `0 : R` is the identity in this monoid) so that one may access the quasiregular elements of `R` as `(PreQuasiregular R)ˣ`, but also as a predicate `IsQuasiregular`. Quasiregularity is closely tied to invertibility. Indeed, `(PreQuasiregular A)ˣ` is isomorphic to the subgroup of `Unitization R A` whose scalar part is `1`, whenever `A` is a non-unital `R`-algebra, and moreover this isomorphism is implemented by the map `(x : A) ↦ (1 + x : Unitization R A)`. It is because of this isomorphism, and the associated ties with multiplicative invertibility, that we choose a `Monoid` (as opposed to an `AddMonoid`) structure on `PreQuasiregular`. In addition, in unital rings, we even have `IsQuasiregular x ↔ IsUnit (1 + x)`. The *quasispectrum* of `a : A` (with respect to `R`) is defined in terms of quasiregularity, and this is the natural analogue of the `spectrum` for non-unital rings. Indeed, it is true that `quasispectrum R a = spectrum R a ∪ {0}` when `A` is unital. In Mathlib, the quasispectrum is the domain of the continuous functions associated to the *non-unital* continuous functional calculus. ## Main definitions + `PreQuasiregular R`: a structure wrapping `R` that inherits a distinct `Monoid` instance when `R` is a non-unital semiring. + `Unitization.unitsFstOne`: the subgroup with carrier `{ x : (Unitization R A)ˣ | x.fst = 1 }`. + `unitsFstOne_mulEquiv_quasiregular`: the group isomorphism between `Unitization.unitsFstOne` and the units of `PreQuasiregular` (i.e., the quasiregular elements) which sends `(1, x) ↦ x`. + `IsQuasiregular x`: the proposition that `x : R` is a unit with respect to the monoid structure on `PreQuasiregular R`, i.e., there is some `u : (PreQuasiregular R)ˣ` such that `u.val` is identified with `x` (via the natural equivalence between `R` and `PreQuasiregular R`). + `quasispectrum R a`: in an algebra over the semifield `R`, this is the set `{r : R | (hr : IsUnit r) → ¬ IsQuasiregular (-(hr.unit⁻¹ • a))}`, which should be thought of as a version of the `spectrum` which is applicable in non-unital algebras. ## Main theorems + `isQuasiregular_iff_isUnit`: in a unital ring, `x` is quasiregular if and only if `1 + x` is a unit. + `quasispectrum_eq_spectrum_union_zero`: in a unital algebra `A` over a semifield `R`, the quasispectrum of `a : A` is the `spectrum` with zero added. + `Unitization.isQuasiregular_inr_iff`: `a : A` is quasiregular if and only if it is quasiregular in `Unitization R A` (via the coercion `Unitization.inr`). + `Unitization.quasispectrum_eq_spectrum_inr`: the quasispectrum of `a` in a non-unital `R`-algebra `A` is precisely the spectrum of `a` in the unitization. in `Unitization R A` (via the coercion `Unitization.inr`). -/ /-- A type synonym for non-unital rings where an alternative monoid structure is introduced. If `R` is a non-unital semiring, then `PreQuasiregular R` is equipped with the monoid structure with binary operation `fun x y ↦ y + x + x * y` and identity `0`. Elements of `R` which are invertible in this monoid satisfy the predicate `IsQuasiregular`. -/ structure PreQuasiregular (R : Type*) where /-- The value wrapped into a term of `PreQuasiregular`. -/ val : R namespace PreQuasiregular variable {R : Type*} [NonUnitalSemiring R] /-- The identity map between `R` and `PreQuasiregular R`. -/ @[simps] def equiv : R ≃ PreQuasiregular R where toFun := .mk invFun := PreQuasiregular.val left_inv _ := rfl right_inv _ := rfl instance instOne : One (PreQuasiregular R) where one := equiv 0 @[simp] lemma val_one : (1 : PreQuasiregular R).val = 0 := rfl instance instMul : Mul (PreQuasiregular R) where mul x y := .mk (y.val + x.val + x.val * y.val) @[simp] lemma val_mul (x y : PreQuasiregular R) : (x * y).val = y.val + x.val + x.val * y.val := rfl instance instMonoid : Monoid (PreQuasiregular R) where one := equiv 0 mul x y := .mk (y.val + x.val + x.val * y.val) mul_one _ := equiv.symm.injective <| by simp [-EmbeddingLike.apply_eq_iff_eq] one_mul _ := equiv.symm.injective <| by simp [-EmbeddingLike.apply_eq_iff_eq] mul_assoc x y z := equiv.symm.injective <| by simp [mul_add, add_mul, mul_assoc]; abel @[simp] lemma inv_add_add_mul_eq_zero (u : (PreQuasiregular R)ˣ) : u⁻¹.val.val + u.val.val + u.val.val * u⁻¹.val.val = 0 := by simpa [-Units.mul_inv] using congr($(u.mul_inv).val) @[simp] lemma add_inv_add_mul_eq_zero (u : (PreQuasiregular R)ˣ) : u.val.val + u⁻¹.val.val + u⁻¹.val.val * u.val.val = 0 := by simpa [-Units.inv_mul] using congr($(u.inv_mul).val) end PreQuasiregular namespace Unitization open PreQuasiregular variable {R A : Type*} [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] variable (R A) in /-- The subgroup of the units of `Unitization R A` whose scalar part is `1`. -/ def unitsFstOne : Subgroup (Unitization R A)ˣ where carrier := {x | x.val.fst = 1} one_mem' := rfl mul_mem' {x} {y} (hx : fst x.val = 1) (hy : fst y.val = 1) := by simp [hx, hy] inv_mem' {x} (hx : fst x.val = 1) := by simpa [-Units.mul_inv, hx] using congr(fstHom R A $(x.mul_inv)) @[simp] lemma mem_unitsFstOne {x : (Unitization R A)ˣ} : x ∈ unitsFstOne R A ↔ x.val.fst = 1 := Iff.rfl @[simp] lemma unitsFstOne_val_val_fst (x : (unitsFstOne R A)) : x.val.val.fst = 1 := mem_unitsFstOne.mp x.property @[simp] lemma unitsFstOne_val_inv_val_fst (x : (unitsFstOne R A)) : x.val⁻¹.val.fst = 1 := mem_unitsFstOne.mp x⁻¹.property variable (R) in /-- If `A` is a non-unital `R`-algebra, then the subgroup of units of `Unitization R A` whose scalar part is `1 : R` (i.e., `Unitization.unitsFstOne`) is isomorphic to the group of units of `PreQuasiregular A`. -/ @[simps] def unitsFstOne_mulEquiv_quasiregular : unitsFstOne R A ≃* (PreQuasiregular A)ˣ where toFun x := { val := equiv x.val.val.snd inv := equiv x⁻¹.val.val.snd val_inv := equiv.symm.injective <| by simpa [-Units.mul_inv] using congr(snd $(x.val.mul_inv)) inv_val := equiv.symm.injective <| by simpa [-Units.inv_mul] using congr(snd $(x.val.inv_mul)) } invFun x := { val := { val := 1 + equiv.symm x.val inv := 1 + equiv.symm x⁻¹.val val_inv := by convert congr((1 + $(inv_add_add_mul_eq_zero x) : Unitization R A)) using 1 · simp only [mul_one, equiv_symm_apply, one_mul, inr_zero, add_zero, mul_add, add_mul, inr_add, inr_mul] abel · simp only [inr_zero, add_zero] inv_val := by convert congr((1 + $(add_inv_add_mul_eq_zero x) : Unitization R A)) using 1 · simp only [mul_one, equiv_symm_apply, one_mul, inr_zero, add_zero, mul_add, add_mul, inr_add, inr_mul] abel · simp only [inr_zero, add_zero] } property := by simp } left_inv x := Subtype.ext <| Units.ext <| by simpa using x.val.val.inl_fst_add_inr_snd_eq right_inv x := Units.ext <| by simp [-equiv_symm_apply] map_mul' x y := Units.ext <| equiv.symm.injective <| by simp end Unitization section PreQuasiregular open PreQuasiregular variable {R : Type*} [NonUnitalSemiring R] /-- In a non-unital semiring `R`, an element `x : R` satisfies `IsQuasiregular` if it is a unit under the monoid operation `fun x y ↦ y + x + x * y`. -/ def IsQuasiregular (x : R) : Prop := ∃ u : (PreQuasiregular R)ˣ, equiv.symm u.val = x @[simp] lemma isQuasiregular_zero : IsQuasiregular 0 := ⟨1, rfl⟩ lemma isQuasiregular_iff {x : R} : IsQuasiregular x ↔ ∃ y, y + x + x * y = 0 ∧ x + y + y * x = 0 := by constructor · rintro ⟨u, rfl⟩ exact ⟨equiv.symm u⁻¹.val, by simp⟩ · rintro ⟨y, hy₁, hy₂⟩ refine ⟨⟨equiv x, equiv y, ?_, ?_⟩, rfl⟩ all_goals apply equiv.symm.injective assumption end PreQuasiregular lemma IsQuasiregular.map {F R S : Type*} [NonUnitalSemiring R] [NonUnitalSemiring S] [FunLike F R S] [NonUnitalRingHomClass F R S] (f : F) {x : R} (hx : IsQuasiregular x) : IsQuasiregular (f x) := by rw [isQuasiregular_iff] at hx ⊢ obtain ⟨y, hy₁, hy₂⟩ := hx exact ⟨f y, by simpa using And.intro congr(f $(hy₁)) congr(f $(hy₂))⟩ lemma IsQuasiregular.isUnit_one_add {R : Type*} [Semiring R] {x : R} (hx : IsQuasiregular x) : IsUnit (1 + x) := by obtain ⟨y, hy₁, hy₂⟩ := isQuasiregular_iff.mp hx refine ⟨⟨1 + x, 1 + y, ?_, ?_⟩, rfl⟩ · convert congr(1 + $(hy₁)) using 1 <;> [noncomm_ring; simp] · convert congr(1 + $(hy₂)) using 1 <;> [noncomm_ring; simp] lemma isQuasiregular_iff_isUnit {R : Type*} [Ring R] {x : R} : IsQuasiregular x ↔ IsUnit (1 + x) := by refine ⟨IsQuasiregular.isUnit_one_add, fun hx ↦ ?_⟩ rw [isQuasiregular_iff] use hx.unit⁻¹ - 1 constructor case' h.left => have := congr($(hx.mul_val_inv) - 1) case' h.right => have := congr($(hx.val_inv_mul) - 1) all_goals rw [← sub_add_cancel (↑hx.unit⁻¹ : R) 1, sub_self] at this convert this using 1 noncomm_ring -- interestingly, this holds even in the semiring case. lemma isQuasiregular_iff_isUnit' (R : Type*) {A : Type*} [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] {x : A} : IsQuasiregular x ↔ IsUnit (1 + x : Unitization R A) := by refine ⟨?_, fun hx ↦ ?_⟩ · rintro ⟨u, rfl⟩ exact (Unitization.unitsFstOne_mulEquiv_quasiregular R).symm u |>.val.isUnit · exact ⟨(Unitization.unitsFstOne_mulEquiv_quasiregular R) ⟨hx.unit, by simp⟩, by simp⟩ variable (R : Type*) {A : Type*} [CommSemiring R] [NonUnitalRing A] [Module R A] /-- If `A` is a non-unital `R`-algebra, the `R`-quasispectrum of `a : A` consists of those `r : R` such that if `r` is invertible (in `R`), then `-(r⁻¹ • a)` is not quasiregular. The quasispectrum is precisely the spectrum in the unitization when `R` is a commutative ring. See `Unitization.quasispectrum_eq_spectrum_inr`. -/ def quasispectrum (a : A) : Set R := {r : R | (hr : IsUnit r) → ¬ IsQuasiregular (-(hr.unit⁻¹ • a))} variable {R} in lemma quasispectrum.not_isUnit_mem (a : A) {r : R} (hr : ¬ IsUnit r) : r ∈ quasispectrum R a := fun hr' ↦ (hr hr').elim @[simp] lemma quasispectrum.zero_mem [Nontrivial R] (a : A) : 0 ∈ quasispectrum R a := quasispectrum.not_isUnit_mem a <| by simp instance quasispectrum.instZero [Nontrivial R] (a : A) : Zero (quasispectrum R a) where zero := ⟨0, quasispectrum.zero_mem R a⟩ variable {R} @[simp] lemma quasispectrum.coe_zero [Nontrivial R] (a : A) : (0 : quasispectrum R a) = (0 : R) := rfl lemma quasispectrum.mem_of_not_quasiregular (a : A) {r : Rˣ} (hr : ¬ IsQuasiregular (-(r⁻¹ • a))) : (r : R) ∈ quasispectrum R a := fun _ ↦ by simpa using hr lemma quasispectrum_eq_spectrum_union (R : Type*) {A : Type*} [CommSemiring R] [Ring A] [Algebra R A] (a : A) : quasispectrum R a = spectrum R a ∪ {r : R | ¬ IsUnit r} := by ext r rw [quasispectrum] simp only [Set.mem_setOf_eq, Set.mem_union, ← imp_iff_or_not, spectrum.mem_iff] congr! 1 with hr rw [not_iff_not, isQuasiregular_iff_isUnit, ← sub_eq_add_neg, Algebra.algebraMap_eq_smul_one] exact (IsUnit.smul_sub_iff_sub_inv_smul hr.unit a).symm lemma spectrum_subset_quasispectrum (R : Type*) {A : Type*} [CommSemiring R] [Ring A] [Algebra R A] (a : A) : spectrum R a ⊆ quasispectrum R a := quasispectrum_eq_spectrum_union R a ▸ Set.subset_union_left lemma quasispectrum_eq_spectrum_union_zero (R : Type*) {A : Type*} [Semifield R] [Ring A] [Algebra R A] (a : A) : quasispectrum R a = spectrum R a ∪ {0} := by convert quasispectrum_eq_spectrum_union R a ext x simp lemma mem_quasispectrum_iff {R A : Type*} [Semifield R] [Ring A] [Algebra R A] {a : A} {x : R} : x ∈ quasispectrum R a ↔ x = 0 ∨ x ∈ spectrum R a := by simp [quasispectrum_eq_spectrum_union_zero] namespace Unitization variable [IsScalarTower R A A] [SMulCommClass R A A] lemma isQuasiregular_inr_iff (a : A) : IsQuasiregular (a : Unitization R A) ↔ IsQuasiregular a := by refine ⟨fun ha ↦ ?_, IsQuasiregular.map (inrNonUnitalAlgHom R A)⟩ rw [isQuasiregular_iff] at ha ⊢ obtain ⟨y, hy₁, hy₂⟩ := ha lift y to A using by simpa using congr(fstHom R A $(hy₁)) refine ⟨y, ?_, ?_⟩ <;> exact inr_injective (R := R) <| by simpa lemma zero_mem_spectrum_inr (R S : Type*) {A : Type*} [CommSemiring R] [CommRing S] [Nontrivial S] [NonUnitalRing A] [Algebra R S] [Module S A] [IsScalarTower S A A] [SMulCommClass S A A] [Module R A] [IsScalarTower R S A] (a : A) : 0 ∈ spectrum R (a : Unitization S A) := by rw [spectrum.zero_mem_iff] rintro ⟨u, hu⟩ simpa [-Units.mul_inv, hu] using congr($(u.mul_inv).fst) lemma mem_spectrum_inr_of_not_isUnit {R A : Type*} [CommRing R] [NonUnitalRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] (a : A) (r : R) (hr : ¬ IsUnit r) : r ∈ spectrum R (a : Unitization R A) := fun h ↦ hr <| by simpa using h.map (fstHom R A) lemma quasispectrum_eq_spectrum_inr (R : Type*) {A : Type*} [CommRing R] [Ring A] [Algebra R A] (a : A) : quasispectrum R a = spectrum R (a : Unitization R A) := by ext r have : { r | ¬ IsUnit r} ⊆ spectrum R _ := mem_spectrum_inr_of_not_isUnit a rw [← Set.union_eq_left.mpr this, ← quasispectrum_eq_spectrum_union] apply forall_congr' fun hr ↦ ?_ rw [not_iff_not, Units.smul_def, Units.smul_def, ← inr_smul, ← inr_neg, isQuasiregular_inr_iff] lemma quasispectrum_eq_spectrum_inr' (R S : Type*) {A : Type*} [Semifield R] [Field S] [NonUnitalRing A] [Algebra R S] [Module S A] [IsScalarTower S A A] [SMulCommClass S A A] [Module R A] [IsScalarTower R S A] (a : A) : quasispectrum R a = spectrum R (a : Unitization S A) := by ext r have := Set.singleton_subset_iff.mpr (zero_mem_spectrum_inr R S a) rw [← Set.union_eq_self_of_subset_right this, ← quasispectrum_eq_spectrum_union_zero] apply forall_congr' fun x ↦ ?_ rw [not_iff_not, Units.smul_def, Units.smul_def, ← inr_smul, ← inr_neg, isQuasiregular_inr_iff] end Unitization /-- A class for `𝕜`-algebras with a partial order where the ordering is compatible with the (quasi)spectrum. -/ class NonnegSpectrumClass (𝕜 A : Type*) [OrderedCommSemiring 𝕜] [NonUnitalRing A] [PartialOrder A] [Module 𝕜 A] : Prop where quasispectrum_nonneg_of_nonneg : ∀ a : A, 0 ≤ a → ∀ x ∈ quasispectrum 𝕜 a, 0 ≤ x export NonnegSpectrumClass (quasispectrum_nonneg_of_nonneg) namespace NonnegSpectrumClass lemma iff_spectrum_nonneg {𝕜 A : Type*} [LinearOrderedSemifield 𝕜] [Ring A] [PartialOrder A] [Algebra 𝕜 A] : NonnegSpectrumClass 𝕜 A ↔ ∀ a : A, 0 ≤ a → ∀ x ∈ spectrum 𝕜 a, 0 ≤ x := by simp [show NonnegSpectrumClass 𝕜 A ↔ _ from ⟨fun ⟨h⟩ ↦ h, (⟨·⟩)⟩, quasispectrum_eq_spectrum_union_zero] alias ⟨_, of_spectrum_nonneg⟩ := iff_spectrum_nonneg end NonnegSpectrumClass lemma spectrum_nonneg_of_nonneg {𝕜 A : Type*} [OrderedCommSemiring 𝕜] [Ring A] [PartialOrder A] [Algebra 𝕜 A] [NonnegSpectrumClass 𝕜 A] ⦃a : A⦄ (ha : 0 ≤ a) ⦃x : 𝕜⦄ (hx : x ∈ spectrum 𝕜 a) : 0 ≤ x := NonnegSpectrumClass.quasispectrum_nonneg_of_nonneg a ha x (spectrum_subset_quasispectrum 𝕜 a hx) /-! ### Restriction of the spectrum -/ /-- Given an element `a : A` of an `S`-algebra, where `S` is itself an `R`-algebra, we say that the spectrum of `a` restricts via a function `f : S → R` if `f` is a left inverse of `algebraMap R S`, and `f` is a right inverse of `algebraMap R S` on `spectrum S a`. For example, when `f = Complex.re` (so `S := ℂ` and `R := ℝ`), `SpectrumRestricts a f` means that the `ℂ`-spectrum of `a` is contained within `ℝ`. This arises naturally when `a` is selfadjoint and `A` is a C⋆-algebra. This is the property allows us to restrict a continuous functional calculus over `S` to a continuous functional calculus over `R`. -/ structure QuasispectrumRestricts {R S A : Type*} [CommSemiring R] [CommSemiring S] [NonUnitalRing A] [Module R A] [Module S A] [Algebra R S] (a : A) (f : S → R) : Prop where /-- `f` is a right inverse of `algebraMap R S` when restricted to `quasispectrum S a`. -/ rightInvOn : (quasispectrum S a).RightInvOn f (algebraMap R S) /-- `f` is a left inverse of `algebraMap R S`. -/ left_inv : Function.LeftInverse f (algebraMap R S) lemma quasispectrumRestricts_iff {R S A : Type*} [CommSemiring R] [CommSemiring S] [NonUnitalRing A] [Module R A] [Module S A] [Algebra R S] (a : A) (f : S → R) : QuasispectrumRestricts a f ↔ (quasispectrum S a).RightInvOn f (algebraMap R S) ∧ Function.LeftInverse f (algebraMap R S) := ⟨fun ⟨h₁, h₂⟩ ↦ ⟨h₁, h₂⟩, fun ⟨h₁, h₂⟩ ↦ ⟨h₁, h₂⟩⟩ @[simp] theorem quasispectrum.algebraMap_mem_iff (S : Type*) {R A : Type*} [Semifield R] [Field S] [NonUnitalRing A] [Algebra R S] [Module S A] [IsScalarTower S A A] [SMulCommClass S A A] [Module R A] [IsScalarTower R S A] {a : A} {r : R} : algebraMap R S r ∈ quasispectrum S a ↔ r ∈ quasispectrum R a := by simp_rw [Unitization.quasispectrum_eq_spectrum_inr' _ S a, spectrum.algebraMap_mem_iff] protected alias ⟨quasispectrum.of_algebraMap_mem, quasispectrum.algebraMap_mem⟩ := quasispectrum.algebraMap_mem_iff @[simp] theorem quasispectrum.preimage_algebraMap (S : Type*) {R A : Type*} [Semifield R] [Field S] [NonUnitalRing A] [Algebra R S] [Module S A] [IsScalarTower S A A] [SMulCommClass S A A] [Module R A] [IsScalarTower R S A] {a : A} : algebraMap R S ⁻¹' quasispectrum S a = quasispectrum R a := Set.ext fun _ => quasispectrum.algebraMap_mem_iff _ namespace QuasispectrumRestricts section NonUnital variable {R S A : Type*} [Semifield R] [Field S] [NonUnitalRing A] [Module R A] [Module S A] variable [Algebra R S] {a : A} {f : S → R} protected theorem map_zero (h : QuasispectrumRestricts a f) : f 0 = 0 := by rw [← h.left_inv 0, map_zero (algebraMap R S)] theorem of_subset_range_algebraMap (hf : f.LeftInverse (algebraMap R S)) (h : quasispectrum S a ⊆ Set.range (algebraMap R S)) : QuasispectrumRestricts a f where rightInvOn := fun s hs => by obtain ⟨r, rfl⟩ := h hs; rw [hf r] left_inv := hf lemma of_quasispectrum_eq {a b : A} {f : S → R} (ha : QuasispectrumRestricts a f) (h : quasispectrum S a = quasispectrum S b) : QuasispectrumRestricts b f where rightInvOn := h ▸ ha.rightInvOn left_inv := ha.left_inv variable [IsScalarTower S A A] [SMulCommClass S A A] [IsScalarTower R S A] theorem algebraMap_image (h : QuasispectrumRestricts a f) : algebraMap R S '' quasispectrum R a = quasispectrum S a := by refine Set.eq_of_subset_of_subset ?_ fun s hs => ⟨f s, ?_⟩ · simpa only [quasispectrum.preimage_algebraMap] using (quasispectrum S a).image_preimage_subset (algebraMap R S) exact ⟨quasispectrum.of_algebraMap_mem S ((h.rightInvOn hs).symm ▸ hs), h.rightInvOn hs⟩ theorem image (h : QuasispectrumRestricts a f) : f '' quasispectrum S a = quasispectrum R a := by simp only [← h.algebraMap_image, Set.image_image, h.left_inv _, Set.image_id'] theorem apply_mem (h : QuasispectrumRestricts a f) {s : S} (hs : s ∈ quasispectrum S a) : f s ∈ quasispectrum R a := h.image ▸ ⟨s, hs, rfl⟩ theorem subset_preimage (h : QuasispectrumRestricts a f) : quasispectrum S a ⊆ f ⁻¹' quasispectrum R a := h.image ▸ (quasispectrum S a).subset_preimage_image f protected lemma comp {R₁ R₂ R₃ A : Type*} [Semifield R₁] [Field R₂] [Field R₃] [NonUnitalRing A] [Module R₁ A] [Module R₂ A] [Module R₃ A] [Algebra R₁ R₂] [Algebra R₂ R₃] [Algebra R₁ R₃] [IsScalarTower R₁ R₂ R₃] [IsScalarTower R₂ R₃ A] [IsScalarTower R₃ A A] [SMulCommClass R₃ A A] {a : A} {f : R₃ → R₂} {g : R₂ → R₁} {e : R₃ → R₁} (hfge : g ∘ f = e) (hf : QuasispectrumRestricts a f) (hg : QuasispectrumRestricts a g) : QuasispectrumRestricts a e where left_inv := by convert hfge ▸ hf.left_inv.comp hg.left_inv congrm(⇑$(IsScalarTower.algebraMap_eq R₁ R₂ R₃)) rightInvOn := by convert hfge ▸ hg.rightInvOn.comp hf.rightInvOn fun _ ↦ hf.apply_mem congrm(⇑$(IsScalarTower.algebraMap_eq R₁ R₂ R₃)) end NonUnital end QuasispectrumRestricts /-- A (reducible) alias of `QuasispectrumRestricts` which enforces stronger type class assumptions on the types involved, as it's really intended for the `spectrum`. The separate definition also allows for dot notation. -/ @[reducible] def SpectrumRestricts {R S A : Type*} [Semifield R] [Semifield S] [Ring A] [Algebra R A] [Algebra S A] [Algebra R S] (a : A) (f : S → R) : Prop := QuasispectrumRestricts a f namespace SpectrumRestricts section Unital variable {R S A : Type*} [Semifield R] [Semifield S] [Ring A] variable [Algebra R S] [Algebra R A] [Algebra S A] {a : A} {f : S → R} theorem rightInvOn (h : SpectrumRestricts a f) : (spectrum S a).RightInvOn f (algebraMap R S) := (QuasispectrumRestricts.rightInvOn h).mono <| spectrum_subset_quasispectrum _ _ theorem of_rightInvOn (h₁ : Function.LeftInverse f (algebraMap R S)) (h₂ : (spectrum S a).RightInvOn f (algebraMap R S)) : SpectrumRestricts a f where rightInvOn x hx := by obtain (rfl | hx) := mem_quasispectrum_iff.mp hx · simpa using h₁ 0 · exact h₂ hx left_inv := h₁ lemma _root_.spectrumRestricts_iff : SpectrumRestricts a f ↔ (spectrum S a).RightInvOn f (algebraMap R S) ∧ Function.LeftInverse f (algebraMap R S) := ⟨fun h ↦ ⟨h.rightInvOn, h.left_inv⟩, fun h ↦ .of_rightInvOn h.2 h.1⟩ theorem of_subset_range_algebraMap (hf : f.LeftInverse (algebraMap R S)) (h : spectrum S a ⊆ Set.range (algebraMap R S)) : SpectrumRestricts a f where rightInvOn := fun s hs => by rw [mem_quasispectrum_iff] at hs obtain (rfl | hs) := hs · simpa using hf 0 · obtain ⟨r, rfl⟩ := h hs rw [hf r] left_inv := hf lemma of_spectrum_eq {a b : A} {f : S → R} (ha : SpectrumRestricts a f) (h : spectrum S a = spectrum S b) : SpectrumRestricts b f where rightInvOn := by rw [quasispectrum_eq_spectrum_union_zero, ← h, ← quasispectrum_eq_spectrum_union_zero] exact QuasispectrumRestricts.rightInvOn ha left_inv := ha.left_inv variable [IsScalarTower R S A] theorem algebraMap_image (h : SpectrumRestricts a f) : algebraMap R S '' spectrum R a = spectrum S a := by refine Set.eq_of_subset_of_subset ?_ fun s hs => ⟨f s, ?_⟩ · simpa only [spectrum.preimage_algebraMap] using (spectrum S a).image_preimage_subset (algebraMap R S) exact ⟨spectrum.of_algebraMap_mem S ((h.rightInvOn hs).symm ▸ hs), h.rightInvOn hs⟩ theorem image (h : SpectrumRestricts a f) : f '' spectrum S a = spectrum R a := by simp only [← h.algebraMap_image, Set.image_image, h.left_inv _, Set.image_id'] theorem apply_mem (h : SpectrumRestricts a f) {s : S} (hs : s ∈ spectrum S a) : f s ∈ spectrum R a := h.image ▸ ⟨s, hs, rfl⟩ theorem subset_preimage (h : SpectrumRestricts a f) : spectrum S a ⊆ f ⁻¹' spectrum R a := h.image ▸ (spectrum S a).subset_preimage_image f end Unital end SpectrumRestricts theorem quasispectrumRestricts_iff_spectrumRestricts_inr (S : Type*) {R A : Type*} [Semifield R] [Field S] [NonUnitalRing A] [Algebra R S] [Module R A] [Module S A] [IsScalarTower S A A] [SMulCommClass S A A] [IsScalarTower R S A] {a : A} {f : S → R} : QuasispectrumRestricts a f ↔ SpectrumRestricts (a : Unitization S A) f := by rw [quasispectrumRestricts_iff, spectrumRestricts_iff, ← Unitization.quasispectrum_eq_spectrum_inr'] theorem quasispectrumRestricts_iff_spectrumRestricts {R S A : Type*} [Semifield R] [Semifield S] [Ring A] [Algebra R S] [Algebra R A] [Algebra S A] {a : A} {f : S → R} : QuasispectrumRestricts a f ↔ SpectrumRestricts a f := by rw [quasispectrumRestricts_iff, spectrumRestricts_iff, quasispectrum_eq_spectrum_union_zero] refine and_congr_left fun h ↦ ?_ refine ⟨(Set.RightInvOn.mono · Set.subset_union_left), fun h' x hx ↦ ?_⟩ simp only [Set.union_singleton, Set.mem_insert_iff] at hx obtain (rfl | hx) := hx · simpa using h 0 · exact h' hx
Algebra\Algebra\Rat.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Data.Rat.Cast.CharZero import Mathlib.Algebra.Algebra.Defs /-! # Further basic results about `Algebra`'s over `ℚ`. This file could usefully be split further. -/ namespace RingHom variable {R S : Type*} -- Porting note: changed `[Ring R] [Ring S]` to `[Semiring R] [Semiring S]` -- otherwise, Lean failed to find a `Subsingleton (ℚ →+* S)` instance @[simp] theorem map_rat_algebraMap [Semiring R] [Semiring S] [Algebra ℚ R] [Algebra ℚ S] (f : R →+* S) (r : ℚ) : f (algebraMap ℚ R r) = algebraMap ℚ S r := RingHom.ext_iff.1 (Subsingleton.elim (f.comp (algebraMap ℚ R)) (algebraMap ℚ S)) r end RingHom section Rat /-- Every division ring of characteristic zero is an algebra over the rationals. -/ instance DivisionRing.toRatAlgebra {α} [DivisionRing α] [CharZero α] : Algebra ℚ α where smul := (· • ·) smul_def' := Rat.smul_def toRingHom := Rat.castHom α commutes' := Rat.cast_commute /-- The rational numbers are an algebra over the non-negative rationals. -/ instance : Algebra NNRat ℚ := NNRat.coeHom.toAlgebra /-- The two `Algebra ℚ ℚ` instances should coincide. -/ example : DivisionRing.toRatAlgebra = Algebra.id ℚ := rfl @[simp] theorem algebraMap_rat_rat : algebraMap ℚ ℚ = RingHom.id ℚ := rfl instance algebra_rat_subsingleton {α} [Semiring α] : Subsingleton (Algebra ℚ α) := ⟨fun x y => Algebra.algebra_ext x y <| RingHom.congr_fun <| Subsingleton.elim _ _⟩ end Rat
Algebra\Algebra\RestrictScalars.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Tower /-! # The `RestrictScalars` type alias See the documentation attached to the `RestrictScalars` definition for advice on how and when to use this type alias. As described there, it is often a better choice to use the `IsScalarTower` typeclass instead. ## Main definitions * `RestrictScalars R S M`: the `S`-module `M` viewed as an `R` module when `S` is an `R`-algebra. Note that by default we do *not* have a `Module S (RestrictScalars R S M)` instance for the original action. This is available as a def `RestrictScalars.moduleOrig` if really needed. * `RestrictScalars.addEquiv : RestrictScalars R S M ≃+ M`: the additive equivalence between the restricted and original space (in fact, they are definitionally equal, but sometimes it is helpful to avoid using this fact, to keep instances from leaking). * `RestrictScalars.ringEquiv : RestrictScalars R S A ≃+* A`: the ring equivalence between the restricted and original space when the module is an algebra. ## See also There are many similarly-named definitions elsewhere which do not refer to this type alias. These refer to restricting the scalar type in a bundled type, such as from `A →ₗ[R] B` to `A →ₗ[S] B`: * `LinearMap.restrictScalars` * `LinearEquiv.restrictScalars` * `AlgHom.restrictScalars` * `AlgEquiv.restrictScalars` * `Submodule.restrictScalars` * `Subalgebra.restrictScalars` -/ variable (R S M A : Type*) /-- If we put an `R`-algebra structure on a semiring `S`, we get a natural equivalence from the category of `S`-modules to the category of representations of the algebra `S` (over `R`). The type synonym `RestrictScalars` is essentially this equivalence. Warning: use this type synonym judiciously! Consider an example where we want to construct an `R`-linear map from `M` to `S`, given: ```lean variable (R S M : Type*) variable [CommSemiring R] [Semiring S] [Algebra R S] [AddCommMonoid M] [Module S M] ``` With the assumptions above we can't directly state our map as we have no `Module R M` structure, but `RestrictScalars` permits it to be written as: ```lean -- an `R`-module structure on `M` is provided by `RestrictScalars` which is compatible example : RestrictScalars R S M →ₗ[R] S := sorry ``` However, it is usually better just to add this extra structure as an argument: ```lean -- an `R`-module structure on `M` and proof of its compatibility is provided by the user example [Module R M] [IsScalarTower R S M] : M →ₗ[R] S := sorry ``` The advantage of the second approach is that it defers the duty of providing the missing typeclasses `[Module R M] [IsScalarTower R S M]`. If some concrete `M` naturally carries these (as is often the case) then we have avoided `RestrictScalars` entirely. If not, we can pass `RestrictScalars R S M` later on instead of `M`. Note that this means we almost always want to state definitions and lemmas in the language of `IsScalarTower` rather than `RestrictScalars`. An example of when one might want to use `RestrictScalars` would be if one has a vector space over a field of characteristic zero and wishes to make use of the `ℚ`-algebra structure. -/ @[nolint unusedArguments] def RestrictScalars (_R _S M : Type*) : Type _ := M instance [I : Inhabited M] : Inhabited (RestrictScalars R S M) := I instance [I : AddCommMonoid M] : AddCommMonoid (RestrictScalars R S M) := I instance [I : AddCommGroup M] : AddCommGroup (RestrictScalars R S M) := I section Module section variable [Semiring S] [AddCommMonoid M] /-- We temporarily install an action of the original ring on `RestrictScalars R S M`. -/ def RestrictScalars.moduleOrig [I : Module S M] : Module S (RestrictScalars R S M) := I variable [CommSemiring R] [Algebra R S] section attribute [local instance] RestrictScalars.moduleOrig /-- When `M` is a module over a ring `S`, and `S` is an algebra over `R`, then `M` inherits a module structure over `R`. The preferred way of setting this up is `[Module R M] [Module S M] [IsScalarTower R S M]`. -/ instance RestrictScalars.module [Module S M] : Module R (RestrictScalars R S M) := Module.compHom M (algebraMap R S) /-- This instance is only relevant when `RestrictScalars.moduleOrig` is available as an instance. -/ instance RestrictScalars.isScalarTower [Module S M] : IsScalarTower R S (RestrictScalars R S M) := ⟨fun r S M ↦ by rw [Algebra.smul_def, mul_smul] rfl⟩ end /-- When `M` is a right-module over a ring `S`, and `S` is an algebra over `R`, then `M` inherits a right-module structure over `R`. The preferred way of setting this up is `[Module Rᵐᵒᵖ M] [Module Sᵐᵒᵖ M] [IsScalarTower Rᵐᵒᵖ Sᵐᵒᵖ M]`. -/ instance RestrictScalars.opModule [Module Sᵐᵒᵖ M] : Module Rᵐᵒᵖ (RestrictScalars R S M) := letI : Module Sᵐᵒᵖ (RestrictScalars R S M) := ‹Module Sᵐᵒᵖ M› Module.compHom M (RingHom.op <| algebraMap R S) instance RestrictScalars.isCentralScalar [Module S M] [Module Sᵐᵒᵖ M] [IsCentralScalar S M] : IsCentralScalar R (RestrictScalars R S M) where op_smul_eq_smul r _x := (op_smul_eq_smul (algebraMap R S r) (_ : M) : _) /-- The `R`-algebra homomorphism from the original coefficient algebra `S` to endomorphisms of `RestrictScalars R S M`. -/ def RestrictScalars.lsmul [Module S M] : S →ₐ[R] Module.End R (RestrictScalars R S M) := -- We use `RestrictScalars.moduleOrig` in the implementation, -- but not in the type. letI : Module S (RestrictScalars R S M) := RestrictScalars.moduleOrig R S M Algebra.lsmul R R (RestrictScalars R S M) end variable [AddCommMonoid M] /-- `RestrictScalars.addEquiv` is the additive equivalence with the original module. -/ def RestrictScalars.addEquiv : RestrictScalars R S M ≃+ M := AddEquiv.refl M variable [CommSemiring R] [Semiring S] [Algebra R S] [Module S M] theorem RestrictScalars.smul_def (c : R) (x : RestrictScalars R S M) : c • x = (RestrictScalars.addEquiv R S M).symm (algebraMap R S c • RestrictScalars.addEquiv R S M x) := rfl @[simp] theorem RestrictScalars.addEquiv_map_smul (c : R) (x : RestrictScalars R S M) : RestrictScalars.addEquiv R S M (c • x) = algebraMap R S c • RestrictScalars.addEquiv R S M x := rfl theorem RestrictScalars.addEquiv_symm_map_algebraMap_smul (r : R) (x : M) : (RestrictScalars.addEquiv R S M).symm (algebraMap R S r • x) = r • (RestrictScalars.addEquiv R S M).symm x := rfl theorem RestrictScalars.addEquiv_symm_map_smul_smul (r : R) (s : S) (x : M) : (RestrictScalars.addEquiv R S M).symm ((r • s) • x) = r • (RestrictScalars.addEquiv R S M).symm (s • x) := by rw [Algebra.smul_def, mul_smul] rfl theorem RestrictScalars.lsmul_apply_apply (s : S) (x : RestrictScalars R S M) : RestrictScalars.lsmul R S M s x = (RestrictScalars.addEquiv R S M).symm (s • RestrictScalars.addEquiv R S M x) := rfl end Module section Algebra instance [I : Semiring A] : Semiring (RestrictScalars R S A) := I instance [I : Ring A] : Ring (RestrictScalars R S A) := I instance [I : CommSemiring A] : CommSemiring (RestrictScalars R S A) := I instance [I : CommRing A] : CommRing (RestrictScalars R S A) := I variable [Semiring A] /-- Tautological ring isomorphism `RestrictScalars R S A ≃+* A`. -/ def RestrictScalars.ringEquiv : RestrictScalars R S A ≃+* A := RingEquiv.refl _ variable [CommSemiring S] [Algebra S A] [CommSemiring R] [Algebra R S] @[simp] theorem RestrictScalars.ringEquiv_map_smul (r : R) (x : RestrictScalars R S A) : RestrictScalars.ringEquiv R S A (r • x) = algebraMap R S r • RestrictScalars.ringEquiv R S A x := rfl /-- `R ⟶ S` induces `S-Alg ⥤ R-Alg` -/ instance RestrictScalars.algebra : Algebra R (RestrictScalars R S A) := { (algebraMap S A).comp (algebraMap R S) with smul := (· • ·) commutes' := fun _ _ ↦ Algebra.commutes' (A := A) _ _ smul_def' := fun _ _ ↦ Algebra.smul_def' (A := A) _ _ } @[simp] theorem RestrictScalars.ringEquiv_algebraMap (r : R) : RestrictScalars.ringEquiv R S A (algebraMap R (RestrictScalars R S A) r) = algebraMap S A (algebraMap R S r) := rfl end Algebra
Algebra\Algebra\Spectrum.lean
/- Copyright (c) 2021 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Star.Subalgebra import Mathlib.RingTheory.Ideal.Maps import Mathlib.Tactic.NoncommRing /-! # Spectrum of an element in an algebra This file develops the basic theory of the spectrum of an element of an algebra. This theory will serve as the foundation for spectral theory in Banach algebras. ## Main definitions * `resolventSet a : Set R`: the resolvent set of an element `a : A` where `A` is an `R`-algebra. * `spectrum a : Set R`: the spectrum of an element `a : A` where `A` is an `R`-algebra. * `resolvent : R → A`: the resolvent function is `fun r ↦ Ring.inverse (↑ₐr - a)`, and hence when `r ∈ resolvent R A`, it is actually the inverse of the unit `(↑ₐr - a)`. ## Main statements * `spectrum.unit_smul_eq_smul` and `spectrum.smul_eq_smul`: units in the scalar ring commute (multiplication) with the spectrum, and over a field even `0` commutes with the spectrum. * `spectrum.left_add_coset_eq`: elements of the scalar ring commute (addition) with the spectrum. * `spectrum.unit_mem_mul_iff_mem_swap_mul` and `spectrum.preimage_units_mul_eq_swap_mul`: the units (of `R`) in `σ (a*b)` coincide with those in `σ (b*a)`. * `spectrum.scalar_eq`: in a nontrivial algebra over a field, the spectrum of a scalar is a singleton. ## Notations * `σ a` : `spectrum R a` of `a : A` -/ open Set open scoped Pointwise universe u v section Defs variable (R : Type u) {A : Type v} variable [CommSemiring R] [Ring A] [Algebra R A] local notation "↑ₐ" => algebraMap R A -- definition and basic properties /-- Given a commutative ring `R` and an `R`-algebra `A`, the *resolvent set* of `a : A` is the `Set R` consisting of those `r : R` for which `r•1 - a` is a unit of the algebra `A`. -/ def resolventSet (a : A) : Set R := {r : R | IsUnit (↑ₐ r - a)} /-- Given a commutative ring `R` and an `R`-algebra `A`, the *spectrum* of `a : A` is the `Set R` consisting of those `r : R` for which `r•1 - a` is not a unit of the algebra `A`. The spectrum is simply the complement of the resolvent set. -/ def spectrum (a : A) : Set R := (resolventSet R a)ᶜ variable {R} /-- Given an `a : A` where `A` is an `R`-algebra, the *resolvent* is a map `R → A` which sends `r : R` to `(algebraMap R A r - a)⁻¹` when `r ∈ resolvent R A` and `0` when `r ∈ spectrum R A`. -/ noncomputable def resolvent (a : A) (r : R) : A := Ring.inverse (↑ₐ r - a) /-- The unit `1 - r⁻¹ • a` constructed from `r • 1 - a` when the latter is a unit. -/ @[simps] noncomputable def IsUnit.subInvSMul {r : Rˣ} {s : R} {a : A} (h : IsUnit <| r • ↑ₐ s - a) : Aˣ where val := ↑ₐ s - r⁻¹ • a inv := r • ↑h.unit⁻¹ val_inv := by rw [mul_smul_comm, ← smul_mul_assoc, smul_sub, smul_inv_smul, h.mul_val_inv] inv_val := by rw [smul_mul_assoc, ← mul_smul_comm, smul_sub, smul_inv_smul, h.val_inv_mul] end Defs namespace spectrum section ScalarSemiring variable {R : Type u} {A : Type v} variable [CommSemiring R] [Ring A] [Algebra R A] local notation "σ" => spectrum R local notation "↑ₐ" => algebraMap R A theorem mem_iff {r : R} {a : A} : r ∈ σ a ↔ ¬IsUnit (↑ₐ r - a) := Iff.rfl theorem not_mem_iff {r : R} {a : A} : r ∉ σ a ↔ IsUnit (↑ₐ r - a) := by apply not_iff_not.mp simp [Set.not_not_mem, mem_iff] variable (R) theorem zero_mem_iff {a : A} : (0 : R) ∈ σ a ↔ ¬IsUnit a := by rw [mem_iff, map_zero, zero_sub, IsUnit.neg_iff] alias ⟨not_isUnit_of_zero_mem, zero_mem⟩ := spectrum.zero_mem_iff theorem zero_not_mem_iff {a : A} : (0 : R) ∉ σ a ↔ IsUnit a := by rw [zero_mem_iff, Classical.not_not] alias ⟨isUnit_of_zero_not_mem, zero_not_mem⟩ := spectrum.zero_not_mem_iff lemma subset_singleton_zero_compl {a : A} (ha : IsUnit a) : spectrum R a ⊆ {0}ᶜ := Set.subset_compl_singleton_iff.mpr <| spectrum.zero_not_mem R ha variable {R} theorem mem_resolventSet_of_left_right_inverse {r : R} {a b c : A} (h₁ : (↑ₐ r - a) * b = 1) (h₂ : c * (↑ₐ r - a) = 1) : r ∈ resolventSet R a := Units.isUnit ⟨↑ₐ r - a, b, h₁, by rwa [← left_inv_eq_right_inv h₂ h₁]⟩ theorem mem_resolventSet_iff {r : R} {a : A} : r ∈ resolventSet R a ↔ IsUnit (↑ₐ r - a) := Iff.rfl @[simp] theorem algebraMap_mem_iff (S : Type*) {R A : Type*} [CommSemiring R] [CommSemiring S] [Ring A] [Algebra R S] [Algebra R A] [Algebra S A] [IsScalarTower R S A] {a : A} {r : R} : algebraMap R S r ∈ spectrum S a ↔ r ∈ spectrum R a := by simp only [spectrum.mem_iff, Algebra.algebraMap_eq_smul_one, smul_assoc, one_smul] protected alias ⟨of_algebraMap_mem, algebraMap_mem⟩ := spectrum.algebraMap_mem_iff @[simp] theorem preimage_algebraMap (S : Type*) {R A : Type*} [CommSemiring R] [CommSemiring S] [Ring A] [Algebra R S] [Algebra R A] [Algebra S A] [IsScalarTower R S A] {a : A} : algebraMap R S ⁻¹' spectrum S a = spectrum R a := Set.ext fun _ => spectrum.algebraMap_mem_iff _ @[simp] theorem resolventSet_of_subsingleton [Subsingleton A] (a : A) : resolventSet R a = Set.univ := by simp_rw [resolventSet, Subsingleton.elim (algebraMap R A _ - a) 1, isUnit_one, Set.setOf_true] @[simp] theorem of_subsingleton [Subsingleton A] (a : A) : spectrum R a = ∅ := by rw [spectrum, resolventSet_of_subsingleton, Set.compl_univ] theorem resolvent_eq {a : A} {r : R} (h : r ∈ resolventSet R a) : resolvent a r = ↑h.unit⁻¹ := Ring.inverse_unit h.unit theorem units_smul_resolvent {r : Rˣ} {s : R} {a : A} : r • resolvent a (s : R) = resolvent (r⁻¹ • a) (r⁻¹ • s : R) := by by_cases h : s ∈ spectrum R a · rw [mem_iff] at h simp only [resolvent, Algebra.algebraMap_eq_smul_one] at * rw [smul_assoc, ← smul_sub] have h' : ¬IsUnit (r⁻¹ • (s • (1 : A) - a)) := fun hu => h (by simpa only [smul_inv_smul] using IsUnit.smul r hu) simp only [Ring.inverse_non_unit _ h, Ring.inverse_non_unit _ h', smul_zero] · simp only [resolvent] have h' : IsUnit (r • algebraMap R A (r⁻¹ • s) - a) := by simpa [Algebra.algebraMap_eq_smul_one, smul_assoc] using not_mem_iff.mp h rw [← h'.val_subInvSMul, ← (not_mem_iff.mp h).unit_spec, Ring.inverse_unit, Ring.inverse_unit, h'.val_inv_subInvSMul] simp only [Algebra.algebraMap_eq_smul_one, smul_assoc, smul_inv_smul] theorem units_smul_resolvent_self {r : Rˣ} {a : A} : r • resolvent a (r : R) = resolvent (r⁻¹ • a) (1 : R) := by simpa only [Units.smul_def, Algebra.id.smul_eq_mul, Units.inv_mul] using @units_smul_resolvent _ _ _ _ _ r r a /-- The resolvent is a unit when the argument is in the resolvent set. -/ theorem isUnit_resolvent {r : R} {a : A} : r ∈ resolventSet R a ↔ IsUnit (resolvent a r) := isUnit_ring_inverse.symm theorem inv_mem_resolventSet {r : Rˣ} {a : Aˣ} (h : (r : R) ∈ resolventSet R (a : A)) : (↑r⁻¹ : R) ∈ resolventSet R (↑a⁻¹ : A) := by rw [mem_resolventSet_iff, Algebra.algebraMap_eq_smul_one, ← Units.smul_def] at h ⊢ rw [IsUnit.smul_sub_iff_sub_inv_smul, inv_inv, IsUnit.sub_iff] have h₁ : (a : A) * (r • (↑a⁻¹ : A) - 1) = r • (1 : A) - a := by rw [mul_sub, mul_smul_comm, a.mul_inv, mul_one] have h₂ : (r • (↑a⁻¹ : A) - 1) * a = r • (1 : A) - a := by rw [sub_mul, smul_mul_assoc, a.inv_mul, one_mul] have hcomm : Commute (a : A) (r • (↑a⁻¹ : A) - 1) := by rwa [← h₂] at h₁ exact (hcomm.isUnit_mul_iff.mp (h₁.symm ▸ h)).2 theorem inv_mem_iff {r : Rˣ} {a : Aˣ} : (r : R) ∈ σ (a : A) ↔ (↑r⁻¹ : R) ∈ σ (↑a⁻¹ : A) := not_iff_not.2 <| ⟨inv_mem_resolventSet, inv_mem_resolventSet⟩ theorem zero_mem_resolventSet_of_unit (a : Aˣ) : 0 ∈ resolventSet R (a : A) := by simpa only [mem_resolventSet_iff, ← not_mem_iff, zero_not_mem_iff] using a.isUnit theorem ne_zero_of_mem_of_unit {a : Aˣ} {r : R} (hr : r ∈ σ (a : A)) : r ≠ 0 := fun hn => (hn ▸ hr) (zero_mem_resolventSet_of_unit a) theorem add_mem_iff {a : A} {r s : R} : r + s ∈ σ a ↔ r ∈ σ (-↑ₐ s + a) := by simp only [mem_iff, sub_neg_eq_add, ← sub_sub, map_add] theorem add_mem_add_iff {a : A} {r s : R} : r + s ∈ σ (↑ₐ s + a) ↔ r ∈ σ a := by rw [add_mem_iff, neg_add_cancel_left] theorem smul_mem_smul_iff {a : A} {s : R} {r : Rˣ} : r • s ∈ σ (r • a) ↔ s ∈ σ a := by simp only [mem_iff, not_iff_not, Algebra.algebraMap_eq_smul_one, smul_assoc, ← smul_sub, isUnit_smul_iff] theorem unit_smul_eq_smul (a : A) (r : Rˣ) : σ (r • a) = r • σ a := by ext x have x_eq : x = r • r⁻¹ • x := by simp nth_rw 1 [x_eq] rw [smul_mem_smul_iff] constructor · exact fun h => ⟨r⁻¹ • x, ⟨h, show r • r⁻¹ • x = x by simp⟩⟩ · rintro ⟨w, _, (x'_eq : r • w = x)⟩ simpa [← x'_eq ] -- `r ∈ σ(a*b) ↔ r ∈ σ(b*a)` for any `r : Rˣ` theorem unit_mem_mul_iff_mem_swap_mul {a b : A} {r : Rˣ} : ↑r ∈ σ (a * b) ↔ ↑r ∈ σ (b * a) := by have h₁ : ∀ x y : A, IsUnit (1 - x * y) → IsUnit (1 - y * x) := by refine fun x y h => ⟨⟨1 - y * x, 1 + y * h.unit.inv * x, ?_, ?_⟩, rfl⟩ · calc (1 - y * x) * (1 + y * (IsUnit.unit h).inv * x) = 1 - y * x + y * ((1 - x * y) * h.unit.inv) * x := by noncomm_ring _ = 1 := by simp only [Units.inv_eq_val_inv, IsUnit.mul_val_inv, mul_one, sub_add_cancel] · calc (1 + y * (IsUnit.unit h).inv * x) * (1 - y * x) = 1 - y * x + y * (h.unit.inv * (1 - x * y)) * x := by noncomm_ring _ = 1 := by simp only [Units.inv_eq_val_inv, IsUnit.val_inv_mul, mul_one, sub_add_cancel] have := Iff.intro (h₁ (r⁻¹ • a) b) (h₁ b (r⁻¹ • a)) rw [mul_smul_comm r⁻¹ b a] at this simpa only [mem_iff, not_iff_not, Algebra.algebraMap_eq_smul_one, ← Units.smul_def, IsUnit.smul_sub_iff_sub_inv_smul, smul_mul_assoc] theorem preimage_units_mul_eq_swap_mul {a b : A} : ((↑) : Rˣ → R) ⁻¹' σ (a * b) = (↑) ⁻¹' σ (b * a) := Set.ext fun _ => unit_mem_mul_iff_mem_swap_mul section Star variable [InvolutiveStar R] [StarRing A] [StarModule R A] theorem star_mem_resolventSet_iff {r : R} {a : A} : star r ∈ resolventSet R a ↔ r ∈ resolventSet R (star a) := by refine ⟨fun h => ?_, fun h => ?_⟩ <;> simpa only [mem_resolventSet_iff, Algebra.algebraMap_eq_smul_one, star_sub, star_smul, star_star, star_one] using IsUnit.star h protected theorem map_star (a : A) : σ (star a) = star (σ a) := by ext simpa only [Set.mem_star, mem_iff, not_iff_not] using star_mem_resolventSet_iff.symm end Star end ScalarSemiring section ScalarRing variable {R : Type u} {A : Type v} variable [CommRing R] [Ring A] [Algebra R A] local notation "σ" => spectrum R local notation "↑ₐ" => algebraMap R A theorem subset_subalgebra {S R A : Type*} [CommSemiring R] [Ring A] [Algebra R A] [SetLike S A] [SubringClass S A] [SMulMemClass S R A] {s : S} (a : s) : spectrum R (a : A) ⊆ spectrum R a := Set.compl_subset_compl.mpr fun _ ↦ IsUnit.map (SubalgebraClass.val s) @[deprecated subset_subalgebra (since := "2024-07-19")] theorem subset_starSubalgebra [StarRing R] [StarRing A] [StarModule R A] {S : StarSubalgebra R A} (a : S) : spectrum R (a : A) ⊆ spectrum R a := subset_subalgebra a theorem singleton_add_eq (a : A) (r : R) : {r} + σ a = σ (↑ₐ r + a) := ext fun x => by rw [singleton_add, image_add_left, mem_preimage, add_comm, add_mem_iff, map_neg, neg_neg] theorem add_singleton_eq (a : A) (r : R) : σ a + {r} = σ (a + ↑ₐ r) := add_comm {r} (σ a) ▸ add_comm (algebraMap R A r) a ▸ singleton_add_eq a r theorem vadd_eq (a : A) (r : R) : r +ᵥ σ a = σ (↑ₐ r + a) := singleton_add.symm.trans <| singleton_add_eq a r theorem neg_eq (a : A) : -σ a = σ (-a) := Set.ext fun x => by simp only [mem_neg, mem_iff, map_neg, ← neg_add', IsUnit.neg_iff, sub_neg_eq_add] theorem singleton_sub_eq (a : A) (r : R) : {r} - σ a = σ (↑ₐ r - a) := by rw [sub_eq_add_neg, neg_eq, singleton_add_eq, sub_eq_add_neg] theorem sub_singleton_eq (a : A) (r : R) : σ a - {r} = σ (a - ↑ₐ r) := by simpa only [neg_sub, neg_eq] using congr_arg Neg.neg (singleton_sub_eq a r) end ScalarRing section ScalarField variable {𝕜 : Type u} {A : Type v} variable [Field 𝕜] [Ring A] [Algebra 𝕜 A] local notation "σ" => spectrum 𝕜 local notation "↑ₐ" => algebraMap 𝕜 A /-- Without the assumption `Nontrivial A`, then `0 : A` would be invertible. -/ @[simp] theorem zero_eq [Nontrivial A] : σ (0 : A) = {0} := by refine Set.Subset.antisymm ?_ (by simp [Algebra.algebraMap_eq_smul_one, mem_iff]) rw [spectrum, Set.compl_subset_comm] intro k hk rw [Set.mem_compl_singleton_iff] at hk have : IsUnit (Units.mk0 k hk • (1 : A)) := IsUnit.smul (Units.mk0 k hk) isUnit_one simpa [mem_resolventSet_iff, Algebra.algebraMap_eq_smul_one] @[simp] theorem scalar_eq [Nontrivial A] (k : 𝕜) : σ (↑ₐ k) = {k} := by rw [← add_zero (↑ₐ k), ← singleton_add_eq, zero_eq, Set.singleton_add_singleton, add_zero] @[simp] theorem one_eq [Nontrivial A] : σ (1 : A) = {1} := calc σ (1 : A) = σ (↑ₐ 1) := by rw [Algebra.algebraMap_eq_smul_one, one_smul] _ = {1} := scalar_eq 1 /-- the assumption `(σ a).Nonempty` is necessary and cannot be removed without further conditions on the algebra `A` and scalar field `𝕜`. -/ theorem smul_eq_smul [Nontrivial A] (k : 𝕜) (a : A) (ha : (σ a).Nonempty) : σ (k • a) = k • σ a := by rcases eq_or_ne k 0 with (rfl | h) · simpa [ha, zero_smul_set] using (show {(0 : 𝕜)} = (0 : Set 𝕜) from rfl) · exact unit_smul_eq_smul a (Units.mk0 k h) theorem nonzero_mul_eq_swap_mul (a b : A) : σ (a * b) \ {0} = σ (b * a) \ {0} := by suffices h : ∀ x y : A, σ (x * y) \ {0} ⊆ σ (y * x) \ {0} from Set.eq_of_subset_of_subset (h a b) (h b a) rintro _ _ k ⟨k_mem, k_neq⟩ change ((Units.mk0 k k_neq) : 𝕜) ∈ _ at k_mem exact ⟨unit_mem_mul_iff_mem_swap_mul.mp k_mem, k_neq⟩ protected theorem map_inv (a : Aˣ) : (σ (a : A))⁻¹ = σ (↑a⁻¹ : A) := by refine Set.eq_of_subset_of_subset (fun k hk => ?_) fun k hk => ?_ · rw [Set.mem_inv] at hk have : k ≠ 0 := by simpa only [inv_inv] using inv_ne_zero (ne_zero_of_mem_of_unit hk) lift k to 𝕜ˣ using isUnit_iff_ne_zero.mpr this rw [← Units.val_inv_eq_inv_val k] at hk exact inv_mem_iff.mp hk · lift k to 𝕜ˣ using isUnit_iff_ne_zero.mpr (ne_zero_of_mem_of_unit hk) simpa only [Units.val_inv_eq_inv_val] using inv_mem_iff.mp hk end ScalarField end spectrum namespace AlgHom section CommSemiring variable {F R A B : Type*} [CommSemiring R] [Ring A] [Algebra R A] [Ring B] [Algebra R B] variable [FunLike F A B] [AlgHomClass F R A B] local notation "σ" => spectrum R local notation "↑ₐ" => algebraMap R A theorem mem_resolventSet_apply (φ : F) {a : A} {r : R} (h : r ∈ resolventSet R a) : r ∈ resolventSet R ((φ : A → B) a) := by simpa only [map_sub, AlgHomClass.commutes] using h.map φ theorem spectrum_apply_subset (φ : F) (a : A) : σ ((φ : A → B) a) ⊆ σ a := fun _ => mt (mem_resolventSet_apply φ) end CommSemiring section CommRing variable {F R A B : Type*} [CommRing R] [Ring A] [Algebra R A] [Ring B] [Algebra R B] variable [FunLike F A R] [AlgHomClass F R A R] local notation "σ" => spectrum R local notation "↑ₐ" => algebraMap R A theorem apply_mem_spectrum [Nontrivial R] (φ : F) (a : A) : φ a ∈ σ a := by have h : ↑ₐ (φ a) - a ∈ RingHom.ker (φ : A →+* R) := by simp only [RingHom.mem_ker, map_sub, RingHom.coe_coe, AlgHomClass.commutes, Algebra.id.map_eq_id, RingHom.id_apply, sub_self] simp only [spectrum.mem_iff, ← mem_nonunits_iff, coe_subset_nonunits (RingHom.ker_ne_top (φ : A →+* R)) h] end CommRing end AlgHom @[simp] theorem AlgEquiv.spectrum_eq {F R A B : Type*} [CommSemiring R] [Ring A] [Ring B] [Algebra R A] [Algebra R B] [EquivLike F A B] [AlgEquivClass F R A B] (f : F) (a : A) : spectrum R (f a) = spectrum R a := Set.Subset.antisymm (AlgHom.spectrum_apply_subset _ _) <| by simpa only [AlgEquiv.coe_algHom, AlgEquiv.coe_coe_symm_apply_coe_apply] using AlgHom.spectrum_apply_subset (f : A ≃ₐ[R] B).symm (f a) section ConjugateUnits variable {R A : Type*} [CommSemiring R] [Ring A] [Algebra R A] /-- Conjugation by a unit preserves the spectrum, inverse on right. -/ @[simp] lemma spectrum.units_conjugate {a : A} {u : Aˣ} : spectrum R (u * a * u⁻¹) = spectrum R a := by suffices ∀ (b : A) (v : Aˣ), spectrum R (v * b * v⁻¹) ⊆ spectrum R b by refine le_antisymm (this a u) ?_ apply le_of_eq_of_le ?_ <| this (u * a * u⁻¹) u⁻¹ simp [mul_assoc] intro a u μ hμ rw [spectrum.mem_iff] at hμ ⊢ contrapose! hμ simpa [mul_sub, sub_mul, Algebra.right_comm] using u.isUnit.mul hμ |>.mul u⁻¹.isUnit /-- Conjugation by a unit preserves the spectrum, inverse on left. -/ @[simp] lemma spectrum.units_conjugate' {a : A} {u : Aˣ} : spectrum R (u⁻¹ * a * u) = spectrum R a := by simpa using spectrum.units_conjugate (u := u⁻¹) end ConjugateUnits
Algebra\Algebra\Tower.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Anne Baanen -/ import Mathlib.Algebra.Algebra.Equiv import Mathlib.LinearAlgebra.Span /-! # Towers of algebras In this file we prove basic facts about towers of algebra. An algebra tower A/S/R is expressed by having instances of `Algebra A S`, `Algebra R S`, `Algebra R A` and `IsScalarTower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. An important definition is `toAlgHom R S A`, the canonical `R`-algebra homomorphism `S →ₐ[R] A`. -/ open Pointwise universe u v w u₁ v₁ variable (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace Algebra variable [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] variable [AddCommMonoid M] [Module R M] [Module A M] [Module B M] variable [IsScalarTower R A M] [IsScalarTower R B M] [SMulCommClass A B M] variable {A} /-- The `R`-algebra morphism `A → End (M)` corresponding to the representation of the algebra `A` on the `B`-module `M`. This is a stronger version of `DistribMulAction.toLinearMap`, and could also have been called `Algebra.toModuleEnd`. The typeclasses correspond to the situation where the types act on each other as ``` R ----→ B | ⟍ | | ⟍ | ↓ ↘ ↓ A ----→ M ``` where the diagram commutes, the action by `R` commutes with everything, and the action by `A` and `B` on `M` commute. Typically this is most useful with `B = R` as `Algebra.lsmul R R A : A →ₐ[R] Module.End R M`. However this can be used to get the fact that left-multiplication by `A` is right `A`-linear, and vice versa, as ```lean example : A →ₐ[R] Module.End Aᵐᵒᵖ A := Algebra.lsmul R Aᵐᵒᵖ A example : Aᵐᵒᵖ →ₐ[R] Module.End A A := Algebra.lsmul R A A ``` respectively; though `LinearMap.mulLeft` and `LinearMap.mulRight` can also be used here. -/ def lsmul : A →ₐ[R] Module.End B M where toFun := DistribMulAction.toLinearMap B M map_one' := LinearMap.ext fun _ => one_smul A _ map_mul' a b := LinearMap.ext <| smul_assoc a b map_zero' := LinearMap.ext fun _ => zero_smul A _ map_add' _a _b := LinearMap.ext fun _ => add_smul _ _ _ commutes' r := LinearMap.ext <| algebraMap_smul A r @[simp] theorem lsmul_coe (a : A) : (lsmul R B M a : M → M) = (a • ·) := rfl end Algebra namespace IsScalarTower section Module variable [CommSemiring R] [Semiring A] [Algebra R A] variable [MulAction A M] variable {R} {M} theorem algebraMap_smul [SMul R M] [IsScalarTower R A M] (r : R) (x : M) : algebraMap R A r • x = r • x := by rw [Algebra.algebraMap_eq_smul_one, smul_assoc, one_smul] variable {A} in theorem of_algebraMap_smul [SMul R M] (h : ∀ (r : R) (x : M), algebraMap R A r • x = r • x) : IsScalarTower R A M where smul_assoc r a x := by rw [Algebra.smul_def, mul_smul, h] variable (R M) in theorem of_compHom : letI := MulAction.compHom M (algebraMap R A : R →* A); IsScalarTower R A M := letI := MulAction.compHom M (algebraMap R A : R →* A); of_algebraMap_smul fun _ _ ↦ rfl end Module section Semiring variable [CommSemiring R] [CommSemiring S] [Semiring A] [Semiring B] variable [Algebra R S] [Algebra S A] [Algebra S B] variable {R S A} theorem of_algebraMap_eq [Algebra R A] (h : ∀ x, algebraMap R A x = algebraMap S A (algebraMap R S x)) : IsScalarTower R S A := ⟨fun x y z => by simp_rw [Algebra.smul_def, RingHom.map_mul, mul_assoc, h]⟩ /-- See note [partially-applied ext lemmas]. -/ theorem of_algebraMap_eq' [Algebra R A] (h : algebraMap R A = (algebraMap S A).comp (algebraMap R S)) : IsScalarTower R S A := of_algebraMap_eq <| RingHom.ext_iff.1 h variable (R S A) variable [Algebra R A] [Algebra R B] variable [IsScalarTower R S A] [IsScalarTower R S B] theorem algebraMap_eq : algebraMap R A = (algebraMap S A).comp (algebraMap R S) := RingHom.ext fun x => by simp_rw [RingHom.comp_apply, Algebra.algebraMap_eq_smul_one, smul_assoc, one_smul] theorem algebraMap_apply (x : R) : algebraMap R A x = algebraMap S A (algebraMap R S x) := by rw [algebraMap_eq R S A, RingHom.comp_apply] @[ext] theorem Algebra.ext {S : Type u} {A : Type v} [CommSemiring S] [Semiring A] (h1 h2 : Algebra S A) (h : ∀ (r : S) (x : A), (by have I := h1; exact r • x) = r • x) : h1 = h2 := Algebra.algebra_ext _ _ fun r => by simpa only [@Algebra.smul_def _ _ _ _ h1, @Algebra.smul_def _ _ _ _ h2, mul_one] using h r 1 /-- In a tower, the canonical map from the middle element to the top element is an algebra homomorphism over the bottom element. -/ def toAlgHom : S →ₐ[R] A := { algebraMap S A with commutes' := fun _ => (algebraMap_apply _ _ _ _).symm } theorem toAlgHom_apply (y : S) : toAlgHom R S A y = algebraMap S A y := rfl @[simp] theorem coe_toAlgHom : ↑(toAlgHom R S A) = algebraMap S A := RingHom.ext fun _ => rfl @[simp] theorem coe_toAlgHom' : (toAlgHom R S A : S → A) = algebraMap S A := rfl variable {R S A B} @[simp] theorem _root_.AlgHom.map_algebraMap (f : A →ₐ[S] B) (r : R) : f (algebraMap R A r) = algebraMap R B r := by rw [algebraMap_apply R S A r, f.commutes, ← algebraMap_apply R S B] variable (R) @[simp] theorem _root_.AlgHom.comp_algebraMap_of_tower (f : A →ₐ[S] B) : (f : A →+* B).comp (algebraMap R A) = algebraMap R B := RingHom.ext (AlgHom.map_algebraMap f) -- conflicts with IsScalarTower.Subalgebra instance (priority := 999) subsemiring (U : Subsemiring S) : IsScalarTower U S A := of_algebraMap_eq fun _x => rfl -- Porting note(#12096): removed @[nolint instance_priority], linter not ported yet instance (priority := 999) of_algHom {R A B : Type*} [CommSemiring R] [CommSemiring A] [CommSemiring B] [Algebra R A] [Algebra R B] (f : A →ₐ[R] B) : @IsScalarTower R A B _ f.toRingHom.toAlgebra.toSMul _ := letI := (f : A →+* B).toAlgebra of_algebraMap_eq fun x => (f.commutes x).symm end Semiring end IsScalarTower section Homs variable [CommSemiring R] [CommSemiring S] [Semiring A] [Semiring B] variable [Algebra R S] [Algebra S A] [Algebra S B] variable [Algebra R A] [Algebra R B] variable [IsScalarTower R S A] [IsScalarTower R S B] variable {A S B} open IsScalarTower namespace AlgHom /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrictScalars (f : A →ₐ[S] B) : A →ₐ[R] B := { (f : A →+* B) with commutes' := fun r => by rw [algebraMap_apply R S A, algebraMap_apply R S B] exact f.commutes (algebraMap R S r) } theorem restrictScalars_apply (f : A →ₐ[S] B) (x : A) : f.restrictScalars R x = f x := rfl @[simp] theorem coe_restrictScalars (f : A →ₐ[S] B) : (f.restrictScalars R : A →+* B) = f := rfl @[simp] theorem coe_restrictScalars' (f : A →ₐ[S] B) : (restrictScalars R f : A → B) = f := rfl theorem restrictScalars_injective : Function.Injective (restrictScalars R : (A →ₐ[S] B) → A →ₐ[R] B) := fun _ _ h => AlgHom.ext (AlgHom.congr_fun h : _) end AlgHom namespace AlgEquiv /-- R ⟶ S induces S-Alg ⥤ R-Alg -/ def restrictScalars (f : A ≃ₐ[S] B) : A ≃ₐ[R] B := { (f : A ≃+* B) with commutes' := fun r => by rw [algebraMap_apply R S A, algebraMap_apply R S B] exact f.commutes (algebraMap R S r) } theorem restrictScalars_apply (f : A ≃ₐ[S] B) (x : A) : f.restrictScalars R x = f x := rfl @[simp] theorem coe_restrictScalars (f : A ≃ₐ[S] B) : (f.restrictScalars R : A ≃+* B) = f := rfl @[simp] theorem coe_restrictScalars' (f : A ≃ₐ[S] B) : (restrictScalars R f : A → B) = f := rfl theorem restrictScalars_injective : Function.Injective (restrictScalars R : (A ≃ₐ[S] B) → A ≃ₐ[R] B) := fun _ _ h => AlgEquiv.ext (AlgEquiv.congr_fun h : _) end AlgEquiv end Homs namespace Submodule variable {M} variable [CommSemiring R] [Semiring A] [Algebra R A] [AddCommMonoid M] variable [Module R M] [Module A M] [IsScalarTower R A M] /-- If `A` is an `R`-algebra such that the induced morphism `R →+* A` is surjective, then the `R`-module generated by a set `X` equals the `A`-module generated by `X`. -/ theorem restrictScalars_span (hsur : Function.Surjective (algebraMap R A)) (X : Set M) : restrictScalars R (span A X) = span R X := by refine ((span_le_restrictScalars R A X).antisymm fun m hm => ?_).symm refine span_induction hm subset_span (zero_mem _) (fun _ _ => add_mem) fun a m hm => ?_ obtain ⟨r, rfl⟩ := hsur a simpa [algebraMap_smul] using smul_mem _ r hm theorem coe_span_eq_span_of_surjective (h : Function.Surjective (algebraMap R A)) (s : Set M) : (Submodule.span A s : Set M) = Submodule.span R s := congr_arg ((↑) : Submodule R M → Set M) (Submodule.restrictScalars_span R A h s) end Submodule section Semiring variable {R S A} namespace Submodule section Module variable [Semiring R] [Semiring S] [AddCommMonoid A] variable [Module R S] [Module S A] [Module R A] [IsScalarTower R S A] open IsScalarTower theorem smul_mem_span_smul_of_mem {s : Set S} {t : Set A} {k : S} (hks : k ∈ span R s) {x : A} (hx : x ∈ t) : k • x ∈ span R (s • t) := span_induction hks (fun c hc => subset_span <| Set.smul_mem_smul hc hx) (by rw [zero_smul]; exact zero_mem _) (fun c₁ c₂ ih₁ ih₂ => by rw [add_smul]; exact add_mem ih₁ ih₂) fun b c hc => by rw [IsScalarTower.smul_assoc]; exact smul_mem _ _ hc theorem span_smul_of_span_eq_top {s : Set S} (hs : span R s = ⊤) (t : Set A) : span R (s • t) = (span S t).restrictScalars R := le_antisymm (span_le.2 fun _x ⟨p, _hps, _q, hqt, hpqx⟩ ↦ hpqx ▸ (span S t).smul_mem p (subset_span hqt)) fun p hp ↦ closure_induction hp (zero_mem _) (fun _ _ ↦ add_mem) fun s0 y hy ↦ by refine span_induction (hs ▸ mem_top : s0 ∈ span R s) (fun x hx ↦ subset_span ⟨x, hx, y, hy, rfl⟩) ?_ ?_ ?_ · rw [zero_smul]; apply zero_mem · intro _ _; rw [add_smul]; apply add_mem · intro r s0 hy; rw [IsScalarTower.smul_assoc]; exact smul_mem _ r hy -- The following two lemmas were originally used to prove `span_smul_of_span_eq_top` -- but are now not needed. theorem smul_mem_span_smul' {s : Set S} (hs : span R s = ⊤) {t : Set A} {k : S} {x : A} (hx : x ∈ span R (s • t)) : k • x ∈ span R (s • t) := by rw [span_smul_of_span_eq_top hs] at hx ⊢; exact (span S t).smul_mem k hx theorem smul_mem_span_smul {s : Set S} (hs : span R s = ⊤) {t : Set A} {k : S} {x : A} (hx : x ∈ span R t) : k • x ∈ span R (s • t) := by rw [span_smul_of_span_eq_top hs] exact (span S t).smul_mem k (span_le_restrictScalars R S t hx) end Module section Algebra variable [CommSemiring R] [Semiring S] [AddCommMonoid A] variable [Algebra R S] [Module S A] [Module R A] [IsScalarTower R S A] /-- A variant of `Submodule.span_image` for `algebraMap`. -/ theorem span_algebraMap_image (a : Set R) : Submodule.span R (algebraMap R S '' a) = (Submodule.span R a).map (Algebra.linearMap R S) := (Submodule.span_image <| Algebra.linearMap R S).trans rfl theorem span_algebraMap_image_of_tower {S T : Type*} [CommSemiring S] [Semiring T] [Module R S] [Algebra R T] [Algebra S T] [IsScalarTower R S T] (a : Set S) : Submodule.span R (algebraMap S T '' a) = (Submodule.span R a).map ((Algebra.linearMap S T).restrictScalars R) := (Submodule.span_image <| (Algebra.linearMap S T).restrictScalars R).trans rfl theorem map_mem_span_algebraMap_image {S T : Type*} [CommSemiring S] [Semiring T] [Algebra R S] [Algebra R T] [Algebra S T] [IsScalarTower R S T] (x : S) (a : Set S) (hx : x ∈ Submodule.span R a) : algebraMap S T x ∈ Submodule.span R (algebraMap S T '' a) := by rw [span_algebraMap_image_of_tower, mem_map] exact ⟨x, hx, rfl⟩ end Algebra end Submodule end Semiring section Ring namespace Algebra variable [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] variable [AddCommGroup M] [Module R M] [Module A M] [Module B M] variable [IsScalarTower R A M] [IsScalarTower R B M] [SMulCommClass A B M] theorem lsmul_injective [NoZeroSMulDivisors A M] {x : A} (hx : x ≠ 0) : Function.Injective (lsmul R B M x) := smul_right_injective M hx end Algebra end Ring
Algebra\Algebra\Unitization.lean
/- Copyright (c) 2022 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Algebra.Defs import Mathlib.Algebra.Algebra.NonUnitalHom import Mathlib.Algebra.Star.Module import Mathlib.Algebra.Star.NonUnitalSubalgebra import Mathlib.LinearAlgebra.Prod import Mathlib.Tactic.Abel /-! # Unitization of a non-unital algebra Given a non-unital `R`-algebra `A` (given via the type classes `[NonUnitalRing A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A]`) we construct the minimal unital `R`-algebra containing `A` as an ideal. This object `Unitization R A` is a type synonym for `R × A` on which we place a different multiplicative structure, namely, `(r₁, a₁) * (r₂, a₂) = (r₁ * r₂, r₁ • a₂ + r₂ • a₁ + a₁ * a₂)` where the multiplicative identity is `(1, 0)`. Note, when `A` is a *unital* `R`-algebra, then `Unitization R A` constructs a new multiplicative identity different from the old one, and so in general `Unitization R A` and `A` will not be isomorphic even in the unital case. This approach actually has nice functorial properties. There is a natural coercion from `A` to `Unitization R A` given by `fun a ↦ (0, a)`, the image of which is a proper ideal (TODO), and when `R` is a field this ideal is maximal. Moreover, this ideal is always an essential ideal (it has nontrivial intersection with every other nontrivial ideal). Every non-unital algebra homomorphism from `A` into a *unital* `R`-algebra `B` has a unique extension to a (unital) algebra homomorphism from `Unitization R A` to `B`. ## Main definitions * `Unitization R A`: the unitization of a non-unital `R`-algebra `A`. * `Unitization.algebra`: the unitization of `A` as a (unital) `R`-algebra. * `Unitization.coeNonUnitalAlgHom`: coercion as a non-unital algebra homomorphism. * `NonUnitalAlgHom.toAlgHom φ`: the extension of a non-unital algebra homomorphism `φ : A → B` into a unital `R`-algebra `B` to an algebra homomorphism `Unitization R A →ₐ[R] B`. * `Unitization.lift`: the universal property of the unitization, the extension `NonUnitalAlgHom.toAlgHom` actually implements an equivalence `(A →ₙₐ[R] B) ≃ (Unitization R A ≃ₐ[R] B)` ## Main results * `AlgHom.ext'`: an extensionality lemma for algebra homomorphisms whose domain is `Unitization R A`; it suffices that they agree on `A`. ## TODO * prove the unitization operation is a functor between the appropriate categories * prove the image of the coercion is an essential ideal, maximal if scalars are a field. -/ /-- The minimal unitization of a non-unital `R`-algebra `A`. This is just a type synonym for `R × A`. -/ def Unitization (R A : Type*) := R × A namespace Unitization section Basic variable {R A : Type*} /-- The canonical inclusion `R → Unitization R A`. -/ def inl [Zero A] (r : R) : Unitization R A := (r, 0) -- Porting note: we need a def to which we can attach `@[coe]` /-- The canonical inclusion `A → Unitization R A`. -/ @[coe] def inr [Zero R] (a : A) : Unitization R A := (0, a) instance [Zero R] : CoeTC A (Unitization R A) where coe := inr /-- The canonical projection `Unitization R A → R`. -/ def fst (x : Unitization R A) : R := x.1 /-- The canonical projection `Unitization R A → A`. -/ def snd (x : Unitization R A) : A := x.2 @[ext] theorem ext {x y : Unitization R A} (h1 : x.fst = y.fst) (h2 : x.snd = y.snd) : x = y := Prod.ext h1 h2 section variable (A) @[simp] theorem fst_inl [Zero A] (r : R) : (inl r : Unitization R A).fst = r := rfl @[simp] theorem snd_inl [Zero A] (r : R) : (inl r : Unitization R A).snd = 0 := rfl end section variable (R) @[simp] theorem fst_inr [Zero R] (a : A) : (a : Unitization R A).fst = 0 := rfl @[simp] theorem snd_inr [Zero R] (a : A) : (a : Unitization R A).snd = a := rfl end theorem inl_injective [Zero A] : Function.Injective (inl : R → Unitization R A) := Function.LeftInverse.injective <| fst_inl _ theorem inr_injective [Zero R] : Function.Injective ((↑) : A → Unitization R A) := Function.LeftInverse.injective <| snd_inr _ instance instNontrivialLeft {𝕜 A} [Nontrivial 𝕜] [Nonempty A] : Nontrivial (Unitization 𝕜 A) := nontrivial_prod_left instance instNontrivialRight {𝕜 A} [Nonempty 𝕜] [Nontrivial A] : Nontrivial (Unitization 𝕜 A) := nontrivial_prod_right end Basic /-! ### Structures inherited from `Prod` Additive operators and scalar multiplication operate elementwise. -/ section Additive variable {T : Type*} {S : Type*} {R : Type*} {A : Type*} instance instCanLift [Zero R] : CanLift (Unitization R A) A inr (fun x ↦ x.fst = 0) where prf x hx := ⟨x.snd, ext (hx ▸ fst_inr R (snd x)) rfl⟩ instance instInhabited [Inhabited R] [Inhabited A] : Inhabited (Unitization R A) := instInhabitedProd instance instZero [Zero R] [Zero A] : Zero (Unitization R A) := Prod.instZero instance instAdd [Add R] [Add A] : Add (Unitization R A) := Prod.instAdd instance instNeg [Neg R] [Neg A] : Neg (Unitization R A) := Prod.instNeg instance instAddSemigroup [AddSemigroup R] [AddSemigroup A] : AddSemigroup (Unitization R A) := Prod.instAddSemigroup instance instAddZeroClass [AddZeroClass R] [AddZeroClass A] : AddZeroClass (Unitization R A) := Prod.instAddZeroClass instance instAddMonoid [AddMonoid R] [AddMonoid A] : AddMonoid (Unitization R A) := Prod.instAddMonoid instance instAddGroup [AddGroup R] [AddGroup A] : AddGroup (Unitization R A) := Prod.instAddGroup instance instAddCommSemigroup [AddCommSemigroup R] [AddCommSemigroup A] : AddCommSemigroup (Unitization R A) := Prod.instAddCommSemigroup instance instAddCommMonoid [AddCommMonoid R] [AddCommMonoid A] : AddCommMonoid (Unitization R A) := Prod.instAddCommMonoid instance instAddCommGroup [AddCommGroup R] [AddCommGroup A] : AddCommGroup (Unitization R A) := Prod.instAddCommGroup instance instSMul [SMul S R] [SMul S A] : SMul S (Unitization R A) := Prod.smul instance instIsScalarTower [SMul T R] [SMul T A] [SMul S R] [SMul S A] [SMul T S] [IsScalarTower T S R] [IsScalarTower T S A] : IsScalarTower T S (Unitization R A) := Prod.isScalarTower instance instSMulCommClass [SMul T R] [SMul T A] [SMul S R] [SMul S A] [SMulCommClass T S R] [SMulCommClass T S A] : SMulCommClass T S (Unitization R A) := Prod.smulCommClass instance instIsCentralScalar [SMul S R] [SMul S A] [SMul Sᵐᵒᵖ R] [SMul Sᵐᵒᵖ A] [IsCentralScalar S R] [IsCentralScalar S A] : IsCentralScalar S (Unitization R A) := Prod.isCentralScalar instance instMulAction [Monoid S] [MulAction S R] [MulAction S A] : MulAction S (Unitization R A) := Prod.mulAction instance instDistribMulAction [Monoid S] [AddMonoid R] [AddMonoid A] [DistribMulAction S R] [DistribMulAction S A] : DistribMulAction S (Unitization R A) := Prod.distribMulAction instance instModule [Semiring S] [AddCommMonoid R] [AddCommMonoid A] [Module S R] [Module S A] : Module S (Unitization R A) := Prod.instModule variable (R A) in /-- The identity map between `Unitization R A` and `R × A` as an `AddEquiv`. -/ def addEquiv [Add R] [Add A] : Unitization R A ≃+ R × A := AddEquiv.refl _ @[simp] theorem fst_zero [Zero R] [Zero A] : (0 : Unitization R A).fst = 0 := rfl @[simp] theorem snd_zero [Zero R] [Zero A] : (0 : Unitization R A).snd = 0 := rfl @[simp] theorem fst_add [Add R] [Add A] (x₁ x₂ : Unitization R A) : (x₁ + x₂).fst = x₁.fst + x₂.fst := rfl @[simp] theorem snd_add [Add R] [Add A] (x₁ x₂ : Unitization R A) : (x₁ + x₂).snd = x₁.snd + x₂.snd := rfl @[simp] theorem fst_neg [Neg R] [Neg A] (x : Unitization R A) : (-x).fst = -x.fst := rfl @[simp] theorem snd_neg [Neg R] [Neg A] (x : Unitization R A) : (-x).snd = -x.snd := rfl @[simp] theorem fst_smul [SMul S R] [SMul S A] (s : S) (x : Unitization R A) : (s • x).fst = s • x.fst := rfl @[simp] theorem snd_smul [SMul S R] [SMul S A] (s : S) (x : Unitization R A) : (s • x).snd = s • x.snd := rfl section variable (A) @[simp] theorem inl_zero [Zero R] [Zero A] : (inl 0 : Unitization R A) = 0 := rfl @[simp] theorem inl_add [Add R] [AddZeroClass A] (r₁ r₂ : R) : (inl (r₁ + r₂) : Unitization R A) = inl r₁ + inl r₂ := ext rfl (add_zero 0).symm @[simp] theorem inl_neg [Neg R] [AddGroup A] (r : R) : (inl (-r) : Unitization R A) = -inl r := ext rfl neg_zero.symm @[simp] theorem inl_sub [AddGroup R] [AddGroup A] (r₁ r₂ : R) : (inl (r₁ - r₂) : Unitization R A) = inl r₁ - inl r₂ := ext rfl (sub_zero 0).symm @[simp] theorem inl_smul [Monoid S] [AddMonoid A] [SMul S R] [DistribMulAction S A] (s : S) (r : R) : (inl (s • r) : Unitization R A) = s • inl r := ext rfl (smul_zero s).symm end section variable (R) @[simp] theorem inr_zero [Zero R] [Zero A] : ↑(0 : A) = (0 : Unitization R A) := rfl @[simp] theorem inr_add [AddZeroClass R] [Add A] (m₁ m₂ : A) : (↑(m₁ + m₂) : Unitization R A) = m₁ + m₂ := ext (add_zero 0).symm rfl @[simp] theorem inr_neg [AddGroup R] [Neg A] (m : A) : (↑(-m) : Unitization R A) = -m := ext neg_zero.symm rfl @[simp] theorem inr_sub [AddGroup R] [AddGroup A] (m₁ m₂ : A) : (↑(m₁ - m₂) : Unitization R A) = m₁ - m₂ := ext (sub_zero 0).symm rfl @[simp] theorem inr_smul [Zero R] [Zero S] [SMulWithZero S R] [SMul S A] (r : S) (m : A) : (↑(r • m) : Unitization R A) = r • (m : Unitization R A) := ext (smul_zero _).symm rfl end theorem inl_fst_add_inr_snd_eq [AddZeroClass R] [AddZeroClass A] (x : Unitization R A) : inl x.fst + (x.snd : Unitization R A) = x := ext (add_zero x.1) (zero_add x.2) /-- To show a property hold on all `Unitization R A` it suffices to show it holds on terms of the form `inl r + a`. This can be used as `induction x`. -/ @[elab_as_elim, induction_eliminator, cases_eliminator] theorem ind {R A} [AddZeroClass R] [AddZeroClass A] {P : Unitization R A → Prop} (inl_add_inr : ∀ (r : R) (a : A), P (inl r + (a : Unitization R A))) (x) : P x := inl_fst_add_inr_snd_eq x ▸ inl_add_inr x.1 x.2 /-- This cannot be marked `@[ext]` as it ends up being used instead of `LinearMap.prod_ext` when working with `R × A`. -/ theorem linearMap_ext {N} [Semiring S] [AddCommMonoid R] [AddCommMonoid A] [AddCommMonoid N] [Module S R] [Module S A] [Module S N] ⦃f g : Unitization R A →ₗ[S] N⦄ (hl : ∀ r, f (inl r) = g (inl r)) (hr : ∀ a : A, f a = g a) : f = g := LinearMap.prod_ext (LinearMap.ext hl) (LinearMap.ext hr) variable (R A) /-- The canonical `R`-linear inclusion `A → Unitization R A`. -/ @[simps apply] def inrHom [Semiring R] [AddCommMonoid A] [Module R A] : A →ₗ[R] Unitization R A := { LinearMap.inr R R A with toFun := (↑) } /-- The canonical `R`-linear projection `Unitization R A → A`. -/ @[simps apply] def sndHom [Semiring R] [AddCommMonoid A] [Module R A] : Unitization R A →ₗ[R] A := { LinearMap.snd _ _ _ with toFun := snd } end Additive /-! ### Multiplicative structure -/ section Mul variable {R A : Type*} instance instOne [One R] [Zero A] : One (Unitization R A) := ⟨(1, 0)⟩ instance instMul [Mul R] [Add A] [Mul A] [SMul R A] : Mul (Unitization R A) := ⟨fun x y => (x.1 * y.1, x.1 • y.2 + y.1 • x.2 + x.2 * y.2)⟩ @[simp] theorem fst_one [One R] [Zero A] : (1 : Unitization R A).fst = 1 := rfl @[simp] theorem snd_one [One R] [Zero A] : (1 : Unitization R A).snd = 0 := rfl @[simp] theorem fst_mul [Mul R] [Add A] [Mul A] [SMul R A] (x₁ x₂ : Unitization R A) : (x₁ * x₂).fst = x₁.fst * x₂.fst := rfl @[simp] theorem snd_mul [Mul R] [Add A] [Mul A] [SMul R A] (x₁ x₂ : Unitization R A) : (x₁ * x₂).snd = x₁.fst • x₂.snd + x₂.fst • x₁.snd + x₁.snd * x₂.snd := rfl section variable (A) @[simp] theorem inl_one [One R] [Zero A] : (inl 1 : Unitization R A) = 1 := rfl @[simp] theorem inl_mul [Monoid R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] (r₁ r₂ : R) : (inl (r₁ * r₂) : Unitization R A) = inl r₁ * inl r₂ := ext rfl <| show (0 : A) = r₁ • (0 : A) + r₂ • (0 : A) + 0 * 0 by simp only [smul_zero, add_zero, mul_zero] theorem inl_mul_inl [Monoid R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] (r₁ r₂ : R) : (inl r₁ * inl r₂ : Unitization R A) = inl (r₁ * r₂) := (inl_mul A r₁ r₂).symm end section variable (R) @[simp] theorem inr_mul [Semiring R] [AddCommMonoid A] [Mul A] [SMulWithZero R A] (a₁ a₂ : A) : (↑(a₁ * a₂) : Unitization R A) = a₁ * a₂ := ext (mul_zero _).symm <| show a₁ * a₂ = (0 : R) • a₂ + (0 : R) • a₁ + a₁ * a₂ by simp only [zero_smul, zero_add] end theorem inl_mul_inr [Semiring R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] (r : R) (a : A) : ((inl r : Unitization R A) * a) = ↑(r • a) := ext (mul_zero r) <| show r • a + (0 : R) • (0 : A) + 0 * a = r • a by rw [smul_zero, add_zero, zero_mul, add_zero] theorem inr_mul_inl [Semiring R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] (r : R) (a : A) : a * (inl r : Unitization R A) = ↑(r • a) := ext (zero_mul r) <| show (0 : R) • (0 : A) + r • a + a * 0 = r • a by rw [smul_zero, zero_add, mul_zero, add_zero] instance instMulOneClass [Monoid R] [NonUnitalNonAssocSemiring A] [DistribMulAction R A] : MulOneClass (Unitization R A) := { Unitization.instOne, Unitization.instMul with one_mul := fun x => ext (one_mul x.1) <| show (1 : R) • x.2 + x.1 • (0 : A) + 0 * x.2 = x.2 by rw [one_smul, smul_zero, add_zero, zero_mul, add_zero] mul_one := fun x => ext (mul_one x.1) <| show (x.1 • (0 : A)) + (1 : R) • x.2 + x.2 * (0 : A) = x.2 by rw [smul_zero, zero_add, one_smul, mul_zero, add_zero] } instance instNonAssocSemiring [Semiring R] [NonUnitalNonAssocSemiring A] [Module R A] : NonAssocSemiring (Unitization R A) := { Unitization.instMulOneClass, Unitization.instAddCommMonoid with zero_mul := fun x => ext (zero_mul x.1) <| show (0 : R) • x.2 + x.1 • (0 : A) + 0 * x.2 = 0 by rw [zero_smul, zero_add, smul_zero, zero_mul, add_zero] mul_zero := fun x => ext (mul_zero x.1) <| show x.1 • (0 : A) + (0 : R) • x.2 + x.2 * 0 = 0 by rw [smul_zero, zero_add, zero_smul, mul_zero, add_zero] left_distrib := fun x₁ x₂ x₃ => ext (mul_add x₁.1 x₂.1 x₃.1) <| show x₁.1 • (x₂.2 + x₃.2) + (x₂.1 + x₃.1) • x₁.2 + x₁.2 * (x₂.2 + x₃.2) = x₁.1 • x₂.2 + x₂.1 • x₁.2 + x₁.2 * x₂.2 + (x₁.1 • x₃.2 + x₃.1 • x₁.2 + x₁.2 * x₃.2) by simp only [smul_add, add_smul, mul_add] abel right_distrib := fun x₁ x₂ x₃ => ext (add_mul x₁.1 x₂.1 x₃.1) <| show (x₁.1 + x₂.1) • x₃.2 + x₃.1 • (x₁.2 + x₂.2) + (x₁.2 + x₂.2) * x₃.2 = x₁.1 • x₃.2 + x₃.1 • x₁.2 + x₁.2 * x₃.2 + (x₂.1 • x₃.2 + x₃.1 • x₂.2 + x₂.2 * x₃.2) by simp only [add_smul, smul_add, add_mul] abel } instance instMonoid [CommMonoid R] [NonUnitalSemiring A] [DistribMulAction R A] [IsScalarTower R A A] [SMulCommClass R A A] : Monoid (Unitization R A) := { Unitization.instMulOneClass with mul_assoc := fun x y z => ext (mul_assoc x.1 y.1 z.1) <| show (x.1 * y.1) • z.2 + z.1 • (x.1 • y.2 + y.1 • x.2 + x.2 * y.2) + (x.1 • y.2 + y.1 • x.2 + x.2 * y.2) * z.2 = x.1 • (y.1 • z.2 + z.1 • y.2 + y.2 * z.2) + (y.1 * z.1) • x.2 + x.2 * (y.1 • z.2 + z.1 • y.2 + y.2 * z.2) by simp only [smul_add, mul_add, add_mul, smul_smul, smul_mul_assoc, mul_smul_comm, mul_assoc] rw [mul_comm z.1 x.1] rw [mul_comm z.1 y.1] abel } instance instCommMonoid [CommMonoid R] [NonUnitalCommSemiring A] [DistribMulAction R A] [IsScalarTower R A A] [SMulCommClass R A A] : CommMonoid (Unitization R A) := { Unitization.instMonoid with mul_comm := fun x₁ x₂ => ext (mul_comm x₁.1 x₂.1) <| show x₁.1 • x₂.2 + x₂.1 • x₁.2 + x₁.2 * x₂.2 = x₂.1 • x₁.2 + x₁.1 • x₂.2 + x₂.2 * x₁.2 by rw [add_comm (x₁.1 • x₂.2), mul_comm] } instance instSemiring [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : Semiring (Unitization R A) := { Unitization.instMonoid, Unitization.instNonAssocSemiring with } instance instCommSemiring [CommSemiring R] [NonUnitalCommSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : CommSemiring (Unitization R A) := { Unitization.instCommMonoid, Unitization.instNonAssocSemiring with } instance instNonAssocRing [CommRing R] [NonUnitalNonAssocRing A] [Module R A] : NonAssocRing (Unitization R A) := { Unitization.instAddCommGroup, Unitization.instNonAssocSemiring with } instance instRing [CommRing R] [NonUnitalRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : Ring (Unitization R A) := { Unitization.instAddCommGroup, Unitization.instSemiring with } instance instCommRing [CommRing R] [NonUnitalCommRing A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : CommRing (Unitization R A) := { Unitization.instAddCommGroup, Unitization.instCommSemiring with } variable (R A) /-- The canonical inclusion of rings `R →+* Unitization R A`. -/ @[simps apply] def inlRingHom [Semiring R] [NonUnitalSemiring A] [Module R A] : R →+* Unitization R A where toFun := inl map_one' := inl_one A map_mul' := inl_mul A map_zero' := inl_zero A map_add' := inl_add A end Mul /-! ### Star structure -/ section Star variable {R A : Type*} instance instStar [Star R] [Star A] : Star (Unitization R A) := ⟨fun ra => (star ra.fst, star ra.snd)⟩ @[simp] theorem fst_star [Star R] [Star A] (x : Unitization R A) : (star x).fst = star x.fst := rfl @[simp] theorem snd_star [Star R] [Star A] (x : Unitization R A) : (star x).snd = star x.snd := rfl @[simp] theorem inl_star [Star R] [AddMonoid A] [StarAddMonoid A] (r : R) : inl (star r) = star (inl r : Unitization R A) := ext rfl (by simp only [snd_star, star_zero, snd_inl]) @[simp] theorem inr_star [AddMonoid R] [StarAddMonoid R] [Star A] (a : A) : ↑(star a) = star (a : Unitization R A) := ext (by simp only [fst_star, star_zero, fst_inr]) rfl instance instStarAddMonoid [AddMonoid R] [AddMonoid A] [StarAddMonoid R] [StarAddMonoid A] : StarAddMonoid (Unitization R A) where star_involutive x := ext (star_star x.fst) (star_star x.snd) star_add x y := ext (star_add x.fst y.fst) (star_add x.snd y.snd) instance instStarModule [CommSemiring R] [StarRing R] [AddCommMonoid A] [StarAddMonoid A] [Module R A] [StarModule R A] : StarModule R (Unitization R A) where star_smul r x := ext (by simp) (by simp) instance instStarRing [CommSemiring R] [StarRing R] [NonUnitalNonAssocSemiring A] [StarRing A] [Module R A] [StarModule R A] : StarRing (Unitization R A) := { Unitization.instStarAddMonoid with star_mul := fun x y => ext (by simp [-star_mul']) (by simp [-star_mul', add_comm (star x.fst • star y.snd)]) } end Star /-! ### Algebra structure -/ section Algebra variable (S R A : Type*) [CommSemiring S] [CommSemiring R] [NonUnitalSemiring A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] [Algebra S R] [DistribMulAction S A] [IsScalarTower S R A] instance instAlgebra : Algebra S (Unitization R A) := { (Unitization.inlRingHom R A).comp (algebraMap S R) with commutes' := fun s x => by induction' x with r a show inl (algebraMap S R s) * _ = _ * inl (algebraMap S R s) rw [mul_add, add_mul, inl_mul_inl, inl_mul_inl, inl_mul_inr, inr_mul_inl, mul_comm] smul_def' := fun s x => by induction' x with r a show _ = inl (algebraMap S R s) * _ rw [mul_add, smul_add,Algebra.algebraMap_eq_smul_one, inl_mul_inl, inl_mul_inr, smul_one_mul, inl_smul, inr_smul, smul_one_smul] } theorem algebraMap_eq_inl_comp : ⇑(algebraMap S (Unitization R A)) = inl ∘ algebraMap S R := rfl theorem algebraMap_eq_inlRingHom_comp : algebraMap S (Unitization R A) = (inlRingHom R A).comp (algebraMap S R) := rfl theorem algebraMap_eq_inl : ⇑(algebraMap R (Unitization R A)) = inl := rfl theorem algebraMap_eq_inlRingHom : algebraMap R (Unitization R A) = inlRingHom R A := rfl /-- The canonical `R`-algebra projection `Unitization R A → R`. -/ @[simps] def fstHom : Unitization R A →ₐ[R] R where toFun := fst map_one' := fst_one map_mul' := fst_mul map_zero' := fst_zero (A := A) map_add' := fst_add commutes' := fst_inl A end Algebra section coe /-- The coercion from a non-unital `R`-algebra `A` to its unitization `Unitization R A` realized as a non-unital algebra homomorphism. -/ @[simps] def inrNonUnitalAlgHom (R A : Type*) [CommSemiring R] [NonUnitalSemiring A] [Module R A] : A →ₙₐ[R] Unitization R A where toFun := (↑) map_smul' := inr_smul R map_zero' := inr_zero R map_add' := inr_add R map_mul' := inr_mul R /-- The coercion from a non-unital `R`-algebra `A` to its unitization `Unitization R A` realized as a non-unital star algebra homomorphism. -/ @[simps!] def inrNonUnitalStarAlgHom (R A : Type*) [CommSemiring R] [StarAddMonoid R] [NonUnitalSemiring A] [Star A] [Module R A] : A →⋆ₙₐ[R] Unitization R A where toNonUnitalAlgHom := inrNonUnitalAlgHom R A map_star' := inr_star /-- The star algebra equivalence obtained by restricting `Unitization.inrNonUnitalStarAlgHom` to its range. -/ @[simps!] def inrRangeEquiv (R A : Type*) [CommSemiring R] [StarAddMonoid R] [NonUnitalSemiring A] [Star A] [Module R A] [IsScalarTower R A A] [SMulCommClass R A A] : A ≃⋆ₐ[R] NonUnitalStarAlgHom.range (inrNonUnitalStarAlgHom R A) := StarAlgEquiv.ofLeftInverse' (snd_inr R) end coe section AlgHom variable {S R A : Type*} [CommSemiring S] [CommSemiring R] [NonUnitalSemiring A] [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] {B : Type*} [Semiring B] [Algebra S B] [Algebra S R] [DistribMulAction S A] [IsScalarTower S R A] {C : Type*} [Semiring C] [Algebra R C] theorem algHom_ext {F : Type*} [FunLike F (Unitization R A) B] [AlgHomClass F S (Unitization R A) B] {φ ψ : F} (h : ∀ a : A, φ a = ψ a) (h' : ∀ r, φ (algebraMap R (Unitization R A) r) = ψ (algebraMap R (Unitization R A) r)) : φ = ψ := by refine DFunLike.ext φ ψ (fun x ↦ ?_) induction x simp only [map_add, ← algebraMap_eq_inl, h, h'] lemma algHom_ext'' {F : Type*} [FunLike F (Unitization R A) C] [AlgHomClass F R (Unitization R A) C] {φ ψ : F} (h : ∀ a : A, φ a = ψ a) : φ = ψ := algHom_ext h (fun r => by simp only [AlgHomClass.commutes]) /-- See note [partially-applied ext lemmas] -/ @[ext 1100] theorem algHom_ext' {φ ψ : Unitization R A →ₐ[R] C} (h : φ.toNonUnitalAlgHom.comp (inrNonUnitalAlgHom R A) = ψ.toNonUnitalAlgHom.comp (inrNonUnitalAlgHom R A)) : φ = ψ := algHom_ext'' (NonUnitalAlgHom.congr_fun h) /- porting note: this was extracted from `Unitization.lift` below, where it had previously been inlined. Unfortunately, `Unitization.lift` was relatively slow in Lean 3, but in Lean 4 it just times out. -/ /-- A non-unital algebra homomorphism from `A` into a unital `R`-algebra `C` lifts to a unital algebra homomorphism from the unitization into `C`. This is extended to an `Equiv` in `Unitization.lift` and that should be used instead. This declaration only exists for performance reasons. -/ @[simps] def _root_.NonUnitalAlgHom.toAlgHom (φ : A →ₙₐ[R] C) : Unitization R A →ₐ[R] C where toFun := fun x => algebraMap R C x.fst + φ x.snd map_one' := by simp only [fst_one, map_one, snd_one, φ.map_zero, add_zero] map_mul' := fun x y => by induction' x with x_r x_a induction' y with y_r y_a simp only [fst_mul, fst_add, fst_inl, fst_inr, snd_mul, snd_add, snd_inl, snd_inr, add_zero, map_mul, zero_add, map_add, map_smul φ] rw [add_mul, mul_add, mul_add] rw [← Algebra.commutes _ (φ x_a)] simp only [Algebra.algebraMap_eq_smul_one, smul_one_mul, add_assoc] map_zero' := by simp only [fst_zero, map_zero, snd_zero, φ.map_zero, add_zero] map_add' := fun x y => by induction' x with x_r x_a induction' y with y_r y_a simp only [fst_add, fst_inl, fst_inr, add_zero, map_add, snd_add, snd_inl, snd_inr, zero_add, φ.map_add] rw [add_add_add_comm] commutes' := fun r => by simp only [algebraMap_eq_inl, fst_inl, snd_inl, φ.map_zero, add_zero] /-- Non-unital algebra homomorphisms from `A` into a unital `R`-algebra `C` lift uniquely to `Unitization R A →ₐ[R] C`. This is the universal property of the unitization. -/ @[simps! apply symm_apply apply_apply] def lift : (A →ₙₐ[R] C) ≃ (Unitization R A →ₐ[R] C) where toFun := NonUnitalAlgHom.toAlgHom invFun φ := φ.toNonUnitalAlgHom.comp (inrNonUnitalAlgHom R A) left_inv φ := by ext; simp [NonUnitalAlgHomClass.toNonUnitalAlgHom] right_inv φ := by ext; simp [NonUnitalAlgHomClass.toNonUnitalAlgHom] theorem lift_symm_apply_apply (φ : Unitization R A →ₐ[R] C) (a : A) : Unitization.lift.symm φ a = φ a := rfl @[simp] lemma _root_.NonUnitalAlgHom.toAlgHom_zero : ⇑(0 : A →ₙₐ[R] R).toAlgHom = Unitization.fst := by ext simp end AlgHom section StarAlgHom variable {R A C : Type*} [CommSemiring R] [StarRing R] [NonUnitalSemiring A] [StarRing A] variable [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] variable [Semiring C] [Algebra R C] [StarRing C] /-- See note [partially-applied ext lemmas] -/ @[ext] theorem starAlgHom_ext {φ ψ : Unitization R A →⋆ₐ[R] C} (h : (φ : Unitization R A →⋆ₙₐ[R] C).comp (Unitization.inrNonUnitalStarAlgHom R A) = (ψ : Unitization R A →⋆ₙₐ[R] C).comp (Unitization.inrNonUnitalStarAlgHom R A)) : φ = ψ := Unitization.algHom_ext'' <| DFunLike.congr_fun h variable [StarModule R C] /-- Non-unital star algebra homomorphisms from `A` into a unital star `R`-algebra `C` lift uniquely to `Unitization R A →⋆ₐ[R] C`. This is the universal property of the unitization. -/ @[simps! apply symm_apply apply_apply] def starLift : (A →⋆ₙₐ[R] C) ≃ (Unitization R A →⋆ₐ[R] C) := { toFun := fun φ ↦ { toAlgHom := Unitization.lift φ.toNonUnitalAlgHom map_star' := fun x => by induction x simp [map_star] } invFun := fun φ ↦ φ.toNonUnitalStarAlgHom.comp (inrNonUnitalStarAlgHom R A), left_inv := fun φ => by ext; simp, right_inv := fun φ => Unitization.algHom_ext'' <| by simp } -- Note (#6057) : tagging simpNF because linter complains @[simp high, nolint simpNF] theorem starLift_symm_apply_apply (φ : Unitization R A →⋆ₐ[R] C) (a : A) : Unitization.starLift.symm φ a = φ a := rfl end StarAlgHom section StarNormal variable {R A : Type*} [Semiring R] variable [StarAddMonoid R] [Star A] {a : A} @[simp] lemma isSelfAdjoint_inr : IsSelfAdjoint (a : Unitization R A) ↔ IsSelfAdjoint a := by simp only [isSelfAdjoint_iff, ← inr_star, ← inr_mul, inr_injective.eq_iff] alias ⟨_root_.IsSelfAdjoint.of_inr, _⟩ := isSelfAdjoint_inr variable (R) in lemma _root_.IsSelfAdjoint.inr (ha : IsSelfAdjoint a) : IsSelfAdjoint (a : Unitization R A) := isSelfAdjoint_inr.mpr ha variable [AddCommMonoid A] [Mul A] [SMulWithZero R A] @[simp] lemma isStarNormal_inr : IsStarNormal (a : Unitization R A) ↔ IsStarNormal a := by simp only [isStarNormal_iff, commute_iff_eq, ← inr_star, ← inr_mul, inr_injective.eq_iff] alias ⟨_root_.IsStarNormal.of_inr, _⟩ := isStarNormal_inr variable (R a) in instance instIsStarNormal (a : A) [IsStarNormal a] : IsStarNormal (a : Unitization R A) := isStarNormal_inr.mpr ‹_› end StarNormal end Unitization
Algebra\Algebra\Hom\Rat.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Hom import Mathlib.Algebra.Algebra.Rat /-! # Homomorphisms of `ℚ`-algebras -/ namespace RingHom variable {R S : Type*} /-- Reinterpret a `RingHom` as a `ℚ`-algebra homomorphism. This actually yields an equivalence, see `RingHom.equivRatAlgHom`. -/ def toRatAlgHom [Ring R] [Ring S] [Algebra ℚ R] [Algebra ℚ S] (f : R →+* S) : R →ₐ[ℚ] S := { f with commutes' := f.map_rat_algebraMap } @[simp] theorem toRatAlgHom_toRingHom [Ring R] [Ring S] [Algebra ℚ R] [Algebra ℚ S] (f : R →+* S) : ↑f.toRatAlgHom = f := RingHom.ext fun _x => rfl end RingHom section variable {R S : Type*} @[simp] theorem AlgHom.toRingHom_toRatAlgHom [Ring R] [Ring S] [Algebra ℚ R] [Algebra ℚ S] (f : R →ₐ[ℚ] S) : (f : R →+* S).toRatAlgHom = f := AlgHom.ext fun _x => rfl /-- The equivalence between `RingHom` and `ℚ`-algebra homomorphisms. -/ @[simps] def RingHom.equivRatAlgHom [Ring R] [Ring S] [Algebra ℚ R] [Algebra ℚ S] : (R →+* S) ≃ (R →ₐ[ℚ] S) where toFun := RingHom.toRatAlgHom invFun := AlgHom.toRingHom left_inv f := RingHom.toRatAlgHom_toRingHom f right_inv f := AlgHom.toRingHom_toRatAlgHom f end
Algebra\Algebra\Subalgebra\Basic.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Yury Kudryashov -/ import Mathlib.Algebra.Algebra.Operations /-! # Subalgebras over Commutative Semiring In this file we define `Subalgebra`s and the usual operations on them (`map`, `comap`). More lemmas about `adjoin` can be found in `RingTheory.Adjoin`. -/ universe u u' v w w' /-- A subalgebra is a sub(semi)ring that includes the range of `algebraMap`. -/ structure Subalgebra (R : Type u) (A : Type v) [CommSemiring R] [Semiring A] [Algebra R A] extends Subsemiring A : Type v where /-- The image of `algebraMap` is contained in the underlying set of the subalgebra -/ algebraMap_mem' : ∀ r, algebraMap R A r ∈ carrier zero_mem' := (algebraMap R A).map_zero ▸ algebraMap_mem' 0 one_mem' := (algebraMap R A).map_one ▸ algebraMap_mem' 1 /-- Reinterpret a `Subalgebra` as a `Subsemiring`. -/ add_decl_doc Subalgebra.toSubsemiring namespace Subalgebra variable {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} variable [CommSemiring R] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] instance : SetLike (Subalgebra R A) A where coe s := s.carrier coe_injective' p q h := by cases p; cases q; congr; exact SetLike.coe_injective' h instance SubsemiringClass : SubsemiringClass (Subalgebra R A) A where add_mem {s} := add_mem (s := s.toSubsemiring) mul_mem {s} := mul_mem (s := s.toSubsemiring) one_mem {s} := one_mem s.toSubsemiring zero_mem {s} := zero_mem s.toSubsemiring @[simp] theorem mem_toSubsemiring {S : Subalgebra R A} {x} : x ∈ S.toSubsemiring ↔ x ∈ S := Iff.rfl -- @[simp] -- Porting note (#10618): simp can prove this theorem mem_carrier {s : Subalgebra R A} {x : A} : x ∈ s.carrier ↔ x ∈ s := Iff.rfl @[ext] theorem ext {S T : Subalgebra R A} (h : ∀ x : A, x ∈ S ↔ x ∈ T) : S = T := SetLike.ext h @[simp] theorem coe_toSubsemiring (S : Subalgebra R A) : (↑S.toSubsemiring : Set A) = S := rfl theorem toSubsemiring_injective : Function.Injective (toSubsemiring : Subalgebra R A → Subsemiring A) := fun S T h => ext fun x => by rw [← mem_toSubsemiring, ← mem_toSubsemiring, h] theorem toSubsemiring_inj {S U : Subalgebra R A} : S.toSubsemiring = U.toSubsemiring ↔ S = U := toSubsemiring_injective.eq_iff /-- Copy of a subalgebra with a new `carrier` equal to the old one. Useful to fix definitional equalities. -/ protected def copy (S : Subalgebra R A) (s : Set A) (hs : s = ↑S) : Subalgebra R A := { S.toSubsemiring.copy s hs with carrier := s algebraMap_mem' := hs.symm ▸ S.algebraMap_mem' } @[simp] theorem coe_copy (S : Subalgebra R A) (s : Set A) (hs : s = ↑S) : (S.copy s hs : Set A) = s := rfl theorem copy_eq (S : Subalgebra R A) (s : Set A) (hs : s = ↑S) : S.copy s hs = S := SetLike.coe_injective hs variable (S : Subalgebra R A) instance instSMulMemClass : SMulMemClass (Subalgebra R A) R A where smul_mem {S} r x hx := (Algebra.smul_def r x).symm ▸ mul_mem (S.algebraMap_mem' r) hx @[aesop safe apply (rule_sets := [SetLike])] theorem _root_.algebraMap_mem {S R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [SetLike S A] [OneMemClass S A] [SMulMemClass S R A] (s : S) (r : R) : algebraMap R A r ∈ s := Algebra.algebraMap_eq_smul_one (A := A) r ▸ SMulMemClass.smul_mem r (one_mem s) protected theorem algebraMap_mem (r : R) : algebraMap R A r ∈ S := algebraMap_mem S r theorem rangeS_le : (algebraMap R A).rangeS ≤ S.toSubsemiring := fun _x ⟨r, hr⟩ => hr ▸ S.algebraMap_mem r theorem range_subset : Set.range (algebraMap R A) ⊆ S := fun _x ⟨r, hr⟩ => hr ▸ S.algebraMap_mem r theorem range_le : Set.range (algebraMap R A) ≤ S := S.range_subset theorem smul_mem {x : A} (hx : x ∈ S) (r : R) : r • x ∈ S := SMulMemClass.smul_mem r hx protected theorem one_mem : (1 : A) ∈ S := one_mem S protected theorem mul_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x * y ∈ S := mul_mem hx hy protected theorem pow_mem {x : A} (hx : x ∈ S) (n : ℕ) : x ^ n ∈ S := pow_mem hx n protected theorem zero_mem : (0 : A) ∈ S := zero_mem S protected theorem add_mem {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x + y ∈ S := add_mem hx hy protected theorem nsmul_mem {x : A} (hx : x ∈ S) (n : ℕ) : n • x ∈ S := nsmul_mem hx n protected theorem natCast_mem (n : ℕ) : (n : A) ∈ S := natCast_mem S n protected theorem list_prod_mem {L : List A} (h : ∀ x ∈ L, x ∈ S) : L.prod ∈ S := list_prod_mem h protected theorem list_sum_mem {L : List A} (h : ∀ x ∈ L, x ∈ S) : L.sum ∈ S := list_sum_mem h protected theorem multiset_sum_mem {m : Multiset A} (h : ∀ x ∈ m, x ∈ S) : m.sum ∈ S := multiset_sum_mem m h protected theorem sum_mem {ι : Type w} {t : Finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : (∑ x ∈ t, f x) ∈ S := sum_mem h protected theorem multiset_prod_mem {R : Type u} {A : Type v} [CommSemiring R] [CommSemiring A] [Algebra R A] (S : Subalgebra R A) {m : Multiset A} (h : ∀ x ∈ m, x ∈ S) : m.prod ∈ S := multiset_prod_mem m h protected theorem prod_mem {R : Type u} {A : Type v} [CommSemiring R] [CommSemiring A] [Algebra R A] (S : Subalgebra R A) {ι : Type w} {t : Finset ι} {f : ι → A} (h : ∀ x ∈ t, f x ∈ S) : (∏ x ∈ t, f x) ∈ S := prod_mem h instance {R A : Type*} [CommRing R] [Ring A] [Algebra R A] : SubringClass (Subalgebra R A) A := { Subalgebra.SubsemiringClass with neg_mem := fun {S x} hx => neg_one_smul R x ▸ S.smul_mem hx _ } protected theorem neg_mem {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) {x : A} (hx : x ∈ S) : -x ∈ S := neg_mem hx protected theorem sub_mem {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) {x y : A} (hx : x ∈ S) (hy : y ∈ S) : x - y ∈ S := sub_mem hx hy protected theorem zsmul_mem {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) {x : A} (hx : x ∈ S) (n : ℤ) : n • x ∈ S := zsmul_mem hx n protected theorem intCast_mem {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) (n : ℤ) : (n : A) ∈ S := intCast_mem S n @[deprecated natCast_mem (since := "2024-04-05")] alias coe_nat_mem := Subalgebra.natCast_mem @[deprecated intCast_mem (since := "2024-04-05")] alias coe_int_mem := Subalgebra.intCast_mem /-- The projection from a subalgebra of `A` to an additive submonoid of `A`. -/ def toAddSubmonoid {R : Type u} {A : Type v} [CommSemiring R] [Semiring A] [Algebra R A] (S : Subalgebra R A) : AddSubmonoid A := S.toSubsemiring.toAddSubmonoid -- Porting note: this field already exists in Lean 4. -- /-- The projection from a subalgebra of `A` to a submonoid of `A`. -/ -- def toSubmonoid {R : Type u} {A : Type v} [CommSemiring R] [Semiring A] [Algebra R A] -- (S : Subalgebra R A) : Submonoid A := -- S.toSubsemiring.toSubmonoid /-- A subalgebra over a ring is also a `Subring`. -/ def toSubring {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) : Subring A := { S.toSubsemiring with neg_mem' := S.neg_mem } @[simp] theorem mem_toSubring {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S : Subalgebra R A} {x} : x ∈ S.toSubring ↔ x ∈ S := Iff.rfl @[simp] theorem coe_toSubring {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) : (↑S.toSubring : Set A) = S := rfl theorem toSubring_injective {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] : Function.Injective (toSubring : Subalgebra R A → Subring A) := fun S T h => ext fun x => by rw [← mem_toSubring, ← mem_toSubring, h] theorem toSubring_inj {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S U : Subalgebra R A} : S.toSubring = U.toSubring ↔ S = U := toSubring_injective.eq_iff instance : Inhabited S := ⟨(0 : S.toSubsemiring)⟩ section /-! `Subalgebra`s inherit structure from their `Subsemiring` / `Semiring` coercions. -/ instance toSemiring {R A} [CommSemiring R] [Semiring A] [Algebra R A] (S : Subalgebra R A) : Semiring S := S.toSubsemiring.toSemiring instance toCommSemiring {R A} [CommSemiring R] [CommSemiring A] [Algebra R A] (S : Subalgebra R A) : CommSemiring S := S.toSubsemiring.toCommSemiring instance toRing {R A} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) : Ring S := S.toSubring.toRing instance toCommRing {R A} [CommRing R] [CommRing A] [Algebra R A] (S : Subalgebra R A) : CommRing S := S.toSubring.toCommRing end /-- The forgetful map from `Subalgebra` to `Submodule` as an `OrderEmbedding` -/ def toSubmodule : Subalgebra R A ↪o Submodule R A where toEmbedding := { toFun := fun S => { S with carrier := S smul_mem' := fun c {x} hx ↦ (Algebra.smul_def c x).symm ▸ mul_mem (S.range_le ⟨c, rfl⟩) hx } inj' := fun _ _ h ↦ ext fun x ↦ SetLike.ext_iff.mp h x } map_rel_iff' := SetLike.coe_subset_coe.symm.trans SetLike.coe_subset_coe /- TODO: bundle other forgetful maps between algebraic substructures, e.g. `to_subsemiring` and `to_subring` in this file. -/ @[simp] theorem mem_toSubmodule {x} : x ∈ (toSubmodule S) ↔ x ∈ S := Iff.rfl @[simp] theorem coe_toSubmodule (S : Subalgebra R A) : (toSubmodule S : Set A) = S := rfl theorem toSubmodule_injective : Function.Injective (toSubmodule : Subalgebra R A → Submodule R A) := fun _S₁ _S₂ h => SetLike.ext (SetLike.ext_iff.mp h :) section /-! `Subalgebra`s inherit structure from their `Submodule` coercions. -/ instance (priority := low) module' [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] : Module R' S := S.toSubmodule.module' instance : Module R S := S.module' instance [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] : IsScalarTower R' R S := inferInstanceAs (IsScalarTower R' R (toSubmodule S)) /- More general form of `Subalgebra.algebra`. This instance should have low priority since it is slow to fail: before failing, it will cause a search through all `SMul R' R` instances, which can quickly get expensive. -/ instance (priority := 500) algebra' [CommSemiring R'] [SMul R' R] [Algebra R' A] [IsScalarTower R' R A] : Algebra R' S := { (algebraMap R' A).codRestrict S fun x => by rw [Algebra.algebraMap_eq_smul_one, ← smul_one_smul R x (1 : A), ← Algebra.algebraMap_eq_smul_one] exact algebraMap_mem S _ with commutes' := fun c x => Subtype.eq <| Algebra.commutes _ _ smul_def' := fun c x => Subtype.eq <| Algebra.smul_def _ _ } instance algebra : Algebra R S := S.algebra' end instance noZeroSMulDivisors_bot [NoZeroSMulDivisors R A] : NoZeroSMulDivisors R S := ⟨fun {c} {x : S} h => have : c = 0 ∨ (x : A) = 0 := eq_zero_or_eq_zero_of_smul_eq_zero (congr_arg Subtype.val h) this.imp_right (@Subtype.ext_iff _ _ x 0).mpr⟩ protected theorem coe_add (x y : S) : (↑(x + y) : A) = ↑x + ↑y := rfl protected theorem coe_mul (x y : S) : (↑(x * y) : A) = ↑x * ↑y := rfl protected theorem coe_zero : ((0 : S) : A) = 0 := rfl protected theorem coe_one : ((1 : S) : A) = 1 := rfl protected theorem coe_neg {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S : Subalgebra R A} (x : S) : (↑(-x) : A) = -↑x := rfl protected theorem coe_sub {R : Type u} {A : Type v} [CommRing R] [Ring A] [Algebra R A] {S : Subalgebra R A} (x y : S) : (↑(x - y) : A) = ↑x - ↑y := rfl @[simp, norm_cast] theorem coe_smul [Semiring R'] [SMul R' R] [Module R' A] [IsScalarTower R' R A] (r : R') (x : S) : (↑(r • x) : A) = r • (x : A) := rfl @[simp, norm_cast] theorem coe_algebraMap [CommSemiring R'] [SMul R' R] [Algebra R' A] [IsScalarTower R' R A] (r : R') : ↑(algebraMap R' S r) = algebraMap R' A r := rfl protected theorem coe_pow (x : S) (n : ℕ) : (↑(x ^ n) : A) = (x : A) ^ n := SubmonoidClass.coe_pow x n protected theorem coe_eq_zero {x : S} : (x : A) = 0 ↔ x = 0 := ZeroMemClass.coe_eq_zero protected theorem coe_eq_one {x : S} : (x : A) = 1 ↔ x = 1 := OneMemClass.coe_eq_one -- todo: standardize on the names these morphisms -- compare with submodule.subtype /-- Embedding of a subalgebra into the algebra. -/ def val : S →ₐ[R] A := { toFun := ((↑) : S → A) map_zero' := rfl map_one' := rfl map_add' := fun _ _ ↦ rfl map_mul' := fun _ _ ↦ rfl commutes' := fun _ ↦ rfl } @[simp] theorem coe_val : (S.val : S → A) = ((↑) : S → A) := rfl theorem val_apply (x : S) : S.val x = (x : A) := rfl @[simp] theorem toSubsemiring_subtype : S.toSubsemiring.subtype = (S.val : S →+* A) := rfl @[simp] theorem toSubring_subtype {R A : Type*} [CommRing R] [Ring A] [Algebra R A] (S : Subalgebra R A) : S.toSubring.subtype = (S.val : S →+* A) := rfl /-- Linear equivalence between `S : Submodule R A` and `S`. Though these types are equal, we define it as a `LinearEquiv` to avoid type equalities. -/ def toSubmoduleEquiv (S : Subalgebra R A) : toSubmodule S ≃ₗ[R] S := LinearEquiv.ofEq _ _ rfl /-- Transport a subalgebra via an algebra homomorphism. -/ def map (f : A →ₐ[R] B) (S : Subalgebra R A) : Subalgebra R B := { S.toSubsemiring.map (f : A →+* B) with algebraMap_mem' := fun r => f.commutes r ▸ Set.mem_image_of_mem _ (S.algebraMap_mem r) } theorem map_mono {S₁ S₂ : Subalgebra R A} {f : A →ₐ[R] B} : S₁ ≤ S₂ → S₁.map f ≤ S₂.map f := Set.image_subset f theorem map_injective {f : A →ₐ[R] B} (hf : Function.Injective f) : Function.Injective (map f) := fun _S₁ _S₂ ih => ext <| Set.ext_iff.1 <| Set.image_injective.2 hf <| Set.ext <| SetLike.ext_iff.mp ih @[simp] theorem map_id (S : Subalgebra R A) : S.map (AlgHom.id R A) = S := SetLike.coe_injective <| Set.image_id _ theorem map_map (S : Subalgebra R A) (g : B →ₐ[R] C) (f : A →ₐ[R] B) : (S.map f).map g = S.map (g.comp f) := SetLike.coe_injective <| Set.image_image _ _ _ @[simp] theorem mem_map {S : Subalgebra R A} {f : A →ₐ[R] B} {y : B} : y ∈ map f S ↔ ∃ x ∈ S, f x = y := Subsemiring.mem_map theorem map_toSubmodule {S : Subalgebra R A} {f : A →ₐ[R] B} : (toSubmodule <| S.map f) = S.toSubmodule.map f.toLinearMap := SetLike.coe_injective rfl theorem map_toSubsemiring {S : Subalgebra R A} {f : A →ₐ[R] B} : (S.map f).toSubsemiring = S.toSubsemiring.map f.toRingHom := SetLike.coe_injective rfl @[simp] theorem coe_map (S : Subalgebra R A) (f : A →ₐ[R] B) : (S.map f : Set B) = f '' S := rfl /-- Preimage of a subalgebra under an algebra homomorphism. -/ def comap (f : A →ₐ[R] B) (S : Subalgebra R B) : Subalgebra R A := { S.toSubsemiring.comap (f : A →+* B) with algebraMap_mem' := fun r => show f (algebraMap R A r) ∈ S from (f.commutes r).symm ▸ S.algebraMap_mem r } theorem map_le {S : Subalgebra R A} {f : A →ₐ[R] B} {U : Subalgebra R B} : map f S ≤ U ↔ S ≤ comap f U := Set.image_subset_iff theorem gc_map_comap (f : A →ₐ[R] B) : GaloisConnection (map f) (comap f) := fun _S _U => map_le @[simp] theorem mem_comap (S : Subalgebra R B) (f : A →ₐ[R] B) (x : A) : x ∈ S.comap f ↔ f x ∈ S := Iff.rfl @[simp, norm_cast] theorem coe_comap (S : Subalgebra R B) (f : A →ₐ[R] B) : (S.comap f : Set A) = f ⁻¹' (S : Set B) := rfl instance noZeroDivisors {R A : Type*} [CommSemiring R] [Semiring A] [NoZeroDivisors A] [Algebra R A] (S : Subalgebra R A) : NoZeroDivisors S := inferInstanceAs (NoZeroDivisors S.toSubsemiring) instance isDomain {R A : Type*} [CommRing R] [Ring A] [IsDomain A] [Algebra R A] (S : Subalgebra R A) : IsDomain S := inferInstanceAs (IsDomain S.toSubring) end Subalgebra namespace SubalgebraClass variable {S R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] variable [SetLike S A] [SubsemiringClass S A] [hSR : SMulMemClass S R A] (s : S) instance (priority := 75) toAlgebra : Algebra R s where toFun r := ⟨algebraMap R A r, algebraMap_mem s r⟩ map_one' := Subtype.ext <| by simp map_mul' _ _ := Subtype.ext <| by simp map_zero' := Subtype.ext <| by simp map_add' _ _ := Subtype.ext <| by simp commutes' r x := Subtype.ext <| Algebra.commutes r (x : A) smul_def' r x := Subtype.ext <| (algebraMap_smul A r (x : A)).symm @[simp, norm_cast] lemma coe_algebraMap (r : R) : (algebraMap R s r : A) = algebraMap R A r := rfl /-- Embedding of a subalgebra into the algebra, as an algebra homomorphism. -/ def val (s : S) : s →ₐ[R] A := { SubsemiringClass.subtype s, SMulMemClass.subtype s with toFun := (↑) commutes' := fun _ ↦ rfl } @[simp] theorem coe_val : (val s : s → A) = ((↑) : s → A) := rfl end SubalgebraClass namespace Submodule variable {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] variable (p : Submodule R A) /-- A submodule containing `1` and closed under multiplication is a subalgebra. -/ def toSubalgebra (p : Submodule R A) (h_one : (1 : A) ∈ p) (h_mul : ∀ x y, x ∈ p → y ∈ p → x * y ∈ p) : Subalgebra R A := { p with mul_mem' := fun hx hy ↦ h_mul _ _ hx hy one_mem' := h_one algebraMap_mem' := fun r => by rw [Algebra.algebraMap_eq_smul_one] exact p.smul_mem _ h_one } @[simp] theorem mem_toSubalgebra {p : Submodule R A} {h_one h_mul} {x} : x ∈ p.toSubalgebra h_one h_mul ↔ x ∈ p := Iff.rfl @[simp] theorem coe_toSubalgebra (p : Submodule R A) (h_one h_mul) : (p.toSubalgebra h_one h_mul : Set A) = p := rfl -- Porting note: changed statement to reflect new structures -- @[simp] -- Porting note: as a result, it is no longer a great simp lemma theorem toSubalgebra_mk (s : Submodule R A) (h1 hmul) : s.toSubalgebra h1 hmul = Subalgebra.mk ⟨⟨⟨s, @hmul⟩, h1⟩, s.add_mem, s.zero_mem⟩ (by intro r; rw [Algebra.algebraMap_eq_smul_one]; apply s.smul_mem _ h1) := rfl @[simp] theorem toSubalgebra_toSubmodule (p : Submodule R A) (h_one h_mul) : Subalgebra.toSubmodule (p.toSubalgebra h_one h_mul) = p := SetLike.coe_injective rfl @[simp] theorem _root_.Subalgebra.toSubmodule_toSubalgebra (S : Subalgebra R A) : (S.toSubmodule.toSubalgebra S.one_mem fun _ _ => S.mul_mem) = S := SetLike.coe_injective rfl end Submodule namespace AlgHom variable {R' : Type u'} {R : Type u} {A : Type v} {B : Type w} {C : Type w'} variable [CommSemiring R] variable [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] [Semiring C] [Algebra R C] variable (φ : A →ₐ[R] B) /-- Range of an `AlgHom` as a subalgebra. -/ protected def range (φ : A →ₐ[R] B) : Subalgebra R B := { φ.toRingHom.rangeS with algebraMap_mem' := fun r => ⟨algebraMap R A r, φ.commutes r⟩ } @[simp] theorem mem_range (φ : A →ₐ[R] B) {y : B} : y ∈ φ.range ↔ ∃ x, φ x = y := RingHom.mem_rangeS theorem mem_range_self (φ : A →ₐ[R] B) (x : A) : φ x ∈ φ.range := φ.mem_range.2 ⟨x, rfl⟩ @[simp] theorem coe_range (φ : A →ₐ[R] B) : (φ.range : Set B) = Set.range φ := by ext rw [SetLike.mem_coe, mem_range] rfl theorem range_comp (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range = f.range.map g := SetLike.coe_injective (Set.range_comp g f) theorem range_comp_le_range (f : A →ₐ[R] B) (g : B →ₐ[R] C) : (g.comp f).range ≤ g.range := SetLike.coe_mono (Set.range_comp_subset_range f g) /-- Restrict the codomain of an algebra homomorphism. -/ def codRestrict (f : A →ₐ[R] B) (S : Subalgebra R B) (hf : ∀ x, f x ∈ S) : A →ₐ[R] S := { RingHom.codRestrict (f : A →+* B) S hf with commutes' := fun r => Subtype.eq <| f.commutes r } @[simp] theorem val_comp_codRestrict (f : A →ₐ[R] B) (S : Subalgebra R B) (hf : ∀ x, f x ∈ S) : S.val.comp (f.codRestrict S hf) = f := AlgHom.ext fun _ => rfl @[simp] theorem coe_codRestrict (f : A →ₐ[R] B) (S : Subalgebra R B) (hf : ∀ x, f x ∈ S) (x : A) : ↑(f.codRestrict S hf x) = f x := rfl theorem injective_codRestrict (f : A →ₐ[R] B) (S : Subalgebra R B) (hf : ∀ x, f x ∈ S) : Function.Injective (f.codRestrict S hf) ↔ Function.Injective f := ⟨fun H _x _y hxy => H <| Subtype.eq hxy, fun H _x _y hxy => H (congr_arg Subtype.val hxy : _)⟩ /-- Restrict the codomain of an `AlgHom` `f` to `f.range`. This is the bundled version of `Set.rangeFactorization`. -/ abbrev rangeRestrict (f : A →ₐ[R] B) : A →ₐ[R] f.range := f.codRestrict f.range f.mem_range_self theorem rangeRestrict_surjective (f : A →ₐ[R] B) : Function.Surjective (f.rangeRestrict) := fun ⟨_y, hy⟩ => let ⟨x, hx⟩ := hy ⟨x, SetCoe.ext hx⟩ /-- The range of a morphism of algebras is a fintype, if the domain is a fintype. Note that this instance can cause a diamond with `Subtype.fintype` if `B` is also a fintype. -/ instance fintypeRange [Fintype A] [DecidableEq B] (φ : A →ₐ[R] B) : Fintype φ.range := Set.fintypeRange φ end AlgHom namespace AlgEquiv variable {R : Type u} {A : Type v} {B : Type w} variable [CommSemiring R] [Semiring A] [Semiring B] [Algebra R A] [Algebra R B] /-- Restrict an algebra homomorphism with a left inverse to an algebra isomorphism to its range. This is a computable alternative to `AlgEquiv.ofInjective`. -/ def ofLeftInverse {g : B → A} {f : A →ₐ[R] B} (h : Function.LeftInverse g f) : A ≃ₐ[R] f.range := { f.rangeRestrict with toFun := f.rangeRestrict invFun := g ∘ f.range.val left_inv := h right_inv := fun x => Subtype.ext <| let ⟨x', hx'⟩ := f.mem_range.mp x.prop show f (g x) = x by rw [← hx', h x'] } @[simp] theorem ofLeftInverse_apply {g : B → A} {f : A →ₐ[R] B} (h : Function.LeftInverse g f) (x : A) : ↑(ofLeftInverse h x) = f x := rfl @[simp] theorem ofLeftInverse_symm_apply {g : B → A} {f : A →ₐ[R] B} (h : Function.LeftInverse g f) (x : f.range) : (ofLeftInverse h).symm x = g x := rfl /-- Restrict an injective algebra homomorphism to an algebra isomorphism -/ noncomputable def ofInjective (f : A →ₐ[R] B) (hf : Function.Injective f) : A ≃ₐ[R] f.range := ofLeftInverse (Classical.choose_spec hf.hasLeftInverse) @[simp] theorem ofInjective_apply (f : A →ₐ[R] B) (hf : Function.Injective f) (x : A) : ↑(ofInjective f hf x) = f x := rfl /-- Restrict an algebra homomorphism between fields to an algebra isomorphism -/ noncomputable def ofInjectiveField {E F : Type*} [DivisionRing E] [Semiring F] [Nontrivial F] [Algebra R E] [Algebra R F] (f : E →ₐ[R] F) : E ≃ₐ[R] f.range := ofInjective f f.toRingHom.injective /-- Given an equivalence `e : A ≃ₐ[R] B` of `R`-algebras and a subalgebra `S` of `A`, `subalgebraMap` is the induced equivalence between `S` and `S.map e` -/ @[simps!] def subalgebraMap (e : A ≃ₐ[R] B) (S : Subalgebra R A) : S ≃ₐ[R] S.map (e : A →ₐ[R] B) := { e.toRingEquiv.subsemiringMap S.toSubsemiring with commutes' := fun r => by ext; dsimp only; erw [RingEquiv.subsemiringMap_apply_coe] exact e.commutes _ } end AlgEquiv namespace Algebra variable (R : Type u) {A : Type v} {B : Type w} variable [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] /-- The minimal subalgebra that includes `s`. -/ def adjoin (s : Set A) : Subalgebra R A := { Subsemiring.closure (Set.range (algebraMap R A) ∪ s) with algebraMap_mem' := fun r => Subsemiring.subset_closure <| Or.inl ⟨r, rfl⟩ } variable {R} protected theorem gc : GaloisConnection (adjoin R : Set A → Subalgebra R A) (↑) := fun s S => ⟨fun H => le_trans (le_trans Set.subset_union_right Subsemiring.subset_closure) H, fun H => show Subsemiring.closure (Set.range (algebraMap R A) ∪ s) ≤ S.toSubsemiring from Subsemiring.closure_le.2 <| Set.union_subset S.range_subset H⟩ /-- Galois insertion between `adjoin` and `coe`. -/ protected def gi : GaloisInsertion (adjoin R : Set A → Subalgebra R A) (↑) where choice s hs := (adjoin R s).copy s <| le_antisymm (Algebra.gc.le_u_l s) hs gc := Algebra.gc le_l_u S := (Algebra.gc (S : Set A) (adjoin R S)).1 <| le_rfl choice_eq _ _ := Subalgebra.copy_eq _ _ _ instance : CompleteLattice (Subalgebra R A) where __ := GaloisInsertion.liftCompleteLattice Algebra.gi bot := (Algebra.ofId R A).range bot_le _S := fun _a ⟨_r, hr⟩ => hr ▸ algebraMap_mem _ _ @[simp] theorem coe_top : (↑(⊤ : Subalgebra R A) : Set A) = Set.univ := rfl @[simp] theorem mem_top {x : A} : x ∈ (⊤ : Subalgebra R A) := Set.mem_univ x @[simp] theorem top_toSubmodule : Subalgebra.toSubmodule (⊤ : Subalgebra R A) = ⊤ := rfl @[simp] theorem top_toSubsemiring : (⊤ : Subalgebra R A).toSubsemiring = ⊤ := rfl @[simp] theorem top_toSubring {R A : Type*} [CommRing R] [Ring A] [Algebra R A] : (⊤ : Subalgebra R A).toSubring = ⊤ := rfl @[simp] theorem toSubmodule_eq_top {S : Subalgebra R A} : Subalgebra.toSubmodule S = ⊤ ↔ S = ⊤ := Subalgebra.toSubmodule.injective.eq_iff' top_toSubmodule @[simp] theorem toSubsemiring_eq_top {S : Subalgebra R A} : S.toSubsemiring = ⊤ ↔ S = ⊤ := Subalgebra.toSubsemiring_injective.eq_iff' top_toSubsemiring @[simp] theorem toSubring_eq_top {R A : Type*} [CommRing R] [Ring A] [Algebra R A] {S : Subalgebra R A} : S.toSubring = ⊤ ↔ S = ⊤ := Subalgebra.toSubring_injective.eq_iff' top_toSubring theorem mem_sup_left {S T : Subalgebra R A} : ∀ {x : A}, x ∈ S → x ∈ S ⊔ T := have : S ≤ S ⊔ T := le_sup_left; (this ·) -- Porting note: need `have` instead of `show` theorem mem_sup_right {S T : Subalgebra R A} : ∀ {x : A}, x ∈ T → x ∈ S ⊔ T := have : T ≤ S ⊔ T := le_sup_right; (this ·) -- Porting note: need `have` instead of `show` theorem mul_mem_sup {S T : Subalgebra R A} {x y : A} (hx : x ∈ S) (hy : y ∈ T) : x * y ∈ S ⊔ T := (S ⊔ T).mul_mem (mem_sup_left hx) (mem_sup_right hy) theorem map_sup (f : A →ₐ[R] B) (S T : Subalgebra R A) : (S ⊔ T).map f = S.map f ⊔ T.map f := (Subalgebra.gc_map_comap f).l_sup @[simp, norm_cast] theorem coe_inf (S T : Subalgebra R A) : (↑(S ⊓ T) : Set A) = (S ∩ T : Set A) := rfl @[simp] theorem mem_inf {S T : Subalgebra R A} {x : A} : x ∈ S ⊓ T ↔ x ∈ S ∧ x ∈ T := Iff.rfl open Subalgebra in @[simp] theorem inf_toSubmodule (S T : Subalgebra R A) : toSubmodule (S ⊓ T) = toSubmodule S ⊓ toSubmodule T := rfl @[simp] theorem inf_toSubsemiring (S T : Subalgebra R A) : (S ⊓ T).toSubsemiring = S.toSubsemiring ⊓ T.toSubsemiring := rfl @[simp, norm_cast] theorem coe_sInf (S : Set (Subalgebra R A)) : (↑(sInf S) : Set A) = ⋂ s ∈ S, ↑s := sInf_image theorem mem_sInf {S : Set (Subalgebra R A)} {x : A} : x ∈ sInf S ↔ ∀ p ∈ S, x ∈ p := by simp only [← SetLike.mem_coe, coe_sInf, Set.mem_iInter₂] @[simp] theorem sInf_toSubmodule (S : Set (Subalgebra R A)) : Subalgebra.toSubmodule (sInf S) = sInf (Subalgebra.toSubmodule '' S) := SetLike.coe_injective <| by simp @[simp] theorem sInf_toSubsemiring (S : Set (Subalgebra R A)) : (sInf S).toSubsemiring = sInf (Subalgebra.toSubsemiring '' S) := SetLike.coe_injective <| by simp @[simp, norm_cast] theorem coe_iInf {ι : Sort*} {S : ι → Subalgebra R A} : (↑(⨅ i, S i) : Set A) = ⋂ i, S i := by simp [iInf] theorem mem_iInf {ι : Sort*} {S : ι → Subalgebra R A} {x : A} : (x ∈ ⨅ i, S i) ↔ ∀ i, x ∈ S i := by simp only [iInf, mem_sInf, Set.forall_mem_range] open Subalgebra in @[simp] theorem iInf_toSubmodule {ι : Sort*} (S : ι → Subalgebra R A) : toSubmodule (⨅ i, S i) = ⨅ i, toSubmodule (S i) := SetLike.coe_injective <| by simp instance : Inhabited (Subalgebra R A) := ⟨⊥⟩ theorem mem_bot {x : A} : x ∈ (⊥ : Subalgebra R A) ↔ x ∈ Set.range (algebraMap R A) := Iff.rfl theorem toSubmodule_bot : Subalgebra.toSubmodule (⊥ : Subalgebra R A) = 1 := rfl @[simp] theorem coe_bot : ((⊥ : Subalgebra R A) : Set A) = Set.range (algebraMap R A) := rfl theorem eq_top_iff {S : Subalgebra R A} : S = ⊤ ↔ ∀ x : A, x ∈ S := ⟨fun h x => by rw [h]; exact mem_top, fun h => by ext x; exact ⟨fun _ => mem_top, fun _ => h x⟩⟩ theorem range_top_iff_surjective (f : A →ₐ[R] B) : f.range = (⊤ : Subalgebra R B) ↔ Function.Surjective f := Algebra.eq_top_iff @[simp] theorem range_id : (AlgHom.id R A).range = ⊤ := SetLike.coe_injective Set.range_id @[simp] theorem map_top (f : A →ₐ[R] B) : (⊤ : Subalgebra R A).map f = f.range := SetLike.coe_injective Set.image_univ @[simp] theorem map_bot (f : A →ₐ[R] B) : (⊥ : Subalgebra R A).map f = ⊥ := Subalgebra.toSubmodule_injective <| Submodule.map_one _ @[simp] theorem comap_top (f : A →ₐ[R] B) : (⊤ : Subalgebra R B).comap f = ⊤ := eq_top_iff.2 fun _x => mem_top /-- `AlgHom` to `⊤ : Subalgebra R A`. -/ def toTop : A →ₐ[R] (⊤ : Subalgebra R A) := (AlgHom.id R A).codRestrict ⊤ fun _ => mem_top theorem surjective_algebraMap_iff : Function.Surjective (algebraMap R A) ↔ (⊤ : Subalgebra R A) = ⊥ := ⟨fun h => eq_bot_iff.2 fun y _ => let ⟨_x, hx⟩ := h y hx ▸ Subalgebra.algebraMap_mem _ _, fun h y => Algebra.mem_bot.1 <| eq_bot_iff.1 h (Algebra.mem_top : y ∈ _)⟩ theorem bijective_algebraMap_iff {R A : Type*} [Field R] [Semiring A] [Nontrivial A] [Algebra R A] : Function.Bijective (algebraMap R A) ↔ (⊤ : Subalgebra R A) = ⊥ := ⟨fun h => surjective_algebraMap_iff.1 h.2, fun h => ⟨(algebraMap R A).injective, surjective_algebraMap_iff.2 h⟩⟩ /-- The bottom subalgebra is isomorphic to the base ring. -/ noncomputable def botEquivOfInjective (h : Function.Injective (algebraMap R A)) : (⊥ : Subalgebra R A) ≃ₐ[R] R := AlgEquiv.symm <| AlgEquiv.ofBijective (Algebra.ofId R _) ⟨fun _x _y hxy => h (congr_arg Subtype.val hxy : _), fun ⟨_y, x, hx⟩ => ⟨x, Subtype.eq hx⟩⟩ /-- The bottom subalgebra is isomorphic to the field. -/ @[simps! symm_apply] noncomputable def botEquiv (F R : Type*) [Field F] [Semiring R] [Nontrivial R] [Algebra F R] : (⊥ : Subalgebra F R) ≃ₐ[F] F := botEquivOfInjective (RingHom.injective _) end Algebra namespace Subalgebra open Algebra variable {R : Type u} {A : Type v} {B : Type w} variable [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] variable (S : Subalgebra R A) /-- The top subalgebra is isomorphic to the algebra. This is the algebra version of `Submodule.topEquiv`. -/ @[simps!] def topEquiv : (⊤ : Subalgebra R A) ≃ₐ[R] A := AlgEquiv.ofAlgHom (Subalgebra.val ⊤) toTop rfl <| AlgHom.ext fun _ => Subtype.ext rfl instance subsingleton_of_subsingleton [Subsingleton A] : Subsingleton (Subalgebra R A) := ⟨fun B C => ext fun x => by simp only [Subsingleton.elim x 0, zero_mem B, zero_mem C]⟩ instance _root_.AlgHom.subsingleton [Subsingleton (Subalgebra R A)] : Subsingleton (A →ₐ[R] B) := ⟨fun f g => AlgHom.ext fun a => have : a ∈ (⊥ : Subalgebra R A) := Subsingleton.elim (⊤ : Subalgebra R A) ⊥ ▸ mem_top let ⟨_x, hx⟩ := Set.mem_range.mp (mem_bot.mp this) hx ▸ (f.commutes _).trans (g.commutes _).symm⟩ instance _root_.AlgEquiv.subsingleton_left [Subsingleton (Subalgebra R A)] : Subsingleton (A ≃ₐ[R] B) := ⟨fun f g => AlgEquiv.ext fun x => AlgHom.ext_iff.mp (Subsingleton.elim f.toAlgHom g.toAlgHom) x⟩ instance _root_.AlgEquiv.subsingleton_right [Subsingleton (Subalgebra R B)] : Subsingleton (A ≃ₐ[R] B) := ⟨fun f g => by rw [← f.symm_symm, Subsingleton.elim f.symm g.symm, g.symm_symm]⟩ theorem range_val : S.val.range = S := ext <| Set.ext_iff.1 <| S.val.coe_range.trans Subtype.range_val instance : Unique (Subalgebra R R) := { inferInstanceAs (Inhabited (Subalgebra R R)) with uniq := by intro S refine le_antisymm ?_ bot_le intro _ _ simp only [Set.mem_range, mem_bot, id.map_eq_self, exists_apply_eq_apply, default] } /-- The map `S → T` when `S` is a subalgebra contained in the subalgebra `T`. This is the subalgebra version of `Submodule.inclusion`, or `Subring.inclusion` -/ def inclusion {S T : Subalgebra R A} (h : S ≤ T) : S →ₐ[R] T where toFun := Set.inclusion h map_one' := rfl map_add' _ _ := rfl map_mul' _ _ := rfl map_zero' := rfl commutes' _ := rfl theorem inclusion_injective {S T : Subalgebra R A} (h : S ≤ T) : Function.Injective (inclusion h) := fun _ _ => Subtype.ext ∘ Subtype.mk.inj @[simp] theorem inclusion_self {S : Subalgebra R A} : inclusion (le_refl S) = AlgHom.id R S := AlgHom.ext fun _x => Subtype.ext rfl @[simp] theorem inclusion_mk {S T : Subalgebra R A} (h : S ≤ T) (x : A) (hx : x ∈ S) : inclusion h ⟨x, hx⟩ = ⟨x, h hx⟩ := rfl theorem inclusion_right {S T : Subalgebra R A} (h : S ≤ T) (x : T) (m : (x : A) ∈ S) : inclusion h ⟨x, m⟩ = x := Subtype.ext rfl @[simp] theorem inclusion_inclusion {S T U : Subalgebra R A} (hst : S ≤ T) (htu : T ≤ U) (x : S) : inclusion htu (inclusion hst x) = inclusion (le_trans hst htu) x := Subtype.ext rfl @[simp] theorem coe_inclusion {S T : Subalgebra R A} (h : S ≤ T) (s : S) : (inclusion h s : A) = s := rfl /-- Two subalgebras that are equal are also equivalent as algebras. This is the `Subalgebra` version of `LinearEquiv.ofEq` and `Equiv.Set.ofEq`. -/ @[simps apply] def equivOfEq (S T : Subalgebra R A) (h : S = T) : S ≃ₐ[R] T where __ := LinearEquiv.ofEq _ _ (congr_arg toSubmodule h) toFun x := ⟨x, h ▸ x.2⟩ invFun x := ⟨x, h.symm ▸ x.2⟩ map_mul' _ _ := rfl commutes' _ := rfl @[simp] theorem equivOfEq_symm (S T : Subalgebra R A) (h : S = T) : (equivOfEq S T h).symm = equivOfEq T S h.symm := rfl @[simp] theorem equivOfEq_rfl (S : Subalgebra R A) : equivOfEq S S rfl = AlgEquiv.refl := by ext; rfl @[simp] theorem equivOfEq_trans (S T U : Subalgebra R A) (hST : S = T) (hTU : T = U) : (equivOfEq S T hST).trans (equivOfEq T U hTU) = equivOfEq S U (hST.trans hTU) := rfl section equivMapOfInjective variable (f : A →ₐ[R] B) theorem range_comp_val : (f.comp S.val).range = S.map f := by rw [AlgHom.range_comp, range_val] variable (hf : Function.Injective f) /-- A subalgebra is isomorphic to its image under an injective `AlgHom` -/ noncomputable def equivMapOfInjective : S ≃ₐ[R] S.map f := (AlgEquiv.ofInjective (f.comp S.val) (hf.comp Subtype.val_injective)).trans (equivOfEq _ _ (range_comp_val S f)) @[simp] theorem coe_equivMapOfInjective_apply (x : S) : ↑(equivMapOfInjective S f hf x) = f x := rfl end equivMapOfInjective /-! ## Actions by `Subalgebra`s These are just copies of the definitions about `Subsemiring` starting from `Subring.mulAction`. -/ section Actions variable {α β : Type*} /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [SMul A α] (S : Subalgebra R A) : SMul S α := inferInstanceAs (SMul S.toSubsemiring α) theorem smul_def [SMul A α] {S : Subalgebra R A} (g : S) (m : α) : g • m = (g : A) • m := rfl instance smulCommClass_left [SMul A β] [SMul α β] [SMulCommClass A α β] (S : Subalgebra R A) : SMulCommClass S α β := S.toSubsemiring.smulCommClass_left instance smulCommClass_right [SMul α β] [SMul A β] [SMulCommClass α A β] (S : Subalgebra R A) : SMulCommClass α S β := S.toSubsemiring.smulCommClass_right /-- Note that this provides `IsScalarTower S R R` which is needed by `smul_mul_assoc`. -/ instance isScalarTower_left [SMul α β] [SMul A α] [SMul A β] [IsScalarTower A α β] (S : Subalgebra R A) : IsScalarTower S α β := inferInstanceAs (IsScalarTower S.toSubsemiring α β) instance isScalarTower_mid {R S T : Type*} [CommSemiring R] [Semiring S] [AddCommMonoid T] [Algebra R S] [Module R T] [Module S T] [IsScalarTower R S T] (S' : Subalgebra R S) : IsScalarTower R S' T := ⟨fun _x y _z => (smul_assoc _ (y : S) _ : _)⟩ instance [SMul A α] [FaithfulSMul A α] (S : Subalgebra R A) : FaithfulSMul S α := inferInstanceAs (FaithfulSMul S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [MulAction A α] (S : Subalgebra R A) : MulAction S α := inferInstanceAs (MulAction S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [AddMonoid α] [DistribMulAction A α] (S : Subalgebra R A) : DistribMulAction S α := inferInstanceAs (DistribMulAction S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [Zero α] [SMulWithZero A α] (S : Subalgebra R A) : SMulWithZero S α := inferInstanceAs (SMulWithZero S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance [Zero α] [MulActionWithZero A α] (S : Subalgebra R A) : MulActionWithZero S α := inferInstanceAs (MulActionWithZero S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance moduleLeft [AddCommMonoid α] [Module A α] (S : Subalgebra R A) : Module S α := inferInstanceAs (Module S.toSubsemiring α) /-- The action by a subalgebra is the action by the underlying algebra. -/ instance toAlgebra {R A : Type*} [CommSemiring R] [CommSemiring A] [Semiring α] [Algebra R A] [Algebra A α] (S : Subalgebra R A) : Algebra S α := Algebra.ofSubsemiring S.toSubsemiring theorem algebraMap_eq {R A : Type*} [CommSemiring R] [CommSemiring A] [Semiring α] [Algebra R A] [Algebra A α] (S : Subalgebra R A) : algebraMap S α = (algebraMap A α).comp S.val := rfl @[simp] theorem rangeS_algebraMap {R A : Type*} [CommSemiring R] [CommSemiring A] [Algebra R A] (S : Subalgebra R A) : (algebraMap S A).rangeS = S.toSubsemiring := by rw [algebraMap_eq, Algebra.id.map_eq_id, RingHom.id_comp, ← toSubsemiring_subtype, Subsemiring.rangeS_subtype] @[simp] theorem range_algebraMap {R A : Type*} [CommRing R] [CommRing A] [Algebra R A] (S : Subalgebra R A) : (algebraMap S A).range = S.toSubring := by rw [algebraMap_eq, Algebra.id.map_eq_id, RingHom.id_comp, ← toSubring_subtype, Subring.range_subtype] instance noZeroSMulDivisors_top [NoZeroDivisors A] (S : Subalgebra R A) : NoZeroSMulDivisors S A := ⟨fun {c} x h => have : (c : A) = 0 ∨ x = 0 := eq_zero_or_eq_zero_of_mul_eq_zero h this.imp_left (@Subtype.ext_iff _ _ c 0).mpr⟩ end Actions section Center theorem _root_.Set.algebraMap_mem_center (r : R) : algebraMap R A r ∈ Set.center A := by simp only [Semigroup.mem_center_iff, commutes, forall_const] variable (R A) /-- The center of an algebra is the set of elements which commute with every element. They form a subalgebra. -/ def center : Subalgebra R A := { Subsemiring.center A with algebraMap_mem' := Set.algebraMap_mem_center } theorem coe_center : (center R A : Set A) = Set.center A := rfl @[simp] theorem center_toSubsemiring : (center R A).toSubsemiring = Subsemiring.center A := rfl @[simp] theorem center_toSubring (R A : Type*) [CommRing R] [Ring A] [Algebra R A] : (center R A).toSubring = Subring.center A := rfl @[simp] theorem center_eq_top (A : Type*) [CommSemiring A] [Algebra R A] : center R A = ⊤ := SetLike.coe_injective (Set.center_eq_univ A) variable {R A} instance : CommSemiring (center R A) := inferInstanceAs (CommSemiring (Subsemiring.center A)) instance {A : Type*} [Ring A] [Algebra R A] : CommRing (center R A) := inferInstanceAs (CommRing (Subring.center A)) theorem mem_center_iff {a : A} : a ∈ center R A ↔ ∀ b : A, b * a = a * b := Subsemigroup.mem_center_iff end Center section Centralizer @[simp] theorem _root_.Set.algebraMap_mem_centralizer {s : Set A} (r : R) : algebraMap R A r ∈ s.centralizer := fun _a _h => (Algebra.commutes _ _).symm variable (R) /-- The centralizer of a set as a subalgebra. -/ def centralizer (s : Set A) : Subalgebra R A := { Subsemiring.centralizer s with algebraMap_mem' := Set.algebraMap_mem_centralizer } @[simp, norm_cast] theorem coe_centralizer (s : Set A) : (centralizer R s : Set A) = s.centralizer := rfl theorem mem_centralizer_iff {s : Set A} {z : A} : z ∈ centralizer R s ↔ ∀ g ∈ s, g * z = z * g := Iff.rfl theorem center_le_centralizer (s) : center R A ≤ centralizer R s := s.center_subset_centralizer theorem centralizer_le (s t : Set A) (h : s ⊆ t) : centralizer R t ≤ centralizer R s := Set.centralizer_subset h @[simp] theorem centralizer_eq_top_iff_subset {s : Set A} : centralizer R s = ⊤ ↔ s ⊆ center R A := SetLike.ext'_iff.trans Set.centralizer_eq_top_iff_subset @[simp] theorem centralizer_univ : centralizer R Set.univ = center R A := SetLike.ext' (Set.centralizer_univ A) lemma le_centralizer_centralizer {s : Subalgebra R A} : s ≤ centralizer R (centralizer R (s : Set A)) := Set.subset_centralizer_centralizer @[simp] lemma centralizer_centralizer_centralizer {s : Set A} : centralizer R s.centralizer.centralizer = centralizer R s := by apply SetLike.coe_injective simp only [coe_centralizer, Set.centralizer_centralizer_centralizer] end Centralizer end Subalgebra section Nat variable {R : Type*} [Semiring R] /-- A subsemiring is an `ℕ`-subalgebra. -/ def subalgebraOfSubsemiring (S : Subsemiring R) : Subalgebra ℕ R := { S with algebraMap_mem' := fun i => natCast_mem S i } @[simp] theorem mem_subalgebraOfSubsemiring {x : R} {S : Subsemiring R} : x ∈ subalgebraOfSubsemiring S ↔ x ∈ S := Iff.rfl end Nat section Int variable {R : Type*} [Ring R] /-- A subring is a `ℤ`-subalgebra. -/ def subalgebraOfSubring (S : Subring R) : Subalgebra ℤ R := { S with algebraMap_mem' := fun i => Int.induction_on i (by simpa using S.zero_mem) (fun i ih => by simpa using S.add_mem ih S.one_mem) fun i ih => show ((-i - 1 : ℤ) : R) ∈ S by rw [Int.cast_sub, Int.cast_one] exact S.sub_mem ih S.one_mem } variable {S : Type*} [Semiring S] @[simp] theorem mem_subalgebraOfSubring {x : R} {S : Subring R} : x ∈ subalgebraOfSubring S ↔ x ∈ S := Iff.rfl end Int section Equalizer namespace AlgHom variable {R A B : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] variable {F : Type*} [FunLike F A B] [AlgHomClass F R A B] /-- The equalizer of two R-algebra homomorphisms -/ def equalizer (ϕ ψ : F) : Subalgebra R A where carrier := { a | ϕ a = ψ a } zero_mem' := by simp only [Set.mem_setOf_eq, map_zero] one_mem' := by simp only [Set.mem_setOf_eq, map_one] add_mem' {x y} (hx : ϕ x = ψ x) (hy : ϕ y = ψ y) := by rw [Set.mem_setOf_eq, map_add, map_add, hx, hy] mul_mem' {x y} (hx : ϕ x = ψ x) (hy : ϕ y = ψ y) := by rw [Set.mem_setOf_eq, map_mul, map_mul, hx, hy] algebraMap_mem' x := by simp only [Set.mem_setOf_eq, AlgHomClass.commutes] @[simp] theorem mem_equalizer (φ ψ : F) (x : A) : x ∈ equalizer φ ψ ↔ φ x = ψ x := Iff.rfl theorem equalizer_toSubmodule {φ ψ : F} : Subalgebra.toSubmodule (equalizer φ ψ) = LinearMap.eqLocus φ ψ := rfl @[simp] theorem equalizer_eq_top {φ ψ : F} : equalizer φ ψ = ⊤ ↔ φ = ψ := by simp [SetLike.ext_iff, DFunLike.ext_iff] @[simp] theorem equalizer_same (φ : F) : equalizer φ φ = ⊤ := equalizer_eq_top.2 rfl theorem le_equalizer {φ ψ : F} {S : Subalgebra R A} : S ≤ equalizer φ ψ ↔ Set.EqOn φ ψ S := Iff.rfl theorem eqOn_sup {φ ψ : F} {S T : Subalgebra R A} (hS : Set.EqOn φ ψ S) (hT : Set.EqOn φ ψ T) : Set.EqOn φ ψ ↑(S ⊔ T) := by rw [← le_equalizer] at hS hT ⊢ exact sup_le hS hT theorem ext_on_codisjoint {φ ψ : F} {S T : Subalgebra R A} (hST : Codisjoint S T) (hS : Set.EqOn φ ψ S) (hT : Set.EqOn φ ψ T) : φ = ψ := DFunLike.ext _ _ fun _ ↦ eqOn_sup hS hT <| hST.eq_top.symm ▸ trivial end AlgHom end Equalizer section MapComap namespace Subalgebra variable {R A B : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] theorem map_comap_eq (f : A →ₐ[R] B) (S : Subalgebra R B) : (S.comap f).map f = S ⊓ f.range := SetLike.coe_injective Set.image_preimage_eq_inter_range theorem map_comap_eq_self {f : A →ₐ[R] B} {S : Subalgebra R B} (h : S ≤ f.range) : (S.comap f).map f = S := by simpa only [inf_of_le_left h] using map_comap_eq f S theorem map_comap_eq_self_of_surjective {f : A →ₐ[R] B} (hf : Function.Surjective f) (S : Subalgebra R B) : (S.comap f).map f = S := map_comap_eq_self <| by simp [(Algebra.range_top_iff_surjective f).2 hf] theorem comap_map_eq_self_of_injective {f : A →ₐ[R] B} (hf : Function.Injective f) (S : Subalgebra R A) : (S.map f).comap f = S := SetLike.coe_injective (Set.preimage_image_eq _ hf) end Subalgebra end MapComap
Algebra\Algebra\Subalgebra\Directed.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Data.Set.UnionLift /-! # Subalgebras and directed Unions of sets ## Main results * `Subalgebra.coe_iSup_of_directed`: a directed supremum consists of the union of the algebras * `Subalgebra.iSupLift`: define an algebra homomorphism on a directed supremum of subalgebras by defining it on each subalgebra, and proving that it agrees on the intersection of subalgebras. -/ namespace Subalgebra open Algebra variable {R A B : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] variable (S : Subalgebra R A) variable {ι : Type*} [Nonempty ι] {K : ι → Subalgebra R A} theorem coe_iSup_of_directed (dir : Directed (· ≤ ·) K) : ↑(iSup K) = ⋃ i, (K i : Set A) := let s : Subalgebra R A := { __ := Subsemiring.copy _ _ (Subsemiring.coe_iSup_of_directed dir).symm algebraMap_mem' := fun _ ↦ Set.mem_iUnion.2 ⟨Classical.arbitrary ι, Subalgebra.algebraMap_mem _ _⟩ } have : iSup K = s := le_antisymm (iSup_le fun i ↦ le_iSup (fun i ↦ (K i : Set A)) i) (Set.iUnion_subset fun _ ↦ le_iSup K _) this.symm ▸ rfl variable (K) -- Porting note (#11215): TODO: turn `hT` into an assumption `T ≤ iSup K`. -- That's what `Set.iUnionLift` needs -- Porting note: the proofs of `map_{zero,one,add,mul}` got a bit uglier, probably unification trbls /-- Define an algebra homomorphism on a directed supremum of subalgebras by defining it on each subalgebra, and proving that it agrees on the intersection of subalgebras. -/ noncomputable def iSupLift (dir : Directed (· ≤ ·) K) (f : ∀ i, K i →ₐ[R] B) (hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)) (T : Subalgebra R A) (hT : T = iSup K): ↥T →ₐ[R] B := { toFun := Set.iUnionLift (fun i => ↑(K i)) (fun i x => f i x) (fun i j x hxi hxj => by let ⟨k, hik, hjk⟩ := dir i j dsimp rw [hf i k hik, hf j k hjk] rfl) T (by rw [hT, coe_iSup_of_directed dir]) map_one' := by apply Set.iUnionLift_const _ (fun _ => 1) <;> simp map_zero' := by dsimp; apply Set.iUnionLift_const _ (fun _ => 0) <;> simp map_mul' := by subst hT; dsimp apply Set.iUnionLift_binary (coe_iSup_of_directed dir) dir _ (fun _ => (· * ·)) all_goals simp map_add' := by subst hT; dsimp apply Set.iUnionLift_binary (coe_iSup_of_directed dir) dir _ (fun _ => (· + ·)) all_goals simp commutes' := fun r => by dsimp apply Set.iUnionLift_const _ (fun _ => algebraMap R _ r) <;> simp } @[simp] theorem iSupLift_inclusion {dir : Directed (· ≤ ·) K} {f : ∀ i, K i →ₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : Subalgebra R A} {hT : T = iSup K} {i : ι} (x : K i) (h : K i ≤ T) : iSupLift K dir f hf T hT (inclusion h x) = f i x := by dsimp [iSupLift, inclusion] rw [Set.iUnionLift_inclusion] @[simp] theorem iSupLift_comp_inclusion {dir : Directed (· ≤ ·) K} {f : ∀ i, K i →ₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : Subalgebra R A} {hT : T = iSup K} {i : ι} (h : K i ≤ T) : (iSupLift K dir f hf T hT).comp (inclusion h) = f i := by ext; simp @[simp] theorem iSupLift_mk {dir : Directed (· ≤ ·) K} {f : ∀ i, K i →ₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : Subalgebra R A} {hT : T = iSup K} {i : ι} (x : K i) (hx : (x : A) ∈ T) : iSupLift K dir f hf T hT ⟨x, hx⟩ = f i x := by dsimp [iSupLift, inclusion] rw [Set.iUnionLift_mk] theorem iSupLift_of_mem {dir : Directed (· ≤ ·) K} {f : ∀ i, K i →ₐ[R] B} {hf : ∀ (i j : ι) (h : K i ≤ K j), f i = (f j).comp (inclusion h)} {T : Subalgebra R A} {hT : T = iSup K} {i : ι} (x : T) (hx : (x : A) ∈ K i) : iSupLift K dir f hf T hT x = f i ⟨x, hx⟩ := by dsimp [iSupLift, inclusion] rw [Set.iUnionLift_of_mem] end Subalgebra
Algebra\Algebra\Subalgebra\MulOpposite.lean
/- Copyright (c) 2024 Jz Pan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jz Pan -/ import Mathlib.Algebra.Ring.Subring.MulOpposite import Mathlib.Algebra.Algebra.Subalgebra.Basic /-! # Subalgebras of opposite rings For every ring `A` over a commutative ring `R`, we construct an equivalence between subalgebras of `A / R` and that of `Aᵐᵒᵖ / R`. -/ namespace Subalgebra section Semiring variable {ι : Sort*} {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] /-- Pull a subalgebra back to an opposite subalgebra along `MulOpposite.unop` -/ @[simps toSubsemiring] protected def op (S : Subalgebra R A) : Subalgebra R Aᵐᵒᵖ where toSubsemiring := S.toSubsemiring.op algebraMap_mem' := S.algebraMap_mem @[simp, norm_cast] theorem op_coe (S : Subalgebra R A) : S.op = MulOpposite.unop ⁻¹' (S : Set A) := rfl @[simp] theorem mem_op {x : Aᵐᵒᵖ} {S : Subalgebra R A} : x ∈ S.op ↔ x.unop ∈ S := Iff.rfl /-- Pull an subalgebra subring back to a subalgebra along `MulOpposite.op` -/ @[simps toSubsemiring] protected def unop (S : Subalgebra R Aᵐᵒᵖ) : Subalgebra R A where toSubsemiring := S.toSubsemiring.unop algebraMap_mem' := S.algebraMap_mem @[simp, norm_cast] theorem unop_coe (S : Subalgebra R Aᵐᵒᵖ) : S.unop = MulOpposite.op ⁻¹' (S : Set Aᵐᵒᵖ) := rfl @[simp] theorem mem_unop {x : A} {S : Subalgebra R Aᵐᵒᵖ} : x ∈ S.unop ↔ MulOpposite.op x ∈ S := Iff.rfl @[simp] theorem unop_op (S : Subalgebra R A) : S.op.unop = S := rfl @[simp] theorem op_unop (S : Subalgebra R Aᵐᵒᵖ) : S.unop.op = S := rfl /-! ### Lattice results -/ theorem op_le_iff {S₁ : Subalgebra R A} {S₂ : Subalgebra R Aᵐᵒᵖ} : S₁.op ≤ S₂ ↔ S₁ ≤ S₂.unop := MulOpposite.op_surjective.forall theorem le_op_iff {S₁ : Subalgebra R Aᵐᵒᵖ} {S₂ : Subalgebra R A} : S₁ ≤ S₂.op ↔ S₁.unop ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem op_le_op_iff {S₁ S₂ : Subalgebra R A} : S₁.op ≤ S₂.op ↔ S₁ ≤ S₂ := MulOpposite.op_surjective.forall @[simp] theorem unop_le_unop_iff {S₁ S₂ : Subalgebra R Aᵐᵒᵖ} : S₁.unop ≤ S₂.unop ↔ S₁ ≤ S₂ := MulOpposite.unop_surjective.forall /-- A subalgebra `S` of `A / R` determines a subring `S.op` of the opposite ring `Aᵐᵒᵖ / R`. -/ @[simps] def opEquiv : Subalgebra R A ≃o Subalgebra R Aᵐᵒᵖ where toFun := Subalgebra.op invFun := Subalgebra.unop left_inv := unop_op right_inv := op_unop map_rel_iff' := op_le_op_iff @[simp] theorem op_bot : (⊥ : Subalgebra R A).op = ⊥ := opEquiv.map_bot @[simp] theorem unop_bot : (⊥ : Subalgebra R Aᵐᵒᵖ).unop = ⊥ := opEquiv.symm.map_bot @[simp] theorem op_top : (⊤ : Subalgebra R A).op = ⊤ := opEquiv.map_top @[simp] theorem unop_top : (⊤ : Subalgebra R Aᵐᵒᵖ).unop = ⊤ := opEquiv.symm.map_top theorem op_sup (S₁ S₂ : Subalgebra R A) : (S₁ ⊔ S₂).op = S₁.op ⊔ S₂.op := opEquiv.map_sup _ _ theorem unop_sup (S₁ S₂ : Subalgebra R Aᵐᵒᵖ) : (S₁ ⊔ S₂).unop = S₁.unop ⊔ S₂.unop := opEquiv.symm.map_sup _ _ theorem op_inf (S₁ S₂ : Subalgebra R A) : (S₁ ⊓ S₂).op = S₁.op ⊓ S₂.op := opEquiv.map_inf _ _ theorem unop_inf (S₁ S₂ : Subalgebra R Aᵐᵒᵖ) : (S₁ ⊓ S₂).unop = S₁.unop ⊓ S₂.unop := opEquiv.symm.map_inf _ _ theorem op_sSup (S : Set (Subalgebra R A)) : (sSup S).op = sSup (.unop ⁻¹' S) := opEquiv.map_sSup_eq_sSup_symm_preimage _ theorem unop_sSup (S : Set (Subalgebra R Aᵐᵒᵖ)) : (sSup S).unop = sSup (.op ⁻¹' S) := opEquiv.symm.map_sSup_eq_sSup_symm_preimage _ theorem op_sInf (S : Set (Subalgebra R A)) : (sInf S).op = sInf (.unop ⁻¹' S) := opEquiv.map_sInf_eq_sInf_symm_preimage _ theorem unop_sInf (S : Set (Subalgebra R Aᵐᵒᵖ)) : (sInf S).unop = sInf (.op ⁻¹' S) := opEquiv.symm.map_sInf_eq_sInf_symm_preimage _ theorem op_iSup (S : ι → Subalgebra R A) : (iSup S).op = ⨆ i, (S i).op := opEquiv.map_iSup _ theorem unop_iSup (S : ι → Subalgebra R Aᵐᵒᵖ) : (iSup S).unop = ⨆ i, (S i).unop := opEquiv.symm.map_iSup _ theorem op_iInf (S : ι → Subalgebra R A) : (iInf S).op = ⨅ i, (S i).op := opEquiv.map_iInf _ theorem unop_iInf (S : ι → Subalgebra R Aᵐᵒᵖ) : (iInf S).unop = ⨅ i, (S i).unop := opEquiv.symm.map_iInf _ theorem op_adjoin (s : Set A) : (Algebra.adjoin R s).op = Algebra.adjoin R (MulOpposite.unop ⁻¹' s) := by apply toSubsemiring_injective simp_rw [Algebra.adjoin, op_toSubsemiring, Subsemiring.op_closure, Set.preimage_union] congr with x simp_rw [Set.mem_preimage, Set.mem_range, MulOpposite.algebraMap_apply] congr! rw [← MulOpposite.op_injective.eq_iff (b := x.unop), MulOpposite.op_unop] theorem unop_adjoin (s : Set Aᵐᵒᵖ) : (Algebra.adjoin R s).unop = Algebra.adjoin R (MulOpposite.op ⁻¹' s) := by apply toSubsemiring_injective simp_rw [Algebra.adjoin, unop_toSubsemiring, Subsemiring.unop_closure, Set.preimage_union] congr with x simp /-- Bijection between a subalgebra `S` and its opposite. -/ @[simps!] def linearEquivOp (S : Subalgebra R A) : S ≃ₗ[R] S.op where __ := S.toSubsemiring.addEquivOp map_smul' _ _ := rfl /-- Bijection between a subalgebra `S` and `MulOpposite` of its opposite. -/ @[simps!] def algEquivOpMop (S : Subalgebra R A) : S ≃ₐ[R] (S.op)ᵐᵒᵖ where __ := S.toSubsemiring.ringEquivOpMop commutes' _ := rfl /-- Bijection between `MulOpposite` of a subalgebra `S` and its opposite. -/ @[simps!] def mopAlgEquivOp (S : Subalgebra R A) : Sᵐᵒᵖ ≃ₐ[R] S.op where __ := S.toSubsemiring.mopRingEquivOp commutes' _ := rfl end Semiring section Ring variable {R A : Type*} [CommRing R] [Ring A] [Algebra R A] @[simp] theorem op_toSubring (S : Subalgebra R A) : S.op.toSubring = S.toSubring.op := rfl @[simp] theorem unop_toSubring (S : Subalgebra R Aᵐᵒᵖ) : S.unop.toSubring = S.toSubring.unop := rfl end Ring end Subalgebra
Algebra\Algebra\Subalgebra\Operations.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang, Antoine Chambert-Loir -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.RingTheory.Ideal.Maps /-! # More operations on subalgebras In this file we relate the definitions in `Mathlib.RingTheory.Ideal.Operations` to subalgebras. The contents of this file are somewhat random since both `Mathlib.Algebra.Algebra.Subalgebra.Basic` and `Mathlib.RingTheory.Ideal.Operations` are somewhat of a grab-bag of definitions, and this is whatever ends up in the intersection. -/ namespace AlgHom variable {R A B : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] theorem ker_rangeRestrict (f : A →ₐ[R] B) : RingHom.ker f.rangeRestrict = RingHom.ker f := Ideal.ext fun _ ↦ Subtype.ext_iff end AlgHom namespace Subalgebra open Algebra variable {R S : Type*} [CommSemiring R] [CommRing S] [Algebra R S] variable (S' : Subalgebra R S) /-- Suppose we are given `∑ i, lᵢ * sᵢ = 1` ∈ `S`, and `S'` a subalgebra of `S` that contains `lᵢ` and `sᵢ`. To check that an `x : S` falls in `S'`, we only need to show that `sᵢ ^ n • x ∈ S'` for some `n` for each `sᵢ`. -/ theorem mem_of_finset_sum_eq_one_of_pow_smul_mem {ι : Type*} (ι' : Finset ι) (s : ι → S) (l : ι → S) (e : ∑ i ∈ ι', l i * s i = 1) (hs : ∀ i, s i ∈ S') (hl : ∀ i, l i ∈ S') (x : S) (H : ∀ i, ∃ n : ℕ, (s i ^ n : S) • x ∈ S') : x ∈ S' := by -- Porting note: needed to add this instance let _i : Algebra { x // x ∈ S' } { x // x ∈ S' } := Algebra.id _ suffices x ∈ Subalgebra.toSubmodule (Algebra.ofId S' S).range by obtain ⟨x, rfl⟩ := this exact x.2 choose n hn using H let s' : ι → S' := fun x => ⟨s x, hs x⟩ let l' : ι → S' := fun x => ⟨l x, hl x⟩ have e' : ∑ i ∈ ι', l' i * s' i = 1 := by ext show S'.subtype (∑ i ∈ ι', l' i * s' i) = 1 simpa only [map_sum, map_mul] using e have : Ideal.span (s' '' ι') = ⊤ := by rw [Ideal.eq_top_iff_one, ← e'] apply sum_mem intros i hi exact Ideal.mul_mem_left _ _ <| Ideal.subset_span <| Set.mem_image_of_mem s' hi let N := ι'.sup n have hN := Ideal.span_pow_eq_top _ this N apply (Algebra.ofId S' S).range.toSubmodule.mem_of_span_top_of_smul_mem _ hN rintro ⟨_, _, ⟨i, hi, rfl⟩, rfl⟩ change s' i ^ N • x ∈ _ rw [← tsub_add_cancel_of_le (show n i ≤ N from Finset.le_sup hi), pow_add, mul_smul] refine Submodule.smul_mem _ (⟨_, pow_mem (hs i) _⟩ : S') ?_ exact ⟨⟨_, hn i⟩, rfl⟩ theorem mem_of_span_eq_top_of_smul_pow_mem (s : Set S) (l : s →₀ S) (hs : Finsupp.total s S S (↑) l = 1) (hs' : s ⊆ S') (hl : ∀ i, l i ∈ S') (x : S) (H : ∀ r : s, ∃ n : ℕ, (r : S) ^ n • x ∈ S') : x ∈ S' := mem_of_finset_sum_eq_one_of_pow_smul_mem S' l.support (↑) l hs (fun x => hs' x.2) hl x H end Subalgebra
Algebra\Algebra\Subalgebra\Order.lean
/- Copyright (c) 2021 Damiano Testa. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Damiano Testa -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Algebra.Module.Submodule.Order import Mathlib.Algebra.Ring.Subsemiring.Order import Mathlib.Algebra.Ring.Subring.Order /-! # Order instances on subalgebras -/ namespace Subalgebra variable {R A : Type*} instance toOrderedSemiring [CommSemiring R] [OrderedSemiring A] [Algebra R A] (S : Subalgebra R A) : OrderedSemiring S := S.toSubsemiring.toOrderedSemiring instance toStrictOrderedSemiring [CommSemiring R] [StrictOrderedSemiring A] [Algebra R A] (S : Subalgebra R A) : StrictOrderedSemiring S := S.toSubsemiring.toStrictOrderedSemiring instance toOrderedCommSemiring [CommSemiring R] [OrderedCommSemiring A] [Algebra R A] (S : Subalgebra R A) : OrderedCommSemiring S := S.toSubsemiring.toOrderedCommSemiring instance toStrictOrderedCommSemiring [CommSemiring R] [StrictOrderedCommSemiring A] [Algebra R A] (S : Subalgebra R A) : StrictOrderedCommSemiring S := S.toSubsemiring.toStrictOrderedCommSemiring instance toOrderedRing [CommRing R] [OrderedRing A] [Algebra R A] (S : Subalgebra R A) : OrderedRing S := S.toSubring.toOrderedRing instance toOrderedCommRing [CommRing R] [OrderedCommRing A] [Algebra R A] (S : Subalgebra R A) : OrderedCommRing S := S.toSubring.toOrderedCommRing instance toLinearOrderedSemiring [CommSemiring R] [LinearOrderedSemiring A] [Algebra R A] (S : Subalgebra R A) : LinearOrderedSemiring S := S.toSubsemiring.toLinearOrderedSemiring instance toLinearOrderedCommSemiring [CommSemiring R] [LinearOrderedCommSemiring A] [Algebra R A] (S : Subalgebra R A) : LinearOrderedCommSemiring S := S.toSubsemiring.toLinearOrderedCommSemiring instance toLinearOrderedRing [CommRing R] [LinearOrderedRing A] [Algebra R A] (S : Subalgebra R A) : LinearOrderedRing S := S.toSubring.toLinearOrderedRing instance toLinearOrderedCommRing [CommRing R] [LinearOrderedCommRing A] [Algebra R A] (S : Subalgebra R A) : LinearOrderedCommRing S := S.toSubring.toLinearOrderedCommRing end Subalgebra
Algebra\Algebra\Subalgebra\Pointwise.lean
/- Copyright (c) 2021 Eric Weiser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Algebra.Operations import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Algebra.Ring.Subring.Pointwise import Mathlib.RingTheory.Adjoin.Basic /-! # Pointwise actions on subalgebras. If `R'` acts on an `R`-algebra `A` (so that `R'` and `R` actions commute) then we get an `R'` action on the collection of `R`-subalgebras. -/ namespace Subalgebra section Pointwise variable {R : Type*} {A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] theorem mul_toSubmodule_le (S T : Subalgebra R A) : (Subalgebra.toSubmodule S)* (Subalgebra.toSubmodule T) ≤ Subalgebra.toSubmodule (S ⊔ T) := by rw [Submodule.mul_le] intro y hy z hz show y * z ∈ S ⊔ T exact mul_mem (Algebra.mem_sup_left hy) (Algebra.mem_sup_right hz) /-- As submodules, subalgebras are idempotent. -/ @[simp] theorem mul_self (S : Subalgebra R A) : (Subalgebra.toSubmodule S) * (Subalgebra.toSubmodule S) = (Subalgebra.toSubmodule S) := by apply le_antisymm · refine (mul_toSubmodule_le _ _).trans_eq ?_ rw [sup_idem] · intro x hx1 rw [← mul_one x] exact Submodule.mul_mem_mul hx1 (show (1 : A) ∈ S from one_mem S) /-- When `A` is commutative, `Subalgebra.mul_toSubmodule_le` is strict. -/ theorem mul_toSubmodule {R : Type*} {A : Type*} [CommSemiring R] [CommSemiring A] [Algebra R A] (S T : Subalgebra R A) : (Subalgebra.toSubmodule S) * (Subalgebra.toSubmodule T) = Subalgebra.toSubmodule (S ⊔ T) := by refine le_antisymm (mul_toSubmodule_le _ _) ?_ rintro x (hx : x ∈ Algebra.adjoin R (S ∪ T : Set A)) refine Algebra.adjoin_induction hx (fun x hx => ?_) (fun r => ?_) (fun _ _ => Submodule.add_mem _) fun x y hx hy => ?_ · cases' hx with hxS hxT · rw [← mul_one x] exact Submodule.mul_mem_mul hxS (show (1 : A) ∈ T from one_mem T) · rw [← one_mul x] exact Submodule.mul_mem_mul (show (1 : A) ∈ S from one_mem S) hxT · rw [← one_mul (algebraMap _ _ _)] exact Submodule.mul_mem_mul (show (1 : A) ∈ S from one_mem S) (algebraMap_mem T _) have := Submodule.mul_mem_mul hx hy rwa [mul_assoc, mul_comm _ (Subalgebra.toSubmodule T), ← mul_assoc _ _ (Subalgebra.toSubmodule S), mul_self, mul_comm (Subalgebra.toSubmodule T), ← mul_assoc, mul_self] at this variable {R' : Type*} [Semiring R'] [MulSemiringAction R' A] [SMulCommClass R' R A] /-- The action on a subalgebra corresponding to applying the action to every element. This is available as an instance in the `Pointwise` locale. -/ protected def pointwiseMulAction : MulAction R' (Subalgebra R A) where smul a S := S.map (MulSemiringAction.toAlgHom _ _ a) one_smul S := (congr_arg (fun f => S.map f) (AlgHom.ext <| one_smul R')).trans S.map_id mul_smul _a₁ _a₂ S := (congr_arg (fun f => S.map f) (AlgHom.ext <| mul_smul _ _)).trans (S.map_map _ _).symm scoped[Pointwise] attribute [instance] Subalgebra.pointwiseMulAction open Pointwise @[simp] theorem coe_pointwise_smul (m : R') (S : Subalgebra R A) : ↑(m • S) = m • (S : Set A) := rfl @[simp] theorem pointwise_smul_toSubsemiring (m : R') (S : Subalgebra R A) : (m • S).toSubsemiring = m • S.toSubsemiring := rfl @[simp] theorem pointwise_smul_toSubmodule (m : R') (S : Subalgebra R A) : Subalgebra.toSubmodule (m • S) = m • Subalgebra.toSubmodule S := rfl @[simp] theorem pointwise_smul_toSubring {R' R A : Type*} [Semiring R'] [CommRing R] [Ring A] [MulSemiringAction R' A] [Algebra R A] [SMulCommClass R' R A] (m : R') (S : Subalgebra R A) : (m • S).toSubring = m • S.toSubring := rfl theorem smul_mem_pointwise_smul (m : R') (r : A) (S : Subalgebra R A) : r ∈ S → m • r ∈ m • S := (Set.smul_mem_smul_set : _ → _ ∈ m • (S : Set A)) instance : CovariantClass R' (Subalgebra R A) HSMul.hSMul LE.le := ⟨fun _ _ => map_mono⟩ end Pointwise end Subalgebra
Algebra\Algebra\Subalgebra\Prod.lean
/- Copyright (c) 2018 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Riccardo Brasca -/ import Mathlib.Algebra.Algebra.Prod import Mathlib.Algebra.Algebra.Subalgebra.Basic /-! # Products of subalgebras In this file we define the product of two subalgebras as a subalgebra of the product algebra. ## Main definitions * `Subalgebra.prod`: the product of two subalgebras. -/ namespace Subalgebra open Algebra variable {R A B : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [Semiring B] [Algebra R B] variable (S : Subalgebra R A) (S₁ : Subalgebra R B) /-- The product of two subalgebras is a subalgebra. -/ def prod : Subalgebra R (A × B) := { S.toSubsemiring.prod S₁.toSubsemiring with carrier := S ×ˢ S₁ algebraMap_mem' := fun _ => ⟨algebraMap_mem _ _, algebraMap_mem _ _⟩ } @[simp] theorem coe_prod : (prod S S₁ : Set (A × B)) = (S : Set A) ×ˢ (S₁ : Set B) := rfl open Subalgebra in theorem prod_toSubmodule : toSubmodule (S.prod S₁) = (toSubmodule S).prod (toSubmodule S₁) := rfl @[simp] theorem mem_prod {S : Subalgebra R A} {S₁ : Subalgebra R B} {x : A × B} : x ∈ prod S S₁ ↔ x.1 ∈ S ∧ x.2 ∈ S₁ := Set.mem_prod @[simp] theorem prod_top : (prod ⊤ ⊤ : Subalgebra R (A × B)) = ⊤ := by ext; simp theorem prod_mono {S T : Subalgebra R A} {S₁ T₁ : Subalgebra R B} : S ≤ T → S₁ ≤ T₁ → prod S S₁ ≤ prod T T₁ := Set.prod_mono @[simp] theorem prod_inf_prod {S T : Subalgebra R A} {S₁ T₁ : Subalgebra R B} : S.prod S₁ ⊓ T.prod T₁ = (S ⊓ T).prod (S₁ ⊓ T₁) := SetLike.coe_injective Set.prod_inter_prod end Subalgebra
Algebra\Algebra\Subalgebra\Rank.lean
/- Copyright (c) 2024 Jz Pan. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jz Pan -/ import Mathlib.LinearAlgebra.Dimension.Free import Mathlib.LinearAlgebra.Dimension.Finite import Mathlib.LinearAlgebra.FreeModule.StrongRankCondition /-! # Some results on the ranks of subalgebras This file contains some results on the ranks of subalgebras, which are corollaries of `rank_mul_rank`. Since their proof essentially depends on the fact that a non-trivial commutative ring satisfies strong rank condition, we put them into a separate file. -/ open FiniteDimensional namespace Subalgebra variable {R S : Type*} [CommRing R] [CommRing S] [Algebra R S] (A B : Subalgebra R S) section variable [Module.Free R A] [Module.Free A (Algebra.adjoin A (B : Set S))] theorem rank_sup_eq_rank_left_mul_rank_of_free : Module.rank R ↥(A ⊔ B) = Module.rank R A * Module.rank A (Algebra.adjoin A (B : Set S)) := by rcases subsingleton_or_nontrivial R with _ | _ · haveI := Module.subsingleton R S; simp nontriviality S using rank_subsingleton' letI : Algebra A (Algebra.adjoin A (B : Set S)) := Subalgebra.algebra _ letI : SMul A (Algebra.adjoin A (B : Set S)) := Algebra.toSMul haveI : IsScalarTower R A (Algebra.adjoin A (B : Set S)) := IsScalarTower.of_algebraMap_eq (congrFun rfl) rw [rank_mul_rank R A (Algebra.adjoin A (B : Set S))] change _ = Module.rank R ((Algebra.adjoin A (B : Set S)).restrictScalars R) rw [Algebra.restrictScalars_adjoin]; rfl theorem finrank_sup_eq_finrank_left_mul_finrank_of_free : finrank R ↥(A ⊔ B) = finrank R A * finrank A (Algebra.adjoin A (B : Set S)) := by simpa only [map_mul] using congr(Cardinal.toNat $(rank_sup_eq_rank_left_mul_rank_of_free A B)) theorem finrank_left_dvd_finrank_sup_of_free : finrank R A ∣ finrank R ↥(A ⊔ B) := ⟨_, finrank_sup_eq_finrank_left_mul_finrank_of_free A B⟩ end section variable [Module.Free R B] [Module.Free B (Algebra.adjoin B (A : Set S))] theorem rank_sup_eq_rank_right_mul_rank_of_free : Module.rank R ↥(A ⊔ B) = Module.rank R B * Module.rank B (Algebra.adjoin B (A : Set S)) := by rw [sup_comm, rank_sup_eq_rank_left_mul_rank_of_free] theorem finrank_sup_eq_finrank_right_mul_finrank_of_free : finrank R ↥(A ⊔ B) = finrank R B * finrank B (Algebra.adjoin B (A : Set S)) := by rw [sup_comm, finrank_sup_eq_finrank_left_mul_finrank_of_free] theorem finrank_right_dvd_finrank_sup_of_free : finrank R B ∣ finrank R ↥(A ⊔ B) := ⟨_, finrank_sup_eq_finrank_right_mul_finrank_of_free A B⟩ end end Subalgebra
Algebra\Algebra\Subalgebra\Tower.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Anne Baanen -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Algebra.Algebra.Tower /-! # Subalgebras in towers of algebras In this file we prove facts about subalgebras in towers of algebra. An algebra tower A/S/R is expressed by having instances of `Algebra A S`, `Algebra R S`, `Algebra R A` and `IsScalarTower R S A`, the later asserting the compatibility condition `(r • s) • a = r • (s • a)`. ## Main results * `IsScalarTower.Subalgebra`: if `A/S/R` is a tower and `S₀` is a subalgebra between `S` and `R`, then `A/S/S₀` is a tower * `IsScalarTower.Subalgebra'`: if `A/S/R` is a tower and `S₀` is a subalgebra between `S` and `R`, then `A/S₀/R` is a tower * `Subalgebra.restrictScalars`: turn an `S`-subalgebra of `A` into an `R`-subalgebra of `A`, given that `A/S/R` is a tower -/ open Pointwise universe u v w u₁ v₁ variable (R : Type u) (S : Type v) (A : Type w) (B : Type u₁) (M : Type v₁) namespace Algebra variable [CommSemiring R] [Semiring A] [Algebra R A] variable [AddCommMonoid M] [Module R M] [Module A M] [IsScalarTower R A M] variable {A} theorem lmul_algebraMap (x : R) : Algebra.lmul R A (algebraMap R A x) = Algebra.lsmul R R A x := Eq.symm <| LinearMap.ext <| smul_def x end Algebra namespace IsScalarTower section Semiring variable [CommSemiring R] [CommSemiring S] [Semiring A] variable [Algebra R S] [Algebra S A] instance subalgebra (S₀ : Subalgebra R S) : IsScalarTower S₀ S A := of_algebraMap_eq fun _ ↦ rfl variable [Algebra R A] [IsScalarTower R S A] instance subalgebra' (S₀ : Subalgebra R S) : IsScalarTower R S₀ A := @IsScalarTower.of_algebraMap_eq R S₀ A _ _ _ _ _ _ fun _ ↦ (IsScalarTower.algebraMap_apply R S A _ : _) end Semiring end IsScalarTower namespace Subalgebra open IsScalarTower section Semiring variable {S A B} [CommSemiring R] [CommSemiring S] [Semiring A] [Semiring B] variable [Algebra R S] [Algebra S A] [Algebra R A] [Algebra S B] [Algebra R B] variable [IsScalarTower R S A] [IsScalarTower R S B] /-- Given a tower `A / ↥U / S / R` of algebras, where `U` is an `S`-subalgebra of `A`, reinterpret `U` as an `R`-subalgebra of `A`. -/ def restrictScalars (U : Subalgebra S A) : Subalgebra R A := { U with algebraMap_mem' := fun x ↦ by rw [algebraMap_apply R S A] exact U.algebraMap_mem _ } @[simp] theorem coe_restrictScalars {U : Subalgebra S A} : (restrictScalars R U : Set A) = (U : Set A) := rfl @[simp] theorem restrictScalars_top : restrictScalars R (⊤ : Subalgebra S A) = ⊤ := SetLike.coe_injective <| by dsimp -- Porting note: why does `rfl` not work instead of `by dsimp`? @[simp] theorem restrictScalars_toSubmodule {U : Subalgebra S A} : Subalgebra.toSubmodule (U.restrictScalars R) = U.toSubmodule.restrictScalars R := SetLike.coe_injective rfl @[simp] theorem mem_restrictScalars {U : Subalgebra S A} {x : A} : x ∈ restrictScalars R U ↔ x ∈ U := Iff.rfl theorem restrictScalars_injective : Function.Injective (restrictScalars R : Subalgebra S A → Subalgebra R A) := fun U V H ↦ ext fun x ↦ by rw [← mem_restrictScalars R, H, mem_restrictScalars] /-- Produces an `R`-algebra map from `U.restrictScalars R` given an `S`-algebra map from `U`. This is a special case of `AlgHom.restrictScalars` that can be helpful in elaboration. -/ @[simp] def ofRestrictScalars (U : Subalgebra S A) (f : U →ₐ[S] B) : U.restrictScalars R →ₐ[R] B := f.restrictScalars R end Semiring section CommSemiring @[simp] lemma range_isScalarTower_toAlgHom [CommSemiring R] [CommSemiring A] [Algebra R A] (S : Subalgebra R A) : LinearMap.range (IsScalarTower.toAlgHom R S A) = Subalgebra.toSubmodule S := by ext simp only [← Submodule.range_subtype (Subalgebra.toSubmodule S), LinearMap.mem_range, IsScalarTower.coe_toAlgHom', Subalgebra.mem_toSubmodule] rfl end CommSemiring end Subalgebra namespace IsScalarTower open Subalgebra variable [CommSemiring R] [CommSemiring S] [CommSemiring A] variable [Algebra R S] [Algebra S A] [Algebra R A] [IsScalarTower R S A] theorem adjoin_range_toAlgHom (t : Set A) : (Algebra.adjoin (toAlgHom R S A).range t).restrictScalars R = (Algebra.adjoin S t).restrictScalars R := Subalgebra.ext fun z ↦ show z ∈ Subsemiring.closure (Set.range (algebraMap (toAlgHom R S A).range A) ∪ t : Set A) ↔ z ∈ Subsemiring.closure (Set.range (algebraMap S A) ∪ t : Set A) by suffices Set.range (algebraMap (toAlgHom R S A).range A) = Set.range (algebraMap S A) by rw [this] ext z exact ⟨fun ⟨⟨_, y, h1⟩, h2⟩ ↦ ⟨y, h2 ▸ h1⟩, fun ⟨y, hy⟩ ↦ ⟨⟨z, y, hy⟩, rfl⟩⟩ end IsScalarTower
Algebra\Algebra\Subalgebra\Unitization.lean
/- Copyright (c) 2023 Jireh Loreaux. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jireh Loreaux -/ import Mathlib.Algebra.Algebra.Unitization import Mathlib.Algebra.Star.NonUnitalSubalgebra import Mathlib.Algebra.Star.Subalgebra import Mathlib.GroupTheory.GroupAction.Ring /-! # Relating unital and non-unital substructures This file relates various algebraic structures and provides maps (generally algebra homomorphisms), from the unitization of a non-unital subobject into the full structure. The range of this map is the unital closure of the non-unital subobject (e.g., `Algebra.adjoin`, `Subring.closure`, `Subsemiring.closure` or `StarAlgebra.adjoin`). When the underlying scalar ring is a field, for this map to be injective it suffices that the range omits `1`. In this setting we provide suitable `AlgEquiv` (or `StarAlgEquiv`) onto the range. ## Main declarations * `NonUnitalSubalgebra.unitization s : Unitization R s →ₐ[R] A`: where `s` is a non-unital subalgebra of a unital `R`-algebra `A`, this is the natural algebra homomorphism sending `(r, a)` to `r • 1 + a`. The range of this map is `Algebra.adjoin R (s : Set A)`. * `NonUnitalSubalgebra.unitizationAlgEquiv s : Unitization R s ≃ₐ[R] Algebra.adjoin R (s : Set A)` when `R` is a field and `1 ∉ s`. This is `NonUnitalSubalgebra.unitization` upgraded to an `AlgEquiv` onto its range. * `NonUnitalSubsemiring.unitization : Unitization ℕ s →ₐ[ℕ] R`: the natural `ℕ`-algebra homomorphism from the unitization of a non-unital subsemiring `s` into the ring containing it. The range of this map is `subalgebraOfSubsemiring (Subsemiring.closure s)`. This is just `NonUnitalSubalgebra.unitization s` but we provide a separate declaration because there is an instance Lean can't find on its own due to `outParam`. * `NonUnitalSubring.unitization : Unitization ℤ s →ₐ[ℤ] R`: the natural `ℤ`-algebra homomorphism from the unitization of a non-unital subring `s` into the ring containing it. The range of this map is `subalgebraOfSubring (Subring.closure s)`. This is just `NonUnitalSubalgebra.unitization s` but we provide a separate declaration because there is an instance Lean can't find on its own due to `outParam`. * `NonUnitalStarSubalgebra s : Unitization R s →⋆ₐ[R] A`: a version of `NonUnitalSubalgebra.unitization` for star algebras. * `NonUnitalStarSubalgebra.unitizationStarAlgEquiv s :` `Unitization R s ≃⋆ₐ[R] StarAlgebra.adjoin R (s : Set A)`: a version of `NonUnitalSubalgebra.unitizationAlgEquiv` for star algebras. -/ /-! ## Subalgebras -/ section Subalgebra variable {R A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] /-- Turn a `Subalgebra` into a `NonUnitalSubalgebra` by forgetting that it contains `1`. -/ def Subalgebra.toNonUnitalSubalgebra (S : Subalgebra R A) : NonUnitalSubalgebra R A := { S with smul_mem' := fun r _x hx => S.smul_mem hx r } theorem Subalgebra.one_mem_toNonUnitalSubalgebra (S : Subalgebra R A) : (1 : A) ∈ S.toNonUnitalSubalgebra := S.one_mem /-- Turn a non-unital subalgebra containing `1` into a subalgebra. -/ def NonUnitalSubalgebra.toSubalgebra (S : NonUnitalSubalgebra R A) (h1 : (1 : A) ∈ S) : Subalgebra R A := { S with one_mem' := h1 algebraMap_mem' := fun r => (Algebra.algebraMap_eq_smul_one (R := R) (A := A) r).symm ▸ SMulMemClass.smul_mem r h1 } theorem Subalgebra.toNonUnitalSubalgebra_toSubalgebra (S : Subalgebra R A) : S.toNonUnitalSubalgebra.toSubalgebra S.one_mem = S := by cases S; rfl theorem NonUnitalSubalgebra.toSubalgebra_toNonUnitalSubalgebra (S : NonUnitalSubalgebra R A) (h1 : (1 : A) ∈ S) : (NonUnitalSubalgebra.toSubalgebra S h1).toNonUnitalSubalgebra = S := by cases S; rfl open Submodule in lemma Algebra.adjoin_nonUnitalSubalgebra_eq_span (s : NonUnitalSubalgebra R A) : Subalgebra.toSubmodule (adjoin R (s : Set A)) = span R {1} ⊔ s.toSubmodule := by rw [adjoin_eq_span, Submonoid.closure_eq_one_union, span_union, ← NonUnitalAlgebra.adjoin_eq_span, NonUnitalAlgebra.adjoin_eq] variable (R) lemma NonUnitalAlgebra.adjoin_le_algebra_adjoin (s : Set A) : adjoin R s ≤ (Algebra.adjoin R s).toNonUnitalSubalgebra := adjoin_le Algebra.subset_adjoin lemma Algebra.adjoin_nonUnitalSubalgebra (s : Set A) : adjoin R (NonUnitalAlgebra.adjoin R s : Set A) = adjoin R s := le_antisymm (adjoin_le <| NonUnitalAlgebra.adjoin_le_algebra_adjoin R s) (adjoin_le <| (NonUnitalAlgebra.subset_adjoin R).trans subset_adjoin) end Subalgebra namespace Unitization variable {R A C : Type*} [CommSemiring R] [NonUnitalSemiring A] variable [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] [Semiring C] [Algebra R C] theorem lift_range_le {f : A →ₙₐ[R] C} {S : Subalgebra R C} : (lift f).range ≤ S ↔ NonUnitalAlgHom.range f ≤ S.toNonUnitalSubalgebra := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rintro - ⟨x, rfl⟩ exact @h (f x) ⟨x, by simp⟩ · rintro - ⟨x, rfl⟩ induction x with | _ r a => simpa using add_mem (algebraMap_mem S r) (h ⟨a, rfl⟩) theorem lift_range (f : A →ₙₐ[R] C) : (lift f).range = Algebra.adjoin R (NonUnitalAlgHom.range f : Set C) := eq_of_forall_ge_iff fun c ↦ by rw [lift_range_le, Algebra.adjoin_le_iff]; rfl end Unitization namespace NonUnitalSubalgebra section Semiring variable {R S A : Type*} [CommSemiring R] [Semiring A] [Algebra R A] [SetLike S A] [hSA : NonUnitalSubsemiringClass S A] [hSRA : SMulMemClass S R A] (s : S) /-- The natural `R`-algebra homomorphism from the unitization of a non-unital subalgebra into the algebra containing it. -/ def unitization : Unitization R s →ₐ[R] A := Unitization.lift (NonUnitalSubalgebraClass.subtype s) @[simp] theorem unitization_apply (x : Unitization R s) : unitization s x = algebraMap R A x.fst + x.snd := rfl theorem unitization_range : (unitization s).range = Algebra.adjoin R (s : Set A) := by rw [unitization, Unitization.lift_range] simp only [NonUnitalAlgHom.coe_range, NonUnitalSubalgebraClass.coeSubtype, Subtype.range_coe_subtype, SetLike.mem_coe] rfl end Semiring /-- A sufficient condition for injectivity of `NonUnitalSubalgebra.unitization` when the scalars are a commutative ring. When the scalars are a field, one should use the more natural `NonUnitalStarSubalgebra.unitization_injective` whose hypothesis is easier to verify. -/ theorem _root_.AlgHomClass.unitization_injective' {F R S A : Type*} [CommRing R] [Ring A] [Algebra R A] [SetLike S A] [hSA : NonUnitalSubringClass S A] [hSRA : SMulMemClass S R A] (s : S) (h : ∀ r, r ≠ 0 → algebraMap R A r ∉ s) [FunLike F (Unitization R s) A] [AlgHomClass F R (Unitization R s) A] (f : F) (hf : ∀ x : s, f x = x) : Function.Injective f := by refine (injective_iff_map_eq_zero f).mpr fun x hx => ?_ induction' x with r a simp_rw [map_add, hf, ← Unitization.algebraMap_eq_inl, AlgHomClass.commutes] at hx rw [add_eq_zero_iff_eq_neg] at hx ⊢ by_cases hr : r = 0 · ext <;> simp [hr] at hx ⊢ exact hx · exact (h r hr <| hx ▸ (neg_mem a.property)).elim /-- This is a generic version which allows us to prove both `NonUnitalSubalgebra.unitization_injective` and `NonUnitalStarSubalgebra.unitization_injective`. -/ theorem _root_.AlgHomClass.unitization_injective {F R S A : Type*} [Field R] [Ring A] [Algebra R A] [SetLike S A] [hSA : NonUnitalSubringClass S A] [hSRA : SMulMemClass S R A] (s : S) (h1 : 1 ∉ s) [FunLike F (Unitization R s) A] [AlgHomClass F R (Unitization R s) A] (f : F) (hf : ∀ x : s, f x = x) : Function.Injective f := by refine AlgHomClass.unitization_injective' s (fun r hr hr' ↦ ?_) f hf rw [Algebra.algebraMap_eq_smul_one] at hr' exact h1 <| inv_smul_smul₀ hr (1 : A) ▸ SMulMemClass.smul_mem r⁻¹ hr' section Field variable {R S A : Type*} [Field R] [Ring A] [Algebra R A] [SetLike S A] [hSA : NonUnitalSubringClass S A] [hSRA : SMulMemClass S R A] (s : S) theorem unitization_injective (h1 : (1 : A) ∉ s) : Function.Injective (unitization s) := AlgHomClass.unitization_injective s h1 (unitization s) fun _ ↦ by simp /-- If a `NonUnitalSubalgebra` over a field does not contain `1`, then its unitization is isomorphic to its `Algebra.adjoin`. -/ @[simps! apply_coe] noncomputable def unitizationAlgEquiv (h1 : (1 : A) ∉ s) : Unitization R s ≃ₐ[R] Algebra.adjoin R (s : Set A) := let algHom : Unitization R s →ₐ[R] Algebra.adjoin R (s : Set A) := ((unitization s).codRestrict _ fun x ↦ (unitization_range s).le <| AlgHom.mem_range_self _ x) AlgEquiv.ofBijective algHom <| by refine ⟨?_, fun x ↦ ?_⟩ · have := AlgHomClass.unitization_injective s h1 ((Subalgebra.val _).comp algHom) fun _ ↦ by simp [algHom] rw [AlgHom.coe_comp] at this exact this.of_comp · obtain (⟨a, ha⟩ : (x : A) ∈ (unitization s).range) := (unitization_range s).ge x.property exact ⟨a, Subtype.ext ha⟩ end Field end NonUnitalSubalgebra /-! ## Subsemirings -/ section Subsemiring variable {R : Type*} [NonAssocSemiring R] /-- Turn a `Subsemiring` into a `NonUnitalSubsemiring` by forgetting that it contains `1`. -/ def Subsemiring.toNonUnitalSubsemiring (S : Subsemiring R) : NonUnitalSubsemiring R := { S with } theorem Subsemiring.one_mem_toNonUnitalSubsemiring (S : Subsemiring R) : (1 : R) ∈ S.toNonUnitalSubsemiring := S.one_mem /-- Turn a non-unital subsemiring containing `1` into a subsemiring. -/ def NonUnitalSubsemiring.toSubsemiring (S : NonUnitalSubsemiring R) (h1 : (1 : R) ∈ S) : Subsemiring R := { S with one_mem' := h1 } theorem Subsemiring.toNonUnitalSubsemiring_toSubsemiring (S : Subsemiring R) : S.toNonUnitalSubsemiring.toSubsemiring S.one_mem = S := by cases S; rfl theorem NonUnitalSubsemiring.toSubsemiring_toNonUnitalSubsemiring (S : NonUnitalSubsemiring R) (h1 : (1 : R) ∈ S) : (NonUnitalSubsemiring.toSubsemiring S h1).toNonUnitalSubsemiring = S := by cases S; rfl end Subsemiring namespace NonUnitalSubsemiring variable {R S : Type*} [Semiring R] [SetLike S R] [hSR : NonUnitalSubsemiringClass S R] (s : S) /-- The natural `ℕ`-algebra homomorphism from the unitization of a non-unital subsemiring to its `Subsemiring.closure`. -/ def unitization : Unitization ℕ s →ₐ[ℕ] R := NonUnitalSubalgebra.unitization (hSRA := AddSubmonoidClass.nsmulMemClass) s @[simp] theorem unitization_apply (x : Unitization ℕ s) : unitization s x = x.fst + x.snd := rfl theorem unitization_range : (unitization s).range = subalgebraOfSubsemiring (Subsemiring.closure s) := by have := AddSubmonoidClass.nsmulMemClass (S := S) rw [unitization, NonUnitalSubalgebra.unitization_range (hSRA := this), Algebra.adjoin_nat] end NonUnitalSubsemiring /-! ## Subrings -/ section Subring -- TODO: Maybe we could use `NonAssocRing` here but right now `Subring` takes a `Ring` argument. variable {R : Type*} [Ring R] /-- Turn a `Subring` into a `NonUnitalSubring` by forgetting that it contains `1`. -/ def Subring.toNonUnitalSubring (S : Subring R) : NonUnitalSubring R := { S with } theorem Subring.one_mem_toNonUnitalSubring (S : Subring R) : (1 : R) ∈ S.toNonUnitalSubring := S.one_mem /-- Turn a non-unital subring containing `1` into a subring. -/ def NonUnitalSubring.toSubring (S : NonUnitalSubring R) (h1 : (1 : R) ∈ S) : Subring R := { S with one_mem' := h1 } theorem Subring.toNonUnitalSubring_toSubring (S : Subring R) : S.toNonUnitalSubring.toSubring S.one_mem = S := by cases S; rfl theorem NonUnitalSubring.toSubring_toNonUnitalSubring (S : NonUnitalSubring R) (h1 : (1 : R) ∈ S) : (NonUnitalSubring.toSubring S h1).toNonUnitalSubring = S := by cases S; rfl end Subring namespace NonUnitalSubring variable {R S : Type*} [Ring R] [SetLike S R] [hSR : NonUnitalSubringClass S R] (s : S) /-- The natural `ℤ`-algebra homomorphism from the unitization of a non-unital subring to its `Subring.closure`. -/ def unitization : Unitization ℤ s →ₐ[ℤ] R := NonUnitalSubalgebra.unitization (hSRA := AddSubgroupClass.zsmulMemClass) s @[simp] theorem unitization_apply (x : Unitization ℤ s) : unitization s x = x.fst + x.snd := rfl theorem unitization_range : (unitization s).range = subalgebraOfSubring (Subring.closure s) := by have := AddSubgroupClass.zsmulMemClass (S := S) rw [unitization, NonUnitalSubalgebra.unitization_range (hSRA := this), Algebra.adjoin_int] end NonUnitalSubring /-! ## Star subalgebras -/ section StarSubalgebra variable {R A : Type*} [CommSemiring R] [StarRing R] [Semiring A] [StarRing A] variable [Algebra R A] [StarModule R A] /-- Turn a `StarSubalgebra` into a `NonUnitalStarSubalgebra` by forgetting that it contains `1`. -/ def StarSubalgebra.toNonUnitalStarSubalgebra (S : StarSubalgebra R A) : NonUnitalStarSubalgebra R A := { S with carrier := S.carrier smul_mem' := fun r _x hx => S.smul_mem hx r } theorem StarSubalgebra.one_mem_toNonUnitalStarSubalgebra (S : StarSubalgebra R A) : (1 : A) ∈ S.toNonUnitalStarSubalgebra := S.one_mem' /-- Turn a non-unital star subalgebra containing `1` into a `StarSubalgebra`. -/ def NonUnitalStarSubalgebra.toStarSubalgebra (S : NonUnitalStarSubalgebra R A) (h1 : (1 : A) ∈ S) : StarSubalgebra R A := { S with carrier := S.carrier one_mem' := h1 algebraMap_mem' := fun r => (Algebra.algebraMap_eq_smul_one (R := R) (A := A) r).symm ▸ SMulMemClass.smul_mem r h1 } theorem StarSubalgebra.toNonUnitalStarSubalgebra_toStarSubalgebra (S : StarSubalgebra R A) : S.toNonUnitalStarSubalgebra.toStarSubalgebra S.one_mem' = S := by cases S; rfl theorem NonUnitalStarSubalgebra.toStarSubalgebra_toNonUnitalStarSubalgebra (S : NonUnitalStarSubalgebra R A) (h1 : (1 : A) ∈ S) : (S.toStarSubalgebra h1).toNonUnitalStarSubalgebra = S := by cases S; rfl open Submodule in lemma StarAlgebra.adjoin_nonUnitalStarSubalgebra_eq_span (s : NonUnitalStarSubalgebra R A) : Subalgebra.toSubmodule (adjoin R (s : Set A)).toSubalgebra = span R {1} ⊔ s.toSubmodule := by rw [adjoin_eq_span, Submonoid.closure_eq_one_union, span_union, ← NonUnitalStarAlgebra.adjoin_eq_span, NonUnitalStarAlgebra.adjoin_eq] variable (R) lemma NonUnitalStarAlgebra.adjoin_le_starAlgebra_adjoin (s : Set A) : adjoin R s ≤ (StarAlgebra.adjoin R s).toNonUnitalStarSubalgebra := adjoin_le <| StarAlgebra.subset_adjoin R s lemma StarAlgebra.adjoin_nonUnitalStarSubalgebra (s : Set A) : adjoin R (NonUnitalStarAlgebra.adjoin R s : Set A) = adjoin R s := le_antisymm (adjoin_le <| NonUnitalStarAlgebra.adjoin_le_starAlgebra_adjoin R s) (adjoin_le <| (NonUnitalStarAlgebra.subset_adjoin R s).trans <| subset_adjoin R _) end StarSubalgebra namespace Unitization variable {R A C : Type*} [CommSemiring R] [NonUnitalSemiring A] [StarRing R] [StarRing A] variable [Module R A] [SMulCommClass R A A] [IsScalarTower R A A] [StarModule R A] variable [Semiring C] [StarRing C] [Algebra R C] [StarModule R C] theorem starLift_range_le {f : A →⋆ₙₐ[R] C} {S : StarSubalgebra R C} : (starLift f).range ≤ S ↔ NonUnitalStarAlgHom.range f ≤ S.toNonUnitalStarSubalgebra := by refine ⟨fun h ↦ ?_, fun h ↦ ?_⟩ · rintro - ⟨x, rfl⟩ exact @h (f x) ⟨x, by simp⟩ · rintro - ⟨x, rfl⟩ induction x with | _ r a => simpa using add_mem (algebraMap_mem S r) (h ⟨a, rfl⟩) theorem starLift_range (f : A →⋆ₙₐ[R] C) : (starLift f).range = StarAlgebra.adjoin R (NonUnitalStarAlgHom.range f : Set C) := eq_of_forall_ge_iff fun c ↦ by rw [starLift_range_le, StarAlgebra.adjoin_le_iff] rfl end Unitization namespace NonUnitalStarSubalgebra section Semiring variable {R S A : Type*} [CommSemiring R] [StarRing R] [Semiring A] [StarRing A] [Algebra R A] [StarModule R A] [SetLike S A] [hSA : NonUnitalSubsemiringClass S A] [hSRA : SMulMemClass S R A] [StarMemClass S A] (s : S) /-- The natural star `R`-algebra homomorphism from the unitization of a non-unital star subalgebra to its `StarAlgebra.adjoin`. -/ def unitization : Unitization R s →⋆ₐ[R] A := Unitization.starLift <| NonUnitalStarSubalgebraClass.subtype s @[simp] theorem unitization_apply (x : Unitization R s) : unitization s x = algebraMap R A x.fst + x.snd := rfl theorem unitization_range : (unitization s).range = StarAlgebra.adjoin R s := by rw [unitization, Unitization.starLift_range] simp only [NonUnitalStarAlgHom.coe_range, NonUnitalStarSubalgebraClass.coeSubtype, Subtype.range_coe_subtype] rfl end Semiring section Field variable {R S A : Type*} [Field R] [StarRing R] [Ring A] [StarRing A] [Algebra R A] [StarModule R A] [SetLike S A] [hSA : NonUnitalSubringClass S A] [hSRA : SMulMemClass S R A] [StarMemClass S A] (s : S) theorem unitization_injective (h1 : (1 : A) ∉ s) : Function.Injective (unitization s) := AlgHomClass.unitization_injective s h1 (unitization s) fun _ ↦ by simp /-- If a `NonUnitalStarSubalgebra` over a field does not contain `1`, then its unitization is isomorphic to its `StarAlgebra.adjoin`. -/ @[simps! apply_coe] noncomputable def unitizationStarAlgEquiv (h1 : (1 : A) ∉ s) : Unitization R s ≃⋆ₐ[R] StarAlgebra.adjoin R (s : Set A) := let starAlgHom : Unitization R s →⋆ₐ[R] StarAlgebra.adjoin R (s : Set A) := ((unitization s).codRestrict _ fun x ↦ (unitization_range s).le <| Set.mem_range_self x) StarAlgEquiv.ofBijective starAlgHom <| by refine ⟨?_, fun x ↦ ?_⟩ · have := AlgHomClass.unitization_injective s h1 ((StarSubalgebra.subtype _).comp starAlgHom) fun _ ↦ by simp [starAlgHom] rw [StarAlgHom.coe_comp] at this exact this.of_comp · obtain (⟨a, ha⟩ : (x : A) ∈ (unitization s).range) := (unitization_range s).ge x.property exact ⟨a, Subtype.ext ha⟩ end Field end NonUnitalStarSubalgebra
Algebra\Associated\Basic.lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import Mathlib.Algebra.Group.Even import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.GroupWithZero.Hom import Mathlib.Algebra.Group.Commute.Units import Mathlib.Algebra.Group.Units.Hom import Mathlib.Order.BoundedOrder import Mathlib.Algebra.Ring.Units /-! # Associated, prime, and irreducible elements. In this file we define the predicate `Prime p` saying that an element of a commutative monoid with zero is prime. Namely, `Prime p` means that `p` isn't zero, it isn't a unit, and `p ∣ a * b → p ∣ a ∨ p ∣ b` for all `a`, `b`; In decomposition monoids (e.g., `ℕ`, `ℤ`), this predicate is equivalent to `Irreducible`, however this is not true in general. We also define an equivalence relation `Associated` saying that two elements of a monoid differ by a multiplication by a unit. Then we show that the quotient type `Associates` is a monoid and prove basic properties of this quotient. -/ variable {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} section Prime variable [CommMonoidWithZero α] /-- An element `p` of a commutative monoid with zero (e.g., a ring) is called *prime*, if it's not zero, not a unit, and `p ∣ a * b → p ∣ a ∨ p ∣ b` for all `a`, `b`. -/ def Prime (p : α) : Prop := p ≠ 0 ∧ ¬IsUnit p ∧ ∀ a b, p ∣ a * b → p ∣ a ∨ p ∣ b namespace Prime variable {p : α} (hp : Prime p) theorem ne_zero : p ≠ 0 := hp.1 theorem not_unit : ¬IsUnit p := hp.2.1 theorem not_dvd_one : ¬p ∣ 1 := mt (isUnit_of_dvd_one ·) hp.not_unit theorem ne_one : p ≠ 1 := fun h => hp.2.1 (h.symm ▸ isUnit_one) theorem dvd_or_dvd (hp : Prime p) {a b : α} (h : p ∣ a * b) : p ∣ a ∨ p ∣ b := hp.2.2 a b h theorem dvd_mul {a b : α} : p ∣ a * b ↔ p ∣ a ∨ p ∣ b := ⟨hp.dvd_or_dvd, (Or.elim · (dvd_mul_of_dvd_left · _) (dvd_mul_of_dvd_right · _))⟩ theorem isPrimal (hp : Prime p) : IsPrimal p := fun _a _b dvd ↦ (hp.dvd_or_dvd dvd).elim (fun h ↦ ⟨p, 1, h, one_dvd _, (mul_one p).symm⟩) fun h ↦ ⟨1, p, one_dvd _, h, (one_mul p).symm⟩ theorem not_dvd_mul {a b : α} (ha : ¬ p ∣ a) (hb : ¬ p ∣ b) : ¬ p ∣ a * b := hp.dvd_mul.not.mpr <| not_or.mpr ⟨ha, hb⟩ theorem dvd_of_dvd_pow (hp : Prime p) {a : α} {n : ℕ} (h : p ∣ a ^ n) : p ∣ a := by induction' n with n ih · rw [pow_zero] at h have := isUnit_of_dvd_one h have := not_unit hp contradiction rw [pow_succ'] at h cases' dvd_or_dvd hp h with dvd_a dvd_pow · assumption exact ih dvd_pow theorem dvd_pow_iff_dvd {a : α} {n : ℕ} (hn : n ≠ 0) : p ∣ a ^ n ↔ p ∣ a := ⟨hp.dvd_of_dvd_pow, (dvd_pow · hn)⟩ end Prime @[simp] theorem not_prime_zero : ¬Prime (0 : α) := fun h => h.ne_zero rfl @[simp] theorem not_prime_one : ¬Prime (1 : α) := fun h => h.not_unit isUnit_one section Map variable [CommMonoidWithZero β] {F : Type*} {G : Type*} [FunLike F α β] variable [MonoidWithZeroHomClass F α β] [FunLike G β α] [MulHomClass G β α] variable (f : F) (g : G) {p : α} theorem comap_prime (hinv : ∀ a, g (f a : β) = a) (hp : Prime (f p)) : Prime p := ⟨fun h => hp.1 <| by simp [h], fun h => hp.2.1 <| h.map f, fun a b h => by refine (hp.2.2 (f a) (f b) <| by convert map_dvd f h simp).imp ?_ ?_ <;> · intro h convert ← map_dvd g h <;> apply hinv⟩ theorem MulEquiv.prime_iff (e : α ≃* β) : Prime p ↔ Prime (e p) := ⟨fun h => (comap_prime e.symm e fun a => by simp) <| (e.symm_apply_apply p).substr h, comap_prime e e.symm fun a => by simp⟩ end Map end Prime theorem Prime.left_dvd_or_dvd_right_of_dvd_mul [CancelCommMonoidWithZero α] {p : α} (hp : Prime p) {a b : α} : a ∣ p * b → p ∣ a ∨ a ∣ b := by rintro ⟨c, hc⟩ rcases hp.2.2 a c (hc ▸ dvd_mul_right _ _) with (h | ⟨x, rfl⟩) · exact Or.inl h · rw [mul_left_comm, mul_right_inj' hp.ne_zero] at hc exact Or.inr (hc.symm ▸ dvd_mul_right _ _) theorem Prime.pow_dvd_of_dvd_mul_left [CancelCommMonoidWithZero α] {p a b : α} (hp : Prime p) (n : ℕ) (h : ¬p ∣ a) (h' : p ^ n ∣ a * b) : p ^ n ∣ b := by induction' n with n ih · rw [pow_zero] exact one_dvd b · obtain ⟨c, rfl⟩ := ih (dvd_trans (pow_dvd_pow p n.le_succ) h') rw [pow_succ] apply mul_dvd_mul_left _ ((hp.dvd_or_dvd _).resolve_left h) rwa [← mul_dvd_mul_iff_left (pow_ne_zero n hp.ne_zero), ← pow_succ, mul_left_comm] theorem Prime.pow_dvd_of_dvd_mul_right [CancelCommMonoidWithZero α] {p a b : α} (hp : Prime p) (n : ℕ) (h : ¬p ∣ b) (h' : p ^ n ∣ a * b) : p ^ n ∣ a := by rw [mul_comm] at h' exact hp.pow_dvd_of_dvd_mul_left n h h' theorem Prime.dvd_of_pow_dvd_pow_mul_pow_of_square_not_dvd [CancelCommMonoidWithZero α] {p a b : α} {n : ℕ} (hp : Prime p) (hpow : p ^ n.succ ∣ a ^ n.succ * b ^ n) (hb : ¬p ^ 2 ∣ b) : p ∣ a := by -- Suppose `p ∣ b`, write `b = p * x` and `hy : a ^ n.succ * b ^ n = p ^ n.succ * y`. cases' hp.dvd_or_dvd ((dvd_pow_self p (Nat.succ_ne_zero n)).trans hpow) with H hbdiv · exact hp.dvd_of_dvd_pow H obtain ⟨x, rfl⟩ := hp.dvd_of_dvd_pow hbdiv obtain ⟨y, hy⟩ := hpow -- Then we can divide out a common factor of `p ^ n` from the equation `hy`. have : a ^ n.succ * x ^ n = p * y := by refine mul_left_cancel₀ (pow_ne_zero n hp.ne_zero) ?_ rw [← mul_assoc _ p, ← pow_succ, ← hy, mul_pow, ← mul_assoc (a ^ n.succ), mul_comm _ (p ^ n), mul_assoc] -- So `p ∣ a` (and we're done) or `p ∣ x`, which can't be the case since it implies `p^2 ∣ b`. refine hp.dvd_of_dvd_pow ((hp.dvd_or_dvd ⟨_, this⟩).resolve_right fun hdvdx => hb ?_) obtain ⟨z, rfl⟩ := hp.dvd_of_dvd_pow hdvdx rw [pow_two, ← mul_assoc] exact dvd_mul_right _ _ theorem prime_pow_succ_dvd_mul {α : Type*} [CancelCommMonoidWithZero α] {p x y : α} (h : Prime p) {i : ℕ} (hxy : p ^ (i + 1) ∣ x * y) : p ^ (i + 1) ∣ x ∨ p ∣ y := by rw [or_iff_not_imp_right] intro hy induction' i with i ih generalizing x · rw [pow_one] at hxy ⊢ exact (h.dvd_or_dvd hxy).resolve_right hy rw [pow_succ'] at hxy ⊢ obtain ⟨x', rfl⟩ := (h.dvd_or_dvd (dvd_of_mul_right_dvd hxy)).resolve_right hy rw [mul_assoc] at hxy exact mul_dvd_mul_left p (ih ((mul_dvd_mul_iff_left h.ne_zero).mp hxy)) /-- `Irreducible p` states that `p` is non-unit and only factors into units. We explicitly avoid stating that `p` is non-zero, this would require a semiring. Assuming only a monoid allows us to reuse irreducible for associated elements. -/ structure Irreducible [Monoid α] (p : α) : Prop where /-- `p` is not a unit -/ not_unit : ¬IsUnit p /-- if `p` factors then one factor is a unit -/ isUnit_or_isUnit' : ∀ a b, p = a * b → IsUnit a ∨ IsUnit b namespace Irreducible theorem not_dvd_one [CommMonoid α] {p : α} (hp : Irreducible p) : ¬p ∣ 1 := mt (isUnit_of_dvd_one ·) hp.not_unit theorem isUnit_or_isUnit [Monoid α] {p : α} (hp : Irreducible p) {a b : α} (h : p = a * b) : IsUnit a ∨ IsUnit b := hp.isUnit_or_isUnit' a b h end Irreducible theorem irreducible_iff [Monoid α] {p : α} : Irreducible p ↔ ¬IsUnit p ∧ ∀ a b, p = a * b → IsUnit a ∨ IsUnit b := ⟨fun h => ⟨h.1, h.2⟩, fun h => ⟨h.1, h.2⟩⟩ @[simp] theorem not_irreducible_one [Monoid α] : ¬Irreducible (1 : α) := by simp [irreducible_iff] theorem Irreducible.ne_one [Monoid α] : ∀ {p : α}, Irreducible p → p ≠ 1 | _, hp, rfl => not_irreducible_one hp @[simp] theorem not_irreducible_zero [MonoidWithZero α] : ¬Irreducible (0 : α) | ⟨hn0, h⟩ => have : IsUnit (0 : α) ∨ IsUnit (0 : α) := h 0 0 (mul_zero 0).symm this.elim hn0 hn0 theorem Irreducible.ne_zero [MonoidWithZero α] : ∀ {p : α}, Irreducible p → p ≠ 0 | _, hp, rfl => not_irreducible_zero hp theorem of_irreducible_mul {α} [Monoid α] {x y : α} : Irreducible (x * y) → IsUnit x ∨ IsUnit y | ⟨_, h⟩ => h _ _ rfl theorem not_irreducible_pow {α} [Monoid α] {x : α} {n : ℕ} (hn : n ≠ 1) : ¬ Irreducible (x ^ n) := by cases n with | zero => simp | succ n => intro ⟨h₁, h₂⟩ have := h₂ _ _ (pow_succ _ _) rw [isUnit_pow_iff (Nat.succ_ne_succ.mp hn), or_self] at this exact h₁ (this.pow _) theorem irreducible_or_factor {α} [Monoid α] (x : α) (h : ¬IsUnit x) : Irreducible x ∨ ∃ a b, ¬IsUnit a ∧ ¬IsUnit b ∧ a * b = x := by haveI := Classical.dec refine or_iff_not_imp_right.2 fun H => ?_ simp? [h, irreducible_iff] at H ⊢ says simp only [exists_and_left, not_exists, not_and, irreducible_iff, h, not_false_eq_true, true_and] at H ⊢ refine fun a b h => by_contradiction fun o => ?_ simp? [not_or] at o says simp only [not_or] at o exact H _ o.1 _ o.2 h.symm /-- If `p` and `q` are irreducible, then `p ∣ q` implies `q ∣ p`. -/ theorem Irreducible.dvd_symm [Monoid α] {p q : α} (hp : Irreducible p) (hq : Irreducible q) : p ∣ q → q ∣ p := by rintro ⟨q', rfl⟩ rw [IsUnit.mul_right_dvd (Or.resolve_left (of_irreducible_mul hq) hp.not_unit)] theorem Irreducible.dvd_comm [Monoid α] {p q : α} (hp : Irreducible p) (hq : Irreducible q) : p ∣ q ↔ q ∣ p := ⟨hp.dvd_symm hq, hq.dvd_symm hp⟩ section variable [Monoid α] theorem irreducible_units_mul (a : αˣ) (b : α) : Irreducible (↑a * b) ↔ Irreducible b := by simp only [irreducible_iff, Units.isUnit_units_mul, and_congr_right_iff] refine fun _ => ⟨fun h A B HAB => ?_, fun h A B HAB => ?_⟩ · rw [← a.isUnit_units_mul] apply h rw [mul_assoc, ← HAB] · rw [← a⁻¹.isUnit_units_mul] apply h rw [mul_assoc, ← HAB, Units.inv_mul_cancel_left] theorem irreducible_isUnit_mul {a b : α} (h : IsUnit a) : Irreducible (a * b) ↔ Irreducible b := let ⟨a, ha⟩ := h ha ▸ irreducible_units_mul a b theorem irreducible_mul_units (a : αˣ) (b : α) : Irreducible (b * ↑a) ↔ Irreducible b := by simp only [irreducible_iff, Units.isUnit_mul_units, and_congr_right_iff] refine fun _ => ⟨fun h A B HAB => ?_, fun h A B HAB => ?_⟩ · rw [← Units.isUnit_mul_units B a] apply h rw [← mul_assoc, ← HAB] · rw [← Units.isUnit_mul_units B a⁻¹] apply h rw [← mul_assoc, ← HAB, Units.mul_inv_cancel_right] theorem irreducible_mul_isUnit {a b : α} (h : IsUnit a) : Irreducible (b * a) ↔ Irreducible b := let ⟨a, ha⟩ := h ha ▸ irreducible_mul_units a b theorem irreducible_mul_iff {a b : α} : Irreducible (a * b) ↔ Irreducible a ∧ IsUnit b ∨ Irreducible b ∧ IsUnit a := by constructor · refine fun h => Or.imp (fun h' => ⟨?_, h'⟩) (fun h' => ⟨?_, h'⟩) (h.isUnit_or_isUnit rfl).symm · rwa [irreducible_mul_isUnit h'] at h · rwa [irreducible_isUnit_mul h'] at h · rintro (⟨ha, hb⟩ | ⟨hb, ha⟩) · rwa [irreducible_mul_isUnit hb] · rwa [irreducible_isUnit_mul ha] end section CommMonoid variable [CommMonoid α] {a : α} theorem Irreducible.not_square (ha : Irreducible a) : ¬IsSquare a := by rw [isSquare_iff_exists_sq] rintro ⟨b, rfl⟩ exact not_irreducible_pow (by decide) ha theorem IsSquare.not_irreducible (ha : IsSquare a) : ¬Irreducible a := fun h => h.not_square ha end CommMonoid section CommMonoidWithZero variable [CommMonoidWithZero α] theorem Irreducible.prime_of_isPrimal {a : α} (irr : Irreducible a) (primal : IsPrimal a) : Prime a := ⟨irr.ne_zero, irr.not_unit, fun a b dvd ↦ by obtain ⟨d₁, d₂, h₁, h₂, rfl⟩ := primal dvd exact (of_irreducible_mul irr).symm.imp (·.mul_right_dvd.mpr h₁) (·.mul_left_dvd.mpr h₂)⟩ theorem Irreducible.prime [DecompositionMonoid α] {a : α} (irr : Irreducible a) : Prime a := irr.prime_of_isPrimal (DecompositionMonoid.primal a) end CommMonoidWithZero section CancelCommMonoidWithZero variable [CancelCommMonoidWithZero α] {a p : α} protected theorem Prime.irreducible (hp : Prime p) : Irreducible p := ⟨hp.not_unit, fun a b ↦ by rintro rfl exact (hp.dvd_or_dvd dvd_rfl).symm.imp (isUnit_of_dvd_one <| (mul_dvd_mul_iff_right <| right_ne_zero_of_mul hp.ne_zero).mp <| dvd_mul_of_dvd_right · _) (isUnit_of_dvd_one <| (mul_dvd_mul_iff_left <| left_ne_zero_of_mul hp.ne_zero).mp <| dvd_mul_of_dvd_left · _)⟩ theorem irreducible_iff_prime [DecompositionMonoid α] {a : α} : Irreducible a ↔ Prime a := ⟨Irreducible.prime, Prime.irreducible⟩ theorem succ_dvd_or_succ_dvd_of_succ_sum_dvd_mul (hp : Prime p) {a b : α} {k l : ℕ} : p ^ k ∣ a → p ^ l ∣ b → p ^ (k + l + 1) ∣ a * b → p ^ (k + 1) ∣ a ∨ p ^ (l + 1) ∣ b := fun ⟨x, hx⟩ ⟨y, hy⟩ ⟨z, hz⟩ => have h : p ^ (k + l) * (x * y) = p ^ (k + l) * (p * z) := by simpa [mul_comm, pow_add, hx, hy, mul_assoc, mul_left_comm] using hz have hp0 : p ^ (k + l) ≠ 0 := pow_ne_zero _ hp.ne_zero have hpd : p ∣ x * y := ⟨z, by rwa [mul_right_inj' hp0] at h⟩ (hp.dvd_or_dvd hpd).elim (fun ⟨d, hd⟩ => Or.inl ⟨d, by simp [*, pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩) fun ⟨d, hd⟩ => Or.inr ⟨d, by simp [*, pow_succ, mul_comm, mul_left_comm, mul_assoc]⟩ theorem Prime.not_square (hp : Prime p) : ¬IsSquare p := hp.irreducible.not_square theorem IsSquare.not_prime (ha : IsSquare a) : ¬Prime a := fun h => h.not_square ha theorem not_prime_pow {n : ℕ} (hn : n ≠ 1) : ¬Prime (a ^ n) := fun hp => not_irreducible_pow hn hp.irreducible end CancelCommMonoidWithZero /-- Two elements of a `Monoid` are `Associated` if one of them is another one multiplied by a unit on the right. -/ def Associated [Monoid α] (x y : α) : Prop := ∃ u : αˣ, x * u = y /-- Notation for two elements of a monoid are associated, i.e. if one of them is another one multiplied by a unit on the right. -/ local infixl:50 " ~ᵤ " => Associated namespace Associated @[refl] protected theorem refl [Monoid α] (x : α) : x ~ᵤ x := ⟨1, by simp⟩ protected theorem rfl [Monoid α] {x : α} : x ~ᵤ x := .refl x instance [Monoid α] : IsRefl α Associated := ⟨Associated.refl⟩ @[symm] protected theorem symm [Monoid α] : ∀ {x y : α}, x ~ᵤ y → y ~ᵤ x | x, _, ⟨u, rfl⟩ => ⟨u⁻¹, by rw [mul_assoc, Units.mul_inv, mul_one]⟩ instance [Monoid α] : IsSymm α Associated := ⟨fun _ _ => Associated.symm⟩ protected theorem comm [Monoid α] {x y : α} : x ~ᵤ y ↔ y ~ᵤ x := ⟨Associated.symm, Associated.symm⟩ @[trans] protected theorem trans [Monoid α] : ∀ {x y z : α}, x ~ᵤ y → y ~ᵤ z → x ~ᵤ z | x, _, _, ⟨u, rfl⟩, ⟨v, rfl⟩ => ⟨u * v, by rw [Units.val_mul, mul_assoc]⟩ instance [Monoid α] : IsTrans α Associated := ⟨fun _ _ _ => Associated.trans⟩ /-- The setoid of the relation `x ~ᵤ y` iff there is a unit `u` such that `x * u = y` -/ protected def setoid (α : Type*) [Monoid α] : Setoid α where r := Associated iseqv := ⟨Associated.refl, Associated.symm, Associated.trans⟩ theorem map {M N : Type*} [Monoid M] [Monoid N] {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) {x y : M} (ha : Associated x y) : Associated (f x) (f y) := by obtain ⟨u, ha⟩ := ha exact ⟨Units.map f u, by rw [← ha, map_mul, Units.coe_map, MonoidHom.coe_coe]⟩ end Associated attribute [local instance] Associated.setoid theorem unit_associated_one [Monoid α] {u : αˣ} : (u : α) ~ᵤ 1 := ⟨u⁻¹, Units.mul_inv u⟩ @[simp] theorem associated_one_iff_isUnit [Monoid α] {a : α} : (a : α) ~ᵤ 1 ↔ IsUnit a := Iff.intro (fun h => let ⟨c, h⟩ := h.symm h ▸ ⟨c, (one_mul _).symm⟩) fun ⟨c, h⟩ => Associated.symm ⟨c, by simp [h]⟩ @[simp] theorem associated_zero_iff_eq_zero [MonoidWithZero α] (a : α) : a ~ᵤ 0 ↔ a = 0 := Iff.intro (fun h => by let ⟨u, h⟩ := h.symm simpa using h.symm) fun h => h ▸ Associated.refl a theorem associated_one_of_mul_eq_one [CommMonoid α] {a : α} (b : α) (hab : a * b = 1) : a ~ᵤ 1 := show (Units.mkOfMulEqOne a b hab : α) ~ᵤ 1 from unit_associated_one theorem associated_one_of_associated_mul_one [CommMonoid α] {a b : α} : a * b ~ᵤ 1 → a ~ᵤ 1 | ⟨u, h⟩ => associated_one_of_mul_eq_one (b * u) <| by simpa [mul_assoc] using h theorem associated_mul_unit_left {β : Type*} [Monoid β] (a u : β) (hu : IsUnit u) : Associated (a * u) a := let ⟨u', hu⟩ := hu ⟨u'⁻¹, hu ▸ Units.mul_inv_cancel_right _ _⟩ theorem associated_unit_mul_left {β : Type*} [CommMonoid β] (a u : β) (hu : IsUnit u) : Associated (u * a) a := by rw [mul_comm] exact associated_mul_unit_left _ _ hu theorem associated_mul_unit_right {β : Type*} [Monoid β] (a u : β) (hu : IsUnit u) : Associated a (a * u) := (associated_mul_unit_left a u hu).symm theorem associated_unit_mul_right {β : Type*} [CommMonoid β] (a u : β) (hu : IsUnit u) : Associated a (u * a) := (associated_unit_mul_left a u hu).symm theorem associated_mul_isUnit_left_iff {β : Type*} [Monoid β] {a u b : β} (hu : IsUnit u) : Associated (a * u) b ↔ Associated a b := ⟨(associated_mul_unit_right _ _ hu).trans, (associated_mul_unit_left _ _ hu).trans⟩ theorem associated_isUnit_mul_left_iff {β : Type*} [CommMonoid β] {u a b : β} (hu : IsUnit u) : Associated (u * a) b ↔ Associated a b := by rw [mul_comm] exact associated_mul_isUnit_left_iff hu theorem associated_mul_isUnit_right_iff {β : Type*} [Monoid β] {a b u : β} (hu : IsUnit u) : Associated a (b * u) ↔ Associated a b := Associated.comm.trans <| (associated_mul_isUnit_left_iff hu).trans Associated.comm theorem associated_isUnit_mul_right_iff {β : Type*} [CommMonoid β] {a u b : β} (hu : IsUnit u) : Associated a (u * b) ↔ Associated a b := Associated.comm.trans <| (associated_isUnit_mul_left_iff hu).trans Associated.comm @[simp] theorem associated_mul_unit_left_iff {β : Type*} [Monoid β] {a b : β} {u : Units β} : Associated (a * u) b ↔ Associated a b := associated_mul_isUnit_left_iff u.isUnit @[simp] theorem associated_unit_mul_left_iff {β : Type*} [CommMonoid β] {a b : β} {u : Units β} : Associated (↑u * a) b ↔ Associated a b := associated_isUnit_mul_left_iff u.isUnit @[simp] theorem associated_mul_unit_right_iff {β : Type*} [Monoid β] {a b : β} {u : Units β} : Associated a (b * u) ↔ Associated a b := associated_mul_isUnit_right_iff u.isUnit @[simp] theorem associated_unit_mul_right_iff {β : Type*} [CommMonoid β] {a b : β} {u : Units β} : Associated a (↑u * b) ↔ Associated a b := associated_isUnit_mul_right_iff u.isUnit theorem Associated.mul_left [Monoid α] (a : α) {b c : α} (h : b ~ᵤ c) : a * b ~ᵤ a * c := by obtain ⟨d, rfl⟩ := h; exact ⟨d, mul_assoc _ _ _⟩ theorem Associated.mul_right [CommMonoid α] {a b : α} (h : a ~ᵤ b) (c : α) : a * c ~ᵤ b * c := by obtain ⟨d, rfl⟩ := h; exact ⟨d, mul_right_comm _ _ _⟩ theorem Associated.mul_mul [CommMonoid α] {a₁ a₂ b₁ b₂ : α} (h₁ : a₁ ~ᵤ b₁) (h₂ : a₂ ~ᵤ b₂) : a₁ * a₂ ~ᵤ b₁ * b₂ := (h₁.mul_right _).trans (h₂.mul_left _) theorem Associated.pow_pow [CommMonoid α] {a b : α} {n : ℕ} (h : a ~ᵤ b) : a ^ n ~ᵤ b ^ n := by induction' n with n ih · simp [Associated.refl] convert h.mul_mul ih <;> rw [pow_succ'] protected theorem Associated.dvd [Monoid α] {a b : α} : a ~ᵤ b → a ∣ b := fun ⟨u, hu⟩ => ⟨u, hu.symm⟩ protected theorem Associated.dvd' [Monoid α] {a b : α} (h : a ~ᵤ b) : b ∣ a := h.symm.dvd protected theorem Associated.dvd_dvd [Monoid α] {a b : α} (h : a ~ᵤ b) : a ∣ b ∧ b ∣ a := ⟨h.dvd, h.symm.dvd⟩ theorem associated_of_dvd_dvd [CancelMonoidWithZero α] {a b : α} (hab : a ∣ b) (hba : b ∣ a) : a ~ᵤ b := by rcases hab with ⟨c, rfl⟩ rcases hba with ⟨d, a_eq⟩ by_cases ha0 : a = 0 · simp_all have hac0 : a * c ≠ 0 := by intro con rw [con, zero_mul] at a_eq apply ha0 a_eq have : a * (c * d) = a * 1 := by rw [← mul_assoc, ← a_eq, mul_one] have hcd : c * d = 1 := mul_left_cancel₀ ha0 this have : a * c * (d * c) = a * c * 1 := by rw [← mul_assoc, ← a_eq, mul_one] have hdc : d * c = 1 := mul_left_cancel₀ hac0 this exact ⟨⟨c, d, hcd, hdc⟩, rfl⟩ theorem dvd_dvd_iff_associated [CancelMonoidWithZero α] {a b : α} : a ∣ b ∧ b ∣ a ↔ a ~ᵤ b := ⟨fun ⟨h1, h2⟩ => associated_of_dvd_dvd h1 h2, Associated.dvd_dvd⟩ instance [CancelMonoidWithZero α] [DecidableRel ((· ∣ ·) : α → α → Prop)] : DecidableRel ((· ~ᵤ ·) : α → α → Prop) := fun _ _ => decidable_of_iff _ dvd_dvd_iff_associated theorem Associated.dvd_iff_dvd_left [Monoid α] {a b c : α} (h : a ~ᵤ b) : a ∣ c ↔ b ∣ c := let ⟨_, hu⟩ := h hu ▸ Units.mul_right_dvd.symm theorem Associated.dvd_iff_dvd_right [Monoid α] {a b c : α} (h : b ~ᵤ c) : a ∣ b ↔ a ∣ c := let ⟨_, hu⟩ := h hu ▸ Units.dvd_mul_right.symm theorem Associated.eq_zero_iff [MonoidWithZero α] {a b : α} (h : a ~ᵤ b) : a = 0 ↔ b = 0 := by obtain ⟨u, rfl⟩ := h rw [← Units.eq_mul_inv_iff_mul_eq, zero_mul] theorem Associated.ne_zero_iff [MonoidWithZero α] {a b : α} (h : a ~ᵤ b) : a ≠ 0 ↔ b ≠ 0 := not_congr h.eq_zero_iff theorem Associated.neg_left [Monoid α] [HasDistribNeg α] {a b : α} (h : Associated a b) : Associated (-a) b := let ⟨u, hu⟩ := h; ⟨-u, by simp [hu]⟩ theorem Associated.neg_right [Monoid α] [HasDistribNeg α] {a b : α} (h : Associated a b) : Associated a (-b) := h.symm.neg_left.symm theorem Associated.neg_neg [Monoid α] [HasDistribNeg α] {a b : α} (h : Associated a b) : Associated (-a) (-b) := h.neg_left.neg_right protected theorem Associated.prime [CommMonoidWithZero α] {p q : α} (h : p ~ᵤ q) (hp : Prime p) : Prime q := ⟨h.ne_zero_iff.1 hp.ne_zero, let ⟨u, hu⟩ := h ⟨fun ⟨v, hv⟩ => hp.not_unit ⟨v * u⁻¹, by simp [hv, hu.symm]⟩, hu ▸ by simp only [IsUnit.mul_iff, Units.isUnit, and_true, IsUnit.mul_right_dvd] intro a b exact hp.dvd_or_dvd⟩⟩ theorem prime_mul_iff [CancelCommMonoidWithZero α] {x y : α} : Prime (x * y) ↔ (Prime x ∧ IsUnit y) ∨ (IsUnit x ∧ Prime y) := by refine ⟨fun h ↦ ?_, ?_⟩ · rcases of_irreducible_mul h.irreducible with hx | hy · exact Or.inr ⟨hx, (associated_unit_mul_left y x hx).prime h⟩ · exact Or.inl ⟨(associated_mul_unit_left x y hy).prime h, hy⟩ · rintro (⟨hx, hy⟩ | ⟨hx, hy⟩) · exact (associated_mul_unit_left x y hy).symm.prime hx · exact (associated_unit_mul_right y x hx).prime hy @[simp] lemma prime_pow_iff [CancelCommMonoidWithZero α] {p : α} {n : ℕ} : Prime (p ^ n) ↔ Prime p ∧ n = 1 := by refine ⟨fun hp ↦ ?_, fun ⟨hp, hn⟩ ↦ by simpa [hn]⟩ suffices n = 1 by aesop cases' n with n · simp at hp · rw [Nat.succ.injEq] rw [pow_succ', prime_mul_iff] at hp rcases hp with ⟨hp, hpn⟩ | ⟨hp, hpn⟩ · by_contra contra rw [isUnit_pow_iff contra] at hpn exact hp.not_unit hpn · exfalso exact hpn.not_unit (hp.pow n) theorem Irreducible.dvd_iff [Monoid α] {x y : α} (hx : Irreducible x) : y ∣ x ↔ IsUnit y ∨ Associated x y := by constructor · rintro ⟨z, hz⟩ obtain (h|h) := hx.isUnit_or_isUnit hz · exact Or.inl h · rw [hz] exact Or.inr (associated_mul_unit_left _ _ h) · rintro (hy|h) · exact hy.dvd · exact h.symm.dvd theorem Irreducible.associated_of_dvd [Monoid α] {p q : α} (p_irr : Irreducible p) (q_irr : Irreducible q) (dvd : p ∣ q) : Associated p q := ((q_irr.dvd_iff.mp dvd).resolve_left p_irr.not_unit).symm theorem Irreducible.dvd_irreducible_iff_associated [Monoid α] {p q : α} (pp : Irreducible p) (qp : Irreducible q) : p ∣ q ↔ Associated p q := ⟨Irreducible.associated_of_dvd pp qp, Associated.dvd⟩ theorem Prime.associated_of_dvd [CancelCommMonoidWithZero α] {p q : α} (p_prime : Prime p) (q_prime : Prime q) (dvd : p ∣ q) : Associated p q := p_prime.irreducible.associated_of_dvd q_prime.irreducible dvd theorem Prime.dvd_prime_iff_associated [CancelCommMonoidWithZero α] {p q : α} (pp : Prime p) (qp : Prime q) : p ∣ q ↔ Associated p q := pp.irreducible.dvd_irreducible_iff_associated qp.irreducible theorem Associated.prime_iff [CommMonoidWithZero α] {p q : α} (h : p ~ᵤ q) : Prime p ↔ Prime q := ⟨h.prime, h.symm.prime⟩ protected theorem Associated.isUnit [Monoid α] {a b : α} (h : a ~ᵤ b) : IsUnit a → IsUnit b := let ⟨u, hu⟩ := h fun ⟨v, hv⟩ => ⟨v * u, by simp [hv, hu.symm]⟩ theorem Associated.isUnit_iff [Monoid α] {a b : α} (h : a ~ᵤ b) : IsUnit a ↔ IsUnit b := ⟨h.isUnit, h.symm.isUnit⟩ theorem Irreducible.isUnit_iff_not_associated_of_dvd [Monoid α] {x y : α} (hx : Irreducible x) (hy : y ∣ x) : IsUnit y ↔ ¬ Associated x y := ⟨fun hy hxy => hx.1 (hxy.symm.isUnit hy), (hx.dvd_iff.mp hy).resolve_right⟩ protected theorem Associated.irreducible [Monoid α] {p q : α} (h : p ~ᵤ q) (hp : Irreducible p) : Irreducible q := ⟨mt h.symm.isUnit hp.1, let ⟨u, hu⟩ := h fun a b hab => have hpab : p = a * (b * (u⁻¹ : αˣ)) := calc p = p * u * (u⁻¹ : αˣ) := by simp _ = _ := by rw [hu]; simp [hab, mul_assoc] (hp.isUnit_or_isUnit hpab).elim Or.inl fun ⟨v, hv⟩ => Or.inr ⟨v * u, by simp [hv]⟩⟩ protected theorem Associated.irreducible_iff [Monoid α] {p q : α} (h : p ~ᵤ q) : Irreducible p ↔ Irreducible q := ⟨h.irreducible, h.symm.irreducible⟩ theorem Associated.of_mul_left [CancelCommMonoidWithZero α] {a b c d : α} (h : a * b ~ᵤ c * d) (h₁ : a ~ᵤ c) (ha : a ≠ 0) : b ~ᵤ d := let ⟨u, hu⟩ := h let ⟨v, hv⟩ := Associated.symm h₁ ⟨u * (v : αˣ), mul_left_cancel₀ ha (by rw [← hv, mul_assoc c (v : α) d, mul_left_comm c, ← hu] simp [hv.symm, mul_assoc, mul_comm, mul_left_comm])⟩ theorem Associated.of_mul_right [CancelCommMonoidWithZero α] {a b c d : α} : a * b ~ᵤ c * d → b ~ᵤ d → b ≠ 0 → a ~ᵤ c := by rw [mul_comm a, mul_comm c]; exact Associated.of_mul_left theorem Associated.of_pow_associated_of_prime [CancelCommMonoidWithZero α] {p₁ p₂ : α} {k₁ k₂ : ℕ} (hp₁ : Prime p₁) (hp₂ : Prime p₂) (hk₁ : 0 < k₁) (h : p₁ ^ k₁ ~ᵤ p₂ ^ k₂) : p₁ ~ᵤ p₂ := by have : p₁ ∣ p₂ ^ k₂ := by rw [← h.dvd_iff_dvd_right] apply dvd_pow_self _ hk₁.ne' rw [← hp₁.dvd_prime_iff_associated hp₂] exact hp₁.dvd_of_dvd_pow this theorem Associated.of_pow_associated_of_prime' [CancelCommMonoidWithZero α] {p₁ p₂ : α} {k₁ k₂ : ℕ} (hp₁ : Prime p₁) (hp₂ : Prime p₂) (hk₂ : 0 < k₂) (h : p₁ ^ k₁ ~ᵤ p₂ ^ k₂) : p₁ ~ᵤ p₂ := (h.symm.of_pow_associated_of_prime hp₂ hp₁ hk₂).symm /-- See also `Irreducible.coprime_iff_not_dvd`. -/ lemma Irreducible.isRelPrime_iff_not_dvd [Monoid α] {p n : α} (hp : Irreducible p) : IsRelPrime p n ↔ ¬ p ∣ n := by refine ⟨fun h contra ↦ hp.not_unit (h dvd_rfl contra), fun hpn d hdp hdn ↦ ?_⟩ contrapose! hpn suffices Associated p d from this.dvd.trans hdn exact (hp.dvd_iff.mp hdp).resolve_left hpn lemma Irreducible.dvd_or_isRelPrime [Monoid α] {p n : α} (hp : Irreducible p) : p ∣ n ∨ IsRelPrime p n := Classical.or_iff_not_imp_left.mpr hp.isRelPrime_iff_not_dvd.2 section UniqueUnits variable [Monoid α] [Unique αˣ] theorem associated_iff_eq {x y : α} : x ~ᵤ y ↔ x = y := by constructor · rintro ⟨c, rfl⟩ rw [units_eq_one c, Units.val_one, mul_one] · rintro rfl rfl theorem associated_eq_eq : (Associated : α → α → Prop) = Eq := by ext rw [associated_iff_eq] theorem prime_dvd_prime_iff_eq {M : Type*} [CancelCommMonoidWithZero M] [Unique Mˣ] {p q : M} (pp : Prime p) (qp : Prime q) : p ∣ q ↔ p = q := by rw [pp.dvd_prime_iff_associated qp, ← associated_eq_eq] end UniqueUnits section UniqueUnits₀ variable {R : Type*} [CancelCommMonoidWithZero R] [Unique Rˣ] {p₁ p₂ : R} {k₁ k₂ : ℕ} theorem eq_of_prime_pow_eq (hp₁ : Prime p₁) (hp₂ : Prime p₂) (hk₁ : 0 < k₁) (h : p₁ ^ k₁ = p₂ ^ k₂) : p₁ = p₂ := by rw [← associated_iff_eq] at h ⊢ apply h.of_pow_associated_of_prime hp₁ hp₂ hk₁ theorem eq_of_prime_pow_eq' (hp₁ : Prime p₁) (hp₂ : Prime p₂) (hk₁ : 0 < k₂) (h : p₁ ^ k₁ = p₂ ^ k₂) : p₁ = p₂ := by rw [← associated_iff_eq] at h ⊢ apply h.of_pow_associated_of_prime' hp₁ hp₂ hk₁ end UniqueUnits₀ /-- The quotient of a monoid by the `Associated` relation. Two elements `x` and `y` are associated iff there is a unit `u` such that `x * u = y`. There is a natural monoid structure on `Associates α`. -/ abbrev Associates (α : Type*) [Monoid α] : Type _ := Quotient (Associated.setoid α) namespace Associates open Associated /-- The canonical quotient map from a monoid `α` into the `Associates` of `α` -/ protected abbrev mk {α : Type*} [Monoid α] (a : α) : Associates α := ⟦a⟧ instance [Monoid α] : Inhabited (Associates α) := ⟨⟦1⟧⟩ theorem mk_eq_mk_iff_associated [Monoid α] {a b : α} : Associates.mk a = Associates.mk b ↔ a ~ᵤ b := Iff.intro Quotient.exact Quot.sound theorem quotient_mk_eq_mk [Monoid α] (a : α) : ⟦a⟧ = Associates.mk a := rfl theorem quot_mk_eq_mk [Monoid α] (a : α) : Quot.mk Setoid.r a = Associates.mk a := rfl @[simp] theorem quot_out [Monoid α] (a : Associates α) : Associates.mk (Quot.out a) = a := by rw [← quot_mk_eq_mk, Quot.out_eq] theorem mk_quot_out [Monoid α] (a : α) : Quot.out (Associates.mk a) ~ᵤ a := by rw [← Associates.mk_eq_mk_iff_associated, Associates.quot_out] theorem forall_associated [Monoid α] {p : Associates α → Prop} : (∀ a, p a) ↔ ∀ a, p (Associates.mk a) := Iff.intro (fun h _ => h _) fun h a => Quotient.inductionOn a h theorem mk_surjective [Monoid α] : Function.Surjective (@Associates.mk α _) := forall_associated.2 fun a => ⟨a, rfl⟩ instance [Monoid α] : One (Associates α) := ⟨⟦1⟧⟩ @[simp] theorem mk_one [Monoid α] : Associates.mk (1 : α) = 1 := rfl theorem one_eq_mk_one [Monoid α] : (1 : Associates α) = Associates.mk 1 := rfl @[simp] theorem mk_eq_one [Monoid α] {a : α} : Associates.mk a = 1 ↔ IsUnit a := by rw [← mk_one, mk_eq_mk_iff_associated, associated_one_iff_isUnit] instance [Monoid α] : Bot (Associates α) := ⟨1⟩ theorem bot_eq_one [Monoid α] : (⊥ : Associates α) = 1 := rfl theorem exists_rep [Monoid α] (a : Associates α) : ∃ a0 : α, Associates.mk a0 = a := Quot.exists_rep a instance [Monoid α] [Subsingleton α] : Unique (Associates α) where default := 1 uniq := forall_associated.2 fun _ ↦ mk_eq_one.2 <| isUnit_of_subsingleton _ theorem mk_injective [Monoid α] [Unique (Units α)] : Function.Injective (@Associates.mk α _) := fun _ _ h => associated_iff_eq.mp (Associates.mk_eq_mk_iff_associated.mp h) section CommMonoid variable [CommMonoid α] instance instMul : Mul (Associates α) := ⟨Quotient.map₂ (· * ·) fun _ _ h₁ _ _ h₂ ↦ h₁.mul_mul h₂⟩ theorem mk_mul_mk {x y : α} : Associates.mk x * Associates.mk y = Associates.mk (x * y) := rfl instance instCommMonoid : CommMonoid (Associates α) where one := 1 mul := (· * ·) mul_one a' := Quotient.inductionOn a' fun a => show ⟦a * 1⟧ = ⟦a⟧ by simp one_mul a' := Quotient.inductionOn a' fun a => show ⟦1 * a⟧ = ⟦a⟧ by simp mul_assoc a' b' c' := Quotient.inductionOn₃ a' b' c' fun a b c => show ⟦a * b * c⟧ = ⟦a * (b * c)⟧ by rw [mul_assoc] mul_comm a' b' := Quotient.inductionOn₂ a' b' fun a b => show ⟦a * b⟧ = ⟦b * a⟧ by rw [mul_comm] instance instPreorder : Preorder (Associates α) where le := Dvd.dvd le_refl := dvd_refl le_trans a b c := dvd_trans /-- `Associates.mk` as a `MonoidHom`. -/ protected def mkMonoidHom : α →* Associates α where toFun := Associates.mk map_one' := mk_one map_mul' _ _ := mk_mul_mk @[simp] theorem mkMonoidHom_apply (a : α) : Associates.mkMonoidHom a = Associates.mk a := rfl theorem associated_map_mk {f : Associates α →* α} (hinv : Function.RightInverse f Associates.mk) (a : α) : a ~ᵤ f (Associates.mk a) := Associates.mk_eq_mk_iff_associated.1 (hinv (Associates.mk a)).symm theorem mk_pow (a : α) (n : ℕ) : Associates.mk (a ^ n) = Associates.mk a ^ n := by induction n <;> simp [*, pow_succ, Associates.mk_mul_mk.symm] theorem dvd_eq_le : ((· ∣ ·) : Associates α → Associates α → Prop) = (· ≤ ·) := rfl theorem mul_eq_one_iff {x y : Associates α} : x * y = 1 ↔ x = 1 ∧ y = 1 := Iff.intro (Quotient.inductionOn₂ x y fun a b h => have : a * b ~ᵤ 1 := Quotient.exact h ⟨Quotient.sound <| associated_one_of_associated_mul_one this, Quotient.sound <| associated_one_of_associated_mul_one (b := a) (by rwa [mul_comm])⟩) (by simp (config := { contextual := true })) theorem units_eq_one (u : (Associates α)ˣ) : u = 1 := Units.ext (mul_eq_one_iff.1 u.val_inv).1 instance uniqueUnits : Unique (Associates α)ˣ where default := 1 uniq := Associates.units_eq_one @[simp] theorem coe_unit_eq_one (u : (Associates α)ˣ) : (u : Associates α) = 1 := by simp [eq_iff_true_of_subsingleton] theorem isUnit_iff_eq_one (a : Associates α) : IsUnit a ↔ a = 1 := Iff.intro (fun ⟨_, h⟩ => h ▸ coe_unit_eq_one _) fun h => h.symm ▸ isUnit_one theorem isUnit_iff_eq_bot {a : Associates α} : IsUnit a ↔ a = ⊥ := by rw [Associates.isUnit_iff_eq_one, bot_eq_one] theorem isUnit_mk {a : α} : IsUnit (Associates.mk a) ↔ IsUnit a := calc IsUnit (Associates.mk a) ↔ a ~ᵤ 1 := by rw [isUnit_iff_eq_one, one_eq_mk_one, mk_eq_mk_iff_associated] _ ↔ IsUnit a := associated_one_iff_isUnit section Order theorem mul_mono {a b c d : Associates α} (h₁ : a ≤ b) (h₂ : c ≤ d) : a * c ≤ b * d := let ⟨x, hx⟩ := h₁ let ⟨y, hy⟩ := h₂ ⟨x * y, by simp [hx, hy, mul_comm, mul_assoc, mul_left_comm]⟩ theorem one_le {a : Associates α} : 1 ≤ a := Dvd.intro _ (one_mul a) theorem le_mul_right {a b : Associates α} : a ≤ a * b := ⟨b, rfl⟩ theorem le_mul_left {a b : Associates α} : a ≤ b * a := by rw [mul_comm]; exact le_mul_right instance instOrderBot : OrderBot (Associates α) where bot := 1 bot_le _ := one_le end Order @[simp] theorem mk_dvd_mk {a b : α} : Associates.mk a ∣ Associates.mk b ↔ a ∣ b := by simp only [dvd_def, mk_surjective.exists, mk_mul_mk, mk_eq_mk_iff_associated, Associated.comm (x := b)] constructor · rintro ⟨x, u, rfl⟩ exact ⟨_, mul_assoc ..⟩ · rintro ⟨c, rfl⟩ use c theorem dvd_of_mk_le_mk {a b : α} : Associates.mk a ≤ Associates.mk b → a ∣ b := mk_dvd_mk.mp theorem mk_le_mk_of_dvd {a b : α} : a ∣ b → Associates.mk a ≤ Associates.mk b := mk_dvd_mk.mpr theorem mk_le_mk_iff_dvd {a b : α} : Associates.mk a ≤ Associates.mk b ↔ a ∣ b := mk_dvd_mk @[deprecated (since := "2024-03-16")] alias mk_le_mk_iff_dvd_iff := mk_le_mk_iff_dvd @[simp] theorem isPrimal_mk {a : α} : IsPrimal (Associates.mk a) ↔ IsPrimal a := by simp_rw [IsPrimal, forall_associated, mk_surjective.exists, mk_mul_mk, mk_dvd_mk] constructor <;> intro h b c dvd <;> obtain ⟨a₁, a₂, h₁, h₂, eq⟩ := @h b c dvd · obtain ⟨u, rfl⟩ := mk_eq_mk_iff_associated.mp eq.symm exact ⟨a₁, a₂ * u, h₁, Units.mul_right_dvd.mpr h₂, mul_assoc _ _ _⟩ · exact ⟨a₁, a₂, h₁, h₂, congr_arg _ eq⟩ @[deprecated (since := "2024-03-16")] alias isPrimal_iff := isPrimal_mk @[simp] theorem decompositionMonoid_iff : DecompositionMonoid (Associates α) ↔ DecompositionMonoid α := by simp_rw [_root_.decompositionMonoid_iff, forall_associated, isPrimal_mk] instance instDecompositionMonoid [DecompositionMonoid α] : DecompositionMonoid (Associates α) := decompositionMonoid_iff.mpr ‹_› @[simp] theorem mk_isRelPrime_iff {a b : α} : IsRelPrime (Associates.mk a) (Associates.mk b) ↔ IsRelPrime a b := by simp_rw [IsRelPrime, forall_associated, mk_dvd_mk, isUnit_mk] end CommMonoid instance [Zero α] [Monoid α] : Zero (Associates α) := ⟨⟦0⟧⟩ instance [Zero α] [Monoid α] : Top (Associates α) := ⟨0⟩ @[simp] theorem mk_zero [Zero α] [Monoid α] : Associates.mk (0 : α) = 0 := rfl section MonoidWithZero variable [MonoidWithZero α] @[simp] theorem mk_eq_zero {a : α} : Associates.mk a = 0 ↔ a = 0 := ⟨fun h => (associated_zero_iff_eq_zero a).1 <| Quotient.exact h, fun h => h.symm ▸ rfl⟩ @[simp] theorem quot_out_zero : Quot.out (0 : Associates α) = 0 := by rw [← mk_eq_zero, quot_out] theorem mk_ne_zero {a : α} : Associates.mk a ≠ 0 ↔ a ≠ 0 := not_congr mk_eq_zero instance [Nontrivial α] : Nontrivial (Associates α) := ⟨⟨1, 0, mk_ne_zero.2 one_ne_zero⟩⟩ theorem exists_non_zero_rep {a : Associates α} : a ≠ 0 → ∃ a0 : α, a0 ≠ 0 ∧ Associates.mk a0 = a := Quotient.inductionOn a fun b nz => ⟨b, mt (congr_arg Quotient.mk'') nz, rfl⟩ end MonoidWithZero section CommMonoidWithZero variable [CommMonoidWithZero α] instance instCommMonoidWithZero : CommMonoidWithZero (Associates α) where zero_mul := forall_associated.2 fun a ↦ by rw [← mk_zero, mk_mul_mk, zero_mul] mul_zero := forall_associated.2 fun a ↦ by rw [← mk_zero, mk_mul_mk, mul_zero] instance instOrderTop : OrderTop (Associates α) where top := 0 le_top := dvd_zero @[simp] protected theorem le_zero (a : Associates α) : a ≤ 0 := le_top instance instBoundedOrder : BoundedOrder (Associates α) where instance [DecidableRel ((· ∣ ·) : α → α → Prop)] : DecidableRel ((· ∣ ·) : Associates α → Associates α → Prop) := fun a b => Quotient.recOnSubsingleton₂ a b fun _ _ => decidable_of_iff' _ mk_dvd_mk theorem Prime.le_or_le {p : Associates α} (hp : Prime p) {a b : Associates α} (h : p ≤ a * b) : p ≤ a ∨ p ≤ b := hp.2.2 a b h @[simp] theorem prime_mk {p : α} : Prime (Associates.mk p) ↔ Prime p := by rw [Prime, _root_.Prime, forall_associated] simp only [forall_associated, mk_ne_zero, isUnit_mk, mk_mul_mk, mk_dvd_mk] @[simp] theorem irreducible_mk {a : α} : Irreducible (Associates.mk a) ↔ Irreducible a := by simp only [irreducible_iff, isUnit_mk, forall_associated, isUnit_mk, mk_mul_mk, mk_eq_mk_iff_associated, Associated.comm (x := a)] apply Iff.rfl.and constructor · rintro h x y rfl exact h _ _ <| .refl _ · rintro h x y ⟨u, rfl⟩ simpa using h x (y * u) (mul_assoc _ _ _) @[simp] theorem mk_dvdNotUnit_mk_iff {a b : α} : DvdNotUnit (Associates.mk a) (Associates.mk b) ↔ DvdNotUnit a b := by simp only [DvdNotUnit, mk_ne_zero, mk_surjective.exists, isUnit_mk, mk_mul_mk, mk_eq_mk_iff_associated, Associated.comm (x := b)] refine Iff.rfl.and ?_ constructor · rintro ⟨x, hx, u, rfl⟩ refine ⟨x * u, ?_, mul_assoc ..⟩ simpa · rintro ⟨x, ⟨hx, rfl⟩⟩ use x theorem dvdNotUnit_of_lt {a b : Associates α} (hlt : a < b) : DvdNotUnit a b := by constructor · rintro rfl apply not_lt_of_le _ hlt apply dvd_zero rcases hlt with ⟨⟨x, rfl⟩, ndvd⟩ refine ⟨x, ?_, rfl⟩ contrapose! ndvd rcases ndvd with ⟨u, rfl⟩ simp theorem irreducible_iff_prime_iff : (∀ a : α, Irreducible a ↔ Prime a) ↔ ∀ a : Associates α, Irreducible a ↔ Prime a := by simp_rw [forall_associated, irreducible_mk, prime_mk] end CommMonoidWithZero section CancelCommMonoidWithZero variable [CancelCommMonoidWithZero α] instance instPartialOrder : PartialOrder (Associates α) where le_antisymm := mk_surjective.forall₂.2 fun _a _b hab hba => mk_eq_mk_iff_associated.2 <| associated_of_dvd_dvd (dvd_of_mk_le_mk hab) (dvd_of_mk_le_mk hba) instance instCancelCommMonoidWithZero : CancelCommMonoidWithZero (Associates α) := { (by infer_instance : CommMonoidWithZero (Associates α)) with mul_left_cancel_of_ne_zero := by rintro ⟨a⟩ ⟨b⟩ ⟨c⟩ ha h rcases Quotient.exact' h with ⟨u, hu⟩ have hu : a * (b * ↑u) = a * c := by rwa [← mul_assoc] exact Quotient.sound' ⟨u, mul_left_cancel₀ (mk_ne_zero.1 ha) hu⟩ } theorem _root_.associates_irreducible_iff_prime [DecompositionMonoid α] {p : Associates α} : Irreducible p ↔ Prime p := irreducible_iff_prime instance : NoZeroDivisors (Associates α) := by infer_instance theorem le_of_mul_le_mul_left (a b c : Associates α) (ha : a ≠ 0) : a * b ≤ a * c → b ≤ c | ⟨d, hd⟩ => ⟨d, mul_left_cancel₀ ha <| by rwa [← mul_assoc]⟩ theorem one_or_eq_of_le_of_prime {p m : Associates α} (hp : Prime p) (hle : m ≤ p) : m = 1 ∨ m = p := by rcases mk_surjective p with ⟨p, rfl⟩ rcases mk_surjective m with ⟨m, rfl⟩ simpa [mk_eq_mk_iff_associated, Associated.comm, -Quotient.eq] using (prime_mk.1 hp).irreducible.dvd_iff.mp (mk_le_mk_iff_dvd.1 hle) theorem dvdNotUnit_iff_lt {a b : Associates α} : DvdNotUnit a b ↔ a < b := dvd_and_not_dvd_iff.symm theorem le_one_iff {p : Associates α} : p ≤ 1 ↔ p = 1 := by rw [← Associates.bot_eq_one, le_bot_iff] end CancelCommMonoidWithZero end Associates section CommMonoidWithZero theorem DvdNotUnit.isUnit_of_irreducible_right [CommMonoidWithZero α] {p q : α} (h : DvdNotUnit p q) (hq : Irreducible q) : IsUnit p := by obtain ⟨_, x, hx, hx'⟩ := h exact Or.resolve_right ((irreducible_iff.1 hq).right p x hx') hx theorem not_irreducible_of_not_unit_dvdNotUnit [CommMonoidWithZero α] {p q : α} (hp : ¬IsUnit p) (h : DvdNotUnit p q) : ¬Irreducible q := mt h.isUnit_of_irreducible_right hp theorem DvdNotUnit.not_unit [CommMonoidWithZero α] {p q : α} (hp : DvdNotUnit p q) : ¬IsUnit q := by obtain ⟨-, x, hx, rfl⟩ := hp exact fun hc => hx (isUnit_iff_dvd_one.mpr (dvd_of_mul_left_dvd (isUnit_iff_dvd_one.mp hc))) theorem dvdNotUnit_of_dvdNotUnit_associated [CommMonoidWithZero α] [Nontrivial α] {p q r : α} (h : DvdNotUnit p q) (h' : Associated q r) : DvdNotUnit p r := by obtain ⟨u, rfl⟩ := Associated.symm h' obtain ⟨hp, x, hx⟩ := h refine ⟨hp, x * ↑u⁻¹, DvdNotUnit.not_unit ⟨u⁻¹.ne_zero, x, hx.left, mul_comm _ _⟩, ?_⟩ rw [← mul_assoc, ← hx.right, mul_assoc, Units.mul_inv, mul_one] end CommMonoidWithZero section CancelCommMonoidWithZero theorem isUnit_of_associated_mul [CancelCommMonoidWithZero α] {p b : α} (h : Associated (p * b) p) (hp : p ≠ 0) : IsUnit b := by cases' h with a ha refine isUnit_of_mul_eq_one b a ((mul_right_inj' hp).mp ?_) rwa [← mul_assoc, mul_one] theorem DvdNotUnit.not_associated [CancelCommMonoidWithZero α] {p q : α} (h : DvdNotUnit p q) : ¬Associated p q := by rintro ⟨a, rfl⟩ obtain ⟨hp, x, hx, hx'⟩ := h rcases (mul_right_inj' hp).mp hx' with rfl exact hx a.isUnit theorem DvdNotUnit.ne [CancelCommMonoidWithZero α] {p q : α} (h : DvdNotUnit p q) : p ≠ q := by by_contra hcontra obtain ⟨hp, x, hx', hx''⟩ := h conv_lhs at hx'' => rw [← hcontra, ← mul_one p] rw [(mul_left_cancel₀ hp hx'').symm] at hx' exact hx' isUnit_one theorem pow_injective_of_not_unit [CancelCommMonoidWithZero α] {q : α} (hq : ¬IsUnit q) (hq' : q ≠ 0) : Function.Injective fun n : ℕ => q ^ n := by refine injective_of_lt_imp_ne fun n m h => DvdNotUnit.ne ⟨pow_ne_zero n hq', q ^ (m - n), ?_, ?_⟩ · exact not_isUnit_of_not_isUnit_dvd hq (dvd_pow (dvd_refl _) (Nat.sub_pos_of_lt h).ne') · exact (pow_mul_pow_sub q h.le).symm theorem dvd_prime_pow [CancelCommMonoidWithZero α] {p q : α} (hp : Prime p) (n : ℕ) : q ∣ p ^ n ↔ ∃ i ≤ n, Associated q (p ^ i) := by induction' n with n ih generalizing q · simp [← isUnit_iff_dvd_one, associated_one_iff_isUnit] refine ⟨fun h => ?_, fun ⟨i, hi, hq⟩ => hq.dvd.trans (pow_dvd_pow p hi)⟩ rw [pow_succ'] at h rcases hp.left_dvd_or_dvd_right_of_dvd_mul h with (⟨q, rfl⟩ | hno) · rw [mul_dvd_mul_iff_left hp.ne_zero, ih] at h rcases h with ⟨i, hi, hq⟩ refine ⟨i + 1, Nat.succ_le_succ hi, (hq.mul_left p).trans ?_⟩ rw [pow_succ'] · obtain ⟨i, hi, hq⟩ := ih.mp hno exact ⟨i, hi.trans n.le_succ, hq⟩ end CancelCommMonoidWithZero assert_not_exists OrderedCommMonoid assert_not_exists Multiset
Algebra\Associated\OrderedCommMonoid.lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker -/ import Mathlib.Algebra.Associated.Basic import Mathlib.Algebra.Order.Monoid.Canonical.Defs /-! # Associated, prime, and irreducible elements. In this file we define the predicate `Prime p` saying that an element of a commutative monoid with zero is prime. Namely, `Prime p` means that `p` isn't zero, it isn't a unit, and `p ∣ a * b → p ∣ a ∨ p ∣ b` for all `a`, `b`; In decomposition monoids (e.g., `ℕ`, `ℤ`), this predicate is equivalent to `Irreducible`, however this is not true in general. We also define an equivalence relation `Associated` saying that two elements of a monoid differ by a multiplication by a unit. Then we show that the quotient type `Associates` is a monoid and prove basic properties of this quotient. -/ variable {α : Type*} [CancelCommMonoidWithZero α] namespace Associates instance instOrderedCommMonoid : OrderedCommMonoid (Associates α) where mul_le_mul_left := fun a _ ⟨d, hd⟩ c => hd.symm ▸ mul_assoc c a d ▸ le_mul_right (α := α) instance : CanonicallyOrderedCommMonoid (Associates α) where exists_mul_of_le h := h le_self_mul _ b := ⟨b, rfl⟩ bot_le _ := one_le end Associates
Algebra\BigOperators\Associated.lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Jens Wagemaker, Anne Baanen -/ import Mathlib.Algebra.Associated.Basic import Mathlib.Algebra.BigOperators.Finsupp /-! # Products of associated, prime, and irreducible elements. This file contains some theorems relating definitions in `Algebra.Associated` and products of multisets, finsets, and finsupps. -/ variable {α β γ δ : Type*} -- the same local notation used in `Algebra.Associated` local infixl:50 " ~ᵤ " => Associated namespace Prime variable [CommMonoidWithZero α] {p : α} theorem exists_mem_multiset_dvd (hp : Prime p) {s : Multiset α} : p ∣ s.prod → ∃ a ∈ s, p ∣ a := Multiset.induction_on s (fun h => (hp.not_dvd_one h).elim) fun a s ih h => have : p ∣ a * s.prod := by simpa using h match hp.dvd_or_dvd this with | Or.inl h => ⟨a, Multiset.mem_cons_self a s, h⟩ | Or.inr h => let ⟨a, has, h⟩ := ih h ⟨a, Multiset.mem_cons_of_mem has, h⟩ theorem exists_mem_multiset_map_dvd (hp : Prime p) {s : Multiset β} {f : β → α} : p ∣ (s.map f).prod → ∃ a ∈ s, p ∣ f a := fun h => by simpa only [exists_prop, Multiset.mem_map, exists_exists_and_eq_and] using hp.exists_mem_multiset_dvd h theorem exists_mem_finset_dvd (hp : Prime p) {s : Finset β} {f : β → α} : p ∣ s.prod f → ∃ i ∈ s, p ∣ f i := hp.exists_mem_multiset_map_dvd end Prime theorem Prod.associated_iff {M N : Type*} [Monoid M] [Monoid N] {x z : M × N} : x ~ᵤ z ↔ x.1 ~ᵤ z.1 ∧ x.2 ~ᵤ z.2 := ⟨fun ⟨u, hu⟩ => ⟨⟨(MulEquiv.prodUnits.toFun u).1, (Prod.eq_iff_fst_eq_snd_eq.1 hu).1⟩, ⟨(MulEquiv.prodUnits.toFun u).2, (Prod.eq_iff_fst_eq_snd_eq.1 hu).2⟩⟩, fun ⟨⟨u₁, h₁⟩, ⟨u₂, h₂⟩⟩ => ⟨MulEquiv.prodUnits.invFun (u₁, u₂), Prod.eq_iff_fst_eq_snd_eq.2 ⟨h₁, h₂⟩⟩⟩ theorem Associated.prod {M : Type*} [CommMonoid M] {ι : Type*} (s : Finset ι) (f : ι → M) (g : ι → M) (h : ∀ i, i ∈ s → (f i) ~ᵤ (g i)) : (∏ i ∈ s, f i) ~ᵤ (∏ i ∈ s, g i) := by induction s using Finset.induction with | empty => simp only [Finset.prod_empty] rfl | @insert j s hjs IH => classical convert_to (∏ i ∈ insert j s, f i) ~ᵤ (∏ i ∈ insert j s, g i) rw [Finset.prod_insert hjs, Finset.prod_insert hjs] exact Associated.mul_mul (h j (Finset.mem_insert_self j s)) (IH (fun i hi ↦ h i (Finset.mem_insert_of_mem hi))) theorem exists_associated_mem_of_dvd_prod [CancelCommMonoidWithZero α] {p : α} (hp : Prime p) {s : Multiset α} : (∀ r ∈ s, Prime r) → p ∣ s.prod → ∃ q ∈ s, p ~ᵤ q := Multiset.induction_on s (by simp [mt isUnit_iff_dvd_one.2 hp.not_unit]) fun a s ih hs hps => by rw [Multiset.prod_cons] at hps cases' hp.dvd_or_dvd hps with h h · have hap := hs a (Multiset.mem_cons.2 (Or.inl rfl)) exact ⟨a, Multiset.mem_cons_self a _, hp.associated_of_dvd hap h⟩ · rcases ih (fun r hr => hs _ (Multiset.mem_cons.2 (Or.inr hr))) h with ⟨q, hq₁, hq₂⟩ exact ⟨q, Multiset.mem_cons.2 (Or.inr hq₁), hq₂⟩ theorem Multiset.prod_primes_dvd [CancelCommMonoidWithZero α] [∀ a : α, DecidablePred (Associated a)] {s : Multiset α} (n : α) (h : ∀ a ∈ s, Prime a) (div : ∀ a ∈ s, a ∣ n) (uniq : ∀ a, s.countP (Associated a) ≤ 1) : s.prod ∣ n := by induction' s using Multiset.induction_on with a s induct n primes divs generalizing n · simp only [Multiset.prod_zero, one_dvd] · rw [Multiset.prod_cons] obtain ⟨k, rfl⟩ : a ∣ n := div a (Multiset.mem_cons_self a s) apply mul_dvd_mul_left a refine induct _ (fun a ha => h a (Multiset.mem_cons_of_mem ha)) (fun b b_in_s => ?_) fun a => (Multiset.countP_le_of_le _ (Multiset.le_cons_self _ _)).trans (uniq a) have b_div_n := div b (Multiset.mem_cons_of_mem b_in_s) have a_prime := h a (Multiset.mem_cons_self a s) have b_prime := h b (Multiset.mem_cons_of_mem b_in_s) refine (b_prime.dvd_or_dvd b_div_n).resolve_left fun b_div_a => ?_ have assoc := b_prime.associated_of_dvd a_prime b_div_a have := uniq a rw [Multiset.countP_cons_of_pos _ (Associated.refl _), Nat.succ_le_succ_iff, ← not_lt, Multiset.countP_pos] at this exact this ⟨b, b_in_s, assoc.symm⟩ theorem Finset.prod_primes_dvd [CancelCommMonoidWithZero α] [Unique αˣ] {s : Finset α} (n : α) (h : ∀ a ∈ s, Prime a) (div : ∀ a ∈ s, a ∣ n) : (∏ p ∈ s, p) ∣ n := by classical exact Multiset.prod_primes_dvd n (by simpa only [Multiset.map_id', Finset.mem_def] using h) (by simpa only [Multiset.map_id', Finset.mem_def] using div) (by simp only [Multiset.map_id', associated_eq_eq, Multiset.countP_eq_card_filter, ← s.val.count_eq_card_filter_eq, ← Multiset.nodup_iff_count_le_one, s.nodup]) namespace Associates section CommMonoid variable [CommMonoid α] theorem prod_mk {p : Multiset α} : (p.map Associates.mk).prod = Associates.mk p.prod := Multiset.induction_on p (by simp) fun a s ih => by simp [ih, Associates.mk_mul_mk] theorem finset_prod_mk {p : Finset β} {f : β → α} : (∏ i ∈ p, Associates.mk (f i)) = Associates.mk (∏ i ∈ p, f i) := by -- Porting note: added have : (fun i => Associates.mk (f i)) = Associates.mk ∘ f := funext fun x => Function.comp_apply rw [Finset.prod_eq_multiset_prod, this, ← Multiset.map_map, prod_mk, ← Finset.prod_eq_multiset_prod] theorem rel_associated_iff_map_eq_map {p q : Multiset α} : Multiset.Rel Associated p q ↔ p.map Associates.mk = q.map Associates.mk := by rw [← Multiset.rel_eq, Multiset.rel_map] simp only [mk_eq_mk_iff_associated] theorem prod_eq_one_iff {p : Multiset (Associates α)} : p.prod = 1 ↔ ∀ a ∈ p, (a : Associates α) = 1 := Multiset.induction_on p (by simp) (by simp (config := { contextual := true }) [mul_eq_one_iff, or_imp, forall_and]) theorem prod_le_prod {p q : Multiset (Associates α)} (h : p ≤ q) : p.prod ≤ q.prod := by haveI := Classical.decEq (Associates α) haveI := Classical.decEq α suffices p.prod ≤ (p + (q - p)).prod by rwa [add_tsub_cancel_of_le h] at this suffices p.prod * 1 ≤ p.prod * (q - p).prod by simpa exact mul_mono (le_refl p.prod) one_le end CommMonoid section CancelCommMonoidWithZero variable [CancelCommMonoidWithZero α] theorem exists_mem_multiset_le_of_prime {s : Multiset (Associates α)} {p : Associates α} (hp : Prime p) : p ≤ s.prod → ∃ a ∈ s, p ≤ a := Multiset.induction_on s (fun ⟨d, Eq⟩ => (hp.ne_one (mul_eq_one_iff.1 Eq.symm).1).elim) fun a s ih h => have : p ≤ a * s.prod := by simpa using h match Prime.le_or_le hp this with | Or.inl h => ⟨a, Multiset.mem_cons_self a s, h⟩ | Or.inr h => let ⟨a, has, h⟩ := ih h ⟨a, Multiset.mem_cons_of_mem has, h⟩ end CancelCommMonoidWithZero end Associates namespace Multiset theorem prod_ne_zero_of_prime [CancelCommMonoidWithZero α] [Nontrivial α] (s : Multiset α) (h : ∀ x ∈ s, Prime x) : s.prod ≠ 0 := Multiset.prod_ne_zero fun h0 => Prime.ne_zero (h 0 h0) rfl end Multiset open Finset Finsupp section CommMonoidWithZero variable {M : Type*} [CommMonoidWithZero M] theorem Prime.dvd_finset_prod_iff {S : Finset α} {p : M} (pp : Prime p) (g : α → M) : p ∣ S.prod g ↔ ∃ a ∈ S, p ∣ g a := ⟨pp.exists_mem_finset_dvd, fun ⟨_, ha1, ha2⟩ => dvd_trans ha2 (dvd_prod_of_mem g ha1)⟩ theorem Prime.not_dvd_finset_prod {S : Finset α} {p : M} (pp : Prime p) {g : α → M} (hS : ∀ a ∈ S, ¬p ∣ g a) : ¬p ∣ S.prod g := by exact mt (Prime.dvd_finset_prod_iff pp _).1 <| not_exists.2 fun a => not_and.2 (hS a) theorem Prime.dvd_finsupp_prod_iff {f : α →₀ M} {g : α → M → ℕ} {p : ℕ} (pp : Prime p) : p ∣ f.prod g ↔ ∃ a ∈ f.support, p ∣ g a (f a) := Prime.dvd_finset_prod_iff pp _ theorem Prime.not_dvd_finsupp_prod {f : α →₀ M} {g : α → M → ℕ} {p : ℕ} (pp : Prime p) (hS : ∀ a ∈ f.support, ¬p ∣ g a (f a)) : ¬p ∣ f.prod g := Prime.not_dvd_finset_prod pp hS end CommMonoidWithZero
Algebra\BigOperators\Fin.lean
/- Copyright (c) 2020 Yury Kudryashov, Anne Baanen. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov, Anne Baanen -/ import Mathlib.Algebra.BigOperators.Ring import Mathlib.Algebra.Group.Action.Pi import Mathlib.Data.Fintype.BigOperators import Mathlib.Data.Fintype.Fin import Mathlib.Logic.Equiv.Fin /-! # Big operators and `Fin` Some results about products and sums over the type `Fin`. The most important results are the induction formulas `Fin.prod_univ_castSucc` and `Fin.prod_univ_succ`, and the formula `Fin.prod_const` for the product of a constant function. These results have variants for sums instead of products. ## Main declarations * `finFunctionFinEquiv`: An explicit equivalence between `Fin n → Fin m` and `Fin (m ^ n)`. -/ open Finset variable {α : Type*} {β : Type*} namespace Finset @[to_additive] theorem prod_range [CommMonoid β] {n : ℕ} (f : ℕ → β) : ∏ i ∈ Finset.range n, f i = ∏ i : Fin n, f i := (Fin.prod_univ_eq_prod_range _ _).symm end Finset namespace Fin @[to_additive] theorem prod_ofFn [CommMonoid β] {n : ℕ} (f : Fin n → β) : (List.ofFn f).prod = ∏ i, f i := by simp [prod_eq_multiset_prod] @[to_additive] theorem prod_univ_def [CommMonoid β] {n : ℕ} (f : Fin n → β) : ∏ i, f i = ((List.finRange n).map f).prod := by rw [← List.ofFn_eq_map, prod_ofFn] /-- A product of a function `f : Fin 0 → β` is `1` because `Fin 0` is empty -/ @[to_additive "A sum of a function `f : Fin 0 → β` is `0` because `Fin 0` is empty"] theorem prod_univ_zero [CommMonoid β] (f : Fin 0 → β) : ∏ i, f i = 1 := rfl /-- A product of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the product of `f x`, for some `x : Fin (n + 1)` times the remaining product -/ @[to_additive "A sum of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the sum of `f x`, for some `x : Fin (n + 1)` plus the remaining product"] theorem prod_univ_succAbove [CommMonoid β] {n : ℕ} (f : Fin (n + 1) → β) (x : Fin (n + 1)) : ∏ i, f i = f x * ∏ i : Fin n, f (x.succAbove i) := by rw [univ_succAbove, prod_cons, Finset.prod_map _ x.succAboveEmb] rfl /-- A product of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the product of `f 0` plus the remaining product -/ @[to_additive "A sum of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the sum of `f 0` plus the remaining product"] theorem prod_univ_succ [CommMonoid β] {n : ℕ} (f : Fin (n + 1) → β) : ∏ i, f i = f 0 * ∏ i : Fin n, f i.succ := prod_univ_succAbove f 0 /-- A product of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the product of `f (Fin.last n)` plus the remaining product -/ @[to_additive "A sum of a function `f : Fin (n + 1) → β` over all `Fin (n + 1)` is the sum of `f (Fin.last n)` plus the remaining sum"] theorem prod_univ_castSucc [CommMonoid β] {n : ℕ} (f : Fin (n + 1) → β) : ∏ i, f i = (∏ i : Fin n, f (Fin.castSucc i)) * f (last n) := by simpa [mul_comm] using prod_univ_succAbove f (last n) @[to_additive (attr := simp)] theorem prod_univ_get [CommMonoid α] (l : List α) : ∏ i : Fin l.length, l[i.1] = l.prod := by simp [Finset.prod_eq_multiset_prod] @[to_additive (attr := simp)] theorem prod_univ_get' [CommMonoid β] (l : List α) (f : α → β) : ∏ i : Fin l.length, f l[i.1] = (l.map f).prod := by simp [Finset.prod_eq_multiset_prod] @[to_additive] theorem prod_cons [CommMonoid β] {n : ℕ} (x : β) (f : Fin n → β) : (∏ i : Fin n.succ, (cons x f : Fin n.succ → β) i) = x * ∏ i : Fin n, f i := by simp_rw [prod_univ_succ, cons_zero, cons_succ] @[to_additive sum_univ_one] theorem prod_univ_one [CommMonoid β] (f : Fin 1 → β) : ∏ i, f i = f 0 := by simp @[to_additive (attr := simp)] theorem prod_univ_two [CommMonoid β] (f : Fin 2 → β) : ∏ i, f i = f 0 * f 1 := by simp [prod_univ_succ] @[to_additive] theorem prod_univ_two' [CommMonoid β] (f : α → β) (a b : α) : ∏ i, f (![a, b] i) = f a * f b := prod_univ_two _ @[to_additive] theorem prod_univ_three [CommMonoid β] (f : Fin 3 → β) : ∏ i, f i = f 0 * f 1 * f 2 := by rw [prod_univ_castSucc, prod_univ_two] rfl @[to_additive] theorem prod_univ_four [CommMonoid β] (f : Fin 4 → β) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 := by rw [prod_univ_castSucc, prod_univ_three] rfl @[to_additive] theorem prod_univ_five [CommMonoid β] (f : Fin 5 → β) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 := by rw [prod_univ_castSucc, prod_univ_four] rfl @[to_additive] theorem prod_univ_six [CommMonoid β] (f : Fin 6 → β) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 := by rw [prod_univ_castSucc, prod_univ_five] rfl @[to_additive] theorem prod_univ_seven [CommMonoid β] (f : Fin 7 → β) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 * f 6 := by rw [prod_univ_castSucc, prod_univ_six] rfl @[to_additive] theorem prod_univ_eight [CommMonoid β] (f : Fin 8 → β) : ∏ i, f i = f 0 * f 1 * f 2 * f 3 * f 4 * f 5 * f 6 * f 7 := by rw [prod_univ_castSucc, prod_univ_seven] rfl theorem sum_pow_mul_eq_add_pow {n : ℕ} {R : Type*} [CommSemiring R] (a b : R) : (∑ s : Finset (Fin n), a ^ s.card * b ^ (n - s.card)) = (a + b) ^ n := by simpa using Fintype.sum_pow_mul_eq_add_pow (Fin n) a b theorem prod_const [CommMonoid α] (n : ℕ) (x : α) : ∏ _i : Fin n, x = x ^ n := by simp theorem sum_const [AddCommMonoid α] (n : ℕ) (x : α) : ∑ _i : Fin n, x = n • x := by simp @[to_additive] theorem prod_Ioi_zero {M : Type*} [CommMonoid M] {n : ℕ} {v : Fin n.succ → M} : ∏ i ∈ Ioi 0, v i = ∏ j : Fin n, v j.succ := by rw [Ioi_zero_eq_map, Finset.prod_map, val_succEmb] @[to_additive] theorem prod_Ioi_succ {M : Type*} [CommMonoid M] {n : ℕ} (i : Fin n) (v : Fin n.succ → M) : ∏ j ∈ Ioi i.succ, v j = ∏ j ∈ Ioi i, v j.succ := by rw [Ioi_succ, Finset.prod_map, val_succEmb] @[to_additive] theorem prod_congr' {M : Type*} [CommMonoid M] {a b : ℕ} (f : Fin b → M) (h : a = b) : (∏ i : Fin a, f (cast h i)) = ∏ i : Fin b, f i := by subst h congr @[to_additive] theorem prod_univ_add {M : Type*} [CommMonoid M] {a b : ℕ} (f : Fin (a + b) → M) : (∏ i : Fin (a + b), f i) = (∏ i : Fin a, f (castAdd b i)) * ∏ i : Fin b, f (natAdd a i) := by rw [Fintype.prod_equiv finSumFinEquiv.symm f fun i => f (finSumFinEquiv.toFun i)] · apply Fintype.prod_sum_type · intro x simp only [Equiv.toFun_as_coe, Equiv.apply_symm_apply] @[to_additive] theorem prod_trunc {M : Type*} [CommMonoid M] {a b : ℕ} (f : Fin (a + b) → M) (hf : ∀ j : Fin b, f (natAdd a j) = 1) : (∏ i : Fin (a + b), f i) = ∏ i : Fin a, f (castLE (Nat.le.intro rfl) i) := by rw [prod_univ_add, Fintype.prod_eq_one _ hf, mul_one] rfl section PartialProd variable [Monoid α] {n : ℕ} /-- For `f = (a₁, ..., aₙ)` in `αⁿ`, `partialProd f` is `(1, a₁, a₁a₂, ..., a₁...aₙ)` in `αⁿ⁺¹`. -/ @[to_additive "For `f = (a₁, ..., aₙ)` in `αⁿ`, `partialSum f` is\n `(0, a₁, a₁ + a₂, ..., a₁ + ... + aₙ)` in `αⁿ⁺¹`."] def partialProd (f : Fin n → α) (i : Fin (n + 1)) : α := ((List.ofFn f).take i).prod @[to_additive (attr := simp)] theorem partialProd_zero (f : Fin n → α) : partialProd f 0 = 1 := by simp [partialProd] @[to_additive] theorem partialProd_succ (f : Fin n → α) (j : Fin n) : partialProd f j.succ = partialProd f (Fin.castSucc j) * f j := by simp [partialProd, List.take_succ, List.ofFnNthVal, dif_pos j.is_lt] @[to_additive] theorem partialProd_succ' (f : Fin (n + 1) → α) (j : Fin (n + 1)) : partialProd f j.succ = f 0 * partialProd (Fin.tail f) j := by simp [partialProd] rfl @[to_additive] theorem partialProd_left_inv {G : Type*} [Group G] (f : Fin (n + 1) → G) : (f 0 • partialProd fun i : Fin n => (f i)⁻¹ * f i.succ) = f := funext fun x => Fin.inductionOn x (by simp) fun x hx => by simp only [coe_eq_castSucc, Pi.smul_apply, smul_eq_mul] at hx ⊢ rw [partialProd_succ, ← mul_assoc, hx, mul_inv_cancel_left] @[to_additive] theorem partialProd_right_inv {G : Type*} [Group G] (f : Fin n → G) (i : Fin n) : (partialProd f (Fin.castSucc i))⁻¹ * partialProd f i.succ = f i := by cases' i with i hn induction i with | zero => simp [-Fin.succ_mk, partialProd_succ] | succ i hi => specialize hi (lt_trans (Nat.lt_succ_self i) hn) simp only [Fin.coe_eq_castSucc, Fin.succ_mk, Fin.castSucc_mk] at hi ⊢ rw [← Fin.succ_mk _ _ (lt_trans (Nat.lt_succ_self _) hn), ← Fin.succ_mk _ _ hn] simp only [partialProd_succ, mul_inv_rev, Fin.castSucc_mk] -- Porting note: was -- assoc_rw [hi, inv_mul_cancel_left] rw [← mul_assoc, mul_left_eq_self, mul_assoc, hi, mul_left_inv] /-- Let `(g₀, g₁, ..., gₙ)` be a tuple of elements in `Gⁿ⁺¹`. Then if `k < j`, this says `(g₀g₁...gₖ₋₁)⁻¹ * g₀g₁...gₖ = gₖ`. If `k = j`, it says `(g₀g₁...gₖ₋₁)⁻¹ * g₀g₁...gₖ₊₁ = gₖgₖ₊₁`. If `k > j`, it says `(g₀g₁...gₖ)⁻¹ * g₀g₁...gₖ₊₁ = gₖ₊₁.` Useful for defining group cohomology. -/ @[to_additive "Let `(g₀, g₁, ..., gₙ)` be a tuple of elements in `Gⁿ⁺¹`. Then if `k < j`, this says `-(g₀ + g₁ + ... + gₖ₋₁) + (g₀ + g₁ + ... + gₖ) = gₖ`. If `k = j`, it says `-(g₀ + g₁ + ... + gₖ₋₁) + (g₀ + g₁ + ... + gₖ₊₁) = gₖ + gₖ₊₁`. If `k > j`, it says `-(g₀ + g₁ + ... + gₖ) + (g₀ + g₁ + ... + gₖ₊₁) = gₖ₊₁.` Useful for defining group cohomology."] theorem inv_partialProd_mul_eq_contractNth {G : Type*} [Group G] (g : Fin (n + 1) → G) (j : Fin (n + 1)) (k : Fin n) : (partialProd g (j.succ.succAbove (Fin.castSucc k)))⁻¹ * partialProd g (j.succAbove k).succ = j.contractNth (· * ·) g k := by rcases lt_trichotomy (k : ℕ) j with (h | h | h) · rwa [succAbove_of_castSucc_lt, succAbove_of_castSucc_lt, partialProd_right_inv, contractNth_apply_of_lt] · assumption · rw [castSucc_lt_iff_succ_le, succ_le_succ_iff, le_iff_val_le_val] exact le_of_lt h · rwa [succAbove_of_castSucc_lt, succAbove_of_le_castSucc, partialProd_succ, castSucc_fin_succ, ← mul_assoc, partialProd_right_inv, contractNth_apply_of_eq] · simp [le_iff_val_le_val, ← h] · rw [castSucc_lt_iff_succ_le, succ_le_succ_iff, le_iff_val_le_val] exact le_of_eq h · rwa [succAbove_of_le_castSucc, succAbove_of_le_castSucc, partialProd_succ, partialProd_succ, castSucc_fin_succ, partialProd_succ, inv_mul_cancel_left, contractNth_apply_of_gt] · exact le_iff_val_le_val.2 (le_of_lt h) · rw [le_iff_val_le_val, val_succ] exact Nat.succ_le_of_lt h end PartialProd end Fin /-- Equivalence between `Fin n → Fin m` and `Fin (m ^ n)`. -/ @[simps!] def finFunctionFinEquiv {m n : ℕ} : (Fin n → Fin m) ≃ Fin (m ^ n) := Equiv.ofRightInverseOfCardLE (le_of_eq <| by simp_rw [Fintype.card_fun, Fintype.card_fin]) (fun f => ⟨∑ i, f i * m ^ (i : ℕ), by induction' n with n ih · simp cases m · exact isEmptyElim (f <| Fin.last _) simp_rw [Fin.sum_univ_castSucc, Fin.coe_castSucc, Fin.val_last] refine (Nat.add_lt_add_of_lt_of_le (ih _) <| Nat.mul_le_mul_right _ (Fin.is_le _)).trans_eq ?_ rw [← one_add_mul (_ : ℕ), add_comm, pow_succ']⟩) (fun a b => ⟨a / m ^ (b : ℕ) % m, by cases' n with n · exact b.elim0 cases' m with m · rw [zero_pow n.succ_ne_zero] at a exact a.elim0 · exact Nat.mod_lt _ m.succ_pos⟩) fun a => by dsimp induction' n with n ih · subsingleton [(finCongr <| pow_zero _).subsingleton] simp_rw [Fin.forall_iff, Fin.ext_iff] at ih ext simp_rw [Fin.sum_univ_succ, Fin.val_zero, Fin.val_succ, pow_zero, Nat.div_one, mul_one, pow_succ', ← Nat.div_div_eq_div_mul, mul_left_comm _ m, ← mul_sum] rw [ih _ (Nat.div_lt_of_lt_mul ?_), Nat.mod_add_div] -- Porting note: replaces `a.is_lt` in the wildcard above. Caused by a refactor of the `npow` -- instance for `Fin`. exact a.is_lt.trans_eq (pow_succ' _ _) theorem finFunctionFinEquiv_apply {m n : ℕ} (f : Fin n → Fin m) : (finFunctionFinEquiv f : ℕ) = ∑ i : Fin n, ↑(f i) * m ^ (i : ℕ) := rfl theorem finFunctionFinEquiv_single {m n : ℕ} [NeZero m] (i : Fin n) (j : Fin m) : (finFunctionFinEquiv (Pi.single i j) : ℕ) = j * m ^ (i : ℕ) := by rw [finFunctionFinEquiv_apply, Fintype.sum_eq_single i, Pi.single_eq_same] rintro x hx rw [Pi.single_eq_of_ne hx, Fin.val_zero', zero_mul] /-- Equivalence between `∀ i : Fin m, Fin (n i)` and `Fin (∏ i : Fin m, n i)`. -/ def finPiFinEquiv {m : ℕ} {n : Fin m → ℕ} : (∀ i : Fin m, Fin (n i)) ≃ Fin (∏ i : Fin m, n i) := Equiv.ofRightInverseOfCardLE (le_of_eq <| by simp_rw [Fintype.card_pi, Fintype.card_fin]) (fun f => ⟨∑ i, f i * ∏ j, n (Fin.castLE i.is_lt.le j), by induction' m with m ih · simp rw [Fin.prod_univ_castSucc, Fin.sum_univ_castSucc] suffices ∀ (n : Fin m → ℕ) (nn : ℕ) (f : ∀ i : Fin m, Fin (n i)) (fn : Fin nn), ((∑ i : Fin m, ↑(f i) * ∏ j : Fin i, n (Fin.castLE i.prop.le j)) + ↑fn * ∏ j, n j) < (∏ i : Fin m, n i) * nn by replace := this (Fin.init n) (n (Fin.last _)) (Fin.init f) (f (Fin.last _)) rw [← Fin.snoc_init_self f] simp (config := { singlePass := true }) only [← Fin.snoc_init_self n] simp_rw [Fin.snoc_castSucc, Fin.snoc_last, Fin.snoc_init_self n] exact this intro n nn f fn cases nn · exact isEmptyElim fn refine (Nat.add_lt_add_of_lt_of_le (ih _) <| Nat.mul_le_mul_right _ (Fin.is_le _)).trans_eq ?_ rw [← one_add_mul (_ : ℕ), mul_comm, add_comm]⟩) (fun a b => ⟨(a / ∏ j : Fin b, n (Fin.castLE b.is_lt.le j)) % n b, by cases m · exact b.elim0 cases' h : n b with nb · rw [prod_eq_zero (Finset.mem_univ _) h] at a exact isEmptyElim a exact Nat.mod_lt _ nb.succ_pos⟩) (by intro a; revert a; dsimp only [Fin.val_mk] refine Fin.consInduction ?_ ?_ n · intro a have : Subsingleton (Fin (∏ i : Fin 0, i.elim0)) := (finCongr <| prod_empty).subsingleton subsingleton · intro n x xs ih a simp_rw [Fin.forall_iff, Fin.ext_iff] at ih ext simp_rw [Fin.sum_univ_succ, Fin.cons_succ] have := fun i : Fin n => Fintype.prod_equiv (finCongr <| Fin.val_succ i) (fun j => (Fin.cons x xs : _ → ℕ) (Fin.castLE (Fin.is_lt _).le j)) (fun j => (Fin.cons x xs : _ → ℕ) (Fin.castLE (Nat.succ_le_succ (Fin.is_lt _).le) j)) fun j => rfl simp_rw [this] clear this dsimp only [Fin.val_zero] simp_rw [Fintype.prod_empty, Nat.div_one, mul_one, Fin.cons_zero, Fin.prod_univ_succ] change (_ + ∑ y : _, _ / (x * _) % _ * (x * _)) = _ simp_rw [← Nat.div_div_eq_div_mul, mul_left_comm (_ % _ : ℕ), ← mul_sum] convert Nat.mod_add_div _ _ -- Porting note: new refine (ih (a / x) (Nat.div_lt_of_lt_mul <| a.is_lt.trans_eq ?_)) exact Fin.prod_univ_succ _ -- Porting note: was: /- refine' Eq.trans _ (ih (a / x) (Nat.div_lt_of_lt_mul <| a.is_lt.trans_eq _)) swap · convert Fin.prod_univ_succ (Fin.cons x xs : _ → ℕ) simp_rw [Fin.cons_succ] congr with i congr with j · cases j rfl · cases j rfl-/) theorem finPiFinEquiv_apply {m : ℕ} {n : Fin m → ℕ} (f : ∀ i : Fin m, Fin (n i)) : (finPiFinEquiv f : ℕ) = ∑ i, f i * ∏ j, n (Fin.castLE i.is_lt.le j) := rfl theorem finPiFinEquiv_single {m : ℕ} {n : Fin m → ℕ} [∀ i, NeZero (n i)] (i : Fin m) (j : Fin (n i)) : (finPiFinEquiv (Pi.single i j : ∀ i : Fin m, Fin (n i)) : ℕ) = j * ∏ j, n (Fin.castLE i.is_lt.le j) := by rw [finPiFinEquiv_apply, Fintype.sum_eq_single i, Pi.single_eq_same] rintro x hx rw [Pi.single_eq_of_ne hx, Fin.val_zero', zero_mul] namespace List section CommMonoid variable [CommMonoid α] @[to_additive] theorem prod_take_ofFn {n : ℕ} (f : Fin n → α) (i : ℕ) : ((ofFn f).take i).prod = ∏ j ∈ Finset.univ.filter fun j : Fin n => j.val < i, f j := by induction i with | zero => simp | succ i IH => by_cases h : i < n · have : i < length (ofFn f) := by rwa [length_ofFn f] rw [prod_take_succ _ _ this] have A : ((Finset.univ : Finset (Fin n)).filter fun j => j.val < i + 1) = ((Finset.univ : Finset (Fin n)).filter fun j => j.val < i) ∪ {(⟨i, h⟩ : Fin n)} := by ext ⟨_, _⟩ simp [Nat.lt_succ_iff_lt_or_eq] have B : _root_.Disjoint (Finset.filter (fun j : Fin n => j.val < i) Finset.univ) (singleton (⟨i, h⟩ : Fin n)) := by simp rw [A, Finset.prod_union B, IH] simp · have A : (ofFn f).take i = (ofFn f).take i.succ := by rw [← length_ofFn f] at h have : length (ofFn f) ≤ i := not_lt.mp h rw [take_of_length_le this, take_of_length_le (le_trans this (Nat.le_succ _))] have B : ∀ j : Fin n, ((j : ℕ) < i.succ) = ((j : ℕ) < i) := by intro j have : (j : ℕ) < i := lt_of_lt_of_le j.2 (not_lt.mp h) simp [this, lt_trans this (Nat.lt_succ_self _)] simp [← A, B, IH] @[to_additive] theorem prod_ofFn {n : ℕ} {f : Fin n → α} : (ofFn f).prod = ∏ i, f i := Fin.prod_ofFn f end CommMonoid @[to_additive] theorem alternatingProd_eq_finset_prod {G : Type*} [CommGroup G] : ∀ (L : List G), alternatingProd L = ∏ i : Fin L.length, L.get i ^ (-1 : ℤ) ^ (i : ℕ) | [] => by rw [alternatingProd, Finset.prod_eq_one] rintro ⟨i, ⟨⟩⟩ | g::[] => by show g = ∏ i : Fin 1, [g].get i ^ (-1 : ℤ) ^ (i : ℕ) rw [Fin.prod_univ_succ]; simp | g::h::L => calc g * h⁻¹ * L.alternatingProd = g * h⁻¹ * ∏ i : Fin L.length, L.get i ^ (-1 : ℤ) ^ (i : ℕ) := congr_arg _ (alternatingProd_eq_finset_prod _) _ = ∏ i : Fin (L.length + 2), List.get (g::h::L) i ^ (-1 : ℤ) ^ (i : ℕ) := by { rw [Fin.prod_univ_succ, Fin.prod_univ_succ, mul_assoc] simp [pow_add]} end List
Algebra\BigOperators\Finprod.lean
/- Copyright (c) 2020 Kexing Ying and Kevin Buzzard. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kexing Ying, Kevin Buzzard, Yury Kudryashov -/ import Mathlib.Algebra.BigOperators.GroupWithZero.Finset import Mathlib.Algebra.Group.FiniteSupport import Mathlib.Algebra.Module.Defs import Mathlib.Algebra.Order.BigOperators.Group.Finset import Mathlib.Data.Set.Subsingleton /-! # Finite products and sums over types and sets We define products and sums over types and subsets of types, with no finiteness hypotheses. All infinite products and sums are defined to be junk values (i.e. one or zero). This approach is sometimes easier to use than `Finset.sum`, when issues arise with `Finset` and `Fintype` being data. ## Main definitions We use the following variables: * `α`, `β` - types with no structure; * `s`, `t` - sets * `M`, `N` - additive or multiplicative commutative monoids * `f`, `g` - functions Definitions in this file: * `finsum f : M` : the sum of `f x` as `x` ranges over the support of `f`, if it's finite. Zero otherwise. * `finprod f : M` : the product of `f x` as `x` ranges over the multiplicative support of `f`, if it's finite. One otherwise. ## Notation * `∑ᶠ i, f i` and `∑ᶠ i : α, f i` for `finsum f` * `∏ᶠ i, f i` and `∏ᶠ i : α, f i` for `finprod f` This notation works for functions `f : p → M`, where `p : Prop`, so the following works: * `∑ᶠ i ∈ s, f i`, where `f : α → M`, `s : Set α` : sum over the set `s`; * `∑ᶠ n < 5, f n`, where `f : ℕ → M` : same as `f 0 + f 1 + f 2 + f 3 + f 4`; * `∏ᶠ (n >= -2) (hn : n < 3), f n`, where `f : ℤ → M` : same as `f (-2) * f (-1) * f 0 * f 1 * f 2`. ## Implementation notes `finsum` and `finprod` is "yet another way of doing finite sums and products in Lean". However experiments in the wild (e.g. with matroids) indicate that it is a helpful approach in settings where the user is not interested in computability and wants to do reasoning without running into typeclass diamonds caused by the constructive finiteness used in definitions such as `Finset` and `Fintype`. By sticking solely to `Set.Finite` we avoid these problems. We are aware that there are other solutions but for beginner mathematicians this approach is easier in practice. Another application is the construction of a partition of unity from a collection of “bump” function. In this case the finite set depends on the point and it's convenient to have a definition that does not mention the set explicitly. The first arguments in all definitions and lemmas is the codomain of the function of the big operator. This is necessary for the heuristic in `@[to_additive]`. See the documentation of `to_additive.attr` for more information. We did not add `IsFinite (X : Type) : Prop`, because it is simply `Nonempty (Fintype X)`. ## Tags finsum, finprod, finite sum, finite product -/ open Function Set /-! ### Definition and relation to `Finset.sum` and `Finset.prod` -/ -- Porting note: Used to be section Sort section sort variable {G M N : Type*} {α β ι : Sort*} [CommMonoid M] [CommMonoid N] section /- Note: we use classical logic only for these definitions, to ensure that we do not write lemmas with `Classical.dec` in their statement. -/ open Classical in /-- Sum of `f x` as `x` ranges over the elements of the support of `f`, if it's finite. Zero otherwise. -/ noncomputable irreducible_def finsum (lemma := finsum_def') [AddCommMonoid M] (f : α → M) : M := if h : (support (f ∘ PLift.down)).Finite then ∑ i ∈ h.toFinset, f i.down else 0 open Classical in /-- Product of `f x` as `x` ranges over the elements of the multiplicative support of `f`, if it's finite. One otherwise. -/ @[to_additive existing] noncomputable irreducible_def finprod (lemma := finprod_def') (f : α → M) : M := if h : (mulSupport (f ∘ PLift.down)).Finite then ∏ i ∈ h.toFinset, f i.down else 1 attribute [to_additive existing] finprod_def' end open Batteries.ExtendedBinder /-- `∑ᶠ x, f x` is notation for `finsum f`. It is the sum of `f x`, where `x` ranges over the support of `f`, if it's finite, zero otherwise. Taking the sum over multiple arguments or conditions is possible, e.g. `∏ᶠ (x) (y), f x y` and `∏ᶠ (x) (h: x ∈ s), f x`-/ notation3"∑ᶠ "(...)", "r:67:(scoped f => finsum f) => r /-- `∏ᶠ x, f x` is notation for `finprod f`. It is the product of `f x`, where `x` ranges over the multiplicative support of `f`, if it's finite, one otherwise. Taking the product over multiple arguments or conditions is possible, e.g. `∏ᶠ (x) (y), f x y` and `∏ᶠ (x) (h: x ∈ s), f x`-/ notation3"∏ᶠ "(...)", "r:67:(scoped f => finprod f) => r -- Porting note: The following ports the lean3 notation for this file, but is currently very fickle. -- syntax (name := bigfinsum) "∑ᶠ" extBinders ", " term:67 : term -- macro_rules (kind := bigfinsum) -- | `(∑ᶠ $x:ident, $p) => `(finsum (fun $x:ident ↦ $p)) -- | `(∑ᶠ $x:ident : $t, $p) => `(finsum (fun $x:ident : $t ↦ $p)) -- | `(∑ᶠ $x:ident $b:binderPred, $p) => -- `(finsum fun $x => (finsum (α := satisfies_binder_pred% $x $b) (fun _ => $p))) -- | `(∑ᶠ ($x:ident) ($h:ident : $t), $p) => -- `(finsum fun ($x) => finsum (α := $t) (fun $h => $p)) -- | `(∑ᶠ ($x:ident : $_) ($h:ident : $t), $p) => -- `(finsum fun ($x) => finsum (α := $t) (fun $h => $p)) -- | `(∑ᶠ ($x:ident) ($y:ident), $p) => -- `(finsum fun $x => (finsum fun $y => $p)) -- | `(∑ᶠ ($x:ident) ($y:ident) ($h:ident : $t), $p) => -- `(finsum fun $x => (finsum fun $y => (finsum (α := $t) fun $h => $p))) -- | `(∑ᶠ ($x:ident) ($y:ident) ($z:ident), $p) => -- `(finsum fun $x => (finsum fun $y => (finsum fun $z => $p))) -- | `(∑ᶠ ($x:ident) ($y:ident) ($z:ident) ($h:ident : $t), $p) => -- `(finsum fun $x => (finsum fun $y => (finsum fun $z => (finsum (α := $t) fun $h => $p)))) -- -- -- syntax (name := bigfinprod) "∏ᶠ " extBinders ", " term:67 : term -- macro_rules (kind := bigfinprod) -- | `(∏ᶠ $x:ident, $p) => `(finprod (fun $x:ident ↦ $p)) -- | `(∏ᶠ $x:ident : $t, $p) => `(finprod (fun $x:ident : $t ↦ $p)) -- | `(∏ᶠ $x:ident $b:binderPred, $p) => -- `(finprod fun $x => (finprod (α := satisfies_binder_pred% $x $b) (fun _ => $p))) -- | `(∏ᶠ ($x:ident) ($h:ident : $t), $p) => -- `(finprod fun ($x) => finprod (α := $t) (fun $h => $p)) -- | `(∏ᶠ ($x:ident : $_) ($h:ident : $t), $p) => -- `(finprod fun ($x) => finprod (α := $t) (fun $h => $p)) -- | `(∏ᶠ ($x:ident) ($y:ident), $p) => -- `(finprod fun $x => (finprod fun $y => $p)) -- | `(∏ᶠ ($x:ident) ($y:ident) ($h:ident : $t), $p) => -- `(finprod fun $x => (finprod fun $y => (finprod (α := $t) fun $h => $p))) -- | `(∏ᶠ ($x:ident) ($y:ident) ($z:ident), $p) => -- `(finprod fun $x => (finprod fun $y => (finprod fun $z => $p))) -- | `(∏ᶠ ($x:ident) ($y:ident) ($z:ident) ($h:ident : $t), $p) => -- `(finprod fun $x => (finprod fun $y => (finprod fun $z => -- (finprod (α := $t) fun $h => $p)))) @[to_additive] theorem finprod_eq_prod_plift_of_mulSupport_toFinset_subset {f : α → M} (hf : (mulSupport (f ∘ PLift.down)).Finite) {s : Finset (PLift α)} (hs : hf.toFinset ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i.down := by rw [finprod, dif_pos] refine Finset.prod_subset hs fun x _ hxf => ?_ rwa [hf.mem_toFinset, nmem_mulSupport] at hxf @[to_additive] theorem finprod_eq_prod_plift_of_mulSupport_subset {f : α → M} {s : Finset (PLift α)} (hs : mulSupport (f ∘ PLift.down) ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i.down := finprod_eq_prod_plift_of_mulSupport_toFinset_subset (s.finite_toSet.subset hs) fun x hx => by rw [Finite.mem_toFinset] at hx exact hs hx @[to_additive (attr := simp)] theorem finprod_one : (∏ᶠ _ : α, (1 : M)) = 1 := by have : (mulSupport fun x : PLift α => (fun _ => 1 : α → M) x.down) ⊆ (∅ : Finset (PLift α)) := fun x h => by simp at h rw [finprod_eq_prod_plift_of_mulSupport_subset this, Finset.prod_empty] @[to_additive] theorem finprod_of_isEmpty [IsEmpty α] (f : α → M) : ∏ᶠ i, f i = 1 := by rw [← finprod_one] congr simp [eq_iff_true_of_subsingleton] @[to_additive (attr := simp)] theorem finprod_false (f : False → M) : ∏ᶠ i, f i = 1 := finprod_of_isEmpty _ @[to_additive] theorem finprod_eq_single (f : α → M) (a : α) (ha : ∀ x, x ≠ a → f x = 1) : ∏ᶠ x, f x = f a := by have : mulSupport (f ∘ PLift.down) ⊆ ({PLift.up a} : Finset (PLift α)) := by intro x contrapose simpa [PLift.eq_up_iff_down_eq] using ha x.down rw [finprod_eq_prod_plift_of_mulSupport_subset this, Finset.prod_singleton] @[to_additive] theorem finprod_unique [Unique α] (f : α → M) : ∏ᶠ i, f i = f default := finprod_eq_single f default fun _x hx => (hx <| Unique.eq_default _).elim @[to_additive (attr := simp)] theorem finprod_true (f : True → M) : ∏ᶠ i, f i = f trivial := @finprod_unique M True _ ⟨⟨trivial⟩, fun _ => rfl⟩ f @[to_additive] theorem finprod_eq_dif {p : Prop} [Decidable p] (f : p → M) : ∏ᶠ i, f i = if h : p then f h else 1 := by split_ifs with h · haveI : Unique p := ⟨⟨h⟩, fun _ => rfl⟩ exact finprod_unique f · haveI : IsEmpty p := ⟨h⟩ exact finprod_of_isEmpty f @[to_additive] theorem finprod_eq_if {p : Prop} [Decidable p] {x : M} : ∏ᶠ _ : p, x = if p then x else 1 := finprod_eq_dif fun _ => x @[to_additive] theorem finprod_congr {f g : α → M} (h : ∀ x, f x = g x) : finprod f = finprod g := congr_arg _ <| funext h @[to_additive (attr := congr)] theorem finprod_congr_Prop {p q : Prop} {f : p → M} {g : q → M} (hpq : p = q) (hfg : ∀ h : q, f (hpq.mpr h) = g h) : finprod f = finprod g := by subst q exact finprod_congr hfg /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on the factors. -/ @[to_additive "To prove a property of a finite sum, it suffices to prove that the property is additive and holds on the summands."] theorem finprod_induction {f : α → M} (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ i, p (f i)) : p (∏ᶠ i, f i) := by rw [finprod] split_ifs exacts [Finset.prod_induction _ _ hp₁ hp₀ fun i _ => hp₂ _, hp₀] theorem finprod_nonneg {R : Type*} [OrderedCommSemiring R] {f : α → R} (hf : ∀ x, 0 ≤ f x) : 0 ≤ ∏ᶠ x, f x := finprod_induction (fun x => 0 ≤ x) zero_le_one (fun _ _ => mul_nonneg) hf @[to_additive finsum_nonneg] theorem one_le_finprod' {M : Type*} [OrderedCommMonoid M] {f : α → M} (hf : ∀ i, 1 ≤ f i) : 1 ≤ ∏ᶠ i, f i := finprod_induction _ le_rfl (fun _ _ => one_le_mul) hf @[to_additive] theorem MonoidHom.map_finprod_plift (f : M →* N) (g : α → M) (h : (mulSupport <| g ∘ PLift.down).Finite) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := by rw [finprod_eq_prod_plift_of_mulSupport_subset h.coe_toFinset.ge, finprod_eq_prod_plift_of_mulSupport_subset, map_prod] rw [h.coe_toFinset] exact mulSupport_comp_subset f.map_one (g ∘ PLift.down) @[to_additive] theorem MonoidHom.map_finprod_Prop {p : Prop} (f : M →* N) (g : p → M) : f (∏ᶠ x, g x) = ∏ᶠ x, f (g x) := f.map_finprod_plift g (Set.toFinite _) @[to_additive] theorem MonoidHom.map_finprod_of_preimage_one (f : M →* N) (hf : ∀ x, f x = 1 → x = 1) (g : α → M) : f (∏ᶠ i, g i) = ∏ᶠ i, f (g i) := by by_cases hg : (mulSupport <| g ∘ PLift.down).Finite; · exact f.map_finprod_plift g hg rw [finprod, dif_neg, f.map_one, finprod, dif_neg] exacts [Infinite.mono (fun x hx => mt (hf (g x.down)) hx) hg, hg] @[to_additive] theorem MonoidHom.map_finprod_of_injective (g : M →* N) (hg : Injective g) (f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.map_finprod_of_preimage_one (fun _ => (hg.eq_iff' g.map_one).mp) f @[to_additive] theorem MulEquiv.map_finprod (g : M ≃* N) (f : α → M) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.toMonoidHom.map_finprod_of_injective (EquivLike.injective g) f /-- The `NoZeroSMulDivisors` makes sure that the result holds even when the support of `f` is infinite. For a more usual version assuming `(support f).Finite` instead, see `finsum_smul'`. -/ theorem finsum_smul {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M] (f : ι → R) (x : M) : (∑ᶠ i, f i) • x = ∑ᶠ i, f i • x := by rcases eq_or_ne x 0 with (rfl | hx) · simp · exact ((smulAddHom R M).flip x).map_finsum_of_injective (smul_left_injective R hx) _ /-- The `NoZeroSMulDivisors` makes sure that the result holds even when the support of `f` is infinite. For a more usual version assuming `(support f).Finite` instead, see `smul_finsum'`. -/ theorem smul_finsum {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] [NoZeroSMulDivisors R M] (c : R) (f : ι → M) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i := by rcases eq_or_ne c 0 with (rfl | hc) · simp · exact (smulAddHom R M c).map_finsum_of_injective (smul_right_injective M hc) _ @[to_additive] theorem finprod_inv_distrib [DivisionCommMonoid G] (f : α → G) : (∏ᶠ x, (f x)⁻¹) = (∏ᶠ x, f x)⁻¹ := ((MulEquiv.inv G).map_finprod f).symm end sort -- Porting note: Used to be section Type section type variable {α β ι G M N : Type*} [CommMonoid M] [CommMonoid N] @[to_additive] theorem finprod_eq_mulIndicator_apply (s : Set α) (f : α → M) (a : α) : ∏ᶠ _ : a ∈ s, f a = mulIndicator s f a := by classical convert finprod_eq_if (M := M) (p := a ∈ s) (x := f a) @[to_additive (attr := simp)] theorem finprod_mem_mulSupport (f : α → M) (a : α) : ∏ᶠ _ : f a ≠ 1, f a = f a := by rw [← mem_mulSupport, finprod_eq_mulIndicator_apply, mulIndicator_mulSupport] @[to_additive] theorem finprod_mem_def (s : Set α) (f : α → M) : ∏ᶠ a ∈ s, f a = ∏ᶠ a, mulIndicator s f a := finprod_congr <| finprod_eq_mulIndicator_apply s f @[to_additive] theorem finprod_eq_prod_of_mulSupport_subset (f : α → M) {s : Finset α} (h : mulSupport f ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i := by have A : mulSupport (f ∘ PLift.down) = Equiv.plift.symm '' mulSupport f := by rw [mulSupport_comp_eq_preimage] exact (Equiv.plift.symm.image_eq_preimage _).symm have : mulSupport (f ∘ PLift.down) ⊆ s.map Equiv.plift.symm.toEmbedding := by rw [A, Finset.coe_map] exact image_subset _ h rw [finprod_eq_prod_plift_of_mulSupport_subset this] simp only [Finset.prod_map, Equiv.coe_toEmbedding] congr @[to_additive] theorem finprod_eq_prod_of_mulSupport_toFinset_subset (f : α → M) (hf : (mulSupport f).Finite) {s : Finset α} (h : hf.toFinset ⊆ s) : ∏ᶠ i, f i = ∏ i ∈ s, f i := finprod_eq_prod_of_mulSupport_subset _ fun _ hx => h <| hf.mem_toFinset.2 hx @[to_additive] theorem finprod_eq_finset_prod_of_mulSupport_subset (f : α → M) {s : Finset α} (h : mulSupport f ⊆ (s : Set α)) : ∏ᶠ i, f i = ∏ i ∈ s, f i := haveI h' : (s.finite_toSet.subset h).toFinset ⊆ s := by simpa [← Finset.coe_subset, Set.coe_toFinset] finprod_eq_prod_of_mulSupport_toFinset_subset _ _ h' @[to_additive] theorem finprod_def (f : α → M) [Decidable (mulSupport f).Finite] : ∏ᶠ i : α, f i = if h : (mulSupport f).Finite then ∏ i ∈ h.toFinset, f i else 1 := by split_ifs with h · exact finprod_eq_prod_of_mulSupport_toFinset_subset _ h (Finset.Subset.refl _) · rw [finprod, dif_neg] rw [mulSupport_comp_eq_preimage] exact mt (fun hf => hf.of_preimage Equiv.plift.surjective) h @[to_additive] theorem finprod_of_infinite_mulSupport {f : α → M} (hf : (mulSupport f).Infinite) : ∏ᶠ i, f i = 1 := by classical rw [finprod_def, dif_neg hf] @[to_additive] theorem finprod_eq_prod (f : α → M) (hf : (mulSupport f).Finite) : ∏ᶠ i : α, f i = ∏ i ∈ hf.toFinset, f i := by classical rw [finprod_def, dif_pos hf] @[to_additive] theorem finprod_eq_prod_of_fintype [Fintype α] (f : α → M) : ∏ᶠ i : α, f i = ∏ i, f i := finprod_eq_prod_of_mulSupport_toFinset_subset _ (Set.toFinite _) <| Finset.subset_univ _ @[to_additive] theorem finprod_cond_eq_prod_of_cond_iff (f : α → M) {p : α → Prop} {t : Finset α} (h : ∀ {x}, f x ≠ 1 → (p x ↔ x ∈ t)) : (∏ᶠ (i) (_ : p i), f i) = ∏ i ∈ t, f i := by set s := { x | p x } have : mulSupport (s.mulIndicator f) ⊆ t := by rw [Set.mulSupport_mulIndicator] intro x hx exact (h hx.2).1 hx.1 erw [finprod_mem_def, finprod_eq_prod_of_mulSupport_subset _ this] refine Finset.prod_congr rfl fun x hx => mulIndicator_apply_eq_self.2 fun hxs => ?_ contrapose! hxs exact (h hxs).2 hx @[to_additive] theorem finprod_cond_ne (f : α → M) (a : α) [DecidableEq α] (hf : (mulSupport f).Finite) : (∏ᶠ (i) (_ : i ≠ a), f i) = ∏ i ∈ hf.toFinset.erase a, f i := by apply finprod_cond_eq_prod_of_cond_iff intro x hx rw [Finset.mem_erase, Finite.mem_toFinset, mem_mulSupport] exact ⟨fun h => And.intro h hx, fun h => h.1⟩ @[to_additive] theorem finprod_mem_eq_prod_of_inter_mulSupport_eq (f : α → M) {s : Set α} {t : Finset α} (h : s ∩ mulSupport f = t.toSet ∩ mulSupport f) : ∏ᶠ i ∈ s, f i = ∏ i ∈ t, f i := finprod_cond_eq_prod_of_cond_iff _ <| by intro x hxf rw [← mem_mulSupport] at hxf refine ⟨fun hx => ?_, fun hx => ?_⟩ · refine ((mem_inter_iff x t (mulSupport f)).mp ?_).1 rw [← Set.ext_iff.mp h x, mem_inter_iff] exact ⟨hx, hxf⟩ · refine ((mem_inter_iff x s (mulSupport f)).mp ?_).1 rw [Set.ext_iff.mp h x, mem_inter_iff] exact ⟨hx, hxf⟩ @[to_additive] theorem finprod_mem_eq_prod_of_subset (f : α → M) {s : Set α} {t : Finset α} (h₁ : s ∩ mulSupport f ⊆ t) (h₂ : ↑t ⊆ s) : ∏ᶠ i ∈ s, f i = ∏ i ∈ t, f i := finprod_cond_eq_prod_of_cond_iff _ fun hx => ⟨fun h => h₁ ⟨h, hx⟩, fun h => h₂ h⟩ @[to_additive] theorem finprod_mem_eq_prod (f : α → M) {s : Set α} (hf : (s ∩ mulSupport f).Finite) : ∏ᶠ i ∈ s, f i = ∏ i ∈ hf.toFinset, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by simp [inter_assoc] @[to_additive] theorem finprod_mem_eq_prod_filter (f : α → M) (s : Set α) [DecidablePred (· ∈ s)] (hf : (mulSupport f).Finite) : ∏ᶠ i ∈ s, f i = ∏ i ∈ Finset.filter (· ∈ s) hf.toFinset, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by ext x simp [and_comm] @[to_additive] theorem finprod_mem_eq_toFinset_prod (f : α → M) (s : Set α) [Fintype s] : ∏ᶠ i ∈ s, f i = ∏ i ∈ s.toFinset, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by simp_rw [coe_toFinset s] @[to_additive] theorem finprod_mem_eq_finite_toFinset_prod (f : α → M) {s : Set α} (hs : s.Finite) : ∏ᶠ i ∈ s, f i = ∏ i ∈ hs.toFinset, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ <| by rw [hs.coe_toFinset] @[to_additive] theorem finprod_mem_finset_eq_prod (f : α → M) (s : Finset α) : ∏ᶠ i ∈ s, f i = ∏ i ∈ s, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ rfl @[to_additive] theorem finprod_mem_coe_finset (f : α → M) (s : Finset α) : (∏ᶠ i ∈ (s : Set α), f i) = ∏ i ∈ s, f i := finprod_mem_eq_prod_of_inter_mulSupport_eq _ rfl @[to_additive] theorem finprod_mem_eq_one_of_infinite {f : α → M} {s : Set α} (hs : (s ∩ mulSupport f).Infinite) : ∏ᶠ i ∈ s, f i = 1 := by rw [finprod_mem_def] apply finprod_of_infinite_mulSupport rwa [← mulSupport_mulIndicator] at hs @[to_additive] theorem finprod_mem_eq_one_of_forall_eq_one {f : α → M} {s : Set α} (h : ∀ x ∈ s, f x = 1) : ∏ᶠ i ∈ s, f i = 1 := by simp (config := { contextual := true }) [h] @[to_additive] theorem finprod_mem_inter_mulSupport (f : α → M) (s : Set α) : ∏ᶠ i ∈ s ∩ mulSupport f, f i = ∏ᶠ i ∈ s, f i := by rw [finprod_mem_def, finprod_mem_def, mulIndicator_inter_mulSupport] @[to_additive] theorem finprod_mem_inter_mulSupport_eq (f : α → M) (s t : Set α) (h : s ∩ mulSupport f = t ∩ mulSupport f) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mulSupport, h, finprod_mem_inter_mulSupport] @[to_additive] theorem finprod_mem_inter_mulSupport_eq' (f : α → M) (s t : Set α) (h : ∀ x ∈ mulSupport f, x ∈ s ↔ x ∈ t) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, f i := by apply finprod_mem_inter_mulSupport_eq ext x exact and_congr_left (h x) @[to_additive] theorem finprod_mem_univ (f : α → M) : ∏ᶠ i ∈ @Set.univ α, f i = ∏ᶠ i : α, f i := finprod_congr fun _ => finprod_true _ variable {f g : α → M} {a b : α} {s t : Set α} @[to_additive] theorem finprod_mem_congr (h₀ : s = t) (h₁ : ∀ x ∈ t, f x = g x) : ∏ᶠ i ∈ s, f i = ∏ᶠ i ∈ t, g i := h₀.symm ▸ finprod_congr fun i => finprod_congr_Prop rfl (h₁ i) @[to_additive] theorem finprod_eq_one_of_forall_eq_one {f : α → M} (h : ∀ x, f x = 1) : ∏ᶠ i, f i = 1 := by simp (config := { contextual := true }) [h] @[to_additive finsum_pos'] theorem one_lt_finprod' {M : Type*} [OrderedCancelCommMonoid M] {f : ι → M} (h : ∀ i, 1 ≤ f i) (h' : ∃ i, 1 < f i) (hf : (mulSupport f).Finite) : 1 < ∏ᶠ i, f i := by rcases h' with ⟨i, hi⟩ rw [finprod_eq_prod _ hf] refine Finset.one_lt_prod' (fun i _ ↦ h i) ⟨i, ?_, hi⟩ simpa only [Finite.mem_toFinset, mem_mulSupport] using ne_of_gt hi /-! ### Distributivity w.r.t. addition, subtraction, and (scalar) multiplication -/ /-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i * g i` equals the product of `f i` multiplied by the product of `g i`. -/ @[to_additive "If the additive supports of `f` and `g` are finite, then the sum of `f i + g i` equals the sum of `f i` plus the sum of `g i`."] theorem finprod_mul_distrib (hf : (mulSupport f).Finite) (hg : (mulSupport g).Finite) : ∏ᶠ i, f i * g i = (∏ᶠ i, f i) * ∏ᶠ i, g i := by classical rw [finprod_eq_prod_of_mulSupport_toFinset_subset f hf Finset.subset_union_left, finprod_eq_prod_of_mulSupport_toFinset_subset g hg Finset.subset_union_right, ← Finset.prod_mul_distrib] refine finprod_eq_prod_of_mulSupport_subset _ ?_ simp only [Finset.coe_union, Finite.coe_toFinset, mulSupport_subset_iff, mem_union, mem_mulSupport] intro x contrapose! rintro ⟨hf, hg⟩ simp [hf, hg] /-- If the multiplicative supports of `f` and `g` are finite, then the product of `f i / g i` equals the product of `f i` divided by the product of `g i`. -/ @[to_additive "If the additive supports of `f` and `g` are finite, then the sum of `f i - g i` equals the sum of `f i` minus the sum of `g i`."] theorem finprod_div_distrib [DivisionCommMonoid G] {f g : α → G} (hf : (mulSupport f).Finite) (hg : (mulSupport g).Finite) : ∏ᶠ i, f i / g i = (∏ᶠ i, f i) / ∏ᶠ i, g i := by simp only [div_eq_mul_inv, finprod_mul_distrib hf ((mulSupport_inv g).symm.rec hg), finprod_inv_distrib] /-- A more general version of `finprod_mem_mul_distrib` that only requires `s ∩ mulSupport f` and `s ∩ mulSupport g` rather than `s` to be finite. -/ @[to_additive "A more general version of `finsum_mem_add_distrib` that only requires `s ∩ support f` and `s ∩ support g` rather than `s` to be finite."] theorem finprod_mem_mul_distrib' (hf : (s ∩ mulSupport f).Finite) (hg : (s ∩ mulSupport g).Finite) : ∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := by rw [← mulSupport_mulIndicator] at hf hg simp only [finprod_mem_def, mulIndicator_mul, finprod_mul_distrib hf hg] /-- The product of the constant function `1` over any set equals `1`. -/ @[to_additive "The sum of the constant function `0` over any set equals `0`."] theorem finprod_mem_one (s : Set α) : (∏ᶠ i ∈ s, (1 : M)) = 1 := by simp /-- If a function `f` equals `1` on a set `s`, then the product of `f i` over `i ∈ s` equals `1`. -/ @[to_additive "If a function `f` equals `0` on a set `s`, then the product of `f i` over `i ∈ s` equals `0`."] theorem finprod_mem_of_eqOn_one (hf : s.EqOn f 1) : ∏ᶠ i ∈ s, f i = 1 := by rw [← finprod_mem_one s] exact finprod_mem_congr rfl hf /-- If the product of `f i` over `i ∈ s` is not equal to `1`, then there is some `x ∈ s` such that `f x ≠ 1`. -/ @[to_additive "If the product of `f i` over `i ∈ s` is not equal to `0`, then there is some `x ∈ s` such that `f x ≠ 0`."] theorem exists_ne_one_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : ∃ x ∈ s, f x ≠ 1 := by by_contra! h' exact h (finprod_mem_of_eqOn_one h') /-- Given a finite set `s`, the product of `f i * g i` over `i ∈ s` equals the product of `f i` over `i ∈ s` times the product of `g i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s`, the sum of `f i + g i` over `i ∈ s` equals the sum of `f i` over `i ∈ s` plus the sum of `g i` over `i ∈ s`."] theorem finprod_mem_mul_distrib (hs : s.Finite) : ∏ᶠ i ∈ s, f i * g i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ s, g i := finprod_mem_mul_distrib' (hs.inter_of_left _) (hs.inter_of_left _) @[to_additive] theorem MonoidHom.map_finprod {f : α → M} (g : M →* N) (hf : (mulSupport f).Finite) : g (∏ᶠ i, f i) = ∏ᶠ i, g (f i) := g.map_finprod_plift f <| hf.preimage Equiv.plift.injective.injOn @[to_additive] theorem finprod_pow (hf : (mulSupport f).Finite) (n : ℕ) : (∏ᶠ i, f i) ^ n = ∏ᶠ i, f i ^ n := (powMonoidHom n).map_finprod hf /-- See also `finsum_smul` for a version that works even when the support of `f` is not finite, but with slightly stronger typeclass requirements. -/ theorem finsum_smul' {R M : Type*} [Semiring R] [AddCommMonoid M] [Module R M] {f : ι → R} (hf : (support f).Finite) (x : M) : (∑ᶠ i, f i) • x = ∑ᶠ i, f i • x := ((smulAddHom R M).flip x).map_finsum hf /-- See also `smul_finsum` for a version that works even when the support of `f` is not finite, but with slightly stronger typeclass requirements. -/ theorem smul_finsum' {R M : Type*} [Semiring R] [AddCommMonoid M] [Module R M] (c : R) {f : ι → M} (hf : (support f).Finite) : (c • ∑ᶠ i, f i) = ∑ᶠ i, c • f i := (smulAddHom R M c).map_finsum hf /-- A more general version of `MonoidHom.map_finprod_mem` that requires `s ∩ mulSupport f` rather than `s` to be finite. -/ @[to_additive "A more general version of `AddMonoidHom.map_finsum_mem` that requires `s ∩ support f` rather than `s` to be finite."] theorem MonoidHom.map_finprod_mem' {f : α → M} (g : M →* N) (h₀ : (s ∩ mulSupport f).Finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := by rw [g.map_finprod] · simp only [g.map_finprod_Prop] · simpa only [finprod_eq_mulIndicator_apply, mulSupport_mulIndicator] /-- Given a monoid homomorphism `g : M →* N` and a function `f : α → M`, the value of `g` at the product of `f i` over `i ∈ s` equals the product of `g (f i)` over `s`. -/ @[to_additive "Given an additive monoid homomorphism `g : M →* N` and a function `f : α → M`, the value of `g` at the sum of `f i` over `i ∈ s` equals the sum of `g (f i)` over `s`."] theorem MonoidHom.map_finprod_mem (f : α → M) (g : M →* N) (hs : s.Finite) : g (∏ᶠ j ∈ s, f j) = ∏ᶠ i ∈ s, g (f i) := g.map_finprod_mem' (hs.inter_of_left _) @[to_additive] theorem MulEquiv.map_finprod_mem (g : M ≃* N) (f : α → M) {s : Set α} (hs : s.Finite) : g (∏ᶠ i ∈ s, f i) = ∏ᶠ i ∈ s, g (f i) := g.toMonoidHom.map_finprod_mem f hs @[to_additive] theorem finprod_mem_inv_distrib [DivisionCommMonoid G] (f : α → G) (hs : s.Finite) : (∏ᶠ x ∈ s, (f x)⁻¹) = (∏ᶠ x ∈ s, f x)⁻¹ := ((MulEquiv.inv G).map_finprod_mem f hs).symm /-- Given a finite set `s`, the product of `f i / g i` over `i ∈ s` equals the product of `f i` over `i ∈ s` divided by the product of `g i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s`, the sum of `f i / g i` over `i ∈ s` equals the sum of `f i` over `i ∈ s` minus the sum of `g i` over `i ∈ s`."] theorem finprod_mem_div_distrib [DivisionCommMonoid G] (f g : α → G) (hs : s.Finite) : ∏ᶠ i ∈ s, f i / g i = (∏ᶠ i ∈ s, f i) / ∏ᶠ i ∈ s, g i := by simp only [div_eq_mul_inv, finprod_mem_mul_distrib hs, finprod_mem_inv_distrib g hs] /-! ### `∏ᶠ x ∈ s, f x` and set operations -/ /-- The product of any function over an empty set is `1`. -/ @[to_additive "The sum of any function over an empty set is `0`."] theorem finprod_mem_empty : (∏ᶠ i ∈ (∅ : Set α), f i) = 1 := by simp /-- A set `s` is nonempty if the product of some function over `s` is not equal to `1`. -/ @[to_additive "A set `s` is nonempty if the sum of some function over `s` is not equal to `0`."] theorem nonempty_of_finprod_mem_ne_one (h : ∏ᶠ i ∈ s, f i ≠ 1) : s.Nonempty := nonempty_iff_ne_empty.2 fun h' => h <| h'.symm ▸ finprod_mem_empty /-- Given finite sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` times the product of `f i` over `i ∈ s ∩ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive "Given finite sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` plus the sum of `f i` over `i ∈ s ∩ t` equals the sum of `f i` over `i ∈ s` plus the sum of `f i` over `i ∈ t`."] theorem finprod_mem_union_inter (hs : s.Finite) (ht : t.Finite) : ((∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by lift s to Finset α using hs; lift t to Finset α using ht classical rw [← Finset.coe_union, ← Finset.coe_inter] simp only [finprod_mem_coe_finset, Finset.prod_union_inter] /-- A more general version of `finprod_mem_union_inter` that requires `s ∩ mulSupport f` and `t ∩ mulSupport f` rather than `s` and `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_union_inter` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be finite."] theorem finprod_mem_union_inter' (hs : (s ∩ mulSupport f).Finite) (ht : (t ∩ mulSupport f).Finite) : ((∏ᶠ i ∈ s ∪ t, f i) * ∏ᶠ i ∈ s ∩ t, f i) = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mulSupport f s, ← finprod_mem_inter_mulSupport f t, ← finprod_mem_union_inter hs ht, ← union_inter_distrib_right, finprod_mem_inter_mulSupport, ← finprod_mem_inter_mulSupport f (s ∩ t)] congr 2 rw [inter_left_comm, inter_assoc, inter_assoc, inter_self, inter_left_comm] /-- A more general version of `finprod_mem_union` that requires `s ∩ mulSupport f` and `t ∩ mulSupport f` rather than `s` and `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_union` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be finite."] theorem finprod_mem_union' (hst : Disjoint s t) (hs : (s ∩ mulSupport f).Finite) (ht : (t ∩ mulSupport f).Finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_union_inter' hs ht, disjoint_iff_inter_eq_empty.1 hst, finprod_mem_empty, mul_one] /-- Given two finite disjoint sets `s` and `t`, the product of `f i` over `i ∈ s ∪ t` equals the product of `f i` over `i ∈ s` times the product of `f i` over `i ∈ t`. -/ @[to_additive "Given two finite disjoint sets `s` and `t`, the sum of `f i` over `i ∈ s ∪ t` equals the sum of `f i` over `i ∈ s` plus the sum of `f i` over `i ∈ t`."] theorem finprod_mem_union (hst : Disjoint s t) (hs : s.Finite) (ht : t.Finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := finprod_mem_union' hst (hs.inter_of_left _) (ht.inter_of_left _) /-- A more general version of `finprod_mem_union'` that requires `s ∩ mulSupport f` and `t ∩ mulSupport f` rather than `s` and `t` to be disjoint -/ @[to_additive "A more general version of `finsum_mem_union'` that requires `s ∩ support f` and `t ∩ support f` rather than `s` and `t` to be disjoint"] theorem finprod_mem_union'' (hst : Disjoint (s ∩ mulSupport f) (t ∩ mulSupport f)) (hs : (s ∩ mulSupport f).Finite) (ht : (t ∩ mulSupport f).Finite) : ∏ᶠ i ∈ s ∪ t, f i = (∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mulSupport f s, ← finprod_mem_inter_mulSupport f t, ← finprod_mem_union hst hs ht, ← union_inter_distrib_right, finprod_mem_inter_mulSupport] /-- The product of `f i` over `i ∈ {a}` equals `f a`. -/ @[to_additive "The sum of `f i` over `i ∈ {a}` equals `f a`."] theorem finprod_mem_singleton : (∏ᶠ i ∈ ({a} : Set α), f i) = f a := by rw [← Finset.coe_singleton, finprod_mem_coe_finset, Finset.prod_singleton] @[to_additive (attr := simp)] theorem finprod_cond_eq_left : (∏ᶠ (i) (_ : i = a), f i) = f a := finprod_mem_singleton @[to_additive (attr := simp)] theorem finprod_cond_eq_right : (∏ᶠ (i) (_ : a = i), f i) = f a := by simp [@eq_comm _ a] /-- A more general version of `finprod_mem_insert` that requires `s ∩ mulSupport f` rather than `s` to be finite. -/ @[to_additive "A more general version of `finsum_mem_insert` that requires `s ∩ support f` rather than `s` to be finite."] theorem finprod_mem_insert' (f : α → M) (h : a ∉ s) (hs : (s ∩ mulSupport f).Finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := by rw [insert_eq, finprod_mem_union' _ _ hs, finprod_mem_singleton] · rwa [disjoint_singleton_left] · exact (finite_singleton a).inter_of_left _ /-- Given a finite set `s` and an element `a ∉ s`, the product of `f i` over `i ∈ insert a s` equals `f a` times the product of `f i` over `i ∈ s`. -/ @[to_additive "Given a finite set `s` and an element `a ∉ s`, the sum of `f i` over `i ∈ insert a s` equals `f a` plus the sum of `f i` over `i ∈ s`."] theorem finprod_mem_insert (f : α → M) (h : a ∉ s) (hs : s.Finite) : ∏ᶠ i ∈ insert a s, f i = f a * ∏ᶠ i ∈ s, f i := finprod_mem_insert' f h <| hs.inter_of_left _ /-- If `f a = 1` when `a ∉ s`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive "If `f a = 0` when `a ∉ s`, then the sum of `f i` over `i ∈ insert a s` equals the sum of `f i` over `i ∈ s`."] theorem finprod_mem_insert_of_eq_one_if_not_mem (h : a ∉ s → f a = 1) : ∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i := by refine finprod_mem_inter_mulSupport_eq' _ _ _ fun x hx => ⟨?_, Or.inr⟩ rintro (rfl | hxs) exacts [not_imp_comm.1 h hx, hxs] /-- If `f a = 1`, then the product of `f i` over `i ∈ insert a s` equals the product of `f i` over `i ∈ s`. -/ @[to_additive "If `f a = 0`, then the sum of `f i` over `i ∈ insert a s` equals the sum of `f i` over `i ∈ s`."] theorem finprod_mem_insert_one (h : f a = 1) : ∏ᶠ i ∈ insert a s, f i = ∏ᶠ i ∈ s, f i := finprod_mem_insert_of_eq_one_if_not_mem fun _ => h /-- If the multiplicative support of `f` is finite, then for every `x` in the domain of `f`, `f x` divides `finprod f`. -/ theorem finprod_mem_dvd {f : α → N} (a : α) (hf : (mulSupport f).Finite) : f a ∣ finprod f := by by_cases ha : a ∈ mulSupport f · rw [finprod_eq_prod_of_mulSupport_toFinset_subset f hf (Set.Subset.refl _)] exact Finset.dvd_prod_of_mem f ((Finite.mem_toFinset hf).mpr ha) · rw [nmem_mulSupport.mp ha] exact one_dvd (finprod f) /-- The product of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a * f b`. -/ @[to_additive "The sum of `f i` over `i ∈ {a, b}`, `a ≠ b`, is equal to `f a + f b`."] theorem finprod_mem_pair (h : a ≠ b) : (∏ᶠ i ∈ ({a, b} : Set α), f i) = f a * f b := by rw [finprod_mem_insert, finprod_mem_singleton] exacts [h, finite_singleton b] /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s ∩ mulSupport (f ∘ g)`. -/ @[to_additive "The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that `g` is injective on `s ∩ support (f ∘ g)`."] theorem finprod_mem_image' {s : Set β} {g : β → α} (hg : (s ∩ mulSupport (f ∘ g)).InjOn g) : ∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) := by classical by_cases hs : (s ∩ mulSupport (f ∘ g)).Finite · have hg : ∀ x ∈ hs.toFinset, ∀ y ∈ hs.toFinset, g x = g y → x = y := by simpa only [hs.mem_toFinset] have := finprod_mem_eq_prod (comp f g) hs unfold Function.comp at this rw [this, ← Finset.prod_image hg] refine finprod_mem_eq_prod_of_inter_mulSupport_eq f ?_ rw [Finset.coe_image, hs.coe_toFinset, ← image_inter_mulSupport_eq, inter_assoc, inter_self] · unfold Function.comp at hs rw [finprod_mem_eq_one_of_infinite hs, finprod_mem_eq_one_of_infinite] rwa [image_inter_mulSupport_eq, infinite_image_iff hg] /-- The product of `f y` over `y ∈ g '' s` equals the product of `f (g i)` over `s` provided that `g` is injective on `s`. -/ @[to_additive "The sum of `f y` over `y ∈ g '' s` equals the sum of `f (g i)` over `s` provided that `g` is injective on `s`."] theorem finprod_mem_image {s : Set β} {g : β → α} (hg : s.InjOn g) : ∏ᶠ i ∈ g '' s, f i = ∏ᶠ j ∈ s, f (g j) := finprod_mem_image' <| hg.mono inter_subset_left /-- The product of `f y` over `y ∈ Set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective on `mulSupport (f ∘ g)`. -/ @[to_additive "The sum of `f y` over `y ∈ Set.range g` equals the sum of `f (g i)` over all `i` provided that `g` is injective on `support (f ∘ g)`."] theorem finprod_mem_range' {g : β → α} (hg : (mulSupport (f ∘ g)).InjOn g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := by rw [← image_univ, finprod_mem_image', finprod_mem_univ] rwa [univ_inter] /-- The product of `f y` over `y ∈ Set.range g` equals the product of `f (g i)` over all `i` provided that `g` is injective. -/ @[to_additive "The sum of `f y` over `y ∈ Set.range g` equals the sum of `f (g i)` over all `i` provided that `g` is injective."] theorem finprod_mem_range {g : β → α} (hg : Injective g) : ∏ᶠ i ∈ range g, f i = ∏ᶠ j, f (g j) := finprod_mem_range' hg.injOn /-- See also `Finset.prod_bij`. -/ @[to_additive "See also `Finset.sum_bij`."] theorem finprod_mem_eq_of_bijOn {s : Set α} {t : Set β} {f : α → M} {g : β → M} (e : α → β) (he₀ : s.BijOn e t) (he₁ : ∀ x ∈ s, f x = g (e x)) : ∏ᶠ i ∈ s, f i = ∏ᶠ j ∈ t, g j := by rw [← Set.BijOn.image_eq he₀, finprod_mem_image he₀.2.1] exact finprod_mem_congr rfl he₁ /-- See `finprod_comp`, `Fintype.prod_bijective` and `Finset.prod_bij`. -/ @[to_additive "See `finsum_comp`, `Fintype.sum_bijective` and `Finset.sum_bij`."] theorem finprod_eq_of_bijective {f : α → M} {g : β → M} (e : α → β) (he₀ : Bijective e) (he₁ : ∀ x, f x = g (e x)) : ∏ᶠ i, f i = ∏ᶠ j, g j := by rw [← finprod_mem_univ f, ← finprod_mem_univ g] exact finprod_mem_eq_of_bijOn _ (bijective_iff_bijOn_univ.mp he₀) fun x _ => he₁ x /-- See also `finprod_eq_of_bijective`, `Fintype.prod_bijective` and `Finset.prod_bij`. -/ @[to_additive "See also `finsum_eq_of_bijective`, `Fintype.sum_bijective` and `Finset.sum_bij`."] theorem finprod_comp {g : β → M} (e : α → β) (he₀ : Function.Bijective e) : (∏ᶠ i, g (e i)) = ∏ᶠ j, g j := finprod_eq_of_bijective e he₀ fun _ => rfl @[to_additive] theorem finprod_comp_equiv (e : α ≃ β) {f : β → M} : (∏ᶠ i, f (e i)) = ∏ᶠ i', f i' := finprod_comp e e.bijective @[to_additive] theorem finprod_set_coe_eq_finprod_mem (s : Set α) : ∏ᶠ j : s, f j = ∏ᶠ i ∈ s, f i := by rw [← finprod_mem_range, Subtype.range_coe] exact Subtype.coe_injective @[to_additive] theorem finprod_subtype_eq_finprod_cond (p : α → Prop) : ∏ᶠ j : Subtype p, f j = ∏ᶠ (i) (_ : p i), f i := finprod_set_coe_eq_finprod_mem { i | p i } @[to_additive] theorem finprod_mem_inter_mul_diff' (t : Set α) (h : (s ∩ mulSupport f).Finite) : ((∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i) = ∏ᶠ i ∈ s, f i := by rw [← finprod_mem_union', inter_union_diff] · rw [disjoint_iff_inf_le] exact fun x hx => hx.2.2 hx.1.2 exacts [h.subset fun x hx => ⟨hx.1.1, hx.2⟩, h.subset fun x hx => ⟨hx.1.1, hx.2⟩] @[to_additive] theorem finprod_mem_inter_mul_diff (t : Set α) (h : s.Finite) : ((∏ᶠ i ∈ s ∩ t, f i) * ∏ᶠ i ∈ s \ t, f i) = ∏ᶠ i ∈ s, f i := finprod_mem_inter_mul_diff' _ <| h.inter_of_left _ /-- A more general version of `finprod_mem_mul_diff` that requires `t ∩ mulSupport f` rather than `t` to be finite. -/ @[to_additive "A more general version of `finsum_mem_add_diff` that requires `t ∩ support f` rather than `t` to be finite."] theorem finprod_mem_mul_diff' (hst : s ⊆ t) (ht : (t ∩ mulSupport f).Finite) : ((∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i) = ∏ᶠ i ∈ t, f i := by rw [← finprod_mem_inter_mul_diff' _ ht, inter_eq_self_of_subset_right hst] /-- Given a finite set `t` and a subset `s` of `t`, the product of `f i` over `i ∈ s` times the product of `f i` over `t \ s` equals the product of `f i` over `i ∈ t`. -/ @[to_additive "Given a finite set `t` and a subset `s` of `t`, the sum of `f i` over `i ∈ s` plus the sum of `f i` over `t \\ s` equals the sum of `f i` over `i ∈ t`."] theorem finprod_mem_mul_diff (hst : s ⊆ t) (ht : t.Finite) : ((∏ᶠ i ∈ s, f i) * ∏ᶠ i ∈ t \ s, f i) = ∏ᶠ i ∈ t, f i := finprod_mem_mul_diff' hst (ht.inter_of_left _) /-- Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the product of `f a` over the union `⋃ i, t i` is equal to the product over all indexes `i` of the products of `f a` over `a ∈ t i`. -/ @[to_additive "Given a family of pairwise disjoint finite sets `t i` indexed by a finite type, the sum of `f a` over the union `⋃ i, t i` is equal to the sum over all indexes `i` of the sums of `f a` over `a ∈ t i`."] theorem finprod_mem_iUnion [Finite ι] {t : ι → Set α} (h : Pairwise (Disjoint on t)) (ht : ∀ i, (t i).Finite) : ∏ᶠ a ∈ ⋃ i : ι, t i, f a = ∏ᶠ i, ∏ᶠ a ∈ t i, f a := by cases nonempty_fintype ι lift t to ι → Finset α using ht classical rw [← biUnion_univ, ← Finset.coe_univ, ← Finset.coe_biUnion, finprod_mem_coe_finset, Finset.prod_biUnion] · simp only [finprod_mem_coe_finset, finprod_eq_prod_of_fintype] · exact fun x _ y _ hxy => Finset.disjoint_coe.1 (h hxy) /-- Given a family of sets `t : ι → Set α`, a finite set `I` in the index type such that all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the product of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the product over `i ∈ I` of the products of `f a` over `a ∈ t i`. -/ @[to_additive "Given a family of sets `t : ι → Set α`, a finite set `I` in the index type such that all sets `t i`, `i ∈ I`, are finite, if all `t i`, `i ∈ I`, are pairwise disjoint, then the sum of `f a` over `a ∈ ⋃ i ∈ I, t i` is equal to the sum over `i ∈ I` of the sums of `f a` over `a ∈ t i`."] theorem finprod_mem_biUnion {I : Set ι} {t : ι → Set α} (h : I.PairwiseDisjoint t) (hI : I.Finite) (ht : ∀ i ∈ I, (t i).Finite) : ∏ᶠ a ∈ ⋃ x ∈ I, t x, f a = ∏ᶠ i ∈ I, ∏ᶠ j ∈ t i, f j := by haveI := hI.fintype rw [biUnion_eq_iUnion, finprod_mem_iUnion, ← finprod_set_coe_eq_finprod_mem] exacts [fun x y hxy => h x.2 y.2 (Subtype.coe_injective.ne hxy), fun b => ht b b.2] /-- If `t` is a finite set of pairwise disjoint finite sets, then the product of `f a` over `a ∈ ⋃₀ t` is the product over `s ∈ t` of the products of `f a` over `a ∈ s`. -/ @[to_additive "If `t` is a finite set of pairwise disjoint finite sets, then the sum of `f a` over `a ∈ ⋃₀ t` is the sum over `s ∈ t` of the sums of `f a` over `a ∈ s`."] theorem finprod_mem_sUnion {t : Set (Set α)} (h : t.PairwiseDisjoint id) (ht₀ : t.Finite) (ht₁ : ∀ x ∈ t, Set.Finite x) : ∏ᶠ a ∈ ⋃₀ t, f a = ∏ᶠ s ∈ t, ∏ᶠ a ∈ s, f a := by rw [Set.sUnion_eq_biUnion] exact finprod_mem_biUnion h ht₀ ht₁ @[to_additive] theorem mul_finprod_cond_ne (a : α) (hf : (mulSupport f).Finite) : (f a * ∏ᶠ (i) (_ : i ≠ a), f i) = ∏ᶠ i, f i := by classical rw [finprod_eq_prod _ hf] have h : ∀ x : α, f x ≠ 1 → (x ≠ a ↔ x ∈ hf.toFinset \ {a}) := by intro x hx rw [Finset.mem_sdiff, Finset.mem_singleton, Finite.mem_toFinset, mem_mulSupport] exact ⟨fun h => And.intro hx h, fun h => h.2⟩ rw [finprod_cond_eq_prod_of_cond_iff f (fun hx => h _ hx), Finset.sdiff_singleton_eq_erase] by_cases ha : a ∈ mulSupport f · apply Finset.mul_prod_erase _ _ ((Finite.mem_toFinset _).mpr ha) · rw [mem_mulSupport, not_not] at ha rw [ha, one_mul] apply Finset.prod_erase _ ha /-- If `s : Set α` and `t : Set β` are finite sets, then taking the product over `s` commutes with taking the product over `t`. -/ @[to_additive "If `s : Set α` and `t : Set β` are finite sets, then summing over `s` commutes with summing over `t`."] theorem finprod_mem_comm {s : Set α} {t : Set β} (f : α → β → M) (hs : s.Finite) (ht : t.Finite) : (∏ᶠ i ∈ s, ∏ᶠ j ∈ t, f i j) = ∏ᶠ j ∈ t, ∏ᶠ i ∈ s, f i j := by lift s to Finset α using hs; lift t to Finset β using ht simp only [finprod_mem_coe_finset] exact Finset.prod_comm /-- To prove a property of a finite product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a finite sum, it suffices to prove that the property is additive and holds on summands."] theorem finprod_mem_induction (p : M → Prop) (hp₀ : p 1) (hp₁ : ∀ x y, p x → p y → p (x * y)) (hp₂ : ∀ x ∈ s, p <| f x) : p (∏ᶠ i ∈ s, f i) := finprod_induction _ hp₀ hp₁ fun x => finprod_induction _ hp₀ hp₁ <| hp₂ x theorem finprod_cond_nonneg {R : Type*} [OrderedCommSemiring R] {p : α → Prop} {f : α → R} (hf : ∀ x, p x → 0 ≤ f x) : 0 ≤ ∏ᶠ (x) (_ : p x), f x := finprod_nonneg fun x => finprod_nonneg <| hf x @[to_additive] theorem single_le_finprod {M : Type*} [OrderedCommMonoid M] (i : α) {f : α → M} (hf : (mulSupport f).Finite) (h : ∀ j, 1 ≤ f j) : f i ≤ ∏ᶠ j, f j := by classical calc f i ≤ ∏ j ∈ insert i hf.toFinset, f j := Finset.single_le_prod' (fun j _ => h j) (Finset.mem_insert_self _ _) _ = ∏ᶠ j, f j := (finprod_eq_prod_of_mulSupport_toFinset_subset _ hf (Finset.subset_insert _ _)).symm theorem finprod_eq_zero {M₀ : Type*} [CommMonoidWithZero M₀] (f : α → M₀) (x : α) (hx : f x = 0) (hf : (mulSupport f).Finite) : ∏ᶠ x, f x = 0 := by nontriviality rw [finprod_eq_prod f hf] refine Finset.prod_eq_zero (hf.mem_toFinset.2 ?_) hx simp [hx] @[to_additive] theorem finprod_prod_comm (s : Finset β) (f : α → β → M) (h : ∀ b ∈ s, (mulSupport fun a => f a b).Finite) : (∏ᶠ a : α, ∏ b ∈ s, f a b) = ∏ b ∈ s, ∏ᶠ a : α, f a b := by have hU : (mulSupport fun a => ∏ b ∈ s, f a b) ⊆ (s.finite_toSet.biUnion fun b hb => h b (Finset.mem_coe.1 hb)).toFinset := by rw [Finite.coe_toFinset] intro x hx simp only [exists_prop, mem_iUnion, Ne, mem_mulSupport, Finset.mem_coe] contrapose! hx rw [mem_mulSupport, not_not, Finset.prod_congr rfl hx, Finset.prod_const_one] rw [finprod_eq_prod_of_mulSupport_subset _ hU, Finset.prod_comm] refine Finset.prod_congr rfl fun b hb => (finprod_eq_prod_of_mulSupport_subset _ ?_).symm intro a ha simp only [Finite.coe_toFinset, mem_iUnion] exact ⟨b, hb, ha⟩ @[to_additive] theorem prod_finprod_comm (s : Finset α) (f : α → β → M) (h : ∀ a ∈ s, (mulSupport (f a)).Finite) : (∏ a ∈ s, ∏ᶠ b : β, f a b) = ∏ᶠ b : β, ∏ a ∈ s, f a b := (finprod_prod_comm s (fun b a => f a b) h).symm theorem mul_finsum {R : Type*} [Semiring R] (f : α → R) (r : R) (h : (support f).Finite) : (r * ∑ᶠ a : α, f a) = ∑ᶠ a : α, r * f a := (AddMonoidHom.mulLeft r).map_finsum h theorem finsum_mul {R : Type*} [Semiring R] (f : α → R) (r : R) (h : (support f).Finite) : (∑ᶠ a : α, f a) * r = ∑ᶠ a : α, f a * r := (AddMonoidHom.mulRight r).map_finsum h @[to_additive] theorem Finset.mulSupport_of_fiberwise_prod_subset_image [DecidableEq β] (s : Finset α) (f : α → M) (g : α → β) : (mulSupport fun b => (s.filter fun a => g a = b).prod f) ⊆ s.image g := by simp only [Finset.coe_image, Set.mem_image, Finset.mem_coe, Function.support_subset_iff] intro b h suffices (s.filter fun a : α => g a = b).Nonempty by simpa only [s.fiber_nonempty_iff_mem_image g b, Finset.mem_image, exists_prop] exact Finset.nonempty_of_prod_ne_one h /-- Note that `b ∈ (s.filter (fun ab => Prod.fst ab = a)).image Prod.snd` iff `(a, b) ∈ s` so we can simplify the right hand side of this lemma. However the form stated here is more useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`. -/ @[to_additive "Note that `b ∈ (s.filter (fun ab => Prod.fst ab = a)).image Prod.snd` iff `(a, b) ∈ s` so we can simplify the right hand side of this lemma. However the form stated here is more useful for iterating this lemma, e.g., if we have `f : α × β × γ → M`."] theorem finprod_mem_finset_product' [DecidableEq α] [DecidableEq β] (s : Finset (α × β)) (f : α × β → M) : (∏ᶠ (ab) (_ : ab ∈ s), f ab) = ∏ᶠ (a) (b) (_ : b ∈ (s.filter fun ab => Prod.fst ab = a).image Prod.snd), f (a, b) := by have (a) : ∏ i ∈ (s.filter fun ab => Prod.fst ab = a).image Prod.snd, f (a, i) = (s.filter (Prod.fst · = a)).prod f := by refine Finset.prod_nbij' (fun b ↦ (a, b)) Prod.snd ?_ ?_ ?_ ?_ ?_ <;> aesop rw [finprod_mem_finset_eq_prod] simp_rw [finprod_mem_finset_eq_prod, this] rw [finprod_eq_prod_of_mulSupport_subset _ (s.mulSupport_of_fiberwise_prod_subset_image f Prod.fst), ← Finset.prod_fiberwise_of_maps_to (t := Finset.image Prod.fst s) _ f] -- `finish` could close the goal here simp only [Finset.mem_image] exact fun x hx => ⟨x, hx, rfl⟩ /-- See also `finprod_mem_finset_product'`. -/ @[to_additive "See also `finsum_mem_finset_product'`."] theorem finprod_mem_finset_product (s : Finset (α × β)) (f : α × β → M) : (∏ᶠ (ab) (_ : ab ∈ s), f ab) = ∏ᶠ (a) (b) (_ : (a, b) ∈ s), f (a, b) := by classical rw [finprod_mem_finset_product'] simp @[to_additive] theorem finprod_mem_finset_product₃ {γ : Type*} (s : Finset (α × β × γ)) (f : α × β × γ → M) : (∏ᶠ (abc) (_ : abc ∈ s), f abc) = ∏ᶠ (a) (b) (c) (_ : (a, b, c) ∈ s), f (a, b, c) := by classical rw [finprod_mem_finset_product'] simp_rw [finprod_mem_finset_product'] simp @[to_additive] theorem finprod_curry (f : α × β → M) (hf : (mulSupport f).Finite) : ∏ᶠ ab, f ab = ∏ᶠ (a) (b), f (a, b) := by have h₁ : ∀ a, ∏ᶠ _ : a ∈ hf.toFinset, f a = f a := by simp have h₂ : ∏ᶠ a, f a = ∏ᶠ (a) (_ : a ∈ hf.toFinset), f a := by simp simp_rw [h₂, finprod_mem_finset_product, h₁] @[to_additive] theorem finprod_curry₃ {γ : Type*} (f : α × β × γ → M) (h : (mulSupport f).Finite) : ∏ᶠ abc, f abc = ∏ᶠ (a) (b) (c), f (a, b, c) := by rw [finprod_curry f h] congr ext a rw [finprod_curry] simp [h] @[to_additive] theorem finprod_dmem {s : Set α} [DecidablePred (· ∈ s)] (f : ∀ a : α, a ∈ s → M) : (∏ᶠ (a : α) (h : a ∈ s), f a h) = ∏ᶠ (a : α) (_ : a ∈ s), if h' : a ∈ s then f a h' else 1 := finprod_congr fun _ => finprod_congr fun ha => (dif_pos ha).symm @[to_additive] theorem finprod_emb_domain' {f : α → β} (hf : Injective f) [DecidablePred (· ∈ Set.range f)] (g : α → M) : (∏ᶠ b : β, if h : b ∈ Set.range f then g (Classical.choose h) else 1) = ∏ᶠ a : α, g a := by simp_rw [← finprod_eq_dif] rw [finprod_dmem, finprod_mem_range hf, finprod_congr fun a => _] intro a rw [dif_pos (Set.mem_range_self a), hf (Classical.choose_spec (Set.mem_range_self a))] @[to_additive] theorem finprod_emb_domain (f : α ↪ β) [DecidablePred (· ∈ Set.range f)] (g : α → M) : (∏ᶠ b : β, if h : b ∈ Set.range f then g (Classical.choose h) else 1) = ∏ᶠ a : α, g a := finprod_emb_domain' f.injective g end type
Algebra\BigOperators\Finsupp.lean
/- Copyright (c) 2020 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau -/ import Mathlib.Algebra.BigOperators.Pi import Mathlib.Algebra.BigOperators.Ring import Mathlib.Algebra.BigOperators.Fin import Mathlib.Algebra.Group.Submonoid.Membership import Mathlib.Data.Finsupp.Fin import Mathlib.Data.Finsupp.Indicator /-! # Big operators for finsupps This file contains theorems relevant to big operators in finitely supported functions. -/ noncomputable section open Finset Function variable {α ι γ A B C : Type*} [AddCommMonoid A] [AddCommMonoid B] [AddCommMonoid C] variable {t : ι → A → C} variable {s : Finset α} {f : α → ι →₀ A} (i : ι) variable (g : ι →₀ A) (k : ι → A → γ → B) (x : γ) variable {β M M' N P G H R S : Type*} namespace Finsupp /-! ### Declarations about `Finsupp.sum` and `Finsupp.prod` In most of this section, the domain `β` is assumed to be an `AddMonoid`. -/ section SumProd /-- `prod f g` is the product of `g a (f a)` over the support of `f`. -/ @[to_additive "`sum f g` is the sum of `g a (f a)` over the support of `f`. "] def prod [Zero M] [CommMonoid N] (f : α →₀ M) (g : α → M → N) : N := ∏ a ∈ f.support, g a (f a) variable [Zero M] [Zero M'] [CommMonoid N] @[to_additive] theorem prod_of_support_subset (f : α →₀ M) {s : Finset α} (hs : f.support ⊆ s) (g : α → M → N) (h : ∀ i ∈ s, g i 0 = 1) : f.prod g = ∏ x ∈ s, g x (f x) := by refine Finset.prod_subset hs fun x hxs hx => h x hxs ▸ (congr_arg (g x) ?_) exact not_mem_support_iff.1 hx @[to_additive] theorem prod_fintype [Fintype α] (f : α →₀ M) (g : α → M → N) (h : ∀ i, g i 0 = 1) : f.prod g = ∏ i, g i (f i) := f.prod_of_support_subset (subset_univ _) g fun x _ => h x @[to_additive (attr := simp)] theorem prod_single_index {a : α} {b : M} {h : α → M → N} (h_zero : h a 0 = 1) : (single a b).prod h = h a b := calc (single a b).prod h = ∏ x ∈ {a}, h x (single a b x) := prod_of_support_subset _ support_single_subset h fun x hx => (mem_singleton.1 hx).symm ▸ h_zero _ = h a b := by simp @[to_additive] theorem prod_mapRange_index {f : M → M'} {hf : f 0 = 0} {g : α →₀ M} {h : α → M' → N} (h0 : ∀ a, h a 0 = 1) : (mapRange f hf g).prod h = g.prod fun a b => h a (f b) := Finset.prod_subset support_mapRange fun _ _ H => by rw [not_mem_support_iff.1 H, h0] @[to_additive (attr := simp)] theorem prod_zero_index {h : α → M → N} : (0 : α →₀ M).prod h = 1 := rfl @[to_additive] theorem prod_comm (f : α →₀ M) (g : β →₀ M') (h : α → M → β → M' → N) : (f.prod fun x v => g.prod fun x' v' => h x v x' v') = g.prod fun x' v' => f.prod fun x v => h x v x' v' := Finset.prod_comm @[to_additive (attr := simp)] theorem prod_ite_eq [DecidableEq α] (f : α →₀ M) (a : α) (b : α → M → N) : (f.prod fun x v => ite (a = x) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by dsimp [Finsupp.prod] rw [f.support.prod_ite_eq] /- Porting note: simpnf linter, added aux lemma below Left-hand side simplifies from Finsupp.sum f fun x v => if a = x then v else 0 to if ↑f a = 0 then 0 else ↑f a -/ -- @[simp] theorem sum_ite_self_eq [DecidableEq α] {N : Type*} [AddCommMonoid N] (f : α →₀ N) (a : α) : (f.sum fun x v => ite (a = x) v 0) = f a := by classical convert f.sum_ite_eq a fun _ => id simp [ite_eq_right_iff.2 Eq.symm] -- Porting note: Added this thm to replace the simp in the previous one. Need to add [DecidableEq N] @[simp] theorem sum_ite_self_eq_aux [DecidableEq α] {N : Type*} [AddCommMonoid N] (f : α →₀ N) (a : α) : (if a ∈ f.support then f a else 0) = f a := by simp only [mem_support_iff, ne_eq, ite_eq_left_iff, not_not] exact fun h ↦ h.symm /-- A restatement of `prod_ite_eq` with the equality test reversed. -/ @[to_additive (attr := simp) "A restatement of `sum_ite_eq` with the equality test reversed."] theorem prod_ite_eq' [DecidableEq α] (f : α →₀ M) (a : α) (b : α → M → N) : (f.prod fun x v => ite (x = a) (b x v) 1) = ite (a ∈ f.support) (b a (f a)) 1 := by dsimp [Finsupp.prod] rw [f.support.prod_ite_eq'] -- Porting note (#10618): simp can prove this -- @[simp] theorem sum_ite_self_eq' [DecidableEq α] {N : Type*} [AddCommMonoid N] (f : α →₀ N) (a : α) : (f.sum fun x v => ite (x = a) v 0) = f a := by classical convert f.sum_ite_eq' a fun _ => id simp [ite_eq_right_iff.2 Eq.symm] @[simp] theorem prod_pow [Fintype α] (f : α →₀ ℕ) (g : α → N) : (f.prod fun a b => g a ^ b) = ∏ a, g a ^ f a := f.prod_fintype _ fun _ ↦ pow_zero _ /-- If `g` maps a second argument of 0 to 1, then multiplying it over the result of `onFinset` is the same as multiplying it over the original `Finset`. -/ @[to_additive "If `g` maps a second argument of 0 to 0, summing it over the result of `onFinset` is the same as summing it over the original `Finset`."] theorem onFinset_prod {s : Finset α} {f : α → M} {g : α → M → N} (hf : ∀ a, f a ≠ 0 → a ∈ s) (hg : ∀ a, g a 0 = 1) : (onFinset s f hf).prod g = ∏ a ∈ s, g a (f a) := Finset.prod_subset support_onFinset_subset <| by simp (config := { contextual := true }) [*] /-- Taking a product over `f : α →₀ M` is the same as multiplying the value on a single element `y ∈ f.support` by the product over `erase y f`. -/ @[to_additive " Taking a sum over `f : α →₀ M` is the same as adding the value on a single element `y ∈ f.support` to the sum over `erase y f`. "] theorem mul_prod_erase (f : α →₀ M) (y : α) (g : α → M → N) (hyf : y ∈ f.support) : g y (f y) * (erase y f).prod g = f.prod g := by classical rw [Finsupp.prod, Finsupp.prod, ← Finset.mul_prod_erase _ _ hyf, Finsupp.support_erase, Finset.prod_congr rfl] intro h hx rw [Finsupp.erase_ne (ne_of_mem_erase hx)] /-- Generalization of `Finsupp.mul_prod_erase`: if `g` maps a second argument of 0 to 1, then its product over `f : α →₀ M` is the same as multiplying the value on any element `y : α` by the product over `erase y f`. -/ @[to_additive " Generalization of `Finsupp.add_sum_erase`: if `g` maps a second argument of 0 to 0, then its sum over `f : α →₀ M` is the same as adding the value on any element `y : α` to the sum over `erase y f`. "] theorem mul_prod_erase' (f : α →₀ M) (y : α) (g : α → M → N) (hg : ∀ i : α, g i 0 = 1) : g y (f y) * (erase y f).prod g = f.prod g := by classical by_cases hyf : y ∈ f.support · exact Finsupp.mul_prod_erase f y g hyf · rw [not_mem_support_iff.mp hyf, hg y, erase_of_not_mem_support hyf, one_mul] @[to_additive] theorem _root_.SubmonoidClass.finsupp_prod_mem {S : Type*} [SetLike S N] [SubmonoidClass S N] (s : S) (f : α →₀ M) (g : α → M → N) (h : ∀ c, f c ≠ 0 → g c (f c) ∈ s) : f.prod g ∈ s := prod_mem fun _i hi => h _ (Finsupp.mem_support_iff.mp hi) @[to_additive] theorem prod_congr {f : α →₀ M} {g1 g2 : α → M → N} (h : ∀ x ∈ f.support, g1 x (f x) = g2 x (f x)) : f.prod g1 = f.prod g2 := Finset.prod_congr rfl h @[to_additive] theorem prod_eq_single {f : α →₀ M} (a : α) {g : α → M → N} (h₀ : ∀ b, f b ≠ 0 → b ≠ a → g b (f b) = 1) (h₁ : f a = 0 → g a 0 = 1) : f.prod g = g a (f a) := by refine Finset.prod_eq_single a (fun b hb₁ hb₂ => ?_) (fun h => ?_) · exact h₀ b (mem_support_iff.mp hb₁) hb₂ · simp only [not_mem_support_iff] at h rw [h] exact h₁ h end SumProd section CommMonoidWithZero variable [Zero α] [CommMonoidWithZero β] [Nontrivial β] [NoZeroDivisors β] {f : ι →₀ α} (a : α) {g : ι → α → β} @[simp] lemma prod_eq_zero_iff : f.prod g = 0 ↔ ∃ i ∈ f.support, g i (f i) = 0 := Finset.prod_eq_zero_iff lemma prod_ne_zero_iff : f.prod g ≠ 0 ↔ ∀ i ∈ f.support, g i (f i) ≠ 0 := Finset.prod_ne_zero_iff end CommMonoidWithZero end Finsupp @[to_additive] theorem map_finsupp_prod [Zero M] [CommMonoid N] [CommMonoid P] {H : Type*} [FunLike H N P] [MonoidHomClass H N P] (h : H) (f : α →₀ M) (g : α → M → N) : h (f.prod g) = f.prod fun a b => h (g a b) := map_prod h _ _ -- Porting note: inserted ⇑ on the rhs @[to_additive] theorem MonoidHom.coe_finsupp_prod [Zero β] [Monoid N] [CommMonoid P] (f : α →₀ β) (g : α → β → N →* P) : ⇑(f.prod g) = f.prod fun i fi => ⇑(g i fi) := MonoidHom.coe_finset_prod _ _ @[to_additive (attr := simp)] theorem MonoidHom.finsupp_prod_apply [Zero β] [Monoid N] [CommMonoid P] (f : α →₀ β) (g : α → β → N →* P) (x : N) : f.prod g x = f.prod fun i fi => g i fi x := MonoidHom.finset_prod_apply _ _ _ namespace Finsupp theorem single_multiset_sum [AddCommMonoid M] (s : Multiset M) (a : α) : single a s.sum = (s.map (single a)).sum := Multiset.induction_on s (single_zero _) fun a s ih => by rw [Multiset.sum_cons, single_add, ih, Multiset.map_cons, Multiset.sum_cons] theorem single_finset_sum [AddCommMonoid M] (s : Finset ι) (f : ι → M) (a : α) : single a (∑ b ∈ s, f b) = ∑ b ∈ s, single a (f b) := by trans · apply single_multiset_sum · rw [Multiset.map_map] rfl theorem single_sum [Zero M] [AddCommMonoid N] (s : ι →₀ M) (f : ι → M → N) (a : α) : single a (s.sum f) = s.sum fun d c => single a (f d c) := single_finset_sum _ _ _ @[to_additive] theorem prod_neg_index [AddGroup G] [CommMonoid M] {g : α →₀ G} {h : α → G → M} (h0 : ∀ a, h a 0 = 1) : (-g).prod h = g.prod fun a b => h a (-b) := prod_mapRange_index h0 end Finsupp namespace Finsupp theorem finset_sum_apply [AddCommMonoid N] (S : Finset ι) (f : ι → α →₀ N) (a : α) : (∑ i ∈ S, f i) a = ∑ i ∈ S, f i a := map_sum (applyAddHom a) _ _ @[simp] theorem sum_apply [Zero M] [AddCommMonoid N] {f : α →₀ M} {g : α → M → β →₀ N} {a₂ : β} : (f.sum g) a₂ = f.sum fun a₁ b => g a₁ b a₂ := finset_sum_apply _ _ _ -- Porting note: inserted ⇑ on the rhs theorem coe_finset_sum [AddCommMonoid N] (S : Finset ι) (f : ι → α →₀ N) : ⇑(∑ i ∈ S, f i) = ∑ i ∈ S, ⇑(f i) := map_sum (coeFnAddHom : (α →₀ N) →+ _) _ _ -- Porting note: inserted ⇑ on the rhs theorem coe_sum [Zero M] [AddCommMonoid N] (f : α →₀ M) (g : α → M → β →₀ N) : ⇑(f.sum g) = f.sum fun a₁ b => ⇑(g a₁ b) := coe_finset_sum _ _ theorem support_sum [DecidableEq β] [Zero M] [AddCommMonoid N] {f : α →₀ M} {g : α → M → β →₀ N} : (f.sum g).support ⊆ f.support.biUnion fun a => (g a (f a)).support := by have : ∀ c, (f.sum fun a b => g a b c) ≠ 0 → ∃ a, f a ≠ 0 ∧ ¬(g a (f a)) c = 0 := fun a₁ h => let ⟨a, ha, ne⟩ := Finset.exists_ne_zero_of_sum_ne_zero h ⟨a, mem_support_iff.mp ha, ne⟩ simpa only [Finset.subset_iff, mem_support_iff, Finset.mem_biUnion, sum_apply, exists_prop] theorem support_finset_sum [DecidableEq β] [AddCommMonoid M] {s : Finset α} {f : α → β →₀ M} : (Finset.sum s f).support ⊆ s.biUnion fun x => (f x).support := by rw [← Finset.sup_eq_biUnion] induction' s using Finset.cons_induction_on with a s ha ih · rfl · rw [Finset.sum_cons, Finset.sup_cons] exact support_add.trans (Finset.union_subset_union (Finset.Subset.refl _) ih) @[simp] theorem sum_zero [Zero M] [AddCommMonoid N] {f : α →₀ M} : (f.sum fun _ _ => (0 : N)) = 0 := Finset.sum_const_zero @[to_additive (attr := simp)] theorem prod_mul [Zero M] [CommMonoid N] {f : α →₀ M} {h₁ h₂ : α → M → N} : (f.prod fun a b => h₁ a b * h₂ a b) = f.prod h₁ * f.prod h₂ := Finset.prod_mul_distrib @[to_additive (attr := simp)] theorem prod_inv [Zero M] [CommGroup G] {f : α →₀ M} {h : α → M → G} : (f.prod fun a b => (h a b)⁻¹) = (f.prod h)⁻¹ := (map_prod (MonoidHom.id G)⁻¹ _ _).symm @[simp] theorem sum_sub [Zero M] [AddCommGroup G] {f : α →₀ M} {h₁ h₂ : α → M → G} : (f.sum fun a b => h₁ a b - h₂ a b) = f.sum h₁ - f.sum h₂ := Finset.sum_sub_distrib /-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps, if `h` is an additive-to-multiplicative homomorphism on the support. This is a more general version of `Finsupp.prod_add_index'`; the latter has simpler hypotheses. -/ @[to_additive "Taking the product under `h` is an additive homomorphism of finsupps, if `h` is an additive homomorphism on the support. This is a more general version of `Finsupp.sum_add_index'`; the latter has simpler hypotheses."] theorem prod_add_index [DecidableEq α] [AddZeroClass M] [CommMonoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀ a ∈ f.support ∪ g.support, h a 0 = 1) (h_add : ∀ a ∈ f.support ∪ g.support, ∀ (b₁ b₂), h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := by rw [Finsupp.prod_of_support_subset f subset_union_left h h_zero, Finsupp.prod_of_support_subset g subset_union_right h h_zero, ← Finset.prod_mul_distrib, Finsupp.prod_of_support_subset (f + g) Finsupp.support_add h h_zero] exact Finset.prod_congr rfl fun x hx => by apply h_add x hx /-- Taking the product under `h` is an additive-to-multiplicative homomorphism of finsupps, if `h` is an additive-to-multiplicative homomorphism. This is a more specialized version of `Finsupp.prod_add_index` with simpler hypotheses. -/ @[to_additive "Taking the sum under `h` is an additive homomorphism of finsupps,if `h` is an additive homomorphism. This is a more specific version of `Finsupp.sum_add_index` with simpler hypotheses."] theorem prod_add_index' [AddZeroClass M] [CommMonoid N] {f g : α →₀ M} {h : α → M → N} (h_zero : ∀ a, h a 0 = 1) (h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f + g).prod h = f.prod h * g.prod h := by classical exact prod_add_index (fun a _ => h_zero a) fun a _ => h_add a @[simp] theorem sum_hom_add_index [AddZeroClass M] [AddCommMonoid N] {f g : α →₀ M} (h : α → M →+ N) : ((f + g).sum fun x => h x) = (f.sum fun x => h x) + g.sum fun x => h x := sum_add_index' (fun a => (h a).map_zero) fun a => (h a).map_add @[simp] theorem prod_hom_add_index [AddZeroClass M] [CommMonoid N] {f g : α →₀ M} (h : α → Multiplicative M →* N) : ((f + g).prod fun a b => h a (Multiplicative.ofAdd b)) = (f.prod fun a b => h a (Multiplicative.ofAdd b)) * g.prod fun a b => h a (Multiplicative.ofAdd b) := prod_add_index' (fun a => (h a).map_one) fun a => (h a).map_mul /-- The canonical isomorphism between families of additive monoid homomorphisms `α → (M →+ N)` and monoid homomorphisms `(α →₀ M) →+ N`. -/ def liftAddHom [AddZeroClass M] [AddCommMonoid N] : (α → M →+ N) ≃+ ((α →₀ M) →+ N) where toFun F := { toFun := fun f ↦ f.sum fun x ↦ F x map_zero' := Finset.sum_empty map_add' := fun _ _ => sum_add_index' (fun x => (F x).map_zero) fun x => (F x).map_add } invFun F x := F.comp (singleAddHom x) left_inv F := by ext simp [singleAddHom] right_inv F := by -- Porting note: This was `ext` and used the wrong lemma apply Finsupp.addHom_ext' simp [singleAddHom, AddMonoidHom.comp, Function.comp] map_add' F G := by ext x exact sum_add @[simp] theorem liftAddHom_apply [AddCommMonoid M] [AddCommMonoid N] (F : α → M →+ N) (f : α →₀ M) : (liftAddHom (α := α) (M := M) (N := N)) F f = f.sum fun x => F x := rfl @[simp] theorem liftAddHom_symm_apply [AddCommMonoid M] [AddCommMonoid N] (F : (α →₀ M) →+ N) (x : α) : (liftAddHom (α := α) (M := M) (N := N)).symm F x = F.comp (singleAddHom x) := rfl theorem liftAddHom_symm_apply_apply [AddCommMonoid M] [AddCommMonoid N] (F : (α →₀ M) →+ N) (x : α) (y : M) : (liftAddHom (α := α) (M := M) (N := N)).symm F x y = F (single x y) := rfl @[simp] theorem liftAddHom_singleAddHom [AddCommMonoid M] : (liftAddHom (α := α) (M := M) (N := α →₀ M)) (singleAddHom : α → M →+ α →₀ M) = AddMonoidHom.id _ := liftAddHom.toEquiv.apply_eq_iff_eq_symm_apply.2 rfl @[simp] theorem sum_single [AddCommMonoid M] (f : α →₀ M) : f.sum single = f := DFunLike.congr_fun liftAddHom_singleAddHom f /-- The `Finsupp` version of `Finset.univ_sum_single` -/ @[simp] theorem univ_sum_single [Fintype α] [AddCommMonoid M] (f : α →₀ M) : ∑ a : α, single a (f a) = f := by classical refine DFunLike.coe_injective ?_ simp_rw [coe_finset_sum, single_eq_pi_single, Finset.univ_sum_single] @[simp] theorem univ_sum_single_apply [AddCommMonoid M] [Fintype α] (i : α) (m : M) : ∑ j : α, single i m j = m := by -- Porting note: rewrite due to leaky classical in lean3 classical rw [single, coe_mk, Finset.sum_pi_single'] simp @[simp] theorem univ_sum_single_apply' [AddCommMonoid M] [Fintype α] (i : α) (m : M) : ∑ j : α, single j m i = m := by -- Porting note: rewrite due to leaky classical in lean3 simp_rw [single, coe_mk] classical rw [Finset.sum_pi_single] simp theorem equivFunOnFinite_symm_eq_sum [Fintype α] [AddCommMonoid M] (f : α → M) : equivFunOnFinite.symm f = ∑ a, Finsupp.single a (f a) := by rw [← univ_sum_single (equivFunOnFinite.symm f)] ext simp -- Porting note (#10618): simp can prove this -- @[simp] theorem liftAddHom_apply_single [AddCommMonoid M] [AddCommMonoid N] (f : α → M →+ N) (a : α) (b : M) : (liftAddHom (α := α) (M := M) (N := N)) f (single a b) = f a b := sum_single_index (f a).map_zero @[simp] theorem liftAddHom_comp_single [AddCommMonoid M] [AddCommMonoid N] (f : α → M →+ N) (a : α) : ((liftAddHom (α := α) (M := M) (N := N)) f).comp (singleAddHom a) = f a := AddMonoidHom.ext fun b => liftAddHom_apply_single f a b theorem comp_liftAddHom [AddCommMonoid M] [AddCommMonoid N] [AddCommMonoid P] (g : N →+ P) (f : α → M →+ N) : g.comp ((liftAddHom (α := α) (M := M) (N := N)) f) = (liftAddHom (α := α) (M := M) (N := P)) fun a => g.comp (f a) := liftAddHom.symm_apply_eq.1 <| funext fun a => by rw [liftAddHom_symm_apply, AddMonoidHom.comp_assoc, liftAddHom_comp_single] theorem sum_sub_index [AddCommGroup β] [AddCommGroup γ] {f g : α →₀ β} {h : α → β → γ} (h_sub : ∀ a b₁ b₂, h a (b₁ - b₂) = h a b₁ - h a b₂) : (f - g).sum h = f.sum h - g.sum h := ((liftAddHom (α := α) (M := β) (N := γ)) fun a => AddMonoidHom.ofMapSub (h a) (h_sub a)).map_sub f g @[to_additive] theorem prod_embDomain [Zero M] [CommMonoid N] {v : α →₀ M} {f : α ↪ β} {g : β → M → N} : (v.embDomain f).prod g = v.prod fun a b => g (f a) b := by rw [prod, prod, support_embDomain, Finset.prod_map] simp_rw [embDomain_apply] @[to_additive] theorem prod_finset_sum_index [AddCommMonoid M] [CommMonoid N] {s : Finset ι} {g : ι → α →₀ M} {h : α → M → N} (h_zero : ∀ a, h a 0 = 1) (h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (∏ i ∈ s, (g i).prod h) = (∑ i ∈ s, g i).prod h := Finset.cons_induction_on s rfl fun a s has ih => by rw [prod_cons, ih, sum_cons, prod_add_index' h_zero h_add] @[to_additive] theorem prod_sum_index [AddCommMonoid M] [AddCommMonoid N] [CommMonoid P] {f : α →₀ M} {g : α → M → β →₀ N} {h : β → N → P} (h_zero : ∀ a, h a 0 = 1) (h_add : ∀ a b₁ b₂, h a (b₁ + b₂) = h a b₁ * h a b₂) : (f.sum g).prod h = f.prod fun a b => (g a b).prod h := (prod_finset_sum_index h_zero h_add).symm theorem multiset_sum_sum_index [AddCommMonoid M] [AddCommMonoid N] (f : Multiset (α →₀ M)) (h : α → M → N) (h₀ : ∀ a, h a 0 = 0) (h₁ : ∀ (a : α) (b₁ b₂ : M), h a (b₁ + b₂) = h a b₁ + h a b₂) : f.sum.sum h = (f.map fun g : α →₀ M => g.sum h).sum := Multiset.induction_on f rfl fun a s ih => by rw [Multiset.sum_cons, Multiset.map_cons, Multiset.sum_cons, sum_add_index' h₀ h₁, ih] theorem support_sum_eq_biUnion {α : Type*} {ι : Type*} {M : Type*} [DecidableEq α] [AddCommMonoid M] {g : ι → α →₀ M} (s : Finset ι) (h : ∀ i₁ i₂, i₁ ≠ i₂ → Disjoint (g i₁).support (g i₂).support) : (∑ i ∈ s, g i).support = s.biUnion fun i => (g i).support := by classical -- Porting note: apply Finset.induction_on s was not working; refine does. refine Finset.induction_on s ?_ ?_ · simp · intro i s hi simp only [hi, sum_insert, not_false_iff, biUnion_insert] intro hs rw [Finsupp.support_add_eq, hs] rw [hs, Finset.disjoint_biUnion_right] intro j hj exact h _ _ (ne_of_mem_of_not_mem hj hi).symm theorem multiset_map_sum [Zero M] {f : α →₀ M} {m : β → γ} {h : α → M → Multiset β} : Multiset.map m (f.sum h) = f.sum fun a b => (h a b).map m := map_sum (Multiset.mapAddMonoidHom m) _ f.support theorem multiset_sum_sum [Zero M] [AddCommMonoid N] {f : α →₀ M} {h : α → M → Multiset N} : Multiset.sum (f.sum h) = f.sum fun a b => Multiset.sum (h a b) := map_sum Multiset.sumAddMonoidHom _ f.support /-- For disjoint `f1` and `f2`, and function `g`, the product of the products of `g` over `f1` and `f2` equals the product of `g` over `f1 + f2` -/ @[to_additive "For disjoint `f1` and `f2`, and function `g`, the sum of the sums of `g` over `f1` and `f2` equals the sum of `g` over `f1 + f2`"] theorem prod_add_index_of_disjoint [AddCommMonoid M] {f1 f2 : α →₀ M} (hd : Disjoint f1.support f2.support) {β : Type*} [CommMonoid β] (g : α → M → β) : (f1 + f2).prod g = f1.prod g * f2.prod g := by have : ∀ {f1 f2 : α →₀ M}, Disjoint f1.support f2.support → (∏ x ∈ f1.support, g x (f1 x + f2 x)) = f1.prod g := fun hd => Finset.prod_congr rfl fun x hx => by simp only [not_mem_support_iff.mp (disjoint_left.mp hd hx), add_zero] classical simp_rw [← this hd, ← this hd.symm, add_comm (f2 _), Finsupp.prod, support_add_eq hd, prod_union hd, add_apply] theorem prod_dvd_prod_of_subset_of_dvd [AddCommMonoid M] [CommMonoid N] {f1 f2 : α →₀ M} {g1 g2 : α → M → N} (h1 : f1.support ⊆ f2.support) (h2 : ∀ a : α, a ∈ f1.support → g1 a (f1 a) ∣ g2 a (f2 a)) : f1.prod g1 ∣ f2.prod g2 := by classical simp only [Finsupp.prod, Finsupp.prod_mul] rw [← sdiff_union_of_subset h1, prod_union sdiff_disjoint] apply dvd_mul_of_dvd_right apply prod_dvd_prod_of_dvd exact h2 lemma indicator_eq_sum_attach_single [AddCommMonoid M] {s : Finset α} (f : ∀ a ∈ s, M) : indicator s f = ∑ x ∈ s.attach, single ↑x (f x x.2) := by rw [← sum_single (indicator s f), sum, sum_subset (support_indicator_subset _ _), ← sum_attach] · refine Finset.sum_congr rfl (fun _ _ => ?_) rw [indicator_of_mem] · intro i _ hi rw [not_mem_support_iff.mp hi, single_zero] lemma indicator_eq_sum_single [AddCommMonoid M] (s : Finset α) (f : α → M) : indicator s (fun x _ ↦ f x) = ∑ x ∈ s, single x (f x) := (indicator_eq_sum_attach_single _).trans <| sum_attach _ fun x ↦ single x (f x) @[to_additive (attr := simp)] lemma prod_indicator_index_eq_prod_attach [Zero M] [CommMonoid N] {s : Finset α} (f : ∀ a ∈ s, M) {h : α → M → N} (h_zero : ∀ a ∈ s, h a 0 = 1) : (indicator s f).prod h = ∏ x ∈ s.attach, h ↑x (f x x.2) := by rw [prod_of_support_subset _ (support_indicator_subset _ _) h h_zero, ← prod_attach] refine Finset.prod_congr rfl (fun _ _ => ?_) rw [indicator_of_mem] @[to_additive (attr := simp)] lemma prod_indicator_index [Zero M] [CommMonoid N] {s : Finset α} (f : α → M) {h : α → M → N} (h_zero : ∀ a ∈ s, h a 0 = 1) : (indicator s (fun x _ ↦ f x)).prod h = ∏ x ∈ s, h x (f x) := (prod_indicator_index_eq_prod_attach _ h_zero).trans <| prod_attach _ fun x ↦ h x (f x) lemma sum_cons [AddCommMonoid M] (n : ℕ) (σ : Fin n →₀ M) (i : M) : (sum (cons i σ) fun _ e ↦ e) = i + sum σ (fun _ e ↦ e) := by rw [sum_fintype _ _ (fun _ => rfl), sum_fintype _ _ (fun _ => rfl)] exact Fin.sum_cons i σ lemma sum_cons' [AddCommMonoid M] [AddCommMonoid N] (n : ℕ) (σ : Fin n →₀ M) (i : M) (f : Fin (n+1) → M → N) (h : ∀ x, f x 0 = 0) : (sum (Finsupp.cons i σ) f) = f 0 i + sum σ (Fin.tail f) := by rw [sum_fintype _ _ (fun _ => by apply h), sum_fintype _ _ (fun _ => by apply h)] simp_rw [Fin.sum_univ_succ, cons_zero, cons_succ] congr @[to_additive] lemma prod_mul_eq_prod_mul_of_exists [Zero M] [CommMonoid N] {f : α →₀ M} {g : α → M → N} {n₁ n₂ : N} (a : α) (ha : a ∈ f.support) (h : g a (f a) * n₁ = g a (f a) * n₂) : f.prod g * n₁ = f.prod g * n₂ := by classical exact Finset.prod_mul_eq_prod_mul_of_exists a ha h end Finsupp theorem Finset.sum_apply' : (∑ k ∈ s, f k) i = ∑ k ∈ s, f k i := map_sum (Finsupp.applyAddHom i) f s theorem Finsupp.sum_apply' : g.sum k x = g.sum fun i b => k i b x := Finset.sum_apply _ _ _ theorem Finsupp.sum_sum_index' (h0 : ∀ i, t i 0 = 0) (h1 : ∀ i x y, t i (x + y) = t i x + t i y) : (∑ x ∈ s, f x).sum t = ∑ x ∈ s, (f x).sum t := by classical exact Finset.induction_on s rfl fun a s has ih => by simp_rw [Finset.sum_insert has, Finsupp.sum_add_index' h0 h1, ih] section variable [NonUnitalNonAssocSemiring R] [NonUnitalNonAssocSemiring S] theorem Finsupp.sum_mul (b : S) (s : α →₀ R) {f : α → R → S} : s.sum f * b = s.sum fun a c => f a c * b := by simp only [Finsupp.sum, Finset.sum_mul] theorem Finsupp.mul_sum (b : S) (s : α →₀ R) {f : α → R → S} : b * s.sum f = s.sum fun a c => b * f a c := by simp only [Finsupp.sum, Finset.mul_sum] end namespace Nat -- Porting note: Needed to replace pow with (· ^ ·) /-- If `0 : ℕ` is not in the support of `f : ℕ →₀ ℕ` then `0 < ∏ x ∈ f.support, x ^ (f x)`. -/ theorem prod_pow_pos_of_zero_not_mem_support {f : ℕ →₀ ℕ} (nhf : 0 ∉ f.support) : 0 < f.prod (· ^ ·) := Nat.pos_iff_ne_zero.mpr <| Finset.prod_ne_zero_iff.mpr fun _ hf => pow_ne_zero _ fun H => by subst H; exact nhf hf end Nat
Algebra\BigOperators\Intervals.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Order.Interval.Finset import Mathlib.Order.Interval.Finset.Nat import Mathlib.Tactic.Linarith /-! # Results about big operators over intervals We prove results about big operators over intervals. -/ open Nat variable {α M : Type*} namespace Finset section PartialOrder variable [PartialOrder α] [CommMonoid M] {f : α → M} {a b : α} section LocallyFiniteOrder variable [LocallyFiniteOrder α] @[to_additive] lemma mul_prod_Ico_eq_prod_Icc (h : a ≤ b) : f b * ∏ x ∈ Ico a b, f x = ∏ x ∈ Icc a b, f x := by rw [Icc_eq_cons_Ico h, prod_cons] @[to_additive] lemma prod_Ico_mul_eq_prod_Icc (h : a ≤ b) : (∏ x ∈ Ico a b, f x) * f b = ∏ x ∈ Icc a b, f x := by rw [mul_comm, mul_prod_Ico_eq_prod_Icc h] @[to_additive] lemma mul_prod_Ioc_eq_prod_Icc (h : a ≤ b) : f a * ∏ x ∈ Ioc a b, f x = ∏ x ∈ Icc a b, f x := by rw [Icc_eq_cons_Ioc h, prod_cons] @[to_additive] lemma prod_Ioc_mul_eq_prod_Icc (h : a ≤ b) : (∏ x ∈ Ioc a b, f x) * f a = ∏ x ∈ Icc a b, f x := by rw [mul_comm, mul_prod_Ioc_eq_prod_Icc h] end LocallyFiniteOrder section LocallyFiniteOrderTop variable [LocallyFiniteOrderTop α] @[to_additive] lemma mul_prod_Ioi_eq_prod_Ici (a : α) : f a * ∏ x ∈ Ioi a, f x = ∏ x ∈ Ici a, f x := by rw [Ici_eq_cons_Ioi, prod_cons] @[to_additive] lemma prod_Ioi_mul_eq_prod_Ici (a : α) : (∏ x ∈ Ioi a, f x) * f a = ∏ x ∈ Ici a, f x := by rw [mul_comm, mul_prod_Ioi_eq_prod_Ici] end LocallyFiniteOrderTop section LocallyFiniteOrderBot variable [LocallyFiniteOrderBot α] @[to_additive] lemma mul_prod_Iio_eq_prod_Iic (a : α) : f a * ∏ x ∈ Iio a, f x = ∏ x ∈ Iic a, f x := by rw [Iic_eq_cons_Iio, prod_cons] @[to_additive] lemma prod_Iio_mul_eq_prod_Iic (a : α) : (∏ x ∈ Iio a, f x) * f a = ∏ x ∈ Iic a, f x := by rw [mul_comm, mul_prod_Iio_eq_prod_Iic] end LocallyFiniteOrderBot end PartialOrder section LinearOrder variable [Fintype α] [LinearOrder α] [LocallyFiniteOrderTop α] [LocallyFiniteOrderBot α] [CommMonoid M] @[to_additive] lemma prod_prod_Ioi_mul_eq_prod_prod_off_diag (f : α → α → M) : ∏ i, ∏ j ∈ Ioi i, f j i * f i j = ∏ i, ∏ j ∈ {i}ᶜ, f j i := by simp_rw [← Ioi_disjUnion_Iio, prod_disjUnion, prod_mul_distrib] congr 1 rw [prod_sigma', prod_sigma'] refine prod_nbij' (fun i ↦ ⟨i.2, i.1⟩) (fun i ↦ ⟨i.2, i.1⟩) ?_ ?_ ?_ ?_ ?_ <;> simp end LinearOrder section Generic variable [CommMonoid M] {s₂ s₁ s : Finset α} {a : α} {g f : α → M} @[to_additive] theorem prod_Ico_add' [OrderedCancelAddCommMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] (f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (x + c)) = ∏ x ∈ Ico (a + c) (b + c), f x := by rw [← map_add_right_Ico, prod_map] rfl @[to_additive] theorem prod_Ico_add [OrderedCancelAddCommMonoid α] [ExistsAddOfLE α] [LocallyFiniteOrder α] (f : α → M) (a b c : α) : (∏ x ∈ Ico a b, f (c + x)) = ∏ x ∈ Ico (a + c) (b + c), f x := by convert prod_Ico_add' f a b c using 2 rw [add_comm] @[to_additive] theorem prod_Ico_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) : (∏ k ∈ Ico a (b + 1), f k) = (∏ k ∈ Ico a b, f k) * f b := by rw [Nat.Ico_succ_right_eq_insert_Ico hab, prod_insert right_not_mem_Ico, mul_comm] @[to_additive] theorem prod_eq_prod_Ico_succ_bot {a b : ℕ} (hab : a < b) (f : ℕ → M) : ∏ k ∈ Ico a b, f k = f a * ∏ k ∈ Ico (a + 1) b, f k := by have ha : a ∉ Ico (a + 1) b := by simp rw [← prod_insert ha, Nat.Ico_insert_succ_left hab] @[to_additive] theorem prod_Ico_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : ((∏ i ∈ Ico m n, f i) * ∏ i ∈ Ico n k, f i) = ∏ i ∈ Ico m k, f i := Ico_union_Ico_eq_Ico hmn hnk ▸ Eq.symm (prod_union (Ico_disjoint_Ico_consecutive m n k)) @[to_additive] theorem prod_Ioc_consecutive (f : ℕ → M) {m n k : ℕ} (hmn : m ≤ n) (hnk : n ≤ k) : ((∏ i ∈ Ioc m n, f i) * ∏ i ∈ Ioc n k, f i) = ∏ i ∈ Ioc m k, f i := by rw [← Ioc_union_Ioc_eq_Ioc hmn hnk, prod_union] apply disjoint_left.2 fun x hx h'x => _ intros x hx h'x exact lt_irrefl _ ((mem_Ioc.1 h'x).1.trans_le (mem_Ioc.1 hx).2) @[to_additive] theorem prod_Ioc_succ_top {a b : ℕ} (hab : a ≤ b) (f : ℕ → M) : (∏ k ∈ Ioc a (b + 1), f k) = (∏ k ∈ Ioc a b, f k) * f (b + 1) := by rw [← prod_Ioc_consecutive _ hab (Nat.le_succ b), Nat.Ioc_succ_singleton, prod_singleton] @[to_additive] theorem prod_Icc_succ_top {a b : ℕ} (hab : a ≤ b + 1) (f : ℕ → M) : (∏ k in Icc a (b + 1), f k) = (∏ k in Icc a b, f k) * f (b + 1) := by rw [← Nat.Ico_succ_right, prod_Ico_succ_top hab, Nat.Ico_succ_right] @[to_additive] theorem prod_range_mul_prod_Ico (f : ℕ → M) {m n : ℕ} (h : m ≤ n) : ((∏ k ∈ range m, f k) * ∏ k ∈ Ico m n, f k) = ∏ k ∈ range n, f k := Nat.Ico_zero_eq_range ▸ Nat.Ico_zero_eq_range ▸ prod_Ico_consecutive f m.zero_le h @[to_additive] theorem prod_Ico_eq_mul_inv {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : ∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) * (∏ k ∈ range m, f k)⁻¹ := eq_mul_inv_iff_mul_eq.2 <| by (rw [mul_comm]; exact prod_range_mul_prod_Ico f h) @[to_additive] theorem prod_Ico_eq_div {δ : Type*} [CommGroup δ] (f : ℕ → δ) {m n : ℕ} (h : m ≤ n) : ∏ k ∈ Ico m n, f k = (∏ k ∈ range n, f k) / ∏ k ∈ range m, f k := by simpa only [div_eq_mul_inv] using prod_Ico_eq_mul_inv f h @[to_additive] theorem prod_range_div_prod_range {α : Type*} [CommGroup α] {f : ℕ → α} {n m : ℕ} (hnm : n ≤ m) : ((∏ k ∈ range m, f k) / ∏ k ∈ range n, f k) = ∏ k ∈ (range m).filter fun k => n ≤ k, f k := by rw [← prod_Ico_eq_div f hnm] congr apply Finset.ext simp only [mem_Ico, mem_filter, mem_range, *] tauto /-- The two ways of summing over `(i, j)` in the range `a ≤ i ≤ j < b` are equal. -/ theorem sum_Ico_Ico_comm {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) : (∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico i b, f i j) = ∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a (j + 1), f i j := by rw [Finset.sum_sigma', Finset.sum_sigma'] refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) <;> simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;> rintro a b ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩ <;> omega /-- The two ways of summing over `(i, j)` in the range `a ≤ i < j < b` are equal. -/ theorem sum_Ico_Ico_comm' {M : Type*} [AddCommMonoid M] (a b : ℕ) (f : ℕ → ℕ → M) : (∑ i ∈ Finset.Ico a b, ∑ j ∈ Finset.Ico (i + 1) b, f i j) = ∑ j ∈ Finset.Ico a b, ∑ i ∈ Finset.Ico a j, f i j := by rw [Finset.sum_sigma', Finset.sum_sigma'] refine sum_nbij' (fun x ↦ ⟨x.2, x.1⟩) (fun x ↦ ⟨x.2, x.1⟩) ?_ ?_ (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl) <;> simp only [Finset.mem_Ico, Sigma.forall, Finset.mem_sigma] <;> rintro a b ⟨⟨h₁, h₂⟩, ⟨h₃, h₄⟩⟩ <;> omega @[to_additive] theorem prod_Ico_eq_prod_range (f : ℕ → M) (m n : ℕ) : ∏ k ∈ Ico m n, f k = ∏ k ∈ range (n - m), f (m + k) := by by_cases h : m ≤ n · rw [← Nat.Ico_zero_eq_range, prod_Ico_add, zero_add, tsub_add_cancel_of_le h] · replace h : n ≤ m := le_of_not_ge h rw [Ico_eq_empty_of_le h, tsub_eq_zero_iff_le.mpr h, range_zero, prod_empty, prod_empty] theorem prod_Ico_reflect (f : ℕ → M) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : (∏ j ∈ Ico k m, f (n - j)) = ∏ j ∈ Ico (n + 1 - m) (n + 1 - k), f j := by have : ∀ i < m, i ≤ n := by intro i hi exact (add_le_add_iff_right 1).1 (le_trans (Nat.lt_iff_add_one_le.1 hi) h) cases' lt_or_le k m with hkm hkm · rw [← Nat.Ico_image_const_sub_eq_Ico (this _ hkm)] refine (prod_image ?_).symm simp only [mem_Ico] rintro i ⟨_, im⟩ j ⟨_, jm⟩ Hij rw [← tsub_tsub_cancel_of_le (this _ im), Hij, tsub_tsub_cancel_of_le (this _ jm)] · have : n + 1 - k ≤ n + 1 - m := by rw [tsub_le_tsub_iff_left h] exact hkm simp only [hkm, Ico_eq_empty_of_le, prod_empty, tsub_le_iff_right, Ico_eq_empty_of_le this] theorem sum_Ico_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (k : ℕ) {m n : ℕ} (h : m ≤ n + 1) : (∑ j ∈ Ico k m, f (n - j)) = ∑ j ∈ Ico (n + 1 - m) (n + 1 - k), f j := @prod_Ico_reflect (Multiplicative δ) _ f k m n h theorem prod_range_reflect (f : ℕ → M) (n : ℕ) : (∏ j ∈ range n, f (n - 1 - j)) = ∏ j ∈ range n, f j := by cases n · simp · simp only [← Nat.Ico_zero_eq_range, Nat.succ_sub_succ_eq_sub, tsub_zero] rw [prod_Ico_reflect _ _ le_rfl] simp theorem sum_range_reflect {δ : Type*} [AddCommMonoid δ] (f : ℕ → δ) (n : ℕ) : (∑ j ∈ range n, f (n - 1 - j)) = ∑ j ∈ range n, f j := @prod_range_reflect (Multiplicative δ) _ f n @[simp] theorem prod_Ico_id_eq_factorial : ∀ n : ℕ, (∏ x ∈ Ico 1 (n + 1), x) = n ! | 0 => rfl | n + 1 => by rw [prod_Ico_succ_top <| Nat.succ_le_succ <| Nat.zero_le n, Nat.factorial_succ, prod_Ico_id_eq_factorial n, Nat.succ_eq_add_one, mul_comm] @[simp] theorem prod_range_add_one_eq_factorial : ∀ n : ℕ, (∏ x ∈ range n, (x + 1)) = n ! | 0 => rfl | n + 1 => by simp [factorial, Finset.range_succ, prod_range_add_one_eq_factorial n] section GaussSum /-- Gauss' summation formula -/ theorem sum_range_id_mul_two (n : ℕ) : (∑ i ∈ range n, i) * 2 = n * (n - 1) := calc (∑ i ∈ range n, i) * 2 = (∑ i ∈ range n, i) + ∑ i ∈ range n, (n - 1 - i) := by rw [sum_range_reflect (fun i => i) n, mul_two] _ = ∑ i ∈ range n, (i + (n - 1 - i)) := sum_add_distrib.symm _ = ∑ i ∈ range n, (n - 1) := sum_congr rfl fun i hi => add_tsub_cancel_of_le <| Nat.le_sub_one_of_lt <| mem_range.1 hi _ = n * (n - 1) := by rw [sum_const, card_range, Nat.nsmul_eq_mul] /-- Gauss' summation formula -/ theorem sum_range_id (n : ℕ) : ∑ i ∈ range n, i = n * (n - 1) / 2 := by rw [← sum_range_id_mul_two n, Nat.mul_div_cancel _ zero_lt_two] end GaussSum @[to_additive] lemma prod_range_diag_flip (n : ℕ) (f : ℕ → ℕ → M) : (∏ m ∈ range n, ∏ k ∈ range (m + 1), f k (m - k)) = ∏ m ∈ range n, ∏ k ∈ range (n - m), f m k := by rw [prod_sigma', prod_sigma'] refine prod_nbij' (fun a ↦ ⟨a.2, a.1 - a.2⟩) (fun a ↦ ⟨a.1 + a.2, a.1⟩) ?_ ?_ ?_ ?_ ?_ <;> simp (config := { contextual := true }) only [mem_sigma, mem_range, lt_tsub_iff_left, Nat.lt_succ_iff, le_add_iff_nonneg_right, Nat.zero_le, and_true, and_imp, imp_self, implies_true, Sigma.forall, forall_const, add_tsub_cancel_of_le, Sigma.mk.inj_iff, add_tsub_cancel_left, heq_eq_eq] exact fun a b han hba ↦ lt_of_le_of_lt hba han end Generic section Nat variable {M : Type*} variable (f g : ℕ → M) {m n : ℕ} section Group variable [CommGroup M] @[to_additive] theorem prod_range_succ_div_prod : ((∏ i ∈ range (n + 1), f i) / ∏ i ∈ range n, f i) = f n := div_eq_iff_eq_mul'.mpr <| prod_range_succ f n @[to_additive] theorem prod_range_succ_div_top : (∏ i ∈ range (n + 1), f i) / f n = ∏ i ∈ range n, f i := div_eq_iff_eq_mul.mpr <| prod_range_succ f n @[to_additive] theorem prod_Ico_div_bot (hmn : m < n) : (∏ i ∈ Ico m n, f i) / f m = ∏ i ∈ Ico (m + 1) n, f i := div_eq_iff_eq_mul'.mpr <| prod_eq_prod_Ico_succ_bot hmn _ @[to_additive] theorem prod_Ico_succ_div_top (hmn : m ≤ n) : (∏ i ∈ Ico m (n + 1), f i) / f n = ∏ i ∈ Ico m n, f i := div_eq_iff_eq_mul.mpr <| prod_Ico_succ_top hmn _ end Group end Nat end Finset
Algebra\BigOperators\Module.lean
/- Copyright (c) 2022 Dylan MacKenzie. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Dylan MacKenzie -/ import Mathlib.Algebra.BigOperators.Intervals import Mathlib.Algebra.Module.Defs import Mathlib.Tactic.Abel /-! # Summation by parts -/ namespace Finset variable {R M : Type*} [Ring R] [AddCommGroup M] [Module R M] (f : ℕ → R) (g : ℕ → M) {m n : ℕ} -- The partial sum of `g`, starting from zero local notation "G " n:80 => ∑ i ∈ range n, g i /-- **Summation by parts**, also known as **Abel's lemma** or an **Abel transformation** -/ theorem sum_Ico_by_parts (hmn : m < n) : ∑ i ∈ Ico m n, f i • g i = f (n - 1) • G n - f m • G m - ∑ i ∈ Ico m (n - 1), (f (i + 1) - f i) • G (i + 1) := by have h₁ : (∑ i ∈ Ico (m + 1) n, f i • G i) = ∑ i ∈ Ico m (n - 1), f (i + 1) • G (i + 1) := by rw [← Nat.sub_add_cancel (Nat.one_le_of_lt hmn), ← sum_Ico_add'] simp only [tsub_le_iff_right, add_le_iff_nonpos_left, nonpos_iff_eq_zero, tsub_eq_zero_iff_le, add_tsub_cancel_right] have h₂ : (∑ i ∈ Ico (m + 1) n, f i • G (i + 1)) = (∑ i ∈ Ico m (n - 1), f i • G (i + 1)) + f (n - 1) • G n - f m • G (m + 1) := by rw [← sum_Ico_sub_bot _ hmn, ← sum_Ico_succ_sub_top _ (Nat.le_sub_one_of_lt hmn), Nat.sub_add_cancel (pos_of_gt hmn), sub_add_cancel] rw [sum_eq_sum_Ico_succ_bot hmn] -- Porting note: the following used to be done with `conv` have h₃ : (Finset.sum (Ico (m + 1) n) fun i => f i • g i) = (Finset.sum (Ico (m + 1) n) fun i => f i • ((Finset.sum (Finset.range (i + 1)) g) - (Finset.sum (Finset.range i) g))) := by congr; funext; rw [← sum_range_succ_sub_sum g] rw [h₃] simp_rw [smul_sub, sum_sub_distrib, h₂, h₁] -- Porting note: the following used to be done with `conv` have h₄ : ((((Finset.sum (Ico m (n - 1)) fun i => f i • Finset.sum (range (i + 1)) fun i => g i) + f (n - 1) • Finset.sum (range n) fun i => g i) - f m • Finset.sum (range (m + 1)) fun i => g i) - Finset.sum (Ico m (n - 1)) fun i => f (i + 1) • Finset.sum (range (i + 1)) fun i => g i) = f (n - 1) • (range n).sum g - f m • (range (m + 1)).sum g + Finset.sum (Ico m (n - 1)) (fun i => f i • (range (i + 1)).sum g - f (i + 1) • (range (i + 1)).sum g) := by rw [← add_sub, add_comm, ← add_sub, ← sum_sub_distrib] rw [h₄] have : ∀ i, f i • G (i + 1) - f (i + 1) • G (i + 1) = -((f (i + 1) - f i) • G (i + 1)) := by intro i rw [sub_smul] abel simp_rw [this, sum_neg_distrib, sum_range_succ, smul_add] abel variable (n) /-- **Summation by parts** for ranges -/ theorem sum_range_by_parts : ∑ i ∈ range n, f i • g i = f (n - 1) • G n - ∑ i ∈ range (n - 1), (f (i + 1) - f i) • G (i + 1) := by by_cases hn : n = 0 · simp [hn] · rw [range_eq_Ico, sum_Ico_by_parts f g (Nat.pos_of_ne_zero hn), sum_range_zero, smul_zero, sub_zero, range_eq_Ico] end Finset
Algebra\BigOperators\NatAntidiagonal.lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Data.Finset.NatAntidiagonal /-! # Big operators for `NatAntidiagonal` This file contains theorems relevant to big operators over `Finset.NatAntidiagonal`. -/ variable {M N : Type*} [CommMonoid M] [AddCommMonoid N] namespace Finset namespace Nat theorem prod_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → M} : (∏ p ∈ antidiagonal (n + 1), f p) = f (0, n + 1) * ∏ p ∈ antidiagonal n, f (p.1 + 1, p.2) := by rw [antidiagonal_succ, prod_cons, prod_map]; rfl theorem sum_antidiagonal_succ {n : ℕ} {f : ℕ × ℕ → N} : (∑ p ∈ antidiagonal (n + 1), f p) = f (0, n + 1) + ∑ p ∈ antidiagonal n, f (p.1 + 1, p.2) := @prod_antidiagonal_succ (Multiplicative N) _ _ _ @[to_additive] theorem prod_antidiagonal_swap {n : ℕ} {f : ℕ × ℕ → M} : ∏ p ∈ antidiagonal n, f p.swap = ∏ p ∈ antidiagonal n, f p := by conv_lhs => rw [← map_swap_antidiagonal, Finset.prod_map] rfl theorem prod_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → M} : (∏ p ∈ antidiagonal (n + 1), f p) = f (n + 1, 0) * ∏ p ∈ antidiagonal n, f (p.1, p.2 + 1) := by rw [← prod_antidiagonal_swap, prod_antidiagonal_succ, ← prod_antidiagonal_swap] rfl theorem sum_antidiagonal_succ' {n : ℕ} {f : ℕ × ℕ → N} : (∑ p ∈ antidiagonal (n + 1), f p) = f (n + 1, 0) + ∑ p ∈ antidiagonal n, f (p.1, p.2 + 1) := @prod_antidiagonal_succ' (Multiplicative N) _ _ _ @[to_additive] theorem prod_antidiagonal_subst {n : ℕ} {f : ℕ × ℕ → ℕ → M} : ∏ p ∈ antidiagonal n, f p n = ∏ p ∈ antidiagonal n, f p (p.1 + p.2) := prod_congr rfl fun p hp ↦ by rw [mem_antidiagonal.mp hp] @[to_additive] theorem prod_antidiagonal_eq_prod_range_succ_mk {M : Type*} [CommMonoid M] (f : ℕ × ℕ → M) (n : ℕ) : ∏ ij ∈ antidiagonal n, f ij = ∏ k ∈ range n.succ, f (k, n - k) := Finset.prod_map (range n.succ) ⟨fun i ↦ (i, n - i), fun _ _ h ↦ (Prod.mk.inj h).1⟩ f /-- This lemma matches more generally than `Finset.Nat.prod_antidiagonal_eq_prod_range_succ_mk` when using `rw ← `. -/ @[to_additive "This lemma matches more generally than `Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk` when using `rw ← `."] theorem prod_antidiagonal_eq_prod_range_succ {M : Type*} [CommMonoid M] (f : ℕ → ℕ → M) (n : ℕ) : ∏ ij ∈ antidiagonal n, f ij.1 ij.2 = ∏ k ∈ range n.succ, f k (n - k) := prod_antidiagonal_eq_prod_range_succ_mk _ _ end Nat end Finset
Algebra\BigOperators\Option.lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Data.Finset.Option /-! # Lemmas about products and sums over finite sets in `Option α` In this file we prove formulas for products and sums over `Finset.insertNone s` and `Finset.eraseNone s`. -/ open Function namespace Finset variable {α M : Type*} [CommMonoid M] @[to_additive (attr := simp)] theorem prod_insertNone (f : Option α → M) (s : Finset α) : ∏ x ∈ insertNone s, f x = f none * ∏ x ∈ s, f (some x) := by simp [insertNone] @[to_additive] theorem mul_prod_eq_prod_insertNone (f : α → M) (x : M) (s : Finset α) : x * ∏ i ∈ s, f i = ∏ i ∈ insertNone s, i.elim x f := (prod_insertNone (fun i => i.elim x f) _).symm @[to_additive] theorem prod_eraseNone (f : α → M) (s : Finset (Option α)) : ∏ x ∈ eraseNone s, f x = ∏ x ∈ s, Option.elim' 1 f x := by classical calc ∏ x ∈ eraseNone s, f x = ∏ x ∈ (eraseNone s).map Embedding.some, Option.elim' 1 f x := (prod_map (eraseNone s) Embedding.some <| Option.elim' 1 f).symm _ = ∏ x ∈ s.erase none, Option.elim' 1 f x := by rw [map_some_eraseNone] _ = ∏ x ∈ s, Option.elim' 1 f x := prod_erase _ rfl end Finset
Algebra\BigOperators\Pi.lean
/- Copyright (c) 2018 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Patrick Massot -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Group.Action.Pi import Mathlib.Algebra.Ring.Pi /-! # Big operators for Pi Types This file contains theorems relevant to big operators in binary and arbitrary product of monoids and groups -/ namespace Pi @[to_additive] theorem list_prod_apply {α : Type*} {β : α → Type*} [∀ a, Monoid (β a)] (a : α) (l : List (∀ a, β a)) : l.prod a = (l.map fun f : ∀ a, β a ↦ f a).prod := map_list_prod (evalMonoidHom β a) _ @[to_additive] theorem multiset_prod_apply {α : Type*} {β : α → Type*} [∀ a, CommMonoid (β a)] (a : α) (s : Multiset (∀ a, β a)) : s.prod a = (s.map fun f : ∀ a, β a ↦ f a).prod := (evalMonoidHom β a).map_multiset_prod _ end Pi @[to_additive (attr := simp)] theorem Finset.prod_apply {α : Type*} {β : α → Type*} {γ} [∀ a, CommMonoid (β a)] (a : α) (s : Finset γ) (g : γ → ∀ a, β a) : (∏ c ∈ s, g c) a = ∏ c ∈ s, g c a := map_prod (Pi.evalMonoidHom β a) _ _ /-- An 'unapplied' analogue of `Finset.prod_apply`. -/ @[to_additive "An 'unapplied' analogue of `Finset.sum_apply`."] theorem Finset.prod_fn {α : Type*} {β : α → Type*} {γ} [∀ a, CommMonoid (β a)] (s : Finset γ) (g : γ → ∀ a, β a) : ∏ c ∈ s, g c = fun a ↦ ∏ c ∈ s, g c a := funext fun _ ↦ Finset.prod_apply _ _ _ @[to_additive] theorem Fintype.prod_apply {α : Type*} {β : α → Type*} {γ : Type*} [Fintype γ] [∀ a, CommMonoid (β a)] (a : α) (g : γ → ∀ a, β a) : (∏ c, g c) a = ∏ c, g c a := Finset.prod_apply a Finset.univ g @[to_additive prod_mk_sum] theorem prod_mk_prod {α β γ : Type*} [CommMonoid α] [CommMonoid β] (s : Finset γ) (f : γ → α) (g : γ → β) : (∏ x ∈ s, f x, ∏ x ∈ s, g x) = ∏ x ∈ s, (f x, g x) := haveI := Classical.decEq γ Finset.induction_on s rfl (by simp (config := { contextual := true }) [Prod.ext_iff]) /-- decomposing `x : ι → R` as a sum along the canonical basis -/ theorem pi_eq_sum_univ {ι : Type*} [Fintype ι] [DecidableEq ι] {R : Type*} [Semiring R] (x : ι → R) : x = ∑ i, (x i) • fun j => if i = j then (1 : R) else 0 := by ext simp section MulSingle variable {I : Type*} [DecidableEq I] {Z : I → Type*} variable [∀ i, CommMonoid (Z i)] @[to_additive] theorem Finset.univ_prod_mulSingle [Fintype I] (f : ∀ i, Z i) : (∏ i, Pi.mulSingle i (f i)) = f := by ext a simp @[to_additive] theorem MonoidHom.functions_ext [Finite I] (G : Type*) [CommMonoid G] (g h : (∀ i, Z i) →* G) (H : ∀ i x, g (Pi.mulSingle i x) = h (Pi.mulSingle i x)) : g = h := by cases nonempty_fintype I ext k rw [← Finset.univ_prod_mulSingle k, map_prod, map_prod] simp only [H] /-- This is used as the ext lemma instead of `MonoidHom.functions_ext` for reasons explained in note [partially-applied ext lemmas]. -/ @[to_additive (attr := ext) "\nThis is used as the ext lemma instead of `AddMonoidHom.functions_ext` for reasons explained in note [partially-applied ext lemmas]."] theorem MonoidHom.functions_ext' [Finite I] (M : Type*) [CommMonoid M] (g h : (∀ i, Z i) →* M) (H : ∀ i, g.comp (MonoidHom.mulSingle Z i) = h.comp (MonoidHom.mulSingle Z i)) : g = h := g.functions_ext M h fun i => DFunLike.congr_fun (H i) end MulSingle section RingHom open Pi variable {I : Type*} [DecidableEq I] {f : I → Type*} variable [∀ i, NonAssocSemiring (f i)] @[ext] theorem RingHom.functions_ext [Finite I] (G : Type*) [NonAssocSemiring G] (g h : (∀ i, f i) →+* G) (H : ∀ (i : I) (x : f i), g (single i x) = h (single i x)) : g = h := RingHom.coe_addMonoidHom_injective <| @AddMonoidHom.functions_ext I _ f _ _ G _ (g : (∀ i, f i) →+ G) h H end RingHom namespace Prod variable {α β γ : Type*} [CommMonoid α] [CommMonoid β] {s : Finset γ} {f : γ → α × β} @[to_additive] theorem fst_prod : (∏ c ∈ s, f c).1 = ∏ c ∈ s, (f c).1 := map_prod (MonoidHom.fst α β) f s @[to_additive] theorem snd_prod : (∏ c ∈ s, f c).2 = ∏ c ∈ s, (f c).2 := map_prod (MonoidHom.snd α β) f s end Prod
Algebra\BigOperators\Ring.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Algebra.BigOperators.GroupWithZero.Finset import Mathlib.Algebra.BigOperators.Ring.Multiset import Mathlib.Algebra.Field.Defs import Mathlib.Data.Fintype.Powerset import Mathlib.Data.Int.Cast.Lemmas /-! # Results about big operators with values in a (semi)ring We prove results about big operators that involve some interaction between multiplicative and additive structures on the values being combined. -/ open Fintype variable {ι ι' α β γ : Type*} {κ : ι → Type*} {s s₁ s₂ : Finset ι} {i : ι} {a : α} {f g : ι → α} namespace Finset section AddCommMonoidWithOne variable [AddCommMonoidWithOne α] lemma natCast_card_filter (p) [DecidablePred p] (s : Finset ι) : ((filter p s).card : α) = ∑ a ∈ s, if p a then (1 : α) else 0 := by rw [sum_ite, sum_const_zero, add_zero, sum_const, nsmul_one] @[simp] lemma sum_boole (p) [DecidablePred p] (s : Finset ι) : (∑ x ∈ s, if p x then 1 else 0 : α) = (s.filter p).card := (natCast_card_filter _ _).symm end AddCommMonoidWithOne section NonUnitalNonAssocSemiring variable [NonUnitalNonAssocSemiring α] lemma sum_mul (s : Finset ι) (f : ι → α) (a : α) : (∑ i ∈ s, f i) * a = ∑ i ∈ s, f i * a := map_sum (AddMonoidHom.mulRight a) _ s lemma mul_sum (s : Finset ι) (f : ι → α) (a : α) : a * ∑ i ∈ s, f i = ∑ i ∈ s, a * f i := map_sum (AddMonoidHom.mulLeft a) _ s lemma sum_mul_sum {κ : Type*} (s : Finset ι) (t : Finset κ) (f : ι → α) (g : κ → α) : (∑ i ∈ s, f i) * ∑ j ∈ t, g j = ∑ i ∈ s, ∑ j ∈ t, f i * g j := by simp_rw [sum_mul, ← mul_sum] lemma _root_.Fintype.sum_mul_sum {κ : Type*} [Fintype ι] [Fintype κ] (f : ι → α) (g : κ → α) : (∑ i, f i) * ∑ j, g j = ∑ i, ∑ j, f i * g j := Finset.sum_mul_sum _ _ _ _ lemma _root_.Commute.sum_right (s : Finset ι) (f : ι → α) (b : α) (h : ∀ i ∈ s, Commute b (f i)) : Commute b (∑ i ∈ s, f i) := (Commute.multiset_sum_right _ _) fun b hb => by obtain ⟨i, hi, rfl⟩ := Multiset.mem_map.mp hb exact h _ hi lemma _root_.Commute.sum_left (s : Finset ι) (f : ι → α) (b : α) (h : ∀ i ∈ s, Commute (f i) b) : Commute (∑ i ∈ s, f i) b := ((Commute.sum_right _ _ _) fun _i hi => (h _ hi).symm).symm lemma sum_range_succ_mul_sum_range_succ (m n : ℕ) (f g : ℕ → α) : (∑ i ∈ range (m + 1), f i) * ∑ i ∈ range (n + 1), g i = (∑ i ∈ range m, f i) * ∑ i ∈ range n, g i + f m * ∑ i ∈ range n, g i + (∑ i ∈ range m, f i) * g n + f m * g n := by simp only [add_mul, mul_add, add_assoc, sum_range_succ] end NonUnitalNonAssocSemiring section NonUnitalSemiring variable [NonUnitalSemiring α] lemma dvd_sum (h : ∀ i ∈ s, a ∣ f i) : a ∣ ∑ i ∈ s, f i := Multiset.dvd_sum fun y hy => by rcases Multiset.mem_map.1 hy with ⟨x, hx, rfl⟩; exact h x hx end NonUnitalSemiring section NonAssocSemiring variable [NonAssocSemiring α] [DecidableEq ι] lemma sum_mul_boole (s : Finset ι) (f : ι → α) (i : ι) : ∑ j ∈ s, f j * ite (i = j) 1 0 = ite (i ∈ s) (f i) 0 := by simp lemma sum_boole_mul (s : Finset ι) (f : ι → α) (i : ι) : ∑ j ∈ s, ite (i = j) 1 0 * f j = ite (i ∈ s) (f i) 0 := by simp end NonAssocSemiring section CommSemiring variable [CommSemiring α] /-- If `f = g = h` everywhere but at `i`, where `f i = g i + h i`, then the product of `f` over `s` is the sum of the products of `g` and `h`. -/ theorem prod_add_prod_eq {s : Finset ι} {i : ι} {f g h : ι → α} (hi : i ∈ s) (h1 : g i + h i = f i) (h2 : ∀ j ∈ s, j ≠ i → g j = f j) (h3 : ∀ j ∈ s, j ≠ i → h j = f j) : (∏ i ∈ s, g i) + ∏ i ∈ s, h i = ∏ i ∈ s, f i := by classical simp_rw [prod_eq_mul_prod_diff_singleton hi, ← h1, right_distrib] congr 2 <;> apply prod_congr rfl <;> simpa section DecidableEq variable [DecidableEq ι] /-- The product over a sum can be written as a sum over the product of sets, `Finset.Pi`. `Finset.prod_univ_sum` is an alternative statement when the product is over `univ`. -/ lemma prod_sum (s : Finset ι) (t : ∀ i, Finset (κ i)) (f : ∀ i, κ i → α) : ∏ a ∈ s, ∑ b ∈ t a, f a b = ∑ p ∈ s.pi t, ∏ x ∈ s.attach, f x.1 (p x.1 x.2) := by classical induction' s using Finset.induction with a s ha ih · rw [pi_empty, sum_singleton] rfl · have h₁ : ∀ x ∈ t a, ∀ y ∈ t a, x ≠ y → Disjoint (image (Pi.cons s a x) (pi s t)) (image (Pi.cons s a y) (pi s t)) := by intro x _ y _ h simp only [disjoint_iff_ne, mem_image] rintro _ ⟨p₂, _, eq₂⟩ _ ⟨p₃, _, eq₃⟩ eq have : Pi.cons s a x p₂ a (mem_insert_self _ _) = Pi.cons s a y p₃ a (mem_insert_self _ _) := by rw [eq₂, eq₃, eq] rw [Pi.cons_same, Pi.cons_same] at this exact h this rw [prod_insert ha, pi_insert ha, ih, sum_mul, sum_biUnion h₁] refine sum_congr rfl fun b _ => ?_ have h₂ : ∀ p₁ ∈ pi s t, ∀ p₂ ∈ pi s t, Pi.cons s a b p₁ = Pi.cons s a b p₂ → p₁ = p₂ := fun p₁ _ p₂ _ eq => Pi.cons_injective ha eq rw [sum_image h₂, mul_sum] refine sum_congr rfl fun g _ => ?_ rw [attach_insert, prod_insert, prod_image] · simp only [Pi.cons_same] congr with ⟨v, hv⟩ congr exact (Pi.cons_ne (by rintro rfl; exact ha hv)).symm · exact fun _ _ _ _ => Subtype.eq ∘ Subtype.mk.inj · simpa only [mem_image, mem_attach, Subtype.mk.injEq, true_and, Subtype.exists, exists_prop, exists_eq_right] using ha /-- The product over `univ` of a sum can be written as a sum over the product of sets, `Fintype.piFinset`. `Finset.prod_sum` is an alternative statement when the product is not over `univ`. -/ lemma prod_univ_sum [Fintype ι] (t : ∀ i, Finset (κ i)) (f : ∀ i, κ i → α) : ∏ i, ∑ j ∈ t i, f i j = ∑ x ∈ piFinset t, ∏ i, f i (x i) := by simp only [prod_attach_univ, prod_sum, Finset.sum_univ_pi] lemma sum_prod_piFinset {κ : Type*} [Fintype ι] (s : Finset κ) (g : ι → κ → α) : ∑ f ∈ piFinset fun _ : ι ↦ s, ∏ i, g i (f i) = ∏ i, ∑ j ∈ s, g i j := by rw [← prod_univ_sum] lemma sum_pow' (s : Finset ι') (f : ι' → α) (n : ℕ) : (∑ a ∈ s, f a) ^ n = ∑ p ∈ piFinset fun _i : Fin n ↦ s, ∏ i, f (p i) := by convert @prod_univ_sum (Fin n) _ _ _ _ _ (fun _i ↦ s) fun _i d ↦ f d; simp /-- The product of `f a + g a` over all of `s` is the sum over the powerset of `s` of the product of `f` over a subset `t` times the product of `g` over the complement of `t` -/ theorem prod_add (f g : ι → α) (s : Finset ι) : ∏ i ∈ s, (f i + g i) = ∑ t ∈ s.powerset, (∏ i ∈ t, f i) * ∏ i ∈ s \ t, g i := by classical calc ∏ i ∈ s, (f i + g i) = ∏ i ∈ s, ∑ p ∈ ({True, False} : Finset Prop), if p then f i else g i := by simp _ = ∑ p ∈ (s.pi fun _ => {True, False} : Finset (∀ a ∈ s, Prop)), ∏ a ∈ s.attach, if p a.1 a.2 then f a.1 else g a.1 := prod_sum _ _ _ _ = ∑ t ∈ s.powerset, (∏ a ∈ t, f a) * ∏ a ∈ s \ t, g a := sum_bij' (fun f _ ↦ s.filter fun a ↦ ∃ h : a ∈ s, f a h) (fun t _ a _ => a ∈ t) (by simp) (by simp [Classical.em]) (by simp_rw [mem_filter, Function.funext_iff, eq_iff_iff, mem_pi, mem_insert]; tauto) (by simp_rw [ext_iff, @mem_filter _ _ (id _), mem_powerset]; tauto) (fun a _ ↦ by simp only [prod_ite, filter_attach', prod_map, Function.Embedding.coeFn_mk, Subtype.map_coe, id_eq, prod_attach, filter_congr_decidable] congr 2 with x simp only [mem_filter, mem_sdiff, not_and, not_exists, and_congr_right_iff] tauto) end DecidableEq /-- `∏ i, (f i + g i) = (∏ i, f i) + ∑ i, g i * (∏ j < i, f j + g j) * (∏ j > i, f j)`. -/ theorem prod_add_ordered [LinearOrder ι] (s : Finset ι) (f g : ι → α) : ∏ i ∈ s, (f i + g i) = (∏ i ∈ s, f i) + ∑ i ∈ s, g i * (∏ j ∈ s.filter (· < i), (f j + g j)) * ∏ j ∈ s.filter fun j => i < j, f j := by refine Finset.induction_on_max s (by simp) ?_ clear s intro a s ha ihs have ha' : a ∉ s := fun ha' => lt_irrefl a (ha a ha') rw [prod_insert ha', prod_insert ha', sum_insert ha', filter_insert, if_neg (lt_irrefl a), filter_true_of_mem ha, ihs, add_mul, mul_add, mul_add, add_assoc] congr 1 rw [add_comm] congr 1 · rw [filter_false_of_mem, prod_empty, mul_one] exact (forall_mem_insert _ _ _).2 ⟨lt_irrefl a, fun i hi => (ha i hi).not_lt⟩ · rw [mul_sum] refine sum_congr rfl fun i hi => ?_ rw [filter_insert, if_neg (ha i hi).not_lt, filter_insert, if_pos (ha i hi), prod_insert, mul_left_comm] exact mt (fun ha => (mem_filter.1 ha).1) ha' /-- Summing `a^s.card * b^(n-s.card)` over all finite subsets `s` of a `Finset` gives `(a + b)^s.card`. -/ theorem sum_pow_mul_eq_add_pow (a b : α) (s : Finset ι) : (∑ t ∈ s.powerset, a ^ t.card * b ^ (s.card - t.card)) = (a + b) ^ s.card := by classical rw [← prod_const, prod_add] refine Finset.sum_congr rfl fun t ht => ?_ rw [prod_const, prod_const, ← card_sdiff (mem_powerset.1 ht)] /-- Summing `a^s.card * b^(n-s.card)` over all finite subsets `s` of a fintype of cardinality `n` gives `(a + b)^n`. The "good" proof involves expanding along all coordinates using the fact that `x^n` is multilinear, but multilinear maps are only available now over rings, so we give instead a proof reducing to the usual binomial theorem to have a result over semirings. -/ lemma _root_.Fintype.sum_pow_mul_eq_add_pow (ι : Type*) [Fintype ι] (a b : α) : ∑ s : Finset ι, a ^ s.card * b ^ (Fintype.card ι - s.card) = (a + b) ^ Fintype.card ι := Finset.sum_pow_mul_eq_add_pow _ _ _ @[norm_cast] theorem prod_natCast (s : Finset ι) (f : ι → ℕ) : ↑(∏ i ∈ s, f i : ℕ) = ∏ i ∈ s, (f i : α) := map_prod (Nat.castRingHom α) f s end CommSemiring section CommRing variable [CommRing α] /-- `∏ i, (f i - g i) = (∏ i, f i) - ∑ i, g i * (∏ j < i, f j - g j) * (∏ j > i, f j)`. -/ lemma prod_sub_ordered [LinearOrder ι] (s : Finset ι) (f g : ι → α) : ∏ i ∈ s, (f i - g i) = (∏ i ∈ s, f i) - ∑ i ∈ s, g i * (∏ j ∈ s.filter (· < i), (f j - g j)) * ∏ j ∈ s.filter fun j => i < j, f j := by simp only [sub_eq_add_neg] convert prod_add_ordered s f fun i => -g i simp /-- `∏ i, (1 - f i) = 1 - ∑ i, f i * (∏ j < i, 1 - f j)`. This formula is useful in construction of a partition of unity from a collection of “bump” functions. -/ theorem prod_one_sub_ordered [LinearOrder ι] (s : Finset ι) (f : ι → α) : ∏ i ∈ s, (1 - f i) = 1 - ∑ i ∈ s, f i * ∏ j ∈ s.filter (· < i), (1 - f j) := by rw [prod_sub_ordered] simp theorem prod_range_natCast_sub (n k : ℕ) : ∏ i ∈ range k, (n - i : α) = (∏ i ∈ range k, (n - i) : ℕ) := by rw [prod_natCast] rcases le_or_lt k n with hkn | hnk · exact prod_congr rfl fun i hi => (Nat.cast_sub <| (mem_range.1 hi).le.trans hkn).symm · rw [← mem_range] at hnk rw [prod_eq_zero hnk, prod_eq_zero hnk] <;> simp @[deprecated (since := "2024-05-27")] alias prod_range_cast_nat_sub := prod_range_natCast_sub end CommRing section DivisionSemiring variable [DivisionSemiring α] lemma sum_div (s : Finset ι) (f : ι → α) (a : α) : (∑ i ∈ s, f i) / a = ∑ i ∈ s, f i / a := by simp only [div_eq_mul_inv, sum_mul] end DivisionSemiring end Finset open Finset namespace Fintype variable {ι κ α : Type*} [Fintype ι] [Fintype κ] [CommSemiring α] lemma sum_pow (f : ι → α) (n : ℕ) : (∑ a, f a) ^ n = ∑ p : Fin n → ι, ∏ i, f (p i) := by simp [sum_pow'] variable [DecidableEq ι] /-- A product of sums can be written as a sum of products. -/ lemma prod_sum {κ : ι → Type*} [∀ i, Fintype (κ i)] (f : ∀ i, κ i → α) : ∏ i, ∑ j, f i j = ∑ x : ∀ i, κ i, ∏ i, f i (x i) := Finset.prod_univ_sum _ _ lemma prod_add (f g : ι → α) : ∏ a, (f a + g a) = ∑ t, (∏ a ∈ t, f a) * ∏ a ∈ tᶜ, g a := by simpa [compl_eq_univ_sdiff] using Finset.prod_add f g univ end Fintype namespace Nat variable {ι : Type*} {s : Finset ι} {f : ι → ℕ} {n : ℕ} protected lemma sum_div (hf : ∀ i ∈ s, n ∣ f i) : (∑ i ∈ s, f i) / n = ∑ i ∈ s, f i / n := by obtain rfl | hn := n.eq_zero_or_pos · simp rw [Nat.div_eq_iff_eq_mul_left hn (dvd_sum hf), sum_mul] refine sum_congr rfl fun s hs ↦ ?_ rw [Nat.div_mul_cancel (hf _ hs)] @[simp, norm_cast] lemma cast_list_sum [AddMonoidWithOne β] (s : List ℕ) : (↑s.sum : β) = (s.map (↑)).sum := map_list_sum (castAddMonoidHom β) _ @[simp, norm_cast] lemma cast_list_prod [Semiring β] (s : List ℕ) : (↑s.prod : β) = (s.map (↑)).prod := map_list_prod (castRingHom β) _ @[simp, norm_cast] lemma cast_multiset_sum [AddCommMonoidWithOne β] (s : Multiset ℕ) : (↑s.sum : β) = (s.map (↑)).sum := map_multiset_sum (castAddMonoidHom β) _ @[simp, norm_cast] lemma cast_multiset_prod [CommSemiring β] (s : Multiset ℕ) : (↑s.prod : β) = (s.map (↑)).prod := map_multiset_prod (castRingHom β) _ @[simp, norm_cast] lemma cast_sum [AddCommMonoidWithOne β] (s : Finset α) (f : α → ℕ) : ↑(∑ x ∈ s, f x : ℕ) = ∑ x ∈ s, (f x : β) := map_sum (castAddMonoidHom β) _ _ @[simp, norm_cast] lemma cast_prod [CommSemiring β] (f : α → ℕ) (s : Finset α) : (↑(∏ i ∈ s, f i) : β) = ∏ i ∈ s, (f i : β) := map_prod (castRingHom β) _ _ end Nat namespace Int variable {ι : Type*} {s : Finset ι} {f : ι → ℤ} {n : ℤ} protected lemma sum_div (hf : ∀ i ∈ s, n ∣ f i) : (∑ i ∈ s, f i) / n = ∑ i ∈ s, f i / n := by obtain rfl | hn := eq_or_ne n 0 · simp rw [Int.ediv_eq_iff_eq_mul_left hn (dvd_sum hf), sum_mul] refine sum_congr rfl fun s hs ↦ ?_ rw [Int.ediv_mul_cancel (hf _ hs)] @[simp, norm_cast] lemma cast_list_sum [AddGroupWithOne β] (s : List ℤ) : (↑s.sum : β) = (s.map (↑)).sum := map_list_sum (castAddHom β) _ @[simp, norm_cast] lemma cast_list_prod [Ring β] (s : List ℤ) : (↑s.prod : β) = (s.map (↑)).prod := map_list_prod (castRingHom β) _ @[simp, norm_cast] lemma cast_multiset_sum [AddCommGroupWithOne β] (s : Multiset ℤ) : (↑s.sum : β) = (s.map (↑)).sum := map_multiset_sum (castAddHom β) _ @[simp, norm_cast] lemma cast_multiset_prod {R : Type*} [CommRing R] (s : Multiset ℤ) : (↑s.prod : R) = (s.map (↑)).prod := map_multiset_prod (castRingHom R) _ @[simp, norm_cast] lemma cast_sum [AddCommGroupWithOne β] (s : Finset α) (f : α → ℤ) : ↑(∑ x ∈ s, f x : ℤ) = ∑ x ∈ s, (f x : β) := map_sum (castAddHom β) _ _ @[simp, norm_cast] lemma cast_prod {R : Type*} [CommRing R] (f : α → ℤ) (s : Finset α) : (↑(∏ i ∈ s, f i) : R) = ∏ i ∈ s, (f i : R) := map_prod (Int.castRingHom R) _ _ end Int
Algebra\BigOperators\RingEquiv.lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Callum Sutton, Yury Kudryashov -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Ring.Equiv import Mathlib.Algebra.Ring.Opposite /-! # Results about mapping big operators across ring equivalences -/ namespace RingEquiv variable {α R S : Type*} protected theorem map_list_prod [Semiring R] [Semiring S] (f : R ≃+* S) (l : List R) : f l.prod = (l.map f).prod := map_list_prod f l protected theorem map_list_sum [NonAssocSemiring R] [NonAssocSemiring S] (f : R ≃+* S) (l : List R) : f l.sum = (l.map f).sum := map_list_sum f l /-- An isomorphism into the opposite ring acts on the product by acting on the reversed elements -/ protected theorem unop_map_list_prod [Semiring R] [Semiring S] (f : R ≃+* Sᵐᵒᵖ) (l : List R) : MulOpposite.unop (f l.prod) = (l.map (MulOpposite.unop ∘ f)).reverse.prod := unop_map_list_prod f l protected theorem map_multiset_prod [CommSemiring R] [CommSemiring S] (f : R ≃+* S) (s : Multiset R) : f s.prod = (s.map f).prod := map_multiset_prod f s protected theorem map_multiset_sum [NonAssocSemiring R] [NonAssocSemiring S] (f : R ≃+* S) (s : Multiset R) : f s.sum = (s.map f).sum := map_multiset_sum f s protected theorem map_prod [CommSemiring R] [CommSemiring S] (g : R ≃+* S) (f : α → R) (s : Finset α) : g (∏ x ∈ s, f x) = ∏ x ∈ s, g (f x) := map_prod g f s protected theorem map_sum [NonAssocSemiring R] [NonAssocSemiring S] (g : R ≃+* S) (f : α → R) (s : Finset α) : g (∑ x ∈ s, f x) = ∑ x ∈ s, g (f x) := map_sum g f s end RingEquiv
Algebra\BigOperators\WithTop.lean
/- Copyright (c) 2024 Zhouhang Zhou. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Zhouhang Zhou, Yaël Dillies -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Order.Ring.WithTop /-! # Sums in `WithTop` This file proves results about finite sums over monoids extended by a bottom or top element. -/ open Finset variable {ι α : Type*} namespace WithTop section AddCommMonoid variable [AddCommMonoid α] {s : Finset ι} {f : ι → WithTop α} @[simp, norm_cast] lemma coe_sum (s : Finset ι) (f : ι → α) : ∑ i ∈ s, f i = ∑ i ∈ s, (f i : WithTop α) := map_sum addHom f s /-- A sum is infinite iff one term is infinite. -/ lemma sum_eq_top_iff : ∑ i ∈ s, f i = ⊤ ↔ ∃ i ∈ s, f i = ⊤ := by induction s using Finset.cons_induction <;> simp [*] variable [LT α] /-- A sum is finite iff all terms are finite. -/ lemma sum_lt_top_iff : ∑ i ∈ s, f i < ⊤ ↔ ∀ i ∈ s, f i < ⊤ := by simp only [WithTop.lt_top_iff_ne_top, ne_eq, sum_eq_top_iff, not_exists, not_and] /-- A sum of finite terms is finite. -/ lemma sum_lt_top (h : ∀ i ∈ s, f i ≠ ⊤) : ∑ i ∈ s, f i < ⊤ := sum_lt_top_iff.2 fun i hi ↦ WithTop.lt_top_iff_ne_top.2 (h i hi) end AddCommMonoid /-- A product of finite terms is finite. -/ lemma prod_lt_top [CommMonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] [DecidableEq α] [LT α] {s : Finset ι} {f : ι → WithTop α} (h : ∀ i ∈ s, f i ≠ ⊤) : ∏ i ∈ s, f i < ⊤ := prod_induction f (· < ⊤) (fun _ _ h₁ h₂ ↦ mul_lt_top' h₁ h₂) (coe_lt_top 1) fun a ha ↦ WithTop.lt_top_iff_ne_top.2 (h a ha) end WithTop namespace WithBot section AddCommMonoid variable [AddCommMonoid α] {s : Finset ι} {f : ι → WithBot α} @[simp, norm_cast] lemma coe_sum (s : Finset ι) (f : ι → α) : ∑ i ∈ s, f i = ∑ i ∈ s, (f i : WithBot α) := map_sum addHom f s /-- A sum is infinite iff one term is infinite. -/ lemma sum_eq_bot_iff : ∑ i ∈ s, f i = ⊥ ↔ ∃ i ∈ s, f i = ⊥ := by induction s using Finset.cons_induction <;> simp [*] variable [LT α] /-- A sum is finite iff all terms are finite. -/ lemma bot_lt_sum_iff : ⊥ < ∑ i ∈ s, f i ↔ ∀ i ∈ s, ⊥ < f i := by simp only [WithBot.bot_lt_iff_ne_bot, ne_eq, sum_eq_bot_iff, not_exists, not_and] /-- A sum of finite terms is finite. -/ lemma sum_lt_bot (h : ∀ i ∈ s, f i ≠ ⊥) : ⊥ < ∑ i ∈ s, f i := bot_lt_sum_iff.2 fun i hi ↦ WithBot.bot_lt_iff_ne_bot.2 (h i hi) end AddCommMonoid /-- A product of finite terms is finite. -/ lemma prod_lt_bot [CommMonoidWithZero α] [NoZeroDivisors α] [Nontrivial α] [DecidableEq α] [LT α] {s : Finset ι} {f : ι → WithBot α} (h : ∀ i ∈ s, f i ≠ ⊥) : ⊥ < ∏ i ∈ s, f i := prod_induction f (⊥ < ·) (fun _ _ h₁ h₂ ↦ bot_lt_mul' h₁ h₂) (bot_lt_coe 1) fun a ha ↦ WithBot.bot_lt_iff_ne_bot.2 (h a ha) end WithBot
Algebra\BigOperators\Group\Finset.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Algebra.Group.Indicator import Mathlib.Data.Finset.Piecewise import Mathlib.Data.Finset.Preimage import Mathlib.Data.Finset.Sym import Mathlib.Data.Sym.Sym2.Order /-! # Big operators In this file we define products and sums indexed by finite sets (specifically, `Finset`). ## Notation We introduce the following notation. Let `s` be a `Finset α`, and `f : α → β` a function. * `∏ x ∈ s, f x` is notation for `Finset.prod s f` (assuming `β` is a `CommMonoid`) * `∑ x ∈ s, f x` is notation for `Finset.sum s f` (assuming `β` is an `AddCommMonoid`) * `∏ x, f x` is notation for `Finset.prod Finset.univ f` (assuming `α` is a `Fintype` and `β` is a `CommMonoid`) * `∑ x, f x` is notation for `Finset.sum Finset.univ f` (assuming `α` is a `Fintype` and `β` is an `AddCommMonoid`) ## Implementation Notes The first arguments in all definitions and lemmas is the codomain of the function of the big operator. This is necessary for the heuristic in `@[to_additive]`. See the documentation of `to_additive.attr` for more information. -/ -- TODO -- assert_not_exists AddCommMonoidWithOne assert_not_exists MonoidWithZero assert_not_exists MulAction variable {ι κ α β γ : Type*} open Fin Function namespace Finset /-- `∏ x ∈ s, f x` is the product of `f x` as `x` ranges over the elements of the finite set `s`. When the index type is a `Fintype`, the notation `∏ x, f x`, is a shorthand for `∏ x ∈ Finset.univ, f x`. -/ @[to_additive "`∑ x ∈ s, f x` is the sum of `f x` as `x` ranges over the elements of the finite set `s`. When the index type is a `Fintype`, the notation `∑ x, f x`, is a shorthand for `∑ x ∈ Finset.univ, f x`."] protected def prod [CommMonoid β] (s : Finset α) (f : α → β) : β := (s.1.map f).prod @[to_additive (attr := simp)] theorem prod_mk [CommMonoid β] (s : Multiset α) (hs : s.Nodup) (f : α → β) : (⟨s, hs⟩ : Finset α).prod f = (s.map f).prod := rfl @[to_additive (attr := simp)] theorem prod_val [CommMonoid α] (s : Finset α) : s.1.prod = s.prod id := by rw [Finset.prod, Multiset.map_id] end Finset library_note "operator precedence of big operators"/-- There is no established mathematical convention for the operator precedence of big operators like `∏` and `∑`. We will have to make a choice. Online discussions, such as https://math.stackexchange.com/q/185538/30839 seem to suggest that `∏` and `∑` should have the same precedence, and that this should be somewhere between `*` and `+`. The latter have precedence levels `70` and `65` respectively, and we therefore choose the level `67`. In practice, this means that parentheses should be placed as follows: ```lean ∑ k ∈ K, (a k + b k) = ∑ k ∈ K, a k + ∑ k ∈ K, b k → ∏ k ∈ K, a k * b k = (∏ k ∈ K, a k) * (∏ k ∈ K, b k) ``` (Example taken from page 490 of Knuth's *Concrete Mathematics*.) -/ namespace BigOperators open Batteries.ExtendedBinder Lean Meta -- TODO: contribute this modification back to `extBinder` /-- A `bigOpBinder` is like an `extBinder` and has the form `x`, `x : ty`, or `x pred` where `pred` is a `binderPred` like `< 2`. Unlike `extBinder`, `x` is a term. -/ syntax bigOpBinder := term:max ((" : " term) <|> binderPred)? /-- A BigOperator binder in parentheses -/ syntax bigOpBinderParenthesized := " (" bigOpBinder ")" /-- A list of parenthesized binders -/ syntax bigOpBinderCollection := bigOpBinderParenthesized+ /-- A single (unparenthesized) binder, or a list of parenthesized binders -/ syntax bigOpBinders := bigOpBinderCollection <|> (ppSpace bigOpBinder) /-- Collects additional binder/Finset pairs for the given `bigOpBinder`. Note: this is not extensible at the moment, unlike the usual `bigOpBinder` expansions. -/ def processBigOpBinder (processed : (Array (Term × Term))) (binder : TSyntax ``bigOpBinder) : MacroM (Array (Term × Term)) := set_option hygiene false in withRef binder do match binder with | `(bigOpBinder| $x:term) => match x with | `(($a + $b = $n)) => -- Maybe this is too cute. return processed |>.push (← `(⟨$a, $b⟩), ← `(Finset.Nat.antidiagonal $n)) | _ => return processed |>.push (x, ← ``(Finset.univ)) | `(bigOpBinder| $x : $t) => return processed |>.push (x, ← ``((Finset.univ : Finset $t))) | `(bigOpBinder| $x ∈ $s) => return processed |>.push (x, ← `(finset% $s)) | `(bigOpBinder| $x < $n) => return processed |>.push (x, ← `(Finset.Iio $n)) | `(bigOpBinder| $x ≤ $n) => return processed |>.push (x, ← `(Finset.Iic $n)) | `(bigOpBinder| $x > $n) => return processed |>.push (x, ← `(Finset.Ioi $n)) | `(bigOpBinder| $x ≥ $n) => return processed |>.push (x, ← `(Finset.Ici $n)) | _ => Macro.throwUnsupported /-- Collects the binder/Finset pairs for the given `bigOpBinders`. -/ def processBigOpBinders (binders : TSyntax ``bigOpBinders) : MacroM (Array (Term × Term)) := match binders with | `(bigOpBinders| $b:bigOpBinder) => processBigOpBinder #[] b | `(bigOpBinders| $[($bs:bigOpBinder)]*) => bs.foldlM processBigOpBinder #[] | _ => Macro.throwUnsupported /-- Collect the binderIdents into a `⟨...⟩` expression. -/ def bigOpBindersPattern (processed : (Array (Term × Term))) : MacroM Term := do let ts := processed.map Prod.fst if ts.size == 1 then return ts[0]! else `(⟨$ts,*⟩) /-- Collect the terms into a product of sets. -/ def bigOpBindersProd (processed : (Array (Term × Term))) : MacroM Term := do if processed.isEmpty then `((Finset.univ : Finset Unit)) else if processed.size == 1 then return processed[0]!.2 else processed.foldrM (fun s p => `(SProd.sprod $(s.2) $p)) processed.back.2 (start := processed.size - 1) /-- - `∑ x, f x` is notation for `Finset.sum Finset.univ f`. It is the sum of `f x`, where `x` ranges over the finite domain of `f`. - `∑ x ∈ s, f x` is notation for `Finset.sum s f`. It is the sum of `f x`, where `x` ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance). - `∑ x ∈ s with p x, f x` is notation for `Finset.sum (Finset.filter p s) f`. - `∑ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.sum (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. These support destructuring, for example `∑ ⟨x, y⟩ ∈ s ×ˢ t, f x y`. Notation: `"∑" bigOpBinders* ("with" term)? "," term` -/ syntax (name := bigsum) "∑ " bigOpBinders ("with " term)? ", " term:67 : term /-- - `∏ x, f x` is notation for `Finset.prod Finset.univ f`. It is the product of `f x`, where `x` ranges over the finite domain of `f`. - `∏ x ∈ s, f x` is notation for `Finset.prod s f`. It is the product of `f x`, where `x` ranges over the finite set `s` (either a `Finset` or a `Set` with a `Fintype` instance). - `∏ x ∈ s with p x, f x` is notation for `Finset.prod (Finset.filter p s) f`. - `∏ (x ∈ s) (y ∈ t), f x y` is notation for `Finset.prod (s ×ˢ t) (fun ⟨x, y⟩ ↦ f x y)`. These support destructuring, for example `∏ ⟨x, y⟩ ∈ s ×ˢ t, f x y`. Notation: `"∏" bigOpBinders* ("with" term)? "," term` -/ syntax (name := bigprod) "∏ " bigOpBinders ("with " term)? ", " term:67 : term macro_rules (kind := bigsum) | `(∑ $bs:bigOpBinders $[with $p?]?, $v) => do let processed ← processBigOpBinders bs let x ← bigOpBindersPattern processed let s ← bigOpBindersProd processed match p? with | some p => `(Finset.sum (Finset.filter (fun $x ↦ $p) $s) (fun $x ↦ $v)) | none => `(Finset.sum $s (fun $x ↦ $v)) macro_rules (kind := bigprod) | `(∏ $bs:bigOpBinders $[with $p?]?, $v) => do let processed ← processBigOpBinders bs let x ← bigOpBindersPattern processed let s ← bigOpBindersProd processed match p? with | some p => `(Finset.prod (Finset.filter (fun $x ↦ $p) $s) (fun $x ↦ $v)) | none => `(Finset.prod $s (fun $x ↦ $v)) /-- (Deprecated, use `∑ x ∈ s, f x`) `∑ x in s, f x` is notation for `Finset.sum s f`. It is the sum of `f x`, where `x` ranges over the finite set `s`. -/ syntax (name := bigsumin) "∑ " extBinder " in " term ", " term:67 : term macro_rules (kind := bigsumin) | `(∑ $x:ident in $s, $r) => `(∑ $x:ident ∈ $s, $r) | `(∑ $x:ident : $t in $s, $r) => `(∑ $x:ident ∈ ($s : Finset $t), $r) /-- (Deprecated, use `∏ x ∈ s, f x`) `∏ x in s, f x` is notation for `Finset.prod s f`. It is the product of `f x`, where `x` ranges over the finite set `s`. -/ syntax (name := bigprodin) "∏ " extBinder " in " term ", " term:67 : term macro_rules (kind := bigprodin) | `(∏ $x:ident in $s, $r) => `(∏ $x:ident ∈ $s, $r) | `(∏ $x:ident : $t in $s, $r) => `(∏ $x:ident ∈ ($s : Finset $t), $r) open Lean Meta Parser.Term PrettyPrinter.Delaborator SubExpr open Batteries.ExtendedBinder /-- Delaborator for `Finset.prod`. The `pp.piBinderTypes` option controls whether to show the domain type when the product is over `Finset.univ`. -/ @[delab app.Finset.prod] def delabFinsetProd : Delab := whenPPOption getPPNotation <| withOverApp 5 <| do let #[_, _, _, s, f] := (← getExpr).getAppArgs | failure guard <| f.isLambda let ppDomain ← getPPOption getPPPiBinderTypes let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do return (i, ← delab) if s.isAppOfArity ``Finset.univ 2 then let binder ← if ppDomain then let ty ← withNaryArg 0 delab `(bigOpBinder| $(.mk i):ident : $ty) else `(bigOpBinder| $(.mk i):ident) `(∏ $binder:bigOpBinder, $body) else let ss ← withNaryArg 3 <| delab `(∏ $(.mk i):ident ∈ $ss, $body) /-- Delaborator for `Finset.sum`. The `pp.piBinderTypes` option controls whether to show the domain type when the sum is over `Finset.univ`. -/ @[delab app.Finset.sum] def delabFinsetSum : Delab := whenPPOption getPPNotation <| withOverApp 5 <| do let #[_, _, _, s, f] := (← getExpr).getAppArgs | failure guard <| f.isLambda let ppDomain ← getPPOption getPPPiBinderTypes let (i, body) ← withAppArg <| withBindingBodyUnusedName fun i => do return (i, ← delab) if s.isAppOfArity ``Finset.univ 2 then let binder ← if ppDomain then let ty ← withNaryArg 0 delab `(bigOpBinder| $(.mk i):ident : $ty) else `(bigOpBinder| $(.mk i):ident) `(∑ $binder:bigOpBinder, $body) else let ss ← withNaryArg 3 <| delab `(∑ $(.mk i):ident ∈ $ss, $body) end BigOperators namespace Finset variable {s s₁ s₂ : Finset α} {a : α} {f g : α → β} @[to_additive] theorem prod_eq_multiset_prod [CommMonoid β] (s : Finset α) (f : α → β) : ∏ x ∈ s, f x = (s.1.map f).prod := rfl @[to_additive (attr := simp)] lemma prod_map_val [CommMonoid β] (s : Finset α) (f : α → β) : (s.1.map f).prod = ∏ a ∈ s, f a := rfl @[to_additive] theorem prod_eq_fold [CommMonoid β] (s : Finset α) (f : α → β) : ∏ x ∈ s, f x = s.fold ((· * ·) : β → β → β) 1 f := rfl @[simp] theorem sum_multiset_singleton (s : Finset α) : (s.sum fun x => {x}) = s.val := by simp only [sum_eq_multiset_sum, Multiset.sum_map_singleton] end Finset @[to_additive (attr := simp)] theorem map_prod [CommMonoid β] [CommMonoid γ] {G : Type*} [FunLike G β γ] [MonoidHomClass G β γ] (g : G) (f : α → β) (s : Finset α) : g (∏ x ∈ s, f x) = ∏ x ∈ s, g (f x) := by simp only [Finset.prod_eq_multiset_prod, map_multiset_prod, Multiset.map_map]; rfl @[to_additive] theorem MonoidHom.coe_finset_prod [MulOneClass β] [CommMonoid γ] (f : α → β →* γ) (s : Finset α) : ⇑(∏ x ∈ s, f x) = ∏ x ∈ s, ⇑(f x) := map_prod (MonoidHom.coeFn β γ) _ _ /-- See also `Finset.prod_apply`, with the same conclusion but with the weaker hypothesis `f : α → β → γ` -/ @[to_additive (attr := simp) "See also `Finset.sum_apply`, with the same conclusion but with the weaker hypothesis `f : α → β → γ`"] theorem MonoidHom.finset_prod_apply [MulOneClass β] [CommMonoid γ] (f : α → β →* γ) (s : Finset α) (b : β) : (∏ x ∈ s, f x) b = ∏ x ∈ s, f x b := map_prod (MonoidHom.eval b) _ _ variable {s s₁ s₂ : Finset α} {a : α} {f g : α → β} namespace Finset section CommMonoid variable [CommMonoid β] @[to_additive (attr := simp)] theorem prod_empty : ∏ x ∈ ∅, f x = 1 := rfl @[to_additive] theorem prod_of_isEmpty [IsEmpty α] (s : Finset α) : ∏ i ∈ s, f i = 1 := by rw [eq_empty_of_isEmpty s, prod_empty] @[deprecated (since := "2024-06-11")] alias prod_of_empty := prod_of_isEmpty @[deprecated (since := "2024-06-11")] alias sum_of_empty := sum_of_isEmpty @[to_additive (attr := simp)] theorem prod_cons (h : a ∉ s) : ∏ x ∈ cons a s h, f x = f a * ∏ x ∈ s, f x := fold_cons h @[to_additive (attr := simp)] theorem prod_insert [DecidableEq α] : a ∉ s → ∏ x ∈ insert a s, f x = f a * ∏ x ∈ s, f x := fold_insert /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `a` is in `s` or `f a = 1`. -/ @[to_additive (attr := simp) "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `a` is in `s` or `f a = 0`."] theorem prod_insert_of_eq_one_if_not_mem [DecidableEq α] (h : a ∉ s → f a = 1) : ∏ x ∈ insert a s, f x = ∏ x ∈ s, f x := by by_cases hm : a ∈ s · simp_rw [insert_eq_of_mem hm] · rw [prod_insert hm, h hm, one_mul] /-- The product of `f` over `insert a s` is the same as the product over `s`, as long as `f a = 1`. -/ @[to_additive (attr := simp) "The sum of `f` over `insert a s` is the same as the sum over `s`, as long as `f a = 0`."] theorem prod_insert_one [DecidableEq α] (h : f a = 1) : ∏ x ∈ insert a s, f x = ∏ x ∈ s, f x := prod_insert_of_eq_one_if_not_mem fun _ => h @[to_additive] theorem prod_insert_div {M : Type*} [CommGroup M] [DecidableEq α] (ha : a ∉ s) {f : α → M} : (∏ x ∈ insert a s, f x) / f a = ∏ x ∈ s, f x := by simp [ha] @[to_additive (attr := simp)] theorem prod_singleton (f : α → β) (a : α) : ∏ x ∈ singleton a, f x = f a := Eq.trans fold_singleton <| mul_one _ @[to_additive] theorem prod_pair [DecidableEq α] {a b : α} (h : a ≠ b) : (∏ x ∈ ({a, b} : Finset α), f x) = f a * f b := by rw [prod_insert (not_mem_singleton.2 h), prod_singleton] @[to_additive (attr := simp)] theorem prod_const_one : (∏ _x ∈ s, (1 : β)) = 1 := by simp only [Finset.prod, Multiset.map_const', Multiset.prod_replicate, one_pow] @[to_additive (attr := simp)] theorem prod_image [DecidableEq α] {s : Finset γ} {g : γ → α} : (∀ x ∈ s, ∀ y ∈ s, g x = g y → x = y) → ∏ x ∈ s.image g, f x = ∏ x ∈ s, f (g x) := fold_image @[to_additive (attr := simp)] theorem prod_map (s : Finset α) (e : α ↪ γ) (f : γ → β) : ∏ x ∈ s.map e, f x = ∏ x ∈ s, f (e x) := by rw [Finset.prod, Finset.map_val, Multiset.map_map]; rfl @[to_additive] lemma prod_attach (s : Finset α) (f : α → β) : ∏ x ∈ s.attach, f x = ∏ x ∈ s, f x := by classical rw [← prod_image Subtype.coe_injective.injOn, attach_image_val] @[to_additive (attr := congr)] theorem prod_congr (h : s₁ = s₂) : (∀ x ∈ s₂, f x = g x) → s₁.prod f = s₂.prod g := by rw [h]; exact fold_congr @[to_additive] theorem prod_eq_one {f : α → β} {s : Finset α} (h : ∀ x ∈ s, f x = 1) : ∏ x ∈ s, f x = 1 := calc ∏ x ∈ s, f x = ∏ _x ∈ s, 1 := Finset.prod_congr rfl h _ = 1 := Finset.prod_const_one @[to_additive] theorem prod_disjUnion (h) : ∏ x ∈ s₁.disjUnion s₂ h, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x := by refine Eq.trans ?_ (fold_disjUnion h) rw [one_mul] rfl @[to_additive] theorem prod_disjiUnion (s : Finset ι) (t : ι → Finset α) (h) : ∏ x ∈ s.disjiUnion t h, f x = ∏ i ∈ s, ∏ x ∈ t i, f x := by refine Eq.trans ?_ (fold_disjiUnion h) dsimp [Finset.prod, Multiset.prod, Multiset.fold, Finset.disjUnion, Finset.fold] congr exact prod_const_one.symm @[to_additive] theorem prod_union_inter [DecidableEq α] : (∏ x ∈ s₁ ∪ s₂, f x) * ∏ x ∈ s₁ ∩ s₂, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x := fold_union_inter @[to_additive] theorem prod_union [DecidableEq α] (h : Disjoint s₁ s₂) : ∏ x ∈ s₁ ∪ s₂, f x = (∏ x ∈ s₁, f x) * ∏ x ∈ s₂, f x := by rw [← prod_union_inter, disjoint_iff_inter_eq_empty.mp h]; exact (mul_one _).symm @[to_additive] theorem prod_filter_mul_prod_filter_not (s : Finset α) (p : α → Prop) [DecidablePred p] [∀ x, Decidable (¬p x)] (f : α → β) : (∏ x ∈ s.filter p, f x) * ∏ x ∈ s.filter fun x => ¬p x, f x = ∏ x ∈ s, f x := by have := Classical.decEq α rw [← prod_union (disjoint_filter_filter_neg s s p), filter_union_filter_neg_eq] section ToList @[to_additive (attr := simp)] theorem prod_to_list (s : Finset α) (f : α → β) : (s.toList.map f).prod = s.prod f := by rw [Finset.prod, ← Multiset.prod_coe, ← Multiset.map_coe, Finset.coe_toList] end ToList @[to_additive] theorem _root_.Equiv.Perm.prod_comp (σ : Equiv.Perm α) (s : Finset α) (f : α → β) (hs : { a | σ a ≠ a } ⊆ s) : (∏ x ∈ s, f (σ x)) = ∏ x ∈ s, f x := by convert (prod_map s σ.toEmbedding f).symm exact (map_perm hs).symm @[to_additive] theorem _root_.Equiv.Perm.prod_comp' (σ : Equiv.Perm α) (s : Finset α) (f : α → α → β) (hs : { a | σ a ≠ a } ⊆ s) : (∏ x ∈ s, f (σ x) x) = ∏ x ∈ s, f x (σ.symm x) := by convert σ.prod_comp s (fun x => f x (σ.symm x)) hs rw [Equiv.symm_apply_apply] /-- A product over all subsets of `s ∪ {x}` is obtained by multiplying the product over all subsets of `s`, and over all subsets of `s` to which one adds `x`. -/ @[to_additive "A sum over all subsets of `s ∪ {x}` is obtained by summing the sum over all subsets of `s`, and over all subsets of `s` to which one adds `x`."] lemma prod_powerset_insert [DecidableEq α] (ha : a ∉ s) (f : Finset α → β) : ∏ t ∈ (insert a s).powerset, f t = (∏ t ∈ s.powerset, f t) * ∏ t ∈ s.powerset, f (insert a t) := by rw [powerset_insert, prod_union, prod_image] · exact insert_erase_invOn.2.injOn.mono fun t ht ↦ not_mem_mono (mem_powerset.1 ht) ha · aesop (add simp [disjoint_left, insert_subset_iff]) /-- A product over all subsets of `s ∪ {x}` is obtained by multiplying the product over all subsets of `s`, and over all subsets of `s` to which one adds `x`. -/ @[to_additive "A sum over all subsets of `s ∪ {x}` is obtained by summing the sum over all subsets of `s`, and over all subsets of `s` to which one adds `x`."] lemma prod_powerset_cons (ha : a ∉ s) (f : Finset α → β) : ∏ t ∈ (s.cons a ha).powerset, f t = (∏ t ∈ s.powerset, f t) * ∏ t ∈ s.powerset.attach, f (cons a t $ not_mem_mono (mem_powerset.1 t.2) ha) := by classical simp_rw [cons_eq_insert] rw [prod_powerset_insert ha, prod_attach _ fun t ↦ f (insert a t)] /-- A product over `powerset s` is equal to the double product over sets of subsets of `s` with `card s = k`, for `k = 1, ..., card s`. -/ @[to_additive "A sum over `powerset s` is equal to the double sum over sets of subsets of `s` with `card s = k`, for `k = 1, ..., card s`"] lemma prod_powerset (s : Finset α) (f : Finset α → β) : ∏ t ∈ powerset s, f t = ∏ j ∈ range (card s + 1), ∏ t ∈ powersetCard j s, f t := by rw [powerset_card_disjiUnion, prod_disjiUnion] end CommMonoid end Finset section open Finset variable [Fintype α] [CommMonoid β] @[to_additive] theorem IsCompl.prod_mul_prod {s t : Finset α} (h : IsCompl s t) (f : α → β) : (∏ i ∈ s, f i) * ∏ i ∈ t, f i = ∏ i, f i := (Finset.prod_disjUnion h.disjoint).symm.trans <| by classical rw [Finset.disjUnion_eq_union, ← Finset.sup_eq_union, h.sup_eq_top]; rfl end namespace Finset section CommMonoid variable [CommMonoid β] /-- Multiplying the products of a function over `s` and over `sᶜ` gives the whole product. For a version expressed with subtypes, see `Fintype.prod_subtype_mul_prod_subtype`. -/ @[to_additive "Adding the sums of a function over `s` and over `sᶜ` gives the whole sum. For a version expressed with subtypes, see `Fintype.sum_subtype_add_sum_subtype`. "] theorem prod_mul_prod_compl [Fintype α] [DecidableEq α] (s : Finset α) (f : α → β) : (∏ i ∈ s, f i) * ∏ i ∈ sᶜ, f i = ∏ i, f i := IsCompl.prod_mul_prod isCompl_compl f @[to_additive] theorem prod_compl_mul_prod [Fintype α] [DecidableEq α] (s : Finset α) (f : α → β) : (∏ i ∈ sᶜ, f i) * ∏ i ∈ s, f i = ∏ i, f i := (@isCompl_compl _ s _).symm.prod_mul_prod f @[to_additive] theorem prod_sdiff [DecidableEq α] (h : s₁ ⊆ s₂) : (∏ x ∈ s₂ \ s₁, f x) * ∏ x ∈ s₁, f x = ∏ x ∈ s₂, f x := by rw [← prod_union sdiff_disjoint, sdiff_union_of_subset h] @[to_additive] theorem prod_subset_one_on_sdiff [DecidableEq α] (h : s₁ ⊆ s₂) (hg : ∀ x ∈ s₂ \ s₁, g x = 1) (hfg : ∀ x ∈ s₁, f x = g x) : ∏ i ∈ s₁, f i = ∏ i ∈ s₂, g i := by rw [← prod_sdiff h, prod_eq_one hg, one_mul] exact prod_congr rfl hfg @[to_additive] theorem prod_subset (h : s₁ ⊆ s₂) (hf : ∀ x ∈ s₂, x ∉ s₁ → f x = 1) : ∏ x ∈ s₁, f x = ∏ x ∈ s₂, f x := haveI := Classical.decEq α prod_subset_one_on_sdiff h (by simpa) fun _ _ => rfl @[to_additive (attr := simp)] theorem prod_disj_sum (s : Finset α) (t : Finset γ) (f : α ⊕ γ → β) : ∏ x ∈ s.disjSum t, f x = (∏ x ∈ s, f (Sum.inl x)) * ∏ x ∈ t, f (Sum.inr x) := by rw [← map_inl_disjUnion_map_inr, prod_disjUnion, prod_map, prod_map] rfl @[to_additive] theorem prod_sum_elim (s : Finset α) (t : Finset γ) (f : α → β) (g : γ → β) : ∏ x ∈ s.disjSum t, Sum.elim f g x = (∏ x ∈ s, f x) * ∏ x ∈ t, g x := by simp @[to_additive] theorem prod_biUnion [DecidableEq α] {s : Finset γ} {t : γ → Finset α} (hs : Set.PairwiseDisjoint (↑s) t) : ∏ x ∈ s.biUnion t, f x = ∏ x ∈ s, ∏ i ∈ t x, f i := by rw [← disjiUnion_eq_biUnion _ _ hs, prod_disjiUnion] /-- Product over a sigma type equals the product of fiberwise products. For rewriting in the reverse direction, use `Finset.prod_sigma'`. -/ @[to_additive "Sum over a sigma type equals the sum of fiberwise sums. For rewriting in the reverse direction, use `Finset.sum_sigma'`"] theorem prod_sigma {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) (f : Sigma σ → β) : ∏ x ∈ s.sigma t, f x = ∏ a ∈ s, ∏ s ∈ t a, f ⟨a, s⟩ := by simp_rw [← disjiUnion_map_sigma_mk, prod_disjiUnion, prod_map, Function.Embedding.sigmaMk_apply] @[to_additive] theorem prod_sigma' {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) (f : ∀ a, σ a → β) : (∏ a ∈ s, ∏ s ∈ t a, f a s) = ∏ x ∈ s.sigma t, f x.1 x.2 := Eq.symm <| prod_sigma s t fun x => f x.1 x.2 section bij variable {ι κ α : Type*} [CommMonoid α] {s : Finset ι} {t : Finset κ} {f : ι → α} {g : κ → α} /-- Reorder a product. The difference with `Finset.prod_bij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. The difference with `Finset.prod_nbij` is that the bijection is allowed to use membership of the domain of the product, rather than being a non-dependent function. -/ @[to_additive "Reorder a sum. The difference with `Finset.sum_bij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. The difference with `Finset.sum_nbij` is that the bijection is allowed to use membership of the domain of the sum, rather than being a non-dependent function."] theorem prod_bij (i : ∀ a ∈ s, κ) (hi : ∀ a ha, i a ha ∈ t) (i_inj : ∀ a₁ ha₁ a₂ ha₂, i a₁ ha₁ = i a₂ ha₂ → a₁ = a₂) (i_surj : ∀ b ∈ t, ∃ a ha, i a ha = b) (h : ∀ a ha, f a = g (i a ha)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x := congr_arg Multiset.prod (Multiset.map_eq_map_of_bij_of_nodup f g s.2 t.2 i hi i_inj i_surj h) /-- Reorder a product. The difference with `Finset.prod_bij` is that the bijection is specified with an inverse, rather than as a surjective injection. The difference with `Finset.prod_nbij'` is that the bijection and its inverse are allowed to use membership of the domains of the products, rather than being non-dependent functions. -/ @[to_additive "Reorder a sum. The difference with `Finset.sum_bij` is that the bijection is specified with an inverse, rather than as a surjective injection. The difference with `Finset.sum_nbij'` is that the bijection and its inverse are allowed to use membership of the domains of the sums, rather than being non-dependent functions."] theorem prod_bij' (i : ∀ a ∈ s, κ) (j : ∀ a ∈ t, ι) (hi : ∀ a ha, i a ha ∈ t) (hj : ∀ a ha, j a ha ∈ s) (left_inv : ∀ a ha, j (i a ha) (hi a ha) = a) (right_inv : ∀ a ha, i (j a ha) (hj a ha) = a) (h : ∀ a ha, f a = g (i a ha)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x := by refine prod_bij i hi (fun a1 h1 a2 h2 eq ↦ ?_) (fun b hb ↦ ⟨_, hj b hb, right_inv b hb⟩) h rw [← left_inv a1 h1, ← left_inv a2 h2] simp only [eq] /-- Reorder a product. The difference with `Finset.prod_nbij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. The difference with `Finset.prod_bij` is that the bijection is a non-dependent function, rather than being allowed to use membership of the domain of the product. -/ @[to_additive "Reorder a sum. The difference with `Finset.sum_nbij'` is that the bijection is specified as a surjective injection, rather than by an inverse function. The difference with `Finset.sum_bij` is that the bijection is a non-dependent function, rather than being allowed to use membership of the domain of the sum."] lemma prod_nbij (i : ι → κ) (hi : ∀ a ∈ s, i a ∈ t) (i_inj : (s : Set ι).InjOn i) (i_surj : (s : Set ι).SurjOn i t) (h : ∀ a ∈ s, f a = g (i a)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x := prod_bij (fun a _ ↦ i a) hi i_inj (by simpa using i_surj) h /-- Reorder a product. The difference with `Finset.prod_nbij` is that the bijection is specified with an inverse, rather than as a surjective injection. The difference with `Finset.prod_bij'` is that the bijection and its inverse are non-dependent functions, rather than being allowed to use membership of the domains of the products. The difference with `Finset.prod_equiv` is that bijectivity is only required to hold on the domains of the products, rather than on the entire types. -/ @[to_additive "Reorder a sum. The difference with `Finset.sum_nbij` is that the bijection is specified with an inverse, rather than as a surjective injection. The difference with `Finset.sum_bij'` is that the bijection and its inverse are non-dependent functions, rather than being allowed to use membership of the domains of the sums. The difference with `Finset.sum_equiv` is that bijectivity is only required to hold on the domains of the sums, rather than on the entire types."] lemma prod_nbij' (i : ι → κ) (j : κ → ι) (hi : ∀ a ∈ s, i a ∈ t) (hj : ∀ a ∈ t, j a ∈ s) (left_inv : ∀ a ∈ s, j (i a) = a) (right_inv : ∀ a ∈ t, i (j a) = a) (h : ∀ a ∈ s, f a = g (i a)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x := prod_bij' (fun a _ ↦ i a) (fun b _ ↦ j b) hi hj left_inv right_inv h /-- Specialization of `Finset.prod_nbij'` that automatically fills in most arguments. See `Fintype.prod_equiv` for the version where `s` and `t` are `univ`. -/ @[to_additive "`Specialization of `Finset.sum_nbij'` that automatically fills in most arguments. See `Fintype.sum_equiv` for the version where `s` and `t` are `univ`."] lemma prod_equiv (e : ι ≃ κ) (hst : ∀ i, i ∈ s ↔ e i ∈ t) (hfg : ∀ i ∈ s, f i = g (e i)) : ∏ i ∈ s, f i = ∏ i ∈ t, g i := by refine prod_nbij' e e.symm ?_ ?_ ?_ ?_ hfg <;> simp [hst] /-- Specialization of `Finset.prod_bij` that automatically fills in most arguments. See `Fintype.prod_bijective` for the version where `s` and `t` are `univ`. -/ @[to_additive "`Specialization of `Finset.sum_bij` that automatically fills in most arguments. See `Fintype.sum_bijective` for the version where `s` and `t` are `univ`."] lemma prod_bijective (e : ι → κ) (he : e.Bijective) (hst : ∀ i, i ∈ s ↔ e i ∈ t) (hfg : ∀ i ∈ s, f i = g (e i)) : ∏ i ∈ s, f i = ∏ i ∈ t, g i := prod_equiv (.ofBijective e he) hst hfg @[to_additive] lemma prod_of_injOn (e : ι → κ) (he : Set.InjOn e s) (hest : Set.MapsTo e s t) (h' : ∀ i ∈ t, i ∉ e '' s → g i = 1) (h : ∀ i ∈ s, f i = g (e i)) : ∏ i ∈ s, f i = ∏ j ∈ t, g j := by classical exact (prod_nbij e (fun a ↦ mem_image_of_mem e) he (by simp [Set.surjOn_image]) h).trans <| prod_subset (image_subset_iff.2 hest) <| by simpa using h' variable [DecidableEq κ] @[to_additive] lemma prod_fiberwise_eq_prod_filter (s : Finset ι) (t : Finset κ) (g : ι → κ) (f : ι → α) : ∏ j ∈ t, ∏ i ∈ s.filter fun i ↦ g i = j, f i = ∏ i ∈ s.filter fun i ↦ g i ∈ t, f i := by rw [← prod_disjiUnion, disjiUnion_filter_eq] @[to_additive] lemma prod_fiberwise_eq_prod_filter' (s : Finset ι) (t : Finset κ) (g : ι → κ) (f : κ → α) : ∏ j ∈ t, ∏ _i ∈ s.filter fun i ↦ g i = j, f j = ∏ i ∈ s.filter fun i ↦ g i ∈ t, f (g i) := by calc _ = ∏ j ∈ t, ∏ i ∈ s.filter fun i ↦ g i = j, f (g i) := prod_congr rfl fun j _ ↦ prod_congr rfl fun i hi ↦ by rw [(mem_filter.1 hi).2] _ = _ := prod_fiberwise_eq_prod_filter _ _ _ _ @[to_additive] lemma prod_fiberwise_of_maps_to {g : ι → κ} (h : ∀ i ∈ s, g i ∈ t) (f : ι → α) : ∏ j ∈ t, ∏ i ∈ s.filter fun i ↦ g i = j, f i = ∏ i ∈ s, f i := by rw [← prod_disjiUnion, disjiUnion_filter_eq_of_maps_to h] @[to_additive] lemma prod_fiberwise_of_maps_to' {g : ι → κ} (h : ∀ i ∈ s, g i ∈ t) (f : κ → α) : ∏ j ∈ t, ∏ _i ∈ s.filter fun i ↦ g i = j, f j = ∏ i ∈ s, f (g i) := by calc _ = ∏ y ∈ t, ∏ x ∈ s.filter fun x ↦ g x = y, f (g x) := prod_congr rfl fun y _ ↦ prod_congr rfl fun x hx ↦ by rw [(mem_filter.1 hx).2] _ = _ := prod_fiberwise_of_maps_to h _ variable [Fintype κ] @[to_additive] lemma prod_fiberwise (s : Finset ι) (g : ι → κ) (f : ι → α) : ∏ j, ∏ i ∈ s.filter fun i ↦ g i = j, f i = ∏ i ∈ s, f i := prod_fiberwise_of_maps_to (fun _ _ ↦ mem_univ _) _ @[to_additive] lemma prod_fiberwise' (s : Finset ι) (g : ι → κ) (f : κ → α) : ∏ j, ∏ _i ∈ s.filter fun i ↦ g i = j, f j = ∏ i ∈ s, f (g i) := prod_fiberwise_of_maps_to' (fun _ _ ↦ mem_univ _) _ end bij /-- Taking a product over `univ.pi t` is the same as taking the product over `Fintype.piFinset t`. `univ.pi t` and `Fintype.piFinset t` are essentially the same `Finset`, but differ in the type of their element, `univ.pi t` is a `Finset (Π a ∈ univ, t a)` and `Fintype.piFinset t` is a `Finset (Π a, t a)`. -/ @[to_additive "Taking a sum over `univ.pi t` is the same as taking the sum over `Fintype.piFinset t`. `univ.pi t` and `Fintype.piFinset t` are essentially the same `Finset`, but differ in the type of their element, `univ.pi t` is a `Finset (Π a ∈ univ, t a)` and `Fintype.piFinset t` is a `Finset (Π a, t a)`."] lemma prod_univ_pi [DecidableEq ι] [Fintype ι] {κ : ι → Type*} (t : ∀ i, Finset (κ i)) (f : (∀ i ∈ (univ : Finset ι), κ i) → β) : ∏ x ∈ univ.pi t, f x = ∏ x ∈ Fintype.piFinset t, f fun a _ ↦ x a := by apply prod_nbij' (fun x i ↦ x i $ mem_univ _) (fun x i _ ↦ x i) <;> simp @[to_additive (attr := simp)] lemma prod_diag [DecidableEq α] (s : Finset α) (f : α × α → β) : ∏ i ∈ s.diag, f i = ∏ i ∈ s, f (i, i) := by apply prod_nbij' Prod.fst (fun i ↦ (i, i)) <;> simp @[to_additive] theorem prod_finset_product (r : Finset (γ × α)) (s : Finset γ) (t : γ → Finset α) (h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ × α → β} : ∏ p ∈ r, f p = ∏ c ∈ s, ∏ a ∈ t c, f (c, a) := by refine Eq.trans ?_ (prod_sigma s t fun p => f (p.1, p.2)) apply prod_equiv (Equiv.sigmaEquivProd _ _).symm <;> simp [h] @[to_additive] theorem prod_finset_product' (r : Finset (γ × α)) (s : Finset γ) (t : γ → Finset α) (h : ∀ p : γ × α, p ∈ r ↔ p.1 ∈ s ∧ p.2 ∈ t p.1) {f : γ → α → β} : ∏ p ∈ r, f p.1 p.2 = ∏ c ∈ s, ∏ a ∈ t c, f c a := prod_finset_product r s t h @[to_additive] theorem prod_finset_product_right (r : Finset (α × γ)) (s : Finset γ) (t : γ → Finset α) (h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α × γ → β} : ∏ p ∈ r, f p = ∏ c ∈ s, ∏ a ∈ t c, f (a, c) := by refine Eq.trans ?_ (prod_sigma s t fun p => f (p.2, p.1)) apply prod_equiv ((Equiv.prodComm _ _).trans (Equiv.sigmaEquivProd _ _).symm) <;> simp [h] @[to_additive] theorem prod_finset_product_right' (r : Finset (α × γ)) (s : Finset γ) (t : γ → Finset α) (h : ∀ p : α × γ, p ∈ r ↔ p.2 ∈ s ∧ p.1 ∈ t p.2) {f : α → γ → β} : ∏ p ∈ r, f p.1 p.2 = ∏ c ∈ s, ∏ a ∈ t c, f a c := prod_finset_product_right r s t h @[to_additive] theorem prod_image' [DecidableEq α] {s : Finset γ} {g : γ → α} (h : γ → β) (eq : ∀ c ∈ s, f (g c) = ∏ x ∈ s.filter fun c' => g c' = g c, h x) : ∏ x ∈ s.image g, f x = ∏ x ∈ s, h x := calc ∏ x ∈ s.image g, f x = ∏ x ∈ s.image g, ∏ x ∈ s.filter fun c' => g c' = x, h x := (prod_congr rfl) fun _x hx => let ⟨c, hcs, hc⟩ := mem_image.1 hx hc ▸ eq c hcs _ = ∏ x ∈ s, h x := prod_fiberwise_of_maps_to (fun _x => mem_image_of_mem g) _ @[to_additive] theorem prod_mul_distrib : ∏ x ∈ s, f x * g x = (∏ x ∈ s, f x) * ∏ x ∈ s, g x := Eq.trans (by rw [one_mul]; rfl) fold_op_distrib @[to_additive] lemma prod_mul_prod_comm (f g h i : α → β) : (∏ a ∈ s, f a * g a) * ∏ a ∈ s, h a * i a = (∏ a ∈ s, f a * h a) * ∏ a ∈ s, g a * i a := by simp_rw [prod_mul_distrib, mul_mul_mul_comm] @[to_additive] theorem prod_product {s : Finset γ} {t : Finset α} {f : γ × α → β} : ∏ x ∈ s ×ˢ t, f x = ∏ x ∈ s, ∏ y ∈ t, f (x, y) := prod_finset_product (s ×ˢ t) s (fun _a => t) fun _p => mem_product /-- An uncurried version of `Finset.prod_product`. -/ @[to_additive "An uncurried version of `Finset.sum_product`"] theorem prod_product' {s : Finset γ} {t : Finset α} {f : γ → α → β} : ∏ x ∈ s ×ˢ t, f x.1 x.2 = ∏ x ∈ s, ∏ y ∈ t, f x y := prod_product @[to_additive] theorem prod_product_right {s : Finset γ} {t : Finset α} {f : γ × α → β} : ∏ x ∈ s ×ˢ t, f x = ∏ y ∈ t, ∏ x ∈ s, f (x, y) := prod_finset_product_right (s ×ˢ t) t (fun _a => s) fun _p => mem_product.trans and_comm /-- An uncurried version of `Finset.prod_product_right`. -/ @[to_additive "An uncurried version of `Finset.sum_product_right`"] theorem prod_product_right' {s : Finset γ} {t : Finset α} {f : γ → α → β} : ∏ x ∈ s ×ˢ t, f x.1 x.2 = ∏ y ∈ t, ∏ x ∈ s, f x y := prod_product_right /-- Generalization of `Finset.prod_comm` to the case when the inner `Finset`s depend on the outer variable. -/ @[to_additive "Generalization of `Finset.sum_comm` to the case when the inner `Finset`s depend on the outer variable."] theorem prod_comm' {s : Finset γ} {t : γ → Finset α} {t' : Finset α} {s' : α → Finset γ} (h : ∀ x y, x ∈ s ∧ y ∈ t x ↔ x ∈ s' y ∧ y ∈ t') {f : γ → α → β} : (∏ x ∈ s, ∏ y ∈ t x, f x y) = ∏ y ∈ t', ∏ x ∈ s' y, f x y := by classical have : ∀ z : γ × α, (z ∈ s.biUnion fun x => (t x).map <| Function.Embedding.sectr x _) ↔ z.1 ∈ s ∧ z.2 ∈ t z.1 := by rintro ⟨x, y⟩ simp only [mem_biUnion, mem_map, Function.Embedding.sectr_apply, Prod.mk.injEq, exists_eq_right, ← and_assoc] exact (prod_finset_product' _ _ _ this).symm.trans ((prod_finset_product_right' _ _ _) fun ⟨x, y⟩ => (this _).trans ((h x y).trans and_comm)) @[to_additive] theorem prod_comm {s : Finset γ} {t : Finset α} {f : γ → α → β} : (∏ x ∈ s, ∏ y ∈ t, f x y) = ∏ y ∈ t, ∏ x ∈ s, f x y := prod_comm' fun _ _ => Iff.rfl @[to_additive] theorem prod_hom_rel [CommMonoid γ] {r : β → γ → Prop} {f : α → β} {g : α → γ} {s : Finset α} (h₁ : r 1 1) (h₂ : ∀ a b c, r b c → r (f a * b) (g a * c)) : r (∏ x ∈ s, f x) (∏ x ∈ s, g x) := by delta Finset.prod apply Multiset.prod_hom_rel <;> assumption @[to_additive] theorem prod_filter_of_ne {p : α → Prop} [DecidablePred p] (hp : ∀ x ∈ s, f x ≠ 1 → p x) : ∏ x ∈ s.filter p, f x = ∏ x ∈ s, f x := (prod_subset (filter_subset _ _)) fun x => by classical rw [not_imp_comm, mem_filter] exact fun h₁ h₂ => ⟨h₁, by simpa using hp _ h₁ h₂⟩ -- If we use `[DecidableEq β]` here, some rewrites fail because they find a wrong `Decidable` -- instance first; `{∀ x, Decidable (f x ≠ 1)}` doesn't work with `rw ← prod_filter_ne_one` @[to_additive] theorem prod_filter_ne_one (s : Finset α) [∀ x, Decidable (f x ≠ 1)] : ∏ x ∈ s.filter fun x => f x ≠ 1, f x = ∏ x ∈ s, f x := prod_filter_of_ne fun _ _ => id @[to_additive] theorem prod_filter (p : α → Prop) [DecidablePred p] (f : α → β) : ∏ a ∈ s.filter p, f a = ∏ a ∈ s, if p a then f a else 1 := calc ∏ a ∈ s.filter p, f a = ∏ a ∈ s.filter p, if p a then f a else 1 := prod_congr rfl fun a h => by rw [if_pos]; simpa using (mem_filter.1 h).2 _ = ∏ a ∈ s, if p a then f a else 1 := by { refine prod_subset (filter_subset _ s) fun x hs h => ?_ rw [mem_filter, not_and] at h exact if_neg (by simpa using h hs) } @[to_additive] theorem prod_eq_single_of_mem {s : Finset α} {f : α → β} (a : α) (h : a ∈ s) (h₀ : ∀ b ∈ s, b ≠ a → f b = 1) : ∏ x ∈ s, f x = f a := by haveI := Classical.decEq α calc ∏ x ∈ s, f x = ∏ x ∈ {a}, f x := by { refine (prod_subset ?_ ?_).symm · intro _ H rwa [mem_singleton.1 H] · simpa only [mem_singleton] } _ = f a := prod_singleton _ _ @[to_additive] theorem prod_eq_single {s : Finset α} {f : α → β} (a : α) (h₀ : ∀ b ∈ s, b ≠ a → f b = 1) (h₁ : a ∉ s → f a = 1) : ∏ x ∈ s, f x = f a := haveI := Classical.decEq α by_cases (prod_eq_single_of_mem a · h₀) fun this => (prod_congr rfl fun b hb => h₀ b hb <| by rintro rfl; exact this hb).trans <| prod_const_one.trans (h₁ this).symm @[to_additive] lemma prod_union_eq_left [DecidableEq α] (hs : ∀ a ∈ s₂, a ∉ s₁ → f a = 1) : ∏ a ∈ s₁ ∪ s₂, f a = ∏ a ∈ s₁, f a := Eq.symm <| prod_subset subset_union_left fun _a ha ha' ↦ hs _ ((mem_union.1 ha).resolve_left ha') ha' @[to_additive] lemma prod_union_eq_right [DecidableEq α] (hs : ∀ a ∈ s₁, a ∉ s₂ → f a = 1) : ∏ a ∈ s₁ ∪ s₂, f a = ∏ a ∈ s₂, f a := by rw [union_comm, prod_union_eq_left hs] @[to_additive] theorem prod_eq_mul_of_mem {s : Finset α} {f : α → β} (a b : α) (ha : a ∈ s) (hb : b ∈ s) (hn : a ≠ b) (h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) : ∏ x ∈ s, f x = f a * f b := by haveI := Classical.decEq α; let s' := ({a, b} : Finset α) have hu : s' ⊆ s := by refine insert_subset_iff.mpr ?_ apply And.intro ha apply singleton_subset_iff.mpr hb have hf : ∀ c ∈ s, c ∉ s' → f c = 1 := by intro c hc hcs apply h₀ c hc apply not_or.mp intro hab apply hcs rw [mem_insert, mem_singleton] exact hab rw [← prod_subset hu hf] exact Finset.prod_pair hn @[to_additive] theorem prod_eq_mul {s : Finset α} {f : α → β} (a b : α) (hn : a ≠ b) (h₀ : ∀ c ∈ s, c ≠ a ∧ c ≠ b → f c = 1) (ha : a ∉ s → f a = 1) (hb : b ∉ s → f b = 1) : ∏ x ∈ s, f x = f a * f b := by haveI := Classical.decEq α; by_cases h₁ : a ∈ s <;> by_cases h₂ : b ∈ s · exact prod_eq_mul_of_mem a b h₁ h₂ hn h₀ · rw [hb h₂, mul_one] apply prod_eq_single_of_mem a h₁ exact fun c hc hca => h₀ c hc ⟨hca, ne_of_mem_of_not_mem hc h₂⟩ · rw [ha h₁, one_mul] apply prod_eq_single_of_mem b h₂ exact fun c hc hcb => h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, hcb⟩ · rw [ha h₁, hb h₂, mul_one] exact _root_.trans (prod_congr rfl fun c hc => h₀ c hc ⟨ne_of_mem_of_not_mem hc h₁, ne_of_mem_of_not_mem hc h₂⟩) prod_const_one -- Porting note: simpNF linter complains that LHS doesn't simplify, but it does /-- A product over `s.subtype p` equals one over `s.filter p`. -/ @[to_additive (attr := simp, nolint simpNF) "A sum over `s.subtype p` equals one over `s.filter p`."] theorem prod_subtype_eq_prod_filter (f : α → β) {p : α → Prop} [DecidablePred p] : ∏ x ∈ s.subtype p, f x = ∏ x ∈ s.filter p, f x := by conv_lhs => erw [← prod_map (s.subtype p) (Function.Embedding.subtype _) f] exact prod_congr (subtype_map _) fun x _hx => rfl /-- If all elements of a `Finset` satisfy the predicate `p`, a product over `s.subtype p` equals that product over `s`. -/ @[to_additive "If all elements of a `Finset` satisfy the predicate `p`, a sum over `s.subtype p` equals that sum over `s`."] theorem prod_subtype_of_mem (f : α → β) {p : α → Prop} [DecidablePred p] (h : ∀ x ∈ s, p x) : ∏ x ∈ s.subtype p, f x = ∏ x ∈ s, f x := by rw [prod_subtype_eq_prod_filter, filter_true_of_mem] simpa using h /-- A product of a function over a `Finset` in a subtype equals a product in the main type of a function that agrees with the first function on that `Finset`. -/ @[to_additive "A sum of a function over a `Finset` in a subtype equals a sum in the main type of a function that agrees with the first function on that `Finset`."] theorem prod_subtype_map_embedding {p : α → Prop} {s : Finset { x // p x }} {f : { x // p x } → β} {g : α → β} (h : ∀ x : { x // p x }, x ∈ s → g x = f x) : (∏ x ∈ s.map (Function.Embedding.subtype _), g x) = ∏ x ∈ s, f x := by rw [Finset.prod_map] exact Finset.prod_congr rfl h variable (f s) @[to_additive] theorem prod_coe_sort_eq_attach (f : s → β) : ∏ i : s, f i = ∏ i ∈ s.attach, f i := rfl @[to_additive] theorem prod_coe_sort : ∏ i : s, f i = ∏ i ∈ s, f i := prod_attach _ _ @[to_additive] theorem prod_finset_coe (f : α → β) (s : Finset α) : (∏ i : (s : Set α), f i) = ∏ i ∈ s, f i := prod_coe_sort s f variable {f s} @[to_additive] theorem prod_subtype {p : α → Prop} {F : Fintype (Subtype p)} (s : Finset α) (h : ∀ x, x ∈ s ↔ p x) (f : α → β) : ∏ a ∈ s, f a = ∏ a : Subtype p, f a := by have : (· ∈ s) = p := Set.ext h subst p rw [← prod_coe_sort] congr! @[to_additive] lemma prod_preimage' (f : ι → κ) [DecidablePred (· ∈ Set.range f)] (s : Finset κ) (hf) (g : κ → β) : ∏ x ∈ s.preimage f hf, g (f x) = ∏ x ∈ s.filter (· ∈ Set.range f), g x := by classical calc ∏ x ∈ preimage s f hf, g (f x) = ∏ x ∈ image f (preimage s f hf), g x := Eq.symm <| prod_image <| by simpa only [mem_preimage, Set.InjOn] using hf _ = ∏ x ∈ s.filter fun x => x ∈ Set.range f, g x := by rw [image_preimage] @[to_additive] lemma prod_preimage (f : ι → κ) (s : Finset κ) (hf) (g : κ → β) (hg : ∀ x ∈ s, x ∉ Set.range f → g x = 1) : ∏ x ∈ s.preimage f hf, g (f x) = ∏ x ∈ s, g x := by classical rw [prod_preimage', prod_filter_of_ne]; exact fun x hx ↦ Not.imp_symm (hg x hx) @[to_additive] lemma prod_preimage_of_bij (f : ι → κ) (s : Finset κ) (hf : Set.BijOn f (f ⁻¹' ↑s) ↑s) (g : κ → β) : ∏ x ∈ s.preimage f hf.injOn, g (f x) = ∏ x ∈ s, g x := prod_preimage _ _ hf.injOn g fun _ hs h_f ↦ (h_f <| hf.subset_range hs).elim @[to_additive] theorem prod_set_coe (s : Set α) [Fintype s] : (∏ i : s, f i) = ∏ i ∈ s.toFinset, f i := (Finset.prod_subtype s.toFinset (fun _ ↦ Set.mem_toFinset) f).symm /-- The product of a function `g` defined only on a set `s` is equal to the product of a function `f` defined everywhere, as long as `f` and `g` agree on `s`, and `f = 1` off `s`. -/ @[to_additive "The sum of a function `g` defined only on a set `s` is equal to the sum of a function `f` defined everywhere, as long as `f` and `g` agree on `s`, and `f = 0` off `s`."] theorem prod_congr_set {α : Type*} [CommMonoid α] {β : Type*} [Fintype β] (s : Set β) [DecidablePred (· ∈ s)] (f : β → α) (g : s → α) (w : ∀ (x : β) (h : x ∈ s), f x = g ⟨x, h⟩) (w' : ∀ x : β, x ∉ s → f x = 1) : Finset.univ.prod f = Finset.univ.prod g := by rw [← @Finset.prod_subset _ _ s.toFinset Finset.univ f _ (by simp)] · rw [Finset.prod_subtype] · apply Finset.prod_congr rfl exact fun ⟨x, h⟩ _ => w x h · simp · rintro x _ h exact w' x (by simpa using h) @[to_additive] theorem prod_apply_dite {s : Finset α} {p : α → Prop} {hp : DecidablePred p} [DecidablePred fun x => ¬p x] (f : ∀ x : α, p x → γ) (g : ∀ x : α, ¬p x → γ) (h : γ → β) : (∏ x ∈ s, h (if hx : p x then f x hx else g x hx)) = (∏ x ∈ (s.filter p).attach, h (f x.1 <| by simpa using (mem_filter.mp x.2).2)) * ∏ x ∈ (s.filter fun x => ¬p x).attach, h (g x.1 <| by simpa using (mem_filter.mp x.2).2) := calc (∏ x ∈ s, h (if hx : p x then f x hx else g x hx)) = (∏ x ∈ s.filter p, h (if hx : p x then f x hx else g x hx)) * ∏ x ∈ s.filter (¬p ·), h (if hx : p x then f x hx else g x hx) := (prod_filter_mul_prod_filter_not s p _).symm _ = (∏ x ∈ (s.filter p).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx)) * ∏ x ∈ (s.filter (¬p ·)).attach, h (if hx : p x.1 then f x.1 hx else g x.1 hx) := congr_arg₂ _ (prod_attach _ _).symm (prod_attach _ _).symm _ = (∏ x ∈ (s.filter p).attach, h (f x.1 <| by simpa using (mem_filter.mp x.2).2)) * ∏ x ∈ (s.filter (¬p ·)).attach, h (g x.1 <| by simpa using (mem_filter.mp x.2).2) := congr_arg₂ _ (prod_congr rfl fun x _hx ↦ congr_arg h (dif_pos <| by simpa using (mem_filter.mp x.2).2)) (prod_congr rfl fun x _hx => congr_arg h (dif_neg <| by simpa using (mem_filter.mp x.2).2)) @[to_additive] theorem prod_apply_ite {s : Finset α} {p : α → Prop} {_hp : DecidablePred p} (f g : α → γ) (h : γ → β) : (∏ x ∈ s, h (if p x then f x else g x)) = (∏ x ∈ s.filter p, h (f x)) * ∏ x ∈ s.filter fun x => ¬p x, h (g x) := (prod_apply_dite _ _ _).trans <| congr_arg₂ _ (prod_attach _ (h ∘ f)) (prod_attach _ (h ∘ g)) @[to_additive] theorem prod_dite {s : Finset α} {p : α → Prop} {hp : DecidablePred p} (f : ∀ x : α, p x → β) (g : ∀ x : α, ¬p x → β) : ∏ x ∈ s, (if hx : p x then f x hx else g x hx) = (∏ x ∈ (s.filter p).attach, f x.1 (by simpa using (mem_filter.mp x.2).2)) * ∏ x ∈ (s.filter fun x => ¬p x).attach, g x.1 (by simpa using (mem_filter.mp x.2).2) := by simp [prod_apply_dite _ _ fun x => x] @[to_additive] theorem prod_ite {s : Finset α} {p : α → Prop} {hp : DecidablePred p} (f g : α → β) : ∏ x ∈ s, (if p x then f x else g x) = (∏ x ∈ s.filter p, f x) * ∏ x ∈ s.filter fun x => ¬p x, g x := by simp [prod_apply_ite _ _ fun x => x] @[to_additive] lemma prod_dite_of_false {p : α → Prop} {_ : DecidablePred p} (h : ∀ i ∈ s, ¬ p i) (f : ∀ i, p i → β) (g : ∀ i, ¬ p i → β) : ∏ i ∈ s, (if hi : p i then f i hi else g i hi) = ∏ i : s, g i.1 (h _ i.2) := by refine prod_bij' (fun x hx => ⟨x, hx⟩) (fun x _ ↦ x) ?_ ?_ ?_ ?_ ?_ <;> aesop @[to_additive] lemma prod_ite_of_false {p : α → Prop} {_ : DecidablePred p} (h : ∀ x ∈ s, ¬p x) (f g : α → β) : ∏ x ∈ s, (if p x then f x else g x) = ∏ x ∈ s, g x := (prod_dite_of_false h _ _).trans (prod_attach _ _) @[to_additive] lemma prod_dite_of_true {p : α → Prop} {_ : DecidablePred p} (h : ∀ i ∈ s, p i) (f : ∀ i, p i → β) (g : ∀ i, ¬ p i → β) : ∏ i ∈ s, (if hi : p i then f i hi else g i hi) = ∏ i : s, f i.1 (h _ i.2) := by refine prod_bij' (fun x hx => ⟨x, hx⟩) (fun x _ ↦ x) ?_ ?_ ?_ ?_ ?_ <;> aesop @[to_additive] lemma prod_ite_of_true {p : α → Prop} {_ : DecidablePred p} (h : ∀ x ∈ s, p x) (f g : α → β) : ∏ x ∈ s, (if p x then f x else g x) = ∏ x ∈ s, f x := (prod_dite_of_true h _ _).trans (prod_attach _ _) @[to_additive] theorem prod_apply_ite_of_false {p : α → Prop} {hp : DecidablePred p} (f g : α → γ) (k : γ → β) (h : ∀ x ∈ s, ¬p x) : (∏ x ∈ s, k (if p x then f x else g x)) = ∏ x ∈ s, k (g x) := by simp_rw [apply_ite k] exact prod_ite_of_false h _ _ @[to_additive] theorem prod_apply_ite_of_true {p : α → Prop} {hp : DecidablePred p} (f g : α → γ) (k : γ → β) (h : ∀ x ∈ s, p x) : (∏ x ∈ s, k (if p x then f x else g x)) = ∏ x ∈ s, k (f x) := by simp_rw [apply_ite k] exact prod_ite_of_true h _ _ @[to_additive] theorem prod_extend_by_one [DecidableEq α] (s : Finset α) (f : α → β) : ∏ i ∈ s, (if i ∈ s then f i else 1) = ∏ i ∈ s, f i := (prod_congr rfl) fun _i hi => if_pos hi @[to_additive (attr := simp)] theorem prod_ite_mem [DecidableEq α] (s t : Finset α) (f : α → β) : ∏ i ∈ s, (if i ∈ t then f i else 1) = ∏ i ∈ s ∩ t, f i := by rw [← Finset.prod_filter, Finset.filter_mem_eq_inter] @[to_additive (attr := simp)] theorem prod_dite_eq [DecidableEq α] (s : Finset α) (a : α) (b : ∀ x : α, a = x → β) : ∏ x ∈ s, (if h : a = x then b x h else 1) = ite (a ∈ s) (b a rfl) 1 := by split_ifs with h · rw [Finset.prod_eq_single a, dif_pos rfl] · intros _ _ h rw [dif_neg] exact h.symm · simp [h] · rw [Finset.prod_eq_one] intros rw [dif_neg] rintro rfl contradiction @[to_additive (attr := simp)] theorem prod_dite_eq' [DecidableEq α] (s : Finset α) (a : α) (b : ∀ x : α, x = a → β) : ∏ x ∈ s, (if h : x = a then b x h else 1) = ite (a ∈ s) (b a rfl) 1 := by split_ifs with h · rw [Finset.prod_eq_single a, dif_pos rfl] · intros _ _ h rw [dif_neg] exact h · simp [h] · rw [Finset.prod_eq_one] intros rw [dif_neg] rintro rfl contradiction @[to_additive (attr := simp)] theorem prod_ite_eq [DecidableEq α] (s : Finset α) (a : α) (b : α → β) : (∏ x ∈ s, ite (a = x) (b x) 1) = ite (a ∈ s) (b a) 1 := prod_dite_eq s a fun x _ => b x /-- A product taken over a conditional whose condition is an equality test on the index and whose alternative is `1` has value either the term at that index or `1`. The difference with `Finset.prod_ite_eq` is that the arguments to `Eq` are swapped. -/ @[to_additive (attr := simp) "A sum taken over a conditional whose condition is an equality test on the index and whose alternative is `0` has value either the term at that index or `0`. The difference with `Finset.sum_ite_eq` is that the arguments to `Eq` are swapped."] theorem prod_ite_eq' [DecidableEq α] (s : Finset α) (a : α) (b : α → β) : (∏ x ∈ s, ite (x = a) (b x) 1) = ite (a ∈ s) (b a) 1 := prod_dite_eq' s a fun x _ => b x @[to_additive] theorem prod_ite_index (p : Prop) [Decidable p] (s t : Finset α) (f : α → β) : ∏ x ∈ if p then s else t, f x = if p then ∏ x ∈ s, f x else ∏ x ∈ t, f x := apply_ite (fun s => ∏ x ∈ s, f x) _ _ _ @[to_additive (attr := simp)] theorem prod_ite_irrel (p : Prop) [Decidable p] (s : Finset α) (f g : α → β) : ∏ x ∈ s, (if p then f x else g x) = if p then ∏ x ∈ s, f x else ∏ x ∈ s, g x := by split_ifs with h <;> rfl @[to_additive (attr := simp)] theorem prod_dite_irrel (p : Prop) [Decidable p] (s : Finset α) (f : p → α → β) (g : ¬p → α → β) : ∏ x ∈ s, (if h : p then f h x else g h x) = if h : p then ∏ x ∈ s, f h x else ∏ x ∈ s, g h x := by split_ifs with h <;> rfl @[to_additive (attr := simp)] theorem prod_pi_mulSingle' [DecidableEq α] (a : α) (x : β) (s : Finset α) : ∏ a' ∈ s, Pi.mulSingle a x a' = if a ∈ s then x else 1 := prod_dite_eq' _ _ _ @[to_additive (attr := simp)] theorem prod_pi_mulSingle {β : α → Type*} [DecidableEq α] [∀ a, CommMonoid (β a)] (a : α) (f : ∀ a, β a) (s : Finset α) : (∏ a' ∈ s, Pi.mulSingle a' (f a') a) = if a ∈ s then f a else 1 := prod_dite_eq _ _ _ @[to_additive] lemma mulSupport_prod (s : Finset ι) (f : ι → α → β) : mulSupport (fun x ↦ ∏ i ∈ s, f i x) ⊆ ⋃ i ∈ s, mulSupport (f i) := by simp only [mulSupport_subset_iff', Set.mem_iUnion, not_exists, nmem_mulSupport] exact fun x ↦ prod_eq_one section indicator open Set variable {κ : Type*} /-- Consider a product of `g i (f i)` over a finset. Suppose `g` is a function such as `n ↦ (· ^ n)`, which maps a second argument of `1` to `1`. Then if `f` is replaced by the corresponding multiplicative indicator function, the finset may be replaced by a possibly larger finset without changing the value of the product. -/ @[to_additive "Consider a sum of `g i (f i)` over a finset. Suppose `g` is a function such as `n ↦ (n • ·)`, which maps a second argument of `0` to `0` (or a weighted sum of `f i * h i` or `f i • h i`, where `f` gives the weights that are multiplied by some other function `h`). Then if `f` is replaced by the corresponding indicator function, the finset may be replaced by a possibly larger finset without changing the value of the sum."] lemma prod_mulIndicator_subset_of_eq_one [One α] (f : ι → α) (g : ι → α → β) {s t : Finset ι} (h : s ⊆ t) (hg : ∀ a, g a 1 = 1) : ∏ i ∈ t, g i (mulIndicator ↑s f i) = ∏ i ∈ s, g i (f i) := by calc _ = ∏ i ∈ s, g i (mulIndicator ↑s f i) := by rw [prod_subset h fun i _ hn ↦ by simp [hn, hg]] -- Porting note: This did not use to need the implicit argument _ = _ := prod_congr rfl fun i hi ↦ congr_arg _ <| mulIndicator_of_mem (α := ι) hi f /-- Taking the product of an indicator function over a possibly larger finset is the same as taking the original function over the original finset. -/ @[to_additive "Summing an indicator function over a possibly larger `Finset` is the same as summing the original function over the original finset."] lemma prod_mulIndicator_subset (f : ι → β) {s t : Finset ι} (h : s ⊆ t) : ∏ i ∈ t, mulIndicator (↑s) f i = ∏ i ∈ s, f i := prod_mulIndicator_subset_of_eq_one _ (fun _ ↦ id) h fun _ ↦ rfl @[to_additive] lemma prod_mulIndicator_eq_prod_filter (s : Finset ι) (f : ι → κ → β) (t : ι → Set κ) (g : ι → κ) [DecidablePred fun i ↦ g i ∈ t i] : ∏ i ∈ s, mulIndicator (t i) (f i) (g i) = ∏ i ∈ s.filter fun i ↦ g i ∈ t i, f i (g i) := by refine (prod_filter_mul_prod_filter_not s (fun i ↦ g i ∈ t i) _).symm.trans <| Eq.trans (congr_arg₂ (· * ·) ?_ ?_) (mul_one _) · exact prod_congr rfl fun x hx ↦ mulIndicator_of_mem (mem_filter.1 hx).2 _ · exact prod_eq_one fun x hx ↦ mulIndicator_of_not_mem (mem_filter.1 hx).2 _ @[to_additive] lemma prod_mulIndicator_eq_prod_inter [DecidableEq ι] (s t : Finset ι) (f : ι → β) : ∏ i ∈ s, (t : Set ι).mulIndicator f i = ∏ i ∈ s ∩ t, f i := by rw [← filter_mem_eq_inter, prod_mulIndicator_eq_prod_filter]; rfl @[to_additive] lemma mulIndicator_prod (s : Finset ι) (t : Set κ) (f : ι → κ → β) : mulIndicator t (∏ i ∈ s, f i) = ∏ i ∈ s, mulIndicator t (f i) := map_prod (mulIndicatorHom _ _) _ _ variable {κ : Type*} @[to_additive] lemma mulIndicator_biUnion (s : Finset ι) (t : ι → Set κ) {f : κ → β} : ((s : Set ι).PairwiseDisjoint t) → mulIndicator (⋃ i ∈ s, t i) f = fun a ↦ ∏ i ∈ s, mulIndicator (t i) f a := by classical refine Finset.induction_on s (by simp) fun i s hi ih hs ↦ funext fun j ↦ ?_ rw [prod_insert hi, set_biUnion_insert, mulIndicator_union_of_not_mem_inter, ih (hs.subset <| subset_insert _ _)] simp only [not_exists, exists_prop, mem_iUnion, mem_inter_iff, not_and] exact fun hji i' hi' hji' ↦ (ne_of_mem_of_not_mem hi' hi).symm <| hs.elim_set (mem_insert_self _ _) (mem_insert_of_mem hi') _ hji hji' @[to_additive] lemma mulIndicator_biUnion_apply (s : Finset ι) (t : ι → Set κ) {f : κ → β} (h : (s : Set ι).PairwiseDisjoint t) (x : κ) : mulIndicator (⋃ i ∈ s, t i) f x = ∏ i ∈ s, mulIndicator (t i) f x := by rw [mulIndicator_biUnion s t h] end indicator @[to_additive] theorem prod_bij_ne_one {s : Finset α} {t : Finset γ} {f : α → β} {g : γ → β} (i : ∀ a ∈ s, f a ≠ 1 → γ) (hi : ∀ a h₁ h₂, i a h₁ h₂ ∈ t) (i_inj : ∀ a₁ h₁₁ h₁₂ a₂ h₂₁ h₂₂, i a₁ h₁₁ h₁₂ = i a₂ h₂₁ h₂₂ → a₁ = a₂) (i_surj : ∀ b ∈ t, g b ≠ 1 → ∃ a h₁ h₂, i a h₁ h₂ = b) (h : ∀ a h₁ h₂, f a = g (i a h₁ h₂)) : ∏ x ∈ s, f x = ∏ x ∈ t, g x := by classical calc ∏ x ∈ s, f x = ∏ x ∈ s.filter fun x => f x ≠ 1, f x := by rw [prod_filter_ne_one] _ = ∏ x ∈ t.filter fun x => g x ≠ 1, g x := prod_bij (fun a ha => i a (mem_filter.mp ha).1 <| by simpa using (mem_filter.mp ha).2) ?_ ?_ ?_ ?_ _ = ∏ x ∈ t, g x := prod_filter_ne_one _ · intros a ha refine (mem_filter.mp ha).elim ?_ intros h₁ h₂ refine (mem_filter.mpr ⟨hi a h₁ _, ?_⟩) specialize h a h₁ fun H ↦ by rw [H] at h₂; simp at h₂ rwa [← h] · intros a₁ ha₁ a₂ ha₂ refine (mem_filter.mp ha₁).elim fun _ha₁₁ _ha₁₂ ↦ ?_ refine (mem_filter.mp ha₂).elim fun _ha₂₁ _ha₂₂ ↦ ?_ apply i_inj · intros b hb refine (mem_filter.mp hb).elim fun h₁ h₂ ↦ ?_ obtain ⟨a, ha₁, ha₂, eq⟩ := i_surj b h₁ fun H ↦ by rw [H] at h₂; simp at h₂ exact ⟨a, mem_filter.mpr ⟨ha₁, ha₂⟩, eq⟩ · refine (fun a ha => (mem_filter.mp ha).elim fun h₁ h₂ ↦ ?_) exact h a h₁ fun H ↦ by rw [H] at h₂; simp at h₂ @[to_additive] theorem nonempty_of_prod_ne_one (h : ∏ x ∈ s, f x ≠ 1) : s.Nonempty := s.eq_empty_or_nonempty.elim (fun H => False.elim <| h <| H.symm ▸ prod_empty) id @[to_additive] theorem exists_ne_one_of_prod_ne_one (h : ∏ x ∈ s, f x ≠ 1) : ∃ a ∈ s, f a ≠ 1 := by classical rw [← prod_filter_ne_one] at h rcases nonempty_of_prod_ne_one h with ⟨x, hx⟩ exact ⟨x, (mem_filter.1 hx).1, by simpa using (mem_filter.1 hx).2⟩ @[to_additive] theorem prod_range_succ_comm (f : ℕ → β) (n : ℕ) : (∏ x ∈ range (n + 1), f x) = f n * ∏ x ∈ range n, f x := by rw [range_succ, prod_insert not_mem_range_self] @[to_additive] theorem prod_range_succ (f : ℕ → β) (n : ℕ) : (∏ x ∈ range (n + 1), f x) = (∏ x ∈ range n, f x) * f n := by simp only [mul_comm, prod_range_succ_comm] @[to_additive] theorem prod_range_succ' (f : ℕ → β) : ∀ n : ℕ, (∏ k ∈ range (n + 1), f k) = (∏ k ∈ range n, f (k + 1)) * f 0 | 0 => prod_range_succ _ _ | n + 1 => by rw [prod_range_succ _ n, mul_right_comm, ← prod_range_succ' _ n, prod_range_succ] @[to_additive] theorem eventually_constant_prod {u : ℕ → β} {N : ℕ} (hu : ∀ n ≥ N, u n = 1) {n : ℕ} (hn : N ≤ n) : (∏ k ∈ range n, u k) = ∏ k ∈ range N, u k := by obtain ⟨m, rfl : n = N + m⟩ := Nat.exists_eq_add_of_le hn clear hn induction' m with m hm · simp · simp [← add_assoc, prod_range_succ, hm, hu] @[to_additive] theorem prod_range_add (f : ℕ → β) (n m : ℕ) : (∏ x ∈ range (n + m), f x) = (∏ x ∈ range n, f x) * ∏ x ∈ range m, f (n + x) := by induction' m with m hm · simp · erw [Nat.add_succ, prod_range_succ, prod_range_succ, hm, mul_assoc] @[to_additive] theorem prod_range_add_div_prod_range {α : Type*} [CommGroup α] (f : ℕ → α) (n m : ℕ) : (∏ k ∈ range (n + m), f k) / ∏ k ∈ range n, f k = ∏ k ∈ Finset.range m, f (n + k) := div_eq_of_eq_mul' (prod_range_add f n m) @[to_additive] theorem prod_range_zero (f : ℕ → β) : ∏ k ∈ range 0, f k = 1 := by rw [range_zero, prod_empty] @[to_additive sum_range_one] theorem prod_range_one (f : ℕ → β) : ∏ k ∈ range 1, f k = f 0 := by rw [range_one, prod_singleton] open List @[to_additive] theorem prod_list_map_count [DecidableEq α] (l : List α) {M : Type*} [CommMonoid M] (f : α → M) : (l.map f).prod = ∏ m ∈ l.toFinset, f m ^ l.count m := by induction' l with a s IH; · simp only [map_nil, prod_nil, count_nil, pow_zero, prod_const_one] simp only [List.map, List.prod_cons, toFinset_cons, IH] by_cases has : a ∈ s.toFinset · rw [insert_eq_of_mem has, ← insert_erase has, prod_insert (not_mem_erase _ _), prod_insert (not_mem_erase _ _), ← mul_assoc, count_cons_self, pow_succ'] congr 1 refine prod_congr rfl fun x hx => ?_ rw [count_cons_of_ne (ne_of_mem_erase hx)] rw [prod_insert has, count_cons_self, count_eq_zero_of_not_mem (mt mem_toFinset.2 has), pow_one] congr 1 refine prod_congr rfl fun x hx => ?_ rw [count_cons_of_ne] rintro rfl exact has hx @[to_additive] theorem prod_list_count [DecidableEq α] [CommMonoid α] (s : List α) : s.prod = ∏ m ∈ s.toFinset, m ^ s.count m := by simpa using prod_list_map_count s id @[to_additive] theorem prod_list_count_of_subset [DecidableEq α] [CommMonoid α] (m : List α) (s : Finset α) (hs : m.toFinset ⊆ s) : m.prod = ∏ i ∈ s, i ^ m.count i := by rw [prod_list_count] refine prod_subset hs fun x _ hx => ?_ rw [mem_toFinset] at hx rw [count_eq_zero_of_not_mem hx, pow_zero] theorem sum_filter_count_eq_countP [DecidableEq α] (p : α → Prop) [DecidablePred p] (l : List α) : ∑ x ∈ l.toFinset.filter p, l.count x = l.countP p := by simp [Finset.sum, sum_map_count_dedup_filter_eq_countP p l] open Multiset @[to_additive] theorem prod_multiset_map_count [DecidableEq α] (s : Multiset α) {M : Type*} [CommMonoid M] (f : α → M) : (s.map f).prod = ∏ m ∈ s.toFinset, f m ^ s.count m := by refine Quot.induction_on s fun l => ?_ simp [prod_list_map_count l f] @[to_additive] theorem prod_multiset_count [DecidableEq α] [CommMonoid α] (s : Multiset α) : s.prod = ∏ m ∈ s.toFinset, m ^ s.count m := by convert prod_multiset_map_count s id rw [Multiset.map_id] @[to_additive] theorem prod_multiset_count_of_subset [DecidableEq α] [CommMonoid α] (m : Multiset α) (s : Finset α) (hs : m.toFinset ⊆ s) : m.prod = ∏ i ∈ s, i ^ m.count i := by revert hs refine Quot.induction_on m fun l => ?_ simp only [quot_mk_to_coe'', prod_coe, coe_count] apply prod_list_count_of_subset l s @[to_additive] theorem prod_mem_multiset [DecidableEq α] (m : Multiset α) (f : { x // x ∈ m } → β) (g : α → β) (hfg : ∀ x, f x = g x) : ∏ x : { x // x ∈ m }, f x = ∏ x ∈ m.toFinset, g x := by refine prod_bij' (fun x _ ↦ x) (fun x hx ↦ ⟨x, Multiset.mem_toFinset.1 hx⟩) ?_ ?_ ?_ ?_ ?_ <;> simp [hfg] /-- To prove a property of a product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a sum, it suffices to prove that the property is additive and holds on summands."] theorem prod_induction {M : Type*} [CommMonoid M] (f : α → M) (p : M → Prop) (hom : ∀ a b, p a → p b → p (a * b)) (unit : p 1) (base : ∀ x ∈ s, p <| f x) : p <| ∏ x ∈ s, f x := Multiset.prod_induction _ _ hom unit (Multiset.forall_mem_map_iff.mpr base) /-- To prove a property of a product, it suffices to prove that the property is multiplicative and holds on factors. -/ @[to_additive "To prove a property of a sum, it suffices to prove that the property is additive and holds on summands."] theorem prod_induction_nonempty {M : Type*} [CommMonoid M] (f : α → M) (p : M → Prop) (hom : ∀ a b, p a → p b → p (a * b)) (nonempty : s.Nonempty) (base : ∀ x ∈ s, p <| f x) : p <| ∏ x ∈ s, f x := Multiset.prod_induction_nonempty p hom (by simp [nonempty_iff_ne_empty.mp nonempty]) (Multiset.forall_mem_map_iff.mpr base) /-- For any product along `{0, ..., n - 1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking ratios of adjacent terms. This is a multiplicative discrete analogue of the fundamental theorem of calculus. -/ @[to_additive "For any sum along `{0, ..., n - 1}` of a commutative-monoid-valued function, we can verify that it's equal to a different function just by checking differences of adjacent terms. This is a discrete analogue of the fundamental theorem of calculus."] theorem prod_range_induction (f s : ℕ → β) (base : s 0 = 1) (step : ∀ n, s (n + 1) = s n * f n) (n : ℕ) : ∏ k ∈ Finset.range n, f k = s n := by induction' n with k hk · rw [Finset.prod_range_zero, base] · simp only [hk, Finset.prod_range_succ, step, mul_comm] /-- A telescoping product along `{0, ..., n - 1}` of a commutative group valued function reduces to the ratio of the last and first factors. -/ @[to_additive "A telescoping sum along `{0, ..., n - 1}` of an additive commutative group valued function reduces to the difference of the last and first terms."] theorem prod_range_div {M : Type*} [CommGroup M] (f : ℕ → M) (n : ℕ) : (∏ i ∈ range n, f (i + 1) / f i) = f n / f 0 := by apply prod_range_induction <;> simp @[to_additive] theorem prod_range_div' {M : Type*} [CommGroup M] (f : ℕ → M) (n : ℕ) : (∏ i ∈ range n, f i / f (i + 1)) = f 0 / f n := by apply prod_range_induction <;> simp @[to_additive] theorem eq_prod_range_div {M : Type*} [CommGroup M] (f : ℕ → M) (n : ℕ) : f n = f 0 * ∏ i ∈ range n, f (i + 1) / f i := by rw [prod_range_div, mul_div_cancel] @[to_additive] theorem eq_prod_range_div' {M : Type*} [CommGroup M] (f : ℕ → M) (n : ℕ) : f n = ∏ i ∈ range (n + 1), if i = 0 then f 0 else f i / f (i - 1) := by conv_lhs => rw [Finset.eq_prod_range_div f] simp [Finset.prod_range_succ', mul_comm] /-- A telescoping sum along `{0, ..., n-1}` of an `ℕ`-valued function reduces to the difference of the last and first terms when the function we are summing is monotone. -/ theorem sum_range_tsub [CanonicallyOrderedAddCommMonoid α] [Sub α] [OrderedSub α] [ContravariantClass α α (· + ·) (· ≤ ·)] {f : ℕ → α} (h : Monotone f) (n : ℕ) : ∑ i ∈ range n, (f (i + 1) - f i) = f n - f 0 := by apply sum_range_induction case base => apply tsub_self case step => intro n have h₁ : f n ≤ f (n + 1) := h (Nat.le_succ _) have h₂ : f 0 ≤ f n := h (Nat.zero_le _) rw [tsub_add_eq_add_tsub h₂, add_tsub_cancel_of_le h₁] @[to_additive (attr := simp)] theorem prod_const (b : β) : ∏ _x ∈ s, b = b ^ s.card := (congr_arg _ <| s.val.map_const b).trans <| Multiset.prod_replicate s.card b @[to_additive sum_eq_card_nsmul] theorem prod_eq_pow_card {b : β} (hf : ∀ a ∈ s, f a = b) : ∏ a ∈ s, f a = b ^ s.card := (prod_congr rfl hf).trans <| prod_const _ @[to_additive card_nsmul_add_sum] theorem pow_card_mul_prod {b : β} : b ^ s.card * ∏ a ∈ s, f a = ∏ a ∈ s, b * f a := (Finset.prod_const b).symm ▸ prod_mul_distrib.symm @[to_additive sum_add_card_nsmul] theorem prod_mul_pow_card {b : β} : (∏ a ∈ s, f a) * b ^ s.card = ∏ a ∈ s, f a * b := (Finset.prod_const b).symm ▸ prod_mul_distrib.symm @[to_additive] theorem pow_eq_prod_const (b : β) : ∀ n, b ^ n = ∏ _k ∈ range n, b := by simp @[to_additive] theorem prod_pow (s : Finset α) (n : ℕ) (f : α → β) : ∏ x ∈ s, f x ^ n = (∏ x ∈ s, f x) ^ n := Multiset.prod_map_pow @[to_additive sum_nsmul_assoc] lemma prod_pow_eq_pow_sum (s : Finset ι) (f : ι → ℕ) (a : β) : ∏ i ∈ s, a ^ f i = a ^ ∑ i ∈ s, f i := cons_induction (by simp) (fun _ _ _ _ ↦ by simp [prod_cons, sum_cons, pow_add, *]) s /-- A product over `Finset.powersetCard` which only depends on the size of the sets is constant. -/ @[to_additive "A sum over `Finset.powersetCard` which only depends on the size of the sets is constant."] lemma prod_powersetCard (n : ℕ) (s : Finset α) (f : ℕ → β) : ∏ t ∈ powersetCard n s, f t.card = f n ^ s.card.choose n := by rw [prod_eq_pow_card, card_powersetCard]; rintro a ha; rw [(mem_powersetCard.1 ha).2] @[to_additive] theorem prod_flip {n : ℕ} (f : ℕ → β) : (∏ r ∈ range (n + 1), f (n - r)) = ∏ k ∈ range (n + 1), f k := by induction' n with n ih · rw [prod_range_one, prod_range_one] · rw [prod_range_succ', prod_range_succ _ (Nat.succ n)] simp [← ih] @[to_additive] theorem prod_involution {s : Finset α} {f : α → β} : ∀ (g : ∀ a ∈ s, α) (_ : ∀ a ha, f a * f (g a ha) = 1) (_ : ∀ a ha, f a ≠ 1 → g a ha ≠ a) (g_mem : ∀ a ha, g a ha ∈ s) (_ : ∀ a ha, g (g a ha) (g_mem a ha) = a), ∏ x ∈ s, f x = 1 := by haveI := Classical.decEq α; haveI := Classical.decEq β exact Finset.strongInductionOn s fun s ih g h g_ne g_mem g_inv => s.eq_empty_or_nonempty.elim (fun hs => hs.symm ▸ rfl) fun ⟨x, hx⟩ => have hmem : ∀ y ∈ (s.erase x).erase (g x hx), y ∈ s := fun y hy => mem_of_mem_erase (mem_of_mem_erase hy) have g_inj : ∀ {x hx y hy}, g x hx = g y hy → x = y := fun {x hx y hy} h => by rw [← g_inv x hx, ← g_inv y hy]; simp [h] have ih' : (∏ y ∈ erase (erase s x) (g x hx), f y) = (1 : β) := ih ((s.erase x).erase (g x hx)) ⟨Subset.trans (erase_subset _ _) (erase_subset _ _), fun h => not_mem_erase (g x hx) (s.erase x) (h (g_mem x hx))⟩ (fun y hy => g y (hmem y hy)) (fun y hy => h y (hmem y hy)) (fun y hy => g_ne y (hmem y hy)) (fun y hy => mem_erase.2 ⟨fun h : g y _ = g x hx => by simp [g_inj h] at hy, mem_erase.2 ⟨fun h : g y _ = x => by have : y = g x hx := g_inv y (hmem y hy) ▸ by simp [h] simp [this] at hy, g_mem y (hmem y hy)⟩⟩) fun y hy => g_inv y (hmem y hy) if hx1 : f x = 1 then ih' ▸ Eq.symm (prod_subset hmem fun y hy hy₁ => have : y = x ∨ y = g x hx := by simpa [hy, -not_and, mem_erase, not_and_or, or_comm] using hy₁ this.elim (fun hy => hy.symm ▸ hx1) fun hy => h x hx ▸ hy ▸ hx1.symm ▸ (one_mul _).symm) else by rw [← insert_erase hx, prod_insert (not_mem_erase _ _), ← insert_erase (mem_erase.2 ⟨g_ne x hx hx1, g_mem x hx⟩), prod_insert (not_mem_erase _ _), ih', mul_one, h x hx] /-- The product of the composition of functions `f` and `g`, is the product over `b ∈ s.image g` of `f b` to the power of the cardinality of the fibre of `b`. See also `Finset.prod_image`. -/ @[to_additive "The sum of the composition of functions `f` and `g`, is the sum over `b ∈ s.image g` of `f b` times of the cardinality of the fibre of `b`. See also `Finset.sum_image`."] theorem prod_comp [DecidableEq γ] (f : γ → β) (g : α → γ) : ∏ a ∈ s, f (g a) = ∏ b ∈ s.image g, f b ^ (s.filter fun a => g a = b).card := by simp_rw [← prod_const, prod_fiberwise_of_maps_to' fun _ ↦ mem_image_of_mem _] @[to_additive] theorem prod_piecewise [DecidableEq α] (s t : Finset α) (f g : α → β) : (∏ x ∈ s, (t.piecewise f g) x) = (∏ x ∈ s ∩ t, f x) * ∏ x ∈ s \ t, g x := by erw [prod_ite, filter_mem_eq_inter, ← sdiff_eq_filter] @[to_additive] theorem prod_inter_mul_prod_diff [DecidableEq α] (s t : Finset α) (f : α → β) : (∏ x ∈ s ∩ t, f x) * ∏ x ∈ s \ t, f x = ∏ x ∈ s, f x := by convert (s.prod_piecewise t f f).symm simp (config := { unfoldPartialApp := true }) [Finset.piecewise] @[to_additive] theorem prod_eq_mul_prod_diff_singleton [DecidableEq α] {s : Finset α} {i : α} (h : i ∈ s) (f : α → β) : ∏ x ∈ s, f x = f i * ∏ x ∈ s \ {i}, f x := by convert (s.prod_inter_mul_prod_diff {i} f).symm simp [h] @[to_additive] theorem prod_eq_prod_diff_singleton_mul [DecidableEq α] {s : Finset α} {i : α} (h : i ∈ s) (f : α → β) : ∏ x ∈ s, f x = (∏ x ∈ s \ {i}, f x) * f i := by rw [prod_eq_mul_prod_diff_singleton h, mul_comm] @[to_additive] theorem _root_.Fintype.prod_eq_mul_prod_compl [DecidableEq α] [Fintype α] (a : α) (f : α → β) : ∏ i, f i = f a * ∏ i ∈ {a}ᶜ, f i := prod_eq_mul_prod_diff_singleton (mem_univ a) f @[to_additive] theorem _root_.Fintype.prod_eq_prod_compl_mul [DecidableEq α] [Fintype α] (a : α) (f : α → β) : ∏ i, f i = (∏ i ∈ {a}ᶜ, f i) * f a := prod_eq_prod_diff_singleton_mul (mem_univ a) f theorem dvd_prod_of_mem (f : α → β) {a : α} {s : Finset α} (ha : a ∈ s) : f a ∣ ∏ i ∈ s, f i := by classical rw [Finset.prod_eq_mul_prod_diff_singleton ha] exact dvd_mul_right _ _ /-- A product can be partitioned into a product of products, each equivalent under a setoid. -/ @[to_additive "A sum can be partitioned into a sum of sums, each equivalent under a setoid."] theorem prod_partition (R : Setoid α) [DecidableRel R.r] : ∏ x ∈ s, f x = ∏ xbar ∈ s.image Quotient.mk'', ∏ y ∈ s.filter (⟦·⟧ = xbar), f y := by refine (Finset.prod_image' f fun x _hx => ?_).symm rfl /-- If we can partition a product into subsets that cancel out, then the whole product cancels. -/ @[to_additive "If we can partition a sum into subsets that cancel out, then the whole sum cancels."] theorem prod_cancels_of_partition_cancels (R : Setoid α) [DecidableRel R.r] (h : ∀ x ∈ s, ∏ a ∈ s.filter fun y => y ≈ x, f a = 1) : ∏ x ∈ s, f x = 1 := by rw [prod_partition R, ← Finset.prod_eq_one] intro xbar xbar_in_s obtain ⟨x, x_in_s, rfl⟩ := mem_image.mp xbar_in_s simp only [← Quotient.eq] at h exact h x x_in_s @[to_additive] theorem prod_update_of_not_mem [DecidableEq α] {s : Finset α} {i : α} (h : i ∉ s) (f : α → β) (b : β) : ∏ x ∈ s, Function.update f i b x = ∏ x ∈ s, f x := by apply prod_congr rfl intros j hj have : j ≠ i := by rintro rfl exact h hj simp [this] @[to_additive] theorem prod_update_of_mem [DecidableEq α] {s : Finset α} {i : α} (h : i ∈ s) (f : α → β) (b : β) : ∏ x ∈ s, Function.update f i b x = b * ∏ x ∈ s \ singleton i, f x := by rw [update_eq_piecewise, prod_piecewise] simp [h] /-- If a product of a `Finset` of size at most 1 has a given value, so do the terms in that product. -/ @[to_additive eq_of_card_le_one_of_sum_eq "If a sum of a `Finset` of size at most 1 has a given value, so do the terms in that sum."] theorem eq_of_card_le_one_of_prod_eq {s : Finset α} (hc : s.card ≤ 1) {f : α → β} {b : β} (h : ∏ x ∈ s, f x = b) : ∀ x ∈ s, f x = b := by intro x hx by_cases hc0 : s.card = 0 · exact False.elim (card_ne_zero_of_mem hx hc0) · have h1 : s.card = 1 := le_antisymm hc (Nat.one_le_of_lt (Nat.pos_of_ne_zero hc0)) rw [card_eq_one] at h1 cases' h1 with x2 hx2 rw [hx2, mem_singleton] at hx simp_rw [hx2] at h rw [hx] rw [prod_singleton] at h exact h /-- Taking a product over `s : Finset α` is the same as multiplying the value on a single element `f a` by the product of `s.erase a`. See `Multiset.prod_map_erase` for the `Multiset` version. -/ @[to_additive "Taking a sum over `s : Finset α` is the same as adding the value on a single element `f a` to the sum over `s.erase a`. See `Multiset.sum_map_erase` for the `Multiset` version."] theorem mul_prod_erase [DecidableEq α] (s : Finset α) (f : α → β) {a : α} (h : a ∈ s) : (f a * ∏ x ∈ s.erase a, f x) = ∏ x ∈ s, f x := by rw [← prod_insert (not_mem_erase a s), insert_erase h] /-- A variant of `Finset.mul_prod_erase` with the multiplication swapped. -/ @[to_additive "A variant of `Finset.add_sum_erase` with the addition swapped."] theorem prod_erase_mul [DecidableEq α] (s : Finset α) (f : α → β) {a : α} (h : a ∈ s) : (∏ x ∈ s.erase a, f x) * f a = ∏ x ∈ s, f x := by rw [mul_comm, mul_prod_erase s f h] /-- If a function applied at a point is 1, a product is unchanged by removing that point, if present, from a `Finset`. -/ @[to_additive "If a function applied at a point is 0, a sum is unchanged by removing that point, if present, from a `Finset`."] theorem prod_erase [DecidableEq α] (s : Finset α) {f : α → β} {a : α} (h : f a = 1) : ∏ x ∈ s.erase a, f x = ∏ x ∈ s, f x := by rw [← sdiff_singleton_eq_erase] refine prod_subset sdiff_subset fun x hx hnx => ?_ rw [sdiff_singleton_eq_erase] at hnx rwa [eq_of_mem_of_not_mem_erase hx hnx] /-- See also `Finset.prod_boole`. -/ @[to_additive "See also `Finset.sum_boole`."] theorem prod_ite_one (s : Finset α) (p : α → Prop) [DecidablePred p] (h : ∀ i ∈ s, ∀ j ∈ s, p i → p j → i = j) (a : β) : ∏ i ∈ s, ite (p i) a 1 = ite (∃ i ∈ s, p i) a 1 := by split_ifs with h · obtain ⟨i, hi, hpi⟩ := h rw [prod_eq_single_of_mem _ hi, if_pos hpi] exact fun j hj hji ↦ if_neg fun hpj ↦ hji <| h _ hj _ hi hpj hpi · push_neg at h rw [prod_eq_one] exact fun i hi => if_neg (h i hi) @[to_additive] theorem prod_erase_lt_of_one_lt {γ : Type*} [DecidableEq α] [OrderedCommMonoid γ] [CovariantClass γ γ (· * ·) (· < ·)] {s : Finset α} {d : α} (hd : d ∈ s) {f : α → γ} (hdf : 1 < f d) : ∏ m ∈ s.erase d, f m < ∏ m ∈ s, f m := by conv in ∏ m ∈ s, f m => rw [← Finset.insert_erase hd] rw [Finset.prod_insert (Finset.not_mem_erase d s)] exact lt_mul_of_one_lt_left' _ hdf /-- If a product is 1 and the function is 1 except possibly at one point, it is 1 everywhere on the `Finset`. -/ @[to_additive "If a sum is 0 and the function is 0 except possibly at one point, it is 0 everywhere on the `Finset`."] theorem eq_one_of_prod_eq_one {s : Finset α} {f : α → β} {a : α} (hp : ∏ x ∈ s, f x = 1) (h1 : ∀ x ∈ s, x ≠ a → f x = 1) : ∀ x ∈ s, f x = 1 := by intro x hx classical by_cases h : x = a · rw [h] rw [h] at hx rw [← prod_subset (singleton_subset_iff.2 hx) fun t ht ha => h1 t ht (not_mem_singleton.1 ha), prod_singleton] at hp exact hp · exact h1 x hx h @[to_additive sum_boole_nsmul] theorem prod_pow_boole [DecidableEq α] (s : Finset α) (f : α → β) (a : α) : (∏ x ∈ s, f x ^ ite (a = x) 1 0) = ite (a ∈ s) (f a) 1 := by simp theorem prod_dvd_prod_of_dvd {S : Finset α} (g1 g2 : α → β) (h : ∀ a ∈ S, g1 a ∣ g2 a) : S.prod g1 ∣ S.prod g2 := by classical induction' S using Finset.induction_on' with a T _haS _hTS haT IH · simp · rw [Finset.prod_insert haT, prod_insert haT] exact mul_dvd_mul (h a <| T.mem_insert_self a) <| IH fun b hb ↦ h b <| mem_insert_of_mem hb theorem prod_dvd_prod_of_subset {ι M : Type*} [CommMonoid M] (s t : Finset ι) (f : ι → M) (h : s ⊆ t) : (∏ i ∈ s, f i) ∣ ∏ i ∈ t, f i := Multiset.prod_dvd_prod_of_le <| Multiset.map_le_map <| by simpa @[to_additive] lemma prod_mul_eq_prod_mul_of_exists {s : Finset α} {f : α → β} {b₁ b₂ : β} (a : α) (ha : a ∈ s) (h : f a * b₁ = f a * b₂) : (∏ a ∈ s, f a) * b₁ = (∏ a ∈ s, f a) * b₂ := by classical rw [← insert_erase ha] simp only [mem_erase, ne_eq, not_true_eq_false, false_and, not_false_eq_true, prod_insert] rw [mul_assoc, mul_comm, mul_assoc, mul_comm b₁, h, ← mul_assoc, mul_comm _ (f a)] @[to_additive] lemma isSquare_prod {s : Finset ι} [CommMonoid α] (f : ι → α) (h : ∀ c ∈ s, IsSquare (f c)) : IsSquare (∏ i ∈ s, f i) := by rw [isSquare_iff_exists_sq] use (∏ (x : s), ((isSquare_iff_exists_sq _).mp (h _ x.2)).choose) rw [@sq, ← Finset.prod_mul_distrib, ← Finset.prod_coe_sort] congr ext i rw [← @sq] exact ((isSquare_iff_exists_sq _).mp (h _ i.2)).choose_spec end CommMonoid section CancelCommMonoid variable [DecidableEq ι] [CancelCommMonoid α] {s t : Finset ι} {f : ι → α} @[to_additive] lemma prod_sdiff_eq_prod_sdiff_iff : ∏ i ∈ s \ t, f i = ∏ i ∈ t \ s, f i ↔ ∏ i ∈ s, f i = ∏ i ∈ t, f i := eq_comm.trans $ eq_iff_eq_of_mul_eq_mul $ by rw [← prod_union disjoint_sdiff_self_left, ← prod_union disjoint_sdiff_self_left, sdiff_union_self_eq_union, sdiff_union_self_eq_union, union_comm] @[to_additive] lemma prod_sdiff_ne_prod_sdiff_iff : ∏ i ∈ s \ t, f i ≠ ∏ i ∈ t \ s, f i ↔ ∏ i ∈ s, f i ≠ ∏ i ∈ t, f i := prod_sdiff_eq_prod_sdiff_iff.not end CancelCommMonoid theorem card_eq_sum_ones (s : Finset α) : s.card = ∑ x ∈ s, 1 := by simp theorem sum_const_nat {m : ℕ} {f : α → ℕ} (h₁ : ∀ x ∈ s, f x = m) : ∑ x ∈ s, f x = card s * m := by rw [← Nat.nsmul_eq_mul, ← sum_const] apply sum_congr rfl h₁ lemma sum_card_fiberwise_eq_card_filter {κ : Type*} [DecidableEq κ] (s : Finset ι) (t : Finset κ) (g : ι → κ) : ∑ j ∈ t, (s.filter fun i ↦ g i = j).card = (s.filter fun i ↦ g i ∈ t).card := by simpa only [card_eq_sum_ones] using sum_fiberwise_eq_sum_filter _ _ _ _ lemma card_filter (p) [DecidablePred p] (s : Finset α) : (filter p s).card = ∑ a ∈ s, ite (p a) 1 0 := by simp [sum_ite] section Opposite open MulOpposite /-- Moving to the opposite additive commutative monoid commutes with summing. -/ @[simp] theorem op_sum [AddCommMonoid β] {s : Finset α} (f : α → β) : op (∑ x ∈ s, f x) = ∑ x ∈ s, op (f x) := map_sum (opAddEquiv : β ≃+ βᵐᵒᵖ) _ _ @[simp] theorem unop_sum [AddCommMonoid β] {s : Finset α} (f : α → βᵐᵒᵖ) : unop (∑ x ∈ s, f x) = ∑ x ∈ s, unop (f x) := map_sum (opAddEquiv : β ≃+ βᵐᵒᵖ).symm _ _ end Opposite section DivisionCommMonoid variable [DivisionCommMonoid β] @[to_additive (attr := simp)] theorem prod_inv_distrib : (∏ x ∈ s, (f x)⁻¹) = (∏ x ∈ s, f x)⁻¹ := Multiset.prod_map_inv @[to_additive (attr := simp)] theorem prod_div_distrib : ∏ x ∈ s, f x / g x = (∏ x ∈ s, f x) / ∏ x ∈ s, g x := Multiset.prod_map_div @[to_additive] theorem prod_zpow (f : α → β) (s : Finset α) (n : ℤ) : ∏ a ∈ s, f a ^ n = (∏ a ∈ s, f a) ^ n := Multiset.prod_map_zpow end DivisionCommMonoid section CommGroup variable [CommGroup β] [DecidableEq α] @[to_additive (attr := simp)] theorem prod_sdiff_eq_div (h : s₁ ⊆ s₂) : ∏ x ∈ s₂ \ s₁, f x = (∏ x ∈ s₂, f x) / ∏ x ∈ s₁, f x := by rw [eq_div_iff_mul_eq', prod_sdiff h] @[to_additive] theorem prod_sdiff_div_prod_sdiff : (∏ x ∈ s₂ \ s₁, f x) / ∏ x ∈ s₁ \ s₂, f x = (∏ x ∈ s₂, f x) / ∏ x ∈ s₁, f x := by simp [← Finset.prod_sdiff (@inf_le_left _ _ s₁ s₂), ← Finset.prod_sdiff (@inf_le_right _ _ s₁ s₂)] @[to_additive (attr := simp)] theorem prod_erase_eq_div {a : α} (h : a ∈ s) : ∏ x ∈ s.erase a, f x = (∏ x ∈ s, f x) / f a := by rw [eq_div_iff_mul_eq', prod_erase_mul _ _ h] end CommGroup @[simp] theorem card_sigma {σ : α → Type*} (s : Finset α) (t : ∀ a, Finset (σ a)) : card (s.sigma t) = ∑ a ∈ s, card (t a) := Multiset.card_sigma _ _ @[simp] theorem card_disjiUnion (s : Finset α) (t : α → Finset β) (h) : (s.disjiUnion t h).card = s.sum fun i => (t i).card := Multiset.card_bind _ _ theorem card_biUnion [DecidableEq β] {s : Finset α} {t : α → Finset β} (h : ∀ x ∈ s, ∀ y ∈ s, x ≠ y → Disjoint (t x) (t y)) : (s.biUnion t).card = ∑ u ∈ s, card (t u) := calc (s.biUnion t).card = ∑ i ∈ s.biUnion t, 1 := card_eq_sum_ones _ _ = ∑ a ∈ s, ∑ _i ∈ t a, 1 := Finset.sum_biUnion h _ = ∑ u ∈ s, card (t u) := by simp_rw [card_eq_sum_ones] theorem card_biUnion_le [DecidableEq β] {s : Finset α} {t : α → Finset β} : (s.biUnion t).card ≤ ∑ a ∈ s, (t a).card := haveI := Classical.decEq α Finset.induction_on s (by simp) fun a s has ih => calc ((insert a s).biUnion t).card ≤ (t a).card + (s.biUnion t).card := by { rw [biUnion_insert]; exact Finset.card_union_le _ _ } _ ≤ ∑ a ∈ insert a s, card (t a) := by rw [sum_insert has]; exact Nat.add_le_add_left ih _ theorem card_eq_sum_card_fiberwise [DecidableEq β] {f : α → β} {s : Finset α} {t : Finset β} (H : ∀ x ∈ s, f x ∈ t) : s.card = ∑ a ∈ t, (s.filter fun x => f x = a).card := by simp only [card_eq_sum_ones, sum_fiberwise_of_maps_to H] theorem card_eq_sum_card_image [DecidableEq β] (f : α → β) (s : Finset α) : s.card = ∑ a ∈ s.image f, (s.filter fun x => f x = a).card := card_eq_sum_card_fiberwise fun _ => mem_image_of_mem _ theorem mem_sum {f : α → Multiset β} (s : Finset α) (b : β) : (b ∈ ∑ x ∈ s, f x) ↔ ∃ a ∈ s, b ∈ f a := by classical refine s.induction_on (by simp) ?_ intro a t hi ih simp [sum_insert hi, ih, or_and_right, exists_or] @[to_additive] theorem prod_unique_nonempty {α β : Type*} [CommMonoid β] [Unique α] (s : Finset α) (f : α → β) (h : s.Nonempty) : ∏ x ∈ s, f x = f default := by rw [h.eq_singleton_default, Finset.prod_singleton] theorem sum_nat_mod (s : Finset α) (n : ℕ) (f : α → ℕ) : (∑ i ∈ s, f i) % n = (∑ i ∈ s, f i % n) % n := (Multiset.sum_nat_mod _ _).trans <| by rw [Finset.sum, Multiset.map_map]; rfl theorem prod_nat_mod (s : Finset α) (n : ℕ) (f : α → ℕ) : (∏ i ∈ s, f i) % n = (∏ i ∈ s, f i % n) % n := (Multiset.prod_nat_mod _ _).trans <| by rw [Finset.prod, Multiset.map_map]; rfl theorem sum_int_mod (s : Finset α) (n : ℤ) (f : α → ℤ) : (∑ i ∈ s, f i) % n = (∑ i ∈ s, f i % n) % n := (Multiset.sum_int_mod _ _).trans <| by rw [Finset.sum, Multiset.map_map]; rfl theorem prod_int_mod (s : Finset α) (n : ℤ) (f : α → ℤ) : (∏ i ∈ s, f i) % n = (∏ i ∈ s, f i % n) % n := (Multiset.prod_int_mod _ _).trans <| by rw [Finset.prod, Multiset.map_map]; rfl end Finset namespace Fintype variable {ι κ α : Type*} [Fintype ι] [Fintype κ] open Finset section CommMonoid variable [CommMonoid α] /-- `Fintype.prod_bijective` is a variant of `Finset.prod_bij` that accepts `Function.Bijective`. See `Function.Bijective.prod_comp` for a version without `h`. -/ @[to_additive "`Fintype.sum_bijective` is a variant of `Finset.sum_bij` that accepts `Function.Bijective`. See `Function.Bijective.sum_comp` for a version without `h`. "] lemma prod_bijective (e : ι → κ) (he : e.Bijective) (f : ι → α) (g : κ → α) (h : ∀ x, f x = g (e x)) : ∏ x, f x = ∏ x, g x := prod_equiv (.ofBijective e he) (by simp) (by simp [h]) @[to_additive] alias _root_.Function.Bijective.finset_prod := prod_bijective /-- `Fintype.prod_equiv` is a specialization of `Finset.prod_bij` that automatically fills in most arguments. See `Equiv.prod_comp` for a version without `h`. -/ @[to_additive "`Fintype.sum_equiv` is a specialization of `Finset.sum_bij` that automatically fills in most arguments. See `Equiv.sum_comp` for a version without `h`."] lemma prod_equiv (e : ι ≃ κ) (f : ι → α) (g : κ → α) (h : ∀ x, f x = g (e x)) : ∏ x, f x = ∏ x, g x := prod_bijective _ e.bijective _ _ h @[to_additive] lemma _root_.Function.Bijective.prod_comp {e : ι → κ} (he : e.Bijective) (g : κ → α) : ∏ i, g (e i) = ∏ i, g i := prod_bijective _ he _ _ fun _ ↦ rfl @[to_additive] lemma _root_.Equiv.prod_comp (e : ι ≃ κ) (g : κ → α) : ∏ i, g (e i) = ∏ i, g i := prod_equiv e _ _ fun _ ↦ rfl @[to_additive] lemma prod_of_injective (e : ι → κ) (he : Injective e) (f : ι → α) (g : κ → α) (h' : ∀ i ∉ Set.range e, g i = 1) (h : ∀ i, f i = g (e i)) : ∏ i, f i = ∏ j, g j := prod_of_injOn e he.injOn (by simp) (by simpa using h') (fun i _ ↦ h i) @[to_additive] lemma prod_fiberwise [DecidableEq κ] (g : ι → κ) (f : ι → α) : ∏ j, ∏ i : {i // g i = j}, f i = ∏ i, f i := by rw [← Finset.prod_fiberwise _ g f] congr with j exact (prod_subtype _ (by simp) _).symm @[to_additive] lemma prod_fiberwise' [DecidableEq κ] (g : ι → κ) (f : κ → α) : ∏ j, ∏ _i : {i // g i = j}, f j = ∏ i, f (g i) := by rw [← Finset.prod_fiberwise' _ g f] congr with j exact (prod_subtype _ (by simp) fun _ ↦ _).symm @[to_additive] theorem prod_unique {α β : Type*} [CommMonoid β] [Unique α] [Fintype α] (f : α → β) : ∏ x : α, f x = f default := by rw [univ_unique, prod_singleton] @[to_additive] theorem prod_empty {α β : Type*} [CommMonoid β] [IsEmpty α] [Fintype α] (f : α → β) : ∏ x : α, f x = 1 := Finset.prod_of_isEmpty _ @[to_additive] theorem prod_subsingleton {α β : Type*} [CommMonoid β] [Subsingleton α] [Fintype α] (f : α → β) (a : α) : ∏ x : α, f x = f a := by have : Unique α := uniqueOfSubsingleton a rw [prod_unique f, Subsingleton.elim default a] @[to_additive] theorem prod_subtype_mul_prod_subtype {α β : Type*} [Fintype α] [CommMonoid β] (p : α → Prop) (f : α → β) [DecidablePred p] : (∏ i : { x // p x }, f i) * ∏ i : { x // ¬p x }, f i = ∏ i, f i := by classical let s := { x | p x }.toFinset rw [← Finset.prod_subtype s, ← Finset.prod_subtype sᶜ] · exact Finset.prod_mul_prod_compl _ _ · simp [s] · simp [s] @[to_additive] lemma prod_subset {s : Finset ι} {f : ι → α} (h : ∀ i, f i ≠ 1 → i ∈ s) : ∏ i ∈ s, f i = ∏ i, f i := Finset.prod_subset s.subset_univ $ by simpa [not_imp_comm (a := _ ∈ s)] @[to_additive] lemma prod_ite_eq_ite_exists (p : ι → Prop) [DecidablePred p] (h : ∀ i j, p i → p j → i = j) (a : α) : ∏ i, ite (p i) a 1 = ite (∃ i, p i) a 1 := by simp [prod_ite_one univ p (by simpa using h)] variable [DecidableEq ι] /-- See also `Finset.prod_dite_eq`. -/ @[to_additive "See also `Finset.sum_dite_eq`."] lemma prod_dite_eq (i : ι) (f : ∀ j, i = j → α) : ∏ j, (if h : i = j then f j h else 1) = f i rfl := by rw [Finset.prod_dite_eq, if_pos (mem_univ _)] /-- See also `Finset.prod_dite_eq'`. -/ @[to_additive "See also `Finset.sum_dite_eq'`."] lemma prod_dite_eq' (i : ι) (f : ∀ j, j = i → α) : ∏ j, (if h : j = i then f j h else 1) = f i rfl := by rw [Finset.prod_dite_eq', if_pos (mem_univ _)] /-- See also `Finset.prod_ite_eq`. -/ @[to_additive "See also `Finset.sum_ite_eq`."] lemma prod_ite_eq (i : ι) (f : ι → α) : ∏ j, (if i = j then f j else 1) = f i := by rw [Finset.prod_ite_eq, if_pos (mem_univ _)] /-- See also `Finset.prod_ite_eq'`. -/ @[to_additive "See also `Finset.sum_ite_eq'`."] lemma prod_ite_eq' (i : ι) (f : ι → α) : ∏ j, (if j = i then f j else 1) = f i := by rw [Finset.prod_ite_eq', if_pos (mem_univ _)] /-- See also `Finset.prod_pi_mulSingle`. -/ @[to_additive "See also `Finset.sum_pi_single`."] lemma prod_pi_mulSingle {α : ι → Type*} [∀ i, CommMonoid (α i)] (i : ι) (f : ∀ i, α i) : ∏ j, Pi.mulSingle j (f j) i = f i := prod_dite_eq _ _ /-- See also `Finset.prod_pi_mulSingle'`. -/ @[to_additive "See also `Finset.sum_pi_single'`."] lemma prod_pi_mulSingle' (i : ι) (a : α) : ∏ j, Pi.mulSingle i a j = a := prod_dite_eq' _ _ end CommMonoid end Fintype namespace Finset variable [CommMonoid α] @[to_additive (attr := simp)] lemma prod_attach_univ [Fintype ι] (f : {i // i ∈ @univ ι _} → α) : ∏ i ∈ univ.attach, f i = ∏ i, f ⟨i, mem_univ _⟩ := Fintype.prod_equiv (Equiv.subtypeUnivEquiv mem_univ) _ _ $ by simp @[to_additive] theorem prod_erase_attach [DecidableEq ι] {s : Finset ι} (f : ι → α) (i : ↑s) : ∏ j ∈ s.attach.erase i, f ↑j = ∏ j ∈ s.erase ↑i, f j := by rw [← Function.Embedding.coe_subtype, ← prod_map] simp [attach_map_val] end Finset namespace List @[to_additive] theorem prod_toFinset {M : Type*} [DecidableEq α] [CommMonoid M] (f : α → M) : ∀ {l : List α} (_hl : l.Nodup), l.toFinset.prod f = (l.map f).prod | [], _ => by simp | a :: l, hl => by let ⟨not_mem, hl⟩ := List.nodup_cons.mp hl simp [Finset.prod_insert (mt List.mem_toFinset.mp not_mem), prod_toFinset _ hl] @[simp] theorem sum_toFinset_count_eq_length [DecidableEq α] (l : List α) : ∑ a in l.toFinset, l.count a = l.length := by simpa [List.map_const'] using (Finset.sum_list_map_count l fun _ => (1 : ℕ)).symm end List namespace Multiset theorem disjoint_list_sum_left {a : Multiset α} {l : List (Multiset α)} : Multiset.Disjoint l.sum a ↔ ∀ b ∈ l, Multiset.Disjoint b a := by induction' l with b bs ih · simp only [zero_disjoint, List.not_mem_nil, IsEmpty.forall_iff, forall_const, List.sum_nil] · simp_rw [List.sum_cons, disjoint_add_left, List.mem_cons, forall_eq_or_imp] simp [and_congr_left_iff, iff_self_iff, ih] theorem disjoint_list_sum_right {a : Multiset α} {l : List (Multiset α)} : Multiset.Disjoint a l.sum ↔ ∀ b ∈ l, Multiset.Disjoint a b := by simpa only [@disjoint_comm _ a] using disjoint_list_sum_left theorem disjoint_sum_left {a : Multiset α} {i : Multiset (Multiset α)} : Multiset.Disjoint i.sum a ↔ ∀ b ∈ i, Multiset.Disjoint b a := Quotient.inductionOn i fun l => by rw [quot_mk_to_coe, Multiset.sum_coe] exact disjoint_list_sum_left theorem disjoint_sum_right {a : Multiset α} {i : Multiset (Multiset α)} : Multiset.Disjoint a i.sum ↔ ∀ b ∈ i, Multiset.Disjoint a b := by simpa only [@disjoint_comm _ a] using disjoint_sum_left theorem disjoint_finset_sum_left {β : Type*} {i : Finset β} {f : β → Multiset α} {a : Multiset α} : Multiset.Disjoint (i.sum f) a ↔ ∀ b ∈ i, Multiset.Disjoint (f b) a := by convert @disjoint_sum_left _ a (map f i.val) simp [and_congr_left_iff, iff_self_iff] theorem disjoint_finset_sum_right {β : Type*} {i : Finset β} {f : β → Multiset α} {a : Multiset α} : Multiset.Disjoint a (i.sum f) ↔ ∀ b ∈ i, Multiset.Disjoint a (f b) := by simpa only [disjoint_comm] using disjoint_finset_sum_left @[simp] lemma mem_sum {s : Finset ι} {m : ι → Multiset α} : a ∈ ∑ i ∈ s, m i ↔ ∃ i ∈ s, a ∈ m i := by induction' s using Finset.cons_induction <;> simp [*] variable [DecidableEq α] theorem toFinset_sum_count_eq (s : Multiset α) : ∑ a in s.toFinset, s.count a = card s := by simpa using (Finset.sum_multiset_map_count s (fun _ => (1 : ℕ))).symm @[simp] lemma sum_count_eq_card {s : Finset α} {m : Multiset α} (hms : ∀ a ∈ m, a ∈ s) : ∑ a ∈ s, m.count a = card m := by rw [← toFinset_sum_count_eq, ← Finset.sum_filter_ne_zero] congr with a simpa using hms a @[deprecated sum_count_eq_card (since := "2024-07-21")] theorem sum_count_eq [Fintype α] (s : Multiset α) : ∑ a, s.count a = Multiset.card s := by simp theorem count_sum' {s : Finset β} {a : α} {f : β → Multiset α} : count a (∑ x ∈ s, f x) = ∑ x ∈ s, count a (f x) := by dsimp only [Finset.sum] rw [count_sum] @[simp] theorem toFinset_sum_count_nsmul_eq (s : Multiset α) : ∑ a ∈ s.toFinset, s.count a • {a} = s := by rw [← Finset.sum_multiset_map_count, Multiset.sum_map_singleton] theorem exists_smul_of_dvd_count (s : Multiset α) {k : ℕ} (h : ∀ a : α, a ∈ s → k ∣ Multiset.count a s) : ∃ u : Multiset α, s = k • u := by use ∑ a ∈ s.toFinset, (s.count a / k) • {a} have h₂ : (∑ x ∈ s.toFinset, k • (count x s / k) • ({x} : Multiset α)) = ∑ x ∈ s.toFinset, count x s • {x} := by apply Finset.sum_congr rfl intro x hx rw [← mul_nsmul', Nat.mul_div_cancel' (h x (mem_toFinset.mp hx))] rw [← Finset.sum_nsmul, h₂, toFinset_sum_count_nsmul_eq] theorem toFinset_prod_dvd_prod [CommMonoid α] (S : Multiset α) : S.toFinset.prod id ∣ S.prod := by rw [Finset.prod_eq_multiset_prod] refine Multiset.prod_dvd_prod_of_le ?_ simp [Multiset.dedup_le S] @[to_additive] theorem prod_sum {α : Type*} {ι : Type*} [CommMonoid α] (f : ι → Multiset α) (s : Finset ι) : (∑ x ∈ s, f x).prod = ∏ x ∈ s, (f x).prod := by classical induction' s using Finset.induction_on with a t hat ih · rw [Finset.sum_empty, Finset.prod_empty, Multiset.prod_zero] · rw [Finset.sum_insert hat, Finset.prod_insert hat, Multiset.prod_add, ih] end Multiset @[simp, norm_cast] theorem Units.coe_prod {M : Type*} [CommMonoid M] (f : α → Mˣ) (s : Finset α) : (↑(∏ i ∈ s, f i) : M) = ∏ i ∈ s, (f i : M) := map_prod (Units.coeHom M) _ _ theorem nat_abs_sum_le {ι : Type*} (s : Finset ι) (f : ι → ℤ) : (∑ i ∈ s, f i).natAbs ≤ ∑ i ∈ s, (f i).natAbs := by classical induction' s using Finset.induction_on with i s his IH · simp only [Finset.sum_empty, Int.natAbs_zero, le_refl] · simp only [his, Finset.sum_insert, not_false_iff] exact (Int.natAbs_add_le _ _).trans (Nat.add_le_add_left IH _) /-! ### `Additive`, `Multiplicative` -/ open Additive Multiplicative section Monoid variable [Monoid α] @[simp] theorem ofMul_list_prod (s : List α) : ofMul s.prod = (s.map ofMul).sum := by simp [ofMul]; rfl @[simp] theorem toMul_list_sum (s : List (Additive α)) : toMul s.sum = (s.map toMul).prod := by simp [toMul, ofMul]; rfl end Monoid section AddMonoid variable [AddMonoid α] @[simp] theorem ofAdd_list_prod (s : List α) : ofAdd s.sum = (s.map ofAdd).prod := by simp [ofAdd]; rfl @[simp] theorem toAdd_list_sum (s : List (Multiplicative α)) : toAdd s.prod = (s.map toAdd).sum := by simp [toAdd, ofAdd]; rfl end AddMonoid section CommMonoid variable [CommMonoid α] @[simp] theorem ofMul_multiset_prod (s : Multiset α) : ofMul s.prod = (s.map ofMul).sum := by simp [ofMul]; rfl @[simp] theorem toMul_multiset_sum (s : Multiset (Additive α)) : toMul s.sum = (s.map toMul).prod := by simp [toMul, ofMul]; rfl @[simp] theorem ofMul_prod (s : Finset ι) (f : ι → α) : ofMul (∏ i ∈ s, f i) = ∑ i ∈ s, ofMul (f i) := rfl @[simp] theorem toMul_sum (s : Finset ι) (f : ι → Additive α) : toMul (∑ i ∈ s, f i) = ∏ i ∈ s, toMul (f i) := rfl end CommMonoid section AddCommMonoid variable [AddCommMonoid α] @[simp] theorem ofAdd_multiset_prod (s : Multiset α) : ofAdd s.sum = (s.map ofAdd).prod := by simp [ofAdd]; rfl @[simp] theorem toAdd_multiset_sum (s : Multiset (Multiplicative α)) : toAdd s.prod = (s.map toAdd).sum := by simp [toAdd, ofAdd]; rfl @[simp] theorem ofAdd_sum (s : Finset ι) (f : ι → α) : ofAdd (∑ i ∈ s, f i) = ∏ i ∈ s, ofAdd (f i) := rfl @[simp] theorem toAdd_prod (s : Finset ι) (f : ι → Multiplicative α) : toAdd (∏ i ∈ s, f i) = ∑ i ∈ s, toAdd (f i) := rfl end AddCommMonoid @[deprecated (since := "2023-12-23")] alias Equiv.prod_comp' := Fintype.prod_equiv @[deprecated (since := "2023-12-23")] alias Equiv.sum_comp' := Fintype.sum_equiv theorem Finset.sum_sym2_filter_not_isDiag {ι α} [LinearOrder ι] [AddCommMonoid α] (s : Finset ι) (p : Sym2 ι → α) : ∑ i in s.sym2.filter (¬ ·.IsDiag), p i = ∑ i in s.offDiag.filter (fun i => i.1 < i.2), p s(i.1, i.2) := by rw [Finset.offDiag_filter_lt_eq_filter_le] conv_rhs => rw [← Finset.sum_subtype_eq_sum_filter] refine (Finset.sum_equiv Sym2.sortEquiv.symm ?_ ?_).symm · rintro ⟨⟨i₁, j₁⟩, hij₁⟩ simp [and_assoc] · rintro ⟨⟨i₁, j₁⟩, hij₁⟩ simp
Algebra\BigOperators\Group\List.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Floris van Doorn, Sébastien Gouëzel, Alex J. Best -/ import Mathlib.Algebra.Divisibility.Basic import Mathlib.Algebra.Group.Int import Mathlib.Algebra.Group.Nat import Mathlib.Algebra.Group.Opposite import Mathlib.Algebra.Group.Units import Mathlib.Data.List.Perm import Mathlib.Data.List.ProdSigma import Mathlib.Data.List.Range import Mathlib.Data.List.Rotate /-! # Sums and products from lists This file provides basic results about `List.prod`, `List.sum`, which calculate the product and sum of elements of a list and `List.alternatingProd`, `List.alternatingSum`, their alternating counterparts. -/ -- Make sure we haven't imported `Data.Nat.Order.Basic` assert_not_exists OrderedSub assert_not_exists Ring variable {ι α β M N P G : Type*} namespace List section Defs /-- Product of a list. `List.prod [a, b, c] = ((1 * a) * b) * c` -/ @[to_additive "Sum of a list.\n\n`List.sum [a, b, c] = ((0 + a) + b) + c`"] def prod {α} [Mul α] [One α] : List α → α := foldl (· * ·) 1 /-- The alternating sum of a list. -/ def alternatingSum {G : Type*} [Zero G] [Add G] [Neg G] : List G → G | [] => 0 | g :: [] => g | g :: h :: t => g + -h + alternatingSum t /-- The alternating product of a list. -/ @[to_additive existing] def alternatingProd {G : Type*} [One G] [Mul G] [Inv G] : List G → G | [] => 1 | g :: [] => g | g :: h :: t => g * h⁻¹ * alternatingProd t end Defs section MulOneClass variable [MulOneClass M] {l : List M} {a : M} @[to_additive (attr := simp)] theorem prod_nil : ([] : List M).prod = 1 := rfl @[to_additive] theorem prod_singleton : [a].prod = a := one_mul a @[to_additive (attr := simp)] theorem prod_one_cons : (1 :: l).prod = l.prod := by rw [prod, foldl, mul_one] @[to_additive] theorem prod_map_one {l : List ι} : (l.map fun _ => (1 : M)).prod = 1 := by induction l with | nil => rfl | cons hd tl ih => rw [map_cons, prod_one_cons, ih] end MulOneClass section Monoid variable [Monoid M] [Monoid N] [Monoid P] {l l₁ l₂ : List M} {a : M} @[to_additive (attr := simp)] theorem prod_cons : (a :: l).prod = a * l.prod := calc (a :: l).prod = foldl (· * ·) (a * 1) l := by simp only [List.prod, foldl_cons, one_mul, mul_one] _ = _ := foldl_assoc @[to_additive] lemma prod_induction (p : M → Prop) (hom : ∀ a b, p a → p b → p (a * b)) (unit : p 1) (base : ∀ x ∈ l, p x) : p l.prod := by induction' l with a l ih · simpa rw [List.prod_cons] simp only [Bool.not_eq_true, List.mem_cons, forall_eq_or_imp] at base exact hom _ _ (base.1) (ih base.2) @[to_additive (attr := simp)] theorem prod_append : (l₁ ++ l₂).prod = l₁.prod * l₂.prod := calc (l₁ ++ l₂).prod = foldl (· * ·) (foldl (· * ·) 1 l₁ * 1) l₂ := by simp [List.prod] _ = l₁.prod * l₂.prod := foldl_assoc @[to_additive] theorem prod_concat : (l.concat a).prod = l.prod * a := by rw [concat_eq_append, prod_append, prod_singleton] @[to_additive (attr := simp)] theorem prod_join {l : List (List M)} : l.join.prod = (l.map List.prod).prod := by induction l <;> [rfl; simp only [*, List.join, map, prod_append, prod_cons]] @[to_additive] theorem prod_eq_foldr : ∀ {l : List M}, l.prod = foldr (· * ·) 1 l | [] => rfl | cons a l => by rw [prod_cons, foldr_cons, prod_eq_foldr] @[to_additive (attr := simp)] theorem prod_replicate (n : ℕ) (a : M) : (replicate n a).prod = a ^ n := by induction' n with n ih · rw [pow_zero] rfl · rw [replicate_succ, prod_cons, ih, pow_succ'] @[to_additive sum_eq_card_nsmul] theorem prod_eq_pow_card (l : List M) (m : M) (h : ∀ x ∈ l, x = m) : l.prod = m ^ l.length := by rw [← prod_replicate, ← List.eq_replicate.mpr ⟨rfl, h⟩] @[to_additive] theorem prod_hom_rel (l : List ι) {r : M → N → Prop} {f : ι → M} {g : ι → N} (h₁ : r 1 1) (h₂ : ∀ ⦃i a b⦄, r a b → r (f i * a) (g i * b)) : r (l.map f).prod (l.map g).prod := List.recOn l h₁ fun a l hl => by simp only [map_cons, prod_cons, h₂ hl] @[to_additive] theorem rel_prod {R : M → N → Prop} (h : R 1 1) (hf : (R ⇒ R ⇒ R) (· * ·) (· * ·)) : (Forall₂ R ⇒ R) prod prod := rel_foldl hf h @[to_additive] theorem prod_hom (l : List M) {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) : (l.map f).prod = f l.prod := by simp only [prod, foldl_map, ← map_one f] exact l.foldl_hom f (· * ·) (· * f ·) 1 (fun x y => (map_mul f x y).symm) @[to_additive] theorem prod_hom₂ (l : List ι) (f : M → N → P) (hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (hf' : f 1 1 = 1) (f₁ : ι → M) (f₂ : ι → N) : (l.map fun i => f (f₁ i) (f₂ i)).prod = f (l.map f₁).prod (l.map f₂).prod := by simp only [prod, foldl_map] -- Porting note: next 3 lines used to be -- convert l.foldl_hom₂ (fun a b => f a b) _ _ _ _ _ fun a b i => _ -- · exact hf'.symm -- · exact hf _ _ _ _ rw [← l.foldl_hom₂ (fun a b => f a b), hf'] intros exact hf _ _ _ _ @[to_additive (attr := simp)] theorem prod_map_mul {α : Type*} [CommMonoid α] {l : List ι} {f g : ι → α} : (l.map fun i => f i * g i).prod = (l.map f).prod * (l.map g).prod := l.prod_hom₂ (· * ·) mul_mul_mul_comm (mul_one _) _ _ @[to_additive] theorem prod_map_hom (L : List ι) (f : ι → M) {G : Type*} [FunLike G M N] [MonoidHomClass G M N] (g : G) : (L.map (g ∘ f)).prod = g (L.map f).prod := by rw [← prod_hom, map_map] @[to_additive] theorem prod_isUnit : ∀ {L : List M}, (∀ m ∈ L, IsUnit m) → IsUnit L.prod | [], _ => by simp | h :: t, u => by simp only [List.prod_cons] exact IsUnit.mul (u h (mem_cons_self h t)) (prod_isUnit fun m mt => u m (mem_cons_of_mem h mt)) @[to_additive] theorem prod_isUnit_iff {α : Type*} [CommMonoid α] {L : List α} : IsUnit L.prod ↔ ∀ m ∈ L, IsUnit m := by refine ⟨fun h => ?_, prod_isUnit⟩ induction' L with m L ih · exact fun m' h' => False.elim (not_mem_nil m' h') rw [prod_cons, IsUnit.mul_iff] at h exact fun m' h' => Or.elim (eq_or_mem_of_mem_cons h') (fun H => H.substr h.1) fun H => ih h.2 _ H @[to_additive (attr := simp)] theorem prod_take_mul_prod_drop : ∀ (L : List M) (i : ℕ), (L.take i).prod * (L.drop i).prod = L.prod | [], i => by simp [Nat.zero_le] | L, 0 => by simp | h :: t, n + 1 => by dsimp rw [prod_cons, prod_cons, mul_assoc, prod_take_mul_prod_drop t] @[to_additive (attr := simp)] theorem prod_take_succ : ∀ (L : List M) (i : ℕ) (p : i < L.length), (L.take (i + 1)).prod = (L.take i).prod * L[i] | [], i, p => by cases p | h :: t, 0, _ => rfl | h :: t, n + 1, p => by dsimp rw [prod_cons, prod_cons, prod_take_succ t n (Nat.lt_of_succ_lt_succ p), mul_assoc] /-- A list with product not one must have positive length. -/ @[to_additive "A list with sum not zero must have positive length."] theorem length_pos_of_prod_ne_one (L : List M) (h : L.prod ≠ 1) : 0 < L.length := by cases L · simp at h · simp /-- A list with product greater than one must have positive length. -/ @[to_additive length_pos_of_sum_pos "A list with positive sum must have positive length."] theorem length_pos_of_one_lt_prod [Preorder M] (L : List M) (h : 1 < L.prod) : 0 < L.length := length_pos_of_prod_ne_one L h.ne' /-- A list with product less than one must have positive length. -/ @[to_additive "A list with negative sum must have positive length."] theorem length_pos_of_prod_lt_one [Preorder M] (L : List M) (h : L.prod < 1) : 0 < L.length := length_pos_of_prod_ne_one L h.ne @[to_additive] theorem prod_set : ∀ (L : List M) (n : ℕ) (a : M), (L.set n a).prod = ((L.take n).prod * if n < L.length then a else 1) * (L.drop (n + 1)).prod | x :: xs, 0, a => by simp [set] | x :: xs, i + 1, a => by simp [set, prod_set xs i a, mul_assoc, Nat.add_lt_add_iff_right] | [], _, _ => by simp [set, (Nat.zero_le _).not_lt, Nat.zero_le] /-- We'd like to state this as `L.headI * L.tail.prod = L.prod`, but because `L.headI` relies on an inhabited instance to return a garbage value on the empty list, this is not possible. Instead, we write the statement in terms of `(L.get? 0).getD 1`. -/ @[to_additive "We'd like to state this as `L.headI + L.tail.sum = L.sum`, but because `L.headI` relies on an inhabited instance to return a garbage value on the empty list, this is not possible. Instead, we write the statement in terms of `(L.get? 0).getD 0`."] theorem get?_zero_mul_tail_prod (l : List M) : (l.get? 0).getD 1 * l.tail.prod = l.prod := by cases l <;> simp /-- Same as `get?_zero_mul_tail_prod`, but avoiding the `List.headI` garbage complication by requiring the list to be nonempty. -/ @[to_additive "Same as `get?_zero_add_tail_sum`, but avoiding the `List.headI` garbage complication by requiring the list to be nonempty."] theorem headI_mul_tail_prod_of_ne_nil [Inhabited M] (l : List M) (h : l ≠ []) : l.headI * l.tail.prod = l.prod := by cases l <;> [contradiction; simp] @[to_additive] theorem _root_.Commute.list_prod_right (l : List M) (y : M) (h : ∀ x ∈ l, Commute y x) : Commute y l.prod := by induction' l with z l IH · simp · rw [List.forall_mem_cons] at h rw [List.prod_cons] exact Commute.mul_right h.1 (IH h.2) @[to_additive] theorem _root_.Commute.list_prod_left (l : List M) (y : M) (h : ∀ x ∈ l, Commute x y) : Commute l.prod y := ((Commute.list_prod_right _ _) fun _ hx => (h _ hx).symm).symm @[to_additive] lemma prod_range_succ (f : ℕ → M) (n : ℕ) : ((range n.succ).map f).prod = ((range n).map f).prod * f n := by rw [range_succ, map_append, map_singleton, prod_append, prod_cons, prod_nil, mul_one] /-- A variant of `prod_range_succ` which pulls off the first term in the product rather than the last. -/ @[to_additive "A variant of `sum_range_succ` which pulls off the first term in the sum rather than the last."] lemma prod_range_succ' (f : ℕ → M) (n : ℕ) : ((range n.succ).map f).prod = f 0 * ((range n).map fun i ↦ f i.succ).prod := Nat.recOn n (show 1 * f 0 = f 0 * 1 by rw [one_mul, mul_one]) fun _ hd => by rw [List.prod_range_succ, hd, mul_assoc, ← List.prod_range_succ] @[to_additive] lemma prod_eq_one (hl : ∀ x ∈ l, x = 1) : l.prod = 1 := by induction' l with i l hil · rfl rw [List.prod_cons, hil fun x hx ↦ hl _ (mem_cons_of_mem i hx), hl _ (mem_cons_self i l), one_mul] @[to_additive] lemma exists_mem_ne_one_of_prod_ne_one (h : l.prod ≠ 1) : ∃ x ∈ l, x ≠ (1 : M) := by simpa only [not_forall, exists_prop] using mt prod_eq_one h @[to_additive] lemma prod_erase_of_comm [DecidableEq M] (ha : a ∈ l) (comm : ∀ x ∈ l, ∀ y ∈ l, x * y = y * x) : a * (l.erase a).prod = l.prod := by induction' l with b l ih · simp only [not_mem_nil] at ha obtain rfl | ⟨ne, h⟩ := List.eq_or_ne_mem_of_mem ha · simp only [erase_cons_head, prod_cons] rw [List.erase, beq_false_of_ne ne.symm, List.prod_cons, List.prod_cons, ← mul_assoc, comm a ha b (l.mem_cons_self b), mul_assoc, ih h fun x hx y hy ↦ comm _ (List.mem_cons_of_mem b hx) _ (List.mem_cons_of_mem b hy)] @[to_additive] lemma prod_map_eq_pow_single [DecidableEq α] {l : List α} (a : α) (f : α → M) (hf : ∀ a', a' ≠ a → a' ∈ l → f a' = 1) : (l.map f).prod = f a ^ l.count a := by induction' l with a' as h generalizing a · rw [map_nil, prod_nil, count_nil, _root_.pow_zero] · specialize h a fun a' ha' hfa' => hf a' ha' (mem_cons_of_mem _ hfa') rw [List.map_cons, List.prod_cons, count_cons, h] simp only [beq_iff_eq] split_ifs with ha' · rw [ha', _root_.pow_succ'] · rw [hf a' ha' (List.mem_cons_self a' as), one_mul, add_zero] @[to_additive] lemma prod_eq_pow_single [DecidableEq M] (a : M) (h : ∀ a', a' ≠ a → a' ∈ l → a' = 1) : l.prod = a ^ l.count a := _root_.trans (by rw [map_id]) (prod_map_eq_pow_single a id h) /-- If elements of a list commute with each other, then their product does not depend on the order of elements. -/ @[to_additive "If elements of a list additively commute with each other, then their sum does not depend on the order of elements."] lemma Perm.prod_eq' (h : l₁ ~ l₂) (hc : l₁.Pairwise Commute) : l₁.prod = l₂.prod := by refine h.foldl_eq' ?_ _ apply Pairwise.forall_of_forall · intro x y h z exact (h z).symm · intros; rfl · apply hc.imp intro a b h z rw [mul_assoc z, mul_assoc z, h] end Monoid section CommMonoid variable [CommMonoid M] {a : M} {l l₁ l₂ : List M} @[to_additive (attr := simp)] lemma prod_erase [DecidableEq M] (ha : a ∈ l) : a * (l.erase a).prod = l.prod := prod_erase_of_comm ha fun x _ y _ ↦ mul_comm x y @[to_additive (attr := simp)] lemma prod_map_erase [DecidableEq α] (f : α → M) {a} : ∀ {l : List α}, a ∈ l → f a * ((l.erase a).map f).prod = (l.map f).prod | b :: l, h => by obtain rfl | ⟨ne, h⟩ := List.eq_or_ne_mem_of_mem h · simp only [map, erase_cons_head, prod_cons] · simp only [map, erase_cons_tail (not_beq_of_ne ne.symm), prod_cons, prod_map_erase _ h, mul_left_comm (f a) (f b)] @[to_additive] lemma Perm.prod_eq (h : Perm l₁ l₂) : prod l₁ = prod l₂ := h.fold_op_eq @[to_additive] lemma prod_reverse (l : List M) : prod l.reverse = prod l := (reverse_perm l).prod_eq @[to_additive] lemma prod_mul_prod_eq_prod_zipWith_mul_prod_drop : ∀ l l' : List M, l.prod * l'.prod = (zipWith (· * ·) l l').prod * (l.drop l'.length).prod * (l'.drop l.length).prod | [], ys => by simp [Nat.zero_le] | xs, [] => by simp [Nat.zero_le] | x :: xs, y :: ys => by simp only [drop, length, zipWith_cons_cons, prod_cons] conv => lhs; rw [mul_assoc]; right; rw [mul_comm, mul_assoc]; right rw [mul_comm, prod_mul_prod_eq_prod_zipWith_mul_prod_drop xs ys] simp [mul_assoc] @[to_additive] lemma prod_mul_prod_eq_prod_zipWith_of_length_eq (l l' : List M) (h : l.length = l'.length) : l.prod * l'.prod = (zipWith (· * ·) l l').prod := by apply (prod_mul_prod_eq_prod_zipWith_mul_prod_drop l l').trans rw [← h, drop_length, h, drop_length, prod_nil, mul_one, mul_one] @[to_additive] lemma prod_map_ite (p : α → Prop) [DecidablePred p] (f g : α → M) (l : List α) : (l.map fun a => if p a then f a else g a).prod = ((l.filter p).map f).prod * ((l.filter fun a ↦ ¬p a).map g).prod := by induction l with | nil => simp | cons x xs ih => simp only [map_cons, filter_cons, prod_cons, nodup_cons, ne_eq, mem_cons, count_cons] at ih ⊢ rw [ih] clear ih by_cases hx : p x · simp only [hx, ↓reduceIte, decide_not, decide_True, map_cons, prod_cons, not_true_eq_false, decide_False, Bool.false_eq_true, mul_assoc] · simp only [hx, ↓reduceIte, decide_not, decide_False, Bool.false_eq_true, not_false_eq_true, decide_True, map_cons, prod_cons, mul_left_comm] @[to_additive] lemma prod_map_filter_mul_prod_map_filter_not (p : α → Prop) [DecidablePred p] (f : α → M) (l : List α) : ((l.filter p).map f).prod * ((l.filter fun x => ¬p x).map f).prod = (l.map f).prod := by rw [← prod_map_ite] simp only [ite_self] end CommMonoid @[to_additive] lemma eq_of_prod_take_eq [LeftCancelMonoid M] {L L' : List M} (h : L.length = L'.length) (h' : ∀ i ≤ L.length, (L.take i).prod = (L'.take i).prod) : L = L' := by refine ext_get h fun i h₁ h₂ => ?_ have : (L.take (i + 1)).prod = (L'.take (i + 1)).prod := h' _ (Nat.succ_le_of_lt h₁) rw [prod_take_succ L i h₁, prod_take_succ L' i h₂, h' i (le_of_lt h₁)] at this convert mul_left_cancel this section Group variable [Group G] /-- This is the `List.prod` version of `mul_inv_rev` -/ @[to_additive "This is the `List.sum` version of `add_neg_rev`"] theorem prod_inv_reverse : ∀ L : List G, L.prod⁻¹ = (L.map fun x => x⁻¹).reverse.prod | [] => by simp | x :: xs => by simp [prod_inv_reverse xs] /-- A non-commutative variant of `List.prod_reverse` -/ @[to_additive "A non-commutative variant of `List.sum_reverse`"] theorem prod_reverse_noncomm : ∀ L : List G, L.reverse.prod = (L.map fun x => x⁻¹).prod⁻¹ := by simp [prod_inv_reverse] /-- Counterpart to `List.prod_take_succ` when we have an inverse operation -/ @[to_additive (attr := simp) "Counterpart to `List.sum_take_succ` when we have a negation operation"] theorem prod_drop_succ : ∀ (L : List G) (i : ℕ) (p), (L.drop (i + 1)).prod = (L.get ⟨i, p⟩)⁻¹ * (L.drop i).prod | [], i, p => False.elim (Nat.not_lt_zero _ p) | x :: xs, 0, _ => by simp | x :: xs, i + 1, p => prod_drop_succ xs i _ /-- Cancellation of a telescoping product. -/ @[to_additive "Cancellation of a telescoping sum."] theorem prod_range_div' (n : ℕ) (f : ℕ → G) : ((range n).map fun k ↦ f k / f (k + 1)).prod = f 0 / f n := by induction' n with n h · exact (div_self' (f 0)).symm · rw [range_succ, map_append, map_singleton, prod_append, prod_singleton, h, div_mul_div_cancel'] lemma prod_rotate_eq_one_of_prod_eq_one : ∀ {l : List G} (_ : l.prod = 1) (n : ℕ), (l.rotate n).prod = 1 | [], _, _ => by simp | a :: l, hl, n => by have : n % List.length (a :: l) ≤ List.length (a :: l) := le_of_lt (Nat.mod_lt _ (by simp)) rw [← List.take_append_drop (n % List.length (a :: l)) (a :: l)] at hl rw [← rotate_mod, rotate_eq_drop_append_take this, List.prod_append, mul_eq_one_iff_inv_eq, ← one_mul (List.prod _)⁻¹, ← hl, List.prod_append, mul_assoc, mul_inv_self, mul_one] end Group section CommGroup variable [CommGroup G] /-- This is the `List.prod` version of `mul_inv` -/ @[to_additive "This is the `List.sum` version of `add_neg`"] theorem prod_inv : ∀ L : List G, L.prod⁻¹ = (L.map fun x => x⁻¹).prod | [] => by simp | x :: xs => by simp [mul_comm, prod_inv xs] /-- Cancellation of a telescoping product. -/ @[to_additive "Cancellation of a telescoping sum."] theorem prod_range_div (n : ℕ) (f : ℕ → G) : ((range n).map fun k ↦ f (k + 1) / f k).prod = f n / f 0 := by have h : ((·⁻¹) ∘ fun k ↦ f (k + 1) / f k) = fun k ↦ f k / f (k + 1) := by ext; apply inv_div rw [← inv_inj, prod_inv, map_map, inv_div, h, prod_range_div'] /-- Alternative version of `List.prod_set` when the list is over a group -/ @[to_additive "Alternative version of `List.sum_set` when the list is over a group"] theorem prod_set' (L : List G) (n : ℕ) (a : G) : (L.set n a).prod = L.prod * if hn : n < L.length then (L.get ⟨n, hn⟩)⁻¹ * a else 1 := by refine (prod_set L n a).trans ?_ split_ifs with hn · rw [mul_comm _ a, mul_assoc a, prod_drop_succ L n hn, mul_comm _ (drop n L).prod, ← mul_assoc (take n L).prod, prod_take_mul_prod_drop, mul_comm a, mul_assoc] · simp only [take_of_length_le (le_of_not_lt hn), prod_nil, mul_one, drop_eq_nil_of_le ((le_of_not_lt hn).trans n.le_succ)] @[to_additive] lemma prod_map_ite_eq {A : Type*} [DecidableEq A] (l : List A) (f g : A → G) (a : A) : (l.map fun x => if x = a then f x else g x).prod = (f a / g a) ^ (l.count a) * (l.map g).prod := by induction l with | nil => simp | cons x xs ih => simp only [map_cons, prod_cons, nodup_cons, ne_eq, mem_cons, count_cons] at ih ⊢ rw [ih] clear ih by_cases hx : x = a · simp only [hx, ite_true, div_pow, pow_add, pow_one, div_eq_mul_inv, mul_assoc, mul_comm, mul_left_comm, mul_inv_cancel_left, beq_self_eq_true] · simp only [hx, ite_false, ne_comm.mp hx, add_zero, mul_assoc, mul_comm (g x) _, beq_iff_eq] end CommGroup theorem sum_const_nat (m n : ℕ) : sum (replicate m n) = m * n := sum_replicate m n /-! Several lemmas about sum/head/tail for `List ℕ`. These are hard to generalize well, as they rely on the fact that `default ℕ = 0`. If desired, we could add a class stating that `default = 0`. -/ /-- This relies on `default ℕ = 0`. -/ theorem headI_add_tail_sum (L : List ℕ) : L.headI + L.tail.sum = L.sum := by cases L <;> simp /-- This relies on `default ℕ = 0`. -/ theorem headI_le_sum (L : List ℕ) : L.headI ≤ L.sum := Nat.le.intro (headI_add_tail_sum L) /-- This relies on `default ℕ = 0`. -/ theorem tail_sum (L : List ℕ) : L.tail.sum = L.sum - L.headI := by rw [← headI_add_tail_sum L, add_comm, Nat.add_sub_cancel_right] section Alternating section variable [One α] [Mul α] [Inv α] @[to_additive (attr := simp)] theorem alternatingProd_nil : alternatingProd ([] : List α) = 1 := rfl @[to_additive (attr := simp)] theorem alternatingProd_singleton (a : α) : alternatingProd [a] = a := rfl @[to_additive] theorem alternatingProd_cons_cons' (a b : α) (l : List α) : alternatingProd (a :: b :: l) = a * b⁻¹ * alternatingProd l := rfl end @[to_additive] theorem alternatingProd_cons_cons [DivInvMonoid α] (a b : α) (l : List α) : alternatingProd (a :: b :: l) = a / b * alternatingProd l := by rw [div_eq_mul_inv, alternatingProd_cons_cons'] variable [CommGroup α] @[to_additive] theorem alternatingProd_cons' : ∀ (a : α) (l : List α), alternatingProd (a :: l) = a * (alternatingProd l)⁻¹ | a, [] => by rw [alternatingProd_nil, inv_one, mul_one, alternatingProd_singleton] | a, b :: l => by rw [alternatingProd_cons_cons', alternatingProd_cons' b l, mul_inv, inv_inv, mul_assoc] @[to_additive (attr := simp)] theorem alternatingProd_cons (a : α) (l : List α) : alternatingProd (a :: l) = a / alternatingProd l := by rw [div_eq_mul_inv, alternatingProd_cons'] end Alternating lemma sum_nat_mod (l : List ℕ) (n : ℕ) : l.sum % n = (l.map (· % n)).sum % n := by induction' l with a l ih · simp only [Nat.zero_mod, map_nil] · simpa only [map_cons, sum_cons, Nat.mod_add_mod, Nat.add_mod_mod] using congr((a + $ih) % n) lemma prod_nat_mod (l : List ℕ) (n : ℕ) : l.prod % n = (l.map (· % n)).prod % n := by induction' l with a l ih · simp only [Nat.zero_mod, map_nil] · simpa only [prod_cons, map_cons, Nat.mod_mul_mod, Nat.mul_mod_mod] using congr((a * $ih) % n) lemma sum_int_mod (l : List ℤ) (n : ℤ) : l.sum % n = (l.map (· % n)).sum % n := by induction l <;> simp [Int.add_emod, *] lemma prod_int_mod (l : List ℤ) (n : ℤ) : l.prod % n = (l.map (· % n)).prod % n := by induction l <;> simp [Int.mul_emod, *] variable [DecidableEq α] /-- Summing the count of `x` over a list filtered by some `p` is just `countP` applied to `p` -/ theorem sum_map_count_dedup_filter_eq_countP (p : α → Bool) (l : List α) : ((l.dedup.filter p).map fun x => l.count x).sum = l.countP p := by induction' l with a as h · simp · simp_rw [List.countP_cons, List.count_cons, List.sum_map_add] congr 1 · refine _root_.trans ?_ h by_cases ha : a ∈ as · simp [dedup_cons_of_mem ha] · simp only [dedup_cons_of_not_mem ha, List.filter] match p a with | true => simp only [List.map_cons, List.sum_cons, List.count_eq_zero.2 ha, zero_add] | false => simp only · simp only [beq_iff_eq] by_cases hp : p a · refine _root_.trans (sum_map_eq_nsmul_single a _ fun _ h _ => by simp [h.symm]) ?_ simp [hp, count_dedup] · refine _root_.trans (List.sum_eq_zero fun n hn => ?_) (by simp [hp]) obtain ⟨a', ha'⟩ := List.mem_map.1 hn split_ifs at ha' with ha · simp only [ha.symm, mem_filter, mem_dedup, find?, mem_cons, true_or, hp, and_false, false_and] at ha' · exact ha'.2.symm theorem sum_map_count_dedup_eq_length (l : List α) : (l.dedup.map fun x => l.count x).sum = l.length := by simpa using sum_map_count_dedup_filter_eq_countP (fun _ => True) l end List section MonoidHom variable [Monoid M] [Monoid N] @[to_additive] theorem map_list_prod {F : Type*} [FunLike F M N] [MonoidHomClass F M N] (f : F) (l : List M) : f l.prod = (l.map f).prod := (l.prod_hom f).symm namespace MonoidHom @[to_additive] protected theorem map_list_prod (f : M →* N) (l : List M) : f l.prod = (l.map f).prod := map_list_prod f l attribute [deprecated map_list_prod (since := "2023-01-10")] MonoidHom.map_list_prod attribute [deprecated map_list_sum (since := "2024-05-02")] AddMonoidHom.map_list_sum end MonoidHom end MonoidHom @[simp] lemma Nat.sum_eq_listSum (l : List ℕ) : Nat.sum l = l.sum := (List.foldl_eq_foldr Nat.add_comm Nat.add_assoc _ _).symm namespace List lemma length_sigma {σ : α → Type*} (l₁ : List α) (l₂ : ∀ a, List (σ a)) : length (l₁.sigma l₂) = (l₁.map fun a ↦ length (l₂ a)).sum := by simp [length_sigma'] lemma ranges_join (l : List ℕ) : l.ranges.join = range l.sum := by simp [ranges_join'] /-- Any entry of any member of `l.ranges` is strictly smaller than `l.sum`. -/ lemma mem_mem_ranges_iff_lt_sum (l : List ℕ) {n : ℕ} : (∃ s ∈ l.ranges, n ∈ s) ↔ n < l.sum := by simp [mem_mem_ranges_iff_lt_natSum] lemma countP_join (p : α → Bool) : ∀ L : List (List α), countP p L.join = (L.map (countP p)).sum | [] => rfl | a :: l => by rw [join, countP_append, map_cons, sum_cons, countP_join _ l] lemma count_join [BEq α] (L : List (List α)) (a : α) : L.join.count a = (L.map (count a)).sum := countP_join _ _ @[simp] theorem length_bind (l : List α) (f : α → List β) : length (List.bind l f) = sum (map (length ∘ f) l) := by rw [List.bind, length_join, map_map, Nat.sum_eq_listSum] lemma countP_bind (p : β → Bool) (l : List α) (f : α → List β) : countP p (l.bind f) = sum (map (countP p ∘ f) l) := by rw [List.bind, countP_join, map_map] lemma count_bind [BEq β] (l : List α) (f : α → List β) (x : β) : count x (l.bind f) = sum (map (count x ∘ f) l) := countP_bind _ _ _ /-- In a join, taking the first elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join of the first `i` sublists. -/ lemma take_sum_join (L : List (List α)) (i : ℕ) : L.join.take ((L.map length).take i).sum = (L.take i).join := by simpa using take_sum_join' _ _ /-- In a join, dropping all the elements up to an index which is the sum of the lengths of the first `i` sublists, is the same as taking the join after dropping the first `i` sublists. -/ lemma drop_sum_join (L : List (List α)) (i : ℕ) : L.join.drop ((L.map length).take i).sum = (L.drop i).join := by simpa using drop_sum_join' _ _ /-- In a join of sublists, taking the slice between the indices `A` and `B - 1` gives back the original sublist of index `i` if `A` is the sum of the lengths of sublists of index `< i`, and `B` is the sum of the lengths of sublists of index `≤ i`. -/ lemma drop_take_succ_join_eq_getElem (L : List (List α)) (i : Nat) (h : i < L.length) : (L.join.take ((L.map length).take (i + 1)).sum).drop ((L.map length).take i).sum = L[i] := by simpa using drop_take_succ_join_eq_getElem' _ _ _ @[deprecated drop_take_succ_join_eq_getElem (since := "2024-06-11")] lemma drop_take_succ_join_eq_get (L : List (List α)) (i : Fin L.length) : (L.join.take ((L.map length).take (i + 1)).sum).drop ((L.map length).take i).sum = get L i := by rw [drop_take_succ_join_eq_getElem _ _ i.2] simp end List namespace List /-- If a product of integers is `-1`, then at least one factor must be `-1`. -/ theorem neg_one_mem_of_prod_eq_neg_one {l : List ℤ} (h : l.prod = -1) : (-1 : ℤ) ∈ l := by obtain ⟨x, h₁, h₂⟩ := exists_mem_ne_one_of_prod_ne_one (ne_of_eq_of_ne h (by decide)) exact Or.resolve_left (Int.isUnit_iff.mp (prod_isUnit_iff.mp (h.symm ▸ ⟨⟨-1, -1, by decide, by decide⟩, rfl⟩ : IsUnit l.prod) x h₁)) h₂ ▸ h₁ /-- If all elements in a list are bounded below by `1`, then the length of the list is bounded by the sum of the elements. -/ theorem length_le_sum_of_one_le (L : List ℕ) (h : ∀ i ∈ L, 1 ≤ i) : L.length ≤ L.sum := by induction' L with j L IH h; · simp rw [sum_cons, length, add_comm] exact Nat.add_le_add (h _ (mem_cons_self _ _)) (IH fun i hi => h i (mem_cons.2 (Or.inr hi))) theorem dvd_prod [CommMonoid M] {a} {l : List M} (ha : a ∈ l) : a ∣ l.prod := by let ⟨s, t, h⟩ := append_of_mem ha rw [h, prod_append, prod_cons, mul_left_comm] exact dvd_mul_right _ _ theorem Sublist.prod_dvd_prod [CommMonoid M] {l₁ l₂ : List M} (h : l₁ <+ l₂) : l₁.prod ∣ l₂.prod := by obtain ⟨l, hl⟩ := h.exists_perm_append rw [hl.prod_eq, prod_append] exact dvd_mul_right _ _ section Alternating variable [CommGroup α] @[to_additive] theorem alternatingProd_append : ∀ l₁ l₂ : List α, alternatingProd (l₁ ++ l₂) = alternatingProd l₁ * alternatingProd l₂ ^ (-1 : ℤ) ^ length l₁ | [], l₂ => by simp | a :: l₁, l₂ => by simp_rw [cons_append, alternatingProd_cons, alternatingProd_append, length_cons, pow_succ', Int.neg_mul, one_mul, zpow_neg, ← div_eq_mul_inv, div_div] @[to_additive] theorem alternatingProd_reverse : ∀ l : List α, alternatingProd (reverse l) = alternatingProd l ^ (-1 : ℤ) ^ (length l + 1) | [] => by simp only [alternatingProd_nil, one_zpow, reverse_nil] | a :: l => by simp_rw [reverse_cons, alternatingProd_append, alternatingProd_reverse, alternatingProd_singleton, alternatingProd_cons, length_reverse, length, pow_succ', Int.neg_mul, one_mul, zpow_neg, inv_inv] rw [mul_comm, ← div_eq_mul_inv, div_zpow] end Alternating end List open List namespace MulOpposite variable [Monoid M] lemma op_list_prod : ∀ l : List M, op l.prod = (l.map op).reverse.prod := by intro l; induction l with | nil => rfl | cons x xs ih => rw [List.prod_cons, List.map_cons, List.reverse_cons', List.prod_concat, op_mul, ih] lemma unop_list_prod (l : List Mᵐᵒᵖ) : l.prod.unop = (l.map unop).reverse.prod := by rw [← op_inj, op_unop, MulOpposite.op_list_prod, map_reverse, map_map, reverse_reverse, op_comp_unop, map_id] end MulOpposite section MonoidHom variable [Monoid M] [Monoid N] /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements. -/ lemma unop_map_list_prod {F : Type*} [FunLike F M Nᵐᵒᵖ] [MonoidHomClass F M Nᵐᵒᵖ] (f : F) (l : List M) : (f l.prod).unop = (l.map (MulOpposite.unop ∘ f)).reverse.prod := by rw [map_list_prod f l, MulOpposite.unop_list_prod, List.map_map] namespace MonoidHom /-- A morphism into the opposite monoid acts on the product by acting on the reversed elements. -/ @[deprecated _root_.unop_map_list_prod (since := "2023-01-10")] protected theorem unop_map_list_prod (f : M →* Nᵐᵒᵖ) (l : List M) : (f l.prod).unop = (l.map (MulOpposite.unop ∘ f)).reverse.prod := unop_map_list_prod f l end MonoidHom end MonoidHom
Algebra\BigOperators\Group\Multiset.lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ import Mathlib.Algebra.BigOperators.Group.List import Mathlib.Algebra.Group.Prod import Mathlib.Data.Multiset.Basic /-! # Sums and products over multisets In this file we define products and sums indexed by multisets. This is later used to define products and sums indexed by finite sets. ## Main declarations * `Multiset.prod`: `s.prod f` is the product of `f i` over all `i ∈ s`. Not to be mistaken with the cartesian product `Multiset.product`. * `Multiset.sum`: `s.sum f` is the sum of `f i` over all `i ∈ s`. -/ assert_not_exists MonoidWithZero variable {F ι α β β' γ : Type*} namespace Multiset section CommMonoid variable [CommMonoid α] [CommMonoid β] {s t : Multiset α} {a : α} {m : Multiset ι} {f g : ι → α} /-- Product of a multiset given a commutative monoid structure on `α`. `prod {a, b, c} = a * b * c` -/ @[to_additive "Sum of a multiset given a commutative additive monoid structure on `α`. `sum {a, b, c} = a + b + c`"] def prod : Multiset α → α := foldr (· * ·) (fun x y z => by simp [mul_left_comm]) 1 @[to_additive] theorem prod_eq_foldr (s : Multiset α) : prod s = foldr (· * ·) (fun x y z => by simp [mul_left_comm]) 1 s := rfl @[to_additive] theorem prod_eq_foldl (s : Multiset α) : prod s = foldl (· * ·) (fun x y z => by simp [mul_right_comm]) 1 s := (foldr_swap _ _ _ _).trans (by simp [mul_comm]) @[to_additive (attr := simp, norm_cast)] theorem prod_coe (l : List α) : prod ↑l = l.prod := prod_eq_foldl _ @[to_additive (attr := simp)] theorem prod_toList (s : Multiset α) : s.toList.prod = s.prod := by conv_rhs => rw [← coe_toList s] rw [prod_coe] @[to_additive (attr := simp)] theorem prod_zero : @prod α _ 0 = 1 := rfl @[to_additive (attr := simp)] theorem prod_cons (a : α) (s) : prod (a ::ₘ s) = a * prod s := foldr_cons _ _ _ _ _ @[to_additive (attr := simp)] theorem prod_erase [DecidableEq α] (h : a ∈ s) : a * (s.erase a).prod = s.prod := by rw [← s.coe_toList, coe_erase, prod_coe, prod_coe, List.prod_erase (mem_toList.2 h)] @[to_additive (attr := simp)] theorem prod_map_erase [DecidableEq ι] {a : ι} (h : a ∈ m) : f a * ((m.erase a).map f).prod = (m.map f).prod := by rw [← m.coe_toList, coe_erase, map_coe, map_coe, prod_coe, prod_coe, List.prod_map_erase f (mem_toList.2 h)] @[to_additive (attr := simp)] theorem prod_singleton (a : α) : prod {a} = a := by simp only [mul_one, prod_cons, ← cons_zero, eq_self_iff_true, prod_zero] @[to_additive] theorem prod_pair (a b : α) : ({a, b} : Multiset α).prod = a * b := by rw [insert_eq_cons, prod_cons, prod_singleton] @[to_additive (attr := simp)] theorem prod_add (s t : Multiset α) : prod (s + t) = prod s * prod t := Quotient.inductionOn₂ s t fun l₁ l₂ => by simp @[to_additive] theorem prod_nsmul (m : Multiset α) : ∀ n : ℕ, (n • m).prod = m.prod ^ n | 0 => by rw [zero_nsmul, pow_zero] rfl | n + 1 => by rw [add_nsmul, one_nsmul, pow_add, pow_one, prod_add, prod_nsmul m n] @[to_additive] theorem prod_filter_mul_prod_filter_not (p) [DecidablePred p] : (s.filter p).prod * (s.filter (fun a ↦ ¬ p a)).prod = s.prod := by rw [← prod_add, filter_add_not] @[to_additive (attr := simp)] theorem prod_replicate (n : ℕ) (a : α) : (replicate n a).prod = a ^ n := by simp [replicate, List.prod_replicate] @[to_additive] theorem prod_map_eq_pow_single [DecidableEq ι] (i : ι) (hf : ∀ i' ≠ i, i' ∈ m → f i' = 1) : (m.map f).prod = f i ^ m.count i := by induction' m using Quotient.inductionOn with l simp [List.prod_map_eq_pow_single i f hf] @[to_additive] theorem prod_eq_pow_single [DecidableEq α] (a : α) (h : ∀ a' ≠ a, a' ∈ s → a' = 1) : s.prod = a ^ s.count a := by induction' s using Quotient.inductionOn with l simp [List.prod_eq_pow_single a h] @[to_additive] lemma prod_eq_one (h : ∀ x ∈ s, x = (1 : α)) : s.prod = 1 := by induction' s using Quotient.inductionOn with l; simp [List.prod_eq_one h] @[to_additive] theorem pow_count [DecidableEq α] (a : α) : a ^ s.count a = (s.filter (Eq a)).prod := by rw [filter_eq, prod_replicate] @[to_additive] theorem prod_hom (s : Multiset α) {F : Type*} [FunLike F α β] [MonoidHomClass F α β] (f : F) : (s.map f).prod = f s.prod := Quotient.inductionOn s fun l => by simp only [l.prod_hom f, quot_mk_to_coe, map_coe, prod_coe] @[to_additive] theorem prod_hom' (s : Multiset ι) {F : Type*} [FunLike F α β] [MonoidHomClass F α β] (f : F) (g : ι → α) : (s.map fun i => f <| g i).prod = f (s.map g).prod := by convert (s.map g).prod_hom f exact (map_map _ _ _).symm @[to_additive] theorem prod_hom₂ [CommMonoid γ] (s : Multiset ι) (f : α → β → γ) (hf : ∀ a b c d, f (a * b) (c * d) = f a c * f b d) (hf' : f 1 1 = 1) (f₁ : ι → α) (f₂ : ι → β) : (s.map fun i => f (f₁ i) (f₂ i)).prod = f (s.map f₁).prod (s.map f₂).prod := Quotient.inductionOn s fun l => by simp only [l.prod_hom₂ f hf hf', quot_mk_to_coe, map_coe, prod_coe] @[to_additive] theorem prod_hom_rel (s : Multiset ι) {r : α → β → Prop} {f : ι → α} {g : ι → β} (h₁ : r 1 1) (h₂ : ∀ ⦃a b c⦄, r b c → r (f a * b) (g a * c)) : r (s.map f).prod (s.map g).prod := Quotient.inductionOn s fun l => by simp only [l.prod_hom_rel h₁ h₂, quot_mk_to_coe, map_coe, prod_coe] @[to_additive] theorem prod_map_one : prod (m.map fun _ => (1 : α)) = 1 := by rw [map_const', prod_replicate, one_pow] @[to_additive (attr := simp)] theorem prod_map_mul : (m.map fun i => f i * g i).prod = (m.map f).prod * (m.map g).prod := m.prod_hom₂ (· * ·) mul_mul_mul_comm (mul_one _) _ _ @[to_additive] theorem prod_map_pow {n : ℕ} : (m.map fun i => f i ^ n).prod = (m.map f).prod ^ n := m.prod_hom' (powMonoidHom n : α →* α) f @[to_additive] theorem prod_map_prod_map (m : Multiset β') (n : Multiset γ) {f : β' → γ → α} : prod (m.map fun a => prod <| n.map fun b => f a b) = prod (n.map fun b => prod <| m.map fun a => f a b) := Multiset.induction_on m (by simp) fun a m ih => by simp [ih] @[to_additive] theorem prod_induction (p : α → Prop) (s : Multiset α) (p_mul : ∀ a b, p a → p b → p (a * b)) (p_one : p 1) (p_s : ∀ a ∈ s, p a) : p s.prod := by rw [prod_eq_foldr] exact foldr_induction (· * ·) (fun x y z => by simp [mul_left_comm]) 1 p s p_mul p_one p_s @[to_additive] theorem prod_induction_nonempty (p : α → Prop) (p_mul : ∀ a b, p a → p b → p (a * b)) (hs : s ≠ ∅) (p_s : ∀ a ∈ s, p a) : p s.prod := by -- Porting note: used to be `refine' Multiset.induction _ _` induction' s using Multiset.induction_on with a s hsa · simp at hs rw [prod_cons] by_cases hs_empty : s = ∅ · simp [hs_empty, p_s a] have hps : ∀ x, x ∈ s → p x := fun x hxs => p_s x (mem_cons_of_mem hxs) exact p_mul a s.prod (p_s a (mem_cons_self a s)) (hsa hs_empty hps) theorem prod_dvd_prod_of_le (h : s ≤ t) : s.prod ∣ t.prod := by obtain ⟨z, rfl⟩ := exists_add_of_le h simp only [prod_add, dvd_mul_right] @[to_additive] lemma _root_.map_multiset_prod [FunLike F α β] [MonoidHomClass F α β] (f : F) (s : Multiset α) : f s.prod = (s.map f).prod := (s.prod_hom f).symm @[to_additive] protected lemma _root_.MonoidHom.map_multiset_prod (f : α →* β) (s : Multiset α) : f s.prod = (s.map f).prod := (s.prod_hom f).symm lemma dvd_prod : a ∈ s → a ∣ s.prod := Quotient.inductionOn s (fun l a h ↦ by simpa using List.dvd_prod h) a @[to_additive] lemma fst_prod (s : Multiset (α × β)) : s.prod.1 = (s.map Prod.fst).prod := map_multiset_prod (MonoidHom.fst _ _) _ @[to_additive] lemma snd_prod (s : Multiset (α × β)) : s.prod.2 = (s.map Prod.snd).prod := map_multiset_prod (MonoidHom.snd _ _) _ end CommMonoid theorem prod_dvd_prod_of_dvd [CommMonoid β] {S : Multiset α} (g1 g2 : α → β) (h : ∀ a ∈ S, g1 a ∣ g2 a) : (Multiset.map g1 S).prod ∣ (Multiset.map g2 S).prod := by apply Multiset.induction_on' S · simp intro a T haS _ IH simp [mul_dvd_mul (h a haS) IH] section AddCommMonoid variable [AddCommMonoid α] /-- `Multiset.sum`, the sum of the elements of a multiset, promoted to a morphism of `AddCommMonoid`s. -/ def sumAddMonoidHom : Multiset α →+ α where toFun := sum map_zero' := sum_zero map_add' := sum_add @[simp] theorem coe_sumAddMonoidHom : (sumAddMonoidHom : Multiset α → α) = sum := rfl end AddCommMonoid section DivisionCommMonoid variable [DivisionCommMonoid α] {m : Multiset ι} {f g : ι → α} @[to_additive] theorem prod_map_inv' (m : Multiset α) : (m.map Inv.inv).prod = m.prod⁻¹ := m.prod_hom (invMonoidHom : α →* α) @[to_additive (attr := simp)] theorem prod_map_inv : (m.map fun i => (f i)⁻¹).prod = (m.map f).prod⁻¹ := by -- Porting note: used `convert` simp_rw [← (m.map f).prod_map_inv', map_map, Function.comp_apply] @[to_additive (attr := simp)] theorem prod_map_div : (m.map fun i => f i / g i).prod = (m.map f).prod / (m.map g).prod := m.prod_hom₂ (· / ·) mul_div_mul_comm (div_one _) _ _ @[to_additive] theorem prod_map_zpow {n : ℤ} : (m.map fun i => f i ^ n).prod = (m.map f).prod ^ n := by convert (m.map f).prod_hom (zpowGroupHom n : α →* α) simp only [map_map, Function.comp_apply, zpowGroupHom_apply] end DivisionCommMonoid @[simp] theorem sum_map_singleton (s : Multiset α) : (s.map fun a => ({a} : Multiset α)).sum = s := Multiset.induction_on s (by simp) (by simp) theorem sum_nat_mod (s : Multiset ℕ) (n : ℕ) : s.sum % n = (s.map (· % n)).sum % n := by induction s using Multiset.induction <;> simp [Nat.add_mod, *] theorem prod_nat_mod (s : Multiset ℕ) (n : ℕ) : s.prod % n = (s.map (· % n)).prod % n := by induction s using Multiset.induction <;> simp [Nat.mul_mod, *] theorem sum_int_mod (s : Multiset ℤ) (n : ℤ) : s.sum % n = (s.map (· % n)).sum % n := by induction s using Multiset.induction <;> simp [Int.add_emod, *] theorem prod_int_mod (s : Multiset ℤ) (n : ℤ) : s.prod % n = (s.map (· % n)).prod % n := by induction s using Multiset.induction <;> simp [Int.mul_emod, *] end Multiset
Algebra\BigOperators\GroupWithZero\Finset.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.GroupWithZero.Units.Basic /-! # Big operators on a finset in groups with zero This file contains the results concerning the interaction of finset big operators with groups with zero. -/ open Function variable {ι κ G₀ M₀ : Type*} namespace Finset variable [CommMonoidWithZero M₀] {p : ι → Prop} [DecidablePred p] {f : ι → M₀} {s : Finset ι} {i : ι} lemma prod_eq_zero (hi : i ∈ s) (h : f i = 0) : ∏ j ∈ s, f j = 0 := by classical rw [← prod_erase_mul _ _ hi, h, mul_zero] lemma prod_boole : ∏ i ∈ s, (ite (p i) 1 0 : M₀) = ite (∀ i ∈ s, p i) 1 0 := by split_ifs with h · apply prod_eq_one intro i hi rw [if_pos (h i hi)] · push_neg at h rcases h with ⟨i, hi, hq⟩ apply prod_eq_zero hi rw [if_neg hq] lemma support_prod_subset (s : Finset ι) (f : ι → κ → M₀) : support (fun x ↦ ∏ i ∈ s, f i x) ⊆ ⋂ i ∈ s, support (f i) := fun _ hx ↦ Set.mem_iInter₂.2 fun _ hi H ↦ hx <| prod_eq_zero hi H variable [Nontrivial M₀] [NoZeroDivisors M₀] lemma prod_eq_zero_iff : ∏ x ∈ s, f x = 0 ↔ ∃ a ∈ s, f a = 0 := by classical induction' s using Finset.induction_on with a s ha ih · exact ⟨Not.elim one_ne_zero, fun ⟨_, H, _⟩ => by simp at H⟩ · rw [prod_insert ha, mul_eq_zero, exists_mem_insert, ih] lemma prod_ne_zero_iff : ∏ x ∈ s, f x ≠ 0 ↔ ∀ a ∈ s, f a ≠ 0 := by rw [Ne, prod_eq_zero_iff] push_neg; rfl lemma support_prod (s : Finset ι) (f : ι → κ → M₀) : support (fun j ↦ ∏ i ∈ s, f i j) = ⋂ i ∈ s, support (f i) := Set.ext fun x ↦ by simp [support, prod_eq_zero_iff] end Finset namespace Fintype variable [Fintype ι] [CommMonoidWithZero M₀] {p : ι → Prop} [DecidablePred p] lemma prod_boole : ∏ i, (ite (p i) 1 0 : M₀) = ite (∀ i, p i) 1 0 := by simp [Finset.prod_boole] end Fintype lemma Units.mk0_prod [CommGroupWithZero G₀] (s : Finset ι) (f : ι → G₀) (h) : Units.mk0 (∏ i ∈ s, f i) h = ∏ i ∈ s.attach, Units.mk0 (f i) fun hh ↦ h (Finset.prod_eq_zero i.2 hh) := by classical induction s using Finset.induction_on <;> simp [*]
Algebra\BigOperators\Ring\List.lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Floris van Doorn, Sébastien Gouëzel, Alex J. Best -/ import Mathlib.Algebra.BigOperators.Group.List import Mathlib.Algebra.GroupWithZero.Commute import Mathlib.Algebra.GroupWithZero.Divisibility import Mathlib.Algebra.Ring.Basic import Mathlib.Algebra.Ring.Divisibility.Basic import Mathlib.Algebra.Ring.Commute /-! # Big operators on a list in rings This file contains the results concerning the interaction of list big operators with rings. -/ open MulOpposite List variable {ι κ M M₀ R : Type*} namespace Commute variable [NonUnitalNonAssocSemiring R] lemma list_sum_right (a : R) (l : List R) (h : ∀ b ∈ l, Commute a b) : Commute a l.sum := by induction' l with x xs ih · exact Commute.zero_right _ · rw [List.sum_cons] exact (h _ <| mem_cons_self _ _).add_right (ih fun j hj ↦ h _ <| mem_cons_of_mem _ hj) lemma list_sum_left (b : R) (l : List R) (h : ∀ a ∈ l, Commute a b) : Commute l.sum b := ((Commute.list_sum_right _ _) fun _x hx ↦ (h _ hx).symm).symm end Commute namespace List section HasDistribNeg variable [CommMonoid M] [HasDistribNeg M] @[simp] lemma prod_map_neg (l : List M) : (l.map Neg.neg).prod = (-1) ^ l.length * l.prod := by induction l <;> simp [*, pow_succ, ((Commute.neg_one_left _).pow_left _).left_comm] end HasDistribNeg section MonoidWithZero variable [MonoidWithZero M₀] {l : List M₀} /-- If zero is an element of a list `l`, then `List.prod l = 0`. If the domain is a nontrivial monoid with zero with no divisors, then this implication becomes an `iff`, see `List.prod_eq_zero_iff`. -/ lemma prod_eq_zero : ∀ {l : List M₀}, (0 : M₀) ∈ l → l.prod = 0 -- | absurd h (not_mem_nil _) | a :: l, h => by rw [prod_cons] cases' mem_cons.1 h with ha hl exacts [mul_eq_zero_of_left ha.symm _, mul_eq_zero_of_right _ (prod_eq_zero hl)] variable [Nontrivial M₀] [NoZeroDivisors M₀] /-- Product of elements of a list `l` equals zero if and only if `0 ∈ l`. See also `List.prod_eq_zero` for an implication that needs weaker typeclass assumptions. -/ @[simp] lemma prod_eq_zero_iff : ∀ {l : List M₀}, l.prod = 0 ↔ (0 : M₀) ∈ l | [] => by simp | a :: l => by rw [prod_cons, mul_eq_zero, prod_eq_zero_iff, mem_cons, eq_comm] lemma prod_ne_zero (hL : (0 : M₀) ∉ l) : l.prod ≠ 0 := mt prod_eq_zero_iff.1 hL end MonoidWithZero section NonUnitalNonAssocSemiring variable [NonUnitalNonAssocSemiring R] (l : List ι) (f : ι → R) (r : R) lemma sum_map_mul_left : (l.map fun b ↦ r * f b).sum = r * (l.map f).sum := sum_map_hom l f <| AddMonoidHom.mulLeft r lemma sum_map_mul_right : (l.map fun b ↦ f b * r).sum = (l.map f).sum * r := sum_map_hom l f <| AddMonoidHom.mulRight r end NonUnitalNonAssocSemiring lemma dvd_sum [NonUnitalSemiring R] {a} {l : List R} (h : ∀ x ∈ l, a ∣ x) : a ∣ l.sum := by induction' l with x l ih · exact dvd_zero _ · rw [List.sum_cons] exact dvd_add (h _ (mem_cons_self _ _)) (ih fun x hx ↦ h x (mem_cons_of_mem _ hx)) @[simp] lemma sum_zipWith_distrib_left [Semiring R] (f : ι → κ → R) (a : R) : ∀ (l₁ : List ι) (l₂ : List κ), (zipWith (fun i j ↦ a * f i j) l₁ l₂).sum = a * (zipWith f l₁ l₂).sum | [], _ => by simp | _, [] => by simp | i :: l₁, j :: l₂ => by simp [sum_zipWith_distrib_left, mul_add] end List
Algebra\BigOperators\Ring\Multiset.lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes, Bhavik Mehta, Eric Wieser -/ import Mathlib.Algebra.BigOperators.Group.Multiset import Mathlib.Algebra.BigOperators.Ring.List import Mathlib.Data.Multiset.Antidiagonal import Mathlib.Data.Multiset.Sections /-! # Lemmas about `Multiset.sum` and `Multiset.prod` requiring extra algebra imports -/ variable {ι α β : Type*} namespace Multiset section CommMonoid variable [CommMonoid α] [HasDistribNeg α] @[simp] lemma prod_map_neg (s : Multiset α) : (s.map Neg.neg).prod = (-1) ^ card s * s.prod := Quotient.inductionOn s (by simp) end CommMonoid section CommMonoidWithZero variable [CommMonoidWithZero α] {s : Multiset α} lemma prod_eq_zero (h : (0 : α) ∈ s) : s.prod = 0 := by rcases Multiset.exists_cons_of_mem h with ⟨s', hs'⟩; simp [hs', Multiset.prod_cons] variable [NoZeroDivisors α] [Nontrivial α] {s : Multiset α} @[simp] lemma prod_eq_zero_iff : s.prod = 0 ↔ (0 : α) ∈ s := Quotient.inductionOn s fun l ↦ by rw [quot_mk_to_coe, prod_coe]; exact List.prod_eq_zero_iff lemma prod_ne_zero (h : (0 : α) ∉ s) : s.prod ≠ 0 := mt prod_eq_zero_iff.1 h end CommMonoidWithZero section NonUnitalNonAssocSemiring variable [NonUnitalNonAssocSemiring α] {a : α} {s : Multiset ι} {f : ι → α} lemma sum_map_mul_left : sum (s.map fun i ↦ a * f i) = a * sum (s.map f) := Multiset.induction_on s (by simp) fun i s ih => by simp [ih, mul_add] lemma sum_map_mul_right : sum (s.map fun i ↦ f i * a) = sum (s.map f) * a := Multiset.induction_on s (by simp) fun a s ih => by simp [ih, add_mul] end NonUnitalNonAssocSemiring section NonUnitalSemiring variable [NonUnitalSemiring α] {s : Multiset α} {a : α} lemma dvd_sum : (∀ x ∈ s, a ∣ x) → a ∣ s.sum := Multiset.induction_on s (fun _ ↦ dvd_zero _) fun x s ih h ↦ by rw [sum_cons] exact dvd_add (h _ (mem_cons_self _ _)) (ih fun y hy ↦ h _ <| mem_cons.2 <| Or.inr hy) end NonUnitalSemiring section CommSemiring variable [CommSemiring α] lemma prod_map_sum {s : Multiset (Multiset α)} : prod (s.map sum) = sum ((Sections s).map prod) := Multiset.induction_on s (by simp) fun a s ih ↦ by simp [ih, map_bind, sum_map_mul_left, sum_map_mul_right] lemma prod_map_add {s : Multiset ι} {f g : ι → α} : prod (s.map fun i ↦ f i + g i) = sum ((antidiagonal s).map fun p ↦ (p.1.map f).prod * (p.2.map g).prod) := by refine s.induction_on ?_ fun a s ih ↦ ?_ · simp only [map_zero, prod_zero, antidiagonal_zero, map_singleton, mul_one, sum_singleton] · simp only [map_cons, prod_cons, ih, sum_map_mul_left.symm, add_mul, mul_left_comm (f a), mul_left_comm (g a), sum_map_add, antidiagonal_cons, Prod.map_fst, Prod.map_snd, id_eq, map_add, map_map, Function.comp_apply, mul_assoc, sum_add] exact add_comm _ _ end CommSemiring end Multiset open Multiset namespace Commute variable [NonUnitalNonAssocSemiring α] (s : Multiset α) theorem multiset_sum_right (a : α) (h : ∀ b ∈ s, Commute a b) : Commute a s.sum := by induction s using Quotient.inductionOn rw [quot_mk_to_coe, sum_coe] exact Commute.list_sum_right _ _ h theorem multiset_sum_left (b : α) (h : ∀ a ∈ s, Commute a b) : Commute s.sum b := ((Commute.multiset_sum_right _ _) fun _ ha => (h _ ha).symm).symm end Commute
Algebra\BigOperators\Ring\Nat.lean
/- Copyright (c) 2024 Pim Otte. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Pim Otte -/ import Mathlib.Algebra.BigOperators.Group.Finset import Mathlib.Algebra.Ring.Parity /-! # Big operators on a finset in the natural numbers This file contains the results concerning the interaction of finset big operators with natural numbers. -/ variable {ι : Type*} namespace Finset lemma even_sum_iff_even_card_odd {s : Finset ι} (f : ι → ℕ) : Even (∑ i ∈ s, f i) ↔ Even (s.filter fun x ↦ Odd (f x)).card := by rw [← Finset.sum_filter_add_sum_filter_not _ (fun x ↦ Even (f x)), Nat.even_add] simp only [Finset.mem_filter, and_imp, imp_self, implies_true, Finset.even_sum, true_iff] rw [Nat.even_iff, Finset.sum_nat_mod, Finset.sum_filter] simp (config := { contextual := true }) only [← Nat.odd_iff_not_even, Nat.odd_iff.mp] simp_rw [← Finset.sum_filter, ← Nat.even_iff, Finset.card_eq_sum_ones] lemma odd_sum_iff_odd_card_odd {s : Finset ι} (f : ι → ℕ) : Odd (∑ i ∈ s, f i) ↔ Odd (s.filter fun x ↦ Odd (f x)).card := by simp only [Nat.odd_iff_not_even, even_sum_iff_even_card_odd] end Finset
Algebra\Category\BoolRing.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Category.Ring.Basic import Mathlib.Algebra.Ring.BooleanRing import Mathlib.Order.Category.BoolAlg /-! # The category of Boolean rings This file defines `BoolRing`, the category of Boolean rings. ## TODO Finish the equivalence with `BoolAlg`. -/ universe u open CategoryTheory Order /-- The category of Boolean rings. -/ def BoolRing := Bundled BooleanRing namespace BoolRing instance : CoeSort BoolRing Type* := Bundled.coeSort instance (X : BoolRing) : BooleanRing X := X.str /-- Construct a bundled `BoolRing` from a `BooleanRing`. -/ def of (α : Type*) [BooleanRing α] : BoolRing := Bundled.of α @[simp] theorem coe_of (α : Type*) [BooleanRing α] : ↥(of α) = α := rfl instance : Inhabited BoolRing := ⟨of PUnit⟩ instance : BundledHom.ParentProjection @BooleanRing.toCommRing := ⟨⟩ -- Porting note: `deriving` `ConcreteCategory` failed, added it manually -- see https://github.com/leanprover-community/mathlib4/issues/5020 deriving instance LargeCategory for BoolRing instance : ConcreteCategory BoolRing := by dsimp [BoolRing] infer_instance -- Porting note: disabled `simps` -- Invalid simp lemma BoolRing.hasForgetToCommRing_forget₂_obj_str_add. -- The given definition is not a constructor application: -- inferInstance.1 -- @[simps] instance hasForgetToCommRing : HasForget₂ BoolRing CommRingCat := BundledHom.forget₂ _ _ /-- Constructs an isomorphism of Boolean rings from a ring isomorphism between them. -/ @[simps] def Iso.mk {α β : BoolRing.{u}} (e : α ≃+* β) : α ≅ β where hom := (e : RingHom _ _) inv := (e.symm : RingHom _ _) hom_inv_id := by ext; exact e.symm_apply_apply _ inv_hom_id := by ext; exact e.apply_symm_apply _ end BoolRing /-! ### Equivalence between `BoolAlg` and `BoolRing` -/ @[simps] instance BoolRing.hasForgetToBoolAlg : HasForget₂ BoolRing BoolAlg where forget₂ := { obj := fun X => BoolAlg.of (AsBoolAlg X) map := fun {X Y} => RingHom.asBoolAlg } -- Porting note: Added. somehow it does not find this instance. instance {X : BoolAlg} : BooleanAlgebra ↑(BddDistLat.toBddLat (X.toBddDistLat)).toLat := BoolAlg.instBooleanAlgebra _ @[simps] instance BoolAlg.hasForgetToBoolRing : HasForget₂ BoolAlg BoolRing where forget₂ := { obj := fun X => BoolRing.of (AsBoolRing X) map := fun {X Y} => BoundedLatticeHom.asBoolRing } /-- The equivalence between Boolean rings and Boolean algebras. This is actually an isomorphism. -/ @[simps functor inverse] def boolRingCatEquivBoolAlg : BoolRing ≌ BoolAlg where functor := forget₂ BoolRing BoolAlg inverse := forget₂ BoolAlg BoolRing unitIso := NatIso.ofComponents (fun X => BoolRing.Iso.mk <| (RingEquiv.asBoolRingAsBoolAlg X).symm) fun {X Y} f => rfl counitIso := NatIso.ofComponents (fun X => BoolAlg.Iso.mk <| OrderIso.asBoolAlgAsBoolRing X) fun {X Y} f => rfl
Algebra\Category\GrpWithZero.lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import Mathlib.Algebra.Category.MonCat.Basic import Mathlib.Algebra.GroupWithZero.WithZero import Mathlib.CategoryTheory.Category.Bipointed /-! # The category of groups with zero This file defines `GrpWithZero`, the category of groups with zero. -/ universe u open CategoryTheory Order /-- The category of groups with zero. -/ def GrpWithZero := Bundled GroupWithZero namespace GrpWithZero instance : CoeSort GrpWithZero Type* := Bundled.coeSort instance (X : GrpWithZero) : GroupWithZero X := X.str /-- Construct a bundled `GrpWithZero` from a `GroupWithZero`. -/ def of (α : Type*) [GroupWithZero α] : GrpWithZero := Bundled.of α instance : Inhabited GrpWithZero := ⟨of (WithZero PUnit)⟩ instance : LargeCategory.{u} GrpWithZero where Hom X Y := MonoidWithZeroHom X Y id X := MonoidWithZeroHom.id X comp f g := g.comp f id_comp := MonoidWithZeroHom.comp_id comp_id := MonoidWithZeroHom.id_comp assoc _ _ _ := MonoidWithZeroHom.comp_assoc _ _ _ instance {M N : GrpWithZero} : FunLike (M ⟶ N) M N := ⟨fun f => f.toFun, fun f g h => by cases f cases g congr apply DFunLike.coe_injective' exact h⟩ lemma coe_id {X : GrpWithZero} : (𝟙 X : X → X) = id := rfl lemma coe_comp {X Y Z : GrpWithZero} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g : X → Z) = g ∘ f := rfl instance groupWithZeroConcreteCategory : ConcreteCategory GrpWithZero where forget := { obj := fun G => G map := fun f => f.toFun } forget_faithful := ⟨fun h => DFunLike.coe_injective h⟩ @[simp] lemma forget_map {X Y : GrpWithZero} (f : X ⟶ Y) : (forget GrpWithZero).map f = f := rfl instance hasForgetToBipointed : HasForget₂ GrpWithZero Bipointed where forget₂ := { obj := fun X => ⟨X, 0, 1⟩ map := fun f => ⟨f, f.map_zero', f.map_one'⟩ } instance hasForgetToMon : HasForget₂ GrpWithZero MonCat where forget₂ := { obj := fun X => ⟨ X , _ ⟩ map := fun f => f.toMonoidHom } /-- Constructs an isomorphism of groups with zero from a group isomorphism between them. -/ @[simps] def Iso.mk {α β : GrpWithZero.{u}} (e : α ≃* β) : α ≅ β where hom := (e : α →*₀ β) inv := (e.symm : β →*₀ α) hom_inv_id := by ext exact e.symm_apply_apply _ inv_hom_id := by ext exact e.apply_symm_apply _ end GrpWithZero
Algebra\Category\AlgebraCat\Basic.lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.Algebra.Algebra.Subalgebra.Basic import Mathlib.Algebra.FreeAlgebra import Mathlib.Algebra.Category.Ring.Basic import Mathlib.Algebra.Category.ModuleCat.Basic /-! # Category instance for algebras over a commutative ring We introduce the bundled category `AlgebraCat` of algebras over a fixed commutative ring `R` along with the forgetful functors to `RingCat` and `ModuleCat`. We furthermore show that the functor associating to a type the free `R`-algebra on that type is left adjoint to the forgetful functor. -/ open CategoryTheory Limits universe v u variable (R : Type u) [CommRing R] /-- The category of R-algebras and their morphisms. -/ structure AlgebraCat where carrier : Type v [isRing : Ring carrier] [isAlgebra : Algebra R carrier] -- Porting note: typemax hack to fix universe complaints /-- An alias for `AlgebraCat.{max u₁ u₂}`, to deal around unification issues. Since the universe the ring lives in can be inferred, we put that last. -/ @[nolint checkUnivs] abbrev AlgebraCatMax.{v₁, v₂, u₁} (R : Type u₁) [CommRing R] := AlgebraCat.{max v₁ v₂} R attribute [instance] AlgebraCat.isRing AlgebraCat.isAlgebra initialize_simps_projections AlgebraCat (-isRing, -isAlgebra) namespace AlgebraCat instance : CoeSort (AlgebraCat R) (Type v) := ⟨AlgebraCat.carrier⟩ attribute [coe] AlgebraCat.carrier instance : Category (AlgebraCat.{v} R) where Hom A B := A →ₐ[R] B id A := AlgHom.id R A comp f g := g.comp f instance {M N : AlgebraCat.{v} R} : FunLike (M ⟶ N) M N := AlgHom.funLike instance {M N : AlgebraCat.{v} R} : AlgHomClass (M ⟶ N) R M N := AlgHom.algHomClass instance : ConcreteCategory.{v} (AlgebraCat.{v} R) where forget := { obj := fun R => R map := fun f => f.toFun } forget_faithful := ⟨fun h => AlgHom.ext (by intros x; dsimp at h; rw [h])⟩ instance {S : AlgebraCat.{v} R} : Ring ((forget (AlgebraCat R)).obj S) := (inferInstance : Ring S.carrier) instance {S : AlgebraCat.{v} R} : Algebra R ((forget (AlgebraCat R)).obj S) := (inferInstance : Algebra R S.carrier) instance hasForgetToRing : HasForget₂ (AlgebraCat.{v} R) RingCat.{v} where forget₂ := { obj := fun A => RingCat.of A map := fun f => RingCat.ofHom f.toRingHom } instance hasForgetToModule : HasForget₂ (AlgebraCat.{v} R) (ModuleCat.{v} R) where forget₂ := { obj := fun M => ModuleCat.of R M map := fun f => ModuleCat.ofHom f.toLinearMap } @[simp] lemma forget₂_module_obj (X : AlgebraCat.{v} R) : (forget₂ (AlgebraCat.{v} R) (ModuleCat.{v} R)).obj X = ModuleCat.of R X := rfl @[simp] lemma forget₂_module_map {X Y : AlgebraCat.{v} R} (f : X ⟶ Y) : (forget₂ (AlgebraCat.{v} R) (ModuleCat.{v} R)).map f = ModuleCat.ofHom f.toLinearMap := rfl /-- The object in the category of R-algebras associated to a type equipped with the appropriate typeclasses. -/ def of (X : Type v) [Ring X] [Algebra R X] : AlgebraCat.{v} R := ⟨X⟩ /-- Typecheck a `AlgHom` as a morphism in `AlgebraCat R`. -/ def ofHom {R : Type u} [CommRing R] {X Y : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] (f : X →ₐ[R] Y) : of R X ⟶ of R Y := f @[simp] theorem ofHom_apply {R : Type u} [CommRing R] {X Y : Type v} [Ring X] [Algebra R X] [Ring Y] [Algebra R Y] (f : X →ₐ[R] Y) (x : X) : ofHom f x = f x := rfl instance : Inhabited (AlgebraCat R) := ⟨of R R⟩ @[simp] theorem coe_of (X : Type u) [Ring X] [Algebra R X] : (of R X : Type u) = X := rfl variable {R} /-- Forgetting to the underlying type and then building the bundled object returns the original algebra. -/ @[simps] def ofSelfIso (M : AlgebraCat.{v} R) : AlgebraCat.of R M ≅ M where hom := 𝟙 M inv := 𝟙 M variable {M N U : ModuleCat.{v} R} @[simp] theorem id_apply (m : M) : (𝟙 M : M → M) m = m := rfl @[simp] theorem coe_comp (f : M ⟶ N) (g : N ⟶ U) : (f ≫ g : M → U) = g ∘ f := rfl variable (R) /-- The "free algebra" functor, sending a type `S` to the free algebra on `S`. -/ @[simps!] def free : Type u ⥤ AlgebraCat.{u} R where obj S := { carrier := FreeAlgebra R S isRing := Algebra.semiringToRing R } map f := FreeAlgebra.lift _ <| FreeAlgebra.ι _ ∘ f -- Porting note (#11041): `apply FreeAlgebra.hom_ext` was `ext1`. map_id := by intro X; apply FreeAlgebra.hom_ext; simp only [FreeAlgebra.ι_comp_lift]; rfl map_comp := by -- Porting note (#11041): `apply FreeAlgebra.hom_ext` was `ext1`. intros; apply FreeAlgebra.hom_ext; simp only [FreeAlgebra.ι_comp_lift]; ext1 -- Porting node: this ↓ `erw` used to be handled by the `simp` below it erw [CategoryTheory.coe_comp] simp only [CategoryTheory.coe_comp, Function.comp_apply, types_comp_apply] -- Porting node: this ↓ `erw` and `rfl` used to be handled by the `simp` above erw [FreeAlgebra.lift_ι_apply, FreeAlgebra.lift_ι_apply] rfl /-- The free/forget adjunction for `R`-algebras. -/ def adj : free.{u} R ⊣ forget (AlgebraCat.{u} R) := Adjunction.mkOfHomEquiv { homEquiv := fun X A => (FreeAlgebra.lift _).symm -- Relying on `obviously` to fill out these proofs is very slow :( homEquiv_naturality_left_symm := by -- Porting note (#11041): `apply FreeAlgebra.hom_ext` was `ext1`. intros; apply FreeAlgebra.hom_ext; simp only [FreeAlgebra.ι_comp_lift]; ext1 simp only [free_map, Equiv.symm_symm, FreeAlgebra.lift_ι_apply, CategoryTheory.coe_comp, Function.comp_apply, types_comp_apply] -- Porting node: this ↓ `erw` and `rfl` used to be handled by the `simp` above erw [FreeAlgebra.lift_ι_apply, CategoryTheory.comp_apply, FreeAlgebra.lift_ι_apply, Function.comp_apply, FreeAlgebra.lift_ι_apply] rfl homEquiv_naturality_right := by intros; ext simp only [CategoryTheory.coe_comp, Function.comp_apply, FreeAlgebra.lift_symm_apply, types_comp_apply] -- Porting note: proof used to be done after this ↑ `simp`; added ↓ two lines erw [FreeAlgebra.lift_symm_apply, FreeAlgebra.lift_symm_apply] rfl } instance : (forget (AlgebraCat.{u} R)).IsRightAdjoint := (adj R).isRightAdjoint end AlgebraCat variable {R} variable {X₁ X₂ : Type u} /-- Build an isomorphism in the category `AlgebraCat R` from a `AlgEquiv` between `Algebra`s. -/ @[simps] def AlgEquiv.toAlgebraIso {g₁ : Ring X₁} {g₂ : Ring X₂} {m₁ : Algebra R X₁} {m₂ : Algebra R X₂} (e : X₁ ≃ₐ[R] X₂) : AlgebraCat.of R X₁ ≅ AlgebraCat.of R X₂ where hom := (e : X₁ →ₐ[R] X₂) inv := (e.symm : X₂ →ₐ[R] X₁) hom_inv_id := by ext x; exact e.left_inv x inv_hom_id := by ext x; exact e.right_inv x namespace CategoryTheory.Iso /-- Build a `AlgEquiv` from an isomorphism in the category `AlgebraCat R`. -/ @[simps] def toAlgEquiv {X Y : AlgebraCat R} (i : X ≅ Y) : X ≃ₐ[R] Y := { i.hom with toFun := i.hom invFun := i.inv left_inv := fun x => by -- Porting note: was `by tidy` change (i.hom ≫ i.inv) x = x simp only [hom_inv_id] -- This used to be `rw`, but we need `erw` after leanprover/lean4#2644 erw [id_apply] right_inv := fun x => by -- Porting note: was `by tidy` change (i.inv ≫ i.hom) x = x simp only [inv_hom_id] -- This used to be `rw`, but we need `erw` after leanprover/lean4#2644 erw [id_apply] } end CategoryTheory.Iso /-- Algebra equivalences between `Algebra`s are the same as (isomorphic to) isomorphisms in `AlgebraCat`. -/ @[simps] def algEquivIsoAlgebraIso {X Y : Type u} [Ring X] [Ring Y] [Algebra R X] [Algebra R Y] : (X ≃ₐ[R] Y) ≅ AlgebraCat.of R X ≅ AlgebraCat.of R Y where hom e := e.toAlgebraIso inv i := i.toAlgEquiv instance AlgebraCat.forget_reflects_isos : (forget (AlgebraCat.{u} R)).ReflectsIsomorphisms where reflects {X Y} f _ := by let i := asIso ((forget (AlgebraCat.{u} R)).map f) let e : X ≃ₐ[R] Y := { f, i.toEquiv with } exact e.toAlgebraIso.isIso_hom /-! `@[simp]` lemmas for `AlgHom.comp` and categorical identities. -/ @[simp] theorem AlgHom.comp_id_algebraCat {R} [CommRing R] {G : AlgebraCat.{u} R} {H : Type u} [Ring H] [Algebra R H] (f : G →ₐ[R] H) : f.comp (𝟙 G) = f := Category.id_comp (AlgebraCat.ofHom f) @[simp] theorem AlgHom.id_algebraCat_comp {R} [CommRing R] {G : Type u} [Ring G] [Algebra R G] {H : AlgebraCat.{u} R} (f : G →ₐ[R] H) : AlgHom.comp (𝟙 H) f = f := Category.comp_id (AlgebraCat.ofHom f)
Algebra\Category\AlgebraCat\Limits.lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.Algebra.Category.AlgebraCat.Basic import Mathlib.Algebra.Category.ModuleCat.Basic import Mathlib.Algebra.Category.ModuleCat.Limits import Mathlib.Algebra.Category.Ring.Limits /-! # The category of R-algebras has all limits Further, these limits are preserved by the forgetful functor --- that is, the underlying types are just the limits in the category of types. -/ open CategoryTheory Limits universe v w u t -- `u` is determined by the ring, so can come later noncomputable section namespace AlgebraCat variable {R : Type u} [CommRing R] variable {J : Type v} [Category.{t} J] (F : J ⥤ AlgebraCat.{w} R) instance semiringObj (j) : Semiring ((F ⋙ forget (AlgebraCat R)).obj j) := inferInstanceAs <| Semiring (F.obj j) instance algebraObj (j) : Algebra R ((F ⋙ forget (AlgebraCat R)).obj j) := inferInstanceAs <| Algebra R (F.obj j) /-- The flat sections of a functor into `AlgebraCat R` form a submodule of all sections. -/ def sectionsSubalgebra : Subalgebra R (∀ j, F.obj j) := { SemiRingCat.sectionsSubsemiring (F ⋙ forget₂ (AlgebraCat R) RingCat.{w} ⋙ forget₂ RingCat SemiRingCat.{w}) with algebraMap_mem' := fun r _ _ f => (F.map f).commutes r } instance (F : J ⥤ AlgebraCat.{w} R) : Ring (F ⋙ forget _).sections := inferInstanceAs <| Ring (sectionsSubalgebra F) instance (F : J ⥤ AlgebraCat.{w} R) : Algebra R (F ⋙ forget _).sections := inferInstanceAs <| Algebra R (sectionsSubalgebra F) variable [Small.{w} (F ⋙ forget (AlgebraCat.{w} R)).sections] instance : Small.{w} (sectionsSubalgebra F) := inferInstanceAs <| Small.{w} (F ⋙ forget _).sections instance limitSemiring : Ring.{w} (Types.Small.limitCone.{v, w} (F ⋙ forget (AlgebraCat.{w} R))).pt := inferInstanceAs <| Ring (Shrink (sectionsSubalgebra F)) instance limitAlgebra : Algebra R (Types.Small.limitCone (F ⋙ forget (AlgebraCat.{w} R))).pt := inferInstanceAs <| Algebra R (Shrink (sectionsSubalgebra F)) /-- `limit.π (F ⋙ forget (AlgebraCat R)) j` as a `AlgHom`. -/ def limitπAlgHom (j) : (Types.Small.limitCone (F ⋙ forget (AlgebraCat R))).pt →ₐ[R] (F ⋙ forget (AlgebraCat.{w} R)).obj j := letI : Small.{w} (Functor.sections ((F ⋙ forget₂ _ RingCat ⋙ forget₂ _ SemiRingCat) ⋙ forget _)) := inferInstanceAs <| Small.{w} (F ⋙ forget _).sections { SemiRingCat.limitπRingHom (F ⋙ forget₂ (AlgebraCat R) RingCat.{w} ⋙ forget₂ RingCat SemiRingCat.{w}) j with toFun := (Types.Small.limitCone (F ⋙ forget (AlgebraCat.{w} R))).π.app j commutes' := fun x => by simp only [Types.Small.limitCone_π_app, ← Shrink.algEquiv_apply _ R, Types.Small.limitCone_pt, AlgEquiv.commutes] rfl } namespace HasLimits -- The next two definitions are used in the construction of `HasLimits (AlgebraCat R)`. -- After that, the limits should be constructed using the generic limits API, -- e.g. `limit F`, `limit.cone F`, and `limit.isLimit F`. /-- Construction of a limit cone in `AlgebraCat R`. (Internal use only; use the limits API.) -/ def limitCone : Cone F where pt := AlgebraCat.of R (Types.Small.limitCone (F ⋙ forget _)).pt π := { app := limitπAlgHom F naturality := fun _ _ f => AlgHom.coe_fn_injective ((Types.Small.limitCone (F ⋙ forget _)).π.naturality f) } /-- Witness that the limit cone in `AlgebraCat R` is a limit cone. (Internal use only; use the limits API.) -/ def limitConeIsLimit : IsLimit (limitCone.{v, w} F) := by refine IsLimit.ofFaithful (forget (AlgebraCat R)) (Types.Small.limitConeIsLimit.{v, w} _) -- Porting note: in mathlib3 the function term -- `fun v => ⟨fun j => ((forget (AlgebraCat R)).mapCone s).π.app j v` -- was provided by unification, and the last argument `(fun s => _)` was `(fun s => rfl)`. (fun s => { toFun := _, map_one' := ?_, map_mul' := ?_, map_zero' := ?_, map_add' := ?_, commutes' := ?_ }) (fun s => rfl) · congr ext j simp only [Functor.comp_obj, Functor.mapCone_pt, Functor.mapCone_π_app, forget_map_eq_coe] erw [map_one] rfl · intro x y simp only [Functor.comp_obj, Functor.mapCone_pt, Functor.mapCone_π_app] erw [← map_mul (MulEquiv.symm Shrink.mulEquiv)] apply congrArg ext j simp only [Functor.comp_obj, Functor.mapCone_pt, Functor.mapCone_π_app, forget_map_eq_coe, map_mul] rfl · simp only [Functor.mapCone_π_app, forget_map_eq_coe] congr funext j simp only [map_zero, Pi.zero_apply] · intro x y simp only [Functor.mapCone_π_app] erw [← map_add (AddEquiv.symm Shrink.addEquiv)] apply congrArg ext j simp only [forget_map_eq_coe, map_add] rfl · intro r simp only [← Shrink.algEquiv_symm_apply _ R, limitCone, Equiv.algebraMap_def, Equiv.symm_symm] apply congrArg apply Subtype.ext ext j exact (s.π.app j).commutes r end HasLimits open HasLimits -- Porting note: mathport translated this as `irreducible_def`, but as `HasLimitsOfSize` -- is a `Prop`, declaring this as `irreducible` should presumably have no effect /-- The category of R-algebras has all limits. -/ lemma hasLimitsOfSize [UnivLE.{v, w}] : HasLimitsOfSize.{t, v} (AlgebraCat.{w} R) := { has_limits_of_shape := fun _ _ => { has_limit := fun F => HasLimit.mk { cone := limitCone F isLimit := limitConeIsLimit F } } } instance hasLimits : HasLimits (AlgebraCat.{w} R) := AlgebraCat.hasLimitsOfSize.{w, w, u} /-- The forgetful functor from R-algebras to rings preserves all limits. -/ instance forget₂RingPreservesLimitsOfSize [UnivLE.{v, w}] : PreservesLimitsOfSize.{t, v} (forget₂ (AlgebraCat.{w} R) RingCat.{w}) where preservesLimitsOfShape := { preservesLimit := fun {K} ↦ preservesLimitOfPreservesLimitCone (limitConeIsLimit K) (RingCat.limitConeIsLimit.{v, w} (_ ⋙ forget₂ (AlgebraCat.{w} R) RingCat.{w})) } instance forget₂RingPreservesLimits : PreservesLimits (forget₂ (AlgebraCat R) RingCat.{w}) := AlgebraCat.forget₂RingPreservesLimitsOfSize.{w, w} /-- The forgetful functor from R-algebras to R-modules preserves all limits. -/ instance forget₂ModulePreservesLimitsOfSize [UnivLE.{v, w}] : PreservesLimitsOfSize.{t, v} (forget₂ (AlgebraCat.{w} R) (ModuleCat.{w} R)) where preservesLimitsOfShape := { preservesLimit := fun {K} ↦ preservesLimitOfPreservesLimitCone (limitConeIsLimit K) (ModuleCat.HasLimits.limitConeIsLimit (K ⋙ forget₂ (AlgebraCat.{w} R) (ModuleCat.{w} R))) } instance forget₂ModulePreservesLimits : PreservesLimits (forget₂ (AlgebraCat R) (ModuleCat.{w} R)) := AlgebraCat.forget₂ModulePreservesLimitsOfSize.{w, w} /-- The forgetful functor from R-algebras to types preserves all limits. -/ instance forgetPreservesLimitsOfSize [UnivLE.{v, w}] : PreservesLimitsOfSize.{t, v} (forget (AlgebraCat.{w} R)) where preservesLimitsOfShape := { preservesLimit := fun {K} ↦ preservesLimitOfPreservesLimitCone (limitConeIsLimit K) (Types.Small.limitConeIsLimit.{v} (K ⋙ forget _)) } instance forgetPreservesLimits : PreservesLimits (forget (AlgebraCat.{w} R)) := AlgebraCat.forgetPreservesLimitsOfSize.{w, w} end AlgebraCat
Algebra\Category\AlgebraCat\Monoidal.lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.CategoryTheory.Monoidal.Transport import Mathlib.Algebra.Category.AlgebraCat.Basic import Mathlib.Algebra.Category.ModuleCat.Monoidal.Basic import Mathlib.RingTheory.TensorProduct.Basic /-! # The monoidal category structure on R-algebras -/ open CategoryTheory open scoped MonoidalCategory universe v u variable {R : Type u} [CommRing R] namespace AlgebraCat noncomputable section namespace instMonoidalCategory open scoped TensorProduct /-- Auxiliary definition used to fight a timeout when building `AlgebraCat.instMonoidalCategory`. -/ @[simps!] noncomputable abbrev tensorObj (X Y : AlgebraCat.{u} R) : AlgebraCat.{u} R := of R (X ⊗[R] Y) /-- Auxiliary definition used to fight a timeout when building `AlgebraCat.instMonoidalCategory`. -/ noncomputable abbrev tensorHom {W X Y Z : AlgebraCat.{u} R} (f : W ⟶ X) (g : Y ⟶ Z) : tensorObj W Y ⟶ tensorObj X Z := Algebra.TensorProduct.map f g open MonoidalCategory end instMonoidalCategory open instMonoidalCategory instance : MonoidalCategoryStruct (AlgebraCat.{u} R) where tensorObj := instMonoidalCategory.tensorObj whiskerLeft X _ _ f := tensorHom (𝟙 X) f whiskerRight {X₁ X₂} (f : X₁ ⟶ X₂) Y := tensorHom f (𝟙 Y) tensorHom := tensorHom tensorUnit := of R R associator X Y Z := (Algebra.TensorProduct.assoc R X Y Z).toAlgebraIso leftUnitor X := (Algebra.TensorProduct.lid R X).toAlgebraIso rightUnitor X := (Algebra.TensorProduct.rid R R X).toAlgebraIso theorem forget₂_map_associator_hom (X Y Z : AlgebraCat.{u} R) : (forget₂ (AlgebraCat R) (ModuleCat R)).map (α_ X Y Z).hom = (α_ (forget₂ _ (ModuleCat R) |>.obj X) (forget₂ _ (ModuleCat R) |>.obj Y) (forget₂ _ (ModuleCat R) |>.obj Z)).hom := by rfl theorem forget₂_map_associator_inv (X Y Z : AlgebraCat.{u} R) : (forget₂ (AlgebraCat R) (ModuleCat R)).map (α_ X Y Z).inv = (α_ (forget₂ _ (ModuleCat R) |>.obj X) (forget₂ _ (ModuleCat R) |>.obj Y) (forget₂ _ (ModuleCat R) |>.obj Z)).inv := by rfl set_option maxHeartbeats 800000 in noncomputable instance instMonoidalCategory : MonoidalCategory (AlgebraCat.{u} R) := Monoidal.induced (forget₂ (AlgebraCat R) (ModuleCat R)) { μIso := fun X Y => Iso.refl _ εIso := Iso.refl _ associator_eq := fun X Y Z => by dsimp only [forget₂_module_obj, forget₂_map_associator_hom] simp only [eqToIso_refl, Iso.refl_trans, Iso.refl_symm, Iso.trans_hom, tensorIso_hom, Iso.refl_hom, MonoidalCategory.tensor_id] erw [Category.id_comp, Category.comp_id, MonoidalCategory.tensor_id, Category.id_comp] leftUnitor_eq := fun X => by dsimp only [forget₂_module_obj, forget₂_module_map, Iso.refl_symm, Iso.trans_hom, Iso.refl_hom, tensorIso_hom] erw [Category.id_comp, MonoidalCategory.tensor_id, Category.id_comp] rfl rightUnitor_eq := fun X => by dsimp erw [Category.id_comp, MonoidalCategory.tensor_id, Category.id_comp] exact congr_arg LinearEquiv.toLinearMap <| TensorProduct.AlgebraTensorModule.rid_eq_rid R X } variable (R) in /-- `forget₂ (AlgebraCat R) (ModuleCat R)` as a monoidal functor. -/ def toModuleCatMonoidalFunctor : MonoidalFunctor (AlgebraCat.{u} R) (ModuleCat.{u} R) := by unfold instMonoidalCategory exact Monoidal.fromInduced (forget₂ (AlgebraCat R) (ModuleCat R)) _ instance : (toModuleCatMonoidalFunctor R).Faithful := forget₂_faithful _ _ end end AlgebraCat
Algebra\Category\AlgebraCat\Symmetric.lean
/- Copyright (c) 2023 Eric Wieser. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Eric Wieser -/ import Mathlib.Algebra.Category.AlgebraCat.Monoidal import Mathlib.Algebra.Category.ModuleCat.Monoidal.Symmetric /-! # The monoidal structure on `AlgebraCat` is symmetric. In this file we show: * `AlgebraCat.instSymmetricCategory : SymmetricCategory (AlgebraCat.{u} R)` -/ open CategoryTheory noncomputable section universe v u variable {R : Type u} [CommRing R] namespace AlgebraCat instance : BraidedCategory (AlgebraCat.{u} R) := braidedCategoryOfFaithful (toModuleCatMonoidalFunctor R) (fun X Y => (Algebra.TensorProduct.comm R X Y).toAlgebraIso) (by aesop_cat) variable (R) in /-- `forget₂ (AlgebraCat R) (ModuleCat R)` as a braided functor. -/ @[simps toMonoidalFunctor] def toModuleCatBraidedFunctor : BraidedFunctor (AlgebraCat.{u} R) (ModuleCat.{u} R) where toMonoidalFunctor := toModuleCatMonoidalFunctor R instance : (toModuleCatBraidedFunctor R).Faithful := forget₂_faithful _ _ instance instSymmetricCategory : SymmetricCategory (AlgebraCat.{u} R) := symmetricCategoryOfFaithful (toModuleCatBraidedFunctor R) end AlgebraCat
Algebra\Category\BialgebraCat\Basic.lean
/- Copyright (c) 2024 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import Mathlib.Algebra.Category.CoalgebraCat.Basic import Mathlib.Algebra.Category.AlgebraCat.Basic import Mathlib.RingTheory.Bialgebra.Equiv /-! # The category of bialgebras over a commutative ring We introduce the bundled category `BialgebraCat` of bialgebras over a fixed commutative ring `R` along with the forgetful functors to `CoalgebraCat` and `AlgebraCat`. This file mimics `Mathlib.LinearAlgebra.QuadraticForm.QuadraticModuleCat`. -/ open CategoryTheory universe v u variable (R : Type u) [CommRing R] /-- The category of `R`-bialgebras. -/ structure BialgebraCat extends Bundled Ring.{v} where [instBialgebra : Bialgebra R α] attribute [instance] BialgebraCat.instBialgebra variable {R} namespace BialgebraCat open Bialgebra instance : CoeSort (BialgebraCat.{v} R) (Type v) := ⟨(·.α)⟩ variable (R) /-- The object in the category of `R`-bialgebras associated to an `R`-bialgebra. -/ @[simps] def of (X : Type v) [Ring X] [Bialgebra R X] : BialgebraCat R where instBialgebra := (inferInstance : Bialgebra R X) variable {R} @[simp] lemma of_comul {X : Type v} [Ring X] [Bialgebra R X] : Coalgebra.comul (A := of R X) = Coalgebra.comul (R := R) (A := X) := rfl @[simp] lemma of_counit {X : Type v} [Ring X] [Bialgebra R X] : Coalgebra.counit (A := of R X) = Coalgebra.counit (R := R) (A := X) := rfl /-- A type alias for `BialgHom` to avoid confusion between the categorical and algebraic spellings of composition. -/ @[ext] structure Hom (V W : BialgebraCat.{v} R) := /-- The underlying `BialgHom` -/ toBialgHom : V →ₐc[R] W lemma Hom.toBialgHom_injective (V W : BialgebraCat.{v} R) : Function.Injective (Hom.toBialgHom : Hom V W → _) := fun ⟨f⟩ ⟨g⟩ _ => by congr instance category : Category (BialgebraCat.{v} R) where Hom X Y := Hom X Y id X := ⟨BialgHom.id R X⟩ comp f g := ⟨BialgHom.comp g.toBialgHom f.toBialgHom⟩ -- TODO: if `Quiver.Hom` and the instance above were `reducible`, this wouldn't be needed. @[ext] lemma hom_ext {X Y : BialgebraCat.{v} R} (f g : X ⟶ Y) (h : f.toBialgHom = g.toBialgHom) : f = g := Hom.ext h /-- Typecheck a `BialgHom` as a morphism in `BialgebraCat R`. -/ abbrev ofHom {X Y : Type v} [Ring X] [Ring Y] [Bialgebra R X] [Bialgebra R Y] (f : X →ₐc[R] Y) : of R X ⟶ of R Y := ⟨f⟩ @[simp] theorem toBialgHom_comp {X Y Z : BialgebraCat.{v} R} (f : X ⟶ Y) (g : Y ⟶ Z) : (f ≫ g).toBialgHom = g.toBialgHom.comp f.toBialgHom := rfl @[simp] theorem toBialgHom_id {M : BialgebraCat.{v} R} : Hom.toBialgHom (𝟙 M) = BialgHom.id _ _ := rfl instance concreteCategory : ConcreteCategory.{v} (BialgebraCat.{v} R) where forget := { obj := fun M => M map := fun f => f.toBialgHom } forget_faithful := { map_injective := fun {M N} => DFunLike.coe_injective.comp <| Hom.toBialgHom_injective _ _ } instance hasForgetToAlgebra : HasForget₂ (BialgebraCat R) (AlgebraCat R) where forget₂ := { obj := fun X => AlgebraCat.of R X map := fun {X Y} f => (f.toBialgHom : X →ₐ[R] Y) } @[simp] theorem forget₂_algebra_obj (X : BialgebraCat R) : (forget₂ (BialgebraCat R) (AlgebraCat R)).obj X = AlgebraCat.of R X := rfl @[simp] theorem forget₂_algebra_map (X Y : BialgebraCat R) (f : X ⟶ Y) : (forget₂ (BialgebraCat R) (AlgebraCat R)).map f = (f.toBialgHom : X →ₐ[R] Y) := rfl instance hasForgetToCoalgebra : HasForget₂ (BialgebraCat R) (CoalgebraCat R) where forget₂ := { obj := fun X => CoalgebraCat.of R X map := fun {X Y} f => CoalgebraCat.ofHom f.toBialgHom } @[simp] theorem forget₂_coalgebra_obj (X : BialgebraCat R) : (forget₂ (BialgebraCat R) (CoalgebraCat R)).obj X = CoalgebraCat.of R X := rfl @[simp] theorem forget₂_coalgebra_map (X Y : BialgebraCat R) (f : X ⟶ Y) : (forget₂ (BialgebraCat R) (CoalgebraCat R)).map f = CoalgebraCat.ofHom f.toBialgHom := rfl end BialgebraCat namespace BialgEquiv open BialgebraCat variable {X Y Z : Type v} variable [Ring X] [Ring Y] [Ring Z] variable [Bialgebra R X] [Bialgebra R Y] [Bialgebra R Z] /-- Build an isomorphism in the category `BialgebraCat R` from a `BialgEquiv`. -/ @[simps] def toBialgebraCatIso (e : X ≃ₐc[R] Y) : BialgebraCat.of R X ≅ BialgebraCat.of R Y where hom := BialgebraCat.ofHom e inv := BialgebraCat.ofHom e.symm hom_inv_id := Hom.ext <| DFunLike.ext _ _ e.left_inv inv_hom_id := Hom.ext <| DFunLike.ext _ _ e.right_inv @[simp] theorem toBialgebraCatIso_refl : toBialgebraCatIso (BialgEquiv.refl R X) = .refl _ := rfl @[simp] theorem toBialgebraCatIso_symm (e : X ≃ₐc[R] Y) : toBialgebraCatIso e.symm = (toBialgebraCatIso e).symm := rfl @[simp] theorem toBialgebraCatIso_trans (e : X ≃ₐc[R] Y) (f : Y ≃ₐc[R] Z) : toBialgebraCatIso (e.trans f) = toBialgebraCatIso e ≪≫ toBialgebraCatIso f := rfl end BialgEquiv namespace CategoryTheory.Iso open Bialgebra variable {X Y Z : BialgebraCat.{v} R} /-- Build a `BialgEquiv` from an isomorphism in the category `BialgebraCat R`. -/ def toBialgEquiv (i : X ≅ Y) : X ≃ₐc[R] Y := { i.hom.toBialgHom with invFun := i.inv.toBialgHom left_inv := fun x => BialgHom.congr_fun (congr_arg BialgebraCat.Hom.toBialgHom i.3) x right_inv := fun x => BialgHom.congr_fun (congr_arg BialgebraCat.Hom.toBialgHom i.4) x } @[simp] theorem toBialgEquiv_toBialgHom (i : X ≅ Y) : (i.toBialgEquiv : X →ₐc[R] Y) = i.hom.1 := rfl @[simp] theorem toBialgEquiv_refl : toBialgEquiv (.refl X) = .refl _ _ := rfl @[simp] theorem toBialgEquiv_symm (e : X ≅ Y) : toBialgEquiv e.symm = (toBialgEquiv e).symm := rfl @[simp] theorem toBialgEquiv_trans (e : X ≅ Y) (f : Y ≅ Z) : toBialgEquiv (e ≪≫ f) = e.toBialgEquiv.trans f.toBialgEquiv := rfl end CategoryTheory.Iso instance BialgebraCat.forget_reflects_isos : (forget (BialgebraCat.{v} R)).ReflectsIsomorphisms where reflects {X Y} f _ := by let i := asIso ((forget (BialgebraCat.{v} R)).map f) let e : X ≃ₐc[R] Y := { f.toBialgHom, i.toEquiv with } exact ⟨e.toBialgebraCatIso.isIso_hom.1⟩
Algebra\Category\CoalgebraCat\Basic.lean
/- Copyright (c) 2024 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import Mathlib.Algebra.Category.ModuleCat.Basic import Mathlib.RingTheory.Coalgebra.Equiv /-! # The category of coalgebras over a commutative ring We introduce the bundled category `CoalgebraCat` of coalgebras over a fixed commutative ring `R` along with the forgetful functor to `ModuleCat`. This file mimics `Mathlib.LinearAlgebra.QuadraticForm.QuadraticModuleCat`. -/ open CategoryTheory universe v u variable (R : Type u) [CommRing R] /-- The category of `R`-coalgebras. -/ structure CoalgebraCat extends ModuleCat.{v} R where instCoalgebra : Coalgebra R carrier attribute [instance] CoalgebraCat.instCoalgebra variable {R} namespace CoalgebraCat open Coalgebra instance : CoeSort (CoalgebraCat.{v} R) (Type v) := ⟨(·.carrier)⟩ @[simp] theorem moduleCat_of_toModuleCat (X : CoalgebraCat.{v} R) : ModuleCat.of R X.toModuleCat = X.toModuleCat := rfl variable (R) /-- The object in the category of `R`-coalgebras associated to an `R`-coalgebra. -/ @[simps] def of (X : Type v) [AddCommGroup X] [Module R X] [Coalgebra R X] : CoalgebraCat R where instCoalgebra := (inferInstance : Coalgebra R X) variable {R} @[simp] lemma of_comul {X : Type v} [AddCommGroup X] [Module R X] [Coalgebra R X] : Coalgebra.comul (A := of R X) = Coalgebra.comul (R := R) (A := X) := rfl @[simp] lemma of_counit {X : Type v} [AddCommGroup X] [Module R X] [Coalgebra R X] : Coalgebra.counit (A := of R X) = Coalgebra.counit (R := R) (A := X) := rfl /-- A type alias for `CoalgHom` to avoid confusion between the categorical and algebraic spellings of composition. -/ @[ext] structure Hom (V W : CoalgebraCat.{v} R) := /-- The underlying `CoalgHom` -/ toCoalgHom : V →ₗc[R] W lemma Hom.toCoalgHom_injective (V W : CoalgebraCat.{v} R) : Function.Injective (Hom.toCoalgHom : Hom V W → _) := fun ⟨f⟩ ⟨g⟩ _ => by congr instance category : Category (CoalgebraCat.{v} R) where Hom M N := Hom M N id M := ⟨CoalgHom.id R M⟩ comp f g := ⟨CoalgHom.comp g.toCoalgHom f.toCoalgHom⟩ -- TODO: if `Quiver.Hom` and the instance above were `reducible`, this wouldn't be needed. @[ext] lemma hom_ext {M N : CoalgebraCat.{v} R} (f g : M ⟶ N) (h : f.toCoalgHom = g.toCoalgHom) : f = g := Hom.ext h /-- Typecheck a `CoalgHom` as a morphism in `CoalgebraCat R`. -/ abbrev ofHom {X Y : Type v} [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] [Coalgebra R X] [Coalgebra R Y] (f : X →ₗc[R] Y) : of R X ⟶ of R Y := ⟨f⟩ @[simp] theorem toCoalgHom_comp {M N U : CoalgebraCat.{v} R} (f : M ⟶ N) (g : N ⟶ U) : (f ≫ g).toCoalgHom = g.toCoalgHom.comp f.toCoalgHom := rfl @[simp] theorem toCoalgHom_id {M : CoalgebraCat.{v} R} : Hom.toCoalgHom (𝟙 M) = CoalgHom.id _ _ := rfl instance concreteCategory : ConcreteCategory.{v} (CoalgebraCat.{v} R) where forget := { obj := fun M => M map := fun f => f.toCoalgHom } forget_faithful := { map_injective := fun {M N} => DFunLike.coe_injective.comp <| Hom.toCoalgHom_injective _ _ } instance hasForgetToModule : HasForget₂ (CoalgebraCat R) (ModuleCat R) where forget₂ := { obj := fun M => ModuleCat.of R M map := fun f => f.toCoalgHom.toLinearMap } @[simp] theorem forget₂_obj (X : CoalgebraCat R) : (forget₂ (CoalgebraCat R) (ModuleCat R)).obj X = ModuleCat.of R X := rfl @[simp] theorem forget₂_map (X Y : CoalgebraCat R) (f : X ⟶ Y) : (forget₂ (CoalgebraCat R) (ModuleCat R)).map f = (f.toCoalgHom : X →ₗ[R] Y) := rfl end CoalgebraCat namespace CoalgEquiv open CoalgebraCat variable {X Y Z : Type v} variable [AddCommGroup X] [Module R X] [AddCommGroup Y] [Module R Y] [AddCommGroup Z] [Module R Z] variable [Coalgebra R X] [Coalgebra R Y] [Coalgebra R Z] /-- Build an isomorphism in the category `CoalgebraCat R` from a `CoalgEquiv`. -/ @[simps] def toCoalgebraCatIso (e : X ≃ₗc[R] Y) : CoalgebraCat.of R X ≅ CoalgebraCat.of R Y where hom := CoalgebraCat.ofHom e inv := CoalgebraCat.ofHom e.symm hom_inv_id := Hom.ext <| DFunLike.ext _ _ e.left_inv inv_hom_id := Hom.ext <| DFunLike.ext _ _ e.right_inv @[simp] theorem toCoalgebraCatIso_refl : toCoalgebraCatIso (CoalgEquiv.refl R X) = .refl _ := rfl @[simp] theorem toCoalgebraCatIso_symm (e : X ≃ₗc[R] Y) : toCoalgebraCatIso e.symm = (toCoalgebraCatIso e).symm := rfl @[simp] theorem toCoalgebraCatIso_trans (e : X ≃ₗc[R] Y) (f : Y ≃ₗc[R] Z) : toCoalgebraCatIso (e.trans f) = toCoalgebraCatIso e ≪≫ toCoalgebraCatIso f := rfl end CoalgEquiv namespace CategoryTheory.Iso open Coalgebra variable {X Y Z : CoalgebraCat.{v} R} /-- Build a `CoalgEquiv` from an isomorphism in the category `CoalgebraCat R`. -/ def toCoalgEquiv (i : X ≅ Y) : X ≃ₗc[R] Y := { i.hom.toCoalgHom with invFun := i.inv.toCoalgHom left_inv := fun x => CoalgHom.congr_fun (congr_arg CoalgebraCat.Hom.toCoalgHom i.3) x right_inv := fun x => CoalgHom.congr_fun (congr_arg CoalgebraCat.Hom.toCoalgHom i.4) x } @[simp] theorem toCoalgEquiv_toCoalgHom (i : X ≅ Y) : i.toCoalgEquiv = i.hom.toCoalgHom := rfl @[simp] theorem toCoalgEquiv_refl : toCoalgEquiv (.refl X) = .refl _ _ := rfl @[simp] theorem toCoalgEquiv_symm (e : X ≅ Y) : toCoalgEquiv e.symm = (toCoalgEquiv e).symm := rfl @[simp] theorem toCoalgEquiv_trans (e : X ≅ Y) (f : Y ≅ Z) : toCoalgEquiv (e ≪≫ f) = e.toCoalgEquiv.trans f.toCoalgEquiv := rfl end CategoryTheory.Iso instance CoalgebraCat.forget_reflects_isos : (forget (CoalgebraCat.{v} R)).ReflectsIsomorphisms where reflects {X Y} f _ := by let i := asIso ((forget (CoalgebraCat.{v} R)).map f) let e : X ≃ₗc[R] Y := { f.toCoalgHom, i.toEquiv with } exact ⟨e.toCoalgebraCatIso.isIso_hom.1⟩
Algebra\Category\CoalgebraCat\ComonEquivalence.lean
/- Copyright (c) 2024 Amelia Livingston. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Amelia Livingston -/ import Mathlib.CategoryTheory.Monoidal.Braided.Opposite import Mathlib.Algebra.Category.ModuleCat.Monoidal.Symmetric import Mathlib.CategoryTheory.Monoidal.Comon_ import Mathlib.Algebra.Category.CoalgebraCat.Basic /-! # The category equivalence between `R`-coalgebras and comonoid objects in `R-Mod` Given a commutative ring `R`, this file defines the equivalence of categories between `R`-coalgebras and comonoid objects in the category of `R`-modules. We then use this to set up boilerplate for the `Coalgebra` instance on a tensor product of coalgebras defined in `Mathlib.RingTheory.Coalgebra.TensorProduct` in #11975. ## Implementation notes We make the definiton `CoalgebraCat.instMonoidalCategoryAux` in this file, which is the monoidal structure on `CoalgebraCat` induced by the equivalence with `Comon(R-Mod)`. We use this to show the comultiplication and counit on a tensor product of coalgebras satisfy the coalgebra axioms, but our actual `MonoidalCategory` instance on `CoalgebraCat` is constructed in `Mathlib.Algebra.Category.CoalgebraCat.Monoidal` in #11976 to have better definitional equalities. -/ universe v u namespace CoalgebraCat open CategoryTheory MonoidalCategory variable {R : Type u} [CommRing R] /-- An `R`-coalgebra is a comonoid object in the category of `R`-modules. -/ @[simps] def toComonObj (X : CoalgebraCat R) : Comon_ (ModuleCat R) where X := ModuleCat.of R X counit := ModuleCat.ofHom Coalgebra.counit comul := ModuleCat.ofHom Coalgebra.comul counit_comul := by simpa only [ModuleCat.of_coe] using Coalgebra.rTensor_counit_comp_comul comul_counit := by simpa only [ModuleCat.of_coe] using Coalgebra.lTensor_counit_comp_comul comul_assoc := by simp_rw [ModuleCat.of_coe]; exact Coalgebra.coassoc.symm variable (R) in /-- The natural functor from `R`-coalgebras to comonoid objects in the category of `R`-modules. -/ @[simps] def toComon : CoalgebraCat R ⥤ Comon_ (ModuleCat R) where obj X := toComonObj X map f := { hom := ModuleCat.ofHom f.1 hom_counit := f.1.counit_comp hom_comul := f.1.map_comp_comul.symm } /-- A comonoid object in the category of `R`-modules has a natural comultiplication and counit. -/ @[simps] instance ofComonObjCoalgebraStruct (X : Comon_ (ModuleCat R)) : CoalgebraStruct R X.X where comul := X.comul counit := X.counit /-- A comonoid object in the category of `R`-modules has a natural `R`-coalgebra structure. -/ def ofComonObj (X : Comon_ (ModuleCat R)) : CoalgebraCat R where carrier := X.X instCoalgebra := { ofComonObjCoalgebraStruct X with coassoc := X.comul_assoc.symm rTensor_counit_comp_comul := X.counit_comul lTensor_counit_comp_comul := X.comul_counit } variable (R) /-- The natural functor from comonoid objects in the category of `R`-modules to `R`-coalgebras. -/ def ofComon : Comon_ (ModuleCat R) ⥤ CoalgebraCat R where obj X := ofComonObj X map f := { toCoalgHom := { f.hom with counit_comp := f.hom_counit map_comp_comul := f.hom_comul.symm }} /-- The natural category equivalence between `R`-coalgebras and comonoid objects in the category of `R`-modules. -/ @[simps] noncomputable def comonEquivalence : CoalgebraCat R ≌ Comon_ (ModuleCat R) where functor := toComon R inverse := ofComon R unitIso := NatIso.ofComponents (fun _ => Iso.refl _) fun _ => by rfl counitIso := NatIso.ofComponents (fun _ => Iso.refl _) fun _ => by rfl variable {R} /-- The monoidal category structure on the category of `R`-coalgebras induced by the equivalence with `Comon(R-Mod)`. This is just an auxiliary definition; the `MonoidalCategory` instance we make in `Mathlib.Algebra.Category.CoalgebraCat.Monoidal` in #11976 will have better definitional equalities. -/ noncomputable def instMonoidalCategoryAux : MonoidalCategory (CoalgebraCat R) := Monoidal.transport (comonEquivalence R).symm namespace MonoidalCategoryAux variable {M N P Q : Type u} [AddCommGroup M] [AddCommGroup N] [AddCommGroup P] [AddCommGroup Q] [Module R M] [Module R N] [Module R P] [Module R Q] [Coalgebra R M] [Coalgebra R N] [Coalgebra R P] [Coalgebra R Q] attribute [local instance] instMonoidalCategoryAux open MonoidalCategory ModuleCat.MonoidalCategory theorem tensorObj_comul (K L : CoalgebraCat R) : Coalgebra.comul (R := R) (A := (K ⊗ L : CoalgebraCat R)) = (TensorProduct.tensorTensorTensorComm R K K L L).toLinearMap ∘ₗ TensorProduct.map Coalgebra.comul Coalgebra.comul := by rw [ofComonObjCoalgebraStruct_comul] dsimp [ModuleCat.ofHom, -Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def] simp only [BraidedCategory.unop_tensor_μ, tensor_μ_eq_tensorTensorTensorComm] theorem tensorHom_toLinearMap (f : M →ₗc[R] N) (g : P →ₗc[R] Q) : (CoalgebraCat.ofHom f ⊗ CoalgebraCat.ofHom g).1.toLinearMap = TensorProduct.map f.toLinearMap g.toLinearMap := rfl theorem associator_hom_toLinearMap : (α_ (CoalgebraCat.of R M) (CoalgebraCat.of R N) (CoalgebraCat.of R P)).hom.1.toLinearMap = (TensorProduct.assoc R M N P).toLinearMap := TensorProduct.ext <| TensorProduct.ext <| by ext; rfl theorem leftUnitor_hom_toLinearMap : (λ_ (CoalgebraCat.of R M)).hom.1.toLinearMap = (TensorProduct.lid R M).toLinearMap := TensorProduct.ext <| by ext; rfl theorem rightUnitor_hom_toLinearMap : (ρ_ (CoalgebraCat.of R M)).hom.1.toLinearMap = (TensorProduct.rid R M).toLinearMap := TensorProduct.ext <| by ext; rfl open TensorProduct theorem comul_tensorObj : Coalgebra.comul (R := R) (A := (CoalgebraCat.of R M ⊗ CoalgebraCat.of R N : CoalgebraCat R)) = Coalgebra.comul (A := M ⊗[R] N) := by rw [ofComonObjCoalgebraStruct_comul] dsimp [- Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def, ModuleCat.ofHom, ModuleCat.of] simp only [BraidedCategory.unop_tensor_μ, tensor_μ_eq_tensorTensorTensorComm] theorem comul_tensorObj_tensorObj_right : Coalgebra.comul (R := R) (A := (CoalgebraCat.of R M ⊗ (CoalgebraCat.of R N ⊗ CoalgebraCat.of R P) : CoalgebraCat R)) = Coalgebra.comul (A := M ⊗[R] N ⊗[R] P) := by rw [ofComonObjCoalgebraStruct_comul] dsimp [- Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def, ModuleCat.ofHom, ModuleCat.of] rw [ofComonObjCoalgebraStruct_comul] dsimp [- Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def, ModuleCat.ofHom, ModuleCat.of] simp only [instMonoidalCategoryStruct_tensorObj, ModuleCat.MonoidalCategory.tensorObj, ModuleCat.coe_of, BraidedCategory.unop_tensor_μ, tensor_μ_eq_tensorTensorTensorComm] rfl theorem comul_tensorObj_tensorObj_left : Coalgebra.comul (R := R) (A := ((CoalgebraCat.of R M ⊗ CoalgebraCat.of R N) ⊗ CoalgebraCat.of R P : CoalgebraCat R)) = Coalgebra.comul (A := (M ⊗[R] N) ⊗[R] P) := by rw [ofComonObjCoalgebraStruct_comul] dsimp [- Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def, ModuleCat.ofHom, ModuleCat.of] rw [ofComonObjCoalgebraStruct_comul] dsimp [- Mon_.monMonoidalStruct_tensorObj_X, instMonoidalCategoryStruct_tensorHom, ModuleCat.comp_def, ModuleCat.ofHom, ModuleCat.of] simp only [instMonoidalCategoryStruct_tensorObj, ModuleCat.MonoidalCategory.tensorObj, ModuleCat.coe_of, BraidedCategory.unop_tensor_μ, tensor_μ_eq_tensorTensorTensorComm] rfl theorem counit_tensorObj : Coalgebra.counit (R := R) (A := (CoalgebraCat.of R M ⊗ CoalgebraCat.of R N : CoalgebraCat R)) = Coalgebra.counit (A := M ⊗[R] N) := by rfl theorem counit_tensorObj_tensorObj_right : Coalgebra.counit (R := R) (A := (CoalgebraCat.of R M ⊗ (CoalgebraCat.of R N ⊗ CoalgebraCat.of R P) : CoalgebraCat R)) = Coalgebra.counit (A := M ⊗[R] (N ⊗[R] P)) := by ext; rfl theorem counit_tensorObj_tensorObj_left : Coalgebra.counit (R := R) (A := ((CoalgebraCat.of R M ⊗ CoalgebraCat.of R N) ⊗ CoalgebraCat.of R P : CoalgebraCat R)) = Coalgebra.counit (A := (M ⊗[R] N) ⊗[R] P) := by ext; rfl end CoalgebraCat.MonoidalCategoryAux
Algebra\Category\FGModuleCat\Basic.lean
/- Copyright (c) 2021 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer -/ import Mathlib.CategoryTheory.Monoidal.Rigid.Basic import Mathlib.CategoryTheory.Monoidal.Subcategory import Mathlib.LinearAlgebra.Coevaluation import Mathlib.LinearAlgebra.FreeModule.Finite.Matrix import Mathlib.Algebra.Category.ModuleCat.Monoidal.Closed /-! # The category of finitely generated modules over a ring This introduces `FGModuleCat R`, the category of finitely generated modules over a ring `R`. It is implemented as a full subcategory on a subtype of `ModuleCat R`. When `K` is a field, `FGModuleCatCat K` is the category of finite dimensional vector spaces over `K`. We first create the instance as a preadditive category. When `R` is commutative we then give the structure as an `R`-linear monoidal category. When `R` is a field we give it the structure of a closed monoidal category and then as a right-rigid monoidal category. ## Future work * Show that `FGModuleCat R` is abelian when `R` is (left)-noetherian. -/ noncomputable section open CategoryTheory ModuleCat.monoidalCategory universe u section Ring variable (R : Type u) [Ring R] /-- Define `FGModuleCat` as the subtype of `ModuleCat.{u} R` of finitely generated modules. -/ def FGModuleCat := FullSubcategory fun V : ModuleCat.{u} R => Module.Finite R V -- Porting note: still no derive handler via `dsimp`. -- see https://github.com/leanprover-community/mathlib4/issues/5020 -- deriving LargeCategory, ConcreteCategory,Preadditive variable {R} /-- A synonym for `M.obj.carrier`, which we can mark with `@[coe]`. -/ def FGModuleCat.carrier (M : FGModuleCat R) : Type u := M.obj.carrier instance : CoeSort (FGModuleCat R) (Type u) := ⟨FGModuleCat.carrier⟩ attribute [coe] FGModuleCat.carrier @[simp] lemma obj_carrier (M : FGModuleCat R) : M.obj.carrier = M.carrier := rfl instance (M : FGModuleCat R) : AddCommGroup M := by change AddCommGroup M.obj infer_instance instance (M : FGModuleCat R) : Module R M := by change Module R M.obj infer_instance instance : LargeCategory (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance instance {M N : FGModuleCat R} : FunLike (M ⟶ N) M N := LinearMap.instFunLike instance {M N : FGModuleCat R} : LinearMapClass (M ⟶ N) R M N := LinearMap.semilinearMapClass instance : ConcreteCategory (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance instance : Preadditive (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance end Ring namespace FGModuleCat section Ring variable (R : Type u) [Ring R] instance finite (V : FGModuleCat R) : Module.Finite R V := V.property instance : Inhabited (FGModuleCat R) := ⟨⟨ModuleCat.of R R, Module.Finite.self R⟩⟩ /-- Lift an unbundled finitely generated module to `FGModuleCat R`. -/ def of (V : Type u) [AddCommGroup V] [Module R V] [Module.Finite R V] : FGModuleCat R := ⟨ModuleCat.of R V, by change Module.Finite R V; infer_instance⟩ instance (V : FGModuleCat R) : Module.Finite R V := V.property instance : HasForget₂ (FGModuleCat.{u} R) (ModuleCat.{u} R) := by dsimp [FGModuleCat] infer_instance instance : (forget₂ (FGModuleCat R) (ModuleCat.{u} R)).Full where map_surjective f := ⟨f, rfl⟩ variable {R} /-- Converts and isomorphism in the category `FGModuleCat R` to a `LinearEquiv` between the underlying modules. -/ def isoToLinearEquiv {V W : FGModuleCat R} (i : V ≅ W) : V ≃ₗ[R] W := ((forget₂ (FGModuleCat.{u} R) (ModuleCat.{u} R)).mapIso i).toLinearEquiv /-- Converts a `LinearEquiv` to an isomorphism in the category `FGModuleCat R`. -/ @[simps] def _root_.LinearEquiv.toFGModuleCatIso {V W : Type u} [AddCommGroup V] [Module R V] [Module.Finite R V] [AddCommGroup W] [Module R W] [Module.Finite R W] (e : V ≃ₗ[R] W) : FGModuleCat.of R V ≅ FGModuleCat.of R W where hom := e.toLinearMap inv := e.symm.toLinearMap hom_inv_id := by ext x; exact e.left_inv x inv_hom_id := by ext x; exact e.right_inv x end Ring section CommRing variable (R : Type u) [CommRing R] instance : Linear R (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance instance monoidalPredicate_module_finite : MonoidalCategory.MonoidalPredicate fun V : ModuleCat.{u} R => Module.Finite R V where prop_id := Module.Finite.self R prop_tensor := @fun X Y _ _ => Module.Finite.tensorProduct R X Y instance : MonoidalCategory (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance open MonoidalCategory @[simp] lemma tensorUnit_obj : (𝟙_ (FGModuleCat R)).obj = 𝟙_ (ModuleCat R) := rfl @[simp] lemma tensorObj_obj (M N : FGModuleCat.{u} R) : (M ⊗ N).obj = (M.obj ⊗ N.obj) := rfl instance : SymmetricCategory (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance instance : MonoidalPreadditive (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance instance : MonoidalLinear R (FGModuleCat R) := by dsimp [FGModuleCat] infer_instance /-- The forgetful functor `FGModuleCat R ⥤ Module R` as a monoidal functor. -/ def forget₂Monoidal : MonoidalFunctor (FGModuleCat R) (ModuleCat.{u} R) := MonoidalCategory.fullMonoidalSubcategoryInclusion _ instance forget₂Monoidal_faithful : (forget₂Monoidal R).Faithful := by dsimp [forget₂Monoidal] -- Porting note (#11187): was `infer_instance` exact FullSubcategory.faithful _ instance forget₂Monoidal_additive : (forget₂Monoidal R).Additive := by dsimp [forget₂Monoidal] -- Porting note (#11187): was `infer_instance` exact Functor.fullSubcategoryInclusion_additive _ instance forget₂Monoidal_linear : (forget₂Monoidal R).Linear R := by dsimp [forget₂Monoidal] -- Porting note (#11187): was `infer_instance` exact Functor.fullSubcategoryInclusionLinear _ _ theorem Iso.conj_eq_conj {V W : FGModuleCat R} (i : V ≅ W) (f : End V) : Iso.conj i f = LinearEquiv.conj (isoToLinearEquiv i) f := rfl end CommRing section Field variable (K : Type u) [Field K] instance (V W : FGModuleCat K) : Module.Finite K (V ⟶ W) := (by infer_instance : Module.Finite K (V →ₗ[K] W)) instance closedPredicateModuleFinite : MonoidalCategory.ClosedPredicate fun V : ModuleCat.{u} K ↦ Module.Finite K V where prop_ihom {X Y} _ _ := Module.Finite.linearMap K K X Y instance : MonoidalClosed (FGModuleCat K) := by dsimp [FGModuleCat] -- Porting note (#11187): was `infer_instance` exact MonoidalCategory.fullMonoidalClosedSubcategory (fun V : ModuleCat.{u} K => Module.Finite K V) variable (V W : FGModuleCat K) @[simp] theorem ihom_obj : (ihom V).obj W = FGModuleCat.of K (V →ₗ[K] W) := rfl /-- The dual module is the dual in the rigid monoidal category `FGModuleCat K`. -/ def FGModuleCatDual : FGModuleCat K := ⟨ModuleCat.of K (Module.Dual K V), Subspace.instModuleDualFiniteDimensional⟩ @[simp] lemma FGModuleCatDual_obj : (FGModuleCatDual K V).obj = ModuleCat.of K (Module.Dual K V) := rfl @[simp] lemma FGModuleCatDual_coe : (FGModuleCatDual K V : Type u) = Module.Dual K V := rfl open CategoryTheory.MonoidalCategory /-- The coevaluation map is defined in `LinearAlgebra.coevaluation`. -/ def FGModuleCatCoevaluation : 𝟙_ (FGModuleCat K) ⟶ V ⊗ FGModuleCatDual K V := coevaluation K V theorem FGModuleCatCoevaluation_apply_one : FGModuleCatCoevaluation K V (1 : K) = ∑ i : Basis.ofVectorSpaceIndex K V, (Basis.ofVectorSpace K V) i ⊗ₜ[K] (Basis.ofVectorSpace K V).coord i := coevaluation_apply_one K V /-- The evaluation morphism is given by the contraction map. -/ def FGModuleCatEvaluation : FGModuleCatDual K V ⊗ V ⟶ 𝟙_ (FGModuleCat K) := contractLeft K V @[simp] theorem FGModuleCatEvaluation_apply (f : FGModuleCatDual K V) (x : V) : (FGModuleCatEvaluation K V) (f ⊗ₜ x) = f.toFun x := contractLeft_apply f x private theorem coevaluation_evaluation : letI V' : FGModuleCat K := FGModuleCatDual K V V' ◁ FGModuleCatCoevaluation K V ≫ (α_ V' V V').inv ≫ FGModuleCatEvaluation K V ▷ V' = (ρ_ V').hom ≫ (λ_ V').inv := by apply contractLeft_assoc_coevaluation K V private theorem evaluation_coevaluation : FGModuleCatCoevaluation K V ▷ V ≫ (α_ V (FGModuleCatDual K V) V).hom ≫ V ◁ FGModuleCatEvaluation K V = (λ_ V).hom ≫ (ρ_ V).inv := by apply contractLeft_assoc_coevaluation' K V instance exactPairing : ExactPairing V (FGModuleCatDual K V) where coevaluation' := FGModuleCatCoevaluation K V evaluation' := FGModuleCatEvaluation K V coevaluation_evaluation' := coevaluation_evaluation K V evaluation_coevaluation' := evaluation_coevaluation K V instance rightDual : HasRightDual V := ⟨FGModuleCatDual K V⟩ instance rightRigidCategory : RightRigidCategory (FGModuleCat K) where end Field end FGModuleCat /-! `@[simp]` lemmas for `LinearMap.comp` and categorical identities. -/ @[simp] theorem LinearMap.comp_id_fgModuleCat {R} [Ring R] {G : FGModuleCat.{u} R} {H : Type u} [AddCommGroup H] [Module R H] (f : G →ₗ[R] H) : f.comp (𝟙 G) = f := Category.id_comp (ModuleCat.ofHom f) @[simp] theorem LinearMap.id_fgModuleCat_comp {R} [Ring R] {G : Type u} [AddCommGroup G] [Module R G] {H : FGModuleCat.{u} R} (f : G →ₗ[R] H) : LinearMap.comp (𝟙 H) f = f := Category.comp_id (ModuleCat.ofHom f)
Algebra\Category\FGModuleCat\Limits.lean
/- Copyright (c) 2022 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import Mathlib.Algebra.Category.FGModuleCat.Basic import Mathlib.Algebra.Category.ModuleCat.Limits import Mathlib.Algebra.Category.ModuleCat.Products import Mathlib.Algebra.Category.ModuleCat.EpiMono import Mathlib.CategoryTheory.Limits.Creates import Mathlib.CategoryTheory.Limits.Shapes.FiniteLimits import Mathlib.CategoryTheory.Limits.Constructions.LimitsOfProductsAndEqualizers /-! # `forget₂ (FGModuleCat K) (ModuleCat K)` creates all finite limits. And hence `FGModuleCat K` has all finite limits. ## Future work After generalising `FGModuleCat` to allow the ring and the module to live in different universes, generalize this construction so we can take limits over smaller diagrams, as is done for the other algebraic categories. Analogous constructions for Noetherian modules. -/ noncomputable section universe v u open CategoryTheory Limits namespace FGModuleCat variable {J : Type} [SmallCategory J] [FinCategory J] variable {k : Type v} [Field k] instance {J : Type} [Finite J] (Z : J → ModuleCat.{v} k) [∀ j, FiniteDimensional k (Z j)] : FiniteDimensional k (∏ᶜ fun j => Z j : ModuleCat.{v} k) := haveI : FiniteDimensional k (ModuleCat.of k (∀ j, Z j)) := by unfold ModuleCat.of; infer_instance FiniteDimensional.of_injective (ModuleCat.piIsoPi _).hom ((ModuleCat.mono_iff_injective _).1 (by infer_instance)) /-- Finite limits of finite dimensional vectors spaces are finite dimensional, because we can realise them as subobjects of a finite product. -/ instance (F : J ⥤ FGModuleCat k) : FiniteDimensional k (limit (F ⋙ forget₂ (FGModuleCat k) (ModuleCat.{v} k)) : ModuleCat.{v} k) := haveI : ∀ j, FiniteDimensional k ((F ⋙ forget₂ (FGModuleCat k) (ModuleCat.{v} k)).obj j) := by intro j; change FiniteDimensional k (F.obj j); infer_instance FiniteDimensional.of_injective (limitSubobjectProduct (F ⋙ forget₂ (FGModuleCat k) (ModuleCat.{v} k))) ((ModuleCat.mono_iff_injective _).1 inferInstance) /-- The forgetful functor from `FGModuleCat k` to `ModuleCat k` creates all finite limits. -/ def forget₂CreatesLimit (F : J ⥤ FGModuleCat k) : CreatesLimit F (forget₂ (FGModuleCat k) (ModuleCat.{v} k)) := createsLimitOfFullyFaithfulOfIso ⟨(limit (F ⋙ forget₂ (FGModuleCat k) (ModuleCat.{v} k)) : ModuleCat.{v} k), inferInstance⟩ (Iso.refl _) instance : CreatesLimitsOfShape J (forget₂ (FGModuleCat k) (ModuleCat.{v} k)) where CreatesLimit {F} := forget₂CreatesLimit F instance (J : Type) [Category J] [FinCategory J] : HasLimitsOfShape J (FGModuleCat.{v} k) := hasLimitsOfShape_of_hasLimitsOfShape_createsLimitsOfShape (forget₂ (FGModuleCat k) (ModuleCat.{v} k)) instance : HasFiniteLimits (FGModuleCat k) where out _ _ _ := inferInstance instance : PreservesFiniteLimits (forget₂ (FGModuleCat k) (ModuleCat.{v} k)) where preservesFiniteLimits _ _ _ := inferInstance end FGModuleCat
Algebra\Category\Grp\AB5.lean
/- Copyright (c) 2023 David Kurniadi Angdinata. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: David Kurniadi Angdinata, Moritz Firsching, Nikolas Kuhn, Amelia Livingston -/ import Mathlib.Algebra.Homology.ShortComplex.Ab import Mathlib.Algebra.Homology.ShortComplex.ExactFunctor import Mathlib.CategoryTheory.Abelian.Exact import Mathlib.Algebra.Category.Grp.FilteredColimits import Mathlib.CategoryTheory.Abelian.FunctorCategory /-! # The category of abelian groups satisfies Grothendieck's axiom AB5 -/ universe u open CategoryTheory Limits instance {J C : Type*} [Category J] [Category C] [HasColimitsOfShape J C] [Preadditive C] : (colim (J := J) (C := C)).Additive where variable {J : Type u} [SmallCategory J] [IsFiltered J] noncomputable instance : (colim (J := J) (C := AddCommGrp.{u})).PreservesHomology := Functor.preservesHomologyOfMapExact _ (fun S hS ↦ by replace hS := fun j => hS.map ((evaluation _ _).obj j) simp only [ShortComplex.ab_exact_iff_ker_le_range] at hS ⊢ intro x (hx : _ = _) dsimp at hx rcases Concrete.colimit_exists_rep S.X₂ x with ⟨j, y, rfl⟩ erw [← comp_apply, colimit.ι_map, comp_apply, ← map_zero (by exact colimit.ι S.X₃ j : (S.X₃).obj j →+ ↑(colimit S.X₃))] at hx rcases Concrete.colimit_exists_of_rep_eq.{u, u, u} S.X₃ _ _ hx with ⟨k, e₁, e₂, hk : _ = S.X₃.map e₂ 0⟩ rw [map_zero, ← comp_apply, ← NatTrans.naturality, comp_apply] at hk rcases hS k hk with ⟨t, ht⟩ use colimit.ι S.X₁ k t erw [← comp_apply, colimit.ι_map, comp_apply, ht] exact colimit.w_apply S.X₂ e₁ y) noncomputable instance : PreservesFiniteLimits <| colim (J := J) (C := AddCommGrp.{u}) := by apply Functor.preservesFiniteLimitsOfPreservesHomology
Algebra\Category\Grp\Abelian.lean
/- Copyright (c) 2020 Markus Himmel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Markus Himmel -/ import Mathlib.Algebra.Category.Grp.Colimits import Mathlib.Algebra.Category.Grp.Limits import Mathlib.Algebra.Category.Grp.ZModuleEquivalence import Mathlib.Algebra.Category.ModuleCat.Abelian import Mathlib.CategoryTheory.Adjunction.Limits import Mathlib.CategoryTheory.Limits.ConcreteCategory.Basic /-! # The category of abelian groups is abelian -/ open CategoryTheory Limits universe u noncomputable section namespace AddCommGrp variable {X Y Z : AddCommGrp.{u}} (f : X ⟶ Y) (g : Y ⟶ Z) /-- In the category of abelian groups, every monomorphism is normal. -/ def normalMono (_ : Mono f) : NormalMono f := equivalenceReflectsNormalMono (forget₂ (ModuleCat.{u} ℤ) AddCommGrp.{u}).inv <| ModuleCat.normalMono _ inferInstance /-- In the category of abelian groups, every epimorphism is normal. -/ def normalEpi (_ : Epi f) : NormalEpi f := equivalenceReflectsNormalEpi (forget₂ (ModuleCat.{u} ℤ) AddCommGrp.{u}).inv <| ModuleCat.normalEpi _ inferInstance /-- The category of abelian groups is abelian. -/ instance : Abelian AddCommGrp.{u} where has_finite_products := ⟨HasFiniteProducts.out⟩ normalMonoOfMono := normalMono normalEpiOfEpi := normalEpi end AddCommGrp
Algebra\Category\Grp\Adjunctions.lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Johannes Hölzl -/ import Mathlib.Algebra.Category.Grp.Preadditive import Mathlib.GroupTheory.FreeAbelianGroup import Mathlib.CategoryTheory.Limits.Shapes.Types /-! # Adjunctions regarding the category of (abelian) groups This file contains construction of basic adjunctions concerning the category of groups and the category of abelian groups. ## Main definitions * `AddCommGrp.free`: constructs the functor associating to a type `X` the free abelian group with generators `x : X`. * `Grp.free`: constructs the functor associating to a type `X` the free group with generators `x : X`. * `Grp.abelianize`: constructs the functor which associates to a group `G` its abelianization `Gᵃᵇ`. ## Main statements * `AddCommGrp.adj`: proves that `AddCommGrp.free` is the left adjoint of the forgetful functor from abelian groups to types. * `Grp.adj`: proves that `Grp.free` is the left adjoint of the forgetful functor from groups to types. * `abelianizeAdj`: proves that `Grp.abelianize` is left adjoint to the forgetful functor from abelian groups to groups. -/ noncomputable section universe u open CategoryTheory Limits namespace AddCommGrp /-- The free functor `Type u ⥤ AddCommGroup` sending a type `X` to the free abelian group with generators `x : X`. -/ def free : Type u ⥤ AddCommGrp where obj α := of (FreeAbelianGroup α) map := FreeAbelianGroup.map map_id _ := AddMonoidHom.ext FreeAbelianGroup.map_id_apply map_comp _ _ := AddMonoidHom.ext FreeAbelianGroup.map_comp_apply @[simp] theorem free_obj_coe {α : Type u} : (free.obj α : Type u) = FreeAbelianGroup α := rfl -- This currently can't be a `simp` lemma, -- because `free_obj_coe` will simplify implicit arguments in the LHS. -- (The `simpNF` linter will, correctly, complain.) theorem free_map_coe {α β : Type u} {f : α → β} (x : FreeAbelianGroup α) : (free.map f) x = f <$> x := rfl /-- The free-forgetful adjunction for abelian groups. -/ def adj : free ⊣ forget AddCommGrp.{u} := Adjunction.mkOfHomEquiv { homEquiv := fun X G => FreeAbelianGroup.lift.symm -- Porting note (#11041): used to be just `by intros; ext; rfl`. homEquiv_naturality_left_symm := by intros ext simp only [Equiv.symm_symm] apply FreeAbelianGroup.lift_comp } instance : free.{u}.IsLeftAdjoint := ⟨_, ⟨adj⟩⟩ instance : (forget AddCommGrp.{u}).IsRightAdjoint := ⟨_, ⟨adj⟩⟩ /-- As an example, we now give a high-powered proof that the monomorphisms in `AddCommGroup` are just the injective functions. (This proof works in all universes.) -/ example {G H : AddCommGrp.{u}} (f : G ⟶ H) [Mono f] : Function.Injective f := (mono_iff_injective (f : G → H)).mp (Functor.map_mono (forget AddCommGrp) f) instance : (free.{u}).PreservesMonomorphisms where preserves {X Y} f _ := by by_cases hX : IsEmpty X · constructor intros apply (IsInitial.isInitialObj free _ ((Types.initial_iff_empty X).2 hX).some).isZero.eq_of_tgt · simp only [not_isEmpty_iff] at hX have hf : Function.Injective f := by rwa [← mono_iff_injective] obtain ⟨g, hg⟩ := hf.hasLeftInverse have : IsSplitMono f := IsSplitMono.mk' { retraction := g } infer_instance end AddCommGrp namespace Grp /-- The free functor `Type u ⥤ Group` sending a type `X` to the free group with generators `x : X`. -/ def free : Type u ⥤ Grp where obj α := of (FreeGroup α) map := FreeGroup.map map_id := by -- This used to be `rw`, but we need `erw` after leanprover/lean4#2644 intros; ext1; erw [← FreeGroup.map.unique] <;> intros <;> rfl map_comp := by -- This used to be `rw`, but we need `erw` after leanprover/lean4#2644 intros; ext1; erw [← FreeGroup.map.unique] <;> intros <;> rfl /-- The free-forgetful adjunction for groups. -/ def adj : free ⊣ forget Grp.{u} := Adjunction.mkOfHomEquiv { homEquiv := fun X G => FreeGroup.lift.symm -- Porting note (#11041): used to be just `by intros; ext1; rfl`. homEquiv_naturality_left_symm := by intros ext1 simp only [Equiv.symm_symm] apply Eq.symm apply FreeGroup.lift.unique intros apply FreeGroup.lift.of } instance : (forget Grp.{u}).IsRightAdjoint := ⟨_, ⟨adj⟩⟩ section Abelianization /-- The abelianization functor `Group ⥤ CommGroup` sending a group `G` to its abelianization `Gᵃᵇ`. -/ def abelianize : Grp.{u} ⥤ CommGrp.{u} where obj G := CommGrp.of (Abelianization G) map f := Abelianization.lift (Abelianization.of.comp f) map_id := by intros; simp only [coe_id] apply (Equiv.apply_eq_iff_eq_symm_apply Abelianization.lift).mpr; rfl map_comp := by intros; simp only [coe_comp] apply (Equiv.apply_eq_iff_eq_symm_apply Abelianization.lift).mpr; rfl /-- The abelianization-forgetful adjuction from `Group` to `CommGroup`. -/ def abelianizeAdj : abelianize ⊣ forget₂ CommGrp.{u} Grp.{u} := Adjunction.mkOfHomEquiv { homEquiv := fun G A => Abelianization.lift.symm -- Porting note (#11041): used to be just `by intros; ext1; rfl`. homEquiv_naturality_left_symm := by intros ext1 simp only [Equiv.symm_symm] apply Eq.symm apply Abelianization.lift.unique intros apply Abelianization.lift.of } end Abelianization end Grp /-- The functor taking a monoid to its subgroup of units. -/ @[simps] def MonCat.units : MonCat.{u} ⥤ Grp.{u} where obj R := Grp.of Rˣ map f := Grp.ofHom <| Units.map f map_id _ := MonoidHom.ext fun _ => Units.ext rfl map_comp _ _ := MonoidHom.ext fun _ => Units.ext rfl /-- The forgetful-units adjunction between `Grp` and `MonCat`. -/ def Grp.forget₂MonAdj : forget₂ Grp MonCat ⊣ MonCat.units.{u} where homEquiv X Y := { toFun := fun f => MonoidHom.toHomUnits f invFun := fun f => (Units.coeHom Y).comp f left_inv := fun f => MonoidHom.ext fun _ => rfl right_inv := fun f => MonoidHom.ext fun _ => Units.ext rfl } unit := { app := fun X => { (@toUnits X _).toMonoidHom with } naturality := fun X Y f => MonoidHom.ext fun x => Units.ext rfl } counit := { app := fun X => Units.coeHom X naturality := by intros; exact MonoidHom.ext fun x => rfl } homEquiv_unit := MonoidHom.ext fun _ => Units.ext rfl homEquiv_counit := MonoidHom.ext fun _ => rfl instance : MonCat.units.{u}.IsRightAdjoint := ⟨_, ⟨Grp.forget₂MonAdj⟩⟩ /-- The functor taking a monoid to its subgroup of units. -/ @[simps] def CommMonCat.units : CommMonCat.{u} ⥤ CommGrp.{u} where obj R := CommGrp.of Rˣ map f := CommGrp.ofHom <| Units.map f map_id _ := MonoidHom.ext fun _ => Units.ext rfl map_comp _ _ := MonoidHom.ext fun _ => Units.ext rfl /-- The forgetful-units adjunction between `CommGrp` and `CommMonCat`. -/ def CommGrp.forget₂CommMonAdj : forget₂ CommGrp CommMonCat ⊣ CommMonCat.units.{u} where homEquiv X Y := { toFun := fun f => MonoidHom.toHomUnits f invFun := fun f => (Units.coeHom Y).comp f left_inv := fun f => MonoidHom.ext fun _ => rfl right_inv := fun f => MonoidHom.ext fun _ => Units.ext rfl } unit := { app := fun X => { (@toUnits X _).toMonoidHom with } naturality := fun X Y f => MonoidHom.ext fun x => Units.ext rfl } counit := { app := fun X => Units.coeHom X naturality := by intros; exact MonoidHom.ext fun x => rfl } homEquiv_unit := MonoidHom.ext fun _ => Units.ext rfl homEquiv_counit := MonoidHom.ext fun _ => rfl instance : CommMonCat.units.{u}.IsRightAdjoint := ⟨_, ⟨CommGrp.forget₂CommMonAdj⟩⟩
Algebra\Category\Grp\Basic.lean
/- Copyright (c) 2018 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.Algebra.Category.MonCat.Basic import Mathlib.Algebra.Group.ULift import Mathlib.CategoryTheory.Endomorphism import Mathlib.Data.Int.Cast.Lemmas import Mathlib.GroupTheory.Perm.Basic /-! # Category instances for Group, AddGroup, CommGroup, and AddCommGroup. We introduce the bundled categories: * `Grp` * `AddGrp` * `CommGrp` * `AddCommGrp` along with the relevant forgetful functors between them, and to the bundled monoid categories. -/ universe u v open CategoryTheory /-- The category of groups and group morphisms. -/ @[to_additive] def Grp : Type (u + 1) := Bundled Group /-- The category of additive groups and group morphisms -/ add_decl_doc AddGrp namespace Grp @[to_additive] instance : BundledHom.ParentProjection (fun {α : Type*} (h : Group α) => h.toDivInvMonoid.toMonoid) := ⟨⟩ deriving instance LargeCategory for Grp attribute [to_additive] instGrpLargeCategory @[to_additive] instance concreteCategory : ConcreteCategory Grp := by dsimp only [Grp] infer_instance @[to_additive] instance : CoeSort Grp Type* where coe X := X.α @[to_additive] instance (X : Grp) : Group X := X.str -- porting note (#10670): this instance was not necessary in mathlib @[to_additive] instance {X Y : Grp} : CoeFun (X ⟶ Y) fun _ => X → Y where coe (f : X →* Y) := f @[to_additive] instance instFunLike (X Y : Grp) : FunLike (X ⟶ Y) X Y := show FunLike (X →* Y) X Y from inferInstance @[to_additive (attr := simp)] lemma coe_id {X : Grp} : (𝟙 X : X → X) = id := rfl @[to_additive (attr := simp)] lemma coe_comp {X Y Z : Grp} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g : X → Z) = g ∘ f := rfl @[to_additive] lemma comp_def {X Y Z : Grp} {f : X ⟶ Y} {g : Y ⟶ Z} : f ≫ g = g.comp f := rfl @[simp] lemma forget_map {X Y : Grp} (f : X ⟶ Y) : (forget Grp).map f = (f : X → Y) := rfl @[to_additive (attr := ext)] lemma ext {X Y : Grp} {f g : X ⟶ Y} (w : ∀ x : X, f x = g x) : f = g := MonoidHom.ext w /-- Construct a bundled `Group` from the underlying type and typeclass. -/ @[to_additive] def of (X : Type u) [Group X] : Grp := Bundled.of X /-- Construct a bundled `AddGroup` from the underlying type and typeclass. -/ add_decl_doc AddGrp.of @[to_additive (attr := simp)] theorem coe_of (R : Type u) [Group R] : ↑(Grp.of R) = R := rfl @[to_additive (attr := simp)] theorem coe_comp' {G H K : Type _} [Group G] [Group H] [Group K] (f : G →* H) (g : H →* K) : @DFunLike.coe (G →* K) G (fun _ ↦ K) MonoidHom.instFunLike (CategoryStruct.comp (X := Grp.of G) (Y := Grp.of H) (Z := Grp.of K) f g) = g ∘ f := rfl @[to_additive (attr := simp)] theorem coe_id' {G : Type _} [Group G] : @DFunLike.coe (G →* G) G (fun _ ↦ G) MonoidHom.instFunLike (CategoryStruct.id (X := Grp.of G)) = id := rfl @[to_additive] instance : Inhabited Grp := ⟨Grp.of PUnit⟩ @[to_additive hasForgetToAddMonCat] instance hasForgetToMonCat : HasForget₂ Grp MonCat := BundledHom.forget₂ _ _ @[to_additive] instance : Coe Grp.{u} MonCat.{u} where coe := (forget₂ Grp MonCat).obj -- porting note (#10670): this instance was not necessary in mathlib @[to_additive] instance (G H : Grp) : One (G ⟶ H) := (inferInstance : One (MonoidHom G H)) @[to_additive (attr := simp)] theorem one_apply (G H : Grp) (g : G) : ((1 : G ⟶ H) : G → H) g = 1 := rfl /-- Typecheck a `MonoidHom` as a morphism in `Grp`. -/ @[to_additive] def ofHom {X Y : Type u} [Group X] [Group Y] (f : X →* Y) : of X ⟶ of Y := f /-- Typecheck an `AddMonoidHom` as a morphism in `AddGroup`. -/ add_decl_doc AddGrp.ofHom @[to_additive] theorem ofHom_apply {X Y : Type _} [Group X] [Group Y] (f : X →* Y) (x : X) : (ofHom f) x = f x := rfl @[to_additive] instance ofUnique (G : Type*) [Group G] [i : Unique G] : Unique (Grp.of G) := i -- We verify that simp lemmas apply when coercing morphisms to functions. @[to_additive] example {R S : Grp} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 := by simp [h] /-- Universe lift functor for groups. -/ @[to_additive (attr := simps) "Universe lift functor for additive groups."] def uliftFunctor : Grp.{u} ⥤ Grp.{max u v} where obj X := Grp.of (ULift.{v, u} X) map {X Y} f := Grp.ofHom <| MulEquiv.ulift.symm.toMonoidHom.comp <| f.comp MulEquiv.ulift.toMonoidHom map_id X := by rfl map_comp {X Y Z} f g := by rfl end Grp /-- The category of commutative groups and group morphisms. -/ @[to_additive] def CommGrp : Type (u + 1) := Bundled CommGroup /-- The category of additive commutative groups and group morphisms. -/ add_decl_doc AddCommGrp /-- `Ab` is an abbreviation for `AddCommGroup`, for the sake of mathematicians' sanity. -/ abbrev Ab := AddCommGrp namespace CommGrp @[to_additive] instance : BundledHom.ParentProjection @CommGroup.toGroup := ⟨⟩ deriving instance LargeCategory for CommGrp attribute [to_additive] instCommGrpLargeCategory @[to_additive] instance concreteCategory : ConcreteCategory CommGrp := by dsimp only [CommGrp] infer_instance @[to_additive] instance : CoeSort CommGrp Type* where coe X := X.α @[to_additive] instance commGroupInstance (X : CommGrp) : CommGroup X := X.str -- porting note (#10670): this instance was not necessary in mathlib @[to_additive] instance {X Y : CommGrp} : CoeFun (X ⟶ Y) fun _ => X → Y where coe (f : X →* Y) := f @[to_additive] instance instFunLike (X Y : CommGrp) : FunLike (X ⟶ Y) X Y := show FunLike (X →* Y) X Y from inferInstance @[to_additive (attr := simp)] lemma coe_id {X : CommGrp} : (𝟙 X : X → X) = id := rfl @[to_additive (attr := simp)] lemma coe_comp {X Y Z : CommGrp} {f : X ⟶ Y} {g : Y ⟶ Z} : (f ≫ g : X → Z) = g ∘ f := rfl @[to_additive] lemma comp_def {X Y Z : CommGrp} {f : X ⟶ Y} {g : Y ⟶ Z} : f ≫ g = g.comp f := rfl @[to_additive (attr := simp)] lemma forget_map {X Y : CommGrp} (f : X ⟶ Y) : (forget CommGrp).map f = (f : X → Y) := rfl @[to_additive (attr := ext)] lemma ext {X Y : CommGrp} {f g : X ⟶ Y} (w : ∀ x : X, f x = g x) : f = g := MonoidHom.ext w /-- Construct a bundled `CommGroup` from the underlying type and typeclass. -/ @[to_additive] def of (G : Type u) [CommGroup G] : CommGrp := Bundled.of G /-- Construct a bundled `AddCommGroup` from the underlying type and typeclass. -/ add_decl_doc AddCommGrp.of @[to_additive] instance : Inhabited CommGrp := ⟨CommGrp.of PUnit⟩ @[to_additive (attr := simp)] theorem coe_of (R : Type u) [CommGroup R] : (CommGrp.of R : Type u) = R := rfl @[to_additive (attr := simp)] theorem coe_comp' {G H K : Type _} [CommGroup G] [CommGroup H] [CommGroup K] (f : G →* H) (g : H →* K) : @DFunLike.coe (G →* K) G (fun _ ↦ K) MonoidHom.instFunLike (CategoryStruct.comp (X := CommGrp.of G) (Y := CommGrp.of H) (Z := CommGrp.of K) f g) = g ∘ f := rfl @[to_additive (attr := simp)] theorem coe_id' {G : Type _} [CommGroup G] : @DFunLike.coe (G →* G) G (fun _ ↦ G) MonoidHom.instFunLike (CategoryStruct.id (X := CommGrp.of G)) = id := rfl @[to_additive] instance ofUnique (G : Type*) [CommGroup G] [i : Unique G] : Unique (CommGrp.of G) := i @[to_additive] instance hasForgetToGroup : HasForget₂ CommGrp Grp := BundledHom.forget₂ _ _ @[to_additive] instance : Coe CommGrp.{u} Grp.{u} where coe := (forget₂ CommGrp Grp).obj @[to_additive hasForgetToAddCommMonCat] instance hasForgetToCommMonCat : HasForget₂ CommGrp CommMonCat := InducedCategory.hasForget₂ fun G : CommGrp => CommMonCat.of G @[to_additive] instance : Coe CommGrp.{u} CommMonCat.{u} where coe := (forget₂ CommGrp CommMonCat).obj -- porting note (#10670): this instance was not necessary in mathlib @[to_additive] instance (G H : CommGrp) : One (G ⟶ H) := (inferInstance : One (MonoidHom G H)) @[to_additive (attr := simp)] theorem one_apply (G H : CommGrp) (g : G) : ((1 : G ⟶ H) : G → H) g = 1 := rfl /-- Typecheck a `MonoidHom` as a morphism in `CommGroup`. -/ @[to_additive] def ofHom {X Y : Type u} [CommGroup X] [CommGroup Y] (f : X →* Y) : of X ⟶ of Y := f /-- Typecheck an `AddMonoidHom` as a morphism in `AddCommGroup`. -/ add_decl_doc AddCommGrp.ofHom @[to_additive (attr := simp)] theorem ofHom_apply {X Y : Type _} [CommGroup X] [CommGroup Y] (f : X →* Y) (x : X) : @DFunLike.coe (X →* Y) X (fun _ ↦ Y) _ (ofHom f) x = f x := rfl -- We verify that simp lemmas apply when coercing morphisms to functions. @[to_additive] example {R S : CommGrp} (i : R ⟶ S) (r : R) (h : r = 1) : i r = 1 := by simp [h] /-- Universe lift functor for commutative groups. -/ @[to_additive (attr := simps) "Universe lift functor for additive commutative groups."] def uliftFunctor : CommGrp.{u} ⥤ CommGrp.{max u v} where obj X := CommGrp.of (ULift.{v, u} X) map {X Y} f := CommGrp.ofHom <| MulEquiv.ulift.symm.toMonoidHom.comp <| f.comp MulEquiv.ulift.toMonoidHom map_id X := by rfl map_comp {X Y Z} f g := by rfl end CommGrp namespace AddCommGrp -- Note that because `ℤ : Type 0`, this forces `G : AddCommGroup.{0}`, -- so we write this explicitly to be clear. -- TODO generalize this, requiring a `ULiftInstances.lean` file /-- Any element of an abelian group gives a unique morphism from `ℤ` sending `1` to that element. -/ def asHom {G : AddCommGrp.{0}} (g : G) : AddCommGrp.of ℤ ⟶ G := zmultiplesHom G g @[simp] theorem asHom_apply {G : AddCommGrp.{0}} (g : G) (i : ℤ) : @DFunLike.coe (ℤ →+ ↑G) ℤ (fun _ ↦ ↑G) _ (asHom g) i = i • g := rfl theorem asHom_injective {G : AddCommGrp.{0}} : Function.Injective (@asHom G) := fun h k w => by convert congr_arg (fun k : AddCommGrp.of ℤ ⟶ G => (k : ℤ → G) (1 : ℤ)) w <;> simp @[ext] theorem int_hom_ext {G : AddCommGrp.{0}} (f g : AddCommGrp.of ℤ ⟶ G) (w : f (1 : ℤ) = g (1 : ℤ)) : f = g := @AddMonoidHom.ext_int G _ f g w -- TODO: this argument should be generalised to the situation where -- the forgetful functor is representable. theorem injective_of_mono {G H : AddCommGrp.{0}} (f : G ⟶ H) [Mono f] : Function.Injective f := fun g₁ g₂ h => by have t0 : asHom g₁ ≫ f = asHom g₂ ≫ f := by aesop_cat have t1 : asHom g₁ = asHom g₂ := (cancel_mono _).1 t0 apply asHom_injective t1 end AddCommGrp /-- Build an isomorphism in the category `Grp` from a `MulEquiv` between `Group`s. -/ @[to_additive (attr := simps)] def MulEquiv.toGrpIso {X Y : Grp} (e : X ≃* Y) : X ≅ Y where hom := e.toMonoidHom inv := e.symm.toMonoidHom /-- Build an isomorphism in the category `AddGroup` from an `AddEquiv` between `AddGroup`s. -/ add_decl_doc AddEquiv.toAddGrpIso /-- Build an isomorphism in the category `CommGrp` from a `MulEquiv` between `CommGroup`s. -/ @[to_additive (attr := simps)] def MulEquiv.toCommGrpIso {X Y : CommGrp} (e : X ≃* Y) : X ≅ Y where hom := e.toMonoidHom inv := e.symm.toMonoidHom /-- Build an isomorphism in the category `AddCommGrp` from an `AddEquiv` between `AddCommGroup`s. -/ add_decl_doc AddEquiv.toAddCommGrpIso namespace CategoryTheory.Iso /-- Build a `MulEquiv` from an isomorphism in the category `Grp`. -/ @[to_additive (attr := simp)] def groupIsoToMulEquiv {X Y : Grp} (i : X ≅ Y) : X ≃* Y := MonoidHom.toMulEquiv i.hom i.inv i.hom_inv_id i.inv_hom_id /-- Build an `addEquiv` from an isomorphism in the category `AddGroup` -/ add_decl_doc addGroupIsoToAddEquiv /-- Build a `MulEquiv` from an isomorphism in the category `CommGroup`. -/ @[to_additive (attr := simps!)] def commGroupIsoToMulEquiv {X Y : CommGrp} (i : X ≅ Y) : X ≃* Y := MonoidHom.toMulEquiv i.hom i.inv i.hom_inv_id i.inv_hom_id /-- Build an `AddEquiv` from an isomorphism in the category `AddCommGroup`. -/ add_decl_doc addCommGroupIsoToAddEquiv end CategoryTheory.Iso /-- multiplicative equivalences between `Group`s are the same as (isomorphic to) isomorphisms in `Grp` -/ @[to_additive] def mulEquivIsoGroupIso {X Y : Grp.{u}} : X ≃* Y ≅ X ≅ Y where hom e := e.toGrpIso inv i := i.groupIsoToMulEquiv /-- Additive equivalences between `AddGroup`s are the same as (isomorphic to) isomorphisms in `AddGrp`. -/ add_decl_doc addEquivIsoAddGroupIso /-- Multiplicative equivalences between `CommGroup`s are the same as (isomorphic to) isomorphisms in `CommGrp`. -/ @[to_additive] def mulEquivIsoCommGroupIso {X Y : CommGrp.{u}} : X ≃* Y ≅ X ≅ Y where hom e := e.toCommGrpIso inv i := i.commGroupIsoToMulEquiv /-- Additive equivalences between `AddCommGroup`s are the same as (isomorphic to) isomorphisms in `AddCommGrp`. -/ add_decl_doc addEquivIsoAddCommGroupIso namespace CategoryTheory.Aut /-- The (bundled) group of automorphisms of a type is isomorphic to the (bundled) group of permutations. -/ def isoPerm {α : Type u} : Grp.of (Aut α) ≅ Grp.of (Equiv.Perm α) where hom := { toFun := fun g => g.toEquiv map_one' := by aesop map_mul' := by aesop } inv := { toFun := fun g => g.toIso map_one' := by aesop map_mul' := by aesop } /-- The (unbundled) group of automorphisms of a type is `MulEquiv` to the (unbundled) group of permutations. -/ def mulEquivPerm {α : Type u} : Aut α ≃* Equiv.Perm α := isoPerm.groupIsoToMulEquiv end CategoryTheory.Aut @[to_additive] instance Grp.forget_reflects_isos : (forget Grp.{u}).ReflectsIsomorphisms where reflects {X Y} f _ := by let i := asIso ((forget Grp).map f) let e : X ≃* Y := { i.toEquiv with map_mul' := map_mul _ } exact e.toGrpIso.isIso_hom @[to_additive] instance CommGrp.forget_reflects_isos : (forget CommGrp.{u}).ReflectsIsomorphisms where reflects {X Y} f _ := by let i := asIso ((forget CommGrp).map f) let e : X ≃* Y := { i.toEquiv with map_mul' := map_mul _} exact e.toCommGrpIso.isIso_hom -- note: in the following definitions, there is a problem with `@[to_additive]` -- as the `Category` instance is not found on the additive variant -- this variant is then renamed with a `Aux` suffix /-- An alias for `Grp.{max u v}`, to deal around unification issues. -/ @[to_additive (attr := nolint checkUnivs) GrpMaxAux "An alias for `AddGrp.{max u v}`, to deal around unification issues."] abbrev GrpMax.{u1, u2} := Grp.{max u1 u2} /-- An alias for `AddGrp.{max u v}`, to deal around unification issues. -/ @[nolint checkUnivs] abbrev AddGrpMax.{u1, u2} := AddGrp.{max u1 u2} /-- An alias for `CommGrp.{max u v}`, to deal around unification issues. -/ @[to_additive (attr := nolint checkUnivs) AddCommGrpMaxAux "An alias for `AddCommGrp.{max u v}`, to deal around unification issues."] abbrev CommGrpMax.{u1, u2} := CommGrp.{max u1 u2} /-- An alias for `AddCommGrp.{max u v}`, to deal around unification issues. -/ @[nolint checkUnivs] abbrev AddCommGrpMax.{u1, u2} := AddCommGrp.{max u1 u2} /-! `@[simp]` lemmas for `MonoidHom.comp` and categorical identities. -/ @[to_additive (attr := simp)] theorem MonoidHom.comp_id_grp {G : Grp.{u}} {H : Type u} [Group H] (f : G →* H) : f.comp (𝟙 G) = f := Category.id_comp (Grp.ofHom f) @[to_additive (attr := simp)] theorem MonoidHom.id_grp_comp {G : Type u} [Group G] {H : Grp.{u}} (f : G →* H) : MonoidHom.comp (𝟙 H) f = f := Category.comp_id (Grp.ofHom f) @[to_additive (attr := simp)] theorem MonoidHom.comp_id_commGrp {G : CommGrp.{u}} {H : Type u} [CommGroup H] (f : G →* H) : f.comp (𝟙 G) = f := Category.id_comp (CommGrp.ofHom f) @[to_additive (attr := simp)] theorem MonoidHom.id_commGrp_comp {G : Type u} [CommGroup G] {H : CommGrp.{u}} (f : G →* H) : MonoidHom.comp (𝟙 H) f = f := Category.comp_id (CommGrp.ofHom f)