Hacker News new | ask | show | jobs
by harrisi 3709 days ago
This is incorrect. Haskell and OCaml both support parametric polymorphism, but Haskell also supports ad-hoc polymorphism, whereas OCaml does not. Modular implicits, it seems, are OCaml's solution to the problem of reducing the set of valid types in a polymorphic function.

Haskell: concat :: [a] -> [a] -> [a]

OCaml: val concat : 'a list -> 'a list -> 'a list = <fun>

Both are parametrically polymorphic. However, in Haskell, I can use a type class to support ad-hoc polymorphism:

concat :: (Num a) => [a] -> [a] -> [a]

Now concat works forall a, as long as a is an instance of Num. This is important for arithmetic operators:

(+) :: (Num a) => a -> a -> a

Now I can have (+) be defined in a meaningful way, since Int, Integer, Float, etc. are all instances of Num, and implement (+). An easy way to think of this (since it's all it is, really) is operator/method overloading. In Haskell, (+) is overloaded for every type that you would want it to work for (the details of this are actually probably different, but are not important for learning).

In OCaml, if I have

val (+) : 'a -> 'a -> 'a = <fun>

There is actually only two implementations of that function:

let (+) a b = a

or

let (+) a b = b

Since I cannot restrict the set of types to the function, I can't do anything with the arguments. I don't know what the types are! Ad-hoc polymorphism allows for restricting the set of types, which allows for Haskell to use + for any Num-like thing.

2 comments

I also don't see the disagreement between your comment and the parent.
I appreciate the clearer explanation, but I'm not sure what I said that was incorrect. Can you explain?
Hm, after re-reading a couple times, I think I merely misinterpreted how you said "OCaml supports parametric polymorphism" and "Haskell supports ad-hoc polymorphism." Based on your wording I thought you were saying that Haskell only supports ad-hoc polymorphism and OCaml only supports parametric polymorphism. Surely there was something in my head at 2am that made me think you were off in your description, but I don't know what it was. Sorry! I would edit it if I could.

EDIT: I think the line that stood out to me as being odd was "That's why OCaml has more than one print function, and why it has integer and floating point versions of operators like + and *." which was after you explained what parametric polymorphism was. So my interpretation was "OCaml supports parametric polymorphism, which is why there are multiple addition functions for different types." Of course, the real reason isn't due to supporting parametric polymorphism, but specifically because OCaml lacks support for ad-hoc polymorphism.

Interestingly, OCaml does have sort of pseudo-ad-hoc polymorphic implementations for comparison operators.

val (<) : 'a -> 'a -> bool = <fun>

But this is a wonderfully hacky implementation.

Cool, thanks for the clarification! It was a late night for me too so I wasn't as clear as I thought I was being.