BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

Declarative Concurrency For Ruby With Dataflow

by Sebastien Auvray on May 04, 2009 |

Part of the Ruby language was influenced by functional programming techniques. Ruby programmers then adopted techniques from Erlang or Haskell with, or created bridges to these languages, eg. Erlectricity.

Larry Diehl brings a declarative concurrent model to Ruby by importing the concept of unification from Oz Language. Oz is a multiparadigm programming language. It is mainly known as a functional (lazy and eager evaluation), distributed, and concurrent programming language, but also supports constraint, logic, imperative and object-oriented programming.

In light of the rising number of processor cores, Larry wanted to take advantage of the declarative concurrency model in Ruby. The advantages are:

  • It is easy to reason about what the program does
  • Simple but powerful concurrency is possible

To accomplish this, Larry uses the concept of Dataflow threading behavior as described in Concepts, Techniques, and Models of Computer Programming:

What happens if an operation tries to use a variable that is not yet bound? From a purely aesthetic point of view, it would be nice if the operation would simply wait. Perhaps some other thread will bind the variable, and then the operationcan continue. This civilized behavior is known as dataflow.

And to realize this the Oz-way, he uses unification:

The idea of unification is to describe values by logical equations which can be resolved automatically by some unification algorithm

Practically this will be transcribed this way (from the project site):

# Local variables
include Dataflow

local do |x, y, z|
  # notice how the order automatically gets resolved
  Thread.new { unify y, x + 2 }
  Thread.new { unify z, y + 3 }
  Thread.new { unify x, 1 }
  z #=> 6
end

You create new variables with local or declare (for Instance variable), and use unify to bind variables.

Examples of Oz Ports (nondeterministic behavior), or Erlang-ish Actors  is provided.

Brian Morearty adresses some interesting debugging, performance, and memory concerns of Dataflow usage. Larry Diehl points out on Brian's blog:

One thing to note is that this library makes JRuby shine over MRI due to its green threads + native thread pool implementation.

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