Difference between revisions of "Do notation considered harmful"

From HaskellWiki
Jump to navigation Jump to search
(monadic return values)
(things to avoid)
Line 87: Line 87:
 
* Paul Hudak in Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-August/030178.html A regressive view of support for imperative programming in Haskell]
 
* Paul Hudak in Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-August/030178.html A regressive view of support for imperative programming in Haskell]
 
* Data.Syntaxfree on Wordpress: [http://syntaxfree.wordpress.com/2006/12/12/do-notation-considered-harmful/ Do-notation considered harmful]
 
* Data.Syntaxfree on Wordpress: [http://syntaxfree.wordpress.com/2006/12/12/do-notation-considered-harmful/ Do-notation considered harmful]
  +
* [[Things to avoid#do notation]]
   
 
[[Category:Syntax]]
 
[[Category:Syntax]]

Revision as of 07:21, 5 November 2007

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.

Didactics

The do notation hides functional details. This is wanted in order to simplify writing imperative style code fragments. The downsides are

  • that, since do notation is used almost everywhere, where IO takes place, newcomers quickly believe that the do notation is necessary for doing IO,
  • and that newcomers think, that IO is somehow special and non-functional, in contrast to the advertisement for Haskell being purely functional.

These misunderstandings let people write clumsy code like

do putStrLn "text"

instead of

putStrLn "text"

or

do text <- getLine
   return text

instead of

getLine

or

do
  text <- readFile "foo"
  writeFile "bar" text

instead of

readFile "foo" >>= writeFile "bar"

.

Library design

Safety

With do 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 even not be evaluated. The situation is different for IO: While processing the IO you might still ignore the contained return value.

You can write

do getLine
   putStrLn "text"

and thus silently ignore the result of getLine. The same applies to

do System.cmd.system "echo foo >bar"

where you ignore the ExitCode. 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 ()

Writing _ <- 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 do notation. It would suffice to restrict the type of the (>>) combinator to

(>>) :: m () -> m a -> m a

This way, you can omit _ <- only if the monadic return value has type ().


See also