Artificial chemistry experiments with chemlambda, lambda calculus, interaction combinators

03/31/2020 ∙ by Marius Buliga, et al. ∙ ProtonMail 0

Given a graph rewrite system, a graph G is a quine graph if it has a non-void maximal collection of non-conflicting matches of left patterns of graphs rewrites, such that after the parallel application of the rewrites we obtain a graph isomorphic with G. Such graphs exhibit a metabolism, they can multiply or they can die, when reduced by a random rewriting algorithm. These are introductory notes to the pages of artificial chemistry experiments with chemlambda, lambda calculus or interaction combinators, available from the entry page https://chemlambda.github.io/index.html . The experiments are bundled into pages, all of them based on a library of programs, on a database which contains hundreds of graphs and on a database of about 150 pages of text comments and a collection of more than 200 animations, most of them which can be re-done live, via the programs. There are links to public repositories of other contributors to these experiments, with versions of these programs in python, haskell, awk or javascript.

READ FULL TEXT VIEW PDF
POST COMMENT

Comments

There are no comments yet.

Authors

page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 How not to read these notes

These notes serve to clarify the basics and as a reference for the concepts used in the programs and pages of the library of experiments [13]. You only need a browser with the javascript turned on to start exploring these artificial chemistry experiments.

But if you don’t understand what are you looking at, then come back to these notes.

2 How you can contribute

Find new quines.

By going to the page How to test a quine, read the instructions. After this choose from the menu one of the variants which create random graphs, like this or this for chemlambda, or this for interaction combinators. Copy the CODE and mail it to me. If the quine is new then congratulations, you can name this new life form.

Create a new chemistry.

For this you have to read these notes. The programs are commented too. A new chemistry needs new nodes and new graph rewrites. The nodes are defined in the script nodes.js, as explained in section 7.1. The chemistries are defined in the script chemistry.js, as explained in section 8. You can use The quine lab to input new graphs, in mol format. Or you can clone or download the quinegraphs Github repository, then edit the chemistry.js, nodes.js to add a new chemistry, then edit the library of graphs to add new graphs. You can add comments for your new graphs in comments. I would like to see, for example, new chemistries like one for the project Formality or to invent a chemistry which would be able to graphically parse lambda terms as an enhancement of the lambda calculus to chemlambda parser.

Make artificial life animations.

The Chemlambda collection of animations contains hundreds of recordings of artificial chemistry simulations. You can remake such simulations and record the screen. There is only one rule: minimal editorial intervention. You can speed the screencast to fit into a short time and you have the freedom to chose the framing of the animation. In parameters.js you can modify the dimension of the simulations window, the gravity, the forces acting on the links of the graphs, or the color of the nodes. During the simulation, you can translate the graph by mouse drag, or you can zoom with the mouse wheel. The screencast can be then transformed into an animated gif, as an art creation.

Find bugs, suggest corrections.

Please inform me about research lines which are not mentioned here, but are relevant for the subject. I am a mathematician with a geometry and applied mathematics background, therefore help me to not reinvent the wheel too often. If you like programming, then contribute to one of the repositories or contact me.

Ask questions.

A good question is a great contribution. These experiments are the outcome of a research subject which has ramifications in fields which seem far away from artificial chemistry, like metric geometry. Maybe your research involves applications of graph rewriting in a new context.

3 Introduction

The experiments are bundled into pages, all of them based on a library of programs, on a database which contains hundreds of graphs and on a database of about 150 pages of text comments and a collection of more than 200 animations, most of them which can be re-done live, via the programs.

The entry page of the library [13] contains links to the following pages:

  1. Interaction combinators and chemlambda quine graphs, to explore various quine graphs from chemlambda and interaction combinators [23] and to generate new ones. Given a graph rewrite system, a graph G is a quine graph if it has a non-void maximal collection of non-conflicting matches of left patterns of graphs rewrites, such that after the parallel application of the rewrites we obtain a graph isomorphic with G. Such graphs exhibit a metabolism, they can multiply or they can die, when reduced by a random rewriting algorithm.

  2. How to test a quine, which allows you to check or to prove that a graph is indeed a quine graph or not

  3. The quine lab allows you to input or output an interesting graph or it’s reductions, with examples to play with. The mol format used to encode the graphs is a variant of linear graphs [2] [3], or port graphs [27] familiar to those interested in interaction nets [24] and linear logic [20].

For those interested in lambda calculus [15] there are two pages:

  1. Lambda calculus to chemlambda parser, to transform untyped lambda terms into chemlambda molecules, to reduce them graphically and then to output the result,

  2. The Ouroboros explains how the first chemlambda quine graph, the ouroboros, was discovered from the graphical reduction of a lambda term.

