r/purescript • • Oct 09 '19

What are the differences between Bounded Parametric Polymorphism and Ad Hoc Polymorphism?

For the longest time I've always associated ad hoc polymorphism with function overloading where the type that's passed in matters because the underlying implementation may be different.

I've associated parametric polymorphism with Generics in other languages as well as functions that can accept any type as the type truly does not matter with respect to how it's used.

However, whenever you place a constraint on a type, it seems to me that the type does matter in the sense that you've narrowed the scope as to what the type can be.

So, is it the case that with bounded parametric polymorphism that the type truly doesn't matter because the usage is going to be the same so long as it meets some constraint?

I'm probably thinking about this incorrectly and have some implicit assumption I'm making that's incorrect but I'm having trouble teasing it out. Let me know if I can clearer. I think the difference has to do with the underlying usage of the type and whether or not it's the same or has to change.

The question is how does bounded parametric polymorphism differ from ad hoc polymorphism?

Thanks in advance!

Edit: Thank you all for the replies! I've got a lot of thinking to do.

8 Upvotes

11 comments sorted by

View all comments

Show parent comment

4

u/ct075 Oct 09 '19

I agree with everything said here except the assertion that "any interesting polymorphism is bounded". Of course, to do anything immediately useful, you need some structure, but in fact with less structure we get theorems for free.

Of course, doing useful things with these does require some other structure (such as theorems over polymorphic list types, which I suppose also provides some bounding), but it's often a virtue to make things as polymorphic as possible. A function with type

([a] -> l) -> ([a] -> r) -> [a] -> (l,r)

has to call the two function inputs on some sublist of the other input, for example, which constraints its behavior. To do anything nontrivial, then, it probably partitions the list somehow, etc etc.

2

u/patrick_thomson Oct 09 '19

Absolutely. “Immediately useful” is a better phrase than what I came up with.