[Haskell-cafe] Re: Aren't type system extensions fun?

Chris Smith cdsmith at twu.net
Tue May 27 11:23:39 EDT 2008

Thomas Davie wrote:
> This is perhaps best explained with an example of something that can be
> typed with rank-2 types, but can't be typed otherwise:
> main = f id 4
> f g n = (g g) n

That's certainly interesting.  Here's another interesting example.  In an 
old blog post of mine...


I was doing my first playing around with automatic differentiation.  It 
turns out that the type that's inferred for the AD function diff there 
can sometimes lead to incorrect behavior.  Barak Pearlmutter pointed out 
such a case in the comments:

    diff (\x -> (diff (x*) 2)) 1

This mixes several variables on which one is performing a derivative, and 
leads to confusion between the various derivatives.  The result is a 
wrong answer.  The version with rank 2 types, which are the types you 
really want here, catches this error at compile time.  (Granted, it would 
be nice to get the correct answer; but an error is infinitely superior to 
the wrong answer!)

As I mentioned in the article, though, rank 2 types have their own 
problems, in that one needs many versions of the function: one for each 
subclass of Num.


More information about the Haskell-Cafe mailing list