{-# OPTIONS --safe #-}
module Cubical.Algebra.Module.Submodule where

open import Cubical.Foundations.Prelude
open import Cubical.Foundations.Powerset
open import Cubical.Foundations.Structure

open import Cubical.Data.Unit

open import Cubical.Algebra.Module
open import Cubical.Algebra.Ring
open import Cubical.Algebra.AbGroup

private
  variable
     ℓ' : Level

module _ (R : Ring ) (M : LeftModule R ℓ') where
  open ModuleTheory R M
  open LeftModuleStr (snd M)
  private
    module R = RingStr (snd R)

  record isSubmodule (N :   M ) : Type (ℓ-max  ℓ') where
    field
      +-closed : {x y :  M }  x  N  y  N  x + y  N
      0r-closed : 0m  N
      ⋆-closed :  {x :  M } (r :  R )  x  N  r  x  N

    -closed : {x :  M }  x  N  - x  N
    -closed {x = x} x∈N =
      subst (_∈ N)
            (((R.- R.1r)  x) ≡⟨ minusByMult x 
              (- x) )
            (⋆-closed (R.- R.1r) x∈N)

  Submodule : Type _
  Submodule = Σ[ N    M  ] isSubmodule N

  open isSubmodule

  zeroSubmodule : Submodule
  fst zeroSubmodule x = (x  0m) , is-set x 0m
  +-closed  (snd zeroSubmodule) x≡0 y≡0 =  i  x≡0 i + y≡0 i)  +IdL 0m
  0r-closed (snd zeroSubmodule)         = refl
  ⋆-closed  (snd zeroSubmodule) r x≡0   =  i  r  x≡0 i)  ⋆AnnihilR r

  allSubmodule : Submodule
  fst allSubmodule x = Unit* , isOfHLevelUnit* 1
  +-closed  (snd allSubmodule) _ _ = tt*
  0r-closed (snd allSubmodule)     = tt*
  ⋆-closed  (snd allSubmodule) _ _ = tt*