Add newtype for Alternative using QuantifiedConstraints in base-4.13

Wolfgang Jeltsch wolfgang-it at jeltsch.info
Mon Feb 18 20:19:13 UTC 2019


Am Freitag, den 15.02.2019, 19:04 -0500 schrieb theindigamer:
> Thoughts on adding a newtype with the following Alternative instance?
> (The name chosen is a placeholder, I don't have any particular
> preference).
>
>     newtype Mon f a = Mon { unMon :: f a }
>
>     instance (forall a. Monoid (f a)) => Alternative (Mon f) where
>
>         empty = Mon mempty
>
>         (<|>) (Mon m1) (Mon m2) = Mon (m1 <> m2)

If I remember correctly, then something that is an `Applicative` and a
`Monoid` isn’t necessarily an `Alternative` and something that is a
`Monad` and a `Monoid` isn’t necessarily a `MonadPlus`. There are
certain axioms that are supposed to hold, which link the `Applicative`
or `Monad` structure to the `Monoid` structure. At least this is how it
ought to be. For details see the paper [“From Monoids to Near-Semirings:
The Essence of `MonadPlus` and `Alternative`”][*].

All the best,
Wolfgang

[*]:
    https://usuarios.fceia.unr.edu.ar/~mauro/pubs/FromMonoidstoNearsemirings.pdf
    "From Monoids to Near-Semirings: The Essence of `MonadPlus` and `Alternative`"


More information about the Libraries mailing list