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

박신환 ndospark320 at naver.com
Wed May 2 14:31:55 UTC 2018


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
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20180502/218ad535/attachment.html>


More information about the Libraries mailing list