ANN: H98 FFI Addendum 1.0, Release Candidate 7
Nick Name
nick.name@inwind.it
Sun, 22 Sep 2002 14:04:35 +0200
On Thu, 19 Sep 2002 14:23:02 +0100
Ross Paterson <ross@soi.city.ac.uk> wrote:
> If the FFI stuff were available with ST variants, and foreign
> functions of this sort could be declared with ST return types, would
> it be possible to replace unsafePerformIO in such cases with runST?
Even runST would be unsafe: if haskell performs (and a good lazy
language implementation should) the optimization which I think is called
"factorizing common subexpressions",
** when you have multiple invocations of the "same" expressions, where
"same" means two constants with the same name, or two functions with the
same name and the "same" arguments, this expression is evaluated only
once, and then, on every occurrence, the first value obtained is used
**,
then runST is unsafe.
Consider this example:
print :: String -> ST ()
myPureFunction = (runST (print "This is to be printed twice"),
runST (print "This is to be printed twice")
How many times is the string printed? Only once, because (runST (...))
is a pure expression and so it can be factorized. Guess what would
happen with multiple lock/unlock of a resource called with runST...
This, and many other semantic drawbacks, have lead to the use of monads.
So, to be safe, every C call, wich potentially has side effects, *must*
be used only in the IO monad, where it's correctly sequenced.
But one could say "hey! I got a pure C function, like "int f(int
x){return x+1;}", it hurts to be obliged to use it in a monad, only
because your haskell doesn't trust my C!". To satisfy theese silly
people :) unsafePerformIO has been provided.
Hope this helps and sorry for bad english.
Vincenzo
--
Fedeli alla linea, anche quando non c'è
Quando l'imperatore è malato, quando muore,o è dubbioso, o è perplesso.
Fedeli alla linea la linea non c'è.
[CCCP]