numeric-prelude-0.2: An experimental alternative hierarchy of numeric type classes

Algebra.Additive

Contents

Synopsis

Class

class C a whereSource

Additive a encapsulates the notion of a commutative group, specified by the following laws:

          a + b === b + a
    (a + b) + c === a + (b + c)
       zero + a === a
   a + negate a === 0

Typical examples include integers, dollars, and vectors.

Minimal definition: +, zero, and (negate or '(-)')

Methods

zero :: aSource

zero element of the vector space

(+) :: a -> a -> aSource

add and subtract elements

(-) :: a -> a -> aSource

negate :: a -> aSource

inverse with respect to +

Instances

C Double 
C Float 
C Int 
C Int8 
C Int16 
C Int32 
C Int64 
C Integer 
C Word 
C Word8 
C Word16 
C Word32 
C Word64 
C T 
C T 
C T 
C T 
C v => C [v] 
Integral a => C (Ratio a) 
(Ord a, C a) => C (T a) 
C a => C (T a) 
C a => C (T a) 
(C a, C a, C a) => C (T a) 
C a => C (T a) 
C a => C (T a) 
C a => C (T a) 
C a => C (T a) 
(C a, C a) => C (T a) 
C a => C (T a) 
(Eq a, C a) => C (T a) 
(Eq a, C a) => C (T a) 
C a => C (T a) 
C a => C (T a) 
C a => C (T a) 
C a => C (T a) 
C a => C (T a) 
C v => C (b -> v) 
(C v0, C v1) => C (v0, v1) 
(Ord i, Eq v, C v) => C (Map i v) 
(Ord a, C b) => C (T a b) 
(C u, C a) => C (T u a) 
C v => C (T a v) 
(Ord i, C a) => C (T i a) 
C v => C (T a v) 
(C v0, C v1, C v2) => C (v0, v1, v2) 

subtract :: C a => a -> a -> aSource

subtract is (-) with swapped operand order. This is the operand order which will be needed in most cases of partial application.

Complex functions

sum :: C a => [a] -> aSource

Sum up all elements of a list. An empty list yields zero.

This function is inappropriate for number types like Peano. Maybe we should make sum a method of Additive. This would also make lengthLeft and lengthRight superfluous.

sum1 :: C a => [a] -> aSource

Sum up all elements of a non-empty list. This avoids including a zero which is useful for types where no universal zero is available.

Instance definition helpers

elementAdd :: C x => (v -> x) -> T (v, v) xSource

Instead of baking the add operation into the element function, we could use higher rank types and pass a generic uncurry (+) to the run function. We do not do so in order to stay Haskell 98 at least for parts of NumericPrelude.

elementSub :: C x => (v -> x) -> T (v, v) xSource

elementNeg :: C x => (v -> x) -> T v xSource

(<*>.+) :: C x => T (v, v) (x -> a) -> (v -> x) -> T (v, v) aSource

 addPair :: (Additive.C a, Additive.C b) => (a,b) -> (a,b) -> (a,b)
 addPair = Elem.run2 $ Elem.with (,) <*>.+  fst <*>.+  snd

(<*>.-) :: C x => T (v, v) (x -> a) -> (v -> x) -> T (v, v) aSource

(<*>.-$) :: C x => T v (x -> a) -> (v -> x) -> T v aSource

Instances for atomic types

propAssociative :: (Eq a, C a) => a -> a -> a -> BoolSource

propCommutative :: (Eq a, C a) => a -> a -> BoolSource

propIdentity :: (Eq a, C a) => a -> BoolSource

propInverse :: (Eq a, C a) => a -> BoolSource