Finally, for those interested into the possibilities of these artificial chemistry, or into artificial life, there is a big collection of animations, with a more artistical nature. These animation are produced from simulations performed with the programs from the library, with a minimal editing concerning only the speed and the framing of the visual output. They come with abundant comments.

was produced initially as a Google+ collection, after an effort of public research communication. It is now enhanced by the possibility to re-done the animations in javascript, with the programs from the library.

The entry page contains links to more informations and, more importantly, to public repositories of other contributors to this experiment. You find there versions of these programs in python, haskell or my original programs in awk. I want to express my thanks for all contributions. This version of the programs, in javascript, is actually based on the first javascript-only port of my original programs. All the visualizations are done via the excellent d3.js.

4 About this project

The source of these experiments is the discovery of a graphical formalism for differential calculus in metric spaces known as sub-riemannian spaces. See [4] [5] [6], if you are more interested in geometry or analysis in metric spaces, or [7] for the most recent mix of lambda calculus with emergent algebras.

This formalism of dilation structures, or emergent algebras, composes graph rewriting with a passage to the limit to prove differential or algebraic statements. Trying to understand the computational power of this formalism I started from an algebraic point. Emergent algebras are families of idempotent right quasigroups, indexed by a topological commutative groups, along with a topological structure which allows uniform limits. This algebraic framework did not allow to understand the full generality of the graphical formalism. Therefore I changed to an interaction nets like proposal called graphic lambda calculus [8]. As my intuition was that these graphs and graph rewrites represent a sort of programs with space itself, it follows that the graphical reduction algorithm should be the most simple one. This is how I concentrated on a purely local version of graphic lambda calculus, an artificial chemistry called chemlambda (from ”chemistry” and ”lambda calculus”) which sees graphical rewrites as chemical reactions mediated by enzymes (local machines). Here artificial chemistry is used not in the usual sense [1], [17] but in the sense of asynchronous graph rewrite automata [28].

Chemlambda artificial chemistry was initially proposed with the name chemical concrete machine [9] (an allusion to Berry and Boudol chemical abstract machine [14]). A version of the graph rewrites, but without the algorithm of applications, appears with the name chemlambda in a collaboration with Kauffman [10], where we also explore interactions of knot theory with computation. Knot theory is related to this subject via the fact that idempotent right quasigroups which are also distributive are quandles, or quandles are algebraic structures which encode the Reidemeister moves (graph rewrites) of knot diagrams. Kauffman [21] [22] [25] has a long time interest in the relation between knot theory and computation. Previously with Kauffman, we proposed a model of computation based on graphic lambda calculus and actors in [11], which can be recognized as a mathematicians rediscovery of a version of Bawden [2] [3] linear graphs applications in distributed computing.

The actual version of chemlambda as an artificial chemistry is built into the first collection of programs and the associated demo pages, referenced in this library. These programs appeared from the realization that, besides the mathematician specialized interest, there may be applications of these ideas in chemistry. Lambda calculus has been used with a chemical theme, by Fontana and Buss [18] [19], in their Alchemy (algorithmic chemistry). They consider populations of lambda terms, whose members interact by lambda calculus application. Graph rewriting is of obvious interest for chemistry applications and frameworks like the Graph Grammar Library [26], which allow creation of graph rewriting as chemical reactions rules, existed. The new idea brought by chemlambda was that interaction nets like graph rewriting could be the base of interesting artificial chemistries, or why not, real chemistry. Hence the idea of a molecular computer [12] which is simply a (chemical) molecule (in this toy chemistry) which computes via random chemical rewrites, that is independently, not in a controlled medium or laboratory. The main question was if interesting computations can be done, taking as inspiration graphical reductions of lambda calculus terms, in a interaction nets asynchronous automaton. (Chemlambda however, differently from interaction nets, does have conflicting rewrites.)

The chemlambda project took a completely open turn, based on programs and demonstrations which could be validated by reproduction or by porting of the programs into other languages (the first programs used awk for the processing and d3.js for visualization). And indeed the project was validated by the other contributors which are listed in the library entry page.

5 Informal description of patterns, mols, graph rewrites

This is an informal description of a variant of linear graphs [2] [3], or port graphs [27] and their double push-out [16] graph rewriting, as they appear in the artificial chemistry experiments described in these notes.

We are concerned with graphs which have a finite number of nodes. Nodes are connected with edges via node ports. The valence of a node is the number of its node ports. Each edge connects two different node ports. Every node port is connected to an edge. Each node has a type from the list of node types. The type of a node gives the valence of the node, as well as a numbering of the node ports (from 0 to in the programs, or from 1 to ar(A) in the description).

Any graph made of a finite number of nodes, each node of a type from the list of node types, admits a mol notation. We need an infinite alphabet of symbols which we use to decorate the edges of the graph, such that every two different edges have different decorations. We arbitrarily order the nodes of the graph. Then we produce the mol notation of the graph which is a list of node items, one for each node. Each item is a list which starts with the node type, then with the edge decorations of each edge incident to a node port, in the order of the node ports.

