numhask
Safe HaskellNone
LanguageGHC2024

NumHask.Free.Additive

Description

Free commutative monoid — the initial encoding of Additive.

Synopsis

Documentation

data Additive a Source #

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.

Constructors

Zero 
Plus (Additive a) (Additive a) 
Embed a 

Instances

Instances details
Eq a => Eq (Additive a) Source # 
Instance details

Defined in NumHask.Free.Additive

Methods

(==) :: Additive a -> Additive a -> Bool #

(/=) :: Additive a -> Additive a -> Bool #

Show a => Show (Additive a) Source # 
Instance details

Defined in NumHask.Free.Additive

Methods

showsPrec :: Int -> Additive a -> ShowS #

show :: Additive a -> String #

showList :: [Additive a] -> ShowS #

zero :: Additive a Source #

Additive identity.

plus :: Additive a -> Additive a -> Additive a Source #

Addition with identity absorption.

plus zero a = a
plus a zero = a

embed :: a -> Additive a Source #

Embed a carrier value as an atomic generator.

lift :: (Eq a, Additive a) => a -> Additive a Source #

Lift a carrier value, absorbing the additive identity.

>>> lift 0
Zero

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)

flatten :: Additive a -> [a] Source #

Flatten a term to a list of embedded generators, discarding identities. This is the bag-of-generators view of the free commutative monoid; commutativity is not enforced structurally.