[Haskell-cafe] FFI basics

Simon Peyton-Jones simonpj at microsoft.com
Mon Feb 19 03:58:38 EST 2007


| Thanks to everyone for all the help! Everything
| is working for me now. It turns out that the main
| detail I was missing was exactly what commands
| to type to compile it, and how to use it in GHCI.
| Pretty important detail, actually.
|
| Alistair - yes, there are a few simpler pages about
| FFI on the old wiki that I missed, thanks
| for the helpful links.
|
| I'll try to put it all up on the new wiki in a few days
| when I have time. What Bulat wrote is in my opinion
| _exactly_ what is needed. I'll incorporate the suggestion
| of Don and Sven, and then combine it with the two
| simpler pages from the old wiki.

Yitz, Please do make time to do this!  This is the moment, while it is still fresh in your mind.  You have learned something that wasn't obvious; it will save others going through the same loop if you write it down, and you are the ideal person to do so.

The obvious place to do so would be here
        http://haskell.org/haskellwiki/GHC/Using_the_FFI

PS: be sure to include Sven's advice in this thread (below)

Thanks

Simon


| > > foreign import ccall "mysin.h mysin"
| > >   c_mysin :: Double -> Double
| >
| > Shouldn't that be CDouble? At least for Int/CInt you can hit troubles on
| > 64 bit machines...
|
| Yes, the code above is wrong in the sense that it makes assumptions which are
| not guaranteed at all in the FFI spec. The rules to follow are extremely
| simple, so there is no reason to ignore them:
|
|    * If you want to use a C type "foo" in Haskell, use "CFoo" in the FFI
| (imported from Foreign.C.Types).
|
|    * If you want to use a Haskell type "Bar" in C, use "HsBar" in C code
| (#included from HsFFI.h).
|
| It depends on the application/library in question which alternative is easier,
| but never use a mix.


More information about the Haskell-Cafe mailing list