Is do-notation specific to "base:GHC.Base.Monad"?
Posted
by yairchu
on Stack Overflow
See other posts from Stack Overflow
or by yairchu
Published on 2010-05-13T23:09:06Z
Indexed on
2010/05/13
23:14 UTC
Read the original article
Hit count: 302
The idea that the standard Monad
class is flawed and that it should actually extend Functor
or Pointed
is floating around.
I'm not necessarily claiming that it is the right thing to do, but suppose that one was trying to do it:
import Prelude hiding (Monad(..))
class Functor m => Monad m where
return :: a -> m a
join :: m (m a) -> m a
join = (>>= id)
(>>=) :: m a -> (a -> m b) -> m b
a >>= t = join (fmap t a)
(>>) :: m a -> m b -> m b
a >> b = a >>= const b
So far so good, but then when trying to use do-notation:
whileM :: Monad m => m Bool -> m ()
whileM iteration = do
done <- iteration
if done
then return ()
else whileM iteration
The compiler complains:
Could not deduce (base:GHC.Base.Monad m) from the context (Monad m)
Question:
Does do-notation work only for base:GHC.Base.Monad
? Is there a way to make it work with an alternative Monad
class?
Extra context:
What I really want to do is replace base:Control.Arrow.Arrow
with a "generalized" Arrow
class:
{-# LANGUAGE TypeFamilies #-}
class Category a => Arrow a where
type Pair a :: * -> * -> *
arr :: (b -> c) -> a b c
first :: a b c -> a (Pair a b d) (Pair a c d)
second :: a b c -> a (Pair a d b) (Pair a d c)
(***) :: a b c -> a b' c' -> a (Pair a b b') (Pair a c c')
(&&&) :: a b c -> a b c' -> a b (Pair a c c')
And then use the Arrow
's proc-notation with my Arrow
class, but that fails like in the example above of do-notation and Monad
.
I'll use mostly Either
as my pair type constructor and not the (,)
type constructor as with the current Arrow
class. This might allow to make the code of my toy RTS game (cabal install DefendTheKind
) much prettier.
© Stack Overflow or respective owner