BT

Empower Your Ruby With Haskell And Hubris

by Sebastien Auvray on Aug 26, 2009 |

Embedding C in Ruby or Rails applications is a way to fix performance bottle necks. RubyInline made this easy for C. The Ruby community is also tightly linked with various functional programming communities: Erlang, Caml, and Haskell. Bridges already exist for Erlang with Erlectricity, and for Objective Caml with rocaml. Apache's Thrift is another way to let Ruby communicate with other languages via RPC and a serialization format.

Mark Wotton wrote Hubris, a bridge which makes it possible to call Haskell code from Ruby. You'll need to install ghc which comes up with Haskell platform in order to be able to compile jhc (John's Haskell Compiler). You should notice that jhc will only work under Linux or Mac.

Once you have the requirements, you'll firstly write an Haskell file with some extra ccall exports where you define your function, ie Test.hs:

fibonacci :: Int -> Int
fibonacci n = fibs !! n
  where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

You'll then need to call jhc_builder.sh Test.hs in order to create the dynamic library (libdynhs.so).
After this you can write your Ruby code and take advantage of the exported function by loading the dynamic library with Ruby/DL:

require 'dl/import'
 
module HaskyPants
  extend DL::Importable
  dlload "./libdynhs.so"
  extern "int fibonacci_hs(int)"
end
 
puts HaskyPants.fibonacci_hs(12)

Mark will give a presentation about Hubris at next rorosyd in september. While still embryonic, the project will surely attract some early Haskell adopters.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT