Sunday, May 25, 2025

Algebraic effects

Pixy Misa yesterday raised an event: "effect handlers". Apparently various software engineers have defined whole languages just for this, like Effekt and Ante.

If, like me, you work with C# or freakin' BASIC, you are working with an imperative language. The program starts, does stuff, ends. Your job - programmer - is to set the commands which run in sequence. In our machines, other processes might be listening in on your program; if nothing else, the operating-system is out there awaiting your keyboard, which might want to stop the program if it's broken and frozen.

Programs often have exception-handlers once called error-handlers. As a courtesy, you might want to trap incoming input where some idiot using your program has put in, oh, a zero where your program is dividing by the input. So you "throw" the exception before the procedure divides by that zero and crashes. C#, further, has delegate / event functionality: if it's done something important, it "raises" the event so that anyone listening can handle it in its own way.

If I am reading the documentation aright, then the "effect" coagulates exceptions and events. It leaves to the handler, not to the function throwing it, what to do. If there is simply nothing left to do, like that division about to be SINGULARITYed by a /0, it's an exception; if there's more to do, you put in a "resume()" command and it's an event.

Okay, you've consolidated a bunch of stuff. Have you really simplified it? We programmers view Sloth as a virtue; we don't waaaanna learn a whole new language. What raises Effekt over all our other blubs as C#-with-threads?

Ante explains why even do this: Bob Nystrom's colored functions. For those not going to read all that, I'll spoil it: they mean that horrific "async" zombie plague effect where, if your function can be async, those calling it must "await" it and, at best, be marked async too even if they're not. Nystrom thought that "await" wasn't a cure such that C# devs should all RETVRN to threads. Ante's term, for their own vaccine against the plague: polymorphic over effects.

They need a new language for this because they're talking to compilers: continuation-passing style, from the disco era. Ante implies their particular language isn't even imperative. So instead of (C) "void" or (structured-BASIC) "sub", it's "Task" - which they label "Unit".

No comments:

Post a Comment