[Haskell-cafe] Monadic vs "pure" style (was: pros and cons of sta tic typing and side effects)

Bulat Ziganshin bulatz at HotPOP.com
Thu Sep 1 07:16:22 EDT 2005


Hello Juan,

Thursday, September 01, 2005, 12:03:15 PM, you wrote:

JCAB> instance MyNum Int where
JCAB>     (.+) a b = a + b

JCAB> instance (Monad m, MyNum v) => MyNum (m v) where
JCAB>     (.+) a b = do
JCAB>         ra <- a
JCAB>         rb <- b
JCAB>         return (ra .+ rb)

JCAB>    Just beware: to make this

i think, it is very practical. we can make alternative Prelude, this
question has been already discussed in light of redefining some list
functions (append, head...) as belonging to some Collection class

interestingly that Template Haskell, which uses Q monad to generate
unique identifiers, also use technique of defining rich set of
operations working immediately with monadic values. module
Language.Haskell.TH.Lib is full of definitions like this:

infixP p1 n p2 = do p1' <- p1
                    p2' <- p2
                    return (InfixP p1' n p2')


btw, such definitions can be simplified by using liftM/ap operations:

instance (Monad m, MyNum v) => MyNum (m v) where
    (.+) = liftM2 (.+)


real program will also need mixed operations, for example addition of
variable and constant


-- 
Best regards,
 Bulat                            mailto:bulatz at HotPOP.com





More information about the Haskell-Cafe mailing list