[Haskell-cafe] Another question about unsafePerformIO

John Meacham john at repetae.net
Thu Jun 25 09:49:26 EDT 2009

On Thu, Jun 25, 2009 at 03:38:41PM +0200, 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?

If it is actually fully pure, including things like debugging output,
then this is fine. However, if the algorithm takes a signifigant amount
of time or resources, you may want to keep it in IO just so users can
have control over exactly when and how often it is run. 

unsafePerformIO was included in the FFI spec for just this sort of case.


John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/

More information about the Haskell-Cafe mailing list