← Back to context

Comment by jbrownson

15 hours ago

Everyone seems to be converging on React/Elm arch for UI. I saw a paper years ago for incremental lambda calculus (https://inc-lc.github.io) that I feel addresses the fundamental problem here more honestly. It's mostly intended for optimizing repeated computations after small changes to the inputs, but I think it applies nicely to UI. Our data model needs to be a change structure along with our UI model. We write a function from our data model to the UI model just like React, but we also get a function from deltas on the data model to deltas on the UI model. The main thing missing that we would need for UI is since we get a stream of deltas on both the data model and UI model (scrolling, selecting, etc) we need to merge them.

This way we don't have to zipper together each "frame" of the output tree trying to figure out which bit goes to which other bit, we get an explicit delta saying "the 5th thing got deleted". You do probably need a language with HKT for this.

I mentioned this to Phil Freeman years ago and he did a prototype in Purescript, but I haven't seen it anywhere else: https://github.com/paf31/purescript-purview

Another thing I'd like to see is a _truly_ immediate mode framework where each widget is a pure function to render it given its state, and it's up to the user to manage the UI state (scroll position, text cursor location, etc), you can mix it in with/ your data model, or whatever. Even egui/etc aren't truly immediate as they need to keep some sort of stable identity for events, UI state and focus. You could of course build a React like thing on top of that, but it would be helpful to have that for experimentation with wildly different state models without having to reinvent the text box and everything yet again. I've started some experimentation w/ this in Haskell and it's going well so far.

Incremental lambda calculus is a fascinating concept, I hadn't heard of it before.

A theory of changes for higher-order languages: incrementalizing λ-calculi by static differentiation - https://dl.acm.org/doi/10.1145/2594291.2594304

From the abstract, I get the impression it would require very granular atomic-level language transformations, that are only practical for languages that are designed from the ground up to support creating and applying such deltas. Rust and JavaScript/React seem overly complex for reducing down to that level of granularity, especially the latter for strictly typed and deterministic changes. Maybe a language like Haskell is more suitable, since I've heard that it (or a subset) can be compiled down to typed combinators as primitives.

> probably need a language with HKT

Ah, that sounds related to the thought above, that the technique may be best implemented at the level of language design. Does "HKT" mean "higher kinded types"? Closest I could find was a library for TypeScript: https://majorlift.github.io/hkt-toolbelt/

> _truly_ immediate mode framework where each widget is a pure function

It sounds like the "functional reactive" paradigm, which I think React is loosely based on and probably UI libraries like Imgui and others in Rust also. But none of them are "pure" like you describe because the languages they're built on are not pure all the way to the bottom. (Maybe Rust is, but it's likely too complex to practically "diff" and "patch" not only the data models but the running code incrementally, though I may be misunderstanding.)