-------- Original Message --------Subject: Re: [Haskell-beginners] Semigroup InstancesLocal Time: February 6, 2017 5:56 PMUTC Time: February 6, 2017 11:56 PMFrom: tanuki@gmail.comTo: Atrudyjane <atrudyjane@protonmail.com>, The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@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) whereSuccess x <> Success y = Success xFailure x <> Success y = Success ySuccess x <> Failure y = Success xFailure x <> Failure y = Failure (x <> y)On Thu, Jan 26, 2017 at 1:55 PM, Atrudyjane <atrudyjane@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 bderiving (Eq, Show)instance Semigroup a => Semigroup (Validation a b) whereSuccess a <> Success b = Success aFailure a <> Success b = Success bSuccess a <> Failure b = Success aFailure 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) whereAccumulateRight (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,AndreaSent with ProtonMail Secure Email._______________________________________________ Beginners mailing list