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.Why is that sweet?Thing is, you can already write a very genericthat would do the trick.So you're hoping to write a Rev type class with singleton membergiven D, returns B,given C, returns er, CYou'd like a function thatdata A = A | B | C | D | ESuppose you have:Here's what I'm seeing:Jeffrey,You didn't explain what you're trying to accomplish, and therefore folks can only address the symptoms.
given A returns E,
given B, returns D
given E, returns A.
rev :: Rev a => a -> a
rev :: (Enum a, Bounded a) => a -> a
that would do the job.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-EeOn Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown <jeffbrown.the@gmail.com> 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
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners