{-# OPTIONS_GHC -Wall #-}
module NumHask.Algebra.Additive
( Additive (..),
sum,
Subtractive (..),
)
where
import Data.Int (Int16, Int32, Int64, Int8)
import Data.Word (Word, Word16, Word32, Word64, Word8)
import GHC.Natural (Natural (..))
import Prelude (Bool, Double, Float, Int, Integer)
import qualified Prelude as P
class Additive a where
infixl 6 +
(+) :: a -> a -> a
zero :: a
sum :: (Additive a, P.Foldable f) => f a -> a
sum :: f a -> a
sum = (a -> a -> a) -> a -> f a -> a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
P.foldr a -> a -> a
forall a. Additive a => a -> a -> a
(+) a
forall a. Additive a => a
zero
class (Additive a) => Subtractive a where
negate :: a -> a
infixl 6 -
(-) :: a -> a -> a
(-) a
a a
b = a
a a -> a -> a
forall a. Additive a => a -> a -> a
+ a -> a
forall a. Subtractive a => a -> a
negate a
b
instance Additive Double where
+ :: Double -> Double -> Double
(+) = Double -> Double -> Double
forall a. Num a => a -> a -> a
(P.+)
zero :: Double
zero = Double
0
instance Subtractive Double where
negate :: Double -> Double
negate = Double -> Double
forall a. Num a => a -> a
P.negate
instance Additive Float where
+ :: Float -> Float -> Float
(+) = Float -> Float -> Float
forall a. Num a => a -> a -> a
(P.+)
zero :: Float
zero = Float
0
instance Subtractive Float where
negate :: Float -> Float
negate = Float -> Float
forall a. Num a => a -> a
P.negate
instance Additive Int where
+ :: Int -> Int -> Int
(+) = Int -> Int -> Int
forall a. Num a => a -> a -> a
(P.+)
zero :: Int
zero = Int
0
instance Subtractive Int where
negate :: Int -> Int
negate = Int -> Int
forall a. Num a => a -> a
P.negate
instance Additive Integer where
+ :: Integer -> Integer -> Integer
(+) = Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(P.+)
zero :: Integer
zero = Integer
0
instance Subtractive Integer where
negate :: Integer -> Integer
negate = Integer -> Integer
forall a. Num a => a -> a
P.negate
instance Additive Bool where
+ :: Bool -> Bool -> Bool
(+) = Bool -> Bool -> Bool
(P.||)
zero :: Bool
zero = Bool
P.False
instance Subtractive Bool where
negate :: Bool -> Bool
negate = Bool -> Bool
P.not
instance Additive Natural where
+ :: Natural -> Natural -> Natural
(+) = Natural -> Natural -> Natural
forall a. Num a => a -> a -> a
(P.+)
zero :: Natural
zero = Natural
0
instance Subtractive Natural where
negate :: Natural -> Natural
negate = Natural -> Natural
forall a. Num a => a -> a
P.negate
instance Additive Int8 where
+ :: Int8 -> Int8 -> Int8
(+) = Int8 -> Int8 -> Int8
forall a. Num a => a -> a -> a
(P.+)
zero :: Int8
zero = Int8
0
instance Subtractive Int8 where
negate :: Int8 -> Int8
negate = Int8 -> Int8
forall a. Num a => a -> a
P.negate
instance Additive Int16 where
+ :: Int16 -> Int16 -> Int16
(+) = Int16 -> Int16 -> Int16
forall a. Num a => a -> a -> a
(P.+)
zero :: Int16
zero = Int16
0
instance Subtractive Int16 where
negate :: Int16 -> Int16
negate = Int16 -> Int16
forall a. Num a => a -> a
P.negate
instance Additive Int32 where
+ :: Int32 -> Int32 -> Int32
(+) = Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
(P.+)
zero :: Int32
zero = Int32
0
instance Subtractive Int32 where
negate :: Int32 -> Int32
negate = Int32 -> Int32
forall a. Num a => a -> a
P.negate
instance Additive Int64 where
+ :: Int64 -> Int64 -> Int64
(+) = Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
(P.+)
zero :: Int64
zero = Int64
0
instance Subtractive Int64 where
negate :: Int64 -> Int64
negate = Int64 -> Int64
forall a. Num a => a -> a
P.negate
instance Additive Word where
+ :: Word -> Word -> Word
(+) = Word -> Word -> Word
forall a. Num a => a -> a -> a
(P.+)
zero :: Word
zero = Word
0
instance Subtractive Word where
negate :: Word -> Word
negate = Word -> Word
forall a. Num a => a -> a
P.negate
instance Additive Word8 where
+ :: Word8 -> Word8 -> Word8
(+) = Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
(P.+)
zero :: Word8
zero = Word8
0
instance Subtractive Word8 where
negate :: Word8 -> Word8
negate = Word8 -> Word8
forall a. Num a => a -> a
P.negate
instance Additive Word16 where
+ :: Word16 -> Word16 -> Word16
(+) = Word16 -> Word16 -> Word16
forall a. Num a => a -> a -> a
(P.+)
zero :: Word16
zero = Word16
0
instance Subtractive Word16 where
negate :: Word16 -> Word16
negate = Word16 -> Word16
forall a. Num a => a -> a
P.negate
instance Additive Word32 where
+ :: Word32 -> Word32 -> Word32
(+) = Word32 -> Word32 -> Word32
forall a. Num a => a -> a -> a
(P.+)
zero :: Word32
zero = Word32
0
instance Subtractive Word32 where
negate :: Word32 -> Word32
negate = Word32 -> Word32
forall a. Num a => a -> a
P.negate
instance Additive Word64 where
+ :: Word64 -> Word64 -> Word64
(+) = Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
(P.+)
zero :: Word64
zero = Word64
0
instance Subtractive Word64 where
negate :: Word64 -> Word64
negate = Word64 -> Word64
forall a. Num a => a -> a
P.negate
instance Additive b => Additive (a -> b) where
a -> b
f + :: (a -> b) -> (a -> b) -> a -> b
+ a -> b
f' = \a
a -> a -> b
f a
a b -> b -> b
forall a. Additive a => a -> a -> a
+ a -> b
f' a
a
zero :: a -> b
zero a
_ = b
forall a. Additive a => a
zero
instance Subtractive b => Subtractive (a -> b) where
negate :: (a -> b) -> a -> b
negate a -> b
f = b -> b
forall a. Subtractive a => a -> a
negate (b -> b) -> (a -> b) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
P.. a -> b
f