
Hi, I would like to use FFI for the first time. Can someone give me a really, really simple complete example? I want to be able to take a simple C program and access a function from it in Haskell. A simple example of the other way around would be nice, too, but I don't happen to need that just now. I started reading the Addendum, but that looks like it would be a lot easier to read if I saw a simple complete example first. Everything I found on the wiki seems way beyond that - maybe I missed it. I am using GHC. I am not using Windows. Thanks, Yitz

I would like to use FFI for the first time. Can someone give me a really, really simple complete example?
Everything I found on the wiki seems way beyond that - maybe I missed it.
I am using GHC. I am not using Windows.
From the old wiki: http://www.haskell.org/hawiki/FfiCookbook http://www.haskell.org/hawiki/FfiExample http://www.haskell.org/hawiki/FfiTutorial http://www.haskell.org/hawiki/FfiWithArrays
From the new wiki, this search shows you pages with titles starting with F: http://www.haskell.org/haskellwiki/?title=Special%3AAllpages&from=f&namespace=0
Is this the stuff you're referring to which seems to be "way beyond a simple example"? The FFI tutorial on the old wiki seems to be quite simple. If you find the pages on the old wiki useful, then please move them over to the new wiki. Or feel free to write some new pages that fill the gaps you find. There's quite a bit of Haskell code about which does FFI. From memory: darcs, and the various database libs (HSQL, HDBC, Takusen). Alistair

Hello Yitzchak, Friday, February 9, 2007, 3:23:53 PM, you wrote:
I would like to use FFI for the first time. Can someone give me a really, really simple complete example?
nothing can be easier main = print (c_mysin 1.0) foreign import ccall "mysin.h mysin" c_mysin :: Double -> Double mysin.c: double mysin(double x) { return sin(x); } mysin.h: double mysin(double x); compilation: ghc -c mysin.c ghc main.hs mysin.o if you need to call imperative function (wjich returns different values for different calls with the same parameters) - just add IO to return type: foreign import ccall "myrnd.h rnd" c_rnd :: Double -> IO Double in C, function defined in the same way as in previous example look also at "Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell" http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdo... -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

bulat.ziganshin:
Hello Yitzchak,
Friday, February 9, 2007, 3:23:53 PM, you wrote:
I would like to use FFI for the first time. Can someone give me a really, really simple complete example?
nothing can be easier
main = print (c_mysin 1.0)
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... -- Don

Hello Donald, Saturday, February 10, 2007, 11:21:58 AM, you wrote:
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...
conceptually - yes. practically, it should be the same for all current platforms. of course, for maintained programs it's better to use safer approach -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Am Samstag, 10. Februar 2007 09:21 schrieb Donald Bruce Stewart:
bulat.ziganshin:
Hello Yitzchak,
Friday, February 9, 2007, 3:23:53 PM, you wrote:
I would like to use FFI for the first time. Can someone give me a really, really simple complete example?
nothing can be easier
main = print (c_mysin 1.0)
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. Cheers, S.

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

Hello Yitzchak, Sunday, February 11, 2007, 4:14:39 AM, you wrote:
when I have time. What Bulat wrote is in my opinion _exactly_ what is needed.
i has a pedagogical talent :D searching for Haskell teacher position :))) -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

| 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.

On 2/19/07, Yitzchak Gale
Simon Peyton-Jones wrote:
Yitz, Please do make time to do this! This is the moment, while it is still fresh in your mind.
Of course, you are correct. Thanks for the push. I am a bit busy with work, but the information is not lost. I'll have it up soon.
I just did the same thing (started using FFI) and I had some questions, so I thought I would put up a slightly flawed wiki page and let people answer my questions by fixing the problems! The page is "FFI Introduction", and the question is how to nicely integrate make to compile the C files and ghc --make for the haskell ones. Oops, I just realized I didn't use the recommended "Using_the_FFI" (it didn't show up on the wiki search, but I'm new to mediawiki...)! Oh well, it can be merged if necessary. "Using" seems to concentrate on calling haskell from C, mine is about calling C from haskell, and is more basic.

... and now I have more questions. Maybe it would be better if I just asked them on the mailing list, and then incorporated the answers into the wiki myself. Question #1 is compiling FFI using modules. I'd like to use ghc --make for the speed and convenience, but obviously it doesn't know how to compile the C. So my hybrid approach has been to write a makefile for the C, and then make the haskell program depend on the .o files and invoke ghc --make on them, like so: _dummy_target: cfile.o ghc -Ic_includes -Lc_libs --make -main-is HsMain -o target HsMain.hs cfile.o -lclib Unfortunately, ghc doesn't seem relink the target if cfile.o changed, so as a hack I put 'rm target' before the ghc line to make it link every time. Does anyone have a better solution? #2 is structs. What's a good way to marshal structs? The straightforward way would be C stubs like "T get_t(struct foo *x)" for every field of the struct, but clearly at any kind of scale something like greencard will be necessary (as an aside, greencard's last update seems to have been "support this newfangled FFI"... is it still alive and just "done" or are people using something else nowadays?). Hopefully in can be made efficient though (I don't know if cross C-haskell inlining will happen, but maybe with -fvia-c?). The other issue is that the haskell type will not be Storable, so the Foreign.Array stuff won't work... I need a separate sizeof C stub and allocaBytes. It's much easier to declare e.g. "type SomeCStruct = Word64" and then use Bits to pick out the fields in pure haskell, but I imagine this assumes way too much about how the struct is layed out in memory.

