Algebraic Effects for React Developers
1–10 of 98 posts
Re: Algebraic Effects for React Developers
#2https://en.wikipedia.org/wiki/Exception_handling#Termination...
Re: Algebraic Effects for React Developers
#3Re: Algebraic Effects for React Developers
#4Hooks are one of the strangest features I've seen in a language yet. I doubt they will be a lasting paradigm outside of the React ecosystem.
I like JSX a lot. The idea of reusable stateless components is totally on the right track.
I've found React to be very hard to debug. I get stack traces that don't seem to start from any code I've written.
I now somehow suspect that the next wave in the JS ecosystem will be doing something like microservices on the frontend - message passing between isolated webworkers? I ponder this because React has not, for me, achieved any level of enforcing a sane way of deciding which objects in memory belong to a given component. Something feels really wrong with the the React model of programming.
Writing a project in React seems doubly frustrating - there's the challenge of a new stack and new programming model, but I don't seem to be getting anything for the trade: the code isn't easily debuggable, doesn't have a great upgrade and maintainability story, the app still has race conditions, memory leaks, browser incompatibilities, tightly coupled components. The problem seems to be that React only gently encourages, but does not enforce, any good practices.
Re: Algebraic Effects for React Developers
#5"Algebraic effects" seems to just be a fancy/confusing name for resumable exceptions. FWIW, resumable exceptions have been used in the past but it seems they have been seen almost exclusively as a net-negative. https://en.wikipedia.org/wiki/Exception_handling#Termination...
Algebraic effects are a structured form of delimited continuations, for people who know about that concept. They give you nearly the full power of monads.
Re: Algebraic Effects for React Developers
#6"Algebraic effects" seems to just be a fancy/confusing name for resumable exceptions. FWIW, resumable exceptions have been used in the past but it seems they have been seen almost exclusively as a net-negative. https://en.wikipedia.org/wiki/Exception_handling#Termination...
I think the key is that the system lets you specify multiple resumption points, so then the exception handler can make an intelligent decision of how to resume.
Re: Algebraic Effects for React Developers
#7The one sentence summary I would use for algebraic effects is “they’re the way to add delimited continuations to a statically typed language.” Similarly, one might say that ML/ocaml/haskell style polymorphism [excluding typeclasses] is the way to take functions like map or filter from dynamically typed to statically typed languages.
I guess delimited continuations are an ok model for how react hooks work except that (as far as I’m aware) nothing continuation-like is involved. Instead one might think of them as scheduling work to be done very soon (in some sense) after your function runs.
An alternative question is: how do you type something like hooks? I think this mostly boils down to: how do you enforce the rules of hook usage with types? The rules are that: 1. Only certain functions may use hooks, 2. Any function which uses hooks must always create hooks (including hooks created by called functions) in exactly the same order every time it is run.
Algebraic effects aren’t really suitable for enforcing this rule (the OP has an example of state effects but there is only one state. It misses the killer feature of hooks which is to say in a relatively ad-hoc way, “I need a bit of state, please let me use some”. Instead you need to add an effect and handler for every bit of state or thread through access to a single bit of state (or have code where one must manually ensure that the rules are followed, eg a “useState” effect)).
I tried writing such a thing in ocaml about a year ago. I used a monadic interface with a second type parameter tracking the types of the states which are created. But this isn’t sufficient as you can still write rule-breaking code like:
function
| true -> map (useState 1) ...
| false -> map (useState 2) ...
You need to ensure that the type parameter tracking the calls is always abstract in such a way that the compiler will never determine that it is equal to a different useState. But I didn’t think of a way to do that and stopped looking.Re: Algebraic Effects for React Developers
#8Re: Algebraic Effects for React Developers
#9"Algebraic effects" seems to just be a fancy/confusing name for resumable exceptions. FWIW, resumable exceptions have been used in the past but it seems they have been seen almost exclusively as a net-negative. https://en.wikipedia.org/wiki/Exception_handling#Termination...
One important difference between algebraic effects and resumable exceptions is that with algebraic effects, the handler gets access to a reified version of the current continuation. The handler can then use that continuation to resume an arbitrary number of times, or not at all. For example, the handler could resume once for each item in a list, essentially using the continuation to map the list to a new list. The ha…
I think delimited continuations are a much more accurate description of what algebraic effects do. There are implementations of delimited continuations in Common Lisp based on the condition system but if you have too many continuations (or if you switch between them too often) you will blow up the stack. Other implementations (of async rather than continuations) use macros to translate source to continuation passing style and then rely on the compiler being clever, which sbcl is ok at.
Note that you can implement resumable exceptions so long as you have a reliable way to do nonlocal transfers of control (eg return-from or go in CL; or some kind of exception which can only be caught by a try that you write and not by someone else’s, though the finally would still run), and some dynamically scoped list of first clsss functions. Implementing delimited continuations is much more involved.
Re: Algebraic Effects for React Developers
#10So I think this is a good introduction to what algebraic effects are but I think it also misses the mark a bit. The one sentence summary I would use for algebraic effects is “they’re the way to add delimited continuations to a statically typed language.” Similarly, one might say that ML/ocaml/haskell style polymorphism [excluding typeclasses] is the way to take functions like map or filter from dynamically typed to s…
This also illustrates how weird it is to call function components pure. They’re not, there as effecful as can be!