[Haskell-beginners] Defining an instance: Syntax that works exactly sometimes

Brandon Allbery allbery.b at gmail.com
Thu Jan 22 00:32:38 UTC 2015

On Wed, Jan 21, 2015 at 7:23 PM, Jeffrey Brown <jeffbrown.the at gmail.com>

> 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

You derived Ord; why are you doing this? You are locally overriding the Ord
instance you derived with a (<=) that is *not* the one that is part of
Ord.. and will therefore not contribute to the other functions from Ord.

If you want to implement an instance manually, use the instance keyword.

    instance Ord Dim where
      (<=) Succ Pred = False
      (<=) _ _ = True

The same applies to Rev later, but in that case it's redefining something
defined in the same module and errors out.

brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150121/142ad7a4/attachment.html>

More information about the Beginners mailing list