I was recently playing with monoids in Standard ML. The signature is easy to write:
signature MONOID =
sig
type monoid
val neutral : monoid
val combine : monoid -> monoid -> monoid
end
And so are simple monoids, such as an integer addition monoid:
structure IntSumMonoid : MONOID =
struct
type monoid = int
val neutral = 0
fun combine a b = a + b
end
But I got stuck at defining a monoid for a higher-kinded type, such as list
. This won't compile, of course:
structure ListMonoid : MONOID =
struct
type monoid = 'a list
val neutral = []
fun combine a b = a @ b
end
After some searching I've found the following solution, based on a functor:
functor ListMonoid (type m) : MONOID =
struct
type monoid = m list
val neutral = []
fun combine a b = a @ b
end
My question is whether there exists an alternative for declaring a generic list monoid, ideally one that doesn't require a functor
declaration. It's obviously unnecessary to know the contained type of a list when trying to declare a list monoid.