For example the list:

describes a graph with 4 nodes, 3 of them of type A and one of type B. All nodes have valence 3. There are 6 edges, decorated with the symbols a,b,c,d,e,f. (By using the order of the nodes given by the list) we know that the edge ”a” connects the 1st port of the first node with the 2nd port of the second node, and so on until the last edge, ”f”, which connects the 2nd and 3rd ports of the 4th node.

Further we shall write node items and node words in the following format: one node item per line, without parantheses or commas, like this

Notice that each edge decoration appears exactly twice.

The mol notation of a graph is unique up to the renaming of the edges (decorations) and up to permutation of the node items. Similarly, two graphs are isomorphic if they admit the same mol notation, again up to renaming of the edges and up to permutation of the node items.

A pattern is a collection of nodes of the graph, together with the internal edges (joining the nodes of the pattern) and with their external half-edges, but seen in the same mol notation. For the example of a graph taken previously, any collection of lines in the mol notation is a pattern, like this one:

In the mol notation of a pattern, each edge decoration appears at most twice. Those edge decorations which appear once are the external half-edges.

A match of a pattern into a graph (described by its mol notation) is a pair of functions , where is an injective function from the nodes (lines in the list) of the pattern to the nodes of the graph, and is a renaming of the edge decorations, such that on those those decorations which appear twice in the pattern the renaming is injective, and on the external half-edges the renaming is at most 2-to-1.

For example if we take the pattern:

then there is a match with the graph taken as an example:

Indeed, here the function matches the 1st line of the pattern with the 2nd line of the mol notation of the graph and the 2nd line of the pattern with the 4th line of the mol notation of the graph. The function is defined as:

A graph rewrite is defined by two patterns with the same external half-edges, called the LHS and the RHS patterns of the rewrite. For our example suppose that we have the following graph rewrite:

In the usual style of DPO rewrites [16] (adapted to the mol notation) the application of the graph rewrite is a 3 parts process, where:

  1. we start from a match of the LHS pattern into the graph, for example the one considered previously,

  2. we produce an intermediary mol notation by deleting the lines in the mol notation of the graph which are in the scope of the match thus obtaining

  3. we concatenate the intermediary mol notation with a copy of the RHS pattern, such that all the internal edges have fresh decorations (not the case in our example, where the RHS does not have internal edges) and such that the external edges inherit the decoration from the match of the LHS pattern. In our example we get:

In order to allow graphs with some free edges we suppose that we always have in 1-valent nodes named ”FR*” where ”*” is a word of our choice. For example a pattern which has external half-edges, like

could be seen as the mol notation of a graph with free half-edges a, b, d, e, by adding to it 4 FR* nodes, like this:

so that now each edge decoration appears exactly twice. This completion of a pattern to a mol notation does not erase the difference between mols and patterns though.

There are two kinds of graph rewrites which we want to allow. We want to allow parallel graph rewrites which don’t share nodes, but they do share half-edges. Also we want to allow graph rewrites which delete nodes. That is why we suppose that we always have in 2-valent nodes named ”Arrow*”. Moreover we shall always add to the graph rewrite systems considered a family of graph rewrites called ”COMB”, which consist into the elimination of the Arrow* elements. For example suppose we have a graph rewrite which deletes a pair of neighboring nodes and joins the dangling half-edges. This can be done by a graph rewrite of the form

This allows to apply several such rewrites in parallel, without confusion concerning the wiring of the edges. After the rewrites are done (one or more in parallel), we end up with the following two situations. A node connected to an Arrow*, or an Arrow* with both ports connected to the same edge. The COMB rewrites are then used to erase the Arrow* elements. For any

and

6 Mathematical description of patterns, mols, graph rewrite systems

Linear graphs [2] [3], or port graphs [27] and their double push-out [16] graph rewriting, appear mostly in relation to interaction nets [24] and linear logic [20]. Here we present a variant of this formalism, with a chemical blend, as it used in these experiments.

6.1 Notations

A vector

is a function , . The support of the vector is the image of the function , that is the set of values . The arity of the vector is .

6.2 Molecules

Let be a finite, non-empty set of node types or colors and node type arity a function:

A -molecule pattern is a triple of sets , where is a -colored partition of into ordered subsets, called nodes, and is a set of unordered pairs of half-edges, called edges.

Definition 6.1

A -molecule pattern is a triple of sets , where:

(a) is a finite, non-empty set of half-edges,

(b) is a set of nodes. Each node

is colored with a and has a vector of half-edges incident to the node, of arity ,

(c) is a set of edges. Each edge is a set of two different half-edges incident to the edge,

