ruff

Gem Version

ONE-SHOT Algebraic Effects Library for Ruby!

require "ruff"

Double = Ruff.instance
Triple = Ruff.instance
Log = Ruff.instance

h1 = Ruff.handler
  .on(Double){|k, v| k[v * 2] }
  .on(Triple){|k, v| k[v * 3] }

h2 = Ruff.handler.on(Log){|k, msg|
  k[]
  puts "logger: #{msg}"
}

h1.run{
  h2.run{
    v = Double.perform 2
    Log.perform (v + 2)
    puts Triple.perform 3
  }
}
# ==> prints
# 9
# logger: 6

Feature

One-shot algebraic effects

You can access the delimited continuation which can run only once. Even the limitation exists, you can write powerful control flow manipulation, like async/await, call1cc.

We have an formal definition for the implementation, by showing a conversion from algebraic effects and handlers to asymmetric coroutines. See here (in Japanese).

Subtyping on effects

You can define an sub effect for another effect. It enables to make an effect hierarchy, such as Exceptions in Java. This implementation is based on a calculus λσ<: (Description (in Japanese)).

Pre-defined effect and handlers

We provide some ready-to-use effect and handlers. You can use quickly powerful control flows.

LICENSE

MIT