[clean-list] How can I define an equality test ?

Pieter Koopman pieter at cs.ru.nl
Mon Nov 26 08:42:44 MET 2007


Hi Ferreira,

there are two ways to define equality for an algebraic data type:

1)  Define it yourself as Jerzy wrote:
At 11:13 PM 11/25/2007, jerzy.karczmarczuk at info.unicaen.fr wrote:
>Unless I am wrong, you cannot count on any intelligence of the compiler...
>(==) Monday Monday = True
>(==) Tuesday Tuesday = True
>...
>(==) _ _ = False   // otherwise

2) Using generics as Ben wrote:At 12:44 AM 11/26/2007, Ben Lippmeier wrote:
>Hi Ferreria
>I had the same problem some time ago. You can use the clean generics 
>library to derive the equality operator (===) that you want. See the 
>mail archive at: 
>http://mailman.science.ru.nl/pipermail/clean-list/2004/002702.html

This reads:

derive gEq Day
instance == Day where (==) x y = x === y

This implementation would be somewhat less efficient.
By the way, a simpler definition of your function isGood is:

isGood Mon = True
isGood _ = False

Have fun,

Pieter Koopman

At 04:49 PM 11/23/2007, Ferreira Maurizio wrote:
>Im trying to define the == operator
>with  an algebraic data type.
>How can I define it ?
>what have I to write i the following example, instead of ????
>Regards
>maurizio
>
>
>module test
>import StdEnv
>
>:: Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
>
>instance == Day
>where
>   (==) :: Day Day -> Bool
>   (==) x y = x ????? y   ////  what have I to write ?
>
>IsGood :: Day -> Bool
>IsGood x = x == Mon
>
>Start = IsGood Mon
>
>_______________________________________________
>clean-list mailing list
>clean-list at science.ru.nl
>http://mailman.science.ru.nl/mailman/listinfo/clean-list



More information about the clean-list mailing list