Subclasses of Monoid with a solid thoretical foundation and practical purposes
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Subclasses of Monoid with a solid theoretical foundation and practical purposes

The monoid-subclasses package has been released on Hackage. The package defines several classes that are richer than monoids but less demanding than groups:

  • ReductiveMonoid provides the operator </> which acts as a partial inverse of the <> operator, i.e., Monoid.mappend.

  • CancellativeMonoid is a subclass of ReductiveMonoid that provides additional guarantees about the </> operation result:

    (a <> b) </> a == Just b
    (a <> b) </> b == Just a

    Every group (i.e., every Monoid a with the operation inverse :: a -> a) is a CancellativeMonoid where a </> b = Just (a <> inverse b) but not every CancellativeMonoid is a group.

  • GCDMonoid is a subclass of ReductiveMonoid that provides the gcd operation for getting the greatest common denominator for two given monoid values.

  • MonoidNull class provides the Boolean null operation that checks if the argument monoid is mempty.

  • FactorialMonoid class represents monoids that can be split up into irreducible factors.

That's the theoretical point of view. From the practical point of view, the main purpose of the monoid-subclasses package is similar to that of ListLike - to provide unifying abstractions for various monoidal data types in Haskell, primarily String, ByteString, and Text. All three types are already instances of the Monoid class. While that abstraction is useful for building sequences of data, it doesn't help with deconstructing them.

That being said, there are two major differences in the goals of ListLike and monoid-subclasses:

  • ListLike strives to reproduce the standard Data.List interface, whereas monoid-subclasses builds from deeper theoretical foundations; and
  • The monoid-subclasses implementation uses standard Haskell 2010, with the exception of two minor extensions which can be worked around if necessary.

The incremental-parser package provides one example of use of monoid-subclasses. Another example is picoparsec, a fork of attoparsec.

A more thorough description of the library can be found in the Haskell Symposium 2013 paper Adding Structure to Monoids