How does Haskell know which type instance you have in mind?

This question arose while reading a new chapter in the book "Learn You a Haskell" on applicative functors.

The applicable typeclass has, as part of the Maybe instance definition:

pure = Just 

If I just go into GHCi and import Control.Applicative and do:

 pure (3+) 

I get nothing (it makes sense). But if I use it in the expression part:

 pure (3+) <*> Just 4 

I get Just 7. I think this is also not surprising, but I don’t see anything in common with how cool classes work, I think there is no ambiguity in calling pure here.

If my confusion makes sense, can someone explain what is going on in detail?

+4
source share
4 answers

It just enter the output. The operator (<*>) requires that both arguments use the same Applicative instance. The right side is Maybe , so the left side should also be Maybe . So how does it determine which instance is used here. You can look at the type of any expression in the interpreter by typing :t expression , and perhaps if you just look at each subexpression and look at the type that was inferred, you get a more complete picture of what is going on.

+6
source

It is worth looking at the type that the compiler requests for pure (3+) :

 Prelude Control.Applicative> :t pure (3+) pure (3+) :: (Num a, Applicative f) => f (a -> a) 

The type of this term is overloaded, and the decision on the numerical class and applicative class is postponed until a later time. But you can force to create a specific type with annotation, for example:

 *Showfun Control.Applicative> pure (3+) :: Maybe (Double -> Double) Just <function> 

(This works because Showfun has an instance declaration that prints the value of the function as <function> .)

It is simply a matter of when the compiler has accumulated enough information to make a decision.

+3
source

To expand the bit on the newacct response, if there is not enough information to deduce the actual type, the compiler may (in some cases) try to choose the default type, limited to those that satisfy the specified type restrictions, In this case, the type assumed is IO ( n β†’ n) for some hard-to-define instance Num => n. GHCi then evaluates it and discards the return value with no visible effect.

+2
source

Here is an interesting stream of SO on output type . Not Haskell specific, but a lot of good references and stuff to read about the type of output in functional languages.

+1
source

All Articles