4 ms·
whats the third function for a monad? bind return and...?
by dmead 10y ago
whats the third function for a monad? bind return and...?
- setra 10y ago(>>=) :: m a -> (a -> m b) -> m b (>>) :: m a -> m b -> m b There are two forms of bind. The >> function is used when the function does not need the value produced by the first monadic operator.
- mrkgnao 10y agoThe "categorical" definition of monads uses return and, instead of bind, join :: m(m a) -> m a You don't need to specify it if you already have bind/>>= and return/pure, since they can be derived from each other. For instance, purely by "following the types", f :: a -> m b fmap f :: m a -> m (m b) join (fmap f) :: m a -> m b In other words, (>>=) :: m a -> (a -> m b) -> m b ma >>= f = join (fmap f ma) is a perfectly good definition of bind I you have join.
- gizmo686 10y agoHaskell does have "fail" as part of the Monad typeclass. But it is being depreciated and moved into the MonadFail class. Haskell's Monad also has >> and >>= both defined as part of the typeclass itself; instead of making >> a normal function. The typeclass does provide a default implementation of >> in terms of >>=. But you are correct. In general all you need for a monad is bind and return. http://hackage.haskell.org/package/base-4.9.1.0/docs/Prelude.html#t:Monad http://hackage.haskell.org/package/base-4.9.1.0/docs/Prelude...
- platz 10y agoThat's all you need to make the compiler happy, but it's not a monad without satisfying the laws too