我正在浏览类型类的源代码MonadPlus
及其实例,例如Maybe
,[]
等等。我没有找到这些实例的方法 -mzero
或mplus
定义。这是实例的源代码:Maybe
MonadPlus
class (Alternative m, Monad m) => MonadPlus m where
mzero :: m a
mzero = empty
mplus :: m a -> m a -> m a
mplus = (<|>)
instance MonadPlus Maybe
在Real World Haskell 的第 15 章中,它说以下是 and 的标准定义mzero
and 。mplus
Maybe
[]
class Monad m => MonadPlus m where
mzero :: m a
mplus :: m a -> m a -> m a
instance MonadPlus [] where
mzero = []
mplus = (++)
instance MonadPlus Maybe where
mzero = Nothing
Nothing `mplus` ys = ys
xs `mplus` _ = xs
所以基本上我的问题是为什么会有区别?