which satisfy the conditions:

(d) for any half-edge there exists and are unique a node and an index such that ,

(e) for any half-edge there is at most one edge such that .

The half-edge is bound in if there exists an edge as in condition (e). We write . Otherwise we say that is free in , , and we define .

A molecule is a molecule pattern without free half-edges.

For completeness we accept as a molecule the empty one , with no half-edges.

We may supplement the node type arity with a node valence function, which associates to each node type a vector

with support in . We say that the port of the color is ”in” if , otherwise is ”out”.

An oriented molecule pattern is a molecule as in definition 6.1, with the following modifications: edges are ordered, they have a source and a target and sources of edges are connected to ”out” ports of nodes and targets of edges are connected to ”in” ports of nodes. More precisely, we have the following modifications of conditions (c) and (e):

(c) is a set of edges. Each edge is a pair with ,

(e) for any half-edge there is at most one edge such that or . Let be the node incident with and be the index of in . Let be the color of the node. If then . If then .

From definition 6.1 we see that a molecule pattern is equally described by the associated functions , , , and . A molecule pattern morphism is one which commutes with all these functions.

Definition 6.2

A morphism from a -molecule pattern , with associated , , , and , to another -molecule pattern , with associated , , , and , is a function

with the properties:

(a) for any , if then

(b) for any ,

(c) for any ,

(d) for any we have and

Let and be two sets of node types with associated arity functions. A connection morphism from a -molecule pattern , with associated , , , and , to another -molecule pattern is a pair

such that satisfies conditions (a), (b), (d) and , satisfy the modified condition:

(c’) for any and for any

For oriented molecule patterns there are similar definitions of morphisms and connection morphisms.

Notice that a morphism does not preserve free half-edges: it is not true, in general, that if then . Indeed, it is possible for the images of two free half-edges in to form an edge in .

If we accept among the node types 1-valent node types called FREE, or FRIN (free in) or FROUT (free out) in the case of oriented molecule patterns, then we can transform any molecule pattern with into a molecule simply by adding for any half-edge a new half-edge , a node , (or if is ”in” or is ”out”), and an edge (or if is ”in” or if is ”out”).

Remark that does not induce a functor in the category of mol patterns, because morphisms do not preserve free half-edges. However, if is isomorphic with then is isomorphic with .

6.3 The graph associated to a molecule

Let be a finite, non-empty set of colors and be a color arity function.

Definition 6.3

A -colored graph is a triple where is a finite nonempty set of nodes, and E is a set of edges. Each edge is a set of two different nodes.

If a node belongs to an edge then we say that the edge is incident to the node. If two different nodes belong to an edge then we say that the edge links the nodes.

The arity of a node is the number of edges which are incident to that node. We demand that:

for any node of the graph.

To any molecule is associated a colored graph .

Denote by the maximal arity of nodes in the molecule . Then we add to the set of colors the set (we suppose of course that is disjoint from this set) and we obtain a new set of colors . We exetnd the node type arity

to a color arity (denoted the same) defined over , with

for each .

To each node in , of arity p, we add p + 1 nodes in , the -th node colored with , for , and the last node colored with . This last node is named a center node and the other nodes are named port nodes. We add edges in from the center node to each of its port nodes, we call these internal edges. Finally, for each pair of half-edges which form an edge in , we add an external edge linking the corresponding node ports.

If is a molecule pattern with , then by definition

determines the molecule pattern up to isomorphism. In general a is a functor only from the category of molecules to the category of graphs, because, again, morphisms of pattern molecules do not preserve free half-edges.

6.4 Mol notation

The mol notation corresponds to Bawden [3] linear graph notation.

Definition 6.4

An -mol node with edge tags in the set is a vector

where is the node type of the mol node and is a vector of tags, elements of , such that any occurs at most twice.

An -mol pattern with edge tags in the set is a vector of -mol nodes, such that any occurs at most twice over all mol nodes vectors.

In the oriented version, when comes with a node valence function , an -mol pattern with edge tags in the set is mol pattern such that every tag which occurs twice, it does appear two positions (ports) which are ”in” and ”out”.

In general, the tags which occur twice in the mol pattern are said to be bounded, their set is denoted . The tags which occur once we said to be free, their set is denoted .

A mol pattern (or just mol) is a record of a molecule pattern.

Definition 6.5

Let be -molecule pattern, a set of tags in bijection with , a bijective numbering of nodes.

The mol (notation) of is the mol pattern , where for each ,

6.5 Local machines

Given a set of node types , with node type arity or node valence function in the oriented case, a set of edge tags, a finite set of special symbols and a natural number , we define a

-local machine. This is a Turing machine which has two tapes: an IO (input-output tape) and a work tape, and a set of internal states, such that the size of the work tape plus the number of possible internal states is at most

