Is Last a free monoid?
Here's one way to understand a free monoid: If somebody gives you a value, how much can you deduce about how it was created? Consider an additive monoid of natural numbers. I give you a 7 and ask you how I got it. I could have added 4+3, or 3+4, or 2+5, etc. There are many possibilities. This information was lost. If, on the other hand, I give you a list [4, 3]
, you know it was created from singletons [4]
and [3]
. Except that maybe there was a unit []
involved. Maybe it was [4]<>[3]<>[]
or [4]<>[]<>[]<>[3]
. But it definitely wasn't [3]<>[4]
.
With a longer list, [1, 2, 3]
, you have additional options ([1]<>[2]) <> [3]
, or [1] <> ([2]<>[3])
, plus all possible insertions of the empty list. So the information you lose follows the unit laws and associativity, but nothing else. A free monoid value remembers how it was created, modulo unit laws and associativity.
For the sake of example, let's take non-negative Integer numbers, i.e. 0,1,2,...
. How many monoids can we make?
Defining mempty = 0
and (<>) = (+)
. You can proof easily that this is a monoid.
Defining mempty = 1
and (<>) = (*)
. Again, This is a monoid (Prove it, it is easy)
The two monoids defined above, are called additive and multiplicative monoids over Natural numbers. They are different in structure, for example, the element 0
in the multiplicative monoid, behaves totally different than any other element in the additive monoid, hence there is something inner to Natural numbers, that makes this monoids different (hold this assertion till the next paragraph).
There exists a third monoid we can create, let's call it concatenation monoid.
Defining mempty = no-action
and (<>) = glue one integer beside the other
.
As an example, 3 <> mempty = 3
and 3 <> 2 = 32
. Notice, that the fact that elements, are natural numbers is not relevant here. If instead of Natural, we take Rationals, or what ever symbols you like, the monoid would be exactly the same thing.(* read foot note) Hence, there is nothing inner to the underlying set that makes the monoid different to others. Thats why, the monoid is free because it doesn't depend on arithmetic rules of the Naturals, nor any other rule aside from monoid ones.
And this is the only way to build a monoid freely, not depending on the inner rules of the underlying set. Of course, concatenation is expressed as lists in haskell.
- Note: The only important bit is that they share the same number of elements. For example, the free monoid with 3 elements
a
,b
andc
would be any arbitrary concatenation of those three, but you can choose what ever symbol:1
,2
,3
orα
,β
,γ
... and the monoid would be the very same thing
Here is another law that Last
satisfies:
forall (t :: Type) (x, y :: t).
Last (Just x) <> Last (Just y) === Last (Just y)
Since it satisfies another law, it must not be the free Monoid.