[go: up one dir, main page]

0% found this document useful (0 votes)
58 views2 pages

Calling Haskell From C - HaskellWiki

This document describes how to call Haskell functions from C code by defining a Haskell function, exporting it for use in C using foreign export, compiling the Haskell code to generate C stubs, including the stubs in a C program and calling the Haskell function from C.

Uploaded by

jase21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views2 pages

Calling Haskell From C - HaskellWiki

This document describes how to call Haskell functions from C code by defining a Haskell function, exporting it for use in C using foreign export, compiling the Haskell code to generate C stubs, including the stubs in a C program and calling the Haskell function from C.

Uploaded by

jase21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Calling Haskell from C - HaskellWiki http://www.haskell.

org/haskellwiki/Calling_Haskell_from_C

From HaskellWiki

It is not uncommon to want to call a Haskell function from C code. Here's how to do that.

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

Note the foreign export. When GHC sees this, it will generate stubs for C, to help it work out the Haskell
types.

And call it from C:

#include <HsFFI.h>
#ifdef __GLASGOW_HASKELL__
#include "Safe_stub.h"
extern void __stginit_Safe(void);
#endif
#include <stdio.h>

int main(int argc, char *argv[]) {


int i;
hs_init(&argc, &argv);
#ifdef __GLASGOW_HASKELL__
hs_add_root(__stginit_Safe);
#endif

i = fibonacci_hs(42);
printf("Fibonacci: %d\n", i);

hs_exit();
return 0;
}

Now, first compile the Haskell file:

$ ghc -c -O Safe.hs

Which creates Safe_stub.c, Safe_stub.o, Safe_stub.h, 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 Safe.o Safe_stub.o -o test

(Alternatively, fewer files to enumerate:

1 of 2 6/17/2011 5:31 AM
Calling Haskell from C - HaskellWiki http://www.haskell.org/haskellwiki/Calling_Haskell_from_C

$ ghc --make -no-hs-main -optc-O test.c Safe -o test

Then run your C code:

$ ./test
Fibonacci: 267914296

And that's it.

Retrieved from "http://www.haskell.org/haskellwiki/Calling_Haskell_from_C"

Category: Tutorials

This page was last modified 03:26, 5 May 2011.


Recent content is available under a simple permissive license.

2 of 2 6/17/2011 5:31 AM

You might also like