[Haskell-cafe] Re: Threads with high CPU usage

Malcolm Wallace Malcolm.Wallace at cs.york.ac.uk
Mon Dec 22 05:30:51 EST 2008

Don Stewart <dons at galois.com> wrote:

> Modify the 'unsafe' inports to be 'safe'? I don't think HDBC is going
> to call back in, so should be fine. John?

For those who are puzzled, Don is suggesting that
    foreign import ccall unsafe "foo" :: Bar -> Baz
should simply be changed to
    foreign import ccall safe "foo" :: Bar -> Baz

And in case anyone is wondering whether fiddling with safety
declarations is entirely wise:

   "unsafe" means I, the programmer, guarantee that this foreign function
   cannot call back into Haskell land, so you, the compiler, are free to
   do unsafe things when implementing the call.

   "safe" means I the programmer, cannot guarantee that this foreign
   function is safe, so please, you the compiler, make sure that you add
   extra checks and assurances to ensure that the call really will be
   safe in case it calls back into Haskell land.

The terminology seems counter-intuitive, but in other other words, a
"safe" call is slower but more flexible, an "unsafe" call is fast and
dangerous.  Therefore it is always OK to convert an "unsafe" declaration
into a "safe" one, but never OK to convert from "safe" to "unsafe"
without looking at what the foreign side actually does.


More information about the Haskell-Cafe mailing list