[Haskell-beginners] multi-parameter typeclass with default implementation
TP
paratribulations at free.fr
Tue Aug 20 12:13:28 CEST 2013
Ben Gamari wrote:
> As far as I know, you'd need to do something like this to accomplish
> what you are after,
>
> {-# LANGUAGE MultiParamTypeClasses, DefaultSignatures #-}
>
> class Bar a where
> bar :: a -> Int
>
> class FooBar a b where
> foobar :: a -> b -> Int
> default foobar :: Bar a => a -> b -> Int
> foobar avalue bvalue = bar avalue
>
> instance Bar Int where
> bar i = 5
> instance FooBar Int Int
>
> main = do
> print $ bar (4::Int)
> print $ foobar (5::Int) (2::Int)
It seems that the "DefaultSignatures" extension is not necessary, the
following version works correctly on my computer (ghc 7.6.2):
-------
{-# LANGUAGE MultiParamTypeClasses #-}
class Bar a where
bar :: a -> Int
class FooBar a b where
foobar :: Bar a => a -> b -> Int
foobar avalue bvalue = bar avalue
instance Bar Int where
bar i = 5
instance FooBar Int Int
main = do
print $ bar (4::Int)
print $ foobar (5::Int) (2::Int)
-------
More information about the Beginners
mailing list