Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Semigroup Instances (Atrudyjane)


----------------------------------------------------------------------

Message: 1
Date: Mon, 06 Feb 2017 22:53:49 -0500
From: Atrudyjane <atrudyj...@protonmail.com>
To: Theodore Lief Gannon <tan...@gmail.com>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Semigroup Instances
Message-ID:
        
<L_mWusr09uq-dRFAN4GHMT3pm2WGk6pPRlPI1wPQ7Tk1L2evZz4_NA_OB2-JGC5mE_akUE3S575JtjNtmAv5bwVBzdqeTu_gkuTPAAFNdzE=@protonmail.com>
        
Content-Type: text/plain; charset="utf-8"

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: tan...@gmail.com
To: Atrudyjane <atrudyj...@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) 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 <atrudyj...@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@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-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 104, Issue 4
*****************************************

Reply via email to