<div dir="auto">As far as I'm aware, distributivity is neither required nor adhered to by many types in core libraries and beyond. Only left and right identity, and associativity. This instance would also play well with the existing Applicative instance.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Mar 21, 2020, 3:54 PM Oleg Grenrus <<a href="mailto:oleg.grenrus@iki.fi">oleg.grenrus@iki.fi</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><a href="https://wiki.haskell.org/Typeclassopedia#Laws_6" rel="noreferrer noreferrer" target="_blank">https://wiki.haskell.org/Typeclassopedia#Laws_6</a><br>
<br>
On 21.3.2020 23.46, Simon Jakobi via Libraries wrote:<br>
> Hi Zemyla,<br>
><br>
> Could you clarify where the requirement for distributivity comes from?<br>
><br>
> The haddocks for Alternative don't mention it:<br>
> <a href="http://downloads.haskell.org/~ghc/8.10.1-rc1/docs/html/libraries/base-4.14.0.0/Control-Applicative.html#t:Alternative" rel="noreferrer noreferrer" target="_blank">http://downloads.haskell.org/~ghc/8.10.1-rc1/docs/html/libraries/base-4.14.0.0/Control-Applicative.html#t:Alternative</a><br>
><br>
> Thanks,<br>
> Simon<br>
><br>
> Am Sa., 21. März 2020 um 21:23 Uhr schrieb Zemyla <<a href="mailto:zemyla@gmail.com" target="_blank" rel="noreferrer">zemyla@gmail.com</a>>:<br>
>> No, the Alternative instance should be based on semirings instead. The one you propose isn't distributive.<br>
>><br>
>> Lacking general semirings, the best we can do at the moment is newtypes for each semiring we have. For instance:<br>
>><br>
>> newtype NumConst a b = NumConst { getNumConst :: a }<br>
>>    deriving (Functor)<br>
>><br>
>> instance Num a => Applicative (NumConst a) where<br>
>>    pure = const (NumConst 1)<br>
>>    (<*>) = (coerce :: (a -> a -> a) -> NumConst a (u -> v) -> NumConst a u -> NumConst a v) (*)<br>
>><br>
>> instance Num a => Alternative (NumConst a) where<br>
>>    empty = NumConst 0<br>
>>    (<|>) = (coerce :: (a -> a -> a) -> NumConst a b -> NumConst a b -> NumConst a b) (+)<br>
>><br>
>> On Sat, Mar 21, 2020, 13:44 chessai . <<a href="mailto:chessai1996@gmail.com" target="_blank" rel="noreferrer">chessai1996@gmail.com</a>> wrote:<br>
>>> We already have<br>
>>><br>
>>> instance Monoid m => Applicative (Const m)<br>
>>><br>
>>> we could easily add<br>
>>><br>
>>> instance Monoid m => Alternative (Const m) where<br>
>>>    empty = coerce mempty<br>
>>>    (<|>) = coerce (<>)<br>
>>><br>
>>> which trivially satisfies left/right identity and associativity.<br>
>>><br>
>>> I propose we add this instance to base.<br>
>>> _______________________________________________<br>
>>> Libraries mailing list<br>
>>> <a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
>>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
>> _______________________________________________<br>
>> Libraries mailing list<br>
>> <a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
>> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
> _______________________________________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
_______________________________________________<br>
Libraries mailing list<br>
<a href="mailto:Libraries@haskell.org" target="_blank" rel="noreferrer">Libraries@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
</blockquote></div>