Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program

2008-05-11 Thread Luke Palmer
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: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program

2008-05-08 Thread Bulat Ziganshin
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


Re: [Haskell-cafe] Newbie Question: Using Haskell Functions in a C Program

2008-05-08 Thread Don Stewart
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 stdio.h

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

2008-05-08 Thread 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.
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

2008-05-08 Thread Don Stewart
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