[Haskell] randomgen for foreign random functions

Hal Daume III hdaume at ISI.EDU
Fri Jun 25 12:42:51 EDT 2004


Hi All --

Say I have a foreign function:

foreign import ccall "statistical_c.h gengam"     c_gengam :: CDouble -> CDouble -> IO CDouble

that returns a random value parameterized by the two CDoubles.  clearly 
this must happen in IO since the return value will be different each time, 
and some global state stuff is getting modified on the C side to 
facilitate future random # generation.

However, I would like to wrap this into an essentially pure function, ala 
the Random class.  basically, i want to wrap this up as something like:

gengam :: RandomGen g => g -> Double -> Double -> (g, Double)

analogously to the normal random # generation stuff.

is there a safe way to do this?  one option, of course, is just to 
unsafePerformIO it and {-# NOINLINE #-} the call, but that is rather, uhm, 
unsafe, I believe.  it seems to me that something like this should be 
possible.

Any thoughts?

 - Hal


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



More information about the Haskell mailing list