unsafePerformIO safety.

David Brown haskell2 at davidb.org
Tue Mar 6 19:03:59 EST 2007


Neil Mitchell wrote:

> On 3/6/07, Lennart Augustsson <lennart at augustsson.net> wrote:
>> Yeah, you really need {-# NOINLINE var #-} to make it reasonable safe.
>
> Couldn't GHC bake in knowledge about unsafePerformIO, and never inline
> it? It is a slightly hacky solution, but since unsafePerformIO is
> pretty much only used in hacks, I think its almost fitting.

It seems to be used a bit more than just as a hack.  Many things that
interface with the real world, but try to present lazy interfaces have
to use it.

Maintaining a shared state that doesn't have to be passed around to
everything that uses it isn't really a hack.

As an example, I would like to have something that performs logging,
and is used by many clients.  Without the unsafePerformIO, everything
has to somehow find and pass around the state of this logging system,
whereas with it, it can just allocate one when first needed.

Non-strict semantics are kind of a new thing for me.  I'm still trying
to get a good grasp of what can be lazy, what should live in the IO
Monad, and what should have a carefully crafted "bridge" between the
two.  Things like hGetContents have somewhat set a trend here.

Dave



More information about the Glasgow-haskell-users mailing list