Skip to content

Instantly share code, notes, and snippets.

@Nymphium
Last active May 25, 2019 13:08
Show Gist options
  • Save Nymphium/bb8235134121c57d8c470ee39b74f586 to your computer and use it in GitHub Desktop.
Save Nymphium/bb8235134121c57d8c470ee39b74f586 to your computer and use it in GitHub Desktop.
from algebraic effects to N-Barrelled CPS
-- support functions {{{
-- to find corresponding effect handler
local function lookup(h, eff, v)
local effh = h[eff]
if effh then
return function(k)
return effh(v, k)
end
else
error(("handler for ``%s'' not found"):format(eff))
end
end
local VALUE = "value"
-- to fetch value handler
local lookup_v = function(h, v)
return function(k)
return h[VALUE](v, k)
end
end
--}}}
--[[
let r =
print (
handle (perform (Foo 5)) with
| val y -> y * 20
| effect (Foo x) k -> k (x * x)
| effect (Bar b) k -> k (b - b)
)
--]]
-- new effects
local Foo = "foo"
local Bar = "bar"
local r = (function (k0, h0)
return (function(k1, h1)
return k1(5)
end)(function (v) -- let v = 5 in
return --[[perform (Foo v) ]] lookup(h0, Foo, v)(function(res)
return lookup_v(h0, res)(k0)
end)
end, h0)
end)(print,
{ [VALUE] = function(v, k) return k(v * 20) end
, [Foo] = function(x, k) return k(x + x) end
, [Bar] = function(b, k) return k(b - b) end })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment