Monoid over XOR or XNOR, and Monoid lifted by Applicative.

Andrew Martin andrew.thaddeus at gmail.com
Wed May 2 14:34:26 UTC 2018


I agree that the AMonoid type you describe is useful. In fact, it's going
to be in base when GHC 8.6 is released: https://github.com/ghc/ghc/pull/123

On Wed, May 2, 2018 at 10:31 AM, 박신환 <ndospark320 at naver.com> wrote:

> Logical XOR is associative, also is XNOR. And they both have an identity
> element.
>
>
>
> Hence:
>
>
>
> newtype Even = Even {getEven :: Bool}
>
> newtype Odd = Odd {getOdd :: Bool}
>
>
>
> instance Semigroup Even where
>
>     (<>) = (==) -- (==) over Bool == Logical XNOR
>
>
>
> instance Semigroup Odd where
>
>     (<>) = (/=) -- (/=) over Bool == Logical XOR
>
>
>
> instance Monoid Even where
>
>     mempty = True
>
>
>
> instance Monoid Odd where
>
>     mempty = False
>
>
>
> So foldMap would determine the parity of the number of Trues.
>
>
>
>
>
> Also, Monoid lifted by Applicative is also Monoid. This might be useful:
>
>
>
> newtype AMonoid f a = AMonoid {getAMonoid :: f a}
>
>
>
> instance (Applicative f, Semigroup a) => Semigroup (AMonoid f a) where
>
>     (<>) = liftA2 (<>)
>
>
>
> instance (Applicative f, Monoid a) => Monoid (AMonoid f a) where
>
>     mempty = pure mempty
>
>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
>


-- 
-Andrew Thaddeus Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20180502/484af53b/attachment.html>


More information about the Libraries mailing list