[Haskell-cafe] What unsafeInterleaveIO is unsafe
Jonathan Cast
jonathanccast at fastmail.fm
Sun Mar 15 21:13:20 EDT 2009
On Sun, 2009-03-15 at 18:11 -0700, Ryan Ingram wrote:
> On Sun, Mar 15, 2009 at 1:56 PM, Jonathan Cast
> <jonathanccast at fastmail.fm> wrote:
> >> But not if you switch the (x <- ...) and (y <- ...) parts:
> >>
> >> main = do
> >> r <- newIORef 0
> >> v <- unsafeInterleaveIO $ do
> >> writeIORef r 1
> >> return 1
> >> y <- readIORef r
> >> x <- case f v of
> >> 0 -> return 0
> >> n -> return (n - 1)
> >> print y
> >>
> >> Now the IORef is read before the case has a chance to trigger the writing.
> >
> > But if the compiler is free to do this itself, what guarantee do I have
> > that it won't?
>
> You don't really have any guarantee; the compiler is free to assume
> that v is a pure integer and that f is a pure function from integers
> to integers. Therefore, it can assume that the only observable affect
> of calling f v is non-termination. Note that unsafeInterleaveIO
> *breaks* this assumption;
[Ignored; begging the question]
jcc
More information about the Haskell-Cafe
mailing list