[Haskell-cafe] Baffled by Disk IO

Matthew Bromberg mattcbro at earthlink.net
Sat Jul 29 19:09:52 EDT 2006

I have run into a baffling distinction between the behavior of GHCi and 
the compiled binary from GHC.
I suspect it's something pretty stupid on my part.

I have the following test program
import Matrix

main = let
    f1 = bRgauss 4 3
    f2 = bRgauss 3 2
    fu = f1 *. f2   
    in bsave "fumat" fu
The type signature of bsave is

    -- | save a matrix to a file
    bsave :: String -> m -> IO()
    bsave str z = do
        k <- matindx z
        withCString str (\x -> bmsave x k)

and bmsave is a C routine that does a simple write to disk using fprintf. 
If I compile using GHC and run this as test.exe it does absolutely 
nothing, no file is actually saved.  In fact no file is saved even
if I try to print components of fu in the IO monad.

Whereas if I load it into GHCi and run main, everything works as 
expected.  Well sort of.  Apparently everytime I extract something out
of fu and use it in an IO monad it executes all the IO actions in fu, so 
it's easy to get unwanted behavior with incautious use.

More information about the Haskell-Cafe mailing list