No. The monad instance has no notion of emptiness1, so it can't check on Nothing
and replace the value in that case.
What you basically need is the monoid instance; that has Nothing
as its identity element so whatever you combine with Nothing
will just come out as is.
instance (Monoid a) => Monoid (Maybe a)
Unfortunately, Bool
is in itself not a monoid. Well, actually it is a monoid! but not in a unique way, so they couldn't've chosen any particular instances. But with newtype
wrappers, those are in Data.Monoid
:
newtype Any = Any { getAny :: Bool }
instance Monoid Any
let's try it...
Prelude Data.Monoid> fmap getAny $ (Just $ Any True) <> (Just $ Any False)
Just True
Prelude Data.Monoid> fmap getAny $ (Nothing) <> (Just $ Any False)
Just False
1Of course, there's fail
... but that's a historic accident.