HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulenumeric-prelude-0.4.4Haskell98

Algebra.Additive

  • 1 class
  • 15 values

Class

6 declarations
classclass C a where
#

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 (-))

Instances48C, …
  • C IntegerDefined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Int16Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Int32Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Int64Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Int8Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Word16Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Word32Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Word64Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C Word8Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C DoubleDefined in numeric-prelude-0.4.4 · Algebra.Additive
  • C FloatDefined in numeric-prelude-0.4.4 · Algebra.Additive
  • C IntDefined in numeric-prelude-0.4.4 · Algebra.Additive
  • C WordDefined in numeric-prelude-0.4.4 · Algebra.Additive
  • C TDefined in numeric-prelude-0.4.4 · Number.FixedPoint.Check
  • C TDefined in numeric-prelude-0.4.4 · Number.GaloisField2p32m5
  • C TDefined in numeric-prelude-0.4.4 · Number.Peano
  • C TDefined in numeric-prelude-0.4.4 · Number.Positional.Check
  • RealFloat a => C (Complex a)Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • Num a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Wrapper.Haskell98
  • Integral a => C (Ratio a)Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.LaurentPolynomial
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Matrix
    Property
    genIntMatrix /\ \a -> genSameMatrix a /\ \b -> Laws.commutative (+) a b
    Property
    genIntMatrix /\ \a -> genSameMatrix a /\ \b -> genSameMatrix b /\ \c -> Laws.associative (+) a b c
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Polynomial
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.PowerSeries
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.PowerSeries2
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.Wrapper.NumericPrelude
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.Complex
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.PartiallyTranscendental
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.Quaternion
  • C v => C [v]Defined in numeric-prelude-0.4.4 · Algebra.Additive

    The Additive instantiations treat lists as prefixes of infinite lists with zero filled tail. This interpretation is not always appropriate. The end of a list may just mean: End of available data. In this case the shortening zip semantics would be more appropriate.

  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.ResidueClass.Func
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.NonNegativeChunky
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · Number.Ratio
  • C a => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.PowerSum
  • (Eq a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · Number.ResidueClass.Check
  • (Eq a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · Number.ResidueClass.Maybe
  • (Ord a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · Number.NonNegative · orphan
  • (C a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.RootSet
  • (C a, C a, C a) => C (T a)Defined in numeric-prelude-0.4.4 · MathObj.PartialFraction
    Property
    genPartialFractionInt /\ \x -> genPartialFractionInt /\ \y -> add x y
    Property
    genPartialFractionInt /\ \x -> genPartialFractionInt /\ \y -> sub x y
    Property
    genPartialFractionPoly /\ \x -> genPartialFractionPoly /\ \y -> add x y
    Property
    genPartialFractionPoly /\ \x -> genPartialFractionPoly /\ \y -> sub x y
  • C v => C (T a v)Defined in numeric-prelude-0.4.4 · Number.OccasionallyScalarExpression
  • C v => C (T a v)Defined in numeric-prelude-0.4.4 · Number.SI
  • C v => C (b -> v)Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • (Ord a, C b) => C (T a b)Defined in numeric-prelude-0.4.4 · MathObj.Algebra
  • (Ord i, Eq v, C v) => C (Map i v)Defined in numeric-prelude-0.4.4 · MathObj.DiscreteMap · orphan
  • (Ord i, C a) => C (T i a)Defined in numeric-prelude-0.4.4 · Number.Physical
  • (C v0, C v1) => C (v0, v1)Defined in numeric-prelude-0.4.4 · Algebra.Additive
  • (C u, C a) => C (T u a)Defined in numeric-prelude-0.4.4 · Number.DimensionTerm
  • (C v0, C v1, C v2) => C (v0, v1, v2)Defined in numeric-prelude-0.4.4 · Algebra.Additive
methodzero :: a
#

zero element of the vector space

method(+) :: a -> a -> a
#

add and subtract elements

method(-) :: a -> a -> a
#

add and subtract elements

methodnegate :: a -> a
#

inverse with respect to +

valuesubtract :: C a => a -> a -> a
#

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

Complex functions

4 declarations
valuesum :: C a => [a] -> a
#

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.

valuesum1 :: C a => [a] -> a
#

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. ToDo: Should have NonEmpty type.

Property
\(QC.NonEmpty ns) -> A.sum ns == (A.sum1 ns :: Integer)
valuesumNestedAssociative :: C a => [a] -> a
#

Sum the operands in an order, such that the dependencies are minimized. Does this have a measurably effect on speed?

Requires associativity.

Property
\ns -> A.sum ns == (A.sumNestedAssociative ns :: Integer)
valuesumNestedCommutative :: C a => [a] -> a
#

Make sure that the last entries in the list are equally often part of an addition. Maybe this can reduce rounding errors. The list that sum2 computes is a breadth-first-flattened binary tree.

Requires associativity and commutativity.

Property
\ns -> A.sum ns == (A.sumNestedCommutative ns :: Integer)

Instance definition helpers

6 declarations
valueelementAdd :: C x => (v -> x) -> T (v, v) x
#

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.

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

Instances for atomic types

4 declarations