[Haskell-cafe] evaluation semantics of bind
ok at cs.otago.ac.nz
Sun Feb 8 19:25:30 EST 2009
On 6 Feb 2009, at 4:20 am, Gregg Reynolds wrote:
> However, consider:
> getChar >>= \x -> getChar
> An optimizer can see that the result of the first getChar is
> discarded and replace the entire expression with one getChar without
> changing the formal semantics.
But the result of the first getChar is *NOT* discarded.
**As an analogy**, think of the type IO t as (World -> (t,World))
for some hidden type World, and
getChar w = (c, w')
-- get a character c out of world w somehow,
-- changing w to w' as you go
(f >>= g) w = let (v,w') = f w in (g v) w'
In this analogy, you see that the result of getChar is a value of
type IO Char (not of type Char), and that while the character
part of the result of performing the result of getChar may be
discarded, the "changed world" part is NOT.
More information about the Haskell-Cafe