Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Defining an instance: Syntax that works exactly
sometimes (Kim-Ee Yeoh)
2. Re: Defining an instance: Syntax that works exactly
sometimes (Jeffrey Brown)
3. Re: Defining an instance: Syntax that works exactly
sometimes (Brandon Allbery)
4. Re: Defining an instance: Syntax that works exactly
sometimes (Jeffrey Brown)
----------------------------------------------------------------------
Message: 1
Date: Thu, 22 Jan 2015 23:57:59 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<CAPY+ZdThK_X13Bfwrw4vUhUXe2bhgbJbGK=p0zuf7syqwht...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Jeffrey,
You didn't explain what you're trying to accomplish, and therefore folks
can only address the symptoms.
Here's what I'm seeing:
Suppose you have:
data A = A | B | C | D | E
You'd like a function that
given A returns E,
given B, returns D
given C, returns er, C
given D, returns B,
given E, returns A.
So you're hoping to write a Rev type class with singleton member
rev :: Rev a => a -> a
that would do the trick.
Thing is, you can already write a very generic
rev :: (Enum a, Bounded a) => a -> a
that would do the job.
Why is that sweet?
Because Enum + Bounded are auto-derivable for huge swathes of data types.
So you write the rev function (not type class instance!) once, and it'll
work for lots of your structures.
The best type class is often no type class. Especially when the compiler
isn't smart enough to read your mind. But I'm sure it's flattered you
thought so highly of it ;)
-- Kim-Ee
On Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown <[email protected]>
wrote:
> Dear Haskellers,
>
> The following compiles. (Rev stands for Reversible, and Dirn for
> Direction.)
>
> class Rev a where
> rev :: a -> a
>
> data Dirn = Succ | Pred
> deriving (Eq, Show, Ord)
>
> -- implement Ord
> (<=) Succ Pred = False
> (<=) _ _ = True
>
> -- implement Rev
> instance Rev Dirn where
> rev Succ = Pred
> rev Pred = Succ
>
> But if I try to define the Rev instance the same way the Ord instance is
> being defined, it does not compile:
>
> class Rev a where
> rev :: a -> a
>
> data Dirn = Succ | Pred
> deriving (Eq, Show, Ord, Rev)
>
> -- implement Ord, because Dirn is used as a key in a Map
> (<=) Succ Pred = False
> (<=) _ _ = True
>
> -- implement Rev
> rev Succ = Pred
> rev Pred = Succ
>
> What's going on?
>
> Many thanks,
> Jeff
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150122/c6e9d0d8/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 22 Jan 2015 19:34:15 -0800
From: Jeffrey Brown <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<caec4ma1mh+zv-a+xo2jab53omv+u0ltozvdfx-qfod1ivap...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thank you, everybody. I had thought "deriving" and "instance" were just
different words for the same thing. Your answers were deeper than I am
currently capable of understanding.
I found instance declarations specified in 7.6.3 of The Glorious Glasgow
Haskell Compilation System User's Guide, Version 7.0.2:
https://downloads.haskell.org/~ghc/7.0.2/docs/html/users_guide/type-class-extensions.html
I have not found the "deriving" keyword's specification anywhere.
On Thu, Jan 22, 2015 at 8:57 AM, Kim-Ee Yeoh <[email protected]> wrote:
> Jeffrey,
>
> You didn't explain what you're trying to accomplish, and therefore folks
> can only address the symptoms.
>
> Here's what I'm seeing:
>
> Suppose you have:
>
> data A = A | B | C | D | E
>
> You'd like a function that
> given A returns E,
> given B, returns D
> given C, returns er, C
> given D, returns B,
> given E, returns A.
>
> So you're hoping to write a Rev type class with singleton member
> rev :: Rev a => a -> a
> that would do the trick.
>
> Thing is, you can already write a very generic
>
> rev :: (Enum a, Bounded a) => a -> a
>
> that would do the job.
>
> Why is that sweet?
>
> Because Enum + Bounded are auto-derivable for huge swathes of data types.
> So you write the rev function (not type class instance!) once, and it'll
> work for lots of your structures.
>
> The best type class is often no type class. Especially when the compiler
> isn't smart enough to read your mind. But I'm sure it's flattered you
> thought so highly of it ;)
>
>
> -- Kim-Ee
>
> On Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown <[email protected]>
> wrote:
>
>> Dear Haskellers,
>>
>> The following compiles. (Rev stands for Reversible, and Dirn for
>> Direction.)
>>
>> class Rev a where
>> rev :: a -> a
>>
>> data Dirn = Succ | Pred
>> deriving (Eq, Show, Ord)
>>
>> -- implement Ord
>> (<=) Succ Pred = False
>> (<=) _ _ = True
>>
>> -- implement Rev
>> instance Rev Dirn where
>> rev Succ = Pred
>> rev Pred = Succ
>>
>> But if I try to define the Rev instance the same way the Ord instance is
>> being defined, it does not compile:
>>
>> class Rev a where
>> rev :: a -> a
>>
>> data Dirn = Succ | Pred
>> deriving (Eq, Show, Ord, Rev)
>>
>> -- implement Ord, because Dirn is used as a key in a Map
>> (<=) Succ Pred = False
>> (<=) _ _ = True
>>
>> -- implement Rev
>> rev Succ = Pred
>> rev Pred = Succ
>>
>> What's going on?
>>
>> Many thanks,
>> Jeff
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150122/51ca811d/attachment-0001.html>
------------------------------
Message: 3
Date: Thu, 22 Jan 2015 22:40:46 -0500
From: Brandon Allbery <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<CAKFCL4V+XnAsp4CYrpF_N0tz=cNJ4MivyY5VP9tnnBqTJs=k...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown <[email protected]>
wrote:
> I have not found the "deriving" keyword's specification anywhere.
The correct place to look for this and other things, including instances,
is the Haskell Report.
https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3
4.3.3. Derived Instances
https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2
4.3.2. Instance Declarations
--
brandon s allbery kf8nh sine nomine associates
[email protected] [email protected]
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150122/7f4796fb/attachment-0001.html>
------------------------------
Message: 4
Date: Thu, 22 Jan 2015 20:52:31 -0800
From: Jeffrey Brown <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Defining an instance: Syntax that
works exactly sometimes
Message-ID:
<CAEc4Ma0gL_hxur_1ef7Y7N2ku9H+NK=da7x-6sh2n8ef8gy...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks (again) Brandon!
On Thu, Jan 22, 2015 at 7:40 PM, Brandon Allbery <[email protected]>
wrote:
> On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown <[email protected]>
> wrote:
>
>> I have not found the "deriving" keyword's specification anywhere.
>
>
> The correct place to look for this and other things, including instances,
> is the Haskell Report.
>
>
> https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3
> 4.3.3. Derived Instances
>
>
> https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2
> 4.3.2. Instance Declarations
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> [email protected]
> [email protected]
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20150122/da291013/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 79, Issue 25
*****************************************