Type error when using functional composition with RankNTypes

I got confused in the next program.

{-# LANGUAGE RankNTypes #-}
newtype A a = A ( forall f. Applicative f => f a )

good :: a -> A a
good x = A $ pure $ x

bad  :: a -> A a
bad  x = A . pure $ x

When I try to compile, I get this error message complaining about bad:

Couldn't match type `f0 a'
               with `forall (f :: * -> *). Applicative f => f a'
Expected type: f0 a -> A a
  Actual type: (forall (f :: * -> *). Applicative f => f a) -> A a
Relevant bindings include
  x :: a (bound at huh.hs:8:6)
  bad :: a -> A a (bound at huh.hs:8:1)
In the first argument of `(.)', namely `A'
In the expression: A . pure

Why is the function goodtypecheck, but ghc refuses to accept the function bad? And what can I do to make the latest version work? As far as I can see, both examples should be equivalent.

+4
source share
1 answer

, , , , . , . .

:

-- Type specialized composition
(.!) :: ((forall f. Applicative f => f b) -> c) ->
        (a -> (forall f. Applicative f => f b)) -> a -> c
(.!) f g x = f(g x)

-- Use the new version of composition
notBad  :: a -> A a
notBad  x = A .! pure $ x
+2

Source: https://habr.com/ru/post/1612854/


All Articles