module Algebra.Additive (
C,
zero,
(+), (),
negate, subtract,
sum, sum1,
sumNestedAssociative,
sumNestedCommutative,
elementAdd, elementSub, elementNeg,
(<*>.+), (<*>.-), (<*>.-$),
propAssociative,
propCommutative,
propIdentity,
propInverse,
) where
import qualified Algebra.Laws as Laws
import Data.Int (Int, Int8, Int16, Int32, Int64, )
import Data.Word (Word, Word8, Word16, Word32, Word64, )
import qualified NumericPrelude.Elementwise as Elem
import Control.Applicative (Applicative(pure, (<*>)), )
import Data.Tuple.HT (fst3, snd3, thd3, )
import qualified Data.List.Match as Match
import qualified Data.Complex as Complex98
import qualified Data.Ratio as Ratio98
import qualified Prelude as P
import Prelude (Integer, Float, Double, fromInteger, )
import NumericPrelude.Base
infixl 6 +,
class C a where
zero :: a
(+), () :: a -> a -> a
negate :: a -> a
negate a = zero a
a b = a + negate b
subtract :: C a => a -> a -> a
subtract = flip ()
sum :: (C a) => [a] -> a
sum = foldl (+) zero
sum1 :: (C a) => [a] -> a
sum1 = foldl1 (+)
sumNestedAssociative :: (C a) => [a] -> a
sumNestedAssociative [] = zero
sumNestedAssociative [x] = x
sumNestedAssociative xs = sumNestedAssociative (sum2 xs)
sumNestedCommutative :: (C a) => [a] -> a
sumNestedCommutative [] = zero
sumNestedCommutative xs@(_:rs) =
let ys = xs ++ Match.take rs (sum2 ys)
in last ys
_sumNestedCommutative :: (C a) => [a] -> a
_sumNestedCommutative [] = zero
_sumNestedCommutative xs@(_:rs) =
let ys = xs ++ take (length rs) (sum2 ys)
in last ys
sum2 :: (C a) => [a] -> [a]
sum2 (x:y:rest) = (x+y) : sum2 rest
sum2 xs = xs
elementAdd ::
(C x) =>
(v -> x) -> Elem.T (v,v) x
elementAdd f =
Elem.element (\(x,y) -> f x + f y)
elementSub ::
(C x) =>
(v -> x) -> Elem.T (v,v) x
elementSub f =
Elem.element (\(x,y) -> f x f y)
elementNeg ::
(C x) =>
(v -> x) -> Elem.T v x
elementNeg f =
Elem.element (negate . f)
infixl 4 <*>.+, <*>.-, <*>.-$
(<*>.+) ::
(C x) =>
Elem.T (v,v) (x -> a) -> (v -> x) -> Elem.T (v,v) a
(<*>.+) f acc =
f <*> elementAdd acc
(<*>.-) ::
(C x) =>
Elem.T (v,v) (x -> a) -> (v -> x) -> Elem.T (v,v) a
(<*>.-) f acc =
f <*> elementSub acc
(<*>.-$) ::
(C x) =>
Elem.T v (x -> a) -> (v -> x) -> Elem.T v a
(<*>.-$) f acc =
f <*> elementNeg acc
instance C Integer where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Float where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Double where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Int where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Int8 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Int16 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Int32 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Int64 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Word where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Word8 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Word16 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Word32 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance C Word64 where
zero = P.fromInteger 0
negate = P.negate
(+) = (P.+)
() = (P.-)
instance (C v0, C v1) => C (v0, v1) where
zero = (,) zero zero
(+) = Elem.run2 $ pure (,) <*>.+ fst <*>.+ snd
() = Elem.run2 $ pure (,) <*>.- fst <*>.- snd
negate = Elem.run $ pure (,) <*>.-$ fst <*>.-$ snd
instance (C v0, C v1, C v2) => C (v0, v1, v2) where
zero = (,,) zero zero zero
(+) = Elem.run2 $ pure (,,) <*>.+ fst3 <*>.+ snd3 <*>.+ thd3
() = Elem.run2 $ pure (,,) <*>.- fst3 <*>.- snd3 <*>.- thd3
negate = Elem.run $ pure (,,) <*>.-$ fst3 <*>.-$ snd3 <*>.-$ thd3
instance (C v) => C [v] where
zero = []
negate = map negate
(+) (x:xs) (y:ys) = (+) x y : (+) xs ys
(+) xs [] = xs
(+) [] ys = ys
() (x:xs) (y:ys) = () x y : () xs ys
() xs [] = xs
() [] ys = negate ys
instance (C v) => C (b -> v) where
zero _ = zero
(+) f g x = (+) (f x) (g x)
() f g x = () (f x) (g x)
negate f x = negate (f x)
propAssociative :: (Eq a, C a) => a -> a -> a -> Bool
propCommutative :: (Eq a, C a) => a -> a -> Bool
propIdentity :: (Eq a, C a) => a -> Bool
propInverse :: (Eq a, C a) => a -> Bool
propCommutative = Laws.commutative (+)
propAssociative = Laws.associative (+)
propIdentity = Laws.identity (+) zero
propInverse = Laws.inverse (+) negate zero
instance (P.Integral a) => C (Ratio98.Ratio a) where
zero = P.fromInteger 0
(+) = (P.+)
() = (P.-)
negate = P.negate
instance (P.RealFloat a) => C (Complex98.Complex a) where
zero = P.fromInteger 0
(+) = (P.+)
() = (P.-)
negate = P.negate