.

The tapes are made by cells, each cell can contain a node type, a tag, or a special symbol.

The IO tape has to contain at each moment only a mol pattern and special symbols, like field separators of a node vector or line separator for the mol vector. The IO tape head, after reading a cell, if it reads a tag it can then jump to the beginning of the mol node containing it, or to the cell which contains the other occurence of the tag (if it exists), or to one of the other tags from the same mol node, or to the end of mol pattern.

The machine can read from on the IO tape a cell, write a whole mol node at the end of the mol pattern, or delete a whole mol node. For each read/write or delete from/to the IO tape the machine writes the same on the work tape. The machine can read from the work tape and it can write only on the blank cells of the work tape.

When the machine halts (by writing a halt symbol on the work tape) it either deletes the whole work tape and the IO head jumps to a random mol node beginning, or the machine halts.

The machine may have a source of new tags (not already present on the IO tape) or not. The machine may have access to a random coin.

Section 8.3 explains why we may not need a source for new tags.

7 Molecules for programmers

If you are familiar with linear graphs [2] [3], or port graphs [27] and their double push-out [16] graph rewriting, then you can use this section for faster understanding of the programs in the library.

Such graphs are familiar to programmers interested in interaction nets [24] and linear logic [20]. However, in these experiments, the accent is on the use of the simplest, purely local algorithms, and not on the semantics or the high level point of view. Here we want to understand if these simplest algorithms can do something interesting in the artificial chemistries we consider.

In order to define mols (molecules) and mol patterns, we introduce mol node types and their nodeValence vectors.

7.1 Mol nodes

The mol nodes are defined in the script nodes.js.

Mol nodes types.

Let NT be a finite set of mol node types. In the script NT is in the vector autoFilter.

  1. autoFilter = [
    "L","A","FI","D","FOE","FOX","FO","T","Arrow","GAMMA","DELTA"
    ];
    

nodeValence associates to any mol node type a valence vector, with elements 0 or 1, whose length is the valence of the mol node.

  1. nodeValence = {
      "L":  [0,1,1], // (12) , 1-z
      "A":  [0,0,1], // (231), (z-1)/z
      "FI": [0,0,1], // (312), 1/(1-z)
      "D": [0,0,1],  // ()   , z
      "FOE":[0,1,1], // (23) , 1/z
      "FOX": [0,1,1], // (13) , z/(z-1)
      "FO": [0,1,1], //
      "T":  [0],
      "FRIN":[1],
      "FROUT":[0],
      "Arrow":[0,1],
    // interaction combinators
      "GAMMA":[0,0,0],
      "DELTA":[0,0,0],
    }
    

Mathematically, given the set NT of mol node types, the nodeValence is a function which associates to a mol node type ”t” the word w = nodeValence(t). Here the word w is made of letters ”0” and ”1”. The valence of the mol node type is the length of the word w.

In the definition of nodeValence we see some mol node types with comments. These mol node types can be decorated with permutations of 3 elements, or with elements of the anharmonic group. These decorations are a bridge from the present work to emergent algebras (see for example arXiv:1807.02058) which are ”commutative” in the sense that they satisfy a rewrite called ”the shuffle trick”.

Here we are going to use this correspondence only heuristically, for example to choose the form of the rewrites ”DIST”, which increase the number of nodes. See more about this correspondence at:

  1. the tool to choose DIST rewrites

  2. the commented js script rhs.js

A detailed explanation of the relation with commutative emergent algebras will appear in the future.

Mol nodes.

Given a nonempty, finite set of edge tags E, a mol node whose ports are E-decorated is a vector (t, ) where:

  1. t is a mol node type, called the type of the mol node

  2. is a word over the alphabet E, such that:

    1. any letter from E appears at most twice,

    2. the length of equals the valence of t.

For a mol node we shall use a notation like

  1. L a b c
    

where ”L” is a mol node type and ”abc” is the word with letters from an alphabet E which contains a, b, c. You can see that the mol node type L has nodeValence vector [0,1,1], whose length is 3, equal to the length of the word abc.

7.2 Mol patterns

A mol pattern (which is E-decorated) is a vector of mol nodes, such that any letter from E appears at most twice over all mol nodes.

Any letter which appears exactly once in a mol pattern P, is called a free edge. The set of free edges of the mol pattern P is denoted by Free(P). The set of edges of the mol pattern P which are not free is denoted by Bound(P).

A mol (which is E-decorated), is a mol pattern without free edges.

For mol patterns, in particular for mols, we use a notation like

  1. L a b c
    A c d e
    

which is amenable to a record with a line separator (here we use newline) which separates the record into lines. Each line is a mol node, itself a record with a field separator (here we use space, therefore we can’t admit space or newline in the alphabet E).

