Does Haskell contact Ruby through FFI?

Since Ruby and Haskell support FFI,

  • Is it possible to call Haskell code from ruby, maybe through FFI?
  • Is there a Haskell binding in Ruby?
+7
ruby haskell ffi libffi
source share
6 answers

I'm a little late for this discussion, but right now I'm writing a bridge between Ruby and Haskell. It is located at http://github.com/mwotton/Hubris - this is a binding that works at level C. However, at a very early stage of development.

+11
source share

GHC 6.12.1 supports the creation of dynamic libraries in Linux. Try something like:

Example.hs

{-# LANGUAGE ForeignFunctionInterface #-} module Example 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 

wrapper.c

 #include <stdlib.h> #include "HsFFI.h" void example_init (void) { hs_init (NULL, NULL); } void example_exit (void) { hs_exit (); } 

script.rb

 require 'dl' require 'dl/import' module Example extend DL::Importable dlload "./libffi-example.so" extern "void example_init()" extern "void example_exit()" extern "int fibonacci_hs(int)" end Example.example_init 1.upto( 40 ) do | x | puts "#{ x } -> #{ Example.fibonacci_hs x }\n" end Example.example_exit 

Makefile

 GHC=ghc-6.12.1 libffi-example.so: Example.o wrapper.o Example_stub.o $(GHC) -o $@ -shared -dynamic -fPIC $^ -lHSrts-ghc6.12.1 Example_stub.c Example_stub.h Example.o: Example.hs $(GHC) -c -dynamic -fPIC Example.hs Example_stub.o: Example_stub.c $(GHC) -c -dynamic -fPIC Example_stub.c wrapper.o: wrapper.c Example_stub.h $(GHC) -c -dynamic -fPIC wrapper.c clean: rm -f *.hi *.o *_stub.[ch] *.so 

Commands to Run

 make ruby script.rb 
+8
source share

I have not seen this before, but it is possible.

  • Use Haskell FFI to port libruby . Your main executable will be written in Haskell, which will call ruby_init() and related functions to start the Ruby executable. This allows you to run arbitrary Ruby code.
  • Use Ruby FFI to port the GHC module as a library . Your Ruby script should call hs_init() and is only available for foreign export ed functions.

You will need to write glue code, some in C, to get either of these two parameters.

  • Run Ruby and Haskell in separate processes, using some IPCs to communicate between them. Perhaps XML-RPC ( Haskell / Ruby ) or JSON ( Haskell / Ruby ) via sockets, or maybe even just with your own protocol.

I do not know what your requirements are, but this is what I would like to do - it is much easier.

+6
source share

I'm not sure about Haskell, but here's a cool video from Mountain West Ruby Conf 09 about working with FFI from Ruby. It looks like a pretty nice interface.

http://mwrc2009.confreaks.com/13-mar-2009-16-10-ffi-jeremy-hinegardner.html

+1
source share

@ephemient, I'm really looking to somehow use Ruby (high level + dynamic) to be the main logic of the controller and call haskell for a lot of data crunches (functional + speed)

I think that native binding is close to nonexistence, besides this tweet http://twitter.com/BlurredWeasel/status/1321484127

Using JSON RPC is likely to be the easiest way to implement where there is a thin shell in ruby ​​to (method_missing) to trigger haskell on JSON / Socket.

The advantage of JSON is that you can easily map primitives to native types between different languages.

 class SciHs def method_missing(method, *args) // json encode // request Haskell over tcp socket or unix pipes // json decode end end 

Another alternative for quickly crunching a ruby ​​number (+ functional style)

  • NArray or other scientific library in ruby
  • Linking the Ruby GNU Science Library
  • or go to ruby ​​1.9 and use YARV2LLVM to JIT calculate the logic in LLVM for faster execution.

Anyone's thoughts?

0
source share

I tried just that (I'm one of the tweet mentioned).

I did not think of the libruby approach, but I spent a lot of effort trying to use ruby ​​FFI to wrap the exported function from haskell and could never get it to all the links and run it.

If you look at the FFI haskell examples, you will see that they all include the C main () function. Since Ruby FFI does not (and cannot have) main (), this will not work. If you try without this, you will get strange link errors.

I can share what I have, ping me on freenode (cschneid) or twitter (BlurredWeasel).

0
source share

All Articles