[Haskell-cafe] Can't resolve class dependences (making Boolean class)

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Tue Sep 13 15:16:40 CEST 2011


On 13 September 2011 23:04, Grigory Sarnitskiy <sargrigory at ya.ru> wrote:
> Hello! I'm trying to have a class for booleans called Boolean (the methods are not complete):
>
> class MyEq a where
>    (===) :: (Boolean b) => a -> a -> b

This means that === potentially returns _any_ instance of Boolean.

Let's say for example that I define "data MyBool = T | F" and make it
an instance of MyEq and Boolean.

That means that T === F must be able to return either a Bool, MyBool
or any other instance of Boolean that the _caller_ chooses.

If this is what you want, then I suggest that you do something like:

class (MyEq a) => Boolean a where
   (/\) :: a -> a -> a

    fromBool :: Bool -> a

Then the instance becomes:

instance  MyEq Bool where
    x === y = fromBool $ x==y

instance Boolean Bool where
   (/\) = (&&)

   fromBool = id

-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com



More information about the Haskell-Cafe mailing list