DEV Community

Discussion on: What the heck is polymorphism?

Collapse
 
wongjiahau profile image
WJH • Edited

Nice article! It helps me a lot on contemplating about the design of my programming language.

I kind of understand every category of polymorphism you mentioned, but I still couldn’t understand how can higher-rank polymorphism is useful.

So, do you mind to provide any practical example of higher rank polymorphism (It would be better if the example you provided is actually used in industrial code, not just in the academia)

Collapse
 
jvanbruegge profile image
Jan van Brügge

Yes, I've added a paragraph about the ST trick there. Another possible use case would be callback functions that receive data from your API:

withHook: (forall a. IsApiData a => a -> IO ()) -> IO ()

This allows withHook to send any data to the callback that implements the IsApiData type class

Collapse
 
drbearhands profile image
DrBearhands

I'm confused, isn't this ad-hoc (or 'first-rank' I guess) as it only quantifies over a?

Thread Thread
 
jvanbruegge profile image
Jan van Brügge

No, as you can see the forall is in the parenthesis, the scope does not go until the return type

Thread Thread
 
drbearhands profile image
DrBearhands

herp derp, realized it myself just now, ∀x(P(x))->Q ≠ ∀x(P(x)->Q).

So this would require an an ad-hoc polymorphic function as argument, yes?

Thread Thread
 
jvanbruegge profile image
Jan van Brügge

Correct 👍