Newbie Question: Using Haskell Functions in a C Program

Hi, I'm in the process of writing a C program, but I can't stop thinking about how some functions would be much nicer implemented in Haskell. Is there a way to write some of the functions in Haskell and then use them in my C code via some kind of interface? BTW yes, I have been thinking about writing the whole program in Haskell, but I just don't have the level of experience in Haskell programming for that, since it's really heavy on IO. Thanks in advance for the answers! Regards Philip

Philip Müller
Is there a way to write some of the functions in Haskell and then use them in my C code via some kind of interface?
The beast you are looking for is called the FFI: Foreign Function Interface. C doesn't make IO any easier, though. More verbose and low-level, I would say. -- (c) this sig last receiving data processing entity. Inspect headers for past copyright information. All rights reserved. Unauthorised copying, hiring, renting, public performance and/or broadcasting of this signature prohibited.

Hello Philip, Friday, May 9, 2008, 2:17:41 AM, you wrote:
Is there a way to write some of the functions in Haskell and then use them in my C code via some kind of interface?
http://haskell.org/haskellwiki/IO_inside#Interfacing_with_foreign_evil_.28un... and then entries 1,6,7 in http://haskell.org/haskellwiki/IO_inside#Further_reading -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

mail:
Is there a way to write some of the functions in Haskell and then use them in my C code via some kind of interface?
Using C just for IO is a bit weird -- perhaps you could illustrate
the kind of IO you're doing? Learning how to do IO in Haskell is
a much safer solution that linking the Haskell runtime into your
C program.
That said, this is done by using 'foreign export' declarations
in your Haskell code, then linking the compiled Haskell objects
into your C code, as follows:
We define the fibonacci function in Haskell:
{-# LANGUAGE ForeignFunctionInterface #-}
module Safe where
import Foreign.C.Types
fibonacci :: Int -> Int
fibonacci n = fibs !! n
where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral
foreign export ccall fibonacci_hs :: CInt -> CInt
And call it from C:
#include "A_stub.h"
#include

Thanks for all the answers. I'm testing this right now and simples cases work as expected. However from what I've read it seems it'll get ugly once I try to pass a C array to a Haskell function. Well, maybe arrays in C have been ugly before trying to pass them to Haskell functions ;) To elaborate a bit about the C program, it's a small game using OpenGL for output and mouse and keyboard for input. I just don't know how to do that in Haskell - my knowledge is quite basic in nature ;) Regards Philip

mail:
Thanks for all the answers. I'm testing this right now and simples cases work as expected. However from what I've read it seems it'll get ugly once I try to pass a C array to a Haskell function.
Right, passing arrays back and forth is going to get tiring.
Well, maybe arrays in C have been ugly before trying to pass them to Haskell functions ;)
To elaborate a bit about the C program, it's a small game using OpenGL for output and mouse and keyboard for input. I just don't know how to do that in Haskell - my knowledge is quite basic in nature ;)
Oh, then you'll want to use the Haskell OpenGL bindings. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/OpenGL There's a few OpenGL-based games in Haskell, here: http://haskell.org/haskellwiki/Applications_and_libraries/Games and a section of blog articles on using the OpenGL bindings: http://haskell.org/haskellwiki/Blog_articles/Graphics#OpenGL -- Don

Hello Philip, Friday, May 9, 2008, 3:09:33 AM, you wrote:
Thanks for all the answers. I'm testing this right now and simples cases work as expected. However from what I've read it seems it'll get ugly once I try to pass a C array to a Haskell function.
http://haskell.org/haskellwiki/Modern_array_libraries read about foreign arrays -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

On Thu, May 8, 2008 at 5:09 PM, Philip Müller
Thanks for all the answers. I'm testing this right now and simples cases work as expected. However from what I've read it seems it'll get ugly once I try to pass a C array to a Haskell function.
Well, maybe arrays in C have been ugly before trying to pass them to Haskell functions ;)
To elaborate a bit about the C program, it's a small game using OpenGL for output and mouse and keyboard for input.
The SDL package (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/SDL) is quite nice for input handling and window making and whatnot. I have heard it is a bitch to get working on Windows, though. The OpenGL package (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/OpenGL) is good, but can be a real pain to get going because the documentation layout is terrible. To toot my own horn, if your graphics are simple enough (specifically, 2D), you can use graphics-drawingcombinators (http://hackage.haskell.org/cgi-bin/hackage-scripts/package/graphics-drawingc...), which has a simple interface, for output. It depends on SDL, so above caveat applies. But once you learn the pattern, I/O is basically a transliteration from C. The reason I struggle with such C-like libraries in Haskell is that I spend all my time trying to clean them up and make them more Haskell-like. I definitely consider FFI more of a pain (when you want to do anything sophisticated) than the OpenGL package, though. Luke
participants (5)
-
Achim Schneider
-
Bulat Ziganshin
-
Don Stewart
-
Luke Palmer
-
Philip Müller