<div dir="ltr"><div><div><div><div><div><div><div><div><div><div><div><div>Jeffrey,<br><br></div>You didn't explain what you're trying to accomplish, and therefore folks can only address the symptoms.<br><br></div>Here's what I'm seeing:<br><br></div>Suppose you have:<br><br></div>    data A = A | B | C | D | E<br><br></div>You'd like a function that <br>given A returns E, <br>given B, returns D<br></div>given C, returns er, C<br></div>given D, returns B,<br>given E, returns A.<br><br></div>So you're hoping to write a Rev type class with singleton member <br>    rev :: Rev a => a -> a<br></div>that would do the trick.<br><br></div>Thing is, you can already write a very generic <br><br>    rev :: (Enum a, Bounded a) => a -> a<br><br>that would do the job.<br><br></div>Why is that sweet?<br><br></div>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.<br><div><div><br><div><div><div>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  ;)<br></div><div><br></div></div></div></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature">-- Kim-Ee</div></div>
<br><div class="gmail_quote">On Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown <span dir="ltr"><<a href="mailto:jeffbrown.the@gmail.com" target="_blank">jeffbrown.the@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear Haskellers,<br><br>The following compiles. (Rev stands for Reversible, and Dirn for Direction.)<br><br>    class Rev a where<br>        rev :: a -> a<br>    <br>    data Dirn = Succ | Pred<br>        deriving (Eq, Show, Ord)<br><br>    -- implement Ord<br>    (<=) Succ Pred = False<br>    (<=) _ _ = True<br>    <br>    -- implement Rev<br>    instance Rev Dirn where<br>        rev Succ = Pred<br>        rev Pred = Succ<br><br>But if I try to define the Rev instance the same way the Ord instance is being defined, it does not compile:<br><br>    class Rev a where<br>        rev :: a -> a<br>    <br>    data Dirn = Succ | Pred<br>        deriving (Eq, Show, Ord, Rev)<br>    <br>    -- implement Ord, because Dirn is used as a key in a Map<br>    (<=) Succ Pred = False<br>    (<=) _ _ = True<br>    <br>    -- implement Rev<br>    rev Succ = Pred<br>    rev Pred = Succ<br><br>What's going on?<br><br>Many thanks,<br>Jeff<br><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>