more unsafePerformIO questions (is it safe to use with ReadMode Handles)?

Hal Daume t-hald@microsoft.com
Mon, 18 Aug 2003 09:13:47 -0700


Hi folks,

I'm finishing up my Haskell interface to WordNet
(http://www.cogsci.princeton.edu/~wn/) and have a standard
unsafePerformIO question :).

Basically, the interface functions by first calling an initialization
function, 'initializeWordNet :: IO WordNetEnv'.  WordNetEnv is
essentially just a record of a lot of Handles which we will be reading
from and doing binary search in.

All the functions which use the WordNetEnv (i.e., every other function
in the interface) basically do the following:

  - take one handle from the WordNetEnv and do binary search=20
    in it, read a line.

  - use that line to read another line from another of the handles

  - parse that last one

of course, therefore, all of these functions have type '... -> IO
something'.

However, one of the "rules of thumb" for using unsafePerformIO is when
you can imagine a functional interface doing the same thing.  I can:
simply read in all the databases in initializeWordNet and then do
Data.List.lookup on the results.  Does this mean it's safe to wrap all
these functions in unsafePerformIO?

If not, I'll probably just create a WordNet Reader monad and wrap things
up like that -- if you have to be in a monad anyway, there's no reason
to pass the WordNetEnv around everywhere.  But still, if possible, it
would be nice for these to be pure.

 - Hal

--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume