Haskell function signature

I have doubts about the type signature in haskell. Reading about the applicative functor, I found:

pure (+) <*> Just 3 

which returns Just (+3) , which is of type Maybe (a->a) . Now the signature <*> is equal to

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

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

And here I was surprised because, as far as I knew, b cannot unite (sorry if I do not use the indicated terminology, but hope that they are clear enough) with a->a .

Is this possible only because we are inside an applicative functor or is there something else that I miss?

+4
source share
2 answers

b is a type variable (n unlimited), so it can always be combined with each type. It has nothing to do with Applicative functors, it works wherever a type variable needs to be unified with a type.

Roughly speaking, the union of two type expressions leads to an expression of the most general type, which is not more general than any of the partners of the union. If one of the two type expressions is more general than the other, unification always succeeds and leads to a more specific join partner. The most common of all type expressions is one without any structure — a type variable. Therefore, a type variable can be unified with any type expression by creating an instance of a type variable with an expression of this type (provided that the types match, a type variable whose type * may, of course, not be unified with an expression of type Maybe whise kind * -> * )

+11
source

:t pure (+) is

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

Pay attention to f(a -> a -> a)

Since :t (<*>) is

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

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

So, a variable of type b in this case a -> a

+7
source

All Articles