r/ProgrammingLanguages • u/Honest_Medium_2872 • Jun 09 '26
Blog post Exploring How UI Frameworks Converge Toward DSLs
https://kura.tazz.codes/posts/01-exploring-first-class-ui.htmlAll,
While working on my first game engine, I ended up spending a lot of time thinking about UI systems and the tradeoffs between approaches like ImGui, WebKit, JSON-based descriptions, scripting languages, and custom solutions.
One observation I kept running into was that many UI frameworks seem to gradually evolve toward domain-specific languages, whether intentionally or not. Once you start introducing reusable components, control flow, composition, and abstraction, it often feels like you're creating a UI language disguised as something else.
I wrote up some thoughts on that design journey and why I think UI systems naturally converge toward DSLs.
Longer term, I'm interested in exploring what it means to treat UI as a first-class language feature and how that changes the role of the compiler.
I'd be interested to hear whether others have observed similar patterns in UI systems, configuration languages, or other DSL-heavy domains.
Thanks,
~ Tazz
16
u/tobega Jun 09 '26
html. Add possibility to define custom tags that are really just html snippets. Throw in parameters as attributes. This is pretty much what web frameworks converged to, and it is actually pretty good.
12
u/munificent Jun 09 '26
Not sure if the author is kidding or not, but Lua was originally designed in the 90s to be a configuration DSL.
11
u/aatd86 Jun 09 '26
You don't really need a dsl when you have higher order functions. Although the risk is to turn everything into a lisp.
5
u/tmarsh1024 Jun 09 '26
I have been thinking about these things for a number of years.. okay decades. I'm definitely more on the practitioner side as opposed to theoretician side, but I can share a few of my thoughts.
One is that there is sort of a universal setting you can imagine all these UI gadgets and tooling approaches living in (spoiler: it's the category Poly). You can look at Phil Freeman's and Arthur Xavier's work to try to understand how they approached this from a theoretical standpoint (but in a very limited setting). The applied category theory crew has great intuitions about this problem overall, but no practical approaches or answers (just tradeoffs).
You can also contrast imperative models (retained mode UIs where you create the widgets, then react to things and set properties on those instances - like updating a text field programmatically) with declarative models (like SwiftUI and the DSLs you mention).
So the problem tends to break down in a few recognizable ways and raises two important question: 1) is the UI widget tree imperative (the retained mode approach) or imperatively constructed in immediate mode (like ImGui), or 2) is it declarative as a DSL, and 3) how do you maintain state and side-effects.
The thing to recognize is that they the imperative vs. declarative (DSL) approaches are interchangeable and just present different ergonomics and guardrails. The declarative approach abandons state mutations which are hard to reason about and test, and which tend to scale poorly except in the hands of very talented and disciplined teams. This is the advantage of declarative approaches. However, imperative approaches are often great for simple problems and give you easy escape hatches when you want to "just do one quick thing".
We haven't touched on the third question from my list, which is about state management. This is different than the UI layout problem. SwiftUI, for example, or Angular, provide two-way data bindings and leave the state management architecture largely up to you. Here too, you can be imperative (frequently leading to monster view controllers, god objects, etc., in the pathological cases) or you can be declarative (state machines, reducer architectures, etc.).
And then you also need to layer in the question of side-effects (reified effects in Elm, continuation-like effects in TCA, middleware in Redux). What's interesting about reducers is they are just the same as the imperative code translated to continuation passing style and with labeled suspension points. So again, we see a kind of equivalence between the different styles, and you could even programmatically compile between the styles with the right kind of compiler (and, in fact, compilers are often doing exactly some of this work internally to translate code).
One wonders (and I'll try to tie it back to your question): if all these things are "sort of equivalent", what is the "best" approach? And the answer seems to really be as much cultural as technical. I would argue my preference (something closer to PointFree's TCA) is "better", but only by my metrics of testability. An imperative programmer might find a closer affinity to Uber RIBS and its different ceremony. A very formal team with a specific discipline might prefer something like XState.
But I think you are right to observe that we tend toward declarative code in DSLs for the same reasons we tend toward functional programming (which is declarative code) more generally: it composes better and more safely and gives you the right tools for a restricted domain. DSLs save you from having to write certain tests and are often correct by construction. The only downside is that it is alien to many and may require training. We have seen that the transition to DSLs has been gradual. It's also worth noting that sometimes DSLs fail us and it's simply easier to resort to monkey-brain mode and feed imperative instructions to the computer. We need both, but we have found that only one mode scales well.
Those are some slightly unorganized thoughts, but maybe a part of it resonates.
5
u/initial-algebra Jun 09 '26
The biggest problem with expressing UI in a typical programming language is order. Usually, the order in which you declare UI elements in code matches the layout order, which is fine. However, whenever this order diverges from the order in which elements behaviourally depend on one another, you have to contort your code by "lifting state up" or otherwise declaring dependencies separately from declaring layout. And that's assuming no circular/mutual dependencies, which are usually impossible to express without some imperative escape hatch (e.g. refs in React). Encapsulating behaviour and layout together into components is often not really feasible in complex scenarios; instead, your components are mostly just dumb blocks of layout with the behaviour declared in some top level context (e.g. Redux).
I know of exactly one UI system that solves this problem: Reflex-FRP in Haskell, which takes advantage of lazy evaluation (and recursive do syntax) to let you declare UI in layout order, but also use the behavioural outputs (e.g. a stream of button clicks, or a signal for the current contents of a text box) of components before they are declared. It's possible to create an infinite loop if you're not careful, but you can fully encapsulate behaviour and layout together. No lifting state up, no refs.
3
u/mc_chad Jun 09 '26
The idea was initial developed at Xerox PARC and with Smalltalk-80. The initial object-oriented programming was really for making it easier to build UIs. Then the ideas and code evolved. You can see with the Microsoft GUI toolkits over the years. Some used XML in place of a more traditional programming language but the result is similar.
A conference paper on point is Roberts, D., and Johnson, R. "Evolve frameworks into domain-specific languages". In Proceedings of the 3rd International Conference on Pattern Languages for Programming
3
u/MattDTO Jun 14 '26
This topic has been explored a ton. Look at Slint, Wasp, Clay UI, etc
2
u/farsightfallen Jun 09 '26
The DSL is basically some markup for some kid of renderer - QML, HTML, or prebuilt ui-component library for a framework like flutter/react-native, or the myraid of headless ui-component libs in js world (e.g. Radix UI). All of which share a lot of common elements. Because ultimately, UI design can be brought to a certain level of abstraciton that covers 99% of use cases (text box, input area, button, etc.).
The problem is marrying the component logic into the declarative langage. Which is why I think youve got it the other way around. It's not that languages need a DSL for UI, it's that UI needs a language, and the actual ui part is simple enough that it's easy to reimplement in different langauges because it's just a shell around the difficult part of component behavior.
I think you'd have to clarify on not just how the lanauge improves UI design, but how langauge specific features improve the targeted goal. Like I saw match in the comment you posted, and that's veering into why should this logic be done in your language, rather than setting up some integration with another mature language. I think match actually does make a lot of sense for UI design, but specially because UI is often broken down into events.
BTW it's funny that you mention this, because I am working on a programming language that's basically for UI design, so this is right up my alley. It's a language that's simple enough to be transpiled to other langs, but also augmented so that AST-derivatives (like the HIR) can be represented in a visual editor since UI design tends to require visual inspection. So e.g. things like color or transforms can be transformed in an explicit chain, and that shows up in the properties panel, or as a node graph like you'd find in game editors. separate to the programming lanage, is an asset system that covers more typical ui base components like Container, TextBox, etc. that have exporters to targets like web, and maybe Flutter or whatever.
2
u/SwimmingPermit6444 Jun 10 '26
Lua is actually a good language to write UI in because it's very flexible so there's no need for a DSL. Or maybe you could look at it as implementing a DSL inside Lua.
2
u/Tuhkis1 Jun 10 '26
Conflating dear imgui with imgui is bound to make someone angry.
I don't think that making a dsl is inherently bad or even unmaintainable unlike some other people. Though retrofitting these gui frameworks in gp languages isn't really an issue in my mind.
2
u/[deleted] Jun 10 '26 edited Jun 10 '26
I've had a lot of fun bending Lua as far as it can go (I think) for WoW addons:
https://github.com/weltensturm/DTT/blob/master/editor/DTTSidebar.lua
(The library itself is here: https://github.com/weltensturm/LQT/tree/master/LQT-1.0 )
I've never really released the stuff because I've stopped playing WoW, which makes me sad. The latest Expansion broke a lot of stuff too.
It's kind of a builder pattern but for classes, to actually instantiate stuff you would call ScriptEntry.new() for example. At the root of the chain is a creation call to one of WoW's UI primitives (unless it's a Style without constructor, which apply their "chain" to an existing element instead of a newly constructed one)
2
u/Inconstant_Moo 🧿 Pipefish Jun 09 '26
I was just thinking about this in a more general way. People will knock DSLs because of complexity and maintainability and onboarding and lack of documentation and support and so on and so forth. But you have to represent your domain objects somehow, it's going to be a bunch of object declarations in a framework, or a file full of JSON, or something, which will suffer from the same problems and won't be as good.
1
u/Clorofilla Jun 18 '26
Hello OP,
You are correct. UI concerns are so vast and tentaculars that there is no neat way to capture them. Many DSL rise and fall around the topic. Think of all the many dialects of HTML, CSS, JS and JS frameworks that exist just to manage the so called "frontend UI".
For me a few things are important to note when approaching the problem:
- UI is mostly states that hints at behaviors. Many interconnected states are an hard thing to manage, so UI will always be a hard thing to manage.
- The UI structure is never independent of the presentation. It may look like it in some specific situation (e.g. theming), but a button is a button only when looking like a button in relation to everything that surrounds it. When your button alignment makes it go outside of the viewport, making it unclickable... is it a structural or presentational issue? The border is fuzzy and such separation is leaky. Skip it entirely.
- Drafting, sketching, drawing or putting together UI via text will always be a bad experience. It is very tempting to spend time making the process easier but the returns of investment are terribile. Having a GUI tool for it is the real solution (see Figma and similar).
- Drawing the UI is not the big problem. Capturing its fickle, contextual, permutative and invisible states and behaviors in a manageable set of artifacts (source code or GUI) is the real challenge. For it we tend to build a very declarative DSL with a lot of hidden magic, so that with one idea we can define role, behavior and rendering. But then we need a slight variation on it, and we need a way to add manganale exceptions, which often grow so large that we end up fighting the declarative idea we created in the first place. Is and endless cycle. Hurt people hurt people. Hurt code hurt code.
- UI concerns are so varied and surprising that focused ad hoc solutions are often easier to manage. Ready made solution can give a big head-start, but their most important feature is how easy they allow you to break the rules they supposedly put in place so "you do not have to worry about UI". The first things one learn in HTML CSS is how to break all the convention that the designer of those languages put in place, so that it just renders exactly what you need. Not by accident the CSS bible was called "CSS tricks", as tricks it the correct mental model to use when assembling visual experiences.
You have a series of problem to solve which each have their separate solution:
And you need a way to make them play nice with each other, because for a human (despite interacting with all the above mentioned things in non trivial ways) a button is a simple thing and it should be trivial to add and change it.
Furthermore, you say that you are not just building a UI but a generic system to help building generic UIs. Basically the worse situation ever in terms of complexity.
For point 2. and 4. I would wonder why you do not use whatever you already have in your game engine. I would imagine that you already have an event system, that you already have some structured way to store, instantiate and update the nested entities that make up the levels and so on.
You may want some easy literal/defaults to quickly put out primitives in your UI (through code or through the engine GUI) this could have its own DSL syntax sugar, but once instantiated I would suggest you to make them behave as close as possible to the others standard entities in your engine code. The heavy separation between HTML and JS has always created more friction than anything else. People made the best out of it but languages like https://imba.io/ shows how unnecessary that was.
Finally, in case your engine language has static typing or similar, just accept that most of your UI will not. UI is a problem space which needs dynamic behaviors which pair poorly with strict typing. You can add checks and guards but not much else. It's similar to the testing problem, as UI is also very hard to test.
---
But yes UI is DSL oriented, the behaviors are just too many and describing them imperatively takes too long. So a new lexicon is always needed so you have a DSL. Even in Figma you need to learn a few abstract concept to be proficient with the software.
1
u/Appropriate-Rub-2948 Sep 10 '26
I think the optimal paradigm for highly interactive UIs is already known. It's finite state machines. More specifically, statecharts. Look at xstate. People who have tried it seem to rave about it.