strange behaviour
Jon Fairbairn
Jon.Fairbairn@cl.cam.ac.uk
Mon, 11 Aug 2003 11:01:11 +0100
On 2003-08-11 at 11:44+0200 "David Sabel" wrote:
> module Main(main) where
>
> import System.IO.Unsafe
>
> main = case unsafePerformIO (print "test") of
> () -> main
>
>
> ok, probably I use unsafePerformIO in an "unsafe" way and so on,
> but executing the program prints infinitely often "test" on the screen,
> but I think it would be correct to do so one time?
It's correct behaviour to print "test" any number of
times. Haskell is non-strict, which only means that things
aren't evaluated unless needed. It's not (defined to be)
lazy, which would mean that named expressions would be
evaluated at most once (though ghc meets this). It's also
not defined to be "fully lazy" meaning that unnamed
expressions would be evaluated at most once in any given
closure. So GHC is entirely within its rights to evaluate
<<unsafePerformIO (print "test")>> any number of times, or
possible even none, since it knows that that expression
always returns the same value ().
So you /have/ used it in an unsafe way, and the above
discussion illustrates why unsafePerformIO really is
completely unsafe.
Jón
--
Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk