Skip to content

Example how to call Haskell from Python easily (using ctypes.cdll.LoadLibrary)

Notifications You must be signed in to change notification settings

ajohnson-uoregon/haskell-from-python

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

haskell-from-python

Example how to call Haskell from Python easily (using ctypes.cdll.LoadLibrary).

Originally from: https://bitbucket.org/darq/haskellfrompython

How to run the example

Use make to compile the Haskell and C helper code.

Then run python program.py to run the example.

You might need to adjust some paths or versions in the Makefile.

Explanation

The Haskell function we want to call is fibonacci :: Int -> Int in Example.hs, and there is corresponding FFI function fibonacci_hs.

This Haskell code is compiled into a libffi-example.so object file loaded with ctypes.cdll.LoadLibrary in Python. On the returned object, you can then call fibonacci_hs(5).

Because the main entry point is now from Python instead from Haskell, we have to start the Haskell runtime system ourselves. There is a small C wrapper wrapper.c to do that which is compiled into the object file together with the Haskell code.

Removing the C wrapper

You can also entirely remove the C wrapper, if you like, and immediately call from Python:

lib.hs_init(0, 0)        # Instead of lib.example_init()
# ...
lib.hs_exit()            # Instead of lib.example_exit()

It is your choice if you prefer this; the mediating C file type-checks it agains HsFFI.h so that you don't forget the (0, 0) arguments (this would result in a segfault) and that you get a compile-time error in case one of these functions changes in future.

About

Example how to call Haskell from Python easily (using ctypes.cdll.LoadLibrary)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Makefile 41.7%
  • Haskell 29.0%
  • Python 17.0%
  • C 12.3%