[Haskell-cafe] a bunch of newbie questions
Brian Hulley
brianh at metamilk.com
Fri Aug 4 12:50:25 EDT 2006
Martin Percossi wrote:
> Bulat Ziganshin wrote:
>> this is called ad-hoc polymorphism which is not supported by Haskell.
>> instead Haskell supports parametric polymorphism via type classes.
>
> I think you are wrong here Bulat. In fact, I think
> a) Haskell supports parametric polymorphism, e.g.
> id :: t -> t
> id x = x
> b) Haskell supports ad-hoc polymorphism via type classes
Sometimes a distinction is made between ad-hoc polymorphism of the kind
you'd get in C++ with method overloading, and "restricted parametric
polymorphism" as in "Monad m =>" ie:
1) id :: t -> t -- Unrestricted parametric polymorphism
2) foo :: Monad m => m a -- Restricted parametric polymorphism for (m) and
unrestricted for (a)
3) bar :: Int -> Int -> String
bar :: Char -> Bool
The only way to describe this is ad-hoc polymorphism, and the fact that
any function is of the form A -> B means regardless of the arity of the
overloaded functions it can also be supported by typeclasses (*) eg:
class Bar a b where
bar :: a -> b
instance Bar Int (Int -> String) where ...
instance Bar Char Bool where ...
And a function or value in scope can be making use of unrestricted,
restricted, and ad-hoc polymorphism at the same time eg:
zap :: Monad m => Char -> m a
zap :: Int -> String -> a String
class Zap a b where
zap :: a -> b
instance Monad m => Zap Char (m a) where ...
instance Zap Int (String -> a String) where ...
(*) But there's one exception: you can't use typeclasses to resolve
overloadings between values and functions because non-function values don't
have a type of the form A -> B:
cool :: Int
cool :: Char -> String
class Cool -- Ooops! fundamental problem encountered ;-)
Regards, Brian.
--
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.
http://www.metamilk.com
More information about the Haskell-Cafe
mailing list