ONE-SHOT Algebraic Effects for Lua!
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
example
src
test
.gitignore
LICENSE
README.md
eff-2.0-11.rockspec

README.md

eff.lua

ONE-SHOT Algebraic Effects for Lua!

installation

$ luarocks --local install eff

usage

eff provides four objects, Eff, UncaughtEff, perform and handler.

effect definition and invocation

Eff requires the effect name and returns the effect instance. perform invoke the passed effect.

local Write = Eff("Write") -- definition
perform(Write("Hello!")) -- invocation

effect handler

handler(eff, value-handler, effect-handler)

handler requires the handling effect eff, value handler and effect handler, and returns the closure that requires thunk and crush the thunk, with handling eff.

local printh = handler(Write,
  function(v) print("printh ended", v) end,
  function(k, arg)
    print(arg)
    k()
  end)

printh(function()
  local x = perform(Write("hello"))
  return x
end)

--[[ prints:
hello
printh ended    nil
]]

The continuation effect handler received is ONE-SHOT, in other words, the continuatoin cannot run twice.

handler(Write,
  function(v) print("printh ended", v) end,
  function(k, arg)
    print(arg)
    k()
    k() -- call continuation twice
  end)
(function()
  perform(Write("Foo"))
end)

--[[prints
lua: ./eff.lua:91: ./eff.lua:82: continuation cannot be performed twice
stack traceback:
        [C]: in function 'error'
        ./eff.lua:91: in local 'printh'
        ../example/example.lua:28: in main chunk
        [C]: in ?
]]

LICENSE

MIT