What uses have you found for higher-rank types in Haskell?

Viewed 2103

Higher rank types look like great fun. From the Haskell wikibook comes this example:

foo :: (forall a. a -> a) -> (Char,Bool)
foo f = (f 'c', f True)

Now we can evaluate foo id without the compiler exploding. This example is quickly followed in the book by the real-world example I have seen in a few other places: the ST monad and runST. That's pretty cool.

But I have yet to come across a situation where I solve a problem by writing my own function with an argument of higher-rank. Have you? What examples do you have of rank-2 or rank-n polymorphism in the wild?

4 Answers

A relatively-simple example of higher-ranked types in action can be found in Luke Palmer's IO-free splittable supply - he shows how to make the use of simple unique-value supplies easier:

runSupply :: (forall a. Eq a => Supply a -> b) -> b
Related