On Mon, Feb 26, 2007 at 01:02:57PM -0800, Evan Laforge wrote:
Question #1 is compiling FFI using modules. I'd like to use ghc --make for the speed and convenience, but obviously it doesn't know how to compile the C. So my hybrid approach has been to write a makefile for the C, and then make the haskell program depend on the .o files and invoke ghc --make on them, like so:
_dummy_target: cfile.o ghc -Ic_includes -Lc_libs --make -main-is HsMain -o target HsMain.hs cfile.o -lclib
Unfortunately, ghc doesn't seem relink the target if cfile.o changed, so as a hack I put 'rm target' before the ghc line to make it link every time.
Does anyone have a better solution?
Yeah. GHC knows how to call GCC on .c files, so just do: ghc -Ic_includes -Lc_libs --make -main-is HsMain -o target HsMain.hs cfile.c -lclib Don't worry about recompilation checking, GCC is two orders of magnitude faster than GHC so the extra time will be neglible unless your program is 95% C.
#2 is structs. What's a good way to marshal structs? The straightforward way would be C stubs like "T get_t(struct foo *x)" for every field of the struct, but clearly at any kind of scale something like greencard will be necessary (as an aside, greencard's last update seems to have been "support this newfangled FFI"... is it still alive and just "done" or are people using something else nowadays?). Hopefully in can be made efficient though (I don't know if cross C-haskell inlining will happen, but maybe with -fvia-c?). The other issue is that the haskell type will not be Storable, so the Foreign.Array stuff won't work... I need a separate sizeof C stub and allocaBytes.
It's much easier to declare e.g. "type SomeCStruct = Word64" and then use Bits to pick out the fields in pure haskell, but I imagine this assumes way too much about how the struct is layed out in memory.
I don't know about GreenCard, but hsc2hs is still very much alive and also addresses that goal.

Hello Evan, Tuesday, February 27, 2007, 12:02:57 AM, you wrote:
Unfortunately, ghc doesn't seem relink the target if cfile.o changed, so as a hack I put 'rm target' before the ghc line to make it link every time.
it's a bug. i don't know whether it's already fixed, try it with the latest 6.6 snapshot
#2 is structs. What's a good way to marshal structs?
this depends on your actual task. in particular, you can write instances of Storable for your structures: instance (Storable a, Storable b) => Storable (a,b) where sizeOf (x,y) = sizeOf x + sizeOf y ...... hsc2hs translator allows you to use offsets in C structures, see http://therning.org/magnus/archives/238 -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Fri, Feb 09, 2007 at 14:23:53 +0200, Yitzchak Gale wrote:
Hi,
I would like to use FFI for the first time. Can someone give me a really, really simple complete example?
I want to be able to take a simple C program and access a function from it in Haskell. A simple example of the other way around would be nice, too, but I don't happen to need that just now.
I started reading the Addendum, but that looks like it would be a lot easier to read if I saw a simple complete example first. Everything I found on the wiki seems way beyond that - maybe I missed it.
I am using GHC. I am not using Windows.
Shameless self-promotion: http://therning.org/magnus/archives/238 /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus@therning.org Jabber: magnus.therning@gmail.com http://therning.org/magnus

Hello Magnus, Thursday, February 15, 2007, 3:08:41 PM, you wrote:
I would like to use FFI for the first time. Can someone give me a
Shameless self-promotion: http://therning.org/magnus/archives/238
great intro, better than my own! -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

I want to be able to take a simple C program and access a function from it in Haskell. A simple example
The attachement is a file where the Haskell program first creates the C-source, compiles it to an shared object file, loads the object using the dynamic linker and uses the just constructed function. Regards, -- -- Mirko Rahn -- Tel +49-721 608 7504 -- --- http://liinwww.ira.uka.de/~rahn/ ---
participants (10)
-
Alistair Bayley
-
Bulat Ziganshin
-
dons@cse.unsw.edu.au
-
Evan Laforge
-
Magnus Therning
-
Mirko Rahn
-
Simon Peyton-Jones
-
Stefan O'Rear
-
Sven Panne
-
Yitzchak Gale