user1544128
user1544128

Reputation: 582

haskell function signature

I'm having a doubt about type signature in haskell. Reading about applicative functor, I have found:

pure (+) <*> Just 3 

which gives back Just (+3) which is of type Maybe (a->a). Now the signature of <*> is

 (<*>) :: Applicative f => f (a -> b) -> f a -> f b

which means that our f b in the above example is is obtained substituting f with Maybe and b with a->a.

And here I was kind of surprised because as far as I knew, b cannot unify (sorry if I'm not using specified terminology, but I hope they are clear enough) with a->a.

Is that possible just because we are inside and applicative functor or there's something else that I'm missing?

Upvotes: 4

Views: 630

Answers (2)

Daniel Fischer
Daniel Fischer

Reputation: 183888

b is a(n unconstrained) type variable, so it can unify with every type, always. It has nothing to do with Applicative functors, it works wherever a type variable has to be unified with a type.

Roughly, unifying two type expressions results in the most general type expression that is not more general than either partner of the unification. If one of the two type expressions is more general than the other, unification always succeeds and results in the more specific partner of the unification. The most general of all type expressions is one without any structure at all, a type variable. Hence a type variable can be unified with any type expression by instantiating the type variable with that type expression (provided the kinds match, a type variable whose kind is * can of course not be unified with the type expression Maybe whise kind is * -> *).

Upvotes: 11

manojlds
manojlds

Reputation: 301177

:t pure (+) is

pure (+) :: (Num a, Applicative f) => f (a -> a -> a)

Note the f(a -> a -> a)

So since :t (<*>) is

(<*>) :: Applicative f => f (a -> b) -> f a -> f b

f ( a -> b) is actually a f ( a -> a -> a).

So the type variable b in this case is a -> a

Upvotes: 7

Related Questions