[Haskell-cafe] More newbie typeclass confusion...

alex maskif at yahoo.com.au
Sat Dec 29 22:31:51 EST 2007

Hi there.

If someone can tell me why I am getting type ambiguity
in the following code:

    class (Ord s, Num s) => Scalar s where
        zero :: s

    class Metric m where
        delta   :: Scalar s => m -> m -> s

        (=~)    :: m -> m -> Bool
        (/~)    :: m -> m -> Bool

        (=~) a b    = (delta a b) <= zero
        (/~) a b    = not (a =~ b)

I will scream.

The error I get compiling is:

    Ambiguous type variable `s' in the constraint:
      `Scalar s' arising from a use of `delta' at
    Probable fix: add a type signature that fixes
these type variable(s)

If I change "Scalar" to "Num", and "zero" to "0"
within the class "Metric", then it works.

I don't get it. "zero" is defined in the "Scalar"
class, and "delta" returns a Scalar, and Scalar
derives "Ord", so where's the ambiguity?

Thanks. I will understand Haskell eventually... =)

      Make the switch to the world's best email. Get the new Yahoo!7 Mail now. www.yahoo7.com.au/worldsbestemail

More information about the Haskell-Cafe mailing list