[Haskell] Annoying naming clashes

Tom Pledger tpledger at ihug.co.nz
Wed Jun 16 06:04:37 EDT 2004


John Meacham wrote:

>[...]
>
>I find trying to draw analogies between haskell classes and constructs
>in other languages to be problematic as people then try to apply
>knowledge from other fields incorrectly to haskell unless you give a
>full explanation of haskell classes anyway.. but YMMV. 
>        John
>

In the particular case of Haskell classes and Java interfaces, I like 
the analogy *provided that* the Haskell class in question doesn't have a 
method with two occurrences of the class variable in parameter positions.

For example,

    class Set s where
        empty  :: (a -> a -> Ordering) -> s a
        insert :: s a -> a -> s a
        member :: s a -> a -> Bool


fits nicely (until you add a union method), but Eq does not.

The classes which fit the Java interface analogy are also the classes 
which can be rendered as explicit dictionaries instead:

    module Set where
    data Set a
        = Set {insert :: a -> Set a,
               member :: a -> Bool}

    module RedBlack(empty) where
    import Set
    empty :: (a -> a -> Ordering) -> Set a
    empty cmp = ...


Importantly (IMHO), if you use an explicit dictionary instead of a 
class, the 'heterogeneous list of class instances' problem goes away.

Regards,
Tom




More information about the Haskell mailing list