[Haskell-cafe] Another question about unsafePerformIO
Jochem Berndsen
jochem at functor.nl
Thu Jun 25 09:44:31 EDT 2009
Matthias Görgens wrote:
> I have a program that optimizes train schedules. It employs an
> external solver for Integer Linear Programs. The solve function has
> the following type:
>
>> solve :: Constraints -> IO (Maybe Solution)
>
> And this works. However, my external solver also behaves like a pure
> function from input to output. I wonder whether this guarantee should
> be reflected in the type system. I'd also appreciate if the compiler
> would be able to eliminate some calls to the solver.
>
>> solvePure :: Constraints -> Maybe Solution
>> solvePure = unsafePerformIO . solve
>
> Is this a good idea?
This is safe as long as there are no side effects, and not depend on its
environment (e.g. don't open files, read from environment variables).
In general, functions that do not IO should not have IO in their type
signature, because this will contaminate calling functions unnecessarily.
Adding 'unsafePerformIO' will work, but a better idea might be to
understand why your solver has IO in its type signature. Is this because
of FFI calls? You can remove IO in FFI calls if they are free from side
effects as well.
Regards,
--
Jochem Berndsen | jochem at functor.nl
GPG: 0xE6FABFAB
More information about the Haskell-Cafe
mailing list