Search code examples
haskelltype-systemsy-combinatorhigher-rank-types

Weird error when using scoped type variables and the y combinator in haskell


So I'm playing around with the y-combinator and anonymous functions, and I ran into this weird error:

Couldn't match expected type `t0 -> t1 -> t2'
            with actual type `forall b. b -> [b] -> [b]'
The lambda expression `\ (n :: Int) newVal xs -> ...'
has three arguments,
but its type `Int -> forall b. b -> [b] -> [b]' has only one

(source code that creates the error, and version that I eventually got working)

If I modify the types slightly to avoid Rank N polymorphism (use forall b. Int -> b -> [b] -> [b]), the error is similar:

Couldn't match expected type `t0 -> t1 -> t2 -> t3'
            with actual type `forall b. Int -> b -> [b] -> [b]'
The lambda expression `\ (n :: Int) newVal xs -> ...'
has three arguments,
but its type `forall b. Int -> b -> [b] -> [b]' has none

Could someone explain to me why forall b. b -> [b] -> [b] has no arguments?


Solution

  • Since you're using GHC 7, this appears to have the same root cause as the bug reported in http://hackage.haskell.org/trac/ghc/ticket/4347 . While that bug report talks about impredicative polymorphism, it seems like it's most likely in a unification problem in higher-rank polymorphism. In your case, it's being triggered by your placement of the forall, which makes the type syntactically rank-2.

    Note that this isn't really a bug. The further clarification provided makes it clear this is intended behavior, in that polymorphic instantiations of types, including rank-N types and impredicative types, are not inferred. Careful addition of type signatures can make it work.

    But since the type wasn't intended to be higher-rank, at all, in your case, it's best to just get rid of that.