[Haskell-cafe] instance Monoid a => Monad ((,) a)

Jinxuan Zhu zhujinxuan at gmail.com
Mon Aug 5 20:33:33 UTC 2019


Hi, I think the error message says there is no Monoid for Bool.  It is
because Bool can be monoid by either || or && operations, which would lead
to ambiguity if Bool is monoid by default.

You can:
1. use Maybe Unit instead
2. (overkill) Define AndMonoid Bool newtype and use DeriveVia and coerce



On Fri, Aug 2, 2019 at 7:39 PM Benjamin Franksen <ben.franksen at online.de>
wrote:

> I wanted to define a simple Monad instance for (Bool,) as
>
> instance Monad ((,) Bool) where
>   return x = (False, x)
>   (True, x) >>= k = (True, snd (k x))
>   (False, x) >>= k = k x
>   -- or: (b, x) >>= k = let (c, y) = k x in (b||c, y)
>
> The idea was to keep track of whether functions change their input value
> or not.
>
> To my astonishment I found that this definition overlaps with an
> existing one. GHC tells me
>
> x.hs:2:10: error:
>     • No instance for (Monoid Bool)
>         arising from the superclasses of an instance declaration
>     • In the instance declaration for ‘Monad ((,) Bool)’
>   |
> 2 | instance Monad ((,) Bool) where
>   |          ^^^^^^^^^^^^^^^^
>
> x.hs:2:10: error:
>     • Overlapping instances for Monad ((,) Bool)
>         arising from a use of ‘GHC.Base.$dm>>’
>       Matching instances:
>         instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’
>         instance Monad ((,) Bool) -- Defined at x.hs:2:10
>     • In the expression: GHC.Base.$dm>> @((,) Bool)
>       In an equation for ‘>>’: (>>) = GHC.Base.$dm>> @((,) Bool)
>       In the instance declaration for ‘Monad ((,) Bool)’
>   |
> 2 | instance Monad ((,) Bool) where
>   |          ^^^^^^^^^^^^^^^^
>
> [one more similar overlap error elided]
>
> But I could not find the
>
>   instance Monoid a => Monad ((,) a)
>
> documented anywhere in the base package. Should I look harder? Or is
> this instance accidentally leaking from GHC.Base?
>
> Eventually, through some experimenting I found that if I replace Bool
> with Any, then the existing instance seems to do what I want.
>
> Cheers
> Ben
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190805/73ef3a5f/attachment.html>


More information about the Haskell-Cafe mailing list