Such records are available in iceMol.js, in the form of the function molLibrary(). In those records the line separator is ””̂.

Note that any mol pattern can be turned into a mol by adding some new mol nodes ”FRIN” (i.e. ”free in”) and ”FROUT” (i.e. ”free out”).

7.3 Molecules

A molecule is an equivalence class of mol patterns, up to the reordering of mol nodes and up to renaming of the edge decorations. This equivalence is defined in terms of morphisms of mol patterns.

Consider two mol patterns:

  1. , which is E-decorated,

  2. , which is F-decorated.

A morphism from P to Q is a pair of functions , where and . The function induces a function which transforms a word into the word which is obtained by replacing each letter of with the letter . The pair of functions satisfies the property: for every we have

  1. is injective

  2. restricted to takes values in and is injective

  3. restricted to is at most 2-to-1.

In this category of mol patterns, two of them are equivalent if they are isomorphic.

Part of the condition (d) (that takes bound edge decorations to bound edge decorations) and the condition (e) are needed, even if they seem overly complex. In fact we shall identify molecules with decorated graphs as made of half-edges. The mentioned part of the condition (d) means just that the morphism preserves edges of these graphs. The condition (e) allows pairs of half-edges which are not part of edges in (the graph associated to) P to be transformed in pairs of half-edges which form an edge (in the graph associated to) Q.

At the level of the library of programs, we have to be sure that our programs respect this equivalence relation.

A mol pattern contains more information than a molecule, but this information is not geometrical and has to be destroyed. One of the means to keep only the geometrical information is to use random permutations (or morphisms) where needed in the algorithms.

7.4 Molecules as graphs

A molecule can be turned into a graph. This means we need a conversion from a mol pattern to a graph such that two isomorphic mol patterns are converted into the same graph. In order to understand the conversion (done by functions in ioprep.js), we have to understand the relation between mol patterns and graphs.

A graph is pair (G,E) where G is a finite nonempty set of nodes and E is a set of edges. Each edge is a set of two nodes (therefore the nodes have to be different). We say that an edge links the two nodes. There are no edges which connect a node with itself.

An oriented graph is a different mathematical object. It is a pair of functions (source, target) defined on a set E of edges, with values in a set G of nodes. An oriented graph admits edges with the source and target being the same node. If an oriented graph does not have such edges, then it can be turned into a graph (as defined previously) by associating to each edge e the set formed by the nodes source(e) and target(e).

A graph can also be seen as a collection of half-edges, according to the following definition. A graph is a finite set of H of half-edges, with a set N of disjoint sets of half-edges, called nodes, and a set E of disjoint unordered pairs of half-edges, called edges.

We shall use for our graphs the following modification of a graph as a collection of half-edges. Let C be a set of colors. A C-decorated graph is:

  1. a finite collection of half-edges H,

  2. with a set N of nodes, where each node is a vector where is the arity of the node and are half-edges, such that a half-edge appears exactly once in one of the nodes,

  3. and with a set of unordered pairs of half-edges, called edges.

We transform a C-decorated graph G into a unoriented usual graph G’ with colored nodes. Denote by the maximal arity of nodes in G. Then we add to the set of colors the set (we suppose of course that is disjoint from this set) and we obtain a new set of colors . To each node in G, of arity p, we add p + 1 nodes in G’, the -th node colored with , for , and the last node colored with . This last node is named a center node and the other nodes are named port nodes. We add edges in G’ from the center node to each of its port nodes, we call these internal edges. Finally, for each pair of half-edges which form an edge in G, we add an external edge linking the corresponding node ports.

d3 graphs are oriented graphs, in the format used in the js library d3.js. See more about d3 graphs in myD3Graph.js. A d3 graph is given as a pair of vectors, called nodes and links.

The nodes vector has elements, each one (node) is an object

  1. {"id": id,
    "type": type,
    x: x, y: y,
    vx:0, vy:0,
    links:[],
    "age":age}
    

where ”id” is the identity (index of the node in the nodes vector), ”type” is the graph node type, x, y, vx, vy are the coordinates and velocities (used in the force graph simulation done by d3), links is a vector of links (i.e. edges) connected with the present node. ”age” is the age of the node, used in some of the experiments. Possibly other fields may be added.

The links vector describes the edges of the oriented graph. Each links element is an object

  1. {"source": nsource,
    "target": ntarget,
    "value": value,
    "age":age}
    

where ”source” is the node (index) of the source node, ”target” is the node (index) of the target node and ”value” is a number which is used for the width of the link (edge) as drawn by d3.js. Internal edges (those from the center node to its port nodes) and the external edges will have different width. The paramater ”age” is the age of the link, used in some experiments. Possibly other fields may be added.

An oriented graph structure. like these d3 graphs, do contain non-geometrical information, like the one we mentioned previously concerning mol patterns. We have to be careful to not use this information, or to destroy it somehow, in the algorithms.

