Type error with ranking types and lenses

I have a simple polymorphic data type Foo

{-# LANGUAGE TemplateHaskell #-}
import Control.Lens

data Foo c = 
    Foo {
        _bar :: c,
        _baz :: c,
        _quux :: c
    }   

makeLenses ''Foo

The generated lenses are, of course, polymorphic in c. Type from GHCi:

*Main> :t bar
bar :: Functor f => (c0 -> f c0) -> Foo c0 -> f (Foo c0)

I made a data type Blahto wrap around the lens. This works great in simple cases (with extension RankNTypes, of course):

data Blah = Blah (forall c . Lens' (Foo c) c)

orange :: Blah
orange = Blah bar

But something a little more complicated does not work, for example

cheese :: [Blah]
cheese = map Blah [bar]

This last piece of code gives an error from the GHC:

    Couldn't match type โ€˜(c0 -> f0 c0) -> Foo c0 -> f0 (Foo c0)โ€™
                  with โ€˜forall c (f :: * -> *).
                        Functor f =>
                        (c -> f c) -> Foo c -> f (Foo c)โ€™
    Expected type: ((c0 -> f0 c0) -> Foo c0 -> f0 (Foo c0)) -> Blah
      Actual type: (forall c. Lens' (Foo c) c) -> Blah
    In the first argument of โ€˜mapโ€™, namely โ€˜Blahโ€™
    In the expression: map Blah [bar]

It seems to have forall c f .disappeared from โ€˜(c0 -> f0 c0) -> Foo c0 -> f0 (Foo c0)โ€™, but I do not understand why.

Why doesn't this compile and what can I do to make something like this work?

+4
source share
1 answer

, [bar] [forall c . Lens' (Foo c) c], forall f c . Functor f => [(c -> f c) -> Foo c -> f (Foo c)]. , . (im) . , - , . , a forall (, []).

[bar] , ImpredicativeTypes. map Blah - , .

bar' :: [forall c . Lens' (Foo c) c]
bar' = [bar] 

mapBlah :: [forall c . Lens' (Foo c) c] -> [Blah]
mapBlah = map Blah 

typechecks:

> mapBlah bar'

> (map Blah :: [forall c . Lens' (Foo c) c] -> [Blah]) 
    ([bar] :: [forall c . Lens' (Foo c) c])

, lens Control.Lens.Reified, . , , c Lens' (Foo c) c , .

+2

All Articles