the real reason not to circumvent the IO Monad?

14 views
Skip to first unread message

Phaedon Sinis

unread,
Oct 28, 2011, 10:59:33 PM10/28/11
to stanford-11au-cs240h
Question, please, on this fine Friday evening!

The following code is segfaulting and/or acting erratically, whereas it runs just fine if I type it line-by-line into GHCI.

I suspect the following reason: 
c_ImageSpecCreate, c_ImageOutput_open, c_ImageOutput_close, etc
all have side effects that are dependent on each other... but I didn't bother to wrap them in the IO Monad.

So I guess the order of execution is not guaranteed, since they are not monadic?
(I thought I could get around that by using the return operator)


=======================================
-- as an example of my FFI imports (note lack of IO monadic types)
foreign import ccall "ImageOutput_write_image_0"
c_ImageOutput_write_image :: Ptr ImageOutput -> BaseType -> Ptr Void -> Bool

-- and then....
writeImage :: Int -> Int -> String -> IO Bool
writeImage xres yres name = do

           -- convert the name to a CString, and take out of IO Monad                                                                                                              
           uname <- newCString name
           uempty <- newCString ""

           -- allocate some memory                                                                                                                                                  
           let memSize = xres * yres * 8 * 3
           mem <- mallocBytes memSize

           -- create an ImageSpec object                                                                                                                                            
           spec <- return $ c_ImageSpecCreate (mkCInt xres) (mkCInt yres) (3::CInt) nullPtr

           -- create an ImageOutput object                                                                                                                                          
           outImg <- return $ c_ImageOutputCreate uname uempty

           -- open it in memory...                                                                                                                                                  
           openbool <- return $ c_ImageOutput_open outImg uname spec create

           -- write the image to file                                                                                                                                              
           writebool <- return $ c_ImageOutput_write_image outImg uint8 mem

           -- close the file                                                                                                                                                        
           closebool <- return $ c_ImageOutput_close outImg

           -- release mem                                                                                                                                                          
           free uname
           free mem

           return (closebool && writebool && openbool)



Drew Haven

unread,
Oct 29, 2011, 2:28:18 AM10/29/11
to stanford-1...@googlegroups.com
I'd have to go over the notes again, but have you tried making them IO operations and then calling them directly instead of through return?  I believe it's just a matter of adding IO to the type signature and maybe adding another keyword in the FFI binding.

Drew Haven
drew....@gmail.com

Phaedon Sinis

unread,
Oct 29, 2011, 2:32:12 AM10/29/11
to stanford-1...@googlegroups.com
I did that just after sending my question -- I'm now saving garishly colored checkerboard images with alpha transparency from Haskell!

If anyone needs help with the FFI for projects, feel free to contact me.

Drew Haven

unread,
Oct 29, 2011, 2:33:53 AM10/29/11
to stanford-1...@googlegroups.com
Garishly colored checkerboards! I'll trade you a limited edition picture of a pony for one.  Unfortunately, it isn't as cool as the one David has.

Drew Haven
drew....@gmail.com
Reply all
Reply to author
Forward
0 new messages