Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
On Thu, May 8, 2008 at 5:09 PM, Philip Müller <[EMAIL PROTECTED]> 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. > > 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-drawingcombinators), 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re[2]: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
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, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
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 int main(int argc, char *argv[]) { int i; hs_init(&argc, &argv); i = fibonacci_hs(42); printf("Fibonacci: %d\n", i); hs_exit(); return 0; } Now, first compile the Haskell file: $ ghc -c -O A.hs Which creates some *.c and *.h headers, which you import into your C program. Now compile your C code with ghc (!), passing the Haskell objects on the command line: $ ghc -optc-O test.c A.o A_stub.o -o test How run your C code: $ ./test Fibonacci: 267914296 And that's it. -- Don P.S. Its easier to learn how to do IO in Haskell :) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program
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_.28under_development.29 and then entries 1,6,7 in http://haskell.org/haskellwiki/IO_inside#Further_reading -- Best regards, Bulatmailto:[EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe