[Haskell-cafe] Martin Odersky on "What's wrong with Monads"

Tillmann Rendel rendel at informatik.uni-marburg.de
Wed Jun 27 00:19:30 CEST 2012


MightyByte wrote:
> Of course every line of your program that uses a Foo will change if you switch
> to IO Foo instead.

But we often have to also change lines that don't use Foo at all. For 
example, here is the type of binary trees of integers:

   data Tree = Leaf Integer | Branch (Tree Integer) (Tree Integer)

A function to add up all integers in a tree:

   amount:: Tree -> Integer
   amount (Leaf x) = x
   amount (Branch t1 t2) = amountt1 + amountt2

All fine so far. Now, consider the following additional requirement: "If 
the command-line flag --multiply is set, the function amount computes 
the product instead of the sum."

In a language with implicit side effects, it is easy to implement this. 
We just change the third line of the amount function to check whether to 
call (+) or (*). In particular, we would not touch the other two lines.

How would you implement this requirement in Haskell without changing the 
line "amount (Leaf x) = x"?

(I actually see three ways of doing this in Haskell, but all have 
serious drawbacks and do not fully solve the problem).

Here it seems not so bad just to change all three lines of the amount 
function, even if they are not strictly related to the semantic change 
we want to make. But in a real program, this situation can translate to 
changing thousands of lines of code in many functions just to implement 
a minor change to a single requirement.


More information about the Haskell-Cafe mailing list