FtheBuilder
FtheBuilder

Reputation: 1437

Haskell - Applicative/Monad instances

I've seen that the type of pure 1 is (Num a, Applicative f) => f a, which is quite obvious. So if I would like to make it a Maybe Int:

Prelude> pure 1 :: Maybe Int
Just 1

What about this?

Prelude> pure 1
1
Prelude> return 1
1

What is going on? Why doesn't it complain about not knowing which instance to choose??

Edit

I think that this behaviour actually has nothing to do with monads or applicatives, but this was the context I came to it...

Upvotes: 2

Views: 68

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120751

That's a peculiarity of ghci. It has special handling for IO actions, which aren't just “computed” and printed (you can't print an IO action anyway), but executed and the result printed. So,

Prelude> pure 1 :: IO Integer
1

As per behzad.nouri's comment, here's the relevant manual section.


Provided it's IO a with Show a and not a~(), otherwise only the action is executed.

Upvotes: 3

Related Questions