RAAR Random Asymmetric Asemantic Research Lab
Chemlambda-js: a javascript library for graph-rewriting systems like chemlambda and interaction combinators. Marius Buliga (2025). DOI

Version: december 2025





This is a javascript library used for experimentation with graph-rewriting systems, seen as artificial chemistries, like chemlambda v2 and interaction combinators (IC) and directed interaction combinators (dirIC). For more informations about these "chemistries" go to the playable article (which uses this library) Alife properties of directed interaction combinators vs. chemlambda or, for more, to the page of chemlambda projects.

It is a continuation of chemlambda-gui, a repository of programs for simulation of chemlambda chemistries, where javascript was used only for visualisation of the history of rewrites, after these were computed by awk scripts.

History of the project and acknowledgements. After my initial implementation (awk,js) chemlambda-gui, there were several other implementations: (python) github.com/4lhc/chemlambda-py, (haskell) chemlambda-hask with discussion github.com/chorasimilarity/chemlambda-gui/issues/4 and youtube presentation, (js) chemlambda-editor and discussion github.com/chorasimilarity/chemlambda-gui/issues/9. Other discussions, like github.com/chorasimilarity/chemlambda-gui/issues/2 led to independent projects. Finally, this library started as a modification of the (js) version implementation, initially at github.com/mbuliga/quinegraphs repository. For the most recent and updated versions of chemlambda programs see the page of chemlambda projects. Parts of this projects are actively pursued by my RAAR AI lab. Special thanks to the other authors of chemlambda repositories listed here. Their work greatly enhanced this project and also validated it. The discussions, see those listed here or in the issues of the other repositories, helped very much to clarify the subject.

This library does all in javascript, which has pros and cons. Pro: it allows to visualise easier small graphs and interfere with the reduction process. Cons: it simplifies the algorithms of application of rewrites, in the sense that in the javascript version the rewrites are done sequentially, randomly or based on the age of elements which enter in the rewrite patterns. In the awk version there is the possibility to do as many rewrites as possible simultaneously, or based on criteria which modify the probability of new rewrites based on the rewrites already performed. While the awk version is closer to the goal of graph rewrites as purely local in space and time chemical reactions between individual molecules (graphs), the javascript version needs subtle modifications (like the sequentialization of rewrites, or a global notation of nodes and edges) which somehow depart from the original pure goal. Also, the javascript version does not function well for big graphs and it is much slower than the older awk version.

Not included in this library are lambda calculus and SKI calculus parsers.

The chemistries are described in the file js/chemistry.js, which includes detailed explanations. For example chemlambda rewrites are obtained by concatenation of "CHEMLAMBDABARE" with "CHEMLAMBDAEND" and dirIC rewrites are obtained from concatenation of "CHEMLAMBDABARE" with "DICMOD". General pure termination rewrites "T" and the "COMB" rewrites which manage the Arrow nodes are part of both chemistries.

The graphs are encoded as mol files. The mol notation is explained in js/nodes.js.

The library of graphs (as mol files) is in js/iceMol.js. Each mol file should be seen as the equivalent of a program.

js/icequine.js contains the main loop of reductions (ie graph rewrites) which use chemistry.js and nodes.js. General parameters are in js/parameters.js.

Each mol file from the library has comments attached, they are in js/iceComments.js.

Various IO functions and auxiliary functions are in js/pagelook-vs.js and js/ioprep.js. The visualisation is done with d3.js and a local copy of d3 and of jquery are found in the folder "other-libs-local-copies". The file js/myD3Graph.js uses these local copies for visualisation.




This is a minimal page which has the elements needed by the library, in order to work. Further are decribed these elements. For any use of the library you can style them as you wish, but they are needed, mainly by the functions in js/pagelook-vs.js and js/ioprep.js, for visualisation by js/myD3Graph.js or in the main loop js/icequine.js.

The element with id="citeas" contains the value selected by the menu:


The menu with id="listofmols" is a list of proposed mol file to be reduced.



The rewrites weights slider changes the likelihood of rewrites. The rewrites are grouped into two families: GROW, made of those who increase the number of nodes and SLIM, the rest of them (therefore those who keep the same or decrease the number of nodes). Each family has a probability, decided by the slider.

GROW     SLIM


Gravity slider. The graph is represented as a force-graph, under a gravity field. This slider changes gravity, which in combination with the mouse drag and mouse wheel allows to zoom and pan the graph. In lower gravity, if you zoom out then you shall see that the graph relaxes and you can see the overall shape of the graph. On the contrary, in higher gravity the graph nodes regroup in a more compact shape, which may be useful if you want to keep the whole graph visible, but also well see the nodes.

MIN     MAX


Buttons:

this changes the algorithm of rewrites. It has two states: "older is first", when among all the rewrites possible, it does first the rewrite involving the oldest graph edge, and "random choices" which allows the random selection of a rewrite.

random choices

change


this changes the chemistry. It has two states: "chemλ" for chemlambda and "dirIC" for dirIC. This is done with the function chemistryChoice() from js/pagelook-vs.js, where you can see that each chemistry may be a composite of other chemistries. In this case the chemistry of interaction combinators "IC" is part of both chemlambda and dirIC, therefore no matther which chemistry is chosen by this button, you can still reduce Lafont' quine.

chemλ

change


this changes the color of nodes as they appear in the visualization. It has two states: "colored nodes" where each node keeps it's original color and "uncolored nodes" where all nodes have the same color (ex. white). By switching from one state to another it is possible to see how the nodes of the graph are replaces, making visible metabolism like phenomena, where the structure of the graph is visible from the the edges even if the constituent nodes change.

colored nodes

change


this button converts IC graphs to dirIC graphs:




starts the reduction:




stops the reduction:




does only one step of the reduction:




reloads the initial graph:




Element with id="nodenumber" for the number of nodes.

nodes ∼

This element has id="errors", initially an element for the printing of errors (hopefully you will not see anything here) which later was used to also show the balance of nodes.



This element with id="maxnodenumber" defines the maximum number of nodes of the graph. If more then the rewrite algorithm stops.

(Rewrites stop at max. 1024 nodes)

This element with id="molyoulookat" shows the mol of the graph as a list with line separator "^".

CODE:

This element with id="comments" shows the comments associated with the mol file chosen.



This element with id="svgdiv" contains the main animation.

You can use the mouse to manipulate the image and the rewrites:

  • translate the whole image by pressing left click (anywhere in the element) and keeping it pressed while you drag the image
  • left-click pressed and drag on any graph node moves the node with the mouse
  • mouse wheel zooms in/out the image.
  • mouse hover over a node which is involved into a potential rewrite makes that rewrite to happen.




The following elements show how the reduction happens. They are necessary for the programs, because some functions read from them and some others write to them.

This id="puttransformcachealt" shows the list of possible rewrites and their age (which is the max age of the elements in the rewrite pattern), before any rewrite is chosen.

before:

This id="chosentransform" shows the rewrite which was chosen:

chosen:

This "puttransformcachealtafter" is the list of possible rewrites after the chosen one was performed:

after:

This id="molexport" is the mol before the rewrite (mind that the edges are renumbered by the programs)

mol before:

This id="molexportafter" is the mol after the rewrite:

mol after: