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

Jeffrey Brown jeffbrown.the at gmail.com
Thu Jan 22 00:23:09 UTC 2015


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150121/d62f9bbf/attachment.html>


More information about the Beginners mailing list