From the source of Maybe
in ghc:
instance Functor Maybe where
fmap _ Nothing = Nothing
fmap f (Just a) = Just (f a)
When fmap
is applied to Nothing
, it should return Nothing
.
For example, run this in ghci (v8.2.2):
Prelude> fmap (+1) Nothing
Nothing
However, when I apply a function with an arity of 2:
Prelude> fmap (++) Nothing
<interactive>:11:1: error:
• No instance for (Show ([a0] -> [a0]))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In a stmt of an interactive GHCi command: print it
In fact, the result seems to be Nothing
:
Prelude> import Data.Maybe
Prelude Data.Maybe> isNothing $ fmap (++) Nothing
True
My question is, does fmap (++) Nothing
really return Nothing
?
Yes, fmap f Nothing = Nothing
no matter what f
is or what type it has. The Show
instance for Maybe
looks like
instance Show a => Show (Maybe a) where
...
So you can't show
or print
a value of type Maybe (A -> B)
because functions don't have a Show
instance.