[Haskell-beginners] Semigroup Instances

Atrudyjane atrudyjane at protonmail.com
Thu Jan 26 21:55:12 UTC 2017


I'm currently studying semigroups and trying to figure out how to determine which type variables need a semigroup instance. Here are a couple of examples from Evan Cameron's github (https://github.com/leshow/haskell-programming-book/blob/master/src/Ch15ex.hs):
(1)
data Validation a b
= Failure a
| Success b
deriving (Eq, Show)

instance Semigroup a => Semigroup (Validation a b) where
Success a <> Success b = Success a
Failure a <> Success b = Success b
Success a <> Failure b = Success a
Failure a <> Failure b = Failure (a <> b)

* Why doesn't 'b' need an instance of semigroup?
(2)
newtype AccumulateRight a b = AccumulateRight (Validation a b) deriving (Eq, Show)

instance Semigroup b => Semigroup (AccumulateRight a b) where
AccumulateRight (Success a) <>AccumulateRight (Failure b) =AccumulateRight (Success a)
AccumulateRight (Failure a) <>AccumulateRight (Success b) =AccumulateRight (Success b)
AccumulateRight (Failure a) <>AccumulateRight (Failure b) =AccumulateRight (Failure a)


AccumulateRight (Success a) <> AccumulateRight (Success b) = AccumulateRight (Success (a <> b))

* Why doesn't 'a' need an instance of semigroup?


Thank you,
Andrea





Sent with [ProtonMail](https://protonmail.com) Secure Email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170126/bd92bf53/attachment-0001.html>


More information about the Beginners mailing list