Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Do notation considered harmful
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Criticism == Haskell's [[Keywords#do | 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. === Didactics === The <hask>do</hask> notation hides functional details. This is wanted in order to simplify writing imperative style code fragments. The downsides are that: * Since <hask>do</hask> notation is used almost everywhere <hask>IO</hask> takes place, newcomers quickly believe that the <hask>do</hask> notation is necessary for doing <hask>IO</hask>, * Newcomers might think that <hask>IO</hask> is somehow special and non-functional, in contrast to the advertisement for Haskell being purely functional, * Newcomers might think that the order of statements determines the order of execution. These misunderstandings let people write clumsy code like <haskell> do putStrLn "text" </haskell> instead of <haskell> putStrLn "text" </haskell> or <haskell> do text <- getLine return text </haskell> instead of <haskell> getLine </haskell> or <haskell> do text <- readFile "foo" writeFile "bar" text </haskell> instead of <haskell> readFile "foo" >>= writeFile "bar" </haskell> . The order of statements is also not the criterion for the evaluation order. Also here only the data dependencies count. See for instance <haskell> do x <- Just (3+5) y <- Just (5*7) return (x-y) </haskell> where <hask>3+5</hask> and <hask>5*7</hask> can be evaluated in any order, also in parallel. Or consider <haskell> do x <- Just (3+5) y <- Nothing return (x-y) </haskell> where <hask>3+5</hask> is probably not evaluated at all, because its result is not necessary to find out that the entire <hask>do</hask> describes a <hask>Nothing</hask>. === Library design === Unfortunately, the <hask>do</hask> notation is so popular that people write more things with monads than necessary. See for instance the [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/binary-0.4.1 Binary] package. It contains the <hask>Put</hask> monad, which in principle [http://www.haskell.org/pipermail/haskell-cafe/2009-January/053317.html has nothing to do with a monad]. All "put" operations have the monadic result <hask>()</hask>. In fact it is a <hask>Writer</hask> monad using the <hask>Builder</hask> type, and all you need is just the <hask>Builder</hask> monoid. Even more unfortunate, the [[applicative functor]]s were introduced to Haskell's standard libraries only after [[monad]]s and [[arrow]]s, thus many types were instances of the <hask>Monad</hask> and <hask>Arrow</hask> classes, but not instances of <hask>Applicative</hask>. Fortunately, since GHC 7.10 the [[Functor-Applicative-Monad Proposal]] is implemented and now <hask>Applicative</hask> ''is'' a superclass of <hask>Monad</hask>. There is no special syntax for applicative functors because it is hardly necessary. You just write <haskell> data Header = Header Char Int Bool readHeader :: Get Header readHeader = liftA3 Header get get get </haskell> or <haskell> readHeader = Header <$> get <*> get <*> get </haskell> <br>Not using monads, along with the <hask>do</hask> notation, can have advantages. Consider a generator of unique identifiers. First you might think of a <hask>State</hask> monad which increments a counter each time an identifier is requested. <haskell> run :: 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) </haskell> If you are confident that you will not need the counter state at the end and that you will not combine blocks of code using the counter (where the second block needs the state at the end of the first block), you can enforce a more strict scheme of usage. The following is like a <hask>Reader</hask> monad, where we call <hask>local</hask> on an incremented counter for each generated identifier. Alternatively you can view it as [[Continuation]] monad. <haskell> newtype T a = T (Int -> a) run :: T a -> a run (T f) = f 0 newId :: (Int -> T a) -> T a newId f = T $ \i -> case f i of T g -> g (succ i) example :: (Int -> Int -> T a) -> a example f = run $ newId $ \a -> newId $ \b -> f a b </haskell> This way users cannot accidentally place a <hask>return</hask> somewhere in a <hask>do</hask> block where it has no effect. === Safety === {{essay}} With <hask>do</hask> notation we have kept alive a dark side of the C programming language: The 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 not even be evaluated. The situation is different for <hask>IO</hask>: While processing the <hask>IO</hask>, you might still ignore the contained return value. You can write <haskell> do getLine putStrLn "text" </haskell> and thus silently ignore the result of <hask>getLine</hask>. The same applies to <haskell> do System.Cmd.system "echo foo >bar" </haskell> where you ignore the <hask>ExitCode</hask>. Is this behaviour wanted? There are possibilities to explicitly ignore return values in safety oriented languages (e.g. <code>EVAL</code> in [http://www.modula3.org/ Modula-3]). Haskell does not need this, because you can already write <haskell> do _ <- System.Cmd.system "echo foo >bar" return () </haskell> Writing <hask> _ <- </hask> should always make you cautious whether ignoring the result is the right thing to do. The possibility for silently ignoring monadic return values is not entirely the fault of the <hask>do</hask> notation. It would suffice to restrict the type of the <hask>(>>)</hask> combinator to <haskell> (>>) :: m () -> m a -> m a </haskell> This way, you can omit <hask> _ <- </hask> only if the monadic return value has type <hask>()</hask>. New developments: * GHC since version 6.12 emits a warning when you silently ignore a return value * There is a new function called <hask>void</hask> that makes ignoring of return values explicit: [http://hackage.haskell.org/trac/ghc/ticket/3292 GHC ticket 3292] <!-- related is the problem on inefficient void (mapM f xs) vs. (mapM_ f xs) -->
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width