Here we use d3 graphs as usual graphs, in the sense that we don’t treat edges as oriented. (At the level of the programs, for example starting with a node, we may search for the other nodes which are linked to this one, irrespective to the fact that they are sources or targets). This is done in the following way.

7.4.1 Graph nodes

Graph nodes types.

We introduce a set GT of graph node types. Further we exploit the fact that here we use mol node types with valence at most 3 (but the extension to any valence should be obvious).

If NT is the set of mol node types, then GT is obtained from NT by adding 3 new types:

  1. ”in”, ”middle”, ”out”.

We also have a predicate isCenter (defined in myD3Graph.js), which is true for any graph node type which is not ”in”, ”middle” or ”out”. We say that a graph node is a center if it has a graph node type for which isCenter is true.

A node which is not a center is called a port node.

GT is kept in the vector graphNodes, in the script nodes.js.

  1. graphNodes = [
    "in","out","middle",
    "L","A","FI","D","FOE","FOX","FO",
    "T","FRIN","FROUT","Arrow",
    "GAMMA","DELTA"
    ];
    

Because we want to represent graphically the nodes, we associate colors to each graph node type. Remark that we don’t use a bijection from the graph node types to the colors.

Each graph node (which we are going to define from a mol pattern) is graphically represented by a colored circle. The radius of the circle is a function of the predicate isCenter, that is center nodes have a radius and the port nodes have a different radius.

From mol nodes to graph nodes.

To each mol node we associate a GT-decorated graph. The association is the following.

There is a center node according to the mol node type. There are port nodes, one for each letter of the mol node word. Here we exploit the fact that we use mol nodes of valence at most 3.

The function which associates a vector of graph node types (among ”in”, ”out” or ”middle”) to the valence of a mol node is nodePortTypes, in the script nodes.js.

  1. nodePortTypes = [
    ["in"],
    ["in","out"],
    ["in","middle","out"]
    ];
    

So the first port node is always ”in”, the last one (for valence at least 2) is always ”out”, the remaining port is ”middle”.

The graph associated to a mol node is formed by the center node and the port nodes, with edges from the center nodes to the port nodes. Specifically, we draw a center graph node as an ”o” and a port node as a dash ”—” with a number (starting from 1).

Then a mol node of valence 1, of mol node type ”t” and edge ”a”

  1. t a
    

becomes a graph as in figure 1.

Figure 1: The graph associated to a 1-valent mol node

A mol node of valence 2, of mol node type ”t” and edges ”ab”

  1. t a b
    

becomes a graph as in figure 2.

Figure 2: The graph associated to a 2-valent mol node

A mol node of valence 3, of mol type ”t” and edges ”abc”

  1. t a b c
    

becomes a graph as in figure 3.

Figure 3: The graph associated to a 3-valent mol node

7.4.2 Mol patterns as graphs

To a mol pattern we associate the graph formed by (the graphs of) all mol nodes, with edges connecting port nodes which have the same edge tag. The definition of a mol pattern assures us that this is possible, because each edge tag cannot occur more than twice.

Let us come back to the d3 graphs, which are oriented. The orientation (i.e. which node is the source and which one is the target) will be neglected. This is possible because the graphs which we obtain do not have edges which connect a node with itself. Indeed, even if an edge tag appears twice in the same mol node, the corresponding edge connects different node ports.

8 Rewrites

A rewrite is an object, like

  1. {
    left:"A",right:"FO",             \\ the nodes types of the LHS pattern
    action:"DIST1",                  \\ the action name
    named:"A-FO",                    \\ the name of the rewrite
    t1:"FOE",t2:"FOE",t3:"A",t4:"A", \\ the node types of the RHS pattern
    blocks:["FOE-A"],                \\ patterns which have LHS in this RHS
    kind:"DIST"                      \\ the general type of rewrite
    }
    

8.1 Rewrites of mol patterns

A mol pattern rewrite is a double push-out [16] rewrite. In this case of mol patterns, a general rewrite is defined by a pair of mol patterns , with the property that

is called the left hand side pattern and is called the right hand side pattern. is the interface.

Given a mol pattern and a rewrite , an application of the rewrite consists of:

  1. a morphism from to , with image

  2. a morphism from to , with image , such that

  3. from the mol pattern M are eliminated (spliced) the mol nodes of

  4. the mol modes of are added to the result.

This definition of a rewrite application has to be backed by algorithms which can be implemented by local machines (section 6.5):

  1. from and output

  2. from , , and output

  3. from , and output , the mol pattern after the rewrite.

