The title said it all, actually. I can't understand why this following code does not actually print "Hello World" as opposed of what >>=
does.
main = fmap putStrLn getLine
Currently, here is my line of reasoning, please check if it has any fallacy.
If we compare fmap
with >>=
(>>=) :: Monad m => m a -> (a -> m b) -> m b
fmap :: Functor f => (a -> b) -> f a -> f b
In bind, the context, or in IO terms "World" the first m
and the second m
is entirely different aside of the types. (a -> m b)
essentially recreates a new "World". This is not true in Functor, the context f
are the same hence side effects are impossible.
Now, if that's indeed the case, why doesn't the compiler gives a warning when we try to fmap
an effectful IO to an existing IO Monad?