Do notation considered harmful
From HaskellWiki
(State monad) |
(alternative Applicative syntax) |
||
| Line 59: | Line 59: | ||
readHeader = liftA3 Header get get get | readHeader = liftA3 Header get get get | ||
</haskell> | </haskell> | ||
| - | + | or | |
| - | + | <haskell> | |
| + | readHeader = Header <$> get <*> get <*> get | ||
| + | </haskell> | ||
Not using monads and thus <hask>do</hask> notation can have advantages. | Not using monads and thus <hask>do</hask> notation can have advantages. | ||
Revision as of 12:51, 5 November 2007
Contents |
1 Criticism
Haskell's do notation is popular and ubiquitous. However we shall not ignore that there are several problems. Here we like to shed some light on aspects you may not have thought about, so far.
1.1 Didactics
TheThis is wanted in order to simplify writing imperative style code fragments. The downsides are
- that, since notation is used almost everywhere, wheredotakes place, newcomers quickly believe that theIOnotation is necessary for doingdo,IO
- and that newcomers think, that is somehow special and non-functional, in contrast to the advertisement for Haskell being purely functional.IO
These misunderstandings let people write clumsy code like
do putStrLn "text"
instead of
putStrLn "text"
or
do text <- getLine return text
instead of
getLineor
do text <- readFile "foo" writeFile "bar" text
instead of
readFile "foo" >>= writeFile "bar"
.
1.2 Library design
Unfortunately, theSee for instance the Binary package.
It contains theEven more unfortunate, the applicative functors were introduced to Haskell's standard libraries only after monads and arrows,
thus many types are instances ofThere is not special syntax for applicative functors because it is hardly necessary. You just write
data Header = Header Char Int Bool readHeader :: Get Header readHeader = liftA3 Header get get get
or
readHeader = Header <$> get <*> get <*> get
Consider a generator of unique identifiers.
First you might think of arun :: State Int a -> a run m = evalState m 0 newId :: State Int Int newId = do n <- get modify succ return n example :: (Int -> Int -> a) -> a example f = run $ do x <- newId y <- newId return (f x y)
1.3 Safety
WithThe silent neglect of return values of functions. In an imperative language it is common to return an error code and provide the real work by side effects. In Haskell this cannot happen, because functions have no side effects. If you ignore the result of a Haskell function the function will even not be evaluated.
The situation is different forYou can write
do getLine putStrLn "text"
The same applies to
do System.cmd.system "echo foo >bar"
Is this behaviour wanted?
In safety oriented languages there are possibilities to explicitly ignore return values
(e.g. EVAL in Modula-3).
Haskell does not need this, because you can already write
do _ <- System.cmd.system "echo foo >bar" return ()
(>>) :: m () -> m a -> m a
2 Useful applications
It shall be mentioned that theE.g. in
getRight :: Either a b -> Maybe b getRight y = do Right x <- y return x
Compare
mdo x <- f x y z y <- g x y z z <- h x y z return (x+y+z)
and
mfix (\ ~( ~(x,y,z), _) -> do x <- f x y z y <- g x y z z <- h x y z return ((x,y,z),x+y+z))
3 See also
- Paul Hudak in Haskell-Cafe: A regressive view of support for imperative programming in Haskell
- Data.Syntaxfree on Wordpress: Do-notation considered harmful
- Things to avoid#do notation
