[Haskell-cafe] A very nontrivial parser

Jonathan Cast jcast at ou.edu
Sun Jul 8 13:39:55 EDT 2007

On Sunday 08 July 2007, Andrew Coppin wrote:
> Jonathan Cast wrote:
> > I wouldn't call rank-2 types extremely rare . . .
> Well now, my parser is annoyingly clunky to use, but it *works*.
> However, I just found something where it seems to be *impossible* to
> write the necessary code without rank-2 types...
> I tried to write this type:
>   data Encoder2 = Encoder2 {stage1 :: [Word8] -> x, stage2 :: x ->
> [Word8] -> [Word8]}
> However, that doesn't work. All type variables on the right must appear
> on the left:
>   data Encoder2 x = Encoder2 {stage1 :: [Word8] -> x, stage2 :: x ->
> [Word8] -> [Word8]}
> Now I have a problem. I want to put several of these puppies into a big
> list - and I do *not* want to force the type variable 'x' to be the same
> in all cases! (Although one can easily imagine situations where you
> might want this.) So as of now, my code uses rank-2 types - despite the
> fact that I don't actually know what a rank-2 type *is* yet! o_O This is
> rather troubling...

I think surely you're using existential data types rather than rank-2 types.

Existential types: each application of Encoder2 is to arguments which require 
a specific value of x.

Rank-2 types (polymorphic fields, actually): each application of Encoder2 is 
to arguments which work with any value of x.

Jonathan Cast

More information about the Haskell-Cafe mailing list