[Haskell-beginners] understanding type classes and class constraints

Robert Krahn robert.krahn at gmail.com
Mon Nov 18 00:47:58 UTC 2013


Hi everyone,

I'm currently enjoying to learn Haskell and came upon a problem I don't
understand:

Motivated by the Learn you a Haskell / yes-no typeclass example I tried to
create a simple "Tester" type class:

class Tester a where
    test :: a -> Bool

I wasn't to define the rules when test returns True/False for various
types, e.g.

instance Tester Integer where
    test 0 = False
    test _ = True

For the Maybe instance I want to delegate to the value of Just and add a
class constraint:

instance (Tester m) => Tester (Maybe m) where
    test Nothing = False
    test (Just x) = test x

It compiles nicely and works for Just values
test (Just 3) -- True
test (Just 0) -- False

But
test Nothing

gives me
    No instance for (Tester a0) arising from a use of `test'
    The type variable `a0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there are several potential instances:
      instance Tester m => Tester (Maybe m)
      instance Tester Integer
    In the expression: test Nothing

Could you please enlighten me what's going on and how to fix my code?

Thank you!
Robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131117/7bc98c74/attachment.html>


More information about the Beginners mailing list