[Haskell-cafe] Re: evaluation semantics of bind
gleb.alexeev at gmail.com
Thu Feb 5 12:22:24 EST 2009
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?
Perhaps my example doesn't work, so I'll try another example.
As you know, (>>=) is just an (overloaded) higher-order function.
Let's consider another higher-order function, map. The expression
> map (\x -> 42) [1..3]
evaluates to [42, 42, 42].
As you can see, the function (\x -> 42) passed to map ignores its first
argument. Would you expect the compiler to discard the call to map?
Can you see the analogy? The shapes of the two expressions, map (\x ->
42) [1..3] and (>>=) getChar (\x -> getChar) are similar.
(>>=) is a combinator just like map, you cannot optimize it away in general.
More information about the Haskell-Cafe