semirings-0.1.3.0: two monoids as one, in holy haskimony

Safe HaskellNone
LanguageHaskell98

Data.Semiring

Contents

Synopsis

Semiring typeclass

class 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: A commutative monoid and an associative monoid. For any type R with a Num instance, the commutative monoid is (R, +, 0) and the associative monoid is (R, *, 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
multiplicative associativity
x * (y * z) = (x * y) * z
left- and right-distributivity of * over +
x * (y + z) = (x * y) + (x * z)
(x + y) * z = (x * z) + (y * z)
annihilation
zero * x = x * zero = zero

Minimal complete definition

plus, zero, times, one

Methods

plus infixl 6 #

Arguments

:: a 
-> a 
-> a

Commutative Operation

zero #

Arguments

:: a

Commutative Unit

times infixl 7 #

Arguments

:: a 
-> a 
-> a

Associative Operation

one #

Arguments

:: a

Associative Unit

zero #

Arguments

:: Num a 
=> a

Commutative Unit

one #

Arguments

:: Num a 
=> a

Associative Unit

plus infixl 6 #

Arguments

:: Num a 
=> a 
-> a 
-> a

Commutative Operation

times infixl 7 #

Arguments

:: Num a 
=> a 
-> a 
-> a

Associative Operation

Instances

Semiring Bool # 

Methods

plus :: Bool -> Bool -> Bool #

zero :: Bool #

times :: Bool -> Bool -> Bool #

one :: Bool #

Semiring Double # 
Semiring Float # 

Methods

plus :: Float -> Float -> Float #

zero :: Float #

times :: Float -> Float -> Float #

one :: Float #

Semiring Int # 

Methods

plus :: Int -> Int -> Int #

zero :: Int #

times :: Int -> Int -> Int #

one :: Int #

Semiring Int8 # 

Methods

plus :: Int8 -> Int8 -> Int8 #

zero :: Int8 #

times :: Int8 -> Int8 -> Int8 #

one :: Int8 #

Semiring Int16 # 

Methods

plus :: Int16 -> Int16 -> Int16 #

zero :: Int16 #

times :: Int16 -> Int16 -> Int16 #

one :: Int16 #

Semiring Int32 # 

Methods

plus :: Int32 -> Int32 -> Int32 #

zero :: Int32 #

times :: Int32 -> Int32 -> Int32 #

one :: Int32 #

Semiring Int64 # 

Methods

plus :: Int64 -> Int64 -> Int64 #

zero :: Int64 #

times :: Int64 -> Int64 -> Int64 #

one :: Int64 #

Semiring Integer # 
Semiring Natural # 
Semiring Word # 

Methods

plus :: Word -> Word -> Word #

zero :: Word #

times :: Word -> Word -> Word #

one :: Word #

Semiring Word8 # 

Methods

plus :: Word8 -> Word8 -> Word8 #

zero :: Word8 #

times :: Word8 -> Word8 -> Word8 #

one :: Word8 #

Semiring Word16 # 
Semiring Word32 # 
Semiring Word64 # 
Semiring () # 

Methods

plus :: () -> () -> () #

zero :: () #

times :: () -> () -> () #

one :: () #

Semiring CDev # 

Methods

plus :: CDev -> CDev -> CDev #

zero :: CDev #

times :: CDev -> CDev -> CDev #

one :: CDev #

Semiring CIno # 

Methods

plus :: CIno -> CIno -> CIno #

zero :: CIno #

times :: CIno -> CIno -> CIno #

one :: CIno #

Semiring CMode # 

Methods

plus :: CMode -> CMode -> CMode #

zero :: CMode #

times :: CMode -> CMode -> CMode #

one :: CMode #

Semiring COff # 

Methods

plus :: COff -> COff -> COff #

zero :: COff #

times :: COff -> COff -> COff #

one :: COff #

Semiring CPid # 

Methods

plus :: CPid -> CPid -> CPid #

zero :: CPid #

times :: CPid -> CPid -> CPid #

one :: CPid #

Semiring CSsize # 
Semiring CGid # 

Methods

plus :: CGid -> CGid -> CGid #

zero :: CGid #

times :: CGid -> CGid -> CGid #

one :: CGid #

Semiring CNlink # 
Semiring CUid # 

Methods

plus :: CUid -> CUid -> CUid #

zero :: CUid #

times :: CUid -> CUid -> CUid #

one :: CUid #

Semiring CCc # 

Methods

plus :: CCc -> CCc -> CCc #

zero :: CCc #

times :: CCc -> CCc -> CCc #

one :: CCc #

Semiring CSpeed # 
Semiring CTcflag # 
Semiring CRLim # 

Methods

plus :: CRLim -> CRLim -> CRLim #

zero :: CRLim #

times :: CRLim -> CRLim -> CRLim #

one :: CRLim #

Semiring Fd # 

Methods

plus :: Fd -> Fd -> Fd #

zero :: Fd #

times :: Fd -> Fd -> Fd #

one :: Fd #

Semiring CChar # 

Methods

plus :: CChar -> CChar -> CChar #

zero :: CChar #

times :: CChar -> CChar -> CChar #

one :: CChar #

Semiring CSChar # 
Semiring CUChar # 
Semiring CShort # 
Semiring CUShort # 
Semiring CInt # 

Methods

plus :: CInt -> CInt -> CInt #

zero :: CInt #

times :: CInt -> CInt -> CInt #

one :: CInt #

Semiring CUInt # 

Methods

plus :: CUInt -> CUInt -> CUInt #

zero :: CUInt #

times :: CUInt -> CUInt -> CUInt #

one :: CUInt #

Semiring CLong # 

Methods

plus :: CLong -> CLong -> CLong #

zero :: CLong #

times :: CLong -> CLong -> CLong #

one :: CLong #

Semiring CULong # 
Semiring CLLong # 
Semiring CULLong # 
Semiring CFloat # 
Semiring CDouble # 
Semiring CPtrdiff # 
Semiring CSize # 

Methods

plus :: CSize -> CSize -> CSize #

zero :: CSize #

times :: CSize -> CSize -> CSize #

one :: CSize #

Semiring CWchar # 
Semiring CSigAtomic # 
Semiring CClock # 
Semiring CTime # 

Methods

plus :: CTime -> CTime -> CTime #

zero :: CTime #

times :: CTime -> CTime -> CTime #

one :: CTime #

Semiring CUSeconds # 
Semiring CSUSeconds # 
Semiring CIntPtr # 
Semiring CUIntPtr # 
Semiring CIntMax # 
Semiring CUIntMax # 
Semiring WordPtr # 
Semiring IntPtr # 
Semiring IntSet # 
Semiring a => Semiring [a] # 

Methods

plus :: [a] -> [a] -> [a] #

zero :: [a] #

times :: [a] -> [a] -> [a] #

one :: [a] #

Semiring a => Semiring (Maybe a) # 

Methods

plus :: Maybe a -> Maybe a -> Maybe a #

zero :: Maybe a #

times :: Maybe a -> Maybe a -> Maybe a #

one :: Maybe a #

Integral a => Semiring (Ratio a) # 

Methods

plus :: Ratio a -> Ratio a -> Ratio a #

zero :: Ratio a #

times :: Ratio a -> Ratio a -> Ratio a #

one :: Ratio a #

Semiring a => Semiring (IO a) # 

Methods

plus :: IO a -> IO a -> IO a #

zero :: IO a #

times :: IO a -> IO a -> IO a #

one :: IO a #

Ring a => Semiring (Complex a) # 

Methods

plus :: Complex a -> Complex a -> Complex a #

zero :: Complex a #

times :: Complex a -> Complex a -> Complex a #

one :: Complex a #

HasResolution a => Semiring (Fixed a) # 

Methods

plus :: Fixed a -> Fixed a -> Fixed a #

zero :: Fixed a #

times :: Fixed a -> Fixed a -> Fixed a #

one :: Fixed a #

Semiring a => Semiring (Min a) # 

Methods

plus :: Min a -> Min a -> Min a #

zero :: Min a #

times :: Min a -> Min a -> Min a #

one :: Min a #

Semiring a => Semiring (Max a) # 

Methods

plus :: Max a -> Max a -> Max a #

zero :: Max a #

times :: Max a -> Max a -> Max a #

one :: Max a #

Semiring a => Semiring (Identity a) # 

Methods

plus :: Identity a -> Identity a -> Identity a #

zero :: Identity a #

times :: Identity a -> Identity a -> Identity a #

one :: Identity a #

Semiring a => Semiring (Dual a) # 

Methods

plus :: Dual a -> Dual a -> Dual a #

zero :: Dual a #

times :: Dual a -> Dual a -> Dual a #

one :: Dual a #

Monoid a => Semiring (Endo a) #

This is not a true semiring. Even if the underlying monoid is commutative, it is only a near semiring. It is, however, quite useful. For instance, this type:

forall a. Endo (Endo a)

is a valid encoding of church numerals, with addition and multiplication being their semiring variants.

Methods

plus :: Endo a -> Endo a -> Endo a #

zero :: Endo a #

times :: Endo a -> Endo a -> Endo a #

one :: Endo a #

Semiring a => Semiring (Sum a) # 

Methods

plus :: Sum a -> Sum a -> Sum a #

zero :: Sum a #

times :: Sum a -> Sum a -> Sum a #

one :: Sum a #

Semiring a => Semiring (Product a) # 

Methods

plus :: Product a -> Product a -> Product a #

zero :: Product a #

times :: Product a -> Product a -> Product a #

one :: Product a #

Semiring a => Semiring (Down a) # 

Methods

plus :: Down a -> Down a -> Down a #

zero :: Down a #

times :: Down a -> Down a -> Down a #

one :: Down a #

Semiring a => Semiring (IntMap a) # 

Methods

plus :: IntMap a -> IntMap a -> IntMap a #

zero :: IntMap a #

times :: IntMap a -> IntMap a -> IntMap a #

one :: IntMap a #

Semiring a => Semiring (Seq a) # 

Methods

plus :: Seq a -> Seq a -> Seq a #

zero :: Seq a #

times :: Seq a -> Seq a -> Seq a #

one :: Seq a #

(Ord a, Semiring a) => Semiring (Set a) # 

Methods

plus :: Set a -> Set a -> Set a #

zero :: Set a #

times :: Set a -> Set a -> Set a #

one :: Set a #

(Eq a, Hashable a, Semiring a) => Semiring (HashSet a) # 

Methods

plus :: HashSet a -> HashSet a -> HashSet a #

zero :: HashSet a #

times :: HashSet a -> HashSet a -> HashSet a #

one :: HashSet a #

(Unbox a, Semiring a) => Semiring (Vector a) # 

Methods

plus :: Vector a -> Vector a -> Vector a #

zero :: Vector a #

times :: Vector a -> Vector a -> Vector a #

one :: Vector a #

(Storable a, Semiring a) => Semiring (Vector a) # 

Methods

plus :: Vector a -> Vector a -> Vector a #

zero :: Vector a #

times :: Vector a -> Vector a -> Vector a #

one :: Vector a #

Semiring a => Semiring (Vector a) # 

Methods

plus :: Vector a -> Vector a -> Vector a #

zero :: Vector a #

times :: Vector a -> Vector a -> Vector a #

one :: Vector a #

Semiring b => Semiring (a -> b) # 

Methods

plus :: (a -> b) -> (a -> b) -> a -> b #

zero :: a -> b #

times :: (a -> b) -> (a -> b) -> a -> b #

one :: a -> b #

(Ord a, Semiring a, Semiring b) => Semiring (Map a b) # 

Methods

plus :: Map a b -> Map a b -> Map a b #

zero :: Map a b #

times :: Map a b -> Map a b -> Map a b #

one :: Map a b #

(Eq k, Hashable k, Semiring k, Semiring v) => Semiring (HashMap k v) # 

Methods

plus :: HashMap k v -> HashMap k v -> HashMap k v #

zero :: HashMap k v #

times :: HashMap k v -> HashMap k v -> HashMap k v #

one :: HashMap k v #

Semiring a => Semiring (Const * a b) # 

Methods

plus :: Const * a b -> Const * a b -> Const * a b #

zero :: Const * a b #

times :: Const * a b -> Const * a b -> Const * a b #

one :: Const * a b #

(Alternative f, Semiring a) => Semiring (Alt * f a) # 

Methods

plus :: Alt * f a -> Alt * f a -> Alt * f a #

zero :: Alt * f a #

times :: Alt * f a -> Alt * f a -> Alt * f a #

one :: Alt * f a #

(+) :: Semiring a => a -> a -> a infixl 6 #

Infix shorthand for plus.

(*) :: Semiring a => a -> a -> a infixl 7 #

Infix shorthand for times.

(^) :: (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.

sum :: (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'.

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

The prod function computes the multiplicative sum of the elements in a structure. This function is lazy. for a strict version, see prod'.

sum' :: (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.

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

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

Ring typeclass

class Semiring a => Ring a where #

The class of semirings with an additive inverse.

negate a + a = zero

Minimal complete definition

negate

Methods

negate :: a -> a #

negate :: Num a => a -> a #

Instances

Ring Bool # 

Methods

negate :: Bool -> Bool #

Ring Double # 

Methods

negate :: Double -> Double #

Ring Float # 

Methods

negate :: Float -> Float #

Ring Int # 

Methods

negate :: Int -> Int #

Ring Int8 # 

Methods

negate :: Int8 -> Int8 #

Ring Int16 # 

Methods

negate :: Int16 -> Int16 #

Ring Int32 # 

Methods

negate :: Int32 -> Int32 #

Ring Int64 # 

Methods

negate :: Int64 -> Int64 #

Ring Integer # 

Methods

negate :: Integer -> Integer #

Ring Natural # 

Methods

negate :: Natural -> Natural #

Ring Word # 

Methods

negate :: Word -> Word #

Ring Word8 # 

Methods

negate :: Word8 -> Word8 #

Ring Word16 # 

Methods

negate :: Word16 -> Word16 #

Ring Word32 # 

Methods

negate :: Word32 -> Word32 #

Ring Word64 # 

Methods

negate :: Word64 -> Word64 #

Ring () # 

Methods

negate :: () -> () #

Ring CDev # 

Methods

negate :: CDev -> CDev #

Ring CIno # 

Methods

negate :: CIno -> CIno #

Ring CMode # 

Methods

negate :: CMode -> CMode #

Ring COff # 

Methods

negate :: COff -> COff #

Ring CPid # 

Methods

negate :: CPid -> CPid #

Ring CSsize # 

Methods

negate :: CSsize -> CSsize #

Ring CGid # 

Methods

negate :: CGid -> CGid #

Ring CNlink # 

Methods

negate :: CNlink -> CNlink #

Ring CUid # 

Methods

negate :: CUid -> CUid #

Ring CCc # 

Methods

negate :: CCc -> CCc #

Ring CSpeed # 

Methods

negate :: CSpeed -> CSpeed #

Ring CTcflag # 

Methods

negate :: CTcflag -> CTcflag #

Ring CRLim # 

Methods

negate :: CRLim -> CRLim #

Ring Fd # 

Methods

negate :: Fd -> Fd #

Ring CChar # 

Methods

negate :: CChar -> CChar #

Ring CSChar # 

Methods

negate :: CSChar -> CSChar #

Ring CUChar # 

Methods

negate :: CUChar -> CUChar #

Ring CShort # 

Methods

negate :: CShort -> CShort #

Ring CUShort # 

Methods

negate :: CUShort -> CUShort #

Ring CInt # 

Methods

negate :: CInt -> CInt #

Ring CUInt # 

Methods

negate :: CUInt -> CUInt #

Ring CLong # 

Methods

negate :: CLong -> CLong #

Ring CULong # 

Methods

negate :: CULong -> CULong #

Ring CLLong # 

Methods

negate :: CLLong -> CLLong #

Ring CULLong # 

Methods

negate :: CULLong -> CULLong #

Ring CFloat # 

Methods

negate :: CFloat -> CFloat #

Ring CDouble # 

Methods

negate :: CDouble -> CDouble #

Ring CPtrdiff # 

Methods

negate :: CPtrdiff -> CPtrdiff #

Ring CSize # 

Methods

negate :: CSize -> CSize #

Ring CWchar # 

Methods

negate :: CWchar -> CWchar #

Ring CSigAtomic # 
Ring CClock # 

Methods

negate :: CClock -> CClock #

Ring CTime # 

Methods

negate :: CTime -> CTime #

Ring CUSeconds # 
Ring CSUSeconds # 
Ring CIntPtr # 

Methods

negate :: CIntPtr -> CIntPtr #

Ring CUIntPtr # 

Methods

negate :: CUIntPtr -> CUIntPtr #

Ring CIntMax # 

Methods

negate :: CIntMax -> CIntMax #

Ring CUIntMax # 

Methods

negate :: CUIntMax -> CUIntMax #

Ring WordPtr # 

Methods

negate :: WordPtr -> WordPtr #

Ring IntPtr # 

Methods

negate :: IntPtr -> IntPtr #

Ring a => Ring [a] # 

Methods

negate :: [a] -> [a] #

Ring a => Ring (Maybe a) # 

Methods

negate :: Maybe a -> Maybe a #

Integral a => Ring (Ratio a) # 

Methods

negate :: Ratio a -> Ratio a #

Ring a => Ring (IO a) # 

Methods

negate :: IO a -> IO a #

Ring a => Ring (Complex a) # 

Methods

negate :: Complex a -> Complex a #

HasResolution a => Ring (Fixed a) # 

Methods

negate :: Fixed a -> Fixed a #

Ring a => Ring (Min a) # 

Methods

negate :: Min a -> Min a #

Ring a => Ring (Max a) # 

Methods

negate :: Max a -> Max a #

Ring a => Ring (Identity a) # 

Methods

negate :: Identity a -> Identity a #

Ring a => Ring (Dual a) # 

Methods

negate :: Dual a -> Dual a #

(Monoid a, Ring a) => Ring (Endo a) # 

Methods

negate :: Endo a -> Endo a #

Ring a => Ring (Sum a) # 

Methods

negate :: Sum a -> Sum a #

Ring a => Ring (Product a) # 

Methods

negate :: Product a -> Product a #

Ring a => Ring (Down a) # 

Methods

negate :: Down a -> Down a #

(Unbox a, Ring a) => Ring (Vector a) # 

Methods

negate :: Vector a -> Vector a #

(Storable a, Ring a) => Ring (Vector a) # 

Methods

negate :: Vector a -> Vector a #

Ring a => Ring (Vector a) # 

Methods

negate :: Vector a -> Vector a #

Ring b => Ring (a -> b) # 

Methods

negate :: (a -> b) -> a -> b #

Ring a => Ring (Const * a b) # 

Methods

negate :: Const * a b -> Const * a b #

(Alternative f, Ring a) => Ring (Alt * f a) # 

Methods

negate :: Alt * f a -> Alt * f a #

(-) :: Ring a => a -> a -> a infixl 6 #

Infix shorthand for minus.

minus :: Ring a => a -> a -> a infixl 6 #

Substract two Ring values. For any type R with a Num instance, this is the same as Prelude's -.

x minus y = x + negate y