[nhc-bugs] fixIO

Ross Paterson ross@soi.city.ac.uk
Tue, 2 Sep 2003 14:46:39 +0100


The definition of fixIO used by Nhc (in System.IO), namely

	fixIO f = let x = unsafePerformIO (f x) in return x

produces the wrong results for programs like

	main = do
		putStrLn "aaa"
		ds <- fixIO $ \ds -> do
			putStrLn "bbb"
			return ('d':ds)
		putStrLn "ccc"
		putStrLn (take 3 ds)

(The "left tightening" law says that moving putStrLn "bbb" up a line
should not change the meaning.)

I suggest:

	fixIO f = IO (\w -> let { IO f' = f a; r = f' w; Right a = r } in r)