{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TypeFamilies #-}

module Data.Monoid.GCD where

import Data.Coerce (coerce)
import qualified Data.Foldable as F
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed as U

{- |
>>> mempty :: GCD Int
GCD {getGCD = 0}
>>> GCD (-4) <> GCD 6
GCD {getGCD = 2}
>>> GCD (-1) <> mempty
GCD {getGCD = 1}
-}
newtype GCD a = GCD {forall a. GCD a -> a
getGCD :: a}
  deriving (GCD a -> GCD a -> Bool
(GCD a -> GCD a -> Bool) -> (GCD a -> GCD a -> Bool) -> Eq (GCD a)
forall a. Eq a => GCD a -> GCD a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => GCD a -> GCD a -> Bool
== :: GCD a -> GCD a -> Bool
$c/= :: forall a. Eq a => GCD a -> GCD a -> Bool
/= :: GCD a -> GCD a -> Bool
Eq, Int -> GCD a -> ShowS
[GCD a] -> ShowS
GCD a -> String
(Int -> GCD a -> ShowS)
-> (GCD a -> String) -> ([GCD a] -> ShowS) -> Show (GCD a)
forall a. Show a => Int -> GCD a -> ShowS
forall a. Show a => [GCD a] -> ShowS
forall a. Show a => GCD a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> GCD a -> ShowS
showsPrec :: Int -> GCD a -> ShowS
$cshow :: forall a. Show a => GCD a -> String
show :: GCD a -> String
$cshowList :: forall a. Show a => [GCD a] -> ShowS
showList :: [GCD a] -> ShowS
Show)

instance (Integral a) => Semigroup (GCD a) where
  <> :: GCD a -> GCD a -> GCD a
(<>) = (a -> a -> a) -> GCD a -> GCD a -> GCD a
forall a b. Coercible a b => a -> b
coerce (forall a. Integral a => a -> a -> a
gcd @a)
  {-# INLINE (<>) #-}

instance (Num a, Integral a) => Monoid (GCD a) where
  mempty :: GCD a
mempty = a -> GCD a
forall a. a -> GCD a
GCD a
0
  {-# INLINE mempty #-}
  mconcat :: [GCD a] -> GCD a
mconcat = (GCD a -> GCD a -> GCD a) -> GCD a -> [GCD a] -> GCD a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' GCD a -> GCD a -> GCD a
forall a. Monoid a => a -> a -> a
mappend GCD a
forall a. Monoid a => a
mempty
  {-# INLINE mconcat #-}

newtype instance U.MVector s (GCD a) = MV_GCD (U.MVector s a)
newtype instance U.Vector (GCD a) = V_GCD (U.Vector a)
deriving newtype instance (U.Unbox a) => GM.MVector U.MVector (GCD a)
deriving newtype instance (U.Unbox a) => G.Vector U.Vector (GCD a)
instance (U.Unbox a) => U.Unbox (GCD a)