1

I thought that a algebraic structure similar to the composition of a function was a Monoid's unique character not Monad.

To compose (a -> m b) (in other words, To make Monoid) something which requires unit and return is Monad. so I thought that all Monad is also Monoid.

if I am wrong, could show me an example of Monad that is not Monoid.

  • 3
    It depends on your definition of monad and monoid. If you're talking about the Haskell typeclasses called `Monad` and `Monoid`, no, not all `Monad`s are instances of `Monoid`. The `ST` monad is one example. Instances of `Monad` have kind `Type -> Type`, while instances of `Monoid` have kind `Type`. – 4castle Feb 05 '22 at 15:44
  • 1
    Every monoid can be *lifted* by an `Applicative`, since all `Monad`s are `Applicative`s this seems relevant to your question. For example functions `(->) a`, `IO`, tuples `(,) a`, `Identity` are all lifted in this way. This lifting has a name [`Ap`](https://hackage.haskell.org/package/base-4.16.0.0/docs/Data-Monoid.html#t:Ap) and resides in `Data.Monoid`. If you have an `Applicative F` you can derive the instances via: `data F a = .. deriving (Semigroup, Monoid) via Ap F a`. – Iceland_jack Feb 05 '22 at 19:25
  • 3
    When you say "to compose .. to make a monoid" you have to distinguish: `Monoid`s are **untyped** composition, [`Category`-ies](https://hackage.haskell.org/package/base-4.16.0.0/docs/Control-Category.html) are **typed** composition. In this sense, monadic functions `a -> m b` ([`Kleisli m a b`](https://hackage.haskell.org/package/base-4.16.0.0/docs/Control-Arrow.html#t:Kleisli)) are indeed a `Category (Kleisli m)` and can be composed. If you restrict the `a ~ b` to be the same `a -> m a` you can define a `Monoid` for any `Monad`. – Iceland_jack Feb 05 '22 at 19:28
  • 1
    I think you're referring to something like [EndoM](https://hackage.haskell.org/package/foldl-1.4.12/docs/src/Control.Foldl.html#line-1376), which forms a Monoid for any choice of Monad. `pure` is `mempty` and `<=<` is `<>`. However, it only works for functions of type `a -> m a`, not `a -> m b`. – Steven Fontanella Feb 05 '22 at 22:22

0 Answers0