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

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Sep 13 15:19:33 CEST 2011


The problem is that an implementation of (===) must return a result of
type b for *any* type b which is an instance of Boolean.  For example,
if I made

  data X = A | B

  instance Boolean X where
    ...

then I would be able to have:

  eqX :: MyEq a => a -> a -> X
  eqX = (===)

That's all fine and probably the reason why you are going through
these hoops.  However, look at your definition for MyEq Bool:

  a === b  =  a == b

Now, "a == b" has type Bool.  What would happen if I wanted an X instead?

You would need something like

  class Boolean a where
    (/\) :: a -> a -> a
    fromBool :: Bool -> a

Your instance would then become

  instance MyEq Bool where
    a === b  =  fromBool (a == b)

You may want to take a look at the Awesome Prelude [1], which
implement this and much more.

Cheers! =)

[1] https://github.com/tomlokhorst/AwesomePrelude

-- 
Felipe.



More information about the Haskell-Cafe mailing list