| Safe Haskell | None |
|---|---|
| Language | GHC2024 |
NumHask.Free.Additive
Description
Free commutative monoid — the initial encoding of Additive.
Synopsis
- data Additive a
- zero :: Additive a
- plus :: Additive a -> Additive a -> Additive a
- embed :: a -> Additive a
- lift :: (Eq a, Additive a) => a -> Additive a
- normalize :: (Eq a, Additive a) => Additive a -> Additive a
- eval :: Additive a => Additive a -> a
- foldAdditive :: b -> (b -> b -> b) -> (a -> b) -> Additive a -> b
- flatten :: Additive a -> [a]
Documentation
Free commutative monoid over a carrier type.
The initial encoding of Additive.
Terms are built from zero, plus, and embed.
Use smart constructors zero and plus for identity-normalised
terms. eval projects into any target monoid satisfying the
Additive laws.
plus :: Additive a -> Additive a -> Additive a Source #
Addition with identity absorption.
plus zero a = a plus a zero = a
lift :: (Eq a, Additive a) => a -> Additive a Source #
Lift a carrier value, absorbing the additive identity.
>>>lift 0Zero
normalize :: (Eq a, Additive a) => Additive a -> Additive a Source #
Normalize a term with respect to additive-monoid laws.
>>>normalize (embed 0)Zero
eval :: Additive a => Additive a -> a Source #
Evaluate a term into any Additive.
This is the unique homomorphism out of the free commutative monoid.
>>>eval (plus (embed 1) (embed 2))3
foldAdditive :: b -> (b -> b -> b) -> (a -> b) -> Additive a -> b Source #
Universal property: fold with a target monoid.
foldAdditive z p f . embed == f foldAdditive z p f zero == z foldAdditive z p f (plus a b) == p (foldAdditive z p f a) (foldAdditive z p f b)