[Haskell-cafe] Help Haskell driving Medical Instruments

Ben Millwood haskell at benmachine.co.uk
Wed Nov 11 08:59:39 EST 2009

On Tue, Nov 10, 2009 at 5:04 AM, Philippos Apolinarius
<phi500ac at yahoo.ca> wrote:
>  foreign import ccall "rs232.h opencport" opencport :: CInt -> IO ()
>  foreign import ccall "rs232.h closecport" closecport :: CInt -> CInt
> Originally, I had the following line (that did not work properly):
> foreign import ccall "rs232.h closecport" closecport ::  IO ()

I don't know why the latter line didn't work properly, but I'm pretty
sure it's closer to the right answer than the former. If you don't
have an IO type for your function, then Haskell is allowed to assume
it is pure (has no side effects) and can then call it only when the
result is needed, or multiple times if it likes, without affecting the
meaning of the program. For a function that closes a handle this is
clearly not the case.
So I'm pretty sure your type signature needs to be in IO if you want
to guarantee it is called at the right time; it might be worth
elaborating on how the IO () version did not work, and how you used
The way you are using it now would appear to work most of the time
because the print statement will force the result to be evaluated,
forcing the function to be called - but having a handle closed based
on when an apparently irrelevant print statement runs or doesn't is
obviously not ideal.

More information about the Haskell-Cafe mailing list