<div dir="auto">Bool is also a monoid under xor.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Aug 5, 2019, 4:34 PM Jinxuan Zhu <<a href="mailto:zhujinxuan@gmail.com">zhujinxuan@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div dir="ltr"><div>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.</div><div><br></div><div>You can:</div><div>1. use Maybe Unit instead<br></div><div>2. (overkill) Define AndMonoid Bool newtype and use DeriveVia and coerce</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Aug 2, 2019 at 7:39 PM Benjamin Franksen <<a href="mailto:ben.franksen@online.de" target="_blank" rel="noreferrer">ben.franksen@online.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I wanted to define a simple Monad instance for (Bool,) as<br>
<br>
instance Monad ((,) Bool) where<br>
return x = (False, x)<br>
(True, x) >>= k = (True, snd (k x))<br>
(False, x) >>= k = k x<br>
-- or: (b, x) >>= k = let (c, y) = k x in (b||c, y)<br>
<br>
The idea was to keep track of whether functions change their input value<br>
or not.<br>
<br>
To my astonishment I found that this definition overlaps with an<br>
existing one. GHC tells me<br>
<br>
x.hs:2:10: error:<br>
• No instance for (Monoid Bool)<br>
arising from the superclasses of an instance declaration<br>
• In the instance declaration for ‘Monad ((,) Bool)’<br>
|<br>
2 | instance Monad ((,) Bool) where<br>
| ^^^^^^^^^^^^^^^^<br>
<br>
x.hs:2:10: error:<br>
• Overlapping instances for Monad ((,) Bool)<br>
arising from a use of ‘GHC.Base.$dm>>’<br>
Matching instances:<br>
instance Monoid a => Monad ((,) a) -- Defined in ‘GHC.Base’<br>
instance Monad ((,) Bool) -- Defined at x.hs:2:10<br>
• In the expression: GHC.Base.$dm>> @((,) Bool)<br>
In an equation for ‘>>’: (>>) = GHC.Base.$dm>> @((,) Bool)<br>
In the instance declaration for ‘Monad ((,) Bool)’<br>
|<br>
2 | instance Monad ((,) Bool) where<br>
| ^^^^^^^^^^^^^^^^<br>
<br>
[one more similar overlap error elided]<br>
<br>
But I could not find the<br>
<br>
instance Monoid a => Monad ((,) a)<br>
<br>
documented anywhere in the base package. Should I look harder? Or is<br>
this instance accidentally leaking from GHC.Base?<br>
<br>
Eventually, through some experimenting I found that if I replace Bool<br>
with Any, then the existing instance seems to do what I want.<br>
<br>
Cheers<br>
Ben<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div></div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>