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?
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 * -> *
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With