RE: Re[2]: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-10 Thread Simon Marlow
On 09 March 2006 16:39, Bulat Ziganshin wrote:

 Hello Simon,
 
 Thursday, March 9, 2006, 5:08:09 PM, you wrote:
 
 for small home projects you can even use Int and int which
 would work in most of Haskell implementations
 
 Yes, and to give a concrete example:  HsInt and int are different
 types on x86_64 (and most other 64-bit C implementations).
 
 yes, it's better to use Int and long types :)

I hope that smiley means you're joking...

Cheers,
Simon
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re[2]: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-09 Thread Bulat Ziganshin
Hello Marcin,

Thursday, March 9, 2006, 2:20:00 AM, you wrote:

 foreign import ccall duma_init :: Int - IO Int

MQK HsInt duma_init(HsInt arg);

MQK Or use int on the C side and CInt on the Haskell side.

MQK fromIntegral can be used for converting integers in Haskell.

for small home projects you can even use Int and int which
would work in most of Haskell implementations


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-09 Thread Sven Panne
Am Donnerstag, 9. März 2006 08:46 schrieb Bulat Ziganshin:
 Thursday, March 9, 2006, 2:20:00 AM, you wrote:
  foreign import ccall duma_init :: Int - IO Int

 MQK HsInt duma_init(HsInt arg);
 MQK Or use int on the C side and CInt on the Haskell side.
 MQK fromIntegral can be used for converting integers in Haskell.

 for small home projects you can even use Int and int which
 would work in most of Haskell implementations

