Optimisation and unsafePerformIO

Simon Marlow simonmar@microsoft.com
Mon, 28 Oct 2002 09:41:44 -0000


> Consider the following program:
>=20
> ---------------------------------
> {-# NOINLINE b #-}
>=20
> b x  =3D if even x then unsafePerformIO getChar else bot
>=20
> bot =3D bot
>=20
> main =3D do
>          putChar (b 4)
>          putChar (b 6)
>=20
> -----------------------------
>=20
> when you compile the programm with the options: -O0
> and execute the program you get:
> > test
> ab      (That's the input)
> ab      (That's the ouput)
>=20
> when you compile the programm with the options: -O1 -fno-cse
> you get:
> > test
> ab
> aa

You are using unsafePerformIO in an unsafe way.  The meaning of your
program depends on whether the compiler implements full laziness or not,
which is a decision left entirely up to the compiler implementor.  If
you want to write portable code, don't use unsafePerformIO in this way.

What exactly is it you're trying to achieve?  Perhaps we can suggest a
better solution.

Cheers,
	Simon