Among these algorithm, (c) is clear, (a) is a matching algorithm and (b) is more problematic because of the condition (var2), which asks for a way to produce new bound edges, i.e. new names which are not in . As (c) is trivial, we may think about it as the final part of the algorithm (b), thus we have:

  1. match algorithm: from and output

  2. rewrite algorithm: from , , and output ,then replace by in . Output , the mol pattern after the rewrite.

It is also interesting to consider a finite collection of general rewrites

Algorithms and for all can be merged into one (a) algorithm and (b) algorithm. This can be done by abstracting over the node types. Looking back at the definition of molecules as equivalence classes of mol patterns, we could consider a larger equivalence relation, by admitting mol pattern morphisms which do not preserve mol node types. Instead we might treat mol node types as we treated edges tags, perhaps retaining only nodeValence of the node type (or only the nodes arities).

The most general formalism may turn out to be much more verbose than what we need. That is why let’s make only the following definition: a connection morphism between two mol patterns

  1. , which is E-decorated, with mol node types in NT,

  2. , which is F-decorated, with mol node types in NT’,

is a triple of functions , where , and with the properties: for every we have

  1. and are injective

  2. restricted to takes values in and is injective

  3. restricted to is at most 2-to-1.

A connection pattern is then an equivalence class of mol patterns up to connection isomorphisms.

Connection patterns rewrites are defined like mol patterns rewrites. The advantage is that we may compress a finite collection of general mol pattern rewrites into a much smaller collection of connection patterns rewrites. For a general connection pattern rewrite we shall use the name ”action” (which explains the field ”action” in the description of a rewrite).

At the action level, all LHS patterns we shall consider are of the form.

  1. n1type e ...
    n2type ...e...
    

where n1type and n2type are the types of the nodes. To identify such a LHS pattern we need to specify

  1. left:n2type, right:n1type,    \\ the two nodes types of the LHS pattern
    named:n2type + "-" + n1type,  \\ the name of the rewrite
    

Here the ”left” and ”right” node type come from the image that the edge ”e” is oriented from left to right, with the mol node of type n1type at right.

This notation for a LHS pattern supposes that there is only one connection pattern for a pair of node types n1type, n2type. The connection pattern will appear as a predicate in the algorithm (a).

Each action (i.e. connection pattern rewrite) is identified by a name, for our example:

  1. action:"DIST1",                  \\ the action name
    

Finally, the RHS patterns are also given via their connection patterns, for our example:

  1. t1:"FOE",t2:"FOE",t3:"A",t4:"A", \\ the node types of the RHS pattern
    

The algorithm (b) takes as input:

  1. the mol pattern , given by the algorithm (a)

  2. the action name (which gives the connection pattern rewrite) and the node types needed to build the pattern.

The problem of how to generate new names which are not in remains.

8.2 Graph rewrites

At the graph level, the connection pattern

  1. n1type e ...
    n2type ...e...
    

translates into a center node of type , whose first node port is connected to a node port of a center node of type . The match algorithm (a) is the function findTransform(n1) from chemistry.js, where n1 is the center node of type . The function findAllTransforms() uses the previous function for all center nodes to make a vector of all possible graph rewrites.

The algorithm (b) is embodied by the function doTransform(n1, trans)from chemistry.js, which takes as input the center node n1 and a matching graph rewrite trans. Instead of generating new names (of nodes or edges), the algorithm delegates this to d3.js part, by using functions like addNodeAndEdges, or removeNodeAndEdges, which are defined in myD3Graph.js.

8.3 A solution of the problem of new names

Project Hapax gives such a solution, described here.

The idea is simple: we can exclude the need of new names if we reformulate all rewrites as permutations of edges. As an example, let’s consider the rewrite ”L-A”, as it appears in chemlambda (and which is a well known graphical version of the rewrite). In terms of mol patterns, this rewrite transforms the

  1. L c b a
    A a d e
    

into the

  1. Arrow c e
    Arrow d b
    

(Here we don’t need new edge names, but the example is easy to follow.)

This rewrite can be reformulated as: transform the new pattern

  1. L c b a
    A a d e
    Arrow b’ a’
    Arrow a’ b’
    

into the new pattern

  1. L b’ a b’
    A a’ a a’
    Arrow c e
    Arrow d b
    

This can be further restated as a chemical reaction:

  1. L c b a       Arrow b’ a’        Arrow c e       L b’ a b’
    A a d e   +   Arrow a’ b’   -->  Arrow d b   +   A a’ a a’
    

which has the form

  1. LHS   +   Token1   -->  RHS   +   Token2
    

Here and are 2-mol nodes patterns which have to be added to the rewrite in order to make it conservative.

The solution thus transfers the problem of generating new names to the problem of generating new tokens. Or, we know how to reliably generate new tokens. We may imagine that , are money tokens and that each rewrite involves a cost (but mind that all it really matters is that there exist well known ways to generate new tokens in a decentralized way).

References