strange behaviour

David Sabel dsabel@stud.uni-frankfurt.de
Mon, 11 Aug 2003 12:27:01 +0200


> 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 ().

Thanks for your comments.

Ok, Haskell is not lazy, an "referential transparency" allows you
to evalute (unsafePerformIO print "test") as often you want.

But GHC provides to to do sharing in
some sense and the compilation should be "Work-safe".

My point here is to know, what's the reason for the different
behaviour, rather than discussing the
correctness of using unsafePerformIO.


>
> 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
>
>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users@haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users