[Haskell-cafe] Endo a, endomorphisms

Adrian metabind at protonmail.ch
Sun Dec 5 14:02:09 UTC 2021


In the Data.Monoid library there is a monoid type instance for Endo a that is described in the following manner:

> The monoid of endomorphisms under composition.

I understand how it is a monoid of endofunctions, but am unclear on how it is a monoid of endomorphisms.
In the pattern: "forms a monoid of X under Y", X refers to the set and Y to the associative binary operation.
So it seems that X in this case is the set of endofunctions. However, it might be possible that the intention was
to state that function composition is an endomorphism of the set of endofunctions (Endo a). However even
this statement seems difficult to prove in Haskell:

opening :: String -> String
opening = ("Hello, " ++)

closing :: String -> String

closing = (++ "!")

eo = Endo opening

ec = Endo closing

appEndo (Endo $ opening <> closing) "Haskell"

-- "Hello, HaskellHaskell!"

(appEndo eo <> appEndo ec) "Haskell"

-- "Hello, HaskellHaskell!"

appEndo (eo <> ec) "Haskell

-- "Hello, Haskell!"

(opening <> closing) "Haskell"

-- "Hello, HaskellHaskell!"

(opening . closing) "Haskell"

-- "Hello, Haskell!"

I believe the reason for this behavior is that the fallback for mappend for the general endofunction with type a -> b
is found in the Base library as:

-- @since 4.9.0.0
instance Semigroup b => Semigroup (a -> b) where
f <> g = \x -> f x <> g x
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20211205/f52596a3/attachment.html>


More information about the Haskell-Cafe mailing list