BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Declarative Concurrency For Ruby With Dataflow

Declarative Concurrency For Ruby With Dataflow

Leia em Português

This item in japanese

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.

Rate this Article

Adoption
Style

BT