Equality for algebraic types

Ron Wichers Schreur ronny@cs.kun.nl
Wed, 5 Nov 1997 16:48:29 +0100


Marco de Wit wrote to the Clean mailing list:

[HTML codes removed (please use plain text on this list)]

> And I have a question also:
> 
> about the program:
> 
> module TestProg
> 
> import StdEnv
> 
> ::Alg=Aa|Bb|Cc
> 
> function::Alg->Bool
> function a
> |a==Aa = True
> |otherwise = False
> 
> Start=function Aa
> 
> This program won't compile because '==' isn't defined for the type Alg
> What should I do about this, except rebuilding the function to 
> pattern-matching ?

In the current version of Clean you have to define the instance for
algebraic types yourself:

instance == Alg where
    (==) Aa Aa = True
    (==) Bb Bb = True
    (==) Cc Cc = True
    (==) _  _  = False

This can get cumbersome if your type has a large number of constructors
(for example an enumeration type).

> Shouldn't '<>' and '==' be predefined for algebraic types in Clean?

I don't think that these instances should be predefined for all
algebraic types. Haskell has the 'deriving' construct, with you
can use to derive the definition above. It would be nice to have
this in Clean too.


Cheers,

Ronny Wichers Schreur