import Mathlib namespace OAI namespace PiExponent.NatToIntGrading noncomputable section open DirectSum variable {M ΟƒM : Type*} [AddCommGroup M] [SetLike ΟƒM M] [AddSubgroupClass ΟƒM M] (π“œ : β„• β†’ ΟƒM) [DirectSum.Decomposition π“œ] def piece : β„€ β†’ AddSubgroup M | .ofNat n => AddSubgroup.ofClass (π“œ n) | .negSucc _ => βŠ₯ def component (m : M) : βˆ€ d, piece π“œ d | .ofNat n => ⟨DirectSum.decompose π“œ m n, (DirectSum.decompose π“œ m n).property⟩ | .negSucc _ => 0 def support (m : M) : Finset β„€ := by classical exact (DirectSum.decompose π“œ m).support.image (fun n : β„• => (n : β„€)) theorem component_eq_zero_of_not_mem (m : M) (d : β„€) (hd : d βˆ‰ support π“œ m) : component π“œ m d = 0 := by classical cases d with | ofNat n => apply Subtype.ext have hn : n βˆ‰ (DirectSum.decompose π“œ m).support := by intro hn exact hd (Finset.mem_image.mpr ⟨n, hn, rfl⟩) change (DirectSum.decompose π“œ m n : M) = 0 rw [DFinsupp.notMem_support_iff.mp hn] rfl | negSucc n => rfl def decompositionMap : M β†’+ ⨁ d, piece π“œ d where toFun m := DFinsupp.mk' (component π“œ m) (Trunc.mk ⟨(support π“œ m).val, fun d => by classical by_cases hd : d ∈ support π“œ m Β· exact Or.inl hd Β· exact Or.inr (component_eq_zero_of_not_mem π“œ m d hd)⟩) map_zero' := by apply DFinsupp.ext intro d apply Subtype.ext cases d with | ofNat n => change (DirectSum.decompose π“œ (0 : M) n : M) = 0 rw [DirectSum.decompose_zero] rfl | negSucc n => rfl map_add' m n := by apply DFinsupp.ext intro d apply Subtype.ext cases d with | ofNat k => change (DirectSum.decompose π“œ (m + n) k : M) = (DirectSum.decompose π“œ m k : M) + (DirectSum.decompose π“œ n k : M) rw [DirectSum.decompose_add] rfl | negSucc k => exact (zero_add (0 : M)).symm @[simp] theorem decompositionMap_nat (m : M) (n : β„•) : (decompositionMap π“œ m (n : β„€) : M) = (DirectSum.decompose π“œ m n : M) := rfl theorem decompositionMap_injective : Function.Injective (decompositionMap π“œ) := by intro m n h apply (DirectSum.decompose π“œ).injective ext k exact congrArg (fun z : ⨁ d, piece π“œ d => (z (k : β„€) : M)) h theorem decompositionMap_homogeneous (d : β„€) (m : piece π“œ d) : decompositionMap π“œ m.val = DirectSum.of (fun d => piece π“œ d) d m := by classical cases d with | negSucc n => have hm : m = 0 := Subtype.ext m.property subst m simp | ofNat n => apply DFinsupp.ext intro e apply Subtype.ext cases e with | negSucc k => rw [DirectSum.of_eq_of_ne _ _ _ (show Int.negSucc k β‰  Int.ofNat n from by intro h; cases h)] rfl | ofNat k => by_cases hnk : n = k Β· subst k rw [DirectSum.of_eq_same] exact DirectSum.decompose_of_mem_same π“œ m.property Β· rw [DirectSum.of_eq_of_ne _ _ _ (show Int.ofNat k β‰  Int.ofNat n from fun h => hnk (Int.ofNat.inj h).symm)] exact DirectSum.decompose_of_mem_ne π“œ m.property hnk instance decomposition : DirectSum.Decomposition (piece π“œ) := by have hr : (decompositionMap π“œ).comp (DirectSum.coeAddMonoidHom (piece π“œ)) = AddMonoidHom.id _ := by apply DirectSum.addHom_ext intro d m simp only [AddMonoidHom.comp_apply, DirectSum.coeAddMonoidHom_of, AddMonoidHom.id_apply, decompositionMap_homogeneous] apply DirectSum.Decomposition.ofAddHom (piece π“œ) (decompositionMap π“œ) ?_ hr apply AddMonoidHom.ext intro m apply decompositionMap_injective π“œ exact DFunLike.congr_fun hr (decompositionMap π“œ m) @[simp] theorem decompose_nat (m : M) (n : β„•) : (DirectSum.decompose (piece π“œ) m (n : β„€) : M) = (DirectSum.decompose π“œ m n : M) := rfl @[simp] theorem decompose_negSucc (m : M) (n : β„•) : (DirectSum.decompose (piece π“œ) m (.negSucc n) : M) = 0 := rfl section Scalars variable {R ΟƒR : Type*} [Ring R] [Module R M] [SetLike ΟƒR R] [AddSubgroupClass ΟƒR R] (π’œ : β„• β†’ ΟƒR) [SetLike.GradedSMul π’œ π“œ] instance gradedSMul : SetLike.GradedSMul (piece π’œ) (piece π“œ) where smul_mem := by intro i j a m ha hm cases i with | negSucc n => have ha' : a = 0 := ha rw [ha', zero_smul] exact zero_mem _ | ofNat n => cases j with | negSucc k => have hm' : m = 0 := hm rw [hm', smul_zero] exact zero_mem _ | ofNat k => change a β€’ m ∈ π“œ (n + k) exact SetLike.GradedSMul.smul_mem (show a ∈ π’œ n from ha) (show m ∈ π“œ k from hm) end Scalars section Ring variable {R ΟƒR : Type*} [Ring R] [SetLike ΟƒR R] [AddSubgroupClass ΟƒR R] (π’œ : β„• β†’ ΟƒR) [SetLike.GradedMonoid π’œ] instance gradedMonoid : SetLike.GradedMonoid (piece π’œ) where one_mem := by change (1 : R) ∈ π’œ 0 exact SetLike.GradedOne.one_mem mul_mem := by intro i j a b ha hb cases i with | negSucc n => have ha' : a = 0 := ha rw [ha', zero_mul] exact zero_mem _ | ofNat n => cases j with | negSucc k => have hb' : b = 0 := hb rw [hb', mul_zero] exact zero_mem _ | ofNat k => change a * b ∈ π’œ (n + k) exact SetLike.mul_mem_graded (show a ∈ π’œ n from ha) (show b ∈ π’œ k from hb) end Ring end end PiExponent.NatToIntGrading end OAI