I came across Effekt language and the idea of algebraic effects. But I'm not sure if I understood the concept correctly. I tried to re-implement the basic "hello world" example in Python and came up with this:
import contextlib
import contextvars
handlers = contextvars.ContextVar('handlers', default={})
def effect(name, *args, **kwargs):
try:
return handlers.get()[name](*args, **kwargs)
except KeyError:
raise RuntimeError(f'No handler for effect {name}')
@contextlib.contextmanager
def handle(**kwargs):
token = handlers.set(dict(**handlers.get(), **kwargs))
try:
yield
finally:
handlers.reset(token)
def example():
n = 0
produce = True
while produce:
produce = effect('Yield', n)
n += 1
return n
with handle(Yield=lambda n: n < 3):
example()
My question is, can this snippet qualify as a proper algebraic effect? If not, where am I wrong?