Uh, oh... :-( This is roughly as safe as assuming that short, int, long, and 
long long are of the same size, so this is highly discouraged. The rationale 
behind the FFI addendum is as follows:

 * If you want to use a given C API, use the types from Foreign.C.Types on the 
Haskell side in the foreign imports.

 * If the C API is under your control, you have basically two options: Either 
use Foreign.C.Types as above, or use the types/preprocessor #defines in the 
HsFFI.h header to bind to basic Haskell types. Which option is better suited 
depends on what you are trying to achieve.

This should be easy enough to use and everything else is not guaranteed to 
work...

Cheers,
   S.
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-09 Thread Simon Marlow

Sven Panne wrote:

Am Donnerstag, 9. März 2006 08:46 schrieb Bulat Ziganshin:


Thursday, March 9, 2006, 2:20:00 AM, you wrote:


foreign import ccall duma_init :: Int - IO Int


MQK HsInt duma_init(HsInt arg);
MQK Or use int on the C side and CInt on the Haskell side.
MQK fromIntegral can be used for converting integers in Haskell.

for small home projects you can even use Int and int which
would work in most of Haskell implementations



Uh, oh... :-( This is roughly as safe as assuming that short, int, long, and 
long long are of the same size, so this is highly discouraged. The rationale 
behind the FFI addendum is as follows:


 * If you want to use a given C API, use the types from Foreign.C.Types on the 
Haskell side in the foreign imports.


 * If the C API is under your control, you have basically two options: Either 
use Foreign.C.Types as above, or use the types/preprocessor #defines in the 
HsFFI.h header to bind to basic Haskell types. Which option is better suited 
depends on what you are trying to achieve.


This should be easy enough to use and everything else is not guaranteed to 
work...


Yes, and to give a concrete example:  HsInt and int are different types 
on x86_64 (and most other 64-bit C implementations).


Cheers,
Simon
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re[2]: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-08 Thread Bulat Ziganshin
Hello Brian,

Wednesday, March 8, 2006, 12:03:27 AM, you wrote:

BH mycallback :: GUIMonad m = EventInfo - m EventResult

BH 'm' will have to combine a state monad with the IO monad (so I can use IORef
BH etc if needed).

as Ian Lynagh wrote, it's no problem if your monad is IO-based. if FFI
by itself don't support this, you can add liftIO wrappers:

foreign import f :: IO ()

f_wrapper :: GUIMonad m = m ()
f_wrapper = liftIO f

-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-08 Thread Marcin 'Qrczak' Kowalczyk
Brian Hulley [EMAIL PROTECTED] writes:

 I've got a Haskell module with the following ffi import:

 foreign import ccall duma_init :: Int - IO Int

 However my problem is that I've got no idea what the type signature
 for the corresponding C function should be,

HsInt duma_init(HsInt arg);

Or use int on the C side and CInt on the Haskell side.

fromIntegral can be used for converting integers in Haskell.

-- 
   __( Marcin Kowalczyk
   \__/   [EMAIL PROTECTED]
^^ http://qrnik.knm.org.pl/~qrczak/
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-07 Thread Brian Hulley

Hi -
I've got a Haskell module with the following ffi import:

foreign import ccall duma_init :: Int - IO Int

However my problem is that I've got no idea what the type signature for the 
corresponding C function should be, and when I compile the above module with 
ghc -fglasgow-exts -fffi --make it doesn't create any stubs either.


I've tried looking at the wiki but that only seems to give specific 
examples. I'm trying to find what the mapping is between Haskell function 
signatures and C signatures is in general. Any ideas?


Also, I really wanted to be able to use () - IO () but () doesn't seem to 
be allowed in FFI...


A third point is, how would I pass an arbitrary monad instead of just using 
IO?


Thanks, Brian.


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-07 Thread Bulat Ziganshin
Hello Brian,

Tuesday, March 7, 2006, 7:35:27 PM, you wrote:

BH foreign import ccall duma_init :: Int - IO Int

int duma_init(int);

BH I've tried looking at the wiki but that only seems to give specific
BH examples. I'm trying to find what the mapping is between Haskell function
BH signatures and C signatures is in general. Any ideas?

see official FFI documentation - 
http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi.pdf
and excellent paper Tackling the awkward squad: monadic input/output, 
concurrency,
exceptions, and foreign-language calls in Haskell
(http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdorf.ps.gz)

BH Also, I really wanted to be able to use () - IO () but () doesn't seem to
BH be allowed in FFI...

void f(void);

foreign import ccall f :: IO ()

BH A third point is, how would I pass an arbitrary monad instead of just using
BH IO?

if you are sure what you do, you can even declare C function as pure:

foreign import ccall duma_init :: Int - Int

-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-07 Thread Tomasz Zielonka
On Tue, Mar 07, 2006 at 04:35:27PM -, Brian Hulley wrote:
 A third point is, how would I pass an arbitrary monad instead of just using 
 IO?

What for? IO is the monad that most closely matches the imperative, C
semantics. That's why FFI only supports the IO monad (and pure
functions).  Other monads (you say arbitrary) may use rather complicated
machinery to implement their semantics (HOFs, laziness, algebraic data
types). I don't think it's a good idea to try implementing their actions
in C (if that's what you want).

Why do you need that?

Best regards
Tomasz

-- 
I am searching for programmers who are good at least in
(Haskell || ML)  (Linux || FreeBSD || math)
for work in Warsaw, Poland
___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-07 Thread Ian Lynagh
On Tue, Mar 07, 2006 at 07:57:50PM +0100, Tomasz Zielonka wrote:
 On Tue, Mar 07, 2006 at 04:35:27PM -, Brian Hulley wrote:
  A third point is, how would I pass an arbitrary monad instead of just using 
  IO?
 
 What for? IO is the monad that most closely matches the imperative, C
 semantics. That's why FFI only supports the IO monad (and pure
 functions).  Other monads (you say arbitrary) may use rather complicated
 machinery to implement their semantics (HOFs, laziness, algebraic data
 types). I don't think it's a good idea to try implementing their actions
 in C (if that's what you want).
 
 Why do you need that?

I tend to wrap FFI imports with functions that can be called in any
MonadIO monad. I sometimes think I should suggest the FFI should be able
to do this itself, but given I'm generally needing to convert types,
allocate memory etc in these functions too the gain would be fairly
minimal.


Thanks
Ian

___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users


Re: How to find out the C type signature corresponding to a Haskell function type in FFI?

2006-03-07 Thread Brian Hulley

Bulat Ziganshin wrote:

Hello Brian,

Tuesday, March 7, 2006, 7:35:27 PM, you wrote:


foreign import ccall duma_init :: Int - IO Int


int duma_init(int);

Also, I really wanted to be able to use () - IO () but () doesn't
seem to be allowed in FFI...


void f(void);

foreign import ccall f :: IO ()


Thanks Bulat for these function prototypes (+ the links you posted). I'd 
thought that it would be complicated to deal with the IO monad but I see 
from these prototypes that it's not so complicated after all... :-)


Best regards,
Brian. 


___
Glasgow-haskell-users mailing list
Glasgow-haskell-users@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users