Skip to content

Commit

Permalink
Use pat syn, make identity bidirectional
Browse files Browse the repository at this point in the history
  • Loading branch information
tbidne committed Dec 27, 2024
1 parent abc117a commit 074ec81
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Numeric/Algebra/Additive/AMonoid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class (ASemigroup m) => AMonoid m where
-- @since 0.1
pattern Zero :: (AMonoid m, Eq m) => m
pattern Zero <- ((== zero) -> True)
where
Zero = zero

-- | Pattern synonym for @x /= 'zero'@.
--
Expand Down
26 changes: 17 additions & 9 deletions src/Numeric/Algebra/Multiplicative/MEuclidean.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{-# LANGUAGE CPP #-}

-- see NOTE: [Pattern Synonym COMPLETE]
#if !MIN_VERSION_base(4, 16, 0)
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
#endif

-- | Provides typeclass for euclidean division.
--
-- @since 0.1
Expand All @@ -14,7 +21,11 @@ import Data.Int (Int16, Int32, Int64, Int8)
import Data.Kind (Constraint, Type)
import Data.Word (Word16, Word32, Word64, Word8)
import GHC.Natural (Natural)
import Numeric.Algebra.Additive.AMonoid (AMonoid (zero))
import Numeric.Algebra.Additive.AMonoid
( AMonoid,
pattern NonZero,
pattern Zero,
)
import Numeric.Algebra.Multiplicative.MGroup (MGroup)
import Numeric.Algebra.Multiplicative.MSemigroup ((.*.))
import Numeric.Algebra.Normed (Normed (norm))
Expand All @@ -41,18 +52,15 @@ mmod x d = snd $ mdivMod x d
mgcd :: (AMonoid g, Eq g, MEuclidean g, Normed g) => g -> g -> g
mgcd x y = gcd' (norm x) (norm y)
where
gcd' a b =
if b == zero
then a
else gcd' b (a `mmod` b)
gcd' a Zero = a
gcd' a (NonZero b) = gcd' b (a `mmod` b)
{-# INLINE mgcd #-}

-- | @since 0.1
mlcm :: (AMonoid g, Eq g, MEuclidean g, Normed g) => g -> g -> g
mlcm x y
| x == zero = zero
| y == zero = zero
| otherwise = norm (x `mdiv` mgcd x y .*. y)
mlcm Zero _ = Zero
mlcm _ Zero = Zero
mlcm x y = norm (x `mdiv` mgcd x y .*. y)
{-# INLINE mlcm #-}

-- | @since 0.1
Expand Down
2 changes: 2 additions & 0 deletions src/Numeric/Algebra/Multiplicative/MMonoid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class (MSemigroup m) => MMonoid m where
-- @since 0.1
pattern One :: (MMonoid m, Eq m) => m
pattern One <- ((== one) -> True)
where
One = one

-- | Pattern synonym for @x /= 'one'@.
--
Expand Down

0 comments on commit 074ec81

Please sign in to comment.