Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
FFI complete examples
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== When main is in Python == === Using ctypes === This is described at the Python wiki page [https://wiki.python.org/moin/PythonVsHaskell PythonVsHaskell], look for "Using both Python & Haskell with ctypes" === Using swig (and a c wrapper) === This example is adapted from [http://www.swig.org/tutorial.html The Swig Tutorial]. The factorial function has been modified to call into Haskell. Foo.hs: <haskell> {-# LANGUAGE ForeignFunctionInterface #-} module Foo where import Foreign.C.Types foreign export ccall hs_fact :: CInt -> CInt hs_fact :: CInt -> CInt hs_fact n = product [1..n] </haskell> example.c: <pre> #include "Foo_stub.h" void py_init(int argc, char *argv[]) { hs_init(&argc, &argv); } void py_exit() { hs_exit(); } int fact(int n) { return hs_fact(n); } </pre> example.i: <pre> %module example %{ /* Put header files here or function declarations like below */ extern int fact(int n); extern void py_init(int argc, char** argv); extern void py_exit(); %} %typemap(in) (int argc, char **argv) { /* Check if is a list */ if (PyList_Check($input)) { int i; $1 = PyList_Size($input); $2 = (char **) malloc(($1+1)*sizeof(char *)); for (i = 0; i < $1; i++) { PyObject *o = PyList_GetItem($input,i); if (PyString_Check(o)) $2[i] = PyString_AsString(PyList_GetItem($input,i)); else { PyErr_SetString(PyExc_TypeError,"list must contain strings"); free($2); return NULL; } } $2[i] = 0; } else { PyErr_SetString(PyExc_TypeError,"not a list"); return NULL; } } %typemap(freearg) (int argc, char **argv) { free((char *) $2); } extern int fact(int n); extern void py_init(int argc, char** argv); extern void py_exit(); </pre> run.py: <pre> import example import sys example.py_init(sys.argv) print example.fact(5) example.py_exit() </pre> To build (you may need to change the include path if you are using a different installation of python): <pre> $ ghc -c -dynamic -fPIC Foo.hs $ swig -python example.i $ gcc -fpic -c example.c example_wrap.c -I/usr/include/python2.6 -I`ghc --print-libdir`/include $ ghc -o _example.so -shared -dynamic -fPIC example.o example_wrap.o Foo.o Foo_stub.o -lHSrts-ghc6.12.3 $ python run.py </pre>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width