Search code examples
frege

Type not as polymorphic as suggested


I'm trying to make a Functor instance for the following data type:

data Event t a = Event { runEvent :: t -> ([a], Event t a) }

instance Functor (Event t) where
    fmap :: (a -> b) -> Event t a -> Event t b
    fmap f e = Event go
        where
            go t = (fmap f x, e')
                where
                    (x, e') = Event.runEvent e t

But compilation fails with the following errors:

E FRP.fr:11: type `γ` is not as polymorphic as suggested in
    the annotation where just `β` is announced.
E FRP.fr:11: type error in expression go
    type is : ([γ],Event α β)
    expected: ([γ],Event α γ)

I tried adding some type annotations to generalise the let bindings but this didn't work.


Solution

  • So we have incoming e::Event t a

    Applying runEvent to e gives us still an Event t a in the second element of the tuple, is it not so? But it should rather be Event t b , right?

    (The error is quite confusing because it arises resp. go and uses the fresh type variables that have been used in type checking go)