[Haskell-cafe] Re: evaluation semantics of bind

Jeremy Shaw jeremy at n-heptane.com
Thu Feb 5 12:35:10 EST 2009

At Thu, 5 Feb 2009 11:06:22 -0600,
Gregg Reynolds wrote:

> Are you saying that using equations to add a level of indirection
> prevents optimization?  I still don't see it - discarding x doesn't
> change the semantics, so a good compiler /should/ do this.  How is
> this different from optimizing out application of a constant function?

Let rewrite this:

  'getChar >>= \x -> getChar'

in a slightly different looking form, where bind = (>>=) so that we have:

 bind getChar (\x -> getChar)

If I understand your argument, you think that the compiler should be
able to look at that expression, and instead of calling 'bind' with
the two arguments 'getChar' and '(\x -> getChar)' it should just
optimize it to 'getChar'?

By comparision, consider this function:

 op :: Maybe a -> (a -> Maybe b) -> Maybe b
 op x f = Nothing

I believe that by your argument we should be able to rewrite this:

   (Just "foo") `op` \x -> (Just "bar")


   (Just "bar")

because the 'x' is ignored? But, 'op' *always* returns Nothing, and
clearly Nothing /= (Just "bar")?

Maybe I misunderstanding what you mean when you say that 'discarding
x does not change the semantics'.

- jeremy

More information about the Haskell-Cafe mailing list