[Haskell-cafe] ST Monad - what's wrong?

Stefan O'Rear stefanor at cox.net
Sun Dec 9 12:34:59 EST 2007


On Sun, Dec 09, 2007 at 12:11:42PM +0100, Nicu Ionita wrote:
> Hi,
> 
> I'm trying to use the ST monad in order to turn an almost pure function into
> a pure one: reading a precalculated list of primes into a prime set. But the
> following code brings an error:
> 
> > primes :: Set Integer
> > primes = runST $ getPrimes "primes10h7.txt"
> 
> > getPrimes :: String -> (forall s. ST s (Set Integer))
> > getPrimes file =
> >     do cont <- unsafeIOToST (readFile file)
> >        let set = fromList $ map read $ lines cont
> >        return set
> 
> And here is the error:
> 
> Couldn't match expected type `forall s. ST s a'
> against inferred type `ST s (Set Integer)'
> In the second argument of `($)', namely
> `getPrimes "primes10h7.txt"'
> In the expression: runST $ (getPrimes "primes10h7.txt")
> In the definition of `primes':
> primes = runST $ (getPrimes "primes10h7.txt")

ST is far too big a hammer to use here.  It gives you safety you're not
using, at the cost of much more complicated typechecking.  I'd just use
unsafePerformIO.

Stefan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071209/0afd1d4e/attachment.bin


More information about the Haskell-Cafe mailing list