Elm Language Bonsai

I had a take at the elm language papers, and realised the FRP construct doesn't require a new language to be potentially useful. I implemented some combinators in python. The source code is available in elm-bonsai repository.

I am especially pleased about how this abstraction can combine interaction patterns and their visualizations together and make it easy to alter them, but all the things I tried were trivial. I do not know what would happen if you tried to apply this to something complex, although everything composes pleasantly together.


Elm bonsai mainly consists of signals, cells, reactions and reaction groups:

Signals are combined but not mutated, forming directed acyclic signal graphs.


During spawning a signal graph is traversed in postorder. Exactly one cell is constructed for every signal node that is reached and appended into a list. The cells reference other cells during updating their contents.

To make it simpler to mix signals and constants, every non-signal object in signal graph is converted into a constant cell.


There are one or more reaction groups that receive SDL and timing events. These reaction groups relay every event to reaction objects that they contain. Every reaction iterates through its list of cells with the given event.

If the cell is an input cell, it may react to the event. If it reacts it has to set itself changed, or not changed if it did not react.

If the cell is any other kind of cell, it checks whether input cells have changed and if they are, re-evaluates its value. Cell flag changes according to whether the value changed.


Every reaction or reaction group can be discarded. The group describes itself as inactive and it is dropped out in the subsequent update.


The reaction groups take care of input but do nothing to describe what is displayed. So there are layers. The layers can be used to combine signals into elements. During spawning a signal, a displayable is created and associated to the reaction that is created. The displayable samples values from the cells of the reaction. As-it the mechanism can be used to implement pushbuttons or formatted text displays from the cell values.

Things whose difficulty not affected

If you wanted to create a verlet simulator on this system, it wouldn't be easier than before. Hooking the particles and links between them into signal graph would require something similar to what's used to display results now. It'd be structurally a kind of an entity component system.

Overall if there are only few values that depend on others, with accumulating state in the middle, the FRP model presented works all right. It's also quite fun to work with when it works.

Similar posts