Cheatsheet: Rank 2 types with typeclasses in Haskell

Posted on July 8, 2017

Here’s a brief example of rank-2 polymorphism in Haskell.

The key parts are the flag enabling the Rank2Types compiler feature:

and the portion of the type signature dealing with the input function f:

It is key that the forall a. (and the typeclass (Num a)) are inside the input function type, rather that part of the type of applyFn.