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

Modulesemirings-0.7Haskell98

Data.Semiring

A class for semirings (types with two binary operations, one commutative and one associative, and two respective identities), with various general-purpose instances.

  • 6 types
  • 2 classes
  • 15 values
  • Packagesemirings-0.7
  • Exports23
  • LanguageHaskell98
  • LicenceBSD-3-Clause
  • SourceSemiring.hs

Semiring typeclass

12 declarations
classclass Semiring a where
#

The class of semirings (types with two binary operations and two respective identities). One can think of a semiring as two monoids of the same underlying type, with the first being commutative. In the documentation, you will often see the first monoid being referred to as additive, and the second monoid being referred to as multiplicative, a typical convention when talking about semirings.

For any type R with a Num instance, the additive monoid is (R, +, 0) and the multiplicative monoid is (R, *, 1).

For Prelude.Bool, the additive monoid is (Prelude.Bool, Prelude.||, Prelude.False) and the multiplicative monoid is (Prelude.Bool, Prelude.&&, Prelude.True).

Instances should satisfy the following laws:

additive left identity

zero + x = x

additive right identity

x + zero = x

additive associativity

x + (y + z) = (x + y) + z

additive commutativity

x + y = y + x

multiplicative left identity

one * x = x

multiplicative right identity

x * one = x

multiplicative associativity

x * (y * z) = (x * y) * z

left-distributivity of * over +

x * (y + z) = (x * y) + (x * z)

right-distributivity of * over +

(x + y) * z = (x * z) + (y * z)

annihilation

zero * x = x * zero = zero

Methods

Instances96Semiring, …
value(^) :: (Semiring a, Integral b) => a -> b -> a
#

Raise a number to a non-negative integral power. If the power is negative, this will call error.

valuefoldMapP :: (Foldable t, Semiring s) => (a -> s) -> t a -> s
#

Map each element of the structure to a semiring, and combine the results using plus.

valuefoldMapT :: (Foldable t, Semiring s) => (a -> s) -> t a -> s
#

Map each element of the structure to a semiring, and combine the results using times.

valuesum :: (Foldable t, Semiring a) => t a -> a
#

The sum function computes the additive sum of the elements in a structure. This function is lazy. For a strict version, see sum'.

valuesum' :: (Foldable t, Semiring a) => t a -> a
#

The sum' function computes the additive sum of the elements in a structure. This function is strict. For a lazy version, see sum.

Types

6 declarations
newtypenewtype Add a
#

Monoid under plus. Analogous to Sum, but uses the Semiring constraint rather than Num.

Constructors

Instances20Functor, Foldable, Traversable, Generic1, Bounded, Enum, …
newtypenewtype Mul a
#

Monoid under times. Analogous to Product, but uses the Semiring constraint rather than Num.

Constructors

Instances20Functor, Foldable, Traversable, Generic1, Bounded, Enum, …
newtypenewtype WrappedNum a
#

Provide Semiring and Ring for an arbitrary Num. It is useful with GHC 8.6+'s DerivingVia extension.

Constructors

Instances21Functor, Foldable, Traversable, Generic1, Bounded, Enum, …
newtypenewtype Mod2
#

Mod2 represents the integers mod 2.

It is useful in the computing of Zhegalkin polynomials.

Constructors

Instances14Bounded, Enum, Eq, Ord, Read, Show, …
newtypenewtype IntSetOf a
#

Wrapper to mimic Set (Data.Semigroup.Sum Int), Set (Data.Semigroup.Product Int), etc., while having a more efficient underlying representation.

Constructors

Instances11Generic1, Eq, Ord, Read, Show, Generic, …
newtypenewtype IntMapOf k v
#

Wrapper to mimic Map (Data.Semigroup.Sum Int) v, Map (Data.Semigroup.Product Int) v, etc., while having a more efficient underlying representation.

Constructors

Instances11Generic1, Eq, Ord, Read, Show, Generic, …

Ring typeclass

5 declarations
classclass Semiring a => Ring a where
#

The class of semirings with an additive inverse.

negate a + a = zero

Methods

Instances81Ring, …
valuefromInteger :: Ring a => Integer -> a
#

Convert from integer to ring.

When {-# LANGUAGE RebindableSyntax #-} is enabled, this function is used for desugaring integer literals. This may be used to facilitate transition from Num to Ring: no need to replace 0 and 1 with one and zero or to cast numeric literals.

valueminus :: Ring a => a -> a -> a
#

Subtract two Ring values. For any type R with a Num instance, this is the same as (Prelude.-).

x minus y = x + negate y
value(-) :: Ring a => a -> a -> a
#

Infix shorthand for minus.