Mixing C and Haskell code in the same file

Is it possible to insert C code directly to, say, .hsc files? I see this construct in ghc user docs: #def ... which should insert definitions, but I could not find examples of usage in Hackage. My goal is to write some cpp macros that build both a C function and its 'foreign import ccall' wrapper in the same file. Thanks for your help, Maurício

On Fri, 10 Jul 2009 15:24:51 +0200, Maurício
Is it possible to insert C code directly to, say, .hsc files? I see this construct in ghc user docs:
I think you can find your answer here: http://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html -- Reagrds, Henk-Jan van Tuyl -- http://functor.bamikanarie.com http://Van.Tuyl.eu/ --

Is it possible to insert C code directly to, say, .hsc files? I see this construct in ghc user docs:
I think you can find your answer here: http://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html
I did read that. It says I can use "#def" to insert a C definition, but there are no examples of use, and I could not find one. Maurício

Hello Maurício, Sunday, July 12, 2009, 9:11:53 PM, you wrote:
I did read that. It says I can use "#def" to insert a C definition, but there are no examples of use, and I could not find one.
#def inline int signof(int x) {return x<0?-1:x>0?1:0;} foreign import ccall safe "" signof :: CInt -> CInt main = print (signof 33) compilation: hsc2hs tabi.hsc ghc --make tabi.hs tabi_hsc.c -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

#def inline int signof(int x) {return x<0?-1:x>0?1:0;} foreign import ccall safe "" signof :: CInt -> CInt
Is it possible to get that #def as a result of a macro? Say, something like this: --- (WARNING: invalid code) #define ret(name,value,type) \ #def inline type name (void) {return value;} --- Thanks, Maurício

On 12/07/2009 19:26, Bulat Ziganshin wrote:
Hello Maurício,
Sunday, July 12, 2009, 9:11:53 PM, you wrote:
I did read that. It says I can use "#def" to insert a C definition, but there are no examples of use, and I could not find one.
#def inline int signof(int x) {return x<0?-1:x>0?1:0;} foreign import ccall safe "" signof :: CInt -> CInt
main = print (signof 33)
compilation: hsc2hs tabi.hsc ghc --make tabi.hs tabi_hsc.c
That use of "inline" doesn't have any effect since GHC 6.10, because even when using -fvia-C we're not #including the header file in the generated C code. Also that note in the docs about not using #def in library code is outdated: it should be safe to use #def in libraries now. I've fixed the docs. Cheers, Simon
participants (4)
-
Bulat Ziganshin
-
Henk-Jan van Tuyl
-
Maurício
-
Simon Marlow