Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- class Semigroup r => Semiring r where
- (><) :: r -> r -> r
- fromBoolean :: Monoid r => Bool -> r
- unit :: (Monoid r, Semiring r) => r
- fromBooleanDef :: (Monoid r, Semiring r) => r -> Bool -> r
- product :: (Foldable t, Monoid r, Semiring r) => (a -> r) -> t a -> r
- product1 :: (Foldable1 t, Semiring r) => (a -> r) -> t a -> r
- cross :: (Foldable t, Applicative t, Monoid r, Semiring r) => t r -> t r -> r
- cross1 :: (Foldable1 t, Apply t, Semiring r) => t r -> t r -> r
- foldPresemiring :: Semiring r => (a -> r) -> NonEmpty (NonEmpty a) -> r
- foldNonunital :: (Monoid r, Semiring r) => (a -> r) -> [NonEmpty a] -> r
- foldUnital :: (Monoid r, Semiring r) => (a -> r) -> [[a]] -> r
- replicate :: Monoid r => Natural -> r -> r
- replicate' :: (Monoid r, Semiring r) => Natural -> r -> r
- (^) :: (Monoid r, Semiring r) => r -> Natural -> r
- powers :: (Monoid r, Semiring r) => Natural -> r -> r
- newtype Prod a = Prod {
- getProd :: a
Documentation
class Semigroup r => Semiring r where Source #
Right pre-semirings and (non-unital and unital) right semirings.
A right pre-semiring (sometimes referred to as a bisemigroup) is a type R endowed with two associative binary (i.e. semigroup) operations: (<>) and (><), along with a right-distributivity property connecting them:
(a <> b) >= (a< (b >< c)
A non-unital right semiring (sometimes referred to as a bimonoid) is a pre-semiring
with a mempty
element that is neutral with respect to both addition and multiplication.
A unital right semiring is a pre-semiring with two distinct neutral elements, mempty
and unit
, such that mempty
is right-neutral wrt addition, unit
is right-neutral wrt
multiplication, and mempty
is right-annihilative wrt multiplication.
Note that unit
needn't be distinct from mempty
.
Instances also need not be commutative nor left-distributive.
See the properties module for a detailed specification of the laws.
Instances
product :: (Foldable t, Monoid r, Semiring r) => (a -> r) -> t a -> r Source #
Fold over a collection using the multiplicative operation of a semiring.
product
f ≡foldr'
((><) . f)unit
>>>
(foldMap . product) id [[1, 2], [3, (4 :: Int)]] -- 1 >< 2 <> 3 >< 4
14
>>>
(product . foldMap) id [[1, 2], [3, (4 :: Int)]] -- 1 <> 2 >< 3 <> 4
21
For semirings without a distinct multiplicative unit this is equivalent to const mempty
:
>>>
product Just [1..(5 :: Int)]
Just 0
In this situation you most likely want to use product1
.
product1 :: (Foldable1 t, Semiring r) => (a -> r) -> t a -> r Source #
Fold over a non-empty collection using the multiplicative operation of a semiring.
As the collection is non-empty this does not require a distinct multiplicative unit:
>>>
product1 Just $ 1 :| [2..(5 :: Int)]
Just 120
cross :: (Foldable t, Applicative t, Monoid r, Semiring r) => t r -> t r -> r Source #
Cross-multiply two collections.
>>>
cross [1,2,3 ::Int] [1,2,3]
36
>>>
cross [1,2,3 ::Int] []
0
foldPresemiring :: Semiring r => (a -> r) -> NonEmpty (NonEmpty a) -> r Source #
Fold with no additive or multiplicative unit.
foldNonunital :: (Monoid r, Semiring r) => (a -> r) -> [NonEmpty a] -> r Source #
Fold with no multiplicative unit.
foldUnital :: (Monoid r, Semiring r) => (a -> r) -> [[a]] -> r Source #
Fold with additive & multiplicative units.
This function will zero out if there is no multiplicative unit.
replicate :: Monoid r => Natural -> r -> r Source #
A generalization of replicate
to an arbitrary Monoid
.
Adapted from http://augustss.blogspot.com/2008/07/lost-and-found-if-i-write-108-in.html.
Instances
Functor Prod Source # | |
Applicative Prod Source # | |
Bounded a => Bounded (Prod a) Source # | |
Eq a => Eq (Prod a) Source # | |
Ord a => Ord (Prod a) Source # | |
Show a => Show (Prod a) Source # | |
Generic (Prod a) Source # | |
Semiring a => Semigroup (Prod a) Source # | |
(Monoid a, Semiring a) => Monoid (Prod a) Source # | |
Generic1 Prod Source # | |
type Rep (Prod a) Source # | |
Defined in Data.Semiring | |
type Rep1 Prod Source # | |
Defined in Data.Semiring |