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

Erlend Hamberg ehamberg at gmail.com
Thu Aug 29 08:31:55 CEST 2013


You could also use functional dependencies here:

{-# LANGUAGE FunctionalDependencies #-}

class Foo a b | a -> b where
 bar :: a -> Int
foobar :: a -> b -> Int
foobar avalue bvalue = bar avalue

instance Foo Int Int where
bar i = 5

main = print $ bar (4::Int)

Here you are saying that the type parameter b is determined by the
parameter a, so GHC knows that the instance Foo Int Int is the only
instance of Foo with a = Int, thus removing the ambiguity.

-- 
Erlend Hamberg
ehamberg at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20130829/b9aa3f9d/attachment.htm>


More information about the Beginners mailing list