| Safe Haskell | None |
|---|---|
| Language | Haskell98 |
Data.Semiring
Description
A class for semirings (types with two binary operations, one commutative and one associative, and two respective identites), with various general-purpose instances.
Synopsis
- class Semiring a where
- (+) :: Semiring a => a -> a -> a
- (*) :: Semiring a => a -> a -> a
- (^) :: (Semiring a, Integral b) => a -> b -> a
- foldMapP :: (Foldable t, Semiring s) => (a -> s) -> t a -> s
- foldMapT :: (Foldable t, Semiring s) => (a -> s) -> t a -> s
- sum :: (Foldable t, Semiring a) => t a -> a
- product :: (Foldable t, Semiring a) => t a -> a
- sum' :: (Foldable t, Semiring a) => t a -> a
- product' :: (Foldable t, Semiring a) => t a -> a
- newtype Add a = Add {
- getAdd :: a
- newtype Mul a = Mul {
- getMul :: a
- newtype WrappedNum a = WrapNum {
- unwrapNum :: a
- class Semiring a => Ring a where
- negate :: a -> a
- (-) :: Ring a => a -> a -> a
- minus :: Ring a => a -> a -> a
Semiring typeclass
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: A commutative monoid and an
associative monoid. For any type R with a Num
instance, the commutative monoid is (R, '(Prelude.+)', 0)
and the associative monoid is (R, '(Prelude.*)', 1).
Instances should satisfy the following laws:
- additive identity
x+zero=zero+x = x
- additive associativity
x+(y+z) = (x+y)+z
- additive commutativity
x+y = y+x
- multiplicative identity
x*one=one*x = x
Methods
Arguments
| :: a | |
| -> a | |
| -> a | Commutative Operation |
Arguments
| :: a | Commutative Unit |
Arguments
| :: a | |
| -> a | |
| -> a | Associative Operation |
Arguments
| :: a | Associative Unit |
Instances
(^) :: (Semiring a, Integral b) => a -> b -> a infixr 8 #
Raise a number to a non-negative integral power.
If the power is negative, this will return zero.
foldMapP :: (Foldable t, Semiring s) => (a -> s) -> t a -> s #
Map each element of the structure to a semiring, and combine the results
using plus.
foldMapT :: (Foldable t, Semiring s) => (a -> s) -> t a -> s #
Map each element of the structure to a semiring, and combine the results
using times.
Types
Instances
| Functor Add # | |
| Foldable Add # | |
Defined in Data.Semiring Methods fold :: Monoid m => Add m -> m # foldMap :: Monoid m => (a -> m) -> Add a -> m # foldr :: (a -> b -> b) -> b -> Add a -> b # foldr' :: (a -> b -> b) -> b -> Add a -> b # foldl :: (b -> a -> b) -> b -> Add a -> b # foldl' :: (b -> a -> b) -> b -> Add a -> b # foldr1 :: (a -> a -> a) -> Add a -> a # foldl1 :: (a -> a -> a) -> Add a -> a # elem :: Eq a => a -> Add a -> Bool # maximum :: Ord a => Add a -> a # | |
| Traversable Add # | |
| Bounded a => Bounded (Add a) # | |
| Enum a => Enum (Add a) # | |
| Eq a => Eq (Add a) # | |
| Fractional a => Fractional (Add a) # | |
| Num a => Num (Add a) # | |
| Ord a => Ord (Add a) # | |
| Read a => Read (Add a) # | |
| Real a => Real (Add a) # | |
Defined in Data.Semiring Methods toRational :: Add a -> Rational # | |
| RealFrac a => RealFrac (Add a) # | |
| Show a => Show (Add a) # | |
| Generic (Add a) # | |
| Semiring a => Semigroup (Add a) # | |
| Semiring a => Monoid (Add a) # | |
| Storable a => Storable (Add a) # | |
| Semiring a => Semiring (Add a) # | |
| Generic1 Add # | |
| type Rep (Add a) # | |
Defined in Data.Semiring | |
| type Rep1 Add # | |
Defined in Data.Semiring | |
Instances
| Functor Mul # | |
| Foldable Mul # | |
Defined in Data.Semiring Methods fold :: Monoid m => Mul m -> m # foldMap :: Monoid m => (a -> m) -> Mul a -> m # foldr :: (a -> b -> b) -> b -> Mul a -> b # foldr' :: (a -> b -> b) -> b -> Mul a -> b # foldl :: (b -> a -> b) -> b -> Mul a -> b # foldl' :: (b -> a -> b) -> b -> Mul a -> b # foldr1 :: (a -> a -> a) -> Mul a -> a # foldl1 :: (a -> a -> a) -> Mul a -> a # elem :: Eq a => a -> Mul a -> Bool # maximum :: Ord a => Mul a -> a # | |
| Traversable Mul # | |
| Bounded a => Bounded (Mul a) # | |
| Enum a => Enum (Mul a) # | |
| Eq a => Eq (Mul a) # | |
| Fractional a => Fractional (Mul a) # | |
| Num a => Num (Mul a) # | |
| Ord a => Ord (Mul a) # | |
| Read a => Read (Mul a) # | |
| Real a => Real (Mul a) # | |
Defined in Data.Semiring Methods toRational :: Mul a -> Rational # | |
| RealFrac a => RealFrac (Mul a) # | |
| Show a => Show (Mul a) # | |
| Generic (Mul a) # | |
| Semiring a => Semigroup (Mul a) # | |
| Semiring a => Monoid (Mul a) # | |
| Storable a => Storable (Mul a) # | |
| Semiring a => Semiring (Mul a) # | |
| Generic1 Mul # | |
| type Rep (Mul a) # | |
Defined in Data.Semiring | |
| type Rep1 Mul # | |
Defined in Data.Semiring | |
newtype WrappedNum a #
Provide Semiring and Ring for an arbitrary Num. It is useful with GHC 8.6+'s DerivingVia extension.
Instances
Ring typeclass
class Semiring a => Ring a where #