Firstly, let us go over the definition of free monoids. I have never quite understood how is it possible to define a free monoid as a structure which abides by monoid laws and nothing else. How do we prove that something abides by no rules but stated above? Or is this just an intuition?
Let me illustrate the purpose of free monoids.
If I tell you there is a monoid, with some elements a
, b
, c
, what can you deduce from that?
- We can find more elements of that monoid by writing expressions involving the generators
a
, b
, c
and the monoid operations (+)
and 0
(aka. (<>)
and mempty
). (cf. Definition 1, in the second half of this answer.)
- We can use the monoid laws to prove that some expressions denote the same element: we can prove equations such as
((a + 0) + b) = (a + b)
. (Definition 2.) In fact, equations we can prove with just that knowledge are equations which hold in any monoid, for any values a
, b
, c
. (Theorem 1.)
What about equations we can't prove from just the monoid laws? For example, we can't prove (a + b) = (b + a)
. But we can't prove its negation either, (a + b) /= (b + a)
, if we only know the monoid laws. What does that mean? It turns out that that equation holds in some monoids (e.g., commutative monoids), but not in others: for example, pick a monoid where x + y = y
for almost all x
and y
(this is the Last
monoid in Haskell), if we choose distinct a
and b
, then (a + b) /= (b + a)
.
But that was just one example. What can we say in general about equations that we cannot prove from just the monoid laws? The free monoid offers a definitive answer, in fact, a universal counterexample: unprovable equations are false in the free monoid (generated by a
, b
, c
). In other words, we can prove an equation e = f
using just the monoid laws if and only if it is true in the free monoid (emphasis on "if"). (Theorem 2.) This corresponds to the intuition that the free monoid "only abides by the monoid laws and nothing else".
So, does this functor make Last a free monoid? More generally, if there is a law-abiding instance for Monoid (T a), is T a free monoid?
The Last
monoid is not free because it makes more equations true than what you can actually prove purely from the monoid laws. See other answer:
forall (t :: Type) (x, y :: t).
Last (Just x) <> Last (Just y) === Last (Just y)
Here's a sketch of how to formalize the above.
Definition 1. The set of monoidal expressions generated by (some atomic symbols) A
, B
, C
is defined by the grammar:
e ::=
| A | B | C -- generators
| e + e -- binary operation (<>)
| 0 -- identity (mempty)
Given any "suitable monoid", that is to say, a monoid (M, (+), 0)
with some chosen elements a
, b
, c
in M
(which don't have to be distinct), an expression e
denotes an element eval e
in M
.
Definition 2. An equation is a pair of expressions, written e ~ f
. The set of provable equations is the smallest set of equations ("smallest" when ordered by inclusion) satisfying the following:
- It includes the monoid laws:
(e + 0) ~ e
, (0 + e) ~ e
, ((e + f) + g) ~ (e + (f + g))
are provable.
- It is an equivalence relation (viewing a set of tuples as a relation): for example, for reflexivity,
e ~ e
is provable.
- It is a congruence relation: if
e ~ f
is provable then (g + e) ~ (g + f)
and (e + g) ~ (f + g)
are provable.
(The idea of that definition is that the assertion "e ~ f
is provable" holds if and only if it can be deduced by "applying" those rules. "Smallest set" is a conventional method to formalize that.)
The definition of "provable equations" may seem arbitrary. Are those the right rules to define "provability"? Why these three rules in particular? Notably, the congruence rule may not be obvious in a first attempt at giving such a definition. This is the point of the following theorems, soundness and completeness. Add a (non-redundant) rule, and we lose soundness. Remove a rule, and we lose completeness.
Theorem 1. (Soundness) If e ~ f
is provable, then eval e = eval f
in any "suitable monoid" M
.
Theorem 2. (Completeness) If e ~ f
is not provable, then their denotations differ in F
, eval e /= eval f
, where F
is the free monoid generated by A
, B
, C
.
(Soundness is much easier to prove than completeness. Exercises for the reader.)
This completeness theorem is a characterization of the free monoid: any other monoid F
which keeps the statement of the theorem true is isomorphic to the free monoid (technically, this requires both completeness and an assumption that the denotation function eval : Expr -> M
is surjective). That is why we may say "the free monoid" instead of "the monoid of lists"; that practice is most accurate in contexts where the representation does not matter ("up to isomorphism").
In fact, completeness is trivial if you define "the free monoid" as the quotient of monoidal expressions by the equivalence relation "_ ~ _
is provable". The hard work actually resides in a separate proof, that this monoid is isomorphic to the monoid of lists.