module Algebra.Module where
import qualified Number.Ratio as Ratio
import qualified Algebra.PrincipalIdealDomain as PID
import qualified Algebra.Ring as Ring
import qualified Algebra.Additive as Additive
import qualified Algebra.ToInteger as ToInteger
import qualified Algebra.Laws as Laws
import Algebra.Ring ((*), fromInteger)
import Algebra.Additive ((+), zero)
import NumericPrelude.List (reduceRepeated)
import Data.List (map, zipWith, foldl)
import Prelude((.), Eq, Bool, Int, Integer, Float, Double)
infixr 7 *>
class (Additive.C b, Ring.C a) => C a b where
(*>) :: a -> b -> b
instance C Float Float where
(*>) = (*)
instance C Double Double where
(*>) = (*)
instance C Int Int where
(*>) = (*)
instance C Integer Integer where
(*>) = (*)
instance (PID.C a) => C (Ratio.T a) (Ratio.T a) where
(*>) = (*)
instance (PID.C a) => C Integer (Ratio.T a) where
x *> y = fromInteger x * y
instance (C a b0, C a b1) => C a (b0, b1) where
s *> (x0,x1) = (s *> x0, s *> x1)
instance (C a b0, C a b1, C a b2) => C a (b0, b1, b2) where
s *> (x0,x1,x2) = (s *> x0, s *> x1, s *> x2)
instance (C a b) => C a [b] where
(*>) = map . (*>)
instance (C a b) => C a (c -> b) where
(*>) s f = (*>) s . f
linearComb :: C a b => [a] -> [b] -> b
linearComb c = foldl (+) zero . zipWith (*>) c
integerMultiply :: (ToInteger.C a, Additive.C b) => a -> b -> b
integerMultiply a b =
reduceRepeated (+) zero b (ToInteger.toInteger a)
propCascade :: (Eq b, C a b) => b -> a -> a -> Bool
propCascade = Laws.leftCascade (*) (*>)
propRightDistributive :: (Eq b, C a b) => a -> b -> b -> Bool
propRightDistributive = Laws.rightDistributive (*>) (+)
propLeftDistributive :: (Eq b, C a b) => b -> a -> a -> Bool
propLeftDistributive x = Laws.homomorphism (*>x) (+) (+)