Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions example/example.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local eff = require('eff')
local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
local inst, perform, handler = eff.inst, eff.perform, eff.handler

local Write = Eff("Write")
local Write = inst()

local test = function()
local x = perform(Write("hello"))
Expand All @@ -24,15 +24,15 @@ function(k, arg)
k()
end)

local Choice = Eff("Choice")
local Choice = inst()

local choiceh = handler(Choice,
function(v) return v end,
function(k, l, _)
k(l)
end)

local Any = Eff("Any")
local Any = inst()

local anyh = handler(Any,
function(v) print("anyh ended", v) return v end,
Expand Down
4 changes: 2 additions & 2 deletions example/exception.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ algebraic-exception for Lua
local exn
do
local eff = require('eff')
local Eff, perform, handlers = eff.Eff, eff.perform, eff.handlers
local inst, perform, handlers = eff.inst, eff.perform, eff.handlers

local handlers_ = function(...)
local effeffhs = {...}
Expand All @@ -23,7 +23,7 @@ do

exn = { raise = perform
, handlers = handlers_
, Exception = Eff
, Exception = inst
}
end

Expand Down
27 changes: 27 additions & 0 deletions example/monadic_pcall.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
local eff = require('eff')
local inst, perform, handler = eff.inst, eff.perform, eff.handler

local Pcall = inst()
local epcall = function(f, ...)
return perform(Pcall(f, ...))
end

local pcallh = handler(Pcall,
function(v) return v end,
function(k, f, ...)
local ok, content = pcall(f, ...)
if ok then
return k(content)
else -- error
return nil, content
end
end)

pcallh(function()
local file = epcall(io.open, you_cannot_read, "r")
for l in file:lines() do
print(l)
end

file:close()
end)
38 changes: 38 additions & 0 deletions example/nilcheck.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
local eff = require('eff')
local inst, perform, handler = eff.inst, eff.perform, eff.handler

local Fetch = inst()
local fetch = function(it) return perform(Fetch(it)) end

local Fallback = inst()

local fetchh = handler(Fetch,
function(v) return v end,
function(k, it)
if it then
return k(it)
else
return k(perform(Fallback()))
end
end)

local runfallback = function(default, th)
return handler(Fallback,
function(v) return v end,
function(k)
return k(default)
end)(th)
end

local t = {1, 2, 3}
local u = {4, 5, 6}

local result = runfallback(0, function()
return fetchh(function()
local x = fetch(t[3])
local y = fetch(u[4])
return x + y
end)
end)

print(result)
6 changes: 3 additions & 3 deletions example/shiftreset.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local eff = require("eff")
local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
local inst, perform, handler = eff.inst, eff.perform, eff.handler

local inspect = require("inspect")

local sr
do
local new_prompt = function()
local Shift0 = Eff("Shift0")
local Shift0 = inst()

return {
take = function(f) return perform(Shift0(f)) end,
Expand Down Expand Up @@ -84,7 +84,7 @@ print([[

local promless
do
local Shift0 = Eff("Shift0")
local Shift0 = inst()

local shift0 = function(f)
return perform(Shift0(f))
Expand Down
8 changes: 4 additions & 4 deletions example/typeclass.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
local eff = require('eff')
local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
local inst, perform, handler = eff.inst, eff.perform, eff.handler

print([[
monoid
=====]])

do
local Empty = Eff("Empty")
local Concat = Eff("Concat")
local Empty = inst()
local Concat = inst()

local empty = function()
return perform(Empty())
Expand Down Expand Up @@ -81,7 +81,7 @@ fmap
===]])

do
local Map = Eff("Map")
local Map = inst()

local map = function(f, fa)
return perform(Map(f, fa))
Expand Down
8 changes: 4 additions & 4 deletions spec/async_await.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- https://github.com/ocamllabs/ocaml-effects-tutorial/blob/master/sources/solved/async_await.ml

local eff = require('src/eff')
local Eff, perform, handlers = eff.Eff, eff.perform, eff.handlers
local inst, perform, handlers = eff.inst, eff.perform, eff.handlers

local imut = require('spec/utils/imut')
local ref = require('spec/utils/ref')
Expand All @@ -16,17 +16,17 @@ local Done = function(a)
return { a, cls = "done" }
end

local Async = Eff("Async")
local Async = inst()
local async = function(f)
return perform(Async(f))
end

local Yield = Eff("Yield")
local Yield = inst()
local yield = function()
return perform(Yield())
end

local Await = Eff("Await")
local Await = inst()
local await = function(p)
return perform(Await(p))
end
Expand Down
4 changes: 2 additions & 2 deletions spec/fringe.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-- https://github.com/ocamllabs/ocaml-effects-tutorial/blob/master/sources/solved/fringe.ml

local eff = require('src/eff')
local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
local inst, perform, handler = eff.inst, eff.perform, eff.handler

local Yield = Eff("Yield")
local Yield = inst()

local generate = function(iter, c)
local step = { f = nil }
Expand Down
4 changes: 2 additions & 2 deletions spec/generator.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- https://github.com/ocamllabs/ocaml-effects-tutorial/blob/master/sources/solved/generator.ml

local eff = require('src/eff')
local Eff, perform, handler = eff.Eff, eff.perform, eff.handler
local inst, perform, handler = eff.inst, eff.perform, eff.handler

--[[
iter : ('a table, 'a -> 'b) -> ()
--]]
local Yield = Eff("Yield")
local Yield = inst()

local generate = function(iter, c)
local step = { f = nil }
Expand Down
8 changes: 4 additions & 4 deletions spec/state2.lua
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
-- https://github.com/ocamllabs/ocaml-effects-tutorial/blob/master/sources/solved/state2.ml

local eff = require('src/eff')
local Eff, perform, handlers = eff.Eff, eff.perform, eff.handlers
local inst, perform, handlers = eff.inst, eff.perform, eff.handlers

local imut = require('spec/utils/imut')

local State = function()
local Get = Eff("Get")
local Put = Eff("Put")
local History = Eff("History")
local Get = inst()
local Put = inst()
local History = inst()

local get = function()
return perform(Get())
Expand Down
14 changes: 7 additions & 7 deletions src/eff.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local unpack = function(t)
end
end

local Eff
local inst
do
local __tostring = function(self)
return tostring(self.eff)
Expand All @@ -18,9 +18,9 @@ do
local v = {}
v.cls = ("Eff: %s"):format(tostring(v):match('0x[0-f]+'))

Eff = setmetatable(v, {__call = function(self, eff)
inst = setmetatable(v, {__call = function(self)
-- uniqnize
eff = ("%s: %s"):format(eff, tostring{}:match('0x[0-f]+'))
local eff = ("instance: %s"):format(tostring{}:match('0x[0-f]+'))
local _Eff = setmetatable({eff = eff}, {__index = self})

return setmetatable({--[[arg = nil]]}, {
Expand Down Expand Up @@ -62,7 +62,7 @@ do
end

local is_eff_obj = function(obj)
return type(obj) == "table" and (obj.cls == Eff.cls or obj.cls == Resend.cls)
return type(obj) == "table" and (obj.cls == inst.cls or obj.cls == Resend.cls)
end

local function get_effh(eff, effeffhs)
Expand Down Expand Up @@ -109,7 +109,7 @@ handler = function(eff, vh, effh)
return vh(r)
end

if r.cls == Eff.cls then
if r.cls == inst.cls then
if is_the_eff(r.eff) then
return effh(function(arg)
return continue(gr, arg)
Expand Down Expand Up @@ -166,7 +166,7 @@ handlers = function(vh, ...)
return vh(r)
end

if r.cls == Eff.cls then
if r.cls == inst.cls then
local effh = get_effh(r.eff, effeffhs)
if effh then
return effh(function(arg)
Expand Down Expand Up @@ -206,7 +206,7 @@ end


return {
Eff = Eff,
inst = inst,
perform = yield,
handler = handler,
handlers = handlers
Expand Down