[Haskell-beginners] Why do class instance functions behave differently when executed directly in WinGHCi than when loaded from a file?

Costello, Roger L. costello at mitre.org
Fri Jun 10 12:53:30 CEST 2011


Hi Folks,

I created a file, and in the file I entered this type class and an instance:

    class MyEqualityOperator a where
        isEqual :: a -> a -> Bool

    instance MyEqualityOperator Double where
        isEqual x y = x == y

To test the isEqual function I entered this:

    test = isEqual  3 4

When I loaded my file into WinGHCi, I got this error:

----------------------------------------------------------------------------
    Ambiguous type variable `t' in the constraints:
      `Num t'
        arising from the literal `3' at simpleTypeClassTest.hs:11:14
      `MyEqualityOperator t'
        arising from a use of `isEqual' at simpleTypeClassTest.hs:11:5-16
    Probable fix: add a type signature that fixes these type variable(s)
----------------------------------------------------------------------------

Why is it "ambiguous"?

Interestingly, when I typed this at the WinGHCi command prompt:

    isEqual  3 4

I got no error and I got the expected result (False).

Why is this? 

What would I need to do, in my file, to make this work properly: 

    test = isEqual  3 4


/Roger





More information about the Beginners mailing list