[Haskell-beginners] Semigroup Instances

Atrudyjane atrudyjane at protonmail.com
Tue Feb 7 03:53:49 UTC 2017


Thank you Theodore. Yes, changing the variable names makes it clearer. Also the fact that only failures are combined on the right hand side...

Regards,
Andrea



Sent with [ProtonMail](https://protonmail.com) Secure Email.


-------- Original Message --------
Subject: Re: [Haskell-beginners] Semigroup Instances
Local Time: February 6, 2017 5:56 PM
UTC Time: February 6, 2017 11:56 PM
From: tanuki at gmail.com
To: Atrudyjane <atrudyjane at protonmail.com>, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners at haskell.org>


Gmail put you in spam.

If you haven't figured this out since you asked -- it's a matter of confusing (IMO bad) variable names. Check the data definition:

data Validation a b
= Failure a
| Success b
deriving (Eq, Show)

Failures are always type a, and successes are always type b. The type variables used in the first line correspond to these. But in the definitions of (<>), they are just local values. The instance could be rewritten like so:

instance Semigroup a => Semigroup (Validation a b) where

Success x <> Success y = Success x
Failure x <> Success y = Success y
Success x <> Failure y = Success x
Failure x <> Failure y = Failure (x <> y)



On Thu, Jan 26, 2017 at 1:55 PM, Atrudyjane <atrudyjane at protonmail.com> wrote:

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.


_______________________________________________
Beginners mailing list
Beginners at haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170206/df657ab2/attachment.html>


More information about the Beginners mailing list