[Haskell-beginners] multi-parameter typeclass with default implementation

TP paratribulations at free.fr
Mon Aug 19 22:28:14 CEST 2013


Hi,

I struggle with a dummy example using a multi-parameter typeclass containing 
a default implementation for a function:

---------------------------------------
{-# LANGUAGE MultiParamTypeClasses #-}

class Foo a b where

    bar :: a -> Int

    foobar :: a -> b -> Int
    foobar avalue bvalue = bar avalue

instance Foo Int Int where

    bar i = 5

main = do

print $ bar (4::Int)
---------------------------------------

I obtain the following errors. I have tried various things without any 
success.
Any help appreciated!

Thanks

TP


PS: The errors:

$ runghc test.hs
test.hs:8:28:
    Could not deduce (Foo a b1) arising from a use of `bar'
    from the context (Foo a b)
      bound by the class declaration for `Foo' at test.hs:(3,1)-(8,37)
    The type variable `b1' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    In the expression: bar avalue
    In an equation for `foobar': foobar avalue bvalue = bar avalue
test.hs:16:9:
    No instance for (Foo Int b0) arising from a use of `bar'
    The type variable `b0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s)
    Note: there is a potential instance available:
      instance Foo Int Int -- Defined at test.hs:10:10
    Possible fix: add an instance declaration for (Foo Int b0)
    In the second argument of `($)', namely `bar (4 :: Int)'
    In a stmt of a 'do' block: print $ bar (4 :: Int)
    In the expression: do { print $ bar (4 :: Int) }





More information about the Beginners mailing list