Day 90 - Monoids

December 2, 2018
Haskell Monoids

Monoids

This blog post has been in the making for a while as I didn’t fully understand Monoids to make a Blogpost on them.

What is a Monoid?

A Monoid is a datatype that takes 2 arguments and is governed by 2 laws:

2+(6+10) == (2+6)+10 == 16
OR ([6,3,7] ++ [4]) ++ [0,8] == [6,3,7] ++ ([4] ++ [0,8]) == [6,3,7,4,0,8]

It doesn’t matter where the parenthesis are, the results are the same.

Commutative: This is when a functions arguments are interchangeable and still yield the same result. This is not a Monoid law Some Monoids are commutative (Numeric addition and multiplication), but they are specific types of Monoids .

The Monoid typeclass has 3 expressions:

class Monoid m where
mempty :: m                       --The identity expression
mappend :: m -> m -> m            --The applicability expression
mconcat :: [m] -> m               --A concatenation expression which utilises the monoidic properties
mconcat = foldr mappend mempty

Semigroups

Semigroups are a superclass of Monoids. And are essentially the same as Monoid but without the identity law, and derives the <> method, which is analogous to mappend.

class Semigroup a where
  (<>) :: a -> a -> a

My confusion in this topic was due to the required inheritance of the Semigroup superclass in order to instantiate a Monoid (something which got implemented in a relatively new version of the GHC). And therefore the Monoid typeclass is constrained by the Semigroup supergroup.

This confusion also came from me not understanding how to read the typeclass definition:

Prelude> :i Monoid
class Semigroup a => Monoid a where

Which reads,a has the typeclass Monoid, if it also has an instance for Semigroup

An example for an alias of the Maybe a datatype :

data Optional a =
 Nada
 | Only a
 deriving (Eq, Show)

instance Monoid a => Monoid (Optional a) where
    mempty = Nada

    mappend (Only x) (Only y) = Only $ mappend x y
    mappend (Only x) Nada     = Only x
    mappend Nada     (Only y) = Only y
    mappend Nada     Nada     = Nada

instance Semigroup a => Semigroup (Optional a) where
    Only a <> Only b = Only $ a <> b

Day 107 - What do you call a typeclass from the 80's?

December 18, 2018
Haskell Functors

Day 102 - Katas but not yet Functors

December 14, 2018
Haskell Kata

Day 98 - ToDo List ft. Functor/Applicative

December 10, 2018
Haskell Testing
comments powered by Disqus